upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / drivers / net / tokenring / ibmtr.c
1 /* ibmtr.c:  A shared-memory IBM Token Ring 16/4 driver for linux
2  *
3  *      Written 1993 by Mark Swanson and Peter De Schrijver.
4  *      This software may be used and distributed according to the terms
5  *      of the GNU General Public License, incorporated herein by reference.
6  *
7  *      This device driver should work with Any IBM Token Ring Card that does
8  *      not use DMA.
9  *
10  *      I used Donald Becker's (becker@scyld.com) device driver work
11  *      as a base for most of my initial work.
12  *
13  *      Changes by Peter De Schrijver
14  *              (Peter.Deschrijver@linux.cc.kuleuven.ac.be) :
15  *
16  *      + changed name to ibmtr.c in anticipation of other tr boards.
17  *      + changed reset code and adapter open code.
18  *      + added SAP open code.
19  *      + a first attempt to write interrupt, transmit and receive routines.
20  *
21  *      Changes by David W. Morris (dwm@shell.portal.com) :
22  *      941003 dwm: - Restructure tok_probe for multiple adapters, devices.
23  *      + Add comments, misc reorg for clarity.
24  *      + Flatten interrupt handler levels.
25  *
26  *      Changes by Farzad Farid (farzy@zen.via.ecp.fr)
27  *      and Pascal Andre (andre@chimay.via.ecp.fr) (March 9 1995) :
28  *      + multi ring support clean up.
29  *      + RFC1042 compliance enhanced.
30  *
31  *      Changes by Pascal Andre (andre@chimay.via.ecp.fr) (September 7 1995) :
32  *      + bug correction in tr_tx
33  *      + removed redundant information display
34  *      + some code reworking
35  *
36  *      Changes by Michel Lespinasse (walken@via.ecp.fr),
37  *      Yann Doussot (doussot@via.ecp.fr) and Pascal Andre (andre@via.ecp.fr)
38  *      (February 18, 1996) :
39  *      + modified shared memory and mmio access port the driver to
40  *        alpha platform (structure access -> readb/writeb)
41  *
42  *      Changes by Steve Kipisz (bungy@ibm.net or kipisz@vnet.ibm.com)
43  *      (January 18 1996):
44  *      + swapped WWOR and WWCR in ibmtr.h
45  *      + moved some init code from tok_probe into trdev_init.  The
46  *        PCMCIA code can call trdev_init to complete initializing
47  *        the driver.
48  *      + added -DPCMCIA to support PCMCIA
49  *      + detecting PCMCIA Card Removal in interrupt handler.  If
50  *        ISRP is FF, then a PCMCIA card has been removed
51  *        10/2000 Burt needed a new method to avoid crashing the OS
52  *
53  *      Changes by Paul Norton (pnorton@cts.com) :
54  *      + restructured the READ.LOG logic to prevent the transmit SRB
55  *        from being rudely overwritten before the transmit cycle is
56  *        complete. (August 15 1996)
57  *      + completed multiple adapter support. (November 20 1996)
58  *      + implemented csum_partial_copy in tr_rx and increased receive 
59  *        buffer size and count. Minor fixes. (March 15, 1997)
60  *
61  *      Changes by Christopher Turcksin <wabbit@rtfc.demon.co.uk>
62  *      + Now compiles ok as a module again.
63  *
64  *      Changes by Paul Norton (pnorton@ieee.org) :
65  *      + moved the header manipulation code in tr_tx and tr_rx to
66  *        net/802/tr.c. (July 12 1997)
67  *      + add retry and timeout on open if cable disconnected. (May 5 1998)
68  *      + lifted 2000 byte mtu limit. now depends on shared-RAM size.
69  *        May 25 1998)
70  *      + can't allocate 2k recv buff at 8k shared-RAM. (20 October 1998)
71  *
72  *      Changes by Joel Sloan (jjs@c-me.com) :
73  *      + disable verbose debug messages by default - to enable verbose
74  *        debugging, edit the IBMTR_DEBUG_MESSAGES define below 
75  *      
76  *      Changes by Mike Phillips <phillim@amtrak.com> :
77  *      + Added extra #ifdef's to work with new PCMCIA Token Ring Code.
78  *        The PCMCIA code now just sets up the card so it can be recognized
79  *        by ibmtr_probe. Also checks allocated memory vs. on-board memory
80  *        for correct figure to use.
81  *
82  *      Changes by Tim Hockin (thockin@isunix.it.ilstu.edu) :
83  *      + added spinlocks for SMP sanity (10 March 1999)
84  *
85  *      Changes by Jochen Friedrich to enable RFC1469 Option 2 multicasting
86  *      i.e. using functional address C0 00 00 04 00 00 to transmit and 
87  *      receive multicast packets.
88  *
89  *      Changes by Mike Sullivan (based on original sram patch by Dave Grothe
90  *      to support windowing into on adapter shared ram.
91  *      i.e. Use LANAID to setup a PnP configuration with 16K RAM. Paging
92  *      will shift this 16K window over the entire available shared RAM.
93  *
94  *      Changes by Peter De Schrijver (p2@mind.be) :
95  *      + fixed a problem with PCMCIA card removal
96  *
97  *      Change by Mike Sullivan et al.:
98  *      + added turbo card support. No need to use lanaid to configure
99  *      the adapter into isa compatiblity mode.
100  *
101  *      Changes by Burt Silverman to allow the computer to behave nicely when
102  *      a cable is pulled or not in place, or a PCMCIA card is removed hot.
103  */
104
105 /* change the define of IBMTR_DEBUG_MESSAGES to a nonzero value 
106 in the event that chatty debug messages are desired - jjs 12/30/98 */
107
108 #define IBMTR_DEBUG_MESSAGES 0
109
110 #include <linux/module.h>
111
112 #ifdef PCMCIA           /* required for ibmtr_cs.c to build */
113 #undef MODULE           /* yes, really */
114 #undef ENABLE_PAGING
115 #else
116 #define ENABLE_PAGING 1         
117 #endif
118
119 #define FALSE 0
120 #define TRUE (!FALSE)
121
122 /* changes the output format of driver initialization */
123 #define TR_VERBOSE      0
124
125 /* some 95 OS send many non UI frame; this allow removing the warning */
126 #define TR_FILTERNONUI  1
127
128 #include <linux/ioport.h>
129 #include <linux/netdevice.h>
130 #include <linux/ip.h>
131 #include <linux/trdevice.h>
132 #include <linux/ibmtr.h>
133
134 #include <net/checksum.h>
135
136 #include <asm/io.h>
137
138 #define DPRINTK(format, args...) printk("%s: " format, dev->name , ## args)
139 #define DPRINTD(format, args...) DummyCall("%s: " format, dev->name , ## args)
140
141 /* version and credits */
142 #ifndef PCMCIA
143 static char version[] __initdata =
144     "\nibmtr.c: v1.3.57   8/ 7/94 Peter De Schrijver and Mark Swanson\n"
145     "         v2.1.125 10/20/98 Paul Norton    <pnorton@ieee.org>\n"
146     "         v2.2.0   12/30/98 Joel Sloan     <jjs@c-me.com>\n"
147     "         v2.2.1   02/08/00 Mike Sullivan  <sullivam@us.ibm.com>\n" 
148     "         v2.2.2   07/27/00 Burt Silverman <burts@us.ibm.com>\n" 
149     "         v2.4.0   03/01/01 Mike Sullivan <sullivan@us.ibm.com>\n";
150 #endif
151
152 /* this allows displaying full adapter information */
153
154 char *channel_def[] __devinitdata = { "ISA", "MCA", "ISA P&P" };
155
156 static char pcchannelid[] __devinitdata = {
157         0x05, 0x00, 0x04, 0x09,
158         0x04, 0x03, 0x04, 0x0f,
159         0x03, 0x06, 0x03, 0x01,
160         0x03, 0x01, 0x03, 0x00,
161         0x03, 0x09, 0x03, 0x09,
162         0x03, 0x00, 0x02, 0x00
163 };
164
165 static char mcchannelid[] __devinitdata =  {
166         0x04, 0x0d, 0x04, 0x01,
167         0x05, 0x02, 0x05, 0x03,
168         0x03, 0x06, 0x03, 0x03,
169         0x05, 0x08, 0x03, 0x04,
170         0x03, 0x05, 0x03, 0x01,
171         0x03, 0x08, 0x02, 0x00
172 };
173
174 char __devinit *adapter_def(char type)
175 {
176         switch (type) {
177         case 0xF: return "PC Adapter | PC Adapter II | Adapter/A";
178         case 0xE: return "16/4 Adapter | 16/4 Adapter/A (long)";
179         case 0xD: return "16/4 Adapter/A (short) | 16/4 ISA-16 Adapter";
180         case 0xC: return "Auto 16/4 Adapter";
181         default: return "adapter (unknown type)";
182         };
183 };
184
185 #define TRC_INIT 0x01           /*  Trace initialization & PROBEs */
186 #define TRC_INITV 0x02          /*  verbose init trace points     */
187 unsigned char ibmtr_debug_trace = 0;
188
189 static int      ibmtr_probe(struct net_device *dev);
190 static int      ibmtr_probe1(struct net_device *dev, int ioaddr);
191 static unsigned char get_sram_size(struct tok_info *adapt_info);
192 static int      trdev_init(struct net_device *dev);
193 static int      tok_open(struct net_device *dev);
194 static int      tok_init_card(struct net_device *dev);
195 void            tok_open_adapter(unsigned long dev_addr);
196 static void     open_sap(unsigned char type, struct net_device *dev);
197 static void     tok_set_multicast_list(struct net_device *dev);
198 static int      tok_send_packet(struct sk_buff *skb, struct net_device *dev);
199 static int      tok_close(struct net_device *dev);
200 irqreturn_t tok_interrupt(int irq, void *dev_id, struct pt_regs *regs);
201 static void     initial_tok_int(struct net_device *dev);
202 static void     tr_tx(struct net_device *dev);
203 static void     tr_rx(struct net_device *dev);
204 void            ibmtr_reset_timer(struct timer_list*tmr,struct net_device *dev);
205 static void     tok_rerun(unsigned long dev_addr);
206 void            ibmtr_readlog(struct net_device *dev);
207 static struct   net_device_stats *tok_get_stats(struct net_device *dev);
208 int             ibmtr_change_mtu(struct net_device *dev, int mtu);
209 static void     find_turbo_adapters(int *iolist);
210
211 static int ibmtr_portlist[IBMTR_MAX_ADAPTERS+1] __devinitdata = {
212         0xa20, 0xa24, 0, 0, 0
213 };
214 static int __devinitdata turbo_io[IBMTR_MAX_ADAPTERS] = {0};
215 static int __devinitdata turbo_irq[IBMTR_MAX_ADAPTERS] = {0};
216 static int __devinitdata turbo_searched = 0;
217
218 #ifndef PCMCIA
219 static __u32 ibmtr_mem_base __initdata = 0xd0000;
220 #endif
221
222 static void __devinit PrtChanID(char *pcid, short stride)
223 {
224         short i, j;
225         for (i = 0, j = 0; i < 24; i++, j += stride)
226                 printk("%1x", ((int) pcid[j]) & 0x0f);
227         printk("\n");
228 }
229
230 static void __devinit HWPrtChanID(void * pcid, short stride)
231 {
232         short i, j;
233         for (i = 0, j = 0; i < 24; i++, j += stride)
234                 printk("%1x", ((int) readb(pcid + j)) & 0x0f);
235         printk("\n");
236 }
237
238 /* We have to ioremap every checked address, because isa_readb is 
239  * going away. 
240  */
241
242 static void __devinit find_turbo_adapters(int *iolist) {
243         int ram_addr;
244         int index=0;
245         void *chanid;
246         int found_turbo=0;
247         unsigned char *tchanid, ctemp;
248         int i, j;
249         unsigned long jif;
250         void *ram_mapped ;   
251
252         if (turbo_searched == 1) return;
253         turbo_searched=1;
254         for (ram_addr=0xC0000; ram_addr < 0xE0000; ram_addr+=0x2000) {
255
256                 __u32 intf_tbl=0;
257
258                 found_turbo=1;
259                 ram_mapped = ioremap((u32)ram_addr,0x1fff) ; 
260                 if (ram_mapped==NULL) 
261                         continue ; 
262                 chanid=(CHANNEL_ID + ram_mapped);
263                 tchanid=pcchannelid;
264                 ctemp=readb(chanid) & 0x0f;
265                 if (ctemp != *tchanid) continue;
266                 for (i=2,j=1; i<=46; i=i+2,j++) {
267                         if ((readb(chanid+i) & 0x0f) != tchanid[j]){
268                                 found_turbo=0;
269                                 break;
270                         }
271                 }
272                 if (!found_turbo) continue;
273
274                 writeb(0x90, ram_mapped+0x1E01);
275                 for(i=2; i<0x0f; i++) {
276                         writeb(0x00, ram_mapped+0x1E01+i);
277                 }
278                 writeb(0x00, ram_mapped+0x1E01);
279                 for(jif=jiffies+TR_BUSY_INTERVAL; time_before_eq(jiffies,jif););
280                 intf_tbl=ntohs(readw(ram_mapped+ACA_OFFSET+ACA_RW+WRBR_EVEN));
281                 if (intf_tbl) {
282 #if IBMTR_DEBUG_MESSAGES
283                         printk("ibmtr::find_turbo_adapters, Turbo found at "
284                                 "ram_addr %x\n",ram_addr);
285                         printk("ibmtr::find_turbo_adapters, interface_table ");
286                         for(i=0; i<6; i++) {
287                                 printk("%x:",readb(ram_addr+intf_tbl+i));
288                         }
289                         printk("\n");
290 #endif
291                         turbo_io[index]=ntohs(readw(ram_mapped+intf_tbl+4));
292                         turbo_irq[index]=readb(ram_mapped+intf_tbl+3);
293                         outb(0, turbo_io[index] + ADAPTRESET);
294                         for(jif=jiffies+TR_RST_TIME;time_before_eq(jiffies,jif););
295                         outb(0, turbo_io[index] + ADAPTRESETREL);
296                         index++;
297                         continue;
298                 }
299 #if IBMTR_DEBUG_MESSAGES 
300                 printk("ibmtr::find_turbo_adapters, ibmtr card found at"
301                         " %x but not a Turbo model\n",ram_addr);
302 #endif
303         iounmap(ram_mapped) ;   
304         } /* for */
305         for(i=0; i<IBMTR_MAX_ADAPTERS; i++) {
306                 if(!turbo_io[i]) break;
307                 for (j=0; j<IBMTR_MAX_ADAPTERS; j++) {
308                         if ( iolist[j] && iolist[j] != turbo_io[i]) continue;
309                         iolist[j]=turbo_io[i];
310                         break;
311                 }
312         }
313 }
314
315 static void ibmtr_cleanup_card(struct net_device *dev)
316 {
317         if (dev->base_addr) {
318                 outb(0,dev->base_addr+ADAPTRESET);
319                 
320                 schedule_timeout(TR_RST_TIME); /* wait 50ms */
321
322                 outb(0,dev->base_addr+ADAPTRESETREL);
323         }
324
325 #ifndef PCMCIA
326         free_irq(dev->irq, dev);
327         release_region(dev->base_addr, IBMTR_IO_EXTENT);
328
329         { 
330                 struct tok_info *ti = (struct tok_info *) dev->priv;
331                 iounmap((u32 *)ti->mmio);
332                 iounmap((u32 *)ti->sram_virt);
333         }
334 #endif          
335 }
336
337 int ibmtr_probe_card(struct net_device *dev)
338 {
339         int err = ibmtr_probe(dev);
340         if (!err) {
341                 err = register_netdev(dev);
342                 if (err)
343                         ibmtr_cleanup_card(dev);
344         }
345         return err;
346 }
347
348 /****************************************************************************
349  *      ibmtr_probe():  Routine specified in the network device structure
350  *      to probe for an IBM Token Ring Adapter.  Routine outline:
351  *      I.    Interrogate hardware to determine if an adapter exists
352  *            and what the speeds and feeds are
353  *      II.   Setup data structures to control execution based upon
354  *            adapter characteristics.
355  *
356  *      We expect ibmtr_probe to be called once for each device entry
357  *      which references it.
358  ****************************************************************************/
359
360 static int ibmtr_probe(struct net_device *dev)
361 {
362         int i;
363         int base_addr = dev->base_addr;
364
365         if (base_addr && base_addr <= 0x1ff) /* Don't probe at all. */
366                 return -ENXIO;
367         if (base_addr > 0x1ff) { /* Check a single specified location.  */
368                 if (!ibmtr_probe1(dev, base_addr)) return 0;
369                 return -ENODEV;
370         }
371         find_turbo_adapters(ibmtr_portlist);
372         for (i = 0; ibmtr_portlist[i]; i++) {
373                 int ioaddr = ibmtr_portlist[i];
374
375                 if (!ibmtr_probe1(dev, ioaddr)) return 0;
376         }
377         return -ENODEV;
378 }
379
380 /*****************************************************************************/
381
382 static int __devinit ibmtr_probe1(struct net_device *dev, int PIOaddr)
383 {
384
385         unsigned char segment, intr=0, irq=0, i, j, cardpresent=NOTOK, temp=0;
386         void * t_mmio = NULL;
387         struct tok_info *ti = dev->priv;
388         void *cd_chanid;
389         unsigned char *tchanid, ctemp;
390 #ifndef PCMCIA
391         unsigned char t_irq=0;
392         unsigned long timeout;
393         static int version_printed;
394 #endif
395
396         /*    Query the adapter PIO base port which will return
397          *    indication of where MMIO was placed. We also have a
398          *    coded interrupt number.
399          */
400         segment = inb(PIOaddr);
401         if (segment < 0x40 || segment > 0xe0) {
402                 /* Out of range values so we'll assume non-existent IO device
403                  * but this is not necessarily a problem, esp if a turbo
404                  * adapter is being used.  */
405 #if IBMTR_DEBUG_MESSAGES
406                 DPRINTK("ibmtr_probe1(): unhappy that inb(0x%X) == 0x%X, "
407                         "Hardware Problem?\n",PIOaddr,segment);
408 #endif
409                 return -ENODEV;
410         }
411         /*
412          *    Compute the linear base address of the MMIO area
413          *    as LINUX doesn't care about segments
414          */
415         t_mmio = ioremap(((__u32) (segment & 0xfc) << 11) + 0x80000,2048);
416         if (!t_mmio) { 
417                 DPRINTK("Cannot remap mmiobase memory area") ; 
418                 return -ENODEV ; 
419         } 
420         intr = segment & 0x03;  /* low bits is coded interrupt # */
421         if (ibmtr_debug_trace & TRC_INIT)
422                 DPRINTK("PIOaddr: %4hx seg/intr: %2x mmio base: %p intr: %d\n"
423                                 , PIOaddr, (int) segment, t_mmio, (int) intr);
424
425         /*
426          *    Now we will compare expected 'channelid' strings with
427          *    what we is there to learn of ISA/MCA or not TR card
428          */
429 #ifdef PCMCIA
430         iounmap(t_mmio);
431         t_mmio = (void *)ti->mmio;      /*BMS to get virtual address */
432         irq = ti->irq;          /*BMS to display the irq!   */
433 #endif
434         cd_chanid = (CHANNEL_ID + t_mmio);      /* for efficiency */
435         tchanid = pcchannelid;
436         cardpresent = TR_ISA;   /* try ISA */
437
438         /*    Suboptimize knowing first byte different */
439         ctemp = readb(cd_chanid) & 0x0f;
440         if (ctemp != *tchanid) {        /* NOT ISA card, try MCA */
441                 tchanid = mcchannelid;
442                 cardpresent = TR_MCA;
443                 if (ctemp != *tchanid)  /* Neither ISA nor MCA */
444                         cardpresent = NOTOK;
445         }
446         if (cardpresent != NOTOK) {
447                 /*       Know presumed type, try rest of ID */
448                 for (i = 2, j = 1; i <= 46; i = i + 2, j++) {
449                         if( (readb(cd_chanid+i)&0x0f) == tchanid[j]) continue;
450                         /* match failed, not TR card */
451                         cardpresent = NOTOK;
452                         break;
453                 }
454         }
455         /* 
456          *    If we have an ISA board check for the ISA P&P version,
457          *    as it has different IRQ settings 
458          */
459         if (cardpresent == TR_ISA && (readb(AIPFID + t_mmio) == 0x0e))
460                 cardpresent = TR_ISAPNP;
461         if (cardpresent == NOTOK) {     /* "channel_id" did not match, report */
462                 if (!(ibmtr_debug_trace & TRC_INIT)) {
463 #ifndef PCMCIA
464                         iounmap(t_mmio);
465 #endif
466                         return -ENODEV;
467                 }
468                 DPRINTK( "Channel ID string not found for PIOaddr: %4hx\n",
469                                                                 PIOaddr);
470                 DPRINTK("Expected for ISA: ");
471                 PrtChanID(pcchannelid, 1);
472                 DPRINTK("           found: ");
473 /* BMS Note that this can be misleading, when hardware is flaky, because you
474    are reading it a second time here. So with my flaky hardware, I'll see my-
475    self in this block, with the HW ID matching the ISA ID exactly! */
476                 HWPrtChanID(cd_chanid, 2);
477                 DPRINTK("Expected for MCA: ");
478                 PrtChanID(mcchannelid, 1);
479         }
480         /* Now, setup some of the pl0 buffers for this driver.. */
481         /* If called from PCMCIA, it is already set up, so no need to 
482            waste the memory, just use the existing structure */
483 #ifndef PCMCIA
484         ti->mmio = t_mmio;
485         for (i = 0; i < IBMTR_MAX_ADAPTERS; i++) {
486                 if (turbo_io[i] != PIOaddr)
487                         continue;
488 #if IBMTR_DEBUG_MESSAGES 
489                 printk("ibmtr::tr_probe1, setting PIOaddr %x to Turbo\n",
490                        PIOaddr);
491 #endif
492                 ti->turbo = 1;
493                 t_irq = turbo_irq[i];
494         }
495 #endif /* !PCMCIA */
496         ti->readlog_pending = 0;
497         init_waitqueue_head(&ti->wait_for_reset);
498
499         /* if PCMCIA, the card can be recognized as either TR_ISA or TR_ISAPNP
500          * depending which card is inserted.    */
501         
502 #ifndef PCMCIA
503         switch (cardpresent) {
504         case TR_ISA:
505                 if (intr == 0) irq = 9; /* irq2 really is irq9 */
506                 if (intr == 1) irq = 3;
507                 if (intr == 2) irq = 6;
508                 if (intr == 3) irq = 7;
509                 ti->adapter_int_enable = PIOaddr + ADAPTINTREL;
510                 break;
511         case TR_MCA:
512                 if (intr == 0) irq = 9;
513                 if (intr == 1) irq = 3;
514                 if (intr == 2) irq = 10;
515                 if (intr == 3) irq = 11;
516                 ti->global_int_enable = 0;
517                 ti->adapter_int_enable = 0;
518                 ti->sram_virt=(__u32)(inb(PIOaddr+ADAPTRESETREL) & 0xfe) << 12;
519                 break;
520         case TR_ISAPNP:
521                 if (!t_irq) {
522                         if (intr == 0) irq = 9;
523                         if (intr == 1) irq = 3;
524                         if (intr == 2) irq = 10;
525                         if (intr == 3) irq = 11;
526                 } else
527                         irq=t_irq;
528                 timeout = jiffies + TR_SPIN_INTERVAL;
529                 while (!readb(ti->mmio + ACA_OFFSET + ACA_RW + RRR_EVEN)){
530                         if (!time_after(jiffies, timeout)) continue;
531                         DPRINTK( "Hardware timeout during initialization.\n");
532                         iounmap(t_mmio);
533                         kfree(ti);
534                         return -ENODEV;
535                 }
536                 ti->sram_virt =
537                      ((__u32)readb(ti->mmio+ACA_OFFSET+ACA_RW+RRR_EVEN)<<12);
538                 ti->adapter_int_enable = PIOaddr + ADAPTINTREL;
539                 break;
540         } /*end switch (cardpresent) */
541 #endif  /*not PCMCIA */
542
543         if (ibmtr_debug_trace & TRC_INIT) {     /* just report int */
544                 DPRINTK("irq=%d", irq);
545                 printk(", sram_virt=0x%x", ti->sram_virt);
546                 if(ibmtr_debug_trace&TRC_INITV){ /* full chat in verbose only */
547                         DPRINTK(", ti->mmio=%p", ti->mmio);
548                         printk(", segment=%02X", segment);
549                 }
550                 printk(".\n");
551         }
552
553         /* Get hw address of token ring card */
554         j = 0;
555         for (i = 0; i < 0x18; i = i + 2) {
556                 /* technical reference states to do this */
557                 temp = readb(ti->mmio + AIP + i) & 0x0f;
558                 ti->hw_address[j] = temp;
559                 if (j & 1)
560                         dev->dev_addr[(j / 2)] =
561                                 ti->hw_address[j]+ (ti->hw_address[j - 1] << 4);
562                 ++j;
563         }
564         /* get Adapter type:  'F' = Adapter/A, 'E' = 16/4 Adapter II,... */
565         ti->adapter_type = readb(ti->mmio + AIPADAPTYPE);
566
567         /* get Data Rate:  F=4Mb, E=16Mb, D=4Mb & 16Mb ?? */
568         ti->data_rate = readb(ti->mmio + AIPDATARATE);
569
570         /* Get Early Token Release support?: F=no, E=4Mb, D=16Mb, C=4&16Mb */
571         ti->token_release = readb(ti->mmio + AIPEARLYTOKEN);
572
573         /* How much shared RAM is on adapter ? */
574         if (ti->turbo) {
575                 ti->avail_shared_ram=127;
576         } else {
577                 ti->avail_shared_ram = get_sram_size(ti);/*in 512 byte units */
578         }
579         /* We need to set or do a bunch of work here based on previous results*/
580         /* Support paging?  What sizes?:  F=no, E=16k, D=32k, C=16 & 32k */
581         ti->shared_ram_paging = readb(ti->mmio + AIPSHRAMPAGE);
582
583         /* Available DHB  4Mb size:   F=2048, E=4096, D=4464 */
584         switch (readb(ti->mmio + AIP4MBDHB)) {
585         case 0xe: ti->dhb_size4mb = 4096; break;
586         case 0xd: ti->dhb_size4mb = 4464; break;
587         default:  ti->dhb_size4mb = 2048; break;
588         }
589
590         /* Available DHB 16Mb size:  F=2048, E=4096, D=8192, C=16384, B=17960 */
591         switch (readb(ti->mmio + AIP16MBDHB)) {
592         case 0xe: ti->dhb_size16mb = 4096; break;
593         case 0xd: ti->dhb_size16mb = 8192; break;
594         case 0xc: ti->dhb_size16mb = 16384; break;
595         case 0xb: ti->dhb_size16mb = 17960; break;
596         default:  ti->dhb_size16mb = 2048; break;
597         }
598
599         /*    We must figure out how much shared memory space this adapter
600          *    will occupy so that if there are two adapters we can fit both
601          *    in.  Given a choice, we will limit this adapter to 32K.  The
602          *    maximum space will will use for two adapters is 64K so if the
603          *    adapter we are working on demands 64K (it also doesn't support
604          *    paging), then only one adapter can be supported.  
605          */
606
607         /*
608          *    determine how much of total RAM is mapped into PC space 
609          */
610         ti->mapped_ram_size= /*sixteen to onehundredtwentyeight 512byte blocks*/
611             1<< ((readb(ti->mmio+ACA_OFFSET+ACA_RW+RRR_ODD) >> 2 & 0x03) + 4);
612         ti->page_mask = 0;
613         if (ti->turbo)  ti->page_mask=0xf0;
614         else if (ti->shared_ram_paging == 0xf);  /* No paging in adapter */
615         else {
616 #ifdef ENABLE_PAGING
617                 unsigned char pg_size = 0;
618                 /* BMS:   page size: PCMCIA, use configuration register;
619                    ISAPNP, use LANAIDC config tool from www.ibm.com  */
620                 switch (ti->shared_ram_paging) {
621                 case 0xf:
622                         break;
623                 case 0xe:
624                         ti->page_mask = (ti->mapped_ram_size == 32) ? 0xc0 : 0;
625                         pg_size = 32;   /* 16KB page size */
626                         break;
627                 case 0xd:
628                         ti->page_mask = (ti->mapped_ram_size == 64) ? 0x80 : 0;
629                         pg_size = 64;   /* 32KB page size */
630                         break;
631                 case 0xc:
632                         switch (ti->mapped_ram_size) {
633                         case 32:
634                                 ti->page_mask = 0xc0;
635                                 pg_size = 32;
636                                 break;
637                         case 64:
638                                 ti->page_mask = 0x80;
639                                 pg_size = 64;
640                                 break;
641                         }
642                         break;
643                 default:
644                         DPRINTK("Unknown shared ram paging info %01X\n",
645                                                         ti->shared_ram_paging);
646                         iounmap(t_mmio); 
647                         kfree(ti);
648                         return -ENODEV;
649                         break;
650                 } /*end switch shared_ram_paging */
651
652                 if (ibmtr_debug_trace & TRC_INIT)
653                         DPRINTK("Shared RAM paging code: %02X, "
654                                 "mapped RAM size: %dK, shared RAM size: %dK, "
655                                 "page mask: %02X\n:",
656                                 ti->shared_ram_paging, ti->mapped_ram_size / 2,
657                                 ti->avail_shared_ram / 2, ti->page_mask);
658 #endif  /*ENABLE_PAGING */
659         }
660
661 #ifndef PCMCIA
662         /* finish figuring the shared RAM address */
663         if (cardpresent == TR_ISA) {
664                 static __u32 ram_bndry_mask[] =
665                         { 0xffffe000, 0xffffc000, 0xffff8000, 0xffff0000 };
666                 __u32 new_base, rrr_32, chk_base, rbm;
667
668                 rrr_32=readb(ti->mmio+ACA_OFFSET+ACA_RW+RRR_ODD) >> 2 & 0x03;
669                 rbm = ram_bndry_mask[rrr_32];
670                 new_base = (ibmtr_mem_base + (~rbm)) & rbm;/* up to boundary */
671                 chk_base = new_base + (ti->mapped_ram_size << 9);
672                 if (chk_base > (ibmtr_mem_base + IBMTR_SHARED_RAM_SIZE)) {
673                         DPRINTK("Shared RAM for this adapter (%05x) exceeds "
674                         "driver limit (%05x), adapter not started.\n",
675                         chk_base, ibmtr_mem_base + IBMTR_SHARED_RAM_SIZE);
676                         iounmap(t_mmio);
677                         kfree(ti);
678                         return -ENODEV;
679                 } else { /* seems cool, record what we have figured out */
680                         ti->sram_base = new_base >> 12;
681                         ibmtr_mem_base = chk_base;
682                 }
683         }
684         else  ti->sram_base = ti->sram_virt >> 12;
685
686         /* The PCMCIA has already got the interrupt line and the io port, 
687            so no chance of anybody else getting it - MLP */
688         if (request_irq(dev->irq = irq, &tok_interrupt, 0, "ibmtr", dev) != 0) {
689                 DPRINTK("Could not grab irq %d.  Halting Token Ring driver.\n",
690                                         irq);
691                 iounmap(t_mmio);
692                 kfree(ti);
693                 return -ENODEV;
694         }
695         /*?? Now, allocate some of the PIO PORTs for this driver.. */
696         /* record PIOaddr range as busy */
697         if (!request_region(PIOaddr, IBMTR_IO_EXTENT, "ibmtr")) {
698                 DPRINTK("Could not grab PIO range. Halting driver.\n");
699                 free_irq(dev->irq, dev);
700                 iounmap(t_mmio);
701                 kfree(ti);
702                 return -EBUSY;
703         }
704
705         if (!version_printed++) {
706                 printk(version);
707         }
708 #endif /* !PCMCIA */
709         DPRINTK("%s %s found\n",
710                 channel_def[cardpresent - 1], adapter_def(ti->adapter_type));
711         DPRINTK("using irq %d, PIOaddr %hx, %dK shared RAM.\n",
712                         irq, PIOaddr, ti->mapped_ram_size / 2);
713         DPRINTK("Hardware address : %02X:%02X:%02X:%02X:%02X:%02X\n",
714                 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
715                 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
716         if (ti->page_mask)
717                 DPRINTK("Shared RAM paging enabled. "
718                         "Page size: %uK Shared Ram size %dK\n",
719                         ((ti->page_mask^0xff)+1) >>2, ti->avail_shared_ram / 2);
720         else
721                 DPRINTK("Shared RAM paging disabled. ti->page_mask %x\n",
722                                                                 ti->page_mask);
723
724         /* Calculate the maximum DHB we can use */
725         /* two cases where avail_shared_ram doesn't equal mapped_ram_size:
726             1. avail_shared_ram is 127 but mapped_ram_size is 128 (typical)
727             2. user has configured adapter for less than avail_shared_ram
728                but is not using paging (she should use paging, I believe)
729         */
730         if (!ti->page_mask) {
731                 ti->avail_shared_ram=
732                                 min(ti->mapped_ram_size,ti->avail_shared_ram);
733         }
734
735         switch (ti->avail_shared_ram) {
736         case 16:                /* 8KB shared RAM */
737                 ti->dhb_size4mb = min(ti->dhb_size4mb, (unsigned short)2048);
738                 ti->rbuf_len4 = 1032;
739                 ti->rbuf_cnt4=2;
740                 ti->dhb_size16mb = min(ti->dhb_size16mb, (unsigned short)2048);
741                 ti->rbuf_len16 = 1032;
742                 ti->rbuf_cnt16=2;
743                 break;
744         case 32:                /* 16KB shared RAM */
745                 ti->dhb_size4mb = min(ti->dhb_size4mb, (unsigned short)4464);
746                 ti->rbuf_len4 = 1032;
747                 ti->rbuf_cnt4=4;
748                 ti->dhb_size16mb = min(ti->dhb_size16mb, (unsigned short)4096);
749                 ti->rbuf_len16 = 1032;  /*1024 usable */
750                 ti->rbuf_cnt16=4;
751                 break;
752         case 64:                /* 32KB shared RAM */
753                 ti->dhb_size4mb = min(ti->dhb_size4mb, (unsigned short)4464);
754                 ti->rbuf_len4 = 1032;
755                 ti->rbuf_cnt4=6;
756                 ti->dhb_size16mb = min(ti->dhb_size16mb, (unsigned short)10240);
757                 ti->rbuf_len16 = 1032;
758                 ti->rbuf_cnt16=6;
759                 break;
760         case 127:               /* 63.5KB shared RAM */
761                 ti->dhb_size4mb = min(ti->dhb_size4mb, (unsigned short)4464);
762                 ti->rbuf_len4 = 1032;
763                 ti->rbuf_cnt4=6;
764                 ti->dhb_size16mb = min(ti->dhb_size16mb, (unsigned short)16384);
765                 ti->rbuf_len16 = 1032;
766                 ti->rbuf_cnt16=16;
767                 break;
768         case 128:               /* 64KB   shared RAM */
769                 ti->dhb_size4mb = min(ti->dhb_size4mb, (unsigned short)4464);
770                 ti->rbuf_len4 = 1032;
771                 ti->rbuf_cnt4=6;
772                 ti->dhb_size16mb = min(ti->dhb_size16mb, (unsigned short)17960);
773                 ti->rbuf_len16 = 1032;
774                 ti->rbuf_cnt16=16;
775                 break;
776         default:
777                 ti->dhb_size4mb = 2048;
778                 ti->rbuf_len4 = 1032;
779                 ti->rbuf_cnt4=2;
780                 ti->dhb_size16mb = 2048;
781                 ti->rbuf_len16 = 1032;
782                 ti->rbuf_cnt16=2;
783                 break;
784         }
785         /* this formula is not smart enough for the paging case
786         ti->rbuf_cnt<x> = (ti->avail_shared_ram * BLOCKSZ - ADAPT_PRIVATE -
787                         ARBLENGTH - SSBLENGTH - DLC_MAX_SAP * SAPLENGTH -
788                         DLC_MAX_STA * STALENGTH - ti->dhb_size<x>mb * NUM_DHB -
789                         SRBLENGTH - ASBLENGTH) / ti->rbuf_len<x>;
790         */
791         ti->maxmtu16 = (ti->rbuf_len16 - 8) * ti->rbuf_cnt16  - TR_HLEN;
792         ti->maxmtu4 = (ti->rbuf_len4 - 8) * ti->rbuf_cnt4 - TR_HLEN;
793         /*BMS assuming 18 bytes of Routing Information (usually works) */
794         DPRINTK("Maximum Receive Internet Protocol MTU 16Mbps: %d, 4Mbps: %d\n",
795                                                      ti->maxmtu16, ti->maxmtu4);
796
797         dev->base_addr = PIOaddr;       /* set the value for device */
798         dev->mem_start = ti->sram_base << 12;
799         dev->mem_end = dev->mem_start + (ti->mapped_ram_size << 9) - 1;
800         trdev_init(dev);
801         return 0;   /* Return 0 to indicate we have found a Token Ring card. */
802 }                               /*ibmtr_probe1() */
803
804 /*****************************************************************************/
805
806 /* query the adapter for the size of shared RAM  */
807 /* the function returns the RAM size in units of 512 bytes */
808
809 static unsigned char __devinit get_sram_size(struct tok_info *adapt_info)
810 {
811         unsigned char avail_sram_code;
812         static unsigned char size_code[] = { 0, 16, 32, 64, 127, 128 };
813         /* Adapter gives
814            'F' -- use RRR bits 3,2
815            'E' -- 8kb   'D' -- 16kb
816            'C' -- 32kb  'A' -- 64KB
817            'B' - 64KB less 512 bytes at top
818            (WARNING ... must zero top bytes in INIT */
819
820         avail_sram_code = 0xf - readb(adapt_info->mmio + AIPAVAILSHRAM);
821         if (avail_sram_code) return size_code[avail_sram_code];
822         else            /* for code 'F', must compute size from RRR(3,2) bits */
823                 return 1 <<
824                  ((readb(adapt_info->mmio+ACA_OFFSET+ACA_RW+RRR_ODD)>>2&3)+4);
825 }
826
827 /*****************************************************************************/
828
829 static int __devinit trdev_init(struct net_device *dev)
830 {
831         struct tok_info *ti = (struct tok_info *) dev->priv;
832
833         SET_PAGE(ti->srb_page);
834         ti->open_failure = NO    ;
835         dev->open = tok_open;
836         dev->stop = tok_close;
837         dev->hard_start_xmit = tok_send_packet;
838         dev->get_stats = tok_get_stats;
839         dev->set_multicast_list = tok_set_multicast_list;
840         dev->change_mtu = ibmtr_change_mtu;
841
842         return 0;
843 }
844
845 /*****************************************************************************/
846
847 static int tok_init_card(struct net_device *dev)
848 {
849         struct tok_info *ti;
850         short PIOaddr;
851         unsigned long i;
852         wait_queue_t __wait;
853         init_waitqueue_entry(&__wait, current);
854
855         PIOaddr = dev->base_addr;
856         ti = (struct tok_info *) dev->priv;
857         /* Special processing for first interrupt after reset */
858         ti->do_tok_int = FIRST_INT;
859         /* Reset adapter */
860         writeb(~INT_ENABLE, ti->mmio + ACA_OFFSET + ACA_RESET + ISRP_EVEN);
861         outb(0, PIOaddr + ADAPTRESET);
862
863         current->state=TASK_UNINTERRUPTIBLE;
864         schedule_timeout(TR_RST_TIME); /* wait 50ms */
865
866         add_wait_queue(&ti->wait_for_reset, &__wait);
867         set_current_state(TASK_UNINTERRUPTIBLE);
868         outb(0, PIOaddr + ADAPTRESETREL);
869 #ifdef ENABLE_PAGING
870         if (ti->page_mask)
871                 writeb(SRPR_ENABLE_PAGING,ti->mmio+ACA_OFFSET+ACA_RW+SRPR_EVEN);
872 #endif
873         writeb(INT_ENABLE, ti->mmio + ACA_OFFSET + ACA_SET + ISRP_EVEN);
874         #warning pci posting bug
875         i = schedule_timeout(4 * HZ);
876         current->state = TASK_RUNNING;
877         remove_wait_queue(&ti->wait_for_reset, &__wait);
878         return i? 0 : -EAGAIN;
879 }
880
881 /*****************************************************************************/
882 static int tok_open(struct net_device *dev)
883 {
884         struct tok_info *ti = (struct tok_info *) dev->priv;
885         int i;
886
887         /*the case we were left in a failure state during a previous open */
888         if (ti->open_failure == YES) {
889                 DPRINTK("Last time you were disconnected, how about now?\n");
890                 printk("You can't insert with an ICS connector half-cocked.\n");
891         }
892
893         ti->open_status  = CLOSED; /* CLOSED or OPEN      */
894         ti->sap_status   = CLOSED; /* CLOSED or OPEN      */
895         ti->open_failure =     NO; /* NO     or YES       */
896         ti->open_mode    = MANUAL; /* MANUAL or AUTOMATIC */
897         /* 12/2000 not typical Linux, but we can use RUNNING to let us know when
898         the network has crapped out or cables are disconnected. Useful because
899         the IFF_UP flag stays up the whole time, until ifconfig tr0 down.
900         */
901         dev->flags &= ~IFF_RUNNING;
902
903         ti->sram_virt &= ~1; /* to reverse what we do in tok_close */
904         /* init the spinlock */
905         spin_lock_init(&ti->lock);
906         init_timer(&ti->tr_timer);
907         
908         i = tok_init_card(dev);
909         if (i) return i;
910
911         while (1){
912                 tok_open_adapter((unsigned long) dev);
913                 i= interruptible_sleep_on_timeout(&ti->wait_for_reset, 25 * HZ);
914                 /* sig catch: estimate opening adapter takes more than .5 sec*/
915                 if (i>(245*HZ)/10) break; /* fancier than if (i==25*HZ) */
916                 if (i==0) break;
917                 if (ti->open_status == OPEN && ti->sap_status==OPEN) {
918                         netif_start_queue(dev);
919                         DPRINTK("Adapter is up and running\n");
920                         return 0;
921                 }
922                 current->state=TASK_INTERRUPTIBLE;
923                 i=schedule_timeout(TR_RETRY_INTERVAL); /* wait 30 seconds */
924                 if(i!=0) break; /*prob. a signal, like the i>24*HZ case above */
925         }
926         outb(0, dev->base_addr + ADAPTRESET);/* kill pending interrupts*/
927         DPRINTK("TERMINATED via signal\n");     /*BMS useful */
928         return -EAGAIN;
929 }
930
931 /*****************************************************************************/
932
933 #define COMMAND_OFST             0
934 #define OPEN_OPTIONS_OFST        8
935 #define NUM_RCV_BUF_OFST        24
936 #define RCV_BUF_LEN_OFST        26
937 #define DHB_LENGTH_OFST         28
938 #define NUM_DHB_OFST            30
939 #define DLC_MAX_SAP_OFST        32
940 #define DLC_MAX_STA_OFST        33
941
942 void tok_open_adapter(unsigned long dev_addr)
943 {
944         struct net_device *dev = (struct net_device *) dev_addr;
945         struct tok_info *ti;
946         int i;
947
948         ti = (struct tok_info *) dev->priv;
949         SET_PAGE(ti->init_srb_page); 
950         writeb(~SRB_RESP_INT, ti->mmio + ACA_OFFSET + ACA_RESET + ISRP_ODD);
951         for (i = 0; i < sizeof(struct dir_open_adapter); i++)
952                 writeb(0, ti->init_srb + i);
953         writeb(DIR_OPEN_ADAPTER, ti->init_srb + COMMAND_OFST);
954         writew(htons(OPEN_PASS_BCON_MAC), ti->init_srb + OPEN_OPTIONS_OFST);
955         if (ti->ring_speed == 16) {
956                 writew(htons(ti->dhb_size16mb), ti->init_srb + DHB_LENGTH_OFST);
957                 writew(htons(ti->rbuf_cnt16), ti->init_srb + NUM_RCV_BUF_OFST);
958                 writew(htons(ti->rbuf_len16), ti->init_srb + RCV_BUF_LEN_OFST);
959         } else {
960                 writew(htons(ti->dhb_size4mb), ti->init_srb + DHB_LENGTH_OFST);
961                 writew(htons(ti->rbuf_cnt4), ti->init_srb + NUM_RCV_BUF_OFST);
962                 writew(htons(ti->rbuf_len4), ti->init_srb + RCV_BUF_LEN_OFST);
963         }
964         writeb(NUM_DHB,         /* always 2 */ ti->init_srb + NUM_DHB_OFST);
965         writeb(DLC_MAX_SAP, ti->init_srb + DLC_MAX_SAP_OFST);
966         writeb(DLC_MAX_STA, ti->init_srb + DLC_MAX_STA_OFST);
967         ti->srb = ti->init_srb; /* We use this one in the interrupt handler */
968         ti->srb_page = ti->init_srb_page;
969         DPRINTK("Opening adapter: Xmit bfrs: %d X %d, Rcv bfrs: %d X %d\n",
970                 readb(ti->init_srb + NUM_DHB_OFST),
971                 ntohs(readw(ti->init_srb + DHB_LENGTH_OFST)),
972                 ntohs(readw(ti->init_srb + NUM_RCV_BUF_OFST)),
973                 ntohs(readw(ti->init_srb + RCV_BUF_LEN_OFST)));
974         writeb(INT_ENABLE, ti->mmio + ACA_OFFSET + ACA_SET + ISRP_EVEN);
975         writeb(CMD_IN_SRB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
976 }
977
978 /*****************************************************************************/
979
980 static void open_sap(unsigned char type, struct net_device *dev)
981 {
982         int i;
983         struct tok_info *ti = (struct tok_info *) dev->priv;
984
985         SET_PAGE(ti->srb_page);
986         for (i = 0; i < sizeof(struct dlc_open_sap); i++)
987                 writeb(0, ti->srb + i);
988
989 #define MAX_I_FIELD_OFST        14
990 #define SAP_VALUE_OFST          16
991 #define SAP_OPTIONS_OFST        17
992 #define STATION_COUNT_OFST      18
993
994         writeb(DLC_OPEN_SAP, ti->srb + COMMAND_OFST);
995         writew(htons(MAX_I_FIELD), ti->srb + MAX_I_FIELD_OFST);
996         writeb(SAP_OPEN_IND_SAP | SAP_OPEN_PRIORITY, ti->srb+ SAP_OPTIONS_OFST);
997         writeb(SAP_OPEN_STATION_CNT, ti->srb + STATION_COUNT_OFST);
998         writeb(type, ti->srb + SAP_VALUE_OFST);
999         writeb(CMD_IN_SRB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
1000 }
1001
1002
1003 /*****************************************************************************/
1004
1005 static void tok_set_multicast_list(struct net_device *dev)
1006 {
1007         struct tok_info *ti = (struct tok_info *) dev->priv;
1008         struct dev_mc_list *mclist;
1009         unsigned char address[4];
1010
1011         int i;
1012
1013         /*BMS the next line is CRUCIAL or you may be sad when you */
1014         /*BMS ifconfig tr down or hot unplug a PCMCIA card ??hownowbrowncow*/
1015         if (/*BMSHELPdev->start == 0 ||*/ ti->open_status != OPEN) return;
1016         address[0] = address[1] = address[2] = address[3] = 0;
1017         mclist = dev->mc_list;
1018         for (i = 0; i < dev->mc_count; i++) {
1019                 address[0] |= mclist->dmi_addr[2];
1020                 address[1] |= mclist->dmi_addr[3];
1021                 address[2] |= mclist->dmi_addr[4];
1022                 address[3] |= mclist->dmi_addr[5];
1023                 mclist = mclist->next;
1024         }
1025         SET_PAGE(ti->srb_page);
1026         for (i = 0; i < sizeof(struct srb_set_funct_addr); i++)
1027                 writeb(0, ti->srb + i);
1028
1029 #define FUNCT_ADDRESS_OFST 6
1030
1031         writeb(DIR_SET_FUNC_ADDR, ti->srb + COMMAND_OFST);
1032         for (i = 0; i < 4; i++) 
1033                 writeb(address[i], ti->srb + FUNCT_ADDRESS_OFST + i);
1034         writeb(CMD_IN_SRB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
1035 #if TR_VERBOSE
1036         DPRINTK("Setting functional address: ");
1037         for (i=0;i<4;i++)  printk("%02X ", address[i]);
1038         printk("\n");
1039 #endif
1040 }
1041
1042 /*****************************************************************************/
1043
1044 #define STATION_ID_OFST 4
1045
1046 static int tok_send_packet(struct sk_buff *skb, struct net_device *dev)
1047 {
1048         struct tok_info *ti;
1049         unsigned long flags;
1050         ti = (struct tok_info *) dev->priv;
1051
1052         netif_stop_queue(dev);
1053
1054         /* lock against other CPUs */
1055         spin_lock_irqsave(&(ti->lock), flags);
1056
1057         /* Save skb; we'll need it when the adapter asks for the data */
1058         ti->current_skb = skb;
1059         SET_PAGE(ti->srb_page);
1060         writeb(XMIT_UI_FRAME, ti->srb + COMMAND_OFST);
1061         writew(ti->exsap_station_id, ti->srb + STATION_ID_OFST);
1062         writeb(CMD_IN_SRB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
1063         spin_unlock_irqrestore(&(ti->lock), flags);
1064         dev->trans_start = jiffies;
1065         return 0;
1066 }
1067
1068 /*****************************************************************************/
1069
1070 static int tok_close(struct net_device *dev)
1071 {
1072         struct tok_info *ti = (struct tok_info *) dev->priv;
1073
1074         /* Important for PCMCIA hot unplug, otherwise, we'll pull the card, */
1075         /* unloading the module from memory, and then if a timer pops, ouch */
1076         del_timer_sync(&ti->tr_timer);
1077         outb(0, dev->base_addr + ADAPTRESET);
1078         ti->sram_virt |= 1;
1079         ti->open_status = CLOSED;
1080
1081         netif_stop_queue(dev);
1082         DPRINTK("Adapter is closed.\n");
1083         return 0;
1084 }
1085
1086 /*****************************************************************************/
1087
1088 #define RETCODE_OFST            2
1089 #define OPEN_ERROR_CODE_OFST    6
1090 #define ASB_ADDRESS_OFST        8
1091 #define SRB_ADDRESS_OFST        10
1092 #define ARB_ADDRESS_OFST        12
1093 #define SSB_ADDRESS_OFST        14
1094
1095 static char *printphase[]= {"Lobe media test","Physical insertion",
1096               "Address verification","Roll call poll","Request Parameters"};
1097 static char *printerror[]={"Function failure","Signal loss","Reserved",
1098                 "Frequency error","Timeout","Ring failure","Ring beaconing",
1099                 "Duplicate node address",
1100                 "Parameter request-retry count exceeded","Remove received",
1101                 "IMPL force received","Duplicate modifier",
1102                 "No monitor detected","Monitor contention failed for RPL"};
1103
1104 void dir_open_adapter (struct net_device *dev) {
1105
1106         struct tok_info *ti = (struct tok_info *) dev->priv;
1107         unsigned char ret_code;
1108         __u16 err;
1109
1110         ti->srb = ntohs(readw(ti->init_srb + SRB_ADDRESS_OFST));
1111         ti->ssb = ntohs(readw(ti->init_srb + SSB_ADDRESS_OFST));
1112         ti->arb = ntohs(readw(ti->init_srb + ARB_ADDRESS_OFST));
1113         ti->asb = ntohs(readw(ti->init_srb + ASB_ADDRESS_OFST));
1114         if (ti->page_mask) {
1115                 ti->srb_page = (ti->srb >> 8) & ti->page_mask;
1116                 ti->srb &= ~(ti->page_mask << 8);
1117                 ti->ssb_page = (ti->ssb >> 8) & ti->page_mask;
1118                 ti->ssb &= ~(ti->page_mask << 8);
1119                 ti->arb_page = (ti->arb >> 8) & ti->page_mask;
1120                 ti->arb &= ~(ti->page_mask << 8);
1121                 ti->asb_page = (ti->asb >> 8) & ti->page_mask;
1122                 ti->asb &= ~(ti->page_mask << 8);
1123         }
1124         ti->srb += ti->sram_virt;
1125         ti->ssb += ti->sram_virt;
1126         ti->arb += ti->sram_virt;
1127         ti->asb += ti->sram_virt;
1128         ti->current_skb = NULL;
1129         ret_code = readb(ti->init_srb + RETCODE_OFST);
1130         err = ntohs(readw(ti->init_srb + OPEN_ERROR_CODE_OFST));
1131         if (!ret_code) {
1132                 ti->open_status = OPEN; /* TR adapter is now available */
1133                 if (ti->open_mode == AUTOMATIC) {
1134                         DPRINTK("Adapter reopened.\n");
1135                 }
1136                 writeb(~SRB_RESP_INT, ti->mmio+ACA_OFFSET+ACA_RESET+ISRP_ODD);
1137                 open_sap(EXTENDED_SAP, dev);
1138                 return;
1139         }
1140         ti->open_failure = YES;
1141         if (ret_code == 7){
1142                if (err == 0x24) {
1143                         if (!ti->auto_speedsave) {
1144                                 DPRINTK("Open failed: Adapter speed must match "
1145                                  "ring speed if Automatic Ring Speed Save is "
1146                                  "disabled.\n");
1147                                 ti->open_action = FAIL;
1148                         }else
1149                                 DPRINTK("Retrying open to adjust to "
1150                                         "ring speed, ");
1151                 } else if (err == 0x2d) {
1152                         DPRINTK("Physical Insertion: No Monitor Detected, ");
1153                         printk("retrying after %ds delay...\n",
1154                                         TR_RETRY_INTERVAL/HZ);
1155                 } else if (err == 0x11) {
1156                         DPRINTK("Lobe Media Function Failure (0x11), ");
1157                         printk(" retrying after %ds delay...\n",
1158                                         TR_RETRY_INTERVAL/HZ);
1159                 } else {
1160                         char **prphase = printphase;
1161                         char **prerror = printerror;
1162                         DPRINTK("TR Adapter misc open failure, error code = ");
1163                         printk("0x%x, Phase: %s, Error: %s\n",
1164                                 err, prphase[err/16 -1], prerror[err%16 -1]);
1165                         printk(" retrying after %ds delay...\n",
1166                                         TR_RETRY_INTERVAL/HZ);
1167                 }
1168         } else DPRINTK("open failed: ret_code = %02X..., ", ret_code);
1169         if (ti->open_action != FAIL) {
1170                 if (ti->open_mode==AUTOMATIC){
1171                         ti->open_action = REOPEN;
1172                         ibmtr_reset_timer(&(ti->tr_timer), dev);
1173                         return;
1174                 }
1175                 wake_up(&ti->wait_for_reset);
1176                 return;
1177         }
1178         DPRINTK("FAILURE, CAPUT\n");
1179 }
1180
1181 /******************************************************************************/
1182
1183 irqreturn_t tok_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1184 {
1185         unsigned char status;
1186         /*  unsigned char status_even ; */
1187         struct tok_info *ti;
1188         struct net_device *dev;
1189 #ifdef ENABLE_PAGING
1190         unsigned char save_srpr;
1191 #endif
1192
1193         dev = dev_id;
1194 #if TR_VERBOSE
1195         DPRINTK("Int from tok_driver, dev : %p irq%d regs=%p\n", dev,irq,regs);
1196 #endif
1197         ti = (struct tok_info *) dev->priv;
1198         if (ti->sram_virt & 1)
1199                 return IRQ_NONE;         /* PCMCIA card extraction flag */
1200         spin_lock(&(ti->lock));
1201 #ifdef ENABLE_PAGING
1202         save_srpr = readb(ti->mmio + ACA_OFFSET + ACA_RW + SRPR_EVEN);
1203 #endif
1204
1205         /* Disable interrupts till processing is finished */
1206         writeb((~INT_ENABLE), ti->mmio + ACA_OFFSET + ACA_RESET + ISRP_EVEN);
1207
1208         /* Reset interrupt for ISA boards */
1209         if (ti->adapter_int_enable)
1210                 outb(0, ti->adapter_int_enable);
1211         else /* used for PCMCIA cards */
1212                 outb(0, ti->global_int_enable);
1213         if (ti->do_tok_int == FIRST_INT){
1214                 initial_tok_int(dev);
1215 #ifdef ENABLE_PAGING
1216                 writeb(save_srpr, ti->mmio + ACA_OFFSET + ACA_RW + SRPR_EVEN);
1217 #endif
1218                 spin_unlock(&(ti->lock));
1219                 return IRQ_HANDLED;
1220         }
1221         /*  Begin interrupt handler HERE inline to avoid the extra
1222             levels of logic and call depth for the original solution. */
1223         status = readb(ti->mmio + ACA_OFFSET + ACA_RW + ISRP_ODD);
1224         /*BMSstatus_even = readb (ti->mmio + ACA_OFFSET + ACA_RW + ISRP_EVEN) */
1225         /*BMSdebugprintk("tok_interrupt: ISRP_ODD = 0x%x ISRP_EVEN = 0x%x\n", */
1226         /*BMS                                       status,status_even);      */
1227
1228         if (status & ADAP_CHK_INT) {
1229                 int i;
1230                 __u32 check_reason;
1231                 __u8 check_reason_page = 0;
1232                 check_reason =
1233                         ntohs(readw(ti->mmio+ ACA_OFFSET+ACA_RW + WWCR_EVEN));
1234                 if (ti->page_mask) {
1235                         check_reason_page = (check_reason >> 8) & ti->page_mask;
1236                         check_reason &= ~(ti->page_mask << 8);
1237                 }
1238                 check_reason += ti->sram_virt;
1239                 SET_PAGE(check_reason_page);
1240
1241                 DPRINTK("Adapter check interrupt\n");
1242                 DPRINTK("8 reason bytes follow: ");
1243                 for (i = 0; i < 8; i++, check_reason++)
1244                         printk("%02X ", (int) readb(check_reason));
1245                 printk("\n");
1246                 writeb(~ADAP_CHK_INT, ti->mmio+ ACA_OFFSET+ACA_RESET+ ISRP_ODD);
1247                 status = readb(ti->mmio + ACA_OFFSET + ACA_RW + ISRA_EVEN);
1248                 DPRINTK("ISRA_EVEN == 0x02%x\n",status);
1249                 ti->open_status = CLOSED;
1250                 ti->sap_status  = CLOSED;
1251                 ti->open_mode   = AUTOMATIC;
1252                 dev->flags &= ~IFF_RUNNING;
1253                 netif_stop_queue(dev);
1254                 ti->open_action = RESTART;
1255                 outb(0, dev->base_addr + ADAPTRESET);
1256                 ibmtr_reset_timer(&(ti->tr_timer), dev);/*BMS try to reopen*/
1257                 spin_unlock(&(ti->lock));
1258                 return IRQ_HANDLED;
1259         }
1260         if (readb(ti->mmio + ACA_OFFSET + ACA_RW + ISRP_EVEN)
1261                 & (TCR_INT | ERR_INT | ACCESS_INT)) {
1262                 DPRINTK("adapter error: ISRP_EVEN : %02x\n",
1263                         (int)readb(ti->mmio+ ACA_OFFSET + ACA_RW + ISRP_EVEN));
1264                 writeb(~(TCR_INT | ERR_INT | ACCESS_INT),
1265                         ti->mmio + ACA_OFFSET + ACA_RESET + ISRP_EVEN);
1266                 status= readb(ti->mmio+ ACA_OFFSET + ACA_RW + ISRA_EVEN);/*BMS*/
1267                 DPRINTK("ISRA_EVEN == 0x02%x\n",status);/*BMS*/
1268                 writeb(INT_ENABLE, ti->mmio + ACA_OFFSET + ACA_SET + ISRP_EVEN);
1269 #ifdef ENABLE_PAGING
1270                 writeb(save_srpr, ti->mmio + ACA_OFFSET + ACA_RW + SRPR_EVEN);
1271 #endif
1272                 spin_unlock(&(ti->lock));
1273                 return IRQ_HANDLED;
1274         }
1275         if (status & SRB_RESP_INT) {    /* SRB response */
1276                 SET_PAGE(ti->srb_page);
1277 #if TR_VERBOSE
1278                 DPRINTK("SRB resp: cmd=%02X rsp=%02X\n",
1279                                 readb(ti->srb), readb(ti->srb + RETCODE_OFST));
1280 #endif
1281                 switch (readb(ti->srb)) {       /* SRB command check */
1282                 case XMIT_DIR_FRAME:{
1283                         unsigned char xmit_ret_code;
1284                         xmit_ret_code = readb(ti->srb + RETCODE_OFST);
1285                         if (xmit_ret_code == 0xff) break;
1286                         DPRINTK("error on xmit_dir_frame request: %02X\n",
1287                                                                 xmit_ret_code);
1288                         if (ti->current_skb) {
1289                                 dev_kfree_skb_irq(ti->current_skb);
1290                                 ti->current_skb = NULL;
1291                         }
1292                         /*dev->tbusy = 0;*/
1293                         netif_wake_queue(dev);
1294                         if (ti->readlog_pending)
1295                                 ibmtr_readlog(dev);
1296                         break;
1297                 }
1298                 case XMIT_UI_FRAME:{
1299                         unsigned char xmit_ret_code;
1300
1301                         xmit_ret_code = readb(ti->srb + RETCODE_OFST);
1302                         if (xmit_ret_code == 0xff) break;
1303                         DPRINTK("error on xmit_ui_frame request: %02X\n",
1304                                                                 xmit_ret_code);
1305                         if (ti->current_skb) {
1306                                 dev_kfree_skb_irq(ti->current_skb);
1307                                 ti->current_skb = NULL;
1308                         }
1309                         netif_wake_queue(dev);
1310                         if (ti->readlog_pending)
1311                                 ibmtr_readlog(dev);
1312                         break;
1313                 }
1314                 case DIR_OPEN_ADAPTER:
1315                         dir_open_adapter(dev);
1316                         break;
1317                 case DLC_OPEN_SAP:
1318                         if (readb(ti->srb + RETCODE_OFST)) {
1319                                 DPRINTK("open_sap failed: ret_code = %02X, "
1320                                         "retrying\n",
1321                                         (int) readb(ti->srb + RETCODE_OFST));
1322                                 ti->open_action = REOPEN;
1323                                 ibmtr_reset_timer(&(ti->tr_timer), dev);
1324                                 break;
1325                         }
1326                         ti->exsap_station_id = readw(ti->srb + STATION_ID_OFST);
1327                         ti->sap_status = OPEN;/* TR adapter is now available */
1328                         if (ti->open_mode==MANUAL){
1329                                 wake_up(&ti->wait_for_reset);
1330                                 break;
1331                         }
1332                         netif_wake_queue(dev);
1333                         dev->flags |= IFF_RUNNING;/*BMS 12/2000*/
1334                         break;
1335                 case DIR_INTERRUPT:
1336                 case DIR_MOD_OPEN_PARAMS:
1337                 case DIR_SET_GRP_ADDR:
1338                 case DIR_SET_FUNC_ADDR:
1339                 case DLC_CLOSE_SAP:
1340                         if (readb(ti->srb + RETCODE_OFST))
1341                                 DPRINTK("error on %02X: %02X\n",
1342                                         (int) readb(ti->srb + COMMAND_OFST),
1343                                         (int) readb(ti->srb + RETCODE_OFST));
1344                         break;
1345                 case DIR_READ_LOG:
1346                         if (readb(ti->srb + RETCODE_OFST)){
1347                                 DPRINTK("error on dir_read_log: %02X\n",
1348                                         (int) readb(ti->srb + RETCODE_OFST));
1349                                 netif_wake_queue(dev);
1350                                 break;
1351                         }
1352 #if IBMTR_DEBUG_MESSAGES
1353
1354 #define LINE_ERRORS_OFST                 0
1355 #define INTERNAL_ERRORS_OFST             1
1356 #define BURST_ERRORS_OFST                2
1357 #define AC_ERRORS_OFST                   3
1358 #define ABORT_DELIMITERS_OFST            4
1359 #define LOST_FRAMES_OFST                 6
1360 #define RECV_CONGEST_COUNT_OFST          7
1361 #define FRAME_COPIED_ERRORS_OFST         8
1362 #define FREQUENCY_ERRORS_OFST            9
1363 #define TOKEN_ERRORS_OFST               10
1364
1365                         DPRINTK("Line errors %02X, Internal errors %02X, "
1366                         "Burst errors %02X\n" "A/C errors %02X, "
1367                         "Abort delimiters %02X, Lost frames %02X\n"
1368                         "Receive congestion count %02X, "
1369                         "Frame copied errors %02X\nFrequency errors %02X, "
1370                         "Token errors %02X\n",
1371                         (int) readb(ti->srb + LINE_ERRORS_OFST),
1372                         (int) readb(ti->srb + INTERNAL_ERRORS_OFST),
1373                         (int) readb(ti->srb + BURST_ERRORS_OFST),
1374                         (int) readb(ti->srb + AC_ERRORS_OFST),
1375                         (int) readb(ti->srb + ABORT_DELIMITERS_OFST),
1376                         (int) readb(ti->srb + LOST_FRAMES_OFST),
1377                         (int) readb(ti->srb + RECV_CONGEST_COUNT_OFST),
1378                         (int) readb(ti->srb + FRAME_COPIED_ERRORS_OFST),
1379                         (int) readb(ti->srb + FREQUENCY_ERRORS_OFST),
1380                         (int) readb(ti->srb + TOKEN_ERRORS_OFST));
1381 #endif
1382                         netif_wake_queue(dev);
1383                         break;
1384                 default:
1385                         DPRINTK("Unknown command %02X encountered\n",
1386                                                 (int) readb(ti->srb));
1387                 }       /* end switch SRB command check */
1388                 writeb(~SRB_RESP_INT, ti->mmio+ ACA_OFFSET+ACA_RESET+ ISRP_ODD);
1389         }       /* if SRB response */
1390         if (status & ASB_FREE_INT) {    /* ASB response */
1391                 SET_PAGE(ti->asb_page);
1392 #if TR_VERBOSE
1393                 DPRINTK("ASB resp: cmd=%02X\n", readb(ti->asb));
1394 #endif
1395
1396                 switch (readb(ti->asb)) {       /* ASB command check */
1397                 case REC_DATA:
1398                 case XMIT_UI_FRAME:
1399                 case XMIT_DIR_FRAME:
1400                         break;
1401                 default:
1402                         DPRINTK("unknown command in asb %02X\n",
1403                                                 (int) readb(ti->asb));
1404                 }       /* switch ASB command check */
1405                 if (readb(ti->asb + 2) != 0xff) /* checks ret_code */
1406                         DPRINTK("ASB error %02X in cmd %02X\n",
1407                                 (int) readb(ti->asb + 2), (int) readb(ti->asb));
1408                 writeb(~ASB_FREE_INT, ti->mmio+ ACA_OFFSET+ACA_RESET+ ISRP_ODD);
1409         }       /* if ASB response */
1410
1411 #define STATUS_OFST             6
1412 #define NETW_STATUS_OFST        6
1413
1414         if (status & ARB_CMD_INT) {     /* ARB response */
1415                 SET_PAGE(ti->arb_page);
1416 #if TR_VERBOSE
1417                 DPRINTK("ARB resp: cmd=%02X\n", readb(ti->arb));
1418 #endif
1419
1420                 switch (readb(ti->arb)) {       /* ARB command check */
1421                 case DLC_STATUS:
1422                         DPRINTK("DLC_STATUS new status: %02X on station %02X\n",
1423                                 ntohs(readw(ti->arb + STATUS_OFST)),
1424                                 ntohs(readw(ti->arb+ STATION_ID_OFST)));
1425                         break;
1426                 case REC_DATA:
1427                         tr_rx(dev);
1428                         break;
1429                 case RING_STAT_CHANGE:{
1430                         unsigned short ring_status;
1431                         ring_status= ntohs(readw(ti->arb + NETW_STATUS_OFST));
1432                         if (ibmtr_debug_trace & TRC_INIT)
1433                                 DPRINTK("Ring Status Change...(0x%x)\n",
1434                                                                 ring_status);
1435                         if(ring_status& (REMOVE_RECV|AUTO_REMOVAL|LOBE_FAULT)){
1436                                 netif_stop_queue(dev);
1437                                 dev->flags &= ~IFF_RUNNING;/*not typical Linux*/
1438                                 DPRINTK("Remove received, or Auto-removal error"
1439                                         ", or Lobe fault\n");
1440                                 DPRINTK("We'll try to reopen the closed adapter"
1441                                         " after a %d second delay.\n",
1442                                                 TR_RETRY_INTERVAL/HZ);
1443                                 /*I was confused: I saw the TR reopening but */
1444                                 /*forgot:with an RJ45 in an RJ45/ICS adapter */
1445                                 /*but adapter not in the ring, the TR will   */
1446                                 /* open, and then soon close and come here.  */
1447                                 ti->open_mode = AUTOMATIC;
1448                                 ti->open_status = CLOSED; /*12/2000 BMS*/
1449                                 ti->open_action = REOPEN;
1450                                 ibmtr_reset_timer(&(ti->tr_timer), dev);
1451                         } else if (ring_status & LOG_OVERFLOW) {
1452                                 if(netif_queue_stopped(dev))
1453                                         ti->readlog_pending = 1;
1454                                 else
1455                                         ibmtr_readlog(dev);
1456                         }
1457                         break;
1458                 }
1459                 case XMIT_DATA_REQ:
1460                         tr_tx(dev);
1461                         break;
1462                 default:
1463                         DPRINTK("Unknown command %02X in arb\n",
1464                                                 (int) readb(ti->arb));
1465                         break;
1466                 }       /* switch ARB command check */
1467                 writeb(~ARB_CMD_INT, ti->mmio+ ACA_OFFSET+ACA_RESET + ISRP_ODD);
1468                 writeb(ARB_FREE, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
1469         }       /* if ARB response */
1470         if (status & SSB_RESP_INT) {    /* SSB response */
1471                 unsigned char retcode;
1472                 SET_PAGE(ti->ssb_page);
1473 #if TR_VERBOSE
1474                 DPRINTK("SSB resp: cmd=%02X rsp=%02X\n",
1475                                 readb(ti->ssb), readb(ti->ssb + 2));
1476 #endif
1477
1478                 switch (readb(ti->ssb)) {       /* SSB command check */
1479                 case XMIT_DIR_FRAME:
1480                 case XMIT_UI_FRAME:
1481                         retcode = readb(ti->ssb + 2);
1482                         if (retcode && (retcode != 0x22))/* checks ret_code */
1483                                 DPRINTK("xmit ret_code: %02X xmit error code: "
1484                                         "%02X\n",
1485                                         (int)retcode, (int)readb(ti->ssb + 6));
1486                         else
1487                                 ti->tr_stats.tx_packets++;
1488                         break;
1489                 case XMIT_XID_CMD:
1490                         DPRINTK("xmit xid ret_code: %02X\n",
1491                                                 (int) readb(ti->ssb + 2));
1492                 default:
1493                         DPRINTK("Unknown command %02X in ssb\n",
1494                                                 (int) readb(ti->ssb));
1495                 }       /* SSB command check */
1496                 writeb(~SSB_RESP_INT, ti->mmio+ ACA_OFFSET+ACA_RESET+ ISRP_ODD);
1497                 writeb(SSB_FREE, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
1498         }       /* if SSB response */
1499 #ifdef ENABLE_PAGING
1500         writeb(save_srpr, ti->mmio + ACA_OFFSET + ACA_RW + SRPR_EVEN);
1501 #endif
1502         writeb(INT_ENABLE, ti->mmio + ACA_OFFSET + ACA_SET + ISRP_EVEN);
1503         spin_unlock(&(ti->lock));
1504         return IRQ_HANDLED;
1505 }                               /*tok_interrupt */
1506
1507 /*****************************************************************************/
1508
1509 #define INIT_STATUS_OFST        1
1510 #define INIT_STATUS_2_OFST      2
1511 #define ENCODED_ADDRESS_OFST    8
1512
1513 static void initial_tok_int(struct net_device *dev)
1514 {
1515
1516         __u32 encoded_addr, hw_encoded_addr;
1517         struct tok_info *ti;
1518         unsigned char init_status; /*BMS 12/2000*/
1519
1520         ti = (struct tok_info *) dev->priv;
1521
1522         ti->do_tok_int = NOT_FIRST;
1523
1524         /* we assign the shared-ram address for ISA devices */
1525         writeb(ti->sram_base, ti->mmio + ACA_OFFSET + ACA_RW + RRR_EVEN);
1526 #ifndef PCMCIA
1527         ti->sram_virt = (u32)ioremap(((__u32)ti->sram_base << 12), ti->avail_shared_ram);
1528 #endif
1529         ti->init_srb = ntohs(readw(ti->mmio + ACA_OFFSET + WRBR_EVEN));
1530         if (ti->page_mask) {
1531                 ti->init_srb_page = (ti->init_srb >> 8) & ti->page_mask;
1532                 ti->init_srb &= ~(ti->page_mask << 8);
1533         }
1534         ti->init_srb += ti->sram_virt;
1535         if (ti->page_mask && ti->avail_shared_ram == 127) {
1536                 int last_512 = 0xfe00, i;
1537                 int last_512_page=0;
1538                 last_512_page=(last_512>>8)&ti->page_mask;
1539                 last_512 &= ~(ti->page_mask << 8);
1540                 /* initialize high section of ram (if necessary) */
1541                 SET_PAGE(last_512_page);
1542                 for (i = 0; i < 512; i++)
1543                         writeb(0, ti->sram_virt + last_512 + i);
1544         }
1545         SET_PAGE(ti->init_srb_page);
1546
1547 #if TR_VERBOSE
1548         {
1549         int i;
1550
1551         DPRINTK("ti->init_srb_page=0x%x\n", ti->init_srb_page);
1552         DPRINTK("init_srb(%x):", (ti->init_srb) );
1553         for (i = 0; i < 20; i++)
1554                 printk("%02X ", (int) readb(ti->init_srb + i));
1555         printk("\n");
1556         }
1557 #endif
1558
1559         hw_encoded_addr = readw(ti->init_srb + ENCODED_ADDRESS_OFST);
1560         encoded_addr = ntohs(hw_encoded_addr);
1561         init_status= /*BMS 12/2000 check for shallow mode possibility (Turbo)*/
1562         readb(ti->init_srb+offsetof(struct srb_init_response,init_status));
1563         /*printk("Initial interrupt: init_status= 0x%02x\n",init_status);*/
1564         ti->ring_speed = init_status & 0x01 ? 16 : 4;
1565         DPRINTK("Initial interrupt : %d Mbps, shared RAM base %08x.\n",
1566                                 ti->ring_speed, (unsigned int)dev->mem_start);
1567         ti->auto_speedsave=readb(ti->init_srb+INIT_STATUS_2_OFST)&4?TRUE:FALSE;
1568
1569         if (ti->open_mode == MANUAL)    wake_up(&ti->wait_for_reset);
1570         else                            tok_open_adapter((unsigned long)dev);
1571         
1572 } /*initial_tok_int() */
1573
1574 /*****************************************************************************/
1575
1576 #define CMD_CORRELATE_OFST      1
1577 #define DHB_ADDRESS_OFST        6
1578
1579 #define FRAME_LENGTH_OFST       6
1580 #define HEADER_LENGTH_OFST      8
1581 #define RSAP_VALUE_OFST         9
1582
1583 static void tr_tx(struct net_device *dev)
1584 {
1585         struct tok_info *ti = (struct tok_info *) dev->priv;
1586         struct trh_hdr *trhdr = (struct trh_hdr *) ti->current_skb->data;
1587         unsigned int hdr_len;
1588         __u32 dhb=0,dhb_base;
1589         unsigned char xmit_command;
1590         int i,dhb_len=0x4000,src_len,src_offset;
1591         struct trllc *llc;
1592         struct srb_xmit xsrb;
1593         __u8 dhb_page = 0;
1594         __u8 llc_ssap;
1595
1596         SET_PAGE(ti->asb_page);
1597
1598         if (readb(ti->asb+RETCODE_OFST) != 0xFF) DPRINTK("ASB not free !!!\n");
1599
1600         /* in providing the transmit interrupts, is telling us it is ready for
1601            data and providing a shared memory address for us to stuff with data.
1602            Here we compute the effective address where we will place data.
1603         */
1604         SET_PAGE(ti->arb_page);
1605         dhb=dhb_base=ntohs(readw(ti->arb + DHB_ADDRESS_OFST));
1606         if (ti->page_mask) {
1607                 dhb_page = (dhb_base >> 8) & ti->page_mask;
1608                 dhb=dhb_base & ~(ti->page_mask << 8);
1609         }
1610         dhb += ti->sram_virt;
1611
1612         /* Figure out the size of the 802.5 header */
1613         if (!(trhdr->saddr[0] & 0x80))  /* RIF present? */
1614                 hdr_len = sizeof(struct trh_hdr) - TR_MAXRIFLEN;
1615         else
1616                 hdr_len = ((ntohs(trhdr->rcf) & TR_RCF_LEN_MASK) >> 8)
1617                     + sizeof(struct trh_hdr) - TR_MAXRIFLEN;
1618
1619         llc = (struct trllc *) (ti->current_skb->data + hdr_len);
1620
1621         llc_ssap = llc->ssap;
1622         SET_PAGE(ti->srb_page);
1623         memcpy_fromio(&xsrb, ti->srb, sizeof(xsrb));
1624         SET_PAGE(ti->asb_page);
1625         xmit_command = xsrb.command;
1626
1627         writeb(xmit_command, ti->asb + COMMAND_OFST);
1628         writew(xsrb.station_id, ti->asb + STATION_ID_OFST);
1629         writeb(llc_ssap, ti->asb + RSAP_VALUE_OFST);
1630         writeb(xsrb.cmd_corr, ti->asb + CMD_CORRELATE_OFST);
1631         writeb(0, ti->asb + RETCODE_OFST);
1632         if ((xmit_command == XMIT_XID_CMD) || (xmit_command == XMIT_TEST_CMD)) {
1633                 writew(htons(0x11), ti->asb + FRAME_LENGTH_OFST);
1634                 writeb(0x0e, ti->asb + HEADER_LENGTH_OFST);
1635                 SET_PAGE(dhb_page);
1636                 writeb(AC, dhb);
1637                 writeb(LLC_FRAME, dhb + 1);
1638                 for (i = 0; i < TR_ALEN; i++)
1639                         writeb((int) 0x0FF, dhb + i + 2);
1640                 for (i = 0; i < TR_ALEN; i++)
1641                         writeb(0, dhb + i + TR_ALEN + 2);
1642                 writeb(RESP_IN_ASB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
1643                 return;
1644         }
1645         /*
1646          *    the token ring packet is copied from sk_buff to the adapter
1647          *    buffer identified in the command data received with the interrupt.
1648          */
1649         writeb(hdr_len, ti->asb + HEADER_LENGTH_OFST);
1650         writew(htons(ti->current_skb->len), ti->asb + FRAME_LENGTH_OFST);
1651         src_len=ti->current_skb->len;
1652         src_offset=0;
1653         dhb=dhb_base;
1654         while(1) {
1655                 if (ti->page_mask) {
1656                         dhb_page=(dhb >> 8) & ti->page_mask;
1657                         dhb=dhb & ~(ti->page_mask << 8);
1658                         dhb_len=0x4000-dhb; /* remaining size of this page */
1659                 }
1660                 dhb+=ti->sram_virt;
1661                 SET_PAGE(dhb_page);
1662                 if (src_len > dhb_len) {
1663                         memcpy_toio(dhb,&ti->current_skb->data[src_offset],
1664                                         dhb_len);
1665                         src_len -= dhb_len;
1666                         src_offset += dhb_len;
1667                         dhb_base+=dhb_len;
1668                         dhb=dhb_base;
1669                         continue;
1670                 }
1671                 memcpy_toio(dhb, &ti->current_skb->data[src_offset], src_len);
1672                 break;
1673         }
1674         writeb(RESP_IN_ASB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
1675         ti->tr_stats.tx_bytes += ti->current_skb->len;
1676         dev_kfree_skb_irq(ti->current_skb);
1677         ti->current_skb = NULL;
1678         netif_wake_queue(dev);
1679         if (ti->readlog_pending)
1680                 ibmtr_readlog(dev);
1681 }                               /*tr_tx */
1682
1683 /*****************************************************************************/
1684
1685
1686 #define RECEIVE_BUFFER_OFST     6
1687 #define LAN_HDR_LENGTH_OFST     8
1688 #define DLC_HDR_LENGTH_OFST     9
1689
1690 #define DSAP_OFST               0
1691 #define SSAP_OFST               1
1692 #define LLC_OFST                2
1693 #define PROTID_OFST             3
1694 #define ETHERTYPE_OFST          6
1695
1696 static void tr_rx(struct net_device *dev)
1697 {
1698         struct tok_info *ti = (struct tok_info *) dev->priv;
1699         __u32 rbuffer, rbufdata;
1700         __u8 rbuffer_page = 0;
1701         __u32 llc;
1702         unsigned char *data;
1703         unsigned int rbuffer_len, lan_hdr_len, hdr_len, ip_len, length;
1704         unsigned char dlc_hdr_len;
1705         struct sk_buff *skb;
1706         unsigned int skb_size = 0;
1707         int IPv4_p = 0;
1708         unsigned int chksum = 0;
1709         struct iphdr *iph;
1710         struct arb_rec_req rarb;
1711
1712         SET_PAGE(ti->arb_page);
1713         memcpy_fromio(&rarb, ti->arb, sizeof(rarb));
1714         rbuffer = ntohs(rarb.rec_buf_addr) ;
1715         if (ti->page_mask) {
1716                 rbuffer_page = (rbuffer >> 8) & ti->page_mask;
1717                 rbuffer &= ~(ti->page_mask << 8);
1718         }
1719         rbuffer += ti->sram_virt;
1720
1721         SET_PAGE(ti->asb_page);
1722
1723         if (readb(ti->asb + RETCODE_OFST) !=0xFF) DPRINTK("ASB not free !!!\n");
1724
1725         writeb(REC_DATA, ti->asb + COMMAND_OFST);
1726         writew(rarb.station_id, ti->asb + STATION_ID_OFST);
1727         writew(rarb.rec_buf_addr, ti->asb + RECEIVE_BUFFER_OFST);
1728
1729         lan_hdr_len = rarb.lan_hdr_len;
1730         if (lan_hdr_len > sizeof(struct trh_hdr)) {
1731                 DPRINTK("Linux cannot handle greater than 18 bytes RIF\n");
1732                 return;
1733         }                       /*BMS I added this above just to be very safe */
1734         dlc_hdr_len = readb(ti->arb + DLC_HDR_LENGTH_OFST);
1735         hdr_len = lan_hdr_len + sizeof(struct trllc) + sizeof(struct iphdr);
1736
1737         SET_PAGE(rbuffer_page);
1738         llc = (rbuffer + offsetof(struct rec_buf, data) + lan_hdr_len);
1739
1740 #if TR_VERBOSE
1741         DPRINTK("offsetof data: %02X lan_hdr_len: %02X\n",
1742         (__u32) offsetof(struct rec_buf, data), (unsigned int) lan_hdr_len);
1743         DPRINTK("llc: %08X rec_buf_addr: %04X dev->mem_start: %lX\n",
1744                 llc, ntohs(rarb.rec_buf_addr), dev->mem_start);
1745         DPRINTK("dsap: %02X, ssap: %02X, llc: %02X, protid: %02X%02X%02X, "
1746                 "ethertype: %04X\n",
1747                 (int) readb(llc + DSAP_OFST), (int) readb(llc + SSAP_OFST),
1748                 (int) readb(llc + LLC_OFST), (int) readb(llc + PROTID_OFST),
1749                 (int) readb(llc+PROTID_OFST+1),(int)readb(llc+PROTID_OFST + 2),
1750                 (int) ntohs(readw(llc + ETHERTYPE_OFST)));
1751 #endif
1752         if (readb(llc + offsetof(struct trllc, llc)) != UI_CMD) {
1753                 SET_PAGE(ti->asb_page);
1754                 writeb(DATA_LOST, ti->asb + RETCODE_OFST);
1755                 ti->tr_stats.rx_dropped++;
1756                 writeb(RESP_IN_ASB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
1757                 return;
1758         }
1759         length = ntohs(rarb.frame_len);
1760         if (readb(llc + DSAP_OFST) == EXTENDED_SAP &&
1761            readb(llc + SSAP_OFST) == EXTENDED_SAP &&
1762                 length >= hdr_len)      IPv4_p = 1;
1763 #if TR_VERBOSE
1764 #define SADDR_OFST      8
1765 #define DADDR_OFST      2
1766
1767         if (!IPv4_p) {
1768
1769                 __u32 trhhdr;
1770
1771                 trhhdr = (rbuffer + offsetof(struct rec_buf, data));
1772
1773                 DPRINTK("Probably non-IP frame received.\n");
1774                 DPRINTK("ssap: %02X dsap: %02X "
1775                         "saddr: %02X:%02X:%02X:%02X:%02X:%02X "
1776                         "daddr: %02X:%02X:%02X:%02X:%02X:%02X\n",
1777                         readb(llc + SSAP_OFST), readb(llc + DSAP_OFST),
1778                         readb(trhhdr+SADDR_OFST), readb(trhhdr+ SADDR_OFST+1),
1779                         readb(trhhdr+SADDR_OFST+2), readb(trhhdr+SADDR_OFST+3),
1780                         readb(trhhdr+SADDR_OFST+4), readb(trhhdr+SADDR_OFST+5),
1781                         readb(trhhdr+DADDR_OFST), readb(trhhdr+DADDR_OFST + 1),
1782                         readb(trhhdr+DADDR_OFST+2), readb(trhhdr+DADDR_OFST+3),
1783                         readb(trhhdr+DADDR_OFST+4), readb(trhhdr+DADDR_OFST+5));
1784         }
1785 #endif
1786
1787         /*BMS handle the case she comes in with few hops but leaves with many */
1788         skb_size=length-lan_hdr_len+sizeof(struct trh_hdr)+sizeof(struct trllc);
1789
1790         if (!(skb = dev_alloc_skb(skb_size))) {
1791                 DPRINTK("out of memory. frame dropped.\n");
1792                 ti->tr_stats.rx_dropped++;
1793                 SET_PAGE(ti->asb_page);
1794                 writeb(DATA_LOST, ti->asb + offsetof(struct asb_rec, ret_code));
1795                 writeb(RESP_IN_ASB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
1796                 return;
1797         }
1798         /*BMS again, if she comes in with few but leaves with many */
1799         skb_reserve(skb, sizeof(struct trh_hdr) - lan_hdr_len);
1800         skb_put(skb, length);
1801         skb->dev = dev;
1802         data = skb->data;
1803         rbuffer_len = ntohs(readw(rbuffer + offsetof(struct rec_buf, buf_len)));
1804         rbufdata = rbuffer + offsetof(struct rec_buf, data);
1805
1806         if (IPv4_p) {
1807                 /* Copy the headers without checksumming */
1808                 memcpy_fromio(data, rbufdata, hdr_len);
1809
1810                 /* Watch for padded packets and bogons */
1811                 iph= (struct iphdr *)(data+ lan_hdr_len + sizeof(struct trllc));
1812                 ip_len = ntohs(iph->tot_len) - sizeof(struct iphdr);
1813                 length -= hdr_len;
1814                 if ((ip_len <= length) && (ip_len > 7))
1815                         length = ip_len;
1816                 data += hdr_len;
1817                 rbuffer_len -= hdr_len;
1818                 rbufdata += hdr_len;
1819         }
1820         /* Copy the payload... */
1821 #define BUFFER_POINTER_OFST     2
1822 #define BUFFER_LENGTH_OFST      6
1823         for (;;) {
1824                 if (ibmtr_debug_trace&TRC_INITV && length < rbuffer_len)
1825                         DPRINTK("CURIOUS, length=%d < rbuffer_len=%d\n",
1826                                                 length,rbuffer_len);
1827                 if (IPv4_p)
1828                         chksum=csum_partial_copy_nocheck((void*)rbufdata,
1829                             data,length<rbuffer_len?length:rbuffer_len,chksum);
1830                 else
1831                         memcpy_fromio(data, rbufdata, rbuffer_len);
1832                 rbuffer = ntohs(readw(rbuffer+BUFFER_POINTER_OFST)) ;
1833                 if (!rbuffer)
1834                         break;
1835                 rbuffer -= 2;
1836                 length -= rbuffer_len;
1837                 data += rbuffer_len;
1838                 if (ti->page_mask) {
1839                         rbuffer_page = (rbuffer >> 8) & ti->page_mask;
1840                         rbuffer &= ~(ti->page_mask << 8);
1841                 }
1842                 rbuffer += ti->sram_virt;
1843                 SET_PAGE(rbuffer_page);
1844                 rbuffer_len = ntohs(readw(rbuffer + BUFFER_LENGTH_OFST));
1845                 rbufdata = rbuffer + offsetof(struct rec_buf, data);
1846         }
1847
1848         SET_PAGE(ti->asb_page);
1849         writeb(0, ti->asb + offsetof(struct asb_rec, ret_code));
1850
1851         writeb(RESP_IN_ASB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
1852
1853         ti->tr_stats.rx_bytes += skb->len;
1854         ti->tr_stats.rx_packets++;
1855
1856         skb->protocol = tr_type_trans(skb, dev);
1857         if (IPv4_p) {
1858                 skb->csum = chksum;
1859                 skb->ip_summed = 1;
1860         }
1861         netif_rx(skb);
1862         dev->last_rx = jiffies;
1863 }                               /*tr_rx */
1864
1865 /*****************************************************************************/
1866
1867 void ibmtr_reset_timer(struct timer_list *tmr, struct net_device *dev)
1868 {
1869         tmr->expires = jiffies + TR_RETRY_INTERVAL;
1870         tmr->data = (unsigned long) dev;
1871         tmr->function = tok_rerun;
1872         init_timer(tmr);
1873         add_timer(tmr);
1874 }
1875
1876 /*****************************************************************************/
1877
1878 void tok_rerun(unsigned long dev_addr){
1879
1880         struct net_device *dev = (struct net_device *)dev_addr;
1881         struct tok_info *ti = (struct tok_info *) dev->priv;
1882
1883         if ( ti->open_action == RESTART){
1884                 ti->do_tok_int = FIRST_INT;
1885                 outb(0, dev->base_addr + ADAPTRESETREL);
1886 #ifdef ENABLE_PAGING
1887                 if (ti->page_mask)
1888                         writeb(SRPR_ENABLE_PAGING,
1889                                 ti->mmio + ACA_OFFSET + ACA_RW + SRPR_EVEN);
1890 #endif
1891
1892                 writeb(INT_ENABLE, ti->mmio + ACA_OFFSET + ACA_SET + ISRP_EVEN);
1893         } else
1894                 tok_open_adapter(dev_addr);
1895 }
1896
1897 /*****************************************************************************/
1898
1899 void ibmtr_readlog(struct net_device *dev)
1900 {
1901         struct tok_info *ti;
1902
1903         ti = (struct tok_info *) dev->priv;
1904
1905         ti->readlog_pending = 0;
1906         SET_PAGE(ti->srb_page);
1907         writeb(DIR_READ_LOG, ti->srb);
1908         writeb(INT_ENABLE, ti->mmio + ACA_OFFSET + ACA_SET + ISRP_EVEN);
1909         writeb(CMD_IN_SRB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
1910
1911         netif_stop_queue(dev);
1912
1913 }
1914
1915 /*****************************************************************************/
1916
1917 /* tok_get_stats():  Basically a scaffold routine which will return
1918    the address of the tr_statistics structure associated with
1919    this device -- the tr.... structure is an ethnet look-alike
1920    so at least for this iteration may suffice.   */
1921
1922 static struct net_device_stats *tok_get_stats(struct net_device *dev)
1923 {
1924
1925         struct tok_info *toki;
1926         toki = (struct tok_info *) dev->priv;
1927         return (struct net_device_stats *) &toki->tr_stats;
1928 }
1929
1930 /*****************************************************************************/
1931
1932 int ibmtr_change_mtu(struct net_device *dev, int mtu)
1933 {
1934         struct tok_info *ti = (struct tok_info *) dev->priv;
1935
1936         if (ti->ring_speed == 16 && mtu > ti->maxmtu16)
1937                 return -EINVAL;
1938         if (ti->ring_speed == 4 && mtu > ti->maxmtu4)
1939                 return -EINVAL;
1940         dev->mtu = mtu;
1941         return 0;
1942 }
1943
1944 /*****************************************************************************/
1945 #ifdef MODULE
1946
1947 /* 3COM 3C619C supports 8 interrupts, 32 I/O ports */
1948 static struct net_device *dev_ibmtr[IBMTR_MAX_ADAPTERS];
1949 static int io[IBMTR_MAX_ADAPTERS] = { 0xa20, 0xa24 };
1950 static int irq[IBMTR_MAX_ADAPTERS];
1951 static int mem[IBMTR_MAX_ADAPTERS];
1952
1953 MODULE_LICENSE("GPL");
1954
1955 MODULE_PARM(io, "1-" __MODULE_STRING(IBMTR_MAX_ADAPTERS) "i");
1956 MODULE_PARM(irq, "1-" __MODULE_STRING(IBMTR_MAX_ADAPTERS) "i");
1957 MODULE_PARM(mem, "1-" __MODULE_STRING(IBMTR_MAX_ADAPTERS) "i");
1958
1959 static int __init ibmtr_init(void)
1960 {
1961         int i;
1962         int count=0;
1963
1964         find_turbo_adapters(io);
1965
1966         for (i = 0; io[i] && (i < IBMTR_MAX_ADAPTERS); i++) {
1967                 struct net_device *dev;
1968                 irq[i] = 0;
1969                 mem[i] = 0;
1970                 dev = alloc_trdev(sizeof(struct tok_info));
1971                 if (dev == NULL) { 
1972                         if (i == 0)
1973                                 return -ENOMEM;
1974                         break;
1975                 }
1976                 dev->base_addr = io[i];
1977                 dev->irq = irq[i];
1978                 dev->mem_start = mem[i];
1979
1980                 if (ibmtr_probe_card(dev)) {
1981                         free_netdev(dev);
1982                         continue;
1983                 }
1984                 dev_ibmtr[i] = dev;
1985                 count++;
1986         }
1987         if (count) return 0;
1988         printk("ibmtr: register_netdev() returned non-zero.\n");
1989         return -EIO;
1990 }
1991 module_init(ibmtr_init);
1992
1993 static void __exit ibmtr_cleanup(void)
1994 {
1995         int i;
1996
1997         for (i = 0; i < IBMTR_MAX_ADAPTERS; i++){
1998                 if (!dev_ibmtr[i])
1999                         continue;
2000                 unregister_netdev(dev_ibmtr[i]);
2001                 ibmtr_cleanup_card(dev_ibmtr[i]);
2002                 free_netdev(dev_ibmtr[i]);
2003         }
2004 }
2005 module_exit(ibmtr_cleanup);
2006 #endif