* add p cc
[mascara-docs.git] / i386 / linux / linux-2.3.21 / drivers / net / rrunner.c
blob1353e23e967e50407dbc8419762d3f0882a48a55
1 /*
2 * rrunner.c: Linux driver for the Essential RoadRunner HIPPI board.
4 * Written 1998 by Jes Sorensen, <Jes.Sorensen@cern.ch>.
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.
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.
18 #define DEBUG 1
19 #define RX_DMA_SKBUFF 1
20 #define PKT_COPY_THRESHOLD 512
22 #include <linux/module.h>
24 #include <linux/types.h>
25 #include <linux/errno.h>
26 #include <linux/ioport.h>
27 #include <linux/pci.h>
28 #include <linux/kernel.h>
29 #include <linux/netdevice.h>
30 #include <linux/hippidevice.h>
31 #include <linux/skbuff.h>
32 #include <linux/init.h>
33 #include <linux/delay.h>
34 #include <linux/mm.h>
35 #include <net/sock.h>
37 #include <asm/system.h>
38 #include <asm/cache.h>
39 #include <asm/byteorder.h>
40 #include <asm/io.h>
41 #include <asm/irq.h>
42 #include <asm/uaccess.h>
44 #include "rrunner.h"
48 * Implementation notes:
50 * The DMA engine only allows for DMA within physical 64KB chunks of
51 * memory. The current approach of the driver (and stack) is to use
52 * linear blocks of memory for the skbuffs. However, as the data block
53 * is always the first part of the skb and skbs are 2^n aligned so we
54 * are guarantted to get the whole block within one 64KB align 64KB
55 * chunk.
57 * On the long term, relying on being able to allocate 64KB linear
58 * chunks of memory is not feasible and the skb handling code and the
59 * stack will need to know about I/O vectors or something similar.
62 static const char __initdata *version = "rrunner.c: v0.17 03/09/99 Jes Sorensen (Jes.Sorensen@cern.ch)\n";
66 * These are checked at init time to see if they are at least 256KB
67 * and increased to 256KB if they are not. This is done to avoid ending
68 * up with socket buffers smaller than the MTU size,
70 extern __u32 sysctl_wmem_max;
71 extern __u32 sysctl_rmem_max;
73 static int probed __initdata = 0;
75 int __init rr_hippi_probe (struct net_device *dev)
77 int boards_found = 0;
78 int version_disp; /* was version info already displayed? */
79 struct pci_dev *pdev = NULL;
80 struct pci_dev *opdev = NULL;
81 u8 pci_latency;
82 struct rr_private *rrpriv;
84 if (probed)
85 return -ENODEV;
86 probed++;
88 if (!pci_present()) /* is PCI BIOS even present? */
89 return -ENODEV;
91 version_disp = 0;
93 while((pdev = pci_find_device(PCI_VENDOR_ID_ESSENTIAL,
94 PCI_DEVICE_ID_ESSENTIAL_ROADRUNNER,
95 pdev)))
97 if (pdev == opdev)
98 return 0;
101 * So we found our HIPPI ... time to tell the system.
104 dev = init_hippi_dev(dev, sizeof(struct rr_private));
106 if (!dev)
107 break;
109 if (!dev->priv)
110 dev->priv = kmalloc(sizeof(*rrpriv), GFP_KERNEL);
112 if (!dev->priv)
113 return -ENOMEM;
115 rrpriv = (struct rr_private *)dev->priv;
116 memset(rrpriv, 0, sizeof(*rrpriv));
118 #ifdef __SMP__
119 spin_lock_init(&rrpriv->lock);
120 #endif
121 sprintf(rrpriv->name, "RoadRunner serial HIPPI");
123 dev->irq = pdev->irq;
124 dev->open = &rr_open;
125 dev->hard_start_xmit = &rr_start_xmit;
126 dev->stop = &rr_close;
127 dev->get_stats = &rr_get_stats;
128 dev->do_ioctl = &rr_ioctl;
131 * Dummy value.
133 dev->base_addr = 42;
135 /* display version info if adapter is found */
136 if (!version_disp)
138 /* set display flag to TRUE so that */
139 /* we only display this string ONCE */
140 version_disp = 1;
141 printk(version);
144 pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &pci_latency);
145 if (pci_latency <= 0x58){
146 pci_latency = 0x58;
147 pci_write_config_byte(pdev, PCI_LATENCY_TIMER,
148 pci_latency);
151 pci_set_master(pdev);
153 printk(KERN_INFO "%s: Essential RoadRunner serial HIPPI "
154 "at 0x%08lx, irq %i, PCI latency %i\n", dev->name,
155 pdev->resource[0].start, dev->irq, pci_latency);
158 * Remap the regs into kernel space.
161 rrpriv->regs = (struct rr_regs *)
162 ioremap(pdev->resource[0].start, 0x1000);
164 if (!rrpriv->regs){
165 printk(KERN_ERR "%s: Unable to map I/O register, "
166 "RoadRunner %i will be disabled.\n",
167 dev->name, boards_found);
168 break;
172 * Don't access any registes before this point!
174 #ifdef __BIG_ENDIAN
175 writel(readl(&regs->HostCtrl) | NO_SWAP, &regs->HostCtrl);
176 #endif
178 * Need to add a case for little-endian 64-bit hosts here.
181 rr_init(dev);
183 boards_found++;
184 dev->base_addr = 0;
185 dev = NULL;
186 opdev = pdev;
190 * If we're at this point we're going through rr_hippi_probe()
191 * for the first time. Return success (0) if we've initialized
192 * 1 or more boards. Otherwise, return failure (-ENODEV).
195 #ifdef MODULE
196 return boards_found;
197 #else
198 if (boards_found > 0)
199 return 0;
200 else
201 return -ENODEV;
202 #endif
205 static struct net_device *root_dev = NULL;
207 #ifdef MODULE
208 #if LINUX_VERSION_CODE > 0x20118
209 MODULE_AUTHOR("Jes Sorensen <Jes.Sorensen@cern.ch>");
210 MODULE_DESCRIPTION("Essential RoadRunner HIPPI driver");
211 #endif
214 int init_module(void)
216 int cards;
218 root_dev = NULL;
220 cards = rr_hippi_probe(NULL);
221 return cards ? 0 : -ENODEV;
224 void cleanup_module(void)
226 struct rr_private *rr;
227 struct net_device *next;
229 while (root_dev) {
230 next = ((struct rr_private *)root_dev->priv)->next;
231 rr = (struct rr_private *)root_dev->priv;
233 if (!(readl(&rr->regs->HostCtrl) & NIC_HALTED)){
234 printk(KERN_ERR "%s: trying to unload running NIC\n",
235 root_dev->name);
236 writel(HALT_NIC, &rr->regs->HostCtrl);
239 iounmap(rr->regs);
240 unregister_hipdev(root_dev);
241 kfree(root_dev);
243 root_dev = next;
246 #endif
250 * Commands are considered to be slow, thus there is no reason to
251 * inline this.
253 static void rr_issue_cmd(struct rr_private *rrpriv, struct cmd *cmd)
255 struct rr_regs *regs;
256 u32 idx;
258 regs = rrpriv->regs;
260 * This is temporary - it will go away in the final version.
261 * We probably also want to make this function inline.
263 if (readl(&regs->HostCtrl) & NIC_HALTED){
264 printk("issuing command for halted NIC, code 0x%x, "
265 "HostCtrl %08x\n", cmd->code, readl(&regs->HostCtrl));
266 if (readl(&regs->Mode) & FATAL_ERR)
267 printk("error codes Fail1 %02x, Fail2 %02x\n",
268 readl(&regs->Fail1), readl(&regs->Fail2));
271 idx = rrpriv->info->cmd_ctrl.pi;
273 writel(*(u32*)(cmd), &regs->CmdRing[idx]);
274 mb();
276 idx = (idx - 1) % CMD_RING_ENTRIES;
277 rrpriv->info->cmd_ctrl.pi = idx;
278 mb();
280 if (readl(&regs->Mode) & FATAL_ERR)
281 printk("error code %02x\n", readl(&regs->Fail1));
286 * Reset the board in a sensible manner. The NIC is already halted
287 * when we get here and a spin-lock is held.
289 static int rr_reset(struct net_device *dev)
291 struct rr_private *rrpriv;
292 struct rr_regs *regs;
293 struct eeprom *hw = NULL;
294 u32 start_pc;
295 int i;
297 rrpriv = (struct rr_private *)dev->priv;
298 regs = rrpriv->regs;
300 rr_load_firmware(dev);
302 writel(0x01000000, &regs->TX_state);
303 writel(0xff800000, &regs->RX_state);
304 writel(0, &regs->AssistState);
305 writel(CLEAR_INTA, &regs->LocalCtrl);
306 writel(0x01, &regs->BrkPt);
307 writel(0, &regs->Timer);
308 writel(0, &regs->TimerRef);
309 writel(RESET_DMA, &regs->DmaReadState);
310 writel(RESET_DMA, &regs->DmaWriteState);
311 writel(0, &regs->DmaWriteHostHi);
312 writel(0, &regs->DmaWriteHostLo);
313 writel(0, &regs->DmaReadHostHi);
314 writel(0, &regs->DmaReadHostLo);
315 writel(0, &regs->DmaReadLen);
316 writel(0, &regs->DmaWriteLen);
317 writel(0, &regs->DmaWriteLcl);
318 writel(0, &regs->DmaWriteIPchecksum);
319 writel(0, &regs->DmaReadLcl);
320 writel(0, &regs->DmaReadIPchecksum);
321 writel(0, &regs->PciState);
322 #if (BITS_PER_LONG == 64) && defined __LITTLE_ENDIAN
323 writel(SWAP_DATA | PTR64BIT | PTR_WD_SWAP, &regs->Mode);
324 #elif (BITS_PER_LONG == 64)
325 writel(SWAP_DATA | PTR64BIT | PTR_WD_NOSWAP, &regs->Mode);
326 #else
327 writel(SWAP_DATA | PTR32BIT | PTR_WD_NOSWAP, &regs->Mode);
328 #endif
330 #if 0
332 * Don't worry, this is just black magic.
334 writel(0xdf000, &regs->RxBase);
335 writel(0xdf000, &regs->RxPrd);
336 writel(0xdf000, &regs->RxCon);
337 writel(0xce000, &regs->TxBase);
338 writel(0xce000, &regs->TxPrd);
339 writel(0xce000, &regs->TxCon);
340 writel(0, &regs->RxIndPro);
341 writel(0, &regs->RxIndCon);
342 writel(0, &regs->RxIndRef);
343 writel(0, &regs->TxIndPro);
344 writel(0, &regs->TxIndCon);
345 writel(0, &regs->TxIndRef);
346 writel(0xcc000, &regs->pad10[0]);
347 writel(0, &regs->DrCmndPro);
348 writel(0, &regs->DrCmndCon);
349 writel(0, &regs->DwCmndPro);
350 writel(0, &regs->DwCmndCon);
351 writel(0, &regs->DwCmndRef);
352 writel(0, &regs->DrDataPro);
353 writel(0, &regs->DrDataCon);
354 writel(0, &regs->DrDataRef);
355 writel(0, &regs->DwDataPro);
356 writel(0, &regs->DwDataCon);
357 writel(0, &regs->DwDataRef);
358 #endif
360 writel(0xffffffff, &regs->MbEvent);
361 writel(0, &regs->Event);
363 writel(0, &regs->TxPi);
364 writel(0, &regs->IpRxPi);
366 writel(0, &regs->EvtCon);
367 writel(0, &regs->EvtPrd);
369 rrpriv->info->evt_ctrl.pi = 0;
371 for (i = 0; i < CMD_RING_ENTRIES; i++)
372 writel(0, &regs->CmdRing[i]);
375 * Why 32 ? is this not cache line size dependant?
377 writel(WBURST_32, &regs->PciState);
378 mb();
380 start_pc = rr_read_eeprom_word(rrpriv, &hw->rncd_info.FwStart);
382 #if (DEBUG > 1)
383 printk("%s: Executing firmware at address 0x%06x\n",
384 dev->name, start_pc);
385 #endif
387 writel(start_pc + 0x800, &regs->Pc);
388 mb();
389 udelay(5);
391 writel(start_pc, &regs->Pc);
392 mb();
394 return 0;
399 * Read a string from the EEPROM.
401 static unsigned int rr_read_eeprom(struct rr_private *rrpriv,
402 unsigned long offset,
403 unsigned char *buf,
404 unsigned long length)
406 struct rr_regs *regs = rrpriv->regs;
407 u32 misc, io, host, i;
409 io = readl(&regs->ExtIo);
410 writel(0, &regs->ExtIo);
411 misc = readl(&regs->LocalCtrl);
412 writel(0, &regs->LocalCtrl);
413 host = readl(&regs->HostCtrl);
414 writel(host | HALT_NIC, &regs->HostCtrl);
415 mb();
417 for (i = 0; i < length; i++){
418 writel((EEPROM_BASE + ((offset+i) << 3)), &regs->WinBase);
419 mb();
420 buf[i] = (readl(&regs->WinData) >> 24) & 0xff;
421 mb();
424 writel(host, &regs->HostCtrl);
425 writel(misc, &regs->LocalCtrl);
426 writel(io, &regs->ExtIo);
427 mb();
428 return i;
433 * Shortcut to read one word (4 bytes) out of the EEPROM and convert
434 * it to our CPU byte-order.
436 static u32 rr_read_eeprom_word(struct rr_private *rrpriv,
437 void * offset)
439 u32 word;
441 if ((rr_read_eeprom(rrpriv, (unsigned long)offset,
442 (char *)&word, 4) == 4))
443 return be32_to_cpu(word);
444 return 0;
449 * Write a string to the EEPROM.
451 * This is only called when the firmware is not running.
453 static unsigned int write_eeprom(struct rr_private *rrpriv,
454 unsigned long offset,
455 unsigned char *buf,
456 unsigned long length)
458 struct rr_regs *regs = rrpriv->regs;
459 u32 misc, io, data, i, j, ready, error = 0;
461 io = readl(&regs->ExtIo);
462 writel(0, &regs->ExtIo);
463 misc = readl(&regs->LocalCtrl);
464 writel(ENABLE_EEPROM_WRITE, &regs->LocalCtrl);
465 mb();
467 for (i = 0; i < length; i++){
468 writel((EEPROM_BASE + ((offset+i) << 3)), &regs->WinBase);
469 mb();
470 data = buf[i] << 24;
472 * Only try to write the data if it is not the same
473 * value already.
475 if ((readl(&regs->WinData) & 0xff000000) != data){
476 writel(data, &regs->WinData);
477 ready = 0;
478 j = 0;
479 mb();
480 while(!ready){
481 udelay(20);
482 if ((readl(&regs->WinData) & 0xff000000) ==
483 data)
484 ready = 1;
485 mb();
486 if (j++ > 5000){
487 printk("data mismatch: %08x, "
488 "WinData %08x\n", data,
489 readl(&regs->WinData));
490 ready = 1;
491 error = 1;
497 writel(misc, &regs->LocalCtrl);
498 writel(io, &regs->ExtIo);
499 mb();
501 return error;
505 static int __init rr_init(struct net_device *dev)
507 struct rr_private *rrpriv;
508 struct rr_regs *regs;
509 u32 sram_size, rev;
511 rrpriv = (struct rr_private *)dev->priv;
512 regs = rrpriv->regs;
514 rev = readl(&regs->FwRev);
515 rrpriv->fw_rev = rev;
516 if (rev > 0x00020024)
517 printk(" Firmware revision: %i.%i.%i\n", (rev >> 16),
518 ((rev >> 8) & 0xff), (rev & 0xff));
519 else if (rev >= 0x00020000) {
520 printk(" Firmware revision: %i.%i.%i (2.0.37 or "
521 "later is recommended)\n", (rev >> 16),
522 ((rev >> 8) & 0xff), (rev & 0xff));
523 }else{
524 printk(" Firmware revision too old: %i.%i.%i, please "
525 "upgrade to 2.0.37 or later.\n",
526 (rev >> 16), ((rev >> 8) & 0xff), (rev & 0xff));
529 #if (DEBUG > 2)
530 printk(" Maximum receive rings %i\n", readl(&regs->MaxRxRng));
531 #endif
533 sram_size = rr_read_eeprom_word(rrpriv, (void *)8);
534 printk(" SRAM size 0x%06x\n", sram_size);
536 if (sysctl_rmem_max < 262144){
537 printk(" Receive socket buffer limit too low (%i), "
538 "setting to 262144\n", sysctl_rmem_max);
539 sysctl_rmem_max = 262144;
542 if (sysctl_wmem_max < 262144){
543 printk(" Transmit socket buffer limit too low (%i), "
544 "setting to 262144\n", sysctl_wmem_max);
545 sysctl_wmem_max = 262144;
548 rrpriv->next = root_dev;
549 root_dev = dev;
551 return 0;
555 static int rr_init1(struct net_device *dev)
557 struct rr_private *rrpriv;
558 struct rr_regs *regs;
559 u32 hostctrl;
560 unsigned long myjif, flags;
561 struct cmd cmd;
562 short i;
564 rrpriv = (struct rr_private *)dev->priv;
565 regs = rrpriv->regs;
567 spin_lock_irqsave(&rrpriv->lock, flags);
569 hostctrl = readl(&regs->HostCtrl);
570 writel(hostctrl | HALT_NIC | RR_CLEAR_INT, &regs->HostCtrl);
571 mb();
573 if (hostctrl & PARITY_ERR){
574 printk("%s: Parity error halting NIC - this is serious!\n",
575 dev->name);
576 spin_unlock_irqrestore(&rrpriv->lock, flags);
577 return -EFAULT;
580 set_rxaddr(regs, rrpriv->rx_ctrl);
581 set_infoaddr(regs, rrpriv->info);
583 rrpriv->info->evt_ctrl.entry_size = sizeof(struct event);
584 rrpriv->info->evt_ctrl.entries = EVT_RING_ENTRIES;
585 rrpriv->info->evt_ctrl.mode = 0;
586 rrpriv->info->evt_ctrl.pi = 0;
587 set_rraddr(&rrpriv->info->evt_ctrl.rngptr, rrpriv->evt_ring);
589 rrpriv->info->cmd_ctrl.entry_size = sizeof(struct cmd);
590 rrpriv->info->cmd_ctrl.entries = CMD_RING_ENTRIES;
591 rrpriv->info->cmd_ctrl.mode = 0;
592 rrpriv->info->cmd_ctrl.pi = 15;
594 for (i = 0; i < CMD_RING_ENTRIES; i++) {
595 writel(0, &regs->CmdRing[i]);
598 for (i = 0; i < TX_RING_ENTRIES; i++) {
599 rrpriv->tx_ring[i].size = 0;
600 set_rraddr(&rrpriv->tx_ring[i].addr, 0);
601 rrpriv->tx_skbuff[i] = 0;
603 rrpriv->info->tx_ctrl.entry_size = sizeof(struct tx_desc);
604 rrpriv->info->tx_ctrl.entries = TX_RING_ENTRIES;
605 rrpriv->info->tx_ctrl.mode = 0;
606 rrpriv->info->tx_ctrl.pi = 0;
607 set_rraddr(&rrpriv->info->tx_ctrl.rngptr, rrpriv->tx_ring);
610 * Set dirty_tx before we start receiving interrupts, otherwise
611 * the interrupt handler might think it is supposed to process
612 * tx ints before we are up and running, which may cause a null
613 * pointer access in the int handler.
615 rrpriv->tx_full = 0;
616 rrpriv->cur_rx = 0;
617 rrpriv->dirty_rx = rrpriv->dirty_tx = 0;
619 rr_reset(dev);
621 writel(0x60, &regs->IntrTmr);
623 * These seem to have no real effect as the Firmware sets
624 * it's own default values
626 writel(0x10, &regs->WriteDmaThresh);
627 writel(0x20, &regs->ReadDmaThresh);
629 rrpriv->fw_running = 0;
630 mb();
632 hostctrl &= ~(HALT_NIC | INVALID_INST_B | PARITY_ERR);
633 writel(hostctrl, &regs->HostCtrl);
634 mb();
636 spin_unlock_irqrestore(&rrpriv->lock, flags);
638 udelay(1000);
641 * Now start the FirmWare.
643 cmd.code = C_START_FW;
644 cmd.ring = 0;
645 cmd.index = 0;
647 rr_issue_cmd(rrpriv, &cmd);
650 * Give the FirmWare time to chew on the `get running' command.
652 myjif = jiffies + 5 * HZ;
653 while ((jiffies < myjif) && !rrpriv->fw_running);
655 for (i = 0; i < RX_RING_ENTRIES; i++) {
656 struct sk_buff *skb;
658 rrpriv->rx_ring[i].mode = 0;
659 skb = alloc_skb(dev->mtu + HIPPI_HLEN, GFP_ATOMIC);
660 rrpriv->rx_skbuff[i] = skb;
662 * Sanity test to see if we conflict with the DMA
663 * limitations of the Roadrunner.
665 if ((((unsigned long)skb->data) & 0xfff) > ~65320)
666 printk("skb alloc error\n");
668 set_rraddr(&rrpriv->rx_ring[i].addr, skb->data);
669 rrpriv->rx_ring[i].size = dev->mtu + HIPPI_HLEN;
672 rrpriv->rx_ctrl[4].entry_size = sizeof(struct rx_desc);
673 rrpriv->rx_ctrl[4].entries = RX_RING_ENTRIES;
674 rrpriv->rx_ctrl[4].mode = 8;
675 rrpriv->rx_ctrl[4].pi = 0;
676 mb();
677 set_rraddr(&rrpriv->rx_ctrl[4].rngptr, rrpriv->rx_ring);
679 cmd.code = C_NEW_RNG;
680 cmd.ring = 4;
681 cmd.index = 0;
682 rr_issue_cmd(rrpriv, &cmd);
684 #if 0
686 u32 tmp;
687 tmp = readl(&regs->ExtIo);
688 writel(0x80, &regs->ExtIo);
690 i = jiffies + 1 * HZ;
691 while (jiffies < i);
692 writel(tmp, &regs->ExtIo);
694 #endif
695 dev->tbusy = 0;
696 dev->start = 1;
697 return 0;
702 * All events are considered to be slow (RX/TX ints do not generate
703 * events) and are handled here, outside the main interrupt handler,
704 * to reduce the size of the handler.
706 static u32 rr_handle_event(struct net_device *dev, u32 prodidx, u32 eidx)
708 struct rr_private *rrpriv;
709 struct rr_regs *regs;
710 u32 tmp;
712 rrpriv = (struct rr_private *)dev->priv;
713 regs = rrpriv->regs;
715 while (prodidx != eidx){
716 switch (rrpriv->evt_ring[eidx].code){
717 case E_NIC_UP:
718 tmp = readl(&regs->FwRev);
719 printk("%s: Firmware revision %i.%i.%i up and running\n",
720 dev->name, (tmp >> 16), ((tmp >> 8) & 0xff),
721 (tmp & 0xff));
722 rrpriv->fw_running = 1;
723 mb();
724 break;
725 case E_LINK_ON:
726 printk("%s: Optical link ON\n", dev->name);
727 break;
728 case E_LINK_OFF:
729 printk("%s: Optical link OFF\n", dev->name);
730 break;
731 case E_RX_IDLE:
732 printk("%s: RX data not moving\n", dev->name);
733 break;
734 case E_WATCHDOG:
735 printk("%s: The watchdog is here to see us\n",
736 dev->name);
737 break;
739 * TX events.
741 case E_CON_REJ:
742 printk("%s: Connection rejected\n", dev->name);
743 rrpriv->stats.tx_aborted_errors++;
744 break;
745 case E_CON_TMOUT:
746 printk("%s: Connection timeout\n", dev->name);
747 break;
748 case E_DISC_ERR:
749 printk("%s: HIPPI disconnect error\n", dev->name);
750 rrpriv->stats.tx_aborted_errors++;
751 break;
752 case E_TX_IDLE:
753 printk("%s: Transmitter idle\n", dev->name);
754 break;
755 case E_TX_LINK_DROP:
756 printk("%s: Link lost during transmit\n", dev->name);
757 rrpriv->stats.tx_aborted_errors++;
758 break;
760 * RX events.
762 case E_VAL_RNG: /* Should be ignored */
763 #if (DEBUG > 2)
764 printk("%s: RX ring valid event\n", dev->name);
765 #endif
766 writel(RX_RING_ENTRIES - 1, &regs->IpRxPi);
767 break;
768 case E_INV_RNG:
769 printk("%s: RX ring invalid event\n", dev->name);
770 break;
771 case E_RX_RNG_OUT:
772 printk("%s: Receive ring full\n", dev->name);
773 break;
775 case E_RX_PAR_ERR:
776 printk("%s: Receive parity error.\n", dev->name);
777 break;
778 case E_RX_LLRC_ERR:
779 printk("%s: Receive LLRC error.\n", dev->name);
780 break;
781 case E_PKT_LN_ERR:
782 printk("%s: Receive packet length error.\n",
783 dev->name);
784 break;
785 default:
786 printk("%s: Unhandled event 0x%02x\n",
787 dev->name, rrpriv->evt_ring[eidx].code);
789 eidx = (eidx + 1) % EVT_RING_ENTRIES;
792 rrpriv->info->evt_ctrl.pi = eidx;
793 mb();
794 return eidx;
798 static void rx_int(struct net_device *dev, u32 rxlimit, u32 index)
800 struct rr_private *rrpriv = (struct rr_private *)dev->priv;
801 u32 pkt_len;
802 struct rr_regs *regs = rrpriv->regs;
804 do {
805 pkt_len = rrpriv->rx_ring[index].size;
806 #if (DEBUG > 2)
807 printk("index %i, rxlimit %i\n", index, rxlimit);
808 printk("len %x, mode %x\n", pkt_len,
809 rrpriv->rx_ring[index].mode);
810 #endif
811 if (pkt_len > 0){
812 struct sk_buff *skb;
814 if (pkt_len < PKT_COPY_THRESHOLD) {
815 skb = alloc_skb(pkt_len, GFP_ATOMIC);
816 if (skb == NULL){
817 printk("%s: Out of memory deferring "
818 "packet\n", dev->name);
819 rrpriv->stats.rx_dropped++;
820 goto defer;
821 }else
822 memcpy(skb_put(skb, pkt_len),
823 rrpriv->rx_skbuff[index]->data,
824 pkt_len);
825 }else{
826 struct sk_buff *newskb;
828 newskb = alloc_skb(dev->mtu + HIPPI_HLEN,
829 GFP_ATOMIC);
830 if (newskb){
831 skb = rrpriv->rx_skbuff[index];
832 skb_put(skb, pkt_len);
833 rrpriv->rx_skbuff[index] = newskb;
834 set_rraddr(&rrpriv->rx_ring[index].addr, newskb->data);
835 }else{
836 printk("%s: Out of memory, deferring "
837 "packet\n", dev->name);
838 rrpriv->stats.rx_dropped++;
839 goto defer;
842 skb->dev = dev;
843 skb->protocol = hippi_type_trans(skb, dev);
845 netif_rx(skb); /* send it up */
847 rrpriv->stats.rx_packets++;
848 rrpriv->stats.rx_bytes += skb->len;
850 defer:
851 rrpriv->rx_ring[index].mode = 0;
852 rrpriv->rx_ring[index].size = dev->mtu + HIPPI_HLEN;
854 if ((index & 7) == 7)
855 writel(index, &regs->IpRxPi);
857 index = (index + 1) % RX_RING_ENTRIES;
858 } while(index != rxlimit);
860 rrpriv->cur_rx = index;
861 mb();
865 static void rr_interrupt(int irq, void *dev_id, struct pt_regs *ptregs)
867 struct rr_private *rrpriv;
868 struct rr_regs *regs;
869 struct net_device *dev = (struct net_device *)dev_id;
870 u32 prodidx, rxindex, eidx, txcsmr, rxlimit, txcon;
871 unsigned long flags;
873 rrpriv = (struct rr_private *)dev->priv;
874 regs = rrpriv->regs;
876 if (!(readl(&regs->HostCtrl) & RR_INT))
877 return;
879 spin_lock_irqsave(&rrpriv->lock, flags);
881 prodidx = readl(&regs->EvtPrd);
882 txcsmr = (prodidx >> 8) & 0xff;
883 rxlimit = (prodidx >> 16) & 0xff;
884 prodidx &= 0xff;
886 #if (DEBUG > 2)
887 printk("%s: interrupt, prodidx = %i, eidx = %i\n", dev->name,
888 prodidx, rrpriv->info->evt_ctrl.pi);
889 #endif
891 rxindex = rrpriv->cur_rx;
892 if (rxindex != rxlimit)
893 rx_int(dev, rxlimit, rxindex);
895 txcon = rrpriv->dirty_tx;
896 if (txcsmr != txcon) {
897 do {
898 rrpriv->stats.tx_packets++;
899 rrpriv->stats.tx_bytes +=rrpriv->tx_skbuff[txcon]->len;
900 dev_kfree_skb(rrpriv->tx_skbuff[txcon]);
902 rrpriv->tx_skbuff[txcon] = NULL;
903 rrpriv->tx_ring[txcon].size = 0;
904 set_rraddr(&rrpriv->tx_ring[txcon].addr, 0);
905 rrpriv->tx_ring[txcon].mode = 0;
907 txcon = (txcon + 1) % TX_RING_ENTRIES;
908 } while (txcsmr != txcon);
909 mb();
911 rrpriv->dirty_tx = txcon;
912 if (rrpriv->tx_full && dev->tbusy &&
913 (((rrpriv->info->tx_ctrl.pi + 1) % TX_RING_ENTRIES)
914 != rrpriv->dirty_tx)){
915 rrpriv->tx_full = 0;
916 dev->tbusy = 0;
917 mark_bh(NET_BH);
921 eidx = rrpriv->info->evt_ctrl.pi;
922 if (prodidx != eidx)
923 eidx = rr_handle_event(dev, prodidx, eidx);
925 eidx |= ((txcsmr << 8) | (rxlimit << 16));
926 writel(eidx, &regs->EvtCon);
927 mb();
929 spin_unlock_irqrestore(&rrpriv->lock, flags);
933 static int rr_open(struct net_device *dev)
935 struct rr_private *rrpriv;
936 struct rr_regs *regs;
937 int ecode = 0;
938 unsigned long flags;
940 rrpriv = (struct rr_private *)dev->priv;
941 regs = rrpriv->regs;
943 if (rrpriv->fw_rev < 0x00020000) {
944 printk(KERN_WARNING "%s: trying to configure device with "
945 "obsolete firmware\n", dev->name);
946 ecode = -EBUSY;
947 goto error;
950 rrpriv->rx_ctrl = kmalloc(256*sizeof(struct ring_ctrl),
951 GFP_KERNEL | GFP_DMA);
952 if (!rrpriv->rx_ctrl) {
953 ecode = -ENOMEM;
954 goto error;
957 rrpriv->info = kmalloc(sizeof(struct rr_info), GFP_KERNEL | GFP_DMA);
958 if (!rrpriv->info){
959 kfree(rrpriv->rx_ctrl);
960 ecode = -ENOMEM;
961 goto error;
963 memset(rrpriv->rx_ctrl, 0, 256 * sizeof(struct ring_ctrl));
964 memset(rrpriv->info, 0, sizeof(struct rr_info));
965 mb();
967 spin_lock_irqsave(&rrpriv->lock, flags);
968 writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT, &regs->HostCtrl);
969 spin_unlock_irqrestore(&rrpriv->lock, flags);
971 if (request_irq(dev->irq, rr_interrupt, SA_SHIRQ, rrpriv->name, dev))
973 printk(KERN_WARNING "%s: Requested IRQ %d is busy\n",
974 dev->name, dev->irq);
975 ecode = -EAGAIN;
976 goto error;
979 rr_init1(dev);
981 dev->tbusy = 0;
982 dev->start = 1;
984 MOD_INC_USE_COUNT;
985 return 0;
987 error:
988 spin_lock_irqsave(&rrpriv->lock, flags);
989 writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT, &regs->HostCtrl);
990 spin_unlock_irqrestore(&rrpriv->lock, flags);
992 dev->tbusy = 1;
993 dev->start = 0;
994 return -ENOMEM;
998 static void rr_dump(struct net_device *dev)
1000 struct rr_private *rrpriv;
1001 struct rr_regs *regs;
1002 u32 index, cons;
1003 short i;
1004 int len;
1006 rrpriv = (struct rr_private *)dev->priv;
1007 regs = rrpriv->regs;
1009 printk("%s: dumping NIC TX rings\n", dev->name);
1011 printk("RxPrd %08x, TxPrd %02x, EvtPrd %08x, TxPi %02x, TxCtrlPi %02x\n",
1012 readl(&regs->RxPrd), readl(&regs->TxPrd),
1013 readl(&regs->EvtPrd), readl(&regs->TxPi),
1014 rrpriv->info->tx_ctrl.pi);
1016 printk("Error code 0x%x\n", readl(&regs->Fail1));
1018 index = (((readl(&regs->EvtPrd) >> 8) & 0xff ) - 1) % EVT_RING_ENTRIES;
1019 cons = rrpriv->dirty_tx;
1020 printk("TX ring index %i, TX consumer %i\n",
1021 index, cons);
1023 if (rrpriv->tx_skbuff[index]){
1024 len = min(0x80, rrpriv->tx_skbuff[index]->len);
1025 printk("skbuff for index %i is valid - dumping data (0x%x bytes - DMA len 0x%x)\n", index, len, rrpriv->tx_ring[index].size);
1026 for (i = 0; i < len; i++){
1027 if (!(i & 7))
1028 printk("\n");
1029 printk("%02x ", (unsigned char) rrpriv->tx_skbuff[index]->data[i]);
1031 printk("\n");
1034 if (rrpriv->tx_skbuff[cons]){
1035 len = min(0x80, rrpriv->tx_skbuff[cons]->len);
1036 printk("skbuff for cons %i is valid - dumping data (0x%x bytes - skbuff len 0x%x)\n", cons, len, rrpriv->tx_skbuff[cons]->len);
1037 printk("mode 0x%x, size 0x%x,\n phys %08x (virt %08lx), skbuff-addr %08lx, truesize 0x%x\n",
1038 rrpriv->tx_ring[cons].mode,
1039 rrpriv->tx_ring[cons].size,
1040 rrpriv->tx_ring[cons].addr.addrlo,
1041 (unsigned long)bus_to_virt(rrpriv->tx_ring[cons].addr.addrlo),
1042 (unsigned long)rrpriv->tx_skbuff[cons]->data,
1043 (unsigned int)rrpriv->tx_skbuff[cons]->truesize);
1044 for (i = 0; i < len; i++){
1045 if (!(i & 7))
1046 printk("\n");
1047 printk("%02x ", (unsigned char)rrpriv->tx_ring[cons].size);
1049 printk("\n");
1052 printk("dumping TX ring info:\n");
1053 for (i = 0; i < TX_RING_ENTRIES; i++)
1054 printk("mode 0x%x, size 0x%x, phys-addr %08x\n",
1055 rrpriv->tx_ring[i].mode,
1056 rrpriv->tx_ring[i].size,
1057 rrpriv->tx_ring[i].addr.addrlo);
1062 static int rr_close(struct net_device *dev)
1064 struct rr_private *rrpriv;
1065 struct rr_regs *regs;
1066 u32 tmp;
1067 short i;
1069 dev->start = 0;
1070 set_bit(0, (void*)&dev->tbusy);
1072 rrpriv = (struct rr_private *)dev->priv;
1073 regs = rrpriv->regs;
1076 * Lock to make sure we are not cleaning up while another CPU
1077 * handling interrupts.
1079 spin_lock(&rrpriv->lock);
1081 tmp = readl(&regs->HostCtrl);
1082 if (tmp & NIC_HALTED){
1083 printk("%s: NIC already halted\n", dev->name);
1084 rr_dump(dev);
1085 }else{
1086 tmp |= HALT_NIC | RR_CLEAR_INT;
1087 writel(tmp, &regs->HostCtrl);
1088 mb();
1091 rrpriv->fw_running = 0;
1093 writel(0, &regs->TxPi);
1094 writel(0, &regs->IpRxPi);
1096 writel(0, &regs->EvtCon);
1097 writel(0, &regs->EvtPrd);
1099 for (i = 0; i < CMD_RING_ENTRIES; i++)
1100 writel(0, &regs->CmdRing[i]);
1102 rrpriv->info->tx_ctrl.entries = 0;
1103 rrpriv->info->cmd_ctrl.pi = 0;
1104 rrpriv->info->evt_ctrl.pi = 0;
1105 rrpriv->rx_ctrl[4].entries = 0;
1107 for (i = 0; i < TX_RING_ENTRIES; i++) {
1108 if (rrpriv->tx_skbuff[i]) {
1109 rrpriv->tx_ring[i].size = 0;
1110 set_rraddr(&rrpriv->tx_ring[i].addr, 0);
1111 dev_kfree_skb(rrpriv->tx_skbuff[i]);
1115 for (i = 0; i < RX_RING_ENTRIES; i++) {
1116 if (rrpriv->rx_skbuff[i]) {
1117 rrpriv->rx_ring[i].size = 0;
1118 set_rraddr(&rrpriv->rx_ring[i].addr, 0);
1119 dev_kfree_skb(rrpriv->rx_skbuff[i]);
1123 kfree(rrpriv->rx_ctrl);
1124 kfree(rrpriv->info);
1126 free_irq(dev->irq, dev);
1127 spin_unlock(&rrpriv->lock);
1129 MOD_DEC_USE_COUNT;
1130 return 0;
1134 static int rr_start_xmit(struct sk_buff *skb, struct net_device *dev)
1136 struct rr_private *rrpriv = (struct rr_private *)dev->priv;
1137 struct rr_regs *regs = rrpriv->regs;
1138 struct ring_ctrl *txctrl;
1139 unsigned long flags;
1140 u32 index, len = skb->len;
1141 u32 *ifield;
1142 struct sk_buff *new_skb;
1144 if (readl(&regs->Mode) & FATAL_ERR)
1145 printk("error codes Fail1 %02x, Fail2 %02x\n",
1146 readl(&regs->Fail1), readl(&regs->Fail2));
1149 * We probably need to deal with tbusy here to prevent overruns.
1152 if (skb_headroom(skb) < 8){
1153 printk("incoming skb too small - reallocating\n");
1154 if (!(new_skb = dev_alloc_skb(len + 8))) {
1155 dev_kfree_skb(skb);
1156 dev->tbusy = 0;
1157 return -EBUSY;
1159 skb_reserve(new_skb, 8);
1160 skb_put(new_skb, len);
1161 memcpy(new_skb->data, skb->data, len);
1162 dev_kfree_skb(skb);
1163 skb = new_skb;
1166 ifield = (u32 *)skb_push(skb, 8);
1168 ifield[0] = 0;
1169 ifield[1] = skb->private.ifield;
1172 * We don't need the lock before we are actually going to start
1173 * fiddling with the control blocks.
1175 spin_lock_irqsave(&rrpriv->lock, flags);
1177 txctrl = &rrpriv->info->tx_ctrl;
1179 index = txctrl->pi;
1181 rrpriv->tx_skbuff[index] = skb;
1182 set_rraddr(&rrpriv->tx_ring[index].addr, skb->data);
1183 rrpriv->tx_ring[index].size = len + 8; /* include IFIELD */
1184 rrpriv->tx_ring[index].mode = PACKET_START | PACKET_END;
1185 txctrl->pi = (index + 1) % TX_RING_ENTRIES;
1186 writel(txctrl->pi, &regs->TxPi);
1188 if (txctrl->pi == rrpriv->dirty_tx){
1189 rrpriv->tx_full = 1;
1190 set_bit(0, (void*)&dev->tbusy);
1193 spin_unlock_irqrestore(&rrpriv->lock, flags);
1195 dev->trans_start = jiffies;
1196 return 0;
1200 static struct net_device_stats *rr_get_stats(struct net_device *dev)
1202 struct rr_private *rrpriv;
1204 rrpriv = (struct rr_private *)dev->priv;
1206 return(&rrpriv->stats);
1211 * Read the firmware out of the EEPROM and put it into the SRAM
1212 * (or from user space - later)
1214 * This operation requires the NIC to be halted and is performed with
1215 * interrupts disabled and with the spinlock hold.
1217 static int rr_load_firmware(struct net_device *dev)
1219 struct rr_private *rrpriv;
1220 struct rr_regs *regs;
1221 unsigned long eptr, segptr;
1222 int i, j;
1223 u32 localctrl, sptr, len, tmp;
1224 u32 p2len, p2size, nr_seg, revision, io, sram_size;
1225 struct eeprom *hw = NULL;
1227 rrpriv = (struct rr_private *)dev->priv;
1228 regs = rrpriv->regs;
1230 if (dev->flags & IFF_UP)
1231 return -EBUSY;
1233 if (!(readl(&regs->HostCtrl) & NIC_HALTED)){
1234 printk("%s: Trying to load firmware to a running NIC.\n",
1235 dev->name);
1236 return -EBUSY;
1239 localctrl = readl(&regs->LocalCtrl);
1240 writel(0, &regs->LocalCtrl);
1242 writel(0, &regs->EvtPrd);
1243 writel(0, &regs->RxPrd);
1244 writel(0, &regs->TxPrd);
1247 * First wipe the entire SRAM, otherwise we might run into all
1248 * kinds of trouble ... sigh, this took almost all afternoon
1249 * to track down ;-(
1251 io = readl(&regs->ExtIo);
1252 writel(0, &regs->ExtIo);
1253 sram_size = rr_read_eeprom_word(rrpriv, (void *)8);
1255 for (i = 200; i < sram_size / 4; i++){
1256 writel(i * 4, &regs->WinBase);
1257 mb();
1258 writel(0, &regs->WinData);
1259 mb();
1261 writel(io, &regs->ExtIo);
1262 mb();
1264 eptr = (unsigned long)rr_read_eeprom_word(rrpriv,
1265 &hw->rncd_info.AddrRunCodeSegs);
1266 eptr = ((eptr & 0x1fffff) >> 3);
1268 p2len = rr_read_eeprom_word(rrpriv, (void *)(0x83*4));
1269 p2len = (p2len << 2);
1270 p2size = rr_read_eeprom_word(rrpriv, (void *)(0x84*4));
1271 p2size = ((p2size & 0x1fffff) >> 3);
1273 if ((eptr < p2size) || (eptr > (p2size + p2len))){
1274 printk("%s: eptr is invalid\n", dev->name);
1275 goto out;
1278 revision = rr_read_eeprom_word(rrpriv, &hw->manf.HeaderFmt);
1280 if (revision != 1){
1281 printk("%s: invalid firmware format (%i)\n",
1282 dev->name, revision);
1283 goto out;
1286 nr_seg = rr_read_eeprom_word(rrpriv, (void *)eptr);
1287 eptr +=4;
1288 #if (DEBUG > 1)
1289 printk("%s: nr_seg %i\n", dev->name, nr_seg);
1290 #endif
1292 for (i = 0; i < nr_seg; i++){
1293 sptr = rr_read_eeprom_word(rrpriv, (void *)eptr);
1294 eptr += 4;
1295 len = rr_read_eeprom_word(rrpriv, (void *)eptr);
1296 eptr += 4;
1297 segptr = (unsigned long)rr_read_eeprom_word(rrpriv, (void *)eptr);
1298 segptr = ((segptr & 0x1fffff) >> 3);
1299 eptr += 4;
1300 #if (DEBUG > 1)
1301 printk("%s: segment %i, sram address %06x, length %04x, segptr %06x\n",
1302 dev->name, i, sptr, len, segptr);
1303 #endif
1304 for (j = 0; j < len; j++){
1305 tmp = rr_read_eeprom_word(rrpriv, (void *)segptr);
1306 writel(sptr, &regs->WinBase);
1307 mb();
1308 writel(tmp, &regs->WinData);
1309 mb();
1310 segptr += 4;
1311 sptr += 4;
1315 out:
1316 writel(localctrl, &regs->LocalCtrl);
1317 mb();
1318 return 0;
1322 static int rr_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1324 struct rr_private *rrpriv;
1325 unsigned char *image, *oldimage;
1326 unsigned int i;
1327 int error = -EOPNOTSUPP;
1329 rrpriv = (struct rr_private *)dev->priv;
1331 spin_lock(&rrpriv->lock);
1333 switch(cmd){
1334 case SIOCRRGFW:
1335 if (!suser()){
1336 error = -EPERM;
1337 goto out;
1340 if (rrpriv->fw_running){
1341 printk("%s: Firmware already running\n", dev->name);
1342 error = -EPERM;
1343 goto out;
1346 image = kmalloc(EEPROM_WORDS * sizeof(u32), GFP_KERNEL);
1347 if (!image){
1348 printk(KERN_ERR "%s: Unable to allocate memory "
1349 "for EEPROM image\n", dev->name);
1350 error = -ENOMEM;
1351 goto out;
1353 i = rr_read_eeprom(rrpriv, 0, image, EEPROM_BYTES);
1354 if (i != EEPROM_BYTES){
1355 kfree(image);
1356 printk(KERN_ERR "%s: Error reading EEPROM\n",
1357 dev->name);
1358 error = -EFAULT;
1359 goto out;
1361 error = copy_to_user(rq->ifr_data, image, EEPROM_BYTES);
1362 if (error)
1363 error = -EFAULT;
1364 kfree(image);
1365 break;
1366 case SIOCRRPFW:
1367 if (!suser()){
1368 error = -EPERM;
1369 goto out;
1372 if (rrpriv->fw_running){
1373 printk("%s: Firmware already running\n", dev->name);
1374 error = -EPERM;
1375 goto out;
1378 image = kmalloc(EEPROM_WORDS * sizeof(u32), GFP_KERNEL);
1379 if (!image){
1380 printk(KERN_ERR "%s: Unable to allocate memory "
1381 "for EEPROM image\n", dev->name);
1382 error = -ENOMEM;
1383 goto out;
1386 oldimage = kmalloc(EEPROM_WORDS * sizeof(u32), GFP_KERNEL);
1387 if (!oldimage){
1388 printk(KERN_ERR "%s: Unable to allocate memory "
1389 "for old EEPROM image\n", dev->name);
1390 error = -ENOMEM;
1391 goto out;
1394 error = copy_from_user(image, rq->ifr_data, EEPROM_BYTES);
1395 if (error)
1396 error = -EFAULT;
1398 printk("%s: Updating EEPROM firmware\n", dev->name);
1400 error = write_eeprom(rrpriv, 0, image, EEPROM_BYTES);
1401 if (error)
1402 printk(KERN_ERR "%s: Error writing EEPROM\n",
1403 dev->name);
1405 i = rr_read_eeprom(rrpriv, 0, oldimage, EEPROM_BYTES);
1406 if (i != EEPROM_BYTES)
1407 printk(KERN_ERR "%s: Error reading back EEPROM "
1408 "image\n", dev->name);
1410 error = memcmp(image, oldimage, EEPROM_BYTES);
1411 if (error){
1412 printk(KERN_ERR "%s: Error verifying EEPROM image\n",
1413 dev->name);
1414 error = -EFAULT;
1416 kfree(image);
1417 kfree(oldimage);
1418 break;
1419 case SIOCRRID:
1420 error = put_user(0x52523032, (int *)(&rq->ifr_data[0]));
1421 if (error)
1422 error = -EFAULT;
1423 break;
1424 default:
1427 out:
1428 spin_unlock(&rrpriv->lock);
1429 return error;
1434 * Local variables:
1435 * compile-command: "gcc -D__SMP__ -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 -DCPU=686 -DMODULE -DMODVERSIONS -include ../../include/linux/modversions.h -c rrunner.c"
1436 * End: