Merge ../linux-2.6 by hand
[linux-2.6] / drivers / net / tokenring / tms380tr.c
1 /*
2  *  tms380tr.c: A network driver library for Texas Instruments TMS380-based
3  *              Token Ring Adapters.
4  *
5  *  Originally sktr.c: Written 1997 by Christoph Goos
6  *
7  *  A fine result of the Linux Systems Network Architecture Project.
8  *  http://www.linux-sna.org
9  *
10  *  This software may be used and distributed according to the terms
11  *  of the GNU General Public License, incorporated herein by reference.
12  *
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)
17  *
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
29  *
30  *  Maintainer(s):
31  *    JS        Jay Schulist            jschlst@samba.org
32  *    CG        Christoph Goos          cgoos@syskonnect.de
33  *    AF        Adam Fritzler           mid@auk.cx
34  *    MLP       Mike Phillips           phillim@amtrak.com
35  *    JF        Jochen Friedrich        jochen@scram.de
36  *     
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.
66  *                              
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.
74  */
75
76 #ifdef MODULE
77 static const char version[] = "tms380tr.c: v1.10 30/12/2002 by Christoph Goos, Adam Fritzler\n";
78 #endif
79
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>
101
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>
107
108 #include "tms380tr.h"           /* Our Stuff */
109
110 /* Use 0 for production, 1 for verification, 2 for debug, and
111  * 3 for very verbose debug.
112  */
113 #ifndef TMS380TR_DEBUG
114 #define TMS380TR_DEBUG 0
115 #endif
116 static unsigned int tms380tr_debug = TMS380TR_DEBUG;
117
118 /* Index to functions, as function prototypes.
119  * Alphabetical by function name.
120  */
121
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);
182
183 #define SIFREADB(reg) (((struct net_local *)dev->priv)->sifreadb(dev, reg))
184 #define SIFWRITEB(val, reg) (((struct net_local *)dev->priv)->sifwriteb(dev, val, reg))
185 #define SIFREADW(reg) (((struct net_local *)dev->priv)->sifreadw(dev, reg))
186 #define SIFWRITEW(val, reg) (((struct net_local *)dev->priv)->sifwritew(dev, val, reg))
187
188
189
190 #if 0 /* TMS380TR_DEBUG > 0 */
191 static int madgemc_sifprobe(struct net_device *dev)
192 {
193         unsigned char old, chk1, chk2;
194         
195         old = SIFREADB(SIFADR);  /* Get the old SIFADR value */
196
197         chk1 = 0;       /* Begin with check value 0 */
198         do {
199                 madgemc_setregpage(dev, 0);
200                 /* Write new SIFADR value */
201                 SIFWRITEB(chk1, SIFADR);
202                 chk2 = SIFREADB(SIFADR);
203                 if (chk2 != chk1)
204                         return -1;
205                 
206                 madgemc_setregpage(dev, 1);
207                 /* Read, invert and write */
208                 chk2 = SIFREADB(SIFADD);
209                 if (chk2 != chk1)
210                         return -1;
211
212                 madgemc_setregpage(dev, 0);
213                 chk2 ^= 0x0FE;
214                 SIFWRITEB(chk2, SIFADR);
215
216                 /* Read, invert and compare */
217                 madgemc_setregpage(dev, 1);
218                 chk2 = SIFREADB(SIFADD);
219                 madgemc_setregpage(dev, 0);
220                 chk2 ^= 0x0FE;
221
222                 if(chk1 != chk2)
223                         return (-1);    /* No adapter */
224                 chk1 -= 2;
225         } while(chk1 != 0);     /* Repeat 128 times (all byte values) */
226
227         madgemc_setregpage(dev, 0); /* sanity */
228         /* Restore the SIFADR value */
229         SIFWRITEB(old, SIFADR);
230
231         return (0);
232 }
233 #endif
234
235 /*
236  * Open/initialize the board. This is called sometime after
237  * booting when the 'ifconfig' program is run.
238  *
239  * This routine should set everything up anew at each open, even
240  * registers that "should" only need to be set once at boot, so that
241  * there is non-reboot way to recover if something goes wrong.
242  */
243 int tms380tr_open(struct net_device *dev)
244 {
245         struct net_local *tp = netdev_priv(dev);
246         int err;
247         
248         /* init the spinlock */
249         spin_lock_init(&tp->lock);
250         init_timer(&tp->timer);
251
252         /* Reset the hardware here. Don't forget to set the station address. */
253
254 #ifdef CONFIG_ISA
255         if(dev->dma > 0) 
256         {
257                 unsigned long flags=claim_dma_lock();
258                 disable_dma(dev->dma);
259                 set_dma_mode(dev->dma, DMA_MODE_CASCADE);
260                 enable_dma(dev->dma);
261                 release_dma_lock(flags);
262         }
263 #endif
264         
265         err = tms380tr_chipset_init(dev);
266         if(err)
267         {
268                 printk(KERN_INFO "%s: Chipset initialization error\n", 
269                         dev->name);
270                 return (-1);
271         }
272
273         tp->timer.expires       = jiffies + 30*HZ;
274         tp->timer.function      = tms380tr_timer_end_wait;
275         tp->timer.data          = (unsigned long)dev;
276         add_timer(&tp->timer);
277
278         printk(KERN_DEBUG "%s: Adapter RAM size: %dK\n", 
279                dev->name, tms380tr_read_ptr(dev));
280
281         tms380tr_enable_interrupts(dev);
282         tms380tr_open_adapter(dev);
283
284         netif_start_queue(dev);
285         
286         /* Wait for interrupt from hardware. If interrupt does not come,
287          * there will be a timeout from the timer.
288          */
289         tp->Sleeping = 1;
290         interruptible_sleep_on(&tp->wait_for_tok_int);
291         del_timer(&tp->timer);
292
293         /* If AdapterVirtOpenFlag is 1, the adapter is now open for use */
294         if(tp->AdapterVirtOpenFlag == 0)
295         {
296                 tms380tr_disable_interrupts(dev);
297                 return (-1);
298         }
299
300         tp->StartTime = jiffies;
301
302         /* Start function control timer */
303         tp->timer.expires       = jiffies + 2*HZ;
304         tp->timer.function      = tms380tr_timer_chk;
305         tp->timer.data          = (unsigned long)dev;
306         add_timer(&tp->timer);
307
308         return (0);
309 }
310
311 /*
312  * Timeout function while waiting for event
313  */
314 static void tms380tr_timer_end_wait(unsigned long data)
315 {
316         struct net_device *dev = (struct net_device*)data;
317         struct net_local *tp = netdev_priv(dev);
318
319         if(tp->Sleeping)
320         {
321                 tp->Sleeping = 0;
322                 wake_up_interruptible(&tp->wait_for_tok_int);
323         }
324
325         return;
326 }
327
328 /*
329  * Initialize the chipset
330  */
331 static int tms380tr_chipset_init(struct net_device *dev)
332 {
333         struct net_local *tp = netdev_priv(dev);
334         int err;
335
336         tms380tr_init_ipb(tp);
337         tms380tr_init_opb(dev);
338         tms380tr_init_net_local(dev);
339
340         if(tms380tr_debug > 3)
341                 printk(KERN_DEBUG "%s: Resetting adapter...\n", dev->name);
342         err = tms380tr_reset_adapter(dev);
343         if(err < 0)
344                 return (-1);
345
346         if(tms380tr_debug > 3)
347                 printk(KERN_DEBUG "%s: Bringup diags...\n", dev->name);
348         err = tms380tr_bringup_diags(dev);
349         if(err < 0)
350                 return (-1);
351
352         if(tms380tr_debug > 3)
353                 printk(KERN_DEBUG "%s: Init adapter...\n", dev->name);
354         err = tms380tr_init_adapter(dev);
355         if(err < 0)
356                 return (-1);
357
358         if(tms380tr_debug > 3)
359                 printk(KERN_DEBUG "%s: Done!\n", dev->name);
360         return (0);
361 }
362
363 /*
364  * Initializes the net_local structure.
365  */
366 static void tms380tr_init_net_local(struct net_device *dev)
367 {
368         struct net_local *tp = netdev_priv(dev);
369         int i;
370         dma_addr_t dmabuf;
371
372         tp->scb.CMD     = 0;
373         tp->scb.Parm[0] = 0;
374         tp->scb.Parm[1] = 0;
375
376         tp->ssb.STS     = 0;
377         tp->ssb.Parm[0] = 0;
378         tp->ssb.Parm[1] = 0;
379         tp->ssb.Parm[2] = 0;
380
381         tp->CMDqueue    = 0;
382
383         tp->AdapterOpenFlag     = 0;
384         tp->AdapterVirtOpenFlag = 0;
385         tp->ScbInUse            = 0;
386         tp->OpenCommandIssued   = 0;
387         tp->ReOpenInProgress    = 0;
388         tp->HaltInProgress      = 0;
389         tp->TransmitHaltScheduled = 0;
390         tp->LobeWireFaultLogged = 0;
391         tp->LastOpenStatus      = 0;
392         tp->MaxPacketSize       = DEFAULT_PACKET_SIZE;
393
394         /* Create circular chain of transmit lists */
395         for (i = 0; i < TPL_NUM; i++)
396         {
397                 tp->Tpl[i].NextTPLAddr = htonl(((char *)(&tp->Tpl[(i+1) % TPL_NUM]) - (char *)tp) + tp->dmabuffer); /* DMA buffer may be MMU driven */
398                 tp->Tpl[i].Status       = 0;
399                 tp->Tpl[i].FrameSize    = 0;
400                 tp->Tpl[i].FragList[0].DataCount        = 0;
401                 tp->Tpl[i].FragList[0].DataAddr         = 0;
402                 tp->Tpl[i].NextTPLPtr   = &tp->Tpl[(i+1) % TPL_NUM];
403                 tp->Tpl[i].MData        = NULL;
404                 tp->Tpl[i].TPLIndex     = i;
405                 tp->Tpl[i].DMABuff      = 0;
406                 tp->Tpl[i].BusyFlag     = 0;
407         }
408
409         tp->TplFree = tp->TplBusy = &tp->Tpl[0];
410
411         /* Create circular chain of receive lists */
412         for (i = 0; i < RPL_NUM; i++)
413         {
414                 tp->Rpl[i].NextRPLAddr = htonl(((char *)(&tp->Rpl[(i+1) % RPL_NUM]) - (char *)tp) + tp->dmabuffer); /* DMA buffer may be MMU driven */
415                 tp->Rpl[i].Status = (RX_VALID | RX_START_FRAME | RX_END_FRAME | RX_FRAME_IRQ);
416                 tp->Rpl[i].FrameSize = 0;
417                 tp->Rpl[i].FragList[0].DataCount = cpu_to_be16((unsigned short)tp->MaxPacketSize);
418
419                 /* Alloc skb and point adapter to data area */
420                 tp->Rpl[i].Skb = dev_alloc_skb(tp->MaxPacketSize);
421                         tp->Rpl[i].DMABuff = 0;
422
423                 /* skb == NULL ? then use local buffer */
424                 if(tp->Rpl[i].Skb == NULL)
425                 {
426                         tp->Rpl[i].SkbStat = SKB_UNAVAILABLE;
427                         tp->Rpl[i].FragList[0].DataAddr = htonl(((char *)tp->LocalRxBuffers[i] - (char *)tp) + tp->dmabuffer);
428                         tp->Rpl[i].MData = tp->LocalRxBuffers[i];
429                 }
430                 else    /* SKB != NULL */
431                 {
432                         tp->Rpl[i].Skb->dev = dev;
433                         skb_put(tp->Rpl[i].Skb, tp->MaxPacketSize);
434
435                         /* data unreachable for DMA ? then use local buffer */
436                         dmabuf = dma_map_single(tp->pdev, tp->Rpl[i].Skb->data, tp->MaxPacketSize, DMA_FROM_DEVICE);
437                         if(tp->dmalimit && (dmabuf + tp->MaxPacketSize > tp->dmalimit))
438                         {
439                                 tp->Rpl[i].SkbStat = SKB_DATA_COPY;
440                                 tp->Rpl[i].FragList[0].DataAddr = htonl(((char *)tp->LocalRxBuffers[i] - (char *)tp) + tp->dmabuffer);
441                                 tp->Rpl[i].MData = tp->LocalRxBuffers[i];
442                         }
443                         else    /* DMA directly in skb->data */
444                         {
445                                 tp->Rpl[i].SkbStat = SKB_DMA_DIRECT;
446                                 tp->Rpl[i].FragList[0].DataAddr = htonl(dmabuf);
447                                 tp->Rpl[i].MData = tp->Rpl[i].Skb->data;
448                                 tp->Rpl[i].DMABuff = dmabuf;
449                         }
450                 }
451
452                 tp->Rpl[i].NextRPLPtr = &tp->Rpl[(i+1) % RPL_NUM];
453                 tp->Rpl[i].RPLIndex = i;
454         }
455
456         tp->RplHead = &tp->Rpl[0];
457         tp->RplTail = &tp->Rpl[RPL_NUM-1];
458         tp->RplTail->Status = (RX_START_FRAME | RX_END_FRAME | RX_FRAME_IRQ);
459
460         return;
461 }
462
463 /*
464  * Initializes the initialisation parameter block.
465  */
466 static void tms380tr_init_ipb(struct net_local *tp)
467 {
468         tp->ipb.Init_Options    = BURST_MODE;
469         tp->ipb.CMD_Status_IV   = 0;
470         tp->ipb.TX_IV           = 0;
471         tp->ipb.RX_IV           = 0;
472         tp->ipb.Ring_Status_IV  = 0;
473         tp->ipb.SCB_Clear_IV    = 0;
474         tp->ipb.Adapter_CHK_IV  = 0;
475         tp->ipb.RX_Burst_Size   = BURST_SIZE;
476         tp->ipb.TX_Burst_Size   = BURST_SIZE;
477         tp->ipb.DMA_Abort_Thrhld = DMA_RETRIES;
478         tp->ipb.SCB_Addr        = 0;
479         tp->ipb.SSB_Addr        = 0;
480
481         return;
482 }
483
484 /*
485  * Initializes the open parameter block.
486  */
487 static void tms380tr_init_opb(struct net_device *dev)
488 {
489         struct net_local *tp;
490         unsigned long Addr;
491         unsigned short RplSize    = RPL_SIZE;
492         unsigned short TplSize    = TPL_SIZE;
493         unsigned short BufferSize = BUFFER_SIZE;
494         int i;
495
496         tp = netdev_priv(dev);
497
498         tp->ocpl.OPENOptions     = 0;
499         tp->ocpl.OPENOptions    |= ENABLE_FULL_DUPLEX_SELECTION;
500         tp->ocpl.FullDuplex      = 0;
501         tp->ocpl.FullDuplex     |= OPEN_FULL_DUPLEX_OFF;
502
503         /* 
504          * Set node address 
505          *
506          * We go ahead and put it in the OPB even though on
507          * most of the generic adapters this isn't required.
508          * Its simpler this way.  -- ASF
509          */
510         for (i=0;i<6;i++)
511                 tp->ocpl.NodeAddr[i] = ((unsigned char *)dev->dev_addr)[i];
512
513         tp->ocpl.GroupAddr       = 0;
514         tp->ocpl.FunctAddr       = 0;
515         tp->ocpl.RxListSize      = cpu_to_be16((unsigned short)RplSize);
516         tp->ocpl.TxListSize      = cpu_to_be16((unsigned short)TplSize);
517         tp->ocpl.BufSize         = cpu_to_be16((unsigned short)BufferSize);
518         tp->ocpl.Reserved        = 0;
519         tp->ocpl.TXBufMin        = TX_BUF_MIN;
520         tp->ocpl.TXBufMax        = TX_BUF_MAX;
521
522         Addr = htonl(((char *)tp->ProductID - (char *)tp) + tp->dmabuffer);
523
524         tp->ocpl.ProdIDAddr[0]   = LOWORD(Addr);
525         tp->ocpl.ProdIDAddr[1]   = HIWORD(Addr);
526
527         return;
528 }
529
530 /*
531  * Send OPEN command to adapter
532  */
533 static void tms380tr_open_adapter(struct net_device *dev)
534 {
535         struct net_local *tp = netdev_priv(dev);
536
537         if(tp->OpenCommandIssued)
538                 return;
539
540         tp->OpenCommandIssued = 1;
541         tms380tr_exec_cmd(dev, OC_OPEN);
542
543         return;
544 }
545
546 /*
547  * Clear the adapter's interrupt flag. Clear system interrupt enable
548  * (SINTEN): disable adapter to system interrupts.
549  */
550 static void tms380tr_disable_interrupts(struct net_device *dev)
551 {
552         SIFWRITEB(0, SIFACL);
553
554         return;
555 }
556
557 /*
558  * Set the adapter's interrupt flag. Set system interrupt enable
559  * (SINTEN): enable adapter to system interrupts.
560  */
561 static void tms380tr_enable_interrupts(struct net_device *dev)
562 {
563         SIFWRITEB(ACL_SINTEN, SIFACL);
564
565         return;
566 }
567
568 /*
569  * Put command in command queue, try to execute it.
570  */
571 static void tms380tr_exec_cmd(struct net_device *dev, unsigned short Command)
572 {
573         struct net_local *tp = netdev_priv(dev);
574
575         tp->CMDqueue |= Command;
576         tms380tr_chk_outstanding_cmds(dev);
577
578         return;
579 }
580
581 static void tms380tr_timeout(struct net_device *dev)
582 {
583         /*
584          * If we get here, some higher level has decided we are broken.
585          * There should really be a "kick me" function call instead.
586          *
587          * Resetting the token ring adapter takes a long time so just
588          * fake transmission time and go on trying. Our own timeout
589          * routine is in tms380tr_timer_chk()
590          */
591         dev->trans_start = jiffies;
592         netif_wake_queue(dev);
593 }
594
595 /*
596  * Gets skb from system, queues it and checks if it can be sent
597  */
598 static int tms380tr_send_packet(struct sk_buff *skb, struct net_device *dev)
599 {
600         struct net_local *tp = netdev_priv(dev);
601         int err;
602
603         err = tms380tr_hardware_send_packet(skb, dev);
604         if(tp->TplFree->NextTPLPtr->BusyFlag)
605                 netif_stop_queue(dev);
606         return (err);
607 }
608
609 /*
610  * Move frames into adapter tx queue
611  */
612 static int tms380tr_hardware_send_packet(struct sk_buff *skb, struct net_device *dev)
613 {
614         TPL *tpl;
615         short length;
616         unsigned char *buf;
617         unsigned long flags;
618         int i;
619         dma_addr_t dmabuf, newbuf;
620         struct net_local *tp = netdev_priv(dev);
621    
622         /* Try to get a free TPL from the chain.
623          *
624          * NOTE: We *must* always leave one unused TPL in the chain,
625          * because otherwise the adapter might send frames twice.
626          */
627         spin_lock_irqsave(&tp->lock, flags);
628         if(tp->TplFree->NextTPLPtr->BusyFlag)  { /* No free TPL */
629                 if (tms380tr_debug > 0)
630                         printk(KERN_DEBUG "%s: No free TPL\n", dev->name);
631                 spin_unlock_irqrestore(&tp->lock, flags);
632                 return 1;
633         }
634
635         dmabuf = 0;
636
637         /* Is buffer reachable for Busmaster-DMA? */
638
639         length  = skb->len;
640         dmabuf = dma_map_single(tp->pdev, skb->data, length, DMA_TO_DEVICE);
641         if(tp->dmalimit && (dmabuf + length > tp->dmalimit)) {
642                 /* Copy frame to local buffer */
643                 dma_unmap_single(tp->pdev, dmabuf, length, DMA_TO_DEVICE);
644                 dmabuf  = 0;
645                 i       = tp->TplFree->TPLIndex;
646                 buf     = tp->LocalTxBuffers[i];
647                 memcpy(buf, skb->data, length);
648                 newbuf  = ((char *)buf - (char *)tp) + tp->dmabuffer;
649         }
650         else {
651                 /* Send direct from skb->data */
652                 newbuf  = dmabuf;
653                 buf     = skb->data;
654         }
655         /* Source address in packet? */
656         tms380tr_chk_src_addr(buf, dev->dev_addr);
657         tp->LastSendTime        = jiffies;
658         tpl                     = tp->TplFree;  /* Get the "free" TPL */
659         tpl->BusyFlag           = 1;            /* Mark TPL as busy */
660         tp->TplFree             = tpl->NextTPLPtr;
661     
662         /* Save the skb for delayed return of skb to system */
663         tpl->Skb = skb;
664         tpl->DMABuff = dmabuf;
665         tpl->FragList[0].DataCount = cpu_to_be16((unsigned short)length);
666         tpl->FragList[0].DataAddr  = htonl(newbuf);
667
668         /* Write the data length in the transmit list. */
669         tpl->FrameSize  = cpu_to_be16((unsigned short)length);
670         tpl->MData      = buf;
671
672         /* Transmit the frame and set the status values. */
673         tms380tr_write_tpl_status(tpl, TX_VALID | TX_START_FRAME
674                                 | TX_END_FRAME | TX_PASS_SRC_ADDR
675                                 | TX_FRAME_IRQ);
676
677         /* Let adapter send the frame. */
678         tms380tr_exec_sifcmd(dev, CMD_TX_VALID);
679         spin_unlock_irqrestore(&tp->lock, flags);
680
681         return 0;
682 }
683
684 /*
685  * Write the given value to the 'Status' field of the specified TPL.
686  * NOTE: This function should be used whenever the status of any TPL must be
687  * modified by the driver, because the compiler may otherwise change the
688  * order of instructions such that writing the TPL status may be executed at
689  * an undesireable time. When this function is used, the status is always
690  * written when the function is called.
691  */
692 static void tms380tr_write_tpl_status(TPL *tpl, unsigned int Status)
693 {
694         tpl->Status = Status;
695 }
696
697 static void tms380tr_chk_src_addr(unsigned char *frame, unsigned char *hw_addr)
698 {
699         unsigned char SRBit;
700
701         if((((unsigned long)frame[8]) & ~0x80) != 0)    /* Compare 4 bytes */
702                 return;
703         if((unsigned short)frame[12] != 0)              /* Compare 2 bytes */
704                 return;
705
706         SRBit = frame[8] & 0x80;
707         memcpy(&frame[8], hw_addr, 6);
708         frame[8] |= SRBit;
709
710         return;
711 }
712
713 /*
714  * The timer routine: Check if adapter still open and working, reopen if not. 
715  */
716 static void tms380tr_timer_chk(unsigned long data)
717 {
718         struct net_device *dev = (struct net_device*)data;
719         struct net_local *tp = netdev_priv(dev);
720
721         if(tp->HaltInProgress)
722                 return;
723
724         tms380tr_chk_outstanding_cmds(dev);
725         if(time_before(tp->LastSendTime + SEND_TIMEOUT, jiffies)
726                 && (tp->TplFree != tp->TplBusy))
727         {
728                 /* Anything to send, but stalled too long */
729                 tp->LastSendTime = jiffies;
730                 tms380tr_exec_cmd(dev, OC_CLOSE);       /* Does reopen automatically */
731         }
732
733         tp->timer.expires = jiffies + 2*HZ;
734         add_timer(&tp->timer);
735
736         if(tp->AdapterOpenFlag || tp->ReOpenInProgress)
737                 return;
738         tp->ReOpenInProgress = 1;
739         tms380tr_open_adapter(dev);
740
741         return;
742 }
743
744 /*
745  * The typical workload of the driver: Handle the network interface interrupts.
746  */
747 irqreturn_t tms380tr_interrupt(int irq, void *dev_id, struct pt_regs *regs)
748 {
749         struct net_device *dev = dev_id;
750         struct net_local *tp;
751         unsigned short irq_type;
752         int handled = 0;
753
754         if(dev == NULL) {
755                 printk(KERN_INFO "%s: irq %d for unknown device.\n", dev->name, irq);
756                 return IRQ_NONE;
757         }
758
759         tp = netdev_priv(dev);
760
761         irq_type = SIFREADW(SIFSTS);
762
763         while(irq_type & STS_SYSTEM_IRQ) {
764                 handled = 1;
765                 irq_type &= STS_IRQ_MASK;
766
767                 if(!tms380tr_chk_ssb(tp, irq_type)) {
768                         printk(KERN_DEBUG "%s: DATA LATE occurred\n", dev->name);
769                         break;
770                 }
771
772                 switch(irq_type) {
773                 case STS_IRQ_RECEIVE_STATUS:
774                         tms380tr_reset_interrupt(dev);
775                         tms380tr_rcv_status_irq(dev);
776                         break;
777
778                 case STS_IRQ_TRANSMIT_STATUS:
779                         /* Check if TRANSMIT.HALT command is complete */
780                         if(tp->ssb.Parm[0] & COMMAND_COMPLETE) {
781                                 tp->TransmitCommandActive = 0;
782                                         tp->TransmitHaltScheduled = 0;
783
784                                         /* Issue a new transmit command. */
785                                         tms380tr_exec_cmd(dev, OC_TRANSMIT);
786                                 }
787
788                                 tms380tr_reset_interrupt(dev);
789                                 tms380tr_tx_status_irq(dev);
790                                 break;
791
792                 case STS_IRQ_COMMAND_STATUS:
793                         /* The SSB contains status of last command
794                          * other than receive/transmit.
795                          */
796                         tms380tr_cmd_status_irq(dev);
797                         break;
798                         
799                 case STS_IRQ_SCB_CLEAR:
800                         /* The SCB is free for another command. */
801                         tp->ScbInUse = 0;
802                         tms380tr_chk_outstanding_cmds(dev);
803                         break;
804                         
805                 case STS_IRQ_RING_STATUS:
806                         tms380tr_ring_status_irq(dev);
807                         break;
808
809                 case STS_IRQ_ADAPTER_CHECK:
810                         tms380tr_chk_irq(dev);
811                         break;
812
813                 case STS_IRQ_LLC_STATUS:
814                         printk(KERN_DEBUG "tms380tr: unexpected LLC status IRQ\n");
815                         break;
816                         
817                 case STS_IRQ_TIMER:
818                         printk(KERN_DEBUG "tms380tr: unexpected Timer IRQ\n");
819                         break;
820                         
821                 case STS_IRQ_RECEIVE_PENDING:
822                         printk(KERN_DEBUG "tms380tr: unexpected Receive Pending IRQ\n");
823                         break;
824                         
825                 default:
826                         printk(KERN_DEBUG "Unknown Token Ring IRQ (0x%04x)\n", irq_type);
827                         break;
828                 }
829
830                 /* Reset system interrupt if not already done. */
831                 if(irq_type != STS_IRQ_TRANSMIT_STATUS
832                         && irq_type != STS_IRQ_RECEIVE_STATUS) {
833                         tms380tr_reset_interrupt(dev);
834                 }
835
836                 irq_type = SIFREADW(SIFSTS);
837         }
838
839         return IRQ_RETVAL(handled);
840 }
841
842 /*
843  *  Reset the INTERRUPT SYSTEM bit and issue SSB CLEAR command.
844  */
845 static void tms380tr_reset_interrupt(struct net_device *dev)
846 {
847         struct net_local *tp = netdev_priv(dev);
848         SSB *ssb = &tp->ssb;
849
850         /*
851          * [Workaround for "Data Late"]
852          * Set all fields of the SSB to well-defined values so we can
853          * check if the adapter has written the SSB.
854          */
855
856         ssb->STS        = (unsigned short) -1;
857         ssb->Parm[0]    = (unsigned short) -1;
858         ssb->Parm[1]    = (unsigned short) -1;
859         ssb->Parm[2]    = (unsigned short) -1;
860
861         /* Free SSB by issuing SSB_CLEAR command after reading IRQ code
862          * and clear STS_SYSTEM_IRQ bit: enable adapter for further interrupts.
863          */
864         tms380tr_exec_sifcmd(dev, CMD_SSB_CLEAR | CMD_CLEAR_SYSTEM_IRQ);
865
866         return;
867 }
868
869 /*
870  * Check if the SSB has actually been written by the adapter.
871  */
872 static unsigned char tms380tr_chk_ssb(struct net_local *tp, unsigned short IrqType)
873 {
874         SSB *ssb = &tp->ssb;    /* The address of the SSB. */
875
876         /* C 0 1 2 INTERRUPT CODE
877          * - - - - --------------
878          * 1 1 1 1 TRANSMIT STATUS
879          * 1 1 1 1 RECEIVE STATUS
880          * 1 ? ? 0 COMMAND STATUS
881          * 0 0 0 0 SCB CLEAR
882          * 1 1 0 0 RING STATUS
883          * 0 0 0 0 ADAPTER CHECK
884          *
885          * 0 = SSB field not affected by interrupt
886          * 1 = SSB field is affected by interrupt
887          *
888          * C = SSB ADDRESS +0: COMMAND
889          * 0 = SSB ADDRESS +2: STATUS 0
890          * 1 = SSB ADDRESS +4: STATUS 1
891          * 2 = SSB ADDRESS +6: STATUS 2
892          */
893
894         /* Check if this interrupt does use the SSB. */
895
896         if(IrqType != STS_IRQ_TRANSMIT_STATUS
897                 && IrqType != STS_IRQ_RECEIVE_STATUS
898                 && IrqType != STS_IRQ_COMMAND_STATUS
899                 && IrqType != STS_IRQ_RING_STATUS)
900         {
901                 return (1);     /* SSB not involved. */
902         }
903
904         /* Note: All fields of the SSB have been set to all ones (-1) after it
905          * has last been used by the software (see DriverIsr()).
906          *
907          * Check if the affected SSB fields are still unchanged.
908          */
909
910         if(ssb->STS == (unsigned short) -1)
911                 return (0);     /* Command field not yet available. */
912         if(IrqType == STS_IRQ_COMMAND_STATUS)
913                 return (1);     /* Status fields not always affected. */
914         if(ssb->Parm[0] == (unsigned short) -1)
915                 return (0);     /* Status 1 field not yet available. */
916         if(IrqType == STS_IRQ_RING_STATUS)
917                 return (1);     /* Status 2 & 3 fields not affected. */
918
919         /* Note: At this point, the interrupt is either TRANSMIT or RECEIVE. */
920         if(ssb->Parm[1] == (unsigned short) -1)
921                 return (0);     /* Status 2 field not yet available. */
922         if(ssb->Parm[2] == (unsigned short) -1)
923                 return (0);     /* Status 3 field not yet available. */
924
925         return (1);     /* All SSB fields have been written by the adapter. */
926 }
927
928 /*
929  * Evaluates the command results status in the SSB status field.
930  */
931 static void tms380tr_cmd_status_irq(struct net_device *dev)
932 {
933         struct net_local *tp = netdev_priv(dev);
934         unsigned short ssb_cmd, ssb_parm_0;
935         unsigned short ssb_parm_1;
936         char *open_err = "Open error -";
937         char *code_err = "Open code -";
938
939         /* Copy the ssb values to local variables */
940         ssb_cmd    = tp->ssb.STS;
941         ssb_parm_0 = tp->ssb.Parm[0];
942         ssb_parm_1 = tp->ssb.Parm[1];
943
944         if(ssb_cmd == OPEN)
945         {
946                 tp->Sleeping = 0;
947                 if(!tp->ReOpenInProgress)
948                         wake_up_interruptible(&tp->wait_for_tok_int);
949
950                 tp->OpenCommandIssued = 0;
951                 tp->ScbInUse = 0;
952
953                 if((ssb_parm_0 & 0x00FF) == GOOD_COMPLETION)
954                 {
955                         /* Success, the adapter is open. */
956                         tp->LobeWireFaultLogged = 0;
957                         tp->AdapterOpenFlag     = 1;
958                         tp->AdapterVirtOpenFlag = 1;
959                         tp->TransmitCommandActive = 0;
960                         tms380tr_exec_cmd(dev, OC_TRANSMIT);
961                         tms380tr_exec_cmd(dev, OC_RECEIVE);
962
963                         if(tp->ReOpenInProgress)
964                                 tp->ReOpenInProgress = 0;
965
966                         return;
967                 }
968                 else    /* The adapter did not open. */
969                 {
970                         if(ssb_parm_0 & NODE_ADDR_ERROR)
971                                 printk(KERN_INFO "%s: Node address error\n",
972                                         dev->name);
973                         if(ssb_parm_0 & LIST_SIZE_ERROR)
974                                 printk(KERN_INFO "%s: List size error\n",
975                                         dev->name);
976                         if(ssb_parm_0 & BUF_SIZE_ERROR)
977                                 printk(KERN_INFO "%s: Buffer size error\n",
978                                         dev->name);
979                         if(ssb_parm_0 & TX_BUF_COUNT_ERROR)
980                                 printk(KERN_INFO "%s: Tx buffer count error\n",
981                                         dev->name);
982                         if(ssb_parm_0 & INVALID_OPEN_OPTION)
983                                 printk(KERN_INFO "%s: Invalid open option\n",
984                                         dev->name);
985                         if(ssb_parm_0 & OPEN_ERROR)
986                         {
987                                 /* Show the open phase. */
988                                 switch(ssb_parm_0 & OPEN_PHASES_MASK)
989                                 {
990                                         case LOBE_MEDIA_TEST:
991                                                 if(!tp->LobeWireFaultLogged)
992                                                 {
993                                                         tp->LobeWireFaultLogged = 1;
994                                                         printk(KERN_INFO "%s: %s Lobe wire fault (check cable !).\n", dev->name, open_err);
995                                                 }
996                                                 tp->ReOpenInProgress    = 1;
997                                                 tp->AdapterOpenFlag     = 0;
998                                                 tp->AdapterVirtOpenFlag = 1;
999                                                 tms380tr_open_adapter(dev);
1000                                                 return;
1001
1002                                         case PHYSICAL_INSERTION:
1003                                                 printk(KERN_INFO "%s: %s Physical insertion.\n", dev->name, open_err);
1004                                                 break;
1005
1006                                         case ADDRESS_VERIFICATION:
1007                                                 printk(KERN_INFO "%s: %s Address verification.\n", dev->name, open_err);
1008                                                 break;
1009
1010                                         case PARTICIPATION_IN_RING_POLL:
1011                                                 printk(KERN_INFO "%s: %s Participation in ring poll.\n", dev->name, open_err);
1012                                                 break;
1013
1014                                         case REQUEST_INITIALISATION:
1015                                                 printk(KERN_INFO "%s: %s Request initialisation.\n", dev->name, open_err);
1016                                                 break;
1017
1018                                         case FULLDUPLEX_CHECK:
1019                                                 printk(KERN_INFO "%s: %s Full duplex check.\n", dev->name, open_err);
1020                                                 break;
1021
1022                                         default:
1023                                                 printk(KERN_INFO "%s: %s Unknown open phase\n", dev->name, open_err);
1024                                                 break;
1025                                 }
1026
1027                                 /* Show the open errors. */
1028                                 switch(ssb_parm_0 & OPEN_ERROR_CODES_MASK)
1029                                 {
1030                                         case OPEN_FUNCTION_FAILURE:
1031                                                 printk(KERN_INFO "%s: %s OPEN_FUNCTION_FAILURE", dev->name, code_err);
1032                                                 tp->LastOpenStatus =
1033                                                         OPEN_FUNCTION_FAILURE;
1034                                                 break;
1035
1036                                         case OPEN_SIGNAL_LOSS:
1037                                                 printk(KERN_INFO "%s: %s OPEN_SIGNAL_LOSS\n", dev->name, code_err);
1038                                                 tp->LastOpenStatus =
1039                                                         OPEN_SIGNAL_LOSS;
1040                                                 break;
1041
1042                                         case OPEN_TIMEOUT:
1043                                                 printk(KERN_INFO "%s: %s OPEN_TIMEOUT\n", dev->name, code_err);
1044                                                 tp->LastOpenStatus =
1045                                                         OPEN_TIMEOUT;
1046                                                 break;
1047
1048                                         case OPEN_RING_FAILURE:
1049                                                 printk(KERN_INFO "%s: %s OPEN_RING_FAILURE\n", dev->name, code_err);
1050                                                 tp->LastOpenStatus =
1051                                                         OPEN_RING_FAILURE;
1052                                                 break;
1053
1054                                         case OPEN_RING_BEACONING:
1055                                                 printk(KERN_INFO "%s: %s OPEN_RING_BEACONING\n", dev->name, code_err);
1056                                                 tp->LastOpenStatus =
1057                                                         OPEN_RING_BEACONING;
1058                                                 break;
1059
1060                                         case OPEN_DUPLICATE_NODEADDR:
1061                                                 printk(KERN_INFO "%s: %s OPEN_DUPLICATE_NODEADDR\n", dev->name, code_err);
1062                                                 tp->LastOpenStatus =
1063                                                         OPEN_DUPLICATE_NODEADDR;
1064                                                 break;
1065
1066                                         case OPEN_REQUEST_INIT:
1067                                                 printk(KERN_INFO "%s: %s OPEN_REQUEST_INIT\n", dev->name, code_err);
1068                                                 tp->LastOpenStatus =
1069                                                         OPEN_REQUEST_INIT;
1070                                                 break;
1071
1072                                         case OPEN_REMOVE_RECEIVED:
1073                                                 printk(KERN_INFO "%s: %s OPEN_REMOVE_RECEIVED", dev->name, code_err);
1074                                                 tp->LastOpenStatus =
1075                                                         OPEN_REMOVE_RECEIVED;
1076                                                 break;
1077
1078                                         case OPEN_FULLDUPLEX_SET:
1079                                                 printk(KERN_INFO "%s: %s OPEN_FULLDUPLEX_SET\n", dev->name, code_err);
1080                                                 tp->LastOpenStatus =
1081                                                         OPEN_FULLDUPLEX_SET;
1082                                                 break;
1083
1084                                         default:
1085                                                 printk(KERN_INFO "%s: %s Unknown open err code", dev->name, code_err);
1086                                                 tp->LastOpenStatus =
1087                                                         OPEN_FUNCTION_FAILURE;
1088                                                 break;
1089                                 }
1090                         }
1091
1092                         tp->AdapterOpenFlag     = 0;
1093                         tp->AdapterVirtOpenFlag = 0;
1094
1095                         return;
1096                 }
1097         }
1098         else
1099         {
1100                 if(ssb_cmd != READ_ERROR_LOG)
1101                         return;
1102
1103                 /* Add values from the error log table to the MAC
1104                  * statistics counters and update the errorlogtable
1105                  * memory.
1106                  */
1107                 tp->MacStat.line_errors += tp->errorlogtable.Line_Error;
1108                 tp->MacStat.burst_errors += tp->errorlogtable.Burst_Error;
1109                 tp->MacStat.A_C_errors += tp->errorlogtable.ARI_FCI_Error;
1110                 tp->MacStat.lost_frames += tp->errorlogtable.Lost_Frame_Error;
1111                 tp->MacStat.recv_congest_count += tp->errorlogtable.Rx_Congest_Error;
1112                 tp->MacStat.rx_errors += tp->errorlogtable.Rx_Congest_Error;
1113                 tp->MacStat.frame_copied_errors += tp->errorlogtable.Frame_Copied_Error;
1114                 tp->MacStat.token_errors += tp->errorlogtable.Token_Error;
1115                 tp->MacStat.dummy1 += tp->errorlogtable.DMA_Bus_Error;
1116                 tp->MacStat.dummy1 += tp->errorlogtable.DMA_Parity_Error;
1117                 tp->MacStat.abort_delimiters += tp->errorlogtable.AbortDelimeters;
1118                 tp->MacStat.frequency_errors += tp->errorlogtable.Frequency_Error;
1119                 tp->MacStat.internal_errors += tp->errorlogtable.Internal_Error;
1120         }
1121
1122         return;
1123 }
1124
1125 /*
1126  * The inverse routine to tms380tr_open().
1127  */
1128 int tms380tr_close(struct net_device *dev)
1129 {
1130         struct net_local *tp = netdev_priv(dev);
1131         netif_stop_queue(dev);
1132         
1133         del_timer(&tp->timer);
1134
1135         /* Flush the Tx and disable Rx here. */
1136
1137         tp->HaltInProgress      = 1;
1138         tms380tr_exec_cmd(dev, OC_CLOSE);
1139         tp->timer.expires       = jiffies + 1*HZ;
1140         tp->timer.function      = tms380tr_timer_end_wait;
1141         tp->timer.data          = (unsigned long)dev;
1142         add_timer(&tp->timer);
1143
1144         tms380tr_enable_interrupts(dev);
1145
1146         tp->Sleeping = 1;
1147         interruptible_sleep_on(&tp->wait_for_tok_int);
1148         tp->TransmitCommandActive = 0;
1149     
1150         del_timer(&tp->timer);
1151         tms380tr_disable_interrupts(dev);
1152    
1153 #ifdef CONFIG_ISA
1154         if(dev->dma > 0) 
1155         {
1156                 unsigned long flags=claim_dma_lock();
1157                 disable_dma(dev->dma);
1158                 release_dma_lock(flags);
1159         }
1160 #endif
1161         
1162         SIFWRITEW(0xFF00, SIFCMD);
1163 #if 0
1164         if(dev->dma > 0) /* what the? */
1165                 SIFWRITEB(0xff, POSREG);
1166 #endif
1167         tms380tr_cancel_tx_queue(tp);
1168
1169         return (0);
1170 }
1171
1172 /*
1173  * Get the current statistics. This may be called with the card open
1174  * or closed.
1175  */
1176 static struct net_device_stats *tms380tr_get_stats(struct net_device *dev)
1177 {
1178         struct net_local *tp = netdev_priv(dev);
1179
1180         return ((struct net_device_stats *)&tp->MacStat);
1181 }
1182
1183 /*
1184  * Set or clear the multicast filter for this adapter.
1185  */
1186 static void tms380tr_set_multicast_list(struct net_device *dev)
1187 {
1188         struct net_local *tp = netdev_priv(dev);
1189         unsigned int OpenOptions;
1190         
1191         OpenOptions = tp->ocpl.OPENOptions &
1192                 ~(PASS_ADAPTER_MAC_FRAMES
1193                   | PASS_ATTENTION_FRAMES
1194                   | PASS_BEACON_MAC_FRAMES
1195                   | COPY_ALL_MAC_FRAMES
1196                   | COPY_ALL_NON_MAC_FRAMES);
1197         
1198         tp->ocpl.FunctAddr = 0;
1199         
1200         if(dev->flags & IFF_PROMISC)
1201                 /* Enable promiscuous mode */
1202                 OpenOptions |= COPY_ALL_NON_MAC_FRAMES |
1203                         COPY_ALL_MAC_FRAMES;
1204         else
1205         {
1206                 if(dev->flags & IFF_ALLMULTI)
1207                 {
1208                         /* Disable promiscuous mode, use normal mode. */
1209                         tp->ocpl.FunctAddr = 0xFFFFFFFF;
1210                 }
1211                 else
1212                 {
1213                         int i;
1214                         struct dev_mc_list *mclist = dev->mc_list;
1215                         for (i=0; i< dev->mc_count; i++)
1216                         {
1217                                 ((char *)(&tp->ocpl.FunctAddr))[0] |=
1218                                         mclist->dmi_addr[2];
1219                                 ((char *)(&tp->ocpl.FunctAddr))[1] |=
1220                                         mclist->dmi_addr[3];
1221                                 ((char *)(&tp->ocpl.FunctAddr))[2] |=
1222                                         mclist->dmi_addr[4];
1223                                 ((char *)(&tp->ocpl.FunctAddr))[3] |=
1224                                         mclist->dmi_addr[5];
1225                                 mclist = mclist->next;
1226                         }
1227                 }
1228                 tms380tr_exec_cmd(dev, OC_SET_FUNCT_ADDR);
1229         }
1230         
1231         tp->ocpl.OPENOptions = OpenOptions;
1232         tms380tr_exec_cmd(dev, OC_MODIFY_OPEN_PARMS);
1233         return;
1234 }
1235
1236 /*
1237  * Wait for some time (microseconds)
1238  */
1239 void tms380tr_wait(unsigned long time)
1240 {
1241 #if 0
1242         long tmp;
1243         
1244         tmp = jiffies + time/(1000000/HZ);
1245         do {
1246                 tmp = schedule_timeout_interruptible(tmp);
1247         } while(time_after(tmp, jiffies));
1248 #else
1249         udelay(time);
1250 #endif
1251         return;
1252 }
1253
1254 /*
1255  * Write a command value to the SIFCMD register
1256  */
1257 static void tms380tr_exec_sifcmd(struct net_device *dev, unsigned int WriteValue)
1258 {
1259         unsigned short cmd;
1260         unsigned short SifStsValue;
1261         unsigned long loop_counter;
1262
1263         WriteValue = ((WriteValue ^ CMD_SYSTEM_IRQ) | CMD_INTERRUPT_ADAPTER);
1264         cmd = (unsigned short)WriteValue;
1265         loop_counter = 0,5 * 800000;
1266         do {
1267                 SifStsValue = SIFREADW(SIFSTS);
1268         } while((SifStsValue & CMD_INTERRUPT_ADAPTER) && loop_counter--);
1269         SIFWRITEW(cmd, SIFCMD);
1270
1271         return;
1272 }
1273
1274 /*
1275  * Processes adapter hardware reset, halts adapter and downloads firmware,
1276  * clears the halt bit.
1277  */
1278 static int tms380tr_reset_adapter(struct net_device *dev)
1279 {
1280         struct net_local *tp = netdev_priv(dev);
1281         unsigned short *fw_ptr;
1282         unsigned short count, c, count2;
1283         const struct firmware *fw_entry = NULL;
1284
1285         if (request_firmware(&fw_entry, "tms380tr.bin", tp->pdev) != 0) {
1286                 printk(KERN_ALERT "%s: firmware %s is missing, cannot start.\n",
1287                         dev->name, "tms380tr.bin");
1288                 return (-1);
1289         }
1290
1291         fw_ptr = (unsigned short *)fw_entry->data;
1292         count2 = fw_entry->size / 2;
1293
1294         /* Hardware adapter reset */
1295         SIFWRITEW(ACL_ARESET, SIFACL);
1296         tms380tr_wait(40);
1297         
1298         c = SIFREADW(SIFACL);
1299         tms380tr_wait(20);
1300
1301         if(dev->dma == 0)       /* For PCI adapters */
1302         {
1303                 c &= ~(ACL_NSELOUT0 | ACL_NSELOUT1);    /* Clear bits */
1304                 if(tp->setnselout)
1305                   c |= (*tp->setnselout)(dev);
1306         }
1307
1308         /* In case a command is pending - forget it */
1309         tp->ScbInUse = 0;
1310
1311         c &= ~ACL_ARESET;               /* Clear adapter reset bit */
1312         c |=  ACL_CPHALT;               /* Halt adapter CPU, allow download */
1313         c |= ACL_BOOT;
1314         c |= ACL_SINTEN;
1315         c &= ~ACL_PSDMAEN;              /* Clear pseudo dma bit */
1316         SIFWRITEW(c, SIFACL);
1317         tms380tr_wait(40);
1318
1319         count = 0;
1320         /* Download firmware via DIO interface: */
1321         do {
1322                 if (count2 < 3) continue;
1323
1324                 /* Download first address part */
1325                 SIFWRITEW(*fw_ptr, SIFADX);
1326                 fw_ptr++;
1327                 count2--;
1328                 /* Download second address part */
1329                 SIFWRITEW(*fw_ptr, SIFADD);
1330                 fw_ptr++;
1331                 count2--;
1332
1333                 if((count = *fw_ptr) != 0)      /* Load loop counter */
1334                 {
1335                         fw_ptr++;       /* Download block data */
1336                         count2--;
1337                         if (count > count2) continue;
1338
1339                         for(; count > 0; count--)
1340                         {
1341                                 SIFWRITEW(*fw_ptr, SIFINC);
1342                                 fw_ptr++;
1343                                 count2--;
1344                         }
1345                 }
1346                 else    /* Stop, if last block downloaded */
1347                 {
1348                         c = SIFREADW(SIFACL);
1349                         c &= (~ACL_CPHALT | ACL_SINTEN);
1350
1351                         /* Clear CPHALT and start BUD */
1352                         SIFWRITEW(c, SIFACL);
1353                         if (fw_entry)
1354                                 release_firmware(fw_entry);
1355                         return (1);
1356                 }
1357         } while(count == 0);
1358
1359         if (fw_entry)
1360                 release_firmware(fw_entry);
1361         printk(KERN_INFO "%s: Adapter Download Failed\n", dev->name);
1362         return (-1);
1363 }
1364
1365 /*
1366  * Starts bring up diagnostics of token ring adapter and evaluates
1367  * diagnostic results.
1368  */
1369 static int tms380tr_bringup_diags(struct net_device *dev)
1370 {
1371         int loop_cnt, retry_cnt;
1372         unsigned short Status;
1373
1374         tms380tr_wait(HALF_SECOND);
1375         tms380tr_exec_sifcmd(dev, EXEC_SOFT_RESET);
1376         tms380tr_wait(HALF_SECOND);
1377
1378         retry_cnt = BUD_MAX_RETRIES;    /* maximal number of retrys */
1379
1380         do {
1381                 retry_cnt--;
1382                 if(tms380tr_debug > 3)
1383                         printk(KERN_DEBUG "BUD-Status: ");
1384                 loop_cnt = BUD_MAX_LOOPCNT;     /* maximum: three seconds*/
1385                 do {                    /* Inspect BUD results */
1386                         loop_cnt--;
1387                         tms380tr_wait(HALF_SECOND);
1388                         Status = SIFREADW(SIFSTS);
1389                         Status &= STS_MASK;
1390
1391                         if(tms380tr_debug > 3)
1392                                 printk(KERN_DEBUG " %04X \n", Status);
1393                         /* BUD successfully completed */
1394                         if(Status == STS_INITIALIZE)
1395                                 return (1);
1396                 /* Unrecoverable hardware error, BUD not completed? */
1397                 } while((loop_cnt > 0) && ((Status & (STS_ERROR | STS_TEST))
1398                         != (STS_ERROR | STS_TEST)));
1399
1400                 /* Error preventing completion of BUD */
1401                 if(retry_cnt > 0)
1402                 {
1403                         printk(KERN_INFO "%s: Adapter Software Reset.\n", 
1404                                 dev->name);
1405                         tms380tr_exec_sifcmd(dev, EXEC_SOFT_RESET);
1406                         tms380tr_wait(HALF_SECOND);
1407                 }
1408         } while(retry_cnt > 0);
1409
1410         Status = SIFREADW(SIFSTS);
1411         
1412         printk(KERN_INFO "%s: Hardware error\n", dev->name);
1413         /* Hardware error occurred! */
1414         Status &= 0x001f;
1415         if (Status & 0x0010)
1416                 printk(KERN_INFO "%s: BUD Error: Timeout\n", dev->name);
1417         else if ((Status & 0x000f) > 6)
1418                 printk(KERN_INFO "%s: BUD Error: Illegal Failure\n", dev->name);
1419         else
1420                 printk(KERN_INFO "%s: Bring Up Diagnostics Error (%04X) occurred\n", dev->name, Status & 0x000f);
1421
1422         return (-1);
1423 }
1424
1425 /*
1426  * Copy initialisation data to adapter memory, beginning at address
1427  * 1:0A00; Starting DMA test and evaluating result bits.
1428  */
1429 static int tms380tr_init_adapter(struct net_device *dev)
1430 {
1431         struct net_local *tp = netdev_priv(dev);
1432
1433         const unsigned char SCB_Test[6] = {0x00, 0x00, 0xC1, 0xE2, 0xD4, 0x8B};
1434         const unsigned char SSB_Test[8] = {0xFF, 0xFF, 0xD1, 0xD7,
1435                                                 0xC5, 0xD9, 0xC3, 0xD4};
1436         void *ptr = (void *)&tp->ipb;
1437         unsigned short *ipb_ptr = (unsigned short *)ptr;
1438         unsigned char *cb_ptr = (unsigned char *) &tp->scb;
1439         unsigned char *sb_ptr = (unsigned char *) &tp->ssb;
1440         unsigned short Status;
1441         int i, loop_cnt, retry_cnt;
1442
1443         /* Normalize: byte order low/high, word order high/low! (only IPB!) */
1444         tp->ipb.SCB_Addr = SWAPW(((char *)&tp->scb - (char *)tp) + tp->dmabuffer);
1445         tp->ipb.SSB_Addr = SWAPW(((char *)&tp->ssb - (char *)tp) + tp->dmabuffer);
1446
1447         if(tms380tr_debug > 3)
1448         {
1449                 printk(KERN_DEBUG "%s: buffer (real): %lx\n", dev->name, (long) &tp->scb);
1450                 printk(KERN_DEBUG "%s: buffer (virt): %lx\n", dev->name, (long) ((char *)&tp->scb - (char *)tp) + (long) tp->dmabuffer);
1451                 printk(KERN_DEBUG "%s: buffer (DMA) : %lx\n", dev->name, (long) tp->dmabuffer);
1452                 printk(KERN_DEBUG "%s: buffer (tp)  : %lx\n", dev->name, (long) tp);
1453         }
1454         /* Maximum: three initialization retries */
1455         retry_cnt = INIT_MAX_RETRIES;
1456
1457         do {
1458                 retry_cnt--;
1459
1460                 /* Transfer initialization block */
1461                 SIFWRITEW(0x0001, SIFADX);
1462
1463                 /* To address 0001:0A00 of adapter RAM */
1464                 SIFWRITEW(0x0A00, SIFADD);
1465
1466                 /* Write 11 words to adapter RAM */
1467                 for(i = 0; i < 11; i++)
1468                         SIFWRITEW(ipb_ptr[i], SIFINC);
1469
1470                 /* Execute SCB adapter command */
1471                 tms380tr_exec_sifcmd(dev, CMD_EXECUTE);
1472
1473                 loop_cnt = INIT_MAX_LOOPCNT;    /* Maximum: 11 seconds */
1474
1475                 /* While remaining retries, no error and not completed */
1476                 do {
1477                         Status = 0;
1478                         loop_cnt--;
1479                         tms380tr_wait(HALF_SECOND);
1480
1481                         /* Mask interesting status bits */
1482                         Status = SIFREADW(SIFSTS);
1483                         Status &= STS_MASK;
1484                 } while(((Status &(STS_INITIALIZE | STS_ERROR | STS_TEST)) != 0)
1485                         && ((Status & STS_ERROR) == 0) && (loop_cnt != 0));
1486
1487                 if((Status & (STS_INITIALIZE | STS_ERROR | STS_TEST)) == 0)
1488                 {
1489                         /* Initialization completed without error */
1490                         i = 0;
1491                         do {    /* Test if contents of SCB is valid */
1492                                 if(SCB_Test[i] != *(cb_ptr + i))
1493                                 {
1494                                         printk(KERN_INFO "%s: DMA failed\n", dev->name);
1495                                         /* DMA data error: wrong data in SCB */
1496                                         return (-1);
1497                                 }
1498                                 i++;
1499                         } while(i < 6);
1500
1501                         i = 0;
1502                         do {    /* Test if contents of SSB is valid */
1503                                 if(SSB_Test[i] != *(sb_ptr + i))
1504                                         /* DMA data error: wrong data in SSB */
1505                                         return (-1);
1506                                 i++;
1507                         } while (i < 8);
1508
1509                         return (1);     /* Adapter successfully initialized */
1510                 }
1511                 else
1512                 {
1513                         if((Status & STS_ERROR) != 0)
1514                         {
1515                                 /* Initialization error occurred */
1516                                 Status = SIFREADW(SIFSTS);
1517                                 Status &= STS_ERROR_MASK;
1518                                 /* ShowInitialisationErrorCode(Status); */
1519                                 printk(KERN_INFO "%s: Status error: %d\n", dev->name, Status);
1520                                 return (-1); /* Unrecoverable error */
1521                         }
1522                         else
1523                         {
1524                                 if(retry_cnt > 0)
1525                                 {
1526                                         /* Reset adapter and try init again */
1527                                         tms380tr_exec_sifcmd(dev, EXEC_SOFT_RESET);
1528                                         tms380tr_wait(HALF_SECOND);
1529                                 }
1530                         }
1531                 }
1532         } while(retry_cnt > 0);
1533
1534         printk(KERN_INFO "%s: Retry exceeded\n", dev->name);
1535         return (-1);
1536 }
1537
1538 /*
1539  * Check for outstanding commands in command queue and tries to execute
1540  * command immediately. Corresponding command flag in command queue is cleared.
1541  */
1542 static void tms380tr_chk_outstanding_cmds(struct net_device *dev)
1543 {
1544         struct net_local *tp = netdev_priv(dev);
1545         unsigned long Addr = 0;
1546
1547         if(tp->CMDqueue == 0)
1548                 return;         /* No command execution */
1549
1550         /* If SCB in use: no command */
1551         if(tp->ScbInUse == 1)
1552                 return;
1553
1554         /* Check if adapter is opened, avoiding COMMAND_REJECT
1555          * interrupt by the adapter!
1556          */
1557         if(tp->AdapterOpenFlag == 0)
1558         {
1559                 if(tp->CMDqueue & OC_OPEN)
1560                 {
1561                         /* Execute OPEN command */
1562                         tp->CMDqueue ^= OC_OPEN;
1563
1564                         Addr = htonl(((char *)&tp->ocpl - (char *)tp) + tp->dmabuffer);
1565                         tp->scb.Parm[0] = LOWORD(Addr);
1566                         tp->scb.Parm[1] = HIWORD(Addr);
1567                         tp->scb.CMD = OPEN;
1568                 }
1569                 else
1570                         /* No OPEN command queued, but adapter closed. Note:
1571                          * We'll try to re-open the adapter in DriverPoll()
1572                          */
1573                         return;         /* No adapter command issued */
1574         }
1575         else
1576         {
1577                 /* Adapter is open; evaluate command queue: try to execute
1578                  * outstanding commands (depending on priority!) CLOSE
1579                  * command queued
1580                  */
1581                 if(tp->CMDqueue & OC_CLOSE)
1582                 {
1583                         tp->CMDqueue ^= OC_CLOSE;
1584                         tp->AdapterOpenFlag = 0;
1585                         tp->scb.Parm[0] = 0; /* Parm[0], Parm[1] are ignored */
1586                         tp->scb.Parm[1] = 0; /* but should be set to zero! */
1587                         tp->scb.CMD = CLOSE;
1588                         if(!tp->HaltInProgress)
1589                                 tp->CMDqueue |= OC_OPEN; /* re-open adapter */
1590                         else
1591                                 tp->CMDqueue = 0;       /* no more commands */
1592                 }
1593                 else
1594                 {
1595                         if(tp->CMDqueue & OC_RECEIVE)
1596                         {
1597                                 tp->CMDqueue ^= OC_RECEIVE;
1598                                 Addr = htonl(((char *)tp->RplHead - (char *)tp) + tp->dmabuffer);
1599                                 tp->scb.Parm[0] = LOWORD(Addr);
1600                                 tp->scb.Parm[1] = HIWORD(Addr);
1601                                 tp->scb.CMD = RECEIVE;
1602                         }
1603                         else
1604                         {
1605                                 if(tp->CMDqueue & OC_TRANSMIT_HALT)
1606                                 {
1607                                         /* NOTE: TRANSMIT.HALT must be checked 
1608                                          * before TRANSMIT.
1609                                          */
1610                                         tp->CMDqueue ^= OC_TRANSMIT_HALT;
1611                                         tp->scb.CMD = TRANSMIT_HALT;
1612
1613                                         /* Parm[0] and Parm[1] are ignored
1614                                          * but should be set to zero!
1615                                          */
1616                                         tp->scb.Parm[0] = 0;
1617                                         tp->scb.Parm[1] = 0;
1618                                 }
1619                                 else
1620                                 {
1621                                         if(tp->CMDqueue & OC_TRANSMIT)
1622                                         {
1623                                                 /* NOTE: TRANSMIT must be 
1624                                                  * checked after TRANSMIT.HALT
1625                                                  */
1626                                                 if(tp->TransmitCommandActive)
1627                                                 {
1628                                                         if(!tp->TransmitHaltScheduled)
1629                                                         {
1630                                                                 tp->TransmitHaltScheduled = 1;
1631                                                                 tms380tr_exec_cmd(dev, OC_TRANSMIT_HALT) ;
1632                                                         }
1633                                                         tp->TransmitCommandActive = 0;
1634                                                         return;
1635                                                 }
1636
1637                                                 tp->CMDqueue ^= OC_TRANSMIT;
1638                                                 tms380tr_cancel_tx_queue(tp);
1639                                                 Addr = htonl(((char *)tp->TplBusy - (char *)tp) + tp->dmabuffer);
1640                                                 tp->scb.Parm[0] = LOWORD(Addr);
1641                                                 tp->scb.Parm[1] = HIWORD(Addr);
1642                                                 tp->scb.CMD = TRANSMIT;
1643                                                 tp->TransmitCommandActive = 1;
1644                                         }
1645                                         else
1646                                         {
1647                                                 if(tp->CMDqueue & OC_MODIFY_OPEN_PARMS)
1648                                                 {
1649                                                         tp->CMDqueue ^= OC_MODIFY_OPEN_PARMS;
1650                                                         tp->scb.Parm[0] = tp->ocpl.OPENOptions; /* new OPEN options*/
1651                                                         tp->scb.Parm[0] |= ENABLE_FULL_DUPLEX_SELECTION;
1652                                                         tp->scb.Parm[1] = 0; /* is ignored but should be zero */
1653                                                         tp->scb.CMD = MODIFY_OPEN_PARMS;
1654                                                 }
1655                                                 else
1656                                                 {
1657                                                         if(tp->CMDqueue & OC_SET_FUNCT_ADDR)
1658                                                         {
1659                                                                 tp->CMDqueue ^= OC_SET_FUNCT_ADDR;
1660                                                                 tp->scb.Parm[0] = LOWORD(tp->ocpl.FunctAddr);
1661                                                                 tp->scb.Parm[1] = HIWORD(tp->ocpl.FunctAddr);
1662                                                                 tp->scb.CMD = SET_FUNCT_ADDR;
1663                                                         }
1664                                                         else
1665                                                         {
1666                                                                 if(tp->CMDqueue & OC_SET_GROUP_ADDR)
1667                                                                 {
1668                                                                         tp->CMDqueue ^= OC_SET_GROUP_ADDR;
1669                                                                         tp->scb.Parm[0] = LOWORD(tp->ocpl.GroupAddr);
1670                                                                         tp->scb.Parm[1] = HIWORD(tp->ocpl.GroupAddr);
1671                                                                         tp->scb.CMD = SET_GROUP_ADDR;
1672                                                                 }
1673                                                                 else
1674                                                                 {
1675                                                                         if(tp->CMDqueue & OC_READ_ERROR_LOG)
1676                                                                         {
1677                                                                                 tp->CMDqueue ^= OC_READ_ERROR_LOG;
1678                                                                                 Addr = htonl(((char *)&tp->errorlogtable - (char *)tp) + tp->dmabuffer);
1679                                                                                 tp->scb.Parm[0] = LOWORD(Addr);
1680                                                                                 tp->scb.Parm[1] = HIWORD(Addr);
1681                                                                                 tp->scb.CMD = READ_ERROR_LOG;
1682                                                                         }
1683                                                                         else
1684                                                                         {
1685                                                                                 printk(KERN_WARNING "CheckForOutstandingCommand: unknown Command\n");
1686                                                                                 tp->CMDqueue = 0;
1687                                                                                 return;
1688                                                                         }
1689                                                                 }
1690                                                         }
1691                                                 }
1692                                         }
1693                                 }
1694                         }
1695                 }
1696         }
1697
1698         tp->ScbInUse = 1;       /* Set semaphore: SCB in use. */
1699
1700         /* Execute SCB and generate IRQ when done. */
1701         tms380tr_exec_sifcmd(dev, CMD_EXECUTE | CMD_SCB_REQUEST);
1702
1703         return;
1704 }
1705
1706 /*
1707  * IRQ conditions: signal loss on the ring, transmit or receive of beacon
1708  * frames (disabled if bit 1 of OPEN option is set); report error MAC
1709  * frame transmit (disabled if bit 2 of OPEN option is set); open or short
1710  * circuit fault on the lobe is detected; remove MAC frame received;
1711  * error counter overflow (255); opened adapter is the only station in ring.
1712  * After some of the IRQs the adapter is closed!
1713  */
1714 static void tms380tr_ring_status_irq(struct net_device *dev)
1715 {
1716         struct net_local *tp = netdev_priv(dev);
1717
1718         tp->CurrentRingStatus = be16_to_cpu((unsigned short)tp->ssb.Parm[0]);
1719
1720         /* First: fill up statistics */
1721         if(tp->ssb.Parm[0] & SIGNAL_LOSS)
1722         {
1723                 printk(KERN_INFO "%s: Signal Loss\n", dev->name);
1724                 tp->MacStat.line_errors++;
1725         }
1726
1727         /* Adapter is closed, but initialized */
1728         if(tp->ssb.Parm[0] & LOBE_WIRE_FAULT)
1729         {
1730                 printk(KERN_INFO "%s: Lobe Wire Fault, Reopen Adapter\n", 
1731                         dev->name);
1732                 tp->MacStat.line_errors++;
1733         }
1734
1735         if(tp->ssb.Parm[0] & RING_RECOVERY)
1736                 printk(KERN_INFO "%s: Ring Recovery\n", dev->name);
1737
1738         /* Counter overflow: read error log */
1739         if(tp->ssb.Parm[0] & COUNTER_OVERFLOW)
1740         {
1741                 printk(KERN_INFO "%s: Counter Overflow\n", dev->name);
1742                 tms380tr_exec_cmd(dev, OC_READ_ERROR_LOG);
1743         }
1744
1745         /* Adapter is closed, but initialized */
1746         if(tp->ssb.Parm[0] & REMOVE_RECEIVED)
1747                 printk(KERN_INFO "%s: Remove Received, Reopen Adapter\n", 
1748                         dev->name);
1749
1750         /* Adapter is closed, but initialized */
1751         if(tp->ssb.Parm[0] & AUTO_REMOVAL_ERROR)
1752                 printk(KERN_INFO "%s: Auto Removal Error, Reopen Adapter\n", 
1753                         dev->name);
1754
1755         if(tp->ssb.Parm[0] & HARD_ERROR)
1756                 printk(KERN_INFO "%s: Hard Error\n", dev->name);
1757
1758         if(tp->ssb.Parm[0] & SOFT_ERROR)
1759                 printk(KERN_INFO "%s: Soft Error\n", dev->name);
1760
1761         if(tp->ssb.Parm[0] & TRANSMIT_BEACON)
1762                 printk(KERN_INFO "%s: Transmit Beacon\n", dev->name);
1763
1764         if(tp->ssb.Parm[0] & SINGLE_STATION)
1765                 printk(KERN_INFO "%s: Single Station\n", dev->name);
1766
1767         /* Check if adapter has been closed */
1768         if(tp->ssb.Parm[0] & ADAPTER_CLOSED)
1769         {
1770                 printk(KERN_INFO "%s: Adapter closed (Reopening)," 
1771                         "CurrentRingStat %x\n",
1772                         dev->name, tp->CurrentRingStatus);
1773                 tp->AdapterOpenFlag = 0;
1774                 tms380tr_open_adapter(dev);
1775         }
1776
1777         return;
1778 }
1779
1780 /*
1781  * Issued if adapter has encountered an unrecoverable hardware
1782  * or software error.
1783  */
1784 static void tms380tr_chk_irq(struct net_device *dev)
1785 {
1786         int i;
1787         unsigned short AdapterCheckBlock[4];
1788         struct net_local *tp = netdev_priv(dev);
1789
1790         tp->AdapterOpenFlag = 0;        /* Adapter closed now */
1791
1792         /* Page number of adapter memory */
1793         SIFWRITEW(0x0001, SIFADX);
1794         /* Address offset */
1795         SIFWRITEW(CHECKADDR, SIFADR);
1796
1797         /* Reading 8 byte adapter check block. */
1798         for(i = 0; i < 4; i++)
1799                 AdapterCheckBlock[i] = SIFREADW(SIFINC);
1800
1801         if(tms380tr_debug > 3)
1802         {
1803                 printk(KERN_DEBUG "%s: AdapterCheckBlock: ", dev->name);
1804                 for (i = 0; i < 4; i++)
1805                         printk("%04X", AdapterCheckBlock[i]);
1806                 printk("\n");
1807         }
1808
1809         switch(AdapterCheckBlock[0])
1810         {
1811                 case DIO_PARITY:
1812                         printk(KERN_INFO "%s: DIO parity error\n", dev->name);
1813                         break;
1814
1815                 case DMA_READ_ABORT:
1816                         printk(KERN_INFO "%s DMA read operation aborted:\n",
1817                                 dev->name);
1818                         switch (AdapterCheckBlock[1])
1819                         {
1820                                 case 0:
1821                                         printk(KERN_INFO "Timeout\n");
1822                                         printk(KERN_INFO "Address: %04X %04X\n",
1823                                                 AdapterCheckBlock[2],
1824                                                 AdapterCheckBlock[3]);
1825                                         break;
1826
1827                                 case 1:
1828                                         printk(KERN_INFO "Parity error\n");
1829                                         printk(KERN_INFO "Address: %04X %04X\n",
1830                                                 AdapterCheckBlock[2], 
1831                                                 AdapterCheckBlock[3]);
1832                                         break;
1833
1834                                 case 2: 
1835                                         printk(KERN_INFO "Bus error\n");
1836                                         printk(KERN_INFO "Address: %04X %04X\n",
1837                                                 AdapterCheckBlock[2], 
1838                                                 AdapterCheckBlock[3]);
1839                                         break;
1840
1841                                 default:
1842                                         printk(KERN_INFO "Unknown error.\n");
1843                                         break;
1844                         }
1845                         break;
1846
1847                 case DMA_WRITE_ABORT:
1848                         printk(KERN_INFO "%s: DMA write operation aborted: \n",
1849                                 dev->name);
1850                         switch (AdapterCheckBlock[1])
1851                         {
1852                                 case 0: 
1853                                         printk(KERN_INFO "Timeout\n");
1854                                         printk(KERN_INFO "Address: %04X %04X\n",
1855                                                 AdapterCheckBlock[2], 
1856                                                 AdapterCheckBlock[3]);
1857                                         break;
1858
1859                                 case 1: 
1860                                         printk(KERN_INFO "Parity error\n");
1861                                         printk(KERN_INFO "Address: %04X %04X\n",
1862                                                 AdapterCheckBlock[2], 
1863                                                 AdapterCheckBlock[3]);
1864                                         break;
1865
1866                                 case 2: 
1867                                         printk(KERN_INFO "Bus error\n");
1868                                         printk(KERN_INFO "Address: %04X %04X\n",
1869                                                 AdapterCheckBlock[2], 
1870                                                 AdapterCheckBlock[3]);
1871                                         break;
1872
1873                                 default:
1874                                         printk(KERN_INFO "Unknown error.\n");
1875                                         break;
1876                         }
1877                         break;
1878
1879                 case ILLEGAL_OP_CODE:
1880                         printk(KERN_INFO "%s: Illegal operation code in firmware\n",
1881                                 dev->name);
1882                         /* Parm[0-3]: adapter internal register R13-R15 */
1883                         break;
1884
1885                 case PARITY_ERRORS:
1886                         printk(KERN_INFO "%s: Adapter internal bus parity error\n",
1887                                 dev->name);
1888                         /* Parm[0-3]: adapter internal register R13-R15 */
1889                         break;
1890
1891                 case RAM_DATA_ERROR:
1892                         printk(KERN_INFO "%s: RAM data error\n", dev->name);
1893                         /* Parm[0-1]: MSW/LSW address of RAM location. */
1894                         break;
1895
1896                 case RAM_PARITY_ERROR:
1897                         printk(KERN_INFO "%s: RAM parity error\n", dev->name);
1898                         /* Parm[0-1]: MSW/LSW address of RAM location. */
1899                         break;
1900
1901                 case RING_UNDERRUN:
1902                         printk(KERN_INFO "%s: Internal DMA underrun detected\n",
1903                                 dev->name);
1904                         break;
1905
1906                 case INVALID_IRQ:
1907                         printk(KERN_INFO "%s: Unrecognized interrupt detected\n",
1908                                 dev->name);
1909                         /* Parm[0-3]: adapter internal register R13-R15 */
1910                         break;
1911
1912                 case INVALID_ERROR_IRQ:
1913                         printk(KERN_INFO "%s: Unrecognized error interrupt detected\n",
1914                                 dev->name);
1915                         /* Parm[0-3]: adapter internal register R13-R15 */
1916                         break;
1917
1918                 case INVALID_XOP:
1919                         printk(KERN_INFO "%s: Unrecognized XOP request detected\n",
1920                                 dev->name);
1921                         /* Parm[0-3]: adapter internal register R13-R15 */
1922                         break;
1923
1924                 default:
1925                         printk(KERN_INFO "%s: Unknown status", dev->name);
1926                         break;
1927         }
1928
1929         if(tms380tr_chipset_init(dev) == 1)
1930         {
1931                 /* Restart of firmware successful */
1932                 tp->AdapterOpenFlag = 1;
1933         }
1934
1935         return;
1936 }
1937
1938 /*
1939  * Internal adapter pointer to RAM data are copied from adapter into
1940  * host system.
1941  */
1942 static int tms380tr_read_ptr(struct net_device *dev)
1943 {
1944         struct net_local *tp = netdev_priv(dev);
1945         unsigned short adapterram;
1946
1947         tms380tr_read_ram(dev, (unsigned char *)&tp->intptrs.BurnedInAddrPtr,
1948                         ADAPTER_INT_PTRS, 16);
1949         tms380tr_read_ram(dev, (unsigned char *)&adapterram,
1950                         cpu_to_be16((unsigned short)tp->intptrs.AdapterRAMPtr), 2);
1951         return be16_to_cpu(adapterram); 
1952 }
1953
1954 /*
1955  * Reads a number of bytes from adapter to system memory.
1956  */
1957 static void tms380tr_read_ram(struct net_device *dev, unsigned char *Data,
1958                                 unsigned short Address, int Length)
1959 {
1960         int i;
1961         unsigned short old_sifadx, old_sifadr, InWord;
1962
1963         /* Save the current values */
1964         old_sifadx = SIFREADW(SIFADX);
1965         old_sifadr = SIFREADW(SIFADR);
1966
1967         /* Page number of adapter memory */
1968         SIFWRITEW(0x0001, SIFADX);
1969         /* Address offset in adapter RAM */
1970         SIFWRITEW(Address, SIFADR);
1971
1972         /* Copy len byte from adapter memory to system data area. */
1973         i = 0;
1974         for(;;)
1975         {
1976                 InWord = SIFREADW(SIFINC);
1977
1978                 *(Data + i) = HIBYTE(InWord);   /* Write first byte */
1979                 if(++i == Length)               /* All is done break */
1980                         break;
1981
1982                 *(Data + i) = LOBYTE(InWord);   /* Write second byte */
1983                 if (++i == Length)              /* All is done break */
1984                         break;
1985         }
1986
1987         /* Restore original values */
1988         SIFWRITEW(old_sifadx, SIFADX);
1989         SIFWRITEW(old_sifadr, SIFADR);
1990
1991         return;
1992 }
1993
1994 /*
1995  * Cancel all queued packets in the transmission queue.
1996  */
1997 static void tms380tr_cancel_tx_queue(struct net_local* tp)
1998 {
1999         TPL *tpl;
2000
2001         /*
2002          * NOTE: There must not be an active TRANSMIT command pending, when
2003          * this function is called.
2004          */
2005         if(tp->TransmitCommandActive)
2006                 return;
2007
2008         for(;;)
2009         {
2010                 tpl = tp->TplBusy;
2011                 if(!tpl->BusyFlag)
2012                         break;
2013                 /* "Remove" TPL from busy list. */
2014                 tp->TplBusy = tpl->NextTPLPtr;
2015                 tms380tr_write_tpl_status(tpl, 0);      /* Clear VALID bit */
2016                 tpl->BusyFlag = 0;              /* "free" TPL */
2017
2018                 printk(KERN_INFO "Cancel tx (%08lXh).\n", (unsigned long)tpl);
2019                 if (tpl->DMABuff)
2020                         dma_unmap_single(tp->pdev, tpl->DMABuff, tpl->Skb->len, DMA_TO_DEVICE);
2021                 dev_kfree_skb_any(tpl->Skb);
2022         }
2023
2024         return;
2025 }
2026
2027 /*
2028  * This function is called whenever a transmit interrupt is generated by the
2029  * adapter. For a command complete interrupt, it is checked if we have to
2030  * issue a new transmit command or not.
2031  */
2032 static void tms380tr_tx_status_irq(struct net_device *dev)
2033 {
2034         struct net_local *tp = netdev_priv(dev);
2035         unsigned char HighByte, HighAc, LowAc;
2036         TPL *tpl;
2037
2038         /* NOTE: At this point the SSB from TRANSMIT STATUS is no longer
2039          * available, because the CLEAR SSB command has already been issued.
2040          *
2041          * Process all complete transmissions.
2042          */
2043
2044         for(;;)
2045         {
2046                 tpl = tp->TplBusy;
2047                 if(!tpl->BusyFlag || (tpl->Status
2048                         & (TX_VALID | TX_FRAME_COMPLETE))
2049                         != TX_FRAME_COMPLETE)
2050                 {
2051                         break;
2052                 }
2053
2054                 /* "Remove" TPL from busy list. */
2055                 tp->TplBusy = tpl->NextTPLPtr ;
2056
2057                 /* Check the transmit status field only for directed frames*/
2058                 if(DIRECTED_FRAME(tpl) && (tpl->Status & TX_ERROR) == 0)
2059                 {
2060                         HighByte = GET_TRANSMIT_STATUS_HIGH_BYTE(tpl->Status);
2061                         HighAc   = GET_FRAME_STATUS_HIGH_AC(HighByte);
2062                         LowAc    = GET_FRAME_STATUS_LOW_AC(HighByte);
2063
2064                         if((HighAc != LowAc) || (HighAc == AC_NOT_RECOGNIZED))
2065                         {
2066                                 printk(KERN_DEBUG "%s: (DA=%08lX not recognized)\n",
2067                                         dev->name,
2068                                         *(unsigned long *)&tpl->MData[2+2]);
2069                         }
2070                         else
2071                         {
2072                                 if(tms380tr_debug > 3)
2073                                         printk(KERN_DEBUG "%s: Directed frame tx'd\n", 
2074                                                 dev->name);
2075                         }
2076                 }
2077                 else
2078                 {
2079                         if(!DIRECTED_FRAME(tpl))
2080                         {
2081                                 if(tms380tr_debug > 3)
2082                                         printk(KERN_DEBUG "%s: Broadcast frame tx'd\n",
2083                                                 dev->name);
2084                         }
2085                 }
2086
2087                 tp->MacStat.tx_packets++;
2088                 if (tpl->DMABuff)
2089                         dma_unmap_single(tp->pdev, tpl->DMABuff, tpl->Skb->len, DMA_TO_DEVICE);
2090                 dev_kfree_skb_irq(tpl->Skb);
2091                 tpl->BusyFlag = 0;      /* "free" TPL */
2092         }
2093
2094         if(!tp->TplFree->NextTPLPtr->BusyFlag)
2095                 netif_wake_queue(dev);
2096         return;
2097 }
2098
2099 /*
2100  * Called if a frame receive interrupt is generated by the adapter.
2101  * Check if the frame is valid and indicate it to system.
2102  */
2103 static void tms380tr_rcv_status_irq(struct net_device *dev)
2104 {
2105         struct net_local *tp = netdev_priv(dev);
2106         unsigned char *ReceiveDataPtr;
2107         struct sk_buff *skb;
2108         unsigned int Length, Length2;
2109         RPL *rpl;
2110         RPL *SaveHead;
2111         dma_addr_t dmabuf;
2112
2113         /* NOTE: At this point the SSB from RECEIVE STATUS is no longer
2114          * available, because the CLEAR SSB command has already been issued.
2115          *
2116          * Process all complete receives.
2117          */
2118
2119         for(;;)
2120         {
2121                 rpl = tp->RplHead;
2122                 if(rpl->Status & RX_VALID)
2123                         break;          /* RPL still in use by adapter */
2124
2125                 /* Forward RPLHead pointer to next list. */
2126                 SaveHead = tp->RplHead;
2127                 tp->RplHead = rpl->NextRPLPtr;
2128
2129                 /* Get the frame size (Byte swap for Intel).
2130                  * Do this early (see workaround comment below)
2131                  */
2132                 Length = be16_to_cpu((unsigned short)rpl->FrameSize);
2133
2134                 /* Check if the Frame_Start, Frame_End and
2135                  * Frame_Complete bits are set.
2136                  */
2137                 if((rpl->Status & VALID_SINGLE_BUFFER_FRAME)
2138                         == VALID_SINGLE_BUFFER_FRAME)
2139                 {
2140                         ReceiveDataPtr = rpl->MData;
2141
2142                         /* Workaround for delayed write of FrameSize on ISA
2143                          * (FrameSize is false but valid-bit is reset)
2144                          * Frame size is set to zero when the RPL is freed.
2145                          * Length2 is there because there have also been
2146                          * cases where the FrameSize was partially written
2147                          */
2148                         Length2 = be16_to_cpu((unsigned short)rpl->FrameSize);
2149
2150                         if(Length == 0 || Length != Length2)
2151                         {
2152                                 tp->RplHead = SaveHead;
2153                                 break;  /* Return to tms380tr_interrupt */
2154                         }
2155                         tms380tr_update_rcv_stats(tp,ReceiveDataPtr,Length);
2156                           
2157                         if(tms380tr_debug > 3)
2158                                 printk(KERN_DEBUG "%s: Packet Length %04X (%d)\n",
2159                                         dev->name, Length, Length);
2160                           
2161                         /* Indicate the received frame to system the
2162                          * adapter does the Source-Routing padding for 
2163                          * us. See: OpenOptions in tms380tr_init_opb()
2164                          */
2165                         skb = rpl->Skb;
2166                         if(rpl->SkbStat == SKB_UNAVAILABLE)
2167                         {
2168                                 /* Try again to allocate skb */
2169                                 skb = dev_alloc_skb(tp->MaxPacketSize);
2170                                 if(skb == NULL)
2171                                 {
2172                                         /* Update Stats ?? */
2173                                 }
2174                                 else
2175                                 {
2176                                         skb->dev        = dev;
2177                                         skb_put(skb, tp->MaxPacketSize);
2178                                         rpl->SkbStat    = SKB_DATA_COPY;
2179                                         ReceiveDataPtr  = rpl->MData;
2180                                 }
2181                         }
2182
2183                         if(skb && (rpl->SkbStat == SKB_DATA_COPY
2184                                 || rpl->SkbStat == SKB_DMA_DIRECT))
2185                         {
2186                                 if(rpl->SkbStat == SKB_DATA_COPY)
2187                                         memcpy(skb->data, ReceiveDataPtr, Length);
2188
2189                                 /* Deliver frame to system */
2190                                 rpl->Skb = NULL;
2191                                 skb_trim(skb,Length);
2192                                 skb->protocol = tr_type_trans(skb,dev);
2193                                 netif_rx(skb);
2194                                 dev->last_rx = jiffies;
2195                         }
2196                 }
2197                 else    /* Invalid frame */
2198                 {
2199                         if(rpl->Skb != NULL)
2200                                 dev_kfree_skb_irq(rpl->Skb);
2201
2202                         /* Skip list. */
2203                         if(rpl->Status & RX_START_FRAME)
2204                                 /* Frame start bit is set -> overflow. */
2205                                 tp->MacStat.rx_errors++;
2206                 }
2207                 if (rpl->DMABuff)
2208                         dma_unmap_single(tp->pdev, rpl->DMABuff, tp->MaxPacketSize, DMA_TO_DEVICE);
2209                 rpl->DMABuff = 0;
2210
2211                 /* Allocate new skb for rpl */
2212                 rpl->Skb = dev_alloc_skb(tp->MaxPacketSize);
2213                 /* skb == NULL ? then use local buffer */
2214                 if(rpl->Skb == NULL)
2215                 {
2216                         rpl->SkbStat = SKB_UNAVAILABLE;
2217                         rpl->FragList[0].DataAddr = htonl(((char *)tp->LocalRxBuffers[rpl->RPLIndex] - (char *)tp) + tp->dmabuffer);
2218                         rpl->MData = tp->LocalRxBuffers[rpl->RPLIndex];
2219                 }
2220                 else    /* skb != NULL */
2221                 {
2222                         rpl->Skb->dev = dev;
2223                         skb_put(rpl->Skb, tp->MaxPacketSize);
2224
2225                         /* Data unreachable for DMA ? then use local buffer */
2226                         dmabuf = dma_map_single(tp->pdev, rpl->Skb->data, tp->MaxPacketSize, DMA_FROM_DEVICE);
2227                         if(tp->dmalimit && (dmabuf + tp->MaxPacketSize > tp->dmalimit))
2228                         {
2229                                 rpl->SkbStat = SKB_DATA_COPY;
2230                                 rpl->FragList[0].DataAddr = htonl(((char *)tp->LocalRxBuffers[rpl->RPLIndex] - (char *)tp) + tp->dmabuffer);
2231                                 rpl->MData = tp->LocalRxBuffers[rpl->RPLIndex];
2232                         }
2233                         else
2234                         {
2235                                 /* DMA directly in skb->data */
2236                                 rpl->SkbStat = SKB_DMA_DIRECT;
2237                                 rpl->FragList[0].DataAddr = htonl(dmabuf);
2238                                 rpl->MData = rpl->Skb->data;
2239                                 rpl->DMABuff = dmabuf;
2240                         }
2241                 }
2242
2243                 rpl->FragList[0].DataCount = cpu_to_be16((unsigned short)tp->MaxPacketSize);
2244                 rpl->FrameSize = 0;
2245
2246                 /* Pass the last RPL back to the adapter */
2247                 tp->RplTail->FrameSize = 0;
2248
2249                 /* Reset the CSTAT field in the list. */
2250                 tms380tr_write_rpl_status(tp->RplTail, RX_VALID | RX_FRAME_IRQ);
2251
2252                 /* Current RPL becomes last one in list. */
2253                 tp->RplTail = tp->RplTail->NextRPLPtr;
2254
2255                 /* Inform adapter about RPL valid. */
2256                 tms380tr_exec_sifcmd(dev, CMD_RX_VALID);
2257         }
2258
2259         return;
2260 }
2261
2262 /*
2263  * This function should be used whenever the status of any RPL must be
2264  * modified by the driver, because the compiler may otherwise change the
2265  * order of instructions such that writing the RPL status may be executed
2266  * at an undesireable time. When this function is used, the status is
2267  * always written when the function is called.
2268  */
2269 static void tms380tr_write_rpl_status(RPL *rpl, unsigned int Status)
2270 {
2271         rpl->Status = Status;
2272
2273         return;
2274 }
2275
2276 /*
2277  * The function updates the statistic counters in mac->MacStat.
2278  * It differtiates between directed and broadcast/multicast ( ==functional)
2279  * frames.
2280  */
2281 static void tms380tr_update_rcv_stats(struct net_local *tp, unsigned char DataPtr[],
2282                                         unsigned int Length)
2283 {
2284         tp->MacStat.rx_packets++;
2285         tp->MacStat.rx_bytes += Length;
2286         
2287         /* Test functional bit */
2288         if(DataPtr[2] & GROUP_BIT)
2289                 tp->MacStat.multicast++;
2290
2291         return;
2292 }
2293
2294 static int tms380tr_set_mac_address(struct net_device *dev, void *addr)
2295 {
2296         struct net_local *tp = netdev_priv(dev);
2297         struct sockaddr *saddr = addr;
2298         
2299         if (tp->AdapterOpenFlag || tp->AdapterVirtOpenFlag) {
2300                 printk(KERN_WARNING "%s: Cannot set MAC/LAA address while card is open\n", dev->name);
2301                 return -EIO;
2302         }
2303         memcpy(dev->dev_addr, saddr->sa_data, dev->addr_len);
2304         return 0;
2305 }
2306
2307 #if TMS380TR_DEBUG > 0
2308 /*
2309  * Dump Packet (data)
2310  */
2311 static void tms380tr_dump(unsigned char *Data, int length)
2312 {
2313         int i, j;
2314
2315         for (i = 0, j = 0; i < length / 8; i++, j += 8)
2316         {
2317                 printk(KERN_DEBUG "%02x %02x %02x %02x %02x %02x %02x %02x\n",
2318                        Data[j+0],Data[j+1],Data[j+2],Data[j+3],
2319                        Data[j+4],Data[j+5],Data[j+6],Data[j+7]);
2320         }
2321
2322         return;
2323 }
2324 #endif
2325
2326 void tmsdev_term(struct net_device *dev)
2327 {
2328         struct net_local *tp;
2329
2330         tp = netdev_priv(dev);
2331         dma_unmap_single(tp->pdev, tp->dmabuffer, sizeof(struct net_local),
2332                 DMA_BIDIRECTIONAL);
2333 }
2334
2335 int tmsdev_init(struct net_device *dev, struct device *pdev)
2336 {
2337         struct net_local *tms_local;
2338
2339         memset(dev->priv, 0, sizeof(struct net_local));
2340         tms_local = netdev_priv(dev);
2341         init_waitqueue_head(&tms_local->wait_for_tok_int);
2342         if (pdev->dma_mask)
2343                 tms_local->dmalimit = *pdev->dma_mask;
2344         else
2345                 return -ENOMEM;
2346         tms_local->pdev = pdev;
2347         tms_local->dmabuffer = dma_map_single(pdev, (void *)tms_local,
2348             sizeof(struct net_local), DMA_BIDIRECTIONAL);
2349         if (tms_local->dmabuffer + sizeof(struct net_local) > 
2350                         tms_local->dmalimit)
2351         {
2352                 printk(KERN_INFO "%s: Memory not accessible for DMA\n",
2353                         dev->name);
2354                 tmsdev_term(dev);
2355                 return -ENOMEM;
2356         }
2357         
2358         /* These can be overridden by the card driver if needed */
2359         dev->open               = tms380tr_open;
2360         dev->stop               = tms380tr_close;
2361         dev->do_ioctl           = NULL; 
2362         dev->hard_start_xmit    = tms380tr_send_packet;
2363         dev->tx_timeout         = tms380tr_timeout;
2364         dev->watchdog_timeo     = HZ;
2365         dev->get_stats          = tms380tr_get_stats;
2366         dev->set_multicast_list = &tms380tr_set_multicast_list;
2367         dev->set_mac_address    = tms380tr_set_mac_address;
2368
2369         return 0;
2370 }
2371
2372 EXPORT_SYMBOL(tms380tr_open);
2373 EXPORT_SYMBOL(tms380tr_close);
2374 EXPORT_SYMBOL(tms380tr_interrupt);
2375 EXPORT_SYMBOL(tmsdev_init);
2376 EXPORT_SYMBOL(tmsdev_term);
2377 EXPORT_SYMBOL(tms380tr_wait);
2378
2379 #ifdef MODULE
2380
2381 static struct module *TMS380_module = NULL;
2382
2383 int init_module(void)
2384 {
2385         printk(KERN_DEBUG "%s", version);
2386         
2387         TMS380_module = &__this_module;
2388         return 0;
2389 }
2390
2391 void cleanup_module(void)
2392 {
2393         TMS380_module = NULL;
2394 }
2395 #endif
2396
2397 MODULE_LICENSE("GPL");
2398