perf_counter tools: Uniform help printouts
[linux-2.6/verdex.git] / drivers / net / tokenring / tms380tr.c
blobb11bb72dc7abae085326c605351cd92f52c6f70d
1 /*
2 * tms380tr.c: A network driver library for Texas Instruments TMS380-based
3 * Token Ring Adapters.
5 * Originally sktr.c: Written 1997 by Christoph Goos
7 * A fine result of the Linux Systems Network Architecture Project.
8 * http://www.linux-sna.org
10 * This software may be used and distributed according to the terms
11 * of the GNU General Public License, incorporated herein by reference.
13 * The following modules are currently available for card support:
14 * - tmspci (Generic PCI card support)
15 * - abyss (Madge PCI support)
16 * - tmsisa (SysKonnect TR4/16 ISA)
18 * Sources:
19 * - The hardware related parts of this driver are take from
20 * the SysKonnect Token Ring driver for Windows NT.
21 * - I used the IBM Token Ring driver 'ibmtr.c' as a base for this
22 * driver, as well as the 'skeleton.c' driver by Donald Becker.
23 * - Also various other drivers in the linux source tree were taken
24 * as samples for some tasks.
25 * - TI TMS380 Second-Generation Token Ring User's Guide
26 * - TI datasheets for respective chips
27 * - David Hein at Texas Instruments
28 * - Various Madge employees
30 * Maintainer(s):
31 * JS Jay Schulist jschlst@samba.org
32 * CG Christoph Goos cgoos@syskonnect.de
33 * AF Adam Fritzler
34 * MLP Mike Phillips phillim@amtrak.com
35 * JF Jochen Friedrich jochen@scram.de
37 * Modification History:
38 * 29-Aug-97 CG Created
39 * 04-Apr-98 CG Fixed problems caused by tok_timer_check
40 * 10-Apr-98 CG Fixed lockups at cable disconnection
41 * 27-May-98 JS Formated to Linux Kernel Format
42 * 31-May-98 JS Hacked in PCI support
43 * 16-Jun-98 JS Modulized for multiple cards with one driver
44 * Sep-99 AF Renamed to tms380tr (supports more than SK's)
45 * 23-Sep-99 AF Added Compaq and Thomas-Conrad PCI support
46 * Fixed a bug causing double copies on PCI
47 * Fixed for new multicast stuff (2.2/2.3)
48 * 25-Sep-99 AF Uped TPL_NUM from 3 to 9
49 * Removed extraneous 'No free TPL'
50 * 22-Dec-99 AF Added Madge PCI Mk2 support and generalized
51 * parts of the initilization procedure.
52 * 30-Dec-99 AF Turned tms380tr into a library ala 8390.
53 * Madge support is provided in the abyss module
54 * Generic PCI support is in the tmspci module.
55 * 30-Nov-00 JF Updated PCI code to support IO MMU via
56 * pci_map_static(). Alpha uses this MMU for ISA
57 * as well.
58 * 14-Jan-01 JF Fix DMA on ifdown/ifup sequences. Some
59 * cleanup.
60 * 13-Jan-02 JF Add spinlock to fix race condition.
61 * 09-Nov-02 JF Fixed printks to not SPAM the console during
62 * normal operation.
63 * 30-Dec-02 JF Removed incorrect __init from
64 * tms380tr_init_card.
65 * 22-Jul-05 JF Converted to dma-mapping.
67 * To do:
68 * 1. Multi/Broadcast packet handling (this may have fixed itself)
69 * 2. Write a sktrisa module that includes the old ISA support (done)
70 * 3. Allow modules to load their own microcode
71 * 4. Speed up the BUD process -- freezing the kernel for 3+sec is
72 * quite unacceptable.
73 * 5. Still a few remaining stalls when the cable is unplugged.
76 #ifdef MODULE
77 static const char version[] = "tms380tr.c: v1.10 30/12/2002 by Christoph Goos, Adam Fritzler\n";
78 #endif
80 #include <linux/module.h>
81 #include <linux/kernel.h>
82 #include <linux/types.h>
83 #include <linux/fcntl.h>
84 #include <linux/interrupt.h>
85 #include <linux/ptrace.h>
86 #include <linux/ioport.h>
87 #include <linux/in.h>
88 #include <linux/slab.h>
89 #include <linux/string.h>
90 #include <linux/time.h>
91 #include <linux/errno.h>
92 #include <linux/init.h>
93 #include <linux/dma-mapping.h>
94 #include <linux/delay.h>
95 #include <linux/netdevice.h>
96 #include <linux/etherdevice.h>
97 #include <linux/skbuff.h>
98 #include <linux/trdevice.h>
99 #include <linux/firmware.h>
100 #include <linux/bitops.h>
102 #include <asm/system.h>
103 #include <asm/io.h>
104 #include <asm/dma.h>
105 #include <asm/irq.h>
106 #include <asm/uaccess.h>
108 #include "tms380tr.h" /* Our Stuff */
110 /* Use 0 for production, 1 for verification, 2 for debug, and
111 * 3 for very verbose debug.
113 #ifndef TMS380TR_DEBUG
114 #define TMS380TR_DEBUG 0
115 #endif
116 static unsigned int tms380tr_debug = TMS380TR_DEBUG;
118 /* Index to functions, as function prototypes.
119 * Alphabetical by function name.
122 /* "A" */
123 /* "B" */
124 static int tms380tr_bringup_diags(struct net_device *dev);
125 /* "C" */
126 static void tms380tr_cancel_tx_queue(struct net_local* tp);
127 static int tms380tr_chipset_init(struct net_device *dev);
128 static void tms380tr_chk_irq(struct net_device *dev);
129 static void tms380tr_chk_outstanding_cmds(struct net_device *dev);
130 static void tms380tr_chk_src_addr(unsigned char *frame, unsigned char *hw_addr);
131 static unsigned char tms380tr_chk_ssb(struct net_local *tp, unsigned short IrqType);
132 int tms380tr_close(struct net_device *dev);
133 static void tms380tr_cmd_status_irq(struct net_device *dev);
134 /* "D" */
135 static void tms380tr_disable_interrupts(struct net_device *dev);
136 #if TMS380TR_DEBUG > 0
137 static void tms380tr_dump(unsigned char *Data, int length);
138 #endif
139 /* "E" */
140 static void tms380tr_enable_interrupts(struct net_device *dev);
141 static void tms380tr_exec_cmd(struct net_device *dev, unsigned short Command);
142 static void tms380tr_exec_sifcmd(struct net_device *dev, unsigned int WriteValue);
143 /* "F" */
144 /* "G" */
145 static struct net_device_stats *tms380tr_get_stats(struct net_device *dev);
146 /* "H" */
147 static int tms380tr_hardware_send_packet(struct sk_buff *skb,
148 struct net_device *dev);
149 /* "I" */
150 static int tms380tr_init_adapter(struct net_device *dev);
151 static void tms380tr_init_ipb(struct net_local *tp);
152 static void tms380tr_init_net_local(struct net_device *dev);
153 static void tms380tr_init_opb(struct net_device *dev);
154 /* "M" */
155 /* "O" */
156 int tms380tr_open(struct net_device *dev);
157 static void tms380tr_open_adapter(struct net_device *dev);
158 /* "P" */
159 /* "R" */
160 static void tms380tr_rcv_status_irq(struct net_device *dev);
161 static int tms380tr_read_ptr(struct net_device *dev);
162 static void tms380tr_read_ram(struct net_device *dev, unsigned char *Data,
163 unsigned short Address, int Length);
164 static int tms380tr_reset_adapter(struct net_device *dev);
165 static void tms380tr_reset_interrupt(struct net_device *dev);
166 static void tms380tr_ring_status_irq(struct net_device *dev);
167 /* "S" */
168 static int tms380tr_send_packet(struct sk_buff *skb, struct net_device *dev);
169 static void tms380tr_set_multicast_list(struct net_device *dev);
170 static int tms380tr_set_mac_address(struct net_device *dev, void *addr);
171 /* "T" */
172 static void tms380tr_timer_chk(unsigned long data);
173 static void tms380tr_timer_end_wait(unsigned long data);
174 static void tms380tr_tx_status_irq(struct net_device *dev);
175 /* "U" */
176 static void tms380tr_update_rcv_stats(struct net_local *tp,
177 unsigned char DataPtr[], unsigned int Length);
178 /* "W" */
179 void tms380tr_wait(unsigned long time);
180 static void tms380tr_write_rpl_status(RPL *rpl, unsigned int Status);
181 static void tms380tr_write_tpl_status(TPL *tpl, unsigned int Status);
183 #define SIFREADB(reg) \
184 (((struct net_local *)netdev_priv(dev))->sifreadb(dev, reg))
185 #define SIFWRITEB(val, reg) \
186 (((struct net_local *)netdev_priv(dev))->sifwriteb(dev, val, reg))
187 #define SIFREADW(reg) \
188 (((struct net_local *)netdev_priv(dev))->sifreadw(dev, reg))
189 #define SIFWRITEW(val, reg) \
190 (((struct net_local *)netdev_priv(dev))->sifwritew(dev, val, reg))
194 #if 0 /* TMS380TR_DEBUG > 0 */
195 static int madgemc_sifprobe(struct net_device *dev)
197 unsigned char old, chk1, chk2;
199 old = SIFREADB(SIFADR); /* Get the old SIFADR value */
201 chk1 = 0; /* Begin with check value 0 */
202 do {
203 madgemc_setregpage(dev, 0);
204 /* Write new SIFADR value */
205 SIFWRITEB(chk1, SIFADR);
206 chk2 = SIFREADB(SIFADR);
207 if (chk2 != chk1)
208 return -1;
210 madgemc_setregpage(dev, 1);
211 /* Read, invert and write */
212 chk2 = SIFREADB(SIFADD);
213 if (chk2 != chk1)
214 return -1;
216 madgemc_setregpage(dev, 0);
217 chk2 ^= 0x0FE;
218 SIFWRITEB(chk2, SIFADR);
220 /* Read, invert and compare */
221 madgemc_setregpage(dev, 1);
222 chk2 = SIFREADB(SIFADD);
223 madgemc_setregpage(dev, 0);
224 chk2 ^= 0x0FE;
226 if(chk1 != chk2)
227 return (-1); /* No adapter */
228 chk1 -= 2;
229 } while(chk1 != 0); /* Repeat 128 times (all byte values) */
231 madgemc_setregpage(dev, 0); /* sanity */
232 /* Restore the SIFADR value */
233 SIFWRITEB(old, SIFADR);
235 return (0);
237 #endif
240 * Open/initialize the board. This is called sometime after
241 * booting when the 'ifconfig' program is run.
243 * This routine should set everything up anew at each open, even
244 * registers that "should" only need to be set once at boot, so that
245 * there is non-reboot way to recover if something goes wrong.
247 int tms380tr_open(struct net_device *dev)
249 struct net_local *tp = netdev_priv(dev);
250 int err;
252 /* init the spinlock */
253 spin_lock_init(&tp->lock);
254 init_timer(&tp->timer);
256 /* Reset the hardware here. Don't forget to set the station address. */
258 #ifdef CONFIG_ISA
259 if(dev->dma > 0)
261 unsigned long flags=claim_dma_lock();
262 disable_dma(dev->dma);
263 set_dma_mode(dev->dma, DMA_MODE_CASCADE);
264 enable_dma(dev->dma);
265 release_dma_lock(flags);
267 #endif
269 err = tms380tr_chipset_init(dev);
270 if(err)
272 printk(KERN_INFO "%s: Chipset initialization error\n",
273 dev->name);
274 return (-1);
277 tp->timer.expires = jiffies + 30*HZ;
278 tp->timer.function = tms380tr_timer_end_wait;
279 tp->timer.data = (unsigned long)dev;
280 add_timer(&tp->timer);
282 printk(KERN_DEBUG "%s: Adapter RAM size: %dK\n",
283 dev->name, tms380tr_read_ptr(dev));
285 tms380tr_enable_interrupts(dev);
286 tms380tr_open_adapter(dev);
288 netif_start_queue(dev);
290 /* Wait for interrupt from hardware. If interrupt does not come,
291 * there will be a timeout from the timer.
293 tp->Sleeping = 1;
294 interruptible_sleep_on(&tp->wait_for_tok_int);
295 del_timer(&tp->timer);
297 /* If AdapterVirtOpenFlag is 1, the adapter is now open for use */
298 if(tp->AdapterVirtOpenFlag == 0)
300 tms380tr_disable_interrupts(dev);
301 return (-1);
304 tp->StartTime = jiffies;
306 /* Start function control timer */
307 tp->timer.expires = jiffies + 2*HZ;
308 tp->timer.function = tms380tr_timer_chk;
309 tp->timer.data = (unsigned long)dev;
310 add_timer(&tp->timer);
312 return (0);
316 * Timeout function while waiting for event
318 static void tms380tr_timer_end_wait(unsigned long data)
320 struct net_device *dev = (struct net_device*)data;
321 struct net_local *tp = netdev_priv(dev);
323 if(tp->Sleeping)
325 tp->Sleeping = 0;
326 wake_up_interruptible(&tp->wait_for_tok_int);
329 return;
333 * Initialize the chipset
335 static int tms380tr_chipset_init(struct net_device *dev)
337 struct net_local *tp = netdev_priv(dev);
338 int err;
340 tms380tr_init_ipb(tp);
341 tms380tr_init_opb(dev);
342 tms380tr_init_net_local(dev);
344 if(tms380tr_debug > 3)
345 printk(KERN_DEBUG "%s: Resetting adapter...\n", dev->name);
346 err = tms380tr_reset_adapter(dev);
347 if(err < 0)
348 return (-1);
350 if(tms380tr_debug > 3)
351 printk(KERN_DEBUG "%s: Bringup diags...\n", dev->name);
352 err = tms380tr_bringup_diags(dev);
353 if(err < 0)
354 return (-1);
356 if(tms380tr_debug > 3)
357 printk(KERN_DEBUG "%s: Init adapter...\n", dev->name);
358 err = tms380tr_init_adapter(dev);
359 if(err < 0)
360 return (-1);
362 if(tms380tr_debug > 3)
363 printk(KERN_DEBUG "%s: Done!\n", dev->name);
364 return (0);
368 * Initializes the net_local structure.
370 static void tms380tr_init_net_local(struct net_device *dev)
372 struct net_local *tp = netdev_priv(dev);
373 int i;
374 dma_addr_t dmabuf;
376 tp->scb.CMD = 0;
377 tp->scb.Parm[0] = 0;
378 tp->scb.Parm[1] = 0;
380 tp->ssb.STS = 0;
381 tp->ssb.Parm[0] = 0;
382 tp->ssb.Parm[1] = 0;
383 tp->ssb.Parm[2] = 0;
385 tp->CMDqueue = 0;
387 tp->AdapterOpenFlag = 0;
388 tp->AdapterVirtOpenFlag = 0;
389 tp->ScbInUse = 0;
390 tp->OpenCommandIssued = 0;
391 tp->ReOpenInProgress = 0;
392 tp->HaltInProgress = 0;
393 tp->TransmitHaltScheduled = 0;
394 tp->LobeWireFaultLogged = 0;
395 tp->LastOpenStatus = 0;
396 tp->MaxPacketSize = DEFAULT_PACKET_SIZE;
398 /* Create circular chain of transmit lists */
399 for (i = 0; i < TPL_NUM; i++)
401 tp->Tpl[i].NextTPLAddr = htonl(((char *)(&tp->Tpl[(i+1) % TPL_NUM]) - (char *)tp) + tp->dmabuffer); /* DMA buffer may be MMU driven */
402 tp->Tpl[i].Status = 0;
403 tp->Tpl[i].FrameSize = 0;
404 tp->Tpl[i].FragList[0].DataCount = 0;
405 tp->Tpl[i].FragList[0].DataAddr = 0;
406 tp->Tpl[i].NextTPLPtr = &tp->Tpl[(i+1) % TPL_NUM];
407 tp->Tpl[i].MData = NULL;
408 tp->Tpl[i].TPLIndex = i;
409 tp->Tpl[i].DMABuff = 0;
410 tp->Tpl[i].BusyFlag = 0;
413 tp->TplFree = tp->TplBusy = &tp->Tpl[0];
415 /* Create circular chain of receive lists */
416 for (i = 0; i < RPL_NUM; i++)
418 tp->Rpl[i].NextRPLAddr = htonl(((char *)(&tp->Rpl[(i+1) % RPL_NUM]) - (char *)tp) + tp->dmabuffer); /* DMA buffer may be MMU driven */
419 tp->Rpl[i].Status = (RX_VALID | RX_START_FRAME | RX_END_FRAME | RX_FRAME_IRQ);
420 tp->Rpl[i].FrameSize = 0;
421 tp->Rpl[i].FragList[0].DataCount = cpu_to_be16((unsigned short)tp->MaxPacketSize);
423 /* Alloc skb and point adapter to data area */
424 tp->Rpl[i].Skb = dev_alloc_skb(tp->MaxPacketSize);
425 tp->Rpl[i].DMABuff = 0;
427 /* skb == NULL ? then use local buffer */
428 if(tp->Rpl[i].Skb == NULL)
430 tp->Rpl[i].SkbStat = SKB_UNAVAILABLE;
431 tp->Rpl[i].FragList[0].DataAddr = htonl(((char *)tp->LocalRxBuffers[i] - (char *)tp) + tp->dmabuffer);
432 tp->Rpl[i].MData = tp->LocalRxBuffers[i];
434 else /* SKB != NULL */
436 tp->Rpl[i].Skb->dev = dev;
437 skb_put(tp->Rpl[i].Skb, tp->MaxPacketSize);
439 /* data unreachable for DMA ? then use local buffer */
440 dmabuf = dma_map_single(tp->pdev, tp->Rpl[i].Skb->data, tp->MaxPacketSize, DMA_FROM_DEVICE);
441 if(tp->dmalimit && (dmabuf + tp->MaxPacketSize > tp->dmalimit))
443 tp->Rpl[i].SkbStat = SKB_DATA_COPY;
444 tp->Rpl[i].FragList[0].DataAddr = htonl(((char *)tp->LocalRxBuffers[i] - (char *)tp) + tp->dmabuffer);
445 tp->Rpl[i].MData = tp->LocalRxBuffers[i];
447 else /* DMA directly in skb->data */
449 tp->Rpl[i].SkbStat = SKB_DMA_DIRECT;
450 tp->Rpl[i].FragList[0].DataAddr = htonl(dmabuf);
451 tp->Rpl[i].MData = tp->Rpl[i].Skb->data;
452 tp->Rpl[i].DMABuff = dmabuf;
456 tp->Rpl[i].NextRPLPtr = &tp->Rpl[(i+1) % RPL_NUM];
457 tp->Rpl[i].RPLIndex = i;
460 tp->RplHead = &tp->Rpl[0];
461 tp->RplTail = &tp->Rpl[RPL_NUM-1];
462 tp->RplTail->Status = (RX_START_FRAME | RX_END_FRAME | RX_FRAME_IRQ);
464 return;
468 * Initializes the initialisation parameter block.
470 static void tms380tr_init_ipb(struct net_local *tp)
472 tp->ipb.Init_Options = BURST_MODE;
473 tp->ipb.CMD_Status_IV = 0;
474 tp->ipb.TX_IV = 0;
475 tp->ipb.RX_IV = 0;
476 tp->ipb.Ring_Status_IV = 0;
477 tp->ipb.SCB_Clear_IV = 0;
478 tp->ipb.Adapter_CHK_IV = 0;
479 tp->ipb.RX_Burst_Size = BURST_SIZE;
480 tp->ipb.TX_Burst_Size = BURST_SIZE;
481 tp->ipb.DMA_Abort_Thrhld = DMA_RETRIES;
482 tp->ipb.SCB_Addr = 0;
483 tp->ipb.SSB_Addr = 0;
485 return;
489 * Initializes the open parameter block.
491 static void tms380tr_init_opb(struct net_device *dev)
493 struct net_local *tp;
494 unsigned long Addr;
495 unsigned short RplSize = RPL_SIZE;
496 unsigned short TplSize = TPL_SIZE;
497 unsigned short BufferSize = BUFFER_SIZE;
498 int i;
500 tp = netdev_priv(dev);
502 tp->ocpl.OPENOptions = 0;
503 tp->ocpl.OPENOptions |= ENABLE_FULL_DUPLEX_SELECTION;
504 tp->ocpl.FullDuplex = 0;
505 tp->ocpl.FullDuplex |= OPEN_FULL_DUPLEX_OFF;
508 * Set node address
510 * We go ahead and put it in the OPB even though on
511 * most of the generic adapters this isn't required.
512 * Its simpler this way. -- ASF
514 for (i=0;i<6;i++)
515 tp->ocpl.NodeAddr[i] = ((unsigned char *)dev->dev_addr)[i];
517 tp->ocpl.GroupAddr = 0;
518 tp->ocpl.FunctAddr = 0;
519 tp->ocpl.RxListSize = cpu_to_be16((unsigned short)RplSize);
520 tp->ocpl.TxListSize = cpu_to_be16((unsigned short)TplSize);
521 tp->ocpl.BufSize = cpu_to_be16((unsigned short)BufferSize);
522 tp->ocpl.Reserved = 0;
523 tp->ocpl.TXBufMin = TX_BUF_MIN;
524 tp->ocpl.TXBufMax = TX_BUF_MAX;
526 Addr = htonl(((char *)tp->ProductID - (char *)tp) + tp->dmabuffer);
528 tp->ocpl.ProdIDAddr[0] = LOWORD(Addr);
529 tp->ocpl.ProdIDAddr[1] = HIWORD(Addr);
531 return;
535 * Send OPEN command to adapter
537 static void tms380tr_open_adapter(struct net_device *dev)
539 struct net_local *tp = netdev_priv(dev);
541 if(tp->OpenCommandIssued)
542 return;
544 tp->OpenCommandIssued = 1;
545 tms380tr_exec_cmd(dev, OC_OPEN);
547 return;
551 * Clear the adapter's interrupt flag. Clear system interrupt enable
552 * (SINTEN): disable adapter to system interrupts.
554 static void tms380tr_disable_interrupts(struct net_device *dev)
556 SIFWRITEB(0, SIFACL);
558 return;
562 * Set the adapter's interrupt flag. Set system interrupt enable
563 * (SINTEN): enable adapter to system interrupts.
565 static void tms380tr_enable_interrupts(struct net_device *dev)
567 SIFWRITEB(ACL_SINTEN, SIFACL);
569 return;
573 * Put command in command queue, try to execute it.
575 static void tms380tr_exec_cmd(struct net_device *dev, unsigned short Command)
577 struct net_local *tp = netdev_priv(dev);
579 tp->CMDqueue |= Command;
580 tms380tr_chk_outstanding_cmds(dev);
582 return;
585 static void tms380tr_timeout(struct net_device *dev)
588 * If we get here, some higher level has decided we are broken.
589 * There should really be a "kick me" function call instead.
591 * Resetting the token ring adapter takes a long time so just
592 * fake transmission time and go on trying. Our own timeout
593 * routine is in tms380tr_timer_chk()
595 dev->trans_start = jiffies;
596 netif_wake_queue(dev);
600 * Gets skb from system, queues it and checks if it can be sent
602 static int tms380tr_send_packet(struct sk_buff *skb, struct net_device *dev)
604 struct net_local *tp = netdev_priv(dev);
605 int err;
607 err = tms380tr_hardware_send_packet(skb, dev);
608 if(tp->TplFree->NextTPLPtr->BusyFlag)
609 netif_stop_queue(dev);
610 return (err);
614 * Move frames into adapter tx queue
616 static int tms380tr_hardware_send_packet(struct sk_buff *skb, struct net_device *dev)
618 TPL *tpl;
619 short length;
620 unsigned char *buf;
621 unsigned long flags;
622 int i;
623 dma_addr_t dmabuf, newbuf;
624 struct net_local *tp = netdev_priv(dev);
626 /* Try to get a free TPL from the chain.
628 * NOTE: We *must* always leave one unused TPL in the chain,
629 * because otherwise the adapter might send frames twice.
631 spin_lock_irqsave(&tp->lock, flags);
632 if(tp->TplFree->NextTPLPtr->BusyFlag) { /* No free TPL */
633 if (tms380tr_debug > 0)
634 printk(KERN_DEBUG "%s: No free TPL\n", dev->name);
635 spin_unlock_irqrestore(&tp->lock, flags);
636 return 1;
639 dmabuf = 0;
641 /* Is buffer reachable for Busmaster-DMA? */
643 length = skb->len;
644 dmabuf = dma_map_single(tp->pdev, skb->data, length, DMA_TO_DEVICE);
645 if(tp->dmalimit && (dmabuf + length > tp->dmalimit)) {
646 /* Copy frame to local buffer */
647 dma_unmap_single(tp->pdev, dmabuf, length, DMA_TO_DEVICE);
648 dmabuf = 0;
649 i = tp->TplFree->TPLIndex;
650 buf = tp->LocalTxBuffers[i];
651 skb_copy_from_linear_data(skb, buf, length);
652 newbuf = ((char *)buf - (char *)tp) + tp->dmabuffer;
654 else {
655 /* Send direct from skb->data */
656 newbuf = dmabuf;
657 buf = skb->data;
659 /* Source address in packet? */
660 tms380tr_chk_src_addr(buf, dev->dev_addr);
661 tp->LastSendTime = jiffies;
662 tpl = tp->TplFree; /* Get the "free" TPL */
663 tpl->BusyFlag = 1; /* Mark TPL as busy */
664 tp->TplFree = tpl->NextTPLPtr;
666 /* Save the skb for delayed return of skb to system */
667 tpl->Skb = skb;
668 tpl->DMABuff = dmabuf;
669 tpl->FragList[0].DataCount = cpu_to_be16((unsigned short)length);
670 tpl->FragList[0].DataAddr = htonl(newbuf);
672 /* Write the data length in the transmit list. */
673 tpl->FrameSize = cpu_to_be16((unsigned short)length);
674 tpl->MData = buf;
676 /* Transmit the frame and set the status values. */
677 tms380tr_write_tpl_status(tpl, TX_VALID | TX_START_FRAME
678 | TX_END_FRAME | TX_PASS_SRC_ADDR
679 | TX_FRAME_IRQ);
681 /* Let adapter send the frame. */
682 tms380tr_exec_sifcmd(dev, CMD_TX_VALID);
683 spin_unlock_irqrestore(&tp->lock, flags);
685 return 0;
689 * Write the given value to the 'Status' field of the specified TPL.
690 * NOTE: This function should be used whenever the status of any TPL must be
691 * modified by the driver, because the compiler may otherwise change the
692 * order of instructions such that writing the TPL status may be executed at
693 * an undesireable time. When this function is used, the status is always
694 * written when the function is called.
696 static void tms380tr_write_tpl_status(TPL *tpl, unsigned int Status)
698 tpl->Status = Status;
701 static void tms380tr_chk_src_addr(unsigned char *frame, unsigned char *hw_addr)
703 unsigned char SRBit;
705 if((((unsigned long)frame[8]) & ~0x80) != 0) /* Compare 4 bytes */
706 return;
707 if((unsigned short)frame[12] != 0) /* Compare 2 bytes */
708 return;
710 SRBit = frame[8] & 0x80;
711 memcpy(&frame[8], hw_addr, 6);
712 frame[8] |= SRBit;
714 return;
718 * The timer routine: Check if adapter still open and working, reopen if not.
720 static void tms380tr_timer_chk(unsigned long data)
722 struct net_device *dev = (struct net_device*)data;
723 struct net_local *tp = netdev_priv(dev);
725 if(tp->HaltInProgress)
726 return;
728 tms380tr_chk_outstanding_cmds(dev);
729 if(time_before(tp->LastSendTime + SEND_TIMEOUT, jiffies)
730 && (tp->TplFree != tp->TplBusy))
732 /* Anything to send, but stalled too long */
733 tp->LastSendTime = jiffies;
734 tms380tr_exec_cmd(dev, OC_CLOSE); /* Does reopen automatically */
737 tp->timer.expires = jiffies + 2*HZ;
738 add_timer(&tp->timer);
740 if(tp->AdapterOpenFlag || tp->ReOpenInProgress)
741 return;
742 tp->ReOpenInProgress = 1;
743 tms380tr_open_adapter(dev);
745 return;
749 * The typical workload of the driver: Handle the network interface interrupts.
751 irqreturn_t tms380tr_interrupt(int irq, void *dev_id)
753 struct net_device *dev = dev_id;
754 struct net_local *tp;
755 unsigned short irq_type;
756 int handled = 0;
758 tp = netdev_priv(dev);
760 irq_type = SIFREADW(SIFSTS);
762 while(irq_type & STS_SYSTEM_IRQ) {
763 handled = 1;
764 irq_type &= STS_IRQ_MASK;
766 if(!tms380tr_chk_ssb(tp, irq_type)) {
767 printk(KERN_DEBUG "%s: DATA LATE occurred\n", dev->name);
768 break;
771 switch(irq_type) {
772 case STS_IRQ_RECEIVE_STATUS:
773 tms380tr_reset_interrupt(dev);
774 tms380tr_rcv_status_irq(dev);
775 break;
777 case STS_IRQ_TRANSMIT_STATUS:
778 /* Check if TRANSMIT.HALT command is complete */
779 if(tp->ssb.Parm[0] & COMMAND_COMPLETE) {
780 tp->TransmitCommandActive = 0;
781 tp->TransmitHaltScheduled = 0;
783 /* Issue a new transmit command. */
784 tms380tr_exec_cmd(dev, OC_TRANSMIT);
787 tms380tr_reset_interrupt(dev);
788 tms380tr_tx_status_irq(dev);
789 break;
791 case STS_IRQ_COMMAND_STATUS:
792 /* The SSB contains status of last command
793 * other than receive/transmit.
795 tms380tr_cmd_status_irq(dev);
796 break;
798 case STS_IRQ_SCB_CLEAR:
799 /* The SCB is free for another command. */
800 tp->ScbInUse = 0;
801 tms380tr_chk_outstanding_cmds(dev);
802 break;
804 case STS_IRQ_RING_STATUS:
805 tms380tr_ring_status_irq(dev);
806 break;
808 case STS_IRQ_ADAPTER_CHECK:
809 tms380tr_chk_irq(dev);
810 break;
812 case STS_IRQ_LLC_STATUS:
813 printk(KERN_DEBUG "tms380tr: unexpected LLC status IRQ\n");
814 break;
816 case STS_IRQ_TIMER:
817 printk(KERN_DEBUG "tms380tr: unexpected Timer IRQ\n");
818 break;
820 case STS_IRQ_RECEIVE_PENDING:
821 printk(KERN_DEBUG "tms380tr: unexpected Receive Pending IRQ\n");
822 break;
824 default:
825 printk(KERN_DEBUG "Unknown Token Ring IRQ (0x%04x)\n", irq_type);
826 break;
829 /* Reset system interrupt if not already done. */
830 if(irq_type != STS_IRQ_TRANSMIT_STATUS
831 && irq_type != STS_IRQ_RECEIVE_STATUS) {
832 tms380tr_reset_interrupt(dev);
835 irq_type = SIFREADW(SIFSTS);
838 return IRQ_RETVAL(handled);
842 * Reset the INTERRUPT SYSTEM bit and issue SSB CLEAR command.
844 static void tms380tr_reset_interrupt(struct net_device *dev)
846 struct net_local *tp = netdev_priv(dev);
847 SSB *ssb = &tp->ssb;
850 * [Workaround for "Data Late"]
851 * Set all fields of the SSB to well-defined values so we can
852 * check if the adapter has written the SSB.
855 ssb->STS = (unsigned short) -1;
856 ssb->Parm[0] = (unsigned short) -1;
857 ssb->Parm[1] = (unsigned short) -1;
858 ssb->Parm[2] = (unsigned short) -1;
860 /* Free SSB by issuing SSB_CLEAR command after reading IRQ code
861 * and clear STS_SYSTEM_IRQ bit: enable adapter for further interrupts.
863 tms380tr_exec_sifcmd(dev, CMD_SSB_CLEAR | CMD_CLEAR_SYSTEM_IRQ);
865 return;
869 * Check if the SSB has actually been written by the adapter.
871 static unsigned char tms380tr_chk_ssb(struct net_local *tp, unsigned short IrqType)
873 SSB *ssb = &tp->ssb; /* The address of the SSB. */
875 /* C 0 1 2 INTERRUPT CODE
876 * - - - - --------------
877 * 1 1 1 1 TRANSMIT STATUS
878 * 1 1 1 1 RECEIVE STATUS
879 * 1 ? ? 0 COMMAND STATUS
880 * 0 0 0 0 SCB CLEAR
881 * 1 1 0 0 RING STATUS
882 * 0 0 0 0 ADAPTER CHECK
884 * 0 = SSB field not affected by interrupt
885 * 1 = SSB field is affected by interrupt
887 * C = SSB ADDRESS +0: COMMAND
888 * 0 = SSB ADDRESS +2: STATUS 0
889 * 1 = SSB ADDRESS +4: STATUS 1
890 * 2 = SSB ADDRESS +6: STATUS 2
893 /* Check if this interrupt does use the SSB. */
895 if(IrqType != STS_IRQ_TRANSMIT_STATUS
896 && IrqType != STS_IRQ_RECEIVE_STATUS
897 && IrqType != STS_IRQ_COMMAND_STATUS
898 && IrqType != STS_IRQ_RING_STATUS)
900 return (1); /* SSB not involved. */
903 /* Note: All fields of the SSB have been set to all ones (-1) after it
904 * has last been used by the software (see DriverIsr()).
906 * Check if the affected SSB fields are still unchanged.
909 if(ssb->STS == (unsigned short) -1)
910 return (0); /* Command field not yet available. */
911 if(IrqType == STS_IRQ_COMMAND_STATUS)
912 return (1); /* Status fields not always affected. */
913 if(ssb->Parm[0] == (unsigned short) -1)
914 return (0); /* Status 1 field not yet available. */
915 if(IrqType == STS_IRQ_RING_STATUS)
916 return (1); /* Status 2 & 3 fields not affected. */
918 /* Note: At this point, the interrupt is either TRANSMIT or RECEIVE. */
919 if(ssb->Parm[1] == (unsigned short) -1)
920 return (0); /* Status 2 field not yet available. */
921 if(ssb->Parm[2] == (unsigned short) -1)
922 return (0); /* Status 3 field not yet available. */
924 return (1); /* All SSB fields have been written by the adapter. */
928 * Evaluates the command results status in the SSB status field.
930 static void tms380tr_cmd_status_irq(struct net_device *dev)
932 struct net_local *tp = netdev_priv(dev);
933 unsigned short ssb_cmd, ssb_parm_0;
934 unsigned short ssb_parm_1;
935 char *open_err = "Open error -";
936 char *code_err = "Open code -";
938 /* Copy the ssb values to local variables */
939 ssb_cmd = tp->ssb.STS;
940 ssb_parm_0 = tp->ssb.Parm[0];
941 ssb_parm_1 = tp->ssb.Parm[1];
943 if(ssb_cmd == OPEN)
945 tp->Sleeping = 0;
946 if(!tp->ReOpenInProgress)
947 wake_up_interruptible(&tp->wait_for_tok_int);
949 tp->OpenCommandIssued = 0;
950 tp->ScbInUse = 0;
952 if((ssb_parm_0 & 0x00FF) == GOOD_COMPLETION)
954 /* Success, the adapter is open. */
955 tp->LobeWireFaultLogged = 0;
956 tp->AdapterOpenFlag = 1;
957 tp->AdapterVirtOpenFlag = 1;
958 tp->TransmitCommandActive = 0;
959 tms380tr_exec_cmd(dev, OC_TRANSMIT);
960 tms380tr_exec_cmd(dev, OC_RECEIVE);
962 if(tp->ReOpenInProgress)
963 tp->ReOpenInProgress = 0;
965 return;
967 else /* The adapter did not open. */
969 if(ssb_parm_0 & NODE_ADDR_ERROR)
970 printk(KERN_INFO "%s: Node address error\n",
971 dev->name);
972 if(ssb_parm_0 & LIST_SIZE_ERROR)
973 printk(KERN_INFO "%s: List size error\n",
974 dev->name);
975 if(ssb_parm_0 & BUF_SIZE_ERROR)
976 printk(KERN_INFO "%s: Buffer size error\n",
977 dev->name);
978 if(ssb_parm_0 & TX_BUF_COUNT_ERROR)
979 printk(KERN_INFO "%s: Tx buffer count error\n",
980 dev->name);
981 if(ssb_parm_0 & INVALID_OPEN_OPTION)
982 printk(KERN_INFO "%s: Invalid open option\n",
983 dev->name);
984 if(ssb_parm_0 & OPEN_ERROR)
986 /* Show the open phase. */
987 switch(ssb_parm_0 & OPEN_PHASES_MASK)
989 case LOBE_MEDIA_TEST:
990 if(!tp->LobeWireFaultLogged)
992 tp->LobeWireFaultLogged = 1;
993 printk(KERN_INFO "%s: %s Lobe wire fault (check cable !).\n", dev->name, open_err);
995 tp->ReOpenInProgress = 1;
996 tp->AdapterOpenFlag = 0;
997 tp->AdapterVirtOpenFlag = 1;
998 tms380tr_open_adapter(dev);
999 return;
1001 case PHYSICAL_INSERTION:
1002 printk(KERN_INFO "%s: %s Physical insertion.\n", dev->name, open_err);
1003 break;
1005 case ADDRESS_VERIFICATION:
1006 printk(KERN_INFO "%s: %s Address verification.\n", dev->name, open_err);
1007 break;
1009 case PARTICIPATION_IN_RING_POLL:
1010 printk(KERN_INFO "%s: %s Participation in ring poll.\n", dev->name, open_err);
1011 break;
1013 case REQUEST_INITIALISATION:
1014 printk(KERN_INFO "%s: %s Request initialisation.\n", dev->name, open_err);
1015 break;
1017 case FULLDUPLEX_CHECK:
1018 printk(KERN_INFO "%s: %s Full duplex check.\n", dev->name, open_err);
1019 break;
1021 default:
1022 printk(KERN_INFO "%s: %s Unknown open phase\n", dev->name, open_err);
1023 break;
1026 /* Show the open errors. */
1027 switch(ssb_parm_0 & OPEN_ERROR_CODES_MASK)
1029 case OPEN_FUNCTION_FAILURE:
1030 printk(KERN_INFO "%s: %s OPEN_FUNCTION_FAILURE", dev->name, code_err);
1031 tp->LastOpenStatus =
1032 OPEN_FUNCTION_FAILURE;
1033 break;
1035 case OPEN_SIGNAL_LOSS:
1036 printk(KERN_INFO "%s: %s OPEN_SIGNAL_LOSS\n", dev->name, code_err);
1037 tp->LastOpenStatus =
1038 OPEN_SIGNAL_LOSS;
1039 break;
1041 case OPEN_TIMEOUT:
1042 printk(KERN_INFO "%s: %s OPEN_TIMEOUT\n", dev->name, code_err);
1043 tp->LastOpenStatus =
1044 OPEN_TIMEOUT;
1045 break;
1047 case OPEN_RING_FAILURE:
1048 printk(KERN_INFO "%s: %s OPEN_RING_FAILURE\n", dev->name, code_err);
1049 tp->LastOpenStatus =
1050 OPEN_RING_FAILURE;
1051 break;
1053 case OPEN_RING_BEACONING:
1054 printk(KERN_INFO "%s: %s OPEN_RING_BEACONING\n", dev->name, code_err);
1055 tp->LastOpenStatus =
1056 OPEN_RING_BEACONING;
1057 break;
1059 case OPEN_DUPLICATE_NODEADDR:
1060 printk(KERN_INFO "%s: %s OPEN_DUPLICATE_NODEADDR\n", dev->name, code_err);
1061 tp->LastOpenStatus =
1062 OPEN_DUPLICATE_NODEADDR;
1063 break;
1065 case OPEN_REQUEST_INIT:
1066 printk(KERN_INFO "%s: %s OPEN_REQUEST_INIT\n", dev->name, code_err);
1067 tp->LastOpenStatus =
1068 OPEN_REQUEST_INIT;
1069 break;
1071 case OPEN_REMOVE_RECEIVED:
1072 printk(KERN_INFO "%s: %s OPEN_REMOVE_RECEIVED", dev->name, code_err);
1073 tp->LastOpenStatus =
1074 OPEN_REMOVE_RECEIVED;
1075 break;
1077 case OPEN_FULLDUPLEX_SET:
1078 printk(KERN_INFO "%s: %s OPEN_FULLDUPLEX_SET\n", dev->name, code_err);
1079 tp->LastOpenStatus =
1080 OPEN_FULLDUPLEX_SET;
1081 break;
1083 default:
1084 printk(KERN_INFO "%s: %s Unknown open err code", dev->name, code_err);
1085 tp->LastOpenStatus =
1086 OPEN_FUNCTION_FAILURE;
1087 break;
1091 tp->AdapterOpenFlag = 0;
1092 tp->AdapterVirtOpenFlag = 0;
1094 return;
1097 else
1099 if(ssb_cmd != READ_ERROR_LOG)
1100 return;
1102 /* Add values from the error log table to the MAC
1103 * statistics counters and update the errorlogtable
1104 * memory.
1106 tp->MacStat.line_errors += tp->errorlogtable.Line_Error;
1107 tp->MacStat.burst_errors += tp->errorlogtable.Burst_Error;
1108 tp->MacStat.A_C_errors += tp->errorlogtable.ARI_FCI_Error;
1109 tp->MacStat.lost_frames += tp->errorlogtable.Lost_Frame_Error;
1110 tp->MacStat.recv_congest_count += tp->errorlogtable.Rx_Congest_Error;
1111 tp->MacStat.rx_errors += tp->errorlogtable.Rx_Congest_Error;
1112 tp->MacStat.frame_copied_errors += tp->errorlogtable.Frame_Copied_Error;
1113 tp->MacStat.token_errors += tp->errorlogtable.Token_Error;
1114 tp->MacStat.dummy1 += tp->errorlogtable.DMA_Bus_Error;
1115 tp->MacStat.dummy1 += tp->errorlogtable.DMA_Parity_Error;
1116 tp->MacStat.abort_delimiters += tp->errorlogtable.AbortDelimeters;
1117 tp->MacStat.frequency_errors += tp->errorlogtable.Frequency_Error;
1118 tp->MacStat.internal_errors += tp->errorlogtable.Internal_Error;
1121 return;
1125 * The inverse routine to tms380tr_open().
1127 int tms380tr_close(struct net_device *dev)
1129 struct net_local *tp = netdev_priv(dev);
1130 netif_stop_queue(dev);
1132 del_timer(&tp->timer);
1134 /* Flush the Tx and disable Rx here. */
1136 tp->HaltInProgress = 1;
1137 tms380tr_exec_cmd(dev, OC_CLOSE);
1138 tp->timer.expires = jiffies + 1*HZ;
1139 tp->timer.function = tms380tr_timer_end_wait;
1140 tp->timer.data = (unsigned long)dev;
1141 add_timer(&tp->timer);
1143 tms380tr_enable_interrupts(dev);
1145 tp->Sleeping = 1;
1146 interruptible_sleep_on(&tp->wait_for_tok_int);
1147 tp->TransmitCommandActive = 0;
1149 del_timer(&tp->timer);
1150 tms380tr_disable_interrupts(dev);
1152 #ifdef CONFIG_ISA
1153 if(dev->dma > 0)
1155 unsigned long flags=claim_dma_lock();
1156 disable_dma(dev->dma);
1157 release_dma_lock(flags);
1159 #endif
1161 SIFWRITEW(0xFF00, SIFCMD);
1162 #if 0
1163 if(dev->dma > 0) /* what the? */
1164 SIFWRITEB(0xff, POSREG);
1165 #endif
1166 tms380tr_cancel_tx_queue(tp);
1168 return (0);
1172 * Get the current statistics. This may be called with the card open
1173 * or closed.
1175 static struct net_device_stats *tms380tr_get_stats(struct net_device *dev)
1177 struct net_local *tp = netdev_priv(dev);
1179 return ((struct net_device_stats *)&tp->MacStat);
1183 * Set or clear the multicast filter for this adapter.
1185 static void tms380tr_set_multicast_list(struct net_device *dev)
1187 struct net_local *tp = netdev_priv(dev);
1188 unsigned int OpenOptions;
1190 OpenOptions = tp->ocpl.OPENOptions &
1191 ~(PASS_ADAPTER_MAC_FRAMES
1192 | PASS_ATTENTION_FRAMES
1193 | PASS_BEACON_MAC_FRAMES
1194 | COPY_ALL_MAC_FRAMES
1195 | COPY_ALL_NON_MAC_FRAMES);
1197 tp->ocpl.FunctAddr = 0;
1199 if(dev->flags & IFF_PROMISC)
1200 /* Enable promiscuous mode */
1201 OpenOptions |= COPY_ALL_NON_MAC_FRAMES |
1202 COPY_ALL_MAC_FRAMES;
1203 else
1205 if(dev->flags & IFF_ALLMULTI)
1207 /* Disable promiscuous mode, use normal mode. */
1208 tp->ocpl.FunctAddr = 0xFFFFFFFF;
1210 else
1212 int i;
1213 struct dev_mc_list *mclist = dev->mc_list;
1214 for (i=0; i< dev->mc_count; i++)
1216 ((char *)(&tp->ocpl.FunctAddr))[0] |=
1217 mclist->dmi_addr[2];
1218 ((char *)(&tp->ocpl.FunctAddr))[1] |=
1219 mclist->dmi_addr[3];
1220 ((char *)(&tp->ocpl.FunctAddr))[2] |=
1221 mclist->dmi_addr[4];
1222 ((char *)(&tp->ocpl.FunctAddr))[3] |=
1223 mclist->dmi_addr[5];
1224 mclist = mclist->next;
1227 tms380tr_exec_cmd(dev, OC_SET_FUNCT_ADDR);
1230 tp->ocpl.OPENOptions = OpenOptions;
1231 tms380tr_exec_cmd(dev, OC_MODIFY_OPEN_PARMS);
1232 return;
1236 * Wait for some time (microseconds)
1238 void tms380tr_wait(unsigned long time)
1240 #if 0
1241 long tmp;
1243 tmp = jiffies + time/(1000000/HZ);
1244 do {
1245 tmp = schedule_timeout_interruptible(tmp);
1246 } while(time_after(tmp, jiffies));
1247 #else
1248 udelay(time);
1249 #endif
1250 return;
1254 * Write a command value to the SIFCMD register
1256 static void tms380tr_exec_sifcmd(struct net_device *dev, unsigned int WriteValue)
1258 unsigned short cmd;
1259 unsigned short SifStsValue;
1260 unsigned long loop_counter;
1262 WriteValue = ((WriteValue ^ CMD_SYSTEM_IRQ) | CMD_INTERRUPT_ADAPTER);
1263 cmd = (unsigned short)WriteValue;
1264 loop_counter = 0,5 * 800000;
1265 do {
1266 SifStsValue = SIFREADW(SIFSTS);
1267 } while((SifStsValue & CMD_INTERRUPT_ADAPTER) && loop_counter--);
1268 SIFWRITEW(cmd, SIFCMD);
1270 return;
1274 * Processes adapter hardware reset, halts adapter and downloads firmware,
1275 * clears the halt bit.
1277 static int tms380tr_reset_adapter(struct net_device *dev)
1279 struct net_local *tp = netdev_priv(dev);
1280 unsigned short *fw_ptr;
1281 unsigned short count, c, count2;
1282 const struct firmware *fw_entry = NULL;
1284 if (request_firmware(&fw_entry, "tms380tr.bin", tp->pdev) != 0) {
1285 printk(KERN_ALERT "%s: firmware %s is missing, cannot start.\n",
1286 dev->name, "tms380tr.bin");
1287 return (-1);
1290 fw_ptr = (unsigned short *)fw_entry->data;
1291 count2 = fw_entry->size / 2;
1293 /* Hardware adapter reset */
1294 SIFWRITEW(ACL_ARESET, SIFACL);
1295 tms380tr_wait(40);
1297 c = SIFREADW(SIFACL);
1298 tms380tr_wait(20);
1300 if(dev->dma == 0) /* For PCI adapters */
1302 c &= ~(ACL_NSELOUT0 | ACL_NSELOUT1); /* Clear bits */
1303 if(tp->setnselout)
1304 c |= (*tp->setnselout)(dev);
1307 /* In case a command is pending - forget it */
1308 tp->ScbInUse = 0;
1310 c &= ~ACL_ARESET; /* Clear adapter reset bit */
1311 c |= ACL_CPHALT; /* Halt adapter CPU, allow download */
1312 c |= ACL_BOOT;
1313 c |= ACL_SINTEN;
1314 c &= ~ACL_PSDMAEN; /* Clear pseudo dma bit */
1315 SIFWRITEW(c, SIFACL);
1316 tms380tr_wait(40);
1318 count = 0;
1319 /* Download firmware via DIO interface: */
1320 do {
1321 if (count2 < 3) continue;
1323 /* Download first address part */
1324 SIFWRITEW(*fw_ptr, SIFADX);
1325 fw_ptr++;
1326 count2--;
1327 /* Download second address part */
1328 SIFWRITEW(*fw_ptr, SIFADD);
1329 fw_ptr++;
1330 count2--;
1332 if((count = *fw_ptr) != 0) /* Load loop counter */
1334 fw_ptr++; /* Download block data */
1335 count2--;
1336 if (count > count2) continue;
1338 for(; count > 0; count--)
1340 SIFWRITEW(*fw_ptr, SIFINC);
1341 fw_ptr++;
1342 count2--;
1345 else /* Stop, if last block downloaded */
1347 c = SIFREADW(SIFACL);
1348 c &= (~ACL_CPHALT | ACL_SINTEN);
1350 /* Clear CPHALT and start BUD */
1351 SIFWRITEW(c, SIFACL);
1352 if (fw_entry)
1353 release_firmware(fw_entry);
1354 return (1);
1356 } while(count == 0);
1358 if (fw_entry)
1359 release_firmware(fw_entry);
1360 printk(KERN_INFO "%s: Adapter Download Failed\n", dev->name);
1361 return (-1);
1365 * Starts bring up diagnostics of token ring adapter and evaluates
1366 * diagnostic results.
1368 static int tms380tr_bringup_diags(struct net_device *dev)
1370 int loop_cnt, retry_cnt;
1371 unsigned short Status;
1373 tms380tr_wait(HALF_SECOND);
1374 tms380tr_exec_sifcmd(dev, EXEC_SOFT_RESET);
1375 tms380tr_wait(HALF_SECOND);
1377 retry_cnt = BUD_MAX_RETRIES; /* maximal number of retrys */
1379 do {
1380 retry_cnt--;
1381 if(tms380tr_debug > 3)
1382 printk(KERN_DEBUG "BUD-Status: ");
1383 loop_cnt = BUD_MAX_LOOPCNT; /* maximum: three seconds*/
1384 do { /* Inspect BUD results */
1385 loop_cnt--;
1386 tms380tr_wait(HALF_SECOND);
1387 Status = SIFREADW(SIFSTS);
1388 Status &= STS_MASK;
1390 if(tms380tr_debug > 3)
1391 printk(KERN_DEBUG " %04X \n", Status);
1392 /* BUD successfully completed */
1393 if(Status == STS_INITIALIZE)
1394 return (1);
1395 /* Unrecoverable hardware error, BUD not completed? */
1396 } while((loop_cnt > 0) && ((Status & (STS_ERROR | STS_TEST))
1397 != (STS_ERROR | STS_TEST)));
1399 /* Error preventing completion of BUD */
1400 if(retry_cnt > 0)
1402 printk(KERN_INFO "%s: Adapter Software Reset.\n",
1403 dev->name);
1404 tms380tr_exec_sifcmd(dev, EXEC_SOFT_RESET);
1405 tms380tr_wait(HALF_SECOND);
1407 } while(retry_cnt > 0);
1409 Status = SIFREADW(SIFSTS);
1411 printk(KERN_INFO "%s: Hardware error\n", dev->name);
1412 /* Hardware error occurred! */
1413 Status &= 0x001f;
1414 if (Status & 0x0010)
1415 printk(KERN_INFO "%s: BUD Error: Timeout\n", dev->name);
1416 else if ((Status & 0x000f) > 6)
1417 printk(KERN_INFO "%s: BUD Error: Illegal Failure\n", dev->name);
1418 else
1419 printk(KERN_INFO "%s: Bring Up Diagnostics Error (%04X) occurred\n", dev->name, Status & 0x000f);
1421 return (-1);
1425 * Copy initialisation data to adapter memory, beginning at address
1426 * 1:0A00; Starting DMA test and evaluating result bits.
1428 static int tms380tr_init_adapter(struct net_device *dev)
1430 struct net_local *tp = netdev_priv(dev);
1432 const unsigned char SCB_Test[6] = {0x00, 0x00, 0xC1, 0xE2, 0xD4, 0x8B};
1433 const unsigned char SSB_Test[8] = {0xFF, 0xFF, 0xD1, 0xD7,
1434 0xC5, 0xD9, 0xC3, 0xD4};
1435 void *ptr = (void *)&tp->ipb;
1436 unsigned short *ipb_ptr = (unsigned short *)ptr;
1437 unsigned char *cb_ptr = (unsigned char *) &tp->scb;
1438 unsigned char *sb_ptr = (unsigned char *) &tp->ssb;
1439 unsigned short Status;
1440 int i, loop_cnt, retry_cnt;
1442 /* Normalize: byte order low/high, word order high/low! (only IPB!) */
1443 tp->ipb.SCB_Addr = SWAPW(((char *)&tp->scb - (char *)tp) + tp->dmabuffer);
1444 tp->ipb.SSB_Addr = SWAPW(((char *)&tp->ssb - (char *)tp) + tp->dmabuffer);
1446 if(tms380tr_debug > 3)
1448 printk(KERN_DEBUG "%s: buffer (real): %lx\n", dev->name, (long) &tp->scb);
1449 printk(KERN_DEBUG "%s: buffer (virt): %lx\n", dev->name, (long) ((char *)&tp->scb - (char *)tp) + (long) tp->dmabuffer);
1450 printk(KERN_DEBUG "%s: buffer (DMA) : %lx\n", dev->name, (long) tp->dmabuffer);
1451 printk(KERN_DEBUG "%s: buffer (tp) : %lx\n", dev->name, (long) tp);
1453 /* Maximum: three initialization retries */
1454 retry_cnt = INIT_MAX_RETRIES;
1456 do {
1457 retry_cnt--;
1459 /* Transfer initialization block */
1460 SIFWRITEW(0x0001, SIFADX);
1462 /* To address 0001:0A00 of adapter RAM */
1463 SIFWRITEW(0x0A00, SIFADD);
1465 /* Write 11 words to adapter RAM */
1466 for(i = 0; i < 11; i++)
1467 SIFWRITEW(ipb_ptr[i], SIFINC);
1469 /* Execute SCB adapter command */
1470 tms380tr_exec_sifcmd(dev, CMD_EXECUTE);
1472 loop_cnt = INIT_MAX_LOOPCNT; /* Maximum: 11 seconds */
1474 /* While remaining retries, no error and not completed */
1475 do {
1476 Status = 0;
1477 loop_cnt--;
1478 tms380tr_wait(HALF_SECOND);
1480 /* Mask interesting status bits */
1481 Status = SIFREADW(SIFSTS);
1482 Status &= STS_MASK;
1483 } while(((Status &(STS_INITIALIZE | STS_ERROR | STS_TEST)) != 0)
1484 && ((Status & STS_ERROR) == 0) && (loop_cnt != 0));
1486 if((Status & (STS_INITIALIZE | STS_ERROR | STS_TEST)) == 0)
1488 /* Initialization completed without error */
1489 i = 0;
1490 do { /* Test if contents of SCB is valid */
1491 if(SCB_Test[i] != *(cb_ptr + i))
1493 printk(KERN_INFO "%s: DMA failed\n", dev->name);
1494 /* DMA data error: wrong data in SCB */
1495 return (-1);
1497 i++;
1498 } while(i < 6);
1500 i = 0;
1501 do { /* Test if contents of SSB is valid */
1502 if(SSB_Test[i] != *(sb_ptr + i))
1503 /* DMA data error: wrong data in SSB */
1504 return (-1);
1505 i++;
1506 } while (i < 8);
1508 return (1); /* Adapter successfully initialized */
1510 else
1512 if((Status & STS_ERROR) != 0)
1514 /* Initialization error occurred */
1515 Status = SIFREADW(SIFSTS);
1516 Status &= STS_ERROR_MASK;
1517 /* ShowInitialisationErrorCode(Status); */
1518 printk(KERN_INFO "%s: Status error: %d\n", dev->name, Status);
1519 return (-1); /* Unrecoverable error */
1521 else
1523 if(retry_cnt > 0)
1525 /* Reset adapter and try init again */
1526 tms380tr_exec_sifcmd(dev, EXEC_SOFT_RESET);
1527 tms380tr_wait(HALF_SECOND);
1531 } while(retry_cnt > 0);
1533 printk(KERN_INFO "%s: Retry exceeded\n", dev->name);
1534 return (-1);
1538 * Check for outstanding commands in command queue and tries to execute
1539 * command immediately. Corresponding command flag in command queue is cleared.
1541 static void tms380tr_chk_outstanding_cmds(struct net_device *dev)
1543 struct net_local *tp = netdev_priv(dev);
1544 unsigned long Addr = 0;
1546 if(tp->CMDqueue == 0)
1547 return; /* No command execution */
1549 /* If SCB in use: no command */
1550 if(tp->ScbInUse == 1)
1551 return;
1553 /* Check if adapter is opened, avoiding COMMAND_REJECT
1554 * interrupt by the adapter!
1556 if(tp->AdapterOpenFlag == 0)
1558 if(tp->CMDqueue & OC_OPEN)
1560 /* Execute OPEN command */
1561 tp->CMDqueue ^= OC_OPEN;
1563 Addr = htonl(((char *)&tp->ocpl - (char *)tp) + tp->dmabuffer);
1564 tp->scb.Parm[0] = LOWORD(Addr);
1565 tp->scb.Parm[1] = HIWORD(Addr);
1566 tp->scb.CMD = OPEN;
1568 else
1569 /* No OPEN command queued, but adapter closed. Note:
1570 * We'll try to re-open the adapter in DriverPoll()
1572 return; /* No adapter command issued */
1574 else
1576 /* Adapter is open; evaluate command queue: try to execute
1577 * outstanding commands (depending on priority!) CLOSE
1578 * command queued
1580 if(tp->CMDqueue & OC_CLOSE)
1582 tp->CMDqueue ^= OC_CLOSE;
1583 tp->AdapterOpenFlag = 0;
1584 tp->scb.Parm[0] = 0; /* Parm[0], Parm[1] are ignored */
1585 tp->scb.Parm[1] = 0; /* but should be set to zero! */
1586 tp->scb.CMD = CLOSE;
1587 if(!tp->HaltInProgress)
1588 tp->CMDqueue |= OC_OPEN; /* re-open adapter */
1589 else
1590 tp->CMDqueue = 0; /* no more commands */
1592 else
1594 if(tp->CMDqueue & OC_RECEIVE)
1596 tp->CMDqueue ^= OC_RECEIVE;
1597 Addr = htonl(((char *)tp->RplHead - (char *)tp) + tp->dmabuffer);
1598 tp->scb.Parm[0] = LOWORD(Addr);
1599 tp->scb.Parm[1] = HIWORD(Addr);
1600 tp->scb.CMD = RECEIVE;
1602 else
1604 if(tp->CMDqueue & OC_TRANSMIT_HALT)
1606 /* NOTE: TRANSMIT.HALT must be checked
1607 * before TRANSMIT.
1609 tp->CMDqueue ^= OC_TRANSMIT_HALT;
1610 tp->scb.CMD = TRANSMIT_HALT;
1612 /* Parm[0] and Parm[1] are ignored
1613 * but should be set to zero!
1615 tp->scb.Parm[0] = 0;
1616 tp->scb.Parm[1] = 0;
1618 else
1620 if(tp->CMDqueue & OC_TRANSMIT)
1622 /* NOTE: TRANSMIT must be
1623 * checked after TRANSMIT.HALT
1625 if(tp->TransmitCommandActive)
1627 if(!tp->TransmitHaltScheduled)
1629 tp->TransmitHaltScheduled = 1;
1630 tms380tr_exec_cmd(dev, OC_TRANSMIT_HALT) ;
1632 tp->TransmitCommandActive = 0;
1633 return;
1636 tp->CMDqueue ^= OC_TRANSMIT;
1637 tms380tr_cancel_tx_queue(tp);
1638 Addr = htonl(((char *)tp->TplBusy - (char *)tp) + tp->dmabuffer);
1639 tp->scb.Parm[0] = LOWORD(Addr);
1640 tp->scb.Parm[1] = HIWORD(Addr);
1641 tp->scb.CMD = TRANSMIT;
1642 tp->TransmitCommandActive = 1;
1644 else
1646 if(tp->CMDqueue & OC_MODIFY_OPEN_PARMS)
1648 tp->CMDqueue ^= OC_MODIFY_OPEN_PARMS;
1649 tp->scb.Parm[0] = tp->ocpl.OPENOptions; /* new OPEN options*/
1650 tp->scb.Parm[0] |= ENABLE_FULL_DUPLEX_SELECTION;
1651 tp->scb.Parm[1] = 0; /* is ignored but should be zero */
1652 tp->scb.CMD = MODIFY_OPEN_PARMS;
1654 else
1656 if(tp->CMDqueue & OC_SET_FUNCT_ADDR)
1658 tp->CMDqueue ^= OC_SET_FUNCT_ADDR;
1659 tp->scb.Parm[0] = LOWORD(tp->ocpl.FunctAddr);
1660 tp->scb.Parm[1] = HIWORD(tp->ocpl.FunctAddr);
1661 tp->scb.CMD = SET_FUNCT_ADDR;
1663 else
1665 if(tp->CMDqueue & OC_SET_GROUP_ADDR)
1667 tp->CMDqueue ^= OC_SET_GROUP_ADDR;
1668 tp->scb.Parm[0] = LOWORD(tp->ocpl.GroupAddr);
1669 tp->scb.Parm[1] = HIWORD(tp->ocpl.GroupAddr);
1670 tp->scb.CMD = SET_GROUP_ADDR;
1672 else
1674 if(tp->CMDqueue & OC_READ_ERROR_LOG)
1676 tp->CMDqueue ^= OC_READ_ERROR_LOG;
1677 Addr = htonl(((char *)&tp->errorlogtable - (char *)tp) + tp->dmabuffer);
1678 tp->scb.Parm[0] = LOWORD(Addr);
1679 tp->scb.Parm[1] = HIWORD(Addr);
1680 tp->scb.CMD = READ_ERROR_LOG;
1682 else
1684 printk(KERN_WARNING "CheckForOutstandingCommand: unknown Command\n");
1685 tp->CMDqueue = 0;
1686 return;
1697 tp->ScbInUse = 1; /* Set semaphore: SCB in use. */
1699 /* Execute SCB and generate IRQ when done. */
1700 tms380tr_exec_sifcmd(dev, CMD_EXECUTE | CMD_SCB_REQUEST);
1702 return;
1706 * IRQ conditions: signal loss on the ring, transmit or receive of beacon
1707 * frames (disabled if bit 1 of OPEN option is set); report error MAC
1708 * frame transmit (disabled if bit 2 of OPEN option is set); open or short
1709 * circuit fault on the lobe is detected; remove MAC frame received;
1710 * error counter overflow (255); opened adapter is the only station in ring.
1711 * After some of the IRQs the adapter is closed!
1713 static void tms380tr_ring_status_irq(struct net_device *dev)
1715 struct net_local *tp = netdev_priv(dev);
1717 tp->CurrentRingStatus = be16_to_cpu((unsigned short)tp->ssb.Parm[0]);
1719 /* First: fill up statistics */
1720 if(tp->ssb.Parm[0] & SIGNAL_LOSS)
1722 printk(KERN_INFO "%s: Signal Loss\n", dev->name);
1723 tp->MacStat.line_errors++;
1726 /* Adapter is closed, but initialized */
1727 if(tp->ssb.Parm[0] & LOBE_WIRE_FAULT)
1729 printk(KERN_INFO "%s: Lobe Wire Fault, Reopen Adapter\n",
1730 dev->name);
1731 tp->MacStat.line_errors++;
1734 if(tp->ssb.Parm[0] & RING_RECOVERY)
1735 printk(KERN_INFO "%s: Ring Recovery\n", dev->name);
1737 /* Counter overflow: read error log */
1738 if(tp->ssb.Parm[0] & COUNTER_OVERFLOW)
1740 printk(KERN_INFO "%s: Counter Overflow\n", dev->name);
1741 tms380tr_exec_cmd(dev, OC_READ_ERROR_LOG);
1744 /* Adapter is closed, but initialized */
1745 if(tp->ssb.Parm[0] & REMOVE_RECEIVED)
1746 printk(KERN_INFO "%s: Remove Received, Reopen Adapter\n",
1747 dev->name);
1749 /* Adapter is closed, but initialized */
1750 if(tp->ssb.Parm[0] & AUTO_REMOVAL_ERROR)
1751 printk(KERN_INFO "%s: Auto Removal Error, Reopen Adapter\n",
1752 dev->name);
1754 if(tp->ssb.Parm[0] & HARD_ERROR)
1755 printk(KERN_INFO "%s: Hard Error\n", dev->name);
1757 if(tp->ssb.Parm[0] & SOFT_ERROR)
1758 printk(KERN_INFO "%s: Soft Error\n", dev->name);
1760 if(tp->ssb.Parm[0] & TRANSMIT_BEACON)
1761 printk(KERN_INFO "%s: Transmit Beacon\n", dev->name);
1763 if(tp->ssb.Parm[0] & SINGLE_STATION)
1764 printk(KERN_INFO "%s: Single Station\n", dev->name);
1766 /* Check if adapter has been closed */
1767 if(tp->ssb.Parm[0] & ADAPTER_CLOSED)
1769 printk(KERN_INFO "%s: Adapter closed (Reopening),"
1770 "CurrentRingStat %x\n",
1771 dev->name, tp->CurrentRingStatus);
1772 tp->AdapterOpenFlag = 0;
1773 tms380tr_open_adapter(dev);
1776 return;
1780 * Issued if adapter has encountered an unrecoverable hardware
1781 * or software error.
1783 static void tms380tr_chk_irq(struct net_device *dev)
1785 int i;
1786 unsigned short AdapterCheckBlock[4];
1787 struct net_local *tp = netdev_priv(dev);
1789 tp->AdapterOpenFlag = 0; /* Adapter closed now */
1791 /* Page number of adapter memory */
1792 SIFWRITEW(0x0001, SIFADX);
1793 /* Address offset */
1794 SIFWRITEW(CHECKADDR, SIFADR);
1796 /* Reading 8 byte adapter check block. */
1797 for(i = 0; i < 4; i++)
1798 AdapterCheckBlock[i] = SIFREADW(SIFINC);
1800 if(tms380tr_debug > 3)
1802 printk(KERN_DEBUG "%s: AdapterCheckBlock: ", dev->name);
1803 for (i = 0; i < 4; i++)
1804 printk("%04X", AdapterCheckBlock[i]);
1805 printk("\n");
1808 switch(AdapterCheckBlock[0])
1810 case DIO_PARITY:
1811 printk(KERN_INFO "%s: DIO parity error\n", dev->name);
1812 break;
1814 case DMA_READ_ABORT:
1815 printk(KERN_INFO "%s DMA read operation aborted:\n",
1816 dev->name);
1817 switch (AdapterCheckBlock[1])
1819 case 0:
1820 printk(KERN_INFO "Timeout\n");
1821 printk(KERN_INFO "Address: %04X %04X\n",
1822 AdapterCheckBlock[2],
1823 AdapterCheckBlock[3]);
1824 break;
1826 case 1:
1827 printk(KERN_INFO "Parity error\n");
1828 printk(KERN_INFO "Address: %04X %04X\n",
1829 AdapterCheckBlock[2],
1830 AdapterCheckBlock[3]);
1831 break;
1833 case 2:
1834 printk(KERN_INFO "Bus error\n");
1835 printk(KERN_INFO "Address: %04X %04X\n",
1836 AdapterCheckBlock[2],
1837 AdapterCheckBlock[3]);
1838 break;
1840 default:
1841 printk(KERN_INFO "Unknown error.\n");
1842 break;
1844 break;
1846 case DMA_WRITE_ABORT:
1847 printk(KERN_INFO "%s: DMA write operation aborted: \n",
1848 dev->name);
1849 switch (AdapterCheckBlock[1])
1851 case 0:
1852 printk(KERN_INFO "Timeout\n");
1853 printk(KERN_INFO "Address: %04X %04X\n",
1854 AdapterCheckBlock[2],
1855 AdapterCheckBlock[3]);
1856 break;
1858 case 1:
1859 printk(KERN_INFO "Parity error\n");
1860 printk(KERN_INFO "Address: %04X %04X\n",
1861 AdapterCheckBlock[2],
1862 AdapterCheckBlock[3]);
1863 break;
1865 case 2:
1866 printk(KERN_INFO "Bus error\n");
1867 printk(KERN_INFO "Address: %04X %04X\n",
1868 AdapterCheckBlock[2],
1869 AdapterCheckBlock[3]);
1870 break;
1872 default:
1873 printk(KERN_INFO "Unknown error.\n");
1874 break;
1876 break;
1878 case ILLEGAL_OP_CODE:
1879 printk(KERN_INFO "%s: Illegal operation code in firmware\n",
1880 dev->name);
1881 /* Parm[0-3]: adapter internal register R13-R15 */
1882 break;
1884 case PARITY_ERRORS:
1885 printk(KERN_INFO "%s: Adapter internal bus parity error\n",
1886 dev->name);
1887 /* Parm[0-3]: adapter internal register R13-R15 */
1888 break;
1890 case RAM_DATA_ERROR:
1891 printk(KERN_INFO "%s: RAM data error\n", dev->name);
1892 /* Parm[0-1]: MSW/LSW address of RAM location. */
1893 break;
1895 case RAM_PARITY_ERROR:
1896 printk(KERN_INFO "%s: RAM parity error\n", dev->name);
1897 /* Parm[0-1]: MSW/LSW address of RAM location. */
1898 break;
1900 case RING_UNDERRUN:
1901 printk(KERN_INFO "%s: Internal DMA underrun detected\n",
1902 dev->name);
1903 break;
1905 case INVALID_IRQ:
1906 printk(KERN_INFO "%s: Unrecognized interrupt detected\n",
1907 dev->name);
1908 /* Parm[0-3]: adapter internal register R13-R15 */
1909 break;
1911 case INVALID_ERROR_IRQ:
1912 printk(KERN_INFO "%s: Unrecognized error interrupt detected\n",
1913 dev->name);
1914 /* Parm[0-3]: adapter internal register R13-R15 */
1915 break;
1917 case INVALID_XOP:
1918 printk(KERN_INFO "%s: Unrecognized XOP request detected\n",
1919 dev->name);
1920 /* Parm[0-3]: adapter internal register R13-R15 */
1921 break;
1923 default:
1924 printk(KERN_INFO "%s: Unknown status", dev->name);
1925 break;
1928 if(tms380tr_chipset_init(dev) == 1)
1930 /* Restart of firmware successful */
1931 tp->AdapterOpenFlag = 1;
1934 return;
1938 * Internal adapter pointer to RAM data are copied from adapter into
1939 * host system.
1941 static int tms380tr_read_ptr(struct net_device *dev)
1943 struct net_local *tp = netdev_priv(dev);
1944 unsigned short adapterram;
1946 tms380tr_read_ram(dev, (unsigned char *)&tp->intptrs.BurnedInAddrPtr,
1947 ADAPTER_INT_PTRS, 16);
1948 tms380tr_read_ram(dev, (unsigned char *)&adapterram,
1949 cpu_to_be16((unsigned short)tp->intptrs.AdapterRAMPtr), 2);
1950 return be16_to_cpu(adapterram);
1954 * Reads a number of bytes from adapter to system memory.
1956 static void tms380tr_read_ram(struct net_device *dev, unsigned char *Data,
1957 unsigned short Address, int Length)
1959 int i;
1960 unsigned short old_sifadx, old_sifadr, InWord;
1962 /* Save the current values */
1963 old_sifadx = SIFREADW(SIFADX);
1964 old_sifadr = SIFREADW(SIFADR);
1966 /* Page number of adapter memory */
1967 SIFWRITEW(0x0001, SIFADX);
1968 /* Address offset in adapter RAM */
1969 SIFWRITEW(Address, SIFADR);
1971 /* Copy len byte from adapter memory to system data area. */
1972 i = 0;
1973 for(;;)
1975 InWord = SIFREADW(SIFINC);
1977 *(Data + i) = HIBYTE(InWord); /* Write first byte */
1978 if(++i == Length) /* All is done break */
1979 break;
1981 *(Data + i) = LOBYTE(InWord); /* Write second byte */
1982 if (++i == Length) /* All is done break */
1983 break;
1986 /* Restore original values */
1987 SIFWRITEW(old_sifadx, SIFADX);
1988 SIFWRITEW(old_sifadr, SIFADR);
1990 return;
1994 * Cancel all queued packets in the transmission queue.
1996 static void tms380tr_cancel_tx_queue(struct net_local* tp)
1998 TPL *tpl;
2001 * NOTE: There must not be an active TRANSMIT command pending, when
2002 * this function is called.
2004 if(tp->TransmitCommandActive)
2005 return;
2007 for(;;)
2009 tpl = tp->TplBusy;
2010 if(!tpl->BusyFlag)
2011 break;
2012 /* "Remove" TPL from busy list. */
2013 tp->TplBusy = tpl->NextTPLPtr;
2014 tms380tr_write_tpl_status(tpl, 0); /* Clear VALID bit */
2015 tpl->BusyFlag = 0; /* "free" TPL */
2017 printk(KERN_INFO "Cancel tx (%08lXh).\n", (unsigned long)tpl);
2018 if (tpl->DMABuff)
2019 dma_unmap_single(tp->pdev, tpl->DMABuff, tpl->Skb->len, DMA_TO_DEVICE);
2020 dev_kfree_skb_any(tpl->Skb);
2023 return;
2027 * This function is called whenever a transmit interrupt is generated by the
2028 * adapter. For a command complete interrupt, it is checked if we have to
2029 * issue a new transmit command or not.
2031 static void tms380tr_tx_status_irq(struct net_device *dev)
2033 struct net_local *tp = netdev_priv(dev);
2034 unsigned char HighByte, HighAc, LowAc;
2035 TPL *tpl;
2037 /* NOTE: At this point the SSB from TRANSMIT STATUS is no longer
2038 * available, because the CLEAR SSB command has already been issued.
2040 * Process all complete transmissions.
2043 for(;;)
2045 tpl = tp->TplBusy;
2046 if(!tpl->BusyFlag || (tpl->Status
2047 & (TX_VALID | TX_FRAME_COMPLETE))
2048 != TX_FRAME_COMPLETE)
2050 break;
2053 /* "Remove" TPL from busy list. */
2054 tp->TplBusy = tpl->NextTPLPtr ;
2056 /* Check the transmit status field only for directed frames*/
2057 if(DIRECTED_FRAME(tpl) && (tpl->Status & TX_ERROR) == 0)
2059 HighByte = GET_TRANSMIT_STATUS_HIGH_BYTE(tpl->Status);
2060 HighAc = GET_FRAME_STATUS_HIGH_AC(HighByte);
2061 LowAc = GET_FRAME_STATUS_LOW_AC(HighByte);
2063 if((HighAc != LowAc) || (HighAc == AC_NOT_RECOGNIZED))
2065 printk(KERN_DEBUG "%s: (DA=%08lX not recognized)\n",
2066 dev->name,
2067 *(unsigned long *)&tpl->MData[2+2]);
2069 else
2071 if(tms380tr_debug > 3)
2072 printk(KERN_DEBUG "%s: Directed frame tx'd\n",
2073 dev->name);
2076 else
2078 if(!DIRECTED_FRAME(tpl))
2080 if(tms380tr_debug > 3)
2081 printk(KERN_DEBUG "%s: Broadcast frame tx'd\n",
2082 dev->name);
2086 tp->MacStat.tx_packets++;
2087 if (tpl->DMABuff)
2088 dma_unmap_single(tp->pdev, tpl->DMABuff, tpl->Skb->len, DMA_TO_DEVICE);
2089 dev_kfree_skb_irq(tpl->Skb);
2090 tpl->BusyFlag = 0; /* "free" TPL */
2093 if(!tp->TplFree->NextTPLPtr->BusyFlag)
2094 netif_wake_queue(dev);
2095 return;
2099 * Called if a frame receive interrupt is generated by the adapter.
2100 * Check if the frame is valid and indicate it to system.
2102 static void tms380tr_rcv_status_irq(struct net_device *dev)
2104 struct net_local *tp = netdev_priv(dev);
2105 unsigned char *ReceiveDataPtr;
2106 struct sk_buff *skb;
2107 unsigned int Length, Length2;
2108 RPL *rpl;
2109 RPL *SaveHead;
2110 dma_addr_t dmabuf;
2112 /* NOTE: At this point the SSB from RECEIVE STATUS is no longer
2113 * available, because the CLEAR SSB command has already been issued.
2115 * Process all complete receives.
2118 for(;;)
2120 rpl = tp->RplHead;
2121 if(rpl->Status & RX_VALID)
2122 break; /* RPL still in use by adapter */
2124 /* Forward RPLHead pointer to next list. */
2125 SaveHead = tp->RplHead;
2126 tp->RplHead = rpl->NextRPLPtr;
2128 /* Get the frame size (Byte swap for Intel).
2129 * Do this early (see workaround comment below)
2131 Length = be16_to_cpu(rpl->FrameSize);
2133 /* Check if the Frame_Start, Frame_End and
2134 * Frame_Complete bits are set.
2136 if((rpl->Status & VALID_SINGLE_BUFFER_FRAME)
2137 == VALID_SINGLE_BUFFER_FRAME)
2139 ReceiveDataPtr = rpl->MData;
2141 /* Workaround for delayed write of FrameSize on ISA
2142 * (FrameSize is false but valid-bit is reset)
2143 * Frame size is set to zero when the RPL is freed.
2144 * Length2 is there because there have also been
2145 * cases where the FrameSize was partially written
2147 Length2 = be16_to_cpu(rpl->FrameSize);
2149 if(Length == 0 || Length != Length2)
2151 tp->RplHead = SaveHead;
2152 break; /* Return to tms380tr_interrupt */
2154 tms380tr_update_rcv_stats(tp,ReceiveDataPtr,Length);
2156 if(tms380tr_debug > 3)
2157 printk(KERN_DEBUG "%s: Packet Length %04X (%d)\n",
2158 dev->name, Length, Length);
2160 /* Indicate the received frame to system the
2161 * adapter does the Source-Routing padding for
2162 * us. See: OpenOptions in tms380tr_init_opb()
2164 skb = rpl->Skb;
2165 if(rpl->SkbStat == SKB_UNAVAILABLE)
2167 /* Try again to allocate skb */
2168 skb = dev_alloc_skb(tp->MaxPacketSize);
2169 if(skb == NULL)
2171 /* Update Stats ?? */
2173 else
2175 skb_put(skb, tp->MaxPacketSize);
2176 rpl->SkbStat = SKB_DATA_COPY;
2177 ReceiveDataPtr = rpl->MData;
2181 if(skb && (rpl->SkbStat == SKB_DATA_COPY
2182 || rpl->SkbStat == SKB_DMA_DIRECT))
2184 if(rpl->SkbStat == SKB_DATA_COPY)
2185 skb_copy_to_linear_data(skb, ReceiveDataPtr,
2186 Length);
2188 /* Deliver frame to system */
2189 rpl->Skb = NULL;
2190 skb_trim(skb,Length);
2191 skb->protocol = tr_type_trans(skb,dev);
2192 netif_rx(skb);
2195 else /* Invalid frame */
2197 if(rpl->Skb != NULL)
2198 dev_kfree_skb_irq(rpl->Skb);
2200 /* Skip list. */
2201 if(rpl->Status & RX_START_FRAME)
2202 /* Frame start bit is set -> overflow. */
2203 tp->MacStat.rx_errors++;
2205 if (rpl->DMABuff)
2206 dma_unmap_single(tp->pdev, rpl->DMABuff, tp->MaxPacketSize, DMA_TO_DEVICE);
2207 rpl->DMABuff = 0;
2209 /* Allocate new skb for rpl */
2210 rpl->Skb = dev_alloc_skb(tp->MaxPacketSize);
2211 /* skb == NULL ? then use local buffer */
2212 if(rpl->Skb == NULL)
2214 rpl->SkbStat = SKB_UNAVAILABLE;
2215 rpl->FragList[0].DataAddr = htonl(((char *)tp->LocalRxBuffers[rpl->RPLIndex] - (char *)tp) + tp->dmabuffer);
2216 rpl->MData = tp->LocalRxBuffers[rpl->RPLIndex];
2218 else /* skb != NULL */
2220 rpl->Skb->dev = dev;
2221 skb_put(rpl->Skb, tp->MaxPacketSize);
2223 /* Data unreachable for DMA ? then use local buffer */
2224 dmabuf = dma_map_single(tp->pdev, rpl->Skb->data, tp->MaxPacketSize, DMA_FROM_DEVICE);
2225 if(tp->dmalimit && (dmabuf + tp->MaxPacketSize > tp->dmalimit))
2227 rpl->SkbStat = SKB_DATA_COPY;
2228 rpl->FragList[0].DataAddr = htonl(((char *)tp->LocalRxBuffers[rpl->RPLIndex] - (char *)tp) + tp->dmabuffer);
2229 rpl->MData = tp->LocalRxBuffers[rpl->RPLIndex];
2231 else
2233 /* DMA directly in skb->data */
2234 rpl->SkbStat = SKB_DMA_DIRECT;
2235 rpl->FragList[0].DataAddr = htonl(dmabuf);
2236 rpl->MData = rpl->Skb->data;
2237 rpl->DMABuff = dmabuf;
2241 rpl->FragList[0].DataCount = cpu_to_be16((unsigned short)tp->MaxPacketSize);
2242 rpl->FrameSize = 0;
2244 /* Pass the last RPL back to the adapter */
2245 tp->RplTail->FrameSize = 0;
2247 /* Reset the CSTAT field in the list. */
2248 tms380tr_write_rpl_status(tp->RplTail, RX_VALID | RX_FRAME_IRQ);
2250 /* Current RPL becomes last one in list. */
2251 tp->RplTail = tp->RplTail->NextRPLPtr;
2253 /* Inform adapter about RPL valid. */
2254 tms380tr_exec_sifcmd(dev, CMD_RX_VALID);
2257 return;
2261 * This function should be used whenever the status of any RPL must be
2262 * modified by the driver, because the compiler may otherwise change the
2263 * order of instructions such that writing the RPL status may be executed
2264 * at an undesireable time. When this function is used, the status is
2265 * always written when the function is called.
2267 static void tms380tr_write_rpl_status(RPL *rpl, unsigned int Status)
2269 rpl->Status = Status;
2271 return;
2275 * The function updates the statistic counters in mac->MacStat.
2276 * It differtiates between directed and broadcast/multicast ( ==functional)
2277 * frames.
2279 static void tms380tr_update_rcv_stats(struct net_local *tp, unsigned char DataPtr[],
2280 unsigned int Length)
2282 tp->MacStat.rx_packets++;
2283 tp->MacStat.rx_bytes += Length;
2285 /* Test functional bit */
2286 if(DataPtr[2] & GROUP_BIT)
2287 tp->MacStat.multicast++;
2289 return;
2292 static int tms380tr_set_mac_address(struct net_device *dev, void *addr)
2294 struct net_local *tp = netdev_priv(dev);
2295 struct sockaddr *saddr = addr;
2297 if (tp->AdapterOpenFlag || tp->AdapterVirtOpenFlag) {
2298 printk(KERN_WARNING "%s: Cannot set MAC/LAA address while card is open\n", dev->name);
2299 return -EIO;
2301 memcpy(dev->dev_addr, saddr->sa_data, dev->addr_len);
2302 return 0;
2305 #if TMS380TR_DEBUG > 0
2307 * Dump Packet (data)
2309 static void tms380tr_dump(unsigned char *Data, int length)
2311 int i, j;
2313 for (i = 0, j = 0; i < length / 8; i++, j += 8)
2315 printk(KERN_DEBUG "%02x %02x %02x %02x %02x %02x %02x %02x\n",
2316 Data[j+0],Data[j+1],Data[j+2],Data[j+3],
2317 Data[j+4],Data[j+5],Data[j+6],Data[j+7]);
2320 return;
2322 #endif
2324 void tmsdev_term(struct net_device *dev)
2326 struct net_local *tp;
2328 tp = netdev_priv(dev);
2329 dma_unmap_single(tp->pdev, tp->dmabuffer, sizeof(struct net_local),
2330 DMA_BIDIRECTIONAL);
2333 const struct net_device_ops tms380tr_netdev_ops = {
2334 .ndo_open = tms380tr_open,
2335 .ndo_stop = tms380tr_close,
2336 .ndo_start_xmit = tms380tr_send_packet,
2337 .ndo_tx_timeout = tms380tr_timeout,
2338 .ndo_get_stats = tms380tr_get_stats,
2339 .ndo_set_multicast_list = tms380tr_set_multicast_list,
2340 .ndo_set_mac_address = tms380tr_set_mac_address,
2342 EXPORT_SYMBOL(tms380tr_netdev_ops);
2344 int tmsdev_init(struct net_device *dev, struct device *pdev)
2346 struct net_local *tms_local;
2348 memset(netdev_priv(dev), 0, sizeof(struct net_local));
2349 tms_local = netdev_priv(dev);
2350 init_waitqueue_head(&tms_local->wait_for_tok_int);
2351 if (pdev->dma_mask)
2352 tms_local->dmalimit = *pdev->dma_mask;
2353 else
2354 return -ENOMEM;
2355 tms_local->pdev = pdev;
2356 tms_local->dmabuffer = dma_map_single(pdev, (void *)tms_local,
2357 sizeof(struct net_local), DMA_BIDIRECTIONAL);
2358 if (tms_local->dmabuffer + sizeof(struct net_local) >
2359 tms_local->dmalimit)
2361 printk(KERN_INFO "%s: Memory not accessible for DMA\n",
2362 dev->name);
2363 tmsdev_term(dev);
2364 return -ENOMEM;
2367 dev->netdev_ops = &tms380tr_netdev_ops;
2368 dev->watchdog_timeo = HZ;
2370 return 0;
2373 EXPORT_SYMBOL(tms380tr_open);
2374 EXPORT_SYMBOL(tms380tr_close);
2375 EXPORT_SYMBOL(tms380tr_interrupt);
2376 EXPORT_SYMBOL(tmsdev_init);
2377 EXPORT_SYMBOL(tmsdev_term);
2378 EXPORT_SYMBOL(tms380tr_wait);
2380 #ifdef MODULE
2382 static struct module *TMS380_module = NULL;
2384 int init_module(void)
2386 printk(KERN_DEBUG "%s", version);
2388 TMS380_module = &__this_module;
2389 return 0;
2392 void cleanup_module(void)
2394 TMS380_module = NULL;
2396 #endif
2398 MODULE_LICENSE("GPL");