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