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