blob: f8d7d0d91a6dca9251bbb2a256408924d4b9005c [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{
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500649 struct spider_net_descr *descr;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500650 dma_addr_t buf;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500651 unsigned long flags;
Linas Vepstas808999c2006-10-10 16:01:00 -0500652 int length;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500653
Linas Vepstas808999c2006-10-10 16:01:00 -0500654 length = skb->len;
655 if (length < ETH_ZLEN) {
656 if (skb_pad(skb, ETH_ZLEN-length))
657 return 0;
658 length = ETH_ZLEN;
659 }
660
661 buf = pci_map_single(card->pdev, skb->data, length, PCI_DMA_TODEVICE);
Olof Johanssond4b0a4c2006-09-27 14:02:26 -0500662 if (pci_dma_mapping_error(buf)) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500663 if (netif_msg_tx_err(card) && net_ratelimit())
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700664 pr_err("could not iommu-map packet (%p, %i). "
Linas Vepstas808999c2006-10-10 16:01:00 -0500665 "Dropping packet\n", skb->data, length);
Jim Lewis9b6b0b82006-09-22 17:22:53 -0700666 card->spider_stats.tx_iommu_map_error++;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700667 return -ENOMEM;
668 }
669
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500670 spin_lock_irqsave(&card->tx_chain.lock, flags);
671 descr = card->tx_chain.head;
672 card->tx_chain.head = descr->next;
673
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500674 descr->buf_addr = buf;
Linas Vepstas808999c2006-10-10 16:01:00 -0500675 descr->buf_size = length;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200676 descr->next_descr_addr = 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700677 descr->skb = skb;
678 descr->data_status = 0;
679
Jens Osterkampbdd01502006-07-13 11:54:08 +0200680 descr->dmac_cmd_status =
681 SPIDER_NET_DESCR_CARDOWNED | SPIDER_NET_DMAC_NOCS;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500682 spin_unlock_irqrestore(&card->tx_chain.lock, flags);
683
Jens Osterkampbdd01502006-07-13 11:54:08 +0200684 if (skb->protocol == htons(ETH_P_IP))
685 switch (skb->nh.iph->protocol) {
686 case IPPROTO_TCP:
687 descr->dmac_cmd_status |= SPIDER_NET_DMAC_TCP;
688 break;
689 case IPPROTO_UDP:
690 descr->dmac_cmd_status |= SPIDER_NET_DMAC_UDP;
691 break;
692 }
693
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500694 /* Chain the bus address, so that the DMA engine finds this descr. */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200695 descr->prev->next_descr_addr = descr->bus_addr;
696
Linas Vepstas917a5b82006-10-10 16:01:51 -0500697 card->netdev->trans_start = jiffies; /* set netdev watchdog timer */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200698 return 0;
699}
700
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500701static void
702spider_net_set_low_watermark(struct spider_net_card *card)
703{
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500704 unsigned long flags;
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500705 int status;
706 int cnt=0;
707 int i;
708 struct spider_net_descr *descr = card->tx_chain.tail;
709
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500710 /* Measure the length of the queue. Measurement does not
711 * need to be precise -- does not need a lock. */
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500712 while (descr != card->tx_chain.head) {
713 status = descr->dmac_cmd_status & SPIDER_NET_DESCR_NOT_IN_USE;
714 if (status == SPIDER_NET_DESCR_NOT_IN_USE)
715 break;
716 descr = descr->next;
717 cnt++;
718 }
719
720 /* If TX queue is short, don't even bother with interrupts */
721 if (cnt < card->tx_desc/4)
722 return;
723
724 /* Set low-watermark 3/4th's of the way into the queue. */
725 descr = card->tx_chain.tail;
726 cnt = (cnt*3)/4;
727 for (i=0;i<cnt; i++)
728 descr = descr->next;
729
730 /* Set the new watermark, clear the old watermark */
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500731 spin_lock_irqsave(&card->tx_chain.lock, flags);
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500732 descr->dmac_cmd_status |= SPIDER_NET_DESCR_TXDESFLG;
733 if (card->low_watermark && card->low_watermark != descr)
734 card->low_watermark->dmac_cmd_status =
735 card->low_watermark->dmac_cmd_status & ~SPIDER_NET_DESCR_TXDESFLG;
736 card->low_watermark = descr;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500737 spin_unlock_irqrestore(&card->tx_chain.lock, flags);
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500738}
739
Jens Osterkampbdd01502006-07-13 11:54:08 +0200740/**
741 * spider_net_release_tx_chain - processes sent tx descriptors
742 * @card: adapter structure
743 * @brutal: if set, don't care about whether descriptor seems to be in use
744 *
745 * returns 0 if the tx ring is empty, otherwise 1.
746 *
747 * spider_net_release_tx_chain releases the tx descriptors that spider has
748 * finished with (if non-brutal) or simply release tx descriptors (if brutal).
749 * If some other context is calling this function, we return 1 so that we're
750 * scheduled again (if we were scheduled) and will not loose initiative.
751 */
752static int
753spider_net_release_tx_chain(struct spider_net_card *card, int brutal)
754{
755 struct spider_net_descr_chain *chain = &card->tx_chain;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500756 struct spider_net_descr *descr;
757 struct sk_buff *skb;
758 u32 buf_addr;
759 unsigned long flags;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200760 int status;
761
762 spider_net_read_reg(card, SPIDER_NET_GDTDMACCNTR);
763
764 while (chain->tail != chain->head) {
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500765 spin_lock_irqsave(&chain->lock, flags);
766 descr = chain->tail;
767
768 status = spider_net_get_descr_status(descr);
Jens Osterkampbdd01502006-07-13 11:54:08 +0200769 switch (status) {
770 case SPIDER_NET_DESCR_COMPLETE:
771 card->netdev_stats.tx_packets++;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500772 card->netdev_stats.tx_bytes += descr->skb->len;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200773 break;
774
775 case SPIDER_NET_DESCR_CARDOWNED:
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500776 if (!brutal) {
777 spin_unlock_irqrestore(&chain->lock, flags);
Jens Osterkampbdd01502006-07-13 11:54:08 +0200778 return 1;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500779 }
780
Jens Osterkampbdd01502006-07-13 11:54:08 +0200781 /* fallthrough, if we release the descriptors
782 * brutally (then we don't care about
783 * SPIDER_NET_DESCR_CARDOWNED) */
784
785 case SPIDER_NET_DESCR_RESPONSE_ERROR:
786 case SPIDER_NET_DESCR_PROTECTION_ERROR:
787 case SPIDER_NET_DESCR_FORCE_END:
788 if (netif_msg_tx_err(card))
789 pr_err("%s: forcing end of tx descriptor "
790 "with status x%02x\n",
791 card->netdev->name, status);
792 card->netdev_stats.tx_errors++;
793 break;
794
795 default:
796 card->netdev_stats.tx_dropped++;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500797 if (!brutal) {
798 spin_unlock_irqrestore(&chain->lock, flags);
Linas Vepstasc3fee4c2006-10-10 16:00:04 -0500799 return 1;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500800 }
Jens Osterkampbdd01502006-07-13 11:54:08 +0200801 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700802
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500803 chain->tail = descr->next;
804 descr->dmac_cmd_status |= SPIDER_NET_DESCR_NOT_IN_USE;
805 skb = descr->skb;
806 buf_addr = descr->buf_addr;
807 spin_unlock_irqrestore(&chain->lock, flags);
808
809 /* unmap the skb */
810 if (skb) {
811 int len = skb->len < ETH_ZLEN ? ETH_ZLEN : skb->len;
812 pci_unmap_single(card->pdev, buf_addr, len, PCI_DMA_TODEVICE);
813 dev_kfree_skb(skb);
814 }
815 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700816 return 0;
817}
818
819/**
820 * spider_net_kick_tx_dma - enables TX DMA processing
821 * @card: card structure
822 * @descr: descriptor address to enable TX processing at
823 *
824 * spider_net_kick_tx_dma writes the current tx chain head as start address
825 * of the tx descriptor chain and enables the transmission DMA engine
826 */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200827static inline void
828spider_net_kick_tx_dma(struct spider_net_card *card)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700829{
Jens Osterkampbdd01502006-07-13 11:54:08 +0200830 struct spider_net_descr *descr;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700831
Jens Osterkampbdd01502006-07-13 11:54:08 +0200832 if (spider_net_read_reg(card, SPIDER_NET_GDTDMACCNTR) &
833 SPIDER_NET_TX_DMA_EN)
834 goto out;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700835
Jens Osterkampbdd01502006-07-13 11:54:08 +0200836 descr = card->tx_chain.tail;
837 for (;;) {
838 if (spider_net_get_descr_status(descr) ==
839 SPIDER_NET_DESCR_CARDOWNED) {
840 spider_net_write_reg(card, SPIDER_NET_GDTDCHA,
841 descr->bus_addr);
842 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
843 SPIDER_NET_DMA_TX_VALUE);
844 break;
845 }
846 if (descr == card->tx_chain.head)
847 break;
848 descr = descr->next;
849 }
850
851out:
852 mod_timer(&card->tx_timer, jiffies + SPIDER_NET_TX_TIMER);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700853}
854
855/**
856 * spider_net_xmit - transmits a frame over the device
857 * @skb: packet to send out
858 * @netdev: interface device structure
859 *
Jens Osterkampbdd01502006-07-13 11:54:08 +0200860 * returns 0 on success, !0 on failure
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700861 */
862static int
863spider_net_xmit(struct sk_buff *skb, struct net_device *netdev)
864{
865 struct spider_net_card *card = netdev_priv(netdev);
Jens Osterkampbdd01502006-07-13 11:54:08 +0200866 struct spider_net_descr_chain *chain = &card->tx_chain;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200867
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500868 spider_net_release_tx_chain(card, 0);
869
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500870 if ((chain->head->next == chain->tail->prev) ||
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500871 (spider_net_prepare_tx_descr(card, skb) != 0)) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500872
Jim Lewis9b6b0b82006-09-22 17:22:53 -0700873 card->netdev_stats.tx_dropped++;
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500874 netif_stop_queue(netdev);
875 return NETDEV_TX_BUSY;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200876 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700877
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500878 spider_net_set_low_watermark(card);
Jens Osterkampbdd01502006-07-13 11:54:08 +0200879 spider_net_kick_tx_dma(card);
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500880 return NETDEV_TX_OK;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200881}
882
883/**
884 * spider_net_cleanup_tx_ring - cleans up the TX ring
885 * @card: card structure
886 *
Linas Vepstas68a8c602006-10-10 16:13:05 -0500887 * spider_net_cleanup_tx_ring is called by either the tx_timer
888 * or from the NAPI polling routine.
889 * This routine releases resources associted with transmitted
890 * packets, including updating the queue tail pointer.
Jens Osterkampbdd01502006-07-13 11:54:08 +0200891 */
892static void
893spider_net_cleanup_tx_ring(struct spider_net_card *card)
894{
Jens Osterkampbdd01502006-07-13 11:54:08 +0200895 if ((spider_net_release_tx_chain(card, 0) != 0) &&
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500896 (card->netdev->flags & IFF_UP)) {
Jens Osterkampbdd01502006-07-13 11:54:08 +0200897 spider_net_kick_tx_dma(card);
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500898 netif_wake_queue(card->netdev);
899 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700900}
901
902/**
903 * spider_net_do_ioctl - called for device ioctls
904 * @netdev: interface device structure
905 * @ifr: request parameter structure for ioctl
906 * @cmd: command code for ioctl
907 *
908 * returns 0 on success, <0 on failure. Currently, we have no special ioctls.
909 * -EOPNOTSUPP is returned, if an unknown ioctl was requested
910 */
911static int
912spider_net_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
913{
914 switch (cmd) {
915 default:
916 return -EOPNOTSUPP;
917 }
918}
919
920/**
921 * spider_net_pass_skb_up - takes an skb from a descriptor and passes it on
922 * @descr: descriptor to process
923 * @card: card structure
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500924 * @napi: whether caller is in NAPI context
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700925 *
926 * returns 1 on success, 0 if no packet was passed to the stack
927 *
928 * iommu-unmaps the skb, fills out skb structure and passes the data to the
929 * stack. The descriptor state is not changed.
930 */
931static int
932spider_net_pass_skb_up(struct spider_net_descr *descr,
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500933 struct spider_net_card *card, int napi)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700934{
935 struct sk_buff *skb;
936 struct net_device *netdev;
937 u32 data_status, data_error;
938
939 data_status = descr->data_status;
940 data_error = descr->data_error;
941
942 netdev = card->netdev;
943
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500944 /* unmap descriptor */
945 pci_unmap_single(card->pdev, descr->buf_addr, SPIDER_NET_MAX_FRAME,
Jens Osterkampbdd01502006-07-13 11:54:08 +0200946 PCI_DMA_FROMDEVICE);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700947
948 /* the cases we'll throw away the packet immediately */
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500949 if (data_error & SPIDER_NET_DESTROY_RX_FLAGS) {
950 if (netif_msg_rx_err(card))
951 pr_err("error in received descriptor found, "
952 "data_status=x%08x, data_error=x%08x\n",
953 data_status, data_error);
Jim Lewis9b6b0b82006-09-22 17:22:53 -0700954 card->spider_stats.rx_desc_error++;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700955 return 0;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500956 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700957
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500958 skb = descr->skb;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700959 skb->dev = netdev;
960 skb_put(skb, descr->valid_size);
961
962 /* the card seems to add 2 bytes of junk in front
963 * of the ethernet frame */
964#define SPIDER_MISALIGN 2
965 skb_pull(skb, SPIDER_MISALIGN);
966 skb->protocol = eth_type_trans(skb, netdev);
967
968 /* checksum offload */
969 if (card->options.rx_csum) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500970 if ( ( (data_status & SPIDER_NET_DATA_STATUS_CKSUM_MASK) ==
971 SPIDER_NET_DATA_STATUS_CKSUM_MASK) &&
972 !(data_error & SPIDER_NET_DATA_ERR_CKSUM_MASK))
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700973 skb->ip_summed = CHECKSUM_UNNECESSARY;
974 else
975 skb->ip_summed = CHECKSUM_NONE;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500976 } else
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700977 skb->ip_summed = CHECKSUM_NONE;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700978
979 if (data_status & SPIDER_NET_VLAN_PACKET) {
980 /* further enhancements: HW-accel VLAN
981 * vlan_hwaccel_receive_skb
982 */
983 }
984
985 /* pass skb up to stack */
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500986 if (napi)
987 netif_receive_skb(skb);
988 else
989 netif_rx_ni(skb);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700990
991 /* update netdevice statistics */
992 card->netdev_stats.rx_packets++;
993 card->netdev_stats.rx_bytes += skb->len;
994
995 return 1;
996}
997
998/**
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500999 * spider_net_decode_one_descr - processes an rx descriptor
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001000 * @card: card structure
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001001 * @napi: whether caller is in NAPI context
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001002 *
1003 * returns 1 if a packet has been sent to the stack, otherwise 0
1004 *
1005 * processes an rx descriptor by iommu-unmapping the data buffer and passing
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001006 * the packet up to the stack. This function is called in softirq
1007 * context, e.g. either bottom half from interrupt or NAPI polling context
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001008 */
1009static int
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001010spider_net_decode_one_descr(struct spider_net_card *card, int napi)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001011{
Jens Osterkampbdd01502006-07-13 11:54:08 +02001012 struct spider_net_descr_chain *chain = &card->rx_chain;
1013 struct spider_net_descr *descr = chain->tail;
1014 int status;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001015 int result;
1016
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001017 status = spider_net_get_descr_status(descr);
1018
1019 if (status == SPIDER_NET_DESCR_CARDOWNED) {
1020 /* nothing in the descriptor yet */
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001021 result=0;
1022 goto out;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001023 }
1024
1025 if (status == SPIDER_NET_DESCR_NOT_IN_USE) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001026 /* not initialized yet, the ring must be empty */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001027 spider_net_refill_rx_chain(card);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001028 spider_net_enable_rxdmac(card);
1029 result=0;
1030 goto out;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001031 }
1032
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001033 /* descriptor definitively used -- move on tail */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001034 chain->tail = descr->next;
1035
1036 result = 0;
1037 if ( (status == SPIDER_NET_DESCR_RESPONSE_ERROR) ||
1038 (status == SPIDER_NET_DESCR_PROTECTION_ERROR) ||
1039 (status == SPIDER_NET_DESCR_FORCE_END) ) {
1040 if (netif_msg_rx_err(card))
1041 pr_err("%s: dropping RX descriptor with state %d\n",
1042 card->netdev->name, status);
1043 card->netdev_stats.rx_dropped++;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001044 pci_unmap_single(card->pdev, descr->buf_addr,
Jens Osterkampbdd01502006-07-13 11:54:08 +02001045 SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001046 dev_kfree_skb_irq(descr->skb);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001047 goto refill;
1048 }
1049
1050 if ( (status != SPIDER_NET_DESCR_COMPLETE) &&
1051 (status != SPIDER_NET_DESCR_FRAME_END) ) {
Jim Lewis9b6b0b82006-09-22 17:22:53 -07001052 if (netif_msg_rx_err(card)) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001053 pr_err("%s: RX descriptor with state %d\n",
1054 card->netdev->name, status);
Jim Lewis9b6b0b82006-09-22 17:22:53 -07001055 card->spider_stats.rx_desc_unk_state++;
1056 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001057 goto refill;
1058 }
1059
1060 /* ok, we've got a packet in descr */
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001061 result = spider_net_pass_skb_up(descr, card, napi);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001062refill:
Jens Osterkampbdd01502006-07-13 11:54:08 +02001063 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001064 /* change the descriptor state: */
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001065 if (!napi)
1066 spider_net_refill_rx_chain(card);
1067out:
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001068 return result;
1069}
1070
1071/**
1072 * spider_net_poll - NAPI poll function called by the stack to return packets
1073 * @netdev: interface device structure
1074 * @budget: number of packets we can pass to the stack at most
1075 *
1076 * returns 0 if no more packets available to the driver/stack. Returns 1,
1077 * if the quota is exceeded, but the driver has still packets.
1078 *
1079 * spider_net_poll returns all packets from the rx descriptors to the stack
1080 * (using netif_receive_skb). If all/enough packets are up, the driver
1081 * reenables interrupts and returns 0. If not, 1 is returned.
1082 */
1083static int
1084spider_net_poll(struct net_device *netdev, int *budget)
1085{
1086 struct spider_net_card *card = netdev_priv(netdev);
1087 int packets_to_do, packets_done = 0;
1088 int no_more_packets = 0;
1089
Linas Vepstas68a8c602006-10-10 16:13:05 -05001090 spider_net_cleanup_tx_ring(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001091 packets_to_do = min(*budget, netdev->quota);
1092
1093 while (packets_to_do) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001094 if (spider_net_decode_one_descr(card, 1)) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001095 packets_done++;
1096 packets_to_do--;
1097 } else {
1098 /* no more packets for the stack */
1099 no_more_packets = 1;
1100 break;
1101 }
1102 }
1103
1104 netdev->quota -= packets_done;
1105 *budget -= packets_done;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001106 spider_net_refill_rx_chain(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001107
1108 /* if all packets are in the stack, enable interrupts and return 0 */
1109 /* if not, return 1 */
1110 if (no_more_packets) {
1111 netif_rx_complete(netdev);
1112 spider_net_rx_irq_on(card);
1113 return 0;
1114 }
1115
1116 return 1;
1117}
1118
1119/**
1120 * spider_net_vlan_rx_reg - initializes VLAN structures in the driver and card
1121 * @netdev: interface device structure
1122 * @grp: vlan_group structure that is registered (NULL on destroying interface)
1123 */
1124static void
1125spider_net_vlan_rx_reg(struct net_device *netdev, struct vlan_group *grp)
1126{
1127 /* further enhancement... yet to do */
1128 return;
1129}
1130
1131/**
1132 * spider_net_vlan_rx_add - adds VLAN id to the card filter
1133 * @netdev: interface device structure
1134 * @vid: VLAN id to add
1135 */
1136static void
1137spider_net_vlan_rx_add(struct net_device *netdev, uint16_t vid)
1138{
1139 /* further enhancement... yet to do */
1140 /* add vid to card's VLAN filter table */
1141 return;
1142}
1143
1144/**
1145 * spider_net_vlan_rx_kill - removes VLAN id to the card filter
1146 * @netdev: interface device structure
1147 * @vid: VLAN id to remove
1148 */
1149static void
1150spider_net_vlan_rx_kill(struct net_device *netdev, uint16_t vid)
1151{
1152 /* further enhancement... yet to do */
1153 /* remove vid from card's VLAN filter table */
1154}
1155
1156/**
1157 * spider_net_get_stats - get interface statistics
1158 * @netdev: interface device structure
1159 *
1160 * returns the interface statistics residing in the spider_net_card struct
1161 */
1162static struct net_device_stats *
1163spider_net_get_stats(struct net_device *netdev)
1164{
1165 struct spider_net_card *card = netdev_priv(netdev);
1166 struct net_device_stats *stats = &card->netdev_stats;
1167 return stats;
1168}
1169
1170/**
1171 * spider_net_change_mtu - changes the MTU of an interface
1172 * @netdev: interface device structure
1173 * @new_mtu: new MTU value
1174 *
1175 * returns 0 on success, <0 on failure
1176 */
1177static int
1178spider_net_change_mtu(struct net_device *netdev, int new_mtu)
1179{
1180 /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
1181 * and mtu is outbound only anyway */
1182 if ( (new_mtu < SPIDER_NET_MIN_MTU ) ||
1183 (new_mtu > SPIDER_NET_MAX_MTU) )
1184 return -EINVAL;
1185 netdev->mtu = new_mtu;
1186 return 0;
1187}
1188
1189/**
1190 * spider_net_set_mac - sets the MAC of an interface
1191 * @netdev: interface device structure
1192 * @ptr: pointer to new MAC address
1193 *
1194 * Returns 0 on success, <0 on failure. Currently, we don't support this
1195 * and will always return EOPNOTSUPP.
1196 */
1197static int
1198spider_net_set_mac(struct net_device *netdev, void *p)
1199{
1200 struct spider_net_card *card = netdev_priv(netdev);
Jens Osterkamp054034d2005-09-06 19:30:55 -07001201 u32 macl, macu, regvalue;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001202 struct sockaddr *addr = p;
1203
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001204 if (!is_valid_ether_addr(addr->sa_data))
1205 return -EADDRNOTAVAIL;
1206
Jens Osterkamp054034d2005-09-06 19:30:55 -07001207 /* switch off GMACTPE and GMACRPE */
1208 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1209 regvalue &= ~((1 << 5) | (1 << 6));
1210 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1211
1212 /* write mac */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001213 macu = (addr->sa_data[0]<<24) + (addr->sa_data[1]<<16) +
1214 (addr->sa_data[2]<<8) + (addr->sa_data[3]);
1215 macl = (addr->sa_data[4]<<8) + (addr->sa_data[5]);
1216 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACU, macu);
1217 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACL, macl);
1218
Jens Osterkamp054034d2005-09-06 19:30:55 -07001219 /* switch GMACTPE and GMACRPE back on */
1220 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1221 regvalue |= ((1 << 5) | (1 << 6));
1222 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1223
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001224 spider_net_set_promisc(card);
1225
1226 /* look up, whether we have been successful */
1227 if (spider_net_get_mac_address(netdev))
1228 return -EADDRNOTAVAIL;
1229 if (memcmp(netdev->dev_addr,addr->sa_data,netdev->addr_len))
1230 return -EADDRNOTAVAIL;
1231
1232 return 0;
1233}
1234
1235/**
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001236 * spider_net_handle_rxram_full - cleans up RX ring upon RX RAM full interrupt
1237 * @card: card structure
1238 *
1239 * spider_net_handle_rxram_full empties the RX ring so that spider can put
1240 * more packets in it and empty its RX RAM. This is called in bottom half
1241 * context
1242 */
1243static void
1244spider_net_handle_rxram_full(struct spider_net_card *card)
1245{
1246 while (spider_net_decode_one_descr(card, 0))
1247 ;
1248 spider_net_enable_rxchtails(card);
1249 spider_net_enable_rxdmac(card);
1250 netif_rx_schedule(card->netdev);
1251}
1252
1253/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001254 * spider_net_handle_error_irq - handles errors raised by an interrupt
1255 * @card: card structure
1256 * @status_reg: interrupt status register 0 (GHIINT0STS)
1257 *
1258 * spider_net_handle_error_irq treats or ignores all error conditions
1259 * found when an interrupt is presented
1260 */
1261static void
1262spider_net_handle_error_irq(struct spider_net_card *card, u32 status_reg)
1263{
1264 u32 error_reg1, error_reg2;
1265 u32 i;
1266 int show_error = 1;
1267
1268 error_reg1 = spider_net_read_reg(card, SPIDER_NET_GHIINT1STS);
1269 error_reg2 = spider_net_read_reg(card, SPIDER_NET_GHIINT2STS);
1270
1271 /* check GHIINT0STS ************************************/
1272 if (status_reg)
1273 for (i = 0; i < 32; i++)
1274 if (status_reg & (1<<i))
1275 switch (i)
1276 {
1277 /* let error_reg1 and error_reg2 evaluation decide, what to do
1278 case SPIDER_NET_PHYINT:
1279 case SPIDER_NET_GMAC2INT:
1280 case SPIDER_NET_GMAC1INT:
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001281 case SPIDER_NET_GFIFOINT:
1282 case SPIDER_NET_DMACINT:
1283 case SPIDER_NET_GSYSINT:
1284 break; */
1285
Linas Vepstas98b90402006-10-10 16:08:42 -05001286 case SPIDER_NET_GIPSINT:
1287 show_error = 0;
1288 break;
1289
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001290 case SPIDER_NET_GPWOPCMPINT:
1291 /* PHY write operation completed */
1292 show_error = 0;
1293 break;
1294 case SPIDER_NET_GPROPCMPINT:
1295 /* PHY read operation completed */
1296 /* we don't use semaphores, as we poll for the completion
1297 * of the read operation in spider_net_read_phy. Should take
1298 * about 50 us */
1299 show_error = 0;
1300 break;
1301 case SPIDER_NET_GPWFFINT:
1302 /* PHY command queue full */
1303 if (netif_msg_intr(card))
1304 pr_err("PHY write queue full\n");
1305 show_error = 0;
1306 break;
1307
1308 /* case SPIDER_NET_GRMDADRINT: not used. print a message */
1309 /* case SPIDER_NET_GRMARPINT: not used. print a message */
1310 /* case SPIDER_NET_GRMMPINT: not used. print a message */
1311
1312 case SPIDER_NET_GDTDEN0INT:
1313 /* someone has set TX_DMA_EN to 0 */
1314 show_error = 0;
1315 break;
1316
1317 case SPIDER_NET_GDDDEN0INT: /* fallthrough */
1318 case SPIDER_NET_GDCDEN0INT: /* fallthrough */
1319 case SPIDER_NET_GDBDEN0INT: /* fallthrough */
1320 case SPIDER_NET_GDADEN0INT:
1321 /* someone has set RX_DMA_EN to 0 */
1322 show_error = 0;
1323 break;
1324
1325 /* RX interrupts */
1326 case SPIDER_NET_GDDFDCINT:
1327 case SPIDER_NET_GDCFDCINT:
1328 case SPIDER_NET_GDBFDCINT:
1329 case SPIDER_NET_GDAFDCINT:
1330 /* case SPIDER_NET_GDNMINT: not used. print a message */
1331 /* case SPIDER_NET_GCNMINT: not used. print a message */
1332 /* case SPIDER_NET_GBNMINT: not used. print a message */
1333 /* case SPIDER_NET_GANMINT: not used. print a message */
1334 /* case SPIDER_NET_GRFNMINT: not used. print a message */
1335 show_error = 0;
1336 break;
1337
1338 /* TX interrupts */
1339 case SPIDER_NET_GDTFDCINT:
1340 show_error = 0;
1341 break;
1342 case SPIDER_NET_GTTEDINT:
1343 show_error = 0;
1344 break;
1345 case SPIDER_NET_GDTDCEINT:
1346 /* chain end. If a descriptor should be sent, kick off
1347 * tx dma
Linas Vepstas98b90402006-10-10 16:08:42 -05001348 if (card->tx_chain.tail != card->tx_chain.head)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001349 spider_net_kick_tx_dma(card);
Linas Vepstas98b90402006-10-10 16:08:42 -05001350 */
1351 show_error = 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001352 break;
1353
1354 /* case SPIDER_NET_G1TMCNTINT: not used. print a message */
1355 /* case SPIDER_NET_GFREECNTINT: not used. print a message */
1356 }
1357
1358 /* check GHIINT1STS ************************************/
1359 if (error_reg1)
1360 for (i = 0; i < 32; i++)
1361 if (error_reg1 & (1<<i))
1362 switch (i)
1363 {
1364 case SPIDER_NET_GTMFLLINT:
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001365 if (netif_msg_intr(card) && net_ratelimit())
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001366 pr_err("Spider TX RAM full\n");
1367 show_error = 0;
1368 break;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001369 case SPIDER_NET_GRFDFLLINT: /* fallthrough */
1370 case SPIDER_NET_GRFCFLLINT: /* fallthrough */
1371 case SPIDER_NET_GRFBFLLINT: /* fallthrough */
1372 case SPIDER_NET_GRFAFLLINT: /* fallthrough */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001373 case SPIDER_NET_GRMFLLINT:
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001374 if (netif_msg_intr(card) && net_ratelimit())
Jens Osterkamp4e5077b2006-03-28 15:59:55 +02001375 pr_debug("Spider RX RAM full, incoming packets "
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001376 "might be discarded!\n");
1377 spider_net_rx_irq_off(card);
1378 tasklet_schedule(&card->rxram_full_tl);
1379 show_error = 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001380 break;
1381
1382 /* case SPIDER_NET_GTMSHTINT: problem, print a message */
1383 case SPIDER_NET_GDTINVDINT:
1384 /* allrighty. tx from previous descr ok */
1385 show_error = 0;
1386 break;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001387
1388 /* chain end */
1389 case SPIDER_NET_GDDDCEINT: /* fallthrough */
1390 case SPIDER_NET_GDCDCEINT: /* fallthrough */
1391 case SPIDER_NET_GDBDCEINT: /* fallthrough */
1392 case SPIDER_NET_GDADCEINT:
1393 if (netif_msg_intr(card))
1394 pr_err("got descriptor chain end interrupt, "
1395 "restarting DMAC %c.\n",
Linas Vepstas37aad752006-10-10 16:06:53 -05001396 'D'-(i-SPIDER_NET_GDDDCEINT)/3);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001397 spider_net_refill_rx_chain(card);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001398 spider_net_enable_rxdmac(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001399 show_error = 0;
1400 break;
1401
1402 /* invalid descriptor */
1403 case SPIDER_NET_GDDINVDINT: /* fallthrough */
1404 case SPIDER_NET_GDCINVDINT: /* fallthrough */
1405 case SPIDER_NET_GDBINVDINT: /* fallthrough */
1406 case SPIDER_NET_GDAINVDINT:
1407 /* could happen when rx chain is full */
1408 spider_net_refill_rx_chain(card);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001409 spider_net_enable_rxdmac(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001410 show_error = 0;
1411 break;
1412
1413 /* case SPIDER_NET_GDTRSERINT: problem, print a message */
1414 /* case SPIDER_NET_GDDRSERINT: problem, print a message */
1415 /* case SPIDER_NET_GDCRSERINT: problem, print a message */
1416 /* case SPIDER_NET_GDBRSERINT: problem, print a message */
1417 /* case SPIDER_NET_GDARSERINT: problem, print a message */
1418 /* case SPIDER_NET_GDSERINT: problem, print a message */
1419 /* case SPIDER_NET_GDTPTERINT: problem, print a message */
1420 /* case SPIDER_NET_GDDPTERINT: problem, print a message */
1421 /* case SPIDER_NET_GDCPTERINT: problem, print a message */
1422 /* case SPIDER_NET_GDBPTERINT: problem, print a message */
1423 /* case SPIDER_NET_GDAPTERINT: problem, print a message */
1424 default:
1425 show_error = 1;
1426 break;
1427 }
1428
1429 /* check GHIINT2STS ************************************/
1430 if (error_reg2)
1431 for (i = 0; i < 32; i++)
1432 if (error_reg2 & (1<<i))
1433 switch (i)
1434 {
1435 /* there is nothing we can (want to) do at this time. Log a
1436 * message, we can switch on and off the specific values later on
1437 case SPIDER_NET_GPROPERINT:
1438 case SPIDER_NET_GMCTCRSNGINT:
1439 case SPIDER_NET_GMCTLCOLINT:
1440 case SPIDER_NET_GMCTTMOTINT:
1441 case SPIDER_NET_GMCRCAERINT:
1442 case SPIDER_NET_GMCRCALERINT:
1443 case SPIDER_NET_GMCRALNERINT:
1444 case SPIDER_NET_GMCROVRINT:
1445 case SPIDER_NET_GMCRRNTINT:
1446 case SPIDER_NET_GMCRRXERINT:
1447 case SPIDER_NET_GTITCSERINT:
1448 case SPIDER_NET_GTIFMTERINT:
1449 case SPIDER_NET_GTIPKTRVKINT:
1450 case SPIDER_NET_GTISPINGINT:
1451 case SPIDER_NET_GTISADNGINT:
1452 case SPIDER_NET_GTISPDNGINT:
1453 case SPIDER_NET_GRIFMTERINT:
1454 case SPIDER_NET_GRIPKTRVKINT:
1455 case SPIDER_NET_GRISPINGINT:
1456 case SPIDER_NET_GRISADNGINT:
1457 case SPIDER_NET_GRISPDNGINT:
1458 break;
1459 */
1460 default:
1461 break;
1462 }
1463
1464 if ((show_error) && (netif_msg_intr(card)))
Linas Vepstas98b90402006-10-10 16:08:42 -05001465 pr_err("Got error interrupt on %s, GHIINT0STS = 0x%08x, "
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001466 "GHIINT1STS = 0x%08x, GHIINT2STS = 0x%08x\n",
Linas Vepstas98b90402006-10-10 16:08:42 -05001467 card->netdev->name,
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001468 status_reg, error_reg1, error_reg2);
1469
1470 /* clear interrupt sources */
1471 spider_net_write_reg(card, SPIDER_NET_GHIINT1STS, error_reg1);
1472 spider_net_write_reg(card, SPIDER_NET_GHIINT2STS, error_reg2);
1473}
1474
1475/**
1476 * spider_net_interrupt - interrupt handler for spider_net
1477 * @irq: interupt number
1478 * @ptr: pointer to net_device
1479 * @regs: PU registers
1480 *
1481 * returns IRQ_HANDLED, if interrupt was for driver, or IRQ_NONE, if no
1482 * interrupt found raised by card.
1483 *
1484 * This is the interrupt handler, that turns off
1485 * interrupts for this device and makes the stack poll the driver
1486 */
1487static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001488spider_net_interrupt(int irq, void *ptr)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001489{
1490 struct net_device *netdev = ptr;
1491 struct spider_net_card *card = netdev_priv(netdev);
1492 u32 status_reg;
1493
1494 status_reg = spider_net_read_reg(card, SPIDER_NET_GHIINT0STS);
1495
1496 if (!status_reg)
1497 return IRQ_NONE;
1498
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001499 if (status_reg & SPIDER_NET_RXINT ) {
1500 spider_net_rx_irq_off(card);
1501 netif_rx_schedule(netdev);
1502 }
Linas Vepstas68a8c602006-10-10 16:13:05 -05001503 if (status_reg & SPIDER_NET_TXINT)
1504 netif_rx_schedule(netdev);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001505
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001506 if (status_reg & SPIDER_NET_ERRINT )
1507 spider_net_handle_error_irq(card, status_reg);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001508
1509 /* clear interrupt sources */
1510 spider_net_write_reg(card, SPIDER_NET_GHIINT0STS, status_reg);
1511
1512 return IRQ_HANDLED;
1513}
1514
1515#ifdef CONFIG_NET_POLL_CONTROLLER
1516/**
1517 * spider_net_poll_controller - artificial interrupt for netconsole etc.
1518 * @netdev: interface device structure
1519 *
1520 * see Documentation/networking/netconsole.txt
1521 */
1522static void
1523spider_net_poll_controller(struct net_device *netdev)
1524{
1525 disable_irq(netdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01001526 spider_net_interrupt(netdev->irq, netdev);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001527 enable_irq(netdev->irq);
1528}
1529#endif /* CONFIG_NET_POLL_CONTROLLER */
1530
1531/**
1532 * spider_net_init_card - initializes the card
1533 * @card: card structure
1534 *
1535 * spider_net_init_card initializes the card so that other registers can
1536 * be used
1537 */
1538static void
1539spider_net_init_card(struct spider_net_card *card)
1540{
1541 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1542 SPIDER_NET_CKRCTRL_STOP_VALUE);
1543
1544 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1545 SPIDER_NET_CKRCTRL_RUN_VALUE);
1546}
1547
1548/**
1549 * spider_net_enable_card - enables the card by setting all kinds of regs
1550 * @card: card structure
1551 *
1552 * spider_net_enable_card sets a lot of SMMIO registers to enable the device
1553 */
1554static void
1555spider_net_enable_card(struct spider_net_card *card)
1556{
1557 int i;
1558 /* the following array consists of (register),(value) pairs
1559 * that are set in this function. A register of 0 ends the list */
1560 u32 regs[][2] = {
1561 { SPIDER_NET_GRESUMINTNUM, 0 },
1562 { SPIDER_NET_GREINTNUM, 0 },
1563
1564 /* set interrupt frame number registers */
1565 /* clear the single DMA engine registers first */
1566 { SPIDER_NET_GFAFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1567 { SPIDER_NET_GFBFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1568 { SPIDER_NET_GFCFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1569 { SPIDER_NET_GFDFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1570 /* then set, what we really need */
1571 { SPIDER_NET_GFFRMNUM, SPIDER_NET_FRAMENUM_VALUE },
1572
1573 /* timer counter registers and stuff */
1574 { SPIDER_NET_GFREECNNUM, 0 },
1575 { SPIDER_NET_GONETIMENUM, 0 },
1576 { SPIDER_NET_GTOUTFRMNUM, 0 },
1577
1578 /* RX mode setting */
1579 { SPIDER_NET_GRXMDSET, SPIDER_NET_RXMODE_VALUE },
1580 /* TX mode setting */
1581 { SPIDER_NET_GTXMDSET, SPIDER_NET_TXMODE_VALUE },
1582 /* IPSEC mode setting */
1583 { SPIDER_NET_GIPSECINIT, SPIDER_NET_IPSECINIT_VALUE },
1584
1585 { SPIDER_NET_GFTRESTRT, SPIDER_NET_RESTART_VALUE },
1586
1587 { SPIDER_NET_GMRWOLCTRL, 0 },
Jens Osterkampb636d172006-05-04 05:59:56 -04001588 { SPIDER_NET_GTESTMD, 0x10000000 },
1589 { SPIDER_NET_GTTQMSK, 0x00400040 },
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001590
1591 { SPIDER_NET_GMACINTEN, 0 },
1592
1593 /* flow control stuff */
1594 { SPIDER_NET_GMACAPAUSE, SPIDER_NET_MACAPAUSE_VALUE },
1595 { SPIDER_NET_GMACTXPAUSE, SPIDER_NET_TXPAUSE_VALUE },
1596
1597 { SPIDER_NET_GMACBSTLMT, SPIDER_NET_BURSTLMT_VALUE },
1598 { 0, 0}
1599 };
1600
1601 i = 0;
1602 while (regs[i][0]) {
1603 spider_net_write_reg(card, regs[i][0], regs[i][1]);
1604 i++;
1605 }
1606
1607 /* clear unicast filter table entries 1 to 14 */
1608 for (i = 1; i <= 14; i++) {
1609 spider_net_write_reg(card,
1610 SPIDER_NET_GMRUAFILnR + i * 8,
1611 0x00080000);
1612 spider_net_write_reg(card,
1613 SPIDER_NET_GMRUAFILnR + i * 8 + 4,
1614 0x00000000);
1615 }
1616
1617 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R, 0x08080000);
1618
1619 spider_net_write_reg(card, SPIDER_NET_ECMODE, SPIDER_NET_ECMODE_VALUE);
1620
1621 /* set chain tail adress for RX chains and
1622 * enable DMA */
1623 spider_net_enable_rxchtails(card);
1624 spider_net_enable_rxdmac(card);
1625
1626 spider_net_write_reg(card, SPIDER_NET_GRXDMAEN, SPIDER_NET_WOL_VALUE);
1627
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001628 spider_net_write_reg(card, SPIDER_NET_GMACLENLMT,
1629 SPIDER_NET_LENLMT_VALUE);
1630 spider_net_write_reg(card, SPIDER_NET_GMACMODE,
1631 SPIDER_NET_MACMODE_VALUE);
1632 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD,
1633 SPIDER_NET_OPMODE_VALUE);
1634
1635 /* set interrupt mask registers */
1636 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK,
1637 SPIDER_NET_INT0_MASK_VALUE);
1638 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK,
1639 SPIDER_NET_INT1_MASK_VALUE);
1640 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK,
1641 SPIDER_NET_INT2_MASK_VALUE);
Jens Osterkampbdd01502006-07-13 11:54:08 +02001642
1643 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
Linas Vepstasded80282006-10-10 16:02:54 -05001644 SPIDER_NET_GDTBSTA | SPIDER_NET_GDTDCEIDIS);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001645}
1646
1647/**
1648 * spider_net_open - called upon ifonfig up
1649 * @netdev: interface device structure
1650 *
1651 * returns 0 on success, <0 on failure
1652 *
1653 * spider_net_open allocates all the descriptors and memory needed for
1654 * operation, sets up multicast list and enables interrupts
1655 */
1656int
1657spider_net_open(struct net_device *netdev)
1658{
1659 struct spider_net_card *card = netdev_priv(netdev);
1660 int result;
1661
1662 result = -ENOMEM;
Jim Lewisb68a60e2006-08-14 23:00:23 -07001663 if (spider_net_init_chain(card, &card->tx_chain, card->descr,
1664 PCI_DMA_TODEVICE, card->tx_desc))
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001665 goto alloc_tx_failed;
Linas Vepstasb21606a2006-10-10 16:09:40 -05001666
Linas Vepstas204e5fa2006-10-10 16:11:33 -05001667 card->low_watermark = NULL;
1668
Linas Vepstasb21606a2006-10-10 16:09:40 -05001669 /* rx_chain is after tx_chain, so offset is descr + tx_count */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001670 if (spider_net_init_chain(card, &card->rx_chain,
Linas Vepstasb21606a2006-10-10 16:09:40 -05001671 card->descr + card->tx_desc,
Jim Lewisb68a60e2006-08-14 23:00:23 -07001672 PCI_DMA_FROMDEVICE, card->rx_desc))
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001673 goto alloc_rx_failed;
1674
1675 /* allocate rx skbs */
1676 if (spider_net_alloc_rx_skbs(card))
1677 goto alloc_skbs_failed;
1678
1679 spider_net_set_multi(netdev);
1680
1681 /* further enhancement: setup hw vlan, if needed */
1682
1683 result = -EBUSY;
1684 if (request_irq(netdev->irq, spider_net_interrupt,
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07001685 IRQF_SHARED, netdev->name, netdev))
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001686 goto register_int_failed;
1687
1688 spider_net_enable_card(card);
1689
Jens Osterkamp543cec52005-09-06 19:30:54 -07001690 netif_start_queue(netdev);
1691 netif_carrier_on(netdev);
1692 netif_poll_enable(netdev);
1693
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001694 return 0;
1695
1696register_int_failed:
1697 spider_net_free_rx_chain_contents(card);
1698alloc_skbs_failed:
1699 spider_net_free_chain(card, &card->rx_chain);
1700alloc_rx_failed:
1701 spider_net_free_chain(card, &card->tx_chain);
1702alloc_tx_failed:
1703 return result;
1704}
1705
1706/**
1707 * spider_net_setup_phy - setup PHY
1708 * @card: card structure
1709 *
1710 * returns 0 on success, <0 on failure
1711 *
1712 * spider_net_setup_phy is used as part of spider_net_probe. Sets
1713 * the PHY to 1000 Mbps
1714 **/
1715static int
1716spider_net_setup_phy(struct spider_net_card *card)
1717{
1718 struct mii_phy *phy = &card->phy;
1719
1720 spider_net_write_reg(card, SPIDER_NET_GDTDMASEL,
1721 SPIDER_NET_DMASEL_VALUE);
1722 spider_net_write_reg(card, SPIDER_NET_GPCCTRL,
1723 SPIDER_NET_PHY_CTRL_VALUE);
1724 phy->mii_id = 1;
1725 phy->dev = card->netdev;
1726 phy->mdio_read = spider_net_read_phy;
1727 phy->mdio_write = spider_net_write_phy;
1728
1729 mii_phy_probe(phy, phy->mii_id);
1730
1731 if (phy->def->ops->setup_forced)
1732 phy->def->ops->setup_forced(phy, SPEED_1000, DUPLEX_FULL);
1733
Jens Osterkamp8ec93452006-05-04 05:59:41 -04001734 phy->def->ops->enable_fiber(phy);
Jens Osterkamp53abbf72005-09-16 08:55:33 +02001735
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001736 phy->def->ops->read_link(phy);
1737 pr_info("Found %s with %i Mbps, %s-duplex.\n", phy->def->name,
1738 phy->speed, phy->duplex==1 ? "Full" : "Half");
1739
1740 return 0;
1741}
1742
1743/**
1744 * spider_net_download_firmware - loads firmware into the adapter
1745 * @card: card structure
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001746 * @firmware_ptr: pointer to firmware data
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001747 *
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001748 * spider_net_download_firmware loads the firmware data into the
1749 * adapter. It assumes the length etc. to be allright.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001750 */
Arnd Bergmann0d3ea162006-01-12 17:16:41 -05001751static int
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001752spider_net_download_firmware(struct spider_net_card *card,
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001753 const void *firmware_ptr)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001754{
1755 int sequencer, i;
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001756 const u32 *fw_ptr = firmware_ptr;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001757
1758 /* stop sequencers */
1759 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1760 SPIDER_NET_STOP_SEQ_VALUE);
1761
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001762 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1763 sequencer++) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001764 spider_net_write_reg(card,
1765 SPIDER_NET_GSnPRGADR + sequencer * 8, 0);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001766 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001767 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1768 sequencer * 8, *fw_ptr);
1769 fw_ptr++;
1770 }
1771 }
1772
Arnd Bergmann0d3ea162006-01-12 17:16:41 -05001773 if (spider_net_read_reg(card, SPIDER_NET_GSINIT))
1774 return -EIO;
1775
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001776 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1777 SPIDER_NET_RUN_SEQ_VALUE);
Arnd Bergmann0d3ea162006-01-12 17:16:41 -05001778
1779 return 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001780}
1781
1782/**
1783 * spider_net_init_firmware - reads in firmware parts
1784 * @card: card structure
1785 *
1786 * Returns 0 on success, <0 on failure
1787 *
1788 * spider_net_init_firmware opens the sequencer firmware and does some basic
1789 * checks. This function opens and releases the firmware structure. A call
1790 * to download the firmware is performed before the release.
1791 *
1792 * Firmware format
1793 * ===============
1794 * spider_fw.bin is expected to be a file containing 6*1024*4 bytes, 4k being
1795 * the program for each sequencer. Use the command
1796 * tail -q -n +2 Seq_code1_0x088.txt Seq_code2_0x090.txt \
1797 * Seq_code3_0x098.txt Seq_code4_0x0A0.txt Seq_code5_0x0A8.txt \
1798 * Seq_code6_0x0B0.txt | xxd -r -p -c4 > spider_fw.bin
1799 *
1800 * to generate spider_fw.bin, if you have sequencer programs with something
1801 * like the following contents for each sequencer:
1802 * <ONE LINE COMMENT>
1803 * <FIRST 4-BYTES-WORD FOR SEQUENCER>
1804 * <SECOND 4-BYTES-WORD FOR SEQUENCER>
1805 * ...
1806 * <1024th 4-BYTES-WORD FOR SEQUENCER>
1807 */
1808static int
1809spider_net_init_firmware(struct spider_net_card *card)
1810{
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001811 struct firmware *firmware = NULL;
Arnd Bergmann030d6752006-01-12 17:16:42 -05001812 struct device_node *dn;
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001813 const u8 *fw_prop = NULL;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001814 int err = -ENOENT;
1815 int fw_size;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001816
Arnd Bergmann030d6752006-01-12 17:16:42 -05001817 if (request_firmware((const struct firmware **)&firmware,
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001818 SPIDER_NET_FIRMWARE_NAME, &card->pdev->dev) == 0) {
1819 if ( (firmware->size != SPIDER_NET_FIRMWARE_LEN) &&
1820 netif_msg_probe(card) ) {
1821 pr_err("Incorrect size of spidernet firmware in " \
1822 "filesystem. Looking in host firmware...\n");
1823 goto try_host_fw;
1824 }
1825 err = spider_net_download_firmware(card, firmware->data);
Arnd Bergmann030d6752006-01-12 17:16:42 -05001826
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001827 release_firmware(firmware);
1828 if (err)
1829 goto try_host_fw;
Arnd Bergmann030d6752006-01-12 17:16:42 -05001830
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001831 goto done;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001832 }
1833
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001834try_host_fw:
1835 dn = pci_device_to_OF_node(card->pdev);
1836 if (!dn)
1837 goto out_err;
1838
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001839 fw_prop = get_property(dn, "firmware", &fw_size);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001840 if (!fw_prop)
1841 goto out_err;
1842
1843 if ( (fw_size != SPIDER_NET_FIRMWARE_LEN) &&
1844 netif_msg_probe(card) ) {
1845 pr_err("Incorrect size of spidernet firmware in " \
1846 "host firmware\n");
1847 goto done;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001848 }
1849
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001850 err = spider_net_download_firmware(card, fw_prop);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001851
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001852done:
1853 return err;
1854out_err:
1855 if (netif_msg_probe(card))
1856 pr_err("Couldn't find spidernet firmware in filesystem " \
1857 "or host firmware\n");
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001858 return err;
1859}
1860
1861/**
1862 * spider_net_workaround_rxramfull - work around firmware bug
1863 * @card: card structure
1864 *
1865 * no return value
1866 **/
1867static void
1868spider_net_workaround_rxramfull(struct spider_net_card *card)
1869{
1870 int i, sequencer = 0;
1871
1872 /* cancel reset */
1873 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1874 SPIDER_NET_CKRCTRL_RUN_VALUE);
1875
1876 /* empty sequencer data */
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001877 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1878 sequencer++) {
Jens Osterkampee962a52006-07-13 11:54:23 +02001879 spider_net_write_reg(card, SPIDER_NET_GSnPRGADR +
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001880 sequencer * 8, 0x0);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001881 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001882 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1883 sequencer * 8, 0x0);
1884 }
1885 }
1886
1887 /* set sequencer operation */
1888 spider_net_write_reg(card, SPIDER_NET_GSINIT, 0x000000fe);
1889
1890 /* reset */
1891 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1892 SPIDER_NET_CKRCTRL_STOP_VALUE);
1893}
1894
1895/**
Jens Osterkampbdd01502006-07-13 11:54:08 +02001896 * spider_net_stop - called upon ifconfig down
1897 * @netdev: interface device structure
1898 *
1899 * always returns 0
1900 */
1901int
1902spider_net_stop(struct net_device *netdev)
1903{
1904 struct spider_net_card *card = netdev_priv(netdev);
1905
1906 tasklet_kill(&card->rxram_full_tl);
1907 netif_poll_disable(netdev);
1908 netif_carrier_off(netdev);
1909 netif_stop_queue(netdev);
1910 del_timer_sync(&card->tx_timer);
1911
1912 /* disable/mask all interrupts */
1913 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, 0);
1914 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK, 0);
1915 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK, 0);
1916
1917 /* free_irq(netdev->irq, netdev);*/
1918 free_irq(to_pci_dev(netdev->class_dev.dev)->irq, netdev);
1919
1920 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
1921 SPIDER_NET_DMA_TX_FEND_VALUE);
1922
1923 /* turn off DMA, force end */
1924 spider_net_disable_rxdmac(card);
1925
1926 /* release chains */
Linas Vepstas9cc7bf72006-10-10 16:14:29 -05001927 spider_net_release_tx_chain(card, 1);
Jens Osterkampbdd01502006-07-13 11:54:08 +02001928
1929 spider_net_free_chain(card, &card->tx_chain);
1930 spider_net_free_chain(card, &card->rx_chain);
1931
1932 return 0;
1933}
1934
1935/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001936 * spider_net_tx_timeout_task - task scheduled by the watchdog timeout
1937 * function (to be called not under interrupt status)
1938 * @data: data, is interface device structure
1939 *
1940 * called as task when tx hangs, resets interface (if interface is up)
1941 */
1942static void
1943spider_net_tx_timeout_task(void *data)
1944{
1945 struct net_device *netdev = data;
1946 struct spider_net_card *card = netdev_priv(netdev);
1947
1948 if (!(netdev->flags & IFF_UP))
1949 goto out;
1950
1951 netif_device_detach(netdev);
1952 spider_net_stop(netdev);
1953
1954 spider_net_workaround_rxramfull(card);
1955 spider_net_init_card(card);
1956
1957 if (spider_net_setup_phy(card))
1958 goto out;
1959 if (spider_net_init_firmware(card))
1960 goto out;
1961
1962 spider_net_open(netdev);
Jens Osterkampbdd01502006-07-13 11:54:08 +02001963 spider_net_kick_tx_dma(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001964 netif_device_attach(netdev);
1965
1966out:
1967 atomic_dec(&card->tx_timeout_task_counter);
1968}
1969
1970/**
1971 * spider_net_tx_timeout - called when the tx timeout watchdog kicks in.
1972 * @netdev: interface device structure
1973 *
1974 * called, if tx hangs. Schedules a task that resets the interface
1975 */
1976static void
1977spider_net_tx_timeout(struct net_device *netdev)
1978{
1979 struct spider_net_card *card;
1980
1981 card = netdev_priv(netdev);
1982 atomic_inc(&card->tx_timeout_task_counter);
1983 if (netdev->flags & IFF_UP)
1984 schedule_work(&card->tx_timeout_task);
1985 else
1986 atomic_dec(&card->tx_timeout_task_counter);
Jim Lewis9b6b0b82006-09-22 17:22:53 -07001987 card->spider_stats.tx_timeouts++;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001988}
1989
1990/**
1991 * spider_net_setup_netdev_ops - initialization of net_device operations
1992 * @netdev: net_device structure
1993 *
1994 * fills out function pointers in the net_device structure
1995 */
1996static void
1997spider_net_setup_netdev_ops(struct net_device *netdev)
1998{
1999 netdev->open = &spider_net_open;
2000 netdev->stop = &spider_net_stop;
2001 netdev->hard_start_xmit = &spider_net_xmit;
2002 netdev->get_stats = &spider_net_get_stats;
2003 netdev->set_multicast_list = &spider_net_set_multi;
2004 netdev->set_mac_address = &spider_net_set_mac;
2005 netdev->change_mtu = &spider_net_change_mtu;
2006 netdev->do_ioctl = &spider_net_do_ioctl;
2007 /* tx watchdog */
2008 netdev->tx_timeout = &spider_net_tx_timeout;
2009 netdev->watchdog_timeo = SPIDER_NET_WATCHDOG_TIMEOUT;
2010 /* NAPI */
2011 netdev->poll = &spider_net_poll;
2012 netdev->weight = SPIDER_NET_NAPI_WEIGHT;
2013 /* HW VLAN */
2014 netdev->vlan_rx_register = &spider_net_vlan_rx_reg;
2015 netdev->vlan_rx_add_vid = &spider_net_vlan_rx_add;
2016 netdev->vlan_rx_kill_vid = &spider_net_vlan_rx_kill;
2017#ifdef CONFIG_NET_POLL_CONTROLLER
2018 /* poll controller */
2019 netdev->poll_controller = &spider_net_poll_controller;
2020#endif /* CONFIG_NET_POLL_CONTROLLER */
2021 /* ethtool ops */
2022 netdev->ethtool_ops = &spider_net_ethtool_ops;
2023}
2024
2025/**
2026 * spider_net_setup_netdev - initialization of net_device
2027 * @card: card structure
2028 *
2029 * Returns 0 on success or <0 on failure
2030 *
2031 * spider_net_setup_netdev initializes the net_device structure
2032 **/
2033static int
2034spider_net_setup_netdev(struct spider_net_card *card)
2035{
2036 int result;
2037 struct net_device *netdev = card->netdev;
2038 struct device_node *dn;
2039 struct sockaddr addr;
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10002040 const u8 *mac;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002041
2042 SET_MODULE_OWNER(netdev);
2043 SET_NETDEV_DEV(netdev, &card->pdev->dev);
2044
2045 pci_set_drvdata(card->pdev, netdev);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05002046
Arnd Bergmann11f1a522006-01-12 17:16:44 -05002047 card->rxram_full_tl.data = (unsigned long) card;
2048 card->rxram_full_tl.func =
2049 (void (*)(unsigned long)) spider_net_handle_rxram_full;
2050 init_timer(&card->tx_timer);
2051 card->tx_timer.function =
2052 (void (*)(unsigned long)) spider_net_cleanup_tx_ring;
2053 card->tx_timer.data = (unsigned long) card;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002054 netdev->irq = card->pdev->irq;
2055
2056 card->options.rx_csum = SPIDER_NET_RX_CSUM_DEFAULT;
2057
Jim Lewisb68a60e2006-08-14 23:00:23 -07002058 card->tx_desc = tx_descriptors;
2059 card->rx_desc = rx_descriptors;
2060
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002061 spider_net_setup_netdev_ops(netdev);
2062
Jens Osterkampbdd01502006-07-13 11:54:08 +02002063 netdev->features = NETIF_F_HW_CSUM | NETIF_F_LLTX;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002064 /* some time: NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
2065 * NETIF_F_HW_VLAN_FILTER */
2066
2067 netdev->irq = card->pdev->irq;
2068
2069 dn = pci_device_to_OF_node(card->pdev);
Jens Osterkamp543cec52005-09-06 19:30:54 -07002070 if (!dn)
2071 return -EIO;
2072
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10002073 mac = get_property(dn, "local-mac-address", NULL);
Jens Osterkamp543cec52005-09-06 19:30:54 -07002074 if (!mac)
2075 return -EIO;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002076 memcpy(addr.sa_data, mac, ETH_ALEN);
2077
2078 result = spider_net_set_mac(netdev, &addr);
2079 if ((result) && (netif_msg_probe(card)))
2080 pr_err("Failed to set MAC address: %i\n", result);
2081
2082 result = register_netdev(netdev);
2083 if (result) {
2084 if (netif_msg_probe(card))
2085 pr_err("Couldn't register net_device: %i\n",
2086 result);
2087 return result;
2088 }
2089
2090 if (netif_msg_probe(card))
2091 pr_info("Initialized device %s.\n", netdev->name);
2092
2093 return 0;
2094}
2095
2096/**
2097 * spider_net_alloc_card - allocates net_device and card structure
2098 *
2099 * returns the card structure or NULL in case of errors
2100 *
2101 * the card and net_device structures are linked to each other
2102 */
2103static struct spider_net_card *
2104spider_net_alloc_card(void)
2105{
2106 struct net_device *netdev;
2107 struct spider_net_card *card;
2108 size_t alloc_size;
2109
2110 alloc_size = sizeof (*card) +
2111 sizeof (struct spider_net_descr) * rx_descriptors +
2112 sizeof (struct spider_net_descr) * tx_descriptors;
2113 netdev = alloc_etherdev(alloc_size);
2114 if (!netdev)
2115 return NULL;
2116
2117 card = netdev_priv(netdev);
2118 card->netdev = netdev;
2119 card->msg_enable = SPIDER_NET_DEFAULT_MSG;
2120 INIT_WORK(&card->tx_timeout_task, spider_net_tx_timeout_task, netdev);
2121 init_waitqueue_head(&card->waitq);
2122 atomic_set(&card->tx_timeout_task_counter, 0);
2123
2124 return card;
2125}
2126
2127/**
2128 * spider_net_undo_pci_setup - releases PCI ressources
2129 * @card: card structure
2130 *
2131 * spider_net_undo_pci_setup releases the mapped regions
2132 */
2133static void
2134spider_net_undo_pci_setup(struct spider_net_card *card)
2135{
2136 iounmap(card->regs);
2137 pci_release_regions(card->pdev);
2138}
2139
2140/**
2141 * spider_net_setup_pci_dev - sets up the device in terms of PCI operations
2142 * @card: card structure
2143 * @pdev: PCI device
2144 *
2145 * Returns the card structure or NULL if any errors occur
2146 *
2147 * spider_net_setup_pci_dev initializes pdev and together with the
2148 * functions called in spider_net_open configures the device so that
2149 * data can be transferred over it
2150 * The net_device structure is attached to the card structure, if the
2151 * function returns without error.
2152 **/
2153static struct spider_net_card *
2154spider_net_setup_pci_dev(struct pci_dev *pdev)
2155{
2156 struct spider_net_card *card;
2157 unsigned long mmio_start, mmio_len;
2158
2159 if (pci_enable_device(pdev)) {
2160 pr_err("Couldn't enable PCI device\n");
2161 return NULL;
2162 }
2163
2164 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
2165 pr_err("Couldn't find proper PCI device base address.\n");
2166 goto out_disable_dev;
2167 }
2168
2169 if (pci_request_regions(pdev, spider_net_driver_name)) {
2170 pr_err("Couldn't obtain PCI resources, aborting.\n");
2171 goto out_disable_dev;
2172 }
2173
2174 pci_set_master(pdev);
2175
2176 card = spider_net_alloc_card();
2177 if (!card) {
2178 pr_err("Couldn't allocate net_device structure, "
2179 "aborting.\n");
2180 goto out_release_regions;
2181 }
2182 card->pdev = pdev;
2183
2184 /* fetch base address and length of first resource */
2185 mmio_start = pci_resource_start(pdev, 0);
2186 mmio_len = pci_resource_len(pdev, 0);
2187
2188 card->netdev->mem_start = mmio_start;
2189 card->netdev->mem_end = mmio_start + mmio_len;
2190 card->regs = ioremap(mmio_start, mmio_len);
2191
2192 if (!card->regs) {
2193 pr_err("Couldn't obtain PCI resources, aborting.\n");
2194 goto out_release_regions;
2195 }
2196
2197 return card;
2198
2199out_release_regions:
2200 pci_release_regions(pdev);
2201out_disable_dev:
2202 pci_disable_device(pdev);
2203 pci_set_drvdata(pdev, NULL);
2204 return NULL;
2205}
2206
2207/**
2208 * spider_net_probe - initialization of a device
2209 * @pdev: PCI device
2210 * @ent: entry in the device id list
2211 *
2212 * Returns 0 on success, <0 on failure
2213 *
2214 * spider_net_probe initializes pdev and registers a net_device
2215 * structure for it. After that, the device can be ifconfig'ed up
2216 **/
2217static int __devinit
2218spider_net_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2219{
2220 int err = -EIO;
2221 struct spider_net_card *card;
2222
2223 card = spider_net_setup_pci_dev(pdev);
2224 if (!card)
2225 goto out;
2226
2227 spider_net_workaround_rxramfull(card);
2228 spider_net_init_card(card);
2229
2230 err = spider_net_setup_phy(card);
2231 if (err)
2232 goto out_undo_pci;
2233
2234 err = spider_net_init_firmware(card);
2235 if (err)
2236 goto out_undo_pci;
2237
2238 err = spider_net_setup_netdev(card);
2239 if (err)
2240 goto out_undo_pci;
2241
2242 return 0;
2243
2244out_undo_pci:
2245 spider_net_undo_pci_setup(card);
2246 free_netdev(card->netdev);
2247out:
2248 return err;
2249}
2250
2251/**
2252 * spider_net_remove - removal of a device
2253 * @pdev: PCI device
2254 *
2255 * Returns 0 on success, <0 on failure
2256 *
2257 * spider_net_remove is called to remove the device and unregisters the
2258 * net_device
2259 **/
2260static void __devexit
2261spider_net_remove(struct pci_dev *pdev)
2262{
2263 struct net_device *netdev;
2264 struct spider_net_card *card;
2265
2266 netdev = pci_get_drvdata(pdev);
2267 card = netdev_priv(netdev);
2268
2269 wait_event(card->waitq,
2270 atomic_read(&card->tx_timeout_task_counter) == 0);
2271
2272 unregister_netdev(netdev);
Jens Osterkamp543cec52005-09-06 19:30:54 -07002273
2274 /* switch off card */
2275 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2276 SPIDER_NET_CKRCTRL_STOP_VALUE);
2277 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2278 SPIDER_NET_CKRCTRL_RUN_VALUE);
2279
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002280 spider_net_undo_pci_setup(card);
2281 free_netdev(netdev);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002282}
2283
2284static struct pci_driver spider_net_driver = {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002285 .name = spider_net_driver_name,
2286 .id_table = spider_net_pci_tbl,
2287 .probe = spider_net_probe,
2288 .remove = __devexit_p(spider_net_remove)
2289};
2290
2291/**
2292 * spider_net_init - init function when the driver is loaded
2293 *
2294 * spider_net_init registers the device driver
2295 */
2296static int __init spider_net_init(void)
2297{
Linas Vepstas90f10842006-10-10 15:56:04 -05002298 printk(KERN_INFO "Spidernet version %s.\n", VERSION);
2299
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002300 if (rx_descriptors < SPIDER_NET_RX_DESCRIPTORS_MIN) {
2301 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MIN;
2302 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2303 }
2304 if (rx_descriptors > SPIDER_NET_RX_DESCRIPTORS_MAX) {
2305 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MAX;
2306 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2307 }
2308 if (tx_descriptors < SPIDER_NET_TX_DESCRIPTORS_MIN) {
2309 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MIN;
2310 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2311 }
2312 if (tx_descriptors > SPIDER_NET_TX_DESCRIPTORS_MAX) {
2313 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MAX;
2314 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2315 }
2316
2317 return pci_register_driver(&spider_net_driver);
2318}
2319
2320/**
2321 * spider_net_cleanup - exit function when driver is unloaded
2322 *
2323 * spider_net_cleanup unregisters the device driver
2324 */
2325static void __exit spider_net_cleanup(void)
2326{
2327 pci_unregister_driver(&spider_net_driver);
2328}
2329
2330module_init(spider_net_init);
2331module_exit(spider_net_cleanup);