blob: 9d2ed04f9fd111eb5878f9f3879bc55811fa783e [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
Jens Osterkampaaec0fa2005-09-05 15:19:29 -070024#include <linux/compiler.h>
25#include <linux/crc32.h>
26#include <linux/delay.h>
27#include <linux/etherdevice.h>
28#include <linux/ethtool.h>
29#include <linux/firmware.h>
30#include <linux/if_vlan.h>
Arnd Bergmann7c5c2202006-01-12 17:16:45 -050031#include <linux/in.h>
Jens Osterkampaaec0fa2005-09-05 15:19:29 -070032#include <linux/init.h>
33#include <linux/ioport.h>
34#include <linux/ip.h>
35#include <linux/kernel.h>
36#include <linux/mii.h>
37#include <linux/module.h>
38#include <linux/netdevice.h>
39#include <linux/device.h>
40#include <linux/pci.h>
41#include <linux/skbuff.h>
42#include <linux/slab.h>
43#include <linux/tcp.h>
44#include <linux/types.h>
Arnd Bergmann11f1a522006-01-12 17:16:44 -050045#include <linux/vmalloc.h>
Jens Osterkampaaec0fa2005-09-05 15:19:29 -070046#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");
Linas Vepstas90f10842006-10-10 15:56:04 -050058MODULE_VERSION(VERSION);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -070059
60static int rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_DEFAULT;
61static int tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_DEFAULT;
62
Linas Vepstase2874f22006-10-10 15:59:02 -050063module_param(rx_descriptors, int, 0444);
64module_param(tx_descriptors, int, 0444);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -070065
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 */
Jens Osterkampbdd01502006-07-13 11:54:08 +020088static inline u32
Jens Osterkampaaec0fa2005-09-05 15:19:29 -070089spider_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 */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200105static inline void
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700106spider_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 */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200263static inline int
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700264spider_net_get_descr_status(struct spider_net_descr *descr)
265{
Jens Osterkampbdd01502006-07-13 11:54:08 +0200266 return descr->dmac_cmd_status & SPIDER_NET_DESCR_IND_PROC_MASK;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700267}
268
269/**
270 * spider_net_free_chain - free descriptor chain
271 * @card: card structure
272 * @chain: address of chain
273 *
274 */
275static void
276spider_net_free_chain(struct spider_net_card *card,
277 struct spider_net_descr_chain *chain)
278{
279 struct spider_net_descr *descr;
280
281 for (descr = chain->tail; !descr->bus_addr; descr = descr->next) {
282 pci_unmap_single(card->pdev, descr->bus_addr,
283 SPIDER_NET_DESCR_SIZE, PCI_DMA_BIDIRECTIONAL);
284 descr->bus_addr = 0;
285 }
286}
287
288/**
289 * spider_net_init_chain - links descriptor chain
290 * @card: card structure
291 * @chain: address of chain
292 * @start_descr: address of descriptor array
293 * @no: number of descriptors
294 *
295 * we manage a circular list that mirrors the hardware structure,
296 * except that the hardware uses bus addresses.
297 *
298 * returns 0 on success, <0 on failure
299 */
300static int
301spider_net_init_chain(struct spider_net_card *card,
302 struct spider_net_descr_chain *chain,
Jens Osterkampbdd01502006-07-13 11:54:08 +0200303 struct spider_net_descr *start_descr,
304 int direction, int no)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700305{
306 int i;
307 struct spider_net_descr *descr;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500308 dma_addr_t buf;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700309
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700310 descr = start_descr;
311 memset(descr, 0, sizeof(*descr) * no);
312
313 /* set up the hardware pointers in each descriptor */
314 for (i=0; i<no; i++, descr++) {
Jens Osterkampbdd01502006-07-13 11:54:08 +0200315 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700316
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500317 buf = pci_map_single(card->pdev, descr,
318 SPIDER_NET_DESCR_SIZE,
Jens Osterkampbdd01502006-07-13 11:54:08 +0200319 direction);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700320
Olof Johanssond4b0a4c2006-09-27 14:02:26 -0500321 if (pci_dma_mapping_error(buf))
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700322 goto iommu_error;
323
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500324 descr->bus_addr = buf;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700325 descr->next = descr + 1;
326 descr->prev = descr - 1;
327
328 }
329 /* do actual circular list */
330 (descr-1)->next = start_descr;
331 start_descr->prev = descr-1;
332
333 descr = start_descr;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200334 if (direction == PCI_DMA_FROMDEVICE)
335 for (i=0; i < no; i++, descr++)
336 descr->next_descr_addr = descr->next->bus_addr;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700337
Jens Osterkampbdd01502006-07-13 11:54:08 +0200338 spin_lock_init(&chain->lock);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700339 chain->head = start_descr;
340 chain->tail = start_descr;
341
342 return 0;
343
344iommu_error:
345 descr = start_descr;
346 for (i=0; i < no; i++, descr++)
347 if (descr->bus_addr)
348 pci_unmap_single(card->pdev, descr->bus_addr,
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500349 SPIDER_NET_DESCR_SIZE,
Jens Osterkampbdd01502006-07-13 11:54:08 +0200350 direction);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700351 return -ENOMEM;
352}
353
354/**
355 * spider_net_free_rx_chain_contents - frees descr contents in rx chain
356 * @card: card structure
357 *
358 * returns 0 on success, <0 on failure
359 */
360static void
361spider_net_free_rx_chain_contents(struct spider_net_card *card)
362{
363 struct spider_net_descr *descr;
364
365 descr = card->rx_chain.head;
366 while (descr->next != card->rx_chain.head) {
367 if (descr->skb) {
368 dev_kfree_skb(descr->skb);
369 pci_unmap_single(card->pdev, descr->buf_addr,
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500370 SPIDER_NET_MAX_FRAME,
Jens Osterkampbdd01502006-07-13 11:54:08 +0200371 PCI_DMA_FROMDEVICE);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700372 }
373 descr = descr->next;
374 }
375}
376
377/**
378 * spider_net_prepare_rx_descr - reinitializes a rx descriptor
379 * @card: card structure
380 * @descr: descriptor to re-init
381 *
382 * return 0 on succes, <0 on failure
383 *
384 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
385 * Activate the descriptor state-wise
386 */
387static int
388spider_net_prepare_rx_descr(struct spider_net_card *card,
389 struct spider_net_descr *descr)
390{
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500391 dma_addr_t buf;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700392 int error = 0;
393 int offset;
394 int bufsize;
395
396 /* we need to round up the buffer size to a multiple of 128 */
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500397 bufsize = (SPIDER_NET_MAX_FRAME + SPIDER_NET_RXBUF_ALIGN - 1) &
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700398 (~(SPIDER_NET_RXBUF_ALIGN - 1));
399
400 /* and we need to have it 128 byte aligned, therefore we allocate a
401 * bit more */
402 /* allocate an skb */
403 descr->skb = dev_alloc_skb(bufsize + SPIDER_NET_RXBUF_ALIGN - 1);
404 if (!descr->skb) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500405 if (netif_msg_rx_err(card) && net_ratelimit())
406 pr_err("Not enough memory to allocate rx buffer\n");
Jim Lewis9b6b0b82006-09-22 17:22:53 -0700407 card->spider_stats.alloc_rx_skb_error++;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700408 return -ENOMEM;
409 }
410 descr->buf_size = bufsize;
411 descr->result_size = 0;
412 descr->valid_size = 0;
413 descr->data_status = 0;
414 descr->data_error = 0;
415
416 offset = ((unsigned long)descr->skb->data) &
417 (SPIDER_NET_RXBUF_ALIGN - 1);
418 if (offset)
419 skb_reserve(descr->skb, SPIDER_NET_RXBUF_ALIGN - offset);
420 /* io-mmu-map the skb */
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500421 buf = pci_map_single(card->pdev, descr->skb->data,
Jens Osterkampbdd01502006-07-13 11:54:08 +0200422 SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500423 descr->buf_addr = buf;
Olof Johanssond4b0a4c2006-09-27 14:02:26 -0500424 if (pci_dma_mapping_error(buf)) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700425 dev_kfree_skb_any(descr->skb);
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500426 if (netif_msg_rx_err(card) && net_ratelimit())
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700427 pr_err("Could not iommu-map rx buffer\n");
Jim Lewis9b6b0b82006-09-22 17:22:53 -0700428 card->spider_stats.rx_iommu_map_error++;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200429 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700430 } else {
Jens Osterkampbdd01502006-07-13 11:54:08 +0200431 descr->dmac_cmd_status = SPIDER_NET_DESCR_CARDOWNED |
432 SPIDER_NET_DMAC_NOINTR_COMPLETE;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700433 }
434
435 return error;
436}
437
438/**
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500439 * spider_net_enable_rxchtails - sets RX dmac chain tail addresses
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700440 * @card: card structure
441 *
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500442 * spider_net_enable_rxchtails sets the RX DMAC chain tail adresses in the
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700443 * chip by writing to the appropriate register. DMA is enabled in
444 * spider_net_enable_rxdmac.
445 */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200446static inline void
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700447spider_net_enable_rxchtails(struct spider_net_card *card)
448{
449 /* assume chain is aligned correctly */
450 spider_net_write_reg(card, SPIDER_NET_GDADCHA ,
451 card->rx_chain.tail->bus_addr);
452}
453
454/**
455 * spider_net_enable_rxdmac - enables a receive DMA controller
456 * @card: card structure
457 *
458 * spider_net_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
459 * in the GDADMACCNTR register
460 */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200461static inline void
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700462spider_net_enable_rxdmac(struct spider_net_card *card)
463{
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500464 wmb();
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700465 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
466 SPIDER_NET_DMA_RX_VALUE);
467}
468
469/**
470 * spider_net_refill_rx_chain - refills descriptors/skbs in the rx chains
471 * @card: card structure
472 *
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500473 * refills descriptors in the rx chain: allocates skbs and iommu-maps them.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700474 */
475static void
476spider_net_refill_rx_chain(struct spider_net_card *card)
477{
Jens Osterkampbdd01502006-07-13 11:54:08 +0200478 struct spider_net_descr_chain *chain = &card->rx_chain;
479 unsigned long flags;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700480
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500481 /* one context doing the refill (and a second context seeing that
482 * and omitting it) is ok. If called by NAPI, we'll be called again
483 * as spider_net_decode_one_descr is called several times. If some
484 * interrupt calls us, the NAPI is about to clean up anyway. */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200485 if (!spin_trylock_irqsave(&chain->lock, flags))
486 return;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700487
Jens Osterkampbdd01502006-07-13 11:54:08 +0200488 while (spider_net_get_descr_status(chain->head) ==
489 SPIDER_NET_DESCR_NOT_IN_USE) {
490 if (spider_net_prepare_rx_descr(card, chain->head))
491 break;
492 chain->head = chain->head->next;
493 }
494
495 spin_unlock_irqrestore(&chain->lock, flags);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700496}
497
498/**
499 * spider_net_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
500 * @card: card structure
501 *
502 * returns 0 on success, <0 on failure
503 */
504static int
505spider_net_alloc_rx_skbs(struct spider_net_card *card)
506{
507 int result;
508 struct spider_net_descr_chain *chain;
509
510 result = -ENOMEM;
511
512 chain = &card->rx_chain;
513 /* put at least one buffer into the chain. if this fails,
514 * we've got a problem. if not, spider_net_refill_rx_chain
515 * will do the rest at the end of this function */
516 if (spider_net_prepare_rx_descr(card, chain->head))
517 goto error;
518 else
519 chain->head = chain->head->next;
520
521 /* this will allocate the rest of the rx buffers; if not, it's
522 * business as usual later on */
523 spider_net_refill_rx_chain(card);
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500524 spider_net_enable_rxdmac(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700525 return 0;
526
527error:
528 spider_net_free_rx_chain_contents(card);
529 return result;
530}
531
532/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700533 * spider_net_get_multicast_hash - generates hash for multicast filter table
534 * @addr: multicast address
535 *
536 * returns the hash value.
537 *
538 * spider_net_get_multicast_hash calculates a hash value for a given multicast
539 * address, that is used to set the multicast filter tables
540 */
541static u8
542spider_net_get_multicast_hash(struct net_device *netdev, __u8 *addr)
543{
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700544 u32 crc;
545 u8 hash;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500546 char addr_for_crc[ETH_ALEN] = { 0, };
547 int i, bit;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700548
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500549 for (i = 0; i < ETH_ALEN * 8; i++) {
550 bit = (addr[i / 8] >> (i % 8)) & 1;
551 addr_for_crc[ETH_ALEN - 1 - i / 8] += bit << (7 - (i % 8));
552 }
553
554 crc = crc32_be(~0, addr_for_crc, netdev->addr_len);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700555
556 hash = (crc >> 27);
557 hash <<= 3;
558 hash |= crc & 7;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500559 hash &= 0xff;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700560
561 return hash;
562}
563
564/**
565 * spider_net_set_multi - sets multicast addresses and promisc flags
566 * @netdev: interface device structure
567 *
568 * spider_net_set_multi configures multicast addresses as needed for the
569 * netdev interface. It also sets up multicast, allmulti and promisc
570 * flags appropriately
571 */
572static void
573spider_net_set_multi(struct net_device *netdev)
574{
575 struct dev_mc_list *mc;
576 u8 hash;
577 int i;
578 u32 reg;
579 struct spider_net_card *card = netdev_priv(netdev);
580 unsigned long bitmask[SPIDER_NET_MULTICAST_HASHES / BITS_PER_LONG] =
581 {0, };
582
583 spider_net_set_promisc(card);
584
585 if (netdev->flags & IFF_ALLMULTI) {
586 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES; i++) {
587 set_bit(i, bitmask);
588 }
589 goto write_hash;
590 }
591
592 /* well, we know, what the broadcast hash value is: it's xfd
593 hash = spider_net_get_multicast_hash(netdev, netdev->broadcast); */
594 set_bit(0xfd, bitmask);
595
596 for (mc = netdev->mc_list; mc; mc = mc->next) {
597 hash = spider_net_get_multicast_hash(netdev, mc->dmi_addr);
598 set_bit(hash, bitmask);
599 }
600
601write_hash:
602 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES / 4; i++) {
603 reg = 0;
604 if (test_bit(i * 4, bitmask))
605 reg += 0x08;
606 reg <<= 8;
607 if (test_bit(i * 4 + 1, bitmask))
608 reg += 0x08;
609 reg <<= 8;
610 if (test_bit(i * 4 + 2, bitmask))
611 reg += 0x08;
612 reg <<= 8;
613 if (test_bit(i * 4 + 3, bitmask))
614 reg += 0x08;
615
616 spider_net_write_reg(card, SPIDER_NET_GMRMHFILnR + i * 4, reg);
617 }
618}
619
620/**
621 * spider_net_disable_rxdmac - disables the receive DMA controller
622 * @card: card structure
623 *
624 * spider_net_disable_rxdmac terminates processing on the DMA controller by
625 * turing off DMA and issueing a force end
626 */
627static void
628spider_net_disable_rxdmac(struct spider_net_card *card)
629{
630 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
631 SPIDER_NET_DMA_RX_FEND_VALUE);
632}
633
634/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700635 * spider_net_prepare_tx_descr - fill tx descriptor with skb data
636 * @card: card structure
637 * @descr: descriptor structure to fill out
638 * @skb: packet to use
639 *
640 * returns 0 on success, <0 on failure.
641 *
642 * fills out the descriptor structure with skb data and len. Copies data,
643 * if needed (32bit DMA!)
644 */
645static int
646spider_net_prepare_tx_descr(struct spider_net_card *card,
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700647 struct sk_buff *skb)
648{
Jens Osterkampbdd01502006-07-13 11:54:08 +0200649 struct spider_net_descr *descr = card->tx_chain.head;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500650 dma_addr_t buf;
Linas Vepstas808999c2006-10-10 16:01:00 -0500651 int length;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500652
Linas Vepstas808999c2006-10-10 16:01:00 -0500653 length = skb->len;
654 if (length < ETH_ZLEN) {
655 if (skb_pad(skb, ETH_ZLEN-length))
656 return 0;
657 length = ETH_ZLEN;
658 }
659
660 buf = pci_map_single(card->pdev, skb->data, length, PCI_DMA_TODEVICE);
Olof Johanssond4b0a4c2006-09-27 14:02:26 -0500661 if (pci_dma_mapping_error(buf)) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500662 if (netif_msg_tx_err(card) && net_ratelimit())
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700663 pr_err("could not iommu-map packet (%p, %i). "
Linas Vepstas808999c2006-10-10 16:01:00 -0500664 "Dropping packet\n", skb->data, length);
Jim Lewis9b6b0b82006-09-22 17:22:53 -0700665 card->spider_stats.tx_iommu_map_error++;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700666 return -ENOMEM;
667 }
668
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500669 descr->buf_addr = buf;
Linas Vepstas808999c2006-10-10 16:01:00 -0500670 descr->buf_size = length;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200671 descr->next_descr_addr = 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700672 descr->skb = skb;
673 descr->data_status = 0;
674
Jens Osterkampbdd01502006-07-13 11:54:08 +0200675 descr->dmac_cmd_status =
676 SPIDER_NET_DESCR_CARDOWNED | SPIDER_NET_DMAC_NOCS;
677 if (skb->protocol == htons(ETH_P_IP))
678 switch (skb->nh.iph->protocol) {
679 case IPPROTO_TCP:
680 descr->dmac_cmd_status |= SPIDER_NET_DMAC_TCP;
681 break;
682 case IPPROTO_UDP:
683 descr->dmac_cmd_status |= SPIDER_NET_DMAC_UDP;
684 break;
685 }
686
687 descr->prev->next_descr_addr = descr->bus_addr;
688
Linas Vepstas917a5b82006-10-10 16:01:51 -0500689 card->netdev->trans_start = jiffies; /* set netdev watchdog timer */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200690 return 0;
691}
692
693/**
694 * spider_net_release_tx_descr - processes a used tx descriptor
695 * @card: card structure
696 * @descr: descriptor to release
697 *
698 * releases a used tx descriptor (unmapping, freeing of skb)
699 */
700static inline void
701spider_net_release_tx_descr(struct spider_net_card *card)
702{
703 struct spider_net_descr *descr = card->tx_chain.tail;
704 struct sk_buff *skb;
Linas Vepstas808999c2006-10-10 16:01:00 -0500705 unsigned int len;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200706
707 card->tx_chain.tail = card->tx_chain.tail->next;
708 descr->dmac_cmd_status |= SPIDER_NET_DESCR_NOT_IN_USE;
709
710 /* unmap the skb */
711 skb = descr->skb;
Linas Vepstasc3fee4c2006-10-10 16:00:04 -0500712 if (!skb)
713 return;
Linas Vepstas808999c2006-10-10 16:01:00 -0500714 len = skb->len < ETH_ZLEN ? ETH_ZLEN : skb->len;
715 pci_unmap_single(card->pdev, descr->buf_addr, len,
Jens Osterkampbdd01502006-07-13 11:54:08 +0200716 PCI_DMA_TODEVICE);
717 dev_kfree_skb_any(skb);
718}
719
720/**
721 * spider_net_release_tx_chain - processes sent tx descriptors
722 * @card: adapter structure
723 * @brutal: if set, don't care about whether descriptor seems to be in use
724 *
725 * returns 0 if the tx ring is empty, otherwise 1.
726 *
727 * spider_net_release_tx_chain releases the tx descriptors that spider has
728 * finished with (if non-brutal) or simply release tx descriptors (if brutal).
729 * If some other context is calling this function, we return 1 so that we're
730 * scheduled again (if we were scheduled) and will not loose initiative.
731 */
732static int
733spider_net_release_tx_chain(struct spider_net_card *card, int brutal)
734{
735 struct spider_net_descr_chain *chain = &card->tx_chain;
736 int status;
737
738 spider_net_read_reg(card, SPIDER_NET_GDTDMACCNTR);
739
740 while (chain->tail != chain->head) {
741 status = spider_net_get_descr_status(chain->tail);
742 switch (status) {
743 case SPIDER_NET_DESCR_COMPLETE:
744 card->netdev_stats.tx_packets++;
745 card->netdev_stats.tx_bytes += chain->tail->skb->len;
746 break;
747
748 case SPIDER_NET_DESCR_CARDOWNED:
749 if (!brutal)
750 return 1;
751 /* fallthrough, if we release the descriptors
752 * brutally (then we don't care about
753 * SPIDER_NET_DESCR_CARDOWNED) */
754
755 case SPIDER_NET_DESCR_RESPONSE_ERROR:
756 case SPIDER_NET_DESCR_PROTECTION_ERROR:
757 case SPIDER_NET_DESCR_FORCE_END:
758 if (netif_msg_tx_err(card))
759 pr_err("%s: forcing end of tx descriptor "
760 "with status x%02x\n",
761 card->netdev->name, status);
762 card->netdev_stats.tx_errors++;
763 break;
764
765 default:
766 card->netdev_stats.tx_dropped++;
Linas Vepstasc3fee4c2006-10-10 16:00:04 -0500767 if (!brutal)
768 return 1;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200769 }
770 spider_net_release_tx_descr(card);
771 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700772
773 return 0;
774}
775
776/**
777 * spider_net_kick_tx_dma - enables TX DMA processing
778 * @card: card structure
779 * @descr: descriptor address to enable TX processing at
780 *
781 * spider_net_kick_tx_dma writes the current tx chain head as start address
782 * of the tx descriptor chain and enables the transmission DMA engine
783 */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200784static inline void
785spider_net_kick_tx_dma(struct spider_net_card *card)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700786{
Jens Osterkampbdd01502006-07-13 11:54:08 +0200787 struct spider_net_descr *descr;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700788
Jens Osterkampbdd01502006-07-13 11:54:08 +0200789 if (spider_net_read_reg(card, SPIDER_NET_GDTDMACCNTR) &
790 SPIDER_NET_TX_DMA_EN)
791 goto out;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700792
Jens Osterkampbdd01502006-07-13 11:54:08 +0200793 descr = card->tx_chain.tail;
794 for (;;) {
795 if (spider_net_get_descr_status(descr) ==
796 SPIDER_NET_DESCR_CARDOWNED) {
797 spider_net_write_reg(card, SPIDER_NET_GDTDCHA,
798 descr->bus_addr);
799 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
800 SPIDER_NET_DMA_TX_VALUE);
801 break;
802 }
803 if (descr == card->tx_chain.head)
804 break;
805 descr = descr->next;
806 }
807
808out:
809 mod_timer(&card->tx_timer, jiffies + SPIDER_NET_TX_TIMER);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700810}
811
812/**
813 * spider_net_xmit - transmits a frame over the device
814 * @skb: packet to send out
815 * @netdev: interface device structure
816 *
Jens Osterkampbdd01502006-07-13 11:54:08 +0200817 * returns 0 on success, !0 on failure
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700818 */
819static int
820spider_net_xmit(struct sk_buff *skb, struct net_device *netdev)
821{
822 struct spider_net_card *card = netdev_priv(netdev);
Jens Osterkampbdd01502006-07-13 11:54:08 +0200823 struct spider_net_descr_chain *chain = &card->tx_chain;
824 struct spider_net_descr *descr = chain->head;
825 unsigned long flags;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700826
Jens Osterkampbdd01502006-07-13 11:54:08 +0200827 spin_lock_irqsave(&chain->lock, flags);
828
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500829 spider_net_release_tx_chain(card, 0);
830
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500831 if ((chain->head->next == chain->tail->prev) ||
832 (spider_net_get_descr_status(descr) != SPIDER_NET_DESCR_NOT_IN_USE) ||
833 (spider_net_prepare_tx_descr(card, skb) != 0)) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500834
Jim Lewis9b6b0b82006-09-22 17:22:53 -0700835 card->netdev_stats.tx_dropped++;
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500836 spin_unlock_irqrestore(&chain->lock, flags);
837 netif_stop_queue(netdev);
838 return NETDEV_TX_BUSY;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200839 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700840
Jens Osterkampbdd01502006-07-13 11:54:08 +0200841 spider_net_kick_tx_dma(card);
842 card->tx_chain.head = card->tx_chain.head->next;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200843 spin_unlock_irqrestore(&chain->lock, flags);
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500844 return NETDEV_TX_OK;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200845}
846
847/**
848 * spider_net_cleanup_tx_ring - cleans up the TX ring
849 * @card: card structure
850 *
851 * spider_net_cleanup_tx_ring is called by the tx_timer (as we don't use
852 * interrupts to cleanup our TX ring) and returns sent packets to the stack
853 * by freeing them
854 */
855static void
856spider_net_cleanup_tx_ring(struct spider_net_card *card)
857{
858 unsigned long flags;
859
860 spin_lock_irqsave(&card->tx_chain.lock, flags);
861
862 if ((spider_net_release_tx_chain(card, 0) != 0) &&
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500863 (card->netdev->flags & IFF_UP)) {
Jens Osterkampbdd01502006-07-13 11:54:08 +0200864 spider_net_kick_tx_dma(card);
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500865 netif_wake_queue(card->netdev);
866 }
Jens Osterkampbdd01502006-07-13 11:54:08 +0200867 spin_unlock_irqrestore(&card->tx_chain.lock, flags);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700868}
869
870/**
871 * spider_net_do_ioctl - called for device ioctls
872 * @netdev: interface device structure
873 * @ifr: request parameter structure for ioctl
874 * @cmd: command code for ioctl
875 *
876 * returns 0 on success, <0 on failure. Currently, we have no special ioctls.
877 * -EOPNOTSUPP is returned, if an unknown ioctl was requested
878 */
879static int
880spider_net_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
881{
882 switch (cmd) {
883 default:
884 return -EOPNOTSUPP;
885 }
886}
887
888/**
889 * spider_net_pass_skb_up - takes an skb from a descriptor and passes it on
890 * @descr: descriptor to process
891 * @card: card structure
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500892 * @napi: whether caller is in NAPI context
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700893 *
894 * returns 1 on success, 0 if no packet was passed to the stack
895 *
896 * iommu-unmaps the skb, fills out skb structure and passes the data to the
897 * stack. The descriptor state is not changed.
898 */
899static int
900spider_net_pass_skb_up(struct spider_net_descr *descr,
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500901 struct spider_net_card *card, int napi)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700902{
903 struct sk_buff *skb;
904 struct net_device *netdev;
905 u32 data_status, data_error;
906
907 data_status = descr->data_status;
908 data_error = descr->data_error;
909
910 netdev = card->netdev;
911
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500912 /* unmap descriptor */
913 pci_unmap_single(card->pdev, descr->buf_addr, SPIDER_NET_MAX_FRAME,
Jens Osterkampbdd01502006-07-13 11:54:08 +0200914 PCI_DMA_FROMDEVICE);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700915
916 /* the cases we'll throw away the packet immediately */
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500917 if (data_error & SPIDER_NET_DESTROY_RX_FLAGS) {
918 if (netif_msg_rx_err(card))
919 pr_err("error in received descriptor found, "
920 "data_status=x%08x, data_error=x%08x\n",
921 data_status, data_error);
Jim Lewis9b6b0b82006-09-22 17:22:53 -0700922 card->spider_stats.rx_desc_error++;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700923 return 0;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500924 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700925
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500926 skb = descr->skb;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700927 skb->dev = netdev;
928 skb_put(skb, descr->valid_size);
929
930 /* the card seems to add 2 bytes of junk in front
931 * of the ethernet frame */
932#define SPIDER_MISALIGN 2
933 skb_pull(skb, SPIDER_MISALIGN);
934 skb->protocol = eth_type_trans(skb, netdev);
935
936 /* checksum offload */
937 if (card->options.rx_csum) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500938 if ( ( (data_status & SPIDER_NET_DATA_STATUS_CKSUM_MASK) ==
939 SPIDER_NET_DATA_STATUS_CKSUM_MASK) &&
940 !(data_error & SPIDER_NET_DATA_ERR_CKSUM_MASK))
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700941 skb->ip_summed = CHECKSUM_UNNECESSARY;
942 else
943 skb->ip_summed = CHECKSUM_NONE;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500944 } else
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700945 skb->ip_summed = CHECKSUM_NONE;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700946
947 if (data_status & SPIDER_NET_VLAN_PACKET) {
948 /* further enhancements: HW-accel VLAN
949 * vlan_hwaccel_receive_skb
950 */
951 }
952
953 /* pass skb up to stack */
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500954 if (napi)
955 netif_receive_skb(skb);
956 else
957 netif_rx_ni(skb);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700958
959 /* update netdevice statistics */
960 card->netdev_stats.rx_packets++;
961 card->netdev_stats.rx_bytes += skb->len;
962
963 return 1;
964}
965
966/**
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500967 * spider_net_decode_one_descr - processes an rx descriptor
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700968 * @card: card structure
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500969 * @napi: whether caller is in NAPI context
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700970 *
971 * returns 1 if a packet has been sent to the stack, otherwise 0
972 *
973 * processes an rx descriptor by iommu-unmapping the data buffer and passing
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500974 * the packet up to the stack. This function is called in softirq
975 * context, e.g. either bottom half from interrupt or NAPI polling context
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700976 */
977static int
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500978spider_net_decode_one_descr(struct spider_net_card *card, int napi)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700979{
Jens Osterkampbdd01502006-07-13 11:54:08 +0200980 struct spider_net_descr_chain *chain = &card->rx_chain;
981 struct spider_net_descr *descr = chain->tail;
982 int status;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700983 int result;
984
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700985 status = spider_net_get_descr_status(descr);
986
987 if (status == SPIDER_NET_DESCR_CARDOWNED) {
988 /* nothing in the descriptor yet */
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500989 result=0;
990 goto out;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700991 }
992
993 if (status == SPIDER_NET_DESCR_NOT_IN_USE) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500994 /* not initialized yet, the ring must be empty */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700995 spider_net_refill_rx_chain(card);
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500996 spider_net_enable_rxdmac(card);
997 result=0;
998 goto out;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700999 }
1000
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001001 /* descriptor definitively used -- move on tail */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001002 chain->tail = descr->next;
1003
1004 result = 0;
1005 if ( (status == SPIDER_NET_DESCR_RESPONSE_ERROR) ||
1006 (status == SPIDER_NET_DESCR_PROTECTION_ERROR) ||
1007 (status == SPIDER_NET_DESCR_FORCE_END) ) {
1008 if (netif_msg_rx_err(card))
1009 pr_err("%s: dropping RX descriptor with state %d\n",
1010 card->netdev->name, status);
1011 card->netdev_stats.rx_dropped++;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001012 pci_unmap_single(card->pdev, descr->buf_addr,
Jens Osterkampbdd01502006-07-13 11:54:08 +02001013 SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001014 dev_kfree_skb_irq(descr->skb);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001015 goto refill;
1016 }
1017
1018 if ( (status != SPIDER_NET_DESCR_COMPLETE) &&
1019 (status != SPIDER_NET_DESCR_FRAME_END) ) {
Jim Lewis9b6b0b82006-09-22 17:22:53 -07001020 if (netif_msg_rx_err(card)) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001021 pr_err("%s: RX descriptor with state %d\n",
1022 card->netdev->name, status);
Jim Lewis9b6b0b82006-09-22 17:22:53 -07001023 card->spider_stats.rx_desc_unk_state++;
1024 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001025 goto refill;
1026 }
1027
1028 /* ok, we've got a packet in descr */
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001029 result = spider_net_pass_skb_up(descr, card, napi);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001030refill:
Jens Osterkampbdd01502006-07-13 11:54:08 +02001031 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001032 /* change the descriptor state: */
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001033 if (!napi)
1034 spider_net_refill_rx_chain(card);
1035out:
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001036 return result;
1037}
1038
1039/**
1040 * spider_net_poll - NAPI poll function called by the stack to return packets
1041 * @netdev: interface device structure
1042 * @budget: number of packets we can pass to the stack at most
1043 *
1044 * returns 0 if no more packets available to the driver/stack. Returns 1,
1045 * if the quota is exceeded, but the driver has still packets.
1046 *
1047 * spider_net_poll returns all packets from the rx descriptors to the stack
1048 * (using netif_receive_skb). If all/enough packets are up, the driver
1049 * reenables interrupts and returns 0. If not, 1 is returned.
1050 */
1051static int
1052spider_net_poll(struct net_device *netdev, int *budget)
1053{
1054 struct spider_net_card *card = netdev_priv(netdev);
1055 int packets_to_do, packets_done = 0;
1056 int no_more_packets = 0;
1057
1058 packets_to_do = min(*budget, netdev->quota);
1059
1060 while (packets_to_do) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001061 if (spider_net_decode_one_descr(card, 1)) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001062 packets_done++;
1063 packets_to_do--;
1064 } else {
1065 /* no more packets for the stack */
1066 no_more_packets = 1;
1067 break;
1068 }
1069 }
1070
1071 netdev->quota -= packets_done;
1072 *budget -= packets_done;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001073 spider_net_refill_rx_chain(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001074
1075 /* if all packets are in the stack, enable interrupts and return 0 */
1076 /* if not, return 1 */
1077 if (no_more_packets) {
1078 netif_rx_complete(netdev);
1079 spider_net_rx_irq_on(card);
1080 return 0;
1081 }
1082
1083 return 1;
1084}
1085
1086/**
1087 * spider_net_vlan_rx_reg - initializes VLAN structures in the driver and card
1088 * @netdev: interface device structure
1089 * @grp: vlan_group structure that is registered (NULL on destroying interface)
1090 */
1091static void
1092spider_net_vlan_rx_reg(struct net_device *netdev, struct vlan_group *grp)
1093{
1094 /* further enhancement... yet to do */
1095 return;
1096}
1097
1098/**
1099 * spider_net_vlan_rx_add - adds VLAN id to the card filter
1100 * @netdev: interface device structure
1101 * @vid: VLAN id to add
1102 */
1103static void
1104spider_net_vlan_rx_add(struct net_device *netdev, uint16_t vid)
1105{
1106 /* further enhancement... yet to do */
1107 /* add vid to card's VLAN filter table */
1108 return;
1109}
1110
1111/**
1112 * spider_net_vlan_rx_kill - removes VLAN id to the card filter
1113 * @netdev: interface device structure
1114 * @vid: VLAN id to remove
1115 */
1116static void
1117spider_net_vlan_rx_kill(struct net_device *netdev, uint16_t vid)
1118{
1119 /* further enhancement... yet to do */
1120 /* remove vid from card's VLAN filter table */
1121}
1122
1123/**
1124 * spider_net_get_stats - get interface statistics
1125 * @netdev: interface device structure
1126 *
1127 * returns the interface statistics residing in the spider_net_card struct
1128 */
1129static struct net_device_stats *
1130spider_net_get_stats(struct net_device *netdev)
1131{
1132 struct spider_net_card *card = netdev_priv(netdev);
1133 struct net_device_stats *stats = &card->netdev_stats;
1134 return stats;
1135}
1136
1137/**
1138 * spider_net_change_mtu - changes the MTU of an interface
1139 * @netdev: interface device structure
1140 * @new_mtu: new MTU value
1141 *
1142 * returns 0 on success, <0 on failure
1143 */
1144static int
1145spider_net_change_mtu(struct net_device *netdev, int new_mtu)
1146{
1147 /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
1148 * and mtu is outbound only anyway */
1149 if ( (new_mtu < SPIDER_NET_MIN_MTU ) ||
1150 (new_mtu > SPIDER_NET_MAX_MTU) )
1151 return -EINVAL;
1152 netdev->mtu = new_mtu;
1153 return 0;
1154}
1155
1156/**
1157 * spider_net_set_mac - sets the MAC of an interface
1158 * @netdev: interface device structure
1159 * @ptr: pointer to new MAC address
1160 *
1161 * Returns 0 on success, <0 on failure. Currently, we don't support this
1162 * and will always return EOPNOTSUPP.
1163 */
1164static int
1165spider_net_set_mac(struct net_device *netdev, void *p)
1166{
1167 struct spider_net_card *card = netdev_priv(netdev);
Jens Osterkamp054034d2005-09-06 19:30:55 -07001168 u32 macl, macu, regvalue;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001169 struct sockaddr *addr = p;
1170
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001171 if (!is_valid_ether_addr(addr->sa_data))
1172 return -EADDRNOTAVAIL;
1173
Jens Osterkamp054034d2005-09-06 19:30:55 -07001174 /* switch off GMACTPE and GMACRPE */
1175 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1176 regvalue &= ~((1 << 5) | (1 << 6));
1177 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1178
1179 /* write mac */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001180 macu = (addr->sa_data[0]<<24) + (addr->sa_data[1]<<16) +
1181 (addr->sa_data[2]<<8) + (addr->sa_data[3]);
1182 macl = (addr->sa_data[4]<<8) + (addr->sa_data[5]);
1183 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACU, macu);
1184 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACL, macl);
1185
Jens Osterkamp054034d2005-09-06 19:30:55 -07001186 /* switch GMACTPE and GMACRPE back on */
1187 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1188 regvalue |= ((1 << 5) | (1 << 6));
1189 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1190
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001191 spider_net_set_promisc(card);
1192
1193 /* look up, whether we have been successful */
1194 if (spider_net_get_mac_address(netdev))
1195 return -EADDRNOTAVAIL;
1196 if (memcmp(netdev->dev_addr,addr->sa_data,netdev->addr_len))
1197 return -EADDRNOTAVAIL;
1198
1199 return 0;
1200}
1201
1202/**
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001203 * spider_net_handle_rxram_full - cleans up RX ring upon RX RAM full interrupt
1204 * @card: card structure
1205 *
1206 * spider_net_handle_rxram_full empties the RX ring so that spider can put
1207 * more packets in it and empty its RX RAM. This is called in bottom half
1208 * context
1209 */
1210static void
1211spider_net_handle_rxram_full(struct spider_net_card *card)
1212{
1213 while (spider_net_decode_one_descr(card, 0))
1214 ;
1215 spider_net_enable_rxchtails(card);
1216 spider_net_enable_rxdmac(card);
1217 netif_rx_schedule(card->netdev);
1218}
1219
1220/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001221 * spider_net_handle_error_irq - handles errors raised by an interrupt
1222 * @card: card structure
1223 * @status_reg: interrupt status register 0 (GHIINT0STS)
1224 *
1225 * spider_net_handle_error_irq treats or ignores all error conditions
1226 * found when an interrupt is presented
1227 */
1228static void
1229spider_net_handle_error_irq(struct spider_net_card *card, u32 status_reg)
1230{
1231 u32 error_reg1, error_reg2;
1232 u32 i;
1233 int show_error = 1;
1234
1235 error_reg1 = spider_net_read_reg(card, SPIDER_NET_GHIINT1STS);
1236 error_reg2 = spider_net_read_reg(card, SPIDER_NET_GHIINT2STS);
1237
1238 /* check GHIINT0STS ************************************/
1239 if (status_reg)
1240 for (i = 0; i < 32; i++)
1241 if (status_reg & (1<<i))
1242 switch (i)
1243 {
1244 /* let error_reg1 and error_reg2 evaluation decide, what to do
1245 case SPIDER_NET_PHYINT:
1246 case SPIDER_NET_GMAC2INT:
1247 case SPIDER_NET_GMAC1INT:
1248 case SPIDER_NET_GIPSINT:
1249 case SPIDER_NET_GFIFOINT:
1250 case SPIDER_NET_DMACINT:
1251 case SPIDER_NET_GSYSINT:
1252 break; */
1253
1254 case SPIDER_NET_GPWOPCMPINT:
1255 /* PHY write operation completed */
1256 show_error = 0;
1257 break;
1258 case SPIDER_NET_GPROPCMPINT:
1259 /* PHY read operation completed */
1260 /* we don't use semaphores, as we poll for the completion
1261 * of the read operation in spider_net_read_phy. Should take
1262 * about 50 us */
1263 show_error = 0;
1264 break;
1265 case SPIDER_NET_GPWFFINT:
1266 /* PHY command queue full */
1267 if (netif_msg_intr(card))
1268 pr_err("PHY write queue full\n");
1269 show_error = 0;
1270 break;
1271
1272 /* case SPIDER_NET_GRMDADRINT: not used. print a message */
1273 /* case SPIDER_NET_GRMARPINT: not used. print a message */
1274 /* case SPIDER_NET_GRMMPINT: not used. print a message */
1275
1276 case SPIDER_NET_GDTDEN0INT:
1277 /* someone has set TX_DMA_EN to 0 */
1278 show_error = 0;
1279 break;
1280
1281 case SPIDER_NET_GDDDEN0INT: /* fallthrough */
1282 case SPIDER_NET_GDCDEN0INT: /* fallthrough */
1283 case SPIDER_NET_GDBDEN0INT: /* fallthrough */
1284 case SPIDER_NET_GDADEN0INT:
1285 /* someone has set RX_DMA_EN to 0 */
1286 show_error = 0;
1287 break;
1288
1289 /* RX interrupts */
1290 case SPIDER_NET_GDDFDCINT:
1291 case SPIDER_NET_GDCFDCINT:
1292 case SPIDER_NET_GDBFDCINT:
1293 case SPIDER_NET_GDAFDCINT:
1294 /* case SPIDER_NET_GDNMINT: not used. print a message */
1295 /* case SPIDER_NET_GCNMINT: not used. print a message */
1296 /* case SPIDER_NET_GBNMINT: not used. print a message */
1297 /* case SPIDER_NET_GANMINT: not used. print a message */
1298 /* case SPIDER_NET_GRFNMINT: not used. print a message */
1299 show_error = 0;
1300 break;
1301
1302 /* TX interrupts */
1303 case SPIDER_NET_GDTFDCINT:
1304 show_error = 0;
1305 break;
1306 case SPIDER_NET_GTTEDINT:
1307 show_error = 0;
1308 break;
1309 case SPIDER_NET_GDTDCEINT:
1310 /* chain end. If a descriptor should be sent, kick off
1311 * tx dma
1312 if (card->tx_chain.tail == card->tx_chain.head)
1313 spider_net_kick_tx_dma(card);
1314 show_error = 0; */
1315 break;
1316
1317 /* case SPIDER_NET_G1TMCNTINT: not used. print a message */
1318 /* case SPIDER_NET_GFREECNTINT: not used. print a message */
1319 }
1320
1321 /* check GHIINT1STS ************************************/
1322 if (error_reg1)
1323 for (i = 0; i < 32; i++)
1324 if (error_reg1 & (1<<i))
1325 switch (i)
1326 {
1327 case SPIDER_NET_GTMFLLINT:
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001328 if (netif_msg_intr(card) && net_ratelimit())
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001329 pr_err("Spider TX RAM full\n");
1330 show_error = 0;
1331 break;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001332 case SPIDER_NET_GRFDFLLINT: /* fallthrough */
1333 case SPIDER_NET_GRFCFLLINT: /* fallthrough */
1334 case SPIDER_NET_GRFBFLLINT: /* fallthrough */
1335 case SPIDER_NET_GRFAFLLINT: /* fallthrough */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001336 case SPIDER_NET_GRMFLLINT:
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001337 if (netif_msg_intr(card) && net_ratelimit())
Jens Osterkamp4e5077b2006-03-28 15:59:55 +02001338 pr_debug("Spider RX RAM full, incoming packets "
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001339 "might be discarded!\n");
1340 spider_net_rx_irq_off(card);
1341 tasklet_schedule(&card->rxram_full_tl);
1342 show_error = 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001343 break;
1344
1345 /* case SPIDER_NET_GTMSHTINT: problem, print a message */
1346 case SPIDER_NET_GDTINVDINT:
1347 /* allrighty. tx from previous descr ok */
1348 show_error = 0;
1349 break;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001350
1351 /* chain end */
1352 case SPIDER_NET_GDDDCEINT: /* fallthrough */
1353 case SPIDER_NET_GDCDCEINT: /* fallthrough */
1354 case SPIDER_NET_GDBDCEINT: /* fallthrough */
1355 case SPIDER_NET_GDADCEINT:
1356 if (netif_msg_intr(card))
1357 pr_err("got descriptor chain end interrupt, "
1358 "restarting DMAC %c.\n",
Linas Vepstas37aad752006-10-10 16:06:53 -05001359 'D'-(i-SPIDER_NET_GDDDCEINT)/3);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001360 spider_net_refill_rx_chain(card);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001361 spider_net_enable_rxdmac(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001362 show_error = 0;
1363 break;
1364
1365 /* invalid descriptor */
1366 case SPIDER_NET_GDDINVDINT: /* fallthrough */
1367 case SPIDER_NET_GDCINVDINT: /* fallthrough */
1368 case SPIDER_NET_GDBINVDINT: /* fallthrough */
1369 case SPIDER_NET_GDAINVDINT:
1370 /* could happen when rx chain is full */
1371 spider_net_refill_rx_chain(card);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001372 spider_net_enable_rxdmac(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001373 show_error = 0;
1374 break;
1375
1376 /* case SPIDER_NET_GDTRSERINT: problem, print a message */
1377 /* case SPIDER_NET_GDDRSERINT: problem, print a message */
1378 /* case SPIDER_NET_GDCRSERINT: problem, print a message */
1379 /* case SPIDER_NET_GDBRSERINT: problem, print a message */
1380 /* case SPIDER_NET_GDARSERINT: problem, print a message */
1381 /* case SPIDER_NET_GDSERINT: problem, print a message */
1382 /* case SPIDER_NET_GDTPTERINT: problem, print a message */
1383 /* case SPIDER_NET_GDDPTERINT: problem, print a message */
1384 /* case SPIDER_NET_GDCPTERINT: problem, print a message */
1385 /* case SPIDER_NET_GDBPTERINT: problem, print a message */
1386 /* case SPIDER_NET_GDAPTERINT: problem, print a message */
1387 default:
1388 show_error = 1;
1389 break;
1390 }
1391
1392 /* check GHIINT2STS ************************************/
1393 if (error_reg2)
1394 for (i = 0; i < 32; i++)
1395 if (error_reg2 & (1<<i))
1396 switch (i)
1397 {
1398 /* there is nothing we can (want to) do at this time. Log a
1399 * message, we can switch on and off the specific values later on
1400 case SPIDER_NET_GPROPERINT:
1401 case SPIDER_NET_GMCTCRSNGINT:
1402 case SPIDER_NET_GMCTLCOLINT:
1403 case SPIDER_NET_GMCTTMOTINT:
1404 case SPIDER_NET_GMCRCAERINT:
1405 case SPIDER_NET_GMCRCALERINT:
1406 case SPIDER_NET_GMCRALNERINT:
1407 case SPIDER_NET_GMCROVRINT:
1408 case SPIDER_NET_GMCRRNTINT:
1409 case SPIDER_NET_GMCRRXERINT:
1410 case SPIDER_NET_GTITCSERINT:
1411 case SPIDER_NET_GTIFMTERINT:
1412 case SPIDER_NET_GTIPKTRVKINT:
1413 case SPIDER_NET_GTISPINGINT:
1414 case SPIDER_NET_GTISADNGINT:
1415 case SPIDER_NET_GTISPDNGINT:
1416 case SPIDER_NET_GRIFMTERINT:
1417 case SPIDER_NET_GRIPKTRVKINT:
1418 case SPIDER_NET_GRISPINGINT:
1419 case SPIDER_NET_GRISADNGINT:
1420 case SPIDER_NET_GRISPDNGINT:
1421 break;
1422 */
1423 default:
1424 break;
1425 }
1426
1427 if ((show_error) && (netif_msg_intr(card)))
1428 pr_err("Got error interrupt, GHIINT0STS = 0x%08x, "
1429 "GHIINT1STS = 0x%08x, GHIINT2STS = 0x%08x\n",
1430 status_reg, error_reg1, error_reg2);
1431
1432 /* clear interrupt sources */
1433 spider_net_write_reg(card, SPIDER_NET_GHIINT1STS, error_reg1);
1434 spider_net_write_reg(card, SPIDER_NET_GHIINT2STS, error_reg2);
1435}
1436
1437/**
1438 * spider_net_interrupt - interrupt handler for spider_net
1439 * @irq: interupt number
1440 * @ptr: pointer to net_device
1441 * @regs: PU registers
1442 *
1443 * returns IRQ_HANDLED, if interrupt was for driver, or IRQ_NONE, if no
1444 * interrupt found raised by card.
1445 *
1446 * This is the interrupt handler, that turns off
1447 * interrupts for this device and makes the stack poll the driver
1448 */
1449static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001450spider_net_interrupt(int irq, void *ptr)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001451{
1452 struct net_device *netdev = ptr;
1453 struct spider_net_card *card = netdev_priv(netdev);
1454 u32 status_reg;
1455
1456 status_reg = spider_net_read_reg(card, SPIDER_NET_GHIINT0STS);
1457
1458 if (!status_reg)
1459 return IRQ_NONE;
1460
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001461 if (status_reg & SPIDER_NET_RXINT ) {
1462 spider_net_rx_irq_off(card);
1463 netif_rx_schedule(netdev);
1464 }
1465
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001466 if (status_reg & SPIDER_NET_ERRINT )
1467 spider_net_handle_error_irq(card, status_reg);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001468
1469 /* clear interrupt sources */
1470 spider_net_write_reg(card, SPIDER_NET_GHIINT0STS, status_reg);
1471
1472 return IRQ_HANDLED;
1473}
1474
1475#ifdef CONFIG_NET_POLL_CONTROLLER
1476/**
1477 * spider_net_poll_controller - artificial interrupt for netconsole etc.
1478 * @netdev: interface device structure
1479 *
1480 * see Documentation/networking/netconsole.txt
1481 */
1482static void
1483spider_net_poll_controller(struct net_device *netdev)
1484{
1485 disable_irq(netdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01001486 spider_net_interrupt(netdev->irq, netdev);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001487 enable_irq(netdev->irq);
1488}
1489#endif /* CONFIG_NET_POLL_CONTROLLER */
1490
1491/**
1492 * spider_net_init_card - initializes the card
1493 * @card: card structure
1494 *
1495 * spider_net_init_card initializes the card so that other registers can
1496 * be used
1497 */
1498static void
1499spider_net_init_card(struct spider_net_card *card)
1500{
1501 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1502 SPIDER_NET_CKRCTRL_STOP_VALUE);
1503
1504 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1505 SPIDER_NET_CKRCTRL_RUN_VALUE);
1506}
1507
1508/**
1509 * spider_net_enable_card - enables the card by setting all kinds of regs
1510 * @card: card structure
1511 *
1512 * spider_net_enable_card sets a lot of SMMIO registers to enable the device
1513 */
1514static void
1515spider_net_enable_card(struct spider_net_card *card)
1516{
1517 int i;
1518 /* the following array consists of (register),(value) pairs
1519 * that are set in this function. A register of 0 ends the list */
1520 u32 regs[][2] = {
1521 { SPIDER_NET_GRESUMINTNUM, 0 },
1522 { SPIDER_NET_GREINTNUM, 0 },
1523
1524 /* set interrupt frame number registers */
1525 /* clear the single DMA engine registers first */
1526 { SPIDER_NET_GFAFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1527 { SPIDER_NET_GFBFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1528 { SPIDER_NET_GFCFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1529 { SPIDER_NET_GFDFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1530 /* then set, what we really need */
1531 { SPIDER_NET_GFFRMNUM, SPIDER_NET_FRAMENUM_VALUE },
1532
1533 /* timer counter registers and stuff */
1534 { SPIDER_NET_GFREECNNUM, 0 },
1535 { SPIDER_NET_GONETIMENUM, 0 },
1536 { SPIDER_NET_GTOUTFRMNUM, 0 },
1537
1538 /* RX mode setting */
1539 { SPIDER_NET_GRXMDSET, SPIDER_NET_RXMODE_VALUE },
1540 /* TX mode setting */
1541 { SPIDER_NET_GTXMDSET, SPIDER_NET_TXMODE_VALUE },
1542 /* IPSEC mode setting */
1543 { SPIDER_NET_GIPSECINIT, SPIDER_NET_IPSECINIT_VALUE },
1544
1545 { SPIDER_NET_GFTRESTRT, SPIDER_NET_RESTART_VALUE },
1546
1547 { SPIDER_NET_GMRWOLCTRL, 0 },
Jens Osterkampb636d172006-05-04 05:59:56 -04001548 { SPIDER_NET_GTESTMD, 0x10000000 },
1549 { SPIDER_NET_GTTQMSK, 0x00400040 },
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001550
1551 { SPIDER_NET_GMACINTEN, 0 },
1552
1553 /* flow control stuff */
1554 { SPIDER_NET_GMACAPAUSE, SPIDER_NET_MACAPAUSE_VALUE },
1555 { SPIDER_NET_GMACTXPAUSE, SPIDER_NET_TXPAUSE_VALUE },
1556
1557 { SPIDER_NET_GMACBSTLMT, SPIDER_NET_BURSTLMT_VALUE },
1558 { 0, 0}
1559 };
1560
1561 i = 0;
1562 while (regs[i][0]) {
1563 spider_net_write_reg(card, regs[i][0], regs[i][1]);
1564 i++;
1565 }
1566
1567 /* clear unicast filter table entries 1 to 14 */
1568 for (i = 1; i <= 14; i++) {
1569 spider_net_write_reg(card,
1570 SPIDER_NET_GMRUAFILnR + i * 8,
1571 0x00080000);
1572 spider_net_write_reg(card,
1573 SPIDER_NET_GMRUAFILnR + i * 8 + 4,
1574 0x00000000);
1575 }
1576
1577 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R, 0x08080000);
1578
1579 spider_net_write_reg(card, SPIDER_NET_ECMODE, SPIDER_NET_ECMODE_VALUE);
1580
1581 /* set chain tail adress for RX chains and
1582 * enable DMA */
1583 spider_net_enable_rxchtails(card);
1584 spider_net_enable_rxdmac(card);
1585
1586 spider_net_write_reg(card, SPIDER_NET_GRXDMAEN, SPIDER_NET_WOL_VALUE);
1587
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001588 spider_net_write_reg(card, SPIDER_NET_GMACLENLMT,
1589 SPIDER_NET_LENLMT_VALUE);
1590 spider_net_write_reg(card, SPIDER_NET_GMACMODE,
1591 SPIDER_NET_MACMODE_VALUE);
1592 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD,
1593 SPIDER_NET_OPMODE_VALUE);
1594
1595 /* set interrupt mask registers */
1596 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK,
1597 SPIDER_NET_INT0_MASK_VALUE);
1598 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK,
1599 SPIDER_NET_INT1_MASK_VALUE);
1600 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK,
1601 SPIDER_NET_INT2_MASK_VALUE);
Jens Osterkampbdd01502006-07-13 11:54:08 +02001602
1603 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
Linas Vepstasded80282006-10-10 16:02:54 -05001604 SPIDER_NET_GDTBSTA | SPIDER_NET_GDTDCEIDIS);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001605}
1606
1607/**
1608 * spider_net_open - called upon ifonfig up
1609 * @netdev: interface device structure
1610 *
1611 * returns 0 on success, <0 on failure
1612 *
1613 * spider_net_open allocates all the descriptors and memory needed for
1614 * operation, sets up multicast list and enables interrupts
1615 */
1616int
1617spider_net_open(struct net_device *netdev)
1618{
1619 struct spider_net_card *card = netdev_priv(netdev);
1620 int result;
1621
1622 result = -ENOMEM;
Jim Lewisb68a60e2006-08-14 23:00:23 -07001623 if (spider_net_init_chain(card, &card->tx_chain, card->descr,
1624 PCI_DMA_TODEVICE, card->tx_desc))
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001625 goto alloc_tx_failed;
1626 if (spider_net_init_chain(card, &card->rx_chain,
Jim Lewisb68a60e2006-08-14 23:00:23 -07001627 card->descr + card->rx_desc,
1628 PCI_DMA_FROMDEVICE, card->rx_desc))
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001629 goto alloc_rx_failed;
1630
1631 /* allocate rx skbs */
1632 if (spider_net_alloc_rx_skbs(card))
1633 goto alloc_skbs_failed;
1634
1635 spider_net_set_multi(netdev);
1636
1637 /* further enhancement: setup hw vlan, if needed */
1638
1639 result = -EBUSY;
1640 if (request_irq(netdev->irq, spider_net_interrupt,
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07001641 IRQF_SHARED, netdev->name, netdev))
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001642 goto register_int_failed;
1643
1644 spider_net_enable_card(card);
1645
Jens Osterkamp543cec52005-09-06 19:30:54 -07001646 netif_start_queue(netdev);
1647 netif_carrier_on(netdev);
1648 netif_poll_enable(netdev);
1649
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001650 return 0;
1651
1652register_int_failed:
1653 spider_net_free_rx_chain_contents(card);
1654alloc_skbs_failed:
1655 spider_net_free_chain(card, &card->rx_chain);
1656alloc_rx_failed:
1657 spider_net_free_chain(card, &card->tx_chain);
1658alloc_tx_failed:
1659 return result;
1660}
1661
1662/**
1663 * spider_net_setup_phy - setup PHY
1664 * @card: card structure
1665 *
1666 * returns 0 on success, <0 on failure
1667 *
1668 * spider_net_setup_phy is used as part of spider_net_probe. Sets
1669 * the PHY to 1000 Mbps
1670 **/
1671static int
1672spider_net_setup_phy(struct spider_net_card *card)
1673{
1674 struct mii_phy *phy = &card->phy;
1675
1676 spider_net_write_reg(card, SPIDER_NET_GDTDMASEL,
1677 SPIDER_NET_DMASEL_VALUE);
1678 spider_net_write_reg(card, SPIDER_NET_GPCCTRL,
1679 SPIDER_NET_PHY_CTRL_VALUE);
1680 phy->mii_id = 1;
1681 phy->dev = card->netdev;
1682 phy->mdio_read = spider_net_read_phy;
1683 phy->mdio_write = spider_net_write_phy;
1684
1685 mii_phy_probe(phy, phy->mii_id);
1686
1687 if (phy->def->ops->setup_forced)
1688 phy->def->ops->setup_forced(phy, SPEED_1000, DUPLEX_FULL);
1689
Jens Osterkamp8ec93452006-05-04 05:59:41 -04001690 phy->def->ops->enable_fiber(phy);
Jens Osterkamp53abbf72005-09-16 08:55:33 +02001691
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001692 phy->def->ops->read_link(phy);
1693 pr_info("Found %s with %i Mbps, %s-duplex.\n", phy->def->name,
1694 phy->speed, phy->duplex==1 ? "Full" : "Half");
1695
1696 return 0;
1697}
1698
1699/**
1700 * spider_net_download_firmware - loads firmware into the adapter
1701 * @card: card structure
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001702 * @firmware_ptr: pointer to firmware data
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001703 *
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001704 * spider_net_download_firmware loads the firmware data into the
1705 * adapter. It assumes the length etc. to be allright.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001706 */
Arnd Bergmann0d3ea162006-01-12 17:16:41 -05001707static int
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001708spider_net_download_firmware(struct spider_net_card *card,
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001709 const void *firmware_ptr)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001710{
1711 int sequencer, i;
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001712 const u32 *fw_ptr = firmware_ptr;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001713
1714 /* stop sequencers */
1715 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1716 SPIDER_NET_STOP_SEQ_VALUE);
1717
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001718 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1719 sequencer++) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001720 spider_net_write_reg(card,
1721 SPIDER_NET_GSnPRGADR + sequencer * 8, 0);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001722 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001723 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1724 sequencer * 8, *fw_ptr);
1725 fw_ptr++;
1726 }
1727 }
1728
Arnd Bergmann0d3ea162006-01-12 17:16:41 -05001729 if (spider_net_read_reg(card, SPIDER_NET_GSINIT))
1730 return -EIO;
1731
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001732 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1733 SPIDER_NET_RUN_SEQ_VALUE);
Arnd Bergmann0d3ea162006-01-12 17:16:41 -05001734
1735 return 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001736}
1737
1738/**
1739 * spider_net_init_firmware - reads in firmware parts
1740 * @card: card structure
1741 *
1742 * Returns 0 on success, <0 on failure
1743 *
1744 * spider_net_init_firmware opens the sequencer firmware and does some basic
1745 * checks. This function opens and releases the firmware structure. A call
1746 * to download the firmware is performed before the release.
1747 *
1748 * Firmware format
1749 * ===============
1750 * spider_fw.bin is expected to be a file containing 6*1024*4 bytes, 4k being
1751 * the program for each sequencer. Use the command
1752 * tail -q -n +2 Seq_code1_0x088.txt Seq_code2_0x090.txt \
1753 * Seq_code3_0x098.txt Seq_code4_0x0A0.txt Seq_code5_0x0A8.txt \
1754 * Seq_code6_0x0B0.txt | xxd -r -p -c4 > spider_fw.bin
1755 *
1756 * to generate spider_fw.bin, if you have sequencer programs with something
1757 * like the following contents for each sequencer:
1758 * <ONE LINE COMMENT>
1759 * <FIRST 4-BYTES-WORD FOR SEQUENCER>
1760 * <SECOND 4-BYTES-WORD FOR SEQUENCER>
1761 * ...
1762 * <1024th 4-BYTES-WORD FOR SEQUENCER>
1763 */
1764static int
1765spider_net_init_firmware(struct spider_net_card *card)
1766{
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001767 struct firmware *firmware = NULL;
Arnd Bergmann030d6752006-01-12 17:16:42 -05001768 struct device_node *dn;
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001769 const u8 *fw_prop = NULL;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001770 int err = -ENOENT;
1771 int fw_size;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001772
Arnd Bergmann030d6752006-01-12 17:16:42 -05001773 if (request_firmware((const struct firmware **)&firmware,
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001774 SPIDER_NET_FIRMWARE_NAME, &card->pdev->dev) == 0) {
1775 if ( (firmware->size != SPIDER_NET_FIRMWARE_LEN) &&
1776 netif_msg_probe(card) ) {
1777 pr_err("Incorrect size of spidernet firmware in " \
1778 "filesystem. Looking in host firmware...\n");
1779 goto try_host_fw;
1780 }
1781 err = spider_net_download_firmware(card, firmware->data);
Arnd Bergmann030d6752006-01-12 17:16:42 -05001782
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001783 release_firmware(firmware);
1784 if (err)
1785 goto try_host_fw;
Arnd Bergmann030d6752006-01-12 17:16:42 -05001786
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001787 goto done;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001788 }
1789
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001790try_host_fw:
1791 dn = pci_device_to_OF_node(card->pdev);
1792 if (!dn)
1793 goto out_err;
1794
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001795 fw_prop = get_property(dn, "firmware", &fw_size);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001796 if (!fw_prop)
1797 goto out_err;
1798
1799 if ( (fw_size != SPIDER_NET_FIRMWARE_LEN) &&
1800 netif_msg_probe(card) ) {
1801 pr_err("Incorrect size of spidernet firmware in " \
1802 "host firmware\n");
1803 goto done;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001804 }
1805
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001806 err = spider_net_download_firmware(card, fw_prop);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001807
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001808done:
1809 return err;
1810out_err:
1811 if (netif_msg_probe(card))
1812 pr_err("Couldn't find spidernet firmware in filesystem " \
1813 "or host firmware\n");
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001814 return err;
1815}
1816
1817/**
1818 * spider_net_workaround_rxramfull - work around firmware bug
1819 * @card: card structure
1820 *
1821 * no return value
1822 **/
1823static void
1824spider_net_workaround_rxramfull(struct spider_net_card *card)
1825{
1826 int i, sequencer = 0;
1827
1828 /* cancel reset */
1829 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1830 SPIDER_NET_CKRCTRL_RUN_VALUE);
1831
1832 /* empty sequencer data */
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001833 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1834 sequencer++) {
Jens Osterkampee962a52006-07-13 11:54:23 +02001835 spider_net_write_reg(card, SPIDER_NET_GSnPRGADR +
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001836 sequencer * 8, 0x0);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001837 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001838 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1839 sequencer * 8, 0x0);
1840 }
1841 }
1842
1843 /* set sequencer operation */
1844 spider_net_write_reg(card, SPIDER_NET_GSINIT, 0x000000fe);
1845
1846 /* reset */
1847 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1848 SPIDER_NET_CKRCTRL_STOP_VALUE);
1849}
1850
1851/**
Jens Osterkampbdd01502006-07-13 11:54:08 +02001852 * spider_net_stop - called upon ifconfig down
1853 * @netdev: interface device structure
1854 *
1855 * always returns 0
1856 */
1857int
1858spider_net_stop(struct net_device *netdev)
1859{
1860 struct spider_net_card *card = netdev_priv(netdev);
1861
1862 tasklet_kill(&card->rxram_full_tl);
1863 netif_poll_disable(netdev);
1864 netif_carrier_off(netdev);
1865 netif_stop_queue(netdev);
1866 del_timer_sync(&card->tx_timer);
1867
1868 /* disable/mask all interrupts */
1869 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, 0);
1870 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK, 0);
1871 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK, 0);
1872
1873 /* free_irq(netdev->irq, netdev);*/
1874 free_irq(to_pci_dev(netdev->class_dev.dev)->irq, netdev);
1875
1876 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
1877 SPIDER_NET_DMA_TX_FEND_VALUE);
1878
1879 /* turn off DMA, force end */
1880 spider_net_disable_rxdmac(card);
1881
1882 /* release chains */
1883 if (spin_trylock(&card->tx_chain.lock)) {
1884 spider_net_release_tx_chain(card, 1);
1885 spin_unlock(&card->tx_chain.lock);
1886 }
1887
1888 spider_net_free_chain(card, &card->tx_chain);
1889 spider_net_free_chain(card, &card->rx_chain);
1890
1891 return 0;
1892}
1893
1894/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001895 * spider_net_tx_timeout_task - task scheduled by the watchdog timeout
1896 * function (to be called not under interrupt status)
1897 * @data: data, is interface device structure
1898 *
1899 * called as task when tx hangs, resets interface (if interface is up)
1900 */
1901static void
1902spider_net_tx_timeout_task(void *data)
1903{
1904 struct net_device *netdev = data;
1905 struct spider_net_card *card = netdev_priv(netdev);
1906
1907 if (!(netdev->flags & IFF_UP))
1908 goto out;
1909
1910 netif_device_detach(netdev);
1911 spider_net_stop(netdev);
1912
1913 spider_net_workaround_rxramfull(card);
1914 spider_net_init_card(card);
1915
1916 if (spider_net_setup_phy(card))
1917 goto out;
1918 if (spider_net_init_firmware(card))
1919 goto out;
1920
1921 spider_net_open(netdev);
Jens Osterkampbdd01502006-07-13 11:54:08 +02001922 spider_net_kick_tx_dma(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001923 netif_device_attach(netdev);
1924
1925out:
1926 atomic_dec(&card->tx_timeout_task_counter);
1927}
1928
1929/**
1930 * spider_net_tx_timeout - called when the tx timeout watchdog kicks in.
1931 * @netdev: interface device structure
1932 *
1933 * called, if tx hangs. Schedules a task that resets the interface
1934 */
1935static void
1936spider_net_tx_timeout(struct net_device *netdev)
1937{
1938 struct spider_net_card *card;
1939
1940 card = netdev_priv(netdev);
1941 atomic_inc(&card->tx_timeout_task_counter);
1942 if (netdev->flags & IFF_UP)
1943 schedule_work(&card->tx_timeout_task);
1944 else
1945 atomic_dec(&card->tx_timeout_task_counter);
Jim Lewis9b6b0b82006-09-22 17:22:53 -07001946 card->spider_stats.tx_timeouts++;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001947}
1948
1949/**
1950 * spider_net_setup_netdev_ops - initialization of net_device operations
1951 * @netdev: net_device structure
1952 *
1953 * fills out function pointers in the net_device structure
1954 */
1955static void
1956spider_net_setup_netdev_ops(struct net_device *netdev)
1957{
1958 netdev->open = &spider_net_open;
1959 netdev->stop = &spider_net_stop;
1960 netdev->hard_start_xmit = &spider_net_xmit;
1961 netdev->get_stats = &spider_net_get_stats;
1962 netdev->set_multicast_list = &spider_net_set_multi;
1963 netdev->set_mac_address = &spider_net_set_mac;
1964 netdev->change_mtu = &spider_net_change_mtu;
1965 netdev->do_ioctl = &spider_net_do_ioctl;
1966 /* tx watchdog */
1967 netdev->tx_timeout = &spider_net_tx_timeout;
1968 netdev->watchdog_timeo = SPIDER_NET_WATCHDOG_TIMEOUT;
1969 /* NAPI */
1970 netdev->poll = &spider_net_poll;
1971 netdev->weight = SPIDER_NET_NAPI_WEIGHT;
1972 /* HW VLAN */
1973 netdev->vlan_rx_register = &spider_net_vlan_rx_reg;
1974 netdev->vlan_rx_add_vid = &spider_net_vlan_rx_add;
1975 netdev->vlan_rx_kill_vid = &spider_net_vlan_rx_kill;
1976#ifdef CONFIG_NET_POLL_CONTROLLER
1977 /* poll controller */
1978 netdev->poll_controller = &spider_net_poll_controller;
1979#endif /* CONFIG_NET_POLL_CONTROLLER */
1980 /* ethtool ops */
1981 netdev->ethtool_ops = &spider_net_ethtool_ops;
1982}
1983
1984/**
1985 * spider_net_setup_netdev - initialization of net_device
1986 * @card: card structure
1987 *
1988 * Returns 0 on success or <0 on failure
1989 *
1990 * spider_net_setup_netdev initializes the net_device structure
1991 **/
1992static int
1993spider_net_setup_netdev(struct spider_net_card *card)
1994{
1995 int result;
1996 struct net_device *netdev = card->netdev;
1997 struct device_node *dn;
1998 struct sockaddr addr;
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001999 const u8 *mac;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002000
2001 SET_MODULE_OWNER(netdev);
2002 SET_NETDEV_DEV(netdev, &card->pdev->dev);
2003
2004 pci_set_drvdata(card->pdev, netdev);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05002005
Arnd Bergmann11f1a522006-01-12 17:16:44 -05002006 card->rxram_full_tl.data = (unsigned long) card;
2007 card->rxram_full_tl.func =
2008 (void (*)(unsigned long)) spider_net_handle_rxram_full;
2009 init_timer(&card->tx_timer);
2010 card->tx_timer.function =
2011 (void (*)(unsigned long)) spider_net_cleanup_tx_ring;
2012 card->tx_timer.data = (unsigned long) card;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002013 netdev->irq = card->pdev->irq;
2014
2015 card->options.rx_csum = SPIDER_NET_RX_CSUM_DEFAULT;
2016
Jim Lewisb68a60e2006-08-14 23:00:23 -07002017 card->tx_desc = tx_descriptors;
2018 card->rx_desc = rx_descriptors;
2019
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002020 spider_net_setup_netdev_ops(netdev);
2021
Jens Osterkampbdd01502006-07-13 11:54:08 +02002022 netdev->features = NETIF_F_HW_CSUM | NETIF_F_LLTX;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002023 /* some time: NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
2024 * NETIF_F_HW_VLAN_FILTER */
2025
2026 netdev->irq = card->pdev->irq;
2027
2028 dn = pci_device_to_OF_node(card->pdev);
Jens Osterkamp543cec52005-09-06 19:30:54 -07002029 if (!dn)
2030 return -EIO;
2031
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10002032 mac = get_property(dn, "local-mac-address", NULL);
Jens Osterkamp543cec52005-09-06 19:30:54 -07002033 if (!mac)
2034 return -EIO;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002035 memcpy(addr.sa_data, mac, ETH_ALEN);
2036
2037 result = spider_net_set_mac(netdev, &addr);
2038 if ((result) && (netif_msg_probe(card)))
2039 pr_err("Failed to set MAC address: %i\n", result);
2040
2041 result = register_netdev(netdev);
2042 if (result) {
2043 if (netif_msg_probe(card))
2044 pr_err("Couldn't register net_device: %i\n",
2045 result);
2046 return result;
2047 }
2048
2049 if (netif_msg_probe(card))
2050 pr_info("Initialized device %s.\n", netdev->name);
2051
2052 return 0;
2053}
2054
2055/**
2056 * spider_net_alloc_card - allocates net_device and card structure
2057 *
2058 * returns the card structure or NULL in case of errors
2059 *
2060 * the card and net_device structures are linked to each other
2061 */
2062static struct spider_net_card *
2063spider_net_alloc_card(void)
2064{
2065 struct net_device *netdev;
2066 struct spider_net_card *card;
2067 size_t alloc_size;
2068
2069 alloc_size = sizeof (*card) +
2070 sizeof (struct spider_net_descr) * rx_descriptors +
2071 sizeof (struct spider_net_descr) * tx_descriptors;
2072 netdev = alloc_etherdev(alloc_size);
2073 if (!netdev)
2074 return NULL;
2075
2076 card = netdev_priv(netdev);
2077 card->netdev = netdev;
2078 card->msg_enable = SPIDER_NET_DEFAULT_MSG;
2079 INIT_WORK(&card->tx_timeout_task, spider_net_tx_timeout_task, netdev);
2080 init_waitqueue_head(&card->waitq);
2081 atomic_set(&card->tx_timeout_task_counter, 0);
2082
2083 return card;
2084}
2085
2086/**
2087 * spider_net_undo_pci_setup - releases PCI ressources
2088 * @card: card structure
2089 *
2090 * spider_net_undo_pci_setup releases the mapped regions
2091 */
2092static void
2093spider_net_undo_pci_setup(struct spider_net_card *card)
2094{
2095 iounmap(card->regs);
2096 pci_release_regions(card->pdev);
2097}
2098
2099/**
2100 * spider_net_setup_pci_dev - sets up the device in terms of PCI operations
2101 * @card: card structure
2102 * @pdev: PCI device
2103 *
2104 * Returns the card structure or NULL if any errors occur
2105 *
2106 * spider_net_setup_pci_dev initializes pdev and together with the
2107 * functions called in spider_net_open configures the device so that
2108 * data can be transferred over it
2109 * The net_device structure is attached to the card structure, if the
2110 * function returns without error.
2111 **/
2112static struct spider_net_card *
2113spider_net_setup_pci_dev(struct pci_dev *pdev)
2114{
2115 struct spider_net_card *card;
2116 unsigned long mmio_start, mmio_len;
2117
2118 if (pci_enable_device(pdev)) {
2119 pr_err("Couldn't enable PCI device\n");
2120 return NULL;
2121 }
2122
2123 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
2124 pr_err("Couldn't find proper PCI device base address.\n");
2125 goto out_disable_dev;
2126 }
2127
2128 if (pci_request_regions(pdev, spider_net_driver_name)) {
2129 pr_err("Couldn't obtain PCI resources, aborting.\n");
2130 goto out_disable_dev;
2131 }
2132
2133 pci_set_master(pdev);
2134
2135 card = spider_net_alloc_card();
2136 if (!card) {
2137 pr_err("Couldn't allocate net_device structure, "
2138 "aborting.\n");
2139 goto out_release_regions;
2140 }
2141 card->pdev = pdev;
2142
2143 /* fetch base address and length of first resource */
2144 mmio_start = pci_resource_start(pdev, 0);
2145 mmio_len = pci_resource_len(pdev, 0);
2146
2147 card->netdev->mem_start = mmio_start;
2148 card->netdev->mem_end = mmio_start + mmio_len;
2149 card->regs = ioremap(mmio_start, mmio_len);
2150
2151 if (!card->regs) {
2152 pr_err("Couldn't obtain PCI resources, aborting.\n");
2153 goto out_release_regions;
2154 }
2155
2156 return card;
2157
2158out_release_regions:
2159 pci_release_regions(pdev);
2160out_disable_dev:
2161 pci_disable_device(pdev);
2162 pci_set_drvdata(pdev, NULL);
2163 return NULL;
2164}
2165
2166/**
2167 * spider_net_probe - initialization of a device
2168 * @pdev: PCI device
2169 * @ent: entry in the device id list
2170 *
2171 * Returns 0 on success, <0 on failure
2172 *
2173 * spider_net_probe initializes pdev and registers a net_device
2174 * structure for it. After that, the device can be ifconfig'ed up
2175 **/
2176static int __devinit
2177spider_net_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2178{
2179 int err = -EIO;
2180 struct spider_net_card *card;
2181
2182 card = spider_net_setup_pci_dev(pdev);
2183 if (!card)
2184 goto out;
2185
2186 spider_net_workaround_rxramfull(card);
2187 spider_net_init_card(card);
2188
2189 err = spider_net_setup_phy(card);
2190 if (err)
2191 goto out_undo_pci;
2192
2193 err = spider_net_init_firmware(card);
2194 if (err)
2195 goto out_undo_pci;
2196
2197 err = spider_net_setup_netdev(card);
2198 if (err)
2199 goto out_undo_pci;
2200
2201 return 0;
2202
2203out_undo_pci:
2204 spider_net_undo_pci_setup(card);
2205 free_netdev(card->netdev);
2206out:
2207 return err;
2208}
2209
2210/**
2211 * spider_net_remove - removal of a device
2212 * @pdev: PCI device
2213 *
2214 * Returns 0 on success, <0 on failure
2215 *
2216 * spider_net_remove is called to remove the device and unregisters the
2217 * net_device
2218 **/
2219static void __devexit
2220spider_net_remove(struct pci_dev *pdev)
2221{
2222 struct net_device *netdev;
2223 struct spider_net_card *card;
2224
2225 netdev = pci_get_drvdata(pdev);
2226 card = netdev_priv(netdev);
2227
2228 wait_event(card->waitq,
2229 atomic_read(&card->tx_timeout_task_counter) == 0);
2230
2231 unregister_netdev(netdev);
Jens Osterkamp543cec52005-09-06 19:30:54 -07002232
2233 /* switch off card */
2234 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2235 SPIDER_NET_CKRCTRL_STOP_VALUE);
2236 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2237 SPIDER_NET_CKRCTRL_RUN_VALUE);
2238
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002239 spider_net_undo_pci_setup(card);
2240 free_netdev(netdev);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002241}
2242
2243static struct pci_driver spider_net_driver = {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002244 .name = spider_net_driver_name,
2245 .id_table = spider_net_pci_tbl,
2246 .probe = spider_net_probe,
2247 .remove = __devexit_p(spider_net_remove)
2248};
2249
2250/**
2251 * spider_net_init - init function when the driver is loaded
2252 *
2253 * spider_net_init registers the device driver
2254 */
2255static int __init spider_net_init(void)
2256{
Linas Vepstas90f10842006-10-10 15:56:04 -05002257 printk(KERN_INFO "Spidernet version %s.\n", VERSION);
2258
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002259 if (rx_descriptors < SPIDER_NET_RX_DESCRIPTORS_MIN) {
2260 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MIN;
2261 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2262 }
2263 if (rx_descriptors > SPIDER_NET_RX_DESCRIPTORS_MAX) {
2264 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MAX;
2265 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2266 }
2267 if (tx_descriptors < SPIDER_NET_TX_DESCRIPTORS_MIN) {
2268 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MIN;
2269 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2270 }
2271 if (tx_descriptors > SPIDER_NET_TX_DESCRIPTORS_MAX) {
2272 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MAX;
2273 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2274 }
2275
2276 return pci_register_driver(&spider_net_driver);
2277}
2278
2279/**
2280 * spider_net_cleanup - exit function when driver is unloaded
2281 *
2282 * spider_net_cleanup unregisters the device driver
2283 */
2284static void __exit spider_net_cleanup(void)
2285{
2286 pci_unregister_driver(&spider_net_driver);
2287}
2288
2289module_init(spider_net_init);
2290module_exit(spider_net_cleanup);