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