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