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