blob: bf6ff39e02bbc035e2da03992be4c3661d07fa1c [file] [log] [blame]
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001/*
2 * Network device driver for Cell Processor-Based Blade
3 *
4 * (C) Copyright IBM Corp. 2005
5 *
6 * Authors : Utz Bacher <utz.bacher@de.ibm.com>
7 * Jens Osterkamp <Jens.Osterkamp@de.ibm.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
Jens Osterkampaaec0fa2005-09-05 15:19:29 -070024#include <linux/compiler.h>
25#include <linux/crc32.h>
26#include <linux/delay.h>
27#include <linux/etherdevice.h>
28#include <linux/ethtool.h>
29#include <linux/firmware.h>
30#include <linux/if_vlan.h>
Arnd Bergmann7c5c2202006-01-12 17:16:45 -050031#include <linux/in.h>
Jens Osterkampaaec0fa2005-09-05 15:19:29 -070032#include <linux/init.h>
33#include <linux/ioport.h>
34#include <linux/ip.h>
35#include <linux/kernel.h>
36#include <linux/mii.h>
37#include <linux/module.h>
38#include <linux/netdevice.h>
39#include <linux/device.h>
40#include <linux/pci.h>
41#include <linux/skbuff.h>
42#include <linux/slab.h>
43#include <linux/tcp.h>
44#include <linux/types.h>
Arnd Bergmann11f1a522006-01-12 17:16:44 -050045#include <linux/vmalloc.h>
Jens Osterkampaaec0fa2005-09-05 15:19:29 -070046#include <linux/wait.h>
47#include <linux/workqueue.h>
48#include <asm/bitops.h>
49#include <asm/pci-bridge.h>
50#include <net/checksum.h>
51
52#include "spider_net.h"
53
54MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com> and Jens Osterkamp " \
55 "<Jens.Osterkamp@de.ibm.com>");
56MODULE_DESCRIPTION("Spider Southbridge Gigabit Ethernet driver");
57MODULE_LICENSE("GPL");
Linas Vepstas90f10842006-10-10 15:56:04 -050058MODULE_VERSION(VERSION);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -070059
60static int rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_DEFAULT;
61static int tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_DEFAULT;
62
Linas Vepstase2874f22006-10-10 15:59:02 -050063module_param(rx_descriptors, int, 0444);
64module_param(tx_descriptors, int, 0444);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -070065
66MODULE_PARM_DESC(rx_descriptors, "number of descriptors used " \
67 "in rx chains");
68MODULE_PARM_DESC(tx_descriptors, "number of descriptors used " \
69 "in tx chain");
70
71char spider_net_driver_name[] = "spidernet";
72
73static struct pci_device_id spider_net_pci_tbl[] = {
74 { PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_SPIDER_NET,
75 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
76 { 0, }
77};
78
79MODULE_DEVICE_TABLE(pci, spider_net_pci_tbl);
80
81/**
82 * spider_net_read_reg - reads an SMMIO register of a card
83 * @card: device structure
84 * @reg: register to read from
85 *
86 * returns the content of the specified SMMIO register.
87 */
Jens Osterkampbdd01502006-07-13 11:54:08 +020088static inline u32
Jens Osterkampaaec0fa2005-09-05 15:19:29 -070089spider_net_read_reg(struct spider_net_card *card, u32 reg)
90{
Benjamin Herrenschmidt3bc0f402006-11-11 17:25:13 +110091 /* We use the powerpc specific variants instead of readl_be() because
92 * we know spidernet is not a real PCI device and we can thus avoid the
93 * performance hit caused by the PCI workarounds.
94 */
95 return in_be32(card->regs + reg);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -070096}
97
98/**
99 * spider_net_write_reg - writes to an SMMIO register of a card
100 * @card: device structure
101 * @reg: register to write to
102 * @value: value to write into the specified SMMIO register
103 */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200104static inline void
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700105spider_net_write_reg(struct spider_net_card *card, u32 reg, u32 value)
106{
Benjamin Herrenschmidt3bc0f402006-11-11 17:25:13 +1100107 /* We use the powerpc specific variants instead of writel_be() because
108 * we know spidernet is not a real PCI device and we can thus avoid the
109 * performance hit caused by the PCI workarounds.
110 */
111 out_be32(card->regs + reg, value);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700112}
113
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700114/** spider_net_write_phy - write to phy register
115 * @netdev: adapter to be written to
116 * @mii_id: id of MII
117 * @reg: PHY register
118 * @val: value to be written to phy register
119 *
120 * spider_net_write_phy_register writes to an arbitrary PHY
121 * register via the spider GPCWOPCMD register. We assume the queue does
122 * not run full (not more than 15 commands outstanding).
123 **/
124static void
125spider_net_write_phy(struct net_device *netdev, int mii_id,
126 int reg, int val)
127{
128 struct spider_net_card *card = netdev_priv(netdev);
129 u32 writevalue;
130
131 writevalue = ((u32)mii_id << 21) |
132 ((u32)reg << 16) | ((u32)val);
133
134 spider_net_write_reg(card, SPIDER_NET_GPCWOPCMD, writevalue);
135}
136
137/** spider_net_read_phy - read from phy register
138 * @netdev: network device to be read from
139 * @mii_id: id of MII
140 * @reg: PHY register
141 *
142 * Returns value read from PHY register
143 *
144 * spider_net_write_phy reads from an arbitrary PHY
145 * register via the spider GPCROPCMD register
146 **/
147static int
148spider_net_read_phy(struct net_device *netdev, int mii_id, int reg)
149{
150 struct spider_net_card *card = netdev_priv(netdev);
151 u32 readvalue;
152
153 readvalue = ((u32)mii_id << 21) | ((u32)reg << 16);
154 spider_net_write_reg(card, SPIDER_NET_GPCROPCMD, readvalue);
155
156 /* we don't use semaphores to wait for an SPIDER_NET_GPROPCMPINT
157 * interrupt, as we poll for the completion of the read operation
158 * in spider_net_read_phy. Should take about 50 us */
159 do {
160 readvalue = spider_net_read_reg(card, SPIDER_NET_GPCROPCMD);
161 } while (readvalue & SPIDER_NET_GPREXEC);
162
163 readvalue &= SPIDER_NET_GPRDAT_MASK;
164
165 return readvalue;
166}
167
168/**
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500169 * spider_net_rx_irq_off - switch off rx irq on this spider card
170 * @card: device structure
171 *
172 * switches off rx irq by masking them out in the GHIINTnMSK register
173 */
174static void
175spider_net_rx_irq_off(struct spider_net_card *card)
176{
177 u32 regvalue;
178
179 regvalue = SPIDER_NET_INT0_MASK_VALUE & (~SPIDER_NET_RXINT);
180 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, regvalue);
181}
182
183/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700184 * spider_net_rx_irq_on - switch on rx irq on this spider card
185 * @card: device structure
186 *
187 * switches on rx irq by enabling them in the GHIINTnMSK register
188 */
189static void
190spider_net_rx_irq_on(struct spider_net_card *card)
191{
192 u32 regvalue;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700193
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500194 regvalue = SPIDER_NET_INT0_MASK_VALUE | SPIDER_NET_RXINT;
195 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, regvalue);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700196}
197
198/**
199 * spider_net_set_promisc - sets the unicast address or the promiscuous mode
200 * @card: card structure
201 *
202 * spider_net_set_promisc sets the unicast destination address filter and
203 * thus either allows for non-promisc mode or promisc mode
204 */
205static void
206spider_net_set_promisc(struct spider_net_card *card)
207{
208 u32 macu, macl;
209 struct net_device *netdev = card->netdev;
210
211 if (netdev->flags & IFF_PROMISC) {
212 /* clear destination entry 0 */
213 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR, 0);
214 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR + 0x04, 0);
215 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R,
216 SPIDER_NET_PROMISC_VALUE);
217 } else {
218 macu = netdev->dev_addr[0];
219 macu <<= 8;
220 macu |= netdev->dev_addr[1];
221 memcpy(&macl, &netdev->dev_addr[2], sizeof(macl));
222
223 macu |= SPIDER_NET_UA_DESCR_VALUE;
224 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR, macu);
225 spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR + 0x04, macl);
226 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R,
227 SPIDER_NET_NONPROMISC_VALUE);
228 }
229}
230
231/**
232 * spider_net_get_mac_address - read mac address from spider card
233 * @card: device structure
234 *
235 * reads MAC address from GMACUNIMACU and GMACUNIMACL registers
236 */
237static int
238spider_net_get_mac_address(struct net_device *netdev)
239{
240 struct spider_net_card *card = netdev_priv(netdev);
241 u32 macl, macu;
242
243 macl = spider_net_read_reg(card, SPIDER_NET_GMACUNIMACL);
244 macu = spider_net_read_reg(card, SPIDER_NET_GMACUNIMACU);
245
246 netdev->dev_addr[0] = (macu >> 24) & 0xff;
247 netdev->dev_addr[1] = (macu >> 16) & 0xff;
248 netdev->dev_addr[2] = (macu >> 8) & 0xff;
249 netdev->dev_addr[3] = macu & 0xff;
250 netdev->dev_addr[4] = (macl >> 8) & 0xff;
251 netdev->dev_addr[5] = macl & 0xff;
252
253 if (!is_valid_ether_addr(&netdev->dev_addr[0]))
254 return -EINVAL;
255
256 return 0;
257}
258
259/**
260 * spider_net_get_descr_status -- returns the status of a descriptor
261 * @descr: descriptor to look at
262 *
263 * returns the status as in the dmac_cmd_status field of the descriptor
264 */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200265static inline int
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700266spider_net_get_descr_status(struct spider_net_descr *descr)
267{
Jens Osterkampbdd01502006-07-13 11:54:08 +0200268 return descr->dmac_cmd_status & SPIDER_NET_DESCR_IND_PROC_MASK;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700269}
270
271/**
272 * spider_net_free_chain - free descriptor chain
273 * @card: card structure
274 * @chain: address of chain
275 *
276 */
277static void
278spider_net_free_chain(struct spider_net_card *card,
279 struct spider_net_descr_chain *chain)
280{
281 struct spider_net_descr *descr;
282
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600283 descr = chain->ring;
284 do {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700285 descr->bus_addr = 0;
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600286 descr->next_descr_addr = 0;
287 descr = descr->next;
288 } while (descr != chain->ring);
289
290 dma_free_coherent(&card->pdev->dev, chain->num_desc,
291 chain->ring, chain->dma_addr);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700292}
293
294/**
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600295 * spider_net_init_chain - alloc and link descriptor chain
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700296 * @card: card structure
297 * @chain: address of chain
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700298 *
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600299 * We manage a circular list that mirrors the hardware structure,
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700300 * except that the hardware uses bus addresses.
301 *
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600302 * Returns 0 on success, <0 on failure
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700303 */
304static int
305spider_net_init_chain(struct spider_net_card *card,
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600306 struct spider_net_descr_chain *chain)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700307{
308 int i;
309 struct spider_net_descr *descr;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500310 dma_addr_t buf;
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600311 size_t alloc_size;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700312
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600313 alloc_size = chain->num_desc * sizeof (struct spider_net_descr);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700314
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600315 chain->ring = dma_alloc_coherent(&card->pdev->dev, alloc_size,
316 &chain->dma_addr, GFP_KERNEL);
317
318 if (!chain->ring)
319 return -ENOMEM;
320
321 descr = chain->ring;
322 memset(descr, 0, alloc_size);
323
324 /* Set up the hardware pointers in each descriptor */
325 buf = chain->dma_addr;
326 for (i=0; i < chain->num_desc; i++, descr++) {
Jens Osterkampbdd01502006-07-13 11:54:08 +0200327 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700328
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500329 descr->bus_addr = buf;
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600330 descr->next_descr_addr = 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700331 descr->next = descr + 1;
332 descr->prev = descr - 1;
333
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600334 buf += sizeof(struct spider_net_descr);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700335 }
336 /* do actual circular list */
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600337 (descr-1)->next = chain->ring;
338 chain->ring->prev = descr-1;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700339
Jens Osterkampbdd01502006-07-13 11:54:08 +0200340 spin_lock_init(&chain->lock);
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600341 chain->head = chain->ring;
342 chain->tail = chain->ring;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700343 return 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700344}
345
346/**
347 * spider_net_free_rx_chain_contents - frees descr contents in rx chain
348 * @card: card structure
349 *
350 * returns 0 on success, <0 on failure
351 */
352static void
353spider_net_free_rx_chain_contents(struct spider_net_card *card)
354{
355 struct spider_net_descr *descr;
356
357 descr = card->rx_chain.head;
Linas Vepstas64751912006-10-10 16:22:29 -0500358 do {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700359 if (descr->skb) {
360 dev_kfree_skb(descr->skb);
361 pci_unmap_single(card->pdev, descr->buf_addr,
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500362 SPIDER_NET_MAX_FRAME,
Linas Vepstas348bc2a2006-10-10 16:21:10 -0500363 PCI_DMA_BIDIRECTIONAL);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700364 }
365 descr = descr->next;
Linas Vepstas64751912006-10-10 16:22:29 -0500366 } while (descr != card->rx_chain.head);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700367}
368
369/**
Linas Vepstasa4182c52006-12-13 15:19:54 -0600370 * spider_net_prepare_rx_descr - Reinitialize RX descriptor
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700371 * @card: card structure
372 * @descr: descriptor to re-init
373 *
Linas Vepstasa4182c52006-12-13 15:19:54 -0600374 * Return 0 on succes, <0 on failure.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700375 *
Linas Vepstasa4182c52006-12-13 15:19:54 -0600376 * Allocates a new rx skb, iommu-maps it and attaches it to the
377 * descriptor. Mark the descriptor as activated, ready-to-use.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700378 */
379static int
380spider_net_prepare_rx_descr(struct spider_net_card *card,
381 struct spider_net_descr *descr)
382{
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500383 dma_addr_t buf;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700384 int offset;
385 int bufsize;
386
387 /* we need to round up the buffer size to a multiple of 128 */
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500388 bufsize = (SPIDER_NET_MAX_FRAME + SPIDER_NET_RXBUF_ALIGN - 1) &
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700389 (~(SPIDER_NET_RXBUF_ALIGN - 1));
390
391 /* and we need to have it 128 byte aligned, therefore we allocate a
392 * bit more */
393 /* allocate an skb */
394 descr->skb = dev_alloc_skb(bufsize + SPIDER_NET_RXBUF_ALIGN - 1);
395 if (!descr->skb) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500396 if (netif_msg_rx_err(card) && net_ratelimit())
397 pr_err("Not enough memory to allocate rx buffer\n");
Jim Lewis9b6b0b82006-09-22 17:22:53 -0700398 card->spider_stats.alloc_rx_skb_error++;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700399 return -ENOMEM;
400 }
401 descr->buf_size = bufsize;
402 descr->result_size = 0;
403 descr->valid_size = 0;
404 descr->data_status = 0;
405 descr->data_error = 0;
406
407 offset = ((unsigned long)descr->skb->data) &
408 (SPIDER_NET_RXBUF_ALIGN - 1);
409 if (offset)
410 skb_reserve(descr->skb, SPIDER_NET_RXBUF_ALIGN - offset);
Linas Vepstasa4182c52006-12-13 15:19:54 -0600411 /* iommu-map the skb */
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500412 buf = pci_map_single(card->pdev, descr->skb->data,
Jens Osterkampbdd01502006-07-13 11:54:08 +0200413 SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500414 descr->buf_addr = buf;
Olof Johanssond4b0a4c2006-09-27 14:02:26 -0500415 if (pci_dma_mapping_error(buf)) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700416 dev_kfree_skb_any(descr->skb);
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500417 if (netif_msg_rx_err(card) && net_ratelimit())
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700418 pr_err("Could not iommu-map rx buffer\n");
Jim Lewis9b6b0b82006-09-22 17:22:53 -0700419 card->spider_stats.rx_iommu_map_error++;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200420 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700421 } else {
Linas Vepstasd9a97202007-02-05 16:29:43 -0800422 descr->next_descr_addr = 0;
Linas Vepstas90476a22006-12-13 15:22:04 -0600423 wmb();
Jens Osterkampbdd01502006-07-13 11:54:08 +0200424 descr->dmac_cmd_status = SPIDER_NET_DESCR_CARDOWNED |
425 SPIDER_NET_DMAC_NOINTR_COMPLETE;
Linas Vepstasd9a97202007-02-05 16:29:43 -0800426
427 wmb();
428 descr->prev->next_descr_addr = descr->bus_addr;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700429 }
430
Linas Vepstasa4182c52006-12-13 15:19:54 -0600431 return 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700432}
433
434/**
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500435 * spider_net_enable_rxchtails - sets RX dmac chain tail addresses
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700436 * @card: card structure
437 *
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500438 * spider_net_enable_rxchtails sets the RX DMAC chain tail adresses in the
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700439 * chip by writing to the appropriate register. DMA is enabled in
440 * spider_net_enable_rxdmac.
441 */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200442static inline void
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700443spider_net_enable_rxchtails(struct spider_net_card *card)
444{
445 /* assume chain is aligned correctly */
446 spider_net_write_reg(card, SPIDER_NET_GDADCHA ,
447 card->rx_chain.tail->bus_addr);
448}
449
450/**
451 * spider_net_enable_rxdmac - enables a receive DMA controller
452 * @card: card structure
453 *
454 * spider_net_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
455 * in the GDADMACCNTR register
456 */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200457static inline void
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700458spider_net_enable_rxdmac(struct spider_net_card *card)
459{
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500460 wmb();
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700461 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
462 SPIDER_NET_DMA_RX_VALUE);
463}
464
465/**
466 * spider_net_refill_rx_chain - refills descriptors/skbs in the rx chains
467 * @card: card structure
468 *
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500469 * refills descriptors in the rx chain: allocates skbs and iommu-maps them.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700470 */
471static void
472spider_net_refill_rx_chain(struct spider_net_card *card)
473{
Jens Osterkampbdd01502006-07-13 11:54:08 +0200474 struct spider_net_descr_chain *chain = &card->rx_chain;
475 unsigned long flags;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700476
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500477 /* one context doing the refill (and a second context seeing that
478 * and omitting it) is ok. If called by NAPI, we'll be called again
479 * as spider_net_decode_one_descr is called several times. If some
480 * interrupt calls us, the NAPI is about to clean up anyway. */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200481 if (!spin_trylock_irqsave(&chain->lock, flags))
482 return;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700483
Jens Osterkampbdd01502006-07-13 11:54:08 +0200484 while (spider_net_get_descr_status(chain->head) ==
485 SPIDER_NET_DESCR_NOT_IN_USE) {
486 if (spider_net_prepare_rx_descr(card, chain->head))
487 break;
488 chain->head = chain->head->next;
489 }
490
491 spin_unlock_irqrestore(&chain->lock, flags);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700492}
493
494/**
Linas Vepstas2c307db2006-12-13 15:20:59 -0600495 * spider_net_alloc_rx_skbs - Allocates rx skbs in rx descriptor chains
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700496 * @card: card structure
497 *
Linas Vepstas2c307db2006-12-13 15:20:59 -0600498 * Returns 0 on success, <0 on failure.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700499 */
500static int
501spider_net_alloc_rx_skbs(struct spider_net_card *card)
502{
503 int result;
504 struct spider_net_descr_chain *chain;
505
506 result = -ENOMEM;
507
508 chain = &card->rx_chain;
Linas Vepstas2c307db2006-12-13 15:20:59 -0600509 /* Put at least one buffer into the chain. if this fails,
510 * we've got a problem. If not, spider_net_refill_rx_chain
511 * will do the rest at the end of this function. */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700512 if (spider_net_prepare_rx_descr(card, chain->head))
513 goto error;
514 else
515 chain->head = chain->head->next;
516
Linas Vepstas2c307db2006-12-13 15:20:59 -0600517 /* This will allocate the rest of the rx buffers;
518 * if not, it's business as usual later on. */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700519 spider_net_refill_rx_chain(card);
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500520 spider_net_enable_rxdmac(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700521 return 0;
522
523error:
524 spider_net_free_rx_chain_contents(card);
525 return result;
526}
527
528/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700529 * spider_net_get_multicast_hash - generates hash for multicast filter table
530 * @addr: multicast address
531 *
532 * returns the hash value.
533 *
534 * spider_net_get_multicast_hash calculates a hash value for a given multicast
535 * address, that is used to set the multicast filter tables
536 */
537static u8
538spider_net_get_multicast_hash(struct net_device *netdev, __u8 *addr)
539{
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700540 u32 crc;
541 u8 hash;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500542 char addr_for_crc[ETH_ALEN] = { 0, };
543 int i, bit;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700544
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500545 for (i = 0; i < ETH_ALEN * 8; i++) {
546 bit = (addr[i / 8] >> (i % 8)) & 1;
547 addr_for_crc[ETH_ALEN - 1 - i / 8] += bit << (7 - (i % 8));
548 }
549
550 crc = crc32_be(~0, addr_for_crc, netdev->addr_len);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700551
552 hash = (crc >> 27);
553 hash <<= 3;
554 hash |= crc & 7;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500555 hash &= 0xff;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700556
557 return hash;
558}
559
560/**
561 * spider_net_set_multi - sets multicast addresses and promisc flags
562 * @netdev: interface device structure
563 *
564 * spider_net_set_multi configures multicast addresses as needed for the
565 * netdev interface. It also sets up multicast, allmulti and promisc
566 * flags appropriately
567 */
568static void
569spider_net_set_multi(struct net_device *netdev)
570{
571 struct dev_mc_list *mc;
572 u8 hash;
573 int i;
574 u32 reg;
575 struct spider_net_card *card = netdev_priv(netdev);
576 unsigned long bitmask[SPIDER_NET_MULTICAST_HASHES / BITS_PER_LONG] =
577 {0, };
578
579 spider_net_set_promisc(card);
580
581 if (netdev->flags & IFF_ALLMULTI) {
582 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES; i++) {
583 set_bit(i, bitmask);
584 }
585 goto write_hash;
586 }
587
588 /* well, we know, what the broadcast hash value is: it's xfd
589 hash = spider_net_get_multicast_hash(netdev, netdev->broadcast); */
590 set_bit(0xfd, bitmask);
591
592 for (mc = netdev->mc_list; mc; mc = mc->next) {
593 hash = spider_net_get_multicast_hash(netdev, mc->dmi_addr);
594 set_bit(hash, bitmask);
595 }
596
597write_hash:
598 for (i = 0; i < SPIDER_NET_MULTICAST_HASHES / 4; i++) {
599 reg = 0;
600 if (test_bit(i * 4, bitmask))
601 reg += 0x08;
602 reg <<= 8;
603 if (test_bit(i * 4 + 1, bitmask))
604 reg += 0x08;
605 reg <<= 8;
606 if (test_bit(i * 4 + 2, bitmask))
607 reg += 0x08;
608 reg <<= 8;
609 if (test_bit(i * 4 + 3, bitmask))
610 reg += 0x08;
611
612 spider_net_write_reg(card, SPIDER_NET_GMRMHFILnR + i * 4, reg);
613 }
614}
615
616/**
617 * spider_net_disable_rxdmac - disables the receive DMA controller
618 * @card: card structure
619 *
620 * spider_net_disable_rxdmac terminates processing on the DMA controller by
621 * turing off DMA and issueing a force end
622 */
623static void
624spider_net_disable_rxdmac(struct spider_net_card *card)
625{
626 spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR,
627 SPIDER_NET_DMA_RX_FEND_VALUE);
628}
629
630/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700631 * spider_net_prepare_tx_descr - fill tx descriptor with skb data
632 * @card: card structure
633 * @descr: descriptor structure to fill out
634 * @skb: packet to use
635 *
636 * returns 0 on success, <0 on failure.
637 *
638 * fills out the descriptor structure with skb data and len. Copies data,
639 * if needed (32bit DMA!)
640 */
641static int
642spider_net_prepare_tx_descr(struct spider_net_card *card,
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700643 struct sk_buff *skb)
644{
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500645 struct spider_net_descr *descr;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500646 dma_addr_t buf;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500647 unsigned long flags;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500648
James K Lewis9c434f52006-11-17 14:39:36 -0800649 buf = pci_map_single(card->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
Olof Johanssond4b0a4c2006-09-27 14:02:26 -0500650 if (pci_dma_mapping_error(buf)) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500651 if (netif_msg_tx_err(card) && net_ratelimit())
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700652 pr_err("could not iommu-map packet (%p, %i). "
James K Lewis9c434f52006-11-17 14:39:36 -0800653 "Dropping packet\n", skb->data, skb->len);
Jim Lewis9b6b0b82006-09-22 17:22:53 -0700654 card->spider_stats.tx_iommu_map_error++;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700655 return -ENOMEM;
656 }
657
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500658 spin_lock_irqsave(&card->tx_chain.lock, flags);
659 descr = card->tx_chain.head;
660 card->tx_chain.head = descr->next;
661
Arnd Bergmann8e0a6132006-01-12 17:16:43 -0500662 descr->buf_addr = buf;
James K Lewis9c434f52006-11-17 14:39:36 -0800663 descr->buf_size = skb->len;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200664 descr->next_descr_addr = 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700665 descr->skb = skb;
666 descr->data_status = 0;
667
Jens Osterkampbdd01502006-07-13 11:54:08 +0200668 descr->dmac_cmd_status =
669 SPIDER_NET_DESCR_CARDOWNED | SPIDER_NET_DMAC_NOCS;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500670 spin_unlock_irqrestore(&card->tx_chain.lock, flags);
671
Jens Osterkampbdd01502006-07-13 11:54:08 +0200672 if (skb->protocol == htons(ETH_P_IP))
673 switch (skb->nh.iph->protocol) {
674 case IPPROTO_TCP:
675 descr->dmac_cmd_status |= SPIDER_NET_DMAC_TCP;
676 break;
677 case IPPROTO_UDP:
678 descr->dmac_cmd_status |= SPIDER_NET_DMAC_UDP;
679 break;
680 }
681
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500682 /* Chain the bus address, so that the DMA engine finds this descr. */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200683 descr->prev->next_descr_addr = descr->bus_addr;
684
Linas Vepstas917a5b82006-10-10 16:01:51 -0500685 card->netdev->trans_start = jiffies; /* set netdev watchdog timer */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200686 return 0;
687}
688
Linas Vepstasa664ccf2006-10-10 16:18:18 -0500689static int
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500690spider_net_set_low_watermark(struct spider_net_card *card)
691{
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500692 unsigned long flags;
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500693 int status;
694 int cnt=0;
695 int i;
696 struct spider_net_descr *descr = card->tx_chain.tail;
697
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500698 /* Measure the length of the queue. Measurement does not
699 * need to be precise -- does not need a lock. */
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500700 while (descr != card->tx_chain.head) {
701 status = descr->dmac_cmd_status & SPIDER_NET_DESCR_NOT_IN_USE;
702 if (status == SPIDER_NET_DESCR_NOT_IN_USE)
703 break;
704 descr = descr->next;
705 cnt++;
706 }
707
708 /* If TX queue is short, don't even bother with interrupts */
Linas Vepstasd4ed8f82006-12-13 15:06:59 -0600709 if (cnt < card->tx_chain.num_desc/4)
Linas Vepstasa664ccf2006-10-10 16:18:18 -0500710 return cnt;
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500711
712 /* Set low-watermark 3/4th's of the way into the queue. */
713 descr = card->tx_chain.tail;
714 cnt = (cnt*3)/4;
715 for (i=0;i<cnt; i++)
716 descr = descr->next;
717
718 /* Set the new watermark, clear the old watermark */
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500719 spin_lock_irqsave(&card->tx_chain.lock, flags);
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500720 descr->dmac_cmd_status |= SPIDER_NET_DESCR_TXDESFLG;
721 if (card->low_watermark && card->low_watermark != descr)
722 card->low_watermark->dmac_cmd_status =
723 card->low_watermark->dmac_cmd_status & ~SPIDER_NET_DESCR_TXDESFLG;
724 card->low_watermark = descr;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500725 spin_unlock_irqrestore(&card->tx_chain.lock, flags);
Linas Vepstasa664ccf2006-10-10 16:18:18 -0500726 return cnt;
Linas Vepstas204e5fa2006-10-10 16:11:33 -0500727}
728
Jens Osterkampbdd01502006-07-13 11:54:08 +0200729/**
730 * spider_net_release_tx_chain - processes sent tx descriptors
731 * @card: adapter structure
732 * @brutal: if set, don't care about whether descriptor seems to be in use
733 *
734 * returns 0 if the tx ring is empty, otherwise 1.
735 *
736 * spider_net_release_tx_chain releases the tx descriptors that spider has
737 * finished with (if non-brutal) or simply release tx descriptors (if brutal).
738 * If some other context is calling this function, we return 1 so that we're
739 * scheduled again (if we were scheduled) and will not loose initiative.
740 */
741static int
742spider_net_release_tx_chain(struct spider_net_card *card, int brutal)
743{
744 struct spider_net_descr_chain *chain = &card->tx_chain;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500745 struct spider_net_descr *descr;
746 struct sk_buff *skb;
747 u32 buf_addr;
748 unsigned long flags;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200749 int status;
750
Jens Osterkampbdd01502006-07-13 11:54:08 +0200751 while (chain->tail != chain->head) {
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500752 spin_lock_irqsave(&chain->lock, flags);
753 descr = chain->tail;
754
755 status = spider_net_get_descr_status(descr);
Jens Osterkampbdd01502006-07-13 11:54:08 +0200756 switch (status) {
757 case SPIDER_NET_DESCR_COMPLETE:
758 card->netdev_stats.tx_packets++;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500759 card->netdev_stats.tx_bytes += descr->skb->len;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200760 break;
761
762 case SPIDER_NET_DESCR_CARDOWNED:
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500763 if (!brutal) {
764 spin_unlock_irqrestore(&chain->lock, flags);
Jens Osterkampbdd01502006-07-13 11:54:08 +0200765 return 1;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500766 }
767
Jens Osterkampbdd01502006-07-13 11:54:08 +0200768 /* fallthrough, if we release the descriptors
769 * brutally (then we don't care about
770 * SPIDER_NET_DESCR_CARDOWNED) */
771
772 case SPIDER_NET_DESCR_RESPONSE_ERROR:
773 case SPIDER_NET_DESCR_PROTECTION_ERROR:
774 case SPIDER_NET_DESCR_FORCE_END:
775 if (netif_msg_tx_err(card))
776 pr_err("%s: forcing end of tx descriptor "
777 "with status x%02x\n",
778 card->netdev->name, status);
779 card->netdev_stats.tx_errors++;
780 break;
781
782 default:
783 card->netdev_stats.tx_dropped++;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500784 if (!brutal) {
785 spin_unlock_irqrestore(&chain->lock, flags);
Linas Vepstasc3fee4c2006-10-10 16:00:04 -0500786 return 1;
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500787 }
Jens Osterkampbdd01502006-07-13 11:54:08 +0200788 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700789
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500790 chain->tail = descr->next;
791 descr->dmac_cmd_status |= SPIDER_NET_DESCR_NOT_IN_USE;
792 skb = descr->skb;
793 buf_addr = descr->buf_addr;
794 spin_unlock_irqrestore(&chain->lock, flags);
795
796 /* unmap the skb */
797 if (skb) {
James K Lewis9c434f52006-11-17 14:39:36 -0800798 pci_unmap_single(card->pdev, buf_addr, skb->len,
799 PCI_DMA_TODEVICE);
Linas Vepstas9cc7bf72006-10-10 16:14:29 -0500800 dev_kfree_skb(skb);
801 }
802 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700803 return 0;
804}
805
806/**
807 * spider_net_kick_tx_dma - enables TX DMA processing
808 * @card: card structure
809 * @descr: descriptor address to enable TX processing at
810 *
Linas Vepstasa664ccf2006-10-10 16:18:18 -0500811 * This routine will start the transmit DMA running if
812 * it is not already running. This routine ned only be
813 * called when queueing a new packet to an empty tx queue.
814 * Writes the current tx chain head as start address
815 * of the tx descriptor chain and enables the transmission
816 * DMA engine.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700817 */
Jens Osterkampbdd01502006-07-13 11:54:08 +0200818static inline void
819spider_net_kick_tx_dma(struct spider_net_card *card)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700820{
Jens Osterkampbdd01502006-07-13 11:54:08 +0200821 struct spider_net_descr *descr;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700822
Jens Osterkampbdd01502006-07-13 11:54:08 +0200823 if (spider_net_read_reg(card, SPIDER_NET_GDTDMACCNTR) &
824 SPIDER_NET_TX_DMA_EN)
825 goto out;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700826
Jens Osterkampbdd01502006-07-13 11:54:08 +0200827 descr = card->tx_chain.tail;
828 for (;;) {
829 if (spider_net_get_descr_status(descr) ==
830 SPIDER_NET_DESCR_CARDOWNED) {
831 spider_net_write_reg(card, SPIDER_NET_GDTDCHA,
832 descr->bus_addr);
833 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
834 SPIDER_NET_DMA_TX_VALUE);
835 break;
836 }
837 if (descr == card->tx_chain.head)
838 break;
839 descr = descr->next;
840 }
841
842out:
843 mod_timer(&card->tx_timer, jiffies + SPIDER_NET_TX_TIMER);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700844}
845
846/**
847 * spider_net_xmit - transmits a frame over the device
848 * @skb: packet to send out
849 * @netdev: interface device structure
850 *
Jens Osterkampbdd01502006-07-13 11:54:08 +0200851 * returns 0 on success, !0 on failure
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700852 */
853static int
854spider_net_xmit(struct sk_buff *skb, struct net_device *netdev)
855{
Linas Vepstasa664ccf2006-10-10 16:18:18 -0500856 int cnt;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700857 struct spider_net_card *card = netdev_priv(netdev);
Jens Osterkampbdd01502006-07-13 11:54:08 +0200858 struct spider_net_descr_chain *chain = &card->tx_chain;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200859
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500860 spider_net_release_tx_chain(card, 0);
861
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500862 if ((chain->head->next == chain->tail->prev) ||
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500863 (spider_net_prepare_tx_descr(card, skb) != 0)) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500864
Jim Lewis9b6b0b82006-09-22 17:22:53 -0700865 card->netdev_stats.tx_dropped++;
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500866 netif_stop_queue(netdev);
867 return NETDEV_TX_BUSY;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200868 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700869
Linas Vepstasa664ccf2006-10-10 16:18:18 -0500870 cnt = spider_net_set_low_watermark(card);
871 if (cnt < 5)
872 spider_net_kick_tx_dma(card);
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500873 return NETDEV_TX_OK;
Jens Osterkampbdd01502006-07-13 11:54:08 +0200874}
875
876/**
877 * spider_net_cleanup_tx_ring - cleans up the TX ring
878 * @card: card structure
879 *
Linas Vepstas68a8c602006-10-10 16:13:05 -0500880 * spider_net_cleanup_tx_ring is called by either the tx_timer
881 * or from the NAPI polling routine.
882 * This routine releases resources associted with transmitted
883 * packets, including updating the queue tail pointer.
Jens Osterkampbdd01502006-07-13 11:54:08 +0200884 */
885static void
886spider_net_cleanup_tx_ring(struct spider_net_card *card)
887{
Jens Osterkampbdd01502006-07-13 11:54:08 +0200888 if ((spider_net_release_tx_chain(card, 0) != 0) &&
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500889 (card->netdev->flags & IFF_UP)) {
Jens Osterkampbdd01502006-07-13 11:54:08 +0200890 spider_net_kick_tx_dma(card);
Linas Vepstas313ef4b2006-10-10 16:04:00 -0500891 netif_wake_queue(card->netdev);
892 }
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700893}
894
895/**
896 * spider_net_do_ioctl - called for device ioctls
897 * @netdev: interface device structure
898 * @ifr: request parameter structure for ioctl
899 * @cmd: command code for ioctl
900 *
901 * returns 0 on success, <0 on failure. Currently, we have no special ioctls.
902 * -EOPNOTSUPP is returned, if an unknown ioctl was requested
903 */
904static int
905spider_net_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
906{
907 switch (cmd) {
908 default:
909 return -EOPNOTSUPP;
910 }
911}
912
913/**
914 * spider_net_pass_skb_up - takes an skb from a descriptor and passes it on
915 * @descr: descriptor to process
916 * @card: card structure
917 *
Linas Vepstas05b346b2006-12-13 15:15:15 -0600918 * Fills out skb structure and passes the data to the stack.
919 * The descriptor state is not changed.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700920 */
Linas Vepstas7f7223b2006-12-13 15:17:39 -0600921static void
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700922spider_net_pass_skb_up(struct spider_net_descr *descr,
Linas Vepstas1cd173f2006-12-13 15:12:26 -0600923 struct spider_net_card *card)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700924{
925 struct sk_buff *skb;
926 struct net_device *netdev;
927 u32 data_status, data_error;
928
929 data_status = descr->data_status;
930 data_error = descr->data_error;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700931 netdev = card->netdev;
932
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500933 skb = descr->skb;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700934 skb->dev = netdev;
935 skb_put(skb, descr->valid_size);
936
937 /* the card seems to add 2 bytes of junk in front
938 * of the ethernet frame */
939#define SPIDER_MISALIGN 2
940 skb_pull(skb, SPIDER_MISALIGN);
941 skb->protocol = eth_type_trans(skb, netdev);
942
943 /* checksum offload */
944 if (card->options.rx_csum) {
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500945 if ( ( (data_status & SPIDER_NET_DATA_STATUS_CKSUM_MASK) ==
946 SPIDER_NET_DATA_STATUS_CKSUM_MASK) &&
947 !(data_error & SPIDER_NET_DATA_ERR_CKSUM_MASK))
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700948 skb->ip_summed = CHECKSUM_UNNECESSARY;
949 else
950 skb->ip_summed = CHECKSUM_NONE;
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500951 } else
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700952 skb->ip_summed = CHECKSUM_NONE;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700953
954 if (data_status & SPIDER_NET_VLAN_PACKET) {
955 /* further enhancements: HW-accel VLAN
956 * vlan_hwaccel_receive_skb
957 */
958 }
959
960 /* pass skb up to stack */
Linas Vepstas1cd173f2006-12-13 15:12:26 -0600961 netif_receive_skb(skb);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700962
963 /* update netdevice statistics */
964 card->netdev_stats.rx_packets++;
965 card->netdev_stats.rx_bytes += skb->len;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700966}
967
Linas Vepstas6d249982006-12-13 15:23:56 -0600968#ifdef DEBUG
969static void show_rx_chain(struct spider_net_card *card)
970{
971 struct spider_net_descr_chain *chain = &card->rx_chain;
972 struct spider_net_descr *start= chain->tail;
973 struct spider_net_descr *descr= start;
974 int status;
975
976 int cnt = 0;
977 int cstat = spider_net_get_descr_status(descr);
978 printk(KERN_INFO "RX chain tail at descr=%ld\n",
979 (start - card->descr) - card->tx_chain.num_desc);
980 status = cstat;
981 do
982 {
983 status = spider_net_get_descr_status(descr);
984 if (cstat != status) {
985 printk(KERN_INFO "Have %d descrs with stat=x%08x\n", cnt, cstat);
986 cstat = status;
987 cnt = 0;
988 }
989 cnt ++;
990 descr = descr->next;
991 } while (descr != start);
992 printk(KERN_INFO "Last %d descrs with stat=x%08x\n", cnt, cstat);
993}
994#endif
995
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700996/**
Arnd Bergmann11f1a522006-01-12 17:16:44 -0500997 * spider_net_decode_one_descr - processes an rx descriptor
Jens Osterkampaaec0fa2005-09-05 15:19:29 -0700998 * @card: card structure
999 *
Linas Vepstas1cd173f2006-12-13 15:12:26 -06001000 * Returns 1 if a packet has been sent to the stack, otherwise 0
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001001 *
Linas Vepstas1cd173f2006-12-13 15:12:26 -06001002 * Processes an rx descriptor by iommu-unmapping the data buffer and passing
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001003 * the packet up to the stack. This function is called in softirq
1004 * context, e.g. either bottom half from interrupt or NAPI polling context
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001005 */
1006static int
Linas Vepstas1cd173f2006-12-13 15:12:26 -06001007spider_net_decode_one_descr(struct spider_net_card *card)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001008{
Jens Osterkampbdd01502006-07-13 11:54:08 +02001009 struct spider_net_descr_chain *chain = &card->rx_chain;
1010 struct spider_net_descr *descr = chain->tail;
1011 int status;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001012
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001013 status = spider_net_get_descr_status(descr);
1014
Linas Vepstas80dab7c2006-12-13 15:18:52 -06001015 /* Nothing in the descriptor, or ring must be empty */
1016 if ((status == SPIDER_NET_DESCR_CARDOWNED) ||
1017 (status == SPIDER_NET_DESCR_NOT_IN_USE))
Linas Vepstas1cd173f2006-12-13 15:12:26 -06001018 return 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001019
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001020 /* descriptor definitively used -- move on tail */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001021 chain->tail = descr->next;
1022
Linas Vepstas05b346b2006-12-13 15:15:15 -06001023 /* unmap descriptor */
1024 pci_unmap_single(card->pdev, descr->buf_addr,
1025 SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE);
1026
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001027 if ( (status == SPIDER_NET_DESCR_RESPONSE_ERROR) ||
1028 (status == SPIDER_NET_DESCR_PROTECTION_ERROR) ||
1029 (status == SPIDER_NET_DESCR_FORCE_END) ) {
1030 if (netif_msg_rx_err(card))
1031 pr_err("%s: dropping RX descriptor with state %d\n",
1032 card->netdev->name, status);
1033 card->netdev_stats.rx_dropped++;
Linas Vepstas7f7223b2006-12-13 15:17:39 -06001034 goto bad_desc;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001035 }
1036
1037 if ( (status != SPIDER_NET_DESCR_COMPLETE) &&
1038 (status != SPIDER_NET_DESCR_FRAME_END) ) {
Linas Vepstas5a028872006-12-13 15:08:25 -06001039 if (netif_msg_rx_err(card))
Linas Vepstas05b346b2006-12-13 15:15:15 -06001040 pr_err("%s: RX descriptor with unkown state %d\n",
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001041 card->netdev->name, status);
Linas Vepstas5a028872006-12-13 15:08:25 -06001042 card->spider_stats.rx_desc_unk_state++;
Linas Vepstas7f7223b2006-12-13 15:17:39 -06001043 goto bad_desc;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001044 }
1045
Linas Vepstas366684b2006-12-13 15:16:18 -06001046 /* The cases we'll throw away the packet immediately */
1047 if (descr->data_error & SPIDER_NET_DESTROY_RX_FLAGS) {
1048 if (netif_msg_rx_err(card))
1049 pr_err("%s: error in received descriptor found, "
1050 "data_status=x%08x, data_error=x%08x\n",
1051 card->netdev->name,
1052 descr->data_status, descr->data_error);
Linas Vepstas7f7223b2006-12-13 15:17:39 -06001053 goto bad_desc;
Linas Vepstas366684b2006-12-13 15:16:18 -06001054 }
1055
Linas Vepstas6d249982006-12-13 15:23:56 -06001056 if (descr->dmac_cmd_status & 0xfefe) {
1057 pr_err("%s: bad status, cmd_status=x%08x\n",
1058 card->netdev->name,
1059 descr->dmac_cmd_status);
1060 pr_err("buf_addr=x%08x\n", descr->buf_addr);
1061 pr_err("buf_size=x%08x\n", descr->buf_size);
1062 pr_err("next_descr_addr=x%08x\n", descr->next_descr_addr);
1063 pr_err("result_size=x%08x\n", descr->result_size);
1064 pr_err("valid_size=x%08x\n", descr->valid_size);
1065 pr_err("data_status=x%08x\n", descr->data_status);
1066 pr_err("data_error=x%08x\n", descr->data_error);
1067 pr_err("bus_addr=x%08x\n", descr->bus_addr);
1068 pr_err("which=%ld\n", descr - card->rx_chain.ring);
1069
1070 card->spider_stats.rx_desc_error++;
1071 goto bad_desc;
1072 }
1073
Linas Vepstas7f7223b2006-12-13 15:17:39 -06001074 /* Ok, we've got a packet in descr */
1075 spider_net_pass_skb_up(descr, card);
Linas Vepstas1cd173f2006-12-13 15:12:26 -06001076 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
Linas Vepstas7f7223b2006-12-13 15:17:39 -06001077 return 1;
1078
1079bad_desc:
1080 dev_kfree_skb_irq(descr->skb);
1081 descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE;
1082 return 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001083}
1084
1085/**
1086 * spider_net_poll - NAPI poll function called by the stack to return packets
1087 * @netdev: interface device structure
1088 * @budget: number of packets we can pass to the stack at most
1089 *
1090 * returns 0 if no more packets available to the driver/stack. Returns 1,
1091 * if the quota is exceeded, but the driver has still packets.
1092 *
1093 * spider_net_poll returns all packets from the rx descriptors to the stack
1094 * (using netif_receive_skb). If all/enough packets are up, the driver
1095 * reenables interrupts and returns 0. If not, 1 is returned.
1096 */
1097static int
1098spider_net_poll(struct net_device *netdev, int *budget)
1099{
1100 struct spider_net_card *card = netdev_priv(netdev);
1101 int packets_to_do, packets_done = 0;
1102 int no_more_packets = 0;
1103
Linas Vepstas68a8c602006-10-10 16:13:05 -05001104 spider_net_cleanup_tx_ring(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001105 packets_to_do = min(*budget, netdev->quota);
1106
1107 while (packets_to_do) {
Linas Vepstas1cd173f2006-12-13 15:12:26 -06001108 if (spider_net_decode_one_descr(card)) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001109 packets_done++;
1110 packets_to_do--;
1111 } else {
1112 /* no more packets for the stack */
1113 no_more_packets = 1;
1114 break;
1115 }
1116 }
1117
1118 netdev->quota -= packets_done;
1119 *budget -= packets_done;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001120 spider_net_refill_rx_chain(card);
Linas Vepstas80dab7c2006-12-13 15:18:52 -06001121 spider_net_enable_rxdmac(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001122
1123 /* if all packets are in the stack, enable interrupts and return 0 */
1124 /* if not, return 1 */
1125 if (no_more_packets) {
1126 netif_rx_complete(netdev);
1127 spider_net_rx_irq_on(card);
1128 return 0;
1129 }
1130
1131 return 1;
1132}
1133
1134/**
1135 * spider_net_vlan_rx_reg - initializes VLAN structures in the driver and card
1136 * @netdev: interface device structure
1137 * @grp: vlan_group structure that is registered (NULL on destroying interface)
1138 */
1139static void
1140spider_net_vlan_rx_reg(struct net_device *netdev, struct vlan_group *grp)
1141{
1142 /* further enhancement... yet to do */
1143 return;
1144}
1145
1146/**
1147 * spider_net_vlan_rx_add - adds VLAN id to the card filter
1148 * @netdev: interface device structure
1149 * @vid: VLAN id to add
1150 */
1151static void
1152spider_net_vlan_rx_add(struct net_device *netdev, uint16_t vid)
1153{
1154 /* further enhancement... yet to do */
1155 /* add vid to card's VLAN filter table */
1156 return;
1157}
1158
1159/**
1160 * spider_net_vlan_rx_kill - removes VLAN id to the card filter
1161 * @netdev: interface device structure
1162 * @vid: VLAN id to remove
1163 */
1164static void
1165spider_net_vlan_rx_kill(struct net_device *netdev, uint16_t vid)
1166{
1167 /* further enhancement... yet to do */
1168 /* remove vid from card's VLAN filter table */
1169}
1170
1171/**
1172 * spider_net_get_stats - get interface statistics
1173 * @netdev: interface device structure
1174 *
1175 * returns the interface statistics residing in the spider_net_card struct
1176 */
1177static struct net_device_stats *
1178spider_net_get_stats(struct net_device *netdev)
1179{
1180 struct spider_net_card *card = netdev_priv(netdev);
1181 struct net_device_stats *stats = &card->netdev_stats;
1182 return stats;
1183}
1184
1185/**
1186 * spider_net_change_mtu - changes the MTU of an interface
1187 * @netdev: interface device structure
1188 * @new_mtu: new MTU value
1189 *
1190 * returns 0 on success, <0 on failure
1191 */
1192static int
1193spider_net_change_mtu(struct net_device *netdev, int new_mtu)
1194{
1195 /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
1196 * and mtu is outbound only anyway */
1197 if ( (new_mtu < SPIDER_NET_MIN_MTU ) ||
1198 (new_mtu > SPIDER_NET_MAX_MTU) )
1199 return -EINVAL;
1200 netdev->mtu = new_mtu;
1201 return 0;
1202}
1203
1204/**
1205 * spider_net_set_mac - sets the MAC of an interface
1206 * @netdev: interface device structure
1207 * @ptr: pointer to new MAC address
1208 *
1209 * Returns 0 on success, <0 on failure. Currently, we don't support this
1210 * and will always return EOPNOTSUPP.
1211 */
1212static int
1213spider_net_set_mac(struct net_device *netdev, void *p)
1214{
1215 struct spider_net_card *card = netdev_priv(netdev);
Jens Osterkamp054034d2005-09-06 19:30:55 -07001216 u32 macl, macu, regvalue;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001217 struct sockaddr *addr = p;
1218
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001219 if (!is_valid_ether_addr(addr->sa_data))
1220 return -EADDRNOTAVAIL;
1221
Jens Osterkamp054034d2005-09-06 19:30:55 -07001222 /* switch off GMACTPE and GMACRPE */
1223 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1224 regvalue &= ~((1 << 5) | (1 << 6));
1225 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1226
1227 /* write mac */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001228 macu = (addr->sa_data[0]<<24) + (addr->sa_data[1]<<16) +
1229 (addr->sa_data[2]<<8) + (addr->sa_data[3]);
1230 macl = (addr->sa_data[4]<<8) + (addr->sa_data[5]);
1231 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACU, macu);
1232 spider_net_write_reg(card, SPIDER_NET_GMACUNIMACL, macl);
1233
Jens Osterkamp054034d2005-09-06 19:30:55 -07001234 /* switch GMACTPE and GMACRPE back on */
1235 regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD);
1236 regvalue |= ((1 << 5) | (1 << 6));
1237 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue);
1238
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001239 spider_net_set_promisc(card);
1240
1241 /* look up, whether we have been successful */
1242 if (spider_net_get_mac_address(netdev))
1243 return -EADDRNOTAVAIL;
1244 if (memcmp(netdev->dev_addr,addr->sa_data,netdev->addr_len))
1245 return -EADDRNOTAVAIL;
1246
1247 return 0;
1248}
1249
1250/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001251 * spider_net_handle_error_irq - handles errors raised by an interrupt
1252 * @card: card structure
1253 * @status_reg: interrupt status register 0 (GHIINT0STS)
1254 *
1255 * spider_net_handle_error_irq treats or ignores all error conditions
1256 * found when an interrupt is presented
1257 */
1258static void
1259spider_net_handle_error_irq(struct spider_net_card *card, u32 status_reg)
1260{
1261 u32 error_reg1, error_reg2;
1262 u32 i;
1263 int show_error = 1;
1264
1265 error_reg1 = spider_net_read_reg(card, SPIDER_NET_GHIINT1STS);
1266 error_reg2 = spider_net_read_reg(card, SPIDER_NET_GHIINT2STS);
1267
1268 /* check GHIINT0STS ************************************/
1269 if (status_reg)
1270 for (i = 0; i < 32; i++)
1271 if (status_reg & (1<<i))
1272 switch (i)
1273 {
1274 /* let error_reg1 and error_reg2 evaluation decide, what to do
1275 case SPIDER_NET_PHYINT:
1276 case SPIDER_NET_GMAC2INT:
1277 case SPIDER_NET_GMAC1INT:
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001278 case SPIDER_NET_GFIFOINT:
1279 case SPIDER_NET_DMACINT:
1280 case SPIDER_NET_GSYSINT:
1281 break; */
1282
Linas Vepstas98b90402006-10-10 16:08:42 -05001283 case SPIDER_NET_GIPSINT:
1284 show_error = 0;
1285 break;
1286
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001287 case SPIDER_NET_GPWOPCMPINT:
1288 /* PHY write operation completed */
1289 show_error = 0;
1290 break;
1291 case SPIDER_NET_GPROPCMPINT:
1292 /* PHY read operation completed */
1293 /* we don't use semaphores, as we poll for the completion
1294 * of the read operation in spider_net_read_phy. Should take
1295 * about 50 us */
1296 show_error = 0;
1297 break;
1298 case SPIDER_NET_GPWFFINT:
1299 /* PHY command queue full */
1300 if (netif_msg_intr(card))
1301 pr_err("PHY write queue full\n");
1302 show_error = 0;
1303 break;
1304
1305 /* case SPIDER_NET_GRMDADRINT: not used. print a message */
1306 /* case SPIDER_NET_GRMARPINT: not used. print a message */
1307 /* case SPIDER_NET_GRMMPINT: not used. print a message */
1308
1309 case SPIDER_NET_GDTDEN0INT:
1310 /* someone has set TX_DMA_EN to 0 */
1311 show_error = 0;
1312 break;
1313
1314 case SPIDER_NET_GDDDEN0INT: /* fallthrough */
1315 case SPIDER_NET_GDCDEN0INT: /* fallthrough */
1316 case SPIDER_NET_GDBDEN0INT: /* fallthrough */
1317 case SPIDER_NET_GDADEN0INT:
1318 /* someone has set RX_DMA_EN to 0 */
1319 show_error = 0;
1320 break;
1321
1322 /* RX interrupts */
1323 case SPIDER_NET_GDDFDCINT:
1324 case SPIDER_NET_GDCFDCINT:
1325 case SPIDER_NET_GDBFDCINT:
1326 case SPIDER_NET_GDAFDCINT:
1327 /* case SPIDER_NET_GDNMINT: not used. print a message */
1328 /* case SPIDER_NET_GCNMINT: not used. print a message */
1329 /* case SPIDER_NET_GBNMINT: not used. print a message */
1330 /* case SPIDER_NET_GANMINT: not used. print a message */
1331 /* case SPIDER_NET_GRFNMINT: not used. print a message */
1332 show_error = 0;
1333 break;
1334
1335 /* TX interrupts */
1336 case SPIDER_NET_GDTFDCINT:
1337 show_error = 0;
1338 break;
1339 case SPIDER_NET_GTTEDINT:
1340 show_error = 0;
1341 break;
1342 case SPIDER_NET_GDTDCEINT:
1343 /* chain end. If a descriptor should be sent, kick off
1344 * tx dma
Linas Vepstas98b90402006-10-10 16:08:42 -05001345 if (card->tx_chain.tail != card->tx_chain.head)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001346 spider_net_kick_tx_dma(card);
Linas Vepstas98b90402006-10-10 16:08:42 -05001347 */
1348 show_error = 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001349 break;
1350
1351 /* case SPIDER_NET_G1TMCNTINT: not used. print a message */
1352 /* case SPIDER_NET_GFREECNTINT: not used. print a message */
1353 }
1354
1355 /* check GHIINT1STS ************************************/
1356 if (error_reg1)
1357 for (i = 0; i < 32; i++)
1358 if (error_reg1 & (1<<i))
1359 switch (i)
1360 {
1361 case SPIDER_NET_GTMFLLINT:
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001362 if (netif_msg_intr(card) && net_ratelimit())
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001363 pr_err("Spider TX RAM full\n");
1364 show_error = 0;
1365 break;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001366 case SPIDER_NET_GRFDFLLINT: /* fallthrough */
1367 case SPIDER_NET_GRFCFLLINT: /* fallthrough */
1368 case SPIDER_NET_GRFBFLLINT: /* fallthrough */
1369 case SPIDER_NET_GRFAFLLINT: /* fallthrough */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001370 case SPIDER_NET_GRMFLLINT:
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001371 if (netif_msg_intr(card) && net_ratelimit())
Linas Vepstas5a028872006-12-13 15:08:25 -06001372 pr_err("Spider RX RAM full, incoming packets "
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001373 "might be discarded!\n");
1374 spider_net_rx_irq_off(card);
Linas Vepstas75856172006-12-13 15:10:06 -06001375 netif_rx_schedule(card->netdev);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001376 show_error = 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001377 break;
1378
1379 /* case SPIDER_NET_GTMSHTINT: problem, print a message */
1380 case SPIDER_NET_GDTINVDINT:
1381 /* allrighty. tx from previous descr ok */
1382 show_error = 0;
1383 break;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001384
1385 /* chain end */
1386 case SPIDER_NET_GDDDCEINT: /* fallthrough */
1387 case SPIDER_NET_GDCDCEINT: /* fallthrough */
1388 case SPIDER_NET_GDBDCEINT: /* fallthrough */
1389 case SPIDER_NET_GDADCEINT:
Linas Vepstas5a028872006-12-13 15:08:25 -06001390 if (netif_msg_intr(card) && net_ratelimit())
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001391 pr_err("got descriptor chain end interrupt, "
1392 "restarting DMAC %c.\n",
Linas Vepstas37aad752006-10-10 16:06:53 -05001393 'D'-(i-SPIDER_NET_GDDDCEINT)/3);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001394 spider_net_refill_rx_chain(card);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001395 spider_net_enable_rxdmac(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001396 show_error = 0;
1397 break;
1398
1399 /* invalid descriptor */
1400 case SPIDER_NET_GDDINVDINT: /* fallthrough */
1401 case SPIDER_NET_GDCINVDINT: /* fallthrough */
1402 case SPIDER_NET_GDBINVDINT: /* fallthrough */
1403 case SPIDER_NET_GDAINVDINT:
1404 /* could happen when rx chain is full */
1405 spider_net_refill_rx_chain(card);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001406 spider_net_enable_rxdmac(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001407 show_error = 0;
1408 break;
1409
1410 /* case SPIDER_NET_GDTRSERINT: problem, print a message */
1411 /* case SPIDER_NET_GDDRSERINT: problem, print a message */
1412 /* case SPIDER_NET_GDCRSERINT: problem, print a message */
1413 /* case SPIDER_NET_GDBRSERINT: problem, print a message */
1414 /* case SPIDER_NET_GDARSERINT: problem, print a message */
1415 /* case SPIDER_NET_GDSERINT: problem, print a message */
1416 /* case SPIDER_NET_GDTPTERINT: problem, print a message */
1417 /* case SPIDER_NET_GDDPTERINT: problem, print a message */
1418 /* case SPIDER_NET_GDCPTERINT: problem, print a message */
1419 /* case SPIDER_NET_GDBPTERINT: problem, print a message */
1420 /* case SPIDER_NET_GDAPTERINT: problem, print a message */
1421 default:
1422 show_error = 1;
1423 break;
1424 }
1425
1426 /* check GHIINT2STS ************************************/
1427 if (error_reg2)
1428 for (i = 0; i < 32; i++)
1429 if (error_reg2 & (1<<i))
1430 switch (i)
1431 {
1432 /* there is nothing we can (want to) do at this time. Log a
1433 * message, we can switch on and off the specific values later on
1434 case SPIDER_NET_GPROPERINT:
1435 case SPIDER_NET_GMCTCRSNGINT:
1436 case SPIDER_NET_GMCTLCOLINT:
1437 case SPIDER_NET_GMCTTMOTINT:
1438 case SPIDER_NET_GMCRCAERINT:
1439 case SPIDER_NET_GMCRCALERINT:
1440 case SPIDER_NET_GMCRALNERINT:
1441 case SPIDER_NET_GMCROVRINT:
1442 case SPIDER_NET_GMCRRNTINT:
1443 case SPIDER_NET_GMCRRXERINT:
1444 case SPIDER_NET_GTITCSERINT:
1445 case SPIDER_NET_GTIFMTERINT:
1446 case SPIDER_NET_GTIPKTRVKINT:
1447 case SPIDER_NET_GTISPINGINT:
1448 case SPIDER_NET_GTISADNGINT:
1449 case SPIDER_NET_GTISPDNGINT:
1450 case SPIDER_NET_GRIFMTERINT:
1451 case SPIDER_NET_GRIPKTRVKINT:
1452 case SPIDER_NET_GRISPINGINT:
1453 case SPIDER_NET_GRISADNGINT:
1454 case SPIDER_NET_GRISPDNGINT:
1455 break;
1456 */
1457 default:
1458 break;
1459 }
1460
Linas Vepstas5a028872006-12-13 15:08:25 -06001461 if ((show_error) && (netif_msg_intr(card)) && net_ratelimit())
Linas Vepstas98b90402006-10-10 16:08:42 -05001462 pr_err("Got error interrupt on %s, GHIINT0STS = 0x%08x, "
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001463 "GHIINT1STS = 0x%08x, GHIINT2STS = 0x%08x\n",
Linas Vepstas98b90402006-10-10 16:08:42 -05001464 card->netdev->name,
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001465 status_reg, error_reg1, error_reg2);
1466
1467 /* clear interrupt sources */
1468 spider_net_write_reg(card, SPIDER_NET_GHIINT1STS, error_reg1);
1469 spider_net_write_reg(card, SPIDER_NET_GHIINT2STS, error_reg2);
1470}
1471
1472/**
1473 * spider_net_interrupt - interrupt handler for spider_net
1474 * @irq: interupt number
1475 * @ptr: pointer to net_device
1476 * @regs: PU registers
1477 *
1478 * returns IRQ_HANDLED, if interrupt was for driver, or IRQ_NONE, if no
1479 * interrupt found raised by card.
1480 *
1481 * This is the interrupt handler, that turns off
1482 * interrupts for this device and makes the stack poll the driver
1483 */
1484static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001485spider_net_interrupt(int irq, void *ptr)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001486{
1487 struct net_device *netdev = ptr;
1488 struct spider_net_card *card = netdev_priv(netdev);
1489 u32 status_reg;
1490
1491 status_reg = spider_net_read_reg(card, SPIDER_NET_GHIINT0STS);
1492
1493 if (!status_reg)
1494 return IRQ_NONE;
1495
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001496 if (status_reg & SPIDER_NET_RXINT ) {
1497 spider_net_rx_irq_off(card);
1498 netif_rx_schedule(netdev);
1499 }
Linas Vepstas68a8c602006-10-10 16:13:05 -05001500 if (status_reg & SPIDER_NET_TXINT)
1501 netif_rx_schedule(netdev);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001502
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001503 if (status_reg & SPIDER_NET_ERRINT )
1504 spider_net_handle_error_irq(card, status_reg);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001505
1506 /* clear interrupt sources */
1507 spider_net_write_reg(card, SPIDER_NET_GHIINT0STS, status_reg);
1508
1509 return IRQ_HANDLED;
1510}
1511
1512#ifdef CONFIG_NET_POLL_CONTROLLER
1513/**
1514 * spider_net_poll_controller - artificial interrupt for netconsole etc.
1515 * @netdev: interface device structure
1516 *
1517 * see Documentation/networking/netconsole.txt
1518 */
1519static void
1520spider_net_poll_controller(struct net_device *netdev)
1521{
1522 disable_irq(netdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01001523 spider_net_interrupt(netdev->irq, netdev);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001524 enable_irq(netdev->irq);
1525}
1526#endif /* CONFIG_NET_POLL_CONTROLLER */
1527
1528/**
1529 * spider_net_init_card - initializes the card
1530 * @card: card structure
1531 *
1532 * spider_net_init_card initializes the card so that other registers can
1533 * be used
1534 */
1535static void
1536spider_net_init_card(struct spider_net_card *card)
1537{
1538 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1539 SPIDER_NET_CKRCTRL_STOP_VALUE);
1540
1541 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1542 SPIDER_NET_CKRCTRL_RUN_VALUE);
1543}
1544
1545/**
1546 * spider_net_enable_card - enables the card by setting all kinds of regs
1547 * @card: card structure
1548 *
1549 * spider_net_enable_card sets a lot of SMMIO registers to enable the device
1550 */
1551static void
1552spider_net_enable_card(struct spider_net_card *card)
1553{
1554 int i;
1555 /* the following array consists of (register),(value) pairs
1556 * that are set in this function. A register of 0 ends the list */
1557 u32 regs[][2] = {
1558 { SPIDER_NET_GRESUMINTNUM, 0 },
1559 { SPIDER_NET_GREINTNUM, 0 },
1560
1561 /* set interrupt frame number registers */
1562 /* clear the single DMA engine registers first */
1563 { SPIDER_NET_GFAFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1564 { SPIDER_NET_GFBFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1565 { SPIDER_NET_GFCFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1566 { SPIDER_NET_GFDFRMNUM, SPIDER_NET_GFXFRAMES_VALUE },
1567 /* then set, what we really need */
1568 { SPIDER_NET_GFFRMNUM, SPIDER_NET_FRAMENUM_VALUE },
1569
1570 /* timer counter registers and stuff */
1571 { SPIDER_NET_GFREECNNUM, 0 },
1572 { SPIDER_NET_GONETIMENUM, 0 },
1573 { SPIDER_NET_GTOUTFRMNUM, 0 },
1574
1575 /* RX mode setting */
1576 { SPIDER_NET_GRXMDSET, SPIDER_NET_RXMODE_VALUE },
1577 /* TX mode setting */
1578 { SPIDER_NET_GTXMDSET, SPIDER_NET_TXMODE_VALUE },
1579 /* IPSEC mode setting */
1580 { SPIDER_NET_GIPSECINIT, SPIDER_NET_IPSECINIT_VALUE },
1581
1582 { SPIDER_NET_GFTRESTRT, SPIDER_NET_RESTART_VALUE },
1583
1584 { SPIDER_NET_GMRWOLCTRL, 0 },
Jens Osterkampb636d172006-05-04 05:59:56 -04001585 { SPIDER_NET_GTESTMD, 0x10000000 },
1586 { SPIDER_NET_GTTQMSK, 0x00400040 },
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001587
1588 { SPIDER_NET_GMACINTEN, 0 },
1589
1590 /* flow control stuff */
1591 { SPIDER_NET_GMACAPAUSE, SPIDER_NET_MACAPAUSE_VALUE },
1592 { SPIDER_NET_GMACTXPAUSE, SPIDER_NET_TXPAUSE_VALUE },
1593
1594 { SPIDER_NET_GMACBSTLMT, SPIDER_NET_BURSTLMT_VALUE },
1595 { 0, 0}
1596 };
1597
1598 i = 0;
1599 while (regs[i][0]) {
1600 spider_net_write_reg(card, regs[i][0], regs[i][1]);
1601 i++;
1602 }
1603
1604 /* clear unicast filter table entries 1 to 14 */
1605 for (i = 1; i <= 14; i++) {
1606 spider_net_write_reg(card,
1607 SPIDER_NET_GMRUAFILnR + i * 8,
1608 0x00080000);
1609 spider_net_write_reg(card,
1610 SPIDER_NET_GMRUAFILnR + i * 8 + 4,
1611 0x00000000);
1612 }
1613
1614 spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R, 0x08080000);
1615
1616 spider_net_write_reg(card, SPIDER_NET_ECMODE, SPIDER_NET_ECMODE_VALUE);
1617
1618 /* set chain tail adress for RX chains and
1619 * enable DMA */
1620 spider_net_enable_rxchtails(card);
1621 spider_net_enable_rxdmac(card);
1622
1623 spider_net_write_reg(card, SPIDER_NET_GRXDMAEN, SPIDER_NET_WOL_VALUE);
1624
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001625 spider_net_write_reg(card, SPIDER_NET_GMACLENLMT,
1626 SPIDER_NET_LENLMT_VALUE);
1627 spider_net_write_reg(card, SPIDER_NET_GMACMODE,
1628 SPIDER_NET_MACMODE_VALUE);
1629 spider_net_write_reg(card, SPIDER_NET_GMACOPEMD,
1630 SPIDER_NET_OPMODE_VALUE);
1631
1632 /* set interrupt mask registers */
1633 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK,
1634 SPIDER_NET_INT0_MASK_VALUE);
1635 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK,
1636 SPIDER_NET_INT1_MASK_VALUE);
1637 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK,
1638 SPIDER_NET_INT2_MASK_VALUE);
Jens Osterkampbdd01502006-07-13 11:54:08 +02001639
1640 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
Linas Vepstas7bd54c82006-11-17 14:39:40 -08001641 SPIDER_NET_GDTBSTA);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001642}
1643
1644/**
1645 * spider_net_open - called upon ifonfig up
1646 * @netdev: interface device structure
1647 *
1648 * returns 0 on success, <0 on failure
1649 *
1650 * spider_net_open allocates all the descriptors and memory needed for
1651 * operation, sets up multicast list and enables interrupts
1652 */
1653int
1654spider_net_open(struct net_device *netdev)
1655{
1656 struct spider_net_card *card = netdev_priv(netdev);
Linas Vepstasd4ed8f82006-12-13 15:06:59 -06001657 int result;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001658
Linas Vepstasd4ed8f82006-12-13 15:06:59 -06001659 result = spider_net_init_chain(card, &card->tx_chain);
1660 if (result)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001661 goto alloc_tx_failed;
Linas Vepstas204e5fa2006-10-10 16:11:33 -05001662 card->low_watermark = NULL;
1663
Linas Vepstasd4ed8f82006-12-13 15:06:59 -06001664 result = spider_net_init_chain(card, &card->rx_chain);
1665 if (result)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001666 goto alloc_rx_failed;
1667
Linas Vepstasd4ed8f82006-12-13 15:06:59 -06001668 /* Allocate rx skbs */
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001669 if (spider_net_alloc_rx_skbs(card))
1670 goto alloc_skbs_failed;
1671
1672 spider_net_set_multi(netdev);
1673
1674 /* further enhancement: setup hw vlan, if needed */
1675
1676 result = -EBUSY;
1677 if (request_irq(netdev->irq, spider_net_interrupt,
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07001678 IRQF_SHARED, netdev->name, netdev))
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001679 goto register_int_failed;
1680
1681 spider_net_enable_card(card);
1682
Jens Osterkamp543cec52005-09-06 19:30:54 -07001683 netif_start_queue(netdev);
1684 netif_carrier_on(netdev);
1685 netif_poll_enable(netdev);
1686
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001687 return 0;
1688
1689register_int_failed:
1690 spider_net_free_rx_chain_contents(card);
1691alloc_skbs_failed:
1692 spider_net_free_chain(card, &card->rx_chain);
1693alloc_rx_failed:
1694 spider_net_free_chain(card, &card->tx_chain);
1695alloc_tx_failed:
1696 return result;
1697}
1698
1699/**
1700 * spider_net_setup_phy - setup PHY
1701 * @card: card structure
1702 *
1703 * returns 0 on success, <0 on failure
1704 *
1705 * spider_net_setup_phy is used as part of spider_net_probe. Sets
1706 * the PHY to 1000 Mbps
1707 **/
1708static int
1709spider_net_setup_phy(struct spider_net_card *card)
1710{
1711 struct mii_phy *phy = &card->phy;
1712
1713 spider_net_write_reg(card, SPIDER_NET_GDTDMASEL,
1714 SPIDER_NET_DMASEL_VALUE);
1715 spider_net_write_reg(card, SPIDER_NET_GPCCTRL,
1716 SPIDER_NET_PHY_CTRL_VALUE);
1717 phy->mii_id = 1;
1718 phy->dev = card->netdev;
1719 phy->mdio_read = spider_net_read_phy;
1720 phy->mdio_write = spider_net_write_phy;
1721
1722 mii_phy_probe(phy, phy->mii_id);
1723
1724 if (phy->def->ops->setup_forced)
1725 phy->def->ops->setup_forced(phy, SPEED_1000, DUPLEX_FULL);
1726
Jens Osterkamp8ec93452006-05-04 05:59:41 -04001727 phy->def->ops->enable_fiber(phy);
Jens Osterkamp53abbf72005-09-16 08:55:33 +02001728
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001729 phy->def->ops->read_link(phy);
1730 pr_info("Found %s with %i Mbps, %s-duplex.\n", phy->def->name,
1731 phy->speed, phy->duplex==1 ? "Full" : "Half");
1732
1733 return 0;
1734}
1735
1736/**
1737 * spider_net_download_firmware - loads firmware into the adapter
1738 * @card: card structure
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001739 * @firmware_ptr: pointer to firmware data
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001740 *
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001741 * spider_net_download_firmware loads the firmware data into the
1742 * adapter. It assumes the length etc. to be allright.
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001743 */
Arnd Bergmann0d3ea162006-01-12 17:16:41 -05001744static int
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001745spider_net_download_firmware(struct spider_net_card *card,
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001746 const void *firmware_ptr)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001747{
1748 int sequencer, i;
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001749 const u32 *fw_ptr = firmware_ptr;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001750
1751 /* stop sequencers */
1752 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1753 SPIDER_NET_STOP_SEQ_VALUE);
1754
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001755 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1756 sequencer++) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001757 spider_net_write_reg(card,
1758 SPIDER_NET_GSnPRGADR + sequencer * 8, 0);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001759 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001760 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1761 sequencer * 8, *fw_ptr);
1762 fw_ptr++;
1763 }
1764 }
1765
Arnd Bergmann0d3ea162006-01-12 17:16:41 -05001766 if (spider_net_read_reg(card, SPIDER_NET_GSINIT))
1767 return -EIO;
1768
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001769 spider_net_write_reg(card, SPIDER_NET_GSINIT,
1770 SPIDER_NET_RUN_SEQ_VALUE);
Arnd Bergmann0d3ea162006-01-12 17:16:41 -05001771
1772 return 0;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001773}
1774
1775/**
1776 * spider_net_init_firmware - reads in firmware parts
1777 * @card: card structure
1778 *
1779 * Returns 0 on success, <0 on failure
1780 *
1781 * spider_net_init_firmware opens the sequencer firmware and does some basic
1782 * checks. This function opens and releases the firmware structure. A call
1783 * to download the firmware is performed before the release.
1784 *
1785 * Firmware format
1786 * ===============
1787 * spider_fw.bin is expected to be a file containing 6*1024*4 bytes, 4k being
1788 * the program for each sequencer. Use the command
1789 * tail -q -n +2 Seq_code1_0x088.txt Seq_code2_0x090.txt \
1790 * Seq_code3_0x098.txt Seq_code4_0x0A0.txt Seq_code5_0x0A8.txt \
1791 * Seq_code6_0x0B0.txt | xxd -r -p -c4 > spider_fw.bin
1792 *
1793 * to generate spider_fw.bin, if you have sequencer programs with something
1794 * like the following contents for each sequencer:
1795 * <ONE LINE COMMENT>
1796 * <FIRST 4-BYTES-WORD FOR SEQUENCER>
1797 * <SECOND 4-BYTES-WORD FOR SEQUENCER>
1798 * ...
1799 * <1024th 4-BYTES-WORD FOR SEQUENCER>
1800 */
1801static int
1802spider_net_init_firmware(struct spider_net_card *card)
1803{
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001804 struct firmware *firmware = NULL;
Arnd Bergmann030d6752006-01-12 17:16:42 -05001805 struct device_node *dn;
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001806 const u8 *fw_prop = NULL;
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001807 int err = -ENOENT;
1808 int fw_size;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001809
Arnd Bergmann030d6752006-01-12 17:16:42 -05001810 if (request_firmware((const struct firmware **)&firmware,
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001811 SPIDER_NET_FIRMWARE_NAME, &card->pdev->dev) == 0) {
1812 if ( (firmware->size != SPIDER_NET_FIRMWARE_LEN) &&
1813 netif_msg_probe(card) ) {
1814 pr_err("Incorrect size of spidernet firmware in " \
1815 "filesystem. Looking in host firmware...\n");
1816 goto try_host_fw;
1817 }
1818 err = spider_net_download_firmware(card, firmware->data);
Arnd Bergmann030d6752006-01-12 17:16:42 -05001819
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001820 release_firmware(firmware);
1821 if (err)
1822 goto try_host_fw;
Arnd Bergmann030d6752006-01-12 17:16:42 -05001823
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001824 goto done;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001825 }
1826
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001827try_host_fw:
1828 dn = pci_device_to_OF_node(card->pdev);
1829 if (!dn)
1830 goto out_err;
1831
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001832 fw_prop = get_property(dn, "firmware", &fw_size);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001833 if (!fw_prop)
1834 goto out_err;
1835
1836 if ( (fw_size != SPIDER_NET_FIRMWARE_LEN) &&
1837 netif_msg_probe(card) ) {
1838 pr_err("Incorrect size of spidernet firmware in " \
1839 "host firmware\n");
1840 goto done;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001841 }
1842
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001843 err = spider_net_download_firmware(card, fw_prop);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001844
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001845done:
1846 return err;
1847out_err:
1848 if (netif_msg_probe(card))
1849 pr_err("Couldn't find spidernet firmware in filesystem " \
1850 "or host firmware\n");
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001851 return err;
1852}
1853
1854/**
1855 * spider_net_workaround_rxramfull - work around firmware bug
1856 * @card: card structure
1857 *
1858 * no return value
1859 **/
1860static void
1861spider_net_workaround_rxramfull(struct spider_net_card *card)
1862{
1863 int i, sequencer = 0;
1864
1865 /* cancel reset */
1866 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1867 SPIDER_NET_CKRCTRL_RUN_VALUE);
1868
1869 /* empty sequencer data */
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001870 for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS;
1871 sequencer++) {
Jens Osterkampee962a52006-07-13 11:54:23 +02001872 spider_net_write_reg(card, SPIDER_NET_GSnPRGADR +
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001873 sequencer * 8, 0x0);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05001874 for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001875 spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT +
1876 sequencer * 8, 0x0);
1877 }
1878 }
1879
1880 /* set sequencer operation */
1881 spider_net_write_reg(card, SPIDER_NET_GSINIT, 0x000000fe);
1882
1883 /* reset */
1884 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
1885 SPIDER_NET_CKRCTRL_STOP_VALUE);
1886}
1887
1888/**
Jens Osterkampbdd01502006-07-13 11:54:08 +02001889 * spider_net_stop - called upon ifconfig down
1890 * @netdev: interface device structure
1891 *
1892 * always returns 0
1893 */
1894int
1895spider_net_stop(struct net_device *netdev)
1896{
1897 struct spider_net_card *card = netdev_priv(netdev);
1898
Jens Osterkampbdd01502006-07-13 11:54:08 +02001899 netif_poll_disable(netdev);
1900 netif_carrier_off(netdev);
1901 netif_stop_queue(netdev);
1902 del_timer_sync(&card->tx_timer);
1903
1904 /* disable/mask all interrupts */
1905 spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, 0);
1906 spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK, 0);
1907 spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK, 0);
1908
1909 /* free_irq(netdev->irq, netdev);*/
1910 free_irq(to_pci_dev(netdev->class_dev.dev)->irq, netdev);
1911
1912 spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR,
1913 SPIDER_NET_DMA_TX_FEND_VALUE);
1914
1915 /* turn off DMA, force end */
1916 spider_net_disable_rxdmac(card);
1917
1918 /* release chains */
Linas Vepstas9cc7bf72006-10-10 16:14:29 -05001919 spider_net_release_tx_chain(card, 1);
Linas Vepstasd4ed8f82006-12-13 15:06:59 -06001920 spider_net_free_rx_chain_contents(card);
Jens Osterkampbdd01502006-07-13 11:54:08 +02001921
Jens Osterkampa55eb052007-02-01 12:07:47 +01001922 spider_net_free_rx_chain_contents(card);
1923
Jens Osterkampbdd01502006-07-13 11:54:08 +02001924 spider_net_free_chain(card, &card->tx_chain);
1925 spider_net_free_chain(card, &card->rx_chain);
1926
1927 return 0;
1928}
1929
1930/**
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001931 * spider_net_tx_timeout_task - task scheduled by the watchdog timeout
1932 * function (to be called not under interrupt status)
1933 * @data: data, is interface device structure
1934 *
1935 * called as task when tx hangs, resets interface (if interface is up)
1936 */
1937static void
David Howellsc4028952006-11-22 14:57:56 +00001938spider_net_tx_timeout_task(struct work_struct *work)
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001939{
David Howellsc4028952006-11-22 14:57:56 +00001940 struct spider_net_card *card =
1941 container_of(work, struct spider_net_card, tx_timeout_task);
1942 struct net_device *netdev = card->netdev;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001943
1944 if (!(netdev->flags & IFF_UP))
1945 goto out;
1946
1947 netif_device_detach(netdev);
1948 spider_net_stop(netdev);
1949
1950 spider_net_workaround_rxramfull(card);
1951 spider_net_init_card(card);
1952
1953 if (spider_net_setup_phy(card))
1954 goto out;
1955 if (spider_net_init_firmware(card))
1956 goto out;
1957
1958 spider_net_open(netdev);
Jens Osterkampbdd01502006-07-13 11:54:08 +02001959 spider_net_kick_tx_dma(card);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001960 netif_device_attach(netdev);
1961
1962out:
1963 atomic_dec(&card->tx_timeout_task_counter);
1964}
1965
1966/**
1967 * spider_net_tx_timeout - called when the tx timeout watchdog kicks in.
1968 * @netdev: interface device structure
1969 *
1970 * called, if tx hangs. Schedules a task that resets the interface
1971 */
1972static void
1973spider_net_tx_timeout(struct net_device *netdev)
1974{
1975 struct spider_net_card *card;
1976
1977 card = netdev_priv(netdev);
1978 atomic_inc(&card->tx_timeout_task_counter);
1979 if (netdev->flags & IFF_UP)
1980 schedule_work(&card->tx_timeout_task);
1981 else
1982 atomic_dec(&card->tx_timeout_task_counter);
Jim Lewis9b6b0b82006-09-22 17:22:53 -07001983 card->spider_stats.tx_timeouts++;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07001984}
1985
1986/**
1987 * spider_net_setup_netdev_ops - initialization of net_device operations
1988 * @netdev: net_device structure
1989 *
1990 * fills out function pointers in the net_device structure
1991 */
1992static void
1993spider_net_setup_netdev_ops(struct net_device *netdev)
1994{
1995 netdev->open = &spider_net_open;
1996 netdev->stop = &spider_net_stop;
1997 netdev->hard_start_xmit = &spider_net_xmit;
1998 netdev->get_stats = &spider_net_get_stats;
1999 netdev->set_multicast_list = &spider_net_set_multi;
2000 netdev->set_mac_address = &spider_net_set_mac;
2001 netdev->change_mtu = &spider_net_change_mtu;
2002 netdev->do_ioctl = &spider_net_do_ioctl;
2003 /* tx watchdog */
2004 netdev->tx_timeout = &spider_net_tx_timeout;
2005 netdev->watchdog_timeo = SPIDER_NET_WATCHDOG_TIMEOUT;
2006 /* NAPI */
2007 netdev->poll = &spider_net_poll;
2008 netdev->weight = SPIDER_NET_NAPI_WEIGHT;
2009 /* HW VLAN */
2010 netdev->vlan_rx_register = &spider_net_vlan_rx_reg;
2011 netdev->vlan_rx_add_vid = &spider_net_vlan_rx_add;
2012 netdev->vlan_rx_kill_vid = &spider_net_vlan_rx_kill;
2013#ifdef CONFIG_NET_POLL_CONTROLLER
2014 /* poll controller */
2015 netdev->poll_controller = &spider_net_poll_controller;
2016#endif /* CONFIG_NET_POLL_CONTROLLER */
2017 /* ethtool ops */
2018 netdev->ethtool_ops = &spider_net_ethtool_ops;
2019}
2020
2021/**
2022 * spider_net_setup_netdev - initialization of net_device
2023 * @card: card structure
2024 *
2025 * Returns 0 on success or <0 on failure
2026 *
2027 * spider_net_setup_netdev initializes the net_device structure
2028 **/
2029static int
2030spider_net_setup_netdev(struct spider_net_card *card)
2031{
2032 int result;
2033 struct net_device *netdev = card->netdev;
2034 struct device_node *dn;
2035 struct sockaddr addr;
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10002036 const u8 *mac;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002037
2038 SET_MODULE_OWNER(netdev);
2039 SET_NETDEV_DEV(netdev, &card->pdev->dev);
2040
2041 pci_set_drvdata(card->pdev, netdev);
Arnd Bergmann11f1a522006-01-12 17:16:44 -05002042
Arnd Bergmann11f1a522006-01-12 17:16:44 -05002043 init_timer(&card->tx_timer);
2044 card->tx_timer.function =
2045 (void (*)(unsigned long)) spider_net_cleanup_tx_ring;
2046 card->tx_timer.data = (unsigned long) card;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002047 netdev->irq = card->pdev->irq;
2048
2049 card->options.rx_csum = SPIDER_NET_RX_CSUM_DEFAULT;
2050
Linas Vepstasd4ed8f82006-12-13 15:06:59 -06002051 card->tx_chain.num_desc = tx_descriptors;
2052 card->rx_chain.num_desc = rx_descriptors;
Jim Lewisb68a60e2006-08-14 23:00:23 -07002053
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002054 spider_net_setup_netdev_ops(netdev);
2055
Jens Osterkampbdd01502006-07-13 11:54:08 +02002056 netdev->features = NETIF_F_HW_CSUM | NETIF_F_LLTX;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002057 /* some time: NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
2058 * NETIF_F_HW_VLAN_FILTER */
2059
2060 netdev->irq = card->pdev->irq;
2061
2062 dn = pci_device_to_OF_node(card->pdev);
Jens Osterkamp543cec52005-09-06 19:30:54 -07002063 if (!dn)
2064 return -EIO;
2065
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10002066 mac = get_property(dn, "local-mac-address", NULL);
Jens Osterkamp543cec52005-09-06 19:30:54 -07002067 if (!mac)
2068 return -EIO;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002069 memcpy(addr.sa_data, mac, ETH_ALEN);
2070
2071 result = spider_net_set_mac(netdev, &addr);
2072 if ((result) && (netif_msg_probe(card)))
2073 pr_err("Failed to set MAC address: %i\n", result);
2074
2075 result = register_netdev(netdev);
2076 if (result) {
2077 if (netif_msg_probe(card))
2078 pr_err("Couldn't register net_device: %i\n",
2079 result);
2080 return result;
2081 }
2082
2083 if (netif_msg_probe(card))
2084 pr_info("Initialized device %s.\n", netdev->name);
2085
2086 return 0;
2087}
2088
2089/**
2090 * spider_net_alloc_card - allocates net_device and card structure
2091 *
2092 * returns the card structure or NULL in case of errors
2093 *
2094 * the card and net_device structures are linked to each other
2095 */
2096static struct spider_net_card *
2097spider_net_alloc_card(void)
2098{
2099 struct net_device *netdev;
2100 struct spider_net_card *card;
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002101
Linas Vepstasd4ed8f82006-12-13 15:06:59 -06002102 netdev = alloc_etherdev(sizeof(struct spider_net_card));
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002103 if (!netdev)
2104 return NULL;
2105
2106 card = netdev_priv(netdev);
2107 card->netdev = netdev;
2108 card->msg_enable = SPIDER_NET_DEFAULT_MSG;
David Howellsc4028952006-11-22 14:57:56 +00002109 INIT_WORK(&card->tx_timeout_task, spider_net_tx_timeout_task);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002110 init_waitqueue_head(&card->waitq);
2111 atomic_set(&card->tx_timeout_task_counter, 0);
2112
2113 return card;
2114}
2115
2116/**
2117 * spider_net_undo_pci_setup - releases PCI ressources
2118 * @card: card structure
2119 *
2120 * spider_net_undo_pci_setup releases the mapped regions
2121 */
2122static void
2123spider_net_undo_pci_setup(struct spider_net_card *card)
2124{
2125 iounmap(card->regs);
2126 pci_release_regions(card->pdev);
2127}
2128
2129/**
2130 * spider_net_setup_pci_dev - sets up the device in terms of PCI operations
2131 * @card: card structure
2132 * @pdev: PCI device
2133 *
2134 * Returns the card structure or NULL if any errors occur
2135 *
2136 * spider_net_setup_pci_dev initializes pdev and together with the
2137 * functions called in spider_net_open configures the device so that
2138 * data can be transferred over it
2139 * The net_device structure is attached to the card structure, if the
2140 * function returns without error.
2141 **/
2142static struct spider_net_card *
2143spider_net_setup_pci_dev(struct pci_dev *pdev)
2144{
2145 struct spider_net_card *card;
2146 unsigned long mmio_start, mmio_len;
2147
2148 if (pci_enable_device(pdev)) {
2149 pr_err("Couldn't enable PCI device\n");
2150 return NULL;
2151 }
2152
2153 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
2154 pr_err("Couldn't find proper PCI device base address.\n");
2155 goto out_disable_dev;
2156 }
2157
2158 if (pci_request_regions(pdev, spider_net_driver_name)) {
2159 pr_err("Couldn't obtain PCI resources, aborting.\n");
2160 goto out_disable_dev;
2161 }
2162
2163 pci_set_master(pdev);
2164
2165 card = spider_net_alloc_card();
2166 if (!card) {
2167 pr_err("Couldn't allocate net_device structure, "
2168 "aborting.\n");
2169 goto out_release_regions;
2170 }
2171 card->pdev = pdev;
2172
2173 /* fetch base address and length of first resource */
2174 mmio_start = pci_resource_start(pdev, 0);
2175 mmio_len = pci_resource_len(pdev, 0);
2176
2177 card->netdev->mem_start = mmio_start;
2178 card->netdev->mem_end = mmio_start + mmio_len;
2179 card->regs = ioremap(mmio_start, mmio_len);
2180
2181 if (!card->regs) {
2182 pr_err("Couldn't obtain PCI resources, aborting.\n");
2183 goto out_release_regions;
2184 }
2185
2186 return card;
2187
2188out_release_regions:
2189 pci_release_regions(pdev);
2190out_disable_dev:
2191 pci_disable_device(pdev);
2192 pci_set_drvdata(pdev, NULL);
2193 return NULL;
2194}
2195
2196/**
2197 * spider_net_probe - initialization of a device
2198 * @pdev: PCI device
2199 * @ent: entry in the device id list
2200 *
2201 * Returns 0 on success, <0 on failure
2202 *
2203 * spider_net_probe initializes pdev and registers a net_device
2204 * structure for it. After that, the device can be ifconfig'ed up
2205 **/
2206static int __devinit
2207spider_net_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2208{
2209 int err = -EIO;
2210 struct spider_net_card *card;
2211
2212 card = spider_net_setup_pci_dev(pdev);
2213 if (!card)
2214 goto out;
2215
2216 spider_net_workaround_rxramfull(card);
2217 spider_net_init_card(card);
2218
2219 err = spider_net_setup_phy(card);
2220 if (err)
2221 goto out_undo_pci;
2222
2223 err = spider_net_init_firmware(card);
2224 if (err)
2225 goto out_undo_pci;
2226
2227 err = spider_net_setup_netdev(card);
2228 if (err)
2229 goto out_undo_pci;
2230
2231 return 0;
2232
2233out_undo_pci:
2234 spider_net_undo_pci_setup(card);
2235 free_netdev(card->netdev);
2236out:
2237 return err;
2238}
2239
2240/**
2241 * spider_net_remove - removal of a device
2242 * @pdev: PCI device
2243 *
2244 * Returns 0 on success, <0 on failure
2245 *
2246 * spider_net_remove is called to remove the device and unregisters the
2247 * net_device
2248 **/
2249static void __devexit
2250spider_net_remove(struct pci_dev *pdev)
2251{
2252 struct net_device *netdev;
2253 struct spider_net_card *card;
2254
2255 netdev = pci_get_drvdata(pdev);
2256 card = netdev_priv(netdev);
2257
2258 wait_event(card->waitq,
2259 atomic_read(&card->tx_timeout_task_counter) == 0);
2260
2261 unregister_netdev(netdev);
Jens Osterkamp543cec52005-09-06 19:30:54 -07002262
2263 /* switch off card */
2264 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2265 SPIDER_NET_CKRCTRL_STOP_VALUE);
2266 spider_net_write_reg(card, SPIDER_NET_CKRCTRL,
2267 SPIDER_NET_CKRCTRL_RUN_VALUE);
2268
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002269 spider_net_undo_pci_setup(card);
2270 free_netdev(netdev);
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002271}
2272
2273static struct pci_driver spider_net_driver = {
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002274 .name = spider_net_driver_name,
2275 .id_table = spider_net_pci_tbl,
2276 .probe = spider_net_probe,
2277 .remove = __devexit_p(spider_net_remove)
2278};
2279
2280/**
2281 * spider_net_init - init function when the driver is loaded
2282 *
2283 * spider_net_init registers the device driver
2284 */
2285static int __init spider_net_init(void)
2286{
Linas Vepstas90f10842006-10-10 15:56:04 -05002287 printk(KERN_INFO "Spidernet version %s.\n", VERSION);
2288
Jens Osterkampaaec0fa2005-09-05 15:19:29 -07002289 if (rx_descriptors < SPIDER_NET_RX_DESCRIPTORS_MIN) {
2290 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MIN;
2291 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2292 }
2293 if (rx_descriptors > SPIDER_NET_RX_DESCRIPTORS_MAX) {
2294 rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MAX;
2295 pr_info("adjusting rx descriptors to %i.\n", rx_descriptors);
2296 }
2297 if (tx_descriptors < SPIDER_NET_TX_DESCRIPTORS_MIN) {
2298 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MIN;
2299 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2300 }
2301 if (tx_descriptors > SPIDER_NET_TX_DESCRIPTORS_MAX) {
2302 tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MAX;
2303 pr_info("adjusting tx descriptors to %i.\n", tx_descriptors);
2304 }
2305
2306 return pci_register_driver(&spider_net_driver);
2307}
2308
2309/**
2310 * spider_net_cleanup - exit function when driver is unloaded
2311 *
2312 * spider_net_cleanup unregisters the device driver
2313 */
2314static void __exit spider_net_cleanup(void)
2315{
2316 pci_unregister_driver(&spider_net_driver);
2317}
2318
2319module_init(spider_net_init);
2320module_exit(spider_net_cleanup);