Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[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[] __devinitdata =
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 static 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 static 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 static 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 static 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 static 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 static void     ibmtr_reset_timer(struct timer_list*tmr,struct net_device *dev);
205 static void     tok_rerun(unsigned long dev_addr);
206 static void     ibmtr_readlog(struct net_device *dev);
207 static struct   net_device_stats *tok_get_stats(struct net_device *dev);
208 static 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 __devinitdata = 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 __iomem *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 {
244         int ram_addr;
245         int index=0;
246         void __iomem *chanid;
247         int found_turbo=0;
248         unsigned char *tchanid, ctemp;
249         int i, j;
250         unsigned long jif;
251         void __iomem *ram_mapped ;   
252
253         if (turbo_searched == 1) return;
254         turbo_searched=1;
255         for (ram_addr=0xC0000; ram_addr < 0xE0000; ram_addr+=0x2000) {
256
257                 __u32 intf_tbl=0;
258
259                 found_turbo=1;
260                 ram_mapped = ioremap((u32)ram_addr,0x1fff) ; 
261                 if (ram_mapped==NULL) 
262                         continue ; 
263                 chanid=(CHANNEL_ID + ram_mapped);
264                 tchanid=pcchannelid;
265                 ctemp=readb(chanid) & 0x0f;
266                 if (ctemp != *tchanid) continue;
267                 for (i=2,j=1; i<=46; i=i+2,j++) {
268                         if ((readb(chanid+i) & 0x0f) != tchanid[j]){
269                                 found_turbo=0;
270                                 break;
271                         }
272                 }
273                 if (!found_turbo) continue;
274
275                 writeb(0x90, ram_mapped+0x1E01);
276                 for(i=2; i<0x0f; i++) {
277                         writeb(0x00, ram_mapped+0x1E01+i);
278                 }
279                 writeb(0x00, ram_mapped+0x1E01);
280                 for(jif=jiffies+TR_BUSY_INTERVAL; time_before_eq(jiffies,jif););
281                 intf_tbl=ntohs(readw(ram_mapped+ACA_OFFSET+ACA_RW+WRBR_EVEN));
282                 if (intf_tbl) {
283 #if IBMTR_DEBUG_MESSAGES
284                         printk("ibmtr::find_turbo_adapters, Turbo found at "
285                                 "ram_addr %x\n",ram_addr);
286                         printk("ibmtr::find_turbo_adapters, interface_table ");
287                         for(i=0; i<6; i++) {
288                                 printk("%x:",readb(ram_addr+intf_tbl+i));
289                         }
290                         printk("\n");
291 #endif
292                         turbo_io[index]=ntohs(readw(ram_mapped+intf_tbl+4));
293                         turbo_irq[index]=readb(ram_mapped+intf_tbl+3);
294                         outb(0, turbo_io[index] + ADAPTRESET);
295                         for(jif=jiffies+TR_RST_TIME;time_before_eq(jiffies,jif););
296                         outb(0, turbo_io[index] + ADAPTRESETREL);
297                         index++;
298                         continue;
299                 }
300 #if IBMTR_DEBUG_MESSAGES 
301                 printk("ibmtr::find_turbo_adapters, ibmtr card found at"
302                         " %x but not a Turbo model\n",ram_addr);
303 #endif
304         iounmap(ram_mapped) ;   
305         } /* for */
306         for(i=0; i<IBMTR_MAX_ADAPTERS; i++) {
307                 if(!turbo_io[i]) break;
308                 for (j=0; j<IBMTR_MAX_ADAPTERS; j++) {
309                         if ( iolist[j] && iolist[j] != turbo_io[i]) continue;
310                         iolist[j]=turbo_io[i];
311                         break;
312                 }
313         }
314 }
315
316 static void ibmtr_cleanup_card(struct net_device *dev)
317 {
318         if (dev->base_addr) {
319                 outb(0,dev->base_addr+ADAPTRESET);
320                 
321                 schedule_timeout_uninterruptible(TR_RST_TIME); /* wait 50ms */
322
323                 outb(0,dev->base_addr+ADAPTRESETREL);
324         }
325
326 #ifndef PCMCIA
327         free_irq(dev->irq, dev);
328         release_region(dev->base_addr, IBMTR_IO_EXTENT);
329
330         { 
331                 struct tok_info *ti = (struct tok_info *) dev->priv;
332                 iounmap(ti->mmio);
333                 iounmap(ti->sram_virt);
334         }
335 #endif          
336 }
337
338 int ibmtr_probe_card(struct net_device *dev)
339 {
340         int err = ibmtr_probe(dev);
341         if (!err) {
342                 err = register_netdev(dev);
343                 if (err)
344                         ibmtr_cleanup_card(dev);
345         }
346         return err;
347 }
348
349 /****************************************************************************
350  *      ibmtr_probe():  Routine specified in the network device structure
351  *      to probe for an IBM Token Ring Adapter.  Routine outline:
352  *      I.    Interrogate hardware to determine if an adapter exists
353  *            and what the speeds and feeds are
354  *      II.   Setup data structures to control execution based upon
355  *            adapter characteristics.
356  *
357  *      We expect ibmtr_probe to be called once for each device entry
358  *      which references it.
359  ****************************************************************************/
360
361 static int ibmtr_probe(struct net_device *dev)
362 {
363         int i;
364         int base_addr = dev->base_addr;
365
366         if (base_addr && base_addr <= 0x1ff) /* Don't probe at all. */
367                 return -ENXIO;
368         if (base_addr > 0x1ff) { /* Check a single specified location.  */
369                 if (!ibmtr_probe1(dev, base_addr)) return 0;
370                 return -ENODEV;
371         }
372         find_turbo_adapters(ibmtr_portlist);
373         for (i = 0; ibmtr_portlist[i]; i++) {
374                 int ioaddr = ibmtr_portlist[i];
375
376                 if (!ibmtr_probe1(dev, ioaddr)) return 0;
377         }
378         return -ENODEV;
379 }
380
381 /*****************************************************************************/
382
383 static int __devinit ibmtr_probe1(struct net_device *dev, int PIOaddr)
384 {
385
386         unsigned char segment, intr=0, irq=0, i, j, cardpresent=NOTOK, temp=0;
387         void __iomem * t_mmio = NULL;
388         struct tok_info *ti = dev->priv;
389         void __iomem *cd_chanid;
390         unsigned char *tchanid, ctemp;
391 #ifndef PCMCIA
392         unsigned char t_irq=0;
393         unsigned long timeout;
394         static int version_printed;
395 #endif
396
397         /*    Query the adapter PIO base port which will return
398          *    indication of where MMIO was placed. We also have a
399          *    coded interrupt number.
400          */
401         segment = inb(PIOaddr);
402         if (segment < 0x40 || segment > 0xe0) {
403                 /* Out of range values so we'll assume non-existent IO device
404                  * but this is not necessarily a problem, esp if a turbo
405                  * adapter is being used.  */
406 #if IBMTR_DEBUG_MESSAGES
407                 DPRINTK("ibmtr_probe1(): unhappy that inb(0x%X) == 0x%X, "
408                         "Hardware Problem?\n",PIOaddr,segment);
409 #endif
410                 return -ENODEV;
411         }
412         /*
413          *    Compute the linear base address of the MMIO area
414          *    as LINUX doesn't care about segments
415          */
416         t_mmio = ioremap(((__u32) (segment & 0xfc) << 11) + 0x80000,2048);
417         if (!t_mmio) { 
418                 DPRINTK("Cannot remap mmiobase memory area") ; 
419                 return -ENODEV ; 
420         } 
421         intr = segment & 0x03;  /* low bits is coded interrupt # */
422         if (ibmtr_debug_trace & TRC_INIT)
423                 DPRINTK("PIOaddr: %4hx seg/intr: %2x mmio base: %p intr: %d\n"
424                                 , PIOaddr, (int) segment, t_mmio, (int) intr);
425
426         /*
427          *    Now we will compare expected 'channelid' strings with
428          *    what we is there to learn of ISA/MCA or not TR card
429          */
430 #ifdef PCMCIA
431         iounmap(t_mmio);
432         t_mmio = ti->mmio;      /*BMS to get virtual address */
433         irq = ti->irq;          /*BMS to display the irq!   */
434 #endif
435         cd_chanid = (CHANNEL_ID + t_mmio);      /* for efficiency */
436         tchanid = pcchannelid;
437         cardpresent = TR_ISA;   /* try ISA */
438
439         /*    Suboptimize knowing first byte different */
440         ctemp = readb(cd_chanid) & 0x0f;
441         if (ctemp != *tchanid) {        /* NOT ISA card, try MCA */
442                 tchanid = mcchannelid;
443                 cardpresent = TR_MCA;
444                 if (ctemp != *tchanid)  /* Neither ISA nor MCA */
445                         cardpresent = NOTOK;
446         }
447         if (cardpresent != NOTOK) {
448                 /*       Know presumed type, try rest of ID */
449                 for (i = 2, j = 1; i <= 46; i = i + 2, j++) {
450                         if( (readb(cd_chanid+i)&0x0f) == tchanid[j]) continue;
451                         /* match failed, not TR card */
452                         cardpresent = NOTOK;
453                         break;
454                 }
455         }
456         /* 
457          *    If we have an ISA board check for the ISA P&P version,
458          *    as it has different IRQ settings 
459          */
460         if (cardpresent == TR_ISA && (readb(AIPFID + t_mmio) == 0x0e))
461                 cardpresent = TR_ISAPNP;
462         if (cardpresent == NOTOK) {     /* "channel_id" did not match, report */
463                 if (!(ibmtr_debug_trace & TRC_INIT)) {
464 #ifndef PCMCIA
465                         iounmap(t_mmio);
466 #endif
467                         return -ENODEV;
468                 }
469                 DPRINTK( "Channel ID string not found for PIOaddr: %4hx\n",
470                                                                 PIOaddr);
471                 DPRINTK("Expected for ISA: ");
472                 PrtChanID(pcchannelid, 1);
473                 DPRINTK("           found: ");
474 /* BMS Note that this can be misleading, when hardware is flaky, because you
475    are reading it a second time here. So with my flaky hardware, I'll see my-
476    self in this block, with the HW ID matching the ISA ID exactly! */
477                 HWPrtChanID(cd_chanid, 2);
478                 DPRINTK("Expected for MCA: ");
479                 PrtChanID(mcchannelid, 1);
480         }
481         /* Now, setup some of the pl0 buffers for this driver.. */
482         /* If called from PCMCIA, it is already set up, so no need to 
483            waste the memory, just use the existing structure */
484 #ifndef PCMCIA
485         ti->mmio = t_mmio;
486         for (i = 0; i < IBMTR_MAX_ADAPTERS; i++) {
487                 if (turbo_io[i] != PIOaddr)
488                         continue;
489 #if IBMTR_DEBUG_MESSAGES 
490                 printk("ibmtr::tr_probe1, setting PIOaddr %x to Turbo\n",
491                        PIOaddr);
492 #endif
493                 ti->turbo = 1;
494                 t_irq = turbo_irq[i];
495         }
496 #endif /* !PCMCIA */
497         ti->readlog_pending = 0;
498         init_waitqueue_head(&ti->wait_for_reset);
499
500         /* if PCMCIA, the card can be recognized as either TR_ISA or TR_ISAPNP
501          * depending which card is inserted.    */
502         
503 #ifndef PCMCIA
504         switch (cardpresent) {
505         case TR_ISA:
506                 if (intr == 0) irq = 9; /* irq2 really is irq9 */
507                 if (intr == 1) irq = 3;
508                 if (intr == 2) irq = 6;
509                 if (intr == 3) irq = 7;
510                 ti->adapter_int_enable = PIOaddr + ADAPTINTREL;
511                 break;
512         case TR_MCA:
513                 if (intr == 0) irq = 9;
514                 if (intr == 1) irq = 3;
515                 if (intr == 2) irq = 10;
516                 if (intr == 3) irq = 11;
517                 ti->global_int_enable = 0;
518                 ti->adapter_int_enable = 0;
519                 ti->sram_phys=(__u32)(inb(PIOaddr+ADAPTRESETREL) & 0xfe) << 12;
520                 break;
521         case TR_ISAPNP:
522                 if (!t_irq) {
523                         if (intr == 0) irq = 9;
524                         if (intr == 1) irq = 3;
525                         if (intr == 2) irq = 10;
526                         if (intr == 3) irq = 11;
527                 } else
528                         irq=t_irq;
529                 timeout = jiffies + TR_SPIN_INTERVAL;
530                 while (!readb(ti->mmio + ACA_OFFSET + ACA_RW + RRR_EVEN)){
531                         if (!time_after(jiffies, timeout)) continue;
532                         DPRINTK( "Hardware timeout during initialization.\n");
533                         iounmap(t_mmio);
534                         return -ENODEV;
535                 }
536                 ti->sram_phys =
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_phys=0x%x", ti->sram_phys);
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                         return -ENODEV;
648                         break;
649                 } /*end switch shared_ram_paging */
650
651                 if (ibmtr_debug_trace & TRC_INIT)
652                         DPRINTK("Shared RAM paging code: %02X, "
653                                 "mapped RAM size: %dK, shared RAM size: %dK, "
654                                 "page mask: %02X\n:",
655                                 ti->shared_ram_paging, ti->mapped_ram_size / 2,
656                                 ti->avail_shared_ram / 2, ti->page_mask);
657 #endif  /*ENABLE_PAGING */
658         }
659
660 #ifndef PCMCIA
661         /* finish figuring the shared RAM address */
662         if (cardpresent == TR_ISA) {
663                 static __u32 ram_bndry_mask[] =
664                         { 0xffffe000, 0xffffc000, 0xffff8000, 0xffff0000 };
665                 __u32 new_base, rrr_32, chk_base, rbm;
666
667                 rrr_32=readb(ti->mmio+ACA_OFFSET+ACA_RW+RRR_ODD) >> 2 & 0x03;
668                 rbm = ram_bndry_mask[rrr_32];
669                 new_base = (ibmtr_mem_base + (~rbm)) & rbm;/* up to boundary */
670                 chk_base = new_base + (ti->mapped_ram_size << 9);
671                 if (chk_base > (ibmtr_mem_base + IBMTR_SHARED_RAM_SIZE)) {
672                         DPRINTK("Shared RAM for this adapter (%05x) exceeds "
673                         "driver limit (%05x), adapter not started.\n",
674                         chk_base, ibmtr_mem_base + IBMTR_SHARED_RAM_SIZE);
675                         iounmap(t_mmio);
676                         return -ENODEV;
677                 } else { /* seems cool, record what we have figured out */
678                         ti->sram_base = new_base >> 12;
679                         ibmtr_mem_base = chk_base;
680                 }
681         }
682         else  ti->sram_base = ti->sram_phys >> 12;
683
684         /* The PCMCIA has already got the interrupt line and the io port, 
685            so no chance of anybody else getting it - MLP */
686         if (request_irq(dev->irq = irq, &tok_interrupt, 0, "ibmtr", dev) != 0) {
687                 DPRINTK("Could not grab irq %d.  Halting Token Ring driver.\n",
688                                         irq);
689                 iounmap(t_mmio);
690                 return -ENODEV;
691         }
692         /*?? Now, allocate some of the PIO PORTs for this driver.. */
693         /* record PIOaddr range as busy */
694         if (!request_region(PIOaddr, IBMTR_IO_EXTENT, "ibmtr")) {
695                 DPRINTK("Could not grab PIO range. Halting driver.\n");
696                 free_irq(dev->irq, dev);
697                 iounmap(t_mmio);
698                 return -EBUSY;
699         }
700
701         if (!version_printed++) {
702                 printk(version);
703         }
704 #endif /* !PCMCIA */
705         DPRINTK("%s %s found\n",
706                 channel_def[cardpresent - 1], adapter_def(ti->adapter_type));
707         DPRINTK("using irq %d, PIOaddr %hx, %dK shared RAM.\n",
708                         irq, PIOaddr, ti->mapped_ram_size / 2);
709         DPRINTK("Hardware address : %02X:%02X:%02X:%02X:%02X:%02X\n",
710                 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
711                 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
712         if (ti->page_mask)
713                 DPRINTK("Shared RAM paging enabled. "
714                         "Page size: %uK Shared Ram size %dK\n",
715                         ((ti->page_mask^0xff)+1) >>2, ti->avail_shared_ram / 2);
716         else
717                 DPRINTK("Shared RAM paging disabled. ti->page_mask %x\n",
718                                                                 ti->page_mask);
719
720         /* Calculate the maximum DHB we can use */
721         /* two cases where avail_shared_ram doesn't equal mapped_ram_size:
722             1. avail_shared_ram is 127 but mapped_ram_size is 128 (typical)
723             2. user has configured adapter for less than avail_shared_ram
724                but is not using paging (she should use paging, I believe)
725         */
726         if (!ti->page_mask) {
727                 ti->avail_shared_ram=
728                                 min(ti->mapped_ram_size,ti->avail_shared_ram);
729         }
730
731         switch (ti->avail_shared_ram) {
732         case 16:                /* 8KB shared RAM */
733                 ti->dhb_size4mb = min(ti->dhb_size4mb, (unsigned short)2048);
734                 ti->rbuf_len4 = 1032;
735                 ti->rbuf_cnt4=2;
736                 ti->dhb_size16mb = min(ti->dhb_size16mb, (unsigned short)2048);
737                 ti->rbuf_len16 = 1032;
738                 ti->rbuf_cnt16=2;
739                 break;
740         case 32:                /* 16KB shared RAM */
741                 ti->dhb_size4mb = min(ti->dhb_size4mb, (unsigned short)4464);
742                 ti->rbuf_len4 = 1032;
743                 ti->rbuf_cnt4=4;
744                 ti->dhb_size16mb = min(ti->dhb_size16mb, (unsigned short)4096);
745                 ti->rbuf_len16 = 1032;  /*1024 usable */
746                 ti->rbuf_cnt16=4;
747                 break;
748         case 64:                /* 32KB shared RAM */
749                 ti->dhb_size4mb = min(ti->dhb_size4mb, (unsigned short)4464);
750                 ti->rbuf_len4 = 1032;
751                 ti->rbuf_cnt4=6;
752                 ti->dhb_size16mb = min(ti->dhb_size16mb, (unsigned short)10240);
753                 ti->rbuf_len16 = 1032;
754                 ti->rbuf_cnt16=6;
755                 break;
756         case 127:               /* 63.5KB shared RAM */
757                 ti->dhb_size4mb = min(ti->dhb_size4mb, (unsigned short)4464);
758                 ti->rbuf_len4 = 1032;
759                 ti->rbuf_cnt4=6;
760                 ti->dhb_size16mb = min(ti->dhb_size16mb, (unsigned short)16384);
761                 ti->rbuf_len16 = 1032;
762                 ti->rbuf_cnt16=16;
763                 break;
764         case 128:               /* 64KB   shared RAM */
765                 ti->dhb_size4mb = min(ti->dhb_size4mb, (unsigned short)4464);
766                 ti->rbuf_len4 = 1032;
767                 ti->rbuf_cnt4=6;
768                 ti->dhb_size16mb = min(ti->dhb_size16mb, (unsigned short)17960);
769                 ti->rbuf_len16 = 1032;
770                 ti->rbuf_cnt16=16;
771                 break;
772         default:
773                 ti->dhb_size4mb = 2048;
774                 ti->rbuf_len4 = 1032;
775                 ti->rbuf_cnt4=2;
776                 ti->dhb_size16mb = 2048;
777                 ti->rbuf_len16 = 1032;
778                 ti->rbuf_cnt16=2;
779                 break;
780         }
781         /* this formula is not smart enough for the paging case
782         ti->rbuf_cnt<x> = (ti->avail_shared_ram * BLOCKSZ - ADAPT_PRIVATE -
783                         ARBLENGTH - SSBLENGTH - DLC_MAX_SAP * SAPLENGTH -
784                         DLC_MAX_STA * STALENGTH - ti->dhb_size<x>mb * NUM_DHB -
785                         SRBLENGTH - ASBLENGTH) / ti->rbuf_len<x>;
786         */
787         ti->maxmtu16 = (ti->rbuf_len16 - 8) * ti->rbuf_cnt16  - TR_HLEN;
788         ti->maxmtu4 = (ti->rbuf_len4 - 8) * ti->rbuf_cnt4 - TR_HLEN;
789         /*BMS assuming 18 bytes of Routing Information (usually works) */
790         DPRINTK("Maximum Receive Internet Protocol MTU 16Mbps: %d, 4Mbps: %d\n",
791                                                      ti->maxmtu16, ti->maxmtu4);
792
793         dev->base_addr = PIOaddr;       /* set the value for device */
794         dev->mem_start = ti->sram_base << 12;
795         dev->mem_end = dev->mem_start + (ti->mapped_ram_size << 9) - 1;
796         trdev_init(dev);
797         return 0;   /* Return 0 to indicate we have found a Token Ring card. */
798 }                               /*ibmtr_probe1() */
799
800 /*****************************************************************************/
801
802 /* query the adapter for the size of shared RAM  */
803 /* the function returns the RAM size in units of 512 bytes */
804
805 static unsigned char __devinit get_sram_size(struct tok_info *adapt_info)
806 {
807         unsigned char avail_sram_code;
808         static unsigned char size_code[] = { 0, 16, 32, 64, 127, 128 };
809         /* Adapter gives
810            'F' -- use RRR bits 3,2
811            'E' -- 8kb   'D' -- 16kb
812            'C' -- 32kb  'A' -- 64KB
813            'B' - 64KB less 512 bytes at top
814            (WARNING ... must zero top bytes in INIT */
815
816         avail_sram_code = 0xf - readb(adapt_info->mmio + AIPAVAILSHRAM);
817         if (avail_sram_code) return size_code[avail_sram_code];
818         else            /* for code 'F', must compute size from RRR(3,2) bits */
819                 return 1 <<
820                  ((readb(adapt_info->mmio+ACA_OFFSET+ACA_RW+RRR_ODD)>>2&3)+4);
821 }
822
823 /*****************************************************************************/
824
825 static int __devinit trdev_init(struct net_device *dev)
826 {
827         struct tok_info *ti = (struct tok_info *) dev->priv;
828
829         SET_PAGE(ti->srb_page);
830         ti->open_failure = NO    ;
831         dev->open = tok_open;
832         dev->stop = tok_close;
833         dev->hard_start_xmit = tok_send_packet;
834         dev->get_stats = tok_get_stats;
835         dev->set_multicast_list = tok_set_multicast_list;
836         dev->change_mtu = ibmtr_change_mtu;
837
838         return 0;
839 }
840
841 /*****************************************************************************/
842
843 static int tok_init_card(struct net_device *dev)
844 {
845         struct tok_info *ti;
846         short PIOaddr;
847         unsigned long i;
848         wait_queue_t __wait;
849         init_waitqueue_entry(&__wait, current);
850
851         PIOaddr = dev->base_addr;
852         ti = (struct tok_info *) dev->priv;
853         /* Special processing for first interrupt after reset */
854         ti->do_tok_int = FIRST_INT;
855         /* Reset adapter */
856         writeb(~INT_ENABLE, ti->mmio + ACA_OFFSET + ACA_RESET + ISRP_EVEN);
857         outb(0, PIOaddr + ADAPTRESET);
858
859         schedule_timeout_uninterruptible(TR_RST_TIME); /* wait 50ms */
860
861         add_wait_queue(&ti->wait_for_reset, &__wait);
862         set_current_state(TASK_UNINTERRUPTIBLE);
863         outb(0, PIOaddr + ADAPTRESETREL);
864 #ifdef ENABLE_PAGING
865         if (ti->page_mask)
866                 writeb(SRPR_ENABLE_PAGING,ti->mmio+ACA_OFFSET+ACA_RW+SRPR_EVEN);
867 #endif
868         writeb(INT_ENABLE, ti->mmio + ACA_OFFSET + ACA_SET + ISRP_EVEN);
869         #warning pci posting bug
870         i = schedule_timeout(4 * HZ);
871         current->state = TASK_RUNNING;
872         remove_wait_queue(&ti->wait_for_reset, &__wait);
873         return i? 0 : -EAGAIN;
874 }
875
876 /*****************************************************************************/
877 static int tok_open(struct net_device *dev)
878 {
879         struct tok_info *ti = (struct tok_info *) dev->priv;
880         int i;
881
882         /*the case we were left in a failure state during a previous open */
883         if (ti->open_failure == YES) {
884                 DPRINTK("Last time you were disconnected, how about now?\n");
885                 printk("You can't insert with an ICS connector half-cocked.\n");
886         }
887
888         ti->open_status  = CLOSED; /* CLOSED or OPEN      */
889         ti->sap_status   = CLOSED; /* CLOSED or OPEN      */
890         ti->open_failure =     NO; /* NO     or YES       */
891         ti->open_mode    = MANUAL; /* MANUAL or AUTOMATIC */
892
893         ti->sram_phys &= ~1; /* to reverse what we do in tok_close */
894         /* init the spinlock */
895         spin_lock_init(&ti->lock);
896         init_timer(&ti->tr_timer);
897         
898         i = tok_init_card(dev);
899         if (i) return i;
900
901         while (1){
902                 tok_open_adapter((unsigned long) dev);
903                 i= interruptible_sleep_on_timeout(&ti->wait_for_reset, 25 * HZ);
904                 /* sig catch: estimate opening adapter takes more than .5 sec*/
905                 if (i>(245*HZ)/10) break; /* fancier than if (i==25*HZ) */
906                 if (i==0) break;
907                 if (ti->open_status == OPEN && ti->sap_status==OPEN) {
908                         netif_start_queue(dev);
909                         DPRINTK("Adapter is up and running\n");
910                         return 0;
911                 }
912                 i=schedule_timeout_interruptible(TR_RETRY_INTERVAL);
913                                                         /* wait 30 seconds */
914                 if(i!=0) break; /*prob. a signal, like the i>24*HZ case above */
915         }
916         outb(0, dev->base_addr + ADAPTRESET);/* kill pending interrupts*/
917         DPRINTK("TERMINATED via signal\n");     /*BMS useful */
918         return -EAGAIN;
919 }
920
921 /*****************************************************************************/
922
923 #define COMMAND_OFST             0
924 #define OPEN_OPTIONS_OFST        8
925 #define NUM_RCV_BUF_OFST        24
926 #define RCV_BUF_LEN_OFST        26
927 #define DHB_LENGTH_OFST         28
928 #define NUM_DHB_OFST            30
929 #define DLC_MAX_SAP_OFST        32
930 #define DLC_MAX_STA_OFST        33
931
932 static void tok_open_adapter(unsigned long dev_addr)
933 {
934         struct net_device *dev = (struct net_device *) dev_addr;
935         struct tok_info *ti;
936         int i;
937
938         ti = (struct tok_info *) dev->priv;
939         SET_PAGE(ti->init_srb_page); 
940         writeb(~SRB_RESP_INT, ti->mmio + ACA_OFFSET + ACA_RESET + ISRP_ODD);
941         for (i = 0; i < sizeof(struct dir_open_adapter); i++)
942                 writeb(0, ti->init_srb + i);
943         writeb(DIR_OPEN_ADAPTER, ti->init_srb + COMMAND_OFST);
944         writew(htons(OPEN_PASS_BCON_MAC), ti->init_srb + OPEN_OPTIONS_OFST);
945         if (ti->ring_speed == 16) {
946                 writew(htons(ti->dhb_size16mb), ti->init_srb + DHB_LENGTH_OFST);
947                 writew(htons(ti->rbuf_cnt16), ti->init_srb + NUM_RCV_BUF_OFST);
948                 writew(htons(ti->rbuf_len16), ti->init_srb + RCV_BUF_LEN_OFST);
949         } else {
950                 writew(htons(ti->dhb_size4mb), ti->init_srb + DHB_LENGTH_OFST);
951                 writew(htons(ti->rbuf_cnt4), ti->init_srb + NUM_RCV_BUF_OFST);
952                 writew(htons(ti->rbuf_len4), ti->init_srb + RCV_BUF_LEN_OFST);
953         }
954         writeb(NUM_DHB,         /* always 2 */ ti->init_srb + NUM_DHB_OFST);
955         writeb(DLC_MAX_SAP, ti->init_srb + DLC_MAX_SAP_OFST);
956         writeb(DLC_MAX_STA, ti->init_srb + DLC_MAX_STA_OFST);
957         ti->srb = ti->init_srb; /* We use this one in the interrupt handler */
958         ti->srb_page = ti->init_srb_page;
959         DPRINTK("Opening adapter: Xmit bfrs: %d X %d, Rcv bfrs: %d X %d\n",
960                 readb(ti->init_srb + NUM_DHB_OFST),
961                 ntohs(readw(ti->init_srb + DHB_LENGTH_OFST)),
962                 ntohs(readw(ti->init_srb + NUM_RCV_BUF_OFST)),
963                 ntohs(readw(ti->init_srb + RCV_BUF_LEN_OFST)));
964         writeb(INT_ENABLE, ti->mmio + ACA_OFFSET + ACA_SET + ISRP_EVEN);
965         writeb(CMD_IN_SRB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
966 }
967
968 /*****************************************************************************/
969
970 static void open_sap(unsigned char type, struct net_device *dev)
971 {
972         int i;
973         struct tok_info *ti = (struct tok_info *) dev->priv;
974
975         SET_PAGE(ti->srb_page);
976         for (i = 0; i < sizeof(struct dlc_open_sap); i++)
977                 writeb(0, ti->srb + i);
978
979 #define MAX_I_FIELD_OFST        14
980 #define SAP_VALUE_OFST          16
981 #define SAP_OPTIONS_OFST        17
982 #define STATION_COUNT_OFST      18
983
984         writeb(DLC_OPEN_SAP, ti->srb + COMMAND_OFST);
985         writew(htons(MAX_I_FIELD), ti->srb + MAX_I_FIELD_OFST);
986         writeb(SAP_OPEN_IND_SAP | SAP_OPEN_PRIORITY, ti->srb+ SAP_OPTIONS_OFST);
987         writeb(SAP_OPEN_STATION_CNT, ti->srb + STATION_COUNT_OFST);
988         writeb(type, ti->srb + SAP_VALUE_OFST);
989         writeb(CMD_IN_SRB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
990 }
991
992
993 /*****************************************************************************/
994
995 static void tok_set_multicast_list(struct net_device *dev)
996 {
997         struct tok_info *ti = (struct tok_info *) dev->priv;
998         struct dev_mc_list *mclist;
999         unsigned char address[4];
1000
1001         int i;
1002
1003         /*BMS the next line is CRUCIAL or you may be sad when you */
1004         /*BMS ifconfig tr down or hot unplug a PCMCIA card ??hownowbrowncow*/
1005         if (/*BMSHELPdev->start == 0 ||*/ ti->open_status != OPEN) return;
1006         address[0] = address[1] = address[2] = address[3] = 0;
1007         mclist = dev->mc_list;
1008         for (i = 0; i < dev->mc_count; i++) {
1009                 address[0] |= mclist->dmi_addr[2];
1010                 address[1] |= mclist->dmi_addr[3];
1011                 address[2] |= mclist->dmi_addr[4];
1012                 address[3] |= mclist->dmi_addr[5];
1013                 mclist = mclist->next;
1014         }
1015         SET_PAGE(ti->srb_page);
1016         for (i = 0; i < sizeof(struct srb_set_funct_addr); i++)
1017                 writeb(0, ti->srb + i);
1018
1019 #define FUNCT_ADDRESS_OFST 6
1020
1021         writeb(DIR_SET_FUNC_ADDR, ti->srb + COMMAND_OFST);
1022         for (i = 0; i < 4; i++) 
1023                 writeb(address[i], ti->srb + FUNCT_ADDRESS_OFST + i);
1024         writeb(CMD_IN_SRB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
1025 #if TR_VERBOSE
1026         DPRINTK("Setting functional address: ");
1027         for (i=0;i<4;i++)  printk("%02X ", address[i]);
1028         printk("\n");
1029 #endif
1030 }
1031
1032 /*****************************************************************************/
1033
1034 #define STATION_ID_OFST 4
1035
1036 static int tok_send_packet(struct sk_buff *skb, struct net_device *dev)
1037 {
1038         struct tok_info *ti;
1039         unsigned long flags;
1040         ti = (struct tok_info *) dev->priv;
1041
1042         netif_stop_queue(dev);
1043
1044         /* lock against other CPUs */
1045         spin_lock_irqsave(&(ti->lock), flags);
1046
1047         /* Save skb; we'll need it when the adapter asks for the data */
1048         ti->current_skb = skb;
1049         SET_PAGE(ti->srb_page);
1050         writeb(XMIT_UI_FRAME, ti->srb + COMMAND_OFST);
1051         writew(ti->exsap_station_id, ti->srb + STATION_ID_OFST);
1052         writeb(CMD_IN_SRB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
1053         spin_unlock_irqrestore(&(ti->lock), flags);
1054         dev->trans_start = jiffies;
1055         return 0;
1056 }
1057
1058 /*****************************************************************************/
1059
1060 static int tok_close(struct net_device *dev)
1061 {
1062         struct tok_info *ti = (struct tok_info *) dev->priv;
1063
1064         /* Important for PCMCIA hot unplug, otherwise, we'll pull the card, */
1065         /* unloading the module from memory, and then if a timer pops, ouch */
1066         del_timer_sync(&ti->tr_timer);
1067         outb(0, dev->base_addr + ADAPTRESET);
1068         ti->sram_phys |= 1;
1069         ti->open_status = CLOSED;
1070
1071         netif_stop_queue(dev);
1072         DPRINTK("Adapter is closed.\n");
1073         return 0;
1074 }
1075
1076 /*****************************************************************************/
1077
1078 #define RETCODE_OFST            2
1079 #define OPEN_ERROR_CODE_OFST    6
1080 #define ASB_ADDRESS_OFST        8
1081 #define SRB_ADDRESS_OFST        10
1082 #define ARB_ADDRESS_OFST        12
1083 #define SSB_ADDRESS_OFST        14
1084
1085 static char *printphase[]= {"Lobe media test","Physical insertion",
1086               "Address verification","Roll call poll","Request Parameters"};
1087 static char *printerror[]={"Function failure","Signal loss","Reserved",
1088                 "Frequency error","Timeout","Ring failure","Ring beaconing",
1089                 "Duplicate node address",
1090                 "Parameter request-retry count exceeded","Remove received",
1091                 "IMPL force received","Duplicate modifier",
1092                 "No monitor detected","Monitor contention failed for RPL"};
1093
1094 static void __iomem *map_address(struct tok_info *ti, unsigned index, __u8 *page)
1095 {
1096         if (ti->page_mask) {
1097                 *page = (index >> 8) & ti->page_mask;
1098                 index &= ~(ti->page_mask << 8);
1099         }
1100         return ti->sram_virt + index;
1101 }
1102
1103 static void dir_open_adapter (struct net_device *dev)
1104 {
1105         struct tok_info *ti = (struct tok_info *) dev->priv;
1106         unsigned char ret_code;
1107         __u16 err;
1108
1109         ti->srb = map_address(ti,
1110                 ntohs(readw(ti->init_srb + SRB_ADDRESS_OFST)),
1111                 &ti->srb_page);
1112         ti->ssb = map_address(ti,
1113                 ntohs(readw(ti->init_srb + SSB_ADDRESS_OFST)),
1114                 &ti->ssb_page);
1115         ti->arb = map_address(ti,
1116                 ntohs(readw(ti->init_srb + ARB_ADDRESS_OFST)),
1117                 &ti->arb_page);
1118         ti->asb = map_address(ti,
1119                 ntohs(readw(ti->init_srb + ASB_ADDRESS_OFST)),
1120                 &ti->asb_page);
1121         ti->current_skb = NULL;
1122         ret_code = readb(ti->init_srb + RETCODE_OFST);
1123         err = ntohs(readw(ti->init_srb + OPEN_ERROR_CODE_OFST));
1124         if (!ret_code) {
1125                 ti->open_status = OPEN; /* TR adapter is now available */
1126                 if (ti->open_mode == AUTOMATIC) {
1127                         DPRINTK("Adapter reopened.\n");
1128                 }
1129                 writeb(~SRB_RESP_INT, ti->mmio+ACA_OFFSET+ACA_RESET+ISRP_ODD);
1130                 open_sap(EXTENDED_SAP, dev);
1131                 return;
1132         }
1133         ti->open_failure = YES;
1134         if (ret_code == 7){
1135                if (err == 0x24) {
1136                         if (!ti->auto_speedsave) {
1137                                 DPRINTK("Open failed: Adapter speed must match "
1138                                  "ring speed if Automatic Ring Speed Save is "
1139                                  "disabled.\n");
1140                                 ti->open_action = FAIL;
1141                         }else
1142                                 DPRINTK("Retrying open to adjust to "
1143                                         "ring speed, ");
1144                 } else if (err == 0x2d) {
1145                         DPRINTK("Physical Insertion: No Monitor Detected, ");
1146                         printk("retrying after %ds delay...\n",
1147                                         TR_RETRY_INTERVAL/HZ);
1148                 } else if (err == 0x11) {
1149                         DPRINTK("Lobe Media Function Failure (0x11), ");
1150                         printk(" retrying after %ds delay...\n",
1151                                         TR_RETRY_INTERVAL/HZ);
1152                 } else {
1153                         char **prphase = printphase;
1154                         char **prerror = printerror;
1155                         DPRINTK("TR Adapter misc open failure, error code = ");
1156                         printk("0x%x, Phase: %s, Error: %s\n",
1157                                 err, prphase[err/16 -1], prerror[err%16 -1]);
1158                         printk(" retrying after %ds delay...\n",
1159                                         TR_RETRY_INTERVAL/HZ);
1160                 }
1161         } else DPRINTK("open failed: ret_code = %02X..., ", ret_code);
1162         if (ti->open_action != FAIL) {
1163                 if (ti->open_mode==AUTOMATIC){
1164                         ti->open_action = REOPEN;
1165                         ibmtr_reset_timer(&(ti->tr_timer), dev);
1166                         return;
1167                 }
1168                 wake_up(&ti->wait_for_reset);
1169                 return;
1170         }
1171         DPRINTK("FAILURE, CAPUT\n");
1172 }
1173
1174 /******************************************************************************/
1175
1176 static irqreturn_t tok_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1177 {
1178         unsigned char status;
1179         /*  unsigned char status_even ; */
1180         struct tok_info *ti;
1181         struct net_device *dev;
1182 #ifdef ENABLE_PAGING
1183         unsigned char save_srpr;
1184 #endif
1185
1186         dev = dev_id;
1187 #if TR_VERBOSE
1188         DPRINTK("Int from tok_driver, dev : %p irq%d regs=%p\n", dev,irq,regs);
1189 #endif
1190         ti = (struct tok_info *) dev->priv;
1191         if (ti->sram_phys & 1)
1192                 return IRQ_NONE;         /* PCMCIA card extraction flag */
1193         spin_lock(&(ti->lock));
1194 #ifdef ENABLE_PAGING
1195         save_srpr = readb(ti->mmio + ACA_OFFSET + ACA_RW + SRPR_EVEN);
1196 #endif
1197
1198         /* Disable interrupts till processing is finished */
1199         writeb((~INT_ENABLE), ti->mmio + ACA_OFFSET + ACA_RESET + ISRP_EVEN);
1200
1201         /* Reset interrupt for ISA boards */
1202         if (ti->adapter_int_enable)
1203                 outb(0, ti->adapter_int_enable);
1204         else /* used for PCMCIA cards */
1205                 outb(0, ti->global_int_enable);
1206         if (ti->do_tok_int == FIRST_INT){
1207                 initial_tok_int(dev);
1208 #ifdef ENABLE_PAGING
1209                 writeb(save_srpr, ti->mmio + ACA_OFFSET + ACA_RW + SRPR_EVEN);
1210 #endif
1211                 spin_unlock(&(ti->lock));
1212                 return IRQ_HANDLED;
1213         }
1214         /*  Begin interrupt handler HERE inline to avoid the extra
1215             levels of logic and call depth for the original solution. */
1216         status = readb(ti->mmio + ACA_OFFSET + ACA_RW + ISRP_ODD);
1217         /*BMSstatus_even = readb (ti->mmio + ACA_OFFSET + ACA_RW + ISRP_EVEN) */
1218         /*BMSdebugprintk("tok_interrupt: ISRP_ODD = 0x%x ISRP_EVEN = 0x%x\n", */
1219         /*BMS                                       status,status_even);      */
1220
1221         if (status & ADAP_CHK_INT) {
1222                 int i;
1223                 void __iomem *check_reason;
1224                 __u8 check_reason_page = 0;
1225                 check_reason = map_address(ti,
1226                         ntohs(readw(ti->mmio+ ACA_OFFSET+ACA_RW + WWCR_EVEN)),
1227                         &check_reason_page);
1228                 SET_PAGE(check_reason_page);
1229
1230                 DPRINTK("Adapter check interrupt\n");
1231                 DPRINTK("8 reason bytes follow: ");
1232                 for (i = 0; i < 8; i++, check_reason++)
1233                         printk("%02X ", (int) readb(check_reason));
1234                 printk("\n");
1235                 writeb(~ADAP_CHK_INT, ti->mmio+ ACA_OFFSET+ACA_RESET+ ISRP_ODD);
1236                 status = readb(ti->mmio + ACA_OFFSET + ACA_RW + ISRA_EVEN);
1237                 DPRINTK("ISRA_EVEN == 0x02%x\n",status);
1238                 ti->open_status = CLOSED;
1239                 ti->sap_status  = CLOSED;
1240                 ti->open_mode   = AUTOMATIC;
1241                 netif_carrier_off(dev);
1242                 netif_stop_queue(dev);
1243                 ti->open_action = RESTART;
1244                 outb(0, dev->base_addr + ADAPTRESET);
1245                 ibmtr_reset_timer(&(ti->tr_timer), dev);/*BMS try to reopen*/
1246                 spin_unlock(&(ti->lock));
1247                 return IRQ_HANDLED;
1248         }
1249         if (readb(ti->mmio + ACA_OFFSET + ACA_RW + ISRP_EVEN)
1250                 & (TCR_INT | ERR_INT | ACCESS_INT)) {
1251                 DPRINTK("adapter error: ISRP_EVEN : %02x\n",
1252                         (int)readb(ti->mmio+ ACA_OFFSET + ACA_RW + ISRP_EVEN));
1253                 writeb(~(TCR_INT | ERR_INT | ACCESS_INT),
1254                         ti->mmio + ACA_OFFSET + ACA_RESET + ISRP_EVEN);
1255                 status= readb(ti->mmio+ ACA_OFFSET + ACA_RW + ISRA_EVEN);/*BMS*/
1256                 DPRINTK("ISRA_EVEN == 0x02%x\n",status);/*BMS*/
1257                 writeb(INT_ENABLE, ti->mmio + ACA_OFFSET + ACA_SET + ISRP_EVEN);
1258 #ifdef ENABLE_PAGING
1259                 writeb(save_srpr, ti->mmio + ACA_OFFSET + ACA_RW + SRPR_EVEN);
1260 #endif
1261                 spin_unlock(&(ti->lock));
1262                 return IRQ_HANDLED;
1263         }
1264         if (status & SRB_RESP_INT) {    /* SRB response */
1265                 SET_PAGE(ti->srb_page);
1266 #if TR_VERBOSE
1267                 DPRINTK("SRB resp: cmd=%02X rsp=%02X\n",
1268                                 readb(ti->srb), readb(ti->srb + RETCODE_OFST));
1269 #endif
1270                 switch (readb(ti->srb)) {       /* SRB command check */
1271                 case XMIT_DIR_FRAME:{
1272                         unsigned char xmit_ret_code;
1273                         xmit_ret_code = readb(ti->srb + RETCODE_OFST);
1274                         if (xmit_ret_code == 0xff) break;
1275                         DPRINTK("error on xmit_dir_frame request: %02X\n",
1276                                                                 xmit_ret_code);
1277                         if (ti->current_skb) {
1278                                 dev_kfree_skb_irq(ti->current_skb);
1279                                 ti->current_skb = NULL;
1280                         }
1281                         /*dev->tbusy = 0;*/
1282                         netif_wake_queue(dev);
1283                         if (ti->readlog_pending)
1284                                 ibmtr_readlog(dev);
1285                         break;
1286                 }
1287                 case XMIT_UI_FRAME:{
1288                         unsigned char xmit_ret_code;
1289
1290                         xmit_ret_code = readb(ti->srb + RETCODE_OFST);
1291                         if (xmit_ret_code == 0xff) break;
1292                         DPRINTK("error on xmit_ui_frame request: %02X\n",
1293                                                                 xmit_ret_code);
1294                         if (ti->current_skb) {
1295                                 dev_kfree_skb_irq(ti->current_skb);
1296                                 ti->current_skb = NULL;
1297                         }
1298                         netif_wake_queue(dev);
1299                         if (ti->readlog_pending)
1300                                 ibmtr_readlog(dev);
1301                         break;
1302                 }
1303                 case DIR_OPEN_ADAPTER:
1304                         dir_open_adapter(dev);
1305                         break;
1306                 case DLC_OPEN_SAP:
1307                         if (readb(ti->srb + RETCODE_OFST)) {
1308                                 DPRINTK("open_sap failed: ret_code = %02X, "
1309                                         "retrying\n",
1310                                         (int) readb(ti->srb + RETCODE_OFST));
1311                                 ti->open_action = REOPEN;
1312                                 ibmtr_reset_timer(&(ti->tr_timer), dev);
1313                                 break;
1314                         }
1315                         ti->exsap_station_id = readw(ti->srb + STATION_ID_OFST);
1316                         ti->sap_status = OPEN;/* TR adapter is now available */
1317                         if (ti->open_mode==MANUAL){
1318                                 wake_up(&ti->wait_for_reset);
1319                                 break;
1320                         }
1321                         netif_wake_queue(dev);
1322                         netif_carrier_on(dev);
1323                         break;
1324                 case DIR_INTERRUPT:
1325                 case DIR_MOD_OPEN_PARAMS:
1326                 case DIR_SET_GRP_ADDR:
1327                 case DIR_SET_FUNC_ADDR:
1328                 case DLC_CLOSE_SAP:
1329                         if (readb(ti->srb + RETCODE_OFST))
1330                                 DPRINTK("error on %02X: %02X\n",
1331                                         (int) readb(ti->srb + COMMAND_OFST),
1332                                         (int) readb(ti->srb + RETCODE_OFST));
1333                         break;
1334                 case DIR_READ_LOG:
1335                         if (readb(ti->srb + RETCODE_OFST)){
1336                                 DPRINTK("error on dir_read_log: %02X\n",
1337                                         (int) readb(ti->srb + RETCODE_OFST));
1338                                 netif_wake_queue(dev);
1339                                 break;
1340                         }
1341 #if IBMTR_DEBUG_MESSAGES
1342
1343 #define LINE_ERRORS_OFST                 0
1344 #define INTERNAL_ERRORS_OFST             1
1345 #define BURST_ERRORS_OFST                2
1346 #define AC_ERRORS_OFST                   3
1347 #define ABORT_DELIMITERS_OFST            4
1348 #define LOST_FRAMES_OFST                 6
1349 #define RECV_CONGEST_COUNT_OFST          7
1350 #define FRAME_COPIED_ERRORS_OFST         8
1351 #define FREQUENCY_ERRORS_OFST            9
1352 #define TOKEN_ERRORS_OFST               10
1353
1354                         DPRINTK("Line errors %02X, Internal errors %02X, "
1355                         "Burst errors %02X\n" "A/C errors %02X, "
1356                         "Abort delimiters %02X, Lost frames %02X\n"
1357                         "Receive congestion count %02X, "
1358                         "Frame copied errors %02X\nFrequency errors %02X, "
1359                         "Token errors %02X\n",
1360                         (int) readb(ti->srb + LINE_ERRORS_OFST),
1361                         (int) readb(ti->srb + INTERNAL_ERRORS_OFST),
1362                         (int) readb(ti->srb + BURST_ERRORS_OFST),
1363                         (int) readb(ti->srb + AC_ERRORS_OFST),
1364                         (int) readb(ti->srb + ABORT_DELIMITERS_OFST),
1365                         (int) readb(ti->srb + LOST_FRAMES_OFST),
1366                         (int) readb(ti->srb + RECV_CONGEST_COUNT_OFST),
1367                         (int) readb(ti->srb + FRAME_COPIED_ERRORS_OFST),
1368                         (int) readb(ti->srb + FREQUENCY_ERRORS_OFST),
1369                         (int) readb(ti->srb + TOKEN_ERRORS_OFST));
1370 #endif
1371                         netif_wake_queue(dev);
1372                         break;
1373                 default:
1374                         DPRINTK("Unknown command %02X encountered\n",
1375                                                 (int) readb(ti->srb));
1376                 }       /* end switch SRB command check */
1377                 writeb(~SRB_RESP_INT, ti->mmio+ ACA_OFFSET+ACA_RESET+ ISRP_ODD);
1378         }       /* if SRB response */
1379         if (status & ASB_FREE_INT) {    /* ASB response */
1380                 SET_PAGE(ti->asb_page);
1381 #if TR_VERBOSE
1382                 DPRINTK("ASB resp: cmd=%02X\n", readb(ti->asb));
1383 #endif
1384
1385                 switch (readb(ti->asb)) {       /* ASB command check */
1386                 case REC_DATA:
1387                 case XMIT_UI_FRAME:
1388                 case XMIT_DIR_FRAME:
1389                         break;
1390                 default:
1391                         DPRINTK("unknown command in asb %02X\n",
1392                                                 (int) readb(ti->asb));
1393                 }       /* switch ASB command check */
1394                 if (readb(ti->asb + 2) != 0xff) /* checks ret_code */
1395                         DPRINTK("ASB error %02X in cmd %02X\n",
1396                                 (int) readb(ti->asb + 2), (int) readb(ti->asb));
1397                 writeb(~ASB_FREE_INT, ti->mmio+ ACA_OFFSET+ACA_RESET+ ISRP_ODD);
1398         }       /* if ASB response */
1399
1400 #define STATUS_OFST             6
1401 #define NETW_STATUS_OFST        6
1402
1403         if (status & ARB_CMD_INT) {     /* ARB response */
1404                 SET_PAGE(ti->arb_page);
1405 #if TR_VERBOSE
1406                 DPRINTK("ARB resp: cmd=%02X\n", readb(ti->arb));
1407 #endif
1408
1409                 switch (readb(ti->arb)) {       /* ARB command check */
1410                 case DLC_STATUS:
1411                         DPRINTK("DLC_STATUS new status: %02X on station %02X\n",
1412                                 ntohs(readw(ti->arb + STATUS_OFST)),
1413                                 ntohs(readw(ti->arb+ STATION_ID_OFST)));
1414                         break;
1415                 case REC_DATA:
1416                         tr_rx(dev);
1417                         break;
1418                 case RING_STAT_CHANGE:{
1419                         unsigned short ring_status;
1420                         ring_status= ntohs(readw(ti->arb + NETW_STATUS_OFST));
1421                         if (ibmtr_debug_trace & TRC_INIT)
1422                                 DPRINTK("Ring Status Change...(0x%x)\n",
1423                                                                 ring_status);
1424                         if(ring_status& (REMOVE_RECV|AUTO_REMOVAL|LOBE_FAULT)){
1425                                 netif_stop_queue(dev);
1426                                 netif_carrier_off(dev);
1427                                 DPRINTK("Remove received, or Auto-removal error"
1428                                         ", or Lobe fault\n");
1429                                 DPRINTK("We'll try to reopen the closed adapter"
1430                                         " after a %d second delay.\n",
1431                                                 TR_RETRY_INTERVAL/HZ);
1432                                 /*I was confused: I saw the TR reopening but */
1433                                 /*forgot:with an RJ45 in an RJ45/ICS adapter */
1434                                 /*but adapter not in the ring, the TR will   */
1435                                 /* open, and then soon close and come here.  */
1436                                 ti->open_mode = AUTOMATIC;
1437                                 ti->open_status = CLOSED; /*12/2000 BMS*/
1438                                 ti->open_action = REOPEN;
1439                                 ibmtr_reset_timer(&(ti->tr_timer), dev);
1440                         } else if (ring_status & LOG_OVERFLOW) {
1441                                 if(netif_queue_stopped(dev))
1442                                         ti->readlog_pending = 1;
1443                                 else
1444                                         ibmtr_readlog(dev);
1445                         }
1446                         break;
1447                 }
1448                 case XMIT_DATA_REQ:
1449                         tr_tx(dev);
1450                         break;
1451                 default:
1452                         DPRINTK("Unknown command %02X in arb\n",
1453                                                 (int) readb(ti->arb));
1454                         break;
1455                 }       /* switch ARB command check */
1456                 writeb(~ARB_CMD_INT, ti->mmio+ ACA_OFFSET+ACA_RESET + ISRP_ODD);
1457                 writeb(ARB_FREE, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
1458         }       /* if ARB response */
1459         if (status & SSB_RESP_INT) {    /* SSB response */
1460                 unsigned char retcode;
1461                 SET_PAGE(ti->ssb_page);
1462 #if TR_VERBOSE
1463                 DPRINTK("SSB resp: cmd=%02X rsp=%02X\n",
1464                                 readb(ti->ssb), readb(ti->ssb + 2));
1465 #endif
1466
1467                 switch (readb(ti->ssb)) {       /* SSB command check */
1468                 case XMIT_DIR_FRAME:
1469                 case XMIT_UI_FRAME:
1470                         retcode = readb(ti->ssb + 2);
1471                         if (retcode && (retcode != 0x22))/* checks ret_code */
1472                                 DPRINTK("xmit ret_code: %02X xmit error code: "
1473                                         "%02X\n",
1474                                         (int)retcode, (int)readb(ti->ssb + 6));
1475                         else
1476                                 ti->tr_stats.tx_packets++;
1477                         break;
1478                 case XMIT_XID_CMD:
1479                         DPRINTK("xmit xid ret_code: %02X\n",
1480                                                 (int) readb(ti->ssb + 2));
1481                 default:
1482                         DPRINTK("Unknown command %02X in ssb\n",
1483                                                 (int) readb(ti->ssb));
1484                 }       /* SSB command check */
1485                 writeb(~SSB_RESP_INT, ti->mmio+ ACA_OFFSET+ACA_RESET+ ISRP_ODD);
1486                 writeb(SSB_FREE, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
1487         }       /* if SSB response */
1488 #ifdef ENABLE_PAGING
1489         writeb(save_srpr, ti->mmio + ACA_OFFSET + ACA_RW + SRPR_EVEN);
1490 #endif
1491         writeb(INT_ENABLE, ti->mmio + ACA_OFFSET + ACA_SET + ISRP_EVEN);
1492         spin_unlock(&(ti->lock));
1493         return IRQ_HANDLED;
1494 }                               /*tok_interrupt */
1495
1496 /*****************************************************************************/
1497
1498 #define INIT_STATUS_OFST        1
1499 #define INIT_STATUS_2_OFST      2
1500 #define ENCODED_ADDRESS_OFST    8
1501
1502 static void initial_tok_int(struct net_device *dev)
1503 {
1504
1505         __u32 encoded_addr, hw_encoded_addr;
1506         struct tok_info *ti;
1507         unsigned char init_status; /*BMS 12/2000*/
1508
1509         ti = (struct tok_info *) dev->priv;
1510
1511         ti->do_tok_int = NOT_FIRST;
1512
1513         /* we assign the shared-ram address for ISA devices */
1514         writeb(ti->sram_base, ti->mmio + ACA_OFFSET + ACA_RW + RRR_EVEN);
1515 #ifndef PCMCIA
1516         ti->sram_virt = ioremap(((__u32)ti->sram_base << 12), ti->avail_shared_ram);
1517 #endif
1518         ti->init_srb = map_address(ti,
1519                 ntohs(readw(ti->mmio + ACA_OFFSET + WRBR_EVEN)),
1520                 &ti->init_srb_page);
1521         if (ti->page_mask && ti->avail_shared_ram == 127) {
1522                 void __iomem *last_512;
1523                 __u8 last_512_page=0;
1524                 int i;
1525                 last_512 = map_address(ti, 0xfe00, &last_512_page);
1526                 /* initialize high section of ram (if necessary) */
1527                 SET_PAGE(last_512_page);
1528                 for (i = 0; i < 512; i++)
1529                         writeb(0, last_512 + i);
1530         }
1531         SET_PAGE(ti->init_srb_page);
1532
1533 #if TR_VERBOSE
1534         {
1535         int i;
1536
1537         DPRINTK("ti->init_srb_page=0x%x\n", ti->init_srb_page);
1538         DPRINTK("init_srb(%p):", ti->init_srb );
1539         for (i = 0; i < 20; i++)
1540                 printk("%02X ", (int) readb(ti->init_srb + i));
1541         printk("\n");
1542         }
1543 #endif
1544
1545         hw_encoded_addr = readw(ti->init_srb + ENCODED_ADDRESS_OFST);
1546         encoded_addr = ntohs(hw_encoded_addr);
1547         init_status= /*BMS 12/2000 check for shallow mode possibility (Turbo)*/
1548         readb(ti->init_srb+offsetof(struct srb_init_response,init_status));
1549         /*printk("Initial interrupt: init_status= 0x%02x\n",init_status);*/
1550         ti->ring_speed = init_status & 0x01 ? 16 : 4;
1551         DPRINTK("Initial interrupt : %d Mbps, shared RAM base %08x.\n",
1552                                 ti->ring_speed, (unsigned int)dev->mem_start);
1553         ti->auto_speedsave=readb(ti->init_srb+INIT_STATUS_2_OFST)&4?TRUE:FALSE;
1554
1555         if (ti->open_mode == MANUAL)    wake_up(&ti->wait_for_reset);
1556         else                            tok_open_adapter((unsigned long)dev);
1557         
1558 } /*initial_tok_int() */
1559
1560 /*****************************************************************************/
1561
1562 #define CMD_CORRELATE_OFST      1
1563 #define DHB_ADDRESS_OFST        6
1564
1565 #define FRAME_LENGTH_OFST       6
1566 #define HEADER_LENGTH_OFST      8
1567 #define RSAP_VALUE_OFST         9
1568
1569 static void tr_tx(struct net_device *dev)
1570 {
1571         struct tok_info *ti = (struct tok_info *) dev->priv;
1572         struct trh_hdr *trhdr = (struct trh_hdr *) ti->current_skb->data;
1573         unsigned int hdr_len;
1574         __u32 dhb=0,dhb_base;
1575         void __iomem *dhbuf = NULL;
1576         unsigned char xmit_command;
1577         int i,dhb_len=0x4000,src_len,src_offset;
1578         struct trllc *llc;
1579         struct srb_xmit xsrb;
1580         __u8 dhb_page = 0;
1581         __u8 llc_ssap;
1582
1583         SET_PAGE(ti->asb_page);
1584
1585         if (readb(ti->asb+RETCODE_OFST) != 0xFF) DPRINTK("ASB not free !!!\n");
1586
1587         /* in providing the transmit interrupts, is telling us it is ready for
1588            data and providing a shared memory address for us to stuff with data.
1589            Here we compute the effective address where we will place data.
1590         */
1591         SET_PAGE(ti->arb_page);
1592         dhb=dhb_base=ntohs(readw(ti->arb + DHB_ADDRESS_OFST));
1593         if (ti->page_mask) {
1594                 dhb_page = (dhb_base >> 8) & ti->page_mask;
1595                 dhb=dhb_base & ~(ti->page_mask << 8);
1596         }
1597         dhbuf = ti->sram_virt + dhb;
1598
1599         /* Figure out the size of the 802.5 header */
1600         if (!(trhdr->saddr[0] & 0x80))  /* RIF present? */
1601                 hdr_len = sizeof(struct trh_hdr) - TR_MAXRIFLEN;
1602         else
1603                 hdr_len = ((ntohs(trhdr->rcf) & TR_RCF_LEN_MASK) >> 8)
1604                     + sizeof(struct trh_hdr) - TR_MAXRIFLEN;
1605
1606         llc = (struct trllc *) (ti->current_skb->data + hdr_len);
1607
1608         llc_ssap = llc->ssap;
1609         SET_PAGE(ti->srb_page);
1610         memcpy_fromio(&xsrb, ti->srb, sizeof(xsrb));
1611         SET_PAGE(ti->asb_page);
1612         xmit_command = xsrb.command;
1613
1614         writeb(xmit_command, ti->asb + COMMAND_OFST);
1615         writew(xsrb.station_id, ti->asb + STATION_ID_OFST);
1616         writeb(llc_ssap, ti->asb + RSAP_VALUE_OFST);
1617         writeb(xsrb.cmd_corr, ti->asb + CMD_CORRELATE_OFST);
1618         writeb(0, ti->asb + RETCODE_OFST);
1619         if ((xmit_command == XMIT_XID_CMD) || (xmit_command == XMIT_TEST_CMD)) {
1620                 writew(htons(0x11), ti->asb + FRAME_LENGTH_OFST);
1621                 writeb(0x0e, ti->asb + HEADER_LENGTH_OFST);
1622                 SET_PAGE(dhb_page);
1623                 writeb(AC, dhbuf);
1624                 writeb(LLC_FRAME, dhbuf + 1);
1625                 for (i = 0; i < TR_ALEN; i++)
1626                         writeb((int) 0x0FF, dhbuf + i + 2);
1627                 for (i = 0; i < TR_ALEN; i++)
1628                         writeb(0, dhbuf + i + TR_ALEN + 2);
1629                 writeb(RESP_IN_ASB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
1630                 return;
1631         }
1632         /*
1633          *    the token ring packet is copied from sk_buff to the adapter
1634          *    buffer identified in the command data received with the interrupt.
1635          */
1636         writeb(hdr_len, ti->asb + HEADER_LENGTH_OFST);
1637         writew(htons(ti->current_skb->len), ti->asb + FRAME_LENGTH_OFST);
1638         src_len=ti->current_skb->len;
1639         src_offset=0;
1640         dhb=dhb_base;
1641         while(1) {
1642                 if (ti->page_mask) {
1643                         dhb_page=(dhb >> 8) & ti->page_mask;
1644                         dhb=dhb & ~(ti->page_mask << 8);
1645                         dhb_len=0x4000-dhb; /* remaining size of this page */
1646                 }
1647                 dhbuf = ti->sram_virt + dhb;
1648                 SET_PAGE(dhb_page);
1649                 if (src_len > dhb_len) {
1650                         memcpy_toio(dhbuf,&ti->current_skb->data[src_offset],
1651                                         dhb_len);
1652                         src_len -= dhb_len;
1653                         src_offset += dhb_len;
1654                         dhb_base+=dhb_len;
1655                         dhb=dhb_base;
1656                         continue;
1657                 }
1658                 memcpy_toio(dhbuf, &ti->current_skb->data[src_offset], src_len);
1659                 break;
1660         }
1661         writeb(RESP_IN_ASB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
1662         ti->tr_stats.tx_bytes += ti->current_skb->len;
1663         dev_kfree_skb_irq(ti->current_skb);
1664         ti->current_skb = NULL;
1665         netif_wake_queue(dev);
1666         if (ti->readlog_pending)
1667                 ibmtr_readlog(dev);
1668 }                               /*tr_tx */
1669
1670 /*****************************************************************************/
1671
1672
1673 #define RECEIVE_BUFFER_OFST     6
1674 #define LAN_HDR_LENGTH_OFST     8
1675 #define DLC_HDR_LENGTH_OFST     9
1676
1677 #define DSAP_OFST               0
1678 #define SSAP_OFST               1
1679 #define LLC_OFST                2
1680 #define PROTID_OFST             3
1681 #define ETHERTYPE_OFST          6
1682
1683 static void tr_rx(struct net_device *dev)
1684 {
1685         struct tok_info *ti = (struct tok_info *) dev->priv;
1686         __u32 rbuffer;
1687         void __iomem *rbuf, *rbufdata, *llc;
1688         __u8 rbuffer_page = 0;
1689         unsigned char *data;
1690         unsigned int rbuffer_len, lan_hdr_len, hdr_len, ip_len, length;
1691         unsigned char dlc_hdr_len;
1692         struct sk_buff *skb;
1693         unsigned int skb_size = 0;
1694         int IPv4_p = 0;
1695         unsigned int chksum = 0;
1696         struct iphdr *iph;
1697         struct arb_rec_req rarb;
1698
1699         SET_PAGE(ti->arb_page);
1700         memcpy_fromio(&rarb, ti->arb, sizeof(rarb));
1701         rbuffer = ntohs(rarb.rec_buf_addr) ;
1702         rbuf = map_address(ti, rbuffer, &rbuffer_page);
1703
1704         SET_PAGE(ti->asb_page);
1705
1706         if (readb(ti->asb + RETCODE_OFST) !=0xFF) DPRINTK("ASB not free !!!\n");
1707
1708         writeb(REC_DATA, ti->asb + COMMAND_OFST);
1709         writew(rarb.station_id, ti->asb + STATION_ID_OFST);
1710         writew(rarb.rec_buf_addr, ti->asb + RECEIVE_BUFFER_OFST);
1711
1712         lan_hdr_len = rarb.lan_hdr_len;
1713         if (lan_hdr_len > sizeof(struct trh_hdr)) {
1714                 DPRINTK("Linux cannot handle greater than 18 bytes RIF\n");
1715                 return;
1716         }                       /*BMS I added this above just to be very safe */
1717         dlc_hdr_len = readb(ti->arb + DLC_HDR_LENGTH_OFST);
1718         hdr_len = lan_hdr_len + sizeof(struct trllc) + sizeof(struct iphdr);
1719
1720         SET_PAGE(rbuffer_page);
1721         llc = rbuf + offsetof(struct rec_buf, data) + lan_hdr_len;
1722
1723 #if TR_VERBOSE
1724         DPRINTK("offsetof data: %02X lan_hdr_len: %02X\n",
1725         (__u32) offsetof(struct rec_buf, data), (unsigned int) lan_hdr_len);
1726         DPRINTK("llc: %08X rec_buf_addr: %04X dev->mem_start: %lX\n",
1727                 llc, ntohs(rarb.rec_buf_addr), dev->mem_start);
1728         DPRINTK("dsap: %02X, ssap: %02X, llc: %02X, protid: %02X%02X%02X, "
1729                 "ethertype: %04X\n",
1730                 (int) readb(llc + DSAP_OFST), (int) readb(llc + SSAP_OFST),
1731                 (int) readb(llc + LLC_OFST), (int) readb(llc + PROTID_OFST),
1732                 (int) readb(llc+PROTID_OFST+1),(int)readb(llc+PROTID_OFST + 2),
1733                 (int) ntohs(readw(llc + ETHERTYPE_OFST)));
1734 #endif
1735         if (readb(llc + offsetof(struct trllc, llc)) != UI_CMD) {
1736                 SET_PAGE(ti->asb_page);
1737                 writeb(DATA_LOST, ti->asb + RETCODE_OFST);
1738                 ti->tr_stats.rx_dropped++;
1739                 writeb(RESP_IN_ASB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
1740                 return;
1741         }
1742         length = ntohs(rarb.frame_len);
1743         if (readb(llc + DSAP_OFST) == EXTENDED_SAP &&
1744            readb(llc + SSAP_OFST) == EXTENDED_SAP &&
1745                 length >= hdr_len)      IPv4_p = 1;
1746 #if TR_VERBOSE
1747 #define SADDR_OFST      8
1748 #define DADDR_OFST      2
1749
1750         if (!IPv4_p) {
1751
1752                 void __iomem *trhhdr = rbuf + offsetof(struct rec_buf, data);
1753
1754                 DPRINTK("Probably non-IP frame received.\n");
1755                 DPRINTK("ssap: %02X dsap: %02X "
1756                         "saddr: %02X:%02X:%02X:%02X:%02X:%02X "
1757                         "daddr: %02X:%02X:%02X:%02X:%02X:%02X\n",
1758                         readb(llc + SSAP_OFST), readb(llc + DSAP_OFST),
1759                         readb(trhhdr+SADDR_OFST), readb(trhhdr+ SADDR_OFST+1),
1760                         readb(trhhdr+SADDR_OFST+2), readb(trhhdr+SADDR_OFST+3),
1761                         readb(trhhdr+SADDR_OFST+4), readb(trhhdr+SADDR_OFST+5),
1762                         readb(trhhdr+DADDR_OFST), readb(trhhdr+DADDR_OFST + 1),
1763                         readb(trhhdr+DADDR_OFST+2), readb(trhhdr+DADDR_OFST+3),
1764                         readb(trhhdr+DADDR_OFST+4), readb(trhhdr+DADDR_OFST+5));
1765         }
1766 #endif
1767
1768         /*BMS handle the case she comes in with few hops but leaves with many */
1769         skb_size=length-lan_hdr_len+sizeof(struct trh_hdr)+sizeof(struct trllc);
1770
1771         if (!(skb = dev_alloc_skb(skb_size))) {
1772                 DPRINTK("out of memory. frame dropped.\n");
1773                 ti->tr_stats.rx_dropped++;
1774                 SET_PAGE(ti->asb_page);
1775                 writeb(DATA_LOST, ti->asb + offsetof(struct asb_rec, ret_code));
1776                 writeb(RESP_IN_ASB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
1777                 return;
1778         }
1779         /*BMS again, if she comes in with few but leaves with many */
1780         skb_reserve(skb, sizeof(struct trh_hdr) - lan_hdr_len);
1781         skb_put(skb, length);
1782         skb->dev = dev;
1783         data = skb->data;
1784         rbuffer_len = ntohs(readw(rbuf + offsetof(struct rec_buf, buf_len)));
1785         rbufdata = rbuf + offsetof(struct rec_buf, data);
1786
1787         if (IPv4_p) {
1788                 /* Copy the headers without checksumming */
1789                 memcpy_fromio(data, rbufdata, hdr_len);
1790
1791                 /* Watch for padded packets and bogons */
1792                 iph= (struct iphdr *)(data+ lan_hdr_len + sizeof(struct trllc));
1793                 ip_len = ntohs(iph->tot_len) - sizeof(struct iphdr);
1794                 length -= hdr_len;
1795                 if ((ip_len <= length) && (ip_len > 7))
1796                         length = ip_len;
1797                 data += hdr_len;
1798                 rbuffer_len -= hdr_len;
1799                 rbufdata += hdr_len;
1800         }
1801         /* Copy the payload... */
1802 #define BUFFER_POINTER_OFST     2
1803 #define BUFFER_LENGTH_OFST      6
1804         for (;;) {
1805                 if (ibmtr_debug_trace&TRC_INITV && length < rbuffer_len)
1806                         DPRINTK("CURIOUS, length=%d < rbuffer_len=%d\n",
1807                                                 length,rbuffer_len);
1808                 if (IPv4_p)
1809                         chksum=csum_partial_copy_nocheck((void*)rbufdata,
1810                             data,length<rbuffer_len?length:rbuffer_len,chksum);
1811                 else
1812                         memcpy_fromio(data, rbufdata, rbuffer_len);
1813                 rbuffer = ntohs(readw(rbuf+BUFFER_POINTER_OFST)) ;
1814                 if (!rbuffer)
1815                         break;
1816                 rbuffer -= 2;
1817                 length -= rbuffer_len;
1818                 data += rbuffer_len;
1819                 rbuf = map_address(ti, rbuffer, &rbuffer_page);
1820                 SET_PAGE(rbuffer_page);
1821                 rbuffer_len = ntohs(readw(rbuf + BUFFER_LENGTH_OFST));
1822                 rbufdata = rbuf + offsetof(struct rec_buf, data);
1823         }
1824
1825         SET_PAGE(ti->asb_page);
1826         writeb(0, ti->asb + offsetof(struct asb_rec, ret_code));
1827
1828         writeb(RESP_IN_ASB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
1829
1830         ti->tr_stats.rx_bytes += skb->len;
1831         ti->tr_stats.rx_packets++;
1832
1833         skb->protocol = tr_type_trans(skb, dev);
1834         if (IPv4_p) {
1835                 skb->csum = chksum;
1836                 skb->ip_summed = 1;
1837         }
1838         netif_rx(skb);
1839         dev->last_rx = jiffies;
1840 }                               /*tr_rx */
1841
1842 /*****************************************************************************/
1843
1844 static void ibmtr_reset_timer(struct timer_list *tmr, struct net_device *dev)
1845 {
1846         tmr->expires = jiffies + TR_RETRY_INTERVAL;
1847         tmr->data = (unsigned long) dev;
1848         tmr->function = tok_rerun;
1849         init_timer(tmr);
1850         add_timer(tmr);
1851 }
1852
1853 /*****************************************************************************/
1854
1855 void tok_rerun(unsigned long dev_addr){
1856
1857         struct net_device *dev = (struct net_device *)dev_addr;
1858         struct tok_info *ti = (struct tok_info *) dev->priv;
1859
1860         if ( ti->open_action == RESTART){
1861                 ti->do_tok_int = FIRST_INT;
1862                 outb(0, dev->base_addr + ADAPTRESETREL);
1863 #ifdef ENABLE_PAGING
1864                 if (ti->page_mask)
1865                         writeb(SRPR_ENABLE_PAGING,
1866                                 ti->mmio + ACA_OFFSET + ACA_RW + SRPR_EVEN);
1867 #endif
1868
1869                 writeb(INT_ENABLE, ti->mmio + ACA_OFFSET + ACA_SET + ISRP_EVEN);
1870         } else
1871                 tok_open_adapter(dev_addr);
1872 }
1873
1874 /*****************************************************************************/
1875
1876 static void ibmtr_readlog(struct net_device *dev)
1877 {
1878         struct tok_info *ti;
1879
1880         ti = (struct tok_info *) dev->priv;
1881
1882         ti->readlog_pending = 0;
1883         SET_PAGE(ti->srb_page);
1884         writeb(DIR_READ_LOG, ti->srb);
1885         writeb(INT_ENABLE, ti->mmio + ACA_OFFSET + ACA_SET + ISRP_EVEN);
1886         writeb(CMD_IN_SRB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
1887
1888         netif_stop_queue(dev);
1889
1890 }
1891
1892 /*****************************************************************************/
1893
1894 /* tok_get_stats():  Basically a scaffold routine which will return
1895    the address of the tr_statistics structure associated with
1896    this device -- the tr.... structure is an ethnet look-alike
1897    so at least for this iteration may suffice.   */
1898
1899 static struct net_device_stats *tok_get_stats(struct net_device *dev)
1900 {
1901
1902         struct tok_info *toki;
1903         toki = (struct tok_info *) dev->priv;
1904         return (struct net_device_stats *) &toki->tr_stats;
1905 }
1906
1907 /*****************************************************************************/
1908
1909 static int ibmtr_change_mtu(struct net_device *dev, int mtu)
1910 {
1911         struct tok_info *ti = (struct tok_info *) dev->priv;
1912
1913         if (ti->ring_speed == 16 && mtu > ti->maxmtu16)
1914                 return -EINVAL;
1915         if (ti->ring_speed == 4 && mtu > ti->maxmtu4)
1916                 return -EINVAL;
1917         dev->mtu = mtu;
1918         return 0;
1919 }
1920
1921 /*****************************************************************************/
1922 #ifdef MODULE
1923
1924 /* 3COM 3C619C supports 8 interrupts, 32 I/O ports */
1925 static struct net_device *dev_ibmtr[IBMTR_MAX_ADAPTERS];
1926 static int io[IBMTR_MAX_ADAPTERS] = { 0xa20, 0xa24 };
1927 static int irq[IBMTR_MAX_ADAPTERS];
1928 static int mem[IBMTR_MAX_ADAPTERS];
1929
1930 MODULE_LICENSE("GPL");
1931
1932 module_param_array(io, int, NULL, 0);
1933 module_param_array(irq, int, NULL, 0);
1934 module_param_array(mem, int, NULL, 0);
1935
1936 static int __init ibmtr_init(void)
1937 {
1938         int i;
1939         int count=0;
1940
1941         find_turbo_adapters(io);
1942
1943         for (i = 0; io[i] && (i < IBMTR_MAX_ADAPTERS); i++) {
1944                 struct net_device *dev;
1945                 irq[i] = 0;
1946                 mem[i] = 0;
1947                 dev = alloc_trdev(sizeof(struct tok_info));
1948                 if (dev == NULL) { 
1949                         if (i == 0)
1950                                 return -ENOMEM;
1951                         break;
1952                 }
1953                 dev->base_addr = io[i];
1954                 dev->irq = irq[i];
1955                 dev->mem_start = mem[i];
1956
1957                 if (ibmtr_probe_card(dev)) {
1958                         free_netdev(dev);
1959                         continue;
1960                 }
1961                 dev_ibmtr[i] = dev;
1962                 count++;
1963         }
1964         if (count) return 0;
1965         printk("ibmtr: register_netdev() returned non-zero.\n");
1966         return -EIO;
1967 }
1968 module_init(ibmtr_init);
1969
1970 static void __exit ibmtr_cleanup(void)
1971 {
1972         int i;
1973
1974         for (i = 0; i < IBMTR_MAX_ADAPTERS; i++){
1975                 if (!dev_ibmtr[i])
1976                         continue;
1977                 unregister_netdev(dev_ibmtr[i]);
1978                 ibmtr_cleanup_card(dev_ibmtr[i]);
1979                 free_netdev(dev_ibmtr[i]);
1980         }
1981 }
1982 module_exit(ibmtr_cleanup);
1983 #endif