* add p cc
[mascara-docs.git] / i386 / linux / linux-2.3.21 / drivers / net / tokenring / sktr.c
blob81b2df4bc00c328a8a67af74712ab275ad6fb784
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
28 * AF Adam Fritzler mid@auk.cx
30 * Modification History:
31 * 29-Aug-97 CG Created
32 * 04-Apr-98 CG Fixed problems caused by tok_timer_check
33 * 10-Apr-98 CG Fixed lockups at cable disconnection
34 * 27-May-98 JS Formated to Linux Kernel Format
35 * 31-May-98 JS Hacked in PCI support
36 * 16-Jun-98 JS Modulized for multiple cards with one driver
37 * 21-Sep-99 CG Fixed source routing issues for 2.2 kernels
38 * 21-Sep-99 AF Added multicast changes recommended by
39 * Jochen Friedrich <jochen@nwe.de> (untested)
40 * Added detection of compatible Compaq PCI card
42 * To do:
43 * 1. Selectable 16 Mbps or 4Mbps
44 * 2. Multi/Broadcast packet handling (might be done)
48 static const char *version = "sktr.c: v1.01 08/29/97 by Christoph Goos\n";
50 #ifdef MODULE
51 #include <linux/module.h>
52 #include <linux/version.h>
53 #endif
55 #include <linux/kernel.h>
56 #include <linux/sched.h>
57 #include <linux/types.h>
58 #include <linux/fcntl.h>
59 #include <linux/interrupt.h>
60 #include <linux/ptrace.h>
61 #include <linux/ioport.h>
62 #include <linux/in.h>
63 #include <linux/malloc.h>
64 #include <linux/string.h>
65 #include <linux/time.h>
66 #include <asm/system.h>
67 #include <asm/bitops.h>
68 #include <asm/io.h>
69 #include <asm/dma.h>
70 #include <asm/irq.h>
71 #include <linux/errno.h>
72 #include <linux/init.h>
73 #include <linux/pci.h>
75 #include <linux/netdevice.h>
76 #include <linux/etherdevice.h>
77 #include <linux/skbuff.h>
78 #include <linux/trdevice.h>
80 #include "sktr.h" /* Our Stuff */
81 #include "sktr_firmware.h" /* SysKonnect adapter firmware */
83 /* A zero-terminated list of I/O addresses to be probed. */
84 static unsigned int sktr_portlist[] __initdata = {
85 0x0A20, 0x1A20, 0x0B20, 0x1B20, 0x0980, 0x1980, 0x0900, 0x1900,
89 /* A zero-terminated list of IRQs to be probed.
90 * Used again after initial probe for sktr_chipset_init, called from sktr_open.
92 static unsigned short sktr_irqlist[] = {
93 3, 5, 9, 10, 11, 12, 15,
97 /* A zero-terminated list of DMAs to be probed. */
98 static int sktr_dmalist[] __initdata = {
99 5, 6, 7,
103 /* Card names */
104 static char *pci_cardname = "SK NET TR 4/16 PCI\0";
105 static char *isa_cardname = "SK NET TR 4/16 ISA\0";
106 static char *AdapterName;
108 /* Use 0 for production, 1 for verification, 2 for debug, and
109 * 3 for very verbose debug.
111 #ifndef SKTR_DEBUG
112 #define SKTR_DEBUG 1
113 #endif
114 static unsigned int sktr_debug = SKTR_DEBUG;
116 /* The number of low I/O ports used by the tokencard. */
117 #define SKTR_IO_EXTENT 32
119 /* Index to functions, as function prototypes.
120 * Alphabetical by function name.
123 /* "B" */
124 static int sktr_bringup_diags(struct net_device *dev);
125 /* "C" */
126 static void sktr_cancel_tx_queue(struct net_local* tp);
127 static int sktr_chipset_init(struct net_device *dev);
128 static void sktr_chk_irq(struct net_device *dev);
129 static unsigned char sktr_chk_frame(struct net_device *dev, unsigned char *Addr);
130 static void sktr_chk_outstanding_cmds(struct net_device *dev);
131 static void sktr_chk_src_addr(unsigned char *frame, unsigned char *hw_addr);
132 static unsigned char sktr_chk_ssb(struct net_local *tp, unsigned short IrqType);
133 static int sktr_close(struct net_device *dev);
134 static void sktr_cmd_status_irq(struct net_device *dev);
135 /* "D" */
136 static void sktr_disable_interrupts(struct net_device *dev);
137 static void sktr_dump(unsigned char *Data, int length);
138 /* "E" */
139 static void sktr_enable_interrupts(struct net_device *dev);
140 static void sktr_exec_cmd(struct net_device *dev, unsigned short Command);
141 static void sktr_exec_sifcmd(struct net_device *dev, unsigned int WriteValue);
142 /* "F" */
143 /* "G" */
144 static struct enet_statistics *sktr_get_stats(struct net_device *dev);
145 /* "H" */
146 static void sktr_hardware_send_packet(struct net_device *dev,
147 struct net_local* tp);
148 /* "I" */
149 static int sktr_init_adapter(struct net_device *dev);
150 static int sktr_init_card(struct net_device *dev);
151 static void sktr_init_ipb(struct net_local *tp);
152 static void sktr_init_net_local(struct net_device *dev);
153 static void sktr_init_opb(struct net_local *tp);
154 static void sktr_interrupt(int irq, void *dev_id, struct pt_regs *regs);
155 static int sktr_isa_chk_card(struct net_device *dev, int ioaddr);
156 static int sktr_isa_chk_ioaddr(int ioaddr);
157 /* "O" */
158 static int sktr_open(struct net_device *dev);
159 static void sktr_open_adapter(struct net_device *dev);
160 /* "P" */
161 static int sktr_pci_chk_card(struct net_device *dev);
162 int sktr_probe(struct net_device *dev);
163 static int sktr_probe1(struct net_device *dev, int ioaddr);
164 /* "R" */
165 static void sktr_rcv_status_irq(struct net_device *dev);
166 static void sktr_read_addr(struct net_device *dev, unsigned char *Address);
167 static void sktr_read_ptr(struct net_device *dev);
168 static void sktr_read_ram(struct net_device *dev, unsigned char *Data,
169 unsigned short Address, int Length);
170 static int sktr_reset_adapter(struct net_device *dev);
171 static void sktr_reset_interrupt(struct net_device *dev);
172 static void sktr_ring_status_irq(struct net_device *dev);
173 /* "S" */
174 static int sktr_send_packet(struct sk_buff *skb, struct net_device *dev);
175 static void sktr_set_multicast_list(struct net_device *dev);
176 /* "T" */
177 static void sktr_timer_chk(unsigned long data);
178 static void sktr_timer_end_wait(unsigned long data);
179 static void sktr_tx_status_irq(struct net_device *dev);
180 /* "U" */
181 static void sktr_update_rcv_stats(struct net_local *tp,
182 unsigned char DataPtr[], unsigned int Length);
183 /* "W" */
184 static void sktr_wait(unsigned long time);
185 static void sktr_write_rpl_status(RPL *rpl, unsigned int Status);
186 static void sktr_write_tpl_status(TPL *tpl, unsigned int Status);
189 * Check for a network adapter of this type, and return '0' if one exists.
190 * If dev->base_addr == 0, probe all likely locations.
191 * If dev->base_addr == 1, always return failure.
193 int __init sktr_probe(struct net_device *dev)
195 int i;
196 int base_addr = dev ? dev->base_addr : 0;
198 if(base_addr > 0x1ff) /* Check a single specified location. */
199 return (sktr_probe1(dev, base_addr));
200 else if(base_addr != 0) /* Don't probe at all. */
201 return (-ENXIO);
203 for(i = 0; sktr_portlist[i]; i++)
205 int ioaddr = sktr_portlist[i];
206 if(check_region(ioaddr, SKTR_IO_EXTENT))
207 continue;
208 if(sktr_probe1(dev, ioaddr))
210 #ifndef MODULE
211 tr_freedev(dev);
212 #endif
214 else
215 return (0);
218 return (-ENODEV);
222 * Detect and setup the PCI SysKonnect TR cards in slot order.
224 static int __init sktr_pci_chk_card(struct net_device *dev)
226 static int pci_index = 0;
227 unsigned char pci_bus, pci_device_fn;
229 if(!pci_present())
230 return (-1); /* No PCI present. */
232 for(; pci_index < 0xff; pci_index++)
234 unsigned int pci_irq_line;
235 struct pci_dev *pdev;
236 unsigned short pci_command, new_command, vendor, device;
237 unsigned int pci_ioaddr;
239 if(pcibios_find_class(PCI_CLASS_NETWORK_TOKEN_RING << 8,
240 pci_index, &pci_bus, &pci_device_fn)
241 != PCIBIOS_SUCCESSFUL)
243 break;
246 pcibios_read_config_word(pci_bus, pci_device_fn,
247 PCI_VENDOR_ID, &vendor);
248 pcibios_read_config_word(pci_bus, pci_device_fn,
249 PCI_DEVICE_ID, &device);
251 pdev = pci_find_slot(pci_bus, pci_device_fn);
252 pci_irq_line = pdev->irq;
253 pci_ioaddr = pdev->resource[0].start;
255 pcibios_read_config_word(pci_bus, pci_device_fn,
256 PCI_COMMAND, &pci_command);
258 /* Remove I/O space marker in bit 0. */
259 pci_ioaddr &= ~3;
261 if((vendor != PCI_VENDOR_ID_SK) &&
262 (vendor != PCI_VENDOR_ID_COMPAQ))
263 continue;
265 if((vendor == PCI_VENDOR_ID_SK) &&
266 (device != PCI_DEVICE_ID_SK_TR))
267 continue;
268 else if((vendor == PCI_VENDOR_ID_COMPAQ) &&
269 (device != PCI_DEVICE_ID_COMPAQ_TOKENRING))
270 continue;
272 if(check_region(pci_ioaddr, SKTR_IO_EXTENT))
273 continue;
274 request_region(pci_ioaddr, SKTR_IO_EXTENT, pci_cardname);
275 if(request_irq(pdev->irq, sktr_interrupt, SA_SHIRQ,
276 pci_cardname, dev))
277 return (-ENODEV); /* continue; ?? */
279 AdapterName = pci_cardname;
281 new_command = (pci_command|PCI_COMMAND_MASTER|PCI_COMMAND_IO);
283 if(pci_command != new_command)
285 printk("The PCI BIOS has not enabled this"
286 "device! Updating PCI command %4.4x->%4.4x.\n",
287 pci_command, new_command);
288 pcibios_write_config_word(pci_bus, pci_device_fn,
289 PCI_COMMAND, new_command);
292 /* At this point we have found a valid PCI TR card. */
293 dev->base_addr = pci_ioaddr;
294 dev->irq = pci_irq_line;
295 dev->dma = 0;
297 printk("%s: %s found at %#4x, using IRQ %d.\n",
298 dev->name, AdapterName, pci_ioaddr, dev->irq);
300 return (0);
303 return (-1);
307 * Detect and setup the ISA SysKonnect TR cards.
309 static int __init sktr_isa_chk_card(struct net_device *dev, int ioaddr)
311 int i, err;
312 unsigned long flags;
314 err = sktr_isa_chk_ioaddr(ioaddr);
315 if(err < 0)
316 return (-ENODEV);
318 if(virt_to_bus((void*)((unsigned long)dev->priv+sizeof(struct net_local)))
319 > ISA_MAX_ADDRESS)
321 printk("%s: Memory not accessible for DMA\n", dev->name);
322 kfree(dev->priv);
323 return (-EAGAIN);
326 AdapterName = isa_cardname;
328 /* Grab the region so that no one else tries to probe our ioports. */
329 request_region(ioaddr, SKTR_IO_EXTENT, AdapterName);
330 dev->base_addr = ioaddr;
332 /* Autoselect IRQ and DMA if dev->irq == 0 */
333 if(dev->irq == 0)
335 for(i = 0; sktr_irqlist[i] != 0; i++)
337 dev->irq = sktr_irqlist[i];
338 err = request_irq(dev->irq, &sktr_interrupt, 0, AdapterName, dev);
339 if(!err)
340 break;
343 if(sktr_irqlist[i] == 0)
345 printk("%s: AutoSelect no IRQ available\n", dev->name);
346 return (-EAGAIN);
349 else
351 err = request_irq(dev->irq, &sktr_interrupt, 0, AdapterName, dev);
352 if(err)
354 printk("%s: Selected IRQ not available\n", dev->name);
355 return (-EAGAIN);
359 /* Always allocate the DMA channel after IRQ and clean up on failure */
360 if(dev->dma == 0)
362 for(i = 0; sktr_dmalist[i] != 0; i++)
364 dev->dma = sktr_dmalist[i];
365 err = request_dma(dev->dma, AdapterName);
366 if(!err)
367 break;
370 if(dev->dma == 0)
372 printk("%s: AutoSelect no DMA available\n", dev->name);
373 free_irq(dev->irq, NULL);
374 return (-EAGAIN);
377 else
379 err = request_dma(dev->dma, AdapterName);
380 if(err)
382 printk("%s: Selected DMA not available\n", dev->name);
383 free_irq(dev->irq, NULL);
384 return (-EAGAIN);
388 flags=claim_dma_lock();
389 disable_dma(dev->dma);
390 set_dma_mode(dev->dma, DMA_MODE_CASCADE);
391 enable_dma(dev->dma);
392 release_dma_lock(flags);
394 printk("%s: %s found at %#4x, using IRQ %d and DMA %d.\n",
395 dev->name, AdapterName, ioaddr, dev->irq, dev->dma);
397 return (0);
400 static int __init sktr_probe1(struct net_device *dev, int ioaddr)
402 static unsigned version_printed = 0;
403 struct net_local *tp;
404 int DeviceType = SK_PCI;
405 int err;
407 if(sktr_debug && version_printed++ == 0)
408 printk("%s", version);
410 #ifndef MODULE
411 dev = init_trdev(dev, 0);
412 if(dev == NULL)
413 return (-ENOMEM);
414 #endif
416 err = sktr_pci_chk_card(dev);
417 if(err < 0)
419 err = sktr_isa_chk_card(dev, ioaddr);
420 if(err < 0)
421 return (-ENODEV);
422 DeviceType = SK_ISA;
425 /* Setup this devices private information structure */
426 tp = (struct net_local *)kmalloc(sizeof(struct net_local), GFP_KERNEL | GFP_DMA);
427 if(tp == NULL)
428 return (-ENOMEM);
429 memset(tp, 0, sizeof(struct net_local));
430 tp->DeviceType = DeviceType;
431 init_waitqueue_head(&tp->wait_for_tok_int);
433 dev->priv = tp;
434 dev->init = sktr_init_card;
435 dev->open = sktr_open;
436 dev->stop = sktr_close;
437 dev->hard_start_xmit = sktr_send_packet;
438 dev->get_stats = sktr_get_stats;
439 dev->set_multicast_list = &sktr_set_multicast_list;
441 return (0);
444 /* Dummy function */
445 static int __init sktr_init_card(struct net_device *dev)
447 if(sktr_debug > 3)
448 printk("%s: sktr_init_card\n", dev->name);
450 return (0);
454 * This function tests if an adapter is really installed at the
455 * given I/O address. Return negative if no adapter at IO addr.
457 static int __init sktr_isa_chk_ioaddr(int ioaddr)
459 unsigned char old, chk1, chk2;
461 old = inb(ioaddr + SIFADR); /* Get the old SIFADR value */
463 chk1 = 0; /* Begin with check value 0 */
464 do {
465 /* Write new SIFADR value */
466 outb(chk1, ioaddr + SIFADR);
468 /* Read, invert and write */
469 chk2 = inb(ioaddr + SIFADD);
470 chk2 ^= 0x0FE;
471 outb(chk2, ioaddr + SIFADR);
473 /* Read, invert and compare */
474 chk2 = inb(ioaddr + SIFADD);
475 chk2 ^= 0x0FE;
477 if(chk1 != chk2)
478 return (-1); /* No adapter */
480 chk1 -= 2;
481 } while(chk1 != 0); /* Repeat 128 times (all byte values) */
483 /* Restore the SIFADR value */
484 outb(old, ioaddr + SIFADR);
486 return (0);
490 * Open/initialize the board. This is called sometime after
491 * booting when the 'ifconfig' program is run.
493 * This routine should set everything up anew at each open, even
494 * registers that "should" only need to be set once at boot, so that
495 * there is non-reboot way to recover if something goes wrong.
497 static int sktr_open(struct net_device *dev)
499 struct net_local *tp = (struct net_local *)dev->priv;
500 int err;
502 /* Reset the hardware here. Don't forget to set the station address. */
503 err = sktr_chipset_init(dev);
504 if(err)
506 printk(KERN_INFO "%s: Chipset initialization error\n",
507 dev->name);
508 return (-1);
511 dev->addr_len = 6;
512 sktr_read_addr(dev, (unsigned char*)dev->dev_addr);
514 init_timer(&tp->timer);
515 tp->timer.expires = jiffies + 30*HZ;
516 tp->timer.function = sktr_timer_end_wait;
517 tp->timer.data = (unsigned long)dev;
518 tp->timer.next = NULL;
519 tp->timer.prev = NULL;
520 add_timer(&tp->timer);
522 sktr_read_ptr(dev);
523 sktr_enable_interrupts(dev);
524 sktr_open_adapter(dev);
526 dev->tbusy = 0;
527 dev->interrupt = 0;
528 dev->start = 0;
530 /* Wait for interrupt from hardware. If interrupt does not come,
531 * there will be a timeout from the timer.
533 tp->Sleeping = 1;
534 interruptible_sleep_on(&tp->wait_for_tok_int);
535 del_timer(&tp->timer);
537 /* If AdapterVirtOpenFlag is 1, the adapter is now open for use */
538 if(tp->AdapterVirtOpenFlag == 0)
540 sktr_disable_interrupts(dev);
541 return (-1);
544 dev->start = 1;
546 tp->StartTime = jiffies;
548 /* Start function control timer */
549 tp->timer.expires = jiffies + 2*HZ;
550 tp->timer.function = sktr_timer_chk;
551 tp->timer.data = (unsigned long)dev;
552 add_timer(&tp->timer);
554 #ifdef MODULE
555 MOD_INC_USE_COUNT;
556 #endif
558 return (0);
562 * Timeout function while waiting for event
564 static void sktr_timer_end_wait(unsigned long data)
566 struct net_device *dev = (struct net_device*)data;
567 struct net_local *tp = (struct net_local *)dev->priv;
569 if(tp->Sleeping)
571 tp->Sleeping = 0;
572 wake_up_interruptible(&tp->wait_for_tok_int);
575 return;
579 * Initialize the chipset
581 static int sktr_chipset_init(struct net_device *dev)
583 struct net_local *tp = (struct net_local *)dev->priv;
584 unsigned char PosReg, Tmp;
585 int i, err;
587 sktr_init_ipb(tp);
588 sktr_init_opb(tp);
589 sktr_init_net_local(dev);
591 /* Set pos register: selects irq and dma channel.
592 * Only for ISA bus adapters.
594 if(dev->dma > 0)
596 PosReg = 0;
597 for(i = 0; sktr_irqlist[i] != 0; i++)
599 if(sktr_irqlist[i] == dev->irq)
600 break;
603 /* Choose default cycle time, 500 nsec */
604 PosReg |= CYCLE_TIME << 2;
605 PosReg |= i << 4;
606 i = dev->dma - 5;
607 PosReg |= i;
609 if(tp->DataRate == SPEED_4)
610 PosReg |= LINE_SPEED_BIT;
611 else
612 PosReg &= ~LINE_SPEED_BIT;
614 outb(PosReg, dev->base_addr + POSREG);
615 Tmp = inb(dev->base_addr + POSREG);
616 if((Tmp & ~CYCLE_TIME) != (PosReg & ~CYCLE_TIME))
617 printk(KERN_INFO "%s: POSREG error\n", dev->name);
620 err = sktr_reset_adapter(dev);
621 if(err < 0)
622 return (-1);
624 err = sktr_bringup_diags(dev);
625 if(err < 0)
626 return (-1);
628 err = sktr_init_adapter(dev);
629 if(err < 0)
630 return (-1);
632 return (0);
636 * Initializes the net_local structure.
638 static void sktr_init_net_local(struct net_device *dev)
640 struct net_local *tp = (struct net_local *)dev->priv;
641 int i;
643 tp->scb.CMD = 0;
644 tp->scb.Parm[0] = 0;
645 tp->scb.Parm[1] = 0;
647 tp->ssb.STS = 0;
648 tp->ssb.Parm[0] = 0;
649 tp->ssb.Parm[1] = 0;
650 tp->ssb.Parm[2] = 0;
652 tp->CMDqueue = 0;
654 tp->AdapterOpenFlag = 0;
655 tp->AdapterVirtOpenFlag = 0;
656 tp->ScbInUse = 0;
657 tp->OpenCommandIssued = 0;
658 tp->ReOpenInProgress = 0;
659 tp->HaltInProgress = 0;
660 tp->TransmitHaltScheduled = 0;
661 tp->LobeWireFaultLogged = 0;
662 tp->LastOpenStatus = 0;
663 tp->MaxPacketSize = DEFAULT_PACKET_SIZE;
665 skb_queue_head_init(&tp->SendSkbQueue);
666 tp->QueueSkb = MAX_TX_QUEUE;
668 /* Create circular chain of transmit lists */
669 for (i = 0; i < TPL_NUM; i++)
671 tp->Tpl[i].NextTPLAddr = htonl((unsigned long) virt_to_bus(&tp->Tpl[(i+1) % TPL_NUM]));
672 tp->Tpl[i].Status = 0;
673 tp->Tpl[i].FrameSize = 0;
674 tp->Tpl[i].FragList[0].DataCount = 0;
675 tp->Tpl[i].FragList[0].DataAddr = 0;
676 tp->Tpl[i].NextTPLPtr = &tp->Tpl[(i+1) % TPL_NUM];
677 tp->Tpl[i].MData = NULL;
678 tp->Tpl[i].TPLIndex = i;
679 tp->Tpl[i].BusyFlag = 0;
682 tp->TplFree = tp->TplBusy = &tp->Tpl[0];
684 /* Create circular chain of receive lists */
685 for (i = 0; i < RPL_NUM; i++)
687 tp->Rpl[i].NextRPLAddr = htonl((unsigned long) virt_to_bus(&tp->Rpl[(i+1) % RPL_NUM]));
688 tp->Rpl[i].Status = (RX_VALID | RX_START_FRAME | RX_END_FRAME | RX_FRAME_IRQ);
689 tp->Rpl[i].FrameSize = 0;
690 tp->Rpl[i].FragList[0].DataCount = SWAPB(tp->MaxPacketSize);
692 /* Alloc skb and point adapter to data area */
693 tp->Rpl[i].Skb = dev_alloc_skb(tp->MaxPacketSize);
695 /* skb == NULL ? then use local buffer */
696 if(tp->Rpl[i].Skb == NULL)
698 tp->Rpl[i].SkbStat = SKB_UNAVAILABLE;
699 tp->Rpl[i].FragList[0].DataAddr = htonl(virt_to_bus(tp->LocalRxBuffers[i]));
700 tp->Rpl[i].MData = tp->LocalRxBuffers[i];
702 else /* SKB != NULL */
704 tp->Rpl[i].Skb->dev = dev;
705 skb_put(tp->Rpl[i].Skb, tp->MaxPacketSize);
707 /* data unreachable for DMA ? then use local buffer */
708 if(tp->DeviceType == SK_ISA &&
709 virt_to_bus(tp->Rpl[i].Skb->data) +
710 tp->MaxPacketSize > ISA_MAX_ADDRESS)
712 tp->Rpl[i].SkbStat = SKB_DATA_COPY;
713 tp->Rpl[i].FragList[0].DataAddr = htonl(virt_to_bus(tp->LocalRxBuffers[i]));
714 tp->Rpl[i].MData = tp->LocalRxBuffers[i];
716 else /* DMA directly in skb->data */
718 tp->Rpl[i].SkbStat = SKB_DMA_DIRECT;
719 tp->Rpl[i].FragList[0].DataAddr = htonl(virt_to_bus(tp->Rpl[i].Skb->data));
720 tp->Rpl[i].MData = tp->Rpl[i].Skb->data;
724 tp->Rpl[i].NextRPLPtr = &tp->Rpl[(i+1) % RPL_NUM];
725 tp->Rpl[i].RPLIndex = i;
728 tp->RplHead = &tp->Rpl[0];
729 tp->RplTail = &tp->Rpl[RPL_NUM-1];
730 tp->RplTail->Status = (RX_START_FRAME | RX_END_FRAME | RX_FRAME_IRQ);
732 return;
736 * Initializes the initialisation parameter block.
738 static void sktr_init_ipb(struct net_local *tp)
740 tp->ipb.Init_Options = BURST_MODE;
741 tp->ipb.CMD_Status_IV = 0;
742 tp->ipb.TX_IV = 0;
743 tp->ipb.RX_IV = 0;
744 tp->ipb.Ring_Status_IV = 0;
745 tp->ipb.SCB_Clear_IV = 0;
746 tp->ipb.Adapter_CHK_IV = 0;
747 tp->ipb.RX_Burst_Size = BURST_SIZE;
748 tp->ipb.TX_Burst_Size = BURST_SIZE;
749 tp->ipb.DMA_Abort_Thrhld = DMA_RETRIES;
750 tp->ipb.SCB_Addr = 0;
751 tp->ipb.SSB_Addr = 0;
753 return;
757 * Initializes the open parameter block.
759 static void sktr_init_opb(struct net_local *tp)
761 unsigned long Addr;
762 unsigned short RplSize = RPL_SIZE;
763 unsigned short TplSize = TPL_SIZE;
764 unsigned short BufferSize = BUFFER_SIZE;
766 tp->ocpl.OPENOptions = 0;
767 tp->ocpl.OPENOptions |= ENABLE_FULL_DUPLEX_SELECTION;
768 /* tp->ocpl.OPENOptions |= PAD_ROUTING_FIELD; no more needed */
769 tp->ocpl.FullDuplex = 0;
770 tp->ocpl.FullDuplex |= OPEN_FULL_DUPLEX_OFF;
772 /* Fixme: If mac address setable:
773 * for (i=0; i<LENGTH_OF_ADDRESS; i++)
774 * mac->Vam->ocpl.NodeAddr[i] = mac->CurrentAddress[i];
777 tp->ocpl.GroupAddr = 0;
778 tp->ocpl.FunctAddr = 0;
779 tp->ocpl.RxListSize = SWAPB(RplSize);
780 tp->ocpl.TxListSize = SWAPB(TplSize);
781 tp->ocpl.BufSize = SWAPB(BufferSize);
782 tp->ocpl.Reserved = 0;
783 tp->ocpl.TXBufMin = TX_BUF_MIN;
784 tp->ocpl.TXBufMax = TX_BUF_MAX;
786 Addr = htonl(virt_to_bus(tp->ProductID));
788 tp->ocpl.ProdIDAddr[0] = LOWORD(Addr);
789 tp->ocpl.ProdIDAddr[1] = HIWORD(Addr);
791 return;
795 * Send OPEN command to adapter
797 static void sktr_open_adapter(struct net_device *dev)
799 struct net_local *tp = (struct net_local *)dev->priv;
801 if(tp->OpenCommandIssued)
802 return;
804 tp->OpenCommandIssued = 1;
805 sktr_exec_cmd(dev, OC_OPEN);
807 return;
811 * Clear the adapter's interrupt flag. Clear system interrupt enable
812 * (SINTEN): disable adapter to system interrupts.
814 static void sktr_disable_interrupts(struct net_device *dev)
816 outb(0, dev->base_addr + SIFACL);
818 return;
822 * Set the adapter's interrupt flag. Set system interrupt enable
823 * (SINTEN): enable adapter to system interrupts.
825 static void sktr_enable_interrupts(struct net_device *dev)
827 outb(ACL_SINTEN, dev->base_addr + SIFACL);
829 return;
833 * Put command in command queue, try to execute it.
835 static void sktr_exec_cmd(struct net_device *dev, unsigned short Command)
837 struct net_local *tp = (struct net_local *)dev->priv;
839 tp->CMDqueue |= Command;
840 sktr_chk_outstanding_cmds(dev);
842 return;
846 * Gets skb from system, queues it and checks if it can be sent
848 static int sktr_send_packet(struct sk_buff *skb, struct net_device *dev)
850 struct net_local *tp = (struct net_local *)dev->priv;
852 if(dev->tbusy)
855 * If we get here, some higher level has decided we are broken.
856 * There should really be a "kick me" function call instead.
858 * Resetting the token ring adapter takes a long time so just
859 * fake transmission time and go on trying. Our own timeout
860 * routine is in sktr_timer_chk()
862 dev->tbusy = 0;
863 dev->trans_start = jiffies;
864 return (1);
868 * If some higher layer thinks we've missed an tx-done interrupt we
869 * are passed NULL.
871 if(skb == NULL)
872 return (0);
875 * Block a timer-based transmit from overlapping. This could better be
876 * done with atomic_swap(1, dev->tbusy), but set_bit() works as well.
878 if(test_and_set_bit(0, (void*)&dev->tbusy) != 0)
880 printk("%s: Transmitter access conflict.\n", dev->name);
881 return (1);
884 if(tp->QueueSkb == 0)
885 return (1); /* Return with tbusy set: queue full */
887 tp->QueueSkb--;
888 skb_queue_tail(&tp->SendSkbQueue, skb);
889 sktr_hardware_send_packet(dev, tp);
890 if(tp->QueueSkb > 0)
891 dev->tbusy = 0;
893 return (0);
897 * Move frames from internal skb queue into adapter tx queue
899 static void sktr_hardware_send_packet(struct net_device *dev, struct net_local* tp)
901 TPL *tpl;
902 short length;
903 unsigned char *buf, *newbuf;
904 struct sk_buff *skb;
905 int i;
907 for(;;)
909 /* Try to get a free TPL from the chain.
911 * NOTE: We *must* always leave one unused TPL in the chain,
912 * because otherwise the adapter might send frames twice.
914 if(tp->TplFree->NextTPLPtr->BusyFlag) /* No free TPL */
916 printk(KERN_INFO "%s: No free TPL\n", dev->name);
917 return;
920 /* Send first buffer from queue */
921 skb = skb_dequeue(&tp->SendSkbQueue);
922 if(skb == NULL)
923 return;
925 tp->QueueSkb++;
926 /* Is buffer reachable for Busmaster-DMA? */
927 if(tp->DeviceType == SK_ISA &&
928 virt_to_bus((void*)(((long) skb->data) + skb->len))
929 > ISA_MAX_ADDRESS)
931 /* Copy frame to local buffer */
932 i = tp->TplFree->TPLIndex;
933 length = skb->len;
934 buf = tp->LocalTxBuffers[i];
935 memcpy(buf, skb->data, length);
936 newbuf = buf;
938 else
940 /* Send direct from skb->data */
941 length = skb->len;
942 newbuf = skb->data;
945 /* Source address in packet? */
946 sktr_chk_src_addr(newbuf, dev->dev_addr);
948 tp->LastSendTime = jiffies;
949 tpl = tp->TplFree; /* Get the "free" TPL */
950 tpl->BusyFlag = 1; /* Mark TPL as busy */
951 tp->TplFree = tpl->NextTPLPtr;
953 /* Save the skb for delayed return of skb to system */
954 tpl->Skb = skb;
955 tpl->FragList[0].DataCount = (unsigned short) SWAPB(length);
956 tpl->FragList[0].DataAddr = htonl(virt_to_bus(newbuf));
958 /* Write the data length in the transmit list. */
959 tpl->FrameSize = (unsigned short) SWAPB(length);
960 tpl->MData = newbuf;
962 /* Transmit the frame and set the status values. */
963 sktr_write_tpl_status(tpl, TX_VALID | TX_START_FRAME
964 | TX_END_FRAME | TX_PASS_SRC_ADDR
965 | TX_FRAME_IRQ);
967 /* Let adapter send the frame. */
968 sktr_exec_sifcmd(dev, CMD_TX_VALID);
971 return;
975 * Write the given value to the 'Status' field of the specified TPL.
976 * NOTE: This function should be used whenever the status of any TPL must be
977 * modified by the driver, because the compiler may otherwise change the
978 * order of instructions such that writing the TPL status may be executed at
979 * an undesireable time. When this function is used, the status is always
980 * written when the function is called.
982 static void sktr_write_tpl_status(TPL *tpl, unsigned int Status)
984 tpl->Status = Status;
987 static void sktr_chk_src_addr(unsigned char *frame, unsigned char *hw_addr)
989 unsigned char SRBit;
991 if((((unsigned long)frame[8]) & ~0x80) != 0) /* Compare 4 bytes */
992 return;
993 if((unsigned short)frame[12] != 0) /* Compare 2 bytes */
994 return;
996 SRBit = frame[8] & 0x80;
997 memcpy(&frame[8], hw_addr, 6);
998 frame[8] |= SRBit;
1000 return;
1004 * The timer routine: Check if adapter still open and working, reopen if not.
1006 static void sktr_timer_chk(unsigned long data)
1008 struct net_device *dev = (struct net_device*)data;
1009 struct net_local *tp = (struct net_local*)dev->priv;
1011 if(tp->HaltInProgress)
1012 return;
1014 sktr_chk_outstanding_cmds(dev);
1015 if(time_before(tp->LastSendTime + SEND_TIMEOUT, jiffies)
1016 && (tp->QueueSkb < MAX_TX_QUEUE || tp->TplFree != tp->TplBusy))
1018 /* Anything to send, but stalled to long */
1019 tp->LastSendTime = jiffies;
1020 sktr_exec_cmd(dev, OC_CLOSE); /* Does reopen automatically */
1023 tp->timer.expires = jiffies + 2*HZ;
1024 add_timer(&tp->timer);
1026 if(tp->AdapterOpenFlag || tp->ReOpenInProgress)
1027 return;
1028 tp->ReOpenInProgress = 1;
1029 sktr_open_adapter(dev);
1031 return;
1035 * The typical workload of the driver: Handle the network interface interrupts.
1037 static void sktr_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1039 struct net_device *dev = dev_id;
1040 struct net_local *tp;
1041 int ioaddr;
1042 unsigned short irq_type;
1044 if(dev == NULL)
1046 printk("%s: irq %d for unknown device.\n", dev->name, irq);
1047 return;
1050 dev->interrupt = 1;
1052 ioaddr = dev->base_addr;
1053 tp = (struct net_local *)dev->priv;
1055 irq_type = inw(ioaddr + SIFSTS);
1057 while(irq_type & STS_SYSTEM_IRQ)
1059 irq_type &= STS_IRQ_MASK;
1061 if(!sktr_chk_ssb(tp, irq_type))
1063 printk(KERN_INFO "%s: DATA LATE occurred\n", dev->name);
1064 break;
1067 switch(irq_type)
1069 case STS_IRQ_RECEIVE_STATUS:
1070 sktr_reset_interrupt(dev);
1071 sktr_rcv_status_irq(dev);
1072 break;
1074 case STS_IRQ_TRANSMIT_STATUS:
1075 /* Check if TRANSMIT.HALT command is complete */
1076 if(tp->ssb.Parm[0] & COMMAND_COMPLETE)
1078 tp->TransmitCommandActive = 0;
1079 tp->TransmitHaltScheduled = 0;
1081 /* Issue a new transmit command. */
1082 sktr_exec_cmd(dev, OC_TRANSMIT);
1085 sktr_reset_interrupt(dev);
1086 sktr_tx_status_irq(dev);
1087 break;
1089 case STS_IRQ_COMMAND_STATUS:
1090 /* The SSB contains status of last command
1091 * other than receive/transmit.
1093 sktr_cmd_status_irq(dev);
1094 break;
1096 case STS_IRQ_SCB_CLEAR:
1097 /* The SCB is free for another command. */
1098 tp->ScbInUse = 0;
1099 sktr_chk_outstanding_cmds(dev);
1100 break;
1102 case STS_IRQ_RING_STATUS:
1103 sktr_ring_status_irq(dev);
1104 break;
1106 case STS_IRQ_ADAPTER_CHECK:
1107 sktr_chk_irq(dev);
1108 break;
1110 default:
1111 printk(KERN_INFO "Unknown Token Ring IRQ\n");
1112 break;
1115 /* Reset system interrupt if not already done. */
1116 if(irq_type != STS_IRQ_TRANSMIT_STATUS
1117 && irq_type != STS_IRQ_RECEIVE_STATUS)
1119 sktr_reset_interrupt(dev);
1122 irq_type = inw(ioaddr + SIFSTS);
1125 dev->interrupt = 0;
1127 return;
1131 * Reset the INTERRUPT SYSTEM bit and issue SSB CLEAR command.
1133 static void sktr_reset_interrupt(struct net_device *dev)
1135 struct net_local *tp = (struct net_local *)dev->priv;
1136 SSB *ssb = &tp->ssb;
1139 * [Workaround for "Data Late"]
1140 * Set all fields of the SSB to well-defined values so we can
1141 * check if the adapter has written the SSB.
1144 ssb->STS = (unsigned short) -1;
1145 ssb->Parm[0] = (unsigned short) -1;
1146 ssb->Parm[1] = (unsigned short) -1;
1147 ssb->Parm[2] = (unsigned short) -1;
1149 /* Free SSB by issuing SSB_CLEAR command after reading IRQ code
1150 * and clear STS_SYSTEM_IRQ bit: enable adapter for further interrupts.
1152 sktr_exec_sifcmd(dev, CMD_SSB_CLEAR | CMD_CLEAR_SYSTEM_IRQ);
1154 return;
1158 * Check if the SSB has actually been written by the adapter.
1160 static unsigned char sktr_chk_ssb(struct net_local *tp, unsigned short IrqType)
1162 SSB *ssb = &tp->ssb; /* The address of the SSB. */
1164 /* C 0 1 2 INTERRUPT CODE
1165 * - - - - --------------
1166 * 1 1 1 1 TRANSMIT STATUS
1167 * 1 1 1 1 RECEIVE STATUS
1168 * 1 ? ? 0 COMMAND STATUS
1169 * 0 0 0 0 SCB CLEAR
1170 * 1 1 0 0 RING STATUS
1171 * 0 0 0 0 ADAPTER CHECK
1173 * 0 = SSB field not affected by interrupt
1174 * 1 = SSB field is affected by interrupt
1176 * C = SSB ADDRESS +0: COMMAND
1177 * 0 = SSB ADDRESS +2: STATUS 0
1178 * 1 = SSB ADDRESS +4: STATUS 1
1179 * 2 = SSB ADDRESS +6: STATUS 2
1182 /* Check if this interrupt does use the SSB. */
1184 if(IrqType != STS_IRQ_TRANSMIT_STATUS
1185 && IrqType != STS_IRQ_RECEIVE_STATUS
1186 && IrqType != STS_IRQ_COMMAND_STATUS
1187 && IrqType != STS_IRQ_RING_STATUS)
1189 return (1); /* SSB not involved. */
1192 /* Note: All fields of the SSB have been set to all ones (-1) after it
1193 * has last been used by the software (see DriverIsr()).
1195 * Check if the affected SSB fields are still unchanged.
1198 if(ssb->STS == (unsigned short) -1)
1199 return (0); /* Command field not yet available. */
1200 if(IrqType == STS_IRQ_COMMAND_STATUS)
1201 return (1); /* Status fields not always affected. */
1202 if(ssb->Parm[0] == (unsigned short) -1)
1203 return (0); /* Status 1 field not yet available. */
1204 if(IrqType == STS_IRQ_RING_STATUS)
1205 return (1); /* Status 2 & 3 fields not affected. */
1207 /* Note: At this point, the interrupt is either TRANSMIT or RECEIVE. */
1208 if(ssb->Parm[1] == (unsigned short) -1)
1209 return (0); /* Status 2 field not yet available. */
1210 if(ssb->Parm[2] == (unsigned short) -1)
1211 return (0); /* Status 3 field not yet available. */
1213 return (1); /* All SSB fields have been written by the adapter. */
1217 * Evaluates the command results status in the SSB status field.
1219 static void sktr_cmd_status_irq(struct net_device *dev)
1221 struct net_local *tp = (struct net_local *)dev->priv;
1222 unsigned short ssb_cmd, ssb_parm_0;
1223 unsigned short ssb_parm_1;
1224 char *open_err = "Open error -";
1225 char *code_err = "Open code -";
1227 /* Copy the ssb values to local variables */
1228 ssb_cmd = tp->ssb.STS;
1229 ssb_parm_0 = tp->ssb.Parm[0];
1230 ssb_parm_1 = tp->ssb.Parm[1];
1232 if(ssb_cmd == OPEN)
1234 tp->Sleeping = 0;
1235 if(!tp->ReOpenInProgress)
1236 wake_up_interruptible(&tp->wait_for_tok_int);
1238 tp->OpenCommandIssued = 0;
1239 tp->ScbInUse = 0;
1241 if((ssb_parm_0 & 0x00FF) == GOOD_COMPLETION)
1243 /* Success, the adapter is open. */
1244 tp->LobeWireFaultLogged = 0;
1245 tp->AdapterOpenFlag = 1;
1246 tp->AdapterVirtOpenFlag = 1;
1247 tp->TransmitCommandActive = 0;
1248 sktr_exec_cmd(dev, OC_TRANSMIT);
1249 sktr_exec_cmd(dev, OC_RECEIVE);
1251 if(tp->ReOpenInProgress)
1252 tp->ReOpenInProgress = 0;
1254 return;
1256 else /* The adapter did not open. */
1258 if(ssb_parm_0 & NODE_ADDR_ERROR)
1259 printk(KERN_INFO "%s: Node address error\n",
1260 dev->name);
1261 if(ssb_parm_0 & LIST_SIZE_ERROR)
1262 printk(KERN_INFO "%s: List size error\n",
1263 dev->name);
1264 if(ssb_parm_0 & BUF_SIZE_ERROR)
1265 printk(KERN_INFO "%s: Buffer size error\n",
1266 dev->name);
1267 if(ssb_parm_0 & TX_BUF_COUNT_ERROR)
1268 printk(KERN_INFO "%s: Tx buffer count error\n",
1269 dev->name);
1270 if(ssb_parm_0 & INVALID_OPEN_OPTION)
1271 printk(KERN_INFO "%s: Invalid open option\n",
1272 dev->name);
1273 if(ssb_parm_0 & OPEN_ERROR)
1275 /* Show the open phase. */
1276 switch(ssb_parm_0 & OPEN_PHASES_MASK)
1278 case LOBE_MEDIA_TEST:
1279 if(!tp->LobeWireFaultLogged)
1281 tp->LobeWireFaultLogged = 1;
1282 printk(KERN_INFO "%s: %s Lobe wire fault (check cable !).\n", dev->name, open_err);
1284 tp->ReOpenInProgress = 1;
1285 tp->AdapterOpenFlag = 0;
1286 tp->AdapterVirtOpenFlag = 1;
1287 sktr_open_adapter(dev);
1288 return;
1290 case PHYSICAL_INSERTION:
1291 printk(KERN_INFO "%s: %s Physical insertion.\n", dev->name, open_err);
1292 break;
1294 case ADDRESS_VERIFICATION:
1295 printk(KERN_INFO "%s: %s Address verification.\n", dev->name, open_err);
1296 break;
1298 case PARTICIPATION_IN_RING_POLL:
1299 printk(KERN_INFO "%s: %s Participation in ring poll.\n", dev->name, open_err);
1300 break;
1302 case REQUEST_INITIALISATION:
1303 printk(KERN_INFO "%s: %s Request initialisation.\n", dev->name, open_err);
1304 break;
1306 case FULLDUPLEX_CHECK:
1307 printk(KERN_INFO "%s: %s Full duplex check.\n", dev->name, open_err);
1308 break;
1310 default:
1311 printk(KERN_INFO "%s: %s Unknown open phase\n", dev->name, open_err);
1312 break;
1315 /* Show the open errors. */
1316 switch(ssb_parm_0 & OPEN_ERROR_CODES_MASK)
1318 case OPEN_FUNCTION_FAILURE:
1319 printk(KERN_INFO "%s: %s OPEN_FUNCTION_FAILURE", dev->name, code_err);
1320 tp->LastOpenStatus =
1321 OPEN_FUNCTION_FAILURE;
1322 break;
1324 case OPEN_SIGNAL_LOSS:
1325 printk(KERN_INFO "%s: %s OPEN_SIGNAL_LOSS\n", dev->name, code_err);
1326 tp->LastOpenStatus =
1327 OPEN_SIGNAL_LOSS;
1328 break;
1330 case OPEN_TIMEOUT:
1331 printk(KERN_INFO "%s: %s OPEN_TIMEOUT\n", dev->name, code_err);
1332 tp->LastOpenStatus =
1333 OPEN_TIMEOUT;
1334 break;
1336 case OPEN_RING_FAILURE:
1337 printk(KERN_INFO "%s: %s OPEN_RING_FAILURE\n", dev->name, code_err);
1338 tp->LastOpenStatus =
1339 OPEN_RING_FAILURE;
1340 break;
1342 case OPEN_RING_BEACONING:
1343 printk(KERN_INFO "%s: %s OPEN_RING_BEACONING\n", dev->name, code_err);
1344 tp->LastOpenStatus =
1345 OPEN_RING_BEACONING;
1346 break;
1348 case OPEN_DUPLICATE_NODEADDR:
1349 printk(KERN_INFO "%s: %s OPEN_DUPLICATE_NODEADDR\n", dev->name, code_err);
1350 tp->LastOpenStatus =
1351 OPEN_DUPLICATE_NODEADDR;
1352 break;
1354 case OPEN_REQUEST_INIT:
1355 printk(KERN_INFO "%s: %s OPEN_REQUEST_INIT\n", dev->name, code_err);
1356 tp->LastOpenStatus =
1357 OPEN_REQUEST_INIT;
1358 break;
1360 case OPEN_REMOVE_RECEIVED:
1361 printk(KERN_INFO "%s: %s OPEN_REMOVE_RECEIVED", dev->name, code_err);
1362 tp->LastOpenStatus =
1363 OPEN_REMOVE_RECEIVED;
1364 break;
1366 case OPEN_FULLDUPLEX_SET:
1367 printk(KERN_INFO "%s: %s OPEN_FULLDUPLEX_SET\n", dev->name, code_err);
1368 tp->LastOpenStatus =
1369 OPEN_FULLDUPLEX_SET;
1370 break;
1372 default:
1373 printk(KERN_INFO "%s: %s Unknown open err code", dev->name, code_err);
1374 tp->LastOpenStatus =
1375 OPEN_FUNCTION_FAILURE;
1376 break;
1380 tp->AdapterOpenFlag = 0;
1381 tp->AdapterVirtOpenFlag = 0;
1383 return;
1386 else
1388 if(ssb_cmd != READ_ERROR_LOG)
1389 return;
1391 /* Add values from the error log table to the MAC
1392 * statistics counters and update the errorlogtable
1393 * memory.
1395 tp->MacStat.line_errors += tp->errorlogtable.Line_Error;
1396 tp->MacStat.burst_errors += tp->errorlogtable.Burst_Error;
1397 tp->MacStat.A_C_errors += tp->errorlogtable.ARI_FCI_Error;
1398 tp->MacStat.lost_frames += tp->errorlogtable.Lost_Frame_Error;
1399 tp->MacStat.recv_congest_count += tp->errorlogtable.Rx_Congest_Error;
1400 tp->MacStat.rx_errors += tp->errorlogtable.Rx_Congest_Error;
1401 tp->MacStat.frame_copied_errors += tp->errorlogtable.Frame_Copied_Error;
1402 tp->MacStat.token_errors += tp->errorlogtable.Token_Error;
1403 tp->MacStat.dummy1 += tp->errorlogtable.DMA_Bus_Error;
1404 tp->MacStat.dummy1 += tp->errorlogtable.DMA_Parity_Error;
1405 tp->MacStat.abort_delimiters += tp->errorlogtable.AbortDelimeters;
1406 tp->MacStat.frequency_errors += tp->errorlogtable.Frequency_Error;
1407 tp->MacStat.internal_errors += tp->errorlogtable.Internal_Error;
1410 return;
1414 * The inverse routine to sktr_open().
1416 static int sktr_close(struct net_device *dev)
1418 struct net_local *tp = (struct net_local *)dev->priv;
1420 dev->tbusy = 1;
1421 dev->start = 0;
1423 del_timer(&tp->timer);
1425 /* Flush the Tx and disable Rx here. */
1427 tp->HaltInProgress = 1;
1428 sktr_exec_cmd(dev, OC_CLOSE);
1429 tp->timer.expires = jiffies + 1*HZ;
1430 tp->timer.function = sktr_timer_end_wait;
1431 tp->timer.data = (unsigned long)dev;
1432 add_timer(&tp->timer);
1434 sktr_enable_interrupts(dev);
1436 tp->Sleeping = 1;
1437 interruptible_sleep_on(&tp->wait_for_tok_int);
1438 tp->TransmitCommandActive = 0;
1440 del_timer(&tp->timer);
1441 sktr_disable_interrupts(dev);
1443 if(dev->dma > 0)
1445 unsigned long flags=claim_dma_lock();
1446 disable_dma(dev->dma);
1447 release_dma_lock(flags);
1450 outw(0xFF00, dev->base_addr + SIFCMD);
1451 if(dev->dma > 0)
1452 outb(0xff, dev->base_addr + POSREG);
1454 #ifdef MODULE
1455 MOD_DEC_USE_COUNT;
1456 #endif
1458 sktr_cancel_tx_queue(tp);
1460 return (0);
1464 * Get the current statistics. This may be called with the card open
1465 * or closed.
1467 static struct enet_statistics *sktr_get_stats(struct net_device *dev)
1469 struct net_local *tp = (struct net_local *)dev->priv;
1471 return ((struct enet_statistics *)&tp->MacStat);
1475 * Set or clear the multicast filter for this adapter.
1477 static void sktr_set_multicast_list(struct net_device *dev)
1479 struct net_local *tp = (struct net_local *)dev->priv;
1480 unsigned int OpenOptions;
1482 OpenOptions = tp->ocpl.OPENOptions &
1483 ~(PASS_ADAPTER_MAC_FRAMES
1484 | PASS_ATTENTION_FRAMES
1485 | PASS_BEACON_MAC_FRAMES
1486 | COPY_ALL_MAC_FRAMES
1487 | COPY_ALL_NON_MAC_FRAMES);
1489 tp->ocpl.FunctAddr = 0;
1491 if(dev->flags & IFF_PROMISC)
1492 /* Enable promiscuous mode */
1493 OpenOptions |= COPY_ALL_NON_MAC_FRAMES |
1494 COPY_ALL_MAC_FRAMES;
1495 else
1497 if(dev->flags & IFF_ALLMULTI)
1499 /* Disable promiscuous mode, use normal mode. */
1500 tp->ocpl.FunctAddr = 0xFFFFFFFF;
1503 else
1505 int i;
1506 struct dev_mc_list *mclist = dev->mc_list;
1507 for (i=0; i< dev->mc_count; i++)
1509 ((char *)(&tp->ocpl.FunctAddr))[0] |=
1510 mclist->dmi_addr[2];
1511 ((char *)(&tp->ocpl.FunctAddr))[1] |=
1512 mclist->dmi_addr[3];
1513 ((char *)(&tp->ocpl.FunctAddr))[2] |=
1514 mclist->dmi_addr[4];
1515 ((char *)(&tp->ocpl.FunctAddr))[3] |=
1516 mclist->dmi_addr[5];
1517 mclist = mclist->next;
1520 sktr_exec_cmd(dev, OC_SET_FUNCT_ADDR);
1523 tp->ocpl.OPENOptions = OpenOptions;
1524 sktr_exec_cmd(dev, OC_MODIFY_OPEN_PARMS);
1525 return;
1529 * Wait for some time (microseconds)
1531 * udelay() is a bit harsh, but using a looser timer causes
1532 * the bring-up-diags to stall indefinitly.
1536 static void sktr_wait(unsigned long time)
1538 udelay(time);
1539 return;
1543 * Write a command value to the SIFCMD register
1545 static void sktr_exec_sifcmd(struct net_device *dev, unsigned int WriteValue)
1547 int ioaddr = dev->base_addr;
1548 unsigned short cmd;
1549 unsigned short SifStsValue;
1550 unsigned long loop_counter;
1552 WriteValue = ((WriteValue ^ CMD_SYSTEM_IRQ) | CMD_INTERRUPT_ADAPTER);
1553 cmd = (unsigned short)WriteValue;
1554 loop_counter = 0,5 * 800000;
1555 do {
1556 SifStsValue = inw(ioaddr + SIFSTS);
1557 } while((SifStsValue & CMD_INTERRUPT_ADAPTER) && loop_counter--);
1558 outw(cmd, ioaddr + SIFCMD);
1560 return;
1564 * Processes adapter hardware reset, halts adapter and downloads firmware,
1565 * clears the halt bit.
1567 static int sktr_reset_adapter(struct net_device *dev)
1569 struct net_local *tp = (struct net_local *)dev->priv;
1570 unsigned short *fw_ptr = (unsigned short *)&sktr_code;
1571 unsigned short count, c;
1572 int ioaddr = dev->base_addr;
1574 /* Hardware adapter reset */
1575 outw(ACL_ARESET, ioaddr + SIFACL);
1576 sktr_wait(40);
1578 c = inw(ioaddr + SIFACL);
1579 sktr_wait(20);
1581 if(dev->dma == 0) /* For PCI adapters */
1583 c &= ~(ACL_SPEED4 | ACL_SPEED16); /* Clear bits */
1584 if(tp->DataRate == SPEED_4)
1585 c |= ACL_SPEED4; /* Set 4Mbps */
1586 else
1587 c |= ACL_SPEED16; /* Set 16Mbps */
1590 /* In case a command is pending - forget it */
1591 tp->ScbInUse = 0;
1593 c &= ~ACL_ARESET; /* Clear adapter reset bit */
1594 c |= ACL_CPHALT; /* Halt adapter CPU, allow download */
1595 c &= ~ACL_PSDMAEN; /* Clear pseudo dma bit */
1596 outw(c, ioaddr + SIFACL);
1597 sktr_wait(40);
1599 /* Download firmware via DIO interface: */
1600 do {
1601 /* Download first address part */
1602 outw(*fw_ptr, ioaddr + SIFADX);
1603 fw_ptr++;
1605 /* Download second address part */
1606 outw(*fw_ptr, ioaddr + SIFADD);
1607 fw_ptr++;
1609 if((count = *fw_ptr) != 0) /* Load loop counter */
1611 fw_ptr++; /* Download block data */
1612 for(; count > 0; count--)
1614 outw(*fw_ptr, ioaddr + SIFINC);
1615 fw_ptr++;
1618 else /* Stop, if last block downloaded */
1620 c = inw(ioaddr + SIFACL);
1621 c &= (~ACL_CPHALT | ACL_SINTEN);
1623 /* Clear CPHALT and start BUD */
1624 outw(c, ioaddr + SIFACL);
1625 return (1);
1627 } while(count == 0);
1629 return (-1);
1633 * Starts bring up diagnostics of token ring adapter and evaluates
1634 * diagnostic results.
1636 static int sktr_bringup_diags(struct net_device *dev)
1638 int loop_cnt, retry_cnt;
1639 unsigned short Status;
1640 int ioaddr = dev->base_addr;
1642 sktr_wait(HALF_SECOND);
1643 sktr_exec_sifcmd(dev, EXEC_SOFT_RESET);
1644 sktr_wait(HALF_SECOND);
1646 retry_cnt = BUD_MAX_RETRIES; /* maximal number of retrys */
1648 do {
1649 retry_cnt--;
1650 if(sktr_debug > 3)
1651 printk(KERN_INFO "BUD-Status: \n");
1652 loop_cnt = BUD_MAX_LOOPCNT; /* maximum: three seconds*/
1653 do { /* Inspect BUD results */
1654 loop_cnt--;
1655 sktr_wait(HALF_SECOND);
1656 Status = inw(ioaddr + SIFSTS);
1657 Status &= STS_MASK;
1659 if(sktr_debug > 3)
1660 printk(KERN_INFO " %04X \n", Status);
1661 /* BUD successfully completed */
1662 if(Status == STS_INITIALIZE)
1663 return (1);
1664 /* Unrecoverable hardware error, BUD not completed? */
1665 } while((loop_cnt > 0) && ((Status & (STS_ERROR | STS_TEST))
1666 != (STS_ERROR | STS_TEST)));
1668 /* Error preventing completion of BUD */
1669 if(retry_cnt > 0)
1671 printk(KERN_INFO "%s: Adapter Software Reset.\n",
1672 dev->name);
1673 sktr_exec_sifcmd(dev, EXEC_SOFT_RESET);
1674 sktr_wait(HALF_SECOND);
1676 } while(retry_cnt > 0);
1678 Status = inw(ioaddr + SIFSTS);
1679 Status &= STS_ERROR_MASK; /* Hardware error occurred! */
1681 printk(KERN_INFO "%s: Bring Up Diagnostics Error (%04X) occurred\n",
1682 dev->name, Status);
1684 return (-1);
1688 * Copy initialisation data to adapter memory, beginning at address
1689 * 1:0A00; Starting DMA test and evaluating result bits.
1691 static int sktr_init_adapter(struct net_device *dev)
1693 struct net_local *tp = (struct net_local *)dev->priv;
1695 const unsigned char SCB_Test[6] = {0x00, 0x00, 0xC1, 0xE2, 0xD4, 0x8B};
1696 const unsigned char SSB_Test[8] = {0xFF, 0xFF, 0xD1, 0xD7,
1697 0xC5, 0xD9, 0xC3, 0xD4};
1698 void *ptr = (void *)&tp->ipb;
1699 unsigned short *ipb_ptr = (unsigned short *)ptr;
1700 unsigned char *cb_ptr = (unsigned char *) &tp->scb;
1701 unsigned char *sb_ptr = (unsigned char *) &tp->ssb;
1702 unsigned short Status;
1703 int i, loop_cnt, retry_cnt;
1704 int ioaddr = dev->base_addr;
1706 /* Normalize: byte order low/high, word order high/low! (only IPB!) */
1707 tp->ipb.SCB_Addr = SWAPW(virt_to_bus(&tp->scb));
1708 tp->ipb.SSB_Addr = SWAPW(virt_to_bus(&tp->ssb));
1710 /* Maximum: three initialization retries */
1711 retry_cnt = INIT_MAX_RETRIES;
1713 do {
1714 retry_cnt--;
1716 /* Transfer initialization block */
1717 outw(0x0001, ioaddr + SIFADX);
1719 /* To address 0001:0A00 of adapter RAM */
1720 outw(0x0A00, ioaddr + SIFADD);
1722 /* Write 11 words to adapter RAM */
1723 for(i = 0; i < 11; i++)
1724 outw(ipb_ptr[i], ioaddr + SIFINC);
1726 /* Execute SCB adapter command */
1727 sktr_exec_sifcmd(dev, CMD_EXECUTE);
1729 loop_cnt = INIT_MAX_LOOPCNT; /* Maximum: 11 seconds */
1731 /* While remaining retries, no error and not completed */
1732 do {
1733 Status = 0;
1734 loop_cnt--;
1735 sktr_wait(HALF_SECOND);
1737 /* Mask interesting status bits */
1738 Status = inw(ioaddr + SIFSTS);
1739 Status &= STS_MASK;
1740 } while(((Status &(STS_INITIALIZE | STS_ERROR | STS_TEST)) != 0)
1741 && ((Status & STS_ERROR) == 0) && (loop_cnt != 0));
1743 if((Status & (STS_INITIALIZE | STS_ERROR | STS_TEST)) == 0)
1745 /* Initialization completed without error */
1746 i = 0;
1747 do { /* Test if contents of SCB is valid */
1748 if(SCB_Test[i] != *(cb_ptr + i))
1749 /* DMA data error: wrong data in SCB */
1750 return (-1);
1751 i++;
1752 } while(i < 6);
1754 i = 0;
1755 do { /* Test if contents of SSB is valid */
1756 if(SSB_Test[i] != *(sb_ptr + i))
1757 /* DMA data error: wrong data in SSB */
1758 return (-1);
1759 i++;
1760 } while (i < 8);
1762 return (1); /* Adapter successfully initialized */
1764 else
1766 if((Status & STS_ERROR) != 0)
1768 /* Initialization error occurred */
1769 Status = inw(ioaddr + SIFSTS);
1770 Status &= STS_ERROR_MASK;
1771 /* ShowInitialisationErrorCode(Status); */
1772 return (-1); /* Unrecoverable error */
1774 else
1776 if(retry_cnt > 0)
1778 /* Reset adapter and try init again */
1779 sktr_exec_sifcmd(dev, EXEC_SOFT_RESET);
1780 sktr_wait(HALF_SECOND);
1784 } while(retry_cnt > 0);
1786 return (-1);
1790 * Check for outstanding commands in command queue and tries to execute
1791 * command immediately. Corresponding command flag in command queue is cleared.
1793 static void sktr_chk_outstanding_cmds(struct net_device *dev)
1795 struct net_local *tp = (struct net_local *)dev->priv;
1796 unsigned long Addr = 0;
1797 unsigned char i = 0;
1799 if(tp->CMDqueue == 0)
1800 return; /* No command execution */
1802 /* If SCB in use: no command */
1803 if(tp->ScbInUse == 1)
1804 return;
1806 /* Check if adapter is opened, avoiding COMMAND_REJECT
1807 * interrupt by the adapter!
1809 if(tp->AdapterOpenFlag == 0)
1811 if(tp->CMDqueue & OC_OPEN)
1813 /* Execute OPEN command */
1814 tp->CMDqueue ^= OC_OPEN;
1816 /* Copy the 18 bytes of the product ID */
1817 while((AdapterName[i] != '\0') && (i < PROD_ID_SIZE))
1819 tp->ProductID[i] = AdapterName[i];
1820 i++;
1823 Addr = htonl(virt_to_bus(&tp->ocpl));
1824 tp->scb.Parm[0] = LOWORD(Addr);
1825 tp->scb.Parm[1] = HIWORD(Addr);
1826 tp->scb.CMD = OPEN;
1828 else
1829 /* No OPEN command queued, but adapter closed. Note:
1830 * We'll try to re-open the adapter in DriverPoll()
1832 return; /* No adapter command issued */
1834 else
1836 /* Adapter is open; evaluate command queue: try to execute
1837 * outstanding commands (depending on priority!) CLOSE
1838 * command queued
1840 if(tp->CMDqueue & OC_CLOSE)
1842 tp->CMDqueue ^= OC_CLOSE;
1843 tp->AdapterOpenFlag = 0;
1844 tp->scb.Parm[0] = 0; /* Parm[0], Parm[1] are ignored */
1845 tp->scb.Parm[1] = 0; /* but should be set to zero! */
1846 tp->scb.CMD = CLOSE;
1847 if(!tp->HaltInProgress)
1848 tp->CMDqueue |= OC_OPEN; /* re-open adapter */
1849 else
1850 tp->CMDqueue = 0; /* no more commands */
1852 else
1854 if(tp->CMDqueue & OC_RECEIVE)
1856 tp->CMDqueue ^= OC_RECEIVE;
1857 Addr = htonl(virt_to_bus(tp->RplHead));
1858 tp->scb.Parm[0] = LOWORD(Addr);
1859 tp->scb.Parm[1] = HIWORD(Addr);
1860 tp->scb.CMD = RECEIVE;
1862 else
1864 if(tp->CMDqueue & OC_TRANSMIT_HALT)
1866 /* NOTE: TRANSMIT.HALT must be checked
1867 * before TRANSMIT.
1869 tp->CMDqueue ^= OC_TRANSMIT_HALT;
1870 tp->scb.CMD = TRANSMIT_HALT;
1872 /* Parm[0] and Parm[1] are ignored
1873 * but should be set to zero!
1875 tp->scb.Parm[0] = 0;
1876 tp->scb.Parm[1] = 0;
1878 else
1880 if(tp->CMDqueue & OC_TRANSMIT)
1882 /* NOTE: TRANSMIT must be
1883 * checked after TRANSMIT.HALT
1885 if(tp->TransmitCommandActive)
1887 if(!tp->TransmitHaltScheduled)
1889 tp->TransmitHaltScheduled = 1;
1890 sktr_exec_cmd(dev, OC_TRANSMIT_HALT) ;
1892 tp->TransmitCommandActive = 0;
1893 return;
1896 tp->CMDqueue ^= OC_TRANSMIT;
1897 sktr_cancel_tx_queue(tp);
1898 Addr = htonl(virt_to_bus(tp->TplBusy));
1899 tp->scb.Parm[0] = LOWORD(Addr);
1900 tp->scb.Parm[1] = HIWORD(Addr);
1901 tp->scb.CMD = TRANSMIT;
1902 tp->TransmitCommandActive = 1;
1904 else
1906 if(tp->CMDqueue & OC_MODIFY_OPEN_PARMS)
1908 tp->CMDqueue ^= OC_MODIFY_OPEN_PARMS;
1909 tp->scb.Parm[0] = tp->ocpl.OPENOptions; /* new OPEN options*/
1910 tp->scb.Parm[0] |= ENABLE_FULL_DUPLEX_SELECTION;
1911 tp->scb.Parm[1] = 0; /* is ignored but should be zero */
1912 tp->scb.CMD = MODIFY_OPEN_PARMS;
1914 else
1916 if(tp->CMDqueue & OC_SET_FUNCT_ADDR)
1918 tp->CMDqueue ^= OC_SET_FUNCT_ADDR;
1919 tp->scb.Parm[0] = LOWORD(tp->ocpl.FunctAddr);
1920 tp->scb.Parm[1] = HIWORD(tp->ocpl.FunctAddr);
1921 tp->scb.CMD = SET_FUNCT_ADDR;
1923 else
1925 if(tp->CMDqueue & OC_SET_GROUP_ADDR)
1927 tp->CMDqueue ^= OC_SET_GROUP_ADDR;
1928 tp->scb.Parm[0] = LOWORD(tp->ocpl.GroupAddr);
1929 tp->scb.Parm[1] = HIWORD(tp->ocpl.GroupAddr);
1930 tp->scb.CMD = SET_GROUP_ADDR;
1932 else
1934 if(tp->CMDqueue & OC_READ_ERROR_LOG)
1936 tp->CMDqueue ^= OC_READ_ERROR_LOG;
1937 Addr = htonl(virt_to_bus(&tp->errorlogtable));
1938 tp->scb.Parm[0] = LOWORD(Addr);
1939 tp->scb.Parm[1] = HIWORD(Addr);
1940 tp->scb.CMD = READ_ERROR_LOG;
1942 else
1944 printk(KERN_WARNING "CheckForOutstandingCommand: unknown Command\n");
1945 tp->CMDqueue = 0;
1946 return;
1957 tp->ScbInUse = 1; /* Set semaphore: SCB in use. */
1959 /* Execute SCB and generate IRQ when done. */
1960 sktr_exec_sifcmd(dev, CMD_EXECUTE | CMD_SCB_REQUEST);
1962 return;
1966 * IRQ conditions: signal loss on the ring, transmit or receive of beacon
1967 * frames (disabled if bit 1 of OPEN option is set); report error MAC
1968 * frame transmit (disabled if bit 2 of OPEN option is set); open or short
1969 * cirquit fault on the lobe is detected; remove MAC frame received;
1970 * error counter overflow (255); opened adapter is the only station in ring.
1971 * After some of the IRQs the adapter is closed!
1973 static void sktr_ring_status_irq(struct net_device *dev)
1975 struct net_local *tp = (struct net_local *)dev->priv;
1977 tp->CurrentRingStatus = SWAPB(tp->ssb.Parm[0]);
1979 /* First: fill up statistics */
1980 if(tp->ssb.Parm[0] & SIGNAL_LOSS)
1982 printk(KERN_INFO "%s: Signal Loss\n", dev->name);
1983 tp->MacStat.line_errors++;
1986 /* Adapter is closed, but initialized */
1987 if(tp->ssb.Parm[0] & LOBE_WIRE_FAULT)
1989 printk(KERN_INFO "%s: Lobe Wire Fault, Reopen Adapter\n",
1990 dev->name);
1991 tp->MacStat.line_errors++;
1994 if(tp->ssb.Parm[0] & RING_RECOVERY)
1995 printk(KERN_INFO "%s: Ring Recovery\n", dev->name);
1997 /* Counter overflow: read error log */
1998 if(tp->ssb.Parm[0] & COUNTER_OVERFLOW)
2000 printk(KERN_INFO "%s: Counter Overflow\n", dev->name);
2001 sktr_exec_cmd(dev, OC_READ_ERROR_LOG);
2004 /* Adapter is closed, but initialized */
2005 if(tp->ssb.Parm[0] & REMOVE_RECEIVED)
2006 printk(KERN_INFO "%s: Remove Received, Reopen Adapter\n",
2007 dev->name);
2009 /* Adapter is closed, but initialized */
2010 if(tp->ssb.Parm[0] & AUTO_REMOVAL_ERROR)
2011 printk(KERN_INFO "%s: Auto Removal Error, Reopen Adapter\n",
2012 dev->name);
2014 if(tp->ssb.Parm[0] & HARD_ERROR)
2015 printk(KERN_INFO "%s: Hard Error\n", dev->name);
2017 if(tp->ssb.Parm[0] & SOFT_ERROR)
2018 printk(KERN_INFO "%s: Soft Error\n", dev->name);
2020 if(tp->ssb.Parm[0] & TRANSMIT_BEACON)
2021 printk(KERN_INFO "%s: Transmit Beacon\n", dev->name);
2023 if(tp->ssb.Parm[0] & SINGLE_STATION)
2024 printk(KERN_INFO "%s: Single Station\n", dev->name);
2026 /* Check if adapter has been closed */
2027 if(tp->ssb.Parm[0] & ADAPTER_CLOSED)
2029 printk(KERN_INFO "%s: Adapter closed (Reopening),"
2030 "QueueSkb %d, CurrentRingStat %x\n",
2031 dev->name, tp->QueueSkb, tp->CurrentRingStatus);
2032 tp->AdapterOpenFlag = 0;
2033 sktr_open_adapter(dev);
2036 return;
2040 * Issued if adapter has encountered an unrecoverable hardware
2041 * or software error.
2043 static void sktr_chk_irq(struct net_device *dev)
2045 int i;
2046 unsigned short AdapterCheckBlock[4];
2047 unsigned short ioaddr = dev->base_addr;
2048 struct net_local *tp = (struct net_local *)dev->priv;
2050 tp->AdapterOpenFlag = 0; /* Adapter closed now */
2052 /* Page number of adapter memory */
2053 outw(0x0001, ioaddr + SIFADX);
2054 /* Address offset */
2055 outw(CHECKADDR, ioaddr + SIFADR);
2057 /* Reading 8 byte adapter check block. */
2058 for(i = 0; i < 4; i++)
2059 AdapterCheckBlock[i] = inw(ioaddr + SIFINC);
2061 if(sktr_debug > 3)
2063 printk("%s: AdapterCheckBlock: ", dev->name);
2064 for (i = 0; i < 4; i++)
2065 printk("%04X", AdapterCheckBlock[i]);
2066 printk("\n");
2069 switch(AdapterCheckBlock[0])
2071 case DIO_PARITY:
2072 printk(KERN_INFO "%s: DIO parity error\n", dev->name);
2073 break;
2075 case DMA_READ_ABORT:
2076 printk(KERN_INFO "%s DMA read operation aborted:\n",
2077 dev->name);
2078 switch (AdapterCheckBlock[1])
2080 case 0:
2081 printk(KERN_INFO "Timeout\n");
2082 printk(KERN_INFO "Address: %04X %04X\n",
2083 AdapterCheckBlock[2],
2084 AdapterCheckBlock[3]);
2085 break;
2087 case 1:
2088 printk(KERN_INFO "Parity error\n");
2089 printk(KERN_INFO "Address: %04X %04X\n",
2090 AdapterCheckBlock[2],
2091 AdapterCheckBlock[3]);
2092 break;
2094 case 2:
2095 printk(KERN_INFO "Bus error\n");
2096 printk(KERN_INFO "Address: %04X %04X\n",
2097 AdapterCheckBlock[2],
2098 AdapterCheckBlock[3]);
2099 break;
2101 default:
2102 printk(KERN_INFO "Unknown error.\n");
2103 break;
2105 break;
2107 case DMA_WRITE_ABORT:
2108 printk(KERN_INFO "%s: DMA write operation aborted: \n",
2109 dev->name);
2110 switch (AdapterCheckBlock[1])
2112 case 0:
2113 printk(KERN_INFO "Timeout\n");
2114 printk(KERN_INFO "Address: %04X %04X\n",
2115 AdapterCheckBlock[2],
2116 AdapterCheckBlock[3]);
2117 break;
2119 case 1:
2120 printk(KERN_INFO "Parity error\n");
2121 printk(KERN_INFO "Address: %04X %04X\n",
2122 AdapterCheckBlock[2],
2123 AdapterCheckBlock[3]);
2124 break;
2126 case 2:
2127 printk(KERN_INFO "Bus error\n");
2128 printk(KERN_INFO "Address: %04X %04X\n",
2129 AdapterCheckBlock[2],
2130 AdapterCheckBlock[3]);
2131 break;
2133 default:
2134 printk(KERN_INFO "Unknown error.\n");
2135 break;
2137 break;
2139 case ILLEGAL_OP_CODE:
2140 printk("%s: Illegal operation code in firmware\n",
2141 dev->name);
2142 /* Parm[0-3]: adapter internal register R13-R15 */
2143 break;
2145 case PARITY_ERRORS:
2146 printk("%s: Adapter internal bus parity error\n",
2147 dev->name);
2148 /* Parm[0-3]: adapter internal register R13-R15 */
2149 break;
2151 case RAM_DATA_ERROR:
2152 printk("%s: RAM data error\n", dev->name);
2153 /* Parm[0-1]: MSW/LSW address of RAM location. */
2154 break;
2156 case RAM_PARITY_ERROR:
2157 printk("%s: RAM parity error\n", dev->name);
2158 /* Parm[0-1]: MSW/LSW address of RAM location. */
2159 break;
2161 case RING_UNDERRUN:
2162 printk("%s: Internal DMA underrun detected\n",
2163 dev->name);
2164 break;
2166 case INVALID_IRQ:
2167 printk("%s: Unrecognized interrupt detected\n",
2168 dev->name);
2169 /* Parm[0-3]: adapter internal register R13-R15 */
2170 break;
2172 case INVALID_ERROR_IRQ:
2173 printk("%s: Unrecognized error interrupt detected\n",
2174 dev->name);
2175 /* Parm[0-3]: adapter internal register R13-R15 */
2176 break;
2178 case INVALID_XOP:
2179 printk("%s: Unrecognized XOP request detected\n",
2180 dev->name);
2181 /* Parm[0-3]: adapter internal register R13-R15 */
2182 break;
2184 default:
2185 printk("%s: Unknown status", dev->name);
2186 break;
2189 if(sktr_chipset_init(dev) == 1)
2191 /* Restart of firmware successful */
2192 tp->AdapterOpenFlag = 1;
2195 return;
2199 * Internal adapter pointer to RAM data are copied from adapter into
2200 * host system.
2202 static void sktr_read_ptr(struct net_device *dev)
2204 struct net_local *tp = (struct net_local *)dev->priv;
2205 unsigned short adapterram;
2207 sktr_read_ram(dev, (unsigned char *)&tp->intptrs.BurnedInAddrPtr,
2208 ADAPTER_INT_PTRS, 16);
2209 sktr_read_ram(dev, (unsigned char *)&adapterram,
2210 (unsigned short)SWAPB(tp->intptrs.AdapterRAMPtr), 2);
2212 printk(KERN_INFO "%s: Adapter RAM size: %d K\n",
2213 dev->name, SWAPB(adapterram));
2215 return;
2219 * Reads a number of bytes from adapter to system memory.
2221 static void sktr_read_ram(struct net_device *dev, unsigned char *Data,
2222 unsigned short Address, int Length)
2224 int i;
2225 unsigned short old_sifadx, old_sifadr, InWord;
2226 unsigned short ioaddr = dev->base_addr;
2228 /* Save the current values */
2229 old_sifadx = inw(ioaddr + SIFADX);
2230 old_sifadr = inw(ioaddr + SIFADR);
2232 /* Page number of adapter memory */
2233 outw(0x0001, ioaddr + SIFADX);
2234 /* Address offset in adapter RAM */
2235 outw(Address, ioaddr + SIFADR);
2237 /* Copy len byte from adapter memory to system data area. */
2238 i = 0;
2239 for(;;)
2241 InWord = inw(ioaddr + SIFINC);
2243 *(Data + i) = HIBYTE(InWord); /* Write first byte */
2244 if(++i == Length) /* All is done break */
2245 break;
2247 *(Data + i) = LOBYTE(InWord); /* Write second byte */
2248 if (++i == Length) /* All is done break */
2249 break;
2252 /* Restore original values */
2253 outw(old_sifadx, ioaddr + SIFADX);
2254 outw(old_sifadr, ioaddr + SIFADR);
2256 return;
2260 * Reads MAC address from adapter ROM.
2262 static void sktr_read_addr(struct net_device *dev, unsigned char *Address)
2264 int i, In;
2265 unsigned short ioaddr = dev->base_addr;
2267 /* Address: 0000:0000 */
2268 outw(0, ioaddr + SIFADX);
2269 outw(0, ioaddr + SIFADR);
2271 /* Read six byte MAC address data */
2272 for(i = 0; i < 6; i++)
2274 In = inw(ioaddr + SIFINC);
2275 *(Address + i) = (unsigned char)(In >> 8);
2278 return;
2282 * Cancel all queued packets in the transmission queue.
2284 static void sktr_cancel_tx_queue(struct net_local* tp)
2286 TPL *tpl;
2287 struct sk_buff *skb;
2290 * NOTE: There must not be an active TRANSMIT command pending, when
2291 * this function is called.
2293 if(tp->TransmitCommandActive)
2294 return;
2296 for(;;)
2298 tpl = tp->TplBusy;
2299 if(!tpl->BusyFlag)
2300 break;
2301 /* "Remove" TPL from busy list. */
2302 tp->TplBusy = tpl->NextTPLPtr;
2303 sktr_write_tpl_status(tpl, 0); /* Clear VALID bit */
2304 tpl->BusyFlag = 0; /* "free" TPL */
2306 printk(KERN_INFO "Cancel tx (%08lXh).\n", (unsigned long)tpl);
2308 dev_kfree_skb(tpl->Skb);
2311 for(;;)
2313 skb = skb_dequeue(&tp->SendSkbQueue);
2314 if(skb == NULL)
2315 break;
2316 tp->QueueSkb++;
2317 dev_kfree_skb(skb);
2320 return;
2324 * This function is called whenever a transmit interrupt is generated by the
2325 * adapter. For a command complete interrupt, it is checked if we have to
2326 * issue a new transmit command or not.
2328 static void sktr_tx_status_irq(struct net_device *dev)
2330 struct net_local *tp = (struct net_local *)dev->priv;
2331 unsigned char HighByte, HighAc, LowAc;
2332 TPL *tpl;
2334 /* NOTE: At this point the SSB from TRANSMIT STATUS is no longer
2335 * available, because the CLEAR SSB command has already been issued.
2337 * Process all complete transmissions.
2340 for(;;)
2342 tpl = tp->TplBusy;
2343 if(!tpl->BusyFlag || (tpl->Status
2344 & (TX_VALID | TX_FRAME_COMPLETE))
2345 != TX_FRAME_COMPLETE)
2347 break;
2350 /* "Remove" TPL from busy list. */
2351 tp->TplBusy = tpl->NextTPLPtr ;
2353 if(sktr_debug > 3)
2354 sktr_dump(tpl->MData, SWAPB(tpl->FrameSize));
2356 /* Check the transmit status field only for directed frames*/
2357 if(DIRECTED_FRAME(tpl) && (tpl->Status & TX_ERROR) == 0)
2359 HighByte = GET_TRANSMIT_STATUS_HIGH_BYTE(tpl->Status);
2360 HighAc = GET_FRAME_STATUS_HIGH_AC(HighByte);
2361 LowAc = GET_FRAME_STATUS_LOW_AC(HighByte);
2363 if((HighAc != LowAc) || (HighAc == AC_NOT_RECOGNIZED))
2365 printk(KERN_INFO "%s: (DA=%08lX not recognized)",
2366 dev->name,
2367 *(unsigned long *)&tpl->MData[2+2]);
2369 else
2371 if(sktr_debug > 3)
2372 printk("%s: Directed frame tx'd\n",
2373 dev->name);
2376 else
2378 if(!DIRECTED_FRAME(tpl))
2380 if(sktr_debug > 3)
2381 printk("%s: Broadcast frame tx'd\n",
2382 dev->name);
2386 tp->MacStat.tx_packets++;
2387 dev_kfree_skb(tpl->Skb);
2388 tpl->BusyFlag = 0; /* "free" TPL */
2391 dev->tbusy = 0;
2392 if(tp->QueueSkb < MAX_TX_QUEUE)
2393 sktr_hardware_send_packet(dev, tp);
2395 return;
2399 * Called if a frame receive interrupt is generated by the adapter.
2400 * Check if the frame is valid and indicate it to system.
2402 static void sktr_rcv_status_irq(struct net_device *dev)
2404 struct net_local *tp = (struct net_local *)dev->priv;
2405 unsigned char *ReceiveDataPtr;
2406 struct sk_buff *skb;
2407 unsigned int Length, Length2;
2408 RPL *rpl;
2409 RPL *SaveHead;
2411 /* NOTE: At this point the SSB from RECEIVE STATUS is no longer
2412 * available, because the CLEAR SSB command has already been issued.
2414 * Process all complete receives.
2417 for(;;)
2419 rpl = tp->RplHead;
2420 if(rpl->Status & RX_VALID)
2421 break; /* RPL still in use by adapter */
2423 /* Forward RPLHead pointer to next list. */
2424 SaveHead = tp->RplHead;
2425 tp->RplHead = rpl->NextRPLPtr;
2427 /* Get the frame size (Byte swap for Intel).
2428 * Do this early (see workaround comment below)
2430 Length = (unsigned short)SWAPB(rpl->FrameSize);
2432 /* Check if the Frame_Start, Frame_End and
2433 * Frame_Complete bits are set.
2435 if((rpl->Status & VALID_SINGLE_BUFFER_FRAME)
2436 == VALID_SINGLE_BUFFER_FRAME)
2438 ReceiveDataPtr = rpl->MData;
2440 /* Workaround for delayed write of FrameSize on ISA
2441 * (FrameSize is false but valid-bit is reset)
2442 * Frame size is set to zero when the RPL is freed.
2443 * Length2 is there because there have also been
2444 * cases where the FrameSize was partially written
2446 Length2 = (unsigned short)SWAPB(rpl->FrameSize);
2448 if(Length == 0 || Length != Length2)
2450 tp->RplHead = SaveHead;
2451 break; /* Return to sktr_interrupt */
2454 /* Drop frames sent by myself */
2455 if(sktr_chk_frame(dev, rpl->MData))
2457 if(rpl->Skb != NULL)
2458 dev_kfree_skb(rpl->Skb);
2460 else
2462 sktr_update_rcv_stats(tp,ReceiveDataPtr,Length);
2464 if(sktr_debug > 3)
2465 printk("%s: Packet Length %04X (%d)\n",
2466 dev->name, Length, Length);
2468 /* Indicate the received frame to system.
2469 * The source routing padding is no more
2470 * necessary with 2.2.x kernel.
2471 * See: OpenOptions in sktr_init_opb()
2473 skb = rpl->Skb;
2474 if(rpl->SkbStat == SKB_UNAVAILABLE)
2476 /* Try again to allocate skb */
2477 skb = dev_alloc_skb(tp->MaxPacketSize);
2478 if(skb == NULL)
2480 /* Update Stats ?? */
2482 else
2484 skb->dev = dev;
2485 skb_put(skb, tp->MaxPacketSize);
2486 rpl->SkbStat = SKB_DATA_COPY;
2487 ReceiveDataPtr = rpl->MData;
2491 if(rpl->SkbStat == SKB_DATA_COPY
2492 || rpl->SkbStat == SKB_DMA_DIRECT)
2494 if(rpl->SkbStat == SKB_DATA_COPY)
2496 memmove(skb->data, ReceiveDataPtr, Length);
2499 /* Deliver frame to system */
2500 rpl->Skb = NULL;
2501 skb_trim(skb,Length);
2502 skb->dev = dev;
2503 skb->protocol = tr_type_trans(skb,dev);
2504 netif_rx(skb);
2508 else /* Invalid frame */
2510 if(rpl->Skb != NULL)
2511 dev_kfree_skb(rpl->Skb);
2513 /* Skip list. */
2514 if(rpl->Status & RX_START_FRAME)
2515 /* Frame start bit is set -> overflow. */
2516 tp->MacStat.rx_errors++;
2519 /* Allocate new skb for rpl */
2520 rpl->Skb = dev_alloc_skb(tp->MaxPacketSize);
2522 /* skb == NULL ? then use local buffer */
2523 if(rpl->Skb == NULL)
2525 rpl->SkbStat = SKB_UNAVAILABLE;
2526 rpl->FragList[0].DataAddr = htonl(virt_to_bus(tp->LocalRxBuffers[rpl->RPLIndex]));
2527 rpl->MData = tp->LocalRxBuffers[rpl->RPLIndex];
2529 else /* skb != NULL */
2531 rpl->Skb->dev = dev;
2532 skb_put(rpl->Skb, tp->MaxPacketSize);
2534 /* Data unreachable for DMA ? then use local buffer */
2535 if(tp->DeviceType == SK_ISA &&
2536 virt_to_bus(rpl->Skb->data) + tp->MaxPacketSize
2537 > ISA_MAX_ADDRESS)
2539 rpl->SkbStat = SKB_DATA_COPY;
2540 rpl->FragList[0].DataAddr = htonl(virt_to_bus(tp->LocalRxBuffers[rpl->RPLIndex]));
2541 rpl->MData = tp->LocalRxBuffers[rpl->RPLIndex];
2543 else
2545 /* DMA directly in skb->data */
2546 rpl->SkbStat = SKB_DMA_DIRECT;
2547 rpl->FragList[0].DataAddr = htonl(virt_to_bus(rpl->Skb->data));
2548 rpl->MData = rpl->Skb->data;
2552 rpl->FragList[0].DataCount = SWAPB(tp->MaxPacketSize);
2553 rpl->FrameSize = 0;
2555 /* Pass the last RPL back to the adapter */
2556 tp->RplTail->FrameSize = 0;
2558 /* Reset the CSTAT field in the list. */
2559 sktr_write_rpl_status(tp->RplTail, RX_VALID | RX_FRAME_IRQ);
2561 /* Current RPL becomes last one in list. */
2562 tp->RplTail = tp->RplTail->NextRPLPtr;
2564 /* Inform adapter about RPL valid. */
2565 sktr_exec_sifcmd(dev, CMD_RX_VALID);
2568 return;
2572 * This function should be used whenever the status of any RPL must be
2573 * modified by the driver, because the compiler may otherwise change the
2574 * order of instructions such that writing the RPL status may be executed
2575 * at an undesireable time. When this function is used, the status is
2576 * always written when the function is called.
2578 static void sktr_write_rpl_status(RPL *rpl, unsigned int Status)
2580 rpl->Status = Status;
2582 return;
2586 * The function updates the statistic counters in mac->MacStat.
2587 * It differtiates between directed and broadcast/multicast ( ==functional)
2588 * frames.
2590 static void sktr_update_rcv_stats(struct net_local *tp, unsigned char DataPtr[],
2591 unsigned int Length)
2593 tp->MacStat.rx_packets++;
2595 /* Test functional bit */
2596 if(DataPtr[2] & GROUP_BIT)
2597 tp->MacStat.multicast++;
2599 return;
2603 * Check if it is a frame of myself. Compare source address with my current
2604 * address in reverse direction, and mask out the TR_RII.
2606 static unsigned char sktr_chk_frame(struct net_device *dev, unsigned char *Addr)
2608 int i;
2610 for(i = 5; i > 0; i--)
2612 if(Addr[8 + i] != dev->dev_addr[i])
2613 return (0);
2616 /* Mask out RIF bit. */
2617 if((Addr[8] & ~TR_RII) != (unsigned char)(dev->dev_addr[0]))
2618 return (0);
2620 return (1); /* It is my frame. */
2624 * Dump Packet (data)
2626 static void sktr_dump(unsigned char *Data, int length)
2628 int i, j;
2630 for (i = 0, j = 0; i < length / 8; i++, j += 8)
2632 printk(KERN_DEBUG "%02x %02x %02x %02x %02x %02x %02x %02x\n",
2633 Data[j+0],Data[j+1],Data[j+2],Data[j+3],
2634 Data[j+4],Data[j+5],Data[j+6],Data[j+7]);
2637 return;
2640 #ifdef MODULE
2642 static struct net_device* dev_sktr[SKTR_MAX_ADAPTERS];
2643 static int io[SKTR_MAX_ADAPTERS] = { 0, 0 };
2644 static int irq[SKTR_MAX_ADAPTERS] = { 0, 0 };
2645 static int mem[SKTR_MAX_ADAPTERS] = { 0, 0 };
2647 MODULE_PARM(io, "1-" __MODULE_STRING(SKTR_MAX_ADAPTERS) "i");
2648 MODULE_PARM(irq, "1-" __MODULE_STRING(SKTR_MAX_ADAPTERS) "i");
2649 MODULE_PARM(mem, "1-" __MODULE_STRING(SKTR_MAX_ADAPTERS) "i");
2651 int init_module(void)
2653 int i;
2655 for(i = 0; i < SKTR_MAX_ADAPTERS; i++)
2657 irq[i] = 0;
2658 mem[i] = 0;
2659 dev_sktr[i] = NULL;
2660 dev_sktr[i] = init_trdev(dev_sktr[i], 0);
2661 if(dev_sktr[i] == NULL)
2662 return (-ENOMEM);
2664 dev_sktr[i]->base_addr = io[i];
2665 dev_sktr[i]->irq = irq[i];
2666 dev_sktr[i]->mem_start = mem[i];
2667 dev_sktr[i]->init = &sktr_probe;
2669 if(register_trdev(dev_sktr[i]) != 0)
2671 kfree_s(dev_sktr[i], sizeof(struct net_device));
2672 dev_sktr[i] = NULL;
2673 if(i == 0)
2675 printk("sktr: register_trdev() returned non-zero.\n");
2676 return (-EIO);
2678 else
2679 return (0);
2683 return (0);
2686 void cleanup_module(void)
2688 int i;
2690 for(i = 0; i < SKTR_MAX_ADAPTERS; i++)
2692 if(dev_sktr[i])
2694 unregister_trdev(dev_sktr[i]);
2695 release_region(dev_sktr[i]->base_addr, SKTR_IO_EXTENT);
2696 if(dev_sktr[i]->irq)
2697 free_irq(dev_sktr[i]->irq, dev_sktr[i]);
2698 if(dev_sktr[i]->dma > 0)
2699 free_dma(dev_sktr[i]->dma);
2700 if(dev_sktr[i]->priv)
2701 kfree_s(dev_sktr[i]->priv, sizeof(struct net_local));
2702 kfree_s(dev_sktr[i], sizeof(struct net_device));
2703 dev_sktr[i] = NULL;
2707 #endif /* MODULE */