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