blob: 418138dd6c687452afe9636bafb87c0658a69712 [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,
Linas Vepstas348bc2a2006-10-10 16:21:10 -0500304 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,
Linas Vepstas348bc2a2006-10-10 16:21:10 -0500319 PCI_DMA_BIDIRECTIONAL);
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
Jens Osterkampbdd01502006-07-13 11:54:08 +0200333 spin_lock_init(&chain->lock);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700334 chain->head = start_descr;
335 chain->tail = start_descr;
336
337 return 0;
338
339iommu_error:
340 descr = start_descr;
341 for (i=0; i < no; i++, descr++)
342 if (descr->bus_addr)
343 pci_unmap_single(card->pdev, descr->bus_addr,
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500344 SPIDER_NET_DESCR_SIZE,
Linas Vepstas348bc2a2006-10-10 16:21:10 -0500345 PCI_DMA_BIDIRECTIONAL);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700346 return -ENOMEM;
347}
348
349/**
350 * spider_net_free_rx_chain_contents - frees descr contents in rx chain
351 * @card: card structure
352 *
353 * returns 0 on success, <0 on failure
354 */
355static void
356spider_net_free_rx_chain_contents(struct spider_net_card *card)
357{
358 struct spider_net_descr *descr;
359
360 descr = card->rx_chain.head;
Linas Vepstas64751912006-10-10 16:22:29 -0500361 do {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700362 if (descr->skb) {
363 dev_kfree_skb(descr->skb);
364 pci_unmap_single(card->pdev, descr->buf_addr,
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500365 SPIDER_NET_MAX_FRAME,
Linas Vepstas348bc2a2006-10-10 16:21:10 -0500366 PCI_DMA_BIDIRECTIONAL);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700367 }
368 descr = descr->next;
Linas Vepstas64751912006-10-10 16:22:29 -0500369 } while (descr != card->rx_chain.head);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700370}
371
372/**
373 * spider_net_prepare_rx_descr - reinitializes a rx descriptor
374 * @card: card structure
375 * @descr: descriptor to re-init
376 *
377 * return 0 on succes, <0 on failure
378 *
379 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
380 * Activate the descriptor state-wise
381 */
382static int
383spider_net_prepare_rx_descr(struct spider_net_card *card,
384 struct spider_net_descr *descr)
385{
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500386 dma_addr_t buf;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700387 int error = 0;
388 int offset;
389 int bufsize;
390
391 /* we need to round up the buffer size to a multiple of 128 */
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500392 bufsize = (SPIDER_NET_MAX_FRAME + SPIDER_NET_RXBUF_ALIGN - 1) &
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700393 (~(SPIDER_NET_RXBUF_ALIGN - 1));
394
395 /* and we need to have it 128 byte aligned, therefore we allocate a
396 * bit more */
397 /* allocate an skb */
398 descr->skb = dev_alloc_skb(bufsize + SPIDER_NET_RXBUF_ALIGN - 1);
399 if (!descr->skb) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500400 if (netif_msg_rx_err(card) && net_ratelimit())
401 pr_err("Not enough memory to allocate rx buffer\n");
Jim Lewis9b6b0b82006-09-22 17:22:53 -0700402 card->spider_stats.alloc_rx_skb_error++;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700403 return -ENOMEM;
404 }
405 descr->buf_size = bufsize;
406 descr->result_size = 0;
407 descr->valid_size = 0;
408 descr->data_status = 0;
409 descr->data_error = 0;
410
411 offset = ((unsigned long)descr->skb->data) &
412 (SPIDER_NET_RXBUF_ALIGN - 1);
413 if (offset)
414 skb_reserve(descr->skb, SPIDER_NET_RXBUF_ALIGN - offset);
415 /* io-mmu-map the skb */
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500416 buf = pci_map_single(card->pdev, descr->skb->data,
Jens Osterkampbdd01502006-07-13 11:54:08 +0200417 SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500418 descr->buf_addr = buf;
Olof Johanssond4b0a4c2006-09-27 14:02:26 -0500419 if (pci_dma_mapping_error(buf)) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700420 dev_kfree_skb_any(descr->skb);
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500421 if (netif_msg_rx_err(card) && net_ratelimit())
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700422 pr_err("Could not iommu-map rx buffer\n");
Jim Lewis9b6b0b82006-09-22 17:22:53 -0700423 card->spider_stats.rx_iommu_map_error++;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200424 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700425 } else {
Jens Osterkampbdd01502006-07-13 11:54:08 +0200426 descr->dmac_cmd_status = SPIDER_NET_DESCR_CARDOWNED |
427 SPIDER_NET_DMAC_NOINTR_COMPLETE;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700428 }
429
430 return error;
431}
432
433/**
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500434 * spider_net_enable_rxchtails - sets RX dmac chain tail addresses
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700435 * @card: card structure
436 *
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500437 * spider_net_enable_rxchtails sets the RX DMAC chain tail adresses in the
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700438 * chip by writing to the appropriate register. DMA is enabled in
439 * spider_net_enable_rxdmac.
440 */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200441static inline void
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700442spider_net_enable_rxchtails(struct spider_net_card *card)
443{
444 /* assume chain is aligned correctly */
445 spider_net_write_reg(card, SPIDER_NET_GDADCHA ,
446 card->rx_chain.tail->bus_addr);
447}
448
449/**
450 * spider_net_enable_rxdmac - enables a receive DMA controller
451 * @card: card structure
452 *
453 * spider_net_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
454 * in the GDADMACCNTR register
455 */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200456static inline void
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700457spider_net_enable_rxdmac(struct spider_net_card *card)
458{
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500459 wmb();
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700460 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
461 SPIDER_NET_DMA_RX_VALUE);
462}
463
464/**
465 * spider_net_refill_rx_chain - refills descriptors/skbs in the rx chains
466 * @card: card structure
467 *
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500468 * refills descriptors in the rx chain: allocates skbs and iommu-maps them.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700469 */
470static void
471spider_net_refill_rx_chain(struct spider_net_card *card)
472{
Jens Osterkampbdd01502006-07-13 11:54:08 +0200473 struct spider_net_descr_chain *chain = &card->rx_chain;
474 unsigned long flags;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700475
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500476 /* one context doing the refill (and a second context seeing that
477 * and omitting it) is ok. If called by NAPI, we'll be called again
478 * as spider_net_decode_one_descr is called several times. If some
479 * interrupt calls us, the NAPI is about to clean up anyway. */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200480 if (!spin_trylock_irqsave(&chain->lock, flags))
481 return;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700482
Jens Osterkampbdd01502006-07-13 11:54:08 +0200483 while (spider_net_get_descr_status(chain->head) ==
484 SPIDER_NET_DESCR_NOT_IN_USE) {
485 if (spider_net_prepare_rx_descr(card, chain->head))
486 break;
487 chain->head = chain->head->next;
488 }
489
490 spin_unlock_irqrestore(&chain->lock, flags);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700491}
492
493/**
494 * spider_net_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
495 * @card: card structure
496 *
497 * returns 0 on success, <0 on failure
498 */
499static int
500spider_net_alloc_rx_skbs(struct spider_net_card *card)
501{
502 int result;
503 struct spider_net_descr_chain *chain;
504
505 result = -ENOMEM;
506
507 chain = &card->rx_chain;
508 /* put at least one buffer into the chain. if this fails,
509 * we've got a problem. if not, spider_net_refill_rx_chain
510 * will do the rest at the end of this function */
511 if (spider_net_prepare_rx_descr(card, chain->head))
512 goto error;
513 else
514 chain->head = chain->head->next;
515
516 /* this will allocate the rest of the rx buffers; if not, it's
517 * business as usual later on */
518 spider_net_refill_rx_chain(card);
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500519 spider_net_enable_rxdmac(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700520 return 0;
521
522error:
523 spider_net_free_rx_chain_contents(card);
524 return result;
525}
526
527/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700528 * spider_net_get_multicast_hash - generates hash for multicast filter table
529 * @addr: multicast address
530 *
531 * returns the hash value.
532 *
533 * spider_net_get_multicast_hash calculates a hash value for a given multicast
534 * address, that is used to set the multicast filter tables
535 */
536static u8
537spider_net_get_multicast_hash(struct net_device *netdev, __u8 *addr)
538{
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700539 u32 crc;
540 u8 hash;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500541 char addr_for_crc[ETH_ALEN] = { 0, };
542 int i, bit;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700543
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500544 for (i = 0; i < ETH_ALEN * 8; i++) {
545 bit = (addr[i / 8] >> (i % 8)) & 1;
546 addr_for_crc[ETH_ALEN - 1 - i / 8] += bit << (7 - (i % 8));
547 }
548
549 crc = crc32_be(~0, addr_for_crc, netdev->addr_len);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700550
551 hash = (crc >> 27);
552 hash <<= 3;
553 hash |= crc & 7;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500554 hash &= 0xff;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700555
556 return hash;
557}
558
559/**
560 * spider_net_set_multi - sets multicast addresses and promisc flags
561 * @netdev: interface device structure
562 *
563 * spider_net_set_multi configures multicast addresses as needed for the
564 * netdev interface. It also sets up multicast, allmulti and promisc
565 * flags appropriately
566 */
567static void
568spider_net_set_multi(struct net_device *netdev)
569{
570 struct dev_mc_list *mc;
571 u8 hash;
572 int i;
573 u32 reg;
574 struct spider_net_card *card = netdev_priv(netdev);
575 unsigned long bitmask[SPIDER_NET_MULTICAST_HASHES / BITS_PER_LONG] =
576 {0, };
577
578 spider_net_set_promisc(card);
579
580 if (netdev->flags & IFF_ALLMULTI) {
581 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES; i++) {
582 set_bit(i, bitmask);
583 }
584 goto write_hash;
585 }
586
587 /* well, we know, what the broadcast hash value is: it's xfd
588 hash = spider_net_get_multicast_hash(netdev, netdev->broadcast); */
589 set_bit(0xfd, bitmask);
590
591 for (mc = netdev->mc_list; mc; mc = mc->next) {
592 hash = spider_net_get_multicast_hash(netdev, mc->dmi_addr);
593 set_bit(hash, bitmask);
594 }
595
596write_hash:
597 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES / 4; i++) {
598 reg = 0;
599 if (test_bit(i * 4, bitmask))
600 reg += 0x08;
601 reg <<= 8;
602 if (test_bit(i * 4 + 1, bitmask))
603 reg += 0x08;
604 reg <<= 8;
605 if (test_bit(i * 4 + 2, bitmask))
606 reg += 0x08;
607 reg <<= 8;
608 if (test_bit(i * 4 + 3, bitmask))
609 reg += 0x08;
610
611 spider_net_write_reg(card, SPIDER_NET_GMRMHFILnR + i * 4, reg);
612 }
613}
614
615/**
616 * spider_net_disable_rxdmac - disables the receive DMA controller
617 * @card: card structure
618 *
619 * spider_net_disable_rxdmac terminates processing on the DMA controller by
620 * turing off DMA and issueing a force end
621 */
622static void
623spider_net_disable_rxdmac(struct spider_net_card *card)
624{
625 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
626 SPIDER_NET_DMA_RX_FEND_VALUE);
627}
628
629/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700630 * spider_net_prepare_tx_descr - fill tx descriptor with skb data
631 * @card: card structure
632 * @descr: descriptor structure to fill out
633 * @skb: packet to use
634 *
635 * returns 0 on success, <0 on failure.
636 *
637 * fills out the descriptor structure with skb data and len. Copies data,
638 * if needed (32bit DMA!)
639 */
640static int
641spider_net_prepare_tx_descr(struct spider_net_card *card,
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700642 struct sk_buff *skb)
643{
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500644 struct spider_net_descr *descr;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500645 dma_addr_t buf;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500646 unsigned long flags;
Linas Vepstas808999c2006-10-10 16:01:00 -0500647 int length;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500648
Linas Vepstas808999c2006-10-10 16:01:00 -0500649 length = skb->len;
650 if (length < ETH_ZLEN) {
651 if (skb_pad(skb, ETH_ZLEN-length))
652 return 0;
653 length = ETH_ZLEN;
654 }
655
656 buf = pci_map_single(card->pdev, skb->data, length, PCI_DMA_TODEVICE);
Olof Johanssond4b0a4c2006-09-27 14:02:26 -0500657 if (pci_dma_mapping_error(buf)) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500658 if (netif_msg_tx_err(card) && net_ratelimit())
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700659 pr_err("could not iommu-map packet (%p, %i). "
Linas Vepstas808999c2006-10-10 16:01:00 -0500660 "Dropping packet\n", skb->data, length);
Jim Lewis9b6b0b82006-09-22 17:22:53 -0700661 card->spider_stats.tx_iommu_map_error++;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700662 return -ENOMEM;
663 }
664
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500665 spin_lock_irqsave(&card->tx_chain.lock, flags);
666 descr = card->tx_chain.head;
667 card->tx_chain.head = descr->next;
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;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500677 spin_unlock_irqrestore(&card->tx_chain.lock, flags);
678
Jens Osterkampbdd01502006-07-13 11:54:08 +0200679 if (skb->protocol == htons(ETH_P_IP))
680 switch (skb->nh.iph->protocol) {
681 case IPPROTO_TCP:
682 descr->dmac_cmd_status |= SPIDER_NET_DMAC_TCP;
683 break;
684 case IPPROTO_UDP:
685 descr->dmac_cmd_status |= SPIDER_NET_DMAC_UDP;
686 break;
687 }
688
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500689 /* Chain the bus address, so that the DMA engine finds this descr. */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200690 descr->prev->next_descr_addr = descr->bus_addr;
691
Linas Vepstas917a5b82006-10-10 16:01:51 -0500692 card->netdev->trans_start = jiffies; /* set netdev watchdog timer */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200693 return 0;
694}
695
Linas Vepstasa664ccf2006-10-10 16:18:18 -0500696static int
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500697spider_net_set_low_watermark(struct spider_net_card *card)
698{
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500699 unsigned long flags;
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500700 int status;
701 int cnt=0;
702 int i;
703 struct spider_net_descr *descr = card->tx_chain.tail;
704
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500705 /* Measure the length of the queue. Measurement does not
706 * need to be precise -- does not need a lock. */
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500707 while (descr != card->tx_chain.head) {
708 status = descr->dmac_cmd_status & SPIDER_NET_DESCR_NOT_IN_USE;
709 if (status == SPIDER_NET_DESCR_NOT_IN_USE)
710 break;
711 descr = descr->next;
712 cnt++;
713 }
714
715 /* If TX queue is short, don't even bother with interrupts */
Linas Vepstas66c09712006-10-10 16:19:34 -0500716 if (cnt < card->num_tx_desc/4)
Linas Vepstasa664ccf2006-10-10 16:18:18 -0500717 return cnt;
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500718
719 /* Set low-watermark 3/4th's of the way into the queue. */
720 descr = card->tx_chain.tail;
721 cnt = (cnt*3)/4;
722 for (i=0;i<cnt; i++)
723 descr = descr->next;
724
725 /* Set the new watermark, clear the old watermark */
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500726 spin_lock_irqsave(&card->tx_chain.lock, flags);
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500727 descr->dmac_cmd_status |= SPIDER_NET_DESCR_TXDESFLG;
728 if (card->low_watermark && card->low_watermark != descr)
729 card->low_watermark->dmac_cmd_status =
730 card->low_watermark->dmac_cmd_status & ~SPIDER_NET_DESCR_TXDESFLG;
731 card->low_watermark = descr;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500732 spin_unlock_irqrestore(&card->tx_chain.lock, flags);
Linas Vepstasa664ccf2006-10-10 16:18:18 -0500733 return cnt;
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500734}
735
Jens Osterkampbdd01502006-07-13 11:54:08 +0200736/**
737 * spider_net_release_tx_chain - processes sent tx descriptors
738 * @card: adapter structure
739 * @brutal: if set, don't care about whether descriptor seems to be in use
740 *
741 * returns 0 if the tx ring is empty, otherwise 1.
742 *
743 * spider_net_release_tx_chain releases the tx descriptors that spider has
744 * finished with (if non-brutal) or simply release tx descriptors (if brutal).
745 * If some other context is calling this function, we return 1 so that we're
746 * scheduled again (if we were scheduled) and will not loose initiative.
747 */
748static int
749spider_net_release_tx_chain(struct spider_net_card *card, int brutal)
750{
751 struct spider_net_descr_chain *chain = &card->tx_chain;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500752 struct spider_net_descr *descr;
753 struct sk_buff *skb;
754 u32 buf_addr;
755 unsigned long flags;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200756 int status;
757
Jens Osterkampbdd01502006-07-13 11:54:08 +0200758 while (chain->tail != chain->head) {
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500759 spin_lock_irqsave(&chain->lock, flags);
760 descr = chain->tail;
761
762 status = spider_net_get_descr_status(descr);
Jens Osterkampbdd01502006-07-13 11:54:08 +0200763 switch (status) {
764 case SPIDER_NET_DESCR_COMPLETE:
765 card->netdev_stats.tx_packets++;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500766 card->netdev_stats.tx_bytes += descr->skb->len;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200767 break;
768
769 case SPIDER_NET_DESCR_CARDOWNED:
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500770 if (!brutal) {
771 spin_unlock_irqrestore(&chain->lock, flags);
Jens Osterkampbdd01502006-07-13 11:54:08 +0200772 return 1;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500773 }
774
Jens Osterkampbdd01502006-07-13 11:54:08 +0200775 /* fallthrough, if we release the descriptors
776 * brutally (then we don't care about
777 * SPIDER_NET_DESCR_CARDOWNED) */
778
779 case SPIDER_NET_DESCR_RESPONSE_ERROR:
780 case SPIDER_NET_DESCR_PROTECTION_ERROR:
781 case SPIDER_NET_DESCR_FORCE_END:
782 if (netif_msg_tx_err(card))
783 pr_err("%s: forcing end of tx descriptor "
784 "with status x%02x\n",
785 card->netdev->name, status);
786 card->netdev_stats.tx_errors++;
787 break;
788
789 default:
790 card->netdev_stats.tx_dropped++;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500791 if (!brutal) {
792 spin_unlock_irqrestore(&chain->lock, flags);
Linas Vepstasc3fee4c2006-10-10 16:00:04 -0500793 return 1;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500794 }
Jens Osterkampbdd01502006-07-13 11:54:08 +0200795 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700796
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500797 chain->tail = descr->next;
798 descr->dmac_cmd_status |= SPIDER_NET_DESCR_NOT_IN_USE;
799 skb = descr->skb;
800 buf_addr = descr->buf_addr;
801 spin_unlock_irqrestore(&chain->lock, flags);
802
803 /* unmap the skb */
804 if (skb) {
805 int len = skb->len < ETH_ZLEN ? ETH_ZLEN : skb->len;
806 pci_unmap_single(card->pdev, buf_addr, len, PCI_DMA_TODEVICE);
807 dev_kfree_skb(skb);
808 }
809 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700810 return 0;
811}
812
813/**
814 * spider_net_kick_tx_dma - enables TX DMA processing
815 * @card: card structure
816 * @descr: descriptor address to enable TX processing at
817 *
Linas Vepstasa664ccf2006-10-10 16:18:18 -0500818 * This routine will start the transmit DMA running if
819 * it is not already running. This routine ned only be
820 * called when queueing a new packet to an empty tx queue.
821 * Writes the current tx chain head as start address
822 * of the tx descriptor chain and enables the transmission
823 * DMA engine.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700824 */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200825static inline void
826spider_net_kick_tx_dma(struct spider_net_card *card)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700827{
Jens Osterkampbdd01502006-07-13 11:54:08 +0200828 struct spider_net_descr *descr;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700829
Jens Osterkampbdd01502006-07-13 11:54:08 +0200830 if (spider_net_read_reg(card, SPIDER_NET_GDTDMACCNTR) &
831 SPIDER_NET_TX_DMA_EN)
832 goto out;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700833
Jens Osterkampbdd01502006-07-13 11:54:08 +0200834 descr = card->tx_chain.tail;
835 for (;;) {
836 if (spider_net_get_descr_status(descr) ==
837 SPIDER_NET_DESCR_CARDOWNED) {
838 spider_net_write_reg(card, SPIDER_NET_GDTDCHA,
839 descr->bus_addr);
840 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
841 SPIDER_NET_DMA_TX_VALUE);
842 break;
843 }
844 if (descr == card->tx_chain.head)
845 break;
846 descr = descr->next;
847 }
848
849out:
850 mod_timer(&card->tx_timer, jiffies + SPIDER_NET_TX_TIMER);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700851}
852
853/**
854 * spider_net_xmit - transmits a frame over the device
855 * @skb: packet to send out
856 * @netdev: interface device structure
857 *
Jens Osterkampbdd01502006-07-13 11:54:08 +0200858 * returns 0 on success, !0 on failure
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700859 */
860static int
861spider_net_xmit(struct sk_buff *skb, struct net_device *netdev)
862{
Linas Vepstasa664ccf2006-10-10 16:18:18 -0500863 int cnt;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700864 struct spider_net_card *card = netdev_priv(netdev);
Jens Osterkampbdd01502006-07-13 11:54:08 +0200865 struct spider_net_descr_chain *chain = &card->tx_chain;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200866
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500867 spider_net_release_tx_chain(card, 0);
868
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500869 if ((chain->head->next == chain->tail->prev) ||
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500870 (spider_net_prepare_tx_descr(card, skb) != 0)) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500871
Jim Lewis9b6b0b82006-09-22 17:22:53 -0700872 card->netdev_stats.tx_dropped++;
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500873 netif_stop_queue(netdev);
874 return NETDEV_TX_BUSY;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200875 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700876
Linas Vepstasa664ccf2006-10-10 16:18:18 -0500877 cnt = spider_net_set_low_watermark(card);
878 if (cnt < 5)
879 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);
Linas Vepstas348bc2a2006-10-10 16:21:10 -05001660 struct spider_net_descr *descr;
1661 int i, result;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001662
1663 result = -ENOMEM;
Jim Lewisb68a60e2006-08-14 23:00:23 -07001664 if (spider_net_init_chain(card, &card->tx_chain, card->descr,
Linas Vepstas348bc2a2006-10-10 16:21:10 -05001665 card->num_tx_desc))
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001666 goto alloc_tx_failed;
Linas Vepstasb21606a2006-10-10 16:09:40 -05001667
Linas Vepstas204e5fa2006-10-10 16:11:33 -05001668 card->low_watermark = NULL;
1669
Linas Vepstasb21606a2006-10-10 16:09:40 -05001670 /* rx_chain is after tx_chain, so offset is descr + tx_count */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001671 if (spider_net_init_chain(card, &card->rx_chain,
Linas Vepstas348bc2a2006-10-10 16:21:10 -05001672 card->descr + card->num_tx_desc,
1673 card->num_rx_desc))
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001674 goto alloc_rx_failed;
1675
Linas Vepstas348bc2a2006-10-10 16:21:10 -05001676 descr = card->rx_chain.head;
1677 for (i=0; i < card->num_rx_desc; i++, descr++)
1678 descr->next_descr_addr = descr->next->bus_addr;
1679
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001680 /* allocate rx skbs */
1681 if (spider_net_alloc_rx_skbs(card))
1682 goto alloc_skbs_failed;
1683
1684 spider_net_set_multi(netdev);
1685
1686 /* further enhancement: setup hw vlan, if needed */
1687
1688 result = -EBUSY;
1689 if (request_irq(netdev->irq, spider_net_interrupt,
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07001690 IRQF_SHARED, netdev->name, netdev))
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001691 goto register_int_failed;
1692
1693 spider_net_enable_card(card);
1694
Jens Osterkamp543cec52005-09-06 19:30:54 -07001695 netif_start_queue(netdev);
1696 netif_carrier_on(netdev);
1697 netif_poll_enable(netdev);
1698
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001699 return 0;
1700
1701register_int_failed:
1702 spider_net_free_rx_chain_contents(card);
1703alloc_skbs_failed:
1704 spider_net_free_chain(card, &card->rx_chain);
1705alloc_rx_failed:
1706 spider_net_free_chain(card, &card->tx_chain);
1707alloc_tx_failed:
1708 return result;
1709}
1710
1711/**
1712 * spider_net_setup_phy - setup PHY
1713 * @card: card structure
1714 *
1715 * returns 0 on success, <0 on failure
1716 *
1717 * spider_net_setup_phy is used as part of spider_net_probe. Sets
1718 * the PHY to 1000 Mbps
1719 **/
1720static int
1721spider_net_setup_phy(struct spider_net_card *card)
1722{
1723 struct mii_phy *phy = &card->phy;
1724
1725 spider_net_write_reg(card, SPIDER_NET_GDTDMASEL,
1726 SPIDER_NET_DMASEL_VALUE);
1727 spider_net_write_reg(card, SPIDER_NET_GPCCTRL,
1728 SPIDER_NET_PHY_CTRL_VALUE);
1729 phy->mii_id = 1;
1730 phy->dev = card->netdev;
1731 phy->mdio_read = spider_net_read_phy;
1732 phy->mdio_write = spider_net_write_phy;
1733
1734 mii_phy_probe(phy, phy->mii_id);
1735
1736 if (phy->def->ops->setup_forced)
1737 phy->def->ops->setup_forced(phy, SPEED_1000, DUPLEX_FULL);
1738
Jens Osterkamp8ec93452006-05-04 05:59:41 -04001739 phy->def->ops->enable_fiber(phy);
Jens Osterkamp53abbf72005-09-16 08:55:33 +02001740
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001741 phy->def->ops->read_link(phy);
1742 pr_info("Found %s with %i Mbps, %s-duplex.\n", phy->def->name,
1743 phy->speed, phy->duplex==1 ? "Full" : "Half");
1744
1745 return 0;
1746}
1747
1748/**
1749 * spider_net_download_firmware - loads firmware into the adapter
1750 * @card: card structure
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001751 * @firmware_ptr: pointer to firmware data
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001752 *
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001753 * spider_net_download_firmware loads the firmware data into the
1754 * adapter. It assumes the length etc. to be allright.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001755 */
Arnd Bergmann0d3ea162006-01-12 17:16:41 -05001756static int
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001757spider_net_download_firmware(struct spider_net_card *card,
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001758 const void *firmware_ptr)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001759{
1760 int sequencer, i;
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001761 const u32 *fw_ptr = firmware_ptr;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001762
1763 /* stop sequencers */
1764 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1765 SPIDER_NET_STOP_SEQ_VALUE);
1766
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001767 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1768 sequencer++) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001769 spider_net_write_reg(card,
1770 SPIDER_NET_GSnPRGADR + sequencer * 8, 0);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001771 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001772 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1773 sequencer * 8, *fw_ptr);
1774 fw_ptr++;
1775 }
1776 }
1777
Arnd Bergmann0d3ea162006-01-12 17:16:41 -05001778 if (spider_net_read_reg(card, SPIDER_NET_GSINIT))
1779 return -EIO;
1780
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001781 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1782 SPIDER_NET_RUN_SEQ_VALUE);
Arnd Bergmann0d3ea162006-01-12 17:16:41 -05001783
1784 return 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001785}
1786
1787/**
1788 * spider_net_init_firmware - reads in firmware parts
1789 * @card: card structure
1790 *
1791 * Returns 0 on success, <0 on failure
1792 *
1793 * spider_net_init_firmware opens the sequencer firmware and does some basic
1794 * checks. This function opens and releases the firmware structure. A call
1795 * to download the firmware is performed before the release.
1796 *
1797 * Firmware format
1798 * ===============
1799 * spider_fw.bin is expected to be a file containing 6*1024*4 bytes, 4k being
1800 * the program for each sequencer. Use the command
1801 * tail -q -n +2 Seq_code1_0x088.txt Seq_code2_0x090.txt \
1802 * Seq_code3_0x098.txt Seq_code4_0x0A0.txt Seq_code5_0x0A8.txt \
1803 * Seq_code6_0x0B0.txt | xxd -r -p -c4 > spider_fw.bin
1804 *
1805 * to generate spider_fw.bin, if you have sequencer programs with something
1806 * like the following contents for each sequencer:
1807 * <ONE LINE COMMENT>
1808 * <FIRST 4-BYTES-WORD FOR SEQUENCER>
1809 * <SECOND 4-BYTES-WORD FOR SEQUENCER>
1810 * ...
1811 * <1024th 4-BYTES-WORD FOR SEQUENCER>
1812 */
1813static int
1814spider_net_init_firmware(struct spider_net_card *card)
1815{
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001816 struct firmware *firmware = NULL;
Arnd Bergmann030d6752006-01-12 17:16:42 -05001817 struct device_node *dn;
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001818 const u8 *fw_prop = NULL;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001819 int err = -ENOENT;
1820 int fw_size;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001821
Arnd Bergmann030d6752006-01-12 17:16:42 -05001822 if (request_firmware((const struct firmware **)&firmware,
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001823 SPIDER_NET_FIRMWARE_NAME, &card->pdev->dev) == 0) {
1824 if ( (firmware->size != SPIDER_NET_FIRMWARE_LEN) &&
1825 netif_msg_probe(card) ) {
1826 pr_err("Incorrect size of spidernet firmware in " \
1827 "filesystem. Looking in host firmware...\n");
1828 goto try_host_fw;
1829 }
1830 err = spider_net_download_firmware(card, firmware->data);
Arnd Bergmann030d6752006-01-12 17:16:42 -05001831
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001832 release_firmware(firmware);
1833 if (err)
1834 goto try_host_fw;
Arnd Bergmann030d6752006-01-12 17:16:42 -05001835
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001836 goto done;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001837 }
1838
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001839try_host_fw:
1840 dn = pci_device_to_OF_node(card->pdev);
1841 if (!dn)
1842 goto out_err;
1843
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001844 fw_prop = get_property(dn, "firmware", &fw_size);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001845 if (!fw_prop)
1846 goto out_err;
1847
1848 if ( (fw_size != SPIDER_NET_FIRMWARE_LEN) &&
1849 netif_msg_probe(card) ) {
1850 pr_err("Incorrect size of spidernet firmware in " \
1851 "host firmware\n");
1852 goto done;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001853 }
1854
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001855 err = spider_net_download_firmware(card, fw_prop);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001856
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001857done:
1858 return err;
1859out_err:
1860 if (netif_msg_probe(card))
1861 pr_err("Couldn't find spidernet firmware in filesystem " \
1862 "or host firmware\n");
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001863 return err;
1864}
1865
1866/**
1867 * spider_net_workaround_rxramfull - work around firmware bug
1868 * @card: card structure
1869 *
1870 * no return value
1871 **/
1872static void
1873spider_net_workaround_rxramfull(struct spider_net_card *card)
1874{
1875 int i, sequencer = 0;
1876
1877 /* cancel reset */
1878 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1879 SPIDER_NET_CKRCTRL_RUN_VALUE);
1880
1881 /* empty sequencer data */
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001882 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1883 sequencer++) {
Jens Osterkampee962a52006-07-13 11:54:23 +02001884 spider_net_write_reg(card, SPIDER_NET_GSnPRGADR +
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001885 sequencer * 8, 0x0);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001886 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001887 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1888 sequencer * 8, 0x0);
1889 }
1890 }
1891
1892 /* set sequencer operation */
1893 spider_net_write_reg(card, SPIDER_NET_GSINIT, 0x000000fe);
1894
1895 /* reset */
1896 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1897 SPIDER_NET_CKRCTRL_STOP_VALUE);
1898}
1899
1900/**
Jens Osterkampbdd01502006-07-13 11:54:08 +02001901 * spider_net_stop - called upon ifconfig down
1902 * @netdev: interface device structure
1903 *
1904 * always returns 0
1905 */
1906int
1907spider_net_stop(struct net_device *netdev)
1908{
1909 struct spider_net_card *card = netdev_priv(netdev);
1910
1911 tasklet_kill(&card->rxram_full_tl);
1912 netif_poll_disable(netdev);
1913 netif_carrier_off(netdev);
1914 netif_stop_queue(netdev);
1915 del_timer_sync(&card->tx_timer);
1916
1917 /* disable/mask all interrupts */
1918 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, 0);
1919 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK, 0);
1920 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK, 0);
1921
1922 /* free_irq(netdev->irq, netdev);*/
1923 free_irq(to_pci_dev(netdev->class_dev.dev)->irq, netdev);
1924
1925 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
1926 SPIDER_NET_DMA_TX_FEND_VALUE);
1927
1928 /* turn off DMA, force end */
1929 spider_net_disable_rxdmac(card);
1930
1931 /* release chains */
Linas Vepstas9cc7bf72006-10-10 16:14:29 -05001932 spider_net_release_tx_chain(card, 1);
Jens Osterkampbdd01502006-07-13 11:54:08 +02001933
1934 spider_net_free_chain(card, &card->tx_chain);
1935 spider_net_free_chain(card, &card->rx_chain);
1936
1937 return 0;
1938}
1939
1940/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001941 * spider_net_tx_timeout_task - task scheduled by the watchdog timeout
1942 * function (to be called not under interrupt status)
1943 * @data: data, is interface device structure
1944 *
1945 * called as task when tx hangs, resets interface (if interface is up)
1946 */
1947static void
1948spider_net_tx_timeout_task(void *data)
1949{
1950 struct net_device *netdev = data;
1951 struct spider_net_card *card = netdev_priv(netdev);
1952
1953 if (!(netdev->flags & IFF_UP))
1954 goto out;
1955
1956 netif_device_detach(netdev);
1957 spider_net_stop(netdev);
1958
1959 spider_net_workaround_rxramfull(card);
1960 spider_net_init_card(card);
1961
1962 if (spider_net_setup_phy(card))
1963 goto out;
1964 if (spider_net_init_firmware(card))
1965 goto out;
1966
1967 spider_net_open(netdev);
Jens Osterkampbdd01502006-07-13 11:54:08 +02001968 spider_net_kick_tx_dma(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001969 netif_device_attach(netdev);
1970
1971out:
1972 atomic_dec(&card->tx_timeout_task_counter);
1973}
1974
1975/**
1976 * spider_net_tx_timeout - called when the tx timeout watchdog kicks in.
1977 * @netdev: interface device structure
1978 *
1979 * called, if tx hangs. Schedules a task that resets the interface
1980 */
1981static void
1982spider_net_tx_timeout(struct net_device *netdev)
1983{
1984 struct spider_net_card *card;
1985
1986 card = netdev_priv(netdev);
1987 atomic_inc(&card->tx_timeout_task_counter);
1988 if (netdev->flags & IFF_UP)
1989 schedule_work(&card->tx_timeout_task);
1990 else
1991 atomic_dec(&card->tx_timeout_task_counter);
Jim Lewis9b6b0b82006-09-22 17:22:53 -07001992 card->spider_stats.tx_timeouts++;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001993}
1994
1995/**
1996 * spider_net_setup_netdev_ops - initialization of net_device operations
1997 * @netdev: net_device structure
1998 *
1999 * fills out function pointers in the net_device structure
2000 */
2001static void
2002spider_net_setup_netdev_ops(struct net_device *netdev)
2003{
2004 netdev->open = &spider_net_open;
2005 netdev->stop = &spider_net_stop;
2006 netdev->hard_start_xmit = &spider_net_xmit;
2007 netdev->get_stats = &spider_net_get_stats;
2008 netdev->set_multicast_list = &spider_net_set_multi;
2009 netdev->set_mac_address = &spider_net_set_mac;
2010 netdev->change_mtu = &spider_net_change_mtu;
2011 netdev->do_ioctl = &spider_net_do_ioctl;
2012 /* tx watchdog */
2013 netdev->tx_timeout = &spider_net_tx_timeout;
2014 netdev->watchdog_timeo = SPIDER_NET_WATCHDOG_TIMEOUT;
2015 /* NAPI */
2016 netdev->poll = &spider_net_poll;
2017 netdev->weight = SPIDER_NET_NAPI_WEIGHT;
2018 /* HW VLAN */
2019 netdev->vlan_rx_register = &spider_net_vlan_rx_reg;
2020 netdev->vlan_rx_add_vid = &spider_net_vlan_rx_add;
2021 netdev->vlan_rx_kill_vid = &spider_net_vlan_rx_kill;
2022#ifdef CONFIG_NET_POLL_CONTROLLER
2023 /* poll controller */
2024 netdev->poll_controller = &spider_net_poll_controller;
2025#endif /* CONFIG_NET_POLL_CONTROLLER */
2026 /* ethtool ops */
2027 netdev->ethtool_ops = &spider_net_ethtool_ops;
2028}
2029
2030/**
2031 * spider_net_setup_netdev - initialization of net_device
2032 * @card: card structure
2033 *
2034 * Returns 0 on success or <0 on failure
2035 *
2036 * spider_net_setup_netdev initializes the net_device structure
2037 **/
2038static int
2039spider_net_setup_netdev(struct spider_net_card *card)
2040{
2041 int result;
2042 struct net_device *netdev = card->netdev;
2043 struct device_node *dn;
2044 struct sockaddr addr;
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10002045 const u8 *mac;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002046
2047 SET_MODULE_OWNER(netdev);
2048 SET_NETDEV_DEV(netdev, &card->pdev->dev);
2049
2050 pci_set_drvdata(card->pdev, netdev);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05002051
Arnd Bergmann11f1a522006-01-12 17:16:44 -05002052 card->rxram_full_tl.data = (unsigned long) card;
2053 card->rxram_full_tl.func =
2054 (void (*)(unsigned long)) spider_net_handle_rxram_full;
2055 init_timer(&card->tx_timer);
2056 card->tx_timer.function =
2057 (void (*)(unsigned long)) spider_net_cleanup_tx_ring;
2058 card->tx_timer.data = (unsigned long) card;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002059 netdev->irq = card->pdev->irq;
2060
2061 card->options.rx_csum = SPIDER_NET_RX_CSUM_DEFAULT;
2062
Linas Vepstas66c09712006-10-10 16:19:34 -05002063 card->num_tx_desc = tx_descriptors;
2064 card->num_rx_desc = rx_descriptors;
Jim Lewisb68a60e2006-08-14 23:00:23 -07002065
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002066 spider_net_setup_netdev_ops(netdev);
2067
Jens Osterkampbdd01502006-07-13 11:54:08 +02002068 netdev->features = NETIF_F_HW_CSUM | NETIF_F_LLTX;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002069 /* some time: NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
2070 * NETIF_F_HW_VLAN_FILTER */
2071
2072 netdev->irq = card->pdev->irq;
2073
2074 dn = pci_device_to_OF_node(card->pdev);
Jens Osterkamp543cec52005-09-06 19:30:54 -07002075 if (!dn)
2076 return -EIO;
2077
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10002078 mac = get_property(dn, "local-mac-address", NULL);
Jens Osterkamp543cec52005-09-06 19:30:54 -07002079 if (!mac)
2080 return -EIO;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002081 memcpy(addr.sa_data, mac, ETH_ALEN);
2082
2083 result = spider_net_set_mac(netdev, &addr);
2084 if ((result) && (netif_msg_probe(card)))
2085 pr_err("Failed to set MAC address: %i\n", result);
2086
2087 result = register_netdev(netdev);
2088 if (result) {
2089 if (netif_msg_probe(card))
2090 pr_err("Couldn't register net_device: %i\n",
2091 result);
2092 return result;
2093 }
2094
2095 if (netif_msg_probe(card))
2096 pr_info("Initialized device %s.\n", netdev->name);
2097
2098 return 0;
2099}
2100
2101/**
2102 * spider_net_alloc_card - allocates net_device and card structure
2103 *
2104 * returns the card structure or NULL in case of errors
2105 *
2106 * the card and net_device structures are linked to each other
2107 */
2108static struct spider_net_card *
2109spider_net_alloc_card(void)
2110{
2111 struct net_device *netdev;
2112 struct spider_net_card *card;
2113 size_t alloc_size;
2114
2115 alloc_size = sizeof (*card) +
2116 sizeof (struct spider_net_descr) * rx_descriptors +
2117 sizeof (struct spider_net_descr) * tx_descriptors;
2118 netdev = alloc_etherdev(alloc_size);
2119 if (!netdev)
2120 return NULL;
2121
2122 card = netdev_priv(netdev);
2123 card->netdev = netdev;
2124 card->msg_enable = SPIDER_NET_DEFAULT_MSG;
2125 INIT_WORK(&card->tx_timeout_task, spider_net_tx_timeout_task, netdev);
2126 init_waitqueue_head(&card->waitq);
2127 atomic_set(&card->tx_timeout_task_counter, 0);
2128
2129 return card;
2130}
2131
2132/**
2133 * spider_net_undo_pci_setup - releases PCI ressources
2134 * @card: card structure
2135 *
2136 * spider_net_undo_pci_setup releases the mapped regions
2137 */
2138static void
2139spider_net_undo_pci_setup(struct spider_net_card *card)
2140{
2141 iounmap(card->regs);
2142 pci_release_regions(card->pdev);
2143}
2144
2145/**
2146 * spider_net_setup_pci_dev - sets up the device in terms of PCI operations
2147 * @card: card structure
2148 * @pdev: PCI device
2149 *
2150 * Returns the card structure or NULL if any errors occur
2151 *
2152 * spider_net_setup_pci_dev initializes pdev and together with the
2153 * functions called in spider_net_open configures the device so that
2154 * data can be transferred over it
2155 * The net_device structure is attached to the card structure, if the
2156 * function returns without error.
2157 **/
2158static struct spider_net_card *
2159spider_net_setup_pci_dev(struct pci_dev *pdev)
2160{
2161 struct spider_net_card *card;
2162 unsigned long mmio_start, mmio_len;
2163
2164 if (pci_enable_device(pdev)) {
2165 pr_err("Couldn't enable PCI device\n");
2166 return NULL;
2167 }
2168
2169 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
2170 pr_err("Couldn't find proper PCI device base address.\n");
2171 goto out_disable_dev;
2172 }
2173
2174 if (pci_request_regions(pdev, spider_net_driver_name)) {
2175 pr_err("Couldn't obtain PCI resources, aborting.\n");
2176 goto out_disable_dev;
2177 }
2178
2179 pci_set_master(pdev);
2180
2181 card = spider_net_alloc_card();
2182 if (!card) {
2183 pr_err("Couldn't allocate net_device structure, "
2184 "aborting.\n");
2185 goto out_release_regions;
2186 }
2187 card->pdev = pdev;
2188
2189 /* fetch base address and length of first resource */
2190 mmio_start = pci_resource_start(pdev, 0);
2191 mmio_len = pci_resource_len(pdev, 0);
2192
2193 card->netdev->mem_start = mmio_start;
2194 card->netdev->mem_end = mmio_start + mmio_len;
2195 card->regs = ioremap(mmio_start, mmio_len);
2196
2197 if (!card->regs) {
2198 pr_err("Couldn't obtain PCI resources, aborting.\n");
2199 goto out_release_regions;
2200 }
2201
2202 return card;
2203
2204out_release_regions:
2205 pci_release_regions(pdev);
2206out_disable_dev:
2207 pci_disable_device(pdev);
2208 pci_set_drvdata(pdev, NULL);
2209 return NULL;
2210}
2211
2212/**
2213 * spider_net_probe - initialization of a device
2214 * @pdev: PCI device
2215 * @ent: entry in the device id list
2216 *
2217 * Returns 0 on success, <0 on failure
2218 *
2219 * spider_net_probe initializes pdev and registers a net_device
2220 * structure for it. After that, the device can be ifconfig'ed up
2221 **/
2222static int __devinit
2223spider_net_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2224{
2225 int err = -EIO;
2226 struct spider_net_card *card;
2227
2228 card = spider_net_setup_pci_dev(pdev);
2229 if (!card)
2230 goto out;
2231
2232 spider_net_workaround_rxramfull(card);
2233 spider_net_init_card(card);
2234
2235 err = spider_net_setup_phy(card);
2236 if (err)
2237 goto out_undo_pci;
2238
2239 err = spider_net_init_firmware(card);
2240 if (err)
2241 goto out_undo_pci;
2242
2243 err = spider_net_setup_netdev(card);
2244 if (err)
2245 goto out_undo_pci;
2246
2247 return 0;
2248
2249out_undo_pci:
2250 spider_net_undo_pci_setup(card);
2251 free_netdev(card->netdev);
2252out:
2253 return err;
2254}
2255
2256/**
2257 * spider_net_remove - removal of a device
2258 * @pdev: PCI device
2259 *
2260 * Returns 0 on success, <0 on failure
2261 *
2262 * spider_net_remove is called to remove the device and unregisters the
2263 * net_device
2264 **/
2265static void __devexit
2266spider_net_remove(struct pci_dev *pdev)
2267{
2268 struct net_device *netdev;
2269 struct spider_net_card *card;
2270
2271 netdev = pci_get_drvdata(pdev);
2272 card = netdev_priv(netdev);
2273
2274 wait_event(card->waitq,
2275 atomic_read(&card->tx_timeout_task_counter) == 0);
2276
2277 unregister_netdev(netdev);
Jens Osterkamp543cec52005-09-06 19:30:54 -07002278
2279 /* switch off card */
2280 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2281 SPIDER_NET_CKRCTRL_STOP_VALUE);
2282 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2283 SPIDER_NET_CKRCTRL_RUN_VALUE);
2284
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002285 spider_net_undo_pci_setup(card);
2286 free_netdev(netdev);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002287}
2288
2289static struct pci_driver spider_net_driver = {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002290 .name = spider_net_driver_name,
2291 .id_table = spider_net_pci_tbl,
2292 .probe = spider_net_probe,
2293 .remove = __devexit_p(spider_net_remove)
2294};
2295
2296/**
2297 * spider_net_init - init function when the driver is loaded
2298 *
2299 * spider_net_init registers the device driver
2300 */
2301static int __init spider_net_init(void)
2302{
Linas Vepstas90f10842006-10-10 15:56:04 -05002303 printk(KERN_INFO "Spidernet version %s.\n", VERSION);
2304
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002305 if (rx_descriptors < SPIDER_NET_RX_DESCRIPTORS_MIN) {
2306 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MIN;
2307 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2308 }
2309 if (rx_descriptors > SPIDER_NET_RX_DESCRIPTORS_MAX) {
2310 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MAX;
2311 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2312 }
2313 if (tx_descriptors < SPIDER_NET_TX_DESCRIPTORS_MIN) {
2314 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MIN;
2315 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2316 }
2317 if (tx_descriptors > SPIDER_NET_TX_DESCRIPTORS_MAX) {
2318 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MAX;
2319 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2320 }
2321
2322 return pci_register_driver(&spider_net_driver);
2323}
2324
2325/**
2326 * spider_net_cleanup - exit function when driver is unloaded
2327 *
2328 * spider_net_cleanup unregisters the device driver
2329 */
2330static void __exit spider_net_cleanup(void)
2331{
2332 pci_unregister_driver(&spider_net_driver);
2333}
2334
2335module_init(spider_net_init);
2336module_exit(spider_net_cleanup);