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