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