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