net: remove interrupt.h inclusion from netdevice.h
[linux-2.6-block.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
a6b7a407 31#include <linux/interrupt.h>
02c18891
MM
32#include <linux/kernel.h>
33#include <linux/module.h>
5a0e3ad6 34#include <linux/slab.h>
02c18891
MM
35
36#include <linux/etherdevice.h>
37#include <linux/ethtool.h>
38#include <linux/if_vlan.h>
39
40#include <linux/in.h>
41#include <linux/ip.h>
42#include <linux/tcp.h>
43
44#include <linux/dma-mapping.h>
45#include <net/checksum.h>
46#include <asm/firmware.h>
47#include <asm/ps3.h>
48#include <asm/lv1call.h>
49
50#include "ps3_gelic_net.h"
09dde54c 51#include "ps3_gelic_wireless.h"
02c18891
MM
52
53#define DRV_NAME "Gelic Network Driver"
09dde54c 54#define DRV_VERSION "2.0"
02c18891
MM
55
56MODULE_AUTHOR("SCE Inc.");
57MODULE_DESCRIPTION("Gelic Network driver");
58MODULE_LICENSE("GPL");
59
589866f9
MM
60
61static inline void gelic_card_enable_rxdmac(struct gelic_card *card);
62static inline void gelic_card_disable_rxdmac(struct gelic_card *card);
63static inline void gelic_card_disable_txdmac(struct gelic_card *card);
64static inline void gelic_card_reset_chain(struct gelic_card *card,
65 struct gelic_descr_chain *chain,
66 struct gelic_descr *start_descr);
02c18891
MM
67
68/* set irq_mask */
589866f9 69int gelic_card_set_irq_mask(struct gelic_card *card, u64 mask)
02c18891
MM
70{
71 int status;
72
73 status = lv1_net_set_interrupt_mask(bus_id(card), dev_id(card),
74 mask, 0);
75 if (status)
76 dev_info(ctodev(card),
589866f9 77 "%s failed %d\n", __func__, status);
02c18891
MM
78 return status;
79}
589866f9 80
59e97327 81static inline void gelic_card_rx_irq_on(struct gelic_card *card)
02c18891 82{
589866f9
MM
83 card->irq_mask |= GELIC_CARD_RXINT;
84 gelic_card_set_irq_mask(card, card->irq_mask);
02c18891 85}
59e97327 86static inline void gelic_card_rx_irq_off(struct gelic_card *card)
02c18891 87{
589866f9
MM
88 card->irq_mask &= ~GELIC_CARD_RXINT;
89 gelic_card_set_irq_mask(card, card->irq_mask);
02c18891 90}
01fed4c2 91
589866f9
MM
92static void gelic_card_get_ether_port_status(struct gelic_card *card,
93 int inform)
01fed4c2
MM
94{
95 u64 v2;
96 struct net_device *ether_netdev;
97
98 lv1_net_control(bus_id(card), dev_id(card),
99 GELIC_LV1_GET_ETH_PORT_STATUS,
d4d7f1f9 100 GELIC_LV1_VLAN_TX_ETHERNET_0, 0, 0,
01fed4c2
MM
101 &card->ether_port_status, &v2);
102
103 if (inform) {
d4d7f1f9 104 ether_netdev = card->netdev[GELIC_PORT_ETHERNET_0];
01fed4c2
MM
105 if (card->ether_port_status & GELIC_LV1_ETHER_LINK_UP)
106 netif_carrier_on(ether_netdev);
107 else
108 netif_carrier_off(ether_netdev);
109 }
110}
111
55873ed8
HS
112static int gelic_card_set_link_mode(struct gelic_card *card, int mode)
113{
114 int status;
115 u64 v1, v2;
116
117 status = lv1_net_control(bus_id(card), dev_id(card),
118 GELIC_LV1_SET_NEGOTIATION_MODE,
119 GELIC_LV1_PHY_ETHERNET_0, mode, 0, &v1, &v2);
120 if (status) {
121 pr_info("%s: failed setting negotiation mode %d\n", __func__,
122 status);
123 return -EBUSY;
124 }
125
126 card->link_mode = mode;
127 return 0;
128}
129
589866f9
MM
130void gelic_card_up(struct gelic_card *card)
131{
132 pr_debug("%s: called\n", __func__);
2914f3ef 133 mutex_lock(&card->updown_lock);
589866f9
MM
134 if (atomic_inc_return(&card->users) == 1) {
135 pr_debug("%s: real do\n", __func__);
136 /* enable irq */
137 gelic_card_set_irq_mask(card, card->irq_mask);
138 /* start rx */
139 gelic_card_enable_rxdmac(card);
140
141 napi_enable(&card->napi);
142 }
2914f3ef 143 mutex_unlock(&card->updown_lock);
589866f9
MM
144 pr_debug("%s: done\n", __func__);
145}
146
147void gelic_card_down(struct gelic_card *card)
148{
149 u64 mask;
150 pr_debug("%s: called\n", __func__);
2914f3ef 151 mutex_lock(&card->updown_lock);
589866f9
MM
152 if (atomic_dec_if_positive(&card->users) == 0) {
153 pr_debug("%s: real do\n", __func__);
154 napi_disable(&card->napi);
155 /*
156 * Disable irq. Wireless interrupts will
157 * be disabled later if any
158 */
159 mask = card->irq_mask & (GELIC_CARD_WLAN_EVENT_RECEIVED |
160 GELIC_CARD_WLAN_COMMAND_COMPLETED);
161 gelic_card_set_irq_mask(card, mask);
162 /* stop rx */
163 gelic_card_disable_rxdmac(card);
164 gelic_card_reset_chain(card, &card->rx_chain,
165 card->descr + GELIC_NET_TX_DESCRIPTORS);
166 /* stop tx */
167 gelic_card_disable_txdmac(card);
168 }
2914f3ef 169 mutex_unlock(&card->updown_lock);
589866f9
MM
170 pr_debug("%s: done\n", __func__);
171}
01fed4c2 172
02c18891 173/**
59e97327 174 * gelic_descr_get_status -- returns the status of a descriptor
02c18891
MM
175 * @descr: descriptor to look at
176 *
177 * returns the status as in the dmac_cmd_status field of the descriptor
178 */
59e97327
MM
179static enum gelic_descr_dma_status
180gelic_descr_get_status(struct gelic_descr *descr)
02c18891 181{
59e97327 182 return be32_to_cpu(descr->dmac_cmd_status) & GELIC_DESCR_DMA_STAT_MASK;
02c18891
MM
183}
184
185/**
59e97327 186 * gelic_descr_set_status -- sets the status of a descriptor
02c18891
MM
187 * @descr: descriptor to change
188 * @status: status to set in the descriptor
189 *
190 * changes the status to the specified value. Doesn't change other bits
191 * in the status
192 */
59e97327
MM
193static void gelic_descr_set_status(struct gelic_descr *descr,
194 enum gelic_descr_dma_status status)
02c18891 195{
59e97327 196 descr->dmac_cmd_status = cpu_to_be32(status |
589866f9
MM
197 (be32_to_cpu(descr->dmac_cmd_status) &
198 ~GELIC_DESCR_DMA_STAT_MASK));
02c18891
MM
199 /*
200 * dma_cmd_status field is used to indicate whether the descriptor
201 * is valid or not.
202 * Usually caller of this function wants to inform that to the
203 * hardware, so we assure here the hardware sees the change.
204 */
205 wmb();
206}
207
208/**
59e97327 209 * gelic_card_free_chain - free descriptor chain
02c18891
MM
210 * @card: card structure
211 * @descr_in: address of desc
212 */
59e97327
MM
213static void gelic_card_free_chain(struct gelic_card *card,
214 struct gelic_descr *descr_in)
02c18891 215{
59e97327 216 struct gelic_descr *descr;
02c18891
MM
217
218 for (descr = descr_in; descr && descr->bus_addr; descr = descr->next) {
219 dma_unmap_single(ctodev(card), descr->bus_addr,
59e97327 220 GELIC_DESCR_SIZE, DMA_BIDIRECTIONAL);
02c18891
MM
221 descr->bus_addr = 0;
222 }
223}
224
225/**
59e97327 226 * gelic_card_init_chain - links descriptor chain
02c18891
MM
227 * @card: card structure
228 * @chain: address of chain
229 * @start_descr: address of descriptor array
230 * @no: number of descriptors
231 *
232 * we manage a circular list that mirrors the hardware structure,
233 * except that the hardware uses bus addresses.
234 *
235 * returns 0 on success, <0 on failure
236 */
48dce82c
GU
237static int __devinit gelic_card_init_chain(struct gelic_card *card,
238 struct gelic_descr_chain *chain,
239 struct gelic_descr *start_descr,
240 int no)
02c18891
MM
241{
242 int i;
59e97327 243 struct gelic_descr *descr;
02c18891
MM
244
245 descr = start_descr;
246 memset(descr, 0, sizeof(*descr) * no);
247
248 /* set up the hardware pointers in each descriptor */
249 for (i = 0; i < no; i++, descr++) {
59e97327 250 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
02c18891
MM
251 descr->bus_addr =
252 dma_map_single(ctodev(card), descr,
59e97327 253 GELIC_DESCR_SIZE,
02c18891
MM
254 DMA_BIDIRECTIONAL);
255
256 if (!descr->bus_addr)
257 goto iommu_error;
258
259 descr->next = descr + 1;
260 descr->prev = descr - 1;
261 }
262 /* make them as ring */
263 (descr - 1)->next = start_descr;
264 start_descr->prev = (descr - 1);
265
266 /* chain bus addr of hw descriptor */
267 descr = start_descr;
268 for (i = 0; i < no; i++, descr++) {
100e1d89 269 descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
02c18891
MM
270 }
271
272 chain->head = start_descr;
273 chain->tail = start_descr;
274
275 /* do not chain last hw descriptor */
276 (descr - 1)->next_descr_addr = 0;
277
278 return 0;
279
280iommu_error:
281 for (i--, descr--; 0 <= i; i--, descr--)
282 if (descr->bus_addr)
283 dma_unmap_single(ctodev(card), descr->bus_addr,
59e97327 284 GELIC_DESCR_SIZE,
02c18891
MM
285 DMA_BIDIRECTIONAL);
286 return -ENOMEM;
287}
288
589866f9
MM
289/**
290 * gelic_card_reset_chain - reset status of a descriptor chain
291 * @card: card structure
292 * @chain: address of chain
293 * @start_descr: address of descriptor array
294 *
295 * Reset the status of dma descriptors to ready state
296 * and re-initialize the hardware chain for later use
297 */
298static void gelic_card_reset_chain(struct gelic_card *card,
299 struct gelic_descr_chain *chain,
300 struct gelic_descr *start_descr)
301{
302 struct gelic_descr *descr;
303
304 for (descr = start_descr; start_descr != descr->next; descr++) {
305 gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
306 descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
307 }
308
309 chain->head = start_descr;
310 chain->tail = (descr - 1);
311
312 (descr - 1)->next_descr_addr = 0;
313}
02c18891 314/**
59e97327 315 * gelic_descr_prepare_rx - reinitializes a rx descriptor
02c18891
MM
316 * @card: card structure
317 * @descr: descriptor to re-init
318 *
af901ca1 319 * return 0 on success, <0 on failure
02c18891
MM
320 *
321 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
322 * Activate the descriptor state-wise
323 */
59e97327 324static int gelic_descr_prepare_rx(struct gelic_card *card,
589866f9 325 struct gelic_descr *descr)
02c18891
MM
326{
327 int offset;
328 unsigned int bufsize;
329
59e97327 330 if (gelic_descr_get_status(descr) != GELIC_DESCR_DMA_NOT_IN_USE)
583d077d 331 dev_info(ctodev(card), "%s: ERROR status\n", __func__);
02c18891
MM
332 /* we need to round up the buffer size to a multiple of 128 */
333 bufsize = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN);
334
335 /* and we need to have it 128 byte aligned, therefore we allocate a
336 * bit more */
589866f9 337 descr->skb = dev_alloc_skb(bufsize + GELIC_NET_RXBUF_ALIGN - 1);
02c18891
MM
338 if (!descr->skb) {
339 descr->buf_addr = 0; /* tell DMAC don't touch memory */
340 dev_info(ctodev(card),
341 "%s:allocate skb failed !!\n", __func__);
342 return -ENOMEM;
343 }
100e1d89 344 descr->buf_size = cpu_to_be32(bufsize);
02c18891
MM
345 descr->dmac_cmd_status = 0;
346 descr->result_size = 0;
347 descr->valid_size = 0;
348 descr->data_error = 0;
349
350 offset = ((unsigned long)descr->skb->data) &
351 (GELIC_NET_RXBUF_ALIGN - 1);
352 if (offset)
353 skb_reserve(descr->skb, GELIC_NET_RXBUF_ALIGN - offset);
354 /* io-mmu-map the skb */
100e1d89
MM
355 descr->buf_addr = cpu_to_be32(dma_map_single(ctodev(card),
356 descr->skb->data,
357 GELIC_NET_MAX_MTU,
358 DMA_FROM_DEVICE));
02c18891
MM
359 if (!descr->buf_addr) {
360 dev_kfree_skb_any(descr->skb);
361 descr->skb = NULL;
362 dev_info(ctodev(card),
363 "%s:Could not iommu-map rx buffer\n", __func__);
59e97327 364 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
02c18891
MM
365 return -ENOMEM;
366 } else {
59e97327 367 gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
02c18891
MM
368 return 0;
369 }
370}
371
372/**
59e97327 373 * gelic_card_release_rx_chain - free all skb of rx descr
02c18891
MM
374 * @card: card structure
375 *
376 */
59e97327 377static void gelic_card_release_rx_chain(struct gelic_card *card)
02c18891 378{
59e97327 379 struct gelic_descr *descr = card->rx_chain.head;
02c18891
MM
380
381 do {
382 if (descr->skb) {
383 dma_unmap_single(ctodev(card),
100e1d89 384 be32_to_cpu(descr->buf_addr),
02c18891
MM
385 descr->skb->len,
386 DMA_FROM_DEVICE);
387 descr->buf_addr = 0;
388 dev_kfree_skb_any(descr->skb);
389 descr->skb = NULL;
59e97327
MM
390 gelic_descr_set_status(descr,
391 GELIC_DESCR_DMA_NOT_IN_USE);
02c18891
MM
392 }
393 descr = descr->next;
394 } while (descr != card->rx_chain.head);
395}
396
397/**
59e97327 398 * gelic_card_fill_rx_chain - fills descriptors/skbs in the rx chains
02c18891
MM
399 * @card: card structure
400 *
401 * fills all descriptors in the rx chain: allocates skbs
402 * and iommu-maps them.
59e97327 403 * returns 0 on success, < 0 on failure
02c18891 404 */
59e97327 405static int gelic_card_fill_rx_chain(struct gelic_card *card)
02c18891 406{
59e97327 407 struct gelic_descr *descr = card->rx_chain.head;
02c18891
MM
408 int ret;
409
410 do {
411 if (!descr->skb) {
59e97327 412 ret = gelic_descr_prepare_rx(card, descr);
02c18891
MM
413 if (ret)
414 goto rewind;
415 }
416 descr = descr->next;
417 } while (descr != card->rx_chain.head);
418
419 return 0;
420rewind:
59e97327 421 gelic_card_release_rx_chain(card);
02c18891
MM
422 return ret;
423}
424
425/**
59e97327 426 * gelic_card_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
02c18891
MM
427 * @card: card structure
428 *
59e97327 429 * returns 0 on success, < 0 on failure
02c18891 430 */
48dce82c 431static int __devinit gelic_card_alloc_rx_skbs(struct gelic_card *card)
02c18891 432{
59e97327 433 struct gelic_descr_chain *chain;
02c18891
MM
434 int ret;
435 chain = &card->rx_chain;
59e97327 436 ret = gelic_card_fill_rx_chain(card);
589866f9 437 chain->tail = card->rx_top->prev; /* point to the last */
02c18891
MM
438 return ret;
439}
440
441/**
59e97327 442 * gelic_descr_release_tx - processes a used tx descriptor
02c18891
MM
443 * @card: card structure
444 * @descr: descriptor to release
445 *
446 * releases a used tx descriptor (unmapping, freeing of skb)
447 */
59e97327 448static void gelic_descr_release_tx(struct gelic_card *card,
589866f9 449 struct gelic_descr *descr)
02c18891 450{
173261ed 451 struct sk_buff *skb = descr->skb;
02c18891 452
589866f9
MM
453 BUG_ON(!(be32_to_cpu(descr->data_status) & GELIC_DESCR_TX_TAIL));
454
455 dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr), skb->len,
456 DMA_TO_DEVICE);
173261ed 457 dev_kfree_skb_any(skb);
02c18891
MM
458
459 descr->buf_addr = 0;
460 descr->buf_size = 0;
461 descr->next_descr_addr = 0;
462 descr->result_size = 0;
463 descr->valid_size = 0;
464 descr->data_status = 0;
465 descr->data_error = 0;
466 descr->skb = NULL;
467
468 /* set descr status */
59e97327 469 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
02c18891
MM
470}
471
589866f9
MM
472static void gelic_card_stop_queues(struct gelic_card *card)
473{
d4d7f1f9 474 netif_stop_queue(card->netdev[GELIC_PORT_ETHERNET_0]);
589866f9
MM
475
476 if (card->netdev[GELIC_PORT_WIRELESS])
477 netif_stop_queue(card->netdev[GELIC_PORT_WIRELESS]);
478}
479static void gelic_card_wake_queues(struct gelic_card *card)
480{
d4d7f1f9 481 netif_wake_queue(card->netdev[GELIC_PORT_ETHERNET_0]);
589866f9
MM
482
483 if (card->netdev[GELIC_PORT_WIRELESS])
484 netif_wake_queue(card->netdev[GELIC_PORT_WIRELESS]);
485}
02c18891 486/**
59e97327 487 * gelic_card_release_tx_chain - processes sent tx descriptors
02c18891
MM
488 * @card: adapter structure
489 * @stop: net_stop sequence
490 *
491 * releases the tx descriptors that gelic has finished with
492 */
59e97327 493static void gelic_card_release_tx_chain(struct gelic_card *card, int stop)
02c18891 494{
59e97327
MM
495 struct gelic_descr_chain *tx_chain;
496 enum gelic_descr_dma_status status;
589866f9 497 struct net_device *netdev;
02c18891
MM
498 int release = 0;
499
500 for (tx_chain = &card->tx_chain;
501 tx_chain->head != tx_chain->tail && tx_chain->tail;
502 tx_chain->tail = tx_chain->tail->next) {
59e97327 503 status = gelic_descr_get_status(tx_chain->tail);
589866f9 504 netdev = tx_chain->tail->skb->dev;
02c18891 505 switch (status) {
59e97327
MM
506 case GELIC_DESCR_DMA_RESPONSE_ERROR:
507 case GELIC_DESCR_DMA_PROTECTION_ERROR:
508 case GELIC_DESCR_DMA_FORCE_END:
02c18891
MM
509 if (printk_ratelimit())
510 dev_info(ctodev(card),
511 "%s: forcing end of tx descriptor " \
512 "with status %x\n",
513 __func__, status);
589866f9 514 netdev->stats.tx_dropped++;
02c18891
MM
515 break;
516
59e97327 517 case GELIC_DESCR_DMA_COMPLETE:
48544cc2 518 if (tx_chain->tail->skb) {
589866f9
MM
519 netdev->stats.tx_packets++;
520 netdev->stats.tx_bytes +=
48544cc2
MM
521 tx_chain->tail->skb->len;
522 }
02c18891
MM
523 break;
524
59e97327 525 case GELIC_DESCR_DMA_CARDOWNED:
02c18891
MM
526 /* pending tx request */
527 default:
59e97327 528 /* any other value (== GELIC_DESCR_DMA_NOT_IN_USE) */
48544cc2
MM
529 if (!stop)
530 goto out;
02c18891 531 }
59e97327 532 gelic_descr_release_tx(card, tx_chain->tail);
48544cc2 533 release ++;
02c18891
MM
534 }
535out:
173261ed 536 if (!stop && release)
589866f9 537 gelic_card_wake_queues(card);
02c18891
MM
538}
539
540/**
541 * gelic_net_set_multi - sets multicast addresses and promisc flags
542 * @netdev: interface device structure
543 *
544 * gelic_net_set_multi configures multicast addresses as needed for the
545 * netdev interface. It also sets up multicast, allmulti and promisc
546 * flags appropriately
547 */
589866f9 548void gelic_net_set_multi(struct net_device *netdev)
02c18891 549{
589866f9 550 struct gelic_card *card = netdev_card(netdev);
22bedad3 551 struct netdev_hw_addr *ha;
02c18891
MM
552 unsigned int i;
553 uint8_t *p;
554 u64 addr;
555 int status;
556
557 /* clear all multicast address */
558 status = lv1_net_remove_multicast_address(bus_id(card), dev_id(card),
559 0, 1);
560 if (status)
561 dev_err(ctodev(card),
562 "lv1_net_remove_multicast_address failed %d\n",
563 status);
564 /* set broadcast address */
565 status = lv1_net_add_multicast_address(bus_id(card), dev_id(card),
566 GELIC_NET_BROADCAST_ADDR, 0);
567 if (status)
568 dev_err(ctodev(card),
569 "lv1_net_add_multicast_address failed, %d\n",
570 status);
571
589866f9 572 if ((netdev->flags & IFF_ALLMULTI) ||
4cd24eaf 573 (netdev_mc_count(netdev) > GELIC_NET_MC_COUNT_MAX)) {
02c18891
MM
574 status = lv1_net_add_multicast_address(bus_id(card),
575 dev_id(card),
576 0, 1);
577 if (status)
578 dev_err(ctodev(card),
579 "lv1_net_add_multicast_address failed, %d\n",
580 status);
581 return;
582 }
583
589866f9 584 /* set multicast addresses */
22bedad3 585 netdev_for_each_mc_addr(ha, netdev) {
02c18891 586 addr = 0;
22bedad3 587 p = ha->addr;
02c18891
MM
588 for (i = 0; i < ETH_ALEN; i++) {
589 addr <<= 8;
590 addr |= *p++;
591 }
592 status = lv1_net_add_multicast_address(bus_id(card),
593 dev_id(card),
594 addr, 0);
595 if (status)
596 dev_err(ctodev(card),
597 "lv1_net_add_multicast_address failed, %d\n",
598 status);
599 }
600}
601
602/**
59e97327 603 * gelic_card_enable_rxdmac - enables the receive DMA controller
02c18891
MM
604 * @card: card structure
605 *
59e97327 606 * gelic_card_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
02c18891
MM
607 * in the GDADMACCNTR register
608 */
59e97327 609static inline void gelic_card_enable_rxdmac(struct gelic_card *card)
02c18891
MM
610{
611 int status;
612
589866f9
MM
613#ifdef DEBUG
614 if (gelic_descr_get_status(card->rx_chain.head) !=
615 GELIC_DESCR_DMA_CARDOWNED) {
616 printk(KERN_ERR "%s: status=%x\n", __func__,
617 be32_to_cpu(card->rx_chain.head->dmac_cmd_status));
618 printk(KERN_ERR "%s: nextphy=%x\n", __func__,
619 be32_to_cpu(card->rx_chain.head->next_descr_addr));
620 printk(KERN_ERR "%s: head=%p\n", __func__,
621 card->rx_chain.head);
622 }
623#endif
02c18891 624 status = lv1_net_start_rx_dma(bus_id(card), dev_id(card),
589866f9 625 card->rx_chain.head->bus_addr, 0);
02c18891
MM
626 if (status)
627 dev_info(ctodev(card),
628 "lv1_net_start_rx_dma failed, status=%d\n", status);
629}
630
631/**
59e97327 632 * gelic_card_disable_rxdmac - disables the receive DMA controller
02c18891
MM
633 * @card: card structure
634 *
59e97327 635 * gelic_card_disable_rxdmac terminates processing on the DMA controller by
25985edc 636 * turing off DMA and issuing a force end
02c18891 637 */
59e97327 638static inline void gelic_card_disable_rxdmac(struct gelic_card *card)
02c18891
MM
639{
640 int status;
641
642 /* this hvc blocks until the DMA in progress really stopped */
643 status = lv1_net_stop_rx_dma(bus_id(card), dev_id(card), 0);
644 if (status)
645 dev_err(ctodev(card),
426d3107 646 "lv1_net_stop_rx_dma failed, %d\n", status);
02c18891
MM
647}
648
649/**
59e97327 650 * gelic_card_disable_txdmac - disables the transmit DMA controller
02c18891
MM
651 * @card: card structure
652 *
59e97327 653 * gelic_card_disable_txdmac terminates processing on the DMA controller by
25985edc 654 * turing off DMA and issuing a force end
02c18891 655 */
59e97327 656static inline void gelic_card_disable_txdmac(struct gelic_card *card)
02c18891
MM
657{
658 int status;
659
660 /* this hvc blocks until the DMA in progress really stopped */
661 status = lv1_net_stop_tx_dma(bus_id(card), dev_id(card), 0);
662 if (status)
663 dev_err(ctodev(card),
426d3107 664 "lv1_net_stop_tx_dma failed, status=%d\n", status);
02c18891
MM
665}
666
667/**
668 * gelic_net_stop - called upon ifconfig down
669 * @netdev: interface device structure
670 *
671 * always returns 0
672 */
589866f9 673int gelic_net_stop(struct net_device *netdev)
02c18891 674{
589866f9 675 struct gelic_card *card;
02c18891 676
589866f9 677 pr_debug("%s: start\n", __func__);
02c18891 678
589866f9 679 netif_stop_queue(netdev);
02c18891
MM
680 netif_carrier_off(netdev);
681
589866f9
MM
682 card = netdev_card(netdev);
683 gelic_card_down(card);
02c18891 684
589866f9 685 pr_debug("%s: done\n", __func__);
02c18891
MM
686 return 0;
687}
688
689/**
59e97327 690 * gelic_card_get_next_tx_descr - returns the next available tx descriptor
02c18891
MM
691 * @card: device structure to get descriptor from
692 *
693 * returns the address of the next descriptor, or NULL if not available.
694 */
59e97327
MM
695static struct gelic_descr *
696gelic_card_get_next_tx_descr(struct gelic_card *card)
02c18891
MM
697{
698 if (!card->tx_chain.head)
699 return NULL;
173261ed 700 /* see if the next descriptor is free */
02c18891 701 if (card->tx_chain.tail != card->tx_chain.head->next &&
59e97327
MM
702 gelic_descr_get_status(card->tx_chain.head) ==
703 GELIC_DESCR_DMA_NOT_IN_USE)
02c18891
MM
704 return card->tx_chain.head;
705 else
706 return NULL;
707
708}
709
710/**
589866f9 711 * gelic_net_set_txdescr_cmdstat - sets the tx descriptor command field
02c18891
MM
712 * @descr: descriptor structure to fill out
713 * @skb: packet to consider
02c18891
MM
714 *
715 * fills out the command and status field of the descriptor structure,
716 * depending on hardware checksum settings. This function assumes a wmb()
717 * has executed before.
718 */
59e97327
MM
719static void gelic_descr_set_tx_cmdstat(struct gelic_descr *descr,
720 struct sk_buff *skb)
02c18891 721{
02c18891 722 if (skb->ip_summed != CHECKSUM_PARTIAL)
100e1d89 723 descr->dmac_cmd_status =
59e97327
MM
724 cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
725 GELIC_DESCR_TX_DMA_FRAME_TAIL);
02c18891
MM
726 else {
727 /* is packet ip?
728 * if yes: tcp? udp? */
729 if (skb->protocol == htons(ETH_P_IP)) {
730 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
731 descr->dmac_cmd_status =
59e97327
MM
732 cpu_to_be32(GELIC_DESCR_DMA_CMD_TCP_CHKSUM |
733 GELIC_DESCR_TX_DMA_FRAME_TAIL);
173261ed 734
02c18891
MM
735 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
736 descr->dmac_cmd_status =
59e97327
MM
737 cpu_to_be32(GELIC_DESCR_DMA_CMD_UDP_CHKSUM |
738 GELIC_DESCR_TX_DMA_FRAME_TAIL);
02c18891
MM
739 else /*
740 * the stack should checksum non-tcp and non-udp
741 * packets on his own: NETIF_F_IP_CSUM
742 */
743 descr->dmac_cmd_status =
59e97327
MM
744 cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
745 GELIC_DESCR_TX_DMA_FRAME_TAIL);
02c18891
MM
746 }
747 }
748}
749
173261ed
MM
750static inline struct sk_buff *gelic_put_vlan_tag(struct sk_buff *skb,
751 unsigned short tag)
752{
753 struct vlan_ethhdr *veth;
754 static unsigned int c;
755
756 if (skb_headroom(skb) < VLAN_HLEN) {
757 struct sk_buff *sk_tmp = skb;
758 pr_debug("%s: hd=%d c=%ud\n", __func__, skb_headroom(skb), c);
759 skb = skb_realloc_headroom(sk_tmp, VLAN_HLEN);
760 if (!skb)
761 return NULL;
762 dev_kfree_skb_any(sk_tmp);
763 }
764 veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN);
765
766 /* Move the mac addresses to the top of buffer */
767 memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN);
768
09640e63 769 veth->h_vlan_proto = cpu_to_be16(ETH_P_8021Q);
173261ed
MM
770 veth->h_vlan_TCI = htons(tag);
771
772 return skb;
773}
774
02c18891 775/**
589866f9 776 * gelic_descr_prepare_tx - setup a descriptor for sending packets
02c18891
MM
777 * @card: card structure
778 * @descr: descriptor structure
779 * @skb: packet to use
780 *
781 * returns 0 on success, <0 on failure.
782 *
783 */
59e97327
MM
784static int gelic_descr_prepare_tx(struct gelic_card *card,
785 struct gelic_descr *descr,
786 struct sk_buff *skb)
02c18891 787{
173261ed 788 dma_addr_t buf;
02c18891 789
589866f9 790 if (card->vlan_required) {
173261ed 791 struct sk_buff *skb_tmp;
589866f9
MM
792 enum gelic_port_type type;
793
794 type = netdev_port(skb->dev)->type;
173261ed 795 skb_tmp = gelic_put_vlan_tag(skb,
589866f9 796 card->vlan[type].tx);
173261ed
MM
797 if (!skb_tmp)
798 return -ENOMEM;
799 skb = skb_tmp;
02c18891
MM
800 }
801
173261ed 802 buf = dma_map_single(ctodev(card), skb->data, skb->len, DMA_TO_DEVICE);
02c18891 803
173261ed 804 if (!buf) {
02c18891
MM
805 dev_err(ctodev(card),
806 "dma map 2 failed (%p, %i). Dropping packet\n",
173261ed 807 skb->data, skb->len);
02c18891
MM
808 return -ENOMEM;
809 }
810
100e1d89
MM
811 descr->buf_addr = cpu_to_be32(buf);
812 descr->buf_size = cpu_to_be32(skb->len);
173261ed 813 descr->skb = skb;
02c18891 814 descr->data_status = 0;
173261ed 815 descr->next_descr_addr = 0; /* terminate hw descr */
59e97327 816 gelic_descr_set_tx_cmdstat(descr, skb);
48544cc2
MM
817
818 /* bump free descriptor pointer */
173261ed 819 card->tx_chain.head = descr->next;
02c18891
MM
820 return 0;
821}
822
823/**
59e97327 824 * gelic_card_kick_txdma - enables TX DMA processing
02c18891
MM
825 * @card: card structure
826 * @descr: descriptor address to enable TX processing at
827 *
828 */
59e97327
MM
829static int gelic_card_kick_txdma(struct gelic_card *card,
830 struct gelic_descr *descr)
02c18891 831{
48544cc2 832 int status = 0;
02c18891
MM
833
834 if (card->tx_dma_progress)
835 return 0;
836
59e97327 837 if (gelic_descr_get_status(descr) == GELIC_DESCR_DMA_CARDOWNED) {
02c18891 838 card->tx_dma_progress = 1;
dc029ad9
MM
839 status = lv1_net_start_tx_dma(bus_id(card), dev_id(card),
840 descr->bus_addr, 0);
841 if (status)
02c18891 842 dev_info(ctodev(card), "lv1_net_start_txdma failed," \
dc029ad9 843 "status=%d\n", status);
02c18891
MM
844 }
845 return status;
846}
847
848/**
849 * gelic_net_xmit - transmits a frame over the device
850 * @skb: packet to send out
851 * @netdev: interface device structure
852 *
853 * returns 0 on success, <0 on failure
854 */
589866f9 855int gelic_net_xmit(struct sk_buff *skb, struct net_device *netdev)
02c18891 856{
589866f9 857 struct gelic_card *card = netdev_card(netdev);
59e97327 858 struct gelic_descr *descr;
02c18891
MM
859 int result;
860 unsigned long flags;
861
589866f9 862 spin_lock_irqsave(&card->tx_lock, flags);
02c18891 863
59e97327 864 gelic_card_release_tx_chain(card, 0);
48544cc2 865
59e97327 866 descr = gelic_card_get_next_tx_descr(card);
02c18891 867 if (!descr) {
48544cc2
MM
868 /*
869 * no more descriptors free
870 */
589866f9
MM
871 gelic_card_stop_queues(card);
872 spin_unlock_irqrestore(&card->tx_lock, flags);
02c18891
MM
873 return NETDEV_TX_BUSY;
874 }
02c18891 875
59e97327 876 result = gelic_descr_prepare_tx(card, descr, skb);
48544cc2
MM
877 if (result) {
878 /*
879 * DMA map failed. As chanses are that failure
880 * would continue, just release skb and return
881 */
589866f9 882 netdev->stats.tx_dropped++;
48544cc2 883 dev_kfree_skb_any(skb);
589866f9 884 spin_unlock_irqrestore(&card->tx_lock, flags);
48544cc2
MM
885 return NETDEV_TX_OK;
886 }
887 /*
888 * link this prepared descriptor to previous one
889 * to achieve high performance
890 */
100e1d89 891 descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
02c18891
MM
892 /*
893 * as hardware descriptor is modified in the above lines,
894 * ensure that the hardware sees it
895 */
896 wmb();
59e97327 897 if (gelic_card_kick_txdma(card, descr)) {
48544cc2
MM
898 /*
899 * kick failed.
900 * release descriptors which were just prepared
901 */
589866f9 902 netdev->stats.tx_dropped++;
59e97327
MM
903 gelic_descr_release_tx(card, descr);
904 gelic_descr_release_tx(card, descr->next);
48544cc2
MM
905 card->tx_chain.tail = descr->next->next;
906 dev_info(ctodev(card), "%s: kick failure\n", __func__);
48544cc2 907 }
02c18891 908
589866f9 909 spin_unlock_irqrestore(&card->tx_lock, flags);
02c18891 910 return NETDEV_TX_OK;
02c18891
MM
911}
912
913/**
914 * gelic_net_pass_skb_up - takes an skb from a descriptor and passes it on
915 * @descr: descriptor to process
916 * @card: card structure
589866f9 917 * @netdev: net_device structure to be passed packet
02c18891
MM
918 *
919 * iommu-unmaps the skb, fills out skb structure and passes the data to the
920 * stack. The descriptor state is not changed.
921 */
59e97327 922static void gelic_net_pass_skb_up(struct gelic_descr *descr,
589866f9
MM
923 struct gelic_card *card,
924 struct net_device *netdev)
925
02c18891 926{
589866f9 927 struct sk_buff *skb = descr->skb;
02c18891
MM
928 u32 data_status, data_error;
929
100e1d89
MM
930 data_status = be32_to_cpu(descr->data_status);
931 data_error = be32_to_cpu(descr->data_error);
02c18891 932 /* unmap skb buffer */
589866f9
MM
933 dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr),
934 GELIC_NET_MAX_MTU,
02c18891
MM
935 DMA_FROM_DEVICE);
936
589866f9 937 skb_put(skb, be32_to_cpu(descr->valid_size)?
100e1d89
MM
938 be32_to_cpu(descr->valid_size) :
939 be32_to_cpu(descr->result_size));
02c18891
MM
940 if (!descr->valid_size)
941 dev_info(ctodev(card), "buffer full %x %x %x\n",
100e1d89
MM
942 be32_to_cpu(descr->result_size),
943 be32_to_cpu(descr->buf_size),
944 be32_to_cpu(descr->dmac_cmd_status));
02c18891
MM
945
946 descr->skb = NULL;
947 /*
948 * the card put 2 bytes vlan tag in front
949 * of the ethernet frame
950 */
951 skb_pull(skb, 2);
952 skb->protocol = eth_type_trans(skb, netdev);
953
954 /* checksum offload */
d1423c7a 955 if (netdev->features & NETIF_F_RXCSUM) {
59e97327
MM
956 if ((data_status & GELIC_DESCR_DATA_STATUS_CHK_MASK) &&
957 (!(data_error & GELIC_DESCR_DATA_ERROR_CHK_MASK)))
02c18891
MM
958 skb->ip_summed = CHECKSUM_UNNECESSARY;
959 else
bc8acf2c 960 skb_checksum_none_assert(skb);
02c18891 961 } else
bc8acf2c 962 skb_checksum_none_assert(skb);
02c18891
MM
963
964 /* update netdevice statistics */
589866f9
MM
965 netdev->stats.rx_packets++;
966 netdev->stats.rx_bytes += skb->len;
02c18891
MM
967
968 /* pass skb up to stack */
969 netif_receive_skb(skb);
970}
971
972/**
59e97327 973 * gelic_card_decode_one_descr - processes an rx descriptor
02c18891
MM
974 * @card: card structure
975 *
976 * returns 1 if a packet has been sent to the stack, otherwise 0
977 *
978 * processes an rx descriptor by iommu-unmapping the data buffer and passing
979 * the packet up to the stack
980 */
59e97327 981static int gelic_card_decode_one_descr(struct gelic_card *card)
02c18891 982{
59e97327
MM
983 enum gelic_descr_dma_status status;
984 struct gelic_descr_chain *chain = &card->rx_chain;
589866f9
MM
985 struct gelic_descr *descr = chain->head;
986 struct net_device *netdev = NULL;
02c18891
MM
987 int dmac_chain_ended;
988
59e97327 989 status = gelic_descr_get_status(descr);
02c18891
MM
990 /* is this descriptor terminated with next_descr == NULL? */
991 dmac_chain_ended =
100e1d89 992 be32_to_cpu(descr->dmac_cmd_status) &
59e97327 993 GELIC_DESCR_RX_DMA_CHAIN_END;
02c18891 994
59e97327 995 if (status == GELIC_DESCR_DMA_CARDOWNED)
02c18891
MM
996 return 0;
997
59e97327 998 if (status == GELIC_DESCR_DMA_NOT_IN_USE) {
02c18891
MM
999 dev_dbg(ctodev(card), "dormant descr? %p\n", descr);
1000 return 0;
1001 }
1002
589866f9
MM
1003 /* netdevice select */
1004 if (card->vlan_required) {
1005 unsigned int i;
1006 u16 vid;
1007 vid = *(u16 *)(descr->skb->data) & VLAN_VID_MASK;
1008 for (i = 0; i < GELIC_PORT_MAX; i++) {
1009 if (card->vlan[i].rx == vid) {
1010 netdev = card->netdev[i];
1011 break;
1012 }
6403eab1 1013 }
589866f9
MM
1014 if (GELIC_PORT_MAX <= i) {
1015 pr_info("%s: unknown packet vid=%x\n", __func__, vid);
1016 goto refill;
1017 }
1018 } else
d4d7f1f9 1019 netdev = card->netdev[GELIC_PORT_ETHERNET_0];
589866f9 1020
59e97327
MM
1021 if ((status == GELIC_DESCR_DMA_RESPONSE_ERROR) ||
1022 (status == GELIC_DESCR_DMA_PROTECTION_ERROR) ||
1023 (status == GELIC_DESCR_DMA_FORCE_END)) {
02c18891
MM
1024 dev_info(ctodev(card), "dropping RX descriptor with state %x\n",
1025 status);
589866f9 1026 netdev->stats.rx_dropped++;
02c18891
MM
1027 goto refill;
1028 }
1029
59e97327 1030 if (status == GELIC_DESCR_DMA_BUFFER_FULL) {
fe6d3a40
MM
1031 /*
1032 * Buffer full would occur if and only if
1033 * the frame length was longer than the size of this
1034 * descriptor's buffer. If the frame length was equal
1035 * to or shorter than buffer'size, FRAME_END condition
1036 * would occur.
1037 * Anyway this frame was longer than the MTU,
1038 * just drop it.
1039 */
1040 dev_info(ctodev(card), "overlength frame\n");
1041 goto refill;
1042 }
1043 /*
1044 * descriptoers any other than FRAME_END here should
1045 * be treated as error.
1046 */
59e97327 1047 if (status != GELIC_DESCR_DMA_FRAME_END) {
02c18891
MM
1048 dev_dbg(ctodev(card), "RX descriptor with state %x\n",
1049 status);
1050 goto refill;
1051 }
1052
1053 /* ok, we've got a packet in descr */
589866f9 1054 gelic_net_pass_skb_up(descr, card, netdev);
02c18891 1055refill:
fe6d3a40
MM
1056 /*
1057 * So that always DMAC can see the end
1058 * of the descriptor chain to avoid
1059 * from unwanted DMAC overrun.
1060 */
1061 descr->next_descr_addr = 0;
02c18891
MM
1062
1063 /* change the descriptor state: */
59e97327 1064 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
02c18891 1065
fe6d3a40
MM
1066 /*
1067 * this call can fail, but for now, just leave this
1068 * decriptor without skb
02c18891 1069 */
59e97327 1070 gelic_descr_prepare_rx(card, descr);
fe6d3a40 1071
589866f9
MM
1072 chain->tail = descr;
1073 chain->head = descr->next;
fe6d3a40
MM
1074
1075 /*
1076 * Set this descriptor the end of the chain.
1077 */
100e1d89 1078 descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
02c18891 1079
fe6d3a40
MM
1080 /*
1081 * If dmac chain was met, DMAC stopped.
1082 * thus re-enable it
1083 */
02c18891 1084 if (dmac_chain_ended) {
583aae10
MM
1085 card->rx_dma_restart_required = 1;
1086 dev_dbg(ctodev(card), "reenable rx dma scheduled\n");
02c18891
MM
1087 }
1088
1089 return 1;
1090}
1091
1092/**
1093 * gelic_net_poll - NAPI poll function called by the stack to return packets
589866f9 1094 * @napi: napi structure
02c18891
MM
1095 * @budget: number of packets we can pass to the stack at most
1096 *
589866f9 1097 * returns the number of the processed packets
02c18891
MM
1098 *
1099 */
bea3348e 1100static int gelic_net_poll(struct napi_struct *napi, int budget)
02c18891 1101{
59e97327 1102 struct gelic_card *card = container_of(napi, struct gelic_card, napi);
bea3348e 1103 int packets_done = 0;
02c18891 1104
bea3348e 1105 while (packets_done < budget) {
59e97327 1106 if (!gelic_card_decode_one_descr(card))
02c18891 1107 break;
bea3348e
SH
1108
1109 packets_done++;
02c18891 1110 }
bea3348e
SH
1111
1112 if (packets_done < budget) {
589866f9 1113 napi_complete(napi);
59e97327 1114 gelic_card_rx_irq_on(card);
bea3348e
SH
1115 }
1116 return packets_done;
02c18891 1117}
02c18891
MM
1118/**
1119 * gelic_net_change_mtu - changes the MTU of an interface
1120 * @netdev: interface device structure
1121 * @new_mtu: new MTU value
1122 *
1123 * returns 0 on success, <0 on failure
1124 */
589866f9 1125int gelic_net_change_mtu(struct net_device *netdev, int new_mtu)
02c18891
MM
1126{
1127 /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
1128 * and mtu is outbound only anyway */
1129 if ((new_mtu < GELIC_NET_MIN_MTU) ||
1130 (new_mtu > GELIC_NET_MAX_MTU)) {
1131 return -EINVAL;
1132 }
1133 netdev->mtu = new_mtu;
1134 return 0;
1135}
1136
1137/**
59e97327 1138 * gelic_card_interrupt - event handler for gelic_net
02c18891 1139 */
59e97327 1140static irqreturn_t gelic_card_interrupt(int irq, void *ptr)
02c18891
MM
1141{
1142 unsigned long flags;
589866f9 1143 struct gelic_card *card = ptr;
02c18891
MM
1144 u64 status;
1145
1146 status = card->irq_status;
1147
1148 if (!status)
1149 return IRQ_NONE;
1150
589866f9
MM
1151 status &= card->irq_mask;
1152
583aae10
MM
1153 if (card->rx_dma_restart_required) {
1154 card->rx_dma_restart_required = 0;
59e97327 1155 gelic_card_enable_rxdmac(card);
583aae10
MM
1156 }
1157
59e97327
MM
1158 if (status & GELIC_CARD_RXINT) {
1159 gelic_card_rx_irq_off(card);
589866f9 1160 napi_schedule(&card->napi);
02c18891
MM
1161 }
1162
59e97327 1163 if (status & GELIC_CARD_TXINT) {
589866f9 1164 spin_lock_irqsave(&card->tx_lock, flags);
02c18891 1165 card->tx_dma_progress = 0;
59e97327 1166 gelic_card_release_tx_chain(card, 0);
48544cc2 1167 /* kick outstanding tx descriptor if any */
59e97327 1168 gelic_card_kick_txdma(card, card->tx_chain.tail);
589866f9 1169 spin_unlock_irqrestore(&card->tx_lock, flags);
02c18891 1170 }
01fed4c2
MM
1171
1172 /* ether port status changed */
1173 if (status & GELIC_CARD_PORT_STATUS_CHANGED)
1174 gelic_card_get_ether_port_status(card, 1);
589866f9 1175
09dde54c
MM
1176#ifdef CONFIG_GELIC_WIRELESS
1177 if (status & (GELIC_CARD_WLAN_EVENT_RECEIVED |
1178 GELIC_CARD_WLAN_COMMAND_COMPLETED))
1179 gelic_wl_interrupt(card->netdev[GELIC_PORT_WIRELESS], status);
1180#endif
1181
02c18891
MM
1182 return IRQ_HANDLED;
1183}
1184
1185#ifdef CONFIG_NET_POLL_CONTROLLER
1186/**
1187 * gelic_net_poll_controller - artificial interrupt for netconsole etc.
1188 * @netdev: interface device structure
1189 *
1190 * see Documentation/networking/netconsole.txt
1191 */
589866f9 1192void gelic_net_poll_controller(struct net_device *netdev)
02c18891 1193{
589866f9 1194 struct gelic_card *card = netdev_card(netdev);
02c18891 1195
59e97327
MM
1196 gelic_card_set_irq_mask(card, 0);
1197 gelic_card_interrupt(netdev->irq, netdev);
589866f9 1198 gelic_card_set_irq_mask(card, card->irq_mask);
02c18891
MM
1199}
1200#endif /* CONFIG_NET_POLL_CONTROLLER */
1201
02c18891
MM
1202/**
1203 * gelic_net_open - called upon ifonfig up
1204 * @netdev: interface device structure
1205 *
1206 * returns 0 on success, <0 on failure
1207 *
1208 * gelic_net_open allocates all the descriptors and memory needed for
1209 * operation, sets up multicast list and enables interrupts
1210 */
589866f9 1211int gelic_net_open(struct net_device *netdev)
02c18891 1212{
589866f9 1213 struct gelic_card *card = netdev_card(netdev);
02c18891 1214
589866f9 1215 dev_dbg(ctodev(card), " -> %s %p\n", __func__, netdev);
02c18891 1216
589866f9 1217 gelic_card_up(card);
02c18891
MM
1218
1219 netif_start_queue(netdev);
01fed4c2 1220 gelic_card_get_ether_port_status(card, 1);
02c18891 1221
589866f9 1222 dev_dbg(ctodev(card), " <- %s\n", __func__);
02c18891 1223 return 0;
02c18891
MM
1224}
1225
589866f9
MM
1226void gelic_net_get_drvinfo(struct net_device *netdev,
1227 struct ethtool_drvinfo *info)
02c18891
MM
1228{
1229 strncpy(info->driver, DRV_NAME, sizeof(info->driver) - 1);
1230 strncpy(info->version, DRV_VERSION, sizeof(info->version) - 1);
1231}
1232
59e97327
MM
1233static int gelic_ether_get_settings(struct net_device *netdev,
1234 struct ethtool_cmd *cmd)
02c18891 1235{
589866f9 1236 struct gelic_card *card = netdev_card(netdev);
02c18891 1237
01fed4c2
MM
1238 gelic_card_get_ether_port_status(card, 0);
1239
1240 if (card->ether_port_status & GELIC_LV1_ETHER_FULL_DUPLEX)
1241 cmd->duplex = DUPLEX_FULL;
1242 else
1243 cmd->duplex = DUPLEX_HALF;
1244
1245 switch (card->ether_port_status & GELIC_LV1_ETHER_SPEED_MASK) {
1246 case GELIC_LV1_ETHER_SPEED_10:
70739497 1247 ethtool_cmd_speed_set(cmd, SPEED_10);
01fed4c2
MM
1248 break;
1249 case GELIC_LV1_ETHER_SPEED_100:
70739497 1250 ethtool_cmd_speed_set(cmd, SPEED_100);
01fed4c2
MM
1251 break;
1252 case GELIC_LV1_ETHER_SPEED_1000:
70739497 1253 ethtool_cmd_speed_set(cmd, SPEED_1000);
01fed4c2
MM
1254 break;
1255 default:
1256 pr_info("%s: speed unknown\n", __func__);
70739497 1257 ethtool_cmd_speed_set(cmd, SPEED_10);
01fed4c2 1258 break;
02c18891 1259 }
01fed4c2 1260
02c18891
MM
1261 cmd->supported = SUPPORTED_TP | SUPPORTED_Autoneg |
1262 SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
1263 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
55873ed8 1264 SUPPORTED_1000baseT_Full;
02c18891 1265 cmd->advertising = cmd->supported;
55873ed8
HS
1266 if (card->link_mode & GELIC_LV1_ETHER_AUTO_NEG) {
1267 cmd->autoneg = AUTONEG_ENABLE;
1268 } else {
1269 cmd->autoneg = AUTONEG_DISABLE;
1270 cmd->advertising &= ~ADVERTISED_Autoneg;
1271 }
02c18891
MM
1272 cmd->port = PORT_TP;
1273
1274 return 0;
1275}
1276
55873ed8
HS
1277static int gelic_ether_set_settings(struct net_device *netdev,
1278 struct ethtool_cmd *cmd)
1279{
1280 struct gelic_card *card = netdev_card(netdev);
1281 u64 mode;
1282 int ret;
1283
1284 if (cmd->autoneg == AUTONEG_ENABLE) {
1285 mode = GELIC_LV1_ETHER_AUTO_NEG;
1286 } else {
1287 switch (cmd->speed) {
1288 case SPEED_10:
1289 mode = GELIC_LV1_ETHER_SPEED_10;
1290 break;
1291 case SPEED_100:
1292 mode = GELIC_LV1_ETHER_SPEED_100;
1293 break;
1294 case SPEED_1000:
1295 mode = GELIC_LV1_ETHER_SPEED_1000;
1296 break;
1297 default:
1298 return -EINVAL;
1299 }
1300 if (cmd->duplex == DUPLEX_FULL)
1301 mode |= GELIC_LV1_ETHER_FULL_DUPLEX;
1302 else if (cmd->speed == SPEED_1000) {
1303 pr_info("1000 half duplex is not supported.\n");
1304 return -EINVAL;
1305 }
1306 }
1307
1308 ret = gelic_card_set_link_mode(card, mode);
1309
1310 if (ret)
1311 return ret;
1312
1313 return 0;
1314}
1315
3faac215
MM
1316static void gelic_net_get_wol(struct net_device *netdev,
1317 struct ethtool_wolinfo *wol)
1318{
1319 if (0 <= ps3_compare_firmware_version(2, 2, 0))
1320 wol->supported = WAKE_MAGIC;
1321 else
1322 wol->supported = 0;
1323
1324 wol->wolopts = ps3_sys_manager_get_wol() ? wol->supported : 0;
1325 memset(&wol->sopass, 0, sizeof(wol->sopass));
1326}
1327static int gelic_net_set_wol(struct net_device *netdev,
1328 struct ethtool_wolinfo *wol)
1329{
1330 int status;
1331 struct gelic_card *card;
1332 u64 v1, v2;
1333
1334 if (ps3_compare_firmware_version(2, 2, 0) < 0 ||
1335 !capable(CAP_NET_ADMIN))
1336 return -EPERM;
1337
1338 if (wol->wolopts & ~WAKE_MAGIC)
1339 return -EINVAL;
1340
1341 card = netdev_card(netdev);
1342 if (wol->wolopts & WAKE_MAGIC) {
1343 status = lv1_net_control(bus_id(card), dev_id(card),
1344 GELIC_LV1_SET_WOL,
1345 GELIC_LV1_WOL_MAGIC_PACKET,
1346 0, GELIC_LV1_WOL_MP_ENABLE,
1347 &v1, &v2);
1348 if (status) {
1349 pr_info("%s: enabling WOL failed %d\n", __func__,
1350 status);
1351 status = -EIO;
1352 goto done;
1353 }
1354 status = lv1_net_control(bus_id(card), dev_id(card),
1355 GELIC_LV1_SET_WOL,
1356 GELIC_LV1_WOL_ADD_MATCH_ADDR,
1357 0, GELIC_LV1_WOL_MATCH_ALL,
1358 &v1, &v2);
1359 if (!status)
1360 ps3_sys_manager_set_wol(1);
1361 else {
1362 pr_info("%s: enabling WOL filter failed %d\n",
1363 __func__, status);
1364 status = -EIO;
1365 }
1366 } else {
1367 status = lv1_net_control(bus_id(card), dev_id(card),
1368 GELIC_LV1_SET_WOL,
1369 GELIC_LV1_WOL_MAGIC_PACKET,
1370 0, GELIC_LV1_WOL_MP_DISABLE,
1371 &v1, &v2);
1372 if (status) {
1373 pr_info("%s: disabling WOL failed %d\n", __func__,
1374 status);
1375 status = -EIO;
1376 goto done;
1377 }
1378 status = lv1_net_control(bus_id(card), dev_id(card),
1379 GELIC_LV1_SET_WOL,
1380 GELIC_LV1_WOL_DELETE_MATCH_ADDR,
1381 0, GELIC_LV1_WOL_MATCH_ALL,
1382 &v1, &v2);
1383 if (!status)
1384 ps3_sys_manager_set_wol(0);
1385 else {
1386 pr_info("%s: removing WOL filter failed %d\n",
1387 __func__, status);
1388 status = -EIO;
1389 }
1390 }
1391done:
1392 return status;
1393}
1394
0fc0b732 1395static const struct ethtool_ops gelic_ether_ethtool_ops = {
02c18891 1396 .get_drvinfo = gelic_net_get_drvinfo,
59e97327 1397 .get_settings = gelic_ether_get_settings,
55873ed8 1398 .set_settings = gelic_ether_set_settings,
7bc56b92 1399 .get_link = ethtool_op_get_link,
3faac215
MM
1400 .get_wol = gelic_net_get_wol,
1401 .set_wol = gelic_net_set_wol,
02c18891 1402};
02c18891
MM
1403
1404/**
1405 * gelic_net_tx_timeout_task - task scheduled by the watchdog timeout
1406 * function (to be called not under interrupt status)
1407 * @work: work is context of tx timout task
1408 *
1409 * called as task when tx hangs, resets interface (if interface is up)
1410 */
1411static void gelic_net_tx_timeout_task(struct work_struct *work)
1412{
59e97327
MM
1413 struct gelic_card *card =
1414 container_of(work, struct gelic_card, tx_timeout_task);
d4d7f1f9 1415 struct net_device *netdev = card->netdev[GELIC_PORT_ETHERNET_0];
02c18891 1416
583d077d 1417 dev_info(ctodev(card), "%s:Timed out. Restarting...\n", __func__);
02c18891
MM
1418
1419 if (!(netdev->flags & IFF_UP))
1420 goto out;
1421
1422 netif_device_detach(netdev);
1423 gelic_net_stop(netdev);
1424
1425 gelic_net_open(netdev);
1426 netif_device_attach(netdev);
1427
1428out:
1429 atomic_dec(&card->tx_timeout_task_counter);
1430}
1431
1432/**
1433 * gelic_net_tx_timeout - called when the tx timeout watchdog kicks in.
1434 * @netdev: interface device structure
1435 *
1436 * called, if tx hangs. Schedules a task that resets the interface
1437 */
589866f9 1438void gelic_net_tx_timeout(struct net_device *netdev)
02c18891 1439{
59e97327 1440 struct gelic_card *card;
02c18891 1441
589866f9 1442 card = netdev_card(netdev);
02c18891
MM
1443 atomic_inc(&card->tx_timeout_task_counter);
1444 if (netdev->flags & IFF_UP)
1445 schedule_work(&card->tx_timeout_task);
1446 else
1447 atomic_dec(&card->tx_timeout_task_counter);
1448}
1449
5384e836
MM
1450static const struct net_device_ops gelic_netdevice_ops = {
1451 .ndo_open = gelic_net_open,
1452 .ndo_stop = gelic_net_stop,
1453 .ndo_start_xmit = gelic_net_xmit,
1454 .ndo_set_multicast_list = gelic_net_set_multi,
1455 .ndo_change_mtu = gelic_net_change_mtu,
1456 .ndo_tx_timeout = gelic_net_tx_timeout,
240c102d 1457 .ndo_set_mac_address = eth_mac_addr,
5384e836
MM
1458 .ndo_validate_addr = eth_validate_addr,
1459#ifdef CONFIG_NET_POLL_CONTROLLER
1460 .ndo_poll_controller = gelic_net_poll_controller,
1461#endif
1462};
1463
02c18891 1464/**
59e97327 1465 * gelic_ether_setup_netdev_ops - initialization of net_device operations
02c18891
MM
1466 * @netdev: net_device structure
1467 *
1468 * fills out function pointers in the net_device structure
1469 */
48dce82c
GU
1470static void __devinit gelic_ether_setup_netdev_ops(struct net_device *netdev,
1471 struct napi_struct *napi)
02c18891 1472{
02c18891 1473 netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;
589866f9
MM
1474 /* NAPI */
1475 netif_napi_add(netdev, napi,
1476 gelic_net_poll, GELIC_NET_NAPI_WEIGHT);
1477 netdev->ethtool_ops = &gelic_ether_ethtool_ops;
5384e836 1478 netdev->netdev_ops = &gelic_netdevice_ops;
02c18891
MM
1479}
1480
1481/**
589866f9
MM
1482 * gelic_ether_setup_netdev - initialization of net_device
1483 * @netdev: net_device structure
02c18891
MM
1484 * @card: card structure
1485 *
1486 * Returns 0 on success or <0 on failure
1487 *
589866f9
MM
1488 * gelic_ether_setup_netdev initializes the net_device structure
1489 * and register it.
02c18891 1490 **/
48dce82c
GU
1491int __devinit gelic_net_setup_netdev(struct net_device *netdev,
1492 struct gelic_card *card)
02c18891 1493{
02c18891
MM
1494 int status;
1495 u64 v1, v2;
1496
d1423c7a
MM
1497 netdev->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1498
02c18891 1499 netdev->features = NETIF_F_IP_CSUM;
d1423c7a
MM
1500 if (GELIC_CARD_RX_CSUM_DEFAULT)
1501 netdev->features |= NETIF_F_RXCSUM;
02c18891
MM
1502
1503 status = lv1_net_control(bus_id(card), dev_id(card),
59e97327 1504 GELIC_LV1_GET_MAC_ADDRESS,
02c18891 1505 0, 0, 0, &v1, &v2);
589866f9 1506 v1 <<= 16;
02c18891
MM
1507 if (status || !is_valid_ether_addr((u8 *)&v1)) {
1508 dev_info(ctodev(card),
1509 "%s:lv1_net_control GET_MAC_ADDR failed %d\n",
1510 __func__, status);
1511 return -EINVAL;
1512 }
589866f9 1513 memcpy(netdev->dev_addr, &v1, ETH_ALEN);
173261ed 1514
589866f9 1515 if (card->vlan_required) {
173261ed 1516 netdev->hard_header_len += VLAN_HLEN;
589866f9
MM
1517 /*
1518 * As vlan is internally used,
1519 * we can not receive vlan packets
1520 */
1521 netdev->features |= NETIF_F_VLAN_CHALLENGED;
173261ed 1522 }
02c18891
MM
1523
1524 status = register_netdev(netdev);
1525 if (status) {
589866f9
MM
1526 dev_err(ctodev(card), "%s:Couldn't register %s %d\n",
1527 __func__, netdev->name, status);
02c18891
MM
1528 return status;
1529 }
e174961c
JB
1530 dev_info(ctodev(card), "%s: MAC addr %pM\n",
1531 netdev->name, netdev->dev_addr);
02c18891
MM
1532
1533 return 0;
1534}
1535
1536/**
59e97327 1537 * gelic_alloc_card_net - allocates net_device and card structure
02c18891
MM
1538 *
1539 * returns the card structure or NULL in case of errors
1540 *
1541 * the card and net_device structures are linked to each other
1542 */
589866f9 1543#define GELIC_ALIGN (32)
48dce82c 1544static struct gelic_card * __devinit gelic_alloc_card_net(struct net_device **netdev)
02c18891 1545{
59e97327 1546 struct gelic_card *card;
589866f9
MM
1547 struct gelic_port *port;
1548 void *p;
02c18891 1549 size_t alloc_size;
02c18891 1550 /*
589866f9
MM
1551 * gelic requires dma descriptor is 32 bytes aligned and
1552 * the hypervisor requires irq_status is 8 bytes aligned.
02c18891 1553 */
59e97327
MM
1554 BUILD_BUG_ON(offsetof(struct gelic_card, irq_status) % 8);
1555 BUILD_BUG_ON(offsetof(struct gelic_card, descr) % 32);
589866f9
MM
1556 alloc_size =
1557 sizeof(struct gelic_card) +
1558 sizeof(struct gelic_descr) * GELIC_NET_RX_DESCRIPTORS +
1559 sizeof(struct gelic_descr) * GELIC_NET_TX_DESCRIPTORS +
1560 GELIC_ALIGN - 1;
02c18891 1561
589866f9
MM
1562 p = kzalloc(alloc_size, GFP_KERNEL);
1563 if (!p)
02c18891 1564 return NULL;
589866f9
MM
1565 card = PTR_ALIGN(p, GELIC_ALIGN);
1566 card->unalign = p;
1567
1568 /*
1569 * alloc netdev
1570 */
1571 *netdev = alloc_etherdev(sizeof(struct gelic_port));
1572 if (!netdev) {
1573 kfree(card->unalign);
1574 return NULL;
1575 }
1576 port = netdev_priv(*netdev);
1577
1578 /* gelic_port */
1579 port->netdev = *netdev;
1580 port->card = card;
d4d7f1f9 1581 port->type = GELIC_PORT_ETHERNET_0;
589866f9
MM
1582
1583 /* gelic_card */
d4d7f1f9 1584 card->netdev[GELIC_PORT_ETHERNET_0] = *netdev;
02c18891 1585
02c18891
MM
1586 INIT_WORK(&card->tx_timeout_task, gelic_net_tx_timeout_task);
1587 init_waitqueue_head(&card->waitq);
1588 atomic_set(&card->tx_timeout_task_counter, 0);
2914f3ef 1589 mutex_init(&card->updown_lock);
589866f9 1590 atomic_set(&card->users, 0);
02c18891
MM
1591
1592 return card;
1593}
1594
48dce82c 1595static void __devinit gelic_card_get_vlan_info(struct gelic_card *card)
589866f9
MM
1596{
1597 u64 v1, v2;
1598 int status;
1599 unsigned int i;
1600 struct {
1601 int tx;
1602 int rx;
1603 } vlan_id_ix[2] = {
d4d7f1f9
GL
1604 [GELIC_PORT_ETHERNET_0] = {
1605 .tx = GELIC_LV1_VLAN_TX_ETHERNET_0,
1606 .rx = GELIC_LV1_VLAN_RX_ETHERNET_0
589866f9
MM
1607 },
1608 [GELIC_PORT_WIRELESS] = {
1609 .tx = GELIC_LV1_VLAN_TX_WIRELESS,
1610 .rx = GELIC_LV1_VLAN_RX_WIRELESS
1611 }
1612 };
1613
1614 for (i = 0; i < ARRAY_SIZE(vlan_id_ix); i++) {
1615 /* tx tag */
1616 status = lv1_net_control(bus_id(card), dev_id(card),
1617 GELIC_LV1_GET_VLAN_ID,
1618 vlan_id_ix[i].tx,
1619 0, 0, &v1, &v2);
1620 if (status || !v1) {
1621 if (status != LV1_NO_ENTRY)
1622 dev_dbg(ctodev(card),
1623 "get vlan id for tx(%d) failed(%d)\n",
1624 vlan_id_ix[i].tx, status);
1625 card->vlan[i].tx = 0;
1626 card->vlan[i].rx = 0;
1627 continue;
1628 }
1629 card->vlan[i].tx = (u16)v1;
1630
1631 /* rx tag */
1632 status = lv1_net_control(bus_id(card), dev_id(card),
1633 GELIC_LV1_GET_VLAN_ID,
1634 vlan_id_ix[i].rx,
1635 0, 0, &v1, &v2);
1636 if (status || !v1) {
1637 if (status != LV1_NO_ENTRY)
1638 dev_info(ctodev(card),
1639 "get vlan id for rx(%d) failed(%d)\n",
1640 vlan_id_ix[i].rx, status);
1641 card->vlan[i].tx = 0;
1642 card->vlan[i].rx = 0;
1643 continue;
1644 }
1645 card->vlan[i].rx = (u16)v1;
1646
1647 dev_dbg(ctodev(card), "vlan_id[%d] tx=%02x rx=%02x\n",
1648 i, card->vlan[i].tx, card->vlan[i].rx);
1649 }
1650
d4d7f1f9 1651 if (card->vlan[GELIC_PORT_ETHERNET_0].tx) {
589866f9
MM
1652 BUG_ON(!card->vlan[GELIC_PORT_WIRELESS].tx);
1653 card->vlan_required = 1;
1654 } else
1655 card->vlan_required = 0;
1656
1657 /* check wirelss capable firmware */
1658 if (ps3_compare_firmware_version(1, 6, 0) < 0) {
1659 card->vlan[GELIC_PORT_WIRELESS].tx = 0;
1660 card->vlan[GELIC_PORT_WIRELESS].rx = 0;
1661 }
1662
1663 dev_info(ctodev(card), "internal vlan %s\n",
1664 card->vlan_required? "enabled" : "disabled");
1665}
02c18891
MM
1666/**
1667 * ps3_gelic_driver_probe - add a device to the control of this driver
1668 */
48dce82c 1669static int __devinit ps3_gelic_driver_probe(struct ps3_system_bus_device *dev)
02c18891 1670{
589866f9
MM
1671 struct gelic_card *card;
1672 struct net_device *netdev;
02c18891
MM
1673 int result;
1674
589866f9 1675 pr_debug("%s: called\n", __func__);
02c18891
MM
1676 result = ps3_open_hv_device(dev);
1677
1678 if (result) {
589866f9
MM
1679 dev_dbg(&dev->core, "%s:ps3_open_hv_device failed\n",
1680 __func__);
02c18891
MM
1681 goto fail_open;
1682 }
1683
1684 result = ps3_dma_region_create(dev->d_region);
1685
1686 if (result) {
589866f9
MM
1687 dev_dbg(&dev->core, "%s:ps3_dma_region_create failed(%d)\n",
1688 __func__, result);
02c18891
MM
1689 BUG_ON("check region type");
1690 goto fail_dma_region;
1691 }
1692
589866f9
MM
1693 /* alloc card/netdevice */
1694 card = gelic_alloc_card_net(&netdev);
1695 if (!card) {
1696 dev_info(&dev->core, "%s:gelic_net_alloc_card failed\n",
1697 __func__);
1698 result = -ENOMEM;
1699 goto fail_alloc_card;
1700 }
03fa68c2 1701 ps3_system_bus_set_drvdata(dev, card);
589866f9
MM
1702 card->dev = dev;
1703
1704 /* get internal vlan info */
1705 gelic_card_get_vlan_info(card);
1706
55873ed8
HS
1707 card->link_mode = GELIC_LV1_ETHER_AUTO_NEG;
1708
589866f9 1709 /* setup interrupt */
02c18891
MM
1710 result = lv1_net_set_interrupt_status_indicator(bus_id(card),
1711 dev_id(card),
1712 ps3_mm_phys_to_lpar(__pa(&card->irq_status)),
1713 0);
1714
1715 if (result) {
1716 dev_dbg(&dev->core,
589866f9
MM
1717 "%s:set_interrupt_status_indicator failed: %s\n",
1718 __func__, ps3_result(result));
02c18891
MM
1719 result = -EIO;
1720 goto fail_status_indicator;
1721 }
1722
589866f9
MM
1723 result = ps3_sb_event_receive_port_setup(dev, PS3_BINDING_CPU_ANY,
1724 &card->irq);
1725
1726 if (result) {
1727 dev_info(ctodev(card),
1728 "%s:gelic_net_open_device failed (%d)\n",
1729 __func__, result);
1730 result = -EPERM;
1731 goto fail_alloc_irq;
1732 }
1733 result = request_irq(card->irq, gelic_card_interrupt,
1734 IRQF_DISABLED, netdev->name, card);
1735
1736 if (result) {
1737 dev_info(ctodev(card), "%s:request_irq failed (%d)\n",
1738 __func__, result);
1739 goto fail_request_irq;
1740 }
1741
1742 /* setup card structure */
1743 card->irq_mask = GELIC_CARD_RXINT | GELIC_CARD_TXINT |
1744 GELIC_CARD_PORT_STATUS_CHANGED;
589866f9
MM
1745
1746
1747 if (gelic_card_init_chain(card, &card->tx_chain,
1748 card->descr, GELIC_NET_TX_DESCRIPTORS))
1749 goto fail_alloc_tx;
1750 if (gelic_card_init_chain(card, &card->rx_chain,
1751 card->descr + GELIC_NET_TX_DESCRIPTORS,
1752 GELIC_NET_RX_DESCRIPTORS))
1753 goto fail_alloc_rx;
1754
1755 /* head of chain */
1756 card->tx_top = card->tx_chain.head;
1757 card->rx_top = card->rx_chain.head;
1758 dev_dbg(ctodev(card), "descr rx %p, tx %p, size %#lx, num %#x\n",
1759 card->rx_top, card->tx_top, sizeof(struct gelic_descr),
1760 GELIC_NET_RX_DESCRIPTORS);
1761 /* allocate rx skbs */
1762 if (gelic_card_alloc_rx_skbs(card))
1763 goto fail_alloc_skbs;
1764
1765 spin_lock_init(&card->tx_lock);
1766 card->tx_dma_progress = 0;
02c18891 1767
589866f9
MM
1768 /* setup net_device structure */
1769 netdev->irq = card->irq;
1770 SET_NETDEV_DEV(netdev, &card->dev->core);
1771 gelic_ether_setup_netdev_ops(netdev, &card->napi);
1772 result = gelic_net_setup_netdev(netdev, card);
02c18891 1773 if (result) {
589866f9
MM
1774 dev_dbg(&dev->core, "%s: setup_netdev failed %d",
1775 __func__, result);
02c18891
MM
1776 goto fail_setup_netdev;
1777 }
1778
09dde54c
MM
1779#ifdef CONFIG_GELIC_WIRELESS
1780 if (gelic_wl_driver_probe(card)) {
1781 dev_dbg(&dev->core, "%s: WL init failed\n", __func__);
1782 goto fail_setup_netdev;
1783 }
1784#endif
589866f9 1785 pr_debug("%s: done\n", __func__);
02c18891
MM
1786 return 0;
1787
1788fail_setup_netdev:
589866f9
MM
1789fail_alloc_skbs:
1790 gelic_card_free_chain(card, card->rx_chain.head);
1791fail_alloc_rx:
1792 gelic_card_free_chain(card, card->tx_chain.head);
1793fail_alloc_tx:
1794 free_irq(card->irq, card);
1795 netdev->irq = NO_IRQ;
1796fail_request_irq:
1797 ps3_sb_event_receive_port_destroy(dev, card->irq);
1798fail_alloc_irq:
02c18891 1799 lv1_net_set_interrupt_status_indicator(bus_id(card),
589866f9
MM
1800 bus_id(card),
1801 0, 0);
02c18891 1802fail_status_indicator:
03fa68c2 1803 ps3_system_bus_set_drvdata(dev, NULL);
589866f9
MM
1804 kfree(netdev_card(netdev)->unalign);
1805 free_netdev(netdev);
1806fail_alloc_card:
02c18891
MM
1807 ps3_dma_region_free(dev->d_region);
1808fail_dma_region:
1809 ps3_close_hv_device(dev);
1810fail_open:
02c18891
MM
1811 return result;
1812}
1813
1814/**
1815 * ps3_gelic_driver_remove - remove a device from the control of this driver
1816 */
1817
59e97327 1818static int ps3_gelic_driver_remove(struct ps3_system_bus_device *dev)
02c18891 1819{
03fa68c2 1820 struct gelic_card *card = ps3_system_bus_get_drvdata(dev);
589866f9
MM
1821 struct net_device *netdev0;
1822 pr_debug("%s: called\n", __func__);
1823
55873ed8
HS
1824 /* set auto-negotiation */
1825 gelic_card_set_link_mode(card, GELIC_LV1_ETHER_AUTO_NEG);
1826
09dde54c
MM
1827#ifdef CONFIG_GELIC_WIRELESS
1828 gelic_wl_driver_remove(card);
1829#endif
589866f9
MM
1830 /* stop interrupt */
1831 gelic_card_set_irq_mask(card, 0);
1832
1833 /* turn off DMA, force end */
1834 gelic_card_disable_rxdmac(card);
1835 gelic_card_disable_txdmac(card);
1836
1837 /* release chains */
1838 gelic_card_release_tx_chain(card, 1);
1839 gelic_card_release_rx_chain(card);
1840
1841 gelic_card_free_chain(card, card->tx_top);
1842 gelic_card_free_chain(card, card->rx_top);
1843
d4d7f1f9 1844 netdev0 = card->netdev[GELIC_PORT_ETHERNET_0];
589866f9
MM
1845 /* disconnect event port */
1846 free_irq(card->irq, card);
1847 netdev0->irq = NO_IRQ;
1848 ps3_sb_event_receive_port_destroy(card->dev, card->irq);
02c18891
MM
1849
1850 wait_event(card->waitq,
1851 atomic_read(&card->tx_timeout_task_counter) == 0);
1852
1853 lv1_net_set_interrupt_status_indicator(bus_id(card), dev_id(card),
1854 0 , 0);
1855
589866f9
MM
1856 unregister_netdev(netdev0);
1857 kfree(netdev_card(netdev0)->unalign);
1858 free_netdev(netdev0);
02c18891 1859
03fa68c2 1860 ps3_system_bus_set_drvdata(dev, NULL);
02c18891
MM
1861
1862 ps3_dma_region_free(dev->d_region);
1863
1864 ps3_close_hv_device(dev);
1865
589866f9 1866 pr_debug("%s: done\n", __func__);
02c18891
MM
1867 return 0;
1868}
1869
1870static struct ps3_system_bus_driver ps3_gelic_driver = {
1871 .match_id = PS3_MATCH_ID_GELIC,
1872 .probe = ps3_gelic_driver_probe,
1873 .remove = ps3_gelic_driver_remove,
1874 .shutdown = ps3_gelic_driver_remove,
1875 .core.name = "ps3_gelic_driver",
1876 .core.owner = THIS_MODULE,
1877};
1878
1879static int __init ps3_gelic_driver_init (void)
1880{
1881 return firmware_has_feature(FW_FEATURE_PS3_LV1)
1882 ? ps3_system_bus_driver_register(&ps3_gelic_driver)
1883 : -ENODEV;
1884}
1885
1886static void __exit ps3_gelic_driver_exit (void)
1887{
1888 ps3_system_bus_driver_unregister(&ps3_gelic_driver);
1889}
1890
59e97327
MM
1891module_init(ps3_gelic_driver_init);
1892module_exit(ps3_gelic_driver_exit);
02c18891
MM
1893
1894MODULE_ALIAS(PS3_MODULE_ALIAS_GELIC);
1895