1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * rrunner.c: Linux driver for the Essential RoadRunner HIPPI board.
7 * Thanks to Essential Communication for providing us with hardware
8 * and very comprehensive documentation without which I would not have
9 * been able to write this driver. A special thank you to John Gibbon
10 * for sorting out the legal issues, with the NDA, allowing the code to
11 * be released under the GPL.
13 * Thanks to Jayaram Bhat from ODS/Essential for fixing some of the
14 * stupid bugs in my code.
16 * Softnet support and various other patches from Val Henson of
19 * PCI DMA mapping code partly based on work by Francois Romieu.
24 #define RX_DMA_SKBUFF 1
25 #define PKT_COPY_THRESHOLD 512
27 #include <linux/module.h>
28 #include <linux/types.h>
29 #include <linux/errno.h>
30 #include <linux/ioport.h>
31 #include <linux/pci.h>
32 #include <linux/kernel.h>
33 #include <linux/netdevice.h>
34 #include <linux/hippidevice.h>
35 #include <linux/skbuff.h>
36 #include <linux/delay.h>
38 #include <linux/slab.h>
41 #include <asm/cache.h>
42 #include <asm/byteorder.h>
45 #include <linux/uaccess.h>
47 #define rr_if_busy(dev) netif_queue_stopped(dev)
48 #define rr_if_running(dev) netif_running(dev)
52 #define RUN_AT(x) (jiffies + (x))
56 MODULE_DESCRIPTION("Essential RoadRunner HIPPI driver");
57 MODULE_LICENSE("GPL");
59 static const char version[] =
63 static const struct net_device_ops rr_netdev_ops = {
66 .ndo_do_ioctl = rr_ioctl,
67 .ndo_start_xmit = rr_start_xmit,
68 .ndo_set_mac_address = hippi_mac_addr,
72 * Implementation notes:
74 * The DMA engine only allows for DMA within physical 64KB chunks of
75 * memory. The current approach of the driver (and stack) is to use
76 * linear blocks of memory for the skbuffs. However, as the data block
77 * is always the first part of the skb and skbs are 2^n aligned so we
78 * are guarantted to get the whole block within one 64KB align 64KB
81 * On the long term, relying on being able to allocate 64KB linear
82 * chunks of memory is not feasible and the skb handling code and the
83 * stack will need to know about I/O vectors or something similar.
86 static int rr_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
88 struct net_device *dev;
89 static int version_disp;
91 struct rr_private *rrpriv;
96 dev = alloc_hippi_dev(sizeof(struct rr_private));
100 ret = pci_enable_device(pdev);
106 rrpriv = netdev_priv(dev);
108 SET_NETDEV_DEV(dev, &pdev->dev);
110 ret = pci_request_regions(pdev, "rrunner");
114 pci_set_drvdata(pdev, dev);
116 rrpriv->pci_dev = pdev;
118 spin_lock_init(&rrpriv->lock);
120 dev->netdev_ops = &rr_netdev_ops;
122 /* display version info if adapter is found */
124 /* set display flag to TRUE so that */
125 /* we only display this string ONCE */
130 pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &pci_latency);
131 if (pci_latency <= 0x58){
133 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, pci_latency);
136 pci_set_master(pdev);
138 printk(KERN_INFO "%s: Essential RoadRunner serial HIPPI "
139 "at 0x%llx, irq %i, PCI latency %i\n", dev->name,
140 (unsigned long long)pci_resource_start(pdev, 0),
141 pdev->irq, pci_latency);
144 * Remap the MMIO regs into kernel space.
146 rrpriv->regs = pci_iomap(pdev, 0, 0x1000);
148 printk(KERN_ERR "%s: Unable to map I/O register, "
149 "RoadRunner will be disabled.\n", dev->name);
154 tmpptr = pci_alloc_consistent(pdev, TX_TOTAL_SIZE, &ring_dma);
155 rrpriv->tx_ring = tmpptr;
156 rrpriv->tx_ring_dma = ring_dma;
163 tmpptr = pci_alloc_consistent(pdev, RX_TOTAL_SIZE, &ring_dma);
164 rrpriv->rx_ring = tmpptr;
165 rrpriv->rx_ring_dma = ring_dma;
172 tmpptr = pci_alloc_consistent(pdev, EVT_RING_SIZE, &ring_dma);
173 rrpriv->evt_ring = tmpptr;
174 rrpriv->evt_ring_dma = ring_dma;
182 * Don't access any register before this point!
185 writel(readl(&rrpriv->regs->HostCtrl) | NO_SWAP,
186 &rrpriv->regs->HostCtrl);
189 * Need to add a case for little-endian 64-bit hosts here.
194 ret = register_netdev(dev);
200 if (rrpriv->evt_ring)
201 pci_free_consistent(pdev, EVT_RING_SIZE, rrpriv->evt_ring,
202 rrpriv->evt_ring_dma);
204 pci_free_consistent(pdev, RX_TOTAL_SIZE, rrpriv->rx_ring,
205 rrpriv->rx_ring_dma);
207 pci_free_consistent(pdev, TX_TOTAL_SIZE, rrpriv->tx_ring,
208 rrpriv->tx_ring_dma);
210 pci_iounmap(pdev, rrpriv->regs);
212 pci_release_regions(pdev);
219 static void rr_remove_one(struct pci_dev *pdev)
221 struct net_device *dev = pci_get_drvdata(pdev);
222 struct rr_private *rr = netdev_priv(dev);
224 if (!(readl(&rr->regs->HostCtrl) & NIC_HALTED)) {
225 printk(KERN_ERR "%s: trying to unload running NIC\n",
227 writel(HALT_NIC, &rr->regs->HostCtrl);
230 unregister_netdev(dev);
231 pci_free_consistent(pdev, EVT_RING_SIZE, rr->evt_ring,
233 pci_free_consistent(pdev, RX_TOTAL_SIZE, rr->rx_ring,
235 pci_free_consistent(pdev, TX_TOTAL_SIZE, rr->tx_ring,
237 pci_iounmap(pdev, rr->regs);
238 pci_release_regions(pdev);
239 pci_disable_device(pdev);
245 * Commands are considered to be slow, thus there is no reason to
248 static void rr_issue_cmd(struct rr_private *rrpriv, struct cmd *cmd)
250 struct rr_regs __iomem *regs;
255 * This is temporary - it will go away in the final version.
256 * We probably also want to make this function inline.
258 if (readl(®s->HostCtrl) & NIC_HALTED){
259 printk("issuing command for halted NIC, code 0x%x, "
260 "HostCtrl %08x\n", cmd->code, readl(®s->HostCtrl));
261 if (readl(®s->Mode) & FATAL_ERR)
262 printk("error codes Fail1 %02x, Fail2 %02x\n",
263 readl(®s->Fail1), readl(®s->Fail2));
266 idx = rrpriv->info->cmd_ctrl.pi;
268 writel(*(u32*)(cmd), ®s->CmdRing[idx]);
271 idx = (idx - 1) % CMD_RING_ENTRIES;
272 rrpriv->info->cmd_ctrl.pi = idx;
275 if (readl(®s->Mode) & FATAL_ERR)
276 printk("error code %02x\n", readl(®s->Fail1));
281 * Reset the board in a sensible manner. The NIC is already halted
282 * when we get here and a spin-lock is held.
284 static int rr_reset(struct net_device *dev)
286 struct rr_private *rrpriv;
287 struct rr_regs __iomem *regs;
291 rrpriv = netdev_priv(dev);
294 rr_load_firmware(dev);
296 writel(0x01000000, ®s->TX_state);
297 writel(0xff800000, ®s->RX_state);
298 writel(0, ®s->AssistState);
299 writel(CLEAR_INTA, ®s->LocalCtrl);
300 writel(0x01, ®s->BrkPt);
301 writel(0, ®s->Timer);
302 writel(0, ®s->TimerRef);
303 writel(RESET_DMA, ®s->DmaReadState);
304 writel(RESET_DMA, ®s->DmaWriteState);
305 writel(0, ®s->DmaWriteHostHi);
306 writel(0, ®s->DmaWriteHostLo);
307 writel(0, ®s->DmaReadHostHi);
308 writel(0, ®s->DmaReadHostLo);
309 writel(0, ®s->DmaReadLen);
310 writel(0, ®s->DmaWriteLen);
311 writel(0, ®s->DmaWriteLcl);
312 writel(0, ®s->DmaWriteIPchecksum);
313 writel(0, ®s->DmaReadLcl);
314 writel(0, ®s->DmaReadIPchecksum);
315 writel(0, ®s->PciState);
316 #if (BITS_PER_LONG == 64) && defined __LITTLE_ENDIAN
317 writel(SWAP_DATA | PTR64BIT | PTR_WD_SWAP, ®s->Mode);
318 #elif (BITS_PER_LONG == 64)
319 writel(SWAP_DATA | PTR64BIT | PTR_WD_NOSWAP, ®s->Mode);
321 writel(SWAP_DATA | PTR32BIT | PTR_WD_NOSWAP, ®s->Mode);
326 * Don't worry, this is just black magic.
328 writel(0xdf000, ®s->RxBase);
329 writel(0xdf000, ®s->RxPrd);
330 writel(0xdf000, ®s->RxCon);
331 writel(0xce000, ®s->TxBase);
332 writel(0xce000, ®s->TxPrd);
333 writel(0xce000, ®s->TxCon);
334 writel(0, ®s->RxIndPro);
335 writel(0, ®s->RxIndCon);
336 writel(0, ®s->RxIndRef);
337 writel(0, ®s->TxIndPro);
338 writel(0, ®s->TxIndCon);
339 writel(0, ®s->TxIndRef);
340 writel(0xcc000, ®s->pad10[0]);
341 writel(0, ®s->DrCmndPro);
342 writel(0, ®s->DrCmndCon);
343 writel(0, ®s->DwCmndPro);
344 writel(0, ®s->DwCmndCon);
345 writel(0, ®s->DwCmndRef);
346 writel(0, ®s->DrDataPro);
347 writel(0, ®s->DrDataCon);
348 writel(0, ®s->DrDataRef);
349 writel(0, ®s->DwDataPro);
350 writel(0, ®s->DwDataCon);
351 writel(0, ®s->DwDataRef);
354 writel(0xffffffff, ®s->MbEvent);
355 writel(0, ®s->Event);
357 writel(0, ®s->TxPi);
358 writel(0, ®s->IpRxPi);
360 writel(0, ®s->EvtCon);
361 writel(0, ®s->EvtPrd);
363 rrpriv->info->evt_ctrl.pi = 0;
365 for (i = 0; i < CMD_RING_ENTRIES; i++)
366 writel(0, ®s->CmdRing[i]);
369 * Why 32 ? is this not cache line size dependent?
371 writel(RBURST_64|WBURST_64, ®s->PciState);
374 start_pc = rr_read_eeprom_word(rrpriv,
375 offsetof(struct eeprom, rncd_info.FwStart));
378 printk("%s: Executing firmware at address 0x%06x\n",
379 dev->name, start_pc);
382 writel(start_pc + 0x800, ®s->Pc);
386 writel(start_pc, ®s->Pc);
394 * Read a string from the EEPROM.
396 static unsigned int rr_read_eeprom(struct rr_private *rrpriv,
397 unsigned long offset,
399 unsigned long length)
401 struct rr_regs __iomem *regs = rrpriv->regs;
402 u32 misc, io, host, i;
404 io = readl(®s->ExtIo);
405 writel(0, ®s->ExtIo);
406 misc = readl(®s->LocalCtrl);
407 writel(0, ®s->LocalCtrl);
408 host = readl(®s->HostCtrl);
409 writel(host | HALT_NIC, ®s->HostCtrl);
412 for (i = 0; i < length; i++){
413 writel((EEPROM_BASE + ((offset+i) << 3)), ®s->WinBase);
415 buf[i] = (readl(®s->WinData) >> 24) & 0xff;
419 writel(host, ®s->HostCtrl);
420 writel(misc, ®s->LocalCtrl);
421 writel(io, ®s->ExtIo);
428 * Shortcut to read one word (4 bytes) out of the EEPROM and convert
429 * it to our CPU byte-order.
431 static u32 rr_read_eeprom_word(struct rr_private *rrpriv,
436 if ((rr_read_eeprom(rrpriv, offset,
437 (unsigned char *)&word, 4) == 4))
438 return be32_to_cpu(word);
444 * Write a string to the EEPROM.
446 * This is only called when the firmware is not running.
448 static unsigned int write_eeprom(struct rr_private *rrpriv,
449 unsigned long offset,
451 unsigned long length)
453 struct rr_regs __iomem *regs = rrpriv->regs;
454 u32 misc, io, data, i, j, ready, error = 0;
456 io = readl(®s->ExtIo);
457 writel(0, ®s->ExtIo);
458 misc = readl(®s->LocalCtrl);
459 writel(ENABLE_EEPROM_WRITE, ®s->LocalCtrl);
462 for (i = 0; i < length; i++){
463 writel((EEPROM_BASE + ((offset+i) << 3)), ®s->WinBase);
467 * Only try to write the data if it is not the same
470 if ((readl(®s->WinData) & 0xff000000) != data){
471 writel(data, ®s->WinData);
477 if ((readl(®s->WinData) & 0xff000000) ==
482 printk("data mismatch: %08x, "
483 "WinData %08x\n", data,
484 readl(®s->WinData));
492 writel(misc, ®s->LocalCtrl);
493 writel(io, ®s->ExtIo);
500 static int rr_init(struct net_device *dev)
502 struct rr_private *rrpriv;
503 struct rr_regs __iomem *regs;
506 rrpriv = netdev_priv(dev);
509 rev = readl(®s->FwRev);
510 rrpriv->fw_rev = rev;
511 if (rev > 0x00020024)
512 printk(" Firmware revision: %i.%i.%i\n", (rev >> 16),
513 ((rev >> 8) & 0xff), (rev & 0xff));
514 else if (rev >= 0x00020000) {
515 printk(" Firmware revision: %i.%i.%i (2.0.37 or "
516 "later is recommended)\n", (rev >> 16),
517 ((rev >> 8) & 0xff), (rev & 0xff));
519 printk(" Firmware revision too old: %i.%i.%i, please "
520 "upgrade to 2.0.37 or later.\n",
521 (rev >> 16), ((rev >> 8) & 0xff), (rev & 0xff));
525 printk(" Maximum receive rings %i\n", readl(®s->MaxRxRng));
529 * Read the hardware address from the eeprom. The HW address
530 * is not really necessary for HIPPI but awfully convenient.
531 * The pointer arithmetic to put it in dev_addr is ugly, but
532 * Donald Becker does it this way for the GigE version of this
533 * card and it's shorter and more portable than any
534 * other method I've seen. -VAL
537 *(__be16 *)(dev->dev_addr) =
538 htons(rr_read_eeprom_word(rrpriv, offsetof(struct eeprom, manf.BoardULA)));
539 *(__be32 *)(dev->dev_addr+2) =
540 htonl(rr_read_eeprom_word(rrpriv, offsetof(struct eeprom, manf.BoardULA[4])));
542 printk(" MAC: %pM\n", dev->dev_addr);
544 sram_size = rr_read_eeprom_word(rrpriv, 8);
545 printk(" SRAM size 0x%06x\n", sram_size);
551 static int rr_init1(struct net_device *dev)
553 struct rr_private *rrpriv;
554 struct rr_regs __iomem *regs;
555 unsigned long myjif, flags;
561 rrpriv = netdev_priv(dev);
564 spin_lock_irqsave(&rrpriv->lock, flags);
566 hostctrl = readl(®s->HostCtrl);
567 writel(hostctrl | HALT_NIC | RR_CLEAR_INT, ®s->HostCtrl);
570 if (hostctrl & PARITY_ERR){
571 printk("%s: Parity error halting NIC - this is serious!\n",
573 spin_unlock_irqrestore(&rrpriv->lock, flags);
578 set_rxaddr(regs, rrpriv->rx_ctrl_dma);
579 set_infoaddr(regs, rrpriv->info_dma);
581 rrpriv->info->evt_ctrl.entry_size = sizeof(struct event);
582 rrpriv->info->evt_ctrl.entries = EVT_RING_ENTRIES;
583 rrpriv->info->evt_ctrl.mode = 0;
584 rrpriv->info->evt_ctrl.pi = 0;
585 set_rraddr(&rrpriv->info->evt_ctrl.rngptr, rrpriv->evt_ring_dma);
587 rrpriv->info->cmd_ctrl.entry_size = sizeof(struct cmd);
588 rrpriv->info->cmd_ctrl.entries = CMD_RING_ENTRIES;
589 rrpriv->info->cmd_ctrl.mode = 0;
590 rrpriv->info->cmd_ctrl.pi = 15;
592 for (i = 0; i < CMD_RING_ENTRIES; i++) {
593 writel(0, ®s->CmdRing[i]);
596 for (i = 0; i < TX_RING_ENTRIES; i++) {
597 rrpriv->tx_ring[i].size = 0;
598 set_rraddr(&rrpriv->tx_ring[i].addr, 0);
599 rrpriv->tx_skbuff[i] = NULL;
601 rrpriv->info->tx_ctrl.entry_size = sizeof(struct tx_desc);
602 rrpriv->info->tx_ctrl.entries = TX_RING_ENTRIES;
603 rrpriv->info->tx_ctrl.mode = 0;
604 rrpriv->info->tx_ctrl.pi = 0;
605 set_rraddr(&rrpriv->info->tx_ctrl.rngptr, rrpriv->tx_ring_dma);
608 * Set dirty_tx before we start receiving interrupts, otherwise
609 * the interrupt handler might think it is supposed to process
610 * tx ints before we are up and running, which may cause a null
611 * pointer access in the int handler.
615 rrpriv->dirty_rx = rrpriv->dirty_tx = 0;
620 writel(0x5000, ®s->ConRetry);
621 writel(0x100, ®s->ConRetryTmr);
622 writel(0x500000, ®s->ConTmout);
623 writel(0x60, ®s->IntrTmr);
624 writel(0x500000, ®s->TxDataMvTimeout);
625 writel(0x200000, ®s->RxDataMvTimeout);
626 writel(0x80, ®s->WriteDmaThresh);
627 writel(0x80, ®s->ReadDmaThresh);
629 rrpriv->fw_running = 0;
632 hostctrl &= ~(HALT_NIC | INVALID_INST_B | PARITY_ERR);
633 writel(hostctrl, ®s->HostCtrl);
636 spin_unlock_irqrestore(&rrpriv->lock, flags);
638 for (i = 0; i < RX_RING_ENTRIES; i++) {
642 rrpriv->rx_ring[i].mode = 0;
643 skb = alloc_skb(dev->mtu + HIPPI_HLEN, GFP_ATOMIC);
645 printk(KERN_WARNING "%s: Unable to allocate memory "
646 "for receive ring - halting NIC\n", dev->name);
650 rrpriv->rx_skbuff[i] = skb;
651 addr = pci_map_single(rrpriv->pci_dev, skb->data,
652 dev->mtu + HIPPI_HLEN, PCI_DMA_FROMDEVICE);
654 * Sanity test to see if we conflict with the DMA
655 * limitations of the Roadrunner.
657 if ((((unsigned long)skb->data) & 0xfff) > ~65320)
658 printk("skb alloc error\n");
660 set_rraddr(&rrpriv->rx_ring[i].addr, addr);
661 rrpriv->rx_ring[i].size = dev->mtu + HIPPI_HLEN;
664 rrpriv->rx_ctrl[4].entry_size = sizeof(struct rx_desc);
665 rrpriv->rx_ctrl[4].entries = RX_RING_ENTRIES;
666 rrpriv->rx_ctrl[4].mode = 8;
667 rrpriv->rx_ctrl[4].pi = 0;
669 set_rraddr(&rrpriv->rx_ctrl[4].rngptr, rrpriv->rx_ring_dma);
674 * Now start the FirmWare.
676 cmd.code = C_START_FW;
680 rr_issue_cmd(rrpriv, &cmd);
683 * Give the FirmWare time to chew on the `get running' command.
685 myjif = jiffies + 5 * HZ;
686 while (time_before(jiffies, myjif) && !rrpriv->fw_running)
689 netif_start_queue(dev);
695 * We might have gotten here because we are out of memory,
696 * make sure we release everything we allocated before failing
698 for (i = 0; i < RX_RING_ENTRIES; i++) {
699 struct sk_buff *skb = rrpriv->rx_skbuff[i];
702 pci_unmap_single(rrpriv->pci_dev,
703 rrpriv->rx_ring[i].addr.addrlo,
704 dev->mtu + HIPPI_HLEN,
706 rrpriv->rx_ring[i].size = 0;
707 set_rraddr(&rrpriv->rx_ring[i].addr, 0);
709 rrpriv->rx_skbuff[i] = NULL;
717 * All events are considered to be slow (RX/TX ints do not generate
718 * events) and are handled here, outside the main interrupt handler,
719 * to reduce the size of the handler.
721 static u32 rr_handle_event(struct net_device *dev, u32 prodidx, u32 eidx)
723 struct rr_private *rrpriv;
724 struct rr_regs __iomem *regs;
727 rrpriv = netdev_priv(dev);
730 while (prodidx != eidx){
731 switch (rrpriv->evt_ring[eidx].code){
733 tmp = readl(®s->FwRev);
734 printk(KERN_INFO "%s: Firmware revision %i.%i.%i "
735 "up and running\n", dev->name,
736 (tmp >> 16), ((tmp >> 8) & 0xff), (tmp & 0xff));
737 rrpriv->fw_running = 1;
738 writel(RX_RING_ENTRIES - 1, ®s->IpRxPi);
742 printk(KERN_INFO "%s: Optical link ON\n", dev->name);
745 printk(KERN_INFO "%s: Optical link OFF\n", dev->name);
748 printk(KERN_WARNING "%s: RX data not moving\n",
752 printk(KERN_INFO "%s: The watchdog is here to see "
756 printk(KERN_ERR "%s: HIPPI Internal NIC error\n",
758 writel(readl(®s->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
763 printk(KERN_ERR "%s: Host software error\n",
765 writel(readl(®s->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
773 printk(KERN_WARNING "%s: Connection rejected\n",
775 dev->stats.tx_aborted_errors++;
778 printk(KERN_WARNING "%s: Connection timeout\n",
782 printk(KERN_WARNING "%s: HIPPI disconnect error\n",
784 dev->stats.tx_aborted_errors++;
787 printk(KERN_ERR "%s: HIPPI Internal Parity error\n",
789 writel(readl(®s->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
794 printk(KERN_WARNING "%s: Transmitter idle\n",
798 printk(KERN_WARNING "%s: Link lost during transmit\n",
800 dev->stats.tx_aborted_errors++;
801 writel(readl(®s->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
806 printk(KERN_ERR "%s: Invalid send ring block\n",
808 writel(readl(®s->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
813 printk(KERN_ERR "%s: Invalid send buffer address\n",
815 writel(readl(®s->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
820 printk(KERN_ERR "%s: Invalid descriptor address\n",
822 writel(readl(®s->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
830 printk(KERN_INFO "%s: Receive ring full\n", dev->name);
834 printk(KERN_WARNING "%s: Receive parity error\n",
838 printk(KERN_WARNING "%s: Receive LLRC error\n",
842 printk(KERN_WARNING "%s: Receive packet length "
843 "error\n", dev->name);
846 printk(KERN_WARNING "%s: Data checksum error\n",
850 printk(KERN_WARNING "%s: Unexpected short burst "
851 "error\n", dev->name);
854 printk(KERN_WARNING "%s: Recv. state transition"
855 " error\n", dev->name);
858 printk(KERN_WARNING "%s: Unexpected data error\n",
862 printk(KERN_WARNING "%s: Link lost error\n",
866 printk(KERN_WARNING "%s: Framing Error\n",
870 printk(KERN_WARNING "%s: Flag sync. lost during "
871 "packet\n", dev->name);
874 printk(KERN_ERR "%s: Invalid receive buffer "
875 "address\n", dev->name);
876 writel(readl(®s->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
881 printk(KERN_ERR "%s: Invalid receive descriptor "
882 "address\n", dev->name);
883 writel(readl(®s->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
888 printk(KERN_ERR "%s: Invalid ring block\n",
890 writel(readl(®s->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
895 /* Label packet to be dropped.
896 * Actual dropping occurs in rx
899 * The index of packet we get to drop is
900 * the index of the packet following
901 * the bad packet. -kbf
904 u16 index = rrpriv->evt_ring[eidx].index;
905 index = (index + (RX_RING_ENTRIES - 1)) %
907 rrpriv->rx_ring[index].mode |=
908 (PACKET_BAD | PACKET_END);
912 printk(KERN_WARNING "%s: Unhandled event 0x%02x\n",
913 dev->name, rrpriv->evt_ring[eidx].code);
915 eidx = (eidx + 1) % EVT_RING_ENTRIES;
918 rrpriv->info->evt_ctrl.pi = eidx;
924 static void rx_int(struct net_device *dev, u32 rxlimit, u32 index)
926 struct rr_private *rrpriv = netdev_priv(dev);
927 struct rr_regs __iomem *regs = rrpriv->regs;
930 struct rx_desc *desc;
933 desc = &(rrpriv->rx_ring[index]);
934 pkt_len = desc->size;
936 printk("index %i, rxlimit %i\n", index, rxlimit);
937 printk("len %x, mode %x\n", pkt_len, desc->mode);
939 if ( (rrpriv->rx_ring[index].mode & PACKET_BAD) == PACKET_BAD){
940 dev->stats.rx_dropped++;
945 struct sk_buff *skb, *rx_skb;
947 rx_skb = rrpriv->rx_skbuff[index];
949 if (pkt_len < PKT_COPY_THRESHOLD) {
950 skb = alloc_skb(pkt_len, GFP_ATOMIC);
952 printk(KERN_WARNING "%s: Unable to allocate skb (%i bytes), deferring packet\n", dev->name, pkt_len);
953 dev->stats.rx_dropped++;
956 pci_dma_sync_single_for_cpu(rrpriv->pci_dev,
961 skb_put_data(skb, rx_skb->data,
964 pci_dma_sync_single_for_device(rrpriv->pci_dev,
970 struct sk_buff *newskb;
972 newskb = alloc_skb(dev->mtu + HIPPI_HLEN,
977 pci_unmap_single(rrpriv->pci_dev,
978 desc->addr.addrlo, dev->mtu +
979 HIPPI_HLEN, PCI_DMA_FROMDEVICE);
981 skb_put(skb, pkt_len);
982 rrpriv->rx_skbuff[index] = newskb;
983 addr = pci_map_single(rrpriv->pci_dev,
985 dev->mtu + HIPPI_HLEN,
987 set_rraddr(&desc->addr, addr);
989 printk("%s: Out of memory, deferring "
990 "packet\n", dev->name);
991 dev->stats.rx_dropped++;
995 skb->protocol = hippi_type_trans(skb, dev);
997 netif_rx(skb); /* send it up */
999 dev->stats.rx_packets++;
1000 dev->stats.rx_bytes += pkt_len;
1004 desc->size = dev->mtu + HIPPI_HLEN;
1006 if ((index & 7) == 7)
1007 writel(index, ®s->IpRxPi);
1009 index = (index + 1) % RX_RING_ENTRIES;
1010 } while(index != rxlimit);
1012 rrpriv->cur_rx = index;
1017 static irqreturn_t rr_interrupt(int irq, void *dev_id)
1019 struct rr_private *rrpriv;
1020 struct rr_regs __iomem *regs;
1021 struct net_device *dev = (struct net_device *)dev_id;
1022 u32 prodidx, rxindex, eidx, txcsmr, rxlimit, txcon;
1024 rrpriv = netdev_priv(dev);
1025 regs = rrpriv->regs;
1027 if (!(readl(®s->HostCtrl) & RR_INT))
1030 spin_lock(&rrpriv->lock);
1032 prodidx = readl(®s->EvtPrd);
1033 txcsmr = (prodidx >> 8) & 0xff;
1034 rxlimit = (prodidx >> 16) & 0xff;
1038 printk("%s: interrupt, prodidx = %i, eidx = %i\n", dev->name,
1039 prodidx, rrpriv->info->evt_ctrl.pi);
1042 * Order here is important. We must handle events
1043 * before doing anything else in order to catch
1044 * such things as LLRC errors, etc -kbf
1047 eidx = rrpriv->info->evt_ctrl.pi;
1048 if (prodidx != eidx)
1049 eidx = rr_handle_event(dev, prodidx, eidx);
1051 rxindex = rrpriv->cur_rx;
1052 if (rxindex != rxlimit)
1053 rx_int(dev, rxlimit, rxindex);
1055 txcon = rrpriv->dirty_tx;
1056 if (txcsmr != txcon) {
1058 /* Due to occational firmware TX producer/consumer out
1059 * of sync. error need to check entry in ring -kbf
1061 if(rrpriv->tx_skbuff[txcon]){
1062 struct tx_desc *desc;
1063 struct sk_buff *skb;
1065 desc = &(rrpriv->tx_ring[txcon]);
1066 skb = rrpriv->tx_skbuff[txcon];
1068 dev->stats.tx_packets++;
1069 dev->stats.tx_bytes += skb->len;
1071 pci_unmap_single(rrpriv->pci_dev,
1072 desc->addr.addrlo, skb->len,
1074 dev_kfree_skb_irq(skb);
1076 rrpriv->tx_skbuff[txcon] = NULL;
1078 set_rraddr(&rrpriv->tx_ring[txcon].addr, 0);
1081 txcon = (txcon + 1) % TX_RING_ENTRIES;
1082 } while (txcsmr != txcon);
1085 rrpriv->dirty_tx = txcon;
1086 if (rrpriv->tx_full && rr_if_busy(dev) &&
1087 (((rrpriv->info->tx_ctrl.pi + 1) % TX_RING_ENTRIES)
1088 != rrpriv->dirty_tx)){
1089 rrpriv->tx_full = 0;
1090 netif_wake_queue(dev);
1094 eidx |= ((txcsmr << 8) | (rxlimit << 16));
1095 writel(eidx, ®s->EvtCon);
1098 spin_unlock(&rrpriv->lock);
1102 static inline void rr_raz_tx(struct rr_private *rrpriv,
1103 struct net_device *dev)
1107 for (i = 0; i < TX_RING_ENTRIES; i++) {
1108 struct sk_buff *skb = rrpriv->tx_skbuff[i];
1111 struct tx_desc *desc = &(rrpriv->tx_ring[i]);
1113 pci_unmap_single(rrpriv->pci_dev, desc->addr.addrlo,
1114 skb->len, PCI_DMA_TODEVICE);
1116 set_rraddr(&desc->addr, 0);
1118 rrpriv->tx_skbuff[i] = NULL;
1124 static inline void rr_raz_rx(struct rr_private *rrpriv,
1125 struct net_device *dev)
1129 for (i = 0; i < RX_RING_ENTRIES; i++) {
1130 struct sk_buff *skb = rrpriv->rx_skbuff[i];
1133 struct rx_desc *desc = &(rrpriv->rx_ring[i]);
1135 pci_unmap_single(rrpriv->pci_dev, desc->addr.addrlo,
1136 dev->mtu + HIPPI_HLEN, PCI_DMA_FROMDEVICE);
1138 set_rraddr(&desc->addr, 0);
1140 rrpriv->rx_skbuff[i] = NULL;
1145 static void rr_timer(struct timer_list *t)
1147 struct rr_private *rrpriv = from_timer(rrpriv, t, timer);
1148 struct net_device *dev = pci_get_drvdata(rrpriv->pci_dev);
1149 struct rr_regs __iomem *regs = rrpriv->regs;
1150 unsigned long flags;
1152 if (readl(®s->HostCtrl) & NIC_HALTED){
1153 printk("%s: Restarting nic\n", dev->name);
1154 memset(rrpriv->rx_ctrl, 0, 256 * sizeof(struct ring_ctrl));
1155 memset(rrpriv->info, 0, sizeof(struct rr_info));
1158 rr_raz_tx(rrpriv, dev);
1159 rr_raz_rx(rrpriv, dev);
1161 if (rr_init1(dev)) {
1162 spin_lock_irqsave(&rrpriv->lock, flags);
1163 writel(readl(®s->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
1165 spin_unlock_irqrestore(&rrpriv->lock, flags);
1168 rrpriv->timer.expires = RUN_AT(5*HZ);
1169 add_timer(&rrpriv->timer);
1173 static int rr_open(struct net_device *dev)
1175 struct rr_private *rrpriv = netdev_priv(dev);
1176 struct pci_dev *pdev = rrpriv->pci_dev;
1177 struct rr_regs __iomem *regs;
1179 unsigned long flags;
1180 dma_addr_t dma_addr;
1182 regs = rrpriv->regs;
1184 if (rrpriv->fw_rev < 0x00020000) {
1185 printk(KERN_WARNING "%s: trying to configure device with "
1186 "obsolete firmware\n", dev->name);
1191 rrpriv->rx_ctrl = pci_alloc_consistent(pdev,
1192 256 * sizeof(struct ring_ctrl),
1194 if (!rrpriv->rx_ctrl) {
1198 rrpriv->rx_ctrl_dma = dma_addr;
1200 rrpriv->info = pci_alloc_consistent(pdev, sizeof(struct rr_info),
1202 if (!rrpriv->info) {
1206 rrpriv->info_dma = dma_addr;
1209 spin_lock_irqsave(&rrpriv->lock, flags);
1210 writel(readl(®s->HostCtrl)|HALT_NIC|RR_CLEAR_INT, ®s->HostCtrl);
1211 readl(®s->HostCtrl);
1212 spin_unlock_irqrestore(&rrpriv->lock, flags);
1214 if (request_irq(pdev->irq, rr_interrupt, IRQF_SHARED, dev->name, dev)) {
1215 printk(KERN_WARNING "%s: Requested IRQ %d is busy\n",
1216 dev->name, pdev->irq);
1221 if ((ecode = rr_init1(dev)))
1224 /* Set the timer to switch to check for link beat and perhaps switch
1225 to an alternate media type. */
1226 timer_setup(&rrpriv->timer, rr_timer, 0);
1227 rrpriv->timer.expires = RUN_AT(5*HZ); /* 5 sec. watchdog */
1228 add_timer(&rrpriv->timer);
1230 netif_start_queue(dev);
1235 spin_lock_irqsave(&rrpriv->lock, flags);
1236 writel(readl(®s->HostCtrl)|HALT_NIC|RR_CLEAR_INT, ®s->HostCtrl);
1237 spin_unlock_irqrestore(&rrpriv->lock, flags);
1240 pci_free_consistent(pdev, sizeof(struct rr_info), rrpriv->info,
1242 rrpriv->info = NULL;
1244 if (rrpriv->rx_ctrl) {
1245 pci_free_consistent(pdev, 256 * sizeof(struct ring_ctrl),
1246 rrpriv->rx_ctrl, rrpriv->rx_ctrl_dma);
1247 rrpriv->rx_ctrl = NULL;
1250 netif_stop_queue(dev);
1256 static void rr_dump(struct net_device *dev)
1258 struct rr_private *rrpriv;
1259 struct rr_regs __iomem *regs;
1264 rrpriv = netdev_priv(dev);
1265 regs = rrpriv->regs;
1267 printk("%s: dumping NIC TX rings\n", dev->name);
1269 printk("RxPrd %08x, TxPrd %02x, EvtPrd %08x, TxPi %02x, TxCtrlPi %02x\n",
1270 readl(®s->RxPrd), readl(®s->TxPrd),
1271 readl(®s->EvtPrd), readl(®s->TxPi),
1272 rrpriv->info->tx_ctrl.pi);
1274 printk("Error code 0x%x\n", readl(®s->Fail1));
1276 index = (((readl(®s->EvtPrd) >> 8) & 0xff) - 1) % TX_RING_ENTRIES;
1277 cons = rrpriv->dirty_tx;
1278 printk("TX ring index %i, TX consumer %i\n",
1281 if (rrpriv->tx_skbuff[index]){
1282 len = min_t(int, 0x80, rrpriv->tx_skbuff[index]->len);
1283 printk("skbuff for index %i is valid - dumping data (0x%x bytes - DMA len 0x%x)\n", index, len, rrpriv->tx_ring[index].size);
1284 for (i = 0; i < len; i++){
1287 printk("%02x ", (unsigned char) rrpriv->tx_skbuff[index]->data[i]);
1292 if (rrpriv->tx_skbuff[cons]){
1293 len = min_t(int, 0x80, rrpriv->tx_skbuff[cons]->len);
1294 printk("skbuff for cons %i is valid - dumping data (0x%x bytes - skbuff len 0x%x)\n", cons, len, rrpriv->tx_skbuff[cons]->len);
1295 printk("mode 0x%x, size 0x%x,\n phys %08Lx, skbuff-addr %p, truesize 0x%x\n",
1296 rrpriv->tx_ring[cons].mode,
1297 rrpriv->tx_ring[cons].size,
1298 (unsigned long long) rrpriv->tx_ring[cons].addr.addrlo,
1299 rrpriv->tx_skbuff[cons]->data,
1300 (unsigned int)rrpriv->tx_skbuff[cons]->truesize);
1301 for (i = 0; i < len; i++){
1304 printk("%02x ", (unsigned char)rrpriv->tx_ring[cons].size);
1309 printk("dumping TX ring info:\n");
1310 for (i = 0; i < TX_RING_ENTRIES; i++)
1311 printk("mode 0x%x, size 0x%x, phys-addr %08Lx\n",
1312 rrpriv->tx_ring[i].mode,
1313 rrpriv->tx_ring[i].size,
1314 (unsigned long long) rrpriv->tx_ring[i].addr.addrlo);
1319 static int rr_close(struct net_device *dev)
1321 struct rr_private *rrpriv = netdev_priv(dev);
1322 struct rr_regs __iomem *regs = rrpriv->regs;
1323 struct pci_dev *pdev = rrpriv->pci_dev;
1324 unsigned long flags;
1328 netif_stop_queue(dev);
1332 * Lock to make sure we are not cleaning up while another CPU
1333 * is handling interrupts.
1335 spin_lock_irqsave(&rrpriv->lock, flags);
1337 tmp = readl(®s->HostCtrl);
1338 if (tmp & NIC_HALTED){
1339 printk("%s: NIC already halted\n", dev->name);
1342 tmp |= HALT_NIC | RR_CLEAR_INT;
1343 writel(tmp, ®s->HostCtrl);
1344 readl(®s->HostCtrl);
1347 rrpriv->fw_running = 0;
1349 del_timer_sync(&rrpriv->timer);
1351 writel(0, ®s->TxPi);
1352 writel(0, ®s->IpRxPi);
1354 writel(0, ®s->EvtCon);
1355 writel(0, ®s->EvtPrd);
1357 for (i = 0; i < CMD_RING_ENTRIES; i++)
1358 writel(0, ®s->CmdRing[i]);
1360 rrpriv->info->tx_ctrl.entries = 0;
1361 rrpriv->info->cmd_ctrl.pi = 0;
1362 rrpriv->info->evt_ctrl.pi = 0;
1363 rrpriv->rx_ctrl[4].entries = 0;
1365 rr_raz_tx(rrpriv, dev);
1366 rr_raz_rx(rrpriv, dev);
1368 pci_free_consistent(pdev, 256 * sizeof(struct ring_ctrl),
1369 rrpriv->rx_ctrl, rrpriv->rx_ctrl_dma);
1370 rrpriv->rx_ctrl = NULL;
1372 pci_free_consistent(pdev, sizeof(struct rr_info), rrpriv->info,
1374 rrpriv->info = NULL;
1376 spin_unlock_irqrestore(&rrpriv->lock, flags);
1377 free_irq(pdev->irq, dev);
1383 static netdev_tx_t rr_start_xmit(struct sk_buff *skb,
1384 struct net_device *dev)
1386 struct rr_private *rrpriv = netdev_priv(dev);
1387 struct rr_regs __iomem *regs = rrpriv->regs;
1388 struct hippi_cb *hcb = (struct hippi_cb *) skb->cb;
1389 struct ring_ctrl *txctrl;
1390 unsigned long flags;
1391 u32 index, len = skb->len;
1393 struct sk_buff *new_skb;
1395 if (readl(®s->Mode) & FATAL_ERR)
1396 printk("error codes Fail1 %02x, Fail2 %02x\n",
1397 readl(®s->Fail1), readl(®s->Fail2));
1400 * We probably need to deal with tbusy here to prevent overruns.
1403 if (skb_headroom(skb) < 8){
1404 printk("incoming skb too small - reallocating\n");
1405 if (!(new_skb = dev_alloc_skb(len + 8))) {
1407 netif_wake_queue(dev);
1408 return NETDEV_TX_OK;
1410 skb_reserve(new_skb, 8);
1411 skb_put(new_skb, len);
1412 skb_copy_from_linear_data(skb, new_skb->data, len);
1417 ifield = skb_push(skb, 8);
1420 ifield[1] = hcb->ifield;
1423 * We don't need the lock before we are actually going to start
1424 * fiddling with the control blocks.
1426 spin_lock_irqsave(&rrpriv->lock, flags);
1428 txctrl = &rrpriv->info->tx_ctrl;
1432 rrpriv->tx_skbuff[index] = skb;
1433 set_rraddr(&rrpriv->tx_ring[index].addr, pci_map_single(
1434 rrpriv->pci_dev, skb->data, len + 8, PCI_DMA_TODEVICE));
1435 rrpriv->tx_ring[index].size = len + 8; /* include IFIELD */
1436 rrpriv->tx_ring[index].mode = PACKET_START | PACKET_END;
1437 txctrl->pi = (index + 1) % TX_RING_ENTRIES;
1439 writel(txctrl->pi, ®s->TxPi);
1441 if (txctrl->pi == rrpriv->dirty_tx){
1442 rrpriv->tx_full = 1;
1443 netif_stop_queue(dev);
1446 spin_unlock_irqrestore(&rrpriv->lock, flags);
1448 return NETDEV_TX_OK;
1453 * Read the firmware out of the EEPROM and put it into the SRAM
1454 * (or from user space - later)
1456 * This operation requires the NIC to be halted and is performed with
1457 * interrupts disabled and with the spinlock hold.
1459 static int rr_load_firmware(struct net_device *dev)
1461 struct rr_private *rrpriv;
1462 struct rr_regs __iomem *regs;
1463 size_t eptr, segptr;
1465 u32 localctrl, sptr, len, tmp;
1466 u32 p2len, p2size, nr_seg, revision, io, sram_size;
1468 rrpriv = netdev_priv(dev);
1469 regs = rrpriv->regs;
1471 if (dev->flags & IFF_UP)
1474 if (!(readl(®s->HostCtrl) & NIC_HALTED)){
1475 printk("%s: Trying to load firmware to a running NIC.\n",
1480 localctrl = readl(®s->LocalCtrl);
1481 writel(0, ®s->LocalCtrl);
1483 writel(0, ®s->EvtPrd);
1484 writel(0, ®s->RxPrd);
1485 writel(0, ®s->TxPrd);
1488 * First wipe the entire SRAM, otherwise we might run into all
1489 * kinds of trouble ... sigh, this took almost all afternoon
1492 io = readl(®s->ExtIo);
1493 writel(0, ®s->ExtIo);
1494 sram_size = rr_read_eeprom_word(rrpriv, 8);
1496 for (i = 200; i < sram_size / 4; i++){
1497 writel(i * 4, ®s->WinBase);
1499 writel(0, ®s->WinData);
1502 writel(io, ®s->ExtIo);
1505 eptr = rr_read_eeprom_word(rrpriv,
1506 offsetof(struct eeprom, rncd_info.AddrRunCodeSegs));
1507 eptr = ((eptr & 0x1fffff) >> 3);
1509 p2len = rr_read_eeprom_word(rrpriv, 0x83*4);
1510 p2len = (p2len << 2);
1511 p2size = rr_read_eeprom_word(rrpriv, 0x84*4);
1512 p2size = ((p2size & 0x1fffff) >> 3);
1514 if ((eptr < p2size) || (eptr > (p2size + p2len))){
1515 printk("%s: eptr is invalid\n", dev->name);
1519 revision = rr_read_eeprom_word(rrpriv,
1520 offsetof(struct eeprom, manf.HeaderFmt));
1523 printk("%s: invalid firmware format (%i)\n",
1524 dev->name, revision);
1528 nr_seg = rr_read_eeprom_word(rrpriv, eptr);
1531 printk("%s: nr_seg %i\n", dev->name, nr_seg);
1534 for (i = 0; i < nr_seg; i++){
1535 sptr = rr_read_eeprom_word(rrpriv, eptr);
1537 len = rr_read_eeprom_word(rrpriv, eptr);
1539 segptr = rr_read_eeprom_word(rrpriv, eptr);
1540 segptr = ((segptr & 0x1fffff) >> 3);
1543 printk("%s: segment %i, sram address %06x, length %04x, segptr %06x\n",
1544 dev->name, i, sptr, len, segptr);
1546 for (j = 0; j < len; j++){
1547 tmp = rr_read_eeprom_word(rrpriv, segptr);
1548 writel(sptr, ®s->WinBase);
1550 writel(tmp, ®s->WinData);
1558 writel(localctrl, ®s->LocalCtrl);
1564 static int rr_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1566 struct rr_private *rrpriv;
1567 unsigned char *image, *oldimage;
1568 unsigned long flags;
1570 int error = -EOPNOTSUPP;
1572 rrpriv = netdev_priv(dev);
1576 if (!capable(CAP_SYS_RAWIO)){
1580 image = kmalloc_array(EEPROM_WORDS, sizeof(u32), GFP_KERNEL);
1584 if (rrpriv->fw_running){
1585 printk("%s: Firmware already running\n", dev->name);
1590 spin_lock_irqsave(&rrpriv->lock, flags);
1591 i = rr_read_eeprom(rrpriv, 0, image, EEPROM_BYTES);
1592 spin_unlock_irqrestore(&rrpriv->lock, flags);
1593 if (i != EEPROM_BYTES){
1594 printk(KERN_ERR "%s: Error reading EEPROM\n",
1599 error = copy_to_user(rq->ifr_data, image, EEPROM_BYTES);
1607 if (!capable(CAP_SYS_RAWIO)){
1611 image = memdup_user(rq->ifr_data, EEPROM_BYTES);
1613 return PTR_ERR(image);
1615 oldimage = kmalloc(EEPROM_BYTES, GFP_KERNEL);
1621 if (rrpriv->fw_running){
1622 printk("%s: Firmware already running\n", dev->name);
1627 printk("%s: Updating EEPROM firmware\n", dev->name);
1629 spin_lock_irqsave(&rrpriv->lock, flags);
1630 error = write_eeprom(rrpriv, 0, image, EEPROM_BYTES);
1632 printk(KERN_ERR "%s: Error writing EEPROM\n",
1635 i = rr_read_eeprom(rrpriv, 0, oldimage, EEPROM_BYTES);
1636 spin_unlock_irqrestore(&rrpriv->lock, flags);
1638 if (i != EEPROM_BYTES)
1639 printk(KERN_ERR "%s: Error reading back EEPROM "
1640 "image\n", dev->name);
1642 error = memcmp(image, oldimage, EEPROM_BYTES);
1644 printk(KERN_ERR "%s: Error verifying EEPROM image\n",
1654 return put_user(0x52523032, (int __user *)rq->ifr_data);
1660 static const struct pci_device_id rr_pci_tbl[] = {
1661 { PCI_VENDOR_ID_ESSENTIAL, PCI_DEVICE_ID_ESSENTIAL_ROADRUNNER,
1662 PCI_ANY_ID, PCI_ANY_ID, },
1665 MODULE_DEVICE_TABLE(pci, rr_pci_tbl);
1667 static struct pci_driver rr_driver = {
1669 .id_table = rr_pci_tbl,
1670 .probe = rr_init_one,
1671 .remove = rr_remove_one,
1674 module_pci_driver(rr_driver);