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