blob: e4a9bdd8d77c37b12c0827c13b06db7687a2a413 [file] [log] [blame]
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001/*
Kou Ishizaki3342cf02007-02-20 16:36:14 -06002 * Network device driver for Cell Processor-Based Blade and Celleb platform
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07003 *
4 * (C) Copyright IBM Corp. 2005
Kou Ishizaki3342cf02007-02-20 16:36:14 -06005 * (C) Copyright 2006 TOSHIBA CORPORATION
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07006 *
7 * Authors : Utz Bacher <utz.bacher@de.ibm.com>
8 * Jens Osterkamp <Jens.Osterkamp@de.ibm.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
13 * any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
Jens Osterkampaaec0fa2005-09-05 15:19:29 -070025#include <linux/compiler.h>
26#include <linux/crc32.h>
27#include <linux/delay.h>
28#include <linux/etherdevice.h>
29#include <linux/ethtool.h>
30#include <linux/firmware.h>
31#include <linux/if_vlan.h>
Arnd Bergmann7c5c2202006-01-12 17:16:45 -050032#include <linux/in.h>
Jens Osterkampaaec0fa2005-09-05 15:19:29 -070033#include <linux/init.h>
34#include <linux/ioport.h>
35#include <linux/ip.h>
36#include <linux/kernel.h>
37#include <linux/mii.h>
38#include <linux/module.h>
39#include <linux/netdevice.h>
40#include <linux/device.h>
41#include <linux/pci.h>
42#include <linux/skbuff.h>
43#include <linux/slab.h>
44#include <linux/tcp.h>
45#include <linux/types.h>
Arnd Bergmann11f1a522006-01-12 17:16:44 -050046#include <linux/vmalloc.h>
Jens Osterkampaaec0fa2005-09-05 15:19:29 -070047#include <linux/wait.h>
48#include <linux/workqueue.h>
49#include <asm/bitops.h>
50#include <asm/pci-bridge.h>
51#include <net/checksum.h>
52
53#include "spider_net.h"
54
55MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com> and Jens Osterkamp " \
56 "<Jens.Osterkamp@de.ibm.com>");
57MODULE_DESCRIPTION("Spider Southbridge Gigabit Ethernet driver");
58MODULE_LICENSE("GPL");
Linas Vepstas90f10842006-10-10 15:56:04 -050059MODULE_VERSION(VERSION);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -070060
61static int rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_DEFAULT;
62static int tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_DEFAULT;
63
Linas Vepstase2874f22006-10-10 15:59:02 -050064module_param(rx_descriptors, int, 0444);
65module_param(tx_descriptors, int, 0444);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -070066
67MODULE_PARM_DESC(rx_descriptors, "number of descriptors used " \
68 "in rx chains");
69MODULE_PARM_DESC(tx_descriptors, "number of descriptors used " \
70 "in tx chain");
71
72char spider_net_driver_name[] = "spidernet";
73
74static struct pci_device_id spider_net_pci_tbl[] = {
75 { PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_SPIDER_NET,
76 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
77 { 0, }
78};
79
80MODULE_DEVICE_TABLE(pci, spider_net_pci_tbl);
81
82/**
83 * spider_net_read_reg - reads an SMMIO register of a card
84 * @card: device structure
85 * @reg: register to read from
86 *
87 * returns the content of the specified SMMIO register.
88 */
Jens Osterkampbdd01502006-07-13 11:54:08 +020089static inline u32
Jens Osterkampaaec0fa2005-09-05 15:19:29 -070090spider_net_read_reg(struct spider_net_card *card, u32 reg)
91{
Benjamin Herrenschmidt3bc0f402006-11-11 17:25:13 +110092 /* We use the powerpc specific variants instead of readl_be() because
93 * we know spidernet is not a real PCI device and we can thus avoid the
94 * performance hit caused by the PCI workarounds.
95 */
96 return in_be32(card->regs + reg);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -070097}
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{
Benjamin Herrenschmidt3bc0f402006-11-11 17:25:13 +1100108 /* We use the powerpc specific variants instead of writel_be() because
109 * we know spidernet is not a real PCI device and we can thus avoid the
110 * performance hit caused by the PCI workarounds.
111 */
112 out_be32(card->regs + reg, value);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700113}
114
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700115/** spider_net_write_phy - write to phy register
116 * @netdev: adapter to be written to
117 * @mii_id: id of MII
118 * @reg: PHY register
119 * @val: value to be written to phy register
120 *
121 * spider_net_write_phy_register writes to an arbitrary PHY
122 * register via the spider GPCWOPCMD register. We assume the queue does
123 * not run full (not more than 15 commands outstanding).
124 **/
125static void
126spider_net_write_phy(struct net_device *netdev, int mii_id,
127 int reg, int val)
128{
129 struct spider_net_card *card = netdev_priv(netdev);
130 u32 writevalue;
131
132 writevalue = ((u32)mii_id << 21) |
133 ((u32)reg << 16) | ((u32)val);
134
135 spider_net_write_reg(card, SPIDER_NET_GPCWOPCMD, writevalue);
136}
137
138/** spider_net_read_phy - read from phy register
139 * @netdev: network device to be read from
140 * @mii_id: id of MII
141 * @reg: PHY register
142 *
143 * Returns value read from PHY register
144 *
145 * spider_net_write_phy reads from an arbitrary PHY
146 * register via the spider GPCROPCMD register
147 **/
148static int
149spider_net_read_phy(struct net_device *netdev, int mii_id, int reg)
150{
151 struct spider_net_card *card = netdev_priv(netdev);
152 u32 readvalue;
153
154 readvalue = ((u32)mii_id << 21) | ((u32)reg << 16);
155 spider_net_write_reg(card, SPIDER_NET_GPCROPCMD, readvalue);
156
157 /* we don't use semaphores to wait for an SPIDER_NET_GPROPCMPINT
158 * interrupt, as we poll for the completion of the read operation
159 * in spider_net_read_phy. Should take about 50 us */
160 do {
161 readvalue = spider_net_read_reg(card, SPIDER_NET_GPCROPCMD);
162 } while (readvalue & SPIDER_NET_GPREXEC);
163
164 readvalue &= SPIDER_NET_GPRDAT_MASK;
165
166 return readvalue;
167}
168
169/**
Kou Ishizakiabdb66b2007-02-20 16:33:41 -0600170 * spider_net_setup_aneg - initial auto-negotiation setup
171 * @card: device structure
172 **/
173static void
174spider_net_setup_aneg(struct spider_net_card *card)
175{
176 struct mii_phy *phy = &card->phy;
177 u32 advertise = 0;
178 u16 bmcr, bmsr, stat1000, estat;
179
180 bmcr = spider_net_read_phy(card->netdev, phy->mii_id, MII_BMCR);
181 bmsr = spider_net_read_phy(card->netdev, phy->mii_id, MII_BMSR);
182 stat1000 = spider_net_read_phy(card->netdev, phy->mii_id, MII_STAT1000);
183 estat = spider_net_read_phy(card->netdev, phy->mii_id, MII_ESTATUS);
184
185 if (bmsr & BMSR_10HALF)
186 advertise |= ADVERTISED_10baseT_Half;
187 if (bmsr & BMSR_10FULL)
188 advertise |= ADVERTISED_10baseT_Full;
189 if (bmsr & BMSR_100HALF)
190 advertise |= ADVERTISED_100baseT_Half;
191 if (bmsr & BMSR_100FULL)
192 advertise |= ADVERTISED_100baseT_Full;
193
194 if ((bmsr & BMSR_ESTATEN) && (estat & ESTATUS_1000_TFULL))
195 advertise |= SUPPORTED_1000baseT_Full;
196 if ((bmsr & BMSR_ESTATEN) && (estat & ESTATUS_1000_THALF))
197 advertise |= SUPPORTED_1000baseT_Half;
198
199 mii_phy_probe(phy, phy->mii_id);
200 phy->def->ops->setup_aneg(phy, advertise);
201
202}
203
204/**
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500205 * spider_net_rx_irq_off - switch off rx irq on this spider card
206 * @card: device structure
207 *
208 * switches off rx irq by masking them out in the GHIINTnMSK register
209 */
210static void
211spider_net_rx_irq_off(struct spider_net_card *card)
212{
213 u32 regvalue;
214
215 regvalue = SPIDER_NET_INT0_MASK_VALUE & (~SPIDER_NET_RXINT);
216 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, regvalue);
217}
218
219/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700220 * spider_net_rx_irq_on - switch on rx irq on this spider card
221 * @card: device structure
222 *
223 * switches on rx irq by enabling them in the GHIINTnMSK register
224 */
225static void
226spider_net_rx_irq_on(struct spider_net_card *card)
227{
228 u32 regvalue;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700229
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500230 regvalue = SPIDER_NET_INT0_MASK_VALUE | SPIDER_NET_RXINT;
231 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, regvalue);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700232}
233
234/**
235 * spider_net_set_promisc - sets the unicast address or the promiscuous mode
236 * @card: card structure
237 *
238 * spider_net_set_promisc sets the unicast destination address filter and
239 * thus either allows for non-promisc mode or promisc mode
240 */
241static void
242spider_net_set_promisc(struct spider_net_card *card)
243{
244 u32 macu, macl;
245 struct net_device *netdev = card->netdev;
246
247 if (netdev->flags & IFF_PROMISC) {
248 /* clear destination entry 0 */
249 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR, 0);
250 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR + 0x04, 0);
251 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R,
252 SPIDER_NET_PROMISC_VALUE);
253 } else {
254 macu = netdev->dev_addr[0];
255 macu <<= 8;
256 macu |= netdev->dev_addr[1];
257 memcpy(&macl, &netdev->dev_addr[2], sizeof(macl));
258
259 macu |= SPIDER_NET_UA_DESCR_VALUE;
260 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR, macu);
261 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR + 0x04, macl);
262 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R,
263 SPIDER_NET_NONPROMISC_VALUE);
264 }
265}
266
267/**
268 * spider_net_get_mac_address - read mac address from spider card
269 * @card: device structure
270 *
271 * reads MAC address from GMACUNIMACU and GMACUNIMACL registers
272 */
273static int
274spider_net_get_mac_address(struct net_device *netdev)
275{
276 struct spider_net_card *card = netdev_priv(netdev);
277 u32 macl, macu;
278
279 macl = spider_net_read_reg(card, SPIDER_NET_GMACUNIMACL);
280 macu = spider_net_read_reg(card, SPIDER_NET_GMACUNIMACU);
281
282 netdev->dev_addr[0] = (macu >> 24) & 0xff;
283 netdev->dev_addr[1] = (macu >> 16) & 0xff;
284 netdev->dev_addr[2] = (macu >> 8) & 0xff;
285 netdev->dev_addr[3] = macu & 0xff;
286 netdev->dev_addr[4] = (macl >> 8) & 0xff;
287 netdev->dev_addr[5] = macl & 0xff;
288
289 if (!is_valid_ether_addr(&netdev->dev_addr[0]))
290 return -EINVAL;
291
292 return 0;
293}
294
295/**
296 * spider_net_get_descr_status -- returns the status of a descriptor
297 * @descr: descriptor to look at
298 *
299 * returns the status as in the dmac_cmd_status field of the descriptor
300 */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200301static inline int
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700302spider_net_get_descr_status(struct spider_net_descr *descr)
303{
Jens Osterkampbdd01502006-07-13 11:54:08 +0200304 return descr->dmac_cmd_status & SPIDER_NET_DESCR_IND_PROC_MASK;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700305}
306
307/**
308 * spider_net_free_chain - free descriptor chain
309 * @card: card structure
310 * @chain: address of chain
311 *
312 */
313static void
314spider_net_free_chain(struct spider_net_card *card,
315 struct spider_net_descr_chain *chain)
316{
317 struct spider_net_descr *descr;
318
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600319 descr = chain->ring;
320 do {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700321 descr->bus_addr = 0;
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600322 descr->next_descr_addr = 0;
323 descr = descr->next;
324 } while (descr != chain->ring);
325
326 dma_free_coherent(&card->pdev->dev, chain->num_desc,
327 chain->ring, chain->dma_addr);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700328}
329
330/**
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600331 * spider_net_init_chain - alloc and link descriptor chain
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700332 * @card: card structure
333 * @chain: address of chain
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700334 *
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600335 * We manage a circular list that mirrors the hardware structure,
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700336 * except that the hardware uses bus addresses.
337 *
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600338 * Returns 0 on success, <0 on failure
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700339 */
340static int
341spider_net_init_chain(struct spider_net_card *card,
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600342 struct spider_net_descr_chain *chain)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700343{
344 int i;
345 struct spider_net_descr *descr;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500346 dma_addr_t buf;
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600347 size_t alloc_size;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700348
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600349 alloc_size = chain->num_desc * sizeof (struct spider_net_descr);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700350
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600351 chain->ring = dma_alloc_coherent(&card->pdev->dev, alloc_size,
352 &chain->dma_addr, GFP_KERNEL);
353
354 if (!chain->ring)
355 return -ENOMEM;
356
357 descr = chain->ring;
358 memset(descr, 0, alloc_size);
359
360 /* Set up the hardware pointers in each descriptor */
361 buf = chain->dma_addr;
362 for (i=0; i < chain->num_desc; i++, descr++) {
Jens Osterkampbdd01502006-07-13 11:54:08 +0200363 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700364
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500365 descr->bus_addr = buf;
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600366 descr->next_descr_addr = 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700367 descr->next = descr + 1;
368 descr->prev = descr - 1;
369
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600370 buf += sizeof(struct spider_net_descr);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700371 }
372 /* do actual circular list */
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600373 (descr-1)->next = chain->ring;
374 chain->ring->prev = descr-1;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700375
Jens Osterkampbdd01502006-07-13 11:54:08 +0200376 spin_lock_init(&chain->lock);
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600377 chain->head = chain->ring;
378 chain->tail = chain->ring;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700379 return 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700380}
381
382/**
383 * spider_net_free_rx_chain_contents - frees descr contents in rx chain
384 * @card: card structure
385 *
386 * returns 0 on success, <0 on failure
387 */
388static void
389spider_net_free_rx_chain_contents(struct spider_net_card *card)
390{
391 struct spider_net_descr *descr;
392
393 descr = card->rx_chain.head;
Linas Vepstas64751912006-10-10 16:22:29 -0500394 do {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700395 if (descr->skb) {
396 dev_kfree_skb(descr->skb);
397 pci_unmap_single(card->pdev, descr->buf_addr,
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500398 SPIDER_NET_MAX_FRAME,
Linas Vepstas348bc2a2006-10-10 16:21:10 -0500399 PCI_DMA_BIDIRECTIONAL);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700400 }
401 descr = descr->next;
Linas Vepstas64751912006-10-10 16:22:29 -0500402 } while (descr != card->rx_chain.head);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700403}
404
405/**
Linas Vepstasa4182c52006-12-13 15:19:54 -0600406 * spider_net_prepare_rx_descr - Reinitialize RX descriptor
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700407 * @card: card structure
408 * @descr: descriptor to re-init
409 *
Linas Vepstasa4182c52006-12-13 15:19:54 -0600410 * Return 0 on succes, <0 on failure.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700411 *
Linas Vepstasa4182c52006-12-13 15:19:54 -0600412 * Allocates a new rx skb, iommu-maps it and attaches it to the
413 * descriptor. Mark the descriptor as activated, ready-to-use.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700414 */
415static int
416spider_net_prepare_rx_descr(struct spider_net_card *card,
417 struct spider_net_descr *descr)
418{
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500419 dma_addr_t buf;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700420 int offset;
421 int bufsize;
422
423 /* we need to round up the buffer size to a multiple of 128 */
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500424 bufsize = (SPIDER_NET_MAX_FRAME + SPIDER_NET_RXBUF_ALIGN - 1) &
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700425 (~(SPIDER_NET_RXBUF_ALIGN - 1));
426
427 /* and we need to have it 128 byte aligned, therefore we allocate a
428 * bit more */
429 /* allocate an skb */
430 descr->skb = dev_alloc_skb(bufsize + SPIDER_NET_RXBUF_ALIGN - 1);
431 if (!descr->skb) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500432 if (netif_msg_rx_err(card) && net_ratelimit())
433 pr_err("Not enough memory to allocate rx buffer\n");
Jim Lewis9b6b0b82006-09-22 17:22:53 -0700434 card->spider_stats.alloc_rx_skb_error++;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700435 return -ENOMEM;
436 }
437 descr->buf_size = bufsize;
438 descr->result_size = 0;
439 descr->valid_size = 0;
440 descr->data_status = 0;
441 descr->data_error = 0;
442
443 offset = ((unsigned long)descr->skb->data) &
444 (SPIDER_NET_RXBUF_ALIGN - 1);
445 if (offset)
446 skb_reserve(descr->skb, SPIDER_NET_RXBUF_ALIGN - offset);
Linas Vepstasa4182c52006-12-13 15:19:54 -0600447 /* iommu-map the skb */
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500448 buf = pci_map_single(card->pdev, descr->skb->data,
Jens Osterkampbdd01502006-07-13 11:54:08 +0200449 SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500450 descr->buf_addr = buf;
Olof Johanssond4b0a4c2006-09-27 14:02:26 -0500451 if (pci_dma_mapping_error(buf)) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700452 dev_kfree_skb_any(descr->skb);
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500453 if (netif_msg_rx_err(card) && net_ratelimit())
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700454 pr_err("Could not iommu-map rx buffer\n");
Jim Lewis9b6b0b82006-09-22 17:22:53 -0700455 card->spider_stats.rx_iommu_map_error++;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200456 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700457 } else {
Linas Vepstasd9a97202007-02-05 16:29:43 -0800458 descr->next_descr_addr = 0;
Linas Vepstas90476a22006-12-13 15:22:04 -0600459 wmb();
Jens Osterkampbdd01502006-07-13 11:54:08 +0200460 descr->dmac_cmd_status = SPIDER_NET_DESCR_CARDOWNED |
461 SPIDER_NET_DMAC_NOINTR_COMPLETE;
Linas Vepstasd9a97202007-02-05 16:29:43 -0800462
463 wmb();
464 descr->prev->next_descr_addr = descr->bus_addr;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700465 }
466
Linas Vepstasa4182c52006-12-13 15:19:54 -0600467 return 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700468}
469
470/**
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500471 * spider_net_enable_rxchtails - sets RX dmac chain tail addresses
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700472 * @card: card structure
473 *
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500474 * spider_net_enable_rxchtails sets the RX DMAC chain tail adresses in the
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700475 * chip by writing to the appropriate register. DMA is enabled in
476 * spider_net_enable_rxdmac.
477 */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200478static inline void
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700479spider_net_enable_rxchtails(struct spider_net_card *card)
480{
481 /* assume chain is aligned correctly */
482 spider_net_write_reg(card, SPIDER_NET_GDADCHA ,
483 card->rx_chain.tail->bus_addr);
484}
485
486/**
487 * spider_net_enable_rxdmac - enables a receive DMA controller
488 * @card: card structure
489 *
490 * spider_net_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
491 * in the GDADMACCNTR register
492 */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200493static inline void
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700494spider_net_enable_rxdmac(struct spider_net_card *card)
495{
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500496 wmb();
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700497 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
498 SPIDER_NET_DMA_RX_VALUE);
499}
500
501/**
502 * spider_net_refill_rx_chain - refills descriptors/skbs in the rx chains
503 * @card: card structure
504 *
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500505 * refills descriptors in the rx chain: allocates skbs and iommu-maps them.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700506 */
507static void
508spider_net_refill_rx_chain(struct spider_net_card *card)
509{
Jens Osterkampbdd01502006-07-13 11:54:08 +0200510 struct spider_net_descr_chain *chain = &card->rx_chain;
511 unsigned long flags;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700512
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500513 /* one context doing the refill (and a second context seeing that
514 * and omitting it) is ok. If called by NAPI, we'll be called again
515 * as spider_net_decode_one_descr is called several times. If some
516 * interrupt calls us, the NAPI is about to clean up anyway. */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200517 if (!spin_trylock_irqsave(&chain->lock, flags))
518 return;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700519
Jens Osterkampbdd01502006-07-13 11:54:08 +0200520 while (spider_net_get_descr_status(chain->head) ==
521 SPIDER_NET_DESCR_NOT_IN_USE) {
522 if (spider_net_prepare_rx_descr(card, chain->head))
523 break;
524 chain->head = chain->head->next;
525 }
526
527 spin_unlock_irqrestore(&chain->lock, flags);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700528}
529
530/**
Linas Vepstas2c307db2006-12-13 15:20:59 -0600531 * spider_net_alloc_rx_skbs - Allocates rx skbs in rx descriptor chains
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700532 * @card: card structure
533 *
Linas Vepstas2c307db2006-12-13 15:20:59 -0600534 * Returns 0 on success, <0 on failure.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700535 */
536static int
537spider_net_alloc_rx_skbs(struct spider_net_card *card)
538{
539 int result;
540 struct spider_net_descr_chain *chain;
541
542 result = -ENOMEM;
543
544 chain = &card->rx_chain;
Linas Vepstas2c307db2006-12-13 15:20:59 -0600545 /* Put at least one buffer into the chain. if this fails,
546 * we've got a problem. If not, spider_net_refill_rx_chain
547 * will do the rest at the end of this function. */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700548 if (spider_net_prepare_rx_descr(card, chain->head))
549 goto error;
550 else
551 chain->head = chain->head->next;
552
Linas Vepstas2c307db2006-12-13 15:20:59 -0600553 /* This will allocate the rest of the rx buffers;
554 * if not, it's business as usual later on. */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700555 spider_net_refill_rx_chain(card);
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500556 spider_net_enable_rxdmac(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700557 return 0;
558
559error:
560 spider_net_free_rx_chain_contents(card);
561 return result;
562}
563
564/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700565 * spider_net_get_multicast_hash - generates hash for multicast filter table
566 * @addr: multicast address
567 *
568 * returns the hash value.
569 *
570 * spider_net_get_multicast_hash calculates a hash value for a given multicast
571 * address, that is used to set the multicast filter tables
572 */
573static u8
574spider_net_get_multicast_hash(struct net_device *netdev, __u8 *addr)
575{
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700576 u32 crc;
577 u8 hash;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500578 char addr_for_crc[ETH_ALEN] = { 0, };
579 int i, bit;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700580
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500581 for (i = 0; i < ETH_ALEN * 8; i++) {
582 bit = (addr[i / 8] >> (i % 8)) & 1;
583 addr_for_crc[ETH_ALEN - 1 - i / 8] += bit << (7 - (i % 8));
584 }
585
586 crc = crc32_be(~0, addr_for_crc, netdev->addr_len);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700587
588 hash = (crc >> 27);
589 hash <<= 3;
590 hash |= crc & 7;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500591 hash &= 0xff;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700592
593 return hash;
594}
595
596/**
597 * spider_net_set_multi - sets multicast addresses and promisc flags
598 * @netdev: interface device structure
599 *
600 * spider_net_set_multi configures multicast addresses as needed for the
601 * netdev interface. It also sets up multicast, allmulti and promisc
602 * flags appropriately
603 */
604static void
605spider_net_set_multi(struct net_device *netdev)
606{
607 struct dev_mc_list *mc;
608 u8 hash;
609 int i;
610 u32 reg;
611 struct spider_net_card *card = netdev_priv(netdev);
612 unsigned long bitmask[SPIDER_NET_MULTICAST_HASHES / BITS_PER_LONG] =
613 {0, };
614
615 spider_net_set_promisc(card);
616
617 if (netdev->flags & IFF_ALLMULTI) {
618 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES; i++) {
619 set_bit(i, bitmask);
620 }
621 goto write_hash;
622 }
623
624 /* well, we know, what the broadcast hash value is: it's xfd
625 hash = spider_net_get_multicast_hash(netdev, netdev->broadcast); */
626 set_bit(0xfd, bitmask);
627
628 for (mc = netdev->mc_list; mc; mc = mc->next) {
629 hash = spider_net_get_multicast_hash(netdev, mc->dmi_addr);
630 set_bit(hash, bitmask);
631 }
632
633write_hash:
634 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES / 4; i++) {
635 reg = 0;
636 if (test_bit(i * 4, bitmask))
637 reg += 0x08;
638 reg <<= 8;
639 if (test_bit(i * 4 + 1, bitmask))
640 reg += 0x08;
641 reg <<= 8;
642 if (test_bit(i * 4 + 2, bitmask))
643 reg += 0x08;
644 reg <<= 8;
645 if (test_bit(i * 4 + 3, bitmask))
646 reg += 0x08;
647
648 spider_net_write_reg(card, SPIDER_NET_GMRMHFILnR + i * 4, reg);
649 }
650}
651
652/**
653 * spider_net_disable_rxdmac - disables the receive DMA controller
654 * @card: card structure
655 *
656 * spider_net_disable_rxdmac terminates processing on the DMA controller by
657 * turing off DMA and issueing a force end
658 */
659static void
660spider_net_disable_rxdmac(struct spider_net_card *card)
661{
662 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
663 SPIDER_NET_DMA_RX_FEND_VALUE);
664}
665
666/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700667 * spider_net_prepare_tx_descr - fill tx descriptor with skb data
668 * @card: card structure
669 * @descr: descriptor structure to fill out
670 * @skb: packet to use
671 *
672 * returns 0 on success, <0 on failure.
673 *
674 * fills out the descriptor structure with skb data and len. Copies data,
675 * if needed (32bit DMA!)
676 */
677static int
678spider_net_prepare_tx_descr(struct spider_net_card *card,
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700679 struct sk_buff *skb)
680{
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500681 struct spider_net_descr *descr;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500682 dma_addr_t buf;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500683 unsigned long flags;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500684
James K Lewis9c434f52006-11-17 14:39:36 -0800685 buf = pci_map_single(card->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
Olof Johanssond4b0a4c2006-09-27 14:02:26 -0500686 if (pci_dma_mapping_error(buf)) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500687 if (netif_msg_tx_err(card) && net_ratelimit())
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700688 pr_err("could not iommu-map packet (%p, %i). "
James K Lewis9c434f52006-11-17 14:39:36 -0800689 "Dropping packet\n", skb->data, skb->len);
Jim Lewis9b6b0b82006-09-22 17:22:53 -0700690 card->spider_stats.tx_iommu_map_error++;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700691 return -ENOMEM;
692 }
693
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500694 spin_lock_irqsave(&card->tx_chain.lock, flags);
695 descr = card->tx_chain.head;
696 card->tx_chain.head = descr->next;
697
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500698 descr->buf_addr = buf;
James K Lewis9c434f52006-11-17 14:39:36 -0800699 descr->buf_size = skb->len;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200700 descr->next_descr_addr = 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700701 descr->skb = skb;
702 descr->data_status = 0;
703
Jens Osterkampbdd01502006-07-13 11:54:08 +0200704 descr->dmac_cmd_status =
705 SPIDER_NET_DESCR_CARDOWNED | SPIDER_NET_DMAC_NOCS;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500706 spin_unlock_irqrestore(&card->tx_chain.lock, flags);
707
Jens Osterkampbdd01502006-07-13 11:54:08 +0200708 if (skb->protocol == htons(ETH_P_IP))
709 switch (skb->nh.iph->protocol) {
710 case IPPROTO_TCP:
711 descr->dmac_cmd_status |= SPIDER_NET_DMAC_TCP;
712 break;
713 case IPPROTO_UDP:
714 descr->dmac_cmd_status |= SPIDER_NET_DMAC_UDP;
715 break;
716 }
717
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500718 /* Chain the bus address, so that the DMA engine finds this descr. */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200719 descr->prev->next_descr_addr = descr->bus_addr;
720
Linas Vepstas917a5b82006-10-10 16:01:51 -0500721 card->netdev->trans_start = jiffies; /* set netdev watchdog timer */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200722 return 0;
723}
724
Linas Vepstasa664ccf2006-10-10 16:18:18 -0500725static int
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500726spider_net_set_low_watermark(struct spider_net_card *card)
727{
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500728 unsigned long flags;
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500729 int status;
730 int cnt=0;
731 int i;
732 struct spider_net_descr *descr = card->tx_chain.tail;
733
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500734 /* Measure the length of the queue. Measurement does not
735 * need to be precise -- does not need a lock. */
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500736 while (descr != card->tx_chain.head) {
737 status = descr->dmac_cmd_status & SPIDER_NET_DESCR_NOT_IN_USE;
738 if (status == SPIDER_NET_DESCR_NOT_IN_USE)
739 break;
740 descr = descr->next;
741 cnt++;
742 }
743
744 /* If TX queue is short, don't even bother with interrupts */
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600745 if (cnt < card->tx_chain.num_desc/4)
Linas Vepstasa664ccf2006-10-10 16:18:18 -0500746 return cnt;
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500747
748 /* Set low-watermark 3/4th's of the way into the queue. */
749 descr = card->tx_chain.tail;
750 cnt = (cnt*3)/4;
751 for (i=0;i<cnt; i++)
752 descr = descr->next;
753
754 /* Set the new watermark, clear the old watermark */
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500755 spin_lock_irqsave(&card->tx_chain.lock, flags);
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500756 descr->dmac_cmd_status |= SPIDER_NET_DESCR_TXDESFLG;
757 if (card->low_watermark && card->low_watermark != descr)
758 card->low_watermark->dmac_cmd_status =
759 card->low_watermark->dmac_cmd_status & ~SPIDER_NET_DESCR_TXDESFLG;
760 card->low_watermark = descr;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500761 spin_unlock_irqrestore(&card->tx_chain.lock, flags);
Linas Vepstasa664ccf2006-10-10 16:18:18 -0500762 return cnt;
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500763}
764
Jens Osterkampbdd01502006-07-13 11:54:08 +0200765/**
766 * spider_net_release_tx_chain - processes sent tx descriptors
767 * @card: adapter structure
768 * @brutal: if set, don't care about whether descriptor seems to be in use
769 *
770 * returns 0 if the tx ring is empty, otherwise 1.
771 *
772 * spider_net_release_tx_chain releases the tx descriptors that spider has
773 * finished with (if non-brutal) or simply release tx descriptors (if brutal).
774 * If some other context is calling this function, we return 1 so that we're
775 * scheduled again (if we were scheduled) and will not loose initiative.
776 */
777static int
778spider_net_release_tx_chain(struct spider_net_card *card, int brutal)
779{
780 struct spider_net_descr_chain *chain = &card->tx_chain;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500781 struct spider_net_descr *descr;
782 struct sk_buff *skb;
783 u32 buf_addr;
784 unsigned long flags;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200785 int status;
786
Jens Osterkampbdd01502006-07-13 11:54:08 +0200787 while (chain->tail != chain->head) {
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500788 spin_lock_irqsave(&chain->lock, flags);
789 descr = chain->tail;
790
791 status = spider_net_get_descr_status(descr);
Jens Osterkampbdd01502006-07-13 11:54:08 +0200792 switch (status) {
793 case SPIDER_NET_DESCR_COMPLETE:
794 card->netdev_stats.tx_packets++;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500795 card->netdev_stats.tx_bytes += descr->skb->len;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200796 break;
797
798 case SPIDER_NET_DESCR_CARDOWNED:
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500799 if (!brutal) {
800 spin_unlock_irqrestore(&chain->lock, flags);
Jens Osterkampbdd01502006-07-13 11:54:08 +0200801 return 1;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500802 }
803
Jens Osterkampbdd01502006-07-13 11:54:08 +0200804 /* fallthrough, if we release the descriptors
805 * brutally (then we don't care about
806 * SPIDER_NET_DESCR_CARDOWNED) */
807
808 case SPIDER_NET_DESCR_RESPONSE_ERROR:
809 case SPIDER_NET_DESCR_PROTECTION_ERROR:
810 case SPIDER_NET_DESCR_FORCE_END:
811 if (netif_msg_tx_err(card))
812 pr_err("%s: forcing end of tx descriptor "
813 "with status x%02x\n",
814 card->netdev->name, status);
815 card->netdev_stats.tx_errors++;
816 break;
817
818 default:
819 card->netdev_stats.tx_dropped++;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500820 if (!brutal) {
821 spin_unlock_irqrestore(&chain->lock, flags);
Linas Vepstasc3fee4c2006-10-10 16:00:04 -0500822 return 1;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500823 }
Jens Osterkampbdd01502006-07-13 11:54:08 +0200824 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700825
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500826 chain->tail = descr->next;
827 descr->dmac_cmd_status |= SPIDER_NET_DESCR_NOT_IN_USE;
828 skb = descr->skb;
829 buf_addr = descr->buf_addr;
830 spin_unlock_irqrestore(&chain->lock, flags);
831
832 /* unmap the skb */
833 if (skb) {
James K Lewis9c434f52006-11-17 14:39:36 -0800834 pci_unmap_single(card->pdev, buf_addr, skb->len,
835 PCI_DMA_TODEVICE);
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500836 dev_kfree_skb(skb);
837 }
838 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700839 return 0;
840}
841
842/**
843 * spider_net_kick_tx_dma - enables TX DMA processing
844 * @card: card structure
845 * @descr: descriptor address to enable TX processing at
846 *
Linas Vepstasa664ccf2006-10-10 16:18:18 -0500847 * This routine will start the transmit DMA running if
848 * it is not already running. This routine ned only be
849 * called when queueing a new packet to an empty tx queue.
850 * Writes the current tx chain head as start address
851 * of the tx descriptor chain and enables the transmission
852 * DMA engine.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700853 */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200854static inline void
855spider_net_kick_tx_dma(struct spider_net_card *card)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700856{
Jens Osterkampbdd01502006-07-13 11:54:08 +0200857 struct spider_net_descr *descr;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700858
Jens Osterkampbdd01502006-07-13 11:54:08 +0200859 if (spider_net_read_reg(card, SPIDER_NET_GDTDMACCNTR) &
860 SPIDER_NET_TX_DMA_EN)
861 goto out;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700862
Jens Osterkampbdd01502006-07-13 11:54:08 +0200863 descr = card->tx_chain.tail;
864 for (;;) {
865 if (spider_net_get_descr_status(descr) ==
866 SPIDER_NET_DESCR_CARDOWNED) {
867 spider_net_write_reg(card, SPIDER_NET_GDTDCHA,
868 descr->bus_addr);
869 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
870 SPIDER_NET_DMA_TX_VALUE);
871 break;
872 }
873 if (descr == card->tx_chain.head)
874 break;
875 descr = descr->next;
876 }
877
878out:
879 mod_timer(&card->tx_timer, jiffies + SPIDER_NET_TX_TIMER);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700880}
881
882/**
883 * spider_net_xmit - transmits a frame over the device
884 * @skb: packet to send out
885 * @netdev: interface device structure
886 *
Jens Osterkampbdd01502006-07-13 11:54:08 +0200887 * returns 0 on success, !0 on failure
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700888 */
889static int
890spider_net_xmit(struct sk_buff *skb, struct net_device *netdev)
891{
Linas Vepstasa664ccf2006-10-10 16:18:18 -0500892 int cnt;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700893 struct spider_net_card *card = netdev_priv(netdev);
Jens Osterkampbdd01502006-07-13 11:54:08 +0200894 struct spider_net_descr_chain *chain = &card->tx_chain;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200895
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500896 spider_net_release_tx_chain(card, 0);
897
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500898 if ((chain->head->next == chain->tail->prev) ||
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500899 (spider_net_prepare_tx_descr(card, skb) != 0)) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500900
Jim Lewis9b6b0b82006-09-22 17:22:53 -0700901 card->netdev_stats.tx_dropped++;
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500902 netif_stop_queue(netdev);
903 return NETDEV_TX_BUSY;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200904 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700905
Linas Vepstasa664ccf2006-10-10 16:18:18 -0500906 cnt = spider_net_set_low_watermark(card);
907 if (cnt < 5)
908 spider_net_kick_tx_dma(card);
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500909 return NETDEV_TX_OK;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200910}
911
912/**
913 * spider_net_cleanup_tx_ring - cleans up the TX ring
914 * @card: card structure
915 *
Linas Vepstas68a8c602006-10-10 16:13:05 -0500916 * spider_net_cleanup_tx_ring is called by either the tx_timer
917 * or from the NAPI polling routine.
918 * This routine releases resources associted with transmitted
919 * packets, including updating the queue tail pointer.
Jens Osterkampbdd01502006-07-13 11:54:08 +0200920 */
921static void
922spider_net_cleanup_tx_ring(struct spider_net_card *card)
923{
Jens Osterkampbdd01502006-07-13 11:54:08 +0200924 if ((spider_net_release_tx_chain(card, 0) != 0) &&
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500925 (card->netdev->flags & IFF_UP)) {
Jens Osterkampbdd01502006-07-13 11:54:08 +0200926 spider_net_kick_tx_dma(card);
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500927 netif_wake_queue(card->netdev);
928 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700929}
930
931/**
932 * spider_net_do_ioctl - called for device ioctls
933 * @netdev: interface device structure
934 * @ifr: request parameter structure for ioctl
935 * @cmd: command code for ioctl
936 *
937 * returns 0 on success, <0 on failure. Currently, we have no special ioctls.
938 * -EOPNOTSUPP is returned, if an unknown ioctl was requested
939 */
940static int
941spider_net_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
942{
943 switch (cmd) {
944 default:
945 return -EOPNOTSUPP;
946 }
947}
948
949/**
950 * spider_net_pass_skb_up - takes an skb from a descriptor and passes it on
951 * @descr: descriptor to process
952 * @card: card structure
953 *
Linas Vepstas05b346b2006-12-13 15:15:15 -0600954 * Fills out skb structure and passes the data to the stack.
955 * The descriptor state is not changed.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700956 */
Linas Vepstas7f7223b2006-12-13 15:17:39 -0600957static void
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700958spider_net_pass_skb_up(struct spider_net_descr *descr,
Linas Vepstas1cd173f2006-12-13 15:12:26 -0600959 struct spider_net_card *card)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700960{
961 struct sk_buff *skb;
962 struct net_device *netdev;
963 u32 data_status, data_error;
964
965 data_status = descr->data_status;
966 data_error = descr->data_error;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700967 netdev = card->netdev;
968
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500969 skb = descr->skb;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700970 skb->dev = netdev;
971 skb_put(skb, descr->valid_size);
972
973 /* the card seems to add 2 bytes of junk in front
974 * of the ethernet frame */
975#define SPIDER_MISALIGN 2
976 skb_pull(skb, SPIDER_MISALIGN);
977 skb->protocol = eth_type_trans(skb, netdev);
978
979 /* checksum offload */
980 if (card->options.rx_csum) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500981 if ( ( (data_status & SPIDER_NET_DATA_STATUS_CKSUM_MASK) ==
982 SPIDER_NET_DATA_STATUS_CKSUM_MASK) &&
983 !(data_error & SPIDER_NET_DATA_ERR_CKSUM_MASK))
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700984 skb->ip_summed = CHECKSUM_UNNECESSARY;
985 else
986 skb->ip_summed = CHECKSUM_NONE;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500987 } else
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700988 skb->ip_summed = CHECKSUM_NONE;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700989
990 if (data_status & SPIDER_NET_VLAN_PACKET) {
991 /* further enhancements: HW-accel VLAN
992 * vlan_hwaccel_receive_skb
993 */
994 }
995
996 /* pass skb up to stack */
Linas Vepstas1cd173f2006-12-13 15:12:26 -0600997 netif_receive_skb(skb);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700998
999 /* update netdevice statistics */
1000 card->netdev_stats.rx_packets++;
1001 card->netdev_stats.rx_bytes += skb->len;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001002}
1003
Linas Vepstas6d249982006-12-13 15:23:56 -06001004#ifdef DEBUG
1005static void show_rx_chain(struct spider_net_card *card)
1006{
1007 struct spider_net_descr_chain *chain = &card->rx_chain;
1008 struct spider_net_descr *start= chain->tail;
1009 struct spider_net_descr *descr= start;
1010 int status;
1011
1012 int cnt = 0;
1013 int cstat = spider_net_get_descr_status(descr);
1014 printk(KERN_INFO "RX chain tail at descr=%ld\n",
1015 (start - card->descr) - card->tx_chain.num_desc);
1016 status = cstat;
1017 do
1018 {
1019 status = spider_net_get_descr_status(descr);
1020 if (cstat != status) {
1021 printk(KERN_INFO "Have %d descrs with stat=x%08x\n", cnt, cstat);
1022 cstat = status;
1023 cnt = 0;
1024 }
1025 cnt ++;
1026 descr = descr->next;
1027 } while (descr != start);
1028 printk(KERN_INFO "Last %d descrs with stat=x%08x\n", cnt, cstat);
1029}
1030#endif
1031
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001032/**
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001033 * spider_net_decode_one_descr - processes an rx descriptor
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001034 * @card: card structure
1035 *
Linas Vepstas1cd173f2006-12-13 15:12:26 -06001036 * Returns 1 if a packet has been sent to the stack, otherwise 0
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001037 *
Linas Vepstas1cd173f2006-12-13 15:12:26 -06001038 * Processes an rx descriptor by iommu-unmapping the data buffer and passing
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001039 * the packet up to the stack. This function is called in softirq
1040 * context, e.g. either bottom half from interrupt or NAPI polling context
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001041 */
1042static int
Linas Vepstas1cd173f2006-12-13 15:12:26 -06001043spider_net_decode_one_descr(struct spider_net_card *card)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001044{
Jens Osterkampbdd01502006-07-13 11:54:08 +02001045 struct spider_net_descr_chain *chain = &card->rx_chain;
1046 struct spider_net_descr *descr = chain->tail;
1047 int status;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001048
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001049 status = spider_net_get_descr_status(descr);
1050
Linas Vepstas80dab7c2006-12-13 15:18:52 -06001051 /* Nothing in the descriptor, or ring must be empty */
1052 if ((status == SPIDER_NET_DESCR_CARDOWNED) ||
1053 (status == SPIDER_NET_DESCR_NOT_IN_USE))
Linas Vepstas1cd173f2006-12-13 15:12:26 -06001054 return 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001055
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001056 /* descriptor definitively used -- move on tail */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001057 chain->tail = descr->next;
1058
Linas Vepstas05b346b2006-12-13 15:15:15 -06001059 /* unmap descriptor */
1060 pci_unmap_single(card->pdev, descr->buf_addr,
1061 SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
1062
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001063 if ( (status == SPIDER_NET_DESCR_RESPONSE_ERROR) ||
1064 (status == SPIDER_NET_DESCR_PROTECTION_ERROR) ||
1065 (status == SPIDER_NET_DESCR_FORCE_END) ) {
1066 if (netif_msg_rx_err(card))
1067 pr_err("%s: dropping RX descriptor with state %d\n",
1068 card->netdev->name, status);
1069 card->netdev_stats.rx_dropped++;
Linas Vepstas7f7223b2006-12-13 15:17:39 -06001070 goto bad_desc;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001071 }
1072
1073 if ( (status != SPIDER_NET_DESCR_COMPLETE) &&
1074 (status != SPIDER_NET_DESCR_FRAME_END) ) {
Linas Vepstas5a028872006-12-13 15:08:25 -06001075 if (netif_msg_rx_err(card))
Linas Vepstas05b346b2006-12-13 15:15:15 -06001076 pr_err("%s: RX descriptor with unkown state %d\n",
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001077 card->netdev->name, status);
Linas Vepstas5a028872006-12-13 15:08:25 -06001078 card->spider_stats.rx_desc_unk_state++;
Linas Vepstas7f7223b2006-12-13 15:17:39 -06001079 goto bad_desc;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001080 }
1081
Linas Vepstas366684b2006-12-13 15:16:18 -06001082 /* The cases we'll throw away the packet immediately */
1083 if (descr->data_error & SPIDER_NET_DESTROY_RX_FLAGS) {
1084 if (netif_msg_rx_err(card))
1085 pr_err("%s: error in received descriptor found, "
1086 "data_status=x%08x, data_error=x%08x\n",
1087 card->netdev->name,
1088 descr->data_status, descr->data_error);
Linas Vepstas7f7223b2006-12-13 15:17:39 -06001089 goto bad_desc;
Linas Vepstas366684b2006-12-13 15:16:18 -06001090 }
1091
Linas Vepstas6d249982006-12-13 15:23:56 -06001092 if (descr->dmac_cmd_status & 0xfefe) {
1093 pr_err("%s: bad status, cmd_status=x%08x\n",
1094 card->netdev->name,
1095 descr->dmac_cmd_status);
1096 pr_err("buf_addr=x%08x\n", descr->buf_addr);
1097 pr_err("buf_size=x%08x\n", descr->buf_size);
1098 pr_err("next_descr_addr=x%08x\n", descr->next_descr_addr);
1099 pr_err("result_size=x%08x\n", descr->result_size);
1100 pr_err("valid_size=x%08x\n", descr->valid_size);
1101 pr_err("data_status=x%08x\n", descr->data_status);
1102 pr_err("data_error=x%08x\n", descr->data_error);
1103 pr_err("bus_addr=x%08x\n", descr->bus_addr);
1104 pr_err("which=%ld\n", descr - card->rx_chain.ring);
1105
1106 card->spider_stats.rx_desc_error++;
1107 goto bad_desc;
1108 }
1109
Linas Vepstas7f7223b2006-12-13 15:17:39 -06001110 /* Ok, we've got a packet in descr */
1111 spider_net_pass_skb_up(descr, card);
Linas Vepstas1cd173f2006-12-13 15:12:26 -06001112 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
Linas Vepstas7f7223b2006-12-13 15:17:39 -06001113 return 1;
1114
1115bad_desc:
1116 dev_kfree_skb_irq(descr->skb);
1117 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
1118 return 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001119}
1120
1121/**
1122 * spider_net_poll - NAPI poll function called by the stack to return packets
1123 * @netdev: interface device structure
1124 * @budget: number of packets we can pass to the stack at most
1125 *
1126 * returns 0 if no more packets available to the driver/stack. Returns 1,
1127 * if the quota is exceeded, but the driver has still packets.
1128 *
1129 * spider_net_poll returns all packets from the rx descriptors to the stack
1130 * (using netif_receive_skb). If all/enough packets are up, the driver
1131 * reenables interrupts and returns 0. If not, 1 is returned.
1132 */
1133static int
1134spider_net_poll(struct net_device *netdev, int *budget)
1135{
1136 struct spider_net_card *card = netdev_priv(netdev);
1137 int packets_to_do, packets_done = 0;
1138 int no_more_packets = 0;
1139
Linas Vepstas68a8c602006-10-10 16:13:05 -05001140 spider_net_cleanup_tx_ring(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001141 packets_to_do = min(*budget, netdev->quota);
1142
1143 while (packets_to_do) {
Linas Vepstas1cd173f2006-12-13 15:12:26 -06001144 if (spider_net_decode_one_descr(card)) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001145 packets_done++;
1146 packets_to_do--;
1147 } else {
1148 /* no more packets for the stack */
1149 no_more_packets = 1;
1150 break;
1151 }
1152 }
1153
1154 netdev->quota -= packets_done;
1155 *budget -= packets_done;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001156 spider_net_refill_rx_chain(card);
Linas Vepstas80dab7c2006-12-13 15:18:52 -06001157 spider_net_enable_rxdmac(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001158
1159 /* if all packets are in the stack, enable interrupts and return 0 */
1160 /* if not, return 1 */
1161 if (no_more_packets) {
1162 netif_rx_complete(netdev);
1163 spider_net_rx_irq_on(card);
1164 return 0;
1165 }
1166
1167 return 1;
1168}
1169
1170/**
1171 * spider_net_vlan_rx_reg - initializes VLAN structures in the driver and card
1172 * @netdev: interface device structure
1173 * @grp: vlan_group structure that is registered (NULL on destroying interface)
1174 */
1175static void
1176spider_net_vlan_rx_reg(struct net_device *netdev, struct vlan_group *grp)
1177{
1178 /* further enhancement... yet to do */
1179 return;
1180}
1181
1182/**
1183 * spider_net_vlan_rx_add - adds VLAN id to the card filter
1184 * @netdev: interface device structure
1185 * @vid: VLAN id to add
1186 */
1187static void
1188spider_net_vlan_rx_add(struct net_device *netdev, uint16_t vid)
1189{
1190 /* further enhancement... yet to do */
1191 /* add vid to card's VLAN filter table */
1192 return;
1193}
1194
1195/**
1196 * spider_net_vlan_rx_kill - removes VLAN id to the card filter
1197 * @netdev: interface device structure
1198 * @vid: VLAN id to remove
1199 */
1200static void
1201spider_net_vlan_rx_kill(struct net_device *netdev, uint16_t vid)
1202{
1203 /* further enhancement... yet to do */
1204 /* remove vid from card's VLAN filter table */
1205}
1206
1207/**
1208 * spider_net_get_stats - get interface statistics
1209 * @netdev: interface device structure
1210 *
1211 * returns the interface statistics residing in the spider_net_card struct
1212 */
1213static struct net_device_stats *
1214spider_net_get_stats(struct net_device *netdev)
1215{
1216 struct spider_net_card *card = netdev_priv(netdev);
1217 struct net_device_stats *stats = &card->netdev_stats;
1218 return stats;
1219}
1220
1221/**
1222 * spider_net_change_mtu - changes the MTU of an interface
1223 * @netdev: interface device structure
1224 * @new_mtu: new MTU value
1225 *
1226 * returns 0 on success, <0 on failure
1227 */
1228static int
1229spider_net_change_mtu(struct net_device *netdev, int new_mtu)
1230{
1231 /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
1232 * and mtu is outbound only anyway */
1233 if ( (new_mtu < SPIDER_NET_MIN_MTU ) ||
1234 (new_mtu > SPIDER_NET_MAX_MTU) )
1235 return -EINVAL;
1236 netdev->mtu = new_mtu;
1237 return 0;
1238}
1239
1240/**
1241 * spider_net_set_mac - sets the MAC of an interface
1242 * @netdev: interface device structure
1243 * @ptr: pointer to new MAC address
1244 *
1245 * Returns 0 on success, <0 on failure. Currently, we don't support this
1246 * and will always return EOPNOTSUPP.
1247 */
1248static int
1249spider_net_set_mac(struct net_device *netdev, void *p)
1250{
1251 struct spider_net_card *card = netdev_priv(netdev);
Jens Osterkamp054034d2005-09-06 19:30:55 -07001252 u32 macl, macu, regvalue;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001253 struct sockaddr *addr = p;
1254
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001255 if (!is_valid_ether_addr(addr->sa_data))
1256 return -EADDRNOTAVAIL;
1257
Jens Osterkamp054034d2005-09-06 19:30:55 -07001258 /* switch off GMACTPE and GMACRPE */
1259 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1260 regvalue &= ~((1 << 5) | (1 << 6));
1261 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1262
1263 /* write mac */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001264 macu = (addr->sa_data[0]<<24) + (addr->sa_data[1]<<16) +
1265 (addr->sa_data[2]<<8) + (addr->sa_data[3]);
1266 macl = (addr->sa_data[4]<<8) + (addr->sa_data[5]);
1267 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACU, macu);
1268 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACL, macl);
1269
Jens Osterkamp054034d2005-09-06 19:30:55 -07001270 /* switch GMACTPE and GMACRPE back on */
1271 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1272 regvalue |= ((1 << 5) | (1 << 6));
1273 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1274
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001275 spider_net_set_promisc(card);
1276
1277 /* look up, whether we have been successful */
1278 if (spider_net_get_mac_address(netdev))
1279 return -EADDRNOTAVAIL;
1280 if (memcmp(netdev->dev_addr,addr->sa_data,netdev->addr_len))
1281 return -EADDRNOTAVAIL;
1282
1283 return 0;
1284}
1285
1286/**
Kou Ishizakiabdb66b2007-02-20 16:33:41 -06001287 * spider_net_link_reset
1288 * @netdev: net device structure
1289 *
1290 * This is called when the PHY_LINK signal is asserted. For the blade this is
1291 * not connected so we should never get here.
1292 *
1293 */
1294static void
1295spider_net_link_reset(struct net_device *netdev)
1296{
1297
1298 struct spider_net_card *card = netdev_priv(netdev);
1299
1300 del_timer_sync(&card->aneg_timer);
1301
1302 /* clear interrupt, block further interrupts */
1303 spider_net_write_reg(card, SPIDER_NET_GMACST,
1304 spider_net_read_reg(card, SPIDER_NET_GMACST));
1305 spider_net_write_reg(card, SPIDER_NET_GMACINTEN, 0);
1306
1307 /* reset phy and setup aneg */
1308 spider_net_setup_aneg(card);
1309 mod_timer(&card->aneg_timer, jiffies + SPIDER_NET_ANEG_TIMER);
1310
1311}
1312
1313/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001314 * spider_net_handle_error_irq - handles errors raised by an interrupt
1315 * @card: card structure
1316 * @status_reg: interrupt status register 0 (GHIINT0STS)
1317 *
1318 * spider_net_handle_error_irq treats or ignores all error conditions
1319 * found when an interrupt is presented
1320 */
1321static void
1322spider_net_handle_error_irq(struct spider_net_card *card, u32 status_reg)
1323{
1324 u32 error_reg1, error_reg2;
1325 u32 i;
1326 int show_error = 1;
1327
1328 error_reg1 = spider_net_read_reg(card, SPIDER_NET_GHIINT1STS);
1329 error_reg2 = spider_net_read_reg(card, SPIDER_NET_GHIINT2STS);
1330
1331 /* check GHIINT0STS ************************************/
1332 if (status_reg)
1333 for (i = 0; i < 32; i++)
1334 if (status_reg & (1<<i))
1335 switch (i)
1336 {
1337 /* let error_reg1 and error_reg2 evaluation decide, what to do
1338 case SPIDER_NET_PHYINT:
1339 case SPIDER_NET_GMAC2INT:
1340 case SPIDER_NET_GMAC1INT:
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001341 case SPIDER_NET_GFIFOINT:
1342 case SPIDER_NET_DMACINT:
1343 case SPIDER_NET_GSYSINT:
1344 break; */
1345
Linas Vepstas98b90402006-10-10 16:08:42 -05001346 case SPIDER_NET_GIPSINT:
1347 show_error = 0;
1348 break;
1349
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001350 case SPIDER_NET_GPWOPCMPINT:
1351 /* PHY write operation completed */
1352 show_error = 0;
1353 break;
1354 case SPIDER_NET_GPROPCMPINT:
1355 /* PHY read operation completed */
1356 /* we don't use semaphores, as we poll for the completion
1357 * of the read operation in spider_net_read_phy. Should take
1358 * about 50 us */
1359 show_error = 0;
1360 break;
1361 case SPIDER_NET_GPWFFINT:
1362 /* PHY command queue full */
1363 if (netif_msg_intr(card))
1364 pr_err("PHY write queue full\n");
1365 show_error = 0;
1366 break;
1367
1368 /* case SPIDER_NET_GRMDADRINT: not used. print a message */
1369 /* case SPIDER_NET_GRMARPINT: not used. print a message */
1370 /* case SPIDER_NET_GRMMPINT: not used. print a message */
1371
1372 case SPIDER_NET_GDTDEN0INT:
1373 /* someone has set TX_DMA_EN to 0 */
1374 show_error = 0;
1375 break;
1376
1377 case SPIDER_NET_GDDDEN0INT: /* fallthrough */
1378 case SPIDER_NET_GDCDEN0INT: /* fallthrough */
1379 case SPIDER_NET_GDBDEN0INT: /* fallthrough */
1380 case SPIDER_NET_GDADEN0INT:
1381 /* someone has set RX_DMA_EN to 0 */
1382 show_error = 0;
1383 break;
1384
1385 /* RX interrupts */
1386 case SPIDER_NET_GDDFDCINT:
1387 case SPIDER_NET_GDCFDCINT:
1388 case SPIDER_NET_GDBFDCINT:
1389 case SPIDER_NET_GDAFDCINT:
1390 /* case SPIDER_NET_GDNMINT: not used. print a message */
1391 /* case SPIDER_NET_GCNMINT: not used. print a message */
1392 /* case SPIDER_NET_GBNMINT: not used. print a message */
1393 /* case SPIDER_NET_GANMINT: not used. print a message */
1394 /* case SPIDER_NET_GRFNMINT: not used. print a message */
1395 show_error = 0;
1396 break;
1397
1398 /* TX interrupts */
1399 case SPIDER_NET_GDTFDCINT:
1400 show_error = 0;
1401 break;
1402 case SPIDER_NET_GTTEDINT:
1403 show_error = 0;
1404 break;
1405 case SPIDER_NET_GDTDCEINT:
1406 /* chain end. If a descriptor should be sent, kick off
1407 * tx dma
Linas Vepstas98b90402006-10-10 16:08:42 -05001408 if (card->tx_chain.tail != card->tx_chain.head)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001409 spider_net_kick_tx_dma(card);
Linas Vepstas98b90402006-10-10 16:08:42 -05001410 */
1411 show_error = 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001412 break;
1413
1414 /* case SPIDER_NET_G1TMCNTINT: not used. print a message */
1415 /* case SPIDER_NET_GFREECNTINT: not used. print a message */
1416 }
1417
1418 /* check GHIINT1STS ************************************/
1419 if (error_reg1)
1420 for (i = 0; i < 32; i++)
1421 if (error_reg1 & (1<<i))
1422 switch (i)
1423 {
1424 case SPIDER_NET_GTMFLLINT:
Ishizaki Koufc8e13d2007-02-20 16:37:42 -06001425 /* TX RAM full may happen on a usual case.
1426 * Logging is not needed. */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001427 show_error = 0;
1428 break;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001429 case SPIDER_NET_GRFDFLLINT: /* fallthrough */
1430 case SPIDER_NET_GRFCFLLINT: /* fallthrough */
1431 case SPIDER_NET_GRFBFLLINT: /* fallthrough */
1432 case SPIDER_NET_GRFAFLLINT: /* fallthrough */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001433 case SPIDER_NET_GRMFLLINT:
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001434 if (netif_msg_intr(card) && net_ratelimit())
Linas Vepstas5a028872006-12-13 15:08:25 -06001435 pr_err("Spider RX RAM full, incoming packets "
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001436 "might be discarded!\n");
1437 spider_net_rx_irq_off(card);
Linas Vepstas75856172006-12-13 15:10:06 -06001438 netif_rx_schedule(card->netdev);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001439 show_error = 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001440 break;
1441
1442 /* case SPIDER_NET_GTMSHTINT: problem, print a message */
1443 case SPIDER_NET_GDTINVDINT:
1444 /* allrighty. tx from previous descr ok */
1445 show_error = 0;
1446 break;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001447
1448 /* chain end */
1449 case SPIDER_NET_GDDDCEINT: /* fallthrough */
1450 case SPIDER_NET_GDCDCEINT: /* fallthrough */
1451 case SPIDER_NET_GDBDCEINT: /* fallthrough */
1452 case SPIDER_NET_GDADCEINT:
Linas Vepstas5a028872006-12-13 15:08:25 -06001453 if (netif_msg_intr(card) && net_ratelimit())
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001454 pr_err("got descriptor chain end interrupt, "
1455 "restarting DMAC %c.\n",
Linas Vepstas37aad752006-10-10 16:06:53 -05001456 'D'-(i-SPIDER_NET_GDDDCEINT)/3);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001457 spider_net_refill_rx_chain(card);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001458 spider_net_enable_rxdmac(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001459 show_error = 0;
1460 break;
1461
1462 /* invalid descriptor */
1463 case SPIDER_NET_GDDINVDINT: /* fallthrough */
1464 case SPIDER_NET_GDCINVDINT: /* fallthrough */
1465 case SPIDER_NET_GDBINVDINT: /* fallthrough */
1466 case SPIDER_NET_GDAINVDINT:
1467 /* could happen when rx chain is full */
1468 spider_net_refill_rx_chain(card);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001469 spider_net_enable_rxdmac(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001470 show_error = 0;
1471 break;
1472
1473 /* case SPIDER_NET_GDTRSERINT: problem, print a message */
1474 /* case SPIDER_NET_GDDRSERINT: problem, print a message */
1475 /* case SPIDER_NET_GDCRSERINT: problem, print a message */
1476 /* case SPIDER_NET_GDBRSERINT: problem, print a message */
1477 /* case SPIDER_NET_GDARSERINT: problem, print a message */
1478 /* case SPIDER_NET_GDSERINT: problem, print a message */
1479 /* case SPIDER_NET_GDTPTERINT: problem, print a message */
1480 /* case SPIDER_NET_GDDPTERINT: problem, print a message */
1481 /* case SPIDER_NET_GDCPTERINT: problem, print a message */
1482 /* case SPIDER_NET_GDBPTERINT: problem, print a message */
1483 /* case SPIDER_NET_GDAPTERINT: problem, print a message */
1484 default:
1485 show_error = 1;
1486 break;
1487 }
1488
1489 /* check GHIINT2STS ************************************/
1490 if (error_reg2)
1491 for (i = 0; i < 32; i++)
1492 if (error_reg2 & (1<<i))
1493 switch (i)
1494 {
1495 /* there is nothing we can (want to) do at this time. Log a
1496 * message, we can switch on and off the specific values later on
1497 case SPIDER_NET_GPROPERINT:
1498 case SPIDER_NET_GMCTCRSNGINT:
1499 case SPIDER_NET_GMCTLCOLINT:
1500 case SPIDER_NET_GMCTTMOTINT:
1501 case SPIDER_NET_GMCRCAERINT:
1502 case SPIDER_NET_GMCRCALERINT:
1503 case SPIDER_NET_GMCRALNERINT:
1504 case SPIDER_NET_GMCROVRINT:
1505 case SPIDER_NET_GMCRRNTINT:
1506 case SPIDER_NET_GMCRRXERINT:
1507 case SPIDER_NET_GTITCSERINT:
1508 case SPIDER_NET_GTIFMTERINT:
1509 case SPIDER_NET_GTIPKTRVKINT:
1510 case SPIDER_NET_GTISPINGINT:
1511 case SPIDER_NET_GTISADNGINT:
1512 case SPIDER_NET_GTISPDNGINT:
1513 case SPIDER_NET_GRIFMTERINT:
1514 case SPIDER_NET_GRIPKTRVKINT:
1515 case SPIDER_NET_GRISPINGINT:
1516 case SPIDER_NET_GRISADNGINT:
1517 case SPIDER_NET_GRISPDNGINT:
1518 break;
1519 */
1520 default:
1521 break;
1522 }
1523
Linas Vepstas5a028872006-12-13 15:08:25 -06001524 if ((show_error) && (netif_msg_intr(card)) && net_ratelimit())
Linas Vepstas98b90402006-10-10 16:08:42 -05001525 pr_err("Got error interrupt on %s, GHIINT0STS = 0x%08x, "
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001526 "GHIINT1STS = 0x%08x, GHIINT2STS = 0x%08x\n",
Linas Vepstas98b90402006-10-10 16:08:42 -05001527 card->netdev->name,
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001528 status_reg, error_reg1, error_reg2);
1529
1530 /* clear interrupt sources */
1531 spider_net_write_reg(card, SPIDER_NET_GHIINT1STS, error_reg1);
1532 spider_net_write_reg(card, SPIDER_NET_GHIINT2STS, error_reg2);
1533}
1534
1535/**
1536 * spider_net_interrupt - interrupt handler for spider_net
1537 * @irq: interupt number
1538 * @ptr: pointer to net_device
1539 * @regs: PU registers
1540 *
1541 * returns IRQ_HANDLED, if interrupt was for driver, or IRQ_NONE, if no
1542 * interrupt found raised by card.
1543 *
1544 * This is the interrupt handler, that turns off
1545 * interrupts for this device and makes the stack poll the driver
1546 */
1547static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001548spider_net_interrupt(int irq, void *ptr)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001549{
1550 struct net_device *netdev = ptr;
1551 struct spider_net_card *card = netdev_priv(netdev);
1552 u32 status_reg;
1553
1554 status_reg = spider_net_read_reg(card, SPIDER_NET_GHIINT0STS);
1555
1556 if (!status_reg)
1557 return IRQ_NONE;
1558
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001559 if (status_reg & SPIDER_NET_RXINT ) {
1560 spider_net_rx_irq_off(card);
1561 netif_rx_schedule(netdev);
1562 }
Linas Vepstas68a8c602006-10-10 16:13:05 -05001563 if (status_reg & SPIDER_NET_TXINT)
1564 netif_rx_schedule(netdev);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001565
Kou Ishizakiabdb66b2007-02-20 16:33:41 -06001566 if (status_reg & SPIDER_NET_LINKINT)
1567 spider_net_link_reset(netdev);
1568
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001569 if (status_reg & SPIDER_NET_ERRINT )
1570 spider_net_handle_error_irq(card, status_reg);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001571
1572 /* clear interrupt sources */
1573 spider_net_write_reg(card, SPIDER_NET_GHIINT0STS, status_reg);
1574
1575 return IRQ_HANDLED;
1576}
1577
1578#ifdef CONFIG_NET_POLL_CONTROLLER
1579/**
1580 * spider_net_poll_controller - artificial interrupt for netconsole etc.
1581 * @netdev: interface device structure
1582 *
1583 * see Documentation/networking/netconsole.txt
1584 */
1585static void
1586spider_net_poll_controller(struct net_device *netdev)
1587{
1588 disable_irq(netdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01001589 spider_net_interrupt(netdev->irq, netdev);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001590 enable_irq(netdev->irq);
1591}
1592#endif /* CONFIG_NET_POLL_CONTROLLER */
1593
1594/**
1595 * spider_net_init_card - initializes the card
1596 * @card: card structure
1597 *
1598 * spider_net_init_card initializes the card so that other registers can
1599 * be used
1600 */
1601static void
1602spider_net_init_card(struct spider_net_card *card)
1603{
1604 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1605 SPIDER_NET_CKRCTRL_STOP_VALUE);
1606
1607 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1608 SPIDER_NET_CKRCTRL_RUN_VALUE);
Kou Ishizaki3342cf02007-02-20 16:36:14 -06001609
1610 /* trigger ETOMOD signal */
1611 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD,
1612 spider_net_read_reg(card, SPIDER_NET_GMACOPEMD) | 0x4);
1613
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001614}
1615
1616/**
1617 * spider_net_enable_card - enables the card by setting all kinds of regs
1618 * @card: card structure
1619 *
1620 * spider_net_enable_card sets a lot of SMMIO registers to enable the device
1621 */
1622static void
1623spider_net_enable_card(struct spider_net_card *card)
1624{
1625 int i;
1626 /* the following array consists of (register),(value) pairs
1627 * that are set in this function. A register of 0 ends the list */
1628 u32 regs[][2] = {
1629 { SPIDER_NET_GRESUMINTNUM, 0 },
1630 { SPIDER_NET_GREINTNUM, 0 },
1631
1632 /* set interrupt frame number registers */
1633 /* clear the single DMA engine registers first */
1634 { SPIDER_NET_GFAFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1635 { SPIDER_NET_GFBFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1636 { SPIDER_NET_GFCFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1637 { SPIDER_NET_GFDFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1638 /* then set, what we really need */
1639 { SPIDER_NET_GFFRMNUM, SPIDER_NET_FRAMENUM_VALUE },
1640
1641 /* timer counter registers and stuff */
1642 { SPIDER_NET_GFREECNNUM, 0 },
1643 { SPIDER_NET_GONETIMENUM, 0 },
1644 { SPIDER_NET_GTOUTFRMNUM, 0 },
1645
1646 /* RX mode setting */
1647 { SPIDER_NET_GRXMDSET, SPIDER_NET_RXMODE_VALUE },
1648 /* TX mode setting */
1649 { SPIDER_NET_GTXMDSET, SPIDER_NET_TXMODE_VALUE },
1650 /* IPSEC mode setting */
1651 { SPIDER_NET_GIPSECINIT, SPIDER_NET_IPSECINIT_VALUE },
1652
1653 { SPIDER_NET_GFTRESTRT, SPIDER_NET_RESTART_VALUE },
1654
1655 { SPIDER_NET_GMRWOLCTRL, 0 },
Jens Osterkampb636d172006-05-04 05:59:56 -04001656 { SPIDER_NET_GTESTMD, 0x10000000 },
1657 { SPIDER_NET_GTTQMSK, 0x00400040 },
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001658
1659 { SPIDER_NET_GMACINTEN, 0 },
1660
1661 /* flow control stuff */
1662 { SPIDER_NET_GMACAPAUSE, SPIDER_NET_MACAPAUSE_VALUE },
1663 { SPIDER_NET_GMACTXPAUSE, SPIDER_NET_TXPAUSE_VALUE },
1664
1665 { SPIDER_NET_GMACBSTLMT, SPIDER_NET_BURSTLMT_VALUE },
1666 { 0, 0}
1667 };
1668
1669 i = 0;
1670 while (regs[i][0]) {
1671 spider_net_write_reg(card, regs[i][0], regs[i][1]);
1672 i++;
1673 }
1674
1675 /* clear unicast filter table entries 1 to 14 */
1676 for (i = 1; i <= 14; i++) {
1677 spider_net_write_reg(card,
1678 SPIDER_NET_GMRUAFILnR + i * 8,
1679 0x00080000);
1680 spider_net_write_reg(card,
1681 SPIDER_NET_GMRUAFILnR + i * 8 + 4,
1682 0x00000000);
1683 }
1684
1685 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R, 0x08080000);
1686
1687 spider_net_write_reg(card, SPIDER_NET_ECMODE, SPIDER_NET_ECMODE_VALUE);
1688
1689 /* set chain tail adress for RX chains and
1690 * enable DMA */
1691 spider_net_enable_rxchtails(card);
1692 spider_net_enable_rxdmac(card);
1693
1694 spider_net_write_reg(card, SPIDER_NET_GRXDMAEN, SPIDER_NET_WOL_VALUE);
1695
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001696 spider_net_write_reg(card, SPIDER_NET_GMACLENLMT,
1697 SPIDER_NET_LENLMT_VALUE);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001698 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD,
1699 SPIDER_NET_OPMODE_VALUE);
1700
1701 /* set interrupt mask registers */
1702 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK,
1703 SPIDER_NET_INT0_MASK_VALUE);
1704 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK,
1705 SPIDER_NET_INT1_MASK_VALUE);
1706 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK,
1707 SPIDER_NET_INT2_MASK_VALUE);
Jens Osterkampbdd01502006-07-13 11:54:08 +02001708
1709 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
Linas Vepstas7bd54c82006-11-17 14:39:40 -08001710 SPIDER_NET_GDTBSTA);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001711}
1712
1713/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001714 * spider_net_download_firmware - loads firmware into the adapter
1715 * @card: card structure
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001716 * @firmware_ptr: pointer to firmware data
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001717 *
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001718 * spider_net_download_firmware loads the firmware data into the
1719 * adapter. It assumes the length etc. to be allright.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001720 */
Arnd Bergmann0d3ea162006-01-12 17:16:41 -05001721static int
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001722spider_net_download_firmware(struct spider_net_card *card,
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001723 const void *firmware_ptr)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001724{
1725 int sequencer, i;
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001726 const u32 *fw_ptr = firmware_ptr;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001727
1728 /* stop sequencers */
1729 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1730 SPIDER_NET_STOP_SEQ_VALUE);
1731
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001732 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1733 sequencer++) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001734 spider_net_write_reg(card,
1735 SPIDER_NET_GSnPRGADR + sequencer * 8, 0);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001736 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001737 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1738 sequencer * 8, *fw_ptr);
1739 fw_ptr++;
1740 }
1741 }
1742
Arnd Bergmann0d3ea162006-01-12 17:16:41 -05001743 if (spider_net_read_reg(card, SPIDER_NET_GSINIT))
1744 return -EIO;
1745
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001746 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1747 SPIDER_NET_RUN_SEQ_VALUE);
Arnd Bergmann0d3ea162006-01-12 17:16:41 -05001748
1749 return 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001750}
1751
1752/**
1753 * spider_net_init_firmware - reads in firmware parts
1754 * @card: card structure
1755 *
1756 * Returns 0 on success, <0 on failure
1757 *
1758 * spider_net_init_firmware opens the sequencer firmware and does some basic
1759 * checks. This function opens and releases the firmware structure. A call
1760 * to download the firmware is performed before the release.
1761 *
1762 * Firmware format
1763 * ===============
1764 * spider_fw.bin is expected to be a file containing 6*1024*4 bytes, 4k being
1765 * the program for each sequencer. Use the command
1766 * tail -q -n +2 Seq_code1_0x088.txt Seq_code2_0x090.txt \
1767 * Seq_code3_0x098.txt Seq_code4_0x0A0.txt Seq_code5_0x0A8.txt \
1768 * Seq_code6_0x0B0.txt | xxd -r -p -c4 > spider_fw.bin
1769 *
1770 * to generate spider_fw.bin, if you have sequencer programs with something
1771 * like the following contents for each sequencer:
1772 * <ONE LINE COMMENT>
1773 * <FIRST 4-BYTES-WORD FOR SEQUENCER>
1774 * <SECOND 4-BYTES-WORD FOR SEQUENCER>
1775 * ...
1776 * <1024th 4-BYTES-WORD FOR SEQUENCER>
1777 */
1778static int
1779spider_net_init_firmware(struct spider_net_card *card)
1780{
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001781 struct firmware *firmware = NULL;
Arnd Bergmann030d6752006-01-12 17:16:42 -05001782 struct device_node *dn;
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001783 const u8 *fw_prop = NULL;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001784 int err = -ENOENT;
1785 int fw_size;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001786
Arnd Bergmann030d6752006-01-12 17:16:42 -05001787 if (request_firmware((const struct firmware **)&firmware,
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001788 SPIDER_NET_FIRMWARE_NAME, &card->pdev->dev) == 0) {
1789 if ( (firmware->size != SPIDER_NET_FIRMWARE_LEN) &&
1790 netif_msg_probe(card) ) {
1791 pr_err("Incorrect size of spidernet firmware in " \
1792 "filesystem. Looking in host firmware...\n");
1793 goto try_host_fw;
1794 }
1795 err = spider_net_download_firmware(card, firmware->data);
Arnd Bergmann030d6752006-01-12 17:16:42 -05001796
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001797 release_firmware(firmware);
1798 if (err)
1799 goto try_host_fw;
Arnd Bergmann030d6752006-01-12 17:16:42 -05001800
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001801 goto done;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001802 }
1803
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001804try_host_fw:
1805 dn = pci_device_to_OF_node(card->pdev);
1806 if (!dn)
1807 goto out_err;
1808
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001809 fw_prop = get_property(dn, "firmware", &fw_size);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001810 if (!fw_prop)
1811 goto out_err;
1812
1813 if ( (fw_size != SPIDER_NET_FIRMWARE_LEN) &&
1814 netif_msg_probe(card) ) {
1815 pr_err("Incorrect size of spidernet firmware in " \
1816 "host firmware\n");
1817 goto done;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001818 }
1819
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001820 err = spider_net_download_firmware(card, fw_prop);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001821
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001822done:
1823 return err;
1824out_err:
1825 if (netif_msg_probe(card))
1826 pr_err("Couldn't find spidernet firmware in filesystem " \
1827 "or host firmware\n");
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001828 return err;
1829}
1830
1831/**
Kou Ishizaki3cf761d2007-02-20 16:34:50 -06001832 * spider_net_open - called upon ifonfig up
1833 * @netdev: interface device structure
1834 *
1835 * returns 0 on success, <0 on failure
1836 *
1837 * spider_net_open allocates all the descriptors and memory needed for
1838 * operation, sets up multicast list and enables interrupts
1839 */
1840int
1841spider_net_open(struct net_device *netdev)
1842{
1843 struct spider_net_card *card = netdev_priv(netdev);
1844 int result;
1845
1846 result = spider_net_init_firmware(card);
1847 if (result)
1848 goto init_firmware_failed;
1849
1850 /* start probing with copper */
1851 spider_net_setup_aneg(card);
1852 if (card->phy.def->phy_id)
1853 mod_timer(&card->aneg_timer, jiffies + SPIDER_NET_ANEG_TIMER);
1854
1855 result = spider_net_init_chain(card, &card->tx_chain);
1856 if (result)
1857 goto alloc_tx_failed;
1858 card->low_watermark = NULL;
1859
1860 result = spider_net_init_chain(card, &card->rx_chain);
1861 if (result)
1862 goto alloc_rx_failed;
1863
1864 /* Allocate rx skbs */
1865 if (spider_net_alloc_rx_skbs(card))
1866 goto alloc_skbs_failed;
1867
1868 spider_net_set_multi(netdev);
1869
1870 /* further enhancement: setup hw vlan, if needed */
1871
1872 result = -EBUSY;
1873 if (request_irq(netdev->irq, spider_net_interrupt,
1874 IRQF_SHARED, netdev->name, netdev))
1875 goto register_int_failed;
1876
1877 spider_net_enable_card(card);
1878
1879 netif_start_queue(netdev);
1880 netif_carrier_on(netdev);
1881 netif_poll_enable(netdev);
1882
1883 return 0;
1884
1885register_int_failed:
1886 spider_net_free_rx_chain_contents(card);
1887alloc_skbs_failed:
1888 spider_net_free_chain(card, &card->rx_chain);
1889alloc_rx_failed:
1890 spider_net_free_chain(card, &card->tx_chain);
1891alloc_tx_failed:
1892 del_timer_sync(&card->aneg_timer);
1893init_firmware_failed:
1894 return result;
1895}
1896
1897/**
1898 * spider_net_link_phy
1899 * @data: used for pointer to card structure
1900 *
1901 */
1902static void spider_net_link_phy(unsigned long data)
1903{
1904 struct spider_net_card *card = (struct spider_net_card *)data;
1905 struct mii_phy *phy = &card->phy;
1906
1907 /* if link didn't come up after SPIDER_NET_ANEG_TIMEOUT tries, setup phy again */
1908 if (card->aneg_count > SPIDER_NET_ANEG_TIMEOUT) {
1909
1910 pr_info("%s: link is down trying to bring it up\n", card->netdev->name);
1911
Jens Osterkamp4b23a552007-02-20 16:39:13 -06001912 switch (card->medium) {
1913 case BCM54XX_COPPER:
Kou Ishizaki3cf761d2007-02-20 16:34:50 -06001914 /* enable fiber with autonegotiation first */
1915 if (phy->def->ops->enable_fiber)
1916 phy->def->ops->enable_fiber(phy, 1);
Jens Osterkamp4b23a552007-02-20 16:39:13 -06001917 card->medium = BCM54XX_FIBER;
Kou Ishizaki3cf761d2007-02-20 16:34:50 -06001918 break;
1919
Jens Osterkamp4b23a552007-02-20 16:39:13 -06001920 case BCM54XX_FIBER:
Kou Ishizaki3cf761d2007-02-20 16:34:50 -06001921 /* fiber didn't come up, try to disable fiber autoneg */
1922 if (phy->def->ops->enable_fiber)
1923 phy->def->ops->enable_fiber(phy, 0);
Jens Osterkamp4b23a552007-02-20 16:39:13 -06001924 card->medium = BCM54XX_UNKNOWN;
Kou Ishizaki3cf761d2007-02-20 16:34:50 -06001925 break;
1926
Jens Osterkamp4b23a552007-02-20 16:39:13 -06001927 case BCM54XX_UNKNOWN:
Kou Ishizaki3cf761d2007-02-20 16:34:50 -06001928 /* copper, fiber with and without failed,
1929 * retry from beginning */
1930 spider_net_setup_aneg(card);
Jens Osterkamp4b23a552007-02-20 16:39:13 -06001931 card->medium = BCM54XX_COPPER;
Kou Ishizaki3cf761d2007-02-20 16:34:50 -06001932 break;
1933 }
1934
1935 card->aneg_count = 0;
1936 mod_timer(&card->aneg_timer, jiffies + SPIDER_NET_ANEG_TIMER);
1937 return;
1938 }
1939
1940 /* link still not up, try again later */
1941 if (!(phy->def->ops->poll_link(phy))) {
1942 card->aneg_count++;
1943 mod_timer(&card->aneg_timer, jiffies + SPIDER_NET_ANEG_TIMER);
1944 return;
1945 }
1946
1947 /* link came up, get abilities */
1948 phy->def->ops->read_link(phy);
1949
1950 spider_net_write_reg(card, SPIDER_NET_GMACST,
1951 spider_net_read_reg(card, SPIDER_NET_GMACST));
1952 spider_net_write_reg(card, SPIDER_NET_GMACINTEN, 0x4);
1953
1954 if (phy->speed == 1000)
1955 spider_net_write_reg(card, SPIDER_NET_GMACMODE, 0x00000001);
1956 else
1957 spider_net_write_reg(card, SPIDER_NET_GMACMODE, 0);
1958
1959 card->aneg_count = 0;
1960
1961 pr_debug("Found %s with %i Mbps, %s-duplex %sautoneg.\n",
1962 phy->def->name, phy->speed, phy->duplex==1 ? "Full" : "Half",
1963 phy->autoneg==1 ? "" : "no ");
1964
1965 return;
1966}
1967
1968/**
1969 * spider_net_setup_phy - setup PHY
1970 * @card: card structure
1971 *
1972 * returns 0 on success, <0 on failure
1973 *
1974 * spider_net_setup_phy is used as part of spider_net_probe.
1975 **/
1976static int
1977spider_net_setup_phy(struct spider_net_card *card)
1978{
1979 struct mii_phy *phy = &card->phy;
1980
1981 spider_net_write_reg(card, SPIDER_NET_GDTDMASEL,
1982 SPIDER_NET_DMASEL_VALUE);
1983 spider_net_write_reg(card, SPIDER_NET_GPCCTRL,
1984 SPIDER_NET_PHY_CTRL_VALUE);
1985
1986 phy->dev = card->netdev;
1987 phy->mdio_read = spider_net_read_phy;
1988 phy->mdio_write = spider_net_write_phy;
1989
1990 for (phy->mii_id = 1; phy->mii_id <= 31; phy->mii_id++) {
1991 unsigned short id;
1992 id = spider_net_read_phy(card->netdev, phy->mii_id, MII_BMSR);
1993 if (id != 0x0000 && id != 0xffff) {
1994 if (!mii_phy_probe(phy, phy->mii_id)) {
1995 pr_info("Found %s.\n", phy->def->name);
1996 break;
1997 }
1998 }
1999 }
2000
2001 return 0;
2002}
2003
2004/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002005 * spider_net_workaround_rxramfull - work around firmware bug
2006 * @card: card structure
2007 *
2008 * no return value
2009 **/
2010static void
2011spider_net_workaround_rxramfull(struct spider_net_card *card)
2012{
2013 int i, sequencer = 0;
2014
2015 /* cancel reset */
2016 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2017 SPIDER_NET_CKRCTRL_RUN_VALUE);
2018
2019 /* empty sequencer data */
Arnd Bergmann11f1a522006-01-12 17:16:44 -05002020 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
2021 sequencer++) {
Jens Osterkampee962a52006-07-13 11:54:23 +02002022 spider_net_write_reg(card, SPIDER_NET_GSnPRGADR +
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002023 sequencer * 8, 0x0);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05002024 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002025 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
2026 sequencer * 8, 0x0);
2027 }
2028 }
2029
2030 /* set sequencer operation */
2031 spider_net_write_reg(card, SPIDER_NET_GSINIT, 0x000000fe);
2032
2033 /* reset */
2034 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2035 SPIDER_NET_CKRCTRL_STOP_VALUE);
2036}
2037
2038/**
Jens Osterkampbdd01502006-07-13 11:54:08 +02002039 * spider_net_stop - called upon ifconfig down
2040 * @netdev: interface device structure
2041 *
2042 * always returns 0
2043 */
2044int
2045spider_net_stop(struct net_device *netdev)
2046{
2047 struct spider_net_card *card = netdev_priv(netdev);
2048
Jens Osterkampbdd01502006-07-13 11:54:08 +02002049 netif_poll_disable(netdev);
2050 netif_carrier_off(netdev);
2051 netif_stop_queue(netdev);
2052 del_timer_sync(&card->tx_timer);
Kou Ishizakiabdb66b2007-02-20 16:33:41 -06002053 del_timer_sync(&card->aneg_timer);
Jens Osterkampbdd01502006-07-13 11:54:08 +02002054
2055 /* disable/mask all interrupts */
2056 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, 0);
2057 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK, 0);
2058 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK, 0);
Kou Ishizakiabdb66b2007-02-20 16:33:41 -06002059 spider_net_write_reg(card, SPIDER_NET_GMACINTEN, 0);
Jens Osterkampbdd01502006-07-13 11:54:08 +02002060
Linas Vepstasd406eaf2007-02-20 16:32:00 -06002061 free_irq(netdev->irq, netdev);
Jens Osterkampbdd01502006-07-13 11:54:08 +02002062
2063 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
2064 SPIDER_NET_DMA_TX_FEND_VALUE);
2065
2066 /* turn off DMA, force end */
2067 spider_net_disable_rxdmac(card);
2068
2069 /* release chains */
Linas Vepstas9cc7bf72006-10-10 16:14:29 -05002070 spider_net_release_tx_chain(card, 1);
Linas Vepstasd4ed8f82006-12-13 15:06:59 -06002071 spider_net_free_rx_chain_contents(card);
Jens Osterkampbdd01502006-07-13 11:54:08 +02002072
2073 spider_net_free_chain(card, &card->tx_chain);
2074 spider_net_free_chain(card, &card->rx_chain);
2075
2076 return 0;
2077}
2078
2079/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002080 * spider_net_tx_timeout_task - task scheduled by the watchdog timeout
2081 * function (to be called not under interrupt status)
2082 * @data: data, is interface device structure
2083 *
2084 * called as task when tx hangs, resets interface (if interface is up)
2085 */
2086static void
David Howellsc4028952006-11-22 14:57:56 +00002087spider_net_tx_timeout_task(struct work_struct *work)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002088{
David Howellsc4028952006-11-22 14:57:56 +00002089 struct spider_net_card *card =
2090 container_of(work, struct spider_net_card, tx_timeout_task);
2091 struct net_device *netdev = card->netdev;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002092
2093 if (!(netdev->flags & IFF_UP))
2094 goto out;
2095
2096 netif_device_detach(netdev);
2097 spider_net_stop(netdev);
2098
2099 spider_net_workaround_rxramfull(card);
2100 spider_net_init_card(card);
2101
2102 if (spider_net_setup_phy(card))
2103 goto out;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002104
2105 spider_net_open(netdev);
Jens Osterkampbdd01502006-07-13 11:54:08 +02002106 spider_net_kick_tx_dma(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002107 netif_device_attach(netdev);
2108
2109out:
2110 atomic_dec(&card->tx_timeout_task_counter);
2111}
2112
2113/**
2114 * spider_net_tx_timeout - called when the tx timeout watchdog kicks in.
2115 * @netdev: interface device structure
2116 *
2117 * called, if tx hangs. Schedules a task that resets the interface
2118 */
2119static void
2120spider_net_tx_timeout(struct net_device *netdev)
2121{
2122 struct spider_net_card *card;
2123
2124 card = netdev_priv(netdev);
2125 atomic_inc(&card->tx_timeout_task_counter);
2126 if (netdev->flags & IFF_UP)
2127 schedule_work(&card->tx_timeout_task);
2128 else
2129 atomic_dec(&card->tx_timeout_task_counter);
Jim Lewis9b6b0b82006-09-22 17:22:53 -07002130 card->spider_stats.tx_timeouts++;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002131}
2132
2133/**
2134 * spider_net_setup_netdev_ops - initialization of net_device operations
2135 * @netdev: net_device structure
2136 *
2137 * fills out function pointers in the net_device structure
2138 */
2139static void
2140spider_net_setup_netdev_ops(struct net_device *netdev)
2141{
2142 netdev->open = &spider_net_open;
2143 netdev->stop = &spider_net_stop;
2144 netdev->hard_start_xmit = &spider_net_xmit;
2145 netdev->get_stats = &spider_net_get_stats;
2146 netdev->set_multicast_list = &spider_net_set_multi;
2147 netdev->set_mac_address = &spider_net_set_mac;
2148 netdev->change_mtu = &spider_net_change_mtu;
2149 netdev->do_ioctl = &spider_net_do_ioctl;
2150 /* tx watchdog */
2151 netdev->tx_timeout = &spider_net_tx_timeout;
2152 netdev->watchdog_timeo = SPIDER_NET_WATCHDOG_TIMEOUT;
2153 /* NAPI */
2154 netdev->poll = &spider_net_poll;
2155 netdev->weight = SPIDER_NET_NAPI_WEIGHT;
2156 /* HW VLAN */
2157 netdev->vlan_rx_register = &spider_net_vlan_rx_reg;
2158 netdev->vlan_rx_add_vid = &spider_net_vlan_rx_add;
2159 netdev->vlan_rx_kill_vid = &spider_net_vlan_rx_kill;
2160#ifdef CONFIG_NET_POLL_CONTROLLER
2161 /* poll controller */
2162 netdev->poll_controller = &spider_net_poll_controller;
2163#endif /* CONFIG_NET_POLL_CONTROLLER */
2164 /* ethtool ops */
2165 netdev->ethtool_ops = &spider_net_ethtool_ops;
2166}
2167
2168/**
2169 * spider_net_setup_netdev - initialization of net_device
2170 * @card: card structure
2171 *
2172 * Returns 0 on success or <0 on failure
2173 *
2174 * spider_net_setup_netdev initializes the net_device structure
2175 **/
2176static int
2177spider_net_setup_netdev(struct spider_net_card *card)
2178{
2179 int result;
2180 struct net_device *netdev = card->netdev;
2181 struct device_node *dn;
2182 struct sockaddr addr;
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10002183 const u8 *mac;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002184
2185 SET_MODULE_OWNER(netdev);
2186 SET_NETDEV_DEV(netdev, &card->pdev->dev);
2187
2188 pci_set_drvdata(card->pdev, netdev);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05002189
Arnd Bergmann11f1a522006-01-12 17:16:44 -05002190 init_timer(&card->tx_timer);
2191 card->tx_timer.function =
2192 (void (*)(unsigned long)) spider_net_cleanup_tx_ring;
2193 card->tx_timer.data = (unsigned long) card;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002194 netdev->irq = card->pdev->irq;
2195
Kou Ishizakiabdb66b2007-02-20 16:33:41 -06002196 card->aneg_count = 0;
2197 init_timer(&card->aneg_timer);
2198 card->aneg_timer.function = spider_net_link_phy;
2199 card->aneg_timer.data = (unsigned long) card;
2200
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002201 card->options.rx_csum = SPIDER_NET_RX_CSUM_DEFAULT;
2202
Linas Vepstasd4ed8f82006-12-13 15:06:59 -06002203 card->tx_chain.num_desc = tx_descriptors;
2204 card->rx_chain.num_desc = rx_descriptors;
Jim Lewisb68a60e2006-08-14 23:00:23 -07002205
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002206 spider_net_setup_netdev_ops(netdev);
2207
Jens Osterkampbdd01502006-07-13 11:54:08 +02002208 netdev->features = NETIF_F_HW_CSUM | NETIF_F_LLTX;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002209 /* some time: NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
2210 * NETIF_F_HW_VLAN_FILTER */
2211
2212 netdev->irq = card->pdev->irq;
2213
2214 dn = pci_device_to_OF_node(card->pdev);
Jens Osterkamp543cec52005-09-06 19:30:54 -07002215 if (!dn)
2216 return -EIO;
2217
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10002218 mac = get_property(dn, "local-mac-address", NULL);
Jens Osterkamp543cec52005-09-06 19:30:54 -07002219 if (!mac)
2220 return -EIO;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002221 memcpy(addr.sa_data, mac, ETH_ALEN);
2222
2223 result = spider_net_set_mac(netdev, &addr);
2224 if ((result) && (netif_msg_probe(card)))
2225 pr_err("Failed to set MAC address: %i\n", result);
2226
2227 result = register_netdev(netdev);
2228 if (result) {
2229 if (netif_msg_probe(card))
2230 pr_err("Couldn't register net_device: %i\n",
2231 result);
2232 return result;
2233 }
2234
2235 if (netif_msg_probe(card))
2236 pr_info("Initialized device %s.\n", netdev->name);
2237
2238 return 0;
2239}
2240
2241/**
2242 * spider_net_alloc_card - allocates net_device and card structure
2243 *
2244 * returns the card structure or NULL in case of errors
2245 *
2246 * the card and net_device structures are linked to each other
2247 */
2248static struct spider_net_card *
2249spider_net_alloc_card(void)
2250{
2251 struct net_device *netdev;
2252 struct spider_net_card *card;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002253
Linas Vepstasd4ed8f82006-12-13 15:06:59 -06002254 netdev = alloc_etherdev(sizeof(struct spider_net_card));
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002255 if (!netdev)
2256 return NULL;
2257
2258 card = netdev_priv(netdev);
2259 card->netdev = netdev;
2260 card->msg_enable = SPIDER_NET_DEFAULT_MSG;
David Howellsc4028952006-11-22 14:57:56 +00002261 INIT_WORK(&card->tx_timeout_task, spider_net_tx_timeout_task);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002262 init_waitqueue_head(&card->waitq);
2263 atomic_set(&card->tx_timeout_task_counter, 0);
2264
2265 return card;
2266}
2267
2268/**
2269 * spider_net_undo_pci_setup - releases PCI ressources
2270 * @card: card structure
2271 *
2272 * spider_net_undo_pci_setup releases the mapped regions
2273 */
2274static void
2275spider_net_undo_pci_setup(struct spider_net_card *card)
2276{
2277 iounmap(card->regs);
2278 pci_release_regions(card->pdev);
2279}
2280
2281/**
2282 * spider_net_setup_pci_dev - sets up the device in terms of PCI operations
2283 * @card: card structure
2284 * @pdev: PCI device
2285 *
2286 * Returns the card structure or NULL if any errors occur
2287 *
2288 * spider_net_setup_pci_dev initializes pdev and together with the
2289 * functions called in spider_net_open configures the device so that
2290 * data can be transferred over it
2291 * The net_device structure is attached to the card structure, if the
2292 * function returns without error.
2293 **/
2294static struct spider_net_card *
2295spider_net_setup_pci_dev(struct pci_dev *pdev)
2296{
2297 struct spider_net_card *card;
2298 unsigned long mmio_start, mmio_len;
2299
2300 if (pci_enable_device(pdev)) {
2301 pr_err("Couldn't enable PCI device\n");
2302 return NULL;
2303 }
2304
2305 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
2306 pr_err("Couldn't find proper PCI device base address.\n");
2307 goto out_disable_dev;
2308 }
2309
2310 if (pci_request_regions(pdev, spider_net_driver_name)) {
2311 pr_err("Couldn't obtain PCI resources, aborting.\n");
2312 goto out_disable_dev;
2313 }
2314
2315 pci_set_master(pdev);
2316
2317 card = spider_net_alloc_card();
2318 if (!card) {
2319 pr_err("Couldn't allocate net_device structure, "
2320 "aborting.\n");
2321 goto out_release_regions;
2322 }
2323 card->pdev = pdev;
2324
2325 /* fetch base address and length of first resource */
2326 mmio_start = pci_resource_start(pdev, 0);
2327 mmio_len = pci_resource_len(pdev, 0);
2328
2329 card->netdev->mem_start = mmio_start;
2330 card->netdev->mem_end = mmio_start + mmio_len;
2331 card->regs = ioremap(mmio_start, mmio_len);
2332
2333 if (!card->regs) {
2334 pr_err("Couldn't obtain PCI resources, aborting.\n");
2335 goto out_release_regions;
2336 }
2337
2338 return card;
2339
2340out_release_regions:
2341 pci_release_regions(pdev);
2342out_disable_dev:
2343 pci_disable_device(pdev);
2344 pci_set_drvdata(pdev, NULL);
2345 return NULL;
2346}
2347
2348/**
2349 * spider_net_probe - initialization of a device
2350 * @pdev: PCI device
2351 * @ent: entry in the device id list
2352 *
2353 * Returns 0 on success, <0 on failure
2354 *
2355 * spider_net_probe initializes pdev and registers a net_device
2356 * structure for it. After that, the device can be ifconfig'ed up
2357 **/
2358static int __devinit
2359spider_net_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2360{
2361 int err = -EIO;
2362 struct spider_net_card *card;
2363
2364 card = spider_net_setup_pci_dev(pdev);
2365 if (!card)
2366 goto out;
2367
2368 spider_net_workaround_rxramfull(card);
2369 spider_net_init_card(card);
2370
2371 err = spider_net_setup_phy(card);
2372 if (err)
2373 goto out_undo_pci;
2374
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002375 err = spider_net_setup_netdev(card);
2376 if (err)
2377 goto out_undo_pci;
2378
2379 return 0;
2380
2381out_undo_pci:
2382 spider_net_undo_pci_setup(card);
2383 free_netdev(card->netdev);
2384out:
2385 return err;
2386}
2387
2388/**
2389 * spider_net_remove - removal of a device
2390 * @pdev: PCI device
2391 *
2392 * Returns 0 on success, <0 on failure
2393 *
2394 * spider_net_remove is called to remove the device and unregisters the
2395 * net_device
2396 **/
2397static void __devexit
2398spider_net_remove(struct pci_dev *pdev)
2399{
2400 struct net_device *netdev;
2401 struct spider_net_card *card;
2402
2403 netdev = pci_get_drvdata(pdev);
2404 card = netdev_priv(netdev);
2405
2406 wait_event(card->waitq,
2407 atomic_read(&card->tx_timeout_task_counter) == 0);
2408
2409 unregister_netdev(netdev);
Jens Osterkamp543cec52005-09-06 19:30:54 -07002410
2411 /* switch off card */
2412 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2413 SPIDER_NET_CKRCTRL_STOP_VALUE);
2414 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2415 SPIDER_NET_CKRCTRL_RUN_VALUE);
2416
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002417 spider_net_undo_pci_setup(card);
2418 free_netdev(netdev);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002419}
2420
2421static struct pci_driver spider_net_driver = {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002422 .name = spider_net_driver_name,
2423 .id_table = spider_net_pci_tbl,
2424 .probe = spider_net_probe,
2425 .remove = __devexit_p(spider_net_remove)
2426};
2427
2428/**
2429 * spider_net_init - init function when the driver is loaded
2430 *
2431 * spider_net_init registers the device driver
2432 */
2433static int __init spider_net_init(void)
2434{
Linas Vepstas90f10842006-10-10 15:56:04 -05002435 printk(KERN_INFO "Spidernet version %s.\n", VERSION);
2436
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002437 if (rx_descriptors < SPIDER_NET_RX_DESCRIPTORS_MIN) {
2438 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MIN;
2439 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2440 }
2441 if (rx_descriptors > SPIDER_NET_RX_DESCRIPTORS_MAX) {
2442 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MAX;
2443 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2444 }
2445 if (tx_descriptors < SPIDER_NET_TX_DESCRIPTORS_MIN) {
2446 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MIN;
2447 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2448 }
2449 if (tx_descriptors > SPIDER_NET_TX_DESCRIPTORS_MAX) {
2450 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MAX;
2451 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2452 }
2453
2454 return pci_register_driver(&spider_net_driver);
2455}
2456
2457/**
2458 * spider_net_cleanup - exit function when driver is unloaded
2459 *
2460 * spider_net_cleanup unregisters the device driver
2461 */
2462static void __exit spider_net_cleanup(void)
2463{
2464 pci_unregister_driver(&spider_net_driver);
2465}
2466
2467module_init(spider_net_init);
2468module_exit(spider_net_cleanup);