Linux 2.1.127
[davej-history.git] / drivers / net / sktr.c
blobf86acd1fbef943317775309ef769f10789e80faa
1 /*
2 * sktr.c: A network driver for the SysKonnect Token Ring ISA/PCI Adapters.
4 * Written 1997 by Christoph Goos
6 * A fine result of the Linux Systems Network Architecture Project.
7 * http://samba.anu.edu.au/linux-sna/
9 * This software may be used and distributed according to the terms
10 * of the GNU Public License, incorporated herein by reference.
12 * This device driver works with the following SysKonnect adapters:
13 * - SysKonnect TR4/16(+) ISA (SK-4190)
14 * - SysKonnect TR4/16(+) PCI (SK-4590)
15 * - SysKonnect TR4/16 PCI (SK-4591)
17 * Sources:
18 * - The hardware related parts of this driver are take from
19 * the SysKonnect Token Ring driver for Windows NT.
20 * - I used the IBM Token Ring driver 'ibmtr.c' as a base for this
21 * driver, as well as the 'skeleton.c' driver by Donald Becker.
22 * - Also various other drivers in the linux source tree were taken
23 * as samples for some tasks.
25 * Maintainer(s):
26 * JS Jay Schulist jschlst@samba.anu.edu.au
27 * CG Christoph Goos cgoos@syskonnect.de
29 * Modification History:
30 * 29-Aug-97 CG Created
31 * 04-Apr-98 CG Fixed problems caused by tok_timer_check
32 * 10-Apr-98 CG Fixed lockups at cable disconnection
33 * 27-May-98 JS Formated to Linux Kernel Format
34 * 31-May-98 JS Hacked in PCI support
35 * 16-Jun-98 JS Modulized for multiple cards with one driver
37 * To do:
38 * 1. Selectable 16 Mbps or 4Mbps
39 * 2. Multi/Broadcast packet handling
43 static const char *version = "sktr.c: v1.01 08/29/97 by Christoph Goos\n";
45 #ifdef MODULE
46 #include <linux/module.h>
47 #include <linux/version.h>
48 #endif
50 #include <linux/kernel.h>
51 #include <linux/sched.h>
52 #include <linux/types.h>
53 #include <linux/fcntl.h>
54 #include <linux/interrupt.h>
55 #include <linux/ptrace.h>
56 #include <linux/ioport.h>
57 #include <linux/in.h>
58 #include <linux/malloc.h>
59 #include <linux/string.h>
60 #include <linux/time.h>
61 #include <asm/system.h>
62 #include <asm/bitops.h>
63 #include <asm/io.h>
64 #include <asm/dma.h>
65 #include <asm/irq.h>
66 #include <linux/errno.h>
67 #include <linux/init.h>
68 #include <linux/pci.h>
70 #include <linux/netdevice.h>
71 #include <linux/etherdevice.h>
72 #include <linux/skbuff.h>
73 #include <linux/trdevice.h>
75 #include "sktr.h" /* Our Stuff */
76 #include "sktr_firmware.h" /* SysKonnect adapter firmware */
78 /* A zero-terminated list of I/O addresses to be probed. */
79 static unsigned int sktr_portlist[] __initdata = {
80 0x0A20, 0x1A20, 0x0B20, 0x1B20, 0x0980, 0x1980, 0x0900, 0x1900,
84 /* A zero-terminated list of IRQs to be probed.
85 * Used again after initial probe for sktr_chipset_init, called from sktr_open.
87 static unsigned short sktr_irqlist[] = {
88 3, 5, 9, 10, 11, 12, 15,
92 /* A zero-terminated list of DMAs to be probed. */
93 static int sktr_dmalist[] __initdata = {
94 5, 6, 7,
98 /* Card names */
99 static char *pci_cardname = "SK NET TR 4/16 PCI\0";
100 static char *isa_cardname = "SK NET TR 4/16 ISA\0";
101 static char *AdapterName;
103 /* Use 0 for production, 1 for verification, 2 for debug, and
104 * 3 for very verbose debug.
106 #ifndef SKTR_DEBUG
107 #define SKTR_DEBUG 1
108 #endif
109 static unsigned int sktr_debug = SKTR_DEBUG;
111 /* The number of low I/O ports used by the tokencard. */
112 #define SKTR_IO_EXTENT 32
114 /* Index to functions, as function prototypes.
115 * Alphabetical by function name.
118 /* "B" */
119 static int sktr_bringup_diags(struct device *dev);
120 /* "C" */
121 static void sktr_cancel_tx_queue(struct net_local* tp);
122 static int sktr_chipset_init(struct device *dev);
123 static void sktr_chk_irq(struct device *dev);
124 static unsigned char sktr_chk_frame(struct device *dev, unsigned char *Addr);
125 static void sktr_chk_outstanding_cmds(struct device *dev);
126 static void sktr_chk_src_addr(unsigned char *frame, unsigned char *hw_addr);
127 static unsigned char sktr_chk_ssb(struct net_local *tp, unsigned short IrqType);
128 static int sktr_close(struct device *dev);
129 static void sktr_cmd_status_irq(struct device *dev);
130 /* "D" */
131 static void sktr_disable_interrupts(struct device *dev);
132 static void sktr_dump(unsigned char *Data, int length);
133 /* "E" */
134 static void sktr_enable_interrupts(struct device *dev);
135 static void sktr_exec_cmd(struct device *dev, unsigned short Command);
136 static void sktr_exec_sifcmd(struct device *dev, unsigned int WriteValue);
137 /* "F" */
138 static unsigned char *sktr_fix_srouting(unsigned char *buf, short *FrameLen);
139 /* "G" */
140 static struct enet_statistics *sktr_get_stats(struct device *dev);
141 /* "H" */
142 static void sktr_hardware_send_packet(struct device *dev,
143 struct net_local* tp);
144 /* "I" */
145 static int sktr_init_adapter(struct device *dev);
146 static int sktr_init_card(struct device *dev);
147 static void sktr_init_ipb(struct net_local *tp);
148 static void sktr_init_net_local(struct device *dev);
149 static void sktr_init_opb(struct net_local *tp);
150 static void sktr_interrupt(int irq, void *dev_id, struct pt_regs *regs);
151 static int sktr_isa_chk_card(struct device *dev, int ioaddr);
152 static int sktr_isa_chk_ioaddr(int ioaddr);
153 /* "O" */
154 static int sktr_open(struct device *dev);
155 static void sktr_open_adapter(struct device *dev);
156 /* "P" */
157 static int sktr_pci_chk_card(struct device *dev);
158 int sktr_probe(struct device *dev);
159 static int sktr_probe1(struct device *dev, int ioaddr);
160 /* "R" */
161 static void sktr_rcv_status_irq(struct device *dev);
162 static void sktr_read_addr(struct device *dev, unsigned char *Address);
163 static void sktr_read_ptr(struct device *dev);
164 static void sktr_read_ram(struct device *dev, unsigned char *Data,
165 unsigned short Address, int Length);
166 static int sktr_reset_adapter(struct device *dev);
167 static void sktr_reset_interrupt(struct device *dev);
168 static void sktr_ring_status_irq(struct device *dev);
169 /* "S" */
170 static int sktr_send_packet(struct sk_buff *skb, struct device *dev);
171 static void sktr_set_multicast_list(struct device *dev);
172 /* "T" */
173 static void sktr_timer_chk(unsigned long data);
174 static void sktr_timer_end_wait(unsigned long data);
175 static void sktr_tx_status_irq(struct device *dev);
176 /* "U" */
177 static void sktr_update_rcv_stats(struct net_local *tp,
178 unsigned char DataPtr[], unsigned int Length);
179 /* "W" */
180 static void sktr_wait(unsigned long time);
181 static void sktr_write_rpl_status(RPL *rpl, unsigned int Status);
182 static void sktr_write_tpl_status(TPL *tpl, unsigned int Status);
185 * Check for a network adapter of this type, and return '0' if one exists.
186 * If dev->base_addr == 0, probe all likely locations.
187 * If dev->base_addr == 1, always return failure.
189 __initfunc(int sktr_probe(struct device *dev))
191 int i;
192 int base_addr = dev ? dev->base_addr : 0;
194 if(base_addr > 0x1ff) /* Check a single specified location. */
195 return (sktr_probe1(dev, base_addr));
196 else if(base_addr != 0) /* Don't probe at all. */
197 return (-ENXIO);
199 for(i = 0; sktr_portlist[i]; i++)
201 int ioaddr = sktr_portlist[i];
202 if(check_region(ioaddr, SKTR_IO_EXTENT))
203 continue;
204 if(sktr_probe1(dev, ioaddr))
206 #ifndef MODULE
207 tr_freedev(dev);
208 #endif
210 else
211 return (0);
214 return (-ENODEV);
218 * Detect and setup the PCI SysKonnect TR cards in slot order.
220 __initfunc(static int sktr_pci_chk_card(struct device *dev))
222 static int pci_index = 0;
223 unsigned char pci_bus, pci_device_fn;
225 if(!pci_present())
226 return (-1); /* No PCI present. */
228 for(; pci_index < 0xff; pci_index++)
230 unsigned int pci_irq_line;
231 struct pci_dev *pdev;
232 unsigned short pci_command, new_command, vendor, device;
233 unsigned int pci_ioaddr;
235 if(pcibios_find_class(PCI_CLASS_NETWORK_TOKEN_RING << 8,
236 pci_index, &pci_bus, &pci_device_fn)
237 != PCIBIOS_SUCCESSFUL)
239 break;
242 pcibios_read_config_word(pci_bus, pci_device_fn,
243 PCI_VENDOR_ID, &vendor);
244 pcibios_read_config_word(pci_bus, pci_device_fn,
245 PCI_DEVICE_ID, &device);
247 pdev = pci_find_slot(pci_bus, pci_device_fn);
248 pci_irq_line = pdev->irq;
249 pci_ioaddr = pdev->base_address[0];
251 pcibios_read_config_word(pci_bus, pci_device_fn,
252 PCI_COMMAND, &pci_command);
254 /* Remove I/O space marker in bit 0. */
255 pci_ioaddr &= ~3;
257 if(vendor != PCI_VENDOR_ID_SK)
258 continue;
259 if(device != PCI_DEVICE_ID_SK_TR)
260 continue;
261 if(check_region(pci_ioaddr, SKTR_IO_EXTENT))
262 continue;
263 request_region(pci_ioaddr, SKTR_IO_EXTENT, pci_cardname);
264 if(request_irq(pdev->irq, sktr_interrupt, SA_SHIRQ,
265 pci_cardname, dev))
266 return (-ENODEV); /* continue; ?? */
268 AdapterName = pci_cardname;
270 new_command = (pci_command|PCI_COMMAND_MASTER|PCI_COMMAND_IO);
272 if(pci_command != new_command)
274 printk("The PCI BIOS has not enabled this"
275 "device! Updating PCI command %4.4x->%4.4x.\n",
276 pci_command, new_command);
277 pcibios_write_config_word(pci_bus, pci_device_fn,
278 PCI_COMMAND, new_command);
281 /* At this point we have found a valid PCI TR card. */
282 dev->base_addr = pci_ioaddr;
283 dev->irq = pci_irq_line;
284 dev->dma = 0;
286 printk("%s: %s found at %#4x, using IRQ %d.\n",
287 dev->name, AdapterName, pci_ioaddr, dev->irq);
289 return (0);
292 return (-1);
296 * Detect and setup the ISA SysKonnect TR cards.
298 __initfunc(static int sktr_isa_chk_card(struct device *dev, int ioaddr))
300 int i, err;
301 unsigned long flags;
303 err = sktr_isa_chk_ioaddr(ioaddr);
304 if(err < 0)
305 return (-ENODEV);
307 if(virt_to_bus((void*)((unsigned long)dev->priv+sizeof(struct net_local)))
308 > ISA_MAX_ADDRESS)
310 printk("%s: Memory not accessible for DMA\n", dev->name);
311 kfree(dev->priv);
312 return (-EAGAIN);
315 AdapterName = isa_cardname;
317 /* Grab the region so that no one else tries to probe our ioports. */
318 request_region(ioaddr, SKTR_IO_EXTENT, AdapterName);
319 dev->base_addr = ioaddr;
321 /* Autoselect IRQ and DMA if dev->irq == 0 */
322 if(dev->irq == 0)
324 for(i = 0; sktr_irqlist[i] != 0; i++)
326 dev->irq = sktr_irqlist[i];
327 err = request_irq(dev->irq, &sktr_interrupt, 0, AdapterName, dev);
328 if(!err)
329 break;
332 if(sktr_irqlist[i] == 0)
334 printk("%s: AutoSelect no IRQ available\n", dev->name);
335 return (-EAGAIN);
338 else
340 err = request_irq(dev->irq, &sktr_interrupt, 0, AdapterName, dev);
341 if(err)
343 printk("%s: Selected IRQ not available\n", dev->name);
344 return (-EAGAIN);
348 /* Always allocate the DMA channel after IRQ and clean up on failure */
349 if(dev->dma == 0)
351 for(i = 0; sktr_dmalist[i] != 0; i++)
353 dev->dma = sktr_dmalist[i];
354 err = request_dma(dev->dma, AdapterName);
355 if(!err)
356 break;
359 if(dev->dma == 0)
361 printk("%s: AutoSelect no DMA available\n", dev->name);
362 free_irq(dev->irq, NULL);
363 return (-EAGAIN);
366 else
368 err = request_dma(dev->dma, AdapterName);
369 if(err)
371 printk("%s: Selected DMA not available\n", dev->name);
372 free_irq(dev->irq, NULL);
373 return (-EAGAIN);
377 flags=claim_dma_lock();
378 disable_dma(dev->dma);
379 set_dma_mode(dev->dma, DMA_MODE_CASCADE);
380 enable_dma(dev->dma);
381 release_dma_lock(flags);
383 printk("%s: %s found at %#4x, using IRQ %d and DMA %d.\n",
384 dev->name, AdapterName, ioaddr, dev->irq, dev->dma);
386 return (0);
389 __initfunc(static int sktr_probe1(struct device *dev, int ioaddr))
391 static unsigned version_printed = 0;
392 struct net_local *tp;
393 int err;
395 if(sktr_debug && version_printed++ == 0)
396 printk("%s", version);
398 #ifndef MODULE
399 dev = init_trdev(dev, 0);
400 if(dev == NULL)
401 return (-ENOMEM);
402 #endif
404 err = sktr_pci_chk_card(dev);
405 if(err < 0)
407 err = sktr_isa_chk_card(dev, ioaddr);
408 if(err < 0)
409 return (-ENODEV);
412 /* Setup this devices private information structure */
413 tp = (struct net_local *)kmalloc(sizeof(struct net_local), GFP_KERNEL | GFP_DMA);
414 if(tp == NULL)
415 return (-ENOMEM);
416 memset(tp, 0, sizeof(struct net_local));
418 dev->priv = tp;
419 dev->init = sktr_init_card;
420 dev->open = sktr_open;
421 dev->stop = sktr_close;
422 dev->hard_start_xmit = sktr_send_packet;
423 dev->get_stats = sktr_get_stats;
424 dev->set_multicast_list = &sktr_set_multicast_list;
426 return (0);
429 /* Dummy function */
430 __initfunc(static int sktr_init_card(struct device *dev))
432 if(sktr_debug > 3)
433 printk("%s: sktr_init_card\n", dev->name);
435 return (0);
439 * This function tests if an adapter is really installed at the
440 * given I/O address. Return negative if no adapter at IO addr.
442 __initfunc(static int sktr_isa_chk_ioaddr(int ioaddr))
444 unsigned char old, chk1, chk2;
446 old = inb(ioaddr + SIFADR); /* Get the old SIFADR value */
448 chk1 = 0; /* Begin with check value 0 */
449 do {
450 /* Write new SIFADR value */
451 outb(chk1, ioaddr + SIFADR);
453 /* Read, invert and write */
454 chk2 = inb(ioaddr + SIFADD);
455 chk2 ^= 0x0FE;
456 outb(chk2, ioaddr + SIFADR);
458 /* Read, invert and compare */
459 chk2 = inb(ioaddr + SIFADD);
460 chk2 ^= 0x0FE;
462 if(chk1 != chk2)
463 return (-1); /* No adapter */
465 chk1 -= 2;
466 } while(chk1 != 0); /* Repeat 128 times (all byte values) */
468 /* Restore the SIFADR value */
469 outb(old, ioaddr + SIFADR);
471 return (0);
475 * Open/initialize the board. This is called sometime after
476 * booting when the 'ifconfig' program is run.
478 * This routine should set everything up anew at each open, even
479 * registers that "should" only need to be set once at boot, so that
480 * there is non-reboot way to recover if something goes wrong.
482 static int sktr_open(struct device *dev)
484 struct net_local *tp = (struct net_local *)dev->priv;
485 int err;
487 /* Reset the hardware here. Don't forget to set the station address. */
488 err = sktr_chipset_init(dev);
489 if(err)
491 printk(KERN_INFO "%s: Chipset initialization error\n",
492 dev->name);
493 return (-1);
496 dev->addr_len = 6;
497 sktr_read_addr(dev, (unsigned char*)dev->dev_addr);
499 init_timer(&tp->timer);
500 tp->timer.expires = jiffies + 30*HZ;
501 tp->timer.function = sktr_timer_end_wait;
502 tp->timer.data = (unsigned long)dev;
503 tp->timer.next = NULL;
504 tp->timer.prev = NULL;
505 add_timer(&tp->timer);
507 sktr_read_ptr(dev);
508 sktr_enable_interrupts(dev);
509 sktr_open_adapter(dev);
511 dev->tbusy = 0;
512 dev->interrupt = 0;
513 dev->start = 0;
515 /* Wait for interrupt from hardware. If interrupt does not come,
516 * there will be a timeout from the timer.
518 tp->Sleeping = 1;
519 interruptible_sleep_on(&tp->wait_for_tok_int);
520 del_timer(&tp->timer);
522 /* If AdapterVirtOpenFlag is 1, the adapter is now open for use */
523 if(tp->AdapterVirtOpenFlag == 0)
525 sktr_disable_interrupts(dev);
526 return (-1);
529 dev->start = 1;
531 tp->StartTime = jiffies;
533 /* Start function control timer */
534 tp->timer.expires = jiffies + 2*HZ;
535 tp->timer.function = sktr_timer_chk;
536 tp->timer.data = (unsigned long)dev;
537 add_timer(&tp->timer);
539 #ifdef MODULE
540 MOD_INC_USE_COUNT;
541 #endif
543 return (0);
547 * Timeout function while waiting for event
549 static void sktr_timer_end_wait(unsigned long data)
551 struct device *dev = (struct device*)data;
552 struct net_local *tp = (struct net_local *)dev->priv;
554 if(tp->Sleeping)
556 tp->Sleeping = 0;
557 wake_up_interruptible(&tp->wait_for_tok_int);
560 return;
564 * Initialize the chipset
566 static int sktr_chipset_init(struct device *dev)
568 struct net_local *tp = (struct net_local *)dev->priv;
569 unsigned char PosReg, Tmp;
570 int i, err;
572 sktr_init_ipb(tp);
573 sktr_init_opb(tp);
574 sktr_init_net_local(dev);
576 /* Set pos register: selects irq and dma channel.
577 * Only for ISA bus adapters.
579 if(dev->dma > 0)
581 PosReg = 0;
582 for(i = 0; sktr_irqlist[i] != 0; i++)
584 if(sktr_irqlist[i] == dev->irq)
585 break;
588 /* Choose default cycle time, 500 nsec */
589 PosReg |= CYCLE_TIME << 2;
590 PosReg |= i << 4;
591 i = dev->dma - 5;
592 PosReg |= i;
594 if(tp->DataRate == SPEED_4)
595 PosReg |= LINE_SPEED_BIT;
596 else
597 PosReg &= ~LINE_SPEED_BIT;
599 outb(PosReg, dev->base_addr + POSREG);
600 Tmp = inb(dev->base_addr + POSREG);
601 if((Tmp & ~CYCLE_TIME) != (PosReg & ~CYCLE_TIME))
602 printk(KERN_INFO "%s: POSREG error\n", dev->name);
605 err = sktr_reset_adapter(dev);
606 if(err < 0)
607 return (-1);
609 err = sktr_bringup_diags(dev);
610 if(err < 0)
611 return (-1);
613 err = sktr_init_adapter(dev);
614 if(err < 0)
615 return (-1);
617 return (0);
621 * Initializes the net_local structure.
623 static void sktr_init_net_local(struct device *dev)
625 struct net_local *tp = (struct net_local *)dev->priv;
626 int i;
628 tp->scb.CMD = 0;
629 tp->scb.Parm[0] = 0;
630 tp->scb.Parm[1] = 0;
632 tp->ssb.STS = 0;
633 tp->ssb.Parm[0] = 0;
634 tp->ssb.Parm[1] = 0;
635 tp->ssb.Parm[2] = 0;
637 tp->CMDqueue = 0;
639 tp->AdapterOpenFlag = 0;
640 tp->AdapterVirtOpenFlag = 0;
641 tp->ScbInUse = 0;
642 tp->OpenCommandIssued = 0;
643 tp->ReOpenInProgress = 0;
644 tp->HaltInProgress = 0;
645 tp->TransmitHaltScheduled = 0;
646 tp->LobeWireFaultLogged = 0;
647 tp->LastOpenStatus = 0;
648 tp->MaxPacketSize = DEFAULT_PACKET_SIZE;
650 skb_queue_head_init(&tp->SendSkbQueue);
651 tp->QueueSkb = MAX_TX_QUEUE;
653 /* Create circular chain of transmit lists */
654 for (i = 0; i < TPL_NUM; i++)
656 tp->Tpl[i].NextTPLAddr = htonl((unsigned long) virt_to_bus(&tp->Tpl[(i+1) % TPL_NUM]));
657 tp->Tpl[i].Status = 0;
658 tp->Tpl[i].FrameSize = 0;
659 tp->Tpl[i].FragList[0].DataCount = 0;
660 tp->Tpl[i].FragList[0].DataAddr = 0;
661 tp->Tpl[i].NextTPLPtr = &tp->Tpl[(i+1) % TPL_NUM];
662 tp->Tpl[i].MData = NULL;
663 tp->Tpl[i].TPLIndex = i;
664 tp->Tpl[i].BusyFlag = 0;
667 tp->TplFree = tp->TplBusy = &tp->Tpl[0];
669 /* Create circular chain of receive lists */
670 for (i = 0; i < RPL_NUM; i++)
672 tp->Rpl[i].NextRPLAddr = htonl((unsigned long) virt_to_bus(&tp->Rpl[(i+1) % RPL_NUM]));
673 tp->Rpl[i].Status = (RX_VALID | RX_START_FRAME | RX_END_FRAME | RX_FRAME_IRQ);
674 tp->Rpl[i].FrameSize = 0;
675 tp->Rpl[i].FragList[0].DataCount = SWAPB(tp->MaxPacketSize);
677 /* Alloc skb and point adapter to data area */
678 tp->Rpl[i].Skb = dev_alloc_skb(tp->MaxPacketSize);
680 /* skb == NULL ? then use local buffer */
681 if(tp->Rpl[i].Skb == NULL)
683 tp->Rpl[i].SkbStat = SKB_UNAVAILABLE;
684 tp->Rpl[i].FragList[0].DataAddr = htonl(virt_to_bus(tp->LocalRxBuffers[i]));
685 tp->Rpl[i].MData = tp->LocalRxBuffers[i];
687 else /* SKB != NULL */
689 tp->Rpl[i].Skb->dev = dev;
690 skb_put(tp->Rpl[i].Skb, tp->MaxPacketSize);
692 /* data unreachable for DMA ? then use local buffer */
693 if(virt_to_bus(tp->Rpl[i].Skb->data) + tp->MaxPacketSize > ISA_MAX_ADDRESS)
695 tp->Rpl[i].SkbStat = SKB_DATA_COPY;
696 tp->Rpl[i].FragList[0].DataAddr = htonl(virt_to_bus(tp->LocalRxBuffers[i]));
697 tp->Rpl[i].MData = tp->LocalRxBuffers[i];
699 else /* DMA directly in skb->data */
701 tp->Rpl[i].SkbStat = SKB_DMA_DIRECT;
702 tp->Rpl[i].FragList[0].DataAddr = htonl(virt_to_bus(tp->Rpl[i].Skb->data));
703 tp->Rpl[i].MData = tp->Rpl[i].Skb->data;
707 tp->Rpl[i].NextRPLPtr = &tp->Rpl[(i+1) % RPL_NUM];
708 tp->Rpl[i].RPLIndex = i;
711 tp->RplHead = &tp->Rpl[0];
712 tp->RplTail = &tp->Rpl[RPL_NUM-1];
713 tp->RplTail->Status = (RX_START_FRAME | RX_END_FRAME | RX_FRAME_IRQ);
715 return;
719 * Initializes the initialisation parameter block.
721 static void sktr_init_ipb(struct net_local *tp)
723 tp->ipb.Init_Options = BURST_MODE;
724 tp->ipb.CMD_Status_IV = 0;
725 tp->ipb.TX_IV = 0;
726 tp->ipb.RX_IV = 0;
727 tp->ipb.Ring_Status_IV = 0;
728 tp->ipb.SCB_Clear_IV = 0;
729 tp->ipb.Adapter_CHK_IV = 0;
730 tp->ipb.RX_Burst_Size = BURST_SIZE;
731 tp->ipb.TX_Burst_Size = BURST_SIZE;
732 tp->ipb.DMA_Abort_Thrhld = DMA_RETRIES;
733 tp->ipb.SCB_Addr = 0;
734 tp->ipb.SSB_Addr = 0;
736 return;
740 * Initializes the open parameter block.
742 static void sktr_init_opb(struct net_local *tp)
744 unsigned long Addr;
745 unsigned short RplSize = RPL_SIZE;
746 unsigned short TplSize = TPL_SIZE;
747 unsigned short BufferSize = BUFFER_SIZE;
749 tp->ocpl.OPENOptions = 0;
750 tp->ocpl.OPENOptions |= ENABLE_FULL_DUPLEX_SELECTION;
751 tp->ocpl.OPENOptions |= PAD_ROUTING_FIELD;
752 tp->ocpl.FullDuplex = 0;
753 tp->ocpl.FullDuplex |= OPEN_FULL_DUPLEX_OFF;
755 /* Fixme: If mac address setable:
756 * for (i=0; i<LENGTH_OF_ADDRESS; i++)
757 * mac->Vam->ocpl.NodeAddr[i] = mac->CurrentAddress[i];
760 tp->ocpl.GroupAddr = 0;
761 tp->ocpl.FunctAddr = 0;
762 tp->ocpl.RxListSize = SWAPB(RplSize);
763 tp->ocpl.TxListSize = SWAPB(TplSize);
764 tp->ocpl.BufSize = SWAPB(BufferSize);
765 tp->ocpl.Reserved = 0;
766 tp->ocpl.TXBufMin = TX_BUF_MIN;
767 tp->ocpl.TXBufMax = TX_BUF_MAX;
769 Addr = htonl(virt_to_bus(tp->ProductID));
771 tp->ocpl.ProdIDAddr[0] = LOWORD(Addr);
772 tp->ocpl.ProdIDAddr[1] = HIWORD(Addr);
774 return;
778 * Send OPEN command to adapter
780 static void sktr_open_adapter(struct device *dev)
782 struct net_local *tp = (struct net_local *)dev->priv;
784 if(tp->OpenCommandIssued)
785 return;
787 tp->OpenCommandIssued = 1;
788 sktr_exec_cmd(dev, OC_OPEN);
790 return;
794 * Clear the adapter's interrupt flag. Clear system interrupt enable
795 * (SINTEN): disable adapter to system interrupts.
797 static void sktr_disable_interrupts(struct device *dev)
799 outb(0, dev->base_addr + SIFACL);
801 return;
805 * Set the adapter's interrupt flag. Set system interrupt enable
806 * (SINTEN): enable adapter to system interrupts.
808 static void sktr_enable_interrupts(struct device *dev)
810 outb(ACL_SINTEN, dev->base_addr + SIFACL);
812 return;
816 * Put command in command queue, try to execute it.
818 static void sktr_exec_cmd(struct device *dev, unsigned short Command)
820 struct net_local *tp = (struct net_local *)dev->priv;
822 tp->CMDqueue |= Command;
823 sktr_chk_outstanding_cmds(dev);
825 return;
829 * Linux always gives 18 byte of source routing information in the frame header.
830 * But the length field can indicate shorter length. Then cut header
831 * appropriate.
833 static unsigned char *sktr_fix_srouting(unsigned char *buf, short *FrameLen)
835 struct trh_hdr *trh = (struct trh_hdr *)buf;
836 int len;
838 if(buf[8] & TR_RII)
840 trh->rcf &= ~SWAPB((unsigned short) TR_RCF_LONGEST_FRAME_MASK);
841 trh->rcf |= SWAPB((unsigned short) TR_RCF_FRAME4K);
842 len = (SWAPB(trh->rcf) & TR_RCF_LEN_MASK) >> 8;
843 if(len < 18)
845 memcpy(&buf[18-len],buf,sizeof(struct trh_hdr)-18+len);
846 *FrameLen -= (18 - len);
848 return (&buf[18-len]);
851 return (buf);
855 * Gets skb from system, queues it and checks if it can be sent
857 static int sktr_send_packet(struct sk_buff *skb, struct device *dev)
859 struct net_local *tp = (struct net_local *)dev->priv;
861 if(dev->tbusy)
864 * If we get here, some higher level has decided we are broken.
865 * There should really be a "kick me" function call instead.
867 * Resetting the token ring adapter takes a long time so just
868 * fake transmission time and go on trying. Our own timeout
869 * routine is in sktr_timer_chk()
871 dev->tbusy = 0;
872 dev->trans_start = jiffies;
873 return (1);
877 * If some higher layer thinks we've missed an tx-done interrupt we
878 * are passed NULL.
880 if(skb == NULL)
881 return (0);
884 * Block a timer-based transmit from overlapping. This could better be
885 * done with atomic_swap(1, dev->tbusy), but set_bit() works as well.
887 if(test_and_set_bit(0, (void*)&dev->tbusy) != 0)
889 printk("%s: Transmitter access conflict.\n", dev->name);
890 return (1);
893 if(tp->QueueSkb == 0)
894 return (1); /* Return with tbusy set: queue full */
896 tp->QueueSkb--;
897 skb_queue_tail(&tp->SendSkbQueue, skb);
898 sktr_hardware_send_packet(dev, tp);
899 if(tp->QueueSkb > 0)
900 dev->tbusy = 0;
902 return (0);
906 * Move frames from internal skb queue into adapter tx queue
908 static void sktr_hardware_send_packet(struct device *dev, struct net_local* tp)
910 TPL *tpl;
911 short length;
912 unsigned char *buf, *newbuf;
913 struct sk_buff *skb;
914 int i;
916 for(;;)
918 /* Try to get a free TPL from the chain.
920 * NOTE: We *must* always leave one unused TPL in the chain,
921 * because otherwise the adapter might send frames twice.
923 if(tp->TplFree->NextTPLPtr->BusyFlag) /* No free TPL */
925 printk(KERN_INFO "%s: No free TPL\n", dev->name);
926 return;
929 /* Send first buffer from queue */
930 skb = skb_dequeue(&tp->SendSkbQueue);
931 if(skb == NULL)
932 return;
934 tp->QueueSkb++;
935 /* Is buffer reachable for Busmaster-DMA? */
936 if(virt_to_bus((void*)(((long) skb->data) + skb->len))
937 > ISA_MAX_ADDRESS)
939 /* Copy frame to local buffer */
940 i = tp->TplFree->TPLIndex;
941 length = skb->len;
942 buf = tp->LocalTxBuffers[i];
943 memcpy(buf, skb->data, length);
944 newbuf = sktr_fix_srouting(buf, &length);
946 else
948 /* Send direct from skb->data */
949 length = skb->len;
950 newbuf = sktr_fix_srouting(skb->data, &length);
953 /* Source address in packet? */
954 sktr_chk_src_addr(newbuf, dev->dev_addr);
956 tp->LastSendTime = jiffies;
957 tpl = tp->TplFree; /* Get the "free" TPL */
958 tpl->BusyFlag = 1; /* Mark TPL as busy */
959 tp->TplFree = tpl->NextTPLPtr;
961 /* Save the skb for delayed return of skb to system */
962 tpl->Skb = skb;
963 tpl->FragList[0].DataCount = (unsigned short) SWAPB(length);
964 tpl->FragList[0].DataAddr = htonl(virt_to_bus(newbuf));
966 /* Write the data length in the transmit list. */
967 tpl->FrameSize = (unsigned short) SWAPB(length);
968 tpl->MData = newbuf;
970 /* Transmit the frame and set the status values. */
971 sktr_write_tpl_status(tpl, TX_VALID | TX_START_FRAME
972 | TX_END_FRAME | TX_PASS_SRC_ADDR
973 | TX_FRAME_IRQ);
975 /* Let adapter send the frame. */
976 sktr_exec_sifcmd(dev, CMD_TX_VALID);
979 return;
983 * Write the given value to the 'Status' field of the specified TPL.
984 * NOTE: This function should be used whenever the status of any TPL must be
985 * modified by the driver, because the compiler may otherwise change the
986 * order of instructions such that writing the TPL status may be executed at
987 * an undesireable time. When this function is used, the status is always
988 * written when the function is called.
990 static void sktr_write_tpl_status(TPL *tpl, unsigned int Status)
992 tpl->Status = Status;
995 static void sktr_chk_src_addr(unsigned char *frame, unsigned char *hw_addr)
997 unsigned char SRBit;
999 if((((unsigned long)frame[8]) & ~0x80) != 0) /* Compare 4 bytes */
1000 return;
1001 if((unsigned short)frame[12] != 0) /* Compare 2 bytes */
1002 return;
1004 SRBit = frame[8] & 0x80;
1005 memcpy(&frame[8], hw_addr, 6);
1006 frame[8] |= SRBit;
1008 return;
1012 * The timer routine: Check if adapter still open and working, reopen if not.
1014 static void sktr_timer_chk(unsigned long data)
1016 struct device *dev = (struct device*)data;
1017 struct net_local *tp = (struct net_local*)dev->priv;
1019 if(tp->HaltInProgress)
1020 return;
1022 sktr_chk_outstanding_cmds(dev);
1023 if(tp->LastSendTime + SEND_TIMEOUT < jiffies
1024 && (tp->QueueSkb < MAX_TX_QUEUE || tp->TplFree != tp->TplBusy))
1026 /* Anything to send, but stalled to long */
1027 tp->LastSendTime = jiffies;
1028 sktr_exec_cmd(dev, OC_CLOSE); /* Does reopen automatically */
1031 tp->timer.expires = jiffies + 2*HZ;
1032 add_timer(&tp->timer);
1034 if(tp->AdapterOpenFlag || tp->ReOpenInProgress)
1035 return;
1036 tp->ReOpenInProgress = 1;
1037 sktr_open_adapter(dev);
1039 return;
1043 * The typical workload of the driver: Handle the network interface interrupts.
1045 static void sktr_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1047 struct device *dev = dev_id;
1048 struct net_local *tp;
1049 int ioaddr;
1050 unsigned short irq_type;
1052 if(dev == NULL)
1054 printk("%s: irq %d for unknown device.\n", dev->name, irq);
1055 return;
1058 dev->interrupt = 1;
1060 ioaddr = dev->base_addr;
1061 tp = (struct net_local *)dev->priv;
1063 irq_type = inw(ioaddr + SIFSTS);
1065 while(irq_type & STS_SYSTEM_IRQ)
1067 irq_type &= STS_IRQ_MASK;
1069 if(!sktr_chk_ssb(tp, irq_type))
1071 printk(KERN_INFO "%s: DATA LATE occured\n", dev->name);
1072 break;
1075 switch(irq_type)
1077 case STS_IRQ_RECEIVE_STATUS:
1078 sktr_reset_interrupt(dev);
1079 sktr_rcv_status_irq(dev);
1080 break;
1082 case STS_IRQ_TRANSMIT_STATUS:
1083 /* Check if TRANSMIT.HALT command is complete */
1084 if(tp->ssb.Parm[0] & COMMAND_COMPLETE)
1086 tp->TransmitCommandActive = 0;
1087 tp->TransmitHaltScheduled = 0;
1089 /* Issue a new transmit command. */
1090 sktr_exec_cmd(dev, OC_TRANSMIT);
1093 sktr_reset_interrupt(dev);
1094 sktr_tx_status_irq(dev);
1095 break;
1097 case STS_IRQ_COMMAND_STATUS:
1098 /* The SSB contains status of last command
1099 * other than receive/transmit.
1101 sktr_cmd_status_irq(dev);
1102 break;
1104 case STS_IRQ_SCB_CLEAR:
1105 /* The SCB is free for another command. */
1106 tp->ScbInUse = 0;
1107 sktr_chk_outstanding_cmds(dev);
1108 break;
1110 case STS_IRQ_RING_STATUS:
1111 sktr_ring_status_irq(dev);
1112 break;
1114 case STS_IRQ_ADAPTER_CHECK:
1115 sktr_chk_irq(dev);
1116 break;
1118 default:
1119 printk(KERN_INFO "Unknown Token Ring IRQ\n");
1120 break;
1123 /* Reset system interrupt if not already done. */
1124 if(irq_type != STS_IRQ_TRANSMIT_STATUS
1125 && irq_type != STS_IRQ_RECEIVE_STATUS)
1127 sktr_reset_interrupt(dev);
1130 irq_type = inw(ioaddr + SIFSTS);
1133 dev->interrupt = 0;
1135 return;
1139 * Reset the INTERRUPT SYSTEM bit and issue SSB CLEAR command.
1141 static void sktr_reset_interrupt(struct device *dev)
1143 struct net_local *tp = (struct net_local *)dev->priv;
1144 SSB *ssb = &tp->ssb;
1147 * [Workaround for "Data Late"]
1148 * Set all fields of the SSB to well-defined values so we can
1149 * check if the adapter has written the SSB.
1152 ssb->STS = (unsigned short) -1;
1153 ssb->Parm[0] = (unsigned short) -1;
1154 ssb->Parm[1] = (unsigned short) -1;
1155 ssb->Parm[2] = (unsigned short) -1;
1157 /* Free SSB by issuing SSB_CLEAR command after reading IRQ code
1158 * and clear STS_SYSTEM_IRQ bit: enable adapter for further interrupts.
1160 sktr_exec_sifcmd(dev, CMD_SSB_CLEAR | CMD_CLEAR_SYSTEM_IRQ);
1162 return;
1166 * Check if the SSB has actually been written by the adapter.
1168 static unsigned char sktr_chk_ssb(struct net_local *tp, unsigned short IrqType)
1170 SSB *ssb = &tp->ssb; /* The address of the SSB. */
1172 /* C 0 1 2 INTERRUPT CODE
1173 * - - - - --------------
1174 * 1 1 1 1 TRANSMIT STATUS
1175 * 1 1 1 1 RECEIVE STATUS
1176 * 1 ? ? 0 COMMAND STATUS
1177 * 0 0 0 0 SCB CLEAR
1178 * 1 1 0 0 RING STATUS
1179 * 0 0 0 0 ADAPTER CHECK
1181 * 0 = SSB field not affected by interrupt
1182 * 1 = SSB field is affected by interrupt
1184 * C = SSB ADDRESS +0: COMMAND
1185 * 0 = SSB ADDRESS +2: STATUS 0
1186 * 1 = SSB ADDRESS +4: STATUS 1
1187 * 2 = SSB ADDRESS +6: STATUS 2
1190 /* Check if this interrupt does use the SSB. */
1192 if(IrqType != STS_IRQ_TRANSMIT_STATUS
1193 && IrqType != STS_IRQ_RECEIVE_STATUS
1194 && IrqType != STS_IRQ_COMMAND_STATUS
1195 && IrqType != STS_IRQ_RING_STATUS)
1197 return (1); /* SSB not involved. */
1200 /* Note: All fields of the SSB have been set to all ones (-1) after it
1201 * has last been used by the software (see DriverIsr()).
1203 * Check if the affected SSB fields are still unchanged.
1206 if(ssb->STS == (unsigned short) -1)
1207 return (0); /* Command field not yet available. */
1208 if(IrqType == STS_IRQ_COMMAND_STATUS)
1209 return (1); /* Status fields not always affected. */
1210 if(ssb->Parm[0] == (unsigned short) -1)
1211 return (0); /* Status 1 field not yet available. */
1212 if(IrqType == STS_IRQ_RING_STATUS)
1213 return (1); /* Status 2 & 3 fields not affected. */
1215 /* Note: At this point, the interrupt is either TRANSMIT or RECEIVE. */
1216 if(ssb->Parm[1] == (unsigned short) -1)
1217 return (0); /* Status 2 field not yet available. */
1218 if(ssb->Parm[2] == (unsigned short) -1)
1219 return (0); /* Status 3 field not yet available. */
1221 return (1); /* All SSB fields have been written by the adapter. */
1225 * Evaluates the command results status in the SSB status field.
1227 static void sktr_cmd_status_irq(struct device *dev)
1229 struct net_local *tp = (struct net_local *)dev->priv;
1230 unsigned short ssb_cmd, ssb_parm_0;
1231 unsigned short ssb_parm_1;
1232 char *open_err = "Open error -";
1233 char *code_err = "Open code -";
1235 /* Copy the ssb values to local variables */
1236 ssb_cmd = tp->ssb.STS;
1237 ssb_parm_0 = tp->ssb.Parm[0];
1238 ssb_parm_1 = tp->ssb.Parm[1];
1240 if(ssb_cmd == OPEN)
1242 tp->Sleeping = 0;
1243 if(!tp->ReOpenInProgress)
1244 wake_up_interruptible(&tp->wait_for_tok_int);
1246 tp->OpenCommandIssued = 0;
1247 tp->ScbInUse = 0;
1249 if((ssb_parm_0 & 0x00FF) == GOOD_COMPLETION)
1251 /* Success, the adapter is open. */
1252 tp->LobeWireFaultLogged = 0;
1253 tp->AdapterOpenFlag = 1;
1254 tp->AdapterVirtOpenFlag = 1;
1255 tp->TransmitCommandActive = 0;
1256 sktr_exec_cmd(dev, OC_TRANSMIT);
1257 sktr_exec_cmd(dev, OC_RECEIVE);
1259 if(tp->ReOpenInProgress)
1260 tp->ReOpenInProgress = 0;
1262 return;
1264 else /* The adapter did not open. */
1266 if(ssb_parm_0 & NODE_ADDR_ERROR)
1267 printk(KERN_INFO "%s: Node address error\n",
1268 dev->name);
1269 if(ssb_parm_0 & LIST_SIZE_ERROR)
1270 printk(KERN_INFO "%s: List size error\n",
1271 dev->name);
1272 if(ssb_parm_0 & BUF_SIZE_ERROR)
1273 printk(KERN_INFO "%s: Buffer size error\n",
1274 dev->name);
1275 if(ssb_parm_0 & TX_BUF_COUNT_ERROR)
1276 printk(KERN_INFO "%s: Tx buffer count error\n",
1277 dev->name);
1278 if(ssb_parm_0 & INVALID_OPEN_OPTION)
1279 printk(KERN_INFO "%s: Invalid open option\n",
1280 dev->name);
1281 if(ssb_parm_0 & OPEN_ERROR)
1283 /* Show the open phase. */
1284 switch(ssb_parm_0 & OPEN_PHASES_MASK)
1286 case LOBE_MEDIA_TEST:
1287 if(!tp->LobeWireFaultLogged)
1289 tp->LobeWireFaultLogged = 1;
1290 printk(KERN_INFO "%s: %s Lobe wire fault (check cable !).\n", dev->name, open_err);
1292 tp->ReOpenInProgress = 1;
1293 tp->AdapterOpenFlag = 0;
1294 tp->AdapterVirtOpenFlag = 1;
1295 sktr_open_adapter(dev);
1296 return;
1298 case PHYSICAL_INSERTION:
1299 printk(KERN_INFO "%s: %s Physical insertion.\n", dev->name, open_err);
1300 break;
1302 case ADDRESS_VERIFICATION:
1303 printk(KERN_INFO "%s: %s Address verification.\n", dev->name, open_err);
1304 break;
1306 case PARTICIPATION_IN_RING_POLL:
1307 printk(KERN_INFO "%s: %s Participation in ring poll.\n", dev->name, open_err);
1308 break;
1310 case REQUEST_INITIALISATION:
1311 printk(KERN_INFO "%s: %s Request initialisation.\n", dev->name, open_err);
1312 break;
1314 case FULLDUPLEX_CHECK:
1315 printk(KERN_INFO "%s: %s Full duplex check.\n", dev->name, open_err);
1316 break;
1318 default:
1319 printk(KERN_INFO "%s: %s Unknown open phase\n", dev->name, open_err);
1320 break;
1323 /* Show the open errors. */
1324 switch(ssb_parm_0 & OPEN_ERROR_CODES_MASK)
1326 case OPEN_FUNCTION_FAILURE:
1327 printk(KERN_INFO "%s: %s OPEN_FUNCTION_FAILURE", dev->name, code_err);
1328 tp->LastOpenStatus =
1329 OPEN_FUNCTION_FAILURE;
1330 break;
1332 case OPEN_SIGNAL_LOSS:
1333 printk(KERN_INFO "%s: %s OPEN_SIGNAL_LOSS\n", dev->name, code_err);
1334 tp->LastOpenStatus =
1335 OPEN_SIGNAL_LOSS;
1336 break;
1338 case OPEN_TIMEOUT:
1339 printk(KERN_INFO "%s: %s OPEN_TIMEOUT\n", dev->name, code_err);
1340 tp->LastOpenStatus =
1341 OPEN_TIMEOUT;
1342 break;
1344 case OPEN_RING_FAILURE:
1345 printk(KERN_INFO "%s: %s OPEN_RING_FAILURE\n", dev->name, code_err);
1346 tp->LastOpenStatus =
1347 OPEN_RING_FAILURE;
1348 break;
1350 case OPEN_RING_BEACONING:
1351 printk(KERN_INFO "%s: %s OPEN_RING_BEACONING\n", dev->name, code_err);
1352 tp->LastOpenStatus =
1353 OPEN_RING_BEACONING;
1354 break;
1356 case OPEN_DUPLICATE_NODEADDR:
1357 printk(KERN_INFO "%s: %s OPEN_DUPLICATE_NODEADDR\n", dev->name, code_err);
1358 tp->LastOpenStatus =
1359 OPEN_DUPLICATE_NODEADDR;
1360 break;
1362 case OPEN_REQUEST_INIT:
1363 printk(KERN_INFO "%s: %s OPEN_REQUEST_INIT\n", dev->name, code_err);
1364 tp->LastOpenStatus =
1365 OPEN_REQUEST_INIT;
1366 break;
1368 case OPEN_REMOVE_RECEIVED:
1369 printk(KERN_INFO "%s: %s OPEN_REMOVE_RECEIVED", dev->name, code_err);
1370 tp->LastOpenStatus =
1371 OPEN_REMOVE_RECEIVED;
1372 break;
1374 case OPEN_FULLDUPLEX_SET:
1375 printk(KERN_INFO "%s: %s OPEN_FULLDUPLEX_SET\n", dev->name, code_err);
1376 tp->LastOpenStatus =
1377 OPEN_FULLDUPLEX_SET;
1378 break;
1380 default:
1381 printk(KERN_INFO "%s: %s Unknown open err code", dev->name, code_err);
1382 tp->LastOpenStatus =
1383 OPEN_FUNCTION_FAILURE;
1384 break;
1388 tp->AdapterOpenFlag = 0;
1389 tp->AdapterVirtOpenFlag = 0;
1391 return;
1394 else
1396 if(ssb_cmd != READ_ERROR_LOG)
1397 return;
1399 /* Add values from the error log table to the MAC
1400 * statistics counters and update the errorlogtable
1401 * memory.
1403 tp->MacStat.line_errors += tp->errorlogtable.Line_Error;
1404 tp->MacStat.burst_errors += tp->errorlogtable.Burst_Error;
1405 tp->MacStat.A_C_errors += tp->errorlogtable.ARI_FCI_Error;
1406 tp->MacStat.lost_frames += tp->errorlogtable.Lost_Frame_Error;
1407 tp->MacStat.recv_congest_count += tp->errorlogtable.Rx_Congest_Error;
1408 tp->MacStat.rx_errors += tp->errorlogtable.Rx_Congest_Error;
1409 tp->MacStat.frame_copied_errors += tp->errorlogtable.Frame_Copied_Error;
1410 tp->MacStat.token_errors += tp->errorlogtable.Token_Error;
1411 tp->MacStat.dummy1 += tp->errorlogtable.DMA_Bus_Error;
1412 tp->MacStat.dummy1 += tp->errorlogtable.DMA_Parity_Error;
1413 tp->MacStat.abort_delimiters += tp->errorlogtable.AbortDelimeters;
1414 tp->MacStat.frequency_errors += tp->errorlogtable.Frequency_Error;
1415 tp->MacStat.internal_errors += tp->errorlogtable.Internal_Error;
1418 return;
1422 * The inverse routine to sktr_open().
1424 static int sktr_close(struct device *dev)
1426 struct net_local *tp = (struct net_local *)dev->priv;
1428 dev->tbusy = 1;
1429 dev->start = 0;
1431 del_timer(&tp->timer);
1433 /* Flush the Tx and disable Rx here. */
1435 tp->HaltInProgress = 1;
1436 sktr_exec_cmd(dev, OC_CLOSE);
1437 tp->timer.expires = jiffies + 1*HZ;
1438 tp->timer.function = sktr_timer_end_wait;
1439 tp->timer.data = (unsigned long)dev;
1440 add_timer(&tp->timer);
1442 sktr_enable_interrupts(dev);
1444 tp->Sleeping = 1;
1445 interruptible_sleep_on(&tp->wait_for_tok_int);
1446 tp->TransmitCommandActive = 0;
1448 del_timer(&tp->timer);
1449 sktr_disable_interrupts(dev);
1451 if(dev->dma > 0)
1453 unsigned long flags=claim_dma_lock();
1454 disable_dma(dev->dma);
1455 release_dma_lock(flags);
1458 outw(0xFF00, dev->base_addr + SIFCMD);
1459 if(dev->dma > 0)
1460 outb(0xff, dev->base_addr + POSREG);
1462 #ifdef MODULE
1463 MOD_DEC_USE_COUNT;
1464 #endif
1466 sktr_cancel_tx_queue(tp);
1468 return (0);
1472 * Get the current statistics. This may be called with the card open
1473 * or closed.
1475 static struct enet_statistics *sktr_get_stats(struct device *dev)
1477 struct net_local *tp = (struct net_local *)dev->priv;
1479 return ((struct enet_statistics *)&tp->MacStat);
1483 * Set or clear the multicast filter for this adapter.
1485 static void sktr_set_multicast_list(struct device *dev)
1487 struct net_local *tp = (struct net_local *)dev->priv;
1488 unsigned int OpenOptions;
1490 OpenOptions = tp->ocpl.OPENOptions &
1491 ~(PASS_ADAPTER_MAC_FRAMES
1492 | PASS_ATTENTION_FRAMES
1493 | PASS_BEACON_MAC_FRAMES
1494 | COPY_ALL_MAC_FRAMES
1495 | COPY_ALL_NON_MAC_FRAMES);
1497 if(dev->flags & IFF_PROMISC)
1498 /* Enable promiscuous mode */
1499 OpenOptions |= COPY_ALL_NON_MAC_FRAMES | COPY_ALL_MAC_FRAMES;
1500 else
1502 if(dev->flags & IFF_ALLMULTI)
1503 /* || dev->mc_count > HW_MAX_ADDRS) */
1505 /* Disable promiscuous mode, use normal mode. */
1507 else
1509 if(dev->mc_count)
1511 /* Walk the address list, and load the filter */
1516 tp->ocpl.OPENOptions = OpenOptions;
1517 sktr_exec_cmd(dev, OC_MODIFY_OPEN_PARMS);
1519 return;
1523 * Wait for some time (microseconds)
1525 static void sktr_wait(unsigned long time)
1527 long tmp;
1529 tmp = time/(1000000/HZ);
1530 do {
1531 current->state = TASK_INTERRUPTIBLE;
1532 tmp = schedule_timeout(tmp);
1533 } while(tmp);
1535 return;
1539 * Write a command value to the SIFCMD register
1541 static void sktr_exec_sifcmd(struct device *dev, unsigned int WriteValue)
1543 int ioaddr = dev->base_addr;
1544 unsigned short cmd;
1545 unsigned short SifStsValue;
1546 unsigned long loop_counter;
1548 WriteValue = ((WriteValue ^ CMD_SYSTEM_IRQ) | CMD_INTERRUPT_ADAPTER);
1549 cmd = (unsigned short)WriteValue;
1550 loop_counter = 0,5 * 800000;
1551 do {
1552 SifStsValue = inw(ioaddr + SIFSTS);
1553 } while((SifStsValue & CMD_INTERRUPT_ADAPTER) && loop_counter--);
1554 outw(cmd, ioaddr + SIFCMD);
1556 return;
1560 * Processes adapter hardware reset, halts adapter and downloads firmware,
1561 * clears the halt bit.
1563 static int sktr_reset_adapter(struct device *dev)
1565 struct net_local *tp = (struct net_local *)dev->priv;
1566 unsigned short *fw_ptr = (unsigned short *)&sktr_code;
1567 unsigned short count, c;
1568 int ioaddr = dev->base_addr;
1570 /* Hardware adapter reset */
1571 outw(ACL_ARESET, ioaddr + SIFACL);
1572 sktr_wait(40);
1574 c = inw(ioaddr + SIFACL);
1575 sktr_wait(20);
1577 if(dev->dma == 0) /* For PCI adapters */
1579 c &= ~(ACL_SPEED4 | ACL_SPEED16); /* Clear bits */
1580 if(tp->DataRate == SPEED_4)
1581 c |= ACL_SPEED4; /* Set 4Mbps */
1582 else
1583 c |= ACL_SPEED16; /* Set 16Mbps */
1586 /* In case a comand is pending - forget it */
1587 tp->ScbInUse = 0;
1589 c &= ~ACL_ARESET; /* Clear adapter reset bit */
1590 c |= ACL_CPHALT; /* Halt adapter CPU, allow download */
1591 c &= ~ACL_PSDMAEN; /* Clear pseudo dma bit */
1592 outw(c, ioaddr + SIFACL);
1593 sktr_wait(40);
1595 /* Download firmware via DIO interface: */
1596 do {
1597 /* Download first address part */
1598 outw(*fw_ptr, ioaddr + SIFADX);
1599 fw_ptr++;
1601 /* Download second address part */
1602 outw(*fw_ptr, ioaddr + SIFADD);
1603 fw_ptr++;
1605 if((count = *fw_ptr) != 0) /* Load loop counter */
1607 fw_ptr++; /* Download block data */
1608 for(; count > 0; count--)
1610 outw(*fw_ptr, ioaddr + SIFINC);
1611 fw_ptr++;
1614 else /* Stop, if last block downloaded */
1616 c = inw(ioaddr + SIFACL);
1617 c &= (~ACL_CPHALT | ACL_SINTEN);
1619 /* Clear CPHALT and start BUD */
1620 outw(c, ioaddr + SIFACL);
1621 return (1);
1623 } while(count == 0);
1625 return (-1);
1629 * Starts bring up diagnostics of token ring adapter and evaluates
1630 * diagnostic results.
1632 static int sktr_bringup_diags(struct device *dev)
1634 int loop_cnt, retry_cnt;
1635 unsigned short Status;
1636 int ioaddr = dev->base_addr;
1638 sktr_wait(HALF_SECOND);
1639 sktr_exec_sifcmd(dev, EXEC_SOFT_RESET);
1640 sktr_wait(HALF_SECOND);
1642 retry_cnt = BUD_MAX_RETRIES; /* maximal number of retrys */
1644 do {
1645 retry_cnt--;
1646 if(sktr_debug > 3)
1647 printk(KERN_INFO "BUD-Status: \n");
1648 loop_cnt = BUD_MAX_LOOPCNT; /* maximum: three seconds*/
1649 do { /* Inspect BUD results */
1650 loop_cnt--;
1651 sktr_wait(HALF_SECOND);
1652 Status = inw(ioaddr + SIFSTS);
1653 Status &= STS_MASK;
1655 if(sktr_debug > 3)
1656 printk(KERN_INFO " %04X \n", Status);
1657 /* BUD successfully completed */
1658 if(Status == STS_INITIALIZE)
1659 return (1);
1660 /* Unrecoverable hardware error, BUD not completed? */
1661 } while((loop_cnt > 0) && ((Status & (STS_ERROR | STS_TEST))
1662 != (STS_ERROR | STS_TEST)));
1664 /* Error preventing completion of BUD */
1665 if(retry_cnt > 0)
1667 printk(KERN_INFO "%s: Adapter Software Reset.\n",
1668 dev->name);
1669 sktr_exec_sifcmd(dev, EXEC_SOFT_RESET);
1670 sktr_wait(HALF_SECOND);
1672 } while(retry_cnt > 0);
1674 Status = inw(ioaddr + SIFSTS);
1675 Status &= STS_ERROR_MASK; /* Hardware error occured! */
1677 printk(KERN_INFO "%s: Bring Up Diagnostics Error (%04X) occurred\n",
1678 dev->name, Status);
1680 return (-1);
1684 * Copy initialisation data to adapter memory, beginning at address
1685 * 1:0A00; Starting DMA test and evaluating result bits.
1687 static int sktr_init_adapter(struct device *dev)
1689 struct net_local *tp = (struct net_local *)dev->priv;
1691 const unsigned char SCB_Test[6] = {0x00, 0x00, 0xC1, 0xE2, 0xD4, 0x8B};
1692 const unsigned char SSB_Test[8] = {0xFF, 0xFF, 0xD1, 0xD7,
1693 0xC5, 0xD9, 0xC3, 0xD4};
1694 void *ptr = (void *)&tp->ipb;
1695 unsigned short *ipb_ptr = (unsigned short *)ptr;
1696 unsigned char *cb_ptr = (unsigned char *) &tp->scb;
1697 unsigned char *sb_ptr = (unsigned char *) &tp->ssb;
1698 unsigned short Status;
1699 int i, loop_cnt, retry_cnt;
1700 int ioaddr = dev->base_addr;
1702 /* Normalize: byte order low/high, word order high/low! (only IPB!) */
1703 tp->ipb.SCB_Addr = SWAPW(virt_to_bus(&tp->scb));
1704 tp->ipb.SSB_Addr = SWAPW(virt_to_bus(&tp->ssb));
1706 /* Maximum: three initialization retries */
1707 retry_cnt = INIT_MAX_RETRIES;
1709 do {
1710 retry_cnt--;
1712 /* Transfer initialization block */
1713 outw(0x0001, ioaddr + SIFADX);
1715 /* To address 0001:0A00 of adapter RAM */
1716 outw(0x0A00, ioaddr + SIFADD);
1718 /* Write 11 words to adapter RAM */
1719 for(i = 0; i < 11; i++)
1720 outw(ipb_ptr[i], ioaddr + SIFINC);
1722 /* Execute SCB adapter command */
1723 sktr_exec_sifcmd(dev, CMD_EXECUTE);
1725 loop_cnt = INIT_MAX_LOOPCNT; /* Maximum: 11 seconds */
1727 /* While remaining retries, no error and not completed */
1728 do {
1729 Status = 0;
1730 loop_cnt--;
1731 sktr_wait(HALF_SECOND);
1733 /* Mask interesting status bits */
1734 Status = inw(ioaddr + SIFSTS);
1735 Status &= STS_MASK;
1736 } while(((Status &(STS_INITIALIZE | STS_ERROR | STS_TEST)) != 0)
1737 && ((Status & STS_ERROR) == 0) && (loop_cnt != 0));
1739 if((Status & (STS_INITIALIZE | STS_ERROR | STS_TEST)) == 0)
1741 /* Initialization completed without error */
1742 i = 0;
1743 do { /* Test if contents of SCB is valid */
1744 if(SCB_Test[i] != *(cb_ptr + i))
1745 /* DMA data error: wrong data in SCB */
1746 return (-1);
1747 i++;
1748 } while(i < 6);
1750 i = 0;
1751 do { /* Test if contents of SSB is valid */
1752 if(SSB_Test[i] != *(sb_ptr + i))
1753 /* DMA data error: wrong data in SSB */
1754 return (-1);
1755 i++;
1756 } while (i < 8);
1758 return (1); /* Adapter successfully initialized */
1760 else
1762 if((Status & STS_ERROR) != 0)
1764 /* Initialization error occured */
1765 Status = inw(ioaddr + SIFSTS);
1766 Status &= STS_ERROR_MASK;
1767 /* ShowInitialisationErrorCode(Status); */
1768 return (-1); /* Unrecoverable error */
1770 else
1772 if(retry_cnt > 0)
1774 /* Reset adapter and try init again */
1775 sktr_exec_sifcmd(dev, EXEC_SOFT_RESET);
1776 sktr_wait(HALF_SECOND);
1780 } while(retry_cnt > 0);
1782 return (-1);
1786 * Check for outstanding commands in command queue and tries to execute
1787 * command immediately. Corresponding command flag in command queue is cleared.
1789 static void sktr_chk_outstanding_cmds(struct device *dev)
1791 struct net_local *tp = (struct net_local *)dev->priv;
1792 unsigned long Addr = 0;
1793 unsigned char i = 0;
1795 if(tp->CMDqueue == 0)
1796 return; /* No command execution */
1798 /* If SCB in use: no command */
1799 if(tp->ScbInUse == 1)
1800 return;
1802 /* Check if adapter is opened, avoiding COMMAND_REJECT
1803 * interrupt by the adapter!
1805 if(tp->AdapterOpenFlag == 0)
1807 if(tp->CMDqueue & OC_OPEN)
1809 /* Execute OPEN command */
1810 tp->CMDqueue ^= OC_OPEN;
1812 /* Copy the 18 bytes of the product ID */
1813 while((AdapterName[i] != '\0') && (i < PROD_ID_SIZE))
1815 tp->ProductID[i] = AdapterName[i];
1816 i++;
1819 Addr = htonl(virt_to_bus(&tp->ocpl));
1820 tp->scb.Parm[0] = LOWORD(Addr);
1821 tp->scb.Parm[1] = HIWORD(Addr);
1822 tp->scb.CMD = OPEN;
1824 else
1825 /* No OPEN command queued, but adapter closed. Note:
1826 * We'll try to re-open the adapter in DriverPoll()
1828 return; /* No adapter command issued */
1830 else
1832 /* Adapter is open; evaluate command queue: try to execute
1833 * outstanding commands (depending on priority!) CLOSE
1834 * command queued
1836 if(tp->CMDqueue & OC_CLOSE)
1838 tp->CMDqueue ^= OC_CLOSE;
1839 tp->AdapterOpenFlag = 0;
1840 tp->scb.Parm[0] = 0; /* Parm[0], Parm[1] are ignored */
1841 tp->scb.Parm[1] = 0; /* but should be set to zero! */
1842 tp->scb.CMD = CLOSE;
1843 if(!tp->HaltInProgress)
1844 tp->CMDqueue |= OC_OPEN; /* re-open adapter */
1845 else
1846 tp->CMDqueue = 0; /* no more commands */
1848 else
1850 if(tp->CMDqueue & OC_RECEIVE)
1852 tp->CMDqueue ^= OC_RECEIVE;
1853 Addr = htonl(virt_to_bus(tp->RplHead));
1854 tp->scb.Parm[0] = LOWORD(Addr);
1855 tp->scb.Parm[1] = HIWORD(Addr);
1856 tp->scb.CMD = RECEIVE;
1858 else
1860 if(tp->CMDqueue & OC_TRANSMIT_HALT)
1862 /* NOTE: TRANSMIT.HALT must be checked
1863 * before TRANSMIT.
1865 tp->CMDqueue ^= OC_TRANSMIT_HALT;
1866 tp->scb.CMD = TRANSMIT_HALT;
1868 /* Parm[0] and Parm[1] are ignored
1869 * but should be set to zero!
1871 tp->scb.Parm[0] = 0;
1872 tp->scb.Parm[1] = 0;
1874 else
1876 if(tp->CMDqueue & OC_TRANSMIT)
1878 /* NOTE: TRANSMIT must be
1879 * checked after TRANSMIT.HALT
1881 if(tp->TransmitCommandActive)
1883 if(!tp->TransmitHaltScheduled)
1885 tp->TransmitHaltScheduled = 1;
1886 sktr_exec_cmd(dev, OC_TRANSMIT_HALT) ;
1888 tp->TransmitCommandActive = 0;
1889 return;
1892 tp->CMDqueue ^= OC_TRANSMIT;
1893 sktr_cancel_tx_queue(tp);
1894 Addr = htonl(virt_to_bus(tp->TplBusy));
1895 tp->scb.Parm[0] = LOWORD(Addr);
1896 tp->scb.Parm[1] = HIWORD(Addr);
1897 tp->scb.CMD = TRANSMIT;
1898 tp->TransmitCommandActive = 1;
1900 else
1902 if(tp->CMDqueue & OC_MODIFY_OPEN_PARMS)
1904 tp->CMDqueue ^= OC_MODIFY_OPEN_PARMS;
1905 tp->scb.Parm[0] = tp->ocpl.OPENOptions; /* new OPEN options*/
1906 tp->scb.Parm[0] |= ENABLE_FULL_DUPLEX_SELECTION;
1907 tp->scb.Parm[1] = 0; /* is ignored but should be zero */
1908 tp->scb.CMD = MODIFY_OPEN_PARMS;
1910 else
1912 if(tp->CMDqueue & OC_SET_FUNCT_ADDR)
1914 tp->CMDqueue ^= OC_SET_FUNCT_ADDR;
1915 tp->scb.Parm[0] = LOWORD(tp->ocpl.FunctAddr);
1916 tp->scb.Parm[1] = HIWORD(tp->ocpl.FunctAddr);
1917 tp->scb.CMD = SET_FUNCT_ADDR;
1919 else
1921 if(tp->CMDqueue & OC_SET_GROUP_ADDR)
1923 tp->CMDqueue ^= OC_SET_GROUP_ADDR;
1924 tp->scb.Parm[0] = LOWORD(tp->ocpl.GroupAddr);
1925 tp->scb.Parm[1] = HIWORD(tp->ocpl.GroupAddr);
1926 tp->scb.CMD = SET_GROUP_ADDR;
1928 else
1930 if(tp->CMDqueue & OC_READ_ERROR_LOG)
1932 tp->CMDqueue ^= OC_READ_ERROR_LOG;
1933 Addr = htonl(virt_to_bus(&tp->errorlogtable));
1934 tp->scb.Parm[0] = LOWORD(Addr);
1935 tp->scb.Parm[1] = HIWORD(Addr);
1936 tp->scb.CMD = READ_ERROR_LOG;
1938 else
1940 printk(KERN_WARNING "CheckForOutstandingCommand: unknown Command\n");
1941 tp->CMDqueue = 0;
1942 return;
1953 tp->ScbInUse = 1; /* Set semaphore: SCB in use. */
1955 /* Execute SCB and generate IRQ when done. */
1956 sktr_exec_sifcmd(dev, CMD_EXECUTE | CMD_SCB_REQUEST);
1958 return;
1962 * IRQ conditions: signal loss on the ring, transmit or receive of beacon
1963 * frames (disabled if bit 1 of OPEN option is set); report error MAC
1964 * frame transmit (disabled if bit 2 of OPEN option is set); open or short
1965 * cirquit fault on the lobe is detected; remove MAC frame received;
1966 * error counter overflow (255); opened adapter is the only station in ring.
1967 * After some of the IRQs the adapter is closed!
1969 static void sktr_ring_status_irq(struct device *dev)
1971 struct net_local *tp = (struct net_local *)dev->priv;
1973 tp->CurrentRingStatus = SWAPB(tp->ssb.Parm[0]);
1975 /* First: fill up statistics */
1976 if(tp->ssb.Parm[0] & SIGNAL_LOSS)
1978 printk(KERN_INFO "%s: Signal Loss\n", dev->name);
1979 tp->MacStat.line_errors++;
1982 /* Adapter is closed, but initialized */
1983 if(tp->ssb.Parm[0] & LOBE_WIRE_FAULT)
1985 printk(KERN_INFO "%s: Lobe Wire Fault, Reopen Adapter\n",
1986 dev->name);
1987 tp->MacStat.line_errors++;
1990 if(tp->ssb.Parm[0] & RING_RECOVERY)
1991 printk(KERN_INFO "%s: Ring Recovery\n", dev->name);
1993 /* Counter overflow: read error log */
1994 if(tp->ssb.Parm[0] & COUNTER_OVERFLOW)
1996 printk(KERN_INFO "%s: Counter Overflow\n", dev->name);
1997 sktr_exec_cmd(dev, OC_READ_ERROR_LOG);
2000 /* Adapter is closed, but initialized */
2001 if(tp->ssb.Parm[0] & REMOVE_RECEIVED)
2002 printk(KERN_INFO "%s: Remove Received, Reopen Adapter\n",
2003 dev->name);
2005 /* Adapter is closed, but initialized */
2006 if(tp->ssb.Parm[0] & AUTO_REMOVAL_ERROR)
2007 printk(KERN_INFO "%s: Auto Removal Error, Reopen Adapter\n",
2008 dev->name);
2010 if(tp->ssb.Parm[0] & HARD_ERROR)
2011 printk(KERN_INFO "%s: Hard Error\n", dev->name);
2013 if(tp->ssb.Parm[0] & SOFT_ERROR)
2014 printk(KERN_INFO "%s: Soft Error\n", dev->name);
2016 if(tp->ssb.Parm[0] & TRANSMIT_BEACON)
2017 printk(KERN_INFO "%s: Transmit Beacon\n", dev->name);
2019 if(tp->ssb.Parm[0] & SINGLE_STATION)
2020 printk(KERN_INFO "%s: Single Station\n", dev->name);
2022 /* Check if adapter has been closed */
2023 if(tp->ssb.Parm[0] & ADAPTER_CLOSED)
2025 printk(KERN_INFO "%s: Adapter closed (Reopening),"
2026 "QueueSkb %d, CurrentRingStat %x\n",
2027 dev->name, tp->QueueSkb, tp->CurrentRingStatus);
2028 tp->AdapterOpenFlag = 0;
2029 sktr_open_adapter(dev);
2032 return;
2036 * Issued if adapter has encountered an unrecoverable hardware
2037 * or software error.
2039 static void sktr_chk_irq(struct device *dev)
2041 int i;
2042 unsigned short AdapterCheckBlock[4];
2043 unsigned short ioaddr = dev->base_addr;
2044 struct net_local *tp = (struct net_local *)dev->priv;
2046 tp->AdapterOpenFlag = 0; /* Adapter closed now */
2048 /* Page number of adapter memory */
2049 outw(0x0001, ioaddr + SIFADX);
2050 /* Address offset */
2051 outw(CHECKADDR, ioaddr + SIFADR);
2053 /* Reading 8 byte adapter check block. */
2054 for(i = 0; i < 4; i++)
2055 AdapterCheckBlock[i] = inw(ioaddr + SIFINC);
2057 if(sktr_debug > 3)
2059 printk("%s: AdapterCheckBlock: ", dev->name);
2060 for (i = 0; i < 4; i++)
2061 printk("%04X", AdapterCheckBlock[i]);
2062 printk("\n");
2065 switch(AdapterCheckBlock[0])
2067 case DIO_PARITY:
2068 printk(KERN_INFO "%s: DIO parity error\n", dev->name);
2069 break;
2071 case DMA_READ_ABORT:
2072 printk(KERN_INFO "%s DMA read operation aborted:\n",
2073 dev->name);
2074 switch (AdapterCheckBlock[1])
2076 case 0:
2077 printk(KERN_INFO "Timeout\n");
2078 printk(KERN_INFO "Address: %04X %04X\n",
2079 AdapterCheckBlock[2],
2080 AdapterCheckBlock[3]);
2081 break;
2083 case 1:
2084 printk(KERN_INFO "Parity error\n");
2085 printk(KERN_INFO "Address: %04X %04X\n",
2086 AdapterCheckBlock[2],
2087 AdapterCheckBlock[3]);
2088 break;
2090 case 2:
2091 printk(KERN_INFO "Bus error\n");
2092 printk(KERN_INFO "Address: %04X %04X\n",
2093 AdapterCheckBlock[2],
2094 AdapterCheckBlock[3]);
2095 break;
2097 default:
2098 printk(KERN_INFO "Unknown error.\n");
2099 break;
2101 break;
2103 case DMA_WRITE_ABORT:
2104 printk(KERN_INFO "%s: DMA write operation aborted: \n",
2105 dev->name);
2106 switch (AdapterCheckBlock[1])
2108 case 0:
2109 printk(KERN_INFO "Timeout\n");
2110 printk(KERN_INFO "Address: %04X %04X\n",
2111 AdapterCheckBlock[2],
2112 AdapterCheckBlock[3]);
2113 break;
2115 case 1:
2116 printk(KERN_INFO "Parity error\n");
2117 printk(KERN_INFO "Address: %04X %04X\n",
2118 AdapterCheckBlock[2],
2119 AdapterCheckBlock[3]);
2120 break;
2122 case 2:
2123 printk(KERN_INFO "Bus error\n");
2124 printk(KERN_INFO "Address: %04X %04X\n",
2125 AdapterCheckBlock[2],
2126 AdapterCheckBlock[3]);
2127 break;
2129 default:
2130 printk(KERN_INFO "Unknown error.\n");
2131 break;
2133 break;
2135 case ILLEGAL_OP_CODE:
2136 printk("%s: Illegal operation code in firmware\n",
2137 dev->name);
2138 /* Parm[0-3]: adapter internal register R13-R15 */
2139 break;
2141 case PARITY_ERRORS:
2142 printk("%s: Adapter internal bus parity error\n",
2143 dev->name);
2144 /* Parm[0-3]: adapter internal register R13-R15 */
2145 break;
2147 case RAM_DATA_ERROR:
2148 printk("%s: RAM data error\n", dev->name);
2149 /* Parm[0-1]: MSW/LSW address of RAM location. */
2150 break;
2152 case RAM_PARITY_ERROR:
2153 printk("%s: RAM parity error\n", dev->name);
2154 /* Parm[0-1]: MSW/LSW address of RAM location. */
2155 break;
2157 case RING_UNDERRUN:
2158 printk("%s: Internal DMA underrun detected\n",
2159 dev->name);
2160 break;
2162 case INVALID_IRQ:
2163 printk("%s: Unrecognized interrupt detected\n",
2164 dev->name);
2165 /* Parm[0-3]: adapter internal register R13-R15 */
2166 break;
2168 case INVALID_ERROR_IRQ:
2169 printk("%s: Unrecognized error interrupt detected\n",
2170 dev->name);
2171 /* Parm[0-3]: adapter internal register R13-R15 */
2172 break;
2174 case INVALID_XOP:
2175 printk("%s: Unrecognized XOP request detected\n",
2176 dev->name);
2177 /* Parm[0-3]: adapter internal register R13-R15 */
2178 break;
2180 default:
2181 printk("%s: Unknown status", dev->name);
2182 break;
2185 if(sktr_chipset_init(dev) == 1)
2187 /* Restart of firmware successful */
2188 tp->AdapterOpenFlag = 1;
2191 return;
2195 * Internal adapter pointer to RAM data are copied from adapter into
2196 * host system.
2198 static void sktr_read_ptr(struct device *dev)
2200 struct net_local *tp = (struct net_local *)dev->priv;
2201 unsigned short adapterram;
2203 sktr_read_ram(dev, (unsigned char *)&tp->intptrs.BurnedInAddrPtr,
2204 ADAPTER_INT_PTRS, 16);
2205 sktr_read_ram(dev, (unsigned char *)&adapterram,
2206 (unsigned short)SWAPB(tp->intptrs.AdapterRAMPtr), 2);
2208 printk(KERN_INFO "%s: Adapter RAM size: %d K\n",
2209 dev->name, SWAPB(adapterram));
2211 return;
2215 * Reads a number of bytes from adapter to system memory.
2217 static void sktr_read_ram(struct device *dev, unsigned char *Data,
2218 unsigned short Address, int Length)
2220 int i;
2221 unsigned short old_sifadx, old_sifadr, InWord;
2222 unsigned short ioaddr = dev->base_addr;
2224 /* Save the current values */
2225 old_sifadx = inw(ioaddr + SIFADX);
2226 old_sifadr = inw(ioaddr + SIFADR);
2228 /* Page number of adapter memory */
2229 outw(0x0001, ioaddr + SIFADX);
2230 /* Address offset in adapter RAM */
2231 outw(Address, ioaddr + SIFADR);
2233 /* Copy len byte from adapter memory to system data area. */
2234 i = 0;
2235 for(;;)
2237 InWord = inw(ioaddr + SIFINC);
2239 *(Data + i) = HIBYTE(InWord); /* Write first byte */
2240 if(++i == Length) /* All is done break */
2241 break;
2243 *(Data + i) = LOBYTE(InWord); /* Write second byte */
2244 if (++i == Length) /* All is done break */
2245 break;
2248 /* Restore original values */
2249 outw(old_sifadx, ioaddr + SIFADX);
2250 outw(old_sifadr, ioaddr + SIFADR);
2252 return;
2256 * Reads MAC address from adapter ROM.
2258 static void sktr_read_addr(struct device *dev, unsigned char *Address)
2260 int i, In;
2261 unsigned short ioaddr = dev->base_addr;
2263 /* Address: 0000:0000 */
2264 outw(0, ioaddr + SIFADX);
2265 outw(0, ioaddr + SIFADR);
2267 /* Read six byte MAC address data */
2268 for(i = 0; i < 6; i++)
2270 In = inw(ioaddr + SIFINC);
2271 *(Address + i) = (unsigned char)(In >> 8);
2274 return;
2278 * Cancel all queued packets in the transmission queue.
2280 static void sktr_cancel_tx_queue(struct net_local* tp)
2282 TPL *tpl;
2283 struct sk_buff *skb;
2286 * NOTE: There must not be an active TRANSMIT command pending, when
2287 * this function is called.
2289 if(tp->TransmitCommandActive)
2290 return;
2292 for(;;)
2294 tpl = tp->TplBusy;
2295 if(!tpl->BusyFlag)
2296 break;
2297 /* "Remove" TPL from busy list. */
2298 tp->TplBusy = tpl->NextTPLPtr;
2299 sktr_write_tpl_status(tpl, 0); /* Clear VALID bit */
2300 tpl->BusyFlag = 0; /* "free" TPL */
2302 printk(KERN_INFO "Cancel tx (%08lXh).\n", (unsigned long)tpl);
2304 dev_kfree_skb(tpl->Skb);
2307 for(;;)
2309 skb = skb_dequeue(&tp->SendSkbQueue);
2310 if(skb == NULL)
2311 break;
2312 tp->QueueSkb++;
2313 dev_kfree_skb(skb);
2316 return;
2320 * This function is called whenever a transmit interrupt is generated by the
2321 * adapter. For a command complete interrupt, it is checked if we have to
2322 * issue a new transmit command or not.
2324 static void sktr_tx_status_irq(struct device *dev)
2326 struct net_local *tp = (struct net_local *)dev->priv;
2327 unsigned char HighByte, HighAc, LowAc;
2328 TPL *tpl;
2330 /* NOTE: At this point the SSB from TRANSMIT STATUS is no longer
2331 * available, because the CLEAR SSB command has already been issued.
2333 * Process all complete transmissions.
2336 for(;;)
2338 tpl = tp->TplBusy;
2339 if(!tpl->BusyFlag || (tpl->Status
2340 & (TX_VALID | TX_FRAME_COMPLETE))
2341 != TX_FRAME_COMPLETE)
2343 break;
2346 /* "Remove" TPL from busy list. */
2347 tp->TplBusy = tpl->NextTPLPtr ;
2349 if(sktr_debug > 3)
2350 sktr_dump(tpl->MData, SWAPB(tpl->FrameSize));
2352 /* Check the transmit status field only for directed frames*/
2353 if(DIRECTED_FRAME(tpl) && (tpl->Status & TX_ERROR) == 0)
2355 HighByte = GET_TRANSMIT_STATUS_HIGH_BYTE(tpl->Status);
2356 HighAc = GET_FRAME_STATUS_HIGH_AC(HighByte);
2357 LowAc = GET_FRAME_STATUS_LOW_AC(HighByte);
2359 if((HighAc != LowAc) || (HighAc == AC_NOT_RECOGNIZED))
2361 printk(KERN_INFO "%s: (DA=%08lX not recognized)",
2362 dev->name,
2363 *(unsigned long *)&tpl->MData[2+2]);
2365 else
2367 if(sktr_debug > 3)
2368 printk("%s: Directed frame tx'd\n",
2369 dev->name);
2372 else
2374 if(!DIRECTED_FRAME(tpl))
2376 if(sktr_debug > 3)
2377 printk("%s: Broadcast frame tx'd\n",
2378 dev->name);
2382 tp->MacStat.tx_packets++;
2383 dev_kfree_skb(tpl->Skb);
2384 tpl->BusyFlag = 0; /* "free" TPL */
2387 dev->tbusy = 0;
2388 if(tp->QueueSkb < MAX_TX_QUEUE)
2389 sktr_hardware_send_packet(dev, tp);
2391 return;
2395 * Called if a frame receive interrupt is generated by the adapter.
2396 * Check if the frame is valid and indicate it to system.
2398 static void sktr_rcv_status_irq(struct device *dev)
2400 struct net_local *tp = (struct net_local *)dev->priv;
2401 unsigned char *ReceiveDataPtr;
2402 struct sk_buff *skb;
2403 unsigned int Length, Length2;
2404 RPL *rpl;
2405 RPL *SaveHead;
2407 /* NOTE: At this point the SSB from RECEIVE STATUS is no longer
2408 * available, because the CLEAR SSB command has already been issued.
2410 * Process all complete receives.
2413 for(;;)
2415 rpl = tp->RplHead;
2416 if(rpl->Status & RX_VALID)
2417 break; /* RPL still in use by adapter */
2419 /* Forward RPLHead pointer to next list. */
2420 SaveHead = tp->RplHead;
2421 tp->RplHead = rpl->NextRPLPtr;
2423 /* Get the frame size (Byte swap for Intel).
2424 * Do this early (see workaround comment below)
2426 Length = (unsigned short)SWAPB(rpl->FrameSize);
2428 /* Check if the Frame_Start, Frame_End and
2429 * Frame_Complete bits are set.
2431 if((rpl->Status & VALID_SINGLE_BUFFER_FRAME)
2432 == VALID_SINGLE_BUFFER_FRAME)
2434 ReceiveDataPtr = rpl->MData;
2436 /* Workaround for delayed write of FrameSize on ISA
2437 * (FrameSize is false but valid-bit is reset)
2438 * Frame size is set to zero when the RPL is freed.
2439 * Length2 is there because there have also been
2440 * cases where the FrameSize was partially written
2442 Length2 = (unsigned short)SWAPB(rpl->FrameSize);
2444 if(Length == 0 || Length != Length2)
2446 tp->RplHead = SaveHead;
2447 break; /* Return to sktr_interrupt */
2450 /* Drop frames sent by myself */
2451 if(sktr_chk_frame(dev, rpl->MData))
2453 printk(KERN_INFO "%s: Received my own frame\n",
2454 dev->name);
2455 if(rpl->Skb != NULL)
2456 dev_kfree_skb(rpl->Skb);
2458 else
2460 sktr_update_rcv_stats(tp,ReceiveDataPtr,Length);
2462 if(sktr_debug > 3)
2463 printk("%s: Packet Length %04X (%d)\n",
2464 dev->name, Length, Length);
2466 /* Indicate the received frame to system the
2467 * adapter does the Source-Routing padding for
2468 * us. See: OpenOptions in sktr_init_opb()
2470 skb = rpl->Skb;
2471 if(rpl->SkbStat == SKB_UNAVAILABLE)
2473 /* Try again to allocate skb */
2474 skb = dev_alloc_skb(tp->MaxPacketSize);
2475 if(skb == NULL)
2477 /* Update Stats ?? */
2479 else
2481 skb->dev = dev;
2482 skb_put(skb, tp->MaxPacketSize);
2483 rpl->SkbStat = SKB_DATA_COPY;
2484 ReceiveDataPtr = rpl->MData;
2488 if(rpl->SkbStat == SKB_DATA_COPY
2489 || rpl->SkbStat == SKB_DMA_DIRECT)
2491 if(rpl->SkbStat == SKB_DATA_COPY)
2493 memmove(skb->data, ReceiveDataPtr, Length);
2496 /* Deliver frame to system */
2497 rpl->Skb = NULL;
2498 skb_trim(skb,Length);
2499 skb->protocol = tr_type_trans(skb,dev);
2500 netif_rx(skb);
2504 else /* Invalid frame */
2506 if(rpl->Skb != NULL)
2507 dev_kfree_skb(rpl->Skb);
2509 /* Skip list. */
2510 if(rpl->Status & RX_START_FRAME)
2511 /* Frame start bit is set -> overflow. */
2512 tp->MacStat.rx_errors++;
2515 /* Allocate new skb for rpl */
2516 rpl->Skb = dev_alloc_skb(tp->MaxPacketSize);
2518 /* skb == NULL ? then use local buffer */
2519 if(rpl->Skb == NULL)
2521 rpl->SkbStat = SKB_UNAVAILABLE;
2522 rpl->FragList[0].DataAddr = htonl(virt_to_bus(tp->LocalRxBuffers[rpl->RPLIndex]));
2523 rpl->MData = tp->LocalRxBuffers[rpl->RPLIndex];
2525 else /* skb != NULL */
2527 rpl->Skb->dev = dev;
2528 skb_put(rpl->Skb, tp->MaxPacketSize);
2530 /* Data unreachable for DMA ? then use local buffer */
2531 if(virt_to_bus(rpl->Skb->data) + tp->MaxPacketSize
2532 > ISA_MAX_ADDRESS)
2534 rpl->SkbStat = SKB_DATA_COPY;
2535 rpl->FragList[0].DataAddr = htonl(virt_to_bus(tp->LocalRxBuffers[rpl->RPLIndex]));
2536 rpl->MData = tp->LocalRxBuffers[rpl->RPLIndex];
2538 else
2540 /* DMA directly in skb->data */
2541 rpl->SkbStat = SKB_DMA_DIRECT;
2542 rpl->FragList[0].DataAddr = htonl(virt_to_bus(rpl->Skb->data));
2543 rpl->MData = rpl->Skb->data;
2547 rpl->FragList[0].DataCount = SWAPB(tp->MaxPacketSize);
2548 rpl->FrameSize = 0;
2550 /* Pass the last RPL back to the adapter */
2551 tp->RplTail->FrameSize = 0;
2553 /* Reset the CSTAT field in the list. */
2554 sktr_write_rpl_status(tp->RplTail, RX_VALID | RX_FRAME_IRQ);
2556 /* Current RPL becomes last one in list. */
2557 tp->RplTail = tp->RplTail->NextRPLPtr;
2559 /* Inform adapter about RPL valid. */
2560 sktr_exec_sifcmd(dev, CMD_RX_VALID);
2563 return;
2567 * This function should be used whenever the status of any RPL must be
2568 * modified by the driver, because the compiler may otherwise change the
2569 * order of instructions such that writing the RPL status may be executed
2570 * at an undesireable time. When this function is used, the status is
2571 * always written when the function is called.
2573 static void sktr_write_rpl_status(RPL *rpl, unsigned int Status)
2575 rpl->Status = Status;
2577 return;
2581 * The function updates the statistic counters in mac->MacStat.
2582 * It differtiates between directed and broadcast/multicast ( ==functional)
2583 * frames.
2585 static void sktr_update_rcv_stats(struct net_local *tp, unsigned char DataPtr[],
2586 unsigned int Length)
2588 tp->MacStat.rx_packets++;
2590 /* Test functional bit */
2591 if(DataPtr[2] & GROUP_BIT)
2592 tp->MacStat.multicast++;
2594 return;
2598 * Check if it is a frame of myself. Compare source address with my current
2599 * address in reverse direction, and mask out the TR_RII.
2601 static unsigned char sktr_chk_frame(struct device *dev, unsigned char *Addr)
2603 int i;
2605 for(i = 5; i > 0; i--)
2607 if(Addr[8 + i] != dev->dev_addr[i])
2608 return (0);
2611 /* Mask out RIF bit. */
2612 if((Addr[8] & ~TR_RII) != (unsigned char)(dev->dev_addr[0]))
2613 return (0);
2615 return (1); /* It is my frame. */
2619 * Dump Packet (data)
2621 static void sktr_dump(unsigned char *Data, int length)
2623 int i, j;
2625 for (i = 0, j = 0; i < length / 8; i++, j += 8)
2627 printk(KERN_DEBUG "%02x %02x %02x %02x %02x %02x %02x %02x\n",
2628 Data[j+0],Data[j+1],Data[j+2],Data[j+3],
2629 Data[j+4],Data[j+5],Data[j+6],Data[j+7]);
2632 return;
2635 #ifdef MODULE
2637 static struct device* dev_sktr[SKTR_MAX_ADAPTERS];
2638 static int io[SKTR_MAX_ADAPTERS] = { 0, 0 };
2639 static int irq[SKTR_MAX_ADAPTERS] = { 0, 0 };
2640 static int mem[SKTR_MAX_ADAPTERS] = { 0, 0 };
2642 MODULE_PARM(io, "1-" __MODULE_STRING(SKTR_MAX_ADAPTERS) "i");
2643 MODULE_PARM(irq, "1-" __MODULE_STRING(SKTR_MAX_ADAPTERS) "i");
2644 MODULE_PARM(mem, "1-" __MODULE_STRING(SKTR_MAX_ADAPTERS) "i");
2646 int init_module(void)
2648 int i;
2650 for(i = 0; i < SKTR_MAX_ADAPTERS; i++)
2652 irq[i] = 0;
2653 mem[i] = 0;
2654 dev_sktr[i] = NULL;
2655 dev_sktr[i] = init_trdev(dev_sktr[i], 0);
2656 if(dev_sktr[i] == NULL)
2657 return (-ENOMEM);
2659 dev_sktr[i]->base_addr = io[i];
2660 dev_sktr[i]->irq = irq[i];
2661 dev_sktr[i]->mem_start = mem[i];
2662 dev_sktr[i]->init = &sktr_probe;
2664 if(register_trdev(dev_sktr[i]) != 0)
2666 kfree_s(dev_sktr[i], sizeof(struct device));
2667 dev_sktr[i] = NULL;
2668 if(i == 0)
2670 printk("sktr: register_trdev() returned non-zero.\n");
2671 return (-EIO);
2673 else
2674 return (0);
2678 return (0);
2681 void cleanup_module(void)
2683 int i;
2685 for(i = 0; i < SKTR_MAX_ADAPTERS; i++)
2687 if(dev_sktr[i])
2689 unregister_trdev(dev_sktr[i]);
2690 release_region(dev_sktr[i]->base_addr, SKTR_IO_EXTENT);
2691 if(dev_sktr[i]->irq)
2692 free_irq(dev_sktr[i]->irq, dev_sktr[i]);
2693 if(dev_sktr[i]->dma > 0)
2694 free_dma(dev_sktr[i]->dma);
2695 if(dev_sktr[i]->priv)
2696 kfree_s(dev_sktr[i]->priv, sizeof(struct net_local));
2697 kfree_s(dev_sktr[i], sizeof(struct device));
2698 dev_sktr[i] = NULL;
2702 #endif /* MODULE */