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