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