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");
406 card->spider_stats.alloc_rx_skb_error++;
409 descr->buf_size = bufsize;
410 descr->result_size = 0;
411 descr->valid_size = 0;
412 descr->data_status = 0;
413 descr->data_error = 0;
415 offset = ((unsigned long)descr->skb->data) &
416 (SPIDER_NET_RXBUF_ALIGN - 1);
418 skb_reserve(descr->skb, SPIDER_NET_RXBUF_ALIGN - offset);
419 /* io-mmu-map the skb */
420 buf = pci_map_single(card->pdev, descr->skb->data,
421 SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
422 descr->buf_addr = buf;
423 if (buf == DMA_ERROR_CODE) {
424 dev_kfree_skb_any(descr->skb);
425 if (netif_msg_rx_err(card) && net_ratelimit())
426 pr_err("Could not iommu-map rx buffer\n");
427 card->spider_stats.rx_iommu_map_error++;
428 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
430 descr->dmac_cmd_status = SPIDER_NET_DESCR_CARDOWNED |
431 SPIDER_NET_DMAC_NOINTR_COMPLETE;
438 * spider_net_enable_rxchtails - sets RX dmac chain tail addresses
439 * @card: card structure
441 * spider_net_enable_rxchtails sets the RX DMAC chain tail adresses in the
442 * chip by writing to the appropriate register. DMA is enabled in
443 * spider_net_enable_rxdmac.
446 spider_net_enable_rxchtails(struct spider_net_card *card)
448 /* assume chain is aligned correctly */
449 spider_net_write_reg(card, SPIDER_NET_GDADCHA ,
450 card->rx_chain.tail->bus_addr);
454 * spider_net_enable_rxdmac - enables a receive DMA controller
455 * @card: card structure
457 * spider_net_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
458 * in the GDADMACCNTR register
461 spider_net_enable_rxdmac(struct spider_net_card *card)
464 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
465 SPIDER_NET_DMA_RX_VALUE);
469 * spider_net_refill_rx_chain - refills descriptors/skbs in the rx chains
470 * @card: card structure
472 * refills descriptors in the rx chain: allocates skbs and iommu-maps them.
475 spider_net_refill_rx_chain(struct spider_net_card *card)
477 struct spider_net_descr_chain *chain = &card->rx_chain;
480 /* one context doing the refill (and a second context seeing that
481 * and omitting it) is ok. If called by NAPI, we'll be called again
482 * as spider_net_decode_one_descr is called several times. If some
483 * interrupt calls us, the NAPI is about to clean up anyway. */
484 if (!spin_trylock_irqsave(&chain->lock, flags))
487 while (spider_net_get_descr_status(chain->head) ==
488 SPIDER_NET_DESCR_NOT_IN_USE) {
489 if (spider_net_prepare_rx_descr(card, chain->head))
491 chain->head = chain->head->next;
494 spin_unlock_irqrestore(&chain->lock, flags);
498 * spider_net_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
499 * @card: card structure
501 * returns 0 on success, <0 on failure
504 spider_net_alloc_rx_skbs(struct spider_net_card *card)
507 struct spider_net_descr_chain *chain;
511 chain = &card->rx_chain;
512 /* put at least one buffer into the chain. if this fails,
513 * we've got a problem. if not, spider_net_refill_rx_chain
514 * will do the rest at the end of this function */
515 if (spider_net_prepare_rx_descr(card, chain->head))
518 chain->head = chain->head->next;
520 /* this will allocate the rest of the rx buffers; if not, it's
521 * business as usual later on */
522 spider_net_refill_rx_chain(card);
523 spider_net_enable_rxdmac(card);
527 spider_net_free_rx_chain_contents(card);
532 * spider_net_get_multicast_hash - generates hash for multicast filter table
533 * @addr: multicast address
535 * returns the hash value.
537 * spider_net_get_multicast_hash calculates a hash value for a given multicast
538 * address, that is used to set the multicast filter tables
541 spider_net_get_multicast_hash(struct net_device *netdev, __u8 *addr)
545 char addr_for_crc[ETH_ALEN] = { 0, };
548 for (i = 0; i < ETH_ALEN * 8; i++) {
549 bit = (addr[i / 8] >> (i % 8)) & 1;
550 addr_for_crc[ETH_ALEN - 1 - i / 8] += bit << (7 - (i % 8));
553 crc = crc32_be(~0, addr_for_crc, netdev->addr_len);
564 * spider_net_set_multi - sets multicast addresses and promisc flags
565 * @netdev: interface device structure
567 * spider_net_set_multi configures multicast addresses as needed for the
568 * netdev interface. It also sets up multicast, allmulti and promisc
569 * flags appropriately
572 spider_net_set_multi(struct net_device *netdev)
574 struct dev_mc_list *mc;
578 struct spider_net_card *card = netdev_priv(netdev);
579 unsigned long bitmask[SPIDER_NET_MULTICAST_HASHES / BITS_PER_LONG] =
582 spider_net_set_promisc(card);
584 if (netdev->flags & IFF_ALLMULTI) {
585 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES; i++) {
591 /* well, we know, what the broadcast hash value is: it's xfd
592 hash = spider_net_get_multicast_hash(netdev, netdev->broadcast); */
593 set_bit(0xfd, bitmask);
595 for (mc = netdev->mc_list; mc; mc = mc->next) {
596 hash = spider_net_get_multicast_hash(netdev, mc->dmi_addr);
597 set_bit(hash, bitmask);
601 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES / 4; i++) {
603 if (test_bit(i * 4, bitmask))
606 if (test_bit(i * 4 + 1, bitmask))
609 if (test_bit(i * 4 + 2, bitmask))
612 if (test_bit(i * 4 + 3, bitmask))
615 spider_net_write_reg(card, SPIDER_NET_GMRMHFILnR + i * 4, reg);
620 * spider_net_disable_rxdmac - disables the receive DMA controller
621 * @card: card structure
623 * spider_net_disable_rxdmac terminates processing on the DMA controller by
624 * turing off DMA and issueing a force end
627 spider_net_disable_rxdmac(struct spider_net_card *card)
629 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
630 SPIDER_NET_DMA_RX_FEND_VALUE);
634 * spider_net_prepare_tx_descr - fill tx descriptor with skb data
635 * @card: card structure
636 * @descr: descriptor structure to fill out
637 * @skb: packet to use
639 * returns 0 on success, <0 on failure.
641 * fills out the descriptor structure with skb data and len. Copies data,
642 * if needed (32bit DMA!)
645 spider_net_prepare_tx_descr(struct spider_net_card *card,
648 struct spider_net_descr *descr = card->tx_chain.head;
651 buf = pci_map_single(card->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
652 if (buf == DMA_ERROR_CODE) {
653 if (netif_msg_tx_err(card) && net_ratelimit())
654 pr_err("could not iommu-map packet (%p, %i). "
655 "Dropping packet\n", skb->data, skb->len);
656 card->spider_stats.tx_iommu_map_error++;
660 descr->buf_addr = buf;
661 descr->buf_size = skb->len;
662 descr->next_descr_addr = 0;
664 descr->data_status = 0;
666 descr->dmac_cmd_status =
667 SPIDER_NET_DESCR_CARDOWNED | SPIDER_NET_DMAC_NOCS;
668 if (skb->protocol == htons(ETH_P_IP))
669 switch (skb->nh.iph->protocol) {
671 descr->dmac_cmd_status |= SPIDER_NET_DMAC_TCP;
674 descr->dmac_cmd_status |= SPIDER_NET_DMAC_UDP;
678 descr->prev->next_descr_addr = descr->bus_addr;
684 * spider_net_release_tx_descr - processes a used tx descriptor
685 * @card: card structure
686 * @descr: descriptor to release
688 * releases a used tx descriptor (unmapping, freeing of skb)
691 spider_net_release_tx_descr(struct spider_net_card *card)
693 struct spider_net_descr *descr = card->tx_chain.tail;
696 card->tx_chain.tail = card->tx_chain.tail->next;
697 descr->dmac_cmd_status |= SPIDER_NET_DESCR_NOT_IN_USE;
701 pci_unmap_single(card->pdev, descr->buf_addr, skb->len,
703 dev_kfree_skb_any(skb);
707 * spider_net_release_tx_chain - processes sent tx descriptors
708 * @card: adapter structure
709 * @brutal: if set, don't care about whether descriptor seems to be in use
711 * returns 0 if the tx ring is empty, otherwise 1.
713 * spider_net_release_tx_chain releases the tx descriptors that spider has
714 * finished with (if non-brutal) or simply release tx descriptors (if brutal).
715 * If some other context is calling this function, we return 1 so that we're
716 * scheduled again (if we were scheduled) and will not loose initiative.
719 spider_net_release_tx_chain(struct spider_net_card *card, int brutal)
721 struct spider_net_descr_chain *chain = &card->tx_chain;
724 spider_net_read_reg(card, SPIDER_NET_GDTDMACCNTR);
726 while (chain->tail != chain->head) {
727 status = spider_net_get_descr_status(chain->tail);
729 case SPIDER_NET_DESCR_COMPLETE:
730 card->netdev_stats.tx_packets++;
731 card->netdev_stats.tx_bytes += chain->tail->skb->len;
734 case SPIDER_NET_DESCR_CARDOWNED:
737 /* fallthrough, if we release the descriptors
738 * brutally (then we don't care about
739 * SPIDER_NET_DESCR_CARDOWNED) */
741 case SPIDER_NET_DESCR_RESPONSE_ERROR:
742 case SPIDER_NET_DESCR_PROTECTION_ERROR:
743 case SPIDER_NET_DESCR_FORCE_END:
744 if (netif_msg_tx_err(card))
745 pr_err("%s: forcing end of tx descriptor "
746 "with status x%02x\n",
747 card->netdev->name, status);
748 card->netdev_stats.tx_errors++;
752 card->netdev_stats.tx_dropped++;
755 spider_net_release_tx_descr(card);
762 * spider_net_kick_tx_dma - enables TX DMA processing
763 * @card: card structure
764 * @descr: descriptor address to enable TX processing at
766 * spider_net_kick_tx_dma writes the current tx chain head as start address
767 * of the tx descriptor chain and enables the transmission DMA engine
770 spider_net_kick_tx_dma(struct spider_net_card *card)
772 struct spider_net_descr *descr;
774 if (spider_net_read_reg(card, SPIDER_NET_GDTDMACCNTR) &
775 SPIDER_NET_TX_DMA_EN)
778 descr = card->tx_chain.tail;
780 if (spider_net_get_descr_status(descr) ==
781 SPIDER_NET_DESCR_CARDOWNED) {
782 spider_net_write_reg(card, SPIDER_NET_GDTDCHA,
784 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
785 SPIDER_NET_DMA_TX_VALUE);
788 if (descr == card->tx_chain.head)
794 mod_timer(&card->tx_timer, jiffies + SPIDER_NET_TX_TIMER);
798 * spider_net_xmit - transmits a frame over the device
799 * @skb: packet to send out
800 * @netdev: interface device structure
802 * returns 0 on success, !0 on failure
805 spider_net_xmit(struct sk_buff *skb, struct net_device *netdev)
807 struct spider_net_card *card = netdev_priv(netdev);
808 struct spider_net_descr_chain *chain = &card->tx_chain;
809 struct spider_net_descr *descr = chain->head;
813 spin_lock_irqsave(&chain->lock, flags);
815 spider_net_release_tx_chain(card, 0);
817 if (chain->head->next == chain->tail->prev) {
818 card->netdev_stats.tx_dropped++;
819 result = NETDEV_TX_LOCKED;
823 if (spider_net_get_descr_status(descr) != SPIDER_NET_DESCR_NOT_IN_USE) {
824 card->netdev_stats.tx_dropped++;
825 result = NETDEV_TX_LOCKED;
829 if (spider_net_prepare_tx_descr(card, skb) != 0) {
830 card->netdev_stats.tx_dropped++;
831 result = NETDEV_TX_BUSY;
835 result = NETDEV_TX_OK;
837 spider_net_kick_tx_dma(card);
838 card->tx_chain.head = card->tx_chain.head->next;
841 spin_unlock_irqrestore(&chain->lock, flags);
842 netif_wake_queue(netdev);
847 * spider_net_cleanup_tx_ring - cleans up the TX ring
848 * @card: card structure
850 * spider_net_cleanup_tx_ring is called by the tx_timer (as we don't use
851 * interrupts to cleanup our TX ring) and returns sent packets to the stack
855 spider_net_cleanup_tx_ring(struct spider_net_card *card)
859 spin_lock_irqsave(&card->tx_chain.lock, flags);
861 if ((spider_net_release_tx_chain(card, 0) != 0) &&
862 (card->netdev->flags & IFF_UP))
863 spider_net_kick_tx_dma(card);
865 spin_unlock_irqrestore(&card->tx_chain.lock, flags);
869 * spider_net_do_ioctl - called for device ioctls
870 * @netdev: interface device structure
871 * @ifr: request parameter structure for ioctl
872 * @cmd: command code for ioctl
874 * returns 0 on success, <0 on failure. Currently, we have no special ioctls.
875 * -EOPNOTSUPP is returned, if an unknown ioctl was requested
878 spider_net_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
887 * spider_net_pass_skb_up - takes an skb from a descriptor and passes it on
888 * @descr: descriptor to process
889 * @card: card structure
890 * @napi: whether caller is in NAPI context
892 * returns 1 on success, 0 if no packet was passed to the stack
894 * iommu-unmaps the skb, fills out skb structure and passes the data to the
895 * stack. The descriptor state is not changed.
898 spider_net_pass_skb_up(struct spider_net_descr *descr,
899 struct spider_net_card *card, int napi)
902 struct net_device *netdev;
903 u32 data_status, data_error;
905 data_status = descr->data_status;
906 data_error = descr->data_error;
908 netdev = card->netdev;
910 /* unmap descriptor */
911 pci_unmap_single(card->pdev, descr->buf_addr, SPIDER_NET_MAX_FRAME,
914 /* the cases we'll throw away the packet immediately */
915 if (data_error & SPIDER_NET_DESTROY_RX_FLAGS) {
916 if (netif_msg_rx_err(card))
917 pr_err("error in received descriptor found, "
918 "data_status=x%08x, data_error=x%08x\n",
919 data_status, data_error);
920 card->spider_stats.rx_desc_error++;
926 skb_put(skb, descr->valid_size);
928 /* the card seems to add 2 bytes of junk in front
929 * of the ethernet frame */
930 #define SPIDER_MISALIGN 2
931 skb_pull(skb, SPIDER_MISALIGN);
932 skb->protocol = eth_type_trans(skb, netdev);
934 /* checksum offload */
935 if (card->options.rx_csum) {
936 if ( ( (data_status & SPIDER_NET_DATA_STATUS_CKSUM_MASK) ==
937 SPIDER_NET_DATA_STATUS_CKSUM_MASK) &&
938 !(data_error & SPIDER_NET_DATA_ERR_CKSUM_MASK))
939 skb->ip_summed = CHECKSUM_UNNECESSARY;
941 skb->ip_summed = CHECKSUM_NONE;
943 skb->ip_summed = CHECKSUM_NONE;
945 if (data_status & SPIDER_NET_VLAN_PACKET) {
946 /* further enhancements: HW-accel VLAN
947 * vlan_hwaccel_receive_skb
951 /* pass skb up to stack */
953 netif_receive_skb(skb);
957 /* update netdevice statistics */
958 card->netdev_stats.rx_packets++;
959 card->netdev_stats.rx_bytes += skb->len;
965 * spider_net_decode_one_descr - processes an rx descriptor
966 * @card: card structure
967 * @napi: whether caller is in NAPI context
969 * returns 1 if a packet has been sent to the stack, otherwise 0
971 * processes an rx descriptor by iommu-unmapping the data buffer and passing
972 * the packet up to the stack. This function is called in softirq
973 * context, e.g. either bottom half from interrupt or NAPI polling context
976 spider_net_decode_one_descr(struct spider_net_card *card, int napi)
978 struct spider_net_descr_chain *chain = &card->rx_chain;
979 struct spider_net_descr *descr = chain->tail;
983 status = spider_net_get_descr_status(descr);
985 if (status == SPIDER_NET_DESCR_CARDOWNED) {
986 /* nothing in the descriptor yet */
991 if (status == SPIDER_NET_DESCR_NOT_IN_USE) {
992 /* not initialized yet, the ring must be empty */
993 spider_net_refill_rx_chain(card);
994 spider_net_enable_rxdmac(card);
999 /* descriptor definitively used -- move on tail */
1000 chain->tail = descr->next;
1003 if ( (status == SPIDER_NET_DESCR_RESPONSE_ERROR) ||
1004 (status == SPIDER_NET_DESCR_PROTECTION_ERROR) ||
1005 (status == SPIDER_NET_DESCR_FORCE_END) ) {
1006 if (netif_msg_rx_err(card))
1007 pr_err("%s: dropping RX descriptor with state %d\n",
1008 card->netdev->name, status);
1009 card->netdev_stats.rx_dropped++;
1010 pci_unmap_single(card->pdev, descr->buf_addr,
1011 SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
1012 dev_kfree_skb_irq(descr->skb);
1016 if ( (status != SPIDER_NET_DESCR_COMPLETE) &&
1017 (status != SPIDER_NET_DESCR_FRAME_END) ) {
1018 if (netif_msg_rx_err(card)) {
1019 pr_err("%s: RX descriptor with state %d\n",
1020 card->netdev->name, status);
1021 card->spider_stats.rx_desc_unk_state++;
1026 /* ok, we've got a packet in descr */
1027 result = spider_net_pass_skb_up(descr, card, napi);
1029 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
1030 /* change the descriptor state: */
1032 spider_net_refill_rx_chain(card);
1038 * spider_net_poll - NAPI poll function called by the stack to return packets
1039 * @netdev: interface device structure
1040 * @budget: number of packets we can pass to the stack at most
1042 * returns 0 if no more packets available to the driver/stack. Returns 1,
1043 * if the quota is exceeded, but the driver has still packets.
1045 * spider_net_poll returns all packets from the rx descriptors to the stack
1046 * (using netif_receive_skb). If all/enough packets are up, the driver
1047 * reenables interrupts and returns 0. If not, 1 is returned.
1050 spider_net_poll(struct net_device *netdev, int *budget)
1052 struct spider_net_card *card = netdev_priv(netdev);
1053 int packets_to_do, packets_done = 0;
1054 int no_more_packets = 0;
1056 packets_to_do = min(*budget, netdev->quota);
1058 while (packets_to_do) {
1059 if (spider_net_decode_one_descr(card, 1)) {
1063 /* no more packets for the stack */
1064 no_more_packets = 1;
1069 netdev->quota -= packets_done;
1070 *budget -= packets_done;
1071 spider_net_refill_rx_chain(card);
1073 /* if all packets are in the stack, enable interrupts and return 0 */
1074 /* if not, return 1 */
1075 if (no_more_packets) {
1076 netif_rx_complete(netdev);
1077 spider_net_rx_irq_on(card);
1085 * spider_net_vlan_rx_reg - initializes VLAN structures in the driver and card
1086 * @netdev: interface device structure
1087 * @grp: vlan_group structure that is registered (NULL on destroying interface)
1090 spider_net_vlan_rx_reg(struct net_device *netdev, struct vlan_group *grp)
1092 /* further enhancement... yet to do */
1097 * spider_net_vlan_rx_add - adds VLAN id to the card filter
1098 * @netdev: interface device structure
1099 * @vid: VLAN id to add
1102 spider_net_vlan_rx_add(struct net_device *netdev, uint16_t vid)
1104 /* further enhancement... yet to do */
1105 /* add vid to card's VLAN filter table */
1110 * spider_net_vlan_rx_kill - removes VLAN id to the card filter
1111 * @netdev: interface device structure
1112 * @vid: VLAN id to remove
1115 spider_net_vlan_rx_kill(struct net_device *netdev, uint16_t vid)
1117 /* further enhancement... yet to do */
1118 /* remove vid from card's VLAN filter table */
1122 * spider_net_get_stats - get interface statistics
1123 * @netdev: interface device structure
1125 * returns the interface statistics residing in the spider_net_card struct
1127 static struct net_device_stats *
1128 spider_net_get_stats(struct net_device *netdev)
1130 struct spider_net_card *card = netdev_priv(netdev);
1131 struct net_device_stats *stats = &card->netdev_stats;
1136 * spider_net_change_mtu - changes the MTU of an interface
1137 * @netdev: interface device structure
1138 * @new_mtu: new MTU value
1140 * returns 0 on success, <0 on failure
1143 spider_net_change_mtu(struct net_device *netdev, int new_mtu)
1145 /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
1146 * and mtu is outbound only anyway */
1147 if ( (new_mtu < SPIDER_NET_MIN_MTU ) ||
1148 (new_mtu > SPIDER_NET_MAX_MTU) )
1150 netdev->mtu = new_mtu;
1155 * spider_net_set_mac - sets the MAC of an interface
1156 * @netdev: interface device structure
1157 * @ptr: pointer to new MAC address
1159 * Returns 0 on success, <0 on failure. Currently, we don't support this
1160 * and will always return EOPNOTSUPP.
1163 spider_net_set_mac(struct net_device *netdev, void *p)
1165 struct spider_net_card *card = netdev_priv(netdev);
1166 u32 macl, macu, regvalue;
1167 struct sockaddr *addr = p;
1169 if (!is_valid_ether_addr(addr->sa_data))
1170 return -EADDRNOTAVAIL;
1172 /* switch off GMACTPE and GMACRPE */
1173 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1174 regvalue &= ~((1 << 5) | (1 << 6));
1175 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1178 macu = (addr->sa_data[0]<<24) + (addr->sa_data[1]<<16) +
1179 (addr->sa_data[2]<<8) + (addr->sa_data[3]);
1180 macl = (addr->sa_data[4]<<8) + (addr->sa_data[5]);
1181 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACU, macu);
1182 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACL, macl);
1184 /* switch GMACTPE and GMACRPE back on */
1185 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1186 regvalue |= ((1 << 5) | (1 << 6));
1187 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1189 spider_net_set_promisc(card);
1191 /* look up, whether we have been successful */
1192 if (spider_net_get_mac_address(netdev))
1193 return -EADDRNOTAVAIL;
1194 if (memcmp(netdev->dev_addr,addr->sa_data,netdev->addr_len))
1195 return -EADDRNOTAVAIL;
1201 * spider_net_handle_rxram_full - cleans up RX ring upon RX RAM full interrupt
1202 * @card: card structure
1204 * spider_net_handle_rxram_full empties the RX ring so that spider can put
1205 * more packets in it and empty its RX RAM. This is called in bottom half
1209 spider_net_handle_rxram_full(struct spider_net_card *card)
1211 while (spider_net_decode_one_descr(card, 0))
1213 spider_net_enable_rxchtails(card);
1214 spider_net_enable_rxdmac(card);
1215 netif_rx_schedule(card->netdev);
1219 * spider_net_handle_error_irq - handles errors raised by an interrupt
1220 * @card: card structure
1221 * @status_reg: interrupt status register 0 (GHIINT0STS)
1223 * spider_net_handle_error_irq treats or ignores all error conditions
1224 * found when an interrupt is presented
1227 spider_net_handle_error_irq(struct spider_net_card *card, u32 status_reg)
1229 u32 error_reg1, error_reg2;
1233 error_reg1 = spider_net_read_reg(card, SPIDER_NET_GHIINT1STS);
1234 error_reg2 = spider_net_read_reg(card, SPIDER_NET_GHIINT2STS);
1236 /* check GHIINT0STS ************************************/
1238 for (i = 0; i < 32; i++)
1239 if (status_reg & (1<<i))
1242 /* let error_reg1 and error_reg2 evaluation decide, what to do
1243 case SPIDER_NET_PHYINT:
1244 case SPIDER_NET_GMAC2INT:
1245 case SPIDER_NET_GMAC1INT:
1246 case SPIDER_NET_GIPSINT:
1247 case SPIDER_NET_GFIFOINT:
1248 case SPIDER_NET_DMACINT:
1249 case SPIDER_NET_GSYSINT:
1252 case SPIDER_NET_GPWOPCMPINT:
1253 /* PHY write operation completed */
1256 case SPIDER_NET_GPROPCMPINT:
1257 /* PHY read operation completed */
1258 /* we don't use semaphores, as we poll for the completion
1259 * of the read operation in spider_net_read_phy. Should take
1263 case SPIDER_NET_GPWFFINT:
1264 /* PHY command queue full */
1265 if (netif_msg_intr(card))
1266 pr_err("PHY write queue full\n");
1270 /* case SPIDER_NET_GRMDADRINT: not used. print a message */
1271 /* case SPIDER_NET_GRMARPINT: not used. print a message */
1272 /* case SPIDER_NET_GRMMPINT: not used. print a message */
1274 case SPIDER_NET_GDTDEN0INT:
1275 /* someone has set TX_DMA_EN to 0 */
1279 case SPIDER_NET_GDDDEN0INT: /* fallthrough */
1280 case SPIDER_NET_GDCDEN0INT: /* fallthrough */
1281 case SPIDER_NET_GDBDEN0INT: /* fallthrough */
1282 case SPIDER_NET_GDADEN0INT:
1283 /* someone has set RX_DMA_EN to 0 */
1288 case SPIDER_NET_GDDFDCINT:
1289 case SPIDER_NET_GDCFDCINT:
1290 case SPIDER_NET_GDBFDCINT:
1291 case SPIDER_NET_GDAFDCINT:
1292 /* case SPIDER_NET_GDNMINT: not used. print a message */
1293 /* case SPIDER_NET_GCNMINT: not used. print a message */
1294 /* case SPIDER_NET_GBNMINT: not used. print a message */
1295 /* case SPIDER_NET_GANMINT: not used. print a message */
1296 /* case SPIDER_NET_GRFNMINT: not used. print a message */
1301 case SPIDER_NET_GDTFDCINT:
1304 case SPIDER_NET_GTTEDINT:
1307 case SPIDER_NET_GDTDCEINT:
1308 /* chain end. If a descriptor should be sent, kick off
1310 if (card->tx_chain.tail == card->tx_chain.head)
1311 spider_net_kick_tx_dma(card);
1315 /* case SPIDER_NET_G1TMCNTINT: not used. print a message */
1316 /* case SPIDER_NET_GFREECNTINT: not used. print a message */
1319 /* check GHIINT1STS ************************************/
1321 for (i = 0; i < 32; i++)
1322 if (error_reg1 & (1<<i))
1325 case SPIDER_NET_GTMFLLINT:
1326 if (netif_msg_intr(card) && net_ratelimit())
1327 pr_err("Spider TX RAM full\n");
1330 case SPIDER_NET_GRFDFLLINT: /* fallthrough */
1331 case SPIDER_NET_GRFCFLLINT: /* fallthrough */
1332 case SPIDER_NET_GRFBFLLINT: /* fallthrough */
1333 case SPIDER_NET_GRFAFLLINT: /* fallthrough */
1334 case SPIDER_NET_GRMFLLINT:
1335 if (netif_msg_intr(card) && net_ratelimit())
1336 pr_debug("Spider RX RAM full, incoming packets "
1337 "might be discarded!\n");
1338 spider_net_rx_irq_off(card);
1339 tasklet_schedule(&card->rxram_full_tl);
1343 /* case SPIDER_NET_GTMSHTINT: problem, print a message */
1344 case SPIDER_NET_GDTINVDINT:
1345 /* allrighty. tx from previous descr ok */
1350 case SPIDER_NET_GDDDCEINT: /* fallthrough */
1351 case SPIDER_NET_GDCDCEINT: /* fallthrough */
1352 case SPIDER_NET_GDBDCEINT: /* fallthrough */
1353 case SPIDER_NET_GDADCEINT:
1354 if (netif_msg_intr(card))
1355 pr_err("got descriptor chain end interrupt, "
1356 "restarting DMAC %c.\n",
1357 'D'+i-SPIDER_NET_GDDDCEINT);
1358 spider_net_refill_rx_chain(card);
1359 spider_net_enable_rxdmac(card);
1363 /* invalid descriptor */
1364 case SPIDER_NET_GDDINVDINT: /* fallthrough */
1365 case SPIDER_NET_GDCINVDINT: /* fallthrough */
1366 case SPIDER_NET_GDBINVDINT: /* fallthrough */
1367 case SPIDER_NET_GDAINVDINT:
1368 /* could happen when rx chain is full */
1369 spider_net_refill_rx_chain(card);
1370 spider_net_enable_rxdmac(card);
1374 /* case SPIDER_NET_GDTRSERINT: problem, print a message */
1375 /* case SPIDER_NET_GDDRSERINT: problem, print a message */
1376 /* case SPIDER_NET_GDCRSERINT: problem, print a message */
1377 /* case SPIDER_NET_GDBRSERINT: problem, print a message */
1378 /* case SPIDER_NET_GDARSERINT: problem, print a message */
1379 /* case SPIDER_NET_GDSERINT: problem, print a message */
1380 /* case SPIDER_NET_GDTPTERINT: problem, print a message */
1381 /* case SPIDER_NET_GDDPTERINT: problem, print a message */
1382 /* case SPIDER_NET_GDCPTERINT: problem, print a message */
1383 /* case SPIDER_NET_GDBPTERINT: problem, print a message */
1384 /* case SPIDER_NET_GDAPTERINT: problem, print a message */
1390 /* check GHIINT2STS ************************************/
1392 for (i = 0; i < 32; i++)
1393 if (error_reg2 & (1<<i))
1396 /* there is nothing we can (want to) do at this time. Log a
1397 * message, we can switch on and off the specific values later on
1398 case SPIDER_NET_GPROPERINT:
1399 case SPIDER_NET_GMCTCRSNGINT:
1400 case SPIDER_NET_GMCTLCOLINT:
1401 case SPIDER_NET_GMCTTMOTINT:
1402 case SPIDER_NET_GMCRCAERINT:
1403 case SPIDER_NET_GMCRCALERINT:
1404 case SPIDER_NET_GMCRALNERINT:
1405 case SPIDER_NET_GMCROVRINT:
1406 case SPIDER_NET_GMCRRNTINT:
1407 case SPIDER_NET_GMCRRXERINT:
1408 case SPIDER_NET_GTITCSERINT:
1409 case SPIDER_NET_GTIFMTERINT:
1410 case SPIDER_NET_GTIPKTRVKINT:
1411 case SPIDER_NET_GTISPINGINT:
1412 case SPIDER_NET_GTISADNGINT:
1413 case SPIDER_NET_GTISPDNGINT:
1414 case SPIDER_NET_GRIFMTERINT:
1415 case SPIDER_NET_GRIPKTRVKINT:
1416 case SPIDER_NET_GRISPINGINT:
1417 case SPIDER_NET_GRISADNGINT:
1418 case SPIDER_NET_GRISPDNGINT:
1425 if ((show_error) && (netif_msg_intr(card)))
1426 pr_err("Got error interrupt, GHIINT0STS = 0x%08x, "
1427 "GHIINT1STS = 0x%08x, GHIINT2STS = 0x%08x\n",
1428 status_reg, error_reg1, error_reg2);
1430 /* clear interrupt sources */
1431 spider_net_write_reg(card, SPIDER_NET_GHIINT1STS, error_reg1);
1432 spider_net_write_reg(card, SPIDER_NET_GHIINT2STS, error_reg2);
1436 * spider_net_interrupt - interrupt handler for spider_net
1437 * @irq: interupt number
1438 * @ptr: pointer to net_device
1439 * @regs: PU registers
1441 * returns IRQ_HANDLED, if interrupt was for driver, or IRQ_NONE, if no
1442 * interrupt found raised by card.
1444 * This is the interrupt handler, that turns off
1445 * interrupts for this device and makes the stack poll the driver
1448 spider_net_interrupt(int irq, void *ptr, struct pt_regs *regs)
1450 struct net_device *netdev = ptr;
1451 struct spider_net_card *card = netdev_priv(netdev);
1454 status_reg = spider_net_read_reg(card, SPIDER_NET_GHIINT0STS);
1459 if (status_reg & SPIDER_NET_RXINT ) {
1460 spider_net_rx_irq_off(card);
1461 netif_rx_schedule(netdev);
1464 if (status_reg & SPIDER_NET_ERRINT )
1465 spider_net_handle_error_irq(card, status_reg);
1467 /* clear interrupt sources */
1468 spider_net_write_reg(card, SPIDER_NET_GHIINT0STS, status_reg);
1473 #ifdef CONFIG_NET_POLL_CONTROLLER
1475 * spider_net_poll_controller - artificial interrupt for netconsole etc.
1476 * @netdev: interface device structure
1478 * see Documentation/networking/netconsole.txt
1481 spider_net_poll_controller(struct net_device *netdev)
1483 disable_irq(netdev->irq);
1484 spider_net_interrupt(netdev->irq, netdev, NULL);
1485 enable_irq(netdev->irq);
1487 #endif /* CONFIG_NET_POLL_CONTROLLER */
1490 * spider_net_init_card - initializes the card
1491 * @card: card structure
1493 * spider_net_init_card initializes the card so that other registers can
1497 spider_net_init_card(struct spider_net_card *card)
1499 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1500 SPIDER_NET_CKRCTRL_STOP_VALUE);
1502 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1503 SPIDER_NET_CKRCTRL_RUN_VALUE);
1507 * spider_net_enable_card - enables the card by setting all kinds of regs
1508 * @card: card structure
1510 * spider_net_enable_card sets a lot of SMMIO registers to enable the device
1513 spider_net_enable_card(struct spider_net_card *card)
1516 /* the following array consists of (register),(value) pairs
1517 * that are set in this function. A register of 0 ends the list */
1519 { SPIDER_NET_GRESUMINTNUM, 0 },
1520 { SPIDER_NET_GREINTNUM, 0 },
1522 /* set interrupt frame number registers */
1523 /* clear the single DMA engine registers first */
1524 { SPIDER_NET_GFAFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1525 { SPIDER_NET_GFBFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1526 { SPIDER_NET_GFCFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1527 { SPIDER_NET_GFDFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1528 /* then set, what we really need */
1529 { SPIDER_NET_GFFRMNUM, SPIDER_NET_FRAMENUM_VALUE },
1531 /* timer counter registers and stuff */
1532 { SPIDER_NET_GFREECNNUM, 0 },
1533 { SPIDER_NET_GONETIMENUM, 0 },
1534 { SPIDER_NET_GTOUTFRMNUM, 0 },
1536 /* RX mode setting */
1537 { SPIDER_NET_GRXMDSET, SPIDER_NET_RXMODE_VALUE },
1538 /* TX mode setting */
1539 { SPIDER_NET_GTXMDSET, SPIDER_NET_TXMODE_VALUE },
1540 /* IPSEC mode setting */
1541 { SPIDER_NET_GIPSECINIT, SPIDER_NET_IPSECINIT_VALUE },
1543 { SPIDER_NET_GFTRESTRT, SPIDER_NET_RESTART_VALUE },
1545 { SPIDER_NET_GMRWOLCTRL, 0 },
1546 { SPIDER_NET_GTESTMD, 0x10000000 },
1547 { SPIDER_NET_GTTQMSK, 0x00400040 },
1549 { SPIDER_NET_GMACINTEN, 0 },
1551 /* flow control stuff */
1552 { SPIDER_NET_GMACAPAUSE, SPIDER_NET_MACAPAUSE_VALUE },
1553 { SPIDER_NET_GMACTXPAUSE, SPIDER_NET_TXPAUSE_VALUE },
1555 { SPIDER_NET_GMACBSTLMT, SPIDER_NET_BURSTLMT_VALUE },
1560 while (regs[i][0]) {
1561 spider_net_write_reg(card, regs[i][0], regs[i][1]);
1565 /* clear unicast filter table entries 1 to 14 */
1566 for (i = 1; i <= 14; i++) {
1567 spider_net_write_reg(card,
1568 SPIDER_NET_GMRUAFILnR + i * 8,
1570 spider_net_write_reg(card,
1571 SPIDER_NET_GMRUAFILnR + i * 8 + 4,
1575 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R, 0x08080000);
1577 spider_net_write_reg(card, SPIDER_NET_ECMODE, SPIDER_NET_ECMODE_VALUE);
1579 /* set chain tail adress for RX chains and
1581 spider_net_enable_rxchtails(card);
1582 spider_net_enable_rxdmac(card);
1584 spider_net_write_reg(card, SPIDER_NET_GRXDMAEN, SPIDER_NET_WOL_VALUE);
1586 spider_net_write_reg(card, SPIDER_NET_GMACLENLMT,
1587 SPIDER_NET_LENLMT_VALUE);
1588 spider_net_write_reg(card, SPIDER_NET_GMACMODE,
1589 SPIDER_NET_MACMODE_VALUE);
1590 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD,
1591 SPIDER_NET_OPMODE_VALUE);
1593 /* set interrupt mask registers */
1594 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK,
1595 SPIDER_NET_INT0_MASK_VALUE);
1596 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK,
1597 SPIDER_NET_INT1_MASK_VALUE);
1598 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK,
1599 SPIDER_NET_INT2_MASK_VALUE);
1601 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
1602 SPIDER_NET_GDTDCEIDIS);
1606 * spider_net_open - called upon ifonfig up
1607 * @netdev: interface device structure
1609 * returns 0 on success, <0 on failure
1611 * spider_net_open allocates all the descriptors and memory needed for
1612 * operation, sets up multicast list and enables interrupts
1615 spider_net_open(struct net_device *netdev)
1617 struct spider_net_card *card = netdev_priv(netdev);
1621 if (spider_net_init_chain(card, &card->tx_chain, card->descr,
1622 PCI_DMA_TODEVICE, card->tx_desc))
1623 goto alloc_tx_failed;
1624 if (spider_net_init_chain(card, &card->rx_chain,
1625 card->descr + card->rx_desc,
1626 PCI_DMA_FROMDEVICE, card->rx_desc))
1627 goto alloc_rx_failed;
1629 /* allocate rx skbs */
1630 if (spider_net_alloc_rx_skbs(card))
1631 goto alloc_skbs_failed;
1633 spider_net_set_multi(netdev);
1635 /* further enhancement: setup hw vlan, if needed */
1638 if (request_irq(netdev->irq, spider_net_interrupt,
1639 IRQF_SHARED, netdev->name, netdev))
1640 goto register_int_failed;
1642 spider_net_enable_card(card);
1644 netif_start_queue(netdev);
1645 netif_carrier_on(netdev);
1646 netif_poll_enable(netdev);
1650 register_int_failed:
1651 spider_net_free_rx_chain_contents(card);
1653 spider_net_free_chain(card, &card->rx_chain);
1655 spider_net_free_chain(card, &card->tx_chain);
1661 * spider_net_setup_phy - setup PHY
1662 * @card: card structure
1664 * returns 0 on success, <0 on failure
1666 * spider_net_setup_phy is used as part of spider_net_probe. Sets
1667 * the PHY to 1000 Mbps
1670 spider_net_setup_phy(struct spider_net_card *card)
1672 struct mii_phy *phy = &card->phy;
1674 spider_net_write_reg(card, SPIDER_NET_GDTDMASEL,
1675 SPIDER_NET_DMASEL_VALUE);
1676 spider_net_write_reg(card, SPIDER_NET_GPCCTRL,
1677 SPIDER_NET_PHY_CTRL_VALUE);
1679 phy->dev = card->netdev;
1680 phy->mdio_read = spider_net_read_phy;
1681 phy->mdio_write = spider_net_write_phy;
1683 mii_phy_probe(phy, phy->mii_id);
1685 if (phy->def->ops->setup_forced)
1686 phy->def->ops->setup_forced(phy, SPEED_1000, DUPLEX_FULL);
1688 phy->def->ops->enable_fiber(phy);
1690 phy->def->ops->read_link(phy);
1691 pr_info("Found %s with %i Mbps, %s-duplex.\n", phy->def->name,
1692 phy->speed, phy->duplex==1 ? "Full" : "Half");
1698 * spider_net_download_firmware - loads firmware into the adapter
1699 * @card: card structure
1700 * @firmware_ptr: pointer to firmware data
1702 * spider_net_download_firmware loads the firmware data into the
1703 * adapter. It assumes the length etc. to be allright.
1706 spider_net_download_firmware(struct spider_net_card *card,
1707 const void *firmware_ptr)
1710 const u32 *fw_ptr = firmware_ptr;
1712 /* stop sequencers */
1713 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1714 SPIDER_NET_STOP_SEQ_VALUE);
1716 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1718 spider_net_write_reg(card,
1719 SPIDER_NET_GSnPRGADR + sequencer * 8, 0);
1720 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
1721 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1722 sequencer * 8, *fw_ptr);
1727 if (spider_net_read_reg(card, SPIDER_NET_GSINIT))
1730 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1731 SPIDER_NET_RUN_SEQ_VALUE);
1737 * spider_net_init_firmware - reads in firmware parts
1738 * @card: card structure
1740 * Returns 0 on success, <0 on failure
1742 * spider_net_init_firmware opens the sequencer firmware and does some basic
1743 * checks. This function opens and releases the firmware structure. A call
1744 * to download the firmware is performed before the release.
1748 * spider_fw.bin is expected to be a file containing 6*1024*4 bytes, 4k being
1749 * the program for each sequencer. Use the command
1750 * tail -q -n +2 Seq_code1_0x088.txt Seq_code2_0x090.txt \
1751 * Seq_code3_0x098.txt Seq_code4_0x0A0.txt Seq_code5_0x0A8.txt \
1752 * Seq_code6_0x0B0.txt | xxd -r -p -c4 > spider_fw.bin
1754 * to generate spider_fw.bin, if you have sequencer programs with something
1755 * like the following contents for each sequencer:
1756 * <ONE LINE COMMENT>
1757 * <FIRST 4-BYTES-WORD FOR SEQUENCER>
1758 * <SECOND 4-BYTES-WORD FOR SEQUENCER>
1760 * <1024th 4-BYTES-WORD FOR SEQUENCER>
1763 spider_net_init_firmware(struct spider_net_card *card)
1765 struct firmware *firmware = NULL;
1766 struct device_node *dn;
1767 const u8 *fw_prop = NULL;
1771 if (request_firmware((const struct firmware **)&firmware,
1772 SPIDER_NET_FIRMWARE_NAME, &card->pdev->dev) == 0) {
1773 if ( (firmware->size != SPIDER_NET_FIRMWARE_LEN) &&
1774 netif_msg_probe(card) ) {
1775 pr_err("Incorrect size of spidernet firmware in " \
1776 "filesystem. Looking in host firmware...\n");
1779 err = spider_net_download_firmware(card, firmware->data);
1781 release_firmware(firmware);
1789 dn = pci_device_to_OF_node(card->pdev);
1793 fw_prop = get_property(dn, "firmware", &fw_size);
1797 if ( (fw_size != SPIDER_NET_FIRMWARE_LEN) &&
1798 netif_msg_probe(card) ) {
1799 pr_err("Incorrect size of spidernet firmware in " \
1804 err = spider_net_download_firmware(card, fw_prop);
1809 if (netif_msg_probe(card))
1810 pr_err("Couldn't find spidernet firmware in filesystem " \
1811 "or host firmware\n");
1816 * spider_net_workaround_rxramfull - work around firmware bug
1817 * @card: card structure
1822 spider_net_workaround_rxramfull(struct spider_net_card *card)
1824 int i, sequencer = 0;
1827 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1828 SPIDER_NET_CKRCTRL_RUN_VALUE);
1830 /* empty sequencer data */
1831 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1833 spider_net_write_reg(card, SPIDER_NET_GSnPRGADR +
1834 sequencer * 8, 0x0);
1835 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
1836 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1837 sequencer * 8, 0x0);
1841 /* set sequencer operation */
1842 spider_net_write_reg(card, SPIDER_NET_GSINIT, 0x000000fe);
1845 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1846 SPIDER_NET_CKRCTRL_STOP_VALUE);
1850 * spider_net_stop - called upon ifconfig down
1851 * @netdev: interface device structure
1856 spider_net_stop(struct net_device *netdev)
1858 struct spider_net_card *card = netdev_priv(netdev);
1860 tasklet_kill(&card->rxram_full_tl);
1861 netif_poll_disable(netdev);
1862 netif_carrier_off(netdev);
1863 netif_stop_queue(netdev);
1864 del_timer_sync(&card->tx_timer);
1866 /* disable/mask all interrupts */
1867 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, 0);
1868 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK, 0);
1869 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK, 0);
1871 /* free_irq(netdev->irq, netdev);*/
1872 free_irq(to_pci_dev(netdev->class_dev.dev)->irq, netdev);
1874 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
1875 SPIDER_NET_DMA_TX_FEND_VALUE);
1877 /* turn off DMA, force end */
1878 spider_net_disable_rxdmac(card);
1880 /* release chains */
1881 if (spin_trylock(&card->tx_chain.lock)) {
1882 spider_net_release_tx_chain(card, 1);
1883 spin_unlock(&card->tx_chain.lock);
1886 spider_net_free_chain(card, &card->tx_chain);
1887 spider_net_free_chain(card, &card->rx_chain);
1893 * spider_net_tx_timeout_task - task scheduled by the watchdog timeout
1894 * function (to be called not under interrupt status)
1895 * @data: data, is interface device structure
1897 * called as task when tx hangs, resets interface (if interface is up)
1900 spider_net_tx_timeout_task(void *data)
1902 struct net_device *netdev = data;
1903 struct spider_net_card *card = netdev_priv(netdev);
1905 if (!(netdev->flags & IFF_UP))
1908 netif_device_detach(netdev);
1909 spider_net_stop(netdev);
1911 spider_net_workaround_rxramfull(card);
1912 spider_net_init_card(card);
1914 if (spider_net_setup_phy(card))
1916 if (spider_net_init_firmware(card))
1919 spider_net_open(netdev);
1920 spider_net_kick_tx_dma(card);
1921 netif_device_attach(netdev);
1924 atomic_dec(&card->tx_timeout_task_counter);
1928 * spider_net_tx_timeout - called when the tx timeout watchdog kicks in.
1929 * @netdev: interface device structure
1931 * called, if tx hangs. Schedules a task that resets the interface
1934 spider_net_tx_timeout(struct net_device *netdev)
1936 struct spider_net_card *card;
1938 card = netdev_priv(netdev);
1939 atomic_inc(&card->tx_timeout_task_counter);
1940 if (netdev->flags & IFF_UP)
1941 schedule_work(&card->tx_timeout_task);
1943 atomic_dec(&card->tx_timeout_task_counter);
1944 card->spider_stats.tx_timeouts++;
1948 * spider_net_setup_netdev_ops - initialization of net_device operations
1949 * @netdev: net_device structure
1951 * fills out function pointers in the net_device structure
1954 spider_net_setup_netdev_ops(struct net_device *netdev)
1956 netdev->open = &spider_net_open;
1957 netdev->stop = &spider_net_stop;
1958 netdev->hard_start_xmit = &spider_net_xmit;
1959 netdev->get_stats = &spider_net_get_stats;
1960 netdev->set_multicast_list = &spider_net_set_multi;
1961 netdev->set_mac_address = &spider_net_set_mac;
1962 netdev->change_mtu = &spider_net_change_mtu;
1963 netdev->do_ioctl = &spider_net_do_ioctl;
1965 netdev->tx_timeout = &spider_net_tx_timeout;
1966 netdev->watchdog_timeo = SPIDER_NET_WATCHDOG_TIMEOUT;
1968 netdev->poll = &spider_net_poll;
1969 netdev->weight = SPIDER_NET_NAPI_WEIGHT;
1971 netdev->vlan_rx_register = &spider_net_vlan_rx_reg;
1972 netdev->vlan_rx_add_vid = &spider_net_vlan_rx_add;
1973 netdev->vlan_rx_kill_vid = &spider_net_vlan_rx_kill;
1974 #ifdef CONFIG_NET_POLL_CONTROLLER
1975 /* poll controller */
1976 netdev->poll_controller = &spider_net_poll_controller;
1977 #endif /* CONFIG_NET_POLL_CONTROLLER */
1979 netdev->ethtool_ops = &spider_net_ethtool_ops;
1983 * spider_net_setup_netdev - initialization of net_device
1984 * @card: card structure
1986 * Returns 0 on success or <0 on failure
1988 * spider_net_setup_netdev initializes the net_device structure
1991 spider_net_setup_netdev(struct spider_net_card *card)
1994 struct net_device *netdev = card->netdev;
1995 struct device_node *dn;
1996 struct sockaddr addr;
1999 SET_MODULE_OWNER(netdev);
2000 SET_NETDEV_DEV(netdev, &card->pdev->dev);
2002 pci_set_drvdata(card->pdev, netdev);
2004 card->rxram_full_tl.data = (unsigned long) card;
2005 card->rxram_full_tl.func =
2006 (void (*)(unsigned long)) spider_net_handle_rxram_full;
2007 init_timer(&card->tx_timer);
2008 card->tx_timer.function =
2009 (void (*)(unsigned long)) spider_net_cleanup_tx_ring;
2010 card->tx_timer.data = (unsigned long) card;
2011 netdev->irq = card->pdev->irq;
2013 card->options.rx_csum = SPIDER_NET_RX_CSUM_DEFAULT;
2015 card->tx_desc = tx_descriptors;
2016 card->rx_desc = rx_descriptors;
2018 spider_net_setup_netdev_ops(netdev);
2020 netdev->features = NETIF_F_HW_CSUM | NETIF_F_LLTX;
2021 /* some time: NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
2022 * NETIF_F_HW_VLAN_FILTER */
2024 netdev->irq = card->pdev->irq;
2026 dn = pci_device_to_OF_node(card->pdev);
2030 mac = get_property(dn, "local-mac-address", NULL);
2033 memcpy(addr.sa_data, mac, ETH_ALEN);
2035 result = spider_net_set_mac(netdev, &addr);
2036 if ((result) && (netif_msg_probe(card)))
2037 pr_err("Failed to set MAC address: %i\n", result);
2039 result = register_netdev(netdev);
2041 if (netif_msg_probe(card))
2042 pr_err("Couldn't register net_device: %i\n",
2047 if (netif_msg_probe(card))
2048 pr_info("Initialized device %s.\n", netdev->name);
2054 * spider_net_alloc_card - allocates net_device and card structure
2056 * returns the card structure or NULL in case of errors
2058 * the card and net_device structures are linked to each other
2060 static struct spider_net_card *
2061 spider_net_alloc_card(void)
2063 struct net_device *netdev;
2064 struct spider_net_card *card;
2067 alloc_size = sizeof (*card) +
2068 sizeof (struct spider_net_descr) * rx_descriptors +
2069 sizeof (struct spider_net_descr) * tx_descriptors;
2070 netdev = alloc_etherdev(alloc_size);
2074 card = netdev_priv(netdev);
2075 card->netdev = netdev;
2076 card->msg_enable = SPIDER_NET_DEFAULT_MSG;
2077 INIT_WORK(&card->tx_timeout_task, spider_net_tx_timeout_task, netdev);
2078 init_waitqueue_head(&card->waitq);
2079 atomic_set(&card->tx_timeout_task_counter, 0);
2085 * spider_net_undo_pci_setup - releases PCI ressources
2086 * @card: card structure
2088 * spider_net_undo_pci_setup releases the mapped regions
2091 spider_net_undo_pci_setup(struct spider_net_card *card)
2093 iounmap(card->regs);
2094 pci_release_regions(card->pdev);
2098 * spider_net_setup_pci_dev - sets up the device in terms of PCI operations
2099 * @card: card structure
2102 * Returns the card structure or NULL if any errors occur
2104 * spider_net_setup_pci_dev initializes pdev and together with the
2105 * functions called in spider_net_open configures the device so that
2106 * data can be transferred over it
2107 * The net_device structure is attached to the card structure, if the
2108 * function returns without error.
2110 static struct spider_net_card *
2111 spider_net_setup_pci_dev(struct pci_dev *pdev)
2113 struct spider_net_card *card;
2114 unsigned long mmio_start, mmio_len;
2116 if (pci_enable_device(pdev)) {
2117 pr_err("Couldn't enable PCI device\n");
2121 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
2122 pr_err("Couldn't find proper PCI device base address.\n");
2123 goto out_disable_dev;
2126 if (pci_request_regions(pdev, spider_net_driver_name)) {
2127 pr_err("Couldn't obtain PCI resources, aborting.\n");
2128 goto out_disable_dev;
2131 pci_set_master(pdev);
2133 card = spider_net_alloc_card();
2135 pr_err("Couldn't allocate net_device structure, "
2137 goto out_release_regions;
2141 /* fetch base address and length of first resource */
2142 mmio_start = pci_resource_start(pdev, 0);
2143 mmio_len = pci_resource_len(pdev, 0);
2145 card->netdev->mem_start = mmio_start;
2146 card->netdev->mem_end = mmio_start + mmio_len;
2147 card->regs = ioremap(mmio_start, mmio_len);
2150 pr_err("Couldn't obtain PCI resources, aborting.\n");
2151 goto out_release_regions;
2156 out_release_regions:
2157 pci_release_regions(pdev);
2159 pci_disable_device(pdev);
2160 pci_set_drvdata(pdev, NULL);
2165 * spider_net_probe - initialization of a device
2167 * @ent: entry in the device id list
2169 * Returns 0 on success, <0 on failure
2171 * spider_net_probe initializes pdev and registers a net_device
2172 * structure for it. After that, the device can be ifconfig'ed up
2174 static int __devinit
2175 spider_net_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2178 struct spider_net_card *card;
2180 card = spider_net_setup_pci_dev(pdev);
2184 spider_net_workaround_rxramfull(card);
2185 spider_net_init_card(card);
2187 err = spider_net_setup_phy(card);
2191 err = spider_net_init_firmware(card);
2195 err = spider_net_setup_netdev(card);
2202 spider_net_undo_pci_setup(card);
2203 free_netdev(card->netdev);
2209 * spider_net_remove - removal of a device
2212 * Returns 0 on success, <0 on failure
2214 * spider_net_remove is called to remove the device and unregisters the
2217 static void __devexit
2218 spider_net_remove(struct pci_dev *pdev)
2220 struct net_device *netdev;
2221 struct spider_net_card *card;
2223 netdev = pci_get_drvdata(pdev);
2224 card = netdev_priv(netdev);
2226 wait_event(card->waitq,
2227 atomic_read(&card->tx_timeout_task_counter) == 0);
2229 unregister_netdev(netdev);
2231 /* switch off card */
2232 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2233 SPIDER_NET_CKRCTRL_STOP_VALUE);
2234 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2235 SPIDER_NET_CKRCTRL_RUN_VALUE);
2237 spider_net_undo_pci_setup(card);
2238 free_netdev(netdev);
2241 static struct pci_driver spider_net_driver = {
2242 .name = spider_net_driver_name,
2243 .id_table = spider_net_pci_tbl,
2244 .probe = spider_net_probe,
2245 .remove = __devexit_p(spider_net_remove)
2249 * spider_net_init - init function when the driver is loaded
2251 * spider_net_init registers the device driver
2253 static int __init spider_net_init(void)
2255 if (rx_descriptors < SPIDER_NET_RX_DESCRIPTORS_MIN) {
2256 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MIN;
2257 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2259 if (rx_descriptors > SPIDER_NET_RX_DESCRIPTORS_MAX) {
2260 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MAX;
2261 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2263 if (tx_descriptors < SPIDER_NET_TX_DESCRIPTORS_MIN) {
2264 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MIN;
2265 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2267 if (tx_descriptors > SPIDER_NET_TX_DESCRIPTORS_MAX) {
2268 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MAX;
2269 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2272 return pci_register_driver(&spider_net_driver);
2276 * spider_net_cleanup - exit function when driver is unloaded
2278 * spider_net_cleanup unregisters the device driver
2280 static void __exit spider_net_cleanup(void)
2282 pci_unregister_driver(&spider_net_driver);
2285 module_init(spider_net_init);
2286 module_exit(spider_net_cleanup);