[PATCH] add bottom_half.h
[linux-2.6/openmoko-kernel.git] / drivers / net / spider_net.c
blobebb6aa39f9c7c18895f7c15d5858674c30f48f70
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 for (descr = chain->tail; !descr->bus_addr; descr = descr->next) {
284 pci_unmap_single(card->pdev, descr->bus_addr,
285 SPIDER_NET_DESCR_SIZE, PCI_DMA_BIDIRECTIONAL);
286 descr->bus_addr = 0;
291 * spider_net_init_chain - links descriptor chain
292 * @card: card structure
293 * @chain: address of chain
294 * @start_descr: address of descriptor array
295 * @no: number of descriptors
297 * we manage a circular list that mirrors the hardware structure,
298 * except that the hardware uses bus addresses.
300 * returns 0 on success, <0 on failure
302 static int
303 spider_net_init_chain(struct spider_net_card *card,
304 struct spider_net_descr_chain *chain,
305 struct spider_net_descr *start_descr,
306 int no)
308 int i;
309 struct spider_net_descr *descr;
310 dma_addr_t buf;
312 descr = start_descr;
313 memset(descr, 0, sizeof(*descr) * no);
315 /* set up the hardware pointers in each descriptor */
316 for (i=0; i<no; i++, descr++) {
317 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
319 buf = pci_map_single(card->pdev, descr,
320 SPIDER_NET_DESCR_SIZE,
321 PCI_DMA_BIDIRECTIONAL);
323 if (pci_dma_mapping_error(buf))
324 goto iommu_error;
326 descr->bus_addr = buf;
327 descr->next = descr + 1;
328 descr->prev = descr - 1;
331 /* do actual circular list */
332 (descr-1)->next = start_descr;
333 start_descr->prev = descr-1;
335 spin_lock_init(&chain->lock);
336 chain->head = start_descr;
337 chain->tail = start_descr;
339 return 0;
341 iommu_error:
342 descr = start_descr;
343 for (i=0; i < no; i++, descr++)
344 if (descr->bus_addr)
345 pci_unmap_single(card->pdev, descr->bus_addr,
346 SPIDER_NET_DESCR_SIZE,
347 PCI_DMA_BIDIRECTIONAL);
348 return -ENOMEM;
352 * spider_net_free_rx_chain_contents - frees descr contents in rx chain
353 * @card: card structure
355 * returns 0 on success, <0 on failure
357 static void
358 spider_net_free_rx_chain_contents(struct spider_net_card *card)
360 struct spider_net_descr *descr;
362 descr = card->rx_chain.head;
363 do {
364 if (descr->skb) {
365 dev_kfree_skb(descr->skb);
366 pci_unmap_single(card->pdev, descr->buf_addr,
367 SPIDER_NET_MAX_FRAME,
368 PCI_DMA_BIDIRECTIONAL);
370 descr = descr->next;
371 } while (descr != card->rx_chain.head);
375 * spider_net_prepare_rx_descr - reinitializes a rx descriptor
376 * @card: card structure
377 * @descr: descriptor to re-init
379 * return 0 on succes, <0 on failure
381 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
382 * Activate the descriptor state-wise
384 static int
385 spider_net_prepare_rx_descr(struct spider_net_card *card,
386 struct spider_net_descr *descr)
388 dma_addr_t buf;
389 int error = 0;
390 int offset;
391 int bufsize;
393 /* we need to round up the buffer size to a multiple of 128 */
394 bufsize = (SPIDER_NET_MAX_FRAME + SPIDER_NET_RXBUF_ALIGN - 1) &
395 (~(SPIDER_NET_RXBUF_ALIGN - 1));
397 /* and we need to have it 128 byte aligned, therefore we allocate a
398 * bit more */
399 /* allocate an skb */
400 descr->skb = dev_alloc_skb(bufsize + SPIDER_NET_RXBUF_ALIGN - 1);
401 if (!descr->skb) {
402 if (netif_msg_rx_err(card) && net_ratelimit())
403 pr_err("Not enough memory to allocate rx buffer\n");
404 card->spider_stats.alloc_rx_skb_error++;
405 return -ENOMEM;
407 descr->buf_size = bufsize;
408 descr->result_size = 0;
409 descr->valid_size = 0;
410 descr->data_status = 0;
411 descr->data_error = 0;
413 offset = ((unsigned long)descr->skb->data) &
414 (SPIDER_NET_RXBUF_ALIGN - 1);
415 if (offset)
416 skb_reserve(descr->skb, SPIDER_NET_RXBUF_ALIGN - offset);
417 /* io-mmu-map the skb */
418 buf = pci_map_single(card->pdev, descr->skb->data,
419 SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
420 descr->buf_addr = buf;
421 if (pci_dma_mapping_error(buf)) {
422 dev_kfree_skb_any(descr->skb);
423 if (netif_msg_rx_err(card) && net_ratelimit())
424 pr_err("Could not iommu-map rx buffer\n");
425 card->spider_stats.rx_iommu_map_error++;
426 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
427 } else {
428 descr->dmac_cmd_status = SPIDER_NET_DESCR_CARDOWNED |
429 SPIDER_NET_DMAC_NOINTR_COMPLETE;
432 return error;
436 * spider_net_enable_rxchtails - sets RX dmac chain tail addresses
437 * @card: card structure
439 * spider_net_enable_rxchtails sets the RX DMAC chain tail adresses in the
440 * chip by writing to the appropriate register. DMA is enabled in
441 * spider_net_enable_rxdmac.
443 static inline void
444 spider_net_enable_rxchtails(struct spider_net_card *card)
446 /* assume chain is aligned correctly */
447 spider_net_write_reg(card, SPIDER_NET_GDADCHA ,
448 card->rx_chain.tail->bus_addr);
452 * spider_net_enable_rxdmac - enables a receive DMA controller
453 * @card: card structure
455 * spider_net_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
456 * in the GDADMACCNTR register
458 static inline void
459 spider_net_enable_rxdmac(struct spider_net_card *card)
461 wmb();
462 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
463 SPIDER_NET_DMA_RX_VALUE);
467 * spider_net_refill_rx_chain - refills descriptors/skbs in the rx chains
468 * @card: card structure
470 * refills descriptors in the rx chain: allocates skbs and iommu-maps them.
472 static void
473 spider_net_refill_rx_chain(struct spider_net_card *card)
475 struct spider_net_descr_chain *chain = &card->rx_chain;
476 unsigned long flags;
478 /* one context doing the refill (and a second context seeing that
479 * and omitting it) is ok. If called by NAPI, we'll be called again
480 * as spider_net_decode_one_descr is called several times. If some
481 * interrupt calls us, the NAPI is about to clean up anyway. */
482 if (!spin_trylock_irqsave(&chain->lock, flags))
483 return;
485 while (spider_net_get_descr_status(chain->head) ==
486 SPIDER_NET_DESCR_NOT_IN_USE) {
487 if (spider_net_prepare_rx_descr(card, chain->head))
488 break;
489 chain->head = chain->head->next;
492 spin_unlock_irqrestore(&chain->lock, flags);
496 * spider_net_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
497 * @card: card structure
499 * returns 0 on success, <0 on failure
501 static int
502 spider_net_alloc_rx_skbs(struct spider_net_card *card)
504 int result;
505 struct spider_net_descr_chain *chain;
507 result = -ENOMEM;
509 chain = &card->rx_chain;
510 /* put at least one buffer into the chain. if this fails,
511 * we've got a problem. if not, spider_net_refill_rx_chain
512 * will do the rest at the end of this function */
513 if (spider_net_prepare_rx_descr(card, chain->head))
514 goto error;
515 else
516 chain->head = chain->head->next;
518 /* this will allocate the rest of the rx buffers; if not, it's
519 * business as usual later on */
520 spider_net_refill_rx_chain(card);
521 spider_net_enable_rxdmac(card);
522 return 0;
524 error:
525 spider_net_free_rx_chain_contents(card);
526 return result;
530 * spider_net_get_multicast_hash - generates hash for multicast filter table
531 * @addr: multicast address
533 * returns the hash value.
535 * spider_net_get_multicast_hash calculates a hash value for a given multicast
536 * address, that is used to set the multicast filter tables
538 static u8
539 spider_net_get_multicast_hash(struct net_device *netdev, __u8 *addr)
541 u32 crc;
542 u8 hash;
543 char addr_for_crc[ETH_ALEN] = { 0, };
544 int i, bit;
546 for (i = 0; i < ETH_ALEN * 8; i++) {
547 bit = (addr[i / 8] >> (i % 8)) & 1;
548 addr_for_crc[ETH_ALEN - 1 - i / 8] += bit << (7 - (i % 8));
551 crc = crc32_be(~0, addr_for_crc, netdev->addr_len);
553 hash = (crc >> 27);
554 hash <<= 3;
555 hash |= crc & 7;
556 hash &= 0xff;
558 return hash;
562 * spider_net_set_multi - sets multicast addresses and promisc flags
563 * @netdev: interface device structure
565 * spider_net_set_multi configures multicast addresses as needed for the
566 * netdev interface. It also sets up multicast, allmulti and promisc
567 * flags appropriately
569 static void
570 spider_net_set_multi(struct net_device *netdev)
572 struct dev_mc_list *mc;
573 u8 hash;
574 int i;
575 u32 reg;
576 struct spider_net_card *card = netdev_priv(netdev);
577 unsigned long bitmask[SPIDER_NET_MULTICAST_HASHES / BITS_PER_LONG] =
578 {0, };
580 spider_net_set_promisc(card);
582 if (netdev->flags & IFF_ALLMULTI) {
583 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES; i++) {
584 set_bit(i, bitmask);
586 goto write_hash;
589 /* well, we know, what the broadcast hash value is: it's xfd
590 hash = spider_net_get_multicast_hash(netdev, netdev->broadcast); */
591 set_bit(0xfd, bitmask);
593 for (mc = netdev->mc_list; mc; mc = mc->next) {
594 hash = spider_net_get_multicast_hash(netdev, mc->dmi_addr);
595 set_bit(hash, bitmask);
598 write_hash:
599 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES / 4; i++) {
600 reg = 0;
601 if (test_bit(i * 4, bitmask))
602 reg += 0x08;
603 reg <<= 8;
604 if (test_bit(i * 4 + 1, bitmask))
605 reg += 0x08;
606 reg <<= 8;
607 if (test_bit(i * 4 + 2, bitmask))
608 reg += 0x08;
609 reg <<= 8;
610 if (test_bit(i * 4 + 3, bitmask))
611 reg += 0x08;
613 spider_net_write_reg(card, SPIDER_NET_GMRMHFILnR + i * 4, reg);
618 * spider_net_disable_rxdmac - disables the receive DMA controller
619 * @card: card structure
621 * spider_net_disable_rxdmac terminates processing on the DMA controller by
622 * turing off DMA and issueing a force end
624 static void
625 spider_net_disable_rxdmac(struct spider_net_card *card)
627 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
628 SPIDER_NET_DMA_RX_FEND_VALUE);
632 * spider_net_prepare_tx_descr - fill tx descriptor with skb data
633 * @card: card structure
634 * @descr: descriptor structure to fill out
635 * @skb: packet to use
637 * returns 0 on success, <0 on failure.
639 * fills out the descriptor structure with skb data and len. Copies data,
640 * if needed (32bit DMA!)
642 static int
643 spider_net_prepare_tx_descr(struct spider_net_card *card,
644 struct sk_buff *skb)
646 struct spider_net_descr *descr;
647 dma_addr_t buf;
648 unsigned long flags;
650 buf = pci_map_single(card->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
651 if (pci_dma_mapping_error(buf)) {
652 if (netif_msg_tx_err(card) && net_ratelimit())
653 pr_err("could not iommu-map packet (%p, %i). "
654 "Dropping packet\n", skb->data, skb->len);
655 card->spider_stats.tx_iommu_map_error++;
656 return -ENOMEM;
659 spin_lock_irqsave(&card->tx_chain.lock, flags);
660 descr = card->tx_chain.head;
661 card->tx_chain.head = descr->next;
663 descr->buf_addr = buf;
664 descr->buf_size = skb->len;
665 descr->next_descr_addr = 0;
666 descr->skb = skb;
667 descr->data_status = 0;
669 descr->dmac_cmd_status =
670 SPIDER_NET_DESCR_CARDOWNED | SPIDER_NET_DMAC_NOCS;
671 spin_unlock_irqrestore(&card->tx_chain.lock, flags);
673 if (skb->protocol == htons(ETH_P_IP))
674 switch (skb->nh.iph->protocol) {
675 case IPPROTO_TCP:
676 descr->dmac_cmd_status |= SPIDER_NET_DMAC_TCP;
677 break;
678 case IPPROTO_UDP:
679 descr->dmac_cmd_status |= SPIDER_NET_DMAC_UDP;
680 break;
683 /* Chain the bus address, so that the DMA engine finds this descr. */
684 descr->prev->next_descr_addr = descr->bus_addr;
686 card->netdev->trans_start = jiffies; /* set netdev watchdog timer */
687 return 0;
690 static int
691 spider_net_set_low_watermark(struct spider_net_card *card)
693 unsigned long flags;
694 int status;
695 int cnt=0;
696 int i;
697 struct spider_net_descr *descr = card->tx_chain.tail;
699 /* Measure the length of the queue. Measurement does not
700 * need to be precise -- does not need a lock. */
701 while (descr != card->tx_chain.head) {
702 status = descr->dmac_cmd_status & SPIDER_NET_DESCR_NOT_IN_USE;
703 if (status == SPIDER_NET_DESCR_NOT_IN_USE)
704 break;
705 descr = descr->next;
706 cnt++;
709 /* If TX queue is short, don't even bother with interrupts */
710 if (cnt < card->num_tx_desc/4)
711 return cnt;
713 /* Set low-watermark 3/4th's of the way into the queue. */
714 descr = card->tx_chain.tail;
715 cnt = (cnt*3)/4;
716 for (i=0;i<cnt; i++)
717 descr = descr->next;
719 /* Set the new watermark, clear the old watermark */
720 spin_lock_irqsave(&card->tx_chain.lock, flags);
721 descr->dmac_cmd_status |= SPIDER_NET_DESCR_TXDESFLG;
722 if (card->low_watermark && card->low_watermark != descr)
723 card->low_watermark->dmac_cmd_status =
724 card->low_watermark->dmac_cmd_status & ~SPIDER_NET_DESCR_TXDESFLG;
725 card->low_watermark = descr;
726 spin_unlock_irqrestore(&card->tx_chain.lock, flags);
727 return cnt;
731 * spider_net_release_tx_chain - processes sent tx descriptors
732 * @card: adapter structure
733 * @brutal: if set, don't care about whether descriptor seems to be in use
735 * returns 0 if the tx ring is empty, otherwise 1.
737 * spider_net_release_tx_chain releases the tx descriptors that spider has
738 * finished with (if non-brutal) or simply release tx descriptors (if brutal).
739 * If some other context is calling this function, we return 1 so that we're
740 * scheduled again (if we were scheduled) and will not loose initiative.
742 static int
743 spider_net_release_tx_chain(struct spider_net_card *card, int brutal)
745 struct spider_net_descr_chain *chain = &card->tx_chain;
746 struct spider_net_descr *descr;
747 struct sk_buff *skb;
748 u32 buf_addr;
749 unsigned long flags;
750 int status;
752 while (chain->tail != chain->head) {
753 spin_lock_irqsave(&chain->lock, flags);
754 descr = chain->tail;
756 status = spider_net_get_descr_status(descr);
757 switch (status) {
758 case SPIDER_NET_DESCR_COMPLETE:
759 card->netdev_stats.tx_packets++;
760 card->netdev_stats.tx_bytes += descr->skb->len;
761 break;
763 case SPIDER_NET_DESCR_CARDOWNED:
764 if (!brutal) {
765 spin_unlock_irqrestore(&chain->lock, flags);
766 return 1;
769 /* fallthrough, if we release the descriptors
770 * brutally (then we don't care about
771 * SPIDER_NET_DESCR_CARDOWNED) */
773 case SPIDER_NET_DESCR_RESPONSE_ERROR:
774 case SPIDER_NET_DESCR_PROTECTION_ERROR:
775 case SPIDER_NET_DESCR_FORCE_END:
776 if (netif_msg_tx_err(card))
777 pr_err("%s: forcing end of tx descriptor "
778 "with status x%02x\n",
779 card->netdev->name, status);
780 card->netdev_stats.tx_errors++;
781 break;
783 default:
784 card->netdev_stats.tx_dropped++;
785 if (!brutal) {
786 spin_unlock_irqrestore(&chain->lock, flags);
787 return 1;
791 chain->tail = descr->next;
792 descr->dmac_cmd_status |= SPIDER_NET_DESCR_NOT_IN_USE;
793 skb = descr->skb;
794 buf_addr = descr->buf_addr;
795 spin_unlock_irqrestore(&chain->lock, flags);
797 /* unmap the skb */
798 if (skb) {
799 pci_unmap_single(card->pdev, buf_addr, skb->len,
800 PCI_DMA_TODEVICE);
801 dev_kfree_skb(skb);
804 return 0;
808 * spider_net_kick_tx_dma - enables TX DMA processing
809 * @card: card structure
810 * @descr: descriptor address to enable TX processing at
812 * This routine will start the transmit DMA running if
813 * it is not already running. This routine ned only be
814 * called when queueing a new packet to an empty tx queue.
815 * Writes the current tx chain head as start address
816 * of the tx descriptor chain and enables the transmission
817 * DMA engine.
819 static inline void
820 spider_net_kick_tx_dma(struct spider_net_card *card)
822 struct spider_net_descr *descr;
824 if (spider_net_read_reg(card, SPIDER_NET_GDTDMACCNTR) &
825 SPIDER_NET_TX_DMA_EN)
826 goto out;
828 descr = card->tx_chain.tail;
829 for (;;) {
830 if (spider_net_get_descr_status(descr) ==
831 SPIDER_NET_DESCR_CARDOWNED) {
832 spider_net_write_reg(card, SPIDER_NET_GDTDCHA,
833 descr->bus_addr);
834 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
835 SPIDER_NET_DMA_TX_VALUE);
836 break;
838 if (descr == card->tx_chain.head)
839 break;
840 descr = descr->next;
843 out:
844 mod_timer(&card->tx_timer, jiffies + SPIDER_NET_TX_TIMER);
848 * spider_net_xmit - transmits a frame over the device
849 * @skb: packet to send out
850 * @netdev: interface device structure
852 * returns 0 on success, !0 on failure
854 static int
855 spider_net_xmit(struct sk_buff *skb, struct net_device *netdev)
857 int cnt;
858 struct spider_net_card *card = netdev_priv(netdev);
859 struct spider_net_descr_chain *chain = &card->tx_chain;
861 spider_net_release_tx_chain(card, 0);
863 if ((chain->head->next == chain->tail->prev) ||
864 (spider_net_prepare_tx_descr(card, skb) != 0)) {
866 card->netdev_stats.tx_dropped++;
867 netif_stop_queue(netdev);
868 return NETDEV_TX_BUSY;
871 cnt = spider_net_set_low_watermark(card);
872 if (cnt < 5)
873 spider_net_kick_tx_dma(card);
874 return NETDEV_TX_OK;
878 * spider_net_cleanup_tx_ring - cleans up the TX ring
879 * @card: card structure
881 * spider_net_cleanup_tx_ring is called by either the tx_timer
882 * or from the NAPI polling routine.
883 * This routine releases resources associted with transmitted
884 * packets, including updating the queue tail pointer.
886 static void
887 spider_net_cleanup_tx_ring(struct spider_net_card *card)
889 if ((spider_net_release_tx_chain(card, 0) != 0) &&
890 (card->netdev->flags & IFF_UP)) {
891 spider_net_kick_tx_dma(card);
892 netif_wake_queue(card->netdev);
897 * spider_net_do_ioctl - called for device ioctls
898 * @netdev: interface device structure
899 * @ifr: request parameter structure for ioctl
900 * @cmd: command code for ioctl
902 * returns 0 on success, <0 on failure. Currently, we have no special ioctls.
903 * -EOPNOTSUPP is returned, if an unknown ioctl was requested
905 static int
906 spider_net_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
908 switch (cmd) {
909 default:
910 return -EOPNOTSUPP;
915 * spider_net_pass_skb_up - takes an skb from a descriptor and passes it on
916 * @descr: descriptor to process
917 * @card: card structure
918 * @napi: whether caller is in NAPI context
920 * returns 1 on success, 0 if no packet was passed to the stack
922 * iommu-unmaps the skb, fills out skb structure and passes the data to the
923 * stack. The descriptor state is not changed.
925 static int
926 spider_net_pass_skb_up(struct spider_net_descr *descr,
927 struct spider_net_card *card, int napi)
929 struct sk_buff *skb;
930 struct net_device *netdev;
931 u32 data_status, data_error;
933 data_status = descr->data_status;
934 data_error = descr->data_error;
936 netdev = card->netdev;
938 /* unmap descriptor */
939 pci_unmap_single(card->pdev, descr->buf_addr, SPIDER_NET_MAX_FRAME,
940 PCI_DMA_FROMDEVICE);
942 /* the cases we'll throw away the packet immediately */
943 if (data_error & SPIDER_NET_DESTROY_RX_FLAGS) {
944 if (netif_msg_rx_err(card))
945 pr_err("error in received descriptor found, "
946 "data_status=x%08x, data_error=x%08x\n",
947 data_status, data_error);
948 card->spider_stats.rx_desc_error++;
949 return 0;
952 skb = descr->skb;
953 skb->dev = netdev;
954 skb_put(skb, descr->valid_size);
956 /* the card seems to add 2 bytes of junk in front
957 * of the ethernet frame */
958 #define SPIDER_MISALIGN 2
959 skb_pull(skb, SPIDER_MISALIGN);
960 skb->protocol = eth_type_trans(skb, netdev);
962 /* checksum offload */
963 if (card->options.rx_csum) {
964 if ( ( (data_status & SPIDER_NET_DATA_STATUS_CKSUM_MASK) ==
965 SPIDER_NET_DATA_STATUS_CKSUM_MASK) &&
966 !(data_error & SPIDER_NET_DATA_ERR_CKSUM_MASK))
967 skb->ip_summed = CHECKSUM_UNNECESSARY;
968 else
969 skb->ip_summed = CHECKSUM_NONE;
970 } else
971 skb->ip_summed = CHECKSUM_NONE;
973 if (data_status & SPIDER_NET_VLAN_PACKET) {
974 /* further enhancements: HW-accel VLAN
975 * vlan_hwaccel_receive_skb
979 /* pass skb up to stack */
980 if (napi)
981 netif_receive_skb(skb);
982 else
983 netif_rx_ni(skb);
985 /* update netdevice statistics */
986 card->netdev_stats.rx_packets++;
987 card->netdev_stats.rx_bytes += skb->len;
989 return 1;
993 * spider_net_decode_one_descr - processes an rx descriptor
994 * @card: card structure
995 * @napi: whether caller is in NAPI context
997 * returns 1 if a packet has been sent to the stack, otherwise 0
999 * processes an rx descriptor by iommu-unmapping the data buffer and passing
1000 * the packet up to the stack. This function is called in softirq
1001 * context, e.g. either bottom half from interrupt or NAPI polling context
1003 static int
1004 spider_net_decode_one_descr(struct spider_net_card *card, int napi)
1006 struct spider_net_descr_chain *chain = &card->rx_chain;
1007 struct spider_net_descr *descr = chain->tail;
1008 int status;
1009 int result;
1011 status = spider_net_get_descr_status(descr);
1013 if (status == SPIDER_NET_DESCR_CARDOWNED) {
1014 /* nothing in the descriptor yet */
1015 result=0;
1016 goto out;
1019 if (status == SPIDER_NET_DESCR_NOT_IN_USE) {
1020 /* not initialized yet, the ring must be empty */
1021 spider_net_refill_rx_chain(card);
1022 spider_net_enable_rxdmac(card);
1023 result=0;
1024 goto out;
1027 /* descriptor definitively used -- move on tail */
1028 chain->tail = descr->next;
1030 result = 0;
1031 if ( (status == SPIDER_NET_DESCR_RESPONSE_ERROR) ||
1032 (status == SPIDER_NET_DESCR_PROTECTION_ERROR) ||
1033 (status == SPIDER_NET_DESCR_FORCE_END) ) {
1034 if (netif_msg_rx_err(card))
1035 pr_err("%s: dropping RX descriptor with state %d\n",
1036 card->netdev->name, status);
1037 card->netdev_stats.rx_dropped++;
1038 pci_unmap_single(card->pdev, descr->buf_addr,
1039 SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
1040 dev_kfree_skb_irq(descr->skb);
1041 goto refill;
1044 if ( (status != SPIDER_NET_DESCR_COMPLETE) &&
1045 (status != SPIDER_NET_DESCR_FRAME_END) ) {
1046 if (netif_msg_rx_err(card)) {
1047 pr_err("%s: RX descriptor with state %d\n",
1048 card->netdev->name, status);
1049 card->spider_stats.rx_desc_unk_state++;
1051 goto refill;
1054 /* ok, we've got a packet in descr */
1055 result = spider_net_pass_skb_up(descr, card, napi);
1056 refill:
1057 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
1058 /* change the descriptor state: */
1059 if (!napi)
1060 spider_net_refill_rx_chain(card);
1061 out:
1062 return result;
1066 * spider_net_poll - NAPI poll function called by the stack to return packets
1067 * @netdev: interface device structure
1068 * @budget: number of packets we can pass to the stack at most
1070 * returns 0 if no more packets available to the driver/stack. Returns 1,
1071 * if the quota is exceeded, but the driver has still packets.
1073 * spider_net_poll returns all packets from the rx descriptors to the stack
1074 * (using netif_receive_skb). If all/enough packets are up, the driver
1075 * reenables interrupts and returns 0. If not, 1 is returned.
1077 static int
1078 spider_net_poll(struct net_device *netdev, int *budget)
1080 struct spider_net_card *card = netdev_priv(netdev);
1081 int packets_to_do, packets_done = 0;
1082 int no_more_packets = 0;
1084 spider_net_cleanup_tx_ring(card);
1085 packets_to_do = min(*budget, netdev->quota);
1087 while (packets_to_do) {
1088 if (spider_net_decode_one_descr(card, 1)) {
1089 packets_done++;
1090 packets_to_do--;
1091 } else {
1092 /* no more packets for the stack */
1093 no_more_packets = 1;
1094 break;
1098 netdev->quota -= packets_done;
1099 *budget -= packets_done;
1100 spider_net_refill_rx_chain(card);
1102 /* if all packets are in the stack, enable interrupts and return 0 */
1103 /* if not, return 1 */
1104 if (no_more_packets) {
1105 netif_rx_complete(netdev);
1106 spider_net_rx_irq_on(card);
1107 return 0;
1110 return 1;
1114 * spider_net_vlan_rx_reg - initializes VLAN structures in the driver and card
1115 * @netdev: interface device structure
1116 * @grp: vlan_group structure that is registered (NULL on destroying interface)
1118 static void
1119 spider_net_vlan_rx_reg(struct net_device *netdev, struct vlan_group *grp)
1121 /* further enhancement... yet to do */
1122 return;
1126 * spider_net_vlan_rx_add - adds VLAN id to the card filter
1127 * @netdev: interface device structure
1128 * @vid: VLAN id to add
1130 static void
1131 spider_net_vlan_rx_add(struct net_device *netdev, uint16_t vid)
1133 /* further enhancement... yet to do */
1134 /* add vid to card's VLAN filter table */
1135 return;
1139 * spider_net_vlan_rx_kill - removes VLAN id to the card filter
1140 * @netdev: interface device structure
1141 * @vid: VLAN id to remove
1143 static void
1144 spider_net_vlan_rx_kill(struct net_device *netdev, uint16_t vid)
1146 /* further enhancement... yet to do */
1147 /* remove vid from card's VLAN filter table */
1151 * spider_net_get_stats - get interface statistics
1152 * @netdev: interface device structure
1154 * returns the interface statistics residing in the spider_net_card struct
1156 static struct net_device_stats *
1157 spider_net_get_stats(struct net_device *netdev)
1159 struct spider_net_card *card = netdev_priv(netdev);
1160 struct net_device_stats *stats = &card->netdev_stats;
1161 return stats;
1165 * spider_net_change_mtu - changes the MTU of an interface
1166 * @netdev: interface device structure
1167 * @new_mtu: new MTU value
1169 * returns 0 on success, <0 on failure
1171 static int
1172 spider_net_change_mtu(struct net_device *netdev, int new_mtu)
1174 /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
1175 * and mtu is outbound only anyway */
1176 if ( (new_mtu < SPIDER_NET_MIN_MTU ) ||
1177 (new_mtu > SPIDER_NET_MAX_MTU) )
1178 return -EINVAL;
1179 netdev->mtu = new_mtu;
1180 return 0;
1184 * spider_net_set_mac - sets the MAC of an interface
1185 * @netdev: interface device structure
1186 * @ptr: pointer to new MAC address
1188 * Returns 0 on success, <0 on failure. Currently, we don't support this
1189 * and will always return EOPNOTSUPP.
1191 static int
1192 spider_net_set_mac(struct net_device *netdev, void *p)
1194 struct spider_net_card *card = netdev_priv(netdev);
1195 u32 macl, macu, regvalue;
1196 struct sockaddr *addr = p;
1198 if (!is_valid_ether_addr(addr->sa_data))
1199 return -EADDRNOTAVAIL;
1201 /* switch off GMACTPE and GMACRPE */
1202 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1203 regvalue &= ~((1 << 5) | (1 << 6));
1204 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1206 /* write mac */
1207 macu = (addr->sa_data[0]<<24) + (addr->sa_data[1]<<16) +
1208 (addr->sa_data[2]<<8) + (addr->sa_data[3]);
1209 macl = (addr->sa_data[4]<<8) + (addr->sa_data[5]);
1210 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACU, macu);
1211 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACL, macl);
1213 /* switch GMACTPE and GMACRPE back on */
1214 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1215 regvalue |= ((1 << 5) | (1 << 6));
1216 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1218 spider_net_set_promisc(card);
1220 /* look up, whether we have been successful */
1221 if (spider_net_get_mac_address(netdev))
1222 return -EADDRNOTAVAIL;
1223 if (memcmp(netdev->dev_addr,addr->sa_data,netdev->addr_len))
1224 return -EADDRNOTAVAIL;
1226 return 0;
1230 * spider_net_handle_rxram_full - cleans up RX ring upon RX RAM full interrupt
1231 * @card: card structure
1233 * spider_net_handle_rxram_full empties the RX ring so that spider can put
1234 * more packets in it and empty its RX RAM. This is called in bottom half
1235 * context
1237 static void
1238 spider_net_handle_rxram_full(struct spider_net_card *card)
1240 while (spider_net_decode_one_descr(card, 0))
1242 spider_net_enable_rxchtails(card);
1243 spider_net_enable_rxdmac(card);
1244 netif_rx_schedule(card->netdev);
1248 * spider_net_handle_error_irq - handles errors raised by an interrupt
1249 * @card: card structure
1250 * @status_reg: interrupt status register 0 (GHIINT0STS)
1252 * spider_net_handle_error_irq treats or ignores all error conditions
1253 * found when an interrupt is presented
1255 static void
1256 spider_net_handle_error_irq(struct spider_net_card *card, u32 status_reg)
1258 u32 error_reg1, error_reg2;
1259 u32 i;
1260 int show_error = 1;
1262 error_reg1 = spider_net_read_reg(card, SPIDER_NET_GHIINT1STS);
1263 error_reg2 = spider_net_read_reg(card, SPIDER_NET_GHIINT2STS);
1265 /* check GHIINT0STS ************************************/
1266 if (status_reg)
1267 for (i = 0; i < 32; i++)
1268 if (status_reg & (1<<i))
1269 switch (i)
1271 /* let error_reg1 and error_reg2 evaluation decide, what to do
1272 case SPIDER_NET_PHYINT:
1273 case SPIDER_NET_GMAC2INT:
1274 case SPIDER_NET_GMAC1INT:
1275 case SPIDER_NET_GFIFOINT:
1276 case SPIDER_NET_DMACINT:
1277 case SPIDER_NET_GSYSINT:
1278 break; */
1280 case SPIDER_NET_GIPSINT:
1281 show_error = 0;
1282 break;
1284 case SPIDER_NET_GPWOPCMPINT:
1285 /* PHY write operation completed */
1286 show_error = 0;
1287 break;
1288 case SPIDER_NET_GPROPCMPINT:
1289 /* PHY read operation completed */
1290 /* we don't use semaphores, as we poll for the completion
1291 * of the read operation in spider_net_read_phy. Should take
1292 * about 50 us */
1293 show_error = 0;
1294 break;
1295 case SPIDER_NET_GPWFFINT:
1296 /* PHY command queue full */
1297 if (netif_msg_intr(card))
1298 pr_err("PHY write queue full\n");
1299 show_error = 0;
1300 break;
1302 /* case SPIDER_NET_GRMDADRINT: not used. print a message */
1303 /* case SPIDER_NET_GRMARPINT: not used. print a message */
1304 /* case SPIDER_NET_GRMMPINT: not used. print a message */
1306 case SPIDER_NET_GDTDEN0INT:
1307 /* someone has set TX_DMA_EN to 0 */
1308 show_error = 0;
1309 break;
1311 case SPIDER_NET_GDDDEN0INT: /* fallthrough */
1312 case SPIDER_NET_GDCDEN0INT: /* fallthrough */
1313 case SPIDER_NET_GDBDEN0INT: /* fallthrough */
1314 case SPIDER_NET_GDADEN0INT:
1315 /* someone has set RX_DMA_EN to 0 */
1316 show_error = 0;
1317 break;
1319 /* RX interrupts */
1320 case SPIDER_NET_GDDFDCINT:
1321 case SPIDER_NET_GDCFDCINT:
1322 case SPIDER_NET_GDBFDCINT:
1323 case SPIDER_NET_GDAFDCINT:
1324 /* case SPIDER_NET_GDNMINT: not used. print a message */
1325 /* case SPIDER_NET_GCNMINT: not used. print a message */
1326 /* case SPIDER_NET_GBNMINT: not used. print a message */
1327 /* case SPIDER_NET_GANMINT: not used. print a message */
1328 /* case SPIDER_NET_GRFNMINT: not used. print a message */
1329 show_error = 0;
1330 break;
1332 /* TX interrupts */
1333 case SPIDER_NET_GDTFDCINT:
1334 show_error = 0;
1335 break;
1336 case SPIDER_NET_GTTEDINT:
1337 show_error = 0;
1338 break;
1339 case SPIDER_NET_GDTDCEINT:
1340 /* chain end. If a descriptor should be sent, kick off
1341 * tx dma
1342 if (card->tx_chain.tail != card->tx_chain.head)
1343 spider_net_kick_tx_dma(card);
1345 show_error = 0;
1346 break;
1348 /* case SPIDER_NET_G1TMCNTINT: not used. print a message */
1349 /* case SPIDER_NET_GFREECNTINT: not used. print a message */
1352 /* check GHIINT1STS ************************************/
1353 if (error_reg1)
1354 for (i = 0; i < 32; i++)
1355 if (error_reg1 & (1<<i))
1356 switch (i)
1358 case SPIDER_NET_GTMFLLINT:
1359 if (netif_msg_intr(card) && net_ratelimit())
1360 pr_err("Spider TX RAM full\n");
1361 show_error = 0;
1362 break;
1363 case SPIDER_NET_GRFDFLLINT: /* fallthrough */
1364 case SPIDER_NET_GRFCFLLINT: /* fallthrough */
1365 case SPIDER_NET_GRFBFLLINT: /* fallthrough */
1366 case SPIDER_NET_GRFAFLLINT: /* fallthrough */
1367 case SPIDER_NET_GRMFLLINT:
1368 if (netif_msg_intr(card) && net_ratelimit())
1369 pr_debug("Spider RX RAM full, incoming packets "
1370 "might be discarded!\n");
1371 spider_net_rx_irq_off(card);
1372 tasklet_schedule(&card->rxram_full_tl);
1373 show_error = 0;
1374 break;
1376 /* case SPIDER_NET_GTMSHTINT: problem, print a message */
1377 case SPIDER_NET_GDTINVDINT:
1378 /* allrighty. tx from previous descr ok */
1379 show_error = 0;
1380 break;
1382 /* chain end */
1383 case SPIDER_NET_GDDDCEINT: /* fallthrough */
1384 case SPIDER_NET_GDCDCEINT: /* fallthrough */
1385 case SPIDER_NET_GDBDCEINT: /* fallthrough */
1386 case SPIDER_NET_GDADCEINT:
1387 if (netif_msg_intr(card))
1388 pr_err("got descriptor chain end interrupt, "
1389 "restarting DMAC %c.\n",
1390 'D'-(i-SPIDER_NET_GDDDCEINT)/3);
1391 spider_net_refill_rx_chain(card);
1392 spider_net_enable_rxdmac(card);
1393 show_error = 0;
1394 break;
1396 /* invalid descriptor */
1397 case SPIDER_NET_GDDINVDINT: /* fallthrough */
1398 case SPIDER_NET_GDCINVDINT: /* fallthrough */
1399 case SPIDER_NET_GDBINVDINT: /* fallthrough */
1400 case SPIDER_NET_GDAINVDINT:
1401 /* could happen when rx chain is full */
1402 spider_net_refill_rx_chain(card);
1403 spider_net_enable_rxdmac(card);
1404 show_error = 0;
1405 break;
1407 /* case SPIDER_NET_GDTRSERINT: problem, print a message */
1408 /* case SPIDER_NET_GDDRSERINT: problem, print a message */
1409 /* case SPIDER_NET_GDCRSERINT: problem, print a message */
1410 /* case SPIDER_NET_GDBRSERINT: problem, print a message */
1411 /* case SPIDER_NET_GDARSERINT: problem, print a message */
1412 /* case SPIDER_NET_GDSERINT: problem, print a message */
1413 /* case SPIDER_NET_GDTPTERINT: problem, print a message */
1414 /* case SPIDER_NET_GDDPTERINT: problem, print a message */
1415 /* case SPIDER_NET_GDCPTERINT: problem, print a message */
1416 /* case SPIDER_NET_GDBPTERINT: problem, print a message */
1417 /* case SPIDER_NET_GDAPTERINT: problem, print a message */
1418 default:
1419 show_error = 1;
1420 break;
1423 /* check GHIINT2STS ************************************/
1424 if (error_reg2)
1425 for (i = 0; i < 32; i++)
1426 if (error_reg2 & (1<<i))
1427 switch (i)
1429 /* there is nothing we can (want to) do at this time. Log a
1430 * message, we can switch on and off the specific values later on
1431 case SPIDER_NET_GPROPERINT:
1432 case SPIDER_NET_GMCTCRSNGINT:
1433 case SPIDER_NET_GMCTLCOLINT:
1434 case SPIDER_NET_GMCTTMOTINT:
1435 case SPIDER_NET_GMCRCAERINT:
1436 case SPIDER_NET_GMCRCALERINT:
1437 case SPIDER_NET_GMCRALNERINT:
1438 case SPIDER_NET_GMCROVRINT:
1439 case SPIDER_NET_GMCRRNTINT:
1440 case SPIDER_NET_GMCRRXERINT:
1441 case SPIDER_NET_GTITCSERINT:
1442 case SPIDER_NET_GTIFMTERINT:
1443 case SPIDER_NET_GTIPKTRVKINT:
1444 case SPIDER_NET_GTISPINGINT:
1445 case SPIDER_NET_GTISADNGINT:
1446 case SPIDER_NET_GTISPDNGINT:
1447 case SPIDER_NET_GRIFMTERINT:
1448 case SPIDER_NET_GRIPKTRVKINT:
1449 case SPIDER_NET_GRISPINGINT:
1450 case SPIDER_NET_GRISADNGINT:
1451 case SPIDER_NET_GRISPDNGINT:
1452 break;
1454 default:
1455 break;
1458 if ((show_error) && (netif_msg_intr(card)))
1459 pr_err("Got error interrupt on %s, GHIINT0STS = 0x%08x, "
1460 "GHIINT1STS = 0x%08x, GHIINT2STS = 0x%08x\n",
1461 card->netdev->name,
1462 status_reg, error_reg1, error_reg2);
1464 /* clear interrupt sources */
1465 spider_net_write_reg(card, SPIDER_NET_GHIINT1STS, error_reg1);
1466 spider_net_write_reg(card, SPIDER_NET_GHIINT2STS, error_reg2);
1470 * spider_net_interrupt - interrupt handler for spider_net
1471 * @irq: interupt number
1472 * @ptr: pointer to net_device
1473 * @regs: PU registers
1475 * returns IRQ_HANDLED, if interrupt was for driver, or IRQ_NONE, if no
1476 * interrupt found raised by card.
1478 * This is the interrupt handler, that turns off
1479 * interrupts for this device and makes the stack poll the driver
1481 static irqreturn_t
1482 spider_net_interrupt(int irq, void *ptr)
1484 struct net_device *netdev = ptr;
1485 struct spider_net_card *card = netdev_priv(netdev);
1486 u32 status_reg;
1488 status_reg = spider_net_read_reg(card, SPIDER_NET_GHIINT0STS);
1490 if (!status_reg)
1491 return IRQ_NONE;
1493 if (status_reg & SPIDER_NET_RXINT ) {
1494 spider_net_rx_irq_off(card);
1495 netif_rx_schedule(netdev);
1497 if (status_reg & SPIDER_NET_TXINT)
1498 netif_rx_schedule(netdev);
1500 if (status_reg & SPIDER_NET_ERRINT )
1501 spider_net_handle_error_irq(card, status_reg);
1503 /* clear interrupt sources */
1504 spider_net_write_reg(card, SPIDER_NET_GHIINT0STS, status_reg);
1506 return IRQ_HANDLED;
1509 #ifdef CONFIG_NET_POLL_CONTROLLER
1511 * spider_net_poll_controller - artificial interrupt for netconsole etc.
1512 * @netdev: interface device structure
1514 * see Documentation/networking/netconsole.txt
1516 static void
1517 spider_net_poll_controller(struct net_device *netdev)
1519 disable_irq(netdev->irq);
1520 spider_net_interrupt(netdev->irq, netdev);
1521 enable_irq(netdev->irq);
1523 #endif /* CONFIG_NET_POLL_CONTROLLER */
1526 * spider_net_init_card - initializes the card
1527 * @card: card structure
1529 * spider_net_init_card initializes the card so that other registers can
1530 * be used
1532 static void
1533 spider_net_init_card(struct spider_net_card *card)
1535 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1536 SPIDER_NET_CKRCTRL_STOP_VALUE);
1538 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1539 SPIDER_NET_CKRCTRL_RUN_VALUE);
1543 * spider_net_enable_card - enables the card by setting all kinds of regs
1544 * @card: card structure
1546 * spider_net_enable_card sets a lot of SMMIO registers to enable the device
1548 static void
1549 spider_net_enable_card(struct spider_net_card *card)
1551 int i;
1552 /* the following array consists of (register),(value) pairs
1553 * that are set in this function. A register of 0 ends the list */
1554 u32 regs[][2] = {
1555 { SPIDER_NET_GRESUMINTNUM, 0 },
1556 { SPIDER_NET_GREINTNUM, 0 },
1558 /* set interrupt frame number registers */
1559 /* clear the single DMA engine registers first */
1560 { SPIDER_NET_GFAFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1561 { SPIDER_NET_GFBFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1562 { SPIDER_NET_GFCFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1563 { SPIDER_NET_GFDFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1564 /* then set, what we really need */
1565 { SPIDER_NET_GFFRMNUM, SPIDER_NET_FRAMENUM_VALUE },
1567 /* timer counter registers and stuff */
1568 { SPIDER_NET_GFREECNNUM, 0 },
1569 { SPIDER_NET_GONETIMENUM, 0 },
1570 { SPIDER_NET_GTOUTFRMNUM, 0 },
1572 /* RX mode setting */
1573 { SPIDER_NET_GRXMDSET, SPIDER_NET_RXMODE_VALUE },
1574 /* TX mode setting */
1575 { SPIDER_NET_GTXMDSET, SPIDER_NET_TXMODE_VALUE },
1576 /* IPSEC mode setting */
1577 { SPIDER_NET_GIPSECINIT, SPIDER_NET_IPSECINIT_VALUE },
1579 { SPIDER_NET_GFTRESTRT, SPIDER_NET_RESTART_VALUE },
1581 { SPIDER_NET_GMRWOLCTRL, 0 },
1582 { SPIDER_NET_GTESTMD, 0x10000000 },
1583 { SPIDER_NET_GTTQMSK, 0x00400040 },
1585 { SPIDER_NET_GMACINTEN, 0 },
1587 /* flow control stuff */
1588 { SPIDER_NET_GMACAPAUSE, SPIDER_NET_MACAPAUSE_VALUE },
1589 { SPIDER_NET_GMACTXPAUSE, SPIDER_NET_TXPAUSE_VALUE },
1591 { SPIDER_NET_GMACBSTLMT, SPIDER_NET_BURSTLMT_VALUE },
1592 { 0, 0}
1595 i = 0;
1596 while (regs[i][0]) {
1597 spider_net_write_reg(card, regs[i][0], regs[i][1]);
1598 i++;
1601 /* clear unicast filter table entries 1 to 14 */
1602 for (i = 1; i <= 14; i++) {
1603 spider_net_write_reg(card,
1604 SPIDER_NET_GMRUAFILnR + i * 8,
1605 0x00080000);
1606 spider_net_write_reg(card,
1607 SPIDER_NET_GMRUAFILnR + i * 8 + 4,
1608 0x00000000);
1611 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R, 0x08080000);
1613 spider_net_write_reg(card, SPIDER_NET_ECMODE, SPIDER_NET_ECMODE_VALUE);
1615 /* set chain tail adress for RX chains and
1616 * enable DMA */
1617 spider_net_enable_rxchtails(card);
1618 spider_net_enable_rxdmac(card);
1620 spider_net_write_reg(card, SPIDER_NET_GRXDMAEN, SPIDER_NET_WOL_VALUE);
1622 spider_net_write_reg(card, SPIDER_NET_GMACLENLMT,
1623 SPIDER_NET_LENLMT_VALUE);
1624 spider_net_write_reg(card, SPIDER_NET_GMACMODE,
1625 SPIDER_NET_MACMODE_VALUE);
1626 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD,
1627 SPIDER_NET_OPMODE_VALUE);
1629 /* set interrupt mask registers */
1630 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK,
1631 SPIDER_NET_INT0_MASK_VALUE);
1632 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK,
1633 SPIDER_NET_INT1_MASK_VALUE);
1634 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK,
1635 SPIDER_NET_INT2_MASK_VALUE);
1637 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
1638 SPIDER_NET_GDTBSTA);
1642 * spider_net_open - called upon ifonfig up
1643 * @netdev: interface device structure
1645 * returns 0 on success, <0 on failure
1647 * spider_net_open allocates all the descriptors and memory needed for
1648 * operation, sets up multicast list and enables interrupts
1651 spider_net_open(struct net_device *netdev)
1653 struct spider_net_card *card = netdev_priv(netdev);
1654 struct spider_net_descr *descr;
1655 int i, result;
1657 result = -ENOMEM;
1658 if (spider_net_init_chain(card, &card->tx_chain, card->descr,
1659 card->num_tx_desc))
1660 goto alloc_tx_failed;
1662 card->low_watermark = NULL;
1664 /* rx_chain is after tx_chain, so offset is descr + tx_count */
1665 if (spider_net_init_chain(card, &card->rx_chain,
1666 card->descr + card->num_tx_desc,
1667 card->num_rx_desc))
1668 goto alloc_rx_failed;
1670 descr = card->rx_chain.head;
1671 for (i=0; i < card->num_rx_desc; i++, descr++)
1672 descr->next_descr_addr = descr->next->bus_addr;
1674 /* allocate rx skbs */
1675 if (spider_net_alloc_rx_skbs(card))
1676 goto alloc_skbs_failed;
1678 spider_net_set_multi(netdev);
1680 /* further enhancement: setup hw vlan, if needed */
1682 result = -EBUSY;
1683 if (request_irq(netdev->irq, spider_net_interrupt,
1684 IRQF_SHARED, netdev->name, netdev))
1685 goto register_int_failed;
1687 spider_net_enable_card(card);
1689 netif_start_queue(netdev);
1690 netif_carrier_on(netdev);
1691 netif_poll_enable(netdev);
1693 return 0;
1695 register_int_failed:
1696 spider_net_free_rx_chain_contents(card);
1697 alloc_skbs_failed:
1698 spider_net_free_chain(card, &card->rx_chain);
1699 alloc_rx_failed:
1700 spider_net_free_chain(card, &card->tx_chain);
1701 alloc_tx_failed:
1702 return result;
1706 * spider_net_setup_phy - setup PHY
1707 * @card: card structure
1709 * returns 0 on success, <0 on failure
1711 * spider_net_setup_phy is used as part of spider_net_probe. Sets
1712 * the PHY to 1000 Mbps
1714 static int
1715 spider_net_setup_phy(struct spider_net_card *card)
1717 struct mii_phy *phy = &card->phy;
1719 spider_net_write_reg(card, SPIDER_NET_GDTDMASEL,
1720 SPIDER_NET_DMASEL_VALUE);
1721 spider_net_write_reg(card, SPIDER_NET_GPCCTRL,
1722 SPIDER_NET_PHY_CTRL_VALUE);
1723 phy->mii_id = 1;
1724 phy->dev = card->netdev;
1725 phy->mdio_read = spider_net_read_phy;
1726 phy->mdio_write = spider_net_write_phy;
1728 mii_phy_probe(phy, phy->mii_id);
1730 if (phy->def->ops->setup_forced)
1731 phy->def->ops->setup_forced(phy, SPEED_1000, DUPLEX_FULL);
1733 phy->def->ops->enable_fiber(phy);
1735 phy->def->ops->read_link(phy);
1736 pr_info("Found %s with %i Mbps, %s-duplex.\n", phy->def->name,
1737 phy->speed, phy->duplex==1 ? "Full" : "Half");
1739 return 0;
1743 * spider_net_download_firmware - loads firmware into the adapter
1744 * @card: card structure
1745 * @firmware_ptr: pointer to firmware data
1747 * spider_net_download_firmware loads the firmware data into the
1748 * adapter. It assumes the length etc. to be allright.
1750 static int
1751 spider_net_download_firmware(struct spider_net_card *card,
1752 const void *firmware_ptr)
1754 int sequencer, i;
1755 const u32 *fw_ptr = firmware_ptr;
1757 /* stop sequencers */
1758 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1759 SPIDER_NET_STOP_SEQ_VALUE);
1761 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1762 sequencer++) {
1763 spider_net_write_reg(card,
1764 SPIDER_NET_GSnPRGADR + sequencer * 8, 0);
1765 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
1766 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1767 sequencer * 8, *fw_ptr);
1768 fw_ptr++;
1772 if (spider_net_read_reg(card, SPIDER_NET_GSINIT))
1773 return -EIO;
1775 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1776 SPIDER_NET_RUN_SEQ_VALUE);
1778 return 0;
1782 * spider_net_init_firmware - reads in firmware parts
1783 * @card: card structure
1785 * Returns 0 on success, <0 on failure
1787 * spider_net_init_firmware opens the sequencer firmware and does some basic
1788 * checks. This function opens and releases the firmware structure. A call
1789 * to download the firmware is performed before the release.
1791 * Firmware format
1792 * ===============
1793 * spider_fw.bin is expected to be a file containing 6*1024*4 bytes, 4k being
1794 * the program for each sequencer. Use the command
1795 * tail -q -n +2 Seq_code1_0x088.txt Seq_code2_0x090.txt \
1796 * Seq_code3_0x098.txt Seq_code4_0x0A0.txt Seq_code5_0x0A8.txt \
1797 * Seq_code6_0x0B0.txt | xxd -r -p -c4 > spider_fw.bin
1799 * to generate spider_fw.bin, if you have sequencer programs with something
1800 * like the following contents for each sequencer:
1801 * <ONE LINE COMMENT>
1802 * <FIRST 4-BYTES-WORD FOR SEQUENCER>
1803 * <SECOND 4-BYTES-WORD FOR SEQUENCER>
1804 * ...
1805 * <1024th 4-BYTES-WORD FOR SEQUENCER>
1807 static int
1808 spider_net_init_firmware(struct spider_net_card *card)
1810 struct firmware *firmware = NULL;
1811 struct device_node *dn;
1812 const u8 *fw_prop = NULL;
1813 int err = -ENOENT;
1814 int fw_size;
1816 if (request_firmware((const struct firmware **)&firmware,
1817 SPIDER_NET_FIRMWARE_NAME, &card->pdev->dev) == 0) {
1818 if ( (firmware->size != SPIDER_NET_FIRMWARE_LEN) &&
1819 netif_msg_probe(card) ) {
1820 pr_err("Incorrect size of spidernet firmware in " \
1821 "filesystem. Looking in host firmware...\n");
1822 goto try_host_fw;
1824 err = spider_net_download_firmware(card, firmware->data);
1826 release_firmware(firmware);
1827 if (err)
1828 goto try_host_fw;
1830 goto done;
1833 try_host_fw:
1834 dn = pci_device_to_OF_node(card->pdev);
1835 if (!dn)
1836 goto out_err;
1838 fw_prop = get_property(dn, "firmware", &fw_size);
1839 if (!fw_prop)
1840 goto out_err;
1842 if ( (fw_size != SPIDER_NET_FIRMWARE_LEN) &&
1843 netif_msg_probe(card) ) {
1844 pr_err("Incorrect size of spidernet firmware in " \
1845 "host firmware\n");
1846 goto done;
1849 err = spider_net_download_firmware(card, fw_prop);
1851 done:
1852 return err;
1853 out_err:
1854 if (netif_msg_probe(card))
1855 pr_err("Couldn't find spidernet firmware in filesystem " \
1856 "or host firmware\n");
1857 return err;
1861 * spider_net_workaround_rxramfull - work around firmware bug
1862 * @card: card structure
1864 * no return value
1866 static void
1867 spider_net_workaround_rxramfull(struct spider_net_card *card)
1869 int i, sequencer = 0;
1871 /* cancel reset */
1872 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1873 SPIDER_NET_CKRCTRL_RUN_VALUE);
1875 /* empty sequencer data */
1876 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1877 sequencer++) {
1878 spider_net_write_reg(card, SPIDER_NET_GSnPRGADR +
1879 sequencer * 8, 0x0);
1880 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
1881 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1882 sequencer * 8, 0x0);
1886 /* set sequencer operation */
1887 spider_net_write_reg(card, SPIDER_NET_GSINIT, 0x000000fe);
1889 /* reset */
1890 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1891 SPIDER_NET_CKRCTRL_STOP_VALUE);
1895 * spider_net_stop - called upon ifconfig down
1896 * @netdev: interface device structure
1898 * always returns 0
1901 spider_net_stop(struct net_device *netdev)
1903 struct spider_net_card *card = netdev_priv(netdev);
1905 tasklet_kill(&card->rxram_full_tl);
1906 netif_poll_disable(netdev);
1907 netif_carrier_off(netdev);
1908 netif_stop_queue(netdev);
1909 del_timer_sync(&card->tx_timer);
1911 /* disable/mask all interrupts */
1912 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, 0);
1913 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK, 0);
1914 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK, 0);
1916 /* free_irq(netdev->irq, netdev);*/
1917 free_irq(to_pci_dev(netdev->class_dev.dev)->irq, netdev);
1919 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
1920 SPIDER_NET_DMA_TX_FEND_VALUE);
1922 /* turn off DMA, force end */
1923 spider_net_disable_rxdmac(card);
1925 /* release chains */
1926 spider_net_release_tx_chain(card, 1);
1928 spider_net_free_chain(card, &card->tx_chain);
1929 spider_net_free_chain(card, &card->rx_chain);
1931 return 0;
1935 * spider_net_tx_timeout_task - task scheduled by the watchdog timeout
1936 * function (to be called not under interrupt status)
1937 * @data: data, is interface device structure
1939 * called as task when tx hangs, resets interface (if interface is up)
1941 static void
1942 spider_net_tx_timeout_task(struct work_struct *work)
1944 struct spider_net_card *card =
1945 container_of(work, struct spider_net_card, tx_timeout_task);
1946 struct net_device *netdev = card->netdev;
1948 if (!(netdev->flags & IFF_UP))
1949 goto out;
1951 netif_device_detach(netdev);
1952 spider_net_stop(netdev);
1954 spider_net_workaround_rxramfull(card);
1955 spider_net_init_card(card);
1957 if (spider_net_setup_phy(card))
1958 goto out;
1959 if (spider_net_init_firmware(card))
1960 goto out;
1962 spider_net_open(netdev);
1963 spider_net_kick_tx_dma(card);
1964 netif_device_attach(netdev);
1966 out:
1967 atomic_dec(&card->tx_timeout_task_counter);
1971 * spider_net_tx_timeout - called when the tx timeout watchdog kicks in.
1972 * @netdev: interface device structure
1974 * called, if tx hangs. Schedules a task that resets the interface
1976 static void
1977 spider_net_tx_timeout(struct net_device *netdev)
1979 struct spider_net_card *card;
1981 card = netdev_priv(netdev);
1982 atomic_inc(&card->tx_timeout_task_counter);
1983 if (netdev->flags & IFF_UP)
1984 schedule_work(&card->tx_timeout_task);
1985 else
1986 atomic_dec(&card->tx_timeout_task_counter);
1987 card->spider_stats.tx_timeouts++;
1991 * spider_net_setup_netdev_ops - initialization of net_device operations
1992 * @netdev: net_device structure
1994 * fills out function pointers in the net_device structure
1996 static void
1997 spider_net_setup_netdev_ops(struct net_device *netdev)
1999 netdev->open = &spider_net_open;
2000 netdev->stop = &spider_net_stop;
2001 netdev->hard_start_xmit = &spider_net_xmit;
2002 netdev->get_stats = &spider_net_get_stats;
2003 netdev->set_multicast_list = &spider_net_set_multi;
2004 netdev->set_mac_address = &spider_net_set_mac;
2005 netdev->change_mtu = &spider_net_change_mtu;
2006 netdev->do_ioctl = &spider_net_do_ioctl;
2007 /* tx watchdog */
2008 netdev->tx_timeout = &spider_net_tx_timeout;
2009 netdev->watchdog_timeo = SPIDER_NET_WATCHDOG_TIMEOUT;
2010 /* NAPI */
2011 netdev->poll = &spider_net_poll;
2012 netdev->weight = SPIDER_NET_NAPI_WEIGHT;
2013 /* HW VLAN */
2014 netdev->vlan_rx_register = &spider_net_vlan_rx_reg;
2015 netdev->vlan_rx_add_vid = &spider_net_vlan_rx_add;
2016 netdev->vlan_rx_kill_vid = &spider_net_vlan_rx_kill;
2017 #ifdef CONFIG_NET_POLL_CONTROLLER
2018 /* poll controller */
2019 netdev->poll_controller = &spider_net_poll_controller;
2020 #endif /* CONFIG_NET_POLL_CONTROLLER */
2021 /* ethtool ops */
2022 netdev->ethtool_ops = &spider_net_ethtool_ops;
2026 * spider_net_setup_netdev - initialization of net_device
2027 * @card: card structure
2029 * Returns 0 on success or <0 on failure
2031 * spider_net_setup_netdev initializes the net_device structure
2033 static int
2034 spider_net_setup_netdev(struct spider_net_card *card)
2036 int result;
2037 struct net_device *netdev = card->netdev;
2038 struct device_node *dn;
2039 struct sockaddr addr;
2040 const u8 *mac;
2042 SET_MODULE_OWNER(netdev);
2043 SET_NETDEV_DEV(netdev, &card->pdev->dev);
2045 pci_set_drvdata(card->pdev, netdev);
2047 card->rxram_full_tl.data = (unsigned long) card;
2048 card->rxram_full_tl.func =
2049 (void (*)(unsigned long)) spider_net_handle_rxram_full;
2050 init_timer(&card->tx_timer);
2051 card->tx_timer.function =
2052 (void (*)(unsigned long)) spider_net_cleanup_tx_ring;
2053 card->tx_timer.data = (unsigned long) card;
2054 netdev->irq = card->pdev->irq;
2056 card->options.rx_csum = SPIDER_NET_RX_CSUM_DEFAULT;
2058 card->num_tx_desc = tx_descriptors;
2059 card->num_rx_desc = rx_descriptors;
2061 spider_net_setup_netdev_ops(netdev);
2063 netdev->features = NETIF_F_HW_CSUM | NETIF_F_LLTX;
2064 /* some time: NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
2065 * NETIF_F_HW_VLAN_FILTER */
2067 netdev->irq = card->pdev->irq;
2069 dn = pci_device_to_OF_node(card->pdev);
2070 if (!dn)
2071 return -EIO;
2073 mac = get_property(dn, "local-mac-address", NULL);
2074 if (!mac)
2075 return -EIO;
2076 memcpy(addr.sa_data, mac, ETH_ALEN);
2078 result = spider_net_set_mac(netdev, &addr);
2079 if ((result) && (netif_msg_probe(card)))
2080 pr_err("Failed to set MAC address: %i\n", result);
2082 result = register_netdev(netdev);
2083 if (result) {
2084 if (netif_msg_probe(card))
2085 pr_err("Couldn't register net_device: %i\n",
2086 result);
2087 return result;
2090 if (netif_msg_probe(card))
2091 pr_info("Initialized device %s.\n", netdev->name);
2093 return 0;
2097 * spider_net_alloc_card - allocates net_device and card structure
2099 * returns the card structure or NULL in case of errors
2101 * the card and net_device structures are linked to each other
2103 static struct spider_net_card *
2104 spider_net_alloc_card(void)
2106 struct net_device *netdev;
2107 struct spider_net_card *card;
2108 size_t alloc_size;
2110 alloc_size = sizeof (*card) +
2111 sizeof (struct spider_net_descr) * rx_descriptors +
2112 sizeof (struct spider_net_descr) * tx_descriptors;
2113 netdev = alloc_etherdev(alloc_size);
2114 if (!netdev)
2115 return NULL;
2117 card = netdev_priv(netdev);
2118 card->netdev = netdev;
2119 card->msg_enable = SPIDER_NET_DEFAULT_MSG;
2120 INIT_WORK(&card->tx_timeout_task, spider_net_tx_timeout_task);
2121 init_waitqueue_head(&card->waitq);
2122 atomic_set(&card->tx_timeout_task_counter, 0);
2124 return card;
2128 * spider_net_undo_pci_setup - releases PCI ressources
2129 * @card: card structure
2131 * spider_net_undo_pci_setup releases the mapped regions
2133 static void
2134 spider_net_undo_pci_setup(struct spider_net_card *card)
2136 iounmap(card->regs);
2137 pci_release_regions(card->pdev);
2141 * spider_net_setup_pci_dev - sets up the device in terms of PCI operations
2142 * @card: card structure
2143 * @pdev: PCI device
2145 * Returns the card structure or NULL if any errors occur
2147 * spider_net_setup_pci_dev initializes pdev and together with the
2148 * functions called in spider_net_open configures the device so that
2149 * data can be transferred over it
2150 * The net_device structure is attached to the card structure, if the
2151 * function returns without error.
2153 static struct spider_net_card *
2154 spider_net_setup_pci_dev(struct pci_dev *pdev)
2156 struct spider_net_card *card;
2157 unsigned long mmio_start, mmio_len;
2159 if (pci_enable_device(pdev)) {
2160 pr_err("Couldn't enable PCI device\n");
2161 return NULL;
2164 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
2165 pr_err("Couldn't find proper PCI device base address.\n");
2166 goto out_disable_dev;
2169 if (pci_request_regions(pdev, spider_net_driver_name)) {
2170 pr_err("Couldn't obtain PCI resources, aborting.\n");
2171 goto out_disable_dev;
2174 pci_set_master(pdev);
2176 card = spider_net_alloc_card();
2177 if (!card) {
2178 pr_err("Couldn't allocate net_device structure, "
2179 "aborting.\n");
2180 goto out_release_regions;
2182 card->pdev = pdev;
2184 /* fetch base address and length of first resource */
2185 mmio_start = pci_resource_start(pdev, 0);
2186 mmio_len = pci_resource_len(pdev, 0);
2188 card->netdev->mem_start = mmio_start;
2189 card->netdev->mem_end = mmio_start + mmio_len;
2190 card->regs = ioremap(mmio_start, mmio_len);
2192 if (!card->regs) {
2193 pr_err("Couldn't obtain PCI resources, aborting.\n");
2194 goto out_release_regions;
2197 return card;
2199 out_release_regions:
2200 pci_release_regions(pdev);
2201 out_disable_dev:
2202 pci_disable_device(pdev);
2203 pci_set_drvdata(pdev, NULL);
2204 return NULL;
2208 * spider_net_probe - initialization of a device
2209 * @pdev: PCI device
2210 * @ent: entry in the device id list
2212 * Returns 0 on success, <0 on failure
2214 * spider_net_probe initializes pdev and registers a net_device
2215 * structure for it. After that, the device can be ifconfig'ed up
2217 static int __devinit
2218 spider_net_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2220 int err = -EIO;
2221 struct spider_net_card *card;
2223 card = spider_net_setup_pci_dev(pdev);
2224 if (!card)
2225 goto out;
2227 spider_net_workaround_rxramfull(card);
2228 spider_net_init_card(card);
2230 err = spider_net_setup_phy(card);
2231 if (err)
2232 goto out_undo_pci;
2234 err = spider_net_init_firmware(card);
2235 if (err)
2236 goto out_undo_pci;
2238 err = spider_net_setup_netdev(card);
2239 if (err)
2240 goto out_undo_pci;
2242 return 0;
2244 out_undo_pci:
2245 spider_net_undo_pci_setup(card);
2246 free_netdev(card->netdev);
2247 out:
2248 return err;
2252 * spider_net_remove - removal of a device
2253 * @pdev: PCI device
2255 * Returns 0 on success, <0 on failure
2257 * spider_net_remove is called to remove the device and unregisters the
2258 * net_device
2260 static void __devexit
2261 spider_net_remove(struct pci_dev *pdev)
2263 struct net_device *netdev;
2264 struct spider_net_card *card;
2266 netdev = pci_get_drvdata(pdev);
2267 card = netdev_priv(netdev);
2269 wait_event(card->waitq,
2270 atomic_read(&card->tx_timeout_task_counter) == 0);
2272 unregister_netdev(netdev);
2274 /* switch off card */
2275 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2276 SPIDER_NET_CKRCTRL_STOP_VALUE);
2277 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2278 SPIDER_NET_CKRCTRL_RUN_VALUE);
2280 spider_net_undo_pci_setup(card);
2281 free_netdev(netdev);
2284 static struct pci_driver spider_net_driver = {
2285 .name = spider_net_driver_name,
2286 .id_table = spider_net_pci_tbl,
2287 .probe = spider_net_probe,
2288 .remove = __devexit_p(spider_net_remove)
2292 * spider_net_init - init function when the driver is loaded
2294 * spider_net_init registers the device driver
2296 static int __init spider_net_init(void)
2298 printk(KERN_INFO "Spidernet version %s.\n", VERSION);
2300 if (rx_descriptors < SPIDER_NET_RX_DESCRIPTORS_MIN) {
2301 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MIN;
2302 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2304 if (rx_descriptors > SPIDER_NET_RX_DESCRIPTORS_MAX) {
2305 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MAX;
2306 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2308 if (tx_descriptors < SPIDER_NET_TX_DESCRIPTORS_MIN) {
2309 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MIN;
2310 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2312 if (tx_descriptors > SPIDER_NET_TX_DESCRIPTORS_MAX) {
2313 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MAX;
2314 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2317 return pci_register_driver(&spider_net_driver);
2321 * spider_net_cleanup - exit function when driver is unloaded
2323 * spider_net_cleanup unregisters the device driver
2325 static void __exit spider_net_cleanup(void)
2327 pci_unregister_driver(&spider_net_driver);
2330 module_init(spider_net_init);
2331 module_exit(spider_net_cleanup);