]>
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 { |
bdd01503 JO |
422 | descr->dmac_cmd_status = SPIDER_NET_DESCR_CARDOWNED | |
423 | SPIDER_NET_DMAC_NOINTR_COMPLETE; | |
aaec0fab JO |
424 | } |
425 | ||
a4182c50 | 426 | return 0; |
aaec0fab JO |
427 | } |
428 | ||
429 | /** | |
11f1a52b | 430 | * spider_net_enable_rxchtails - sets RX dmac chain tail addresses |
aaec0fab JO |
431 | * @card: card structure |
432 | * | |
11f1a52b | 433 | * spider_net_enable_rxchtails sets the RX DMAC chain tail adresses in the |
aaec0fab JO |
434 | * chip by writing to the appropriate register. DMA is enabled in |
435 | * spider_net_enable_rxdmac. | |
436 | */ | |
bdd01503 | 437 | static inline void |
aaec0fab JO |
438 | spider_net_enable_rxchtails(struct spider_net_card *card) |
439 | { | |
440 | /* assume chain is aligned correctly */ | |
441 | spider_net_write_reg(card, SPIDER_NET_GDADCHA , | |
442 | card->rx_chain.tail->bus_addr); | |
443 | } | |
444 | ||
445 | /** | |
446 | * spider_net_enable_rxdmac - enables a receive DMA controller | |
447 | * @card: card structure | |
448 | * | |
449 | * spider_net_enable_rxdmac enables the DMA controller by setting RX_DMA_EN | |
450 | * in the GDADMACCNTR register | |
451 | */ | |
bdd01503 | 452 | static inline void |
aaec0fab JO |
453 | spider_net_enable_rxdmac(struct spider_net_card *card) |
454 | { | |
11f1a52b | 455 | wmb(); |
aaec0fab JO |
456 | spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR, |
457 | SPIDER_NET_DMA_RX_VALUE); | |
458 | } | |
459 | ||
460 | /** | |
461 | * spider_net_refill_rx_chain - refills descriptors/skbs in the rx chains | |
462 | * @card: card structure | |
463 | * | |
11f1a52b | 464 | * refills descriptors in the rx chain: allocates skbs and iommu-maps them. |
aaec0fab JO |
465 | */ |
466 | static void | |
467 | spider_net_refill_rx_chain(struct spider_net_card *card) | |
468 | { | |
bdd01503 JO |
469 | struct spider_net_descr_chain *chain = &card->rx_chain; |
470 | unsigned long flags; | |
aaec0fab | 471 | |
11f1a52b AB |
472 | /* one context doing the refill (and a second context seeing that |
473 | * and omitting it) is ok. If called by NAPI, we'll be called again | |
474 | * as spider_net_decode_one_descr is called several times. If some | |
475 | * interrupt calls us, the NAPI is about to clean up anyway. */ | |
bdd01503 JO |
476 | if (!spin_trylock_irqsave(&chain->lock, flags)) |
477 | return; | |
478 | ||
479 | while (spider_net_get_descr_status(chain->head) == | |
480 | SPIDER_NET_DESCR_NOT_IN_USE) { | |
481 | if (spider_net_prepare_rx_descr(card, chain->head)) | |
482 | break; | |
483 | chain->head = chain->head->next; | |
484 | } | |
aaec0fab | 485 | |
bdd01503 | 486 | spin_unlock_irqrestore(&chain->lock, flags); |
aaec0fab JO |
487 | } |
488 | ||
489 | /** | |
490 | * spider_net_alloc_rx_skbs - allocates rx skbs in rx descriptor chains | |
491 | * @card: card structure | |
492 | * | |
493 | * returns 0 on success, <0 on failure | |
494 | */ | |
495 | static int | |
496 | spider_net_alloc_rx_skbs(struct spider_net_card *card) | |
497 | { | |
498 | int result; | |
499 | struct spider_net_descr_chain *chain; | |
500 | ||
501 | result = -ENOMEM; | |
502 | ||
503 | chain = &card->rx_chain; | |
504 | /* put at least one buffer into the chain. if this fails, | |
505 | * we've got a problem. if not, spider_net_refill_rx_chain | |
506 | * will do the rest at the end of this function */ | |
507 | if (spider_net_prepare_rx_descr(card, chain->head)) | |
508 | goto error; | |
509 | else | |
510 | chain->head = chain->head->next; | |
511 | ||
512 | /* this will allocate the rest of the rx buffers; if not, it's | |
513 | * business as usual later on */ | |
514 | spider_net_refill_rx_chain(card); | |
11f1a52b | 515 | spider_net_enable_rxdmac(card); |
aaec0fab JO |
516 | return 0; |
517 | ||
518 | error: | |
519 | spider_net_free_rx_chain_contents(card); | |
520 | return result; | |
521 | } | |
522 | ||
aaec0fab JO |
523 | /** |
524 | * spider_net_get_multicast_hash - generates hash for multicast filter table | |
525 | * @addr: multicast address | |
526 | * | |
527 | * returns the hash value. | |
528 | * | |
529 | * spider_net_get_multicast_hash calculates a hash value for a given multicast | |
530 | * address, that is used to set the multicast filter tables | |
531 | */ | |
532 | static u8 | |
533 | spider_net_get_multicast_hash(struct net_device *netdev, __u8 *addr) | |
534 | { | |
aaec0fab JO |
535 | u32 crc; |
536 | u8 hash; | |
11f1a52b AB |
537 | char addr_for_crc[ETH_ALEN] = { 0, }; |
538 | int i, bit; | |
539 | ||
540 | for (i = 0; i < ETH_ALEN * 8; i++) { | |
541 | bit = (addr[i / 8] >> (i % 8)) & 1; | |
542 | addr_for_crc[ETH_ALEN - 1 - i / 8] += bit << (7 - (i % 8)); | |
543 | } | |
aaec0fab | 544 | |
11f1a52b | 545 | crc = crc32_be(~0, addr_for_crc, netdev->addr_len); |
aaec0fab JO |
546 | |
547 | hash = (crc >> 27); | |
548 | hash <<= 3; | |
549 | hash |= crc & 7; | |
11f1a52b | 550 | hash &= 0xff; |
aaec0fab JO |
551 | |
552 | return hash; | |
553 | } | |
554 | ||
555 | /** | |
556 | * spider_net_set_multi - sets multicast addresses and promisc flags | |
557 | * @netdev: interface device structure | |
558 | * | |
559 | * spider_net_set_multi configures multicast addresses as needed for the | |
560 | * netdev interface. It also sets up multicast, allmulti and promisc | |
561 | * flags appropriately | |
562 | */ | |
563 | static void | |
564 | spider_net_set_multi(struct net_device *netdev) | |
565 | { | |
566 | struct dev_mc_list *mc; | |
567 | u8 hash; | |
568 | int i; | |
569 | u32 reg; | |
570 | struct spider_net_card *card = netdev_priv(netdev); | |
571 | unsigned long bitmask[SPIDER_NET_MULTICAST_HASHES / BITS_PER_LONG] = | |
572 | {0, }; | |
573 | ||
574 | spider_net_set_promisc(card); | |
575 | ||
576 | if (netdev->flags & IFF_ALLMULTI) { | |
577 | for (i = 0; i < SPIDER_NET_MULTICAST_HASHES; i++) { | |
578 | set_bit(i, bitmask); | |
579 | } | |
580 | goto write_hash; | |
581 | } | |
582 | ||
583 | /* well, we know, what the broadcast hash value is: it's xfd | |
584 | hash = spider_net_get_multicast_hash(netdev, netdev->broadcast); */ | |
585 | set_bit(0xfd, bitmask); | |
586 | ||
587 | for (mc = netdev->mc_list; mc; mc = mc->next) { | |
588 | hash = spider_net_get_multicast_hash(netdev, mc->dmi_addr); | |
589 | set_bit(hash, bitmask); | |
590 | } | |
591 | ||
592 | write_hash: | |
593 | for (i = 0; i < SPIDER_NET_MULTICAST_HASHES / 4; i++) { | |
594 | reg = 0; | |
595 | if (test_bit(i * 4, bitmask)) | |
596 | reg += 0x08; | |
597 | reg <<= 8; | |
598 | if (test_bit(i * 4 + 1, bitmask)) | |
599 | reg += 0x08; | |
600 | reg <<= 8; | |
601 | if (test_bit(i * 4 + 2, bitmask)) | |
602 | reg += 0x08; | |
603 | reg <<= 8; | |
604 | if (test_bit(i * 4 + 3, bitmask)) | |
605 | reg += 0x08; | |
606 | ||
607 | spider_net_write_reg(card, SPIDER_NET_GMRMHFILnR + i * 4, reg); | |
608 | } | |
609 | } | |
610 | ||
611 | /** | |
612 | * spider_net_disable_rxdmac - disables the receive DMA controller | |
613 | * @card: card structure | |
614 | * | |
615 | * spider_net_disable_rxdmac terminates processing on the DMA controller by | |
616 | * turing off DMA and issueing a force end | |
617 | */ | |
618 | static void | |
619 | spider_net_disable_rxdmac(struct spider_net_card *card) | |
620 | { | |
621 | spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR, | |
622 | SPIDER_NET_DMA_RX_FEND_VALUE); | |
623 | } | |
624 | ||
aaec0fab JO |
625 | /** |
626 | * spider_net_prepare_tx_descr - fill tx descriptor with skb data | |
627 | * @card: card structure | |
628 | * @descr: descriptor structure to fill out | |
629 | * @skb: packet to use | |
630 | * | |
631 | * returns 0 on success, <0 on failure. | |
632 | * | |
633 | * fills out the descriptor structure with skb data and len. Copies data, | |
634 | * if needed (32bit DMA!) | |
635 | */ | |
636 | static int | |
637 | spider_net_prepare_tx_descr(struct spider_net_card *card, | |
aaec0fab JO |
638 | struct sk_buff *skb) |
639 | { | |
9cc7bf7e | 640 | struct spider_net_descr *descr; |
11f1a52b | 641 | dma_addr_t buf; |
9cc7bf7e | 642 | unsigned long flags; |
11f1a52b | 643 | |
9c434f5e | 644 | buf = pci_map_single(card->pdev, skb->data, skb->len, PCI_DMA_TODEVICE); |
d4b0a4c1 | 645 | if (pci_dma_mapping_error(buf)) { |
11f1a52b | 646 | if (netif_msg_tx_err(card) && net_ratelimit()) |
aaec0fab | 647 | pr_err("could not iommu-map packet (%p, %i). " |
9c434f5e | 648 | "Dropping packet\n", skb->data, skb->len); |
9b6b0b81 | 649 | card->spider_stats.tx_iommu_map_error++; |
aaec0fab JO |
650 | return -ENOMEM; |
651 | } | |
652 | ||
9cc7bf7e LV |
653 | spin_lock_irqsave(&card->tx_chain.lock, flags); |
654 | descr = card->tx_chain.head; | |
655 | card->tx_chain.head = descr->next; | |
656 | ||
8e0a613b | 657 | descr->buf_addr = buf; |
9c434f5e | 658 | descr->buf_size = skb->len; |
bdd01503 | 659 | descr->next_descr_addr = 0; |
aaec0fab JO |
660 | descr->skb = skb; |
661 | descr->data_status = 0; | |
662 | ||
bdd01503 JO |
663 | descr->dmac_cmd_status = |
664 | SPIDER_NET_DESCR_CARDOWNED | SPIDER_NET_DMAC_NOCS; | |
9cc7bf7e LV |
665 | spin_unlock_irqrestore(&card->tx_chain.lock, flags); |
666 | ||
bdd01503 JO |
667 | if (skb->protocol == htons(ETH_P_IP)) |
668 | switch (skb->nh.iph->protocol) { | |
669 | case IPPROTO_TCP: | |
670 | descr->dmac_cmd_status |= SPIDER_NET_DMAC_TCP; | |
671 | break; | |
672 | case IPPROTO_UDP: | |
673 | descr->dmac_cmd_status |= SPIDER_NET_DMAC_UDP; | |
674 | break; | |
675 | } | |
676 | ||
204e5fa1 | 677 | /* Chain the bus address, so that the DMA engine finds this descr. */ |
bdd01503 JO |
678 | descr->prev->next_descr_addr = descr->bus_addr; |
679 | ||
917a5b8e | 680 | card->netdev->trans_start = jiffies; /* set netdev watchdog timer */ |
bdd01503 JO |
681 | return 0; |
682 | } | |
683 | ||
a664ccf4 | 684 | static int |
204e5fa1 LV |
685 | spider_net_set_low_watermark(struct spider_net_card *card) |
686 | { | |
9cc7bf7e | 687 | unsigned long flags; |
204e5fa1 LV |
688 | int status; |
689 | int cnt=0; | |
690 | int i; | |
691 | struct spider_net_descr *descr = card->tx_chain.tail; | |
692 | ||
9cc7bf7e LV |
693 | /* Measure the length of the queue. Measurement does not |
694 | * need to be precise -- does not need a lock. */ | |
204e5fa1 LV |
695 | while (descr != card->tx_chain.head) { |
696 | status = descr->dmac_cmd_status & SPIDER_NET_DESCR_NOT_IN_USE; | |
697 | if (status == SPIDER_NET_DESCR_NOT_IN_USE) | |
698 | break; | |
699 | descr = descr->next; | |
700 | cnt++; | |
701 | } | |
702 | ||
703 | /* If TX queue is short, don't even bother with interrupts */ | |
d4ed8f8d | 704 | if (cnt < card->tx_chain.num_desc/4) |
a664ccf4 | 705 | return cnt; |
204e5fa1 LV |
706 | |
707 | /* Set low-watermark 3/4th's of the way into the queue. */ | |
708 | descr = card->tx_chain.tail; | |
709 | cnt = (cnt*3)/4; | |
710 | for (i=0;i<cnt; i++) | |
711 | descr = descr->next; | |
712 | ||
713 | /* Set the new watermark, clear the old watermark */ | |
9cc7bf7e | 714 | spin_lock_irqsave(&card->tx_chain.lock, flags); |
204e5fa1 LV |
715 | descr->dmac_cmd_status |= SPIDER_NET_DESCR_TXDESFLG; |
716 | if (card->low_watermark && card->low_watermark != descr) | |
717 | card->low_watermark->dmac_cmd_status = | |
718 | card->low_watermark->dmac_cmd_status & ~SPIDER_NET_DESCR_TXDESFLG; | |
719 | card->low_watermark = descr; | |
9cc7bf7e | 720 | spin_unlock_irqrestore(&card->tx_chain.lock, flags); |
a664ccf4 | 721 | return cnt; |
204e5fa1 LV |
722 | } |
723 | ||
bdd01503 JO |
724 | /** |
725 | * spider_net_release_tx_chain - processes sent tx descriptors | |
726 | * @card: adapter structure | |
727 | * @brutal: if set, don't care about whether descriptor seems to be in use | |
728 | * | |
729 | * returns 0 if the tx ring is empty, otherwise 1. | |
730 | * | |
731 | * spider_net_release_tx_chain releases the tx descriptors that spider has | |
732 | * finished with (if non-brutal) or simply release tx descriptors (if brutal). | |
733 | * If some other context is calling this function, we return 1 so that we're | |
734 | * scheduled again (if we were scheduled) and will not loose initiative. | |
735 | */ | |
736 | static int | |
737 | spider_net_release_tx_chain(struct spider_net_card *card, int brutal) | |
738 | { | |
739 | struct spider_net_descr_chain *chain = &card->tx_chain; | |
9cc7bf7e LV |
740 | struct spider_net_descr *descr; |
741 | struct sk_buff *skb; | |
742 | u32 buf_addr; | |
743 | unsigned long flags; | |
bdd01503 JO |
744 | int status; |
745 | ||
bdd01503 | 746 | while (chain->tail != chain->head) { |
9cc7bf7e LV |
747 | spin_lock_irqsave(&chain->lock, flags); |
748 | descr = chain->tail; | |
749 | ||
750 | status = spider_net_get_descr_status(descr); | |
bdd01503 JO |
751 | switch (status) { |
752 | case SPIDER_NET_DESCR_COMPLETE: | |
753 | card->netdev_stats.tx_packets++; | |
9cc7bf7e | 754 | card->netdev_stats.tx_bytes += descr->skb->len; |
bdd01503 JO |
755 | break; |
756 | ||
757 | case SPIDER_NET_DESCR_CARDOWNED: | |
9cc7bf7e LV |
758 | if (!brutal) { |
759 | spin_unlock_irqrestore(&chain->lock, flags); | |
bdd01503 | 760 | return 1; |
9cc7bf7e LV |
761 | } |
762 | ||
bdd01503 JO |
763 | /* fallthrough, if we release the descriptors |
764 | * brutally (then we don't care about | |
765 | * SPIDER_NET_DESCR_CARDOWNED) */ | |
766 | ||
767 | case SPIDER_NET_DESCR_RESPONSE_ERROR: | |
768 | case SPIDER_NET_DESCR_PROTECTION_ERROR: | |
769 | case SPIDER_NET_DESCR_FORCE_END: | |
770 | if (netif_msg_tx_err(card)) | |
771 | pr_err("%s: forcing end of tx descriptor " | |
772 | "with status x%02x\n", | |
773 | card->netdev->name, status); | |
774 | card->netdev_stats.tx_errors++; | |
775 | break; | |
776 | ||
777 | default: | |
778 | card->netdev_stats.tx_dropped++; | |
9cc7bf7e LV |
779 | if (!brutal) { |
780 | spin_unlock_irqrestore(&chain->lock, flags); | |
c3fee4c5 | 781 | return 1; |
9cc7bf7e | 782 | } |
bdd01503 | 783 | } |
aaec0fab | 784 | |
9cc7bf7e LV |
785 | chain->tail = descr->next; |
786 | descr->dmac_cmd_status |= SPIDER_NET_DESCR_NOT_IN_USE; | |
787 | skb = descr->skb; | |
788 | buf_addr = descr->buf_addr; | |
789 | spin_unlock_irqrestore(&chain->lock, flags); | |
790 | ||
791 | /* unmap the skb */ | |
792 | if (skb) { | |
9c434f5e JL |
793 | pci_unmap_single(card->pdev, buf_addr, skb->len, |
794 | PCI_DMA_TODEVICE); | |
9cc7bf7e LV |
795 | dev_kfree_skb(skb); |
796 | } | |
797 | } | |
aaec0fab JO |
798 | return 0; |
799 | } | |
800 | ||
801 | /** | |
802 | * spider_net_kick_tx_dma - enables TX DMA processing | |
803 | * @card: card structure | |
804 | * @descr: descriptor address to enable TX processing at | |
805 | * | |
a664ccf4 LV |
806 | * This routine will start the transmit DMA running if |
807 | * it is not already running. This routine ned only be | |
808 | * called when queueing a new packet to an empty tx queue. | |
809 | * Writes the current tx chain head as start address | |
810 | * of the tx descriptor chain and enables the transmission | |
811 | * DMA engine. | |
aaec0fab | 812 | */ |
bdd01503 JO |
813 | static inline void |
814 | spider_net_kick_tx_dma(struct spider_net_card *card) | |
aaec0fab | 815 | { |
bdd01503 | 816 | struct spider_net_descr *descr; |
aaec0fab | 817 | |
bdd01503 JO |
818 | if (spider_net_read_reg(card, SPIDER_NET_GDTDMACCNTR) & |
819 | SPIDER_NET_TX_DMA_EN) | |
820 | goto out; | |
aaec0fab | 821 | |
bdd01503 JO |
822 | descr = card->tx_chain.tail; |
823 | for (;;) { | |
824 | if (spider_net_get_descr_status(descr) == | |
825 | SPIDER_NET_DESCR_CARDOWNED) { | |
826 | spider_net_write_reg(card, SPIDER_NET_GDTDCHA, | |
827 | descr->bus_addr); | |
828 | spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR, | |
829 | SPIDER_NET_DMA_TX_VALUE); | |
830 | break; | |
831 | } | |
832 | if (descr == card->tx_chain.head) | |
833 | break; | |
834 | descr = descr->next; | |
835 | } | |
836 | ||
837 | out: | |
838 | mod_timer(&card->tx_timer, jiffies + SPIDER_NET_TX_TIMER); | |
aaec0fab JO |
839 | } |
840 | ||
841 | /** | |
842 | * spider_net_xmit - transmits a frame over the device | |
843 | * @skb: packet to send out | |
844 | * @netdev: interface device structure | |
845 | * | |
bdd01503 | 846 | * returns 0 on success, !0 on failure |
aaec0fab JO |
847 | */ |
848 | static int | |
849 | spider_net_xmit(struct sk_buff *skb, struct net_device *netdev) | |
850 | { | |
a664ccf4 | 851 | int cnt; |
aaec0fab | 852 | struct spider_net_card *card = netdev_priv(netdev); |
bdd01503 | 853 | struct spider_net_descr_chain *chain = &card->tx_chain; |
bdd01503 | 854 | |
11f1a52b | 855 | spider_net_release_tx_chain(card, 0); |
aaec0fab | 856 | |
313ef4b7 | 857 | if ((chain->head->next == chain->tail->prev) || |
313ef4b7 | 858 | (spider_net_prepare_tx_descr(card, skb) != 0)) { |
bdd01503 | 859 | |
9b6b0b81 | 860 | card->netdev_stats.tx_dropped++; |
313ef4b7 LV |
861 | netif_stop_queue(netdev); |
862 | return NETDEV_TX_BUSY; | |
bdd01503 | 863 | } |
aaec0fab | 864 | |
a664ccf4 LV |
865 | cnt = spider_net_set_low_watermark(card); |
866 | if (cnt < 5) | |
867 | spider_net_kick_tx_dma(card); | |
313ef4b7 | 868 | return NETDEV_TX_OK; |
bdd01503 | 869 | } |
11f1a52b | 870 | |
bdd01503 JO |
871 | /** |
872 | * spider_net_cleanup_tx_ring - cleans up the TX ring | |
873 | * @card: card structure | |
874 | * | |
68a8c609 LV |
875 | * spider_net_cleanup_tx_ring is called by either the tx_timer |
876 | * or from the NAPI polling routine. | |
877 | * This routine releases resources associted with transmitted | |
878 | * packets, including updating the queue tail pointer. | |
bdd01503 JO |
879 | */ |
880 | static void | |
881 | spider_net_cleanup_tx_ring(struct spider_net_card *card) | |
882 | { | |
bdd01503 | 883 | if ((spider_net_release_tx_chain(card, 0) != 0) && |
313ef4b7 | 884 | (card->netdev->flags & IFF_UP)) { |
bdd01503 | 885 | spider_net_kick_tx_dma(card); |
313ef4b7 LV |
886 | netif_wake_queue(card->netdev); |
887 | } | |
aaec0fab JO |
888 | } |
889 | ||
890 | /** | |
891 | * spider_net_do_ioctl - called for device ioctls | |
892 | * @netdev: interface device structure | |
893 | * @ifr: request parameter structure for ioctl | |
894 | * @cmd: command code for ioctl | |
895 | * | |
896 | * returns 0 on success, <0 on failure. Currently, we have no special ioctls. | |
897 | * -EOPNOTSUPP is returned, if an unknown ioctl was requested | |
898 | */ | |
899 | static int | |
900 | spider_net_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd) | |
901 | { | |
902 | switch (cmd) { | |
903 | default: | |
904 | return -EOPNOTSUPP; | |
905 | } | |
906 | } | |
907 | ||
908 | /** | |
909 | * spider_net_pass_skb_up - takes an skb from a descriptor and passes it on | |
910 | * @descr: descriptor to process | |
911 | * @card: card structure | |
912 | * | |
05b346b5 LV |
913 | * Fills out skb structure and passes the data to the stack. |
914 | * The descriptor state is not changed. | |
aaec0fab | 915 | */ |
7f7223b8 | 916 | static void |
aaec0fab | 917 | spider_net_pass_skb_up(struct spider_net_descr *descr, |
1cd173f6 | 918 | struct spider_net_card *card) |
aaec0fab JO |
919 | { |
920 | struct sk_buff *skb; | |
921 | struct net_device *netdev; | |
922 | u32 data_status, data_error; | |
923 | ||
924 | data_status = descr->data_status; | |
925 | data_error = descr->data_error; | |
aaec0fab JO |
926 | netdev = card->netdev; |
927 | ||
11f1a52b | 928 | skb = descr->skb; |
aaec0fab JO |
929 | skb->dev = netdev; |
930 | skb_put(skb, descr->valid_size); | |
931 | ||
932 | /* the card seems to add 2 bytes of junk in front | |
933 | * of the ethernet frame */ | |
934 | #define SPIDER_MISALIGN 2 | |
935 | skb_pull(skb, SPIDER_MISALIGN); | |
936 | skb->protocol = eth_type_trans(skb, netdev); | |
937 | ||
938 | /* checksum offload */ | |
939 | if (card->options.rx_csum) { | |
11f1a52b AB |
940 | if ( ( (data_status & SPIDER_NET_DATA_STATUS_CKSUM_MASK) == |
941 | SPIDER_NET_DATA_STATUS_CKSUM_MASK) && | |
942 | !(data_error & SPIDER_NET_DATA_ERR_CKSUM_MASK)) | |
aaec0fab JO |
943 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
944 | else | |
945 | skb->ip_summed = CHECKSUM_NONE; | |
11f1a52b | 946 | } else |
aaec0fab | 947 | skb->ip_summed = CHECKSUM_NONE; |
aaec0fab JO |
948 | |
949 | if (data_status & SPIDER_NET_VLAN_PACKET) { | |
950 | /* further enhancements: HW-accel VLAN | |
951 | * vlan_hwaccel_receive_skb | |
952 | */ | |
953 | } | |
954 | ||
955 | /* pass skb up to stack */ | |
1cd173f6 | 956 | netif_receive_skb(skb); |
aaec0fab JO |
957 | |
958 | /* update netdevice statistics */ | |
959 | card->netdev_stats.rx_packets++; | |
960 | card->netdev_stats.rx_bytes += skb->len; | |
aaec0fab JO |
961 | } |
962 | ||
963 | /** | |
11f1a52b | 964 | * spider_net_decode_one_descr - processes an rx descriptor |
aaec0fab JO |
965 | * @card: card structure |
966 | * | |
1cd173f6 | 967 | * Returns 1 if a packet has been sent to the stack, otherwise 0 |
aaec0fab | 968 | * |
1cd173f6 | 969 | * Processes an rx descriptor by iommu-unmapping the data buffer and passing |
11f1a52b AB |
970 | * the packet up to the stack. This function is called in softirq |
971 | * context, e.g. either bottom half from interrupt or NAPI polling context | |
aaec0fab JO |
972 | */ |
973 | static int | |
1cd173f6 | 974 | spider_net_decode_one_descr(struct spider_net_card *card) |
aaec0fab | 975 | { |
bdd01503 JO |
976 | struct spider_net_descr_chain *chain = &card->rx_chain; |
977 | struct spider_net_descr *descr = chain->tail; | |
978 | int status; | |
aaec0fab | 979 | |
aaec0fab JO |
980 | status = spider_net_get_descr_status(descr); |
981 | ||
80dab7c7 LV |
982 | /* Nothing in the descriptor, or ring must be empty */ |
983 | if ((status == SPIDER_NET_DESCR_CARDOWNED) || | |
984 | (status == SPIDER_NET_DESCR_NOT_IN_USE)) | |
1cd173f6 | 985 | return 0; |
aaec0fab | 986 | |
11f1a52b | 987 | /* descriptor definitively used -- move on tail */ |
aaec0fab JO |
988 | chain->tail = descr->next; |
989 | ||
05b346b5 LV |
990 | /* unmap descriptor */ |
991 | pci_unmap_single(card->pdev, descr->buf_addr, | |
992 | SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE); | |
993 | ||
aaec0fab JO |
994 | if ( (status == SPIDER_NET_DESCR_RESPONSE_ERROR) || |
995 | (status == SPIDER_NET_DESCR_PROTECTION_ERROR) || | |
996 | (status == SPIDER_NET_DESCR_FORCE_END) ) { | |
997 | if (netif_msg_rx_err(card)) | |
998 | pr_err("%s: dropping RX descriptor with state %d\n", | |
999 | card->netdev->name, status); | |
1000 | card->netdev_stats.rx_dropped++; | |
7f7223b8 | 1001 | goto bad_desc; |
aaec0fab JO |
1002 | } |
1003 | ||
1004 | if ( (status != SPIDER_NET_DESCR_COMPLETE) && | |
1005 | (status != SPIDER_NET_DESCR_FRAME_END) ) { | |
5a028877 | 1006 | if (netif_msg_rx_err(card)) |
05b346b5 | 1007 | pr_err("%s: RX descriptor with unkown state %d\n", |
aaec0fab | 1008 | card->netdev->name, status); |
5a028877 | 1009 | card->spider_stats.rx_desc_unk_state++; |
7f7223b8 | 1010 | goto bad_desc; |
aaec0fab JO |
1011 | } |
1012 | ||
366684bd LV |
1013 | /* The cases we'll throw away the packet immediately */ |
1014 | if (descr->data_error & SPIDER_NET_DESTROY_RX_FLAGS) { | |
1015 | if (netif_msg_rx_err(card)) | |
1016 | pr_err("%s: error in received descriptor found, " | |
1017 | "data_status=x%08x, data_error=x%08x\n", | |
1018 | card->netdev->name, | |
1019 | descr->data_status, descr->data_error); | |
7f7223b8 | 1020 | goto bad_desc; |
366684bd LV |
1021 | } |
1022 | ||
7f7223b8 LV |
1023 | /* Ok, we've got a packet in descr */ |
1024 | spider_net_pass_skb_up(descr, card); | |
1cd173f6 | 1025 | descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE; |
7f7223b8 LV |
1026 | return 1; |
1027 | ||
1028 | bad_desc: | |
1029 | dev_kfree_skb_irq(descr->skb); | |
1030 | descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE; | |
1031 | return 0; | |
aaec0fab JO |
1032 | } |
1033 | ||
1034 | /** | |
1035 | * spider_net_poll - NAPI poll function called by the stack to return packets | |
1036 | * @netdev: interface device structure | |
1037 | * @budget: number of packets we can pass to the stack at most | |
1038 | * | |
1039 | * returns 0 if no more packets available to the driver/stack. Returns 1, | |
1040 | * if the quota is exceeded, but the driver has still packets. | |
1041 | * | |
1042 | * spider_net_poll returns all packets from the rx descriptors to the stack | |
1043 | * (using netif_receive_skb). If all/enough packets are up, the driver | |
1044 | * reenables interrupts and returns 0. If not, 1 is returned. | |
1045 | */ | |
1046 | static int | |
1047 | spider_net_poll(struct net_device *netdev, int *budget) | |
1048 | { | |
1049 | struct spider_net_card *card = netdev_priv(netdev); | |
1050 | int packets_to_do, packets_done = 0; | |
1051 | int no_more_packets = 0; | |
1052 | ||
68a8c609 | 1053 | spider_net_cleanup_tx_ring(card); |
aaec0fab JO |
1054 | packets_to_do = min(*budget, netdev->quota); |
1055 | ||
1056 | while (packets_to_do) { | |
1cd173f6 | 1057 | if (spider_net_decode_one_descr(card)) { |
aaec0fab JO |
1058 | packets_done++; |
1059 | packets_to_do--; | |
1060 | } else { | |
1061 | /* no more packets for the stack */ | |
1062 | no_more_packets = 1; | |
1063 | break; | |
1064 | } | |
1065 | } | |
1066 | ||
1067 | netdev->quota -= packets_done; | |
1068 | *budget -= packets_done; | |
11f1a52b | 1069 | spider_net_refill_rx_chain(card); |
80dab7c7 | 1070 | spider_net_enable_rxdmac(card); |
aaec0fab JO |
1071 | |
1072 | /* if all packets are in the stack, enable interrupts and return 0 */ | |
1073 | /* if not, return 1 */ | |
1074 | if (no_more_packets) { | |
1075 | netif_rx_complete(netdev); | |
1076 | spider_net_rx_irq_on(card); | |
1077 | return 0; | |
1078 | } | |
1079 | ||
1080 | return 1; | |
1081 | } | |
1082 | ||
1083 | /** | |
1084 | * spider_net_vlan_rx_reg - initializes VLAN structures in the driver and card | |
1085 | * @netdev: interface device structure | |
1086 | * @grp: vlan_group structure that is registered (NULL on destroying interface) | |
1087 | */ | |
1088 | static void | |
1089 | spider_net_vlan_rx_reg(struct net_device *netdev, struct vlan_group *grp) | |
1090 | { | |
1091 | /* further enhancement... yet to do */ | |
1092 | return; | |
1093 | } | |
1094 | ||
1095 | /** | |
1096 | * spider_net_vlan_rx_add - adds VLAN id to the card filter | |
1097 | * @netdev: interface device structure | |
1098 | * @vid: VLAN id to add | |
1099 | */ | |
1100 | static void | |
1101 | spider_net_vlan_rx_add(struct net_device *netdev, uint16_t vid) | |
1102 | { | |
1103 | /* further enhancement... yet to do */ | |
1104 | /* add vid to card's VLAN filter table */ | |
1105 | return; | |
1106 | } | |
1107 | ||
1108 | /** | |
1109 | * spider_net_vlan_rx_kill - removes VLAN id to the card filter | |
1110 | * @netdev: interface device structure | |
1111 | * @vid: VLAN id to remove | |
1112 | */ | |
1113 | static void | |
1114 | spider_net_vlan_rx_kill(struct net_device *netdev, uint16_t vid) | |
1115 | { | |
1116 | /* further enhancement... yet to do */ | |
1117 | /* remove vid from card's VLAN filter table */ | |
1118 | } | |
1119 | ||
1120 | /** | |
1121 | * spider_net_get_stats - get interface statistics | |
1122 | * @netdev: interface device structure | |
1123 | * | |
1124 | * returns the interface statistics residing in the spider_net_card struct | |
1125 | */ | |
1126 | static struct net_device_stats * | |
1127 | spider_net_get_stats(struct net_device *netdev) | |
1128 | { | |
1129 | struct spider_net_card *card = netdev_priv(netdev); | |
1130 | struct net_device_stats *stats = &card->netdev_stats; | |
1131 | return stats; | |
1132 | } | |
1133 | ||
1134 | /** | |
1135 | * spider_net_change_mtu - changes the MTU of an interface | |
1136 | * @netdev: interface device structure | |
1137 | * @new_mtu: new MTU value | |
1138 | * | |
1139 | * returns 0 on success, <0 on failure | |
1140 | */ | |
1141 | static int | |
1142 | spider_net_change_mtu(struct net_device *netdev, int new_mtu) | |
1143 | { | |
1144 | /* no need to re-alloc skbs or so -- the max mtu is about 2.3k | |
1145 | * and mtu is outbound only anyway */ | |
1146 | if ( (new_mtu < SPIDER_NET_MIN_MTU ) || | |
1147 | (new_mtu > SPIDER_NET_MAX_MTU) ) | |
1148 | return -EINVAL; | |
1149 | netdev->mtu = new_mtu; | |
1150 | return 0; | |
1151 | } | |
1152 | ||
1153 | /** | |
1154 | * spider_net_set_mac - sets the MAC of an interface | |
1155 | * @netdev: interface device structure | |
1156 | * @ptr: pointer to new MAC address | |
1157 | * | |
1158 | * Returns 0 on success, <0 on failure. Currently, we don't support this | |
1159 | * and will always return EOPNOTSUPP. | |
1160 | */ | |
1161 | static int | |
1162 | spider_net_set_mac(struct net_device *netdev, void *p) | |
1163 | { | |
1164 | struct spider_net_card *card = netdev_priv(netdev); | |
054034db | 1165 | u32 macl, macu, regvalue; |
aaec0fab JO |
1166 | struct sockaddr *addr = p; |
1167 | ||
aaec0fab JO |
1168 | if (!is_valid_ether_addr(addr->sa_data)) |
1169 | return -EADDRNOTAVAIL; | |
1170 | ||
054034db JO |
1171 | /* switch off GMACTPE and GMACRPE */ |
1172 | regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD); | |
1173 | regvalue &= ~((1 << 5) | (1 << 6)); | |
1174 | spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue); | |
1175 | ||
1176 | /* write mac */ | |
aaec0fab JO |
1177 | macu = (addr->sa_data[0]<<24) + (addr->sa_data[1]<<16) + |
1178 | (addr->sa_data[2]<<8) + (addr->sa_data[3]); | |
1179 | macl = (addr->sa_data[4]<<8) + (addr->sa_data[5]); | |
1180 | spider_net_write_reg(card, SPIDER_NET_GMACUNIMACU, macu); | |
1181 | spider_net_write_reg(card, SPIDER_NET_GMACUNIMACL, macl); | |
1182 | ||
054034db JO |
1183 | /* switch GMACTPE and GMACRPE back on */ |
1184 | regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD); | |
1185 | regvalue |= ((1 << 5) | (1 << 6)); | |
1186 | spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue); | |
1187 | ||
aaec0fab JO |
1188 | spider_net_set_promisc(card); |
1189 | ||
1190 | /* look up, whether we have been successful */ | |
1191 | if (spider_net_get_mac_address(netdev)) | |
1192 | return -EADDRNOTAVAIL; | |
1193 | if (memcmp(netdev->dev_addr,addr->sa_data,netdev->addr_len)) | |
1194 | return -EADDRNOTAVAIL; | |
1195 | ||
1196 | return 0; | |
1197 | } | |
1198 | ||
aaec0fab JO |
1199 | /** |
1200 | * spider_net_handle_error_irq - handles errors raised by an interrupt | |
1201 | * @card: card structure | |
1202 | * @status_reg: interrupt status register 0 (GHIINT0STS) | |
1203 | * | |
1204 | * spider_net_handle_error_irq treats or ignores all error conditions | |
1205 | * found when an interrupt is presented | |
1206 | */ | |
1207 | static void | |
1208 | spider_net_handle_error_irq(struct spider_net_card *card, u32 status_reg) | |
1209 | { | |
1210 | u32 error_reg1, error_reg2; | |
1211 | u32 i; | |
1212 | int show_error = 1; | |
1213 | ||
1214 | error_reg1 = spider_net_read_reg(card, SPIDER_NET_GHIINT1STS); | |
1215 | error_reg2 = spider_net_read_reg(card, SPIDER_NET_GHIINT2STS); | |
1216 | ||
1217 | /* check GHIINT0STS ************************************/ | |
1218 | if (status_reg) | |
1219 | for (i = 0; i < 32; i++) | |
1220 | if (status_reg & (1<<i)) | |
1221 | switch (i) | |
1222 | { | |
1223 | /* let error_reg1 and error_reg2 evaluation decide, what to do | |
1224 | case SPIDER_NET_PHYINT: | |
1225 | case SPIDER_NET_GMAC2INT: | |
1226 | case SPIDER_NET_GMAC1INT: | |
aaec0fab JO |
1227 | case SPIDER_NET_GFIFOINT: |
1228 | case SPIDER_NET_DMACINT: | |
1229 | case SPIDER_NET_GSYSINT: | |
1230 | break; */ | |
1231 | ||
98b9040c LV |
1232 | case SPIDER_NET_GIPSINT: |
1233 | show_error = 0; | |
1234 | break; | |
1235 | ||
aaec0fab JO |
1236 | case SPIDER_NET_GPWOPCMPINT: |
1237 | /* PHY write operation completed */ | |
1238 | show_error = 0; | |
1239 | break; | |
1240 | case SPIDER_NET_GPROPCMPINT: | |
1241 | /* PHY read operation completed */ | |
1242 | /* we don't use semaphores, as we poll for the completion | |
1243 | * of the read operation in spider_net_read_phy. Should take | |
1244 | * about 50 us */ | |
1245 | show_error = 0; | |
1246 | break; | |
1247 | case SPIDER_NET_GPWFFINT: | |
1248 | /* PHY command queue full */ | |
1249 | if (netif_msg_intr(card)) | |
1250 | pr_err("PHY write queue full\n"); | |
1251 | show_error = 0; | |
1252 | break; | |
1253 | ||
1254 | /* case SPIDER_NET_GRMDADRINT: not used. print a message */ | |
1255 | /* case SPIDER_NET_GRMARPINT: not used. print a message */ | |
1256 | /* case SPIDER_NET_GRMMPINT: not used. print a message */ | |
1257 | ||
1258 | case SPIDER_NET_GDTDEN0INT: | |
1259 | /* someone has set TX_DMA_EN to 0 */ | |
1260 | show_error = 0; | |
1261 | break; | |
1262 | ||
1263 | case SPIDER_NET_GDDDEN0INT: /* fallthrough */ | |
1264 | case SPIDER_NET_GDCDEN0INT: /* fallthrough */ | |
1265 | case SPIDER_NET_GDBDEN0INT: /* fallthrough */ | |
1266 | case SPIDER_NET_GDADEN0INT: | |
1267 | /* someone has set RX_DMA_EN to 0 */ | |
1268 | show_error = 0; | |
1269 | break; | |
1270 | ||
1271 | /* RX interrupts */ | |
1272 | case SPIDER_NET_GDDFDCINT: | |
1273 | case SPIDER_NET_GDCFDCINT: | |
1274 | case SPIDER_NET_GDBFDCINT: | |
1275 | case SPIDER_NET_GDAFDCINT: | |
1276 | /* case SPIDER_NET_GDNMINT: not used. print a message */ | |
1277 | /* case SPIDER_NET_GCNMINT: not used. print a message */ | |
1278 | /* case SPIDER_NET_GBNMINT: not used. print a message */ | |
1279 | /* case SPIDER_NET_GANMINT: not used. print a message */ | |
1280 | /* case SPIDER_NET_GRFNMINT: not used. print a message */ | |
1281 | show_error = 0; | |
1282 | break; | |
1283 | ||
1284 | /* TX interrupts */ | |
1285 | case SPIDER_NET_GDTFDCINT: | |
1286 | show_error = 0; | |
1287 | break; | |
1288 | case SPIDER_NET_GTTEDINT: | |
1289 | show_error = 0; | |
1290 | break; | |
1291 | case SPIDER_NET_GDTDCEINT: | |
1292 | /* chain end. If a descriptor should be sent, kick off | |
1293 | * tx dma | |
98b9040c | 1294 | if (card->tx_chain.tail != card->tx_chain.head) |
aaec0fab | 1295 | spider_net_kick_tx_dma(card); |
98b9040c LV |
1296 | */ |
1297 | show_error = 0; | |
aaec0fab JO |
1298 | break; |
1299 | ||
1300 | /* case SPIDER_NET_G1TMCNTINT: not used. print a message */ | |
1301 | /* case SPIDER_NET_GFREECNTINT: not used. print a message */ | |
1302 | } | |
1303 | ||
1304 | /* check GHIINT1STS ************************************/ | |
1305 | if (error_reg1) | |
1306 | for (i = 0; i < 32; i++) | |
1307 | if (error_reg1 & (1<<i)) | |
1308 | switch (i) | |
1309 | { | |
1310 | case SPIDER_NET_GTMFLLINT: | |
11f1a52b | 1311 | if (netif_msg_intr(card) && net_ratelimit()) |
aaec0fab JO |
1312 | pr_err("Spider TX RAM full\n"); |
1313 | show_error = 0; | |
1314 | break; | |
11f1a52b AB |
1315 | case SPIDER_NET_GRFDFLLINT: /* fallthrough */ |
1316 | case SPIDER_NET_GRFCFLLINT: /* fallthrough */ | |
1317 | case SPIDER_NET_GRFBFLLINT: /* fallthrough */ | |
1318 | case SPIDER_NET_GRFAFLLINT: /* fallthrough */ | |
aaec0fab | 1319 | case SPIDER_NET_GRMFLLINT: |
11f1a52b | 1320 | if (netif_msg_intr(card) && net_ratelimit()) |
5a028877 | 1321 | pr_err("Spider RX RAM full, incoming packets " |
11f1a52b AB |
1322 | "might be discarded!\n"); |
1323 | spider_net_rx_irq_off(card); | |
75856175 | 1324 | netif_rx_schedule(card->netdev); |
11f1a52b | 1325 | show_error = 0; |
aaec0fab JO |
1326 | break; |
1327 | ||
1328 | /* case SPIDER_NET_GTMSHTINT: problem, print a message */ | |
1329 | case SPIDER_NET_GDTINVDINT: | |
1330 | /* allrighty. tx from previous descr ok */ | |
1331 | show_error = 0; | |
1332 | break; | |
aaec0fab JO |
1333 | |
1334 | /* chain end */ | |
1335 | case SPIDER_NET_GDDDCEINT: /* fallthrough */ | |
1336 | case SPIDER_NET_GDCDCEINT: /* fallthrough */ | |
1337 | case SPIDER_NET_GDBDCEINT: /* fallthrough */ | |
1338 | case SPIDER_NET_GDADCEINT: | |
5a028877 | 1339 | if (netif_msg_intr(card) && net_ratelimit()) |
aaec0fab JO |
1340 | pr_err("got descriptor chain end interrupt, " |
1341 | "restarting DMAC %c.\n", | |
37aad750 | 1342 | 'D'-(i-SPIDER_NET_GDDDCEINT)/3); |
aaec0fab | 1343 | spider_net_refill_rx_chain(card); |
11f1a52b | 1344 | spider_net_enable_rxdmac(card); |
aaec0fab JO |
1345 | show_error = 0; |
1346 | break; | |
1347 | ||
1348 | /* invalid descriptor */ | |
1349 | case SPIDER_NET_GDDINVDINT: /* fallthrough */ | |
1350 | case SPIDER_NET_GDCINVDINT: /* fallthrough */ | |
1351 | case SPIDER_NET_GDBINVDINT: /* fallthrough */ | |
1352 | case SPIDER_NET_GDAINVDINT: | |
1353 | /* could happen when rx chain is full */ | |
1354 | spider_net_refill_rx_chain(card); | |
11f1a52b | 1355 | spider_net_enable_rxdmac(card); |
aaec0fab JO |
1356 | show_error = 0; |
1357 | break; | |
1358 | ||
1359 | /* case SPIDER_NET_GDTRSERINT: problem, print a message */ | |
1360 | /* case SPIDER_NET_GDDRSERINT: problem, print a message */ | |
1361 | /* case SPIDER_NET_GDCRSERINT: problem, print a message */ | |
1362 | /* case SPIDER_NET_GDBRSERINT: problem, print a message */ | |
1363 | /* case SPIDER_NET_GDARSERINT: problem, print a message */ | |
1364 | /* case SPIDER_NET_GDSERINT: problem, print a message */ | |
1365 | /* case SPIDER_NET_GDTPTERINT: problem, print a message */ | |
1366 | /* case SPIDER_NET_GDDPTERINT: problem, print a message */ | |
1367 | /* case SPIDER_NET_GDCPTERINT: problem, print a message */ | |
1368 | /* case SPIDER_NET_GDBPTERINT: problem, print a message */ | |
1369 | /* case SPIDER_NET_GDAPTERINT: problem, print a message */ | |
1370 | default: | |
1371 | show_error = 1; | |
1372 | break; | |
1373 | } | |
1374 | ||
1375 | /* check GHIINT2STS ************************************/ | |
1376 | if (error_reg2) | |
1377 | for (i = 0; i < 32; i++) | |
1378 | if (error_reg2 & (1<<i)) | |
1379 | switch (i) | |
1380 | { | |
1381 | /* there is nothing we can (want to) do at this time. Log a | |
1382 | * message, we can switch on and off the specific values later on | |
1383 | case SPIDER_NET_GPROPERINT: | |
1384 | case SPIDER_NET_GMCTCRSNGINT: | |
1385 | case SPIDER_NET_GMCTLCOLINT: | |
1386 | case SPIDER_NET_GMCTTMOTINT: | |
1387 | case SPIDER_NET_GMCRCAERINT: | |
1388 | case SPIDER_NET_GMCRCALERINT: | |
1389 | case SPIDER_NET_GMCRALNERINT: | |
1390 | case SPIDER_NET_GMCROVRINT: | |
1391 | case SPIDER_NET_GMCRRNTINT: | |
1392 | case SPIDER_NET_GMCRRXERINT: | |
1393 | case SPIDER_NET_GTITCSERINT: | |
1394 | case SPIDER_NET_GTIFMTERINT: | |
1395 | case SPIDER_NET_GTIPKTRVKINT: | |
1396 | case SPIDER_NET_GTISPINGINT: | |
1397 | case SPIDER_NET_GTISADNGINT: | |
1398 | case SPIDER_NET_GTISPDNGINT: | |
1399 | case SPIDER_NET_GRIFMTERINT: | |
1400 | case SPIDER_NET_GRIPKTRVKINT: | |
1401 | case SPIDER_NET_GRISPINGINT: | |
1402 | case SPIDER_NET_GRISADNGINT: | |
1403 | case SPIDER_NET_GRISPDNGINT: | |
1404 | break; | |
1405 | */ | |
1406 | default: | |
1407 | break; | |
1408 | } | |
1409 | ||
5a028877 | 1410 | if ((show_error) && (netif_msg_intr(card)) && net_ratelimit()) |
98b9040c | 1411 | pr_err("Got error interrupt on %s, GHIINT0STS = 0x%08x, " |
aaec0fab | 1412 | "GHIINT1STS = 0x%08x, GHIINT2STS = 0x%08x\n", |
98b9040c | 1413 | card->netdev->name, |
aaec0fab JO |
1414 | status_reg, error_reg1, error_reg2); |
1415 | ||
1416 | /* clear interrupt sources */ | |
1417 | spider_net_write_reg(card, SPIDER_NET_GHIINT1STS, error_reg1); | |
1418 | spider_net_write_reg(card, SPIDER_NET_GHIINT2STS, error_reg2); | |
1419 | } | |
1420 | ||
1421 | /** | |
1422 | * spider_net_interrupt - interrupt handler for spider_net | |
1423 | * @irq: interupt number | |
1424 | * @ptr: pointer to net_device | |
1425 | * @regs: PU registers | |
1426 | * | |
1427 | * returns IRQ_HANDLED, if interrupt was for driver, or IRQ_NONE, if no | |
1428 | * interrupt found raised by card. | |
1429 | * | |
1430 | * This is the interrupt handler, that turns off | |
1431 | * interrupts for this device and makes the stack poll the driver | |
1432 | */ | |
1433 | static irqreturn_t | |
7d12e780 | 1434 | spider_net_interrupt(int irq, void *ptr) |
aaec0fab JO |
1435 | { |
1436 | struct net_device *netdev = ptr; | |
1437 | struct spider_net_card *card = netdev_priv(netdev); | |
1438 | u32 status_reg; | |
1439 | ||
1440 | status_reg = spider_net_read_reg(card, SPIDER_NET_GHIINT0STS); | |
1441 | ||
1442 | if (!status_reg) | |
1443 | return IRQ_NONE; | |
1444 | ||
aaec0fab JO |
1445 | if (status_reg & SPIDER_NET_RXINT ) { |
1446 | spider_net_rx_irq_off(card); | |
1447 | netif_rx_schedule(netdev); | |
1448 | } | |
68a8c609 LV |
1449 | if (status_reg & SPIDER_NET_TXINT) |
1450 | netif_rx_schedule(netdev); | |
aaec0fab | 1451 | |
11f1a52b AB |
1452 | if (status_reg & SPIDER_NET_ERRINT ) |
1453 | spider_net_handle_error_irq(card, status_reg); | |
aaec0fab JO |
1454 | |
1455 | /* clear interrupt sources */ | |
1456 | spider_net_write_reg(card, SPIDER_NET_GHIINT0STS, status_reg); | |
1457 | ||
1458 | return IRQ_HANDLED; | |
1459 | } | |
1460 | ||
1461 | #ifdef CONFIG_NET_POLL_CONTROLLER | |
1462 | /** | |
1463 | * spider_net_poll_controller - artificial interrupt for netconsole etc. | |
1464 | * @netdev: interface device structure | |
1465 | * | |
1466 | * see Documentation/networking/netconsole.txt | |
1467 | */ | |
1468 | static void | |
1469 | spider_net_poll_controller(struct net_device *netdev) | |
1470 | { | |
1471 | disable_irq(netdev->irq); | |
7d12e780 | 1472 | spider_net_interrupt(netdev->irq, netdev); |
aaec0fab JO |
1473 | enable_irq(netdev->irq); |
1474 | } | |
1475 | #endif /* CONFIG_NET_POLL_CONTROLLER */ | |
1476 | ||
1477 | /** | |
1478 | * spider_net_init_card - initializes the card | |
1479 | * @card: card structure | |
1480 | * | |
1481 | * spider_net_init_card initializes the card so that other registers can | |
1482 | * be used | |
1483 | */ | |
1484 | static void | |
1485 | spider_net_init_card(struct spider_net_card *card) | |
1486 | { | |
1487 | spider_net_write_reg(card, SPIDER_NET_CKRCTRL, | |
1488 | SPIDER_NET_CKRCTRL_STOP_VALUE); | |
1489 | ||
1490 | spider_net_write_reg(card, SPIDER_NET_CKRCTRL, | |
1491 | SPIDER_NET_CKRCTRL_RUN_VALUE); | |
1492 | } | |
1493 | ||
1494 | /** | |
1495 | * spider_net_enable_card - enables the card by setting all kinds of regs | |
1496 | * @card: card structure | |
1497 | * | |
1498 | * spider_net_enable_card sets a lot of SMMIO registers to enable the device | |
1499 | */ | |
1500 | static void | |
1501 | spider_net_enable_card(struct spider_net_card *card) | |
1502 | { | |
1503 | int i; | |
1504 | /* the following array consists of (register),(value) pairs | |
1505 | * that are set in this function. A register of 0 ends the list */ | |
1506 | u32 regs[][2] = { | |
1507 | { SPIDER_NET_GRESUMINTNUM, 0 }, | |
1508 | { SPIDER_NET_GREINTNUM, 0 }, | |
1509 | ||
1510 | /* set interrupt frame number registers */ | |
1511 | /* clear the single DMA engine registers first */ | |
1512 | { SPIDER_NET_GFAFRMNUM, SPIDER_NET_GFXFRAMES_VALUE }, | |
1513 | { SPIDER_NET_GFBFRMNUM, SPIDER_NET_GFXFRAMES_VALUE }, | |
1514 | { SPIDER_NET_GFCFRMNUM, SPIDER_NET_GFXFRAMES_VALUE }, | |
1515 | { SPIDER_NET_GFDFRMNUM, SPIDER_NET_GFXFRAMES_VALUE }, | |
1516 | /* then set, what we really need */ | |
1517 | { SPIDER_NET_GFFRMNUM, SPIDER_NET_FRAMENUM_VALUE }, | |
1518 | ||
1519 | /* timer counter registers and stuff */ | |
1520 | { SPIDER_NET_GFREECNNUM, 0 }, | |
1521 | { SPIDER_NET_GONETIMENUM, 0 }, | |
1522 | { SPIDER_NET_GTOUTFRMNUM, 0 }, | |
1523 | ||
1524 | /* RX mode setting */ | |
1525 | { SPIDER_NET_GRXMDSET, SPIDER_NET_RXMODE_VALUE }, | |
1526 | /* TX mode setting */ | |
1527 | { SPIDER_NET_GTXMDSET, SPIDER_NET_TXMODE_VALUE }, | |
1528 | /* IPSEC mode setting */ | |
1529 | { SPIDER_NET_GIPSECINIT, SPIDER_NET_IPSECINIT_VALUE }, | |
1530 | ||
1531 | { SPIDER_NET_GFTRESTRT, SPIDER_NET_RESTART_VALUE }, | |
1532 | ||
1533 | { SPIDER_NET_GMRWOLCTRL, 0 }, | |
b636d17a JO |
1534 | { SPIDER_NET_GTESTMD, 0x10000000 }, |
1535 | { SPIDER_NET_GTTQMSK, 0x00400040 }, | |
aaec0fab JO |
1536 | |
1537 | { SPIDER_NET_GMACINTEN, 0 }, | |
1538 | ||
1539 | /* flow control stuff */ | |
1540 | { SPIDER_NET_GMACAPAUSE, SPIDER_NET_MACAPAUSE_VALUE }, | |
1541 | { SPIDER_NET_GMACTXPAUSE, SPIDER_NET_TXPAUSE_VALUE }, | |
1542 | ||
1543 | { SPIDER_NET_GMACBSTLMT, SPIDER_NET_BURSTLMT_VALUE }, | |
1544 | { 0, 0} | |
1545 | }; | |
1546 | ||
1547 | i = 0; | |
1548 | while (regs[i][0]) { | |
1549 | spider_net_write_reg(card, regs[i][0], regs[i][1]); | |
1550 | i++; | |
1551 | } | |
1552 | ||
1553 | /* clear unicast filter table entries 1 to 14 */ | |
1554 | for (i = 1; i <= 14; i++) { | |
1555 | spider_net_write_reg(card, | |
1556 | SPIDER_NET_GMRUAFILnR + i * 8, | |
1557 | 0x00080000); | |
1558 | spider_net_write_reg(card, | |
1559 | SPIDER_NET_GMRUAFILnR + i * 8 + 4, | |
1560 | 0x00000000); | |
1561 | } | |
1562 | ||
1563 | spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R, 0x08080000); | |
1564 | ||
1565 | spider_net_write_reg(card, SPIDER_NET_ECMODE, SPIDER_NET_ECMODE_VALUE); | |
1566 | ||
1567 | /* set chain tail adress for RX chains and | |
1568 | * enable DMA */ | |
1569 | spider_net_enable_rxchtails(card); | |
1570 | spider_net_enable_rxdmac(card); | |
1571 | ||
1572 | spider_net_write_reg(card, SPIDER_NET_GRXDMAEN, SPIDER_NET_WOL_VALUE); | |
1573 | ||
aaec0fab JO |
1574 | spider_net_write_reg(card, SPIDER_NET_GMACLENLMT, |
1575 | SPIDER_NET_LENLMT_VALUE); | |
1576 | spider_net_write_reg(card, SPIDER_NET_GMACMODE, | |
1577 | SPIDER_NET_MACMODE_VALUE); | |
1578 | spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, | |
1579 | SPIDER_NET_OPMODE_VALUE); | |
1580 | ||
1581 | /* set interrupt mask registers */ | |
1582 | spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, | |
1583 | SPIDER_NET_INT0_MASK_VALUE); | |
1584 | spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK, | |
1585 | SPIDER_NET_INT1_MASK_VALUE); | |
1586 | spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK, | |
1587 | SPIDER_NET_INT2_MASK_VALUE); | |
bdd01503 JO |
1588 | |
1589 | spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR, | |
7bd54c86 | 1590 | SPIDER_NET_GDTBSTA); |
aaec0fab JO |
1591 | } |
1592 | ||
1593 | /** | |
1594 | * spider_net_open - called upon ifonfig up | |
1595 | * @netdev: interface device structure | |
1596 | * | |
1597 | * returns 0 on success, <0 on failure | |
1598 | * | |
1599 | * spider_net_open allocates all the descriptors and memory needed for | |
1600 | * operation, sets up multicast list and enables interrupts | |
1601 | */ | |
1602 | int | |
1603 | spider_net_open(struct net_device *netdev) | |
1604 | { | |
1605 | struct spider_net_card *card = netdev_priv(netdev); | |
348bc2a6 | 1606 | struct spider_net_descr *descr; |
d4ed8f8d | 1607 | int result; |
aaec0fab | 1608 | |
d4ed8f8d LV |
1609 | result = spider_net_init_chain(card, &card->tx_chain); |
1610 | if (result) | |
aaec0fab | 1611 | goto alloc_tx_failed; |
204e5fa1 LV |
1612 | card->low_watermark = NULL; |
1613 | ||
d4ed8f8d LV |
1614 | result = spider_net_init_chain(card, &card->rx_chain); |
1615 | if (result) | |
aaec0fab JO |
1616 | goto alloc_rx_failed; |
1617 | ||
d4ed8f8d LV |
1618 | /* Make a ring of of bus addresses */ |
1619 | descr = card->rx_chain.ring; | |
1620 | do { | |
348bc2a6 | 1621 | descr->next_descr_addr = descr->next->bus_addr; |
d4ed8f8d LV |
1622 | descr = descr->next; |
1623 | } while (descr != card->rx_chain.ring); | |
348bc2a6 | 1624 | |
d4ed8f8d | 1625 | /* Allocate rx skbs */ |
aaec0fab JO |
1626 | if (spider_net_alloc_rx_skbs(card)) |
1627 | goto alloc_skbs_failed; | |
1628 | ||
1629 | spider_net_set_multi(netdev); | |
1630 | ||
1631 | /* further enhancement: setup hw vlan, if needed */ | |
1632 | ||
1633 | result = -EBUSY; | |
1634 | if (request_irq(netdev->irq, spider_net_interrupt, | |
1fb9df5d | 1635 | IRQF_SHARED, netdev->name, netdev)) |
aaec0fab JO |
1636 | goto register_int_failed; |
1637 | ||
1638 | spider_net_enable_card(card); | |
1639 | ||
543cec51 JO |
1640 | netif_start_queue(netdev); |
1641 | netif_carrier_on(netdev); | |
1642 | netif_poll_enable(netdev); | |
1643 | ||
aaec0fab JO |
1644 | return 0; |
1645 | ||
1646 | register_int_failed: | |
1647 | spider_net_free_rx_chain_contents(card); | |
1648 | alloc_skbs_failed: | |
1649 | spider_net_free_chain(card, &card->rx_chain); | |
1650 | alloc_rx_failed: | |
1651 | spider_net_free_chain(card, &card->tx_chain); | |
1652 | alloc_tx_failed: | |
1653 | return result; | |
1654 | } | |
1655 | ||
1656 | /** | |
1657 | * spider_net_setup_phy - setup PHY | |
1658 | * @card: card structure | |
1659 | * | |
1660 | * returns 0 on success, <0 on failure | |
1661 | * | |
1662 | * spider_net_setup_phy is used as part of spider_net_probe. Sets | |
1663 | * the PHY to 1000 Mbps | |
1664 | **/ | |
1665 | static int | |
1666 | spider_net_setup_phy(struct spider_net_card *card) | |
1667 | { | |
1668 | struct mii_phy *phy = &card->phy; | |
1669 | ||
1670 | spider_net_write_reg(card, SPIDER_NET_GDTDMASEL, | |
1671 | SPIDER_NET_DMASEL_VALUE); | |
1672 | spider_net_write_reg(card, SPIDER_NET_GPCCTRL, | |
1673 | SPIDER_NET_PHY_CTRL_VALUE); | |
1674 | phy->mii_id = 1; | |
1675 | phy->dev = card->netdev; | |
1676 | phy->mdio_read = spider_net_read_phy; | |
1677 | phy->mdio_write = spider_net_write_phy; | |
1678 | ||
1679 | mii_phy_probe(phy, phy->mii_id); | |
1680 | ||
1681 | if (phy->def->ops->setup_forced) | |
1682 | phy->def->ops->setup_forced(phy, SPEED_1000, DUPLEX_FULL); | |
1683 | ||
8ec93459 | 1684 | phy->def->ops->enable_fiber(phy); |
53abbf7e | 1685 | |
aaec0fab JO |
1686 | phy->def->ops->read_link(phy); |
1687 | pr_info("Found %s with %i Mbps, %s-duplex.\n", phy->def->name, | |
1688 | phy->speed, phy->duplex==1 ? "Full" : "Half"); | |
1689 | ||
1690 | return 0; | |
1691 | } | |
1692 | ||
1693 | /** | |
1694 | * spider_net_download_firmware - loads firmware into the adapter | |
1695 | * @card: card structure | |
11f1a52b | 1696 | * @firmware_ptr: pointer to firmware data |
aaec0fab | 1697 | * |
11f1a52b AB |
1698 | * spider_net_download_firmware loads the firmware data into the |
1699 | * adapter. It assumes the length etc. to be allright. | |
aaec0fab | 1700 | */ |
0d3ea166 | 1701 | static int |
aaec0fab | 1702 | spider_net_download_firmware(struct spider_net_card *card, |
1a2509c9 | 1703 | const void *firmware_ptr) |
aaec0fab JO |
1704 | { |
1705 | int sequencer, i; | |
1a2509c9 | 1706 | const u32 *fw_ptr = firmware_ptr; |
aaec0fab JO |
1707 | |
1708 | /* stop sequencers */ | |
1709 | spider_net_write_reg(card, SPIDER_NET_GSINIT, | |
1710 | SPIDER_NET_STOP_SEQ_VALUE); | |
1711 | ||
11f1a52b AB |
1712 | for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS; |
1713 | sequencer++) { | |
aaec0fab JO |
1714 | spider_net_write_reg(card, |
1715 | SPIDER_NET_GSnPRGADR + sequencer * 8, 0); | |
11f1a52b | 1716 | for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) { |
aaec0fab JO |
1717 | spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT + |
1718 | sequencer * 8, *fw_ptr); | |
1719 | fw_ptr++; | |
1720 | } | |
1721 | } | |
1722 | ||
0d3ea166 AB |
1723 | if (spider_net_read_reg(card, SPIDER_NET_GSINIT)) |
1724 | return -EIO; | |
1725 | ||
aaec0fab JO |
1726 | spider_net_write_reg(card, SPIDER_NET_GSINIT, |
1727 | SPIDER_NET_RUN_SEQ_VALUE); | |
0d3ea166 AB |
1728 | |
1729 | return 0; | |
aaec0fab JO |
1730 | } |
1731 | ||
1732 | /** | |
1733 | * spider_net_init_firmware - reads in firmware parts | |
1734 | * @card: card structure | |
1735 | * | |
1736 | * Returns 0 on success, <0 on failure | |
1737 | * | |
1738 | * spider_net_init_firmware opens the sequencer firmware and does some basic | |
1739 | * checks. This function opens and releases the firmware structure. A call | |
1740 | * to download the firmware is performed before the release. | |
1741 | * | |
1742 | * Firmware format | |
1743 | * =============== | |
1744 | * spider_fw.bin is expected to be a file containing 6*1024*4 bytes, 4k being | |
1745 | * the program for each sequencer. Use the command | |
1746 | * tail -q -n +2 Seq_code1_0x088.txt Seq_code2_0x090.txt \ | |
1747 | * Seq_code3_0x098.txt Seq_code4_0x0A0.txt Seq_code5_0x0A8.txt \ | |
1748 | * Seq_code6_0x0B0.txt | xxd -r -p -c4 > spider_fw.bin | |
1749 | * | |
1750 | * to generate spider_fw.bin, if you have sequencer programs with something | |
1751 | * like the following contents for each sequencer: | |
1752 | * <ONE LINE COMMENT> | |
1753 | * <FIRST 4-BYTES-WORD FOR SEQUENCER> | |
1754 | * <SECOND 4-BYTES-WORD FOR SEQUENCER> | |
1755 | * ... | |
1756 | * <1024th 4-BYTES-WORD FOR SEQUENCER> | |
1757 | */ | |
1758 | static int | |
1759 | spider_net_init_firmware(struct spider_net_card *card) | |
1760 | { | |
11f1a52b | 1761 | struct firmware *firmware = NULL; |
030d6753 | 1762 | struct device_node *dn; |
1a2509c9 | 1763 | const u8 *fw_prop = NULL; |
11f1a52b AB |
1764 | int err = -ENOENT; |
1765 | int fw_size; | |
aaec0fab | 1766 | |
030d6753 | 1767 | if (request_firmware((const struct firmware **)&firmware, |
11f1a52b AB |
1768 | SPIDER_NET_FIRMWARE_NAME, &card->pdev->dev) == 0) { |
1769 | if ( (firmware->size != SPIDER_NET_FIRMWARE_LEN) && | |
1770 | netif_msg_probe(card) ) { | |
1771 | pr_err("Incorrect size of spidernet firmware in " \ | |
1772 | "filesystem. Looking in host firmware...\n"); | |
1773 | goto try_host_fw; | |
1774 | } | |
1775 | err = spider_net_download_firmware(card, firmware->data); | |
030d6753 | 1776 | |
11f1a52b AB |
1777 | release_firmware(firmware); |
1778 | if (err) | |
1779 | goto try_host_fw; | |
030d6753 | 1780 | |
11f1a52b | 1781 | goto done; |
aaec0fab JO |
1782 | } |
1783 | ||
11f1a52b AB |
1784 | try_host_fw: |
1785 | dn = pci_device_to_OF_node(card->pdev); | |
1786 | if (!dn) | |
1787 | goto out_err; | |
1788 | ||
1a2509c9 | 1789 | fw_prop = get_property(dn, "firmware", &fw_size); |
11f1a52b AB |
1790 | if (!fw_prop) |
1791 | goto out_err; | |
1792 | ||
1793 | if ( (fw_size != SPIDER_NET_FIRMWARE_LEN) && | |
1794 | netif_msg_probe(card) ) { | |
1795 | pr_err("Incorrect size of spidernet firmware in " \ | |
1796 | "host firmware\n"); | |
1797 | goto done; | |
aaec0fab JO |
1798 | } |
1799 | ||
11f1a52b | 1800 | err = spider_net_download_firmware(card, fw_prop); |
aaec0fab | 1801 | |
11f1a52b AB |
1802 | done: |
1803 | return err; | |
1804 | out_err: | |
1805 | if (netif_msg_probe(card)) | |
1806 | pr_err("Couldn't find spidernet firmware in filesystem " \ | |
1807 | "or host firmware\n"); | |
aaec0fab JO |
1808 | return err; |
1809 | } | |
1810 | ||
1811 | /** | |
1812 | * spider_net_workaround_rxramfull - work around firmware bug | |
1813 | * @card: card structure | |
1814 | * | |
1815 | * no return value | |
1816 | **/ | |
1817 | static void | |
1818 | spider_net_workaround_rxramfull(struct spider_net_card *card) | |
1819 | { | |
1820 | int i, sequencer = 0; | |
1821 | ||
1822 | /* cancel reset */ | |
1823 | spider_net_write_reg(card, SPIDER_NET_CKRCTRL, | |
1824 | SPIDER_NET_CKRCTRL_RUN_VALUE); | |
1825 | ||
1826 | /* empty sequencer data */ | |
11f1a52b AB |
1827 | for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS; |
1828 | sequencer++) { | |
ee962a5c | 1829 | spider_net_write_reg(card, SPIDER_NET_GSnPRGADR + |
aaec0fab | 1830 | sequencer * 8, 0x0); |
11f1a52b | 1831 | for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) { |
aaec0fab JO |
1832 | spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT + |
1833 | sequencer * 8, 0x0); | |
1834 | } | |
1835 | } | |
1836 | ||
1837 | /* set sequencer operation */ | |
1838 | spider_net_write_reg(card, SPIDER_NET_GSINIT, 0x000000fe); | |
1839 | ||
1840 | /* reset */ | |
1841 | spider_net_write_reg(card, SPIDER_NET_CKRCTRL, | |
1842 | SPIDER_NET_CKRCTRL_STOP_VALUE); | |
1843 | } | |
1844 | ||
bdd01503 JO |
1845 | /** |
1846 | * spider_net_stop - called upon ifconfig down | |
1847 | * @netdev: interface device structure | |
1848 | * | |
1849 | * always returns 0 | |
1850 | */ | |
1851 | int | |
1852 | spider_net_stop(struct net_device *netdev) | |
1853 | { | |
1854 | struct spider_net_card *card = netdev_priv(netdev); | |
1855 | ||
bdd01503 JO |
1856 | netif_poll_disable(netdev); |
1857 | netif_carrier_off(netdev); | |
1858 | netif_stop_queue(netdev); | |
1859 | del_timer_sync(&card->tx_timer); | |
1860 | ||
1861 | /* disable/mask all interrupts */ | |
1862 | spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, 0); | |
1863 | spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK, 0); | |
1864 | spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK, 0); | |
1865 | ||
1866 | /* free_irq(netdev->irq, netdev);*/ | |
1867 | free_irq(to_pci_dev(netdev->class_dev.dev)->irq, netdev); | |
1868 | ||
1869 | spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR, | |
1870 | SPIDER_NET_DMA_TX_FEND_VALUE); | |
1871 | ||
1872 | /* turn off DMA, force end */ | |
1873 | spider_net_disable_rxdmac(card); | |
1874 | ||
1875 | /* release chains */ | |
9cc7bf7e | 1876 | spider_net_release_tx_chain(card, 1); |
d4ed8f8d | 1877 | spider_net_free_rx_chain_contents(card); |
bdd01503 | 1878 | |
a55eb05a JO |
1879 | spider_net_free_rx_chain_contents(card); |
1880 | ||
bdd01503 JO |
1881 | spider_net_free_chain(card, &card->tx_chain); |
1882 | spider_net_free_chain(card, &card->rx_chain); | |
1883 | ||
1884 | return 0; | |
1885 | } | |
1886 | ||
aaec0fab JO |
1887 | /** |
1888 | * spider_net_tx_timeout_task - task scheduled by the watchdog timeout | |
1889 | * function (to be called not under interrupt status) | |
1890 | * @data: data, is interface device structure | |
1891 | * | |
1892 | * called as task when tx hangs, resets interface (if interface is up) | |
1893 | */ | |
1894 | static void | |
c4028958 | 1895 | spider_net_tx_timeout_task(struct work_struct *work) |
aaec0fab | 1896 | { |
c4028958 DH |
1897 | struct spider_net_card *card = |
1898 | container_of(work, struct spider_net_card, tx_timeout_task); | |
1899 | struct net_device *netdev = card->netdev; | |
aaec0fab JO |
1900 | |
1901 | if (!(netdev->flags & IFF_UP)) | |
1902 | goto out; | |
1903 | ||
1904 | netif_device_detach(netdev); | |
1905 | spider_net_stop(netdev); | |
1906 | ||
1907 | spider_net_workaround_rxramfull(card); | |
1908 | spider_net_init_card(card); | |
1909 | ||
1910 | if (spider_net_setup_phy(card)) | |
1911 | goto out; | |
1912 | if (spider_net_init_firmware(card)) | |
1913 | goto out; | |
1914 | ||
1915 | spider_net_open(netdev); | |
bdd01503 | 1916 | spider_net_kick_tx_dma(card); |
aaec0fab JO |
1917 | netif_device_attach(netdev); |
1918 | ||
1919 | out: | |
1920 | atomic_dec(&card->tx_timeout_task_counter); | |
1921 | } | |
1922 | ||
1923 | /** | |
1924 | * spider_net_tx_timeout - called when the tx timeout watchdog kicks in. | |
1925 | * @netdev: interface device structure | |
1926 | * | |
1927 | * called, if tx hangs. Schedules a task that resets the interface | |
1928 | */ | |
1929 | static void | |
1930 | spider_net_tx_timeout(struct net_device *netdev) | |
1931 | { | |
1932 | struct spider_net_card *card; | |
1933 | ||
1934 | card = netdev_priv(netdev); | |
1935 | atomic_inc(&card->tx_timeout_task_counter); | |
1936 | if (netdev->flags & IFF_UP) | |
1937 | schedule_work(&card->tx_timeout_task); | |
1938 | else | |
1939 | atomic_dec(&card->tx_timeout_task_counter); | |
9b6b0b81 | 1940 | card->spider_stats.tx_timeouts++; |
aaec0fab JO |
1941 | } |
1942 | ||
1943 | /** | |
1944 | * spider_net_setup_netdev_ops - initialization of net_device operations | |
1945 | * @netdev: net_device structure | |
1946 | * | |
1947 | * fills out function pointers in the net_device structure | |
1948 | */ | |
1949 | static void | |
1950 | spider_net_setup_netdev_ops(struct net_device *netdev) | |
1951 | { | |
1952 | netdev->open = &spider_net_open; | |
1953 | netdev->stop = &spider_net_stop; | |
1954 | netdev->hard_start_xmit = &spider_net_xmit; | |
1955 | netdev->get_stats = &spider_net_get_stats; | |
1956 | netdev->set_multicast_list = &spider_net_set_multi; | |
1957 | netdev->set_mac_address = &spider_net_set_mac; | |
1958 | netdev->change_mtu = &spider_net_change_mtu; | |
1959 | netdev->do_ioctl = &spider_net_do_ioctl; | |
1960 | /* tx watchdog */ | |
1961 | netdev->tx_timeout = &spider_net_tx_timeout; | |
1962 | netdev->watchdog_timeo = SPIDER_NET_WATCHDOG_TIMEOUT; | |
1963 | /* NAPI */ | |
1964 | netdev->poll = &spider_net_poll; | |
1965 | netdev->weight = SPIDER_NET_NAPI_WEIGHT; | |
1966 | /* HW VLAN */ | |
1967 | netdev->vlan_rx_register = &spider_net_vlan_rx_reg; | |
1968 | netdev->vlan_rx_add_vid = &spider_net_vlan_rx_add; | |
1969 | netdev->vlan_rx_kill_vid = &spider_net_vlan_rx_kill; | |
1970 | #ifdef CONFIG_NET_POLL_CONTROLLER | |
1971 | /* poll controller */ | |
1972 | netdev->poll_controller = &spider_net_poll_controller; | |
1973 | #endif /* CONFIG_NET_POLL_CONTROLLER */ | |
1974 | /* ethtool ops */ | |
1975 | netdev->ethtool_ops = &spider_net_ethtool_ops; | |
1976 | } | |
1977 | ||
1978 | /** | |
1979 | * spider_net_setup_netdev - initialization of net_device | |
1980 | * @card: card structure | |
1981 | * | |
1982 | * Returns 0 on success or <0 on failure | |
1983 | * | |
1984 | * spider_net_setup_netdev initializes the net_device structure | |
1985 | **/ | |
1986 | static int | |
1987 | spider_net_setup_netdev(struct spider_net_card *card) | |
1988 | { | |
1989 | int result; | |
1990 | struct net_device *netdev = card->netdev; | |
1991 | struct device_node *dn; | |
1992 | struct sockaddr addr; | |
1a2509c9 | 1993 | const u8 *mac; |
aaec0fab JO |
1994 | |
1995 | SET_MODULE_OWNER(netdev); | |
1996 | SET_NETDEV_DEV(netdev, &card->pdev->dev); | |
1997 | ||
1998 | pci_set_drvdata(card->pdev, netdev); | |
11f1a52b | 1999 | |
11f1a52b AB |
2000 | init_timer(&card->tx_timer); |
2001 | card->tx_timer.function = | |
2002 | (void (*)(unsigned long)) spider_net_cleanup_tx_ring; | |
2003 | card->tx_timer.data = (unsigned long) card; | |
aaec0fab JO |
2004 | netdev->irq = card->pdev->irq; |
2005 | ||
2006 | card->options.rx_csum = SPIDER_NET_RX_CSUM_DEFAULT; | |
2007 | ||
d4ed8f8d LV |
2008 | card->tx_chain.num_desc = tx_descriptors; |
2009 | card->rx_chain.num_desc = rx_descriptors; | |
b68a60e5 | 2010 | |
aaec0fab JO |
2011 | spider_net_setup_netdev_ops(netdev); |
2012 | ||
bdd01503 | 2013 | netdev->features = NETIF_F_HW_CSUM | NETIF_F_LLTX; |
aaec0fab JO |
2014 | /* some time: NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX | |
2015 | * NETIF_F_HW_VLAN_FILTER */ | |
2016 | ||
2017 | netdev->irq = card->pdev->irq; | |
2018 | ||
2019 | dn = pci_device_to_OF_node(card->pdev); | |
543cec51 JO |
2020 | if (!dn) |
2021 | return -EIO; | |
2022 | ||
1a2509c9 | 2023 | mac = get_property(dn, "local-mac-address", NULL); |
543cec51 JO |
2024 | if (!mac) |
2025 | return -EIO; | |
aaec0fab JO |
2026 | memcpy(addr.sa_data, mac, ETH_ALEN); |
2027 | ||
2028 | result = spider_net_set_mac(netdev, &addr); | |
2029 | if ((result) && (netif_msg_probe(card))) | |
2030 | pr_err("Failed to set MAC address: %i\n", result); | |
2031 | ||
2032 | result = register_netdev(netdev); | |
2033 | if (result) { | |
2034 | if (netif_msg_probe(card)) | |
2035 | pr_err("Couldn't register net_device: %i\n", | |
2036 | result); | |
2037 | return result; | |
2038 | } | |
2039 | ||
2040 | if (netif_msg_probe(card)) | |
2041 | pr_info("Initialized device %s.\n", netdev->name); | |
2042 | ||
2043 | return 0; | |
2044 | } | |
2045 | ||
2046 | /** | |
2047 | * spider_net_alloc_card - allocates net_device and card structure | |
2048 | * | |
2049 | * returns the card structure or NULL in case of errors | |
2050 | * | |
2051 | * the card and net_device structures are linked to each other | |
2052 | */ | |
2053 | static struct spider_net_card * | |
2054 | spider_net_alloc_card(void) | |
2055 | { | |
2056 | struct net_device *netdev; | |
2057 | struct spider_net_card *card; | |
aaec0fab | 2058 | |
d4ed8f8d | 2059 | netdev = alloc_etherdev(sizeof(struct spider_net_card)); |
aaec0fab JO |
2060 | if (!netdev) |
2061 | return NULL; | |
2062 | ||
2063 | card = netdev_priv(netdev); | |
2064 | card->netdev = netdev; | |
2065 | card->msg_enable = SPIDER_NET_DEFAULT_MSG; | |
c4028958 | 2066 | INIT_WORK(&card->tx_timeout_task, spider_net_tx_timeout_task); |
aaec0fab JO |
2067 | init_waitqueue_head(&card->waitq); |
2068 | atomic_set(&card->tx_timeout_task_counter, 0); | |
2069 | ||
2070 | return card; | |
2071 | } | |
2072 | ||
2073 | /** | |
2074 | * spider_net_undo_pci_setup - releases PCI ressources | |
2075 | * @card: card structure | |
2076 | * | |
2077 | * spider_net_undo_pci_setup releases the mapped regions | |
2078 | */ | |
2079 | static void | |
2080 | spider_net_undo_pci_setup(struct spider_net_card *card) | |
2081 | { | |
2082 | iounmap(card->regs); | |
2083 | pci_release_regions(card->pdev); | |
2084 | } | |
2085 | ||
2086 | /** | |
2087 | * spider_net_setup_pci_dev - sets up the device in terms of PCI operations | |
2088 | * @card: card structure | |
2089 | * @pdev: PCI device | |
2090 | * | |
2091 | * Returns the card structure or NULL if any errors occur | |
2092 | * | |
2093 | * spider_net_setup_pci_dev initializes pdev and together with the | |
2094 | * functions called in spider_net_open configures the device so that | |
2095 | * data can be transferred over it | |
2096 | * The net_device structure is attached to the card structure, if the | |
2097 | * function returns without error. | |
2098 | **/ | |
2099 | static struct spider_net_card * | |
2100 | spider_net_setup_pci_dev(struct pci_dev *pdev) | |
2101 | { | |
2102 | struct spider_net_card *card; | |
2103 | unsigned long mmio_start, mmio_len; | |
2104 | ||
2105 | if (pci_enable_device(pdev)) { | |
2106 | pr_err("Couldn't enable PCI device\n"); | |
2107 | return NULL; | |
2108 | } | |
2109 | ||
2110 | if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) { | |
2111 | pr_err("Couldn't find proper PCI device base address.\n"); | |
2112 | goto out_disable_dev; | |
2113 | } | |
2114 | ||
2115 | if (pci_request_regions(pdev, spider_net_driver_name)) { | |
2116 | pr_err("Couldn't obtain PCI resources, aborting.\n"); | |
2117 | goto out_disable_dev; | |
2118 | } | |
2119 | ||
2120 | pci_set_master(pdev); | |
2121 | ||
2122 | card = spider_net_alloc_card(); | |
2123 | if (!card) { | |
2124 | pr_err("Couldn't allocate net_device structure, " | |
2125 | "aborting.\n"); | |
2126 | goto out_release_regions; | |
2127 | } | |
2128 | card->pdev = pdev; | |
2129 | ||
2130 | /* fetch base address and length of first resource */ | |
2131 | mmio_start = pci_resource_start(pdev, 0); | |
2132 | mmio_len = pci_resource_len(pdev, 0); | |
2133 | ||
2134 | card->netdev->mem_start = mmio_start; | |
2135 | card->netdev->mem_end = mmio_start + mmio_len; | |
2136 | card->regs = ioremap(mmio_start, mmio_len); | |
2137 | ||
2138 | if (!card->regs) { | |
2139 | pr_err("Couldn't obtain PCI resources, aborting.\n"); | |
2140 | goto out_release_regions; | |
2141 | } | |
2142 | ||
2143 | return card; | |
2144 | ||
2145 | out_release_regions: | |
2146 | pci_release_regions(pdev); | |
2147 | out_disable_dev: | |
2148 | pci_disable_device(pdev); | |
2149 | pci_set_drvdata(pdev, NULL); | |
2150 | return NULL; | |
2151 | } | |
2152 | ||
2153 | /** | |
2154 | * spider_net_probe - initialization of a device | |
2155 | * @pdev: PCI device | |
2156 | * @ent: entry in the device id list | |
2157 | * | |
2158 | * Returns 0 on success, <0 on failure | |
2159 | * | |
2160 | * spider_net_probe initializes pdev and registers a net_device | |
2161 | * structure for it. After that, the device can be ifconfig'ed up | |
2162 | **/ | |
2163 | static int __devinit | |
2164 | spider_net_probe(struct pci_dev *pdev, const struct pci_device_id *ent) | |
2165 | { | |
2166 | int err = -EIO; | |
2167 | struct spider_net_card *card; | |
2168 | ||
2169 | card = spider_net_setup_pci_dev(pdev); | |
2170 | if (!card) | |
2171 | goto out; | |
2172 | ||
2173 | spider_net_workaround_rxramfull(card); | |
2174 | spider_net_init_card(card); | |
2175 | ||
2176 | err = spider_net_setup_phy(card); | |
2177 | if (err) | |
2178 | goto out_undo_pci; | |
2179 | ||
2180 | err = spider_net_init_firmware(card); | |
2181 | if (err) | |
2182 | goto out_undo_pci; | |
2183 | ||
2184 | err = spider_net_setup_netdev(card); | |
2185 | if (err) | |
2186 | goto out_undo_pci; | |
2187 | ||
2188 | return 0; | |
2189 | ||
2190 | out_undo_pci: | |
2191 | spider_net_undo_pci_setup(card); | |
2192 | free_netdev(card->netdev); | |
2193 | out: | |
2194 | return err; | |
2195 | } | |
2196 | ||
2197 | /** | |
2198 | * spider_net_remove - removal of a device | |
2199 | * @pdev: PCI device | |
2200 | * | |
2201 | * Returns 0 on success, <0 on failure | |
2202 | * | |
2203 | * spider_net_remove is called to remove the device and unregisters the | |
2204 | * net_device | |
2205 | **/ | |
2206 | static void __devexit | |
2207 | spider_net_remove(struct pci_dev *pdev) | |
2208 | { | |
2209 | struct net_device *netdev; | |
2210 | struct spider_net_card *card; | |
2211 | ||
2212 | netdev = pci_get_drvdata(pdev); | |
2213 | card = netdev_priv(netdev); | |
2214 | ||
2215 | wait_event(card->waitq, | |
2216 | atomic_read(&card->tx_timeout_task_counter) == 0); | |
2217 | ||
2218 | unregister_netdev(netdev); | |
543cec51 JO |
2219 | |
2220 | /* switch off card */ | |
2221 | spider_net_write_reg(card, SPIDER_NET_CKRCTRL, | |
2222 | SPIDER_NET_CKRCTRL_STOP_VALUE); | |
2223 | spider_net_write_reg(card, SPIDER_NET_CKRCTRL, | |
2224 | SPIDER_NET_CKRCTRL_RUN_VALUE); | |
2225 | ||
aaec0fab JO |
2226 | spider_net_undo_pci_setup(card); |
2227 | free_netdev(netdev); | |
aaec0fab JO |
2228 | } |
2229 | ||
2230 | static struct pci_driver spider_net_driver = { | |
aaec0fab JO |
2231 | .name = spider_net_driver_name, |
2232 | .id_table = spider_net_pci_tbl, | |
2233 | .probe = spider_net_probe, | |
2234 | .remove = __devexit_p(spider_net_remove) | |
2235 | }; | |
2236 | ||
2237 | /** | |
2238 | * spider_net_init - init function when the driver is loaded | |
2239 | * | |
2240 | * spider_net_init registers the device driver | |
2241 | */ | |
2242 | static int __init spider_net_init(void) | |
2243 | { | |
90f10841 LV |
2244 | printk(KERN_INFO "Spidernet version %s.\n", VERSION); |
2245 | ||
aaec0fab JO |
2246 | if (rx_descriptors < SPIDER_NET_RX_DESCRIPTORS_MIN) { |
2247 | rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MIN; | |
2248 | pr_info("adjusting rx descriptors to %i.\n", rx_descriptors); | |
2249 | } | |
2250 | if (rx_descriptors > SPIDER_NET_RX_DESCRIPTORS_MAX) { | |
2251 | rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MAX; | |
2252 | pr_info("adjusting rx descriptors to %i.\n", rx_descriptors); | |
2253 | } | |
2254 | if (tx_descriptors < SPIDER_NET_TX_DESCRIPTORS_MIN) { | |
2255 | tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MIN; | |
2256 | pr_info("adjusting tx descriptors to %i.\n", tx_descriptors); | |
2257 | } | |
2258 | if (tx_descriptors > SPIDER_NET_TX_DESCRIPTORS_MAX) { | |
2259 | tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MAX; | |
2260 | pr_info("adjusting tx descriptors to %i.\n", tx_descriptors); | |
2261 | } | |
2262 | ||
2263 | return pci_register_driver(&spider_net_driver); | |
2264 | } | |
2265 | ||
2266 | /** | |
2267 | * spider_net_cleanup - exit function when driver is unloaded | |
2268 | * | |
2269 | * spider_net_cleanup unregisters the device driver | |
2270 | */ | |
2271 | static void __exit spider_net_cleanup(void) | |
2272 | { | |
2273 | pci_unregister_driver(&spider_net_driver); | |
2274 | } | |
2275 | ||
2276 | module_init(spider_net_init); | |
2277 | module_exit(spider_net_cleanup); |