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