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