]>
Commit | Line | Data |
---|---|---|
aaec0fab JO |
1 | /* |
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 | ||
aaec0fab JO |
24 | #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> | |
7c5c220e | 31 | #include <linux/in.h> |
aaec0fab JO |
32 | #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> | |
11f1a52b | 45 | #include <linux/vmalloc.h> |
aaec0fab JO |
46 | #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 | ||
54 | MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com> and Jens Osterkamp " \ | |
55 | "<Jens.Osterkamp@de.ibm.com>"); | |
56 | MODULE_DESCRIPTION("Spider Southbridge Gigabit Ethernet driver"); | |
57 | MODULE_LICENSE("GPL"); | |
90f10841 | 58 | MODULE_VERSION(VERSION); |
aaec0fab JO |
59 | |
60 | static int rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_DEFAULT; | |
61 | static int tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_DEFAULT; | |
62 | ||
e2874f2e LV |
63 | module_param(rx_descriptors, int, 0444); |
64 | module_param(tx_descriptors, int, 0444); | |
aaec0fab JO |
65 | |
66 | MODULE_PARM_DESC(rx_descriptors, "number of descriptors used " \ | |
67 | "in rx chains"); | |
68 | MODULE_PARM_DESC(tx_descriptors, "number of descriptors used " \ | |
69 | "in tx chain"); | |
70 | ||
71 | char spider_net_driver_name[] = "spidernet"; | |
72 | ||
73 | static 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 | ||
79 | MODULE_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 | */ | |
bdd01503 | 88 | static inline u32 |
aaec0fab JO |
89 | spider_net_read_reg(struct spider_net_card *card, u32 reg) |
90 | { | |
3bc0f40c BH |
91 | /* 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); | |
aaec0fab JO |
96 | } |
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 | */ | |
bdd01503 | 104 | static inline void |
aaec0fab JO |
105 | spider_net_write_reg(struct spider_net_card *card, u32 reg, u32 value) |
106 | { | |
3bc0f40c BH |
107 | /* 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); | |
aaec0fab JO |
112 | } |
113 | ||
aaec0fab JO |
114 | /** 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 | **/ | |
124 | static void | |
125 | spider_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 | **/ | |
147 | static int | |
148 | spider_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 | /** | |
11f1a52b | 169 | * spider_net_rx_irq_off - switch off rx irq on this spider card |
aaec0fab JO |
170 | * @card: device structure |
171 | * | |
11f1a52b | 172 | * switches off rx irq by masking them out in the GHIINTnMSK register |
aaec0fab JO |
173 | */ |
174 | static void | |
11f1a52b | 175 | spider_net_rx_irq_off(struct spider_net_card *card) |
aaec0fab JO |
176 | { |
177 | u32 regvalue; | |
aaec0fab | 178 | |
11f1a52b AB |
179 | regvalue = SPIDER_NET_INT0_MASK_VALUE & (~SPIDER_NET_RXINT); |
180 | spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, regvalue); | |
aaec0fab JO |
181 | } |
182 | ||
183 | /** | |
11f1a52b | 184 | * spider_net_rx_irq_on - switch on rx irq on this spider card |
aaec0fab JO |
185 | * @card: device structure |
186 | * | |
11f1a52b | 187 | * switches on rx irq by enabling them in the GHIINTnMSK register |
aaec0fab JO |
188 | */ |
189 | static void | |
11f1a52b | 190 | spider_net_rx_irq_on(struct spider_net_card *card) |
aaec0fab JO |
191 | { |
192 | u32 regvalue; | |
aaec0fab | 193 | |
11f1a52b AB |
194 | regvalue = SPIDER_NET_INT0_MASK_VALUE | SPIDER_NET_RXINT; |
195 | spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, regvalue); | |
aaec0fab JO |
196 | } |
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 | */ | |
205 | static void | |
206 | spider_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 | */ | |
237 | static int | |
238 | spider_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 | */ | |
bdd01503 | 265 | static inline int |
aaec0fab JO |
266 | spider_net_get_descr_status(struct spider_net_descr *descr) |
267 | { | |
bdd01503 | 268 | return descr->dmac_cmd_status & SPIDER_NET_DESCR_IND_PROC_MASK; |
aaec0fab JO |
269 | } |
270 | ||
271 | /** | |
272 | * spider_net_free_chain - free descriptor chain | |
273 | * @card: card structure | |
274 | * @chain: address of chain | |
275 | * | |
276 | */ | |
277 | static void | |
278 | spider_net_free_chain(struct spider_net_card *card, | |
279 | struct spider_net_descr_chain *chain) | |
280 | { | |
281 | struct spider_net_descr *descr; | |
282 | ||
d4ed8f8d LV |
283 | descr = chain->ring; |
284 | do { | |
aaec0fab | 285 | descr->bus_addr = 0; |
d4ed8f8d LV |
286 | 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); | |
aaec0fab JO |
292 | } |
293 | ||
294 | /** | |
d4ed8f8d | 295 | * spider_net_init_chain - alloc and link descriptor chain |
aaec0fab JO |
296 | * @card: card structure |
297 | * @chain: address of chain | |
aaec0fab | 298 | * |
d4ed8f8d | 299 | * We manage a circular list that mirrors the hardware structure, |
aaec0fab JO |
300 | * except that the hardware uses bus addresses. |
301 | * | |
d4ed8f8d | 302 | * Returns 0 on success, <0 on failure |
aaec0fab JO |
303 | */ |
304 | static int | |
305 | spider_net_init_chain(struct spider_net_card *card, | |
d4ed8f8d | 306 | struct spider_net_descr_chain *chain) |
aaec0fab JO |
307 | { |
308 | int i; | |
309 | struct spider_net_descr *descr; | |
11f1a52b | 310 | dma_addr_t buf; |
d4ed8f8d | 311 | size_t alloc_size; |
aaec0fab | 312 | |
d4ed8f8d | 313 | alloc_size = chain->num_desc * sizeof (struct spider_net_descr); |
aaec0fab | 314 | |
d4ed8f8d LV |
315 | chain->ring = dma_alloc_coherent(&card->pdev->dev, alloc_size, |
316 | &chain->dma_addr, GFP_KERNEL); | |
aaec0fab | 317 | |
d4ed8f8d LV |
318 | if (!chain->ring) |
319 | return -ENOMEM; | |
aaec0fab | 320 | |
d4ed8f8d LV |
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++) { | |
327 | descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE; | |
aaec0fab | 328 | |
11f1a52b | 329 | descr->bus_addr = buf; |
d4ed8f8d | 330 | descr->next_descr_addr = 0; |
aaec0fab JO |
331 | descr->next = descr + 1; |
332 | descr->prev = descr - 1; | |
333 | ||
d4ed8f8d | 334 | buf += sizeof(struct spider_net_descr); |
aaec0fab JO |
335 | } |
336 | /* do actual circular list */ | |
d4ed8f8d LV |
337 | (descr-1)->next = chain->ring; |
338 | chain->ring->prev = descr-1; | |
aaec0fab | 339 | |
bdd01503 | 340 | spin_lock_init(&chain->lock); |
d4ed8f8d LV |
341 | chain->head = chain->ring; |
342 | chain->tail = chain->ring; | |
aaec0fab | 343 | return 0; |
aaec0fab JO |
344 | } |
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 | */ | |
352 | static void | |
353 | spider_net_free_rx_chain_contents(struct spider_net_card *card) | |
354 | { | |
355 | struct spider_net_descr *descr; | |
356 | ||
357 | descr = card->rx_chain.head; | |
64751910 | 358 | do { |
aaec0fab JO |
359 | if (descr->skb) { |
360 | dev_kfree_skb(descr->skb); | |
361 | pci_unmap_single(card->pdev, descr->buf_addr, | |
11f1a52b | 362 | SPIDER_NET_MAX_FRAME, |
348bc2a6 | 363 | PCI_DMA_BIDIRECTIONAL); |
aaec0fab JO |
364 | } |
365 | descr = descr->next; | |
64751910 | 366 | } while (descr != card->rx_chain.head); |
aaec0fab JO |
367 | } |
368 | ||
369 | /** | |
a4182c50 | 370 | * spider_net_prepare_rx_descr - Reinitialize RX descriptor |
aaec0fab JO |
371 | * @card: card structure |
372 | * @descr: descriptor to re-init | |
373 | * | |
a4182c50 | 374 | * Return 0 on succes, <0 on failure. |
aaec0fab | 375 | * |
a4182c50 LV |
376 | * Allocates a new rx skb, iommu-maps it and attaches it to the |
377 | * descriptor. Mark the descriptor as activated, ready-to-use. | |
aaec0fab JO |
378 | */ |
379 | static int | |
380 | spider_net_prepare_rx_descr(struct spider_net_card *card, | |
381 | struct spider_net_descr *descr) | |
382 | { | |
8e0a613b | 383 | dma_addr_t buf; |
aaec0fab JO |
384 | int offset; |
385 | int bufsize; | |
386 | ||
387 | /* we need to round up the buffer size to a multiple of 128 */ | |
11f1a52b | 388 | bufsize = (SPIDER_NET_MAX_FRAME + SPIDER_NET_RXBUF_ALIGN - 1) & |
aaec0fab JO |
389 | (~(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) { | |
11f1a52b AB |
396 | if (netif_msg_rx_err(card) && net_ratelimit()) |
397 | pr_err("Not enough memory to allocate rx buffer\n"); | |
9b6b0b81 | 398 | card->spider_stats.alloc_rx_skb_error++; |
aaec0fab JO |
399 | 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); | |
a4182c50 | 411 | /* iommu-map the skb */ |
8e0a613b | 412 | buf = pci_map_single(card->pdev, descr->skb->data, |
bdd01503 | 413 | SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE); |
8e0a613b | 414 | descr->buf_addr = buf; |
d4b0a4c1 | 415 | if (pci_dma_mapping_error(buf)) { |
aaec0fab | 416 | dev_kfree_skb_any(descr->skb); |
11f1a52b | 417 | if (netif_msg_rx_err(card) && net_ratelimit()) |
aaec0fab | 418 | pr_err("Could not iommu-map rx buffer\n"); |
9b6b0b81 | 419 | card->spider_stats.rx_iommu_map_error++; |
bdd01503 | 420 | descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE; |
aaec0fab | 421 | } else { |
d9a9720e | 422 | descr->next_descr_addr = 0; |
90476a20 | 423 | wmb(); |
bdd01503 JO |
424 | descr->dmac_cmd_status = SPIDER_NET_DESCR_CARDOWNED | |
425 | SPIDER_NET_DMAC_NOINTR_COMPLETE; | |
d9a9720e LV |
426 | |
427 | wmb(); | |
428 | descr->prev->next_descr_addr = descr->bus_addr; | |
aaec0fab JO |
429 | } |
430 | ||
a4182c50 | 431 | return 0; |
aaec0fab JO |
432 | } |
433 | ||
434 | /** | |
11f1a52b | 435 | * spider_net_enable_rxchtails - sets RX dmac chain tail addresses |
aaec0fab JO |
436 | * @card: card structure |
437 | * | |
11f1a52b | 438 | * spider_net_enable_rxchtails sets the RX DMAC chain tail adresses in the |
aaec0fab JO |
439 | * chip by writing to the appropriate register. DMA is enabled in |
440 | * spider_net_enable_rxdmac. | |
441 | */ | |
bdd01503 | 442 | static inline void |
aaec0fab JO |
443 | spider_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 | */ | |
bdd01503 | 457 | static inline void |
aaec0fab JO |
458 | spider_net_enable_rxdmac(struct spider_net_card *card) |
459 | { | |
11f1a52b | 460 | wmb(); |
aaec0fab JO |
461 | 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 | * | |
11f1a52b | 469 | * refills descriptors in the rx chain: allocates skbs and iommu-maps them. |
aaec0fab JO |
470 | */ |
471 | static void | |
472 | spider_net_refill_rx_chain(struct spider_net_card *card) | |
473 | { | |
bdd01503 JO |
474 | struct spider_net_descr_chain *chain = &card->rx_chain; |
475 | unsigned long flags; | |
aaec0fab | 476 | |
11f1a52b AB |
477 | /* 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. */ | |
bdd01503 JO |
481 | if (!spin_trylock_irqsave(&chain->lock, flags)) |
482 | return; | |
483 | ||
484 | 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 | } | |
aaec0fab | 490 | |
bdd01503 | 491 | spin_unlock_irqrestore(&chain->lock, flags); |
aaec0fab JO |
492 | } |
493 | ||
494 | /** | |
2c307db7 | 495 | * spider_net_alloc_rx_skbs - Allocates rx skbs in rx descriptor chains |
aaec0fab JO |
496 | * @card: card structure |
497 | * | |
2c307db7 | 498 | * Returns 0 on success, <0 on failure. |
aaec0fab JO |
499 | */ |
500 | static int | |
501 | spider_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; | |
2c307db7 LV |
509 | /* 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. */ | |
aaec0fab JO |
512 | if (spider_net_prepare_rx_descr(card, chain->head)) |
513 | goto error; | |
514 | else | |
515 | chain->head = chain->head->next; | |
516 | ||
2c307db7 LV |
517 | /* This will allocate the rest of the rx buffers; |
518 | * if not, it's business as usual later on. */ | |
aaec0fab | 519 | spider_net_refill_rx_chain(card); |
11f1a52b | 520 | spider_net_enable_rxdmac(card); |
aaec0fab JO |
521 | return 0; |
522 | ||
523 | error: | |
524 | spider_net_free_rx_chain_contents(card); | |
525 | return result; | |
526 | } | |
527 | ||
aaec0fab JO |
528 | /** |
529 | * 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 | */ | |
537 | static u8 | |
538 | spider_net_get_multicast_hash(struct net_device *netdev, __u8 *addr) | |
539 | { | |
aaec0fab JO |
540 | u32 crc; |
541 | u8 hash; | |
11f1a52b AB |
542 | char addr_for_crc[ETH_ALEN] = { 0, }; |
543 | int i, bit; | |
544 | ||
545 | 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 | } | |
aaec0fab | 549 | |
11f1a52b | 550 | crc = crc32_be(~0, addr_for_crc, netdev->addr_len); |
aaec0fab JO |
551 | |
552 | hash = (crc >> 27); | |
553 | hash <<= 3; | |
554 | hash |= crc & 7; | |
11f1a52b | 555 | hash &= 0xff; |
aaec0fab JO |
556 | |
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 | */ | |
568 | static void | |
569 | spider_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 | ||
597 | write_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 | */ | |
623 | static void | |
624 | spider_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 | ||
aaec0fab JO |
630 | /** |
631 | * 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 | */ | |
641 | static int | |
642 | spider_net_prepare_tx_descr(struct spider_net_card *card, | |
aaec0fab JO |
643 | struct sk_buff *skb) |
644 | { | |
9cc7bf7e | 645 | struct spider_net_descr *descr; |
11f1a52b | 646 | dma_addr_t buf; |
9cc7bf7e | 647 | unsigned long flags; |
11f1a52b | 648 | |
9c434f5e | 649 | buf = pci_map_single(card->pdev, skb->data, skb->len, PCI_DMA_TODEVICE); |
d4b0a4c1 | 650 | if (pci_dma_mapping_error(buf)) { |
11f1a52b | 651 | if (netif_msg_tx_err(card) && net_ratelimit()) |
aaec0fab | 652 | pr_err("could not iommu-map packet (%p, %i). " |
9c434f5e | 653 | "Dropping packet\n", skb->data, skb->len); |
9b6b0b81 | 654 | card->spider_stats.tx_iommu_map_error++; |
aaec0fab JO |
655 | return -ENOMEM; |
656 | } | |
657 | ||
9cc7bf7e LV |
658 | spin_lock_irqsave(&card->tx_chain.lock, flags); |
659 | descr = card->tx_chain.head; | |
660 | card->tx_chain.head = descr->next; | |
661 | ||
8e0a613b | 662 | descr->buf_addr = buf; |
9c434f5e | 663 | descr->buf_size = skb->len; |
bdd01503 | 664 | descr->next_descr_addr = 0; |
aaec0fab JO |
665 | descr->skb = skb; |
666 | descr->data_status = 0; | |
667 | ||
bdd01503 JO |
668 | descr->dmac_cmd_status = |
669 | SPIDER_NET_DESCR_CARDOWNED | SPIDER_NET_DMAC_NOCS; | |
9cc7bf7e LV |
670 | spin_unlock_irqrestore(&card->tx_chain.lock, flags); |
671 | ||
bdd01503 JO |
672 | 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 | ||
204e5fa1 | 682 | /* Chain the bus address, so that the DMA engine finds this descr. */ |
bdd01503 JO |
683 | descr->prev->next_descr_addr = descr->bus_addr; |
684 | ||
917a5b8e | 685 | card->netdev->trans_start = jiffies; /* set netdev watchdog timer */ |
bdd01503 JO |
686 | return 0; |
687 | } | |
688 | ||
a664ccf4 | 689 | static int |
204e5fa1 LV |
690 | spider_net_set_low_watermark(struct spider_net_card *card) |
691 | { | |
9cc7bf7e | 692 | unsigned long flags; |
204e5fa1 LV |
693 | int status; |
694 | int cnt=0; | |
695 | int i; | |
696 | struct spider_net_descr *descr = card->tx_chain.tail; | |
697 | ||
9cc7bf7e LV |
698 | /* Measure the length of the queue. Measurement does not |
699 | * need to be precise -- does not need a lock. */ | |
204e5fa1 LV |
700 | 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 */ | |
d4ed8f8d | 709 | if (cnt < card->tx_chain.num_desc/4) |
a664ccf4 | 710 | return cnt; |
204e5fa1 LV |
711 | |
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 */ | |
9cc7bf7e | 719 | spin_lock_irqsave(&card->tx_chain.lock, flags); |
204e5fa1 LV |
720 | 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; | |
9cc7bf7e | 725 | spin_unlock_irqrestore(&card->tx_chain.lock, flags); |
a664ccf4 | 726 | return cnt; |
204e5fa1 LV |
727 | } |
728 | ||
bdd01503 JO |
729 | /** |
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 | */ | |
741 | static int | |
742 | spider_net_release_tx_chain(struct spider_net_card *card, int brutal) | |
743 | { | |
744 | struct spider_net_descr_chain *chain = &card->tx_chain; | |
9cc7bf7e LV |
745 | struct spider_net_descr *descr; |
746 | struct sk_buff *skb; | |
747 | u32 buf_addr; | |
748 | unsigned long flags; | |
bdd01503 JO |
749 | int status; |
750 | ||
bdd01503 | 751 | while (chain->tail != chain->head) { |
9cc7bf7e LV |
752 | spin_lock_irqsave(&chain->lock, flags); |
753 | descr = chain->tail; | |
754 | ||
755 | status = spider_net_get_descr_status(descr); | |
bdd01503 JO |
756 | switch (status) { |
757 | case SPIDER_NET_DESCR_COMPLETE: | |
758 | card->netdev_stats.tx_packets++; | |
9cc7bf7e | 759 | card->netdev_stats.tx_bytes += descr->skb->len; |
bdd01503 JO |
760 | break; |
761 | ||
762 | case SPIDER_NET_DESCR_CARDOWNED: | |
9cc7bf7e LV |
763 | if (!brutal) { |
764 | spin_unlock_irqrestore(&chain->lock, flags); | |
bdd01503 | 765 | return 1; |
9cc7bf7e LV |
766 | } |
767 | ||
bdd01503 JO |
768 | /* 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++; | |
9cc7bf7e LV |
784 | if (!brutal) { |
785 | spin_unlock_irqrestore(&chain->lock, flags); | |
c3fee4c5 | 786 | return 1; |
9cc7bf7e | 787 | } |
bdd01503 | 788 | } |
aaec0fab | 789 | |
9cc7bf7e LV |
790 | 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) { | |
9c434f5e JL |
798 | pci_unmap_single(card->pdev, buf_addr, skb->len, |
799 | PCI_DMA_TODEVICE); | |
9cc7bf7e LV |
800 | dev_kfree_skb(skb); |
801 | } | |
802 | } | |
aaec0fab JO |
803 | 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 | * | |
a664ccf4 LV |
811 | * 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. | |
aaec0fab | 817 | */ |
bdd01503 JO |
818 | static inline void |
819 | spider_net_kick_tx_dma(struct spider_net_card *card) | |
aaec0fab | 820 | { |
bdd01503 | 821 | struct spider_net_descr *descr; |
aaec0fab | 822 | |
bdd01503 JO |
823 | if (spider_net_read_reg(card, SPIDER_NET_GDTDMACCNTR) & |
824 | SPIDER_NET_TX_DMA_EN) | |
825 | goto out; | |
aaec0fab | 826 | |
bdd01503 JO |
827 | 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 | ||
842 | out: | |
843 | mod_timer(&card->tx_timer, jiffies + SPIDER_NET_TX_TIMER); | |
aaec0fab JO |
844 | } |
845 | ||
846 | /** | |
847 | * spider_net_xmit - transmits a frame over the device | |
848 | * @skb: packet to send out | |
849 | * @netdev: interface device structure | |
850 | * | |
bdd01503 | 851 | * returns 0 on success, !0 on failure |
aaec0fab JO |
852 | */ |
853 | static int | |
854 | spider_net_xmit(struct sk_buff *skb, struct net_device *netdev) | |
855 | { | |
a664ccf4 | 856 | int cnt; |
aaec0fab | 857 | struct spider_net_card *card = netdev_priv(netdev); |
bdd01503 | 858 | struct spider_net_descr_chain *chain = &card->tx_chain; |
bdd01503 | 859 | |
11f1a52b | 860 | spider_net_release_tx_chain(card, 0); |
aaec0fab | 861 | |
313ef4b7 | 862 | if ((chain->head->next == chain->tail->prev) || |
313ef4b7 | 863 | (spider_net_prepare_tx_descr(card, skb) != 0)) { |
bdd01503 | 864 | |
9b6b0b81 | 865 | card->netdev_stats.tx_dropped++; |
313ef4b7 LV |
866 | netif_stop_queue(netdev); |
867 | return NETDEV_TX_BUSY; | |
bdd01503 | 868 | } |
aaec0fab | 869 | |
a664ccf4 LV |
870 | cnt = spider_net_set_low_watermark(card); |
871 | if (cnt < 5) | |
872 | spider_net_kick_tx_dma(card); | |
313ef4b7 | 873 | return NETDEV_TX_OK; |
bdd01503 | 874 | } |
11f1a52b | 875 | |
bdd01503 JO |
876 | /** |
877 | * spider_net_cleanup_tx_ring - cleans up the TX ring | |
878 | * @card: card structure | |
879 | * | |
68a8c609 LV |
880 | * 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. | |
bdd01503 JO |
884 | */ |
885 | static void | |
886 | spider_net_cleanup_tx_ring(struct spider_net_card *card) | |
887 | { | |
bdd01503 | 888 | if ((spider_net_release_tx_chain(card, 0) != 0) && |
313ef4b7 | 889 | (card->netdev->flags & IFF_UP)) { |
bdd01503 | 890 | spider_net_kick_tx_dma(card); |
313ef4b7 LV |
891 | netif_wake_queue(card->netdev); |
892 | } | |
aaec0fab JO |
893 | } |
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 | */ | |
904 | static int | |
905 | spider_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 | * | |
05b346b5 LV |
918 | * Fills out skb structure and passes the data to the stack. |
919 | * The descriptor state is not changed. | |
aaec0fab | 920 | */ |
7f7223b8 | 921 | static void |
aaec0fab | 922 | spider_net_pass_skb_up(struct spider_net_descr *descr, |
1cd173f6 | 923 | struct spider_net_card *card) |
aaec0fab JO |
924 | { |
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; | |
aaec0fab JO |
931 | netdev = card->netdev; |
932 | ||
11f1a52b | 933 | skb = descr->skb; |
aaec0fab JO |
934 | 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) { | |
11f1a52b AB |
945 | 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)) | |
aaec0fab JO |
948 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
949 | else | |
950 | skb->ip_summed = CHECKSUM_NONE; | |
11f1a52b | 951 | } else |
aaec0fab | 952 | skb->ip_summed = CHECKSUM_NONE; |
aaec0fab JO |
953 | |
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 */ | |
1cd173f6 | 961 | netif_receive_skb(skb); |
aaec0fab JO |
962 | |
963 | /* update netdevice statistics */ | |
964 | card->netdev_stats.rx_packets++; | |
965 | card->netdev_stats.rx_bytes += skb->len; | |
aaec0fab JO |
966 | } |
967 | ||
6d24998f LV |
968 | #ifdef DEBUG |
969 | static 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 | ||
aaec0fab | 996 | /** |
11f1a52b | 997 | * spider_net_decode_one_descr - processes an rx descriptor |
aaec0fab JO |
998 | * @card: card structure |
999 | * | |
1cd173f6 | 1000 | * Returns 1 if a packet has been sent to the stack, otherwise 0 |
aaec0fab | 1001 | * |
1cd173f6 | 1002 | * Processes an rx descriptor by iommu-unmapping the data buffer and passing |
11f1a52b AB |
1003 | * 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 | |
aaec0fab JO |
1005 | */ |
1006 | static int | |
1cd173f6 | 1007 | spider_net_decode_one_descr(struct spider_net_card *card) |
aaec0fab | 1008 | { |
bdd01503 JO |
1009 | struct spider_net_descr_chain *chain = &card->rx_chain; |
1010 | struct spider_net_descr *descr = chain->tail; | |
1011 | int status; | |
aaec0fab | 1012 | |
aaec0fab JO |
1013 | status = spider_net_get_descr_status(descr); |
1014 | ||
80dab7c7 LV |
1015 | /* Nothing in the descriptor, or ring must be empty */ |
1016 | if ((status == SPIDER_NET_DESCR_CARDOWNED) || | |
1017 | (status == SPIDER_NET_DESCR_NOT_IN_USE)) | |
1cd173f6 | 1018 | return 0; |
aaec0fab | 1019 | |
11f1a52b | 1020 | /* descriptor definitively used -- move on tail */ |
aaec0fab JO |
1021 | chain->tail = descr->next; |
1022 | ||
05b346b5 LV |
1023 | /* unmap descriptor */ |
1024 | pci_unmap_single(card->pdev, descr->buf_addr, | |
1025 | SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE); | |
1026 | ||
aaec0fab JO |
1027 | 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++; | |
7f7223b8 | 1034 | goto bad_desc; |
aaec0fab JO |
1035 | } |
1036 | ||
1037 | if ( (status != SPIDER_NET_DESCR_COMPLETE) && | |
1038 | (status != SPIDER_NET_DESCR_FRAME_END) ) { | |
5a028877 | 1039 | if (netif_msg_rx_err(card)) |
05b346b5 | 1040 | pr_err("%s: RX descriptor with unkown state %d\n", |
aaec0fab | 1041 | card->netdev->name, status); |
5a028877 | 1042 | card->spider_stats.rx_desc_unk_state++; |
7f7223b8 | 1043 | goto bad_desc; |
aaec0fab JO |
1044 | } |
1045 | ||
366684bd LV |
1046 | /* 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); | |
7f7223b8 | 1053 | goto bad_desc; |
366684bd LV |
1054 | } |
1055 | ||
6d24998f LV |
1056 | 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 | ||
7f7223b8 LV |
1074 | /* Ok, we've got a packet in descr */ |
1075 | spider_net_pass_skb_up(descr, card); | |
1cd173f6 | 1076 | descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE; |
7f7223b8 LV |
1077 | return 1; |
1078 | ||
1079 | bad_desc: | |
1080 | dev_kfree_skb_irq(descr->skb); | |
1081 | descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE; | |
1082 | return 0; | |
aaec0fab JO |
1083 | } |
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 | */ | |
1097 | static int | |
1098 | spider_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 | ||
68a8c609 | 1104 | spider_net_cleanup_tx_ring(card); |
aaec0fab JO |
1105 | packets_to_do = min(*budget, netdev->quota); |
1106 | ||
1107 | while (packets_to_do) { | |
1cd173f6 | 1108 | if (spider_net_decode_one_descr(card)) { |
aaec0fab JO |
1109 | 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; | |
11f1a52b | 1120 | spider_net_refill_rx_chain(card); |
80dab7c7 | 1121 | spider_net_enable_rxdmac(card); |
aaec0fab JO |
1122 | |
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 | */ | |
1139 | static void | |
1140 | spider_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 | */ | |
1151 | static void | |
1152 | spider_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 | */ | |
1164 | static void | |
1165 | spider_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 | */ | |
1177 | static struct net_device_stats * | |
1178 | spider_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 | */ | |
1192 | static int | |
1193 | spider_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 | */ | |
1212 | static int | |
1213 | spider_net_set_mac(struct net_device *netdev, void *p) | |
1214 | { | |
1215 | struct spider_net_card *card = netdev_priv(netdev); | |
054034db | 1216 | u32 macl, macu, regvalue; |
aaec0fab JO |
1217 | struct sockaddr *addr = p; |
1218 | ||
aaec0fab JO |
1219 | if (!is_valid_ether_addr(addr->sa_data)) |
1220 | return -EADDRNOTAVAIL; | |
1221 | ||
054034db JO |
1222 | /* 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 */ | |
aaec0fab JO |
1228 | 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 | ||
054034db JO |
1234 | /* 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 | ||
aaec0fab JO |
1239 | 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 | ||
aaec0fab JO |
1250 | /** |
1251 | * 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 | */ | |
1258 | static void | |
1259 | spider_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: | |
aaec0fab JO |
1278 | case SPIDER_NET_GFIFOINT: |
1279 | case SPIDER_NET_DMACINT: | |
1280 | case SPIDER_NET_GSYSINT: | |
1281 | break; */ | |
1282 | ||
98b9040c LV |
1283 | case SPIDER_NET_GIPSINT: |
1284 | show_error = 0; | |
1285 | break; | |
1286 | ||
aaec0fab JO |
1287 | 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 | |
98b9040c | 1345 | if (card->tx_chain.tail != card->tx_chain.head) |
aaec0fab | 1346 | spider_net_kick_tx_dma(card); |
98b9040c LV |
1347 | */ |
1348 | show_error = 0; | |
aaec0fab JO |
1349 | 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: | |
11f1a52b | 1362 | if (netif_msg_intr(card) && net_ratelimit()) |
aaec0fab JO |
1363 | pr_err("Spider TX RAM full\n"); |
1364 | show_error = 0; | |
1365 | break; | |
11f1a52b AB |
1366 | case SPIDER_NET_GRFDFLLINT: /* fallthrough */ |
1367 | case SPIDER_NET_GRFCFLLINT: /* fallthrough */ | |
1368 | case SPIDER_NET_GRFBFLLINT: /* fallthrough */ | |
1369 | case SPIDER_NET_GRFAFLLINT: /* fallthrough */ | |
aaec0fab | 1370 | case SPIDER_NET_GRMFLLINT: |
11f1a52b | 1371 | if (netif_msg_intr(card) && net_ratelimit()) |
5a028877 | 1372 | pr_err("Spider RX RAM full, incoming packets " |
11f1a52b AB |
1373 | "might be discarded!\n"); |
1374 | spider_net_rx_irq_off(card); | |
75856175 | 1375 | netif_rx_schedule(card->netdev); |
11f1a52b | 1376 | show_error = 0; |
aaec0fab JO |
1377 | 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; | |
aaec0fab JO |
1384 | |
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: | |
5a028877 | 1390 | if (netif_msg_intr(card) && net_ratelimit()) |
aaec0fab JO |
1391 | pr_err("got descriptor chain end interrupt, " |
1392 | "restarting DMAC %c.\n", | |
37aad750 | 1393 | 'D'-(i-SPIDER_NET_GDDDCEINT)/3); |
aaec0fab | 1394 | spider_net_refill_rx_chain(card); |
11f1a52b | 1395 | spider_net_enable_rxdmac(card); |
aaec0fab JO |
1396 | 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); | |
11f1a52b | 1406 | spider_net_enable_rxdmac(card); |
aaec0fab JO |
1407 | 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 | ||
5a028877 | 1461 | if ((show_error) && (netif_msg_intr(card)) && net_ratelimit()) |
98b9040c | 1462 | pr_err("Got error interrupt on %s, GHIINT0STS = 0x%08x, " |
aaec0fab | 1463 | "GHIINT1STS = 0x%08x, GHIINT2STS = 0x%08x\n", |
98b9040c | 1464 | card->netdev->name, |
aaec0fab JO |
1465 | 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 | */ | |
1484 | static irqreturn_t | |
7d12e780 | 1485 | spider_net_interrupt(int irq, void *ptr) |
aaec0fab JO |
1486 | { |
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 | ||
aaec0fab JO |
1496 | if (status_reg & SPIDER_NET_RXINT ) { |
1497 | spider_net_rx_irq_off(card); | |
1498 | netif_rx_schedule(netdev); | |
1499 | } | |
68a8c609 LV |
1500 | if (status_reg & SPIDER_NET_TXINT) |
1501 | netif_rx_schedule(netdev); | |
aaec0fab | 1502 | |
11f1a52b AB |
1503 | if (status_reg & SPIDER_NET_ERRINT ) |
1504 | spider_net_handle_error_irq(card, status_reg); | |
aaec0fab JO |
1505 | |
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 | */ | |
1519 | static void | |
1520 | spider_net_poll_controller(struct net_device *netdev) | |
1521 | { | |
1522 | disable_irq(netdev->irq); | |
7d12e780 | 1523 | spider_net_interrupt(netdev->irq, netdev); |
aaec0fab JO |
1524 | 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 | */ | |
1535 | static void | |
1536 | spider_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 | */ | |
1551 | static void | |
1552 | spider_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 }, | |
b636d17a JO |
1585 | { SPIDER_NET_GTESTMD, 0x10000000 }, |
1586 | { SPIDER_NET_GTTQMSK, 0x00400040 }, | |
aaec0fab JO |
1587 | |
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 | ||
aaec0fab JO |
1625 | 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); | |
bdd01503 JO |
1639 | |
1640 | spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR, | |
7bd54c86 | 1641 | SPIDER_NET_GDTBSTA); |
aaec0fab JO |
1642 | } |
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 | */ | |
1653 | int | |
1654 | spider_net_open(struct net_device *netdev) | |
1655 | { | |
1656 | struct spider_net_card *card = netdev_priv(netdev); | |
d4ed8f8d | 1657 | int result; |
aaec0fab | 1658 | |
d4ed8f8d LV |
1659 | result = spider_net_init_chain(card, &card->tx_chain); |
1660 | if (result) | |
aaec0fab | 1661 | goto alloc_tx_failed; |
204e5fa1 LV |
1662 | card->low_watermark = NULL; |
1663 | ||
d4ed8f8d LV |
1664 | result = spider_net_init_chain(card, &card->rx_chain); |
1665 | if (result) | |
aaec0fab JO |
1666 | goto alloc_rx_failed; |
1667 | ||
d4ed8f8d | 1668 | /* Allocate rx skbs */ |
aaec0fab JO |
1669 | 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, | |
1fb9df5d | 1678 | IRQF_SHARED, netdev->name, netdev)) |
aaec0fab JO |
1679 | goto register_int_failed; |
1680 | ||
1681 | spider_net_enable_card(card); | |
1682 | ||
543cec51 JO |
1683 | netif_start_queue(netdev); |
1684 | netif_carrier_on(netdev); | |
1685 | netif_poll_enable(netdev); | |
1686 | ||
aaec0fab JO |
1687 | return 0; |
1688 | ||
1689 | register_int_failed: | |
1690 | spider_net_free_rx_chain_contents(card); | |
1691 | alloc_skbs_failed: | |
1692 | spider_net_free_chain(card, &card->rx_chain); | |
1693 | alloc_rx_failed: | |
1694 | spider_net_free_chain(card, &card->tx_chain); | |
1695 | alloc_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 | **/ | |
1708 | static int | |
1709 | spider_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 | ||
8ec93459 | 1727 | phy->def->ops->enable_fiber(phy); |
53abbf7e | 1728 | |
aaec0fab JO |
1729 | 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 | |
11f1a52b | 1739 | * @firmware_ptr: pointer to firmware data |
aaec0fab | 1740 | * |
11f1a52b AB |
1741 | * spider_net_download_firmware loads the firmware data into the |
1742 | * adapter. It assumes the length etc. to be allright. | |
aaec0fab | 1743 | */ |
0d3ea166 | 1744 | static int |
aaec0fab | 1745 | spider_net_download_firmware(struct spider_net_card *card, |
1a2509c9 | 1746 | const void *firmware_ptr) |
aaec0fab JO |
1747 | { |
1748 | int sequencer, i; | |
1a2509c9 | 1749 | const u32 *fw_ptr = firmware_ptr; |
aaec0fab JO |
1750 | |
1751 | /* stop sequencers */ | |
1752 | spider_net_write_reg(card, SPIDER_NET_GSINIT, | |
1753 | SPIDER_NET_STOP_SEQ_VALUE); | |
1754 | ||
11f1a52b AB |
1755 | for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS; |
1756 | sequencer++) { | |
aaec0fab JO |
1757 | spider_net_write_reg(card, |
1758 | SPIDER_NET_GSnPRGADR + sequencer * 8, 0); | |
11f1a52b | 1759 | for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) { |
aaec0fab JO |
1760 | spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT + |
1761 | sequencer * 8, *fw_ptr); | |
1762 | fw_ptr++; | |
1763 | } | |
1764 | } | |
1765 | ||
0d3ea166 AB |
1766 | if (spider_net_read_reg(card, SPIDER_NET_GSINIT)) |
1767 | return -EIO; | |
1768 | ||
aaec0fab JO |
1769 | spider_net_write_reg(card, SPIDER_NET_GSINIT, |
1770 | SPIDER_NET_RUN_SEQ_VALUE); | |
0d3ea166 AB |
1771 | |
1772 | return 0; | |
aaec0fab JO |
1773 | } |
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 | */ | |
1801 | static int | |
1802 | spider_net_init_firmware(struct spider_net_card *card) | |
1803 | { | |
11f1a52b | 1804 | struct firmware *firmware = NULL; |
030d6753 | 1805 | struct device_node *dn; |
1a2509c9 | 1806 | const u8 *fw_prop = NULL; |
11f1a52b AB |
1807 | int err = -ENOENT; |
1808 | int fw_size; | |
aaec0fab | 1809 | |
030d6753 | 1810 | if (request_firmware((const struct firmware **)&firmware, |
11f1a52b AB |
1811 | 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); | |
030d6753 | 1819 | |
11f1a52b AB |
1820 | release_firmware(firmware); |
1821 | if (err) | |
1822 | goto try_host_fw; | |
030d6753 | 1823 | |
11f1a52b | 1824 | goto done; |
aaec0fab JO |
1825 | } |
1826 | ||
11f1a52b AB |
1827 | try_host_fw: |
1828 | dn = pci_device_to_OF_node(card->pdev); | |
1829 | if (!dn) | |
1830 | goto out_err; | |
1831 | ||
1a2509c9 | 1832 | fw_prop = get_property(dn, "firmware", &fw_size); |
11f1a52b AB |
1833 | 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; | |
aaec0fab JO |
1841 | } |
1842 | ||
11f1a52b | 1843 | err = spider_net_download_firmware(card, fw_prop); |
aaec0fab | 1844 | |
11f1a52b AB |
1845 | done: |
1846 | return err; | |
1847 | out_err: | |
1848 | if (netif_msg_probe(card)) | |
1849 | pr_err("Couldn't find spidernet firmware in filesystem " \ | |
1850 | "or host firmware\n"); | |
aaec0fab JO |
1851 | return err; |
1852 | } | |
1853 | ||
1854 | /** | |
1855 | * spider_net_workaround_rxramfull - work around firmware bug | |
1856 | * @card: card structure | |
1857 | * | |
1858 | * no return value | |
1859 | **/ | |
1860 | static void | |
1861 | spider_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 */ | |
11f1a52b AB |
1870 | for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS; |
1871 | sequencer++) { | |
ee962a5c | 1872 | spider_net_write_reg(card, SPIDER_NET_GSnPRGADR + |
aaec0fab | 1873 | sequencer * 8, 0x0); |
11f1a52b | 1874 | for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) { |
aaec0fab JO |
1875 | 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 | ||
bdd01503 JO |
1888 | /** |
1889 | * spider_net_stop - called upon ifconfig down | |
1890 | * @netdev: interface device structure | |
1891 | * | |
1892 | * always returns 0 | |
1893 | */ | |
1894 | int | |
1895 | spider_net_stop(struct net_device *netdev) | |
1896 | { | |
1897 | struct spider_net_card *card = netdev_priv(netdev); | |
1898 | ||
bdd01503 JO |
1899 | 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);*/ | |
97210b5f | 1910 | free_irq(to_pci_dev(netdev->dev.parent)->irq, netdev); |
bdd01503 JO |
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 */ | |
9cc7bf7e | 1919 | spider_net_release_tx_chain(card, 1); |
d4ed8f8d | 1920 | spider_net_free_rx_chain_contents(card); |
bdd01503 | 1921 | |
a55eb05a JO |
1922 | spider_net_free_rx_chain_contents(card); |
1923 | ||
bdd01503 JO |
1924 | spider_net_free_chain(card, &card->tx_chain); |
1925 | spider_net_free_chain(card, &card->rx_chain); | |
1926 | ||
1927 | return 0; | |
1928 | } | |
1929 | ||
aaec0fab JO |
1930 | /** |
1931 | * 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 | */ | |
1937 | static void | |
c4028958 | 1938 | spider_net_tx_timeout_task(struct work_struct *work) |
aaec0fab | 1939 | { |
c4028958 DH |
1940 | struct spider_net_card *card = |
1941 | container_of(work, struct spider_net_card, tx_timeout_task); | |
1942 | struct net_device *netdev = card->netdev; | |
aaec0fab JO |
1943 | |
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); | |
bdd01503 | 1959 | spider_net_kick_tx_dma(card); |
aaec0fab JO |
1960 | netif_device_attach(netdev); |
1961 | ||
1962 | out: | |
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 | */ | |
1972 | static void | |
1973 | spider_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); | |
9b6b0b81 | 1983 | card->spider_stats.tx_timeouts++; |
aaec0fab JO |
1984 | } |
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 | */ | |
1992 | static void | |
1993 | spider_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 | **/ | |
2029 | static int | |
2030 | spider_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; | |
1a2509c9 | 2036 | const u8 *mac; |
aaec0fab JO |
2037 | |
2038 | SET_MODULE_OWNER(netdev); | |
2039 | SET_NETDEV_DEV(netdev, &card->pdev->dev); | |
2040 | ||
2041 | pci_set_drvdata(card->pdev, netdev); | |
11f1a52b | 2042 | |
11f1a52b AB |
2043 | 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; | |
aaec0fab JO |
2047 | netdev->irq = card->pdev->irq; |
2048 | ||
2049 | card->options.rx_csum = SPIDER_NET_RX_CSUM_DEFAULT; | |
2050 | ||
d4ed8f8d LV |
2051 | card->tx_chain.num_desc = tx_descriptors; |
2052 | card->rx_chain.num_desc = rx_descriptors; | |
b68a60e5 | 2053 | |
aaec0fab JO |
2054 | spider_net_setup_netdev_ops(netdev); |
2055 | ||
bdd01503 | 2056 | netdev->features = NETIF_F_HW_CSUM | NETIF_F_LLTX; |
aaec0fab JO |
2057 | /* 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); | |
543cec51 JO |
2063 | if (!dn) |
2064 | return -EIO; | |
2065 | ||
1a2509c9 | 2066 | mac = get_property(dn, "local-mac-address", NULL); |
543cec51 JO |
2067 | if (!mac) |
2068 | return -EIO; | |
aaec0fab JO |
2069 | 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 | */ | |
2096 | static struct spider_net_card * | |
2097 | spider_net_alloc_card(void) | |
2098 | { | |
2099 | struct net_device *netdev; | |
2100 | struct spider_net_card *card; | |
aaec0fab | 2101 | |
d4ed8f8d | 2102 | netdev = alloc_etherdev(sizeof(struct spider_net_card)); |
aaec0fab JO |
2103 | if (!netdev) |
2104 | return NULL; | |
2105 | ||
2106 | card = netdev_priv(netdev); | |
2107 | card->netdev = netdev; | |
2108 | card->msg_enable = SPIDER_NET_DEFAULT_MSG; | |
c4028958 | 2109 | INIT_WORK(&card->tx_timeout_task, spider_net_tx_timeout_task); |
aaec0fab JO |
2110 | 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 | */ | |
2122 | static void | |
2123 | spider_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 | **/ | |
2142 | static struct spider_net_card * | |
2143 | spider_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 | ||
2188 | out_release_regions: | |
2189 | pci_release_regions(pdev); | |
2190 | out_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 | **/ | |
2206 | static int __devinit | |
2207 | spider_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 | ||
2233 | out_undo_pci: | |
2234 | spider_net_undo_pci_setup(card); | |
2235 | free_netdev(card->netdev); | |
2236 | out: | |
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 | **/ | |
2249 | static void __devexit | |
2250 | spider_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); | |
543cec51 JO |
2262 | |
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 | ||
aaec0fab JO |
2269 | spider_net_undo_pci_setup(card); |
2270 | free_netdev(netdev); | |
aaec0fab JO |
2271 | } |
2272 | ||
2273 | static struct pci_driver spider_net_driver = { | |
aaec0fab JO |
2274 | .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 | */ | |
2285 | static int __init spider_net_init(void) | |
2286 | { | |
90f10841 LV |
2287 | printk(KERN_INFO "Spidernet version %s.\n", VERSION); |
2288 | ||
aaec0fab JO |
2289 | 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 | */ | |
2314 | static void __exit spider_net_cleanup(void) | |
2315 | { | |
2316 | pci_unregister_driver(&spider_net_driver); | |
2317 | } | |
2318 | ||
2319 | module_init(spider_net_init); | |
2320 | module_exit(spider_net_cleanup); |