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	strscpy(info->driver, DRV_NAME, sizeof(info->driver));
1194	strscpy(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 * @txqueue: unused
1407 *
1408 * called, if tx hangs. Schedules a task that resets the interface
1409 */
1410void gelic_net_tx_timeout(struct net_device *netdev, unsigned int txqueue)
1411{
1412	struct gelic_card *card;
1413
1414	card = netdev_card(netdev);
1415	atomic_inc(&card->tx_timeout_task_counter);
1416	if (netdev->flags & IFF_UP)
1417		schedule_work(&card->tx_timeout_task);
1418	else
1419		atomic_dec(&card->tx_timeout_task_counter);
1420}
1421
1422static const struct net_device_ops gelic_netdevice_ops = {
1423	.ndo_open = gelic_net_open,
1424	.ndo_stop = gelic_net_stop,
1425	.ndo_start_xmit = gelic_net_xmit,
1426	.ndo_set_rx_mode = gelic_net_set_multi,
1427	.ndo_tx_timeout = gelic_net_tx_timeout,
1428	.ndo_set_mac_address = eth_mac_addr,
1429	.ndo_validate_addr = eth_validate_addr,
1430#ifdef CONFIG_NET_POLL_CONTROLLER
1431	.ndo_poll_controller = gelic_net_poll_controller,
1432#endif
1433};
1434
1435/**
1436 * gelic_ether_setup_netdev_ops - initialization of net_device operations
1437 * @netdev: net_device structure
1438 * @napi: napi structure
1439 *
1440 * fills out function pointers in the net_device structure
1441 */
1442static void gelic_ether_setup_netdev_ops(struct net_device *netdev,
1443					 struct napi_struct *napi)
1444{
1445	netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;
1446	/* NAPI */
1447	netif_napi_add(netdev, napi, gelic_net_poll);
1448	netdev->ethtool_ops = &gelic_ether_ethtool_ops;
1449	netdev->netdev_ops = &gelic_netdevice_ops;
1450}
1451
1452/**
1453 * gelic_ether_setup_netdev - initialization of net_device
1454 * @netdev: net_device structure
1455 * @card: card structure
1456 *
1457 * Returns 0 on success or <0 on failure
1458 *
1459 * gelic_ether_setup_netdev initializes the net_device structure
1460 * and register it.
1461 **/
1462int gelic_net_setup_netdev(struct net_device *netdev, struct gelic_card *card)
1463{
1464	int status;
1465	u64 v1, v2;
1466
1467	netdev->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1468
1469	netdev->features = NETIF_F_IP_CSUM;
1470	if (GELIC_CARD_RX_CSUM_DEFAULT)
1471		netdev->features |= NETIF_F_RXCSUM;
1472
1473	status = lv1_net_control(bus_id(card), dev_id(card),
1474				 GELIC_LV1_GET_MAC_ADDRESS,
1475				 0, 0, 0, &v1, &v2);
1476	v1 <<= 16;
1477	if (status || !is_valid_ether_addr((u8 *)&v1)) {
1478		dev_info(ctodev(card),
1479			 "%s:lv1_net_control GET_MAC_ADDR failed %d\n",
1480			 __func__, status);
1481		return -EINVAL;
1482	}
1483	eth_hw_addr_set(netdev, (u8 *)&v1);
1484
1485	if (card->vlan_required) {
1486		netdev->hard_header_len += VLAN_HLEN;
1487		/*
1488		 * As vlan is internally used,
1489		 * we can not receive vlan packets
1490		 */
1491		netdev->features |= NETIF_F_VLAN_CHALLENGED;
1492	}
1493
1494	/* MTU range: 64 - 1518 */
1495	netdev->min_mtu = GELIC_NET_MIN_MTU;
1496	netdev->max_mtu = GELIC_NET_MAX_MTU;
1497
1498	status = register_netdev(netdev);
1499	if (status) {
1500		dev_err(ctodev(card), "%s:Couldn't register %s %d\n",
1501			__func__, netdev->name, status);
1502		return status;
1503	}
1504	dev_info(ctodev(card), "%s: MAC addr %pM\n",
1505		 netdev->name, netdev->dev_addr);
1506
1507	return 0;
1508}
1509
1510/**
1511 * gelic_alloc_card_net - allocates net_device and card structure
1512 *
1513 * returns the card structure or NULL in case of errors
1514 *
1515 * the card and net_device structures are linked to each other
1516 */
1517#define GELIC_ALIGN (32)
1518static struct gelic_card *gelic_alloc_card_net(struct net_device **netdev)
1519{
1520	struct gelic_card *card;
1521	struct gelic_port *port;
1522	void *p;
1523	size_t alloc_size;
1524	/*
1525	 * gelic requires dma descriptor is 32 bytes aligned and
1526	 * the hypervisor requires irq_status is 8 bytes aligned.
1527	 */
1528	BUILD_BUG_ON(offsetof(struct gelic_card, irq_status) % 8);
1529	BUILD_BUG_ON(offsetof(struct gelic_card, descr) % 32);
1530	alloc_size =
1531		sizeof(struct gelic_card) +
1532		sizeof(struct gelic_descr) * GELIC_NET_RX_DESCRIPTORS +
1533		sizeof(struct gelic_descr) * GELIC_NET_TX_DESCRIPTORS +
1534		GELIC_ALIGN - 1;
1535
1536	p  = kzalloc(alloc_size, GFP_KERNEL);
1537	if (!p)
1538		return NULL;
1539	card = PTR_ALIGN(p, GELIC_ALIGN);
1540	card->unalign = p;
1541
1542	/*
1543	 * alloc netdev
1544	 */
1545	*netdev = alloc_etherdev(sizeof(struct gelic_port));
1546	if (!*netdev) {
1547		kfree(card->unalign);
1548		return NULL;
1549	}
1550	port = netdev_priv(*netdev);
1551
1552	/* gelic_port */
1553	port->netdev = *netdev;
1554	port->card = card;
1555	port->type = GELIC_PORT_ETHERNET_0;
1556
1557	/* gelic_card */
1558	card->netdev[GELIC_PORT_ETHERNET_0] = *netdev;
1559
1560	INIT_WORK(&card->tx_timeout_task, gelic_net_tx_timeout_task);
1561	init_waitqueue_head(&card->waitq);
1562	atomic_set(&card->tx_timeout_task_counter, 0);
1563	mutex_init(&card->updown_lock);
1564	atomic_set(&card->users, 0);
1565
1566	return card;
1567}
1568
1569static void gelic_card_get_vlan_info(struct gelic_card *card)
1570{
1571	u64 v1, v2;
1572	int status;
1573	unsigned int i;
1574	struct {
1575		int tx;
1576		int rx;
1577	} vlan_id_ix[2] = {
1578		[GELIC_PORT_ETHERNET_0] = {
1579			.tx = GELIC_LV1_VLAN_TX_ETHERNET_0,
1580			.rx = GELIC_LV1_VLAN_RX_ETHERNET_0
1581		},
1582		[GELIC_PORT_WIRELESS] = {
1583			.tx = GELIC_LV1_VLAN_TX_WIRELESS,
1584			.rx = GELIC_LV1_VLAN_RX_WIRELESS
1585		}
1586	};
1587
1588	for (i = 0; i < ARRAY_SIZE(vlan_id_ix); i++) {
1589		/* tx tag */
1590		status = lv1_net_control(bus_id(card), dev_id(card),
1591					 GELIC_LV1_GET_VLAN_ID,
1592					 vlan_id_ix[i].tx,
1593					 0, 0, &v1, &v2);
1594		if (status || !v1) {
1595			if (status != LV1_NO_ENTRY)
1596				dev_dbg(ctodev(card),
1597					"get vlan id for tx(%d) failed(%d)\n",
1598					vlan_id_ix[i].tx, status);
1599			card->vlan[i].tx = 0;
1600			card->vlan[i].rx = 0;
1601			continue;
1602		}
1603		card->vlan[i].tx = (u16)v1;
1604
1605		/* rx tag */
1606		status = lv1_net_control(bus_id(card), dev_id(card),
1607					 GELIC_LV1_GET_VLAN_ID,
1608					 vlan_id_ix[i].rx,
1609					 0, 0, &v1, &v2);
1610		if (status || !v1) {
1611			if (status != LV1_NO_ENTRY)
1612				dev_info(ctodev(card),
1613					 "get vlan id for rx(%d) failed(%d)\n",
1614					 vlan_id_ix[i].rx, status);
1615			card->vlan[i].tx = 0;
1616			card->vlan[i].rx = 0;
1617			continue;
1618		}
1619		card->vlan[i].rx = (u16)v1;
1620
1621		dev_dbg(ctodev(card), "vlan_id[%d] tx=%02x rx=%02x\n",
1622			i, card->vlan[i].tx, card->vlan[i].rx);
1623	}
1624
1625	if (card->vlan[GELIC_PORT_ETHERNET_0].tx) {
1626		BUG_ON(!card->vlan[GELIC_PORT_WIRELESS].tx);
1627		card->vlan_required = 1;
1628	} else
1629		card->vlan_required = 0;
1630
1631	/* check wirelss capable firmware */
1632	if (ps3_compare_firmware_version(1, 6, 0) < 0) {
1633		card->vlan[GELIC_PORT_WIRELESS].tx = 0;
1634		card->vlan[GELIC_PORT_WIRELESS].rx = 0;
1635	}
1636
1637	dev_info(ctodev(card), "internal vlan %s\n",
1638		 card->vlan_required? "enabled" : "disabled");
1639}
1640/*
1641 * ps3_gelic_driver_probe - add a device to the control of this driver
1642 */
1643static int ps3_gelic_driver_probe(struct ps3_system_bus_device *dev)
1644{
1645	struct gelic_card *card;
1646	struct net_device *netdev;
1647	int result;
1648
1649	pr_debug("%s: called\n", __func__);
1650
1651	udbg_shutdown_ps3gelic();
1652
1653	result = ps3_open_hv_device(dev);
1654
1655	if (result) {
1656		dev_dbg(&dev->core, "%s:ps3_open_hv_device failed\n",
1657			__func__);
1658		goto fail_open;
1659	}
1660
1661	result = ps3_dma_region_create(dev->d_region);
1662
1663	if (result) {
1664		dev_dbg(&dev->core, "%s:ps3_dma_region_create failed(%d)\n",
1665			__func__, result);
1666		BUG_ON("check region type");
1667		goto fail_dma_region;
1668	}
1669
1670	/* alloc card/netdevice */
1671	card = gelic_alloc_card_net(&netdev);
1672	if (!card) {
1673		dev_info(&dev->core, "%s:gelic_net_alloc_card failed\n",
1674			 __func__);
1675		result = -ENOMEM;
1676		goto fail_alloc_card;
1677	}
1678	ps3_system_bus_set_drvdata(dev, card);
1679	card->dev = dev;
1680
1681	/* get internal vlan info */
1682	gelic_card_get_vlan_info(card);
1683
1684	card->link_mode = GELIC_LV1_ETHER_AUTO_NEG;
1685
1686	/* setup interrupt */
1687	result = lv1_net_set_interrupt_status_indicator(bus_id(card),
1688							dev_id(card),
1689		ps3_mm_phys_to_lpar(__pa(&card->irq_status)),
1690		0);
1691
1692	if (result) {
1693		dev_dbg(&dev->core,
1694			"%s:set_interrupt_status_indicator failed: %s\n",
1695			__func__, ps3_result(result));
1696		result = -EIO;
1697		goto fail_status_indicator;
1698	}
1699
1700	result = ps3_sb_event_receive_port_setup(dev, PS3_BINDING_CPU_ANY,
1701		&card->irq);
1702
1703	if (result) {
1704		dev_info(ctodev(card),
1705			 "%s:gelic_net_open_device failed (%d)\n",
1706			 __func__, result);
1707		result = -EPERM;
1708		goto fail_alloc_irq;
1709	}
1710	result = request_irq(card->irq, gelic_card_interrupt,
1711			     0, netdev->name, card);
1712
1713	if (result) {
1714		dev_info(ctodev(card), "%s:request_irq failed (%d)\n",
1715			__func__, result);
1716		goto fail_request_irq;
1717	}
1718
1719	/* setup card structure */
1720	card->irq_mask = GELIC_CARD_RXINT | GELIC_CARD_TXINT |
1721		GELIC_CARD_PORT_STATUS_CHANGED;
1722
1723
1724	result = gelic_card_init_chain(card, &card->tx_chain,
1725				       card->descr, GELIC_NET_TX_DESCRIPTORS);
1726	if (result)
1727		goto fail_alloc_tx;
1728	result = gelic_card_init_chain(card, &card->rx_chain,
1729				       card->descr + GELIC_NET_TX_DESCRIPTORS,
1730				       GELIC_NET_RX_DESCRIPTORS);
1731	if (result)
1732		goto fail_alloc_rx;
1733
1734	/* head of chain */
1735	card->tx_top = card->tx_chain.head;
1736	card->rx_top = card->rx_chain.head;
1737	dev_dbg(ctodev(card), "descr rx %p, tx %p, size %#lx, num %#x\n",
1738		card->rx_top, card->tx_top, sizeof(struct gelic_descr),
1739		GELIC_NET_RX_DESCRIPTORS);
1740	/* allocate rx skbs */
1741	result = gelic_card_alloc_rx_skbs(card);
1742	if (result)
1743		goto fail_alloc_skbs;
1744
1745	spin_lock_init(&card->tx_lock);
1746	card->tx_dma_progress = 0;
1747
1748	/* setup net_device structure */
1749	netdev->irq = card->irq;
1750	SET_NETDEV_DEV(netdev, &card->dev->core);
1751	gelic_ether_setup_netdev_ops(netdev, &card->napi);
1752	result = gelic_net_setup_netdev(netdev, card);
1753	if (result) {
1754		dev_dbg(&dev->core, "%s: setup_netdev failed %d\n",
1755			__func__, result);
1756		goto fail_setup_netdev;
1757	}
1758
1759#ifdef CONFIG_GELIC_WIRELESS
1760	result = gelic_wl_driver_probe(card);
1761	if (result) {
1762		dev_dbg(&dev->core, "%s: WL init failed\n", __func__);
1763		goto fail_setup_netdev;
1764	}
1765#endif
1766	pr_debug("%s: done\n", __func__);
1767	return 0;
1768
1769fail_setup_netdev:
1770fail_alloc_skbs:
1771	gelic_card_free_chain(card, card->rx_chain.head);
1772fail_alloc_rx:
1773	gelic_card_free_chain(card, card->tx_chain.head);
1774fail_alloc_tx:
1775	free_irq(card->irq, card);
1776	netdev->irq = 0;
1777fail_request_irq:
1778	ps3_sb_event_receive_port_destroy(dev, card->irq);
1779fail_alloc_irq:
1780	lv1_net_set_interrupt_status_indicator(bus_id(card),
1781					       bus_id(card),
1782					       0, 0);
1783fail_status_indicator:
1784	ps3_system_bus_set_drvdata(dev, NULL);
1785	kfree(netdev_card(netdev)->unalign);
1786	free_netdev(netdev);
1787fail_alloc_card:
1788	ps3_dma_region_free(dev->d_region);
1789fail_dma_region:
1790	ps3_close_hv_device(dev);
1791fail_open:
1792	return result;
1793}
1794
1795/*
1796 * ps3_gelic_driver_remove - remove a device from the control of this driver
1797 */
1798
1799static void ps3_gelic_driver_remove(struct ps3_system_bus_device *dev)
1800{
1801	struct gelic_card *card = ps3_system_bus_get_drvdata(dev);
1802	struct net_device *netdev0;
1803	pr_debug("%s: called\n", __func__);
1804
1805	/* set auto-negotiation */
1806	gelic_card_set_link_mode(card, GELIC_LV1_ETHER_AUTO_NEG);
1807
1808#ifdef CONFIG_GELIC_WIRELESS
1809	gelic_wl_driver_remove(card);
1810#endif
1811	/* stop interrupt */
1812	gelic_card_set_irq_mask(card, 0);
1813
1814	/* turn off DMA, force end */
1815	gelic_card_disable_rxdmac(card);
1816	gelic_card_disable_txdmac(card);
1817
1818	/* release chains */
1819	gelic_card_release_tx_chain(card, 1);
1820	gelic_card_release_rx_chain(card);
1821
1822	gelic_card_free_chain(card, card->tx_top);
1823	gelic_card_free_chain(card, card->rx_top);
1824
1825	netdev0 = card->netdev[GELIC_PORT_ETHERNET_0];
1826	/* disconnect event port */
1827	free_irq(card->irq, card);
1828	netdev0->irq = 0;
1829	ps3_sb_event_receive_port_destroy(card->dev, card->irq);
1830
1831	wait_event(card->waitq,
1832		   atomic_read(&card->tx_timeout_task_counter) == 0);
1833
1834	lv1_net_set_interrupt_status_indicator(bus_id(card), dev_id(card),
1835					       0 , 0);
1836
1837	unregister_netdev(netdev0);
1838	kfree(netdev_card(netdev0)->unalign);
1839	free_netdev(netdev0);
1840
1841	ps3_system_bus_set_drvdata(dev, NULL);
1842
1843	ps3_dma_region_free(dev->d_region);
1844
1845	ps3_close_hv_device(dev);
1846
1847	pr_debug("%s: done\n", __func__);
1848}
1849
1850static struct ps3_system_bus_driver ps3_gelic_driver = {
1851	.match_id = PS3_MATCH_ID_GELIC,
1852	.probe = ps3_gelic_driver_probe,
1853	.remove = ps3_gelic_driver_remove,
1854	.shutdown = ps3_gelic_driver_remove,
1855	.core.name = "ps3_gelic_driver",
1856	.core.owner = THIS_MODULE,
1857};
1858
1859static int __init ps3_gelic_driver_init (void)
1860{
1861	return firmware_has_feature(FW_FEATURE_PS3_LV1)
1862		? ps3_system_bus_driver_register(&ps3_gelic_driver)
1863		: -ENODEV;
1864}
1865
1866static void __exit ps3_gelic_driver_exit (void)
1867{
1868	ps3_system_bus_driver_unregister(&ps3_gelic_driver);
1869}
1870
1871module_init(ps3_gelic_driver_init);
1872module_exit(ps3_gelic_driver_exit);
1873
1874MODULE_ALIAS(PS3_MODULE_ALIAS_GELIC);
1875
1876