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