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