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