Linux 2.6.17.3
[linux/fpc-iii.git] / drivers / net / spider_net.c
blob394339d5e87c0494c300fad7f60eaf9984e8c78f
1 /*
2 * Network device driver for Cell Processor-Based Blade
4 * (C) Copyright IBM Corp. 2005
6 * Authors : Utz Bacher <utz.bacher@de.ibm.com>
7 * Jens Osterkamp <Jens.Osterkamp@de.ibm.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
12 * any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/config.h>
25 #include <linux/compiler.h>
26 #include <linux/crc32.h>
27 #include <linux/delay.h>
28 #include <linux/etherdevice.h>
29 #include <linux/ethtool.h>
30 #include <linux/firmware.h>
31 #include <linux/if_vlan.h>
32 #include <linux/in.h>
33 #include <linux/init.h>
34 #include <linux/ioport.h>
35 #include <linux/ip.h>
36 #include <linux/kernel.h>
37 #include <linux/mii.h>
38 #include <linux/module.h>
39 #include <linux/netdevice.h>
40 #include <linux/device.h>
41 #include <linux/pci.h>
42 #include <linux/skbuff.h>
43 #include <linux/slab.h>
44 #include <linux/tcp.h>
45 #include <linux/types.h>
46 #include <linux/vmalloc.h>
47 #include <linux/wait.h>
48 #include <linux/workqueue.h>
49 #include <asm/bitops.h>
50 #include <asm/pci-bridge.h>
51 #include <net/checksum.h>
53 #include "spider_net.h"
55 MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com> and Jens Osterkamp " \
56 "<Jens.Osterkamp@de.ibm.com>");
57 MODULE_DESCRIPTION("Spider Southbridge Gigabit Ethernet driver");
58 MODULE_LICENSE("GPL");
60 static int rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_DEFAULT;
61 static int tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_DEFAULT;
63 module_param(rx_descriptors, int, 0644);
64 module_param(tx_descriptors, int, 0644);
66 MODULE_PARM_DESC(rx_descriptors, "number of descriptors used " \
67 "in rx chains");
68 MODULE_PARM_DESC(tx_descriptors, "number of descriptors used " \
69 "in tx chain");
71 char spider_net_driver_name[] = "spidernet";
73 static struct pci_device_id spider_net_pci_tbl[] = {
74 { PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_SPIDER_NET,
75 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
76 { 0, }
79 MODULE_DEVICE_TABLE(pci, spider_net_pci_tbl);
81 /**
82 * spider_net_read_reg - reads an SMMIO register of a card
83 * @card: device structure
84 * @reg: register to read from
86 * returns the content of the specified SMMIO register.
88 static u32
89 spider_net_read_reg(struct spider_net_card *card, u32 reg)
91 u32 value;
93 value = readl(card->regs + reg);
94 value = le32_to_cpu(value);
96 return value;
99 /**
100 * spider_net_write_reg - writes to an SMMIO register of a card
101 * @card: device structure
102 * @reg: register to write to
103 * @value: value to write into the specified SMMIO register
105 static void
106 spider_net_write_reg(struct spider_net_card *card, u32 reg, u32 value)
108 value = cpu_to_le32(value);
109 writel(value, card->regs + reg);
112 /** spider_net_write_phy - write to phy register
113 * @netdev: adapter to be written to
114 * @mii_id: id of MII
115 * @reg: PHY register
116 * @val: value to be written to phy register
118 * spider_net_write_phy_register writes to an arbitrary PHY
119 * register via the spider GPCWOPCMD register. We assume the queue does
120 * not run full (not more than 15 commands outstanding).
122 static void
123 spider_net_write_phy(struct net_device *netdev, int mii_id,
124 int reg, int val)
126 struct spider_net_card *card = netdev_priv(netdev);
127 u32 writevalue;
129 writevalue = ((u32)mii_id << 21) |
130 ((u32)reg << 16) | ((u32)val);
132 spider_net_write_reg(card, SPIDER_NET_GPCWOPCMD, writevalue);
135 /** spider_net_read_phy - read from phy register
136 * @netdev: network device to be read from
137 * @mii_id: id of MII
138 * @reg: PHY register
140 * Returns value read from PHY register
142 * spider_net_write_phy reads from an arbitrary PHY
143 * register via the spider GPCROPCMD register
145 static int
146 spider_net_read_phy(struct net_device *netdev, int mii_id, int reg)
148 struct spider_net_card *card = netdev_priv(netdev);
149 u32 readvalue;
151 readvalue = ((u32)mii_id << 21) | ((u32)reg << 16);
152 spider_net_write_reg(card, SPIDER_NET_GPCROPCMD, readvalue);
154 /* we don't use semaphores to wait for an SPIDER_NET_GPROPCMPINT
155 * interrupt, as we poll for the completion of the read operation
156 * in spider_net_read_phy. Should take about 50 us */
157 do {
158 readvalue = spider_net_read_reg(card, SPIDER_NET_GPCROPCMD);
159 } while (readvalue & SPIDER_NET_GPREXEC);
161 readvalue &= SPIDER_NET_GPRDAT_MASK;
163 return readvalue;
167 * spider_net_rx_irq_off - switch off rx irq on this spider card
168 * @card: device structure
170 * switches off rx irq by masking them out in the GHIINTnMSK register
172 static void
173 spider_net_rx_irq_off(struct spider_net_card *card)
175 u32 regvalue;
177 regvalue = SPIDER_NET_INT0_MASK_VALUE & (~SPIDER_NET_RXINT);
178 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, regvalue);
182 * spider_net_rx_irq_on - switch on rx irq on this spider card
183 * @card: device structure
185 * switches on rx irq by enabling them in the GHIINTnMSK register
187 static void
188 spider_net_rx_irq_on(struct spider_net_card *card)
190 u32 regvalue;
192 regvalue = SPIDER_NET_INT0_MASK_VALUE | SPIDER_NET_RXINT;
193 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, regvalue);
197 * spider_net_set_promisc - sets the unicast address or the promiscuous mode
198 * @card: card structure
200 * spider_net_set_promisc sets the unicast destination address filter and
201 * thus either allows for non-promisc mode or promisc mode
203 static void
204 spider_net_set_promisc(struct spider_net_card *card)
206 u32 macu, macl;
207 struct net_device *netdev = card->netdev;
209 if (netdev->flags & IFF_PROMISC) {
210 /* clear destination entry 0 */
211 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR, 0);
212 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR + 0x04, 0);
213 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R,
214 SPIDER_NET_PROMISC_VALUE);
215 } else {
216 macu = netdev->dev_addr[0];
217 macu <<= 8;
218 macu |= netdev->dev_addr[1];
219 memcpy(&macl, &netdev->dev_addr[2], sizeof(macl));
221 macu |= SPIDER_NET_UA_DESCR_VALUE;
222 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR, macu);
223 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR + 0x04, macl);
224 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R,
225 SPIDER_NET_NONPROMISC_VALUE);
230 * spider_net_get_mac_address - read mac address from spider card
231 * @card: device structure
233 * reads MAC address from GMACUNIMACU and GMACUNIMACL registers
235 static int
236 spider_net_get_mac_address(struct net_device *netdev)
238 struct spider_net_card *card = netdev_priv(netdev);
239 u32 macl, macu;
241 macl = spider_net_read_reg(card, SPIDER_NET_GMACUNIMACL);
242 macu = spider_net_read_reg(card, SPIDER_NET_GMACUNIMACU);
244 netdev->dev_addr[0] = (macu >> 24) & 0xff;
245 netdev->dev_addr[1] = (macu >> 16) & 0xff;
246 netdev->dev_addr[2] = (macu >> 8) & 0xff;
247 netdev->dev_addr[3] = macu & 0xff;
248 netdev->dev_addr[4] = (macl >> 8) & 0xff;
249 netdev->dev_addr[5] = macl & 0xff;
251 if (!is_valid_ether_addr(&netdev->dev_addr[0]))
252 return -EINVAL;
254 return 0;
258 * spider_net_get_descr_status -- returns the status of a descriptor
259 * @descr: descriptor to look at
261 * returns the status as in the dmac_cmd_status field of the descriptor
263 static enum spider_net_descr_status
264 spider_net_get_descr_status(struct spider_net_descr *descr)
266 u32 cmd_status;
268 cmd_status = descr->dmac_cmd_status;
269 cmd_status >>= SPIDER_NET_DESCR_IND_PROC_SHIFT;
270 /* no need to mask out any bits, as cmd_status is 32 bits wide only
271 * (and unsigned) */
272 return cmd_status;
276 * spider_net_set_descr_status -- sets the status of a descriptor
277 * @descr: descriptor to change
278 * @status: status to set in the descriptor
280 * changes the status to the specified value. Doesn't change other bits
281 * in the status
283 static void
284 spider_net_set_descr_status(struct spider_net_descr *descr,
285 enum spider_net_descr_status status)
287 u32 cmd_status;
288 /* read the status */
289 cmd_status = descr->dmac_cmd_status;
290 /* clean the upper 4 bits */
291 cmd_status &= SPIDER_NET_DESCR_IND_PROC_MASKO;
292 /* add the status to it */
293 cmd_status |= ((u32)status)<<SPIDER_NET_DESCR_IND_PROC_SHIFT;
294 /* and write it back */
295 descr->dmac_cmd_status = cmd_status;
299 * spider_net_free_chain - free descriptor chain
300 * @card: card structure
301 * @chain: address of chain
304 static void
305 spider_net_free_chain(struct spider_net_card *card,
306 struct spider_net_descr_chain *chain)
308 struct spider_net_descr *descr;
310 for (descr = chain->tail; !descr->bus_addr; descr = descr->next) {
311 pci_unmap_single(card->pdev, descr->bus_addr,
312 SPIDER_NET_DESCR_SIZE, PCI_DMA_BIDIRECTIONAL);
313 descr->bus_addr = 0;
318 * spider_net_init_chain - links descriptor chain
319 * @card: card structure
320 * @chain: address of chain
321 * @start_descr: address of descriptor array
322 * @no: number of descriptors
324 * we manage a circular list that mirrors the hardware structure,
325 * except that the hardware uses bus addresses.
327 * returns 0 on success, <0 on failure
329 static int
330 spider_net_init_chain(struct spider_net_card *card,
331 struct spider_net_descr_chain *chain,
332 struct spider_net_descr *start_descr, int no)
334 int i;
335 struct spider_net_descr *descr;
336 dma_addr_t buf;
338 atomic_set(&card->rx_chain_refill,0);
340 descr = start_descr;
341 memset(descr, 0, sizeof(*descr) * no);
343 /* set up the hardware pointers in each descriptor */
344 for (i=0; i<no; i++, descr++) {
345 spider_net_set_descr_status(descr, SPIDER_NET_DESCR_NOT_IN_USE);
347 buf = pci_map_single(card->pdev, descr,
348 SPIDER_NET_DESCR_SIZE,
349 PCI_DMA_BIDIRECTIONAL);
351 if (buf == DMA_ERROR_CODE)
352 goto iommu_error;
354 descr->bus_addr = buf;
355 descr->next = descr + 1;
356 descr->prev = descr - 1;
359 /* do actual circular list */
360 (descr-1)->next = start_descr;
361 start_descr->prev = descr-1;
363 descr = start_descr;
364 for (i=0; i < no; i++, descr++) {
365 descr->next_descr_addr = descr->next->bus_addr;
368 chain->head = start_descr;
369 chain->tail = start_descr;
371 return 0;
373 iommu_error:
374 descr = start_descr;
375 for (i=0; i < no; i++, descr++)
376 if (descr->bus_addr)
377 pci_unmap_single(card->pdev, descr->bus_addr,
378 SPIDER_NET_DESCR_SIZE,
379 PCI_DMA_BIDIRECTIONAL);
380 return -ENOMEM;
384 * spider_net_free_rx_chain_contents - frees descr contents in rx chain
385 * @card: card structure
387 * returns 0 on success, <0 on failure
389 static void
390 spider_net_free_rx_chain_contents(struct spider_net_card *card)
392 struct spider_net_descr *descr;
394 descr = card->rx_chain.head;
395 while (descr->next != card->rx_chain.head) {
396 if (descr->skb) {
397 dev_kfree_skb(descr->skb);
398 pci_unmap_single(card->pdev, descr->buf_addr,
399 SPIDER_NET_MAX_FRAME,
400 PCI_DMA_BIDIRECTIONAL);
402 descr = descr->next;
407 * spider_net_prepare_rx_descr - reinitializes a rx descriptor
408 * @card: card structure
409 * @descr: descriptor to re-init
411 * return 0 on succes, <0 on failure
413 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
414 * Activate the descriptor state-wise
416 static int
417 spider_net_prepare_rx_descr(struct spider_net_card *card,
418 struct spider_net_descr *descr)
420 dma_addr_t buf;
421 int error = 0;
422 int offset;
423 int bufsize;
425 /* we need to round up the buffer size to a multiple of 128 */
426 bufsize = (SPIDER_NET_MAX_FRAME + SPIDER_NET_RXBUF_ALIGN - 1) &
427 (~(SPIDER_NET_RXBUF_ALIGN - 1));
429 /* and we need to have it 128 byte aligned, therefore we allocate a
430 * bit more */
431 /* allocate an skb */
432 descr->skb = dev_alloc_skb(bufsize + SPIDER_NET_RXBUF_ALIGN - 1);
433 if (!descr->skb) {
434 if (netif_msg_rx_err(card) && net_ratelimit())
435 pr_err("Not enough memory to allocate rx buffer\n");
436 return -ENOMEM;
438 descr->buf_size = bufsize;
439 descr->result_size = 0;
440 descr->valid_size = 0;
441 descr->data_status = 0;
442 descr->data_error = 0;
444 offset = ((unsigned long)descr->skb->data) &
445 (SPIDER_NET_RXBUF_ALIGN - 1);
446 if (offset)
447 skb_reserve(descr->skb, SPIDER_NET_RXBUF_ALIGN - offset);
448 /* io-mmu-map the skb */
449 buf = pci_map_single(card->pdev, descr->skb->data,
450 SPIDER_NET_MAX_FRAME, PCI_DMA_BIDIRECTIONAL);
451 descr->buf_addr = buf;
452 if (buf == DMA_ERROR_CODE) {
453 dev_kfree_skb_any(descr->skb);
454 if (netif_msg_rx_err(card) && net_ratelimit())
455 pr_err("Could not iommu-map rx buffer\n");
456 spider_net_set_descr_status(descr, SPIDER_NET_DESCR_NOT_IN_USE);
457 } else {
458 descr->dmac_cmd_status = SPIDER_NET_DMAC_RX_CARDOWNED;
461 return error;
465 * spider_net_enable_rxchtails - sets RX dmac chain tail addresses
466 * @card: card structure
468 * spider_net_enable_rxchtails sets the RX DMAC chain tail adresses in the
469 * chip by writing to the appropriate register. DMA is enabled in
470 * spider_net_enable_rxdmac.
472 static void
473 spider_net_enable_rxchtails(struct spider_net_card *card)
475 /* assume chain is aligned correctly */
476 spider_net_write_reg(card, SPIDER_NET_GDADCHA ,
477 card->rx_chain.tail->bus_addr);
481 * spider_net_enable_rxdmac - enables a receive DMA controller
482 * @card: card structure
484 * spider_net_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
485 * in the GDADMACCNTR register
487 static void
488 spider_net_enable_rxdmac(struct spider_net_card *card)
490 wmb();
491 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
492 SPIDER_NET_DMA_RX_VALUE);
496 * spider_net_refill_rx_chain - refills descriptors/skbs in the rx chains
497 * @card: card structure
499 * refills descriptors in the rx chain: allocates skbs and iommu-maps them.
501 static void
502 spider_net_refill_rx_chain(struct spider_net_card *card)
504 struct spider_net_descr_chain *chain;
506 chain = &card->rx_chain;
508 /* one context doing the refill (and a second context seeing that
509 * and omitting it) is ok. If called by NAPI, we'll be called again
510 * as spider_net_decode_one_descr is called several times. If some
511 * interrupt calls us, the NAPI is about to clean up anyway. */
512 if (atomic_inc_return(&card->rx_chain_refill) == 1)
513 while (spider_net_get_descr_status(chain->head) ==
514 SPIDER_NET_DESCR_NOT_IN_USE) {
515 if (spider_net_prepare_rx_descr(card, chain->head))
516 break;
517 chain->head = chain->head->next;
520 atomic_dec(&card->rx_chain_refill);
524 * spider_net_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
525 * @card: card structure
527 * returns 0 on success, <0 on failure
529 static int
530 spider_net_alloc_rx_skbs(struct spider_net_card *card)
532 int result;
533 struct spider_net_descr_chain *chain;
535 result = -ENOMEM;
537 chain = &card->rx_chain;
538 /* put at least one buffer into the chain. if this fails,
539 * we've got a problem. if not, spider_net_refill_rx_chain
540 * will do the rest at the end of this function */
541 if (spider_net_prepare_rx_descr(card, chain->head))
542 goto error;
543 else
544 chain->head = chain->head->next;
546 /* this will allocate the rest of the rx buffers; if not, it's
547 * business as usual later on */
548 spider_net_refill_rx_chain(card);
549 spider_net_enable_rxdmac(card);
550 return 0;
552 error:
553 spider_net_free_rx_chain_contents(card);
554 return result;
558 * spider_net_release_tx_descr - processes a used tx descriptor
559 * @card: card structure
560 * @descr: descriptor to release
562 * releases a used tx descriptor (unmapping, freeing of skb)
564 static void
565 spider_net_release_tx_descr(struct spider_net_card *card,
566 struct spider_net_descr *descr)
568 struct sk_buff *skb;
570 /* unmap the skb */
571 skb = descr->skb;
572 pci_unmap_single(card->pdev, descr->buf_addr, skb->len,
573 PCI_DMA_BIDIRECTIONAL);
575 dev_kfree_skb_any(skb);
577 /* set status to not used */
578 spider_net_set_descr_status(descr, SPIDER_NET_DESCR_NOT_IN_USE);
582 * spider_net_release_tx_chain - processes sent tx descriptors
583 * @card: adapter structure
584 * @brutal: if set, don't care about whether descriptor seems to be in use
586 * returns 0 if the tx ring is empty, otherwise 1.
588 * spider_net_release_tx_chain releases the tx descriptors that spider has
589 * finished with (if non-brutal) or simply release tx descriptors (if brutal).
590 * If some other context is calling this function, we return 1 so that we're
591 * scheduled again (if we were scheduled) and will not loose initiative.
593 static int
594 spider_net_release_tx_chain(struct spider_net_card *card, int brutal)
596 struct spider_net_descr_chain *tx_chain = &card->tx_chain;
597 enum spider_net_descr_status status;
599 if (atomic_inc_return(&card->tx_chain_release) != 1) {
600 atomic_dec(&card->tx_chain_release);
601 return 1;
604 for (;;) {
605 status = spider_net_get_descr_status(tx_chain->tail);
606 switch (status) {
607 case SPIDER_NET_DESCR_CARDOWNED:
608 if (!brutal)
609 goto out;
610 /* fallthrough, if we release the descriptors
611 * brutally (then we don't care about
612 * SPIDER_NET_DESCR_CARDOWNED) */
613 case SPIDER_NET_DESCR_RESPONSE_ERROR:
614 case SPIDER_NET_DESCR_PROTECTION_ERROR:
615 case SPIDER_NET_DESCR_FORCE_END:
616 if (netif_msg_tx_err(card))
617 pr_err("%s: forcing end of tx descriptor "
618 "with status x%02x\n",
619 card->netdev->name, status);
620 card->netdev_stats.tx_dropped++;
621 break;
623 case SPIDER_NET_DESCR_COMPLETE:
624 card->netdev_stats.tx_packets++;
625 card->netdev_stats.tx_bytes +=
626 tx_chain->tail->skb->len;
627 break;
629 default: /* any other value (== SPIDER_NET_DESCR_NOT_IN_USE) */
630 goto out;
632 spider_net_release_tx_descr(card, tx_chain->tail);
633 tx_chain->tail = tx_chain->tail->next;
635 out:
636 atomic_dec(&card->tx_chain_release);
638 netif_wake_queue(card->netdev);
640 if (status == SPIDER_NET_DESCR_CARDOWNED)
641 return 1;
642 return 0;
646 * spider_net_cleanup_tx_ring - cleans up the TX ring
647 * @card: card structure
649 * spider_net_cleanup_tx_ring is called by the tx_timer (as we don't use
650 * interrupts to cleanup our TX ring) and returns sent packets to the stack
651 * by freeing them
653 static void
654 spider_net_cleanup_tx_ring(struct spider_net_card *card)
656 if ( (spider_net_release_tx_chain(card, 0)) &&
657 (card->netdev->flags & IFF_UP) ) {
658 mod_timer(&card->tx_timer, jiffies + SPIDER_NET_TX_TIMER);
663 * spider_net_get_multicast_hash - generates hash for multicast filter table
664 * @addr: multicast address
666 * returns the hash value.
668 * spider_net_get_multicast_hash calculates a hash value for a given multicast
669 * address, that is used to set the multicast filter tables
671 static u8
672 spider_net_get_multicast_hash(struct net_device *netdev, __u8 *addr)
674 u32 crc;
675 u8 hash;
676 char addr_for_crc[ETH_ALEN] = { 0, };
677 int i, bit;
679 for (i = 0; i < ETH_ALEN * 8; i++) {
680 bit = (addr[i / 8] >> (i % 8)) & 1;
681 addr_for_crc[ETH_ALEN - 1 - i / 8] += bit << (7 - (i % 8));
684 crc = crc32_be(~0, addr_for_crc, netdev->addr_len);
686 hash = (crc >> 27);
687 hash <<= 3;
688 hash |= crc & 7;
689 hash &= 0xff;
691 return hash;
695 * spider_net_set_multi - sets multicast addresses and promisc flags
696 * @netdev: interface device structure
698 * spider_net_set_multi configures multicast addresses as needed for the
699 * netdev interface. It also sets up multicast, allmulti and promisc
700 * flags appropriately
702 static void
703 spider_net_set_multi(struct net_device *netdev)
705 struct dev_mc_list *mc;
706 u8 hash;
707 int i;
708 u32 reg;
709 struct spider_net_card *card = netdev_priv(netdev);
710 unsigned long bitmask[SPIDER_NET_MULTICAST_HASHES / BITS_PER_LONG] =
711 {0, };
713 spider_net_set_promisc(card);
715 if (netdev->flags & IFF_ALLMULTI) {
716 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES; i++) {
717 set_bit(i, bitmask);
719 goto write_hash;
722 /* well, we know, what the broadcast hash value is: it's xfd
723 hash = spider_net_get_multicast_hash(netdev, netdev->broadcast); */
724 set_bit(0xfd, bitmask);
726 for (mc = netdev->mc_list; mc; mc = mc->next) {
727 hash = spider_net_get_multicast_hash(netdev, mc->dmi_addr);
728 set_bit(hash, bitmask);
731 write_hash:
732 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES / 4; i++) {
733 reg = 0;
734 if (test_bit(i * 4, bitmask))
735 reg += 0x08;
736 reg <<= 8;
737 if (test_bit(i * 4 + 1, bitmask))
738 reg += 0x08;
739 reg <<= 8;
740 if (test_bit(i * 4 + 2, bitmask))
741 reg += 0x08;
742 reg <<= 8;
743 if (test_bit(i * 4 + 3, bitmask))
744 reg += 0x08;
746 spider_net_write_reg(card, SPIDER_NET_GMRMHFILnR + i * 4, reg);
751 * spider_net_disable_rxdmac - disables the receive DMA controller
752 * @card: card structure
754 * spider_net_disable_rxdmac terminates processing on the DMA controller by
755 * turing off DMA and issueing a force end
757 static void
758 spider_net_disable_rxdmac(struct spider_net_card *card)
760 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
761 SPIDER_NET_DMA_RX_FEND_VALUE);
765 * spider_net_stop - called upon ifconfig down
766 * @netdev: interface device structure
768 * always returns 0
771 spider_net_stop(struct net_device *netdev)
773 struct spider_net_card *card = netdev_priv(netdev);
775 tasklet_kill(&card->rxram_full_tl);
776 netif_poll_disable(netdev);
777 netif_carrier_off(netdev);
778 netif_stop_queue(netdev);
779 del_timer_sync(&card->tx_timer);
781 /* disable/mask all interrupts */
782 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, 0);
783 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK, 0);
784 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK, 0);
786 /* free_irq(netdev->irq, netdev);*/
787 free_irq(to_pci_dev(netdev->class_dev.dev)->irq, netdev);
789 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
790 SPIDER_NET_DMA_TX_FEND_VALUE);
792 /* turn off DMA, force end */
793 spider_net_disable_rxdmac(card);
795 /* release chains */
796 spider_net_release_tx_chain(card, 1);
798 spider_net_free_chain(card, &card->tx_chain);
799 spider_net_free_chain(card, &card->rx_chain);
801 return 0;
805 * spider_net_get_next_tx_descr - returns the next available tx descriptor
806 * @card: device structure to get descriptor from
808 * returns the address of the next descriptor, or NULL if not available.
810 static struct spider_net_descr *
811 spider_net_get_next_tx_descr(struct spider_net_card *card)
813 /* check, if head points to not-in-use descr */
814 if ( spider_net_get_descr_status(card->tx_chain.head) ==
815 SPIDER_NET_DESCR_NOT_IN_USE ) {
816 return card->tx_chain.head;
817 } else {
818 return NULL;
823 * spider_net_set_txdescr_cmdstat - sets the tx descriptor command field
824 * @descr: descriptor structure to fill out
825 * @skb: packet to consider
827 * fills out the command and status field of the descriptor structure,
828 * depending on hardware checksum settings.
830 static void
831 spider_net_set_txdescr_cmdstat(struct spider_net_descr *descr,
832 struct sk_buff *skb)
834 /* make sure the other fields in the descriptor are written */
835 wmb();
837 if (skb->ip_summed != CHECKSUM_HW) {
838 descr->dmac_cmd_status = SPIDER_NET_DMAC_CMDSTAT_NOCS;
839 return;
842 /* is packet ip?
843 * if yes: tcp? udp? */
844 if (skb->protocol == htons(ETH_P_IP)) {
845 if (skb->nh.iph->protocol == IPPROTO_TCP)
846 descr->dmac_cmd_status = SPIDER_NET_DMAC_CMDSTAT_TCPCS;
847 else if (skb->nh.iph->protocol == IPPROTO_UDP)
848 descr->dmac_cmd_status = SPIDER_NET_DMAC_CMDSTAT_UDPCS;
849 else /* the stack should checksum non-tcp and non-udp
850 packets on his own: NETIF_F_IP_CSUM */
851 descr->dmac_cmd_status = SPIDER_NET_DMAC_CMDSTAT_NOCS;
856 * spider_net_prepare_tx_descr - fill tx descriptor with skb data
857 * @card: card structure
858 * @descr: descriptor structure to fill out
859 * @skb: packet to use
861 * returns 0 on success, <0 on failure.
863 * fills out the descriptor structure with skb data and len. Copies data,
864 * if needed (32bit DMA!)
866 static int
867 spider_net_prepare_tx_descr(struct spider_net_card *card,
868 struct spider_net_descr *descr,
869 struct sk_buff *skb)
871 dma_addr_t buf;
873 buf = pci_map_single(card->pdev, skb->data,
874 skb->len, PCI_DMA_BIDIRECTIONAL);
875 if (buf == DMA_ERROR_CODE) {
876 if (netif_msg_tx_err(card) && net_ratelimit())
877 pr_err("could not iommu-map packet (%p, %i). "
878 "Dropping packet\n", skb->data, skb->len);
879 return -ENOMEM;
882 descr->buf_addr = buf;
883 descr->buf_size = skb->len;
884 descr->skb = skb;
885 descr->data_status = 0;
887 spider_net_set_txdescr_cmdstat(descr,skb);
889 return 0;
893 * spider_net_kick_tx_dma - enables TX DMA processing
894 * @card: card structure
895 * @descr: descriptor address to enable TX processing at
897 * spider_net_kick_tx_dma writes the current tx chain head as start address
898 * of the tx descriptor chain and enables the transmission DMA engine
900 static void
901 spider_net_kick_tx_dma(struct spider_net_card *card,
902 struct spider_net_descr *descr)
904 /* this is the only descriptor in the output chain.
905 * Enable TX DMA */
907 spider_net_write_reg(card, SPIDER_NET_GDTDCHA,
908 descr->bus_addr);
910 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
911 SPIDER_NET_DMA_TX_VALUE);
915 * spider_net_xmit - transmits a frame over the device
916 * @skb: packet to send out
917 * @netdev: interface device structure
919 * returns 0 on success, <0 on failure
921 static int
922 spider_net_xmit(struct sk_buff *skb, struct net_device *netdev)
924 struct spider_net_card *card = netdev_priv(netdev);
925 struct spider_net_descr *descr;
926 int result;
928 spider_net_release_tx_chain(card, 0);
930 descr = spider_net_get_next_tx_descr(card);
932 if (!descr)
933 goto error;
935 result = spider_net_prepare_tx_descr(card, descr, skb);
936 if (result)
937 goto error;
939 card->tx_chain.head = card->tx_chain.head->next;
941 if (spider_net_get_descr_status(descr->prev) !=
942 SPIDER_NET_DESCR_CARDOWNED) {
943 /* make sure the current descriptor is in memory. Then
944 * kicking it on again makes sense, if the previous is not
945 * card-owned anymore. Check the previous descriptor twice
946 * to omit an mb() in heavy traffic cases */
947 mb();
948 if (spider_net_get_descr_status(descr->prev) !=
949 SPIDER_NET_DESCR_CARDOWNED)
950 spider_net_kick_tx_dma(card, descr);
953 mod_timer(&card->tx_timer, jiffies + SPIDER_NET_TX_TIMER);
955 return NETDEV_TX_OK;
957 error:
958 card->netdev_stats.tx_dropped++;
959 return NETDEV_TX_BUSY;
963 * spider_net_do_ioctl - called for device ioctls
964 * @netdev: interface device structure
965 * @ifr: request parameter structure for ioctl
966 * @cmd: command code for ioctl
968 * returns 0 on success, <0 on failure. Currently, we have no special ioctls.
969 * -EOPNOTSUPP is returned, if an unknown ioctl was requested
971 static int
972 spider_net_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
974 switch (cmd) {
975 default:
976 return -EOPNOTSUPP;
981 * spider_net_pass_skb_up - takes an skb from a descriptor and passes it on
982 * @descr: descriptor to process
983 * @card: card structure
984 * @napi: whether caller is in NAPI context
986 * returns 1 on success, 0 if no packet was passed to the stack
988 * iommu-unmaps the skb, fills out skb structure and passes the data to the
989 * stack. The descriptor state is not changed.
991 static int
992 spider_net_pass_skb_up(struct spider_net_descr *descr,
993 struct spider_net_card *card, int napi)
995 struct sk_buff *skb;
996 struct net_device *netdev;
997 u32 data_status, data_error;
999 data_status = descr->data_status;
1000 data_error = descr->data_error;
1002 netdev = card->netdev;
1004 /* unmap descriptor */
1005 pci_unmap_single(card->pdev, descr->buf_addr, SPIDER_NET_MAX_FRAME,
1006 PCI_DMA_BIDIRECTIONAL);
1008 /* the cases we'll throw away the packet immediately */
1009 if (data_error & SPIDER_NET_DESTROY_RX_FLAGS) {
1010 if (netif_msg_rx_err(card))
1011 pr_err("error in received descriptor found, "
1012 "data_status=x%08x, data_error=x%08x\n",
1013 data_status, data_error);
1014 return 0;
1017 skb = descr->skb;
1018 skb->dev = netdev;
1019 skb_put(skb, descr->valid_size);
1021 /* the card seems to add 2 bytes of junk in front
1022 * of the ethernet frame */
1023 #define SPIDER_MISALIGN 2
1024 skb_pull(skb, SPIDER_MISALIGN);
1025 skb->protocol = eth_type_trans(skb, netdev);
1027 /* checksum offload */
1028 if (card->options.rx_csum) {
1029 if ( ( (data_status & SPIDER_NET_DATA_STATUS_CKSUM_MASK) ==
1030 SPIDER_NET_DATA_STATUS_CKSUM_MASK) &&
1031 !(data_error & SPIDER_NET_DATA_ERR_CKSUM_MASK))
1032 skb->ip_summed = CHECKSUM_UNNECESSARY;
1033 else
1034 skb->ip_summed = CHECKSUM_NONE;
1035 } else
1036 skb->ip_summed = CHECKSUM_NONE;
1038 if (data_status & SPIDER_NET_VLAN_PACKET) {
1039 /* further enhancements: HW-accel VLAN
1040 * vlan_hwaccel_receive_skb
1044 /* pass skb up to stack */
1045 if (napi)
1046 netif_receive_skb(skb);
1047 else
1048 netif_rx_ni(skb);
1050 /* update netdevice statistics */
1051 card->netdev_stats.rx_packets++;
1052 card->netdev_stats.rx_bytes += skb->len;
1054 return 1;
1058 * spider_net_decode_one_descr - processes an rx descriptor
1059 * @card: card structure
1060 * @napi: whether caller is in NAPI context
1062 * returns 1 if a packet has been sent to the stack, otherwise 0
1064 * processes an rx descriptor by iommu-unmapping the data buffer and passing
1065 * the packet up to the stack. This function is called in softirq
1066 * context, e.g. either bottom half from interrupt or NAPI polling context
1068 static int
1069 spider_net_decode_one_descr(struct spider_net_card *card, int napi)
1071 enum spider_net_descr_status status;
1072 struct spider_net_descr *descr;
1073 struct spider_net_descr_chain *chain;
1074 int result;
1076 chain = &card->rx_chain;
1077 descr = chain->tail;
1079 status = spider_net_get_descr_status(descr);
1081 if (status == SPIDER_NET_DESCR_CARDOWNED) {
1082 /* nothing in the descriptor yet */
1083 result=0;
1084 goto out;
1087 if (status == SPIDER_NET_DESCR_NOT_IN_USE) {
1088 /* not initialized yet, the ring must be empty */
1089 spider_net_refill_rx_chain(card);
1090 spider_net_enable_rxdmac(card);
1091 result=0;
1092 goto out;
1095 /* descriptor definitively used -- move on tail */
1096 chain->tail = descr->next;
1098 result = 0;
1099 if ( (status == SPIDER_NET_DESCR_RESPONSE_ERROR) ||
1100 (status == SPIDER_NET_DESCR_PROTECTION_ERROR) ||
1101 (status == SPIDER_NET_DESCR_FORCE_END) ) {
1102 if (netif_msg_rx_err(card))
1103 pr_err("%s: dropping RX descriptor with state %d\n",
1104 card->netdev->name, status);
1105 card->netdev_stats.rx_dropped++;
1106 pci_unmap_single(card->pdev, descr->buf_addr,
1107 SPIDER_NET_MAX_FRAME, PCI_DMA_BIDIRECTIONAL);
1108 dev_kfree_skb_irq(descr->skb);
1109 goto refill;
1112 if ( (status != SPIDER_NET_DESCR_COMPLETE) &&
1113 (status != SPIDER_NET_DESCR_FRAME_END) ) {
1114 if (netif_msg_rx_err(card))
1115 pr_err("%s: RX descriptor with state %d\n",
1116 card->netdev->name, status);
1117 goto refill;
1120 /* ok, we've got a packet in descr */
1121 result = spider_net_pass_skb_up(descr, card, napi);
1122 refill:
1123 spider_net_set_descr_status(descr, SPIDER_NET_DESCR_NOT_IN_USE);
1124 /* change the descriptor state: */
1125 if (!napi)
1126 spider_net_refill_rx_chain(card);
1127 out:
1128 return result;
1132 * spider_net_poll - NAPI poll function called by the stack to return packets
1133 * @netdev: interface device structure
1134 * @budget: number of packets we can pass to the stack at most
1136 * returns 0 if no more packets available to the driver/stack. Returns 1,
1137 * if the quota is exceeded, but the driver has still packets.
1139 * spider_net_poll returns all packets from the rx descriptors to the stack
1140 * (using netif_receive_skb). If all/enough packets are up, the driver
1141 * reenables interrupts and returns 0. If not, 1 is returned.
1143 static int
1144 spider_net_poll(struct net_device *netdev, int *budget)
1146 struct spider_net_card *card = netdev_priv(netdev);
1147 int packets_to_do, packets_done = 0;
1148 int no_more_packets = 0;
1150 packets_to_do = min(*budget, netdev->quota);
1152 while (packets_to_do) {
1153 if (spider_net_decode_one_descr(card, 1)) {
1154 packets_done++;
1155 packets_to_do--;
1156 } else {
1157 /* no more packets for the stack */
1158 no_more_packets = 1;
1159 break;
1163 netdev->quota -= packets_done;
1164 *budget -= packets_done;
1165 spider_net_refill_rx_chain(card);
1167 /* if all packets are in the stack, enable interrupts and return 0 */
1168 /* if not, return 1 */
1169 if (no_more_packets) {
1170 netif_rx_complete(netdev);
1171 spider_net_rx_irq_on(card);
1172 return 0;
1175 return 1;
1179 * spider_net_vlan_rx_reg - initializes VLAN structures in the driver and card
1180 * @netdev: interface device structure
1181 * @grp: vlan_group structure that is registered (NULL on destroying interface)
1183 static void
1184 spider_net_vlan_rx_reg(struct net_device *netdev, struct vlan_group *grp)
1186 /* further enhancement... yet to do */
1187 return;
1191 * spider_net_vlan_rx_add - adds VLAN id to the card filter
1192 * @netdev: interface device structure
1193 * @vid: VLAN id to add
1195 static void
1196 spider_net_vlan_rx_add(struct net_device *netdev, uint16_t vid)
1198 /* further enhancement... yet to do */
1199 /* add vid to card's VLAN filter table */
1200 return;
1204 * spider_net_vlan_rx_kill - removes VLAN id to the card filter
1205 * @netdev: interface device structure
1206 * @vid: VLAN id to remove
1208 static void
1209 spider_net_vlan_rx_kill(struct net_device *netdev, uint16_t vid)
1211 /* further enhancement... yet to do */
1212 /* remove vid from card's VLAN filter table */
1216 * spider_net_get_stats - get interface statistics
1217 * @netdev: interface device structure
1219 * returns the interface statistics residing in the spider_net_card struct
1221 static struct net_device_stats *
1222 spider_net_get_stats(struct net_device *netdev)
1224 struct spider_net_card *card = netdev_priv(netdev);
1225 struct net_device_stats *stats = &card->netdev_stats;
1226 return stats;
1230 * spider_net_change_mtu - changes the MTU of an interface
1231 * @netdev: interface device structure
1232 * @new_mtu: new MTU value
1234 * returns 0 on success, <0 on failure
1236 static int
1237 spider_net_change_mtu(struct net_device *netdev, int new_mtu)
1239 /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
1240 * and mtu is outbound only anyway */
1241 if ( (new_mtu < SPIDER_NET_MIN_MTU ) ||
1242 (new_mtu > SPIDER_NET_MAX_MTU) )
1243 return -EINVAL;
1244 netdev->mtu = new_mtu;
1245 return 0;
1249 * spider_net_set_mac - sets the MAC of an interface
1250 * @netdev: interface device structure
1251 * @ptr: pointer to new MAC address
1253 * Returns 0 on success, <0 on failure. Currently, we don't support this
1254 * and will always return EOPNOTSUPP.
1256 static int
1257 spider_net_set_mac(struct net_device *netdev, void *p)
1259 struct spider_net_card *card = netdev_priv(netdev);
1260 u32 macl, macu, regvalue;
1261 struct sockaddr *addr = p;
1263 if (!is_valid_ether_addr(addr->sa_data))
1264 return -EADDRNOTAVAIL;
1266 /* switch off GMACTPE and GMACRPE */
1267 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1268 regvalue &= ~((1 << 5) | (1 << 6));
1269 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1271 /* write mac */
1272 macu = (addr->sa_data[0]<<24) + (addr->sa_data[1]<<16) +
1273 (addr->sa_data[2]<<8) + (addr->sa_data[3]);
1274 macl = (addr->sa_data[4]<<8) + (addr->sa_data[5]);
1275 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACU, macu);
1276 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACL, macl);
1278 /* switch GMACTPE and GMACRPE back on */
1279 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1280 regvalue |= ((1 << 5) | (1 << 6));
1281 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1283 spider_net_set_promisc(card);
1285 /* look up, whether we have been successful */
1286 if (spider_net_get_mac_address(netdev))
1287 return -EADDRNOTAVAIL;
1288 if (memcmp(netdev->dev_addr,addr->sa_data,netdev->addr_len))
1289 return -EADDRNOTAVAIL;
1291 return 0;
1295 * spider_net_enable_txdmac - enables a TX DMA controller
1296 * @card: card structure
1298 * spider_net_enable_txdmac enables the TX DMA controller by setting the
1299 * descriptor chain tail address
1301 static void
1302 spider_net_enable_txdmac(struct spider_net_card *card)
1304 /* assume chain is aligned correctly */
1305 spider_net_write_reg(card, SPIDER_NET_GDTDCHA,
1306 card->tx_chain.tail->bus_addr);
1310 * spider_net_handle_rxram_full - cleans up RX ring upon RX RAM full interrupt
1311 * @card: card structure
1313 * spider_net_handle_rxram_full empties the RX ring so that spider can put
1314 * more packets in it and empty its RX RAM. This is called in bottom half
1315 * context
1317 static void
1318 spider_net_handle_rxram_full(struct spider_net_card *card)
1320 while (spider_net_decode_one_descr(card, 0))
1322 spider_net_enable_rxchtails(card);
1323 spider_net_enable_rxdmac(card);
1324 netif_rx_schedule(card->netdev);
1328 * spider_net_handle_error_irq - handles errors raised by an interrupt
1329 * @card: card structure
1330 * @status_reg: interrupt status register 0 (GHIINT0STS)
1332 * spider_net_handle_error_irq treats or ignores all error conditions
1333 * found when an interrupt is presented
1335 static void
1336 spider_net_handle_error_irq(struct spider_net_card *card, u32 status_reg)
1338 u32 error_reg1, error_reg2;
1339 u32 i;
1340 int show_error = 1;
1342 error_reg1 = spider_net_read_reg(card, SPIDER_NET_GHIINT1STS);
1343 error_reg2 = spider_net_read_reg(card, SPIDER_NET_GHIINT2STS);
1345 /* check GHIINT0STS ************************************/
1346 if (status_reg)
1347 for (i = 0; i < 32; i++)
1348 if (status_reg & (1<<i))
1349 switch (i)
1351 /* let error_reg1 and error_reg2 evaluation decide, what to do
1352 case SPIDER_NET_PHYINT:
1353 case SPIDER_NET_GMAC2INT:
1354 case SPIDER_NET_GMAC1INT:
1355 case SPIDER_NET_GIPSINT:
1356 case SPIDER_NET_GFIFOINT:
1357 case SPIDER_NET_DMACINT:
1358 case SPIDER_NET_GSYSINT:
1359 break; */
1361 case SPIDER_NET_GPWOPCMPINT:
1362 /* PHY write operation completed */
1363 show_error = 0;
1364 break;
1365 case SPIDER_NET_GPROPCMPINT:
1366 /* PHY read operation completed */
1367 /* we don't use semaphores, as we poll for the completion
1368 * of the read operation in spider_net_read_phy. Should take
1369 * about 50 us */
1370 show_error = 0;
1371 break;
1372 case SPIDER_NET_GPWFFINT:
1373 /* PHY command queue full */
1374 if (netif_msg_intr(card))
1375 pr_err("PHY write queue full\n");
1376 show_error = 0;
1377 break;
1379 /* case SPIDER_NET_GRMDADRINT: not used. print a message */
1380 /* case SPIDER_NET_GRMARPINT: not used. print a message */
1381 /* case SPIDER_NET_GRMMPINT: not used. print a message */
1383 case SPIDER_NET_GDTDEN0INT:
1384 /* someone has set TX_DMA_EN to 0 */
1385 show_error = 0;
1386 break;
1388 case SPIDER_NET_GDDDEN0INT: /* fallthrough */
1389 case SPIDER_NET_GDCDEN0INT: /* fallthrough */
1390 case SPIDER_NET_GDBDEN0INT: /* fallthrough */
1391 case SPIDER_NET_GDADEN0INT:
1392 /* someone has set RX_DMA_EN to 0 */
1393 show_error = 0;
1394 break;
1396 /* RX interrupts */
1397 case SPIDER_NET_GDDFDCINT:
1398 case SPIDER_NET_GDCFDCINT:
1399 case SPIDER_NET_GDBFDCINT:
1400 case SPIDER_NET_GDAFDCINT:
1401 /* case SPIDER_NET_GDNMINT: not used. print a message */
1402 /* case SPIDER_NET_GCNMINT: not used. print a message */
1403 /* case SPIDER_NET_GBNMINT: not used. print a message */
1404 /* case SPIDER_NET_GANMINT: not used. print a message */
1405 /* case SPIDER_NET_GRFNMINT: not used. print a message */
1406 show_error = 0;
1407 break;
1409 /* TX interrupts */
1410 case SPIDER_NET_GDTFDCINT:
1411 show_error = 0;
1412 break;
1413 case SPIDER_NET_GTTEDINT:
1414 show_error = 0;
1415 break;
1416 case SPIDER_NET_GDTDCEINT:
1417 /* chain end. If a descriptor should be sent, kick off
1418 * tx dma
1419 if (card->tx_chain.tail == card->tx_chain.head)
1420 spider_net_kick_tx_dma(card);
1421 show_error = 0; */
1422 break;
1424 /* case SPIDER_NET_G1TMCNTINT: not used. print a message */
1425 /* case SPIDER_NET_GFREECNTINT: not used. print a message */
1428 /* check GHIINT1STS ************************************/
1429 if (error_reg1)
1430 for (i = 0; i < 32; i++)
1431 if (error_reg1 & (1<<i))
1432 switch (i)
1434 case SPIDER_NET_GTMFLLINT:
1435 if (netif_msg_intr(card) && net_ratelimit())
1436 pr_err("Spider TX RAM full\n");
1437 show_error = 0;
1438 break;
1439 case SPIDER_NET_GRFDFLLINT: /* fallthrough */
1440 case SPIDER_NET_GRFCFLLINT: /* fallthrough */
1441 case SPIDER_NET_GRFBFLLINT: /* fallthrough */
1442 case SPIDER_NET_GRFAFLLINT: /* fallthrough */
1443 case SPIDER_NET_GRMFLLINT:
1444 if (netif_msg_intr(card) && net_ratelimit())
1445 pr_debug("Spider RX RAM full, incoming packets "
1446 "might be discarded!\n");
1447 spider_net_rx_irq_off(card);
1448 tasklet_schedule(&card->rxram_full_tl);
1449 show_error = 0;
1450 break;
1452 /* case SPIDER_NET_GTMSHTINT: problem, print a message */
1453 case SPIDER_NET_GDTINVDINT:
1454 /* allrighty. tx from previous descr ok */
1455 show_error = 0;
1456 break;
1458 /* chain end */
1459 case SPIDER_NET_GDDDCEINT: /* fallthrough */
1460 case SPIDER_NET_GDCDCEINT: /* fallthrough */
1461 case SPIDER_NET_GDBDCEINT: /* fallthrough */
1462 case SPIDER_NET_GDADCEINT:
1463 if (netif_msg_intr(card))
1464 pr_err("got descriptor chain end interrupt, "
1465 "restarting DMAC %c.\n",
1466 'D'+i-SPIDER_NET_GDDDCEINT);
1467 spider_net_refill_rx_chain(card);
1468 spider_net_enable_rxdmac(card);
1469 show_error = 0;
1470 break;
1472 /* invalid descriptor */
1473 case SPIDER_NET_GDDINVDINT: /* fallthrough */
1474 case SPIDER_NET_GDCINVDINT: /* fallthrough */
1475 case SPIDER_NET_GDBINVDINT: /* fallthrough */
1476 case SPIDER_NET_GDAINVDINT:
1477 /* could happen when rx chain is full */
1478 spider_net_refill_rx_chain(card);
1479 spider_net_enable_rxdmac(card);
1480 show_error = 0;
1481 break;
1483 /* case SPIDER_NET_GDTRSERINT: problem, print a message */
1484 /* case SPIDER_NET_GDDRSERINT: problem, print a message */
1485 /* case SPIDER_NET_GDCRSERINT: problem, print a message */
1486 /* case SPIDER_NET_GDBRSERINT: problem, print a message */
1487 /* case SPIDER_NET_GDARSERINT: problem, print a message */
1488 /* case SPIDER_NET_GDSERINT: problem, print a message */
1489 /* case SPIDER_NET_GDTPTERINT: problem, print a message */
1490 /* case SPIDER_NET_GDDPTERINT: problem, print a message */
1491 /* case SPIDER_NET_GDCPTERINT: problem, print a message */
1492 /* case SPIDER_NET_GDBPTERINT: problem, print a message */
1493 /* case SPIDER_NET_GDAPTERINT: problem, print a message */
1494 default:
1495 show_error = 1;
1496 break;
1499 /* check GHIINT2STS ************************************/
1500 if (error_reg2)
1501 for (i = 0; i < 32; i++)
1502 if (error_reg2 & (1<<i))
1503 switch (i)
1505 /* there is nothing we can (want to) do at this time. Log a
1506 * message, we can switch on and off the specific values later on
1507 case SPIDER_NET_GPROPERINT:
1508 case SPIDER_NET_GMCTCRSNGINT:
1509 case SPIDER_NET_GMCTLCOLINT:
1510 case SPIDER_NET_GMCTTMOTINT:
1511 case SPIDER_NET_GMCRCAERINT:
1512 case SPIDER_NET_GMCRCALERINT:
1513 case SPIDER_NET_GMCRALNERINT:
1514 case SPIDER_NET_GMCROVRINT:
1515 case SPIDER_NET_GMCRRNTINT:
1516 case SPIDER_NET_GMCRRXERINT:
1517 case SPIDER_NET_GTITCSERINT:
1518 case SPIDER_NET_GTIFMTERINT:
1519 case SPIDER_NET_GTIPKTRVKINT:
1520 case SPIDER_NET_GTISPINGINT:
1521 case SPIDER_NET_GTISADNGINT:
1522 case SPIDER_NET_GTISPDNGINT:
1523 case SPIDER_NET_GRIFMTERINT:
1524 case SPIDER_NET_GRIPKTRVKINT:
1525 case SPIDER_NET_GRISPINGINT:
1526 case SPIDER_NET_GRISADNGINT:
1527 case SPIDER_NET_GRISPDNGINT:
1528 break;
1530 default:
1531 break;
1534 if ((show_error) && (netif_msg_intr(card)))
1535 pr_err("Got error interrupt, GHIINT0STS = 0x%08x, "
1536 "GHIINT1STS = 0x%08x, GHIINT2STS = 0x%08x\n",
1537 status_reg, error_reg1, error_reg2);
1539 /* clear interrupt sources */
1540 spider_net_write_reg(card, SPIDER_NET_GHIINT1STS, error_reg1);
1541 spider_net_write_reg(card, SPIDER_NET_GHIINT2STS, error_reg2);
1545 * spider_net_interrupt - interrupt handler for spider_net
1546 * @irq: interupt number
1547 * @ptr: pointer to net_device
1548 * @regs: PU registers
1550 * returns IRQ_HANDLED, if interrupt was for driver, or IRQ_NONE, if no
1551 * interrupt found raised by card.
1553 * This is the interrupt handler, that turns off
1554 * interrupts for this device and makes the stack poll the driver
1556 static irqreturn_t
1557 spider_net_interrupt(int irq, void *ptr, struct pt_regs *regs)
1559 struct net_device *netdev = ptr;
1560 struct spider_net_card *card = netdev_priv(netdev);
1561 u32 status_reg;
1563 status_reg = spider_net_read_reg(card, SPIDER_NET_GHIINT0STS);
1565 if (!status_reg)
1566 return IRQ_NONE;
1568 if (status_reg & SPIDER_NET_RXINT ) {
1569 spider_net_rx_irq_off(card);
1570 netif_rx_schedule(netdev);
1573 if (status_reg & SPIDER_NET_ERRINT )
1574 spider_net_handle_error_irq(card, status_reg);
1576 /* clear interrupt sources */
1577 spider_net_write_reg(card, SPIDER_NET_GHIINT0STS, status_reg);
1579 return IRQ_HANDLED;
1582 #ifdef CONFIG_NET_POLL_CONTROLLER
1584 * spider_net_poll_controller - artificial interrupt for netconsole etc.
1585 * @netdev: interface device structure
1587 * see Documentation/networking/netconsole.txt
1589 static void
1590 spider_net_poll_controller(struct net_device *netdev)
1592 disable_irq(netdev->irq);
1593 spider_net_interrupt(netdev->irq, netdev, NULL);
1594 enable_irq(netdev->irq);
1596 #endif /* CONFIG_NET_POLL_CONTROLLER */
1599 * spider_net_init_card - initializes the card
1600 * @card: card structure
1602 * spider_net_init_card initializes the card so that other registers can
1603 * be used
1605 static void
1606 spider_net_init_card(struct spider_net_card *card)
1608 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1609 SPIDER_NET_CKRCTRL_STOP_VALUE);
1611 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1612 SPIDER_NET_CKRCTRL_RUN_VALUE);
1616 * spider_net_enable_card - enables the card by setting all kinds of regs
1617 * @card: card structure
1619 * spider_net_enable_card sets a lot of SMMIO registers to enable the device
1621 static void
1622 spider_net_enable_card(struct spider_net_card *card)
1624 int i;
1625 /* the following array consists of (register),(value) pairs
1626 * that are set in this function. A register of 0 ends the list */
1627 u32 regs[][2] = {
1628 { SPIDER_NET_GRESUMINTNUM, 0 },
1629 { SPIDER_NET_GREINTNUM, 0 },
1631 /* set interrupt frame number registers */
1632 /* clear the single DMA engine registers first */
1633 { SPIDER_NET_GFAFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1634 { SPIDER_NET_GFBFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1635 { SPIDER_NET_GFCFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1636 { SPIDER_NET_GFDFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1637 /* then set, what we really need */
1638 { SPIDER_NET_GFFRMNUM, SPIDER_NET_FRAMENUM_VALUE },
1640 /* timer counter registers and stuff */
1641 { SPIDER_NET_GFREECNNUM, 0 },
1642 { SPIDER_NET_GONETIMENUM, 0 },
1643 { SPIDER_NET_GTOUTFRMNUM, 0 },
1645 /* RX mode setting */
1646 { SPIDER_NET_GRXMDSET, SPIDER_NET_RXMODE_VALUE },
1647 /* TX mode setting */
1648 { SPIDER_NET_GTXMDSET, SPIDER_NET_TXMODE_VALUE },
1649 /* IPSEC mode setting */
1650 { SPIDER_NET_GIPSECINIT, SPIDER_NET_IPSECINIT_VALUE },
1652 { SPIDER_NET_GFTRESTRT, SPIDER_NET_RESTART_VALUE },
1654 { SPIDER_NET_GMRWOLCTRL, 0 },
1655 { SPIDER_NET_GTESTMD, 0x10000000 },
1656 { SPIDER_NET_GTTQMSK, 0x00400040 },
1657 { SPIDER_NET_GTESTMD, 0 },
1659 { SPIDER_NET_GMACINTEN, 0 },
1661 /* flow control stuff */
1662 { SPIDER_NET_GMACAPAUSE, SPIDER_NET_MACAPAUSE_VALUE },
1663 { SPIDER_NET_GMACTXPAUSE, SPIDER_NET_TXPAUSE_VALUE },
1665 { SPIDER_NET_GMACBSTLMT, SPIDER_NET_BURSTLMT_VALUE },
1666 { 0, 0}
1669 i = 0;
1670 while (regs[i][0]) {
1671 spider_net_write_reg(card, regs[i][0], regs[i][1]);
1672 i++;
1675 /* clear unicast filter table entries 1 to 14 */
1676 for (i = 1; i <= 14; i++) {
1677 spider_net_write_reg(card,
1678 SPIDER_NET_GMRUAFILnR + i * 8,
1679 0x00080000);
1680 spider_net_write_reg(card,
1681 SPIDER_NET_GMRUAFILnR + i * 8 + 4,
1682 0x00000000);
1685 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R, 0x08080000);
1687 spider_net_write_reg(card, SPIDER_NET_ECMODE, SPIDER_NET_ECMODE_VALUE);
1689 /* set chain tail adress for RX chains and
1690 * enable DMA */
1691 spider_net_enable_rxchtails(card);
1692 spider_net_enable_rxdmac(card);
1694 spider_net_write_reg(card, SPIDER_NET_GRXDMAEN, SPIDER_NET_WOL_VALUE);
1696 /* set chain tail adress for TX chain */
1697 spider_net_enable_txdmac(card);
1699 spider_net_write_reg(card, SPIDER_NET_GMACLENLMT,
1700 SPIDER_NET_LENLMT_VALUE);
1701 spider_net_write_reg(card, SPIDER_NET_GMACMODE,
1702 SPIDER_NET_MACMODE_VALUE);
1703 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD,
1704 SPIDER_NET_OPMODE_VALUE);
1706 /* set interrupt mask registers */
1707 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK,
1708 SPIDER_NET_INT0_MASK_VALUE);
1709 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK,
1710 SPIDER_NET_INT1_MASK_VALUE);
1711 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK,
1712 SPIDER_NET_INT2_MASK_VALUE);
1716 * spider_net_open - called upon ifonfig up
1717 * @netdev: interface device structure
1719 * returns 0 on success, <0 on failure
1721 * spider_net_open allocates all the descriptors and memory needed for
1722 * operation, sets up multicast list and enables interrupts
1725 spider_net_open(struct net_device *netdev)
1727 struct spider_net_card *card = netdev_priv(netdev);
1728 int result;
1730 result = -ENOMEM;
1731 if (spider_net_init_chain(card, &card->tx_chain,
1732 card->descr, tx_descriptors))
1733 goto alloc_tx_failed;
1734 if (spider_net_init_chain(card, &card->rx_chain,
1735 card->descr + tx_descriptors, rx_descriptors))
1736 goto alloc_rx_failed;
1738 /* allocate rx skbs */
1739 if (spider_net_alloc_rx_skbs(card))
1740 goto alloc_skbs_failed;
1742 spider_net_set_multi(netdev);
1744 /* further enhancement: setup hw vlan, if needed */
1746 result = -EBUSY;
1747 if (request_irq(netdev->irq, spider_net_interrupt,
1748 SA_SHIRQ, netdev->name, netdev))
1749 goto register_int_failed;
1751 spider_net_enable_card(card);
1753 netif_start_queue(netdev);
1754 netif_carrier_on(netdev);
1755 netif_poll_enable(netdev);
1757 return 0;
1759 register_int_failed:
1760 spider_net_free_rx_chain_contents(card);
1761 alloc_skbs_failed:
1762 spider_net_free_chain(card, &card->rx_chain);
1763 alloc_rx_failed:
1764 spider_net_free_chain(card, &card->tx_chain);
1765 alloc_tx_failed:
1766 return result;
1770 * spider_net_setup_phy - setup PHY
1771 * @card: card structure
1773 * returns 0 on success, <0 on failure
1775 * spider_net_setup_phy is used as part of spider_net_probe. Sets
1776 * the PHY to 1000 Mbps
1778 static int
1779 spider_net_setup_phy(struct spider_net_card *card)
1781 struct mii_phy *phy = &card->phy;
1783 spider_net_write_reg(card, SPIDER_NET_GDTDMASEL,
1784 SPIDER_NET_DMASEL_VALUE);
1785 spider_net_write_reg(card, SPIDER_NET_GPCCTRL,
1786 SPIDER_NET_PHY_CTRL_VALUE);
1787 phy->mii_id = 1;
1788 phy->dev = card->netdev;
1789 phy->mdio_read = spider_net_read_phy;
1790 phy->mdio_write = spider_net_write_phy;
1792 mii_phy_probe(phy, phy->mii_id);
1794 if (phy->def->ops->setup_forced)
1795 phy->def->ops->setup_forced(phy, SPEED_1000, DUPLEX_FULL);
1797 phy->def->ops->enable_fiber(phy);
1799 phy->def->ops->read_link(phy);
1800 pr_info("Found %s with %i Mbps, %s-duplex.\n", phy->def->name,
1801 phy->speed, phy->duplex==1 ? "Full" : "Half");
1803 return 0;
1807 * spider_net_download_firmware - loads firmware into the adapter
1808 * @card: card structure
1809 * @firmware_ptr: pointer to firmware data
1811 * spider_net_download_firmware loads the firmware data into the
1812 * adapter. It assumes the length etc. to be allright.
1814 static int
1815 spider_net_download_firmware(struct spider_net_card *card,
1816 u8 *firmware_ptr)
1818 int sequencer, i;
1819 u32 *fw_ptr = (u32 *)firmware_ptr;
1821 /* stop sequencers */
1822 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1823 SPIDER_NET_STOP_SEQ_VALUE);
1825 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1826 sequencer++) {
1827 spider_net_write_reg(card,
1828 SPIDER_NET_GSnPRGADR + sequencer * 8, 0);
1829 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
1830 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1831 sequencer * 8, *fw_ptr);
1832 fw_ptr++;
1836 if (spider_net_read_reg(card, SPIDER_NET_GSINIT))
1837 return -EIO;
1839 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1840 SPIDER_NET_RUN_SEQ_VALUE);
1842 return 0;
1846 * spider_net_init_firmware - reads in firmware parts
1847 * @card: card structure
1849 * Returns 0 on success, <0 on failure
1851 * spider_net_init_firmware opens the sequencer firmware and does some basic
1852 * checks. This function opens and releases the firmware structure. A call
1853 * to download the firmware is performed before the release.
1855 * Firmware format
1856 * ===============
1857 * spider_fw.bin is expected to be a file containing 6*1024*4 bytes, 4k being
1858 * the program for each sequencer. Use the command
1859 * tail -q -n +2 Seq_code1_0x088.txt Seq_code2_0x090.txt \
1860 * Seq_code3_0x098.txt Seq_code4_0x0A0.txt Seq_code5_0x0A8.txt \
1861 * Seq_code6_0x0B0.txt | xxd -r -p -c4 > spider_fw.bin
1863 * to generate spider_fw.bin, if you have sequencer programs with something
1864 * like the following contents for each sequencer:
1865 * <ONE LINE COMMENT>
1866 * <FIRST 4-BYTES-WORD FOR SEQUENCER>
1867 * <SECOND 4-BYTES-WORD FOR SEQUENCER>
1868 * ...
1869 * <1024th 4-BYTES-WORD FOR SEQUENCER>
1871 static int
1872 spider_net_init_firmware(struct spider_net_card *card)
1874 struct firmware *firmware = NULL;
1875 struct device_node *dn;
1876 u8 *fw_prop = NULL;
1877 int err = -ENOENT;
1878 int fw_size;
1880 if (request_firmware((const struct firmware **)&firmware,
1881 SPIDER_NET_FIRMWARE_NAME, &card->pdev->dev) == 0) {
1882 if ( (firmware->size != SPIDER_NET_FIRMWARE_LEN) &&
1883 netif_msg_probe(card) ) {
1884 pr_err("Incorrect size of spidernet firmware in " \
1885 "filesystem. Looking in host firmware...\n");
1886 goto try_host_fw;
1888 err = spider_net_download_firmware(card, firmware->data);
1890 release_firmware(firmware);
1891 if (err)
1892 goto try_host_fw;
1894 goto done;
1897 try_host_fw:
1898 dn = pci_device_to_OF_node(card->pdev);
1899 if (!dn)
1900 goto out_err;
1902 fw_prop = (u8 *)get_property(dn, "firmware", &fw_size);
1903 if (!fw_prop)
1904 goto out_err;
1906 if ( (fw_size != SPIDER_NET_FIRMWARE_LEN) &&
1907 netif_msg_probe(card) ) {
1908 pr_err("Incorrect size of spidernet firmware in " \
1909 "host firmware\n");
1910 goto done;
1913 err = spider_net_download_firmware(card, fw_prop);
1915 done:
1916 return err;
1917 out_err:
1918 if (netif_msg_probe(card))
1919 pr_err("Couldn't find spidernet firmware in filesystem " \
1920 "or host firmware\n");
1921 return err;
1925 * spider_net_workaround_rxramfull - work around firmware bug
1926 * @card: card structure
1928 * no return value
1930 static void
1931 spider_net_workaround_rxramfull(struct spider_net_card *card)
1933 int i, sequencer = 0;
1935 /* cancel reset */
1936 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1937 SPIDER_NET_CKRCTRL_RUN_VALUE);
1939 /* empty sequencer data */
1940 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1941 sequencer++) {
1942 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1943 sequencer * 8, 0x0);
1944 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
1945 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1946 sequencer * 8, 0x0);
1950 /* set sequencer operation */
1951 spider_net_write_reg(card, SPIDER_NET_GSINIT, 0x000000fe);
1953 /* reset */
1954 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1955 SPIDER_NET_CKRCTRL_STOP_VALUE);
1959 * spider_net_tx_timeout_task - task scheduled by the watchdog timeout
1960 * function (to be called not under interrupt status)
1961 * @data: data, is interface device structure
1963 * called as task when tx hangs, resets interface (if interface is up)
1965 static void
1966 spider_net_tx_timeout_task(void *data)
1968 struct net_device *netdev = data;
1969 struct spider_net_card *card = netdev_priv(netdev);
1971 if (!(netdev->flags & IFF_UP))
1972 goto out;
1974 netif_device_detach(netdev);
1975 spider_net_stop(netdev);
1977 spider_net_workaround_rxramfull(card);
1978 spider_net_init_card(card);
1980 if (spider_net_setup_phy(card))
1981 goto out;
1982 if (spider_net_init_firmware(card))
1983 goto out;
1985 spider_net_open(netdev);
1986 spider_net_kick_tx_dma(card, card->tx_chain.head);
1987 netif_device_attach(netdev);
1989 out:
1990 atomic_dec(&card->tx_timeout_task_counter);
1994 * spider_net_tx_timeout - called when the tx timeout watchdog kicks in.
1995 * @netdev: interface device structure
1997 * called, if tx hangs. Schedules a task that resets the interface
1999 static void
2000 spider_net_tx_timeout(struct net_device *netdev)
2002 struct spider_net_card *card;
2004 card = netdev_priv(netdev);
2005 atomic_inc(&card->tx_timeout_task_counter);
2006 if (netdev->flags & IFF_UP)
2007 schedule_work(&card->tx_timeout_task);
2008 else
2009 atomic_dec(&card->tx_timeout_task_counter);
2013 * spider_net_setup_netdev_ops - initialization of net_device operations
2014 * @netdev: net_device structure
2016 * fills out function pointers in the net_device structure
2018 static void
2019 spider_net_setup_netdev_ops(struct net_device *netdev)
2021 netdev->open = &spider_net_open;
2022 netdev->stop = &spider_net_stop;
2023 netdev->hard_start_xmit = &spider_net_xmit;
2024 netdev->get_stats = &spider_net_get_stats;
2025 netdev->set_multicast_list = &spider_net_set_multi;
2026 netdev->set_mac_address = &spider_net_set_mac;
2027 netdev->change_mtu = &spider_net_change_mtu;
2028 netdev->do_ioctl = &spider_net_do_ioctl;
2029 /* tx watchdog */
2030 netdev->tx_timeout = &spider_net_tx_timeout;
2031 netdev->watchdog_timeo = SPIDER_NET_WATCHDOG_TIMEOUT;
2032 /* NAPI */
2033 netdev->poll = &spider_net_poll;
2034 netdev->weight = SPIDER_NET_NAPI_WEIGHT;
2035 /* HW VLAN */
2036 netdev->vlan_rx_register = &spider_net_vlan_rx_reg;
2037 netdev->vlan_rx_add_vid = &spider_net_vlan_rx_add;
2038 netdev->vlan_rx_kill_vid = &spider_net_vlan_rx_kill;
2039 #ifdef CONFIG_NET_POLL_CONTROLLER
2040 /* poll controller */
2041 netdev->poll_controller = &spider_net_poll_controller;
2042 #endif /* CONFIG_NET_POLL_CONTROLLER */
2043 /* ethtool ops */
2044 netdev->ethtool_ops = &spider_net_ethtool_ops;
2048 * spider_net_setup_netdev - initialization of net_device
2049 * @card: card structure
2051 * Returns 0 on success or <0 on failure
2053 * spider_net_setup_netdev initializes the net_device structure
2055 static int
2056 spider_net_setup_netdev(struct spider_net_card *card)
2058 int result;
2059 struct net_device *netdev = card->netdev;
2060 struct device_node *dn;
2061 struct sockaddr addr;
2062 u8 *mac;
2064 SET_MODULE_OWNER(netdev);
2065 SET_NETDEV_DEV(netdev, &card->pdev->dev);
2067 pci_set_drvdata(card->pdev, netdev);
2069 atomic_set(&card->tx_chain_release,0);
2070 card->rxram_full_tl.data = (unsigned long) card;
2071 card->rxram_full_tl.func =
2072 (void (*)(unsigned long)) spider_net_handle_rxram_full;
2073 init_timer(&card->tx_timer);
2074 card->tx_timer.function =
2075 (void (*)(unsigned long)) spider_net_cleanup_tx_ring;
2076 card->tx_timer.data = (unsigned long) card;
2077 netdev->irq = card->pdev->irq;
2079 card->options.rx_csum = SPIDER_NET_RX_CSUM_DEFAULT;
2081 spider_net_setup_netdev_ops(netdev);
2083 netdev->features = NETIF_F_HW_CSUM;
2084 /* some time: NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
2085 * NETIF_F_HW_VLAN_FILTER */
2087 netdev->irq = card->pdev->irq;
2089 dn = pci_device_to_OF_node(card->pdev);
2090 if (!dn)
2091 return -EIO;
2093 mac = (u8 *)get_property(dn, "local-mac-address", NULL);
2094 if (!mac)
2095 return -EIO;
2096 memcpy(addr.sa_data, mac, ETH_ALEN);
2098 result = spider_net_set_mac(netdev, &addr);
2099 if ((result) && (netif_msg_probe(card)))
2100 pr_err("Failed to set MAC address: %i\n", result);
2102 result = register_netdev(netdev);
2103 if (result) {
2104 if (netif_msg_probe(card))
2105 pr_err("Couldn't register net_device: %i\n",
2106 result);
2107 return result;
2110 if (netif_msg_probe(card))
2111 pr_info("Initialized device %s.\n", netdev->name);
2113 return 0;
2117 * spider_net_alloc_card - allocates net_device and card structure
2119 * returns the card structure or NULL in case of errors
2121 * the card and net_device structures are linked to each other
2123 static struct spider_net_card *
2124 spider_net_alloc_card(void)
2126 struct net_device *netdev;
2127 struct spider_net_card *card;
2128 size_t alloc_size;
2130 alloc_size = sizeof (*card) +
2131 sizeof (struct spider_net_descr) * rx_descriptors +
2132 sizeof (struct spider_net_descr) * tx_descriptors;
2133 netdev = alloc_etherdev(alloc_size);
2134 if (!netdev)
2135 return NULL;
2137 card = netdev_priv(netdev);
2138 card->netdev = netdev;
2139 card->msg_enable = SPIDER_NET_DEFAULT_MSG;
2140 INIT_WORK(&card->tx_timeout_task, spider_net_tx_timeout_task, netdev);
2141 init_waitqueue_head(&card->waitq);
2142 atomic_set(&card->tx_timeout_task_counter, 0);
2144 return card;
2148 * spider_net_undo_pci_setup - releases PCI ressources
2149 * @card: card structure
2151 * spider_net_undo_pci_setup releases the mapped regions
2153 static void
2154 spider_net_undo_pci_setup(struct spider_net_card *card)
2156 iounmap(card->regs);
2157 pci_release_regions(card->pdev);
2161 * spider_net_setup_pci_dev - sets up the device in terms of PCI operations
2162 * @card: card structure
2163 * @pdev: PCI device
2165 * Returns the card structure or NULL if any errors occur
2167 * spider_net_setup_pci_dev initializes pdev and together with the
2168 * functions called in spider_net_open configures the device so that
2169 * data can be transferred over it
2170 * The net_device structure is attached to the card structure, if the
2171 * function returns without error.
2173 static struct spider_net_card *
2174 spider_net_setup_pci_dev(struct pci_dev *pdev)
2176 struct spider_net_card *card;
2177 unsigned long mmio_start, mmio_len;
2179 if (pci_enable_device(pdev)) {
2180 pr_err("Couldn't enable PCI device\n");
2181 return NULL;
2184 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
2185 pr_err("Couldn't find proper PCI device base address.\n");
2186 goto out_disable_dev;
2189 if (pci_request_regions(pdev, spider_net_driver_name)) {
2190 pr_err("Couldn't obtain PCI resources, aborting.\n");
2191 goto out_disable_dev;
2194 pci_set_master(pdev);
2196 card = spider_net_alloc_card();
2197 if (!card) {
2198 pr_err("Couldn't allocate net_device structure, "
2199 "aborting.\n");
2200 goto out_release_regions;
2202 card->pdev = pdev;
2204 /* fetch base address and length of first resource */
2205 mmio_start = pci_resource_start(pdev, 0);
2206 mmio_len = pci_resource_len(pdev, 0);
2208 card->netdev->mem_start = mmio_start;
2209 card->netdev->mem_end = mmio_start + mmio_len;
2210 card->regs = ioremap(mmio_start, mmio_len);
2212 if (!card->regs) {
2213 pr_err("Couldn't obtain PCI resources, aborting.\n");
2214 goto out_release_regions;
2217 return card;
2219 out_release_regions:
2220 pci_release_regions(pdev);
2221 out_disable_dev:
2222 pci_disable_device(pdev);
2223 pci_set_drvdata(pdev, NULL);
2224 return NULL;
2228 * spider_net_probe - initialization of a device
2229 * @pdev: PCI device
2230 * @ent: entry in the device id list
2232 * Returns 0 on success, <0 on failure
2234 * spider_net_probe initializes pdev and registers a net_device
2235 * structure for it. After that, the device can be ifconfig'ed up
2237 static int __devinit
2238 spider_net_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2240 int err = -EIO;
2241 struct spider_net_card *card;
2243 card = spider_net_setup_pci_dev(pdev);
2244 if (!card)
2245 goto out;
2247 spider_net_workaround_rxramfull(card);
2248 spider_net_init_card(card);
2250 err = spider_net_setup_phy(card);
2251 if (err)
2252 goto out_undo_pci;
2254 err = spider_net_init_firmware(card);
2255 if (err)
2256 goto out_undo_pci;
2258 err = spider_net_setup_netdev(card);
2259 if (err)
2260 goto out_undo_pci;
2262 return 0;
2264 out_undo_pci:
2265 spider_net_undo_pci_setup(card);
2266 free_netdev(card->netdev);
2267 out:
2268 return err;
2272 * spider_net_remove - removal of a device
2273 * @pdev: PCI device
2275 * Returns 0 on success, <0 on failure
2277 * spider_net_remove is called to remove the device and unregisters the
2278 * net_device
2280 static void __devexit
2281 spider_net_remove(struct pci_dev *pdev)
2283 struct net_device *netdev;
2284 struct spider_net_card *card;
2286 netdev = pci_get_drvdata(pdev);
2287 card = netdev_priv(netdev);
2289 wait_event(card->waitq,
2290 atomic_read(&card->tx_timeout_task_counter) == 0);
2292 unregister_netdev(netdev);
2294 /* switch off card */
2295 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2296 SPIDER_NET_CKRCTRL_STOP_VALUE);
2297 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2298 SPIDER_NET_CKRCTRL_RUN_VALUE);
2300 spider_net_undo_pci_setup(card);
2301 free_netdev(netdev);
2304 static struct pci_driver spider_net_driver = {
2305 .name = spider_net_driver_name,
2306 .id_table = spider_net_pci_tbl,
2307 .probe = spider_net_probe,
2308 .remove = __devexit_p(spider_net_remove)
2312 * spider_net_init - init function when the driver is loaded
2314 * spider_net_init registers the device driver
2316 static int __init spider_net_init(void)
2318 if (rx_descriptors < SPIDER_NET_RX_DESCRIPTORS_MIN) {
2319 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MIN;
2320 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2322 if (rx_descriptors > SPIDER_NET_RX_DESCRIPTORS_MAX) {
2323 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MAX;
2324 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2326 if (tx_descriptors < SPIDER_NET_TX_DESCRIPTORS_MIN) {
2327 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MIN;
2328 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2330 if (tx_descriptors > SPIDER_NET_TX_DESCRIPTORS_MAX) {
2331 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MAX;
2332 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2335 return pci_register_driver(&spider_net_driver);
2339 * spider_net_cleanup - exit function when driver is unloaded
2341 * spider_net_cleanup unregisters the device driver
2343 static void __exit spider_net_cleanup(void)
2345 pci_unregister_driver(&spider_net_driver);
2348 module_init(spider_net_init);
2349 module_exit(spider_net_cleanup);