[POWERPC] netdevices: Constify & voidify get_property()
[usb.git] / drivers / net / spider_net.c
blobb30290d53f79f384db4281fe0fa2741740a08f6c
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/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>
31 #include <linux/in.h>
32 #include <linux/init.h>
33 #include <linux/ioport.h>
34 #include <linux/ip.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 " \
66 "in rx chains");
67 MODULE_PARM_DESC(tx_descriptors, "number of descriptors used " \
68 "in tx chain");
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 },
75 { 0, }
78 MODULE_DEVICE_TABLE(pci, spider_net_pci_tbl);
80 /**
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.
87 static u32
88 spider_net_read_reg(struct spider_net_card *card, u32 reg)
90 u32 value;
92 value = readl(card->regs + reg);
93 value = le32_to_cpu(value);
95 return value;
98 /**
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
104 static void
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
113 * @mii_id: id of MII
114 * @reg: PHY register
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).
121 static void
122 spider_net_write_phy(struct net_device *netdev, int mii_id,
123 int reg, int val)
125 struct spider_net_card *card = netdev_priv(netdev);
126 u32 writevalue;
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
136 * @mii_id: id of MII
137 * @reg: PHY register
139 * Returns value read from PHY register
141 * spider_net_write_phy reads from an arbitrary PHY
142 * register via the spider GPCROPCMD register
144 static int
145 spider_net_read_phy(struct net_device *netdev, int mii_id, int reg)
147 struct spider_net_card *card = netdev_priv(netdev);
148 u32 readvalue;
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 */
156 do {
157 readvalue = spider_net_read_reg(card, SPIDER_NET_GPCROPCMD);
158 } while (readvalue & SPIDER_NET_GPREXEC);
160 readvalue &= SPIDER_NET_GPRDAT_MASK;
162 return readvalue;
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
171 static void
172 spider_net_rx_irq_off(struct spider_net_card *card)
174 u32 regvalue;
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
186 static void
187 spider_net_rx_irq_on(struct spider_net_card *card)
189 u32 regvalue;
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
202 static void
203 spider_net_set_promisc(struct spider_net_card *card)
205 u32 macu, macl;
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);
214 } else {
215 macu = netdev->dev_addr[0];
216 macu <<= 8;
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
234 static int
235 spider_net_get_mac_address(struct net_device *netdev)
237 struct spider_net_card *card = netdev_priv(netdev);
238 u32 macl, macu;
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]))
251 return -EINVAL;
253 return 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
262 static enum spider_net_descr_status
263 spider_net_get_descr_status(struct spider_net_descr *descr)
265 u32 cmd_status;
267 cmd_status = descr->dmac_cmd_status;
268 cmd_status >>= SPIDER_NET_DESCR_IND_PROC_SHIFT;
269 /* no need to mask out any bits, as cmd_status is 32 bits wide only
270 * (and unsigned) */
271 return cmd_status;
275 * spider_net_set_descr_status -- sets the status of a descriptor
276 * @descr: descriptor to change
277 * @status: status to set in the descriptor
279 * changes the status to the specified value. Doesn't change other bits
280 * in the status
282 static void
283 spider_net_set_descr_status(struct spider_net_descr *descr,
284 enum spider_net_descr_status status)
286 u32 cmd_status;
287 /* read the status */
288 cmd_status = descr->dmac_cmd_status;
289 /* clean the upper 4 bits */
290 cmd_status &= SPIDER_NET_DESCR_IND_PROC_MASKO;
291 /* add the status to it */
292 cmd_status |= ((u32)status)<<SPIDER_NET_DESCR_IND_PROC_SHIFT;
293 /* and write it back */
294 descr->dmac_cmd_status = cmd_status;
298 * spider_net_free_chain - free descriptor chain
299 * @card: card structure
300 * @chain: address of chain
303 static void
304 spider_net_free_chain(struct spider_net_card *card,
305 struct spider_net_descr_chain *chain)
307 struct spider_net_descr *descr;
309 for (descr = chain->tail; !descr->bus_addr; descr = descr->next) {
310 pci_unmap_single(card->pdev, descr->bus_addr,
311 SPIDER_NET_DESCR_SIZE, PCI_DMA_BIDIRECTIONAL);
312 descr->bus_addr = 0;
317 * spider_net_init_chain - links descriptor chain
318 * @card: card structure
319 * @chain: address of chain
320 * @start_descr: address of descriptor array
321 * @no: number of descriptors
323 * we manage a circular list that mirrors the hardware structure,
324 * except that the hardware uses bus addresses.
326 * returns 0 on success, <0 on failure
328 static int
329 spider_net_init_chain(struct spider_net_card *card,
330 struct spider_net_descr_chain *chain,
331 struct spider_net_descr *start_descr, int no)
333 int i;
334 struct spider_net_descr *descr;
335 dma_addr_t buf;
337 atomic_set(&card->rx_chain_refill,0);
339 descr = start_descr;
340 memset(descr, 0, sizeof(*descr) * no);
342 /* set up the hardware pointers in each descriptor */
343 for (i=0; i<no; i++, descr++) {
344 spider_net_set_descr_status(descr, SPIDER_NET_DESCR_NOT_IN_USE);
346 buf = pci_map_single(card->pdev, descr,
347 SPIDER_NET_DESCR_SIZE,
348 PCI_DMA_BIDIRECTIONAL);
350 if (buf == DMA_ERROR_CODE)
351 goto iommu_error;
353 descr->bus_addr = buf;
354 descr->next = descr + 1;
355 descr->prev = descr - 1;
358 /* do actual circular list */
359 (descr-1)->next = start_descr;
360 start_descr->prev = descr-1;
362 descr = start_descr;
363 for (i=0; i < no; i++, descr++) {
364 descr->next_descr_addr = descr->next->bus_addr;
367 chain->head = start_descr;
368 chain->tail = start_descr;
370 return 0;
372 iommu_error:
373 descr = start_descr;
374 for (i=0; i < no; i++, descr++)
375 if (descr->bus_addr)
376 pci_unmap_single(card->pdev, descr->bus_addr,
377 SPIDER_NET_DESCR_SIZE,
378 PCI_DMA_BIDIRECTIONAL);
379 return -ENOMEM;
383 * spider_net_free_rx_chain_contents - frees descr contents in rx chain
384 * @card: card structure
386 * returns 0 on success, <0 on failure
388 static void
389 spider_net_free_rx_chain_contents(struct spider_net_card *card)
391 struct spider_net_descr *descr;
393 descr = card->rx_chain.head;
394 while (descr->next != card->rx_chain.head) {
395 if (descr->skb) {
396 dev_kfree_skb(descr->skb);
397 pci_unmap_single(card->pdev, descr->buf_addr,
398 SPIDER_NET_MAX_FRAME,
399 PCI_DMA_BIDIRECTIONAL);
401 descr = descr->next;
406 * spider_net_prepare_rx_descr - reinitializes a rx descriptor
407 * @card: card structure
408 * @descr: descriptor to re-init
410 * return 0 on succes, <0 on failure
412 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
413 * Activate the descriptor state-wise
415 static int
416 spider_net_prepare_rx_descr(struct spider_net_card *card,
417 struct spider_net_descr *descr)
419 dma_addr_t buf;
420 int error = 0;
421 int offset;
422 int bufsize;
424 /* we need to round up the buffer size to a multiple of 128 */
425 bufsize = (SPIDER_NET_MAX_FRAME + SPIDER_NET_RXBUF_ALIGN - 1) &
426 (~(SPIDER_NET_RXBUF_ALIGN - 1));
428 /* and we need to have it 128 byte aligned, therefore we allocate a
429 * bit more */
430 /* allocate an skb */
431 descr->skb = dev_alloc_skb(bufsize + SPIDER_NET_RXBUF_ALIGN - 1);
432 if (!descr->skb) {
433 if (netif_msg_rx_err(card) && net_ratelimit())
434 pr_err("Not enough memory to allocate rx buffer\n");
435 return -ENOMEM;
437 descr->buf_size = bufsize;
438 descr->result_size = 0;
439 descr->valid_size = 0;
440 descr->data_status = 0;
441 descr->data_error = 0;
443 offset = ((unsigned long)descr->skb->data) &
444 (SPIDER_NET_RXBUF_ALIGN - 1);
445 if (offset)
446 skb_reserve(descr->skb, SPIDER_NET_RXBUF_ALIGN - offset);
447 /* io-mmu-map the skb */
448 buf = pci_map_single(card->pdev, descr->skb->data,
449 SPIDER_NET_MAX_FRAME, PCI_DMA_BIDIRECTIONAL);
450 descr->buf_addr = buf;
451 if (buf == DMA_ERROR_CODE) {
452 dev_kfree_skb_any(descr->skb);
453 if (netif_msg_rx_err(card) && net_ratelimit())
454 pr_err("Could not iommu-map rx buffer\n");
455 spider_net_set_descr_status(descr, SPIDER_NET_DESCR_NOT_IN_USE);
456 } else {
457 descr->dmac_cmd_status = SPIDER_NET_DMAC_RX_CARDOWNED;
460 return error;
464 * spider_net_enable_rxchtails - sets RX dmac chain tail addresses
465 * @card: card structure
467 * spider_net_enable_rxchtails sets the RX DMAC chain tail adresses in the
468 * chip by writing to the appropriate register. DMA is enabled in
469 * spider_net_enable_rxdmac.
471 static void
472 spider_net_enable_rxchtails(struct spider_net_card *card)
474 /* assume chain is aligned correctly */
475 spider_net_write_reg(card, SPIDER_NET_GDADCHA ,
476 card->rx_chain.tail->bus_addr);
480 * spider_net_enable_rxdmac - enables a receive DMA controller
481 * @card: card structure
483 * spider_net_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
484 * in the GDADMACCNTR register
486 static void
487 spider_net_enable_rxdmac(struct spider_net_card *card)
489 wmb();
490 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
491 SPIDER_NET_DMA_RX_VALUE);
495 * spider_net_refill_rx_chain - refills descriptors/skbs in the rx chains
496 * @card: card structure
498 * refills descriptors in the rx chain: allocates skbs and iommu-maps them.
500 static void
501 spider_net_refill_rx_chain(struct spider_net_card *card)
503 struct spider_net_descr_chain *chain;
505 chain = &card->rx_chain;
507 /* one context doing the refill (and a second context seeing that
508 * and omitting it) is ok. If called by NAPI, we'll be called again
509 * as spider_net_decode_one_descr is called several times. If some
510 * interrupt calls us, the NAPI is about to clean up anyway. */
511 if (atomic_inc_return(&card->rx_chain_refill) == 1)
512 while (spider_net_get_descr_status(chain->head) ==
513 SPIDER_NET_DESCR_NOT_IN_USE) {
514 if (spider_net_prepare_rx_descr(card, chain->head))
515 break;
516 chain->head = chain->head->next;
519 atomic_dec(&card->rx_chain_refill);
523 * spider_net_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
524 * @card: card structure
526 * returns 0 on success, <0 on failure
528 static int
529 spider_net_alloc_rx_skbs(struct spider_net_card *card)
531 int result;
532 struct spider_net_descr_chain *chain;
534 result = -ENOMEM;
536 chain = &card->rx_chain;
537 /* put at least one buffer into the chain. if this fails,
538 * we've got a problem. if not, spider_net_refill_rx_chain
539 * will do the rest at the end of this function */
540 if (spider_net_prepare_rx_descr(card, chain->head))
541 goto error;
542 else
543 chain->head = chain->head->next;
545 /* this will allocate the rest of the rx buffers; if not, it's
546 * business as usual later on */
547 spider_net_refill_rx_chain(card);
548 spider_net_enable_rxdmac(card);
549 return 0;
551 error:
552 spider_net_free_rx_chain_contents(card);
553 return result;
557 * spider_net_release_tx_descr - processes a used tx descriptor
558 * @card: card structure
559 * @descr: descriptor to release
561 * releases a used tx descriptor (unmapping, freeing of skb)
563 static void
564 spider_net_release_tx_descr(struct spider_net_card *card,
565 struct spider_net_descr *descr)
567 struct sk_buff *skb;
569 /* unmap the skb */
570 skb = descr->skb;
571 pci_unmap_single(card->pdev, descr->buf_addr, skb->len,
572 PCI_DMA_BIDIRECTIONAL);
574 dev_kfree_skb_any(skb);
576 /* set status to not used */
577 spider_net_set_descr_status(descr, SPIDER_NET_DESCR_NOT_IN_USE);
581 * spider_net_release_tx_chain - processes sent tx descriptors
582 * @card: adapter structure
583 * @brutal: if set, don't care about whether descriptor seems to be in use
585 * returns 0 if the tx ring is empty, otherwise 1.
587 * spider_net_release_tx_chain releases the tx descriptors that spider has
588 * finished with (if non-brutal) or simply release tx descriptors (if brutal).
589 * If some other context is calling this function, we return 1 so that we're
590 * scheduled again (if we were scheduled) and will not loose initiative.
592 static int
593 spider_net_release_tx_chain(struct spider_net_card *card, int brutal)
595 struct spider_net_descr_chain *tx_chain = &card->tx_chain;
596 enum spider_net_descr_status status;
598 if (atomic_inc_return(&card->tx_chain_release) != 1) {
599 atomic_dec(&card->tx_chain_release);
600 return 1;
603 for (;;) {
604 status = spider_net_get_descr_status(tx_chain->tail);
605 switch (status) {
606 case SPIDER_NET_DESCR_CARDOWNED:
607 if (!brutal)
608 goto out;
609 /* fallthrough, if we release the descriptors
610 * brutally (then we don't care about
611 * SPIDER_NET_DESCR_CARDOWNED) */
612 case SPIDER_NET_DESCR_RESPONSE_ERROR:
613 case SPIDER_NET_DESCR_PROTECTION_ERROR:
614 case SPIDER_NET_DESCR_FORCE_END:
615 if (netif_msg_tx_err(card))
616 pr_err("%s: forcing end of tx descriptor "
617 "with status x%02x\n",
618 card->netdev->name, status);
619 card->netdev_stats.tx_dropped++;
620 break;
622 case SPIDER_NET_DESCR_COMPLETE:
623 card->netdev_stats.tx_packets++;
624 card->netdev_stats.tx_bytes +=
625 tx_chain->tail->skb->len;
626 break;
628 default: /* any other value (== SPIDER_NET_DESCR_NOT_IN_USE) */
629 goto out;
631 spider_net_release_tx_descr(card, tx_chain->tail);
632 tx_chain->tail = tx_chain->tail->next;
634 out:
635 atomic_dec(&card->tx_chain_release);
637 netif_wake_queue(card->netdev);
639 if (status == SPIDER_NET_DESCR_CARDOWNED)
640 return 1;
641 return 0;
645 * spider_net_cleanup_tx_ring - cleans up the TX ring
646 * @card: card structure
648 * spider_net_cleanup_tx_ring is called by the tx_timer (as we don't use
649 * interrupts to cleanup our TX ring) and returns sent packets to the stack
650 * by freeing them
652 static void
653 spider_net_cleanup_tx_ring(struct spider_net_card *card)
655 if ( (spider_net_release_tx_chain(card, 0)) &&
656 (card->netdev->flags & IFF_UP) ) {
657 mod_timer(&card->tx_timer, jiffies + SPIDER_NET_TX_TIMER);
662 * spider_net_get_multicast_hash - generates hash for multicast filter table
663 * @addr: multicast address
665 * returns the hash value.
667 * spider_net_get_multicast_hash calculates a hash value for a given multicast
668 * address, that is used to set the multicast filter tables
670 static u8
671 spider_net_get_multicast_hash(struct net_device *netdev, __u8 *addr)
673 u32 crc;
674 u8 hash;
675 char addr_for_crc[ETH_ALEN] = { 0, };
676 int i, bit;
678 for (i = 0; i < ETH_ALEN * 8; i++) {
679 bit = (addr[i / 8] >> (i % 8)) & 1;
680 addr_for_crc[ETH_ALEN - 1 - i / 8] += bit << (7 - (i % 8));
683 crc = crc32_be(~0, addr_for_crc, netdev->addr_len);
685 hash = (crc >> 27);
686 hash <<= 3;
687 hash |= crc & 7;
688 hash &= 0xff;
690 return hash;
694 * spider_net_set_multi - sets multicast addresses and promisc flags
695 * @netdev: interface device structure
697 * spider_net_set_multi configures multicast addresses as needed for the
698 * netdev interface. It also sets up multicast, allmulti and promisc
699 * flags appropriately
701 static void
702 spider_net_set_multi(struct net_device *netdev)
704 struct dev_mc_list *mc;
705 u8 hash;
706 int i;
707 u32 reg;
708 struct spider_net_card *card = netdev_priv(netdev);
709 unsigned long bitmask[SPIDER_NET_MULTICAST_HASHES / BITS_PER_LONG] =
710 {0, };
712 spider_net_set_promisc(card);
714 if (netdev->flags & IFF_ALLMULTI) {
715 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES; i++) {
716 set_bit(i, bitmask);
718 goto write_hash;
721 /* well, we know, what the broadcast hash value is: it's xfd
722 hash = spider_net_get_multicast_hash(netdev, netdev->broadcast); */
723 set_bit(0xfd, bitmask);
725 for (mc = netdev->mc_list; mc; mc = mc->next) {
726 hash = spider_net_get_multicast_hash(netdev, mc->dmi_addr);
727 set_bit(hash, bitmask);
730 write_hash:
731 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES / 4; i++) {
732 reg = 0;
733 if (test_bit(i * 4, bitmask))
734 reg += 0x08;
735 reg <<= 8;
736 if (test_bit(i * 4 + 1, bitmask))
737 reg += 0x08;
738 reg <<= 8;
739 if (test_bit(i * 4 + 2, bitmask))
740 reg += 0x08;
741 reg <<= 8;
742 if (test_bit(i * 4 + 3, bitmask))
743 reg += 0x08;
745 spider_net_write_reg(card, SPIDER_NET_GMRMHFILnR + i * 4, reg);
750 * spider_net_disable_rxdmac - disables the receive DMA controller
751 * @card: card structure
753 * spider_net_disable_rxdmac terminates processing on the DMA controller by
754 * turing off DMA and issueing a force end
756 static void
757 spider_net_disable_rxdmac(struct spider_net_card *card)
759 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
760 SPIDER_NET_DMA_RX_FEND_VALUE);
764 * spider_net_stop - called upon ifconfig down
765 * @netdev: interface device structure
767 * always returns 0
770 spider_net_stop(struct net_device *netdev)
772 struct spider_net_card *card = netdev_priv(netdev);
774 tasklet_kill(&card->rxram_full_tl);
775 netif_poll_disable(netdev);
776 netif_carrier_off(netdev);
777 netif_stop_queue(netdev);
778 del_timer_sync(&card->tx_timer);
780 /* disable/mask all interrupts */
781 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, 0);
782 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK, 0);
783 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK, 0);
785 /* free_irq(netdev->irq, netdev);*/
786 free_irq(to_pci_dev(netdev->class_dev.dev)->irq, netdev);
788 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
789 SPIDER_NET_DMA_TX_FEND_VALUE);
791 /* turn off DMA, force end */
792 spider_net_disable_rxdmac(card);
794 /* release chains */
795 spider_net_release_tx_chain(card, 1);
797 spider_net_free_chain(card, &card->tx_chain);
798 spider_net_free_chain(card, &card->rx_chain);
800 return 0;
804 * spider_net_get_next_tx_descr - returns the next available tx descriptor
805 * @card: device structure to get descriptor from
807 * returns the address of the next descriptor, or NULL if not available.
809 static struct spider_net_descr *
810 spider_net_get_next_tx_descr(struct spider_net_card *card)
812 /* check, if head points to not-in-use descr */
813 if ( spider_net_get_descr_status(card->tx_chain.head) ==
814 SPIDER_NET_DESCR_NOT_IN_USE ) {
815 return card->tx_chain.head;
816 } else {
817 return NULL;
822 * spider_net_set_txdescr_cmdstat - sets the tx descriptor command field
823 * @descr: descriptor structure to fill out
824 * @skb: packet to consider
826 * fills out the command and status field of the descriptor structure,
827 * depending on hardware checksum settings.
829 static void
830 spider_net_set_txdescr_cmdstat(struct spider_net_descr *descr,
831 struct sk_buff *skb)
833 /* make sure the other fields in the descriptor are written */
834 wmb();
836 if (skb->ip_summed != CHECKSUM_HW) {
837 descr->dmac_cmd_status = SPIDER_NET_DMAC_CMDSTAT_NOCS;
838 return;
841 /* is packet ip?
842 * if yes: tcp? udp? */
843 if (skb->protocol == htons(ETH_P_IP)) {
844 if (skb->nh.iph->protocol == IPPROTO_TCP)
845 descr->dmac_cmd_status = SPIDER_NET_DMAC_CMDSTAT_TCPCS;
846 else if (skb->nh.iph->protocol == IPPROTO_UDP)
847 descr->dmac_cmd_status = SPIDER_NET_DMAC_CMDSTAT_UDPCS;
848 else /* the stack should checksum non-tcp and non-udp
849 packets on his own: NETIF_F_IP_CSUM */
850 descr->dmac_cmd_status = SPIDER_NET_DMAC_CMDSTAT_NOCS;
855 * spider_net_prepare_tx_descr - fill tx descriptor with skb data
856 * @card: card structure
857 * @descr: descriptor structure to fill out
858 * @skb: packet to use
860 * returns 0 on success, <0 on failure.
862 * fills out the descriptor structure with skb data and len. Copies data,
863 * if needed (32bit DMA!)
865 static int
866 spider_net_prepare_tx_descr(struct spider_net_card *card,
867 struct spider_net_descr *descr,
868 struct sk_buff *skb)
870 dma_addr_t buf;
872 buf = pci_map_single(card->pdev, skb->data,
873 skb->len, PCI_DMA_BIDIRECTIONAL);
874 if (buf == DMA_ERROR_CODE) {
875 if (netif_msg_tx_err(card) && net_ratelimit())
876 pr_err("could not iommu-map packet (%p, %i). "
877 "Dropping packet\n", skb->data, skb->len);
878 return -ENOMEM;
881 descr->buf_addr = buf;
882 descr->buf_size = skb->len;
883 descr->skb = skb;
884 descr->data_status = 0;
886 spider_net_set_txdescr_cmdstat(descr,skb);
888 return 0;
892 * spider_net_kick_tx_dma - enables TX DMA processing
893 * @card: card structure
894 * @descr: descriptor address to enable TX processing at
896 * spider_net_kick_tx_dma writes the current tx chain head as start address
897 * of the tx descriptor chain and enables the transmission DMA engine
899 static void
900 spider_net_kick_tx_dma(struct spider_net_card *card,
901 struct spider_net_descr *descr)
903 /* this is the only descriptor in the output chain.
904 * Enable TX DMA */
906 spider_net_write_reg(card, SPIDER_NET_GDTDCHA,
907 descr->bus_addr);
909 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
910 SPIDER_NET_DMA_TX_VALUE);
914 * spider_net_xmit - transmits a frame over the device
915 * @skb: packet to send out
916 * @netdev: interface device structure
918 * returns 0 on success, <0 on failure
920 static int
921 spider_net_xmit(struct sk_buff *skb, struct net_device *netdev)
923 struct spider_net_card *card = netdev_priv(netdev);
924 struct spider_net_descr *descr;
925 int result;
927 spider_net_release_tx_chain(card, 0);
929 descr = spider_net_get_next_tx_descr(card);
931 if (!descr)
932 goto error;
934 result = spider_net_prepare_tx_descr(card, descr, skb);
935 if (result)
936 goto error;
938 card->tx_chain.head = card->tx_chain.head->next;
940 if (spider_net_get_descr_status(descr->prev) !=
941 SPIDER_NET_DESCR_CARDOWNED) {
942 /* make sure the current descriptor is in memory. Then
943 * kicking it on again makes sense, if the previous is not
944 * card-owned anymore. Check the previous descriptor twice
945 * to omit an mb() in heavy traffic cases */
946 mb();
947 if (spider_net_get_descr_status(descr->prev) !=
948 SPIDER_NET_DESCR_CARDOWNED)
949 spider_net_kick_tx_dma(card, descr);
952 mod_timer(&card->tx_timer, jiffies + SPIDER_NET_TX_TIMER);
954 return NETDEV_TX_OK;
956 error:
957 card->netdev_stats.tx_dropped++;
958 return NETDEV_TX_BUSY;
962 * spider_net_do_ioctl - called for device ioctls
963 * @netdev: interface device structure
964 * @ifr: request parameter structure for ioctl
965 * @cmd: command code for ioctl
967 * returns 0 on success, <0 on failure. Currently, we have no special ioctls.
968 * -EOPNOTSUPP is returned, if an unknown ioctl was requested
970 static int
971 spider_net_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
973 switch (cmd) {
974 default:
975 return -EOPNOTSUPP;
980 * spider_net_pass_skb_up - takes an skb from a descriptor and passes it on
981 * @descr: descriptor to process
982 * @card: card structure
983 * @napi: whether caller is in NAPI context
985 * returns 1 on success, 0 if no packet was passed to the stack
987 * iommu-unmaps the skb, fills out skb structure and passes the data to the
988 * stack. The descriptor state is not changed.
990 static int
991 spider_net_pass_skb_up(struct spider_net_descr *descr,
992 struct spider_net_card *card, int napi)
994 struct sk_buff *skb;
995 struct net_device *netdev;
996 u32 data_status, data_error;
998 data_status = descr->data_status;
999 data_error = descr->data_error;
1001 netdev = card->netdev;
1003 /* unmap descriptor */
1004 pci_unmap_single(card->pdev, descr->buf_addr, SPIDER_NET_MAX_FRAME,
1005 PCI_DMA_BIDIRECTIONAL);
1007 /* the cases we'll throw away the packet immediately */
1008 if (data_error & SPIDER_NET_DESTROY_RX_FLAGS) {
1009 if (netif_msg_rx_err(card))
1010 pr_err("error in received descriptor found, "
1011 "data_status=x%08x, data_error=x%08x\n",
1012 data_status, data_error);
1013 return 0;
1016 skb = descr->skb;
1017 skb->dev = netdev;
1018 skb_put(skb, descr->valid_size);
1020 /* the card seems to add 2 bytes of junk in front
1021 * of the ethernet frame */
1022 #define SPIDER_MISALIGN 2
1023 skb_pull(skb, SPIDER_MISALIGN);
1024 skb->protocol = eth_type_trans(skb, netdev);
1026 /* checksum offload */
1027 if (card->options.rx_csum) {
1028 if ( ( (data_status & SPIDER_NET_DATA_STATUS_CKSUM_MASK) ==
1029 SPIDER_NET_DATA_STATUS_CKSUM_MASK) &&
1030 !(data_error & SPIDER_NET_DATA_ERR_CKSUM_MASK))
1031 skb->ip_summed = CHECKSUM_UNNECESSARY;
1032 else
1033 skb->ip_summed = CHECKSUM_NONE;
1034 } else
1035 skb->ip_summed = CHECKSUM_NONE;
1037 if (data_status & SPIDER_NET_VLAN_PACKET) {
1038 /* further enhancements: HW-accel VLAN
1039 * vlan_hwaccel_receive_skb
1043 /* pass skb up to stack */
1044 if (napi)
1045 netif_receive_skb(skb);
1046 else
1047 netif_rx_ni(skb);
1049 /* update netdevice statistics */
1050 card->netdev_stats.rx_packets++;
1051 card->netdev_stats.rx_bytes += skb->len;
1053 return 1;
1057 * spider_net_decode_one_descr - processes an rx descriptor
1058 * @card: card structure
1059 * @napi: whether caller is in NAPI context
1061 * returns 1 if a packet has been sent to the stack, otherwise 0
1063 * processes an rx descriptor by iommu-unmapping the data buffer and passing
1064 * the packet up to the stack. This function is called in softirq
1065 * context, e.g. either bottom half from interrupt or NAPI polling context
1067 static int
1068 spider_net_decode_one_descr(struct spider_net_card *card, int napi)
1070 enum spider_net_descr_status status;
1071 struct spider_net_descr *descr;
1072 struct spider_net_descr_chain *chain;
1073 int result;
1075 chain = &card->rx_chain;
1076 descr = chain->tail;
1078 status = spider_net_get_descr_status(descr);
1080 if (status == SPIDER_NET_DESCR_CARDOWNED) {
1081 /* nothing in the descriptor yet */
1082 result=0;
1083 goto out;
1086 if (status == SPIDER_NET_DESCR_NOT_IN_USE) {
1087 /* not initialized yet, the ring must be empty */
1088 spider_net_refill_rx_chain(card);
1089 spider_net_enable_rxdmac(card);
1090 result=0;
1091 goto out;
1094 /* descriptor definitively used -- move on tail */
1095 chain->tail = descr->next;
1097 result = 0;
1098 if ( (status == SPIDER_NET_DESCR_RESPONSE_ERROR) ||
1099 (status == SPIDER_NET_DESCR_PROTECTION_ERROR) ||
1100 (status == SPIDER_NET_DESCR_FORCE_END) ) {
1101 if (netif_msg_rx_err(card))
1102 pr_err("%s: dropping RX descriptor with state %d\n",
1103 card->netdev->name, status);
1104 card->netdev_stats.rx_dropped++;
1105 pci_unmap_single(card->pdev, descr->buf_addr,
1106 SPIDER_NET_MAX_FRAME, PCI_DMA_BIDIRECTIONAL);
1107 dev_kfree_skb_irq(descr->skb);
1108 goto refill;
1111 if ( (status != SPIDER_NET_DESCR_COMPLETE) &&
1112 (status != SPIDER_NET_DESCR_FRAME_END) ) {
1113 if (netif_msg_rx_err(card))
1114 pr_err("%s: RX descriptor with state %d\n",
1115 card->netdev->name, status);
1116 goto refill;
1119 /* ok, we've got a packet in descr */
1120 result = spider_net_pass_skb_up(descr, card, napi);
1121 refill:
1122 spider_net_set_descr_status(descr, SPIDER_NET_DESCR_NOT_IN_USE);
1123 /* change the descriptor state: */
1124 if (!napi)
1125 spider_net_refill_rx_chain(card);
1126 out:
1127 return result;
1131 * spider_net_poll - NAPI poll function called by the stack to return packets
1132 * @netdev: interface device structure
1133 * @budget: number of packets we can pass to the stack at most
1135 * returns 0 if no more packets available to the driver/stack. Returns 1,
1136 * if the quota is exceeded, but the driver has still packets.
1138 * spider_net_poll returns all packets from the rx descriptors to the stack
1139 * (using netif_receive_skb). If all/enough packets are up, the driver
1140 * reenables interrupts and returns 0. If not, 1 is returned.
1142 static int
1143 spider_net_poll(struct net_device *netdev, int *budget)
1145 struct spider_net_card *card = netdev_priv(netdev);
1146 int packets_to_do, packets_done = 0;
1147 int no_more_packets = 0;
1149 packets_to_do = min(*budget, netdev->quota);
1151 while (packets_to_do) {
1152 if (spider_net_decode_one_descr(card, 1)) {
1153 packets_done++;
1154 packets_to_do--;
1155 } else {
1156 /* no more packets for the stack */
1157 no_more_packets = 1;
1158 break;
1162 netdev->quota -= packets_done;
1163 *budget -= packets_done;
1164 spider_net_refill_rx_chain(card);
1166 /* if all packets are in the stack, enable interrupts and return 0 */
1167 /* if not, return 1 */
1168 if (no_more_packets) {
1169 netif_rx_complete(netdev);
1170 spider_net_rx_irq_on(card);
1171 return 0;
1174 return 1;
1178 * spider_net_vlan_rx_reg - initializes VLAN structures in the driver and card
1179 * @netdev: interface device structure
1180 * @grp: vlan_group structure that is registered (NULL on destroying interface)
1182 static void
1183 spider_net_vlan_rx_reg(struct net_device *netdev, struct vlan_group *grp)
1185 /* further enhancement... yet to do */
1186 return;
1190 * spider_net_vlan_rx_add - adds VLAN id to the card filter
1191 * @netdev: interface device structure
1192 * @vid: VLAN id to add
1194 static void
1195 spider_net_vlan_rx_add(struct net_device *netdev, uint16_t vid)
1197 /* further enhancement... yet to do */
1198 /* add vid to card's VLAN filter table */
1199 return;
1203 * spider_net_vlan_rx_kill - removes VLAN id to the card filter
1204 * @netdev: interface device structure
1205 * @vid: VLAN id to remove
1207 static void
1208 spider_net_vlan_rx_kill(struct net_device *netdev, uint16_t vid)
1210 /* further enhancement... yet to do */
1211 /* remove vid from card's VLAN filter table */
1215 * spider_net_get_stats - get interface statistics
1216 * @netdev: interface device structure
1218 * returns the interface statistics residing in the spider_net_card struct
1220 static struct net_device_stats *
1221 spider_net_get_stats(struct net_device *netdev)
1223 struct spider_net_card *card = netdev_priv(netdev);
1224 struct net_device_stats *stats = &card->netdev_stats;
1225 return stats;
1229 * spider_net_change_mtu - changes the MTU of an interface
1230 * @netdev: interface device structure
1231 * @new_mtu: new MTU value
1233 * returns 0 on success, <0 on failure
1235 static int
1236 spider_net_change_mtu(struct net_device *netdev, int new_mtu)
1238 /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
1239 * and mtu is outbound only anyway */
1240 if ( (new_mtu < SPIDER_NET_MIN_MTU ) ||
1241 (new_mtu > SPIDER_NET_MAX_MTU) )
1242 return -EINVAL;
1243 netdev->mtu = new_mtu;
1244 return 0;
1248 * spider_net_set_mac - sets the MAC of an interface
1249 * @netdev: interface device structure
1250 * @ptr: pointer to new MAC address
1252 * Returns 0 on success, <0 on failure. Currently, we don't support this
1253 * and will always return EOPNOTSUPP.
1255 static int
1256 spider_net_set_mac(struct net_device *netdev, void *p)
1258 struct spider_net_card *card = netdev_priv(netdev);
1259 u32 macl, macu, regvalue;
1260 struct sockaddr *addr = p;
1262 if (!is_valid_ether_addr(addr->sa_data))
1263 return -EADDRNOTAVAIL;
1265 /* switch off GMACTPE and GMACRPE */
1266 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1267 regvalue &= ~((1 << 5) | (1 << 6));
1268 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1270 /* write mac */
1271 macu = (addr->sa_data[0]<<24) + (addr->sa_data[1]<<16) +
1272 (addr->sa_data[2]<<8) + (addr->sa_data[3]);
1273 macl = (addr->sa_data[4]<<8) + (addr->sa_data[5]);
1274 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACU, macu);
1275 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACL, macl);
1277 /* switch GMACTPE and GMACRPE back on */
1278 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1279 regvalue |= ((1 << 5) | (1 << 6));
1280 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1282 spider_net_set_promisc(card);
1284 /* look up, whether we have been successful */
1285 if (spider_net_get_mac_address(netdev))
1286 return -EADDRNOTAVAIL;
1287 if (memcmp(netdev->dev_addr,addr->sa_data,netdev->addr_len))
1288 return -EADDRNOTAVAIL;
1290 return 0;
1294 * spider_net_enable_txdmac - enables a TX DMA controller
1295 * @card: card structure
1297 * spider_net_enable_txdmac enables the TX DMA controller by setting the
1298 * descriptor chain tail address
1300 static void
1301 spider_net_enable_txdmac(struct spider_net_card *card)
1303 /* assume chain is aligned correctly */
1304 spider_net_write_reg(card, SPIDER_NET_GDTDCHA,
1305 card->tx_chain.tail->bus_addr);
1309 * spider_net_handle_rxram_full - cleans up RX ring upon RX RAM full interrupt
1310 * @card: card structure
1312 * spider_net_handle_rxram_full empties the RX ring so that spider can put
1313 * more packets in it and empty its RX RAM. This is called in bottom half
1314 * context
1316 static void
1317 spider_net_handle_rxram_full(struct spider_net_card *card)
1319 while (spider_net_decode_one_descr(card, 0))
1321 spider_net_enable_rxchtails(card);
1322 spider_net_enable_rxdmac(card);
1323 netif_rx_schedule(card->netdev);
1327 * spider_net_handle_error_irq - handles errors raised by an interrupt
1328 * @card: card structure
1329 * @status_reg: interrupt status register 0 (GHIINT0STS)
1331 * spider_net_handle_error_irq treats or ignores all error conditions
1332 * found when an interrupt is presented
1334 static void
1335 spider_net_handle_error_irq(struct spider_net_card *card, u32 status_reg)
1337 u32 error_reg1, error_reg2;
1338 u32 i;
1339 int show_error = 1;
1341 error_reg1 = spider_net_read_reg(card, SPIDER_NET_GHIINT1STS);
1342 error_reg2 = spider_net_read_reg(card, SPIDER_NET_GHIINT2STS);
1344 /* check GHIINT0STS ************************************/
1345 if (status_reg)
1346 for (i = 0; i < 32; i++)
1347 if (status_reg & (1<<i))
1348 switch (i)
1350 /* let error_reg1 and error_reg2 evaluation decide, what to do
1351 case SPIDER_NET_PHYINT:
1352 case SPIDER_NET_GMAC2INT:
1353 case SPIDER_NET_GMAC1INT:
1354 case SPIDER_NET_GIPSINT:
1355 case SPIDER_NET_GFIFOINT:
1356 case SPIDER_NET_DMACINT:
1357 case SPIDER_NET_GSYSINT:
1358 break; */
1360 case SPIDER_NET_GPWOPCMPINT:
1361 /* PHY write operation completed */
1362 show_error = 0;
1363 break;
1364 case SPIDER_NET_GPROPCMPINT:
1365 /* PHY read operation completed */
1366 /* we don't use semaphores, as we poll for the completion
1367 * of the read operation in spider_net_read_phy. Should take
1368 * about 50 us */
1369 show_error = 0;
1370 break;
1371 case SPIDER_NET_GPWFFINT:
1372 /* PHY command queue full */
1373 if (netif_msg_intr(card))
1374 pr_err("PHY write queue full\n");
1375 show_error = 0;
1376 break;
1378 /* case SPIDER_NET_GRMDADRINT: not used. print a message */
1379 /* case SPIDER_NET_GRMARPINT: not used. print a message */
1380 /* case SPIDER_NET_GRMMPINT: not used. print a message */
1382 case SPIDER_NET_GDTDEN0INT:
1383 /* someone has set TX_DMA_EN to 0 */
1384 show_error = 0;
1385 break;
1387 case SPIDER_NET_GDDDEN0INT: /* fallthrough */
1388 case SPIDER_NET_GDCDEN0INT: /* fallthrough */
1389 case SPIDER_NET_GDBDEN0INT: /* fallthrough */
1390 case SPIDER_NET_GDADEN0INT:
1391 /* someone has set RX_DMA_EN to 0 */
1392 show_error = 0;
1393 break;
1395 /* RX interrupts */
1396 case SPIDER_NET_GDDFDCINT:
1397 case SPIDER_NET_GDCFDCINT:
1398 case SPIDER_NET_GDBFDCINT:
1399 case SPIDER_NET_GDAFDCINT:
1400 /* case SPIDER_NET_GDNMINT: not used. print a message */
1401 /* case SPIDER_NET_GCNMINT: not used. print a message */
1402 /* case SPIDER_NET_GBNMINT: not used. print a message */
1403 /* case SPIDER_NET_GANMINT: not used. print a message */
1404 /* case SPIDER_NET_GRFNMINT: not used. print a message */
1405 show_error = 0;
1406 break;
1408 /* TX interrupts */
1409 case SPIDER_NET_GDTFDCINT:
1410 show_error = 0;
1411 break;
1412 case SPIDER_NET_GTTEDINT:
1413 show_error = 0;
1414 break;
1415 case SPIDER_NET_GDTDCEINT:
1416 /* chain end. If a descriptor should be sent, kick off
1417 * tx dma
1418 if (card->tx_chain.tail == card->tx_chain.head)
1419 spider_net_kick_tx_dma(card);
1420 show_error = 0; */
1421 break;
1423 /* case SPIDER_NET_G1TMCNTINT: not used. print a message */
1424 /* case SPIDER_NET_GFREECNTINT: not used. print a message */
1427 /* check GHIINT1STS ************************************/
1428 if (error_reg1)
1429 for (i = 0; i < 32; i++)
1430 if (error_reg1 & (1<<i))
1431 switch (i)
1433 case SPIDER_NET_GTMFLLINT:
1434 if (netif_msg_intr(card) && net_ratelimit())
1435 pr_err("Spider TX RAM full\n");
1436 show_error = 0;
1437 break;
1438 case SPIDER_NET_GRFDFLLINT: /* fallthrough */
1439 case SPIDER_NET_GRFCFLLINT: /* fallthrough */
1440 case SPIDER_NET_GRFBFLLINT: /* fallthrough */
1441 case SPIDER_NET_GRFAFLLINT: /* fallthrough */
1442 case SPIDER_NET_GRMFLLINT:
1443 if (netif_msg_intr(card) && net_ratelimit())
1444 pr_debug("Spider RX RAM full, incoming packets "
1445 "might be discarded!\n");
1446 spider_net_rx_irq_off(card);
1447 tasklet_schedule(&card->rxram_full_tl);
1448 show_error = 0;
1449 break;
1451 /* case SPIDER_NET_GTMSHTINT: problem, print a message */
1452 case SPIDER_NET_GDTINVDINT:
1453 /* allrighty. tx from previous descr ok */
1454 show_error = 0;
1455 break;
1457 /* chain end */
1458 case SPIDER_NET_GDDDCEINT: /* fallthrough */
1459 case SPIDER_NET_GDCDCEINT: /* fallthrough */
1460 case SPIDER_NET_GDBDCEINT: /* fallthrough */
1461 case SPIDER_NET_GDADCEINT:
1462 if (netif_msg_intr(card))
1463 pr_err("got descriptor chain end interrupt, "
1464 "restarting DMAC %c.\n",
1465 'D'+i-SPIDER_NET_GDDDCEINT);
1466 spider_net_refill_rx_chain(card);
1467 spider_net_enable_rxdmac(card);
1468 show_error = 0;
1469 break;
1471 /* invalid descriptor */
1472 case SPIDER_NET_GDDINVDINT: /* fallthrough */
1473 case SPIDER_NET_GDCINVDINT: /* fallthrough */
1474 case SPIDER_NET_GDBINVDINT: /* fallthrough */
1475 case SPIDER_NET_GDAINVDINT:
1476 /* could happen when rx chain is full */
1477 spider_net_refill_rx_chain(card);
1478 spider_net_enable_rxdmac(card);
1479 show_error = 0;
1480 break;
1482 /* case SPIDER_NET_GDTRSERINT: problem, print a message */
1483 /* case SPIDER_NET_GDDRSERINT: problem, print a message */
1484 /* case SPIDER_NET_GDCRSERINT: problem, print a message */
1485 /* case SPIDER_NET_GDBRSERINT: problem, print a message */
1486 /* case SPIDER_NET_GDARSERINT: problem, print a message */
1487 /* case SPIDER_NET_GDSERINT: problem, print a message */
1488 /* case SPIDER_NET_GDTPTERINT: problem, print a message */
1489 /* case SPIDER_NET_GDDPTERINT: problem, print a message */
1490 /* case SPIDER_NET_GDCPTERINT: problem, print a message */
1491 /* case SPIDER_NET_GDBPTERINT: problem, print a message */
1492 /* case SPIDER_NET_GDAPTERINT: problem, print a message */
1493 default:
1494 show_error = 1;
1495 break;
1498 /* check GHIINT2STS ************************************/
1499 if (error_reg2)
1500 for (i = 0; i < 32; i++)
1501 if (error_reg2 & (1<<i))
1502 switch (i)
1504 /* there is nothing we can (want to) do at this time. Log a
1505 * message, we can switch on and off the specific values later on
1506 case SPIDER_NET_GPROPERINT:
1507 case SPIDER_NET_GMCTCRSNGINT:
1508 case SPIDER_NET_GMCTLCOLINT:
1509 case SPIDER_NET_GMCTTMOTINT:
1510 case SPIDER_NET_GMCRCAERINT:
1511 case SPIDER_NET_GMCRCALERINT:
1512 case SPIDER_NET_GMCRALNERINT:
1513 case SPIDER_NET_GMCROVRINT:
1514 case SPIDER_NET_GMCRRNTINT:
1515 case SPIDER_NET_GMCRRXERINT:
1516 case SPIDER_NET_GTITCSERINT:
1517 case SPIDER_NET_GTIFMTERINT:
1518 case SPIDER_NET_GTIPKTRVKINT:
1519 case SPIDER_NET_GTISPINGINT:
1520 case SPIDER_NET_GTISADNGINT:
1521 case SPIDER_NET_GTISPDNGINT:
1522 case SPIDER_NET_GRIFMTERINT:
1523 case SPIDER_NET_GRIPKTRVKINT:
1524 case SPIDER_NET_GRISPINGINT:
1525 case SPIDER_NET_GRISADNGINT:
1526 case SPIDER_NET_GRISPDNGINT:
1527 break;
1529 default:
1530 break;
1533 if ((show_error) && (netif_msg_intr(card)))
1534 pr_err("Got error interrupt, GHIINT0STS = 0x%08x, "
1535 "GHIINT1STS = 0x%08x, GHIINT2STS = 0x%08x\n",
1536 status_reg, error_reg1, error_reg2);
1538 /* clear interrupt sources */
1539 spider_net_write_reg(card, SPIDER_NET_GHIINT1STS, error_reg1);
1540 spider_net_write_reg(card, SPIDER_NET_GHIINT2STS, error_reg2);
1544 * spider_net_interrupt - interrupt handler for spider_net
1545 * @irq: interupt number
1546 * @ptr: pointer to net_device
1547 * @regs: PU registers
1549 * returns IRQ_HANDLED, if interrupt was for driver, or IRQ_NONE, if no
1550 * interrupt found raised by card.
1552 * This is the interrupt handler, that turns off
1553 * interrupts for this device and makes the stack poll the driver
1555 static irqreturn_t
1556 spider_net_interrupt(int irq, void *ptr, struct pt_regs *regs)
1558 struct net_device *netdev = ptr;
1559 struct spider_net_card *card = netdev_priv(netdev);
1560 u32 status_reg;
1562 status_reg = spider_net_read_reg(card, SPIDER_NET_GHIINT0STS);
1564 if (!status_reg)
1565 return IRQ_NONE;
1567 if (status_reg & SPIDER_NET_RXINT ) {
1568 spider_net_rx_irq_off(card);
1569 netif_rx_schedule(netdev);
1572 if (status_reg & SPIDER_NET_ERRINT )
1573 spider_net_handle_error_irq(card, status_reg);
1575 /* clear interrupt sources */
1576 spider_net_write_reg(card, SPIDER_NET_GHIINT0STS, status_reg);
1578 return IRQ_HANDLED;
1581 #ifdef CONFIG_NET_POLL_CONTROLLER
1583 * spider_net_poll_controller - artificial interrupt for netconsole etc.
1584 * @netdev: interface device structure
1586 * see Documentation/networking/netconsole.txt
1588 static void
1589 spider_net_poll_controller(struct net_device *netdev)
1591 disable_irq(netdev->irq);
1592 spider_net_interrupt(netdev->irq, netdev, NULL);
1593 enable_irq(netdev->irq);
1595 #endif /* CONFIG_NET_POLL_CONTROLLER */
1598 * spider_net_init_card - initializes the card
1599 * @card: card structure
1601 * spider_net_init_card initializes the card so that other registers can
1602 * be used
1604 static void
1605 spider_net_init_card(struct spider_net_card *card)
1607 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1608 SPIDER_NET_CKRCTRL_STOP_VALUE);
1610 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1611 SPIDER_NET_CKRCTRL_RUN_VALUE);
1615 * spider_net_enable_card - enables the card by setting all kinds of regs
1616 * @card: card structure
1618 * spider_net_enable_card sets a lot of SMMIO registers to enable the device
1620 static void
1621 spider_net_enable_card(struct spider_net_card *card)
1623 int i;
1624 /* the following array consists of (register),(value) pairs
1625 * that are set in this function. A register of 0 ends the list */
1626 u32 regs[][2] = {
1627 { SPIDER_NET_GRESUMINTNUM, 0 },
1628 { SPIDER_NET_GREINTNUM, 0 },
1630 /* set interrupt frame number registers */
1631 /* clear the single DMA engine registers first */
1632 { SPIDER_NET_GFAFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1633 { SPIDER_NET_GFBFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1634 { SPIDER_NET_GFCFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1635 { SPIDER_NET_GFDFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1636 /* then set, what we really need */
1637 { SPIDER_NET_GFFRMNUM, SPIDER_NET_FRAMENUM_VALUE },
1639 /* timer counter registers and stuff */
1640 { SPIDER_NET_GFREECNNUM, 0 },
1641 { SPIDER_NET_GONETIMENUM, 0 },
1642 { SPIDER_NET_GTOUTFRMNUM, 0 },
1644 /* RX mode setting */
1645 { SPIDER_NET_GRXMDSET, SPIDER_NET_RXMODE_VALUE },
1646 /* TX mode setting */
1647 { SPIDER_NET_GTXMDSET, SPIDER_NET_TXMODE_VALUE },
1648 /* IPSEC mode setting */
1649 { SPIDER_NET_GIPSECINIT, SPIDER_NET_IPSECINIT_VALUE },
1651 { SPIDER_NET_GFTRESTRT, SPIDER_NET_RESTART_VALUE },
1653 { SPIDER_NET_GMRWOLCTRL, 0 },
1654 { SPIDER_NET_GTESTMD, 0x10000000 },
1655 { SPIDER_NET_GTTQMSK, 0x00400040 },
1656 { SPIDER_NET_GTESTMD, 0 },
1658 { SPIDER_NET_GMACINTEN, 0 },
1660 /* flow control stuff */
1661 { SPIDER_NET_GMACAPAUSE, SPIDER_NET_MACAPAUSE_VALUE },
1662 { SPIDER_NET_GMACTXPAUSE, SPIDER_NET_TXPAUSE_VALUE },
1664 { SPIDER_NET_GMACBSTLMT, SPIDER_NET_BURSTLMT_VALUE },
1665 { 0, 0}
1668 i = 0;
1669 while (regs[i][0]) {
1670 spider_net_write_reg(card, regs[i][0], regs[i][1]);
1671 i++;
1674 /* clear unicast filter table entries 1 to 14 */
1675 for (i = 1; i <= 14; i++) {
1676 spider_net_write_reg(card,
1677 SPIDER_NET_GMRUAFILnR + i * 8,
1678 0x00080000);
1679 spider_net_write_reg(card,
1680 SPIDER_NET_GMRUAFILnR + i * 8 + 4,
1681 0x00000000);
1684 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R, 0x08080000);
1686 spider_net_write_reg(card, SPIDER_NET_ECMODE, SPIDER_NET_ECMODE_VALUE);
1688 /* set chain tail adress for RX chains and
1689 * enable DMA */
1690 spider_net_enable_rxchtails(card);
1691 spider_net_enable_rxdmac(card);
1693 spider_net_write_reg(card, SPIDER_NET_GRXDMAEN, SPIDER_NET_WOL_VALUE);
1695 /* set chain tail adress for TX chain */
1696 spider_net_enable_txdmac(card);
1698 spider_net_write_reg(card, SPIDER_NET_GMACLENLMT,
1699 SPIDER_NET_LENLMT_VALUE);
1700 spider_net_write_reg(card, SPIDER_NET_GMACMODE,
1701 SPIDER_NET_MACMODE_VALUE);
1702 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD,
1703 SPIDER_NET_OPMODE_VALUE);
1705 /* set interrupt mask registers */
1706 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK,
1707 SPIDER_NET_INT0_MASK_VALUE);
1708 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK,
1709 SPIDER_NET_INT1_MASK_VALUE);
1710 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK,
1711 SPIDER_NET_INT2_MASK_VALUE);
1715 * spider_net_open - called upon ifonfig up
1716 * @netdev: interface device structure
1718 * returns 0 on success, <0 on failure
1720 * spider_net_open allocates all the descriptors and memory needed for
1721 * operation, sets up multicast list and enables interrupts
1724 spider_net_open(struct net_device *netdev)
1726 struct spider_net_card *card = netdev_priv(netdev);
1727 int result;
1729 result = -ENOMEM;
1730 if (spider_net_init_chain(card, &card->tx_chain,
1731 card->descr, tx_descriptors))
1732 goto alloc_tx_failed;
1733 if (spider_net_init_chain(card, &card->rx_chain,
1734 card->descr + tx_descriptors, rx_descriptors))
1735 goto alloc_rx_failed;
1737 /* allocate rx skbs */
1738 if (spider_net_alloc_rx_skbs(card))
1739 goto alloc_skbs_failed;
1741 spider_net_set_multi(netdev);
1743 /* further enhancement: setup hw vlan, if needed */
1745 result = -EBUSY;
1746 if (request_irq(netdev->irq, spider_net_interrupt,
1747 IRQF_SHARED, netdev->name, netdev))
1748 goto register_int_failed;
1750 spider_net_enable_card(card);
1752 netif_start_queue(netdev);
1753 netif_carrier_on(netdev);
1754 netif_poll_enable(netdev);
1756 return 0;
1758 register_int_failed:
1759 spider_net_free_rx_chain_contents(card);
1760 alloc_skbs_failed:
1761 spider_net_free_chain(card, &card->rx_chain);
1762 alloc_rx_failed:
1763 spider_net_free_chain(card, &card->tx_chain);
1764 alloc_tx_failed:
1765 return result;
1769 * spider_net_setup_phy - setup PHY
1770 * @card: card structure
1772 * returns 0 on success, <0 on failure
1774 * spider_net_setup_phy is used as part of spider_net_probe. Sets
1775 * the PHY to 1000 Mbps
1777 static int
1778 spider_net_setup_phy(struct spider_net_card *card)
1780 struct mii_phy *phy = &card->phy;
1782 spider_net_write_reg(card, SPIDER_NET_GDTDMASEL,
1783 SPIDER_NET_DMASEL_VALUE);
1784 spider_net_write_reg(card, SPIDER_NET_GPCCTRL,
1785 SPIDER_NET_PHY_CTRL_VALUE);
1786 phy->mii_id = 1;
1787 phy->dev = card->netdev;
1788 phy->mdio_read = spider_net_read_phy;
1789 phy->mdio_write = spider_net_write_phy;
1791 mii_phy_probe(phy, phy->mii_id);
1793 if (phy->def->ops->setup_forced)
1794 phy->def->ops->setup_forced(phy, SPEED_1000, DUPLEX_FULL);
1796 phy->def->ops->enable_fiber(phy);
1798 phy->def->ops->read_link(phy);
1799 pr_info("Found %s with %i Mbps, %s-duplex.\n", phy->def->name,
1800 phy->speed, phy->duplex==1 ? "Full" : "Half");
1802 return 0;
1806 * spider_net_download_firmware - loads firmware into the adapter
1807 * @card: card structure
1808 * @firmware_ptr: pointer to firmware data
1810 * spider_net_download_firmware loads the firmware data into the
1811 * adapter. It assumes the length etc. to be allright.
1813 static int
1814 spider_net_download_firmware(struct spider_net_card *card,
1815 const void *firmware_ptr)
1817 int sequencer, i;
1818 const u32 *fw_ptr = firmware_ptr;
1820 /* stop sequencers */
1821 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1822 SPIDER_NET_STOP_SEQ_VALUE);
1824 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1825 sequencer++) {
1826 spider_net_write_reg(card,
1827 SPIDER_NET_GSnPRGADR + sequencer * 8, 0);
1828 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
1829 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1830 sequencer * 8, *fw_ptr);
1831 fw_ptr++;
1835 if (spider_net_read_reg(card, SPIDER_NET_GSINIT))
1836 return -EIO;
1838 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1839 SPIDER_NET_RUN_SEQ_VALUE);
1841 return 0;
1845 * spider_net_init_firmware - reads in firmware parts
1846 * @card: card structure
1848 * Returns 0 on success, <0 on failure
1850 * spider_net_init_firmware opens the sequencer firmware and does some basic
1851 * checks. This function opens and releases the firmware structure. A call
1852 * to download the firmware is performed before the release.
1854 * Firmware format
1855 * ===============
1856 * spider_fw.bin is expected to be a file containing 6*1024*4 bytes, 4k being
1857 * the program for each sequencer. Use the command
1858 * tail -q -n +2 Seq_code1_0x088.txt Seq_code2_0x090.txt \
1859 * Seq_code3_0x098.txt Seq_code4_0x0A0.txt Seq_code5_0x0A8.txt \
1860 * Seq_code6_0x0B0.txt | xxd -r -p -c4 > spider_fw.bin
1862 * to generate spider_fw.bin, if you have sequencer programs with something
1863 * like the following contents for each sequencer:
1864 * <ONE LINE COMMENT>
1865 * <FIRST 4-BYTES-WORD FOR SEQUENCER>
1866 * <SECOND 4-BYTES-WORD FOR SEQUENCER>
1867 * ...
1868 * <1024th 4-BYTES-WORD FOR SEQUENCER>
1870 static int
1871 spider_net_init_firmware(struct spider_net_card *card)
1873 struct firmware *firmware = NULL;
1874 struct device_node *dn;
1875 const u8 *fw_prop = NULL;
1876 int err = -ENOENT;
1877 int fw_size;
1879 if (request_firmware((const struct firmware **)&firmware,
1880 SPIDER_NET_FIRMWARE_NAME, &card->pdev->dev) == 0) {
1881 if ( (firmware->size != SPIDER_NET_FIRMWARE_LEN) &&
1882 netif_msg_probe(card) ) {
1883 pr_err("Incorrect size of spidernet firmware in " \
1884 "filesystem. Looking in host firmware...\n");
1885 goto try_host_fw;
1887 err = spider_net_download_firmware(card, firmware->data);
1889 release_firmware(firmware);
1890 if (err)
1891 goto try_host_fw;
1893 goto done;
1896 try_host_fw:
1897 dn = pci_device_to_OF_node(card->pdev);
1898 if (!dn)
1899 goto out_err;
1901 fw_prop = get_property(dn, "firmware", &fw_size);
1902 if (!fw_prop)
1903 goto out_err;
1905 if ( (fw_size != SPIDER_NET_FIRMWARE_LEN) &&
1906 netif_msg_probe(card) ) {
1907 pr_err("Incorrect size of spidernet firmware in " \
1908 "host firmware\n");
1909 goto done;
1912 err = spider_net_download_firmware(card, fw_prop);
1914 done:
1915 return err;
1916 out_err:
1917 if (netif_msg_probe(card))
1918 pr_err("Couldn't find spidernet firmware in filesystem " \
1919 "or host firmware\n");
1920 return err;
1924 * spider_net_workaround_rxramfull - work around firmware bug
1925 * @card: card structure
1927 * no return value
1929 static void
1930 spider_net_workaround_rxramfull(struct spider_net_card *card)
1932 int i, sequencer = 0;
1934 /* cancel reset */
1935 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1936 SPIDER_NET_CKRCTRL_RUN_VALUE);
1938 /* empty sequencer data */
1939 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1940 sequencer++) {
1941 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1942 sequencer * 8, 0x0);
1943 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
1944 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1945 sequencer * 8, 0x0);
1949 /* set sequencer operation */
1950 spider_net_write_reg(card, SPIDER_NET_GSINIT, 0x000000fe);
1952 /* reset */
1953 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1954 SPIDER_NET_CKRCTRL_STOP_VALUE);
1958 * spider_net_tx_timeout_task - task scheduled by the watchdog timeout
1959 * function (to be called not under interrupt status)
1960 * @data: data, is interface device structure
1962 * called as task when tx hangs, resets interface (if interface is up)
1964 static void
1965 spider_net_tx_timeout_task(void *data)
1967 struct net_device *netdev = data;
1968 struct spider_net_card *card = netdev_priv(netdev);
1970 if (!(netdev->flags & IFF_UP))
1971 goto out;
1973 netif_device_detach(netdev);
1974 spider_net_stop(netdev);
1976 spider_net_workaround_rxramfull(card);
1977 spider_net_init_card(card);
1979 if (spider_net_setup_phy(card))
1980 goto out;
1981 if (spider_net_init_firmware(card))
1982 goto out;
1984 spider_net_open(netdev);
1985 spider_net_kick_tx_dma(card, card->tx_chain.head);
1986 netif_device_attach(netdev);
1988 out:
1989 atomic_dec(&card->tx_timeout_task_counter);
1993 * spider_net_tx_timeout - called when the tx timeout watchdog kicks in.
1994 * @netdev: interface device structure
1996 * called, if tx hangs. Schedules a task that resets the interface
1998 static void
1999 spider_net_tx_timeout(struct net_device *netdev)
2001 struct spider_net_card *card;
2003 card = netdev_priv(netdev);
2004 atomic_inc(&card->tx_timeout_task_counter);
2005 if (netdev->flags & IFF_UP)
2006 schedule_work(&card->tx_timeout_task);
2007 else
2008 atomic_dec(&card->tx_timeout_task_counter);
2012 * spider_net_setup_netdev_ops - initialization of net_device operations
2013 * @netdev: net_device structure
2015 * fills out function pointers in the net_device structure
2017 static void
2018 spider_net_setup_netdev_ops(struct net_device *netdev)
2020 netdev->open = &spider_net_open;
2021 netdev->stop = &spider_net_stop;
2022 netdev->hard_start_xmit = &spider_net_xmit;
2023 netdev->get_stats = &spider_net_get_stats;
2024 netdev->set_multicast_list = &spider_net_set_multi;
2025 netdev->set_mac_address = &spider_net_set_mac;
2026 netdev->change_mtu = &spider_net_change_mtu;
2027 netdev->do_ioctl = &spider_net_do_ioctl;
2028 /* tx watchdog */
2029 netdev->tx_timeout = &spider_net_tx_timeout;
2030 netdev->watchdog_timeo = SPIDER_NET_WATCHDOG_TIMEOUT;
2031 /* NAPI */
2032 netdev->poll = &spider_net_poll;
2033 netdev->weight = SPIDER_NET_NAPI_WEIGHT;
2034 /* HW VLAN */
2035 netdev->vlan_rx_register = &spider_net_vlan_rx_reg;
2036 netdev->vlan_rx_add_vid = &spider_net_vlan_rx_add;
2037 netdev->vlan_rx_kill_vid = &spider_net_vlan_rx_kill;
2038 #ifdef CONFIG_NET_POLL_CONTROLLER
2039 /* poll controller */
2040 netdev->poll_controller = &spider_net_poll_controller;
2041 #endif /* CONFIG_NET_POLL_CONTROLLER */
2042 /* ethtool ops */
2043 netdev->ethtool_ops = &spider_net_ethtool_ops;
2047 * spider_net_setup_netdev - initialization of net_device
2048 * @card: card structure
2050 * Returns 0 on success or <0 on failure
2052 * spider_net_setup_netdev initializes the net_device structure
2054 static int
2055 spider_net_setup_netdev(struct spider_net_card *card)
2057 int result;
2058 struct net_device *netdev = card->netdev;
2059 struct device_node *dn;
2060 struct sockaddr addr;
2061 const u8 *mac;
2063 SET_MODULE_OWNER(netdev);
2064 SET_NETDEV_DEV(netdev, &card->pdev->dev);
2066 pci_set_drvdata(card->pdev, netdev);
2068 atomic_set(&card->tx_chain_release,0);
2069 card->rxram_full_tl.data = (unsigned long) card;
2070 card->rxram_full_tl.func =
2071 (void (*)(unsigned long)) spider_net_handle_rxram_full;
2072 init_timer(&card->tx_timer);
2073 card->tx_timer.function =
2074 (void (*)(unsigned long)) spider_net_cleanup_tx_ring;
2075 card->tx_timer.data = (unsigned long) card;
2076 netdev->irq = card->pdev->irq;
2078 card->options.rx_csum = SPIDER_NET_RX_CSUM_DEFAULT;
2080 spider_net_setup_netdev_ops(netdev);
2082 netdev->features = NETIF_F_HW_CSUM;
2083 /* some time: NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
2084 * NETIF_F_HW_VLAN_FILTER */
2086 netdev->irq = card->pdev->irq;
2088 dn = pci_device_to_OF_node(card->pdev);
2089 if (!dn)
2090 return -EIO;
2092 mac = get_property(dn, "local-mac-address", NULL);
2093 if (!mac)
2094 return -EIO;
2095 memcpy(addr.sa_data, mac, ETH_ALEN);
2097 result = spider_net_set_mac(netdev, &addr);
2098 if ((result) && (netif_msg_probe(card)))
2099 pr_err("Failed to set MAC address: %i\n", result);
2101 result = register_netdev(netdev);
2102 if (result) {
2103 if (netif_msg_probe(card))
2104 pr_err("Couldn't register net_device: %i\n",
2105 result);
2106 return result;
2109 if (netif_msg_probe(card))
2110 pr_info("Initialized device %s.\n", netdev->name);
2112 return 0;
2116 * spider_net_alloc_card - allocates net_device and card structure
2118 * returns the card structure or NULL in case of errors
2120 * the card and net_device structures are linked to each other
2122 static struct spider_net_card *
2123 spider_net_alloc_card(void)
2125 struct net_device *netdev;
2126 struct spider_net_card *card;
2127 size_t alloc_size;
2129 alloc_size = sizeof (*card) +
2130 sizeof (struct spider_net_descr) * rx_descriptors +
2131 sizeof (struct spider_net_descr) * tx_descriptors;
2132 netdev = alloc_etherdev(alloc_size);
2133 if (!netdev)
2134 return NULL;
2136 card = netdev_priv(netdev);
2137 card->netdev = netdev;
2138 card->msg_enable = SPIDER_NET_DEFAULT_MSG;
2139 INIT_WORK(&card->tx_timeout_task, spider_net_tx_timeout_task, netdev);
2140 init_waitqueue_head(&card->waitq);
2141 atomic_set(&card->tx_timeout_task_counter, 0);
2143 return card;
2147 * spider_net_undo_pci_setup - releases PCI ressources
2148 * @card: card structure
2150 * spider_net_undo_pci_setup releases the mapped regions
2152 static void
2153 spider_net_undo_pci_setup(struct spider_net_card *card)
2155 iounmap(card->regs);
2156 pci_release_regions(card->pdev);
2160 * spider_net_setup_pci_dev - sets up the device in terms of PCI operations
2161 * @card: card structure
2162 * @pdev: PCI device
2164 * Returns the card structure or NULL if any errors occur
2166 * spider_net_setup_pci_dev initializes pdev and together with the
2167 * functions called in spider_net_open configures the device so that
2168 * data can be transferred over it
2169 * The net_device structure is attached to the card structure, if the
2170 * function returns without error.
2172 static struct spider_net_card *
2173 spider_net_setup_pci_dev(struct pci_dev *pdev)
2175 struct spider_net_card *card;
2176 unsigned long mmio_start, mmio_len;
2178 if (pci_enable_device(pdev)) {
2179 pr_err("Couldn't enable PCI device\n");
2180 return NULL;
2183 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
2184 pr_err("Couldn't find proper PCI device base address.\n");
2185 goto out_disable_dev;
2188 if (pci_request_regions(pdev, spider_net_driver_name)) {
2189 pr_err("Couldn't obtain PCI resources, aborting.\n");
2190 goto out_disable_dev;
2193 pci_set_master(pdev);
2195 card = spider_net_alloc_card();
2196 if (!card) {
2197 pr_err("Couldn't allocate net_device structure, "
2198 "aborting.\n");
2199 goto out_release_regions;
2201 card->pdev = pdev;
2203 /* fetch base address and length of first resource */
2204 mmio_start = pci_resource_start(pdev, 0);
2205 mmio_len = pci_resource_len(pdev, 0);
2207 card->netdev->mem_start = mmio_start;
2208 card->netdev->mem_end = mmio_start + mmio_len;
2209 card->regs = ioremap(mmio_start, mmio_len);
2211 if (!card->regs) {
2212 pr_err("Couldn't obtain PCI resources, aborting.\n");
2213 goto out_release_regions;
2216 return card;
2218 out_release_regions:
2219 pci_release_regions(pdev);
2220 out_disable_dev:
2221 pci_disable_device(pdev);
2222 pci_set_drvdata(pdev, NULL);
2223 return NULL;
2227 * spider_net_probe - initialization of a device
2228 * @pdev: PCI device
2229 * @ent: entry in the device id list
2231 * Returns 0 on success, <0 on failure
2233 * spider_net_probe initializes pdev and registers a net_device
2234 * structure for it. After that, the device can be ifconfig'ed up
2236 static int __devinit
2237 spider_net_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2239 int err = -EIO;
2240 struct spider_net_card *card;
2242 card = spider_net_setup_pci_dev(pdev);
2243 if (!card)
2244 goto out;
2246 spider_net_workaround_rxramfull(card);
2247 spider_net_init_card(card);
2249 err = spider_net_setup_phy(card);
2250 if (err)
2251 goto out_undo_pci;
2253 err = spider_net_init_firmware(card);
2254 if (err)
2255 goto out_undo_pci;
2257 err = spider_net_setup_netdev(card);
2258 if (err)
2259 goto out_undo_pci;
2261 return 0;
2263 out_undo_pci:
2264 spider_net_undo_pci_setup(card);
2265 free_netdev(card->netdev);
2266 out:
2267 return err;
2271 * spider_net_remove - removal of a device
2272 * @pdev: PCI device
2274 * Returns 0 on success, <0 on failure
2276 * spider_net_remove is called to remove the device and unregisters the
2277 * net_device
2279 static void __devexit
2280 spider_net_remove(struct pci_dev *pdev)
2282 struct net_device *netdev;
2283 struct spider_net_card *card;
2285 netdev = pci_get_drvdata(pdev);
2286 card = netdev_priv(netdev);
2288 wait_event(card->waitq,
2289 atomic_read(&card->tx_timeout_task_counter) == 0);
2291 unregister_netdev(netdev);
2293 /* switch off card */
2294 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2295 SPIDER_NET_CKRCTRL_STOP_VALUE);
2296 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2297 SPIDER_NET_CKRCTRL_RUN_VALUE);
2299 spider_net_undo_pci_setup(card);
2300 free_netdev(netdev);
2303 static struct pci_driver spider_net_driver = {
2304 .name = spider_net_driver_name,
2305 .id_table = spider_net_pci_tbl,
2306 .probe = spider_net_probe,
2307 .remove = __devexit_p(spider_net_remove)
2311 * spider_net_init - init function when the driver is loaded
2313 * spider_net_init registers the device driver
2315 static int __init spider_net_init(void)
2317 if (rx_descriptors < SPIDER_NET_RX_DESCRIPTORS_MIN) {
2318 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MIN;
2319 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2321 if (rx_descriptors > SPIDER_NET_RX_DESCRIPTORS_MAX) {
2322 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MAX;
2323 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2325 if (tx_descriptors < SPIDER_NET_TX_DESCRIPTORS_MIN) {
2326 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MIN;
2327 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2329 if (tx_descriptors > SPIDER_NET_TX_DESCRIPTORS_MAX) {
2330 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MAX;
2331 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2334 return pci_register_driver(&spider_net_driver);
2338 * spider_net_cleanup - exit function when driver is unloaded
2340 * spider_net_cleanup unregisters the device driver
2342 static void __exit spider_net_cleanup(void)
2344 pci_unregister_driver(&spider_net_driver);
2347 module_init(spider_net_init);
2348 module_exit(spider_net_cleanup);