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