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