sync hh.org
[hh.org.git] / drivers / net / spider_net.c
blobcef7e6671c49b658d9ce618eafad16213b7b09f9
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 u32 value;
93 value = readl(card->regs + reg);
94 value = le32_to_cpu(value);
96 return value;
99 /**
100 * spider_net_write_reg - writes to an SMMIO register of a card
101 * @card: device structure
102 * @reg: register to write to
103 * @value: value to write into the specified SMMIO register
105 static inline void
106 spider_net_write_reg(struct spider_net_card *card, u32 reg, u32 value)
108 value = cpu_to_le32(value);
109 writel(value, card->regs + reg);
112 /** spider_net_write_phy - write to phy register
113 * @netdev: adapter to be written to
114 * @mii_id: id of MII
115 * @reg: PHY register
116 * @val: value to be written to phy register
118 * spider_net_write_phy_register writes to an arbitrary PHY
119 * register via the spider GPCWOPCMD register. We assume the queue does
120 * not run full (not more than 15 commands outstanding).
122 static void
123 spider_net_write_phy(struct net_device *netdev, int mii_id,
124 int reg, int val)
126 struct spider_net_card *card = netdev_priv(netdev);
127 u32 writevalue;
129 writevalue = ((u32)mii_id << 21) |
130 ((u32)reg << 16) | ((u32)val);
132 spider_net_write_reg(card, SPIDER_NET_GPCWOPCMD, writevalue);
135 /** spider_net_read_phy - read from phy register
136 * @netdev: network device to be read from
137 * @mii_id: id of MII
138 * @reg: PHY register
140 * Returns value read from PHY register
142 * spider_net_write_phy reads from an arbitrary PHY
143 * register via the spider GPCROPCMD register
145 static int
146 spider_net_read_phy(struct net_device *netdev, int mii_id, int reg)
148 struct spider_net_card *card = netdev_priv(netdev);
149 u32 readvalue;
151 readvalue = ((u32)mii_id << 21) | ((u32)reg << 16);
152 spider_net_write_reg(card, SPIDER_NET_GPCROPCMD, readvalue);
154 /* we don't use semaphores to wait for an SPIDER_NET_GPROPCMPINT
155 * interrupt, as we poll for the completion of the read operation
156 * in spider_net_read_phy. Should take about 50 us */
157 do {
158 readvalue = spider_net_read_reg(card, SPIDER_NET_GPCROPCMD);
159 } while (readvalue & SPIDER_NET_GPREXEC);
161 readvalue &= SPIDER_NET_GPRDAT_MASK;
163 return readvalue;
167 * spider_net_rx_irq_off - switch off rx irq on this spider card
168 * @card: device structure
170 * switches off rx irq by masking them out in the GHIINTnMSK register
172 static void
173 spider_net_rx_irq_off(struct spider_net_card *card)
175 u32 regvalue;
177 regvalue = SPIDER_NET_INT0_MASK_VALUE & (~SPIDER_NET_RXINT);
178 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, regvalue);
182 * spider_net_rx_irq_on - switch on rx irq on this spider card
183 * @card: device structure
185 * switches on rx irq by enabling them in the GHIINTnMSK register
187 static void
188 spider_net_rx_irq_on(struct spider_net_card *card)
190 u32 regvalue;
192 regvalue = SPIDER_NET_INT0_MASK_VALUE | SPIDER_NET_RXINT;
193 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, regvalue);
197 * spider_net_set_promisc - sets the unicast address or the promiscuous mode
198 * @card: card structure
200 * spider_net_set_promisc sets the unicast destination address filter and
201 * thus either allows for non-promisc mode or promisc mode
203 static void
204 spider_net_set_promisc(struct spider_net_card *card)
206 u32 macu, macl;
207 struct net_device *netdev = card->netdev;
209 if (netdev->flags & IFF_PROMISC) {
210 /* clear destination entry 0 */
211 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR, 0);
212 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR + 0x04, 0);
213 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R,
214 SPIDER_NET_PROMISC_VALUE);
215 } else {
216 macu = netdev->dev_addr[0];
217 macu <<= 8;
218 macu |= netdev->dev_addr[1];
219 memcpy(&macl, &netdev->dev_addr[2], sizeof(macl));
221 macu |= SPIDER_NET_UA_DESCR_VALUE;
222 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR, macu);
223 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR + 0x04, macl);
224 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R,
225 SPIDER_NET_NONPROMISC_VALUE);
230 * spider_net_get_mac_address - read mac address from spider card
231 * @card: device structure
233 * reads MAC address from GMACUNIMACU and GMACUNIMACL registers
235 static int
236 spider_net_get_mac_address(struct net_device *netdev)
238 struct spider_net_card *card = netdev_priv(netdev);
239 u32 macl, macu;
241 macl = spider_net_read_reg(card, SPIDER_NET_GMACUNIMACL);
242 macu = spider_net_read_reg(card, SPIDER_NET_GMACUNIMACU);
244 netdev->dev_addr[0] = (macu >> 24) & 0xff;
245 netdev->dev_addr[1] = (macu >> 16) & 0xff;
246 netdev->dev_addr[2] = (macu >> 8) & 0xff;
247 netdev->dev_addr[3] = macu & 0xff;
248 netdev->dev_addr[4] = (macl >> 8) & 0xff;
249 netdev->dev_addr[5] = macl & 0xff;
251 if (!is_valid_ether_addr(&netdev->dev_addr[0]))
252 return -EINVAL;
254 return 0;
258 * spider_net_get_descr_status -- returns the status of a descriptor
259 * @descr: descriptor to look at
261 * returns the status as in the dmac_cmd_status field of the descriptor
263 static inline int
264 spider_net_get_descr_status(struct spider_net_descr *descr)
266 return descr->dmac_cmd_status & SPIDER_NET_DESCR_IND_PROC_MASK;
270 * spider_net_free_chain - free descriptor chain
271 * @card: card structure
272 * @chain: address of chain
275 static void
276 spider_net_free_chain(struct spider_net_card *card,
277 struct spider_net_descr_chain *chain)
279 struct spider_net_descr *descr;
281 for (descr = chain->tail; !descr->bus_addr; descr = descr->next) {
282 pci_unmap_single(card->pdev, descr->bus_addr,
283 SPIDER_NET_DESCR_SIZE, PCI_DMA_BIDIRECTIONAL);
284 descr->bus_addr = 0;
289 * spider_net_init_chain - links descriptor chain
290 * @card: card structure
291 * @chain: address of chain
292 * @start_descr: address of descriptor array
293 * @no: number of descriptors
295 * we manage a circular list that mirrors the hardware structure,
296 * except that the hardware uses bus addresses.
298 * returns 0 on success, <0 on failure
300 static int
301 spider_net_init_chain(struct spider_net_card *card,
302 struct spider_net_descr_chain *chain,
303 struct spider_net_descr *start_descr,
304 int no)
306 int i;
307 struct spider_net_descr *descr;
308 dma_addr_t buf;
310 descr = start_descr;
311 memset(descr, 0, sizeof(*descr) * no);
313 /* set up the hardware pointers in each descriptor */
314 for (i=0; i<no; i++, descr++) {
315 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
317 buf = pci_map_single(card->pdev, descr,
318 SPIDER_NET_DESCR_SIZE,
319 PCI_DMA_BIDIRECTIONAL);
321 if (pci_dma_mapping_error(buf))
322 goto iommu_error;
324 descr->bus_addr = buf;
325 descr->next = descr + 1;
326 descr->prev = descr - 1;
329 /* do actual circular list */
330 (descr-1)->next = start_descr;
331 start_descr->prev = descr-1;
333 spin_lock_init(&chain->lock);
334 chain->head = start_descr;
335 chain->tail = start_descr;
337 return 0;
339 iommu_error:
340 descr = start_descr;
341 for (i=0; i < no; i++, descr++)
342 if (descr->bus_addr)
343 pci_unmap_single(card->pdev, descr->bus_addr,
344 SPIDER_NET_DESCR_SIZE,
345 PCI_DMA_BIDIRECTIONAL);
346 return -ENOMEM;
350 * spider_net_free_rx_chain_contents - frees descr contents in rx chain
351 * @card: card structure
353 * returns 0 on success, <0 on failure
355 static void
356 spider_net_free_rx_chain_contents(struct spider_net_card *card)
358 struct spider_net_descr *descr;
360 descr = card->rx_chain.head;
361 do {
362 if (descr->skb) {
363 dev_kfree_skb(descr->skb);
364 pci_unmap_single(card->pdev, descr->buf_addr,
365 SPIDER_NET_MAX_FRAME,
366 PCI_DMA_BIDIRECTIONAL);
368 descr = descr->next;
369 } while (descr != card->rx_chain.head);
373 * spider_net_prepare_rx_descr - reinitializes a rx descriptor
374 * @card: card structure
375 * @descr: descriptor to re-init
377 * return 0 on succes, <0 on failure
379 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
380 * Activate the descriptor state-wise
382 static int
383 spider_net_prepare_rx_descr(struct spider_net_card *card,
384 struct spider_net_descr *descr)
386 dma_addr_t buf;
387 int error = 0;
388 int offset;
389 int bufsize;
391 /* we need to round up the buffer size to a multiple of 128 */
392 bufsize = (SPIDER_NET_MAX_FRAME + SPIDER_NET_RXBUF_ALIGN - 1) &
393 (~(SPIDER_NET_RXBUF_ALIGN - 1));
395 /* and we need to have it 128 byte aligned, therefore we allocate a
396 * bit more */
397 /* allocate an skb */
398 descr->skb = dev_alloc_skb(bufsize + SPIDER_NET_RXBUF_ALIGN - 1);
399 if (!descr->skb) {
400 if (netif_msg_rx_err(card) && net_ratelimit())
401 pr_err("Not enough memory to allocate rx buffer\n");
402 card->spider_stats.alloc_rx_skb_error++;
403 return -ENOMEM;
405 descr->buf_size = bufsize;
406 descr->result_size = 0;
407 descr->valid_size = 0;
408 descr->data_status = 0;
409 descr->data_error = 0;
411 offset = ((unsigned long)descr->skb->data) &
412 (SPIDER_NET_RXBUF_ALIGN - 1);
413 if (offset)
414 skb_reserve(descr->skb, SPIDER_NET_RXBUF_ALIGN - offset);
415 /* io-mmu-map the skb */
416 buf = pci_map_single(card->pdev, descr->skb->data,
417 SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
418 descr->buf_addr = buf;
419 if (pci_dma_mapping_error(buf)) {
420 dev_kfree_skb_any(descr->skb);
421 if (netif_msg_rx_err(card) && net_ratelimit())
422 pr_err("Could not iommu-map rx buffer\n");
423 card->spider_stats.rx_iommu_map_error++;
424 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
425 } else {
426 descr->dmac_cmd_status = SPIDER_NET_DESCR_CARDOWNED |
427 SPIDER_NET_DMAC_NOINTR_COMPLETE;
430 return error;
434 * spider_net_enable_rxchtails - sets RX dmac chain tail addresses
435 * @card: card structure
437 * spider_net_enable_rxchtails sets the RX DMAC chain tail adresses in the
438 * chip by writing to the appropriate register. DMA is enabled in
439 * spider_net_enable_rxdmac.
441 static inline void
442 spider_net_enable_rxchtails(struct spider_net_card *card)
444 /* assume chain is aligned correctly */
445 spider_net_write_reg(card, SPIDER_NET_GDADCHA ,
446 card->rx_chain.tail->bus_addr);
450 * spider_net_enable_rxdmac - enables a receive DMA controller
451 * @card: card structure
453 * spider_net_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
454 * in the GDADMACCNTR register
456 static inline void
457 spider_net_enable_rxdmac(struct spider_net_card *card)
459 wmb();
460 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
461 SPIDER_NET_DMA_RX_VALUE);
465 * spider_net_refill_rx_chain - refills descriptors/skbs in the rx chains
466 * @card: card structure
468 * refills descriptors in the rx chain: allocates skbs and iommu-maps them.
470 static void
471 spider_net_refill_rx_chain(struct spider_net_card *card)
473 struct spider_net_descr_chain *chain = &card->rx_chain;
474 unsigned long flags;
476 /* one context doing the refill (and a second context seeing that
477 * and omitting it) is ok. If called by NAPI, we'll be called again
478 * as spider_net_decode_one_descr is called several times. If some
479 * interrupt calls us, the NAPI is about to clean up anyway. */
480 if (!spin_trylock_irqsave(&chain->lock, flags))
481 return;
483 while (spider_net_get_descr_status(chain->head) ==
484 SPIDER_NET_DESCR_NOT_IN_USE) {
485 if (spider_net_prepare_rx_descr(card, chain->head))
486 break;
487 chain->head = chain->head->next;
490 spin_unlock_irqrestore(&chain->lock, flags);
494 * spider_net_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
495 * @card: card structure
497 * returns 0 on success, <0 on failure
499 static int
500 spider_net_alloc_rx_skbs(struct spider_net_card *card)
502 int result;
503 struct spider_net_descr_chain *chain;
505 result = -ENOMEM;
507 chain = &card->rx_chain;
508 /* put at least one buffer into the chain. if this fails,
509 * we've got a problem. if not, spider_net_refill_rx_chain
510 * will do the rest at the end of this function */
511 if (spider_net_prepare_rx_descr(card, chain->head))
512 goto error;
513 else
514 chain->head = chain->head->next;
516 /* this will allocate the rest of the rx buffers; if not, it's
517 * business as usual later on */
518 spider_net_refill_rx_chain(card);
519 spider_net_enable_rxdmac(card);
520 return 0;
522 error:
523 spider_net_free_rx_chain_contents(card);
524 return result;
528 * spider_net_get_multicast_hash - generates hash for multicast filter table
529 * @addr: multicast address
531 * returns the hash value.
533 * spider_net_get_multicast_hash calculates a hash value for a given multicast
534 * address, that is used to set the multicast filter tables
536 static u8
537 spider_net_get_multicast_hash(struct net_device *netdev, __u8 *addr)
539 u32 crc;
540 u8 hash;
541 char addr_for_crc[ETH_ALEN] = { 0, };
542 int i, bit;
544 for (i = 0; i < ETH_ALEN * 8; i++) {
545 bit = (addr[i / 8] >> (i % 8)) & 1;
546 addr_for_crc[ETH_ALEN - 1 - i / 8] += bit << (7 - (i % 8));
549 crc = crc32_be(~0, addr_for_crc, netdev->addr_len);
551 hash = (crc >> 27);
552 hash <<= 3;
553 hash |= crc & 7;
554 hash &= 0xff;
556 return hash;
560 * spider_net_set_multi - sets multicast addresses and promisc flags
561 * @netdev: interface device structure
563 * spider_net_set_multi configures multicast addresses as needed for the
564 * netdev interface. It also sets up multicast, allmulti and promisc
565 * flags appropriately
567 static void
568 spider_net_set_multi(struct net_device *netdev)
570 struct dev_mc_list *mc;
571 u8 hash;
572 int i;
573 u32 reg;
574 struct spider_net_card *card = netdev_priv(netdev);
575 unsigned long bitmask[SPIDER_NET_MULTICAST_HASHES / BITS_PER_LONG] =
576 {0, };
578 spider_net_set_promisc(card);
580 if (netdev->flags & IFF_ALLMULTI) {
581 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES; i++) {
582 set_bit(i, bitmask);
584 goto write_hash;
587 /* well, we know, what the broadcast hash value is: it's xfd
588 hash = spider_net_get_multicast_hash(netdev, netdev->broadcast); */
589 set_bit(0xfd, bitmask);
591 for (mc = netdev->mc_list; mc; mc = mc->next) {
592 hash = spider_net_get_multicast_hash(netdev, mc->dmi_addr);
593 set_bit(hash, bitmask);
596 write_hash:
597 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES / 4; i++) {
598 reg = 0;
599 if (test_bit(i * 4, bitmask))
600 reg += 0x08;
601 reg <<= 8;
602 if (test_bit(i * 4 + 1, bitmask))
603 reg += 0x08;
604 reg <<= 8;
605 if (test_bit(i * 4 + 2, bitmask))
606 reg += 0x08;
607 reg <<= 8;
608 if (test_bit(i * 4 + 3, bitmask))
609 reg += 0x08;
611 spider_net_write_reg(card, SPIDER_NET_GMRMHFILnR + i * 4, reg);
616 * spider_net_disable_rxdmac - disables the receive DMA controller
617 * @card: card structure
619 * spider_net_disable_rxdmac terminates processing on the DMA controller by
620 * turing off DMA and issueing a force end
622 static void
623 spider_net_disable_rxdmac(struct spider_net_card *card)
625 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
626 SPIDER_NET_DMA_RX_FEND_VALUE);
630 * spider_net_prepare_tx_descr - fill tx descriptor with skb data
631 * @card: card structure
632 * @descr: descriptor structure to fill out
633 * @skb: packet to use
635 * returns 0 on success, <0 on failure.
637 * fills out the descriptor structure with skb data and len. Copies data,
638 * if needed (32bit DMA!)
640 static int
641 spider_net_prepare_tx_descr(struct spider_net_card *card,
642 struct sk_buff *skb)
644 struct spider_net_descr *descr;
645 dma_addr_t buf;
646 unsigned long flags;
648 buf = pci_map_single(card->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
649 if (pci_dma_mapping_error(buf)) {
650 if (netif_msg_tx_err(card) && net_ratelimit())
651 pr_err("could not iommu-map packet (%p, %i). "
652 "Dropping packet\n", skb->data, skb->len);
653 card->spider_stats.tx_iommu_map_error++;
654 return -ENOMEM;
657 spin_lock_irqsave(&card->tx_chain.lock, flags);
658 descr = card->tx_chain.head;
659 card->tx_chain.head = descr->next;
661 descr->buf_addr = buf;
662 descr->buf_size = skb->len;
663 descr->next_descr_addr = 0;
664 descr->skb = skb;
665 descr->data_status = 0;
667 descr->dmac_cmd_status =
668 SPIDER_NET_DESCR_CARDOWNED | SPIDER_NET_DMAC_NOCS;
669 spin_unlock_irqrestore(&card->tx_chain.lock, flags);
671 if (skb->protocol == htons(ETH_P_IP))
672 switch (skb->nh.iph->protocol) {
673 case IPPROTO_TCP:
674 descr->dmac_cmd_status |= SPIDER_NET_DMAC_TCP;
675 break;
676 case IPPROTO_UDP:
677 descr->dmac_cmd_status |= SPIDER_NET_DMAC_UDP;
678 break;
681 /* Chain the bus address, so that the DMA engine finds this descr. */
682 descr->prev->next_descr_addr = descr->bus_addr;
684 card->netdev->trans_start = jiffies; /* set netdev watchdog timer */
685 return 0;
688 static int
689 spider_net_set_low_watermark(struct spider_net_card *card)
691 unsigned long flags;
692 int status;
693 int cnt=0;
694 int i;
695 struct spider_net_descr *descr = card->tx_chain.tail;
697 /* Measure the length of the queue. Measurement does not
698 * need to be precise -- does not need a lock. */
699 while (descr != card->tx_chain.head) {
700 status = descr->dmac_cmd_status & SPIDER_NET_DESCR_NOT_IN_USE;
701 if (status == SPIDER_NET_DESCR_NOT_IN_USE)
702 break;
703 descr = descr->next;
704 cnt++;
707 /* If TX queue is short, don't even bother with interrupts */
708 if (cnt < card->num_tx_desc/4)
709 return cnt;
711 /* Set low-watermark 3/4th's of the way into the queue. */
712 descr = card->tx_chain.tail;
713 cnt = (cnt*3)/4;
714 for (i=0;i<cnt; i++)
715 descr = descr->next;
717 /* Set the new watermark, clear the old watermark */
718 spin_lock_irqsave(&card->tx_chain.lock, flags);
719 descr->dmac_cmd_status |= SPIDER_NET_DESCR_TXDESFLG;
720 if (card->low_watermark && card->low_watermark != descr)
721 card->low_watermark->dmac_cmd_status =
722 card->low_watermark->dmac_cmd_status & ~SPIDER_NET_DESCR_TXDESFLG;
723 card->low_watermark = descr;
724 spin_unlock_irqrestore(&card->tx_chain.lock, flags);
725 return cnt;
729 * spider_net_release_tx_chain - processes sent tx descriptors
730 * @card: adapter structure
731 * @brutal: if set, don't care about whether descriptor seems to be in use
733 * returns 0 if the tx ring is empty, otherwise 1.
735 * spider_net_release_tx_chain releases the tx descriptors that spider has
736 * finished with (if non-brutal) or simply release tx descriptors (if brutal).
737 * If some other context is calling this function, we return 1 so that we're
738 * scheduled again (if we were scheduled) and will not loose initiative.
740 static int
741 spider_net_release_tx_chain(struct spider_net_card *card, int brutal)
743 struct spider_net_descr_chain *chain = &card->tx_chain;
744 struct spider_net_descr *descr;
745 struct sk_buff *skb;
746 u32 buf_addr;
747 unsigned long flags;
748 int status;
750 while (chain->tail != chain->head) {
751 spin_lock_irqsave(&chain->lock, flags);
752 descr = chain->tail;
754 status = spider_net_get_descr_status(descr);
755 switch (status) {
756 case SPIDER_NET_DESCR_COMPLETE:
757 card->netdev_stats.tx_packets++;
758 card->netdev_stats.tx_bytes += descr->skb->len;
759 break;
761 case SPIDER_NET_DESCR_CARDOWNED:
762 if (!brutal) {
763 spin_unlock_irqrestore(&chain->lock, flags);
764 return 1;
767 /* fallthrough, if we release the descriptors
768 * brutally (then we don't care about
769 * SPIDER_NET_DESCR_CARDOWNED) */
771 case SPIDER_NET_DESCR_RESPONSE_ERROR:
772 case SPIDER_NET_DESCR_PROTECTION_ERROR:
773 case SPIDER_NET_DESCR_FORCE_END:
774 if (netif_msg_tx_err(card))
775 pr_err("%s: forcing end of tx descriptor "
776 "with status x%02x\n",
777 card->netdev->name, status);
778 card->netdev_stats.tx_errors++;
779 break;
781 default:
782 card->netdev_stats.tx_dropped++;
783 if (!brutal) {
784 spin_unlock_irqrestore(&chain->lock, flags);
785 return 1;
789 chain->tail = descr->next;
790 descr->dmac_cmd_status |= SPIDER_NET_DESCR_NOT_IN_USE;
791 skb = descr->skb;
792 buf_addr = descr->buf_addr;
793 spin_unlock_irqrestore(&chain->lock, flags);
795 /* unmap the skb */
796 if (skb) {
797 pci_unmap_single(card->pdev, buf_addr, skb->len,
798 PCI_DMA_TODEVICE);
799 dev_kfree_skb(skb);
802 return 0;
806 * spider_net_kick_tx_dma - enables TX DMA processing
807 * @card: card structure
808 * @descr: descriptor address to enable TX processing at
810 * This routine will start the transmit DMA running if
811 * it is not already running. This routine ned only be
812 * called when queueing a new packet to an empty tx queue.
813 * Writes the current tx chain head as start address
814 * of the tx descriptor chain and enables the transmission
815 * DMA engine.
817 static inline void
818 spider_net_kick_tx_dma(struct spider_net_card *card)
820 struct spider_net_descr *descr;
822 if (spider_net_read_reg(card, SPIDER_NET_GDTDMACCNTR) &
823 SPIDER_NET_TX_DMA_EN)
824 goto out;
826 descr = card->tx_chain.tail;
827 for (;;) {
828 if (spider_net_get_descr_status(descr) ==
829 SPIDER_NET_DESCR_CARDOWNED) {
830 spider_net_write_reg(card, SPIDER_NET_GDTDCHA,
831 descr->bus_addr);
832 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
833 SPIDER_NET_DMA_TX_VALUE);
834 break;
836 if (descr == card->tx_chain.head)
837 break;
838 descr = descr->next;
841 out:
842 mod_timer(&card->tx_timer, jiffies + SPIDER_NET_TX_TIMER);
846 * spider_net_xmit - transmits a frame over the device
847 * @skb: packet to send out
848 * @netdev: interface device structure
850 * returns 0 on success, !0 on failure
852 static int
853 spider_net_xmit(struct sk_buff *skb, struct net_device *netdev)
855 int cnt;
856 struct spider_net_card *card = netdev_priv(netdev);
857 struct spider_net_descr_chain *chain = &card->tx_chain;
859 spider_net_release_tx_chain(card, 0);
861 if ((chain->head->next == chain->tail->prev) ||
862 (spider_net_prepare_tx_descr(card, skb) != 0)) {
864 card->netdev_stats.tx_dropped++;
865 netif_stop_queue(netdev);
866 return NETDEV_TX_BUSY;
869 cnt = spider_net_set_low_watermark(card);
870 if (cnt < 5)
871 spider_net_kick_tx_dma(card);
872 return NETDEV_TX_OK;
876 * spider_net_cleanup_tx_ring - cleans up the TX ring
877 * @card: card structure
879 * spider_net_cleanup_tx_ring is called by either the tx_timer
880 * or from the NAPI polling routine.
881 * This routine releases resources associted with transmitted
882 * packets, including updating the queue tail pointer.
884 static void
885 spider_net_cleanup_tx_ring(struct spider_net_card *card)
887 if ((spider_net_release_tx_chain(card, 0) != 0) &&
888 (card->netdev->flags & IFF_UP)) {
889 spider_net_kick_tx_dma(card);
890 netif_wake_queue(card->netdev);
895 * spider_net_do_ioctl - called for device ioctls
896 * @netdev: interface device structure
897 * @ifr: request parameter structure for ioctl
898 * @cmd: command code for ioctl
900 * returns 0 on success, <0 on failure. Currently, we have no special ioctls.
901 * -EOPNOTSUPP is returned, if an unknown ioctl was requested
903 static int
904 spider_net_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
906 switch (cmd) {
907 default:
908 return -EOPNOTSUPP;
913 * spider_net_pass_skb_up - takes an skb from a descriptor and passes it on
914 * @descr: descriptor to process
915 * @card: card structure
916 * @napi: whether caller is in NAPI context
918 * returns 1 on success, 0 if no packet was passed to the stack
920 * iommu-unmaps the skb, fills out skb structure and passes the data to the
921 * stack. The descriptor state is not changed.
923 static int
924 spider_net_pass_skb_up(struct spider_net_descr *descr,
925 struct spider_net_card *card, int napi)
927 struct sk_buff *skb;
928 struct net_device *netdev;
929 u32 data_status, data_error;
931 data_status = descr->data_status;
932 data_error = descr->data_error;
934 netdev = card->netdev;
936 /* unmap descriptor */
937 pci_unmap_single(card->pdev, descr->buf_addr, SPIDER_NET_MAX_FRAME,
938 PCI_DMA_FROMDEVICE);
940 /* the cases we'll throw away the packet immediately */
941 if (data_error & SPIDER_NET_DESTROY_RX_FLAGS) {
942 if (netif_msg_rx_err(card))
943 pr_err("error in received descriptor found, "
944 "data_status=x%08x, data_error=x%08x\n",
945 data_status, data_error);
946 card->spider_stats.rx_desc_error++;
947 return 0;
950 skb = descr->skb;
951 skb->dev = netdev;
952 skb_put(skb, descr->valid_size);
954 /* the card seems to add 2 bytes of junk in front
955 * of the ethernet frame */
956 #define SPIDER_MISALIGN 2
957 skb_pull(skb, SPIDER_MISALIGN);
958 skb->protocol = eth_type_trans(skb, netdev);
960 /* checksum offload */
961 if (card->options.rx_csum) {
962 if ( ( (data_status & SPIDER_NET_DATA_STATUS_CKSUM_MASK) ==
963 SPIDER_NET_DATA_STATUS_CKSUM_MASK) &&
964 !(data_error & SPIDER_NET_DATA_ERR_CKSUM_MASK))
965 skb->ip_summed = CHECKSUM_UNNECESSARY;
966 else
967 skb->ip_summed = CHECKSUM_NONE;
968 } else
969 skb->ip_summed = CHECKSUM_NONE;
971 if (data_status & SPIDER_NET_VLAN_PACKET) {
972 /* further enhancements: HW-accel VLAN
973 * vlan_hwaccel_receive_skb
977 /* pass skb up to stack */
978 if (napi)
979 netif_receive_skb(skb);
980 else
981 netif_rx_ni(skb);
983 /* update netdevice statistics */
984 card->netdev_stats.rx_packets++;
985 card->netdev_stats.rx_bytes += skb->len;
987 return 1;
991 * spider_net_decode_one_descr - processes an rx descriptor
992 * @card: card structure
993 * @napi: whether caller is in NAPI context
995 * returns 1 if a packet has been sent to the stack, otherwise 0
997 * processes an rx descriptor by iommu-unmapping the data buffer and passing
998 * the packet up to the stack. This function is called in softirq
999 * context, e.g. either bottom half from interrupt or NAPI polling context
1001 static int
1002 spider_net_decode_one_descr(struct spider_net_card *card, int napi)
1004 struct spider_net_descr_chain *chain = &card->rx_chain;
1005 struct spider_net_descr *descr = chain->tail;
1006 int status;
1007 int result;
1009 status = spider_net_get_descr_status(descr);
1011 if (status == SPIDER_NET_DESCR_CARDOWNED) {
1012 /* nothing in the descriptor yet */
1013 result=0;
1014 goto out;
1017 if (status == SPIDER_NET_DESCR_NOT_IN_USE) {
1018 /* not initialized yet, the ring must be empty */
1019 spider_net_refill_rx_chain(card);
1020 spider_net_enable_rxdmac(card);
1021 result=0;
1022 goto out;
1025 /* descriptor definitively used -- move on tail */
1026 chain->tail = descr->next;
1028 result = 0;
1029 if ( (status == SPIDER_NET_DESCR_RESPONSE_ERROR) ||
1030 (status == SPIDER_NET_DESCR_PROTECTION_ERROR) ||
1031 (status == SPIDER_NET_DESCR_FORCE_END) ) {
1032 if (netif_msg_rx_err(card))
1033 pr_err("%s: dropping RX descriptor with state %d\n",
1034 card->netdev->name, status);
1035 card->netdev_stats.rx_dropped++;
1036 pci_unmap_single(card->pdev, descr->buf_addr,
1037 SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
1038 dev_kfree_skb_irq(descr->skb);
1039 goto refill;
1042 if ( (status != SPIDER_NET_DESCR_COMPLETE) &&
1043 (status != SPIDER_NET_DESCR_FRAME_END) ) {
1044 if (netif_msg_rx_err(card)) {
1045 pr_err("%s: RX descriptor with state %d\n",
1046 card->netdev->name, status);
1047 card->spider_stats.rx_desc_unk_state++;
1049 goto refill;
1052 /* ok, we've got a packet in descr */
1053 result = spider_net_pass_skb_up(descr, card, napi);
1054 refill:
1055 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
1056 /* change the descriptor state: */
1057 if (!napi)
1058 spider_net_refill_rx_chain(card);
1059 out:
1060 return result;
1064 * spider_net_poll - NAPI poll function called by the stack to return packets
1065 * @netdev: interface device structure
1066 * @budget: number of packets we can pass to the stack at most
1068 * returns 0 if no more packets available to the driver/stack. Returns 1,
1069 * if the quota is exceeded, but the driver has still packets.
1071 * spider_net_poll returns all packets from the rx descriptors to the stack
1072 * (using netif_receive_skb). If all/enough packets are up, the driver
1073 * reenables interrupts and returns 0. If not, 1 is returned.
1075 static int
1076 spider_net_poll(struct net_device *netdev, int *budget)
1078 struct spider_net_card *card = netdev_priv(netdev);
1079 int packets_to_do, packets_done = 0;
1080 int no_more_packets = 0;
1082 spider_net_cleanup_tx_ring(card);
1083 packets_to_do = min(*budget, netdev->quota);
1085 while (packets_to_do) {
1086 if (spider_net_decode_one_descr(card, 1)) {
1087 packets_done++;
1088 packets_to_do--;
1089 } else {
1090 /* no more packets for the stack */
1091 no_more_packets = 1;
1092 break;
1096 netdev->quota -= packets_done;
1097 *budget -= packets_done;
1098 spider_net_refill_rx_chain(card);
1100 /* if all packets are in the stack, enable interrupts and return 0 */
1101 /* if not, return 1 */
1102 if (no_more_packets) {
1103 netif_rx_complete(netdev);
1104 spider_net_rx_irq_on(card);
1105 return 0;
1108 return 1;
1112 * spider_net_vlan_rx_reg - initializes VLAN structures in the driver and card
1113 * @netdev: interface device structure
1114 * @grp: vlan_group structure that is registered (NULL on destroying interface)
1116 static void
1117 spider_net_vlan_rx_reg(struct net_device *netdev, struct vlan_group *grp)
1119 /* further enhancement... yet to do */
1120 return;
1124 * spider_net_vlan_rx_add - adds VLAN id to the card filter
1125 * @netdev: interface device structure
1126 * @vid: VLAN id to add
1128 static void
1129 spider_net_vlan_rx_add(struct net_device *netdev, uint16_t vid)
1131 /* further enhancement... yet to do */
1132 /* add vid to card's VLAN filter table */
1133 return;
1137 * spider_net_vlan_rx_kill - removes VLAN id to the card filter
1138 * @netdev: interface device structure
1139 * @vid: VLAN id to remove
1141 static void
1142 spider_net_vlan_rx_kill(struct net_device *netdev, uint16_t vid)
1144 /* further enhancement... yet to do */
1145 /* remove vid from card's VLAN filter table */
1149 * spider_net_get_stats - get interface statistics
1150 * @netdev: interface device structure
1152 * returns the interface statistics residing in the spider_net_card struct
1154 static struct net_device_stats *
1155 spider_net_get_stats(struct net_device *netdev)
1157 struct spider_net_card *card = netdev_priv(netdev);
1158 struct net_device_stats *stats = &card->netdev_stats;
1159 return stats;
1163 * spider_net_change_mtu - changes the MTU of an interface
1164 * @netdev: interface device structure
1165 * @new_mtu: new MTU value
1167 * returns 0 on success, <0 on failure
1169 static int
1170 spider_net_change_mtu(struct net_device *netdev, int new_mtu)
1172 /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
1173 * and mtu is outbound only anyway */
1174 if ( (new_mtu < SPIDER_NET_MIN_MTU ) ||
1175 (new_mtu > SPIDER_NET_MAX_MTU) )
1176 return -EINVAL;
1177 netdev->mtu = new_mtu;
1178 return 0;
1182 * spider_net_set_mac - sets the MAC of an interface
1183 * @netdev: interface device structure
1184 * @ptr: pointer to new MAC address
1186 * Returns 0 on success, <0 on failure. Currently, we don't support this
1187 * and will always return EOPNOTSUPP.
1189 static int
1190 spider_net_set_mac(struct net_device *netdev, void *p)
1192 struct spider_net_card *card = netdev_priv(netdev);
1193 u32 macl, macu, regvalue;
1194 struct sockaddr *addr = p;
1196 if (!is_valid_ether_addr(addr->sa_data))
1197 return -EADDRNOTAVAIL;
1199 /* switch off GMACTPE and GMACRPE */
1200 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1201 regvalue &= ~((1 << 5) | (1 << 6));
1202 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1204 /* write mac */
1205 macu = (addr->sa_data[0]<<24) + (addr->sa_data[1]<<16) +
1206 (addr->sa_data[2]<<8) + (addr->sa_data[3]);
1207 macl = (addr->sa_data[4]<<8) + (addr->sa_data[5]);
1208 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACU, macu);
1209 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACL, macl);
1211 /* switch GMACTPE and GMACRPE back on */
1212 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1213 regvalue |= ((1 << 5) | (1 << 6));
1214 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1216 spider_net_set_promisc(card);
1218 /* look up, whether we have been successful */
1219 if (spider_net_get_mac_address(netdev))
1220 return -EADDRNOTAVAIL;
1221 if (memcmp(netdev->dev_addr,addr->sa_data,netdev->addr_len))
1222 return -EADDRNOTAVAIL;
1224 return 0;
1228 * spider_net_handle_rxram_full - cleans up RX ring upon RX RAM full interrupt
1229 * @card: card structure
1231 * spider_net_handle_rxram_full empties the RX ring so that spider can put
1232 * more packets in it and empty its RX RAM. This is called in bottom half
1233 * context
1235 static void
1236 spider_net_handle_rxram_full(struct spider_net_card *card)
1238 while (spider_net_decode_one_descr(card, 0))
1240 spider_net_enable_rxchtails(card);
1241 spider_net_enable_rxdmac(card);
1242 netif_rx_schedule(card->netdev);
1246 * spider_net_handle_error_irq - handles errors raised by an interrupt
1247 * @card: card structure
1248 * @status_reg: interrupt status register 0 (GHIINT0STS)
1250 * spider_net_handle_error_irq treats or ignores all error conditions
1251 * found when an interrupt is presented
1253 static void
1254 spider_net_handle_error_irq(struct spider_net_card *card, u32 status_reg)
1256 u32 error_reg1, error_reg2;
1257 u32 i;
1258 int show_error = 1;
1260 error_reg1 = spider_net_read_reg(card, SPIDER_NET_GHIINT1STS);
1261 error_reg2 = spider_net_read_reg(card, SPIDER_NET_GHIINT2STS);
1263 /* check GHIINT0STS ************************************/
1264 if (status_reg)
1265 for (i = 0; i < 32; i++)
1266 if (status_reg & (1<<i))
1267 switch (i)
1269 /* let error_reg1 and error_reg2 evaluation decide, what to do
1270 case SPIDER_NET_PHYINT:
1271 case SPIDER_NET_GMAC2INT:
1272 case SPIDER_NET_GMAC1INT:
1273 case SPIDER_NET_GFIFOINT:
1274 case SPIDER_NET_DMACINT:
1275 case SPIDER_NET_GSYSINT:
1276 break; */
1278 case SPIDER_NET_GIPSINT:
1279 show_error = 0;
1280 break;
1282 case SPIDER_NET_GPWOPCMPINT:
1283 /* PHY write operation completed */
1284 show_error = 0;
1285 break;
1286 case SPIDER_NET_GPROPCMPINT:
1287 /* PHY read operation completed */
1288 /* we don't use semaphores, as we poll for the completion
1289 * of the read operation in spider_net_read_phy. Should take
1290 * about 50 us */
1291 show_error = 0;
1292 break;
1293 case SPIDER_NET_GPWFFINT:
1294 /* PHY command queue full */
1295 if (netif_msg_intr(card))
1296 pr_err("PHY write queue full\n");
1297 show_error = 0;
1298 break;
1300 /* case SPIDER_NET_GRMDADRINT: not used. print a message */
1301 /* case SPIDER_NET_GRMARPINT: not used. print a message */
1302 /* case SPIDER_NET_GRMMPINT: not used. print a message */
1304 case SPIDER_NET_GDTDEN0INT:
1305 /* someone has set TX_DMA_EN to 0 */
1306 show_error = 0;
1307 break;
1309 case SPIDER_NET_GDDDEN0INT: /* fallthrough */
1310 case SPIDER_NET_GDCDEN0INT: /* fallthrough */
1311 case SPIDER_NET_GDBDEN0INT: /* fallthrough */
1312 case SPIDER_NET_GDADEN0INT:
1313 /* someone has set RX_DMA_EN to 0 */
1314 show_error = 0;
1315 break;
1317 /* RX interrupts */
1318 case SPIDER_NET_GDDFDCINT:
1319 case SPIDER_NET_GDCFDCINT:
1320 case SPIDER_NET_GDBFDCINT:
1321 case SPIDER_NET_GDAFDCINT:
1322 /* case SPIDER_NET_GDNMINT: not used. print a message */
1323 /* case SPIDER_NET_GCNMINT: not used. print a message */
1324 /* case SPIDER_NET_GBNMINT: not used. print a message */
1325 /* case SPIDER_NET_GANMINT: not used. print a message */
1326 /* case SPIDER_NET_GRFNMINT: not used. print a message */
1327 show_error = 0;
1328 break;
1330 /* TX interrupts */
1331 case SPIDER_NET_GDTFDCINT:
1332 show_error = 0;
1333 break;
1334 case SPIDER_NET_GTTEDINT:
1335 show_error = 0;
1336 break;
1337 case SPIDER_NET_GDTDCEINT:
1338 /* chain end. If a descriptor should be sent, kick off
1339 * tx dma
1340 if (card->tx_chain.tail != card->tx_chain.head)
1341 spider_net_kick_tx_dma(card);
1343 show_error = 0;
1344 break;
1346 /* case SPIDER_NET_G1TMCNTINT: not used. print a message */
1347 /* case SPIDER_NET_GFREECNTINT: not used. print a message */
1350 /* check GHIINT1STS ************************************/
1351 if (error_reg1)
1352 for (i = 0; i < 32; i++)
1353 if (error_reg1 & (1<<i))
1354 switch (i)
1356 case SPIDER_NET_GTMFLLINT:
1357 if (netif_msg_intr(card) && net_ratelimit())
1358 pr_err("Spider TX RAM full\n");
1359 show_error = 0;
1360 break;
1361 case SPIDER_NET_GRFDFLLINT: /* fallthrough */
1362 case SPIDER_NET_GRFCFLLINT: /* fallthrough */
1363 case SPIDER_NET_GRFBFLLINT: /* fallthrough */
1364 case SPIDER_NET_GRFAFLLINT: /* fallthrough */
1365 case SPIDER_NET_GRMFLLINT:
1366 if (netif_msg_intr(card) && net_ratelimit())
1367 pr_debug("Spider RX RAM full, incoming packets "
1368 "might be discarded!\n");
1369 spider_net_rx_irq_off(card);
1370 tasklet_schedule(&card->rxram_full_tl);
1371 show_error = 0;
1372 break;
1374 /* case SPIDER_NET_GTMSHTINT: problem, print a message */
1375 case SPIDER_NET_GDTINVDINT:
1376 /* allrighty. tx from previous descr ok */
1377 show_error = 0;
1378 break;
1380 /* chain end */
1381 case SPIDER_NET_GDDDCEINT: /* fallthrough */
1382 case SPIDER_NET_GDCDCEINT: /* fallthrough */
1383 case SPIDER_NET_GDBDCEINT: /* fallthrough */
1384 case SPIDER_NET_GDADCEINT:
1385 if (netif_msg_intr(card))
1386 pr_err("got descriptor chain end interrupt, "
1387 "restarting DMAC %c.\n",
1388 'D'-(i-SPIDER_NET_GDDDCEINT)/3);
1389 spider_net_refill_rx_chain(card);
1390 spider_net_enable_rxdmac(card);
1391 show_error = 0;
1392 break;
1394 /* invalid descriptor */
1395 case SPIDER_NET_GDDINVDINT: /* fallthrough */
1396 case SPIDER_NET_GDCINVDINT: /* fallthrough */
1397 case SPIDER_NET_GDBINVDINT: /* fallthrough */
1398 case SPIDER_NET_GDAINVDINT:
1399 /* could happen when rx chain is full */
1400 spider_net_refill_rx_chain(card);
1401 spider_net_enable_rxdmac(card);
1402 show_error = 0;
1403 break;
1405 /* case SPIDER_NET_GDTRSERINT: problem, print a message */
1406 /* case SPIDER_NET_GDDRSERINT: problem, print a message */
1407 /* case SPIDER_NET_GDCRSERINT: problem, print a message */
1408 /* case SPIDER_NET_GDBRSERINT: problem, print a message */
1409 /* case SPIDER_NET_GDARSERINT: problem, print a message */
1410 /* case SPIDER_NET_GDSERINT: problem, print a message */
1411 /* case SPIDER_NET_GDTPTERINT: problem, print a message */
1412 /* case SPIDER_NET_GDDPTERINT: problem, print a message */
1413 /* case SPIDER_NET_GDCPTERINT: problem, print a message */
1414 /* case SPIDER_NET_GDBPTERINT: problem, print a message */
1415 /* case SPIDER_NET_GDAPTERINT: problem, print a message */
1416 default:
1417 show_error = 1;
1418 break;
1421 /* check GHIINT2STS ************************************/
1422 if (error_reg2)
1423 for (i = 0; i < 32; i++)
1424 if (error_reg2 & (1<<i))
1425 switch (i)
1427 /* there is nothing we can (want to) do at this time. Log a
1428 * message, we can switch on and off the specific values later on
1429 case SPIDER_NET_GPROPERINT:
1430 case SPIDER_NET_GMCTCRSNGINT:
1431 case SPIDER_NET_GMCTLCOLINT:
1432 case SPIDER_NET_GMCTTMOTINT:
1433 case SPIDER_NET_GMCRCAERINT:
1434 case SPIDER_NET_GMCRCALERINT:
1435 case SPIDER_NET_GMCRALNERINT:
1436 case SPIDER_NET_GMCROVRINT:
1437 case SPIDER_NET_GMCRRNTINT:
1438 case SPIDER_NET_GMCRRXERINT:
1439 case SPIDER_NET_GTITCSERINT:
1440 case SPIDER_NET_GTIFMTERINT:
1441 case SPIDER_NET_GTIPKTRVKINT:
1442 case SPIDER_NET_GTISPINGINT:
1443 case SPIDER_NET_GTISADNGINT:
1444 case SPIDER_NET_GTISPDNGINT:
1445 case SPIDER_NET_GRIFMTERINT:
1446 case SPIDER_NET_GRIPKTRVKINT:
1447 case SPIDER_NET_GRISPINGINT:
1448 case SPIDER_NET_GRISADNGINT:
1449 case SPIDER_NET_GRISPDNGINT:
1450 break;
1452 default:
1453 break;
1456 if ((show_error) && (netif_msg_intr(card)))
1457 pr_err("Got error interrupt on %s, GHIINT0STS = 0x%08x, "
1458 "GHIINT1STS = 0x%08x, GHIINT2STS = 0x%08x\n",
1459 card->netdev->name,
1460 status_reg, error_reg1, error_reg2);
1462 /* clear interrupt sources */
1463 spider_net_write_reg(card, SPIDER_NET_GHIINT1STS, error_reg1);
1464 spider_net_write_reg(card, SPIDER_NET_GHIINT2STS, error_reg2);
1468 * spider_net_interrupt - interrupt handler for spider_net
1469 * @irq: interupt number
1470 * @ptr: pointer to net_device
1471 * @regs: PU registers
1473 * returns IRQ_HANDLED, if interrupt was for driver, or IRQ_NONE, if no
1474 * interrupt found raised by card.
1476 * This is the interrupt handler, that turns off
1477 * interrupts for this device and makes the stack poll the driver
1479 static irqreturn_t
1480 spider_net_interrupt(int irq, void *ptr)
1482 struct net_device *netdev = ptr;
1483 struct spider_net_card *card = netdev_priv(netdev);
1484 u32 status_reg;
1486 status_reg = spider_net_read_reg(card, SPIDER_NET_GHIINT0STS);
1488 if (!status_reg)
1489 return IRQ_NONE;
1491 if (status_reg & SPIDER_NET_RXINT ) {
1492 spider_net_rx_irq_off(card);
1493 netif_rx_schedule(netdev);
1495 if (status_reg & SPIDER_NET_TXINT)
1496 netif_rx_schedule(netdev);
1498 if (status_reg & SPIDER_NET_ERRINT )
1499 spider_net_handle_error_irq(card, status_reg);
1501 /* clear interrupt sources */
1502 spider_net_write_reg(card, SPIDER_NET_GHIINT0STS, status_reg);
1504 return IRQ_HANDLED;
1507 #ifdef CONFIG_NET_POLL_CONTROLLER
1509 * spider_net_poll_controller - artificial interrupt for netconsole etc.
1510 * @netdev: interface device structure
1512 * see Documentation/networking/netconsole.txt
1514 static void
1515 spider_net_poll_controller(struct net_device *netdev)
1517 disable_irq(netdev->irq);
1518 spider_net_interrupt(netdev->irq, netdev);
1519 enable_irq(netdev->irq);
1521 #endif /* CONFIG_NET_POLL_CONTROLLER */
1524 * spider_net_init_card - initializes the card
1525 * @card: card structure
1527 * spider_net_init_card initializes the card so that other registers can
1528 * be used
1530 static void
1531 spider_net_init_card(struct spider_net_card *card)
1533 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1534 SPIDER_NET_CKRCTRL_STOP_VALUE);
1536 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1537 SPIDER_NET_CKRCTRL_RUN_VALUE);
1541 * spider_net_enable_card - enables the card by setting all kinds of regs
1542 * @card: card structure
1544 * spider_net_enable_card sets a lot of SMMIO registers to enable the device
1546 static void
1547 spider_net_enable_card(struct spider_net_card *card)
1549 int i;
1550 /* the following array consists of (register),(value) pairs
1551 * that are set in this function. A register of 0 ends the list */
1552 u32 regs[][2] = {
1553 { SPIDER_NET_GRESUMINTNUM, 0 },
1554 { SPIDER_NET_GREINTNUM, 0 },
1556 /* set interrupt frame number registers */
1557 /* clear the single DMA engine registers first */
1558 { SPIDER_NET_GFAFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1559 { SPIDER_NET_GFBFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1560 { SPIDER_NET_GFCFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1561 { SPIDER_NET_GFDFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1562 /* then set, what we really need */
1563 { SPIDER_NET_GFFRMNUM, SPIDER_NET_FRAMENUM_VALUE },
1565 /* timer counter registers and stuff */
1566 { SPIDER_NET_GFREECNNUM, 0 },
1567 { SPIDER_NET_GONETIMENUM, 0 },
1568 { SPIDER_NET_GTOUTFRMNUM, 0 },
1570 /* RX mode setting */
1571 { SPIDER_NET_GRXMDSET, SPIDER_NET_RXMODE_VALUE },
1572 /* TX mode setting */
1573 { SPIDER_NET_GTXMDSET, SPIDER_NET_TXMODE_VALUE },
1574 /* IPSEC mode setting */
1575 { SPIDER_NET_GIPSECINIT, SPIDER_NET_IPSECINIT_VALUE },
1577 { SPIDER_NET_GFTRESTRT, SPIDER_NET_RESTART_VALUE },
1579 { SPIDER_NET_GMRWOLCTRL, 0 },
1580 { SPIDER_NET_GTESTMD, 0x10000000 },
1581 { SPIDER_NET_GTTQMSK, 0x00400040 },
1583 { SPIDER_NET_GMACINTEN, 0 },
1585 /* flow control stuff */
1586 { SPIDER_NET_GMACAPAUSE, SPIDER_NET_MACAPAUSE_VALUE },
1587 { SPIDER_NET_GMACTXPAUSE, SPIDER_NET_TXPAUSE_VALUE },
1589 { SPIDER_NET_GMACBSTLMT, SPIDER_NET_BURSTLMT_VALUE },
1590 { 0, 0}
1593 i = 0;
1594 while (regs[i][0]) {
1595 spider_net_write_reg(card, regs[i][0], regs[i][1]);
1596 i++;
1599 /* clear unicast filter table entries 1 to 14 */
1600 for (i = 1; i <= 14; i++) {
1601 spider_net_write_reg(card,
1602 SPIDER_NET_GMRUAFILnR + i * 8,
1603 0x00080000);
1604 spider_net_write_reg(card,
1605 SPIDER_NET_GMRUAFILnR + i * 8 + 4,
1606 0x00000000);
1609 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R, 0x08080000);
1611 spider_net_write_reg(card, SPIDER_NET_ECMODE, SPIDER_NET_ECMODE_VALUE);
1613 /* set chain tail adress for RX chains and
1614 * enable DMA */
1615 spider_net_enable_rxchtails(card);
1616 spider_net_enable_rxdmac(card);
1618 spider_net_write_reg(card, SPIDER_NET_GRXDMAEN, SPIDER_NET_WOL_VALUE);
1620 spider_net_write_reg(card, SPIDER_NET_GMACLENLMT,
1621 SPIDER_NET_LENLMT_VALUE);
1622 spider_net_write_reg(card, SPIDER_NET_GMACMODE,
1623 SPIDER_NET_MACMODE_VALUE);
1624 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD,
1625 SPIDER_NET_OPMODE_VALUE);
1627 /* set interrupt mask registers */
1628 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK,
1629 SPIDER_NET_INT0_MASK_VALUE);
1630 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK,
1631 SPIDER_NET_INT1_MASK_VALUE);
1632 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK,
1633 SPIDER_NET_INT2_MASK_VALUE);
1635 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
1636 SPIDER_NET_GDTBSTA);
1640 * spider_net_open - called upon ifonfig up
1641 * @netdev: interface device structure
1643 * returns 0 on success, <0 on failure
1645 * spider_net_open allocates all the descriptors and memory needed for
1646 * operation, sets up multicast list and enables interrupts
1649 spider_net_open(struct net_device *netdev)
1651 struct spider_net_card *card = netdev_priv(netdev);
1652 struct spider_net_descr *descr;
1653 int i, result;
1655 result = -ENOMEM;
1656 if (spider_net_init_chain(card, &card->tx_chain, card->descr,
1657 card->num_tx_desc))
1658 goto alloc_tx_failed;
1660 card->low_watermark = NULL;
1662 /* rx_chain is after tx_chain, so offset is descr + tx_count */
1663 if (spider_net_init_chain(card, &card->rx_chain,
1664 card->descr + card->num_tx_desc,
1665 card->num_rx_desc))
1666 goto alloc_rx_failed;
1668 descr = card->rx_chain.head;
1669 for (i=0; i < card->num_rx_desc; i++, descr++)
1670 descr->next_descr_addr = descr->next->bus_addr;
1672 /* allocate rx skbs */
1673 if (spider_net_alloc_rx_skbs(card))
1674 goto alloc_skbs_failed;
1676 spider_net_set_multi(netdev);
1678 /* further enhancement: setup hw vlan, if needed */
1680 result = -EBUSY;
1681 if (request_irq(netdev->irq, spider_net_interrupt,
1682 IRQF_SHARED, netdev->name, netdev))
1683 goto register_int_failed;
1685 spider_net_enable_card(card);
1687 netif_start_queue(netdev);
1688 netif_carrier_on(netdev);
1689 netif_poll_enable(netdev);
1691 return 0;
1693 register_int_failed:
1694 spider_net_free_rx_chain_contents(card);
1695 alloc_skbs_failed:
1696 spider_net_free_chain(card, &card->rx_chain);
1697 alloc_rx_failed:
1698 spider_net_free_chain(card, &card->tx_chain);
1699 alloc_tx_failed:
1700 return result;
1704 * spider_net_setup_phy - setup PHY
1705 * @card: card structure
1707 * returns 0 on success, <0 on failure
1709 * spider_net_setup_phy is used as part of spider_net_probe. Sets
1710 * the PHY to 1000 Mbps
1712 static int
1713 spider_net_setup_phy(struct spider_net_card *card)
1715 struct mii_phy *phy = &card->phy;
1717 spider_net_write_reg(card, SPIDER_NET_GDTDMASEL,
1718 SPIDER_NET_DMASEL_VALUE);
1719 spider_net_write_reg(card, SPIDER_NET_GPCCTRL,
1720 SPIDER_NET_PHY_CTRL_VALUE);
1721 phy->mii_id = 1;
1722 phy->dev = card->netdev;
1723 phy->mdio_read = spider_net_read_phy;
1724 phy->mdio_write = spider_net_write_phy;
1726 mii_phy_probe(phy, phy->mii_id);
1728 if (phy->def->ops->setup_forced)
1729 phy->def->ops->setup_forced(phy, SPEED_1000, DUPLEX_FULL);
1731 phy->def->ops->enable_fiber(phy);
1733 phy->def->ops->read_link(phy);
1734 pr_info("Found %s with %i Mbps, %s-duplex.\n", phy->def->name,
1735 phy->speed, phy->duplex==1 ? "Full" : "Half");
1737 return 0;
1741 * spider_net_download_firmware - loads firmware into the adapter
1742 * @card: card structure
1743 * @firmware_ptr: pointer to firmware data
1745 * spider_net_download_firmware loads the firmware data into the
1746 * adapter. It assumes the length etc. to be allright.
1748 static int
1749 spider_net_download_firmware(struct spider_net_card *card,
1750 const void *firmware_ptr)
1752 int sequencer, i;
1753 const u32 *fw_ptr = firmware_ptr;
1755 /* stop sequencers */
1756 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1757 SPIDER_NET_STOP_SEQ_VALUE);
1759 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1760 sequencer++) {
1761 spider_net_write_reg(card,
1762 SPIDER_NET_GSnPRGADR + sequencer * 8, 0);
1763 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
1764 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1765 sequencer * 8, *fw_ptr);
1766 fw_ptr++;
1770 if (spider_net_read_reg(card, SPIDER_NET_GSINIT))
1771 return -EIO;
1773 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1774 SPIDER_NET_RUN_SEQ_VALUE);
1776 return 0;
1780 * spider_net_init_firmware - reads in firmware parts
1781 * @card: card structure
1783 * Returns 0 on success, <0 on failure
1785 * spider_net_init_firmware opens the sequencer firmware and does some basic
1786 * checks. This function opens and releases the firmware structure. A call
1787 * to download the firmware is performed before the release.
1789 * Firmware format
1790 * ===============
1791 * spider_fw.bin is expected to be a file containing 6*1024*4 bytes, 4k being
1792 * the program for each sequencer. Use the command
1793 * tail -q -n +2 Seq_code1_0x088.txt Seq_code2_0x090.txt \
1794 * Seq_code3_0x098.txt Seq_code4_0x0A0.txt Seq_code5_0x0A8.txt \
1795 * Seq_code6_0x0B0.txt | xxd -r -p -c4 > spider_fw.bin
1797 * to generate spider_fw.bin, if you have sequencer programs with something
1798 * like the following contents for each sequencer:
1799 * <ONE LINE COMMENT>
1800 * <FIRST 4-BYTES-WORD FOR SEQUENCER>
1801 * <SECOND 4-BYTES-WORD FOR SEQUENCER>
1802 * ...
1803 * <1024th 4-BYTES-WORD FOR SEQUENCER>
1805 static int
1806 spider_net_init_firmware(struct spider_net_card *card)
1808 struct firmware *firmware = NULL;
1809 struct device_node *dn;
1810 const u8 *fw_prop = NULL;
1811 int err = -ENOENT;
1812 int fw_size;
1814 if (request_firmware((const struct firmware **)&firmware,
1815 SPIDER_NET_FIRMWARE_NAME, &card->pdev->dev) == 0) {
1816 if ( (firmware->size != SPIDER_NET_FIRMWARE_LEN) &&
1817 netif_msg_probe(card) ) {
1818 pr_err("Incorrect size of spidernet firmware in " \
1819 "filesystem. Looking in host firmware...\n");
1820 goto try_host_fw;
1822 err = spider_net_download_firmware(card, firmware->data);
1824 release_firmware(firmware);
1825 if (err)
1826 goto try_host_fw;
1828 goto done;
1831 try_host_fw:
1832 dn = pci_device_to_OF_node(card->pdev);
1833 if (!dn)
1834 goto out_err;
1836 fw_prop = get_property(dn, "firmware", &fw_size);
1837 if (!fw_prop)
1838 goto out_err;
1840 if ( (fw_size != SPIDER_NET_FIRMWARE_LEN) &&
1841 netif_msg_probe(card) ) {
1842 pr_err("Incorrect size of spidernet firmware in " \
1843 "host firmware\n");
1844 goto done;
1847 err = spider_net_download_firmware(card, fw_prop);
1849 done:
1850 return err;
1851 out_err:
1852 if (netif_msg_probe(card))
1853 pr_err("Couldn't find spidernet firmware in filesystem " \
1854 "or host firmware\n");
1855 return err;
1859 * spider_net_workaround_rxramfull - work around firmware bug
1860 * @card: card structure
1862 * no return value
1864 static void
1865 spider_net_workaround_rxramfull(struct spider_net_card *card)
1867 int i, sequencer = 0;
1869 /* cancel reset */
1870 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1871 SPIDER_NET_CKRCTRL_RUN_VALUE);
1873 /* empty sequencer data */
1874 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1875 sequencer++) {
1876 spider_net_write_reg(card, SPIDER_NET_GSnPRGADR +
1877 sequencer * 8, 0x0);
1878 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
1879 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1880 sequencer * 8, 0x0);
1884 /* set sequencer operation */
1885 spider_net_write_reg(card, SPIDER_NET_GSINIT, 0x000000fe);
1887 /* reset */
1888 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1889 SPIDER_NET_CKRCTRL_STOP_VALUE);
1893 * spider_net_stop - called upon ifconfig down
1894 * @netdev: interface device structure
1896 * always returns 0
1899 spider_net_stop(struct net_device *netdev)
1901 struct spider_net_card *card = netdev_priv(netdev);
1903 tasklet_kill(&card->rxram_full_tl);
1904 netif_poll_disable(netdev);
1905 netif_carrier_off(netdev);
1906 netif_stop_queue(netdev);
1907 del_timer_sync(&card->tx_timer);
1909 /* disable/mask all interrupts */
1910 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, 0);
1911 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK, 0);
1912 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK, 0);
1914 /* free_irq(netdev->irq, netdev);*/
1915 free_irq(to_pci_dev(netdev->class_dev.dev)->irq, netdev);
1917 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
1918 SPIDER_NET_DMA_TX_FEND_VALUE);
1920 /* turn off DMA, force end */
1921 spider_net_disable_rxdmac(card);
1923 /* release chains */
1924 spider_net_release_tx_chain(card, 1);
1926 spider_net_free_chain(card, &card->tx_chain);
1927 spider_net_free_chain(card, &card->rx_chain);
1929 return 0;
1933 * spider_net_tx_timeout_task - task scheduled by the watchdog timeout
1934 * function (to be called not under interrupt status)
1935 * @data: data, is interface device structure
1937 * called as task when tx hangs, resets interface (if interface is up)
1939 static void
1940 spider_net_tx_timeout_task(void *data)
1942 struct net_device *netdev = data;
1943 struct spider_net_card *card = netdev_priv(netdev);
1945 if (!(netdev->flags & IFF_UP))
1946 goto out;
1948 netif_device_detach(netdev);
1949 spider_net_stop(netdev);
1951 spider_net_workaround_rxramfull(card);
1952 spider_net_init_card(card);
1954 if (spider_net_setup_phy(card))
1955 goto out;
1956 if (spider_net_init_firmware(card))
1957 goto out;
1959 spider_net_open(netdev);
1960 spider_net_kick_tx_dma(card);
1961 netif_device_attach(netdev);
1963 out:
1964 atomic_dec(&card->tx_timeout_task_counter);
1968 * spider_net_tx_timeout - called when the tx timeout watchdog kicks in.
1969 * @netdev: interface device structure
1971 * called, if tx hangs. Schedules a task that resets the interface
1973 static void
1974 spider_net_tx_timeout(struct net_device *netdev)
1976 struct spider_net_card *card;
1978 card = netdev_priv(netdev);
1979 atomic_inc(&card->tx_timeout_task_counter);
1980 if (netdev->flags & IFF_UP)
1981 schedule_work(&card->tx_timeout_task);
1982 else
1983 atomic_dec(&card->tx_timeout_task_counter);
1984 card->spider_stats.tx_timeouts++;
1988 * spider_net_setup_netdev_ops - initialization of net_device operations
1989 * @netdev: net_device structure
1991 * fills out function pointers in the net_device structure
1993 static void
1994 spider_net_setup_netdev_ops(struct net_device *netdev)
1996 netdev->open = &spider_net_open;
1997 netdev->stop = &spider_net_stop;
1998 netdev->hard_start_xmit = &spider_net_xmit;
1999 netdev->get_stats = &spider_net_get_stats;
2000 netdev->set_multicast_list = &spider_net_set_multi;
2001 netdev->set_mac_address = &spider_net_set_mac;
2002 netdev->change_mtu = &spider_net_change_mtu;
2003 netdev->do_ioctl = &spider_net_do_ioctl;
2004 /* tx watchdog */
2005 netdev->tx_timeout = &spider_net_tx_timeout;
2006 netdev->watchdog_timeo = SPIDER_NET_WATCHDOG_TIMEOUT;
2007 /* NAPI */
2008 netdev->poll = &spider_net_poll;
2009 netdev->weight = SPIDER_NET_NAPI_WEIGHT;
2010 /* HW VLAN */
2011 netdev->vlan_rx_register = &spider_net_vlan_rx_reg;
2012 netdev->vlan_rx_add_vid = &spider_net_vlan_rx_add;
2013 netdev->vlan_rx_kill_vid = &spider_net_vlan_rx_kill;
2014 #ifdef CONFIG_NET_POLL_CONTROLLER
2015 /* poll controller */
2016 netdev->poll_controller = &spider_net_poll_controller;
2017 #endif /* CONFIG_NET_POLL_CONTROLLER */
2018 /* ethtool ops */
2019 netdev->ethtool_ops = &spider_net_ethtool_ops;
2023 * spider_net_setup_netdev - initialization of net_device
2024 * @card: card structure
2026 * Returns 0 on success or <0 on failure
2028 * spider_net_setup_netdev initializes the net_device structure
2030 static int
2031 spider_net_setup_netdev(struct spider_net_card *card)
2033 int result;
2034 struct net_device *netdev = card->netdev;
2035 struct device_node *dn;
2036 struct sockaddr addr;
2037 const u8 *mac;
2039 SET_MODULE_OWNER(netdev);
2040 SET_NETDEV_DEV(netdev, &card->pdev->dev);
2042 pci_set_drvdata(card->pdev, netdev);
2044 card->rxram_full_tl.data = (unsigned long) card;
2045 card->rxram_full_tl.func =
2046 (void (*)(unsigned long)) spider_net_handle_rxram_full;
2047 init_timer(&card->tx_timer);
2048 card->tx_timer.function =
2049 (void (*)(unsigned long)) spider_net_cleanup_tx_ring;
2050 card->tx_timer.data = (unsigned long) card;
2051 netdev->irq = card->pdev->irq;
2053 card->options.rx_csum = SPIDER_NET_RX_CSUM_DEFAULT;
2055 card->num_tx_desc = tx_descriptors;
2056 card->num_rx_desc = rx_descriptors;
2058 spider_net_setup_netdev_ops(netdev);
2060 netdev->features = NETIF_F_HW_CSUM | NETIF_F_LLTX;
2061 /* some time: NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
2062 * NETIF_F_HW_VLAN_FILTER */
2064 netdev->irq = card->pdev->irq;
2066 dn = pci_device_to_OF_node(card->pdev);
2067 if (!dn)
2068 return -EIO;
2070 mac = get_property(dn, "local-mac-address", NULL);
2071 if (!mac)
2072 return -EIO;
2073 memcpy(addr.sa_data, mac, ETH_ALEN);
2075 result = spider_net_set_mac(netdev, &addr);
2076 if ((result) && (netif_msg_probe(card)))
2077 pr_err("Failed to set MAC address: %i\n", result);
2079 result = register_netdev(netdev);
2080 if (result) {
2081 if (netif_msg_probe(card))
2082 pr_err("Couldn't register net_device: %i\n",
2083 result);
2084 return result;
2087 if (netif_msg_probe(card))
2088 pr_info("Initialized device %s.\n", netdev->name);
2090 return 0;
2094 * spider_net_alloc_card - allocates net_device and card structure
2096 * returns the card structure or NULL in case of errors
2098 * the card and net_device structures are linked to each other
2100 static struct spider_net_card *
2101 spider_net_alloc_card(void)
2103 struct net_device *netdev;
2104 struct spider_net_card *card;
2105 size_t alloc_size;
2107 alloc_size = sizeof (*card) +
2108 sizeof (struct spider_net_descr) * rx_descriptors +
2109 sizeof (struct spider_net_descr) * tx_descriptors;
2110 netdev = alloc_etherdev(alloc_size);
2111 if (!netdev)
2112 return NULL;
2114 card = netdev_priv(netdev);
2115 card->netdev = netdev;
2116 card->msg_enable = SPIDER_NET_DEFAULT_MSG;
2117 INIT_WORK(&card->tx_timeout_task, spider_net_tx_timeout_task, netdev);
2118 init_waitqueue_head(&card->waitq);
2119 atomic_set(&card->tx_timeout_task_counter, 0);
2121 return card;
2125 * spider_net_undo_pci_setup - releases PCI ressources
2126 * @card: card structure
2128 * spider_net_undo_pci_setup releases the mapped regions
2130 static void
2131 spider_net_undo_pci_setup(struct spider_net_card *card)
2133 iounmap(card->regs);
2134 pci_release_regions(card->pdev);
2138 * spider_net_setup_pci_dev - sets up the device in terms of PCI operations
2139 * @card: card structure
2140 * @pdev: PCI device
2142 * Returns the card structure or NULL if any errors occur
2144 * spider_net_setup_pci_dev initializes pdev and together with the
2145 * functions called in spider_net_open configures the device so that
2146 * data can be transferred over it
2147 * The net_device structure is attached to the card structure, if the
2148 * function returns without error.
2150 static struct spider_net_card *
2151 spider_net_setup_pci_dev(struct pci_dev *pdev)
2153 struct spider_net_card *card;
2154 unsigned long mmio_start, mmio_len;
2156 if (pci_enable_device(pdev)) {
2157 pr_err("Couldn't enable PCI device\n");
2158 return NULL;
2161 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
2162 pr_err("Couldn't find proper PCI device base address.\n");
2163 goto out_disable_dev;
2166 if (pci_request_regions(pdev, spider_net_driver_name)) {
2167 pr_err("Couldn't obtain PCI resources, aborting.\n");
2168 goto out_disable_dev;
2171 pci_set_master(pdev);
2173 card = spider_net_alloc_card();
2174 if (!card) {
2175 pr_err("Couldn't allocate net_device structure, "
2176 "aborting.\n");
2177 goto out_release_regions;
2179 card->pdev = pdev;
2181 /* fetch base address and length of first resource */
2182 mmio_start = pci_resource_start(pdev, 0);
2183 mmio_len = pci_resource_len(pdev, 0);
2185 card->netdev->mem_start = mmio_start;
2186 card->netdev->mem_end = mmio_start + mmio_len;
2187 card->regs = ioremap(mmio_start, mmio_len);
2189 if (!card->regs) {
2190 pr_err("Couldn't obtain PCI resources, aborting.\n");
2191 goto out_release_regions;
2194 return card;
2196 out_release_regions:
2197 pci_release_regions(pdev);
2198 out_disable_dev:
2199 pci_disable_device(pdev);
2200 pci_set_drvdata(pdev, NULL);
2201 return NULL;
2205 * spider_net_probe - initialization of a device
2206 * @pdev: PCI device
2207 * @ent: entry in the device id list
2209 * Returns 0 on success, <0 on failure
2211 * spider_net_probe initializes pdev and registers a net_device
2212 * structure for it. After that, the device can be ifconfig'ed up
2214 static int __devinit
2215 spider_net_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2217 int err = -EIO;
2218 struct spider_net_card *card;
2220 card = spider_net_setup_pci_dev(pdev);
2221 if (!card)
2222 goto out;
2224 spider_net_workaround_rxramfull(card);
2225 spider_net_init_card(card);
2227 err = spider_net_setup_phy(card);
2228 if (err)
2229 goto out_undo_pci;
2231 err = spider_net_init_firmware(card);
2232 if (err)
2233 goto out_undo_pci;
2235 err = spider_net_setup_netdev(card);
2236 if (err)
2237 goto out_undo_pci;
2239 return 0;
2241 out_undo_pci:
2242 spider_net_undo_pci_setup(card);
2243 free_netdev(card->netdev);
2244 out:
2245 return err;
2249 * spider_net_remove - removal of a device
2250 * @pdev: PCI device
2252 * Returns 0 on success, <0 on failure
2254 * spider_net_remove is called to remove the device and unregisters the
2255 * net_device
2257 static void __devexit
2258 spider_net_remove(struct pci_dev *pdev)
2260 struct net_device *netdev;
2261 struct spider_net_card *card;
2263 netdev = pci_get_drvdata(pdev);
2264 card = netdev_priv(netdev);
2266 wait_event(card->waitq,
2267 atomic_read(&card->tx_timeout_task_counter) == 0);
2269 unregister_netdev(netdev);
2271 /* switch off card */
2272 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2273 SPIDER_NET_CKRCTRL_STOP_VALUE);
2274 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2275 SPIDER_NET_CKRCTRL_RUN_VALUE);
2277 spider_net_undo_pci_setup(card);
2278 free_netdev(netdev);
2281 static struct pci_driver spider_net_driver = {
2282 .name = spider_net_driver_name,
2283 .id_table = spider_net_pci_tbl,
2284 .probe = spider_net_probe,
2285 .remove = __devexit_p(spider_net_remove)
2289 * spider_net_init - init function when the driver is loaded
2291 * spider_net_init registers the device driver
2293 static int __init spider_net_init(void)
2295 printk(KERN_INFO "Spidernet version %s.\n", VERSION);
2297 if (rx_descriptors < SPIDER_NET_RX_DESCRIPTORS_MIN) {
2298 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MIN;
2299 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2301 if (rx_descriptors > SPIDER_NET_RX_DESCRIPTORS_MAX) {
2302 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MAX;
2303 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2305 if (tx_descriptors < SPIDER_NET_TX_DESCRIPTORS_MIN) {
2306 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MIN;
2307 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2309 if (tx_descriptors > SPIDER_NET_TX_DESCRIPTORS_MAX) {
2310 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MAX;
2311 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2314 return pci_register_driver(&spider_net_driver);
2318 * spider_net_cleanup - exit function when driver is unloaded
2320 * spider_net_cleanup unregisters the device driver
2322 static void __exit spider_net_cleanup(void)
2324 pci_unregister_driver(&spider_net_driver);
2327 module_init(spider_net_init);
2328 module_exit(spider_net_cleanup);