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