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