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)
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>
32 #include <linux/init.h>
33 #include <linux/ioport.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 " \
68 MODULE_PARM_DESC(tx_descriptors, "number of descriptors used " \
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 },
79 MODULE_DEVICE_TABLE(pci, spider_net_pci_tbl);
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.
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);
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
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
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).
125 spider_net_write_phy(struct net_device *netdev, int mii_id,
128 struct spider_net_card *card = netdev_priv(netdev);
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
142 * Returns value read from PHY register
144 * spider_net_write_phy reads from an arbitrary PHY
145 * register via the spider GPCROPCMD register
148 spider_net_read_phy(struct net_device *netdev, int mii_id, int reg)
150 struct spider_net_card *card = netdev_priv(netdev);
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 */
160 readvalue = spider_net_read_reg(card, SPIDER_NET_GPCROPCMD);
161 } while (readvalue & SPIDER_NET_GPREXEC);
163 readvalue &= SPIDER_NET_GPRDAT_MASK;
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
175 spider_net_rx_irq_off(struct spider_net_card *card)
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
190 spider_net_rx_irq_on(struct spider_net_card *card)
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
206 spider_net_set_promisc(struct spider_net_card *card)
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);
218 macu = netdev->dev_addr[0];
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
238 spider_net_get_mac_address(struct net_device *netdev)
240 struct spider_net_card *card = netdev_priv(netdev);
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]))
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
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
278 spider_net_free_chain(struct spider_net_card *card,
279 struct spider_net_descr_chain *chain)
281 struct spider_net_descr *descr;
286 descr->next_descr_addr = 0;
288 } while (descr != chain->ring);
290 dma_free_coherent(&card->pdev->dev, chain->num_desc,
291 chain->ring, chain->dma_addr);
295 * spider_net_init_chain - alloc and link descriptor chain
296 * @card: card structure
297 * @chain: address of chain
299 * We manage a circular list that mirrors the hardware structure,
300 * except that the hardware uses bus addresses.
302 * Returns 0 on success, <0 on failure
305 spider_net_init_chain(struct spider_net_card *card,
306 struct spider_net_descr_chain *chain)
309 struct spider_net_descr *descr;
313 alloc_size = chain->num_desc * sizeof (struct spider_net_descr);
315 chain->ring = dma_alloc_coherent(&card->pdev->dev, alloc_size,
316 &chain->dma_addr, GFP_KERNEL);
322 memset(descr, 0, alloc_size);
324 /* Set up the hardware pointers in each descriptor */
325 buf = chain->dma_addr;
326 for (i=0; i < chain->num_desc; i++, descr++) {
327 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
329 descr->bus_addr = buf;
330 descr->next_descr_addr = 0;
331 descr->next = descr + 1;
332 descr->prev = descr - 1;
334 buf += sizeof(struct spider_net_descr);
336 /* do actual circular list */
337 (descr-1)->next = chain->ring;
338 chain->ring->prev = descr-1;
340 spin_lock_init(&chain->lock);
341 chain->head = chain->ring;
342 chain->tail = chain->ring;
347 * spider_net_free_rx_chain_contents - frees descr contents in rx chain
348 * @card: card structure
350 * returns 0 on success, <0 on failure
353 spider_net_free_rx_chain_contents(struct spider_net_card *card)
355 struct spider_net_descr *descr;
357 descr = card->rx_chain.head;
360 dev_kfree_skb(descr->skb);
361 pci_unmap_single(card->pdev, descr->buf_addr,
362 SPIDER_NET_MAX_FRAME,
363 PCI_DMA_BIDIRECTIONAL);
366 } while (descr != card->rx_chain.head);
370 * spider_net_prepare_rx_descr - Reinitialize RX descriptor
371 * @card: card structure
372 * @descr: descriptor to re-init
374 * Return 0 on succes, <0 on failure.
376 * Allocates a new rx skb, iommu-maps it and attaches it to the
377 * descriptor. Mark the descriptor as activated, ready-to-use.
380 spider_net_prepare_rx_descr(struct spider_net_card *card,
381 struct spider_net_descr *descr)
387 /* we need to round up the buffer size to a multiple of 128 */
388 bufsize = (SPIDER_NET_MAX_FRAME + SPIDER_NET_RXBUF_ALIGN - 1) &
389 (~(SPIDER_NET_RXBUF_ALIGN - 1));
391 /* and we need to have it 128 byte aligned, therefore we allocate a
393 /* allocate an skb */
394 descr->skb = dev_alloc_skb(bufsize + SPIDER_NET_RXBUF_ALIGN - 1);
396 if (netif_msg_rx_err(card) && net_ratelimit())
397 pr_err("Not enough memory to allocate rx buffer\n");
398 card->spider_stats.alloc_rx_skb_error++;
401 descr->buf_size = bufsize;
402 descr->result_size = 0;
403 descr->valid_size = 0;
404 descr->data_status = 0;
405 descr->data_error = 0;
407 offset = ((unsigned long)descr->skb->data) &
408 (SPIDER_NET_RXBUF_ALIGN - 1);
410 skb_reserve(descr->skb, SPIDER_NET_RXBUF_ALIGN - offset);
411 /* iommu-map the skb */
412 buf = pci_map_single(card->pdev, descr->skb->data,
413 SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
414 descr->buf_addr = buf;
415 if (pci_dma_mapping_error(buf)) {
416 dev_kfree_skb_any(descr->skb);
417 if (netif_msg_rx_err(card) && net_ratelimit())
418 pr_err("Could not iommu-map rx buffer\n");
419 card->spider_stats.rx_iommu_map_error++;
420 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
422 descr->next_descr_addr = 0;
424 descr->dmac_cmd_status = SPIDER_NET_DESCR_CARDOWNED |
425 SPIDER_NET_DMAC_NOINTR_COMPLETE;
428 descr->prev->next_descr_addr = descr->bus_addr;
435 * spider_net_enable_rxchtails - sets RX dmac chain tail addresses
436 * @card: card structure
438 * spider_net_enable_rxchtails sets the RX DMAC chain tail adresses in the
439 * chip by writing to the appropriate register. DMA is enabled in
440 * spider_net_enable_rxdmac.
443 spider_net_enable_rxchtails(struct spider_net_card *card)
445 /* assume chain is aligned correctly */
446 spider_net_write_reg(card, SPIDER_NET_GDADCHA ,
447 card->rx_chain.tail->bus_addr);
451 * spider_net_enable_rxdmac - enables a receive DMA controller
452 * @card: card structure
454 * spider_net_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
455 * in the GDADMACCNTR register
458 spider_net_enable_rxdmac(struct spider_net_card *card)
461 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
462 SPIDER_NET_DMA_RX_VALUE);
466 * spider_net_refill_rx_chain - refills descriptors/skbs in the rx chains
467 * @card: card structure
469 * refills descriptors in the rx chain: allocates skbs and iommu-maps them.
472 spider_net_refill_rx_chain(struct spider_net_card *card)
474 struct spider_net_descr_chain *chain = &card->rx_chain;
477 /* one context doing the refill (and a second context seeing that
478 * and omitting it) is ok. If called by NAPI, we'll be called again
479 * as spider_net_decode_one_descr is called several times. If some
480 * interrupt calls us, the NAPI is about to clean up anyway. */
481 if (!spin_trylock_irqsave(&chain->lock, flags))
484 while (spider_net_get_descr_status(chain->head) ==
485 SPIDER_NET_DESCR_NOT_IN_USE) {
486 if (spider_net_prepare_rx_descr(card, chain->head))
488 chain->head = chain->head->next;
491 spin_unlock_irqrestore(&chain->lock, flags);
495 * spider_net_alloc_rx_skbs - Allocates rx skbs in rx descriptor chains
496 * @card: card structure
498 * Returns 0 on success, <0 on failure.
501 spider_net_alloc_rx_skbs(struct spider_net_card *card)
504 struct spider_net_descr_chain *chain;
508 chain = &card->rx_chain;
509 /* Put at least one buffer into the chain. if this fails,
510 * we've got a problem. If not, spider_net_refill_rx_chain
511 * will do the rest at the end of this function. */
512 if (spider_net_prepare_rx_descr(card, chain->head))
515 chain->head = chain->head->next;
517 /* This will allocate the rest of the rx buffers;
518 * if not, it's business as usual later on. */
519 spider_net_refill_rx_chain(card);
520 spider_net_enable_rxdmac(card);
524 spider_net_free_rx_chain_contents(card);
529 * spider_net_get_multicast_hash - generates hash for multicast filter table
530 * @addr: multicast address
532 * returns the hash value.
534 * spider_net_get_multicast_hash calculates a hash value for a given multicast
535 * address, that is used to set the multicast filter tables
538 spider_net_get_multicast_hash(struct net_device *netdev, __u8 *addr)
542 char addr_for_crc[ETH_ALEN] = { 0, };
545 for (i = 0; i < ETH_ALEN * 8; i++) {
546 bit = (addr[i / 8] >> (i % 8)) & 1;
547 addr_for_crc[ETH_ALEN - 1 - i / 8] += bit << (7 - (i % 8));
550 crc = crc32_be(~0, addr_for_crc, netdev->addr_len);
561 * spider_net_set_multi - sets multicast addresses and promisc flags
562 * @netdev: interface device structure
564 * spider_net_set_multi configures multicast addresses as needed for the
565 * netdev interface. It also sets up multicast, allmulti and promisc
566 * flags appropriately
569 spider_net_set_multi(struct net_device *netdev)
571 struct dev_mc_list *mc;
575 struct spider_net_card *card = netdev_priv(netdev);
576 unsigned long bitmask[SPIDER_NET_MULTICAST_HASHES / BITS_PER_LONG] =
579 spider_net_set_promisc(card);
581 if (netdev->flags & IFF_ALLMULTI) {
582 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES; i++) {
588 /* well, we know, what the broadcast hash value is: it's xfd
589 hash = spider_net_get_multicast_hash(netdev, netdev->broadcast); */
590 set_bit(0xfd, bitmask);
592 for (mc = netdev->mc_list; mc; mc = mc->next) {
593 hash = spider_net_get_multicast_hash(netdev, mc->dmi_addr);
594 set_bit(hash, bitmask);
598 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES / 4; i++) {
600 if (test_bit(i * 4, bitmask))
603 if (test_bit(i * 4 + 1, bitmask))
606 if (test_bit(i * 4 + 2, bitmask))
609 if (test_bit(i * 4 + 3, bitmask))
612 spider_net_write_reg(card, SPIDER_NET_GMRMHFILnR + i * 4, reg);
617 * spider_net_disable_rxdmac - disables the receive DMA controller
618 * @card: card structure
620 * spider_net_disable_rxdmac terminates processing on the DMA controller by
621 * turing off DMA and issueing a force end
624 spider_net_disable_rxdmac(struct spider_net_card *card)
626 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
627 SPIDER_NET_DMA_RX_FEND_VALUE);
631 * spider_net_prepare_tx_descr - fill tx descriptor with skb data
632 * @card: card structure
633 * @descr: descriptor structure to fill out
634 * @skb: packet to use
636 * returns 0 on success, <0 on failure.
638 * fills out the descriptor structure with skb data and len. Copies data,
639 * if needed (32bit DMA!)
642 spider_net_prepare_tx_descr(struct spider_net_card *card,
645 struct spider_net_descr *descr;
649 buf = pci_map_single(card->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
650 if (pci_dma_mapping_error(buf)) {
651 if (netif_msg_tx_err(card) && net_ratelimit())
652 pr_err("could not iommu-map packet (%p, %i). "
653 "Dropping packet\n", skb->data, skb->len);
654 card->spider_stats.tx_iommu_map_error++;
658 spin_lock_irqsave(&card->tx_chain.lock, flags);
659 descr = card->tx_chain.head;
660 card->tx_chain.head = descr->next;
662 descr->buf_addr = buf;
663 descr->buf_size = skb->len;
664 descr->next_descr_addr = 0;
666 descr->data_status = 0;
668 descr->dmac_cmd_status =
669 SPIDER_NET_DESCR_CARDOWNED | SPIDER_NET_DMAC_NOCS;
670 spin_unlock_irqrestore(&card->tx_chain.lock, flags);
672 if (skb->protocol == htons(ETH_P_IP))
673 switch (skb->nh.iph->protocol) {
675 descr->dmac_cmd_status |= SPIDER_NET_DMAC_TCP;
678 descr->dmac_cmd_status |= SPIDER_NET_DMAC_UDP;
682 /* Chain the bus address, so that the DMA engine finds this descr. */
683 descr->prev->next_descr_addr = descr->bus_addr;
685 card->netdev->trans_start = jiffies; /* set netdev watchdog timer */
690 spider_net_set_low_watermark(struct spider_net_card *card)
696 struct spider_net_descr *descr = card->tx_chain.tail;
698 /* Measure the length of the queue. Measurement does not
699 * need to be precise -- does not need a lock. */
700 while (descr != card->tx_chain.head) {
701 status = descr->dmac_cmd_status & SPIDER_NET_DESCR_NOT_IN_USE;
702 if (status == SPIDER_NET_DESCR_NOT_IN_USE)
708 /* If TX queue is short, don't even bother with interrupts */
709 if (cnt < card->tx_chain.num_desc/4)
712 /* Set low-watermark 3/4th's of the way into the queue. */
713 descr = card->tx_chain.tail;
718 /* Set the new watermark, clear the old watermark */
719 spin_lock_irqsave(&card->tx_chain.lock, flags);
720 descr->dmac_cmd_status |= SPIDER_NET_DESCR_TXDESFLG;
721 if (card->low_watermark && card->low_watermark != descr)
722 card->low_watermark->dmac_cmd_status =
723 card->low_watermark->dmac_cmd_status & ~SPIDER_NET_DESCR_TXDESFLG;
724 card->low_watermark = descr;
725 spin_unlock_irqrestore(&card->tx_chain.lock, flags);
730 * spider_net_release_tx_chain - processes sent tx descriptors
731 * @card: adapter structure
732 * @brutal: if set, don't care about whether descriptor seems to be in use
734 * returns 0 if the tx ring is empty, otherwise 1.
736 * spider_net_release_tx_chain releases the tx descriptors that spider has
737 * finished with (if non-brutal) or simply release tx descriptors (if brutal).
738 * If some other context is calling this function, we return 1 so that we're
739 * scheduled again (if we were scheduled) and will not loose initiative.
742 spider_net_release_tx_chain(struct spider_net_card *card, int brutal)
744 struct spider_net_descr_chain *chain = &card->tx_chain;
745 struct spider_net_descr *descr;
751 while (chain->tail != chain->head) {
752 spin_lock_irqsave(&chain->lock, flags);
755 status = spider_net_get_descr_status(descr);
757 case SPIDER_NET_DESCR_COMPLETE:
758 card->netdev_stats.tx_packets++;
759 card->netdev_stats.tx_bytes += descr->skb->len;
762 case SPIDER_NET_DESCR_CARDOWNED:
764 spin_unlock_irqrestore(&chain->lock, flags);
768 /* fallthrough, if we release the descriptors
769 * brutally (then we don't care about
770 * SPIDER_NET_DESCR_CARDOWNED) */
772 case SPIDER_NET_DESCR_RESPONSE_ERROR:
773 case SPIDER_NET_DESCR_PROTECTION_ERROR:
774 case SPIDER_NET_DESCR_FORCE_END:
775 if (netif_msg_tx_err(card))
776 pr_err("%s: forcing end of tx descriptor "
777 "with status x%02x\n",
778 card->netdev->name, status);
779 card->netdev_stats.tx_errors++;
783 card->netdev_stats.tx_dropped++;
785 spin_unlock_irqrestore(&chain->lock, flags);
790 chain->tail = descr->next;
791 descr->dmac_cmd_status |= SPIDER_NET_DESCR_NOT_IN_USE;
793 buf_addr = descr->buf_addr;
794 spin_unlock_irqrestore(&chain->lock, flags);
798 pci_unmap_single(card->pdev, buf_addr, skb->len,
807 * spider_net_kick_tx_dma - enables TX DMA processing
808 * @card: card structure
809 * @descr: descriptor address to enable TX processing at
811 * This routine will start the transmit DMA running if
812 * it is not already running. This routine ned only be
813 * called when queueing a new packet to an empty tx queue.
814 * Writes the current tx chain head as start address
815 * of the tx descriptor chain and enables the transmission
819 spider_net_kick_tx_dma(struct spider_net_card *card)
821 struct spider_net_descr *descr;
823 if (spider_net_read_reg(card, SPIDER_NET_GDTDMACCNTR) &
824 SPIDER_NET_TX_DMA_EN)
827 descr = card->tx_chain.tail;
829 if (spider_net_get_descr_status(descr) ==
830 SPIDER_NET_DESCR_CARDOWNED) {
831 spider_net_write_reg(card, SPIDER_NET_GDTDCHA,
833 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
834 SPIDER_NET_DMA_TX_VALUE);
837 if (descr == card->tx_chain.head)
843 mod_timer(&card->tx_timer, jiffies + SPIDER_NET_TX_TIMER);
847 * spider_net_xmit - transmits a frame over the device
848 * @skb: packet to send out
849 * @netdev: interface device structure
851 * returns 0 on success, !0 on failure
854 spider_net_xmit(struct sk_buff *skb, struct net_device *netdev)
857 struct spider_net_card *card = netdev_priv(netdev);
858 struct spider_net_descr_chain *chain = &card->tx_chain;
860 spider_net_release_tx_chain(card, 0);
862 if ((chain->head->next == chain->tail->prev) ||
863 (spider_net_prepare_tx_descr(card, skb) != 0)) {
865 card->netdev_stats.tx_dropped++;
866 netif_stop_queue(netdev);
867 return NETDEV_TX_BUSY;
870 cnt = spider_net_set_low_watermark(card);
872 spider_net_kick_tx_dma(card);
877 * spider_net_cleanup_tx_ring - cleans up the TX ring
878 * @card: card structure
880 * spider_net_cleanup_tx_ring is called by either the tx_timer
881 * or from the NAPI polling routine.
882 * This routine releases resources associted with transmitted
883 * packets, including updating the queue tail pointer.
886 spider_net_cleanup_tx_ring(struct spider_net_card *card)
888 if ((spider_net_release_tx_chain(card, 0) != 0) &&
889 (card->netdev->flags & IFF_UP)) {
890 spider_net_kick_tx_dma(card);
891 netif_wake_queue(card->netdev);
896 * spider_net_do_ioctl - called for device ioctls
897 * @netdev: interface device structure
898 * @ifr: request parameter structure for ioctl
899 * @cmd: command code for ioctl
901 * returns 0 on success, <0 on failure. Currently, we have no special ioctls.
902 * -EOPNOTSUPP is returned, if an unknown ioctl was requested
905 spider_net_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
914 * spider_net_pass_skb_up - takes an skb from a descriptor and passes it on
915 * @descr: descriptor to process
916 * @card: card structure
918 * Fills out skb structure and passes the data to the stack.
919 * The descriptor state is not changed.
922 spider_net_pass_skb_up(struct spider_net_descr *descr,
923 struct spider_net_card *card)
926 struct net_device *netdev;
927 u32 data_status, data_error;
929 data_status = descr->data_status;
930 data_error = descr->data_error;
931 netdev = card->netdev;
935 skb_put(skb, descr->valid_size);
937 /* the card seems to add 2 bytes of junk in front
938 * of the ethernet frame */
939 #define SPIDER_MISALIGN 2
940 skb_pull(skb, SPIDER_MISALIGN);
941 skb->protocol = eth_type_trans(skb, netdev);
943 /* checksum offload */
944 if (card->options.rx_csum) {
945 if ( ( (data_status & SPIDER_NET_DATA_STATUS_CKSUM_MASK) ==
946 SPIDER_NET_DATA_STATUS_CKSUM_MASK) &&
947 !(data_error & SPIDER_NET_DATA_ERR_CKSUM_MASK))
948 skb->ip_summed = CHECKSUM_UNNECESSARY;
950 skb->ip_summed = CHECKSUM_NONE;
952 skb->ip_summed = CHECKSUM_NONE;
954 if (data_status & SPIDER_NET_VLAN_PACKET) {
955 /* further enhancements: HW-accel VLAN
956 * vlan_hwaccel_receive_skb
960 /* pass skb up to stack */
961 netif_receive_skb(skb);
963 /* update netdevice statistics */
964 card->netdev_stats.rx_packets++;
965 card->netdev_stats.rx_bytes += skb->len;
969 static void show_rx_chain(struct spider_net_card *card)
971 struct spider_net_descr_chain *chain = &card->rx_chain;
972 struct spider_net_descr *start= chain->tail;
973 struct spider_net_descr *descr= start;
977 int cstat = spider_net_get_descr_status(descr);
978 printk(KERN_INFO "RX chain tail at descr=%ld\n",
979 (start - card->descr) - card->tx_chain.num_desc);
983 status = spider_net_get_descr_status(descr);
984 if (cstat != status) {
985 printk(KERN_INFO "Have %d descrs with stat=x%08x\n", cnt, cstat);
991 } while (descr != start);
992 printk(KERN_INFO "Last %d descrs with stat=x%08x\n", cnt, cstat);
997 * spider_net_decode_one_descr - processes an rx descriptor
998 * @card: card structure
1000 * Returns 1 if a packet has been sent to the stack, otherwise 0
1002 * Processes an rx descriptor by iommu-unmapping the data buffer and passing
1003 * the packet up to the stack. This function is called in softirq
1004 * context, e.g. either bottom half from interrupt or NAPI polling context
1007 spider_net_decode_one_descr(struct spider_net_card *card)
1009 struct spider_net_descr_chain *chain = &card->rx_chain;
1010 struct spider_net_descr *descr = chain->tail;
1013 status = spider_net_get_descr_status(descr);
1015 /* Nothing in the descriptor, or ring must be empty */
1016 if ((status == SPIDER_NET_DESCR_CARDOWNED) ||
1017 (status == SPIDER_NET_DESCR_NOT_IN_USE))
1020 /* descriptor definitively used -- move on tail */
1021 chain->tail = descr->next;
1023 /* unmap descriptor */
1024 pci_unmap_single(card->pdev, descr->buf_addr,
1025 SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
1027 if ( (status == SPIDER_NET_DESCR_RESPONSE_ERROR) ||
1028 (status == SPIDER_NET_DESCR_PROTECTION_ERROR) ||
1029 (status == SPIDER_NET_DESCR_FORCE_END) ) {
1030 if (netif_msg_rx_err(card))
1031 pr_err("%s: dropping RX descriptor with state %d\n",
1032 card->netdev->name, status);
1033 card->netdev_stats.rx_dropped++;
1037 if ( (status != SPIDER_NET_DESCR_COMPLETE) &&
1038 (status != SPIDER_NET_DESCR_FRAME_END) ) {
1039 if (netif_msg_rx_err(card))
1040 pr_err("%s: RX descriptor with unkown state %d\n",
1041 card->netdev->name, status);
1042 card->spider_stats.rx_desc_unk_state++;
1046 /* The cases we'll throw away the packet immediately */
1047 if (descr->data_error & SPIDER_NET_DESTROY_RX_FLAGS) {
1048 if (netif_msg_rx_err(card))
1049 pr_err("%s: error in received descriptor found, "
1050 "data_status=x%08x, data_error=x%08x\n",
1052 descr->data_status, descr->data_error);
1056 if (descr->dmac_cmd_status & 0xfefe) {
1057 pr_err("%s: bad status, cmd_status=x%08x\n",
1059 descr->dmac_cmd_status);
1060 pr_err("buf_addr=x%08x\n", descr->buf_addr);
1061 pr_err("buf_size=x%08x\n", descr->buf_size);
1062 pr_err("next_descr_addr=x%08x\n", descr->next_descr_addr);
1063 pr_err("result_size=x%08x\n", descr->result_size);
1064 pr_err("valid_size=x%08x\n", descr->valid_size);
1065 pr_err("data_status=x%08x\n", descr->data_status);
1066 pr_err("data_error=x%08x\n", descr->data_error);
1067 pr_err("bus_addr=x%08x\n", descr->bus_addr);
1068 pr_err("which=%ld\n", descr - card->rx_chain.ring);
1070 card->spider_stats.rx_desc_error++;
1074 /* Ok, we've got a packet in descr */
1075 spider_net_pass_skb_up(descr, card);
1076 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
1080 dev_kfree_skb_irq(descr->skb);
1081 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
1086 * spider_net_poll - NAPI poll function called by the stack to return packets
1087 * @netdev: interface device structure
1088 * @budget: number of packets we can pass to the stack at most
1090 * returns 0 if no more packets available to the driver/stack. Returns 1,
1091 * if the quota is exceeded, but the driver has still packets.
1093 * spider_net_poll returns all packets from the rx descriptors to the stack
1094 * (using netif_receive_skb). If all/enough packets are up, the driver
1095 * reenables interrupts and returns 0. If not, 1 is returned.
1098 spider_net_poll(struct net_device *netdev, int *budget)
1100 struct spider_net_card *card = netdev_priv(netdev);
1101 int packets_to_do, packets_done = 0;
1102 int no_more_packets = 0;
1104 spider_net_cleanup_tx_ring(card);
1105 packets_to_do = min(*budget, netdev->quota);
1107 while (packets_to_do) {
1108 if (spider_net_decode_one_descr(card)) {
1112 /* no more packets for the stack */
1113 no_more_packets = 1;
1118 netdev->quota -= packets_done;
1119 *budget -= packets_done;
1120 spider_net_refill_rx_chain(card);
1121 spider_net_enable_rxdmac(card);
1123 /* if all packets are in the stack, enable interrupts and return 0 */
1124 /* if not, return 1 */
1125 if (no_more_packets) {
1126 netif_rx_complete(netdev);
1127 spider_net_rx_irq_on(card);
1135 * spider_net_vlan_rx_reg - initializes VLAN structures in the driver and card
1136 * @netdev: interface device structure
1137 * @grp: vlan_group structure that is registered (NULL on destroying interface)
1140 spider_net_vlan_rx_reg(struct net_device *netdev, struct vlan_group *grp)
1142 /* further enhancement... yet to do */
1147 * spider_net_vlan_rx_add - adds VLAN id to the card filter
1148 * @netdev: interface device structure
1149 * @vid: VLAN id to add
1152 spider_net_vlan_rx_add(struct net_device *netdev, uint16_t vid)
1154 /* further enhancement... yet to do */
1155 /* add vid to card's VLAN filter table */
1160 * spider_net_vlan_rx_kill - removes VLAN id to the card filter
1161 * @netdev: interface device structure
1162 * @vid: VLAN id to remove
1165 spider_net_vlan_rx_kill(struct net_device *netdev, uint16_t vid)
1167 /* further enhancement... yet to do */
1168 /* remove vid from card's VLAN filter table */
1172 * spider_net_get_stats - get interface statistics
1173 * @netdev: interface device structure
1175 * returns the interface statistics residing in the spider_net_card struct
1177 static struct net_device_stats *
1178 spider_net_get_stats(struct net_device *netdev)
1180 struct spider_net_card *card = netdev_priv(netdev);
1181 struct net_device_stats *stats = &card->netdev_stats;
1186 * spider_net_change_mtu - changes the MTU of an interface
1187 * @netdev: interface device structure
1188 * @new_mtu: new MTU value
1190 * returns 0 on success, <0 on failure
1193 spider_net_change_mtu(struct net_device *netdev, int new_mtu)
1195 /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
1196 * and mtu is outbound only anyway */
1197 if ( (new_mtu < SPIDER_NET_MIN_MTU ) ||
1198 (new_mtu > SPIDER_NET_MAX_MTU) )
1200 netdev->mtu = new_mtu;
1205 * spider_net_set_mac - sets the MAC of an interface
1206 * @netdev: interface device structure
1207 * @ptr: pointer to new MAC address
1209 * Returns 0 on success, <0 on failure. Currently, we don't support this
1210 * and will always return EOPNOTSUPP.
1213 spider_net_set_mac(struct net_device *netdev, void *p)
1215 struct spider_net_card *card = netdev_priv(netdev);
1216 u32 macl, macu, regvalue;
1217 struct sockaddr *addr = p;
1219 if (!is_valid_ether_addr(addr->sa_data))
1220 return -EADDRNOTAVAIL;
1222 /* switch off GMACTPE and GMACRPE */
1223 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1224 regvalue &= ~((1 << 5) | (1 << 6));
1225 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1228 macu = (addr->sa_data[0]<<24) + (addr->sa_data[1]<<16) +
1229 (addr->sa_data[2]<<8) + (addr->sa_data[3]);
1230 macl = (addr->sa_data[4]<<8) + (addr->sa_data[5]);
1231 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACU, macu);
1232 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACL, macl);
1234 /* switch GMACTPE and GMACRPE back on */
1235 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1236 regvalue |= ((1 << 5) | (1 << 6));
1237 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1239 spider_net_set_promisc(card);
1241 /* look up, whether we have been successful */
1242 if (spider_net_get_mac_address(netdev))
1243 return -EADDRNOTAVAIL;
1244 if (memcmp(netdev->dev_addr,addr->sa_data,netdev->addr_len))
1245 return -EADDRNOTAVAIL;
1251 * spider_net_handle_error_irq - handles errors raised by an interrupt
1252 * @card: card structure
1253 * @status_reg: interrupt status register 0 (GHIINT0STS)
1255 * spider_net_handle_error_irq treats or ignores all error conditions
1256 * found when an interrupt is presented
1259 spider_net_handle_error_irq(struct spider_net_card *card, u32 status_reg)
1261 u32 error_reg1, error_reg2;
1265 error_reg1 = spider_net_read_reg(card, SPIDER_NET_GHIINT1STS);
1266 error_reg2 = spider_net_read_reg(card, SPIDER_NET_GHIINT2STS);
1268 /* check GHIINT0STS ************************************/
1270 for (i = 0; i < 32; i++)
1271 if (status_reg & (1<<i))
1274 /* let error_reg1 and error_reg2 evaluation decide, what to do
1275 case SPIDER_NET_PHYINT:
1276 case SPIDER_NET_GMAC2INT:
1277 case SPIDER_NET_GMAC1INT:
1278 case SPIDER_NET_GFIFOINT:
1279 case SPIDER_NET_DMACINT:
1280 case SPIDER_NET_GSYSINT:
1283 case SPIDER_NET_GIPSINT:
1287 case SPIDER_NET_GPWOPCMPINT:
1288 /* PHY write operation completed */
1291 case SPIDER_NET_GPROPCMPINT:
1292 /* PHY read operation completed */
1293 /* we don't use semaphores, as we poll for the completion
1294 * of the read operation in spider_net_read_phy. Should take
1298 case SPIDER_NET_GPWFFINT:
1299 /* PHY command queue full */
1300 if (netif_msg_intr(card))
1301 pr_err("PHY write queue full\n");
1305 /* case SPIDER_NET_GRMDADRINT: not used. print a message */
1306 /* case SPIDER_NET_GRMARPINT: not used. print a message */
1307 /* case SPIDER_NET_GRMMPINT: not used. print a message */
1309 case SPIDER_NET_GDTDEN0INT:
1310 /* someone has set TX_DMA_EN to 0 */
1314 case SPIDER_NET_GDDDEN0INT: /* fallthrough */
1315 case SPIDER_NET_GDCDEN0INT: /* fallthrough */
1316 case SPIDER_NET_GDBDEN0INT: /* fallthrough */
1317 case SPIDER_NET_GDADEN0INT:
1318 /* someone has set RX_DMA_EN to 0 */
1323 case SPIDER_NET_GDDFDCINT:
1324 case SPIDER_NET_GDCFDCINT:
1325 case SPIDER_NET_GDBFDCINT:
1326 case SPIDER_NET_GDAFDCINT:
1327 /* case SPIDER_NET_GDNMINT: not used. print a message */
1328 /* case SPIDER_NET_GCNMINT: not used. print a message */
1329 /* case SPIDER_NET_GBNMINT: not used. print a message */
1330 /* case SPIDER_NET_GANMINT: not used. print a message */
1331 /* case SPIDER_NET_GRFNMINT: not used. print a message */
1336 case SPIDER_NET_GDTFDCINT:
1339 case SPIDER_NET_GTTEDINT:
1342 case SPIDER_NET_GDTDCEINT:
1343 /* chain end. If a descriptor should be sent, kick off
1345 if (card->tx_chain.tail != card->tx_chain.head)
1346 spider_net_kick_tx_dma(card);
1351 /* case SPIDER_NET_G1TMCNTINT: not used. print a message */
1352 /* case SPIDER_NET_GFREECNTINT: not used. print a message */
1355 /* check GHIINT1STS ************************************/
1357 for (i = 0; i < 32; i++)
1358 if (error_reg1 & (1<<i))
1361 case SPIDER_NET_GTMFLLINT:
1362 if (netif_msg_intr(card) && net_ratelimit())
1363 pr_err("Spider TX RAM full\n");
1366 case SPIDER_NET_GRFDFLLINT: /* fallthrough */
1367 case SPIDER_NET_GRFCFLLINT: /* fallthrough */
1368 case SPIDER_NET_GRFBFLLINT: /* fallthrough */
1369 case SPIDER_NET_GRFAFLLINT: /* fallthrough */
1370 case SPIDER_NET_GRMFLLINT:
1371 if (netif_msg_intr(card) && net_ratelimit())
1372 pr_err("Spider RX RAM full, incoming packets "
1373 "might be discarded!\n");
1374 spider_net_rx_irq_off(card);
1375 netif_rx_schedule(card->netdev);
1379 /* case SPIDER_NET_GTMSHTINT: problem, print a message */
1380 case SPIDER_NET_GDTINVDINT:
1381 /* allrighty. tx from previous descr ok */
1386 case SPIDER_NET_GDDDCEINT: /* fallthrough */
1387 case SPIDER_NET_GDCDCEINT: /* fallthrough */
1388 case SPIDER_NET_GDBDCEINT: /* fallthrough */
1389 case SPIDER_NET_GDADCEINT:
1390 if (netif_msg_intr(card) && net_ratelimit())
1391 pr_err("got descriptor chain end interrupt, "
1392 "restarting DMAC %c.\n",
1393 'D'-(i-SPIDER_NET_GDDDCEINT)/3);
1394 spider_net_refill_rx_chain(card);
1395 spider_net_enable_rxdmac(card);
1399 /* invalid descriptor */
1400 case SPIDER_NET_GDDINVDINT: /* fallthrough */
1401 case SPIDER_NET_GDCINVDINT: /* fallthrough */
1402 case SPIDER_NET_GDBINVDINT: /* fallthrough */
1403 case SPIDER_NET_GDAINVDINT:
1404 /* could happen when rx chain is full */
1405 spider_net_refill_rx_chain(card);
1406 spider_net_enable_rxdmac(card);
1410 /* case SPIDER_NET_GDTRSERINT: problem, print a message */
1411 /* case SPIDER_NET_GDDRSERINT: problem, print a message */
1412 /* case SPIDER_NET_GDCRSERINT: problem, print a message */
1413 /* case SPIDER_NET_GDBRSERINT: problem, print a message */
1414 /* case SPIDER_NET_GDARSERINT: problem, print a message */
1415 /* case SPIDER_NET_GDSERINT: problem, print a message */
1416 /* case SPIDER_NET_GDTPTERINT: problem, print a message */
1417 /* case SPIDER_NET_GDDPTERINT: problem, print a message */
1418 /* case SPIDER_NET_GDCPTERINT: problem, print a message */
1419 /* case SPIDER_NET_GDBPTERINT: problem, print a message */
1420 /* case SPIDER_NET_GDAPTERINT: problem, print a message */
1426 /* check GHIINT2STS ************************************/
1428 for (i = 0; i < 32; i++)
1429 if (error_reg2 & (1<<i))
1432 /* there is nothing we can (want to) do at this time. Log a
1433 * message, we can switch on and off the specific values later on
1434 case SPIDER_NET_GPROPERINT:
1435 case SPIDER_NET_GMCTCRSNGINT:
1436 case SPIDER_NET_GMCTLCOLINT:
1437 case SPIDER_NET_GMCTTMOTINT:
1438 case SPIDER_NET_GMCRCAERINT:
1439 case SPIDER_NET_GMCRCALERINT:
1440 case SPIDER_NET_GMCRALNERINT:
1441 case SPIDER_NET_GMCROVRINT:
1442 case SPIDER_NET_GMCRRNTINT:
1443 case SPIDER_NET_GMCRRXERINT:
1444 case SPIDER_NET_GTITCSERINT:
1445 case SPIDER_NET_GTIFMTERINT:
1446 case SPIDER_NET_GTIPKTRVKINT:
1447 case SPIDER_NET_GTISPINGINT:
1448 case SPIDER_NET_GTISADNGINT:
1449 case SPIDER_NET_GTISPDNGINT:
1450 case SPIDER_NET_GRIFMTERINT:
1451 case SPIDER_NET_GRIPKTRVKINT:
1452 case SPIDER_NET_GRISPINGINT:
1453 case SPIDER_NET_GRISADNGINT:
1454 case SPIDER_NET_GRISPDNGINT:
1461 if ((show_error) && (netif_msg_intr(card)) && net_ratelimit())
1462 pr_err("Got error interrupt on %s, GHIINT0STS = 0x%08x, "
1463 "GHIINT1STS = 0x%08x, GHIINT2STS = 0x%08x\n",
1465 status_reg, error_reg1, error_reg2);
1467 /* clear interrupt sources */
1468 spider_net_write_reg(card, SPIDER_NET_GHIINT1STS, error_reg1);
1469 spider_net_write_reg(card, SPIDER_NET_GHIINT2STS, error_reg2);
1473 * spider_net_interrupt - interrupt handler for spider_net
1474 * @irq: interupt number
1475 * @ptr: pointer to net_device
1476 * @regs: PU registers
1478 * returns IRQ_HANDLED, if interrupt was for driver, or IRQ_NONE, if no
1479 * interrupt found raised by card.
1481 * This is the interrupt handler, that turns off
1482 * interrupts for this device and makes the stack poll the driver
1485 spider_net_interrupt(int irq, void *ptr)
1487 struct net_device *netdev = ptr;
1488 struct spider_net_card *card = netdev_priv(netdev);
1491 status_reg = spider_net_read_reg(card, SPIDER_NET_GHIINT0STS);
1496 if (status_reg & SPIDER_NET_RXINT ) {
1497 spider_net_rx_irq_off(card);
1498 netif_rx_schedule(netdev);
1500 if (status_reg & SPIDER_NET_TXINT)
1501 netif_rx_schedule(netdev);
1503 if (status_reg & SPIDER_NET_ERRINT )
1504 spider_net_handle_error_irq(card, status_reg);
1506 /* clear interrupt sources */
1507 spider_net_write_reg(card, SPIDER_NET_GHIINT0STS, status_reg);
1512 #ifdef CONFIG_NET_POLL_CONTROLLER
1514 * spider_net_poll_controller - artificial interrupt for netconsole etc.
1515 * @netdev: interface device structure
1517 * see Documentation/networking/netconsole.txt
1520 spider_net_poll_controller(struct net_device *netdev)
1522 disable_irq(netdev->irq);
1523 spider_net_interrupt(netdev->irq, netdev);
1524 enable_irq(netdev->irq);
1526 #endif /* CONFIG_NET_POLL_CONTROLLER */
1529 * spider_net_init_card - initializes the card
1530 * @card: card structure
1532 * spider_net_init_card initializes the card so that other registers can
1536 spider_net_init_card(struct spider_net_card *card)
1538 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1539 SPIDER_NET_CKRCTRL_STOP_VALUE);
1541 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1542 SPIDER_NET_CKRCTRL_RUN_VALUE);
1546 * spider_net_enable_card - enables the card by setting all kinds of regs
1547 * @card: card structure
1549 * spider_net_enable_card sets a lot of SMMIO registers to enable the device
1552 spider_net_enable_card(struct spider_net_card *card)
1555 /* the following array consists of (register),(value) pairs
1556 * that are set in this function. A register of 0 ends the list */
1558 { SPIDER_NET_GRESUMINTNUM, 0 },
1559 { SPIDER_NET_GREINTNUM, 0 },
1561 /* set interrupt frame number registers */
1562 /* clear the single DMA engine registers first */
1563 { SPIDER_NET_GFAFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1564 { SPIDER_NET_GFBFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1565 { SPIDER_NET_GFCFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1566 { SPIDER_NET_GFDFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1567 /* then set, what we really need */
1568 { SPIDER_NET_GFFRMNUM, SPIDER_NET_FRAMENUM_VALUE },
1570 /* timer counter registers and stuff */
1571 { SPIDER_NET_GFREECNNUM, 0 },
1572 { SPIDER_NET_GONETIMENUM, 0 },
1573 { SPIDER_NET_GTOUTFRMNUM, 0 },
1575 /* RX mode setting */
1576 { SPIDER_NET_GRXMDSET, SPIDER_NET_RXMODE_VALUE },
1577 /* TX mode setting */
1578 { SPIDER_NET_GTXMDSET, SPIDER_NET_TXMODE_VALUE },
1579 /* IPSEC mode setting */
1580 { SPIDER_NET_GIPSECINIT, SPIDER_NET_IPSECINIT_VALUE },
1582 { SPIDER_NET_GFTRESTRT, SPIDER_NET_RESTART_VALUE },
1584 { SPIDER_NET_GMRWOLCTRL, 0 },
1585 { SPIDER_NET_GTESTMD, 0x10000000 },
1586 { SPIDER_NET_GTTQMSK, 0x00400040 },
1588 { SPIDER_NET_GMACINTEN, 0 },
1590 /* flow control stuff */
1591 { SPIDER_NET_GMACAPAUSE, SPIDER_NET_MACAPAUSE_VALUE },
1592 { SPIDER_NET_GMACTXPAUSE, SPIDER_NET_TXPAUSE_VALUE },
1594 { SPIDER_NET_GMACBSTLMT, SPIDER_NET_BURSTLMT_VALUE },
1599 while (regs[i][0]) {
1600 spider_net_write_reg(card, regs[i][0], regs[i][1]);
1604 /* clear unicast filter table entries 1 to 14 */
1605 for (i = 1; i <= 14; i++) {
1606 spider_net_write_reg(card,
1607 SPIDER_NET_GMRUAFILnR + i * 8,
1609 spider_net_write_reg(card,
1610 SPIDER_NET_GMRUAFILnR + i * 8 + 4,
1614 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R, 0x08080000);
1616 spider_net_write_reg(card, SPIDER_NET_ECMODE, SPIDER_NET_ECMODE_VALUE);
1618 /* set chain tail adress for RX chains and
1620 spider_net_enable_rxchtails(card);
1621 spider_net_enable_rxdmac(card);
1623 spider_net_write_reg(card, SPIDER_NET_GRXDMAEN, SPIDER_NET_WOL_VALUE);
1625 spider_net_write_reg(card, SPIDER_NET_GMACLENLMT,
1626 SPIDER_NET_LENLMT_VALUE);
1627 spider_net_write_reg(card, SPIDER_NET_GMACMODE,
1628 SPIDER_NET_MACMODE_VALUE);
1629 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD,
1630 SPIDER_NET_OPMODE_VALUE);
1632 /* set interrupt mask registers */
1633 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK,
1634 SPIDER_NET_INT0_MASK_VALUE);
1635 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK,
1636 SPIDER_NET_INT1_MASK_VALUE);
1637 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK,
1638 SPIDER_NET_INT2_MASK_VALUE);
1640 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
1641 SPIDER_NET_GDTBSTA);
1645 * spider_net_open - called upon ifonfig up
1646 * @netdev: interface device structure
1648 * returns 0 on success, <0 on failure
1650 * spider_net_open allocates all the descriptors and memory needed for
1651 * operation, sets up multicast list and enables interrupts
1654 spider_net_open(struct net_device *netdev)
1656 struct spider_net_card *card = netdev_priv(netdev);
1659 result = spider_net_init_chain(card, &card->tx_chain);
1661 goto alloc_tx_failed;
1662 card->low_watermark = NULL;
1664 result = spider_net_init_chain(card, &card->rx_chain);
1666 goto alloc_rx_failed;
1668 /* Allocate rx skbs */
1669 if (spider_net_alloc_rx_skbs(card))
1670 goto alloc_skbs_failed;
1672 spider_net_set_multi(netdev);
1674 /* further enhancement: setup hw vlan, if needed */
1677 if (request_irq(netdev->irq, spider_net_interrupt,
1678 IRQF_SHARED, netdev->name, netdev))
1679 goto register_int_failed;
1681 spider_net_enable_card(card);
1683 netif_start_queue(netdev);
1684 netif_carrier_on(netdev);
1685 netif_poll_enable(netdev);
1689 register_int_failed:
1690 spider_net_free_rx_chain_contents(card);
1692 spider_net_free_chain(card, &card->rx_chain);
1694 spider_net_free_chain(card, &card->tx_chain);
1700 * spider_net_setup_phy - setup PHY
1701 * @card: card structure
1703 * returns 0 on success, <0 on failure
1705 * spider_net_setup_phy is used as part of spider_net_probe. Sets
1706 * the PHY to 1000 Mbps
1709 spider_net_setup_phy(struct spider_net_card *card)
1711 struct mii_phy *phy = &card->phy;
1713 spider_net_write_reg(card, SPIDER_NET_GDTDMASEL,
1714 SPIDER_NET_DMASEL_VALUE);
1715 spider_net_write_reg(card, SPIDER_NET_GPCCTRL,
1716 SPIDER_NET_PHY_CTRL_VALUE);
1718 phy->dev = card->netdev;
1719 phy->mdio_read = spider_net_read_phy;
1720 phy->mdio_write = spider_net_write_phy;
1722 mii_phy_probe(phy, phy->mii_id);
1724 if (phy->def->ops->setup_forced)
1725 phy->def->ops->setup_forced(phy, SPEED_1000, DUPLEX_FULL);
1727 phy->def->ops->enable_fiber(phy);
1729 phy->def->ops->read_link(phy);
1730 pr_info("Found %s with %i Mbps, %s-duplex.\n", phy->def->name,
1731 phy->speed, phy->duplex==1 ? "Full" : "Half");
1737 * spider_net_download_firmware - loads firmware into the adapter
1738 * @card: card structure
1739 * @firmware_ptr: pointer to firmware data
1741 * spider_net_download_firmware loads the firmware data into the
1742 * adapter. It assumes the length etc. to be allright.
1745 spider_net_download_firmware(struct spider_net_card *card,
1746 const void *firmware_ptr)
1749 const u32 *fw_ptr = firmware_ptr;
1751 /* stop sequencers */
1752 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1753 SPIDER_NET_STOP_SEQ_VALUE);
1755 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1757 spider_net_write_reg(card,
1758 SPIDER_NET_GSnPRGADR + sequencer * 8, 0);
1759 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
1760 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1761 sequencer * 8, *fw_ptr);
1766 if (spider_net_read_reg(card, SPIDER_NET_GSINIT))
1769 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1770 SPIDER_NET_RUN_SEQ_VALUE);
1776 * spider_net_init_firmware - reads in firmware parts
1777 * @card: card structure
1779 * Returns 0 on success, <0 on failure
1781 * spider_net_init_firmware opens the sequencer firmware and does some basic
1782 * checks. This function opens and releases the firmware structure. A call
1783 * to download the firmware is performed before the release.
1787 * spider_fw.bin is expected to be a file containing 6*1024*4 bytes, 4k being
1788 * the program for each sequencer. Use the command
1789 * tail -q -n +2 Seq_code1_0x088.txt Seq_code2_0x090.txt \
1790 * Seq_code3_0x098.txt Seq_code4_0x0A0.txt Seq_code5_0x0A8.txt \
1791 * Seq_code6_0x0B0.txt | xxd -r -p -c4 > spider_fw.bin
1793 * to generate spider_fw.bin, if you have sequencer programs with something
1794 * like the following contents for each sequencer:
1795 * <ONE LINE COMMENT>
1796 * <FIRST 4-BYTES-WORD FOR SEQUENCER>
1797 * <SECOND 4-BYTES-WORD FOR SEQUENCER>
1799 * <1024th 4-BYTES-WORD FOR SEQUENCER>
1802 spider_net_init_firmware(struct spider_net_card *card)
1804 struct firmware *firmware = NULL;
1805 struct device_node *dn;
1806 const u8 *fw_prop = NULL;
1810 if (request_firmware((const struct firmware **)&firmware,
1811 SPIDER_NET_FIRMWARE_NAME, &card->pdev->dev) == 0) {
1812 if ( (firmware->size != SPIDER_NET_FIRMWARE_LEN) &&
1813 netif_msg_probe(card) ) {
1814 pr_err("Incorrect size of spidernet firmware in " \
1815 "filesystem. Looking in host firmware...\n");
1818 err = spider_net_download_firmware(card, firmware->data);
1820 release_firmware(firmware);
1828 dn = pci_device_to_OF_node(card->pdev);
1832 fw_prop = get_property(dn, "firmware", &fw_size);
1836 if ( (fw_size != SPIDER_NET_FIRMWARE_LEN) &&
1837 netif_msg_probe(card) ) {
1838 pr_err("Incorrect size of spidernet firmware in " \
1843 err = spider_net_download_firmware(card, fw_prop);
1848 if (netif_msg_probe(card))
1849 pr_err("Couldn't find spidernet firmware in filesystem " \
1850 "or host firmware\n");
1855 * spider_net_workaround_rxramfull - work around firmware bug
1856 * @card: card structure
1861 spider_net_workaround_rxramfull(struct spider_net_card *card)
1863 int i, sequencer = 0;
1866 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1867 SPIDER_NET_CKRCTRL_RUN_VALUE);
1869 /* empty sequencer data */
1870 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1872 spider_net_write_reg(card, SPIDER_NET_GSnPRGADR +
1873 sequencer * 8, 0x0);
1874 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
1875 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1876 sequencer * 8, 0x0);
1880 /* set sequencer operation */
1881 spider_net_write_reg(card, SPIDER_NET_GSINIT, 0x000000fe);
1884 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1885 SPIDER_NET_CKRCTRL_STOP_VALUE);
1889 * spider_net_stop - called upon ifconfig down
1890 * @netdev: interface device structure
1895 spider_net_stop(struct net_device *netdev)
1897 struct spider_net_card *card = netdev_priv(netdev);
1899 netif_poll_disable(netdev);
1900 netif_carrier_off(netdev);
1901 netif_stop_queue(netdev);
1902 del_timer_sync(&card->tx_timer);
1904 /* disable/mask all interrupts */
1905 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, 0);
1906 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK, 0);
1907 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK, 0);
1909 /* free_irq(netdev->irq, netdev);*/
1910 free_irq(to_pci_dev(netdev->class_dev.dev)->irq, netdev);
1912 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
1913 SPIDER_NET_DMA_TX_FEND_VALUE);
1915 /* turn off DMA, force end */
1916 spider_net_disable_rxdmac(card);
1918 /* release chains */
1919 spider_net_release_tx_chain(card, 1);
1920 spider_net_free_rx_chain_contents(card);
1922 spider_net_free_rx_chain_contents(card);
1924 spider_net_free_chain(card, &card->tx_chain);
1925 spider_net_free_chain(card, &card->rx_chain);
1931 * spider_net_tx_timeout_task - task scheduled by the watchdog timeout
1932 * function (to be called not under interrupt status)
1933 * @data: data, is interface device structure
1935 * called as task when tx hangs, resets interface (if interface is up)
1938 spider_net_tx_timeout_task(struct work_struct *work)
1940 struct spider_net_card *card =
1941 container_of(work, struct spider_net_card, tx_timeout_task);
1942 struct net_device *netdev = card->netdev;
1944 if (!(netdev->flags & IFF_UP))
1947 netif_device_detach(netdev);
1948 spider_net_stop(netdev);
1950 spider_net_workaround_rxramfull(card);
1951 spider_net_init_card(card);
1953 if (spider_net_setup_phy(card))
1955 if (spider_net_init_firmware(card))
1958 spider_net_open(netdev);
1959 spider_net_kick_tx_dma(card);
1960 netif_device_attach(netdev);
1963 atomic_dec(&card->tx_timeout_task_counter);
1967 * spider_net_tx_timeout - called when the tx timeout watchdog kicks in.
1968 * @netdev: interface device structure
1970 * called, if tx hangs. Schedules a task that resets the interface
1973 spider_net_tx_timeout(struct net_device *netdev)
1975 struct spider_net_card *card;
1977 card = netdev_priv(netdev);
1978 atomic_inc(&card->tx_timeout_task_counter);
1979 if (netdev->flags & IFF_UP)
1980 schedule_work(&card->tx_timeout_task);
1982 atomic_dec(&card->tx_timeout_task_counter);
1983 card->spider_stats.tx_timeouts++;
1987 * spider_net_setup_netdev_ops - initialization of net_device operations
1988 * @netdev: net_device structure
1990 * fills out function pointers in the net_device structure
1993 spider_net_setup_netdev_ops(struct net_device *netdev)
1995 netdev->open = &spider_net_open;
1996 netdev->stop = &spider_net_stop;
1997 netdev->hard_start_xmit = &spider_net_xmit;
1998 netdev->get_stats = &spider_net_get_stats;
1999 netdev->set_multicast_list = &spider_net_set_multi;
2000 netdev->set_mac_address = &spider_net_set_mac;
2001 netdev->change_mtu = &spider_net_change_mtu;
2002 netdev->do_ioctl = &spider_net_do_ioctl;
2004 netdev->tx_timeout = &spider_net_tx_timeout;
2005 netdev->watchdog_timeo = SPIDER_NET_WATCHDOG_TIMEOUT;
2007 netdev->poll = &spider_net_poll;
2008 netdev->weight = SPIDER_NET_NAPI_WEIGHT;
2010 netdev->vlan_rx_register = &spider_net_vlan_rx_reg;
2011 netdev->vlan_rx_add_vid = &spider_net_vlan_rx_add;
2012 netdev->vlan_rx_kill_vid = &spider_net_vlan_rx_kill;
2013 #ifdef CONFIG_NET_POLL_CONTROLLER
2014 /* poll controller */
2015 netdev->poll_controller = &spider_net_poll_controller;
2016 #endif /* CONFIG_NET_POLL_CONTROLLER */
2018 netdev->ethtool_ops = &spider_net_ethtool_ops;
2022 * spider_net_setup_netdev - initialization of net_device
2023 * @card: card structure
2025 * Returns 0 on success or <0 on failure
2027 * spider_net_setup_netdev initializes the net_device structure
2030 spider_net_setup_netdev(struct spider_net_card *card)
2033 struct net_device *netdev = card->netdev;
2034 struct device_node *dn;
2035 struct sockaddr addr;
2038 SET_MODULE_OWNER(netdev);
2039 SET_NETDEV_DEV(netdev, &card->pdev->dev);
2041 pci_set_drvdata(card->pdev, netdev);
2043 init_timer(&card->tx_timer);
2044 card->tx_timer.function =
2045 (void (*)(unsigned long)) spider_net_cleanup_tx_ring;
2046 card->tx_timer.data = (unsigned long) card;
2047 netdev->irq = card->pdev->irq;
2049 card->options.rx_csum = SPIDER_NET_RX_CSUM_DEFAULT;
2051 card->tx_chain.num_desc = tx_descriptors;
2052 card->rx_chain.num_desc = rx_descriptors;
2054 spider_net_setup_netdev_ops(netdev);
2056 netdev->features = NETIF_F_HW_CSUM | NETIF_F_LLTX;
2057 /* some time: NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
2058 * NETIF_F_HW_VLAN_FILTER */
2060 netdev->irq = card->pdev->irq;
2062 dn = pci_device_to_OF_node(card->pdev);
2066 mac = get_property(dn, "local-mac-address", NULL);
2069 memcpy(addr.sa_data, mac, ETH_ALEN);
2071 result = spider_net_set_mac(netdev, &addr);
2072 if ((result) && (netif_msg_probe(card)))
2073 pr_err("Failed to set MAC address: %i\n", result);
2075 result = register_netdev(netdev);
2077 if (netif_msg_probe(card))
2078 pr_err("Couldn't register net_device: %i\n",
2083 if (netif_msg_probe(card))
2084 pr_info("Initialized device %s.\n", netdev->name);
2090 * spider_net_alloc_card - allocates net_device and card structure
2092 * returns the card structure or NULL in case of errors
2094 * the card and net_device structures are linked to each other
2096 static struct spider_net_card *
2097 spider_net_alloc_card(void)
2099 struct net_device *netdev;
2100 struct spider_net_card *card;
2102 netdev = alloc_etherdev(sizeof(struct spider_net_card));
2106 card = netdev_priv(netdev);
2107 card->netdev = netdev;
2108 card->msg_enable = SPIDER_NET_DEFAULT_MSG;
2109 INIT_WORK(&card->tx_timeout_task, spider_net_tx_timeout_task);
2110 init_waitqueue_head(&card->waitq);
2111 atomic_set(&card->tx_timeout_task_counter, 0);
2117 * spider_net_undo_pci_setup - releases PCI ressources
2118 * @card: card structure
2120 * spider_net_undo_pci_setup releases the mapped regions
2123 spider_net_undo_pci_setup(struct spider_net_card *card)
2125 iounmap(card->regs);
2126 pci_release_regions(card->pdev);
2130 * spider_net_setup_pci_dev - sets up the device in terms of PCI operations
2131 * @card: card structure
2134 * Returns the card structure or NULL if any errors occur
2136 * spider_net_setup_pci_dev initializes pdev and together with the
2137 * functions called in spider_net_open configures the device so that
2138 * data can be transferred over it
2139 * The net_device structure is attached to the card structure, if the
2140 * function returns without error.
2142 static struct spider_net_card *
2143 spider_net_setup_pci_dev(struct pci_dev *pdev)
2145 struct spider_net_card *card;
2146 unsigned long mmio_start, mmio_len;
2148 if (pci_enable_device(pdev)) {
2149 pr_err("Couldn't enable PCI device\n");
2153 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
2154 pr_err("Couldn't find proper PCI device base address.\n");
2155 goto out_disable_dev;
2158 if (pci_request_regions(pdev, spider_net_driver_name)) {
2159 pr_err("Couldn't obtain PCI resources, aborting.\n");
2160 goto out_disable_dev;
2163 pci_set_master(pdev);
2165 card = spider_net_alloc_card();
2167 pr_err("Couldn't allocate net_device structure, "
2169 goto out_release_regions;
2173 /* fetch base address and length of first resource */
2174 mmio_start = pci_resource_start(pdev, 0);
2175 mmio_len = pci_resource_len(pdev, 0);
2177 card->netdev->mem_start = mmio_start;
2178 card->netdev->mem_end = mmio_start + mmio_len;
2179 card->regs = ioremap(mmio_start, mmio_len);
2182 pr_err("Couldn't obtain PCI resources, aborting.\n");
2183 goto out_release_regions;
2188 out_release_regions:
2189 pci_release_regions(pdev);
2191 pci_disable_device(pdev);
2192 pci_set_drvdata(pdev, NULL);
2197 * spider_net_probe - initialization of a device
2199 * @ent: entry in the device id list
2201 * Returns 0 on success, <0 on failure
2203 * spider_net_probe initializes pdev and registers a net_device
2204 * structure for it. After that, the device can be ifconfig'ed up
2206 static int __devinit
2207 spider_net_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2210 struct spider_net_card *card;
2212 card = spider_net_setup_pci_dev(pdev);
2216 spider_net_workaround_rxramfull(card);
2217 spider_net_init_card(card);
2219 err = spider_net_setup_phy(card);
2223 err = spider_net_init_firmware(card);
2227 err = spider_net_setup_netdev(card);
2234 spider_net_undo_pci_setup(card);
2235 free_netdev(card->netdev);
2241 * spider_net_remove - removal of a device
2244 * Returns 0 on success, <0 on failure
2246 * spider_net_remove is called to remove the device and unregisters the
2249 static void __devexit
2250 spider_net_remove(struct pci_dev *pdev)
2252 struct net_device *netdev;
2253 struct spider_net_card *card;
2255 netdev = pci_get_drvdata(pdev);
2256 card = netdev_priv(netdev);
2258 wait_event(card->waitq,
2259 atomic_read(&card->tx_timeout_task_counter) == 0);
2261 unregister_netdev(netdev);
2263 /* switch off card */
2264 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2265 SPIDER_NET_CKRCTRL_STOP_VALUE);
2266 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2267 SPIDER_NET_CKRCTRL_RUN_VALUE);
2269 spider_net_undo_pci_setup(card);
2270 free_netdev(netdev);
2273 static struct pci_driver spider_net_driver = {
2274 .name = spider_net_driver_name,
2275 .id_table = spider_net_pci_tbl,
2276 .probe = spider_net_probe,
2277 .remove = __devexit_p(spider_net_remove)
2281 * spider_net_init - init function when the driver is loaded
2283 * spider_net_init registers the device driver
2285 static int __init spider_net_init(void)
2287 printk(KERN_INFO "Spidernet version %s.\n", VERSION);
2289 if (rx_descriptors < SPIDER_NET_RX_DESCRIPTORS_MIN) {
2290 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MIN;
2291 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2293 if (rx_descriptors > SPIDER_NET_RX_DESCRIPTORS_MAX) {
2294 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MAX;
2295 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2297 if (tx_descriptors < SPIDER_NET_TX_DESCRIPTORS_MIN) {
2298 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MIN;
2299 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2301 if (tx_descriptors > SPIDER_NET_TX_DESCRIPTORS_MAX) {
2302 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MAX;
2303 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2306 return pci_register_driver(&spider_net_driver);
2310 * spider_net_cleanup - exit function when driver is unloaded
2312 * spider_net_cleanup unregisters the device driver
2314 static void __exit spider_net_cleanup(void)
2316 pci_unregister_driver(&spider_net_driver);
2319 module_init(spider_net_init);
2320 module_exit(spider_net_cleanup);