]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/net/ps3_gelic_net.c
PS3: gelic: remove duplicated ethtool handlers
[net-next-2.6.git] / drivers / net / ps3_gelic_net.c
1 /*
2  *  PS3 gelic network driver.
3  *
4  * Copyright (C) 2007 Sony Computer Entertainment Inc.
5  * Copyright 2006, 2007 Sony Corporation
6  *
7  * This file is based on: spider_net.c
8  *
9  * (C) Copyright IBM Corp. 2005
10  *
11  * Authors : Utz Bacher <utz.bacher@de.ibm.com>
12  *           Jens Osterkamp <Jens.Osterkamp@de.ibm.com>
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2, or (at your option)
17  * any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27  */
28
29 #undef DEBUG
30
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33
34 #include <linux/etherdevice.h>
35 #include <linux/ethtool.h>
36 #include <linux/if_vlan.h>
37
38 #include <linux/in.h>
39 #include <linux/ip.h>
40 #include <linux/tcp.h>
41
42 #include <linux/dma-mapping.h>
43 #include <net/checksum.h>
44 #include <asm/firmware.h>
45 #include <asm/ps3.h>
46 #include <asm/lv1call.h>
47
48 #include "ps3_gelic_net.h"
49
50 #define DRV_NAME "Gelic Network Driver"
51 #define DRV_VERSION "1.0"
52
53 MODULE_AUTHOR("SCE Inc.");
54 MODULE_DESCRIPTION("Gelic Network driver");
55 MODULE_LICENSE("GPL");
56
57 static inline struct device *ctodev(struct gelic_card *card)
58 {
59         return &card->dev->core;
60 }
61 static inline u64 bus_id(struct gelic_card *card)
62 {
63         return card->dev->bus_id;
64 }
65 static inline u64 dev_id(struct gelic_card *card)
66 {
67         return card->dev->dev_id;
68 }
69
70 /* set irq_mask */
71 static int gelic_card_set_irq_mask(struct gelic_card *card, u64 mask)
72 {
73         int status;
74
75         status = lv1_net_set_interrupt_mask(bus_id(card), dev_id(card),
76                                             mask, 0);
77         if (status)
78                 dev_info(ctodev(card),
79                          "lv1_net_set_interrupt_mask failed %d\n", status);
80         return status;
81 }
82 static inline void gelic_card_rx_irq_on(struct gelic_card *card)
83 {
84         gelic_card_set_irq_mask(card, card->ghiintmask | GELIC_CARD_RXINT);
85 }
86 static inline void gelic_card_rx_irq_off(struct gelic_card *card)
87 {
88         gelic_card_set_irq_mask(card, card->ghiintmask & ~GELIC_CARD_RXINT);
89 }
90 /**
91  * gelic_descr_get_status -- returns the status of a descriptor
92  * @descr: descriptor to look at
93  *
94  * returns the status as in the dmac_cmd_status field of the descriptor
95  */
96 static enum gelic_descr_dma_status
97 gelic_descr_get_status(struct gelic_descr *descr)
98 {
99         return be32_to_cpu(descr->dmac_cmd_status) & GELIC_DESCR_DMA_STAT_MASK;
100 }
101
102 /**
103  * gelic_descr_set_status -- sets the status of a descriptor
104  * @descr: descriptor to change
105  * @status: status to set in the descriptor
106  *
107  * changes the status to the specified value. Doesn't change other bits
108  * in the status
109  */
110 static void gelic_descr_set_status(struct gelic_descr *descr,
111                                    enum gelic_descr_dma_status status)
112 {
113         descr->dmac_cmd_status = cpu_to_be32(status |
114                 (be32_to_cpu(descr->dmac_cmd_status) &
115                  ~GELIC_DESCR_DMA_STAT_MASK));
116         /*
117          * dma_cmd_status field is used to indicate whether the descriptor
118          * is valid or not.
119          * Usually caller of this function wants to inform that to the
120          * hardware, so we assure here the hardware sees the change.
121          */
122         wmb();
123 }
124
125 /**
126  * gelic_card_free_chain - free descriptor chain
127  * @card: card structure
128  * @descr_in: address of desc
129  */
130 static void gelic_card_free_chain(struct gelic_card *card,
131                                   struct gelic_descr *descr_in)
132 {
133         struct gelic_descr *descr;
134
135         for (descr = descr_in; descr && descr->bus_addr; descr = descr->next) {
136                 dma_unmap_single(ctodev(card), descr->bus_addr,
137                                  GELIC_DESCR_SIZE, DMA_BIDIRECTIONAL);
138                 descr->bus_addr = 0;
139         }
140 }
141
142 /**
143  * gelic_card_init_chain - links descriptor chain
144  * @card: card structure
145  * @chain: address of chain
146  * @start_descr: address of descriptor array
147  * @no: number of descriptors
148  *
149  * we manage a circular list that mirrors the hardware structure,
150  * except that the hardware uses bus addresses.
151  *
152  * returns 0 on success, <0 on failure
153  */
154 static int gelic_card_init_chain(struct gelic_card *card,
155                                  struct gelic_descr_chain *chain,
156                                  struct gelic_descr *start_descr, int no)
157 {
158         int i;
159         struct gelic_descr *descr;
160
161         descr = start_descr;
162         memset(descr, 0, sizeof(*descr) * no);
163
164         /* set up the hardware pointers in each descriptor */
165         for (i = 0; i < no; i++, descr++) {
166                 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
167                 descr->bus_addr =
168                         dma_map_single(ctodev(card), descr,
169                                        GELIC_DESCR_SIZE,
170                                        DMA_BIDIRECTIONAL);
171
172                 if (!descr->bus_addr)
173                         goto iommu_error;
174
175                 descr->next = descr + 1;
176                 descr->prev = descr - 1;
177         }
178         /* make them as ring */
179         (descr - 1)->next = start_descr;
180         start_descr->prev = (descr - 1);
181
182         /* chain bus addr of hw descriptor */
183         descr = start_descr;
184         for (i = 0; i < no; i++, descr++) {
185                 descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
186         }
187
188         chain->head = start_descr;
189         chain->tail = start_descr;
190
191         /* do not chain last hw descriptor */
192         (descr - 1)->next_descr_addr = 0;
193
194         return 0;
195
196 iommu_error:
197         for (i--, descr--; 0 <= i; i--, descr--)
198                 if (descr->bus_addr)
199                         dma_unmap_single(ctodev(card), descr->bus_addr,
200                                          GELIC_DESCR_SIZE,
201                                          DMA_BIDIRECTIONAL);
202         return -ENOMEM;
203 }
204
205 /**
206  * gelic_descr_prepare_rx - reinitializes a rx descriptor
207  * @card: card structure
208  * @descr: descriptor to re-init
209  *
210  * return 0 on succes, <0 on failure
211  *
212  * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
213  * Activate the descriptor state-wise
214  */
215 static int gelic_descr_prepare_rx(struct gelic_card *card,
216                                       struct gelic_descr *descr)
217 {
218         int offset;
219         unsigned int bufsize;
220
221         if (gelic_descr_get_status(descr) !=  GELIC_DESCR_DMA_NOT_IN_USE)
222                 dev_info(ctodev(card), "%s: ERROR status \n", __func__);
223
224         /* we need to round up the buffer size to a multiple of 128 */
225         bufsize = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN);
226
227         /* and we need to have it 128 byte aligned, therefore we allocate a
228          * bit more */
229         descr->skb = netdev_alloc_skb(card->netdev,
230                 bufsize + GELIC_NET_RXBUF_ALIGN - 1);
231         if (!descr->skb) {
232                 descr->buf_addr = 0; /* tell DMAC don't touch memory */
233                 dev_info(ctodev(card),
234                          "%s:allocate skb failed !!\n", __func__);
235                 return -ENOMEM;
236         }
237         descr->buf_size = cpu_to_be32(bufsize);
238         descr->dmac_cmd_status = 0;
239         descr->result_size = 0;
240         descr->valid_size = 0;
241         descr->data_error = 0;
242
243         offset = ((unsigned long)descr->skb->data) &
244                 (GELIC_NET_RXBUF_ALIGN - 1);
245         if (offset)
246                 skb_reserve(descr->skb, GELIC_NET_RXBUF_ALIGN - offset);
247         /* io-mmu-map the skb */
248         descr->buf_addr = cpu_to_be32(dma_map_single(ctodev(card),
249                                                      descr->skb->data,
250                                                      GELIC_NET_MAX_MTU,
251                                                      DMA_FROM_DEVICE));
252         if (!descr->buf_addr) {
253                 dev_kfree_skb_any(descr->skb);
254                 descr->skb = NULL;
255                 dev_info(ctodev(card),
256                          "%s:Could not iommu-map rx buffer\n", __func__);
257                 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
258                 return -ENOMEM;
259         } else {
260                 gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
261                 return 0;
262         }
263 }
264
265 /**
266  * gelic_card_release_rx_chain - free all skb of rx descr
267  * @card: card structure
268  *
269  */
270 static void gelic_card_release_rx_chain(struct gelic_card *card)
271 {
272         struct gelic_descr *descr = card->rx_chain.head;
273
274         do {
275                 if (descr->skb) {
276                         dma_unmap_single(ctodev(card),
277                                          be32_to_cpu(descr->buf_addr),
278                                          descr->skb->len,
279                                          DMA_FROM_DEVICE);
280                         descr->buf_addr = 0;
281                         dev_kfree_skb_any(descr->skb);
282                         descr->skb = NULL;
283                         gelic_descr_set_status(descr,
284                                                GELIC_DESCR_DMA_NOT_IN_USE);
285                 }
286                 descr = descr->next;
287         } while (descr != card->rx_chain.head);
288 }
289
290 /**
291  * gelic_card_fill_rx_chain - fills descriptors/skbs in the rx chains
292  * @card: card structure
293  *
294  * fills all descriptors in the rx chain: allocates skbs
295  * and iommu-maps them.
296  * returns 0 on success, < 0 on failure
297  */
298 static int gelic_card_fill_rx_chain(struct gelic_card *card)
299 {
300         struct gelic_descr *descr = card->rx_chain.head;
301         int ret;
302
303         do {
304                 if (!descr->skb) {
305                         ret = gelic_descr_prepare_rx(card, descr);
306                         if (ret)
307                                 goto rewind;
308                 }
309                 descr = descr->next;
310         } while (descr != card->rx_chain.head);
311
312         return 0;
313 rewind:
314         gelic_card_release_rx_chain(card);
315         return ret;
316 }
317
318 /**
319  * gelic_card_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
320  * @card: card structure
321  *
322  * returns 0 on success, < 0 on failure
323  */
324 static int gelic_card_alloc_rx_skbs(struct gelic_card *card)
325 {
326         struct gelic_descr_chain *chain;
327         int ret;
328         chain = &card->rx_chain;
329         ret = gelic_card_fill_rx_chain(card);
330         chain->head = card->rx_top->prev; /* point to the last */
331         return ret;
332 }
333
334 /**
335  * gelic_descr_release_tx - processes a used tx descriptor
336  * @card: card structure
337  * @descr: descriptor to release
338  *
339  * releases a used tx descriptor (unmapping, freeing of skb)
340  */
341 static void gelic_descr_release_tx(struct gelic_card *card,
342                             struct gelic_descr *descr)
343 {
344         struct sk_buff *skb = descr->skb;
345
346 #ifdef DEBUG
347         BUG_ON(!(be32_to_cpu(descr->data_status) &
348                  (1 << GELIC_DESCR_TX_DMA_FRAME_TAIL)));
349 #endif
350         dma_unmap_single(ctodev(card),
351                          be32_to_cpu(descr->buf_addr), skb->len, DMA_TO_DEVICE);
352         dev_kfree_skb_any(skb);
353
354         descr->buf_addr = 0;
355         descr->buf_size = 0;
356         descr->next_descr_addr = 0;
357         descr->result_size = 0;
358         descr->valid_size = 0;
359         descr->data_status = 0;
360         descr->data_error = 0;
361         descr->skb = NULL;
362
363         /* set descr status */
364         gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
365 }
366
367 /**
368  * gelic_card_release_tx_chain - processes sent tx descriptors
369  * @card: adapter structure
370  * @stop: net_stop sequence
371  *
372  * releases the tx descriptors that gelic has finished with
373  */
374 static void gelic_card_release_tx_chain(struct gelic_card *card, int stop)
375 {
376         struct gelic_descr_chain *tx_chain;
377         enum gelic_descr_dma_status status;
378         int release = 0;
379
380         for (tx_chain = &card->tx_chain;
381              tx_chain->head != tx_chain->tail && tx_chain->tail;
382              tx_chain->tail = tx_chain->tail->next) {
383                 status = gelic_descr_get_status(tx_chain->tail);
384                 switch (status) {
385                 case GELIC_DESCR_DMA_RESPONSE_ERROR:
386                 case GELIC_DESCR_DMA_PROTECTION_ERROR:
387                 case GELIC_DESCR_DMA_FORCE_END:
388                         if (printk_ratelimit())
389                                 dev_info(ctodev(card),
390                                          "%s: forcing end of tx descriptor " \
391                                          "with status %x\n",
392                                          __func__, status);
393                         card->netdev->stats.tx_dropped++;
394                         break;
395
396                 case GELIC_DESCR_DMA_COMPLETE:
397                         if (tx_chain->tail->skb) {
398                                 card->netdev->stats.tx_packets++;
399                                 card->netdev->stats.tx_bytes +=
400                                         tx_chain->tail->skb->len;
401                         }
402                         break;
403
404                 case GELIC_DESCR_DMA_CARDOWNED:
405                         /* pending tx request */
406                 default:
407                         /* any other value (== GELIC_DESCR_DMA_NOT_IN_USE) */
408                         if (!stop)
409                                 goto out;
410                 }
411                 gelic_descr_release_tx(card, tx_chain->tail);
412                 release ++;
413         }
414 out:
415         if (!stop && release)
416                 netif_wake_queue(card->netdev);
417 }
418
419 /**
420  * gelic_net_set_multi - sets multicast addresses and promisc flags
421  * @netdev: interface device structure
422  *
423  * gelic_net_set_multi configures multicast addresses as needed for the
424  * netdev interface. It also sets up multicast, allmulti and promisc
425  * flags appropriately
426  */
427 static void gelic_net_set_multi(struct net_device *netdev)
428 {
429         struct gelic_card *card = netdev_priv(netdev);
430         struct dev_mc_list *mc;
431         unsigned int i;
432         uint8_t *p;
433         u64 addr;
434         int status;
435
436         /* clear all multicast address */
437         status = lv1_net_remove_multicast_address(bus_id(card), dev_id(card),
438                                                   0, 1);
439         if (status)
440                 dev_err(ctodev(card),
441                         "lv1_net_remove_multicast_address failed %d\n",
442                         status);
443         /* set broadcast address */
444         status = lv1_net_add_multicast_address(bus_id(card), dev_id(card),
445                                                GELIC_NET_BROADCAST_ADDR, 0);
446         if (status)
447                 dev_err(ctodev(card),
448                         "lv1_net_add_multicast_address failed, %d\n",
449                         status);
450
451         if (netdev->flags & IFF_ALLMULTI
452                 || netdev->mc_count > GELIC_NET_MC_COUNT_MAX) { /* list max */
453                 status = lv1_net_add_multicast_address(bus_id(card),
454                                                        dev_id(card),
455                                                        0, 1);
456                 if (status)
457                         dev_err(ctodev(card),
458                                 "lv1_net_add_multicast_address failed, %d\n",
459                                 status);
460                 return;
461         }
462
463         /* set multicast address */
464         for (mc = netdev->mc_list; mc; mc = mc->next) {
465                 addr = 0;
466                 p = mc->dmi_addr;
467                 for (i = 0; i < ETH_ALEN; i++) {
468                         addr <<= 8;
469                         addr |= *p++;
470                 }
471                 status = lv1_net_add_multicast_address(bus_id(card),
472                                                        dev_id(card),
473                                                        addr, 0);
474                 if (status)
475                         dev_err(ctodev(card),
476                                 "lv1_net_add_multicast_address failed, %d\n",
477                                 status);
478         }
479 }
480
481 /**
482  * gelic_card_enable_rxdmac - enables the receive DMA controller
483  * @card: card structure
484  *
485  * gelic_card_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
486  * in the GDADMACCNTR register
487  */
488 static inline void gelic_card_enable_rxdmac(struct gelic_card *card)
489 {
490         int status;
491
492         status = lv1_net_start_rx_dma(bus_id(card), dev_id(card),
493                                 card->rx_chain.tail->bus_addr, 0);
494         if (status)
495                 dev_info(ctodev(card),
496                          "lv1_net_start_rx_dma failed, status=%d\n", status);
497 }
498
499 /**
500  * gelic_card_disable_rxdmac - disables the receive DMA controller
501  * @card: card structure
502  *
503  * gelic_card_disable_rxdmac terminates processing on the DMA controller by
504  * turing off DMA and issueing a force end
505  */
506 static inline void gelic_card_disable_rxdmac(struct gelic_card *card)
507 {
508         int status;
509
510         /* this hvc blocks until the DMA in progress really stopped */
511         status = lv1_net_stop_rx_dma(bus_id(card), dev_id(card), 0);
512         if (status)
513                 dev_err(ctodev(card),
514                         "lv1_net_stop_rx_dma faild, %d\n", status);
515 }
516
517 /**
518  * gelic_card_disable_txdmac - disables the transmit DMA controller
519  * @card: card structure
520  *
521  * gelic_card_disable_txdmac terminates processing on the DMA controller by
522  * turing off DMA and issueing a force end
523  */
524 static inline void gelic_card_disable_txdmac(struct gelic_card *card)
525 {
526         int status;
527
528         /* this hvc blocks until the DMA in progress really stopped */
529         status = lv1_net_stop_tx_dma(bus_id(card), dev_id(card), 0);
530         if (status)
531                 dev_err(ctodev(card),
532                         "lv1_net_stop_tx_dma faild, status=%d\n", status);
533 }
534
535 /**
536  * gelic_net_stop - called upon ifconfig down
537  * @netdev: interface device structure
538  *
539  * always returns 0
540  */
541 static int gelic_net_stop(struct net_device *netdev)
542 {
543         struct gelic_card *card = netdev_priv(netdev);
544
545         napi_disable(&card->napi);
546         netif_stop_queue(netdev);
547
548         /* turn off DMA, force end */
549         gelic_card_disable_rxdmac(card);
550         gelic_card_disable_txdmac(card);
551
552         gelic_card_set_irq_mask(card, 0);
553
554         /* disconnect event port */
555         free_irq(card->netdev->irq, card->netdev);
556         ps3_sb_event_receive_port_destroy(card->dev, card->netdev->irq);
557         card->netdev->irq = NO_IRQ;
558
559         netif_carrier_off(netdev);
560
561         /* release chains */
562         gelic_card_release_tx_chain(card, 1);
563         gelic_card_release_rx_chain(card);
564
565         gelic_card_free_chain(card, card->tx_top);
566         gelic_card_free_chain(card, card->rx_top);
567
568         return 0;
569 }
570
571 /**
572  * gelic_card_get_next_tx_descr - returns the next available tx descriptor
573  * @card: device structure to get descriptor from
574  *
575  * returns the address of the next descriptor, or NULL if not available.
576  */
577 static struct gelic_descr *
578 gelic_card_get_next_tx_descr(struct gelic_card *card)
579 {
580         if (!card->tx_chain.head)
581                 return NULL;
582         /*  see if the next descriptor is free */
583         if (card->tx_chain.tail != card->tx_chain.head->next &&
584             gelic_descr_get_status(card->tx_chain.head) ==
585             GELIC_DESCR_DMA_NOT_IN_USE)
586                 return card->tx_chain.head;
587         else
588                 return NULL;
589
590 }
591
592 /**
593  * gelic_descr_set_tx_cmdstat - sets the tx descriptor command field
594  * @descr: descriptor structure to fill out
595  * @skb: packet to consider
596  *
597  * fills out the command and status field of the descriptor structure,
598  * depending on hardware checksum settings. This function assumes a wmb()
599  * has executed before.
600  */
601 static void gelic_descr_set_tx_cmdstat(struct gelic_descr *descr,
602                                        struct sk_buff *skb)
603 {
604         if (skb->ip_summed != CHECKSUM_PARTIAL)
605                 descr->dmac_cmd_status =
606                         cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
607                                     GELIC_DESCR_TX_DMA_FRAME_TAIL);
608         else {
609                 /* is packet ip?
610                  * if yes: tcp? udp? */
611                 if (skb->protocol == htons(ETH_P_IP)) {
612                         if (ip_hdr(skb)->protocol == IPPROTO_TCP)
613                                 descr->dmac_cmd_status =
614                                 cpu_to_be32(GELIC_DESCR_DMA_CMD_TCP_CHKSUM |
615                                             GELIC_DESCR_TX_DMA_FRAME_TAIL);
616
617                         else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
618                                 descr->dmac_cmd_status =
619                                 cpu_to_be32(GELIC_DESCR_DMA_CMD_UDP_CHKSUM |
620                                             GELIC_DESCR_TX_DMA_FRAME_TAIL);
621                         else    /*
622                                  * the stack should checksum non-tcp and non-udp
623                                  * packets on his own: NETIF_F_IP_CSUM
624                                  */
625                                 descr->dmac_cmd_status =
626                                 cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
627                                             GELIC_DESCR_TX_DMA_FRAME_TAIL);
628                 }
629         }
630 }
631
632 static inline struct sk_buff *gelic_put_vlan_tag(struct sk_buff *skb,
633                                                  unsigned short tag)
634 {
635         struct vlan_ethhdr *veth;
636         static unsigned int c;
637
638         if (skb_headroom(skb) < VLAN_HLEN) {
639                 struct sk_buff *sk_tmp = skb;
640                 pr_debug("%s: hd=%d c=%ud\n", __func__, skb_headroom(skb), c);
641                 skb = skb_realloc_headroom(sk_tmp, VLAN_HLEN);
642                 if (!skb)
643                         return NULL;
644                 dev_kfree_skb_any(sk_tmp);
645         }
646         veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN);
647
648         /* Move the mac addresses to the top of buffer */
649         memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN);
650
651         veth->h_vlan_proto = __constant_htons(ETH_P_8021Q);
652         veth->h_vlan_TCI = htons(tag);
653
654         return skb;
655 }
656
657 /**
658  * gelic_descr_prepare_tx - get dma address of skb_data
659  * @card: card structure
660  * @descr: descriptor structure
661  * @skb: packet to use
662  *
663  * returns 0 on success, <0 on failure.
664  *
665  */
666 static int gelic_descr_prepare_tx(struct gelic_card *card,
667                                   struct gelic_descr *descr,
668                                   struct sk_buff *skb)
669 {
670         dma_addr_t buf;
671
672         if (card->vlan_index != -1) {
673                 struct sk_buff *skb_tmp;
674                 skb_tmp = gelic_put_vlan_tag(skb,
675                                              card->vlan_id[card->vlan_index]);
676                 if (!skb_tmp)
677                         return -ENOMEM;
678                 skb = skb_tmp;
679         }
680
681         buf = dma_map_single(ctodev(card), skb->data, skb->len, DMA_TO_DEVICE);
682
683         if (!buf) {
684                 dev_err(ctodev(card),
685                         "dma map 2 failed (%p, %i). Dropping packet\n",
686                         skb->data, skb->len);
687                 return -ENOMEM;
688         }
689
690         descr->buf_addr = cpu_to_be32(buf);
691         descr->buf_size = cpu_to_be32(skb->len);
692         descr->skb = skb;
693         descr->data_status = 0;
694         descr->next_descr_addr = 0; /* terminate hw descr */
695         gelic_descr_set_tx_cmdstat(descr, skb);
696
697         /* bump free descriptor pointer */
698         card->tx_chain.head = descr->next;
699         return 0;
700 }
701
702 /**
703  * gelic_card_kick_txdma - enables TX DMA processing
704  * @card: card structure
705  * @descr: descriptor address to enable TX processing at
706  *
707  */
708 static int gelic_card_kick_txdma(struct gelic_card *card,
709                                  struct gelic_descr *descr)
710 {
711         int status = 0;
712
713         if (card->tx_dma_progress)
714                 return 0;
715
716         if (gelic_descr_get_status(descr) == GELIC_DESCR_DMA_CARDOWNED) {
717                 card->tx_dma_progress = 1;
718                 status = lv1_net_start_tx_dma(bus_id(card), dev_id(card),
719                                               descr->bus_addr, 0);
720                 if (status)
721                         dev_info(ctodev(card), "lv1_net_start_txdma failed," \
722                                  "status=%d\n", status);
723         }
724         return status;
725 }
726
727 /**
728  * gelic_net_xmit - transmits a frame over the device
729  * @skb: packet to send out
730  * @netdev: interface device structure
731  *
732  * returns 0 on success, <0 on failure
733  */
734 static int gelic_net_xmit(struct sk_buff *skb, struct net_device *netdev)
735 {
736         struct gelic_card *card = netdev_priv(netdev);
737         struct gelic_descr *descr;
738         int result;
739         unsigned long flags;
740
741         spin_lock_irqsave(&card->tx_dma_lock, flags);
742
743         gelic_card_release_tx_chain(card, 0);
744
745         descr = gelic_card_get_next_tx_descr(card);
746         if (!descr) {
747                 /*
748                  * no more descriptors free
749                  */
750                 netif_stop_queue(netdev);
751                 spin_unlock_irqrestore(&card->tx_dma_lock, flags);
752                 return NETDEV_TX_BUSY;
753         }
754
755         result = gelic_descr_prepare_tx(card, descr, skb);
756         if (result) {
757                 /*
758                  * DMA map failed.  As chanses are that failure
759                  * would continue, just release skb and return
760                  */
761                 card->netdev->stats.tx_dropped++;
762                 dev_kfree_skb_any(skb);
763                 spin_unlock_irqrestore(&card->tx_dma_lock, flags);
764                 return NETDEV_TX_OK;
765         }
766         /*
767          * link this prepared descriptor to previous one
768          * to achieve high performance
769          */
770         descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
771         /*
772          * as hardware descriptor is modified in the above lines,
773          * ensure that the hardware sees it
774          */
775         wmb();
776         if (gelic_card_kick_txdma(card, descr)) {
777                 /*
778                  * kick failed.
779                  * release descriptors which were just prepared
780                  */
781                 card->netdev->stats.tx_dropped++;
782                 gelic_descr_release_tx(card, descr);
783                 gelic_descr_release_tx(card, descr->next);
784                 card->tx_chain.tail = descr->next->next;
785                 dev_info(ctodev(card), "%s: kick failure\n", __func__);
786         } else {
787                 /* OK, DMA started/reserved */
788                 netdev->trans_start = jiffies;
789         }
790
791         spin_unlock_irqrestore(&card->tx_dma_lock, flags);
792         return NETDEV_TX_OK;
793 }
794
795 /**
796  * gelic_net_pass_skb_up - takes an skb from a descriptor and passes it on
797  * @descr: descriptor to process
798  * @card: card structure
799  *
800  * iommu-unmaps the skb, fills out skb structure and passes the data to the
801  * stack. The descriptor state is not changed.
802  */
803 static void gelic_net_pass_skb_up(struct gelic_descr *descr,
804                                  struct gelic_card *card)
805 {
806         struct sk_buff *skb;
807         struct net_device *netdev;
808         u32 data_status, data_error;
809
810         data_status = be32_to_cpu(descr->data_status);
811         data_error = be32_to_cpu(descr->data_error);
812         netdev = card->netdev;
813         /* unmap skb buffer */
814         skb = descr->skb;
815         dma_unmap_single(ctodev(card),
816                          be32_to_cpu(descr->buf_addr), GELIC_NET_MAX_MTU,
817                          DMA_FROM_DEVICE);
818
819         skb_put(skb, descr->valid_size ?
820                 be32_to_cpu(descr->valid_size) :
821                 be32_to_cpu(descr->result_size));
822         if (!descr->valid_size)
823                 dev_info(ctodev(card), "buffer full %x %x %x\n",
824                          be32_to_cpu(descr->result_size),
825                          be32_to_cpu(descr->buf_size),
826                          be32_to_cpu(descr->dmac_cmd_status));
827
828         descr->skb = NULL;
829         /*
830          * the card put 2 bytes vlan tag in front
831          * of the ethernet frame
832          */
833         skb_pull(skb, 2);
834         skb->protocol = eth_type_trans(skb, netdev);
835
836         /* checksum offload */
837         if (card->rx_csum) {
838                 if ((data_status & GELIC_DESCR_DATA_STATUS_CHK_MASK) &&
839                     (!(data_error & GELIC_DESCR_DATA_ERROR_CHK_MASK)))
840                         skb->ip_summed = CHECKSUM_UNNECESSARY;
841                 else
842                         skb->ip_summed = CHECKSUM_NONE;
843         } else
844                 skb->ip_summed = CHECKSUM_NONE;
845
846         /* update netdevice statistics */
847         card->netdev->stats.rx_packets++;
848         card->netdev->stats.rx_bytes += skb->len;
849
850         /* pass skb up to stack */
851         netif_receive_skb(skb);
852 }
853
854 /**
855  * gelic_card_decode_one_descr - processes an rx descriptor
856  * @card: card structure
857  *
858  * returns 1 if a packet has been sent to the stack, otherwise 0
859  *
860  * processes an rx descriptor by iommu-unmapping the data buffer and passing
861  * the packet up to the stack
862  */
863 static int gelic_card_decode_one_descr(struct gelic_card *card)
864 {
865         enum gelic_descr_dma_status status;
866         struct gelic_descr_chain *chain = &card->rx_chain;
867         struct gelic_descr *descr = chain->tail;
868         int dmac_chain_ended;
869
870         status = gelic_descr_get_status(descr);
871         /* is this descriptor terminated with next_descr == NULL? */
872         dmac_chain_ended =
873                 be32_to_cpu(descr->dmac_cmd_status) &
874                 GELIC_DESCR_RX_DMA_CHAIN_END;
875
876         if (status == GELIC_DESCR_DMA_CARDOWNED)
877                 return 0;
878
879         if (status == GELIC_DESCR_DMA_NOT_IN_USE) {
880                 dev_dbg(ctodev(card), "dormant descr? %p\n", descr);
881                 return 0;
882         }
883
884         if ((status == GELIC_DESCR_DMA_RESPONSE_ERROR) ||
885             (status == GELIC_DESCR_DMA_PROTECTION_ERROR) ||
886             (status == GELIC_DESCR_DMA_FORCE_END)) {
887                 dev_info(ctodev(card), "dropping RX descriptor with state %x\n",
888                          status);
889                 card->netdev->stats.rx_dropped++;
890                 goto refill;
891         }
892
893         if (status == GELIC_DESCR_DMA_BUFFER_FULL) {
894                 /*
895                  * Buffer full would occur if and only if
896                  * the frame length was longer than the size of this
897                  * descriptor's buffer.  If the frame length was equal
898                  * to or shorter than buffer'size, FRAME_END condition
899                  * would occur.
900                  * Anyway this frame was longer than the MTU,
901                  * just drop it.
902                  */
903                 dev_info(ctodev(card), "overlength frame\n");
904                 goto refill;
905         }
906         /*
907          * descriptoers any other than FRAME_END here should
908          * be treated as error.
909          */
910         if (status != GELIC_DESCR_DMA_FRAME_END) {
911                 dev_dbg(ctodev(card), "RX descriptor with state %x\n",
912                         status);
913                 goto refill;
914         }
915
916         /* ok, we've got a packet in descr */
917         gelic_net_pass_skb_up(descr, card);
918 refill:
919         /*
920          * So that always DMAC can see the end
921          * of the descriptor chain to avoid
922          * from unwanted DMAC overrun.
923          */
924         descr->next_descr_addr = 0;
925
926         /* change the descriptor state: */
927         gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
928
929         /*
930          * this call can fail, but for now, just leave this
931          * decriptor without skb
932          */
933         gelic_descr_prepare_rx(card, descr);
934
935         chain->head = descr;
936         chain->tail = descr->next;
937
938         /*
939          * Set this descriptor the end of the chain.
940          */
941         descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
942
943         /*
944          * If dmac chain was met, DMAC stopped.
945          * thus re-enable it
946          */
947         if (dmac_chain_ended) {
948                 card->rx_dma_restart_required = 1;
949                 dev_dbg(ctodev(card), "reenable rx dma scheduled\n");
950         }
951
952         return 1;
953 }
954
955 /**
956  * gelic_net_poll - NAPI poll function called by the stack to return packets
957  * @netdev: interface device structure
958  * @budget: number of packets we can pass to the stack at most
959  *
960  * returns 0 if no more packets available to the driver/stack. Returns 1,
961  * if the quota is exceeded, but the driver has still packets.
962  *
963  */
964 static int gelic_net_poll(struct napi_struct *napi, int budget)
965 {
966         struct gelic_card *card = container_of(napi, struct gelic_card, napi);
967         struct net_device *netdev = card->netdev;
968         int packets_done = 0;
969
970         while (packets_done < budget) {
971                 if (!gelic_card_decode_one_descr(card))
972                         break;
973
974                 packets_done++;
975         }
976
977         if (packets_done < budget) {
978                 netif_rx_complete(netdev, napi);
979                 gelic_card_rx_irq_on(card);
980         }
981         return packets_done;
982 }
983 /**
984  * gelic_net_change_mtu - changes the MTU of an interface
985  * @netdev: interface device structure
986  * @new_mtu: new MTU value
987  *
988  * returns 0 on success, <0 on failure
989  */
990 static int gelic_net_change_mtu(struct net_device *netdev, int new_mtu)
991 {
992         /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
993          * and mtu is outbound only anyway */
994         if ((new_mtu < GELIC_NET_MIN_MTU) ||
995             (new_mtu > GELIC_NET_MAX_MTU)) {
996                 return -EINVAL;
997         }
998         netdev->mtu = new_mtu;
999         return 0;
1000 }
1001
1002 /**
1003  * gelic_card_interrupt - event handler for gelic_net
1004  */
1005 static irqreturn_t gelic_card_interrupt(int irq, void *ptr)
1006 {
1007         unsigned long flags;
1008         struct net_device *netdev = ptr;
1009         struct gelic_card *card = netdev_priv(netdev);
1010         u64 status;
1011
1012         status = card->irq_status;
1013
1014         if (!status)
1015                 return IRQ_NONE;
1016
1017         if (card->rx_dma_restart_required) {
1018                 card->rx_dma_restart_required = 0;
1019                 gelic_card_enable_rxdmac(card);
1020         }
1021
1022         if (status & GELIC_CARD_RXINT) {
1023                 gelic_card_rx_irq_off(card);
1024                 netif_rx_schedule(netdev, &card->napi);
1025         }
1026
1027         if (status & GELIC_CARD_TXINT) {
1028                 spin_lock_irqsave(&card->tx_dma_lock, flags);
1029                 card->tx_dma_progress = 0;
1030                 gelic_card_release_tx_chain(card, 0);
1031                 /* kick outstanding tx descriptor if any */
1032                 gelic_card_kick_txdma(card, card->tx_chain.tail);
1033                 spin_unlock_irqrestore(&card->tx_dma_lock, flags);
1034         }
1035         return IRQ_HANDLED;
1036 }
1037
1038 #ifdef CONFIG_NET_POLL_CONTROLLER
1039 /**
1040  * gelic_net_poll_controller - artificial interrupt for netconsole etc.
1041  * @netdev: interface device structure
1042  *
1043  * see Documentation/networking/netconsole.txt
1044  */
1045 static void gelic_net_poll_controller(struct net_device *netdev)
1046 {
1047         struct gelic_card *card = netdev_priv(netdev);
1048
1049         gelic_card_set_irq_mask(card, 0);
1050         gelic_card_interrupt(netdev->irq, netdev);
1051         gelic_card_set_irq_mask(card, card->ghiintmask);
1052 }
1053 #endif /* CONFIG_NET_POLL_CONTROLLER */
1054
1055 /**
1056  * gelic_card_open - open device and map dma region
1057  * @card: card structure
1058  */
1059 static int gelic_card_open(struct gelic_card *card)
1060 {
1061         int result;
1062
1063         result = ps3_sb_event_receive_port_setup(card->dev, PS3_BINDING_CPU_ANY,
1064                 &card->netdev->irq);
1065
1066         if (result) {
1067                 dev_info(ctodev(card),
1068                          "%s:%d: recieve_port_setup failed (%d)\n",
1069                          __func__, __LINE__, result);
1070                 result = -EPERM;
1071                 goto fail_alloc_irq;
1072         }
1073
1074         result = request_irq(card->netdev->irq, gelic_card_interrupt,
1075                              IRQF_DISABLED, card->netdev->name, card->netdev);
1076
1077         if (result) {
1078                 dev_info(ctodev(card), "%s:%d: request_irq failed (%d)\n",
1079                         __func__, __LINE__, result);
1080                 goto fail_request_irq;
1081         }
1082
1083         return 0;
1084
1085 fail_request_irq:
1086         ps3_sb_event_receive_port_destroy(card->dev, card->netdev->irq);
1087         card->netdev->irq = NO_IRQ;
1088 fail_alloc_irq:
1089         return result;
1090 }
1091
1092
1093 /**
1094  * gelic_net_open - called upon ifonfig up
1095  * @netdev: interface device structure
1096  *
1097  * returns 0 on success, <0 on failure
1098  *
1099  * gelic_net_open allocates all the descriptors and memory needed for
1100  * operation, sets up multicast list and enables interrupts
1101  */
1102 static int gelic_net_open(struct net_device *netdev)
1103 {
1104         struct gelic_card *card = netdev_priv(netdev);
1105
1106         dev_dbg(ctodev(card), " -> %s:%d\n", __func__, __LINE__);
1107
1108         gelic_card_open(card);
1109
1110         if (gelic_card_init_chain(card, &card->tx_chain,
1111                                   card->descr, GELIC_NET_TX_DESCRIPTORS))
1112                 goto alloc_tx_failed;
1113         if (gelic_card_init_chain(card, &card->rx_chain,
1114                                   card->descr + GELIC_NET_TX_DESCRIPTORS,
1115                                   GELIC_NET_RX_DESCRIPTORS))
1116                 goto alloc_rx_failed;
1117
1118         /* head of chain */
1119         card->tx_top = card->tx_chain.head;
1120         card->rx_top = card->rx_chain.head;
1121         dev_dbg(ctodev(card), "descr rx %p, tx %p, size %#lx, num %#x\n",
1122                 card->rx_top, card->tx_top, sizeof(struct gelic_descr),
1123                 GELIC_NET_RX_DESCRIPTORS);
1124         /* allocate rx skbs */
1125         if (gelic_card_alloc_rx_skbs(card))
1126                 goto alloc_skbs_failed;
1127
1128         napi_enable(&card->napi);
1129
1130         card->tx_dma_progress = 0;
1131         card->ghiintmask = GELIC_CARD_RXINT | GELIC_CARD_TXINT;
1132
1133         gelic_card_set_irq_mask(card, card->ghiintmask);
1134         gelic_card_enable_rxdmac(card);
1135
1136         netif_start_queue(netdev);
1137         netif_carrier_on(netdev);
1138
1139         return 0;
1140
1141 alloc_skbs_failed:
1142         gelic_card_free_chain(card, card->rx_top);
1143 alloc_rx_failed:
1144         gelic_card_free_chain(card, card->tx_top);
1145 alloc_tx_failed:
1146         return -ENOMEM;
1147 }
1148
1149 static void gelic_net_get_drvinfo(struct net_device *netdev,
1150                                   struct ethtool_drvinfo *info)
1151 {
1152         strncpy(info->driver, DRV_NAME, sizeof(info->driver) - 1);
1153         strncpy(info->version, DRV_VERSION, sizeof(info->version) - 1);
1154 }
1155
1156 static int gelic_ether_get_settings(struct net_device *netdev,
1157                                     struct ethtool_cmd *cmd)
1158 {
1159         struct gelic_card *card = netdev_priv(netdev);
1160         int status;
1161         u64 v1, v2;
1162         int speed, duplex;
1163
1164         speed = duplex = -1;
1165         status = lv1_net_control(bus_id(card), dev_id(card),
1166                                  GELIC_LV1_GET_ETH_PORT_STATUS,
1167                                  GELIC_LV1_VLAN_TX_ETHERNET, 0, 0,
1168                                  &v1, &v2);
1169         if (status) {
1170                 /* link down */
1171         } else {
1172                 if (v1 & GELIC_LV1_ETHER_FULL_DUPLEX) {
1173                         duplex = DUPLEX_FULL;
1174                 } else {
1175                         duplex = DUPLEX_HALF;
1176                 }
1177
1178                 if (v1 & GELIC_LV1_ETHER_SPEED_10) {
1179                         speed = SPEED_10;
1180                 } else if (v1 & GELIC_LV1_ETHER_SPEED_100) {
1181                         speed = SPEED_100;
1182                 } else if (v1 & GELIC_LV1_ETHER_SPEED_1000) {
1183                         speed = SPEED_1000;
1184                 }
1185         }
1186         cmd->supported = SUPPORTED_TP | SUPPORTED_Autoneg |
1187                         SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
1188                         SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
1189                         SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full;
1190         cmd->advertising = cmd->supported;
1191         cmd->speed = speed;
1192         cmd->duplex = duplex;
1193         cmd->autoneg = AUTONEG_ENABLE; /* always enabled */
1194         cmd->port = PORT_TP;
1195
1196         return 0;
1197 }
1198
1199 static int gelic_net_nway_reset(struct net_device *netdev)
1200 {
1201         if (netif_running(netdev)) {
1202                 gelic_net_stop(netdev);
1203                 gelic_net_open(netdev);
1204         }
1205         return 0;
1206 }
1207
1208 static u32 gelic_net_get_rx_csum(struct net_device *netdev)
1209 {
1210         struct gelic_card *card = netdev_priv(netdev);
1211
1212         return card->rx_csum;
1213 }
1214
1215 static int gelic_net_set_rx_csum(struct net_device *netdev, u32 data)
1216 {
1217         struct gelic_card *card = netdev_priv(netdev);
1218
1219         card->rx_csum = data;
1220         return 0;
1221 }
1222
1223 static struct ethtool_ops gelic_net_ethtool_ops = {
1224         .get_drvinfo    = gelic_net_get_drvinfo,
1225         .get_settings   = gelic_ether_get_settings,
1226         .get_link       = ethtool_op_get_link,
1227         .nway_reset     = gelic_net_nway_reset,
1228         .get_tx_csum    = ethtool_op_get_tx_csum,
1229         .set_tx_csum    = ethtool_op_set_tx_csum,
1230         .get_rx_csum    = gelic_net_get_rx_csum,
1231         .set_rx_csum    = gelic_net_set_rx_csum,
1232 };
1233
1234 /**
1235  * gelic_net_tx_timeout_task - task scheduled by the watchdog timeout
1236  * function (to be called not under interrupt status)
1237  * @work: work is context of tx timout task
1238  *
1239  * called as task when tx hangs, resets interface (if interface is up)
1240  */
1241 static void gelic_net_tx_timeout_task(struct work_struct *work)
1242 {
1243         struct gelic_card *card =
1244                 container_of(work, struct gelic_card, tx_timeout_task);
1245         struct net_device *netdev = card->netdev;
1246
1247         dev_info(ctodev(card), "%s:Timed out. Restarting... \n", __func__);
1248
1249         if (!(netdev->flags & IFF_UP))
1250                 goto out;
1251
1252         netif_device_detach(netdev);
1253         gelic_net_stop(netdev);
1254
1255         gelic_net_open(netdev);
1256         netif_device_attach(netdev);
1257
1258 out:
1259         atomic_dec(&card->tx_timeout_task_counter);
1260 }
1261
1262 /**
1263  * gelic_net_tx_timeout - called when the tx timeout watchdog kicks in.
1264  * @netdev: interface device structure
1265  *
1266  * called, if tx hangs. Schedules a task that resets the interface
1267  */
1268 static void gelic_net_tx_timeout(struct net_device *netdev)
1269 {
1270         struct gelic_card *card;
1271
1272         card = netdev_priv(netdev);
1273         atomic_inc(&card->tx_timeout_task_counter);
1274         if (netdev->flags & IFF_UP)
1275                 schedule_work(&card->tx_timeout_task);
1276         else
1277                 atomic_dec(&card->tx_timeout_task_counter);
1278 }
1279
1280 /**
1281  * gelic_ether_setup_netdev_ops - initialization of net_device operations
1282  * @netdev: net_device structure
1283  *
1284  * fills out function pointers in the net_device structure
1285  */
1286 static void gelic_ether_setup_netdev_ops(struct net_device *netdev)
1287 {
1288         netdev->open = &gelic_net_open;
1289         netdev->stop = &gelic_net_stop;
1290         netdev->hard_start_xmit = &gelic_net_xmit;
1291         netdev->set_multicast_list = &gelic_net_set_multi;
1292         netdev->change_mtu = &gelic_net_change_mtu;
1293         /* tx watchdog */
1294         netdev->tx_timeout = &gelic_net_tx_timeout;
1295         netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;
1296         netdev->ethtool_ops = &gelic_net_ethtool_ops;
1297 }
1298
1299 /**
1300  * gelic_net_setup_netdev - initialization of net_device
1301  * @card: card structure
1302  *
1303  * Returns 0 on success or <0 on failure
1304  *
1305  * gelic_net_setup_netdev initializes the net_device structure
1306  **/
1307 static int gelic_net_setup_netdev(struct gelic_card *card)
1308 {
1309         struct net_device *netdev = card->netdev;
1310         struct sockaddr addr;
1311         unsigned int i;
1312         int status;
1313         u64 v1, v2;
1314         DECLARE_MAC_BUF(mac);
1315
1316         SET_NETDEV_DEV(netdev, &card->dev->core);
1317         spin_lock_init(&card->tx_dma_lock);
1318
1319         card->rx_csum = GELIC_NET_RX_CSUM_DEFAULT;
1320
1321         gelic_ether_setup_netdev_ops(netdev);
1322
1323         netif_napi_add(netdev, &card->napi,
1324                        gelic_net_poll, GELIC_NET_NAPI_WEIGHT);
1325
1326         netdev->features = NETIF_F_IP_CSUM;
1327
1328         status = lv1_net_control(bus_id(card), dev_id(card),
1329                                  GELIC_LV1_GET_MAC_ADDRESS,
1330                                  0, 0, 0, &v1, &v2);
1331         if (status || !is_valid_ether_addr((u8 *)&v1)) {
1332                 dev_info(ctodev(card),
1333                          "%s:lv1_net_control GET_MAC_ADDR failed %d\n",
1334                          __func__, status);
1335                 return -EINVAL;
1336         }
1337         v1 <<= 16;
1338         memcpy(addr.sa_data, &v1, ETH_ALEN);
1339         memcpy(netdev->dev_addr, addr.sa_data, ETH_ALEN);
1340         dev_info(ctodev(card), "MAC addr %s\n",
1341                  print_mac(mac, netdev->dev_addr));
1342
1343         card->vlan_index = -1;  /* no vlan */
1344         for (i = 0; i < GELIC_NET_VLAN_MAX; i++) {
1345                 status = lv1_net_control(bus_id(card), dev_id(card),
1346                                         GELIC_LV1_GET_VLAN_ID,
1347                                         i + 1, /* index; one based */
1348                                         0, 0, &v1, &v2);
1349                 if (status == LV1_NO_ENTRY) {
1350                         dev_dbg(ctodev(card),
1351                                 "GELIC_VLAN_ID no entry:%d, VLAN disabled\n",
1352                                 status);
1353                         card->vlan_id[i] = 0;
1354                 } else if (status) {
1355                         dev_dbg(ctodev(card),
1356                                 "%s:get vlan id faild, status=%d\n",
1357                                 __func__, status);
1358                         card->vlan_id[i] = 0;
1359                 } else {
1360                         card->vlan_id[i] = (u32)v1;
1361                         dev_dbg(ctodev(card), "vlan_id:%d, %lx\n", i, v1);
1362                 }
1363         }
1364
1365         if (card->vlan_id[GELIC_LV1_VLAN_TX_ETHERNET - 1]) {
1366                 card->vlan_index = GELIC_LV1_VLAN_TX_ETHERNET - 1;
1367                 netdev->hard_header_len += VLAN_HLEN;
1368         }
1369
1370         status = register_netdev(netdev);
1371         if (status) {
1372                 dev_err(ctodev(card), "%s:Couldn't register net_device: %d\n",
1373                         __func__, status);
1374                 return status;
1375         }
1376
1377         return 0;
1378 }
1379
1380 /**
1381  * gelic_alloc_card_net - allocates net_device and card structure
1382  *
1383  * returns the card structure or NULL in case of errors
1384  *
1385  * the card and net_device structures are linked to each other
1386  */
1387 static struct gelic_card *gelic_alloc_card_net(void)
1388 {
1389         struct net_device *netdev;
1390         struct gelic_card *card;
1391         size_t alloc_size;
1392
1393         alloc_size = sizeof(*card) +
1394                 sizeof(struct gelic_descr) * GELIC_NET_RX_DESCRIPTORS +
1395                 sizeof(struct gelic_descr) * GELIC_NET_TX_DESCRIPTORS;
1396         /*
1397          * we assume private data is allocated 32 bytes (or more) aligned
1398          * so that gelic_descr should be 32 bytes aligned.
1399          * Current alloc_etherdev() does do it because NETDEV_ALIGN
1400          * is 32.
1401          * check this assumption here.
1402          */
1403         BUILD_BUG_ON(NETDEV_ALIGN < 32);
1404         BUILD_BUG_ON(offsetof(struct gelic_card, irq_status) % 8);
1405         BUILD_BUG_ON(offsetof(struct gelic_card, descr) % 32);
1406
1407         netdev = alloc_etherdev(alloc_size);
1408         if (!netdev)
1409                 return NULL;
1410
1411         card = netdev_priv(netdev);
1412         card->netdev = netdev;
1413         INIT_WORK(&card->tx_timeout_task, gelic_net_tx_timeout_task);
1414         init_waitqueue_head(&card->waitq);
1415         atomic_set(&card->tx_timeout_task_counter, 0);
1416
1417         return card;
1418 }
1419
1420 /**
1421  * ps3_gelic_driver_probe - add a device to the control of this driver
1422  */
1423 static int ps3_gelic_driver_probe(struct ps3_system_bus_device *dev)
1424 {
1425         struct gelic_card *card = gelic_alloc_card_net();
1426         int result;
1427
1428         if (!card) {
1429                 dev_info(&dev->core, "gelic_net_alloc_card failed\n");
1430                 result = -ENOMEM;
1431                 goto fail_alloc_card;
1432         }
1433
1434         ps3_system_bus_set_driver_data(dev, card);
1435         card->dev = dev;
1436
1437         result = ps3_open_hv_device(dev);
1438
1439         if (result) {
1440                 dev_dbg(&dev->core, "ps3_open_hv_device failed\n");
1441                 goto fail_open;
1442         }
1443
1444         result = ps3_dma_region_create(dev->d_region);
1445
1446         if (result) {
1447                 dev_dbg(&dev->core, "ps3_dma_region_create failed(%d)\n",
1448                         result);
1449                 BUG_ON("check region type");
1450                 goto fail_dma_region;
1451         }
1452
1453         result = lv1_net_set_interrupt_status_indicator(bus_id(card),
1454                                                         dev_id(card),
1455                 ps3_mm_phys_to_lpar(__pa(&card->irq_status)),
1456                 0);
1457
1458         if (result) {
1459                 dev_dbg(&dev->core,
1460                         "lv1_net_set_interrupt_status_indicator failed: %s\n",
1461                         ps3_result(result));
1462                 result = -EIO;
1463                 goto fail_status_indicator;
1464         }
1465
1466         result = gelic_net_setup_netdev(card);
1467
1468         if (result) {
1469                 dev_dbg(&dev->core, "%s:%d: ps3_dma_region_create failed: "
1470                         "(%d)\n", __func__, __LINE__, result);
1471                 goto fail_setup_netdev;
1472         }
1473
1474         return 0;
1475
1476 fail_setup_netdev:
1477         lv1_net_set_interrupt_status_indicator(bus_id(card),
1478                                                dev_id(card),
1479                                                0 , 0);
1480 fail_status_indicator:
1481         ps3_dma_region_free(dev->d_region);
1482 fail_dma_region:
1483         ps3_close_hv_device(dev);
1484 fail_open:
1485         ps3_system_bus_set_driver_data(dev, NULL);
1486         free_netdev(card->netdev);
1487 fail_alloc_card:
1488         return result;
1489 }
1490
1491 /**
1492  * ps3_gelic_driver_remove - remove a device from the control of this driver
1493  */
1494
1495 static int ps3_gelic_driver_remove(struct ps3_system_bus_device *dev)
1496 {
1497         struct gelic_card *card = ps3_system_bus_get_driver_data(dev);
1498
1499         wait_event(card->waitq,
1500                    atomic_read(&card->tx_timeout_task_counter) == 0);
1501
1502         lv1_net_set_interrupt_status_indicator(bus_id(card), dev_id(card),
1503                                                0 , 0);
1504
1505         unregister_netdev(card->netdev);
1506         free_netdev(card->netdev);
1507
1508         ps3_system_bus_set_driver_data(dev, NULL);
1509
1510         ps3_dma_region_free(dev->d_region);
1511
1512         ps3_close_hv_device(dev);
1513
1514         return 0;
1515 }
1516
1517 static struct ps3_system_bus_driver ps3_gelic_driver = {
1518         .match_id = PS3_MATCH_ID_GELIC,
1519         .probe = ps3_gelic_driver_probe,
1520         .remove = ps3_gelic_driver_remove,
1521         .shutdown = ps3_gelic_driver_remove,
1522         .core.name = "ps3_gelic_driver",
1523         .core.owner = THIS_MODULE,
1524 };
1525
1526 static int __init ps3_gelic_driver_init (void)
1527 {
1528         return firmware_has_feature(FW_FEATURE_PS3_LV1)
1529                 ? ps3_system_bus_driver_register(&ps3_gelic_driver)
1530                 : -ENODEV;
1531 }
1532
1533 static void __exit ps3_gelic_driver_exit (void)
1534 {
1535         ps3_system_bus_driver_unregister(&ps3_gelic_driver);
1536 }
1537
1538 module_init(ps3_gelic_driver_init);
1539 module_exit(ps3_gelic_driver_exit);
1540
1541 MODULE_ALIAS(PS3_MODULE_ALIAS_GELIC);
1542