1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
56MODULE_AUTHOR("SCE Inc.");
57MODULE_DESCRIPTION("Gelic Network driver");
58MODULE_LICENSE("GPL");
59
60
61
62int 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
74static 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}
79static 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
85static 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
107
108
109
110
111static enum gelic_descr_dma_status
112gelic_descr_get_status(struct gelic_descr *descr)
113{
114 return be32_to_cpu(descr->dmac_cmd_status) & GELIC_DESCR_DMA_STAT_MASK;
115}
116
117static 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
137
138
139
140
141
142static void gelic_card_disable_txdmac(struct gelic_card *card)
143{
144 int status;
145
146
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
155
156
157
158
159
160static 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
184
185
186
187
188
189static void gelic_card_disable_rxdmac(struct gelic_card *card)
190{
191 int status;
192
193
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
202
203
204
205
206
207
208static 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
216
217
218
219
220 wmb();
221}
222
223
224
225
226
227
228
229
230
231
232static 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
249void 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
256 gelic_card_set_irq_mask(card, card->irq_mask);
257
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
266void 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
276
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
282 gelic_card_disable_rxdmac(card);
283 gelic_card_reset_chain(card, &card->rx_chain,
284 card->descr + GELIC_NET_TX_DESCRIPTORS);
285
286 gelic_card_disable_txdmac(card);
287 }
288 mutex_unlock(&card->updown_lock);
289 pr_debug("%s: done\n", __func__);
290}
291
292
293
294
295
296
297static 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
311
312
313
314
315
316
317
318
319
320
321static 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
332 for (i = 0; i < no; i++, descr++) {
333 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
334 descr->bus_addr =
335 dma_map_single(ctodev(card), descr,
336 GELIC_DESCR_SIZE,
337 DMA_BIDIRECTIONAL);
338
339 if (!descr->bus_addr)
340 goto iommu_error;
341
342 descr->next = descr + 1;
343 descr->prev = descr - 1;
344 }
345
346 (descr - 1)->next = start_descr;
347 start_descr->prev = (descr - 1);
348
349
350 descr = start_descr;
351 for (i = 0; i < no; i++, descr++) {
352 descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
353 }
354
355 chain->head = start_descr;
356 chain->tail = start_descr;
357
358
359 (descr - 1)->next_descr_addr = 0;
360
361 return 0;
362
363iommu_error:
364 for (i--, descr--; 0 <= i; i--, descr--)
365 if (descr->bus_addr)
366 dma_unmap_single(ctodev(card), descr->bus_addr,
367 GELIC_DESCR_SIZE,
368 DMA_BIDIRECTIONAL);
369 return -ENOMEM;
370}
371
372
373
374
375
376
377
378
379
380
381
382static int gelic_descr_prepare_rx(struct gelic_card *card,
383 struct gelic_descr *descr)
384{
385 int offset;
386 unsigned int bufsize;
387
388 if (gelic_descr_get_status(descr) != GELIC_DESCR_DMA_NOT_IN_USE)
389 dev_info(ctodev(card), "%s: ERROR status\n", __func__);
390
391 bufsize = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN);
392
393
394
395 descr->skb = dev_alloc_skb(bufsize + GELIC_NET_RXBUF_ALIGN - 1);
396 if (!descr->skb) {
397 descr->buf_addr = 0;
398 dev_info(ctodev(card),
399 "%s:allocate skb failed !!\n", __func__);
400 return -ENOMEM;
401 }
402 descr->buf_size = cpu_to_be32(bufsize);
403 descr->dmac_cmd_status = 0;
404 descr->result_size = 0;
405 descr->valid_size = 0;
406 descr->data_error = 0;
407
408 offset = ((unsigned long)descr->skb->data) &
409 (GELIC_NET_RXBUF_ALIGN - 1);
410 if (offset)
411 skb_reserve(descr->skb, GELIC_NET_RXBUF_ALIGN - offset);
412
413 descr->buf_addr = cpu_to_be32(dma_map_single(ctodev(card),
414 descr->skb->data,
415 GELIC_NET_MAX_MTU,
416 DMA_FROM_DEVICE));
417 if (!descr->buf_addr) {
418 dev_kfree_skb_any(descr->skb);
419 descr->skb = NULL;
420 dev_info(ctodev(card),
421 "%s:Could not iommu-map rx buffer\n", __func__);
422 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
423 return -ENOMEM;
424 } else {
425 gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
426 return 0;
427 }
428}
429
430
431
432
433
434
435static void gelic_card_release_rx_chain(struct gelic_card *card)
436{
437 struct gelic_descr *descr = card->rx_chain.head;
438
439 do {
440 if (descr->skb) {
441 dma_unmap_single(ctodev(card),
442 be32_to_cpu(descr->buf_addr),
443 descr->skb->len,
444 DMA_FROM_DEVICE);
445 descr->buf_addr = 0;
446 dev_kfree_skb_any(descr->skb);
447 descr->skb = NULL;
448 gelic_descr_set_status(descr,
449 GELIC_DESCR_DMA_NOT_IN_USE);
450 }
451 descr = descr->next;
452 } while (descr != card->rx_chain.head);
453}
454
455
456
457
458
459
460
461
462
463static int gelic_card_fill_rx_chain(struct gelic_card *card)
464{
465 struct gelic_descr *descr = card->rx_chain.head;
466 int ret;
467
468 do {
469 if (!descr->skb) {
470 ret = gelic_descr_prepare_rx(card, descr);
471 if (ret)
472 goto rewind;
473 }
474 descr = descr->next;
475 } while (descr != card->rx_chain.head);
476
477 return 0;
478rewind:
479 gelic_card_release_rx_chain(card);
480 return ret;
481}
482
483
484
485
486
487
488
489static int gelic_card_alloc_rx_skbs(struct gelic_card *card)
490{
491 struct gelic_descr_chain *chain;
492 int ret;
493 chain = &card->rx_chain;
494 ret = gelic_card_fill_rx_chain(card);
495 chain->tail = card->rx_top->prev;
496 return ret;
497}
498
499
500
501
502
503
504
505
506static void gelic_descr_release_tx(struct gelic_card *card,
507 struct gelic_descr *descr)
508{
509 struct sk_buff *skb = descr->skb;
510
511 BUG_ON(!(be32_to_cpu(descr->data_status) & GELIC_DESCR_TX_TAIL));
512
513 dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr), skb->len,
514 DMA_TO_DEVICE);
515 dev_kfree_skb_any(skb);
516
517 descr->buf_addr = 0;
518 descr->buf_size = 0;
519 descr->next_descr_addr = 0;
520 descr->result_size = 0;
521 descr->valid_size = 0;
522 descr->data_status = 0;
523 descr->data_error = 0;
524 descr->skb = NULL;
525
526
527 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
528}
529
530static void gelic_card_stop_queues(struct gelic_card *card)
531{
532 netif_stop_queue(card->netdev[GELIC_PORT_ETHERNET_0]);
533
534 if (card->netdev[GELIC_PORT_WIRELESS])
535 netif_stop_queue(card->netdev[GELIC_PORT_WIRELESS]);
536}
537static void gelic_card_wake_queues(struct gelic_card *card)
538{
539 netif_wake_queue(card->netdev[GELIC_PORT_ETHERNET_0]);
540
541 if (card->netdev[GELIC_PORT_WIRELESS])
542 netif_wake_queue(card->netdev[GELIC_PORT_WIRELESS]);
543}
544
545
546
547
548
549
550
551static void gelic_card_release_tx_chain(struct gelic_card *card, int stop)
552{
553 struct gelic_descr_chain *tx_chain;
554 enum gelic_descr_dma_status status;
555 struct net_device *netdev;
556 int release = 0;
557
558 for (tx_chain = &card->tx_chain;
559 tx_chain->head != tx_chain->tail && tx_chain->tail;
560 tx_chain->tail = tx_chain->tail->next) {
561 status = gelic_descr_get_status(tx_chain->tail);
562 netdev = tx_chain->tail->skb->dev;
563 switch (status) {
564 case GELIC_DESCR_DMA_RESPONSE_ERROR:
565 case GELIC_DESCR_DMA_PROTECTION_ERROR:
566 case GELIC_DESCR_DMA_FORCE_END:
567 if (printk_ratelimit())
568 dev_info(ctodev(card),
569 "%s: forcing end of tx descriptor " \
570 "with status %x\n",
571 __func__, status);
572 netdev->stats.tx_dropped++;
573 break;
574
575 case GELIC_DESCR_DMA_COMPLETE:
576 if (tx_chain->tail->skb) {
577 netdev->stats.tx_packets++;
578 netdev->stats.tx_bytes +=
579 tx_chain->tail->skb->len;
580 }
581 break;
582
583 case GELIC_DESCR_DMA_CARDOWNED:
584
585 default:
586
587 if (!stop)
588 goto out;
589 }
590 gelic_descr_release_tx(card, tx_chain->tail);
591 release ++;
592 }
593out:
594 if (!stop && release)
595 gelic_card_wake_queues(card);
596}
597
598
599
600
601
602
603
604
605
606void gelic_net_set_multi(struct net_device *netdev)
607{
608 struct gelic_card *card = netdev_card(netdev);
609 struct netdev_hw_addr *ha;
610 unsigned int i;
611 uint8_t *p;
612 u64 addr;
613 int status;
614
615
616 status = lv1_net_remove_multicast_address(bus_id(card), dev_id(card),
617 0, 1);
618 if (status)
619 dev_err(ctodev(card),
620 "lv1_net_remove_multicast_address failed %d\n",
621 status);
622
623 status = lv1_net_add_multicast_address(bus_id(card), dev_id(card),
624 GELIC_NET_BROADCAST_ADDR, 0);
625 if (status)
626 dev_err(ctodev(card),
627 "lv1_net_add_multicast_address failed, %d\n",
628 status);
629
630 if ((netdev->flags & IFF_ALLMULTI) ||
631 (netdev_mc_count(netdev) > GELIC_NET_MC_COUNT_MAX)) {
632 status = lv1_net_add_multicast_address(bus_id(card),
633 dev_id(card),
634 0, 1);
635 if (status)
636 dev_err(ctodev(card),
637 "lv1_net_add_multicast_address failed, %d\n",
638 status);
639 return;
640 }
641
642
643 netdev_for_each_mc_addr(ha, netdev) {
644 addr = 0;
645 p = ha->addr;
646 for (i = 0; i < ETH_ALEN; i++) {
647 addr <<= 8;
648 addr |= *p++;
649 }
650 status = lv1_net_add_multicast_address(bus_id(card),
651 dev_id(card),
652 addr, 0);
653 if (status)
654 dev_err(ctodev(card),
655 "lv1_net_add_multicast_address failed, %d\n",
656 status);
657 }
658}
659
660
661
662
663
664
665
666int gelic_net_stop(struct net_device *netdev)
667{
668 struct gelic_card *card;
669
670 pr_debug("%s: start\n", __func__);
671
672 netif_stop_queue(netdev);
673 netif_carrier_off(netdev);
674
675 card = netdev_card(netdev);
676 gelic_card_down(card);
677
678 pr_debug("%s: done\n", __func__);
679 return 0;
680}
681
682
683
684
685
686
687
688static struct gelic_descr *
689gelic_card_get_next_tx_descr(struct gelic_card *card)
690{
691 if (!card->tx_chain.head)
692 return NULL;
693
694 if (card->tx_chain.tail != card->tx_chain.head->next &&
695 gelic_descr_get_status(card->tx_chain.head) ==
696 GELIC_DESCR_DMA_NOT_IN_USE)
697 return card->tx_chain.head;
698 else
699 return NULL;
700
701}
702
703
704
705
706
707
708
709
710
711
712static void gelic_descr_set_tx_cmdstat(struct gelic_descr *descr,
713 struct sk_buff *skb)
714{
715 if (skb->ip_summed != CHECKSUM_PARTIAL)
716 descr->dmac_cmd_status =
717 cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
718 GELIC_DESCR_TX_DMA_FRAME_TAIL);
719 else {
720
721
722 if (skb->protocol == htons(ETH_P_IP)) {
723 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
724 descr->dmac_cmd_status =
725 cpu_to_be32(GELIC_DESCR_DMA_CMD_TCP_CHKSUM |
726 GELIC_DESCR_TX_DMA_FRAME_TAIL);
727
728 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
729 descr->dmac_cmd_status =
730 cpu_to_be32(GELIC_DESCR_DMA_CMD_UDP_CHKSUM |
731 GELIC_DESCR_TX_DMA_FRAME_TAIL);
732 else
733
734
735
736 descr->dmac_cmd_status =
737 cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
738 GELIC_DESCR_TX_DMA_FRAME_TAIL);
739 }
740 }
741}
742
743static struct sk_buff *gelic_put_vlan_tag(struct sk_buff *skb,
744 unsigned short tag)
745{
746 struct vlan_ethhdr *veth;
747 static unsigned int c;
748
749 if (skb_headroom(skb) < VLAN_HLEN) {
750 struct sk_buff *sk_tmp = skb;
751 pr_debug("%s: hd=%d c=%ud\n", __func__, skb_headroom(skb), c);
752 skb = skb_realloc_headroom(sk_tmp, VLAN_HLEN);
753 if (!skb)
754 return NULL;
755 dev_kfree_skb_any(sk_tmp);
756 }
757 veth = skb_push(skb, VLAN_HLEN);
758
759
760 memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN);
761
762 veth->h_vlan_proto = cpu_to_be16(ETH_P_8021Q);
763 veth->h_vlan_TCI = htons(tag);
764
765 return skb;
766}
767
768
769
770
771
772
773
774
775
776
777static int gelic_descr_prepare_tx(struct gelic_card *card,
778 struct gelic_descr *descr,
779 struct sk_buff *skb)
780{
781 dma_addr_t buf;
782
783 if (card->vlan_required) {
784 struct sk_buff *skb_tmp;
785 enum gelic_port_type type;
786
787 type = netdev_port(skb->dev)->type;
788 skb_tmp = gelic_put_vlan_tag(skb,
789 card->vlan[type].tx);
790 if (!skb_tmp)
791 return -ENOMEM;
792 skb = skb_tmp;
793 }
794
795 buf = dma_map_single(ctodev(card), skb->data, skb->len, DMA_TO_DEVICE);
796
797 if (!buf) {
798 dev_err(ctodev(card),
799 "dma map 2 failed (%p, %i). Dropping packet\n",
800 skb->data, skb->len);
801 return -ENOMEM;
802 }
803
804 descr->buf_addr = cpu_to_be32(buf);
805 descr->buf_size = cpu_to_be32(skb->len);
806 descr->skb = skb;
807 descr->data_status = 0;
808 descr->next_descr_addr = 0;
809 gelic_descr_set_tx_cmdstat(descr, skb);
810
811
812 card->tx_chain.head = descr->next;
813 return 0;
814}
815
816
817
818
819
820
821
822static int gelic_card_kick_txdma(struct gelic_card *card,
823 struct gelic_descr *descr)
824{
825 int status = 0;
826
827 if (card->tx_dma_progress)
828 return 0;
829
830 if (gelic_descr_get_status(descr) == GELIC_DESCR_DMA_CARDOWNED) {
831 card->tx_dma_progress = 1;
832 status = lv1_net_start_tx_dma(bus_id(card), dev_id(card),
833 descr->bus_addr, 0);
834 if (status) {
835 card->tx_dma_progress = 0;
836 dev_info(ctodev(card), "lv1_net_start_txdma failed," \
837 "status=%d\n", status);
838 }
839 }
840 return status;
841}
842
843
844
845
846
847
848
849
850int gelic_net_xmit(struct sk_buff *skb, struct net_device *netdev)
851{
852 struct gelic_card *card = netdev_card(netdev);
853 struct gelic_descr *descr;
854 int result;
855 unsigned long flags;
856
857 spin_lock_irqsave(&card->tx_lock, flags);
858
859 gelic_card_release_tx_chain(card, 0);
860
861 descr = gelic_card_get_next_tx_descr(card);
862 if (!descr) {
863
864
865
866 gelic_card_stop_queues(card);
867 spin_unlock_irqrestore(&card->tx_lock, flags);
868 return NETDEV_TX_BUSY;
869 }
870
871 result = gelic_descr_prepare_tx(card, descr, skb);
872 if (result) {
873
874
875
876
877 netdev->stats.tx_dropped++;
878 dev_kfree_skb_any(skb);
879 spin_unlock_irqrestore(&card->tx_lock, flags);
880 return NETDEV_TX_OK;
881 }
882
883
884
885
886 descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
887
888
889
890
891 wmb();
892 if (gelic_card_kick_txdma(card, descr)) {
893
894
895
896
897 netdev->stats.tx_dropped++;
898
899 descr->data_status = cpu_to_be32(GELIC_DESCR_TX_TAIL);
900 gelic_descr_release_tx(card, descr);
901
902 card->tx_chain.head = descr;
903
904 descr->prev->next_descr_addr = 0;
905 dev_info(ctodev(card), "%s: kick failure\n", __func__);
906 }
907
908 spin_unlock_irqrestore(&card->tx_lock, flags);
909 return NETDEV_TX_OK;
910}
911
912
913
914
915
916
917
918
919
920
921static void gelic_net_pass_skb_up(struct gelic_descr *descr,
922 struct gelic_card *card,
923 struct net_device *netdev)
924
925{
926 struct sk_buff *skb = descr->skb;
927 u32 data_status, data_error;
928
929 data_status = be32_to_cpu(descr->data_status);
930 data_error = be32_to_cpu(descr->data_error);
931
932 dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr),
933 GELIC_NET_MAX_MTU,
934 DMA_FROM_DEVICE);
935
936 skb_put(skb, be32_to_cpu(descr->valid_size)?
937 be32_to_cpu(descr->valid_size) :
938 be32_to_cpu(descr->result_size));
939 if (!descr->valid_size)
940 dev_info(ctodev(card), "buffer full %x %x %x\n",
941 be32_to_cpu(descr->result_size),
942 be32_to_cpu(descr->buf_size),
943 be32_to_cpu(descr->dmac_cmd_status));
944
945 descr->skb = NULL;
946
947
948
949
950 skb_pull(skb, 2);
951 skb->protocol = eth_type_trans(skb, netdev);
952
953
954 if (netdev->features & NETIF_F_RXCSUM) {
955 if ((data_status & GELIC_DESCR_DATA_STATUS_CHK_MASK) &&
956 (!(data_error & GELIC_DESCR_DATA_ERROR_CHK_MASK)))
957 skb->ip_summed = CHECKSUM_UNNECESSARY;
958 else
959 skb_checksum_none_assert(skb);
960 } else
961 skb_checksum_none_assert(skb);
962
963
964 netdev->stats.rx_packets++;
965 netdev->stats.rx_bytes += skb->len;
966
967
968 netif_receive_skb(skb);
969}
970
971
972
973
974
975
976
977
978
979
980static int gelic_card_decode_one_descr(struct gelic_card *card)
981{
982 enum gelic_descr_dma_status status;
983 struct gelic_descr_chain *chain = &card->rx_chain;
984 struct gelic_descr *descr = chain->head;
985 struct net_device *netdev = NULL;
986 int dmac_chain_ended;
987
988 status = gelic_descr_get_status(descr);
989
990 if (status == GELIC_DESCR_DMA_CARDOWNED)
991 return 0;
992
993 if (status == GELIC_DESCR_DMA_NOT_IN_USE) {
994 dev_dbg(ctodev(card), "dormant descr? %p\n", descr);
995 return 0;
996 }
997
998
999 if (card->vlan_required) {
1000 unsigned int i;
1001 u16 vid;
1002 vid = *(u16 *)(descr->skb->data) & VLAN_VID_MASK;
1003 for (i = 0; i < GELIC_PORT_MAX; i++) {
1004 if (card->vlan[i].rx == vid) {
1005 netdev = card->netdev[i];
1006 break;
1007 }
1008 }
1009 if (GELIC_PORT_MAX <= i) {
1010 pr_info("%s: unknown packet vid=%x\n", __func__, vid);
1011 goto refill;
1012 }
1013 } else
1014 netdev = card->netdev[GELIC_PORT_ETHERNET_0];
1015
1016 if ((status == GELIC_DESCR_DMA_RESPONSE_ERROR) ||
1017 (status == GELIC_DESCR_DMA_PROTECTION_ERROR) ||
1018 (status == GELIC_DESCR_DMA_FORCE_END)) {
1019 dev_info(ctodev(card), "dropping RX descriptor with state %x\n",
1020 status);
1021 netdev->stats.rx_dropped++;
1022 goto refill;
1023 }
1024
1025 if (status == GELIC_DESCR_DMA_BUFFER_FULL) {
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035 dev_info(ctodev(card), "overlength frame\n");
1036 goto refill;
1037 }
1038
1039
1040
1041
1042 if (status != GELIC_DESCR_DMA_FRAME_END) {
1043 dev_dbg(ctodev(card), "RX descriptor with state %x\n",
1044 status);
1045 goto refill;
1046 }
1047
1048
1049 gelic_net_pass_skb_up(descr, card, netdev);
1050refill:
1051
1052
1053 dmac_chain_ended =
1054 be32_to_cpu(descr->dmac_cmd_status) &
1055 GELIC_DESCR_RX_DMA_CHAIN_END;
1056
1057
1058
1059
1060
1061 descr->next_descr_addr = 0;
1062
1063
1064 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
1065
1066
1067
1068
1069
1070 gelic_descr_prepare_rx(card, descr);
1071
1072 chain->tail = descr;
1073 chain->head = descr->next;
1074
1075
1076
1077
1078 descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
1079
1080
1081
1082
1083
1084
1085 if (dmac_chain_ended)
1086 gelic_card_enable_rxdmac(card);
1087
1088 return 1;
1089}
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099static int gelic_net_poll(struct napi_struct *napi, int budget)
1100{
1101 struct gelic_card *card = container_of(napi, struct gelic_card, napi);
1102 int packets_done = 0;
1103
1104 while (packets_done < budget) {
1105 if (!gelic_card_decode_one_descr(card))
1106 break;
1107
1108 packets_done++;
1109 }
1110
1111 if (packets_done < budget) {
1112 napi_complete_done(napi, packets_done);
1113 gelic_card_rx_irq_on(card);
1114 }
1115 return packets_done;
1116}
1117
1118
1119
1120
1121static irqreturn_t gelic_card_interrupt(int irq, void *ptr)
1122{
1123 unsigned long flags;
1124 struct gelic_card *card = ptr;
1125 u64 status;
1126
1127 status = card->irq_status;
1128
1129 if (!status)
1130 return IRQ_NONE;
1131
1132 status &= card->irq_mask;
1133
1134 if (status & GELIC_CARD_RXINT) {
1135 gelic_card_rx_irq_off(card);
1136 napi_schedule(&card->napi);
1137 }
1138
1139 if (status & GELIC_CARD_TXINT) {
1140 spin_lock_irqsave(&card->tx_lock, flags);
1141 card->tx_dma_progress = 0;
1142 gelic_card_release_tx_chain(card, 0);
1143
1144 gelic_card_kick_txdma(card, card->tx_chain.tail);
1145 spin_unlock_irqrestore(&card->tx_lock, flags);
1146 }
1147
1148
1149 if (status & GELIC_CARD_PORT_STATUS_CHANGED)
1150 gelic_card_get_ether_port_status(card, 1);
1151
1152#ifdef CONFIG_GELIC_WIRELESS
1153 if (status & (GELIC_CARD_WLAN_EVENT_RECEIVED |
1154 GELIC_CARD_WLAN_COMMAND_COMPLETED))
1155 gelic_wl_interrupt(card->netdev[GELIC_PORT_WIRELESS], status);
1156#endif
1157
1158 return IRQ_HANDLED;
1159}
1160
1161#ifdef CONFIG_NET_POLL_CONTROLLER
1162
1163
1164
1165
1166
1167
1168void gelic_net_poll_controller(struct net_device *netdev)
1169{
1170 struct gelic_card *card = netdev_card(netdev);
1171
1172 gelic_card_set_irq_mask(card, 0);
1173 gelic_card_interrupt(netdev->irq, netdev);
1174 gelic_card_set_irq_mask(card, card->irq_mask);
1175}
1176#endif
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187int gelic_net_open(struct net_device *netdev)
1188{
1189 struct gelic_card *card = netdev_card(netdev);
1190
1191 dev_dbg(ctodev(card), " -> %s %p\n", __func__, netdev);
1192
1193 gelic_card_up(card);
1194
1195 netif_start_queue(netdev);
1196 gelic_card_get_ether_port_status(card, 1);
1197
1198 dev_dbg(ctodev(card), " <- %s\n", __func__);
1199 return 0;
1200}
1201
1202void gelic_net_get_drvinfo(struct net_device *netdev,
1203 struct ethtool_drvinfo *info)
1204{
1205 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
1206 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
1207}
1208
1209static int gelic_ether_get_link_ksettings(struct net_device *netdev,
1210 struct ethtool_link_ksettings *cmd)
1211{
1212 struct gelic_card *card = netdev_card(netdev);
1213 u32 supported, advertising;
1214
1215 gelic_card_get_ether_port_status(card, 0);
1216
1217 if (card->ether_port_status & GELIC_LV1_ETHER_FULL_DUPLEX)
1218 cmd->base.duplex = DUPLEX_FULL;
1219 else
1220 cmd->base.duplex = DUPLEX_HALF;
1221
1222 switch (card->ether_port_status & GELIC_LV1_ETHER_SPEED_MASK) {
1223 case GELIC_LV1_ETHER_SPEED_10:
1224 cmd->base.speed = SPEED_10;
1225 break;
1226 case GELIC_LV1_ETHER_SPEED_100:
1227 cmd->base.speed = SPEED_100;
1228 break;
1229 case GELIC_LV1_ETHER_SPEED_1000:
1230 cmd->base.speed = SPEED_1000;
1231 break;
1232 default:
1233 pr_info("%s: speed unknown\n", __func__);
1234 cmd->base.speed = SPEED_10;
1235 break;
1236 }
1237
1238 supported = SUPPORTED_TP | SUPPORTED_Autoneg |
1239 SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
1240 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
1241 SUPPORTED_1000baseT_Full;
1242 advertising = supported;
1243 if (card->link_mode & GELIC_LV1_ETHER_AUTO_NEG) {
1244 cmd->base.autoneg = AUTONEG_ENABLE;
1245 } else {
1246 cmd->base.autoneg = AUTONEG_DISABLE;
1247 advertising &= ~ADVERTISED_Autoneg;
1248 }
1249 cmd->base.port = PORT_TP;
1250
1251 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1252 supported);
1253 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1254 advertising);
1255
1256 return 0;
1257}
1258
1259static int
1260gelic_ether_set_link_ksettings(struct net_device *netdev,
1261 const struct ethtool_link_ksettings *cmd)
1262{
1263 struct gelic_card *card = netdev_card(netdev);
1264 u64 mode;
1265 int ret;
1266
1267 if (cmd->base.autoneg == AUTONEG_ENABLE) {
1268 mode = GELIC_LV1_ETHER_AUTO_NEG;
1269 } else {
1270 switch (cmd->base.speed) {
1271 case SPEED_10:
1272 mode = GELIC_LV1_ETHER_SPEED_10;
1273 break;
1274 case SPEED_100:
1275 mode = GELIC_LV1_ETHER_SPEED_100;
1276 break;
1277 case SPEED_1000:
1278 mode = GELIC_LV1_ETHER_SPEED_1000;
1279 break;
1280 default:
1281 return -EINVAL;
1282 }
1283 if (cmd->base.duplex == DUPLEX_FULL) {
1284 mode |= GELIC_LV1_ETHER_FULL_DUPLEX;
1285 } else if (cmd->base.speed == SPEED_1000) {
1286 pr_info("1000 half duplex is not supported.\n");
1287 return -EINVAL;
1288 }
1289 }
1290
1291 ret = gelic_card_set_link_mode(card, mode);
1292
1293 if (ret)
1294 return ret;
1295
1296 return 0;
1297}
1298
1299static void gelic_net_get_wol(struct net_device *netdev,
1300 struct ethtool_wolinfo *wol)
1301{
1302 if (0 <= ps3_compare_firmware_version(2, 2, 0))
1303 wol->supported = WAKE_MAGIC;
1304 else
1305 wol->supported = 0;
1306
1307 wol->wolopts = ps3_sys_manager_get_wol() ? wol->supported : 0;
1308 memset(&wol->sopass, 0, sizeof(wol->sopass));
1309}
1310static int gelic_net_set_wol(struct net_device *netdev,
1311 struct ethtool_wolinfo *wol)
1312{
1313 int status;
1314 struct gelic_card *card;
1315 u64 v1, v2;
1316
1317 if (ps3_compare_firmware_version(2, 2, 0) < 0 ||
1318 !capable(CAP_NET_ADMIN))
1319 return -EPERM;
1320
1321 if (wol->wolopts & ~WAKE_MAGIC)
1322 return -EINVAL;
1323
1324 card = netdev_card(netdev);
1325 if (wol->wolopts & WAKE_MAGIC) {
1326 status = lv1_net_control(bus_id(card), dev_id(card),
1327 GELIC_LV1_SET_WOL,
1328 GELIC_LV1_WOL_MAGIC_PACKET,
1329 0, GELIC_LV1_WOL_MP_ENABLE,
1330 &v1, &v2);
1331 if (status) {
1332 pr_info("%s: enabling WOL failed %d\n", __func__,
1333 status);
1334 status = -EIO;
1335 goto done;
1336 }
1337 status = lv1_net_control(bus_id(card), dev_id(card),
1338 GELIC_LV1_SET_WOL,
1339 GELIC_LV1_WOL_ADD_MATCH_ADDR,
1340 0, GELIC_LV1_WOL_MATCH_ALL,
1341 &v1, &v2);
1342 if (!status)
1343 ps3_sys_manager_set_wol(1);
1344 else {
1345 pr_info("%s: enabling WOL filter failed %d\n",
1346 __func__, status);
1347 status = -EIO;
1348 }
1349 } else {
1350 status = lv1_net_control(bus_id(card), dev_id(card),
1351 GELIC_LV1_SET_WOL,
1352 GELIC_LV1_WOL_MAGIC_PACKET,
1353 0, GELIC_LV1_WOL_MP_DISABLE,
1354 &v1, &v2);
1355 if (status) {
1356 pr_info("%s: disabling WOL failed %d\n", __func__,
1357 status);
1358 status = -EIO;
1359 goto done;
1360 }
1361 status = lv1_net_control(bus_id(card), dev_id(card),
1362 GELIC_LV1_SET_WOL,
1363 GELIC_LV1_WOL_DELETE_MATCH_ADDR,
1364 0, GELIC_LV1_WOL_MATCH_ALL,
1365 &v1, &v2);
1366 if (!status)
1367 ps3_sys_manager_set_wol(0);
1368 else {
1369 pr_info("%s: removing WOL filter failed %d\n",
1370 __func__, status);
1371 status = -EIO;
1372 }
1373 }
1374done:
1375 return status;
1376}
1377
1378static const struct ethtool_ops gelic_ether_ethtool_ops = {
1379 .get_drvinfo = gelic_net_get_drvinfo,
1380 .get_link = ethtool_op_get_link,
1381 .get_wol = gelic_net_get_wol,
1382 .set_wol = gelic_net_set_wol,
1383 .get_link_ksettings = gelic_ether_get_link_ksettings,
1384 .set_link_ksettings = gelic_ether_set_link_ksettings,
1385};
1386
1387
1388
1389
1390
1391
1392
1393
1394static void gelic_net_tx_timeout_task(struct work_struct *work)
1395{
1396 struct gelic_card *card =
1397 container_of(work, struct gelic_card, tx_timeout_task);
1398 struct net_device *netdev = card->netdev[GELIC_PORT_ETHERNET_0];
1399
1400 dev_info(ctodev(card), "%s:Timed out. Restarting...\n", __func__);
1401
1402 if (!(netdev->flags & IFF_UP))
1403 goto out;
1404
1405 netif_device_detach(netdev);
1406 gelic_net_stop(netdev);
1407
1408 gelic_net_open(netdev);
1409 netif_device_attach(netdev);
1410
1411out:
1412 atomic_dec(&card->tx_timeout_task_counter);
1413}
1414
1415
1416
1417
1418
1419
1420
1421void gelic_net_tx_timeout(struct net_device *netdev, unsigned int txqueue)
1422{
1423 struct gelic_card *card;
1424
1425 card = netdev_card(netdev);
1426 atomic_inc(&card->tx_timeout_task_counter);
1427 if (netdev->flags & IFF_UP)
1428 schedule_work(&card->tx_timeout_task);
1429 else
1430 atomic_dec(&card->tx_timeout_task_counter);
1431}
1432
1433static const struct net_device_ops gelic_netdevice_ops = {
1434 .ndo_open = gelic_net_open,
1435 .ndo_stop = gelic_net_stop,
1436 .ndo_start_xmit = gelic_net_xmit,
1437 .ndo_set_rx_mode = gelic_net_set_multi,
1438 .ndo_tx_timeout = gelic_net_tx_timeout,
1439 .ndo_set_mac_address = eth_mac_addr,
1440 .ndo_validate_addr = eth_validate_addr,
1441#ifdef CONFIG_NET_POLL_CONTROLLER
1442 .ndo_poll_controller = gelic_net_poll_controller,
1443#endif
1444};
1445
1446
1447
1448
1449
1450
1451
1452static void gelic_ether_setup_netdev_ops(struct net_device *netdev,
1453 struct napi_struct *napi)
1454{
1455 netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;
1456
1457 netif_napi_add(netdev, napi, gelic_net_poll, NAPI_POLL_WEIGHT);
1458 netdev->ethtool_ops = &gelic_ether_ethtool_ops;
1459 netdev->netdev_ops = &gelic_netdevice_ops;
1460}
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472int gelic_net_setup_netdev(struct net_device *netdev, struct gelic_card *card)
1473{
1474 int status;
1475 u64 v1, v2;
1476
1477 netdev->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1478
1479 netdev->features = NETIF_F_IP_CSUM;
1480 if (GELIC_CARD_RX_CSUM_DEFAULT)
1481 netdev->features |= NETIF_F_RXCSUM;
1482
1483 status = lv1_net_control(bus_id(card), dev_id(card),
1484 GELIC_LV1_GET_MAC_ADDRESS,
1485 0, 0, 0, &v1, &v2);
1486 v1 <<= 16;
1487 if (status || !is_valid_ether_addr((u8 *)&v1)) {
1488 dev_info(ctodev(card),
1489 "%s:lv1_net_control GET_MAC_ADDR failed %d\n",
1490 __func__, status);
1491 return -EINVAL;
1492 }
1493 memcpy(netdev->dev_addr, &v1, ETH_ALEN);
1494
1495 if (card->vlan_required) {
1496 netdev->hard_header_len += VLAN_HLEN;
1497
1498
1499
1500
1501 netdev->features |= NETIF_F_VLAN_CHALLENGED;
1502 }
1503
1504
1505 netdev->min_mtu = GELIC_NET_MIN_MTU;
1506 netdev->max_mtu = GELIC_NET_MAX_MTU;
1507
1508 status = register_netdev(netdev);
1509 if (status) {
1510 dev_err(ctodev(card), "%s:Couldn't register %s %d\n",
1511 __func__, netdev->name, status);
1512 return status;
1513 }
1514 dev_info(ctodev(card), "%s: MAC addr %pM\n",
1515 netdev->name, netdev->dev_addr);
1516
1517 return 0;
1518}
1519
1520
1521
1522
1523
1524
1525
1526
1527#define GELIC_ALIGN (32)
1528static struct gelic_card *gelic_alloc_card_net(struct net_device **netdev)
1529{
1530 struct gelic_card *card;
1531 struct gelic_port *port;
1532 void *p;
1533 size_t alloc_size;
1534
1535
1536
1537
1538 BUILD_BUG_ON(offsetof(struct gelic_card, irq_status) % 8);
1539 BUILD_BUG_ON(offsetof(struct gelic_card, descr) % 32);
1540 alloc_size =
1541 sizeof(struct gelic_card) +
1542 sizeof(struct gelic_descr) * GELIC_NET_RX_DESCRIPTORS +
1543 sizeof(struct gelic_descr) * GELIC_NET_TX_DESCRIPTORS +
1544 GELIC_ALIGN - 1;
1545
1546 p = kzalloc(alloc_size, GFP_KERNEL);
1547 if (!p)
1548 return NULL;
1549 card = PTR_ALIGN(p, GELIC_ALIGN);
1550 card->unalign = p;
1551
1552
1553
1554
1555 *netdev = alloc_etherdev(sizeof(struct gelic_port));
1556 if (!*netdev) {
1557 kfree(card->unalign);
1558 return NULL;
1559 }
1560 port = netdev_priv(*netdev);
1561
1562
1563 port->netdev = *netdev;
1564 port->card = card;
1565 port->type = GELIC_PORT_ETHERNET_0;
1566
1567
1568 card->netdev[GELIC_PORT_ETHERNET_0] = *netdev;
1569
1570 INIT_WORK(&card->tx_timeout_task, gelic_net_tx_timeout_task);
1571 init_waitqueue_head(&card->waitq);
1572 atomic_set(&card->tx_timeout_task_counter, 0);
1573 mutex_init(&card->updown_lock);
1574 atomic_set(&card->users, 0);
1575
1576 return card;
1577}
1578
1579static void gelic_card_get_vlan_info(struct gelic_card *card)
1580{
1581 u64 v1, v2;
1582 int status;
1583 unsigned int i;
1584 struct {
1585 int tx;
1586 int rx;
1587 } vlan_id_ix[2] = {
1588 [GELIC_PORT_ETHERNET_0] = {
1589 .tx = GELIC_LV1_VLAN_TX_ETHERNET_0,
1590 .rx = GELIC_LV1_VLAN_RX_ETHERNET_0
1591 },
1592 [GELIC_PORT_WIRELESS] = {
1593 .tx = GELIC_LV1_VLAN_TX_WIRELESS,
1594 .rx = GELIC_LV1_VLAN_RX_WIRELESS
1595 }
1596 };
1597
1598 for (i = 0; i < ARRAY_SIZE(vlan_id_ix); i++) {
1599
1600 status = lv1_net_control(bus_id(card), dev_id(card),
1601 GELIC_LV1_GET_VLAN_ID,
1602 vlan_id_ix[i].tx,
1603 0, 0, &v1, &v2);
1604 if (status || !v1) {
1605 if (status != LV1_NO_ENTRY)
1606 dev_dbg(ctodev(card),
1607 "get vlan id for tx(%d) failed(%d)\n",
1608 vlan_id_ix[i].tx, status);
1609 card->vlan[i].tx = 0;
1610 card->vlan[i].rx = 0;
1611 continue;
1612 }
1613 card->vlan[i].tx = (u16)v1;
1614
1615
1616 status = lv1_net_control(bus_id(card), dev_id(card),
1617 GELIC_LV1_GET_VLAN_ID,
1618 vlan_id_ix[i].rx,
1619 0, 0, &v1, &v2);
1620 if (status || !v1) {
1621 if (status != LV1_NO_ENTRY)
1622 dev_info(ctodev(card),
1623 "get vlan id for rx(%d) failed(%d)\n",
1624 vlan_id_ix[i].rx, status);
1625 card->vlan[i].tx = 0;
1626 card->vlan[i].rx = 0;
1627 continue;
1628 }
1629 card->vlan[i].rx = (u16)v1;
1630
1631 dev_dbg(ctodev(card), "vlan_id[%d] tx=%02x rx=%02x\n",
1632 i, card->vlan[i].tx, card->vlan[i].rx);
1633 }
1634
1635 if (card->vlan[GELIC_PORT_ETHERNET_0].tx) {
1636 BUG_ON(!card->vlan[GELIC_PORT_WIRELESS].tx);
1637 card->vlan_required = 1;
1638 } else
1639 card->vlan_required = 0;
1640
1641
1642 if (ps3_compare_firmware_version(1, 6, 0) < 0) {
1643 card->vlan[GELIC_PORT_WIRELESS].tx = 0;
1644 card->vlan[GELIC_PORT_WIRELESS].rx = 0;
1645 }
1646
1647 dev_info(ctodev(card), "internal vlan %s\n",
1648 card->vlan_required? "enabled" : "disabled");
1649}
1650
1651
1652
1653static int ps3_gelic_driver_probe(struct ps3_system_bus_device *dev)
1654{
1655 struct gelic_card *card;
1656 struct net_device *netdev;
1657 int result;
1658
1659 pr_debug("%s: called\n", __func__);
1660
1661 udbg_shutdown_ps3gelic();
1662
1663 result = ps3_open_hv_device(dev);
1664
1665 if (result) {
1666 dev_dbg(&dev->core, "%s:ps3_open_hv_device failed\n",
1667 __func__);
1668 goto fail_open;
1669 }
1670
1671 result = ps3_dma_region_create(dev->d_region);
1672
1673 if (result) {
1674 dev_dbg(&dev->core, "%s:ps3_dma_region_create failed(%d)\n",
1675 __func__, result);
1676 BUG_ON("check region type");
1677 goto fail_dma_region;
1678 }
1679
1680
1681 card = gelic_alloc_card_net(&netdev);
1682 if (!card) {
1683 dev_info(&dev->core, "%s:gelic_net_alloc_card failed\n",
1684 __func__);
1685 result = -ENOMEM;
1686 goto fail_alloc_card;
1687 }
1688 ps3_system_bus_set_drvdata(dev, card);
1689 card->dev = dev;
1690
1691
1692 gelic_card_get_vlan_info(card);
1693
1694 card->link_mode = GELIC_LV1_ETHER_AUTO_NEG;
1695
1696
1697 result = lv1_net_set_interrupt_status_indicator(bus_id(card),
1698 dev_id(card),
1699 ps3_mm_phys_to_lpar(__pa(&card->irq_status)),
1700 0);
1701
1702 if (result) {
1703 dev_dbg(&dev->core,
1704 "%s:set_interrupt_status_indicator failed: %s\n",
1705 __func__, ps3_result(result));
1706 result = -EIO;
1707 goto fail_status_indicator;
1708 }
1709
1710 result = ps3_sb_event_receive_port_setup(dev, PS3_BINDING_CPU_ANY,
1711 &card->irq);
1712
1713 if (result) {
1714 dev_info(ctodev(card),
1715 "%s:gelic_net_open_device failed (%d)\n",
1716 __func__, result);
1717 result = -EPERM;
1718 goto fail_alloc_irq;
1719 }
1720 result = request_irq(card->irq, gelic_card_interrupt,
1721 0, netdev->name, card);
1722
1723 if (result) {
1724 dev_info(ctodev(card), "%s:request_irq failed (%d)\n",
1725 __func__, result);
1726 goto fail_request_irq;
1727 }
1728
1729
1730 card->irq_mask = GELIC_CARD_RXINT | GELIC_CARD_TXINT |
1731 GELIC_CARD_PORT_STATUS_CHANGED;
1732
1733
1734 result = gelic_card_init_chain(card, &card->tx_chain,
1735 card->descr, GELIC_NET_TX_DESCRIPTORS);
1736 if (result)
1737 goto fail_alloc_tx;
1738 result = gelic_card_init_chain(card, &card->rx_chain,
1739 card->descr + GELIC_NET_TX_DESCRIPTORS,
1740 GELIC_NET_RX_DESCRIPTORS);
1741 if (result)
1742 goto fail_alloc_rx;
1743
1744
1745 card->tx_top = card->tx_chain.head;
1746 card->rx_top = card->rx_chain.head;
1747 dev_dbg(ctodev(card), "descr rx %p, tx %p, size %#lx, num %#x\n",
1748 card->rx_top, card->tx_top, sizeof(struct gelic_descr),
1749 GELIC_NET_RX_DESCRIPTORS);
1750
1751 result = gelic_card_alloc_rx_skbs(card);
1752 if (result)
1753 goto fail_alloc_skbs;
1754
1755 spin_lock_init(&card->tx_lock);
1756 card->tx_dma_progress = 0;
1757
1758
1759 netdev->irq = card->irq;
1760 SET_NETDEV_DEV(netdev, &card->dev->core);
1761 gelic_ether_setup_netdev_ops(netdev, &card->napi);
1762 result = gelic_net_setup_netdev(netdev, card);
1763 if (result) {
1764 dev_dbg(&dev->core, "%s: setup_netdev failed %d\n",
1765 __func__, result);
1766 goto fail_setup_netdev;
1767 }
1768
1769#ifdef CONFIG_GELIC_WIRELESS
1770 result = gelic_wl_driver_probe(card);
1771 if (result) {
1772 dev_dbg(&dev->core, "%s: WL init failed\n", __func__);
1773 goto fail_setup_netdev;
1774 }
1775#endif
1776 pr_debug("%s: done\n", __func__);
1777 return 0;
1778
1779fail_setup_netdev:
1780fail_alloc_skbs:
1781 gelic_card_free_chain(card, card->rx_chain.head);
1782fail_alloc_rx:
1783 gelic_card_free_chain(card, card->tx_chain.head);
1784fail_alloc_tx:
1785 free_irq(card->irq, card);
1786 netdev->irq = 0;
1787fail_request_irq:
1788 ps3_sb_event_receive_port_destroy(dev, card->irq);
1789fail_alloc_irq:
1790 lv1_net_set_interrupt_status_indicator(bus_id(card),
1791 bus_id(card),
1792 0, 0);
1793fail_status_indicator:
1794 ps3_system_bus_set_drvdata(dev, NULL);
1795 kfree(netdev_card(netdev)->unalign);
1796 free_netdev(netdev);
1797fail_alloc_card:
1798 ps3_dma_region_free(dev->d_region);
1799fail_dma_region:
1800 ps3_close_hv_device(dev);
1801fail_open:
1802 return result;
1803}
1804
1805
1806
1807
1808
1809static int ps3_gelic_driver_remove(struct ps3_system_bus_device *dev)
1810{
1811 struct gelic_card *card = ps3_system_bus_get_drvdata(dev);
1812 struct net_device *netdev0;
1813 pr_debug("%s: called\n", __func__);
1814
1815
1816 gelic_card_set_link_mode(card, GELIC_LV1_ETHER_AUTO_NEG);
1817
1818#ifdef CONFIG_GELIC_WIRELESS
1819 gelic_wl_driver_remove(card);
1820#endif
1821
1822 gelic_card_set_irq_mask(card, 0);
1823
1824
1825 gelic_card_disable_rxdmac(card);
1826 gelic_card_disable_txdmac(card);
1827
1828
1829 gelic_card_release_tx_chain(card, 1);
1830 gelic_card_release_rx_chain(card);
1831
1832 gelic_card_free_chain(card, card->tx_top);
1833 gelic_card_free_chain(card, card->rx_top);
1834
1835 netdev0 = card->netdev[GELIC_PORT_ETHERNET_0];
1836
1837 free_irq(card->irq, card);
1838 netdev0->irq = 0;
1839 ps3_sb_event_receive_port_destroy(card->dev, card->irq);
1840
1841 wait_event(card->waitq,
1842 atomic_read(&card->tx_timeout_task_counter) == 0);
1843
1844 lv1_net_set_interrupt_status_indicator(bus_id(card), dev_id(card),
1845 0 , 0);
1846
1847 unregister_netdev(netdev0);
1848 kfree(netdev_card(netdev0)->unalign);
1849 free_netdev(netdev0);
1850
1851 ps3_system_bus_set_drvdata(dev, NULL);
1852
1853 ps3_dma_region_free(dev->d_region);
1854
1855 ps3_close_hv_device(dev);
1856
1857 pr_debug("%s: done\n", __func__);
1858 return 0;
1859}
1860
1861static struct ps3_system_bus_driver ps3_gelic_driver = {
1862 .match_id = PS3_MATCH_ID_GELIC,
1863 .probe = ps3_gelic_driver_probe,
1864 .remove = ps3_gelic_driver_remove,
1865 .shutdown = ps3_gelic_driver_remove,
1866 .core.name = "ps3_gelic_driver",
1867 .core.owner = THIS_MODULE,
1868};
1869
1870static int __init ps3_gelic_driver_init (void)
1871{
1872 return firmware_has_feature(FW_FEATURE_PS3_LV1)
1873 ? ps3_system_bus_driver_register(&ps3_gelic_driver)
1874 : -ENODEV;
1875}
1876
1877static void __exit ps3_gelic_driver_exit (void)
1878{
1879 ps3_system_bus_driver_unregister(&ps3_gelic_driver);
1880}
1881
1882module_init(ps3_gelic_driver_init);
1883module_exit(ps3_gelic_driver_exit);
1884
1885MODULE_ALIAS(PS3_MODULE_ALIAS_GELIC);
1886
1887