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");
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);
111 /** spider_net_write_phy - write to phy register
112 * @netdev: adapter to be written to
115 * @val: value to be written to phy register
117 * spider_net_write_phy_register writes to an arbitrary PHY
118 * register via the spider GPCWOPCMD register. We assume the queue does
119 * not run full (not more than 15 commands outstanding).
122 spider_net_write_phy(struct net_device *netdev, int mii_id,
125 struct spider_net_card *card = netdev_priv(netdev);
128 writevalue = ((u32)mii_id << 21) |
129 ((u32)reg << 16) | ((u32)val);
131 spider_net_write_reg(card, SPIDER_NET_GPCWOPCMD, writevalue);
134 /** spider_net_read_phy - read from phy register
135 * @netdev: network device to be read from
139 * Returns value read from PHY register
141 * spider_net_write_phy reads from an arbitrary PHY
142 * register via the spider GPCROPCMD register
145 spider_net_read_phy(struct net_device *netdev, int mii_id, int reg)
147 struct spider_net_card *card = netdev_priv(netdev);
150 readvalue = ((u32)mii_id << 21) | ((u32)reg << 16);
151 spider_net_write_reg(card, SPIDER_NET_GPCROPCMD, readvalue);
153 /* we don't use semaphores to wait for an SPIDER_NET_GPROPCMPINT
154 * interrupt, as we poll for the completion of the read operation
155 * in spider_net_read_phy. Should take about 50 us */
157 readvalue = spider_net_read_reg(card, SPIDER_NET_GPCROPCMD);
158 } while (readvalue & SPIDER_NET_GPREXEC);
160 readvalue &= SPIDER_NET_GPRDAT_MASK;
166 * spider_net_rx_irq_off - switch off rx irq on this spider card
167 * @card: device structure
169 * switches off rx irq by masking them out in the GHIINTnMSK register
172 spider_net_rx_irq_off(struct spider_net_card *card)
176 regvalue = SPIDER_NET_INT0_MASK_VALUE & (~SPIDER_NET_RXINT);
177 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, regvalue);
181 * spider_net_rx_irq_on - switch on rx irq on this spider card
182 * @card: device structure
184 * switches on rx irq by enabling them in the GHIINTnMSK register
187 spider_net_rx_irq_on(struct spider_net_card *card)
191 regvalue = SPIDER_NET_INT0_MASK_VALUE | SPIDER_NET_RXINT;
192 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, regvalue);
196 * spider_net_set_promisc - sets the unicast address or the promiscuous mode
197 * @card: card structure
199 * spider_net_set_promisc sets the unicast destination address filter and
200 * thus either allows for non-promisc mode or promisc mode
203 spider_net_set_promisc(struct spider_net_card *card)
206 struct net_device *netdev = card->netdev;
208 if (netdev->flags & IFF_PROMISC) {
209 /* clear destination entry 0 */
210 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR, 0);
211 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR + 0x04, 0);
212 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R,
213 SPIDER_NET_PROMISC_VALUE);
215 macu = netdev->dev_addr[0];
217 macu |= netdev->dev_addr[1];
218 memcpy(&macl, &netdev->dev_addr[2], sizeof(macl));
220 macu |= SPIDER_NET_UA_DESCR_VALUE;
221 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR, macu);
222 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR + 0x04, macl);
223 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R,
224 SPIDER_NET_NONPROMISC_VALUE);
229 * spider_net_get_mac_address - read mac address from spider card
230 * @card: device structure
232 * reads MAC address from GMACUNIMACU and GMACUNIMACL registers
235 spider_net_get_mac_address(struct net_device *netdev)
237 struct spider_net_card *card = netdev_priv(netdev);
240 macl = spider_net_read_reg(card, SPIDER_NET_GMACUNIMACL);
241 macu = spider_net_read_reg(card, SPIDER_NET_GMACUNIMACU);
243 netdev->dev_addr[0] = (macu >> 24) & 0xff;
244 netdev->dev_addr[1] = (macu >> 16) & 0xff;
245 netdev->dev_addr[2] = (macu >> 8) & 0xff;
246 netdev->dev_addr[3] = macu & 0xff;
247 netdev->dev_addr[4] = (macl >> 8) & 0xff;
248 netdev->dev_addr[5] = macl & 0xff;
250 if (!is_valid_ether_addr(&netdev->dev_addr[0]))
257 * spider_net_get_descr_status -- returns the status of a descriptor
258 * @descr: descriptor to look at
260 * returns the status as in the dmac_cmd_status field of the descriptor
263 spider_net_get_descr_status(struct spider_net_descr *descr)
265 return descr->dmac_cmd_status & SPIDER_NET_DESCR_IND_PROC_MASK;
269 * spider_net_free_chain - free descriptor chain
270 * @card: card structure
271 * @chain: address of chain
275 spider_net_free_chain(struct spider_net_card *card,
276 struct spider_net_descr_chain *chain)
278 struct spider_net_descr *descr;
280 for (descr = chain->tail; !descr->bus_addr; descr = descr->next) {
281 pci_unmap_single(card->pdev, descr->bus_addr,
282 SPIDER_NET_DESCR_SIZE, PCI_DMA_BIDIRECTIONAL);
288 * spider_net_init_chain - links descriptor chain
289 * @card: card structure
290 * @chain: address of chain
291 * @start_descr: address of descriptor array
292 * @no: number of descriptors
294 * we manage a circular list that mirrors the hardware structure,
295 * except that the hardware uses bus addresses.
297 * returns 0 on success, <0 on failure
300 spider_net_init_chain(struct spider_net_card *card,
301 struct spider_net_descr_chain *chain,
302 struct spider_net_descr *start_descr,
303 int direction, int no)
306 struct spider_net_descr *descr;
310 memset(descr, 0, sizeof(*descr) * no);
312 /* set up the hardware pointers in each descriptor */
313 for (i=0; i<no; i++, descr++) {
314 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
316 buf = pci_map_single(card->pdev, descr,
317 SPIDER_NET_DESCR_SIZE,
320 if (buf == DMA_ERROR_CODE)
323 descr->bus_addr = buf;
324 descr->next = descr + 1;
325 descr->prev = descr - 1;
328 /* do actual circular list */
329 (descr-1)->next = start_descr;
330 start_descr->prev = descr-1;
333 if (direction == PCI_DMA_FROMDEVICE)
334 for (i=0; i < no; i++, descr++)
335 descr->next_descr_addr = descr->next->bus_addr;
337 spin_lock_init(&chain->lock);
338 chain->head = start_descr;
339 chain->tail = start_descr;
345 for (i=0; i < no; i++, descr++)
347 pci_unmap_single(card->pdev, descr->bus_addr,
348 SPIDER_NET_DESCR_SIZE,
354 * spider_net_free_rx_chain_contents - frees descr contents in rx chain
355 * @card: card structure
357 * returns 0 on success, <0 on failure
360 spider_net_free_rx_chain_contents(struct spider_net_card *card)
362 struct spider_net_descr *descr;
364 descr = card->rx_chain.head;
365 while (descr->next != card->rx_chain.head) {
367 dev_kfree_skb(descr->skb);
368 pci_unmap_single(card->pdev, descr->buf_addr,
369 SPIDER_NET_MAX_FRAME,
377 * spider_net_prepare_rx_descr - reinitializes a rx descriptor
378 * @card: card structure
379 * @descr: descriptor to re-init
381 * return 0 on succes, <0 on failure
383 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
384 * Activate the descriptor state-wise
387 spider_net_prepare_rx_descr(struct spider_net_card *card,
388 struct spider_net_descr *descr)
395 /* we need to round up the buffer size to a multiple of 128 */
396 bufsize = (SPIDER_NET_MAX_FRAME + SPIDER_NET_RXBUF_ALIGN - 1) &
397 (~(SPIDER_NET_RXBUF_ALIGN - 1));
399 /* and we need to have it 128 byte aligned, therefore we allocate a
401 /* allocate an skb */
402 descr->skb = dev_alloc_skb(bufsize + SPIDER_NET_RXBUF_ALIGN - 1);
404 if (netif_msg_rx_err(card) && net_ratelimit())
405 pr_err("Not enough memory to allocate rx buffer\n");
408 descr->buf_size = bufsize;
409 descr->result_size = 0;
410 descr->valid_size = 0;
411 descr->data_status = 0;
412 descr->data_error = 0;
414 offset = ((unsigned long)descr->skb->data) &
415 (SPIDER_NET_RXBUF_ALIGN - 1);
417 skb_reserve(descr->skb, SPIDER_NET_RXBUF_ALIGN - offset);
418 /* io-mmu-map the skb */
419 buf = pci_map_single(card->pdev, descr->skb->data,
420 SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
421 descr->buf_addr = buf;
422 if (buf == DMA_ERROR_CODE) {
423 dev_kfree_skb_any(descr->skb);
424 if (netif_msg_rx_err(card) && net_ratelimit())
425 pr_err("Could not iommu-map rx buffer\n");
426 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
428 descr->dmac_cmd_status = SPIDER_NET_DESCR_CARDOWNED |
429 SPIDER_NET_DMAC_NOINTR_COMPLETE;
436 * spider_net_enable_rxchtails - sets RX dmac chain tail addresses
437 * @card: card structure
439 * spider_net_enable_rxchtails sets the RX DMAC chain tail adresses in the
440 * chip by writing to the appropriate register. DMA is enabled in
441 * spider_net_enable_rxdmac.
444 spider_net_enable_rxchtails(struct spider_net_card *card)
446 /* assume chain is aligned correctly */
447 spider_net_write_reg(card, SPIDER_NET_GDADCHA ,
448 card->rx_chain.tail->bus_addr);
452 * spider_net_enable_rxdmac - enables a receive DMA controller
453 * @card: card structure
455 * spider_net_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
456 * in the GDADMACCNTR register
459 spider_net_enable_rxdmac(struct spider_net_card *card)
462 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
463 SPIDER_NET_DMA_RX_VALUE);
467 * spider_net_refill_rx_chain - refills descriptors/skbs in the rx chains
468 * @card: card structure
470 * refills descriptors in the rx chain: allocates skbs and iommu-maps them.
473 spider_net_refill_rx_chain(struct spider_net_card *card)
475 struct spider_net_descr_chain *chain = &card->rx_chain;
478 /* one context doing the refill (and a second context seeing that
479 * and omitting it) is ok. If called by NAPI, we'll be called again
480 * as spider_net_decode_one_descr is called several times. If some
481 * interrupt calls us, the NAPI is about to clean up anyway. */
482 if (!spin_trylock_irqsave(&chain->lock, flags))
485 while (spider_net_get_descr_status(chain->head) ==
486 SPIDER_NET_DESCR_NOT_IN_USE) {
487 if (spider_net_prepare_rx_descr(card, chain->head))
489 chain->head = chain->head->next;
492 spin_unlock_irqrestore(&chain->lock, flags);
496 * spider_net_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
497 * @card: card structure
499 * returns 0 on success, <0 on failure
502 spider_net_alloc_rx_skbs(struct spider_net_card *card)
505 struct spider_net_descr_chain *chain;
509 chain = &card->rx_chain;
510 /* put at least one buffer into the chain. if this fails,
511 * we've got a problem. if not, spider_net_refill_rx_chain
512 * will do the rest at the end of this function */
513 if (spider_net_prepare_rx_descr(card, chain->head))
516 chain->head = chain->head->next;
518 /* this will allocate the rest of the rx buffers; if not, it's
519 * business as usual later on */
520 spider_net_refill_rx_chain(card);
521 spider_net_enable_rxdmac(card);
525 spider_net_free_rx_chain_contents(card);
530 * spider_net_get_multicast_hash - generates hash for multicast filter table
531 * @addr: multicast address
533 * returns the hash value.
535 * spider_net_get_multicast_hash calculates a hash value for a given multicast
536 * address, that is used to set the multicast filter tables
539 spider_net_get_multicast_hash(struct net_device *netdev, __u8 *addr)
543 char addr_for_crc[ETH_ALEN] = { 0, };
546 for (i = 0; i < ETH_ALEN * 8; i++) {
547 bit = (addr[i / 8] >> (i % 8)) & 1;
548 addr_for_crc[ETH_ALEN - 1 - i / 8] += bit << (7 - (i % 8));
551 crc = crc32_be(~0, addr_for_crc, netdev->addr_len);
562 * spider_net_set_multi - sets multicast addresses and promisc flags
563 * @netdev: interface device structure
565 * spider_net_set_multi configures multicast addresses as needed for the
566 * netdev interface. It also sets up multicast, allmulti and promisc
567 * flags appropriately
570 spider_net_set_multi(struct net_device *netdev)
572 struct dev_mc_list *mc;
576 struct spider_net_card *card = netdev_priv(netdev);
577 unsigned long bitmask[SPIDER_NET_MULTICAST_HASHES / BITS_PER_LONG] =
580 spider_net_set_promisc(card);
582 if (netdev->flags & IFF_ALLMULTI) {
583 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES; i++) {
589 /* well, we know, what the broadcast hash value is: it's xfd
590 hash = spider_net_get_multicast_hash(netdev, netdev->broadcast); */
591 set_bit(0xfd, bitmask);
593 for (mc = netdev->mc_list; mc; mc = mc->next) {
594 hash = spider_net_get_multicast_hash(netdev, mc->dmi_addr);
595 set_bit(hash, bitmask);
599 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES / 4; i++) {
601 if (test_bit(i * 4, bitmask))
604 if (test_bit(i * 4 + 1, bitmask))
607 if (test_bit(i * 4 + 2, bitmask))
610 if (test_bit(i * 4 + 3, bitmask))
613 spider_net_write_reg(card, SPIDER_NET_GMRMHFILnR + i * 4, reg);
618 * spider_net_disable_rxdmac - disables the receive DMA controller
619 * @card: card structure
621 * spider_net_disable_rxdmac terminates processing on the DMA controller by
622 * turing off DMA and issueing a force end
625 spider_net_disable_rxdmac(struct spider_net_card *card)
627 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
628 SPIDER_NET_DMA_RX_FEND_VALUE);
632 * spider_net_prepare_tx_descr - fill tx descriptor with skb data
633 * @card: card structure
634 * @descr: descriptor structure to fill out
635 * @skb: packet to use
637 * returns 0 on success, <0 on failure.
639 * fills out the descriptor structure with skb data and len. Copies data,
640 * if needed (32bit DMA!)
643 spider_net_prepare_tx_descr(struct spider_net_card *card,
646 struct spider_net_descr *descr = card->tx_chain.head;
649 buf = pci_map_single(card->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
650 if (buf == DMA_ERROR_CODE) {
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);
657 descr->buf_addr = buf;
658 descr->buf_size = skb->len;
659 descr->next_descr_addr = 0;
661 descr->data_status = 0;
663 descr->dmac_cmd_status =
664 SPIDER_NET_DESCR_CARDOWNED | SPIDER_NET_DMAC_NOCS;
665 if (skb->protocol == htons(ETH_P_IP))
666 switch (skb->nh.iph->protocol) {
668 descr->dmac_cmd_status |= SPIDER_NET_DMAC_TCP;
671 descr->dmac_cmd_status |= SPIDER_NET_DMAC_UDP;
675 descr->prev->next_descr_addr = descr->bus_addr;
681 * spider_net_release_tx_descr - processes a used tx descriptor
682 * @card: card structure
683 * @descr: descriptor to release
685 * releases a used tx descriptor (unmapping, freeing of skb)
688 spider_net_release_tx_descr(struct spider_net_card *card)
690 struct spider_net_descr *descr = card->tx_chain.tail;
693 card->tx_chain.tail = card->tx_chain.tail->next;
694 descr->dmac_cmd_status |= SPIDER_NET_DESCR_NOT_IN_USE;
698 pci_unmap_single(card->pdev, descr->buf_addr, skb->len,
700 dev_kfree_skb_any(skb);
704 * spider_net_release_tx_chain - processes sent tx descriptors
705 * @card: adapter structure
706 * @brutal: if set, don't care about whether descriptor seems to be in use
708 * returns 0 if the tx ring is empty, otherwise 1.
710 * spider_net_release_tx_chain releases the tx descriptors that spider has
711 * finished with (if non-brutal) or simply release tx descriptors (if brutal).
712 * If some other context is calling this function, we return 1 so that we're
713 * scheduled again (if we were scheduled) and will not loose initiative.
716 spider_net_release_tx_chain(struct spider_net_card *card, int brutal)
718 struct spider_net_descr_chain *chain = &card->tx_chain;
721 spider_net_read_reg(card, SPIDER_NET_GDTDMACCNTR);
723 while (chain->tail != chain->head) {
724 status = spider_net_get_descr_status(chain->tail);
726 case SPIDER_NET_DESCR_COMPLETE:
727 card->netdev_stats.tx_packets++;
728 card->netdev_stats.tx_bytes += chain->tail->skb->len;
731 case SPIDER_NET_DESCR_CARDOWNED:
734 /* fallthrough, if we release the descriptors
735 * brutally (then we don't care about
736 * SPIDER_NET_DESCR_CARDOWNED) */
738 case SPIDER_NET_DESCR_RESPONSE_ERROR:
739 case SPIDER_NET_DESCR_PROTECTION_ERROR:
740 case SPIDER_NET_DESCR_FORCE_END:
741 if (netif_msg_tx_err(card))
742 pr_err("%s: forcing end of tx descriptor "
743 "with status x%02x\n",
744 card->netdev->name, status);
745 card->netdev_stats.tx_errors++;
749 card->netdev_stats.tx_dropped++;
752 spider_net_release_tx_descr(card);
759 * spider_net_kick_tx_dma - enables TX DMA processing
760 * @card: card structure
761 * @descr: descriptor address to enable TX processing at
763 * spider_net_kick_tx_dma writes the current tx chain head as start address
764 * of the tx descriptor chain and enables the transmission DMA engine
767 spider_net_kick_tx_dma(struct spider_net_card *card)
769 struct spider_net_descr *descr;
771 if (spider_net_read_reg(card, SPIDER_NET_GDTDMACCNTR) &
772 SPIDER_NET_TX_DMA_EN)
775 descr = card->tx_chain.tail;
777 if (spider_net_get_descr_status(descr) ==
778 SPIDER_NET_DESCR_CARDOWNED) {
779 spider_net_write_reg(card, SPIDER_NET_GDTDCHA,
781 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
782 SPIDER_NET_DMA_TX_VALUE);
785 if (descr == card->tx_chain.head)
791 mod_timer(&card->tx_timer, jiffies + SPIDER_NET_TX_TIMER);
795 * spider_net_xmit - transmits a frame over the device
796 * @skb: packet to send out
797 * @netdev: interface device structure
799 * returns 0 on success, !0 on failure
802 spider_net_xmit(struct sk_buff *skb, struct net_device *netdev)
804 struct spider_net_card *card = netdev_priv(netdev);
805 struct spider_net_descr_chain *chain = &card->tx_chain;
806 struct spider_net_descr *descr = chain->head;
810 spin_lock_irqsave(&chain->lock, flags);
812 spider_net_release_tx_chain(card, 0);
814 if (chain->head->next == chain->tail->prev) {
815 card->netdev_stats.tx_dropped++;
816 result = NETDEV_TX_LOCKED;
820 if (spider_net_get_descr_status(descr) != SPIDER_NET_DESCR_NOT_IN_USE) {
821 result = NETDEV_TX_LOCKED;
825 if (spider_net_prepare_tx_descr(card, skb) != 0) {
826 card->netdev_stats.tx_dropped++;
827 result = NETDEV_TX_BUSY;
831 result = NETDEV_TX_OK;
833 spider_net_kick_tx_dma(card);
834 card->tx_chain.head = card->tx_chain.head->next;
837 spin_unlock_irqrestore(&chain->lock, flags);
838 netif_wake_queue(netdev);
843 * spider_net_cleanup_tx_ring - cleans up the TX ring
844 * @card: card structure
846 * spider_net_cleanup_tx_ring is called by the tx_timer (as we don't use
847 * interrupts to cleanup our TX ring) and returns sent packets to the stack
851 spider_net_cleanup_tx_ring(struct spider_net_card *card)
855 spin_lock_irqsave(&card->tx_chain.lock, flags);
857 if ((spider_net_release_tx_chain(card, 0) != 0) &&
858 (card->netdev->flags & IFF_UP))
859 spider_net_kick_tx_dma(card);
861 spin_unlock_irqrestore(&card->tx_chain.lock, flags);
865 * spider_net_do_ioctl - called for device ioctls
866 * @netdev: interface device structure
867 * @ifr: request parameter structure for ioctl
868 * @cmd: command code for ioctl
870 * returns 0 on success, <0 on failure. Currently, we have no special ioctls.
871 * -EOPNOTSUPP is returned, if an unknown ioctl was requested
874 spider_net_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
883 * spider_net_pass_skb_up - takes an skb from a descriptor and passes it on
884 * @descr: descriptor to process
885 * @card: card structure
886 * @napi: whether caller is in NAPI context
888 * returns 1 on success, 0 if no packet was passed to the stack
890 * iommu-unmaps the skb, fills out skb structure and passes the data to the
891 * stack. The descriptor state is not changed.
894 spider_net_pass_skb_up(struct spider_net_descr *descr,
895 struct spider_net_card *card, int napi)
898 struct net_device *netdev;
899 u32 data_status, data_error;
901 data_status = descr->data_status;
902 data_error = descr->data_error;
904 netdev = card->netdev;
906 /* unmap descriptor */
907 pci_unmap_single(card->pdev, descr->buf_addr, SPIDER_NET_MAX_FRAME,
910 /* the cases we'll throw away the packet immediately */
911 if (data_error & SPIDER_NET_DESTROY_RX_FLAGS) {
912 if (netif_msg_rx_err(card))
913 pr_err("error in received descriptor found, "
914 "data_status=x%08x, data_error=x%08x\n",
915 data_status, data_error);
921 skb_put(skb, descr->valid_size);
923 /* the card seems to add 2 bytes of junk in front
924 * of the ethernet frame */
925 #define SPIDER_MISALIGN 2
926 skb_pull(skb, SPIDER_MISALIGN);
927 skb->protocol = eth_type_trans(skb, netdev);
929 /* checksum offload */
930 if (card->options.rx_csum) {
931 if ( ( (data_status & SPIDER_NET_DATA_STATUS_CKSUM_MASK) ==
932 SPIDER_NET_DATA_STATUS_CKSUM_MASK) &&
933 !(data_error & SPIDER_NET_DATA_ERR_CKSUM_MASK))
934 skb->ip_summed = CHECKSUM_UNNECESSARY;
936 skb->ip_summed = CHECKSUM_NONE;
938 skb->ip_summed = CHECKSUM_NONE;
940 if (data_status & SPIDER_NET_VLAN_PACKET) {
941 /* further enhancements: HW-accel VLAN
942 * vlan_hwaccel_receive_skb
946 /* pass skb up to stack */
948 netif_receive_skb(skb);
952 /* update netdevice statistics */
953 card->netdev_stats.rx_packets++;
954 card->netdev_stats.rx_bytes += skb->len;
960 * spider_net_decode_one_descr - processes an rx descriptor
961 * @card: card structure
962 * @napi: whether caller is in NAPI context
964 * returns 1 if a packet has been sent to the stack, otherwise 0
966 * processes an rx descriptor by iommu-unmapping the data buffer and passing
967 * the packet up to the stack. This function is called in softirq
968 * context, e.g. either bottom half from interrupt or NAPI polling context
971 spider_net_decode_one_descr(struct spider_net_card *card, int napi)
973 struct spider_net_descr_chain *chain = &card->rx_chain;
974 struct spider_net_descr *descr = chain->tail;
978 status = spider_net_get_descr_status(descr);
980 if (status == SPIDER_NET_DESCR_CARDOWNED) {
981 /* nothing in the descriptor yet */
986 if (status == SPIDER_NET_DESCR_NOT_IN_USE) {
987 /* not initialized yet, the ring must be empty */
988 spider_net_refill_rx_chain(card);
989 spider_net_enable_rxdmac(card);
994 /* descriptor definitively used -- move on tail */
995 chain->tail = descr->next;
998 if ( (status == SPIDER_NET_DESCR_RESPONSE_ERROR) ||
999 (status == SPIDER_NET_DESCR_PROTECTION_ERROR) ||
1000 (status == SPIDER_NET_DESCR_FORCE_END) ) {
1001 if (netif_msg_rx_err(card))
1002 pr_err("%s: dropping RX descriptor with state %d\n",
1003 card->netdev->name, status);
1004 card->netdev_stats.rx_dropped++;
1005 pci_unmap_single(card->pdev, descr->buf_addr,
1006 SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
1007 dev_kfree_skb_irq(descr->skb);
1011 if ( (status != SPIDER_NET_DESCR_COMPLETE) &&
1012 (status != SPIDER_NET_DESCR_FRAME_END) ) {
1013 if (netif_msg_rx_err(card))
1014 pr_err("%s: RX descriptor with state %d\n",
1015 card->netdev->name, status);
1019 /* ok, we've got a packet in descr */
1020 result = spider_net_pass_skb_up(descr, card, napi);
1022 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
1023 /* change the descriptor state: */
1025 spider_net_refill_rx_chain(card);
1031 * spider_net_poll - NAPI poll function called by the stack to return packets
1032 * @netdev: interface device structure
1033 * @budget: number of packets we can pass to the stack at most
1035 * returns 0 if no more packets available to the driver/stack. Returns 1,
1036 * if the quota is exceeded, but the driver has still packets.
1038 * spider_net_poll returns all packets from the rx descriptors to the stack
1039 * (using netif_receive_skb). If all/enough packets are up, the driver
1040 * reenables interrupts and returns 0. If not, 1 is returned.
1043 spider_net_poll(struct net_device *netdev, int *budget)
1045 struct spider_net_card *card = netdev_priv(netdev);
1046 int packets_to_do, packets_done = 0;
1047 int no_more_packets = 0;
1049 packets_to_do = min(*budget, netdev->quota);
1051 while (packets_to_do) {
1052 if (spider_net_decode_one_descr(card, 1)) {
1056 /* no more packets for the stack */
1057 no_more_packets = 1;
1062 netdev->quota -= packets_done;
1063 *budget -= packets_done;
1064 spider_net_refill_rx_chain(card);
1066 /* if all packets are in the stack, enable interrupts and return 0 */
1067 /* if not, return 1 */
1068 if (no_more_packets) {
1069 netif_rx_complete(netdev);
1070 spider_net_rx_irq_on(card);
1078 * spider_net_vlan_rx_reg - initializes VLAN structures in the driver and card
1079 * @netdev: interface device structure
1080 * @grp: vlan_group structure that is registered (NULL on destroying interface)
1083 spider_net_vlan_rx_reg(struct net_device *netdev, struct vlan_group *grp)
1085 /* further enhancement... yet to do */
1090 * spider_net_vlan_rx_add - adds VLAN id to the card filter
1091 * @netdev: interface device structure
1092 * @vid: VLAN id to add
1095 spider_net_vlan_rx_add(struct net_device *netdev, uint16_t vid)
1097 /* further enhancement... yet to do */
1098 /* add vid to card's VLAN filter table */
1103 * spider_net_vlan_rx_kill - removes VLAN id to the card filter
1104 * @netdev: interface device structure
1105 * @vid: VLAN id to remove
1108 spider_net_vlan_rx_kill(struct net_device *netdev, uint16_t vid)
1110 /* further enhancement... yet to do */
1111 /* remove vid from card's VLAN filter table */
1115 * spider_net_get_stats - get interface statistics
1116 * @netdev: interface device structure
1118 * returns the interface statistics residing in the spider_net_card struct
1120 static struct net_device_stats *
1121 spider_net_get_stats(struct net_device *netdev)
1123 struct spider_net_card *card = netdev_priv(netdev);
1124 struct net_device_stats *stats = &card->netdev_stats;
1129 * spider_net_change_mtu - changes the MTU of an interface
1130 * @netdev: interface device structure
1131 * @new_mtu: new MTU value
1133 * returns 0 on success, <0 on failure
1136 spider_net_change_mtu(struct net_device *netdev, int new_mtu)
1138 /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
1139 * and mtu is outbound only anyway */
1140 if ( (new_mtu < SPIDER_NET_MIN_MTU ) ||
1141 (new_mtu > SPIDER_NET_MAX_MTU) )
1143 netdev->mtu = new_mtu;
1148 * spider_net_set_mac - sets the MAC of an interface
1149 * @netdev: interface device structure
1150 * @ptr: pointer to new MAC address
1152 * Returns 0 on success, <0 on failure. Currently, we don't support this
1153 * and will always return EOPNOTSUPP.
1156 spider_net_set_mac(struct net_device *netdev, void *p)
1158 struct spider_net_card *card = netdev_priv(netdev);
1159 u32 macl, macu, regvalue;
1160 struct sockaddr *addr = p;
1162 if (!is_valid_ether_addr(addr->sa_data))
1163 return -EADDRNOTAVAIL;
1165 /* switch off GMACTPE and GMACRPE */
1166 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1167 regvalue &= ~((1 << 5) | (1 << 6));
1168 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1171 macu = (addr->sa_data[0]<<24) + (addr->sa_data[1]<<16) +
1172 (addr->sa_data[2]<<8) + (addr->sa_data[3]);
1173 macl = (addr->sa_data[4]<<8) + (addr->sa_data[5]);
1174 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACU, macu);
1175 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACL, macl);
1177 /* switch GMACTPE and GMACRPE back on */
1178 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1179 regvalue |= ((1 << 5) | (1 << 6));
1180 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1182 spider_net_set_promisc(card);
1184 /* look up, whether we have been successful */
1185 if (spider_net_get_mac_address(netdev))
1186 return -EADDRNOTAVAIL;
1187 if (memcmp(netdev->dev_addr,addr->sa_data,netdev->addr_len))
1188 return -EADDRNOTAVAIL;
1194 * spider_net_handle_rxram_full - cleans up RX ring upon RX RAM full interrupt
1195 * @card: card structure
1197 * spider_net_handle_rxram_full empties the RX ring so that spider can put
1198 * more packets in it and empty its RX RAM. This is called in bottom half
1202 spider_net_handle_rxram_full(struct spider_net_card *card)
1204 while (spider_net_decode_one_descr(card, 0))
1206 spider_net_enable_rxchtails(card);
1207 spider_net_enable_rxdmac(card);
1208 netif_rx_schedule(card->netdev);
1212 * spider_net_handle_error_irq - handles errors raised by an interrupt
1213 * @card: card structure
1214 * @status_reg: interrupt status register 0 (GHIINT0STS)
1216 * spider_net_handle_error_irq treats or ignores all error conditions
1217 * found when an interrupt is presented
1220 spider_net_handle_error_irq(struct spider_net_card *card, u32 status_reg)
1222 u32 error_reg1, error_reg2;
1226 error_reg1 = spider_net_read_reg(card, SPIDER_NET_GHIINT1STS);
1227 error_reg2 = spider_net_read_reg(card, SPIDER_NET_GHIINT2STS);
1229 /* check GHIINT0STS ************************************/
1231 for (i = 0; i < 32; i++)
1232 if (status_reg & (1<<i))
1235 /* let error_reg1 and error_reg2 evaluation decide, what to do
1236 case SPIDER_NET_PHYINT:
1237 case SPIDER_NET_GMAC2INT:
1238 case SPIDER_NET_GMAC1INT:
1239 case SPIDER_NET_GIPSINT:
1240 case SPIDER_NET_GFIFOINT:
1241 case SPIDER_NET_DMACINT:
1242 case SPIDER_NET_GSYSINT:
1245 case SPIDER_NET_GPWOPCMPINT:
1246 /* PHY write operation completed */
1249 case SPIDER_NET_GPROPCMPINT:
1250 /* PHY read operation completed */
1251 /* we don't use semaphores, as we poll for the completion
1252 * of the read operation in spider_net_read_phy. Should take
1256 case SPIDER_NET_GPWFFINT:
1257 /* PHY command queue full */
1258 if (netif_msg_intr(card))
1259 pr_err("PHY write queue full\n");
1263 /* case SPIDER_NET_GRMDADRINT: not used. print a message */
1264 /* case SPIDER_NET_GRMARPINT: not used. print a message */
1265 /* case SPIDER_NET_GRMMPINT: not used. print a message */
1267 case SPIDER_NET_GDTDEN0INT:
1268 /* someone has set TX_DMA_EN to 0 */
1272 case SPIDER_NET_GDDDEN0INT: /* fallthrough */
1273 case SPIDER_NET_GDCDEN0INT: /* fallthrough */
1274 case SPIDER_NET_GDBDEN0INT: /* fallthrough */
1275 case SPIDER_NET_GDADEN0INT:
1276 /* someone has set RX_DMA_EN to 0 */
1281 case SPIDER_NET_GDDFDCINT:
1282 case SPIDER_NET_GDCFDCINT:
1283 case SPIDER_NET_GDBFDCINT:
1284 case SPIDER_NET_GDAFDCINT:
1285 /* case SPIDER_NET_GDNMINT: not used. print a message */
1286 /* case SPIDER_NET_GCNMINT: not used. print a message */
1287 /* case SPIDER_NET_GBNMINT: not used. print a message */
1288 /* case SPIDER_NET_GANMINT: not used. print a message */
1289 /* case SPIDER_NET_GRFNMINT: not used. print a message */
1294 case SPIDER_NET_GDTFDCINT:
1297 case SPIDER_NET_GTTEDINT:
1300 case SPIDER_NET_GDTDCEINT:
1301 /* chain end. If a descriptor should be sent, kick off
1303 if (card->tx_chain.tail == card->tx_chain.head)
1304 spider_net_kick_tx_dma(card);
1308 /* case SPIDER_NET_G1TMCNTINT: not used. print a message */
1309 /* case SPIDER_NET_GFREECNTINT: not used. print a message */
1312 /* check GHIINT1STS ************************************/
1314 for (i = 0; i < 32; i++)
1315 if (error_reg1 & (1<<i))
1318 case SPIDER_NET_GTMFLLINT:
1319 if (netif_msg_intr(card) && net_ratelimit())
1320 pr_err("Spider TX RAM full\n");
1323 case SPIDER_NET_GRFDFLLINT: /* fallthrough */
1324 case SPIDER_NET_GRFCFLLINT: /* fallthrough */
1325 case SPIDER_NET_GRFBFLLINT: /* fallthrough */
1326 case SPIDER_NET_GRFAFLLINT: /* fallthrough */
1327 case SPIDER_NET_GRMFLLINT:
1328 if (netif_msg_intr(card) && net_ratelimit())
1329 pr_debug("Spider RX RAM full, incoming packets "
1330 "might be discarded!\n");
1331 spider_net_rx_irq_off(card);
1332 tasklet_schedule(&card->rxram_full_tl);
1336 /* case SPIDER_NET_GTMSHTINT: problem, print a message */
1337 case SPIDER_NET_GDTINVDINT:
1338 /* allrighty. tx from previous descr ok */
1343 case SPIDER_NET_GDDDCEINT: /* fallthrough */
1344 case SPIDER_NET_GDCDCEINT: /* fallthrough */
1345 case SPIDER_NET_GDBDCEINT: /* fallthrough */
1346 case SPIDER_NET_GDADCEINT:
1347 if (netif_msg_intr(card))
1348 pr_err("got descriptor chain end interrupt, "
1349 "restarting DMAC %c.\n",
1350 'D'+i-SPIDER_NET_GDDDCEINT);
1351 spider_net_refill_rx_chain(card);
1352 spider_net_enable_rxdmac(card);
1356 /* invalid descriptor */
1357 case SPIDER_NET_GDDINVDINT: /* fallthrough */
1358 case SPIDER_NET_GDCINVDINT: /* fallthrough */
1359 case SPIDER_NET_GDBINVDINT: /* fallthrough */
1360 case SPIDER_NET_GDAINVDINT:
1361 /* could happen when rx chain is full */
1362 spider_net_refill_rx_chain(card);
1363 spider_net_enable_rxdmac(card);
1367 /* case SPIDER_NET_GDTRSERINT: problem, print a message */
1368 /* case SPIDER_NET_GDDRSERINT: problem, print a message */
1369 /* case SPIDER_NET_GDCRSERINT: problem, print a message */
1370 /* case SPIDER_NET_GDBRSERINT: problem, print a message */
1371 /* case SPIDER_NET_GDARSERINT: problem, print a message */
1372 /* case SPIDER_NET_GDSERINT: problem, print a message */
1373 /* case SPIDER_NET_GDTPTERINT: problem, print a message */
1374 /* case SPIDER_NET_GDDPTERINT: problem, print a message */
1375 /* case SPIDER_NET_GDCPTERINT: problem, print a message */
1376 /* case SPIDER_NET_GDBPTERINT: problem, print a message */
1377 /* case SPIDER_NET_GDAPTERINT: problem, print a message */
1383 /* check GHIINT2STS ************************************/
1385 for (i = 0; i < 32; i++)
1386 if (error_reg2 & (1<<i))
1389 /* there is nothing we can (want to) do at this time. Log a
1390 * message, we can switch on and off the specific values later on
1391 case SPIDER_NET_GPROPERINT:
1392 case SPIDER_NET_GMCTCRSNGINT:
1393 case SPIDER_NET_GMCTLCOLINT:
1394 case SPIDER_NET_GMCTTMOTINT:
1395 case SPIDER_NET_GMCRCAERINT:
1396 case SPIDER_NET_GMCRCALERINT:
1397 case SPIDER_NET_GMCRALNERINT:
1398 case SPIDER_NET_GMCROVRINT:
1399 case SPIDER_NET_GMCRRNTINT:
1400 case SPIDER_NET_GMCRRXERINT:
1401 case SPIDER_NET_GTITCSERINT:
1402 case SPIDER_NET_GTIFMTERINT:
1403 case SPIDER_NET_GTIPKTRVKINT:
1404 case SPIDER_NET_GTISPINGINT:
1405 case SPIDER_NET_GTISADNGINT:
1406 case SPIDER_NET_GTISPDNGINT:
1407 case SPIDER_NET_GRIFMTERINT:
1408 case SPIDER_NET_GRIPKTRVKINT:
1409 case SPIDER_NET_GRISPINGINT:
1410 case SPIDER_NET_GRISADNGINT:
1411 case SPIDER_NET_GRISPDNGINT:
1418 if ((show_error) && (netif_msg_intr(card)))
1419 pr_err("Got error interrupt, GHIINT0STS = 0x%08x, "
1420 "GHIINT1STS = 0x%08x, GHIINT2STS = 0x%08x\n",
1421 status_reg, error_reg1, error_reg2);
1423 /* clear interrupt sources */
1424 spider_net_write_reg(card, SPIDER_NET_GHIINT1STS, error_reg1);
1425 spider_net_write_reg(card, SPIDER_NET_GHIINT2STS, error_reg2);
1429 * spider_net_interrupt - interrupt handler for spider_net
1430 * @irq: interupt number
1431 * @ptr: pointer to net_device
1432 * @regs: PU registers
1434 * returns IRQ_HANDLED, if interrupt was for driver, or IRQ_NONE, if no
1435 * interrupt found raised by card.
1437 * This is the interrupt handler, that turns off
1438 * interrupts for this device and makes the stack poll the driver
1441 spider_net_interrupt(int irq, void *ptr, struct pt_regs *regs)
1443 struct net_device *netdev = ptr;
1444 struct spider_net_card *card = netdev_priv(netdev);
1447 status_reg = spider_net_read_reg(card, SPIDER_NET_GHIINT0STS);
1452 if (status_reg & SPIDER_NET_RXINT ) {
1453 spider_net_rx_irq_off(card);
1454 netif_rx_schedule(netdev);
1457 if (status_reg & SPIDER_NET_ERRINT )
1458 spider_net_handle_error_irq(card, status_reg);
1460 /* clear interrupt sources */
1461 spider_net_write_reg(card, SPIDER_NET_GHIINT0STS, status_reg);
1466 #ifdef CONFIG_NET_POLL_CONTROLLER
1468 * spider_net_poll_controller - artificial interrupt for netconsole etc.
1469 * @netdev: interface device structure
1471 * see Documentation/networking/netconsole.txt
1474 spider_net_poll_controller(struct net_device *netdev)
1476 disable_irq(netdev->irq);
1477 spider_net_interrupt(netdev->irq, netdev, NULL);
1478 enable_irq(netdev->irq);
1480 #endif /* CONFIG_NET_POLL_CONTROLLER */
1483 * spider_net_init_card - initializes the card
1484 * @card: card structure
1486 * spider_net_init_card initializes the card so that other registers can
1490 spider_net_init_card(struct spider_net_card *card)
1492 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1493 SPIDER_NET_CKRCTRL_STOP_VALUE);
1495 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1496 SPIDER_NET_CKRCTRL_RUN_VALUE);
1500 * spider_net_enable_card - enables the card by setting all kinds of regs
1501 * @card: card structure
1503 * spider_net_enable_card sets a lot of SMMIO registers to enable the device
1506 spider_net_enable_card(struct spider_net_card *card)
1509 /* the following array consists of (register),(value) pairs
1510 * that are set in this function. A register of 0 ends the list */
1512 { SPIDER_NET_GRESUMINTNUM, 0 },
1513 { SPIDER_NET_GREINTNUM, 0 },
1515 /* set interrupt frame number registers */
1516 /* clear the single DMA engine registers first */
1517 { SPIDER_NET_GFAFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1518 { SPIDER_NET_GFBFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1519 { SPIDER_NET_GFCFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1520 { SPIDER_NET_GFDFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1521 /* then set, what we really need */
1522 { SPIDER_NET_GFFRMNUM, SPIDER_NET_FRAMENUM_VALUE },
1524 /* timer counter registers and stuff */
1525 { SPIDER_NET_GFREECNNUM, 0 },
1526 { SPIDER_NET_GONETIMENUM, 0 },
1527 { SPIDER_NET_GTOUTFRMNUM, 0 },
1529 /* RX mode setting */
1530 { SPIDER_NET_GRXMDSET, SPIDER_NET_RXMODE_VALUE },
1531 /* TX mode setting */
1532 { SPIDER_NET_GTXMDSET, SPIDER_NET_TXMODE_VALUE },
1533 /* IPSEC mode setting */
1534 { SPIDER_NET_GIPSECINIT, SPIDER_NET_IPSECINIT_VALUE },
1536 { SPIDER_NET_GFTRESTRT, SPIDER_NET_RESTART_VALUE },
1538 { SPIDER_NET_GMRWOLCTRL, 0 },
1539 { SPIDER_NET_GTESTMD, 0x10000000 },
1540 { SPIDER_NET_GTTQMSK, 0x00400040 },
1542 { SPIDER_NET_GMACINTEN, 0 },
1544 /* flow control stuff */
1545 { SPIDER_NET_GMACAPAUSE, SPIDER_NET_MACAPAUSE_VALUE },
1546 { SPIDER_NET_GMACTXPAUSE, SPIDER_NET_TXPAUSE_VALUE },
1548 { SPIDER_NET_GMACBSTLMT, SPIDER_NET_BURSTLMT_VALUE },
1553 while (regs[i][0]) {
1554 spider_net_write_reg(card, regs[i][0], regs[i][1]);
1558 /* clear unicast filter table entries 1 to 14 */
1559 for (i = 1; i <= 14; i++) {
1560 spider_net_write_reg(card,
1561 SPIDER_NET_GMRUAFILnR + i * 8,
1563 spider_net_write_reg(card,
1564 SPIDER_NET_GMRUAFILnR + i * 8 + 4,
1568 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R, 0x08080000);
1570 spider_net_write_reg(card, SPIDER_NET_ECMODE, SPIDER_NET_ECMODE_VALUE);
1572 /* set chain tail adress for RX chains and
1574 spider_net_enable_rxchtails(card);
1575 spider_net_enable_rxdmac(card);
1577 spider_net_write_reg(card, SPIDER_NET_GRXDMAEN, SPIDER_NET_WOL_VALUE);
1579 spider_net_write_reg(card, SPIDER_NET_GMACLENLMT,
1580 SPIDER_NET_LENLMT_VALUE);
1581 spider_net_write_reg(card, SPIDER_NET_GMACMODE,
1582 SPIDER_NET_MACMODE_VALUE);
1583 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD,
1584 SPIDER_NET_OPMODE_VALUE);
1586 /* set interrupt mask registers */
1587 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK,
1588 SPIDER_NET_INT0_MASK_VALUE);
1589 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK,
1590 SPIDER_NET_INT1_MASK_VALUE);
1591 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK,
1592 SPIDER_NET_INT2_MASK_VALUE);
1594 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
1595 SPIDER_NET_GDTDCEIDIS);
1599 * spider_net_open - called upon ifonfig up
1600 * @netdev: interface device structure
1602 * returns 0 on success, <0 on failure
1604 * spider_net_open allocates all the descriptors and memory needed for
1605 * operation, sets up multicast list and enables interrupts
1608 spider_net_open(struct net_device *netdev)
1610 struct spider_net_card *card = netdev_priv(netdev);
1614 if (spider_net_init_chain(card, &card->tx_chain, card->descr,
1615 PCI_DMA_TODEVICE, card->tx_desc))
1616 goto alloc_tx_failed;
1617 if (spider_net_init_chain(card, &card->rx_chain,
1618 card->descr + card->rx_desc,
1619 PCI_DMA_FROMDEVICE, card->rx_desc))
1620 goto alloc_rx_failed;
1622 /* allocate rx skbs */
1623 if (spider_net_alloc_rx_skbs(card))
1624 goto alloc_skbs_failed;
1626 spider_net_set_multi(netdev);
1628 /* further enhancement: setup hw vlan, if needed */
1631 if (request_irq(netdev->irq, spider_net_interrupt,
1632 IRQF_SHARED, netdev->name, netdev))
1633 goto register_int_failed;
1635 spider_net_enable_card(card);
1637 netif_start_queue(netdev);
1638 netif_carrier_on(netdev);
1639 netif_poll_enable(netdev);
1643 register_int_failed:
1644 spider_net_free_rx_chain_contents(card);
1646 spider_net_free_chain(card, &card->rx_chain);
1648 spider_net_free_chain(card, &card->tx_chain);
1654 * spider_net_setup_phy - setup PHY
1655 * @card: card structure
1657 * returns 0 on success, <0 on failure
1659 * spider_net_setup_phy is used as part of spider_net_probe. Sets
1660 * the PHY to 1000 Mbps
1663 spider_net_setup_phy(struct spider_net_card *card)
1665 struct mii_phy *phy = &card->phy;
1667 spider_net_write_reg(card, SPIDER_NET_GDTDMASEL,
1668 SPIDER_NET_DMASEL_VALUE);
1669 spider_net_write_reg(card, SPIDER_NET_GPCCTRL,
1670 SPIDER_NET_PHY_CTRL_VALUE);
1672 phy->dev = card->netdev;
1673 phy->mdio_read = spider_net_read_phy;
1674 phy->mdio_write = spider_net_write_phy;
1676 mii_phy_probe(phy, phy->mii_id);
1678 if (phy->def->ops->setup_forced)
1679 phy->def->ops->setup_forced(phy, SPEED_1000, DUPLEX_FULL);
1681 phy->def->ops->enable_fiber(phy);
1683 phy->def->ops->read_link(phy);
1684 pr_info("Found %s with %i Mbps, %s-duplex.\n", phy->def->name,
1685 phy->speed, phy->duplex==1 ? "Full" : "Half");
1691 * spider_net_download_firmware - loads firmware into the adapter
1692 * @card: card structure
1693 * @firmware_ptr: pointer to firmware data
1695 * spider_net_download_firmware loads the firmware data into the
1696 * adapter. It assumes the length etc. to be allright.
1699 spider_net_download_firmware(struct spider_net_card *card,
1700 const void *firmware_ptr)
1703 const u32 *fw_ptr = firmware_ptr;
1705 /* stop sequencers */
1706 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1707 SPIDER_NET_STOP_SEQ_VALUE);
1709 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1711 spider_net_write_reg(card,
1712 SPIDER_NET_GSnPRGADR + sequencer * 8, 0);
1713 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
1714 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1715 sequencer * 8, *fw_ptr);
1720 if (spider_net_read_reg(card, SPIDER_NET_GSINIT))
1723 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1724 SPIDER_NET_RUN_SEQ_VALUE);
1730 * spider_net_init_firmware - reads in firmware parts
1731 * @card: card structure
1733 * Returns 0 on success, <0 on failure
1735 * spider_net_init_firmware opens the sequencer firmware and does some basic
1736 * checks. This function opens and releases the firmware structure. A call
1737 * to download the firmware is performed before the release.
1741 * spider_fw.bin is expected to be a file containing 6*1024*4 bytes, 4k being
1742 * the program for each sequencer. Use the command
1743 * tail -q -n +2 Seq_code1_0x088.txt Seq_code2_0x090.txt \
1744 * Seq_code3_0x098.txt Seq_code4_0x0A0.txt Seq_code5_0x0A8.txt \
1745 * Seq_code6_0x0B0.txt | xxd -r -p -c4 > spider_fw.bin
1747 * to generate spider_fw.bin, if you have sequencer programs with something
1748 * like the following contents for each sequencer:
1749 * <ONE LINE COMMENT>
1750 * <FIRST 4-BYTES-WORD FOR SEQUENCER>
1751 * <SECOND 4-BYTES-WORD FOR SEQUENCER>
1753 * <1024th 4-BYTES-WORD FOR SEQUENCER>
1756 spider_net_init_firmware(struct spider_net_card *card)
1758 struct firmware *firmware = NULL;
1759 struct device_node *dn;
1760 const u8 *fw_prop = NULL;
1764 if (request_firmware((const struct firmware **)&firmware,
1765 SPIDER_NET_FIRMWARE_NAME, &card->pdev->dev) == 0) {
1766 if ( (firmware->size != SPIDER_NET_FIRMWARE_LEN) &&
1767 netif_msg_probe(card) ) {
1768 pr_err("Incorrect size of spidernet firmware in " \
1769 "filesystem. Looking in host firmware...\n");
1772 err = spider_net_download_firmware(card, firmware->data);
1774 release_firmware(firmware);
1782 dn = pci_device_to_OF_node(card->pdev);
1786 fw_prop = get_property(dn, "firmware", &fw_size);
1790 if ( (fw_size != SPIDER_NET_FIRMWARE_LEN) &&
1791 netif_msg_probe(card) ) {
1792 pr_err("Incorrect size of spidernet firmware in " \
1797 err = spider_net_download_firmware(card, fw_prop);
1802 if (netif_msg_probe(card))
1803 pr_err("Couldn't find spidernet firmware in filesystem " \
1804 "or host firmware\n");
1809 * spider_net_workaround_rxramfull - work around firmware bug
1810 * @card: card structure
1815 spider_net_workaround_rxramfull(struct spider_net_card *card)
1817 int i, sequencer = 0;
1820 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1821 SPIDER_NET_CKRCTRL_RUN_VALUE);
1823 /* empty sequencer data */
1824 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1826 spider_net_write_reg(card, SPIDER_NET_GSnPRGADR +
1827 sequencer * 8, 0x0);
1828 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
1829 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1830 sequencer * 8, 0x0);
1834 /* set sequencer operation */
1835 spider_net_write_reg(card, SPIDER_NET_GSINIT, 0x000000fe);
1838 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1839 SPIDER_NET_CKRCTRL_STOP_VALUE);
1843 * spider_net_stop - called upon ifconfig down
1844 * @netdev: interface device structure
1849 spider_net_stop(struct net_device *netdev)
1851 struct spider_net_card *card = netdev_priv(netdev);
1853 tasklet_kill(&card->rxram_full_tl);
1854 netif_poll_disable(netdev);
1855 netif_carrier_off(netdev);
1856 netif_stop_queue(netdev);
1857 del_timer_sync(&card->tx_timer);
1859 /* disable/mask all interrupts */
1860 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, 0);
1861 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK, 0);
1862 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK, 0);
1864 /* free_irq(netdev->irq, netdev);*/
1865 free_irq(to_pci_dev(netdev->class_dev.dev)->irq, netdev);
1867 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
1868 SPIDER_NET_DMA_TX_FEND_VALUE);
1870 /* turn off DMA, force end */
1871 spider_net_disable_rxdmac(card);
1873 /* release chains */
1874 if (spin_trylock(&card->tx_chain.lock)) {
1875 spider_net_release_tx_chain(card, 1);
1876 spin_unlock(&card->tx_chain.lock);
1879 spider_net_free_chain(card, &card->tx_chain);
1880 spider_net_free_chain(card, &card->rx_chain);
1886 * spider_net_tx_timeout_task - task scheduled by the watchdog timeout
1887 * function (to be called not under interrupt status)
1888 * @data: data, is interface device structure
1890 * called as task when tx hangs, resets interface (if interface is up)
1893 spider_net_tx_timeout_task(void *data)
1895 struct net_device *netdev = data;
1896 struct spider_net_card *card = netdev_priv(netdev);
1898 if (!(netdev->flags & IFF_UP))
1901 netif_device_detach(netdev);
1902 spider_net_stop(netdev);
1904 spider_net_workaround_rxramfull(card);
1905 spider_net_init_card(card);
1907 if (spider_net_setup_phy(card))
1909 if (spider_net_init_firmware(card))
1912 spider_net_open(netdev);
1913 spider_net_kick_tx_dma(card);
1914 netif_device_attach(netdev);
1917 atomic_dec(&card->tx_timeout_task_counter);
1921 * spider_net_tx_timeout - called when the tx timeout watchdog kicks in.
1922 * @netdev: interface device structure
1924 * called, if tx hangs. Schedules a task that resets the interface
1927 spider_net_tx_timeout(struct net_device *netdev)
1929 struct spider_net_card *card;
1931 card = netdev_priv(netdev);
1932 atomic_inc(&card->tx_timeout_task_counter);
1933 if (netdev->flags & IFF_UP)
1934 schedule_work(&card->tx_timeout_task);
1936 atomic_dec(&card->tx_timeout_task_counter);
1940 * spider_net_setup_netdev_ops - initialization of net_device operations
1941 * @netdev: net_device structure
1943 * fills out function pointers in the net_device structure
1946 spider_net_setup_netdev_ops(struct net_device *netdev)
1948 netdev->open = &spider_net_open;
1949 netdev->stop = &spider_net_stop;
1950 netdev->hard_start_xmit = &spider_net_xmit;
1951 netdev->get_stats = &spider_net_get_stats;
1952 netdev->set_multicast_list = &spider_net_set_multi;
1953 netdev->set_mac_address = &spider_net_set_mac;
1954 netdev->change_mtu = &spider_net_change_mtu;
1955 netdev->do_ioctl = &spider_net_do_ioctl;
1957 netdev->tx_timeout = &spider_net_tx_timeout;
1958 netdev->watchdog_timeo = SPIDER_NET_WATCHDOG_TIMEOUT;
1960 netdev->poll = &spider_net_poll;
1961 netdev->weight = SPIDER_NET_NAPI_WEIGHT;
1963 netdev->vlan_rx_register = &spider_net_vlan_rx_reg;
1964 netdev->vlan_rx_add_vid = &spider_net_vlan_rx_add;
1965 netdev->vlan_rx_kill_vid = &spider_net_vlan_rx_kill;
1966 #ifdef CONFIG_NET_POLL_CONTROLLER
1967 /* poll controller */
1968 netdev->poll_controller = &spider_net_poll_controller;
1969 #endif /* CONFIG_NET_POLL_CONTROLLER */
1971 netdev->ethtool_ops = &spider_net_ethtool_ops;
1975 * spider_net_setup_netdev - initialization of net_device
1976 * @card: card structure
1978 * Returns 0 on success or <0 on failure
1980 * spider_net_setup_netdev initializes the net_device structure
1983 spider_net_setup_netdev(struct spider_net_card *card)
1986 struct net_device *netdev = card->netdev;
1987 struct device_node *dn;
1988 struct sockaddr addr;
1991 SET_MODULE_OWNER(netdev);
1992 SET_NETDEV_DEV(netdev, &card->pdev->dev);
1994 pci_set_drvdata(card->pdev, netdev);
1996 card->rxram_full_tl.data = (unsigned long) card;
1997 card->rxram_full_tl.func =
1998 (void (*)(unsigned long)) spider_net_handle_rxram_full;
1999 init_timer(&card->tx_timer);
2000 card->tx_timer.function =
2001 (void (*)(unsigned long)) spider_net_cleanup_tx_ring;
2002 card->tx_timer.data = (unsigned long) card;
2003 netdev->irq = card->pdev->irq;
2005 card->options.rx_csum = SPIDER_NET_RX_CSUM_DEFAULT;
2007 card->tx_desc = tx_descriptors;
2008 card->rx_desc = rx_descriptors;
2010 spider_net_setup_netdev_ops(netdev);
2012 netdev->features = NETIF_F_HW_CSUM | NETIF_F_LLTX;
2013 /* some time: NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
2014 * NETIF_F_HW_VLAN_FILTER */
2016 netdev->irq = card->pdev->irq;
2018 dn = pci_device_to_OF_node(card->pdev);
2022 mac = get_property(dn, "local-mac-address", NULL);
2025 memcpy(addr.sa_data, mac, ETH_ALEN);
2027 result = spider_net_set_mac(netdev, &addr);
2028 if ((result) && (netif_msg_probe(card)))
2029 pr_err("Failed to set MAC address: %i\n", result);
2031 result = register_netdev(netdev);
2033 if (netif_msg_probe(card))
2034 pr_err("Couldn't register net_device: %i\n",
2039 if (netif_msg_probe(card))
2040 pr_info("Initialized device %s.\n", netdev->name);
2046 * spider_net_alloc_card - allocates net_device and card structure
2048 * returns the card structure or NULL in case of errors
2050 * the card and net_device structures are linked to each other
2052 static struct spider_net_card *
2053 spider_net_alloc_card(void)
2055 struct net_device *netdev;
2056 struct spider_net_card *card;
2059 alloc_size = sizeof (*card) +
2060 sizeof (struct spider_net_descr) * rx_descriptors +
2061 sizeof (struct spider_net_descr) * tx_descriptors;
2062 netdev = alloc_etherdev(alloc_size);
2066 card = netdev_priv(netdev);
2067 card->netdev = netdev;
2068 card->msg_enable = SPIDER_NET_DEFAULT_MSG;
2069 INIT_WORK(&card->tx_timeout_task, spider_net_tx_timeout_task, netdev);
2070 init_waitqueue_head(&card->waitq);
2071 atomic_set(&card->tx_timeout_task_counter, 0);
2077 * spider_net_undo_pci_setup - releases PCI ressources
2078 * @card: card structure
2080 * spider_net_undo_pci_setup releases the mapped regions
2083 spider_net_undo_pci_setup(struct spider_net_card *card)
2085 iounmap(card->regs);
2086 pci_release_regions(card->pdev);
2090 * spider_net_setup_pci_dev - sets up the device in terms of PCI operations
2091 * @card: card structure
2094 * Returns the card structure or NULL if any errors occur
2096 * spider_net_setup_pci_dev initializes pdev and together with the
2097 * functions called in spider_net_open configures the device so that
2098 * data can be transferred over it
2099 * The net_device structure is attached to the card structure, if the
2100 * function returns without error.
2102 static struct spider_net_card *
2103 spider_net_setup_pci_dev(struct pci_dev *pdev)
2105 struct spider_net_card *card;
2106 unsigned long mmio_start, mmio_len;
2108 if (pci_enable_device(pdev)) {
2109 pr_err("Couldn't enable PCI device\n");
2113 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
2114 pr_err("Couldn't find proper PCI device base address.\n");
2115 goto out_disable_dev;
2118 if (pci_request_regions(pdev, spider_net_driver_name)) {
2119 pr_err("Couldn't obtain PCI resources, aborting.\n");
2120 goto out_disable_dev;
2123 pci_set_master(pdev);
2125 card = spider_net_alloc_card();
2127 pr_err("Couldn't allocate net_device structure, "
2129 goto out_release_regions;
2133 /* fetch base address and length of first resource */
2134 mmio_start = pci_resource_start(pdev, 0);
2135 mmio_len = pci_resource_len(pdev, 0);
2137 card->netdev->mem_start = mmio_start;
2138 card->netdev->mem_end = mmio_start + mmio_len;
2139 card->regs = ioremap(mmio_start, mmio_len);
2142 pr_err("Couldn't obtain PCI resources, aborting.\n");
2143 goto out_release_regions;
2148 out_release_regions:
2149 pci_release_regions(pdev);
2151 pci_disable_device(pdev);
2152 pci_set_drvdata(pdev, NULL);
2157 * spider_net_probe - initialization of a device
2159 * @ent: entry in the device id list
2161 * Returns 0 on success, <0 on failure
2163 * spider_net_probe initializes pdev and registers a net_device
2164 * structure for it. After that, the device can be ifconfig'ed up
2166 static int __devinit
2167 spider_net_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2170 struct spider_net_card *card;
2172 card = spider_net_setup_pci_dev(pdev);
2176 spider_net_workaround_rxramfull(card);
2177 spider_net_init_card(card);
2179 err = spider_net_setup_phy(card);
2183 err = spider_net_init_firmware(card);
2187 err = spider_net_setup_netdev(card);
2194 spider_net_undo_pci_setup(card);
2195 free_netdev(card->netdev);
2201 * spider_net_remove - removal of a device
2204 * Returns 0 on success, <0 on failure
2206 * spider_net_remove is called to remove the device and unregisters the
2209 static void __devexit
2210 spider_net_remove(struct pci_dev *pdev)
2212 struct net_device *netdev;
2213 struct spider_net_card *card;
2215 netdev = pci_get_drvdata(pdev);
2216 card = netdev_priv(netdev);
2218 wait_event(card->waitq,
2219 atomic_read(&card->tx_timeout_task_counter) == 0);
2221 unregister_netdev(netdev);
2223 /* switch off card */
2224 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2225 SPIDER_NET_CKRCTRL_STOP_VALUE);
2226 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2227 SPIDER_NET_CKRCTRL_RUN_VALUE);
2229 spider_net_undo_pci_setup(card);
2230 free_netdev(netdev);
2233 static struct pci_driver spider_net_driver = {
2234 .name = spider_net_driver_name,
2235 .id_table = spider_net_pci_tbl,
2236 .probe = spider_net_probe,
2237 .remove = __devexit_p(spider_net_remove)
2241 * spider_net_init - init function when the driver is loaded
2243 * spider_net_init registers the device driver
2245 static int __init spider_net_init(void)
2247 if (rx_descriptors < SPIDER_NET_RX_DESCRIPTORS_MIN) {
2248 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MIN;
2249 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2251 if (rx_descriptors > SPIDER_NET_RX_DESCRIPTORS_MAX) {
2252 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MAX;
2253 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2255 if (tx_descriptors < SPIDER_NET_TX_DESCRIPTORS_MIN) {
2256 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MIN;
2257 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2259 if (tx_descriptors > SPIDER_NET_TX_DESCRIPTORS_MAX) {
2260 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MAX;
2261 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2264 return pci_register_driver(&spider_net_driver);
2268 * spider_net_cleanup - exit function when driver is unloaded
2270 * spider_net_cleanup unregisters the device driver
2272 static void __exit spider_net_cleanup(void)
2274 pci_unregister_driver(&spider_net_driver);
2277 module_init(spider_net_init);
2278 module_exit(spider_net_cleanup);