i2c-viapro: Add support for the VT8237A and VT8251
[linux/fpc-iii.git] / drivers / net / spider_net.c
blob1f5975a61e1f1526d8a43869c760ccd96e47753b
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_err("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, 0 },
1657 { SPIDER_NET_GMACINTEN, 0 },
1659 /* flow control stuff */
1660 { SPIDER_NET_GMACAPAUSE, SPIDER_NET_MACAPAUSE_VALUE },
1661 { SPIDER_NET_GMACTXPAUSE, SPIDER_NET_TXPAUSE_VALUE },
1663 { SPIDER_NET_GMACBSTLMT, SPIDER_NET_BURSTLMT_VALUE },
1664 { 0, 0}
1667 i = 0;
1668 while (regs[i][0]) {
1669 spider_net_write_reg(card, regs[i][0], regs[i][1]);
1670 i++;
1673 /* clear unicast filter table entries 1 to 14 */
1674 for (i = 1; i <= 14; i++) {
1675 spider_net_write_reg(card,
1676 SPIDER_NET_GMRUAFILnR + i * 8,
1677 0x00080000);
1678 spider_net_write_reg(card,
1679 SPIDER_NET_GMRUAFILnR + i * 8 + 4,
1680 0x00000000);
1683 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R, 0x08080000);
1685 spider_net_write_reg(card, SPIDER_NET_ECMODE, SPIDER_NET_ECMODE_VALUE);
1687 /* set chain tail adress for RX chains and
1688 * enable DMA */
1689 spider_net_enable_rxchtails(card);
1690 spider_net_enable_rxdmac(card);
1692 spider_net_write_reg(card, SPIDER_NET_GRXDMAEN, SPIDER_NET_WOL_VALUE);
1694 /* set chain tail adress for TX chain */
1695 spider_net_enable_txdmac(card);
1697 spider_net_write_reg(card, SPIDER_NET_GMACLENLMT,
1698 SPIDER_NET_LENLMT_VALUE);
1699 spider_net_write_reg(card, SPIDER_NET_GMACMODE,
1700 SPIDER_NET_MACMODE_VALUE);
1701 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD,
1702 SPIDER_NET_OPMODE_VALUE);
1704 /* set interrupt mask registers */
1705 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK,
1706 SPIDER_NET_INT0_MASK_VALUE);
1707 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK,
1708 SPIDER_NET_INT1_MASK_VALUE);
1709 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK,
1710 SPIDER_NET_INT2_MASK_VALUE);
1714 * spider_net_open - called upon ifonfig up
1715 * @netdev: interface device structure
1717 * returns 0 on success, <0 on failure
1719 * spider_net_open allocates all the descriptors and memory needed for
1720 * operation, sets up multicast list and enables interrupts
1723 spider_net_open(struct net_device *netdev)
1725 struct spider_net_card *card = netdev_priv(netdev);
1726 int result;
1728 result = -ENOMEM;
1729 if (spider_net_init_chain(card, &card->tx_chain,
1730 card->descr, tx_descriptors))
1731 goto alloc_tx_failed;
1732 if (spider_net_init_chain(card, &card->rx_chain,
1733 card->descr + tx_descriptors, rx_descriptors))
1734 goto alloc_rx_failed;
1736 /* allocate rx skbs */
1737 if (spider_net_alloc_rx_skbs(card))
1738 goto alloc_skbs_failed;
1740 spider_net_set_multi(netdev);
1742 /* further enhancement: setup hw vlan, if needed */
1744 result = -EBUSY;
1745 if (request_irq(netdev->irq, spider_net_interrupt,
1746 SA_SHIRQ, netdev->name, netdev))
1747 goto register_int_failed;
1749 spider_net_enable_card(card);
1751 netif_start_queue(netdev);
1752 netif_carrier_on(netdev);
1753 netif_poll_enable(netdev);
1755 return 0;
1757 register_int_failed:
1758 spider_net_free_rx_chain_contents(card);
1759 alloc_skbs_failed:
1760 spider_net_free_chain(card, &card->rx_chain);
1761 alloc_rx_failed:
1762 spider_net_free_chain(card, &card->tx_chain);
1763 alloc_tx_failed:
1764 return result;
1768 * spider_net_setup_phy - setup PHY
1769 * @card: card structure
1771 * returns 0 on success, <0 on failure
1773 * spider_net_setup_phy is used as part of spider_net_probe. Sets
1774 * the PHY to 1000 Mbps
1776 static int
1777 spider_net_setup_phy(struct spider_net_card *card)
1779 struct mii_phy *phy = &card->phy;
1781 spider_net_write_reg(card, SPIDER_NET_GDTDMASEL,
1782 SPIDER_NET_DMASEL_VALUE);
1783 spider_net_write_reg(card, SPIDER_NET_GPCCTRL,
1784 SPIDER_NET_PHY_CTRL_VALUE);
1785 phy->mii_id = 1;
1786 phy->dev = card->netdev;
1787 phy->mdio_read = spider_net_read_phy;
1788 phy->mdio_write = spider_net_write_phy;
1790 mii_phy_probe(phy, phy->mii_id);
1792 if (phy->def->ops->setup_forced)
1793 phy->def->ops->setup_forced(phy, SPEED_1000, DUPLEX_FULL);
1795 /* the following two writes could be moved to sungem_phy.c */
1796 /* enable fiber mode */
1797 spider_net_write_phy(card->netdev, 1, MII_NCONFIG, 0x9020);
1798 /* LEDs active in both modes, autosense prio = fiber */
1799 spider_net_write_phy(card->netdev, 1, MII_NCONFIG, 0x945f);
1801 /* switch off fibre autoneg */
1802 spider_net_write_phy(card->netdev, 1, MII_NCONFIG, 0xfc01);
1803 spider_net_write_phy(card->netdev, 1, 0x0b, 0x0004);
1805 phy->def->ops->read_link(phy);
1806 pr_info("Found %s with %i Mbps, %s-duplex.\n", phy->def->name,
1807 phy->speed, phy->duplex==1 ? "Full" : "Half");
1809 return 0;
1813 * spider_net_download_firmware - loads firmware into the adapter
1814 * @card: card structure
1815 * @firmware_ptr: pointer to firmware data
1817 * spider_net_download_firmware loads the firmware data into the
1818 * adapter. It assumes the length etc. to be allright.
1820 static int
1821 spider_net_download_firmware(struct spider_net_card *card,
1822 u8 *firmware_ptr)
1824 int sequencer, i;
1825 u32 *fw_ptr = (u32 *)firmware_ptr;
1827 /* stop sequencers */
1828 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1829 SPIDER_NET_STOP_SEQ_VALUE);
1831 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1832 sequencer++) {
1833 spider_net_write_reg(card,
1834 SPIDER_NET_GSnPRGADR + sequencer * 8, 0);
1835 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
1836 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1837 sequencer * 8, *fw_ptr);
1838 fw_ptr++;
1842 if (spider_net_read_reg(card, SPIDER_NET_GSINIT))
1843 return -EIO;
1845 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1846 SPIDER_NET_RUN_SEQ_VALUE);
1848 return 0;
1852 * spider_net_init_firmware - reads in firmware parts
1853 * @card: card structure
1855 * Returns 0 on success, <0 on failure
1857 * spider_net_init_firmware opens the sequencer firmware and does some basic
1858 * checks. This function opens and releases the firmware structure. A call
1859 * to download the firmware is performed before the release.
1861 * Firmware format
1862 * ===============
1863 * spider_fw.bin is expected to be a file containing 6*1024*4 bytes, 4k being
1864 * the program for each sequencer. Use the command
1865 * tail -q -n +2 Seq_code1_0x088.txt Seq_code2_0x090.txt \
1866 * Seq_code3_0x098.txt Seq_code4_0x0A0.txt Seq_code5_0x0A8.txt \
1867 * Seq_code6_0x0B0.txt | xxd -r -p -c4 > spider_fw.bin
1869 * to generate spider_fw.bin, if you have sequencer programs with something
1870 * like the following contents for each sequencer:
1871 * <ONE LINE COMMENT>
1872 * <FIRST 4-BYTES-WORD FOR SEQUENCER>
1873 * <SECOND 4-BYTES-WORD FOR SEQUENCER>
1874 * ...
1875 * <1024th 4-BYTES-WORD FOR SEQUENCER>
1877 static int
1878 spider_net_init_firmware(struct spider_net_card *card)
1880 struct firmware *firmware = NULL;
1881 struct device_node *dn;
1882 u8 *fw_prop = NULL;
1883 int err = -ENOENT;
1884 int fw_size;
1886 if (request_firmware((const struct firmware **)&firmware,
1887 SPIDER_NET_FIRMWARE_NAME, &card->pdev->dev) == 0) {
1888 if ( (firmware->size != SPIDER_NET_FIRMWARE_LEN) &&
1889 netif_msg_probe(card) ) {
1890 pr_err("Incorrect size of spidernet firmware in " \
1891 "filesystem. Looking in host firmware...\n");
1892 goto try_host_fw;
1894 err = spider_net_download_firmware(card, firmware->data);
1896 release_firmware(firmware);
1897 if (err)
1898 goto try_host_fw;
1900 goto done;
1903 try_host_fw:
1904 dn = pci_device_to_OF_node(card->pdev);
1905 if (!dn)
1906 goto out_err;
1908 fw_prop = (u8 *)get_property(dn, "firmware", &fw_size);
1909 if (!fw_prop)
1910 goto out_err;
1912 if ( (fw_size != SPIDER_NET_FIRMWARE_LEN) &&
1913 netif_msg_probe(card) ) {
1914 pr_err("Incorrect size of spidernet firmware in " \
1915 "host firmware\n");
1916 goto done;
1919 err = spider_net_download_firmware(card, fw_prop);
1921 done:
1922 return err;
1923 out_err:
1924 if (netif_msg_probe(card))
1925 pr_err("Couldn't find spidernet firmware in filesystem " \
1926 "or host firmware\n");
1927 return err;
1931 * spider_net_workaround_rxramfull - work around firmware bug
1932 * @card: card structure
1934 * no return value
1936 static void
1937 spider_net_workaround_rxramfull(struct spider_net_card *card)
1939 int i, sequencer = 0;
1941 /* cancel reset */
1942 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1943 SPIDER_NET_CKRCTRL_RUN_VALUE);
1945 /* empty sequencer data */
1946 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1947 sequencer++) {
1948 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1949 sequencer * 8, 0x0);
1950 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
1951 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1952 sequencer * 8, 0x0);
1956 /* set sequencer operation */
1957 spider_net_write_reg(card, SPIDER_NET_GSINIT, 0x000000fe);
1959 /* reset */
1960 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1961 SPIDER_NET_CKRCTRL_STOP_VALUE);
1965 * spider_net_tx_timeout_task - task scheduled by the watchdog timeout
1966 * function (to be called not under interrupt status)
1967 * @data: data, is interface device structure
1969 * called as task when tx hangs, resets interface (if interface is up)
1971 static void
1972 spider_net_tx_timeout_task(void *data)
1974 struct net_device *netdev = data;
1975 struct spider_net_card *card = netdev_priv(netdev);
1977 if (!(netdev->flags & IFF_UP))
1978 goto out;
1980 netif_device_detach(netdev);
1981 spider_net_stop(netdev);
1983 spider_net_workaround_rxramfull(card);
1984 spider_net_init_card(card);
1986 if (spider_net_setup_phy(card))
1987 goto out;
1988 if (spider_net_init_firmware(card))
1989 goto out;
1991 spider_net_open(netdev);
1992 spider_net_kick_tx_dma(card, card->tx_chain.head);
1993 netif_device_attach(netdev);
1995 out:
1996 atomic_dec(&card->tx_timeout_task_counter);
2000 * spider_net_tx_timeout - called when the tx timeout watchdog kicks in.
2001 * @netdev: interface device structure
2003 * called, if tx hangs. Schedules a task that resets the interface
2005 static void
2006 spider_net_tx_timeout(struct net_device *netdev)
2008 struct spider_net_card *card;
2010 card = netdev_priv(netdev);
2011 atomic_inc(&card->tx_timeout_task_counter);
2012 if (netdev->flags & IFF_UP)
2013 schedule_work(&card->tx_timeout_task);
2014 else
2015 atomic_dec(&card->tx_timeout_task_counter);
2019 * spider_net_setup_netdev_ops - initialization of net_device operations
2020 * @netdev: net_device structure
2022 * fills out function pointers in the net_device structure
2024 static void
2025 spider_net_setup_netdev_ops(struct net_device *netdev)
2027 netdev->open = &spider_net_open;
2028 netdev->stop = &spider_net_stop;
2029 netdev->hard_start_xmit = &spider_net_xmit;
2030 netdev->get_stats = &spider_net_get_stats;
2031 netdev->set_multicast_list = &spider_net_set_multi;
2032 netdev->set_mac_address = &spider_net_set_mac;
2033 netdev->change_mtu = &spider_net_change_mtu;
2034 netdev->do_ioctl = &spider_net_do_ioctl;
2035 /* tx watchdog */
2036 netdev->tx_timeout = &spider_net_tx_timeout;
2037 netdev->watchdog_timeo = SPIDER_NET_WATCHDOG_TIMEOUT;
2038 /* NAPI */
2039 netdev->poll = &spider_net_poll;
2040 netdev->weight = SPIDER_NET_NAPI_WEIGHT;
2041 /* HW VLAN */
2042 netdev->vlan_rx_register = &spider_net_vlan_rx_reg;
2043 netdev->vlan_rx_add_vid = &spider_net_vlan_rx_add;
2044 netdev->vlan_rx_kill_vid = &spider_net_vlan_rx_kill;
2045 #ifdef CONFIG_NET_POLL_CONTROLLER
2046 /* poll controller */
2047 netdev->poll_controller = &spider_net_poll_controller;
2048 #endif /* CONFIG_NET_POLL_CONTROLLER */
2049 /* ethtool ops */
2050 netdev->ethtool_ops = &spider_net_ethtool_ops;
2054 * spider_net_setup_netdev - initialization of net_device
2055 * @card: card structure
2057 * Returns 0 on success or <0 on failure
2059 * spider_net_setup_netdev initializes the net_device structure
2061 static int
2062 spider_net_setup_netdev(struct spider_net_card *card)
2064 int result;
2065 struct net_device *netdev = card->netdev;
2066 struct device_node *dn;
2067 struct sockaddr addr;
2068 u8 *mac;
2070 SET_MODULE_OWNER(netdev);
2071 SET_NETDEV_DEV(netdev, &card->pdev->dev);
2073 pci_set_drvdata(card->pdev, netdev);
2075 atomic_set(&card->tx_chain_release,0);
2076 card->rxram_full_tl.data = (unsigned long) card;
2077 card->rxram_full_tl.func =
2078 (void (*)(unsigned long)) spider_net_handle_rxram_full;
2079 init_timer(&card->tx_timer);
2080 card->tx_timer.function =
2081 (void (*)(unsigned long)) spider_net_cleanup_tx_ring;
2082 card->tx_timer.data = (unsigned long) card;
2083 netdev->irq = card->pdev->irq;
2085 card->options.rx_csum = SPIDER_NET_RX_CSUM_DEFAULT;
2087 spider_net_setup_netdev_ops(netdev);
2089 netdev->features = 0;
2090 /* some time: NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
2091 * NETIF_F_HW_VLAN_FILTER */
2093 netdev->irq = card->pdev->irq;
2095 dn = pci_device_to_OF_node(card->pdev);
2096 if (!dn)
2097 return -EIO;
2099 mac = (u8 *)get_property(dn, "local-mac-address", NULL);
2100 if (!mac)
2101 return -EIO;
2102 memcpy(addr.sa_data, mac, ETH_ALEN);
2104 result = spider_net_set_mac(netdev, &addr);
2105 if ((result) && (netif_msg_probe(card)))
2106 pr_err("Failed to set MAC address: %i\n", result);
2108 result = register_netdev(netdev);
2109 if (result) {
2110 if (netif_msg_probe(card))
2111 pr_err("Couldn't register net_device: %i\n",
2112 result);
2113 return result;
2116 if (netif_msg_probe(card))
2117 pr_info("Initialized device %s.\n", netdev->name);
2119 return 0;
2123 * spider_net_alloc_card - allocates net_device and card structure
2125 * returns the card structure or NULL in case of errors
2127 * the card and net_device structures are linked to each other
2129 static struct spider_net_card *
2130 spider_net_alloc_card(void)
2132 struct net_device *netdev;
2133 struct spider_net_card *card;
2134 size_t alloc_size;
2136 alloc_size = sizeof (*card) +
2137 sizeof (struct spider_net_descr) * rx_descriptors +
2138 sizeof (struct spider_net_descr) * tx_descriptors;
2139 netdev = alloc_etherdev(alloc_size);
2140 if (!netdev)
2141 return NULL;
2143 card = netdev_priv(netdev);
2144 card->netdev = netdev;
2145 card->msg_enable = SPIDER_NET_DEFAULT_MSG;
2146 INIT_WORK(&card->tx_timeout_task, spider_net_tx_timeout_task, netdev);
2147 init_waitqueue_head(&card->waitq);
2148 atomic_set(&card->tx_timeout_task_counter, 0);
2150 return card;
2154 * spider_net_undo_pci_setup - releases PCI ressources
2155 * @card: card structure
2157 * spider_net_undo_pci_setup releases the mapped regions
2159 static void
2160 spider_net_undo_pci_setup(struct spider_net_card *card)
2162 iounmap(card->regs);
2163 pci_release_regions(card->pdev);
2167 * spider_net_setup_pci_dev - sets up the device in terms of PCI operations
2168 * @card: card structure
2169 * @pdev: PCI device
2171 * Returns the card structure or NULL if any errors occur
2173 * spider_net_setup_pci_dev initializes pdev and together with the
2174 * functions called in spider_net_open configures the device so that
2175 * data can be transferred over it
2176 * The net_device structure is attached to the card structure, if the
2177 * function returns without error.
2179 static struct spider_net_card *
2180 spider_net_setup_pci_dev(struct pci_dev *pdev)
2182 struct spider_net_card *card;
2183 unsigned long mmio_start, mmio_len;
2185 if (pci_enable_device(pdev)) {
2186 pr_err("Couldn't enable PCI device\n");
2187 return NULL;
2190 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
2191 pr_err("Couldn't find proper PCI device base address.\n");
2192 goto out_disable_dev;
2195 if (pci_request_regions(pdev, spider_net_driver_name)) {
2196 pr_err("Couldn't obtain PCI resources, aborting.\n");
2197 goto out_disable_dev;
2200 pci_set_master(pdev);
2202 card = spider_net_alloc_card();
2203 if (!card) {
2204 pr_err("Couldn't allocate net_device structure, "
2205 "aborting.\n");
2206 goto out_release_regions;
2208 card->pdev = pdev;
2210 /* fetch base address and length of first resource */
2211 mmio_start = pci_resource_start(pdev, 0);
2212 mmio_len = pci_resource_len(pdev, 0);
2214 card->netdev->mem_start = mmio_start;
2215 card->netdev->mem_end = mmio_start + mmio_len;
2216 card->regs = ioremap(mmio_start, mmio_len);
2218 if (!card->regs) {
2219 pr_err("Couldn't obtain PCI resources, aborting.\n");
2220 goto out_release_regions;
2223 return card;
2225 out_release_regions:
2226 pci_release_regions(pdev);
2227 out_disable_dev:
2228 pci_disable_device(pdev);
2229 pci_set_drvdata(pdev, NULL);
2230 return NULL;
2234 * spider_net_probe - initialization of a device
2235 * @pdev: PCI device
2236 * @ent: entry in the device id list
2238 * Returns 0 on success, <0 on failure
2240 * spider_net_probe initializes pdev and registers a net_device
2241 * structure for it. After that, the device can be ifconfig'ed up
2243 static int __devinit
2244 spider_net_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2246 int err = -EIO;
2247 struct spider_net_card *card;
2249 card = spider_net_setup_pci_dev(pdev);
2250 if (!card)
2251 goto out;
2253 spider_net_workaround_rxramfull(card);
2254 spider_net_init_card(card);
2256 err = spider_net_setup_phy(card);
2257 if (err)
2258 goto out_undo_pci;
2260 err = spider_net_init_firmware(card);
2261 if (err)
2262 goto out_undo_pci;
2264 err = spider_net_setup_netdev(card);
2265 if (err)
2266 goto out_undo_pci;
2268 return 0;
2270 out_undo_pci:
2271 spider_net_undo_pci_setup(card);
2272 free_netdev(card->netdev);
2273 out:
2274 return err;
2278 * spider_net_remove - removal of a device
2279 * @pdev: PCI device
2281 * Returns 0 on success, <0 on failure
2283 * spider_net_remove is called to remove the device and unregisters the
2284 * net_device
2286 static void __devexit
2287 spider_net_remove(struct pci_dev *pdev)
2289 struct net_device *netdev;
2290 struct spider_net_card *card;
2292 netdev = pci_get_drvdata(pdev);
2293 card = netdev_priv(netdev);
2295 wait_event(card->waitq,
2296 atomic_read(&card->tx_timeout_task_counter) == 0);
2298 unregister_netdev(netdev);
2300 /* switch off card */
2301 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2302 SPIDER_NET_CKRCTRL_STOP_VALUE);
2303 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2304 SPIDER_NET_CKRCTRL_RUN_VALUE);
2306 spider_net_undo_pci_setup(card);
2307 free_netdev(netdev);
2310 static struct pci_driver spider_net_driver = {
2311 .name = spider_net_driver_name,
2312 .id_table = spider_net_pci_tbl,
2313 .probe = spider_net_probe,
2314 .remove = __devexit_p(spider_net_remove)
2318 * spider_net_init - init function when the driver is loaded
2320 * spider_net_init registers the device driver
2322 static int __init spider_net_init(void)
2324 if (rx_descriptors < SPIDER_NET_RX_DESCRIPTORS_MIN) {
2325 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MIN;
2326 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2328 if (rx_descriptors > SPIDER_NET_RX_DESCRIPTORS_MAX) {
2329 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MAX;
2330 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2332 if (tx_descriptors < SPIDER_NET_TX_DESCRIPTORS_MIN) {
2333 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MIN;
2334 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2336 if (tx_descriptors > SPIDER_NET_TX_DESCRIPTORS_MAX) {
2337 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MAX;
2338 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2341 return pci_register_driver(&spider_net_driver);
2345 * spider_net_cleanup - exit function when driver is unloaded
2347 * spider_net_cleanup unregisters the device driver
2349 static void __exit spider_net_cleanup(void)
2351 pci_unregister_driver(&spider_net_driver);
2354 module_init(spider_net_init);
2355 module_exit(spider_net_cleanup);