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