blob: 4c26b9a5683e18eda2224a8bcbde0a313bc280c4 [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{
Benjamin Herrenschmidt3bc0f402006-11-11 17:25:13 +110091 /* We use the powerpc specific variants instead of readl_be() because
92 * we know spidernet is not a real PCI device and we can thus avoid the
93 * performance hit caused by the PCI workarounds.
94 */
95 return in_be32(card->regs + reg);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -070096}
97
98/**
99 * spider_net_write_reg - writes to an SMMIO register of a card
100 * @card: device structure
101 * @reg: register to write to
102 * @value: value to write into the specified SMMIO register
103 */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200104static inline void
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700105spider_net_write_reg(struct spider_net_card *card, u32 reg, u32 value)
106{
Benjamin Herrenschmidt3bc0f402006-11-11 17:25:13 +1100107 /* We use the powerpc specific variants instead of writel_be() because
108 * we know spidernet is not a real PCI device and we can thus avoid the
109 * performance hit caused by the PCI workarounds.
110 */
111 out_be32(card->regs + reg, value);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700112}
113
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700114/** spider_net_write_phy - write to phy register
115 * @netdev: adapter to be written to
116 * @mii_id: id of MII
117 * @reg: PHY register
118 * @val: value to be written to phy register
119 *
120 * spider_net_write_phy_register writes to an arbitrary PHY
121 * register via the spider GPCWOPCMD register. We assume the queue does
122 * not run full (not more than 15 commands outstanding).
123 **/
124static void
125spider_net_write_phy(struct net_device *netdev, int mii_id,
126 int reg, int val)
127{
128 struct spider_net_card *card = netdev_priv(netdev);
129 u32 writevalue;
130
131 writevalue = ((u32)mii_id << 21) |
132 ((u32)reg << 16) | ((u32)val);
133
134 spider_net_write_reg(card, SPIDER_NET_GPCWOPCMD, writevalue);
135}
136
137/** spider_net_read_phy - read from phy register
138 * @netdev: network device to be read from
139 * @mii_id: id of MII
140 * @reg: PHY register
141 *
142 * Returns value read from PHY register
143 *
144 * spider_net_write_phy reads from an arbitrary PHY
145 * register via the spider GPCROPCMD register
146 **/
147static int
148spider_net_read_phy(struct net_device *netdev, int mii_id, int reg)
149{
150 struct spider_net_card *card = netdev_priv(netdev);
151 u32 readvalue;
152
153 readvalue = ((u32)mii_id << 21) | ((u32)reg << 16);
154 spider_net_write_reg(card, SPIDER_NET_GPCROPCMD, readvalue);
155
156 /* we don't use semaphores to wait for an SPIDER_NET_GPROPCMPINT
157 * interrupt, as we poll for the completion of the read operation
158 * in spider_net_read_phy. Should take about 50 us */
159 do {
160 readvalue = spider_net_read_reg(card, SPIDER_NET_GPCROPCMD);
161 } while (readvalue & SPIDER_NET_GPREXEC);
162
163 readvalue &= SPIDER_NET_GPRDAT_MASK;
164
165 return readvalue;
166}
167
168/**
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500169 * spider_net_rx_irq_off - switch off rx irq on this spider card
170 * @card: device structure
171 *
172 * switches off rx irq by masking them out in the GHIINTnMSK register
173 */
174static void
175spider_net_rx_irq_off(struct spider_net_card *card)
176{
177 u32 regvalue;
178
179 regvalue = SPIDER_NET_INT0_MASK_VALUE & (~SPIDER_NET_RXINT);
180 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, regvalue);
181}
182
183/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700184 * spider_net_rx_irq_on - switch on rx irq on this spider card
185 * @card: device structure
186 *
187 * switches on rx irq by enabling them in the GHIINTnMSK register
188 */
189static void
190spider_net_rx_irq_on(struct spider_net_card *card)
191{
192 u32 regvalue;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700193
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500194 regvalue = SPIDER_NET_INT0_MASK_VALUE | SPIDER_NET_RXINT;
195 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, regvalue);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700196}
197
198/**
199 * spider_net_set_promisc - sets the unicast address or the promiscuous mode
200 * @card: card structure
201 *
202 * spider_net_set_promisc sets the unicast destination address filter and
203 * thus either allows for non-promisc mode or promisc mode
204 */
205static void
206spider_net_set_promisc(struct spider_net_card *card)
207{
208 u32 macu, macl;
209 struct net_device *netdev = card->netdev;
210
211 if (netdev->flags & IFF_PROMISC) {
212 /* clear destination entry 0 */
213 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR, 0);
214 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR + 0x04, 0);
215 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R,
216 SPIDER_NET_PROMISC_VALUE);
217 } else {
218 macu = netdev->dev_addr[0];
219 macu <<= 8;
220 macu |= netdev->dev_addr[1];
221 memcpy(&macl, &netdev->dev_addr[2], sizeof(macl));
222
223 macu |= SPIDER_NET_UA_DESCR_VALUE;
224 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR, macu);
225 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR + 0x04, macl);
226 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R,
227 SPIDER_NET_NONPROMISC_VALUE);
228 }
229}
230
231/**
232 * spider_net_get_mac_address - read mac address from spider card
233 * @card: device structure
234 *
235 * reads MAC address from GMACUNIMACU and GMACUNIMACL registers
236 */
237static int
238spider_net_get_mac_address(struct net_device *netdev)
239{
240 struct spider_net_card *card = netdev_priv(netdev);
241 u32 macl, macu;
242
243 macl = spider_net_read_reg(card, SPIDER_NET_GMACUNIMACL);
244 macu = spider_net_read_reg(card, SPIDER_NET_GMACUNIMACU);
245
246 netdev->dev_addr[0] = (macu >> 24) & 0xff;
247 netdev->dev_addr[1] = (macu >> 16) & 0xff;
248 netdev->dev_addr[2] = (macu >> 8) & 0xff;
249 netdev->dev_addr[3] = macu & 0xff;
250 netdev->dev_addr[4] = (macl >> 8) & 0xff;
251 netdev->dev_addr[5] = macl & 0xff;
252
253 if (!is_valid_ether_addr(&netdev->dev_addr[0]))
254 return -EINVAL;
255
256 return 0;
257}
258
259/**
260 * spider_net_get_descr_status -- returns the status of a descriptor
261 * @descr: descriptor to look at
262 *
263 * returns the status as in the dmac_cmd_status field of the descriptor
264 */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200265static inline int
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700266spider_net_get_descr_status(struct spider_net_descr *descr)
267{
Jens Osterkampbdd01502006-07-13 11:54:08 +0200268 return descr->dmac_cmd_status & SPIDER_NET_DESCR_IND_PROC_MASK;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700269}
270
271/**
272 * spider_net_free_chain - free descriptor chain
273 * @card: card structure
274 * @chain: address of chain
275 *
276 */
277static void
278spider_net_free_chain(struct spider_net_card *card,
279 struct spider_net_descr_chain *chain)
280{
281 struct spider_net_descr *descr;
282
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600283 descr = chain->ring;
284 do {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700285 descr->bus_addr = 0;
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600286 descr->next_descr_addr = 0;
287 descr = descr->next;
288 } while (descr != chain->ring);
289
290 dma_free_coherent(&card->pdev->dev, chain->num_desc,
291 chain->ring, chain->dma_addr);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700292}
293
294/**
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600295 * spider_net_init_chain - alloc and link descriptor chain
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700296 * @card: card structure
297 * @chain: address of chain
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700298 *
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600299 * We manage a circular list that mirrors the hardware structure,
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700300 * except that the hardware uses bus addresses.
301 *
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600302 * Returns 0 on success, <0 on failure
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700303 */
304static int
305spider_net_init_chain(struct spider_net_card *card,
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600306 struct spider_net_descr_chain *chain)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700307{
308 int i;
309 struct spider_net_descr *descr;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500310 dma_addr_t buf;
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600311 size_t alloc_size;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700312
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600313 alloc_size = chain->num_desc * sizeof (struct spider_net_descr);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700314
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600315 chain->ring = dma_alloc_coherent(&card->pdev->dev, alloc_size,
316 &chain->dma_addr, GFP_KERNEL);
317
318 if (!chain->ring)
319 return -ENOMEM;
320
321 descr = chain->ring;
322 memset(descr, 0, alloc_size);
323
324 /* Set up the hardware pointers in each descriptor */
325 buf = chain->dma_addr;
326 for (i=0; i < chain->num_desc; i++, descr++) {
Jens Osterkampbdd01502006-07-13 11:54:08 +0200327 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700328
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500329 descr->bus_addr = buf;
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600330 descr->next_descr_addr = 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700331 descr->next = descr + 1;
332 descr->prev = descr - 1;
333
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600334 buf += sizeof(struct spider_net_descr);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700335 }
336 /* do actual circular list */
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600337 (descr-1)->next = chain->ring;
338 chain->ring->prev = descr-1;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700339
Jens Osterkampbdd01502006-07-13 11:54:08 +0200340 spin_lock_init(&chain->lock);
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600341 chain->head = chain->ring;
342 chain->tail = chain->ring;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700343 return 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700344}
345
346/**
347 * spider_net_free_rx_chain_contents - frees descr contents in rx chain
348 * @card: card structure
349 *
350 * returns 0 on success, <0 on failure
351 */
352static void
353spider_net_free_rx_chain_contents(struct spider_net_card *card)
354{
355 struct spider_net_descr *descr;
356
357 descr = card->rx_chain.head;
Linas Vepstas64751912006-10-10 16:22:29 -0500358 do {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700359 if (descr->skb) {
360 dev_kfree_skb(descr->skb);
361 pci_unmap_single(card->pdev, descr->buf_addr,
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500362 SPIDER_NET_MAX_FRAME,
Linas Vepstas348bc2a2006-10-10 16:21:10 -0500363 PCI_DMA_BIDIRECTIONAL);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700364 }
365 descr = descr->next;
Linas Vepstas64751912006-10-10 16:22:29 -0500366 } while (descr != card->rx_chain.head);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700367}
368
369/**
370 * spider_net_prepare_rx_descr - reinitializes a rx descriptor
371 * @card: card structure
372 * @descr: descriptor to re-init
373 *
374 * return 0 on succes, <0 on failure
375 *
376 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
377 * Activate the descriptor state-wise
378 */
379static int
380spider_net_prepare_rx_descr(struct spider_net_card *card,
381 struct spider_net_descr *descr)
382{
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500383 dma_addr_t buf;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700384 int error = 0;
385 int offset;
386 int bufsize;
387
388 /* we need to round up the buffer size to a multiple of 128 */
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500389 bufsize = (SPIDER_NET_MAX_FRAME + SPIDER_NET_RXBUF_ALIGN - 1) &
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700390 (~(SPIDER_NET_RXBUF_ALIGN - 1));
391
392 /* and we need to have it 128 byte aligned, therefore we allocate a
393 * bit more */
394 /* allocate an skb */
395 descr->skb = dev_alloc_skb(bufsize + SPIDER_NET_RXBUF_ALIGN - 1);
396 if (!descr->skb) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500397 if (netif_msg_rx_err(card) && net_ratelimit())
398 pr_err("Not enough memory to allocate rx buffer\n");
Jim Lewis9b6b0b82006-09-22 17:22:53 -0700399 card->spider_stats.alloc_rx_skb_error++;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700400 return -ENOMEM;
401 }
402 descr->buf_size = bufsize;
403 descr->result_size = 0;
404 descr->valid_size = 0;
405 descr->data_status = 0;
406 descr->data_error = 0;
407
408 offset = ((unsigned long)descr->skb->data) &
409 (SPIDER_NET_RXBUF_ALIGN - 1);
410 if (offset)
411 skb_reserve(descr->skb, SPIDER_NET_RXBUF_ALIGN - offset);
412 /* io-mmu-map the skb */
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500413 buf = pci_map_single(card->pdev, descr->skb->data,
Jens Osterkampbdd01502006-07-13 11:54:08 +0200414 SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500415 descr->buf_addr = buf;
Olof Johanssond4b0a4c2006-09-27 14:02:26 -0500416 if (pci_dma_mapping_error(buf)) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700417 dev_kfree_skb_any(descr->skb);
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500418 if (netif_msg_rx_err(card) && net_ratelimit())
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700419 pr_err("Could not iommu-map rx buffer\n");
Jim Lewis9b6b0b82006-09-22 17:22:53 -0700420 card->spider_stats.rx_iommu_map_error++;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200421 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700422 } else {
Jens Osterkampbdd01502006-07-13 11:54:08 +0200423 descr->dmac_cmd_status = SPIDER_NET_DESCR_CARDOWNED |
424 SPIDER_NET_DMAC_NOINTR_COMPLETE;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700425 }
426
427 return error;
428}
429
430/**
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500431 * spider_net_enable_rxchtails - sets RX dmac chain tail addresses
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700432 * @card: card structure
433 *
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500434 * spider_net_enable_rxchtails sets the RX DMAC chain tail adresses in the
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700435 * chip by writing to the appropriate register. DMA is enabled in
436 * spider_net_enable_rxdmac.
437 */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200438static inline void
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700439spider_net_enable_rxchtails(struct spider_net_card *card)
440{
441 /* assume chain is aligned correctly */
442 spider_net_write_reg(card, SPIDER_NET_GDADCHA ,
443 card->rx_chain.tail->bus_addr);
444}
445
446/**
447 * spider_net_enable_rxdmac - enables a receive DMA controller
448 * @card: card structure
449 *
450 * spider_net_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
451 * in the GDADMACCNTR register
452 */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200453static inline void
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700454spider_net_enable_rxdmac(struct spider_net_card *card)
455{
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500456 wmb();
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700457 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
458 SPIDER_NET_DMA_RX_VALUE);
459}
460
461/**
462 * spider_net_refill_rx_chain - refills descriptors/skbs in the rx chains
463 * @card: card structure
464 *
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500465 * refills descriptors in the rx chain: allocates skbs and iommu-maps them.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700466 */
467static void
468spider_net_refill_rx_chain(struct spider_net_card *card)
469{
Jens Osterkampbdd01502006-07-13 11:54:08 +0200470 struct spider_net_descr_chain *chain = &card->rx_chain;
471 unsigned long flags;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700472
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500473 /* one context doing the refill (and a second context seeing that
474 * and omitting it) is ok. If called by NAPI, we'll be called again
475 * as spider_net_decode_one_descr is called several times. If some
476 * interrupt calls us, the NAPI is about to clean up anyway. */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200477 if (!spin_trylock_irqsave(&chain->lock, flags))
478 return;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700479
Jens Osterkampbdd01502006-07-13 11:54:08 +0200480 while (spider_net_get_descr_status(chain->head) ==
481 SPIDER_NET_DESCR_NOT_IN_USE) {
482 if (spider_net_prepare_rx_descr(card, chain->head))
483 break;
484 chain->head = chain->head->next;
485 }
486
487 spin_unlock_irqrestore(&chain->lock, flags);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700488}
489
490/**
491 * spider_net_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
492 * @card: card structure
493 *
494 * returns 0 on success, <0 on failure
495 */
496static int
497spider_net_alloc_rx_skbs(struct spider_net_card *card)
498{
499 int result;
500 struct spider_net_descr_chain *chain;
501
502 result = -ENOMEM;
503
504 chain = &card->rx_chain;
505 /* put at least one buffer into the chain. if this fails,
506 * we've got a problem. if not, spider_net_refill_rx_chain
507 * will do the rest at the end of this function */
508 if (spider_net_prepare_rx_descr(card, chain->head))
509 goto error;
510 else
511 chain->head = chain->head->next;
512
513 /* this will allocate the rest of the rx buffers; if not, it's
514 * business as usual later on */
515 spider_net_refill_rx_chain(card);
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500516 spider_net_enable_rxdmac(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700517 return 0;
518
519error:
520 spider_net_free_rx_chain_contents(card);
521 return result;
522}
523
524/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700525 * spider_net_get_multicast_hash - generates hash for multicast filter table
526 * @addr: multicast address
527 *
528 * returns the hash value.
529 *
530 * spider_net_get_multicast_hash calculates a hash value for a given multicast
531 * address, that is used to set the multicast filter tables
532 */
533static u8
534spider_net_get_multicast_hash(struct net_device *netdev, __u8 *addr)
535{
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700536 u32 crc;
537 u8 hash;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500538 char addr_for_crc[ETH_ALEN] = { 0, };
539 int i, bit;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700540
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500541 for (i = 0; i < ETH_ALEN * 8; i++) {
542 bit = (addr[i / 8] >> (i % 8)) & 1;
543 addr_for_crc[ETH_ALEN - 1 - i / 8] += bit << (7 - (i % 8));
544 }
545
546 crc = crc32_be(~0, addr_for_crc, netdev->addr_len);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700547
548 hash = (crc >> 27);
549 hash <<= 3;
550 hash |= crc & 7;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500551 hash &= 0xff;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700552
553 return hash;
554}
555
556/**
557 * spider_net_set_multi - sets multicast addresses and promisc flags
558 * @netdev: interface device structure
559 *
560 * spider_net_set_multi configures multicast addresses as needed for the
561 * netdev interface. It also sets up multicast, allmulti and promisc
562 * flags appropriately
563 */
564static void
565spider_net_set_multi(struct net_device *netdev)
566{
567 struct dev_mc_list *mc;
568 u8 hash;
569 int i;
570 u32 reg;
571 struct spider_net_card *card = netdev_priv(netdev);
572 unsigned long bitmask[SPIDER_NET_MULTICAST_HASHES / BITS_PER_LONG] =
573 {0, };
574
575 spider_net_set_promisc(card);
576
577 if (netdev->flags & IFF_ALLMULTI) {
578 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES; i++) {
579 set_bit(i, bitmask);
580 }
581 goto write_hash;
582 }
583
584 /* well, we know, what the broadcast hash value is: it's xfd
585 hash = spider_net_get_multicast_hash(netdev, netdev->broadcast); */
586 set_bit(0xfd, bitmask);
587
588 for (mc = netdev->mc_list; mc; mc = mc->next) {
589 hash = spider_net_get_multicast_hash(netdev, mc->dmi_addr);
590 set_bit(hash, bitmask);
591 }
592
593write_hash:
594 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES / 4; i++) {
595 reg = 0;
596 if (test_bit(i * 4, bitmask))
597 reg += 0x08;
598 reg <<= 8;
599 if (test_bit(i * 4 + 1, bitmask))
600 reg += 0x08;
601 reg <<= 8;
602 if (test_bit(i * 4 + 2, bitmask))
603 reg += 0x08;
604 reg <<= 8;
605 if (test_bit(i * 4 + 3, bitmask))
606 reg += 0x08;
607
608 spider_net_write_reg(card, SPIDER_NET_GMRMHFILnR + i * 4, reg);
609 }
610}
611
612/**
613 * spider_net_disable_rxdmac - disables the receive DMA controller
614 * @card: card structure
615 *
616 * spider_net_disable_rxdmac terminates processing on the DMA controller by
617 * turing off DMA and issueing a force end
618 */
619static void
620spider_net_disable_rxdmac(struct spider_net_card *card)
621{
622 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
623 SPIDER_NET_DMA_RX_FEND_VALUE);
624}
625
626/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700627 * spider_net_prepare_tx_descr - fill tx descriptor with skb data
628 * @card: card structure
629 * @descr: descriptor structure to fill out
630 * @skb: packet to use
631 *
632 * returns 0 on success, <0 on failure.
633 *
634 * fills out the descriptor structure with skb data and len. Copies data,
635 * if needed (32bit DMA!)
636 */
637static int
638spider_net_prepare_tx_descr(struct spider_net_card *card,
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700639 struct sk_buff *skb)
640{
Linas Vepstas9cc7bf7e2006-10-10 16:14:29 -0500641 struct spider_net_descr *descr;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500642 dma_addr_t buf;
Linas Vepstas9cc7bf7e2006-10-10 16:14:29 -0500643 unsigned long flags;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500644
James K Lewis9c434f52006-11-17 14:39:36 -0800645 buf = pci_map_single(card->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
Olof Johanssond4b0a4c2006-09-27 14:02:26 -0500646 if (pci_dma_mapping_error(buf)) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500647 if (netif_msg_tx_err(card) && net_ratelimit())
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700648 pr_err("could not iommu-map packet (%p, %i). "
James K Lewis9c434f52006-11-17 14:39:36 -0800649 "Dropping packet\n", skb->data, skb->len);
Jim Lewis9b6b0b82006-09-22 17:22:53 -0700650 card->spider_stats.tx_iommu_map_error++;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700651 return -ENOMEM;
652 }
653
Linas Vepstas9cc7bf7e2006-10-10 16:14:29 -0500654 spin_lock_irqsave(&card->tx_chain.lock, flags);
655 descr = card->tx_chain.head;
656 card->tx_chain.head = descr->next;
657
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500658 descr->buf_addr = buf;
James K Lewis9c434f52006-11-17 14:39:36 -0800659 descr->buf_size = skb->len;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200660 descr->next_descr_addr = 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700661 descr->skb = skb;
662 descr->data_status = 0;
663
Jens Osterkampbdd01502006-07-13 11:54:08 +0200664 descr->dmac_cmd_status =
665 SPIDER_NET_DESCR_CARDOWNED | SPIDER_NET_DMAC_NOCS;
Linas Vepstas9cc7bf7e2006-10-10 16:14:29 -0500666 spin_unlock_irqrestore(&card->tx_chain.lock, flags);
667
Jens Osterkampbdd01502006-07-13 11:54:08 +0200668 if (skb->protocol == htons(ETH_P_IP))
669 switch (skb->nh.iph->protocol) {
670 case IPPROTO_TCP:
671 descr->dmac_cmd_status |= SPIDER_NET_DMAC_TCP;
672 break;
673 case IPPROTO_UDP:
674 descr->dmac_cmd_status |= SPIDER_NET_DMAC_UDP;
675 break;
676 }
677
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500678 /* Chain the bus address, so that the DMA engine finds this descr. */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200679 descr->prev->next_descr_addr = descr->bus_addr;
680
Linas Vepstas917a5b82006-10-10 16:01:51 -0500681 card->netdev->trans_start = jiffies; /* set netdev watchdog timer */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200682 return 0;
683}
684
Linas Vepstasa664ccf2006-10-10 16:18:18 -0500685static int
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500686spider_net_set_low_watermark(struct spider_net_card *card)
687{
Linas Vepstas9cc7bf7e2006-10-10 16:14:29 -0500688 unsigned long flags;
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500689 int status;
690 int cnt=0;
691 int i;
692 struct spider_net_descr *descr = card->tx_chain.tail;
693
Linas Vepstas9cc7bf7e2006-10-10 16:14:29 -0500694 /* Measure the length of the queue. Measurement does not
695 * need to be precise -- does not need a lock. */
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500696 while (descr != card->tx_chain.head) {
697 status = descr->dmac_cmd_status & SPIDER_NET_DESCR_NOT_IN_USE;
698 if (status == SPIDER_NET_DESCR_NOT_IN_USE)
699 break;
700 descr = descr->next;
701 cnt++;
702 }
703
704 /* If TX queue is short, don't even bother with interrupts */
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600705 if (cnt < card->tx_chain.num_desc/4)
Linas Vepstasa664ccf2006-10-10 16:18:18 -0500706 return cnt;
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500707
708 /* Set low-watermark 3/4th's of the way into the queue. */
709 descr = card->tx_chain.tail;
710 cnt = (cnt*3)/4;
711 for (i=0;i<cnt; i++)
712 descr = descr->next;
713
714 /* Set the new watermark, clear the old watermark */
Linas Vepstas9cc7bf7e2006-10-10 16:14:29 -0500715 spin_lock_irqsave(&card->tx_chain.lock, flags);
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500716 descr->dmac_cmd_status |= SPIDER_NET_DESCR_TXDESFLG;
717 if (card->low_watermark && card->low_watermark != descr)
718 card->low_watermark->dmac_cmd_status =
719 card->low_watermark->dmac_cmd_status & ~SPIDER_NET_DESCR_TXDESFLG;
720 card->low_watermark = descr;
Linas Vepstas9cc7bf7e2006-10-10 16:14:29 -0500721 spin_unlock_irqrestore(&card->tx_chain.lock, flags);
Linas Vepstasa664ccf2006-10-10 16:18:18 -0500722 return cnt;
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500723}
724
Jens Osterkampbdd01502006-07-13 11:54:08 +0200725/**
726 * spider_net_release_tx_chain - processes sent tx descriptors
727 * @card: adapter structure
728 * @brutal: if set, don't care about whether descriptor seems to be in use
729 *
730 * returns 0 if the tx ring is empty, otherwise 1.
731 *
732 * spider_net_release_tx_chain releases the tx descriptors that spider has
733 * finished with (if non-brutal) or simply release tx descriptors (if brutal).
734 * If some other context is calling this function, we return 1 so that we're
735 * scheduled again (if we were scheduled) and will not loose initiative.
736 */
737static int
738spider_net_release_tx_chain(struct spider_net_card *card, int brutal)
739{
740 struct spider_net_descr_chain *chain = &card->tx_chain;
Linas Vepstas9cc7bf7e2006-10-10 16:14:29 -0500741 struct spider_net_descr *descr;
742 struct sk_buff *skb;
743 u32 buf_addr;
744 unsigned long flags;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200745 int status;
746
Jens Osterkampbdd01502006-07-13 11:54:08 +0200747 while (chain->tail != chain->head) {
Linas Vepstas9cc7bf7e2006-10-10 16:14:29 -0500748 spin_lock_irqsave(&chain->lock, flags);
749 descr = chain->tail;
750
751 status = spider_net_get_descr_status(descr);
Jens Osterkampbdd01502006-07-13 11:54:08 +0200752 switch (status) {
753 case SPIDER_NET_DESCR_COMPLETE:
754 card->netdev_stats.tx_packets++;
Linas Vepstas9cc7bf7e2006-10-10 16:14:29 -0500755 card->netdev_stats.tx_bytes += descr->skb->len;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200756 break;
757
758 case SPIDER_NET_DESCR_CARDOWNED:
Linas Vepstas9cc7bf7e2006-10-10 16:14:29 -0500759 if (!brutal) {
760 spin_unlock_irqrestore(&chain->lock, flags);
Jens Osterkampbdd01502006-07-13 11:54:08 +0200761 return 1;
Linas Vepstas9cc7bf7e2006-10-10 16:14:29 -0500762 }
763
Jens Osterkampbdd01502006-07-13 11:54:08 +0200764 /* fallthrough, if we release the descriptors
765 * brutally (then we don't care about
766 * SPIDER_NET_DESCR_CARDOWNED) */
767
768 case SPIDER_NET_DESCR_RESPONSE_ERROR:
769 case SPIDER_NET_DESCR_PROTECTION_ERROR:
770 case SPIDER_NET_DESCR_FORCE_END:
771 if (netif_msg_tx_err(card))
772 pr_err("%s: forcing end of tx descriptor "
773 "with status x%02x\n",
774 card->netdev->name, status);
775 card->netdev_stats.tx_errors++;
776 break;
777
778 default:
779 card->netdev_stats.tx_dropped++;
Linas Vepstas9cc7bf7e2006-10-10 16:14:29 -0500780 if (!brutal) {
781 spin_unlock_irqrestore(&chain->lock, flags);
Linas Vepstasc3fee4c2006-10-10 16:00:04 -0500782 return 1;
Linas Vepstas9cc7bf7e2006-10-10 16:14:29 -0500783 }
Jens Osterkampbdd01502006-07-13 11:54:08 +0200784 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700785
Linas Vepstas9cc7bf7e2006-10-10 16:14:29 -0500786 chain->tail = descr->next;
787 descr->dmac_cmd_status |= SPIDER_NET_DESCR_NOT_IN_USE;
788 skb = descr->skb;
789 buf_addr = descr->buf_addr;
790 spin_unlock_irqrestore(&chain->lock, flags);
791
792 /* unmap the skb */
793 if (skb) {
James K Lewis9c434f52006-11-17 14:39:36 -0800794 pci_unmap_single(card->pdev, buf_addr, skb->len,
795 PCI_DMA_TODEVICE);
Linas Vepstas9cc7bf7e2006-10-10 16:14:29 -0500796 dev_kfree_skb(skb);
797 }
798 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700799 return 0;
800}
801
802/**
803 * spider_net_kick_tx_dma - enables TX DMA processing
804 * @card: card structure
805 * @descr: descriptor address to enable TX processing at
806 *
Linas Vepstasa664ccf2006-10-10 16:18:18 -0500807 * This routine will start the transmit DMA running if
808 * it is not already running. This routine ned only be
809 * called when queueing a new packet to an empty tx queue.
810 * Writes the current tx chain head as start address
811 * of the tx descriptor chain and enables the transmission
812 * DMA engine.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700813 */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200814static inline void
815spider_net_kick_tx_dma(struct spider_net_card *card)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700816{
Jens Osterkampbdd01502006-07-13 11:54:08 +0200817 struct spider_net_descr *descr;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700818
Jens Osterkampbdd01502006-07-13 11:54:08 +0200819 if (spider_net_read_reg(card, SPIDER_NET_GDTDMACCNTR) &
820 SPIDER_NET_TX_DMA_EN)
821 goto out;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700822
Jens Osterkampbdd01502006-07-13 11:54:08 +0200823 descr = card->tx_chain.tail;
824 for (;;) {
825 if (spider_net_get_descr_status(descr) ==
826 SPIDER_NET_DESCR_CARDOWNED) {
827 spider_net_write_reg(card, SPIDER_NET_GDTDCHA,
828 descr->bus_addr);
829 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
830 SPIDER_NET_DMA_TX_VALUE);
831 break;
832 }
833 if (descr == card->tx_chain.head)
834 break;
835 descr = descr->next;
836 }
837
838out:
839 mod_timer(&card->tx_timer, jiffies + SPIDER_NET_TX_TIMER);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700840}
841
842/**
843 * spider_net_xmit - transmits a frame over the device
844 * @skb: packet to send out
845 * @netdev: interface device structure
846 *
Jens Osterkampbdd01502006-07-13 11:54:08 +0200847 * returns 0 on success, !0 on failure
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700848 */
849static int
850spider_net_xmit(struct sk_buff *skb, struct net_device *netdev)
851{
Linas Vepstasa664ccf2006-10-10 16:18:18 -0500852 int cnt;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700853 struct spider_net_card *card = netdev_priv(netdev);
Jens Osterkampbdd01502006-07-13 11:54:08 +0200854 struct spider_net_descr_chain *chain = &card->tx_chain;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200855
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500856 spider_net_release_tx_chain(card, 0);
857
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500858 if ((chain->head->next == chain->tail->prev) ||
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500859 (spider_net_prepare_tx_descr(card, skb) != 0)) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500860
Jim Lewis9b6b0b82006-09-22 17:22:53 -0700861 card->netdev_stats.tx_dropped++;
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500862 netif_stop_queue(netdev);
863 return NETDEV_TX_BUSY;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200864 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700865
Linas Vepstasa664ccf2006-10-10 16:18:18 -0500866 cnt = spider_net_set_low_watermark(card);
867 if (cnt < 5)
868 spider_net_kick_tx_dma(card);
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500869 return NETDEV_TX_OK;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200870}
871
872/**
873 * spider_net_cleanup_tx_ring - cleans up the TX ring
874 * @card: card structure
875 *
Linas Vepstas68a8c6092006-10-10 16:13:05 -0500876 * spider_net_cleanup_tx_ring is called by either the tx_timer
877 * or from the NAPI polling routine.
878 * This routine releases resources associted with transmitted
879 * packets, including updating the queue tail pointer.
Jens Osterkampbdd01502006-07-13 11:54:08 +0200880 */
881static void
882spider_net_cleanup_tx_ring(struct spider_net_card *card)
883{
Jens Osterkampbdd01502006-07-13 11:54:08 +0200884 if ((spider_net_release_tx_chain(card, 0) != 0) &&
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500885 (card->netdev->flags & IFF_UP)) {
Jens Osterkampbdd01502006-07-13 11:54:08 +0200886 spider_net_kick_tx_dma(card);
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500887 netif_wake_queue(card->netdev);
888 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700889}
890
891/**
892 * spider_net_do_ioctl - called for device ioctls
893 * @netdev: interface device structure
894 * @ifr: request parameter structure for ioctl
895 * @cmd: command code for ioctl
896 *
897 * returns 0 on success, <0 on failure. Currently, we have no special ioctls.
898 * -EOPNOTSUPP is returned, if an unknown ioctl was requested
899 */
900static int
901spider_net_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
902{
903 switch (cmd) {
904 default:
905 return -EOPNOTSUPP;
906 }
907}
908
909/**
910 * spider_net_pass_skb_up - takes an skb from a descriptor and passes it on
911 * @descr: descriptor to process
912 * @card: card structure
913 *
914 * returns 1 on success, 0 if no packet was passed to the stack
915 *
Linas Vepstas05b346b2006-12-13 15:15:15 -0600916 * Fills out skb structure and passes the data to the stack.
917 * The descriptor state is not changed.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700918 */
919static int
920spider_net_pass_skb_up(struct spider_net_descr *descr,
Linas Vepstas1cd173f2006-12-13 15:12:26 -0600921 struct spider_net_card *card)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700922{
923 struct sk_buff *skb;
924 struct net_device *netdev;
925 u32 data_status, data_error;
926
927 data_status = descr->data_status;
928 data_error = descr->data_error;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700929 netdev = card->netdev;
930
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500931 skb = descr->skb;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700932 skb->dev = netdev;
933 skb_put(skb, descr->valid_size);
934
935 /* the card seems to add 2 bytes of junk in front
936 * of the ethernet frame */
937#define SPIDER_MISALIGN 2
938 skb_pull(skb, SPIDER_MISALIGN);
939 skb->protocol = eth_type_trans(skb, netdev);
940
941 /* checksum offload */
942 if (card->options.rx_csum) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500943 if ( ( (data_status & SPIDER_NET_DATA_STATUS_CKSUM_MASK) ==
944 SPIDER_NET_DATA_STATUS_CKSUM_MASK) &&
945 !(data_error & SPIDER_NET_DATA_ERR_CKSUM_MASK))
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700946 skb->ip_summed = CHECKSUM_UNNECESSARY;
947 else
948 skb->ip_summed = CHECKSUM_NONE;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500949 } else
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700950 skb->ip_summed = CHECKSUM_NONE;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700951
952 if (data_status & SPIDER_NET_VLAN_PACKET) {
953 /* further enhancements: HW-accel VLAN
954 * vlan_hwaccel_receive_skb
955 */
956 }
957
958 /* pass skb up to stack */
Linas Vepstas1cd173f2006-12-13 15:12:26 -0600959 netif_receive_skb(skb);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700960
961 /* update netdevice statistics */
962 card->netdev_stats.rx_packets++;
963 card->netdev_stats.rx_bytes += skb->len;
964
965 return 1;
966}
967
968/**
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500969 * spider_net_decode_one_descr - processes an rx descriptor
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700970 * @card: card structure
971 *
Linas Vepstas1cd173f2006-12-13 15:12:26 -0600972 * Returns 1 if a packet has been sent to the stack, otherwise 0
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700973 *
Linas Vepstas1cd173f2006-12-13 15:12:26 -0600974 * Processes an rx descriptor by iommu-unmapping the data buffer and passing
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500975 * the packet up to the stack. This function is called in softirq
976 * context, e.g. either bottom half from interrupt or NAPI polling context
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700977 */
978static int
Linas Vepstas1cd173f2006-12-13 15:12:26 -0600979spider_net_decode_one_descr(struct spider_net_card *card)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700980{
Jens Osterkampbdd01502006-07-13 11:54:08 +0200981 struct spider_net_descr_chain *chain = &card->rx_chain;
982 struct spider_net_descr *descr = chain->tail;
983 int status;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700984 int result;
985
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700986 status = spider_net_get_descr_status(descr);
987
Linas Vepstas1cd173f2006-12-13 15:12:26 -0600988 /* nothing in the descriptor yet */
989 if (status == SPIDER_NET_DESCR_CARDOWNED)
990 return 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700991
992 if (status == SPIDER_NET_DESCR_NOT_IN_USE) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500993 /* not initialized yet, the ring must be empty */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700994 spider_net_refill_rx_chain(card);
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500995 spider_net_enable_rxdmac(card);
Linas Vepstas1cd173f2006-12-13 15:12:26 -0600996 return 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700997 }
998
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500999 /* descriptor definitively used -- move on tail */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001000 chain->tail = descr->next;
1001
1002 result = 0;
Linas Vepstas05b346b2006-12-13 15:15:15 -06001003
1004 /* unmap descriptor */
1005 pci_unmap_single(card->pdev, descr->buf_addr,
1006 SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
1007
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001008 if ( (status == SPIDER_NET_DESCR_RESPONSE_ERROR) ||
1009 (status == SPIDER_NET_DESCR_PROTECTION_ERROR) ||
1010 (status == SPIDER_NET_DESCR_FORCE_END) ) {
1011 if (netif_msg_rx_err(card))
1012 pr_err("%s: dropping RX descriptor with state %d\n",
1013 card->netdev->name, status);
1014 card->netdev_stats.rx_dropped++;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001015 dev_kfree_skb_irq(descr->skb);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001016 goto refill;
1017 }
1018
1019 if ( (status != SPIDER_NET_DESCR_COMPLETE) &&
1020 (status != SPIDER_NET_DESCR_FRAME_END) ) {
Linas Vepstas5a028872006-12-13 15:08:25 -06001021 if (netif_msg_rx_err(card))
Linas Vepstas05b346b2006-12-13 15:15:15 -06001022 pr_err("%s: RX descriptor with unkown state %d\n",
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001023 card->netdev->name, status);
Linas Vepstas5a028872006-12-13 15:08:25 -06001024 card->spider_stats.rx_desc_unk_state++;
Linas Vepstas05b346b2006-12-13 15:15:15 -06001025 dev_kfree_skb_irq(descr->skb);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001026 goto refill;
1027 }
1028
Linas Vepstas366684b2006-12-13 15:16:18 -06001029 /* The cases we'll throw away the packet immediately */
1030 if (descr->data_error & SPIDER_NET_DESTROY_RX_FLAGS) {
1031 if (netif_msg_rx_err(card))
1032 pr_err("%s: error in received descriptor found, "
1033 "data_status=x%08x, data_error=x%08x\n",
1034 card->netdev->name,
1035 descr->data_status, descr->data_error);
1036 card->spider_stats.rx_desc_error++;
1037 dev_kfree_skb_irq(descr->skb);
1038 goto refill;
1039 }
1040
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001041 /* ok, we've got a packet in descr */
Linas Vepstas1cd173f2006-12-13 15:12:26 -06001042 result = spider_net_pass_skb_up(descr, card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001043refill:
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001044 /* change the descriptor state: */
Linas Vepstas1cd173f2006-12-13 15:12:26 -06001045 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001046 return result;
1047}
1048
1049/**
1050 * spider_net_poll - NAPI poll function called by the stack to return packets
1051 * @netdev: interface device structure
1052 * @budget: number of packets we can pass to the stack at most
1053 *
1054 * returns 0 if no more packets available to the driver/stack. Returns 1,
1055 * if the quota is exceeded, but the driver has still packets.
1056 *
1057 * spider_net_poll returns all packets from the rx descriptors to the stack
1058 * (using netif_receive_skb). If all/enough packets are up, the driver
1059 * reenables interrupts and returns 0. If not, 1 is returned.
1060 */
1061static int
1062spider_net_poll(struct net_device *netdev, int *budget)
1063{
1064 struct spider_net_card *card = netdev_priv(netdev);
1065 int packets_to_do, packets_done = 0;
1066 int no_more_packets = 0;
1067
Linas Vepstas68a8c6092006-10-10 16:13:05 -05001068 spider_net_cleanup_tx_ring(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001069 packets_to_do = min(*budget, netdev->quota);
1070
1071 while (packets_to_do) {
Linas Vepstas1cd173f2006-12-13 15:12:26 -06001072 if (spider_net_decode_one_descr(card)) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001073 packets_done++;
1074 packets_to_do--;
1075 } else {
1076 /* no more packets for the stack */
1077 no_more_packets = 1;
1078 break;
1079 }
1080 }
1081
1082 netdev->quota -= packets_done;
1083 *budget -= packets_done;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001084 spider_net_refill_rx_chain(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001085
1086 /* if all packets are in the stack, enable interrupts and return 0 */
1087 /* if not, return 1 */
1088 if (no_more_packets) {
1089 netif_rx_complete(netdev);
1090 spider_net_rx_irq_on(card);
1091 return 0;
1092 }
1093
1094 return 1;
1095}
1096
1097/**
1098 * spider_net_vlan_rx_reg - initializes VLAN structures in the driver and card
1099 * @netdev: interface device structure
1100 * @grp: vlan_group structure that is registered (NULL on destroying interface)
1101 */
1102static void
1103spider_net_vlan_rx_reg(struct net_device *netdev, struct vlan_group *grp)
1104{
1105 /* further enhancement... yet to do */
1106 return;
1107}
1108
1109/**
1110 * spider_net_vlan_rx_add - adds VLAN id to the card filter
1111 * @netdev: interface device structure
1112 * @vid: VLAN id to add
1113 */
1114static void
1115spider_net_vlan_rx_add(struct net_device *netdev, uint16_t vid)
1116{
1117 /* further enhancement... yet to do */
1118 /* add vid to card's VLAN filter table */
1119 return;
1120}
1121
1122/**
1123 * spider_net_vlan_rx_kill - removes VLAN id to the card filter
1124 * @netdev: interface device structure
1125 * @vid: VLAN id to remove
1126 */
1127static void
1128spider_net_vlan_rx_kill(struct net_device *netdev, uint16_t vid)
1129{
1130 /* further enhancement... yet to do */
1131 /* remove vid from card's VLAN filter table */
1132}
1133
1134/**
1135 * spider_net_get_stats - get interface statistics
1136 * @netdev: interface device structure
1137 *
1138 * returns the interface statistics residing in the spider_net_card struct
1139 */
1140static struct net_device_stats *
1141spider_net_get_stats(struct net_device *netdev)
1142{
1143 struct spider_net_card *card = netdev_priv(netdev);
1144 struct net_device_stats *stats = &card->netdev_stats;
1145 return stats;
1146}
1147
1148/**
1149 * spider_net_change_mtu - changes the MTU of an interface
1150 * @netdev: interface device structure
1151 * @new_mtu: new MTU value
1152 *
1153 * returns 0 on success, <0 on failure
1154 */
1155static int
1156spider_net_change_mtu(struct net_device *netdev, int new_mtu)
1157{
1158 /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
1159 * and mtu is outbound only anyway */
1160 if ( (new_mtu < SPIDER_NET_MIN_MTU ) ||
1161 (new_mtu > SPIDER_NET_MAX_MTU) )
1162 return -EINVAL;
1163 netdev->mtu = new_mtu;
1164 return 0;
1165}
1166
1167/**
1168 * spider_net_set_mac - sets the MAC of an interface
1169 * @netdev: interface device structure
1170 * @ptr: pointer to new MAC address
1171 *
1172 * Returns 0 on success, <0 on failure. Currently, we don't support this
1173 * and will always return EOPNOTSUPP.
1174 */
1175static int
1176spider_net_set_mac(struct net_device *netdev, void *p)
1177{
1178 struct spider_net_card *card = netdev_priv(netdev);
Jens Osterkamp054034d2005-09-06 19:30:55 -07001179 u32 macl, macu, regvalue;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001180 struct sockaddr *addr = p;
1181
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001182 if (!is_valid_ether_addr(addr->sa_data))
1183 return -EADDRNOTAVAIL;
1184
Jens Osterkamp054034d2005-09-06 19:30:55 -07001185 /* switch off GMACTPE and GMACRPE */
1186 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1187 regvalue &= ~((1 << 5) | (1 << 6));
1188 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1189
1190 /* write mac */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001191 macu = (addr->sa_data[0]<<24) + (addr->sa_data[1]<<16) +
1192 (addr->sa_data[2]<<8) + (addr->sa_data[3]);
1193 macl = (addr->sa_data[4]<<8) + (addr->sa_data[5]);
1194 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACU, macu);
1195 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACL, macl);
1196
Jens Osterkamp054034d2005-09-06 19:30:55 -07001197 /* switch GMACTPE and GMACRPE back on */
1198 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1199 regvalue |= ((1 << 5) | (1 << 6));
1200 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1201
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001202 spider_net_set_promisc(card);
1203
1204 /* look up, whether we have been successful */
1205 if (spider_net_get_mac_address(netdev))
1206 return -EADDRNOTAVAIL;
1207 if (memcmp(netdev->dev_addr,addr->sa_data,netdev->addr_len))
1208 return -EADDRNOTAVAIL;
1209
1210 return 0;
1211}
1212
1213/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001214 * spider_net_handle_error_irq - handles errors raised by an interrupt
1215 * @card: card structure
1216 * @status_reg: interrupt status register 0 (GHIINT0STS)
1217 *
1218 * spider_net_handle_error_irq treats or ignores all error conditions
1219 * found when an interrupt is presented
1220 */
1221static void
1222spider_net_handle_error_irq(struct spider_net_card *card, u32 status_reg)
1223{
1224 u32 error_reg1, error_reg2;
1225 u32 i;
1226 int show_error = 1;
1227
1228 error_reg1 = spider_net_read_reg(card, SPIDER_NET_GHIINT1STS);
1229 error_reg2 = spider_net_read_reg(card, SPIDER_NET_GHIINT2STS);
1230
1231 /* check GHIINT0STS ************************************/
1232 if (status_reg)
1233 for (i = 0; i < 32; i++)
1234 if (status_reg & (1<<i))
1235 switch (i)
1236 {
1237 /* let error_reg1 and error_reg2 evaluation decide, what to do
1238 case SPIDER_NET_PHYINT:
1239 case SPIDER_NET_GMAC2INT:
1240 case SPIDER_NET_GMAC1INT:
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001241 case SPIDER_NET_GFIFOINT:
1242 case SPIDER_NET_DMACINT:
1243 case SPIDER_NET_GSYSINT:
1244 break; */
1245
Linas Vepstas98b90402006-10-10 16:08:42 -05001246 case SPIDER_NET_GIPSINT:
1247 show_error = 0;
1248 break;
1249
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001250 case SPIDER_NET_GPWOPCMPINT:
1251 /* PHY write operation completed */
1252 show_error = 0;
1253 break;
1254 case SPIDER_NET_GPROPCMPINT:
1255 /* PHY read operation completed */
1256 /* we don't use semaphores, as we poll for the completion
1257 * of the read operation in spider_net_read_phy. Should take
1258 * about 50 us */
1259 show_error = 0;
1260 break;
1261 case SPIDER_NET_GPWFFINT:
1262 /* PHY command queue full */
1263 if (netif_msg_intr(card))
1264 pr_err("PHY write queue full\n");
1265 show_error = 0;
1266 break;
1267
1268 /* case SPIDER_NET_GRMDADRINT: not used. print a message */
1269 /* case SPIDER_NET_GRMARPINT: not used. print a message */
1270 /* case SPIDER_NET_GRMMPINT: not used. print a message */
1271
1272 case SPIDER_NET_GDTDEN0INT:
1273 /* someone has set TX_DMA_EN to 0 */
1274 show_error = 0;
1275 break;
1276
1277 case SPIDER_NET_GDDDEN0INT: /* fallthrough */
1278 case SPIDER_NET_GDCDEN0INT: /* fallthrough */
1279 case SPIDER_NET_GDBDEN0INT: /* fallthrough */
1280 case SPIDER_NET_GDADEN0INT:
1281 /* someone has set RX_DMA_EN to 0 */
1282 show_error = 0;
1283 break;
1284
1285 /* RX interrupts */
1286 case SPIDER_NET_GDDFDCINT:
1287 case SPIDER_NET_GDCFDCINT:
1288 case SPIDER_NET_GDBFDCINT:
1289 case SPIDER_NET_GDAFDCINT:
1290 /* case SPIDER_NET_GDNMINT: not used. print a message */
1291 /* case SPIDER_NET_GCNMINT: not used. print a message */
1292 /* case SPIDER_NET_GBNMINT: not used. print a message */
1293 /* case SPIDER_NET_GANMINT: not used. print a message */
1294 /* case SPIDER_NET_GRFNMINT: not used. print a message */
1295 show_error = 0;
1296 break;
1297
1298 /* TX interrupts */
1299 case SPIDER_NET_GDTFDCINT:
1300 show_error = 0;
1301 break;
1302 case SPIDER_NET_GTTEDINT:
1303 show_error = 0;
1304 break;
1305 case SPIDER_NET_GDTDCEINT:
1306 /* chain end. If a descriptor should be sent, kick off
1307 * tx dma
Linas Vepstas98b90402006-10-10 16:08:42 -05001308 if (card->tx_chain.tail != card->tx_chain.head)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001309 spider_net_kick_tx_dma(card);
Linas Vepstas98b90402006-10-10 16:08:42 -05001310 */
1311 show_error = 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001312 break;
1313
1314 /* case SPIDER_NET_G1TMCNTINT: not used. print a message */
1315 /* case SPIDER_NET_GFREECNTINT: not used. print a message */
1316 }
1317
1318 /* check GHIINT1STS ************************************/
1319 if (error_reg1)
1320 for (i = 0; i < 32; i++)
1321 if (error_reg1 & (1<<i))
1322 switch (i)
1323 {
1324 case SPIDER_NET_GTMFLLINT:
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001325 if (netif_msg_intr(card) && net_ratelimit())
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001326 pr_err("Spider TX RAM full\n");
1327 show_error = 0;
1328 break;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001329 case SPIDER_NET_GRFDFLLINT: /* fallthrough */
1330 case SPIDER_NET_GRFCFLLINT: /* fallthrough */
1331 case SPIDER_NET_GRFBFLLINT: /* fallthrough */
1332 case SPIDER_NET_GRFAFLLINT: /* fallthrough */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001333 case SPIDER_NET_GRMFLLINT:
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001334 if (netif_msg_intr(card) && net_ratelimit())
Linas Vepstas5a028872006-12-13 15:08:25 -06001335 pr_err("Spider RX RAM full, incoming packets "
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001336 "might be discarded!\n");
1337 spider_net_rx_irq_off(card);
Linas Vepstas75856172006-12-13 15:10:06 -06001338 netif_rx_schedule(card->netdev);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001339 show_error = 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001340 break;
1341
1342 /* case SPIDER_NET_GTMSHTINT: problem, print a message */
1343 case SPIDER_NET_GDTINVDINT:
1344 /* allrighty. tx from previous descr ok */
1345 show_error = 0;
1346 break;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001347
1348 /* chain end */
1349 case SPIDER_NET_GDDDCEINT: /* fallthrough */
1350 case SPIDER_NET_GDCDCEINT: /* fallthrough */
1351 case SPIDER_NET_GDBDCEINT: /* fallthrough */
1352 case SPIDER_NET_GDADCEINT:
Linas Vepstas5a028872006-12-13 15:08:25 -06001353 if (netif_msg_intr(card) && net_ratelimit())
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001354 pr_err("got descriptor chain end interrupt, "
1355 "restarting DMAC %c.\n",
Linas Vepstas37aad752006-10-10 16:06:53 -05001356 'D'-(i-SPIDER_NET_GDDDCEINT)/3);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001357 spider_net_refill_rx_chain(card);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001358 spider_net_enable_rxdmac(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001359 show_error = 0;
1360 break;
1361
1362 /* invalid descriptor */
1363 case SPIDER_NET_GDDINVDINT: /* fallthrough */
1364 case SPIDER_NET_GDCINVDINT: /* fallthrough */
1365 case SPIDER_NET_GDBINVDINT: /* fallthrough */
1366 case SPIDER_NET_GDAINVDINT:
1367 /* could happen when rx chain is full */
1368 spider_net_refill_rx_chain(card);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001369 spider_net_enable_rxdmac(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001370 show_error = 0;
1371 break;
1372
1373 /* case SPIDER_NET_GDTRSERINT: problem, print a message */
1374 /* case SPIDER_NET_GDDRSERINT: problem, print a message */
1375 /* case SPIDER_NET_GDCRSERINT: problem, print a message */
1376 /* case SPIDER_NET_GDBRSERINT: problem, print a message */
1377 /* case SPIDER_NET_GDARSERINT: problem, print a message */
1378 /* case SPIDER_NET_GDSERINT: problem, print a message */
1379 /* case SPIDER_NET_GDTPTERINT: problem, print a message */
1380 /* case SPIDER_NET_GDDPTERINT: problem, print a message */
1381 /* case SPIDER_NET_GDCPTERINT: problem, print a message */
1382 /* case SPIDER_NET_GDBPTERINT: problem, print a message */
1383 /* case SPIDER_NET_GDAPTERINT: problem, print a message */
1384 default:
1385 show_error = 1;
1386 break;
1387 }
1388
1389 /* check GHIINT2STS ************************************/
1390 if (error_reg2)
1391 for (i = 0; i < 32; i++)
1392 if (error_reg2 & (1<<i))
1393 switch (i)
1394 {
1395 /* there is nothing we can (want to) do at this time. Log a
1396 * message, we can switch on and off the specific values later on
1397 case SPIDER_NET_GPROPERINT:
1398 case SPIDER_NET_GMCTCRSNGINT:
1399 case SPIDER_NET_GMCTLCOLINT:
1400 case SPIDER_NET_GMCTTMOTINT:
1401 case SPIDER_NET_GMCRCAERINT:
1402 case SPIDER_NET_GMCRCALERINT:
1403 case SPIDER_NET_GMCRALNERINT:
1404 case SPIDER_NET_GMCROVRINT:
1405 case SPIDER_NET_GMCRRNTINT:
1406 case SPIDER_NET_GMCRRXERINT:
1407 case SPIDER_NET_GTITCSERINT:
1408 case SPIDER_NET_GTIFMTERINT:
1409 case SPIDER_NET_GTIPKTRVKINT:
1410 case SPIDER_NET_GTISPINGINT:
1411 case SPIDER_NET_GTISADNGINT:
1412 case SPIDER_NET_GTISPDNGINT:
1413 case SPIDER_NET_GRIFMTERINT:
1414 case SPIDER_NET_GRIPKTRVKINT:
1415 case SPIDER_NET_GRISPINGINT:
1416 case SPIDER_NET_GRISADNGINT:
1417 case SPIDER_NET_GRISPDNGINT:
1418 break;
1419 */
1420 default:
1421 break;
1422 }
1423
Linas Vepstas5a028872006-12-13 15:08:25 -06001424 if ((show_error) && (netif_msg_intr(card)) && net_ratelimit())
Linas Vepstas98b90402006-10-10 16:08:42 -05001425 pr_err("Got error interrupt on %s, GHIINT0STS = 0x%08x, "
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001426 "GHIINT1STS = 0x%08x, GHIINT2STS = 0x%08x\n",
Linas Vepstas98b90402006-10-10 16:08:42 -05001427 card->netdev->name,
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001428 status_reg, error_reg1, error_reg2);
1429
1430 /* clear interrupt sources */
1431 spider_net_write_reg(card, SPIDER_NET_GHIINT1STS, error_reg1);
1432 spider_net_write_reg(card, SPIDER_NET_GHIINT2STS, error_reg2);
1433}
1434
1435/**
1436 * spider_net_interrupt - interrupt handler for spider_net
1437 * @irq: interupt number
1438 * @ptr: pointer to net_device
1439 * @regs: PU registers
1440 *
1441 * returns IRQ_HANDLED, if interrupt was for driver, or IRQ_NONE, if no
1442 * interrupt found raised by card.
1443 *
1444 * This is the interrupt handler, that turns off
1445 * interrupts for this device and makes the stack poll the driver
1446 */
1447static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001448spider_net_interrupt(int irq, void *ptr)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001449{
1450 struct net_device *netdev = ptr;
1451 struct spider_net_card *card = netdev_priv(netdev);
1452 u32 status_reg;
1453
1454 status_reg = spider_net_read_reg(card, SPIDER_NET_GHIINT0STS);
1455
1456 if (!status_reg)
1457 return IRQ_NONE;
1458
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001459 if (status_reg & SPIDER_NET_RXINT ) {
1460 spider_net_rx_irq_off(card);
1461 netif_rx_schedule(netdev);
1462 }
Linas Vepstas68a8c6092006-10-10 16:13:05 -05001463 if (status_reg & SPIDER_NET_TXINT)
1464 netif_rx_schedule(netdev);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001465
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001466 if (status_reg & SPIDER_NET_ERRINT )
1467 spider_net_handle_error_irq(card, status_reg);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001468
1469 /* clear interrupt sources */
1470 spider_net_write_reg(card, SPIDER_NET_GHIINT0STS, status_reg);
1471
1472 return IRQ_HANDLED;
1473}
1474
1475#ifdef CONFIG_NET_POLL_CONTROLLER
1476/**
1477 * spider_net_poll_controller - artificial interrupt for netconsole etc.
1478 * @netdev: interface device structure
1479 *
1480 * see Documentation/networking/netconsole.txt
1481 */
1482static void
1483spider_net_poll_controller(struct net_device *netdev)
1484{
1485 disable_irq(netdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01001486 spider_net_interrupt(netdev->irq, netdev);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001487 enable_irq(netdev->irq);
1488}
1489#endif /* CONFIG_NET_POLL_CONTROLLER */
1490
1491/**
1492 * spider_net_init_card - initializes the card
1493 * @card: card structure
1494 *
1495 * spider_net_init_card initializes the card so that other registers can
1496 * be used
1497 */
1498static void
1499spider_net_init_card(struct spider_net_card *card)
1500{
1501 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1502 SPIDER_NET_CKRCTRL_STOP_VALUE);
1503
1504 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1505 SPIDER_NET_CKRCTRL_RUN_VALUE);
1506}
1507
1508/**
1509 * spider_net_enable_card - enables the card by setting all kinds of regs
1510 * @card: card structure
1511 *
1512 * spider_net_enable_card sets a lot of SMMIO registers to enable the device
1513 */
1514static void
1515spider_net_enable_card(struct spider_net_card *card)
1516{
1517 int i;
1518 /* the following array consists of (register),(value) pairs
1519 * that are set in this function. A register of 0 ends the list */
1520 u32 regs[][2] = {
1521 { SPIDER_NET_GRESUMINTNUM, 0 },
1522 { SPIDER_NET_GREINTNUM, 0 },
1523
1524 /* set interrupt frame number registers */
1525 /* clear the single DMA engine registers first */
1526 { SPIDER_NET_GFAFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1527 { SPIDER_NET_GFBFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1528 { SPIDER_NET_GFCFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1529 { SPIDER_NET_GFDFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1530 /* then set, what we really need */
1531 { SPIDER_NET_GFFRMNUM, SPIDER_NET_FRAMENUM_VALUE },
1532
1533 /* timer counter registers and stuff */
1534 { SPIDER_NET_GFREECNNUM, 0 },
1535 { SPIDER_NET_GONETIMENUM, 0 },
1536 { SPIDER_NET_GTOUTFRMNUM, 0 },
1537
1538 /* RX mode setting */
1539 { SPIDER_NET_GRXMDSET, SPIDER_NET_RXMODE_VALUE },
1540 /* TX mode setting */
1541 { SPIDER_NET_GTXMDSET, SPIDER_NET_TXMODE_VALUE },
1542 /* IPSEC mode setting */
1543 { SPIDER_NET_GIPSECINIT, SPIDER_NET_IPSECINIT_VALUE },
1544
1545 { SPIDER_NET_GFTRESTRT, SPIDER_NET_RESTART_VALUE },
1546
1547 { SPIDER_NET_GMRWOLCTRL, 0 },
Jens Osterkampb636d172006-05-04 05:59:56 -04001548 { SPIDER_NET_GTESTMD, 0x10000000 },
1549 { SPIDER_NET_GTTQMSK, 0x00400040 },
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001550
1551 { SPIDER_NET_GMACINTEN, 0 },
1552
1553 /* flow control stuff */
1554 { SPIDER_NET_GMACAPAUSE, SPIDER_NET_MACAPAUSE_VALUE },
1555 { SPIDER_NET_GMACTXPAUSE, SPIDER_NET_TXPAUSE_VALUE },
1556
1557 { SPIDER_NET_GMACBSTLMT, SPIDER_NET_BURSTLMT_VALUE },
1558 { 0, 0}
1559 };
1560
1561 i = 0;
1562 while (regs[i][0]) {
1563 spider_net_write_reg(card, regs[i][0], regs[i][1]);
1564 i++;
1565 }
1566
1567 /* clear unicast filter table entries 1 to 14 */
1568 for (i = 1; i <= 14; i++) {
1569 spider_net_write_reg(card,
1570 SPIDER_NET_GMRUAFILnR + i * 8,
1571 0x00080000);
1572 spider_net_write_reg(card,
1573 SPIDER_NET_GMRUAFILnR + i * 8 + 4,
1574 0x00000000);
1575 }
1576
1577 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R, 0x08080000);
1578
1579 spider_net_write_reg(card, SPIDER_NET_ECMODE, SPIDER_NET_ECMODE_VALUE);
1580
1581 /* set chain tail adress for RX chains and
1582 * enable DMA */
1583 spider_net_enable_rxchtails(card);
1584 spider_net_enable_rxdmac(card);
1585
1586 spider_net_write_reg(card, SPIDER_NET_GRXDMAEN, SPIDER_NET_WOL_VALUE);
1587
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001588 spider_net_write_reg(card, SPIDER_NET_GMACLENLMT,
1589 SPIDER_NET_LENLMT_VALUE);
1590 spider_net_write_reg(card, SPIDER_NET_GMACMODE,
1591 SPIDER_NET_MACMODE_VALUE);
1592 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD,
1593 SPIDER_NET_OPMODE_VALUE);
1594
1595 /* set interrupt mask registers */
1596 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK,
1597 SPIDER_NET_INT0_MASK_VALUE);
1598 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK,
1599 SPIDER_NET_INT1_MASK_VALUE);
1600 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK,
1601 SPIDER_NET_INT2_MASK_VALUE);
Jens Osterkampbdd01502006-07-13 11:54:08 +02001602
1603 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
Linas Vepstas7bd54c82006-11-17 14:39:40 -08001604 SPIDER_NET_GDTBSTA);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001605}
1606
1607/**
1608 * spider_net_open - called upon ifonfig up
1609 * @netdev: interface device structure
1610 *
1611 * returns 0 on success, <0 on failure
1612 *
1613 * spider_net_open allocates all the descriptors and memory needed for
1614 * operation, sets up multicast list and enables interrupts
1615 */
1616int
1617spider_net_open(struct net_device *netdev)
1618{
1619 struct spider_net_card *card = netdev_priv(netdev);
Linas Vepstas348bc2a2006-10-10 16:21:10 -05001620 struct spider_net_descr *descr;
Linas Vepstasd4ed8f82006-12-13 15:06:59 -06001621 int result;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001622
Linas Vepstasd4ed8f82006-12-13 15:06:59 -06001623 result = spider_net_init_chain(card, &card->tx_chain);
1624 if (result)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001625 goto alloc_tx_failed;
Linas Vepstas204e5fa2006-10-10 16:11:33 -05001626 card->low_watermark = NULL;
1627
Linas Vepstasd4ed8f82006-12-13 15:06:59 -06001628 result = spider_net_init_chain(card, &card->rx_chain);
1629 if (result)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001630 goto alloc_rx_failed;
1631
Linas Vepstasd4ed8f82006-12-13 15:06:59 -06001632 /* Make a ring of of bus addresses */
1633 descr = card->rx_chain.ring;
1634 do {
Linas Vepstas348bc2a2006-10-10 16:21:10 -05001635 descr->next_descr_addr = descr->next->bus_addr;
Linas Vepstasd4ed8f82006-12-13 15:06:59 -06001636 descr = descr->next;
1637 } while (descr != card->rx_chain.ring);
Linas Vepstas348bc2a2006-10-10 16:21:10 -05001638
Linas Vepstasd4ed8f82006-12-13 15:06:59 -06001639 /* Allocate rx skbs */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001640 if (spider_net_alloc_rx_skbs(card))
1641 goto alloc_skbs_failed;
1642
1643 spider_net_set_multi(netdev);
1644
1645 /* further enhancement: setup hw vlan, if needed */
1646
1647 result = -EBUSY;
1648 if (request_irq(netdev->irq, spider_net_interrupt,
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07001649 IRQF_SHARED, netdev->name, netdev))
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001650 goto register_int_failed;
1651
1652 spider_net_enable_card(card);
1653
Jens Osterkamp543cec52005-09-06 19:30:54 -07001654 netif_start_queue(netdev);
1655 netif_carrier_on(netdev);
1656 netif_poll_enable(netdev);
1657
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001658 return 0;
1659
1660register_int_failed:
1661 spider_net_free_rx_chain_contents(card);
1662alloc_skbs_failed:
1663 spider_net_free_chain(card, &card->rx_chain);
1664alloc_rx_failed:
1665 spider_net_free_chain(card, &card->tx_chain);
1666alloc_tx_failed:
1667 return result;
1668}
1669
1670/**
1671 * spider_net_setup_phy - setup PHY
1672 * @card: card structure
1673 *
1674 * returns 0 on success, <0 on failure
1675 *
1676 * spider_net_setup_phy is used as part of spider_net_probe. Sets
1677 * the PHY to 1000 Mbps
1678 **/
1679static int
1680spider_net_setup_phy(struct spider_net_card *card)
1681{
1682 struct mii_phy *phy = &card->phy;
1683
1684 spider_net_write_reg(card, SPIDER_NET_GDTDMASEL,
1685 SPIDER_NET_DMASEL_VALUE);
1686 spider_net_write_reg(card, SPIDER_NET_GPCCTRL,
1687 SPIDER_NET_PHY_CTRL_VALUE);
1688 phy->mii_id = 1;
1689 phy->dev = card->netdev;
1690 phy->mdio_read = spider_net_read_phy;
1691 phy->mdio_write = spider_net_write_phy;
1692
1693 mii_phy_probe(phy, phy->mii_id);
1694
1695 if (phy->def->ops->setup_forced)
1696 phy->def->ops->setup_forced(phy, SPEED_1000, DUPLEX_FULL);
1697
Jens Osterkamp8ec93452006-05-04 05:59:41 -04001698 phy->def->ops->enable_fiber(phy);
Jens Osterkamp53abbf72005-09-16 08:55:33 +02001699
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001700 phy->def->ops->read_link(phy);
1701 pr_info("Found %s with %i Mbps, %s-duplex.\n", phy->def->name,
1702 phy->speed, phy->duplex==1 ? "Full" : "Half");
1703
1704 return 0;
1705}
1706
1707/**
1708 * spider_net_download_firmware - loads firmware into the adapter
1709 * @card: card structure
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001710 * @firmware_ptr: pointer to firmware data
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001711 *
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001712 * spider_net_download_firmware loads the firmware data into the
1713 * adapter. It assumes the length etc. to be allright.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001714 */
Arnd Bergmann0d3ea162006-01-12 17:16:41 -05001715static int
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001716spider_net_download_firmware(struct spider_net_card *card,
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001717 const void *firmware_ptr)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001718{
1719 int sequencer, i;
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001720 const u32 *fw_ptr = firmware_ptr;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001721
1722 /* stop sequencers */
1723 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1724 SPIDER_NET_STOP_SEQ_VALUE);
1725
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001726 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1727 sequencer++) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001728 spider_net_write_reg(card,
1729 SPIDER_NET_GSnPRGADR + sequencer * 8, 0);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001730 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001731 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1732 sequencer * 8, *fw_ptr);
1733 fw_ptr++;
1734 }
1735 }
1736
Arnd Bergmann0d3ea162006-01-12 17:16:41 -05001737 if (spider_net_read_reg(card, SPIDER_NET_GSINIT))
1738 return -EIO;
1739
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001740 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1741 SPIDER_NET_RUN_SEQ_VALUE);
Arnd Bergmann0d3ea162006-01-12 17:16:41 -05001742
1743 return 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001744}
1745
1746/**
1747 * spider_net_init_firmware - reads in firmware parts
1748 * @card: card structure
1749 *
1750 * Returns 0 on success, <0 on failure
1751 *
1752 * spider_net_init_firmware opens the sequencer firmware and does some basic
1753 * checks. This function opens and releases the firmware structure. A call
1754 * to download the firmware is performed before the release.
1755 *
1756 * Firmware format
1757 * ===============
1758 * spider_fw.bin is expected to be a file containing 6*1024*4 bytes, 4k being
1759 * the program for each sequencer. Use the command
1760 * tail -q -n +2 Seq_code1_0x088.txt Seq_code2_0x090.txt \
1761 * Seq_code3_0x098.txt Seq_code4_0x0A0.txt Seq_code5_0x0A8.txt \
1762 * Seq_code6_0x0B0.txt | xxd -r -p -c4 > spider_fw.bin
1763 *
1764 * to generate spider_fw.bin, if you have sequencer programs with something
1765 * like the following contents for each sequencer:
1766 * <ONE LINE COMMENT>
1767 * <FIRST 4-BYTES-WORD FOR SEQUENCER>
1768 * <SECOND 4-BYTES-WORD FOR SEQUENCER>
1769 * ...
1770 * <1024th 4-BYTES-WORD FOR SEQUENCER>
1771 */
1772static int
1773spider_net_init_firmware(struct spider_net_card *card)
1774{
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001775 struct firmware *firmware = NULL;
Arnd Bergmann030d6752006-01-12 17:16:42 -05001776 struct device_node *dn;
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001777 const u8 *fw_prop = NULL;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001778 int err = -ENOENT;
1779 int fw_size;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001780
Arnd Bergmann030d6752006-01-12 17:16:42 -05001781 if (request_firmware((const struct firmware **)&firmware,
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001782 SPIDER_NET_FIRMWARE_NAME, &card->pdev->dev) == 0) {
1783 if ( (firmware->size != SPIDER_NET_FIRMWARE_LEN) &&
1784 netif_msg_probe(card) ) {
1785 pr_err("Incorrect size of spidernet firmware in " \
1786 "filesystem. Looking in host firmware...\n");
1787 goto try_host_fw;
1788 }
1789 err = spider_net_download_firmware(card, firmware->data);
Arnd Bergmann030d6752006-01-12 17:16:42 -05001790
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001791 release_firmware(firmware);
1792 if (err)
1793 goto try_host_fw;
Arnd Bergmann030d6752006-01-12 17:16:42 -05001794
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001795 goto done;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001796 }
1797
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001798try_host_fw:
1799 dn = pci_device_to_OF_node(card->pdev);
1800 if (!dn)
1801 goto out_err;
1802
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001803 fw_prop = get_property(dn, "firmware", &fw_size);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001804 if (!fw_prop)
1805 goto out_err;
1806
1807 if ( (fw_size != SPIDER_NET_FIRMWARE_LEN) &&
1808 netif_msg_probe(card) ) {
1809 pr_err("Incorrect size of spidernet firmware in " \
1810 "host firmware\n");
1811 goto done;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001812 }
1813
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001814 err = spider_net_download_firmware(card, fw_prop);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001815
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001816done:
1817 return err;
1818out_err:
1819 if (netif_msg_probe(card))
1820 pr_err("Couldn't find spidernet firmware in filesystem " \
1821 "or host firmware\n");
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001822 return err;
1823}
1824
1825/**
1826 * spider_net_workaround_rxramfull - work around firmware bug
1827 * @card: card structure
1828 *
1829 * no return value
1830 **/
1831static void
1832spider_net_workaround_rxramfull(struct spider_net_card *card)
1833{
1834 int i, sequencer = 0;
1835
1836 /* cancel reset */
1837 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1838 SPIDER_NET_CKRCTRL_RUN_VALUE);
1839
1840 /* empty sequencer data */
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001841 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1842 sequencer++) {
Jens Osterkampee962a52006-07-13 11:54:23 +02001843 spider_net_write_reg(card, SPIDER_NET_GSnPRGADR +
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001844 sequencer * 8, 0x0);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001845 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001846 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1847 sequencer * 8, 0x0);
1848 }
1849 }
1850
1851 /* set sequencer operation */
1852 spider_net_write_reg(card, SPIDER_NET_GSINIT, 0x000000fe);
1853
1854 /* reset */
1855 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1856 SPIDER_NET_CKRCTRL_STOP_VALUE);
1857}
1858
1859/**
Jens Osterkampbdd01502006-07-13 11:54:08 +02001860 * spider_net_stop - called upon ifconfig down
1861 * @netdev: interface device structure
1862 *
1863 * always returns 0
1864 */
1865int
1866spider_net_stop(struct net_device *netdev)
1867{
1868 struct spider_net_card *card = netdev_priv(netdev);
1869
Jens Osterkampbdd01502006-07-13 11:54:08 +02001870 netif_poll_disable(netdev);
1871 netif_carrier_off(netdev);
1872 netif_stop_queue(netdev);
1873 del_timer_sync(&card->tx_timer);
1874
1875 /* disable/mask all interrupts */
1876 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, 0);
1877 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK, 0);
1878 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK, 0);
1879
1880 /* free_irq(netdev->irq, netdev);*/
1881 free_irq(to_pci_dev(netdev->class_dev.dev)->irq, netdev);
1882
1883 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
1884 SPIDER_NET_DMA_TX_FEND_VALUE);
1885
1886 /* turn off DMA, force end */
1887 spider_net_disable_rxdmac(card);
1888
1889 /* release chains */
Linas Vepstas9cc7bf7e2006-10-10 16:14:29 -05001890 spider_net_release_tx_chain(card, 1);
Linas Vepstasd4ed8f82006-12-13 15:06:59 -06001891 spider_net_free_rx_chain_contents(card);
Jens Osterkampbdd01502006-07-13 11:54:08 +02001892
Jens Osterkampa55eb052007-02-01 12:07:47 +01001893 spider_net_free_rx_chain_contents(card);
1894
Jens Osterkampbdd01502006-07-13 11:54:08 +02001895 spider_net_free_chain(card, &card->tx_chain);
1896 spider_net_free_chain(card, &card->rx_chain);
1897
1898 return 0;
1899}
1900
1901/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001902 * spider_net_tx_timeout_task - task scheduled by the watchdog timeout
1903 * function (to be called not under interrupt status)
1904 * @data: data, is interface device structure
1905 *
1906 * called as task when tx hangs, resets interface (if interface is up)
1907 */
1908static void
David Howellsc4028952006-11-22 14:57:56 +00001909spider_net_tx_timeout_task(struct work_struct *work)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001910{
David Howellsc4028952006-11-22 14:57:56 +00001911 struct spider_net_card *card =
1912 container_of(work, struct spider_net_card, tx_timeout_task);
1913 struct net_device *netdev = card->netdev;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001914
1915 if (!(netdev->flags & IFF_UP))
1916 goto out;
1917
1918 netif_device_detach(netdev);
1919 spider_net_stop(netdev);
1920
1921 spider_net_workaround_rxramfull(card);
1922 spider_net_init_card(card);
1923
1924 if (spider_net_setup_phy(card))
1925 goto out;
1926 if (spider_net_init_firmware(card))
1927 goto out;
1928
1929 spider_net_open(netdev);
Jens Osterkampbdd01502006-07-13 11:54:08 +02001930 spider_net_kick_tx_dma(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001931 netif_device_attach(netdev);
1932
1933out:
1934 atomic_dec(&card->tx_timeout_task_counter);
1935}
1936
1937/**
1938 * spider_net_tx_timeout - called when the tx timeout watchdog kicks in.
1939 * @netdev: interface device structure
1940 *
1941 * called, if tx hangs. Schedules a task that resets the interface
1942 */
1943static void
1944spider_net_tx_timeout(struct net_device *netdev)
1945{
1946 struct spider_net_card *card;
1947
1948 card = netdev_priv(netdev);
1949 atomic_inc(&card->tx_timeout_task_counter);
1950 if (netdev->flags & IFF_UP)
1951 schedule_work(&card->tx_timeout_task);
1952 else
1953 atomic_dec(&card->tx_timeout_task_counter);
Jim Lewis9b6b0b82006-09-22 17:22:53 -07001954 card->spider_stats.tx_timeouts++;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001955}
1956
1957/**
1958 * spider_net_setup_netdev_ops - initialization of net_device operations
1959 * @netdev: net_device structure
1960 *
1961 * fills out function pointers in the net_device structure
1962 */
1963static void
1964spider_net_setup_netdev_ops(struct net_device *netdev)
1965{
1966 netdev->open = &spider_net_open;
1967 netdev->stop = &spider_net_stop;
1968 netdev->hard_start_xmit = &spider_net_xmit;
1969 netdev->get_stats = &spider_net_get_stats;
1970 netdev->set_multicast_list = &spider_net_set_multi;
1971 netdev->set_mac_address = &spider_net_set_mac;
1972 netdev->change_mtu = &spider_net_change_mtu;
1973 netdev->do_ioctl = &spider_net_do_ioctl;
1974 /* tx watchdog */
1975 netdev->tx_timeout = &spider_net_tx_timeout;
1976 netdev->watchdog_timeo = SPIDER_NET_WATCHDOG_TIMEOUT;
1977 /* NAPI */
1978 netdev->poll = &spider_net_poll;
1979 netdev->weight = SPIDER_NET_NAPI_WEIGHT;
1980 /* HW VLAN */
1981 netdev->vlan_rx_register = &spider_net_vlan_rx_reg;
1982 netdev->vlan_rx_add_vid = &spider_net_vlan_rx_add;
1983 netdev->vlan_rx_kill_vid = &spider_net_vlan_rx_kill;
1984#ifdef CONFIG_NET_POLL_CONTROLLER
1985 /* poll controller */
1986 netdev->poll_controller = &spider_net_poll_controller;
1987#endif /* CONFIG_NET_POLL_CONTROLLER */
1988 /* ethtool ops */
1989 netdev->ethtool_ops = &spider_net_ethtool_ops;
1990}
1991
1992/**
1993 * spider_net_setup_netdev - initialization of net_device
1994 * @card: card structure
1995 *
1996 * Returns 0 on success or <0 on failure
1997 *
1998 * spider_net_setup_netdev initializes the net_device structure
1999 **/
2000static int
2001spider_net_setup_netdev(struct spider_net_card *card)
2002{
2003 int result;
2004 struct net_device *netdev = card->netdev;
2005 struct device_node *dn;
2006 struct sockaddr addr;
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10002007 const u8 *mac;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002008
2009 SET_MODULE_OWNER(netdev);
2010 SET_NETDEV_DEV(netdev, &card->pdev->dev);
2011
2012 pci_set_drvdata(card->pdev, netdev);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05002013
Arnd Bergmann11f1a522006-01-12 17:16:44 -05002014 init_timer(&card->tx_timer);
2015 card->tx_timer.function =
2016 (void (*)(unsigned long)) spider_net_cleanup_tx_ring;
2017 card->tx_timer.data = (unsigned long) card;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002018 netdev->irq = card->pdev->irq;
2019
2020 card->options.rx_csum = SPIDER_NET_RX_CSUM_DEFAULT;
2021
Linas Vepstasd4ed8f82006-12-13 15:06:59 -06002022 card->tx_chain.num_desc = tx_descriptors;
2023 card->rx_chain.num_desc = rx_descriptors;
Jim Lewisb68a60e2006-08-14 23:00:23 -07002024
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002025 spider_net_setup_netdev_ops(netdev);
2026
Jens Osterkampbdd01502006-07-13 11:54:08 +02002027 netdev->features = NETIF_F_HW_CSUM | NETIF_F_LLTX;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002028 /* some time: NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
2029 * NETIF_F_HW_VLAN_FILTER */
2030
2031 netdev->irq = card->pdev->irq;
2032
2033 dn = pci_device_to_OF_node(card->pdev);
Jens Osterkamp543cec52005-09-06 19:30:54 -07002034 if (!dn)
2035 return -EIO;
2036
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10002037 mac = get_property(dn, "local-mac-address", NULL);
Jens Osterkamp543cec52005-09-06 19:30:54 -07002038 if (!mac)
2039 return -EIO;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002040 memcpy(addr.sa_data, mac, ETH_ALEN);
2041
2042 result = spider_net_set_mac(netdev, &addr);
2043 if ((result) && (netif_msg_probe(card)))
2044 pr_err("Failed to set MAC address: %i\n", result);
2045
2046 result = register_netdev(netdev);
2047 if (result) {
2048 if (netif_msg_probe(card))
2049 pr_err("Couldn't register net_device: %i\n",
2050 result);
2051 return result;
2052 }
2053
2054 if (netif_msg_probe(card))
2055 pr_info("Initialized device %s.\n", netdev->name);
2056
2057 return 0;
2058}
2059
2060/**
2061 * spider_net_alloc_card - allocates net_device and card structure
2062 *
2063 * returns the card structure or NULL in case of errors
2064 *
2065 * the card and net_device structures are linked to each other
2066 */
2067static struct spider_net_card *
2068spider_net_alloc_card(void)
2069{
2070 struct net_device *netdev;
2071 struct spider_net_card *card;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002072
Linas Vepstasd4ed8f82006-12-13 15:06:59 -06002073 netdev = alloc_etherdev(sizeof(struct spider_net_card));
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002074 if (!netdev)
2075 return NULL;
2076
2077 card = netdev_priv(netdev);
2078 card->netdev = netdev;
2079 card->msg_enable = SPIDER_NET_DEFAULT_MSG;
David Howellsc4028952006-11-22 14:57:56 +00002080 INIT_WORK(&card->tx_timeout_task, spider_net_tx_timeout_task);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002081 init_waitqueue_head(&card->waitq);
2082 atomic_set(&card->tx_timeout_task_counter, 0);
2083
2084 return card;
2085}
2086
2087/**
2088 * spider_net_undo_pci_setup - releases PCI ressources
2089 * @card: card structure
2090 *
2091 * spider_net_undo_pci_setup releases the mapped regions
2092 */
2093static void
2094spider_net_undo_pci_setup(struct spider_net_card *card)
2095{
2096 iounmap(card->regs);
2097 pci_release_regions(card->pdev);
2098}
2099
2100/**
2101 * spider_net_setup_pci_dev - sets up the device in terms of PCI operations
2102 * @card: card structure
2103 * @pdev: PCI device
2104 *
2105 * Returns the card structure or NULL if any errors occur
2106 *
2107 * spider_net_setup_pci_dev initializes pdev and together with the
2108 * functions called in spider_net_open configures the device so that
2109 * data can be transferred over it
2110 * The net_device structure is attached to the card structure, if the
2111 * function returns without error.
2112 **/
2113static struct spider_net_card *
2114spider_net_setup_pci_dev(struct pci_dev *pdev)
2115{
2116 struct spider_net_card *card;
2117 unsigned long mmio_start, mmio_len;
2118
2119 if (pci_enable_device(pdev)) {
2120 pr_err("Couldn't enable PCI device\n");
2121 return NULL;
2122 }
2123
2124 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
2125 pr_err("Couldn't find proper PCI device base address.\n");
2126 goto out_disable_dev;
2127 }
2128
2129 if (pci_request_regions(pdev, spider_net_driver_name)) {
2130 pr_err("Couldn't obtain PCI resources, aborting.\n");
2131 goto out_disable_dev;
2132 }
2133
2134 pci_set_master(pdev);
2135
2136 card = spider_net_alloc_card();
2137 if (!card) {
2138 pr_err("Couldn't allocate net_device structure, "
2139 "aborting.\n");
2140 goto out_release_regions;
2141 }
2142 card->pdev = pdev;
2143
2144 /* fetch base address and length of first resource */
2145 mmio_start = pci_resource_start(pdev, 0);
2146 mmio_len = pci_resource_len(pdev, 0);
2147
2148 card->netdev->mem_start = mmio_start;
2149 card->netdev->mem_end = mmio_start + mmio_len;
2150 card->regs = ioremap(mmio_start, mmio_len);
2151
2152 if (!card->regs) {
2153 pr_err("Couldn't obtain PCI resources, aborting.\n");
2154 goto out_release_regions;
2155 }
2156
2157 return card;
2158
2159out_release_regions:
2160 pci_release_regions(pdev);
2161out_disable_dev:
2162 pci_disable_device(pdev);
2163 pci_set_drvdata(pdev, NULL);
2164 return NULL;
2165}
2166
2167/**
2168 * spider_net_probe - initialization of a device
2169 * @pdev: PCI device
2170 * @ent: entry in the device id list
2171 *
2172 * Returns 0 on success, <0 on failure
2173 *
2174 * spider_net_probe initializes pdev and registers a net_device
2175 * structure for it. After that, the device can be ifconfig'ed up
2176 **/
2177static int __devinit
2178spider_net_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2179{
2180 int err = -EIO;
2181 struct spider_net_card *card;
2182
2183 card = spider_net_setup_pci_dev(pdev);
2184 if (!card)
2185 goto out;
2186
2187 spider_net_workaround_rxramfull(card);
2188 spider_net_init_card(card);
2189
2190 err = spider_net_setup_phy(card);
2191 if (err)
2192 goto out_undo_pci;
2193
2194 err = spider_net_init_firmware(card);
2195 if (err)
2196 goto out_undo_pci;
2197
2198 err = spider_net_setup_netdev(card);
2199 if (err)
2200 goto out_undo_pci;
2201
2202 return 0;
2203
2204out_undo_pci:
2205 spider_net_undo_pci_setup(card);
2206 free_netdev(card->netdev);
2207out:
2208 return err;
2209}
2210
2211/**
2212 * spider_net_remove - removal of a device
2213 * @pdev: PCI device
2214 *
2215 * Returns 0 on success, <0 on failure
2216 *
2217 * spider_net_remove is called to remove the device and unregisters the
2218 * net_device
2219 **/
2220static void __devexit
2221spider_net_remove(struct pci_dev *pdev)
2222{
2223 struct net_device *netdev;
2224 struct spider_net_card *card;
2225
2226 netdev = pci_get_drvdata(pdev);
2227 card = netdev_priv(netdev);
2228
2229 wait_event(card->waitq,
2230 atomic_read(&card->tx_timeout_task_counter) == 0);
2231
2232 unregister_netdev(netdev);
Jens Osterkamp543cec52005-09-06 19:30:54 -07002233
2234 /* switch off card */
2235 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2236 SPIDER_NET_CKRCTRL_STOP_VALUE);
2237 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2238 SPIDER_NET_CKRCTRL_RUN_VALUE);
2239
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002240 spider_net_undo_pci_setup(card);
2241 free_netdev(netdev);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002242}
2243
2244static struct pci_driver spider_net_driver = {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002245 .name = spider_net_driver_name,
2246 .id_table = spider_net_pci_tbl,
2247 .probe = spider_net_probe,
2248 .remove = __devexit_p(spider_net_remove)
2249};
2250
2251/**
2252 * spider_net_init - init function when the driver is loaded
2253 *
2254 * spider_net_init registers the device driver
2255 */
2256static int __init spider_net_init(void)
2257{
Linas Vepstas90f10842006-10-10 15:56:04 -05002258 printk(KERN_INFO "Spidernet version %s.\n", VERSION);
2259
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002260 if (rx_descriptors < SPIDER_NET_RX_DESCRIPTORS_MIN) {
2261 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MIN;
2262 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2263 }
2264 if (rx_descriptors > SPIDER_NET_RX_DESCRIPTORS_MAX) {
2265 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MAX;
2266 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2267 }
2268 if (tx_descriptors < SPIDER_NET_TX_DESCRIPTORS_MIN) {
2269 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MIN;
2270 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2271 }
2272 if (tx_descriptors > SPIDER_NET_TX_DESCRIPTORS_MAX) {
2273 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MAX;
2274 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2275 }
2276
2277 return pci_register_driver(&spider_net_driver);
2278}
2279
2280/**
2281 * spider_net_cleanup - exit function when driver is unloaded
2282 *
2283 * spider_net_cleanup unregisters the device driver
2284 */
2285static void __exit spider_net_cleanup(void)
2286{
2287 pci_unregister_driver(&spider_net_driver);
2288}
2289
2290module_init(spider_net_init);
2291module_exit(spider_net_cleanup);