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