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