blob: 0aed69382b28772c5f2b838cb17e62adcfe8d910 [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 int result;
827
Jens Osterkampbdd01502006-07-13 11:54:08 +0200828 spin_lock_irqsave(&chain->lock, flags);
829
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500830 spider_net_release_tx_chain(card, 0);
831
Jens Osterkampbdd01502006-07-13 11:54:08 +0200832 if (chain->head->next == chain->tail->prev) {
833 card->netdev_stats.tx_dropped++;
834 result = NETDEV_TX_LOCKED;
835 goto out;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500836 }
837
Jens Osterkampbdd01502006-07-13 11:54:08 +0200838 if (spider_net_get_descr_status(descr) != SPIDER_NET_DESCR_NOT_IN_USE) {
Jim Lewis9b6b0b82006-09-22 17:22:53 -0700839 card->netdev_stats.tx_dropped++;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200840 result = NETDEV_TX_LOCKED;
841 goto out;
842 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700843
Jens Osterkampbdd01502006-07-13 11:54:08 +0200844 if (spider_net_prepare_tx_descr(card, skb) != 0) {
845 card->netdev_stats.tx_dropped++;
846 result = NETDEV_TX_BUSY;
847 goto out;
848 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700849
Jens Osterkampbdd01502006-07-13 11:54:08 +0200850 result = NETDEV_TX_OK;
851
852 spider_net_kick_tx_dma(card);
853 card->tx_chain.head = card->tx_chain.head->next;
854
855out:
856 spin_unlock_irqrestore(&chain->lock, flags);
857 netif_wake_queue(netdev);
858 return result;
859}
860
861/**
862 * spider_net_cleanup_tx_ring - cleans up the TX ring
863 * @card: card structure
864 *
865 * spider_net_cleanup_tx_ring is called by the tx_timer (as we don't use
866 * interrupts to cleanup our TX ring) and returns sent packets to the stack
867 * by freeing them
868 */
869static void
870spider_net_cleanup_tx_ring(struct spider_net_card *card)
871{
872 unsigned long flags;
873
874 spin_lock_irqsave(&card->tx_chain.lock, flags);
875
876 if ((spider_net_release_tx_chain(card, 0) != 0) &&
877 (card->netdev->flags & IFF_UP))
878 spider_net_kick_tx_dma(card);
879
880 spin_unlock_irqrestore(&card->tx_chain.lock, flags);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700881}
882
883/**
884 * spider_net_do_ioctl - called for device ioctls
885 * @netdev: interface device structure
886 * @ifr: request parameter structure for ioctl
887 * @cmd: command code for ioctl
888 *
889 * returns 0 on success, <0 on failure. Currently, we have no special ioctls.
890 * -EOPNOTSUPP is returned, if an unknown ioctl was requested
891 */
892static int
893spider_net_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
894{
895 switch (cmd) {
896 default:
897 return -EOPNOTSUPP;
898 }
899}
900
901/**
902 * spider_net_pass_skb_up - takes an skb from a descriptor and passes it on
903 * @descr: descriptor to process
904 * @card: card structure
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500905 * @napi: whether caller is in NAPI context
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700906 *
907 * returns 1 on success, 0 if no packet was passed to the stack
908 *
909 * iommu-unmaps the skb, fills out skb structure and passes the data to the
910 * stack. The descriptor state is not changed.
911 */
912static int
913spider_net_pass_skb_up(struct spider_net_descr *descr,
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500914 struct spider_net_card *card, int napi)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700915{
916 struct sk_buff *skb;
917 struct net_device *netdev;
918 u32 data_status, data_error;
919
920 data_status = descr->data_status;
921 data_error = descr->data_error;
922
923 netdev = card->netdev;
924
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500925 /* unmap descriptor */
926 pci_unmap_single(card->pdev, descr->buf_addr, SPIDER_NET_MAX_FRAME,
Jens Osterkampbdd01502006-07-13 11:54:08 +0200927 PCI_DMA_FROMDEVICE);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700928
929 /* the cases we'll throw away the packet immediately */
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500930 if (data_error & SPIDER_NET_DESTROY_RX_FLAGS) {
931 if (netif_msg_rx_err(card))
932 pr_err("error in received descriptor found, "
933 "data_status=x%08x, data_error=x%08x\n",
934 data_status, data_error);
Jim Lewis9b6b0b82006-09-22 17:22:53 -0700935 card->spider_stats.rx_desc_error++;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700936 return 0;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500937 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700938
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500939 skb = descr->skb;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700940 skb->dev = netdev;
941 skb_put(skb, descr->valid_size);
942
943 /* the card seems to add 2 bytes of junk in front
944 * of the ethernet frame */
945#define SPIDER_MISALIGN 2
946 skb_pull(skb, SPIDER_MISALIGN);
947 skb->protocol = eth_type_trans(skb, netdev);
948
949 /* checksum offload */
950 if (card->options.rx_csum) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500951 if ( ( (data_status & SPIDER_NET_DATA_STATUS_CKSUM_MASK) ==
952 SPIDER_NET_DATA_STATUS_CKSUM_MASK) &&
953 !(data_error & SPIDER_NET_DATA_ERR_CKSUM_MASK))
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700954 skb->ip_summed = CHECKSUM_UNNECESSARY;
955 else
956 skb->ip_summed = CHECKSUM_NONE;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500957 } else
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700958 skb->ip_summed = CHECKSUM_NONE;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700959
960 if (data_status & SPIDER_NET_VLAN_PACKET) {
961 /* further enhancements: HW-accel VLAN
962 * vlan_hwaccel_receive_skb
963 */
964 }
965
966 /* pass skb up to stack */
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500967 if (napi)
968 netif_receive_skb(skb);
969 else
970 netif_rx_ni(skb);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700971
972 /* update netdevice statistics */
973 card->netdev_stats.rx_packets++;
974 card->netdev_stats.rx_bytes += skb->len;
975
976 return 1;
977}
978
979/**
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500980 * spider_net_decode_one_descr - processes an rx descriptor
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700981 * @card: card structure
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500982 * @napi: whether caller is in NAPI context
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700983 *
984 * returns 1 if a packet has been sent to the stack, otherwise 0
985 *
986 * processes an rx descriptor by iommu-unmapping the data buffer and passing
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500987 * the packet up to the stack. This function is called in softirq
988 * context, e.g. either bottom half from interrupt or NAPI polling context
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700989 */
990static int
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500991spider_net_decode_one_descr(struct spider_net_card *card, int napi)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700992{
Jens Osterkampbdd01502006-07-13 11:54:08 +0200993 struct spider_net_descr_chain *chain = &card->rx_chain;
994 struct spider_net_descr *descr = chain->tail;
995 int status;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700996 int result;
997
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700998 status = spider_net_get_descr_status(descr);
999
1000 if (status == SPIDER_NET_DESCR_CARDOWNED) {
1001 /* nothing in the descriptor yet */
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001002 result=0;
1003 goto out;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001004 }
1005
1006 if (status == SPIDER_NET_DESCR_NOT_IN_USE) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001007 /* not initialized yet, the ring must be empty */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001008 spider_net_refill_rx_chain(card);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001009 spider_net_enable_rxdmac(card);
1010 result=0;
1011 goto out;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001012 }
1013
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001014 /* descriptor definitively used -- move on tail */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001015 chain->tail = descr->next;
1016
1017 result = 0;
1018 if ( (status == SPIDER_NET_DESCR_RESPONSE_ERROR) ||
1019 (status == SPIDER_NET_DESCR_PROTECTION_ERROR) ||
1020 (status == SPIDER_NET_DESCR_FORCE_END) ) {
1021 if (netif_msg_rx_err(card))
1022 pr_err("%s: dropping RX descriptor with state %d\n",
1023 card->netdev->name, status);
1024 card->netdev_stats.rx_dropped++;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001025 pci_unmap_single(card->pdev, descr->buf_addr,
Jens Osterkampbdd01502006-07-13 11:54:08 +02001026 SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001027 dev_kfree_skb_irq(descr->skb);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001028 goto refill;
1029 }
1030
1031 if ( (status != SPIDER_NET_DESCR_COMPLETE) &&
1032 (status != SPIDER_NET_DESCR_FRAME_END) ) {
Jim Lewis9b6b0b82006-09-22 17:22:53 -07001033 if (netif_msg_rx_err(card)) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001034 pr_err("%s: RX descriptor with state %d\n",
1035 card->netdev->name, status);
Jim Lewis9b6b0b82006-09-22 17:22:53 -07001036 card->spider_stats.rx_desc_unk_state++;
1037 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001038 goto refill;
1039 }
1040
1041 /* ok, we've got a packet in descr */
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001042 result = spider_net_pass_skb_up(descr, card, napi);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001043refill:
Jens Osterkampbdd01502006-07-13 11:54:08 +02001044 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001045 /* change the descriptor state: */
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001046 if (!napi)
1047 spider_net_refill_rx_chain(card);
1048out:
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001049 return result;
1050}
1051
1052/**
1053 * spider_net_poll - NAPI poll function called by the stack to return packets
1054 * @netdev: interface device structure
1055 * @budget: number of packets we can pass to the stack at most
1056 *
1057 * returns 0 if no more packets available to the driver/stack. Returns 1,
1058 * if the quota is exceeded, but the driver has still packets.
1059 *
1060 * spider_net_poll returns all packets from the rx descriptors to the stack
1061 * (using netif_receive_skb). If all/enough packets are up, the driver
1062 * reenables interrupts and returns 0. If not, 1 is returned.
1063 */
1064static int
1065spider_net_poll(struct net_device *netdev, int *budget)
1066{
1067 struct spider_net_card *card = netdev_priv(netdev);
1068 int packets_to_do, packets_done = 0;
1069 int no_more_packets = 0;
1070
1071 packets_to_do = min(*budget, netdev->quota);
1072
1073 while (packets_to_do) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001074 if (spider_net_decode_one_descr(card, 1)) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001075 packets_done++;
1076 packets_to_do--;
1077 } else {
1078 /* no more packets for the stack */
1079 no_more_packets = 1;
1080 break;
1081 }
1082 }
1083
1084 netdev->quota -= packets_done;
1085 *budget -= packets_done;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001086 spider_net_refill_rx_chain(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001087
1088 /* if all packets are in the stack, enable interrupts and return 0 */
1089 /* if not, return 1 */
1090 if (no_more_packets) {
1091 netif_rx_complete(netdev);
1092 spider_net_rx_irq_on(card);
1093 return 0;
1094 }
1095
1096 return 1;
1097}
1098
1099/**
1100 * spider_net_vlan_rx_reg - initializes VLAN structures in the driver and card
1101 * @netdev: interface device structure
1102 * @grp: vlan_group structure that is registered (NULL on destroying interface)
1103 */
1104static void
1105spider_net_vlan_rx_reg(struct net_device *netdev, struct vlan_group *grp)
1106{
1107 /* further enhancement... yet to do */
1108 return;
1109}
1110
1111/**
1112 * spider_net_vlan_rx_add - adds VLAN id to the card filter
1113 * @netdev: interface device structure
1114 * @vid: VLAN id to add
1115 */
1116static void
1117spider_net_vlan_rx_add(struct net_device *netdev, uint16_t vid)
1118{
1119 /* further enhancement... yet to do */
1120 /* add vid to card's VLAN filter table */
1121 return;
1122}
1123
1124/**
1125 * spider_net_vlan_rx_kill - removes VLAN id to the card filter
1126 * @netdev: interface device structure
1127 * @vid: VLAN id to remove
1128 */
1129static void
1130spider_net_vlan_rx_kill(struct net_device *netdev, uint16_t vid)
1131{
1132 /* further enhancement... yet to do */
1133 /* remove vid from card's VLAN filter table */
1134}
1135
1136/**
1137 * spider_net_get_stats - get interface statistics
1138 * @netdev: interface device structure
1139 *
1140 * returns the interface statistics residing in the spider_net_card struct
1141 */
1142static struct net_device_stats *
1143spider_net_get_stats(struct net_device *netdev)
1144{
1145 struct spider_net_card *card = netdev_priv(netdev);
1146 struct net_device_stats *stats = &card->netdev_stats;
1147 return stats;
1148}
1149
1150/**
1151 * spider_net_change_mtu - changes the MTU of an interface
1152 * @netdev: interface device structure
1153 * @new_mtu: new MTU value
1154 *
1155 * returns 0 on success, <0 on failure
1156 */
1157static int
1158spider_net_change_mtu(struct net_device *netdev, int new_mtu)
1159{
1160 /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
1161 * and mtu is outbound only anyway */
1162 if ( (new_mtu < SPIDER_NET_MIN_MTU ) ||
1163 (new_mtu > SPIDER_NET_MAX_MTU) )
1164 return -EINVAL;
1165 netdev->mtu = new_mtu;
1166 return 0;
1167}
1168
1169/**
1170 * spider_net_set_mac - sets the MAC of an interface
1171 * @netdev: interface device structure
1172 * @ptr: pointer to new MAC address
1173 *
1174 * Returns 0 on success, <0 on failure. Currently, we don't support this
1175 * and will always return EOPNOTSUPP.
1176 */
1177static int
1178spider_net_set_mac(struct net_device *netdev, void *p)
1179{
1180 struct spider_net_card *card = netdev_priv(netdev);
Jens Osterkamp054034d2005-09-06 19:30:55 -07001181 u32 macl, macu, regvalue;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001182 struct sockaddr *addr = p;
1183
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001184 if (!is_valid_ether_addr(addr->sa_data))
1185 return -EADDRNOTAVAIL;
1186
Jens Osterkamp054034d2005-09-06 19:30:55 -07001187 /* switch off GMACTPE and GMACRPE */
1188 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1189 regvalue &= ~((1 << 5) | (1 << 6));
1190 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1191
1192 /* write mac */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001193 macu = (addr->sa_data[0]<<24) + (addr->sa_data[1]<<16) +
1194 (addr->sa_data[2]<<8) + (addr->sa_data[3]);
1195 macl = (addr->sa_data[4]<<8) + (addr->sa_data[5]);
1196 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACU, macu);
1197 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACL, macl);
1198
Jens Osterkamp054034d2005-09-06 19:30:55 -07001199 /* switch GMACTPE and GMACRPE back on */
1200 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1201 regvalue |= ((1 << 5) | (1 << 6));
1202 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1203
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001204 spider_net_set_promisc(card);
1205
1206 /* look up, whether we have been successful */
1207 if (spider_net_get_mac_address(netdev))
1208 return -EADDRNOTAVAIL;
1209 if (memcmp(netdev->dev_addr,addr->sa_data,netdev->addr_len))
1210 return -EADDRNOTAVAIL;
1211
1212 return 0;
1213}
1214
1215/**
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001216 * spider_net_handle_rxram_full - cleans up RX ring upon RX RAM full interrupt
1217 * @card: card structure
1218 *
1219 * spider_net_handle_rxram_full empties the RX ring so that spider can put
1220 * more packets in it and empty its RX RAM. This is called in bottom half
1221 * context
1222 */
1223static void
1224spider_net_handle_rxram_full(struct spider_net_card *card)
1225{
1226 while (spider_net_decode_one_descr(card, 0))
1227 ;
1228 spider_net_enable_rxchtails(card);
1229 spider_net_enable_rxdmac(card);
1230 netif_rx_schedule(card->netdev);
1231}
1232
1233/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001234 * spider_net_handle_error_irq - handles errors raised by an interrupt
1235 * @card: card structure
1236 * @status_reg: interrupt status register 0 (GHIINT0STS)
1237 *
1238 * spider_net_handle_error_irq treats or ignores all error conditions
1239 * found when an interrupt is presented
1240 */
1241static void
1242spider_net_handle_error_irq(struct spider_net_card *card, u32 status_reg)
1243{
1244 u32 error_reg1, error_reg2;
1245 u32 i;
1246 int show_error = 1;
1247
1248 error_reg1 = spider_net_read_reg(card, SPIDER_NET_GHIINT1STS);
1249 error_reg2 = spider_net_read_reg(card, SPIDER_NET_GHIINT2STS);
1250
1251 /* check GHIINT0STS ************************************/
1252 if (status_reg)
1253 for (i = 0; i < 32; i++)
1254 if (status_reg & (1<<i))
1255 switch (i)
1256 {
1257 /* let error_reg1 and error_reg2 evaluation decide, what to do
1258 case SPIDER_NET_PHYINT:
1259 case SPIDER_NET_GMAC2INT:
1260 case SPIDER_NET_GMAC1INT:
1261 case SPIDER_NET_GIPSINT:
1262 case SPIDER_NET_GFIFOINT:
1263 case SPIDER_NET_DMACINT:
1264 case SPIDER_NET_GSYSINT:
1265 break; */
1266
1267 case SPIDER_NET_GPWOPCMPINT:
1268 /* PHY write operation completed */
1269 show_error = 0;
1270 break;
1271 case SPIDER_NET_GPROPCMPINT:
1272 /* PHY read operation completed */
1273 /* we don't use semaphores, as we poll for the completion
1274 * of the read operation in spider_net_read_phy. Should take
1275 * about 50 us */
1276 show_error = 0;
1277 break;
1278 case SPIDER_NET_GPWFFINT:
1279 /* PHY command queue full */
1280 if (netif_msg_intr(card))
1281 pr_err("PHY write queue full\n");
1282 show_error = 0;
1283 break;
1284
1285 /* case SPIDER_NET_GRMDADRINT: not used. print a message */
1286 /* case SPIDER_NET_GRMARPINT: not used. print a message */
1287 /* case SPIDER_NET_GRMMPINT: not used. print a message */
1288
1289 case SPIDER_NET_GDTDEN0INT:
1290 /* someone has set TX_DMA_EN to 0 */
1291 show_error = 0;
1292 break;
1293
1294 case SPIDER_NET_GDDDEN0INT: /* fallthrough */
1295 case SPIDER_NET_GDCDEN0INT: /* fallthrough */
1296 case SPIDER_NET_GDBDEN0INT: /* fallthrough */
1297 case SPIDER_NET_GDADEN0INT:
1298 /* someone has set RX_DMA_EN to 0 */
1299 show_error = 0;
1300 break;
1301
1302 /* RX interrupts */
1303 case SPIDER_NET_GDDFDCINT:
1304 case SPIDER_NET_GDCFDCINT:
1305 case SPIDER_NET_GDBFDCINT:
1306 case SPIDER_NET_GDAFDCINT:
1307 /* case SPIDER_NET_GDNMINT: not used. print a message */
1308 /* case SPIDER_NET_GCNMINT: not used. print a message */
1309 /* case SPIDER_NET_GBNMINT: not used. print a message */
1310 /* case SPIDER_NET_GANMINT: not used. print a message */
1311 /* case SPIDER_NET_GRFNMINT: not used. print a message */
1312 show_error = 0;
1313 break;
1314
1315 /* TX interrupts */
1316 case SPIDER_NET_GDTFDCINT:
1317 show_error = 0;
1318 break;
1319 case SPIDER_NET_GTTEDINT:
1320 show_error = 0;
1321 break;
1322 case SPIDER_NET_GDTDCEINT:
1323 /* chain end. If a descriptor should be sent, kick off
1324 * tx dma
1325 if (card->tx_chain.tail == card->tx_chain.head)
1326 spider_net_kick_tx_dma(card);
1327 show_error = 0; */
1328 break;
1329
1330 /* case SPIDER_NET_G1TMCNTINT: not used. print a message */
1331 /* case SPIDER_NET_GFREECNTINT: not used. print a message */
1332 }
1333
1334 /* check GHIINT1STS ************************************/
1335 if (error_reg1)
1336 for (i = 0; i < 32; i++)
1337 if (error_reg1 & (1<<i))
1338 switch (i)
1339 {
1340 case SPIDER_NET_GTMFLLINT:
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001341 if (netif_msg_intr(card) && net_ratelimit())
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001342 pr_err("Spider TX RAM full\n");
1343 show_error = 0;
1344 break;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001345 case SPIDER_NET_GRFDFLLINT: /* fallthrough */
1346 case SPIDER_NET_GRFCFLLINT: /* fallthrough */
1347 case SPIDER_NET_GRFBFLLINT: /* fallthrough */
1348 case SPIDER_NET_GRFAFLLINT: /* fallthrough */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001349 case SPIDER_NET_GRMFLLINT:
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001350 if (netif_msg_intr(card) && net_ratelimit())
Jens Osterkamp4e5077b2006-03-28 15:59:55 +02001351 pr_debug("Spider RX RAM full, incoming packets "
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001352 "might be discarded!\n");
1353 spider_net_rx_irq_off(card);
1354 tasklet_schedule(&card->rxram_full_tl);
1355 show_error = 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001356 break;
1357
1358 /* case SPIDER_NET_GTMSHTINT: problem, print a message */
1359 case SPIDER_NET_GDTINVDINT:
1360 /* allrighty. tx from previous descr ok */
1361 show_error = 0;
1362 break;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001363
1364 /* chain end */
1365 case SPIDER_NET_GDDDCEINT: /* fallthrough */
1366 case SPIDER_NET_GDCDCEINT: /* fallthrough */
1367 case SPIDER_NET_GDBDCEINT: /* fallthrough */
1368 case SPIDER_NET_GDADCEINT:
1369 if (netif_msg_intr(card))
1370 pr_err("got descriptor chain end interrupt, "
1371 "restarting DMAC %c.\n",
1372 'D'+i-SPIDER_NET_GDDDCEINT);
1373 spider_net_refill_rx_chain(card);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001374 spider_net_enable_rxdmac(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001375 show_error = 0;
1376 break;
1377
1378 /* invalid descriptor */
1379 case SPIDER_NET_GDDINVDINT: /* fallthrough */
1380 case SPIDER_NET_GDCINVDINT: /* fallthrough */
1381 case SPIDER_NET_GDBINVDINT: /* fallthrough */
1382 case SPIDER_NET_GDAINVDINT:
1383 /* could happen when rx chain is full */
1384 spider_net_refill_rx_chain(card);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001385 spider_net_enable_rxdmac(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001386 show_error = 0;
1387 break;
1388
1389 /* case SPIDER_NET_GDTRSERINT: problem, print a message */
1390 /* case SPIDER_NET_GDDRSERINT: problem, print a message */
1391 /* case SPIDER_NET_GDCRSERINT: problem, print a message */
1392 /* case SPIDER_NET_GDBRSERINT: problem, print a message */
1393 /* case SPIDER_NET_GDARSERINT: problem, print a message */
1394 /* case SPIDER_NET_GDSERINT: problem, print a message */
1395 /* case SPIDER_NET_GDTPTERINT: problem, print a message */
1396 /* case SPIDER_NET_GDDPTERINT: problem, print a message */
1397 /* case SPIDER_NET_GDCPTERINT: problem, print a message */
1398 /* case SPIDER_NET_GDBPTERINT: problem, print a message */
1399 /* case SPIDER_NET_GDAPTERINT: problem, print a message */
1400 default:
1401 show_error = 1;
1402 break;
1403 }
1404
1405 /* check GHIINT2STS ************************************/
1406 if (error_reg2)
1407 for (i = 0; i < 32; i++)
1408 if (error_reg2 & (1<<i))
1409 switch (i)
1410 {
1411 /* there is nothing we can (want to) do at this time. Log a
1412 * message, we can switch on and off the specific values later on
1413 case SPIDER_NET_GPROPERINT:
1414 case SPIDER_NET_GMCTCRSNGINT:
1415 case SPIDER_NET_GMCTLCOLINT:
1416 case SPIDER_NET_GMCTTMOTINT:
1417 case SPIDER_NET_GMCRCAERINT:
1418 case SPIDER_NET_GMCRCALERINT:
1419 case SPIDER_NET_GMCRALNERINT:
1420 case SPIDER_NET_GMCROVRINT:
1421 case SPIDER_NET_GMCRRNTINT:
1422 case SPIDER_NET_GMCRRXERINT:
1423 case SPIDER_NET_GTITCSERINT:
1424 case SPIDER_NET_GTIFMTERINT:
1425 case SPIDER_NET_GTIPKTRVKINT:
1426 case SPIDER_NET_GTISPINGINT:
1427 case SPIDER_NET_GTISADNGINT:
1428 case SPIDER_NET_GTISPDNGINT:
1429 case SPIDER_NET_GRIFMTERINT:
1430 case SPIDER_NET_GRIPKTRVKINT:
1431 case SPIDER_NET_GRISPINGINT:
1432 case SPIDER_NET_GRISADNGINT:
1433 case SPIDER_NET_GRISPDNGINT:
1434 break;
1435 */
1436 default:
1437 break;
1438 }
1439
1440 if ((show_error) && (netif_msg_intr(card)))
1441 pr_err("Got error interrupt, GHIINT0STS = 0x%08x, "
1442 "GHIINT1STS = 0x%08x, GHIINT2STS = 0x%08x\n",
1443 status_reg, error_reg1, error_reg2);
1444
1445 /* clear interrupt sources */
1446 spider_net_write_reg(card, SPIDER_NET_GHIINT1STS, error_reg1);
1447 spider_net_write_reg(card, SPIDER_NET_GHIINT2STS, error_reg2);
1448}
1449
1450/**
1451 * spider_net_interrupt - interrupt handler for spider_net
1452 * @irq: interupt number
1453 * @ptr: pointer to net_device
1454 * @regs: PU registers
1455 *
1456 * returns IRQ_HANDLED, if interrupt was for driver, or IRQ_NONE, if no
1457 * interrupt found raised by card.
1458 *
1459 * This is the interrupt handler, that turns off
1460 * interrupts for this device and makes the stack poll the driver
1461 */
1462static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001463spider_net_interrupt(int irq, void *ptr)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001464{
1465 struct net_device *netdev = ptr;
1466 struct spider_net_card *card = netdev_priv(netdev);
1467 u32 status_reg;
1468
1469 status_reg = spider_net_read_reg(card, SPIDER_NET_GHIINT0STS);
1470
1471 if (!status_reg)
1472 return IRQ_NONE;
1473
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001474 if (status_reg & SPIDER_NET_RXINT ) {
1475 spider_net_rx_irq_off(card);
1476 netif_rx_schedule(netdev);
1477 }
1478
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001479 if (status_reg & SPIDER_NET_ERRINT )
1480 spider_net_handle_error_irq(card, status_reg);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001481
1482 /* clear interrupt sources */
1483 spider_net_write_reg(card, SPIDER_NET_GHIINT0STS, status_reg);
1484
1485 return IRQ_HANDLED;
1486}
1487
1488#ifdef CONFIG_NET_POLL_CONTROLLER
1489/**
1490 * spider_net_poll_controller - artificial interrupt for netconsole etc.
1491 * @netdev: interface device structure
1492 *
1493 * see Documentation/networking/netconsole.txt
1494 */
1495static void
1496spider_net_poll_controller(struct net_device *netdev)
1497{
1498 disable_irq(netdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01001499 spider_net_interrupt(netdev->irq, netdev);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001500 enable_irq(netdev->irq);
1501}
1502#endif /* CONFIG_NET_POLL_CONTROLLER */
1503
1504/**
1505 * spider_net_init_card - initializes the card
1506 * @card: card structure
1507 *
1508 * spider_net_init_card initializes the card so that other registers can
1509 * be used
1510 */
1511static void
1512spider_net_init_card(struct spider_net_card *card)
1513{
1514 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1515 SPIDER_NET_CKRCTRL_STOP_VALUE);
1516
1517 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1518 SPIDER_NET_CKRCTRL_RUN_VALUE);
1519}
1520
1521/**
1522 * spider_net_enable_card - enables the card by setting all kinds of regs
1523 * @card: card structure
1524 *
1525 * spider_net_enable_card sets a lot of SMMIO registers to enable the device
1526 */
1527static void
1528spider_net_enable_card(struct spider_net_card *card)
1529{
1530 int i;
1531 /* the following array consists of (register),(value) pairs
1532 * that are set in this function. A register of 0 ends the list */
1533 u32 regs[][2] = {
1534 { SPIDER_NET_GRESUMINTNUM, 0 },
1535 { SPIDER_NET_GREINTNUM, 0 },
1536
1537 /* set interrupt frame number registers */
1538 /* clear the single DMA engine registers first */
1539 { SPIDER_NET_GFAFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1540 { SPIDER_NET_GFBFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1541 { SPIDER_NET_GFCFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1542 { SPIDER_NET_GFDFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1543 /* then set, what we really need */
1544 { SPIDER_NET_GFFRMNUM, SPIDER_NET_FRAMENUM_VALUE },
1545
1546 /* timer counter registers and stuff */
1547 { SPIDER_NET_GFREECNNUM, 0 },
1548 { SPIDER_NET_GONETIMENUM, 0 },
1549 { SPIDER_NET_GTOUTFRMNUM, 0 },
1550
1551 /* RX mode setting */
1552 { SPIDER_NET_GRXMDSET, SPIDER_NET_RXMODE_VALUE },
1553 /* TX mode setting */
1554 { SPIDER_NET_GTXMDSET, SPIDER_NET_TXMODE_VALUE },
1555 /* IPSEC mode setting */
1556 { SPIDER_NET_GIPSECINIT, SPIDER_NET_IPSECINIT_VALUE },
1557
1558 { SPIDER_NET_GFTRESTRT, SPIDER_NET_RESTART_VALUE },
1559
1560 { SPIDER_NET_GMRWOLCTRL, 0 },
Jens Osterkampb636d172006-05-04 05:59:56 -04001561 { SPIDER_NET_GTESTMD, 0x10000000 },
1562 { SPIDER_NET_GTTQMSK, 0x00400040 },
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001563
1564 { SPIDER_NET_GMACINTEN, 0 },
1565
1566 /* flow control stuff */
1567 { SPIDER_NET_GMACAPAUSE, SPIDER_NET_MACAPAUSE_VALUE },
1568 { SPIDER_NET_GMACTXPAUSE, SPIDER_NET_TXPAUSE_VALUE },
1569
1570 { SPIDER_NET_GMACBSTLMT, SPIDER_NET_BURSTLMT_VALUE },
1571 { 0, 0}
1572 };
1573
1574 i = 0;
1575 while (regs[i][0]) {
1576 spider_net_write_reg(card, regs[i][0], regs[i][1]);
1577 i++;
1578 }
1579
1580 /* clear unicast filter table entries 1 to 14 */
1581 for (i = 1; i <= 14; i++) {
1582 spider_net_write_reg(card,
1583 SPIDER_NET_GMRUAFILnR + i * 8,
1584 0x00080000);
1585 spider_net_write_reg(card,
1586 SPIDER_NET_GMRUAFILnR + i * 8 + 4,
1587 0x00000000);
1588 }
1589
1590 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R, 0x08080000);
1591
1592 spider_net_write_reg(card, SPIDER_NET_ECMODE, SPIDER_NET_ECMODE_VALUE);
1593
1594 /* set chain tail adress for RX chains and
1595 * enable DMA */
1596 spider_net_enable_rxchtails(card);
1597 spider_net_enable_rxdmac(card);
1598
1599 spider_net_write_reg(card, SPIDER_NET_GRXDMAEN, SPIDER_NET_WOL_VALUE);
1600
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001601 spider_net_write_reg(card, SPIDER_NET_GMACLENLMT,
1602 SPIDER_NET_LENLMT_VALUE);
1603 spider_net_write_reg(card, SPIDER_NET_GMACMODE,
1604 SPIDER_NET_MACMODE_VALUE);
1605 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD,
1606 SPIDER_NET_OPMODE_VALUE);
1607
1608 /* set interrupt mask registers */
1609 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK,
1610 SPIDER_NET_INT0_MASK_VALUE);
1611 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK,
1612 SPIDER_NET_INT1_MASK_VALUE);
1613 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK,
1614 SPIDER_NET_INT2_MASK_VALUE);
Jens Osterkampbdd01502006-07-13 11:54:08 +02001615
1616 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
1617 SPIDER_NET_GDTDCEIDIS);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001618}
1619
1620/**
1621 * spider_net_open - called upon ifonfig up
1622 * @netdev: interface device structure
1623 *
1624 * returns 0 on success, <0 on failure
1625 *
1626 * spider_net_open allocates all the descriptors and memory needed for
1627 * operation, sets up multicast list and enables interrupts
1628 */
1629int
1630spider_net_open(struct net_device *netdev)
1631{
1632 struct spider_net_card *card = netdev_priv(netdev);
1633 int result;
1634
1635 result = -ENOMEM;
Jim Lewisb68a60e2006-08-14 23:00:23 -07001636 if (spider_net_init_chain(card, &card->tx_chain, card->descr,
1637 PCI_DMA_TODEVICE, card->tx_desc))
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001638 goto alloc_tx_failed;
1639 if (spider_net_init_chain(card, &card->rx_chain,
Jim Lewisb68a60e2006-08-14 23:00:23 -07001640 card->descr + card->rx_desc,
1641 PCI_DMA_FROMDEVICE, card->rx_desc))
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001642 goto alloc_rx_failed;
1643
1644 /* allocate rx skbs */
1645 if (spider_net_alloc_rx_skbs(card))
1646 goto alloc_skbs_failed;
1647
1648 spider_net_set_multi(netdev);
1649
1650 /* further enhancement: setup hw vlan, if needed */
1651
1652 result = -EBUSY;
1653 if (request_irq(netdev->irq, spider_net_interrupt,
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07001654 IRQF_SHARED, netdev->name, netdev))
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001655 goto register_int_failed;
1656
1657 spider_net_enable_card(card);
1658
Jens Osterkamp543cec52005-09-06 19:30:54 -07001659 netif_start_queue(netdev);
1660 netif_carrier_on(netdev);
1661 netif_poll_enable(netdev);
1662
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001663 return 0;
1664
1665register_int_failed:
1666 spider_net_free_rx_chain_contents(card);
1667alloc_skbs_failed:
1668 spider_net_free_chain(card, &card->rx_chain);
1669alloc_rx_failed:
1670 spider_net_free_chain(card, &card->tx_chain);
1671alloc_tx_failed:
1672 return result;
1673}
1674
1675/**
1676 * spider_net_setup_phy - setup PHY
1677 * @card: card structure
1678 *
1679 * returns 0 on success, <0 on failure
1680 *
1681 * spider_net_setup_phy is used as part of spider_net_probe. Sets
1682 * the PHY to 1000 Mbps
1683 **/
1684static int
1685spider_net_setup_phy(struct spider_net_card *card)
1686{
1687 struct mii_phy *phy = &card->phy;
1688
1689 spider_net_write_reg(card, SPIDER_NET_GDTDMASEL,
1690 SPIDER_NET_DMASEL_VALUE);
1691 spider_net_write_reg(card, SPIDER_NET_GPCCTRL,
1692 SPIDER_NET_PHY_CTRL_VALUE);
1693 phy->mii_id = 1;
1694 phy->dev = card->netdev;
1695 phy->mdio_read = spider_net_read_phy;
1696 phy->mdio_write = spider_net_write_phy;
1697
1698 mii_phy_probe(phy, phy->mii_id);
1699
1700 if (phy->def->ops->setup_forced)
1701 phy->def->ops->setup_forced(phy, SPEED_1000, DUPLEX_FULL);
1702
Jens Osterkamp8ec93452006-05-04 05:59:41 -04001703 phy->def->ops->enable_fiber(phy);
Jens Osterkamp53abbf72005-09-16 08:55:33 +02001704
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001705 phy->def->ops->read_link(phy);
1706 pr_info("Found %s with %i Mbps, %s-duplex.\n", phy->def->name,
1707 phy->speed, phy->duplex==1 ? "Full" : "Half");
1708
1709 return 0;
1710}
1711
1712/**
1713 * spider_net_download_firmware - loads firmware into the adapter
1714 * @card: card structure
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001715 * @firmware_ptr: pointer to firmware data
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001716 *
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001717 * spider_net_download_firmware loads the firmware data into the
1718 * adapter. It assumes the length etc. to be allright.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001719 */
Arnd Bergmann0d3ea162006-01-12 17:16:41 -05001720static int
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001721spider_net_download_firmware(struct spider_net_card *card,
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001722 const void *firmware_ptr)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001723{
1724 int sequencer, i;
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001725 const u32 *fw_ptr = firmware_ptr;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001726
1727 /* stop sequencers */
1728 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1729 SPIDER_NET_STOP_SEQ_VALUE);
1730
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001731 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1732 sequencer++) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001733 spider_net_write_reg(card,
1734 SPIDER_NET_GSnPRGADR + sequencer * 8, 0);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001735 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001736 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1737 sequencer * 8, *fw_ptr);
1738 fw_ptr++;
1739 }
1740 }
1741
Arnd Bergmann0d3ea162006-01-12 17:16:41 -05001742 if (spider_net_read_reg(card, SPIDER_NET_GSINIT))
1743 return -EIO;
1744
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001745 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1746 SPIDER_NET_RUN_SEQ_VALUE);
Arnd Bergmann0d3ea162006-01-12 17:16:41 -05001747
1748 return 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001749}
1750
1751/**
1752 * spider_net_init_firmware - reads in firmware parts
1753 * @card: card structure
1754 *
1755 * Returns 0 on success, <0 on failure
1756 *
1757 * spider_net_init_firmware opens the sequencer firmware and does some basic
1758 * checks. This function opens and releases the firmware structure. A call
1759 * to download the firmware is performed before the release.
1760 *
1761 * Firmware format
1762 * ===============
1763 * spider_fw.bin is expected to be a file containing 6*1024*4 bytes, 4k being
1764 * the program for each sequencer. Use the command
1765 * tail -q -n +2 Seq_code1_0x088.txt Seq_code2_0x090.txt \
1766 * Seq_code3_0x098.txt Seq_code4_0x0A0.txt Seq_code5_0x0A8.txt \
1767 * Seq_code6_0x0B0.txt | xxd -r -p -c4 > spider_fw.bin
1768 *
1769 * to generate spider_fw.bin, if you have sequencer programs with something
1770 * like the following contents for each sequencer:
1771 * <ONE LINE COMMENT>
1772 * <FIRST 4-BYTES-WORD FOR SEQUENCER>
1773 * <SECOND 4-BYTES-WORD FOR SEQUENCER>
1774 * ...
1775 * <1024th 4-BYTES-WORD FOR SEQUENCER>
1776 */
1777static int
1778spider_net_init_firmware(struct spider_net_card *card)
1779{
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001780 struct firmware *firmware = NULL;
Arnd Bergmann030d6752006-01-12 17:16:42 -05001781 struct device_node *dn;
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001782 const u8 *fw_prop = NULL;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001783 int err = -ENOENT;
1784 int fw_size;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001785
Arnd Bergmann030d6752006-01-12 17:16:42 -05001786 if (request_firmware((const struct firmware **)&firmware,
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001787 SPIDER_NET_FIRMWARE_NAME, &card->pdev->dev) == 0) {
1788 if ( (firmware->size != SPIDER_NET_FIRMWARE_LEN) &&
1789 netif_msg_probe(card) ) {
1790 pr_err("Incorrect size of spidernet firmware in " \
1791 "filesystem. Looking in host firmware...\n");
1792 goto try_host_fw;
1793 }
1794 err = spider_net_download_firmware(card, firmware->data);
Arnd Bergmann030d6752006-01-12 17:16:42 -05001795
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001796 release_firmware(firmware);
1797 if (err)
1798 goto try_host_fw;
Arnd Bergmann030d6752006-01-12 17:16:42 -05001799
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001800 goto done;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001801 }
1802
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001803try_host_fw:
1804 dn = pci_device_to_OF_node(card->pdev);
1805 if (!dn)
1806 goto out_err;
1807
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001808 fw_prop = get_property(dn, "firmware", &fw_size);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001809 if (!fw_prop)
1810 goto out_err;
1811
1812 if ( (fw_size != SPIDER_NET_FIRMWARE_LEN) &&
1813 netif_msg_probe(card) ) {
1814 pr_err("Incorrect size of spidernet firmware in " \
1815 "host firmware\n");
1816 goto done;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001817 }
1818
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001819 err = spider_net_download_firmware(card, fw_prop);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001820
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001821done:
1822 return err;
1823out_err:
1824 if (netif_msg_probe(card))
1825 pr_err("Couldn't find spidernet firmware in filesystem " \
1826 "or host firmware\n");
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001827 return err;
1828}
1829
1830/**
1831 * spider_net_workaround_rxramfull - work around firmware bug
1832 * @card: card structure
1833 *
1834 * no return value
1835 **/
1836static void
1837spider_net_workaround_rxramfull(struct spider_net_card *card)
1838{
1839 int i, sequencer = 0;
1840
1841 /* cancel reset */
1842 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1843 SPIDER_NET_CKRCTRL_RUN_VALUE);
1844
1845 /* empty sequencer data */
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001846 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1847 sequencer++) {
Jens Osterkampee962a52006-07-13 11:54:23 +02001848 spider_net_write_reg(card, SPIDER_NET_GSnPRGADR +
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001849 sequencer * 8, 0x0);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001850 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001851 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1852 sequencer * 8, 0x0);
1853 }
1854 }
1855
1856 /* set sequencer operation */
1857 spider_net_write_reg(card, SPIDER_NET_GSINIT, 0x000000fe);
1858
1859 /* reset */
1860 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1861 SPIDER_NET_CKRCTRL_STOP_VALUE);
1862}
1863
1864/**
Jens Osterkampbdd01502006-07-13 11:54:08 +02001865 * spider_net_stop - called upon ifconfig down
1866 * @netdev: interface device structure
1867 *
1868 * always returns 0
1869 */
1870int
1871spider_net_stop(struct net_device *netdev)
1872{
1873 struct spider_net_card *card = netdev_priv(netdev);
1874
1875 tasklet_kill(&card->rxram_full_tl);
1876 netif_poll_disable(netdev);
1877 netif_carrier_off(netdev);
1878 netif_stop_queue(netdev);
1879 del_timer_sync(&card->tx_timer);
1880
1881 /* disable/mask all interrupts */
1882 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, 0);
1883 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK, 0);
1884 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK, 0);
1885
1886 /* free_irq(netdev->irq, netdev);*/
1887 free_irq(to_pci_dev(netdev->class_dev.dev)->irq, netdev);
1888
1889 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
1890 SPIDER_NET_DMA_TX_FEND_VALUE);
1891
1892 /* turn off DMA, force end */
1893 spider_net_disable_rxdmac(card);
1894
1895 /* release chains */
1896 if (spin_trylock(&card->tx_chain.lock)) {
1897 spider_net_release_tx_chain(card, 1);
1898 spin_unlock(&card->tx_chain.lock);
1899 }
1900
1901 spider_net_free_chain(card, &card->tx_chain);
1902 spider_net_free_chain(card, &card->rx_chain);
1903
1904 return 0;
1905}
1906
1907/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001908 * spider_net_tx_timeout_task - task scheduled by the watchdog timeout
1909 * function (to be called not under interrupt status)
1910 * @data: data, is interface device structure
1911 *
1912 * called as task when tx hangs, resets interface (if interface is up)
1913 */
1914static void
1915spider_net_tx_timeout_task(void *data)
1916{
1917 struct net_device *netdev = data;
1918 struct spider_net_card *card = netdev_priv(netdev);
1919
1920 if (!(netdev->flags & IFF_UP))
1921 goto out;
1922
1923 netif_device_detach(netdev);
1924 spider_net_stop(netdev);
1925
1926 spider_net_workaround_rxramfull(card);
1927 spider_net_init_card(card);
1928
1929 if (spider_net_setup_phy(card))
1930 goto out;
1931 if (spider_net_init_firmware(card))
1932 goto out;
1933
1934 spider_net_open(netdev);
Jens Osterkampbdd01502006-07-13 11:54:08 +02001935 spider_net_kick_tx_dma(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001936 netif_device_attach(netdev);
1937
1938out:
1939 atomic_dec(&card->tx_timeout_task_counter);
1940}
1941
1942/**
1943 * spider_net_tx_timeout - called when the tx timeout watchdog kicks in.
1944 * @netdev: interface device structure
1945 *
1946 * called, if tx hangs. Schedules a task that resets the interface
1947 */
1948static void
1949spider_net_tx_timeout(struct net_device *netdev)
1950{
1951 struct spider_net_card *card;
1952
1953 card = netdev_priv(netdev);
1954 atomic_inc(&card->tx_timeout_task_counter);
1955 if (netdev->flags & IFF_UP)
1956 schedule_work(&card->tx_timeout_task);
1957 else
1958 atomic_dec(&card->tx_timeout_task_counter);
Jim Lewis9b6b0b82006-09-22 17:22:53 -07001959 card->spider_stats.tx_timeouts++;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001960}
1961
1962/**
1963 * spider_net_setup_netdev_ops - initialization of net_device operations
1964 * @netdev: net_device structure
1965 *
1966 * fills out function pointers in the net_device structure
1967 */
1968static void
1969spider_net_setup_netdev_ops(struct net_device *netdev)
1970{
1971 netdev->open = &spider_net_open;
1972 netdev->stop = &spider_net_stop;
1973 netdev->hard_start_xmit = &spider_net_xmit;
1974 netdev->get_stats = &spider_net_get_stats;
1975 netdev->set_multicast_list = &spider_net_set_multi;
1976 netdev->set_mac_address = &spider_net_set_mac;
1977 netdev->change_mtu = &spider_net_change_mtu;
1978 netdev->do_ioctl = &spider_net_do_ioctl;
1979 /* tx watchdog */
1980 netdev->tx_timeout = &spider_net_tx_timeout;
1981 netdev->watchdog_timeo = SPIDER_NET_WATCHDOG_TIMEOUT;
1982 /* NAPI */
1983 netdev->poll = &spider_net_poll;
1984 netdev->weight = SPIDER_NET_NAPI_WEIGHT;
1985 /* HW VLAN */
1986 netdev->vlan_rx_register = &spider_net_vlan_rx_reg;
1987 netdev->vlan_rx_add_vid = &spider_net_vlan_rx_add;
1988 netdev->vlan_rx_kill_vid = &spider_net_vlan_rx_kill;
1989#ifdef CONFIG_NET_POLL_CONTROLLER
1990 /* poll controller */
1991 netdev->poll_controller = &spider_net_poll_controller;
1992#endif /* CONFIG_NET_POLL_CONTROLLER */
1993 /* ethtool ops */
1994 netdev->ethtool_ops = &spider_net_ethtool_ops;
1995}
1996
1997/**
1998 * spider_net_setup_netdev - initialization of net_device
1999 * @card: card structure
2000 *
2001 * Returns 0 on success or <0 on failure
2002 *
2003 * spider_net_setup_netdev initializes the net_device structure
2004 **/
2005static int
2006spider_net_setup_netdev(struct spider_net_card *card)
2007{
2008 int result;
2009 struct net_device *netdev = card->netdev;
2010 struct device_node *dn;
2011 struct sockaddr addr;
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10002012 const u8 *mac;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002013
2014 SET_MODULE_OWNER(netdev);
2015 SET_NETDEV_DEV(netdev, &card->pdev->dev);
2016
2017 pci_set_drvdata(card->pdev, netdev);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05002018
Arnd Bergmann11f1a522006-01-12 17:16:44 -05002019 card->rxram_full_tl.data = (unsigned long) card;
2020 card->rxram_full_tl.func =
2021 (void (*)(unsigned long)) spider_net_handle_rxram_full;
2022 init_timer(&card->tx_timer);
2023 card->tx_timer.function =
2024 (void (*)(unsigned long)) spider_net_cleanup_tx_ring;
2025 card->tx_timer.data = (unsigned long) card;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002026 netdev->irq = card->pdev->irq;
2027
2028 card->options.rx_csum = SPIDER_NET_RX_CSUM_DEFAULT;
2029
Jim Lewisb68a60e2006-08-14 23:00:23 -07002030 card->tx_desc = tx_descriptors;
2031 card->rx_desc = rx_descriptors;
2032
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002033 spider_net_setup_netdev_ops(netdev);
2034
Jens Osterkampbdd01502006-07-13 11:54:08 +02002035 netdev->features = NETIF_F_HW_CSUM | NETIF_F_LLTX;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002036 /* some time: NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
2037 * NETIF_F_HW_VLAN_FILTER */
2038
2039 netdev->irq = card->pdev->irq;
2040
2041 dn = pci_device_to_OF_node(card->pdev);
Jens Osterkamp543cec52005-09-06 19:30:54 -07002042 if (!dn)
2043 return -EIO;
2044
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10002045 mac = get_property(dn, "local-mac-address", NULL);
Jens Osterkamp543cec52005-09-06 19:30:54 -07002046 if (!mac)
2047 return -EIO;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002048 memcpy(addr.sa_data, mac, ETH_ALEN);
2049
2050 result = spider_net_set_mac(netdev, &addr);
2051 if ((result) && (netif_msg_probe(card)))
2052 pr_err("Failed to set MAC address: %i\n", result);
2053
2054 result = register_netdev(netdev);
2055 if (result) {
2056 if (netif_msg_probe(card))
2057 pr_err("Couldn't register net_device: %i\n",
2058 result);
2059 return result;
2060 }
2061
2062 if (netif_msg_probe(card))
2063 pr_info("Initialized device %s.\n", netdev->name);
2064
2065 return 0;
2066}
2067
2068/**
2069 * spider_net_alloc_card - allocates net_device and card structure
2070 *
2071 * returns the card structure or NULL in case of errors
2072 *
2073 * the card and net_device structures are linked to each other
2074 */
2075static struct spider_net_card *
2076spider_net_alloc_card(void)
2077{
2078 struct net_device *netdev;
2079 struct spider_net_card *card;
2080 size_t alloc_size;
2081
2082 alloc_size = sizeof (*card) +
2083 sizeof (struct spider_net_descr) * rx_descriptors +
2084 sizeof (struct spider_net_descr) * tx_descriptors;
2085 netdev = alloc_etherdev(alloc_size);
2086 if (!netdev)
2087 return NULL;
2088
2089 card = netdev_priv(netdev);
2090 card->netdev = netdev;
2091 card->msg_enable = SPIDER_NET_DEFAULT_MSG;
2092 INIT_WORK(&card->tx_timeout_task, spider_net_tx_timeout_task, netdev);
2093 init_waitqueue_head(&card->waitq);
2094 atomic_set(&card->tx_timeout_task_counter, 0);
2095
2096 return card;
2097}
2098
2099/**
2100 * spider_net_undo_pci_setup - releases PCI ressources
2101 * @card: card structure
2102 *
2103 * spider_net_undo_pci_setup releases the mapped regions
2104 */
2105static void
2106spider_net_undo_pci_setup(struct spider_net_card *card)
2107{
2108 iounmap(card->regs);
2109 pci_release_regions(card->pdev);
2110}
2111
2112/**
2113 * spider_net_setup_pci_dev - sets up the device in terms of PCI operations
2114 * @card: card structure
2115 * @pdev: PCI device
2116 *
2117 * Returns the card structure or NULL if any errors occur
2118 *
2119 * spider_net_setup_pci_dev initializes pdev and together with the
2120 * functions called in spider_net_open configures the device so that
2121 * data can be transferred over it
2122 * The net_device structure is attached to the card structure, if the
2123 * function returns without error.
2124 **/
2125static struct spider_net_card *
2126spider_net_setup_pci_dev(struct pci_dev *pdev)
2127{
2128 struct spider_net_card *card;
2129 unsigned long mmio_start, mmio_len;
2130
2131 if (pci_enable_device(pdev)) {
2132 pr_err("Couldn't enable PCI device\n");
2133 return NULL;
2134 }
2135
2136 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
2137 pr_err("Couldn't find proper PCI device base address.\n");
2138 goto out_disable_dev;
2139 }
2140
2141 if (pci_request_regions(pdev, spider_net_driver_name)) {
2142 pr_err("Couldn't obtain PCI resources, aborting.\n");
2143 goto out_disable_dev;
2144 }
2145
2146 pci_set_master(pdev);
2147
2148 card = spider_net_alloc_card();
2149 if (!card) {
2150 pr_err("Couldn't allocate net_device structure, "
2151 "aborting.\n");
2152 goto out_release_regions;
2153 }
2154 card->pdev = pdev;
2155
2156 /* fetch base address and length of first resource */
2157 mmio_start = pci_resource_start(pdev, 0);
2158 mmio_len = pci_resource_len(pdev, 0);
2159
2160 card->netdev->mem_start = mmio_start;
2161 card->netdev->mem_end = mmio_start + mmio_len;
2162 card->regs = ioremap(mmio_start, mmio_len);
2163
2164 if (!card->regs) {
2165 pr_err("Couldn't obtain PCI resources, aborting.\n");
2166 goto out_release_regions;
2167 }
2168
2169 return card;
2170
2171out_release_regions:
2172 pci_release_regions(pdev);
2173out_disable_dev:
2174 pci_disable_device(pdev);
2175 pci_set_drvdata(pdev, NULL);
2176 return NULL;
2177}
2178
2179/**
2180 * spider_net_probe - initialization of a device
2181 * @pdev: PCI device
2182 * @ent: entry in the device id list
2183 *
2184 * Returns 0 on success, <0 on failure
2185 *
2186 * spider_net_probe initializes pdev and registers a net_device
2187 * structure for it. After that, the device can be ifconfig'ed up
2188 **/
2189static int __devinit
2190spider_net_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2191{
2192 int err = -EIO;
2193 struct spider_net_card *card;
2194
2195 card = spider_net_setup_pci_dev(pdev);
2196 if (!card)
2197 goto out;
2198
2199 spider_net_workaround_rxramfull(card);
2200 spider_net_init_card(card);
2201
2202 err = spider_net_setup_phy(card);
2203 if (err)
2204 goto out_undo_pci;
2205
2206 err = spider_net_init_firmware(card);
2207 if (err)
2208 goto out_undo_pci;
2209
2210 err = spider_net_setup_netdev(card);
2211 if (err)
2212 goto out_undo_pci;
2213
2214 return 0;
2215
2216out_undo_pci:
2217 spider_net_undo_pci_setup(card);
2218 free_netdev(card->netdev);
2219out:
2220 return err;
2221}
2222
2223/**
2224 * spider_net_remove - removal of a device
2225 * @pdev: PCI device
2226 *
2227 * Returns 0 on success, <0 on failure
2228 *
2229 * spider_net_remove is called to remove the device and unregisters the
2230 * net_device
2231 **/
2232static void __devexit
2233spider_net_remove(struct pci_dev *pdev)
2234{
2235 struct net_device *netdev;
2236 struct spider_net_card *card;
2237
2238 netdev = pci_get_drvdata(pdev);
2239 card = netdev_priv(netdev);
2240
2241 wait_event(card->waitq,
2242 atomic_read(&card->tx_timeout_task_counter) == 0);
2243
2244 unregister_netdev(netdev);
Jens Osterkamp543cec52005-09-06 19:30:54 -07002245
2246 /* switch off card */
2247 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2248 SPIDER_NET_CKRCTRL_STOP_VALUE);
2249 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2250 SPIDER_NET_CKRCTRL_RUN_VALUE);
2251
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002252 spider_net_undo_pci_setup(card);
2253 free_netdev(netdev);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002254}
2255
2256static struct pci_driver spider_net_driver = {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002257 .name = spider_net_driver_name,
2258 .id_table = spider_net_pci_tbl,
2259 .probe = spider_net_probe,
2260 .remove = __devexit_p(spider_net_remove)
2261};
2262
2263/**
2264 * spider_net_init - init function when the driver is loaded
2265 *
2266 * spider_net_init registers the device driver
2267 */
2268static int __init spider_net_init(void)
2269{
Linas Vepstas90f10842006-10-10 15:56:04 -05002270 printk(KERN_INFO "Spidernet version %s.\n", VERSION);
2271
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002272 if (rx_descriptors < SPIDER_NET_RX_DESCRIPTORS_MIN) {
2273 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MIN;
2274 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2275 }
2276 if (rx_descriptors > SPIDER_NET_RX_DESCRIPTORS_MAX) {
2277 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MAX;
2278 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2279 }
2280 if (tx_descriptors < SPIDER_NET_TX_DESCRIPTORS_MIN) {
2281 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MIN;
2282 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2283 }
2284 if (tx_descriptors > SPIDER_NET_TX_DESCRIPTORS_MAX) {
2285 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MAX;
2286 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2287 }
2288
2289 return pci_register_driver(&spider_net_driver);
2290}
2291
2292/**
2293 * spider_net_cleanup - exit function when driver is unloaded
2294 *
2295 * spider_net_cleanup unregisters the device driver
2296 */
2297static void __exit spider_net_cleanup(void)
2298{
2299 pci_unregister_driver(&spider_net_driver);
2300}
2301
2302module_init(spider_net_init);
2303module_exit(spider_net_cleanup);