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