blob: 86969191c3f7bdba9bc0dbf0870e3e155d8d0a38 [file] [log] [blame]
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001/*
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 */
23
24#include <linux/config.h>
25
26#include <linux/compiler.h>
27#include <linux/crc32.h>
28#include <linux/delay.h>
29#include <linux/etherdevice.h>
30#include <linux/ethtool.h>
31#include <linux/firmware.h>
32#include <linux/if_vlan.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/wait.h>
47#include <linux/workqueue.h>
48#include <asm/bitops.h>
49#include <asm/pci-bridge.h>
50#include <net/checksum.h>
51
52#include "spider_net.h"
53
54MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com> and Jens Osterkamp " \
55 "<Jens.Osterkamp@de.ibm.com>");
56MODULE_DESCRIPTION("Spider Southbridge Gigabit Ethernet driver");
57MODULE_LICENSE("GPL");
58
59static int rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_DEFAULT;
60static int tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_DEFAULT;
61
62module_param(rx_descriptors, int, 0644);
63module_param(tx_descriptors, int, 0644);
64
65MODULE_PARM_DESC(rx_descriptors, "number of descriptors used " \
66 "in rx chains");
67MODULE_PARM_DESC(tx_descriptors, "number of descriptors used " \
68 "in tx chain");
69
70char spider_net_driver_name[] = "spidernet";
71
72static 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};
77
78MODULE_DEVICE_TABLE(pci, spider_net_pci_tbl);
79
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 */
87static u32
88spider_net_read_reg(struct spider_net_card *card, u32 reg)
89{
90 u32 value;
91
92 value = readl(card->regs + reg);
93 value = le32_to_cpu(value);
94
95 return value;
96}
97
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 */
104static void
105spider_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}
110
111/**
Jens Osterkamp543cec52005-09-06 19:30:54 -0700112 * spider_net_write_reg_sync - writes to an SMMIO register of a card
113 * @card: device structure
114 * @reg: register to write to
115 * @value: value to write into the specified SMMIO register
116 *
117 * Unlike spider_net_write_reg, this will also make sure the
118 * data arrives on the card by reading the reg again.
119 */
120static void
121spider_net_write_reg_sync(struct spider_net_card *card, u32 reg, u32 value)
122{
123 value = cpu_to_le32(value);
124 writel(value, card->regs + reg);
125 (void)readl(card->regs + reg);
126}
127
128/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700129 * spider_net_rx_irq_off - switch off rx irq on this spider card
130 * @card: device structure
131 *
132 * switches off rx irq by masking them out in the GHIINTnMSK register
133 */
134static void
135spider_net_rx_irq_off(struct spider_net_card *card)
136{
137 u32 regvalue;
138 unsigned long flags;
139
140 spin_lock_irqsave(&card->intmask_lock, flags);
141 regvalue = spider_net_read_reg(card, SPIDER_NET_GHIINT0MSK);
142 regvalue &= ~SPIDER_NET_RXINT;
Jens Osterkamp543cec52005-09-06 19:30:54 -0700143 spider_net_write_reg_sync(card, SPIDER_NET_GHIINT0MSK, regvalue);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700144 spin_unlock_irqrestore(&card->intmask_lock, flags);
145}
146
147/** spider_net_write_phy - write to phy register
148 * @netdev: adapter to be written to
149 * @mii_id: id of MII
150 * @reg: PHY register
151 * @val: value to be written to phy register
152 *
153 * spider_net_write_phy_register writes to an arbitrary PHY
154 * register via the spider GPCWOPCMD register. We assume the queue does
155 * not run full (not more than 15 commands outstanding).
156 **/
157static void
158spider_net_write_phy(struct net_device *netdev, int mii_id,
159 int reg, int val)
160{
161 struct spider_net_card *card = netdev_priv(netdev);
162 u32 writevalue;
163
164 writevalue = ((u32)mii_id << 21) |
165 ((u32)reg << 16) | ((u32)val);
166
167 spider_net_write_reg(card, SPIDER_NET_GPCWOPCMD, writevalue);
168}
169
170/** spider_net_read_phy - read from phy register
171 * @netdev: network device to be read from
172 * @mii_id: id of MII
173 * @reg: PHY register
174 *
175 * Returns value read from PHY register
176 *
177 * spider_net_write_phy reads from an arbitrary PHY
178 * register via the spider GPCROPCMD register
179 **/
180static int
181spider_net_read_phy(struct net_device *netdev, int mii_id, int reg)
182{
183 struct spider_net_card *card = netdev_priv(netdev);
184 u32 readvalue;
185
186 readvalue = ((u32)mii_id << 21) | ((u32)reg << 16);
187 spider_net_write_reg(card, SPIDER_NET_GPCROPCMD, readvalue);
188
189 /* we don't use semaphores to wait for an SPIDER_NET_GPROPCMPINT
190 * interrupt, as we poll for the completion of the read operation
191 * in spider_net_read_phy. Should take about 50 us */
192 do {
193 readvalue = spider_net_read_reg(card, SPIDER_NET_GPCROPCMD);
194 } while (readvalue & SPIDER_NET_GPREXEC);
195
196 readvalue &= SPIDER_NET_GPRDAT_MASK;
197
198 return readvalue;
199}
200
201/**
202 * spider_net_rx_irq_on - switch on rx irq on this spider card
203 * @card: device structure
204 *
205 * switches on rx irq by enabling them in the GHIINTnMSK register
206 */
207static void
208spider_net_rx_irq_on(struct spider_net_card *card)
209{
210 u32 regvalue;
211 unsigned long flags;
212
213 spin_lock_irqsave(&card->intmask_lock, flags);
214 regvalue = spider_net_read_reg(card, SPIDER_NET_GHIINT0MSK);
215 regvalue |= SPIDER_NET_RXINT;
Jens Osterkamp543cec52005-09-06 19:30:54 -0700216 spider_net_write_reg_sync(card, SPIDER_NET_GHIINT0MSK, regvalue);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700217 spin_unlock_irqrestore(&card->intmask_lock, flags);
218}
219
220/**
221 * spider_net_tx_irq_off - switch off tx irq on this spider card
222 * @card: device structure
223 *
224 * switches off tx irq by masking them out in the GHIINTnMSK register
225 */
226static void
227spider_net_tx_irq_off(struct spider_net_card *card)
228{
229 u32 regvalue;
230 unsigned long flags;
231
232 spin_lock_irqsave(&card->intmask_lock, flags);
233 regvalue = spider_net_read_reg(card, SPIDER_NET_GHIINT0MSK);
234 regvalue &= ~SPIDER_NET_TXINT;
Jens Osterkamp543cec52005-09-06 19:30:54 -0700235 spider_net_write_reg_sync(card, SPIDER_NET_GHIINT0MSK, regvalue);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700236 spin_unlock_irqrestore(&card->intmask_lock, flags);
237}
238
239/**
240 * spider_net_tx_irq_on - switch on tx irq on this spider card
241 * @card: device structure
242 *
243 * switches on tx irq by enabling them in the GHIINTnMSK register
244 */
245static void
246spider_net_tx_irq_on(struct spider_net_card *card)
247{
248 u32 regvalue;
249 unsigned long flags;
250
251 spin_lock_irqsave(&card->intmask_lock, flags);
252 regvalue = spider_net_read_reg(card, SPIDER_NET_GHIINT0MSK);
253 regvalue |= SPIDER_NET_TXINT;
Jens Osterkamp543cec52005-09-06 19:30:54 -0700254 spider_net_write_reg_sync(card, SPIDER_NET_GHIINT0MSK, regvalue);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700255 spin_unlock_irqrestore(&card->intmask_lock, flags);
256}
257
258/**
259 * spider_net_set_promisc - sets the unicast address or the promiscuous mode
260 * @card: card structure
261 *
262 * spider_net_set_promisc sets the unicast destination address filter and
263 * thus either allows for non-promisc mode or promisc mode
264 */
265static void
266spider_net_set_promisc(struct spider_net_card *card)
267{
268 u32 macu, macl;
269 struct net_device *netdev = card->netdev;
270
271 if (netdev->flags & IFF_PROMISC) {
272 /* clear destination entry 0 */
273 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR, 0);
274 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR + 0x04, 0);
275 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R,
276 SPIDER_NET_PROMISC_VALUE);
277 } else {
278 macu = netdev->dev_addr[0];
279 macu <<= 8;
280 macu |= netdev->dev_addr[1];
281 memcpy(&macl, &netdev->dev_addr[2], sizeof(macl));
282
283 macu |= SPIDER_NET_UA_DESCR_VALUE;
284 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR, macu);
285 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR + 0x04, macl);
286 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R,
287 SPIDER_NET_NONPROMISC_VALUE);
288 }
289}
290
291/**
292 * spider_net_get_mac_address - read mac address from spider card
293 * @card: device structure
294 *
295 * reads MAC address from GMACUNIMACU and GMACUNIMACL registers
296 */
297static int
298spider_net_get_mac_address(struct net_device *netdev)
299{
300 struct spider_net_card *card = netdev_priv(netdev);
301 u32 macl, macu;
302
303 macl = spider_net_read_reg(card, SPIDER_NET_GMACUNIMACL);
304 macu = spider_net_read_reg(card, SPIDER_NET_GMACUNIMACU);
305
306 netdev->dev_addr[0] = (macu >> 24) & 0xff;
307 netdev->dev_addr[1] = (macu >> 16) & 0xff;
308 netdev->dev_addr[2] = (macu >> 8) & 0xff;
309 netdev->dev_addr[3] = macu & 0xff;
310 netdev->dev_addr[4] = (macl >> 8) & 0xff;
311 netdev->dev_addr[5] = macl & 0xff;
312
313 if (!is_valid_ether_addr(&netdev->dev_addr[0]))
314 return -EINVAL;
315
316 return 0;
317}
318
319/**
320 * spider_net_get_descr_status -- returns the status of a descriptor
321 * @descr: descriptor to look at
322 *
323 * returns the status as in the dmac_cmd_status field of the descriptor
324 */
325static enum spider_net_descr_status
326spider_net_get_descr_status(struct spider_net_descr *descr)
327{
328 u32 cmd_status;
329 rmb();
330 cmd_status = descr->dmac_cmd_status;
331 rmb();
332 cmd_status >>= SPIDER_NET_DESCR_IND_PROC_SHIFT;
333 /* no need to mask out any bits, as cmd_status is 32 bits wide only
334 * (and unsigned) */
335 return cmd_status;
336}
337
338/**
339 * spider_net_set_descr_status -- sets the status of a descriptor
340 * @descr: descriptor to change
341 * @status: status to set in the descriptor
342 *
343 * changes the status to the specified value. Doesn't change other bits
344 * in the status
345 */
346static void
347spider_net_set_descr_status(struct spider_net_descr *descr,
348 enum spider_net_descr_status status)
349{
350 u32 cmd_status;
351 /* read the status */
352 mb();
353 cmd_status = descr->dmac_cmd_status;
354 /* clean the upper 4 bits */
355 cmd_status &= SPIDER_NET_DESCR_IND_PROC_MASKO;
356 /* add the status to it */
357 cmd_status |= ((u32)status)<<SPIDER_NET_DESCR_IND_PROC_SHIFT;
358 /* and write it back */
359 descr->dmac_cmd_status = cmd_status;
360 wmb();
361}
362
363/**
364 * spider_net_free_chain - free descriptor chain
365 * @card: card structure
366 * @chain: address of chain
367 *
368 */
369static void
370spider_net_free_chain(struct spider_net_card *card,
371 struct spider_net_descr_chain *chain)
372{
373 struct spider_net_descr *descr;
374
375 for (descr = chain->tail; !descr->bus_addr; descr = descr->next) {
376 pci_unmap_single(card->pdev, descr->bus_addr,
377 SPIDER_NET_DESCR_SIZE, PCI_DMA_BIDIRECTIONAL);
378 descr->bus_addr = 0;
379 }
380}
381
382/**
383 * spider_net_init_chain - links descriptor chain
384 * @card: card structure
385 * @chain: address of chain
386 * @start_descr: address of descriptor array
387 * @no: number of descriptors
388 *
389 * we manage a circular list that mirrors the hardware structure,
390 * except that the hardware uses bus addresses.
391 *
392 * returns 0 on success, <0 on failure
393 */
394static int
395spider_net_init_chain(struct spider_net_card *card,
396 struct spider_net_descr_chain *chain,
397 struct spider_net_descr *start_descr, int no)
398{
399 int i;
400 struct spider_net_descr *descr;
401
402 spin_lock_init(&card->chain_lock);
403
404 descr = start_descr;
405 memset(descr, 0, sizeof(*descr) * no);
406
407 /* set up the hardware pointers in each descriptor */
408 for (i=0; i<no; i++, descr++) {
409 spider_net_set_descr_status(descr, SPIDER_NET_DESCR_NOT_IN_USE);
410
411 descr->bus_addr =
412 pci_map_single(card->pdev, descr,
413 SPIDER_NET_DESCR_SIZE,
414 PCI_DMA_BIDIRECTIONAL);
415
416 if (descr->bus_addr == DMA_ERROR_CODE)
417 goto iommu_error;
418
419 descr->next = descr + 1;
420 descr->prev = descr - 1;
421
422 }
423 /* do actual circular list */
424 (descr-1)->next = start_descr;
425 start_descr->prev = descr-1;
426
427 descr = start_descr;
428 for (i=0; i < no; i++, descr++) {
429 descr->next_descr_addr = descr->next->bus_addr;
430 }
431
432 chain->head = start_descr;
433 chain->tail = start_descr;
434
435 return 0;
436
437iommu_error:
438 descr = start_descr;
439 for (i=0; i < no; i++, descr++)
440 if (descr->bus_addr)
441 pci_unmap_single(card->pdev, descr->bus_addr,
442 SPIDER_NET_DESCR_SIZE, PCI_DMA_BIDIRECTIONAL);
443 return -ENOMEM;
444}
445
446/**
447 * spider_net_free_rx_chain_contents - frees descr contents in rx chain
448 * @card: card structure
449 *
450 * returns 0 on success, <0 on failure
451 */
452static void
453spider_net_free_rx_chain_contents(struct spider_net_card *card)
454{
455 struct spider_net_descr *descr;
456
457 descr = card->rx_chain.head;
458 while (descr->next != card->rx_chain.head) {
459 if (descr->skb) {
460 dev_kfree_skb(descr->skb);
461 pci_unmap_single(card->pdev, descr->buf_addr,
462 SPIDER_NET_MAX_MTU,
463 PCI_DMA_BIDIRECTIONAL);
464 }
465 descr = descr->next;
466 }
467}
468
469/**
470 * spider_net_prepare_rx_descr - reinitializes a rx descriptor
471 * @card: card structure
472 * @descr: descriptor to re-init
473 *
474 * return 0 on succes, <0 on failure
475 *
476 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
477 * Activate the descriptor state-wise
478 */
479static int
480spider_net_prepare_rx_descr(struct spider_net_card *card,
481 struct spider_net_descr *descr)
482{
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500483 dma_addr_t buf;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700484 int error = 0;
485 int offset;
486 int bufsize;
487
488 /* we need to round up the buffer size to a multiple of 128 */
489 bufsize = (SPIDER_NET_MAX_MTU + SPIDER_NET_RXBUF_ALIGN - 1) &
490 (~(SPIDER_NET_RXBUF_ALIGN - 1));
491
492 /* and we need to have it 128 byte aligned, therefore we allocate a
493 * bit more */
494 /* allocate an skb */
495 descr->skb = dev_alloc_skb(bufsize + SPIDER_NET_RXBUF_ALIGN - 1);
496 if (!descr->skb) {
497 if (net_ratelimit())
498 if (netif_msg_rx_err(card))
499 pr_err("Not enough memory to allocate "
500 "rx buffer\n");
501 return -ENOMEM;
502 }
503 descr->buf_size = bufsize;
504 descr->result_size = 0;
505 descr->valid_size = 0;
506 descr->data_status = 0;
507 descr->data_error = 0;
508
509 offset = ((unsigned long)descr->skb->data) &
510 (SPIDER_NET_RXBUF_ALIGN - 1);
511 if (offset)
512 skb_reserve(descr->skb, SPIDER_NET_RXBUF_ALIGN - offset);
513 /* io-mmu-map the skb */
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500514 buf = pci_map_single(card->pdev, descr->skb->data,
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700515 SPIDER_NET_MAX_MTU,
516 PCI_DMA_BIDIRECTIONAL);
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500517 descr->buf_addr = buf;
518 if (buf == DMA_ERROR_CODE) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700519 dev_kfree_skb_any(descr->skb);
520 if (netif_msg_rx_err(card))
521 pr_err("Could not iommu-map rx buffer\n");
522 spider_net_set_descr_status(descr, SPIDER_NET_DESCR_NOT_IN_USE);
523 } else {
524 descr->dmac_cmd_status = SPIDER_NET_DMAC_RX_CARDOWNED;
525 }
526
527 return error;
528}
529
530/**
531 * spider_net_enable_rxctails - sets RX dmac chain tail addresses
532 * @card: card structure
533 *
534 * spider_net_enable_rxctails sets the RX DMAC chain tail adresses in the
535 * chip by writing to the appropriate register. DMA is enabled in
536 * spider_net_enable_rxdmac.
537 */
538static void
539spider_net_enable_rxchtails(struct spider_net_card *card)
540{
541 /* assume chain is aligned correctly */
542 spider_net_write_reg(card, SPIDER_NET_GDADCHA ,
543 card->rx_chain.tail->bus_addr);
544}
545
546/**
547 * spider_net_enable_rxdmac - enables a receive DMA controller
548 * @card: card structure
549 *
550 * spider_net_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
551 * in the GDADMACCNTR register
552 */
553static void
554spider_net_enable_rxdmac(struct spider_net_card *card)
555{
556 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
557 SPIDER_NET_DMA_RX_VALUE);
558}
559
560/**
561 * spider_net_refill_rx_chain - refills descriptors/skbs in the rx chains
562 * @card: card structure
563 *
564 * refills descriptors in all chains (last used chain first): allocates skbs
565 * and iommu-maps them.
566 */
567static void
568spider_net_refill_rx_chain(struct spider_net_card *card)
569{
570 struct spider_net_descr_chain *chain;
571 int count = 0;
572 unsigned long flags;
573
574 chain = &card->rx_chain;
575
576 spin_lock_irqsave(&card->chain_lock, flags);
577 while (spider_net_get_descr_status(chain->head) ==
578 SPIDER_NET_DESCR_NOT_IN_USE) {
579 if (spider_net_prepare_rx_descr(card, chain->head))
580 break;
581 count++;
582 chain->head = chain->head->next;
583 }
584 spin_unlock_irqrestore(&card->chain_lock, flags);
585
586 /* could be optimized, only do that, if we know the DMA processing
587 * has terminated */
588 if (count)
589 spider_net_enable_rxdmac(card);
590}
591
592/**
593 * spider_net_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
594 * @card: card structure
595 *
596 * returns 0 on success, <0 on failure
597 */
598static int
599spider_net_alloc_rx_skbs(struct spider_net_card *card)
600{
601 int result;
602 struct spider_net_descr_chain *chain;
603
604 result = -ENOMEM;
605
606 chain = &card->rx_chain;
607 /* put at least one buffer into the chain. if this fails,
608 * we've got a problem. if not, spider_net_refill_rx_chain
609 * will do the rest at the end of this function */
610 if (spider_net_prepare_rx_descr(card, chain->head))
611 goto error;
612 else
613 chain->head = chain->head->next;
614
615 /* this will allocate the rest of the rx buffers; if not, it's
616 * business as usual later on */
617 spider_net_refill_rx_chain(card);
618 return 0;
619
620error:
621 spider_net_free_rx_chain_contents(card);
622 return result;
623}
624
625/**
626 * spider_net_release_tx_descr - processes a used tx descriptor
627 * @card: card structure
628 * @descr: descriptor to release
629 *
630 * releases a used tx descriptor (unmapping, freeing of skb)
631 */
632static void
633spider_net_release_tx_descr(struct spider_net_card *card,
634 struct spider_net_descr *descr)
635{
636 struct sk_buff *skb;
637
638 /* unmap the skb */
639 skb = descr->skb;
640 pci_unmap_single(card->pdev, descr->buf_addr, skb->len,
641 PCI_DMA_BIDIRECTIONAL);
642
643 dev_kfree_skb_any(skb);
644
645 /* set status to not used */
646 spider_net_set_descr_status(descr, SPIDER_NET_DESCR_NOT_IN_USE);
647}
648
649/**
650 * spider_net_release_tx_chain - processes sent tx descriptors
651 * @card: adapter structure
652 * @brutal: if set, don't care about whether descriptor seems to be in use
653 *
654 * releases the tx descriptors that spider has finished with (if non-brutal)
655 * or simply release tx descriptors (if brutal)
656 */
657static void
658spider_net_release_tx_chain(struct spider_net_card *card, int brutal)
659{
660 struct spider_net_descr_chain *tx_chain = &card->tx_chain;
661 enum spider_net_descr_status status;
662
663 spider_net_tx_irq_off(card);
664
665 /* no lock for chain needed, if this is only executed once at a time */
666again:
667 for (;;) {
668 status = spider_net_get_descr_status(tx_chain->tail);
669 switch (status) {
670 case SPIDER_NET_DESCR_CARDOWNED:
671 if (!brutal) goto out;
672 /* fallthrough, if we release the descriptors
673 * brutally (then we don't care about
674 * SPIDER_NET_DESCR_CARDOWNED) */
675 case SPIDER_NET_DESCR_RESPONSE_ERROR:
676 case SPIDER_NET_DESCR_PROTECTION_ERROR:
677 case SPIDER_NET_DESCR_FORCE_END:
678 if (netif_msg_tx_err(card))
679 pr_err("%s: forcing end of tx descriptor "
680 "with status x%02x\n",
681 card->netdev->name, status);
682 card->netdev_stats.tx_dropped++;
683 break;
684
685 case SPIDER_NET_DESCR_COMPLETE:
686 card->netdev_stats.tx_packets++;
687 card->netdev_stats.tx_bytes +=
688 tx_chain->tail->skb->len;
689 break;
690
691 default: /* any other value (== SPIDER_NET_DESCR_NOT_IN_USE) */
692 goto out;
693 }
694 spider_net_release_tx_descr(card, tx_chain->tail);
695 tx_chain->tail = tx_chain->tail->next;
696 }
697out:
698 netif_wake_queue(card->netdev);
699
700 if (!brutal) {
701 /* switch on tx irqs (while we are still in the interrupt
702 * handler, so we don't get an interrupt), check again
703 * for done descriptors. This results in fewer interrupts */
704 spider_net_tx_irq_on(card);
705 status = spider_net_get_descr_status(tx_chain->tail);
706 switch (status) {
707 case SPIDER_NET_DESCR_RESPONSE_ERROR:
708 case SPIDER_NET_DESCR_PROTECTION_ERROR:
709 case SPIDER_NET_DESCR_FORCE_END:
710 case SPIDER_NET_DESCR_COMPLETE:
711 goto again;
712 default:
713 break;
714 }
715 }
716
717}
718
719/**
720 * spider_net_get_multicast_hash - generates hash for multicast filter table
721 * @addr: multicast address
722 *
723 * returns the hash value.
724 *
725 * spider_net_get_multicast_hash calculates a hash value for a given multicast
726 * address, that is used to set the multicast filter tables
727 */
728static u8
729spider_net_get_multicast_hash(struct net_device *netdev, __u8 *addr)
730{
731 /* FIXME: an addr of 01:00:5e:00:00:01 must result in 0xa9,
732 * ff:ff:ff:ff:ff:ff must result in 0xfd */
733 u32 crc;
734 u8 hash;
735
736 crc = crc32_be(~0, addr, netdev->addr_len);
737
738 hash = (crc >> 27);
739 hash <<= 3;
740 hash |= crc & 7;
741
742 return hash;
743}
744
745/**
746 * spider_net_set_multi - sets multicast addresses and promisc flags
747 * @netdev: interface device structure
748 *
749 * spider_net_set_multi configures multicast addresses as needed for the
750 * netdev interface. It also sets up multicast, allmulti and promisc
751 * flags appropriately
752 */
753static void
754spider_net_set_multi(struct net_device *netdev)
755{
756 struct dev_mc_list *mc;
757 u8 hash;
758 int i;
759 u32 reg;
760 struct spider_net_card *card = netdev_priv(netdev);
761 unsigned long bitmask[SPIDER_NET_MULTICAST_HASHES / BITS_PER_LONG] =
762 {0, };
763
764 spider_net_set_promisc(card);
765
766 if (netdev->flags & IFF_ALLMULTI) {
767 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES; i++) {
768 set_bit(i, bitmask);
769 }
770 goto write_hash;
771 }
772
773 /* well, we know, what the broadcast hash value is: it's xfd
774 hash = spider_net_get_multicast_hash(netdev, netdev->broadcast); */
775 set_bit(0xfd, bitmask);
776
777 for (mc = netdev->mc_list; mc; mc = mc->next) {
778 hash = spider_net_get_multicast_hash(netdev, mc->dmi_addr);
779 set_bit(hash, bitmask);
780 }
781
782write_hash:
783 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES / 4; i++) {
784 reg = 0;
785 if (test_bit(i * 4, bitmask))
786 reg += 0x08;
787 reg <<= 8;
788 if (test_bit(i * 4 + 1, bitmask))
789 reg += 0x08;
790 reg <<= 8;
791 if (test_bit(i * 4 + 2, bitmask))
792 reg += 0x08;
793 reg <<= 8;
794 if (test_bit(i * 4 + 3, bitmask))
795 reg += 0x08;
796
797 spider_net_write_reg(card, SPIDER_NET_GMRMHFILnR + i * 4, reg);
798 }
799}
800
801/**
802 * spider_net_disable_rxdmac - disables the receive DMA controller
803 * @card: card structure
804 *
805 * spider_net_disable_rxdmac terminates processing on the DMA controller by
806 * turing off DMA and issueing a force end
807 */
808static void
809spider_net_disable_rxdmac(struct spider_net_card *card)
810{
811 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
812 SPIDER_NET_DMA_RX_FEND_VALUE);
813}
814
815/**
816 * spider_net_stop - called upon ifconfig down
817 * @netdev: interface device structure
818 *
819 * always returns 0
820 */
821int
822spider_net_stop(struct net_device *netdev)
823{
824 struct spider_net_card *card = netdev_priv(netdev);
825
826 netif_poll_disable(netdev);
827 netif_carrier_off(netdev);
828 netif_stop_queue(netdev);
829
830 /* disable/mask all interrupts */
831 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, 0);
832 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK, 0);
833 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK, 0);
834
Jens Osterkamp543cec52005-09-06 19:30:54 -0700835 /* free_irq(netdev->irq, netdev);*/
836 free_irq(to_pci_dev(netdev->class_dev.dev)->irq, netdev);
837
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700838 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
839 SPIDER_NET_DMA_TX_FEND_VALUE);
840
841 /* turn off DMA, force end */
842 spider_net_disable_rxdmac(card);
843
844 /* release chains */
845 spider_net_release_tx_chain(card, 1);
846
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700847 spider_net_free_chain(card, &card->tx_chain);
848 spider_net_free_chain(card, &card->rx_chain);
849
850 return 0;
851}
852
853/**
854 * spider_net_get_next_tx_descr - returns the next available tx descriptor
855 * @card: device structure to get descriptor from
856 *
857 * returns the address of the next descriptor, or NULL if not available.
858 */
859static struct spider_net_descr *
860spider_net_get_next_tx_descr(struct spider_net_card *card)
861{
862 /* check, if head points to not-in-use descr */
863 if ( spider_net_get_descr_status(card->tx_chain.head) ==
864 SPIDER_NET_DESCR_NOT_IN_USE ) {
865 return card->tx_chain.head;
866 } else {
867 return NULL;
868 }
869}
870
871/**
872 * spider_net_set_txdescr_cmdstat - sets the tx descriptor command field
873 * @descr: descriptor structure to fill out
874 * @skb: packet to consider
875 *
876 * fills out the command and status field of the descriptor structure,
877 * depending on hardware checksum settings. This function assumes a wmb()
878 * has executed before.
879 */
880static void
881spider_net_set_txdescr_cmdstat(struct spider_net_descr *descr,
882 struct sk_buff *skb)
883{
884 if (skb->ip_summed != CHECKSUM_HW) {
885 descr->dmac_cmd_status = SPIDER_NET_DMAC_CMDSTAT_NOCS;
886 return;
887 }
888
889 /* is packet ip?
890 * if yes: tcp? udp? */
891 if (skb->protocol == htons(ETH_P_IP)) {
892 if (skb->nh.iph->protocol == IPPROTO_TCP) {
893 descr->dmac_cmd_status = SPIDER_NET_DMAC_CMDSTAT_TCPCS;
894 } else if (skb->nh.iph->protocol == IPPROTO_UDP) {
895 descr->dmac_cmd_status = SPIDER_NET_DMAC_CMDSTAT_UDPCS;
896 } else { /* the stack should checksum non-tcp and non-udp
897 packets on his own: NETIF_F_IP_CSUM */
898 descr->dmac_cmd_status = SPIDER_NET_DMAC_CMDSTAT_NOCS;
899 }
900 }
901}
902
903/**
904 * spider_net_prepare_tx_descr - fill tx descriptor with skb data
905 * @card: card structure
906 * @descr: descriptor structure to fill out
907 * @skb: packet to use
908 *
909 * returns 0 on success, <0 on failure.
910 *
911 * fills out the descriptor structure with skb data and len. Copies data,
912 * if needed (32bit DMA!)
913 */
914static int
915spider_net_prepare_tx_descr(struct spider_net_card *card,
916 struct spider_net_descr *descr,
917 struct sk_buff *skb)
918{
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500919 dma_addr_t buf = pci_map_single(card->pdev, skb->data,
920 skb->len, PCI_DMA_BIDIRECTIONAL);
921 if (buf == DMA_ERROR_CODE) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700922 if (netif_msg_tx_err(card))
923 pr_err("could not iommu-map packet (%p, %i). "
924 "Dropping packet\n", skb->data, skb->len);
925 return -ENOMEM;
926 }
927
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500928 descr->buf_addr = buf;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700929 descr->buf_size = skb->len;
930 descr->skb = skb;
931 descr->data_status = 0;
932
933 /* make sure the above values are in memory before we change the
934 * status */
935 wmb();
936
937 spider_net_set_txdescr_cmdstat(descr,skb);
938
939 return 0;
940}
941
942/**
943 * spider_net_kick_tx_dma - enables TX DMA processing
944 * @card: card structure
945 * @descr: descriptor address to enable TX processing at
946 *
947 * spider_net_kick_tx_dma writes the current tx chain head as start address
948 * of the tx descriptor chain and enables the transmission DMA engine
949 */
950static void
951spider_net_kick_tx_dma(struct spider_net_card *card,
952 struct spider_net_descr *descr)
953{
954 /* this is the only descriptor in the output chain.
955 * Enable TX DMA */
956
957 spider_net_write_reg(card, SPIDER_NET_GDTDCHA,
958 descr->bus_addr);
959
960 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
961 SPIDER_NET_DMA_TX_VALUE);
962}
963
964/**
965 * spider_net_xmit - transmits a frame over the device
966 * @skb: packet to send out
967 * @netdev: interface device structure
968 *
969 * returns 0 on success, <0 on failure
970 */
971static int
972spider_net_xmit(struct sk_buff *skb, struct net_device *netdev)
973{
974 struct spider_net_card *card = netdev_priv(netdev);
975 struct spider_net_descr *descr;
976 int result;
977
978 descr = spider_net_get_next_tx_descr(card);
979
980 if (!descr) {
981 netif_stop_queue(netdev);
982
983 descr = spider_net_get_next_tx_descr(card);
984 if (!descr)
985 goto error;
986 else
987 netif_start_queue(netdev);
988 }
989
990 result = spider_net_prepare_tx_descr(card, descr, skb);
991 if (result)
992 goto error;
993
994 card->tx_chain.head = card->tx_chain.head->next;
995
996 /* make sure the status from spider_net_prepare_tx_descr is in
997 * memory before we check out the previous descriptor */
998 wmb();
999
1000 if (spider_net_get_descr_status(descr->prev) !=
1001 SPIDER_NET_DESCR_CARDOWNED)
1002 spider_net_kick_tx_dma(card, descr);
1003
1004 return NETDEV_TX_OK;
1005
1006error:
1007 card->netdev_stats.tx_dropped++;
1008 return NETDEV_TX_LOCKED;
1009}
1010
1011/**
1012 * spider_net_do_ioctl - called for device ioctls
1013 * @netdev: interface device structure
1014 * @ifr: request parameter structure for ioctl
1015 * @cmd: command code for ioctl
1016 *
1017 * returns 0 on success, <0 on failure. Currently, we have no special ioctls.
1018 * -EOPNOTSUPP is returned, if an unknown ioctl was requested
1019 */
1020static int
1021spider_net_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1022{
1023 switch (cmd) {
1024 default:
1025 return -EOPNOTSUPP;
1026 }
1027}
1028
1029/**
1030 * spider_net_pass_skb_up - takes an skb from a descriptor and passes it on
1031 * @descr: descriptor to process
1032 * @card: card structure
1033 *
1034 * returns 1 on success, 0 if no packet was passed to the stack
1035 *
1036 * iommu-unmaps the skb, fills out skb structure and passes the data to the
1037 * stack. The descriptor state is not changed.
1038 */
1039static int
1040spider_net_pass_skb_up(struct spider_net_descr *descr,
1041 struct spider_net_card *card)
1042{
1043 struct sk_buff *skb;
1044 struct net_device *netdev;
1045 u32 data_status, data_error;
1046
1047 data_status = descr->data_status;
1048 data_error = descr->data_error;
1049
1050 netdev = card->netdev;
1051
1052 /* check for errors in the data_error flag */
1053 if ((data_error & SPIDER_NET_DATA_ERROR_MASK) &&
1054 netif_msg_rx_err(card))
1055 pr_err("error in received descriptor found, "
1056 "data_status=x%08x, data_error=x%08x\n",
1057 data_status, data_error);
1058
1059 /* prepare skb, unmap descriptor */
1060 skb = descr->skb;
1061 pci_unmap_single(card->pdev, descr->buf_addr, SPIDER_NET_MAX_MTU,
1062 PCI_DMA_BIDIRECTIONAL);
1063
1064 /* the cases we'll throw away the packet immediately */
1065 if (data_error & SPIDER_NET_DESTROY_RX_FLAGS)
1066 return 0;
1067
1068 skb->dev = netdev;
1069 skb_put(skb, descr->valid_size);
1070
1071 /* the card seems to add 2 bytes of junk in front
1072 * of the ethernet frame */
1073#define SPIDER_MISALIGN 2
1074 skb_pull(skb, SPIDER_MISALIGN);
1075 skb->protocol = eth_type_trans(skb, netdev);
1076
1077 /* checksum offload */
1078 if (card->options.rx_csum) {
1079 if ( (data_status & SPIDER_NET_DATA_STATUS_CHK_MASK) &&
1080 (!(data_error & SPIDER_NET_DATA_ERROR_CHK_MASK)) )
1081 skb->ip_summed = CHECKSUM_UNNECESSARY;
1082 else
1083 skb->ip_summed = CHECKSUM_NONE;
1084 } else {
1085 skb->ip_summed = CHECKSUM_NONE;
1086 }
1087
1088 if (data_status & SPIDER_NET_VLAN_PACKET) {
1089 /* further enhancements: HW-accel VLAN
1090 * vlan_hwaccel_receive_skb
1091 */
1092 }
1093
1094 /* pass skb up to stack */
1095 netif_receive_skb(skb);
1096
1097 /* update netdevice statistics */
1098 card->netdev_stats.rx_packets++;
1099 card->netdev_stats.rx_bytes += skb->len;
1100
1101 return 1;
1102}
1103
1104/**
1105 * spider_net_decode_descr - processes an rx descriptor
1106 * @card: card structure
1107 *
1108 * returns 1 if a packet has been sent to the stack, otherwise 0
1109 *
1110 * processes an rx descriptor by iommu-unmapping the data buffer and passing
1111 * the packet up to the stack
1112 */
1113static int
1114spider_net_decode_one_descr(struct spider_net_card *card)
1115{
1116 enum spider_net_descr_status status;
1117 struct spider_net_descr *descr;
1118 struct spider_net_descr_chain *chain;
1119 int result;
1120
1121 chain = &card->rx_chain;
1122 descr = chain->tail;
1123
1124 status = spider_net_get_descr_status(descr);
1125
1126 if (status == SPIDER_NET_DESCR_CARDOWNED) {
1127 /* nothing in the descriptor yet */
1128 return 0;
1129 }
1130
1131 if (status == SPIDER_NET_DESCR_NOT_IN_USE) {
1132 /* not initialized yet, I bet chain->tail == chain->head
1133 * and the ring is empty */
1134 spider_net_refill_rx_chain(card);
1135 return 0;
1136 }
1137
1138 /* descriptor definitively used -- move on head */
1139 chain->tail = descr->next;
1140
1141 result = 0;
1142 if ( (status == SPIDER_NET_DESCR_RESPONSE_ERROR) ||
1143 (status == SPIDER_NET_DESCR_PROTECTION_ERROR) ||
1144 (status == SPIDER_NET_DESCR_FORCE_END) ) {
1145 if (netif_msg_rx_err(card))
1146 pr_err("%s: dropping RX descriptor with state %d\n",
1147 card->netdev->name, status);
1148 card->netdev_stats.rx_dropped++;
1149 goto refill;
1150 }
1151
1152 if ( (status != SPIDER_NET_DESCR_COMPLETE) &&
1153 (status != SPIDER_NET_DESCR_FRAME_END) ) {
1154 if (netif_msg_rx_err(card))
1155 pr_err("%s: RX descriptor with state %d\n",
1156 card->netdev->name, status);
1157 goto refill;
1158 }
1159
1160 /* ok, we've got a packet in descr */
1161 result = spider_net_pass_skb_up(descr, card);
1162refill:
1163 spider_net_set_descr_status(descr, SPIDER_NET_DESCR_NOT_IN_USE);
1164 /* change the descriptor state: */
1165 spider_net_refill_rx_chain(card);
1166
1167 return result;
1168}
1169
1170/**
1171 * spider_net_poll - NAPI poll function called by the stack to return packets
1172 * @netdev: interface device structure
1173 * @budget: number of packets we can pass to the stack at most
1174 *
1175 * returns 0 if no more packets available to the driver/stack. Returns 1,
1176 * if the quota is exceeded, but the driver has still packets.
1177 *
1178 * spider_net_poll returns all packets from the rx descriptors to the stack
1179 * (using netif_receive_skb). If all/enough packets are up, the driver
1180 * reenables interrupts and returns 0. If not, 1 is returned.
1181 */
1182static int
1183spider_net_poll(struct net_device *netdev, int *budget)
1184{
1185 struct spider_net_card *card = netdev_priv(netdev);
1186 int packets_to_do, packets_done = 0;
1187 int no_more_packets = 0;
1188
1189 packets_to_do = min(*budget, netdev->quota);
1190
1191 while (packets_to_do) {
1192 if (spider_net_decode_one_descr(card)) {
1193 packets_done++;
1194 packets_to_do--;
1195 } else {
1196 /* no more packets for the stack */
1197 no_more_packets = 1;
1198 break;
1199 }
1200 }
1201
1202 netdev->quota -= packets_done;
1203 *budget -= packets_done;
1204
1205 /* if all packets are in the stack, enable interrupts and return 0 */
1206 /* if not, return 1 */
1207 if (no_more_packets) {
1208 netif_rx_complete(netdev);
1209 spider_net_rx_irq_on(card);
1210 return 0;
1211 }
1212
1213 return 1;
1214}
1215
1216/**
1217 * spider_net_vlan_rx_reg - initializes VLAN structures in the driver and card
1218 * @netdev: interface device structure
1219 * @grp: vlan_group structure that is registered (NULL on destroying interface)
1220 */
1221static void
1222spider_net_vlan_rx_reg(struct net_device *netdev, struct vlan_group *grp)
1223{
1224 /* further enhancement... yet to do */
1225 return;
1226}
1227
1228/**
1229 * spider_net_vlan_rx_add - adds VLAN id to the card filter
1230 * @netdev: interface device structure
1231 * @vid: VLAN id to add
1232 */
1233static void
1234spider_net_vlan_rx_add(struct net_device *netdev, uint16_t vid)
1235{
1236 /* further enhancement... yet to do */
1237 /* add vid to card's VLAN filter table */
1238 return;
1239}
1240
1241/**
1242 * spider_net_vlan_rx_kill - removes VLAN id to the card filter
1243 * @netdev: interface device structure
1244 * @vid: VLAN id to remove
1245 */
1246static void
1247spider_net_vlan_rx_kill(struct net_device *netdev, uint16_t vid)
1248{
1249 /* further enhancement... yet to do */
1250 /* remove vid from card's VLAN filter table */
1251}
1252
1253/**
1254 * spider_net_get_stats - get interface statistics
1255 * @netdev: interface device structure
1256 *
1257 * returns the interface statistics residing in the spider_net_card struct
1258 */
1259static struct net_device_stats *
1260spider_net_get_stats(struct net_device *netdev)
1261{
1262 struct spider_net_card *card = netdev_priv(netdev);
1263 struct net_device_stats *stats = &card->netdev_stats;
1264 return stats;
1265}
1266
1267/**
1268 * spider_net_change_mtu - changes the MTU of an interface
1269 * @netdev: interface device structure
1270 * @new_mtu: new MTU value
1271 *
1272 * returns 0 on success, <0 on failure
1273 */
1274static int
1275spider_net_change_mtu(struct net_device *netdev, int new_mtu)
1276{
1277 /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
1278 * and mtu is outbound only anyway */
1279 if ( (new_mtu < SPIDER_NET_MIN_MTU ) ||
1280 (new_mtu > SPIDER_NET_MAX_MTU) )
1281 return -EINVAL;
1282 netdev->mtu = new_mtu;
1283 return 0;
1284}
1285
1286/**
1287 * spider_net_set_mac - sets the MAC of an interface
1288 * @netdev: interface device structure
1289 * @ptr: pointer to new MAC address
1290 *
1291 * Returns 0 on success, <0 on failure. Currently, we don't support this
1292 * and will always return EOPNOTSUPP.
1293 */
1294static int
1295spider_net_set_mac(struct net_device *netdev, void *p)
1296{
1297 struct spider_net_card *card = netdev_priv(netdev);
Jens Osterkamp054034d2005-09-06 19:30:55 -07001298 u32 macl, macu, regvalue;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001299 struct sockaddr *addr = p;
1300
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001301 if (!is_valid_ether_addr(addr->sa_data))
1302 return -EADDRNOTAVAIL;
1303
Jens Osterkamp054034d2005-09-06 19:30:55 -07001304 /* switch off GMACTPE and GMACRPE */
1305 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1306 regvalue &= ~((1 << 5) | (1 << 6));
1307 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1308
1309 /* write mac */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001310 macu = (addr->sa_data[0]<<24) + (addr->sa_data[1]<<16) +
1311 (addr->sa_data[2]<<8) + (addr->sa_data[3]);
1312 macl = (addr->sa_data[4]<<8) + (addr->sa_data[5]);
1313 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACU, macu);
1314 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACL, macl);
1315
Jens Osterkamp054034d2005-09-06 19:30:55 -07001316 /* switch GMACTPE and GMACRPE back on */
1317 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1318 regvalue |= ((1 << 5) | (1 << 6));
1319 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1320
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001321 spider_net_set_promisc(card);
1322
1323 /* look up, whether we have been successful */
1324 if (spider_net_get_mac_address(netdev))
1325 return -EADDRNOTAVAIL;
1326 if (memcmp(netdev->dev_addr,addr->sa_data,netdev->addr_len))
1327 return -EADDRNOTAVAIL;
1328
1329 return 0;
1330}
1331
1332/**
1333 * spider_net_enable_txdmac - enables a TX DMA controller
1334 * @card: card structure
1335 *
1336 * spider_net_enable_txdmac enables the TX DMA controller by setting the
1337 * descriptor chain tail address
1338 */
1339static void
1340spider_net_enable_txdmac(struct spider_net_card *card)
1341{
1342 /* assume chain is aligned correctly */
1343 spider_net_write_reg(card, SPIDER_NET_GDTDCHA,
1344 card->tx_chain.tail->bus_addr);
1345}
1346
1347/**
1348 * spider_net_handle_error_irq - handles errors raised by an interrupt
1349 * @card: card structure
1350 * @status_reg: interrupt status register 0 (GHIINT0STS)
1351 *
1352 * spider_net_handle_error_irq treats or ignores all error conditions
1353 * found when an interrupt is presented
1354 */
1355static void
1356spider_net_handle_error_irq(struct spider_net_card *card, u32 status_reg)
1357{
1358 u32 error_reg1, error_reg2;
1359 u32 i;
1360 int show_error = 1;
1361
1362 error_reg1 = spider_net_read_reg(card, SPIDER_NET_GHIINT1STS);
1363 error_reg2 = spider_net_read_reg(card, SPIDER_NET_GHIINT2STS);
1364
1365 /* check GHIINT0STS ************************************/
1366 if (status_reg)
1367 for (i = 0; i < 32; i++)
1368 if (status_reg & (1<<i))
1369 switch (i)
1370 {
1371 /* let error_reg1 and error_reg2 evaluation decide, what to do
1372 case SPIDER_NET_PHYINT:
1373 case SPIDER_NET_GMAC2INT:
1374 case SPIDER_NET_GMAC1INT:
1375 case SPIDER_NET_GIPSINT:
1376 case SPIDER_NET_GFIFOINT:
1377 case SPIDER_NET_DMACINT:
1378 case SPIDER_NET_GSYSINT:
1379 break; */
1380
1381 case SPIDER_NET_GPWOPCMPINT:
1382 /* PHY write operation completed */
1383 show_error = 0;
1384 break;
1385 case SPIDER_NET_GPROPCMPINT:
1386 /* PHY read operation completed */
1387 /* we don't use semaphores, as we poll for the completion
1388 * of the read operation in spider_net_read_phy. Should take
1389 * about 50 us */
1390 show_error = 0;
1391 break;
1392 case SPIDER_NET_GPWFFINT:
1393 /* PHY command queue full */
1394 if (netif_msg_intr(card))
1395 pr_err("PHY write queue full\n");
1396 show_error = 0;
1397 break;
1398
1399 /* case SPIDER_NET_GRMDADRINT: not used. print a message */
1400 /* case SPIDER_NET_GRMARPINT: not used. print a message */
1401 /* case SPIDER_NET_GRMMPINT: not used. print a message */
1402
1403 case SPIDER_NET_GDTDEN0INT:
1404 /* someone has set TX_DMA_EN to 0 */
1405 show_error = 0;
1406 break;
1407
1408 case SPIDER_NET_GDDDEN0INT: /* fallthrough */
1409 case SPIDER_NET_GDCDEN0INT: /* fallthrough */
1410 case SPIDER_NET_GDBDEN0INT: /* fallthrough */
1411 case SPIDER_NET_GDADEN0INT:
1412 /* someone has set RX_DMA_EN to 0 */
1413 show_error = 0;
1414 break;
1415
1416 /* RX interrupts */
1417 case SPIDER_NET_GDDFDCINT:
1418 case SPIDER_NET_GDCFDCINT:
1419 case SPIDER_NET_GDBFDCINT:
1420 case SPIDER_NET_GDAFDCINT:
1421 /* case SPIDER_NET_GDNMINT: not used. print a message */
1422 /* case SPIDER_NET_GCNMINT: not used. print a message */
1423 /* case SPIDER_NET_GBNMINT: not used. print a message */
1424 /* case SPIDER_NET_GANMINT: not used. print a message */
1425 /* case SPIDER_NET_GRFNMINT: not used. print a message */
1426 show_error = 0;
1427 break;
1428
1429 /* TX interrupts */
1430 case SPIDER_NET_GDTFDCINT:
1431 show_error = 0;
1432 break;
1433 case SPIDER_NET_GTTEDINT:
1434 show_error = 0;
1435 break;
1436 case SPIDER_NET_GDTDCEINT:
1437 /* chain end. If a descriptor should be sent, kick off
1438 * tx dma
1439 if (card->tx_chain.tail == card->tx_chain.head)
1440 spider_net_kick_tx_dma(card);
1441 show_error = 0; */
1442 break;
1443
1444 /* case SPIDER_NET_G1TMCNTINT: not used. print a message */
1445 /* case SPIDER_NET_GFREECNTINT: not used. print a message */
1446 }
1447
1448 /* check GHIINT1STS ************************************/
1449 if (error_reg1)
1450 for (i = 0; i < 32; i++)
1451 if (error_reg1 & (1<<i))
1452 switch (i)
1453 {
1454 case SPIDER_NET_GTMFLLINT:
1455 if (netif_msg_intr(card))
1456 pr_err("Spider TX RAM full\n");
1457 show_error = 0;
1458 break;
1459 case SPIDER_NET_GRMFLLINT:
1460 if (netif_msg_intr(card))
1461 pr_err("Spider RX RAM full, incoming packets "
1462 "might be discarded !\n");
1463 netif_rx_schedule(card->netdev);
1464 spider_net_enable_rxchtails(card);
1465 spider_net_enable_rxdmac(card);
1466 break;
1467
1468 /* case SPIDER_NET_GTMSHTINT: problem, print a message */
1469 case SPIDER_NET_GDTINVDINT:
1470 /* allrighty. tx from previous descr ok */
1471 show_error = 0;
1472 break;
1473 /* case SPIDER_NET_GRFDFLLINT: print a message down there */
1474 /* case SPIDER_NET_GRFCFLLINT: print a message down there */
1475 /* case SPIDER_NET_GRFBFLLINT: print a message down there */
1476 /* case SPIDER_NET_GRFAFLLINT: print a message down there */
1477
1478 /* chain end */
1479 case SPIDER_NET_GDDDCEINT: /* fallthrough */
1480 case SPIDER_NET_GDCDCEINT: /* fallthrough */
1481 case SPIDER_NET_GDBDCEINT: /* fallthrough */
1482 case SPIDER_NET_GDADCEINT:
1483 if (netif_msg_intr(card))
1484 pr_err("got descriptor chain end interrupt, "
1485 "restarting DMAC %c.\n",
1486 'D'+i-SPIDER_NET_GDDDCEINT);
1487 spider_net_refill_rx_chain(card);
1488 show_error = 0;
1489 break;
1490
1491 /* invalid descriptor */
1492 case SPIDER_NET_GDDINVDINT: /* fallthrough */
1493 case SPIDER_NET_GDCINVDINT: /* fallthrough */
1494 case SPIDER_NET_GDBINVDINT: /* fallthrough */
1495 case SPIDER_NET_GDAINVDINT:
1496 /* could happen when rx chain is full */
1497 spider_net_refill_rx_chain(card);
1498 show_error = 0;
1499 break;
1500
1501 /* case SPIDER_NET_GDTRSERINT: problem, print a message */
1502 /* case SPIDER_NET_GDDRSERINT: problem, print a message */
1503 /* case SPIDER_NET_GDCRSERINT: problem, print a message */
1504 /* case SPIDER_NET_GDBRSERINT: problem, print a message */
1505 /* case SPIDER_NET_GDARSERINT: problem, print a message */
1506 /* case SPIDER_NET_GDSERINT: problem, print a message */
1507 /* case SPIDER_NET_GDTPTERINT: problem, print a message */
1508 /* case SPIDER_NET_GDDPTERINT: problem, print a message */
1509 /* case SPIDER_NET_GDCPTERINT: problem, print a message */
1510 /* case SPIDER_NET_GDBPTERINT: problem, print a message */
1511 /* case SPIDER_NET_GDAPTERINT: problem, print a message */
1512 default:
1513 show_error = 1;
1514 break;
1515 }
1516
1517 /* check GHIINT2STS ************************************/
1518 if (error_reg2)
1519 for (i = 0; i < 32; i++)
1520 if (error_reg2 & (1<<i))
1521 switch (i)
1522 {
1523 /* there is nothing we can (want to) do at this time. Log a
1524 * message, we can switch on and off the specific values later on
1525 case SPIDER_NET_GPROPERINT:
1526 case SPIDER_NET_GMCTCRSNGINT:
1527 case SPIDER_NET_GMCTLCOLINT:
1528 case SPIDER_NET_GMCTTMOTINT:
1529 case SPIDER_NET_GMCRCAERINT:
1530 case SPIDER_NET_GMCRCALERINT:
1531 case SPIDER_NET_GMCRALNERINT:
1532 case SPIDER_NET_GMCROVRINT:
1533 case SPIDER_NET_GMCRRNTINT:
1534 case SPIDER_NET_GMCRRXERINT:
1535 case SPIDER_NET_GTITCSERINT:
1536 case SPIDER_NET_GTIFMTERINT:
1537 case SPIDER_NET_GTIPKTRVKINT:
1538 case SPIDER_NET_GTISPINGINT:
1539 case SPIDER_NET_GTISADNGINT:
1540 case SPIDER_NET_GTISPDNGINT:
1541 case SPIDER_NET_GRIFMTERINT:
1542 case SPIDER_NET_GRIPKTRVKINT:
1543 case SPIDER_NET_GRISPINGINT:
1544 case SPIDER_NET_GRISADNGINT:
1545 case SPIDER_NET_GRISPDNGINT:
1546 break;
1547 */
1548 default:
1549 break;
1550 }
1551
1552 if ((show_error) && (netif_msg_intr(card)))
1553 pr_err("Got error interrupt, GHIINT0STS = 0x%08x, "
1554 "GHIINT1STS = 0x%08x, GHIINT2STS = 0x%08x\n",
1555 status_reg, error_reg1, error_reg2);
1556
1557 /* clear interrupt sources */
1558 spider_net_write_reg(card, SPIDER_NET_GHIINT1STS, error_reg1);
1559 spider_net_write_reg(card, SPIDER_NET_GHIINT2STS, error_reg2);
1560}
1561
1562/**
1563 * spider_net_interrupt - interrupt handler for spider_net
1564 * @irq: interupt number
1565 * @ptr: pointer to net_device
1566 * @regs: PU registers
1567 *
1568 * returns IRQ_HANDLED, if interrupt was for driver, or IRQ_NONE, if no
1569 * interrupt found raised by card.
1570 *
1571 * This is the interrupt handler, that turns off
1572 * interrupts for this device and makes the stack poll the driver
1573 */
1574static irqreturn_t
1575spider_net_interrupt(int irq, void *ptr, struct pt_regs *regs)
1576{
1577 struct net_device *netdev = ptr;
1578 struct spider_net_card *card = netdev_priv(netdev);
1579 u32 status_reg;
1580
1581 status_reg = spider_net_read_reg(card, SPIDER_NET_GHIINT0STS);
1582
1583 if (!status_reg)
1584 return IRQ_NONE;
1585
1586 if (status_reg & SPIDER_NET_TXINT)
1587 spider_net_release_tx_chain(card, 0);
1588
1589 if (status_reg & SPIDER_NET_RXINT ) {
1590 spider_net_rx_irq_off(card);
1591 netif_rx_schedule(netdev);
1592 }
1593
1594 /* we do this after rx and tx processing, as we want the tx chain
1595 * processed to see, whether we should restart tx dma processing */
1596 spider_net_handle_error_irq(card, status_reg);
1597
1598 /* clear interrupt sources */
1599 spider_net_write_reg(card, SPIDER_NET_GHIINT0STS, status_reg);
1600
1601 return IRQ_HANDLED;
1602}
1603
1604#ifdef CONFIG_NET_POLL_CONTROLLER
1605/**
1606 * spider_net_poll_controller - artificial interrupt for netconsole etc.
1607 * @netdev: interface device structure
1608 *
1609 * see Documentation/networking/netconsole.txt
1610 */
1611static void
1612spider_net_poll_controller(struct net_device *netdev)
1613{
1614 disable_irq(netdev->irq);
1615 spider_net_interrupt(netdev->irq, netdev, NULL);
1616 enable_irq(netdev->irq);
1617}
1618#endif /* CONFIG_NET_POLL_CONTROLLER */
1619
1620/**
1621 * spider_net_init_card - initializes the card
1622 * @card: card structure
1623 *
1624 * spider_net_init_card initializes the card so that other registers can
1625 * be used
1626 */
1627static void
1628spider_net_init_card(struct spider_net_card *card)
1629{
1630 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1631 SPIDER_NET_CKRCTRL_STOP_VALUE);
1632
1633 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1634 SPIDER_NET_CKRCTRL_RUN_VALUE);
1635}
1636
1637/**
1638 * spider_net_enable_card - enables the card by setting all kinds of regs
1639 * @card: card structure
1640 *
1641 * spider_net_enable_card sets a lot of SMMIO registers to enable the device
1642 */
1643static void
1644spider_net_enable_card(struct spider_net_card *card)
1645{
1646 int i;
1647 /* the following array consists of (register),(value) pairs
1648 * that are set in this function. A register of 0 ends the list */
1649 u32 regs[][2] = {
1650 { SPIDER_NET_GRESUMINTNUM, 0 },
1651 { SPIDER_NET_GREINTNUM, 0 },
1652
1653 /* set interrupt frame number registers */
1654 /* clear the single DMA engine registers first */
1655 { SPIDER_NET_GFAFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1656 { SPIDER_NET_GFBFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1657 { SPIDER_NET_GFCFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1658 { SPIDER_NET_GFDFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1659 /* then set, what we really need */
1660 { SPIDER_NET_GFFRMNUM, SPIDER_NET_FRAMENUM_VALUE },
1661
1662 /* timer counter registers and stuff */
1663 { SPIDER_NET_GFREECNNUM, 0 },
1664 { SPIDER_NET_GONETIMENUM, 0 },
1665 { SPIDER_NET_GTOUTFRMNUM, 0 },
1666
1667 /* RX mode setting */
1668 { SPIDER_NET_GRXMDSET, SPIDER_NET_RXMODE_VALUE },
1669 /* TX mode setting */
1670 { SPIDER_NET_GTXMDSET, SPIDER_NET_TXMODE_VALUE },
1671 /* IPSEC mode setting */
1672 { SPIDER_NET_GIPSECINIT, SPIDER_NET_IPSECINIT_VALUE },
1673
1674 { SPIDER_NET_GFTRESTRT, SPIDER_NET_RESTART_VALUE },
1675
1676 { SPIDER_NET_GMRWOLCTRL, 0 },
1677 { SPIDER_NET_GTESTMD, 0 },
1678
1679 { SPIDER_NET_GMACINTEN, 0 },
1680
1681 /* flow control stuff */
1682 { SPIDER_NET_GMACAPAUSE, SPIDER_NET_MACAPAUSE_VALUE },
1683 { SPIDER_NET_GMACTXPAUSE, SPIDER_NET_TXPAUSE_VALUE },
1684
1685 { SPIDER_NET_GMACBSTLMT, SPIDER_NET_BURSTLMT_VALUE },
1686 { 0, 0}
1687 };
1688
1689 i = 0;
1690 while (regs[i][0]) {
1691 spider_net_write_reg(card, regs[i][0], regs[i][1]);
1692 i++;
1693 }
1694
1695 /* clear unicast filter table entries 1 to 14 */
1696 for (i = 1; i <= 14; i++) {
1697 spider_net_write_reg(card,
1698 SPIDER_NET_GMRUAFILnR + i * 8,
1699 0x00080000);
1700 spider_net_write_reg(card,
1701 SPIDER_NET_GMRUAFILnR + i * 8 + 4,
1702 0x00000000);
1703 }
1704
1705 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R, 0x08080000);
1706
1707 spider_net_write_reg(card, SPIDER_NET_ECMODE, SPIDER_NET_ECMODE_VALUE);
1708
1709 /* set chain tail adress for RX chains and
1710 * enable DMA */
1711 spider_net_enable_rxchtails(card);
1712 spider_net_enable_rxdmac(card);
1713
1714 spider_net_write_reg(card, SPIDER_NET_GRXDMAEN, SPIDER_NET_WOL_VALUE);
1715
1716 /* set chain tail adress for TX chain */
1717 spider_net_enable_txdmac(card);
1718
1719 spider_net_write_reg(card, SPIDER_NET_GMACLENLMT,
1720 SPIDER_NET_LENLMT_VALUE);
1721 spider_net_write_reg(card, SPIDER_NET_GMACMODE,
1722 SPIDER_NET_MACMODE_VALUE);
1723 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD,
1724 SPIDER_NET_OPMODE_VALUE);
1725
1726 /* set interrupt mask registers */
1727 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK,
1728 SPIDER_NET_INT0_MASK_VALUE);
1729 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK,
1730 SPIDER_NET_INT1_MASK_VALUE);
1731 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK,
1732 SPIDER_NET_INT2_MASK_VALUE);
1733}
1734
1735/**
1736 * spider_net_open - called upon ifonfig up
1737 * @netdev: interface device structure
1738 *
1739 * returns 0 on success, <0 on failure
1740 *
1741 * spider_net_open allocates all the descriptors and memory needed for
1742 * operation, sets up multicast list and enables interrupts
1743 */
1744int
1745spider_net_open(struct net_device *netdev)
1746{
1747 struct spider_net_card *card = netdev_priv(netdev);
1748 int result;
1749
1750 result = -ENOMEM;
1751 if (spider_net_init_chain(card, &card->tx_chain,
1752 card->descr, tx_descriptors))
1753 goto alloc_tx_failed;
1754 if (spider_net_init_chain(card, &card->rx_chain,
1755 card->descr + tx_descriptors, rx_descriptors))
1756 goto alloc_rx_failed;
1757
1758 /* allocate rx skbs */
1759 if (spider_net_alloc_rx_skbs(card))
1760 goto alloc_skbs_failed;
1761
1762 spider_net_set_multi(netdev);
1763
1764 /* further enhancement: setup hw vlan, if needed */
1765
1766 result = -EBUSY;
1767 if (request_irq(netdev->irq, spider_net_interrupt,
1768 SA_SHIRQ, netdev->name, netdev))
1769 goto register_int_failed;
1770
1771 spider_net_enable_card(card);
1772
Jens Osterkamp543cec52005-09-06 19:30:54 -07001773 netif_start_queue(netdev);
1774 netif_carrier_on(netdev);
1775 netif_poll_enable(netdev);
1776
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001777 return 0;
1778
1779register_int_failed:
1780 spider_net_free_rx_chain_contents(card);
1781alloc_skbs_failed:
1782 spider_net_free_chain(card, &card->rx_chain);
1783alloc_rx_failed:
1784 spider_net_free_chain(card, &card->tx_chain);
1785alloc_tx_failed:
1786 return result;
1787}
1788
1789/**
1790 * spider_net_setup_phy - setup PHY
1791 * @card: card structure
1792 *
1793 * returns 0 on success, <0 on failure
1794 *
1795 * spider_net_setup_phy is used as part of spider_net_probe. Sets
1796 * the PHY to 1000 Mbps
1797 **/
1798static int
1799spider_net_setup_phy(struct spider_net_card *card)
1800{
1801 struct mii_phy *phy = &card->phy;
1802
1803 spider_net_write_reg(card, SPIDER_NET_GDTDMASEL,
1804 SPIDER_NET_DMASEL_VALUE);
1805 spider_net_write_reg(card, SPIDER_NET_GPCCTRL,
1806 SPIDER_NET_PHY_CTRL_VALUE);
1807 phy->mii_id = 1;
1808 phy->dev = card->netdev;
1809 phy->mdio_read = spider_net_read_phy;
1810 phy->mdio_write = spider_net_write_phy;
1811
1812 mii_phy_probe(phy, phy->mii_id);
1813
1814 if (phy->def->ops->setup_forced)
1815 phy->def->ops->setup_forced(phy, SPEED_1000, DUPLEX_FULL);
1816
1817 /* the following two writes could be moved to sungem_phy.c */
1818 /* enable fiber mode */
1819 spider_net_write_phy(card->netdev, 1, MII_NCONFIG, 0x9020);
1820 /* LEDs active in both modes, autosense prio = fiber */
1821 spider_net_write_phy(card->netdev, 1, MII_NCONFIG, 0x945f);
1822
Jens Osterkamp53abbf72005-09-16 08:55:33 +02001823 /* switch off fibre autoneg */
1824 spider_net_write_phy(card->netdev, 1, MII_NCONFIG, 0xfc01);
1825 spider_net_write_phy(card->netdev, 1, 0x0b, 0x0004);
1826
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001827 phy->def->ops->read_link(phy);
1828 pr_info("Found %s with %i Mbps, %s-duplex.\n", phy->def->name,
1829 phy->speed, phy->duplex==1 ? "Full" : "Half");
1830
1831 return 0;
1832}
1833
1834/**
1835 * spider_net_download_firmware - loads firmware into the adapter
1836 * @card: card structure
1837 * @firmware: firmware pointer
1838 *
1839 * spider_net_download_firmware loads the firmware opened by
1840 * spider_net_init_firmware into the adapter.
1841 */
Arnd Bergmann0d3ea162006-01-12 17:16:41 -05001842static int
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001843spider_net_download_firmware(struct spider_net_card *card,
1844 const struct firmware *firmware)
1845{
1846 int sequencer, i;
1847 u32 *fw_ptr = (u32 *)firmware->data;
1848
1849 /* stop sequencers */
1850 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1851 SPIDER_NET_STOP_SEQ_VALUE);
1852
1853 for (sequencer = 0; sequencer < 6; sequencer++) {
1854 spider_net_write_reg(card,
1855 SPIDER_NET_GSnPRGADR + sequencer * 8, 0);
1856 for (i = 0; i < SPIDER_NET_FIRMWARE_LEN; i++) {
1857 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1858 sequencer * 8, *fw_ptr);
1859 fw_ptr++;
1860 }
1861 }
1862
Arnd Bergmann0d3ea162006-01-12 17:16:41 -05001863 if (spider_net_read_reg(card, SPIDER_NET_GSINIT))
1864 return -EIO;
1865
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001866 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1867 SPIDER_NET_RUN_SEQ_VALUE);
Arnd Bergmann0d3ea162006-01-12 17:16:41 -05001868
1869 return 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001870}
1871
1872/**
1873 * spider_net_init_firmware - reads in firmware parts
1874 * @card: card structure
1875 *
1876 * Returns 0 on success, <0 on failure
1877 *
1878 * spider_net_init_firmware opens the sequencer firmware and does some basic
1879 * checks. This function opens and releases the firmware structure. A call
1880 * to download the firmware is performed before the release.
1881 *
1882 * Firmware format
1883 * ===============
1884 * spider_fw.bin is expected to be a file containing 6*1024*4 bytes, 4k being
1885 * the program for each sequencer. Use the command
1886 * tail -q -n +2 Seq_code1_0x088.txt Seq_code2_0x090.txt \
1887 * Seq_code3_0x098.txt Seq_code4_0x0A0.txt Seq_code5_0x0A8.txt \
1888 * Seq_code6_0x0B0.txt | xxd -r -p -c4 > spider_fw.bin
1889 *
1890 * to generate spider_fw.bin, if you have sequencer programs with something
1891 * like the following contents for each sequencer:
1892 * <ONE LINE COMMENT>
1893 * <FIRST 4-BYTES-WORD FOR SEQUENCER>
1894 * <SECOND 4-BYTES-WORD FOR SEQUENCER>
1895 * ...
1896 * <1024th 4-BYTES-WORD FOR SEQUENCER>
1897 */
1898static int
1899spider_net_init_firmware(struct spider_net_card *card)
1900{
Arnd Bergmann030d6752006-01-12 17:16:42 -05001901 struct firmware *firmware;
1902 struct device_node *dn;
1903 u8 *fw_prop;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001904 int err = -EIO;
1905
Arnd Bergmann030d6752006-01-12 17:16:42 -05001906 if (request_firmware((const struct firmware **)&firmware,
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001907 SPIDER_NET_FIRMWARE_NAME, &card->pdev->dev) < 0) {
1908 if (netif_msg_probe(card))
1909 pr_err("Couldn't read in sequencer data file %s.\n",
1910 SPIDER_NET_FIRMWARE_NAME);
Arnd Bergmann030d6752006-01-12 17:16:42 -05001911
1912 dn = pci_device_to_OF_node(card->pdev);
1913 if (!dn)
1914 goto out;
1915
1916 fw_prop = (u8 *)get_property(dn, "firmware", NULL);
1917 if (!fw_prop)
1918 goto out;
1919
1920 memcpy(firmware->data, fw_prop, 6 * SPIDER_NET_FIRMWARE_LEN * sizeof(u32));
1921 firmware->size = 6 * SPIDER_NET_FIRMWARE_LEN * sizeof(u32);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001922 }
1923
1924 if (firmware->size != 6 * SPIDER_NET_FIRMWARE_LEN * sizeof(u32)) {
1925 if (netif_msg_probe(card))
1926 pr_err("Invalid size of sequencer data file %s.\n",
1927 SPIDER_NET_FIRMWARE_NAME);
1928 goto out;
1929 }
1930
Arnd Bergmann0d3ea162006-01-12 17:16:41 -05001931 if (!spider_net_download_firmware(card, firmware))
1932 err = 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001933out:
1934 release_firmware(firmware);
1935
1936 return err;
1937}
1938
1939/**
1940 * spider_net_workaround_rxramfull - work around firmware bug
1941 * @card: card structure
1942 *
1943 * no return value
1944 **/
1945static void
1946spider_net_workaround_rxramfull(struct spider_net_card *card)
1947{
1948 int i, sequencer = 0;
1949
1950 /* cancel reset */
1951 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1952 SPIDER_NET_CKRCTRL_RUN_VALUE);
1953
1954 /* empty sequencer data */
1955 for (sequencer = 0; sequencer < 6; sequencer++) {
1956 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1957 sequencer * 8, 0x0);
1958 for (i = 0; i < SPIDER_NET_FIRMWARE_LEN; i++) {
1959 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1960 sequencer * 8, 0x0);
1961 }
1962 }
1963
1964 /* set sequencer operation */
1965 spider_net_write_reg(card, SPIDER_NET_GSINIT, 0x000000fe);
1966
1967 /* reset */
1968 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1969 SPIDER_NET_CKRCTRL_STOP_VALUE);
1970}
1971
1972/**
1973 * spider_net_tx_timeout_task - task scheduled by the watchdog timeout
1974 * function (to be called not under interrupt status)
1975 * @data: data, is interface device structure
1976 *
1977 * called as task when tx hangs, resets interface (if interface is up)
1978 */
1979static void
1980spider_net_tx_timeout_task(void *data)
1981{
1982 struct net_device *netdev = data;
1983 struct spider_net_card *card = netdev_priv(netdev);
1984
1985 if (!(netdev->flags & IFF_UP))
1986 goto out;
1987
1988 netif_device_detach(netdev);
1989 spider_net_stop(netdev);
1990
1991 spider_net_workaround_rxramfull(card);
1992 spider_net_init_card(card);
1993
1994 if (spider_net_setup_phy(card))
1995 goto out;
1996 if (spider_net_init_firmware(card))
1997 goto out;
1998
1999 spider_net_open(netdev);
2000 spider_net_kick_tx_dma(card, card->tx_chain.head);
2001 netif_device_attach(netdev);
2002
2003out:
2004 atomic_dec(&card->tx_timeout_task_counter);
2005}
2006
2007/**
2008 * spider_net_tx_timeout - called when the tx timeout watchdog kicks in.
2009 * @netdev: interface device structure
2010 *
2011 * called, if tx hangs. Schedules a task that resets the interface
2012 */
2013static void
2014spider_net_tx_timeout(struct net_device *netdev)
2015{
2016 struct spider_net_card *card;
2017
2018 card = netdev_priv(netdev);
2019 atomic_inc(&card->tx_timeout_task_counter);
2020 if (netdev->flags & IFF_UP)
2021 schedule_work(&card->tx_timeout_task);
2022 else
2023 atomic_dec(&card->tx_timeout_task_counter);
2024}
2025
2026/**
2027 * spider_net_setup_netdev_ops - initialization of net_device operations
2028 * @netdev: net_device structure
2029 *
2030 * fills out function pointers in the net_device structure
2031 */
2032static void
2033spider_net_setup_netdev_ops(struct net_device *netdev)
2034{
2035 netdev->open = &spider_net_open;
2036 netdev->stop = &spider_net_stop;
2037 netdev->hard_start_xmit = &spider_net_xmit;
2038 netdev->get_stats = &spider_net_get_stats;
2039 netdev->set_multicast_list = &spider_net_set_multi;
2040 netdev->set_mac_address = &spider_net_set_mac;
2041 netdev->change_mtu = &spider_net_change_mtu;
2042 netdev->do_ioctl = &spider_net_do_ioctl;
2043 /* tx watchdog */
2044 netdev->tx_timeout = &spider_net_tx_timeout;
2045 netdev->watchdog_timeo = SPIDER_NET_WATCHDOG_TIMEOUT;
2046 /* NAPI */
2047 netdev->poll = &spider_net_poll;
2048 netdev->weight = SPIDER_NET_NAPI_WEIGHT;
2049 /* HW VLAN */
2050 netdev->vlan_rx_register = &spider_net_vlan_rx_reg;
2051 netdev->vlan_rx_add_vid = &spider_net_vlan_rx_add;
2052 netdev->vlan_rx_kill_vid = &spider_net_vlan_rx_kill;
2053#ifdef CONFIG_NET_POLL_CONTROLLER
2054 /* poll controller */
2055 netdev->poll_controller = &spider_net_poll_controller;
2056#endif /* CONFIG_NET_POLL_CONTROLLER */
2057 /* ethtool ops */
2058 netdev->ethtool_ops = &spider_net_ethtool_ops;
2059}
2060
2061/**
2062 * spider_net_setup_netdev - initialization of net_device
2063 * @card: card structure
2064 *
2065 * Returns 0 on success or <0 on failure
2066 *
2067 * spider_net_setup_netdev initializes the net_device structure
2068 **/
2069static int
2070spider_net_setup_netdev(struct spider_net_card *card)
2071{
2072 int result;
2073 struct net_device *netdev = card->netdev;
2074 struct device_node *dn;
2075 struct sockaddr addr;
2076 u8 *mac;
2077
2078 SET_MODULE_OWNER(netdev);
2079 SET_NETDEV_DEV(netdev, &card->pdev->dev);
2080
2081 pci_set_drvdata(card->pdev, netdev);
2082 spin_lock_init(&card->intmask_lock);
2083 netdev->irq = card->pdev->irq;
2084
2085 card->options.rx_csum = SPIDER_NET_RX_CSUM_DEFAULT;
2086
2087 spider_net_setup_netdev_ops(netdev);
2088
2089 netdev->features = 0;
2090 /* some time: NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
2091 * NETIF_F_HW_VLAN_FILTER */
2092
2093 netdev->irq = card->pdev->irq;
2094
2095 dn = pci_device_to_OF_node(card->pdev);
Jens Osterkamp543cec52005-09-06 19:30:54 -07002096 if (!dn)
2097 return -EIO;
2098
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002099 mac = (u8 *)get_property(dn, "local-mac-address", NULL);
Jens Osterkamp543cec52005-09-06 19:30:54 -07002100 if (!mac)
2101 return -EIO;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002102 memcpy(addr.sa_data, mac, ETH_ALEN);
2103
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);
2107
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;
2114 }
2115
2116 if (netif_msg_probe(card))
2117 pr_info("Initialized device %s.\n", netdev->name);
2118
2119 return 0;
2120}
2121
2122/**
2123 * spider_net_alloc_card - allocates net_device and card structure
2124 *
2125 * returns the card structure or NULL in case of errors
2126 *
2127 * the card and net_device structures are linked to each other
2128 */
2129static struct spider_net_card *
2130spider_net_alloc_card(void)
2131{
2132 struct net_device *netdev;
2133 struct spider_net_card *card;
2134 size_t alloc_size;
2135
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;
2142
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);
2149
2150 return card;
2151}
2152
2153/**
2154 * spider_net_undo_pci_setup - releases PCI ressources
2155 * @card: card structure
2156 *
2157 * spider_net_undo_pci_setup releases the mapped regions
2158 */
2159static void
2160spider_net_undo_pci_setup(struct spider_net_card *card)
2161{
2162 iounmap(card->regs);
2163 pci_release_regions(card->pdev);
2164}
2165
2166/**
2167 * spider_net_setup_pci_dev - sets up the device in terms of PCI operations
2168 * @card: card structure
2169 * @pdev: PCI device
2170 *
2171 * Returns the card structure or NULL if any errors occur
2172 *
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.
2178 **/
2179static struct spider_net_card *
2180spider_net_setup_pci_dev(struct pci_dev *pdev)
2181{
2182 struct spider_net_card *card;
2183 unsigned long mmio_start, mmio_len;
2184
2185 if (pci_enable_device(pdev)) {
2186 pr_err("Couldn't enable PCI device\n");
2187 return NULL;
2188 }
2189
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;
2193 }
2194
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;
2198 }
2199
2200 pci_set_master(pdev);
2201
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;
2207 }
2208 card->pdev = pdev;
2209
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);
2213
2214 card->netdev->mem_start = mmio_start;
2215 card->netdev->mem_end = mmio_start + mmio_len;
2216 card->regs = ioremap(mmio_start, mmio_len);
2217
2218 if (!card->regs) {
2219 pr_err("Couldn't obtain PCI resources, aborting.\n");
2220 goto out_release_regions;
2221 }
2222
2223 return card;
2224
2225out_release_regions:
2226 pci_release_regions(pdev);
2227out_disable_dev:
2228 pci_disable_device(pdev);
2229 pci_set_drvdata(pdev, NULL);
2230 return NULL;
2231}
2232
2233/**
2234 * spider_net_probe - initialization of a device
2235 * @pdev: PCI device
2236 * @ent: entry in the device id list
2237 *
2238 * Returns 0 on success, <0 on failure
2239 *
2240 * spider_net_probe initializes pdev and registers a net_device
2241 * structure for it. After that, the device can be ifconfig'ed up
2242 **/
2243static int __devinit
2244spider_net_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2245{
2246 int err = -EIO;
2247 struct spider_net_card *card;
2248
2249 card = spider_net_setup_pci_dev(pdev);
2250 if (!card)
2251 goto out;
2252
2253 spider_net_workaround_rxramfull(card);
2254 spider_net_init_card(card);
2255
2256 err = spider_net_setup_phy(card);
2257 if (err)
2258 goto out_undo_pci;
2259
2260 err = spider_net_init_firmware(card);
2261 if (err)
2262 goto out_undo_pci;
2263
2264 err = spider_net_setup_netdev(card);
2265 if (err)
2266 goto out_undo_pci;
2267
2268 return 0;
2269
2270out_undo_pci:
2271 spider_net_undo_pci_setup(card);
2272 free_netdev(card->netdev);
2273out:
2274 return err;
2275}
2276
2277/**
2278 * spider_net_remove - removal of a device
2279 * @pdev: PCI device
2280 *
2281 * Returns 0 on success, <0 on failure
2282 *
2283 * spider_net_remove is called to remove the device and unregisters the
2284 * net_device
2285 **/
2286static void __devexit
2287spider_net_remove(struct pci_dev *pdev)
2288{
2289 struct net_device *netdev;
2290 struct spider_net_card *card;
2291
2292 netdev = pci_get_drvdata(pdev);
2293 card = netdev_priv(netdev);
2294
2295 wait_event(card->waitq,
2296 atomic_read(&card->tx_timeout_task_counter) == 0);
2297
2298 unregister_netdev(netdev);
Jens Osterkamp543cec52005-09-06 19:30:54 -07002299
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);
2305
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002306 spider_net_undo_pci_setup(card);
2307 free_netdev(netdev);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002308}
2309
2310static struct pci_driver spider_net_driver = {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002311 .name = spider_net_driver_name,
2312 .id_table = spider_net_pci_tbl,
2313 .probe = spider_net_probe,
2314 .remove = __devexit_p(spider_net_remove)
2315};
2316
2317/**
2318 * spider_net_init - init function when the driver is loaded
2319 *
2320 * spider_net_init registers the device driver
2321 */
2322static int __init spider_net_init(void)
2323{
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);
2327 }
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);
2331 }
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);
2335 }
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);
2339 }
2340
2341 return pci_register_driver(&spider_net_driver);
2342}
2343
2344/**
2345 * spider_net_cleanup - exit function when driver is unloaded
2346 *
2347 * spider_net_cleanup unregisters the device driver
2348 */
2349static void __exit spider_net_cleanup(void)
2350{
2351 pci_unregister_driver(&spider_net_driver);
2352}
2353
2354module_init(spider_net_init);
2355module_exit(spider_net_cleanup);