1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Network device driver for Cell Processor-Based Blade and Celleb platform
4  *
5  * (C) Copyright IBM Corp. 2005
6  * (C) Copyright 2006 TOSHIBA CORPORATION
7  *
8  * Authors : Utz Bacher <utz.bacher@de.ibm.com>
9  *           Jens Osterkamp <Jens.Osterkamp@de.ibm.com>
10  */
11 
12 #include <linux/compiler.h>
13 #include <linux/crc32.h>
14 #include <linux/delay.h>
15 #include <linux/etherdevice.h>
16 #include <linux/ethtool.h>
17 #include <linux/firmware.h>
18 #include <linux/if_vlan.h>
19 #include <linux/in.h>
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
22 #include <linux/gfp.h>
23 #include <linux/ioport.h>
24 #include <linux/ip.h>
25 #include <linux/kernel.h>
26 #include <linux/mii.h>
27 #include <linux/module.h>
28 #include <linux/netdevice.h>
29 #include <linux/device.h>
30 #include <linux/pci.h>
31 #include <linux/skbuff.h>
32 #include <linux/tcp.h>
33 #include <linux/types.h>
34 #include <linux/vmalloc.h>
35 #include <linux/wait.h>
36 #include <linux/workqueue.h>
37 #include <linux/bitops.h>
38 #include <net/checksum.h>
39 
40 #include "spider_net.h"
41 
42 MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com> and Jens Osterkamp " \
43 	      "<Jens.Osterkamp@de.ibm.com>");
44 MODULE_DESCRIPTION("Spider Southbridge Gigabit Ethernet driver");
45 MODULE_LICENSE("GPL");
46 MODULE_VERSION(VERSION);
47 MODULE_FIRMWARE(SPIDER_NET_FIRMWARE_NAME);
48 
49 static int rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_DEFAULT;
50 static int tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_DEFAULT;
51 
52 module_param(rx_descriptors, int, 0444);
53 module_param(tx_descriptors, int, 0444);
54 
55 MODULE_PARM_DESC(rx_descriptors, "number of descriptors used " \
56 		 "in rx chains");
57 MODULE_PARM_DESC(tx_descriptors, "number of descriptors used " \
58 		 "in tx chain");
59 
60 char spider_net_driver_name[] = "spidernet";
61 
62 static const struct pci_device_id spider_net_pci_tbl[] = {
63 	{ PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_SPIDER_NET,
64 	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
65 	{ 0, }
66 };
67 
68 MODULE_DEVICE_TABLE(pci, spider_net_pci_tbl);
69 
70 /**
71  * spider_net_read_reg - reads an SMMIO register of a card
72  * @card: device structure
73  * @reg: register to read from
74  *
75  * returns the content of the specified SMMIO register.
76  */
77 static inline u32
78 spider_net_read_reg(struct spider_net_card *card, u32 reg)
79 {
80 	/* We use the powerpc specific variants instead of readl_be() because
81 	 * we know spidernet is not a real PCI device and we can thus avoid the
82 	 * performance hit caused by the PCI workarounds.
83 	 */
84 	return in_be32(card->regs + reg);
85 }
86 
87 /**
88  * spider_net_write_reg - writes to an SMMIO register of a card
89  * @card: device structure
90  * @reg: register to write to
91  * @value: value to write into the specified SMMIO register
92  */
93 static inline void
94 spider_net_write_reg(struct spider_net_card *card, u32 reg, u32 value)
95 {
96 	/* We use the powerpc specific variants instead of writel_be() because
97 	 * we know spidernet is not a real PCI device and we can thus avoid the
98 	 * performance hit caused by the PCI workarounds.
99 	 */
100 	out_be32(card->regs + reg, value);
101 }
102 
103 /**
104  * spider_net_write_phy - write to phy register
105  * @netdev: adapter to be written to
106  * @mii_id: id of MII
107  * @reg: PHY register
108  * @val: value to be written to phy register
109  *
110  * spider_net_write_phy_register writes to an arbitrary PHY
111  * register via the spider GPCWOPCMD register. We assume the queue does
112  * not run full (not more than 15 commands outstanding).
113  **/
114 static void
115 spider_net_write_phy(struct net_device *netdev, int mii_id,
116 		     int reg, int val)
117 {
118 	struct spider_net_card *card = netdev_priv(netdev);
119 	u32 writevalue;
120 
121 	writevalue = ((u32)mii_id << 21) |
122 		((u32)reg << 16) | ((u32)val);
123 
124 	spider_net_write_reg(card, SPIDER_NET_GPCWOPCMD, writevalue);
125 }
126 
127 /**
128  * spider_net_read_phy - read from phy register
129  * @netdev: network device to be read from
130  * @mii_id: id of MII
131  * @reg: PHY register
132  *
133  * Returns value read from PHY register
134  *
135  * spider_net_write_phy reads from an arbitrary PHY
136  * register via the spider GPCROPCMD register
137  **/
138 static int
139 spider_net_read_phy(struct net_device *netdev, int mii_id, int reg)
140 {
141 	struct spider_net_card *card = netdev_priv(netdev);
142 	u32 readvalue;
143 
144 	readvalue = ((u32)mii_id << 21) | ((u32)reg << 16);
145 	spider_net_write_reg(card, SPIDER_NET_GPCROPCMD, readvalue);
146 
147 	/* we don't use semaphores to wait for an SPIDER_NET_GPROPCMPINT
148 	 * interrupt, as we poll for the completion of the read operation
149 	 * in spider_net_read_phy. Should take about 50 us */
150 	do {
151 		readvalue = spider_net_read_reg(card, SPIDER_NET_GPCROPCMD);
152 	} while (readvalue & SPIDER_NET_GPREXEC);
153 
154 	readvalue &= SPIDER_NET_GPRDAT_MASK;
155 
156 	return readvalue;
157 }
158 
159 /**
160  * spider_net_setup_aneg - initial auto-negotiation setup
161  * @card: device structure
162  **/
163 static void
164 spider_net_setup_aneg(struct spider_net_card *card)
165 {
166 	struct mii_phy *phy = &card->phy;
167 	u32 advertise = 0;
168 	u16 bmsr, estat;
169 
170 	bmsr  = spider_net_read_phy(card->netdev, phy->mii_id, MII_BMSR);
171 	estat = spider_net_read_phy(card->netdev, phy->mii_id, MII_ESTATUS);
172 
173 	if (bmsr & BMSR_10HALF)
174 		advertise |= ADVERTISED_10baseT_Half;
175 	if (bmsr & BMSR_10FULL)
176 		advertise |= ADVERTISED_10baseT_Full;
177 	if (bmsr & BMSR_100HALF)
178 		advertise |= ADVERTISED_100baseT_Half;
179 	if (bmsr & BMSR_100FULL)
180 		advertise |= ADVERTISED_100baseT_Full;
181 
182 	if ((bmsr & BMSR_ESTATEN) && (estat & ESTATUS_1000_TFULL))
183 		advertise |= SUPPORTED_1000baseT_Full;
184 	if ((bmsr & BMSR_ESTATEN) && (estat & ESTATUS_1000_THALF))
185 		advertise |= SUPPORTED_1000baseT_Half;
186 
187 	sungem_phy_probe(phy, phy->mii_id);
188 	phy->def->ops->setup_aneg(phy, advertise);
189 
190 }
191 
192 /**
193  * spider_net_rx_irq_off - switch off rx irq on this spider card
194  * @card: device structure
195  *
196  * switches off rx irq by masking them out in the GHIINTnMSK register
197  */
198 static void
199 spider_net_rx_irq_off(struct spider_net_card *card)
200 {
201 	u32 regvalue;
202 
203 	regvalue = SPIDER_NET_INT0_MASK_VALUE & (~SPIDER_NET_RXINT);
204 	spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, regvalue);
205 }
206 
207 /**
208  * spider_net_rx_irq_on - switch on rx irq on this spider card
209  * @card: device structure
210  *
211  * switches on rx irq by enabling them in the GHIINTnMSK register
212  */
213 static void
214 spider_net_rx_irq_on(struct spider_net_card *card)
215 {
216 	u32 regvalue;
217 
218 	regvalue = SPIDER_NET_INT0_MASK_VALUE | SPIDER_NET_RXINT;
219 	spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, regvalue);
220 }
221 
222 /**
223  * spider_net_set_promisc - sets the unicast address or the promiscuous mode
224  * @card: card structure
225  *
226  * spider_net_set_promisc sets the unicast destination address filter and
227  * thus either allows for non-promisc mode or promisc mode
228  */
229 static void
230 spider_net_set_promisc(struct spider_net_card *card)
231 {
232 	u32 macu, macl;
233 	struct net_device *netdev = card->netdev;
234 
235 	if (netdev->flags & IFF_PROMISC) {
236 		/* clear destination entry 0 */
237 		spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR, 0);
238 		spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR + 0x04, 0);
239 		spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R,
240 				     SPIDER_NET_PROMISC_VALUE);
241 	} else {
242 		macu = netdev->dev_addr[0];
243 		macu <<= 8;
244 		macu |= netdev->dev_addr[1];
245 		memcpy(&macl, &netdev->dev_addr[2], sizeof(macl));
246 
247 		macu |= SPIDER_NET_UA_DESCR_VALUE;
248 		spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR, macu);
249 		spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR + 0x04, macl);
250 		spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R,
251 				     SPIDER_NET_NONPROMISC_VALUE);
252 	}
253 }
254 
255 /**
256  * spider_net_get_descr_status -- returns the status of a descriptor
257  * @descr: descriptor to look at
258  *
259  * returns the status as in the dmac_cmd_status field of the descriptor
260  */
261 static inline int
262 spider_net_get_descr_status(struct spider_net_hw_descr *hwdescr)
263 {
264 	return hwdescr->dmac_cmd_status & SPIDER_NET_DESCR_IND_PROC_MASK;
265 }
266 
267 /**
268  * spider_net_free_chain - free descriptor chain
269  * @card: card structure
270  * @chain: address of chain
271  *
272  */
273 static void
274 spider_net_free_chain(struct spider_net_card *card,
275 		      struct spider_net_descr_chain *chain)
276 {
277 	struct spider_net_descr *descr;
278 
279 	descr = chain->ring;
280 	do {
281 		descr->bus_addr = 0;
282 		descr->hwdescr->next_descr_addr = 0;
283 		descr = descr->next;
284 	} while (descr != chain->ring);
285 
286 	dma_free_coherent(&card->pdev->dev, chain->num_desc,
287 	    chain->hwring, chain->dma_addr);
288 }
289 
290 /**
291  * spider_net_init_chain - alloc and link descriptor chain
292  * @card: card structure
293  * @chain: address of chain
294  *
295  * We manage a circular list that mirrors the hardware structure,
296  * except that the hardware uses bus addresses.
297  *
298  * Returns 0 on success, <0 on failure
299  */
300 static int
301 spider_net_init_chain(struct spider_net_card *card,
302 		       struct spider_net_descr_chain *chain)
303 {
304 	int i;
305 	struct spider_net_descr *descr;
306 	struct spider_net_hw_descr *hwdescr;
307 	dma_addr_t buf;
308 	size_t alloc_size;
309 
310 	alloc_size = chain->num_desc * sizeof(struct spider_net_hw_descr);
311 
312 	chain->hwring = dma_alloc_coherent(&card->pdev->dev, alloc_size,
313 					   &chain->dma_addr, GFP_KERNEL);
314 	if (!chain->hwring)
315 		return -ENOMEM;
316 
317 	memset(chain->ring, 0, chain->num_desc * sizeof(struct spider_net_descr));
318 
319 	/* Set up the hardware pointers in each descriptor */
320 	descr = chain->ring;
321 	hwdescr = chain->hwring;
322 	buf = chain->dma_addr;
323 	for (i=0; i < chain->num_desc; i++, descr++, hwdescr++) {
324 		hwdescr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
325 		hwdescr->next_descr_addr = 0;
326 
327 		descr->hwdescr = hwdescr;
328 		descr->bus_addr = buf;
329 		descr->next = descr + 1;
330 		descr->prev = descr - 1;
331 
332 		buf += sizeof(struct spider_net_hw_descr);
333 	}
334 	/* do actual circular list */
335 	(descr-1)->next = chain->ring;
336 	chain->ring->prev = descr-1;
337 
338 	spin_lock_init(&chain->lock);
339 	chain->head = chain->ring;
340 	chain->tail = chain->ring;
341 	return 0;
342 }
343 
344 /**
345  * spider_net_free_rx_chain_contents - frees descr contents in rx chain
346  * @card: card structure
347  *
348  * returns 0 on success, <0 on failure
349  */
350 static void
351 spider_net_free_rx_chain_contents(struct spider_net_card *card)
352 {
353 	struct spider_net_descr *descr;
354 
355 	descr = card->rx_chain.head;
356 	do {
357 		if (descr->skb) {
358 			pci_unmap_single(card->pdev, descr->hwdescr->buf_addr,
359 					 SPIDER_NET_MAX_FRAME,
360 					 PCI_DMA_BIDIRECTIONAL);
361 			dev_kfree_skb(descr->skb);
362 			descr->skb = NULL;
363 		}
364 		descr = descr->next;
365 	} while (descr != card->rx_chain.head);
366 }
367 
368 /**
369  * spider_net_prepare_rx_descr - Reinitialize RX descriptor
370  * @card: card structure
371  * @descr: descriptor to re-init
372  *
373  * Return 0 on success, <0 on failure.
374  *
375  * Allocates a new rx skb, iommu-maps it and attaches it to the
376  * descriptor. Mark the descriptor as activated, ready-to-use.
377  */
378 static int
379 spider_net_prepare_rx_descr(struct spider_net_card *card,
380 			    struct spider_net_descr *descr)
381 {
382 	struct spider_net_hw_descr *hwdescr = descr->hwdescr;
383 	dma_addr_t buf;
384 	int offset;
385 	int bufsize;
386 
387 	/* we need to round up the buffer size to a multiple of 128 */
388 	bufsize = (SPIDER_NET_MAX_FRAME + SPIDER_NET_RXBUF_ALIGN - 1) &
389 		(~(SPIDER_NET_RXBUF_ALIGN - 1));
390 
391 	/* and we need to have it 128 byte aligned, therefore we allocate a
392 	 * bit more */
393 	/* allocate an skb */
394 	descr->skb = netdev_alloc_skb(card->netdev,
395 				      bufsize + SPIDER_NET_RXBUF_ALIGN - 1);
396 	if (!descr->skb) {
397 		if (netif_msg_rx_err(card) && net_ratelimit())
398 			dev_err(&card->netdev->dev,
399 			        "Not enough memory to allocate rx buffer\n");
400 		card->spider_stats.alloc_rx_skb_error++;
401 		return -ENOMEM;
402 	}
403 	hwdescr->buf_size = bufsize;
404 	hwdescr->result_size = 0;
405 	hwdescr->valid_size = 0;
406 	hwdescr->data_status = 0;
407 	hwdescr->data_error = 0;
408 
409 	offset = ((unsigned long)descr->skb->data) &
410 		(SPIDER_NET_RXBUF_ALIGN - 1);
411 	if (offset)
412 		skb_reserve(descr->skb, SPIDER_NET_RXBUF_ALIGN - offset);
413 	/* iommu-map the skb */
414 	buf = pci_map_single(card->pdev, descr->skb->data,
415 			SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
416 	if (pci_dma_mapping_error(card->pdev, buf)) {
417 		dev_kfree_skb_any(descr->skb);
418 		descr->skb = NULL;
419 		if (netif_msg_rx_err(card) && net_ratelimit())
420 			dev_err(&card->netdev->dev, "Could not iommu-map rx buffer\n");
421 		card->spider_stats.rx_iommu_map_error++;
422 		hwdescr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
423 	} else {
424 		hwdescr->buf_addr = buf;
425 		wmb();
426 		hwdescr->dmac_cmd_status = SPIDER_NET_DESCR_CARDOWNED |
427 					 SPIDER_NET_DMAC_NOINTR_COMPLETE;
428 	}
429 
430 	return 0;
431 }
432 
433 /**
434  * spider_net_enable_rxchtails - sets RX dmac chain tail addresses
435  * @card: card structure
436  *
437  * spider_net_enable_rxchtails sets the RX DMAC chain tail addresses in the
438  * chip by writing to the appropriate register. DMA is enabled in
439  * spider_net_enable_rxdmac.
440  */
441 static inline void
442 spider_net_enable_rxchtails(struct spider_net_card *card)
443 {
444 	/* assume chain is aligned correctly */
445 	spider_net_write_reg(card, SPIDER_NET_GDADCHA ,
446 			     card->rx_chain.tail->bus_addr);
447 }
448 
449 /**
450  * spider_net_enable_rxdmac - enables a receive DMA controller
451  * @card: card structure
452  *
453  * spider_net_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
454  * in the GDADMACCNTR register
455  */
456 static inline void
457 spider_net_enable_rxdmac(struct spider_net_card *card)
458 {
459 	wmb();
460 	spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
461 			     SPIDER_NET_DMA_RX_VALUE);
462 }
463 
464 /**
465  * spider_net_disable_rxdmac - disables the receive DMA controller
466  * @card: card structure
467  *
468  * spider_net_disable_rxdmac terminates processing on the DMA controller
469  * by turing off the DMA controller, with the force-end flag set.
470  */
471 static inline void
472 spider_net_disable_rxdmac(struct spider_net_card *card)
473 {
474 	spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
475 			     SPIDER_NET_DMA_RX_FEND_VALUE);
476 }
477 
478 /**
479  * spider_net_refill_rx_chain - refills descriptors/skbs in the rx chains
480  * @card: card structure
481  *
482  * refills descriptors in the rx chain: allocates skbs and iommu-maps them.
483  */
484 static void
485 spider_net_refill_rx_chain(struct spider_net_card *card)
486 {
487 	struct spider_net_descr_chain *chain = &card->rx_chain;
488 	unsigned long flags;
489 
490 	/* one context doing the refill (and a second context seeing that
491 	 * and omitting it) is ok. If called by NAPI, we'll be called again
492 	 * as spider_net_decode_one_descr is called several times. If some
493 	 * interrupt calls us, the NAPI is about to clean up anyway. */
494 	if (!spin_trylock_irqsave(&chain->lock, flags))
495 		return;
496 
497 	while (spider_net_get_descr_status(chain->head->hwdescr) ==
498 			SPIDER_NET_DESCR_NOT_IN_USE) {
499 		if (spider_net_prepare_rx_descr(card, chain->head))
500 			break;
501 		chain->head = chain->head->next;
502 	}
503 
504 	spin_unlock_irqrestore(&chain->lock, flags);
505 }
506 
507 /**
508  * spider_net_alloc_rx_skbs - Allocates rx skbs in rx descriptor chains
509  * @card: card structure
510  *
511  * Returns 0 on success, <0 on failure.
512  */
513 static int
514 spider_net_alloc_rx_skbs(struct spider_net_card *card)
515 {
516 	struct spider_net_descr_chain *chain = &card->rx_chain;
517 	struct spider_net_descr *start = chain->tail;
518 	struct spider_net_descr *descr = start;
519 
520 	/* Link up the hardware chain pointers */
521 	do {
522 		descr->prev->hwdescr->next_descr_addr = descr->bus_addr;
523 		descr = descr->next;
524 	} while (descr != start);
525 
526 	/* Put at least one buffer into the chain. if this fails,
527 	 * we've got a problem. If not, spider_net_refill_rx_chain
528 	 * will do the rest at the end of this function. */
529 	if (spider_net_prepare_rx_descr(card, chain->head))
530 		goto error;
531 	else
532 		chain->head = chain->head->next;
533 
534 	/* This will allocate the rest of the rx buffers;
535 	 * if not, it's business as usual later on. */
536 	spider_net_refill_rx_chain(card);
537 	spider_net_enable_rxdmac(card);
538 	return 0;
539 
540 error:
541 	spider_net_free_rx_chain_contents(card);
542 	return -ENOMEM;
543 }
544 
545 /**
546  * spider_net_get_multicast_hash - generates hash for multicast filter table
547  * @addr: multicast address
548  *
549  * returns the hash value.
550  *
551  * spider_net_get_multicast_hash calculates a hash value for a given multicast
552  * address, that is used to set the multicast filter tables
553  */
554 static u8
555 spider_net_get_multicast_hash(struct net_device *netdev, __u8 *addr)
556 {
557 	u32 crc;
558 	u8 hash;
559 	char addr_for_crc[ETH_ALEN] = { 0, };
560 	int i, bit;
561 
562 	for (i = 0; i < ETH_ALEN * 8; i++) {
563 		bit = (addr[i / 8] >> (i % 8)) & 1;
564 		addr_for_crc[ETH_ALEN - 1 - i / 8] += bit << (7 - (i % 8));
565 	}
566 
567 	crc = crc32_be(~0, addr_for_crc, netdev->addr_len);
568 
569 	hash = (crc >> 27);
570 	hash <<= 3;
571 	hash |= crc & 7;
572 	hash &= 0xff;
573 
574 	return hash;
575 }
576 
577 /**
578  * spider_net_set_multi - sets multicast addresses and promisc flags
579  * @netdev: interface device structure
580  *
581  * spider_net_set_multi configures multicast addresses as needed for the
582  * netdev interface. It also sets up multicast, allmulti and promisc
583  * flags appropriately
584  */
585 static void
586 spider_net_set_multi(struct net_device *netdev)
587 {
588 	struct netdev_hw_addr *ha;
589 	u8 hash;
590 	int i;
591 	u32 reg;
592 	struct spider_net_card *card = netdev_priv(netdev);
593 	DECLARE_BITMAP(bitmask, SPIDER_NET_MULTICAST_HASHES) = {};
594 
595 	spider_net_set_promisc(card);
596 
597 	if (netdev->flags & IFF_ALLMULTI) {
598 		for (i = 0; i < SPIDER_NET_MULTICAST_HASHES; i++) {
599 			set_bit(i, bitmask);
600 		}
601 		goto write_hash;
602 	}
603 
604 	/* well, we know, what the broadcast hash value is: it's xfd
605 	hash = spider_net_get_multicast_hash(netdev, netdev->broadcast); */
606 	set_bit(0xfd, bitmask);
607 
608 	netdev_for_each_mc_addr(ha, netdev) {
609 		hash = spider_net_get_multicast_hash(netdev, ha->addr);
610 		set_bit(hash, bitmask);
611 	}
612 
613 write_hash:
614 	for (i = 0; i < SPIDER_NET_MULTICAST_HASHES / 4; i++) {
615 		reg = 0;
616 		if (test_bit(i * 4, bitmask))
617 			reg += 0x08;
618 		reg <<= 8;
619 		if (test_bit(i * 4 + 1, bitmask))
620 			reg += 0x08;
621 		reg <<= 8;
622 		if (test_bit(i * 4 + 2, bitmask))
623 			reg += 0x08;
624 		reg <<= 8;
625 		if (test_bit(i * 4 + 3, bitmask))
626 			reg += 0x08;
627 
628 		spider_net_write_reg(card, SPIDER_NET_GMRMHFILnR + i * 4, reg);
629 	}
630 }
631 
632 /**
633  * spider_net_prepare_tx_descr - fill tx descriptor with skb data
634  * @card: card structure
635  * @skb: packet to use
636  *
637  * returns 0 on success, <0 on failure.
638  *
639  * fills out the descriptor structure with skb data and len. Copies data,
640  * if needed (32bit DMA!)
641  */
642 static int
643 spider_net_prepare_tx_descr(struct spider_net_card *card,
644 			    struct sk_buff *skb)
645 {
646 	struct spider_net_descr_chain *chain = &card->tx_chain;
647 	struct spider_net_descr *descr;
648 	struct spider_net_hw_descr *hwdescr;
649 	dma_addr_t buf;
650 	unsigned long flags;
651 
652 	buf = pci_map_single(card->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
653 	if (pci_dma_mapping_error(card->pdev, buf)) {
654 		if (netif_msg_tx_err(card) && net_ratelimit())
655 			dev_err(&card->netdev->dev, "could not iommu-map packet (%p, %i). "
656 				  "Dropping packet\n", skb->data, skb->len);
657 		card->spider_stats.tx_iommu_map_error++;
658 		return -ENOMEM;
659 	}
660 
661 	spin_lock_irqsave(&chain->lock, flags);
662 	descr = card->tx_chain.head;
663 	if (descr->next == chain->tail->prev) {
664 		spin_unlock_irqrestore(&chain->lock, flags);
665 		pci_unmap_single(card->pdev, buf, skb->len, PCI_DMA_TODEVICE);
666 		return -ENOMEM;
667 	}
668 	hwdescr = descr->hwdescr;
669 	chain->head = descr->next;
670 
671 	descr->skb = skb;
672 	hwdescr->buf_addr = buf;
673 	hwdescr->buf_size = skb->len;
674 	hwdescr->next_descr_addr = 0;
675 	hwdescr->data_status = 0;
676 
677 	hwdescr->dmac_cmd_status =
678 			SPIDER_NET_DESCR_CARDOWNED | SPIDER_NET_DMAC_TXFRMTL;
679 	spin_unlock_irqrestore(&chain->lock, flags);
680 
681 	if (skb->ip_summed == CHECKSUM_PARTIAL)
682 		switch (ip_hdr(skb)->protocol) {
683 		case IPPROTO_TCP:
684 			hwdescr->dmac_cmd_status |= SPIDER_NET_DMAC_TCP;
685 			break;
686 		case IPPROTO_UDP:
687 			hwdescr->dmac_cmd_status |= SPIDER_NET_DMAC_UDP;
688 			break;
689 		}
690 
691 	/* Chain the bus address, so that the DMA engine finds this descr. */
692 	wmb();
693 	descr->prev->hwdescr->next_descr_addr = descr->bus_addr;
694 
695 	netif_trans_update(card->netdev); /* set netdev watchdog timer */
696 	return 0;
697 }
698 
699 static int
700 spider_net_set_low_watermark(struct spider_net_card *card)
701 {
702 	struct spider_net_descr *descr = card->tx_chain.tail;
703 	struct spider_net_hw_descr *hwdescr;
704 	unsigned long flags;
705 	int status;
706 	int cnt=0;
707 	int i;
708 
709 	/* Measure the length of the queue. Measurement does not
710 	 * need to be precise -- does not need a lock. */
711 	while (descr != card->tx_chain.head) {
712 		status = descr->hwdescr->dmac_cmd_status & SPIDER_NET_DESCR_NOT_IN_USE;
713 		if (status == SPIDER_NET_DESCR_NOT_IN_USE)
714 			break;
715 		descr = descr->next;
716 		cnt++;
717 	}
718 
719 	/* If TX queue is short, don't even bother with interrupts */
720 	if (cnt < card->tx_chain.num_desc/4)
721 		return cnt;
722 
723 	/* Set low-watermark 3/4th's of the way into the queue. */
724 	descr = card->tx_chain.tail;
725 	cnt = (cnt*3)/4;
726 	for (i=0;i<cnt; i++)
727 		descr = descr->next;
728 
729 	/* Set the new watermark, clear the old watermark */
730 	spin_lock_irqsave(&card->tx_chain.lock, flags);
731 	descr->hwdescr->dmac_cmd_status |= SPIDER_NET_DESCR_TXDESFLG;
732 	if (card->low_watermark && card->low_watermark != descr) {
733 		hwdescr = card->low_watermark->hwdescr;
734 		hwdescr->dmac_cmd_status =
735 		     hwdescr->dmac_cmd_status & ~SPIDER_NET_DESCR_TXDESFLG;
736 	}
737 	card->low_watermark = descr;
738 	spin_unlock_irqrestore(&card->tx_chain.lock, flags);
739 	return cnt;
740 }
741 
742 /**
743  * spider_net_release_tx_chain - processes sent tx descriptors
744  * @card: adapter structure
745  * @brutal: if set, don't care about whether descriptor seems to be in use
746  *
747  * returns 0 if the tx ring is empty, otherwise 1.
748  *
749  * spider_net_release_tx_chain releases the tx descriptors that spider has
750  * finished with (if non-brutal) or simply release tx descriptors (if brutal).
751  * If some other context is calling this function, we return 1 so that we're
752  * scheduled again (if we were scheduled) and will not lose initiative.
753  */
754 static int
755 spider_net_release_tx_chain(struct spider_net_card *card, int brutal)
756 {
757 	struct net_device *dev = card->netdev;
758 	struct spider_net_descr_chain *chain = &card->tx_chain;
759 	struct spider_net_descr *descr;
760 	struct spider_net_hw_descr *hwdescr;
761 	struct sk_buff *skb;
762 	u32 buf_addr;
763 	unsigned long flags;
764 	int status;
765 
766 	while (1) {
767 		spin_lock_irqsave(&chain->lock, flags);
768 		if (chain->tail == chain->head) {
769 			spin_unlock_irqrestore(&chain->lock, flags);
770 			return 0;
771 		}
772 		descr = chain->tail;
773 		hwdescr = descr->hwdescr;
774 
775 		status = spider_net_get_descr_status(hwdescr);
776 		switch (status) {
777 		case SPIDER_NET_DESCR_COMPLETE:
778 			dev->stats.tx_packets++;
779 			dev->stats.tx_bytes += descr->skb->len;
780 			break;
781 
782 		case SPIDER_NET_DESCR_CARDOWNED:
783 			if (!brutal) {
784 				spin_unlock_irqrestore(&chain->lock, flags);
785 				return 1;
786 			}
787 
788 			/* fallthrough, if we release the descriptors
789 			 * brutally (then we don't care about
790 			 * SPIDER_NET_DESCR_CARDOWNED) */
791 
792 		case SPIDER_NET_DESCR_RESPONSE_ERROR:
793 		case SPIDER_NET_DESCR_PROTECTION_ERROR:
794 		case SPIDER_NET_DESCR_FORCE_END:
795 			if (netif_msg_tx_err(card))
796 				dev_err(&card->netdev->dev, "forcing end of tx descriptor "
797 				       "with status x%02x\n", status);
798 			dev->stats.tx_errors++;
799 			break;
800 
801 		default:
802 			dev->stats.tx_dropped++;
803 			if (!brutal) {
804 				spin_unlock_irqrestore(&chain->lock, flags);
805 				return 1;
806 			}
807 		}
808 
809 		chain->tail = descr->next;
810 		hwdescr->dmac_cmd_status |= SPIDER_NET_DESCR_NOT_IN_USE;
811 		skb = descr->skb;
812 		descr->skb = NULL;
813 		buf_addr = hwdescr->buf_addr;
814 		spin_unlock_irqrestore(&chain->lock, flags);
815 
816 		/* unmap the skb */
817 		if (skb) {
818 			pci_unmap_single(card->pdev, buf_addr, skb->len,
819 					PCI_DMA_TODEVICE);
820 			dev_consume_skb_any(skb);
821 		}
822 	}
823 	return 0;
824 }
825 
826 /**
827  * spider_net_kick_tx_dma - enables TX DMA processing
828  * @card: card structure
829  *
830  * This routine will start the transmit DMA running if
831  * it is not already running. This routine ned only be
832  * called when queueing a new packet to an empty tx queue.
833  * Writes the current tx chain head as start address
834  * of the tx descriptor chain and enables the transmission
835  * DMA engine.
836  */
837 static inline void
838 spider_net_kick_tx_dma(struct spider_net_card *card)
839 {
840 	struct spider_net_descr *descr;
841 
842 	if (spider_net_read_reg(card, SPIDER_NET_GDTDMACCNTR) &
843 			SPIDER_NET_TX_DMA_EN)
844 		goto out;
845 
846 	descr = card->tx_chain.tail;
847 	for (;;) {
848 		if (spider_net_get_descr_status(descr->hwdescr) ==
849 				SPIDER_NET_DESCR_CARDOWNED) {
850 			spider_net_write_reg(card, SPIDER_NET_GDTDCHA,
851 					descr->bus_addr);
852 			spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
853 					SPIDER_NET_DMA_TX_VALUE);
854 			break;
855 		}
856 		if (descr == card->tx_chain.head)
857 			break;
858 		descr = descr->next;
859 	}
860 
861 out:
862 	mod_timer(&card->tx_timer, jiffies + SPIDER_NET_TX_TIMER);
863 }
864 
865 /**
866  * spider_net_xmit - transmits a frame over the device
867  * @skb: packet to send out
868  * @netdev: interface device structure
869  *
870  * returns NETDEV_TX_OK on success, NETDEV_TX_BUSY on failure
871  */
872 static netdev_tx_t
873 spider_net_xmit(struct sk_buff *skb, struct net_device *netdev)
874 {
875 	int cnt;
876 	struct spider_net_card *card = netdev_priv(netdev);
877 
878 	spider_net_release_tx_chain(card, 0);
879 
880 	if (spider_net_prepare_tx_descr(card, skb) != 0) {
881 		netdev->stats.tx_dropped++;
882 		netif_stop_queue(netdev);
883 		return NETDEV_TX_BUSY;
884 	}
885 
886 	cnt = spider_net_set_low_watermark(card);
887 	if (cnt < 5)
888 		spider_net_kick_tx_dma(card);
889 	return NETDEV_TX_OK;
890 }
891 
892 /**
893  * spider_net_cleanup_tx_ring - cleans up the TX ring
894  * @card: card structure
895  *
896  * spider_net_cleanup_tx_ring is called by either the tx_timer
897  * or from the NAPI polling routine.
898  * This routine releases resources associted with transmitted
899  * packets, including updating the queue tail pointer.
900  */
901 static void
902 spider_net_cleanup_tx_ring(struct timer_list *t)
903 {
904 	struct spider_net_card *card = from_timer(card, t, tx_timer);
905 	if ((spider_net_release_tx_chain(card, 0) != 0) &&
906 	    (card->netdev->flags & IFF_UP)) {
907 		spider_net_kick_tx_dma(card);
908 		netif_wake_queue(card->netdev);
909 	}
910 }
911 
912 /**
913  * spider_net_do_ioctl - called for device ioctls
914  * @netdev: interface device structure
915  * @ifr: request parameter structure for ioctl
916  * @cmd: command code for ioctl
917  *
918  * returns 0 on success, <0 on failure. Currently, we have no special ioctls.
919  * -EOPNOTSUPP is returned, if an unknown ioctl was requested
920  */
921 static int
922 spider_net_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
923 {
924 	switch (cmd) {
925 	default:
926 		return -EOPNOTSUPP;
927 	}
928 }
929 
930 /**
931  * spider_net_pass_skb_up - takes an skb from a descriptor and passes it on
932  * @descr: descriptor to process
933  * @card: card structure
934  *
935  * Fills out skb structure and passes the data to the stack.
936  * The descriptor state is not changed.
937  */
938 static void
939 spider_net_pass_skb_up(struct spider_net_descr *descr,
940 		       struct spider_net_card *card)
941 {
942 	struct spider_net_hw_descr *hwdescr = descr->hwdescr;
943 	struct sk_buff *skb = descr->skb;
944 	struct net_device *netdev = card->netdev;
945 	u32 data_status = hwdescr->data_status;
946 	u32 data_error = hwdescr->data_error;
947 
948 	skb_put(skb, hwdescr->valid_size);
949 
950 	/* the card seems to add 2 bytes of junk in front
951 	 * of the ethernet frame */
952 #define SPIDER_MISALIGN		2
953 	skb_pull(skb, SPIDER_MISALIGN);
954 	skb->protocol = eth_type_trans(skb, netdev);
955 
956 	/* checksum offload */
957 	skb_checksum_none_assert(skb);
958 	if (netdev->features & NETIF_F_RXCSUM) {
959 		if ( ( (data_status & SPIDER_NET_DATA_STATUS_CKSUM_MASK) ==
960 		       SPIDER_NET_DATA_STATUS_CKSUM_MASK) &&
961 		     !(data_error & SPIDER_NET_DATA_ERR_CKSUM_MASK))
962 			skb->ip_summed = CHECKSUM_UNNECESSARY;
963 	}
964 
965 	if (data_status & SPIDER_NET_VLAN_PACKET) {
966 		/* further enhancements: HW-accel VLAN */
967 	}
968 
969 	/* update netdevice statistics */
970 	netdev->stats.rx_packets++;
971 	netdev->stats.rx_bytes += skb->len;
972 
973 	/* pass skb up to stack */
974 	netif_receive_skb(skb);
975 }
976 
977 static void show_rx_chain(struct spider_net_card *card)
978 {
979 	struct spider_net_descr_chain *chain = &card->rx_chain;
980 	struct spider_net_descr *start= chain->tail;
981 	struct spider_net_descr *descr= start;
982 	struct spider_net_hw_descr *hwd = start->hwdescr;
983 	struct device *dev = &card->netdev->dev;
984 	u32 curr_desc, next_desc;
985 	int status;
986 
987 	int tot = 0;
988 	int cnt = 0;
989 	int off = start - chain->ring;
990 	int cstat = hwd->dmac_cmd_status;
991 
992 	dev_info(dev, "Total number of descrs=%d\n",
993 		chain->num_desc);
994 	dev_info(dev, "Chain tail located at descr=%d, status=0x%x\n",
995 		off, cstat);
996 
997 	curr_desc = spider_net_read_reg(card, SPIDER_NET_GDACTDPA);
998 	next_desc = spider_net_read_reg(card, SPIDER_NET_GDACNEXTDA);
999 
1000 	status = cstat;
1001 	do
1002 	{
1003 		hwd = descr->hwdescr;
1004 		off = descr - chain->ring;
1005 		status = hwd->dmac_cmd_status;
1006 
1007 		if (descr == chain->head)
1008 			dev_info(dev, "Chain head is at %d, head status=0x%x\n",
1009 			         off, status);
1010 
1011 		if (curr_desc == descr->bus_addr)
1012 			dev_info(dev, "HW curr desc (GDACTDPA) is at %d, status=0x%x\n",
1013 			         off, status);
1014 
1015 		if (next_desc == descr->bus_addr)
1016 			dev_info(dev, "HW next desc (GDACNEXTDA) is at %d, status=0x%x\n",
1017 			         off, status);
1018 
1019 		if (hwd->next_descr_addr == 0)
1020 			dev_info(dev, "chain is cut at %d\n", off);
1021 
1022 		if (cstat != status) {
1023 			int from = (chain->num_desc + off - cnt) % chain->num_desc;
1024 			int to = (chain->num_desc + off - 1) % chain->num_desc;
1025 			dev_info(dev, "Have %d (from %d to %d) descrs "
1026 			         "with stat=0x%08x\n", cnt, from, to, cstat);
1027 			cstat = status;
1028 			cnt = 0;
1029 		}
1030 
1031 		cnt ++;
1032 		tot ++;
1033 		descr = descr->next;
1034 	} while (descr != start);
1035 
1036 	dev_info(dev, "Last %d descrs with stat=0x%08x "
1037 	         "for a total of %d descrs\n", cnt, cstat, tot);
1038 
1039 #ifdef DEBUG
1040 	/* Now dump the whole ring */
1041 	descr = start;
1042 	do
1043 	{
1044 		struct spider_net_hw_descr *hwd = descr->hwdescr;
1045 		status = spider_net_get_descr_status(hwd);
1046 		cnt = descr - chain->ring;
1047 		dev_info(dev, "Descr %d stat=0x%08x skb=%p\n",
1048 		         cnt, status, descr->skb);
1049 		dev_info(dev, "bus addr=%08x buf addr=%08x sz=%d\n",
1050 		         descr->bus_addr, hwd->buf_addr, hwd->buf_size);
1051 		dev_info(dev, "next=%08x result sz=%d valid sz=%d\n",
1052 		         hwd->next_descr_addr, hwd->result_size,
1053 		         hwd->valid_size);
1054 		dev_info(dev, "dmac=%08x data stat=%08x data err=%08x\n",
1055 		         hwd->dmac_cmd_status, hwd->data_status,
1056 		         hwd->data_error);
1057 		dev_info(dev, "\n");
1058 
1059 		descr = descr->next;
1060 	} while (descr != start);
1061 #endif
1062 
1063 }
1064 
1065 /**
1066  * spider_net_resync_head_ptr - Advance head ptr past empty descrs
1067  *
1068  * If the driver fails to keep up and empty the queue, then the
1069  * hardware wil run out of room to put incoming packets. This
1070  * will cause the hardware to skip descrs that are full (instead
1071  * of halting/retrying). Thus, once the driver runs, it wil need
1072  * to "catch up" to where the hardware chain pointer is at.
1073  */
1074 static void spider_net_resync_head_ptr(struct spider_net_card *card)
1075 {
1076 	unsigned long flags;
1077 	struct spider_net_descr_chain *chain = &card->rx_chain;
1078 	struct spider_net_descr *descr;
1079 	int i, status;
1080 
1081 	/* Advance head pointer past any empty descrs */
1082 	descr = chain->head;
1083 	status = spider_net_get_descr_status(descr->hwdescr);
1084 
1085 	if (status == SPIDER_NET_DESCR_NOT_IN_USE)
1086 		return;
1087 
1088 	spin_lock_irqsave(&chain->lock, flags);
1089 
1090 	descr = chain->head;
1091 	status = spider_net_get_descr_status(descr->hwdescr);
1092 	for (i=0; i<chain->num_desc; i++) {
1093 		if (status != SPIDER_NET_DESCR_CARDOWNED) break;
1094 		descr = descr->next;
1095 		status = spider_net_get_descr_status(descr->hwdescr);
1096 	}
1097 	chain->head = descr;
1098 
1099 	spin_unlock_irqrestore(&chain->lock, flags);
1100 }
1101 
1102 static int spider_net_resync_tail_ptr(struct spider_net_card *card)
1103 {
1104 	struct spider_net_descr_chain *chain = &card->rx_chain;
1105 	struct spider_net_descr *descr;
1106 	int i, status;
1107 
1108 	/* Advance tail pointer past any empty and reaped descrs */
1109 	descr = chain->tail;
1110 	status = spider_net_get_descr_status(descr->hwdescr);
1111 
1112 	for (i=0; i<chain->num_desc; i++) {
1113 		if ((status != SPIDER_NET_DESCR_CARDOWNED) &&
1114 		    (status != SPIDER_NET_DESCR_NOT_IN_USE)) break;
1115 		descr = descr->next;
1116 		status = spider_net_get_descr_status(descr->hwdescr);
1117 	}
1118 	chain->tail = descr;
1119 
1120 	if ((i == chain->num_desc) || (i == 0))
1121 		return 1;
1122 	return 0;
1123 }
1124 
1125 /**
1126  * spider_net_decode_one_descr - processes an RX descriptor
1127  * @card: card structure
1128  *
1129  * Returns 1 if a packet has been sent to the stack, otherwise 0.
1130  *
1131  * Processes an RX descriptor by iommu-unmapping the data buffer
1132  * and passing the packet up to the stack. This function is called
1133  * in softirq context, e.g. either bottom half from interrupt or
1134  * NAPI polling context.
1135  */
1136 static int
1137 spider_net_decode_one_descr(struct spider_net_card *card)
1138 {
1139 	struct net_device *dev = card->netdev;
1140 	struct spider_net_descr_chain *chain = &card->rx_chain;
1141 	struct spider_net_descr *descr = chain->tail;
1142 	struct spider_net_hw_descr *hwdescr = descr->hwdescr;
1143 	u32 hw_buf_addr;
1144 	int status;
1145 
1146 	status = spider_net_get_descr_status(hwdescr);
1147 
1148 	/* Nothing in the descriptor, or ring must be empty */
1149 	if ((status == SPIDER_NET_DESCR_CARDOWNED) ||
1150 	    (status == SPIDER_NET_DESCR_NOT_IN_USE))
1151 		return 0;
1152 
1153 	/* descriptor definitively used -- move on tail */
1154 	chain->tail = descr->next;
1155 
1156 	/* unmap descriptor */
1157 	hw_buf_addr = hwdescr->buf_addr;
1158 	hwdescr->buf_addr = 0xffffffff;
1159 	pci_unmap_single(card->pdev, hw_buf_addr,
1160 			SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
1161 
1162 	if ( (status == SPIDER_NET_DESCR_RESPONSE_ERROR) ||
1163 	     (status == SPIDER_NET_DESCR_PROTECTION_ERROR) ||
1164 	     (status == SPIDER_NET_DESCR_FORCE_END) ) {
1165 		if (netif_msg_rx_err(card))
1166 			dev_err(&dev->dev,
1167 			       "dropping RX descriptor with state %d\n", status);
1168 		dev->stats.rx_dropped++;
1169 		goto bad_desc;
1170 	}
1171 
1172 	if ( (status != SPIDER_NET_DESCR_COMPLETE) &&
1173 	     (status != SPIDER_NET_DESCR_FRAME_END) ) {
1174 		if (netif_msg_rx_err(card))
1175 			dev_err(&card->netdev->dev,
1176 			       "RX descriptor with unknown state %d\n", status);
1177 		card->spider_stats.rx_desc_unk_state++;
1178 		goto bad_desc;
1179 	}
1180 
1181 	/* The cases we'll throw away the packet immediately */
1182 	if (hwdescr->data_error & SPIDER_NET_DESTROY_RX_FLAGS) {
1183 		if (netif_msg_rx_err(card))
1184 			dev_err(&card->netdev->dev,
1185 			       "error in received descriptor found, "
1186 			       "data_status=x%08x, data_error=x%08x\n",
1187 			       hwdescr->data_status, hwdescr->data_error);
1188 		goto bad_desc;
1189 	}
1190 
1191 	if (hwdescr->dmac_cmd_status & SPIDER_NET_DESCR_BAD_STATUS) {
1192 		dev_err(&card->netdev->dev, "bad status, cmd_status=x%08x\n",
1193 			       hwdescr->dmac_cmd_status);
1194 		pr_err("buf_addr=x%08x\n", hw_buf_addr);
1195 		pr_err("buf_size=x%08x\n", hwdescr->buf_size);
1196 		pr_err("next_descr_addr=x%08x\n", hwdescr->next_descr_addr);
1197 		pr_err("result_size=x%08x\n", hwdescr->result_size);
1198 		pr_err("valid_size=x%08x\n", hwdescr->valid_size);
1199 		pr_err("data_status=x%08x\n", hwdescr->data_status);
1200 		pr_err("data_error=x%08x\n", hwdescr->data_error);
1201 		pr_err("which=%ld\n", descr - card->rx_chain.ring);
1202 
1203 		card->spider_stats.rx_desc_error++;
1204 		goto bad_desc;
1205 	}
1206 
1207 	/* Ok, we've got a packet in descr */
1208 	spider_net_pass_skb_up(descr, card);
1209 	descr->skb = NULL;
1210 	hwdescr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
1211 	return 1;
1212 
1213 bad_desc:
1214 	if (netif_msg_rx_err(card))
1215 		show_rx_chain(card);
1216 	dev_kfree_skb_irq(descr->skb);
1217 	descr->skb = NULL;
1218 	hwdescr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
1219 	return 0;
1220 }
1221 
1222 /**
1223  * spider_net_poll - NAPI poll function called by the stack to return packets
1224  * @netdev: interface device structure
1225  * @budget: number of packets we can pass to the stack at most
1226  *
1227  * returns 0 if no more packets available to the driver/stack. Returns 1,
1228  * if the quota is exceeded, but the driver has still packets.
1229  *
1230  * spider_net_poll returns all packets from the rx descriptors to the stack
1231  * (using netif_receive_skb). If all/enough packets are up, the driver
1232  * reenables interrupts and returns 0. If not, 1 is returned.
1233  */
1234 static int spider_net_poll(struct napi_struct *napi, int budget)
1235 {
1236 	struct spider_net_card *card = container_of(napi, struct spider_net_card, napi);
1237 	int packets_done = 0;
1238 
1239 	while (packets_done < budget) {
1240 		if (!spider_net_decode_one_descr(card))
1241 			break;
1242 
1243 		packets_done++;
1244 	}
1245 
1246 	if ((packets_done == 0) && (card->num_rx_ints != 0)) {
1247 		if (!spider_net_resync_tail_ptr(card))
1248 			packets_done = budget;
1249 		spider_net_resync_head_ptr(card);
1250 	}
1251 	card->num_rx_ints = 0;
1252 
1253 	spider_net_refill_rx_chain(card);
1254 	spider_net_enable_rxdmac(card);
1255 
1256 	spider_net_cleanup_tx_ring(&card->tx_timer);
1257 
1258 	/* if all packets are in the stack, enable interrupts and return 0 */
1259 	/* if not, return 1 */
1260 	if (packets_done < budget) {
1261 		napi_complete_done(napi, packets_done);
1262 		spider_net_rx_irq_on(card);
1263 		card->ignore_rx_ramfull = 0;
1264 	}
1265 
1266 	return packets_done;
1267 }
1268 
1269 /**
1270  * spider_net_set_mac - sets the MAC of an interface
1271  * @netdev: interface device structure
1272  * @ptr: pointer to new MAC address
1273  *
1274  * Returns 0 on success, <0 on failure. Currently, we don't support this
1275  * and will always return EOPNOTSUPP.
1276  */
1277 static int
1278 spider_net_set_mac(struct net_device *netdev, void *p)
1279 {
1280 	struct spider_net_card *card = netdev_priv(netdev);
1281 	u32 macl, macu, regvalue;
1282 	struct sockaddr *addr = p;
1283 
1284 	if (!is_valid_ether_addr(addr->sa_data))
1285 		return -EADDRNOTAVAIL;
1286 
1287 	memcpy(netdev->dev_addr, addr->sa_data, ETH_ALEN);
1288 
1289 	/* switch off GMACTPE and GMACRPE */
1290 	regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1291 	regvalue &= ~((1 << 5) | (1 << 6));
1292 	spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1293 
1294 	/* write mac */
1295 	macu = (netdev->dev_addr[0]<<24) + (netdev->dev_addr[1]<<16) +
1296 		(netdev->dev_addr[2]<<8) + (netdev->dev_addr[3]);
1297 	macl = (netdev->dev_addr[4]<<8) + (netdev->dev_addr[5]);
1298 	spider_net_write_reg(card, SPIDER_NET_GMACUNIMACU, macu);
1299 	spider_net_write_reg(card, SPIDER_NET_GMACUNIMACL, macl);
1300 
1301 	/* switch GMACTPE and GMACRPE back on */
1302 	regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1303 	regvalue |= ((1 << 5) | (1 << 6));
1304 	spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1305 
1306 	spider_net_set_promisc(card);
1307 
1308 	return 0;
1309 }
1310 
1311 /**
1312  * spider_net_link_reset
1313  * @netdev: net device structure
1314  *
1315  * This is called when the PHY_LINK signal is asserted. For the blade this is
1316  * not connected so we should never get here.
1317  *
1318  */
1319 static void
1320 spider_net_link_reset(struct net_device *netdev)
1321 {
1322 
1323 	struct spider_net_card *card = netdev_priv(netdev);
1324 
1325 	del_timer_sync(&card->aneg_timer);
1326 
1327 	/* clear interrupt, block further interrupts */
1328 	spider_net_write_reg(card, SPIDER_NET_GMACST,
1329 			     spider_net_read_reg(card, SPIDER_NET_GMACST));
1330 	spider_net_write_reg(card, SPIDER_NET_GMACINTEN, 0);
1331 
1332 	/* reset phy and setup aneg */
1333 	card->aneg_count = 0;
1334 	card->medium = BCM54XX_COPPER;
1335 	spider_net_setup_aneg(card);
1336 	mod_timer(&card->aneg_timer, jiffies + SPIDER_NET_ANEG_TIMER);
1337 
1338 }
1339 
1340 /**
1341  * spider_net_handle_error_irq - handles errors raised by an interrupt
1342  * @card: card structure
1343  * @status_reg: interrupt status register 0 (GHIINT0STS)
1344  *
1345  * spider_net_handle_error_irq treats or ignores all error conditions
1346  * found when an interrupt is presented
1347  */
1348 static void
1349 spider_net_handle_error_irq(struct spider_net_card *card, u32 status_reg,
1350 			    u32 error_reg1, u32 error_reg2)
1351 {
1352 	u32 i;
1353 	int show_error = 1;
1354 
1355 	/* check GHIINT0STS ************************************/
1356 	if (status_reg)
1357 		for (i = 0; i < 32; i++)
1358 			if (status_reg & (1<<i))
1359 				switch (i)
1360 	{
1361 	/* let error_reg1 and error_reg2 evaluation decide, what to do
1362 	case SPIDER_NET_PHYINT:
1363 	case SPIDER_NET_GMAC2INT:
1364 	case SPIDER_NET_GMAC1INT:
1365 	case SPIDER_NET_GFIFOINT:
1366 	case SPIDER_NET_DMACINT:
1367 	case SPIDER_NET_GSYSINT:
1368 		break; */
1369 
1370 	case SPIDER_NET_GIPSINT:
1371 		show_error = 0;
1372 		break;
1373 
1374 	case SPIDER_NET_GPWOPCMPINT:
1375 		/* PHY write operation completed */
1376 		show_error = 0;
1377 		break;
1378 	case SPIDER_NET_GPROPCMPINT:
1379 		/* PHY read operation completed */
1380 		/* we don't use semaphores, as we poll for the completion
1381 		 * of the read operation in spider_net_read_phy. Should take
1382 		 * about 50 us */
1383 		show_error = 0;
1384 		break;
1385 	case SPIDER_NET_GPWFFINT:
1386 		/* PHY command queue full */
1387 		if (netif_msg_intr(card))
1388 			dev_err(&card->netdev->dev, "PHY write queue full\n");
1389 		show_error = 0;
1390 		break;
1391 
1392 	/* case SPIDER_NET_GRMDADRINT: not used. print a message */
1393 	/* case SPIDER_NET_GRMARPINT: not used. print a message */
1394 	/* case SPIDER_NET_GRMMPINT: not used. print a message */
1395 
1396 	case SPIDER_NET_GDTDEN0INT:
1397 		/* someone has set TX_DMA_EN to 0 */
1398 		show_error = 0;
1399 		break;
1400 
1401 	case SPIDER_NET_GDDDEN0INT: /* fallthrough */
1402 	case SPIDER_NET_GDCDEN0INT: /* fallthrough */
1403 	case SPIDER_NET_GDBDEN0INT: /* fallthrough */
1404 	case SPIDER_NET_GDADEN0INT:
1405 		/* someone has set RX_DMA_EN to 0 */
1406 		show_error = 0;
1407 		break;
1408 
1409 	/* RX interrupts */
1410 	case SPIDER_NET_GDDFDCINT:
1411 	case SPIDER_NET_GDCFDCINT:
1412 	case SPIDER_NET_GDBFDCINT:
1413 	case SPIDER_NET_GDAFDCINT:
1414 	/* case SPIDER_NET_GDNMINT: not used. print a message */
1415 	/* case SPIDER_NET_GCNMINT: not used. print a message */
1416 	/* case SPIDER_NET_GBNMINT: not used. print a message */
1417 	/* case SPIDER_NET_GANMINT: not used. print a message */
1418 	/* case SPIDER_NET_GRFNMINT: not used. print a message */
1419 		show_error = 0;
1420 		break;
1421 
1422 	/* TX interrupts */
1423 	case SPIDER_NET_GDTFDCINT:
1424 		show_error = 0;
1425 		break;
1426 	case SPIDER_NET_GTTEDINT:
1427 		show_error = 0;
1428 		break;
1429 	case SPIDER_NET_GDTDCEINT:
1430 		/* chain end. If a descriptor should be sent, kick off
1431 		 * tx dma
1432 		if (card->tx_chain.tail != card->tx_chain.head)
1433 			spider_net_kick_tx_dma(card);
1434 		*/
1435 		show_error = 0;
1436 		break;
1437 
1438 	/* case SPIDER_NET_G1TMCNTINT: not used. print a message */
1439 	/* case SPIDER_NET_GFREECNTINT: not used. print a message */
1440 	}
1441 
1442 	/* check GHIINT1STS ************************************/
1443 	if (error_reg1)
1444 		for (i = 0; i < 32; i++)
1445 			if (error_reg1 & (1<<i))
1446 				switch (i)
1447 	{
1448 	case SPIDER_NET_GTMFLLINT:
1449 		/* TX RAM full may happen on a usual case.
1450 		 * Logging is not needed. */
1451 		show_error = 0;
1452 		break;
1453 	case SPIDER_NET_GRFDFLLINT: /* fallthrough */
1454 	case SPIDER_NET_GRFCFLLINT: /* fallthrough */
1455 	case SPIDER_NET_GRFBFLLINT: /* fallthrough */
1456 	case SPIDER_NET_GRFAFLLINT: /* fallthrough */
1457 	case SPIDER_NET_GRMFLLINT:
1458 		/* Could happen when rx chain is full */
1459 		if (card->ignore_rx_ramfull == 0) {
1460 			card->ignore_rx_ramfull = 1;
1461 			spider_net_resync_head_ptr(card);
1462 			spider_net_refill_rx_chain(card);
1463 			spider_net_enable_rxdmac(card);
1464 			card->num_rx_ints ++;
1465 			napi_schedule(&card->napi);
1466 		}
1467 		show_error = 0;
1468 		break;
1469 
1470 	/* case SPIDER_NET_GTMSHTINT: problem, print a message */
1471 	case SPIDER_NET_GDTINVDINT:
1472 		/* allrighty. tx from previous descr ok */
1473 		show_error = 0;
1474 		break;
1475 
1476 	/* chain end */
1477 	case SPIDER_NET_GDDDCEINT: /* fallthrough */
1478 	case SPIDER_NET_GDCDCEINT: /* fallthrough */
1479 	case SPIDER_NET_GDBDCEINT: /* fallthrough */
1480 	case SPIDER_NET_GDADCEINT:
1481 		spider_net_resync_head_ptr(card);
1482 		spider_net_refill_rx_chain(card);
1483 		spider_net_enable_rxdmac(card);
1484 		card->num_rx_ints ++;
1485 		napi_schedule(&card->napi);
1486 		show_error = 0;
1487 		break;
1488 
1489 	/* invalid descriptor */
1490 	case SPIDER_NET_GDDINVDINT: /* fallthrough */
1491 	case SPIDER_NET_GDCINVDINT: /* fallthrough */
1492 	case SPIDER_NET_GDBINVDINT: /* fallthrough */
1493 	case SPIDER_NET_GDAINVDINT:
1494 		/* Could happen when rx chain is full */
1495 		spider_net_resync_head_ptr(card);
1496 		spider_net_refill_rx_chain(card);
1497 		spider_net_enable_rxdmac(card);
1498 		card->num_rx_ints ++;
1499 		napi_schedule(&card->napi);
1500 		show_error = 0;
1501 		break;
1502 
1503 	/* case SPIDER_NET_GDTRSERINT: problem, print a message */
1504 	/* case SPIDER_NET_GDDRSERINT: problem, print a message */
1505 	/* case SPIDER_NET_GDCRSERINT: problem, print a message */
1506 	/* case SPIDER_NET_GDBRSERINT: problem, print a message */
1507 	/* case SPIDER_NET_GDARSERINT: problem, print a message */
1508 	/* case SPIDER_NET_GDSERINT: problem, print a message */
1509 	/* case SPIDER_NET_GDTPTERINT: problem, print a message */
1510 	/* case SPIDER_NET_GDDPTERINT: problem, print a message */
1511 	/* case SPIDER_NET_GDCPTERINT: problem, print a message */
1512 	/* case SPIDER_NET_GDBPTERINT: problem, print a message */
1513 	/* case SPIDER_NET_GDAPTERINT: problem, print a message */
1514 	default:
1515 		show_error = 1;
1516 		break;
1517 	}
1518 
1519 	/* check GHIINT2STS ************************************/
1520 	if (error_reg2)
1521 		for (i = 0; i < 32; i++)
1522 			if (error_reg2 & (1<<i))
1523 				switch (i)
1524 	{
1525 	/* there is nothing we can (want  to) do at this time. Log a
1526 	 * message, we can switch on and off the specific values later on
1527 	case SPIDER_NET_GPROPERINT:
1528 	case SPIDER_NET_GMCTCRSNGINT:
1529 	case SPIDER_NET_GMCTLCOLINT:
1530 	case SPIDER_NET_GMCTTMOTINT:
1531 	case SPIDER_NET_GMCRCAERINT:
1532 	case SPIDER_NET_GMCRCALERINT:
1533 	case SPIDER_NET_GMCRALNERINT:
1534 	case SPIDER_NET_GMCROVRINT:
1535 	case SPIDER_NET_GMCRRNTINT:
1536 	case SPIDER_NET_GMCRRXERINT:
1537 	case SPIDER_NET_GTITCSERINT:
1538 	case SPIDER_NET_GTIFMTERINT:
1539 	case SPIDER_NET_GTIPKTRVKINT:
1540 	case SPIDER_NET_GTISPINGINT:
1541 	case SPIDER_NET_GTISADNGINT:
1542 	case SPIDER_NET_GTISPDNGINT:
1543 	case SPIDER_NET_GRIFMTERINT:
1544 	case SPIDER_NET_GRIPKTRVKINT:
1545 	case SPIDER_NET_GRISPINGINT:
1546 	case SPIDER_NET_GRISADNGINT:
1547 	case SPIDER_NET_GRISPDNGINT:
1548 		break;
1549 	*/
1550 		default:
1551 			break;
1552 	}
1553 
1554 	if ((show_error) && (netif_msg_intr(card)) && net_ratelimit())
1555 		dev_err(&card->netdev->dev, "Error interrupt, GHIINT0STS = 0x%08x, "
1556 		       "GHIINT1STS = 0x%08x, GHIINT2STS = 0x%08x\n",
1557 		       status_reg, error_reg1, error_reg2);
1558 
1559 	/* clear interrupt sources */
1560 	spider_net_write_reg(card, SPIDER_NET_GHIINT1STS, error_reg1);
1561 	spider_net_write_reg(card, SPIDER_NET_GHIINT2STS, error_reg2);
1562 }
1563 
1564 /**
1565  * spider_net_interrupt - interrupt handler for spider_net
1566  * @irq: interrupt number
1567  * @ptr: pointer to net_device
1568  *
1569  * returns IRQ_HANDLED, if interrupt was for driver, or IRQ_NONE, if no
1570  * interrupt found raised by card.
1571  *
1572  * This is the interrupt handler, that turns off
1573  * interrupts for this device and makes the stack poll the driver
1574  */
1575 static irqreturn_t
1576 spider_net_interrupt(int irq, void *ptr)
1577 {
1578 	struct net_device *netdev = ptr;
1579 	struct spider_net_card *card = netdev_priv(netdev);
1580 	u32 status_reg, error_reg1, error_reg2;
1581 
1582 	status_reg = spider_net_read_reg(card, SPIDER_NET_GHIINT0STS);
1583 	error_reg1 = spider_net_read_reg(card, SPIDER_NET_GHIINT1STS);
1584 	error_reg2 = spider_net_read_reg(card, SPIDER_NET_GHIINT2STS);
1585 
1586 	if (!(status_reg & SPIDER_NET_INT0_MASK_VALUE) &&
1587 	    !(error_reg1 & SPIDER_NET_INT1_MASK_VALUE) &&
1588 	    !(error_reg2 & SPIDER_NET_INT2_MASK_VALUE))
1589 		return IRQ_NONE;
1590 
1591 	if (status_reg & SPIDER_NET_RXINT ) {
1592 		spider_net_rx_irq_off(card);
1593 		napi_schedule(&card->napi);
1594 		card->num_rx_ints ++;
1595 	}
1596 	if (status_reg & SPIDER_NET_TXINT)
1597 		napi_schedule(&card->napi);
1598 
1599 	if (status_reg & SPIDER_NET_LINKINT)
1600 		spider_net_link_reset(netdev);
1601 
1602 	if (status_reg & SPIDER_NET_ERRINT )
1603 		spider_net_handle_error_irq(card, status_reg,
1604 					    error_reg1, error_reg2);
1605 
1606 	/* clear interrupt sources */
1607 	spider_net_write_reg(card, SPIDER_NET_GHIINT0STS, status_reg);
1608 
1609 	return IRQ_HANDLED;
1610 }
1611 
1612 #ifdef CONFIG_NET_POLL_CONTROLLER
1613 /**
1614  * spider_net_poll_controller - artificial interrupt for netconsole etc.
1615  * @netdev: interface device structure
1616  *
1617  * see Documentation/networking/netconsole.txt
1618  */
1619 static void
1620 spider_net_poll_controller(struct net_device *netdev)
1621 {
1622 	disable_irq(netdev->irq);
1623 	spider_net_interrupt(netdev->irq, netdev);
1624 	enable_irq(netdev->irq);
1625 }
1626 #endif /* CONFIG_NET_POLL_CONTROLLER */
1627 
1628 /**
1629  * spider_net_enable_interrupts - enable interrupts
1630  * @card: card structure
1631  *
1632  * spider_net_enable_interrupt enables several interrupts
1633  */
1634 static void
1635 spider_net_enable_interrupts(struct spider_net_card *card)
1636 {
1637 	spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK,
1638 			     SPIDER_NET_INT0_MASK_VALUE);
1639 	spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK,
1640 			     SPIDER_NET_INT1_MASK_VALUE);
1641 	spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK,
1642 			     SPIDER_NET_INT2_MASK_VALUE);
1643 }
1644 
1645 /**
1646  * spider_net_disable_interrupts - disable interrupts
1647  * @card: card structure
1648  *
1649  * spider_net_disable_interrupts disables all the interrupts
1650  */
1651 static void
1652 spider_net_disable_interrupts(struct spider_net_card *card)
1653 {
1654 	spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, 0);
1655 	spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK, 0);
1656 	spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK, 0);
1657 	spider_net_write_reg(card, SPIDER_NET_GMACINTEN, 0);
1658 }
1659 
1660 /**
1661  * spider_net_init_card - initializes the card
1662  * @card: card structure
1663  *
1664  * spider_net_init_card initializes the card so that other registers can
1665  * be used
1666  */
1667 static void
1668 spider_net_init_card(struct spider_net_card *card)
1669 {
1670 	spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1671 			     SPIDER_NET_CKRCTRL_STOP_VALUE);
1672 
1673 	spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1674 			     SPIDER_NET_CKRCTRL_RUN_VALUE);
1675 
1676 	/* trigger ETOMOD signal */
1677 	spider_net_write_reg(card, SPIDER_NET_GMACOPEMD,
1678 		spider_net_read_reg(card, SPIDER_NET_GMACOPEMD) | 0x4);
1679 
1680 	spider_net_disable_interrupts(card);
1681 }
1682 
1683 /**
1684  * spider_net_enable_card - enables the card by setting all kinds of regs
1685  * @card: card structure
1686  *
1687  * spider_net_enable_card sets a lot of SMMIO registers to enable the device
1688  */
1689 static void
1690 spider_net_enable_card(struct spider_net_card *card)
1691 {
1692 	int i;
1693 	/* the following array consists of (register),(value) pairs
1694 	 * that are set in this function. A register of 0 ends the list */
1695 	u32 regs[][2] = {
1696 		{ SPIDER_NET_GRESUMINTNUM, 0 },
1697 		{ SPIDER_NET_GREINTNUM, 0 },
1698 
1699 		/* set interrupt frame number registers */
1700 		/* clear the single DMA engine registers first */
1701 		{ SPIDER_NET_GFAFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1702 		{ SPIDER_NET_GFBFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1703 		{ SPIDER_NET_GFCFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1704 		{ SPIDER_NET_GFDFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1705 		/* then set, what we really need */
1706 		{ SPIDER_NET_GFFRMNUM, SPIDER_NET_FRAMENUM_VALUE },
1707 
1708 		/* timer counter registers and stuff */
1709 		{ SPIDER_NET_GFREECNNUM, 0 },
1710 		{ SPIDER_NET_GONETIMENUM, 0 },
1711 		{ SPIDER_NET_GTOUTFRMNUM, 0 },
1712 
1713 		/* RX mode setting */
1714 		{ SPIDER_NET_GRXMDSET, SPIDER_NET_RXMODE_VALUE },
1715 		/* TX mode setting */
1716 		{ SPIDER_NET_GTXMDSET, SPIDER_NET_TXMODE_VALUE },
1717 		/* IPSEC mode setting */
1718 		{ SPIDER_NET_GIPSECINIT, SPIDER_NET_IPSECINIT_VALUE },
1719 
1720 		{ SPIDER_NET_GFTRESTRT, SPIDER_NET_RESTART_VALUE },
1721 
1722 		{ SPIDER_NET_GMRWOLCTRL, 0 },
1723 		{ SPIDER_NET_GTESTMD, 0x10000000 },
1724 		{ SPIDER_NET_GTTQMSK, 0x00400040 },
1725 
1726 		{ SPIDER_NET_GMACINTEN, 0 },
1727 
1728 		/* flow control stuff */
1729 		{ SPIDER_NET_GMACAPAUSE, SPIDER_NET_MACAPAUSE_VALUE },
1730 		{ SPIDER_NET_GMACTXPAUSE, SPIDER_NET_TXPAUSE_VALUE },
1731 
1732 		{ SPIDER_NET_GMACBSTLMT, SPIDER_NET_BURSTLMT_VALUE },
1733 		{ 0, 0}
1734 	};
1735 
1736 	i = 0;
1737 	while (regs[i][0]) {
1738 		spider_net_write_reg(card, regs[i][0], regs[i][1]);
1739 		i++;
1740 	}
1741 
1742 	/* clear unicast filter table entries 1 to 14 */
1743 	for (i = 1; i <= 14; i++) {
1744 		spider_net_write_reg(card,
1745 				     SPIDER_NET_GMRUAFILnR + i * 8,
1746 				     0x00080000);
1747 		spider_net_write_reg(card,
1748 				     SPIDER_NET_GMRUAFILnR + i * 8 + 4,
1749 				     0x00000000);
1750 	}
1751 
1752 	spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R, 0x08080000);
1753 
1754 	spider_net_write_reg(card, SPIDER_NET_ECMODE, SPIDER_NET_ECMODE_VALUE);
1755 
1756 	/* set chain tail address for RX chains and
1757 	 * enable DMA */
1758 	spider_net_enable_rxchtails(card);
1759 	spider_net_enable_rxdmac(card);
1760 
1761 	spider_net_write_reg(card, SPIDER_NET_GRXDMAEN, SPIDER_NET_WOL_VALUE);
1762 
1763 	spider_net_write_reg(card, SPIDER_NET_GMACLENLMT,
1764 			     SPIDER_NET_LENLMT_VALUE);
1765 	spider_net_write_reg(card, SPIDER_NET_GMACOPEMD,
1766 			     SPIDER_NET_OPMODE_VALUE);
1767 
1768 	spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
1769 			     SPIDER_NET_GDTBSTA);
1770 }
1771 
1772 /**
1773  * spider_net_download_firmware - loads firmware into the adapter
1774  * @card: card structure
1775  * @firmware_ptr: pointer to firmware data
1776  *
1777  * spider_net_download_firmware loads the firmware data into the
1778  * adapter. It assumes the length etc. to be allright.
1779  */
1780 static int
1781 spider_net_download_firmware(struct spider_net_card *card,
1782 			     const void *firmware_ptr)
1783 {
1784 	int sequencer, i;
1785 	const u32 *fw_ptr = firmware_ptr;
1786 
1787 	/* stop sequencers */
1788 	spider_net_write_reg(card, SPIDER_NET_GSINIT,
1789 			     SPIDER_NET_STOP_SEQ_VALUE);
1790 
1791 	for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1792 	     sequencer++) {
1793 		spider_net_write_reg(card,
1794 				     SPIDER_NET_GSnPRGADR + sequencer * 8, 0);
1795 		for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
1796 			spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1797 					     sequencer * 8, *fw_ptr);
1798 			fw_ptr++;
1799 		}
1800 	}
1801 
1802 	if (spider_net_read_reg(card, SPIDER_NET_GSINIT))
1803 		return -EIO;
1804 
1805 	spider_net_write_reg(card, SPIDER_NET_GSINIT,
1806 			     SPIDER_NET_RUN_SEQ_VALUE);
1807 
1808 	return 0;
1809 }
1810 
1811 /**
1812  * spider_net_init_firmware - reads in firmware parts
1813  * @card: card structure
1814  *
1815  * Returns 0 on success, <0 on failure
1816  *
1817  * spider_net_init_firmware opens the sequencer firmware and does some basic
1818  * checks. This function opens and releases the firmware structure. A call
1819  * to download the firmware is performed before the release.
1820  *
1821  * Firmware format
1822  * ===============
1823  * spider_fw.bin is expected to be a file containing 6*1024*4 bytes, 4k being
1824  * the program for each sequencer. Use the command
1825  *    tail -q -n +2 Seq_code1_0x088.txt Seq_code2_0x090.txt              \
1826  *         Seq_code3_0x098.txt Seq_code4_0x0A0.txt Seq_code5_0x0A8.txt   \
1827  *         Seq_code6_0x0B0.txt | xxd -r -p -c4 > spider_fw.bin
1828  *
1829  * to generate spider_fw.bin, if you have sequencer programs with something
1830  * like the following contents for each sequencer:
1831  *    <ONE LINE COMMENT>
1832  *    <FIRST 4-BYTES-WORD FOR SEQUENCER>
1833  *    <SECOND 4-BYTES-WORD FOR SEQUENCER>
1834  *     ...
1835  *    <1024th 4-BYTES-WORD FOR SEQUENCER>
1836  */
1837 static int
1838 spider_net_init_firmware(struct spider_net_card *card)
1839 {
1840 	struct firmware *firmware = NULL;
1841 	struct device_node *dn;
1842 	const u8 *fw_prop = NULL;
1843 	int err = -ENOENT;
1844 	int fw_size;
1845 
1846 	if (request_firmware((const struct firmware **)&firmware,
1847 			     SPIDER_NET_FIRMWARE_NAME, &card->pdev->dev) == 0) {
1848 		if ( (firmware->size != SPIDER_NET_FIRMWARE_LEN) &&
1849 		     netif_msg_probe(card) ) {
1850 			dev_err(&card->netdev->dev,
1851 			       "Incorrect size of spidernet firmware in " \
1852 			       "filesystem. Looking in host firmware...\n");
1853 			goto try_host_fw;
1854 		}
1855 		err = spider_net_download_firmware(card, firmware->data);
1856 
1857 		release_firmware(firmware);
1858 		if (err)
1859 			goto try_host_fw;
1860 
1861 		goto done;
1862 	}
1863 
1864 try_host_fw:
1865 	dn = pci_device_to_OF_node(card->pdev);
1866 	if (!dn)
1867 		goto out_err;
1868 
1869 	fw_prop = of_get_property(dn, "firmware", &fw_size);
1870 	if (!fw_prop)
1871 		goto out_err;
1872 
1873 	if ( (fw_size != SPIDER_NET_FIRMWARE_LEN) &&
1874 	     netif_msg_probe(card) ) {
1875 		dev_err(&card->netdev->dev,
1876 		       "Incorrect size of spidernet firmware in host firmware\n");
1877 		goto done;
1878 	}
1879 
1880 	err = spider_net_download_firmware(card, fw_prop);
1881 
1882 done:
1883 	return err;
1884 out_err:
1885 	if (netif_msg_probe(card))
1886 		dev_err(&card->netdev->dev,
1887 		       "Couldn't find spidernet firmware in filesystem " \
1888 		       "or host firmware\n");
1889 	return err;
1890 }
1891 
1892 /**
1893  * spider_net_open - called upon ifonfig up
1894  * @netdev: interface device structure
1895  *
1896  * returns 0 on success, <0 on failure
1897  *
1898  * spider_net_open allocates all the descriptors and memory needed for
1899  * operation, sets up multicast list and enables interrupts
1900  */
1901 int
1902 spider_net_open(struct net_device *netdev)
1903 {
1904 	struct spider_net_card *card = netdev_priv(netdev);
1905 	int result;
1906 
1907 	result = spider_net_init_firmware(card);
1908 	if (result)
1909 		goto init_firmware_failed;
1910 
1911 	/* start probing with copper */
1912 	card->aneg_count = 0;
1913 	card->medium = BCM54XX_COPPER;
1914 	spider_net_setup_aneg(card);
1915 	if (card->phy.def->phy_id)
1916 		mod_timer(&card->aneg_timer, jiffies + SPIDER_NET_ANEG_TIMER);
1917 
1918 	result = spider_net_init_chain(card, &card->tx_chain);
1919 	if (result)
1920 		goto alloc_tx_failed;
1921 	card->low_watermark = NULL;
1922 
1923 	result = spider_net_init_chain(card, &card->rx_chain);
1924 	if (result)
1925 		goto alloc_rx_failed;
1926 
1927 	/* Allocate rx skbs */
1928 	result = spider_net_alloc_rx_skbs(card);
1929 	if (result)
1930 		goto alloc_skbs_failed;
1931 
1932 	spider_net_set_multi(netdev);
1933 
1934 	/* further enhancement: setup hw vlan, if needed */
1935 
1936 	result = -EBUSY;
1937 	if (request_irq(netdev->irq, spider_net_interrupt,
1938 			     IRQF_SHARED, netdev->name, netdev))
1939 		goto register_int_failed;
1940 
1941 	spider_net_enable_card(card);
1942 
1943 	netif_start_queue(netdev);
1944 	netif_carrier_on(netdev);
1945 	napi_enable(&card->napi);
1946 
1947 	spider_net_enable_interrupts(card);
1948 
1949 	return 0;
1950 
1951 register_int_failed:
1952 	spider_net_free_rx_chain_contents(card);
1953 alloc_skbs_failed:
1954 	spider_net_free_chain(card, &card->rx_chain);
1955 alloc_rx_failed:
1956 	spider_net_free_chain(card, &card->tx_chain);
1957 alloc_tx_failed:
1958 	del_timer_sync(&card->aneg_timer);
1959 init_firmware_failed:
1960 	return result;
1961 }
1962 
1963 /**
1964  * spider_net_link_phy
1965  * @data: used for pointer to card structure
1966  *
1967  */
1968 static void spider_net_link_phy(struct timer_list *t)
1969 {
1970 	struct spider_net_card *card = from_timer(card, t, aneg_timer);
1971 	struct mii_phy *phy = &card->phy;
1972 
1973 	/* if link didn't come up after SPIDER_NET_ANEG_TIMEOUT tries, setup phy again */
1974 	if (card->aneg_count > SPIDER_NET_ANEG_TIMEOUT) {
1975 
1976 		pr_debug("%s: link is down trying to bring it up\n",
1977 			 card->netdev->name);
1978 
1979 		switch (card->medium) {
1980 		case BCM54XX_COPPER:
1981 			/* enable fiber with autonegotiation first */
1982 			if (phy->def->ops->enable_fiber)
1983 				phy->def->ops->enable_fiber(phy, 1);
1984 			card->medium = BCM54XX_FIBER;
1985 			break;
1986 
1987 		case BCM54XX_FIBER:
1988 			/* fiber didn't come up, try to disable fiber autoneg */
1989 			if (phy->def->ops->enable_fiber)
1990 				phy->def->ops->enable_fiber(phy, 0);
1991 			card->medium = BCM54XX_UNKNOWN;
1992 			break;
1993 
1994 		case BCM54XX_UNKNOWN:
1995 			/* copper, fiber with and without failed,
1996 			 * retry from beginning */
1997 			spider_net_setup_aneg(card);
1998 			card->medium = BCM54XX_COPPER;
1999 			break;
2000 		}
2001 
2002 		card->aneg_count = 0;
2003 		mod_timer(&card->aneg_timer, jiffies + SPIDER_NET_ANEG_TIMER);
2004 		return;
2005 	}
2006 
2007 	/* link still not up, try again later */
2008 	if (!(phy->def->ops->poll_link(phy))) {
2009 		card->aneg_count++;
2010 		mod_timer(&card->aneg_timer, jiffies + SPIDER_NET_ANEG_TIMER);
2011 		return;
2012 	}
2013 
2014 	/* link came up, get abilities */
2015 	phy->def->ops->read_link(phy);
2016 
2017 	spider_net_write_reg(card, SPIDER_NET_GMACST,
2018 			     spider_net_read_reg(card, SPIDER_NET_GMACST));
2019 	spider_net_write_reg(card, SPIDER_NET_GMACINTEN, 0x4);
2020 
2021 	if (phy->speed == 1000)
2022 		spider_net_write_reg(card, SPIDER_NET_GMACMODE, 0x00000001);
2023 	else
2024 		spider_net_write_reg(card, SPIDER_NET_GMACMODE, 0);
2025 
2026 	card->aneg_count = 0;
2027 
2028 	pr_info("%s: link up, %i Mbps, %s-duplex %sautoneg.\n",
2029 		card->netdev->name, phy->speed,
2030 		phy->duplex == 1 ? "Full" : "Half",
2031 		phy->autoneg == 1 ? "" : "no ");
2032 }
2033 
2034 /**
2035  * spider_net_setup_phy - setup PHY
2036  * @card: card structure
2037  *
2038  * returns 0 on success, <0 on failure
2039  *
2040  * spider_net_setup_phy is used as part of spider_net_probe.
2041  **/
2042 static int
2043 spider_net_setup_phy(struct spider_net_card *card)
2044 {
2045 	struct mii_phy *phy = &card->phy;
2046 
2047 	spider_net_write_reg(card, SPIDER_NET_GDTDMASEL,
2048 			     SPIDER_NET_DMASEL_VALUE);
2049 	spider_net_write_reg(card, SPIDER_NET_GPCCTRL,
2050 			     SPIDER_NET_PHY_CTRL_VALUE);
2051 
2052 	phy->dev = card->netdev;
2053 	phy->mdio_read = spider_net_read_phy;
2054 	phy->mdio_write = spider_net_write_phy;
2055 
2056 	for (phy->mii_id = 1; phy->mii_id <= 31; phy->mii_id++) {
2057 		unsigned short id;
2058 		id = spider_net_read_phy(card->netdev, phy->mii_id, MII_BMSR);
2059 		if (id != 0x0000 && id != 0xffff) {
2060 			if (!sungem_phy_probe(phy, phy->mii_id)) {
2061 				pr_info("Found %s.\n", phy->def->name);
2062 				break;
2063 			}
2064 		}
2065 	}
2066 
2067 	return 0;
2068 }
2069 
2070 /**
2071  * spider_net_workaround_rxramfull - work around firmware bug
2072  * @card: card structure
2073  *
2074  * no return value
2075  **/
2076 static void
2077 spider_net_workaround_rxramfull(struct spider_net_card *card)
2078 {
2079 	int i, sequencer = 0;
2080 
2081 	/* cancel reset */
2082 	spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2083 			     SPIDER_NET_CKRCTRL_RUN_VALUE);
2084 
2085 	/* empty sequencer data */
2086 	for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
2087 	     sequencer++) {
2088 		spider_net_write_reg(card, SPIDER_NET_GSnPRGADR +
2089 				     sequencer * 8, 0x0);
2090 		for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
2091 			spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
2092 					     sequencer * 8, 0x0);
2093 		}
2094 	}
2095 
2096 	/* set sequencer operation */
2097 	spider_net_write_reg(card, SPIDER_NET_GSINIT, 0x000000fe);
2098 
2099 	/* reset */
2100 	spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2101 			     SPIDER_NET_CKRCTRL_STOP_VALUE);
2102 }
2103 
2104 /**
2105  * spider_net_stop - called upon ifconfig down
2106  * @netdev: interface device structure
2107  *
2108  * always returns 0
2109  */
2110 int
2111 spider_net_stop(struct net_device *netdev)
2112 {
2113 	struct spider_net_card *card = netdev_priv(netdev);
2114 
2115 	napi_disable(&card->napi);
2116 	netif_carrier_off(netdev);
2117 	netif_stop_queue(netdev);
2118 	del_timer_sync(&card->tx_timer);
2119 	del_timer_sync(&card->aneg_timer);
2120 
2121 	spider_net_disable_interrupts(card);
2122 
2123 	free_irq(netdev->irq, netdev);
2124 
2125 	spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
2126 			     SPIDER_NET_DMA_TX_FEND_VALUE);
2127 
2128 	/* turn off DMA, force end */
2129 	spider_net_disable_rxdmac(card);
2130 
2131 	/* release chains */
2132 	spider_net_release_tx_chain(card, 1);
2133 	spider_net_free_rx_chain_contents(card);
2134 
2135 	spider_net_free_chain(card, &card->tx_chain);
2136 	spider_net_free_chain(card, &card->rx_chain);
2137 
2138 	return 0;
2139 }
2140 
2141 /**
2142  * spider_net_tx_timeout_task - task scheduled by the watchdog timeout
2143  * function (to be called not under interrupt status)
2144  * @data: data, is interface device structure
2145  *
2146  * called as task when tx hangs, resets interface (if interface is up)
2147  */
2148 static void
2149 spider_net_tx_timeout_task(struct work_struct *work)
2150 {
2151 	struct spider_net_card *card =
2152 		container_of(work, struct spider_net_card, tx_timeout_task);
2153 	struct net_device *netdev = card->netdev;
2154 
2155 	if (!(netdev->flags & IFF_UP))
2156 		goto out;
2157 
2158 	netif_device_detach(netdev);
2159 	spider_net_stop(netdev);
2160 
2161 	spider_net_workaround_rxramfull(card);
2162 	spider_net_init_card(card);
2163 
2164 	if (spider_net_setup_phy(card))
2165 		goto out;
2166 
2167 	spider_net_open(netdev);
2168 	spider_net_kick_tx_dma(card);
2169 	netif_device_attach(netdev);
2170 
2171 out:
2172 	atomic_dec(&card->tx_timeout_task_counter);
2173 }
2174 
2175 /**
2176  * spider_net_tx_timeout - called when the tx timeout watchdog kicks in.
2177  * @netdev: interface device structure
2178  *
2179  * called, if tx hangs. Schedules a task that resets the interface
2180  */
2181 static void
2182 spider_net_tx_timeout(struct net_device *netdev)
2183 {
2184 	struct spider_net_card *card;
2185 
2186 	card = netdev_priv(netdev);
2187 	atomic_inc(&card->tx_timeout_task_counter);
2188 	if (netdev->flags & IFF_UP)
2189 		schedule_work(&card->tx_timeout_task);
2190 	else
2191 		atomic_dec(&card->tx_timeout_task_counter);
2192 	card->spider_stats.tx_timeouts++;
2193 }
2194 
2195 static const struct net_device_ops spider_net_ops = {
2196 	.ndo_open		= spider_net_open,
2197 	.ndo_stop		= spider_net_stop,
2198 	.ndo_start_xmit		= spider_net_xmit,
2199 	.ndo_set_rx_mode	= spider_net_set_multi,
2200 	.ndo_set_mac_address	= spider_net_set_mac,
2201 	.ndo_do_ioctl		= spider_net_do_ioctl,
2202 	.ndo_tx_timeout		= spider_net_tx_timeout,
2203 	.ndo_validate_addr	= eth_validate_addr,
2204 	/* HW VLAN */
2205 #ifdef CONFIG_NET_POLL_CONTROLLER
2206 	/* poll controller */
2207 	.ndo_poll_controller	= spider_net_poll_controller,
2208 #endif /* CONFIG_NET_POLL_CONTROLLER */
2209 };
2210 
2211 /**
2212  * spider_net_setup_netdev_ops - initialization of net_device operations
2213  * @netdev: net_device structure
2214  *
2215  * fills out function pointers in the net_device structure
2216  */
2217 static void
2218 spider_net_setup_netdev_ops(struct net_device *netdev)
2219 {
2220 	netdev->netdev_ops = &spider_net_ops;
2221 	netdev->watchdog_timeo = SPIDER_NET_WATCHDOG_TIMEOUT;
2222 	/* ethtool ops */
2223 	netdev->ethtool_ops = &spider_net_ethtool_ops;
2224 }
2225 
2226 /**
2227  * spider_net_setup_netdev - initialization of net_device
2228  * @card: card structure
2229  *
2230  * Returns 0 on success or <0 on failure
2231  *
2232  * spider_net_setup_netdev initializes the net_device structure
2233  **/
2234 static int
2235 spider_net_setup_netdev(struct spider_net_card *card)
2236 {
2237 	int result;
2238 	struct net_device *netdev = card->netdev;
2239 	struct device_node *dn;
2240 	struct sockaddr addr;
2241 	const u8 *mac;
2242 
2243 	SET_NETDEV_DEV(netdev, &card->pdev->dev);
2244 
2245 	pci_set_drvdata(card->pdev, netdev);
2246 
2247 	timer_setup(&card->tx_timer, spider_net_cleanup_tx_ring, 0);
2248 	netdev->irq = card->pdev->irq;
2249 
2250 	card->aneg_count = 0;
2251 	timer_setup(&card->aneg_timer, spider_net_link_phy, 0);
2252 
2253 	netif_napi_add(netdev, &card->napi,
2254 		       spider_net_poll, SPIDER_NET_NAPI_WEIGHT);
2255 
2256 	spider_net_setup_netdev_ops(netdev);
2257 
2258 	netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM;
2259 	if (SPIDER_NET_RX_CSUM_DEFAULT)
2260 		netdev->features |= NETIF_F_RXCSUM;
2261 	netdev->features |= NETIF_F_IP_CSUM | NETIF_F_LLTX;
2262 	/* some time: NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
2263 	 *		NETIF_F_HW_VLAN_CTAG_FILTER */
2264 
2265 	/* MTU range: 64 - 2294 */
2266 	netdev->min_mtu = SPIDER_NET_MIN_MTU;
2267 	netdev->max_mtu = SPIDER_NET_MAX_MTU;
2268 
2269 	netdev->irq = card->pdev->irq;
2270 	card->num_rx_ints = 0;
2271 	card->ignore_rx_ramfull = 0;
2272 
2273 	dn = pci_device_to_OF_node(card->pdev);
2274 	if (!dn)
2275 		return -EIO;
2276 
2277 	mac = of_get_property(dn, "local-mac-address", NULL);
2278 	if (!mac)
2279 		return -EIO;
2280 	memcpy(addr.sa_data, mac, ETH_ALEN);
2281 
2282 	result = spider_net_set_mac(netdev, &addr);
2283 	if ((result) && (netif_msg_probe(card)))
2284 		dev_err(&card->netdev->dev,
2285 		        "Failed to set MAC address: %i\n", result);
2286 
2287 	result = register_netdev(netdev);
2288 	if (result) {
2289 		if (netif_msg_probe(card))
2290 			dev_err(&card->netdev->dev,
2291 			        "Couldn't register net_device: %i\n", result);
2292 		return result;
2293 	}
2294 
2295 	if (netif_msg_probe(card))
2296 		pr_info("Initialized device %s.\n", netdev->name);
2297 
2298 	return 0;
2299 }
2300 
2301 /**
2302  * spider_net_alloc_card - allocates net_device and card structure
2303  *
2304  * returns the card structure or NULL in case of errors
2305  *
2306  * the card and net_device structures are linked to each other
2307  */
2308 static struct spider_net_card *
2309 spider_net_alloc_card(void)
2310 {
2311 	struct net_device *netdev;
2312 	struct spider_net_card *card;
2313 	size_t alloc_size;
2314 
2315 	alloc_size = sizeof(struct spider_net_card) +
2316 	   (tx_descriptors + rx_descriptors) * sizeof(struct spider_net_descr);
2317 	netdev = alloc_etherdev(alloc_size);
2318 	if (!netdev)
2319 		return NULL;
2320 
2321 	card = netdev_priv(netdev);
2322 	card->netdev = netdev;
2323 	card->msg_enable = SPIDER_NET_DEFAULT_MSG;
2324 	INIT_WORK(&card->tx_timeout_task, spider_net_tx_timeout_task);
2325 	init_waitqueue_head(&card->waitq);
2326 	atomic_set(&card->tx_timeout_task_counter, 0);
2327 
2328 	card->rx_chain.num_desc = rx_descriptors;
2329 	card->rx_chain.ring = card->darray;
2330 	card->tx_chain.num_desc = tx_descriptors;
2331 	card->tx_chain.ring = card->darray + rx_descriptors;
2332 
2333 	return card;
2334 }
2335 
2336 /**
2337  * spider_net_undo_pci_setup - releases PCI ressources
2338  * @card: card structure
2339  *
2340  * spider_net_undo_pci_setup releases the mapped regions
2341  */
2342 static void
2343 spider_net_undo_pci_setup(struct spider_net_card *card)
2344 {
2345 	iounmap(card->regs);
2346 	pci_release_regions(card->pdev);
2347 }
2348 
2349 /**
2350  * spider_net_setup_pci_dev - sets up the device in terms of PCI operations
2351  * @pdev: PCI device
2352  *
2353  * Returns the card structure or NULL if any errors occur
2354  *
2355  * spider_net_setup_pci_dev initializes pdev and together with the
2356  * functions called in spider_net_open configures the device so that
2357  * data can be transferred over it
2358  * The net_device structure is attached to the card structure, if the
2359  * function returns without error.
2360  **/
2361 static struct spider_net_card *
2362 spider_net_setup_pci_dev(struct pci_dev *pdev)
2363 {
2364 	struct spider_net_card *card;
2365 	unsigned long mmio_start, mmio_len;
2366 
2367 	if (pci_enable_device(pdev)) {
2368 		dev_err(&pdev->dev, "Couldn't enable PCI device\n");
2369 		return NULL;
2370 	}
2371 
2372 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
2373 		dev_err(&pdev->dev,
2374 		        "Couldn't find proper PCI device base address.\n");
2375 		goto out_disable_dev;
2376 	}
2377 
2378 	if (pci_request_regions(pdev, spider_net_driver_name)) {
2379 		dev_err(&pdev->dev,
2380 		        "Couldn't obtain PCI resources, aborting.\n");
2381 		goto out_disable_dev;
2382 	}
2383 
2384 	pci_set_master(pdev);
2385 
2386 	card = spider_net_alloc_card();
2387 	if (!card) {
2388 		dev_err(&pdev->dev,
2389 		        "Couldn't allocate net_device structure, aborting.\n");
2390 		goto out_release_regions;
2391 	}
2392 	card->pdev = pdev;
2393 
2394 	/* fetch base address and length of first resource */
2395 	mmio_start = pci_resource_start(pdev, 0);
2396 	mmio_len = pci_resource_len(pdev, 0);
2397 
2398 	card->netdev->mem_start = mmio_start;
2399 	card->netdev->mem_end = mmio_start + mmio_len;
2400 	card->regs = ioremap(mmio_start, mmio_len);
2401 
2402 	if (!card->regs) {
2403 		dev_err(&pdev->dev,
2404 		        "Couldn't obtain PCI resources, aborting.\n");
2405 		goto out_release_regions;
2406 	}
2407 
2408 	return card;
2409 
2410 out_release_regions:
2411 	pci_release_regions(pdev);
2412 out_disable_dev:
2413 	pci_disable_device(pdev);
2414 	return NULL;
2415 }
2416 
2417 /**
2418  * spider_net_probe - initialization of a device
2419  * @pdev: PCI device
2420  * @ent: entry in the device id list
2421  *
2422  * Returns 0 on success, <0 on failure
2423  *
2424  * spider_net_probe initializes pdev and registers a net_device
2425  * structure for it. After that, the device can be ifconfig'ed up
2426  **/
2427 static int
2428 spider_net_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2429 {
2430 	int err = -EIO;
2431 	struct spider_net_card *card;
2432 
2433 	card = spider_net_setup_pci_dev(pdev);
2434 	if (!card)
2435 		goto out;
2436 
2437 	spider_net_workaround_rxramfull(card);
2438 	spider_net_init_card(card);
2439 
2440 	err = spider_net_setup_phy(card);
2441 	if (err)
2442 		goto out_undo_pci;
2443 
2444 	err = spider_net_setup_netdev(card);
2445 	if (err)
2446 		goto out_undo_pci;
2447 
2448 	return 0;
2449 
2450 out_undo_pci:
2451 	spider_net_undo_pci_setup(card);
2452 	free_netdev(card->netdev);
2453 out:
2454 	return err;
2455 }
2456 
2457 /**
2458  * spider_net_remove - removal of a device
2459  * @pdev: PCI device
2460  *
2461  * Returns 0 on success, <0 on failure
2462  *
2463  * spider_net_remove is called to remove the device and unregisters the
2464  * net_device
2465  **/
2466 static void
2467 spider_net_remove(struct pci_dev *pdev)
2468 {
2469 	struct net_device *netdev;
2470 	struct spider_net_card *card;
2471 
2472 	netdev = pci_get_drvdata(pdev);
2473 	card = netdev_priv(netdev);
2474 
2475 	wait_event(card->waitq,
2476 		   atomic_read(&card->tx_timeout_task_counter) == 0);
2477 
2478 	unregister_netdev(netdev);
2479 
2480 	/* switch off card */
2481 	spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2482 			     SPIDER_NET_CKRCTRL_STOP_VALUE);
2483 	spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2484 			     SPIDER_NET_CKRCTRL_RUN_VALUE);
2485 
2486 	spider_net_undo_pci_setup(card);
2487 	free_netdev(netdev);
2488 }
2489 
2490 static struct pci_driver spider_net_driver = {
2491 	.name		= spider_net_driver_name,
2492 	.id_table	= spider_net_pci_tbl,
2493 	.probe		= spider_net_probe,
2494 	.remove		= spider_net_remove
2495 };
2496 
2497 /**
2498  * spider_net_init - init function when the driver is loaded
2499  *
2500  * spider_net_init registers the device driver
2501  */
2502 static int __init spider_net_init(void)
2503 {
2504 	printk(KERN_INFO "Spidernet version %s.\n", VERSION);
2505 
2506 	if (rx_descriptors < SPIDER_NET_RX_DESCRIPTORS_MIN) {
2507 		rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MIN;
2508 		pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2509 	}
2510 	if (rx_descriptors > SPIDER_NET_RX_DESCRIPTORS_MAX) {
2511 		rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MAX;
2512 		pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2513 	}
2514 	if (tx_descriptors < SPIDER_NET_TX_DESCRIPTORS_MIN) {
2515 		tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MIN;
2516 		pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2517 	}
2518 	if (tx_descriptors > SPIDER_NET_TX_DESCRIPTORS_MAX) {
2519 		tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MAX;
2520 		pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2521 	}
2522 
2523 	return pci_register_driver(&spider_net_driver);
2524 }
2525 
2526 /**
2527  * spider_net_cleanup - exit function when driver is unloaded
2528  *
2529  * spider_net_cleanup unregisters the device driver
2530  */
2531 static void __exit spider_net_cleanup(void)
2532 {
2533 	pci_unregister_driver(&spider_net_driver);
2534 }
2535 
2536 module_init(spider_net_init);
2537 module_exit(spider_net_cleanup);
2538