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/config.h>
26 #include <linux/compiler.h>
27 #include <linux/crc32.h>
28 #include <linux/delay.h>
29 #include <linux/etherdevice.h>
30 #include <linux/ethtool.h>
31 #include <linux/firmware.h>
32 #include <linux/if_vlan.h>
33 #include <linux/init.h>
34 #include <linux/ioport.h>
36 #include <linux/kernel.h>
37 #include <linux/mii.h>
38 #include <linux/module.h>
39 #include <linux/netdevice.h>
40 #include <linux/device.h>
41 #include <linux/pci.h>
42 #include <linux/skbuff.h>
43 #include <linux/slab.h>
44 #include <linux/tcp.h>
45 #include <linux/types.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");
59 static int rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_DEFAULT;
60 static int tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_DEFAULT;
62 module_param(rx_descriptors, int, 0644);
63 module_param(tx_descriptors, int, 0644);
65 MODULE_PARM_DESC(rx_descriptors, "number of descriptors used " \
67 MODULE_PARM_DESC(tx_descriptors, "number of descriptors used " \
70 char spider_net_driver_name[] = "spidernet";
72 static struct pci_device_id spider_net_pci_tbl[] = {
73 { PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_SPIDER_NET,
74 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
78 MODULE_DEVICE_TABLE(pci, spider_net_pci_tbl);
81 * spider_net_read_reg - reads an SMMIO register of a card
82 * @card: device structure
83 * @reg: register to read from
85 * returns the content of the specified SMMIO register.
88 spider_net_read_reg(struct spider_net_card *card, u32 reg)
92 value = readl(card->regs + reg);
93 value = le32_to_cpu(value);
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 value = cpu_to_le32(value);
108 writel(value, card->regs + reg);
112 * spider_net_write_reg_sync - writes to an SMMIO register of a card
113 * @card: device structure
114 * @reg: register to write to
115 * @value: value to write into the specified SMMIO register
117 * Unlike spider_net_write_reg, this will also make sure the
118 * data arrives on the card by reading the reg again.
121 spider_net_write_reg_sync(struct spider_net_card *card, u32 reg, u32 value)
123 value = cpu_to_le32(value);
124 writel(value, card->regs + reg);
125 (void)readl(card->regs + reg);
129 * spider_net_rx_irq_off - switch off rx irq on this spider card
130 * @card: device structure
132 * switches off rx irq by masking them out in the GHIINTnMSK register
135 spider_net_rx_irq_off(struct spider_net_card *card)
140 spin_lock_irqsave(&card->intmask_lock, flags);
141 regvalue = spider_net_read_reg(card, SPIDER_NET_GHIINT0MSK);
142 regvalue &= ~SPIDER_NET_RXINT;
143 spider_net_write_reg_sync(card, SPIDER_NET_GHIINT0MSK, regvalue);
144 spin_unlock_irqrestore(&card->intmask_lock, flags);
147 /** spider_net_write_phy - write to phy register
148 * @netdev: adapter to be written to
151 * @val: value to be written to phy register
153 * spider_net_write_phy_register writes to an arbitrary PHY
154 * register via the spider GPCWOPCMD register. We assume the queue does
155 * not run full (not more than 15 commands outstanding).
158 spider_net_write_phy(struct net_device *netdev, int mii_id,
161 struct spider_net_card *card = netdev_priv(netdev);
164 writevalue = ((u32)mii_id << 21) |
165 ((u32)reg << 16) | ((u32)val);
167 spider_net_write_reg(card, SPIDER_NET_GPCWOPCMD, writevalue);
170 /** spider_net_read_phy - read from phy register
171 * @netdev: network device to be read from
175 * Returns value read from PHY register
177 * spider_net_write_phy reads from an arbitrary PHY
178 * register via the spider GPCROPCMD register
181 spider_net_read_phy(struct net_device *netdev, int mii_id, int reg)
183 struct spider_net_card *card = netdev_priv(netdev);
186 readvalue = ((u32)mii_id << 21) | ((u32)reg << 16);
187 spider_net_write_reg(card, SPIDER_NET_GPCROPCMD, readvalue);
189 /* we don't use semaphores to wait for an SPIDER_NET_GPROPCMPINT
190 * interrupt, as we poll for the completion of the read operation
191 * in spider_net_read_phy. Should take about 50 us */
193 readvalue = spider_net_read_reg(card, SPIDER_NET_GPCROPCMD);
194 } while (readvalue & SPIDER_NET_GPREXEC);
196 readvalue &= SPIDER_NET_GPRDAT_MASK;
202 * spider_net_rx_irq_on - switch on rx irq on this spider card
203 * @card: device structure
205 * switches on rx irq by enabling them in the GHIINTnMSK register
208 spider_net_rx_irq_on(struct spider_net_card *card)
213 spin_lock_irqsave(&card->intmask_lock, flags);
214 regvalue = spider_net_read_reg(card, SPIDER_NET_GHIINT0MSK);
215 regvalue |= SPIDER_NET_RXINT;
216 spider_net_write_reg_sync(card, SPIDER_NET_GHIINT0MSK, regvalue);
217 spin_unlock_irqrestore(&card->intmask_lock, flags);
221 * spider_net_tx_irq_off - switch off tx irq on this spider card
222 * @card: device structure
224 * switches off tx irq by masking them out in the GHIINTnMSK register
227 spider_net_tx_irq_off(struct spider_net_card *card)
232 spin_lock_irqsave(&card->intmask_lock, flags);
233 regvalue = spider_net_read_reg(card, SPIDER_NET_GHIINT0MSK);
234 regvalue &= ~SPIDER_NET_TXINT;
235 spider_net_write_reg_sync(card, SPIDER_NET_GHIINT0MSK, regvalue);
236 spin_unlock_irqrestore(&card->intmask_lock, flags);
240 * spider_net_tx_irq_on - switch on tx irq on this spider card
241 * @card: device structure
243 * switches on tx irq by enabling them in the GHIINTnMSK register
246 spider_net_tx_irq_on(struct spider_net_card *card)
251 spin_lock_irqsave(&card->intmask_lock, flags);
252 regvalue = spider_net_read_reg(card, SPIDER_NET_GHIINT0MSK);
253 regvalue |= SPIDER_NET_TXINT;
254 spider_net_write_reg_sync(card, SPIDER_NET_GHIINT0MSK, regvalue);
255 spin_unlock_irqrestore(&card->intmask_lock, flags);
259 * spider_net_set_promisc - sets the unicast address or the promiscuous mode
260 * @card: card structure
262 * spider_net_set_promisc sets the unicast destination address filter and
263 * thus either allows for non-promisc mode or promisc mode
266 spider_net_set_promisc(struct spider_net_card *card)
269 struct net_device *netdev = card->netdev;
271 if (netdev->flags & IFF_PROMISC) {
272 /* clear destination entry 0 */
273 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR, 0);
274 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR + 0x04, 0);
275 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R,
276 SPIDER_NET_PROMISC_VALUE);
278 macu = netdev->dev_addr[0];
280 macu |= netdev->dev_addr[1];
281 memcpy(&macl, &netdev->dev_addr[2], sizeof(macl));
283 macu |= SPIDER_NET_UA_DESCR_VALUE;
284 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR, macu);
285 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR + 0x04, macl);
286 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R,
287 SPIDER_NET_NONPROMISC_VALUE);
292 * spider_net_get_mac_address - read mac address from spider card
293 * @card: device structure
295 * reads MAC address from GMACUNIMACU and GMACUNIMACL registers
298 spider_net_get_mac_address(struct net_device *netdev)
300 struct spider_net_card *card = netdev_priv(netdev);
303 macl = spider_net_read_reg(card, SPIDER_NET_GMACUNIMACL);
304 macu = spider_net_read_reg(card, SPIDER_NET_GMACUNIMACU);
306 netdev->dev_addr[0] = (macu >> 24) & 0xff;
307 netdev->dev_addr[1] = (macu >> 16) & 0xff;
308 netdev->dev_addr[2] = (macu >> 8) & 0xff;
309 netdev->dev_addr[3] = macu & 0xff;
310 netdev->dev_addr[4] = (macl >> 8) & 0xff;
311 netdev->dev_addr[5] = macl & 0xff;
313 if (!is_valid_ether_addr(&netdev->dev_addr[0]))
320 * spider_net_get_descr_status -- returns the status of a descriptor
321 * @descr: descriptor to look at
323 * returns the status as in the dmac_cmd_status field of the descriptor
325 static enum spider_net_descr_status
326 spider_net_get_descr_status(struct spider_net_descr *descr)
330 cmd_status = descr->dmac_cmd_status;
332 cmd_status >>= SPIDER_NET_DESCR_IND_PROC_SHIFT;
333 /* no need to mask out any bits, as cmd_status is 32 bits wide only
339 * spider_net_set_descr_status -- sets the status of a descriptor
340 * @descr: descriptor to change
341 * @status: status to set in the descriptor
343 * changes the status to the specified value. Doesn't change other bits
347 spider_net_set_descr_status(struct spider_net_descr *descr,
348 enum spider_net_descr_status status)
351 /* read the status */
353 cmd_status = descr->dmac_cmd_status;
354 /* clean the upper 4 bits */
355 cmd_status &= SPIDER_NET_DESCR_IND_PROC_MASKO;
356 /* add the status to it */
357 cmd_status |= ((u32)status)<<SPIDER_NET_DESCR_IND_PROC_SHIFT;
358 /* and write it back */
359 descr->dmac_cmd_status = cmd_status;
364 * spider_net_free_chain - free descriptor chain
365 * @card: card structure
366 * @chain: address of chain
370 spider_net_free_chain(struct spider_net_card *card,
371 struct spider_net_descr_chain *chain)
373 struct spider_net_descr *descr;
375 for (descr = chain->tail; !descr->bus_addr; descr = descr->next) {
376 pci_unmap_single(card->pdev, descr->bus_addr,
377 SPIDER_NET_DESCR_SIZE, PCI_DMA_BIDIRECTIONAL);
383 * spider_net_init_chain - links descriptor chain
384 * @card: card structure
385 * @chain: address of chain
386 * @start_descr: address of descriptor array
387 * @no: number of descriptors
389 * we manage a circular list that mirrors the hardware structure,
390 * except that the hardware uses bus addresses.
392 * returns 0 on success, <0 on failure
395 spider_net_init_chain(struct spider_net_card *card,
396 struct spider_net_descr_chain *chain,
397 struct spider_net_descr *start_descr, int no)
400 struct spider_net_descr *descr;
402 spin_lock_init(&card->chain_lock);
405 memset(descr, 0, sizeof(*descr) * no);
407 /* set up the hardware pointers in each descriptor */
408 for (i=0; i<no; i++, descr++) {
409 spider_net_set_descr_status(descr, SPIDER_NET_DESCR_NOT_IN_USE);
412 pci_map_single(card->pdev, descr,
413 SPIDER_NET_DESCR_SIZE,
414 PCI_DMA_BIDIRECTIONAL);
416 if (descr->bus_addr == DMA_ERROR_CODE)
419 descr->next = descr + 1;
420 descr->prev = descr - 1;
423 /* do actual circular list */
424 (descr-1)->next = start_descr;
425 start_descr->prev = descr-1;
428 for (i=0; i < no; i++, descr++) {
429 descr->next_descr_addr = descr->next->bus_addr;
432 chain->head = start_descr;
433 chain->tail = start_descr;
439 for (i=0; i < no; i++, descr++)
441 pci_unmap_single(card->pdev, descr->bus_addr,
442 SPIDER_NET_DESCR_SIZE, PCI_DMA_BIDIRECTIONAL);
447 * spider_net_free_rx_chain_contents - frees descr contents in rx chain
448 * @card: card structure
450 * returns 0 on success, <0 on failure
453 spider_net_free_rx_chain_contents(struct spider_net_card *card)
455 struct spider_net_descr *descr;
457 descr = card->rx_chain.head;
458 while (descr->next != card->rx_chain.head) {
460 dev_kfree_skb(descr->skb);
461 pci_unmap_single(card->pdev, descr->buf_addr,
463 PCI_DMA_BIDIRECTIONAL);
470 * spider_net_prepare_rx_descr - reinitializes a rx descriptor
471 * @card: card structure
472 * @descr: descriptor to re-init
474 * return 0 on succes, <0 on failure
476 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
477 * Activate the descriptor state-wise
480 spider_net_prepare_rx_descr(struct spider_net_card *card,
481 struct spider_net_descr *descr)
487 /* we need to round up the buffer size to a multiple of 128 */
488 bufsize = (SPIDER_NET_MAX_MTU + SPIDER_NET_RXBUF_ALIGN - 1) &
489 (~(SPIDER_NET_RXBUF_ALIGN - 1));
491 /* and we need to have it 128 byte aligned, therefore we allocate a
493 /* allocate an skb */
494 descr->skb = dev_alloc_skb(bufsize + SPIDER_NET_RXBUF_ALIGN - 1);
497 if (netif_msg_rx_err(card))
498 pr_err("Not enough memory to allocate "
502 descr->buf_size = bufsize;
503 descr->result_size = 0;
504 descr->valid_size = 0;
505 descr->data_status = 0;
506 descr->data_error = 0;
508 offset = ((unsigned long)descr->skb->data) &
509 (SPIDER_NET_RXBUF_ALIGN - 1);
511 skb_reserve(descr->skb, SPIDER_NET_RXBUF_ALIGN - offset);
512 /* io-mmu-map the skb */
513 descr->buf_addr = pci_map_single(card->pdev, descr->skb->data,
515 PCI_DMA_BIDIRECTIONAL);
516 if (descr->buf_addr == DMA_ERROR_CODE) {
517 dev_kfree_skb_any(descr->skb);
518 if (netif_msg_rx_err(card))
519 pr_err("Could not iommu-map rx buffer\n");
520 spider_net_set_descr_status(descr, SPIDER_NET_DESCR_NOT_IN_USE);
522 descr->dmac_cmd_status = SPIDER_NET_DMAC_RX_CARDOWNED;
529 * spider_net_enable_rxctails - sets RX dmac chain tail addresses
530 * @card: card structure
532 * spider_net_enable_rxctails sets the RX DMAC chain tail adresses in the
533 * chip by writing to the appropriate register. DMA is enabled in
534 * spider_net_enable_rxdmac.
537 spider_net_enable_rxchtails(struct spider_net_card *card)
539 /* assume chain is aligned correctly */
540 spider_net_write_reg(card, SPIDER_NET_GDADCHA ,
541 card->rx_chain.tail->bus_addr);
545 * spider_net_enable_rxdmac - enables a receive DMA controller
546 * @card: card structure
548 * spider_net_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
549 * in the GDADMACCNTR register
552 spider_net_enable_rxdmac(struct spider_net_card *card)
554 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
555 SPIDER_NET_DMA_RX_VALUE);
559 * spider_net_refill_rx_chain - refills descriptors/skbs in the rx chains
560 * @card: card structure
562 * refills descriptors in all chains (last used chain first): allocates skbs
563 * and iommu-maps them.
566 spider_net_refill_rx_chain(struct spider_net_card *card)
568 struct spider_net_descr_chain *chain;
572 chain = &card->rx_chain;
574 spin_lock_irqsave(&card->chain_lock, flags);
575 while (spider_net_get_descr_status(chain->head) ==
576 SPIDER_NET_DESCR_NOT_IN_USE) {
577 if (spider_net_prepare_rx_descr(card, chain->head))
580 chain->head = chain->head->next;
582 spin_unlock_irqrestore(&card->chain_lock, flags);
584 /* could be optimized, only do that, if we know the DMA processing
587 spider_net_enable_rxdmac(card);
591 * spider_net_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
592 * @card: card structure
594 * returns 0 on success, <0 on failure
597 spider_net_alloc_rx_skbs(struct spider_net_card *card)
600 struct spider_net_descr_chain *chain;
604 chain = &card->rx_chain;
605 /* put at least one buffer into the chain. if this fails,
606 * we've got a problem. if not, spider_net_refill_rx_chain
607 * will do the rest at the end of this function */
608 if (spider_net_prepare_rx_descr(card, chain->head))
611 chain->head = chain->head->next;
613 /* this will allocate the rest of the rx buffers; if not, it's
614 * business as usual later on */
615 spider_net_refill_rx_chain(card);
619 spider_net_free_rx_chain_contents(card);
624 * spider_net_release_tx_descr - processes a used tx descriptor
625 * @card: card structure
626 * @descr: descriptor to release
628 * releases a used tx descriptor (unmapping, freeing of skb)
631 spider_net_release_tx_descr(struct spider_net_card *card,
632 struct spider_net_descr *descr)
638 pci_unmap_single(card->pdev, descr->buf_addr, skb->len,
639 PCI_DMA_BIDIRECTIONAL);
641 dev_kfree_skb_any(skb);
643 /* set status to not used */
644 spider_net_set_descr_status(descr, SPIDER_NET_DESCR_NOT_IN_USE);
648 * spider_net_release_tx_chain - processes sent tx descriptors
649 * @card: adapter structure
650 * @brutal: if set, don't care about whether descriptor seems to be in use
652 * releases the tx descriptors that spider has finished with (if non-brutal)
653 * or simply release tx descriptors (if brutal)
656 spider_net_release_tx_chain(struct spider_net_card *card, int brutal)
658 struct spider_net_descr_chain *tx_chain = &card->tx_chain;
659 enum spider_net_descr_status status;
661 spider_net_tx_irq_off(card);
663 /* no lock for chain needed, if this is only executed once at a time */
666 status = spider_net_get_descr_status(tx_chain->tail);
668 case SPIDER_NET_DESCR_CARDOWNED:
669 if (!brutal) goto out;
670 /* fallthrough, if we release the descriptors
671 * brutally (then we don't care about
672 * SPIDER_NET_DESCR_CARDOWNED) */
673 case SPIDER_NET_DESCR_RESPONSE_ERROR:
674 case SPIDER_NET_DESCR_PROTECTION_ERROR:
675 case SPIDER_NET_DESCR_FORCE_END:
676 if (netif_msg_tx_err(card))
677 pr_err("%s: forcing end of tx descriptor "
678 "with status x%02x\n",
679 card->netdev->name, status);
680 card->netdev_stats.tx_dropped++;
683 case SPIDER_NET_DESCR_COMPLETE:
684 card->netdev_stats.tx_packets++;
685 card->netdev_stats.tx_bytes +=
686 tx_chain->tail->skb->len;
689 default: /* any other value (== SPIDER_NET_DESCR_NOT_IN_USE) */
692 spider_net_release_tx_descr(card, tx_chain->tail);
693 tx_chain->tail = tx_chain->tail->next;
696 netif_wake_queue(card->netdev);
699 /* switch on tx irqs (while we are still in the interrupt
700 * handler, so we don't get an interrupt), check again
701 * for done descriptors. This results in fewer interrupts */
702 spider_net_tx_irq_on(card);
703 status = spider_net_get_descr_status(tx_chain->tail);
705 case SPIDER_NET_DESCR_RESPONSE_ERROR:
706 case SPIDER_NET_DESCR_PROTECTION_ERROR:
707 case SPIDER_NET_DESCR_FORCE_END:
708 case SPIDER_NET_DESCR_COMPLETE:
718 * spider_net_get_multicast_hash - generates hash for multicast filter table
719 * @addr: multicast address
721 * returns the hash value.
723 * spider_net_get_multicast_hash calculates a hash value for a given multicast
724 * address, that is used to set the multicast filter tables
727 spider_net_get_multicast_hash(struct net_device *netdev, __u8 *addr)
729 /* FIXME: an addr of 01:00:5e:00:00:01 must result in 0xa9,
730 * ff:ff:ff:ff:ff:ff must result in 0xfd */
734 crc = crc32_be(~0, addr, netdev->addr_len);
744 * spider_net_set_multi - sets multicast addresses and promisc flags
745 * @netdev: interface device structure
747 * spider_net_set_multi configures multicast addresses as needed for the
748 * netdev interface. It also sets up multicast, allmulti and promisc
749 * flags appropriately
752 spider_net_set_multi(struct net_device *netdev)
754 struct dev_mc_list *mc;
758 struct spider_net_card *card = netdev_priv(netdev);
759 unsigned long bitmask[SPIDER_NET_MULTICAST_HASHES / BITS_PER_LONG] =
762 spider_net_set_promisc(card);
764 if (netdev->flags & IFF_ALLMULTI) {
765 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES; i++) {
771 /* well, we know, what the broadcast hash value is: it's xfd
772 hash = spider_net_get_multicast_hash(netdev, netdev->broadcast); */
773 set_bit(0xfd, bitmask);
775 for (mc = netdev->mc_list; mc; mc = mc->next) {
776 hash = spider_net_get_multicast_hash(netdev, mc->dmi_addr);
777 set_bit(hash, bitmask);
781 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES / 4; i++) {
783 if (test_bit(i * 4, bitmask))
786 if (test_bit(i * 4 + 1, bitmask))
789 if (test_bit(i * 4 + 2, bitmask))
792 if (test_bit(i * 4 + 3, bitmask))
795 spider_net_write_reg(card, SPIDER_NET_GMRMHFILnR + i * 4, reg);
800 * spider_net_disable_rxdmac - disables the receive DMA controller
801 * @card: card structure
803 * spider_net_disable_rxdmac terminates processing on the DMA controller by
804 * turing off DMA and issueing a force end
807 spider_net_disable_rxdmac(struct spider_net_card *card)
809 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
810 SPIDER_NET_DMA_RX_FEND_VALUE);
814 * spider_net_stop - called upon ifconfig down
815 * @netdev: interface device structure
820 spider_net_stop(struct net_device *netdev)
822 struct spider_net_card *card = netdev_priv(netdev);
824 netif_poll_disable(netdev);
825 netif_carrier_off(netdev);
826 netif_stop_queue(netdev);
828 /* disable/mask all interrupts */
829 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, 0);
830 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK, 0);
831 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK, 0);
833 /* free_irq(netdev->irq, netdev);*/
834 free_irq(to_pci_dev(netdev->class_dev.dev)->irq, netdev);
836 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
837 SPIDER_NET_DMA_TX_FEND_VALUE);
839 /* turn off DMA, force end */
840 spider_net_disable_rxdmac(card);
843 spider_net_release_tx_chain(card, 1);
845 spider_net_free_chain(card, &card->tx_chain);
846 spider_net_free_chain(card, &card->rx_chain);
852 * spider_net_get_next_tx_descr - returns the next available tx descriptor
853 * @card: device structure to get descriptor from
855 * returns the address of the next descriptor, or NULL if not available.
857 static struct spider_net_descr *
858 spider_net_get_next_tx_descr(struct spider_net_card *card)
860 /* check, if head points to not-in-use descr */
861 if ( spider_net_get_descr_status(card->tx_chain.head) ==
862 SPIDER_NET_DESCR_NOT_IN_USE ) {
863 return card->tx_chain.head;
870 * spider_net_set_txdescr_cmdstat - sets the tx descriptor command field
871 * @descr: descriptor structure to fill out
872 * @skb: packet to consider
874 * fills out the command and status field of the descriptor structure,
875 * depending on hardware checksum settings. This function assumes a wmb()
876 * has executed before.
879 spider_net_set_txdescr_cmdstat(struct spider_net_descr *descr,
882 if (skb->ip_summed != CHECKSUM_HW) {
883 descr->dmac_cmd_status = SPIDER_NET_DMAC_CMDSTAT_NOCS;
888 * if yes: tcp? udp? */
889 if (skb->protocol == htons(ETH_P_IP)) {
890 if (skb->nh.iph->protocol == IPPROTO_TCP) {
891 descr->dmac_cmd_status = SPIDER_NET_DMAC_CMDSTAT_TCPCS;
892 } else if (skb->nh.iph->protocol == IPPROTO_UDP) {
893 descr->dmac_cmd_status = SPIDER_NET_DMAC_CMDSTAT_UDPCS;
894 } else { /* the stack should checksum non-tcp and non-udp
895 packets on his own: NETIF_F_IP_CSUM */
896 descr->dmac_cmd_status = SPIDER_NET_DMAC_CMDSTAT_NOCS;
902 * spider_net_prepare_tx_descr - fill tx descriptor with skb data
903 * @card: card structure
904 * @descr: descriptor structure to fill out
905 * @skb: packet to use
907 * returns 0 on success, <0 on failure.
909 * fills out the descriptor structure with skb data and len. Copies data,
910 * if needed (32bit DMA!)
913 spider_net_prepare_tx_descr(struct spider_net_card *card,
914 struct spider_net_descr *descr,
917 descr->buf_addr = pci_map_single(card->pdev, skb->data,
918 skb->len, PCI_DMA_BIDIRECTIONAL);
919 if (descr->buf_addr == DMA_ERROR_CODE) {
920 if (netif_msg_tx_err(card))
921 pr_err("could not iommu-map packet (%p, %i). "
922 "Dropping packet\n", skb->data, skb->len);
926 descr->buf_size = skb->len;
928 descr->data_status = 0;
930 /* make sure the above values are in memory before we change the
934 spider_net_set_txdescr_cmdstat(descr,skb);
940 * spider_net_kick_tx_dma - enables TX DMA processing
941 * @card: card structure
942 * @descr: descriptor address to enable TX processing at
944 * spider_net_kick_tx_dma writes the current tx chain head as start address
945 * of the tx descriptor chain and enables the transmission DMA engine
948 spider_net_kick_tx_dma(struct spider_net_card *card,
949 struct spider_net_descr *descr)
951 /* this is the only descriptor in the output chain.
954 spider_net_write_reg(card, SPIDER_NET_GDTDCHA,
957 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
958 SPIDER_NET_DMA_TX_VALUE);
962 * spider_net_xmit - transmits a frame over the device
963 * @skb: packet to send out
964 * @netdev: interface device structure
966 * returns 0 on success, <0 on failure
969 spider_net_xmit(struct sk_buff *skb, struct net_device *netdev)
971 struct spider_net_card *card = netdev_priv(netdev);
972 struct spider_net_descr *descr;
975 descr = spider_net_get_next_tx_descr(card);
978 netif_stop_queue(netdev);
980 descr = spider_net_get_next_tx_descr(card);
984 netif_start_queue(netdev);
987 result = spider_net_prepare_tx_descr(card, descr, skb);
991 card->tx_chain.head = card->tx_chain.head->next;
993 /* make sure the status from spider_net_prepare_tx_descr is in
994 * memory before we check out the previous descriptor */
997 if (spider_net_get_descr_status(descr->prev) !=
998 SPIDER_NET_DESCR_CARDOWNED)
999 spider_net_kick_tx_dma(card, descr);
1001 return NETDEV_TX_OK;
1004 card->netdev_stats.tx_dropped++;
1005 return NETDEV_TX_LOCKED;
1009 * spider_net_do_ioctl - called for device ioctls
1010 * @netdev: interface device structure
1011 * @ifr: request parameter structure for ioctl
1012 * @cmd: command code for ioctl
1014 * returns 0 on success, <0 on failure. Currently, we have no special ioctls.
1015 * -EOPNOTSUPP is returned, if an unknown ioctl was requested
1018 spider_net_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1027 * spider_net_pass_skb_up - takes an skb from a descriptor and passes it on
1028 * @descr: descriptor to process
1029 * @card: card structure
1031 * returns 1 on success, 0 if no packet was passed to the stack
1033 * iommu-unmaps the skb, fills out skb structure and passes the data to the
1034 * stack. The descriptor state is not changed.
1037 spider_net_pass_skb_up(struct spider_net_descr *descr,
1038 struct spider_net_card *card)
1040 struct sk_buff *skb;
1041 struct net_device *netdev;
1042 u32 data_status, data_error;
1044 data_status = descr->data_status;
1045 data_error = descr->data_error;
1047 netdev = card->netdev;
1049 /* check for errors in the data_error flag */
1050 if ((data_error & SPIDER_NET_DATA_ERROR_MASK) &&
1051 netif_msg_rx_err(card))
1052 pr_err("error in received descriptor found, "
1053 "data_status=x%08x, data_error=x%08x\n",
1054 data_status, data_error);
1056 /* prepare skb, unmap descriptor */
1058 pci_unmap_single(card->pdev, descr->buf_addr, SPIDER_NET_MAX_MTU,
1059 PCI_DMA_BIDIRECTIONAL);
1061 /* the cases we'll throw away the packet immediately */
1062 if (data_error & SPIDER_NET_DESTROY_RX_FLAGS)
1066 skb_put(skb, descr->valid_size);
1068 /* the card seems to add 2 bytes of junk in front
1069 * of the ethernet frame */
1070 #define SPIDER_MISALIGN 2
1071 skb_pull(skb, SPIDER_MISALIGN);
1072 skb->protocol = eth_type_trans(skb, netdev);
1074 /* checksum offload */
1075 if (card->options.rx_csum) {
1076 if ( (data_status & SPIDER_NET_DATA_STATUS_CHK_MASK) &&
1077 (!(data_error & SPIDER_NET_DATA_ERROR_CHK_MASK)) )
1078 skb->ip_summed = CHECKSUM_UNNECESSARY;
1080 skb->ip_summed = CHECKSUM_NONE;
1082 skb->ip_summed = CHECKSUM_NONE;
1085 if (data_status & SPIDER_NET_VLAN_PACKET) {
1086 /* further enhancements: HW-accel VLAN
1087 * vlan_hwaccel_receive_skb
1091 /* pass skb up to stack */
1092 netif_receive_skb(skb);
1094 /* update netdevice statistics */
1095 card->netdev_stats.rx_packets++;
1096 card->netdev_stats.rx_bytes += skb->len;
1102 * spider_net_decode_descr - processes an rx descriptor
1103 * @card: card structure
1105 * returns 1 if a packet has been sent to the stack, otherwise 0
1107 * processes an rx descriptor by iommu-unmapping the data buffer and passing
1108 * the packet up to the stack
1111 spider_net_decode_one_descr(struct spider_net_card *card)
1113 enum spider_net_descr_status status;
1114 struct spider_net_descr *descr;
1115 struct spider_net_descr_chain *chain;
1118 chain = &card->rx_chain;
1119 descr = chain->tail;
1121 status = spider_net_get_descr_status(descr);
1123 if (status == SPIDER_NET_DESCR_CARDOWNED) {
1124 /* nothing in the descriptor yet */
1128 if (status == SPIDER_NET_DESCR_NOT_IN_USE) {
1129 /* not initialized yet, I bet chain->tail == chain->head
1130 * and the ring is empty */
1131 spider_net_refill_rx_chain(card);
1135 /* descriptor definitively used -- move on head */
1136 chain->tail = descr->next;
1139 if ( (status == SPIDER_NET_DESCR_RESPONSE_ERROR) ||
1140 (status == SPIDER_NET_DESCR_PROTECTION_ERROR) ||
1141 (status == SPIDER_NET_DESCR_FORCE_END) ) {
1142 if (netif_msg_rx_err(card))
1143 pr_err("%s: dropping RX descriptor with state %d\n",
1144 card->netdev->name, status);
1145 card->netdev_stats.rx_dropped++;
1149 if ( (status != SPIDER_NET_DESCR_COMPLETE) &&
1150 (status != SPIDER_NET_DESCR_FRAME_END) ) {
1151 if (netif_msg_rx_err(card))
1152 pr_err("%s: RX descriptor with state %d\n",
1153 card->netdev->name, status);
1157 /* ok, we've got a packet in descr */
1158 result = spider_net_pass_skb_up(descr, card);
1160 spider_net_set_descr_status(descr, SPIDER_NET_DESCR_NOT_IN_USE);
1161 /* change the descriptor state: */
1162 spider_net_refill_rx_chain(card);
1168 * spider_net_poll - NAPI poll function called by the stack to return packets
1169 * @netdev: interface device structure
1170 * @budget: number of packets we can pass to the stack at most
1172 * returns 0 if no more packets available to the driver/stack. Returns 1,
1173 * if the quota is exceeded, but the driver has still packets.
1175 * spider_net_poll returns all packets from the rx descriptors to the stack
1176 * (using netif_receive_skb). If all/enough packets are up, the driver
1177 * reenables interrupts and returns 0. If not, 1 is returned.
1180 spider_net_poll(struct net_device *netdev, int *budget)
1182 struct spider_net_card *card = netdev_priv(netdev);
1183 int packets_to_do, packets_done = 0;
1184 int no_more_packets = 0;
1186 packets_to_do = min(*budget, netdev->quota);
1188 while (packets_to_do) {
1189 if (spider_net_decode_one_descr(card)) {
1193 /* no more packets for the stack */
1194 no_more_packets = 1;
1199 netdev->quota -= packets_done;
1200 *budget -= packets_done;
1202 /* if all packets are in the stack, enable interrupts and return 0 */
1203 /* if not, return 1 */
1204 if (no_more_packets) {
1205 netif_rx_complete(netdev);
1206 spider_net_rx_irq_on(card);
1214 * spider_net_vlan_rx_reg - initializes VLAN structures in the driver and card
1215 * @netdev: interface device structure
1216 * @grp: vlan_group structure that is registered (NULL on destroying interface)
1219 spider_net_vlan_rx_reg(struct net_device *netdev, struct vlan_group *grp)
1221 /* further enhancement... yet to do */
1226 * spider_net_vlan_rx_add - adds VLAN id to the card filter
1227 * @netdev: interface device structure
1228 * @vid: VLAN id to add
1231 spider_net_vlan_rx_add(struct net_device *netdev, uint16_t vid)
1233 /* further enhancement... yet to do */
1234 /* add vid to card's VLAN filter table */
1239 * spider_net_vlan_rx_kill - removes VLAN id to the card filter
1240 * @netdev: interface device structure
1241 * @vid: VLAN id to remove
1244 spider_net_vlan_rx_kill(struct net_device *netdev, uint16_t vid)
1246 /* further enhancement... yet to do */
1247 /* remove vid from card's VLAN filter table */
1251 * spider_net_get_stats - get interface statistics
1252 * @netdev: interface device structure
1254 * returns the interface statistics residing in the spider_net_card struct
1256 static struct net_device_stats *
1257 spider_net_get_stats(struct net_device *netdev)
1259 struct spider_net_card *card = netdev_priv(netdev);
1260 struct net_device_stats *stats = &card->netdev_stats;
1265 * spider_net_change_mtu - changes the MTU of an interface
1266 * @netdev: interface device structure
1267 * @new_mtu: new MTU value
1269 * returns 0 on success, <0 on failure
1272 spider_net_change_mtu(struct net_device *netdev, int new_mtu)
1274 /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
1275 * and mtu is outbound only anyway */
1276 if ( (new_mtu < SPIDER_NET_MIN_MTU ) ||
1277 (new_mtu > SPIDER_NET_MAX_MTU) )
1279 netdev->mtu = new_mtu;
1284 * spider_net_set_mac - sets the MAC of an interface
1285 * @netdev: interface device structure
1286 * @ptr: pointer to new MAC address
1288 * Returns 0 on success, <0 on failure. Currently, we don't support this
1289 * and will always return EOPNOTSUPP.
1292 spider_net_set_mac(struct net_device *netdev, void *p)
1294 struct spider_net_card *card = netdev_priv(netdev);
1295 u32 macl, macu, regvalue;
1296 struct sockaddr *addr = p;
1298 if (!is_valid_ether_addr(addr->sa_data))
1299 return -EADDRNOTAVAIL;
1301 /* switch off GMACTPE and GMACRPE */
1302 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1303 regvalue &= ~((1 << 5) | (1 << 6));
1304 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1307 macu = (addr->sa_data[0]<<24) + (addr->sa_data[1]<<16) +
1308 (addr->sa_data[2]<<8) + (addr->sa_data[3]);
1309 macl = (addr->sa_data[4]<<8) + (addr->sa_data[5]);
1310 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACU, macu);
1311 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACL, macl);
1313 /* switch GMACTPE and GMACRPE back on */
1314 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1315 regvalue |= ((1 << 5) | (1 << 6));
1316 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1318 spider_net_set_promisc(card);
1320 /* look up, whether we have been successful */
1321 if (spider_net_get_mac_address(netdev))
1322 return -EADDRNOTAVAIL;
1323 if (memcmp(netdev->dev_addr,addr->sa_data,netdev->addr_len))
1324 return -EADDRNOTAVAIL;
1330 * spider_net_enable_txdmac - enables a TX DMA controller
1331 * @card: card structure
1333 * spider_net_enable_txdmac enables the TX DMA controller by setting the
1334 * descriptor chain tail address
1337 spider_net_enable_txdmac(struct spider_net_card *card)
1339 /* assume chain is aligned correctly */
1340 spider_net_write_reg(card, SPIDER_NET_GDTDCHA,
1341 card->tx_chain.tail->bus_addr);
1345 * spider_net_handle_error_irq - handles errors raised by an interrupt
1346 * @card: card structure
1347 * @status_reg: interrupt status register 0 (GHIINT0STS)
1349 * spider_net_handle_error_irq treats or ignores all error conditions
1350 * found when an interrupt is presented
1353 spider_net_handle_error_irq(struct spider_net_card *card, u32 status_reg)
1355 u32 error_reg1, error_reg2;
1359 error_reg1 = spider_net_read_reg(card, SPIDER_NET_GHIINT1STS);
1360 error_reg2 = spider_net_read_reg(card, SPIDER_NET_GHIINT2STS);
1362 /* check GHIINT0STS ************************************/
1364 for (i = 0; i < 32; i++)
1365 if (status_reg & (1<<i))
1368 /* let error_reg1 and error_reg2 evaluation decide, what to do
1369 case SPIDER_NET_PHYINT:
1370 case SPIDER_NET_GMAC2INT:
1371 case SPIDER_NET_GMAC1INT:
1372 case SPIDER_NET_GIPSINT:
1373 case SPIDER_NET_GFIFOINT:
1374 case SPIDER_NET_DMACINT:
1375 case SPIDER_NET_GSYSINT:
1378 case SPIDER_NET_GPWOPCMPINT:
1379 /* PHY write operation completed */
1382 case SPIDER_NET_GPROPCMPINT:
1383 /* PHY read operation completed */
1384 /* we don't use semaphores, as we poll for the completion
1385 * of the read operation in spider_net_read_phy. Should take
1389 case SPIDER_NET_GPWFFINT:
1390 /* PHY command queue full */
1391 if (netif_msg_intr(card))
1392 pr_err("PHY write queue full\n");
1396 /* case SPIDER_NET_GRMDADRINT: not used. print a message */
1397 /* case SPIDER_NET_GRMARPINT: not used. print a message */
1398 /* case SPIDER_NET_GRMMPINT: not used. print a message */
1400 case SPIDER_NET_GDTDEN0INT:
1401 /* someone has set TX_DMA_EN to 0 */
1405 case SPIDER_NET_GDDDEN0INT: /* fallthrough */
1406 case SPIDER_NET_GDCDEN0INT: /* fallthrough */
1407 case SPIDER_NET_GDBDEN0INT: /* fallthrough */
1408 case SPIDER_NET_GDADEN0INT:
1409 /* someone has set RX_DMA_EN to 0 */
1414 case SPIDER_NET_GDDFDCINT:
1415 case SPIDER_NET_GDCFDCINT:
1416 case SPIDER_NET_GDBFDCINT:
1417 case SPIDER_NET_GDAFDCINT:
1418 /* case SPIDER_NET_GDNMINT: not used. print a message */
1419 /* case SPIDER_NET_GCNMINT: not used. print a message */
1420 /* case SPIDER_NET_GBNMINT: not used. print a message */
1421 /* case SPIDER_NET_GANMINT: not used. print a message */
1422 /* case SPIDER_NET_GRFNMINT: not used. print a message */
1427 case SPIDER_NET_GDTFDCINT:
1430 case SPIDER_NET_GTTEDINT:
1433 case SPIDER_NET_GDTDCEINT:
1434 /* chain end. If a descriptor should be sent, kick off
1436 if (card->tx_chain.tail == card->tx_chain.head)
1437 spider_net_kick_tx_dma(card);
1441 /* case SPIDER_NET_G1TMCNTINT: not used. print a message */
1442 /* case SPIDER_NET_GFREECNTINT: not used. print a message */
1445 /* check GHIINT1STS ************************************/
1447 for (i = 0; i < 32; i++)
1448 if (error_reg1 & (1<<i))
1451 case SPIDER_NET_GTMFLLINT:
1452 if (netif_msg_intr(card))
1453 pr_err("Spider TX RAM full\n");
1456 case SPIDER_NET_GRMFLLINT:
1457 if (netif_msg_intr(card))
1458 pr_err("Spider RX RAM full, incoming packets "
1459 "might be discarded !\n");
1460 netif_rx_schedule(card->netdev);
1461 spider_net_enable_rxchtails(card);
1462 spider_net_enable_rxdmac(card);
1465 /* case SPIDER_NET_GTMSHTINT: problem, print a message */
1466 case SPIDER_NET_GDTINVDINT:
1467 /* allrighty. tx from previous descr ok */
1470 /* case SPIDER_NET_GRFDFLLINT: print a message down there */
1471 /* case SPIDER_NET_GRFCFLLINT: print a message down there */
1472 /* case SPIDER_NET_GRFBFLLINT: print a message down there */
1473 /* case SPIDER_NET_GRFAFLLINT: print a message down there */
1476 case SPIDER_NET_GDDDCEINT: /* fallthrough */
1477 case SPIDER_NET_GDCDCEINT: /* fallthrough */
1478 case SPIDER_NET_GDBDCEINT: /* fallthrough */
1479 case SPIDER_NET_GDADCEINT:
1480 if (netif_msg_intr(card))
1481 pr_err("got descriptor chain end interrupt, "
1482 "restarting DMAC %c.\n",
1483 'D'+i-SPIDER_NET_GDDDCEINT);
1484 spider_net_refill_rx_chain(card);
1488 /* invalid descriptor */
1489 case SPIDER_NET_GDDINVDINT: /* fallthrough */
1490 case SPIDER_NET_GDCINVDINT: /* fallthrough */
1491 case SPIDER_NET_GDBINVDINT: /* fallthrough */
1492 case SPIDER_NET_GDAINVDINT:
1493 /* could happen when rx chain is full */
1494 spider_net_refill_rx_chain(card);
1498 /* case SPIDER_NET_GDTRSERINT: problem, print a message */
1499 /* case SPIDER_NET_GDDRSERINT: problem, print a message */
1500 /* case SPIDER_NET_GDCRSERINT: problem, print a message */
1501 /* case SPIDER_NET_GDBRSERINT: problem, print a message */
1502 /* case SPIDER_NET_GDARSERINT: problem, print a message */
1503 /* case SPIDER_NET_GDSERINT: problem, print a message */
1504 /* case SPIDER_NET_GDTPTERINT: problem, print a message */
1505 /* case SPIDER_NET_GDDPTERINT: problem, print a message */
1506 /* case SPIDER_NET_GDCPTERINT: problem, print a message */
1507 /* case SPIDER_NET_GDBPTERINT: problem, print a message */
1508 /* case SPIDER_NET_GDAPTERINT: problem, print a message */
1514 /* check GHIINT2STS ************************************/
1516 for (i = 0; i < 32; i++)
1517 if (error_reg2 & (1<<i))
1520 /* there is nothing we can (want to) do at this time. Log a
1521 * message, we can switch on and off the specific values later on
1522 case SPIDER_NET_GPROPERINT:
1523 case SPIDER_NET_GMCTCRSNGINT:
1524 case SPIDER_NET_GMCTLCOLINT:
1525 case SPIDER_NET_GMCTTMOTINT:
1526 case SPIDER_NET_GMCRCAERINT:
1527 case SPIDER_NET_GMCRCALERINT:
1528 case SPIDER_NET_GMCRALNERINT:
1529 case SPIDER_NET_GMCROVRINT:
1530 case SPIDER_NET_GMCRRNTINT:
1531 case SPIDER_NET_GMCRRXERINT:
1532 case SPIDER_NET_GTITCSERINT:
1533 case SPIDER_NET_GTIFMTERINT:
1534 case SPIDER_NET_GTIPKTRVKINT:
1535 case SPIDER_NET_GTISPINGINT:
1536 case SPIDER_NET_GTISADNGINT:
1537 case SPIDER_NET_GTISPDNGINT:
1538 case SPIDER_NET_GRIFMTERINT:
1539 case SPIDER_NET_GRIPKTRVKINT:
1540 case SPIDER_NET_GRISPINGINT:
1541 case SPIDER_NET_GRISADNGINT:
1542 case SPIDER_NET_GRISPDNGINT:
1549 if ((show_error) && (netif_msg_intr(card)))
1550 pr_err("Got error interrupt, GHIINT0STS = 0x%08x, "
1551 "GHIINT1STS = 0x%08x, GHIINT2STS = 0x%08x\n",
1552 status_reg, error_reg1, error_reg2);
1554 /* clear interrupt sources */
1555 spider_net_write_reg(card, SPIDER_NET_GHIINT1STS, error_reg1);
1556 spider_net_write_reg(card, SPIDER_NET_GHIINT2STS, error_reg2);
1560 * spider_net_interrupt - interrupt handler for spider_net
1561 * @irq: interupt number
1562 * @ptr: pointer to net_device
1563 * @regs: PU registers
1565 * returns IRQ_HANDLED, if interrupt was for driver, or IRQ_NONE, if no
1566 * interrupt found raised by card.
1568 * This is the interrupt handler, that turns off
1569 * interrupts for this device and makes the stack poll the driver
1572 spider_net_interrupt(int irq, void *ptr, struct pt_regs *regs)
1574 struct net_device *netdev = ptr;
1575 struct spider_net_card *card = netdev_priv(netdev);
1578 status_reg = spider_net_read_reg(card, SPIDER_NET_GHIINT0STS);
1583 if (status_reg & SPIDER_NET_TXINT)
1584 spider_net_release_tx_chain(card, 0);
1586 if (status_reg & SPIDER_NET_RXINT ) {
1587 spider_net_rx_irq_off(card);
1588 netif_rx_schedule(netdev);
1591 /* we do this after rx and tx processing, as we want the tx chain
1592 * processed to see, whether we should restart tx dma processing */
1593 spider_net_handle_error_irq(card, status_reg);
1595 /* clear interrupt sources */
1596 spider_net_write_reg(card, SPIDER_NET_GHIINT0STS, status_reg);
1601 #ifdef CONFIG_NET_POLL_CONTROLLER
1603 * spider_net_poll_controller - artificial interrupt for netconsole etc.
1604 * @netdev: interface device structure
1606 * see Documentation/networking/netconsole.txt
1609 spider_net_poll_controller(struct net_device *netdev)
1611 disable_irq(netdev->irq);
1612 spider_net_interrupt(netdev->irq, netdev, NULL);
1613 enable_irq(netdev->irq);
1615 #endif /* CONFIG_NET_POLL_CONTROLLER */
1618 * spider_net_init_card - initializes the card
1619 * @card: card structure
1621 * spider_net_init_card initializes the card so that other registers can
1625 spider_net_init_card(struct spider_net_card *card)
1627 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1628 SPIDER_NET_CKRCTRL_STOP_VALUE);
1630 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1631 SPIDER_NET_CKRCTRL_RUN_VALUE);
1635 * spider_net_enable_card - enables the card by setting all kinds of regs
1636 * @card: card structure
1638 * spider_net_enable_card sets a lot of SMMIO registers to enable the device
1641 spider_net_enable_card(struct spider_net_card *card)
1644 /* the following array consists of (register),(value) pairs
1645 * that are set in this function. A register of 0 ends the list */
1647 { SPIDER_NET_GRESUMINTNUM, 0 },
1648 { SPIDER_NET_GREINTNUM, 0 },
1650 /* set interrupt frame number registers */
1651 /* clear the single DMA engine registers first */
1652 { SPIDER_NET_GFAFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1653 { SPIDER_NET_GFBFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1654 { SPIDER_NET_GFCFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1655 { SPIDER_NET_GFDFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1656 /* then set, what we really need */
1657 { SPIDER_NET_GFFRMNUM, SPIDER_NET_FRAMENUM_VALUE },
1659 /* timer counter registers and stuff */
1660 { SPIDER_NET_GFREECNNUM, 0 },
1661 { SPIDER_NET_GONETIMENUM, 0 },
1662 { SPIDER_NET_GTOUTFRMNUM, 0 },
1664 /* RX mode setting */
1665 { SPIDER_NET_GRXMDSET, SPIDER_NET_RXMODE_VALUE },
1666 /* TX mode setting */
1667 { SPIDER_NET_GTXMDSET, SPIDER_NET_TXMODE_VALUE },
1668 /* IPSEC mode setting */
1669 { SPIDER_NET_GIPSECINIT, SPIDER_NET_IPSECINIT_VALUE },
1671 { SPIDER_NET_GFTRESTRT, SPIDER_NET_RESTART_VALUE },
1673 { SPIDER_NET_GMRWOLCTRL, 0 },
1674 { SPIDER_NET_GTESTMD, 0 },
1676 { SPIDER_NET_GMACINTEN, 0 },
1678 /* flow control stuff */
1679 { SPIDER_NET_GMACAPAUSE, SPIDER_NET_MACAPAUSE_VALUE },
1680 { SPIDER_NET_GMACTXPAUSE, SPIDER_NET_TXPAUSE_VALUE },
1682 { SPIDER_NET_GMACBSTLMT, SPIDER_NET_BURSTLMT_VALUE },
1687 while (regs[i][0]) {
1688 spider_net_write_reg(card, regs[i][0], regs[i][1]);
1692 /* clear unicast filter table entries 1 to 14 */
1693 for (i = 1; i <= 14; i++) {
1694 spider_net_write_reg(card,
1695 SPIDER_NET_GMRUAFILnR + i * 8,
1697 spider_net_write_reg(card,
1698 SPIDER_NET_GMRUAFILnR + i * 8 + 4,
1702 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R, 0x08080000);
1704 spider_net_write_reg(card, SPIDER_NET_ECMODE, SPIDER_NET_ECMODE_VALUE);
1706 /* set chain tail adress for RX chains and
1708 spider_net_enable_rxchtails(card);
1709 spider_net_enable_rxdmac(card);
1711 spider_net_write_reg(card, SPIDER_NET_GRXDMAEN, SPIDER_NET_WOL_VALUE);
1713 /* set chain tail adress for TX chain */
1714 spider_net_enable_txdmac(card);
1716 spider_net_write_reg(card, SPIDER_NET_GMACLENLMT,
1717 SPIDER_NET_LENLMT_VALUE);
1718 spider_net_write_reg(card, SPIDER_NET_GMACMODE,
1719 SPIDER_NET_MACMODE_VALUE);
1720 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD,
1721 SPIDER_NET_OPMODE_VALUE);
1723 /* set interrupt mask registers */
1724 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK,
1725 SPIDER_NET_INT0_MASK_VALUE);
1726 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK,
1727 SPIDER_NET_INT1_MASK_VALUE);
1728 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK,
1729 SPIDER_NET_INT2_MASK_VALUE);
1733 * spider_net_open - called upon ifonfig up
1734 * @netdev: interface device structure
1736 * returns 0 on success, <0 on failure
1738 * spider_net_open allocates all the descriptors and memory needed for
1739 * operation, sets up multicast list and enables interrupts
1742 spider_net_open(struct net_device *netdev)
1744 struct spider_net_card *card = netdev_priv(netdev);
1748 if (spider_net_init_chain(card, &card->tx_chain,
1749 card->descr, tx_descriptors))
1750 goto alloc_tx_failed;
1751 if (spider_net_init_chain(card, &card->rx_chain,
1752 card->descr + tx_descriptors, rx_descriptors))
1753 goto alloc_rx_failed;
1755 /* allocate rx skbs */
1756 if (spider_net_alloc_rx_skbs(card))
1757 goto alloc_skbs_failed;
1759 spider_net_set_multi(netdev);
1761 /* further enhancement: setup hw vlan, if needed */
1764 if (request_irq(netdev->irq, spider_net_interrupt,
1765 SA_SHIRQ, netdev->name, netdev))
1766 goto register_int_failed;
1768 spider_net_enable_card(card);
1770 netif_start_queue(netdev);
1771 netif_carrier_on(netdev);
1772 netif_poll_enable(netdev);
1776 register_int_failed:
1777 spider_net_free_rx_chain_contents(card);
1779 spider_net_free_chain(card, &card->rx_chain);
1781 spider_net_free_chain(card, &card->tx_chain);
1787 * spider_net_setup_phy - setup PHY
1788 * @card: card structure
1790 * returns 0 on success, <0 on failure
1792 * spider_net_setup_phy is used as part of spider_net_probe. Sets
1793 * the PHY to 1000 Mbps
1796 spider_net_setup_phy(struct spider_net_card *card)
1798 struct mii_phy *phy = &card->phy;
1800 spider_net_write_reg(card, SPIDER_NET_GDTDMASEL,
1801 SPIDER_NET_DMASEL_VALUE);
1802 spider_net_write_reg(card, SPIDER_NET_GPCCTRL,
1803 SPIDER_NET_PHY_CTRL_VALUE);
1805 phy->dev = card->netdev;
1806 phy->mdio_read = spider_net_read_phy;
1807 phy->mdio_write = spider_net_write_phy;
1809 mii_phy_probe(phy, phy->mii_id);
1811 if (phy->def->ops->setup_forced)
1812 phy->def->ops->setup_forced(phy, SPEED_1000, DUPLEX_FULL);
1814 /* the following two writes could be moved to sungem_phy.c */
1815 /* enable fiber mode */
1816 spider_net_write_phy(card->netdev, 1, MII_NCONFIG, 0x9020);
1817 /* LEDs active in both modes, autosense prio = fiber */
1818 spider_net_write_phy(card->netdev, 1, MII_NCONFIG, 0x945f);
1820 /* switch off fibre autoneg */
1821 spider_net_write_phy(card->netdev, 1, MII_NCONFIG, 0xfc01);
1822 spider_net_write_phy(card->netdev, 1, 0x0b, 0x0004);
1824 phy->def->ops->read_link(phy);
1825 pr_info("Found %s with %i Mbps, %s-duplex.\n", phy->def->name,
1826 phy->speed, phy->duplex==1 ? "Full" : "Half");
1832 * spider_net_download_firmware - loads firmware into the adapter
1833 * @card: card structure
1834 * @firmware: firmware pointer
1836 * spider_net_download_firmware loads the firmware opened by
1837 * spider_net_init_firmware into the adapter.
1840 spider_net_download_firmware(struct spider_net_card *card,
1841 const struct firmware *firmware)
1844 u32 *fw_ptr = (u32 *)firmware->data;
1846 /* stop sequencers */
1847 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1848 SPIDER_NET_STOP_SEQ_VALUE);
1850 for (sequencer = 0; sequencer < 6; sequencer++) {
1851 spider_net_write_reg(card,
1852 SPIDER_NET_GSnPRGADR + sequencer * 8, 0);
1853 for (i = 0; i < SPIDER_NET_FIRMWARE_LEN; i++) {
1854 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1855 sequencer * 8, *fw_ptr);
1860 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1861 SPIDER_NET_RUN_SEQ_VALUE);
1865 * spider_net_init_firmware - reads in firmware parts
1866 * @card: card structure
1868 * Returns 0 on success, <0 on failure
1870 * spider_net_init_firmware opens the sequencer firmware and does some basic
1871 * checks. This function opens and releases the firmware structure. A call
1872 * to download the firmware is performed before the release.
1876 * spider_fw.bin is expected to be a file containing 6*1024*4 bytes, 4k being
1877 * the program for each sequencer. Use the command
1878 * tail -q -n +2 Seq_code1_0x088.txt Seq_code2_0x090.txt \
1879 * Seq_code3_0x098.txt Seq_code4_0x0A0.txt Seq_code5_0x0A8.txt \
1880 * Seq_code6_0x0B0.txt | xxd -r -p -c4 > spider_fw.bin
1882 * to generate spider_fw.bin, if you have sequencer programs with something
1883 * like the following contents for each sequencer:
1884 * <ONE LINE COMMENT>
1885 * <FIRST 4-BYTES-WORD FOR SEQUENCER>
1886 * <SECOND 4-BYTES-WORD FOR SEQUENCER>
1888 * <1024th 4-BYTES-WORD FOR SEQUENCER>
1891 spider_net_init_firmware(struct spider_net_card *card)
1893 const struct firmware *firmware;
1896 if (request_firmware(&firmware,
1897 SPIDER_NET_FIRMWARE_NAME, &card->pdev->dev) < 0) {
1898 if (netif_msg_probe(card))
1899 pr_err("Couldn't read in sequencer data file %s.\n",
1900 SPIDER_NET_FIRMWARE_NAME);
1905 if (firmware->size != 6 * SPIDER_NET_FIRMWARE_LEN * sizeof(u32)) {
1906 if (netif_msg_probe(card))
1907 pr_err("Invalid size of sequencer data file %s.\n",
1908 SPIDER_NET_FIRMWARE_NAME);
1912 spider_net_download_firmware(card, firmware);
1916 release_firmware(firmware);
1922 * spider_net_workaround_rxramfull - work around firmware bug
1923 * @card: card structure
1928 spider_net_workaround_rxramfull(struct spider_net_card *card)
1930 int i, sequencer = 0;
1933 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1934 SPIDER_NET_CKRCTRL_RUN_VALUE);
1936 /* empty sequencer data */
1937 for (sequencer = 0; sequencer < 6; sequencer++) {
1938 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1939 sequencer * 8, 0x0);
1940 for (i = 0; i < SPIDER_NET_FIRMWARE_LEN; i++) {
1941 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1942 sequencer * 8, 0x0);
1946 /* set sequencer operation */
1947 spider_net_write_reg(card, SPIDER_NET_GSINIT, 0x000000fe);
1950 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1951 SPIDER_NET_CKRCTRL_STOP_VALUE);
1955 * spider_net_tx_timeout_task - task scheduled by the watchdog timeout
1956 * function (to be called not under interrupt status)
1957 * @data: data, is interface device structure
1959 * called as task when tx hangs, resets interface (if interface is up)
1962 spider_net_tx_timeout_task(void *data)
1964 struct net_device *netdev = data;
1965 struct spider_net_card *card = netdev_priv(netdev);
1967 if (!(netdev->flags & IFF_UP))
1970 netif_device_detach(netdev);
1971 spider_net_stop(netdev);
1973 spider_net_workaround_rxramfull(card);
1974 spider_net_init_card(card);
1976 if (spider_net_setup_phy(card))
1978 if (spider_net_init_firmware(card))
1981 spider_net_open(netdev);
1982 spider_net_kick_tx_dma(card, card->tx_chain.head);
1983 netif_device_attach(netdev);
1986 atomic_dec(&card->tx_timeout_task_counter);
1990 * spider_net_tx_timeout - called when the tx timeout watchdog kicks in.
1991 * @netdev: interface device structure
1993 * called, if tx hangs. Schedules a task that resets the interface
1996 spider_net_tx_timeout(struct net_device *netdev)
1998 struct spider_net_card *card;
2000 card = netdev_priv(netdev);
2001 atomic_inc(&card->tx_timeout_task_counter);
2002 if (netdev->flags & IFF_UP)
2003 schedule_work(&card->tx_timeout_task);
2005 atomic_dec(&card->tx_timeout_task_counter);
2009 * spider_net_setup_netdev_ops - initialization of net_device operations
2010 * @netdev: net_device structure
2012 * fills out function pointers in the net_device structure
2015 spider_net_setup_netdev_ops(struct net_device *netdev)
2017 netdev->open = &spider_net_open;
2018 netdev->stop = &spider_net_stop;
2019 netdev->hard_start_xmit = &spider_net_xmit;
2020 netdev->get_stats = &spider_net_get_stats;
2021 netdev->set_multicast_list = &spider_net_set_multi;
2022 netdev->set_mac_address = &spider_net_set_mac;
2023 netdev->change_mtu = &spider_net_change_mtu;
2024 netdev->do_ioctl = &spider_net_do_ioctl;
2026 netdev->tx_timeout = &spider_net_tx_timeout;
2027 netdev->watchdog_timeo = SPIDER_NET_WATCHDOG_TIMEOUT;
2029 netdev->poll = &spider_net_poll;
2030 netdev->weight = SPIDER_NET_NAPI_WEIGHT;
2032 netdev->vlan_rx_register = &spider_net_vlan_rx_reg;
2033 netdev->vlan_rx_add_vid = &spider_net_vlan_rx_add;
2034 netdev->vlan_rx_kill_vid = &spider_net_vlan_rx_kill;
2035 #ifdef CONFIG_NET_POLL_CONTROLLER
2036 /* poll controller */
2037 netdev->poll_controller = &spider_net_poll_controller;
2038 #endif /* CONFIG_NET_POLL_CONTROLLER */
2040 netdev->ethtool_ops = &spider_net_ethtool_ops;
2044 * spider_net_setup_netdev - initialization of net_device
2045 * @card: card structure
2047 * Returns 0 on success or <0 on failure
2049 * spider_net_setup_netdev initializes the net_device structure
2052 spider_net_setup_netdev(struct spider_net_card *card)
2055 struct net_device *netdev = card->netdev;
2056 struct device_node *dn;
2057 struct sockaddr addr;
2060 SET_MODULE_OWNER(netdev);
2061 SET_NETDEV_DEV(netdev, &card->pdev->dev);
2063 pci_set_drvdata(card->pdev, netdev);
2064 spin_lock_init(&card->intmask_lock);
2065 netdev->irq = card->pdev->irq;
2067 card->options.rx_csum = SPIDER_NET_RX_CSUM_DEFAULT;
2069 spider_net_setup_netdev_ops(netdev);
2071 netdev->features = 0;
2072 /* some time: NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
2073 * NETIF_F_HW_VLAN_FILTER */
2075 netdev->irq = card->pdev->irq;
2077 dn = pci_device_to_OF_node(card->pdev);
2081 mac = (u8 *)get_property(dn, "local-mac-address", NULL);
2084 memcpy(addr.sa_data, mac, ETH_ALEN);
2086 result = spider_net_set_mac(netdev, &addr);
2087 if ((result) && (netif_msg_probe(card)))
2088 pr_err("Failed to set MAC address: %i\n", result);
2090 result = register_netdev(netdev);
2092 if (netif_msg_probe(card))
2093 pr_err("Couldn't register net_device: %i\n",
2098 if (netif_msg_probe(card))
2099 pr_info("Initialized device %s.\n", netdev->name);
2105 * spider_net_alloc_card - allocates net_device and card structure
2107 * returns the card structure or NULL in case of errors
2109 * the card and net_device structures are linked to each other
2111 static struct spider_net_card *
2112 spider_net_alloc_card(void)
2114 struct net_device *netdev;
2115 struct spider_net_card *card;
2118 alloc_size = sizeof (*card) +
2119 sizeof (struct spider_net_descr) * rx_descriptors +
2120 sizeof (struct spider_net_descr) * tx_descriptors;
2121 netdev = alloc_etherdev(alloc_size);
2125 card = netdev_priv(netdev);
2126 card->netdev = netdev;
2127 card->msg_enable = SPIDER_NET_DEFAULT_MSG;
2128 INIT_WORK(&card->tx_timeout_task, spider_net_tx_timeout_task, netdev);
2129 init_waitqueue_head(&card->waitq);
2130 atomic_set(&card->tx_timeout_task_counter, 0);
2136 * spider_net_undo_pci_setup - releases PCI ressources
2137 * @card: card structure
2139 * spider_net_undo_pci_setup releases the mapped regions
2142 spider_net_undo_pci_setup(struct spider_net_card *card)
2144 iounmap(card->regs);
2145 pci_release_regions(card->pdev);
2149 * spider_net_setup_pci_dev - sets up the device in terms of PCI operations
2150 * @card: card structure
2153 * Returns the card structure or NULL if any errors occur
2155 * spider_net_setup_pci_dev initializes pdev and together with the
2156 * functions called in spider_net_open configures the device so that
2157 * data can be transferred over it
2158 * The net_device structure is attached to the card structure, if the
2159 * function returns without error.
2161 static struct spider_net_card *
2162 spider_net_setup_pci_dev(struct pci_dev *pdev)
2164 struct spider_net_card *card;
2165 unsigned long mmio_start, mmio_len;
2167 if (pci_enable_device(pdev)) {
2168 pr_err("Couldn't enable PCI device\n");
2172 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
2173 pr_err("Couldn't find proper PCI device base address.\n");
2174 goto out_disable_dev;
2177 if (pci_request_regions(pdev, spider_net_driver_name)) {
2178 pr_err("Couldn't obtain PCI resources, aborting.\n");
2179 goto out_disable_dev;
2182 pci_set_master(pdev);
2184 card = spider_net_alloc_card();
2186 pr_err("Couldn't allocate net_device structure, "
2188 goto out_release_regions;
2192 /* fetch base address and length of first resource */
2193 mmio_start = pci_resource_start(pdev, 0);
2194 mmio_len = pci_resource_len(pdev, 0);
2196 card->netdev->mem_start = mmio_start;
2197 card->netdev->mem_end = mmio_start + mmio_len;
2198 card->regs = ioremap(mmio_start, mmio_len);
2201 pr_err("Couldn't obtain PCI resources, aborting.\n");
2202 goto out_release_regions;
2207 out_release_regions:
2208 pci_release_regions(pdev);
2210 pci_disable_device(pdev);
2211 pci_set_drvdata(pdev, NULL);
2216 * spider_net_probe - initialization of a device
2218 * @ent: entry in the device id list
2220 * Returns 0 on success, <0 on failure
2222 * spider_net_probe initializes pdev and registers a net_device
2223 * structure for it. After that, the device can be ifconfig'ed up
2225 static int __devinit
2226 spider_net_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2229 struct spider_net_card *card;
2231 card = spider_net_setup_pci_dev(pdev);
2235 spider_net_workaround_rxramfull(card);
2236 spider_net_init_card(card);
2238 err = spider_net_setup_phy(card);
2242 err = spider_net_init_firmware(card);
2246 err = spider_net_setup_netdev(card);
2253 spider_net_undo_pci_setup(card);
2254 free_netdev(card->netdev);
2260 * spider_net_remove - removal of a device
2263 * Returns 0 on success, <0 on failure
2265 * spider_net_remove is called to remove the device and unregisters the
2268 static void __devexit
2269 spider_net_remove(struct pci_dev *pdev)
2271 struct net_device *netdev;
2272 struct spider_net_card *card;
2274 netdev = pci_get_drvdata(pdev);
2275 card = netdev_priv(netdev);
2277 wait_event(card->waitq,
2278 atomic_read(&card->tx_timeout_task_counter) == 0);
2280 unregister_netdev(netdev);
2282 /* switch off card */
2283 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2284 SPIDER_NET_CKRCTRL_STOP_VALUE);
2285 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2286 SPIDER_NET_CKRCTRL_RUN_VALUE);
2288 spider_net_undo_pci_setup(card);
2289 free_netdev(netdev);
2292 static struct pci_driver spider_net_driver = {
2293 .name = spider_net_driver_name,
2294 .id_table = spider_net_pci_tbl,
2295 .probe = spider_net_probe,
2296 .remove = __devexit_p(spider_net_remove)
2300 * spider_net_init - init function when the driver is loaded
2302 * spider_net_init registers the device driver
2304 static int __init spider_net_init(void)
2306 if (rx_descriptors < SPIDER_NET_RX_DESCRIPTORS_MIN) {
2307 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MIN;
2308 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2310 if (rx_descriptors > SPIDER_NET_RX_DESCRIPTORS_MAX) {
2311 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MAX;
2312 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2314 if (tx_descriptors < SPIDER_NET_TX_DESCRIPTORS_MIN) {
2315 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MIN;
2316 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2318 if (tx_descriptors > SPIDER_NET_TX_DESCRIPTORS_MAX) {
2319 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MAX;
2320 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2323 return pci_register_driver(&spider_net_driver);
2327 * spider_net_cleanup - exit function when driver is unloaded
2329 * spider_net_cleanup unregisters the device driver
2331 static void __exit spider_net_cleanup(void)
2333 pci_unregister_driver(&spider_net_driver);
2336 module_init(spider_net_init);
2337 module_exit(spider_net_cleanup);