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