Spidernet: Rework RX linked list
[wrt350n-kernel.git] / drivers / net / spider_net.c
blobbf6ff39e02bbc035e2da03992be4c3661d07fa1c
1 /*
2 * Network device driver for Cell Processor-Based Blade
4 * (C) Copyright IBM Corp. 2005
6 * Authors : Utz Bacher <utz.bacher@de.ibm.com>
7 * Jens Osterkamp <Jens.Osterkamp@de.ibm.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
12 * any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/compiler.h>
25 #include <linux/crc32.h>
26 #include <linux/delay.h>
27 #include <linux/etherdevice.h>
28 #include <linux/ethtool.h>
29 #include <linux/firmware.h>
30 #include <linux/if_vlan.h>
31 #include <linux/in.h>
32 #include <linux/init.h>
33 #include <linux/ioport.h>
34 #include <linux/ip.h>
35 #include <linux/kernel.h>
36 #include <linux/mii.h>
37 #include <linux/module.h>
38 #include <linux/netdevice.h>
39 #include <linux/device.h>
40 #include <linux/pci.h>
41 #include <linux/skbuff.h>
42 #include <linux/slab.h>
43 #include <linux/tcp.h>
44 #include <linux/types.h>
45 #include <linux/vmalloc.h>
46 #include <linux/wait.h>
47 #include <linux/workqueue.h>
48 #include <asm/bitops.h>
49 #include <asm/pci-bridge.h>
50 #include <net/checksum.h>
52 #include "spider_net.h"
54 MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com> and Jens Osterkamp " \
55 "<Jens.Osterkamp@de.ibm.com>");
56 MODULE_DESCRIPTION("Spider Southbridge Gigabit Ethernet driver");
57 MODULE_LICENSE("GPL");
58 MODULE_VERSION(VERSION);
60 static int rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_DEFAULT;
61 static int tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_DEFAULT;
63 module_param(rx_descriptors, int, 0444);
64 module_param(tx_descriptors, int, 0444);
66 MODULE_PARM_DESC(rx_descriptors, "number of descriptors used " \
67 "in rx chains");
68 MODULE_PARM_DESC(tx_descriptors, "number of descriptors used " \
69 "in tx chain");
71 char spider_net_driver_name[] = "spidernet";
73 static struct pci_device_id spider_net_pci_tbl[] = {
74 { PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_SPIDER_NET,
75 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
76 { 0, }
79 MODULE_DEVICE_TABLE(pci, spider_net_pci_tbl);
81 /**
82 * spider_net_read_reg - reads an SMMIO register of a card
83 * @card: device structure
84 * @reg: register to read from
86 * returns the content of the specified SMMIO register.
88 static inline u32
89 spider_net_read_reg(struct spider_net_card *card, u32 reg)
91 /* We use the powerpc specific variants instead of readl_be() because
92 * we know spidernet is not a real PCI device and we can thus avoid the
93 * performance hit caused by the PCI workarounds.
95 return in_be32(card->regs + reg);
98 /**
99 * spider_net_write_reg - writes to an SMMIO register of a card
100 * @card: device structure
101 * @reg: register to write to
102 * @value: value to write into the specified SMMIO register
104 static inline void
105 spider_net_write_reg(struct spider_net_card *card, u32 reg, u32 value)
107 /* We use the powerpc specific variants instead of writel_be() because
108 * we know spidernet is not a real PCI device and we can thus avoid the
109 * performance hit caused by the PCI workarounds.
111 out_be32(card->regs + reg, value);
114 /** spider_net_write_phy - write to phy register
115 * @netdev: adapter to be written to
116 * @mii_id: id of MII
117 * @reg: PHY register
118 * @val: value to be written to phy register
120 * spider_net_write_phy_register writes to an arbitrary PHY
121 * register via the spider GPCWOPCMD register. We assume the queue does
122 * not run full (not more than 15 commands outstanding).
124 static void
125 spider_net_write_phy(struct net_device *netdev, int mii_id,
126 int reg, int val)
128 struct spider_net_card *card = netdev_priv(netdev);
129 u32 writevalue;
131 writevalue = ((u32)mii_id << 21) |
132 ((u32)reg << 16) | ((u32)val);
134 spider_net_write_reg(card, SPIDER_NET_GPCWOPCMD, writevalue);
137 /** spider_net_read_phy - read from phy register
138 * @netdev: network device to be read from
139 * @mii_id: id of MII
140 * @reg: PHY register
142 * Returns value read from PHY register
144 * spider_net_write_phy reads from an arbitrary PHY
145 * register via the spider GPCROPCMD register
147 static int
148 spider_net_read_phy(struct net_device *netdev, int mii_id, int reg)
150 struct spider_net_card *card = netdev_priv(netdev);
151 u32 readvalue;
153 readvalue = ((u32)mii_id << 21) | ((u32)reg << 16);
154 spider_net_write_reg(card, SPIDER_NET_GPCROPCMD, readvalue);
156 /* we don't use semaphores to wait for an SPIDER_NET_GPROPCMPINT
157 * interrupt, as we poll for the completion of the read operation
158 * in spider_net_read_phy. Should take about 50 us */
159 do {
160 readvalue = spider_net_read_reg(card, SPIDER_NET_GPCROPCMD);
161 } while (readvalue & SPIDER_NET_GPREXEC);
163 readvalue &= SPIDER_NET_GPRDAT_MASK;
165 return readvalue;
169 * spider_net_rx_irq_off - switch off rx irq on this spider card
170 * @card: device structure
172 * switches off rx irq by masking them out in the GHIINTnMSK register
174 static void
175 spider_net_rx_irq_off(struct spider_net_card *card)
177 u32 regvalue;
179 regvalue = SPIDER_NET_INT0_MASK_VALUE & (~SPIDER_NET_RXINT);
180 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, regvalue);
184 * spider_net_rx_irq_on - switch on rx irq on this spider card
185 * @card: device structure
187 * switches on rx irq by enabling them in the GHIINTnMSK register
189 static void
190 spider_net_rx_irq_on(struct spider_net_card *card)
192 u32 regvalue;
194 regvalue = SPIDER_NET_INT0_MASK_VALUE | SPIDER_NET_RXINT;
195 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, regvalue);
199 * spider_net_set_promisc - sets the unicast address or the promiscuous mode
200 * @card: card structure
202 * spider_net_set_promisc sets the unicast destination address filter and
203 * thus either allows for non-promisc mode or promisc mode
205 static void
206 spider_net_set_promisc(struct spider_net_card *card)
208 u32 macu, macl;
209 struct net_device *netdev = card->netdev;
211 if (netdev->flags & IFF_PROMISC) {
212 /* clear destination entry 0 */
213 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR, 0);
214 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR + 0x04, 0);
215 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R,
216 SPIDER_NET_PROMISC_VALUE);
217 } else {
218 macu = netdev->dev_addr[0];
219 macu <<= 8;
220 macu |= netdev->dev_addr[1];
221 memcpy(&macl, &netdev->dev_addr[2], sizeof(macl));
223 macu |= SPIDER_NET_UA_DESCR_VALUE;
224 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR, macu);
225 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR + 0x04, macl);
226 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R,
227 SPIDER_NET_NONPROMISC_VALUE);
232 * spider_net_get_mac_address - read mac address from spider card
233 * @card: device structure
235 * reads MAC address from GMACUNIMACU and GMACUNIMACL registers
237 static int
238 spider_net_get_mac_address(struct net_device *netdev)
240 struct spider_net_card *card = netdev_priv(netdev);
241 u32 macl, macu;
243 macl = spider_net_read_reg(card, SPIDER_NET_GMACUNIMACL);
244 macu = spider_net_read_reg(card, SPIDER_NET_GMACUNIMACU);
246 netdev->dev_addr[0] = (macu >> 24) & 0xff;
247 netdev->dev_addr[1] = (macu >> 16) & 0xff;
248 netdev->dev_addr[2] = (macu >> 8) & 0xff;
249 netdev->dev_addr[3] = macu & 0xff;
250 netdev->dev_addr[4] = (macl >> 8) & 0xff;
251 netdev->dev_addr[5] = macl & 0xff;
253 if (!is_valid_ether_addr(&netdev->dev_addr[0]))
254 return -EINVAL;
256 return 0;
260 * spider_net_get_descr_status -- returns the status of a descriptor
261 * @descr: descriptor to look at
263 * returns the status as in the dmac_cmd_status field of the descriptor
265 static inline int
266 spider_net_get_descr_status(struct spider_net_descr *descr)
268 return descr->dmac_cmd_status & SPIDER_NET_DESCR_IND_PROC_MASK;
272 * spider_net_free_chain - free descriptor chain
273 * @card: card structure
274 * @chain: address of chain
277 static void
278 spider_net_free_chain(struct spider_net_card *card,
279 struct spider_net_descr_chain *chain)
281 struct spider_net_descr *descr;
283 descr = chain->ring;
284 do {
285 descr->bus_addr = 0;
286 descr->next_descr_addr = 0;
287 descr = descr->next;
288 } while (descr != chain->ring);
290 dma_free_coherent(&card->pdev->dev, chain->num_desc,
291 chain->ring, chain->dma_addr);
295 * spider_net_init_chain - alloc and link descriptor chain
296 * @card: card structure
297 * @chain: address of chain
299 * We manage a circular list that mirrors the hardware structure,
300 * except that the hardware uses bus addresses.
302 * Returns 0 on success, <0 on failure
304 static int
305 spider_net_init_chain(struct spider_net_card *card,
306 struct spider_net_descr_chain *chain)
308 int i;
309 struct spider_net_descr *descr;
310 dma_addr_t buf;
311 size_t alloc_size;
313 alloc_size = chain->num_desc * sizeof (struct spider_net_descr);
315 chain->ring = dma_alloc_coherent(&card->pdev->dev, alloc_size,
316 &chain->dma_addr, GFP_KERNEL);
318 if (!chain->ring)
319 return -ENOMEM;
321 descr = chain->ring;
322 memset(descr, 0, alloc_size);
324 /* Set up the hardware pointers in each descriptor */
325 buf = chain->dma_addr;
326 for (i=0; i < chain->num_desc; i++, descr++) {
327 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
329 descr->bus_addr = buf;
330 descr->next_descr_addr = 0;
331 descr->next = descr + 1;
332 descr->prev = descr - 1;
334 buf += sizeof(struct spider_net_descr);
336 /* do actual circular list */
337 (descr-1)->next = chain->ring;
338 chain->ring->prev = descr-1;
340 spin_lock_init(&chain->lock);
341 chain->head = chain->ring;
342 chain->tail = chain->ring;
343 return 0;
347 * spider_net_free_rx_chain_contents - frees descr contents in rx chain
348 * @card: card structure
350 * returns 0 on success, <0 on failure
352 static void
353 spider_net_free_rx_chain_contents(struct spider_net_card *card)
355 struct spider_net_descr *descr;
357 descr = card->rx_chain.head;
358 do {
359 if (descr->skb) {
360 dev_kfree_skb(descr->skb);
361 pci_unmap_single(card->pdev, descr->buf_addr,
362 SPIDER_NET_MAX_FRAME,
363 PCI_DMA_BIDIRECTIONAL);
365 descr = descr->next;
366 } while (descr != card->rx_chain.head);
370 * spider_net_prepare_rx_descr - Reinitialize RX descriptor
371 * @card: card structure
372 * @descr: descriptor to re-init
374 * Return 0 on succes, <0 on failure.
376 * Allocates a new rx skb, iommu-maps it and attaches it to the
377 * descriptor. Mark the descriptor as activated, ready-to-use.
379 static int
380 spider_net_prepare_rx_descr(struct spider_net_card *card,
381 struct spider_net_descr *descr)
383 dma_addr_t buf;
384 int offset;
385 int bufsize;
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));
391 /* and we need to have it 128 byte aligned, therefore we allocate a
392 * bit more */
393 /* allocate an skb */
394 descr->skb = dev_alloc_skb(bufsize + SPIDER_NET_RXBUF_ALIGN - 1);
395 if (!descr->skb) {
396 if (netif_msg_rx_err(card) && net_ratelimit())
397 pr_err("Not enough memory to allocate rx buffer\n");
398 card->spider_stats.alloc_rx_skb_error++;
399 return -ENOMEM;
401 descr->buf_size = bufsize;
402 descr->result_size = 0;
403 descr->valid_size = 0;
404 descr->data_status = 0;
405 descr->data_error = 0;
407 offset = ((unsigned long)descr->skb->data) &
408 (SPIDER_NET_RXBUF_ALIGN - 1);
409 if (offset)
410 skb_reserve(descr->skb, SPIDER_NET_RXBUF_ALIGN - offset);
411 /* iommu-map the skb */
412 buf = pci_map_single(card->pdev, descr->skb->data,
413 SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
414 descr->buf_addr = buf;
415 if (pci_dma_mapping_error(buf)) {
416 dev_kfree_skb_any(descr->skb);
417 if (netif_msg_rx_err(card) && net_ratelimit())
418 pr_err("Could not iommu-map rx buffer\n");
419 card->spider_stats.rx_iommu_map_error++;
420 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
421 } else {
422 descr->next_descr_addr = 0;
423 wmb();
424 descr->dmac_cmd_status = SPIDER_NET_DESCR_CARDOWNED |
425 SPIDER_NET_DMAC_NOINTR_COMPLETE;
427 wmb();
428 descr->prev->next_descr_addr = descr->bus_addr;
431 return 0;
435 * spider_net_enable_rxchtails - sets RX dmac chain tail addresses
436 * @card: card structure
438 * spider_net_enable_rxchtails sets the RX DMAC chain tail adresses in the
439 * chip by writing to the appropriate register. DMA is enabled in
440 * spider_net_enable_rxdmac.
442 static inline void
443 spider_net_enable_rxchtails(struct spider_net_card *card)
445 /* assume chain is aligned correctly */
446 spider_net_write_reg(card, SPIDER_NET_GDADCHA ,
447 card->rx_chain.tail->bus_addr);
451 * spider_net_enable_rxdmac - enables a receive DMA controller
452 * @card: card structure
454 * spider_net_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
455 * in the GDADMACCNTR register
457 static inline void
458 spider_net_enable_rxdmac(struct spider_net_card *card)
460 wmb();
461 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
462 SPIDER_NET_DMA_RX_VALUE);
466 * spider_net_refill_rx_chain - refills descriptors/skbs in the rx chains
467 * @card: card structure
469 * refills descriptors in the rx chain: allocates skbs and iommu-maps them.
471 static void
472 spider_net_refill_rx_chain(struct spider_net_card *card)
474 struct spider_net_descr_chain *chain = &card->rx_chain;
475 unsigned long flags;
477 /* one context doing the refill (and a second context seeing that
478 * and omitting it) is ok. If called by NAPI, we'll be called again
479 * as spider_net_decode_one_descr is called several times. If some
480 * interrupt calls us, the NAPI is about to clean up anyway. */
481 if (!spin_trylock_irqsave(&chain->lock, flags))
482 return;
484 while (spider_net_get_descr_status(chain->head) ==
485 SPIDER_NET_DESCR_NOT_IN_USE) {
486 if (spider_net_prepare_rx_descr(card, chain->head))
487 break;
488 chain->head = chain->head->next;
491 spin_unlock_irqrestore(&chain->lock, flags);
495 * spider_net_alloc_rx_skbs - Allocates rx skbs in rx descriptor chains
496 * @card: card structure
498 * Returns 0 on success, <0 on failure.
500 static int
501 spider_net_alloc_rx_skbs(struct spider_net_card *card)
503 int result;
504 struct spider_net_descr_chain *chain;
506 result = -ENOMEM;
508 chain = &card->rx_chain;
509 /* Put at least one buffer into the chain. if this fails,
510 * we've got a problem. If not, spider_net_refill_rx_chain
511 * will do the rest at the end of this function. */
512 if (spider_net_prepare_rx_descr(card, chain->head))
513 goto error;
514 else
515 chain->head = chain->head->next;
517 /* This will allocate the rest of the rx buffers;
518 * if not, it's business as usual later on. */
519 spider_net_refill_rx_chain(card);
520 spider_net_enable_rxdmac(card);
521 return 0;
523 error:
524 spider_net_free_rx_chain_contents(card);
525 return result;
529 * spider_net_get_multicast_hash - generates hash for multicast filter table
530 * @addr: multicast address
532 * returns the hash value.
534 * spider_net_get_multicast_hash calculates a hash value for a given multicast
535 * address, that is used to set the multicast filter tables
537 static u8
538 spider_net_get_multicast_hash(struct net_device *netdev, __u8 *addr)
540 u32 crc;
541 u8 hash;
542 char addr_for_crc[ETH_ALEN] = { 0, };
543 int i, bit;
545 for (i = 0; i < ETH_ALEN * 8; i++) {
546 bit = (addr[i / 8] >> (i % 8)) & 1;
547 addr_for_crc[ETH_ALEN - 1 - i / 8] += bit << (7 - (i % 8));
550 crc = crc32_be(~0, addr_for_crc, netdev->addr_len);
552 hash = (crc >> 27);
553 hash <<= 3;
554 hash |= crc & 7;
555 hash &= 0xff;
557 return hash;
561 * spider_net_set_multi - sets multicast addresses and promisc flags
562 * @netdev: interface device structure
564 * spider_net_set_multi configures multicast addresses as needed for the
565 * netdev interface. It also sets up multicast, allmulti and promisc
566 * flags appropriately
568 static void
569 spider_net_set_multi(struct net_device *netdev)
571 struct dev_mc_list *mc;
572 u8 hash;
573 int i;
574 u32 reg;
575 struct spider_net_card *card = netdev_priv(netdev);
576 unsigned long bitmask[SPIDER_NET_MULTICAST_HASHES / BITS_PER_LONG] =
577 {0, };
579 spider_net_set_promisc(card);
581 if (netdev->flags & IFF_ALLMULTI) {
582 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES; i++) {
583 set_bit(i, bitmask);
585 goto write_hash;
588 /* well, we know, what the broadcast hash value is: it's xfd
589 hash = spider_net_get_multicast_hash(netdev, netdev->broadcast); */
590 set_bit(0xfd, bitmask);
592 for (mc = netdev->mc_list; mc; mc = mc->next) {
593 hash = spider_net_get_multicast_hash(netdev, mc->dmi_addr);
594 set_bit(hash, bitmask);
597 write_hash:
598 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES / 4; i++) {
599 reg = 0;
600 if (test_bit(i * 4, bitmask))
601 reg += 0x08;
602 reg <<= 8;
603 if (test_bit(i * 4 + 1, bitmask))
604 reg += 0x08;
605 reg <<= 8;
606 if (test_bit(i * 4 + 2, bitmask))
607 reg += 0x08;
608 reg <<= 8;
609 if (test_bit(i * 4 + 3, bitmask))
610 reg += 0x08;
612 spider_net_write_reg(card, SPIDER_NET_GMRMHFILnR + i * 4, reg);
617 * spider_net_disable_rxdmac - disables the receive DMA controller
618 * @card: card structure
620 * spider_net_disable_rxdmac terminates processing on the DMA controller by
621 * turing off DMA and issueing a force end
623 static void
624 spider_net_disable_rxdmac(struct spider_net_card *card)
626 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
627 SPIDER_NET_DMA_RX_FEND_VALUE);
631 * spider_net_prepare_tx_descr - fill tx descriptor with skb data
632 * @card: card structure
633 * @descr: descriptor structure to fill out
634 * @skb: packet to use
636 * returns 0 on success, <0 on failure.
638 * fills out the descriptor structure with skb data and len. Copies data,
639 * if needed (32bit DMA!)
641 static int
642 spider_net_prepare_tx_descr(struct spider_net_card *card,
643 struct sk_buff *skb)
645 struct spider_net_descr *descr;
646 dma_addr_t buf;
647 unsigned long flags;
649 buf = pci_map_single(card->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
650 if (pci_dma_mapping_error(buf)) {
651 if (netif_msg_tx_err(card) && net_ratelimit())
652 pr_err("could not iommu-map packet (%p, %i). "
653 "Dropping packet\n", skb->data, skb->len);
654 card->spider_stats.tx_iommu_map_error++;
655 return -ENOMEM;
658 spin_lock_irqsave(&card->tx_chain.lock, flags);
659 descr = card->tx_chain.head;
660 card->tx_chain.head = descr->next;
662 descr->buf_addr = buf;
663 descr->buf_size = skb->len;
664 descr->next_descr_addr = 0;
665 descr->skb = skb;
666 descr->data_status = 0;
668 descr->dmac_cmd_status =
669 SPIDER_NET_DESCR_CARDOWNED | SPIDER_NET_DMAC_NOCS;
670 spin_unlock_irqrestore(&card->tx_chain.lock, flags);
672 if (skb->protocol == htons(ETH_P_IP))
673 switch (skb->nh.iph->protocol) {
674 case IPPROTO_TCP:
675 descr->dmac_cmd_status |= SPIDER_NET_DMAC_TCP;
676 break;
677 case IPPROTO_UDP:
678 descr->dmac_cmd_status |= SPIDER_NET_DMAC_UDP;
679 break;
682 /* Chain the bus address, so that the DMA engine finds this descr. */
683 descr->prev->next_descr_addr = descr->bus_addr;
685 card->netdev->trans_start = jiffies; /* set netdev watchdog timer */
686 return 0;
689 static int
690 spider_net_set_low_watermark(struct spider_net_card *card)
692 unsigned long flags;
693 int status;
694 int cnt=0;
695 int i;
696 struct spider_net_descr *descr = card->tx_chain.tail;
698 /* Measure the length of the queue. Measurement does not
699 * need to be precise -- does not need a lock. */
700 while (descr != card->tx_chain.head) {
701 status = descr->dmac_cmd_status & SPIDER_NET_DESCR_NOT_IN_USE;
702 if (status == SPIDER_NET_DESCR_NOT_IN_USE)
703 break;
704 descr = descr->next;
705 cnt++;
708 /* If TX queue is short, don't even bother with interrupts */
709 if (cnt < card->tx_chain.num_desc/4)
710 return cnt;
712 /* Set low-watermark 3/4th's of the way into the queue. */
713 descr = card->tx_chain.tail;
714 cnt = (cnt*3)/4;
715 for (i=0;i<cnt; i++)
716 descr = descr->next;
718 /* Set the new watermark, clear the old watermark */
719 spin_lock_irqsave(&card->tx_chain.lock, flags);
720 descr->dmac_cmd_status |= SPIDER_NET_DESCR_TXDESFLG;
721 if (card->low_watermark && card->low_watermark != descr)
722 card->low_watermark->dmac_cmd_status =
723 card->low_watermark->dmac_cmd_status & ~SPIDER_NET_DESCR_TXDESFLG;
724 card->low_watermark = descr;
725 spin_unlock_irqrestore(&card->tx_chain.lock, flags);
726 return cnt;
730 * spider_net_release_tx_chain - processes sent tx descriptors
731 * @card: adapter structure
732 * @brutal: if set, don't care about whether descriptor seems to be in use
734 * returns 0 if the tx ring is empty, otherwise 1.
736 * spider_net_release_tx_chain releases the tx descriptors that spider has
737 * finished with (if non-brutal) or simply release tx descriptors (if brutal).
738 * If some other context is calling this function, we return 1 so that we're
739 * scheduled again (if we were scheduled) and will not loose initiative.
741 static int
742 spider_net_release_tx_chain(struct spider_net_card *card, int brutal)
744 struct spider_net_descr_chain *chain = &card->tx_chain;
745 struct spider_net_descr *descr;
746 struct sk_buff *skb;
747 u32 buf_addr;
748 unsigned long flags;
749 int status;
751 while (chain->tail != chain->head) {
752 spin_lock_irqsave(&chain->lock, flags);
753 descr = chain->tail;
755 status = spider_net_get_descr_status(descr);
756 switch (status) {
757 case SPIDER_NET_DESCR_COMPLETE:
758 card->netdev_stats.tx_packets++;
759 card->netdev_stats.tx_bytes += descr->skb->len;
760 break;
762 case SPIDER_NET_DESCR_CARDOWNED:
763 if (!brutal) {
764 spin_unlock_irqrestore(&chain->lock, flags);
765 return 1;
768 /* fallthrough, if we release the descriptors
769 * brutally (then we don't care about
770 * SPIDER_NET_DESCR_CARDOWNED) */
772 case SPIDER_NET_DESCR_RESPONSE_ERROR:
773 case SPIDER_NET_DESCR_PROTECTION_ERROR:
774 case SPIDER_NET_DESCR_FORCE_END:
775 if (netif_msg_tx_err(card))
776 pr_err("%s: forcing end of tx descriptor "
777 "with status x%02x\n",
778 card->netdev->name, status);
779 card->netdev_stats.tx_errors++;
780 break;
782 default:
783 card->netdev_stats.tx_dropped++;
784 if (!brutal) {
785 spin_unlock_irqrestore(&chain->lock, flags);
786 return 1;
790 chain->tail = descr->next;
791 descr->dmac_cmd_status |= SPIDER_NET_DESCR_NOT_IN_USE;
792 skb = descr->skb;
793 buf_addr = descr->buf_addr;
794 spin_unlock_irqrestore(&chain->lock, flags);
796 /* unmap the skb */
797 if (skb) {
798 pci_unmap_single(card->pdev, buf_addr, skb->len,
799 PCI_DMA_TODEVICE);
800 dev_kfree_skb(skb);
803 return 0;
807 * spider_net_kick_tx_dma - enables TX DMA processing
808 * @card: card structure
809 * @descr: descriptor address to enable TX processing at
811 * This routine will start the transmit DMA running if
812 * it is not already running. This routine ned only be
813 * called when queueing a new packet to an empty tx queue.
814 * Writes the current tx chain head as start address
815 * of the tx descriptor chain and enables the transmission
816 * DMA engine.
818 static inline void
819 spider_net_kick_tx_dma(struct spider_net_card *card)
821 struct spider_net_descr *descr;
823 if (spider_net_read_reg(card, SPIDER_NET_GDTDMACCNTR) &
824 SPIDER_NET_TX_DMA_EN)
825 goto out;
827 descr = card->tx_chain.tail;
828 for (;;) {
829 if (spider_net_get_descr_status(descr) ==
830 SPIDER_NET_DESCR_CARDOWNED) {
831 spider_net_write_reg(card, SPIDER_NET_GDTDCHA,
832 descr->bus_addr);
833 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
834 SPIDER_NET_DMA_TX_VALUE);
835 break;
837 if (descr == card->tx_chain.head)
838 break;
839 descr = descr->next;
842 out:
843 mod_timer(&card->tx_timer, jiffies + SPIDER_NET_TX_TIMER);
847 * spider_net_xmit - transmits a frame over the device
848 * @skb: packet to send out
849 * @netdev: interface device structure
851 * returns 0 on success, !0 on failure
853 static int
854 spider_net_xmit(struct sk_buff *skb, struct net_device *netdev)
856 int cnt;
857 struct spider_net_card *card = netdev_priv(netdev);
858 struct spider_net_descr_chain *chain = &card->tx_chain;
860 spider_net_release_tx_chain(card, 0);
862 if ((chain->head->next == chain->tail->prev) ||
863 (spider_net_prepare_tx_descr(card, skb) != 0)) {
865 card->netdev_stats.tx_dropped++;
866 netif_stop_queue(netdev);
867 return NETDEV_TX_BUSY;
870 cnt = spider_net_set_low_watermark(card);
871 if (cnt < 5)
872 spider_net_kick_tx_dma(card);
873 return NETDEV_TX_OK;
877 * spider_net_cleanup_tx_ring - cleans up the TX ring
878 * @card: card structure
880 * spider_net_cleanup_tx_ring is called by either the tx_timer
881 * or from the NAPI polling routine.
882 * This routine releases resources associted with transmitted
883 * packets, including updating the queue tail pointer.
885 static void
886 spider_net_cleanup_tx_ring(struct spider_net_card *card)
888 if ((spider_net_release_tx_chain(card, 0) != 0) &&
889 (card->netdev->flags & IFF_UP)) {
890 spider_net_kick_tx_dma(card);
891 netif_wake_queue(card->netdev);
896 * spider_net_do_ioctl - called for device ioctls
897 * @netdev: interface device structure
898 * @ifr: request parameter structure for ioctl
899 * @cmd: command code for ioctl
901 * returns 0 on success, <0 on failure. Currently, we have no special ioctls.
902 * -EOPNOTSUPP is returned, if an unknown ioctl was requested
904 static int
905 spider_net_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
907 switch (cmd) {
908 default:
909 return -EOPNOTSUPP;
914 * spider_net_pass_skb_up - takes an skb from a descriptor and passes it on
915 * @descr: descriptor to process
916 * @card: card structure
918 * Fills out skb structure and passes the data to the stack.
919 * The descriptor state is not changed.
921 static void
922 spider_net_pass_skb_up(struct spider_net_descr *descr,
923 struct spider_net_card *card)
925 struct sk_buff *skb;
926 struct net_device *netdev;
927 u32 data_status, data_error;
929 data_status = descr->data_status;
930 data_error = descr->data_error;
931 netdev = card->netdev;
933 skb = descr->skb;
934 skb->dev = netdev;
935 skb_put(skb, descr->valid_size);
937 /* the card seems to add 2 bytes of junk in front
938 * of the ethernet frame */
939 #define SPIDER_MISALIGN 2
940 skb_pull(skb, SPIDER_MISALIGN);
941 skb->protocol = eth_type_trans(skb, netdev);
943 /* checksum offload */
944 if (card->options.rx_csum) {
945 if ( ( (data_status & SPIDER_NET_DATA_STATUS_CKSUM_MASK) ==
946 SPIDER_NET_DATA_STATUS_CKSUM_MASK) &&
947 !(data_error & SPIDER_NET_DATA_ERR_CKSUM_MASK))
948 skb->ip_summed = CHECKSUM_UNNECESSARY;
949 else
950 skb->ip_summed = CHECKSUM_NONE;
951 } else
952 skb->ip_summed = CHECKSUM_NONE;
954 if (data_status & SPIDER_NET_VLAN_PACKET) {
955 /* further enhancements: HW-accel VLAN
956 * vlan_hwaccel_receive_skb
960 /* pass skb up to stack */
961 netif_receive_skb(skb);
963 /* update netdevice statistics */
964 card->netdev_stats.rx_packets++;
965 card->netdev_stats.rx_bytes += skb->len;
968 #ifdef DEBUG
969 static void show_rx_chain(struct spider_net_card *card)
971 struct spider_net_descr_chain *chain = &card->rx_chain;
972 struct spider_net_descr *start= chain->tail;
973 struct spider_net_descr *descr= start;
974 int status;
976 int cnt = 0;
977 int cstat = spider_net_get_descr_status(descr);
978 printk(KERN_INFO "RX chain tail at descr=%ld\n",
979 (start - card->descr) - card->tx_chain.num_desc);
980 status = cstat;
983 status = spider_net_get_descr_status(descr);
984 if (cstat != status) {
985 printk(KERN_INFO "Have %d descrs with stat=x%08x\n", cnt, cstat);
986 cstat = status;
987 cnt = 0;
989 cnt ++;
990 descr = descr->next;
991 } while (descr != start);
992 printk(KERN_INFO "Last %d descrs with stat=x%08x\n", cnt, cstat);
994 #endif
997 * spider_net_decode_one_descr - processes an rx descriptor
998 * @card: card structure
1000 * Returns 1 if a packet has been sent to the stack, otherwise 0
1002 * Processes an rx descriptor by iommu-unmapping the data buffer and passing
1003 * the packet up to the stack. This function is called in softirq
1004 * context, e.g. either bottom half from interrupt or NAPI polling context
1006 static int
1007 spider_net_decode_one_descr(struct spider_net_card *card)
1009 struct spider_net_descr_chain *chain = &card->rx_chain;
1010 struct spider_net_descr *descr = chain->tail;
1011 int status;
1013 status = spider_net_get_descr_status(descr);
1015 /* Nothing in the descriptor, or ring must be empty */
1016 if ((status == SPIDER_NET_DESCR_CARDOWNED) ||
1017 (status == SPIDER_NET_DESCR_NOT_IN_USE))
1018 return 0;
1020 /* descriptor definitively used -- move on tail */
1021 chain->tail = descr->next;
1023 /* unmap descriptor */
1024 pci_unmap_single(card->pdev, descr->buf_addr,
1025 SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
1027 if ( (status == SPIDER_NET_DESCR_RESPONSE_ERROR) ||
1028 (status == SPIDER_NET_DESCR_PROTECTION_ERROR) ||
1029 (status == SPIDER_NET_DESCR_FORCE_END) ) {
1030 if (netif_msg_rx_err(card))
1031 pr_err("%s: dropping RX descriptor with state %d\n",
1032 card->netdev->name, status);
1033 card->netdev_stats.rx_dropped++;
1034 goto bad_desc;
1037 if ( (status != SPIDER_NET_DESCR_COMPLETE) &&
1038 (status != SPIDER_NET_DESCR_FRAME_END) ) {
1039 if (netif_msg_rx_err(card))
1040 pr_err("%s: RX descriptor with unkown state %d\n",
1041 card->netdev->name, status);
1042 card->spider_stats.rx_desc_unk_state++;
1043 goto bad_desc;
1046 /* The cases we'll throw away the packet immediately */
1047 if (descr->data_error & SPIDER_NET_DESTROY_RX_FLAGS) {
1048 if (netif_msg_rx_err(card))
1049 pr_err("%s: error in received descriptor found, "
1050 "data_status=x%08x, data_error=x%08x\n",
1051 card->netdev->name,
1052 descr->data_status, descr->data_error);
1053 goto bad_desc;
1056 if (descr->dmac_cmd_status & 0xfefe) {
1057 pr_err("%s: bad status, cmd_status=x%08x\n",
1058 card->netdev->name,
1059 descr->dmac_cmd_status);
1060 pr_err("buf_addr=x%08x\n", descr->buf_addr);
1061 pr_err("buf_size=x%08x\n", descr->buf_size);
1062 pr_err("next_descr_addr=x%08x\n", descr->next_descr_addr);
1063 pr_err("result_size=x%08x\n", descr->result_size);
1064 pr_err("valid_size=x%08x\n", descr->valid_size);
1065 pr_err("data_status=x%08x\n", descr->data_status);
1066 pr_err("data_error=x%08x\n", descr->data_error);
1067 pr_err("bus_addr=x%08x\n", descr->bus_addr);
1068 pr_err("which=%ld\n", descr - card->rx_chain.ring);
1070 card->spider_stats.rx_desc_error++;
1071 goto bad_desc;
1074 /* Ok, we've got a packet in descr */
1075 spider_net_pass_skb_up(descr, card);
1076 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
1077 return 1;
1079 bad_desc:
1080 dev_kfree_skb_irq(descr->skb);
1081 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
1082 return 0;
1086 * spider_net_poll - NAPI poll function called by the stack to return packets
1087 * @netdev: interface device structure
1088 * @budget: number of packets we can pass to the stack at most
1090 * returns 0 if no more packets available to the driver/stack. Returns 1,
1091 * if the quota is exceeded, but the driver has still packets.
1093 * spider_net_poll returns all packets from the rx descriptors to the stack
1094 * (using netif_receive_skb). If all/enough packets are up, the driver
1095 * reenables interrupts and returns 0. If not, 1 is returned.
1097 static int
1098 spider_net_poll(struct net_device *netdev, int *budget)
1100 struct spider_net_card *card = netdev_priv(netdev);
1101 int packets_to_do, packets_done = 0;
1102 int no_more_packets = 0;
1104 spider_net_cleanup_tx_ring(card);
1105 packets_to_do = min(*budget, netdev->quota);
1107 while (packets_to_do) {
1108 if (spider_net_decode_one_descr(card)) {
1109 packets_done++;
1110 packets_to_do--;
1111 } else {
1112 /* no more packets for the stack */
1113 no_more_packets = 1;
1114 break;
1118 netdev->quota -= packets_done;
1119 *budget -= packets_done;
1120 spider_net_refill_rx_chain(card);
1121 spider_net_enable_rxdmac(card);
1123 /* if all packets are in the stack, enable interrupts and return 0 */
1124 /* if not, return 1 */
1125 if (no_more_packets) {
1126 netif_rx_complete(netdev);
1127 spider_net_rx_irq_on(card);
1128 return 0;
1131 return 1;
1135 * spider_net_vlan_rx_reg - initializes VLAN structures in the driver and card
1136 * @netdev: interface device structure
1137 * @grp: vlan_group structure that is registered (NULL on destroying interface)
1139 static void
1140 spider_net_vlan_rx_reg(struct net_device *netdev, struct vlan_group *grp)
1142 /* further enhancement... yet to do */
1143 return;
1147 * spider_net_vlan_rx_add - adds VLAN id to the card filter
1148 * @netdev: interface device structure
1149 * @vid: VLAN id to add
1151 static void
1152 spider_net_vlan_rx_add(struct net_device *netdev, uint16_t vid)
1154 /* further enhancement... yet to do */
1155 /* add vid to card's VLAN filter table */
1156 return;
1160 * spider_net_vlan_rx_kill - removes VLAN id to the card filter
1161 * @netdev: interface device structure
1162 * @vid: VLAN id to remove
1164 static void
1165 spider_net_vlan_rx_kill(struct net_device *netdev, uint16_t vid)
1167 /* further enhancement... yet to do */
1168 /* remove vid from card's VLAN filter table */
1172 * spider_net_get_stats - get interface statistics
1173 * @netdev: interface device structure
1175 * returns the interface statistics residing in the spider_net_card struct
1177 static struct net_device_stats *
1178 spider_net_get_stats(struct net_device *netdev)
1180 struct spider_net_card *card = netdev_priv(netdev);
1181 struct net_device_stats *stats = &card->netdev_stats;
1182 return stats;
1186 * spider_net_change_mtu - changes the MTU of an interface
1187 * @netdev: interface device structure
1188 * @new_mtu: new MTU value
1190 * returns 0 on success, <0 on failure
1192 static int
1193 spider_net_change_mtu(struct net_device *netdev, int new_mtu)
1195 /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
1196 * and mtu is outbound only anyway */
1197 if ( (new_mtu < SPIDER_NET_MIN_MTU ) ||
1198 (new_mtu > SPIDER_NET_MAX_MTU) )
1199 return -EINVAL;
1200 netdev->mtu = new_mtu;
1201 return 0;
1205 * spider_net_set_mac - sets the MAC of an interface
1206 * @netdev: interface device structure
1207 * @ptr: pointer to new MAC address
1209 * Returns 0 on success, <0 on failure. Currently, we don't support this
1210 * and will always return EOPNOTSUPP.
1212 static int
1213 spider_net_set_mac(struct net_device *netdev, void *p)
1215 struct spider_net_card *card = netdev_priv(netdev);
1216 u32 macl, macu, regvalue;
1217 struct sockaddr *addr = p;
1219 if (!is_valid_ether_addr(addr->sa_data))
1220 return -EADDRNOTAVAIL;
1222 /* switch off GMACTPE and GMACRPE */
1223 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1224 regvalue &= ~((1 << 5) | (1 << 6));
1225 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1227 /* write mac */
1228 macu = (addr->sa_data[0]<<24) + (addr->sa_data[1]<<16) +
1229 (addr->sa_data[2]<<8) + (addr->sa_data[3]);
1230 macl = (addr->sa_data[4]<<8) + (addr->sa_data[5]);
1231 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACU, macu);
1232 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACL, macl);
1234 /* switch GMACTPE and GMACRPE back on */
1235 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1236 regvalue |= ((1 << 5) | (1 << 6));
1237 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1239 spider_net_set_promisc(card);
1241 /* look up, whether we have been successful */
1242 if (spider_net_get_mac_address(netdev))
1243 return -EADDRNOTAVAIL;
1244 if (memcmp(netdev->dev_addr,addr->sa_data,netdev->addr_len))
1245 return -EADDRNOTAVAIL;
1247 return 0;
1251 * spider_net_handle_error_irq - handles errors raised by an interrupt
1252 * @card: card structure
1253 * @status_reg: interrupt status register 0 (GHIINT0STS)
1255 * spider_net_handle_error_irq treats or ignores all error conditions
1256 * found when an interrupt is presented
1258 static void
1259 spider_net_handle_error_irq(struct spider_net_card *card, u32 status_reg)
1261 u32 error_reg1, error_reg2;
1262 u32 i;
1263 int show_error = 1;
1265 error_reg1 = spider_net_read_reg(card, SPIDER_NET_GHIINT1STS);
1266 error_reg2 = spider_net_read_reg(card, SPIDER_NET_GHIINT2STS);
1268 /* check GHIINT0STS ************************************/
1269 if (status_reg)
1270 for (i = 0; i < 32; i++)
1271 if (status_reg & (1<<i))
1272 switch (i)
1274 /* let error_reg1 and error_reg2 evaluation decide, what to do
1275 case SPIDER_NET_PHYINT:
1276 case SPIDER_NET_GMAC2INT:
1277 case SPIDER_NET_GMAC1INT:
1278 case SPIDER_NET_GFIFOINT:
1279 case SPIDER_NET_DMACINT:
1280 case SPIDER_NET_GSYSINT:
1281 break; */
1283 case SPIDER_NET_GIPSINT:
1284 show_error = 0;
1285 break;
1287 case SPIDER_NET_GPWOPCMPINT:
1288 /* PHY write operation completed */
1289 show_error = 0;
1290 break;
1291 case SPIDER_NET_GPROPCMPINT:
1292 /* PHY read operation completed */
1293 /* we don't use semaphores, as we poll for the completion
1294 * of the read operation in spider_net_read_phy. Should take
1295 * about 50 us */
1296 show_error = 0;
1297 break;
1298 case SPIDER_NET_GPWFFINT:
1299 /* PHY command queue full */
1300 if (netif_msg_intr(card))
1301 pr_err("PHY write queue full\n");
1302 show_error = 0;
1303 break;
1305 /* case SPIDER_NET_GRMDADRINT: not used. print a message */
1306 /* case SPIDER_NET_GRMARPINT: not used. print a message */
1307 /* case SPIDER_NET_GRMMPINT: not used. print a message */
1309 case SPIDER_NET_GDTDEN0INT:
1310 /* someone has set TX_DMA_EN to 0 */
1311 show_error = 0;
1312 break;
1314 case SPIDER_NET_GDDDEN0INT: /* fallthrough */
1315 case SPIDER_NET_GDCDEN0INT: /* fallthrough */
1316 case SPIDER_NET_GDBDEN0INT: /* fallthrough */
1317 case SPIDER_NET_GDADEN0INT:
1318 /* someone has set RX_DMA_EN to 0 */
1319 show_error = 0;
1320 break;
1322 /* RX interrupts */
1323 case SPIDER_NET_GDDFDCINT:
1324 case SPIDER_NET_GDCFDCINT:
1325 case SPIDER_NET_GDBFDCINT:
1326 case SPIDER_NET_GDAFDCINT:
1327 /* case SPIDER_NET_GDNMINT: not used. print a message */
1328 /* case SPIDER_NET_GCNMINT: not used. print a message */
1329 /* case SPIDER_NET_GBNMINT: not used. print a message */
1330 /* case SPIDER_NET_GANMINT: not used. print a message */
1331 /* case SPIDER_NET_GRFNMINT: not used. print a message */
1332 show_error = 0;
1333 break;
1335 /* TX interrupts */
1336 case SPIDER_NET_GDTFDCINT:
1337 show_error = 0;
1338 break;
1339 case SPIDER_NET_GTTEDINT:
1340 show_error = 0;
1341 break;
1342 case SPIDER_NET_GDTDCEINT:
1343 /* chain end. If a descriptor should be sent, kick off
1344 * tx dma
1345 if (card->tx_chain.tail != card->tx_chain.head)
1346 spider_net_kick_tx_dma(card);
1348 show_error = 0;
1349 break;
1351 /* case SPIDER_NET_G1TMCNTINT: not used. print a message */
1352 /* case SPIDER_NET_GFREECNTINT: not used. print a message */
1355 /* check GHIINT1STS ************************************/
1356 if (error_reg1)
1357 for (i = 0; i < 32; i++)
1358 if (error_reg1 & (1<<i))
1359 switch (i)
1361 case SPIDER_NET_GTMFLLINT:
1362 if (netif_msg_intr(card) && net_ratelimit())
1363 pr_err("Spider TX RAM full\n");
1364 show_error = 0;
1365 break;
1366 case SPIDER_NET_GRFDFLLINT: /* fallthrough */
1367 case SPIDER_NET_GRFCFLLINT: /* fallthrough */
1368 case SPIDER_NET_GRFBFLLINT: /* fallthrough */
1369 case SPIDER_NET_GRFAFLLINT: /* fallthrough */
1370 case SPIDER_NET_GRMFLLINT:
1371 if (netif_msg_intr(card) && net_ratelimit())
1372 pr_err("Spider RX RAM full, incoming packets "
1373 "might be discarded!\n");
1374 spider_net_rx_irq_off(card);
1375 netif_rx_schedule(card->netdev);
1376 show_error = 0;
1377 break;
1379 /* case SPIDER_NET_GTMSHTINT: problem, print a message */
1380 case SPIDER_NET_GDTINVDINT:
1381 /* allrighty. tx from previous descr ok */
1382 show_error = 0;
1383 break;
1385 /* chain end */
1386 case SPIDER_NET_GDDDCEINT: /* fallthrough */
1387 case SPIDER_NET_GDCDCEINT: /* fallthrough */
1388 case SPIDER_NET_GDBDCEINT: /* fallthrough */
1389 case SPIDER_NET_GDADCEINT:
1390 if (netif_msg_intr(card) && net_ratelimit())
1391 pr_err("got descriptor chain end interrupt, "
1392 "restarting DMAC %c.\n",
1393 'D'-(i-SPIDER_NET_GDDDCEINT)/3);
1394 spider_net_refill_rx_chain(card);
1395 spider_net_enable_rxdmac(card);
1396 show_error = 0;
1397 break;
1399 /* invalid descriptor */
1400 case SPIDER_NET_GDDINVDINT: /* fallthrough */
1401 case SPIDER_NET_GDCINVDINT: /* fallthrough */
1402 case SPIDER_NET_GDBINVDINT: /* fallthrough */
1403 case SPIDER_NET_GDAINVDINT:
1404 /* could happen when rx chain is full */
1405 spider_net_refill_rx_chain(card);
1406 spider_net_enable_rxdmac(card);
1407 show_error = 0;
1408 break;
1410 /* case SPIDER_NET_GDTRSERINT: problem, print a message */
1411 /* case SPIDER_NET_GDDRSERINT: problem, print a message */
1412 /* case SPIDER_NET_GDCRSERINT: problem, print a message */
1413 /* case SPIDER_NET_GDBRSERINT: problem, print a message */
1414 /* case SPIDER_NET_GDARSERINT: problem, print a message */
1415 /* case SPIDER_NET_GDSERINT: problem, print a message */
1416 /* case SPIDER_NET_GDTPTERINT: problem, print a message */
1417 /* case SPIDER_NET_GDDPTERINT: problem, print a message */
1418 /* case SPIDER_NET_GDCPTERINT: problem, print a message */
1419 /* case SPIDER_NET_GDBPTERINT: problem, print a message */
1420 /* case SPIDER_NET_GDAPTERINT: problem, print a message */
1421 default:
1422 show_error = 1;
1423 break;
1426 /* check GHIINT2STS ************************************/
1427 if (error_reg2)
1428 for (i = 0; i < 32; i++)
1429 if (error_reg2 & (1<<i))
1430 switch (i)
1432 /* there is nothing we can (want to) do at this time. Log a
1433 * message, we can switch on and off the specific values later on
1434 case SPIDER_NET_GPROPERINT:
1435 case SPIDER_NET_GMCTCRSNGINT:
1436 case SPIDER_NET_GMCTLCOLINT:
1437 case SPIDER_NET_GMCTTMOTINT:
1438 case SPIDER_NET_GMCRCAERINT:
1439 case SPIDER_NET_GMCRCALERINT:
1440 case SPIDER_NET_GMCRALNERINT:
1441 case SPIDER_NET_GMCROVRINT:
1442 case SPIDER_NET_GMCRRNTINT:
1443 case SPIDER_NET_GMCRRXERINT:
1444 case SPIDER_NET_GTITCSERINT:
1445 case SPIDER_NET_GTIFMTERINT:
1446 case SPIDER_NET_GTIPKTRVKINT:
1447 case SPIDER_NET_GTISPINGINT:
1448 case SPIDER_NET_GTISADNGINT:
1449 case SPIDER_NET_GTISPDNGINT:
1450 case SPIDER_NET_GRIFMTERINT:
1451 case SPIDER_NET_GRIPKTRVKINT:
1452 case SPIDER_NET_GRISPINGINT:
1453 case SPIDER_NET_GRISADNGINT:
1454 case SPIDER_NET_GRISPDNGINT:
1455 break;
1457 default:
1458 break;
1461 if ((show_error) && (netif_msg_intr(card)) && net_ratelimit())
1462 pr_err("Got error interrupt on %s, GHIINT0STS = 0x%08x, "
1463 "GHIINT1STS = 0x%08x, GHIINT2STS = 0x%08x\n",
1464 card->netdev->name,
1465 status_reg, error_reg1, error_reg2);
1467 /* clear interrupt sources */
1468 spider_net_write_reg(card, SPIDER_NET_GHIINT1STS, error_reg1);
1469 spider_net_write_reg(card, SPIDER_NET_GHIINT2STS, error_reg2);
1473 * spider_net_interrupt - interrupt handler for spider_net
1474 * @irq: interupt number
1475 * @ptr: pointer to net_device
1476 * @regs: PU registers
1478 * returns IRQ_HANDLED, if interrupt was for driver, or IRQ_NONE, if no
1479 * interrupt found raised by card.
1481 * This is the interrupt handler, that turns off
1482 * interrupts for this device and makes the stack poll the driver
1484 static irqreturn_t
1485 spider_net_interrupt(int irq, void *ptr)
1487 struct net_device *netdev = ptr;
1488 struct spider_net_card *card = netdev_priv(netdev);
1489 u32 status_reg;
1491 status_reg = spider_net_read_reg(card, SPIDER_NET_GHIINT0STS);
1493 if (!status_reg)
1494 return IRQ_NONE;
1496 if (status_reg & SPIDER_NET_RXINT ) {
1497 spider_net_rx_irq_off(card);
1498 netif_rx_schedule(netdev);
1500 if (status_reg & SPIDER_NET_TXINT)
1501 netif_rx_schedule(netdev);
1503 if (status_reg & SPIDER_NET_ERRINT )
1504 spider_net_handle_error_irq(card, status_reg);
1506 /* clear interrupt sources */
1507 spider_net_write_reg(card, SPIDER_NET_GHIINT0STS, status_reg);
1509 return IRQ_HANDLED;
1512 #ifdef CONFIG_NET_POLL_CONTROLLER
1514 * spider_net_poll_controller - artificial interrupt for netconsole etc.
1515 * @netdev: interface device structure
1517 * see Documentation/networking/netconsole.txt
1519 static void
1520 spider_net_poll_controller(struct net_device *netdev)
1522 disable_irq(netdev->irq);
1523 spider_net_interrupt(netdev->irq, netdev);
1524 enable_irq(netdev->irq);
1526 #endif /* CONFIG_NET_POLL_CONTROLLER */
1529 * spider_net_init_card - initializes the card
1530 * @card: card structure
1532 * spider_net_init_card initializes the card so that other registers can
1533 * be used
1535 static void
1536 spider_net_init_card(struct spider_net_card *card)
1538 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1539 SPIDER_NET_CKRCTRL_STOP_VALUE);
1541 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1542 SPIDER_NET_CKRCTRL_RUN_VALUE);
1546 * spider_net_enable_card - enables the card by setting all kinds of regs
1547 * @card: card structure
1549 * spider_net_enable_card sets a lot of SMMIO registers to enable the device
1551 static void
1552 spider_net_enable_card(struct spider_net_card *card)
1554 int i;
1555 /* the following array consists of (register),(value) pairs
1556 * that are set in this function. A register of 0 ends the list */
1557 u32 regs[][2] = {
1558 { SPIDER_NET_GRESUMINTNUM, 0 },
1559 { SPIDER_NET_GREINTNUM, 0 },
1561 /* set interrupt frame number registers */
1562 /* clear the single DMA engine registers first */
1563 { SPIDER_NET_GFAFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1564 { SPIDER_NET_GFBFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1565 { SPIDER_NET_GFCFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1566 { SPIDER_NET_GFDFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1567 /* then set, what we really need */
1568 { SPIDER_NET_GFFRMNUM, SPIDER_NET_FRAMENUM_VALUE },
1570 /* timer counter registers and stuff */
1571 { SPIDER_NET_GFREECNNUM, 0 },
1572 { SPIDER_NET_GONETIMENUM, 0 },
1573 { SPIDER_NET_GTOUTFRMNUM, 0 },
1575 /* RX mode setting */
1576 { SPIDER_NET_GRXMDSET, SPIDER_NET_RXMODE_VALUE },
1577 /* TX mode setting */
1578 { SPIDER_NET_GTXMDSET, SPIDER_NET_TXMODE_VALUE },
1579 /* IPSEC mode setting */
1580 { SPIDER_NET_GIPSECINIT, SPIDER_NET_IPSECINIT_VALUE },
1582 { SPIDER_NET_GFTRESTRT, SPIDER_NET_RESTART_VALUE },
1584 { SPIDER_NET_GMRWOLCTRL, 0 },
1585 { SPIDER_NET_GTESTMD, 0x10000000 },
1586 { SPIDER_NET_GTTQMSK, 0x00400040 },
1588 { SPIDER_NET_GMACINTEN, 0 },
1590 /* flow control stuff */
1591 { SPIDER_NET_GMACAPAUSE, SPIDER_NET_MACAPAUSE_VALUE },
1592 { SPIDER_NET_GMACTXPAUSE, SPIDER_NET_TXPAUSE_VALUE },
1594 { SPIDER_NET_GMACBSTLMT, SPIDER_NET_BURSTLMT_VALUE },
1595 { 0, 0}
1598 i = 0;
1599 while (regs[i][0]) {
1600 spider_net_write_reg(card, regs[i][0], regs[i][1]);
1601 i++;
1604 /* clear unicast filter table entries 1 to 14 */
1605 for (i = 1; i <= 14; i++) {
1606 spider_net_write_reg(card,
1607 SPIDER_NET_GMRUAFILnR + i * 8,
1608 0x00080000);
1609 spider_net_write_reg(card,
1610 SPIDER_NET_GMRUAFILnR + i * 8 + 4,
1611 0x00000000);
1614 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R, 0x08080000);
1616 spider_net_write_reg(card, SPIDER_NET_ECMODE, SPIDER_NET_ECMODE_VALUE);
1618 /* set chain tail adress for RX chains and
1619 * enable DMA */
1620 spider_net_enable_rxchtails(card);
1621 spider_net_enable_rxdmac(card);
1623 spider_net_write_reg(card, SPIDER_NET_GRXDMAEN, SPIDER_NET_WOL_VALUE);
1625 spider_net_write_reg(card, SPIDER_NET_GMACLENLMT,
1626 SPIDER_NET_LENLMT_VALUE);
1627 spider_net_write_reg(card, SPIDER_NET_GMACMODE,
1628 SPIDER_NET_MACMODE_VALUE);
1629 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD,
1630 SPIDER_NET_OPMODE_VALUE);
1632 /* set interrupt mask registers */
1633 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK,
1634 SPIDER_NET_INT0_MASK_VALUE);
1635 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK,
1636 SPIDER_NET_INT1_MASK_VALUE);
1637 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK,
1638 SPIDER_NET_INT2_MASK_VALUE);
1640 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
1641 SPIDER_NET_GDTBSTA);
1645 * spider_net_open - called upon ifonfig up
1646 * @netdev: interface device structure
1648 * returns 0 on success, <0 on failure
1650 * spider_net_open allocates all the descriptors and memory needed for
1651 * operation, sets up multicast list and enables interrupts
1654 spider_net_open(struct net_device *netdev)
1656 struct spider_net_card *card = netdev_priv(netdev);
1657 int result;
1659 result = spider_net_init_chain(card, &card->tx_chain);
1660 if (result)
1661 goto alloc_tx_failed;
1662 card->low_watermark = NULL;
1664 result = spider_net_init_chain(card, &card->rx_chain);
1665 if (result)
1666 goto alloc_rx_failed;
1668 /* Allocate rx skbs */
1669 if (spider_net_alloc_rx_skbs(card))
1670 goto alloc_skbs_failed;
1672 spider_net_set_multi(netdev);
1674 /* further enhancement: setup hw vlan, if needed */
1676 result = -EBUSY;
1677 if (request_irq(netdev->irq, spider_net_interrupt,
1678 IRQF_SHARED, netdev->name, netdev))
1679 goto register_int_failed;
1681 spider_net_enable_card(card);
1683 netif_start_queue(netdev);
1684 netif_carrier_on(netdev);
1685 netif_poll_enable(netdev);
1687 return 0;
1689 register_int_failed:
1690 spider_net_free_rx_chain_contents(card);
1691 alloc_skbs_failed:
1692 spider_net_free_chain(card, &card->rx_chain);
1693 alloc_rx_failed:
1694 spider_net_free_chain(card, &card->tx_chain);
1695 alloc_tx_failed:
1696 return result;
1700 * spider_net_setup_phy - setup PHY
1701 * @card: card structure
1703 * returns 0 on success, <0 on failure
1705 * spider_net_setup_phy is used as part of spider_net_probe. Sets
1706 * the PHY to 1000 Mbps
1708 static int
1709 spider_net_setup_phy(struct spider_net_card *card)
1711 struct mii_phy *phy = &card->phy;
1713 spider_net_write_reg(card, SPIDER_NET_GDTDMASEL,
1714 SPIDER_NET_DMASEL_VALUE);
1715 spider_net_write_reg(card, SPIDER_NET_GPCCTRL,
1716 SPIDER_NET_PHY_CTRL_VALUE);
1717 phy->mii_id = 1;
1718 phy->dev = card->netdev;
1719 phy->mdio_read = spider_net_read_phy;
1720 phy->mdio_write = spider_net_write_phy;
1722 mii_phy_probe(phy, phy->mii_id);
1724 if (phy->def->ops->setup_forced)
1725 phy->def->ops->setup_forced(phy, SPEED_1000, DUPLEX_FULL);
1727 phy->def->ops->enable_fiber(phy);
1729 phy->def->ops->read_link(phy);
1730 pr_info("Found %s with %i Mbps, %s-duplex.\n", phy->def->name,
1731 phy->speed, phy->duplex==1 ? "Full" : "Half");
1733 return 0;
1737 * spider_net_download_firmware - loads firmware into the adapter
1738 * @card: card structure
1739 * @firmware_ptr: pointer to firmware data
1741 * spider_net_download_firmware loads the firmware data into the
1742 * adapter. It assumes the length etc. to be allright.
1744 static int
1745 spider_net_download_firmware(struct spider_net_card *card,
1746 const void *firmware_ptr)
1748 int sequencer, i;
1749 const u32 *fw_ptr = firmware_ptr;
1751 /* stop sequencers */
1752 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1753 SPIDER_NET_STOP_SEQ_VALUE);
1755 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1756 sequencer++) {
1757 spider_net_write_reg(card,
1758 SPIDER_NET_GSnPRGADR + sequencer * 8, 0);
1759 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
1760 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1761 sequencer * 8, *fw_ptr);
1762 fw_ptr++;
1766 if (spider_net_read_reg(card, SPIDER_NET_GSINIT))
1767 return -EIO;
1769 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1770 SPIDER_NET_RUN_SEQ_VALUE);
1772 return 0;
1776 * spider_net_init_firmware - reads in firmware parts
1777 * @card: card structure
1779 * Returns 0 on success, <0 on failure
1781 * spider_net_init_firmware opens the sequencer firmware and does some basic
1782 * checks. This function opens and releases the firmware structure. A call
1783 * to download the firmware is performed before the release.
1785 * Firmware format
1786 * ===============
1787 * spider_fw.bin is expected to be a file containing 6*1024*4 bytes, 4k being
1788 * the program for each sequencer. Use the command
1789 * tail -q -n +2 Seq_code1_0x088.txt Seq_code2_0x090.txt \
1790 * Seq_code3_0x098.txt Seq_code4_0x0A0.txt Seq_code5_0x0A8.txt \
1791 * Seq_code6_0x0B0.txt | xxd -r -p -c4 > spider_fw.bin
1793 * to generate spider_fw.bin, if you have sequencer programs with something
1794 * like the following contents for each sequencer:
1795 * <ONE LINE COMMENT>
1796 * <FIRST 4-BYTES-WORD FOR SEQUENCER>
1797 * <SECOND 4-BYTES-WORD FOR SEQUENCER>
1798 * ...
1799 * <1024th 4-BYTES-WORD FOR SEQUENCER>
1801 static int
1802 spider_net_init_firmware(struct spider_net_card *card)
1804 struct firmware *firmware = NULL;
1805 struct device_node *dn;
1806 const u8 *fw_prop = NULL;
1807 int err = -ENOENT;
1808 int fw_size;
1810 if (request_firmware((const struct firmware **)&firmware,
1811 SPIDER_NET_FIRMWARE_NAME, &card->pdev->dev) == 0) {
1812 if ( (firmware->size != SPIDER_NET_FIRMWARE_LEN) &&
1813 netif_msg_probe(card) ) {
1814 pr_err("Incorrect size of spidernet firmware in " \
1815 "filesystem. Looking in host firmware...\n");
1816 goto try_host_fw;
1818 err = spider_net_download_firmware(card, firmware->data);
1820 release_firmware(firmware);
1821 if (err)
1822 goto try_host_fw;
1824 goto done;
1827 try_host_fw:
1828 dn = pci_device_to_OF_node(card->pdev);
1829 if (!dn)
1830 goto out_err;
1832 fw_prop = get_property(dn, "firmware", &fw_size);
1833 if (!fw_prop)
1834 goto out_err;
1836 if ( (fw_size != SPIDER_NET_FIRMWARE_LEN) &&
1837 netif_msg_probe(card) ) {
1838 pr_err("Incorrect size of spidernet firmware in " \
1839 "host firmware\n");
1840 goto done;
1843 err = spider_net_download_firmware(card, fw_prop);
1845 done:
1846 return err;
1847 out_err:
1848 if (netif_msg_probe(card))
1849 pr_err("Couldn't find spidernet firmware in filesystem " \
1850 "or host firmware\n");
1851 return err;
1855 * spider_net_workaround_rxramfull - work around firmware bug
1856 * @card: card structure
1858 * no return value
1860 static void
1861 spider_net_workaround_rxramfull(struct spider_net_card *card)
1863 int i, sequencer = 0;
1865 /* cancel reset */
1866 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1867 SPIDER_NET_CKRCTRL_RUN_VALUE);
1869 /* empty sequencer data */
1870 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1871 sequencer++) {
1872 spider_net_write_reg(card, SPIDER_NET_GSnPRGADR +
1873 sequencer * 8, 0x0);
1874 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
1875 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1876 sequencer * 8, 0x0);
1880 /* set sequencer operation */
1881 spider_net_write_reg(card, SPIDER_NET_GSINIT, 0x000000fe);
1883 /* reset */
1884 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1885 SPIDER_NET_CKRCTRL_STOP_VALUE);
1889 * spider_net_stop - called upon ifconfig down
1890 * @netdev: interface device structure
1892 * always returns 0
1895 spider_net_stop(struct net_device *netdev)
1897 struct spider_net_card *card = netdev_priv(netdev);
1899 netif_poll_disable(netdev);
1900 netif_carrier_off(netdev);
1901 netif_stop_queue(netdev);
1902 del_timer_sync(&card->tx_timer);
1904 /* disable/mask all interrupts */
1905 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, 0);
1906 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK, 0);
1907 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK, 0);
1909 /* free_irq(netdev->irq, netdev);*/
1910 free_irq(to_pci_dev(netdev->class_dev.dev)->irq, netdev);
1912 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
1913 SPIDER_NET_DMA_TX_FEND_VALUE);
1915 /* turn off DMA, force end */
1916 spider_net_disable_rxdmac(card);
1918 /* release chains */
1919 spider_net_release_tx_chain(card, 1);
1920 spider_net_free_rx_chain_contents(card);
1922 spider_net_free_rx_chain_contents(card);
1924 spider_net_free_chain(card, &card->tx_chain);
1925 spider_net_free_chain(card, &card->rx_chain);
1927 return 0;
1931 * spider_net_tx_timeout_task - task scheduled by the watchdog timeout
1932 * function (to be called not under interrupt status)
1933 * @data: data, is interface device structure
1935 * called as task when tx hangs, resets interface (if interface is up)
1937 static void
1938 spider_net_tx_timeout_task(struct work_struct *work)
1940 struct spider_net_card *card =
1941 container_of(work, struct spider_net_card, tx_timeout_task);
1942 struct net_device *netdev = card->netdev;
1944 if (!(netdev->flags & IFF_UP))
1945 goto out;
1947 netif_device_detach(netdev);
1948 spider_net_stop(netdev);
1950 spider_net_workaround_rxramfull(card);
1951 spider_net_init_card(card);
1953 if (spider_net_setup_phy(card))
1954 goto out;
1955 if (spider_net_init_firmware(card))
1956 goto out;
1958 spider_net_open(netdev);
1959 spider_net_kick_tx_dma(card);
1960 netif_device_attach(netdev);
1962 out:
1963 atomic_dec(&card->tx_timeout_task_counter);
1967 * spider_net_tx_timeout - called when the tx timeout watchdog kicks in.
1968 * @netdev: interface device structure
1970 * called, if tx hangs. Schedules a task that resets the interface
1972 static void
1973 spider_net_tx_timeout(struct net_device *netdev)
1975 struct spider_net_card *card;
1977 card = netdev_priv(netdev);
1978 atomic_inc(&card->tx_timeout_task_counter);
1979 if (netdev->flags & IFF_UP)
1980 schedule_work(&card->tx_timeout_task);
1981 else
1982 atomic_dec(&card->tx_timeout_task_counter);
1983 card->spider_stats.tx_timeouts++;
1987 * spider_net_setup_netdev_ops - initialization of net_device operations
1988 * @netdev: net_device structure
1990 * fills out function pointers in the net_device structure
1992 static void
1993 spider_net_setup_netdev_ops(struct net_device *netdev)
1995 netdev->open = &spider_net_open;
1996 netdev->stop = &spider_net_stop;
1997 netdev->hard_start_xmit = &spider_net_xmit;
1998 netdev->get_stats = &spider_net_get_stats;
1999 netdev->set_multicast_list = &spider_net_set_multi;
2000 netdev->set_mac_address = &spider_net_set_mac;
2001 netdev->change_mtu = &spider_net_change_mtu;
2002 netdev->do_ioctl = &spider_net_do_ioctl;
2003 /* tx watchdog */
2004 netdev->tx_timeout = &spider_net_tx_timeout;
2005 netdev->watchdog_timeo = SPIDER_NET_WATCHDOG_TIMEOUT;
2006 /* NAPI */
2007 netdev->poll = &spider_net_poll;
2008 netdev->weight = SPIDER_NET_NAPI_WEIGHT;
2009 /* HW VLAN */
2010 netdev->vlan_rx_register = &spider_net_vlan_rx_reg;
2011 netdev->vlan_rx_add_vid = &spider_net_vlan_rx_add;
2012 netdev->vlan_rx_kill_vid = &spider_net_vlan_rx_kill;
2013 #ifdef CONFIG_NET_POLL_CONTROLLER
2014 /* poll controller */
2015 netdev->poll_controller = &spider_net_poll_controller;
2016 #endif /* CONFIG_NET_POLL_CONTROLLER */
2017 /* ethtool ops */
2018 netdev->ethtool_ops = &spider_net_ethtool_ops;
2022 * spider_net_setup_netdev - initialization of net_device
2023 * @card: card structure
2025 * Returns 0 on success or <0 on failure
2027 * spider_net_setup_netdev initializes the net_device structure
2029 static int
2030 spider_net_setup_netdev(struct spider_net_card *card)
2032 int result;
2033 struct net_device *netdev = card->netdev;
2034 struct device_node *dn;
2035 struct sockaddr addr;
2036 const u8 *mac;
2038 SET_MODULE_OWNER(netdev);
2039 SET_NETDEV_DEV(netdev, &card->pdev->dev);
2041 pci_set_drvdata(card->pdev, netdev);
2043 init_timer(&card->tx_timer);
2044 card->tx_timer.function =
2045 (void (*)(unsigned long)) spider_net_cleanup_tx_ring;
2046 card->tx_timer.data = (unsigned long) card;
2047 netdev->irq = card->pdev->irq;
2049 card->options.rx_csum = SPIDER_NET_RX_CSUM_DEFAULT;
2051 card->tx_chain.num_desc = tx_descriptors;
2052 card->rx_chain.num_desc = rx_descriptors;
2054 spider_net_setup_netdev_ops(netdev);
2056 netdev->features = NETIF_F_HW_CSUM | NETIF_F_LLTX;
2057 /* some time: NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
2058 * NETIF_F_HW_VLAN_FILTER */
2060 netdev->irq = card->pdev->irq;
2062 dn = pci_device_to_OF_node(card->pdev);
2063 if (!dn)
2064 return -EIO;
2066 mac = get_property(dn, "local-mac-address", NULL);
2067 if (!mac)
2068 return -EIO;
2069 memcpy(addr.sa_data, mac, ETH_ALEN);
2071 result = spider_net_set_mac(netdev, &addr);
2072 if ((result) && (netif_msg_probe(card)))
2073 pr_err("Failed to set MAC address: %i\n", result);
2075 result = register_netdev(netdev);
2076 if (result) {
2077 if (netif_msg_probe(card))
2078 pr_err("Couldn't register net_device: %i\n",
2079 result);
2080 return result;
2083 if (netif_msg_probe(card))
2084 pr_info("Initialized device %s.\n", netdev->name);
2086 return 0;
2090 * spider_net_alloc_card - allocates net_device and card structure
2092 * returns the card structure or NULL in case of errors
2094 * the card and net_device structures are linked to each other
2096 static struct spider_net_card *
2097 spider_net_alloc_card(void)
2099 struct net_device *netdev;
2100 struct spider_net_card *card;
2102 netdev = alloc_etherdev(sizeof(struct spider_net_card));
2103 if (!netdev)
2104 return NULL;
2106 card = netdev_priv(netdev);
2107 card->netdev = netdev;
2108 card->msg_enable = SPIDER_NET_DEFAULT_MSG;
2109 INIT_WORK(&card->tx_timeout_task, spider_net_tx_timeout_task);
2110 init_waitqueue_head(&card->waitq);
2111 atomic_set(&card->tx_timeout_task_counter, 0);
2113 return card;
2117 * spider_net_undo_pci_setup - releases PCI ressources
2118 * @card: card structure
2120 * spider_net_undo_pci_setup releases the mapped regions
2122 static void
2123 spider_net_undo_pci_setup(struct spider_net_card *card)
2125 iounmap(card->regs);
2126 pci_release_regions(card->pdev);
2130 * spider_net_setup_pci_dev - sets up the device in terms of PCI operations
2131 * @card: card structure
2132 * @pdev: PCI device
2134 * Returns the card structure or NULL if any errors occur
2136 * spider_net_setup_pci_dev initializes pdev and together with the
2137 * functions called in spider_net_open configures the device so that
2138 * data can be transferred over it
2139 * The net_device structure is attached to the card structure, if the
2140 * function returns without error.
2142 static struct spider_net_card *
2143 spider_net_setup_pci_dev(struct pci_dev *pdev)
2145 struct spider_net_card *card;
2146 unsigned long mmio_start, mmio_len;
2148 if (pci_enable_device(pdev)) {
2149 pr_err("Couldn't enable PCI device\n");
2150 return NULL;
2153 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
2154 pr_err("Couldn't find proper PCI device base address.\n");
2155 goto out_disable_dev;
2158 if (pci_request_regions(pdev, spider_net_driver_name)) {
2159 pr_err("Couldn't obtain PCI resources, aborting.\n");
2160 goto out_disable_dev;
2163 pci_set_master(pdev);
2165 card = spider_net_alloc_card();
2166 if (!card) {
2167 pr_err("Couldn't allocate net_device structure, "
2168 "aborting.\n");
2169 goto out_release_regions;
2171 card->pdev = pdev;
2173 /* fetch base address and length of first resource */
2174 mmio_start = pci_resource_start(pdev, 0);
2175 mmio_len = pci_resource_len(pdev, 0);
2177 card->netdev->mem_start = mmio_start;
2178 card->netdev->mem_end = mmio_start + mmio_len;
2179 card->regs = ioremap(mmio_start, mmio_len);
2181 if (!card->regs) {
2182 pr_err("Couldn't obtain PCI resources, aborting.\n");
2183 goto out_release_regions;
2186 return card;
2188 out_release_regions:
2189 pci_release_regions(pdev);
2190 out_disable_dev:
2191 pci_disable_device(pdev);
2192 pci_set_drvdata(pdev, NULL);
2193 return NULL;
2197 * spider_net_probe - initialization of a device
2198 * @pdev: PCI device
2199 * @ent: entry in the device id list
2201 * Returns 0 on success, <0 on failure
2203 * spider_net_probe initializes pdev and registers a net_device
2204 * structure for it. After that, the device can be ifconfig'ed up
2206 static int __devinit
2207 spider_net_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2209 int err = -EIO;
2210 struct spider_net_card *card;
2212 card = spider_net_setup_pci_dev(pdev);
2213 if (!card)
2214 goto out;
2216 spider_net_workaround_rxramfull(card);
2217 spider_net_init_card(card);
2219 err = spider_net_setup_phy(card);
2220 if (err)
2221 goto out_undo_pci;
2223 err = spider_net_init_firmware(card);
2224 if (err)
2225 goto out_undo_pci;
2227 err = spider_net_setup_netdev(card);
2228 if (err)
2229 goto out_undo_pci;
2231 return 0;
2233 out_undo_pci:
2234 spider_net_undo_pci_setup(card);
2235 free_netdev(card->netdev);
2236 out:
2237 return err;
2241 * spider_net_remove - removal of a device
2242 * @pdev: PCI device
2244 * Returns 0 on success, <0 on failure
2246 * spider_net_remove is called to remove the device and unregisters the
2247 * net_device
2249 static void __devexit
2250 spider_net_remove(struct pci_dev *pdev)
2252 struct net_device *netdev;
2253 struct spider_net_card *card;
2255 netdev = pci_get_drvdata(pdev);
2256 card = netdev_priv(netdev);
2258 wait_event(card->waitq,
2259 atomic_read(&card->tx_timeout_task_counter) == 0);
2261 unregister_netdev(netdev);
2263 /* switch off card */
2264 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2265 SPIDER_NET_CKRCTRL_STOP_VALUE);
2266 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2267 SPIDER_NET_CKRCTRL_RUN_VALUE);
2269 spider_net_undo_pci_setup(card);
2270 free_netdev(netdev);
2273 static struct pci_driver spider_net_driver = {
2274 .name = spider_net_driver_name,
2275 .id_table = spider_net_pci_tbl,
2276 .probe = spider_net_probe,
2277 .remove = __devexit_p(spider_net_remove)
2281 * spider_net_init - init function when the driver is loaded
2283 * spider_net_init registers the device driver
2285 static int __init spider_net_init(void)
2287 printk(KERN_INFO "Spidernet version %s.\n", VERSION);
2289 if (rx_descriptors < SPIDER_NET_RX_DESCRIPTORS_MIN) {
2290 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MIN;
2291 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2293 if (rx_descriptors > SPIDER_NET_RX_DESCRIPTORS_MAX) {
2294 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MAX;
2295 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2297 if (tx_descriptors < SPIDER_NET_TX_DESCRIPTORS_MIN) {
2298 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MIN;
2299 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2301 if (tx_descriptors > SPIDER_NET_TX_DESCRIPTORS_MAX) {
2302 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MAX;
2303 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2306 return pci_register_driver(&spider_net_driver);
2310 * spider_net_cleanup - exit function when driver is unloaded
2312 * spider_net_cleanup unregisters the device driver
2314 static void __exit spider_net_cleanup(void)
2316 pci_unregister_driver(&spider_net_driver);
2319 module_init(spider_net_init);
2320 module_exit(spider_net_cleanup);