File: /usr/src/linux/drivers/net/rrunner.c
1 /*
2 * rrunner.c: Linux driver for the Essential RoadRunner HIPPI board.
3 *
4 * Copyright (C) 1998-2000 by Jes Sorensen, <Jes.Sorensen@cern.ch>.
5 *
6 * Thanks to Essential Communication for providing us with hardware
7 * and very comprehensive documentation without which I would not have
8 * been able to write this driver. A special thank you to John Gibbon
9 * for sorting out the legal issues, with the NDA, allowing the code to
10 * be released under the GPL.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * Thanks to Jayaram Bhat from ODS/Essential for fixing some of the
18 * stupid bugs in my code.
19 *
20 * Softnet support and various other patches from Val Henson of
21 * ODS/Essential.
22 */
23
24 #define DEBUG 1
25 #define RX_DMA_SKBUFF 1
26 #define PKT_COPY_THRESHOLD 512
27
28 #include <linux/config.h>
29 #include <linux/module.h>
30 #include <linux/version.h>
31 #include <linux/types.h>
32 #include <linux/errno.h>
33 #include <linux/ioport.h>
34 #include <linux/pci.h>
35 #include <linux/kernel.h>
36 #include <linux/netdevice.h>
37 #include <linux/hippidevice.h>
38 #include <linux/skbuff.h>
39 #include <linux/init.h>
40 #include <linux/delay.h>
41 #include <linux/mm.h>
42 #include <net/sock.h>
43
44 #include <asm/system.h>
45 #include <asm/cache.h>
46 #include <asm/byteorder.h>
47 #include <asm/io.h>
48 #include <asm/irq.h>
49 #include <asm/uaccess.h>
50
51 #if (LINUX_VERSION_CODE < 0x02030e)
52 #define net_device device
53 #endif
54
55 #if (LINUX_VERSION_CODE >= 0x02031b)
56 #define NEW_NETINIT
57 #endif
58
59 #if (LINUX_VERSION_CODE < 0x02032b)
60 /*
61 * SoftNet changes
62 */
63 #define dev_kfree_skb_irq(a) dev_kfree_skb(a)
64 #define netif_wake_queue(dev) clear_bit(0, &dev->tbusy)
65 #define netif_stop_queue(dev) set_bit(0, &dev->tbusy)
66
67 static inline void netif_start_queue(struct net_device *dev)
68 {
69 dev->tbusy = 0;
70 dev->start = 1;
71 }
72
73 #define rr_mark_net_bh(foo) mark_bh(foo)
74 #define rr_if_busy(dev) dev->tbusy
75 #define rr_if_running(dev) dev->start /* Currently unused. */
76 #define rr_if_down(dev) {do{dev->start = 0;}while (0);}
77 #else
78 #define NET_BH 0
79 #define rr_mark_net_bh(foo) {do{} while(0);}
80 #define rr_if_busy(dev) netif_queue_stopped(dev)
81 #define rr_if_running(dev) netif_running(dev)
82 #define rr_if_down(dev) {do{} while(0);}
83 #endif
84
85 #include "rrunner.h"
86
87 #define RUN_AT(x) (jiffies + (x))
88
89
90 /*
91 * Implementation notes:
92 *
93 * The DMA engine only allows for DMA within physical 64KB chunks of
94 * memory. The current approach of the driver (and stack) is to use
95 * linear blocks of memory for the skbuffs. However, as the data block
96 * is always the first part of the skb and skbs are 2^n aligned so we
97 * are guarantted to get the whole block within one 64KB align 64KB
98 * chunk.
99 *
100 * On the long term, relying on being able to allocate 64KB linear
101 * chunks of memory is not feasible and the skb handling code and the
102 * stack will need to know about I/O vectors or something similar.
103 */
104
105 static char version[] __initdata = "rrunner.c: v0.22 03/01/2000 Jes Sorensen (Jes.Sorensen@cern.ch)\n";
106
107 static struct net_device *root_dev;
108
109
110 /*
111 * These are checked at init time to see if they are at least 256KB
112 * and increased to 256KB if they are not. This is done to avoid ending
113 * up with socket buffers smaller than the MTU size,
114 */
115 extern __u32 sysctl_wmem_max;
116 extern __u32 sysctl_rmem_max;
117
118 static int probed __initdata = 0;
119
120 #if LINUX_VERSION_CODE >= 0x20400
121 static struct pci_device_id rrunner_pci_tbl[] __initdata = {
122 { PCI_VENDOR_ID_ESSENTIAL, PCI_DEVICE_ID_ESSENTIAL_ROADRUNNER, PCI_ANY_ID, PCI_ANY_ID, },
123 { } /* Terminating entry */
124 };
125 MODULE_DEVICE_TABLE(pci, rrunner_pci_tbl);
126 #endif /* LINUX_VERSION_CODE >= 0x20400 */
127
128 #ifdef NEW_NETINIT
129 int __init rr_hippi_probe (void)
130 #else
131 int __init rr_hippi_probe (struct net_device *dev)
132 #endif
133 {
134 #ifdef NEW_NETINIT
135 struct net_device *dev;
136 #endif
137 int boards_found = 0;
138 int version_disp; /* was version info already displayed? */
139 struct pci_dev *pdev = NULL;
140 struct pci_dev *opdev = NULL;
141 u8 pci_latency;
142 struct rr_private *rrpriv;
143
144 if (probed)
145 return -ENODEV;
146 probed++;
147
148 version_disp = 0;
149
150 while((pdev = pci_find_device(PCI_VENDOR_ID_ESSENTIAL,
151 PCI_DEVICE_ID_ESSENTIAL_ROADRUNNER,
152 pdev)))
153 {
154 if (pci_enable_device(pdev))
155 continue;
156
157 if (pdev == opdev)
158 return 0;
159
160 /*
161 * So we found our HIPPI ... time to tell the system.
162 */
163
164 dev = init_hippi_dev(NULL, sizeof(struct rr_private));
165
166 if (!dev)
167 break;
168
169 if (!dev->priv)
170 dev->priv = kmalloc(sizeof(*rrpriv), GFP_KERNEL);
171
172 if (!dev->priv)
173 return -ENOMEM;
174
175 rrpriv = (struct rr_private *)dev->priv;
176 memset(rrpriv, 0, sizeof(*rrpriv));
177
178 #ifdef CONFIG_SMP
179 spin_lock_init(&rrpriv->lock);
180 #endif
181 sprintf(rrpriv->name, "RoadRunner serial HIPPI");
182
183 dev->irq = pdev->irq;
184 SET_MODULE_OWNER(dev);
185 dev->open = &rr_open;
186 dev->hard_start_xmit = &rr_start_xmit;
187 dev->stop = &rr_close;
188 dev->get_stats = &rr_get_stats;
189 dev->do_ioctl = &rr_ioctl;
190
191 #if (LINUX_VERSION_CODE < 0x02030d)
192 dev->base_addr = pdev->base_address[0];
193 #else
194 dev->base_addr = pdev->resource[0].start;
195 #endif
196
197 /* display version info if adapter is found */
198 if (!version_disp)
199 {
200 /* set display flag to TRUE so that */
201 /* we only display this string ONCE */
202 version_disp = 1;
203 printk(version);
204 }
205
206 pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &pci_latency);
207 if (pci_latency <= 0x58){
208 pci_latency = 0x58;
209 pci_write_config_byte(pdev, PCI_LATENCY_TIMER,
210 pci_latency);
211 }
212
213 pci_set_master(pdev);
214
215 printk(KERN_INFO "%s: Essential RoadRunner serial HIPPI "
216 "at 0x%08lx, irq %i, PCI latency %i\n", dev->name,
217 dev->base_addr, dev->irq, pci_latency);
218
219 /*
220 * Remap the regs into kernel space.
221 */
222
223 rrpriv->regs = (struct rr_regs *)
224 ioremap(dev->base_addr, 0x1000);
225
226 if (!rrpriv->regs){
227 printk(KERN_ERR "%s: Unable to map I/O register, "
228 "RoadRunner %i will be disabled.\n",
229 dev->name, boards_found);
230 break;
231 }
232
233 /*
234 * Don't access any registes before this point!
235 */
236 #ifdef __BIG_ENDIAN
237 writel(readl(®s->HostCtrl) | NO_SWAP, ®s->HostCtrl);
238 #endif
239 /*
240 * Need to add a case for little-endian 64-bit hosts here.
241 */
242
243 rr_init(dev);
244
245 boards_found++;
246 dev->base_addr = 0;
247 dev = NULL;
248 opdev = pdev;
249 }
250
251 /*
252 * If we're at this point we're going through rr_hippi_probe()
253 * for the first time. Return success (0) if we've initialized
254 * 1 or more boards. Otherwise, return failure (-ENODEV).
255 */
256
257 #ifdef MODULE
258 return boards_found;
259 #else
260 if (boards_found > 0)
261 return 0;
262 else
263 return -ENODEV;
264 #endif
265 }
266
267
268 #ifdef MODULE
269 #if LINUX_VERSION_CODE > 0x20118
270 MODULE_AUTHOR("Jes Sorensen <Jes.Sorensen@cern.ch>");
271 MODULE_DESCRIPTION("Essential RoadRunner HIPPI driver");
272 #endif
273
274 int init_module(void)
275 {
276 int cards;
277
278 root_dev = NULL;
279
280 #ifdef NEW_NETINIT
281 cards = rr_hippi_probe();
282 #else
283 cards = rr_hippi_probe(NULL);
284 #endif
285 return cards ? 0 : -ENODEV;
286 }
287
288 void cleanup_module(void)
289 {
290 struct rr_private *rr;
291 struct net_device *next;
292
293 while (root_dev) {
294 next = ((struct rr_private *)root_dev->priv)->next;
295 rr = (struct rr_private *)root_dev->priv;
296
297 if (!(readl(&rr->regs->HostCtrl) & NIC_HALTED)){
298 printk(KERN_ERR "%s: trying to unload running NIC\n",
299 root_dev->name);
300 writel(HALT_NIC, &rr->regs->HostCtrl);
301 }
302
303 iounmap(rr->regs);
304 unregister_hipdev(root_dev);
305 kfree(root_dev);
306
307 root_dev = next;
308 }
309 }
310 #endif
311
312
313 /*
314 * Commands are considered to be slow, thus there is no reason to
315 * inline this.
316 */
317 static void rr_issue_cmd(struct rr_private *rrpriv, struct cmd *cmd)
318 {
319 struct rr_regs *regs;
320 u32 idx;
321
322 regs = rrpriv->regs;
323 /*
324 * This is temporary - it will go away in the final version.
325 * We probably also want to make this function inline.
326 */
327 if (readl(®s->HostCtrl) & NIC_HALTED){
328 printk("issuing command for halted NIC, code 0x%x, "
329 "HostCtrl %08x\n", cmd->code, readl(®s->HostCtrl));
330 if (readl(®s->Mode) & FATAL_ERR)
331 printk("error codes Fail1 %02x, Fail2 %02x\n",
332 readl(®s->Fail1), readl(®s->Fail2));
333 }
334
335 idx = rrpriv->info->cmd_ctrl.pi;
336
337 writel(*(u32*)(cmd), ®s->CmdRing[idx]);
338 wmb();
339
340 idx = (idx - 1) % CMD_RING_ENTRIES;
341 rrpriv->info->cmd_ctrl.pi = idx;
342 wmb();
343
344 if (readl(®s->Mode) & FATAL_ERR)
345 printk("error code %02x\n", readl(®s->Fail1));
346 }
347
348
349 /*
350 * Reset the board in a sensible manner. The NIC is already halted
351 * when we get here and a spin-lock is held.
352 */
353 static int rr_reset(struct net_device *dev)
354 {
355 struct rr_private *rrpriv;
356 struct rr_regs *regs;
357 struct eeprom *hw = NULL;
358 u32 start_pc;
359 int i;
360
361 rrpriv = (struct rr_private *)dev->priv;
362 regs = rrpriv->regs;
363
364 rr_load_firmware(dev);
365
366 writel(0x01000000, ®s->TX_state);
367 writel(0xff800000, ®s->RX_state);
368 writel(0, ®s->AssistState);
369 writel(CLEAR_INTA, ®s->LocalCtrl);
370 writel(0x01, ®s->BrkPt);
371 writel(0, ®s->Timer);
372 writel(0, ®s->TimerRef);
373 writel(RESET_DMA, ®s->DmaReadState);
374 writel(RESET_DMA, ®s->DmaWriteState);
375 writel(0, ®s->DmaWriteHostHi);
376 writel(0, ®s->DmaWriteHostLo);
377 writel(0, ®s->DmaReadHostHi);
378 writel(0, ®s->DmaReadHostLo);
379 writel(0, ®s->DmaReadLen);
380 writel(0, ®s->DmaWriteLen);
381 writel(0, ®s->DmaWriteLcl);
382 writel(0, ®s->DmaWriteIPchecksum);
383 writel(0, ®s->DmaReadLcl);
384 writel(0, ®s->DmaReadIPchecksum);
385 writel(0, ®s->PciState);
386 #if (BITS_PER_LONG == 64) && defined __LITTLE_ENDIAN
387 writel(SWAP_DATA | PTR64BIT | PTR_WD_SWAP, ®s->Mode);
388 #elif (BITS_PER_LONG == 64)
389 writel(SWAP_DATA | PTR64BIT | PTR_WD_NOSWAP, ®s->Mode);
390 #else
391 writel(SWAP_DATA | PTR32BIT | PTR_WD_NOSWAP, ®s->Mode);
392 #endif
393
394 #if 0
395 /*
396 * Don't worry, this is just black magic.
397 */
398 writel(0xdf000, ®s->RxBase);
399 writel(0xdf000, ®s->RxPrd);
400 writel(0xdf000, ®s->RxCon);
401 writel(0xce000, ®s->TxBase);
402 writel(0xce000, ®s->TxPrd);
403 writel(0xce000, ®s->TxCon);
404 writel(0, ®s->RxIndPro);
405 writel(0, ®s->RxIndCon);
406 writel(0, ®s->RxIndRef);
407 writel(0, ®s->TxIndPro);
408 writel(0, ®s->TxIndCon);
409 writel(0, ®s->TxIndRef);
410 writel(0xcc000, ®s->pad10[0]);
411 writel(0, ®s->DrCmndPro);
412 writel(0, ®s->DrCmndCon);
413 writel(0, ®s->DwCmndPro);
414 writel(0, ®s->DwCmndCon);
415 writel(0, ®s->DwCmndRef);
416 writel(0, ®s->DrDataPro);
417 writel(0, ®s->DrDataCon);
418 writel(0, ®s->DrDataRef);
419 writel(0, ®s->DwDataPro);
420 writel(0, ®s->DwDataCon);
421 writel(0, ®s->DwDataRef);
422 #endif
423
424 writel(0xffffffff, ®s->MbEvent);
425 writel(0, ®s->Event);
426
427 writel(0, ®s->TxPi);
428 writel(0, ®s->IpRxPi);
429
430 writel(0, ®s->EvtCon);
431 writel(0, ®s->EvtPrd);
432
433 rrpriv->info->evt_ctrl.pi = 0;
434
435 for (i = 0; i < CMD_RING_ENTRIES; i++)
436 writel(0, ®s->CmdRing[i]);
437
438 /*
439 * Why 32 ? is this not cache line size dependant?
440 */
441 writel(RBURST_64|WBURST_64, ®s->PciState);
442 wmb();
443
444 start_pc = rr_read_eeprom_word(rrpriv, &hw->rncd_info.FwStart);
445
446 #if (DEBUG > 1)
447 printk("%s: Executing firmware at address 0x%06x\n",
448 dev->name, start_pc);
449 #endif
450
451 writel(start_pc + 0x800, ®s->Pc);
452 wmb();
453 udelay(5);
454
455 writel(start_pc, ®s->Pc);
456 wmb();
457
458 return 0;
459 }
460
461
462 /*
463 * Read a string from the EEPROM.
464 */
465 static unsigned int rr_read_eeprom(struct rr_private *rrpriv,
466 unsigned long offset,
467 unsigned char *buf,
468 unsigned long length)
469 {
470 struct rr_regs *regs = rrpriv->regs;
471 u32 misc, io, host, i;
472
473 io = readl(®s->ExtIo);
474 writel(0, ®s->ExtIo);
475 misc = readl(®s->LocalCtrl);
476 writel(0, ®s->LocalCtrl);
477 host = readl(®s->HostCtrl);
478 writel(host | HALT_NIC, ®s->HostCtrl);
479 mb();
480
481 for (i = 0; i < length; i++){
482 writel((EEPROM_BASE + ((offset+i) << 3)), ®s->WinBase);
483 mb();
484 buf[i] = (readl(®s->WinData) >> 24) & 0xff;
485 mb();
486 }
487
488 writel(host, ®s->HostCtrl);
489 writel(misc, ®s->LocalCtrl);
490 writel(io, ®s->ExtIo);
491 mb();
492 return i;
493 }
494
495
496 /*
497 * Shortcut to read one word (4 bytes) out of the EEPROM and convert
498 * it to our CPU byte-order.
499 */
500 static u32 rr_read_eeprom_word(struct rr_private *rrpriv,
501 void * offset)
502 {
503 u32 word;
504
505 if ((rr_read_eeprom(rrpriv, (unsigned long)offset,
506 (char *)&word, 4) == 4))
507 return be32_to_cpu(word);
508 return 0;
509 }
510
511
512 /*
513 * Write a string to the EEPROM.
514 *
515 * This is only called when the firmware is not running.
516 */
517 static unsigned int write_eeprom(struct rr_private *rrpriv,
518 unsigned long offset,
519 unsigned char *buf,
520 unsigned long length)
521 {
522 struct rr_regs *regs = rrpriv->regs;
523 u32 misc, io, data, i, j, ready, error = 0;
524
525 io = readl(®s->ExtIo);
526 writel(0, ®s->ExtIo);
527 misc = readl(®s->LocalCtrl);
528 writel(ENABLE_EEPROM_WRITE, ®s->LocalCtrl);
529 mb();
530
531 for (i = 0; i < length; i++){
532 writel((EEPROM_BASE + ((offset+i) << 3)), ®s->WinBase);
533 mb();
534 data = buf[i] << 24;
535 /*
536 * Only try to write the data if it is not the same
537 * value already.
538 */
539 if ((readl(®s->WinData) & 0xff000000) != data){
540 writel(data, ®s->WinData);
541 ready = 0;
542 j = 0;
543 mb();
544 while(!ready){
545 udelay(20);
546 if ((readl(®s->WinData) & 0xff000000) ==
547 data)
548 ready = 1;
549 mb();
550 if (j++ > 5000){
551 printk("data mismatch: %08x, "
552 "WinData %08x\n", data,
553 readl(®s->WinData));
554 ready = 1;
555 error = 1;
556 }
557 }
558 }
559 }
560
561 writel(misc, ®s->LocalCtrl);
562 writel(io, ®s->ExtIo);
563 mb();
564
565 return error;
566 }
567
568
569 static int __init rr_init(struct net_device *dev)
570 {
571 struct rr_private *rrpriv;
572 struct rr_regs *regs;
573 struct eeprom *hw = NULL;
574 u32 sram_size, rev;
575 int i;
576
577 rrpriv = (struct rr_private *)dev->priv;
578 regs = rrpriv->regs;
579
580 rev = readl(®s->FwRev);
581 rrpriv->fw_rev = rev;
582 if (rev > 0x00020024)
583 printk(" Firmware revision: %i.%i.%i\n", (rev >> 16),
584 ((rev >> 8) & 0xff), (rev & 0xff));
585 else if (rev >= 0x00020000) {
586 printk(" Firmware revision: %i.%i.%i (2.0.37 or "
587 "later is recommended)\n", (rev >> 16),
588 ((rev >> 8) & 0xff), (rev & 0xff));
589 }else{
590 printk(" Firmware revision too old: %i.%i.%i, please "
591 "upgrade to 2.0.37 or later.\n",
592 (rev >> 16), ((rev >> 8) & 0xff), (rev & 0xff));
593 }
594
595 #if (DEBUG > 2)
596 printk(" Maximum receive rings %i\n", readl(®s->MaxRxRng));
597 #endif
598
599 /*
600 * Read the hardware address from the eeprom. The HW address
601 * is not really necessary for HIPPI but awfully convenient.
602 * The pointer arithmetic to put it in dev_addr is ugly, but
603 * Donald Becker does it this way for the GigE version of this
604 * card and it's shorter and more portable than any
605 * other method I've seen. -VAL
606 */
607
608 *(u16 *)(dev->dev_addr) =
609 htons(rr_read_eeprom_word(rrpriv, &hw->manf.BoardULA));
610 *(u32 *)(dev->dev_addr+2) =
611 htonl(rr_read_eeprom_word(rrpriv, &hw->manf.BoardULA[4]));
612
613 printk(" MAC: ");
614
615 for (i = 0; i < 5; i++)
616 printk("%2.2x:", dev->dev_addr[i]);
617 printk("%2.2x\n", dev->dev_addr[i]);
618
619 sram_size = rr_read_eeprom_word(rrpriv, (void *)8);
620 printk(" SRAM size 0x%06x\n", sram_size);
621
622 if (sysctl_rmem_max < 262144){
623 printk(" Receive socket buffer limit too low (%i), "
624 "setting to 262144\n", sysctl_rmem_max);
625 sysctl_rmem_max = 262144;
626 }
627
628 if (sysctl_wmem_max < 262144){
629 printk(" Transmit socket buffer limit too low (%i), "
630 "setting to 262144\n", sysctl_wmem_max);
631 sysctl_wmem_max = 262144;
632 }
633
634 rrpriv->next = root_dev;
635 root_dev = dev;
636
637 return 0;
638 }
639
640
641 static int rr_init1(struct net_device *dev)
642 {
643 struct rr_private *rrpriv;
644 struct rr_regs *regs;
645 unsigned long myjif, flags;
646 struct cmd cmd;
647 u32 hostctrl;
648 int ecode = 0;
649 short i;
650
651 rrpriv = (struct rr_private *)dev->priv;
652 regs = rrpriv->regs;
653
654 spin_lock_irqsave(&rrpriv->lock, flags);
655
656 hostctrl = readl(®s->HostCtrl);
657 writel(hostctrl | HALT_NIC | RR_CLEAR_INT, ®s->HostCtrl);
658 wmb();
659
660 if (hostctrl & PARITY_ERR){
661 printk("%s: Parity error halting NIC - this is serious!\n",
662 dev->name);
663 spin_unlock_irqrestore(&rrpriv->lock, flags);
664 ecode = -EFAULT;
665 goto error;
666 }
667
668 set_rxaddr(regs, rrpriv->rx_ctrl);
669 set_infoaddr(regs, rrpriv->info);
670
671 rrpriv->info->evt_ctrl.entry_size = sizeof(struct event);
672 rrpriv->info->evt_ctrl.entries = EVT_RING_ENTRIES;
673 rrpriv->info->evt_ctrl.mode = 0;
674 rrpriv->info->evt_ctrl.pi = 0;
675 set_rraddr(&rrpriv->info->evt_ctrl.rngptr, rrpriv->evt_ring);
676
677 rrpriv->info->cmd_ctrl.entry_size = sizeof(struct cmd);
678 rrpriv->info->cmd_ctrl.entries = CMD_RING_ENTRIES;
679 rrpriv->info->cmd_ctrl.mode = 0;
680 rrpriv->info->cmd_ctrl.pi = 15;
681
682 for (i = 0; i < CMD_RING_ENTRIES; i++) {
683 writel(0, ®s->CmdRing[i]);
684 }
685
686 for (i = 0; i < TX_RING_ENTRIES; i++) {
687 rrpriv->tx_ring[i].size = 0;
688 set_rraddr(&rrpriv->tx_ring[i].addr, 0);
689 rrpriv->tx_skbuff[i] = 0;
690 }
691 rrpriv->info->tx_ctrl.entry_size = sizeof(struct tx_desc);
692 rrpriv->info->tx_ctrl.entries = TX_RING_ENTRIES;
693 rrpriv->info->tx_ctrl.mode = 0;
694 rrpriv->info->tx_ctrl.pi = 0;
695 set_rraddr(&rrpriv->info->tx_ctrl.rngptr, rrpriv->tx_ring);
696
697 /*
698 * Set dirty_tx before we start receiving interrupts, otherwise
699 * the interrupt handler might think it is supposed to process
700 * tx ints before we are up and running, which may cause a null
701 * pointer access in the int handler.
702 */
703 rrpriv->tx_full = 0;
704 rrpriv->cur_rx = 0;
705 rrpriv->dirty_rx = rrpriv->dirty_tx = 0;
706
707 rr_reset(dev);
708
709 /* Tuning values */
710 writel(0x5000, ®s->ConRetry);
711 writel(0x100, ®s->ConRetryTmr);
712 writel(0x500000, ®s->ConTmout);
713 writel(0x60, ®s->IntrTmr);
714 writel(0x500000, ®s->TxDataMvTimeout);
715 writel(0x200000, ®s->RxDataMvTimeout);
716 writel(0x80, ®s->WriteDmaThresh);
717 writel(0x80, ®s->ReadDmaThresh);
718
719 rrpriv->fw_running = 0;
720 wmb();
721
722 hostctrl &= ~(HALT_NIC | INVALID_INST_B | PARITY_ERR);
723 writel(hostctrl, ®s->HostCtrl);
724 wmb();
725
726 spin_unlock_irqrestore(&rrpriv->lock, flags);
727
728 for (i = 0; i < RX_RING_ENTRIES; i++) {
729 struct sk_buff *skb;
730
731 rrpriv->rx_ring[i].mode = 0;
732 skb = alloc_skb(dev->mtu + HIPPI_HLEN, GFP_ATOMIC);
733 if (!skb) {
734 printk(KERN_WARNING "%s: Unable to allocate memory "
735 "for receive ring - halting NIC\n", dev->name);
736 ecode = -ENOMEM;
737 goto error;
738 }
739 rrpriv->rx_skbuff[i] = skb;
740 /*
741 * Sanity test to see if we conflict with the DMA
742 * limitations of the Roadrunner.
743 */
744 if ((((unsigned long)skb->data) & 0xfff) > ~65320)
745 printk("skb alloc error\n");
746
747 set_rraddr(&rrpriv->rx_ring[i].addr, skb->data);
748 rrpriv->rx_ring[i].size = dev->mtu + HIPPI_HLEN;
749 }
750
751 rrpriv->rx_ctrl[4].entry_size = sizeof(struct rx_desc);
752 rrpriv->rx_ctrl[4].entries = RX_RING_ENTRIES;
753 rrpriv->rx_ctrl[4].mode = 8;
754 rrpriv->rx_ctrl[4].pi = 0;
755 wmb();
756 set_rraddr(&rrpriv->rx_ctrl[4].rngptr, rrpriv->rx_ring);
757
758 udelay(1000);
759
760 /*
761 * Now start the FirmWare.
762 */
763 cmd.code = C_START_FW;
764 cmd.ring = 0;
765 cmd.index = 0;
766
767 rr_issue_cmd(rrpriv, &cmd);
768
769 /*
770 * Give the FirmWare time to chew on the `get running' command.
771 */
772 myjif = jiffies + 5 * HZ;
773 while ((jiffies < myjif) && !rrpriv->fw_running);
774
775 netif_start_queue(dev);
776
777 return ecode;
778
779 error:
780 /*
781 * We might have gotten here because we are out of memory,
782 * make sure we release everything we allocated before failing
783 */
784 for (i = 0; i < RX_RING_ENTRIES; i++) {
785 if (rrpriv->rx_skbuff[i]) {
786 rrpriv->rx_ring[i].size = 0;
787 set_rraddr(&rrpriv->rx_ring[i].addr, 0);
788 dev_kfree_skb(rrpriv->rx_skbuff[i]);
789 }
790 }
791 return ecode;
792 }
793
794
795 /*
796 * All events are considered to be slow (RX/TX ints do not generate
797 * events) and are handled here, outside the main interrupt handler,
798 * to reduce the size of the handler.
799 */
800 static u32 rr_handle_event(struct net_device *dev, u32 prodidx, u32 eidx)
801 {
802 struct rr_private *rrpriv;
803 struct rr_regs *regs;
804 u32 tmp;
805
806 rrpriv = (struct rr_private *)dev->priv;
807 regs = rrpriv->regs;
808
809 while (prodidx != eidx){
810 switch (rrpriv->evt_ring[eidx].code){
811 case E_NIC_UP:
812 tmp = readl(®s->FwRev);
813 printk(KERN_INFO "%s: Firmware revision %i.%i.%i "
814 "up and running\n", dev->name,
815 (tmp >> 16), ((tmp >> 8) & 0xff), (tmp & 0xff));
816 rrpriv->fw_running = 1;
817 writel(RX_RING_ENTRIES - 1, ®s->IpRxPi);
818 wmb();
819 break;
820 case E_LINK_ON:
821 printk(KERN_INFO "%s: Optical link ON\n", dev->name);
822 break;
823 case E_LINK_OFF:
824 printk(KERN_INFO "%s: Optical link OFF\n", dev->name);
825 break;
826 case E_RX_IDLE:
827 printk(KERN_WARNING "%s: RX data not moving\n",
828 dev->name);
829 break;
830 case E_WATCHDOG:
831 printk(KERN_INFO "%s: The watchdog is here to see "
832 "us\n", dev->name);
833 break;
834 case E_INTERN_ERR:
835 printk(KERN_ERR "%s: HIPPI Internal NIC error\n",
836 dev->name);
837 writel(readl(®s->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
838 ®s->HostCtrl);
839 wmb();
840 break;
841 case E_HOST_ERR:
842 printk(KERN_ERR "%s: Host software error\n",
843 dev->name);
844 writel(readl(®s->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
845 ®s->HostCtrl);
846 wmb();
847 break;
848 /*
849 * TX events.
850 */
851 case E_CON_REJ:
852 printk(KERN_WARNING "%s: Connection rejected\n",
853 dev->name);
854 rrpriv->stats.tx_aborted_errors++;
855 break;
856 case E_CON_TMOUT:
857 printk(KERN_WARNING "%s: Connection timeout\n",
858 dev->name);
859 break;
860 case E_DISC_ERR:
861 printk(KERN_WARNING "%s: HIPPI disconnect error\n",
862 dev->name);
863 rrpriv->stats.tx_aborted_errors++;
864 break;
865 case E_INT_PRTY:
866 printk(KERN_ERR "%s: HIPPI Internal Parity error\n",
867 dev->name);
868 writel(readl(®s->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
869 ®s->HostCtrl);
870 wmb();
871 break;
872 case E_TX_IDLE:
873 printk(KERN_WARNING "%s: Transmitter idle\n",
874 dev->name);
875 break;
876 case E_TX_LINK_DROP:
877 printk(KERN_WARNING "%s: Link lost during transmit\n",
878 dev->name);
879 rrpriv->stats.tx_aborted_errors++;
880 writel(readl(®s->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
881 ®s->HostCtrl);
882 wmb();
883 break;
884 case E_TX_INV_RNG:
885 printk(KERN_ERR "%s: Invalid send ring block\n",
886 dev->name);
887 writel(readl(®s->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
888 ®s->HostCtrl);
889 wmb();
890 break;
891 case E_TX_INV_BUF:
892 printk(KERN_ERR "%s: Invalid send buffer address\n",
893 dev->name);
894 writel(readl(®s->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
895 ®s->HostCtrl);
896 wmb();
897 break;
898 case E_TX_INV_DSC:
899 printk(KERN_ERR "%s: Invalid descriptor address\n",
900 dev->name);
901 writel(readl(®s->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
902 ®s->HostCtrl);
903 wmb();
904 break;
905 /*
906 * RX events.
907 */
908 case E_RX_RNG_OUT:
909 printk(KERN_INFO "%s: Receive ring full\n", dev->name);
910 break;
911
912 case E_RX_PAR_ERR:
913 printk(KERN_WARNING "%s: Receive parity error\n",
914 dev->name);
915 break;
916 case E_RX_LLRC_ERR:
917 printk(KERN_WARNING "%s: Receive LLRC error\n",
918 dev->name);
919 break;
920 case E_PKT_LN_ERR:
921 printk(KERN_WARNING "%s: Receive packet length "
922 "error\n", dev->name);
923 break;
924 case E_RX_INV_BUF:
925 printk(KERN_ERR "%s: Invalid receive buffer "
926 "address\n", dev->name);
927 writel(readl(®s->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
928 ®s->HostCtrl);
929 wmb();
930 break;
931 case E_RX_INV_DSC:
932 printk(KERN_ERR "%s: Invalid receive descriptor "
933 "address\n", dev->name);
934 writel(readl(®s->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
935 ®s->HostCtrl);
936 wmb();
937 break;
938 case E_RNG_BLK:
939 printk(KERN_ERR "%s: Invalid ring block\n",
940 dev->name);
941 writel(readl(®s->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
942 ®s->HostCtrl);
943 wmb();
944 break;
945 default:
946 printk(KERN_WARNING "%s: Unhandled event 0x%02x\n",
947 dev->name, rrpriv->evt_ring[eidx].code);
948 }
949 eidx = (eidx + 1) % EVT_RING_ENTRIES;
950 }
951
952 rrpriv->info->evt_ctrl.pi = eidx;
953 wmb();
954 return eidx;
955 }
956
957
958 static void rx_int(struct net_device *dev, u32 rxlimit, u32 index)
959 {
960 struct rr_private *rrpriv = (struct rr_private *)dev->priv;
961 struct rr_regs *regs = rrpriv->regs;
962
963 do {
964 u32 pkt_len;
965 pkt_len = rrpriv->rx_ring[index].size;
966 #if (DEBUG > 2)
967 printk("index %i, rxlimit %i\n", index, rxlimit);
968 printk("len %x, mode %x\n", pkt_len,
969 rrpriv->rx_ring[index].mode);
970 #endif
971 if (pkt_len > 0){
972 struct sk_buff *skb;
973
974 if (pkt_len < PKT_COPY_THRESHOLD) {
975 skb = alloc_skb(pkt_len, GFP_ATOMIC);
976 if (skb == NULL){
977 printk(KERN_WARNING "%s: Unable to allocate skb (%i bytes), deferring packet\n", dev->name, pkt_len);
978 rrpriv->stats.rx_dropped++;
979 goto defer;
980 }else
981 memcpy(skb_put(skb, pkt_len),
982 rrpriv->rx_skbuff[index]->data,
983 pkt_len);
984 }else{
985 struct sk_buff *newskb;
986
987 newskb = alloc_skb(dev->mtu + HIPPI_HLEN,
988 GFP_ATOMIC);
989 if (newskb){
990 skb = rrpriv->rx_skbuff[index];
991 skb_put(skb, pkt_len);
992 rrpriv->rx_skbuff[index] = newskb;
993 set_rraddr(&rrpriv->rx_ring[index].addr, newskb->data);
994 }else{
995 printk("%s: Out of memory, deferring "
996 "packet\n", dev->name);
997 rrpriv->stats.rx_dropped++;
998 goto defer;
999 }
1000 }
1001 skb->dev = dev;
1002 skb->protocol = hippi_type_trans(skb, dev);
1003
1004 netif_rx(skb); /* send it up */
1005
1006 dev->last_rx = jiffies;
1007 rrpriv->stats.rx_packets++;
1008 rrpriv->stats.rx_bytes += pkt_len;
1009 }
1010 defer:
1011 rrpriv->rx_ring[index].mode = 0;
1012 rrpriv->rx_ring[index].size = dev->mtu + HIPPI_HLEN;
1013
1014 if ((index & 7) == 7)
1015 writel(index, ®s->IpRxPi);
1016
1017 index = (index + 1) % RX_RING_ENTRIES;
1018 } while(index != rxlimit);
1019
1020 rrpriv->cur_rx = index;
1021 wmb();
1022 }
1023
1024
1025 static void rr_interrupt(int irq, void *dev_id, struct pt_regs *ptregs)
1026 {
1027 struct rr_private *rrpriv;
1028 struct rr_regs *regs;
1029 struct net_device *dev = (struct net_device *)dev_id;
1030 u32 prodidx, rxindex, eidx, txcsmr, rxlimit, txcon;
1031
1032 rrpriv = (struct rr_private *)dev->priv;
1033 regs = rrpriv->regs;
1034
1035 if (!(readl(®s->HostCtrl) & RR_INT))
1036 return;
1037
1038 spin_lock(&rrpriv->lock);
1039
1040 prodidx = readl(®s->EvtPrd);
1041 txcsmr = (prodidx >> 8) & 0xff;
1042 rxlimit = (prodidx >> 16) & 0xff;
1043 prodidx &= 0xff;
1044
1045 #if (DEBUG > 2)
1046 printk("%s: interrupt, prodidx = %i, eidx = %i\n", dev->name,
1047 prodidx, rrpriv->info->evt_ctrl.pi);
1048 #endif
1049
1050 rxindex = rrpriv->cur_rx;
1051 if (rxindex != rxlimit)
1052 rx_int(dev, rxlimit, rxindex);
1053
1054 txcon = rrpriv->dirty_tx;
1055 if (txcsmr != txcon) {
1056 do {
1057 rrpriv->stats.tx_packets++;
1058 rrpriv->stats.tx_bytes +=rrpriv->tx_skbuff[txcon]->len;
1059 dev_kfree_skb_irq(rrpriv->tx_skbuff[txcon]);
1060
1061 rrpriv->tx_skbuff[txcon] = NULL;
1062 rrpriv->tx_ring[txcon].size = 0;
1063 set_rraddr(&rrpriv->tx_ring[txcon].addr, 0);
1064 rrpriv->tx_ring[txcon].mode = 0;
1065
1066 txcon = (txcon + 1) % TX_RING_ENTRIES;
1067 } while (txcsmr != txcon);
1068 wmb();
1069
1070 rrpriv->dirty_tx = txcon;
1071 if (rrpriv->tx_full && rr_if_busy(dev) &&
1072 (((rrpriv->info->tx_ctrl.pi + 1) % TX_RING_ENTRIES)
1073 != rrpriv->dirty_tx)){
1074 rrpriv->tx_full = 0;
1075 netif_wake_queue(dev);
1076 rr_mark_net_bh(NET_BH);
1077 }
1078 }
1079
1080 eidx = rrpriv->info->evt_ctrl.pi;
1081 if (prodidx != eidx)
1082 eidx = rr_handle_event(dev, prodidx, eidx);
1083
1084 eidx |= ((txcsmr << 8) | (rxlimit << 16));
1085 writel(eidx, ®s->EvtCon);
1086 wmb();
1087
1088 spin_unlock(&rrpriv->lock);
1089 }
1090
1091
1092 static void rr_timer(unsigned long data)
1093 {
1094 struct net_device *dev = (struct net_device *)data;
1095 struct rr_private *rrpriv = (struct rr_private *)dev->priv;
1096 struct rr_regs *regs = rrpriv->regs;
1097 unsigned long flags;
1098 int i;
1099
1100 if (readl(®s->HostCtrl) & NIC_HALTED){
1101 printk("%s: Restarting nic\n", dev->name);
1102 memset(rrpriv->rx_ctrl, 0, 256 * sizeof(struct ring_ctrl));
1103 memset(rrpriv->info, 0, sizeof(struct rr_info));
1104 wmb();
1105 for (i = 0; i < TX_RING_ENTRIES; i++) {
1106 if (rrpriv->tx_skbuff[i]) {
1107 rrpriv->tx_ring[i].size = 0;
1108 set_rraddr(&rrpriv->tx_ring[i].addr, 0);
1109 dev_kfree_skb(rrpriv->tx_skbuff[i]);
1110 rrpriv->tx_skbuff[i] = NULL;
1111 }
1112 }
1113
1114 for (i = 0; i < RX_RING_ENTRIES; i++) {
1115 if (rrpriv->rx_skbuff[i]) {
1116 rrpriv->rx_ring[i].size = 0;
1117 set_rraddr(&rrpriv->rx_ring[i].addr, 0);
1118 dev_kfree_skb(rrpriv->rx_skbuff[i]);
1119 rrpriv->rx_skbuff[i] = NULL;
1120 }
1121 }
1122 if (rr_init1(dev)) {
1123 spin_lock_irqsave(&rrpriv->lock, flags);
1124 writel(readl(®s->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
1125 ®s->HostCtrl);
1126 spin_unlock_irqrestore(&rrpriv->lock, flags);
1127 }
1128 }
1129 rrpriv->timer.expires = RUN_AT(5*HZ);
1130 add_timer(&rrpriv->timer);
1131 }
1132
1133
1134 static int rr_open(struct net_device *dev)
1135 {
1136 struct rr_private *rrpriv;
1137 struct rr_regs *regs;
1138 int ecode = 0;
1139 unsigned long flags;
1140
1141 rrpriv = (struct rr_private *)dev->priv;
1142 regs = rrpriv->regs;
1143
1144 if (rrpriv->fw_rev < 0x00020000) {
1145 printk(KERN_WARNING "%s: trying to configure device with "
1146 "obsolete firmware\n", dev->name);
1147 ecode = -EBUSY;
1148 goto error;
1149 }
1150
1151 rrpriv->rx_ctrl = kmalloc(256*sizeof(struct ring_ctrl), GFP_KERNEL);
1152 if (!rrpriv->rx_ctrl) {
1153 ecode = -ENOMEM;
1154 goto error;
1155 }
1156
1157 rrpriv->info = kmalloc(sizeof(struct rr_info), GFP_KERNEL);
1158 if (!rrpriv->info){
1159 rrpriv->rx_ctrl = NULL;
1160 ecode = -ENOMEM;
1161 goto error;
1162 }
1163 memset(rrpriv->rx_ctrl, 0, 256 * sizeof(struct ring_ctrl));
1164 memset(rrpriv->info, 0, sizeof(struct rr_info));
1165 wmb();
1166
1167 spin_lock_irqsave(&rrpriv->lock, flags);
1168 writel(readl(®s->HostCtrl)|HALT_NIC|RR_CLEAR_INT, ®s->HostCtrl);
1169 spin_unlock_irqrestore(&rrpriv->lock, flags);
1170
1171 if (request_irq(dev->irq, rr_interrupt, SA_SHIRQ, rrpriv->name, dev))
1172 {
1173 printk(KERN_WARNING "%s: Requested IRQ %d is busy\n",
1174 dev->name, dev->irq);
1175 ecode = -EAGAIN;
1176 goto error;
1177 }
1178
1179 if ((ecode = rr_init1(dev)))
1180 goto error;
1181
1182 /* Set the timer to switch to check for link beat and perhaps switch
1183 to an alternate media type. */
1184 init_timer(&rrpriv->timer);
1185 rrpriv->timer.expires = RUN_AT(5*HZ); /* 5 sec. watchdog */
1186 rrpriv->timer.data = (unsigned long)dev;
1187 rrpriv->timer.function = &rr_timer; /* timer handler */
1188 add_timer(&rrpriv->timer);
1189
1190 netif_start_queue(dev);
1191
1192 return ecode;
1193
1194 error:
1195 spin_lock_irqsave(&rrpriv->lock, flags);
1196 writel(readl(®s->HostCtrl)|HALT_NIC|RR_CLEAR_INT, ®s->HostCtrl);
1197 spin_unlock_irqrestore(&rrpriv->lock, flags);
1198
1199 if (rrpriv->info) {
1200 kfree(rrpriv->info);
1201 rrpriv->info = NULL;
1202 }
1203 if (rrpriv->rx_ctrl) {
1204 kfree(rrpriv->rx_ctrl);
1205 rrpriv->rx_ctrl = NULL;
1206 }
1207
1208 netif_stop_queue(dev);
1209 rr_if_down(dev);
1210
1211 return ecode;
1212 }
1213
1214
1215 static void rr_dump(struct net_device *dev)
1216 {
1217 struct rr_private *rrpriv;
1218 struct rr_regs *regs;
1219 u32 index, cons;
1220 short i;
1221 int len;
1222
1223 rrpriv = (struct rr_private *)dev->priv;
1224 regs = rrpriv->regs;
1225
1226 printk("%s: dumping NIC TX rings\n", dev->name);
1227
1228 printk("RxPrd %08x, TxPrd %02x, EvtPrd %08x, TxPi %02x, TxCtrlPi %02x\n",
1229 readl(®s->RxPrd), readl(®s->TxPrd),
1230 readl(®s->EvtPrd), readl(®s->TxPi),
1231 rrpriv->info->tx_ctrl.pi);
1232
1233 printk("Error code 0x%x\n", readl(®s->Fail1));
1234
1235 index = (((readl(®s->EvtPrd) >> 8) & 0xff ) - 1) % EVT_RING_ENTRIES;
1236 cons = rrpriv->dirty_tx;
1237 printk("TX ring index %i, TX consumer %i\n",
1238 index, cons);
1239
1240 if (rrpriv->tx_skbuff[index]){
1241 len = min(0x80, rrpriv->tx_skbuff[index]->len);
1242 printk("skbuff for index %i is valid - dumping data (0x%x bytes - DMA len 0x%x)\n", index, len, rrpriv->tx_ring[index].size);
1243 for (i = 0; i < len; i++){
1244 if (!(i & 7))
1245 printk("\n");
1246 printk("%02x ", (unsigned char) rrpriv->tx_skbuff[index]->data[i]);
1247 }
1248 printk("\n");
1249 }
1250
1251 if (rrpriv->tx_skbuff[cons]){
1252 len = min(0x80, rrpriv->tx_skbuff[cons]->len);
1253 printk("skbuff for cons %i is valid - dumping data (0x%x bytes - skbuff len 0x%x)\n", cons, len, rrpriv->tx_skbuff[cons]->len);
1254 printk("mode 0x%x, size 0x%x,\n phys %08x (virt %08lx), skbuff-addr %08lx, truesize 0x%x\n",
1255 rrpriv->tx_ring[cons].mode,
1256 rrpriv->tx_ring[cons].size,
1257 rrpriv->tx_ring[cons].addr.addrlo,
1258 (unsigned long)bus_to_virt(rrpriv->tx_ring[cons].addr.addrlo),
1259 (unsigned long)rrpriv->tx_skbuff[cons]->data,
1260 (unsigned int)rrpriv->tx_skbuff[cons]->truesize);
1261 for (i = 0; i < len; i++){
1262 if (!(i & 7))
1263 printk("\n");
1264 printk("%02x ", (unsigned char)rrpriv->tx_ring[cons].size);
1265 }
1266 printk("\n");
1267 }
1268
1269 printk("dumping TX ring info:\n");
1270 for (i = 0; i < TX_RING_ENTRIES; i++)
1271 printk("mode 0x%x, size 0x%x, phys-addr %08x\n",
1272 rrpriv->tx_ring[i].mode,
1273 rrpriv->tx_ring[i].size,
1274 rrpriv->tx_ring[i].addr.addrlo);
1275
1276 }
1277
1278
1279 static int rr_close(struct net_device *dev)
1280 {
1281 struct rr_private *rrpriv;
1282 struct rr_regs *regs;
1283 u32 tmp;
1284 short i;
1285
1286 netif_stop_queue(dev);
1287 rr_if_down(dev);
1288
1289 rrpriv = (struct rr_private *)dev->priv;
1290 regs = rrpriv->regs;
1291
1292 /*
1293 * Lock to make sure we are not cleaning up while another CPU
1294 * handling interrupts.
1295 */
1296 spin_lock(&rrpriv->lock);
1297
1298 tmp = readl(®s->HostCtrl);
1299 if (tmp & NIC_HALTED){
1300 printk("%s: NIC already halted\n", dev->name);
1301 rr_dump(dev);
1302 }else{
1303 tmp |= HALT_NIC | RR_CLEAR_INT;
1304 writel(tmp, ®s->HostCtrl);
1305 wmb();
1306 }
1307
1308 rrpriv->fw_running = 0;
1309
1310 del_timer(&rrpriv->timer);
1311
1312 writel(0, ®s->TxPi);
1313 writel(0, ®s->IpRxPi);
1314
1315 writel(0, ®s->EvtCon);
1316 writel(0, ®s->EvtPrd);
1317
1318 for (i = 0; i < CMD_RING_ENTRIES; i++)
1319 writel(0, ®s->CmdRing[i]);
1320
1321 rrpriv->info->tx_ctrl.entries = 0;
1322 rrpriv->info->cmd_ctrl.pi = 0;
1323 rrpriv->info->evt_ctrl.pi = 0;
1324 rrpriv->rx_ctrl[4].entries = 0;
1325
1326 for (i = 0; i < TX_RING_ENTRIES; i++) {
1327 if (rrpriv->tx_skbuff[i]) {
1328 rrpriv->tx_ring[i].size = 0;
1329 set_rraddr(&rrpriv->tx_ring[i].addr, 0);
1330 dev_kfree_skb(rrpriv->tx_skbuff[i]);
1331 rrpriv->tx_skbuff[i] = NULL;
1332 }
1333 }
1334
1335 for (i = 0; i < RX_RING_ENTRIES; i++) {
1336 if (rrpriv->rx_skbuff[i]) {
1337 rrpriv->rx_ring[i].size = 0;
1338 set_rraddr(&rrpriv->rx_ring[i].addr, 0);
1339 dev_kfree_skb(rrpriv->rx_skbuff[i]);
1340 rrpriv->rx_skbuff[i] = NULL;
1341 }
1342 }
1343
1344 if (rrpriv->rx_ctrl) {
1345 kfree(rrpriv->rx_ctrl);
1346 rrpriv->rx_ctrl = NULL;
1347 }
1348 if (rrpriv->info) {
1349 kfree(rrpriv->info);
1350 rrpriv->info = NULL;
1351 }
1352
1353 free_irq(dev->irq, dev);
1354 spin_unlock(&rrpriv->lock);
1355
1356 return 0;
1357 }
1358
1359
1360 static int rr_start_xmit(struct sk_buff *skb, struct net_device *dev)
1361 {
1362 struct rr_private *rrpriv = (struct rr_private *)dev->priv;
1363 struct rr_regs *regs = rrpriv->regs;
1364 struct ring_ctrl *txctrl;
1365 unsigned long flags;
1366 u32 index, len = skb->len;
1367 u32 *ifield;
1368 struct sk_buff *new_skb;
1369
1370 if (readl(®s->Mode) & FATAL_ERR)
1371 printk("error codes Fail1 %02x, Fail2 %02x\n",
1372 readl(®s->Fail1), readl(®s->Fail2));
1373
1374 /*
1375 * We probably need to deal with tbusy here to prevent overruns.
1376 */
1377
1378 if (skb_headroom(skb) < 8){
1379 printk("incoming skb too small - reallocating\n");
1380 if (!(new_skb = dev_alloc_skb(len + 8))) {
1381 dev_kfree_skb(skb);
1382 netif_wake_queue(dev);
1383 return -EBUSY;
1384 }
1385 skb_reserve(new_skb, 8);
1386 skb_put(new_skb, len);
1387 memcpy(new_skb->data, skb->data, len);
1388 dev_kfree_skb(skb);
1389 skb = new_skb;
1390 }
1391
1392 ifield = (u32 *)skb_push(skb, 8);
1393
1394 ifield[0] = 0;
1395 ifield[1] = skb->private.ifield;
1396
1397 /*
1398 * We don't need the lock before we are actually going to start
1399 * fiddling with the control blocks.
1400 */
1401 spin_lock_irqsave(&rrpriv->lock, flags);
1402
1403 txctrl = &rrpriv->info->tx_ctrl;
1404
1405 index = txctrl->pi;
1406
1407 rrpriv->tx_skbuff[index] = skb;
1408 set_rraddr(&rrpriv->tx_ring[index].addr, skb->data);
1409 rrpriv->tx_ring[index].size = len + 8; /* include IFIELD */
1410 rrpriv->tx_ring[index].mode = PACKET_START | PACKET_END;
1411 txctrl->pi = (index + 1) % TX_RING_ENTRIES;
1412 wmb();
1413 writel(txctrl->pi, ®s->TxPi);
1414
1415 if (txctrl->pi == rrpriv->dirty_tx){
1416 rrpriv->tx_full = 1;
1417 netif_stop_queue(dev);
1418 }
1419
1420 spin_unlock_irqrestore(&rrpriv->lock, flags);
1421
1422 dev->trans_start = jiffies;
1423 return 0;
1424 }
1425
1426
1427 static struct net_device_stats *rr_get_stats(struct net_device *dev)
1428 {
1429 struct rr_private *rrpriv;
1430
1431 rrpriv = (struct rr_private *)dev->priv;
1432
1433 return(&rrpriv->stats);
1434 }
1435
1436
1437 /*
1438 * Read the firmware out of the EEPROM and put it into the SRAM
1439 * (or from user space - later)
1440 *
1441 * This operation requires the NIC to be halted and is performed with
1442 * interrupts disabled and with the spinlock hold.
1443 */
1444 static int rr_load_firmware(struct net_device *dev)
1445 {
1446 struct rr_private *rrpriv;
1447 struct rr_regs *regs;
1448 unsigned long eptr, segptr;
1449 int i, j;
1450 u32 localctrl, sptr, len, tmp;
1451 u32 p2len, p2size, nr_seg, revision, io, sram_size;
1452 struct eeprom *hw = NULL;
1453
1454 rrpriv = (struct rr_private *)dev->priv;
1455 regs = rrpriv->regs;
1456
1457 if (dev->flags & IFF_UP)
1458 return -EBUSY;
1459
1460 if (!(readl(®s->HostCtrl) & NIC_HALTED)){
1461 printk("%s: Trying to load firmware to a running NIC.\n",
1462 dev->name);
1463 return -EBUSY;
1464 }
1465
1466 localctrl = readl(®s->LocalCtrl);
1467 writel(0, ®s->LocalCtrl);
1468
1469 writel(0, ®s->EvtPrd);
1470 writel(0, ®s->RxPrd);
1471 writel(0, ®s->TxPrd);
1472
1473 /*
1474 * First wipe the entire SRAM, otherwise we might run into all
1475 * kinds of trouble ... sigh, this took almost all afternoon
1476 * to track down ;-(
1477 */
1478 io = readl(®s->ExtIo);
1479 writel(0, ®s->ExtIo);
1480 sram_size = rr_read_eeprom_word(rrpriv, (void *)8);
1481
1482 for (i = 200; i < sram_size / 4; i++){
1483 writel(i * 4, ®s->WinBase);
1484 mb();
1485 writel(0, ®s->WinData);
1486 mb();
1487 }
1488 writel(io, ®s->ExtIo);
1489 mb();
1490
1491 eptr = (unsigned long)rr_read_eeprom_word(rrpriv,
1492 &hw->rncd_info.AddrRunCodeSegs);
1493 eptr = ((eptr & 0x1fffff) >> 3);
1494
1495 p2len = rr_read_eeprom_word(rrpriv, (void *)(0x83*4));
1496 p2len = (p2len << 2);
1497 p2size = rr_read_eeprom_word(rrpriv, (void *)(0x84*4));
1498 p2size = ((p2size & 0x1fffff) >> 3);
1499
1500 if ((eptr < p2size) || (eptr > (p2size + p2len))){
1501 printk("%s: eptr is invalid\n", dev->name);
1502 goto out;
1503 }
1504
1505 revision = rr_read_eeprom_word(rrpriv, &hw->manf.HeaderFmt);
1506
1507 if (revision != 1){
1508 printk("%s: invalid firmware format (%i)\n",
1509 dev->name, revision);
1510 goto out;
1511 }
1512
1513 nr_seg = rr_read_eeprom_word(rrpriv, (void *)eptr);
1514 eptr +=4;
1515 #if (DEBUG > 1)
1516 printk("%s: nr_seg %i\n", dev->name, nr_seg);
1517 #endif
1518
1519 for (i = 0; i < nr_seg; i++){
1520 sptr = rr_read_eeprom_word(rrpriv, (void *)eptr);
1521 eptr += 4;
1522 len = rr_read_eeprom_word(rrpriv, (void *)eptr);
1523 eptr += 4;
1524 segptr = (unsigned long)rr_read_eeprom_word(rrpriv, (void *)eptr);
1525 segptr = ((segptr & 0x1fffff) >> 3);
1526 eptr += 4;
1527 #if (DEBUG > 1)
1528 printk("%s: segment %i, sram address %06x, length %04x, segptr %06x\n",
1529 dev->name, i, sptr, len, segptr);
1530 #endif
1531 for (j = 0; j < len; j++){
1532 tmp = rr_read_eeprom_word(rrpriv, (void *)segptr);
1533 writel(sptr, ®s->WinBase);
1534 mb();
1535 writel(tmp, ®s->WinData);
1536 mb();
1537 segptr += 4;
1538 sptr += 4;
1539 }
1540 }
1541
1542 out:
1543 writel(localctrl, ®s->LocalCtrl);
1544 mb();
1545 return 0;
1546 }
1547
1548
1549 static int rr_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1550 {
1551 struct rr_private *rrpriv;
1552 unsigned char *image, *oldimage;
1553 unsigned int i;
1554 int error = -EOPNOTSUPP;
1555
1556 rrpriv = dev->priv;
1557
1558 switch(cmd){
1559 case SIOCRRGFW:
1560 if (!capable(CAP_SYS_RAWIO)){
1561 return -EPERM;
1562 }
1563
1564 image = kmalloc(EEPROM_WORDS * sizeof(u32), GFP_KERNEL);
1565 if (!image){
1566 printk(KERN_ERR "%s: Unable to allocate memory "
1567 "for EEPROM image\n", dev->name);
1568 return -ENOMEM;
1569 }
1570
1571 spin_lock(&rrpriv->lock);
1572
1573 if (rrpriv->fw_running){
1574 printk("%s: Firmware already running\n", dev->name);
1575 error = -EPERM;
1576 goto out_spin;
1577 }
1578
1579 i = rr_read_eeprom(rrpriv, 0, image, EEPROM_BYTES);
1580 if (i != EEPROM_BYTES){
1581 printk(KERN_ERR "%s: Error reading EEPROM\n", dev->name);
1582 error = -EFAULT;
1583 goto out_spin;
1584 }
1585 spin_unlock(&rrpriv->lock);
1586 error = copy_to_user(rq->ifr_data, image, EEPROM_BYTES);
1587 if (error)
1588 error = -EFAULT;
1589 kfree(image);
1590 return error;
1591
1592 case SIOCRRPFW:
1593 if (!capable(CAP_SYS_RAWIO)){
1594 return -EPERM;
1595 }
1596
1597 image = kmalloc(EEPROM_WORDS * sizeof(u32), GFP_KERNEL);
1598 if (!image){
1599 printk(KERN_ERR "%s: Unable to allocate memory "
1600 "for EEPROM image\n", dev->name);
1601 return -ENOMEM;
1602 }
1603
1604 oldimage = kmalloc(EEPROM_WORDS * sizeof(u32), GFP_KERNEL);
1605 if (!oldimage){
1606 kfree(image);
1607 printk(KERN_ERR "%s: Unable to allocate memory "
1608 "for old EEPROM image\n", dev->name);
1609 return -ENOMEM;
1610 }
1611
1612 error = copy_from_user(image, rq->ifr_data, EEPROM_BYTES);
1613 if (error) {
1614 kfree(image);
1615 kfree(oldimage);
1616 return -EFAULT;
1617 }
1618
1619 spin_lock(&rrpriv->lock);
1620 if (rrpriv->fw_running){
1621 kfree(oldimage);
1622 printk("%s: Firmware already running\n", dev->name);
1623 error = -EPERM;
1624 goto out_spin;
1625 }
1626
1627 printk("%s: Updating EEPROM firmware\n", dev->name);
1628
1629 error = write_eeprom(rrpriv, 0, image, EEPROM_BYTES);
1630 if (error)
1631 printk(KERN_ERR "%s: Error writing EEPROM\n",
1632 dev->name);
1633
1634 i = rr_read_eeprom(rrpriv, 0, oldimage, EEPROM_BYTES);
1635 if (i != EEPROM_BYTES)
1636 printk(KERN_ERR "%s: Error reading back EEPROM "
1637 "image\n", dev->name);
1638
1639 spin_unlock(&rrpriv->lock);
1640 error = memcmp(image, oldimage, EEPROM_BYTES);
1641 if (error){
1642 printk(KERN_ERR "%s: Error verifying EEPROM image\n",
1643 dev->name);
1644 error = -EFAULT;
1645 }
1646 kfree(image);
1647 kfree(oldimage);
1648 return error;
1649
1650 case SIOCRRID:
1651 return put_user(0x52523032, (int *)(&rq->ifr_data[0]));
1652 default:
1653 return error;
1654 }
1655
1656 out_spin:
1657 kfree(image);
1658 spin_unlock(&rrpriv->lock);
1659 return error;
1660 }
1661
1662
1663 /*
1664 * Local variables:
1665 * compile-command: "gcc -D__KERNEL__ -I../../include -Wall -Wstrict-prototypes -O2 -pipe -fomit-frame-pointer -fno-strength-reduce -m486 -malign-loops=2 -malign-jumps=2 -malign-functions=2 -DMODULE -DMODVERSIONS -include ../../include/linux/modversions.h -c rrunner.c"
1666 * End:
1667 */
1668