[PATCH] tms380tr: move to DMA API
[linux-2.6.git] / 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                 current->state          = TASK_INTERRUPTIBLE;
1247                 tmp = schedule_timeout(tmp);
1248         } while(time_after(tmp, jiffies));
1249 #else
1250         udelay(time);
1251 #endif
1252         return;
1253 }
1254
1255 /*
1256  * Write a command value to the SIFCMD register
1257  */
1258 static void tms380tr_exec_sifcmd(struct net_device *dev, unsigned int WriteValue)
1259 {
1260         unsigned short cmd;
1261         unsigned short SifStsValue;
1262         unsigned long loop_counter;
1263
1264         WriteValue = ((WriteValue ^ CMD_SYSTEM_IRQ) | CMD_INTERRUPT_ADAPTER);
1265         cmd = (unsigned short)WriteValue;
1266         loop_counter = 0,5 * 800000;
1267         do {
1268                 SifStsValue = SIFREADW(SIFSTS);
1269         } while((SifStsValue & CMD_INTERRUPT_ADAPTER) && loop_counter--);
1270         SIFWRITEW(cmd, SIFCMD);
1271
1272         return;
1273 }
1274
1275 /*
1276  * Processes adapter hardware reset, halts adapter and downloads firmware,
1277  * clears the halt bit.
1278  */
1279 static int tms380tr_reset_adapter(struct net_device *dev)
1280 {
1281         struct net_local *tp = netdev_priv(dev);
1282         unsigned short *fw_ptr;
1283         unsigned short count, c, count2;
1284         const struct firmware *fw_entry = NULL;
1285
1286         if (request_firmware(&fw_entry, "tms380tr.bin", tp->pdev) != 0) {
1287                 printk(KERN_ALERT "%s: firmware %s is missing, cannot start.\n",
1288                         dev->name, "tms380tr.bin");
1289                 return (-1);
1290         }
1291
1292         fw_ptr = (unsigned short *)fw_entry->data;
1293         count2 = fw_entry->size / 2;
1294
1295         /* Hardware adapter reset */
1296         SIFWRITEW(ACL_ARESET, SIFACL);
1297         tms380tr_wait(40);
1298         
1299         c = SIFREADW(SIFACL);
1300         tms380tr_wait(20);
1301
1302         if(dev->dma == 0)       /* For PCI adapters */
1303         {
1304                 c &= ~(ACL_NSELOUT0 | ACL_NSELOUT1);    /* Clear bits */
1305                 if(tp->setnselout)
1306                   c |= (*tp->setnselout)(dev);
1307         }
1308
1309         /* In case a command is pending - forget it */
1310         tp->ScbInUse = 0;
1311
1312         c &= ~ACL_ARESET;               /* Clear adapter reset bit */
1313         c |=  ACL_CPHALT;               /* Halt adapter CPU, allow download */
1314         c |= ACL_BOOT;
1315         c |= ACL_SINTEN;
1316         c &= ~ACL_PSDMAEN;              /* Clear pseudo dma bit */
1317         SIFWRITEW(c, SIFACL);
1318         tms380tr_wait(40);
1319
1320         count = 0;
1321         /* Download firmware via DIO interface: */
1322         do {
1323                 if (count2 < 3) continue;
1324
1325                 /* Download first address part */
1326                 SIFWRITEW(*fw_ptr, SIFADX);
1327                 fw_ptr++;
1328                 count2--;
1329                 /* Download second address part */
1330                 SIFWRITEW(*fw_ptr, SIFADD);
1331                 fw_ptr++;
1332                 count2--;
1333
1334                 if((count = *fw_ptr) != 0)      /* Load loop counter */
1335                 {
1336                         fw_ptr++;       /* Download block data */
1337                         count2--;
1338                         if (count > count2) continue;
1339
1340                         for(; count > 0; count--)
1341                         {
1342                                 SIFWRITEW(*fw_ptr, SIFINC);
1343                                 fw_ptr++;
1344                                 count2--;
1345                         }
1346                 }
1347                 else    /* Stop, if last block downloaded */
1348                 {
1349                         c = SIFREADW(SIFACL);
1350                         c &= (~ACL_CPHALT | ACL_SINTEN);
1351
1352                         /* Clear CPHALT and start BUD */
1353                         SIFWRITEW(c, SIFACL);
1354                         if (fw_entry)
1355                                 release_firmware(fw_entry);
1356                         return (1);
1357                 }
1358         } while(count == 0);
1359
1360         if (fw_entry)
1361                 release_firmware(fw_entry);
1362         printk(KERN_INFO "%s: Adapter Download Failed\n", dev->name);
1363         return (-1);
1364 }
1365
1366 /*
1367  * Starts bring up diagnostics of token ring adapter and evaluates
1368  * diagnostic results.
1369  */
1370 static int tms380tr_bringup_diags(struct net_device *dev)
1371 {
1372         int loop_cnt, retry_cnt;
1373         unsigned short Status;
1374
1375         tms380tr_wait(HALF_SECOND);
1376         tms380tr_exec_sifcmd(dev, EXEC_SOFT_RESET);
1377         tms380tr_wait(HALF_SECOND);
1378
1379         retry_cnt = BUD_MAX_RETRIES;    /* maximal number of retrys */
1380
1381         do {
1382                 retry_cnt--;
1383                 if(tms380tr_debug > 3)
1384                         printk(KERN_DEBUG "BUD-Status: ");
1385                 loop_cnt = BUD_MAX_LOOPCNT;     /* maximum: three seconds*/
1386                 do {                    /* Inspect BUD results */
1387                         loop_cnt--;
1388                         tms380tr_wait(HALF_SECOND);
1389                         Status = SIFREADW(SIFSTS);
1390                         Status &= STS_MASK;
1391
1392                         if(tms380tr_debug > 3)
1393                                 printk(KERN_DEBUG " %04X \n", Status);
1394                         /* BUD successfully completed */
1395                         if(Status == STS_INITIALIZE)
1396                                 return (1);
1397                 /* Unrecoverable hardware error, BUD not completed? */
1398                 } while((loop_cnt > 0) && ((Status & (STS_ERROR | STS_TEST))
1399                         != (STS_ERROR | STS_TEST)));
1400
1401                 /* Error preventing completion of BUD */
1402                 if(retry_cnt > 0)
1403                 {
1404                         printk(KERN_INFO "%s: Adapter Software Reset.\n", 
1405                                 dev->name);
1406                         tms380tr_exec_sifcmd(dev, EXEC_SOFT_RESET);
1407                         tms380tr_wait(HALF_SECOND);
1408                 }
1409         } while(retry_cnt > 0);
1410
1411         Status = SIFREADW(SIFSTS);
1412         
1413         printk(KERN_INFO "%s: Hardware error\n", dev->name);
1414         /* Hardware error occurred! */
1415         Status &= 0x001f;
1416         if (Status & 0x0010)
1417                 printk(KERN_INFO "%s: BUD Error: Timeout\n", dev->name);
1418         else if ((Status & 0x000f) > 6)
1419                 printk(KERN_INFO "%s: BUD Error: Illegal Failure\n", dev->name);
1420         else
1421                 printk(KERN_INFO "%s: Bring Up Diagnostics Error (%04X) occurred\n", dev->name, Status & 0x000f);
1422
1423         return (-1);
1424 }
1425
1426 /*
1427  * Copy initialisation data to adapter memory, beginning at address
1428  * 1:0A00; Starting DMA test and evaluating result bits.
1429  */
1430 static int tms380tr_init_adapter(struct net_device *dev)
1431 {
1432         struct net_local *tp = netdev_priv(dev);
1433
1434         const unsigned char SCB_Test[6] = {0x00, 0x00, 0xC1, 0xE2, 0xD4, 0x8B};
1435         const unsigned char SSB_Test[8] = {0xFF, 0xFF, 0xD1, 0xD7,
1436                                                 0xC5, 0xD9, 0xC3, 0xD4};
1437         void *ptr = (void *)&tp->ipb;
1438         unsigned short *ipb_ptr = (unsigned short *)ptr;
1439         unsigned char *cb_ptr = (unsigned char *) &tp->scb;
1440         unsigned char *sb_ptr = (unsigned char *) &tp->ssb;
1441         unsigned short Status;
1442         int i, loop_cnt, retry_cnt;
1443
1444         /* Normalize: byte order low/high, word order high/low! (only IPB!) */
1445         tp->ipb.SCB_Addr = SWAPW(((char *)&tp->scb - (char *)tp) + tp->dmabuffer);
1446         tp->ipb.SSB_Addr = SWAPW(((char *)&tp->ssb - (char *)tp) + tp->dmabuffer);
1447
1448         if(tms380tr_debug > 3)
1449         {
1450                 printk(KERN_DEBUG "%s: buffer (real): %lx\n", dev->name, (long) &tp->scb);
1451                 printk(KERN_DEBUG "%s: buffer (virt): %lx\n", dev->name, (long) ((char *)&tp->scb - (char *)tp) + (long) tp->dmabuffer);
1452                 printk(KERN_DEBUG "%s: buffer (DMA) : %lx\n", dev->name, (long) tp->dmabuffer);
1453                 printk(KERN_DEBUG "%s: buffer (tp)  : %lx\n", dev->name, (long) tp);
1454         }
1455         /* Maximum: three initialization retries */
1456         retry_cnt = INIT_MAX_RETRIES;
1457
1458         do {
1459                 retry_cnt--;
1460
1461                 /* Transfer initialization block */
1462                 SIFWRITEW(0x0001, SIFADX);
1463
1464                 /* To address 0001:0A00 of adapter RAM */
1465                 SIFWRITEW(0x0A00, SIFADD);
1466
1467                 /* Write 11 words to adapter RAM */
1468                 for(i = 0; i < 11; i++)
1469                         SIFWRITEW(ipb_ptr[i], SIFINC);
1470
1471                 /* Execute SCB adapter command */
1472                 tms380tr_exec_sifcmd(dev, CMD_EXECUTE);
1473
1474                 loop_cnt = INIT_MAX_LOOPCNT;    /* Maximum: 11 seconds */
1475
1476                 /* While remaining retries, no error and not completed */
1477                 do {
1478                         Status = 0;
1479                         loop_cnt--;
1480                         tms380tr_wait(HALF_SECOND);
1481
1482                         /* Mask interesting status bits */
1483                         Status = SIFREADW(SIFSTS);
1484                         Status &= STS_MASK;
1485                 } while(((Status &(STS_INITIALIZE | STS_ERROR | STS_TEST)) != 0)
1486                         && ((Status & STS_ERROR) == 0) && (loop_cnt != 0));
1487
1488                 if((Status & (STS_INITIALIZE | STS_ERROR | STS_TEST)) == 0)
1489                 {
1490                         /* Initialization completed without error */
1491                         i = 0;
1492                         do {    /* Test if contents of SCB is valid */
1493                                 if(SCB_Test[i] != *(cb_ptr + i))
1494                                 {
1495                                         printk(KERN_INFO "%s: DMA failed\n", dev->name);
1496                                         /* DMA data error: wrong data in SCB */
1497                                         return (-1);
1498                                 }
1499                                 i++;
1500                         } while(i < 6);
1501
1502                         i = 0;
1503                         do {    /* Test if contents of SSB is valid */
1504                                 if(SSB_Test[i] != *(sb_ptr + i))
1505                                         /* DMA data error: wrong data in SSB */
1506                                         return (-1);
1507                                 i++;
1508                         } while (i < 8);
1509
1510                         return (1);     /* Adapter successfully initialized */
1511                 }
1512                 else
1513                 {
1514                         if((Status & STS_ERROR) != 0)
1515                         {
1516                                 /* Initialization error occurred */
1517                                 Status = SIFREADW(SIFSTS);
1518                                 Status &= STS_ERROR_MASK;
1519                                 /* ShowInitialisationErrorCode(Status); */
1520                                 printk(KERN_INFO "%s: Status error: %d\n", dev->name, Status);
1521                                 return (-1); /* Unrecoverable error */
1522                         }
1523                         else
1524                         {
1525                                 if(retry_cnt > 0)
1526                                 {
1527                                         /* Reset adapter and try init again */
1528                                         tms380tr_exec_sifcmd(dev, EXEC_SOFT_RESET);
1529                                         tms380tr_wait(HALF_SECOND);
1530                                 }
1531                         }
1532                 }
1533         } while(retry_cnt > 0);
1534
1535         printk(KERN_INFO "%s: Retry exceeded\n", dev->name);
1536         return (-1);
1537 }
1538
1539 /*
1540  * Check for outstanding commands in command queue and tries to execute
1541  * command immediately. Corresponding command flag in command queue is cleared.
1542  */
1543 static void tms380tr_chk_outstanding_cmds(struct net_device *dev)
1544 {
1545         struct net_local *tp = netdev_priv(dev);
1546         unsigned long Addr = 0;
1547
1548         if(tp->CMDqueue == 0)
1549                 return;         /* No command execution */
1550
1551         /* If SCB in use: no command */
1552         if(tp->ScbInUse == 1)
1553                 return;
1554
1555         /* Check if adapter is opened, avoiding COMMAND_REJECT
1556          * interrupt by the adapter!
1557          */
1558         if(tp->AdapterOpenFlag == 0)
1559         {
1560                 if(tp->CMDqueue & OC_OPEN)
1561                 {
1562                         /* Execute OPEN command */
1563                         tp->CMDqueue ^= OC_OPEN;
1564
1565                         Addr = htonl(((char *)&tp->ocpl - (char *)tp) + tp->dmabuffer);
1566                         tp->scb.Parm[0] = LOWORD(Addr);
1567                         tp->scb.Parm[1] = HIWORD(Addr);
1568                         tp->scb.CMD = OPEN;
1569                 }
1570                 else
1571                         /* No OPEN command queued, but adapter closed. Note:
1572                          * We'll try to re-open the adapter in DriverPoll()
1573                          */
1574                         return;         /* No adapter command issued */
1575         }
1576         else
1577         {
1578                 /* Adapter is open; evaluate command queue: try to execute
1579                  * outstanding commands (depending on priority!) CLOSE
1580                  * command queued
1581                  */
1582                 if(tp->CMDqueue & OC_CLOSE)
1583                 {
1584                         tp->CMDqueue ^= OC_CLOSE;
1585                         tp->AdapterOpenFlag = 0;
1586                         tp->scb.Parm[0] = 0; /* Parm[0], Parm[1] are ignored */
1587                         tp->scb.Parm[1] = 0; /* but should be set to zero! */
1588                         tp->scb.CMD = CLOSE;
1589                         if(!tp->HaltInProgress)
1590                                 tp->CMDqueue |= OC_OPEN; /* re-open adapter */
1591                         else
1592                                 tp->CMDqueue = 0;       /* no more commands */
1593                 }
1594                 else
1595                 {
1596                         if(tp->CMDqueue & OC_RECEIVE)
1597                         {
1598                                 tp->CMDqueue ^= OC_RECEIVE;
1599                                 Addr = htonl(((char *)tp->RplHead - (char *)tp) + tp->dmabuffer);
1600                                 tp->scb.Parm[0] = LOWORD(Addr);
1601                                 tp->scb.Parm[1] = HIWORD(Addr);
1602                                 tp->scb.CMD = RECEIVE;
1603                         }
1604                         else
1605                         {
1606                                 if(tp->CMDqueue & OC_TRANSMIT_HALT)
1607                                 {
1608                                         /* NOTE: TRANSMIT.HALT must be checked 
1609                                          * before TRANSMIT.
1610                                          */
1611                                         tp->CMDqueue ^= OC_TRANSMIT_HALT;
1612                                         tp->scb.CMD = TRANSMIT_HALT;
1613
1614                                         /* Parm[0] and Parm[1] are ignored
1615                                          * but should be set to zero!
1616                                          */
1617                                         tp->scb.Parm[0] = 0;
1618                                         tp->scb.Parm[1] = 0;
1619                                 }
1620                                 else
1621                                 {
1622                                         if(tp->CMDqueue & OC_TRANSMIT)
1623                                         {
1624                                                 /* NOTE: TRANSMIT must be 
1625                                                  * checked after TRANSMIT.HALT
1626                                                  */
1627                                                 if(tp->TransmitCommandActive)
1628                                                 {
1629                                                         if(!tp->TransmitHaltScheduled)
1630                                                         {
1631                                                                 tp->TransmitHaltScheduled = 1;
1632                                                                 tms380tr_exec_cmd(dev, OC_TRANSMIT_HALT) ;
1633                                                         }
1634                                                         tp->TransmitCommandActive = 0;
1635                                                         return;
1636                                                 }
1637
1638                                                 tp->CMDqueue ^= OC_TRANSMIT;
1639                                                 tms380tr_cancel_tx_queue(tp);
1640                                                 Addr = htonl(((char *)tp->TplBusy - (char *)tp) + tp->dmabuffer);
1641                                                 tp->scb.Parm[0] = LOWORD(Addr);
1642                                                 tp->scb.Parm[1] = HIWORD(Addr);
1643                                                 tp->scb.CMD = TRANSMIT;
1644                                                 tp->TransmitCommandActive = 1;
1645                                         }
1646                                         else
1647                                         {
1648                                                 if(tp->CMDqueue & OC_MODIFY_OPEN_PARMS)
1649                                                 {
1650                                                         tp->CMDqueue ^= OC_MODIFY_OPEN_PARMS;
1651                                                         tp->scb.Parm[0] = tp->ocpl.OPENOptions; /* new OPEN options*/
1652                                                         tp->scb.Parm[0] |= ENABLE_FULL_DUPLEX_SELECTION;
1653                                                         tp->scb.Parm[1] = 0; /* is ignored but should be zero */
1654                                                         tp->scb.CMD = MODIFY_OPEN_PARMS;
1655                                                 }
1656                                                 else
1657                                                 {
1658                                                         if(tp->CMDqueue & OC_SET_FUNCT_ADDR)
1659                                                         {
1660                                                                 tp->CMDqueue ^= OC_SET_FUNCT_ADDR;
1661                                                                 tp->scb.Parm[0] = LOWORD(tp->ocpl.FunctAddr);
1662                                                                 tp->scb.Parm[1] = HIWORD(tp->ocpl.FunctAddr);
1663                                                                 tp->scb.CMD = SET_FUNCT_ADDR;
1664                                                         }
1665                                                         else
1666                                                         {
1667                                                                 if(tp->CMDqueue & OC_SET_GROUP_ADDR)
1668                                                                 {
1669                                                                         tp->CMDqueue ^= OC_SET_GROUP_ADDR;
1670                                                                         tp->scb.Parm[0] = LOWORD(tp->ocpl.GroupAddr);
1671                                                                         tp->scb.Parm[1] = HIWORD(tp->ocpl.GroupAddr);
1672                                                                         tp->scb.CMD = SET_GROUP_ADDR;
1673                                                                 }
1674                                                                 else
1675                                                                 {
1676                                                                         if(tp->CMDqueue & OC_READ_ERROR_LOG)
1677                                                                         {
1678                                                                                 tp->CMDqueue ^= OC_READ_ERROR_LOG;
1679                                                                                 Addr = htonl(((char *)&tp->errorlogtable - (char *)tp) + tp->dmabuffer);
1680                                                                                 tp->scb.Parm[0] = LOWORD(Addr);
1681                                                                                 tp->scb.Parm[1] = HIWORD(Addr);
1682                                                                                 tp->scb.CMD = READ_ERROR_LOG;
1683                                                                         }
1684                                                                         else
1685                                                                         {
1686                                                                                 printk(KERN_WARNING "CheckForOutstandingCommand: unknown Command\n");
1687                                                                                 tp->CMDqueue = 0;
1688                                                                                 return;
1689                                                                         }
1690                                                                 }
1691                                                         }
1692                                                 }
1693                                         }
1694                                 }
1695                         }
1696                 }
1697         }
1698
1699         tp->ScbInUse = 1;       /* Set semaphore: SCB in use. */
1700
1701         /* Execute SCB and generate IRQ when done. */
1702         tms380tr_exec_sifcmd(dev, CMD_EXECUTE | CMD_SCB_REQUEST);
1703
1704         return;
1705 }
1706
1707 /*
1708  * IRQ conditions: signal loss on the ring, transmit or receive of beacon
1709  * frames (disabled if bit 1 of OPEN option is set); report error MAC
1710  * frame transmit (disabled if bit 2 of OPEN option is set); open or short
1711  * circuit fault on the lobe is detected; remove MAC frame received;
1712  * error counter overflow (255); opened adapter is the only station in ring.
1713  * After some of the IRQs the adapter is closed!
1714  */
1715 static void tms380tr_ring_status_irq(struct net_device *dev)
1716 {
1717         struct net_local *tp = netdev_priv(dev);
1718
1719         tp->CurrentRingStatus = be16_to_cpu((unsigned short)tp->ssb.Parm[0]);
1720
1721         /* First: fill up statistics */
1722         if(tp->ssb.Parm[0] & SIGNAL_LOSS)
1723         {
1724                 printk(KERN_INFO "%s: Signal Loss\n", dev->name);
1725                 tp->MacStat.line_errors++;
1726         }
1727
1728         /* Adapter is closed, but initialized */
1729         if(tp->ssb.Parm[0] & LOBE_WIRE_FAULT)
1730         {
1731                 printk(KERN_INFO "%s: Lobe Wire Fault, Reopen Adapter\n", 
1732                         dev->name);
1733                 tp->MacStat.line_errors++;
1734         }
1735
1736         if(tp->ssb.Parm[0] & RING_RECOVERY)
1737                 printk(KERN_INFO "%s: Ring Recovery\n", dev->name);
1738
1739         /* Counter overflow: read error log */
1740         if(tp->ssb.Parm[0] & COUNTER_OVERFLOW)
1741         {
1742                 printk(KERN_INFO "%s: Counter Overflow\n", dev->name);
1743                 tms380tr_exec_cmd(dev, OC_READ_ERROR_LOG);
1744         }
1745
1746         /* Adapter is closed, but initialized */
1747         if(tp->ssb.Parm[0] & REMOVE_RECEIVED)
1748                 printk(KERN_INFO "%s: Remove Received, Reopen Adapter\n", 
1749                         dev->name);
1750
1751         /* Adapter is closed, but initialized */
1752         if(tp->ssb.Parm[0] & AUTO_REMOVAL_ERROR)
1753                 printk(KERN_INFO "%s: Auto Removal Error, Reopen Adapter\n", 
1754                         dev->name);
1755
1756         if(tp->ssb.Parm[0] & HARD_ERROR)
1757                 printk(KERN_INFO "%s: Hard Error\n", dev->name);
1758
1759         if(tp->ssb.Parm[0] & SOFT_ERROR)
1760                 printk(KERN_INFO "%s: Soft Error\n", dev->name);
1761
1762         if(tp->ssb.Parm[0] & TRANSMIT_BEACON)
1763                 printk(KERN_INFO "%s: Transmit Beacon\n", dev->name);
1764
1765         if(tp->ssb.Parm[0] & SINGLE_STATION)
1766                 printk(KERN_INFO "%s: Single Station\n", dev->name);
1767
1768         /* Check if adapter has been closed */
1769         if(tp->ssb.Parm[0] & ADAPTER_CLOSED)
1770         {
1771                 printk(KERN_INFO "%s: Adapter closed (Reopening)," 
1772                         "CurrentRingStat %x\n",
1773                         dev->name, tp->CurrentRingStatus);
1774                 tp->AdapterOpenFlag = 0;
1775                 tms380tr_open_adapter(dev);
1776         }
1777
1778         return;
1779 }
1780
1781 /*
1782  * Issued if adapter has encountered an unrecoverable hardware
1783  * or software error.
1784  */
1785 static void tms380tr_chk_irq(struct net_device *dev)
1786 {
1787         int i;
1788         unsigned short AdapterCheckBlock[4];
1789         struct net_local *tp = netdev_priv(dev);
1790
1791         tp->AdapterOpenFlag = 0;        /* Adapter closed now */
1792
1793         /* Page number of adapter memory */
1794         SIFWRITEW(0x0001, SIFADX);
1795         /* Address offset */
1796         SIFWRITEW(CHECKADDR, SIFADR);
1797
1798         /* Reading 8 byte adapter check block. */
1799         for(i = 0; i < 4; i++)
1800                 AdapterCheckBlock[i] = SIFREADW(SIFINC);
1801
1802         if(tms380tr_debug > 3)
1803         {
1804                 printk(KERN_DEBUG "%s: AdapterCheckBlock: ", dev->name);
1805                 for (i = 0; i < 4; i++)
1806                         printk("%04X", AdapterCheckBlock[i]);
1807                 printk("\n");
1808         }
1809
1810         switch(AdapterCheckBlock[0])
1811         {
1812                 case DIO_PARITY:
1813                         printk(KERN_INFO "%s: DIO parity error\n", dev->name);
1814                         break;
1815
1816                 case DMA_READ_ABORT:
1817                         printk(KERN_INFO "%s DMA read operation aborted:\n",
1818                                 dev->name);
1819                         switch (AdapterCheckBlock[1])
1820                         {
1821                                 case 0:
1822                                         printk(KERN_INFO "Timeout\n");
1823                                         printk(KERN_INFO "Address: %04X %04X\n",
1824                                                 AdapterCheckBlock[2],
1825                                                 AdapterCheckBlock[3]);
1826                                         break;
1827
1828                                 case 1:
1829                                         printk(KERN_INFO "Parity error\n");
1830                                         printk(KERN_INFO "Address: %04X %04X\n",
1831                                                 AdapterCheckBlock[2], 
1832                                                 AdapterCheckBlock[3]);
1833                                         break;
1834
1835                                 case 2: 
1836                                         printk(KERN_INFO "Bus error\n");
1837                                         printk(KERN_INFO "Address: %04X %04X\n",
1838                                                 AdapterCheckBlock[2], 
1839                                                 AdapterCheckBlock[3]);
1840                                         break;
1841
1842                                 default:
1843                                         printk(KERN_INFO "Unknown error.\n");
1844                                         break;
1845                         }
1846                         break;
1847
1848                 case DMA_WRITE_ABORT:
1849                         printk(KERN_INFO "%s: DMA write operation aborted: \n",
1850                                 dev->name);
1851                         switch (AdapterCheckBlock[1])
1852                         {
1853                                 case 0: 
1854                                         printk(KERN_INFO "Timeout\n");
1855                                         printk(KERN_INFO "Address: %04X %04X\n",
1856                                                 AdapterCheckBlock[2], 
1857                                                 AdapterCheckBlock[3]);
1858                                         break;
1859
1860                                 case 1: 
1861                                         printk(KERN_INFO "Parity error\n");
1862                                         printk(KERN_INFO "Address: %04X %04X\n",
1863                                                 AdapterCheckBlock[2], 
1864                                                 AdapterCheckBlock[3]);
1865                                         break;
1866
1867                                 case 2: 
1868                                         printk(KERN_INFO "Bus error\n");
1869                                         printk(KERN_INFO "Address: %04X %04X\n",
1870                                                 AdapterCheckBlock[2], 
1871                                                 AdapterCheckBlock[3]);
1872                                         break;
1873
1874                                 default:
1875                                         printk(KERN_INFO "Unknown error.\n");
1876                                         break;
1877                         }
1878                         break;
1879
1880                 case ILLEGAL_OP_CODE:
1881                         printk(KERN_INFO "%s: Illegal operation code in firmware\n",
1882                                 dev->name);
1883                         /* Parm[0-3]: adapter internal register R13-R15 */
1884                         break;
1885
1886                 case PARITY_ERRORS:
1887                         printk(KERN_INFO "%s: Adapter internal bus parity error\n",
1888                                 dev->name);
1889                         /* Parm[0-3]: adapter internal register R13-R15 */
1890                         break;
1891
1892                 case RAM_DATA_ERROR:
1893                         printk(KERN_INFO "%s: RAM data error\n", dev->name);
1894                         /* Parm[0-1]: MSW/LSW address of RAM location. */
1895                         break;
1896
1897                 case RAM_PARITY_ERROR:
1898                         printk(KERN_INFO "%s: RAM parity error\n", dev->name);
1899                         /* Parm[0-1]: MSW/LSW address of RAM location. */
1900                         break;
1901
1902                 case RING_UNDERRUN:
1903                         printk(KERN_INFO "%s: Internal DMA underrun detected\n",
1904                                 dev->name);
1905                         break;
1906
1907                 case INVALID_IRQ:
1908                         printk(KERN_INFO "%s: Unrecognized interrupt detected\n",
1909                                 dev->name);
1910                         /* Parm[0-3]: adapter internal register R13-R15 */
1911                         break;
1912
1913                 case INVALID_ERROR_IRQ:
1914                         printk(KERN_INFO "%s: Unrecognized error interrupt detected\n",
1915                                 dev->name);
1916                         /* Parm[0-3]: adapter internal register R13-R15 */
1917                         break;
1918
1919                 case INVALID_XOP:
1920                         printk(KERN_INFO "%s: Unrecognized XOP request detected\n",
1921                                 dev->name);
1922                         /* Parm[0-3]: adapter internal register R13-R15 */
1923                         break;
1924
1925                 default:
1926                         printk(KERN_INFO "%s: Unknown status", dev->name);
1927                         break;
1928         }
1929
1930         if(tms380tr_chipset_init(dev) == 1)
1931         {
1932                 /* Restart of firmware successful */
1933                 tp->AdapterOpenFlag = 1;
1934         }
1935
1936         return;
1937 }
1938
1939 /*
1940  * Internal adapter pointer to RAM data are copied from adapter into
1941  * host system.
1942  */
1943 static int tms380tr_read_ptr(struct net_device *dev)
1944 {
1945         struct net_local *tp = netdev_priv(dev);
1946         unsigned short adapterram;
1947
1948         tms380tr_read_ram(dev, (unsigned char *)&tp->intptrs.BurnedInAddrPtr,
1949                         ADAPTER_INT_PTRS, 16);
1950         tms380tr_read_ram(dev, (unsigned char *)&adapterram,
1951                         cpu_to_be16((unsigned short)tp->intptrs.AdapterRAMPtr), 2);
1952         return be16_to_cpu(adapterram); 
1953 }
1954
1955 /*
1956  * Reads a number of bytes from adapter to system memory.
1957  */
1958 static void tms380tr_read_ram(struct net_device *dev, unsigned char *Data,
1959                                 unsigned short Address, int Length)
1960 {
1961         int i;
1962         unsigned short old_sifadx, old_sifadr, InWord;
1963
1964         /* Save the current values */
1965         old_sifadx = SIFREADW(SIFADX);
1966         old_sifadr = SIFREADW(SIFADR);
1967
1968         /* Page number of adapter memory */
1969         SIFWRITEW(0x0001, SIFADX);
1970         /* Address offset in adapter RAM */
1971         SIFWRITEW(Address, SIFADR);
1972
1973         /* Copy len byte from adapter memory to system data area. */
1974         i = 0;
1975         for(;;)
1976         {
1977                 InWord = SIFREADW(SIFINC);
1978
1979                 *(Data + i) = HIBYTE(InWord);   /* Write first byte */
1980                 if(++i == Length)               /* All is done break */
1981                         break;
1982
1983                 *(Data + i) = LOBYTE(InWord);   /* Write second byte */
1984                 if (++i == Length)              /* All is done break */
1985                         break;
1986         }
1987
1988         /* Restore original values */
1989         SIFWRITEW(old_sifadx, SIFADX);
1990         SIFWRITEW(old_sifadr, SIFADR);
1991
1992         return;
1993 }
1994
1995 /*
1996  * Cancel all queued packets in the transmission queue.
1997  */
1998 static void tms380tr_cancel_tx_queue(struct net_local* tp)
1999 {
2000         TPL *tpl;
2001
2002         /*
2003          * NOTE: There must not be an active TRANSMIT command pending, when
2004          * this function is called.
2005          */
2006         if(tp->TransmitCommandActive)
2007                 return;
2008
2009         for(;;)
2010         {
2011                 tpl = tp->TplBusy;
2012                 if(!tpl->BusyFlag)
2013                         break;
2014                 /* "Remove" TPL from busy list. */
2015                 tp->TplBusy = tpl->NextTPLPtr;
2016                 tms380tr_write_tpl_status(tpl, 0);      /* Clear VALID bit */
2017                 tpl->BusyFlag = 0;              /* "free" TPL */
2018
2019                 printk(KERN_INFO "Cancel tx (%08lXh).\n", (unsigned long)tpl);
2020                 if (tpl->DMABuff)
2021                         dma_unmap_single(tp->pdev, tpl->DMABuff, tpl->Skb->len, DMA_TO_DEVICE);
2022                 dev_kfree_skb_any(tpl->Skb);
2023         }
2024
2025         return;
2026 }
2027
2028 /*
2029  * This function is called whenever a transmit interrupt is generated by the
2030  * adapter. For a command complete interrupt, it is checked if we have to
2031  * issue a new transmit command or not.
2032  */
2033 static void tms380tr_tx_status_irq(struct net_device *dev)
2034 {
2035         struct net_local *tp = netdev_priv(dev);
2036         unsigned char HighByte, HighAc, LowAc;
2037         TPL *tpl;
2038
2039         /* NOTE: At this point the SSB from TRANSMIT STATUS is no longer
2040          * available, because the CLEAR SSB command has already been issued.
2041          *
2042          * Process all complete transmissions.
2043          */
2044
2045         for(;;)
2046         {
2047                 tpl = tp->TplBusy;
2048                 if(!tpl->BusyFlag || (tpl->Status
2049                         & (TX_VALID | TX_FRAME_COMPLETE))
2050                         != TX_FRAME_COMPLETE)
2051                 {
2052                         break;
2053                 }
2054
2055                 /* "Remove" TPL from busy list. */
2056                 tp->TplBusy = tpl->NextTPLPtr ;
2057
2058                 /* Check the transmit status field only for directed frames*/
2059                 if(DIRECTED_FRAME(tpl) && (tpl->Status & TX_ERROR) == 0)
2060                 {
2061                         HighByte = GET_TRANSMIT_STATUS_HIGH_BYTE(tpl->Status);
2062                         HighAc   = GET_FRAME_STATUS_HIGH_AC(HighByte);
2063                         LowAc    = GET_FRAME_STATUS_LOW_AC(HighByte);
2064
2065                         if((HighAc != LowAc) || (HighAc == AC_NOT_RECOGNIZED))
2066                         {
2067                                 printk(KERN_DEBUG "%s: (DA=%08lX not recognized)\n",
2068                                         dev->name,
2069                                         *(unsigned long *)&tpl->MData[2+2]);
2070                         }
2071                         else
2072                         {
2073                                 if(tms380tr_debug > 3)
2074                                         printk(KERN_DEBUG "%s: Directed frame tx'd\n", 
2075                                                 dev->name);
2076                         }
2077                 }
2078                 else
2079                 {
2080                         if(!DIRECTED_FRAME(tpl))
2081                         {
2082                                 if(tms380tr_debug > 3)
2083                                         printk(KERN_DEBUG "%s: Broadcast frame tx'd\n",
2084                                                 dev->name);
2085                         }
2086                 }
2087
2088                 tp->MacStat.tx_packets++;
2089                 if (tpl->DMABuff)
2090                         dma_unmap_single(tp->pdev, tpl->DMABuff, tpl->Skb->len, DMA_TO_DEVICE);
2091                 dev_kfree_skb_irq(tpl->Skb);
2092                 tpl->BusyFlag = 0;      /* "free" TPL */
2093         }
2094
2095         if(!tp->TplFree->NextTPLPtr->BusyFlag)
2096                 netif_wake_queue(dev);
2097         return;
2098 }
2099
2100 /*
2101  * Called if a frame receive interrupt is generated by the adapter.
2102  * Check if the frame is valid and indicate it to system.
2103  */
2104 static void tms380tr_rcv_status_irq(struct net_device *dev)
2105 {
2106         struct net_local *tp = netdev_priv(dev);
2107         unsigned char *ReceiveDataPtr;
2108         struct sk_buff *skb;
2109         unsigned int Length, Length2;
2110         RPL *rpl;
2111         RPL *SaveHead;
2112         dma_addr_t dmabuf;
2113
2114         /* NOTE: At this point the SSB from RECEIVE STATUS is no longer
2115          * available, because the CLEAR SSB command has already been issued.
2116          *
2117          * Process all complete receives.
2118          */
2119
2120         for(;;)
2121         {
2122                 rpl = tp->RplHead;
2123                 if(rpl->Status & RX_VALID)
2124                         break;          /* RPL still in use by adapter */
2125
2126                 /* Forward RPLHead pointer to next list. */
2127                 SaveHead = tp->RplHead;
2128                 tp->RplHead = rpl->NextRPLPtr;
2129
2130                 /* Get the frame size (Byte swap for Intel).
2131                  * Do this early (see workaround comment below)
2132                  */
2133                 Length = be16_to_cpu((unsigned short)rpl->FrameSize);
2134
2135                 /* Check if the Frame_Start, Frame_End and
2136                  * Frame_Complete bits are set.
2137                  */
2138                 if((rpl->Status & VALID_SINGLE_BUFFER_FRAME)
2139                         == VALID_SINGLE_BUFFER_FRAME)
2140                 {
2141                         ReceiveDataPtr = rpl->MData;
2142
2143                         /* Workaround for delayed write of FrameSize on ISA
2144                          * (FrameSize is false but valid-bit is reset)
2145                          * Frame size is set to zero when the RPL is freed.
2146                          * Length2 is there because there have also been
2147                          * cases where the FrameSize was partially written
2148                          */
2149                         Length2 = be16_to_cpu((unsigned short)rpl->FrameSize);
2150
2151                         if(Length == 0 || Length != Length2)
2152                         {
2153                                 tp->RplHead = SaveHead;
2154                                 break;  /* Return to tms380tr_interrupt */
2155                         }
2156                         tms380tr_update_rcv_stats(tp,ReceiveDataPtr,Length);
2157                           
2158                         if(tms380tr_debug > 3)
2159                                 printk(KERN_DEBUG "%s: Packet Length %04X (%d)\n",
2160                                         dev->name, Length, Length);
2161                           
2162                         /* Indicate the received frame to system the
2163                          * adapter does the Source-Routing padding for 
2164                          * us. See: OpenOptions in tms380tr_init_opb()
2165                          */
2166                         skb = rpl->Skb;
2167                         if(rpl->SkbStat == SKB_UNAVAILABLE)
2168                         {
2169                                 /* Try again to allocate skb */
2170                                 skb = dev_alloc_skb(tp->MaxPacketSize);
2171                                 if(skb == NULL)
2172                                 {
2173                                         /* Update Stats ?? */
2174                                 }
2175                                 else
2176                                 {
2177                                         skb->dev        = dev;
2178                                         skb_put(skb, tp->MaxPacketSize);
2179                                         rpl->SkbStat    = SKB_DATA_COPY;
2180                                         ReceiveDataPtr  = rpl->MData;
2181                                 }
2182                         }
2183
2184                         if(skb && (rpl->SkbStat == SKB_DATA_COPY
2185                                 || rpl->SkbStat == SKB_DMA_DIRECT))
2186                         {
2187                                 if(rpl->SkbStat == SKB_DATA_COPY)
2188                                         memcpy(skb->data, ReceiveDataPtr, Length);
2189
2190                                 /* Deliver frame to system */
2191                                 rpl->Skb = NULL;
2192                                 skb_trim(skb,Length);
2193                                 skb->protocol = tr_type_trans(skb,dev);
2194                                 netif_rx(skb);
2195                                 dev->last_rx = jiffies;
2196                         }
2197                 }
2198                 else    /* Invalid frame */
2199                 {
2200                         if(rpl->Skb != NULL)
2201                                 dev_kfree_skb_irq(rpl->Skb);
2202
2203                         /* Skip list. */
2204                         if(rpl->Status & RX_START_FRAME)
2205                                 /* Frame start bit is set -> overflow. */
2206                                 tp->MacStat.rx_errors++;
2207                 }
2208                 if (rpl->DMABuff)
2209                         dma_unmap_single(tp->pdev, rpl->DMABuff, tp->MaxPacketSize, DMA_TO_DEVICE);
2210                 rpl->DMABuff = 0;
2211
2212                 /* Allocate new skb for rpl */
2213                 rpl->Skb = dev_alloc_skb(tp->MaxPacketSize);
2214                 /* skb == NULL ? then use local buffer */
2215                 if(rpl->Skb == NULL)
2216                 {
2217                         rpl->SkbStat = SKB_UNAVAILABLE;
2218                         rpl->FragList[0].DataAddr = htonl(((char *)tp->LocalRxBuffers[rpl->RPLIndex] - (char *)tp) + tp->dmabuffer);
2219                         rpl->MData = tp->LocalRxBuffers[rpl->RPLIndex];
2220                 }
2221                 else    /* skb != NULL */
2222                 {
2223                         rpl->Skb->dev = dev;
2224                         skb_put(rpl->Skb, tp->MaxPacketSize);
2225
2226                         /* Data unreachable for DMA ? then use local buffer */
2227                         dmabuf = dma_map_single(tp->pdev, rpl->Skb->data, tp->MaxPacketSize, DMA_FROM_DEVICE);
2228                         if(tp->dmalimit && (dmabuf + tp->MaxPacketSize > tp->dmalimit))
2229                         {
2230                                 rpl->SkbStat = SKB_DATA_COPY;
2231                                 rpl->FragList[0].DataAddr = htonl(((char *)tp->LocalRxBuffers[rpl->RPLIndex] - (char *)tp) + tp->dmabuffer);
2232                                 rpl->MData = tp->LocalRxBuffers[rpl->RPLIndex];
2233                         }
2234                         else
2235                         {
2236                                 /* DMA directly in skb->data */
2237                                 rpl->SkbStat = SKB_DMA_DIRECT;
2238                                 rpl->FragList[0].DataAddr = htonl(dmabuf);
2239                                 rpl->MData = rpl->Skb->data;
2240                                 rpl->DMABuff = dmabuf;
2241                         }
2242                 }
2243
2244                 rpl->FragList[0].DataCount = cpu_to_be16((unsigned short)tp->MaxPacketSize);
2245                 rpl->FrameSize = 0;
2246
2247                 /* Pass the last RPL back to the adapter */
2248                 tp->RplTail->FrameSize = 0;
2249
2250                 /* Reset the CSTAT field in the list. */
2251                 tms380tr_write_rpl_status(tp->RplTail, RX_VALID | RX_FRAME_IRQ);
2252
2253                 /* Current RPL becomes last one in list. */
2254                 tp->RplTail = tp->RplTail->NextRPLPtr;
2255
2256                 /* Inform adapter about RPL valid. */
2257                 tms380tr_exec_sifcmd(dev, CMD_RX_VALID);
2258         }
2259
2260         return;
2261 }
2262
2263 /*
2264  * This function should be used whenever the status of any RPL must be
2265  * modified by the driver, because the compiler may otherwise change the
2266  * order of instructions such that writing the RPL status may be executed
2267  * at an undesireable time. When this function is used, the status is
2268  * always written when the function is called.
2269  */
2270 static void tms380tr_write_rpl_status(RPL *rpl, unsigned int Status)
2271 {
2272         rpl->Status = Status;
2273
2274         return;
2275 }
2276
2277 /*
2278  * The function updates the statistic counters in mac->MacStat.
2279  * It differtiates between directed and broadcast/multicast ( ==functional)
2280  * frames.
2281  */
2282 static void tms380tr_update_rcv_stats(struct net_local *tp, unsigned char DataPtr[],
2283                                         unsigned int Length)
2284 {
2285         tp->MacStat.rx_packets++;
2286         tp->MacStat.rx_bytes += Length;
2287         
2288         /* Test functional bit */
2289         if(DataPtr[2] & GROUP_BIT)
2290                 tp->MacStat.multicast++;
2291
2292         return;
2293 }
2294
2295 static int tms380tr_set_mac_address(struct net_device *dev, void *addr)
2296 {
2297         struct net_local *tp = netdev_priv(dev);
2298         struct sockaddr *saddr = addr;
2299         
2300         if (tp->AdapterOpenFlag || tp->AdapterVirtOpenFlag) {
2301                 printk(KERN_WARNING "%s: Cannot set MAC/LAA address while card is open\n", dev->name);
2302                 return -EIO;
2303         }
2304         memcpy(dev->dev_addr, saddr->sa_data, dev->addr_len);
2305         return 0;
2306 }
2307
2308 #if TMS380TR_DEBUG > 0
2309 /*
2310  * Dump Packet (data)
2311  */
2312 static void tms380tr_dump(unsigned char *Data, int length)
2313 {
2314         int i, j;
2315
2316         for (i = 0, j = 0; i < length / 8; i++, j += 8)
2317         {
2318                 printk(KERN_DEBUG "%02x %02x %02x %02x %02x %02x %02x %02x\n",
2319                        Data[j+0],Data[j+1],Data[j+2],Data[j+3],
2320                        Data[j+4],Data[j+5],Data[j+6],Data[j+7]);
2321         }
2322
2323         return;
2324 }
2325 #endif
2326
2327 void tmsdev_term(struct net_device *dev)
2328 {
2329         struct net_local *tp;
2330
2331         tp = netdev_priv(dev);
2332         dma_unmap_single(tp->pdev, tp->dmabuffer, sizeof(struct net_local),
2333                 DMA_BIDIRECTIONAL);
2334 }
2335
2336 int tmsdev_init(struct net_device *dev, unsigned long dmalimit, 
2337                 struct device *pdev)
2338 {
2339         struct net_local *tms_local;
2340
2341         memset(dev->priv, 0, sizeof(struct net_local));
2342         tms_local = netdev_priv(dev);
2343         init_waitqueue_head(&tms_local->wait_for_tok_int);
2344         tms_local->dmalimit = dmalimit;
2345         tms_local->pdev = pdev;
2346         tms_local->dmabuffer = dma_map_single(pdev, (void *)tms_local,
2347             sizeof(struct net_local), DMA_BIDIRECTIONAL);
2348         if (tms_local->dmabuffer + sizeof(struct net_local) > dmalimit)
2349         {
2350                 printk(KERN_INFO "%s: Memory not accessible for DMA\n",
2351                         dev->name);
2352                 tmsdev_term(dev);
2353                 return -ENOMEM;
2354         }
2355         
2356         /* These can be overridden by the card driver if needed */
2357         dev->open               = tms380tr_open;
2358         dev->stop               = tms380tr_close;
2359         dev->do_ioctl           = NULL; 
2360         dev->hard_start_xmit    = tms380tr_send_packet;
2361         dev->tx_timeout         = tms380tr_timeout;
2362         dev->watchdog_timeo     = HZ;
2363         dev->get_stats          = tms380tr_get_stats;
2364         dev->set_multicast_list = &tms380tr_set_multicast_list;
2365         dev->set_mac_address    = tms380tr_set_mac_address;
2366
2367         return 0;
2368 }
2369
2370 EXPORT_SYMBOL(tms380tr_open);
2371 EXPORT_SYMBOL(tms380tr_close);
2372 EXPORT_SYMBOL(tms380tr_interrupt);
2373 EXPORT_SYMBOL(tmsdev_init);
2374 EXPORT_SYMBOL(tmsdev_term);
2375 EXPORT_SYMBOL(tms380tr_wait);
2376
2377 #ifdef MODULE
2378
2379 static struct module *TMS380_module = NULL;
2380
2381 int init_module(void)
2382 {
2383         printk(KERN_DEBUG "%s", version);
2384         
2385         TMS380_module = &__this_module;
2386         return 0;
2387 }
2388
2389 void cleanup_module(void)
2390 {
2391         TMS380_module = NULL;
2392 }
2393 #endif
2394
2395 MODULE_LICENSE("GPL");
2396