More meth updates.
[linux-2.6/linux-mips.git] / drivers / net / rrunner.c
blob1e01bc50b0b14c8146c3b475bfbe0fe4f15bff89
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/config.h>
32 #include <linux/module.h>
33 #include <linux/version.h>
34 #include <linux/types.h>
35 #include <linux/errno.h>
36 #include <linux/ioport.h>
37 #include <linux/pci.h>
38 #include <linux/kernel.h>
39 #include <linux/netdevice.h>
40 #include <linux/hippidevice.h>
41 #include <linux/skbuff.h>
42 #include <linux/init.h>
43 #include <linux/delay.h>
44 #include <linux/mm.h>
45 #include <net/sock.h>
47 #include <asm/system.h>
48 #include <asm/cache.h>
49 #include <asm/byteorder.h>
50 #include <asm/io.h>
51 #include <asm/irq.h>
52 #include <asm/uaccess.h>
54 #define rr_if_busy(dev) netif_queue_stopped(dev)
55 #define rr_if_running(dev) netif_running(dev)
57 #include "rrunner.h"
59 #define RUN_AT(x) (jiffies + (x))
62 MODULE_AUTHOR("Jes Sorensen <jes@wildopensource.com>");
63 MODULE_DESCRIPTION("Essential RoadRunner HIPPI driver");
64 MODULE_LICENSE("GPL");
66 static char version[] __initdata = "rrunner.c: v0.50 11/11/2002 Jes Sorensen (jes@wildopensource.com)\n";
69 * Implementation notes:
71 * The DMA engine only allows for DMA within physical 64KB chunks of
72 * memory. The current approach of the driver (and stack) is to use
73 * linear blocks of memory for the skbuffs. However, as the data block
74 * is always the first part of the skb and skbs are 2^n aligned so we
75 * are guarantted to get the whole block within one 64KB align 64KB
76 * chunk.
78 * On the long term, relying on being able to allocate 64KB linear
79 * chunks of memory is not feasible and the skb handling code and the
80 * stack will need to know about I/O vectors or something similar.
84 * These are checked at init time to see if they are at least 256KB
85 * and increased to 256KB if they are not. This is done to avoid ending
86 * up with socket buffers smaller than the MTU size,
88 extern __u32 sysctl_wmem_max;
89 extern __u32 sysctl_rmem_max;
91 static int __devinit rr_init_one(struct pci_dev *pdev,
92 const struct pci_device_id *ent)
94 struct net_device *dev;
95 static int version_disp;
96 u8 pci_latency;
97 struct rr_private *rrpriv;
98 void *tmpptr;
99 dma_addr_t ring_dma;
100 int ret = -ENOMEM;
102 dev = alloc_hippi_dev(sizeof(struct rr_private));
103 if (!dev)
104 goto out3;
106 ret = pci_enable_device(pdev);
107 if (ret) {
108 ret = -ENODEV;
109 goto out2;
112 rrpriv = (struct rr_private *)dev->priv;
114 SET_MODULE_OWNER(dev);
115 SET_NETDEV_DEV(dev, &pdev->dev);
117 if (pci_request_regions(pdev, "rrunner")) {
118 ret = -EIO;
119 goto out;
122 pci_set_drvdata(pdev, dev);
124 rrpriv->pci_dev = pdev;
126 spin_lock_init(&rrpriv->lock);
127 sprintf(rrpriv->name, "RoadRunner serial HIPPI");
129 dev->irq = pdev->irq;
130 dev->open = &rr_open;
131 dev->hard_start_xmit = &rr_start_xmit;
132 dev->stop = &rr_close;
133 dev->get_stats = &rr_get_stats;
134 dev->do_ioctl = &rr_ioctl;
136 dev->base_addr = pci_resource_start(pdev, 0);
138 /* display version info if adapter is found */
139 if (!version_disp) {
140 /* set display flag to TRUE so that */
141 /* we only display this string ONCE */
142 version_disp = 1;
143 printk(version);
146 pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &pci_latency);
147 if (pci_latency <= 0x58){
148 pci_latency = 0x58;
149 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, pci_latency);
152 pci_set_master(pdev);
154 printk(KERN_INFO "%s: Essential RoadRunner serial HIPPI "
155 "at 0x%08lx, irq %i, PCI latency %i\n", dev->name,
156 dev->base_addr, dev->irq, pci_latency);
159 * Remap the regs into kernel space.
162 rrpriv->regs = (struct rr_regs *)ioremap(dev->base_addr, 0x1000);
164 if (!rrpriv->regs){
165 printk(KERN_ERR "%s: Unable to map I/O register, "
166 "RoadRunner will be disabled.\n", dev->name);
167 ret = -EIO;
168 goto out;
171 tmpptr = pci_alloc_consistent(pdev, TX_TOTAL_SIZE, &ring_dma);
172 rrpriv->tx_ring = tmpptr;
173 rrpriv->tx_ring_dma = ring_dma;
175 if (!tmpptr) {
176 ret = -ENOMEM;
177 goto out;
180 tmpptr = pci_alloc_consistent(pdev, RX_TOTAL_SIZE, &ring_dma);
181 rrpriv->rx_ring = tmpptr;
182 rrpriv->rx_ring_dma = ring_dma;
184 if (!tmpptr) {
185 ret = -ENOMEM;
186 goto out;
189 tmpptr = pci_alloc_consistent(pdev, EVT_RING_SIZE, &ring_dma);
190 rrpriv->evt_ring = tmpptr;
191 rrpriv->evt_ring_dma = ring_dma;
193 if (!tmpptr) {
194 ret = -ENOMEM;
195 goto out;
199 * Don't access any register before this point!
201 #ifdef __BIG_ENDIAN
202 writel(readl(&regs->HostCtrl) | NO_SWAP, &regs->HostCtrl);
203 #endif
205 * Need to add a case for little-endian 64-bit hosts here.
208 rr_init(dev);
210 dev->base_addr = 0;
212 ret = register_netdev(dev);
213 if (ret)
214 goto out;
215 return 0;
217 out:
218 if (rrpriv->rx_ring)
219 pci_free_consistent(pdev, RX_TOTAL_SIZE, rrpriv->rx_ring,
220 rrpriv->rx_ring_dma);
221 if (rrpriv->tx_ring)
222 pci_free_consistent(pdev, TX_TOTAL_SIZE, rrpriv->tx_ring,
223 rrpriv->tx_ring_dma);
224 if (rrpriv->regs)
225 iounmap(rrpriv->regs);
226 if (pdev) {
227 pci_release_regions(pdev);
228 pci_set_drvdata(pdev, NULL);
230 out2:
231 kfree(dev);
232 out3:
233 return ret;
236 static void __devexit rr_remove_one (struct pci_dev *pdev)
238 struct net_device *dev = pci_get_drvdata(pdev);
239 struct rr_private *rr = (struct rr_private *)dev->priv;
241 if (dev) {
242 if (!(readl(&rr->regs->HostCtrl) & NIC_HALTED)){
243 printk(KERN_ERR "%s: trying to unload running NIC\n",
244 dev->name);
245 writel(HALT_NIC, &rr->regs->HostCtrl);
248 pci_free_consistent(pdev, EVT_RING_SIZE, rr->evt_ring,
249 rr->evt_ring_dma);
250 pci_free_consistent(pdev, RX_TOTAL_SIZE, rr->rx_ring,
251 rr->rx_ring_dma);
252 pci_free_consistent(pdev, TX_TOTAL_SIZE, rr->tx_ring,
253 rr->tx_ring_dma);
254 unregister_netdev(dev);
255 iounmap(rr->regs);
256 kfree(dev);
257 pci_release_regions(pdev);
258 pci_disable_device(pdev);
259 pci_set_drvdata(pdev, NULL);
265 * Commands are considered to be slow, thus there is no reason to
266 * inline this.
268 static void rr_issue_cmd(struct rr_private *rrpriv, struct cmd *cmd)
270 struct rr_regs *regs;
271 u32 idx;
273 regs = rrpriv->regs;
275 * This is temporary - it will go away in the final version.
276 * We probably also want to make this function inline.
278 if (readl(&regs->HostCtrl) & NIC_HALTED){
279 printk("issuing command for halted NIC, code 0x%x, "
280 "HostCtrl %08x\n", cmd->code, readl(&regs->HostCtrl));
281 if (readl(&regs->Mode) & FATAL_ERR)
282 printk("error codes Fail1 %02x, Fail2 %02x\n",
283 readl(&regs->Fail1), readl(&regs->Fail2));
286 idx = rrpriv->info->cmd_ctrl.pi;
288 writel(*(u32*)(cmd), &regs->CmdRing[idx]);
289 wmb();
291 idx = (idx - 1) % CMD_RING_ENTRIES;
292 rrpriv->info->cmd_ctrl.pi = idx;
293 wmb();
295 if (readl(&regs->Mode) & FATAL_ERR)
296 printk("error code %02x\n", readl(&regs->Fail1));
301 * Reset the board in a sensible manner. The NIC is already halted
302 * when we get here and a spin-lock is held.
304 static int rr_reset(struct net_device *dev)
306 struct rr_private *rrpriv;
307 struct rr_regs *regs;
308 struct eeprom *hw = NULL;
309 u32 start_pc;
310 int i;
312 rrpriv = (struct rr_private *)dev->priv;
313 regs = rrpriv->regs;
315 rr_load_firmware(dev);
317 writel(0x01000000, &regs->TX_state);
318 writel(0xff800000, &regs->RX_state);
319 writel(0, &regs->AssistState);
320 writel(CLEAR_INTA, &regs->LocalCtrl);
321 writel(0x01, &regs->BrkPt);
322 writel(0, &regs->Timer);
323 writel(0, &regs->TimerRef);
324 writel(RESET_DMA, &regs->DmaReadState);
325 writel(RESET_DMA, &regs->DmaWriteState);
326 writel(0, &regs->DmaWriteHostHi);
327 writel(0, &regs->DmaWriteHostLo);
328 writel(0, &regs->DmaReadHostHi);
329 writel(0, &regs->DmaReadHostLo);
330 writel(0, &regs->DmaReadLen);
331 writel(0, &regs->DmaWriteLen);
332 writel(0, &regs->DmaWriteLcl);
333 writel(0, &regs->DmaWriteIPchecksum);
334 writel(0, &regs->DmaReadLcl);
335 writel(0, &regs->DmaReadIPchecksum);
336 writel(0, &regs->PciState);
337 #if (BITS_PER_LONG == 64) && defined __LITTLE_ENDIAN
338 writel(SWAP_DATA | PTR64BIT | PTR_WD_SWAP, &regs->Mode);
339 #elif (BITS_PER_LONG == 64)
340 writel(SWAP_DATA | PTR64BIT | PTR_WD_NOSWAP, &regs->Mode);
341 #else
342 writel(SWAP_DATA | PTR32BIT | PTR_WD_NOSWAP, &regs->Mode);
343 #endif
345 #if 0
347 * Don't worry, this is just black magic.
349 writel(0xdf000, &regs->RxBase);
350 writel(0xdf000, &regs->RxPrd);
351 writel(0xdf000, &regs->RxCon);
352 writel(0xce000, &regs->TxBase);
353 writel(0xce000, &regs->TxPrd);
354 writel(0xce000, &regs->TxCon);
355 writel(0, &regs->RxIndPro);
356 writel(0, &regs->RxIndCon);
357 writel(0, &regs->RxIndRef);
358 writel(0, &regs->TxIndPro);
359 writel(0, &regs->TxIndCon);
360 writel(0, &regs->TxIndRef);
361 writel(0xcc000, &regs->pad10[0]);
362 writel(0, &regs->DrCmndPro);
363 writel(0, &regs->DrCmndCon);
364 writel(0, &regs->DwCmndPro);
365 writel(0, &regs->DwCmndCon);
366 writel(0, &regs->DwCmndRef);
367 writel(0, &regs->DrDataPro);
368 writel(0, &regs->DrDataCon);
369 writel(0, &regs->DrDataRef);
370 writel(0, &regs->DwDataPro);
371 writel(0, &regs->DwDataCon);
372 writel(0, &regs->DwDataRef);
373 #endif
375 writel(0xffffffff, &regs->MbEvent);
376 writel(0, &regs->Event);
378 writel(0, &regs->TxPi);
379 writel(0, &regs->IpRxPi);
381 writel(0, &regs->EvtCon);
382 writel(0, &regs->EvtPrd);
384 rrpriv->info->evt_ctrl.pi = 0;
386 for (i = 0; i < CMD_RING_ENTRIES; i++)
387 writel(0, &regs->CmdRing[i]);
390 * Why 32 ? is this not cache line size dependent?
392 writel(RBURST_64|WBURST_64, &regs->PciState);
393 wmb();
395 start_pc = rr_read_eeprom_word(rrpriv, &hw->rncd_info.FwStart);
397 #if (DEBUG > 1)
398 printk("%s: Executing firmware at address 0x%06x\n",
399 dev->name, start_pc);
400 #endif
402 writel(start_pc + 0x800, &regs->Pc);
403 wmb();
404 udelay(5);
406 writel(start_pc, &regs->Pc);
407 wmb();
409 return 0;
414 * Read a string from the EEPROM.
416 static unsigned int rr_read_eeprom(struct rr_private *rrpriv,
417 unsigned long offset,
418 unsigned char *buf,
419 unsigned long length)
421 struct rr_regs *regs = rrpriv->regs;
422 u32 misc, io, host, i;
424 io = readl(&regs->ExtIo);
425 writel(0, &regs->ExtIo);
426 misc = readl(&regs->LocalCtrl);
427 writel(0, &regs->LocalCtrl);
428 host = readl(&regs->HostCtrl);
429 writel(host | HALT_NIC, &regs->HostCtrl);
430 mb();
432 for (i = 0; i < length; i++){
433 writel((EEPROM_BASE + ((offset+i) << 3)), &regs->WinBase);
434 mb();
435 buf[i] = (readl(&regs->WinData) >> 24) & 0xff;
436 mb();
439 writel(host, &regs->HostCtrl);
440 writel(misc, &regs->LocalCtrl);
441 writel(io, &regs->ExtIo);
442 mb();
443 return i;
448 * Shortcut to read one word (4 bytes) out of the EEPROM and convert
449 * it to our CPU byte-order.
451 static u32 rr_read_eeprom_word(struct rr_private *rrpriv,
452 void * offset)
454 u32 word;
456 if ((rr_read_eeprom(rrpriv, (unsigned long)offset,
457 (char *)&word, 4) == 4))
458 return be32_to_cpu(word);
459 return 0;
464 * Write a string to the EEPROM.
466 * This is only called when the firmware is not running.
468 static unsigned int write_eeprom(struct rr_private *rrpriv,
469 unsigned long offset,
470 unsigned char *buf,
471 unsigned long length)
473 struct rr_regs *regs = rrpriv->regs;
474 u32 misc, io, data, i, j, ready, error = 0;
476 io = readl(&regs->ExtIo);
477 writel(0, &regs->ExtIo);
478 misc = readl(&regs->LocalCtrl);
479 writel(ENABLE_EEPROM_WRITE, &regs->LocalCtrl);
480 mb();
482 for (i = 0; i < length; i++){
483 writel((EEPROM_BASE + ((offset+i) << 3)), &regs->WinBase);
484 mb();
485 data = buf[i] << 24;
487 * Only try to write the data if it is not the same
488 * value already.
490 if ((readl(&regs->WinData) & 0xff000000) != data){
491 writel(data, &regs->WinData);
492 ready = 0;
493 j = 0;
494 mb();
495 while(!ready){
496 udelay(20);
497 if ((readl(&regs->WinData) & 0xff000000) ==
498 data)
499 ready = 1;
500 mb();
501 if (j++ > 5000){
502 printk("data mismatch: %08x, "
503 "WinData %08x\n", data,
504 readl(&regs->WinData));
505 ready = 1;
506 error = 1;
512 writel(misc, &regs->LocalCtrl);
513 writel(io, &regs->ExtIo);
514 mb();
516 return error;
520 static int __init rr_init(struct net_device *dev)
522 struct rr_private *rrpriv;
523 struct rr_regs *regs;
524 struct eeprom *hw = NULL;
525 u32 sram_size, rev;
526 int i;
528 rrpriv = (struct rr_private *)dev->priv;
529 regs = rrpriv->regs;
531 rev = readl(&regs->FwRev);
532 rrpriv->fw_rev = rev;
533 if (rev > 0x00020024)
534 printk(" Firmware revision: %i.%i.%i\n", (rev >> 16),
535 ((rev >> 8) & 0xff), (rev & 0xff));
536 else if (rev >= 0x00020000) {
537 printk(" Firmware revision: %i.%i.%i (2.0.37 or "
538 "later is recommended)\n", (rev >> 16),
539 ((rev >> 8) & 0xff), (rev & 0xff));
540 }else{
541 printk(" Firmware revision too old: %i.%i.%i, please "
542 "upgrade to 2.0.37 or later.\n",
543 (rev >> 16), ((rev >> 8) & 0xff), (rev & 0xff));
546 #if (DEBUG > 2)
547 printk(" Maximum receive rings %i\n", readl(&regs->MaxRxRng));
548 #endif
551 * Read the hardware address from the eeprom. The HW address
552 * is not really necessary for HIPPI but awfully convenient.
553 * The pointer arithmetic to put it in dev_addr is ugly, but
554 * Donald Becker does it this way for the GigE version of this
555 * card and it's shorter and more portable than any
556 * other method I've seen. -VAL
559 *(u16 *)(dev->dev_addr) =
560 htons(rr_read_eeprom_word(rrpriv, &hw->manf.BoardULA));
561 *(u32 *)(dev->dev_addr+2) =
562 htonl(rr_read_eeprom_word(rrpriv, &hw->manf.BoardULA[4]));
564 printk(" MAC: ");
566 for (i = 0; i < 5; i++)
567 printk("%2.2x:", dev->dev_addr[i]);
568 printk("%2.2x\n", dev->dev_addr[i]);
570 sram_size = rr_read_eeprom_word(rrpriv, (void *)8);
571 printk(" SRAM size 0x%06x\n", sram_size);
573 if (sysctl_rmem_max < 262144){
574 printk(" Receive socket buffer limit too low (%i), "
575 "setting to 262144\n", sysctl_rmem_max);
576 sysctl_rmem_max = 262144;
579 if (sysctl_wmem_max < 262144){
580 printk(" Transmit socket buffer limit too low (%i), "
581 "setting to 262144\n", sysctl_wmem_max);
582 sysctl_wmem_max = 262144;
585 return 0;
589 static int rr_init1(struct net_device *dev)
591 struct rr_private *rrpriv;
592 struct rr_regs *regs;
593 unsigned long myjif, flags;
594 struct cmd cmd;
595 u32 hostctrl;
596 int ecode = 0;
597 short i;
599 rrpriv = (struct rr_private *)dev->priv;
600 regs = rrpriv->regs;
602 spin_lock_irqsave(&rrpriv->lock, flags);
604 hostctrl = readl(&regs->HostCtrl);
605 writel(hostctrl | HALT_NIC | RR_CLEAR_INT, &regs->HostCtrl);
606 wmb();
608 if (hostctrl & PARITY_ERR){
609 printk("%s: Parity error halting NIC - this is serious!\n",
610 dev->name);
611 spin_unlock_irqrestore(&rrpriv->lock, flags);
612 ecode = -EFAULT;
613 goto error;
616 set_rxaddr(regs, rrpriv->rx_ctrl_dma);
617 set_infoaddr(regs, rrpriv->info_dma);
619 rrpriv->info->evt_ctrl.entry_size = sizeof(struct event);
620 rrpriv->info->evt_ctrl.entries = EVT_RING_ENTRIES;
621 rrpriv->info->evt_ctrl.mode = 0;
622 rrpriv->info->evt_ctrl.pi = 0;
623 set_rraddr(&rrpriv->info->evt_ctrl.rngptr, rrpriv->evt_ring_dma);
625 rrpriv->info->cmd_ctrl.entry_size = sizeof(struct cmd);
626 rrpriv->info->cmd_ctrl.entries = CMD_RING_ENTRIES;
627 rrpriv->info->cmd_ctrl.mode = 0;
628 rrpriv->info->cmd_ctrl.pi = 15;
630 for (i = 0; i < CMD_RING_ENTRIES; i++) {
631 writel(0, &regs->CmdRing[i]);
634 for (i = 0; i < TX_RING_ENTRIES; i++) {
635 rrpriv->tx_ring[i].size = 0;
636 set_rraddr(&rrpriv->tx_ring[i].addr, 0);
637 rrpriv->tx_skbuff[i] = 0;
639 rrpriv->info->tx_ctrl.entry_size = sizeof(struct tx_desc);
640 rrpriv->info->tx_ctrl.entries = TX_RING_ENTRIES;
641 rrpriv->info->tx_ctrl.mode = 0;
642 rrpriv->info->tx_ctrl.pi = 0;
643 set_rraddr(&rrpriv->info->tx_ctrl.rngptr, rrpriv->tx_ring_dma);
646 * Set dirty_tx before we start receiving interrupts, otherwise
647 * the interrupt handler might think it is supposed to process
648 * tx ints before we are up and running, which may cause a null
649 * pointer access in the int handler.
651 rrpriv->tx_full = 0;
652 rrpriv->cur_rx = 0;
653 rrpriv->dirty_rx = rrpriv->dirty_tx = 0;
655 rr_reset(dev);
657 /* Tuning values */
658 writel(0x5000, &regs->ConRetry);
659 writel(0x100, &regs->ConRetryTmr);
660 writel(0x500000, &regs->ConTmout);
661 writel(0x60, &regs->IntrTmr);
662 writel(0x500000, &regs->TxDataMvTimeout);
663 writel(0x200000, &regs->RxDataMvTimeout);
664 writel(0x80, &regs->WriteDmaThresh);
665 writel(0x80, &regs->ReadDmaThresh);
667 rrpriv->fw_running = 0;
668 wmb();
670 hostctrl &= ~(HALT_NIC | INVALID_INST_B | PARITY_ERR);
671 writel(hostctrl, &regs->HostCtrl);
672 wmb();
674 spin_unlock_irqrestore(&rrpriv->lock, flags);
676 for (i = 0; i < RX_RING_ENTRIES; i++) {
677 struct sk_buff *skb;
678 dma_addr_t addr;
680 rrpriv->rx_ring[i].mode = 0;
681 skb = alloc_skb(dev->mtu + HIPPI_HLEN, GFP_ATOMIC);
682 if (!skb) {
683 printk(KERN_WARNING "%s: Unable to allocate memory "
684 "for receive ring - halting NIC\n", dev->name);
685 ecode = -ENOMEM;
686 goto error;
688 rrpriv->rx_skbuff[i] = skb;
689 addr = pci_map_single(rrpriv->pci_dev, skb->data,
690 dev->mtu + HIPPI_HLEN, PCI_DMA_FROMDEVICE);
692 * Sanity test to see if we conflict with the DMA
693 * limitations of the Roadrunner.
695 if ((((unsigned long)skb->data) & 0xfff) > ~65320)
696 printk("skb alloc error\n");
698 set_rraddr(&rrpriv->rx_ring[i].addr, addr);
699 rrpriv->rx_ring[i].size = dev->mtu + HIPPI_HLEN;
702 rrpriv->rx_ctrl[4].entry_size = sizeof(struct rx_desc);
703 rrpriv->rx_ctrl[4].entries = RX_RING_ENTRIES;
704 rrpriv->rx_ctrl[4].mode = 8;
705 rrpriv->rx_ctrl[4].pi = 0;
706 wmb();
707 set_rraddr(&rrpriv->rx_ctrl[4].rngptr, rrpriv->rx_ring_dma);
709 udelay(1000);
712 * Now start the FirmWare.
714 cmd.code = C_START_FW;
715 cmd.ring = 0;
716 cmd.index = 0;
718 rr_issue_cmd(rrpriv, &cmd);
721 * Give the FirmWare time to chew on the `get running' command.
723 myjif = jiffies + 5 * HZ;
724 while (time_before(jiffies, myjif) && !rrpriv->fw_running);
726 netif_start_queue(dev);
728 return ecode;
730 error:
732 * We might have gotten here because we are out of memory,
733 * make sure we release everything we allocated before failing
735 for (i = 0; i < RX_RING_ENTRIES; i++) {
736 struct sk_buff *skb = rrpriv->rx_skbuff[i];
738 if (skb) {
739 pci_unmap_single(rrpriv->pci_dev,
740 rrpriv->rx_ring[i].addr.addrlo,
741 dev->mtu + HIPPI_HLEN,
742 PCI_DMA_FROMDEVICE);
743 rrpriv->rx_ring[i].size = 0;
744 set_rraddr(&rrpriv->rx_ring[i].addr, 0);
745 dev_kfree_skb(skb);
746 rrpriv->rx_skbuff[i] = 0;
749 return ecode;
754 * All events are considered to be slow (RX/TX ints do not generate
755 * events) and are handled here, outside the main interrupt handler,
756 * to reduce the size of the handler.
758 static u32 rr_handle_event(struct net_device *dev, u32 prodidx, u32 eidx)
760 struct rr_private *rrpriv;
761 struct rr_regs *regs;
762 u32 tmp;
764 rrpriv = (struct rr_private *)dev->priv;
765 regs = rrpriv->regs;
767 while (prodidx != eidx){
768 switch (rrpriv->evt_ring[eidx].code){
769 case E_NIC_UP:
770 tmp = readl(&regs->FwRev);
771 printk(KERN_INFO "%s: Firmware revision %i.%i.%i "
772 "up and running\n", dev->name,
773 (tmp >> 16), ((tmp >> 8) & 0xff), (tmp & 0xff));
774 rrpriv->fw_running = 1;
775 writel(RX_RING_ENTRIES - 1, &regs->IpRxPi);
776 wmb();
777 break;
778 case E_LINK_ON:
779 printk(KERN_INFO "%s: Optical link ON\n", dev->name);
780 break;
781 case E_LINK_OFF:
782 printk(KERN_INFO "%s: Optical link OFF\n", dev->name);
783 break;
784 case E_RX_IDLE:
785 printk(KERN_WARNING "%s: RX data not moving\n",
786 dev->name);
787 goto drop;
788 case E_WATCHDOG:
789 printk(KERN_INFO "%s: The watchdog is here to see "
790 "us\n", dev->name);
791 break;
792 case E_INTERN_ERR:
793 printk(KERN_ERR "%s: HIPPI Internal NIC error\n",
794 dev->name);
795 writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
796 &regs->HostCtrl);
797 wmb();
798 break;
799 case E_HOST_ERR:
800 printk(KERN_ERR "%s: Host software error\n",
801 dev->name);
802 writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
803 &regs->HostCtrl);
804 wmb();
805 break;
807 * TX events.
809 case E_CON_REJ:
810 printk(KERN_WARNING "%s: Connection rejected\n",
811 dev->name);
812 rrpriv->stats.tx_aborted_errors++;
813 break;
814 case E_CON_TMOUT:
815 printk(KERN_WARNING "%s: Connection timeout\n",
816 dev->name);
817 break;
818 case E_DISC_ERR:
819 printk(KERN_WARNING "%s: HIPPI disconnect error\n",
820 dev->name);
821 rrpriv->stats.tx_aborted_errors++;
822 break;
823 case E_INT_PRTY:
824 printk(KERN_ERR "%s: HIPPI Internal Parity error\n",
825 dev->name);
826 writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
827 &regs->HostCtrl);
828 wmb();
829 break;
830 case E_TX_IDLE:
831 printk(KERN_WARNING "%s: Transmitter idle\n",
832 dev->name);
833 break;
834 case E_TX_LINK_DROP:
835 printk(KERN_WARNING "%s: Link lost during transmit\n",
836 dev->name);
837 rrpriv->stats.tx_aborted_errors++;
838 writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
839 &regs->HostCtrl);
840 wmb();
841 break;
842 case E_TX_INV_RNG:
843 printk(KERN_ERR "%s: Invalid send ring block\n",
844 dev->name);
845 writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
846 &regs->HostCtrl);
847 wmb();
848 break;
849 case E_TX_INV_BUF:
850 printk(KERN_ERR "%s: Invalid send buffer address\n",
851 dev->name);
852 writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
853 &regs->HostCtrl);
854 wmb();
855 break;
856 case E_TX_INV_DSC:
857 printk(KERN_ERR "%s: Invalid descriptor address\n",
858 dev->name);
859 writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
860 &regs->HostCtrl);
861 wmb();
862 break;
864 * RX events.
866 case E_RX_RNG_OUT:
867 printk(KERN_INFO "%s: Receive ring full\n", dev->name);
868 break;
870 case E_RX_PAR_ERR:
871 printk(KERN_WARNING "%s: Receive parity error\n",
872 dev->name);
873 goto drop;
874 case E_RX_LLRC_ERR:
875 printk(KERN_WARNING "%s: Receive LLRC error\n",
876 dev->name);
877 goto drop;
878 case E_PKT_LN_ERR:
879 printk(KERN_WARNING "%s: Receive packet length "
880 "error\n", dev->name);
881 goto drop;
882 case E_DTA_CKSM_ERR:
883 printk(KERN_WARNING "%s: Data checksum error\n",
884 dev->name);
885 goto drop;
886 case E_SHT_BST:
887 printk(KERN_WARNING "%s: Unexpected short burst "
888 "error\n", dev->name);
889 goto drop;
890 case E_STATE_ERR:
891 printk(KERN_WARNING "%s: Recv. state transition"
892 " error\n", dev->name);
893 goto drop;
894 case E_UNEXP_DATA:
895 printk(KERN_WARNING "%s: Unexpected data error\n",
896 dev->name);
897 goto drop;
898 case E_LST_LNK_ERR:
899 printk(KERN_WARNING "%s: Link lost error\n",
900 dev->name);
901 goto drop;
902 case E_FRM_ERR:
903 printk(KERN_WARNING "%s: Framming Error\n",
904 dev->name);
905 goto drop;
906 case E_FLG_SYN_ERR:
907 printk(KERN_WARNING "%s: Flag sync. lost during"
908 "packet\n", dev->name);
909 goto drop;
910 case E_RX_INV_BUF:
911 printk(KERN_ERR "%s: Invalid receive buffer "
912 "address\n", dev->name);
913 writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
914 &regs->HostCtrl);
915 wmb();
916 break;
917 case E_RX_INV_DSC:
918 printk(KERN_ERR "%s: Invalid receive descriptor "
919 "address\n", dev->name);
920 writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
921 &regs->HostCtrl);
922 wmb();
923 break;
924 case E_RNG_BLK:
925 printk(KERN_ERR "%s: Invalid ring block\n",
926 dev->name);
927 writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
928 &regs->HostCtrl);
929 wmb();
930 break;
931 drop:
932 /* Label packet to be dropped.
933 * Actual dropping occurs in rx
934 * handling.
936 * The index of packet we get to drop is
937 * the index of the packet following
938 * the bad packet. -kbf
941 u16 index = rrpriv->evt_ring[eidx].index;
942 index = (index + (RX_RING_ENTRIES - 1)) %
943 RX_RING_ENTRIES;
944 rrpriv->rx_ring[index].mode |=
945 (PACKET_BAD | PACKET_END);
947 break;
948 default:
949 printk(KERN_WARNING "%s: Unhandled event 0x%02x\n",
950 dev->name, rrpriv->evt_ring[eidx].code);
952 eidx = (eidx + 1) % EVT_RING_ENTRIES;
955 rrpriv->info->evt_ctrl.pi = eidx;
956 wmb();
957 return eidx;
961 static void rx_int(struct net_device *dev, u32 rxlimit, u32 index)
963 struct rr_private *rrpriv = (struct rr_private *)dev->priv;
964 struct rr_regs *regs = rrpriv->regs;
966 do {
967 struct rx_desc *desc;
968 u32 pkt_len;
970 desc = &(rrpriv->rx_ring[index]);
971 pkt_len = desc->size;
972 #if (DEBUG > 2)
973 printk("index %i, rxlimit %i\n", index, rxlimit);
974 printk("len %x, mode %x\n", pkt_len, desc->mode);
975 #endif
976 if ( (rrpriv->rx_ring[index].mode & PACKET_BAD) == PACKET_BAD){
977 rrpriv->stats.rx_dropped++;
978 goto defer;
981 if (pkt_len > 0){
982 struct sk_buff *skb, *rx_skb;
984 rx_skb = rrpriv->rx_skbuff[index];
986 pci_dma_sync_single(rrpriv->pci_dev, desc->addr.addrlo,
987 pkt_len, PCI_DMA_FROMDEVICE);
989 if (pkt_len < PKT_COPY_THRESHOLD) {
990 skb = alloc_skb(pkt_len, GFP_ATOMIC);
991 if (skb == NULL){
992 printk(KERN_WARNING "%s: Unable to allocate skb (%i bytes), deferring packet\n", dev->name, pkt_len);
993 rrpriv->stats.rx_dropped++;
994 goto defer;
995 }else
996 memcpy(skb_put(skb, pkt_len),
997 rx_skb->data, pkt_len);
998 }else{
999 struct sk_buff *newskb;
1001 newskb = alloc_skb(dev->mtu + HIPPI_HLEN,
1002 GFP_ATOMIC);
1003 if (newskb){
1004 dma_addr_t addr;
1006 pci_unmap_single(rrpriv->pci_dev,
1007 desc->addr.addrlo, dev->mtu +
1008 HIPPI_HLEN, PCI_DMA_FROMDEVICE);
1009 skb = rx_skb;
1010 skb_put(skb, pkt_len);
1011 rrpriv->rx_skbuff[index] = newskb;
1012 addr = pci_map_single(rrpriv->pci_dev,
1013 newskb->data,
1014 dev->mtu + HIPPI_HLEN,
1015 PCI_DMA_FROMDEVICE);
1016 set_rraddr(&desc->addr, addr);
1017 } else {
1018 printk("%s: Out of memory, deferring "
1019 "packet\n", dev->name);
1020 rrpriv->stats.rx_dropped++;
1021 goto defer;
1024 skb->dev = dev;
1025 skb->protocol = hippi_type_trans(skb, dev);
1027 netif_rx(skb); /* send it up */
1029 dev->last_rx = jiffies;
1030 rrpriv->stats.rx_packets++;
1031 rrpriv->stats.rx_bytes += pkt_len;
1033 defer:
1034 desc->mode = 0;
1035 desc->size = dev->mtu + HIPPI_HLEN;
1037 if ((index & 7) == 7)
1038 writel(index, &regs->IpRxPi);
1040 index = (index + 1) % RX_RING_ENTRIES;
1041 } while(index != rxlimit);
1043 rrpriv->cur_rx = index;
1044 wmb();
1048 static irqreturn_t rr_interrupt(int irq, void *dev_id, struct pt_regs *ptregs)
1050 struct rr_private *rrpriv;
1051 struct rr_regs *regs;
1052 struct net_device *dev = (struct net_device *)dev_id;
1053 u32 prodidx, rxindex, eidx, txcsmr, rxlimit, txcon;
1055 rrpriv = (struct rr_private *)dev->priv;
1056 regs = rrpriv->regs;
1058 if (!(readl(&regs->HostCtrl) & RR_INT))
1059 return IRQ_NONE;
1061 spin_lock(&rrpriv->lock);
1063 prodidx = readl(&regs->EvtPrd);
1064 txcsmr = (prodidx >> 8) & 0xff;
1065 rxlimit = (prodidx >> 16) & 0xff;
1066 prodidx &= 0xff;
1068 #if (DEBUG > 2)
1069 printk("%s: interrupt, prodidx = %i, eidx = %i\n", dev->name,
1070 prodidx, rrpriv->info->evt_ctrl.pi);
1071 #endif
1073 * Order here is important. We must handle events
1074 * before doing anything else in order to catch
1075 * such things as LLRC errors, etc -kbf
1078 eidx = rrpriv->info->evt_ctrl.pi;
1079 if (prodidx != eidx)
1080 eidx = rr_handle_event(dev, prodidx, eidx);
1082 rxindex = rrpriv->cur_rx;
1083 if (rxindex != rxlimit)
1084 rx_int(dev, rxlimit, rxindex);
1086 txcon = rrpriv->dirty_tx;
1087 if (txcsmr != txcon) {
1088 do {
1089 /* Due to occational firmware TX producer/consumer out
1090 * of sync. error need to check entry in ring -kbf
1092 if(rrpriv->tx_skbuff[txcon]){
1093 struct tx_desc *desc;
1094 struct sk_buff *skb;
1096 desc = &(rrpriv->tx_ring[txcon]);
1097 skb = rrpriv->tx_skbuff[txcon];
1099 rrpriv->stats.tx_packets++;
1100 rrpriv->stats.tx_bytes += skb->len;
1102 pci_unmap_single(rrpriv->pci_dev,
1103 desc->addr.addrlo, skb->len,
1104 PCI_DMA_TODEVICE);
1105 dev_kfree_skb_irq(skb);
1107 rrpriv->tx_skbuff[txcon] = NULL;
1108 desc->size = 0;
1109 set_rraddr(&rrpriv->tx_ring[txcon].addr, 0);
1110 desc->mode = 0;
1112 txcon = (txcon + 1) % TX_RING_ENTRIES;
1113 } while (txcsmr != txcon);
1114 wmb();
1116 rrpriv->dirty_tx = txcon;
1117 if (rrpriv->tx_full && rr_if_busy(dev) &&
1118 (((rrpriv->info->tx_ctrl.pi + 1) % TX_RING_ENTRIES)
1119 != rrpriv->dirty_tx)){
1120 rrpriv->tx_full = 0;
1121 netif_wake_queue(dev);
1125 eidx |= ((txcsmr << 8) | (rxlimit << 16));
1126 writel(eidx, &regs->EvtCon);
1127 wmb();
1129 spin_unlock(&rrpriv->lock);
1130 return IRQ_HANDLED;
1133 static void rr_timer(unsigned long data)
1135 struct net_device *dev = (struct net_device *)data;
1136 struct rr_private *rrpriv = (struct rr_private *)dev->priv;
1137 struct rr_regs *regs = rrpriv->regs;
1138 unsigned long flags;
1140 if (readl(&regs->HostCtrl) & NIC_HALTED){
1141 printk("%s: Restarting nic\n", dev->name);
1142 memset(rrpriv->rx_ctrl, 0, 256 * sizeof(struct ring_ctrl));
1143 memset(rrpriv->info, 0, sizeof(struct rr_info));
1144 wmb();
1146 rr_raz_tx(rrpriv, dev);
1147 rr_raz_rx(rrpriv, dev);
1149 if (rr_init1(dev)) {
1150 spin_lock_irqsave(&rrpriv->lock, flags);
1151 writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT,
1152 &regs->HostCtrl);
1153 spin_unlock_irqrestore(&rrpriv->lock, flags);
1156 rrpriv->timer.expires = RUN_AT(5*HZ);
1157 add_timer(&rrpriv->timer);
1161 static int rr_open(struct net_device *dev)
1163 struct rr_private *rrpriv = (struct rr_private *)dev->priv;
1164 struct pci_dev *pdev = rrpriv->pci_dev;
1165 struct rr_regs *regs;
1166 int ecode = 0;
1167 unsigned long flags;
1168 dma_addr_t dma_addr;
1170 regs = rrpriv->regs;
1172 if (rrpriv->fw_rev < 0x00020000) {
1173 printk(KERN_WARNING "%s: trying to configure device with "
1174 "obsolete firmware\n", dev->name);
1175 ecode = -EBUSY;
1176 goto error;
1179 rrpriv->rx_ctrl = pci_alloc_consistent(pdev,
1180 256 * sizeof(struct ring_ctrl),
1181 &dma_addr);
1182 if (!rrpriv->rx_ctrl) {
1183 ecode = -ENOMEM;
1184 goto error;
1186 rrpriv->rx_ctrl_dma = dma_addr;
1187 memset(rrpriv->rx_ctrl, 0, 256*sizeof(struct ring_ctrl));
1189 rrpriv->info = pci_alloc_consistent(pdev, sizeof(struct rr_info),
1190 &dma_addr);
1191 if (!rrpriv->info) {
1192 ecode = -ENOMEM;
1193 goto error;
1195 rrpriv->info_dma = dma_addr;
1196 memset(rrpriv->info, 0, sizeof(struct rr_info));
1197 wmb();
1199 spin_lock_irqsave(&rrpriv->lock, flags);
1200 writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT, &regs->HostCtrl);
1201 readl(&regs->HostCtrl);
1202 spin_unlock_irqrestore(&rrpriv->lock, flags);
1204 if (request_irq(dev->irq, rr_interrupt, SA_SHIRQ, rrpriv->name, dev))
1206 printk(KERN_WARNING "%s: Requested IRQ %d is busy\n",
1207 dev->name, dev->irq);
1208 ecode = -EAGAIN;
1209 goto error;
1212 if ((ecode = rr_init1(dev)))
1213 goto error;
1215 /* Set the timer to switch to check for link beat and perhaps switch
1216 to an alternate media type. */
1217 init_timer(&rrpriv->timer);
1218 rrpriv->timer.expires = RUN_AT(5*HZ); /* 5 sec. watchdog */
1219 rrpriv->timer.data = (unsigned long)dev;
1220 rrpriv->timer.function = &rr_timer; /* timer handler */
1221 add_timer(&rrpriv->timer);
1223 netif_start_queue(dev);
1225 MOD_INC_USE_COUNT;
1226 return ecode;
1228 error:
1229 spin_lock_irqsave(&rrpriv->lock, flags);
1230 writel(readl(&regs->HostCtrl)|HALT_NIC|RR_CLEAR_INT, &regs->HostCtrl);
1231 spin_unlock_irqrestore(&rrpriv->lock, flags);
1233 if (rrpriv->info) {
1234 pci_free_consistent(pdev, sizeof(struct rr_info), rrpriv->info,
1235 rrpriv->info_dma);
1236 rrpriv->info = NULL;
1238 if (rrpriv->rx_ctrl) {
1239 pci_free_consistent(pdev, sizeof(struct ring_ctrl),
1240 rrpriv->rx_ctrl, rrpriv->rx_ctrl_dma);
1241 rrpriv->rx_ctrl = NULL;
1244 netif_stop_queue(dev);
1246 return ecode;
1250 static inline void rr_raz_tx(struct rr_private *rrpriv,
1251 struct net_device *dev)
1253 int i;
1255 for (i = 0; i < TX_RING_ENTRIES; i++) {
1256 struct sk_buff *skb = rrpriv->tx_skbuff[i];
1258 if (skb) {
1259 struct tx_desc *desc = &(rrpriv->tx_ring[i]);
1261 pci_unmap_single(rrpriv->pci_dev, desc->addr.addrlo,
1262 skb->len, PCI_DMA_TODEVICE);
1263 desc->size = 0;
1264 set_rraddr(&desc->addr, 0);
1265 dev_kfree_skb(skb);
1266 rrpriv->tx_skbuff[i] = NULL;
1272 static inline void rr_raz_rx(struct rr_private *rrpriv,
1273 struct net_device *dev)
1275 int i;
1277 for (i = 0; i < RX_RING_ENTRIES; i++) {
1278 struct sk_buff *skb = rrpriv->rx_skbuff[i];
1280 if (skb) {
1281 struct rx_desc *desc = &(rrpriv->rx_ring[i]);
1283 pci_unmap_single(rrpriv->pci_dev, desc->addr.addrlo,
1284 dev->mtu + HIPPI_HLEN, PCI_DMA_FROMDEVICE);
1285 desc->size = 0;
1286 set_rraddr(&desc->addr, 0);
1287 dev_kfree_skb(skb);
1288 rrpriv->rx_skbuff[i] = NULL;
1293 static void rr_dump(struct net_device *dev)
1295 struct rr_private *rrpriv;
1296 struct rr_regs *regs;
1297 u32 index, cons;
1298 short i;
1299 int len;
1301 rrpriv = (struct rr_private *)dev->priv;
1302 regs = rrpriv->regs;
1304 printk("%s: dumping NIC TX rings\n", dev->name);
1306 printk("RxPrd %08x, TxPrd %02x, EvtPrd %08x, TxPi %02x, TxCtrlPi %02x\n",
1307 readl(&regs->RxPrd), readl(&regs->TxPrd),
1308 readl(&regs->EvtPrd), readl(&regs->TxPi),
1309 rrpriv->info->tx_ctrl.pi);
1311 printk("Error code 0x%x\n", readl(&regs->Fail1));
1313 index = (((readl(&regs->EvtPrd) >> 8) & 0xff ) - 1) % EVT_RING_ENTRIES;
1314 cons = rrpriv->dirty_tx;
1315 printk("TX ring index %i, TX consumer %i\n",
1316 index, cons);
1318 if (rrpriv->tx_skbuff[index]){
1319 len = min_t(int, 0x80, rrpriv->tx_skbuff[index]->len);
1320 printk("skbuff for index %i is valid - dumping data (0x%x bytes - DMA len 0x%x)\n", index, len, rrpriv->tx_ring[index].size);
1321 for (i = 0; i < len; i++){
1322 if (!(i & 7))
1323 printk("\n");
1324 printk("%02x ", (unsigned char) rrpriv->tx_skbuff[index]->data[i]);
1326 printk("\n");
1329 if (rrpriv->tx_skbuff[cons]){
1330 len = min_t(int, 0x80, rrpriv->tx_skbuff[cons]->len);
1331 printk("skbuff for cons %i is valid - dumping data (0x%x bytes - skbuff len 0x%x)\n", cons, len, rrpriv->tx_skbuff[cons]->len);
1332 printk("mode 0x%x, size 0x%x,\n phys %08x, skbuff-addr %08lx, truesize 0x%x\n",
1333 rrpriv->tx_ring[cons].mode,
1334 rrpriv->tx_ring[cons].size,
1335 rrpriv->tx_ring[cons].addr.addrlo,
1336 (unsigned long)rrpriv->tx_skbuff[cons]->data,
1337 (unsigned int)rrpriv->tx_skbuff[cons]->truesize);
1338 for (i = 0; i < len; i++){
1339 if (!(i & 7))
1340 printk("\n");
1341 printk("%02x ", (unsigned char)rrpriv->tx_ring[cons].size);
1343 printk("\n");
1346 printk("dumping TX ring info:\n");
1347 for (i = 0; i < TX_RING_ENTRIES; i++)
1348 printk("mode 0x%x, size 0x%x, phys-addr %08x\n",
1349 rrpriv->tx_ring[i].mode,
1350 rrpriv->tx_ring[i].size,
1351 rrpriv->tx_ring[i].addr.addrlo);
1356 static int rr_close(struct net_device *dev)
1358 struct rr_private *rrpriv;
1359 struct rr_regs *regs;
1360 unsigned long flags;
1361 u32 tmp;
1362 short i;
1364 netif_stop_queue(dev);
1366 rrpriv = (struct rr_private *)dev->priv;
1367 regs = rrpriv->regs;
1370 * Lock to make sure we are not cleaning up while another CPU
1371 * is handling interrupts.
1373 spin_lock_irqsave(&rrpriv->lock, flags);
1375 tmp = readl(&regs->HostCtrl);
1376 if (tmp & NIC_HALTED){
1377 printk("%s: NIC already halted\n", dev->name);
1378 rr_dump(dev);
1379 }else{
1380 tmp |= HALT_NIC | RR_CLEAR_INT;
1381 writel(tmp, &regs->HostCtrl);
1382 readl(&regs->HostCtrl);
1385 rrpriv->fw_running = 0;
1387 del_timer_sync(&rrpriv->timer);
1389 writel(0, &regs->TxPi);
1390 writel(0, &regs->IpRxPi);
1392 writel(0, &regs->EvtCon);
1393 writel(0, &regs->EvtPrd);
1395 for (i = 0; i < CMD_RING_ENTRIES; i++)
1396 writel(0, &regs->CmdRing[i]);
1398 rrpriv->info->tx_ctrl.entries = 0;
1399 rrpriv->info->cmd_ctrl.pi = 0;
1400 rrpriv->info->evt_ctrl.pi = 0;
1401 rrpriv->rx_ctrl[4].entries = 0;
1403 rr_raz_tx(rrpriv, dev);
1404 rr_raz_rx(rrpriv, dev);
1406 pci_free_consistent(rrpriv->pci_dev, 256 * sizeof(struct ring_ctrl),
1407 rrpriv->rx_ctrl, rrpriv->rx_ctrl_dma);
1408 rrpriv->rx_ctrl = NULL;
1410 pci_free_consistent(rrpriv->pci_dev, sizeof(struct rr_info),
1411 rrpriv->info, rrpriv->info_dma);
1412 rrpriv->info = NULL;
1414 free_irq(dev->irq, dev);
1415 spin_unlock_irqrestore(&rrpriv->lock, flags);
1417 MOD_DEC_USE_COUNT;
1418 return 0;
1422 static int rr_start_xmit(struct sk_buff *skb, struct net_device *dev)
1424 struct rr_private *rrpriv = (struct rr_private *)dev->priv;
1425 struct rr_regs *regs = rrpriv->regs;
1426 struct ring_ctrl *txctrl;
1427 unsigned long flags;
1428 u32 index, len = skb->len;
1429 u32 *ifield;
1430 struct sk_buff *new_skb;
1432 if (readl(&regs->Mode) & FATAL_ERR)
1433 printk("error codes Fail1 %02x, Fail2 %02x\n",
1434 readl(&regs->Fail1), readl(&regs->Fail2));
1437 * We probably need to deal with tbusy here to prevent overruns.
1440 if (skb_headroom(skb) < 8){
1441 printk("incoming skb too small - reallocating\n");
1442 if (!(new_skb = dev_alloc_skb(len + 8))) {
1443 dev_kfree_skb(skb);
1444 netif_wake_queue(dev);
1445 return -EBUSY;
1447 skb_reserve(new_skb, 8);
1448 skb_put(new_skb, len);
1449 memcpy(new_skb->data, skb->data, len);
1450 dev_kfree_skb(skb);
1451 skb = new_skb;
1454 ifield = (u32 *)skb_push(skb, 8);
1456 ifield[0] = 0;
1457 ifield[1] = skb->private.ifield;
1460 * We don't need the lock before we are actually going to start
1461 * fiddling with the control blocks.
1463 spin_lock_irqsave(&rrpriv->lock, flags);
1465 txctrl = &rrpriv->info->tx_ctrl;
1467 index = txctrl->pi;
1469 rrpriv->tx_skbuff[index] = skb;
1470 set_rraddr(&rrpriv->tx_ring[index].addr, pci_map_single(
1471 rrpriv->pci_dev, skb->data, len + 8, PCI_DMA_TODEVICE));
1472 rrpriv->tx_ring[index].size = len + 8; /* include IFIELD */
1473 rrpriv->tx_ring[index].mode = PACKET_START | PACKET_END;
1474 txctrl->pi = (index + 1) % TX_RING_ENTRIES;
1475 wmb();
1476 writel(txctrl->pi, &regs->TxPi);
1478 if (txctrl->pi == rrpriv->dirty_tx){
1479 rrpriv->tx_full = 1;
1480 netif_stop_queue(dev);
1483 spin_unlock_irqrestore(&rrpriv->lock, flags);
1485 dev->trans_start = jiffies;
1486 return 0;
1490 static struct net_device_stats *rr_get_stats(struct net_device *dev)
1492 struct rr_private *rrpriv;
1494 rrpriv = (struct rr_private *)dev->priv;
1496 return(&rrpriv->stats);
1501 * Read the firmware out of the EEPROM and put it into the SRAM
1502 * (or from user space - later)
1504 * This operation requires the NIC to be halted and is performed with
1505 * interrupts disabled and with the spinlock hold.
1507 static int rr_load_firmware(struct net_device *dev)
1509 struct rr_private *rrpriv;
1510 struct rr_regs *regs;
1511 unsigned long eptr, segptr;
1512 int i, j;
1513 u32 localctrl, sptr, len, tmp;
1514 u32 p2len, p2size, nr_seg, revision, io, sram_size;
1515 struct eeprom *hw = NULL;
1517 rrpriv = (struct rr_private *)dev->priv;
1518 regs = rrpriv->regs;
1520 if (dev->flags & IFF_UP)
1521 return -EBUSY;
1523 if (!(readl(&regs->HostCtrl) & NIC_HALTED)){
1524 printk("%s: Trying to load firmware to a running NIC.\n",
1525 dev->name);
1526 return -EBUSY;
1529 localctrl = readl(&regs->LocalCtrl);
1530 writel(0, &regs->LocalCtrl);
1532 writel(0, &regs->EvtPrd);
1533 writel(0, &regs->RxPrd);
1534 writel(0, &regs->TxPrd);
1537 * First wipe the entire SRAM, otherwise we might run into all
1538 * kinds of trouble ... sigh, this took almost all afternoon
1539 * to track down ;-(
1541 io = readl(&regs->ExtIo);
1542 writel(0, &regs->ExtIo);
1543 sram_size = rr_read_eeprom_word(rrpriv, (void *)8);
1545 for (i = 200; i < sram_size / 4; i++){
1546 writel(i * 4, &regs->WinBase);
1547 mb();
1548 writel(0, &regs->WinData);
1549 mb();
1551 writel(io, &regs->ExtIo);
1552 mb();
1554 eptr = (unsigned long)rr_read_eeprom_word(rrpriv,
1555 &hw->rncd_info.AddrRunCodeSegs);
1556 eptr = ((eptr & 0x1fffff) >> 3);
1558 p2len = rr_read_eeprom_word(rrpriv, (void *)(0x83*4));
1559 p2len = (p2len << 2);
1560 p2size = rr_read_eeprom_word(rrpriv, (void *)(0x84*4));
1561 p2size = ((p2size & 0x1fffff) >> 3);
1563 if ((eptr < p2size) || (eptr > (p2size + p2len))){
1564 printk("%s: eptr is invalid\n", dev->name);
1565 goto out;
1568 revision = rr_read_eeprom_word(rrpriv, &hw->manf.HeaderFmt);
1570 if (revision != 1){
1571 printk("%s: invalid firmware format (%i)\n",
1572 dev->name, revision);
1573 goto out;
1576 nr_seg = rr_read_eeprom_word(rrpriv, (void *)eptr);
1577 eptr +=4;
1578 #if (DEBUG > 1)
1579 printk("%s: nr_seg %i\n", dev->name, nr_seg);
1580 #endif
1582 for (i = 0; i < nr_seg; i++){
1583 sptr = rr_read_eeprom_word(rrpriv, (void *)eptr);
1584 eptr += 4;
1585 len = rr_read_eeprom_word(rrpriv, (void *)eptr);
1586 eptr += 4;
1587 segptr = (unsigned long)rr_read_eeprom_word(rrpriv, (void *)eptr);
1588 segptr = ((segptr & 0x1fffff) >> 3);
1589 eptr += 4;
1590 #if (DEBUG > 1)
1591 printk("%s: segment %i, sram address %06x, length %04x, segptr %06x\n",
1592 dev->name, i, sptr, len, segptr);
1593 #endif
1594 for (j = 0; j < len; j++){
1595 tmp = rr_read_eeprom_word(rrpriv, (void *)segptr);
1596 writel(sptr, &regs->WinBase);
1597 mb();
1598 writel(tmp, &regs->WinData);
1599 mb();
1600 segptr += 4;
1601 sptr += 4;
1605 out:
1606 writel(localctrl, &regs->LocalCtrl);
1607 mb();
1608 return 0;
1612 static int rr_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1614 struct rr_private *rrpriv;
1615 unsigned char *image, *oldimage;
1616 unsigned long flags;
1617 unsigned int i;
1618 int error = -EOPNOTSUPP;
1620 rrpriv = dev->priv;
1622 switch(cmd){
1623 case SIOCRRGFW:
1624 if (!capable(CAP_SYS_RAWIO)){
1625 return -EPERM;
1628 image = kmalloc(EEPROM_WORDS * sizeof(u32), GFP_KERNEL);
1629 if (!image){
1630 printk(KERN_ERR "%s: Unable to allocate memory "
1631 "for EEPROM image\n", dev->name);
1632 return -ENOMEM;
1636 if (rrpriv->fw_running){
1637 printk("%s: Firmware already running\n", dev->name);
1638 error = -EPERM;
1639 goto gf_out;
1642 spin_lock_irqsave(&rrpriv->lock, flags);
1643 i = rr_read_eeprom(rrpriv, 0, image, EEPROM_BYTES);
1644 if (i != EEPROM_BYTES){
1645 printk(KERN_ERR "%s: Error reading EEPROM\n",
1646 dev->name);
1647 error = -EFAULT;
1648 goto gf_out;
1650 spin_unlock_irqrestore(&rrpriv->lock, flags);
1651 error = copy_to_user(rq->ifr_data, image, EEPROM_BYTES);
1652 if (error)
1653 error = -EFAULT;
1654 gf_out:
1655 kfree(image);
1656 return error;
1658 case SIOCRRPFW:
1659 if (!capable(CAP_SYS_RAWIO)){
1660 return -EPERM;
1663 image = kmalloc(EEPROM_WORDS * sizeof(u32), GFP_KERNEL);
1664 oldimage = kmalloc(EEPROM_WORDS * sizeof(u32), GFP_KERNEL);
1665 if (!image || !oldimage) {
1666 printk(KERN_ERR "%s: Unable to allocate memory "
1667 "for EEPROM image\n", dev->name);
1668 error = -ENOMEM;
1669 goto wf_out;
1672 error = copy_from_user(image, rq->ifr_data, EEPROM_BYTES);
1673 if (error) {
1674 error = -EFAULT;
1675 goto wf_out;
1678 if (rrpriv->fw_running){
1679 printk("%s: Firmware already running\n", dev->name);
1680 error = -EPERM;
1681 goto wf_out;
1684 printk("%s: Updating EEPROM firmware\n", dev->name);
1686 spin_lock_irqsave(&rrpriv->lock, flags);
1687 error = write_eeprom(rrpriv, 0, image, EEPROM_BYTES);
1688 if (error)
1689 printk(KERN_ERR "%s: Error writing EEPROM\n",
1690 dev->name);
1692 i = rr_read_eeprom(rrpriv, 0, oldimage, EEPROM_BYTES);
1693 spin_unlock_irqrestore(&rrpriv->lock, flags);
1695 if (i != EEPROM_BYTES)
1696 printk(KERN_ERR "%s: Error reading back EEPROM "
1697 "image\n", dev->name);
1699 error = memcmp(image, oldimage, EEPROM_BYTES);
1700 if (error){
1701 printk(KERN_ERR "%s: Error verifying EEPROM image\n",
1702 dev->name);
1703 error = -EFAULT;
1705 wf_out:
1706 if (oldimage)
1707 kfree(oldimage);
1708 if (image)
1709 kfree(image);
1710 return error;
1712 case SIOCRRID:
1713 return put_user(0x52523032, (int *)(&rq->ifr_data[0]));
1714 default:
1715 return error;
1719 static struct pci_device_id rr_pci_tbl[] __devinitdata = {
1720 { PCI_VENDOR_ID_ESSENTIAL, PCI_DEVICE_ID_ESSENTIAL_ROADRUNNER,
1721 PCI_ANY_ID, PCI_ANY_ID, },
1722 { 0,}
1724 MODULE_DEVICE_TABLE(pci, rr_pci_tbl);
1726 static struct pci_driver rr_driver = {
1727 .name = "rrunner",
1728 .id_table = rr_pci_tbl,
1729 .probe = rr_init_one,
1730 .remove = rr_remove_one,
1733 static int __init rr_init_module(void)
1735 return pci_module_init(&rr_driver);
1738 static void __exit rr_cleanup_module(void)
1740 pci_unregister_driver(&rr_driver);
1743 module_init(rr_init_module);
1744 module_exit(rr_cleanup_module);
1747 * Local variables:
1748 * 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"
1749 * End: