axnet_cs: use netstats in net_device structure
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / rrunner.c
blob3dd8f1342f70c58eb83ffb01cbb0f40576666076
1 /*
2 * rrunner.c: Linux driver for the Essential RoadRunner HIPPI board.
4 * Copyright (C) 1998-2002 by Jes Sorensen, <jes@wildopensource.com>.
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.
17 * Thanks to Jayaram Bhat from ODS/Essential for fixing some of the
18 * stupid bugs in my code.
20 * Softnet support and various other patches from Val Henson of
21 * ODS/Essential.
23 * PCI DMA mapping code partly based on work by Francois Romieu.
27 #define DEBUG 1
28 #define RX_DMA_SKBUFF 1
29 #define PKT_COPY_THRESHOLD 512
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>
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>
52 #define rr_if_busy(dev) netif_queue_stopped(dev)
53 #define rr_if_running(dev) netif_running(dev)
55 #include "rrunner.h"
57 #define RUN_AT(x) (jiffies + (x))
60 MODULE_AUTHOR("Jes Sorensen <jes@wildopensource.com>");
61 MODULE_DESCRIPTION("Essential RoadRunner HIPPI driver");
62 MODULE_LICENSE("GPL");
64 static char version[] __devinitdata = "rrunner.c: v0.50 11/11/2002 Jes Sorensen (jes@wildopensource.com)\n";
67 * Implementation notes:
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.
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.
81 static int __devinit rr_init_one(struct pci_dev *pdev,
82 const struct pci_device_id *ent)
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;
92 dev = alloc_hippi_dev(sizeof(struct rr_private));
93 if (!dev)
94 goto out3;
96 ret = pci_enable_device(pdev);
97 if (ret) {
98 ret = -ENODEV;
99 goto out2;
102 rrpriv = netdev_priv(dev);
104 SET_NETDEV_DEV(dev, &pdev->dev);
106 if (pci_request_regions(pdev, "rrunner")) {
107 ret = -EIO;
108 goto out;
111 pci_set_drvdata(pdev, dev);
113 rrpriv->pci_dev = pdev;
115 spin_lock_init(&rrpriv->lock);
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;
123 dev->base_addr = pci_resource_start(pdev, 0);
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);
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);
139 pci_set_master(pdev);
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);
146 * Remap the regs into kernel space.
149 rrpriv->regs = ioremap(dev->base_addr, 0x1000);
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;
158 tmpptr = pci_alloc_consistent(pdev, TX_TOTAL_SIZE, &ring_dma);
159 rrpriv->tx_ring = tmpptr;
160 rrpriv->tx_ring_dma = ring_dma;
162 if (!tmpptr) {
163 ret = -ENOMEM;
164 goto out;
167 tmpptr = pci_alloc_consistent(pdev, RX_TOTAL_SIZE, &ring_dma);
168 rrpriv->rx_ring = tmpptr;
169 rrpriv->rx_ring_dma = ring_dma;
171 if (!tmpptr) {
172 ret = -ENOMEM;
173 goto out;
176 tmpptr = pci_alloc_consistent(pdev, EVT_RING_SIZE, &ring_dma);
177 rrpriv->evt_ring = tmpptr;
178 rrpriv->evt_ring_dma = ring_dma;
180 if (!tmpptr) {
181 ret = -ENOMEM;
182 goto out;
186 * Don't access any register before this point!
188 #ifdef __BIG_ENDIAN
189 writel(readl(&rrpriv->regs->HostCtrl) | NO_SWAP,
190 &rrpriv->regs->HostCtrl);
191 #endif
193 * Need to add a case for little-endian 64-bit hosts here.
196 rr_init(dev);
198 dev->base_addr = 0;
200 ret = register_netdev(dev);
201 if (ret)
202 goto out;
203 return 0;
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);
218 out2:
219 free_netdev(dev);
220 out3:
221 return ret;
224 static void __devexit rr_remove_one (struct pci_dev *pdev)
226 struct net_device *dev = pci_get_drvdata(pdev);
228 if (dev) {
229 struct rr_private *rr = netdev_priv(dev);
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);
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);
254 * Commands are considered to be slow, thus there is no reason to
255 * inline this.
257 static void rr_issue_cmd(struct rr_private *rrpriv, struct cmd *cmd)
259 struct rr_regs __iomem *regs;
260 u32 idx;
262 regs = rrpriv->regs;
264 * This is temporary - it will go away in the final version.
265 * We probably also want to make this function inline.
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));
275 idx = rrpriv->info->cmd_ctrl.pi;
277 writel(*(u32*)(cmd), &regs->CmdRing[idx]);
278 wmb();
280 idx = (idx - 1) % CMD_RING_ENTRIES;
281 rrpriv->info->cmd_ctrl.pi = idx;
282 wmb();
284 if (readl(&regs->Mode) & FATAL_ERR)
285 printk("error code %02x\n", readl(&regs->Fail1));
290 * Reset the board in a sensible manner. The NIC is already halted
291 * when we get here and a spin-lock is held.
293 static int rr_reset(struct net_device *dev)
295 struct rr_private *rrpriv;
296 struct rr_regs __iomem *regs;
297 u32 start_pc;
298 int i;
300 rrpriv = netdev_priv(dev);
301 regs = rrpriv->regs;
303 rr_load_firmware(dev);
305 writel(0x01000000, &regs->TX_state);
306 writel(0xff800000, &regs->RX_state);
307 writel(0, &regs->AssistState);
308 writel(CLEAR_INTA, &regs->LocalCtrl);
309 writel(0x01, &regs->BrkPt);
310 writel(0, &regs->Timer);
311 writel(0, &regs->TimerRef);
312 writel(RESET_DMA, &regs->DmaReadState);
313 writel(RESET_DMA, &regs->DmaWriteState);
314 writel(0, &regs->DmaWriteHostHi);
315 writel(0, &regs->DmaWriteHostLo);
316 writel(0, &regs->DmaReadHostHi);
317 writel(0, &regs->DmaReadHostLo);
318 writel(0, &regs->DmaReadLen);
319 writel(0, &regs->DmaWriteLen);
320 writel(0, &regs->DmaWriteLcl);
321 writel(0, &regs->DmaWriteIPchecksum);
322 writel(0, &regs->DmaReadLcl);
323 writel(0, &regs->DmaReadIPchecksum);
324 writel(0, &regs->PciState);
325 #if (BITS_PER_LONG == 64) && defined __LITTLE_ENDIAN
326 writel(SWAP_DATA | PTR64BIT | PTR_WD_SWAP, &regs->Mode);
327 #elif (BITS_PER_LONG == 64)
328 writel(SWAP_DATA | PTR64BIT | PTR_WD_NOSWAP, &regs->Mode);
329 #else
330 writel(SWAP_DATA | PTR32BIT | PTR_WD_NOSWAP, &regs->Mode);
331 #endif
333 #if 0
335 * Don't worry, this is just black magic.
337 writel(0xdf000, &regs->RxBase);
338 writel(0xdf000, &regs->RxPrd);
339 writel(0xdf000, &regs->RxCon);
340 writel(0xce000, &regs->TxBase);
341 writel(0xce000, &regs->TxPrd);
342 writel(0xce000, &regs->TxCon);
343 writel(0, &regs->RxIndPro);
344 writel(0, &regs->RxIndCon);
345 writel(0, &regs->RxIndRef);
346 writel(0, &regs->TxIndPro);
347 writel(0, &regs->TxIndCon);
348 writel(0, &regs->TxIndRef);
349 writel(0xcc000, &regs->pad10[0]);
350 writel(0, &regs->DrCmndPro);
351 writel(0, &regs->DrCmndCon);
352 writel(0, &regs->DwCmndPro);
353 writel(0, &regs->DwCmndCon);
354 writel(0, &regs->DwCmndRef);
355 writel(0, &regs->DrDataPro);
356 writel(0, &regs->DrDataCon);
357 writel(0, &regs->DrDataRef);
358 writel(0, &regs->DwDataPro);
359 writel(0, &regs->DwDataCon);
360 writel(0, &regs->DwDataRef);
361 #endif
363 writel(0xffffffff, &regs->MbEvent);
364 writel(0, &regs->Event);
366 writel(0, &regs->TxPi);
367 writel(0, &regs->IpRxPi);
369 writel(0, &regs->EvtCon);
370 writel(0, &regs->EvtPrd);
372 rrpriv->info->evt_ctrl.pi = 0;
374 for (i = 0; i < CMD_RING_ENTRIES; i++)
375 writel(0, &regs->CmdRing[i]);
378 * Why 32 ? is this not cache line size dependent?
380 writel(RBURST_64|WBURST_64, &regs->PciState);
381 wmb();
383 start_pc = rr_read_eeprom_word(rrpriv,
384 offsetof(struct eeprom, rncd_info.FwStart));
386 #if (DEBUG > 1)
387 printk("%s: Executing firmware at address 0x%06x\n",
388 dev->name, start_pc);
389 #endif
391 writel(start_pc + 0x800, &regs->Pc);
392 wmb();
393 udelay(5);
395 writel(start_pc, &regs->Pc);
396 wmb();
398 return 0;
403 * Read a string from the EEPROM.
405 static unsigned int rr_read_eeprom(struct rr_private *rrpriv,
406 unsigned long offset,
407 unsigned char *buf,
408 unsigned long length)
410 struct rr_regs __iomem *regs = rrpriv->regs;
411 u32 misc, io, host, i;
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();
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();
428 writel(host, &regs->HostCtrl);
429 writel(misc, &regs->LocalCtrl);
430 writel(io, &regs->ExtIo);
431 mb();
432 return i;
437 * Shortcut to read one word (4 bytes) out of the EEPROM and convert
438 * it to our CPU byte-order.
440 static u32 rr_read_eeprom_word(struct rr_private *rrpriv,
441 size_t offset)
443 __be32 word;
445 if ((rr_read_eeprom(rrpriv, offset,
446 (unsigned char *)&word, 4) == 4))
447 return be32_to_cpu(word);
448 return 0;
453 * Write a string to the EEPROM.
455 * This is only called when the firmware is not running.
457 static unsigned int write_eeprom(struct rr_private *rrpriv,
458 unsigned long offset,
459 unsigned char *buf,
460 unsigned long length)
462 struct rr_regs __iomem *regs = rrpriv->regs;
463 u32 misc, io, data, i, j, ready, error = 0;
465 io = readl(&regs->ExtIo);
466 writel(0, &regs->ExtIo);
467 misc = readl(&regs->LocalCtrl);
468 writel(ENABLE_EEPROM_WRITE, &regs->LocalCtrl);
469 mb();
471 for (i = 0; i < length; i++){
472 writel((EEPROM_BASE + ((offset+i) << 3)), &regs->WinBase);
473 mb();
474 data = buf[i] << 24;
476 * Only try to write the data if it is not the same
477 * value already.
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;
501 writel(misc, &regs->LocalCtrl);
502 writel(io, &regs->ExtIo);
503 mb();
505 return error;
509 static int __devinit rr_init(struct net_device *dev)
511 struct rr_private *rrpriv;
512 struct rr_regs __iomem *regs;
513 u32 sram_size, rev;
514 DECLARE_MAC_BUF(mac);
516 rrpriv = netdev_priv(dev);
517 regs = rrpriv->regs;
519 rev = readl(&regs->FwRev);
520 rrpriv->fw_rev = rev;
521 if (rev > 0x00020024)
522 printk(" Firmware revision: %i.%i.%i\n", (rev >> 16),
523 ((rev >> 8) & 0xff), (rev & 0xff));
524 else if (rev >= 0x00020000) {
525 printk(" Firmware revision: %i.%i.%i (2.0.37 or "
526 "later is recommended)\n", (rev >> 16),
527 ((rev >> 8) & 0xff), (rev & 0xff));
528 }else{
529 printk(" Firmware revision too old: %i.%i.%i, please "
530 "upgrade to 2.0.37 or later.\n",
531 (rev >> 16), ((rev >> 8) & 0xff), (rev & 0xff));
534 #if (DEBUG > 2)
535 printk(" Maximum receive rings %i\n", readl(&regs->MaxRxRng));
536 #endif
539 * Read the hardware address from the eeprom. The HW address
540 * is not really necessary for HIPPI but awfully convenient.
541 * The pointer arithmetic to put it in dev_addr is ugly, but
542 * Donald Becker does it this way for the GigE version of this
543 * card and it's shorter and more portable than any
544 * other method I've seen. -VAL
547 *(__be16 *)(dev->dev_addr) =
548 htons(rr_read_eeprom_word(rrpriv, offsetof(struct eeprom, manf.BoardULA)));
549 *(__be32 *)(dev->dev_addr+2) =
550 htonl(rr_read_eeprom_word(rrpriv, offsetof(struct eeprom, manf.BoardULA[4])));
552 printk(" MAC: %s\n", print_mac(mac, dev->dev_addr));
554 sram_size = rr_read_eeprom_word(rrpriv, 8);
555 printk(" SRAM size 0x%06x\n", sram_size);
557 return 0;
561 static int rr_init1(struct net_device *dev)
563 struct rr_private *rrpriv;
564 struct rr_regs __iomem *regs;
565 unsigned long myjif, flags;
566 struct cmd cmd;
567 u32 hostctrl;
568 int ecode = 0;
569 short i;
571 rrpriv = netdev_priv(dev);
572 regs = rrpriv->regs;
574 spin_lock_irqsave(&rrpriv->lock, flags);
576 hostctrl = readl(&regs->HostCtrl);
577 writel(hostctrl | HALT_NIC | RR_CLEAR_INT, &regs->HostCtrl);
578 wmb();
580 if (hostctrl & PARITY_ERR){
581 printk("%s: Parity error halting NIC - this is serious!\n",
582 dev->name);
583 spin_unlock_irqrestore(&rrpriv->lock, flags);
584 ecode = -EFAULT;
585 goto error;
588 set_rxaddr(regs, rrpriv->rx_ctrl_dma);
589 set_infoaddr(regs, rrpriv->info_dma);
591 rrpriv->info->evt_ctrl.entry_size = sizeof(struct event);
592 rrpriv->info->evt_ctrl.entries = EVT_RING_ENTRIES;
593 rrpriv->info->evt_ctrl.mode = 0;
594 rrpriv->info->evt_ctrl.pi = 0;
595 set_rraddr(&rrpriv->info->evt_ctrl.rngptr, rrpriv->evt_ring_dma);
597 rrpriv->info->cmd_ctrl.entry_size = sizeof(struct cmd);
598 rrpriv->info->cmd_ctrl.entries = CMD_RING_ENTRIES;
599 rrpriv->info->cmd_ctrl.mode = 0;
600 rrpriv->info->cmd_ctrl.pi = 15;
602 for (i = 0; i < CMD_RING_ENTRIES; i++) {
603 writel(0, &regs->CmdRing[i]);
606 for (i = 0; i < TX_RING_ENTRIES; i++) {
607 rrpriv->tx_ring[i].size = 0;
608 set_rraddr(&rrpriv->tx_ring[i].addr, 0);
609 rrpriv->tx_skbuff[i] = NULL;
611 rrpriv->info->tx_ctrl.entry_size = sizeof(struct tx_desc);
612 rrpriv->info->tx_ctrl.entries = TX_RING_ENTRIES;
613 rrpriv->info->tx_ctrl.mode = 0;
614 rrpriv->info->tx_ctrl.pi = 0;
615 set_rraddr(&rrpriv->info->tx_ctrl.rngptr, rrpriv->tx_ring_dma);
618 * Set dirty_tx before we start receiving interrupts, otherwise
619 * the interrupt handler might think it is supposed to process
620 * tx ints before we are up and running, which may cause a null
621 * pointer access in the int handler.
623 rrpriv->tx_full = 0;
624 rrpriv->cur_rx = 0;
625 rrpriv->dirty_rx = rrpriv->dirty_tx = 0;
627 rr_reset(dev);
629 /* Tuning values */
630 writel(0x5000, &regs->ConRetry);
631 writel(0x100, &regs->ConRetryTmr);
632 writel(0x500000, &regs->ConTmout);
633 writel(0x60, &regs->IntrTmr);
634 writel(0x500000, &regs->TxDataMvTimeout);
635 writel(0x200000, &regs->RxDataMvTimeout);
636 writel(0x80, &regs->WriteDmaThresh);
637 writel(0x80, &regs->ReadDmaThresh);
639 rrpriv->fw_running = 0;
640 wmb();
642 hostctrl &= ~(HALT_NIC | INVALID_INST_B | PARITY_ERR);
643 writel(hostctrl, &regs->HostCtrl);
644 wmb();
646 spin_unlock_irqrestore(&rrpriv->lock, flags);
648 for (i = 0; i < RX_RING_ENTRIES; i++) {
649 struct sk_buff *skb;
650 dma_addr_t addr;
652 rrpriv->rx_ring[i].mode = 0;
653 skb = alloc_skb(dev->mtu + HIPPI_HLEN, GFP_ATOMIC);
654 if (!skb) {
655 printk(KERN_WARNING "%s: Unable to allocate memory "
656 "for receive ring - halting NIC\n", dev->name);
657 ecode = -ENOMEM;
658 goto error;
660 rrpriv->rx_skbuff[i] = skb;
661 addr = pci_map_single(rrpriv->pci_dev, skb->data,
662 dev->mtu + HIPPI_HLEN, PCI_DMA_FROMDEVICE);
664 * Sanity test to see if we conflict with the DMA
665 * limitations of the Roadrunner.
667 if ((((unsigned long)skb->data) & 0xfff) > ~65320)
668 printk("skb alloc error\n");
670 set_rraddr(&rrpriv->rx_ring[i].addr, addr);
671 rrpriv->rx_ring[i].size = dev->mtu + HIPPI_HLEN;
674 rrpriv->rx_ctrl[4].entry_size = sizeof(struct rx_desc);
675 rrpriv->rx_ctrl[4].entries = RX_RING_ENTRIES;
676 rrpriv->rx_ctrl[4].mode = 8;
677 rrpriv->rx_ctrl[4].pi = 0;
678 wmb();
679 set_rraddr(&rrpriv->rx_ctrl[4].rngptr, rrpriv->rx_ring_dma);
681 udelay(1000);
684 * Now start the FirmWare.
686 cmd.code = C_START_FW;
687 cmd.ring = 0;
688 cmd.index = 0;
690 rr_issue_cmd(rrpriv, &cmd);
693 * Give the FirmWare time to chew on the `get running' command.
695 myjif = jiffies + 5 * HZ;
696 while (time_before(jiffies, myjif) && !rrpriv->fw_running)
697 cpu_relax();
699 netif_start_queue(dev);
701 return ecode;
703 error:
705 * We might have gotten here because we are out of memory,
706 * make sure we release everything we allocated before failing
708 for (i = 0; i < RX_RING_ENTRIES; i++) {
709 struct sk_buff *skb = rrpriv->rx_skbuff[i];
711 if (skb) {
712 pci_unmap_single(rrpriv->pci_dev,
713 rrpriv->rx_ring[i].addr.addrlo,
714 dev->mtu + HIPPI_HLEN,
715 PCI_DMA_FROMDEVICE);
716 rrpriv->rx_ring[i].size = 0;
717 set_rraddr(&rrpriv->rx_ring[i].addr, 0);
718 dev_kfree_skb(skb);
719 rrpriv->rx_skbuff[i] = NULL;
722 return ecode;
727 * All events are considered to be slow (RX/TX ints do not generate
728 * events) and are handled here, outside the main interrupt handler,
729 * to reduce the size of the handler.
731 static u32 rr_handle_event(struct net_device *dev, u32 prodidx, u32 eidx)
733 struct rr_private *rrpriv;
734 struct rr_regs __iomem *regs;
735 u32 tmp;
737 rrpriv = netdev_priv(dev);
738 regs = rrpriv->regs;
740 while (prodidx != eidx){
741 switch (rrpriv->evt_ring[eidx].code){
742 case E_NIC_UP:
743 tmp = readl(&regs->FwRev);
744 printk(KERN_INFO "%s: Firmware revision %i.%i.%i "
745 "up and running\n", dev->name,
746 (tmp >> 16), ((tmp >> 8) & 0xff), (tmp & 0xff));
747 rrpriv->fw_running = 1;
748 writel(RX_RING_ENTRIES - 1, &regs->IpRxPi);
749 wmb();
750 break;
751 case E_LINK_ON:
752 printk(KERN_INFO "%s: Optical link ON\n", dev->name);
753 break;
754 case E_LINK_OFF:
755 printk(KERN_INFO "%s: Optical link OFF\n", dev->name);
756 break;
757 case E_RX_IDLE:
758 printk(KERN_WARNING "%s: RX data not moving\n",
759 dev->name);
760 goto drop;
761 case E_WATCHDOG:
762 printk(KERN_INFO "%s: The watchdog is here to see "
763 "us\n", dev->name);
764 break;
765 case E_INTERN_ERR:
766 printk(KERN_ERR "%s: HIPPI Internal NIC error\n",
767 dev->name);
768 writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
769 &regs->HostCtrl);
770 wmb();
771 break;
772 case E_HOST_ERR:
773 printk(KERN_ERR "%s: Host software error\n",
774 dev->name);
775 writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
776 &regs->HostCtrl);
777 wmb();
778 break;
780 * TX events.
782 case E_CON_REJ:
783 printk(KERN_WARNING "%s: Connection rejected\n",
784 dev->name);
785 dev->stats.tx_aborted_errors++;
786 break;
787 case E_CON_TMOUT:
788 printk(KERN_WARNING "%s: Connection timeout\n",
789 dev->name);
790 break;
791 case E_DISC_ERR:
792 printk(KERN_WARNING "%s: HIPPI disconnect error\n",
793 dev->name);
794 dev->stats.tx_aborted_errors++;
795 break;
796 case E_INT_PRTY:
797 printk(KERN_ERR "%s: HIPPI Internal Parity error\n",
798 dev->name);
799 writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
800 &regs->HostCtrl);
801 wmb();
802 break;
803 case E_TX_IDLE:
804 printk(KERN_WARNING "%s: Transmitter idle\n",
805 dev->name);
806 break;
807 case E_TX_LINK_DROP:
808 printk(KERN_WARNING "%s: Link lost during transmit\n",
809 dev->name);
810 dev->stats.tx_aborted_errors++;
811 writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
812 &regs->HostCtrl);
813 wmb();
814 break;
815 case E_TX_INV_RNG:
816 printk(KERN_ERR "%s: Invalid send ring block\n",
817 dev->name);
818 writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
819 &regs->HostCtrl);
820 wmb();
821 break;
822 case E_TX_INV_BUF:
823 printk(KERN_ERR "%s: Invalid send buffer address\n",
824 dev->name);
825 writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
826 &regs->HostCtrl);
827 wmb();
828 break;
829 case E_TX_INV_DSC:
830 printk(KERN_ERR "%s: Invalid descriptor address\n",
831 dev->name);
832 writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
833 &regs->HostCtrl);
834 wmb();
835 break;
837 * RX events.
839 case E_RX_RNG_OUT:
840 printk(KERN_INFO "%s: Receive ring full\n", dev->name);
841 break;
843 case E_RX_PAR_ERR:
844 printk(KERN_WARNING "%s: Receive parity error\n",
845 dev->name);
846 goto drop;
847 case E_RX_LLRC_ERR:
848 printk(KERN_WARNING "%s: Receive LLRC error\n",
849 dev->name);
850 goto drop;
851 case E_PKT_LN_ERR:
852 printk(KERN_WARNING "%s: Receive packet length "
853 "error\n", dev->name);
854 goto drop;
855 case E_DTA_CKSM_ERR:
856 printk(KERN_WARNING "%s: Data checksum error\n",
857 dev->name);
858 goto drop;
859 case E_SHT_BST:
860 printk(KERN_WARNING "%s: Unexpected short burst "
861 "error\n", dev->name);
862 goto drop;
863 case E_STATE_ERR:
864 printk(KERN_WARNING "%s: Recv. state transition"
865 " error\n", dev->name);
866 goto drop;
867 case E_UNEXP_DATA:
868 printk(KERN_WARNING "%s: Unexpected data error\n",
869 dev->name);
870 goto drop;
871 case E_LST_LNK_ERR:
872 printk(KERN_WARNING "%s: Link lost error\n",
873 dev->name);
874 goto drop;
875 case E_FRM_ERR:
876 printk(KERN_WARNING "%s: Framming Error\n",
877 dev->name);
878 goto drop;
879 case E_FLG_SYN_ERR:
880 printk(KERN_WARNING "%s: Flag sync. lost during "
881 "packet\n", dev->name);
882 goto drop;
883 case E_RX_INV_BUF:
884 printk(KERN_ERR "%s: Invalid receive buffer "
885 "address\n", dev->name);
886 writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
887 &regs->HostCtrl);
888 wmb();
889 break;
890 case E_RX_INV_DSC:
891 printk(KERN_ERR "%s: Invalid receive descriptor "
892 "address\n", dev->name);
893 writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
894 &regs->HostCtrl);
895 wmb();
896 break;
897 case E_RNG_BLK:
898 printk(KERN_ERR "%s: Invalid ring block\n",
899 dev->name);
900 writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
901 &regs->HostCtrl);
902 wmb();
903 break;
904 drop:
905 /* Label packet to be dropped.
906 * Actual dropping occurs in rx
907 * handling.
909 * The index of packet we get to drop is
910 * the index of the packet following
911 * the bad packet. -kbf
914 u16 index = rrpriv->evt_ring[eidx].index;
915 index = (index + (RX_RING_ENTRIES - 1)) %
916 RX_RING_ENTRIES;
917 rrpriv->rx_ring[index].mode |=
918 (PACKET_BAD | PACKET_END);
920 break;
921 default:
922 printk(KERN_WARNING "%s: Unhandled event 0x%02x\n",
923 dev->name, rrpriv->evt_ring[eidx].code);
925 eidx = (eidx + 1) % EVT_RING_ENTRIES;
928 rrpriv->info->evt_ctrl.pi = eidx;
929 wmb();
930 return eidx;
934 static void rx_int(struct net_device *dev, u32 rxlimit, u32 index)
936 struct rr_private *rrpriv = netdev_priv(dev);
937 struct rr_regs __iomem *regs = rrpriv->regs;
939 do {
940 struct rx_desc *desc;
941 u32 pkt_len;
943 desc = &(rrpriv->rx_ring[index]);
944 pkt_len = desc->size;
945 #if (DEBUG > 2)
946 printk("index %i, rxlimit %i\n", index, rxlimit);
947 printk("len %x, mode %x\n", pkt_len, desc->mode);
948 #endif
949 if ( (rrpriv->rx_ring[index].mode & PACKET_BAD) == PACKET_BAD){
950 dev->stats.rx_dropped++;
951 goto defer;
954 if (pkt_len > 0){
955 struct sk_buff *skb, *rx_skb;
957 rx_skb = rrpriv->rx_skbuff[index];
959 if (pkt_len < PKT_COPY_THRESHOLD) {
960 skb = alloc_skb(pkt_len, GFP_ATOMIC);
961 if (skb == NULL){
962 printk(KERN_WARNING "%s: Unable to allocate skb (%i bytes), deferring packet\n", dev->name, pkt_len);
963 dev->stats.rx_dropped++;
964 goto defer;
965 } else {
966 pci_dma_sync_single_for_cpu(rrpriv->pci_dev,
967 desc->addr.addrlo,
968 pkt_len,
969 PCI_DMA_FROMDEVICE);
971 memcpy(skb_put(skb, pkt_len),
972 rx_skb->data, pkt_len);
974 pci_dma_sync_single_for_device(rrpriv->pci_dev,
975 desc->addr.addrlo,
976 pkt_len,
977 PCI_DMA_FROMDEVICE);
979 }else{
980 struct sk_buff *newskb;
982 newskb = alloc_skb(dev->mtu + HIPPI_HLEN,
983 GFP_ATOMIC);
984 if (newskb){
985 dma_addr_t addr;
987 pci_unmap_single(rrpriv->pci_dev,
988 desc->addr.addrlo, dev->mtu +
989 HIPPI_HLEN, PCI_DMA_FROMDEVICE);
990 skb = rx_skb;
991 skb_put(skb, pkt_len);
992 rrpriv->rx_skbuff[index] = newskb;
993 addr = pci_map_single(rrpriv->pci_dev,
994 newskb->data,
995 dev->mtu + HIPPI_HLEN,
996 PCI_DMA_FROMDEVICE);
997 set_rraddr(&desc->addr, addr);
998 } else {
999 printk("%s: Out of memory, deferring "
1000 "packet\n", dev->name);
1001 dev->stats.rx_dropped++;
1002 goto defer;
1005 skb->protocol = hippi_type_trans(skb, dev);
1007 netif_rx(skb); /* send it up */
1009 dev->last_rx = jiffies;
1010 dev->stats.rx_packets++;
1011 dev->stats.rx_bytes += pkt_len;
1013 defer:
1014 desc->mode = 0;
1015 desc->size = dev->mtu + HIPPI_HLEN;
1017 if ((index & 7) == 7)
1018 writel(index, &regs->IpRxPi);
1020 index = (index + 1) % RX_RING_ENTRIES;
1021 } while(index != rxlimit);
1023 rrpriv->cur_rx = index;
1024 wmb();
1028 static irqreturn_t rr_interrupt(int irq, void *dev_id)
1030 struct rr_private *rrpriv;
1031 struct rr_regs __iomem *regs;
1032 struct net_device *dev = (struct net_device *)dev_id;
1033 u32 prodidx, rxindex, eidx, txcsmr, rxlimit, txcon;
1035 rrpriv = netdev_priv(dev);
1036 regs = rrpriv->regs;
1038 if (!(readl(&regs->HostCtrl) & RR_INT))
1039 return IRQ_NONE;
1041 spin_lock(&rrpriv->lock);
1043 prodidx = readl(&regs->EvtPrd);
1044 txcsmr = (prodidx >> 8) & 0xff;
1045 rxlimit = (prodidx >> 16) & 0xff;
1046 prodidx &= 0xff;
1048 #if (DEBUG > 2)
1049 printk("%s: interrupt, prodidx = %i, eidx = %i\n", dev->name,
1050 prodidx, rrpriv->info->evt_ctrl.pi);
1051 #endif
1053 * Order here is important. We must handle events
1054 * before doing anything else in order to catch
1055 * such things as LLRC errors, etc -kbf
1058 eidx = rrpriv->info->evt_ctrl.pi;
1059 if (prodidx != eidx)
1060 eidx = rr_handle_event(dev, prodidx, eidx);
1062 rxindex = rrpriv->cur_rx;
1063 if (rxindex != rxlimit)
1064 rx_int(dev, rxlimit, rxindex);
1066 txcon = rrpriv->dirty_tx;
1067 if (txcsmr != txcon) {
1068 do {
1069 /* Due to occational firmware TX producer/consumer out
1070 * of sync. error need to check entry in ring -kbf
1072 if(rrpriv->tx_skbuff[txcon]){
1073 struct tx_desc *desc;
1074 struct sk_buff *skb;
1076 desc = &(rrpriv->tx_ring[txcon]);
1077 skb = rrpriv->tx_skbuff[txcon];
1079 dev->stats.tx_packets++;
1080 dev->stats.tx_bytes += skb->len;
1082 pci_unmap_single(rrpriv->pci_dev,
1083 desc->addr.addrlo, skb->len,
1084 PCI_DMA_TODEVICE);
1085 dev_kfree_skb_irq(skb);
1087 rrpriv->tx_skbuff[txcon] = NULL;
1088 desc->size = 0;
1089 set_rraddr(&rrpriv->tx_ring[txcon].addr, 0);
1090 desc->mode = 0;
1092 txcon = (txcon + 1) % TX_RING_ENTRIES;
1093 } while (txcsmr != txcon);
1094 wmb();
1096 rrpriv->dirty_tx = txcon;
1097 if (rrpriv->tx_full && rr_if_busy(dev) &&
1098 (((rrpriv->info->tx_ctrl.pi + 1) % TX_RING_ENTRIES)
1099 != rrpriv->dirty_tx)){
1100 rrpriv->tx_full = 0;
1101 netif_wake_queue(dev);
1105 eidx |= ((txcsmr << 8) | (rxlimit << 16));
1106 writel(eidx, &regs->EvtCon);
1107 wmb();
1109 spin_unlock(&rrpriv->lock);
1110 return IRQ_HANDLED;
1113 static inline void rr_raz_tx(struct rr_private *rrpriv,
1114 struct net_device *dev)
1116 int i;
1118 for (i = 0; i < TX_RING_ENTRIES; i++) {
1119 struct sk_buff *skb = rrpriv->tx_skbuff[i];
1121 if (skb) {
1122 struct tx_desc *desc = &(rrpriv->tx_ring[i]);
1124 pci_unmap_single(rrpriv->pci_dev, desc->addr.addrlo,
1125 skb->len, PCI_DMA_TODEVICE);
1126 desc->size = 0;
1127 set_rraddr(&desc->addr, 0);
1128 dev_kfree_skb(skb);
1129 rrpriv->tx_skbuff[i] = NULL;
1135 static inline void rr_raz_rx(struct rr_private *rrpriv,
1136 struct net_device *dev)
1138 int i;
1140 for (i = 0; i < RX_RING_ENTRIES; i++) {
1141 struct sk_buff *skb = rrpriv->rx_skbuff[i];
1143 if (skb) {
1144 struct rx_desc *desc = &(rrpriv->rx_ring[i]);
1146 pci_unmap_single(rrpriv->pci_dev, desc->addr.addrlo,
1147 dev->mtu + HIPPI_HLEN, PCI_DMA_FROMDEVICE);
1148 desc->size = 0;
1149 set_rraddr(&desc->addr, 0);
1150 dev_kfree_skb(skb);
1151 rrpriv->rx_skbuff[i] = NULL;
1156 static void rr_timer(unsigned long data)
1158 struct net_device *dev = (struct net_device *)data;
1159 struct rr_private *rrpriv = netdev_priv(dev);
1160 struct rr_regs __iomem *regs = rrpriv->regs;
1161 unsigned long flags;
1163 if (readl(&regs->HostCtrl) & NIC_HALTED){
1164 printk("%s: Restarting nic\n", dev->name);
1165 memset(rrpriv->rx_ctrl, 0, 256 * sizeof(struct ring_ctrl));
1166 memset(rrpriv->info, 0, sizeof(struct rr_info));
1167 wmb();
1169 rr_raz_tx(rrpriv, dev);
1170 rr_raz_rx(rrpriv, dev);
1172 if (rr_init1(dev)) {
1173 spin_lock_irqsave(&rrpriv->lock, flags);
1174 writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
1175 &regs->HostCtrl);
1176 spin_unlock_irqrestore(&rrpriv->lock, flags);
1179 rrpriv->timer.expires = RUN_AT(5*HZ);
1180 add_timer(&rrpriv->timer);
1184 static int rr_open(struct net_device *dev)
1186 struct rr_private *rrpriv = netdev_priv(dev);
1187 struct pci_dev *pdev = rrpriv->pci_dev;
1188 struct rr_regs __iomem *regs;
1189 int ecode = 0;
1190 unsigned long flags;
1191 dma_addr_t dma_addr;
1193 regs = rrpriv->regs;
1195 if (rrpriv->fw_rev < 0x00020000) {
1196 printk(KERN_WARNING "%s: trying to configure device with "
1197 "obsolete firmware\n", dev->name);
1198 ecode = -EBUSY;
1199 goto error;
1202 rrpriv->rx_ctrl = pci_alloc_consistent(pdev,
1203 256 * sizeof(struct ring_ctrl),
1204 &dma_addr);
1205 if (!rrpriv->rx_ctrl) {
1206 ecode = -ENOMEM;
1207 goto error;
1209 rrpriv->rx_ctrl_dma = dma_addr;
1210 memset(rrpriv->rx_ctrl, 0, 256*sizeof(struct ring_ctrl));
1212 rrpriv->info = pci_alloc_consistent(pdev, sizeof(struct rr_info),
1213 &dma_addr);
1214 if (!rrpriv->info) {
1215 ecode = -ENOMEM;
1216 goto error;
1218 rrpriv->info_dma = dma_addr;
1219 memset(rrpriv->info, 0, sizeof(struct rr_info));
1220 wmb();
1222 spin_lock_irqsave(&rrpriv->lock, flags);
1223 writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT, &regs->HostCtrl);
1224 readl(&regs->HostCtrl);
1225 spin_unlock_irqrestore(&rrpriv->lock, flags);
1227 if (request_irq(dev->irq, rr_interrupt, IRQF_SHARED, dev->name, dev)) {
1228 printk(KERN_WARNING "%s: Requested IRQ %d is busy\n",
1229 dev->name, dev->irq);
1230 ecode = -EAGAIN;
1231 goto error;
1234 if ((ecode = rr_init1(dev)))
1235 goto error;
1237 /* Set the timer to switch to check for link beat and perhaps switch
1238 to an alternate media type. */
1239 init_timer(&rrpriv->timer);
1240 rrpriv->timer.expires = RUN_AT(5*HZ); /* 5 sec. watchdog */
1241 rrpriv->timer.data = (unsigned long)dev;
1242 rrpriv->timer.function = &rr_timer; /* timer handler */
1243 add_timer(&rrpriv->timer);
1245 netif_start_queue(dev);
1247 return ecode;
1249 error:
1250 spin_lock_irqsave(&rrpriv->lock, flags);
1251 writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT, &regs->HostCtrl);
1252 spin_unlock_irqrestore(&rrpriv->lock, flags);
1254 if (rrpriv->info) {
1255 pci_free_consistent(pdev, sizeof(struct rr_info), rrpriv->info,
1256 rrpriv->info_dma);
1257 rrpriv->info = NULL;
1259 if (rrpriv->rx_ctrl) {
1260 pci_free_consistent(pdev, sizeof(struct ring_ctrl),
1261 rrpriv->rx_ctrl, rrpriv->rx_ctrl_dma);
1262 rrpriv->rx_ctrl = NULL;
1265 netif_stop_queue(dev);
1267 return ecode;
1271 static void rr_dump(struct net_device *dev)
1273 struct rr_private *rrpriv;
1274 struct rr_regs __iomem *regs;
1275 u32 index, cons;
1276 short i;
1277 int len;
1279 rrpriv = netdev_priv(dev);
1280 regs = rrpriv->regs;
1282 printk("%s: dumping NIC TX rings\n", dev->name);
1284 printk("RxPrd %08x, TxPrd %02x, EvtPrd %08x, TxPi %02x, TxCtrlPi %02x\n",
1285 readl(&regs->RxPrd), readl(&regs->TxPrd),
1286 readl(&regs->EvtPrd), readl(&regs->TxPi),
1287 rrpriv->info->tx_ctrl.pi);
1289 printk("Error code 0x%x\n", readl(&regs->Fail1));
1291 index = (((readl(&regs->EvtPrd) >> 8) & 0xff ) - 1) % EVT_RING_ENTRIES;
1292 cons = rrpriv->dirty_tx;
1293 printk("TX ring index %i, TX consumer %i\n",
1294 index, cons);
1296 if (rrpriv->tx_skbuff[index]){
1297 len = min_t(int, 0x80, rrpriv->tx_skbuff[index]->len);
1298 printk("skbuff for index %i is valid - dumping data (0x%x bytes - DMA len 0x%x)\n", index, len, rrpriv->tx_ring[index].size);
1299 for (i = 0; i < len; i++){
1300 if (!(i & 7))
1301 printk("\n");
1302 printk("%02x ", (unsigned char) rrpriv->tx_skbuff[index]->data[i]);
1304 printk("\n");
1307 if (rrpriv->tx_skbuff[cons]){
1308 len = min_t(int, 0x80, rrpriv->tx_skbuff[cons]->len);
1309 printk("skbuff for cons %i is valid - dumping data (0x%x bytes - skbuff len 0x%x)\n", cons, len, rrpriv->tx_skbuff[cons]->len);
1310 printk("mode 0x%x, size 0x%x,\n phys %08Lx, skbuff-addr %08lx, truesize 0x%x\n",
1311 rrpriv->tx_ring[cons].mode,
1312 rrpriv->tx_ring[cons].size,
1313 (unsigned long long) rrpriv->tx_ring[cons].addr.addrlo,
1314 (unsigned long)rrpriv->tx_skbuff[cons]->data,
1315 (unsigned int)rrpriv->tx_skbuff[cons]->truesize);
1316 for (i = 0; i < len; i++){
1317 if (!(i & 7))
1318 printk("\n");
1319 printk("%02x ", (unsigned char)rrpriv->tx_ring[cons].size);
1321 printk("\n");
1324 printk("dumping TX ring info:\n");
1325 for (i = 0; i < TX_RING_ENTRIES; i++)
1326 printk("mode 0x%x, size 0x%x, phys-addr %08Lx\n",
1327 rrpriv->tx_ring[i].mode,
1328 rrpriv->tx_ring[i].size,
1329 (unsigned long long) rrpriv->tx_ring[i].addr.addrlo);
1334 static int rr_close(struct net_device *dev)
1336 struct rr_private *rrpriv;
1337 struct rr_regs __iomem *regs;
1338 unsigned long flags;
1339 u32 tmp;
1340 short i;
1342 netif_stop_queue(dev);
1344 rrpriv = netdev_priv(dev);
1345 regs = rrpriv->regs;
1348 * Lock to make sure we are not cleaning up while another CPU
1349 * is handling interrupts.
1351 spin_lock_irqsave(&rrpriv->lock, flags);
1353 tmp = readl(&regs->HostCtrl);
1354 if (tmp & NIC_HALTED){
1355 printk("%s: NIC already halted\n", dev->name);
1356 rr_dump(dev);
1357 }else{
1358 tmp |= HALT_NIC | RR_CLEAR_INT;
1359 writel(tmp, &regs->HostCtrl);
1360 readl(&regs->HostCtrl);
1363 rrpriv->fw_running = 0;
1365 del_timer_sync(&rrpriv->timer);
1367 writel(0, &regs->TxPi);
1368 writel(0, &regs->IpRxPi);
1370 writel(0, &regs->EvtCon);
1371 writel(0, &regs->EvtPrd);
1373 for (i = 0; i < CMD_RING_ENTRIES; i++)
1374 writel(0, &regs->CmdRing[i]);
1376 rrpriv->info->tx_ctrl.entries = 0;
1377 rrpriv->info->cmd_ctrl.pi = 0;
1378 rrpriv->info->evt_ctrl.pi = 0;
1379 rrpriv->rx_ctrl[4].entries = 0;
1381 rr_raz_tx(rrpriv, dev);
1382 rr_raz_rx(rrpriv, dev);
1384 pci_free_consistent(rrpriv->pci_dev, 256 * sizeof(struct ring_ctrl),
1385 rrpriv->rx_ctrl, rrpriv->rx_ctrl_dma);
1386 rrpriv->rx_ctrl = NULL;
1388 pci_free_consistent(rrpriv->pci_dev, sizeof(struct rr_info),
1389 rrpriv->info, rrpriv->info_dma);
1390 rrpriv->info = NULL;
1392 free_irq(dev->irq, dev);
1393 spin_unlock_irqrestore(&rrpriv->lock, flags);
1395 return 0;
1399 static int rr_start_xmit(struct sk_buff *skb, struct net_device *dev)
1401 struct rr_private *rrpriv = netdev_priv(dev);
1402 struct rr_regs __iomem *regs = rrpriv->regs;
1403 struct hippi_cb *hcb = (struct hippi_cb *) skb->cb;
1404 struct ring_ctrl *txctrl;
1405 unsigned long flags;
1406 u32 index, len = skb->len;
1407 u32 *ifield;
1408 struct sk_buff *new_skb;
1410 if (readl(&regs->Mode) & FATAL_ERR)
1411 printk("error codes Fail1 %02x, Fail2 %02x\n",
1412 readl(&regs->Fail1), readl(&regs->Fail2));
1415 * We probably need to deal with tbusy here to prevent overruns.
1418 if (skb_headroom(skb) < 8){
1419 printk("incoming skb too small - reallocating\n");
1420 if (!(new_skb = dev_alloc_skb(len + 8))) {
1421 dev_kfree_skb(skb);
1422 netif_wake_queue(dev);
1423 return -EBUSY;
1425 skb_reserve(new_skb, 8);
1426 skb_put(new_skb, len);
1427 skb_copy_from_linear_data(skb, new_skb->data, len);
1428 dev_kfree_skb(skb);
1429 skb = new_skb;
1432 ifield = (u32 *)skb_push(skb, 8);
1434 ifield[0] = 0;
1435 ifield[1] = hcb->ifield;
1438 * We don't need the lock before we are actually going to start
1439 * fiddling with the control blocks.
1441 spin_lock_irqsave(&rrpriv->lock, flags);
1443 txctrl = &rrpriv->info->tx_ctrl;
1445 index = txctrl->pi;
1447 rrpriv->tx_skbuff[index] = skb;
1448 set_rraddr(&rrpriv->tx_ring[index].addr, pci_map_single(
1449 rrpriv->pci_dev, skb->data, len + 8, PCI_DMA_TODEVICE));
1450 rrpriv->tx_ring[index].size = len + 8; /* include IFIELD */
1451 rrpriv->tx_ring[index].mode = PACKET_START | PACKET_END;
1452 txctrl->pi = (index + 1) % TX_RING_ENTRIES;
1453 wmb();
1454 writel(txctrl->pi, &regs->TxPi);
1456 if (txctrl->pi == rrpriv->dirty_tx){
1457 rrpriv->tx_full = 1;
1458 netif_stop_queue(dev);
1461 spin_unlock_irqrestore(&rrpriv->lock, flags);
1463 dev->trans_start = jiffies;
1464 return 0;
1469 * Read the firmware out of the EEPROM and put it into the SRAM
1470 * (or from user space - later)
1472 * This operation requires the NIC to be halted and is performed with
1473 * interrupts disabled and with the spinlock hold.
1475 static int rr_load_firmware(struct net_device *dev)
1477 struct rr_private *rrpriv;
1478 struct rr_regs __iomem *regs;
1479 size_t eptr, segptr;
1480 int i, j;
1481 u32 localctrl, sptr, len, tmp;
1482 u32 p2len, p2size, nr_seg, revision, io, sram_size;
1484 rrpriv = netdev_priv(dev);
1485 regs = rrpriv->regs;
1487 if (dev->flags & IFF_UP)
1488 return -EBUSY;
1490 if (!(readl(&regs->HostCtrl) & NIC_HALTED)){
1491 printk("%s: Trying to load firmware to a running NIC.\n",
1492 dev->name);
1493 return -EBUSY;
1496 localctrl = readl(&regs->LocalCtrl);
1497 writel(0, &regs->LocalCtrl);
1499 writel(0, &regs->EvtPrd);
1500 writel(0, &regs->RxPrd);
1501 writel(0, &regs->TxPrd);
1504 * First wipe the entire SRAM, otherwise we might run into all
1505 * kinds of trouble ... sigh, this took almost all afternoon
1506 * to track down ;-(
1508 io = readl(&regs->ExtIo);
1509 writel(0, &regs->ExtIo);
1510 sram_size = rr_read_eeprom_word(rrpriv, 8);
1512 for (i = 200; i < sram_size / 4; i++){
1513 writel(i * 4, &regs->WinBase);
1514 mb();
1515 writel(0, &regs->WinData);
1516 mb();
1518 writel(io, &regs->ExtIo);
1519 mb();
1521 eptr = rr_read_eeprom_word(rrpriv,
1522 offsetof(struct eeprom, rncd_info.AddrRunCodeSegs));
1523 eptr = ((eptr & 0x1fffff) >> 3);
1525 p2len = rr_read_eeprom_word(rrpriv, 0x83*4);
1526 p2len = (p2len << 2);
1527 p2size = rr_read_eeprom_word(rrpriv, 0x84*4);
1528 p2size = ((p2size & 0x1fffff) >> 3);
1530 if ((eptr < p2size) || (eptr > (p2size + p2len))){
1531 printk("%s: eptr is invalid\n", dev->name);
1532 goto out;
1535 revision = rr_read_eeprom_word(rrpriv,
1536 offsetof(struct eeprom, manf.HeaderFmt));
1538 if (revision != 1){
1539 printk("%s: invalid firmware format (%i)\n",
1540 dev->name, revision);
1541 goto out;
1544 nr_seg = rr_read_eeprom_word(rrpriv, eptr);
1545 eptr +=4;
1546 #if (DEBUG > 1)
1547 printk("%s: nr_seg %i\n", dev->name, nr_seg);
1548 #endif
1550 for (i = 0; i < nr_seg; i++){
1551 sptr = rr_read_eeprom_word(rrpriv, eptr);
1552 eptr += 4;
1553 len = rr_read_eeprom_word(rrpriv, eptr);
1554 eptr += 4;
1555 segptr = rr_read_eeprom_word(rrpriv, eptr);
1556 segptr = ((segptr & 0x1fffff) >> 3);
1557 eptr += 4;
1558 #if (DEBUG > 1)
1559 printk("%s: segment %i, sram address %06x, length %04x, segptr %06x\n",
1560 dev->name, i, sptr, len, segptr);
1561 #endif
1562 for (j = 0; j < len; j++){
1563 tmp = rr_read_eeprom_word(rrpriv, segptr);
1564 writel(sptr, &regs->WinBase);
1565 mb();
1566 writel(tmp, &regs->WinData);
1567 mb();
1568 segptr += 4;
1569 sptr += 4;
1573 out:
1574 writel(localctrl, &regs->LocalCtrl);
1575 mb();
1576 return 0;
1580 static int rr_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1582 struct rr_private *rrpriv;
1583 unsigned char *image, *oldimage;
1584 unsigned long flags;
1585 unsigned int i;
1586 int error = -EOPNOTSUPP;
1588 rrpriv = netdev_priv(dev);
1590 switch(cmd){
1591 case SIOCRRGFW:
1592 if (!capable(CAP_SYS_RAWIO)){
1593 return -EPERM;
1596 image = kmalloc(EEPROM_WORDS * sizeof(u32), GFP_KERNEL);
1597 if (!image){
1598 printk(KERN_ERR "%s: Unable to allocate memory "
1599 "for EEPROM image\n", dev->name);
1600 return -ENOMEM;
1604 if (rrpriv->fw_running){
1605 printk("%s: Firmware already running\n", dev->name);
1606 error = -EPERM;
1607 goto gf_out;
1610 spin_lock_irqsave(&rrpriv->lock, flags);
1611 i = rr_read_eeprom(rrpriv, 0, image, EEPROM_BYTES);
1612 spin_unlock_irqrestore(&rrpriv->lock, flags);
1613 if (i != EEPROM_BYTES){
1614 printk(KERN_ERR "%s: Error reading EEPROM\n",
1615 dev->name);
1616 error = -EFAULT;
1617 goto gf_out;
1619 error = copy_to_user(rq->ifr_data, image, EEPROM_BYTES);
1620 if (error)
1621 error = -EFAULT;
1622 gf_out:
1623 kfree(image);
1624 return error;
1626 case SIOCRRPFW:
1627 if (!capable(CAP_SYS_RAWIO)){
1628 return -EPERM;
1631 image = kmalloc(EEPROM_WORDS * sizeof(u32), GFP_KERNEL);
1632 oldimage = kmalloc(EEPROM_WORDS * sizeof(u32), GFP_KERNEL);
1633 if (!image || !oldimage) {
1634 printk(KERN_ERR "%s: Unable to allocate memory "
1635 "for EEPROM image\n", dev->name);
1636 error = -ENOMEM;
1637 goto wf_out;
1640 error = copy_from_user(image, rq->ifr_data, EEPROM_BYTES);
1641 if (error) {
1642 error = -EFAULT;
1643 goto wf_out;
1646 if (rrpriv->fw_running){
1647 printk("%s: Firmware already running\n", dev->name);
1648 error = -EPERM;
1649 goto wf_out;
1652 printk("%s: Updating EEPROM firmware\n", dev->name);
1654 spin_lock_irqsave(&rrpriv->lock, flags);
1655 error = write_eeprom(rrpriv, 0, image, EEPROM_BYTES);
1656 if (error)
1657 printk(KERN_ERR "%s: Error writing EEPROM\n",
1658 dev->name);
1660 i = rr_read_eeprom(rrpriv, 0, oldimage, EEPROM_BYTES);
1661 spin_unlock_irqrestore(&rrpriv->lock, flags);
1663 if (i != EEPROM_BYTES)
1664 printk(KERN_ERR "%s: Error reading back EEPROM "
1665 "image\n", dev->name);
1667 error = memcmp(image, oldimage, EEPROM_BYTES);
1668 if (error){
1669 printk(KERN_ERR "%s: Error verifying EEPROM image\n",
1670 dev->name);
1671 error = -EFAULT;
1673 wf_out:
1674 kfree(oldimage);
1675 kfree(image);
1676 return error;
1678 case SIOCRRID:
1679 return put_user(0x52523032, (int __user *)rq->ifr_data);
1680 default:
1681 return error;
1685 static struct pci_device_id rr_pci_tbl[] = {
1686 { PCI_VENDOR_ID_ESSENTIAL, PCI_DEVICE_ID_ESSENTIAL_ROADRUNNER,
1687 PCI_ANY_ID, PCI_ANY_ID, },
1688 { 0,}
1690 MODULE_DEVICE_TABLE(pci, rr_pci_tbl);
1692 static struct pci_driver rr_driver = {
1693 .name = "rrunner",
1694 .id_table = rr_pci_tbl,
1695 .probe = rr_init_one,
1696 .remove = __devexit_p(rr_remove_one),
1699 static int __init rr_init_module(void)
1701 return pci_register_driver(&rr_driver);
1704 static void __exit rr_cleanup_module(void)
1706 pci_unregister_driver(&rr_driver);
1709 module_init(rr_init_module);
1710 module_exit(rr_cleanup_module);
1713 * Local variables:
1714 * 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"
1715 * End: