spider_net.c 69.1 KB
Newer Older
1
/*
2
 * Network device driver for Cell Processor-Based Blade and Celleb platform
3 4
 *
 * (C) Copyright IBM Corp. 2005
5
 * (C) Copyright 2006 TOSHIBA CORPORATION
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 31
 *
 * 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.
 */

#include <linux/compiler.h>
#include <linux/crc32.h>
#include <linux/delay.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/firmware.h>
#include <linux/if_vlan.h>
32
#include <linux/in.h>
33
#include <linux/init.h>
34
#include <linux/interrupt.h>
35
#include <linux/gfp.h>
36 37 38 39 40 41 42 43 44 45 46
#include <linux/ioport.h>
#include <linux/ip.h>
#include <linux/kernel.h>
#include <linux/mii.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/device.h>
#include <linux/pci.h>
#include <linux/skbuff.h>
#include <linux/tcp.h>
#include <linux/types.h>
47
#include <linux/vmalloc.h>
48 49
#include <linux/wait.h>
#include <linux/workqueue.h>
J
Jiri Slaby 已提交
50
#include <linux/bitops.h>
51 52 53 54 55 56 57 58
#include <net/checksum.h>

#include "spider_net.h"

MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com> and Jens Osterkamp " \
	      "<Jens.Osterkamp@de.ibm.com>");
MODULE_DESCRIPTION("Spider Southbridge Gigabit Ethernet driver");
MODULE_LICENSE("GPL");
59
MODULE_VERSION(VERSION);
60
MODULE_FIRMWARE(SPIDER_NET_FIRMWARE_NAME);
61 62 63 64

static int rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_DEFAULT;
static int tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_DEFAULT;

65 66
module_param(rx_descriptors, int, 0444);
module_param(tx_descriptors, int, 0444);
67 68 69 70 71 72 73 74

MODULE_PARM_DESC(rx_descriptors, "number of descriptors used " \
		 "in rx chains");
MODULE_PARM_DESC(tx_descriptors, "number of descriptors used " \
		 "in tx chain");

char spider_net_driver_name[] = "spidernet";

75
static const struct pci_device_id spider_net_pci_tbl[] = {
76 77 78 79 80 81 82 83 84 85 86 87 88 89
	{ PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_SPIDER_NET,
	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
	{ 0, }
};

MODULE_DEVICE_TABLE(pci, spider_net_pci_tbl);

/**
 * spider_net_read_reg - reads an SMMIO register of a card
 * @card: device structure
 * @reg: register to read from
 *
 * returns the content of the specified SMMIO register.
 */
90
static inline u32
91 92
spider_net_read_reg(struct spider_net_card *card, u32 reg)
{
93 94 95 96 97
	/* We use the powerpc specific variants instead of readl_be() because
	 * we know spidernet is not a real PCI device and we can thus avoid the
	 * performance hit caused by the PCI workarounds.
	 */
	return in_be32(card->regs + reg);
98 99 100 101 102 103 104 105
}

/**
 * spider_net_write_reg - writes to an SMMIO register of a card
 * @card: device structure
 * @reg: register to write to
 * @value: value to write into the specified SMMIO register
 */
106
static inline void
107 108
spider_net_write_reg(struct spider_net_card *card, u32 reg, u32 value)
{
109 110 111 112 113
	/* We use the powerpc specific variants instead of writel_be() because
	 * we know spidernet is not a real PCI device and we can thus avoid the
	 * performance hit caused by the PCI workarounds.
	 */
	out_be32(card->regs + reg, value);
114 115
}

116 117
/**
 * spider_net_write_phy - write to phy register
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
 * @netdev: adapter to be written to
 * @mii_id: id of MII
 * @reg: PHY register
 * @val: value to be written to phy register
 *
 * spider_net_write_phy_register writes to an arbitrary PHY
 * register via the spider GPCWOPCMD register. We assume the queue does
 * not run full (not more than 15 commands outstanding).
 **/
static void
spider_net_write_phy(struct net_device *netdev, int mii_id,
		     int reg, int val)
{
	struct spider_net_card *card = netdev_priv(netdev);
	u32 writevalue;

	writevalue = ((u32)mii_id << 21) |
		((u32)reg << 16) | ((u32)val);

	spider_net_write_reg(card, SPIDER_NET_GPCWOPCMD, writevalue);
}

140 141
/**
 * spider_net_read_phy - read from phy register
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
 * @netdev: network device to be read from
 * @mii_id: id of MII
 * @reg: PHY register
 *
 * Returns value read from PHY register
 *
 * spider_net_write_phy reads from an arbitrary PHY
 * register via the spider GPCROPCMD register
 **/
static int
spider_net_read_phy(struct net_device *netdev, int mii_id, int reg)
{
	struct spider_net_card *card = netdev_priv(netdev);
	u32 readvalue;

	readvalue = ((u32)mii_id << 21) | ((u32)reg << 16);
	spider_net_write_reg(card, SPIDER_NET_GPCROPCMD, readvalue);

	/* we don't use semaphores to wait for an SPIDER_NET_GPROPCMPINT
	 * interrupt, as we poll for the completion of the read operation
	 * in spider_net_read_phy. Should take about 50 us */
	do {
		readvalue = spider_net_read_reg(card, SPIDER_NET_GPCROPCMD);
	} while (readvalue & SPIDER_NET_GPREXEC);

	readvalue &= SPIDER_NET_GPRDAT_MASK;

	return readvalue;
}

172 173 174 175 176 177 178 179 180
/**
 * spider_net_setup_aneg - initial auto-negotiation setup
 * @card: device structure
 **/
static void
spider_net_setup_aneg(struct spider_net_card *card)
{
	struct mii_phy *phy = &card->phy;
	u32 advertise = 0;
181
	u16 bmsr, estat;
182

183 184
	bmsr  = spider_net_read_phy(card->netdev, phy->mii_id, MII_BMSR);
	estat = spider_net_read_phy(card->netdev, phy->mii_id, MII_ESTATUS);
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

	if (bmsr & BMSR_10HALF)
		advertise |= ADVERTISED_10baseT_Half;
	if (bmsr & BMSR_10FULL)
		advertise |= ADVERTISED_10baseT_Full;
	if (bmsr & BMSR_100HALF)
		advertise |= ADVERTISED_100baseT_Half;
	if (bmsr & BMSR_100FULL)
		advertise |= ADVERTISED_100baseT_Full;

	if ((bmsr & BMSR_ESTATEN) && (estat & ESTATUS_1000_TFULL))
		advertise |= SUPPORTED_1000baseT_Full;
	if ((bmsr & BMSR_ESTATEN) && (estat & ESTATUS_1000_THALF))
		advertise |= SUPPORTED_1000baseT_Half;

D
David S. Miller 已提交
200
	sungem_phy_probe(phy, phy->mii_id);
201 202 203 204
	phy->def->ops->setup_aneg(phy, advertise);

}

205
/**
206
 * spider_net_rx_irq_off - switch off rx irq on this spider card
207 208
 * @card: device structure
 *
209
 * switches off rx irq by masking them out in the GHIINTnMSK register
210 211
 */
static void
212
spider_net_rx_irq_off(struct spider_net_card *card)
213 214 215
{
	u32 regvalue;

216 217
	regvalue = SPIDER_NET_INT0_MASK_VALUE & (~SPIDER_NET_RXINT);
	spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, regvalue);
218 219 220
}

/**
221
 * spider_net_rx_irq_on - switch on rx irq on this spider card
222 223
 * @card: device structure
 *
224
 * switches on rx irq by enabling them in the GHIINTnMSK register
225 226
 */
static void
227
spider_net_rx_irq_on(struct spider_net_card *card)
228 229 230
{
	u32 regvalue;

231 232
	regvalue = SPIDER_NET_INT0_MASK_VALUE | SPIDER_NET_RXINT;
	spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, regvalue);
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
}

/**
 * spider_net_set_promisc - sets the unicast address or the promiscuous mode
 * @card: card structure
 *
 * spider_net_set_promisc sets the unicast destination address filter and
 * thus either allows for non-promisc mode or promisc mode
 */
static void
spider_net_set_promisc(struct spider_net_card *card)
{
	u32 macu, macl;
	struct net_device *netdev = card->netdev;

	if (netdev->flags & IFF_PROMISC) {
		/* clear destination entry 0 */
		spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR, 0);
		spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR + 0x04, 0);
		spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R,
				     SPIDER_NET_PROMISC_VALUE);
	} else {
		macu = netdev->dev_addr[0];
		macu <<= 8;
		macu |= netdev->dev_addr[1];
		memcpy(&macl, &netdev->dev_addr[2], sizeof(macl));

		macu |= SPIDER_NET_UA_DESCR_VALUE;
		spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR, macu);
		spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR + 0x04, macl);
		spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R,
				     SPIDER_NET_NONPROMISC_VALUE);
	}
}

/**
 * spider_net_get_descr_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
 */
274
static inline int
275
spider_net_get_descr_status(struct spider_net_hw_descr *hwdescr)
276
{
277
	return hwdescr->dmac_cmd_status & SPIDER_NET_DESCR_IND_PROC_MASK;
278 279 280 281 282 283 284 285 286 287 288 289 290 291
}

/**
 * spider_net_free_chain - free descriptor chain
 * @card: card structure
 * @chain: address of chain
 *
 */
static void
spider_net_free_chain(struct spider_net_card *card,
		      struct spider_net_descr_chain *chain)
{
	struct spider_net_descr *descr;

L
Linas Vepstas 已提交
292 293
	descr = chain->ring;
	do {
294
		descr->bus_addr = 0;
295
		descr->hwdescr->next_descr_addr = 0;
L
Linas Vepstas 已提交
296 297 298 299
		descr = descr->next;
	} while (descr != chain->ring);

	dma_free_coherent(&card->pdev->dev, chain->num_desc,
300
	    chain->hwring, chain->dma_addr);
301 302 303
}

/**
L
Linas Vepstas 已提交
304
 * spider_net_init_chain - alloc and link descriptor chain
305 306 307
 * @card: card structure
 * @chain: address of chain
 *
L
Linas Vepstas 已提交
308
 * We manage a circular list that mirrors the hardware structure,
309 310
 * except that the hardware uses bus addresses.
 *
L
Linas Vepstas 已提交
311
 * Returns 0 on success, <0 on failure
312 313 314
 */
static int
spider_net_init_chain(struct spider_net_card *card,
L
Linas Vepstas 已提交
315
		       struct spider_net_descr_chain *chain)
316 317 318
{
	int i;
	struct spider_net_descr *descr;
319
	struct spider_net_hw_descr *hwdescr;
320
	dma_addr_t buf;
L
Linas Vepstas 已提交
321
	size_t alloc_size;
322

323
	alloc_size = chain->num_desc * sizeof(struct spider_net_hw_descr);
324

325
	chain->hwring = dma_alloc_coherent(&card->pdev->dev, alloc_size,
326
					   &chain->dma_addr, GFP_KERNEL);
327
	if (!chain->hwring)
L
Linas Vepstas 已提交
328
		return -ENOMEM;
329

330
	memset(chain->ring, 0, chain->num_desc * sizeof(struct spider_net_descr));
L
Linas Vepstas 已提交
331 332

	/* Set up the hardware pointers in each descriptor */
333 334
	descr = chain->ring;
	hwdescr = chain->hwring;
L
Linas Vepstas 已提交
335
	buf = chain->dma_addr;
336 337 338
	for (i=0; i < chain->num_desc; i++, descr++, hwdescr++) {
		hwdescr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
		hwdescr->next_descr_addr = 0;
339

340
		descr->hwdescr = hwdescr;
341
		descr->bus_addr = buf;
342 343 344
		descr->next = descr + 1;
		descr->prev = descr - 1;

345
		buf += sizeof(struct spider_net_hw_descr);
346 347
	}
	/* do actual circular list */
L
Linas Vepstas 已提交
348 349
	(descr-1)->next = chain->ring;
	chain->ring->prev = descr-1;
350

351
	spin_lock_init(&chain->lock);
L
Linas Vepstas 已提交
352 353
	chain->head = chain->ring;
	chain->tail = chain->ring;
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
	return 0;
}

/**
 * spider_net_free_rx_chain_contents - frees descr contents in rx chain
 * @card: card structure
 *
 * returns 0 on success, <0 on failure
 */
static void
spider_net_free_rx_chain_contents(struct spider_net_card *card)
{
	struct spider_net_descr *descr;

	descr = card->rx_chain.head;
369
	do {
370
		if (descr->skb) {
371
			pci_unmap_single(card->pdev, descr->hwdescr->buf_addr,
372
					 SPIDER_NET_MAX_FRAME,
373
					 PCI_DMA_BIDIRECTIONAL);
374 375
			dev_kfree_skb(descr->skb);
			descr->skb = NULL;
376 377
		}
		descr = descr->next;
378
	} while (descr != card->rx_chain.head);
379 380 381
}

/**
L
Linas Vepstas 已提交
382
 * spider_net_prepare_rx_descr - Reinitialize RX descriptor
383 384 385
 * @card: card structure
 * @descr: descriptor to re-init
 *
386
 * Return 0 on success, <0 on failure.
387
 *
L
Linas Vepstas 已提交
388 389
 * Allocates a new rx skb, iommu-maps it and attaches it to the
 * descriptor. Mark the descriptor as activated, ready-to-use.
390 391 392 393 394
 */
static int
spider_net_prepare_rx_descr(struct spider_net_card *card,
			    struct spider_net_descr *descr)
{
395
	struct spider_net_hw_descr *hwdescr = descr->hwdescr;
396
	dma_addr_t buf;
397 398 399 400
	int offset;
	int bufsize;

	/* we need to round up the buffer size to a multiple of 128 */
401
	bufsize = (SPIDER_NET_MAX_FRAME + SPIDER_NET_RXBUF_ALIGN - 1) &
402 403 404 405 406
		(~(SPIDER_NET_RXBUF_ALIGN - 1));

	/* and we need to have it 128 byte aligned, therefore we allocate a
	 * bit more */
	/* allocate an skb */
407 408
	descr->skb = netdev_alloc_skb(card->netdev,
				      bufsize + SPIDER_NET_RXBUF_ALIGN - 1);
409
	if (!descr->skb) {
410
		if (netif_msg_rx_err(card) && net_ratelimit())
411 412
			dev_err(&card->netdev->dev,
			        "Not enough memory to allocate rx buffer\n");
413
		card->spider_stats.alloc_rx_skb_error++;
414 415
		return -ENOMEM;
	}
416 417 418 419 420
	hwdescr->buf_size = bufsize;
	hwdescr->result_size = 0;
	hwdescr->valid_size = 0;
	hwdescr->data_status = 0;
	hwdescr->data_error = 0;
421 422 423 424 425

	offset = ((unsigned long)descr->skb->data) &
		(SPIDER_NET_RXBUF_ALIGN - 1);
	if (offset)
		skb_reserve(descr->skb, SPIDER_NET_RXBUF_ALIGN - offset);
L
Linas Vepstas 已提交
426
	/* iommu-map the skb */
427
	buf = pci_map_single(card->pdev, descr->skb->data,
428
			SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
429
	if (pci_dma_mapping_error(card->pdev, buf)) {
430
		dev_kfree_skb_any(descr->skb);
431
		descr->skb = NULL;
432
		if (netif_msg_rx_err(card) && net_ratelimit())
433
			dev_err(&card->netdev->dev, "Could not iommu-map rx buffer\n");
434
		card->spider_stats.rx_iommu_map_error++;
435
		hwdescr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
436
	} else {
437
		hwdescr->buf_addr = buf;
L
Linas Vepstas 已提交
438
		wmb();
439
		hwdescr->dmac_cmd_status = SPIDER_NET_DESCR_CARDOWNED |
440
					 SPIDER_NET_DMAC_NOINTR_COMPLETE;
441 442
	}

L
Linas Vepstas 已提交
443
	return 0;
444 445 446
}

/**
447
 * spider_net_enable_rxchtails - sets RX dmac chain tail addresses
448 449
 * @card: card structure
 *
D
Daniel Mack 已提交
450
 * spider_net_enable_rxchtails sets the RX DMAC chain tail addresses in the
451 452 453
 * chip by writing to the appropriate register. DMA is enabled in
 * spider_net_enable_rxdmac.
 */
454
static inline void
455 456 457 458 459 460 461 462 463 464 465 466 467 468
spider_net_enable_rxchtails(struct spider_net_card *card)
{
	/* assume chain is aligned correctly */
	spider_net_write_reg(card, SPIDER_NET_GDADCHA ,
			     card->rx_chain.tail->bus_addr);
}

/**
 * spider_net_enable_rxdmac - enables a receive DMA controller
 * @card: card structure
 *
 * spider_net_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
 * in the GDADMACCNTR register
 */
469
static inline void
470 471
spider_net_enable_rxdmac(struct spider_net_card *card)
{
472
	wmb();
473 474 475 476
	spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
			     SPIDER_NET_DMA_RX_VALUE);
}

477 478 479 480 481 482 483 484 485 486 487 488 489 490
/**
 * spider_net_disable_rxdmac - disables the receive DMA controller
 * @card: card structure
 *
 * spider_net_disable_rxdmac terminates processing on the DMA controller
 * by turing off the DMA controller, with the force-end flag set.
 */
static inline void
spider_net_disable_rxdmac(struct spider_net_card *card)
{
	spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
			     SPIDER_NET_DMA_RX_FEND_VALUE);
}

491 492 493 494
/**
 * spider_net_refill_rx_chain - refills descriptors/skbs in the rx chains
 * @card: card structure
 *
495
 * refills descriptors in the rx chain: allocates skbs and iommu-maps them.
496 497 498 499
 */
static void
spider_net_refill_rx_chain(struct spider_net_card *card)
{
500 501
	struct spider_net_descr_chain *chain = &card->rx_chain;
	unsigned long flags;
502

503 504 505 506
	/* one context doing the refill (and a second context seeing that
	 * and omitting it) is ok. If called by NAPI, we'll be called again
	 * as spider_net_decode_one_descr is called several times. If some
	 * interrupt calls us, the NAPI is about to clean up anyway. */
507 508 509
	if (!spin_trylock_irqsave(&chain->lock, flags))
		return;

510
	while (spider_net_get_descr_status(chain->head->hwdescr) ==
511 512 513 514 515
			SPIDER_NET_DESCR_NOT_IN_USE) {
		if (spider_net_prepare_rx_descr(card, chain->head))
			break;
		chain->head = chain->head->next;
	}
516

517
	spin_unlock_irqrestore(&chain->lock, flags);
518 519 520
}

/**
L
Linas Vepstas 已提交
521
 * spider_net_alloc_rx_skbs - Allocates rx skbs in rx descriptor chains
522 523
 * @card: card structure
 *
L
Linas Vepstas 已提交
524
 * Returns 0 on success, <0 on failure.
525 526 527 528
 */
static int
spider_net_alloc_rx_skbs(struct spider_net_card *card)
{
529 530 531
	struct spider_net_descr_chain *chain = &card->rx_chain;
	struct spider_net_descr *start = chain->tail;
	struct spider_net_descr *descr = start;
532

533 534 535 536 537
	/* Link up the hardware chain pointers */
	do {
		descr->prev->hwdescr->next_descr_addr = descr->bus_addr;
		descr = descr->next;
	} while (descr != start);
538

L
Linas Vepstas 已提交
539 540 541
	/* Put at least one buffer into the chain. if this fails,
	 * we've got a problem. If not, spider_net_refill_rx_chain
	 * will do the rest at the end of this function. */
542 543 544 545 546
	if (spider_net_prepare_rx_descr(card, chain->head))
		goto error;
	else
		chain->head = chain->head->next;

L
Linas Vepstas 已提交
547 548
	/* This will allocate the rest of the rx buffers;
	 * if not, it's business as usual later on. */
549
	spider_net_refill_rx_chain(card);
550
	spider_net_enable_rxdmac(card);
551 552 553 554
	return 0;

error:
	spider_net_free_rx_chain_contents(card);
555
	return -ENOMEM;
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
}

/**
 * spider_net_get_multicast_hash - generates hash for multicast filter table
 * @addr: multicast address
 *
 * returns the hash value.
 *
 * spider_net_get_multicast_hash calculates a hash value for a given multicast
 * address, that is used to set the multicast filter tables
 */
static u8
spider_net_get_multicast_hash(struct net_device *netdev, __u8 *addr)
{
	u32 crc;
	u8 hash;
572 573 574 575 576 577 578
	char addr_for_crc[ETH_ALEN] = { 0, };
	int i, bit;

	for (i = 0; i < ETH_ALEN * 8; i++) {
		bit = (addr[i / 8] >> (i % 8)) & 1;
		addr_for_crc[ETH_ALEN - 1 - i / 8] += bit << (7 - (i % 8));
	}
579

580
	crc = crc32_be(~0, addr_for_crc, netdev->addr_len);
581 582 583 584

	hash = (crc >> 27);
	hash <<= 3;
	hash |= crc & 7;
585
	hash &= 0xff;
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600

	return hash;
}

/**
 * spider_net_set_multi - sets multicast addresses and promisc flags
 * @netdev: interface device structure
 *
 * spider_net_set_multi configures multicast addresses as needed for the
 * netdev interface. It also sets up multicast, allmulti and promisc
 * flags appropriately
 */
static void
spider_net_set_multi(struct net_device *netdev)
{
601
	struct netdev_hw_addr *ha;
602 603 604 605
	u8 hash;
	int i;
	u32 reg;
	struct spider_net_card *card = netdev_priv(netdev);
J
Joe Perches 已提交
606
	DECLARE_BITMAP(bitmask, SPIDER_NET_MULTICAST_HASHES) = {};
607 608 609 610 611 612 613 614 615 616 617 618 619 620

	spider_net_set_promisc(card);

	if (netdev->flags & IFF_ALLMULTI) {
		for (i = 0; i < SPIDER_NET_MULTICAST_HASHES; i++) {
			set_bit(i, bitmask);
		}
		goto write_hash;
	}

	/* well, we know, what the broadcast hash value is: it's xfd
	hash = spider_net_get_multicast_hash(netdev, netdev->broadcast); */
	set_bit(0xfd, bitmask);

621 622
	netdev_for_each_mc_addr(ha, netdev) {
		hash = spider_net_get_multicast_hash(netdev, ha->addr);
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
		set_bit(hash, bitmask);
	}

write_hash:
	for (i = 0; i < SPIDER_NET_MULTICAST_HASHES / 4; i++) {
		reg = 0;
		if (test_bit(i * 4, bitmask))
			reg += 0x08;
		reg <<= 8;
		if (test_bit(i * 4 + 1, bitmask))
			reg += 0x08;
		reg <<= 8;
		if (test_bit(i * 4 + 2, bitmask))
			reg += 0x08;
		reg <<= 8;
		if (test_bit(i * 4 + 3, bitmask))
			reg += 0x08;

		spider_net_write_reg(card, SPIDER_NET_GMRMHFILnR + i * 4, reg);
	}
}

/**
 * spider_net_prepare_tx_descr - fill tx descriptor with skb data
 * @card: card structure
 * @skb: packet to use
 *
 * returns 0 on success, <0 on failure.
 *
 * fills out the descriptor structure with skb data and len. Copies data,
 * if needed (32bit DMA!)
 */
static int
spider_net_prepare_tx_descr(struct spider_net_card *card,
			    struct sk_buff *skb)
{
659
	struct spider_net_descr_chain *chain = &card->tx_chain;
660
	struct spider_net_descr *descr;
661
	struct spider_net_hw_descr *hwdescr;
662
	dma_addr_t buf;
663
	unsigned long flags;
664

665
	buf = pci_map_single(card->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
666
	if (pci_dma_mapping_error(card->pdev, buf)) {
667
		if (netif_msg_tx_err(card) && net_ratelimit())
668
			dev_err(&card->netdev->dev, "could not iommu-map packet (%p, %i). "
669
				  "Dropping packet\n", skb->data, skb->len);
670
		card->spider_stats.tx_iommu_map_error++;
671 672 673
		return -ENOMEM;
	}

674
	spin_lock_irqsave(&chain->lock, flags);
675
	descr = card->tx_chain.head;
676 677 678 679 680
	if (descr->next == chain->tail->prev) {
		spin_unlock_irqrestore(&chain->lock, flags);
		pci_unmap_single(card->pdev, buf, skb->len, PCI_DMA_TODEVICE);
		return -ENOMEM;
	}
681
	hwdescr = descr->hwdescr;
682
	chain->head = descr->next;
683

684
	descr->skb = skb;
685 686 687 688
	hwdescr->buf_addr = buf;
	hwdescr->buf_size = skb->len;
	hwdescr->next_descr_addr = 0;
	hwdescr->data_status = 0;
689

690
	hwdescr->dmac_cmd_status =
L
Linas Vepstas 已提交
691
			SPIDER_NET_DESCR_CARDOWNED | SPIDER_NET_DMAC_TXFRMTL;
692
	spin_unlock_irqrestore(&chain->lock, flags);
693

694
	if (skb->ip_summed == CHECKSUM_PARTIAL)
695
		switch (ip_hdr(skb)->protocol) {
696
		case IPPROTO_TCP:
697
			hwdescr->dmac_cmd_status |= SPIDER_NET_DMAC_TCP;
698 699
			break;
		case IPPROTO_UDP:
700
			hwdescr->dmac_cmd_status |= SPIDER_NET_DMAC_UDP;
701 702 703
			break;
		}

704
	/* Chain the bus address, so that the DMA engine finds this descr. */
705 706
	wmb();
	descr->prev->hwdescr->next_descr_addr = descr->bus_addr;
707

708
	netif_trans_update(card->netdev); /* set netdev watchdog timer */
709 710 711
	return 0;
}

712
static int
713 714
spider_net_set_low_watermark(struct spider_net_card *card)
{
715 716
	struct spider_net_descr *descr = card->tx_chain.tail;
	struct spider_net_hw_descr *hwdescr;
717
	unsigned long flags;
718 719 720 721
	int status;
	int cnt=0;
	int i;

722 723
	/* Measure the length of the queue. Measurement does not
	 * need to be precise -- does not need a lock. */
724
	while (descr != card->tx_chain.head) {
725
		status = descr->hwdescr->dmac_cmd_status & SPIDER_NET_DESCR_NOT_IN_USE;
726 727 728 729 730 731 732
		if (status == SPIDER_NET_DESCR_NOT_IN_USE)
			break;
		descr = descr->next;
		cnt++;
	}

	/* If TX queue is short, don't even bother with interrupts */
L
Linas Vepstas 已提交
733
	if (cnt < card->tx_chain.num_desc/4)
734
		return cnt;
735 736 737 738 739 740 741 742

	/* Set low-watermark 3/4th's of the way into the queue. */
	descr = card->tx_chain.tail;
	cnt = (cnt*3)/4;
	for (i=0;i<cnt; i++)
		descr = descr->next;

	/* Set the new watermark, clear the old watermark */
743
	spin_lock_irqsave(&card->tx_chain.lock, flags);
744 745 746 747 748 749
	descr->hwdescr->dmac_cmd_status |= SPIDER_NET_DESCR_TXDESFLG;
	if (card->low_watermark && card->low_watermark != descr) {
		hwdescr = card->low_watermark->hwdescr;
		hwdescr->dmac_cmd_status =
		     hwdescr->dmac_cmd_status & ~SPIDER_NET_DESCR_TXDESFLG;
	}
750
	card->low_watermark = descr;
751
	spin_unlock_irqrestore(&card->tx_chain.lock, flags);
752
	return cnt;
753 754
}

755 756 757 758 759 760 761 762 763 764
/**
 * spider_net_release_tx_chain - processes sent tx descriptors
 * @card: adapter structure
 * @brutal: if set, don't care about whether descriptor seems to be in use
 *
 * returns 0 if the tx ring is empty, otherwise 1.
 *
 * spider_net_release_tx_chain releases the tx descriptors that spider has
 * finished with (if non-brutal) or simply release tx descriptors (if brutal).
 * If some other context is calling this function, we return 1 so that we're
765
 * scheduled again (if we were scheduled) and will not lose initiative.
766 767 768 769
 */
static int
spider_net_release_tx_chain(struct spider_net_card *card, int brutal)
{
770
	struct net_device *dev = card->netdev;
771
	struct spider_net_descr_chain *chain = &card->tx_chain;
772
	struct spider_net_descr *descr;
773
	struct spider_net_hw_descr *hwdescr;
774 775 776
	struct sk_buff *skb;
	u32 buf_addr;
	unsigned long flags;
777 778
	int status;

L
Linas Vepstas 已提交
779
	while (1) {
780
		spin_lock_irqsave(&chain->lock, flags);
L
Linas Vepstas 已提交
781 782 783 784
		if (chain->tail == chain->head) {
			spin_unlock_irqrestore(&chain->lock, flags);
			return 0;
		}
785
		descr = chain->tail;
786
		hwdescr = descr->hwdescr;
787

788
		status = spider_net_get_descr_status(hwdescr);
789 790
		switch (status) {
		case SPIDER_NET_DESCR_COMPLETE:
791 792
			dev->stats.tx_packets++;
			dev->stats.tx_bytes += descr->skb->len;
793 794 795
			break;

		case SPIDER_NET_DESCR_CARDOWNED:
796 797
			if (!brutal) {
				spin_unlock_irqrestore(&chain->lock, flags);
798
				return 1;
799 800
			}

801 802 803 804 805 806 807 808
			/* fallthrough, if we release the descriptors
			 * brutally (then we don't care about
			 * SPIDER_NET_DESCR_CARDOWNED) */

		case SPIDER_NET_DESCR_RESPONSE_ERROR:
		case SPIDER_NET_DESCR_PROTECTION_ERROR:
		case SPIDER_NET_DESCR_FORCE_END:
			if (netif_msg_tx_err(card))
809 810
				dev_err(&card->netdev->dev, "forcing end of tx descriptor "
				       "with status x%02x\n", status);
811
			dev->stats.tx_errors++;
812 813 814
			break;

		default:
815
			dev->stats.tx_dropped++;
816 817
			if (!brutal) {
				spin_unlock_irqrestore(&chain->lock, flags);
818
				return 1;
819
			}
820
		}
821

822
		chain->tail = descr->next;
823
		hwdescr->dmac_cmd_status |= SPIDER_NET_DESCR_NOT_IN_USE;
824
		skb = descr->skb;
825
		descr->skb = NULL;
826
		buf_addr = hwdescr->buf_addr;
827 828 829 830
		spin_unlock_irqrestore(&chain->lock, flags);

		/* unmap the skb */
		if (skb) {
831 832
			pci_unmap_single(card->pdev, buf_addr, skb->len,
					PCI_DMA_TODEVICE);
833
			dev_consume_skb_any(skb);
834 835
		}
	}
836 837 838 839 840 841 842
	return 0;
}

/**
 * spider_net_kick_tx_dma - enables TX DMA processing
 * @card: card structure
 *
843 844 845 846 847 848
 * This routine will start the transmit DMA running if
 * it is not already running. This routine ned only be
 * called when queueing a new packet to an empty tx queue.
 * Writes the current tx chain head as start address
 * of the tx descriptor chain and enables the transmission
 * DMA engine.
849
 */
850 851
static inline void
spider_net_kick_tx_dma(struct spider_net_card *card)
852
{
853
	struct spider_net_descr *descr;
854

855 856 857
	if (spider_net_read_reg(card, SPIDER_NET_GDTDMACCNTR) &
			SPIDER_NET_TX_DMA_EN)
		goto out;
858

859 860
	descr = card->tx_chain.tail;
	for (;;) {
861
		if (spider_net_get_descr_status(descr->hwdescr) ==
862 863 864 865 866 867 868 869 870 871 872 873 874 875
				SPIDER_NET_DESCR_CARDOWNED) {
			spider_net_write_reg(card, SPIDER_NET_GDTDCHA,
					descr->bus_addr);
			spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
					SPIDER_NET_DMA_TX_VALUE);
			break;
		}
		if (descr == card->tx_chain.head)
			break;
		descr = descr->next;
	}

out:
	mod_timer(&card->tx_timer, jiffies + SPIDER_NET_TX_TIMER);
876 877 878 879 880 881 882
}

/**
 * spider_net_xmit - transmits a frame over the device
 * @skb: packet to send out
 * @netdev: interface device structure
 *
883
 * returns 0 on success, !0 on failure
884 885 886 887
 */
static int
spider_net_xmit(struct sk_buff *skb, struct net_device *netdev)
{
888
	int cnt;
889
	struct spider_net_card *card = netdev_priv(netdev);
890

891
	spider_net_release_tx_chain(card, 0);
892

893
	if (spider_net_prepare_tx_descr(card, skb) != 0) {
894
		netdev->stats.tx_dropped++;
895 896
		netif_stop_queue(netdev);
		return NETDEV_TX_BUSY;
897
	}
898

899 900 901
	cnt = spider_net_set_low_watermark(card);
	if (cnt < 5)
		spider_net_kick_tx_dma(card);
902
	return NETDEV_TX_OK;
903
}
904

905 906 907 908
/**
 * spider_net_cleanup_tx_ring - cleans up the TX ring
 * @card: card structure
 *
909 910 911 912
 * spider_net_cleanup_tx_ring is called by either the tx_timer
 * or from the NAPI polling routine.
 * This routine releases resources associted with transmitted
 * packets, including updating the queue tail pointer.
913 914 915 916 917
 */
static void
spider_net_cleanup_tx_ring(struct spider_net_card *card)
{
	if ((spider_net_release_tx_chain(card, 0) != 0) &&
918
	    (card->netdev->flags & IFF_UP)) {
919
		spider_net_kick_tx_dma(card);
920 921
		netif_wake_queue(card->netdev);
	}
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
}

/**
 * spider_net_do_ioctl - called for device ioctls
 * @netdev: interface device structure
 * @ifr: request parameter structure for ioctl
 * @cmd: command code for ioctl
 *
 * returns 0 on success, <0 on failure. Currently, we have no special ioctls.
 * -EOPNOTSUPP is returned, if an unknown ioctl was requested
 */
static int
spider_net_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
{
	switch (cmd) {
	default:
		return -EOPNOTSUPP;
	}
}

/**
 * spider_net_pass_skb_up - takes an skb from a descriptor and passes it on
 * @descr: descriptor to process
 * @card: card structure
 *
L
Linas Vepstas 已提交
947 948
 * Fills out skb structure and passes the data to the stack.
 * The descriptor state is not changed.
949
 */
L
Linas Vepstas 已提交
950
static void
951
spider_net_pass_skb_up(struct spider_net_descr *descr,
L
Linas Vepstas 已提交
952
		       struct spider_net_card *card)
953
{
954 955 956 957 958
	struct spider_net_hw_descr *hwdescr = descr->hwdescr;
	struct sk_buff *skb = descr->skb;
	struct net_device *netdev = card->netdev;
	u32 data_status = hwdescr->data_status;
	u32 data_error = hwdescr->data_error;
959

960
	skb_put(skb, hwdescr->valid_size);
961 962 963 964 965 966 967 968

	/* the card seems to add 2 bytes of junk in front
	 * of the ethernet frame */
#define SPIDER_MISALIGN		2
	skb_pull(skb, SPIDER_MISALIGN);
	skb->protocol = eth_type_trans(skb, netdev);

	/* checksum offload */
969 970
	skb_checksum_none_assert(skb);
	if (netdev->features & NETIF_F_RXCSUM) {
971 972 973
		if ( ( (data_status & SPIDER_NET_DATA_STATUS_CKSUM_MASK) ==
		       SPIDER_NET_DATA_STATUS_CKSUM_MASK) &&
		     !(data_error & SPIDER_NET_DATA_ERR_CKSUM_MASK))
974
			skb->ip_summed = CHECKSUM_UNNECESSARY;
975
	}
976 977

	if (data_status & SPIDER_NET_VLAN_PACKET) {
978
		/* further enhancements: HW-accel VLAN */
979 980 981
	}

	/* update netdevice statistics */
982 983
	netdev->stats.rx_packets++;
	netdev->stats.rx_bytes += skb->len;
984 985 986

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

L
Linas Vepstas 已提交
989 990 991 992 993
static void show_rx_chain(struct spider_net_card *card)
{
	struct spider_net_descr_chain *chain = &card->rx_chain;
	struct spider_net_descr *start= chain->tail;
	struct spider_net_descr *descr= start;
994 995 996
	struct spider_net_hw_descr *hwd = start->hwdescr;
	struct device *dev = &card->netdev->dev;
	u32 curr_desc, next_desc;
L
Linas Vepstas 已提交
997 998
	int status;

999
	int tot = 0;
L
Linas Vepstas 已提交
1000
	int cnt = 0;
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
	int off = start - chain->ring;
	int cstat = hwd->dmac_cmd_status;

	dev_info(dev, "Total number of descrs=%d\n",
		chain->num_desc);
	dev_info(dev, "Chain tail located at descr=%d, status=0x%x\n",
		off, cstat);

	curr_desc = spider_net_read_reg(card, SPIDER_NET_GDACTDPA);
	next_desc = spider_net_read_reg(card, SPIDER_NET_GDACNEXTDA);

L
Linas Vepstas 已提交
1012 1013 1014
	status = cstat;
	do
	{
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
		hwd = descr->hwdescr;
		off = descr - chain->ring;
		status = hwd->dmac_cmd_status;

		if (descr == chain->head)
			dev_info(dev, "Chain head is at %d, head status=0x%x\n",
			         off, status);

		if (curr_desc == descr->bus_addr)
			dev_info(dev, "HW curr desc (GDACTDPA) is at %d, status=0x%x\n",
			         off, status);

		if (next_desc == descr->bus_addr)
			dev_info(dev, "HW next desc (GDACNEXTDA) is at %d, status=0x%x\n",
			         off, status);

		if (hwd->next_descr_addr == 0)
			dev_info(dev, "chain is cut at %d\n", off);

L
Linas Vepstas 已提交
1034
		if (cstat != status) {
1035 1036 1037 1038
			int from = (chain->num_desc + off - cnt) % chain->num_desc;
			int to = (chain->num_desc + off - 1) % chain->num_desc;
			dev_info(dev, "Have %d (from %d to %d) descrs "
			         "with stat=0x%08x\n", cnt, from, to, cstat);
L
Linas Vepstas 已提交
1039 1040 1041
			cstat = status;
			cnt = 0;
		}
1042

L
Linas Vepstas 已提交
1043
		cnt ++;
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
		tot ++;
		descr = descr->next;
	} while (descr != start);

	dev_info(dev, "Last %d descrs with stat=0x%08x "
	         "for a total of %d descrs\n", cnt, cstat, tot);

#ifdef DEBUG
	/* Now dump the whole ring */
	descr = start;
	do
	{
		struct spider_net_hw_descr *hwd = descr->hwdescr;
		status = spider_net_get_descr_status(hwd);
		cnt = descr - chain->ring;
		dev_info(dev, "Descr %d stat=0x%08x skb=%p\n",
		         cnt, status, descr->skb);
		dev_info(dev, "bus addr=%08x buf addr=%08x sz=%d\n",
		         descr->bus_addr, hwd->buf_addr, hwd->buf_size);
		dev_info(dev, "next=%08x result sz=%d valid sz=%d\n",
		         hwd->next_descr_addr, hwd->result_size,
		         hwd->valid_size);
		dev_info(dev, "dmac=%08x data stat=%08x data err=%08x\n",
		         hwd->dmac_cmd_status, hwd->data_status,
		         hwd->data_error);
		dev_info(dev, "\n");

L
Linas Vepstas 已提交
1071 1072 1073 1074
		descr = descr->next;
	} while (descr != start);
#endif

1075 1076
}

L
Linas Vepstas 已提交
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
/**
 * spider_net_resync_head_ptr - Advance head ptr past empty descrs
 *
 * If the driver fails to keep up and empty the queue, then the
 * hardware wil run out of room to put incoming packets. This
 * will cause the hardware to skip descrs that are full (instead
 * of halting/retrying). Thus, once the driver runs, it wil need
 * to "catch up" to where the hardware chain pointer is at.
 */
static void spider_net_resync_head_ptr(struct spider_net_card *card)
{
	unsigned long flags;
	struct spider_net_descr_chain *chain = &card->rx_chain;
	struct spider_net_descr *descr;
	int i, status;

	/* Advance head pointer past any empty descrs */
	descr = chain->head;
	status = spider_net_get_descr_status(descr->hwdescr);

	if (status == SPIDER_NET_DESCR_NOT_IN_USE)
		return;

	spin_lock_irqsave(&chain->lock, flags);

	descr = chain->head;
	status = spider_net_get_descr_status(descr->hwdescr);
	for (i=0; i<chain->num_desc; i++) {
		if (status != SPIDER_NET_DESCR_CARDOWNED) break;
		descr = descr->next;
		status = spider_net_get_descr_status(descr->hwdescr);
	}
	chain->head = descr;

	spin_unlock_irqrestore(&chain->lock, flags);
}

static int spider_net_resync_tail_ptr(struct spider_net_card *card)
{
	struct spider_net_descr_chain *chain = &card->rx_chain;
	struct spider_net_descr *descr;
	int i, status;

	/* Advance tail pointer past any empty and reaped descrs */
	descr = chain->tail;
	status = spider_net_get_descr_status(descr->hwdescr);

	for (i=0; i<chain->num_desc; i++) {
		if ((status != SPIDER_NET_DESCR_CARDOWNED) &&
		    (status != SPIDER_NET_DESCR_NOT_IN_USE)) break;
		descr = descr->next;
		status = spider_net_get_descr_status(descr->hwdescr);
	}
	chain->tail = descr;

	if ((i == chain->num_desc) || (i == 0))
		return 1;
	return 0;
}

1137
/**
L
Linas Vepstas 已提交
1138
 * spider_net_decode_one_descr - processes an RX descriptor
1139 1140
 * @card: card structure
 *
L
Linas Vepstas 已提交
1141
 * Returns 1 if a packet has been sent to the stack, otherwise 0.
1142
 *
L
Linas Vepstas 已提交
1143 1144 1145 1146
 * Processes an RX descriptor by iommu-unmapping the data buffer
 * and passing the packet up to the stack. This function is called
 * in softirq context, e.g. either bottom half from interrupt or
 * NAPI polling context.
1147 1148
 */
static int
L
Linas Vepstas 已提交
1149
spider_net_decode_one_descr(struct spider_net_card *card)
1150
{
1151
	struct net_device *dev = card->netdev;
1152 1153
	struct spider_net_descr_chain *chain = &card->rx_chain;
	struct spider_net_descr *descr = chain->tail;
1154
	struct spider_net_hw_descr *hwdescr = descr->hwdescr;
1155
	u32 hw_buf_addr;
1156
	int status;
1157

1158
	status = spider_net_get_descr_status(hwdescr);
1159

L
Linas Vepstas 已提交
1160 1161 1162
	/* Nothing in the descriptor, or ring must be empty */
	if ((status == SPIDER_NET_DESCR_CARDOWNED) ||
	    (status == SPIDER_NET_DESCR_NOT_IN_USE))
L
Linas Vepstas 已提交
1163
		return 0;
1164

1165
	/* descriptor definitively used -- move on tail */
1166 1167
	chain->tail = descr->next;

L
Linas Vepstas 已提交
1168
	/* unmap descriptor */
1169 1170 1171
	hw_buf_addr = hwdescr->buf_addr;
	hwdescr->buf_addr = 0xffffffff;
	pci_unmap_single(card->pdev, hw_buf_addr,
L
Linas Vepstas 已提交
1172 1173
			SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);

1174 1175 1176 1177
	if ( (status == SPIDER_NET_DESCR_RESPONSE_ERROR) ||
	     (status == SPIDER_NET_DESCR_PROTECTION_ERROR) ||
	     (status == SPIDER_NET_DESCR_FORCE_END) ) {
		if (netif_msg_rx_err(card))
1178
			dev_err(&dev->dev,
1179
			       "dropping RX descriptor with state %d\n", status);
1180
		dev->stats.rx_dropped++;
L
Linas Vepstas 已提交
1181
		goto bad_desc;
1182 1183 1184 1185
	}

	if ( (status != SPIDER_NET_DESCR_COMPLETE) &&
	     (status != SPIDER_NET_DESCR_FRAME_END) ) {
1186
		if (netif_msg_rx_err(card))
1187 1188
			dev_err(&card->netdev->dev,
			       "RX descriptor with unknown state %d\n", status);
1189
		card->spider_stats.rx_desc_unk_state++;
L
Linas Vepstas 已提交
1190
		goto bad_desc;
1191 1192
	}

L
Linas Vepstas 已提交
1193
	/* The cases we'll throw away the packet immediately */
1194
	if (hwdescr->data_error & SPIDER_NET_DESTROY_RX_FLAGS) {
L
Linas Vepstas 已提交
1195
		if (netif_msg_rx_err(card))
1196 1197
			dev_err(&card->netdev->dev,
			       "error in received descriptor found, "
L
Linas Vepstas 已提交
1198
			       "data_status=x%08x, data_error=x%08x\n",
1199
			       hwdescr->data_status, hwdescr->data_error);
L
Linas Vepstas 已提交
1200
		goto bad_desc;
L
Linas Vepstas 已提交
1201 1202
	}

1203
	if (hwdescr->dmac_cmd_status & SPIDER_NET_DESCR_BAD_STATUS) {
1204
		dev_err(&card->netdev->dev, "bad status, cmd_status=x%08x\n",
1205
			       hwdescr->dmac_cmd_status);
1206
		pr_err("buf_addr=x%08x\n", hw_buf_addr);
1207 1208 1209 1210 1211 1212
		pr_err("buf_size=x%08x\n", hwdescr->buf_size);
		pr_err("next_descr_addr=x%08x\n", hwdescr->next_descr_addr);
		pr_err("result_size=x%08x\n", hwdescr->result_size);
		pr_err("valid_size=x%08x\n", hwdescr->valid_size);
		pr_err("data_status=x%08x\n", hwdescr->data_status);
		pr_err("data_error=x%08x\n", hwdescr->data_error);
L
Linas Vepstas 已提交
1213 1214 1215 1216 1217 1218
		pr_err("which=%ld\n", descr - card->rx_chain.ring);

		card->spider_stats.rx_desc_error++;
		goto bad_desc;
	}

L
Linas Vepstas 已提交
1219 1220
	/* Ok, we've got a packet in descr */
	spider_net_pass_skb_up(descr, card);
1221
	descr->skb = NULL;
1222
	hwdescr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
L
Linas Vepstas 已提交
1223 1224 1225
	return 1;

bad_desc:
1226 1227
	if (netif_msg_rx_err(card))
		show_rx_chain(card);
L
Linas Vepstas 已提交
1228
	dev_kfree_skb_irq(descr->skb);
1229
	descr->skb = NULL;
1230
	hwdescr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
L
Linas Vepstas 已提交
1231
	return 0;
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
}

/**
 * spider_net_poll - NAPI poll function called by the stack to return packets
 * @netdev: interface device structure
 * @budget: number of packets we can pass to the stack at most
 *
 * returns 0 if no more packets available to the driver/stack. Returns 1,
 * if the quota is exceeded, but the driver has still packets.
 *
 * spider_net_poll returns all packets from the rx descriptors to the stack
 * (using netif_receive_skb). If all/enough packets are up, the driver
 * reenables interrupts and returns 0. If not, 1 is returned.
 */
1246
static int spider_net_poll(struct napi_struct *napi, int budget)
1247
{
1248 1249 1250 1251 1252
	struct spider_net_card *card = container_of(napi, struct spider_net_card, napi);
	int packets_done = 0;

	while (packets_done < budget) {
		if (!spider_net_decode_one_descr(card))
1253
			break;
1254 1255

		packets_done++;
1256 1257
	}

L
Linas Vepstas 已提交
1258
	if ((packets_done == 0) && (card->num_rx_ints != 0)) {
1259 1260
		if (!spider_net_resync_tail_ptr(card))
			packets_done = budget;
L
Linas Vepstas 已提交
1261 1262 1263 1264
		spider_net_resync_head_ptr(card);
	}
	card->num_rx_ints = 0;

1265
	spider_net_refill_rx_chain(card);
L
Linas Vepstas 已提交
1266
	spider_net_enable_rxdmac(card);
1267

L
Linas Vepstas 已提交
1268 1269
	spider_net_cleanup_tx_ring(card);

1270 1271
	/* if all packets are in the stack, enable interrupts and return 0 */
	/* if not, return 1 */
1272
	if (packets_done < budget) {
1273
		napi_complete(napi);
1274
		spider_net_rx_irq_on(card);
1275
		card->ignore_rx_ramfull = 0;
1276 1277
	}

1278
	return packets_done;
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
}

/**
 * spider_net_set_mac - sets the MAC of an interface
 * @netdev: interface device structure
 * @ptr: pointer to new MAC address
 *
 * Returns 0 on success, <0 on failure. Currently, we don't support this
 * and will always return EOPNOTSUPP.
 */
static int
spider_net_set_mac(struct net_device *netdev, void *p)
{
	struct spider_net_card *card = netdev_priv(netdev);
1293
	u32 macl, macu, regvalue;
1294 1295 1296 1297 1298
	struct sockaddr *addr = p;

	if (!is_valid_ether_addr(addr->sa_data))
		return -EADDRNOTAVAIL;

1299 1300
	memcpy(netdev->dev_addr, addr->sa_data, ETH_ALEN);

1301 1302 1303 1304 1305 1306
	/* switch off GMACTPE and GMACRPE */
	regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
	regvalue &= ~((1 << 5) | (1 << 6));
	spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);

	/* write mac */
1307 1308 1309
	macu = (netdev->dev_addr[0]<<24) + (netdev->dev_addr[1]<<16) +
		(netdev->dev_addr[2]<<8) + (netdev->dev_addr[3]);
	macl = (netdev->dev_addr[4]<<8) + (netdev->dev_addr[5]);
1310 1311 1312
	spider_net_write_reg(card, SPIDER_NET_GMACUNIMACU, macu);
	spider_net_write_reg(card, SPIDER_NET_GMACUNIMACL, macl);

1313 1314 1315 1316 1317
	/* switch GMACTPE and GMACRPE back on */
	regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
	regvalue |= ((1 << 5) | (1 << 6));
	spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);

1318 1319 1320 1321 1322
	spider_net_set_promisc(card);

	return 0;
}

1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
/**
 * spider_net_link_reset
 * @netdev: net device structure
 *
 * This is called when the PHY_LINK signal is asserted. For the blade this is
 * not connected so we should never get here.
 *
 */
static void
spider_net_link_reset(struct net_device *netdev)
{

	struct spider_net_card *card = netdev_priv(netdev);

	del_timer_sync(&card->aneg_timer);

	/* clear interrupt, block further interrupts */
	spider_net_write_reg(card, SPIDER_NET_GMACST,
			     spider_net_read_reg(card, SPIDER_NET_GMACST));
	spider_net_write_reg(card, SPIDER_NET_GMACINTEN, 0);

	/* reset phy and setup aneg */
1345 1346
	card->aneg_count = 0;
	card->medium = BCM54XX_COPPER;
1347 1348 1349 1350 1351
	spider_net_setup_aneg(card);
	mod_timer(&card->aneg_timer, jiffies + SPIDER_NET_ANEG_TIMER);

}

1352 1353 1354 1355 1356 1357 1358 1359 1360
/**
 * spider_net_handle_error_irq - handles errors raised by an interrupt
 * @card: card structure
 * @status_reg: interrupt status register 0 (GHIINT0STS)
 *
 * spider_net_handle_error_irq treats or ignores all error conditions
 * found when an interrupt is presented
 */
static void
1361 1362
spider_net_handle_error_irq(struct spider_net_card *card, u32 status_reg,
			    u32 error_reg1, u32 error_reg2)
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
{
	u32 i;
	int show_error = 1;

	/* check GHIINT0STS ************************************/
	if (status_reg)
		for (i = 0; i < 32; i++)
			if (status_reg & (1<<i))
				switch (i)
	{
	/* let error_reg1 and error_reg2 evaluation decide, what to do
	case SPIDER_NET_PHYINT:
	case SPIDER_NET_GMAC2INT:
	case SPIDER_NET_GMAC1INT:
	case SPIDER_NET_GFIFOINT:
	case SPIDER_NET_DMACINT:
	case SPIDER_NET_GSYSINT:
		break; */

1382 1383 1384 1385
	case SPIDER_NET_GIPSINT:
		show_error = 0;
		break;

1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
	case SPIDER_NET_GPWOPCMPINT:
		/* PHY write operation completed */
		show_error = 0;
		break;
	case SPIDER_NET_GPROPCMPINT:
		/* PHY read operation completed */
		/* we don't use semaphores, as we poll for the completion
		 * of the read operation in spider_net_read_phy. Should take
		 * about 50 us */
		show_error = 0;
		break;
	case SPIDER_NET_GPWFFINT:
		/* PHY command queue full */
		if (netif_msg_intr(card))
1400
			dev_err(&card->netdev->dev, "PHY write queue full\n");
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
		show_error = 0;
		break;

	/* case SPIDER_NET_GRMDADRINT: not used. print a message */
	/* case SPIDER_NET_GRMARPINT: not used. print a message */
	/* case SPIDER_NET_GRMMPINT: not used. print a message */

	case SPIDER_NET_GDTDEN0INT:
		/* someone has set TX_DMA_EN to 0 */
		show_error = 0;
		break;

	case SPIDER_NET_GDDDEN0INT: /* fallthrough */
	case SPIDER_NET_GDCDEN0INT: /* fallthrough */
	case SPIDER_NET_GDBDEN0INT: /* fallthrough */
	case SPIDER_NET_GDADEN0INT:
		/* someone has set RX_DMA_EN to 0 */
		show_error = 0;
		break;

	/* RX interrupts */
	case SPIDER_NET_GDDFDCINT:
	case SPIDER_NET_GDCFDCINT:
	case SPIDER_NET_GDBFDCINT:
	case SPIDER_NET_GDAFDCINT:
	/* case SPIDER_NET_GDNMINT: not used. print a message */
	/* case SPIDER_NET_GCNMINT: not used. print a message */
	/* case SPIDER_NET_GBNMINT: not used. print a message */
	/* case SPIDER_NET_GANMINT: not used. print a message */
	/* case SPIDER_NET_GRFNMINT: not used. print a message */
		show_error = 0;
		break;

	/* TX interrupts */
	case SPIDER_NET_GDTFDCINT:
		show_error = 0;
		break;
	case SPIDER_NET_GTTEDINT:
		show_error = 0;
		break;
	case SPIDER_NET_GDTDCEINT:
		/* chain end. If a descriptor should be sent, kick off
		 * tx dma
1444
		if (card->tx_chain.tail != card->tx_chain.head)
1445
			spider_net_kick_tx_dma(card);
1446 1447
		*/
		show_error = 0;
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
		break;

	/* case SPIDER_NET_G1TMCNTINT: not used. print a message */
	/* case SPIDER_NET_GFREECNTINT: not used. print a message */
	}

	/* check GHIINT1STS ************************************/
	if (error_reg1)
		for (i = 0; i < 32; i++)
			if (error_reg1 & (1<<i))
				switch (i)
	{
	case SPIDER_NET_GTMFLLINT:
1461 1462
		/* TX RAM full may happen on a usual case.
		 * Logging is not needed. */
1463 1464
		show_error = 0;
		break;
1465 1466 1467 1468
	case SPIDER_NET_GRFDFLLINT: /* fallthrough */
	case SPIDER_NET_GRFCFLLINT: /* fallthrough */
	case SPIDER_NET_GRFBFLLINT: /* fallthrough */
	case SPIDER_NET_GRFAFLLINT: /* fallthrough */
1469
	case SPIDER_NET_GRMFLLINT:
L
Linas Vepstas 已提交
1470
		/* Could happen when rx chain is full */
1471 1472 1473 1474 1475 1476
		if (card->ignore_rx_ramfull == 0) {
			card->ignore_rx_ramfull = 1;
			spider_net_resync_head_ptr(card);
			spider_net_refill_rx_chain(card);
			spider_net_enable_rxdmac(card);
			card->num_rx_ints ++;
1477
			napi_schedule(&card->napi);
1478
		}
1479
		show_error = 0;
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
		break;

	/* case SPIDER_NET_GTMSHTINT: problem, print a message */
	case SPIDER_NET_GDTINVDINT:
		/* allrighty. tx from previous descr ok */
		show_error = 0;
		break;

	/* chain end */
	case SPIDER_NET_GDDDCEINT: /* fallthrough */
	case SPIDER_NET_GDCDCEINT: /* fallthrough */
	case SPIDER_NET_GDBDCEINT: /* fallthrough */
	case SPIDER_NET_GDADCEINT:
L
Linas Vepstas 已提交
1493
		spider_net_resync_head_ptr(card);
1494
		spider_net_refill_rx_chain(card);
1495
		spider_net_enable_rxdmac(card);
L
Linas Vepstas 已提交
1496
		card->num_rx_ints ++;
1497
		napi_schedule(&card->napi);
1498 1499 1500 1501 1502 1503 1504 1505
		show_error = 0;
		break;

	/* invalid descriptor */
	case SPIDER_NET_GDDINVDINT: /* fallthrough */
	case SPIDER_NET_GDCINVDINT: /* fallthrough */
	case SPIDER_NET_GDBINVDINT: /* fallthrough */
	case SPIDER_NET_GDAINVDINT:
L
Linas Vepstas 已提交
1506 1507
		/* Could happen when rx chain is full */
		spider_net_resync_head_ptr(card);
1508
		spider_net_refill_rx_chain(card);
1509
		spider_net_enable_rxdmac(card);
L
Linas Vepstas 已提交
1510
		card->num_rx_ints ++;
1511
		napi_schedule(&card->napi);
1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
		show_error = 0;
		break;

	/* case SPIDER_NET_GDTRSERINT: problem, print a message */
	/* case SPIDER_NET_GDDRSERINT: problem, print a message */
	/* case SPIDER_NET_GDCRSERINT: problem, print a message */
	/* case SPIDER_NET_GDBRSERINT: problem, print a message */
	/* case SPIDER_NET_GDARSERINT: problem, print a message */
	/* case SPIDER_NET_GDSERINT: problem, print a message */
	/* case SPIDER_NET_GDTPTERINT: problem, print a message */
	/* case SPIDER_NET_GDDPTERINT: problem, print a message */
	/* case SPIDER_NET_GDCPTERINT: problem, print a message */
	/* case SPIDER_NET_GDBPTERINT: problem, print a message */
	/* case SPIDER_NET_GDAPTERINT: problem, print a message */
	default:
		show_error = 1;
		break;
	}

	/* check GHIINT2STS ************************************/
	if (error_reg2)
		for (i = 0; i < 32; i++)
			if (error_reg2 & (1<<i))
				switch (i)
	{
	/* there is nothing we can (want  to) do at this time. Log a
	 * message, we can switch on and off the specific values later on
	case SPIDER_NET_GPROPERINT:
	case SPIDER_NET_GMCTCRSNGINT:
	case SPIDER_NET_GMCTLCOLINT:
	case SPIDER_NET_GMCTTMOTINT:
	case SPIDER_NET_GMCRCAERINT:
	case SPIDER_NET_GMCRCALERINT:
	case SPIDER_NET_GMCRALNERINT:
	case SPIDER_NET_GMCROVRINT:
	case SPIDER_NET_GMCRRNTINT:
	case SPIDER_NET_GMCRRXERINT:
	case SPIDER_NET_GTITCSERINT:
	case SPIDER_NET_GTIFMTERINT:
	case SPIDER_NET_GTIPKTRVKINT:
	case SPIDER_NET_GTISPINGINT:
	case SPIDER_NET_GTISADNGINT:
	case SPIDER_NET_GTISPDNGINT:
	case SPIDER_NET_GRIFMTERINT:
	case SPIDER_NET_GRIPKTRVKINT:
	case SPIDER_NET_GRISPINGINT:
	case SPIDER_NET_GRISADNGINT:
	case SPIDER_NET_GRISPDNGINT:
		break;
	*/
		default:
			break;
	}

1566
	if ((show_error) && (netif_msg_intr(card)) && net_ratelimit())
1567
		dev_err(&card->netdev->dev, "Error interrupt, GHIINT0STS = 0x%08x, "
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
		       "GHIINT1STS = 0x%08x, GHIINT2STS = 0x%08x\n",
		       status_reg, error_reg1, error_reg2);

	/* clear interrupt sources */
	spider_net_write_reg(card, SPIDER_NET_GHIINT1STS, error_reg1);
	spider_net_write_reg(card, SPIDER_NET_GHIINT2STS, error_reg2);
}

/**
 * spider_net_interrupt - interrupt handler for spider_net
1578
 * @irq: interrupt number
1579 1580 1581 1582 1583 1584 1585 1586 1587
 * @ptr: pointer to net_device
 *
 * returns IRQ_HANDLED, if interrupt was for driver, or IRQ_NONE, if no
 * interrupt found raised by card.
 *
 * This is the interrupt handler, that turns off
 * interrupts for this device and makes the stack poll the driver
 */
static irqreturn_t
1588
spider_net_interrupt(int irq, void *ptr)
1589 1590 1591
{
	struct net_device *netdev = ptr;
	struct spider_net_card *card = netdev_priv(netdev);
1592
	u32 status_reg, error_reg1, error_reg2;
1593 1594

	status_reg = spider_net_read_reg(card, SPIDER_NET_GHIINT0STS);
1595 1596
	error_reg1 = spider_net_read_reg(card, SPIDER_NET_GHIINT1STS);
	error_reg2 = spider_net_read_reg(card, SPIDER_NET_GHIINT2STS);
1597

1598 1599 1600
	if (!(status_reg & SPIDER_NET_INT0_MASK_VALUE) &&
	    !(error_reg1 & SPIDER_NET_INT1_MASK_VALUE) &&
	    !(error_reg2 & SPIDER_NET_INT2_MASK_VALUE))
1601 1602 1603 1604
		return IRQ_NONE;

	if (status_reg & SPIDER_NET_RXINT ) {
		spider_net_rx_irq_off(card);
1605
		napi_schedule(&card->napi);
L
Linas Vepstas 已提交
1606
		card->num_rx_ints ++;
1607
	}
1608
	if (status_reg & SPIDER_NET_TXINT)
1609
		napi_schedule(&card->napi);
1610

1611 1612 1613
	if (status_reg & SPIDER_NET_LINKINT)
		spider_net_link_reset(netdev);

1614
	if (status_reg & SPIDER_NET_ERRINT )
1615 1616
		spider_net_handle_error_irq(card, status_reg,
					    error_reg1, error_reg2);
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634

	/* clear interrupt sources */
	spider_net_write_reg(card, SPIDER_NET_GHIINT0STS, status_reg);

	return IRQ_HANDLED;
}

#ifdef CONFIG_NET_POLL_CONTROLLER
/**
 * spider_net_poll_controller - artificial interrupt for netconsole etc.
 * @netdev: interface device structure
 *
 * see Documentation/networking/netconsole.txt
 */
static void
spider_net_poll_controller(struct net_device *netdev)
{
	disable_irq(netdev->irq);
1635
	spider_net_interrupt(netdev->irq, netdev);
1636 1637 1638 1639
	enable_irq(netdev->irq);
}
#endif /* CONFIG_NET_POLL_CONTROLLER */

1640 1641 1642 1643 1644 1645
/**
 * spider_net_enable_interrupts - enable interrupts
 * @card: card structure
 *
 * spider_net_enable_interrupt enables several interrupts
 */
1646
static void
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662
spider_net_enable_interrupts(struct spider_net_card *card)
{
	spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK,
			     SPIDER_NET_INT0_MASK_VALUE);
	spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK,
			     SPIDER_NET_INT1_MASK_VALUE);
	spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK,
			     SPIDER_NET_INT2_MASK_VALUE);
}

/**
 * spider_net_disable_interrupts - disable interrupts
 * @card: card structure
 *
 * spider_net_disable_interrupts disables all the interrupts
 */
1663
static void
1664 1665 1666 1667 1668 1669 1670 1671
spider_net_disable_interrupts(struct spider_net_card *card)
{
	spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, 0);
	spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK, 0);
	spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK, 0);
	spider_net_write_reg(card, SPIDER_NET_GMACINTEN, 0);
}

1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
/**
 * spider_net_init_card - initializes the card
 * @card: card structure
 *
 * spider_net_init_card initializes the card so that other registers can
 * be used
 */
static void
spider_net_init_card(struct spider_net_card *card)
{
	spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
			     SPIDER_NET_CKRCTRL_STOP_VALUE);

	spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
			     SPIDER_NET_CKRCTRL_RUN_VALUE);
1687 1688 1689 1690 1691

	/* trigger ETOMOD signal */
	spider_net_write_reg(card, SPIDER_NET_GMACOPEMD,
		spider_net_read_reg(card, SPIDER_NET_GMACOPEMD) | 0x4);

1692
	spider_net_disable_interrupts(card);
1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734
}

/**
 * spider_net_enable_card - enables the card by setting all kinds of regs
 * @card: card structure
 *
 * spider_net_enable_card sets a lot of SMMIO registers to enable the device
 */
static void
spider_net_enable_card(struct spider_net_card *card)
{
	int i;
	/* the following array consists of (register),(value) pairs
	 * that are set in this function. A register of 0 ends the list */
	u32 regs[][2] = {
		{ SPIDER_NET_GRESUMINTNUM, 0 },
		{ SPIDER_NET_GREINTNUM, 0 },

		/* set interrupt frame number registers */
		/* clear the single DMA engine registers first */
		{ SPIDER_NET_GFAFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
		{ SPIDER_NET_GFBFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
		{ SPIDER_NET_GFCFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
		{ SPIDER_NET_GFDFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
		/* then set, what we really need */
		{ SPIDER_NET_GFFRMNUM, SPIDER_NET_FRAMENUM_VALUE },

		/* timer counter registers and stuff */
		{ SPIDER_NET_GFREECNNUM, 0 },
		{ SPIDER_NET_GONETIMENUM, 0 },
		{ SPIDER_NET_GTOUTFRMNUM, 0 },

		/* RX mode setting */
		{ SPIDER_NET_GRXMDSET, SPIDER_NET_RXMODE_VALUE },
		/* TX mode setting */
		{ SPIDER_NET_GTXMDSET, SPIDER_NET_TXMODE_VALUE },
		/* IPSEC mode setting */
		{ SPIDER_NET_GIPSECINIT, SPIDER_NET_IPSECINIT_VALUE },

		{ SPIDER_NET_GFTRESTRT, SPIDER_NET_RESTART_VALUE },

		{ SPIDER_NET_GMRWOLCTRL, 0 },
1735 1736
		{ SPIDER_NET_GTESTMD, 0x10000000 },
		{ SPIDER_NET_GTTQMSK, 0x00400040 },
1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767

		{ SPIDER_NET_GMACINTEN, 0 },

		/* flow control stuff */
		{ SPIDER_NET_GMACAPAUSE, SPIDER_NET_MACAPAUSE_VALUE },
		{ SPIDER_NET_GMACTXPAUSE, SPIDER_NET_TXPAUSE_VALUE },

		{ SPIDER_NET_GMACBSTLMT, SPIDER_NET_BURSTLMT_VALUE },
		{ 0, 0}
	};

	i = 0;
	while (regs[i][0]) {
		spider_net_write_reg(card, regs[i][0], regs[i][1]);
		i++;
	}

	/* clear unicast filter table entries 1 to 14 */
	for (i = 1; i <= 14; i++) {
		spider_net_write_reg(card,
				     SPIDER_NET_GMRUAFILnR + i * 8,
				     0x00080000);
		spider_net_write_reg(card,
				     SPIDER_NET_GMRUAFILnR + i * 8 + 4,
				     0x00000000);
	}

	spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R, 0x08080000);

	spider_net_write_reg(card, SPIDER_NET_ECMODE, SPIDER_NET_ECMODE_VALUE);

D
Daniel Mack 已提交
1768
	/* set chain tail address for RX chains and
1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779
	 * enable DMA */
	spider_net_enable_rxchtails(card);
	spider_net_enable_rxdmac(card);

	spider_net_write_reg(card, SPIDER_NET_GRXDMAEN, SPIDER_NET_WOL_VALUE);

	spider_net_write_reg(card, SPIDER_NET_GMACLENLMT,
			     SPIDER_NET_LENLMT_VALUE);
	spider_net_write_reg(card, SPIDER_NET_GMACOPEMD,
			     SPIDER_NET_OPMODE_VALUE);

1780
	spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
1781
			     SPIDER_NET_GDTBSTA);
1782 1783
}

1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861
/**
 * spider_net_download_firmware - loads firmware into the adapter
 * @card: card structure
 * @firmware_ptr: pointer to firmware data
 *
 * spider_net_download_firmware loads the firmware data into the
 * adapter. It assumes the length etc. to be allright.
 */
static int
spider_net_download_firmware(struct spider_net_card *card,
			     const void *firmware_ptr)
{
	int sequencer, i;
	const u32 *fw_ptr = firmware_ptr;

	/* stop sequencers */
	spider_net_write_reg(card, SPIDER_NET_GSINIT,
			     SPIDER_NET_STOP_SEQ_VALUE);

	for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
	     sequencer++) {
		spider_net_write_reg(card,
				     SPIDER_NET_GSnPRGADR + sequencer * 8, 0);
		for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
			spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
					     sequencer * 8, *fw_ptr);
			fw_ptr++;
		}
	}

	if (spider_net_read_reg(card, SPIDER_NET_GSINIT))
		return -EIO;

	spider_net_write_reg(card, SPIDER_NET_GSINIT,
			     SPIDER_NET_RUN_SEQ_VALUE);

	return 0;
}

/**
 * spider_net_init_firmware - reads in firmware parts
 * @card: card structure
 *
 * Returns 0 on success, <0 on failure
 *
 * spider_net_init_firmware opens the sequencer firmware and does some basic
 * checks. This function opens and releases the firmware structure. A call
 * to download the firmware is performed before the release.
 *
 * Firmware format
 * ===============
 * spider_fw.bin is expected to be a file containing 6*1024*4 bytes, 4k being
 * the program for each sequencer. Use the command
 *    tail -q -n +2 Seq_code1_0x088.txt Seq_code2_0x090.txt              \
 *         Seq_code3_0x098.txt Seq_code4_0x0A0.txt Seq_code5_0x0A8.txt   \
 *         Seq_code6_0x0B0.txt | xxd -r -p -c4 > spider_fw.bin
 *
 * to generate spider_fw.bin, if you have sequencer programs with something
 * like the following contents for each sequencer:
 *    <ONE LINE COMMENT>
 *    <FIRST 4-BYTES-WORD FOR SEQUENCER>
 *    <SECOND 4-BYTES-WORD FOR SEQUENCER>
 *     ...
 *    <1024th 4-BYTES-WORD FOR SEQUENCER>
 */
static int
spider_net_init_firmware(struct spider_net_card *card)
{
	struct firmware *firmware = NULL;
	struct device_node *dn;
	const u8 *fw_prop = NULL;
	int err = -ENOENT;
	int fw_size;

	if (request_firmware((const struct firmware **)&firmware,
			     SPIDER_NET_FIRMWARE_NAME, &card->pdev->dev) == 0) {
		if ( (firmware->size != SPIDER_NET_FIRMWARE_LEN) &&
		     netif_msg_probe(card) ) {
1862 1863
			dev_err(&card->netdev->dev,
			       "Incorrect size of spidernet firmware in " \
1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880
			       "filesystem. Looking in host firmware...\n");
			goto try_host_fw;
		}
		err = spider_net_download_firmware(card, firmware->data);

		release_firmware(firmware);
		if (err)
			goto try_host_fw;

		goto done;
	}

try_host_fw:
	dn = pci_device_to_OF_node(card->pdev);
	if (!dn)
		goto out_err;

1881
	fw_prop = of_get_property(dn, "firmware", &fw_size);
1882 1883 1884 1885 1886
	if (!fw_prop)
		goto out_err;

	if ( (fw_size != SPIDER_NET_FIRMWARE_LEN) &&
	     netif_msg_probe(card) ) {
1887 1888
		dev_err(&card->netdev->dev,
		       "Incorrect size of spidernet firmware in host firmware\n");
1889 1890 1891 1892 1893 1894 1895 1896 1897
		goto done;
	}

	err = spider_net_download_firmware(card, fw_prop);

done:
	return err;
out_err:
	if (netif_msg_probe(card))
1898 1899
		dev_err(&card->netdev->dev,
		       "Couldn't find spidernet firmware in filesystem " \
1900 1901 1902 1903
		       "or host firmware\n");
	return err;
}

1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916
/**
 * spider_net_open - called upon ifonfig up
 * @netdev: interface device structure
 *
 * returns 0 on success, <0 on failure
 *
 * spider_net_open allocates all the descriptors and memory needed for
 * operation, sets up multicast list and enables interrupts
 */
int
spider_net_open(struct net_device *netdev)
{
	struct spider_net_card *card = netdev_priv(netdev);
L
Linas Vepstas 已提交
1917
	int result;
1918

1919 1920 1921 1922
	result = spider_net_init_firmware(card);
	if (result)
		goto init_firmware_failed;

1923
	/* start probing with copper */
1924 1925
	card->aneg_count = 0;
	card->medium = BCM54XX_COPPER;
1926 1927 1928 1929
	spider_net_setup_aneg(card);
	if (card->phy.def->phy_id)
		mod_timer(&card->aneg_timer, jiffies + SPIDER_NET_ANEG_TIMER);

L
Linas Vepstas 已提交
1930 1931
	result = spider_net_init_chain(card, &card->tx_chain);
	if (result)
1932
		goto alloc_tx_failed;
1933 1934
	card->low_watermark = NULL;

L
Linas Vepstas 已提交
1935 1936
	result = spider_net_init_chain(card, &card->rx_chain);
	if (result)
1937 1938
		goto alloc_rx_failed;

L
Linas Vepstas 已提交
1939
	/* Allocate rx skbs */
1940 1941
	result = spider_net_alloc_rx_skbs(card);
	if (result)
1942 1943 1944 1945 1946 1947 1948 1949
		goto alloc_skbs_failed;

	spider_net_set_multi(netdev);

	/* further enhancement: setup hw vlan, if needed */

	result = -EBUSY;
	if (request_irq(netdev->irq, spider_net_interrupt,
1950
			     IRQF_SHARED, netdev->name, netdev))
1951 1952 1953 1954
		goto register_int_failed;

	spider_net_enable_card(card);

1955 1956
	netif_start_queue(netdev);
	netif_carrier_on(netdev);
1957
	napi_enable(&card->napi);
1958

1959 1960
	spider_net_enable_interrupts(card);

1961 1962 1963 1964 1965 1966 1967 1968 1969
	return 0;

register_int_failed:
	spider_net_free_rx_chain_contents(card);
alloc_skbs_failed:
	spider_net_free_chain(card, &card->rx_chain);
alloc_rx_failed:
	spider_net_free_chain(card, &card->tx_chain);
alloc_tx_failed:
1970
	del_timer_sync(&card->aneg_timer);
1971
init_firmware_failed:
1972 1973 1974
	return result;
}

1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987
/**
 * spider_net_link_phy
 * @data: used for pointer to card structure
 *
 */
static void spider_net_link_phy(unsigned long data)
{
	struct spider_net_card *card = (struct spider_net_card *)data;
	struct mii_phy *phy = &card->phy;

	/* if link didn't come up after SPIDER_NET_ANEG_TIMEOUT tries, setup phy again */
	if (card->aneg_count > SPIDER_NET_ANEG_TIMEOUT) {

1988 1989
		pr_debug("%s: link is down trying to bring it up\n",
			 card->netdev->name);
1990

1991 1992
		switch (card->medium) {
		case BCM54XX_COPPER:
1993 1994 1995
			/* enable fiber with autonegotiation first */
			if (phy->def->ops->enable_fiber)
				phy->def->ops->enable_fiber(phy, 1);
1996
			card->medium = BCM54XX_FIBER;
1997 1998
			break;

1999
		case BCM54XX_FIBER:
2000 2001 2002
			/* fiber didn't come up, try to disable fiber autoneg */
			if (phy->def->ops->enable_fiber)
				phy->def->ops->enable_fiber(phy, 0);
2003
			card->medium = BCM54XX_UNKNOWN;
2004 2005
			break;

2006
		case BCM54XX_UNKNOWN:
2007 2008 2009
			/* copper, fiber with and without failed,
			 * retry from beginning */
			spider_net_setup_aneg(card);
2010
			card->medium = BCM54XX_COPPER;
2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039
			break;
		}

		card->aneg_count = 0;
		mod_timer(&card->aneg_timer, jiffies + SPIDER_NET_ANEG_TIMER);
		return;
	}

	/* link still not up, try again later */
	if (!(phy->def->ops->poll_link(phy))) {
		card->aneg_count++;
		mod_timer(&card->aneg_timer, jiffies + SPIDER_NET_ANEG_TIMER);
		return;
	}

	/* link came up, get abilities */
	phy->def->ops->read_link(phy);

	spider_net_write_reg(card, SPIDER_NET_GMACST,
			     spider_net_read_reg(card, SPIDER_NET_GMACST));
	spider_net_write_reg(card, SPIDER_NET_GMACINTEN, 0x4);

	if (phy->speed == 1000)
		spider_net_write_reg(card, SPIDER_NET_GMACMODE, 0x00000001);
	else
		spider_net_write_reg(card, SPIDER_NET_GMACMODE, 0);

	card->aneg_count = 0;

2040 2041 2042 2043
	pr_info("%s: link up, %i Mbps, %s-duplex %sautoneg.\n",
		card->netdev->name, phy->speed,
		phy->duplex == 1 ? "Full" : "Half",
		phy->autoneg == 1 ? "" : "no ");
2044 2045
}

2046 2047 2048 2049 2050 2051
/**
 * spider_net_setup_phy - setup PHY
 * @card: card structure
 *
 * returns 0 on success, <0 on failure
 *
2052
 * spider_net_setup_phy is used as part of spider_net_probe.
2053 2054 2055 2056 2057 2058 2059 2060 2061 2062
 **/
static int
spider_net_setup_phy(struct spider_net_card *card)
{
	struct mii_phy *phy = &card->phy;

	spider_net_write_reg(card, SPIDER_NET_GDTDMASEL,
			     SPIDER_NET_DMASEL_VALUE);
	spider_net_write_reg(card, SPIDER_NET_GPCCTRL,
			     SPIDER_NET_PHY_CTRL_VALUE);
2063

2064 2065 2066 2067
	phy->dev = card->netdev;
	phy->mdio_read = spider_net_read_phy;
	phy->mdio_write = spider_net_write_phy;

2068 2069 2070 2071
	for (phy->mii_id = 1; phy->mii_id <= 31; phy->mii_id++) {
		unsigned short id;
		id = spider_net_read_phy(card->netdev, phy->mii_id, MII_BMSR);
		if (id != 0x0000 && id != 0xffff) {
D
David S. Miller 已提交
2072
			if (!sungem_phy_probe(phy, phy->mii_id)) {
2073 2074 2075 2076 2077
				pr_info("Found %s.\n", phy->def->name);
				break;
			}
		}
	}
2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097

	return 0;
}

/**
 * spider_net_workaround_rxramfull - work around firmware bug
 * @card: card structure
 *
 * no return value
 **/
static void
spider_net_workaround_rxramfull(struct spider_net_card *card)
{
	int i, sequencer = 0;

	/* cancel reset */
	spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
			     SPIDER_NET_CKRCTRL_RUN_VALUE);

	/* empty sequencer data */
2098 2099
	for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
	     sequencer++) {
2100
		spider_net_write_reg(card, SPIDER_NET_GSnPRGADR +
2101
				     sequencer * 8, 0x0);
2102
		for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115
			spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
					     sequencer * 8, 0x0);
		}
	}

	/* set sequencer operation */
	spider_net_write_reg(card, SPIDER_NET_GSINIT, 0x000000fe);

	/* reset */
	spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
			     SPIDER_NET_CKRCTRL_STOP_VALUE);
}

2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126
/**
 * spider_net_stop - called upon ifconfig down
 * @netdev: interface device structure
 *
 * always returns 0
 */
int
spider_net_stop(struct net_device *netdev)
{
	struct spider_net_card *card = netdev_priv(netdev);

2127
	napi_disable(&card->napi);
2128 2129 2130
	netif_carrier_off(netdev);
	netif_stop_queue(netdev);
	del_timer_sync(&card->tx_timer);
2131
	del_timer_sync(&card->aneg_timer);
2132

2133
	spider_net_disable_interrupts(card);
2134

L
Linas Vepstas 已提交
2135
	free_irq(netdev->irq, netdev);
2136 2137 2138 2139 2140 2141 2142 2143

	spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
			     SPIDER_NET_DMA_TX_FEND_VALUE);

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

	/* release chains */
2144
	spider_net_release_tx_chain(card, 1);
L
Linas Vepstas 已提交
2145
	spider_net_free_rx_chain_contents(card);
2146 2147 2148 2149 2150 2151 2152

	spider_net_free_chain(card, &card->tx_chain);
	spider_net_free_chain(card, &card->rx_chain);

	return 0;
}

2153 2154 2155 2156 2157 2158 2159 2160
/**
 * spider_net_tx_timeout_task - task scheduled by the watchdog timeout
 * function (to be called not under interrupt status)
 * @data: data, is interface device structure
 *
 * called as task when tx hangs, resets interface (if interface is up)
 */
static void
D
David Howells 已提交
2161
spider_net_tx_timeout_task(struct work_struct *work)
2162
{
D
David Howells 已提交
2163 2164 2165
	struct spider_net_card *card =
		container_of(work, struct spider_net_card, tx_timeout_task);
	struct net_device *netdev = card->netdev;
2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179

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

	netif_device_detach(netdev);
	spider_net_stop(netdev);

	spider_net_workaround_rxramfull(card);
	spider_net_init_card(card);

	if (spider_net_setup_phy(card))
		goto out;

	spider_net_open(netdev);
2180
	spider_net_kick_tx_dma(card);
2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203
	netif_device_attach(netdev);

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

/**
 * spider_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
 */
static void
spider_net_tx_timeout(struct net_device *netdev)
{
	struct spider_net_card *card;

	card = netdev_priv(netdev);
	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);
2204
	card->spider_stats.tx_timeouts++;
2205 2206
}

2207
static const struct net_device_ops spider_net_ops = {
D
David S. Miller 已提交
2208 2209 2210
	.ndo_open		= spider_net_open,
	.ndo_stop		= spider_net_stop,
	.ndo_start_xmit		= spider_net_xmit,
2211
	.ndo_set_rx_mode	= spider_net_set_multi,
D
David S. Miller 已提交
2212 2213 2214
	.ndo_set_mac_address	= spider_net_set_mac,
	.ndo_do_ioctl		= spider_net_do_ioctl,
	.ndo_tx_timeout		= spider_net_tx_timeout,
2215
	.ndo_validate_addr	= eth_validate_addr,
2216 2217 2218
	/* HW VLAN */
#ifdef CONFIG_NET_POLL_CONTROLLER
	/* poll controller */
D
David S. Miller 已提交
2219
	.ndo_poll_controller	= spider_net_poll_controller,
2220 2221 2222
#endif /* CONFIG_NET_POLL_CONTROLLER */
};

2223 2224 2225 2226 2227 2228 2229 2230 2231
/**
 * spider_net_setup_netdev_ops - initialization of net_device operations
 * @netdev: net_device structure
 *
 * fills out function pointers in the net_device structure
 */
static void
spider_net_setup_netdev_ops(struct net_device *netdev)
{
2232
	netdev->netdev_ops = &spider_net_ops;
2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252
	netdev->watchdog_timeo = SPIDER_NET_WATCHDOG_TIMEOUT;
	/* ethtool ops */
	netdev->ethtool_ops = &spider_net_ethtool_ops;
}

/**
 * spider_net_setup_netdev - initialization of net_device
 * @card: card structure
 *
 * Returns 0 on success or <0 on failure
 *
 * spider_net_setup_netdev initializes the net_device structure
 **/
static int
spider_net_setup_netdev(struct spider_net_card *card)
{
	int result;
	struct net_device *netdev = card->netdev;
	struct device_node *dn;
	struct sockaddr addr;
2253
	const u8 *mac;
2254 2255 2256 2257

	SET_NETDEV_DEV(netdev, &card->pdev->dev);

	pci_set_drvdata(card->pdev, netdev);
2258 2259 2260 2261 2262

	init_timer(&card->tx_timer);
	card->tx_timer.function =
		(void (*)(unsigned long)) spider_net_cleanup_tx_ring;
	card->tx_timer.data = (unsigned long) card;
2263 2264
	netdev->irq = card->pdev->irq;

2265 2266 2267 2268 2269
	card->aneg_count = 0;
	init_timer(&card->aneg_timer);
	card->aneg_timer.function = spider_net_link_phy;
	card->aneg_timer.data = (unsigned long) card;

2270 2271 2272
	netif_napi_add(netdev, &card->napi,
		       spider_net_poll, SPIDER_NET_NAPI_WEIGHT);

2273 2274
	spider_net_setup_netdev_ops(netdev);

2275 2276 2277 2278
	netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM;
	if (SPIDER_NET_RX_CSUM_DEFAULT)
		netdev->features |= NETIF_F_RXCSUM;
	netdev->features |= NETIF_F_IP_CSUM | NETIF_F_LLTX;
2279 2280
	/* some time: NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
	 *		NETIF_F_HW_VLAN_CTAG_FILTER */
2281

2282 2283 2284 2285
	/* MTU range: 64 - 2294 */
	netdev->min_mtu = SPIDER_NET_MIN_MTU;
	netdev->max_mtu = SPIDER_NET_MAX_MTU;

2286
	netdev->irq = card->pdev->irq;
L
Linas Vepstas 已提交
2287
	card->num_rx_ints = 0;
2288
	card->ignore_rx_ramfull = 0;
2289 2290

	dn = pci_device_to_OF_node(card->pdev);
2291 2292 2293
	if (!dn)
		return -EIO;

2294
	mac = of_get_property(dn, "local-mac-address", NULL);
2295 2296
	if (!mac)
		return -EIO;
2297 2298 2299 2300
	memcpy(addr.sa_data, mac, ETH_ALEN);

	result = spider_net_set_mac(netdev, &addr);
	if ((result) && (netif_msg_probe(card)))
2301 2302
		dev_err(&card->netdev->dev,
		        "Failed to set MAC address: %i\n", result);
2303 2304 2305 2306

	result = register_netdev(netdev);
	if (result) {
		if (netif_msg_probe(card))
2307 2308
			dev_err(&card->netdev->dev,
			        "Couldn't register net_device: %i\n", result);
2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329
		return result;
	}

	if (netif_msg_probe(card))
		pr_info("Initialized device %s.\n", netdev->name);

	return 0;
}

/**
 * spider_net_alloc_card - allocates net_device and card structure
 *
 * returns the card structure or NULL in case of errors
 *
 * the card and net_device structures are linked to each other
 */
static struct spider_net_card *
spider_net_alloc_card(void)
{
	struct net_device *netdev;
	struct spider_net_card *card;
2330
	size_t alloc_size;
2331

2332 2333 2334
	alloc_size = sizeof(struct spider_net_card) +
	   (tx_descriptors + rx_descriptors) * sizeof(struct spider_net_descr);
	netdev = alloc_etherdev(alloc_size);
2335 2336 2337 2338 2339 2340
	if (!netdev)
		return NULL;

	card = netdev_priv(netdev);
	card->netdev = netdev;
	card->msg_enable = SPIDER_NET_DEFAULT_MSG;
D
David Howells 已提交
2341
	INIT_WORK(&card->tx_timeout_task, spider_net_tx_timeout_task);
2342 2343 2344
	init_waitqueue_head(&card->waitq);
	atomic_set(&card->tx_timeout_task_counter, 0);

2345 2346 2347 2348 2349
	card->rx_chain.num_desc = rx_descriptors;
	card->rx_chain.ring = card->darray;
	card->tx_chain.num_desc = tx_descriptors;
	card->tx_chain.ring = card->darray + rx_descriptors;

2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384
	return card;
}

/**
 * spider_net_undo_pci_setup - releases PCI ressources
 * @card: card structure
 *
 * spider_net_undo_pci_setup releases the mapped regions
 */
static void
spider_net_undo_pci_setup(struct spider_net_card *card)
{
	iounmap(card->regs);
	pci_release_regions(card->pdev);
}

/**
 * spider_net_setup_pci_dev - sets up the device in terms of PCI operations
 * @pdev: PCI device
 *
 * Returns the card structure or NULL if any errors occur
 *
 * spider_net_setup_pci_dev initializes pdev and together with the
 * functions called in spider_net_open configures the device so that
 * data can be transferred over it
 * The net_device structure is attached to the card structure, if the
 * function returns without error.
 **/
static struct spider_net_card *
spider_net_setup_pci_dev(struct pci_dev *pdev)
{
	struct spider_net_card *card;
	unsigned long mmio_start, mmio_len;

	if (pci_enable_device(pdev)) {
2385
		dev_err(&pdev->dev, "Couldn't enable PCI device\n");
2386 2387 2388 2389
		return NULL;
	}

	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
2390 2391
		dev_err(&pdev->dev,
		        "Couldn't find proper PCI device base address.\n");
2392 2393 2394 2395
		goto out_disable_dev;
	}

	if (pci_request_regions(pdev, spider_net_driver_name)) {
2396 2397
		dev_err(&pdev->dev,
		        "Couldn't obtain PCI resources, aborting.\n");
2398 2399 2400 2401 2402 2403 2404
		goto out_disable_dev;
	}

	pci_set_master(pdev);

	card = spider_net_alloc_card();
	if (!card) {
2405 2406
		dev_err(&pdev->dev,
		        "Couldn't allocate net_device structure, aborting.\n");
2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419
		goto out_release_regions;
	}
	card->pdev = pdev;

	/* fetch base address and length of first resource */
	mmio_start = pci_resource_start(pdev, 0);
	mmio_len = pci_resource_len(pdev, 0);

	card->netdev->mem_start = mmio_start;
	card->netdev->mem_end = mmio_start + mmio_len;
	card->regs = ioremap(mmio_start, mmio_len);

	if (!card->regs) {
2420 2421
		dev_err(&pdev->dev,
		        "Couldn't obtain PCI resources, aborting.\n");
2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443
		goto out_release_regions;
	}

	return card;

out_release_regions:
	pci_release_regions(pdev);
out_disable_dev:
	pci_disable_device(pdev);
	return NULL;
}

/**
 * spider_net_probe - initialization of a device
 * @pdev: PCI device
 * @ent: entry in the device id list
 *
 * Returns 0 on success, <0 on failure
 *
 * spider_net_probe initializes pdev and registers a net_device
 * structure for it. After that, the device can be ifconfig'ed up
 **/
2444
static int
2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482
spider_net_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
{
	int err = -EIO;
	struct spider_net_card *card;

	card = spider_net_setup_pci_dev(pdev);
	if (!card)
		goto out;

	spider_net_workaround_rxramfull(card);
	spider_net_init_card(card);

	err = spider_net_setup_phy(card);
	if (err)
		goto out_undo_pci;

	err = spider_net_setup_netdev(card);
	if (err)
		goto out_undo_pci;

	return 0;

out_undo_pci:
	spider_net_undo_pci_setup(card);
	free_netdev(card->netdev);
out:
	return err;
}

/**
 * spider_net_remove - removal of a device
 * @pdev: PCI device
 *
 * Returns 0 on success, <0 on failure
 *
 * spider_net_remove is called to remove the device and unregisters the
 * net_device
 **/
2483
static void
2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495
spider_net_remove(struct pci_dev *pdev)
{
	struct net_device *netdev;
	struct spider_net_card *card;

	netdev = pci_get_drvdata(pdev);
	card = netdev_priv(netdev);

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

	unregister_netdev(netdev);
2496 2497 2498 2499 2500 2501 2502

	/* switch off card */
	spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
			     SPIDER_NET_CKRCTRL_STOP_VALUE);
	spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
			     SPIDER_NET_CKRCTRL_RUN_VALUE);

2503 2504 2505 2506 2507 2508 2509 2510
	spider_net_undo_pci_setup(card);
	free_netdev(netdev);
}

static struct pci_driver spider_net_driver = {
	.name		= spider_net_driver_name,
	.id_table	= spider_net_pci_tbl,
	.probe		= spider_net_probe,
2511
	.remove		= spider_net_remove
2512 2513 2514 2515 2516 2517 2518 2519 2520
};

/**
 * spider_net_init - init function when the driver is loaded
 *
 * spider_net_init registers the device driver
 */
static int __init spider_net_init(void)
{
2521 2522
	printk(KERN_INFO "Spidernet version %s.\n", VERSION);

2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554
	if (rx_descriptors < SPIDER_NET_RX_DESCRIPTORS_MIN) {
		rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MIN;
		pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
	}
	if (rx_descriptors > SPIDER_NET_RX_DESCRIPTORS_MAX) {
		rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MAX;
		pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
	}
	if (tx_descriptors < SPIDER_NET_TX_DESCRIPTORS_MIN) {
		tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MIN;
		pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
	}
	if (tx_descriptors > SPIDER_NET_TX_DESCRIPTORS_MAX) {
		tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MAX;
		pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
	}

	return pci_register_driver(&spider_net_driver);
}

/**
 * spider_net_cleanup - exit function when driver is unloaded
 *
 * spider_net_cleanup unregisters the device driver
 */
static void __exit spider_net_cleanup(void)
{
	pci_unregister_driver(&spider_net_driver);
}

module_init(spider_net_init);
module_exit(spider_net_cleanup);