b43legacy: Declare all possible ucodeX.fw files
[pandora-kernel.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 #include "ps3_gelic_wireless.h"
50
51 #define DRV_NAME "Gelic Network Driver"
52 #define DRV_VERSION "2.0"
53
54 MODULE_AUTHOR("SCE Inc.");
55 MODULE_DESCRIPTION("Gelic Network driver");
56 MODULE_LICENSE("GPL");
57
58
59 static inline void gelic_card_enable_rxdmac(struct gelic_card *card);
60 static inline void gelic_card_disable_rxdmac(struct gelic_card *card);
61 static inline void gelic_card_disable_txdmac(struct gelic_card *card);
62 static inline void gelic_card_reset_chain(struct gelic_card *card,
63                                           struct gelic_descr_chain *chain,
64                                           struct gelic_descr *start_descr);
65
66 /* set irq_mask */
67 int gelic_card_set_irq_mask(struct gelic_card *card, u64 mask)
68 {
69         int status;
70
71         status = lv1_net_set_interrupt_mask(bus_id(card), dev_id(card),
72                                             mask, 0);
73         if (status)
74                 dev_info(ctodev(card),
75                          "%s failed %d\n", __func__, status);
76         return status;
77 }
78
79 static inline void gelic_card_rx_irq_on(struct gelic_card *card)
80 {
81         card->irq_mask |= GELIC_CARD_RXINT;
82         gelic_card_set_irq_mask(card, card->irq_mask);
83 }
84 static inline void gelic_card_rx_irq_off(struct gelic_card *card)
85 {
86         card->irq_mask &= ~GELIC_CARD_RXINT;
87         gelic_card_set_irq_mask(card, card->irq_mask);
88 }
89
90 static void gelic_card_get_ether_port_status(struct gelic_card *card,
91                                              int inform)
92 {
93         u64 v2;
94         struct net_device *ether_netdev;
95
96         lv1_net_control(bus_id(card), dev_id(card),
97                         GELIC_LV1_GET_ETH_PORT_STATUS,
98                         GELIC_LV1_VLAN_TX_ETHERNET_0, 0, 0,
99                         &card->ether_port_status, &v2);
100
101         if (inform) {
102                 ether_netdev = card->netdev[GELIC_PORT_ETHERNET_0];
103                 if (card->ether_port_status & GELIC_LV1_ETHER_LINK_UP)
104                         netif_carrier_on(ether_netdev);
105                 else
106                         netif_carrier_off(ether_netdev);
107         }
108 }
109
110 static int gelic_card_set_link_mode(struct gelic_card *card, int mode)
111 {
112         int status;
113         u64 v1, v2;
114
115         status = lv1_net_control(bus_id(card), dev_id(card),
116                                  GELIC_LV1_SET_NEGOTIATION_MODE,
117                                  GELIC_LV1_PHY_ETHERNET_0, mode, 0, &v1, &v2);
118         if (status) {
119                 pr_info("%s: failed setting negotiation mode %d\n", __func__,
120                         status);
121                 return -EBUSY;
122         }
123
124         card->link_mode = mode;
125         return 0;
126 }
127
128 void gelic_card_up(struct gelic_card *card)
129 {
130         pr_debug("%s: called\n", __func__);
131         mutex_lock(&card->updown_lock);
132         if (atomic_inc_return(&card->users) == 1) {
133                 pr_debug("%s: real do\n", __func__);
134                 /* enable irq */
135                 gelic_card_set_irq_mask(card, card->irq_mask);
136                 /* start rx */
137                 gelic_card_enable_rxdmac(card);
138
139                 napi_enable(&card->napi);
140         }
141         mutex_unlock(&card->updown_lock);
142         pr_debug("%s: done\n", __func__);
143 }
144
145 void gelic_card_down(struct gelic_card *card)
146 {
147         u64 mask;
148         pr_debug("%s: called\n", __func__);
149         mutex_lock(&card->updown_lock);
150         if (atomic_dec_if_positive(&card->users) == 0) {
151                 pr_debug("%s: real do\n", __func__);
152                 napi_disable(&card->napi);
153                 /*
154                  * Disable irq. Wireless interrupts will
155                  * be disabled later if any
156                  */
157                 mask = card->irq_mask & (GELIC_CARD_WLAN_EVENT_RECEIVED |
158                                          GELIC_CARD_WLAN_COMMAND_COMPLETED);
159                 gelic_card_set_irq_mask(card, mask);
160                 /* stop rx */
161                 gelic_card_disable_rxdmac(card);
162                 gelic_card_reset_chain(card, &card->rx_chain,
163                                        card->descr + GELIC_NET_TX_DESCRIPTORS);
164                 /* stop tx */
165                 gelic_card_disable_txdmac(card);
166         }
167         mutex_unlock(&card->updown_lock);
168         pr_debug("%s: done\n", __func__);
169 }
170
171 /**
172  * gelic_descr_get_status -- returns the status of a descriptor
173  * @descr: descriptor to look at
174  *
175  * returns the status as in the dmac_cmd_status field of the descriptor
176  */
177 static enum gelic_descr_dma_status
178 gelic_descr_get_status(struct gelic_descr *descr)
179 {
180         return be32_to_cpu(descr->dmac_cmd_status) & GELIC_DESCR_DMA_STAT_MASK;
181 }
182
183 /**
184  * gelic_descr_set_status -- sets the status of a descriptor
185  * @descr: descriptor to change
186  * @status: status to set in the descriptor
187  *
188  * changes the status to the specified value. Doesn't change other bits
189  * in the status
190  */
191 static void gelic_descr_set_status(struct gelic_descr *descr,
192                                    enum gelic_descr_dma_status status)
193 {
194         descr->dmac_cmd_status = cpu_to_be32(status |
195                         (be32_to_cpu(descr->dmac_cmd_status) &
196                          ~GELIC_DESCR_DMA_STAT_MASK));
197         /*
198          * dma_cmd_status field is used to indicate whether the descriptor
199          * is valid or not.
200          * Usually caller of this function wants to inform that to the
201          * hardware, so we assure here the hardware sees the change.
202          */
203         wmb();
204 }
205
206 /**
207  * gelic_card_free_chain - free descriptor chain
208  * @card: card structure
209  * @descr_in: address of desc
210  */
211 static void gelic_card_free_chain(struct gelic_card *card,
212                                   struct gelic_descr *descr_in)
213 {
214         struct gelic_descr *descr;
215
216         for (descr = descr_in; descr && descr->bus_addr; descr = descr->next) {
217                 dma_unmap_single(ctodev(card), descr->bus_addr,
218                                  GELIC_DESCR_SIZE, DMA_BIDIRECTIONAL);
219                 descr->bus_addr = 0;
220         }
221 }
222
223 /**
224  * gelic_card_init_chain - links descriptor chain
225  * @card: card structure
226  * @chain: address of chain
227  * @start_descr: address of descriptor array
228  * @no: number of descriptors
229  *
230  * we manage a circular list that mirrors the hardware structure,
231  * except that the hardware uses bus addresses.
232  *
233  * returns 0 on success, <0 on failure
234  */
235 static int __devinit gelic_card_init_chain(struct gelic_card *card,
236                                            struct gelic_descr_chain *chain,
237                                            struct gelic_descr *start_descr,
238                                            int no)
239 {
240         int i;
241         struct gelic_descr *descr;
242
243         descr = start_descr;
244         memset(descr, 0, sizeof(*descr) * no);
245
246         /* set up the hardware pointers in each descriptor */
247         for (i = 0; i < no; i++, descr++) {
248                 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
249                 descr->bus_addr =
250                         dma_map_single(ctodev(card), descr,
251                                        GELIC_DESCR_SIZE,
252                                        DMA_BIDIRECTIONAL);
253
254                 if (!descr->bus_addr)
255                         goto iommu_error;
256
257                 descr->next = descr + 1;
258                 descr->prev = descr - 1;
259         }
260         /* make them as ring */
261         (descr - 1)->next = start_descr;
262         start_descr->prev = (descr - 1);
263
264         /* chain bus addr of hw descriptor */
265         descr = start_descr;
266         for (i = 0; i < no; i++, descr++) {
267                 descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
268         }
269
270         chain->head = start_descr;
271         chain->tail = start_descr;
272
273         /* do not chain last hw descriptor */
274         (descr - 1)->next_descr_addr = 0;
275
276         return 0;
277
278 iommu_error:
279         for (i--, descr--; 0 <= i; i--, descr--)
280                 if (descr->bus_addr)
281                         dma_unmap_single(ctodev(card), descr->bus_addr,
282                                          GELIC_DESCR_SIZE,
283                                          DMA_BIDIRECTIONAL);
284         return -ENOMEM;
285 }
286
287 /**
288  * gelic_card_reset_chain - reset status of a descriptor chain
289  * @card: card structure
290  * @chain: address of chain
291  * @start_descr: address of descriptor array
292  *
293  * Reset the status of dma descriptors to ready state
294  * and re-initialize the hardware chain for later use
295  */
296 static void gelic_card_reset_chain(struct gelic_card *card,
297                                    struct gelic_descr_chain *chain,
298                                    struct gelic_descr *start_descr)
299 {
300         struct gelic_descr *descr;
301
302         for (descr = start_descr; start_descr != descr->next; descr++) {
303                 gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
304                 descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
305         }
306
307         chain->head = start_descr;
308         chain->tail = (descr - 1);
309
310         (descr - 1)->next_descr_addr = 0;
311 }
312 /**
313  * gelic_descr_prepare_rx - reinitializes a rx descriptor
314  * @card: card structure
315  * @descr: descriptor to re-init
316  *
317  * return 0 on succes, <0 on failure
318  *
319  * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
320  * Activate the descriptor state-wise
321  */
322 static int gelic_descr_prepare_rx(struct gelic_card *card,
323                                   struct gelic_descr *descr)
324 {
325         int offset;
326         unsigned int bufsize;
327
328         if (gelic_descr_get_status(descr) !=  GELIC_DESCR_DMA_NOT_IN_USE)
329                 dev_info(ctodev(card), "%s: ERROR status \n", __func__);
330         /* we need to round up the buffer size to a multiple of 128 */
331         bufsize = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN);
332
333         /* and we need to have it 128 byte aligned, therefore we allocate a
334          * bit more */
335         descr->skb = dev_alloc_skb(bufsize + GELIC_NET_RXBUF_ALIGN - 1);
336         if (!descr->skb) {
337                 descr->buf_addr = 0; /* tell DMAC don't touch memory */
338                 dev_info(ctodev(card),
339                          "%s:allocate skb failed !!\n", __func__);
340                 return -ENOMEM;
341         }
342         descr->buf_size = cpu_to_be32(bufsize);
343         descr->dmac_cmd_status = 0;
344         descr->result_size = 0;
345         descr->valid_size = 0;
346         descr->data_error = 0;
347
348         offset = ((unsigned long)descr->skb->data) &
349                 (GELIC_NET_RXBUF_ALIGN - 1);
350         if (offset)
351                 skb_reserve(descr->skb, GELIC_NET_RXBUF_ALIGN - offset);
352         /* io-mmu-map the skb */
353         descr->buf_addr = cpu_to_be32(dma_map_single(ctodev(card),
354                                                      descr->skb->data,
355                                                      GELIC_NET_MAX_MTU,
356                                                      DMA_FROM_DEVICE));
357         if (!descr->buf_addr) {
358                 dev_kfree_skb_any(descr->skb);
359                 descr->skb = NULL;
360                 dev_info(ctodev(card),
361                          "%s:Could not iommu-map rx buffer\n", __func__);
362                 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
363                 return -ENOMEM;
364         } else {
365                 gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
366                 return 0;
367         }
368 }
369
370 /**
371  * gelic_card_release_rx_chain - free all skb of rx descr
372  * @card: card structure
373  *
374  */
375 static void gelic_card_release_rx_chain(struct gelic_card *card)
376 {
377         struct gelic_descr *descr = card->rx_chain.head;
378
379         do {
380                 if (descr->skb) {
381                         dma_unmap_single(ctodev(card),
382                                          be32_to_cpu(descr->buf_addr),
383                                          descr->skb->len,
384                                          DMA_FROM_DEVICE);
385                         descr->buf_addr = 0;
386                         dev_kfree_skb_any(descr->skb);
387                         descr->skb = NULL;
388                         gelic_descr_set_status(descr,
389                                                GELIC_DESCR_DMA_NOT_IN_USE);
390                 }
391                 descr = descr->next;
392         } while (descr != card->rx_chain.head);
393 }
394
395 /**
396  * gelic_card_fill_rx_chain - fills descriptors/skbs in the rx chains
397  * @card: card structure
398  *
399  * fills all descriptors in the rx chain: allocates skbs
400  * and iommu-maps them.
401  * returns 0 on success, < 0 on failure
402  */
403 static int gelic_card_fill_rx_chain(struct gelic_card *card)
404 {
405         struct gelic_descr *descr = card->rx_chain.head;
406         int ret;
407
408         do {
409                 if (!descr->skb) {
410                         ret = gelic_descr_prepare_rx(card, descr);
411                         if (ret)
412                                 goto rewind;
413                 }
414                 descr = descr->next;
415         } while (descr != card->rx_chain.head);
416
417         return 0;
418 rewind:
419         gelic_card_release_rx_chain(card);
420         return ret;
421 }
422
423 /**
424  * gelic_card_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
425  * @card: card structure
426  *
427  * returns 0 on success, < 0 on failure
428  */
429 static int __devinit gelic_card_alloc_rx_skbs(struct gelic_card *card)
430 {
431         struct gelic_descr_chain *chain;
432         int ret;
433         chain = &card->rx_chain;
434         ret = gelic_card_fill_rx_chain(card);
435         chain->tail = card->rx_top->prev; /* point to the last */
436         return ret;
437 }
438
439 /**
440  * gelic_descr_release_tx - processes a used tx descriptor
441  * @card: card structure
442  * @descr: descriptor to release
443  *
444  * releases a used tx descriptor (unmapping, freeing of skb)
445  */
446 static void gelic_descr_release_tx(struct gelic_card *card,
447                                        struct gelic_descr *descr)
448 {
449         struct sk_buff *skb = descr->skb;
450
451         BUG_ON(!(be32_to_cpu(descr->data_status) & GELIC_DESCR_TX_TAIL));
452
453         dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr), skb->len,
454                          DMA_TO_DEVICE);
455         dev_kfree_skb_any(skb);
456
457         descr->buf_addr = 0;
458         descr->buf_size = 0;
459         descr->next_descr_addr = 0;
460         descr->result_size = 0;
461         descr->valid_size = 0;
462         descr->data_status = 0;
463         descr->data_error = 0;
464         descr->skb = NULL;
465
466         /* set descr status */
467         gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
468 }
469
470 static void gelic_card_stop_queues(struct gelic_card *card)
471 {
472         netif_stop_queue(card->netdev[GELIC_PORT_ETHERNET_0]);
473
474         if (card->netdev[GELIC_PORT_WIRELESS])
475                 netif_stop_queue(card->netdev[GELIC_PORT_WIRELESS]);
476 }
477 static void gelic_card_wake_queues(struct gelic_card *card)
478 {
479         netif_wake_queue(card->netdev[GELIC_PORT_ETHERNET_0]);
480
481         if (card->netdev[GELIC_PORT_WIRELESS])
482                 netif_wake_queue(card->netdev[GELIC_PORT_WIRELESS]);
483 }
484 /**
485  * gelic_card_release_tx_chain - processes sent tx descriptors
486  * @card: adapter structure
487  * @stop: net_stop sequence
488  *
489  * releases the tx descriptors that gelic has finished with
490  */
491 static void gelic_card_release_tx_chain(struct gelic_card *card, int stop)
492 {
493         struct gelic_descr_chain *tx_chain;
494         enum gelic_descr_dma_status status;
495         struct net_device *netdev;
496         int release = 0;
497
498         for (tx_chain = &card->tx_chain;
499              tx_chain->head != tx_chain->tail && tx_chain->tail;
500              tx_chain->tail = tx_chain->tail->next) {
501                 status = gelic_descr_get_status(tx_chain->tail);
502                 netdev = tx_chain->tail->skb->dev;
503                 switch (status) {
504                 case GELIC_DESCR_DMA_RESPONSE_ERROR:
505                 case GELIC_DESCR_DMA_PROTECTION_ERROR:
506                 case GELIC_DESCR_DMA_FORCE_END:
507                         if (printk_ratelimit())
508                                 dev_info(ctodev(card),
509                                          "%s: forcing end of tx descriptor " \
510                                          "with status %x\n",
511                                          __func__, status);
512                         netdev->stats.tx_dropped++;
513                         break;
514
515                 case GELIC_DESCR_DMA_COMPLETE:
516                         if (tx_chain->tail->skb) {
517                                 netdev->stats.tx_packets++;
518                                 netdev->stats.tx_bytes +=
519                                         tx_chain->tail->skb->len;
520                         }
521                         break;
522
523                 case GELIC_DESCR_DMA_CARDOWNED:
524                         /* pending tx request */
525                 default:
526                         /* any other value (== GELIC_DESCR_DMA_NOT_IN_USE) */
527                         if (!stop)
528                                 goto out;
529                 }
530                 gelic_descr_release_tx(card, tx_chain->tail);
531                 release ++;
532         }
533 out:
534         if (!stop && release)
535                 gelic_card_wake_queues(card);
536 }
537
538 /**
539  * gelic_net_set_multi - sets multicast addresses and promisc flags
540  * @netdev: interface device structure
541  *
542  * gelic_net_set_multi configures multicast addresses as needed for the
543  * netdev interface. It also sets up multicast, allmulti and promisc
544  * flags appropriately
545  */
546 void gelic_net_set_multi(struct net_device *netdev)
547 {
548         struct gelic_card *card = netdev_card(netdev);
549         struct dev_mc_list *mc;
550         unsigned int i;
551         uint8_t *p;
552         u64 addr;
553         int status;
554
555         /* clear all multicast address */
556         status = lv1_net_remove_multicast_address(bus_id(card), dev_id(card),
557                                                   0, 1);
558         if (status)
559                 dev_err(ctodev(card),
560                         "lv1_net_remove_multicast_address failed %d\n",
561                         status);
562         /* set broadcast address */
563         status = lv1_net_add_multicast_address(bus_id(card), dev_id(card),
564                                                GELIC_NET_BROADCAST_ADDR, 0);
565         if (status)
566                 dev_err(ctodev(card),
567                         "lv1_net_add_multicast_address failed, %d\n",
568                         status);
569
570         if ((netdev->flags & IFF_ALLMULTI) ||
571             (netdev->mc_count > GELIC_NET_MC_COUNT_MAX)) {
572                 status = lv1_net_add_multicast_address(bus_id(card),
573                                                        dev_id(card),
574                                                        0, 1);
575                 if (status)
576                         dev_err(ctodev(card),
577                                 "lv1_net_add_multicast_address failed, %d\n",
578                                 status);
579                 return;
580         }
581
582         /* set multicast addresses */
583         for (mc = netdev->mc_list; mc; mc = mc->next) {
584                 addr = 0;
585                 p = mc->dmi_addr;
586                 for (i = 0; i < ETH_ALEN; i++) {
587                         addr <<= 8;
588                         addr |= *p++;
589                 }
590                 status = lv1_net_add_multicast_address(bus_id(card),
591                                                        dev_id(card),
592                                                        addr, 0);
593                 if (status)
594                         dev_err(ctodev(card),
595                                 "lv1_net_add_multicast_address failed, %d\n",
596                                 status);
597         }
598 }
599
600 /**
601  * gelic_card_enable_rxdmac - enables the receive DMA controller
602  * @card: card structure
603  *
604  * gelic_card_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
605  * in the GDADMACCNTR register
606  */
607 static inline void gelic_card_enable_rxdmac(struct gelic_card *card)
608 {
609         int status;
610
611 #ifdef DEBUG
612         if (gelic_descr_get_status(card->rx_chain.head) !=
613             GELIC_DESCR_DMA_CARDOWNED) {
614                 printk(KERN_ERR "%s: status=%x\n", __func__,
615                        be32_to_cpu(card->rx_chain.head->dmac_cmd_status));
616                 printk(KERN_ERR "%s: nextphy=%x\n", __func__,
617                        be32_to_cpu(card->rx_chain.head->next_descr_addr));
618                 printk(KERN_ERR "%s: head=%p\n", __func__,
619                        card->rx_chain.head);
620         }
621 #endif
622         status = lv1_net_start_rx_dma(bus_id(card), dev_id(card),
623                                 card->rx_chain.head->bus_addr, 0);
624         if (status)
625                 dev_info(ctodev(card),
626                          "lv1_net_start_rx_dma failed, status=%d\n", status);
627 }
628
629 /**
630  * gelic_card_disable_rxdmac - disables the receive DMA controller
631  * @card: card structure
632  *
633  * gelic_card_disable_rxdmac terminates processing on the DMA controller by
634  * turing off DMA and issueing a force end
635  */
636 static inline void gelic_card_disable_rxdmac(struct gelic_card *card)
637 {
638         int status;
639
640         /* this hvc blocks until the DMA in progress really stopped */
641         status = lv1_net_stop_rx_dma(bus_id(card), dev_id(card), 0);
642         if (status)
643                 dev_err(ctodev(card),
644                         "lv1_net_stop_rx_dma faild, %d\n", status);
645 }
646
647 /**
648  * gelic_card_disable_txdmac - disables the transmit DMA controller
649  * @card: card structure
650  *
651  * gelic_card_disable_txdmac terminates processing on the DMA controller by
652  * turing off DMA and issueing a force end
653  */
654 static inline void gelic_card_disable_txdmac(struct gelic_card *card)
655 {
656         int status;
657
658         /* this hvc blocks until the DMA in progress really stopped */
659         status = lv1_net_stop_tx_dma(bus_id(card), dev_id(card), 0);
660         if (status)
661                 dev_err(ctodev(card),
662                         "lv1_net_stop_tx_dma faild, status=%d\n", status);
663 }
664
665 /**
666  * gelic_net_stop - called upon ifconfig down
667  * @netdev: interface device structure
668  *
669  * always returns 0
670  */
671 int gelic_net_stop(struct net_device *netdev)
672 {
673         struct gelic_card *card;
674
675         pr_debug("%s: start\n", __func__);
676
677         netif_stop_queue(netdev);
678         netif_carrier_off(netdev);
679
680         card = netdev_card(netdev);
681         gelic_card_down(card);
682
683         pr_debug("%s: done\n", __func__);
684         return 0;
685 }
686
687 /**
688  * gelic_card_get_next_tx_descr - returns the next available tx descriptor
689  * @card: device structure to get descriptor from
690  *
691  * returns the address of the next descriptor, or NULL if not available.
692  */
693 static struct gelic_descr *
694 gelic_card_get_next_tx_descr(struct gelic_card *card)
695 {
696         if (!card->tx_chain.head)
697                 return NULL;
698         /*  see if the next descriptor is free */
699         if (card->tx_chain.tail != card->tx_chain.head->next &&
700             gelic_descr_get_status(card->tx_chain.head) ==
701             GELIC_DESCR_DMA_NOT_IN_USE)
702                 return card->tx_chain.head;
703         else
704                 return NULL;
705
706 }
707
708 /**
709  * gelic_net_set_txdescr_cmdstat - sets the tx descriptor command field
710  * @descr: descriptor structure to fill out
711  * @skb: packet to consider
712  *
713  * fills out the command and status field of the descriptor structure,
714  * depending on hardware checksum settings. This function assumes a wmb()
715  * has executed before.
716  */
717 static void gelic_descr_set_tx_cmdstat(struct gelic_descr *descr,
718                                        struct sk_buff *skb)
719 {
720         if (skb->ip_summed != CHECKSUM_PARTIAL)
721                 descr->dmac_cmd_status =
722                         cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
723                                     GELIC_DESCR_TX_DMA_FRAME_TAIL);
724         else {
725                 /* is packet ip?
726                  * if yes: tcp? udp? */
727                 if (skb->protocol == htons(ETH_P_IP)) {
728                         if (ip_hdr(skb)->protocol == IPPROTO_TCP)
729                                 descr->dmac_cmd_status =
730                                 cpu_to_be32(GELIC_DESCR_DMA_CMD_TCP_CHKSUM |
731                                             GELIC_DESCR_TX_DMA_FRAME_TAIL);
732
733                         else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
734                                 descr->dmac_cmd_status =
735                                 cpu_to_be32(GELIC_DESCR_DMA_CMD_UDP_CHKSUM |
736                                             GELIC_DESCR_TX_DMA_FRAME_TAIL);
737                         else    /*
738                                  * the stack should checksum non-tcp and non-udp
739                                  * packets on his own: NETIF_F_IP_CSUM
740                                  */
741                                 descr->dmac_cmd_status =
742                                 cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
743                                             GELIC_DESCR_TX_DMA_FRAME_TAIL);
744                 }
745         }
746 }
747
748 static inline struct sk_buff *gelic_put_vlan_tag(struct sk_buff *skb,
749                                                  unsigned short tag)
750 {
751         struct vlan_ethhdr *veth;
752         static unsigned int c;
753
754         if (skb_headroom(skb) < VLAN_HLEN) {
755                 struct sk_buff *sk_tmp = skb;
756                 pr_debug("%s: hd=%d c=%ud\n", __func__, skb_headroom(skb), c);
757                 skb = skb_realloc_headroom(sk_tmp, VLAN_HLEN);
758                 if (!skb)
759                         return NULL;
760                 dev_kfree_skb_any(sk_tmp);
761         }
762         veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN);
763
764         /* Move the mac addresses to the top of buffer */
765         memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN);
766
767         veth->h_vlan_proto = cpu_to_be16(ETH_P_8021Q);
768         veth->h_vlan_TCI = htons(tag);
769
770         return skb;
771 }
772
773 /**
774  * gelic_descr_prepare_tx - setup a descriptor for sending packets
775  * @card: card structure
776  * @descr: descriptor structure
777  * @skb: packet to use
778  *
779  * returns 0 on success, <0 on failure.
780  *
781  */
782 static int gelic_descr_prepare_tx(struct gelic_card *card,
783                                   struct gelic_descr *descr,
784                                   struct sk_buff *skb)
785 {
786         dma_addr_t buf;
787
788         if (card->vlan_required) {
789                 struct sk_buff *skb_tmp;
790                 enum gelic_port_type type;
791
792                 type = netdev_port(skb->dev)->type;
793                 skb_tmp = gelic_put_vlan_tag(skb,
794                                              card->vlan[type].tx);
795                 if (!skb_tmp)
796                         return -ENOMEM;
797                 skb = skb_tmp;
798         }
799
800         buf = dma_map_single(ctodev(card), skb->data, skb->len, DMA_TO_DEVICE);
801
802         if (!buf) {
803                 dev_err(ctodev(card),
804                         "dma map 2 failed (%p, %i). Dropping packet\n",
805                         skb->data, skb->len);
806                 return -ENOMEM;
807         }
808
809         descr->buf_addr = cpu_to_be32(buf);
810         descr->buf_size = cpu_to_be32(skb->len);
811         descr->skb = skb;
812         descr->data_status = 0;
813         descr->next_descr_addr = 0; /* terminate hw descr */
814         gelic_descr_set_tx_cmdstat(descr, skb);
815
816         /* bump free descriptor pointer */
817         card->tx_chain.head = descr->next;
818         return 0;
819 }
820
821 /**
822  * gelic_card_kick_txdma - enables TX DMA processing
823  * @card: card structure
824  * @descr: descriptor address to enable TX processing at
825  *
826  */
827 static int gelic_card_kick_txdma(struct gelic_card *card,
828                                  struct gelic_descr *descr)
829 {
830         int status = 0;
831
832         if (card->tx_dma_progress)
833                 return 0;
834
835         if (gelic_descr_get_status(descr) == GELIC_DESCR_DMA_CARDOWNED) {
836                 card->tx_dma_progress = 1;
837                 status = lv1_net_start_tx_dma(bus_id(card), dev_id(card),
838                                               descr->bus_addr, 0);
839                 if (status)
840                         dev_info(ctodev(card), "lv1_net_start_txdma failed," \
841                                  "status=%d\n", status);
842         }
843         return status;
844 }
845
846 /**
847  * gelic_net_xmit - transmits a frame over the device
848  * @skb: packet to send out
849  * @netdev: interface device structure
850  *
851  * returns 0 on success, <0 on failure
852  */
853 int gelic_net_xmit(struct sk_buff *skb, struct net_device *netdev)
854 {
855         struct gelic_card *card = netdev_card(netdev);
856         struct gelic_descr *descr;
857         int result;
858         unsigned long flags;
859
860         spin_lock_irqsave(&card->tx_lock, flags);
861
862         gelic_card_release_tx_chain(card, 0);
863
864         descr = gelic_card_get_next_tx_descr(card);
865         if (!descr) {
866                 /*
867                  * no more descriptors free
868                  */
869                 gelic_card_stop_queues(card);
870                 spin_unlock_irqrestore(&card->tx_lock, flags);
871                 return NETDEV_TX_BUSY;
872         }
873
874         result = gelic_descr_prepare_tx(card, descr, skb);
875         if (result) {
876                 /*
877                  * DMA map failed.  As chanses are that failure
878                  * would continue, just release skb and return
879                  */
880                 netdev->stats.tx_dropped++;
881                 dev_kfree_skb_any(skb);
882                 spin_unlock_irqrestore(&card->tx_lock, flags);
883                 return NETDEV_TX_OK;
884         }
885         /*
886          * link this prepared descriptor to previous one
887          * to achieve high performance
888          */
889         descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
890         /*
891          * as hardware descriptor is modified in the above lines,
892          * ensure that the hardware sees it
893          */
894         wmb();
895         if (gelic_card_kick_txdma(card, descr)) {
896                 /*
897                  * kick failed.
898                  * release descriptors which were just prepared
899                  */
900                 netdev->stats.tx_dropped++;
901                 gelic_descr_release_tx(card, descr);
902                 gelic_descr_release_tx(card, descr->next);
903                 card->tx_chain.tail = descr->next->next;
904                 dev_info(ctodev(card), "%s: kick failure\n", __func__);
905         } else {
906                 /* OK, DMA started/reserved */
907                 netdev->trans_start = jiffies;
908         }
909
910         spin_unlock_irqrestore(&card->tx_lock, flags);
911         return NETDEV_TX_OK;
912 }
913
914 /**
915  * gelic_net_pass_skb_up - takes an skb from a descriptor and passes it on
916  * @descr: descriptor to process
917  * @card: card structure
918  * @netdev: net_device structure to be passed packet
919  *
920  * iommu-unmaps the skb, fills out skb structure and passes the data to the
921  * stack. The descriptor state is not changed.
922  */
923 static void gelic_net_pass_skb_up(struct gelic_descr *descr,
924                                   struct gelic_card *card,
925                                   struct net_device *netdev)
926
927 {
928         struct sk_buff *skb = descr->skb;
929         u32 data_status, data_error;
930
931         data_status = be32_to_cpu(descr->data_status);
932         data_error = be32_to_cpu(descr->data_error);
933         /* unmap skb buffer */
934         dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr),
935                          GELIC_NET_MAX_MTU,
936                          DMA_FROM_DEVICE);
937
938         skb_put(skb, be32_to_cpu(descr->valid_size)?
939                 be32_to_cpu(descr->valid_size) :
940                 be32_to_cpu(descr->result_size));
941         if (!descr->valid_size)
942                 dev_info(ctodev(card), "buffer full %x %x %x\n",
943                          be32_to_cpu(descr->result_size),
944                          be32_to_cpu(descr->buf_size),
945                          be32_to_cpu(descr->dmac_cmd_status));
946
947         descr->skb = NULL;
948         /*
949          * the card put 2 bytes vlan tag in front
950          * of the ethernet frame
951          */
952         skb_pull(skb, 2);
953         skb->protocol = eth_type_trans(skb, netdev);
954
955         /* checksum offload */
956         if (card->rx_csum) {
957                 if ((data_status & GELIC_DESCR_DATA_STATUS_CHK_MASK) &&
958                     (!(data_error & GELIC_DESCR_DATA_ERROR_CHK_MASK)))
959                         skb->ip_summed = CHECKSUM_UNNECESSARY;
960                 else
961                         skb->ip_summed = CHECKSUM_NONE;
962         } else
963                 skb->ip_summed = CHECKSUM_NONE;
964
965         /* update netdevice statistics */
966         netdev->stats.rx_packets++;
967         netdev->stats.rx_bytes += skb->len;
968
969         /* pass skb up to stack */
970         netif_receive_skb(skb);
971 }
972
973 /**
974  * gelic_card_decode_one_descr - processes an rx descriptor
975  * @card: card structure
976  *
977  * returns 1 if a packet has been sent to the stack, otherwise 0
978  *
979  * processes an rx descriptor by iommu-unmapping the data buffer and passing
980  * the packet up to the stack
981  */
982 static int gelic_card_decode_one_descr(struct gelic_card *card)
983 {
984         enum gelic_descr_dma_status status;
985         struct gelic_descr_chain *chain = &card->rx_chain;
986         struct gelic_descr *descr = chain->head;
987         struct net_device *netdev = NULL;
988         int dmac_chain_ended;
989
990         status = gelic_descr_get_status(descr);
991         /* is this descriptor terminated with next_descr == NULL? */
992         dmac_chain_ended =
993                 be32_to_cpu(descr->dmac_cmd_status) &
994                 GELIC_DESCR_RX_DMA_CHAIN_END;
995
996         if (status == GELIC_DESCR_DMA_CARDOWNED)
997                 return 0;
998
999         if (status == GELIC_DESCR_DMA_NOT_IN_USE) {
1000                 dev_dbg(ctodev(card), "dormant descr? %p\n", descr);
1001                 return 0;
1002         }
1003
1004         /* netdevice select */
1005         if (card->vlan_required) {
1006                 unsigned int i;
1007                 u16 vid;
1008                 vid = *(u16 *)(descr->skb->data) & VLAN_VID_MASK;
1009                 for (i = 0; i < GELIC_PORT_MAX; i++) {
1010                         if (card->vlan[i].rx == vid) {
1011                                 netdev = card->netdev[i];
1012                                 break;
1013                         }
1014                 };
1015                 if (GELIC_PORT_MAX <= i) {
1016                         pr_info("%s: unknown packet vid=%x\n", __func__, vid);
1017                         goto refill;
1018                 }
1019         } else
1020                 netdev = card->netdev[GELIC_PORT_ETHERNET_0];
1021
1022         if ((status == GELIC_DESCR_DMA_RESPONSE_ERROR) ||
1023             (status == GELIC_DESCR_DMA_PROTECTION_ERROR) ||
1024             (status == GELIC_DESCR_DMA_FORCE_END)) {
1025                 dev_info(ctodev(card), "dropping RX descriptor with state %x\n",
1026                          status);
1027                 netdev->stats.rx_dropped++;
1028                 goto refill;
1029         }
1030
1031         if (status == GELIC_DESCR_DMA_BUFFER_FULL) {
1032                 /*
1033                  * Buffer full would occur if and only if
1034                  * the frame length was longer than the size of this
1035                  * descriptor's buffer.  If the frame length was equal
1036                  * to or shorter than buffer'size, FRAME_END condition
1037                  * would occur.
1038                  * Anyway this frame was longer than the MTU,
1039                  * just drop it.
1040                  */
1041                 dev_info(ctodev(card), "overlength frame\n");
1042                 goto refill;
1043         }
1044         /*
1045          * descriptoers any other than FRAME_END here should
1046          * be treated as error.
1047          */
1048         if (status != GELIC_DESCR_DMA_FRAME_END) {
1049                 dev_dbg(ctodev(card), "RX descriptor with state %x\n",
1050                         status);
1051                 goto refill;
1052         }
1053
1054         /* ok, we've got a packet in descr */
1055         gelic_net_pass_skb_up(descr, card, netdev);
1056 refill:
1057         /*
1058          * So that always DMAC can see the end
1059          * of the descriptor chain to avoid
1060          * from unwanted DMAC overrun.
1061          */
1062         descr->next_descr_addr = 0;
1063
1064         /* change the descriptor state: */
1065         gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
1066
1067         /*
1068          * this call can fail, but for now, just leave this
1069          * decriptor without skb
1070          */
1071         gelic_descr_prepare_rx(card, descr);
1072
1073         chain->tail = descr;
1074         chain->head = descr->next;
1075
1076         /*
1077          * Set this descriptor the end of the chain.
1078          */
1079         descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
1080
1081         /*
1082          * If dmac chain was met, DMAC stopped.
1083          * thus re-enable it
1084          */
1085         if (dmac_chain_ended) {
1086                 card->rx_dma_restart_required = 1;
1087                 dev_dbg(ctodev(card), "reenable rx dma scheduled\n");
1088         }
1089
1090         return 1;
1091 }
1092
1093 /**
1094  * gelic_net_poll - NAPI poll function called by the stack to return packets
1095  * @napi: napi structure
1096  * @budget: number of packets we can pass to the stack at most
1097  *
1098  * returns the number of the processed packets
1099  *
1100  */
1101 static int gelic_net_poll(struct napi_struct *napi, int budget)
1102 {
1103         struct gelic_card *card = container_of(napi, struct gelic_card, napi);
1104         int packets_done = 0;
1105
1106         while (packets_done < budget) {
1107                 if (!gelic_card_decode_one_descr(card))
1108                         break;
1109
1110                 packets_done++;
1111         }
1112
1113         if (packets_done < budget) {
1114                 napi_complete(napi);
1115                 gelic_card_rx_irq_on(card);
1116         }
1117         return packets_done;
1118 }
1119 /**
1120  * gelic_net_change_mtu - changes the MTU of an interface
1121  * @netdev: interface device structure
1122  * @new_mtu: new MTU value
1123  *
1124  * returns 0 on success, <0 on failure
1125  */
1126 int gelic_net_change_mtu(struct net_device *netdev, int new_mtu)
1127 {
1128         /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
1129          * and mtu is outbound only anyway */
1130         if ((new_mtu < GELIC_NET_MIN_MTU) ||
1131             (new_mtu > GELIC_NET_MAX_MTU)) {
1132                 return -EINVAL;
1133         }
1134         netdev->mtu = new_mtu;
1135         return 0;
1136 }
1137
1138 /**
1139  * gelic_card_interrupt - event handler for gelic_net
1140  */
1141 static irqreturn_t gelic_card_interrupt(int irq, void *ptr)
1142 {
1143         unsigned long flags;
1144         struct gelic_card *card = ptr;
1145         u64 status;
1146
1147         status = card->irq_status;
1148
1149         if (!status)
1150                 return IRQ_NONE;
1151
1152         status &= card->irq_mask;
1153
1154         if (card->rx_dma_restart_required) {
1155                 card->rx_dma_restart_required = 0;
1156                 gelic_card_enable_rxdmac(card);
1157         }
1158
1159         if (status & GELIC_CARD_RXINT) {
1160                 gelic_card_rx_irq_off(card);
1161                 napi_schedule(&card->napi);
1162         }
1163
1164         if (status & GELIC_CARD_TXINT) {
1165                 spin_lock_irqsave(&card->tx_lock, flags);
1166                 card->tx_dma_progress = 0;
1167                 gelic_card_release_tx_chain(card, 0);
1168                 /* kick outstanding tx descriptor if any */
1169                 gelic_card_kick_txdma(card, card->tx_chain.tail);
1170                 spin_unlock_irqrestore(&card->tx_lock, flags);
1171         }
1172
1173         /* ether port status changed */
1174         if (status & GELIC_CARD_PORT_STATUS_CHANGED)
1175                 gelic_card_get_ether_port_status(card, 1);
1176
1177 #ifdef CONFIG_GELIC_WIRELESS
1178         if (status & (GELIC_CARD_WLAN_EVENT_RECEIVED |
1179                       GELIC_CARD_WLAN_COMMAND_COMPLETED))
1180                 gelic_wl_interrupt(card->netdev[GELIC_PORT_WIRELESS], status);
1181 #endif
1182
1183         return IRQ_HANDLED;
1184 }
1185
1186 #ifdef CONFIG_NET_POLL_CONTROLLER
1187 /**
1188  * gelic_net_poll_controller - artificial interrupt for netconsole etc.
1189  * @netdev: interface device structure
1190  *
1191  * see Documentation/networking/netconsole.txt
1192  */
1193 void gelic_net_poll_controller(struct net_device *netdev)
1194 {
1195         struct gelic_card *card = netdev_card(netdev);
1196
1197         gelic_card_set_irq_mask(card, 0);
1198         gelic_card_interrupt(netdev->irq, netdev);
1199         gelic_card_set_irq_mask(card, card->irq_mask);
1200 }
1201 #endif /* CONFIG_NET_POLL_CONTROLLER */
1202
1203 /**
1204  * gelic_net_open - called upon ifonfig up
1205  * @netdev: interface device structure
1206  *
1207  * returns 0 on success, <0 on failure
1208  *
1209  * gelic_net_open allocates all the descriptors and memory needed for
1210  * operation, sets up multicast list and enables interrupts
1211  */
1212 int gelic_net_open(struct net_device *netdev)
1213 {
1214         struct gelic_card *card = netdev_card(netdev);
1215
1216         dev_dbg(ctodev(card), " -> %s %p\n", __func__, netdev);
1217
1218         gelic_card_up(card);
1219
1220         netif_start_queue(netdev);
1221         gelic_card_get_ether_port_status(card, 1);
1222
1223         dev_dbg(ctodev(card), " <- %s\n", __func__);
1224         return 0;
1225 }
1226
1227 void gelic_net_get_drvinfo(struct net_device *netdev,
1228                            struct ethtool_drvinfo *info)
1229 {
1230         strncpy(info->driver, DRV_NAME, sizeof(info->driver) - 1);
1231         strncpy(info->version, DRV_VERSION, sizeof(info->version) - 1);
1232 }
1233
1234 static int gelic_ether_get_settings(struct net_device *netdev,
1235                                     struct ethtool_cmd *cmd)
1236 {
1237         struct gelic_card *card = netdev_card(netdev);
1238
1239         gelic_card_get_ether_port_status(card, 0);
1240
1241         if (card->ether_port_status & GELIC_LV1_ETHER_FULL_DUPLEX)
1242                 cmd->duplex = DUPLEX_FULL;
1243         else
1244                 cmd->duplex = DUPLEX_HALF;
1245
1246         switch (card->ether_port_status & GELIC_LV1_ETHER_SPEED_MASK) {
1247         case GELIC_LV1_ETHER_SPEED_10:
1248                 cmd->speed = SPEED_10;
1249                 break;
1250         case GELIC_LV1_ETHER_SPEED_100:
1251                 cmd->speed = SPEED_100;
1252                 break;
1253         case GELIC_LV1_ETHER_SPEED_1000:
1254                 cmd->speed = SPEED_1000;
1255                 break;
1256         default:
1257                 pr_info("%s: speed unknown\n", __func__);
1258                 cmd->speed = SPEED_10;
1259                 break;
1260         }
1261
1262         cmd->supported = SUPPORTED_TP | SUPPORTED_Autoneg |
1263                         SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
1264                         SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
1265                         SUPPORTED_1000baseT_Full;
1266         cmd->advertising = cmd->supported;
1267         if (card->link_mode & GELIC_LV1_ETHER_AUTO_NEG) {
1268                 cmd->autoneg = AUTONEG_ENABLE;
1269         } else {
1270                 cmd->autoneg = AUTONEG_DISABLE;
1271                 cmd->advertising &= ~ADVERTISED_Autoneg;
1272         }
1273         cmd->port = PORT_TP;
1274
1275         return 0;
1276 }
1277
1278 static int gelic_ether_set_settings(struct net_device *netdev,
1279                                     struct ethtool_cmd *cmd)
1280 {
1281         struct gelic_card *card = netdev_card(netdev);
1282         u64 mode;
1283         int ret;
1284
1285         if (cmd->autoneg == AUTONEG_ENABLE) {
1286                 mode = GELIC_LV1_ETHER_AUTO_NEG;
1287         } else {
1288                 switch (cmd->speed) {
1289                 case SPEED_10:
1290                         mode = GELIC_LV1_ETHER_SPEED_10;
1291                         break;
1292                 case SPEED_100:
1293                         mode = GELIC_LV1_ETHER_SPEED_100;
1294                         break;
1295                 case SPEED_1000:
1296                         mode = GELIC_LV1_ETHER_SPEED_1000;
1297                         break;
1298                 default:
1299                         return -EINVAL;
1300                 }
1301                 if (cmd->duplex == DUPLEX_FULL)
1302                         mode |= GELIC_LV1_ETHER_FULL_DUPLEX;
1303                 else if (cmd->speed == SPEED_1000) {
1304                         pr_info("1000 half duplex is not supported.\n");
1305                         return -EINVAL;
1306                 }
1307         }
1308
1309         ret = gelic_card_set_link_mode(card, mode);
1310
1311         if (ret)
1312                 return ret;
1313
1314         return 0;
1315 }
1316
1317 u32 gelic_net_get_rx_csum(struct net_device *netdev)
1318 {
1319         struct gelic_card *card = netdev_card(netdev);
1320
1321         return card->rx_csum;
1322 }
1323
1324 int gelic_net_set_rx_csum(struct net_device *netdev, u32 data)
1325 {
1326         struct gelic_card *card = netdev_card(netdev);
1327
1328         card->rx_csum = data;
1329         return 0;
1330 }
1331
1332 static void gelic_net_get_wol(struct net_device *netdev,
1333                               struct ethtool_wolinfo *wol)
1334 {
1335         if (0 <= ps3_compare_firmware_version(2, 2, 0))
1336                 wol->supported = WAKE_MAGIC;
1337         else
1338                 wol->supported = 0;
1339
1340         wol->wolopts = ps3_sys_manager_get_wol() ? wol->supported : 0;
1341         memset(&wol->sopass, 0, sizeof(wol->sopass));
1342 }
1343 static int gelic_net_set_wol(struct net_device *netdev,
1344                              struct ethtool_wolinfo *wol)
1345 {
1346         int status;
1347         struct gelic_card *card;
1348         u64 v1, v2;
1349
1350         if (ps3_compare_firmware_version(2, 2, 0) < 0 ||
1351             !capable(CAP_NET_ADMIN))
1352                 return -EPERM;
1353
1354         if (wol->wolopts & ~WAKE_MAGIC)
1355                 return -EINVAL;
1356
1357         card = netdev_card(netdev);
1358         if (wol->wolopts & WAKE_MAGIC) {
1359                 status = lv1_net_control(bus_id(card), dev_id(card),
1360                                          GELIC_LV1_SET_WOL,
1361                                          GELIC_LV1_WOL_MAGIC_PACKET,
1362                                          0, GELIC_LV1_WOL_MP_ENABLE,
1363                                          &v1, &v2);
1364                 if (status) {
1365                         pr_info("%s: enabling WOL failed %d\n", __func__,
1366                                 status);
1367                         status = -EIO;
1368                         goto done;
1369                 }
1370                 status = lv1_net_control(bus_id(card), dev_id(card),
1371                                          GELIC_LV1_SET_WOL,
1372                                          GELIC_LV1_WOL_ADD_MATCH_ADDR,
1373                                          0, GELIC_LV1_WOL_MATCH_ALL,
1374                                          &v1, &v2);
1375                 if (!status)
1376                         ps3_sys_manager_set_wol(1);
1377                 else {
1378                         pr_info("%s: enabling WOL filter failed %d\n",
1379                                 __func__, status);
1380                         status = -EIO;
1381                 }
1382         } else {
1383                 status = lv1_net_control(bus_id(card), dev_id(card),
1384                                          GELIC_LV1_SET_WOL,
1385                                          GELIC_LV1_WOL_MAGIC_PACKET,
1386                                          0, GELIC_LV1_WOL_MP_DISABLE,
1387                                          &v1, &v2);
1388                 if (status) {
1389                         pr_info("%s: disabling WOL failed %d\n", __func__,
1390                                 status);
1391                         status = -EIO;
1392                         goto done;
1393                 }
1394                 status = lv1_net_control(bus_id(card), dev_id(card),
1395                                          GELIC_LV1_SET_WOL,
1396                                          GELIC_LV1_WOL_DELETE_MATCH_ADDR,
1397                                          0, GELIC_LV1_WOL_MATCH_ALL,
1398                                          &v1, &v2);
1399                 if (!status)
1400                         ps3_sys_manager_set_wol(0);
1401                 else {
1402                         pr_info("%s: removing WOL filter failed %d\n",
1403                                 __func__, status);
1404                         status = -EIO;
1405                 }
1406         }
1407 done:
1408         return status;
1409 }
1410
1411 static const struct ethtool_ops gelic_ether_ethtool_ops = {
1412         .get_drvinfo    = gelic_net_get_drvinfo,
1413         .get_settings   = gelic_ether_get_settings,
1414         .set_settings   = gelic_ether_set_settings,
1415         .get_link       = ethtool_op_get_link,
1416         .get_tx_csum    = ethtool_op_get_tx_csum,
1417         .set_tx_csum    = ethtool_op_set_tx_csum,
1418         .get_rx_csum    = gelic_net_get_rx_csum,
1419         .set_rx_csum    = gelic_net_set_rx_csum,
1420         .get_wol        = gelic_net_get_wol,
1421         .set_wol        = gelic_net_set_wol,
1422 };
1423
1424 /**
1425  * gelic_net_tx_timeout_task - task scheduled by the watchdog timeout
1426  * function (to be called not under interrupt status)
1427  * @work: work is context of tx timout task
1428  *
1429  * called as task when tx hangs, resets interface (if interface is up)
1430  */
1431 static void gelic_net_tx_timeout_task(struct work_struct *work)
1432 {
1433         struct gelic_card *card =
1434                 container_of(work, struct gelic_card, tx_timeout_task);
1435         struct net_device *netdev = card->netdev[GELIC_PORT_ETHERNET_0];
1436
1437         dev_info(ctodev(card), "%s:Timed out. Restarting... \n", __func__);
1438
1439         if (!(netdev->flags & IFF_UP))
1440                 goto out;
1441
1442         netif_device_detach(netdev);
1443         gelic_net_stop(netdev);
1444
1445         gelic_net_open(netdev);
1446         netif_device_attach(netdev);
1447
1448 out:
1449         atomic_dec(&card->tx_timeout_task_counter);
1450 }
1451
1452 /**
1453  * gelic_net_tx_timeout - called when the tx timeout watchdog kicks in.
1454  * @netdev: interface device structure
1455  *
1456  * called, if tx hangs. Schedules a task that resets the interface
1457  */
1458 void gelic_net_tx_timeout(struct net_device *netdev)
1459 {
1460         struct gelic_card *card;
1461
1462         card = netdev_card(netdev);
1463         atomic_inc(&card->tx_timeout_task_counter);
1464         if (netdev->flags & IFF_UP)
1465                 schedule_work(&card->tx_timeout_task);
1466         else
1467                 atomic_dec(&card->tx_timeout_task_counter);
1468 }
1469
1470 static const struct net_device_ops gelic_netdevice_ops = {
1471         .ndo_open = gelic_net_open,
1472         .ndo_stop = gelic_net_stop,
1473         .ndo_start_xmit = gelic_net_xmit,
1474         .ndo_set_multicast_list = gelic_net_set_multi,
1475         .ndo_change_mtu = gelic_net_change_mtu,
1476         .ndo_tx_timeout = gelic_net_tx_timeout,
1477         .ndo_set_mac_address = eth_mac_addr,
1478         .ndo_validate_addr = eth_validate_addr,
1479 #ifdef CONFIG_NET_POLL_CONTROLLER
1480         .ndo_poll_controller = gelic_net_poll_controller,
1481 #endif
1482 };
1483
1484 /**
1485  * gelic_ether_setup_netdev_ops - initialization of net_device operations
1486  * @netdev: net_device structure
1487  *
1488  * fills out function pointers in the net_device structure
1489  */
1490 static void __devinit gelic_ether_setup_netdev_ops(struct net_device *netdev,
1491                                                    struct napi_struct *napi)
1492 {
1493         netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;
1494         /* NAPI */
1495         netif_napi_add(netdev, napi,
1496                        gelic_net_poll, GELIC_NET_NAPI_WEIGHT);
1497         netdev->ethtool_ops = &gelic_ether_ethtool_ops;
1498         netdev->netdev_ops = &gelic_netdevice_ops;
1499 }
1500
1501 /**
1502  * gelic_ether_setup_netdev - initialization of net_device
1503  * @netdev: net_device structure
1504  * @card: card structure
1505  *
1506  * Returns 0 on success or <0 on failure
1507  *
1508  * gelic_ether_setup_netdev initializes the net_device structure
1509  * and register it.
1510  **/
1511 int __devinit gelic_net_setup_netdev(struct net_device *netdev,
1512                                      struct gelic_card *card)
1513 {
1514         int status;
1515         u64 v1, v2;
1516
1517         netdev->features = NETIF_F_IP_CSUM;
1518
1519         status = lv1_net_control(bus_id(card), dev_id(card),
1520                                  GELIC_LV1_GET_MAC_ADDRESS,
1521                                  0, 0, 0, &v1, &v2);
1522         v1 <<= 16;
1523         if (status || !is_valid_ether_addr((u8 *)&v1)) {
1524                 dev_info(ctodev(card),
1525                          "%s:lv1_net_control GET_MAC_ADDR failed %d\n",
1526                          __func__, status);
1527                 return -EINVAL;
1528         }
1529         memcpy(netdev->dev_addr, &v1, ETH_ALEN);
1530
1531         if (card->vlan_required) {
1532                 netdev->hard_header_len += VLAN_HLEN;
1533                 /*
1534                  * As vlan is internally used,
1535                  * we can not receive vlan packets
1536                  */
1537                 netdev->features |= NETIF_F_VLAN_CHALLENGED;
1538         }
1539
1540         status = register_netdev(netdev);
1541         if (status) {
1542                 dev_err(ctodev(card), "%s:Couldn't register %s %d\n",
1543                         __func__, netdev->name, status);
1544                 return status;
1545         }
1546         dev_info(ctodev(card), "%s: MAC addr %pM\n",
1547                  netdev->name, netdev->dev_addr);
1548
1549         return 0;
1550 }
1551
1552 /**
1553  * gelic_alloc_card_net - allocates net_device and card structure
1554  *
1555  * returns the card structure or NULL in case of errors
1556  *
1557  * the card and net_device structures are linked to each other
1558  */
1559 #define GELIC_ALIGN (32)
1560 static struct gelic_card * __devinit gelic_alloc_card_net(struct net_device **netdev)
1561 {
1562         struct gelic_card *card;
1563         struct gelic_port *port;
1564         void *p;
1565         size_t alloc_size;
1566         /*
1567          * gelic requires dma descriptor is 32 bytes aligned and
1568          * the hypervisor requires irq_status is 8 bytes aligned.
1569          */
1570         BUILD_BUG_ON(offsetof(struct gelic_card, irq_status) % 8);
1571         BUILD_BUG_ON(offsetof(struct gelic_card, descr) % 32);
1572         alloc_size =
1573                 sizeof(struct gelic_card) +
1574                 sizeof(struct gelic_descr) * GELIC_NET_RX_DESCRIPTORS +
1575                 sizeof(struct gelic_descr) * GELIC_NET_TX_DESCRIPTORS +
1576                 GELIC_ALIGN - 1;
1577
1578         p  = kzalloc(alloc_size, GFP_KERNEL);
1579         if (!p)
1580                 return NULL;
1581         card = PTR_ALIGN(p, GELIC_ALIGN);
1582         card->unalign = p;
1583
1584         /*
1585          * alloc netdev
1586          */
1587         *netdev = alloc_etherdev(sizeof(struct gelic_port));
1588         if (!netdev) {
1589                 kfree(card->unalign);
1590                 return NULL;
1591         }
1592         port = netdev_priv(*netdev);
1593
1594         /* gelic_port */
1595         port->netdev = *netdev;
1596         port->card = card;
1597         port->type = GELIC_PORT_ETHERNET_0;
1598
1599         /* gelic_card */
1600         card->netdev[GELIC_PORT_ETHERNET_0] = *netdev;
1601
1602         INIT_WORK(&card->tx_timeout_task, gelic_net_tx_timeout_task);
1603         init_waitqueue_head(&card->waitq);
1604         atomic_set(&card->tx_timeout_task_counter, 0);
1605         mutex_init(&card->updown_lock);
1606         atomic_set(&card->users, 0);
1607
1608         return card;
1609 }
1610
1611 static void __devinit gelic_card_get_vlan_info(struct gelic_card *card)
1612 {
1613         u64 v1, v2;
1614         int status;
1615         unsigned int i;
1616         struct {
1617                 int tx;
1618                 int rx;
1619         } vlan_id_ix[2] = {
1620                 [GELIC_PORT_ETHERNET_0] = {
1621                         .tx = GELIC_LV1_VLAN_TX_ETHERNET_0,
1622                         .rx = GELIC_LV1_VLAN_RX_ETHERNET_0
1623                 },
1624                 [GELIC_PORT_WIRELESS] = {
1625                         .tx = GELIC_LV1_VLAN_TX_WIRELESS,
1626                         .rx = GELIC_LV1_VLAN_RX_WIRELESS
1627                 }
1628         };
1629
1630         for (i = 0; i < ARRAY_SIZE(vlan_id_ix); i++) {
1631                 /* tx tag */
1632                 status = lv1_net_control(bus_id(card), dev_id(card),
1633                                          GELIC_LV1_GET_VLAN_ID,
1634                                          vlan_id_ix[i].tx,
1635                                          0, 0, &v1, &v2);
1636                 if (status || !v1) {
1637                         if (status != LV1_NO_ENTRY)
1638                                 dev_dbg(ctodev(card),
1639                                         "get vlan id for tx(%d) failed(%d)\n",
1640                                         vlan_id_ix[i].tx, status);
1641                         card->vlan[i].tx = 0;
1642                         card->vlan[i].rx = 0;
1643                         continue;
1644                 }
1645                 card->vlan[i].tx = (u16)v1;
1646
1647                 /* rx tag */
1648                 status = lv1_net_control(bus_id(card), dev_id(card),
1649                                          GELIC_LV1_GET_VLAN_ID,
1650                                          vlan_id_ix[i].rx,
1651                                          0, 0, &v1, &v2);
1652                 if (status || !v1) {
1653                         if (status != LV1_NO_ENTRY)
1654                                 dev_info(ctodev(card),
1655                                          "get vlan id for rx(%d) failed(%d)\n",
1656                                          vlan_id_ix[i].rx, status);
1657                         card->vlan[i].tx = 0;
1658                         card->vlan[i].rx = 0;
1659                         continue;
1660                 }
1661                 card->vlan[i].rx = (u16)v1;
1662
1663                 dev_dbg(ctodev(card), "vlan_id[%d] tx=%02x rx=%02x\n",
1664                         i, card->vlan[i].tx, card->vlan[i].rx);
1665         }
1666
1667         if (card->vlan[GELIC_PORT_ETHERNET_0].tx) {
1668                 BUG_ON(!card->vlan[GELIC_PORT_WIRELESS].tx);
1669                 card->vlan_required = 1;
1670         } else
1671                 card->vlan_required = 0;
1672
1673         /* check wirelss capable firmware */
1674         if (ps3_compare_firmware_version(1, 6, 0) < 0) {
1675                 card->vlan[GELIC_PORT_WIRELESS].tx = 0;
1676                 card->vlan[GELIC_PORT_WIRELESS].rx = 0;
1677         }
1678
1679         dev_info(ctodev(card), "internal vlan %s\n",
1680                  card->vlan_required? "enabled" : "disabled");
1681 }
1682 /**
1683  * ps3_gelic_driver_probe - add a device to the control of this driver
1684  */
1685 static int __devinit ps3_gelic_driver_probe(struct ps3_system_bus_device *dev)
1686 {
1687         struct gelic_card *card;
1688         struct net_device *netdev;
1689         int result;
1690
1691         pr_debug("%s: called\n", __func__);
1692         result = ps3_open_hv_device(dev);
1693
1694         if (result) {
1695                 dev_dbg(&dev->core, "%s:ps3_open_hv_device failed\n",
1696                         __func__);
1697                 goto fail_open;
1698         }
1699
1700         result = ps3_dma_region_create(dev->d_region);
1701
1702         if (result) {
1703                 dev_dbg(&dev->core, "%s:ps3_dma_region_create failed(%d)\n",
1704                         __func__, result);
1705                 BUG_ON("check region type");
1706                 goto fail_dma_region;
1707         }
1708
1709         /* alloc card/netdevice */
1710         card = gelic_alloc_card_net(&netdev);
1711         if (!card) {
1712                 dev_info(&dev->core, "%s:gelic_net_alloc_card failed\n",
1713                          __func__);
1714                 result = -ENOMEM;
1715                 goto fail_alloc_card;
1716         }
1717         ps3_system_bus_set_drvdata(dev, card);
1718         card->dev = dev;
1719
1720         /* get internal vlan info */
1721         gelic_card_get_vlan_info(card);
1722
1723         card->link_mode = GELIC_LV1_ETHER_AUTO_NEG;
1724
1725         /* setup interrupt */
1726         result = lv1_net_set_interrupt_status_indicator(bus_id(card),
1727                                                         dev_id(card),
1728                 ps3_mm_phys_to_lpar(__pa(&card->irq_status)),
1729                 0);
1730
1731         if (result) {
1732                 dev_dbg(&dev->core,
1733                         "%s:set_interrupt_status_indicator failed: %s\n",
1734                         __func__, ps3_result(result));
1735                 result = -EIO;
1736                 goto fail_status_indicator;
1737         }
1738
1739         result = ps3_sb_event_receive_port_setup(dev, PS3_BINDING_CPU_ANY,
1740                 &card->irq);
1741
1742         if (result) {
1743                 dev_info(ctodev(card),
1744                          "%s:gelic_net_open_device failed (%d)\n",
1745                          __func__, result);
1746                 result = -EPERM;
1747                 goto fail_alloc_irq;
1748         }
1749         result = request_irq(card->irq, gelic_card_interrupt,
1750                              IRQF_DISABLED, netdev->name, card);
1751
1752         if (result) {
1753                 dev_info(ctodev(card), "%s:request_irq failed (%d)\n",
1754                         __func__, result);
1755                 goto fail_request_irq;
1756         }
1757
1758         /* setup card structure */
1759         card->irq_mask = GELIC_CARD_RXINT | GELIC_CARD_TXINT |
1760                 GELIC_CARD_PORT_STATUS_CHANGED;
1761         card->rx_csum = GELIC_CARD_RX_CSUM_DEFAULT;
1762
1763
1764         if (gelic_card_init_chain(card, &card->tx_chain,
1765                         card->descr, GELIC_NET_TX_DESCRIPTORS))
1766                 goto fail_alloc_tx;
1767         if (gelic_card_init_chain(card, &card->rx_chain,
1768                                  card->descr + GELIC_NET_TX_DESCRIPTORS,
1769                                  GELIC_NET_RX_DESCRIPTORS))
1770                 goto fail_alloc_rx;
1771
1772         /* head of chain */
1773         card->tx_top = card->tx_chain.head;
1774         card->rx_top = card->rx_chain.head;
1775         dev_dbg(ctodev(card), "descr rx %p, tx %p, size %#lx, num %#x\n",
1776                 card->rx_top, card->tx_top, sizeof(struct gelic_descr),
1777                 GELIC_NET_RX_DESCRIPTORS);
1778         /* allocate rx skbs */
1779         if (gelic_card_alloc_rx_skbs(card))
1780                 goto fail_alloc_skbs;
1781
1782         spin_lock_init(&card->tx_lock);
1783         card->tx_dma_progress = 0;
1784
1785         /* setup net_device structure */
1786         netdev->irq = card->irq;
1787         SET_NETDEV_DEV(netdev, &card->dev->core);
1788         gelic_ether_setup_netdev_ops(netdev, &card->napi);
1789         result = gelic_net_setup_netdev(netdev, card);
1790         if (result) {
1791                 dev_dbg(&dev->core, "%s: setup_netdev failed %d",
1792                         __func__, result);
1793                 goto fail_setup_netdev;
1794         }
1795
1796 #ifdef CONFIG_GELIC_WIRELESS
1797         if (gelic_wl_driver_probe(card)) {
1798                 dev_dbg(&dev->core, "%s: WL init failed\n", __func__);
1799                 goto fail_setup_netdev;
1800         }
1801 #endif
1802         pr_debug("%s: done\n", __func__);
1803         return 0;
1804
1805 fail_setup_netdev:
1806 fail_alloc_skbs:
1807         gelic_card_free_chain(card, card->rx_chain.head);
1808 fail_alloc_rx:
1809         gelic_card_free_chain(card, card->tx_chain.head);
1810 fail_alloc_tx:
1811         free_irq(card->irq, card);
1812         netdev->irq = NO_IRQ;
1813 fail_request_irq:
1814         ps3_sb_event_receive_port_destroy(dev, card->irq);
1815 fail_alloc_irq:
1816         lv1_net_set_interrupt_status_indicator(bus_id(card),
1817                                                bus_id(card),
1818                                                0, 0);
1819 fail_status_indicator:
1820         ps3_system_bus_set_drvdata(dev, NULL);
1821         kfree(netdev_card(netdev)->unalign);
1822         free_netdev(netdev);
1823 fail_alloc_card:
1824         ps3_dma_region_free(dev->d_region);
1825 fail_dma_region:
1826         ps3_close_hv_device(dev);
1827 fail_open:
1828         return result;
1829 }
1830
1831 /**
1832  * ps3_gelic_driver_remove - remove a device from the control of this driver
1833  */
1834
1835 static int ps3_gelic_driver_remove(struct ps3_system_bus_device *dev)
1836 {
1837         struct gelic_card *card = ps3_system_bus_get_drvdata(dev);
1838         struct net_device *netdev0;
1839         pr_debug("%s: called\n", __func__);
1840
1841         /* set auto-negotiation */
1842         gelic_card_set_link_mode(card, GELIC_LV1_ETHER_AUTO_NEG);
1843
1844 #ifdef CONFIG_GELIC_WIRELESS
1845         gelic_wl_driver_remove(card);
1846 #endif
1847         /* stop interrupt */
1848         gelic_card_set_irq_mask(card, 0);
1849
1850         /* turn off DMA, force end */
1851         gelic_card_disable_rxdmac(card);
1852         gelic_card_disable_txdmac(card);
1853
1854         /* release chains */
1855         gelic_card_release_tx_chain(card, 1);
1856         gelic_card_release_rx_chain(card);
1857
1858         gelic_card_free_chain(card, card->tx_top);
1859         gelic_card_free_chain(card, card->rx_top);
1860
1861         netdev0 = card->netdev[GELIC_PORT_ETHERNET_0];
1862         /* disconnect event port */
1863         free_irq(card->irq, card);
1864         netdev0->irq = NO_IRQ;
1865         ps3_sb_event_receive_port_destroy(card->dev, card->irq);
1866
1867         wait_event(card->waitq,
1868                    atomic_read(&card->tx_timeout_task_counter) == 0);
1869
1870         lv1_net_set_interrupt_status_indicator(bus_id(card), dev_id(card),
1871                                                0 , 0);
1872
1873         unregister_netdev(netdev0);
1874         kfree(netdev_card(netdev0)->unalign);
1875         free_netdev(netdev0);
1876
1877         ps3_system_bus_set_drvdata(dev, NULL);
1878
1879         ps3_dma_region_free(dev->d_region);
1880
1881         ps3_close_hv_device(dev);
1882
1883         pr_debug("%s: done\n", __func__);
1884         return 0;
1885 }
1886
1887 static struct ps3_system_bus_driver ps3_gelic_driver = {
1888         .match_id = PS3_MATCH_ID_GELIC,
1889         .probe = ps3_gelic_driver_probe,
1890         .remove = ps3_gelic_driver_remove,
1891         .shutdown = ps3_gelic_driver_remove,
1892         .core.name = "ps3_gelic_driver",
1893         .core.owner = THIS_MODULE,
1894 };
1895
1896 static int __init ps3_gelic_driver_init (void)
1897 {
1898         return firmware_has_feature(FW_FEATURE_PS3_LV1)
1899                 ? ps3_system_bus_driver_register(&ps3_gelic_driver)
1900                 : -ENODEV;
1901 }
1902
1903 static void __exit ps3_gelic_driver_exit (void)
1904 {
1905         ps3_system_bus_driver_unregister(&ps3_gelic_driver);
1906 }
1907
1908 module_init(ps3_gelic_driver_init);
1909 module_exit(ps3_gelic_driver_exit);
1910
1911 MODULE_ALIAS(PS3_MODULE_ALIAS_GELIC);
1912