ps3_gelic_net.c 47.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 *  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>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#undef DEBUG

31
#include <linux/interrupt.h>
32 33
#include <linux/kernel.h>
#include <linux/module.h>
34
#include <linux/slab.h>
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

#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"
51
#include "ps3_gelic_wireless.h"
52 53

#define DRV_NAME "Gelic Network Driver"
54
#define DRV_VERSION "2.0"
55 56 57 58 59

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

60

61
/* set irq_mask */
62
int gelic_card_set_irq_mask(struct gelic_card *card, u64 mask)
63 64 65 66 67 68 69
{
	int status;

	status = lv1_net_set_interrupt_mask(bus_id(card), dev_id(card),
					    mask, 0);
	if (status)
		dev_info(ctodev(card),
70
			 "%s failed %d\n", __func__, status);
71 72
	return status;
}
73

74
static void gelic_card_rx_irq_on(struct gelic_card *card)
75
{
76 77
	card->irq_mask |= GELIC_CARD_RXINT;
	gelic_card_set_irq_mask(card, card->irq_mask);
78
}
79
static void gelic_card_rx_irq_off(struct gelic_card *card)
80
{
81 82
	card->irq_mask &= ~GELIC_CARD_RXINT;
	gelic_card_set_irq_mask(card, card->irq_mask);
83
}
84

85 86
static void gelic_card_get_ether_port_status(struct gelic_card *card,
					     int inform)
87 88 89 90 91 92
{
	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 已提交
93
			GELIC_LV1_VLAN_TX_ETHERNET_0, 0, 0,
94 95 96
			&card->ether_port_status, &v2);

	if (inform) {
G
Geoff Levand 已提交
97
		ether_netdev = card->netdev[GELIC_PORT_ETHERNET_0];
98 99 100 101 102 103 104
		if (card->ether_port_status & GELIC_LV1_ETHER_LINK_UP)
			netif_carrier_on(ether_netdev);
		else
			netif_carrier_off(ether_netdev);
	}
}

105 106 107 108 109 110 111 112 113 114 115 116
/**
 * 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;
}

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
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;
}

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 236 237 238 239 240 241 242 243 244 245 246 247 248
/**
 * 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;
}

249 250 251
void gelic_card_up(struct gelic_card *card)
{
	pr_debug("%s: called\n", __func__);
252
	mutex_lock(&card->updown_lock);
253 254 255 256 257 258 259 260 261
	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);
	}
262
	mutex_unlock(&card->updown_lock);
263 264 265 266 267 268 269
	pr_debug("%s: done\n", __func__);
}

void gelic_card_down(struct gelic_card *card)
{
	u64 mask;
	pr_debug("%s: called\n", __func__);
270
	mutex_lock(&card->updown_lock);
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
	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);
	}
288
	mutex_unlock(&card->updown_lock);
289 290
	pr_debug("%s: done\n", __func__);
}
291

292
/**
M
Masakazu Mokuno 已提交
293
 * gelic_card_free_chain - free descriptor chain
294 295 296
 * @card: card structure
 * @descr_in: address of desc
 */
M
Masakazu Mokuno 已提交
297 298
static void gelic_card_free_chain(struct gelic_card *card,
				  struct gelic_descr *descr_in)
299
{
M
Masakazu Mokuno 已提交
300
	struct gelic_descr *descr;
301 302 303

	for (descr = descr_in; descr && descr->bus_addr; descr = descr->next) {
		dma_unmap_single(ctodev(card), descr->bus_addr,
M
Masakazu Mokuno 已提交
304
				 GELIC_DESCR_SIZE, DMA_BIDIRECTIONAL);
305 306 307 308 309
		descr->bus_addr = 0;
	}
}

/**
M
Masakazu Mokuno 已提交
310
 * gelic_card_init_chain - links descriptor chain
311 312 313 314 315 316 317 318 319 320
 * @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
 */
321
static int gelic_card_init_chain(struct gelic_card *card,
322 323
				 struct gelic_descr_chain *chain,
				 struct gelic_descr *start_descr, int no)
324 325
{
	int i;
M
Masakazu Mokuno 已提交
326
	struct gelic_descr *descr;
327 328 329 330 331 332

	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 已提交
333
		gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
334 335
		descr->bus_addr =
			dma_map_single(ctodev(card), descr,
M
Masakazu Mokuno 已提交
336
				       GELIC_DESCR_SIZE,
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
				       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++) {
352
		descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
353 354 355 356 357 358 359 360 361 362 363 364 365 366
	}

	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 已提交
367
					 GELIC_DESCR_SIZE,
368 369 370 371 372
					 DMA_BIDIRECTIONAL);
	return -ENOMEM;
}

/**
M
Masakazu Mokuno 已提交
373
 * gelic_descr_prepare_rx - reinitializes a rx descriptor
374 375 376
 * @card: card structure
 * @descr: descriptor to re-init
 *
377
 * return 0 on success, <0 on failure
378 379 380 381
 *
 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
 * Activate the descriptor state-wise
 */
M
Masakazu Mokuno 已提交
382
static int gelic_descr_prepare_rx(struct gelic_card *card,
383
				  struct gelic_descr *descr)
384 385 386 387
{
	int offset;
	unsigned int bufsize;

M
Masakazu Mokuno 已提交
388
	if (gelic_descr_get_status(descr) !=  GELIC_DESCR_DMA_NOT_IN_USE)
389
		dev_info(ctodev(card), "%s: ERROR status\n", __func__);
390 391 392 393 394
	/* 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 */
395
	descr->skb = dev_alloc_skb(bufsize + GELIC_NET_RXBUF_ALIGN - 1);
396 397 398 399 400 401
	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;
	}
402
	descr->buf_size = cpu_to_be32(bufsize);
403 404 405 406 407 408 409 410 411 412
	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 */
413 414 415 416
	descr->buf_addr = cpu_to_be32(dma_map_single(ctodev(card),
						     descr->skb->data,
						     GELIC_NET_MAX_MTU,
						     DMA_FROM_DEVICE));
417 418 419 420 421
	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 已提交
422
		gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
423 424
		return -ENOMEM;
	} else {
M
Masakazu Mokuno 已提交
425
		gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
426 427 428 429 430
		return 0;
	}
}

/**
M
Masakazu Mokuno 已提交
431
 * gelic_card_release_rx_chain - free all skb of rx descr
432 433 434
 * @card: card structure
 *
 */
M
Masakazu Mokuno 已提交
435
static void gelic_card_release_rx_chain(struct gelic_card *card)
436
{
M
Masakazu Mokuno 已提交
437
	struct gelic_descr *descr = card->rx_chain.head;
438 439 440 441

	do {
		if (descr->skb) {
			dma_unmap_single(ctodev(card),
442
					 be32_to_cpu(descr->buf_addr),
443 444 445 446 447
					 descr->skb->len,
					 DMA_FROM_DEVICE);
			descr->buf_addr = 0;
			dev_kfree_skb_any(descr->skb);
			descr->skb = NULL;
M
Masakazu Mokuno 已提交
448 449
			gelic_descr_set_status(descr,
					       GELIC_DESCR_DMA_NOT_IN_USE);
450 451 452 453 454 455
		}
		descr = descr->next;
	} while (descr != card->rx_chain.head);
}

/**
M
Masakazu Mokuno 已提交
456
 * gelic_card_fill_rx_chain - fills descriptors/skbs in the rx chains
457 458 459 460
 * @card: card structure
 *
 * fills all descriptors in the rx chain: allocates skbs
 * and iommu-maps them.
M
Masakazu Mokuno 已提交
461
 * returns 0 on success, < 0 on failure
462
 */
M
Masakazu Mokuno 已提交
463
static int gelic_card_fill_rx_chain(struct gelic_card *card)
464
{
M
Masakazu Mokuno 已提交
465
	struct gelic_descr *descr = card->rx_chain.head;
466 467 468 469
	int ret;

	do {
		if (!descr->skb) {
M
Masakazu Mokuno 已提交
470
			ret = gelic_descr_prepare_rx(card, descr);
471 472 473 474 475 476 477 478
			if (ret)
				goto rewind;
		}
		descr = descr->next;
	} while (descr != card->rx_chain.head);

	return 0;
rewind:
M
Masakazu Mokuno 已提交
479
	gelic_card_release_rx_chain(card);
480 481 482 483
	return ret;
}

/**
M
Masakazu Mokuno 已提交
484
 * gelic_card_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
485 486
 * @card: card structure
 *
M
Masakazu Mokuno 已提交
487
 * returns 0 on success, < 0 on failure
488
 */
489
static int gelic_card_alloc_rx_skbs(struct gelic_card *card)
490
{
M
Masakazu Mokuno 已提交
491
	struct gelic_descr_chain *chain;
492 493
	int ret;
	chain = &card->rx_chain;
M
Masakazu Mokuno 已提交
494
	ret = gelic_card_fill_rx_chain(card);
495
	chain->tail = card->rx_top->prev; /* point to the last */
496 497 498 499
	return ret;
}

/**
M
Masakazu Mokuno 已提交
500
 * gelic_descr_release_tx - processes a used tx descriptor
501 502 503 504 505
 * @card: card structure
 * @descr: descriptor to release
 *
 * releases a used tx descriptor (unmapping, freeing of skb)
 */
M
Masakazu Mokuno 已提交
506
static void gelic_descr_release_tx(struct gelic_card *card,
507
				       struct gelic_descr *descr)
508
{
509
	struct sk_buff *skb = descr->skb;
510

511 512 513 514
	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);
515
	dev_kfree_skb_any(skb);
516 517 518 519 520 521 522 523 524 525 526

	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 已提交
527
	gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
528 529
}

530 531
static void gelic_card_stop_queues(struct gelic_card *card)
{
G
Geoff Levand 已提交
532
	netif_stop_queue(card->netdev[GELIC_PORT_ETHERNET_0]);
533 534 535 536 537 538

	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 已提交
539
	netif_wake_queue(card->netdev[GELIC_PORT_ETHERNET_0]);
540 541 542 543

	if (card->netdev[GELIC_PORT_WIRELESS])
		netif_wake_queue(card->netdev[GELIC_PORT_WIRELESS]);
}
544
/**
M
Masakazu Mokuno 已提交
545
 * gelic_card_release_tx_chain - processes sent tx descriptors
546 547 548 549 550
 * @card: adapter structure
 * @stop: net_stop sequence
 *
 * releases the tx descriptors that gelic has finished with
 */
M
Masakazu Mokuno 已提交
551
static void gelic_card_release_tx_chain(struct gelic_card *card, int stop)
552
{
M
Masakazu Mokuno 已提交
553 554
	struct gelic_descr_chain *tx_chain;
	enum gelic_descr_dma_status status;
555
	struct net_device *netdev;
556 557 558 559 560
	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 已提交
561
		status = gelic_descr_get_status(tx_chain->tail);
562
		netdev = tx_chain->tail->skb->dev;
563
		switch (status) {
M
Masakazu Mokuno 已提交
564 565 566
		case GELIC_DESCR_DMA_RESPONSE_ERROR:
		case GELIC_DESCR_DMA_PROTECTION_ERROR:
		case GELIC_DESCR_DMA_FORCE_END:
567 568 569 570 571
			if (printk_ratelimit())
				dev_info(ctodev(card),
					 "%s: forcing end of tx descriptor " \
					 "with status %x\n",
					 __func__, status);
572
			netdev->stats.tx_dropped++;
573 574
			break;

M
Masakazu Mokuno 已提交
575
		case GELIC_DESCR_DMA_COMPLETE:
576
			if (tx_chain->tail->skb) {
577 578
				netdev->stats.tx_packets++;
				netdev->stats.tx_bytes +=
579 580
					tx_chain->tail->skb->len;
			}
581 582
			break;

M
Masakazu Mokuno 已提交
583
		case GELIC_DESCR_DMA_CARDOWNED:
584 585
			/* pending tx request */
		default:
M
Masakazu Mokuno 已提交
586
			/* any other value (== GELIC_DESCR_DMA_NOT_IN_USE) */
587 588
			if (!stop)
				goto out;
589
		}
M
Masakazu Mokuno 已提交
590
		gelic_descr_release_tx(card, tx_chain->tail);
591
		release ++;
592 593
	}
out:
594
	if (!stop && release)
595
		gelic_card_wake_queues(card);
596 597 598 599 600 601 602 603 604 605
}

/**
 * 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
 */
606
void gelic_net_set_multi(struct net_device *netdev)
607
{
608
	struct gelic_card *card = netdev_card(netdev);
609
	struct netdev_hw_addr *ha;
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
	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);

630
	if ((netdev->flags & IFF_ALLMULTI) ||
631
	    (netdev_mc_count(netdev) > GELIC_NET_MC_COUNT_MAX)) {
632 633 634 635 636 637 638 639 640 641
		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;
	}

642
	/* set multicast addresses */
643
	netdev_for_each_mc_addr(ha, netdev) {
644
		addr = 0;
645
		p = ha->addr;
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
		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
 */
666
int gelic_net_stop(struct net_device *netdev)
667
{
668
	struct gelic_card *card;
669

670
	pr_debug("%s: start\n", __func__);
671

672
	netif_stop_queue(netdev);
673 674
	netif_carrier_off(netdev);

675 676
	card = netdev_card(netdev);
	gelic_card_down(card);
677

678
	pr_debug("%s: done\n", __func__);
679 680 681 682
	return 0;
}

/**
M
Masakazu Mokuno 已提交
683
 * gelic_card_get_next_tx_descr - returns the next available tx descriptor
684 685 686 687
 * @card: device structure to get descriptor from
 *
 * returns the address of the next descriptor, or NULL if not available.
 */
M
Masakazu Mokuno 已提交
688 689
static struct gelic_descr *
gelic_card_get_next_tx_descr(struct gelic_card *card)
690 691 692
{
	if (!card->tx_chain.head)
		return NULL;
693
	/*  see if the next descriptor is free */
694
	if (card->tx_chain.tail != card->tx_chain.head->next &&
M
Masakazu Mokuno 已提交
695 696
	    gelic_descr_get_status(card->tx_chain.head) ==
	    GELIC_DESCR_DMA_NOT_IN_USE)
697 698 699 700 701 702 703
		return card->tx_chain.head;
	else
		return NULL;

}

/**
704
 * gelic_net_set_txdescr_cmdstat - sets the tx descriptor command field
705 706 707 708 709 710 711
 * @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 已提交
712 713
static void gelic_descr_set_tx_cmdstat(struct gelic_descr *descr,
				       struct sk_buff *skb)
714 715
{
	if (skb->ip_summed != CHECKSUM_PARTIAL)
716
		descr->dmac_cmd_status =
M
Masakazu Mokuno 已提交
717 718
			cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
				    GELIC_DESCR_TX_DMA_FRAME_TAIL);
719 720 721 722 723 724
	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 已提交
725 726
				cpu_to_be32(GELIC_DESCR_DMA_CMD_TCP_CHKSUM |
					    GELIC_DESCR_TX_DMA_FRAME_TAIL);
727

728 729
			else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
				descr->dmac_cmd_status =
M
Masakazu Mokuno 已提交
730 731
				cpu_to_be32(GELIC_DESCR_DMA_CMD_UDP_CHKSUM |
					    GELIC_DESCR_TX_DMA_FRAME_TAIL);
732 733 734 735 736
			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 已提交
737 738
				cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
					    GELIC_DESCR_TX_DMA_FRAME_TAIL);
739 740 741 742
		}
	}
}

743
static struct sk_buff *gelic_put_vlan_tag(struct sk_buff *skb,
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
						 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);
	}
	veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN);

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

762
	veth->h_vlan_proto = cpu_to_be16(ETH_P_8021Q);
763 764 765 766 767
	veth->h_vlan_TCI = htons(tag);

	return skb;
}

768
/**
769
 * gelic_descr_prepare_tx - setup a descriptor for sending packets
770 771 772 773 774 775 776
 * @card: card structure
 * @descr: descriptor structure
 * @skb: packet to use
 *
 * returns 0 on success, <0 on failure.
 *
 */
M
Masakazu Mokuno 已提交
777 778 779
static int gelic_descr_prepare_tx(struct gelic_card *card,
				  struct gelic_descr *descr,
				  struct sk_buff *skb)
780
{
781
	dma_addr_t buf;
782

783
	if (card->vlan_required) {
784
		struct sk_buff *skb_tmp;
785 786 787
		enum gelic_port_type type;

		type = netdev_port(skb->dev)->type;
788
		skb_tmp = gelic_put_vlan_tag(skb,
789
					     card->vlan[type].tx);
790 791 792
		if (!skb_tmp)
			return -ENOMEM;
		skb = skb_tmp;
793 794
	}

795
	buf = dma_map_single(ctodev(card), skb->data, skb->len, DMA_TO_DEVICE);
796

797
	if (!buf) {
798 799
		dev_err(ctodev(card),
			"dma map 2 failed (%p, %i). Dropping packet\n",
800
			skb->data, skb->len);
801 802 803
		return -ENOMEM;
	}

804 805
	descr->buf_addr = cpu_to_be32(buf);
	descr->buf_size = cpu_to_be32(skb->len);
806
	descr->skb = skb;
807
	descr->data_status = 0;
808
	descr->next_descr_addr = 0; /* terminate hw descr */
M
Masakazu Mokuno 已提交
809
	gelic_descr_set_tx_cmdstat(descr, skb);
810 811

	/* bump free descriptor pointer */
812
	card->tx_chain.head = descr->next;
813 814 815 816
	return 0;
}

/**
M
Masakazu Mokuno 已提交
817
 * gelic_card_kick_txdma - enables TX DMA processing
818 819 820 821
 * @card: card structure
 * @descr: descriptor address to enable TX processing at
 *
 */
M
Masakazu Mokuno 已提交
822 823
static int gelic_card_kick_txdma(struct gelic_card *card,
				 struct gelic_descr *descr)
824
{
825
	int status = 0;
826 827 828 829

	if (card->tx_dma_progress)
		return 0;

M
Masakazu Mokuno 已提交
830
	if (gelic_descr_get_status(descr) == GELIC_DESCR_DMA_CARDOWNED) {
831
		card->tx_dma_progress = 1;
832 833
		status = lv1_net_start_tx_dma(bus_id(card), dev_id(card),
					      descr->bus_addr, 0);
834 835
		if (status) {
			card->tx_dma_progress = 0;
836
			dev_info(ctodev(card), "lv1_net_start_txdma failed," \
837
				 "status=%d\n", status);
838
		}
839 840 841 842 843 844 845 846 847 848 849
	}
	return status;
}

/**
 * gelic_net_xmit - transmits a frame over the device
 * @skb: packet to send out
 * @netdev: interface device structure
 *
 * returns 0 on success, <0 on failure
 */
850
int gelic_net_xmit(struct sk_buff *skb, struct net_device *netdev)
851
{
852
	struct gelic_card *card = netdev_card(netdev);
M
Masakazu Mokuno 已提交
853
	struct gelic_descr *descr;
854 855 856
	int result;
	unsigned long flags;

857
	spin_lock_irqsave(&card->tx_lock, flags);
858

M
Masakazu Mokuno 已提交
859
	gelic_card_release_tx_chain(card, 0);
860

M
Masakazu Mokuno 已提交
861
	descr = gelic_card_get_next_tx_descr(card);
862
	if (!descr) {
863 864 865
		/*
		 * no more descriptors free
		 */
866 867
		gelic_card_stop_queues(card);
		spin_unlock_irqrestore(&card->tx_lock, flags);
868 869 870
		return NETDEV_TX_BUSY;
	}

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

908
	spin_unlock_irqrestore(&card->tx_lock, flags);
909 910 911 912 913 914 915
	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
916
 * @netdev: net_device structure to be passed packet
917 918 919 920
 *
 * iommu-unmaps the skb, fills out skb structure and passes the data to the
 * stack. The descriptor state is not changed.
 */
M
Masakazu Mokuno 已提交
921
static void gelic_net_pass_skb_up(struct gelic_descr *descr,
922 923 924
				  struct gelic_card *card,
				  struct net_device *netdev)

925
{
926
	struct sk_buff *skb = descr->skb;
927 928
	u32 data_status, data_error;

929 930
	data_status = be32_to_cpu(descr->data_status);
	data_error = be32_to_cpu(descr->data_error);
931
	/* unmap skb buffer */
932 933
	dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr),
			 GELIC_NET_MAX_MTU,
934 935
			 DMA_FROM_DEVICE);

936
	skb_put(skb, be32_to_cpu(descr->valid_size)?
937 938
		be32_to_cpu(descr->valid_size) :
		be32_to_cpu(descr->result_size));
939 940
	if (!descr->valid_size)
		dev_info(ctodev(card), "buffer full %x %x %x\n",
941 942 943
			 be32_to_cpu(descr->result_size),
			 be32_to_cpu(descr->buf_size),
			 be32_to_cpu(descr->dmac_cmd_status));
944 945 946 947 948 949 950 951 952 953

	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 */
954
	if (netdev->features & NETIF_F_RXCSUM) {
M
Masakazu Mokuno 已提交
955 956
		if ((data_status & GELIC_DESCR_DATA_STATUS_CHK_MASK) &&
		    (!(data_error & GELIC_DESCR_DATA_ERROR_CHK_MASK)))
957 958
			skb->ip_summed = CHECKSUM_UNNECESSARY;
		else
959
			skb_checksum_none_assert(skb);
960
	} else
961
		skb_checksum_none_assert(skb);
962 963

	/* update netdevice statistics */
964 965
	netdev->stats.rx_packets++;
	netdev->stats.rx_bytes += skb->len;
966 967 968 969 970 971

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

/**
M
Masakazu Mokuno 已提交
972
 * gelic_card_decode_one_descr - processes an rx descriptor
973 974 975 976 977 978 979
 * @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 已提交
980
static int gelic_card_decode_one_descr(struct gelic_card *card)
981
{
M
Masakazu Mokuno 已提交
982 983
	enum gelic_descr_dma_status status;
	struct gelic_descr_chain *chain = &card->rx_chain;
984 985
	struct gelic_descr *descr = chain->head;
	struct net_device *netdev = NULL;
986 987
	int dmac_chain_ended;

M
Masakazu Mokuno 已提交
988
	status = gelic_descr_get_status(descr);
989

M
Masakazu Mokuno 已提交
990
	if (status == GELIC_DESCR_DMA_CARDOWNED)
991 992
		return 0;

M
Masakazu Mokuno 已提交
993
	if (status == GELIC_DESCR_DMA_NOT_IN_USE) {
994 995 996 997
		dev_dbg(ctodev(card), "dormant descr? %p\n", descr);
		return 0;
	}

998 999 1000 1001 1002 1003 1004 1005 1006 1007
	/* 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;
			}
1008
		}
1009 1010 1011 1012 1013
		if (GELIC_PORT_MAX <= i) {
			pr_info("%s: unknown packet vid=%x\n", __func__, vid);
			goto refill;
		}
	} else
G
Geoff Levand 已提交
1014
		netdev = card->netdev[GELIC_PORT_ETHERNET_0];
1015

M
Masakazu Mokuno 已提交
1016 1017 1018
	if ((status == GELIC_DESCR_DMA_RESPONSE_ERROR) ||
	    (status == GELIC_DESCR_DMA_PROTECTION_ERROR) ||
	    (status == GELIC_DESCR_DMA_FORCE_END)) {
1019 1020
		dev_info(ctodev(card), "dropping RX descriptor with state %x\n",
			 status);
1021
		netdev->stats.rx_dropped++;
1022 1023 1024
		goto refill;
	}

M
Masakazu Mokuno 已提交
1025
	if (status == GELIC_DESCR_DMA_BUFFER_FULL) {
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
		/*
		 * 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 已提交
1039
	 * descriptors any other than FRAME_END here should
1040 1041
	 * be treated as error.
	 */
M
Masakazu Mokuno 已提交
1042
	if (status != GELIC_DESCR_DMA_FRAME_END) {
1043 1044 1045 1046 1047 1048
		dev_dbg(ctodev(card), "RX descriptor with state %x\n",
			status);
		goto refill;
	}

	/* ok, we've got a packet in descr */
1049
	gelic_net_pass_skb_up(descr, card, netdev);
1050
refill:
G
Geoff Levand 已提交
1051 1052 1053 1054 1055

	/* 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;
1056 1057 1058 1059 1060 1061
	/*
	 * So that always DMAC can see the end
	 * of the descriptor chain to avoid
	 * from unwanted DMAC overrun.
	 */
	descr->next_descr_addr = 0;
1062 1063

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

1066 1067
	/*
	 * this call can fail, but for now, just leave this
1068
	 * descriptor without skb
1069
	 */
M
Masakazu Mokuno 已提交
1070
	gelic_descr_prepare_rx(card, descr);
1071

1072 1073
	chain->tail = descr;
	chain->head = descr->next;
1074 1075 1076 1077

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

1080 1081 1082 1083
	/*
	 * If dmac chain was met, DMAC stopped.
	 * thus re-enable it
	 */
G
Geoff Levand 已提交
1084 1085 1086

	if (dmac_chain_ended)
		gelic_card_enable_rxdmac(card);
1087 1088 1089 1090 1091 1092

	return 1;
}

/**
 * gelic_net_poll - NAPI poll function called by the stack to return packets
1093
 * @napi: napi structure
1094 1095
 * @budget: number of packets we can pass to the stack at most
 *
1096
 * returns the number of the processed packets
1097 1098
 *
 */
1099
static int gelic_net_poll(struct napi_struct *napi, int budget)
1100
{
M
Masakazu Mokuno 已提交
1101
	struct gelic_card *card = container_of(napi, struct gelic_card, napi);
1102
	int packets_done = 0;
1103

1104
	while (packets_done < budget) {
M
Masakazu Mokuno 已提交
1105
		if (!gelic_card_decode_one_descr(card))
1106
			break;
1107 1108

		packets_done++;
1109
	}
1110 1111

	if (packets_done < budget) {
1112
		napi_complete_done(napi, packets_done);
M
Masakazu Mokuno 已提交
1113
		gelic_card_rx_irq_on(card);
1114 1115
	}
	return packets_done;
1116 1117 1118
}

/**
M
Masakazu Mokuno 已提交
1119
 * gelic_card_interrupt - event handler for gelic_net
1120
 */
M
Masakazu Mokuno 已提交
1121
static irqreturn_t gelic_card_interrupt(int irq, void *ptr)
1122 1123
{
	unsigned long flags;
1124
	struct gelic_card *card = ptr;
1125 1126 1127 1128 1129 1130 1131
	u64 status;

	status = card->irq_status;

	if (!status)
		return IRQ_NONE;

1132 1133
	status &= card->irq_mask;

M
Masakazu Mokuno 已提交
1134 1135
	if (status & GELIC_CARD_RXINT) {
		gelic_card_rx_irq_off(card);
1136
		napi_schedule(&card->napi);
1137 1138
	}

M
Masakazu Mokuno 已提交
1139
	if (status & GELIC_CARD_TXINT) {
1140
		spin_lock_irqsave(&card->tx_lock, flags);
1141
		card->tx_dma_progress = 0;
M
Masakazu Mokuno 已提交
1142
		gelic_card_release_tx_chain(card, 0);
1143
		/* kick outstanding tx descriptor if any */
M
Masakazu Mokuno 已提交
1144
		gelic_card_kick_txdma(card, card->tx_chain.tail);
1145
		spin_unlock_irqrestore(&card->tx_lock, flags);
1146
	}
1147 1148 1149 1150

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

1152 1153 1154 1155 1156 1157
#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

1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
	return IRQ_HANDLED;
}

#ifdef CONFIG_NET_POLL_CONTROLLER
/**
 * gelic_net_poll_controller - artificial interrupt for netconsole etc.
 * @netdev: interface device structure
 *
 * see Documentation/networking/netconsole.txt
 */
1168
void gelic_net_poll_controller(struct net_device *netdev)
1169
{
1170
	struct gelic_card *card = netdev_card(netdev);
1171

M
Masakazu Mokuno 已提交
1172 1173
	gelic_card_set_irq_mask(card, 0);
	gelic_card_interrupt(netdev->irq, netdev);
1174
	gelic_card_set_irq_mask(card, card->irq_mask);
1175 1176 1177 1178
}
#endif /* CONFIG_NET_POLL_CONTROLLER */

/**
A
Andre Heider 已提交
1179
 * gelic_net_open - called upon ifconfig up
1180 1181 1182 1183 1184 1185 1186
 * @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
 */
1187
int gelic_net_open(struct net_device *netdev)
1188
{
1189
	struct gelic_card *card = netdev_card(netdev);
1190

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

1193
	gelic_card_up(card);
1194 1195

	netif_start_queue(netdev);
1196
	gelic_card_get_ether_port_status(card, 1);
1197

1198
	dev_dbg(ctodev(card), " <- %s\n", __func__);
1199 1200 1201
	return 0;
}

1202 1203
void gelic_net_get_drvinfo(struct net_device *netdev,
			   struct ethtool_drvinfo *info)
1204
{
1205 1206
	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
1207 1208
}

M
Masakazu Mokuno 已提交
1209 1210
static int gelic_ether_get_settings(struct net_device *netdev,
				    struct ethtool_cmd *cmd)
1211
{
1212
	struct gelic_card *card = netdev_card(netdev);
1213

1214 1215 1216 1217 1218 1219 1220 1221 1222
	gelic_card_get_ether_port_status(card, 0);

	if (card->ether_port_status & GELIC_LV1_ETHER_FULL_DUPLEX)
		cmd->duplex = DUPLEX_FULL;
	else
		cmd->duplex = DUPLEX_HALF;

	switch (card->ether_port_status & GELIC_LV1_ETHER_SPEED_MASK) {
	case GELIC_LV1_ETHER_SPEED_10:
1223
		ethtool_cmd_speed_set(cmd, SPEED_10);
1224 1225
		break;
	case GELIC_LV1_ETHER_SPEED_100:
1226
		ethtool_cmd_speed_set(cmd, SPEED_100);
1227 1228
		break;
	case GELIC_LV1_ETHER_SPEED_1000:
1229
		ethtool_cmd_speed_set(cmd, SPEED_1000);
1230 1231 1232
		break;
	default:
		pr_info("%s: speed unknown\n", __func__);
1233
		ethtool_cmd_speed_set(cmd, SPEED_10);
1234
		break;
1235
	}
1236

1237 1238 1239
	cmd->supported = SUPPORTED_TP | SUPPORTED_Autoneg |
			SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
			SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
1240
			SUPPORTED_1000baseT_Full;
1241
	cmd->advertising = cmd->supported;
1242 1243 1244 1245 1246 1247
	if (card->link_mode & GELIC_LV1_ETHER_AUTO_NEG) {
		cmd->autoneg = AUTONEG_ENABLE;
	} else {
		cmd->autoneg = AUTONEG_DISABLE;
		cmd->advertising &= ~ADVERTISED_Autoneg;
	}
1248 1249 1250 1251 1252
	cmd->port = PORT_TP;

	return 0;
}

1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
static int gelic_ether_set_settings(struct net_device *netdev,
				    struct ethtool_cmd *cmd)
{
	struct gelic_card *card = netdev_card(netdev);
	u64 mode;
	int ret;

	if (cmd->autoneg == AUTONEG_ENABLE) {
		mode = GELIC_LV1_ETHER_AUTO_NEG;
	} else {
		switch (cmd->speed) {
		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;
		}
		if (cmd->duplex == DUPLEX_FULL)
			mode |= GELIC_LV1_ETHER_FULL_DUPLEX;
		else if (cmd->speed == SPEED_1000) {
			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;
}

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 1365 1366 1367 1368 1369 1370
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;
}

1371
static const struct ethtool_ops gelic_ether_ethtool_ops = {
1372
	.get_drvinfo	= gelic_net_get_drvinfo,
M
Masakazu Mokuno 已提交
1373
	.get_settings	= gelic_ether_get_settings,
1374
	.set_settings	= gelic_ether_set_settings,
1375
	.get_link	= ethtool_op_get_link,
1376 1377
	.get_wol	= gelic_net_get_wol,
	.set_wol	= gelic_net_set_wol,
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
};

/**
 * 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 已提交
1389 1390
	struct gelic_card *card =
		container_of(work, struct gelic_card, tx_timeout_task);
G
Geoff Levand 已提交
1391
	struct net_device *netdev = card->netdev[GELIC_PORT_ETHERNET_0];
1392

1393
	dev_info(ctodev(card), "%s:Timed out. Restarting...\n", __func__);
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413

	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
 */
1414
void gelic_net_tx_timeout(struct net_device *netdev)
1415
{
M
Masakazu Mokuno 已提交
1416
	struct gelic_card *card;
1417

1418
	card = netdev_card(netdev);
1419 1420 1421 1422 1423 1424 1425
	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);
}

1426 1427 1428 1429
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,
1430
	.ndo_set_rx_mode = gelic_net_set_multi,
1431
	.ndo_tx_timeout = gelic_net_tx_timeout,
1432
	.ndo_set_mac_address = eth_mac_addr,
1433 1434 1435 1436 1437 1438
	.ndo_validate_addr = eth_validate_addr,
#ifdef CONFIG_NET_POLL_CONTROLLER
	.ndo_poll_controller = gelic_net_poll_controller,
#endif
};

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

/**
1456 1457
 * gelic_ether_setup_netdev - initialization of net_device
 * @netdev: net_device structure
1458 1459 1460 1461
 * @card: card structure
 *
 * Returns 0 on success or <0 on failure
 *
1462 1463
 * gelic_ether_setup_netdev initializes the net_device structure
 * and register it.
1464
 **/
1465
int gelic_net_setup_netdev(struct net_device *netdev, struct gelic_card *card)
1466 1467 1468 1469
{
	int status;
	u64 v1, v2;

1470 1471
	netdev->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM;

1472
	netdev->features = NETIF_F_IP_CSUM;
1473 1474
	if (GELIC_CARD_RX_CSUM_DEFAULT)
		netdev->features |= NETIF_F_RXCSUM;
1475 1476

	status = lv1_net_control(bus_id(card), dev_id(card),
M
Masakazu Mokuno 已提交
1477
				 GELIC_LV1_GET_MAC_ADDRESS,
1478
				 0, 0, 0, &v1, &v2);
1479
	v1 <<= 16;
1480 1481 1482 1483 1484 1485
	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;
	}
1486
	memcpy(netdev->dev_addr, &v1, ETH_ALEN);
1487

1488
	if (card->vlan_required) {
1489
		netdev->hard_header_len += VLAN_HLEN;
1490 1491 1492 1493 1494
		/*
		 * As vlan is internally used,
		 * we can not receive vlan packets
		 */
		netdev->features |= NETIF_F_VLAN_CHALLENGED;
1495
	}
1496

1497 1498 1499 1500
	/* MTU range: 64 - 1518 */
	netdev->min_mtu = GELIC_NET_MIN_MTU;
	netdev->max_mtu = GELIC_NET_MAX_MTU;

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

	return 0;
}

/**
M
Masakazu Mokuno 已提交
1514
 * gelic_alloc_card_net - allocates net_device and card structure
1515 1516 1517 1518 1519
 *
 * returns the card structure or NULL in case of errors
 *
 * the card and net_device structures are linked to each other
 */
1520
#define GELIC_ALIGN (32)
1521
static struct gelic_card *gelic_alloc_card_net(struct net_device **netdev)
1522
{
M
Masakazu Mokuno 已提交
1523
	struct gelic_card *card;
1524 1525
	struct gelic_port *port;
	void *p;
1526 1527
	size_t alloc_size;
	/*
1528 1529
	 * gelic requires dma descriptor is 32 bytes aligned and
	 * the hypervisor requires irq_status is 8 bytes aligned.
1530
	 */
M
Masakazu Mokuno 已提交
1531 1532
	BUILD_BUG_ON(offsetof(struct gelic_card, irq_status) % 8);
	BUILD_BUG_ON(offsetof(struct gelic_card, descr) % 32);
1533 1534 1535 1536 1537
	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;
1538

1539 1540
	p  = kzalloc(alloc_size, GFP_KERNEL);
	if (!p)
1541
		return NULL;
1542 1543 1544 1545 1546 1547 1548
	card = PTR_ALIGN(p, GELIC_ALIGN);
	card->unalign = p;

	/*
	 * alloc netdev
	 */
	*netdev = alloc_etherdev(sizeof(struct gelic_port));
1549
	if (!*netdev) {
1550 1551 1552 1553 1554 1555 1556 1557
		kfree(card->unalign);
		return NULL;
	}
	port = netdev_priv(*netdev);

	/* gelic_port */
	port->netdev = *netdev;
	port->card = card;
G
Geoff Levand 已提交
1558
	port->type = GELIC_PORT_ETHERNET_0;
1559 1560

	/* gelic_card */
G
Geoff Levand 已提交
1561
	card->netdev[GELIC_PORT_ETHERNET_0] = *netdev;
1562 1563 1564 1565

	INIT_WORK(&card->tx_timeout_task, gelic_net_tx_timeout_task);
	init_waitqueue_head(&card->waitq);
	atomic_set(&card->tx_timeout_task_counter, 0);
1566
	mutex_init(&card->updown_lock);
1567
	atomic_set(&card->users, 0);
1568 1569 1570 1571

	return card;
}

1572
static void gelic_card_get_vlan_info(struct gelic_card *card)
1573 1574 1575 1576 1577 1578 1579 1580
{
	u64 v1, v2;
	int status;
	unsigned int i;
	struct {
		int tx;
		int rx;
	} vlan_id_ix[2] = {
G
Geoff Levand 已提交
1581 1582 1583
		[GELIC_PORT_ETHERNET_0] = {
			.tx = GELIC_LV1_VLAN_TX_ETHERNET_0,
			.rx = GELIC_LV1_VLAN_RX_ETHERNET_0
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 1622 1623 1624 1625 1626 1627
		},
		[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 已提交
1628
	if (card->vlan[GELIC_PORT_ETHERNET_0].tx) {
1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
		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");
}
1643 1644 1645
/**
 * ps3_gelic_driver_probe - add a device to the control of this driver
 */
1646
static int ps3_gelic_driver_probe(struct ps3_system_bus_device *dev)
1647
{
1648 1649
	struct gelic_card *card;
	struct net_device *netdev;
1650 1651
	int result;

1652
	pr_debug("%s: called\n", __func__);
1653 1654 1655

	udbg_shutdown_ps3gelic();

1656 1657 1658
	result = ps3_open_hv_device(dev);

	if (result) {
1659 1660
		dev_dbg(&dev->core, "%s:ps3_open_hv_device failed\n",
			__func__);
1661 1662 1663 1664 1665 1666
		goto fail_open;
	}

	result = ps3_dma_region_create(dev->d_region);

	if (result) {
1667 1668
		dev_dbg(&dev->core, "%s:ps3_dma_region_create failed(%d)\n",
			__func__, result);
1669 1670 1671 1672
		BUG_ON("check region type");
		goto fail_dma_region;
	}

1673 1674 1675 1676 1677 1678 1679 1680
	/* 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;
	}
1681
	ps3_system_bus_set_drvdata(dev, card);
1682 1683 1684 1685 1686
	card->dev = dev;

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

1687 1688
	card->link_mode = GELIC_LV1_ETHER_AUTO_NEG;

1689
	/* setup interrupt */
1690 1691 1692 1693 1694 1695 1696
	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,
1697 1698
			"%s:set_interrupt_status_indicator failed: %s\n",
			__func__, ps3_result(result));
1699 1700 1701 1702
		result = -EIO;
		goto fail_status_indicator;
	}

1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713
	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,
1714
			     0, netdev->name, card);
1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726

	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 已提交
1727 1728 1729
	result = gelic_card_init_chain(card, &card->tx_chain,
				       card->descr, GELIC_NET_TX_DESCRIPTORS);
	if (result)
1730
		goto fail_alloc_tx;
J
Julia Lawall 已提交
1731 1732 1733 1734
	result = gelic_card_init_chain(card, &card->rx_chain,
				       card->descr + GELIC_NET_TX_DESCRIPTORS,
				       GELIC_NET_RX_DESCRIPTORS);
	if (result)
1735 1736 1737 1738 1739 1740 1741 1742 1743
		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 已提交
1744 1745
	result = gelic_card_alloc_rx_skbs(card);
	if (result)
1746 1747 1748 1749
		goto fail_alloc_skbs;

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

1751 1752 1753 1754 1755
	/* 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);
1756
	if (result) {
1757
		dev_dbg(&dev->core, "%s: setup_netdev failed %d\n",
1758
			__func__, result);
1759 1760 1761
		goto fail_setup_netdev;
	}

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

fail_setup_netdev:
1773 1774 1775 1776 1777 1778
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);
1779
	netdev->irq = 0;
1780 1781 1782
fail_request_irq:
	ps3_sb_event_receive_port_destroy(dev, card->irq);
fail_alloc_irq:
1783
	lv1_net_set_interrupt_status_indicator(bus_id(card),
1784 1785
					       bus_id(card),
					       0, 0);
1786
fail_status_indicator:
1787
	ps3_system_bus_set_drvdata(dev, NULL);
1788 1789 1790
	kfree(netdev_card(netdev)->unalign);
	free_netdev(netdev);
fail_alloc_card:
1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801
	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 已提交
1802
static int ps3_gelic_driver_remove(struct ps3_system_bus_device *dev)
1803
{
1804
	struct gelic_card *card = ps3_system_bus_get_drvdata(dev);
1805 1806 1807
	struct net_device *netdev0;
	pr_debug("%s: called\n", __func__);

1808 1809 1810
	/* set auto-negotiation */
	gelic_card_set_link_mode(card, GELIC_LV1_ETHER_AUTO_NEG);

1811 1812 1813
#ifdef CONFIG_GELIC_WIRELESS
	gelic_wl_driver_remove(card);
#endif
1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827
	/* 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 已提交
1828
	netdev0 = card->netdev[GELIC_PORT_ETHERNET_0];
1829 1830
	/* disconnect event port */
	free_irq(card->irq, card);
1831
	netdev0->irq = 0;
1832
	ps3_sb_event_receive_port_destroy(card->dev, card->irq);
1833 1834 1835 1836 1837 1838 1839

	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);

1840 1841 1842
	unregister_netdev(netdev0);
	kfree(netdev_card(netdev0)->unalign);
	free_netdev(netdev0);
1843

1844
	ps3_system_bus_set_drvdata(dev, NULL);
1845 1846 1847 1848 1849

	ps3_dma_region_free(dev->d_region);

	ps3_close_hv_device(dev);

1850
	pr_debug("%s: done\n", __func__);
1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
	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 已提交
1875 1876
module_init(ps3_gelic_driver_init);
module_exit(ps3_gelic_driver_exit);
1877 1878 1879

MODULE_ALIAS(PS3_MODULE_ALIAS_GELIC);