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