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