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