ia64/linux-2.6.18-xen.hg

view drivers/net/spider_net.c @ 897:329ea0ccb344

balloon: try harder to balloon up under memory pressure.

Currently if the balloon driver is unable to increase the guest's
reservation it assumes the failure was due to reaching its full
allocation, gives up on the ballooning operation and records the limit
it reached as the "hard limit". The driver will not try again until
the target is set again (even to the same value).

However it is possible that ballooning has in fact failed due to
memory pressure in the host and therefore it is desirable to keep
attempting to reach the target in case memory becomes available. The
most likely scenario is that some guests are ballooning down while
others are ballooning up and therefore there is temporary memory
pressure while things stabilise. You would not expect a well behaved
toolstack to ask a domain to balloon to more than its allocation nor
would you expect it to deliberately over-commit memory by setting
balloon targets which exceed the total host memory.

This patch drops the concept of a hard limit and causes the balloon
driver to retry increasing the reservation on a timer in the same
manner as when decreasing the reservation.

Also if we partially succeed in increasing the reservation
(i.e. receive less pages than we asked for) then we may as well keep
those pages rather than returning them to Xen.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Fri Jun 05 14:01:20 2009 +0100 (2009-06-05)
parents 831230e53067
children
line source
1 /*
2 * Network device driver for Cell Processor-Based Blade
3 *
4 * (C) Copyright IBM Corp. 2005
5 *
6 * Authors : Utz Bacher <utz.bacher@de.ibm.com>
7 * Jens Osterkamp <Jens.Osterkamp@de.ibm.com>
8 *
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.
13 *
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.
18 *
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.
22 */
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, }
76 };
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
84 *
85 * returns the content of the specified SMMIO register.
86 */
87 static inline u32
88 spider_net_read_reg(struct spider_net_card *card, u32 reg)
89 {
90 u32 value;
92 value = readl(card->regs + reg);
93 value = le32_to_cpu(value);
95 return value;
96 }
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
103 */
104 static inline void
105 spider_net_write_reg(struct spider_net_card *card, u32 reg, u32 value)
106 {
107 value = cpu_to_le32(value);
108 writel(value, card->regs + reg);
109 }
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
116 *
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).
120 **/
121 static void
122 spider_net_write_phy(struct net_device *netdev, int mii_id,
123 int reg, int val)
124 {
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);
132 }
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
138 *
139 * Returns value read from PHY register
140 *
141 * spider_net_write_phy reads from an arbitrary PHY
142 * register via the spider GPCROPCMD register
143 **/
144 static int
145 spider_net_read_phy(struct net_device *netdev, int mii_id, int reg)
146 {
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;
163 }
165 /**
166 * spider_net_rx_irq_off - switch off rx irq on this spider card
167 * @card: device structure
168 *
169 * switches off rx irq by masking them out in the GHIINTnMSK register
170 */
171 static void
172 spider_net_rx_irq_off(struct spider_net_card *card)
173 {
174 u32 regvalue;
176 regvalue = SPIDER_NET_INT0_MASK_VALUE & (~SPIDER_NET_RXINT);
177 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, regvalue);
178 }
180 /**
181 * spider_net_rx_irq_on - switch on rx irq on this spider card
182 * @card: device structure
183 *
184 * switches on rx irq by enabling them in the GHIINTnMSK register
185 */
186 static void
187 spider_net_rx_irq_on(struct spider_net_card *card)
188 {
189 u32 regvalue;
191 regvalue = SPIDER_NET_INT0_MASK_VALUE | SPIDER_NET_RXINT;
192 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, regvalue);
193 }
195 /**
196 * spider_net_set_promisc - sets the unicast address or the promiscuous mode
197 * @card: card structure
198 *
199 * spider_net_set_promisc sets the unicast destination address filter and
200 * thus either allows for non-promisc mode or promisc mode
201 */
202 static void
203 spider_net_set_promisc(struct spider_net_card *card)
204 {
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);
225 }
226 }
228 /**
229 * spider_net_get_mac_address - read mac address from spider card
230 * @card: device structure
231 *
232 * reads MAC address from GMACUNIMACU and GMACUNIMACL registers
233 */
234 static int
235 spider_net_get_mac_address(struct net_device *netdev)
236 {
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;
254 }
256 /**
257 * spider_net_get_descr_status -- returns the status of a descriptor
258 * @descr: descriptor to look at
259 *
260 * returns the status as in the dmac_cmd_status field of the descriptor
261 */
262 static inline int
263 spider_net_get_descr_status(struct spider_net_descr *descr)
264 {
265 return descr->dmac_cmd_status & SPIDER_NET_DESCR_IND_PROC_MASK;
266 }
268 /**
269 * spider_net_free_chain - free descriptor chain
270 * @card: card structure
271 * @chain: address of chain
272 *
273 */
274 static void
275 spider_net_free_chain(struct spider_net_card *card,
276 struct spider_net_descr_chain *chain)
277 {
278 struct spider_net_descr *descr;
280 for (descr = chain->tail; !descr->bus_addr; descr = descr->next) {
281 pci_unmap_single(card->pdev, descr->bus_addr,
282 SPIDER_NET_DESCR_SIZE, PCI_DMA_BIDIRECTIONAL);
283 descr->bus_addr = 0;
284 }
285 }
287 /**
288 * spider_net_init_chain - links descriptor chain
289 * @card: card structure
290 * @chain: address of chain
291 * @start_descr: address of descriptor array
292 * @no: number of descriptors
293 *
294 * we manage a circular list that mirrors the hardware structure,
295 * except that the hardware uses bus addresses.
296 *
297 * returns 0 on success, <0 on failure
298 */
299 static int
300 spider_net_init_chain(struct spider_net_card *card,
301 struct spider_net_descr_chain *chain,
302 struct spider_net_descr *start_descr,
303 int direction, int no)
304 {
305 int i;
306 struct spider_net_descr *descr;
307 dma_addr_t buf;
309 descr = start_descr;
310 memset(descr, 0, sizeof(*descr) * no);
312 /* set up the hardware pointers in each descriptor */
313 for (i=0; i<no; i++, descr++) {
314 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
316 buf = pci_map_single(card->pdev, descr,
317 SPIDER_NET_DESCR_SIZE,
318 direction);
320 if (buf == DMA_ERROR_CODE)
321 goto iommu_error;
323 descr->bus_addr = buf;
324 descr->next = descr + 1;
325 descr->prev = descr - 1;
327 }
328 /* do actual circular list */
329 (descr-1)->next = start_descr;
330 start_descr->prev = descr-1;
332 descr = start_descr;
333 if (direction == PCI_DMA_FROMDEVICE)
334 for (i=0; i < no; i++, descr++)
335 descr->next_descr_addr = descr->next->bus_addr;
337 spin_lock_init(&chain->lock);
338 chain->head = start_descr;
339 chain->tail = start_descr;
341 return 0;
343 iommu_error:
344 descr = start_descr;
345 for (i=0; i < no; i++, descr++)
346 if (descr->bus_addr)
347 pci_unmap_single(card->pdev, descr->bus_addr,
348 SPIDER_NET_DESCR_SIZE,
349 direction);
350 return -ENOMEM;
351 }
353 /**
354 * spider_net_free_rx_chain_contents - frees descr contents in rx chain
355 * @card: card structure
356 *
357 * returns 0 on success, <0 on failure
358 */
359 static void
360 spider_net_free_rx_chain_contents(struct spider_net_card *card)
361 {
362 struct spider_net_descr *descr;
364 descr = card->rx_chain.head;
365 while (descr->next != card->rx_chain.head) {
366 if (descr->skb) {
367 dev_kfree_skb(descr->skb);
368 pci_unmap_single(card->pdev, descr->buf_addr,
369 SPIDER_NET_MAX_FRAME,
370 PCI_DMA_FROMDEVICE);
371 }
372 descr = descr->next;
373 }
374 }
376 /**
377 * spider_net_prepare_rx_descr - reinitializes a rx descriptor
378 * @card: card structure
379 * @descr: descriptor to re-init
380 *
381 * return 0 on succes, <0 on failure
382 *
383 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
384 * Activate the descriptor state-wise
385 */
386 static int
387 spider_net_prepare_rx_descr(struct spider_net_card *card,
388 struct spider_net_descr *descr)
389 {
390 dma_addr_t buf;
391 int error = 0;
392 int offset;
393 int bufsize;
395 /* we need to round up the buffer size to a multiple of 128 */
396 bufsize = (SPIDER_NET_MAX_FRAME + SPIDER_NET_RXBUF_ALIGN - 1) &
397 (~(SPIDER_NET_RXBUF_ALIGN - 1));
399 /* and we need to have it 128 byte aligned, therefore we allocate a
400 * bit more */
401 /* allocate an skb */
402 descr->skb = dev_alloc_skb(bufsize + SPIDER_NET_RXBUF_ALIGN - 1);
403 if (!descr->skb) {
404 if (netif_msg_rx_err(card) && net_ratelimit())
405 pr_err("Not enough memory to allocate rx buffer\n");
406 return -ENOMEM;
407 }
408 descr->buf_size = bufsize;
409 descr->result_size = 0;
410 descr->valid_size = 0;
411 descr->data_status = 0;
412 descr->data_error = 0;
414 offset = ((unsigned long)descr->skb->data) &
415 (SPIDER_NET_RXBUF_ALIGN - 1);
416 if (offset)
417 skb_reserve(descr->skb, SPIDER_NET_RXBUF_ALIGN - offset);
418 /* io-mmu-map the skb */
419 buf = pci_map_single(card->pdev, descr->skb->data,
420 SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
421 descr->buf_addr = buf;
422 if (buf == DMA_ERROR_CODE) {
423 dev_kfree_skb_any(descr->skb);
424 if (netif_msg_rx_err(card) && net_ratelimit())
425 pr_err("Could not iommu-map rx buffer\n");
426 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
427 } else {
428 descr->dmac_cmd_status = SPIDER_NET_DESCR_CARDOWNED |
429 SPIDER_NET_DMAC_NOINTR_COMPLETE;
430 }
432 return error;
433 }
435 /**
436 * spider_net_enable_rxchtails - sets RX dmac chain tail addresses
437 * @card: card structure
438 *
439 * spider_net_enable_rxchtails sets the RX DMAC chain tail adresses in the
440 * chip by writing to the appropriate register. DMA is enabled in
441 * spider_net_enable_rxdmac.
442 */
443 static inline void
444 spider_net_enable_rxchtails(struct spider_net_card *card)
445 {
446 /* assume chain is aligned correctly */
447 spider_net_write_reg(card, SPIDER_NET_GDADCHA ,
448 card->rx_chain.tail->bus_addr);
449 }
451 /**
452 * spider_net_enable_rxdmac - enables a receive DMA controller
453 * @card: card structure
454 *
455 * spider_net_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
456 * in the GDADMACCNTR register
457 */
458 static inline void
459 spider_net_enable_rxdmac(struct spider_net_card *card)
460 {
461 wmb();
462 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
463 SPIDER_NET_DMA_RX_VALUE);
464 }
466 /**
467 * spider_net_refill_rx_chain - refills descriptors/skbs in the rx chains
468 * @card: card structure
469 *
470 * refills descriptors in the rx chain: allocates skbs and iommu-maps them.
471 */
472 static void
473 spider_net_refill_rx_chain(struct spider_net_card *card)
474 {
475 struct spider_net_descr_chain *chain = &card->rx_chain;
476 unsigned long flags;
478 /* one context doing the refill (and a second context seeing that
479 * and omitting it) is ok. If called by NAPI, we'll be called again
480 * as spider_net_decode_one_descr is called several times. If some
481 * interrupt calls us, the NAPI is about to clean up anyway. */
482 if (!spin_trylock_irqsave(&chain->lock, flags))
483 return;
485 while (spider_net_get_descr_status(chain->head) ==
486 SPIDER_NET_DESCR_NOT_IN_USE) {
487 if (spider_net_prepare_rx_descr(card, chain->head))
488 break;
489 chain->head = chain->head->next;
490 }
492 spin_unlock_irqrestore(&chain->lock, flags);
493 }
495 /**
496 * spider_net_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
497 * @card: card structure
498 *
499 * returns 0 on success, <0 on failure
500 */
501 static int
502 spider_net_alloc_rx_skbs(struct spider_net_card *card)
503 {
504 int result;
505 struct spider_net_descr_chain *chain;
507 result = -ENOMEM;
509 chain = &card->rx_chain;
510 /* put at least one buffer into the chain. if this fails,
511 * we've got a problem. if not, spider_net_refill_rx_chain
512 * will do the rest at the end of this function */
513 if (spider_net_prepare_rx_descr(card, chain->head))
514 goto error;
515 else
516 chain->head = chain->head->next;
518 /* this will allocate the rest of the rx buffers; if not, it's
519 * business as usual later on */
520 spider_net_refill_rx_chain(card);
521 spider_net_enable_rxdmac(card);
522 return 0;
524 error:
525 spider_net_free_rx_chain_contents(card);
526 return result;
527 }
529 /**
530 * spider_net_get_multicast_hash - generates hash for multicast filter table
531 * @addr: multicast address
532 *
533 * returns the hash value.
534 *
535 * spider_net_get_multicast_hash calculates a hash value for a given multicast
536 * address, that is used to set the multicast filter tables
537 */
538 static u8
539 spider_net_get_multicast_hash(struct net_device *netdev, __u8 *addr)
540 {
541 u32 crc;
542 u8 hash;
543 char addr_for_crc[ETH_ALEN] = { 0, };
544 int i, bit;
546 for (i = 0; i < ETH_ALEN * 8; i++) {
547 bit = (addr[i / 8] >> (i % 8)) & 1;
548 addr_for_crc[ETH_ALEN - 1 - i / 8] += bit << (7 - (i % 8));
549 }
551 crc = crc32_be(~0, addr_for_crc, netdev->addr_len);
553 hash = (crc >> 27);
554 hash <<= 3;
555 hash |= crc & 7;
556 hash &= 0xff;
558 return hash;
559 }
561 /**
562 * spider_net_set_multi - sets multicast addresses and promisc flags
563 * @netdev: interface device structure
564 *
565 * spider_net_set_multi configures multicast addresses as needed for the
566 * netdev interface. It also sets up multicast, allmulti and promisc
567 * flags appropriately
568 */
569 static void
570 spider_net_set_multi(struct net_device *netdev)
571 {
572 struct dev_mc_list *mc;
573 u8 hash;
574 int i;
575 u32 reg;
576 struct spider_net_card *card = netdev_priv(netdev);
577 unsigned long bitmask[SPIDER_NET_MULTICAST_HASHES / BITS_PER_LONG] =
578 {0, };
580 spider_net_set_promisc(card);
582 if (netdev->flags & IFF_ALLMULTI) {
583 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES; i++) {
584 set_bit(i, bitmask);
585 }
586 goto write_hash;
587 }
589 /* well, we know, what the broadcast hash value is: it's xfd
590 hash = spider_net_get_multicast_hash(netdev, netdev->broadcast); */
591 set_bit(0xfd, bitmask);
593 for (mc = netdev->mc_list; mc; mc = mc->next) {
594 hash = spider_net_get_multicast_hash(netdev, mc->dmi_addr);
595 set_bit(hash, bitmask);
596 }
598 write_hash:
599 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES / 4; i++) {
600 reg = 0;
601 if (test_bit(i * 4, bitmask))
602 reg += 0x08;
603 reg <<= 8;
604 if (test_bit(i * 4 + 1, bitmask))
605 reg += 0x08;
606 reg <<= 8;
607 if (test_bit(i * 4 + 2, bitmask))
608 reg += 0x08;
609 reg <<= 8;
610 if (test_bit(i * 4 + 3, bitmask))
611 reg += 0x08;
613 spider_net_write_reg(card, SPIDER_NET_GMRMHFILnR + i * 4, reg);
614 }
615 }
617 /**
618 * spider_net_disable_rxdmac - disables the receive DMA controller
619 * @card: card structure
620 *
621 * spider_net_disable_rxdmac terminates processing on the DMA controller by
622 * turing off DMA and issueing a force end
623 */
624 static void
625 spider_net_disable_rxdmac(struct spider_net_card *card)
626 {
627 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
628 SPIDER_NET_DMA_RX_FEND_VALUE);
629 }
631 /**
632 * spider_net_prepare_tx_descr - fill tx descriptor with skb data
633 * @card: card structure
634 * @descr: descriptor structure to fill out
635 * @skb: packet to use
636 *
637 * returns 0 on success, <0 on failure.
638 *
639 * fills out the descriptor structure with skb data and len. Copies data,
640 * if needed (32bit DMA!)
641 */
642 static int
643 spider_net_prepare_tx_descr(struct spider_net_card *card,
644 struct sk_buff *skb)
645 {
646 struct spider_net_descr *descr = card->tx_chain.head;
647 dma_addr_t buf;
649 buf = pci_map_single(card->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
650 if (buf == DMA_ERROR_CODE) {
651 if (netif_msg_tx_err(card) && net_ratelimit())
652 pr_err("could not iommu-map packet (%p, %i). "
653 "Dropping packet\n", skb->data, skb->len);
654 return -ENOMEM;
655 }
657 descr->buf_addr = buf;
658 descr->buf_size = skb->len;
659 descr->next_descr_addr = 0;
660 descr->skb = skb;
661 descr->data_status = 0;
663 descr->dmac_cmd_status =
664 SPIDER_NET_DESCR_CARDOWNED | SPIDER_NET_DMAC_NOCS;
665 if (skb->protocol == htons(ETH_P_IP))
666 switch (skb->nh.iph->protocol) {
667 case IPPROTO_TCP:
668 descr->dmac_cmd_status |= SPIDER_NET_DMAC_TCP;
669 break;
670 case IPPROTO_UDP:
671 descr->dmac_cmd_status |= SPIDER_NET_DMAC_UDP;
672 break;
673 }
675 descr->prev->next_descr_addr = descr->bus_addr;
677 return 0;
678 }
680 /**
681 * spider_net_release_tx_descr - processes a used tx descriptor
682 * @card: card structure
683 * @descr: descriptor to release
684 *
685 * releases a used tx descriptor (unmapping, freeing of skb)
686 */
687 static inline void
688 spider_net_release_tx_descr(struct spider_net_card *card)
689 {
690 struct spider_net_descr *descr = card->tx_chain.tail;
691 struct sk_buff *skb;
693 card->tx_chain.tail = card->tx_chain.tail->next;
694 descr->dmac_cmd_status |= SPIDER_NET_DESCR_NOT_IN_USE;
696 /* unmap the skb */
697 skb = descr->skb;
698 pci_unmap_single(card->pdev, descr->buf_addr, skb->len,
699 PCI_DMA_TODEVICE);
700 dev_kfree_skb_any(skb);
701 }
703 /**
704 * spider_net_release_tx_chain - processes sent tx descriptors
705 * @card: adapter structure
706 * @brutal: if set, don't care about whether descriptor seems to be in use
707 *
708 * returns 0 if the tx ring is empty, otherwise 1.
709 *
710 * spider_net_release_tx_chain releases the tx descriptors that spider has
711 * finished with (if non-brutal) or simply release tx descriptors (if brutal).
712 * If some other context is calling this function, we return 1 so that we're
713 * scheduled again (if we were scheduled) and will not loose initiative.
714 */
715 static int
716 spider_net_release_tx_chain(struct spider_net_card *card, int brutal)
717 {
718 struct spider_net_descr_chain *chain = &card->tx_chain;
719 int status;
721 spider_net_read_reg(card, SPIDER_NET_GDTDMACCNTR);
723 while (chain->tail != chain->head) {
724 status = spider_net_get_descr_status(chain->tail);
725 switch (status) {
726 case SPIDER_NET_DESCR_COMPLETE:
727 card->netdev_stats.tx_packets++;
728 card->netdev_stats.tx_bytes += chain->tail->skb->len;
729 break;
731 case SPIDER_NET_DESCR_CARDOWNED:
732 if (!brutal)
733 return 1;
734 /* fallthrough, if we release the descriptors
735 * brutally (then we don't care about
736 * SPIDER_NET_DESCR_CARDOWNED) */
738 case SPIDER_NET_DESCR_RESPONSE_ERROR:
739 case SPIDER_NET_DESCR_PROTECTION_ERROR:
740 case SPIDER_NET_DESCR_FORCE_END:
741 if (netif_msg_tx_err(card))
742 pr_err("%s: forcing end of tx descriptor "
743 "with status x%02x\n",
744 card->netdev->name, status);
745 card->netdev_stats.tx_errors++;
746 break;
748 default:
749 card->netdev_stats.tx_dropped++;
750 return 1;
751 }
752 spider_net_release_tx_descr(card);
753 }
755 return 0;
756 }
758 /**
759 * spider_net_kick_tx_dma - enables TX DMA processing
760 * @card: card structure
761 * @descr: descriptor address to enable TX processing at
762 *
763 * spider_net_kick_tx_dma writes the current tx chain head as start address
764 * of the tx descriptor chain and enables the transmission DMA engine
765 */
766 static inline void
767 spider_net_kick_tx_dma(struct spider_net_card *card)
768 {
769 struct spider_net_descr *descr;
771 if (spider_net_read_reg(card, SPIDER_NET_GDTDMACCNTR) &
772 SPIDER_NET_TX_DMA_EN)
773 goto out;
775 descr = card->tx_chain.tail;
776 for (;;) {
777 if (spider_net_get_descr_status(descr) ==
778 SPIDER_NET_DESCR_CARDOWNED) {
779 spider_net_write_reg(card, SPIDER_NET_GDTDCHA,
780 descr->bus_addr);
781 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
782 SPIDER_NET_DMA_TX_VALUE);
783 break;
784 }
785 if (descr == card->tx_chain.head)
786 break;
787 descr = descr->next;
788 }
790 out:
791 mod_timer(&card->tx_timer, jiffies + SPIDER_NET_TX_TIMER);
792 }
794 /**
795 * spider_net_xmit - transmits a frame over the device
796 * @skb: packet to send out
797 * @netdev: interface device structure
798 *
799 * returns 0 on success, !0 on failure
800 */
801 static int
802 spider_net_xmit(struct sk_buff *skb, struct net_device *netdev)
803 {
804 struct spider_net_card *card = netdev_priv(netdev);
805 struct spider_net_descr_chain *chain = &card->tx_chain;
806 struct spider_net_descr *descr = chain->head;
807 unsigned long flags;
808 int result;
810 spin_lock_irqsave(&chain->lock, flags);
812 spider_net_release_tx_chain(card, 0);
814 if (chain->head->next == chain->tail->prev) {
815 card->netdev_stats.tx_dropped++;
816 result = NETDEV_TX_LOCKED;
817 goto out;
818 }
820 if (spider_net_get_descr_status(descr) != SPIDER_NET_DESCR_NOT_IN_USE) {
821 result = NETDEV_TX_LOCKED;
822 goto out;
823 }
825 if (spider_net_prepare_tx_descr(card, skb) != 0) {
826 card->netdev_stats.tx_dropped++;
827 result = NETDEV_TX_BUSY;
828 goto out;
829 }
831 result = NETDEV_TX_OK;
833 spider_net_kick_tx_dma(card);
834 card->tx_chain.head = card->tx_chain.head->next;
836 out:
837 spin_unlock_irqrestore(&chain->lock, flags);
838 netif_wake_queue(netdev);
839 return result;
840 }
842 /**
843 * spider_net_cleanup_tx_ring - cleans up the TX ring
844 * @card: card structure
845 *
846 * spider_net_cleanup_tx_ring is called by the tx_timer (as we don't use
847 * interrupts to cleanup our TX ring) and returns sent packets to the stack
848 * by freeing them
849 */
850 static void
851 spider_net_cleanup_tx_ring(struct spider_net_card *card)
852 {
853 unsigned long flags;
855 spin_lock_irqsave(&card->tx_chain.lock, flags);
857 if ((spider_net_release_tx_chain(card, 0) != 0) &&
858 (card->netdev->flags & IFF_UP))
859 spider_net_kick_tx_dma(card);
861 spin_unlock_irqrestore(&card->tx_chain.lock, flags);
862 }
864 /**
865 * spider_net_do_ioctl - called for device ioctls
866 * @netdev: interface device structure
867 * @ifr: request parameter structure for ioctl
868 * @cmd: command code for ioctl
869 *
870 * returns 0 on success, <0 on failure. Currently, we have no special ioctls.
871 * -EOPNOTSUPP is returned, if an unknown ioctl was requested
872 */
873 static int
874 spider_net_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
875 {
876 switch (cmd) {
877 default:
878 return -EOPNOTSUPP;
879 }
880 }
882 /**
883 * spider_net_pass_skb_up - takes an skb from a descriptor and passes it on
884 * @descr: descriptor to process
885 * @card: card structure
886 * @napi: whether caller is in NAPI context
887 *
888 * returns 1 on success, 0 if no packet was passed to the stack
889 *
890 * iommu-unmaps the skb, fills out skb structure and passes the data to the
891 * stack. The descriptor state is not changed.
892 */
893 static int
894 spider_net_pass_skb_up(struct spider_net_descr *descr,
895 struct spider_net_card *card, int napi)
896 {
897 struct sk_buff *skb;
898 struct net_device *netdev;
899 u32 data_status, data_error;
901 data_status = descr->data_status;
902 data_error = descr->data_error;
904 netdev = card->netdev;
906 /* unmap descriptor */
907 pci_unmap_single(card->pdev, descr->buf_addr, SPIDER_NET_MAX_FRAME,
908 PCI_DMA_FROMDEVICE);
910 /* the cases we'll throw away the packet immediately */
911 if (data_error & SPIDER_NET_DESTROY_RX_FLAGS) {
912 if (netif_msg_rx_err(card))
913 pr_err("error in received descriptor found, "
914 "data_status=x%08x, data_error=x%08x\n",
915 data_status, data_error);
916 return 0;
917 }
919 skb = descr->skb;
920 skb->dev = netdev;
921 skb_put(skb, descr->valid_size);
923 /* the card seems to add 2 bytes of junk in front
924 * of the ethernet frame */
925 #define SPIDER_MISALIGN 2
926 skb_pull(skb, SPIDER_MISALIGN);
927 skb->protocol = eth_type_trans(skb, netdev);
929 /* checksum offload */
930 if (card->options.rx_csum) {
931 if ( ( (data_status & SPIDER_NET_DATA_STATUS_CKSUM_MASK) ==
932 SPIDER_NET_DATA_STATUS_CKSUM_MASK) &&
933 !(data_error & SPIDER_NET_DATA_ERR_CKSUM_MASK))
934 skb->ip_summed = CHECKSUM_UNNECESSARY;
935 else
936 skb->ip_summed = CHECKSUM_NONE;
937 } else
938 skb->ip_summed = CHECKSUM_NONE;
940 if (data_status & SPIDER_NET_VLAN_PACKET) {
941 /* further enhancements: HW-accel VLAN
942 * vlan_hwaccel_receive_skb
943 */
944 }
946 /* pass skb up to stack */
947 if (napi)
948 netif_receive_skb(skb);
949 else
950 netif_rx_ni(skb);
952 /* update netdevice statistics */
953 card->netdev_stats.rx_packets++;
954 card->netdev_stats.rx_bytes += skb->len;
956 return 1;
957 }
959 /**
960 * spider_net_decode_one_descr - processes an rx descriptor
961 * @card: card structure
962 * @napi: whether caller is in NAPI context
963 *
964 * returns 1 if a packet has been sent to the stack, otherwise 0
965 *
966 * processes an rx descriptor by iommu-unmapping the data buffer and passing
967 * the packet up to the stack. This function is called in softirq
968 * context, e.g. either bottom half from interrupt or NAPI polling context
969 */
970 static int
971 spider_net_decode_one_descr(struct spider_net_card *card, int napi)
972 {
973 struct spider_net_descr_chain *chain = &card->rx_chain;
974 struct spider_net_descr *descr = chain->tail;
975 int status;
976 int result;
978 status = spider_net_get_descr_status(descr);
980 if (status == SPIDER_NET_DESCR_CARDOWNED) {
981 /* nothing in the descriptor yet */
982 result=0;
983 goto out;
984 }
986 if (status == SPIDER_NET_DESCR_NOT_IN_USE) {
987 /* not initialized yet, the ring must be empty */
988 spider_net_refill_rx_chain(card);
989 spider_net_enable_rxdmac(card);
990 result=0;
991 goto out;
992 }
994 /* descriptor definitively used -- move on tail */
995 chain->tail = descr->next;
997 result = 0;
998 if ( (status == SPIDER_NET_DESCR_RESPONSE_ERROR) ||
999 (status == SPIDER_NET_DESCR_PROTECTION_ERROR) ||
1000 (status == SPIDER_NET_DESCR_FORCE_END) ) {
1001 if (netif_msg_rx_err(card))
1002 pr_err("%s: dropping RX descriptor with state %d\n",
1003 card->netdev->name, status);
1004 card->netdev_stats.rx_dropped++;
1005 pci_unmap_single(card->pdev, descr->buf_addr,
1006 SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
1007 dev_kfree_skb_irq(descr->skb);
1008 goto refill;
1011 if ( (status != SPIDER_NET_DESCR_COMPLETE) &&
1012 (status != SPIDER_NET_DESCR_FRAME_END) ) {
1013 if (netif_msg_rx_err(card))
1014 pr_err("%s: RX descriptor with state %d\n",
1015 card->netdev->name, status);
1016 goto refill;
1019 /* ok, we've got a packet in descr */
1020 result = spider_net_pass_skb_up(descr, card, napi);
1021 refill:
1022 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
1023 /* change the descriptor state: */
1024 if (!napi)
1025 spider_net_refill_rx_chain(card);
1026 out:
1027 return result;
1030 /**
1031 * spider_net_poll - NAPI poll function called by the stack to return packets
1032 * @netdev: interface device structure
1033 * @budget: number of packets we can pass to the stack at most
1035 * returns 0 if no more packets available to the driver/stack. Returns 1,
1036 * if the quota is exceeded, but the driver has still packets.
1038 * spider_net_poll returns all packets from the rx descriptors to the stack
1039 * (using netif_receive_skb). If all/enough packets are up, the driver
1040 * reenables interrupts and returns 0. If not, 1 is returned.
1041 */
1042 static int
1043 spider_net_poll(struct net_device *netdev, int *budget)
1045 struct spider_net_card *card = netdev_priv(netdev);
1046 int packets_to_do, packets_done = 0;
1047 int no_more_packets = 0;
1049 packets_to_do = min(*budget, netdev->quota);
1051 while (packets_to_do) {
1052 if (spider_net_decode_one_descr(card, 1)) {
1053 packets_done++;
1054 packets_to_do--;
1055 } else {
1056 /* no more packets for the stack */
1057 no_more_packets = 1;
1058 break;
1062 netdev->quota -= packets_done;
1063 *budget -= packets_done;
1064 spider_net_refill_rx_chain(card);
1066 /* if all packets are in the stack, enable interrupts and return 0 */
1067 /* if not, return 1 */
1068 if (no_more_packets) {
1069 netif_rx_complete(netdev);
1070 spider_net_rx_irq_on(card);
1071 return 0;
1074 return 1;
1077 /**
1078 * spider_net_vlan_rx_reg - initializes VLAN structures in the driver and card
1079 * @netdev: interface device structure
1080 * @grp: vlan_group structure that is registered (NULL on destroying interface)
1081 */
1082 static void
1083 spider_net_vlan_rx_reg(struct net_device *netdev, struct vlan_group *grp)
1085 /* further enhancement... yet to do */
1086 return;
1089 /**
1090 * spider_net_vlan_rx_add - adds VLAN id to the card filter
1091 * @netdev: interface device structure
1092 * @vid: VLAN id to add
1093 */
1094 static void
1095 spider_net_vlan_rx_add(struct net_device *netdev, uint16_t vid)
1097 /* further enhancement... yet to do */
1098 /* add vid to card's VLAN filter table */
1099 return;
1102 /**
1103 * spider_net_vlan_rx_kill - removes VLAN id to the card filter
1104 * @netdev: interface device structure
1105 * @vid: VLAN id to remove
1106 */
1107 static void
1108 spider_net_vlan_rx_kill(struct net_device *netdev, uint16_t vid)
1110 /* further enhancement... yet to do */
1111 /* remove vid from card's VLAN filter table */
1114 /**
1115 * spider_net_get_stats - get interface statistics
1116 * @netdev: interface device structure
1118 * returns the interface statistics residing in the spider_net_card struct
1119 */
1120 static struct net_device_stats *
1121 spider_net_get_stats(struct net_device *netdev)
1123 struct spider_net_card *card = netdev_priv(netdev);
1124 struct net_device_stats *stats = &card->netdev_stats;
1125 return stats;
1128 /**
1129 * spider_net_change_mtu - changes the MTU of an interface
1130 * @netdev: interface device structure
1131 * @new_mtu: new MTU value
1133 * returns 0 on success, <0 on failure
1134 */
1135 static int
1136 spider_net_change_mtu(struct net_device *netdev, int new_mtu)
1138 /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
1139 * and mtu is outbound only anyway */
1140 if ( (new_mtu < SPIDER_NET_MIN_MTU ) ||
1141 (new_mtu > SPIDER_NET_MAX_MTU) )
1142 return -EINVAL;
1143 netdev->mtu = new_mtu;
1144 return 0;
1147 /**
1148 * spider_net_set_mac - sets the MAC of an interface
1149 * @netdev: interface device structure
1150 * @ptr: pointer to new MAC address
1152 * Returns 0 on success, <0 on failure. Currently, we don't support this
1153 * and will always return EOPNOTSUPP.
1154 */
1155 static int
1156 spider_net_set_mac(struct net_device *netdev, void *p)
1158 struct spider_net_card *card = netdev_priv(netdev);
1159 u32 macl, macu, regvalue;
1160 struct sockaddr *addr = p;
1162 if (!is_valid_ether_addr(addr->sa_data))
1163 return -EADDRNOTAVAIL;
1165 /* switch off GMACTPE and GMACRPE */
1166 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1167 regvalue &= ~((1 << 5) | (1 << 6));
1168 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1170 /* write mac */
1171 macu = (addr->sa_data[0]<<24) + (addr->sa_data[1]<<16) +
1172 (addr->sa_data[2]<<8) + (addr->sa_data[3]);
1173 macl = (addr->sa_data[4]<<8) + (addr->sa_data[5]);
1174 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACU, macu);
1175 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACL, macl);
1177 /* switch GMACTPE and GMACRPE back on */
1178 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1179 regvalue |= ((1 << 5) | (1 << 6));
1180 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1182 spider_net_set_promisc(card);
1184 /* look up, whether we have been successful */
1185 if (spider_net_get_mac_address(netdev))
1186 return -EADDRNOTAVAIL;
1187 if (memcmp(netdev->dev_addr,addr->sa_data,netdev->addr_len))
1188 return -EADDRNOTAVAIL;
1190 return 0;
1193 /**
1194 * spider_net_handle_rxram_full - cleans up RX ring upon RX RAM full interrupt
1195 * @card: card structure
1197 * spider_net_handle_rxram_full empties the RX ring so that spider can put
1198 * more packets in it and empty its RX RAM. This is called in bottom half
1199 * context
1200 */
1201 static void
1202 spider_net_handle_rxram_full(struct spider_net_card *card)
1204 while (spider_net_decode_one_descr(card, 0))
1206 spider_net_enable_rxchtails(card);
1207 spider_net_enable_rxdmac(card);
1208 netif_rx_schedule(card->netdev);
1211 /**
1212 * spider_net_handle_error_irq - handles errors raised by an interrupt
1213 * @card: card structure
1214 * @status_reg: interrupt status register 0 (GHIINT0STS)
1216 * spider_net_handle_error_irq treats or ignores all error conditions
1217 * found when an interrupt is presented
1218 */
1219 static void
1220 spider_net_handle_error_irq(struct spider_net_card *card, u32 status_reg)
1222 u32 error_reg1, error_reg2;
1223 u32 i;
1224 int show_error = 1;
1226 error_reg1 = spider_net_read_reg(card, SPIDER_NET_GHIINT1STS);
1227 error_reg2 = spider_net_read_reg(card, SPIDER_NET_GHIINT2STS);
1229 /* check GHIINT0STS ************************************/
1230 if (status_reg)
1231 for (i = 0; i < 32; i++)
1232 if (status_reg & (1<<i))
1233 switch (i)
1235 /* let error_reg1 and error_reg2 evaluation decide, what to do
1236 case SPIDER_NET_PHYINT:
1237 case SPIDER_NET_GMAC2INT:
1238 case SPIDER_NET_GMAC1INT:
1239 case SPIDER_NET_GIPSINT:
1240 case SPIDER_NET_GFIFOINT:
1241 case SPIDER_NET_DMACINT:
1242 case SPIDER_NET_GSYSINT:
1243 break; */
1245 case SPIDER_NET_GPWOPCMPINT:
1246 /* PHY write operation completed */
1247 show_error = 0;
1248 break;
1249 case SPIDER_NET_GPROPCMPINT:
1250 /* PHY read operation completed */
1251 /* we don't use semaphores, as we poll for the completion
1252 * of the read operation in spider_net_read_phy. Should take
1253 * about 50 us */
1254 show_error = 0;
1255 break;
1256 case SPIDER_NET_GPWFFINT:
1257 /* PHY command queue full */
1258 if (netif_msg_intr(card))
1259 pr_err("PHY write queue full\n");
1260 show_error = 0;
1261 break;
1263 /* case SPIDER_NET_GRMDADRINT: not used. print a message */
1264 /* case SPIDER_NET_GRMARPINT: not used. print a message */
1265 /* case SPIDER_NET_GRMMPINT: not used. print a message */
1267 case SPIDER_NET_GDTDEN0INT:
1268 /* someone has set TX_DMA_EN to 0 */
1269 show_error = 0;
1270 break;
1272 case SPIDER_NET_GDDDEN0INT: /* fallthrough */
1273 case SPIDER_NET_GDCDEN0INT: /* fallthrough */
1274 case SPIDER_NET_GDBDEN0INT: /* fallthrough */
1275 case SPIDER_NET_GDADEN0INT:
1276 /* someone has set RX_DMA_EN to 0 */
1277 show_error = 0;
1278 break;
1280 /* RX interrupts */
1281 case SPIDER_NET_GDDFDCINT:
1282 case SPIDER_NET_GDCFDCINT:
1283 case SPIDER_NET_GDBFDCINT:
1284 case SPIDER_NET_GDAFDCINT:
1285 /* case SPIDER_NET_GDNMINT: not used. print a message */
1286 /* case SPIDER_NET_GCNMINT: not used. print a message */
1287 /* case SPIDER_NET_GBNMINT: not used. print a message */
1288 /* case SPIDER_NET_GANMINT: not used. print a message */
1289 /* case SPIDER_NET_GRFNMINT: not used. print a message */
1290 show_error = 0;
1291 break;
1293 /* TX interrupts */
1294 case SPIDER_NET_GDTFDCINT:
1295 show_error = 0;
1296 break;
1297 case SPIDER_NET_GTTEDINT:
1298 show_error = 0;
1299 break;
1300 case SPIDER_NET_GDTDCEINT:
1301 /* chain end. If a descriptor should be sent, kick off
1302 * tx dma
1303 if (card->tx_chain.tail == card->tx_chain.head)
1304 spider_net_kick_tx_dma(card);
1305 show_error = 0; */
1306 break;
1308 /* case SPIDER_NET_G1TMCNTINT: not used. print a message */
1309 /* case SPIDER_NET_GFREECNTINT: not used. print a message */
1312 /* check GHIINT1STS ************************************/
1313 if (error_reg1)
1314 for (i = 0; i < 32; i++)
1315 if (error_reg1 & (1<<i))
1316 switch (i)
1318 case SPIDER_NET_GTMFLLINT:
1319 if (netif_msg_intr(card) && net_ratelimit())
1320 pr_err("Spider TX RAM full\n");
1321 show_error = 0;
1322 break;
1323 case SPIDER_NET_GRFDFLLINT: /* fallthrough */
1324 case SPIDER_NET_GRFCFLLINT: /* fallthrough */
1325 case SPIDER_NET_GRFBFLLINT: /* fallthrough */
1326 case SPIDER_NET_GRFAFLLINT: /* fallthrough */
1327 case SPIDER_NET_GRMFLLINT:
1328 if (netif_msg_intr(card) && net_ratelimit())
1329 pr_debug("Spider RX RAM full, incoming packets "
1330 "might be discarded!\n");
1331 spider_net_rx_irq_off(card);
1332 tasklet_schedule(&card->rxram_full_tl);
1333 show_error = 0;
1334 break;
1336 /* case SPIDER_NET_GTMSHTINT: problem, print a message */
1337 case SPIDER_NET_GDTINVDINT:
1338 /* allrighty. tx from previous descr ok */
1339 show_error = 0;
1340 break;
1342 /* chain end */
1343 case SPIDER_NET_GDDDCEINT: /* fallthrough */
1344 case SPIDER_NET_GDCDCEINT: /* fallthrough */
1345 case SPIDER_NET_GDBDCEINT: /* fallthrough */
1346 case SPIDER_NET_GDADCEINT:
1347 if (netif_msg_intr(card))
1348 pr_err("got descriptor chain end interrupt, "
1349 "restarting DMAC %c.\n",
1350 'D'+i-SPIDER_NET_GDDDCEINT);
1351 spider_net_refill_rx_chain(card);
1352 spider_net_enable_rxdmac(card);
1353 show_error = 0;
1354 break;
1356 /* invalid descriptor */
1357 case SPIDER_NET_GDDINVDINT: /* fallthrough */
1358 case SPIDER_NET_GDCINVDINT: /* fallthrough */
1359 case SPIDER_NET_GDBINVDINT: /* fallthrough */
1360 case SPIDER_NET_GDAINVDINT:
1361 /* could happen when rx chain is full */
1362 spider_net_refill_rx_chain(card);
1363 spider_net_enable_rxdmac(card);
1364 show_error = 0;
1365 break;
1367 /* case SPIDER_NET_GDTRSERINT: problem, print a message */
1368 /* case SPIDER_NET_GDDRSERINT: problem, print a message */
1369 /* case SPIDER_NET_GDCRSERINT: problem, print a message */
1370 /* case SPIDER_NET_GDBRSERINT: problem, print a message */
1371 /* case SPIDER_NET_GDARSERINT: problem, print a message */
1372 /* case SPIDER_NET_GDSERINT: problem, print a message */
1373 /* case SPIDER_NET_GDTPTERINT: problem, print a message */
1374 /* case SPIDER_NET_GDDPTERINT: problem, print a message */
1375 /* case SPIDER_NET_GDCPTERINT: problem, print a message */
1376 /* case SPIDER_NET_GDBPTERINT: problem, print a message */
1377 /* case SPIDER_NET_GDAPTERINT: problem, print a message */
1378 default:
1379 show_error = 1;
1380 break;
1383 /* check GHIINT2STS ************************************/
1384 if (error_reg2)
1385 for (i = 0; i < 32; i++)
1386 if (error_reg2 & (1<<i))
1387 switch (i)
1389 /* there is nothing we can (want to) do at this time. Log a
1390 * message, we can switch on and off the specific values later on
1391 case SPIDER_NET_GPROPERINT:
1392 case SPIDER_NET_GMCTCRSNGINT:
1393 case SPIDER_NET_GMCTLCOLINT:
1394 case SPIDER_NET_GMCTTMOTINT:
1395 case SPIDER_NET_GMCRCAERINT:
1396 case SPIDER_NET_GMCRCALERINT:
1397 case SPIDER_NET_GMCRALNERINT:
1398 case SPIDER_NET_GMCROVRINT:
1399 case SPIDER_NET_GMCRRNTINT:
1400 case SPIDER_NET_GMCRRXERINT:
1401 case SPIDER_NET_GTITCSERINT:
1402 case SPIDER_NET_GTIFMTERINT:
1403 case SPIDER_NET_GTIPKTRVKINT:
1404 case SPIDER_NET_GTISPINGINT:
1405 case SPIDER_NET_GTISADNGINT:
1406 case SPIDER_NET_GTISPDNGINT:
1407 case SPIDER_NET_GRIFMTERINT:
1408 case SPIDER_NET_GRIPKTRVKINT:
1409 case SPIDER_NET_GRISPINGINT:
1410 case SPIDER_NET_GRISADNGINT:
1411 case SPIDER_NET_GRISPDNGINT:
1412 break;
1413 */
1414 default:
1415 break;
1418 if ((show_error) && (netif_msg_intr(card)))
1419 pr_err("Got error interrupt, GHIINT0STS = 0x%08x, "
1420 "GHIINT1STS = 0x%08x, GHIINT2STS = 0x%08x\n",
1421 status_reg, error_reg1, error_reg2);
1423 /* clear interrupt sources */
1424 spider_net_write_reg(card, SPIDER_NET_GHIINT1STS, error_reg1);
1425 spider_net_write_reg(card, SPIDER_NET_GHIINT2STS, error_reg2);
1428 /**
1429 * spider_net_interrupt - interrupt handler for spider_net
1430 * @irq: interupt number
1431 * @ptr: pointer to net_device
1432 * @regs: PU registers
1434 * returns IRQ_HANDLED, if interrupt was for driver, or IRQ_NONE, if no
1435 * interrupt found raised by card.
1437 * This is the interrupt handler, that turns off
1438 * interrupts for this device and makes the stack poll the driver
1439 */
1440 static irqreturn_t
1441 spider_net_interrupt(int irq, void *ptr, struct pt_regs *regs)
1443 struct net_device *netdev = ptr;
1444 struct spider_net_card *card = netdev_priv(netdev);
1445 u32 status_reg;
1447 status_reg = spider_net_read_reg(card, SPIDER_NET_GHIINT0STS);
1449 if (!status_reg)
1450 return IRQ_NONE;
1452 if (status_reg & SPIDER_NET_RXINT ) {
1453 spider_net_rx_irq_off(card);
1454 netif_rx_schedule(netdev);
1457 if (status_reg & SPIDER_NET_ERRINT )
1458 spider_net_handle_error_irq(card, status_reg);
1460 /* clear interrupt sources */
1461 spider_net_write_reg(card, SPIDER_NET_GHIINT0STS, status_reg);
1463 return IRQ_HANDLED;
1466 #ifdef CONFIG_NET_POLL_CONTROLLER
1467 /**
1468 * spider_net_poll_controller - artificial interrupt for netconsole etc.
1469 * @netdev: interface device structure
1471 * see Documentation/networking/netconsole.txt
1472 */
1473 static void
1474 spider_net_poll_controller(struct net_device *netdev)
1476 disable_irq(netdev->irq);
1477 spider_net_interrupt(netdev->irq, netdev, NULL);
1478 enable_irq(netdev->irq);
1480 #endif /* CONFIG_NET_POLL_CONTROLLER */
1482 /**
1483 * spider_net_init_card - initializes the card
1484 * @card: card structure
1486 * spider_net_init_card initializes the card so that other registers can
1487 * be used
1488 */
1489 static void
1490 spider_net_init_card(struct spider_net_card *card)
1492 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1493 SPIDER_NET_CKRCTRL_STOP_VALUE);
1495 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1496 SPIDER_NET_CKRCTRL_RUN_VALUE);
1499 /**
1500 * spider_net_enable_card - enables the card by setting all kinds of regs
1501 * @card: card structure
1503 * spider_net_enable_card sets a lot of SMMIO registers to enable the device
1504 */
1505 static void
1506 spider_net_enable_card(struct spider_net_card *card)
1508 int i;
1509 /* the following array consists of (register),(value) pairs
1510 * that are set in this function. A register of 0 ends the list */
1511 u32 regs[][2] = {
1512 { SPIDER_NET_GRESUMINTNUM, 0 },
1513 { SPIDER_NET_GREINTNUM, 0 },
1515 /* set interrupt frame number registers */
1516 /* clear the single DMA engine registers first */
1517 { SPIDER_NET_GFAFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1518 { SPIDER_NET_GFBFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1519 { SPIDER_NET_GFCFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1520 { SPIDER_NET_GFDFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1521 /* then set, what we really need */
1522 { SPIDER_NET_GFFRMNUM, SPIDER_NET_FRAMENUM_VALUE },
1524 /* timer counter registers and stuff */
1525 { SPIDER_NET_GFREECNNUM, 0 },
1526 { SPIDER_NET_GONETIMENUM, 0 },
1527 { SPIDER_NET_GTOUTFRMNUM, 0 },
1529 /* RX mode setting */
1530 { SPIDER_NET_GRXMDSET, SPIDER_NET_RXMODE_VALUE },
1531 /* TX mode setting */
1532 { SPIDER_NET_GTXMDSET, SPIDER_NET_TXMODE_VALUE },
1533 /* IPSEC mode setting */
1534 { SPIDER_NET_GIPSECINIT, SPIDER_NET_IPSECINIT_VALUE },
1536 { SPIDER_NET_GFTRESTRT, SPIDER_NET_RESTART_VALUE },
1538 { SPIDER_NET_GMRWOLCTRL, 0 },
1539 { SPIDER_NET_GTESTMD, 0x10000000 },
1540 { SPIDER_NET_GTTQMSK, 0x00400040 },
1542 { SPIDER_NET_GMACINTEN, 0 },
1544 /* flow control stuff */
1545 { SPIDER_NET_GMACAPAUSE, SPIDER_NET_MACAPAUSE_VALUE },
1546 { SPIDER_NET_GMACTXPAUSE, SPIDER_NET_TXPAUSE_VALUE },
1548 { SPIDER_NET_GMACBSTLMT, SPIDER_NET_BURSTLMT_VALUE },
1549 { 0, 0}
1550 };
1552 i = 0;
1553 while (regs[i][0]) {
1554 spider_net_write_reg(card, regs[i][0], regs[i][1]);
1555 i++;
1558 /* clear unicast filter table entries 1 to 14 */
1559 for (i = 1; i <= 14; i++) {
1560 spider_net_write_reg(card,
1561 SPIDER_NET_GMRUAFILnR + i * 8,
1562 0x00080000);
1563 spider_net_write_reg(card,
1564 SPIDER_NET_GMRUAFILnR + i * 8 + 4,
1565 0x00000000);
1568 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R, 0x08080000);
1570 spider_net_write_reg(card, SPIDER_NET_ECMODE, SPIDER_NET_ECMODE_VALUE);
1572 /* set chain tail adress for RX chains and
1573 * enable DMA */
1574 spider_net_enable_rxchtails(card);
1575 spider_net_enable_rxdmac(card);
1577 spider_net_write_reg(card, SPIDER_NET_GRXDMAEN, SPIDER_NET_WOL_VALUE);
1579 spider_net_write_reg(card, SPIDER_NET_GMACLENLMT,
1580 SPIDER_NET_LENLMT_VALUE);
1581 spider_net_write_reg(card, SPIDER_NET_GMACMODE,
1582 SPIDER_NET_MACMODE_VALUE);
1583 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD,
1584 SPIDER_NET_OPMODE_VALUE);
1586 /* set interrupt mask registers */
1587 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK,
1588 SPIDER_NET_INT0_MASK_VALUE);
1589 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK,
1590 SPIDER_NET_INT1_MASK_VALUE);
1591 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK,
1592 SPIDER_NET_INT2_MASK_VALUE);
1594 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
1595 SPIDER_NET_GDTDCEIDIS);
1598 /**
1599 * spider_net_open - called upon ifonfig up
1600 * @netdev: interface device structure
1602 * returns 0 on success, <0 on failure
1604 * spider_net_open allocates all the descriptors and memory needed for
1605 * operation, sets up multicast list and enables interrupts
1606 */
1607 int
1608 spider_net_open(struct net_device *netdev)
1610 struct spider_net_card *card = netdev_priv(netdev);
1611 int result;
1613 result = -ENOMEM;
1614 if (spider_net_init_chain(card, &card->tx_chain, card->descr,
1615 PCI_DMA_TODEVICE, card->tx_desc))
1616 goto alloc_tx_failed;
1617 if (spider_net_init_chain(card, &card->rx_chain,
1618 card->descr + card->rx_desc,
1619 PCI_DMA_FROMDEVICE, card->rx_desc))
1620 goto alloc_rx_failed;
1622 /* allocate rx skbs */
1623 if (spider_net_alloc_rx_skbs(card))
1624 goto alloc_skbs_failed;
1626 spider_net_set_multi(netdev);
1628 /* further enhancement: setup hw vlan, if needed */
1630 result = -EBUSY;
1631 if (request_irq(netdev->irq, spider_net_interrupt,
1632 IRQF_SHARED, netdev->name, netdev))
1633 goto register_int_failed;
1635 spider_net_enable_card(card);
1637 netif_start_queue(netdev);
1638 netif_carrier_on(netdev);
1639 netif_poll_enable(netdev);
1641 return 0;
1643 register_int_failed:
1644 spider_net_free_rx_chain_contents(card);
1645 alloc_skbs_failed:
1646 spider_net_free_chain(card, &card->rx_chain);
1647 alloc_rx_failed:
1648 spider_net_free_chain(card, &card->tx_chain);
1649 alloc_tx_failed:
1650 return result;
1653 /**
1654 * spider_net_setup_phy - setup PHY
1655 * @card: card structure
1657 * returns 0 on success, <0 on failure
1659 * spider_net_setup_phy is used as part of spider_net_probe. Sets
1660 * the PHY to 1000 Mbps
1661 **/
1662 static int
1663 spider_net_setup_phy(struct spider_net_card *card)
1665 struct mii_phy *phy = &card->phy;
1667 spider_net_write_reg(card, SPIDER_NET_GDTDMASEL,
1668 SPIDER_NET_DMASEL_VALUE);
1669 spider_net_write_reg(card, SPIDER_NET_GPCCTRL,
1670 SPIDER_NET_PHY_CTRL_VALUE);
1671 phy->mii_id = 1;
1672 phy->dev = card->netdev;
1673 phy->mdio_read = spider_net_read_phy;
1674 phy->mdio_write = spider_net_write_phy;
1676 mii_phy_probe(phy, phy->mii_id);
1678 if (phy->def->ops->setup_forced)
1679 phy->def->ops->setup_forced(phy, SPEED_1000, DUPLEX_FULL);
1681 phy->def->ops->enable_fiber(phy);
1683 phy->def->ops->read_link(phy);
1684 pr_info("Found %s with %i Mbps, %s-duplex.\n", phy->def->name,
1685 phy->speed, phy->duplex==1 ? "Full" : "Half");
1687 return 0;
1690 /**
1691 * spider_net_download_firmware - loads firmware into the adapter
1692 * @card: card structure
1693 * @firmware_ptr: pointer to firmware data
1695 * spider_net_download_firmware loads the firmware data into the
1696 * adapter. It assumes the length etc. to be allright.
1697 */
1698 static int
1699 spider_net_download_firmware(struct spider_net_card *card,
1700 u8 *firmware_ptr)
1702 int sequencer, i;
1703 u32 *fw_ptr = (u32 *)firmware_ptr;
1705 /* stop sequencers */
1706 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1707 SPIDER_NET_STOP_SEQ_VALUE);
1709 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1710 sequencer++) {
1711 spider_net_write_reg(card,
1712 SPIDER_NET_GSnPRGADR + sequencer * 8, 0);
1713 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
1714 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1715 sequencer * 8, *fw_ptr);
1716 fw_ptr++;
1720 if (spider_net_read_reg(card, SPIDER_NET_GSINIT))
1721 return -EIO;
1723 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1724 SPIDER_NET_RUN_SEQ_VALUE);
1726 return 0;
1729 /**
1730 * spider_net_init_firmware - reads in firmware parts
1731 * @card: card structure
1733 * Returns 0 on success, <0 on failure
1735 * spider_net_init_firmware opens the sequencer firmware and does some basic
1736 * checks. This function opens and releases the firmware structure. A call
1737 * to download the firmware is performed before the release.
1739 * Firmware format
1740 * ===============
1741 * spider_fw.bin is expected to be a file containing 6*1024*4 bytes, 4k being
1742 * the program for each sequencer. Use the command
1743 * tail -q -n +2 Seq_code1_0x088.txt Seq_code2_0x090.txt \
1744 * Seq_code3_0x098.txt Seq_code4_0x0A0.txt Seq_code5_0x0A8.txt \
1745 * Seq_code6_0x0B0.txt | xxd -r -p -c4 > spider_fw.bin
1747 * to generate spider_fw.bin, if you have sequencer programs with something
1748 * like the following contents for each sequencer:
1749 * <ONE LINE COMMENT>
1750 * <FIRST 4-BYTES-WORD FOR SEQUENCER>
1751 * <SECOND 4-BYTES-WORD FOR SEQUENCER>
1752 * ...
1753 * <1024th 4-BYTES-WORD FOR SEQUENCER>
1754 */
1755 static int
1756 spider_net_init_firmware(struct spider_net_card *card)
1758 struct firmware *firmware = NULL;
1759 struct device_node *dn;
1760 u8 *fw_prop = NULL;
1761 int err = -ENOENT;
1762 int fw_size;
1764 if (request_firmware((const struct firmware **)&firmware,
1765 SPIDER_NET_FIRMWARE_NAME, &card->pdev->dev) == 0) {
1766 if ( (firmware->size != SPIDER_NET_FIRMWARE_LEN) &&
1767 netif_msg_probe(card) ) {
1768 pr_err("Incorrect size of spidernet firmware in " \
1769 "filesystem. Looking in host firmware...\n");
1770 goto try_host_fw;
1772 err = spider_net_download_firmware(card, firmware->data);
1774 release_firmware(firmware);
1775 if (err)
1776 goto try_host_fw;
1778 goto done;
1781 try_host_fw:
1782 dn = pci_device_to_OF_node(card->pdev);
1783 if (!dn)
1784 goto out_err;
1786 fw_prop = (u8 *)get_property(dn, "firmware", &fw_size);
1787 if (!fw_prop)
1788 goto out_err;
1790 if ( (fw_size != SPIDER_NET_FIRMWARE_LEN) &&
1791 netif_msg_probe(card) ) {
1792 pr_err("Incorrect size of spidernet firmware in " \
1793 "host firmware\n");
1794 goto done;
1797 err = spider_net_download_firmware(card, fw_prop);
1799 done:
1800 return err;
1801 out_err:
1802 if (netif_msg_probe(card))
1803 pr_err("Couldn't find spidernet firmware in filesystem " \
1804 "or host firmware\n");
1805 return err;
1808 /**
1809 * spider_net_workaround_rxramfull - work around firmware bug
1810 * @card: card structure
1812 * no return value
1813 **/
1814 static void
1815 spider_net_workaround_rxramfull(struct spider_net_card *card)
1817 int i, sequencer = 0;
1819 /* cancel reset */
1820 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1821 SPIDER_NET_CKRCTRL_RUN_VALUE);
1823 /* empty sequencer data */
1824 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1825 sequencer++) {
1826 spider_net_write_reg(card, SPIDER_NET_GSnPRGADR +
1827 sequencer * 8, 0x0);
1828 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
1829 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1830 sequencer * 8, 0x0);
1834 /* set sequencer operation */
1835 spider_net_write_reg(card, SPIDER_NET_GSINIT, 0x000000fe);
1837 /* reset */
1838 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1839 SPIDER_NET_CKRCTRL_STOP_VALUE);
1842 /**
1843 * spider_net_stop - called upon ifconfig down
1844 * @netdev: interface device structure
1846 * always returns 0
1847 */
1848 int
1849 spider_net_stop(struct net_device *netdev)
1851 struct spider_net_card *card = netdev_priv(netdev);
1853 tasklet_kill(&card->rxram_full_tl);
1854 netif_poll_disable(netdev);
1855 netif_carrier_off(netdev);
1856 netif_stop_queue(netdev);
1857 del_timer_sync(&card->tx_timer);
1859 /* disable/mask all interrupts */
1860 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, 0);
1861 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK, 0);
1862 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK, 0);
1864 /* free_irq(netdev->irq, netdev);*/
1865 free_irq(to_pci_dev(netdev->class_dev.dev)->irq, netdev);
1867 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
1868 SPIDER_NET_DMA_TX_FEND_VALUE);
1870 /* turn off DMA, force end */
1871 spider_net_disable_rxdmac(card);
1873 /* release chains */
1874 if (spin_trylock(&card->tx_chain.lock)) {
1875 spider_net_release_tx_chain(card, 1);
1876 spin_unlock(&card->tx_chain.lock);
1879 spider_net_free_chain(card, &card->tx_chain);
1880 spider_net_free_chain(card, &card->rx_chain);
1882 return 0;
1885 /**
1886 * spider_net_tx_timeout_task - task scheduled by the watchdog timeout
1887 * function (to be called not under interrupt status)
1888 * @data: data, is interface device structure
1890 * called as task when tx hangs, resets interface (if interface is up)
1891 */
1892 static void
1893 spider_net_tx_timeout_task(void *data)
1895 struct net_device *netdev = data;
1896 struct spider_net_card *card = netdev_priv(netdev);
1898 if (!(netdev->flags & IFF_UP))
1899 goto out;
1901 netif_device_detach(netdev);
1902 spider_net_stop(netdev);
1904 spider_net_workaround_rxramfull(card);
1905 spider_net_init_card(card);
1907 if (spider_net_setup_phy(card))
1908 goto out;
1909 if (spider_net_init_firmware(card))
1910 goto out;
1912 spider_net_open(netdev);
1913 spider_net_kick_tx_dma(card);
1914 netif_device_attach(netdev);
1916 out:
1917 atomic_dec(&card->tx_timeout_task_counter);
1920 /**
1921 * spider_net_tx_timeout - called when the tx timeout watchdog kicks in.
1922 * @netdev: interface device structure
1924 * called, if tx hangs. Schedules a task that resets the interface
1925 */
1926 static void
1927 spider_net_tx_timeout(struct net_device *netdev)
1929 struct spider_net_card *card;
1931 card = netdev_priv(netdev);
1932 atomic_inc(&card->tx_timeout_task_counter);
1933 if (netdev->flags & IFF_UP)
1934 schedule_work(&card->tx_timeout_task);
1935 else
1936 atomic_dec(&card->tx_timeout_task_counter);
1939 /**
1940 * spider_net_setup_netdev_ops - initialization of net_device operations
1941 * @netdev: net_device structure
1943 * fills out function pointers in the net_device structure
1944 */
1945 static void
1946 spider_net_setup_netdev_ops(struct net_device *netdev)
1948 netdev->open = &spider_net_open;
1949 netdev->stop = &spider_net_stop;
1950 netdev->hard_start_xmit = &spider_net_xmit;
1951 netdev->get_stats = &spider_net_get_stats;
1952 netdev->set_multicast_list = &spider_net_set_multi;
1953 netdev->set_mac_address = &spider_net_set_mac;
1954 netdev->change_mtu = &spider_net_change_mtu;
1955 netdev->do_ioctl = &spider_net_do_ioctl;
1956 /* tx watchdog */
1957 netdev->tx_timeout = &spider_net_tx_timeout;
1958 netdev->watchdog_timeo = SPIDER_NET_WATCHDOG_TIMEOUT;
1959 /* NAPI */
1960 netdev->poll = &spider_net_poll;
1961 netdev->weight = SPIDER_NET_NAPI_WEIGHT;
1962 /* HW VLAN */
1963 netdev->vlan_rx_register = &spider_net_vlan_rx_reg;
1964 netdev->vlan_rx_add_vid = &spider_net_vlan_rx_add;
1965 netdev->vlan_rx_kill_vid = &spider_net_vlan_rx_kill;
1966 #ifdef CONFIG_NET_POLL_CONTROLLER
1967 /* poll controller */
1968 netdev->poll_controller = &spider_net_poll_controller;
1969 #endif /* CONFIG_NET_POLL_CONTROLLER */
1970 /* ethtool ops */
1971 netdev->ethtool_ops = &spider_net_ethtool_ops;
1974 /**
1975 * spider_net_setup_netdev - initialization of net_device
1976 * @card: card structure
1978 * Returns 0 on success or <0 on failure
1980 * spider_net_setup_netdev initializes the net_device structure
1981 **/
1982 static int
1983 spider_net_setup_netdev(struct spider_net_card *card)
1985 int result;
1986 struct net_device *netdev = card->netdev;
1987 struct device_node *dn;
1988 struct sockaddr addr;
1989 u8 *mac;
1991 SET_MODULE_OWNER(netdev);
1992 SET_NETDEV_DEV(netdev, &card->pdev->dev);
1994 pci_set_drvdata(card->pdev, netdev);
1996 card->rxram_full_tl.data = (unsigned long) card;
1997 card->rxram_full_tl.func =
1998 (void (*)(unsigned long)) spider_net_handle_rxram_full;
1999 init_timer(&card->tx_timer);
2000 card->tx_timer.function =
2001 (void (*)(unsigned long)) spider_net_cleanup_tx_ring;
2002 card->tx_timer.data = (unsigned long) card;
2003 netdev->irq = card->pdev->irq;
2005 card->options.rx_csum = SPIDER_NET_RX_CSUM_DEFAULT;
2007 card->tx_desc = tx_descriptors;
2008 card->rx_desc = rx_descriptors;
2010 spider_net_setup_netdev_ops(netdev);
2012 netdev->features = NETIF_F_HW_CSUM | NETIF_F_LLTX;
2013 /* some time: NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
2014 * NETIF_F_HW_VLAN_FILTER */
2016 netdev->irq = card->pdev->irq;
2018 dn = pci_device_to_OF_node(card->pdev);
2019 if (!dn)
2020 return -EIO;
2022 mac = (u8 *)get_property(dn, "local-mac-address", NULL);
2023 if (!mac)
2024 return -EIO;
2025 memcpy(addr.sa_data, mac, ETH_ALEN);
2027 result = spider_net_set_mac(netdev, &addr);
2028 if ((result) && (netif_msg_probe(card)))
2029 pr_err("Failed to set MAC address: %i\n", result);
2031 result = register_netdev(netdev);
2032 if (result) {
2033 if (netif_msg_probe(card))
2034 pr_err("Couldn't register net_device: %i\n",
2035 result);
2036 return result;
2039 if (netif_msg_probe(card))
2040 pr_info("Initialized device %s.\n", netdev->name);
2042 return 0;
2045 /**
2046 * spider_net_alloc_card - allocates net_device and card structure
2048 * returns the card structure or NULL in case of errors
2050 * the card and net_device structures are linked to each other
2051 */
2052 static struct spider_net_card *
2053 spider_net_alloc_card(void)
2055 struct net_device *netdev;
2056 struct spider_net_card *card;
2057 size_t alloc_size;
2059 alloc_size = sizeof (*card) +
2060 sizeof (struct spider_net_descr) * rx_descriptors +
2061 sizeof (struct spider_net_descr) * tx_descriptors;
2062 netdev = alloc_etherdev(alloc_size);
2063 if (!netdev)
2064 return NULL;
2066 card = netdev_priv(netdev);
2067 card->netdev = netdev;
2068 card->msg_enable = SPIDER_NET_DEFAULT_MSG;
2069 INIT_WORK(&card->tx_timeout_task, spider_net_tx_timeout_task, netdev);
2070 init_waitqueue_head(&card->waitq);
2071 atomic_set(&card->tx_timeout_task_counter, 0);
2073 return card;
2076 /**
2077 * spider_net_undo_pci_setup - releases PCI ressources
2078 * @card: card structure
2080 * spider_net_undo_pci_setup releases the mapped regions
2081 */
2082 static void
2083 spider_net_undo_pci_setup(struct spider_net_card *card)
2085 iounmap(card->regs);
2086 pci_release_regions(card->pdev);
2089 /**
2090 * spider_net_setup_pci_dev - sets up the device in terms of PCI operations
2091 * @card: card structure
2092 * @pdev: PCI device
2094 * Returns the card structure or NULL if any errors occur
2096 * spider_net_setup_pci_dev initializes pdev and together with the
2097 * functions called in spider_net_open configures the device so that
2098 * data can be transferred over it
2099 * The net_device structure is attached to the card structure, if the
2100 * function returns without error.
2101 **/
2102 static struct spider_net_card *
2103 spider_net_setup_pci_dev(struct pci_dev *pdev)
2105 struct spider_net_card *card;
2106 unsigned long mmio_start, mmio_len;
2108 if (pci_enable_device(pdev)) {
2109 pr_err("Couldn't enable PCI device\n");
2110 return NULL;
2113 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
2114 pr_err("Couldn't find proper PCI device base address.\n");
2115 goto out_disable_dev;
2118 if (pci_request_regions(pdev, spider_net_driver_name)) {
2119 pr_err("Couldn't obtain PCI resources, aborting.\n");
2120 goto out_disable_dev;
2123 pci_set_master(pdev);
2125 card = spider_net_alloc_card();
2126 if (!card) {
2127 pr_err("Couldn't allocate net_device structure, "
2128 "aborting.\n");
2129 goto out_release_regions;
2131 card->pdev = pdev;
2133 /* fetch base address and length of first resource */
2134 mmio_start = pci_resource_start(pdev, 0);
2135 mmio_len = pci_resource_len(pdev, 0);
2137 card->netdev->mem_start = mmio_start;
2138 card->netdev->mem_end = mmio_start + mmio_len;
2139 card->regs = ioremap(mmio_start, mmio_len);
2141 if (!card->regs) {
2142 pr_err("Couldn't obtain PCI resources, aborting.\n");
2143 goto out_release_regions;
2146 return card;
2148 out_release_regions:
2149 pci_release_regions(pdev);
2150 out_disable_dev:
2151 pci_disable_device(pdev);
2152 pci_set_drvdata(pdev, NULL);
2153 return NULL;
2156 /**
2157 * spider_net_probe - initialization of a device
2158 * @pdev: PCI device
2159 * @ent: entry in the device id list
2161 * Returns 0 on success, <0 on failure
2163 * spider_net_probe initializes pdev and registers a net_device
2164 * structure for it. After that, the device can be ifconfig'ed up
2165 **/
2166 static int __devinit
2167 spider_net_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2169 int err = -EIO;
2170 struct spider_net_card *card;
2172 card = spider_net_setup_pci_dev(pdev);
2173 if (!card)
2174 goto out;
2176 spider_net_workaround_rxramfull(card);
2177 spider_net_init_card(card);
2179 err = spider_net_setup_phy(card);
2180 if (err)
2181 goto out_undo_pci;
2183 err = spider_net_init_firmware(card);
2184 if (err)
2185 goto out_undo_pci;
2187 err = spider_net_setup_netdev(card);
2188 if (err)
2189 goto out_undo_pci;
2191 return 0;
2193 out_undo_pci:
2194 spider_net_undo_pci_setup(card);
2195 free_netdev(card->netdev);
2196 out:
2197 return err;
2200 /**
2201 * spider_net_remove - removal of a device
2202 * @pdev: PCI device
2204 * Returns 0 on success, <0 on failure
2206 * spider_net_remove is called to remove the device and unregisters the
2207 * net_device
2208 **/
2209 static void __devexit
2210 spider_net_remove(struct pci_dev *pdev)
2212 struct net_device *netdev;
2213 struct spider_net_card *card;
2215 netdev = pci_get_drvdata(pdev);
2216 card = netdev_priv(netdev);
2218 wait_event(card->waitq,
2219 atomic_read(&card->tx_timeout_task_counter) == 0);
2221 unregister_netdev(netdev);
2223 /* switch off card */
2224 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2225 SPIDER_NET_CKRCTRL_STOP_VALUE);
2226 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2227 SPIDER_NET_CKRCTRL_RUN_VALUE);
2229 spider_net_undo_pci_setup(card);
2230 free_netdev(netdev);
2233 static struct pci_driver spider_net_driver = {
2234 .name = spider_net_driver_name,
2235 .id_table = spider_net_pci_tbl,
2236 .probe = spider_net_probe,
2237 .remove = __devexit_p(spider_net_remove)
2238 };
2240 /**
2241 * spider_net_init - init function when the driver is loaded
2243 * spider_net_init registers the device driver
2244 */
2245 static int __init spider_net_init(void)
2247 if (rx_descriptors < SPIDER_NET_RX_DESCRIPTORS_MIN) {
2248 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MIN;
2249 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2251 if (rx_descriptors > SPIDER_NET_RX_DESCRIPTORS_MAX) {
2252 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MAX;
2253 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2255 if (tx_descriptors < SPIDER_NET_TX_DESCRIPTORS_MIN) {
2256 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MIN;
2257 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2259 if (tx_descriptors > SPIDER_NET_TX_DESCRIPTORS_MAX) {
2260 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MAX;
2261 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2264 return pci_register_driver(&spider_net_driver);
2267 /**
2268 * spider_net_cleanup - exit function when driver is unloaded
2270 * spider_net_cleanup unregisters the device driver
2271 */
2272 static void __exit spider_net_cleanup(void)
2274 pci_unregister_driver(&spider_net_driver);
2277 module_init(spider_net_init);
2278 module_exit(spider_net_cleanup);