ps3_gelic_net.c 47.0 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 *  PS3 gelic network driver.
 *
 * Copyright (C) 2007 Sony Computer Entertainment Inc.
 * Copyright 2006, 2007 Sony Corporation
 *
 * This file is based on: spider_net.c
 *
 * (C) Copyright IBM Corp. 2005
 *
 * Authors : Utz Bacher <utz.bacher@de.ibm.com>
 *           Jens Osterkamp <Jens.Osterkamp@de.ibm.com>
 */

#undef DEBUG

18
#include <linux/interrupt.h>
19 20
#include <linux/kernel.h>
#include <linux/module.h>
21
#include <linux/slab.h>
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/if_vlan.h>

#include <linux/in.h>
#include <linux/ip.h>
#include <linux/tcp.h>

#include <linux/dma-mapping.h>
#include <net/checksum.h>
#include <asm/firmware.h>
#include <asm/ps3.h>
#include <asm/lv1call.h>

#include "ps3_gelic_net.h"
38
#include "ps3_gelic_wireless.h"
39 40

#define DRV_NAME "Gelic Network Driver"
41
#define DRV_VERSION "2.0"
42 43 44 45 46

MODULE_AUTHOR("SCE Inc.");
MODULE_DESCRIPTION("Gelic Network driver");
MODULE_LICENSE("GPL");

47

48
/* set irq_mask */
49
int gelic_card_set_irq_mask(struct gelic_card *card, u64 mask)
50 51 52 53 54 55 56
{
	int status;

	status = lv1_net_set_interrupt_mask(bus_id(card), dev_id(card),
					    mask, 0);
	if (status)
		dev_info(ctodev(card),
57
			 "%s failed %d\n", __func__, status);
58 59
	return status;
}
60

61
static void gelic_card_rx_irq_on(struct gelic_card *card)
62
{
63 64
	card->irq_mask |= GELIC_CARD_RXINT;
	gelic_card_set_irq_mask(card, card->irq_mask);
65
}
66
static void gelic_card_rx_irq_off(struct gelic_card *card)
67
{
68 69
	card->irq_mask &= ~GELIC_CARD_RXINT;
	gelic_card_set_irq_mask(card, card->irq_mask);
70
}
71

72 73
static void gelic_card_get_ether_port_status(struct gelic_card *card,
					     int inform)
74 75 76 77 78 79
{
	u64 v2;
	struct net_device *ether_netdev;

	lv1_net_control(bus_id(card), dev_id(card),
			GELIC_LV1_GET_ETH_PORT_STATUS,
G
Geoff Levand 已提交
80
			GELIC_LV1_VLAN_TX_ETHERNET_0, 0, 0,
81 82 83
			&card->ether_port_status, &v2);

	if (inform) {
G
Geoff Levand 已提交
84
		ether_netdev = card->netdev[GELIC_PORT_ETHERNET_0];
85 86 87 88 89 90 91
		if (card->ether_port_status & GELIC_LV1_ETHER_LINK_UP)
			netif_carrier_on(ether_netdev);
		else
			netif_carrier_off(ether_netdev);
	}
}

92 93 94 95 96 97 98 99 100 101 102 103
/**
 * gelic_descr_get_status -- returns the status of a descriptor
 * @descr: descriptor to look at
 *
 * returns the status as in the dmac_cmd_status field of the descriptor
 */
static enum gelic_descr_dma_status
gelic_descr_get_status(struct gelic_descr *descr)
{
	return be32_to_cpu(descr->dmac_cmd_status) & GELIC_DESCR_DMA_STAT_MASK;
}

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
static int gelic_card_set_link_mode(struct gelic_card *card, int mode)
{
	int status;
	u64 v1, v2;

	status = lv1_net_control(bus_id(card), dev_id(card),
				 GELIC_LV1_SET_NEGOTIATION_MODE,
				 GELIC_LV1_PHY_ETHERNET_0, mode, 0, &v1, &v2);
	if (status) {
		pr_info("%s: failed setting negotiation mode %d\n", __func__,
			status);
		return -EBUSY;
	}

	card->link_mode = mode;
	return 0;
}

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
/**
 * gelic_card_disable_txdmac - disables the transmit DMA controller
 * @card: card structure
 *
 * gelic_card_disable_txdmac terminates processing on the DMA controller by
 * turing off DMA and issuing a force end
 */
static void gelic_card_disable_txdmac(struct gelic_card *card)
{
	int status;

	/* this hvc blocks until the DMA in progress really stopped */
	status = lv1_net_stop_tx_dma(bus_id(card), dev_id(card));
	if (status)
		dev_err(ctodev(card),
			"lv1_net_stop_tx_dma failed, status=%d\n", status);
}

/**
 * gelic_card_enable_rxdmac - enables the receive DMA controller
 * @card: card structure
 *
 * gelic_card_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
 * in the GDADMACCNTR register
 */
static void gelic_card_enable_rxdmac(struct gelic_card *card)
{
	int status;

#ifdef DEBUG
	if (gelic_descr_get_status(card->rx_chain.head) !=
	    GELIC_DESCR_DMA_CARDOWNED) {
		printk(KERN_ERR "%s: status=%x\n", __func__,
		       be32_to_cpu(card->rx_chain.head->dmac_cmd_status));
		printk(KERN_ERR "%s: nextphy=%x\n", __func__,
		       be32_to_cpu(card->rx_chain.head->next_descr_addr));
		printk(KERN_ERR "%s: head=%p\n", __func__,
		       card->rx_chain.head);
	}
#endif
	status = lv1_net_start_rx_dma(bus_id(card), dev_id(card),
				card->rx_chain.head->bus_addr, 0);
	if (status)
		dev_info(ctodev(card),
			 "lv1_net_start_rx_dma failed, status=%d\n", status);
}

/**
 * gelic_card_disable_rxdmac - disables the receive DMA controller
 * @card: card structure
 *
 * gelic_card_disable_rxdmac terminates processing on the DMA controller by
 * turing off DMA and issuing a force end
 */
static void gelic_card_disable_rxdmac(struct gelic_card *card)
{
	int status;

	/* this hvc blocks until the DMA in progress really stopped */
	status = lv1_net_stop_rx_dma(bus_id(card), dev_id(card));
	if (status)
		dev_err(ctodev(card),
			"lv1_net_stop_rx_dma failed, %d\n", status);
}

/**
 * gelic_descr_set_status -- sets the status of a descriptor
 * @descr: descriptor to change
 * @status: status to set in the descriptor
 *
 * changes the status to the specified value. Doesn't change other bits
 * in the status
 */
static void gelic_descr_set_status(struct gelic_descr *descr,
				   enum gelic_descr_dma_status status)
{
	descr->dmac_cmd_status = cpu_to_be32(status |
			(be32_to_cpu(descr->dmac_cmd_status) &
			 ~GELIC_DESCR_DMA_STAT_MASK));
	/*
	 * dma_cmd_status field is used to indicate whether the descriptor
	 * is valid or not.
	 * Usually caller of this function wants to inform that to the
	 * hardware, so we assure here the hardware sees the change.
	 */
	wmb();
}

/**
 * gelic_card_reset_chain - reset status of a descriptor chain
 * @card: card structure
 * @chain: address of chain
 * @start_descr: address of descriptor array
 *
 * Reset the status of dma descriptors to ready state
 * and re-initialize the hardware chain for later use
 */
static void gelic_card_reset_chain(struct gelic_card *card,
				   struct gelic_descr_chain *chain,
				   struct gelic_descr *start_descr)
{
	struct gelic_descr *descr;

	for (descr = start_descr; start_descr != descr->next; descr++) {
		gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
		descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
	}

	chain->head = start_descr;
	chain->tail = (descr - 1);

	(descr - 1)->next_descr_addr = 0;
}

236 237 238
void gelic_card_up(struct gelic_card *card)
{
	pr_debug("%s: called\n", __func__);
239
	mutex_lock(&card->updown_lock);
240 241 242 243 244 245 246 247 248
	if (atomic_inc_return(&card->users) == 1) {
		pr_debug("%s: real do\n", __func__);
		/* enable irq */
		gelic_card_set_irq_mask(card, card->irq_mask);
		/* start rx */
		gelic_card_enable_rxdmac(card);

		napi_enable(&card->napi);
	}
249
	mutex_unlock(&card->updown_lock);
250 251 252 253 254 255 256
	pr_debug("%s: done\n", __func__);
}

void gelic_card_down(struct gelic_card *card)
{
	u64 mask;
	pr_debug("%s: called\n", __func__);
257
	mutex_lock(&card->updown_lock);
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
	if (atomic_dec_if_positive(&card->users) == 0) {
		pr_debug("%s: real do\n", __func__);
		napi_disable(&card->napi);
		/*
		 * Disable irq. Wireless interrupts will
		 * be disabled later if any
		 */
		mask = card->irq_mask & (GELIC_CARD_WLAN_EVENT_RECEIVED |
					 GELIC_CARD_WLAN_COMMAND_COMPLETED);
		gelic_card_set_irq_mask(card, mask);
		/* stop rx */
		gelic_card_disable_rxdmac(card);
		gelic_card_reset_chain(card, &card->rx_chain,
				       card->descr + GELIC_NET_TX_DESCRIPTORS);
		/* stop tx */
		gelic_card_disable_txdmac(card);
	}
275
	mutex_unlock(&card->updown_lock);
276 277
	pr_debug("%s: done\n", __func__);
}
278

279
/**
M
Masakazu Mokuno 已提交
280
 * gelic_card_free_chain - free descriptor chain
281 282 283
 * @card: card structure
 * @descr_in: address of desc
 */
M
Masakazu Mokuno 已提交
284 285
static void gelic_card_free_chain(struct gelic_card *card,
				  struct gelic_descr *descr_in)
286
{
M
Masakazu Mokuno 已提交
287
	struct gelic_descr *descr;
288 289 290

	for (descr = descr_in; descr && descr->bus_addr; descr = descr->next) {
		dma_unmap_single(ctodev(card), descr->bus_addr,
M
Masakazu Mokuno 已提交
291
				 GELIC_DESCR_SIZE, DMA_BIDIRECTIONAL);
292 293 294 295 296
		descr->bus_addr = 0;
	}
}

/**
M
Masakazu Mokuno 已提交
297
 * gelic_card_init_chain - links descriptor chain
298 299 300 301 302 303 304 305 306 307
 * @card: card structure
 * @chain: address of chain
 * @start_descr: address of descriptor array
 * @no: number of descriptors
 *
 * we manage a circular list that mirrors the hardware structure,
 * except that the hardware uses bus addresses.
 *
 * returns 0 on success, <0 on failure
 */
308
static int gelic_card_init_chain(struct gelic_card *card,
309 310
				 struct gelic_descr_chain *chain,
				 struct gelic_descr *start_descr, int no)
311 312
{
	int i;
M
Masakazu Mokuno 已提交
313
	struct gelic_descr *descr;
314 315 316 317 318 319

	descr = start_descr;
	memset(descr, 0, sizeof(*descr) * no);

	/* set up the hardware pointers in each descriptor */
	for (i = 0; i < no; i++, descr++) {
M
Masakazu Mokuno 已提交
320
		gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
321 322
		descr->bus_addr =
			dma_map_single(ctodev(card), descr,
M
Masakazu Mokuno 已提交
323
				       GELIC_DESCR_SIZE,
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
				       DMA_BIDIRECTIONAL);

		if (!descr->bus_addr)
			goto iommu_error;

		descr->next = descr + 1;
		descr->prev = descr - 1;
	}
	/* make them as ring */
	(descr - 1)->next = start_descr;
	start_descr->prev = (descr - 1);

	/* chain bus addr of hw descriptor */
	descr = start_descr;
	for (i = 0; i < no; i++, descr++) {
339
		descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
340 341 342 343 344 345 346 347 348 349 350 351 352 353
	}

	chain->head = start_descr;
	chain->tail = start_descr;

	/* do not chain last hw descriptor */
	(descr - 1)->next_descr_addr = 0;

	return 0;

iommu_error:
	for (i--, descr--; 0 <= i; i--, descr--)
		if (descr->bus_addr)
			dma_unmap_single(ctodev(card), descr->bus_addr,
M
Masakazu Mokuno 已提交
354
					 GELIC_DESCR_SIZE,
355 356 357 358 359
					 DMA_BIDIRECTIONAL);
	return -ENOMEM;
}

/**
M
Masakazu Mokuno 已提交
360
 * gelic_descr_prepare_rx - reinitializes a rx descriptor
361 362 363
 * @card: card structure
 * @descr: descriptor to re-init
 *
364
 * return 0 on success, <0 on failure
365 366 367 368
 *
 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
 * Activate the descriptor state-wise
 */
M
Masakazu Mokuno 已提交
369
static int gelic_descr_prepare_rx(struct gelic_card *card,
370
				  struct gelic_descr *descr)
371 372 373 374
{
	int offset;
	unsigned int bufsize;

M
Masakazu Mokuno 已提交
375
	if (gelic_descr_get_status(descr) !=  GELIC_DESCR_DMA_NOT_IN_USE)
376
		dev_info(ctodev(card), "%s: ERROR status\n", __func__);
377 378 379 380 381
	/* we need to round up the buffer size to a multiple of 128 */
	bufsize = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN);

	/* and we need to have it 128 byte aligned, therefore we allocate a
	 * bit more */
382
	descr->skb = dev_alloc_skb(bufsize + GELIC_NET_RXBUF_ALIGN - 1);
383 384 385 386 387 388
	if (!descr->skb) {
		descr->buf_addr = 0; /* tell DMAC don't touch memory */
		dev_info(ctodev(card),
			 "%s:allocate skb failed !!\n", __func__);
		return -ENOMEM;
	}
389
	descr->buf_size = cpu_to_be32(bufsize);
390 391 392 393 394 395 396 397 398 399
	descr->dmac_cmd_status = 0;
	descr->result_size = 0;
	descr->valid_size = 0;
	descr->data_error = 0;

	offset = ((unsigned long)descr->skb->data) &
		(GELIC_NET_RXBUF_ALIGN - 1);
	if (offset)
		skb_reserve(descr->skb, GELIC_NET_RXBUF_ALIGN - offset);
	/* io-mmu-map the skb */
400 401 402 403
	descr->buf_addr = cpu_to_be32(dma_map_single(ctodev(card),
						     descr->skb->data,
						     GELIC_NET_MAX_MTU,
						     DMA_FROM_DEVICE));
404 405 406 407 408
	if (!descr->buf_addr) {
		dev_kfree_skb_any(descr->skb);
		descr->skb = NULL;
		dev_info(ctodev(card),
			 "%s:Could not iommu-map rx buffer\n", __func__);
M
Masakazu Mokuno 已提交
409
		gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
410 411
		return -ENOMEM;
	} else {
M
Masakazu Mokuno 已提交
412
		gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
413 414 415 416 417
		return 0;
	}
}

/**
M
Masakazu Mokuno 已提交
418
 * gelic_card_release_rx_chain - free all skb of rx descr
419 420 421
 * @card: card structure
 *
 */
M
Masakazu Mokuno 已提交
422
static void gelic_card_release_rx_chain(struct gelic_card *card)
423
{
M
Masakazu Mokuno 已提交
424
	struct gelic_descr *descr = card->rx_chain.head;
425 426 427 428

	do {
		if (descr->skb) {
			dma_unmap_single(ctodev(card),
429
					 be32_to_cpu(descr->buf_addr),
430 431 432 433 434
					 descr->skb->len,
					 DMA_FROM_DEVICE);
			descr->buf_addr = 0;
			dev_kfree_skb_any(descr->skb);
			descr->skb = NULL;
M
Masakazu Mokuno 已提交
435 436
			gelic_descr_set_status(descr,
					       GELIC_DESCR_DMA_NOT_IN_USE);
437 438 439 440 441 442
		}
		descr = descr->next;
	} while (descr != card->rx_chain.head);
}

/**
M
Masakazu Mokuno 已提交
443
 * gelic_card_fill_rx_chain - fills descriptors/skbs in the rx chains
444 445 446 447
 * @card: card structure
 *
 * fills all descriptors in the rx chain: allocates skbs
 * and iommu-maps them.
M
Masakazu Mokuno 已提交
448
 * returns 0 on success, < 0 on failure
449
 */
M
Masakazu Mokuno 已提交
450
static int gelic_card_fill_rx_chain(struct gelic_card *card)
451
{
M
Masakazu Mokuno 已提交
452
	struct gelic_descr *descr = card->rx_chain.head;
453 454 455 456
	int ret;

	do {
		if (!descr->skb) {
M
Masakazu Mokuno 已提交
457
			ret = gelic_descr_prepare_rx(card, descr);
458 459 460 461 462 463 464 465
			if (ret)
				goto rewind;
		}
		descr = descr->next;
	} while (descr != card->rx_chain.head);

	return 0;
rewind:
M
Masakazu Mokuno 已提交
466
	gelic_card_release_rx_chain(card);
467 468 469 470
	return ret;
}

/**
M
Masakazu Mokuno 已提交
471
 * gelic_card_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
472 473
 * @card: card structure
 *
M
Masakazu Mokuno 已提交
474
 * returns 0 on success, < 0 on failure
475
 */
476
static int gelic_card_alloc_rx_skbs(struct gelic_card *card)
477
{
M
Masakazu Mokuno 已提交
478
	struct gelic_descr_chain *chain;
479 480
	int ret;
	chain = &card->rx_chain;
M
Masakazu Mokuno 已提交
481
	ret = gelic_card_fill_rx_chain(card);
482
	chain->tail = card->rx_top->prev; /* point to the last */
483 484 485 486
	return ret;
}

/**
M
Masakazu Mokuno 已提交
487
 * gelic_descr_release_tx - processes a used tx descriptor
488 489 490 491 492
 * @card: card structure
 * @descr: descriptor to release
 *
 * releases a used tx descriptor (unmapping, freeing of skb)
 */
M
Masakazu Mokuno 已提交
493
static void gelic_descr_release_tx(struct gelic_card *card,
494
				       struct gelic_descr *descr)
495
{
496
	struct sk_buff *skb = descr->skb;
497

498 499 500 501
	BUG_ON(!(be32_to_cpu(descr->data_status) & GELIC_DESCR_TX_TAIL));

	dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr), skb->len,
			 DMA_TO_DEVICE);
502
	dev_kfree_skb_any(skb);
503 504 505 506 507 508 509 510 511 512 513

	descr->buf_addr = 0;
	descr->buf_size = 0;
	descr->next_descr_addr = 0;
	descr->result_size = 0;
	descr->valid_size = 0;
	descr->data_status = 0;
	descr->data_error = 0;
	descr->skb = NULL;

	/* set descr status */
M
Masakazu Mokuno 已提交
514
	gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
515 516
}

517 518
static void gelic_card_stop_queues(struct gelic_card *card)
{
G
Geoff Levand 已提交
519
	netif_stop_queue(card->netdev[GELIC_PORT_ETHERNET_0]);
520 521 522 523 524 525

	if (card->netdev[GELIC_PORT_WIRELESS])
		netif_stop_queue(card->netdev[GELIC_PORT_WIRELESS]);
}
static void gelic_card_wake_queues(struct gelic_card *card)
{
G
Geoff Levand 已提交
526
	netif_wake_queue(card->netdev[GELIC_PORT_ETHERNET_0]);
527 528 529 530

	if (card->netdev[GELIC_PORT_WIRELESS])
		netif_wake_queue(card->netdev[GELIC_PORT_WIRELESS]);
}
531
/**
M
Masakazu Mokuno 已提交
532
 * gelic_card_release_tx_chain - processes sent tx descriptors
533 534 535 536 537
 * @card: adapter structure
 * @stop: net_stop sequence
 *
 * releases the tx descriptors that gelic has finished with
 */
M
Masakazu Mokuno 已提交
538
static void gelic_card_release_tx_chain(struct gelic_card *card, int stop)
539
{
M
Masakazu Mokuno 已提交
540 541
	struct gelic_descr_chain *tx_chain;
	enum gelic_descr_dma_status status;
542
	struct net_device *netdev;
543 544 545 546 547
	int release = 0;

	for (tx_chain = &card->tx_chain;
	     tx_chain->head != tx_chain->tail && tx_chain->tail;
	     tx_chain->tail = tx_chain->tail->next) {
M
Masakazu Mokuno 已提交
548
		status = gelic_descr_get_status(tx_chain->tail);
549
		netdev = tx_chain->tail->skb->dev;
550
		switch (status) {
M
Masakazu Mokuno 已提交
551 552 553
		case GELIC_DESCR_DMA_RESPONSE_ERROR:
		case GELIC_DESCR_DMA_PROTECTION_ERROR:
		case GELIC_DESCR_DMA_FORCE_END:
554 555 556 557 558
			if (printk_ratelimit())
				dev_info(ctodev(card),
					 "%s: forcing end of tx descriptor " \
					 "with status %x\n",
					 __func__, status);
559
			netdev->stats.tx_dropped++;
560 561
			break;

M
Masakazu Mokuno 已提交
562
		case GELIC_DESCR_DMA_COMPLETE:
563
			if (tx_chain->tail->skb) {
564 565
				netdev->stats.tx_packets++;
				netdev->stats.tx_bytes +=
566 567
					tx_chain->tail->skb->len;
			}
568 569
			break;

M
Masakazu Mokuno 已提交
570
		case GELIC_DESCR_DMA_CARDOWNED:
571 572
			/* pending tx request */
		default:
M
Masakazu Mokuno 已提交
573
			/* any other value (== GELIC_DESCR_DMA_NOT_IN_USE) */
574 575
			if (!stop)
				goto out;
576
		}
M
Masakazu Mokuno 已提交
577
		gelic_descr_release_tx(card, tx_chain->tail);
578
		release ++;
579 580
	}
out:
581
	if (!stop && release)
582
		gelic_card_wake_queues(card);
583 584 585 586 587 588 589 590 591 592
}

/**
 * gelic_net_set_multi - sets multicast addresses and promisc flags
 * @netdev: interface device structure
 *
 * gelic_net_set_multi configures multicast addresses as needed for the
 * netdev interface. It also sets up multicast, allmulti and promisc
 * flags appropriately
 */
593
void gelic_net_set_multi(struct net_device *netdev)
594
{
595
	struct gelic_card *card = netdev_card(netdev);
596
	struct netdev_hw_addr *ha;
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
	unsigned int i;
	uint8_t *p;
	u64 addr;
	int status;

	/* clear all multicast address */
	status = lv1_net_remove_multicast_address(bus_id(card), dev_id(card),
						  0, 1);
	if (status)
		dev_err(ctodev(card),
			"lv1_net_remove_multicast_address failed %d\n",
			status);
	/* set broadcast address */
	status = lv1_net_add_multicast_address(bus_id(card), dev_id(card),
					       GELIC_NET_BROADCAST_ADDR, 0);
	if (status)
		dev_err(ctodev(card),
			"lv1_net_add_multicast_address failed, %d\n",
			status);

617
	if ((netdev->flags & IFF_ALLMULTI) ||
618
	    (netdev_mc_count(netdev) > GELIC_NET_MC_COUNT_MAX)) {
619 620 621 622 623 624 625 626 627 628
		status = lv1_net_add_multicast_address(bus_id(card),
						       dev_id(card),
						       0, 1);
		if (status)
			dev_err(ctodev(card),
				"lv1_net_add_multicast_address failed, %d\n",
				status);
		return;
	}

629
	/* set multicast addresses */
630
	netdev_for_each_mc_addr(ha, netdev) {
631
		addr = 0;
632
		p = ha->addr;
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
		for (i = 0; i < ETH_ALEN; i++) {
			addr <<= 8;
			addr |= *p++;
		}
		status = lv1_net_add_multicast_address(bus_id(card),
						       dev_id(card),
						       addr, 0);
		if (status)
			dev_err(ctodev(card),
				"lv1_net_add_multicast_address failed, %d\n",
				status);
	}
}

/**
 * gelic_net_stop - called upon ifconfig down
 * @netdev: interface device structure
 *
 * always returns 0
 */
653
int gelic_net_stop(struct net_device *netdev)
654
{
655
	struct gelic_card *card;
656

657
	pr_debug("%s: start\n", __func__);
658

659
	netif_stop_queue(netdev);
660 661
	netif_carrier_off(netdev);

662 663
	card = netdev_card(netdev);
	gelic_card_down(card);
664

665
	pr_debug("%s: done\n", __func__);
666 667 668 669
	return 0;
}

/**
M
Masakazu Mokuno 已提交
670
 * gelic_card_get_next_tx_descr - returns the next available tx descriptor
671 672 673 674
 * @card: device structure to get descriptor from
 *
 * returns the address of the next descriptor, or NULL if not available.
 */
M
Masakazu Mokuno 已提交
675 676
static struct gelic_descr *
gelic_card_get_next_tx_descr(struct gelic_card *card)
677 678 679
{
	if (!card->tx_chain.head)
		return NULL;
680
	/*  see if the next descriptor is free */
681
	if (card->tx_chain.tail != card->tx_chain.head->next &&
M
Masakazu Mokuno 已提交
682 683
	    gelic_descr_get_status(card->tx_chain.head) ==
	    GELIC_DESCR_DMA_NOT_IN_USE)
684 685 686 687 688 689 690
		return card->tx_chain.head;
	else
		return NULL;

}

/**
691
 * gelic_net_set_txdescr_cmdstat - sets the tx descriptor command field
692 693 694 695 696 697 698
 * @descr: descriptor structure to fill out
 * @skb: packet to consider
 *
 * fills out the command and status field of the descriptor structure,
 * depending on hardware checksum settings. This function assumes a wmb()
 * has executed before.
 */
M
Masakazu Mokuno 已提交
699 700
static void gelic_descr_set_tx_cmdstat(struct gelic_descr *descr,
				       struct sk_buff *skb)
701 702
{
	if (skb->ip_summed != CHECKSUM_PARTIAL)
703
		descr->dmac_cmd_status =
M
Masakazu Mokuno 已提交
704 705
			cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
				    GELIC_DESCR_TX_DMA_FRAME_TAIL);
706 707 708 709 710 711
	else {
		/* is packet ip?
		 * if yes: tcp? udp? */
		if (skb->protocol == htons(ETH_P_IP)) {
			if (ip_hdr(skb)->protocol == IPPROTO_TCP)
				descr->dmac_cmd_status =
M
Masakazu Mokuno 已提交
712 713
				cpu_to_be32(GELIC_DESCR_DMA_CMD_TCP_CHKSUM |
					    GELIC_DESCR_TX_DMA_FRAME_TAIL);
714

715 716
			else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
				descr->dmac_cmd_status =
M
Masakazu Mokuno 已提交
717 718
				cpu_to_be32(GELIC_DESCR_DMA_CMD_UDP_CHKSUM |
					    GELIC_DESCR_TX_DMA_FRAME_TAIL);
719 720 721 722 723
			else	/*
				 * the stack should checksum non-tcp and non-udp
				 * packets on his own: NETIF_F_IP_CSUM
				 */
				descr->dmac_cmd_status =
M
Masakazu Mokuno 已提交
724 725
				cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
					    GELIC_DESCR_TX_DMA_FRAME_TAIL);
726 727 728 729
		}
	}
}

730
static struct sk_buff *gelic_put_vlan_tag(struct sk_buff *skb,
731 732 733 734 735 736 737 738 739 740 741 742 743
						 unsigned short tag)
{
	struct vlan_ethhdr *veth;
	static unsigned int c;

	if (skb_headroom(skb) < VLAN_HLEN) {
		struct sk_buff *sk_tmp = skb;
		pr_debug("%s: hd=%d c=%ud\n", __func__, skb_headroom(skb), c);
		skb = skb_realloc_headroom(sk_tmp, VLAN_HLEN);
		if (!skb)
			return NULL;
		dev_kfree_skb_any(sk_tmp);
	}
744
	veth = skb_push(skb, VLAN_HLEN);
745 746 747 748

	/* Move the mac addresses to the top of buffer */
	memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN);

749
	veth->h_vlan_proto = cpu_to_be16(ETH_P_8021Q);
750 751 752 753 754
	veth->h_vlan_TCI = htons(tag);

	return skb;
}

755
/**
756
 * gelic_descr_prepare_tx - setup a descriptor for sending packets
757 758 759 760 761 762 763
 * @card: card structure
 * @descr: descriptor structure
 * @skb: packet to use
 *
 * returns 0 on success, <0 on failure.
 *
 */
M
Masakazu Mokuno 已提交
764 765 766
static int gelic_descr_prepare_tx(struct gelic_card *card,
				  struct gelic_descr *descr,
				  struct sk_buff *skb)
767
{
768
	dma_addr_t buf;
769

770
	if (card->vlan_required) {
771
		struct sk_buff *skb_tmp;
772 773 774
		enum gelic_port_type type;

		type = netdev_port(skb->dev)->type;
775
		skb_tmp = gelic_put_vlan_tag(skb,
776
					     card->vlan[type].tx);
777 778 779
		if (!skb_tmp)
			return -ENOMEM;
		skb = skb_tmp;
780 781
	}

782
	buf = dma_map_single(ctodev(card), skb->data, skb->len, DMA_TO_DEVICE);
783

784
	if (!buf) {
785 786
		dev_err(ctodev(card),
			"dma map 2 failed (%p, %i). Dropping packet\n",
787
			skb->data, skb->len);
788 789 790
		return -ENOMEM;
	}

791 792
	descr->buf_addr = cpu_to_be32(buf);
	descr->buf_size = cpu_to_be32(skb->len);
793
	descr->skb = skb;
794
	descr->data_status = 0;
795
	descr->next_descr_addr = 0; /* terminate hw descr */
M
Masakazu Mokuno 已提交
796
	gelic_descr_set_tx_cmdstat(descr, skb);
797 798

	/* bump free descriptor pointer */
799
	card->tx_chain.head = descr->next;
800 801 802 803
	return 0;
}

/**
M
Masakazu Mokuno 已提交
804
 * gelic_card_kick_txdma - enables TX DMA processing
805 806 807 808
 * @card: card structure
 * @descr: descriptor address to enable TX processing at
 *
 */
M
Masakazu Mokuno 已提交
809 810
static int gelic_card_kick_txdma(struct gelic_card *card,
				 struct gelic_descr *descr)
811
{
812
	int status = 0;
813 814 815 816

	if (card->tx_dma_progress)
		return 0;

M
Masakazu Mokuno 已提交
817
	if (gelic_descr_get_status(descr) == GELIC_DESCR_DMA_CARDOWNED) {
818
		card->tx_dma_progress = 1;
819 820
		status = lv1_net_start_tx_dma(bus_id(card), dev_id(card),
					      descr->bus_addr, 0);
821 822
		if (status) {
			card->tx_dma_progress = 0;
823
			dev_info(ctodev(card), "lv1_net_start_txdma failed," \
824
				 "status=%d\n", status);
825
		}
826 827 828 829 830 831 832 833 834
	}
	return status;
}

/**
 * gelic_net_xmit - transmits a frame over the device
 * @skb: packet to send out
 * @netdev: interface device structure
 *
835
 * returns NETDEV_TX_OK on success, NETDEV_TX_BUSY on failure
836
 */
837
netdev_tx_t gelic_net_xmit(struct sk_buff *skb, struct net_device *netdev)
838
{
839
	struct gelic_card *card = netdev_card(netdev);
M
Masakazu Mokuno 已提交
840
	struct gelic_descr *descr;
841 842 843
	int result;
	unsigned long flags;

844
	spin_lock_irqsave(&card->tx_lock, flags);
845

M
Masakazu Mokuno 已提交
846
	gelic_card_release_tx_chain(card, 0);
847

M
Masakazu Mokuno 已提交
848
	descr = gelic_card_get_next_tx_descr(card);
849
	if (!descr) {
850 851 852
		/*
		 * no more descriptors free
		 */
853 854
		gelic_card_stop_queues(card);
		spin_unlock_irqrestore(&card->tx_lock, flags);
855 856 857
		return NETDEV_TX_BUSY;
	}

M
Masakazu Mokuno 已提交
858
	result = gelic_descr_prepare_tx(card, descr, skb);
859 860
	if (result) {
		/*
A
Andre Heider 已提交
861
		 * DMA map failed.  As chances are that failure
862 863
		 * would continue, just release skb and return
		 */
864
		netdev->stats.tx_dropped++;
865
		dev_kfree_skb_any(skb);
866
		spin_unlock_irqrestore(&card->tx_lock, flags);
867 868 869 870 871 872
		return NETDEV_TX_OK;
	}
	/*
	 * link this prepared descriptor to previous one
	 * to achieve high performance
	 */
873
	descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
874 875 876 877 878
	/*
	 * as hardware descriptor is modified in the above lines,
	 * ensure that the hardware sees it
	 */
	wmb();
M
Masakazu Mokuno 已提交
879
	if (gelic_card_kick_txdma(card, descr)) {
880 881
		/*
		 * kick failed.
882
		 * release descriptor which was just prepared
883
		 */
884
		netdev->stats.tx_dropped++;
885 886
		/* don't trigger BUG_ON() in gelic_descr_release_tx */
		descr->data_status = cpu_to_be32(GELIC_DESCR_TX_TAIL);
M
Masakazu Mokuno 已提交
887
		gelic_descr_release_tx(card, descr);
888 889 890 891
		/* reset head */
		card->tx_chain.head = descr;
		/* reset hw termination */
		descr->prev->next_descr_addr = 0;
892 893
		dev_info(ctodev(card), "%s: kick failure\n", __func__);
	}
894

895
	spin_unlock_irqrestore(&card->tx_lock, flags);
896 897 898 899 900 901 902
	return NETDEV_TX_OK;
}

/**
 * gelic_net_pass_skb_up - takes an skb from a descriptor and passes it on
 * @descr: descriptor to process
 * @card: card structure
903
 * @netdev: net_device structure to be passed packet
904 905 906 907
 *
 * iommu-unmaps the skb, fills out skb structure and passes the data to the
 * stack. The descriptor state is not changed.
 */
M
Masakazu Mokuno 已提交
908
static void gelic_net_pass_skb_up(struct gelic_descr *descr,
909 910 911
				  struct gelic_card *card,
				  struct net_device *netdev)

912
{
913
	struct sk_buff *skb = descr->skb;
914 915
	u32 data_status, data_error;

916 917
	data_status = be32_to_cpu(descr->data_status);
	data_error = be32_to_cpu(descr->data_error);
918
	/* unmap skb buffer */
919 920
	dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr),
			 GELIC_NET_MAX_MTU,
921 922
			 DMA_FROM_DEVICE);

923
	skb_put(skb, be32_to_cpu(descr->valid_size)?
924 925
		be32_to_cpu(descr->valid_size) :
		be32_to_cpu(descr->result_size));
926 927
	if (!descr->valid_size)
		dev_info(ctodev(card), "buffer full %x %x %x\n",
928 929 930
			 be32_to_cpu(descr->result_size),
			 be32_to_cpu(descr->buf_size),
			 be32_to_cpu(descr->dmac_cmd_status));
931 932 933 934 935 936 937 938 939 940

	descr->skb = NULL;
	/*
	 * the card put 2 bytes vlan tag in front
	 * of the ethernet frame
	 */
	skb_pull(skb, 2);
	skb->protocol = eth_type_trans(skb, netdev);

	/* checksum offload */
941
	if (netdev->features & NETIF_F_RXCSUM) {
M
Masakazu Mokuno 已提交
942 943
		if ((data_status & GELIC_DESCR_DATA_STATUS_CHK_MASK) &&
		    (!(data_error & GELIC_DESCR_DATA_ERROR_CHK_MASK)))
944 945
			skb->ip_summed = CHECKSUM_UNNECESSARY;
		else
946
			skb_checksum_none_assert(skb);
947
	} else
948
		skb_checksum_none_assert(skb);
949 950

	/* update netdevice statistics */
951 952
	netdev->stats.rx_packets++;
	netdev->stats.rx_bytes += skb->len;
953 954 955 956 957 958

	/* pass skb up to stack */
	netif_receive_skb(skb);
}

/**
M
Masakazu Mokuno 已提交
959
 * gelic_card_decode_one_descr - processes an rx descriptor
960 961 962 963 964 965 966
 * @card: card structure
 *
 * returns 1 if a packet has been sent to the stack, otherwise 0
 *
 * processes an rx descriptor by iommu-unmapping the data buffer and passing
 * the packet up to the stack
 */
M
Masakazu Mokuno 已提交
967
static int gelic_card_decode_one_descr(struct gelic_card *card)
968
{
M
Masakazu Mokuno 已提交
969 970
	enum gelic_descr_dma_status status;
	struct gelic_descr_chain *chain = &card->rx_chain;
971 972
	struct gelic_descr *descr = chain->head;
	struct net_device *netdev = NULL;
973 974
	int dmac_chain_ended;

M
Masakazu Mokuno 已提交
975
	status = gelic_descr_get_status(descr);
976

M
Masakazu Mokuno 已提交
977
	if (status == GELIC_DESCR_DMA_CARDOWNED)
978 979
		return 0;

M
Masakazu Mokuno 已提交
980
	if (status == GELIC_DESCR_DMA_NOT_IN_USE) {
981 982 983 984
		dev_dbg(ctodev(card), "dormant descr? %p\n", descr);
		return 0;
	}

985 986 987 988 989 990 991 992 993 994
	/* netdevice select */
	if (card->vlan_required) {
		unsigned int i;
		u16 vid;
		vid = *(u16 *)(descr->skb->data) & VLAN_VID_MASK;
		for (i = 0; i < GELIC_PORT_MAX; i++) {
			if (card->vlan[i].rx == vid) {
				netdev = card->netdev[i];
				break;
			}
995
		}
996 997 998 999 1000
		if (GELIC_PORT_MAX <= i) {
			pr_info("%s: unknown packet vid=%x\n", __func__, vid);
			goto refill;
		}
	} else
G
Geoff Levand 已提交
1001
		netdev = card->netdev[GELIC_PORT_ETHERNET_0];
1002

M
Masakazu Mokuno 已提交
1003 1004 1005
	if ((status == GELIC_DESCR_DMA_RESPONSE_ERROR) ||
	    (status == GELIC_DESCR_DMA_PROTECTION_ERROR) ||
	    (status == GELIC_DESCR_DMA_FORCE_END)) {
1006 1007
		dev_info(ctodev(card), "dropping RX descriptor with state %x\n",
			 status);
1008
		netdev->stats.rx_dropped++;
1009 1010 1011
		goto refill;
	}

M
Masakazu Mokuno 已提交
1012
	if (status == GELIC_DESCR_DMA_BUFFER_FULL) {
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
		/*
		 * Buffer full would occur if and only if
		 * the frame length was longer than the size of this
		 * descriptor's buffer.  If the frame length was equal
		 * to or shorter than buffer'size, FRAME_END condition
		 * would occur.
		 * Anyway this frame was longer than the MTU,
		 * just drop it.
		 */
		dev_info(ctodev(card), "overlength frame\n");
		goto refill;
	}
	/*
A
Andre Heider 已提交
1026
	 * descriptors any other than FRAME_END here should
1027 1028
	 * be treated as error.
	 */
M
Masakazu Mokuno 已提交
1029
	if (status != GELIC_DESCR_DMA_FRAME_END) {
1030 1031 1032 1033 1034 1035
		dev_dbg(ctodev(card), "RX descriptor with state %x\n",
			status);
		goto refill;
	}

	/* ok, we've got a packet in descr */
1036
	gelic_net_pass_skb_up(descr, card, netdev);
1037
refill:
G
Geoff Levand 已提交
1038 1039 1040 1041 1042

	/* is the current descriptor terminated with next_descr == NULL? */
	dmac_chain_ended =
		be32_to_cpu(descr->dmac_cmd_status) &
		GELIC_DESCR_RX_DMA_CHAIN_END;
1043 1044 1045 1046 1047 1048
	/*
	 * So that always DMAC can see the end
	 * of the descriptor chain to avoid
	 * from unwanted DMAC overrun.
	 */
	descr->next_descr_addr = 0;
1049 1050

	/* change the descriptor state: */
M
Masakazu Mokuno 已提交
1051
	gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
1052

1053 1054
	/*
	 * this call can fail, but for now, just leave this
1055
	 * descriptor without skb
1056
	 */
M
Masakazu Mokuno 已提交
1057
	gelic_descr_prepare_rx(card, descr);
1058

1059 1060
	chain->tail = descr;
	chain->head = descr->next;
1061 1062 1063 1064

	/*
	 * Set this descriptor the end of the chain.
	 */
1065
	descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
1066

1067 1068 1069 1070
	/*
	 * If dmac chain was met, DMAC stopped.
	 * thus re-enable it
	 */
G
Geoff Levand 已提交
1071 1072 1073

	if (dmac_chain_ended)
		gelic_card_enable_rxdmac(card);
1074 1075 1076 1077 1078 1079

	return 1;
}

/**
 * gelic_net_poll - NAPI poll function called by the stack to return packets
1080
 * @napi: napi structure
1081 1082
 * @budget: number of packets we can pass to the stack at most
 *
1083
 * returns the number of the processed packets
1084 1085
 *
 */
1086
static int gelic_net_poll(struct napi_struct *napi, int budget)
1087
{
M
Masakazu Mokuno 已提交
1088
	struct gelic_card *card = container_of(napi, struct gelic_card, napi);
1089
	int packets_done = 0;
1090

1091
	while (packets_done < budget) {
M
Masakazu Mokuno 已提交
1092
		if (!gelic_card_decode_one_descr(card))
1093
			break;
1094 1095

		packets_done++;
1096
	}
1097 1098

	if (packets_done < budget) {
1099
		napi_complete_done(napi, packets_done);
M
Masakazu Mokuno 已提交
1100
		gelic_card_rx_irq_on(card);
1101 1102
	}
	return packets_done;
1103 1104 1105
}

/**
M
Masakazu Mokuno 已提交
1106
 * gelic_card_interrupt - event handler for gelic_net
1107
 */
M
Masakazu Mokuno 已提交
1108
static irqreturn_t gelic_card_interrupt(int irq, void *ptr)
1109 1110
{
	unsigned long flags;
1111
	struct gelic_card *card = ptr;
1112 1113 1114 1115 1116 1117 1118
	u64 status;

	status = card->irq_status;

	if (!status)
		return IRQ_NONE;

1119 1120
	status &= card->irq_mask;

M
Masakazu Mokuno 已提交
1121 1122
	if (status & GELIC_CARD_RXINT) {
		gelic_card_rx_irq_off(card);
1123
		napi_schedule(&card->napi);
1124 1125
	}

M
Masakazu Mokuno 已提交
1126
	if (status & GELIC_CARD_TXINT) {
1127
		spin_lock_irqsave(&card->tx_lock, flags);
1128
		card->tx_dma_progress = 0;
M
Masakazu Mokuno 已提交
1129
		gelic_card_release_tx_chain(card, 0);
1130
		/* kick outstanding tx descriptor if any */
M
Masakazu Mokuno 已提交
1131
		gelic_card_kick_txdma(card, card->tx_chain.tail);
1132
		spin_unlock_irqrestore(&card->tx_lock, flags);
1133
	}
1134 1135 1136 1137

	/* ether port status changed */
	if (status & GELIC_CARD_PORT_STATUS_CHANGED)
		gelic_card_get_ether_port_status(card, 1);
1138

1139 1140 1141 1142 1143 1144
#ifdef CONFIG_GELIC_WIRELESS
	if (status & (GELIC_CARD_WLAN_EVENT_RECEIVED |
		      GELIC_CARD_WLAN_COMMAND_COMPLETED))
		gelic_wl_interrupt(card->netdev[GELIC_PORT_WIRELESS], status);
#endif

1145 1146 1147 1148 1149 1150 1151 1152
	return IRQ_HANDLED;
}

#ifdef CONFIG_NET_POLL_CONTROLLER
/**
 * gelic_net_poll_controller - artificial interrupt for netconsole etc.
 * @netdev: interface device structure
 *
1153
 * see Documentation/networking/netconsole.rst
1154
 */
1155
void gelic_net_poll_controller(struct net_device *netdev)
1156
{
1157
	struct gelic_card *card = netdev_card(netdev);
1158

M
Masakazu Mokuno 已提交
1159 1160
	gelic_card_set_irq_mask(card, 0);
	gelic_card_interrupt(netdev->irq, netdev);
1161
	gelic_card_set_irq_mask(card, card->irq_mask);
1162 1163 1164 1165
}
#endif /* CONFIG_NET_POLL_CONTROLLER */

/**
A
Andre Heider 已提交
1166
 * gelic_net_open - called upon ifconfig up
1167 1168 1169 1170 1171 1172 1173
 * @netdev: interface device structure
 *
 * returns 0 on success, <0 on failure
 *
 * gelic_net_open allocates all the descriptors and memory needed for
 * operation, sets up multicast list and enables interrupts
 */
1174
int gelic_net_open(struct net_device *netdev)
1175
{
1176
	struct gelic_card *card = netdev_card(netdev);
1177

1178
	dev_dbg(ctodev(card), " -> %s %p\n", __func__, netdev);
1179

1180
	gelic_card_up(card);
1181 1182

	netif_start_queue(netdev);
1183
	gelic_card_get_ether_port_status(card, 1);
1184

1185
	dev_dbg(ctodev(card), " <- %s\n", __func__);
1186 1187 1188
	return 0;
}

1189 1190
void gelic_net_get_drvinfo(struct net_device *netdev,
			   struct ethtool_drvinfo *info)
1191
{
1192 1193
	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
1194 1195
}

1196 1197
static int gelic_ether_get_link_ksettings(struct net_device *netdev,
					  struct ethtool_link_ksettings *cmd)
1198
{
1199
	struct gelic_card *card = netdev_card(netdev);
1200
	u32 supported, advertising;
1201

1202 1203 1204
	gelic_card_get_ether_port_status(card, 0);

	if (card->ether_port_status & GELIC_LV1_ETHER_FULL_DUPLEX)
1205
		cmd->base.duplex = DUPLEX_FULL;
1206
	else
1207
		cmd->base.duplex = DUPLEX_HALF;
1208 1209 1210

	switch (card->ether_port_status & GELIC_LV1_ETHER_SPEED_MASK) {
	case GELIC_LV1_ETHER_SPEED_10:
1211
		cmd->base.speed = SPEED_10;
1212 1213
		break;
	case GELIC_LV1_ETHER_SPEED_100:
1214
		cmd->base.speed = SPEED_100;
1215 1216
		break;
	case GELIC_LV1_ETHER_SPEED_1000:
1217
		cmd->base.speed = SPEED_1000;
1218 1219 1220
		break;
	default:
		pr_info("%s: speed unknown\n", __func__);
1221
		cmd->base.speed = SPEED_10;
1222
		break;
1223
	}
1224

1225
	supported = SUPPORTED_TP | SUPPORTED_Autoneg |
1226 1227
			SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
			SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
1228
			SUPPORTED_1000baseT_Full;
1229
	advertising = supported;
1230
	if (card->link_mode & GELIC_LV1_ETHER_AUTO_NEG) {
1231
		cmd->base.autoneg = AUTONEG_ENABLE;
1232
	} else {
1233 1234
		cmd->base.autoneg = AUTONEG_DISABLE;
		advertising &= ~ADVERTISED_Autoneg;
1235
	}
1236 1237 1238 1239 1240 1241
	cmd->base.port = PORT_TP;

	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
						supported);
	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
						advertising);
1242 1243 1244 1245

	return 0;
}

1246 1247 1248
static int
gelic_ether_set_link_ksettings(struct net_device *netdev,
			       const struct ethtool_link_ksettings *cmd)
1249 1250 1251 1252 1253
{
	struct gelic_card *card = netdev_card(netdev);
	u64 mode;
	int ret;

1254
	if (cmd->base.autoneg == AUTONEG_ENABLE) {
1255 1256
		mode = GELIC_LV1_ETHER_AUTO_NEG;
	} else {
1257
		switch (cmd->base.speed) {
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
		case SPEED_10:
			mode = GELIC_LV1_ETHER_SPEED_10;
			break;
		case SPEED_100:
			mode = GELIC_LV1_ETHER_SPEED_100;
			break;
		case SPEED_1000:
			mode = GELIC_LV1_ETHER_SPEED_1000;
			break;
		default:
			return -EINVAL;
		}
1270
		if (cmd->base.duplex == DUPLEX_FULL) {
1271
			mode |= GELIC_LV1_ETHER_FULL_DUPLEX;
1272
		} else if (cmd->base.speed == SPEED_1000) {
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
			pr_info("1000 half duplex is not supported.\n");
			return -EINVAL;
		}
	}

	ret = gelic_card_set_link_mode(card, mode);

	if (ret)
		return ret;

	return 0;
}

1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
static void gelic_net_get_wol(struct net_device *netdev,
			      struct ethtool_wolinfo *wol)
{
	if (0 <= ps3_compare_firmware_version(2, 2, 0))
		wol->supported = WAKE_MAGIC;
	else
		wol->supported = 0;

	wol->wolopts = ps3_sys_manager_get_wol() ? wol->supported : 0;
	memset(&wol->sopass, 0, sizeof(wol->sopass));
}
static int gelic_net_set_wol(struct net_device *netdev,
			     struct ethtool_wolinfo *wol)
{
	int status;
	struct gelic_card *card;
	u64 v1, v2;

	if (ps3_compare_firmware_version(2, 2, 0) < 0 ||
	    !capable(CAP_NET_ADMIN))
		return -EPERM;

	if (wol->wolopts & ~WAKE_MAGIC)
		return -EINVAL;

	card = netdev_card(netdev);
	if (wol->wolopts & WAKE_MAGIC) {
		status = lv1_net_control(bus_id(card), dev_id(card),
					 GELIC_LV1_SET_WOL,
					 GELIC_LV1_WOL_MAGIC_PACKET,
					 0, GELIC_LV1_WOL_MP_ENABLE,
					 &v1, &v2);
		if (status) {
			pr_info("%s: enabling WOL failed %d\n", __func__,
				status);
			status = -EIO;
			goto done;
		}
		status = lv1_net_control(bus_id(card), dev_id(card),
					 GELIC_LV1_SET_WOL,
					 GELIC_LV1_WOL_ADD_MATCH_ADDR,
					 0, GELIC_LV1_WOL_MATCH_ALL,
					 &v1, &v2);
		if (!status)
			ps3_sys_manager_set_wol(1);
		else {
			pr_info("%s: enabling WOL filter failed %d\n",
				__func__, status);
			status = -EIO;
		}
	} else {
		status = lv1_net_control(bus_id(card), dev_id(card),
					 GELIC_LV1_SET_WOL,
					 GELIC_LV1_WOL_MAGIC_PACKET,
					 0, GELIC_LV1_WOL_MP_DISABLE,
					 &v1, &v2);
		if (status) {
			pr_info("%s: disabling WOL failed %d\n", __func__,
				status);
			status = -EIO;
			goto done;
		}
		status = lv1_net_control(bus_id(card), dev_id(card),
					 GELIC_LV1_SET_WOL,
					 GELIC_LV1_WOL_DELETE_MATCH_ADDR,
					 0, GELIC_LV1_WOL_MATCH_ALL,
					 &v1, &v2);
		if (!status)
			ps3_sys_manager_set_wol(0);
		else {
			pr_info("%s: removing WOL filter failed %d\n",
				__func__, status);
			status = -EIO;
		}
	}
done:
	return status;
}

1365
static const struct ethtool_ops gelic_ether_ethtool_ops = {
1366
	.get_drvinfo	= gelic_net_get_drvinfo,
1367
	.get_link	= ethtool_op_get_link,
1368 1369
	.get_wol	= gelic_net_get_wol,
	.set_wol	= gelic_net_set_wol,
1370 1371
	.get_link_ksettings = gelic_ether_get_link_ksettings,
	.set_link_ksettings = gelic_ether_set_link_ksettings,
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
};

/**
 * gelic_net_tx_timeout_task - task scheduled by the watchdog timeout
 * function (to be called not under interrupt status)
 * @work: work is context of tx timout task
 *
 * called as task when tx hangs, resets interface (if interface is up)
 */
static void gelic_net_tx_timeout_task(struct work_struct *work)
{
M
Masakazu Mokuno 已提交
1383 1384
	struct gelic_card *card =
		container_of(work, struct gelic_card, tx_timeout_task);
G
Geoff Levand 已提交
1385
	struct net_device *netdev = card->netdev[GELIC_PORT_ETHERNET_0];
1386

1387
	dev_info(ctodev(card), "%s:Timed out. Restarting...\n", __func__);
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407

	if (!(netdev->flags & IFF_UP))
		goto out;

	netif_device_detach(netdev);
	gelic_net_stop(netdev);

	gelic_net_open(netdev);
	netif_device_attach(netdev);

out:
	atomic_dec(&card->tx_timeout_task_counter);
}

/**
 * gelic_net_tx_timeout - called when the tx timeout watchdog kicks in.
 * @netdev: interface device structure
 *
 * called, if tx hangs. Schedules a task that resets the interface
 */
1408
void gelic_net_tx_timeout(struct net_device *netdev, unsigned int txqueue)
1409
{
M
Masakazu Mokuno 已提交
1410
	struct gelic_card *card;
1411

1412
	card = netdev_card(netdev);
1413 1414 1415 1416 1417 1418 1419
	atomic_inc(&card->tx_timeout_task_counter);
	if (netdev->flags & IFF_UP)
		schedule_work(&card->tx_timeout_task);
	else
		atomic_dec(&card->tx_timeout_task_counter);
}

1420 1421 1422 1423
static const struct net_device_ops gelic_netdevice_ops = {
	.ndo_open = gelic_net_open,
	.ndo_stop = gelic_net_stop,
	.ndo_start_xmit = gelic_net_xmit,
1424
	.ndo_set_rx_mode = gelic_net_set_multi,
1425
	.ndo_tx_timeout = gelic_net_tx_timeout,
1426
	.ndo_set_mac_address = eth_mac_addr,
1427 1428 1429 1430 1431 1432
	.ndo_validate_addr = eth_validate_addr,
#ifdef CONFIG_NET_POLL_CONTROLLER
	.ndo_poll_controller = gelic_net_poll_controller,
#endif
};

1433
/**
M
Masakazu Mokuno 已提交
1434
 * gelic_ether_setup_netdev_ops - initialization of net_device operations
1435 1436 1437 1438
 * @netdev: net_device structure
 *
 * fills out function pointers in the net_device structure
 */
1439
static void gelic_ether_setup_netdev_ops(struct net_device *netdev,
1440
					 struct napi_struct *napi)
1441 1442
{
	netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;
1443
	/* NAPI */
M
Michal Schmidt 已提交
1444
	netif_napi_add(netdev, napi, gelic_net_poll, NAPI_POLL_WEIGHT);
1445
	netdev->ethtool_ops = &gelic_ether_ethtool_ops;
1446
	netdev->netdev_ops = &gelic_netdevice_ops;
1447 1448 1449
}

/**
1450 1451
 * gelic_ether_setup_netdev - initialization of net_device
 * @netdev: net_device structure
1452 1453 1454 1455
 * @card: card structure
 *
 * Returns 0 on success or <0 on failure
 *
1456 1457
 * gelic_ether_setup_netdev initializes the net_device structure
 * and register it.
1458
 **/
1459
int gelic_net_setup_netdev(struct net_device *netdev, struct gelic_card *card)
1460 1461 1462 1463
{
	int status;
	u64 v1, v2;

1464 1465
	netdev->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM;

1466
	netdev->features = NETIF_F_IP_CSUM;
1467 1468
	if (GELIC_CARD_RX_CSUM_DEFAULT)
		netdev->features |= NETIF_F_RXCSUM;
1469 1470

	status = lv1_net_control(bus_id(card), dev_id(card),
M
Masakazu Mokuno 已提交
1471
				 GELIC_LV1_GET_MAC_ADDRESS,
1472
				 0, 0, 0, &v1, &v2);
1473
	v1 <<= 16;
1474 1475 1476 1477 1478 1479
	if (status || !is_valid_ether_addr((u8 *)&v1)) {
		dev_info(ctodev(card),
			 "%s:lv1_net_control GET_MAC_ADDR failed %d\n",
			 __func__, status);
		return -EINVAL;
	}
1480
	memcpy(netdev->dev_addr, &v1, ETH_ALEN);
1481

1482
	if (card->vlan_required) {
1483
		netdev->hard_header_len += VLAN_HLEN;
1484 1485 1486 1487 1488
		/*
		 * As vlan is internally used,
		 * we can not receive vlan packets
		 */
		netdev->features |= NETIF_F_VLAN_CHALLENGED;
1489
	}
1490

1491 1492 1493 1494
	/* MTU range: 64 - 1518 */
	netdev->min_mtu = GELIC_NET_MIN_MTU;
	netdev->max_mtu = GELIC_NET_MAX_MTU;

1495 1496
	status = register_netdev(netdev);
	if (status) {
1497 1498
		dev_err(ctodev(card), "%s:Couldn't register %s %d\n",
			__func__, netdev->name, status);
1499 1500
		return status;
	}
J
Johannes Berg 已提交
1501 1502
	dev_info(ctodev(card), "%s: MAC addr %pM\n",
		 netdev->name, netdev->dev_addr);
1503 1504 1505 1506 1507

	return 0;
}

/**
M
Masakazu Mokuno 已提交
1508
 * gelic_alloc_card_net - allocates net_device and card structure
1509 1510 1511 1512 1513
 *
 * returns the card structure or NULL in case of errors
 *
 * the card and net_device structures are linked to each other
 */
1514
#define GELIC_ALIGN (32)
1515
static struct gelic_card *gelic_alloc_card_net(struct net_device **netdev)
1516
{
M
Masakazu Mokuno 已提交
1517
	struct gelic_card *card;
1518 1519
	struct gelic_port *port;
	void *p;
1520 1521
	size_t alloc_size;
	/*
1522 1523
	 * gelic requires dma descriptor is 32 bytes aligned and
	 * the hypervisor requires irq_status is 8 bytes aligned.
1524
	 */
M
Masakazu Mokuno 已提交
1525 1526
	BUILD_BUG_ON(offsetof(struct gelic_card, irq_status) % 8);
	BUILD_BUG_ON(offsetof(struct gelic_card, descr) % 32);
1527 1528 1529 1530 1531
	alloc_size =
		sizeof(struct gelic_card) +
		sizeof(struct gelic_descr) * GELIC_NET_RX_DESCRIPTORS +
		sizeof(struct gelic_descr) * GELIC_NET_TX_DESCRIPTORS +
		GELIC_ALIGN - 1;
1532

1533 1534
	p  = kzalloc(alloc_size, GFP_KERNEL);
	if (!p)
1535
		return NULL;
1536 1537 1538 1539 1540 1541 1542
	card = PTR_ALIGN(p, GELIC_ALIGN);
	card->unalign = p;

	/*
	 * alloc netdev
	 */
	*netdev = alloc_etherdev(sizeof(struct gelic_port));
1543
	if (!*netdev) {
1544 1545 1546 1547 1548 1549 1550 1551
		kfree(card->unalign);
		return NULL;
	}
	port = netdev_priv(*netdev);

	/* gelic_port */
	port->netdev = *netdev;
	port->card = card;
G
Geoff Levand 已提交
1552
	port->type = GELIC_PORT_ETHERNET_0;
1553 1554

	/* gelic_card */
G
Geoff Levand 已提交
1555
	card->netdev[GELIC_PORT_ETHERNET_0] = *netdev;
1556 1557 1558 1559

	INIT_WORK(&card->tx_timeout_task, gelic_net_tx_timeout_task);
	init_waitqueue_head(&card->waitq);
	atomic_set(&card->tx_timeout_task_counter, 0);
1560
	mutex_init(&card->updown_lock);
1561
	atomic_set(&card->users, 0);
1562 1563 1564 1565

	return card;
}

1566
static void gelic_card_get_vlan_info(struct gelic_card *card)
1567 1568 1569 1570 1571 1572 1573 1574
{
	u64 v1, v2;
	int status;
	unsigned int i;
	struct {
		int tx;
		int rx;
	} vlan_id_ix[2] = {
G
Geoff Levand 已提交
1575 1576 1577
		[GELIC_PORT_ETHERNET_0] = {
			.tx = GELIC_LV1_VLAN_TX_ETHERNET_0,
			.rx = GELIC_LV1_VLAN_RX_ETHERNET_0
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621
		},
		[GELIC_PORT_WIRELESS] = {
			.tx = GELIC_LV1_VLAN_TX_WIRELESS,
			.rx = GELIC_LV1_VLAN_RX_WIRELESS
		}
	};

	for (i = 0; i < ARRAY_SIZE(vlan_id_ix); i++) {
		/* tx tag */
		status = lv1_net_control(bus_id(card), dev_id(card),
					 GELIC_LV1_GET_VLAN_ID,
					 vlan_id_ix[i].tx,
					 0, 0, &v1, &v2);
		if (status || !v1) {
			if (status != LV1_NO_ENTRY)
				dev_dbg(ctodev(card),
					"get vlan id for tx(%d) failed(%d)\n",
					vlan_id_ix[i].tx, status);
			card->vlan[i].tx = 0;
			card->vlan[i].rx = 0;
			continue;
		}
		card->vlan[i].tx = (u16)v1;

		/* rx tag */
		status = lv1_net_control(bus_id(card), dev_id(card),
					 GELIC_LV1_GET_VLAN_ID,
					 vlan_id_ix[i].rx,
					 0, 0, &v1, &v2);
		if (status || !v1) {
			if (status != LV1_NO_ENTRY)
				dev_info(ctodev(card),
					 "get vlan id for rx(%d) failed(%d)\n",
					 vlan_id_ix[i].rx, status);
			card->vlan[i].tx = 0;
			card->vlan[i].rx = 0;
			continue;
		}
		card->vlan[i].rx = (u16)v1;

		dev_dbg(ctodev(card), "vlan_id[%d] tx=%02x rx=%02x\n",
			i, card->vlan[i].tx, card->vlan[i].rx);
	}

G
Geoff Levand 已提交
1622
	if (card->vlan[GELIC_PORT_ETHERNET_0].tx) {
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
		BUG_ON(!card->vlan[GELIC_PORT_WIRELESS].tx);
		card->vlan_required = 1;
	} else
		card->vlan_required = 0;

	/* check wirelss capable firmware */
	if (ps3_compare_firmware_version(1, 6, 0) < 0) {
		card->vlan[GELIC_PORT_WIRELESS].tx = 0;
		card->vlan[GELIC_PORT_WIRELESS].rx = 0;
	}

	dev_info(ctodev(card), "internal vlan %s\n",
		 card->vlan_required? "enabled" : "disabled");
}
1637 1638 1639
/**
 * ps3_gelic_driver_probe - add a device to the control of this driver
 */
1640
static int ps3_gelic_driver_probe(struct ps3_system_bus_device *dev)
1641
{
1642 1643
	struct gelic_card *card;
	struct net_device *netdev;
1644 1645
	int result;

1646
	pr_debug("%s: called\n", __func__);
1647 1648 1649

	udbg_shutdown_ps3gelic();

1650 1651 1652
	result = ps3_open_hv_device(dev);

	if (result) {
1653 1654
		dev_dbg(&dev->core, "%s:ps3_open_hv_device failed\n",
			__func__);
1655 1656 1657 1658 1659 1660
		goto fail_open;
	}

	result = ps3_dma_region_create(dev->d_region);

	if (result) {
1661 1662
		dev_dbg(&dev->core, "%s:ps3_dma_region_create failed(%d)\n",
			__func__, result);
1663 1664 1665 1666
		BUG_ON("check region type");
		goto fail_dma_region;
	}

1667 1668 1669 1670 1671 1672 1673 1674
	/* alloc card/netdevice */
	card = gelic_alloc_card_net(&netdev);
	if (!card) {
		dev_info(&dev->core, "%s:gelic_net_alloc_card failed\n",
			 __func__);
		result = -ENOMEM;
		goto fail_alloc_card;
	}
1675
	ps3_system_bus_set_drvdata(dev, card);
1676 1677 1678 1679 1680
	card->dev = dev;

	/* get internal vlan info */
	gelic_card_get_vlan_info(card);

1681 1682
	card->link_mode = GELIC_LV1_ETHER_AUTO_NEG;

1683
	/* setup interrupt */
1684 1685 1686 1687 1688 1689 1690
	result = lv1_net_set_interrupt_status_indicator(bus_id(card),
							dev_id(card),
		ps3_mm_phys_to_lpar(__pa(&card->irq_status)),
		0);

	if (result) {
		dev_dbg(&dev->core,
1691 1692
			"%s:set_interrupt_status_indicator failed: %s\n",
			__func__, ps3_result(result));
1693 1694 1695 1696
		result = -EIO;
		goto fail_status_indicator;
	}

1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707
	result = ps3_sb_event_receive_port_setup(dev, PS3_BINDING_CPU_ANY,
		&card->irq);

	if (result) {
		dev_info(ctodev(card),
			 "%s:gelic_net_open_device failed (%d)\n",
			 __func__, result);
		result = -EPERM;
		goto fail_alloc_irq;
	}
	result = request_irq(card->irq, gelic_card_interrupt,
1708
			     0, netdev->name, card);
1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720

	if (result) {
		dev_info(ctodev(card), "%s:request_irq failed (%d)\n",
			__func__, result);
		goto fail_request_irq;
	}

	/* setup card structure */
	card->irq_mask = GELIC_CARD_RXINT | GELIC_CARD_TXINT |
		GELIC_CARD_PORT_STATUS_CHANGED;


J
Julia Lawall 已提交
1721 1722 1723
	result = gelic_card_init_chain(card, &card->tx_chain,
				       card->descr, GELIC_NET_TX_DESCRIPTORS);
	if (result)
1724
		goto fail_alloc_tx;
J
Julia Lawall 已提交
1725 1726 1727 1728
	result = gelic_card_init_chain(card, &card->rx_chain,
				       card->descr + GELIC_NET_TX_DESCRIPTORS,
				       GELIC_NET_RX_DESCRIPTORS);
	if (result)
1729 1730 1731 1732 1733 1734 1735 1736 1737
		goto fail_alloc_rx;

	/* head of chain */
	card->tx_top = card->tx_chain.head;
	card->rx_top = card->rx_chain.head;
	dev_dbg(ctodev(card), "descr rx %p, tx %p, size %#lx, num %#x\n",
		card->rx_top, card->tx_top, sizeof(struct gelic_descr),
		GELIC_NET_RX_DESCRIPTORS);
	/* allocate rx skbs */
J
Julia Lawall 已提交
1738 1739
	result = gelic_card_alloc_rx_skbs(card);
	if (result)
1740 1741 1742 1743
		goto fail_alloc_skbs;

	spin_lock_init(&card->tx_lock);
	card->tx_dma_progress = 0;
1744

1745 1746 1747 1748 1749
	/* setup net_device structure */
	netdev->irq = card->irq;
	SET_NETDEV_DEV(netdev, &card->dev->core);
	gelic_ether_setup_netdev_ops(netdev, &card->napi);
	result = gelic_net_setup_netdev(netdev, card);
1750
	if (result) {
1751
		dev_dbg(&dev->core, "%s: setup_netdev failed %d\n",
1752
			__func__, result);
1753 1754 1755
		goto fail_setup_netdev;
	}

1756
#ifdef CONFIG_GELIC_WIRELESS
J
Julia Lawall 已提交
1757 1758
	result = gelic_wl_driver_probe(card);
	if (result) {
1759 1760 1761 1762
		dev_dbg(&dev->core, "%s: WL init failed\n", __func__);
		goto fail_setup_netdev;
	}
#endif
1763
	pr_debug("%s: done\n", __func__);
1764 1765 1766
	return 0;

fail_setup_netdev:
1767 1768 1769 1770 1771 1772
fail_alloc_skbs:
	gelic_card_free_chain(card, card->rx_chain.head);
fail_alloc_rx:
	gelic_card_free_chain(card, card->tx_chain.head);
fail_alloc_tx:
	free_irq(card->irq, card);
1773
	netdev->irq = 0;
1774 1775 1776
fail_request_irq:
	ps3_sb_event_receive_port_destroy(dev, card->irq);
fail_alloc_irq:
1777
	lv1_net_set_interrupt_status_indicator(bus_id(card),
1778 1779
					       bus_id(card),
					       0, 0);
1780
fail_status_indicator:
1781
	ps3_system_bus_set_drvdata(dev, NULL);
1782 1783 1784
	kfree(netdev_card(netdev)->unalign);
	free_netdev(netdev);
fail_alloc_card:
1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
	ps3_dma_region_free(dev->d_region);
fail_dma_region:
	ps3_close_hv_device(dev);
fail_open:
	return result;
}

/**
 * ps3_gelic_driver_remove - remove a device from the control of this driver
 */

M
Masakazu Mokuno 已提交
1796
static int ps3_gelic_driver_remove(struct ps3_system_bus_device *dev)
1797
{
1798
	struct gelic_card *card = ps3_system_bus_get_drvdata(dev);
1799 1800 1801
	struct net_device *netdev0;
	pr_debug("%s: called\n", __func__);

1802 1803 1804
	/* set auto-negotiation */
	gelic_card_set_link_mode(card, GELIC_LV1_ETHER_AUTO_NEG);

1805 1806 1807
#ifdef CONFIG_GELIC_WIRELESS
	gelic_wl_driver_remove(card);
#endif
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821
	/* stop interrupt */
	gelic_card_set_irq_mask(card, 0);

	/* turn off DMA, force end */
	gelic_card_disable_rxdmac(card);
	gelic_card_disable_txdmac(card);

	/* release chains */
	gelic_card_release_tx_chain(card, 1);
	gelic_card_release_rx_chain(card);

	gelic_card_free_chain(card, card->tx_top);
	gelic_card_free_chain(card, card->rx_top);

G
Geoff Levand 已提交
1822
	netdev0 = card->netdev[GELIC_PORT_ETHERNET_0];
1823 1824
	/* disconnect event port */
	free_irq(card->irq, card);
1825
	netdev0->irq = 0;
1826
	ps3_sb_event_receive_port_destroy(card->dev, card->irq);
1827 1828 1829 1830 1831 1832 1833

	wait_event(card->waitq,
		   atomic_read(&card->tx_timeout_task_counter) == 0);

	lv1_net_set_interrupt_status_indicator(bus_id(card), dev_id(card),
					       0 , 0);

1834 1835 1836
	unregister_netdev(netdev0);
	kfree(netdev_card(netdev0)->unalign);
	free_netdev(netdev0);
1837

1838
	ps3_system_bus_set_drvdata(dev, NULL);
1839 1840 1841 1842 1843

	ps3_dma_region_free(dev->d_region);

	ps3_close_hv_device(dev);

1844
	pr_debug("%s: done\n", __func__);
1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
	return 0;
}

static struct ps3_system_bus_driver ps3_gelic_driver = {
	.match_id = PS3_MATCH_ID_GELIC,
	.probe = ps3_gelic_driver_probe,
	.remove = ps3_gelic_driver_remove,
	.shutdown = ps3_gelic_driver_remove,
	.core.name = "ps3_gelic_driver",
	.core.owner = THIS_MODULE,
};

static int __init ps3_gelic_driver_init (void)
{
	return firmware_has_feature(FW_FEATURE_PS3_LV1)
		? ps3_system_bus_driver_register(&ps3_gelic_driver)
		: -ENODEV;
}

static void __exit ps3_gelic_driver_exit (void)
{
	ps3_system_bus_driver_unregister(&ps3_gelic_driver);
}

M
Masakazu Mokuno 已提交
1869 1870
module_init(ps3_gelic_driver_init);
module_exit(ps3_gelic_driver_exit);
1871 1872 1873

MODULE_ALIAS(PS3_MODULE_ALIAS_GELIC);