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