blob: 394339d5e87c0494c300fad7f60eaf9984e8c78f [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>
Jens Osterkampaaec0fa2005-09-05 15:19:29 -070025#include <linux/compiler.h>
26#include <linux/crc32.h>
27#include <linux/delay.h>
28#include <linux/etherdevice.h>
29#include <linux/ethtool.h>
30#include <linux/firmware.h>
31#include <linux/if_vlan.h>
Arnd Bergmann7c5c2202006-01-12 17:16:45 -050032#include <linux/in.h>
Jens Osterkampaaec0fa2005-09-05 15:19:29 -070033#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>
Arnd Bergmann11f1a522006-01-12 17:16:44 -050046#include <linux/vmalloc.h>
Jens Osterkampaaec0fa2005-09-05 15:19:29 -070047#include <linux/wait.h>
48#include <linux/workqueue.h>
49#include <asm/bitops.h>
50#include <asm/pci-bridge.h>
51#include <net/checksum.h>
52
53#include "spider_net.h"
54
55MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com> and Jens Osterkamp " \
56 "<Jens.Osterkamp@de.ibm.com>");
57MODULE_DESCRIPTION("Spider Southbridge Gigabit Ethernet driver");
58MODULE_LICENSE("GPL");
59
60static int rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_DEFAULT;
61static int tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_DEFAULT;
62
63module_param(rx_descriptors, int, 0644);
64module_param(tx_descriptors, int, 0644);
65
66MODULE_PARM_DESC(rx_descriptors, "number of descriptors used " \
67 "in rx chains");
68MODULE_PARM_DESC(tx_descriptors, "number of descriptors used " \
69 "in tx chain");
70
71char spider_net_driver_name[] = "spidernet";
72
73static struct pci_device_id spider_net_pci_tbl[] = {
74 { PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_SPIDER_NET,
75 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
76 { 0, }
77};
78
79MODULE_DEVICE_TABLE(pci, spider_net_pci_tbl);
80
81/**
82 * spider_net_read_reg - reads an SMMIO register of a card
83 * @card: device structure
84 * @reg: register to read from
85 *
86 * returns the content of the specified SMMIO register.
87 */
88static u32
89spider_net_read_reg(struct spider_net_card *card, u32 reg)
90{
91 u32 value;
92
93 value = readl(card->regs + reg);
94 value = le32_to_cpu(value);
95
96 return value;
97}
98
99/**
100 * spider_net_write_reg - writes to an SMMIO register of a card
101 * @card: device structure
102 * @reg: register to write to
103 * @value: value to write into the specified SMMIO register
104 */
105static void
106spider_net_write_reg(struct spider_net_card *card, u32 reg, u32 value)
107{
108 value = cpu_to_le32(value);
109 writel(value, card->regs + reg);
110}
111
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700112/** spider_net_write_phy - write to phy register
113 * @netdev: adapter to be written to
114 * @mii_id: id of MII
115 * @reg: PHY register
116 * @val: value to be written to phy register
117 *
118 * spider_net_write_phy_register writes to an arbitrary PHY
119 * register via the spider GPCWOPCMD register. We assume the queue does
120 * not run full (not more than 15 commands outstanding).
121 **/
122static void
123spider_net_write_phy(struct net_device *netdev, int mii_id,
124 int reg, int val)
125{
126 struct spider_net_card *card = netdev_priv(netdev);
127 u32 writevalue;
128
129 writevalue = ((u32)mii_id << 21) |
130 ((u32)reg << 16) | ((u32)val);
131
132 spider_net_write_reg(card, SPIDER_NET_GPCWOPCMD, writevalue);
133}
134
135/** spider_net_read_phy - read from phy register
136 * @netdev: network device to be read from
137 * @mii_id: id of MII
138 * @reg: PHY register
139 *
140 * Returns value read from PHY register
141 *
142 * spider_net_write_phy reads from an arbitrary PHY
143 * register via the spider GPCROPCMD register
144 **/
145static int
146spider_net_read_phy(struct net_device *netdev, int mii_id, int reg)
147{
148 struct spider_net_card *card = netdev_priv(netdev);
149 u32 readvalue;
150
151 readvalue = ((u32)mii_id << 21) | ((u32)reg << 16);
152 spider_net_write_reg(card, SPIDER_NET_GPCROPCMD, readvalue);
153
154 /* we don't use semaphores to wait for an SPIDER_NET_GPROPCMPINT
155 * interrupt, as we poll for the completion of the read operation
156 * in spider_net_read_phy. Should take about 50 us */
157 do {
158 readvalue = spider_net_read_reg(card, SPIDER_NET_GPCROPCMD);
159 } while (readvalue & SPIDER_NET_GPREXEC);
160
161 readvalue &= SPIDER_NET_GPRDAT_MASK;
162
163 return readvalue;
164}
165
166/**
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500167 * spider_net_rx_irq_off - switch off rx irq on this spider card
168 * @card: device structure
169 *
170 * switches off rx irq by masking them out in the GHIINTnMSK register
171 */
172static void
173spider_net_rx_irq_off(struct spider_net_card *card)
174{
175 u32 regvalue;
176
177 regvalue = SPIDER_NET_INT0_MASK_VALUE & (~SPIDER_NET_RXINT);
178 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, regvalue);
179}
180
181/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700182 * spider_net_rx_irq_on - switch on rx irq on this spider card
183 * @card: device structure
184 *
185 * switches on rx irq by enabling them in the GHIINTnMSK register
186 */
187static void
188spider_net_rx_irq_on(struct spider_net_card *card)
189{
190 u32 regvalue;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700191
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500192 regvalue = SPIDER_NET_INT0_MASK_VALUE | SPIDER_NET_RXINT;
193 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, regvalue);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700194}
195
196/**
197 * spider_net_set_promisc - sets the unicast address or the promiscuous mode
198 * @card: card structure
199 *
200 * spider_net_set_promisc sets the unicast destination address filter and
201 * thus either allows for non-promisc mode or promisc mode
202 */
203static void
204spider_net_set_promisc(struct spider_net_card *card)
205{
206 u32 macu, macl;
207 struct net_device *netdev = card->netdev;
208
209 if (netdev->flags & IFF_PROMISC) {
210 /* clear destination entry 0 */
211 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR, 0);
212 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR + 0x04, 0);
213 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R,
214 SPIDER_NET_PROMISC_VALUE);
215 } else {
216 macu = netdev->dev_addr[0];
217 macu <<= 8;
218 macu |= netdev->dev_addr[1];
219 memcpy(&macl, &netdev->dev_addr[2], sizeof(macl));
220
221 macu |= SPIDER_NET_UA_DESCR_VALUE;
222 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR, macu);
223 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR + 0x04, macl);
224 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R,
225 SPIDER_NET_NONPROMISC_VALUE);
226 }
227}
228
229/**
230 * spider_net_get_mac_address - read mac address from spider card
231 * @card: device structure
232 *
233 * reads MAC address from GMACUNIMACU and GMACUNIMACL registers
234 */
235static int
236spider_net_get_mac_address(struct net_device *netdev)
237{
238 struct spider_net_card *card = netdev_priv(netdev);
239 u32 macl, macu;
240
241 macl = spider_net_read_reg(card, SPIDER_NET_GMACUNIMACL);
242 macu = spider_net_read_reg(card, SPIDER_NET_GMACUNIMACU);
243
244 netdev->dev_addr[0] = (macu >> 24) & 0xff;
245 netdev->dev_addr[1] = (macu >> 16) & 0xff;
246 netdev->dev_addr[2] = (macu >> 8) & 0xff;
247 netdev->dev_addr[3] = macu & 0xff;
248 netdev->dev_addr[4] = (macl >> 8) & 0xff;
249 netdev->dev_addr[5] = macl & 0xff;
250
251 if (!is_valid_ether_addr(&netdev->dev_addr[0]))
252 return -EINVAL;
253
254 return 0;
255}
256
257/**
258 * spider_net_get_descr_status -- returns the status of a descriptor
259 * @descr: descriptor to look at
260 *
261 * returns the status as in the dmac_cmd_status field of the descriptor
262 */
263static enum spider_net_descr_status
264spider_net_get_descr_status(struct spider_net_descr *descr)
265{
266 u32 cmd_status;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500267
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700268 cmd_status = descr->dmac_cmd_status;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700269 cmd_status >>= SPIDER_NET_DESCR_IND_PROC_SHIFT;
270 /* no need to mask out any bits, as cmd_status is 32 bits wide only
271 * (and unsigned) */
272 return cmd_status;
273}
274
275/**
276 * spider_net_set_descr_status -- sets the status of a descriptor
277 * @descr: descriptor to change
278 * @status: status to set in the descriptor
279 *
280 * changes the status to the specified value. Doesn't change other bits
281 * in the status
282 */
283static void
284spider_net_set_descr_status(struct spider_net_descr *descr,
285 enum spider_net_descr_status status)
286{
287 u32 cmd_status;
288 /* read the status */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700289 cmd_status = descr->dmac_cmd_status;
290 /* clean the upper 4 bits */
291 cmd_status &= SPIDER_NET_DESCR_IND_PROC_MASKO;
292 /* add the status to it */
293 cmd_status |= ((u32)status)<<SPIDER_NET_DESCR_IND_PROC_SHIFT;
294 /* and write it back */
295 descr->dmac_cmd_status = cmd_status;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700296}
297
298/**
299 * spider_net_free_chain - free descriptor chain
300 * @card: card structure
301 * @chain: address of chain
302 *
303 */
304static void
305spider_net_free_chain(struct spider_net_card *card,
306 struct spider_net_descr_chain *chain)
307{
308 struct spider_net_descr *descr;
309
310 for (descr = chain->tail; !descr->bus_addr; descr = descr->next) {
311 pci_unmap_single(card->pdev, descr->bus_addr,
312 SPIDER_NET_DESCR_SIZE, PCI_DMA_BIDIRECTIONAL);
313 descr->bus_addr = 0;
314 }
315}
316
317/**
318 * spider_net_init_chain - links descriptor chain
319 * @card: card structure
320 * @chain: address of chain
321 * @start_descr: address of descriptor array
322 * @no: number of descriptors
323 *
324 * we manage a circular list that mirrors the hardware structure,
325 * except that the hardware uses bus addresses.
326 *
327 * returns 0 on success, <0 on failure
328 */
329static int
330spider_net_init_chain(struct spider_net_card *card,
331 struct spider_net_descr_chain *chain,
332 struct spider_net_descr *start_descr, int no)
333{
334 int i;
335 struct spider_net_descr *descr;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500336 dma_addr_t buf;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700337
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500338 atomic_set(&card->rx_chain_refill,0);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700339
340 descr = start_descr;
341 memset(descr, 0, sizeof(*descr) * no);
342
343 /* set up the hardware pointers in each descriptor */
344 for (i=0; i<no; i++, descr++) {
345 spider_net_set_descr_status(descr, SPIDER_NET_DESCR_NOT_IN_USE);
346
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500347 buf = pci_map_single(card->pdev, descr,
348 SPIDER_NET_DESCR_SIZE,
349 PCI_DMA_BIDIRECTIONAL);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700350
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500351 if (buf == DMA_ERROR_CODE)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700352 goto iommu_error;
353
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500354 descr->bus_addr = buf;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700355 descr->next = descr + 1;
356 descr->prev = descr - 1;
357
358 }
359 /* do actual circular list */
360 (descr-1)->next = start_descr;
361 start_descr->prev = descr-1;
362
363 descr = start_descr;
364 for (i=0; i < no; i++, descr++) {
365 descr->next_descr_addr = descr->next->bus_addr;
366 }
367
368 chain->head = start_descr;
369 chain->tail = start_descr;
370
371 return 0;
372
373iommu_error:
374 descr = start_descr;
375 for (i=0; i < no; i++, descr++)
376 if (descr->bus_addr)
377 pci_unmap_single(card->pdev, descr->bus_addr,
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500378 SPIDER_NET_DESCR_SIZE,
379 PCI_DMA_BIDIRECTIONAL);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700380 return -ENOMEM;
381}
382
383/**
384 * spider_net_free_rx_chain_contents - frees descr contents in rx chain
385 * @card: card structure
386 *
387 * returns 0 on success, <0 on failure
388 */
389static void
390spider_net_free_rx_chain_contents(struct spider_net_card *card)
391{
392 struct spider_net_descr *descr;
393
394 descr = card->rx_chain.head;
395 while (descr->next != card->rx_chain.head) {
396 if (descr->skb) {
397 dev_kfree_skb(descr->skb);
398 pci_unmap_single(card->pdev, descr->buf_addr,
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500399 SPIDER_NET_MAX_FRAME,
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700400 PCI_DMA_BIDIRECTIONAL);
401 }
402 descr = descr->next;
403 }
404}
405
406/**
407 * spider_net_prepare_rx_descr - reinitializes a rx descriptor
408 * @card: card structure
409 * @descr: descriptor to re-init
410 *
411 * return 0 on succes, <0 on failure
412 *
413 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
414 * Activate the descriptor state-wise
415 */
416static int
417spider_net_prepare_rx_descr(struct spider_net_card *card,
418 struct spider_net_descr *descr)
419{
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500420 dma_addr_t buf;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700421 int error = 0;
422 int offset;
423 int bufsize;
424
425 /* we need to round up the buffer size to a multiple of 128 */
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500426 bufsize = (SPIDER_NET_MAX_FRAME + SPIDER_NET_RXBUF_ALIGN - 1) &
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700427 (~(SPIDER_NET_RXBUF_ALIGN - 1));
428
429 /* and we need to have it 128 byte aligned, therefore we allocate a
430 * bit more */
431 /* allocate an skb */
432 descr->skb = dev_alloc_skb(bufsize + SPIDER_NET_RXBUF_ALIGN - 1);
433 if (!descr->skb) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500434 if (netif_msg_rx_err(card) && net_ratelimit())
435 pr_err("Not enough memory to allocate rx buffer\n");
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700436 return -ENOMEM;
437 }
438 descr->buf_size = bufsize;
439 descr->result_size = 0;
440 descr->valid_size = 0;
441 descr->data_status = 0;
442 descr->data_error = 0;
443
444 offset = ((unsigned long)descr->skb->data) &
445 (SPIDER_NET_RXBUF_ALIGN - 1);
446 if (offset)
447 skb_reserve(descr->skb, SPIDER_NET_RXBUF_ALIGN - offset);
448 /* io-mmu-map the skb */
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500449 buf = pci_map_single(card->pdev, descr->skb->data,
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500450 SPIDER_NET_MAX_FRAME, PCI_DMA_BIDIRECTIONAL);
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500451 descr->buf_addr = buf;
452 if (buf == DMA_ERROR_CODE) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700453 dev_kfree_skb_any(descr->skb);
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500454 if (netif_msg_rx_err(card) && net_ratelimit())
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700455 pr_err("Could not iommu-map rx buffer\n");
456 spider_net_set_descr_status(descr, SPIDER_NET_DESCR_NOT_IN_USE);
457 } else {
458 descr->dmac_cmd_status = SPIDER_NET_DMAC_RX_CARDOWNED;
459 }
460
461 return error;
462}
463
464/**
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500465 * spider_net_enable_rxchtails - sets RX dmac chain tail addresses
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700466 * @card: card structure
467 *
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500468 * spider_net_enable_rxchtails sets the RX DMAC chain tail adresses in the
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700469 * chip by writing to the appropriate register. DMA is enabled in
470 * spider_net_enable_rxdmac.
471 */
472static void
473spider_net_enable_rxchtails(struct spider_net_card *card)
474{
475 /* assume chain is aligned correctly */
476 spider_net_write_reg(card, SPIDER_NET_GDADCHA ,
477 card->rx_chain.tail->bus_addr);
478}
479
480/**
481 * spider_net_enable_rxdmac - enables a receive DMA controller
482 * @card: card structure
483 *
484 * spider_net_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
485 * in the GDADMACCNTR register
486 */
487static void
488spider_net_enable_rxdmac(struct spider_net_card *card)
489{
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500490 wmb();
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700491 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
492 SPIDER_NET_DMA_RX_VALUE);
493}
494
495/**
496 * spider_net_refill_rx_chain - refills descriptors/skbs in the rx chains
497 * @card: card structure
498 *
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500499 * refills descriptors in the rx chain: allocates skbs and iommu-maps them.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700500 */
501static void
502spider_net_refill_rx_chain(struct spider_net_card *card)
503{
504 struct spider_net_descr_chain *chain;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700505
506 chain = &card->rx_chain;
507
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500508 /* one context doing the refill (and a second context seeing that
509 * and omitting it) is ok. If called by NAPI, we'll be called again
510 * as spider_net_decode_one_descr is called several times. If some
511 * interrupt calls us, the NAPI is about to clean up anyway. */
512 if (atomic_inc_return(&card->rx_chain_refill) == 1)
513 while (spider_net_get_descr_status(chain->head) ==
514 SPIDER_NET_DESCR_NOT_IN_USE) {
515 if (spider_net_prepare_rx_descr(card, chain->head))
516 break;
517 chain->head = chain->head->next;
518 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700519
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500520 atomic_dec(&card->rx_chain_refill);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700521}
522
523/**
524 * spider_net_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
525 * @card: card structure
526 *
527 * returns 0 on success, <0 on failure
528 */
529static int
530spider_net_alloc_rx_skbs(struct spider_net_card *card)
531{
532 int result;
533 struct spider_net_descr_chain *chain;
534
535 result = -ENOMEM;
536
537 chain = &card->rx_chain;
538 /* put at least one buffer into the chain. if this fails,
539 * we've got a problem. if not, spider_net_refill_rx_chain
540 * will do the rest at the end of this function */
541 if (spider_net_prepare_rx_descr(card, chain->head))
542 goto error;
543 else
544 chain->head = chain->head->next;
545
546 /* this will allocate the rest of the rx buffers; if not, it's
547 * business as usual later on */
548 spider_net_refill_rx_chain(card);
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500549 spider_net_enable_rxdmac(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700550 return 0;
551
552error:
553 spider_net_free_rx_chain_contents(card);
554 return result;
555}
556
557/**
558 * spider_net_release_tx_descr - processes a used tx descriptor
559 * @card: card structure
560 * @descr: descriptor to release
561 *
562 * releases a used tx descriptor (unmapping, freeing of skb)
563 */
564static void
565spider_net_release_tx_descr(struct spider_net_card *card,
566 struct spider_net_descr *descr)
567{
568 struct sk_buff *skb;
569
570 /* unmap the skb */
571 skb = descr->skb;
572 pci_unmap_single(card->pdev, descr->buf_addr, skb->len,
573 PCI_DMA_BIDIRECTIONAL);
574
575 dev_kfree_skb_any(skb);
576
577 /* set status to not used */
578 spider_net_set_descr_status(descr, SPIDER_NET_DESCR_NOT_IN_USE);
579}
580
581/**
582 * spider_net_release_tx_chain - processes sent tx descriptors
583 * @card: adapter structure
584 * @brutal: if set, don't care about whether descriptor seems to be in use
585 *
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500586 * returns 0 if the tx ring is empty, otherwise 1.
587 *
588 * spider_net_release_tx_chain releases the tx descriptors that spider has
589 * finished with (if non-brutal) or simply release tx descriptors (if brutal).
590 * If some other context is calling this function, we return 1 so that we're
591 * scheduled again (if we were scheduled) and will not loose initiative.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700592 */
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500593static int
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700594spider_net_release_tx_chain(struct spider_net_card *card, int brutal)
595{
596 struct spider_net_descr_chain *tx_chain = &card->tx_chain;
597 enum spider_net_descr_status status;
598
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500599 if (atomic_inc_return(&card->tx_chain_release) != 1) {
600 atomic_dec(&card->tx_chain_release);
601 return 1;
602 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700603
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700604 for (;;) {
605 status = spider_net_get_descr_status(tx_chain->tail);
606 switch (status) {
607 case SPIDER_NET_DESCR_CARDOWNED:
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500608 if (!brutal)
609 goto out;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700610 /* fallthrough, if we release the descriptors
611 * brutally (then we don't care about
612 * SPIDER_NET_DESCR_CARDOWNED) */
613 case SPIDER_NET_DESCR_RESPONSE_ERROR:
614 case SPIDER_NET_DESCR_PROTECTION_ERROR:
615 case SPIDER_NET_DESCR_FORCE_END:
616 if (netif_msg_tx_err(card))
617 pr_err("%s: forcing end of tx descriptor "
618 "with status x%02x\n",
619 card->netdev->name, status);
620 card->netdev_stats.tx_dropped++;
621 break;
622
623 case SPIDER_NET_DESCR_COMPLETE:
624 card->netdev_stats.tx_packets++;
625 card->netdev_stats.tx_bytes +=
626 tx_chain->tail->skb->len;
627 break;
628
629 default: /* any other value (== SPIDER_NET_DESCR_NOT_IN_USE) */
630 goto out;
631 }
632 spider_net_release_tx_descr(card, tx_chain->tail);
633 tx_chain->tail = tx_chain->tail->next;
634 }
635out:
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500636 atomic_dec(&card->tx_chain_release);
637
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700638 netif_wake_queue(card->netdev);
639
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500640 if (status == SPIDER_NET_DESCR_CARDOWNED)
641 return 1;
642 return 0;
643}
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700644
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500645/**
646 * spider_net_cleanup_tx_ring - cleans up the TX ring
647 * @card: card structure
648 *
649 * spider_net_cleanup_tx_ring is called by the tx_timer (as we don't use
650 * interrupts to cleanup our TX ring) and returns sent packets to the stack
651 * by freeing them
652 */
653static void
654spider_net_cleanup_tx_ring(struct spider_net_card *card)
655{
656 if ( (spider_net_release_tx_chain(card, 0)) &&
657 (card->netdev->flags & IFF_UP) ) {
658 mod_timer(&card->tx_timer, jiffies + SPIDER_NET_TX_TIMER);
659 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700660}
661
662/**
663 * spider_net_get_multicast_hash - generates hash for multicast filter table
664 * @addr: multicast address
665 *
666 * returns the hash value.
667 *
668 * spider_net_get_multicast_hash calculates a hash value for a given multicast
669 * address, that is used to set the multicast filter tables
670 */
671static u8
672spider_net_get_multicast_hash(struct net_device *netdev, __u8 *addr)
673{
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700674 u32 crc;
675 u8 hash;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500676 char addr_for_crc[ETH_ALEN] = { 0, };
677 int i, bit;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700678
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500679 for (i = 0; i < ETH_ALEN * 8; i++) {
680 bit = (addr[i / 8] >> (i % 8)) & 1;
681 addr_for_crc[ETH_ALEN - 1 - i / 8] += bit << (7 - (i % 8));
682 }
683
684 crc = crc32_be(~0, addr_for_crc, netdev->addr_len);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700685
686 hash = (crc >> 27);
687 hash <<= 3;
688 hash |= crc & 7;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500689 hash &= 0xff;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700690
691 return hash;
692}
693
694/**
695 * spider_net_set_multi - sets multicast addresses and promisc flags
696 * @netdev: interface device structure
697 *
698 * spider_net_set_multi configures multicast addresses as needed for the
699 * netdev interface. It also sets up multicast, allmulti and promisc
700 * flags appropriately
701 */
702static void
703spider_net_set_multi(struct net_device *netdev)
704{
705 struct dev_mc_list *mc;
706 u8 hash;
707 int i;
708 u32 reg;
709 struct spider_net_card *card = netdev_priv(netdev);
710 unsigned long bitmask[SPIDER_NET_MULTICAST_HASHES / BITS_PER_LONG] =
711 {0, };
712
713 spider_net_set_promisc(card);
714
715 if (netdev->flags & IFF_ALLMULTI) {
716 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES; i++) {
717 set_bit(i, bitmask);
718 }
719 goto write_hash;
720 }
721
722 /* well, we know, what the broadcast hash value is: it's xfd
723 hash = spider_net_get_multicast_hash(netdev, netdev->broadcast); */
724 set_bit(0xfd, bitmask);
725
726 for (mc = netdev->mc_list; mc; mc = mc->next) {
727 hash = spider_net_get_multicast_hash(netdev, mc->dmi_addr);
728 set_bit(hash, bitmask);
729 }
730
731write_hash:
732 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES / 4; i++) {
733 reg = 0;
734 if (test_bit(i * 4, bitmask))
735 reg += 0x08;
736 reg <<= 8;
737 if (test_bit(i * 4 + 1, bitmask))
738 reg += 0x08;
739 reg <<= 8;
740 if (test_bit(i * 4 + 2, bitmask))
741 reg += 0x08;
742 reg <<= 8;
743 if (test_bit(i * 4 + 3, bitmask))
744 reg += 0x08;
745
746 spider_net_write_reg(card, SPIDER_NET_GMRMHFILnR + i * 4, reg);
747 }
748}
749
750/**
751 * spider_net_disable_rxdmac - disables the receive DMA controller
752 * @card: card structure
753 *
754 * spider_net_disable_rxdmac terminates processing on the DMA controller by
755 * turing off DMA and issueing a force end
756 */
757static void
758spider_net_disable_rxdmac(struct spider_net_card *card)
759{
760 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
761 SPIDER_NET_DMA_RX_FEND_VALUE);
762}
763
764/**
765 * spider_net_stop - called upon ifconfig down
766 * @netdev: interface device structure
767 *
768 * always returns 0
769 */
770int
771spider_net_stop(struct net_device *netdev)
772{
773 struct spider_net_card *card = netdev_priv(netdev);
774
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500775 tasklet_kill(&card->rxram_full_tl);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700776 netif_poll_disable(netdev);
777 netif_carrier_off(netdev);
778 netif_stop_queue(netdev);
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500779 del_timer_sync(&card->tx_timer);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700780
781 /* disable/mask all interrupts */
782 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, 0);
783 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK, 0);
784 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK, 0);
785
Jens Osterkamp543cec52005-09-06 19:30:54 -0700786 /* free_irq(netdev->irq, netdev);*/
787 free_irq(to_pci_dev(netdev->class_dev.dev)->irq, netdev);
788
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700789 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
790 SPIDER_NET_DMA_TX_FEND_VALUE);
791
792 /* turn off DMA, force end */
793 spider_net_disable_rxdmac(card);
794
795 /* release chains */
796 spider_net_release_tx_chain(card, 1);
797
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700798 spider_net_free_chain(card, &card->tx_chain);
799 spider_net_free_chain(card, &card->rx_chain);
800
801 return 0;
802}
803
804/**
805 * spider_net_get_next_tx_descr - returns the next available tx descriptor
806 * @card: device structure to get descriptor from
807 *
808 * returns the address of the next descriptor, or NULL if not available.
809 */
810static struct spider_net_descr *
811spider_net_get_next_tx_descr(struct spider_net_card *card)
812{
813 /* check, if head points to not-in-use descr */
814 if ( spider_net_get_descr_status(card->tx_chain.head) ==
815 SPIDER_NET_DESCR_NOT_IN_USE ) {
816 return card->tx_chain.head;
817 } else {
818 return NULL;
819 }
820}
821
822/**
823 * spider_net_set_txdescr_cmdstat - sets the tx descriptor command field
824 * @descr: descriptor structure to fill out
825 * @skb: packet to consider
826 *
827 * fills out the command and status field of the descriptor structure,
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500828 * depending on hardware checksum settings.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700829 */
830static void
831spider_net_set_txdescr_cmdstat(struct spider_net_descr *descr,
832 struct sk_buff *skb)
833{
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500834 /* make sure the other fields in the descriptor are written */
835 wmb();
836
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700837 if (skb->ip_summed != CHECKSUM_HW) {
838 descr->dmac_cmd_status = SPIDER_NET_DMAC_CMDSTAT_NOCS;
839 return;
840 }
841
842 /* is packet ip?
843 * if yes: tcp? udp? */
844 if (skb->protocol == htons(ETH_P_IP)) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500845 if (skb->nh.iph->protocol == IPPROTO_TCP)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700846 descr->dmac_cmd_status = SPIDER_NET_DMAC_CMDSTAT_TCPCS;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500847 else if (skb->nh.iph->protocol == IPPROTO_UDP)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700848 descr->dmac_cmd_status = SPIDER_NET_DMAC_CMDSTAT_UDPCS;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500849 else /* the stack should checksum non-tcp and non-udp
850 packets on his own: NETIF_F_IP_CSUM */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700851 descr->dmac_cmd_status = SPIDER_NET_DMAC_CMDSTAT_NOCS;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700852 }
853}
854
855/**
856 * spider_net_prepare_tx_descr - fill tx descriptor with skb data
857 * @card: card structure
858 * @descr: descriptor structure to fill out
859 * @skb: packet to use
860 *
861 * returns 0 on success, <0 on failure.
862 *
863 * fills out the descriptor structure with skb data and len. Copies data,
864 * if needed (32bit DMA!)
865 */
866static int
867spider_net_prepare_tx_descr(struct spider_net_card *card,
868 struct spider_net_descr *descr,
869 struct sk_buff *skb)
870{
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500871 dma_addr_t buf;
872
873 buf = pci_map_single(card->pdev, skb->data,
874 skb->len, PCI_DMA_BIDIRECTIONAL);
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500875 if (buf == DMA_ERROR_CODE) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500876 if (netif_msg_tx_err(card) && net_ratelimit())
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700877 pr_err("could not iommu-map packet (%p, %i). "
878 "Dropping packet\n", skb->data, skb->len);
879 return -ENOMEM;
880 }
881
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500882 descr->buf_addr = buf;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700883 descr->buf_size = skb->len;
884 descr->skb = skb;
885 descr->data_status = 0;
886
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700887 spider_net_set_txdescr_cmdstat(descr,skb);
888
889 return 0;
890}
891
892/**
893 * spider_net_kick_tx_dma - enables TX DMA processing
894 * @card: card structure
895 * @descr: descriptor address to enable TX processing at
896 *
897 * spider_net_kick_tx_dma writes the current tx chain head as start address
898 * of the tx descriptor chain and enables the transmission DMA engine
899 */
900static void
901spider_net_kick_tx_dma(struct spider_net_card *card,
902 struct spider_net_descr *descr)
903{
904 /* this is the only descriptor in the output chain.
905 * Enable TX DMA */
906
907 spider_net_write_reg(card, SPIDER_NET_GDTDCHA,
908 descr->bus_addr);
909
910 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
911 SPIDER_NET_DMA_TX_VALUE);
912}
913
914/**
915 * spider_net_xmit - transmits a frame over the device
916 * @skb: packet to send out
917 * @netdev: interface device structure
918 *
919 * returns 0 on success, <0 on failure
920 */
921static int
922spider_net_xmit(struct sk_buff *skb, struct net_device *netdev)
923{
924 struct spider_net_card *card = netdev_priv(netdev);
925 struct spider_net_descr *descr;
926 int result;
927
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500928 spider_net_release_tx_chain(card, 0);
929
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700930 descr = spider_net_get_next_tx_descr(card);
931
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500932 if (!descr)
933 goto error;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700934
935 result = spider_net_prepare_tx_descr(card, descr, skb);
936 if (result)
937 goto error;
938
939 card->tx_chain.head = card->tx_chain.head->next;
940
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700941 if (spider_net_get_descr_status(descr->prev) !=
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500942 SPIDER_NET_DESCR_CARDOWNED) {
943 /* make sure the current descriptor is in memory. Then
944 * kicking it on again makes sense, if the previous is not
945 * card-owned anymore. Check the previous descriptor twice
946 * to omit an mb() in heavy traffic cases */
947 mb();
948 if (spider_net_get_descr_status(descr->prev) !=
949 SPIDER_NET_DESCR_CARDOWNED)
950 spider_net_kick_tx_dma(card, descr);
951 }
952
953 mod_timer(&card->tx_timer, jiffies + SPIDER_NET_TX_TIMER);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700954
955 return NETDEV_TX_OK;
956
957error:
958 card->netdev_stats.tx_dropped++;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500959 return NETDEV_TX_BUSY;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700960}
961
962/**
963 * spider_net_do_ioctl - called for device ioctls
964 * @netdev: interface device structure
965 * @ifr: request parameter structure for ioctl
966 * @cmd: command code for ioctl
967 *
968 * returns 0 on success, <0 on failure. Currently, we have no special ioctls.
969 * -EOPNOTSUPP is returned, if an unknown ioctl was requested
970 */
971static int
972spider_net_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
973{
974 switch (cmd) {
975 default:
976 return -EOPNOTSUPP;
977 }
978}
979
980/**
981 * spider_net_pass_skb_up - takes an skb from a descriptor and passes it on
982 * @descr: descriptor to process
983 * @card: card structure
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500984 * @napi: whether caller is in NAPI context
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700985 *
986 * returns 1 on success, 0 if no packet was passed to the stack
987 *
988 * iommu-unmaps the skb, fills out skb structure and passes the data to the
989 * stack. The descriptor state is not changed.
990 */
991static int
992spider_net_pass_skb_up(struct spider_net_descr *descr,
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500993 struct spider_net_card *card, int napi)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700994{
995 struct sk_buff *skb;
996 struct net_device *netdev;
997 u32 data_status, data_error;
998
999 data_status = descr->data_status;
1000 data_error = descr->data_error;
1001
1002 netdev = card->netdev;
1003
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001004 /* unmap descriptor */
1005 pci_unmap_single(card->pdev, descr->buf_addr, SPIDER_NET_MAX_FRAME,
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001006 PCI_DMA_BIDIRECTIONAL);
1007
1008 /* the cases we'll throw away the packet immediately */
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001009 if (data_error & SPIDER_NET_DESTROY_RX_FLAGS) {
1010 if (netif_msg_rx_err(card))
1011 pr_err("error in received descriptor found, "
1012 "data_status=x%08x, data_error=x%08x\n",
1013 data_status, data_error);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001014 return 0;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001015 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001016
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001017 skb = descr->skb;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001018 skb->dev = netdev;
1019 skb_put(skb, descr->valid_size);
1020
1021 /* the card seems to add 2 bytes of junk in front
1022 * of the ethernet frame */
1023#define SPIDER_MISALIGN 2
1024 skb_pull(skb, SPIDER_MISALIGN);
1025 skb->protocol = eth_type_trans(skb, netdev);
1026
1027 /* checksum offload */
1028 if (card->options.rx_csum) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001029 if ( ( (data_status & SPIDER_NET_DATA_STATUS_CKSUM_MASK) ==
1030 SPIDER_NET_DATA_STATUS_CKSUM_MASK) &&
1031 !(data_error & SPIDER_NET_DATA_ERR_CKSUM_MASK))
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001032 skb->ip_summed = CHECKSUM_UNNECESSARY;
1033 else
1034 skb->ip_summed = CHECKSUM_NONE;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001035 } else
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001036 skb->ip_summed = CHECKSUM_NONE;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001037
1038 if (data_status & SPIDER_NET_VLAN_PACKET) {
1039 /* further enhancements: HW-accel VLAN
1040 * vlan_hwaccel_receive_skb
1041 */
1042 }
1043
1044 /* pass skb up to stack */
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001045 if (napi)
1046 netif_receive_skb(skb);
1047 else
1048 netif_rx_ni(skb);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001049
1050 /* update netdevice statistics */
1051 card->netdev_stats.rx_packets++;
1052 card->netdev_stats.rx_bytes += skb->len;
1053
1054 return 1;
1055}
1056
1057/**
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001058 * spider_net_decode_one_descr - processes an rx descriptor
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001059 * @card: card structure
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001060 * @napi: whether caller is in NAPI context
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001061 *
1062 * returns 1 if a packet has been sent to the stack, otherwise 0
1063 *
1064 * processes an rx descriptor by iommu-unmapping the data buffer and passing
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001065 * the packet up to the stack. This function is called in softirq
1066 * context, e.g. either bottom half from interrupt or NAPI polling context
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001067 */
1068static int
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001069spider_net_decode_one_descr(struct spider_net_card *card, int napi)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001070{
1071 enum spider_net_descr_status status;
1072 struct spider_net_descr *descr;
1073 struct spider_net_descr_chain *chain;
1074 int result;
1075
1076 chain = &card->rx_chain;
1077 descr = chain->tail;
1078
1079 status = spider_net_get_descr_status(descr);
1080
1081 if (status == SPIDER_NET_DESCR_CARDOWNED) {
1082 /* nothing in the descriptor yet */
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001083 result=0;
1084 goto out;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001085 }
1086
1087 if (status == SPIDER_NET_DESCR_NOT_IN_USE) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001088 /* not initialized yet, the ring must be empty */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001089 spider_net_refill_rx_chain(card);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001090 spider_net_enable_rxdmac(card);
1091 result=0;
1092 goto out;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001093 }
1094
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001095 /* descriptor definitively used -- move on tail */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001096 chain->tail = descr->next;
1097
1098 result = 0;
1099 if ( (status == SPIDER_NET_DESCR_RESPONSE_ERROR) ||
1100 (status == SPIDER_NET_DESCR_PROTECTION_ERROR) ||
1101 (status == SPIDER_NET_DESCR_FORCE_END) ) {
1102 if (netif_msg_rx_err(card))
1103 pr_err("%s: dropping RX descriptor with state %d\n",
1104 card->netdev->name, status);
1105 card->netdev_stats.rx_dropped++;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001106 pci_unmap_single(card->pdev, descr->buf_addr,
1107 SPIDER_NET_MAX_FRAME, PCI_DMA_BIDIRECTIONAL);
1108 dev_kfree_skb_irq(descr->skb);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001109 goto refill;
1110 }
1111
1112 if ( (status != SPIDER_NET_DESCR_COMPLETE) &&
1113 (status != SPIDER_NET_DESCR_FRAME_END) ) {
1114 if (netif_msg_rx_err(card))
1115 pr_err("%s: RX descriptor with state %d\n",
1116 card->netdev->name, status);
1117 goto refill;
1118 }
1119
1120 /* ok, we've got a packet in descr */
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001121 result = spider_net_pass_skb_up(descr, card, napi);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001122refill:
1123 spider_net_set_descr_status(descr, SPIDER_NET_DESCR_NOT_IN_USE);
1124 /* change the descriptor state: */
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001125 if (!napi)
1126 spider_net_refill_rx_chain(card);
1127out:
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001128 return result;
1129}
1130
1131/**
1132 * spider_net_poll - NAPI poll function called by the stack to return packets
1133 * @netdev: interface device structure
1134 * @budget: number of packets we can pass to the stack at most
1135 *
1136 * returns 0 if no more packets available to the driver/stack. Returns 1,
1137 * if the quota is exceeded, but the driver has still packets.
1138 *
1139 * spider_net_poll returns all packets from the rx descriptors to the stack
1140 * (using netif_receive_skb). If all/enough packets are up, the driver
1141 * reenables interrupts and returns 0. If not, 1 is returned.
1142 */
1143static int
1144spider_net_poll(struct net_device *netdev, int *budget)
1145{
1146 struct spider_net_card *card = netdev_priv(netdev);
1147 int packets_to_do, packets_done = 0;
1148 int no_more_packets = 0;
1149
1150 packets_to_do = min(*budget, netdev->quota);
1151
1152 while (packets_to_do) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001153 if (spider_net_decode_one_descr(card, 1)) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001154 packets_done++;
1155 packets_to_do--;
1156 } else {
1157 /* no more packets for the stack */
1158 no_more_packets = 1;
1159 break;
1160 }
1161 }
1162
1163 netdev->quota -= packets_done;
1164 *budget -= packets_done;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001165 spider_net_refill_rx_chain(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001166
1167 /* if all packets are in the stack, enable interrupts and return 0 */
1168 /* if not, return 1 */
1169 if (no_more_packets) {
1170 netif_rx_complete(netdev);
1171 spider_net_rx_irq_on(card);
1172 return 0;
1173 }
1174
1175 return 1;
1176}
1177
1178/**
1179 * spider_net_vlan_rx_reg - initializes VLAN structures in the driver and card
1180 * @netdev: interface device structure
1181 * @grp: vlan_group structure that is registered (NULL on destroying interface)
1182 */
1183static void
1184spider_net_vlan_rx_reg(struct net_device *netdev, struct vlan_group *grp)
1185{
1186 /* further enhancement... yet to do */
1187 return;
1188}
1189
1190/**
1191 * spider_net_vlan_rx_add - adds VLAN id to the card filter
1192 * @netdev: interface device structure
1193 * @vid: VLAN id to add
1194 */
1195static void
1196spider_net_vlan_rx_add(struct net_device *netdev, uint16_t vid)
1197{
1198 /* further enhancement... yet to do */
1199 /* add vid to card's VLAN filter table */
1200 return;
1201}
1202
1203/**
1204 * spider_net_vlan_rx_kill - removes VLAN id to the card filter
1205 * @netdev: interface device structure
1206 * @vid: VLAN id to remove
1207 */
1208static void
1209spider_net_vlan_rx_kill(struct net_device *netdev, uint16_t vid)
1210{
1211 /* further enhancement... yet to do */
1212 /* remove vid from card's VLAN filter table */
1213}
1214
1215/**
1216 * spider_net_get_stats - get interface statistics
1217 * @netdev: interface device structure
1218 *
1219 * returns the interface statistics residing in the spider_net_card struct
1220 */
1221static struct net_device_stats *
1222spider_net_get_stats(struct net_device *netdev)
1223{
1224 struct spider_net_card *card = netdev_priv(netdev);
1225 struct net_device_stats *stats = &card->netdev_stats;
1226 return stats;
1227}
1228
1229/**
1230 * spider_net_change_mtu - changes the MTU of an interface
1231 * @netdev: interface device structure
1232 * @new_mtu: new MTU value
1233 *
1234 * returns 0 on success, <0 on failure
1235 */
1236static int
1237spider_net_change_mtu(struct net_device *netdev, int new_mtu)
1238{
1239 /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
1240 * and mtu is outbound only anyway */
1241 if ( (new_mtu < SPIDER_NET_MIN_MTU ) ||
1242 (new_mtu > SPIDER_NET_MAX_MTU) )
1243 return -EINVAL;
1244 netdev->mtu = new_mtu;
1245 return 0;
1246}
1247
1248/**
1249 * spider_net_set_mac - sets the MAC of an interface
1250 * @netdev: interface device structure
1251 * @ptr: pointer to new MAC address
1252 *
1253 * Returns 0 on success, <0 on failure. Currently, we don't support this
1254 * and will always return EOPNOTSUPP.
1255 */
1256static int
1257spider_net_set_mac(struct net_device *netdev, void *p)
1258{
1259 struct spider_net_card *card = netdev_priv(netdev);
Jens Osterkamp054034d2005-09-06 19:30:55 -07001260 u32 macl, macu, regvalue;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001261 struct sockaddr *addr = p;
1262
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001263 if (!is_valid_ether_addr(addr->sa_data))
1264 return -EADDRNOTAVAIL;
1265
Jens Osterkamp054034d2005-09-06 19:30:55 -07001266 /* switch off GMACTPE and GMACRPE */
1267 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1268 regvalue &= ~((1 << 5) | (1 << 6));
1269 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1270
1271 /* write mac */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001272 macu = (addr->sa_data[0]<<24) + (addr->sa_data[1]<<16) +
1273 (addr->sa_data[2]<<8) + (addr->sa_data[3]);
1274 macl = (addr->sa_data[4]<<8) + (addr->sa_data[5]);
1275 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACU, macu);
1276 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACL, macl);
1277
Jens Osterkamp054034d2005-09-06 19:30:55 -07001278 /* switch GMACTPE and GMACRPE back on */
1279 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1280 regvalue |= ((1 << 5) | (1 << 6));
1281 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1282
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001283 spider_net_set_promisc(card);
1284
1285 /* look up, whether we have been successful */
1286 if (spider_net_get_mac_address(netdev))
1287 return -EADDRNOTAVAIL;
1288 if (memcmp(netdev->dev_addr,addr->sa_data,netdev->addr_len))
1289 return -EADDRNOTAVAIL;
1290
1291 return 0;
1292}
1293
1294/**
1295 * spider_net_enable_txdmac - enables a TX DMA controller
1296 * @card: card structure
1297 *
1298 * spider_net_enable_txdmac enables the TX DMA controller by setting the
1299 * descriptor chain tail address
1300 */
1301static void
1302spider_net_enable_txdmac(struct spider_net_card *card)
1303{
1304 /* assume chain is aligned correctly */
1305 spider_net_write_reg(card, SPIDER_NET_GDTDCHA,
1306 card->tx_chain.tail->bus_addr);
1307}
1308
1309/**
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001310 * spider_net_handle_rxram_full - cleans up RX ring upon RX RAM full interrupt
1311 * @card: card structure
1312 *
1313 * spider_net_handle_rxram_full empties the RX ring so that spider can put
1314 * more packets in it and empty its RX RAM. This is called in bottom half
1315 * context
1316 */
1317static void
1318spider_net_handle_rxram_full(struct spider_net_card *card)
1319{
1320 while (spider_net_decode_one_descr(card, 0))
1321 ;
1322 spider_net_enable_rxchtails(card);
1323 spider_net_enable_rxdmac(card);
1324 netif_rx_schedule(card->netdev);
1325}
1326
1327/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001328 * spider_net_handle_error_irq - handles errors raised by an interrupt
1329 * @card: card structure
1330 * @status_reg: interrupt status register 0 (GHIINT0STS)
1331 *
1332 * spider_net_handle_error_irq treats or ignores all error conditions
1333 * found when an interrupt is presented
1334 */
1335static void
1336spider_net_handle_error_irq(struct spider_net_card *card, u32 status_reg)
1337{
1338 u32 error_reg1, error_reg2;
1339 u32 i;
1340 int show_error = 1;
1341
1342 error_reg1 = spider_net_read_reg(card, SPIDER_NET_GHIINT1STS);
1343 error_reg2 = spider_net_read_reg(card, SPIDER_NET_GHIINT2STS);
1344
1345 /* check GHIINT0STS ************************************/
1346 if (status_reg)
1347 for (i = 0; i < 32; i++)
1348 if (status_reg & (1<<i))
1349 switch (i)
1350 {
1351 /* let error_reg1 and error_reg2 evaluation decide, what to do
1352 case SPIDER_NET_PHYINT:
1353 case SPIDER_NET_GMAC2INT:
1354 case SPIDER_NET_GMAC1INT:
1355 case SPIDER_NET_GIPSINT:
1356 case SPIDER_NET_GFIFOINT:
1357 case SPIDER_NET_DMACINT:
1358 case SPIDER_NET_GSYSINT:
1359 break; */
1360
1361 case SPIDER_NET_GPWOPCMPINT:
1362 /* PHY write operation completed */
1363 show_error = 0;
1364 break;
1365 case SPIDER_NET_GPROPCMPINT:
1366 /* PHY read operation completed */
1367 /* we don't use semaphores, as we poll for the completion
1368 * of the read operation in spider_net_read_phy. Should take
1369 * about 50 us */
1370 show_error = 0;
1371 break;
1372 case SPIDER_NET_GPWFFINT:
1373 /* PHY command queue full */
1374 if (netif_msg_intr(card))
1375 pr_err("PHY write queue full\n");
1376 show_error = 0;
1377 break;
1378
1379 /* case SPIDER_NET_GRMDADRINT: not used. print a message */
1380 /* case SPIDER_NET_GRMARPINT: not used. print a message */
1381 /* case SPIDER_NET_GRMMPINT: not used. print a message */
1382
1383 case SPIDER_NET_GDTDEN0INT:
1384 /* someone has set TX_DMA_EN to 0 */
1385 show_error = 0;
1386 break;
1387
1388 case SPIDER_NET_GDDDEN0INT: /* fallthrough */
1389 case SPIDER_NET_GDCDEN0INT: /* fallthrough */
1390 case SPIDER_NET_GDBDEN0INT: /* fallthrough */
1391 case SPIDER_NET_GDADEN0INT:
1392 /* someone has set RX_DMA_EN to 0 */
1393 show_error = 0;
1394 break;
1395
1396 /* RX interrupts */
1397 case SPIDER_NET_GDDFDCINT:
1398 case SPIDER_NET_GDCFDCINT:
1399 case SPIDER_NET_GDBFDCINT:
1400 case SPIDER_NET_GDAFDCINT:
1401 /* case SPIDER_NET_GDNMINT: not used. print a message */
1402 /* case SPIDER_NET_GCNMINT: not used. print a message */
1403 /* case SPIDER_NET_GBNMINT: not used. print a message */
1404 /* case SPIDER_NET_GANMINT: not used. print a message */
1405 /* case SPIDER_NET_GRFNMINT: not used. print a message */
1406 show_error = 0;
1407 break;
1408
1409 /* TX interrupts */
1410 case SPIDER_NET_GDTFDCINT:
1411 show_error = 0;
1412 break;
1413 case SPIDER_NET_GTTEDINT:
1414 show_error = 0;
1415 break;
1416 case SPIDER_NET_GDTDCEINT:
1417 /* chain end. If a descriptor should be sent, kick off
1418 * tx dma
1419 if (card->tx_chain.tail == card->tx_chain.head)
1420 spider_net_kick_tx_dma(card);
1421 show_error = 0; */
1422 break;
1423
1424 /* case SPIDER_NET_G1TMCNTINT: not used. print a message */
1425 /* case SPIDER_NET_GFREECNTINT: not used. print a message */
1426 }
1427
1428 /* check GHIINT1STS ************************************/
1429 if (error_reg1)
1430 for (i = 0; i < 32; i++)
1431 if (error_reg1 & (1<<i))
1432 switch (i)
1433 {
1434 case SPIDER_NET_GTMFLLINT:
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001435 if (netif_msg_intr(card) && net_ratelimit())
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001436 pr_err("Spider TX RAM full\n");
1437 show_error = 0;
1438 break;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001439 case SPIDER_NET_GRFDFLLINT: /* fallthrough */
1440 case SPIDER_NET_GRFCFLLINT: /* fallthrough */
1441 case SPIDER_NET_GRFBFLLINT: /* fallthrough */
1442 case SPIDER_NET_GRFAFLLINT: /* fallthrough */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001443 case SPIDER_NET_GRMFLLINT:
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001444 if (netif_msg_intr(card) && net_ratelimit())
Jens Osterkamp4e5077b2006-03-28 15:59:55 +02001445 pr_debug("Spider RX RAM full, incoming packets "
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001446 "might be discarded!\n");
1447 spider_net_rx_irq_off(card);
1448 tasklet_schedule(&card->rxram_full_tl);
1449 show_error = 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001450 break;
1451
1452 /* case SPIDER_NET_GTMSHTINT: problem, print a message */
1453 case SPIDER_NET_GDTINVDINT:
1454 /* allrighty. tx from previous descr ok */
1455 show_error = 0;
1456 break;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001457
1458 /* chain end */
1459 case SPIDER_NET_GDDDCEINT: /* fallthrough */
1460 case SPIDER_NET_GDCDCEINT: /* fallthrough */
1461 case SPIDER_NET_GDBDCEINT: /* fallthrough */
1462 case SPIDER_NET_GDADCEINT:
1463 if (netif_msg_intr(card))
1464 pr_err("got descriptor chain end interrupt, "
1465 "restarting DMAC %c.\n",
1466 'D'+i-SPIDER_NET_GDDDCEINT);
1467 spider_net_refill_rx_chain(card);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001468 spider_net_enable_rxdmac(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001469 show_error = 0;
1470 break;
1471
1472 /* invalid descriptor */
1473 case SPIDER_NET_GDDINVDINT: /* fallthrough */
1474 case SPIDER_NET_GDCINVDINT: /* fallthrough */
1475 case SPIDER_NET_GDBINVDINT: /* fallthrough */
1476 case SPIDER_NET_GDAINVDINT:
1477 /* could happen when rx chain is full */
1478 spider_net_refill_rx_chain(card);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001479 spider_net_enable_rxdmac(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001480 show_error = 0;
1481 break;
1482
1483 /* case SPIDER_NET_GDTRSERINT: problem, print a message */
1484 /* case SPIDER_NET_GDDRSERINT: problem, print a message */
1485 /* case SPIDER_NET_GDCRSERINT: problem, print a message */
1486 /* case SPIDER_NET_GDBRSERINT: problem, print a message */
1487 /* case SPIDER_NET_GDARSERINT: problem, print a message */
1488 /* case SPIDER_NET_GDSERINT: problem, print a message */
1489 /* case SPIDER_NET_GDTPTERINT: problem, print a message */
1490 /* case SPIDER_NET_GDDPTERINT: problem, print a message */
1491 /* case SPIDER_NET_GDCPTERINT: problem, print a message */
1492 /* case SPIDER_NET_GDBPTERINT: problem, print a message */
1493 /* case SPIDER_NET_GDAPTERINT: problem, print a message */
1494 default:
1495 show_error = 1;
1496 break;
1497 }
1498
1499 /* check GHIINT2STS ************************************/
1500 if (error_reg2)
1501 for (i = 0; i < 32; i++)
1502 if (error_reg2 & (1<<i))
1503 switch (i)
1504 {
1505 /* there is nothing we can (want to) do at this time. Log a
1506 * message, we can switch on and off the specific values later on
1507 case SPIDER_NET_GPROPERINT:
1508 case SPIDER_NET_GMCTCRSNGINT:
1509 case SPIDER_NET_GMCTLCOLINT:
1510 case SPIDER_NET_GMCTTMOTINT:
1511 case SPIDER_NET_GMCRCAERINT:
1512 case SPIDER_NET_GMCRCALERINT:
1513 case SPIDER_NET_GMCRALNERINT:
1514 case SPIDER_NET_GMCROVRINT:
1515 case SPIDER_NET_GMCRRNTINT:
1516 case SPIDER_NET_GMCRRXERINT:
1517 case SPIDER_NET_GTITCSERINT:
1518 case SPIDER_NET_GTIFMTERINT:
1519 case SPIDER_NET_GTIPKTRVKINT:
1520 case SPIDER_NET_GTISPINGINT:
1521 case SPIDER_NET_GTISADNGINT:
1522 case SPIDER_NET_GTISPDNGINT:
1523 case SPIDER_NET_GRIFMTERINT:
1524 case SPIDER_NET_GRIPKTRVKINT:
1525 case SPIDER_NET_GRISPINGINT:
1526 case SPIDER_NET_GRISADNGINT:
1527 case SPIDER_NET_GRISPDNGINT:
1528 break;
1529 */
1530 default:
1531 break;
1532 }
1533
1534 if ((show_error) && (netif_msg_intr(card)))
1535 pr_err("Got error interrupt, GHIINT0STS = 0x%08x, "
1536 "GHIINT1STS = 0x%08x, GHIINT2STS = 0x%08x\n",
1537 status_reg, error_reg1, error_reg2);
1538
1539 /* clear interrupt sources */
1540 spider_net_write_reg(card, SPIDER_NET_GHIINT1STS, error_reg1);
1541 spider_net_write_reg(card, SPIDER_NET_GHIINT2STS, error_reg2);
1542}
1543
1544/**
1545 * spider_net_interrupt - interrupt handler for spider_net
1546 * @irq: interupt number
1547 * @ptr: pointer to net_device
1548 * @regs: PU registers
1549 *
1550 * returns IRQ_HANDLED, if interrupt was for driver, or IRQ_NONE, if no
1551 * interrupt found raised by card.
1552 *
1553 * This is the interrupt handler, that turns off
1554 * interrupts for this device and makes the stack poll the driver
1555 */
1556static irqreturn_t
1557spider_net_interrupt(int irq, void *ptr, struct pt_regs *regs)
1558{
1559 struct net_device *netdev = ptr;
1560 struct spider_net_card *card = netdev_priv(netdev);
1561 u32 status_reg;
1562
1563 status_reg = spider_net_read_reg(card, SPIDER_NET_GHIINT0STS);
1564
1565 if (!status_reg)
1566 return IRQ_NONE;
1567
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001568 if (status_reg & SPIDER_NET_RXINT ) {
1569 spider_net_rx_irq_off(card);
1570 netif_rx_schedule(netdev);
1571 }
1572
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001573 if (status_reg & SPIDER_NET_ERRINT )
1574 spider_net_handle_error_irq(card, status_reg);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001575
1576 /* clear interrupt sources */
1577 spider_net_write_reg(card, SPIDER_NET_GHIINT0STS, status_reg);
1578
1579 return IRQ_HANDLED;
1580}
1581
1582#ifdef CONFIG_NET_POLL_CONTROLLER
1583/**
1584 * spider_net_poll_controller - artificial interrupt for netconsole etc.
1585 * @netdev: interface device structure
1586 *
1587 * see Documentation/networking/netconsole.txt
1588 */
1589static void
1590spider_net_poll_controller(struct net_device *netdev)
1591{
1592 disable_irq(netdev->irq);
1593 spider_net_interrupt(netdev->irq, netdev, NULL);
1594 enable_irq(netdev->irq);
1595}
1596#endif /* CONFIG_NET_POLL_CONTROLLER */
1597
1598/**
1599 * spider_net_init_card - initializes the card
1600 * @card: card structure
1601 *
1602 * spider_net_init_card initializes the card so that other registers can
1603 * be used
1604 */
1605static void
1606spider_net_init_card(struct spider_net_card *card)
1607{
1608 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1609 SPIDER_NET_CKRCTRL_STOP_VALUE);
1610
1611 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1612 SPIDER_NET_CKRCTRL_RUN_VALUE);
1613}
1614
1615/**
1616 * spider_net_enable_card - enables the card by setting all kinds of regs
1617 * @card: card structure
1618 *
1619 * spider_net_enable_card sets a lot of SMMIO registers to enable the device
1620 */
1621static void
1622spider_net_enable_card(struct spider_net_card *card)
1623{
1624 int i;
1625 /* the following array consists of (register),(value) pairs
1626 * that are set in this function. A register of 0 ends the list */
1627 u32 regs[][2] = {
1628 { SPIDER_NET_GRESUMINTNUM, 0 },
1629 { SPIDER_NET_GREINTNUM, 0 },
1630
1631 /* set interrupt frame number registers */
1632 /* clear the single DMA engine registers first */
1633 { SPIDER_NET_GFAFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1634 { SPIDER_NET_GFBFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1635 { SPIDER_NET_GFCFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1636 { SPIDER_NET_GFDFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1637 /* then set, what we really need */
1638 { SPIDER_NET_GFFRMNUM, SPIDER_NET_FRAMENUM_VALUE },
1639
1640 /* timer counter registers and stuff */
1641 { SPIDER_NET_GFREECNNUM, 0 },
1642 { SPIDER_NET_GONETIMENUM, 0 },
1643 { SPIDER_NET_GTOUTFRMNUM, 0 },
1644
1645 /* RX mode setting */
1646 { SPIDER_NET_GRXMDSET, SPIDER_NET_RXMODE_VALUE },
1647 /* TX mode setting */
1648 { SPIDER_NET_GTXMDSET, SPIDER_NET_TXMODE_VALUE },
1649 /* IPSEC mode setting */
1650 { SPIDER_NET_GIPSECINIT, SPIDER_NET_IPSECINIT_VALUE },
1651
1652 { SPIDER_NET_GFTRESTRT, SPIDER_NET_RESTART_VALUE },
1653
1654 { SPIDER_NET_GMRWOLCTRL, 0 },
Jens Osterkampb636d172006-05-04 05:59:56 -04001655 { SPIDER_NET_GTESTMD, 0x10000000 },
1656 { SPIDER_NET_GTTQMSK, 0x00400040 },
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001657 { SPIDER_NET_GTESTMD, 0 },
1658
1659 { SPIDER_NET_GMACINTEN, 0 },
1660
1661 /* flow control stuff */
1662 { SPIDER_NET_GMACAPAUSE, SPIDER_NET_MACAPAUSE_VALUE },
1663 { SPIDER_NET_GMACTXPAUSE, SPIDER_NET_TXPAUSE_VALUE },
1664
1665 { SPIDER_NET_GMACBSTLMT, SPIDER_NET_BURSTLMT_VALUE },
1666 { 0, 0}
1667 };
1668
1669 i = 0;
1670 while (regs[i][0]) {
1671 spider_net_write_reg(card, regs[i][0], regs[i][1]);
1672 i++;
1673 }
1674
1675 /* clear unicast filter table entries 1 to 14 */
1676 for (i = 1; i <= 14; i++) {
1677 spider_net_write_reg(card,
1678 SPIDER_NET_GMRUAFILnR + i * 8,
1679 0x00080000);
1680 spider_net_write_reg(card,
1681 SPIDER_NET_GMRUAFILnR + i * 8 + 4,
1682 0x00000000);
1683 }
1684
1685 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R, 0x08080000);
1686
1687 spider_net_write_reg(card, SPIDER_NET_ECMODE, SPIDER_NET_ECMODE_VALUE);
1688
1689 /* set chain tail adress for RX chains and
1690 * enable DMA */
1691 spider_net_enable_rxchtails(card);
1692 spider_net_enable_rxdmac(card);
1693
1694 spider_net_write_reg(card, SPIDER_NET_GRXDMAEN, SPIDER_NET_WOL_VALUE);
1695
1696 /* set chain tail adress for TX chain */
1697 spider_net_enable_txdmac(card);
1698
1699 spider_net_write_reg(card, SPIDER_NET_GMACLENLMT,
1700 SPIDER_NET_LENLMT_VALUE);
1701 spider_net_write_reg(card, SPIDER_NET_GMACMODE,
1702 SPIDER_NET_MACMODE_VALUE);
1703 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD,
1704 SPIDER_NET_OPMODE_VALUE);
1705
1706 /* set interrupt mask registers */
1707 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK,
1708 SPIDER_NET_INT0_MASK_VALUE);
1709 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK,
1710 SPIDER_NET_INT1_MASK_VALUE);
1711 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK,
1712 SPIDER_NET_INT2_MASK_VALUE);
1713}
1714
1715/**
1716 * spider_net_open - called upon ifonfig up
1717 * @netdev: interface device structure
1718 *
1719 * returns 0 on success, <0 on failure
1720 *
1721 * spider_net_open allocates all the descriptors and memory needed for
1722 * operation, sets up multicast list and enables interrupts
1723 */
1724int
1725spider_net_open(struct net_device *netdev)
1726{
1727 struct spider_net_card *card = netdev_priv(netdev);
1728 int result;
1729
1730 result = -ENOMEM;
1731 if (spider_net_init_chain(card, &card->tx_chain,
1732 card->descr, tx_descriptors))
1733 goto alloc_tx_failed;
1734 if (spider_net_init_chain(card, &card->rx_chain,
1735 card->descr + tx_descriptors, rx_descriptors))
1736 goto alloc_rx_failed;
1737
1738 /* allocate rx skbs */
1739 if (spider_net_alloc_rx_skbs(card))
1740 goto alloc_skbs_failed;
1741
1742 spider_net_set_multi(netdev);
1743
1744 /* further enhancement: setup hw vlan, if needed */
1745
1746 result = -EBUSY;
1747 if (request_irq(netdev->irq, spider_net_interrupt,
1748 SA_SHIRQ, netdev->name, netdev))
1749 goto register_int_failed;
1750
1751 spider_net_enable_card(card);
1752
Jens Osterkamp543cec52005-09-06 19:30:54 -07001753 netif_start_queue(netdev);
1754 netif_carrier_on(netdev);
1755 netif_poll_enable(netdev);
1756
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001757 return 0;
1758
1759register_int_failed:
1760 spider_net_free_rx_chain_contents(card);
1761alloc_skbs_failed:
1762 spider_net_free_chain(card, &card->rx_chain);
1763alloc_rx_failed:
1764 spider_net_free_chain(card, &card->tx_chain);
1765alloc_tx_failed:
1766 return result;
1767}
1768
1769/**
1770 * spider_net_setup_phy - setup PHY
1771 * @card: card structure
1772 *
1773 * returns 0 on success, <0 on failure
1774 *
1775 * spider_net_setup_phy is used as part of spider_net_probe. Sets
1776 * the PHY to 1000 Mbps
1777 **/
1778static int
1779spider_net_setup_phy(struct spider_net_card *card)
1780{
1781 struct mii_phy *phy = &card->phy;
1782
1783 spider_net_write_reg(card, SPIDER_NET_GDTDMASEL,
1784 SPIDER_NET_DMASEL_VALUE);
1785 spider_net_write_reg(card, SPIDER_NET_GPCCTRL,
1786 SPIDER_NET_PHY_CTRL_VALUE);
1787 phy->mii_id = 1;
1788 phy->dev = card->netdev;
1789 phy->mdio_read = spider_net_read_phy;
1790 phy->mdio_write = spider_net_write_phy;
1791
1792 mii_phy_probe(phy, phy->mii_id);
1793
1794 if (phy->def->ops->setup_forced)
1795 phy->def->ops->setup_forced(phy, SPEED_1000, DUPLEX_FULL);
1796
Jens Osterkamp8ec93452006-05-04 05:59:41 -04001797 phy->def->ops->enable_fiber(phy);
Jens Osterkamp53abbf72005-09-16 08:55:33 +02001798
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001799 phy->def->ops->read_link(phy);
1800 pr_info("Found %s with %i Mbps, %s-duplex.\n", phy->def->name,
1801 phy->speed, phy->duplex==1 ? "Full" : "Half");
1802
1803 return 0;
1804}
1805
1806/**
1807 * spider_net_download_firmware - loads firmware into the adapter
1808 * @card: card structure
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001809 * @firmware_ptr: pointer to firmware data
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001810 *
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001811 * spider_net_download_firmware loads the firmware data into the
1812 * adapter. It assumes the length etc. to be allright.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001813 */
Arnd Bergmann0d3ea162006-01-12 17:16:41 -05001814static int
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001815spider_net_download_firmware(struct spider_net_card *card,
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001816 u8 *firmware_ptr)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001817{
1818 int sequencer, i;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001819 u32 *fw_ptr = (u32 *)firmware_ptr;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001820
1821 /* stop sequencers */
1822 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1823 SPIDER_NET_STOP_SEQ_VALUE);
1824
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001825 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1826 sequencer++) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001827 spider_net_write_reg(card,
1828 SPIDER_NET_GSnPRGADR + sequencer * 8, 0);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001829 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001830 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1831 sequencer * 8, *fw_ptr);
1832 fw_ptr++;
1833 }
1834 }
1835
Arnd Bergmann0d3ea162006-01-12 17:16:41 -05001836 if (spider_net_read_reg(card, SPIDER_NET_GSINIT))
1837 return -EIO;
1838
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001839 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1840 SPIDER_NET_RUN_SEQ_VALUE);
Arnd Bergmann0d3ea162006-01-12 17:16:41 -05001841
1842 return 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001843}
1844
1845/**
1846 * spider_net_init_firmware - reads in firmware parts
1847 * @card: card structure
1848 *
1849 * Returns 0 on success, <0 on failure
1850 *
1851 * spider_net_init_firmware opens the sequencer firmware and does some basic
1852 * checks. This function opens and releases the firmware structure. A call
1853 * to download the firmware is performed before the release.
1854 *
1855 * Firmware format
1856 * ===============
1857 * spider_fw.bin is expected to be a file containing 6*1024*4 bytes, 4k being
1858 * the program for each sequencer. Use the command
1859 * tail -q -n +2 Seq_code1_0x088.txt Seq_code2_0x090.txt \
1860 * Seq_code3_0x098.txt Seq_code4_0x0A0.txt Seq_code5_0x0A8.txt \
1861 * Seq_code6_0x0B0.txt | xxd -r -p -c4 > spider_fw.bin
1862 *
1863 * to generate spider_fw.bin, if you have sequencer programs with something
1864 * like the following contents for each sequencer:
1865 * <ONE LINE COMMENT>
1866 * <FIRST 4-BYTES-WORD FOR SEQUENCER>
1867 * <SECOND 4-BYTES-WORD FOR SEQUENCER>
1868 * ...
1869 * <1024th 4-BYTES-WORD FOR SEQUENCER>
1870 */
1871static int
1872spider_net_init_firmware(struct spider_net_card *card)
1873{
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001874 struct firmware *firmware = NULL;
Arnd Bergmann030d6752006-01-12 17:16:42 -05001875 struct device_node *dn;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001876 u8 *fw_prop = NULL;
1877 int err = -ENOENT;
1878 int fw_size;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001879
Arnd Bergmann030d6752006-01-12 17:16:42 -05001880 if (request_firmware((const struct firmware **)&firmware,
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001881 SPIDER_NET_FIRMWARE_NAME, &card->pdev->dev) == 0) {
1882 if ( (firmware->size != SPIDER_NET_FIRMWARE_LEN) &&
1883 netif_msg_probe(card) ) {
1884 pr_err("Incorrect size of spidernet firmware in " \
1885 "filesystem. Looking in host firmware...\n");
1886 goto try_host_fw;
1887 }
1888 err = spider_net_download_firmware(card, firmware->data);
Arnd Bergmann030d6752006-01-12 17:16:42 -05001889
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001890 release_firmware(firmware);
1891 if (err)
1892 goto try_host_fw;
Arnd Bergmann030d6752006-01-12 17:16:42 -05001893
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001894 goto done;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001895 }
1896
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001897try_host_fw:
1898 dn = pci_device_to_OF_node(card->pdev);
1899 if (!dn)
1900 goto out_err;
1901
1902 fw_prop = (u8 *)get_property(dn, "firmware", &fw_size);
1903 if (!fw_prop)
1904 goto out_err;
1905
1906 if ( (fw_size != SPIDER_NET_FIRMWARE_LEN) &&
1907 netif_msg_probe(card) ) {
1908 pr_err("Incorrect size of spidernet firmware in " \
1909 "host firmware\n");
1910 goto done;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001911 }
1912
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001913 err = spider_net_download_firmware(card, fw_prop);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001914
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001915done:
1916 return err;
1917out_err:
1918 if (netif_msg_probe(card))
1919 pr_err("Couldn't find spidernet firmware in filesystem " \
1920 "or host firmware\n");
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001921 return err;
1922}
1923
1924/**
1925 * spider_net_workaround_rxramfull - work around firmware bug
1926 * @card: card structure
1927 *
1928 * no return value
1929 **/
1930static void
1931spider_net_workaround_rxramfull(struct spider_net_card *card)
1932{
1933 int i, sequencer = 0;
1934
1935 /* cancel reset */
1936 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1937 SPIDER_NET_CKRCTRL_RUN_VALUE);
1938
1939 /* empty sequencer data */
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001940 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1941 sequencer++) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001942 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1943 sequencer * 8, 0x0);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001944 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001945 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1946 sequencer * 8, 0x0);
1947 }
1948 }
1949
1950 /* set sequencer operation */
1951 spider_net_write_reg(card, SPIDER_NET_GSINIT, 0x000000fe);
1952
1953 /* reset */
1954 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1955 SPIDER_NET_CKRCTRL_STOP_VALUE);
1956}
1957
1958/**
1959 * spider_net_tx_timeout_task - task scheduled by the watchdog timeout
1960 * function (to be called not under interrupt status)
1961 * @data: data, is interface device structure
1962 *
1963 * called as task when tx hangs, resets interface (if interface is up)
1964 */
1965static void
1966spider_net_tx_timeout_task(void *data)
1967{
1968 struct net_device *netdev = data;
1969 struct spider_net_card *card = netdev_priv(netdev);
1970
1971 if (!(netdev->flags & IFF_UP))
1972 goto out;
1973
1974 netif_device_detach(netdev);
1975 spider_net_stop(netdev);
1976
1977 spider_net_workaround_rxramfull(card);
1978 spider_net_init_card(card);
1979
1980 if (spider_net_setup_phy(card))
1981 goto out;
1982 if (spider_net_init_firmware(card))
1983 goto out;
1984
1985 spider_net_open(netdev);
1986 spider_net_kick_tx_dma(card, card->tx_chain.head);
1987 netif_device_attach(netdev);
1988
1989out:
1990 atomic_dec(&card->tx_timeout_task_counter);
1991}
1992
1993/**
1994 * spider_net_tx_timeout - called when the tx timeout watchdog kicks in.
1995 * @netdev: interface device structure
1996 *
1997 * called, if tx hangs. Schedules a task that resets the interface
1998 */
1999static void
2000spider_net_tx_timeout(struct net_device *netdev)
2001{
2002 struct spider_net_card *card;
2003
2004 card = netdev_priv(netdev);
2005 atomic_inc(&card->tx_timeout_task_counter);
2006 if (netdev->flags & IFF_UP)
2007 schedule_work(&card->tx_timeout_task);
2008 else
2009 atomic_dec(&card->tx_timeout_task_counter);
2010}
2011
2012/**
2013 * spider_net_setup_netdev_ops - initialization of net_device operations
2014 * @netdev: net_device structure
2015 *
2016 * fills out function pointers in the net_device structure
2017 */
2018static void
2019spider_net_setup_netdev_ops(struct net_device *netdev)
2020{
2021 netdev->open = &spider_net_open;
2022 netdev->stop = &spider_net_stop;
2023 netdev->hard_start_xmit = &spider_net_xmit;
2024 netdev->get_stats = &spider_net_get_stats;
2025 netdev->set_multicast_list = &spider_net_set_multi;
2026 netdev->set_mac_address = &spider_net_set_mac;
2027 netdev->change_mtu = &spider_net_change_mtu;
2028 netdev->do_ioctl = &spider_net_do_ioctl;
2029 /* tx watchdog */
2030 netdev->tx_timeout = &spider_net_tx_timeout;
2031 netdev->watchdog_timeo = SPIDER_NET_WATCHDOG_TIMEOUT;
2032 /* NAPI */
2033 netdev->poll = &spider_net_poll;
2034 netdev->weight = SPIDER_NET_NAPI_WEIGHT;
2035 /* HW VLAN */
2036 netdev->vlan_rx_register = &spider_net_vlan_rx_reg;
2037 netdev->vlan_rx_add_vid = &spider_net_vlan_rx_add;
2038 netdev->vlan_rx_kill_vid = &spider_net_vlan_rx_kill;
2039#ifdef CONFIG_NET_POLL_CONTROLLER
2040 /* poll controller */
2041 netdev->poll_controller = &spider_net_poll_controller;
2042#endif /* CONFIG_NET_POLL_CONTROLLER */
2043 /* ethtool ops */
2044 netdev->ethtool_ops = &spider_net_ethtool_ops;
2045}
2046
2047/**
2048 * spider_net_setup_netdev - initialization of net_device
2049 * @card: card structure
2050 *
2051 * Returns 0 on success or <0 on failure
2052 *
2053 * spider_net_setup_netdev initializes the net_device structure
2054 **/
2055static int
2056spider_net_setup_netdev(struct spider_net_card *card)
2057{
2058 int result;
2059 struct net_device *netdev = card->netdev;
2060 struct device_node *dn;
2061 struct sockaddr addr;
2062 u8 *mac;
2063
2064 SET_MODULE_OWNER(netdev);
2065 SET_NETDEV_DEV(netdev, &card->pdev->dev);
2066
2067 pci_set_drvdata(card->pdev, netdev);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05002068
2069 atomic_set(&card->tx_chain_release,0);
2070 card->rxram_full_tl.data = (unsigned long) card;
2071 card->rxram_full_tl.func =
2072 (void (*)(unsigned long)) spider_net_handle_rxram_full;
2073 init_timer(&card->tx_timer);
2074 card->tx_timer.function =
2075 (void (*)(unsigned long)) spider_net_cleanup_tx_ring;
2076 card->tx_timer.data = (unsigned long) card;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002077 netdev->irq = card->pdev->irq;
2078
2079 card->options.rx_csum = SPIDER_NET_RX_CSUM_DEFAULT;
2080
2081 spider_net_setup_netdev_ops(netdev);
2082
Jens Osterkamp8dfc9142006-03-27 17:16:36 +02002083 netdev->features = NETIF_F_HW_CSUM;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002084 /* some time: NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
2085 * NETIF_F_HW_VLAN_FILTER */
2086
2087 netdev->irq = card->pdev->irq;
2088
2089 dn = pci_device_to_OF_node(card->pdev);
Jens Osterkamp543cec52005-09-06 19:30:54 -07002090 if (!dn)
2091 return -EIO;
2092
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002093 mac = (u8 *)get_property(dn, "local-mac-address", NULL);
Jens Osterkamp543cec52005-09-06 19:30:54 -07002094 if (!mac)
2095 return -EIO;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002096 memcpy(addr.sa_data, mac, ETH_ALEN);
2097
2098 result = spider_net_set_mac(netdev, &addr);
2099 if ((result) && (netif_msg_probe(card)))
2100 pr_err("Failed to set MAC address: %i\n", result);
2101
2102 result = register_netdev(netdev);
2103 if (result) {
2104 if (netif_msg_probe(card))
2105 pr_err("Couldn't register net_device: %i\n",
2106 result);
2107 return result;
2108 }
2109
2110 if (netif_msg_probe(card))
2111 pr_info("Initialized device %s.\n", netdev->name);
2112
2113 return 0;
2114}
2115
2116/**
2117 * spider_net_alloc_card - allocates net_device and card structure
2118 *
2119 * returns the card structure or NULL in case of errors
2120 *
2121 * the card and net_device structures are linked to each other
2122 */
2123static struct spider_net_card *
2124spider_net_alloc_card(void)
2125{
2126 struct net_device *netdev;
2127 struct spider_net_card *card;
2128 size_t alloc_size;
2129
2130 alloc_size = sizeof (*card) +
2131 sizeof (struct spider_net_descr) * rx_descriptors +
2132 sizeof (struct spider_net_descr) * tx_descriptors;
2133 netdev = alloc_etherdev(alloc_size);
2134 if (!netdev)
2135 return NULL;
2136
2137 card = netdev_priv(netdev);
2138 card->netdev = netdev;
2139 card->msg_enable = SPIDER_NET_DEFAULT_MSG;
2140 INIT_WORK(&card->tx_timeout_task, spider_net_tx_timeout_task, netdev);
2141 init_waitqueue_head(&card->waitq);
2142 atomic_set(&card->tx_timeout_task_counter, 0);
2143
2144 return card;
2145}
2146
2147/**
2148 * spider_net_undo_pci_setup - releases PCI ressources
2149 * @card: card structure
2150 *
2151 * spider_net_undo_pci_setup releases the mapped regions
2152 */
2153static void
2154spider_net_undo_pci_setup(struct spider_net_card *card)
2155{
2156 iounmap(card->regs);
2157 pci_release_regions(card->pdev);
2158}
2159
2160/**
2161 * spider_net_setup_pci_dev - sets up the device in terms of PCI operations
2162 * @card: card structure
2163 * @pdev: PCI device
2164 *
2165 * Returns the card structure or NULL if any errors occur
2166 *
2167 * spider_net_setup_pci_dev initializes pdev and together with the
2168 * functions called in spider_net_open configures the device so that
2169 * data can be transferred over it
2170 * The net_device structure is attached to the card structure, if the
2171 * function returns without error.
2172 **/
2173static struct spider_net_card *
2174spider_net_setup_pci_dev(struct pci_dev *pdev)
2175{
2176 struct spider_net_card *card;
2177 unsigned long mmio_start, mmio_len;
2178
2179 if (pci_enable_device(pdev)) {
2180 pr_err("Couldn't enable PCI device\n");
2181 return NULL;
2182 }
2183
2184 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
2185 pr_err("Couldn't find proper PCI device base address.\n");
2186 goto out_disable_dev;
2187 }
2188
2189 if (pci_request_regions(pdev, spider_net_driver_name)) {
2190 pr_err("Couldn't obtain PCI resources, aborting.\n");
2191 goto out_disable_dev;
2192 }
2193
2194 pci_set_master(pdev);
2195
2196 card = spider_net_alloc_card();
2197 if (!card) {
2198 pr_err("Couldn't allocate net_device structure, "
2199 "aborting.\n");
2200 goto out_release_regions;
2201 }
2202 card->pdev = pdev;
2203
2204 /* fetch base address and length of first resource */
2205 mmio_start = pci_resource_start(pdev, 0);
2206 mmio_len = pci_resource_len(pdev, 0);
2207
2208 card->netdev->mem_start = mmio_start;
2209 card->netdev->mem_end = mmio_start + mmio_len;
2210 card->regs = ioremap(mmio_start, mmio_len);
2211
2212 if (!card->regs) {
2213 pr_err("Couldn't obtain PCI resources, aborting.\n");
2214 goto out_release_regions;
2215 }
2216
2217 return card;
2218
2219out_release_regions:
2220 pci_release_regions(pdev);
2221out_disable_dev:
2222 pci_disable_device(pdev);
2223 pci_set_drvdata(pdev, NULL);
2224 return NULL;
2225}
2226
2227/**
2228 * spider_net_probe - initialization of a device
2229 * @pdev: PCI device
2230 * @ent: entry in the device id list
2231 *
2232 * Returns 0 on success, <0 on failure
2233 *
2234 * spider_net_probe initializes pdev and registers a net_device
2235 * structure for it. After that, the device can be ifconfig'ed up
2236 **/
2237static int __devinit
2238spider_net_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2239{
2240 int err = -EIO;
2241 struct spider_net_card *card;
2242
2243 card = spider_net_setup_pci_dev(pdev);
2244 if (!card)
2245 goto out;
2246
2247 spider_net_workaround_rxramfull(card);
2248 spider_net_init_card(card);
2249
2250 err = spider_net_setup_phy(card);
2251 if (err)
2252 goto out_undo_pci;
2253
2254 err = spider_net_init_firmware(card);
2255 if (err)
2256 goto out_undo_pci;
2257
2258 err = spider_net_setup_netdev(card);
2259 if (err)
2260 goto out_undo_pci;
2261
2262 return 0;
2263
2264out_undo_pci:
2265 spider_net_undo_pci_setup(card);
2266 free_netdev(card->netdev);
2267out:
2268 return err;
2269}
2270
2271/**
2272 * spider_net_remove - removal of a device
2273 * @pdev: PCI device
2274 *
2275 * Returns 0 on success, <0 on failure
2276 *
2277 * spider_net_remove is called to remove the device and unregisters the
2278 * net_device
2279 **/
2280static void __devexit
2281spider_net_remove(struct pci_dev *pdev)
2282{
2283 struct net_device *netdev;
2284 struct spider_net_card *card;
2285
2286 netdev = pci_get_drvdata(pdev);
2287 card = netdev_priv(netdev);
2288
2289 wait_event(card->waitq,
2290 atomic_read(&card->tx_timeout_task_counter) == 0);
2291
2292 unregister_netdev(netdev);
Jens Osterkamp543cec52005-09-06 19:30:54 -07002293
2294 /* switch off card */
2295 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2296 SPIDER_NET_CKRCTRL_STOP_VALUE);
2297 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2298 SPIDER_NET_CKRCTRL_RUN_VALUE);
2299
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002300 spider_net_undo_pci_setup(card);
2301 free_netdev(netdev);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002302}
2303
2304static struct pci_driver spider_net_driver = {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002305 .name = spider_net_driver_name,
2306 .id_table = spider_net_pci_tbl,
2307 .probe = spider_net_probe,
2308 .remove = __devexit_p(spider_net_remove)
2309};
2310
2311/**
2312 * spider_net_init - init function when the driver is loaded
2313 *
2314 * spider_net_init registers the device driver
2315 */
2316static int __init spider_net_init(void)
2317{
2318 if (rx_descriptors < SPIDER_NET_RX_DESCRIPTORS_MIN) {
2319 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MIN;
2320 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2321 }
2322 if (rx_descriptors > SPIDER_NET_RX_DESCRIPTORS_MAX) {
2323 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MAX;
2324 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2325 }
2326 if (tx_descriptors < SPIDER_NET_TX_DESCRIPTORS_MIN) {
2327 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MIN;
2328 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2329 }
2330 if (tx_descriptors > SPIDER_NET_TX_DESCRIPTORS_MAX) {
2331 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MAX;
2332 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2333 }
2334
2335 return pci_register_driver(&spider_net_driver);
2336}
2337
2338/**
2339 * spider_net_cleanup - exit function when driver is unloaded
2340 *
2341 * spider_net_cleanup unregisters the device driver
2342 */
2343static void __exit spider_net_cleanup(void)
2344{
2345 pci_unregister_driver(&spider_net_driver);
2346}
2347
2348module_init(spider_net_init);
2349module_exit(spider_net_cleanup);