1/* Intel EtherExpress 16 device driver for Linux
2 *
3 * Written by John Sullivan, 1995
4 *  based on original code by Donald Becker, with changes by
5 *  Alan Cox and Pauline Middelink.
6 *
7 * Support for 8-bit mode by Zoltan Szilagyi <zoltans@cs.arizona.edu>
8 *
9 * Many modifications, and currently maintained, by
10 *  Philip Blundell <philb@gnu.org>
11 * Added the Compaq LTE  Alan Cox <alan@redhat.com>
12 * Added MCA support Adam Fritzler <mid@auk.cx>
13 *
14 * Note - this driver is experimental still - it has problems on faster
15 * machines. Someone needs to sit down and go through it line by line with
16 * a databook...
17 */
18
19/* The EtherExpress 16 is a fairly simple card, based on a shared-memory
20 * design using the i82586 Ethernet coprocessor.  It bears no relationship,
21 * as far as I know, to the similarly-named "EtherExpress Pro" range.
22 *
23 * Historically, Linux support for these cards has been very bad.  However,
24 * things seem to be getting better slowly.
25 */
26
27/* If your card is confused about what sort of interface it has (eg it
28 * persistently reports "10baseT" when none is fitted), running 'SOFTSET /BART'
29 * or 'SOFTSET /LISA' from DOS seems to help.
30 */
31
32/* Here's the scoop on memory mapping.
33 *
34 * There are three ways to access EtherExpress card memory: either using the
35 * shared-memory mapping, or using PIO through the dataport, or using PIO
36 * through the "shadow memory" ports.
37 *
38 * The shadow memory system works by having the card map some of its memory
39 * as follows:
40 *
41 * (the low five bits of the SMPTR are ignored)
42 *
43 *  base+0x4000..400f      memory at SMPTR+0..15
44 *  base+0x8000..800f      memory at SMPTR+16..31
45 *  base+0xc000..c007      dubious stuff (memory at SMPTR+16..23 apparently)
46 *  base+0xc008..c00f      memory at 0x0008..0x000f
47 *
48 * This last set (the one at c008) is particularly handy because the SCB
49 * lives at 0x0008.  So that set of ports gives us easy random access to data
50 * in the SCB without having to mess around setting up pointers and the like.
51 * We always use this method to access the SCB (via the scb_xx() functions).
52 *
53 * Dataport access works by aiming the appropriate (read or write) pointer
54 * at the first address you're interested in, and then reading or writing from
55 * the dataport.  The pointers auto-increment after each transfer.  We use
56 * this for data transfer.
57 *
58 * We don't use the shared-memory system because it allegedly doesn't work on
59 * all cards, and because it's a bit more prone to go wrong (it's one more
60 * thing to configure...).
61 */
62
63/* Known bugs:
64 *
65 * - The card seems to want to give us two interrupts every time something
66 *   happens, where just one would be better.
67 */
68
69/*
70 *
71 * Note by Zoltan Szilagyi 10-12-96:
72 *
73 * I've succeeded in eliminating the "CU wedged" messages, and hence the
74 * lockups, which were only occurring with cards running in 8-bit mode ("force
75 * 8-bit operation" in Intel's SoftSet utility). This version of the driver
76 * sets the 82586 and the ASIC to 8-bit mode at startup; it also stops the
77 * CU before submitting a packet for transmission, and then restarts it as soon
78 * as the process of handing the packet is complete. This is definitely an
79 * unnecessary slowdown if the card is running in 16-bit mode; therefore one
80 * should detect 16-bit vs 8-bit mode from the EEPROM settings and act
81 * accordingly. In 8-bit mode with this bugfix I'm getting about 150 K/s for
82 * ftp's, which is significantly better than I get in DOS, so the overhead of
83 * stopping and restarting the CU with each transmit is not prohibitive in
84 * practice.
85 *
86 * Update by David Woodhouse 11/5/99:
87 *
88 * I've seen "CU wedged" messages in 16-bit mode, on the Alpha architecture.
89 * I assume that this is because 16-bit accesses are actually handled as two
90 * 8-bit accesses.
91 */
92
93#ifdef __alpha__
94#define LOCKUP16 1
95#endif
96#ifndef LOCKUP16
97#define LOCKUP16 0
98#endif
99
100#include <linux/module.h>
101#include <linux/kernel.h>
102#include <linux/types.h>
103#include <linux/fcntl.h>
104#include <linux/interrupt.h>
105#include <linux/ioport.h>
106#include <linux/string.h>
107#include <linux/in.h>
108#include <linux/delay.h>
109#include <linux/errno.h>
110#include <linux/init.h>
111#include <linux/netdevice.h>
112#include <linux/etherdevice.h>
113#include <linux/skbuff.h>
114#include <linux/slab.h>
115#include <linux/mca-legacy.h>
116#include <linux/spinlock.h>
117#include <linux/bitops.h>
118#include <linux/jiffies.h>
119
120#include <asm/system.h>
121#include <asm/io.h>
122#include <asm/irq.h>
123
124#ifndef NET_DEBUG
125#define NET_DEBUG 4
126#endif
127
128#include "eexpress.h"
129
130#define EEXP_IO_EXTENT  16
131
132/*
133 * Private data declarations
134 */
135
136struct net_local
137{
138	struct net_device_stats stats;
139	unsigned long last_tx;       /* jiffies when last transmit started */
140	unsigned long init_time;     /* jiffies when eexp_hw_init586 called */
141	unsigned short rx_first;     /* first rx buf, same as RX_BUF_START */
142	unsigned short rx_last;      /* last rx buf */
143	unsigned short rx_ptr;       /* first rx buf to look at */
144	unsigned short tx_head;      /* next free tx buf */
145	unsigned short tx_reap;      /* first in-use tx buf */
146	unsigned short tx_tail;      /* previous tx buf to tx_head */
147	unsigned short tx_link;      /* last known-executing tx buf */
148	unsigned short last_tx_restart;   /* set to tx_link when we
149					     restart the CU */
150	unsigned char started;
151	unsigned short rx_buf_start;
152	unsigned short rx_buf_end;
153	unsigned short num_tx_bufs;
154	unsigned short num_rx_bufs;
155	unsigned char width;         /* 0 for 16bit, 1 for 8bit */
156	unsigned char was_promisc;
157	unsigned char old_mc_count;
158	spinlock_t lock;
159};
160
161/* This is the code and data that is downloaded to the EtherExpress card's
162 * memory at boot time.
163 */
164
165static unsigned short start_code[] = {
166/* 0x0000 */
167	0x0001,                 /* ISCP: busy - cleared after reset */
168	0x0008,0x0000,0x0000,   /* offset,address (lo,hi) of SCB */
169
170	0x0000,0x0000,          /* SCB: status, commands */
171	0x0000,0x0000,          /* links to first command block,
172				   first receive descriptor */
173	0x0000,0x0000,          /* CRC error, alignment error counts */
174	0x0000,0x0000,          /* out of resources, overrun error counts */
175
176	0x0000,0x0000,          /* pad */
177	0x0000,0x0000,
178
179/* 0x20 -- start of 82586 CU program */
180#define CONF_LINK 0x20
181	0x0000,Cmd_Config,
182	0x0032,                 /* link to next command */
183	0x080c,                 /* 12 bytes follow : fifo threshold=8 */
184	0x2e40,                 /* don't rx bad frames
185				 * SRDY/ARDY => ext. sync. : preamble len=8
186	                         * take addresses from data buffers
187				 * 6 bytes/address
188				 */
189	0x6000,                 /* default backoff method & priority
190				 * interframe spacing = 0x60 */
191	0xf200,                 /* slot time=0x200
192				 * max collision retry = 0xf */
193#define CONF_PROMISC  0x2e
194	0x0000,                 /* no HDLC : normal CRC : enable broadcast
195				 * disable promiscuous/multicast modes */
196	0x003c,                 /* minimum frame length = 60 octets) */
197
198	0x0000,Cmd_SetAddr,
199	0x003e,                 /* link to next command */
200#define CONF_HWADDR  0x38
201	0x0000,0x0000,0x0000,   /* hardware address placed here */
202
203	0x0000,Cmd_MCast,
204	0x0076,                 /* link to next command */
205#define CONF_NR_MULTICAST 0x44
206	0x0000,                 /* number of multicast addresses */
207#define CONF_MULTICAST 0x46
208	0x0000, 0x0000, 0x0000, /* some addresses */
209	0x0000, 0x0000, 0x0000,
210	0x0000, 0x0000, 0x0000,
211	0x0000, 0x0000, 0x0000,
212	0x0000, 0x0000, 0x0000,
213	0x0000, 0x0000, 0x0000,
214	0x0000, 0x0000, 0x0000,
215	0x0000, 0x0000, 0x0000,
216
217#define CONF_DIAG_RESULT  0x76
218	0x0000, Cmd_Diag,
219	0x007c,                 /* link to next command */
220
221	0x0000,Cmd_TDR|Cmd_INT,
222	0x0084,
223#define CONF_TDR_RESULT  0x82
224	0x0000,
225
226	0x0000,Cmd_END|Cmd_Nop, /* end of configure sequence */
227	0x0084                  /* dummy link */
228};
229
230/* maps irq number to EtherExpress magic value */
231static char irqrmap[] = { 0,0,1,2,3,4,0,0,0,1,5,6,0,0,0,0 };
232
233#ifdef CONFIG_MCA_LEGACY
234/* mapping of the first four bits of the second POS register */
235static unsigned short mca_iomap[] = {
236	0x270, 0x260, 0x250, 0x240, 0x230, 0x220, 0x210, 0x200,
237	0x370, 0x360, 0x350, 0x340, 0x330, 0x320, 0x310, 0x300
238};
239/* bits 5-7 of the second POS register */
240static char mca_irqmap[] = { 12, 9, 3, 4, 5, 10, 11, 15 };
241#endif
242
243/*
244 * Prototypes for Linux interface
245 */
246
247static int eexp_open(struct net_device *dev);
248static int eexp_close(struct net_device *dev);
249static void eexp_timeout(struct net_device *dev);
250static struct net_device_stats *eexp_stats(struct net_device *dev);
251static int eexp_xmit(struct sk_buff *buf, struct net_device *dev);
252
253static irqreturn_t eexp_irq(int irq, void *dev_addr);
254static void eexp_set_multicast(struct net_device *dev);
255
256/*
257 * Prototypes for hardware access functions
258 */
259
260static void eexp_hw_rx_pio(struct net_device *dev);
261static void eexp_hw_tx_pio(struct net_device *dev, unsigned short *buf,
262		       unsigned short len);
263static int eexp_hw_probe(struct net_device *dev,unsigned short ioaddr);
264static unsigned short eexp_hw_readeeprom(unsigned short ioaddr,
265					 unsigned char location);
266
267static unsigned short eexp_hw_lasttxstat(struct net_device *dev);
268static void eexp_hw_txrestart(struct net_device *dev);
269
270static void eexp_hw_txinit    (struct net_device *dev);
271static void eexp_hw_rxinit    (struct net_device *dev);
272
273static void eexp_hw_init586   (struct net_device *dev);
274static void eexp_setup_filter (struct net_device *dev);
275
276static char *eexp_ifmap[]={"AUI", "BNC", "RJ45"};
277enum eexp_iftype {AUI=0, BNC=1, TPE=2};
278
279#define STARTED_RU      2
280#define STARTED_CU      1
281
282/*
283 * Primitive hardware access functions.
284 */
285
286static inline unsigned short scb_status(struct net_device *dev)
287{
288	return inw(dev->base_addr + 0xc008);
289}
290
291static inline unsigned short scb_rdcmd(struct net_device *dev)
292{
293	return inw(dev->base_addr + 0xc00a);
294}
295
296static inline void scb_command(struct net_device *dev, unsigned short cmd)
297{
298	outw(cmd, dev->base_addr + 0xc00a);
299}
300
301static inline void scb_wrcbl(struct net_device *dev, unsigned short val)
302{
303	outw(val, dev->base_addr + 0xc00c);
304}
305
306static inline void scb_wrrfa(struct net_device *dev, unsigned short val)
307{
308	outw(val, dev->base_addr + 0xc00e);
309}
310
311static inline void set_loopback(struct net_device *dev)
312{
313	outb(inb(dev->base_addr + Config) | 2, dev->base_addr + Config);
314}
315
316static inline void clear_loopback(struct net_device *dev)
317{
318	outb(inb(dev->base_addr + Config) & ~2, dev->base_addr + Config);
319}
320
321static inline unsigned short int SHADOW(short int addr)
322{
323	addr &= 0x1f;
324	if (addr > 0xf) addr += 0x3ff0;
325	return addr + 0x4000;
326}
327
328/*
329 * Linux interface
330 */
331
332/*
333 * checks for presence of EtherExpress card
334 */
335
336static int __init do_express_probe(struct net_device *dev)
337{
338	unsigned short *port;
339	static unsigned short ports[] = { 0x240,0x300,0x310,0x270,0x320,0x340,0 };
340	unsigned short ioaddr = dev->base_addr;
341	int dev_irq = dev->irq;
342	int err;
343
344	SET_MODULE_OWNER(dev);
345
346	dev->if_port = 0xff; /* not set */
347
348#ifdef CONFIG_MCA_LEGACY
349	if (MCA_bus) {
350		int slot = 0;
351
352		/*
353		 * Only find one card at a time.  Subsequent calls
354		 * will find others, however, proper multicard MCA
355		 * probing and setup can't be done with the
356		 * old-style Space.c init routines.  -- ASF
357		 */
358		while (slot != MCA_NOTFOUND) {
359			int pos0, pos1;
360
361			slot = mca_find_unused_adapter(0x628B, slot);
362			if (slot == MCA_NOTFOUND)
363				break;
364
365			pos0 = mca_read_stored_pos(slot, 2);
366			pos1 = mca_read_stored_pos(slot, 3);
367			ioaddr = mca_iomap[pos1&0xf];
368
369			dev->irq = mca_irqmap[(pos1>>4)&0x7];
370
371			if ((pos0 & 0x7) == 0x1)
372				dev->if_port = AUI;
373			else if ((pos0 & 0x7) == 0x5) {
374				if (pos1 & 0x80)
375					dev->if_port = BNC;
376				else
377					dev->if_port = TPE;
378			}
379
380			mca_set_adapter_name(slot, "Intel EtherExpress 16 MCA");
381			mca_set_adapter_procfn(slot, NULL, dev);
382			mca_mark_as_used(slot);
383
384			break;
385		}
386	}
387#endif
388	if (ioaddr&0xfe00) {
389		if (!request_region(ioaddr, EEXP_IO_EXTENT, "EtherExpress"))
390			return -EBUSY;
391		err = eexp_hw_probe(dev,ioaddr);
392		release_region(ioaddr, EEXP_IO_EXTENT);
393		return err;
394	} else if (ioaddr)
395		return -ENXIO;
396
397	for (port=&ports[0] ; *port ; port++ )
398	{
399		unsigned short sum = 0;
400		int i;
401		if (!request_region(*port, EEXP_IO_EXTENT, "EtherExpress"))
402			continue;
403		for ( i=0 ; i<4 ; i++ )
404		{
405			unsigned short t;
406			t = inb(*port + ID_PORT);
407			sum |= (t>>4) << ((t & 0x03)<<2);
408		}
409		if (sum==0xbaba && !eexp_hw_probe(dev,*port)) {
410			release_region(*port, EEXP_IO_EXTENT);
411			return 0;
412		}
413		release_region(*port, EEXP_IO_EXTENT);
414		dev->irq = dev_irq;
415	}
416	return -ENODEV;
417}
418
419#ifndef MODULE
420struct net_device * __init express_probe(int unit)
421{
422	struct net_device *dev = alloc_etherdev(sizeof(struct net_local));
423	int err;
424
425	if (!dev)
426		return ERR_PTR(-ENOMEM);
427
428	sprintf(dev->name, "eth%d", unit);
429	netdev_boot_setup_check(dev);
430
431	err = do_express_probe(dev);
432	if (!err)
433		return dev;
434	free_netdev(dev);
435	return ERR_PTR(err);
436}
437#endif
438
439/*
440 * open and initialize the adapter, ready for use
441 */
442
443static int eexp_open(struct net_device *dev)
444{
445	int ret;
446	unsigned short ioaddr = dev->base_addr;
447	struct net_local *lp = netdev_priv(dev);
448
449#if NET_DEBUG > 6
450	printk(KERN_DEBUG "%s: eexp_open()\n", dev->name);
451#endif
452
453	if (!dev->irq || !irqrmap[dev->irq])
454		return -ENXIO;
455
456	ret = request_irq(dev->irq,&eexp_irq,0,dev->name,dev);
457	if (ret) return ret;
458
459	if (!request_region(ioaddr, EEXP_IO_EXTENT, "EtherExpress")) {
460		printk(KERN_WARNING "EtherExpress io port %x, is busy.\n"
461			, ioaddr);
462		goto err_out1;
463	}
464	if (!request_region(ioaddr+0x4000, EEXP_IO_EXTENT, "EtherExpress shadow")) {
465		printk(KERN_WARNING "EtherExpress io port %x, is busy.\n"
466			, ioaddr+0x4000);
467		goto err_out2;
468	}
469	if (!request_region(ioaddr+0x8000, EEXP_IO_EXTENT, "EtherExpress shadow")) {
470		printk(KERN_WARNING "EtherExpress io port %x, is busy.\n"
471			, ioaddr+0x8000);
472		goto err_out3;
473	}
474	if (!request_region(ioaddr+0xc000, EEXP_IO_EXTENT, "EtherExpress shadow")) {
475		printk(KERN_WARNING "EtherExpress io port %x, is busy.\n"
476			, ioaddr+0xc000);
477		goto err_out4;
478	}
479
480	if (lp->width) {
481		printk("%s: forcing ASIC to 8-bit mode\n", dev->name);
482		outb(inb(dev->base_addr+Config)&~4, dev->base_addr+Config);
483	}
484
485	eexp_hw_init586(dev);
486	netif_start_queue(dev);
487#if NET_DEBUG > 6
488	printk(KERN_DEBUG "%s: leaving eexp_open()\n", dev->name);
489#endif
490	return 0;
491
492	err_out4:
493		release_region(ioaddr+0x8000, EEXP_IO_EXTENT);
494	err_out3:
495		release_region(ioaddr+0x4000, EEXP_IO_EXTENT);
496	err_out2:
497		release_region(ioaddr, EEXP_IO_EXTENT);
498	err_out1:
499		free_irq(dev->irq, dev);
500		return -EBUSY;
501}
502
503/*
504 * close and disable the interface, leaving the 586 in reset.
505 */
506
507static int eexp_close(struct net_device *dev)
508{
509	unsigned short ioaddr = dev->base_addr;
510	struct net_local *lp = netdev_priv(dev);
511
512	int irq = dev->irq;
513
514	netif_stop_queue(dev);
515
516	outb(SIRQ_dis|irqrmap[irq],ioaddr+SET_IRQ);
517	lp->started = 0;
518	scb_command(dev, SCB_CUsuspend|SCB_RUsuspend);
519	outb(0,ioaddr+SIGNAL_CA);
520	free_irq(irq,dev);
521	outb(i586_RST,ioaddr+EEPROM_Ctrl);
522	release_region(ioaddr, EEXP_IO_EXTENT);
523	release_region(ioaddr+0x4000, 16);
524	release_region(ioaddr+0x8000, 16);
525	release_region(ioaddr+0xc000, 16);
526
527	return 0;
528}
529
530/*
531 * Return interface stats
532 */
533
534static struct net_device_stats *eexp_stats(struct net_device *dev)
535{
536	struct net_local *lp = netdev_priv(dev);
537
538	return &lp->stats;
539}
540
541/*
542 * This gets called when a higher level thinks we are broken.  Check that
543 * nothing has become jammed in the CU.
544 */
545
546static void unstick_cu(struct net_device *dev)
547{
548	struct net_local *lp = netdev_priv(dev);
549	unsigned short ioaddr = dev->base_addr;
550
551	if (lp->started)
552	{
553		if (time_after(jiffies, dev->trans_start + 50))
554		{
555			if (lp->tx_link==lp->last_tx_restart)
556			{
557				unsigned short boguscount=200,rsst;
558				printk(KERN_WARNING "%s: Retransmit timed out, status %04x, resetting...\n",
559				       dev->name, scb_status(dev));
560				eexp_hw_txinit(dev);
561				lp->last_tx_restart = 0;
562				scb_wrcbl(dev, lp->tx_link);
563				scb_command(dev, SCB_CUstart);
564				outb(0,ioaddr+SIGNAL_CA);
565				while (!SCB_complete(rsst=scb_status(dev)))
566				{
567					if (!--boguscount)
568					{
569						boguscount=200;
570						printk(KERN_WARNING "%s: Reset timed out status %04x, retrying...\n",
571						       dev->name,rsst);
572						scb_wrcbl(dev, lp->tx_link);
573						scb_command(dev, SCB_CUstart);
574						outb(0,ioaddr+SIGNAL_CA);
575					}
576				}
577				netif_wake_queue(dev);
578			}
579			else
580			{
581				unsigned short status = scb_status(dev);
582				if (SCB_CUdead(status))
583				{
584					unsigned short txstatus = eexp_hw_lasttxstat(dev);
585					printk(KERN_WARNING "%s: Transmit timed out, CU not active status %04x %04x, restarting...\n",
586					       dev->name, status, txstatus);
587					eexp_hw_txrestart(dev);
588				}
589				else
590				{
591					unsigned short txstatus = eexp_hw_lasttxstat(dev);
592					if (netif_queue_stopped(dev) && !txstatus)
593					{
594						printk(KERN_WARNING "%s: CU wedged, status %04x %04x, resetting...\n",
595						       dev->name,status,txstatus);
596						eexp_hw_init586(dev);
597						netif_wake_queue(dev);
598					}
599					else
600					{
601						printk(KERN_WARNING "%s: transmit timed out\n", dev->name);
602					}
603				}
604			}
605		}
606	}
607	else
608	{
609		if (time_after(jiffies, lp->init_time + 10))
610		{
611			unsigned short status = scb_status(dev);
612			printk(KERN_WARNING "%s: i82586 startup timed out, status %04x, resetting...\n",
613			       dev->name, status);
614			eexp_hw_init586(dev);
615			netif_wake_queue(dev);
616		}
617	}
618}
619
620static void eexp_timeout(struct net_device *dev)
621{
622	struct net_local *lp = netdev_priv(dev);
623#ifdef CONFIG_SMP
624	unsigned long flags;
625#endif
626	int status;
627
628	disable_irq(dev->irq);
629
630	/*
631	 *	Best would be to use synchronize_irq(); spin_lock() here
632	 *	lets make it work first..
633	 */
634
635#ifdef CONFIG_SMP
636	spin_lock_irqsave(&lp->lock, flags);
637#endif
638
639	status = scb_status(dev);
640	unstick_cu(dev);
641	printk(KERN_INFO "%s: transmit timed out, %s?\n", dev->name,
642	       (SCB_complete(status)?"lost interrupt":
643		"board on fire"));
644	lp->stats.tx_errors++;
645	lp->last_tx = jiffies;
646	if (!SCB_complete(status)) {
647		scb_command(dev, SCB_CUabort);
648		outb(0,dev->base_addr+SIGNAL_CA);
649	}
650	netif_wake_queue(dev);
651#ifdef CONFIG_SMP
652	spin_unlock_irqrestore(&lp->lock, flags);
653#endif
654}
655
656/*
657 * Called to transmit a packet, or to allow us to right ourselves
658 * if the kernel thinks we've died.
659 */
660static int eexp_xmit(struct sk_buff *buf, struct net_device *dev)
661{
662	struct net_local *lp = netdev_priv(dev);
663	short length = buf->len;
664#ifdef CONFIG_SMP
665	unsigned long flags;
666#endif
667
668#if NET_DEBUG > 6
669	printk(KERN_DEBUG "%s: eexp_xmit()\n", dev->name);
670#endif
671
672	if (buf->len < ETH_ZLEN) {
673		if (skb_padto(buf, ETH_ZLEN))
674			return 0;
675		length = ETH_ZLEN;
676	}
677
678	disable_irq(dev->irq);
679
680	/*
681	 *	Best would be to use synchronize_irq(); spin_lock() here
682	 *	lets make it work first..
683	 */
684
685#ifdef CONFIG_SMP
686	spin_lock_irqsave(&lp->lock, flags);
687#endif
688
689	{
690		unsigned short *data = (unsigned short *)buf->data;
691
692		lp->stats.tx_bytes += length;
693
694	        eexp_hw_tx_pio(dev,data,length);
695	}
696	dev_kfree_skb(buf);
697#ifdef CONFIG_SMP
698	spin_unlock_irqrestore(&lp->lock, flags);
699#endif
700	enable_irq(dev->irq);
701	return 0;
702}
703
704/*
705 * Handle an EtherExpress interrupt
706 * If we've finished initializing, start the RU and CU up.
707 * If we've already started, reap tx buffers, handle any received packets,
708 * check to make sure we've not become wedged.
709 */
710
711static unsigned short eexp_start_irq(struct net_device *dev,
712				     unsigned short status)
713{
714	unsigned short ack_cmd = SCB_ack(status);
715	struct net_local *lp = netdev_priv(dev);
716	unsigned short ioaddr = dev->base_addr;
717	if ((dev->flags & IFF_UP) && !(lp->started & STARTED_CU)) {
718		short diag_status, tdr_status;
719		while (SCB_CUstat(status)==2)
720			status = scb_status(dev);
721#if NET_DEBUG > 4
722		printk("%s: CU went non-active (status %04x)\n",
723		       dev->name, status);
724#endif
725
726		outw(CONF_DIAG_RESULT & ~31, ioaddr + SM_PTR);
727		diag_status = inw(ioaddr + SHADOW(CONF_DIAG_RESULT));
728		if (diag_status & 1<<11) {
729			printk(KERN_WARNING "%s: 82586 failed self-test\n",
730			       dev->name);
731		} else if (!(diag_status & 1<<13)) {
732			printk(KERN_WARNING "%s: 82586 self-test failed to complete\n", dev->name);
733		}
734
735		outw(CONF_TDR_RESULT & ~31, ioaddr + SM_PTR);
736		tdr_status = inw(ioaddr + SHADOW(CONF_TDR_RESULT));
737		if (tdr_status & (TDR_SHORT|TDR_OPEN)) {
738			printk(KERN_WARNING "%s: TDR reports cable %s at %d tick%s\n", dev->name, (tdr_status & TDR_SHORT)?"short":"broken", tdr_status & TDR_TIME, ((tdr_status & TDR_TIME) != 1) ? "s" : "");
739		}
740		else if (tdr_status & TDR_XCVRPROBLEM) {
741			printk(KERN_WARNING "%s: TDR reports transceiver problem\n", dev->name);
742		}
743		else if (tdr_status & TDR_LINKOK) {
744#if NET_DEBUG > 4
745			printk(KERN_DEBUG "%s: TDR reports link OK\n", dev->name);
746#endif
747		} else {
748			printk("%s: TDR is ga-ga (status %04x)\n", dev->name,
749			       tdr_status);
750		}
751
752		lp->started |= STARTED_CU;
753		scb_wrcbl(dev, lp->tx_link);
754		/* if the RU isn't running, start it now */
755		if (!(lp->started & STARTED_RU)) {
756			ack_cmd |= SCB_RUstart;
757			scb_wrrfa(dev, lp->rx_buf_start);
758			lp->rx_ptr = lp->rx_buf_start;
759			lp->started |= STARTED_RU;
760		}
761		ack_cmd |= SCB_CUstart | 0x2000;
762	}
763
764	if ((dev->flags & IFF_UP) && !(lp->started & STARTED_RU) && SCB_RUstat(status)==4)
765		lp->started|=STARTED_RU;
766
767	return ack_cmd;
768}
769
770static void eexp_cmd_clear(struct net_device *dev)
771{
772	unsigned long int oldtime = jiffies;
773	while (scb_rdcmd(dev) && (time_before(jiffies, oldtime + 10)));
774	if (scb_rdcmd(dev)) {
775		printk("%s: command didn't clear\n", dev->name);
776	}
777}
778
779static irqreturn_t eexp_irq(int irq, void *dev_info)
780{
781	struct net_device *dev = dev_info;
782	struct net_local *lp;
783	unsigned short ioaddr,status,ack_cmd;
784	unsigned short old_read_ptr, old_write_ptr;
785
786	lp = netdev_priv(dev);
787	ioaddr = dev->base_addr;
788
789	spin_lock(&lp->lock);
790
791	old_read_ptr = inw(ioaddr+READ_PTR);
792	old_write_ptr = inw(ioaddr+WRITE_PTR);
793
794	outb(SIRQ_dis|irqrmap[irq],ioaddr+SET_IRQ);
795
796
797	status = scb_status(dev);
798
799#if NET_DEBUG > 4
800	printk(KERN_DEBUG "%s: interrupt (status %x)\n", dev->name, status);
801#endif
802
803	if (lp->started == (STARTED_CU | STARTED_RU)) {
804
805		do {
806			eexp_cmd_clear(dev);
807
808			ack_cmd = SCB_ack(status);
809			scb_command(dev, ack_cmd);
810			outb(0,ioaddr+SIGNAL_CA);
811
812			eexp_cmd_clear(dev);
813
814			if (SCB_complete(status)) {
815				if (!eexp_hw_lasttxstat(dev)) {
816					printk("%s: tx interrupt but no status\n", dev->name);
817				}
818			}
819
820			if (SCB_rxdframe(status))
821				eexp_hw_rx_pio(dev);
822
823			status = scb_status(dev);
824		} while (status & 0xc000);
825
826		if (SCB_RUdead(status))
827		{
828			printk(KERN_WARNING "%s: RU stopped: status %04x\n",
829			       dev->name,status);
830			lp->stats.rx_errors++;
831		        eexp_hw_rxinit(dev);
832			scb_wrrfa(dev, lp->rx_buf_start);
833			scb_command(dev, SCB_RUstart);
834			outb(0,ioaddr+SIGNAL_CA);
835		}
836	} else {
837		if (status & 0x8000)
838			ack_cmd = eexp_start_irq(dev, status);
839		else
840			ack_cmd = SCB_ack(status);
841		scb_command(dev, ack_cmd);
842		outb(0,ioaddr+SIGNAL_CA);
843	}
844
845	eexp_cmd_clear(dev);
846
847	outb(SIRQ_en|irqrmap[irq],ioaddr+SET_IRQ);
848
849#if NET_DEBUG > 6
850	printk("%s: leaving eexp_irq()\n", dev->name);
851#endif
852	outw(old_read_ptr, ioaddr+READ_PTR);
853	outw(old_write_ptr, ioaddr+WRITE_PTR);
854
855	spin_unlock(&lp->lock);
856	return IRQ_HANDLED;
857}
858
859/*
860 * Hardware access functions
861 */
862
863/*
864 * Set the cable type to use.
865 */
866
867static void eexp_hw_set_interface(struct net_device *dev)
868{
869	unsigned char oldval = inb(dev->base_addr + 0x300e);
870	oldval &= ~0x82;
871	switch (dev->if_port) {
872	case TPE:
873		oldval |= 0x2;
874	case BNC:
875		oldval |= 0x80;
876		break;
877	}
878	outb(oldval, dev->base_addr+0x300e);
879	mdelay(20);
880}
881
882/*
883 * Check all the receive buffers, and hand any received packets
884 * to the upper levels. Basic sanity check on each frame
885 * descriptor, though we don't bother trying to fix broken ones.
886 */
887
888static void eexp_hw_rx_pio(struct net_device *dev)
889{
890	struct net_local *lp = netdev_priv(dev);
891	unsigned short rx_block = lp->rx_ptr;
892	unsigned short boguscount = lp->num_rx_bufs;
893	unsigned short ioaddr = dev->base_addr;
894	unsigned short status;
895
896#if NET_DEBUG > 6
897	printk(KERN_DEBUG "%s: eexp_hw_rx()\n", dev->name);
898#endif
899
900 	do {
901 		unsigned short rfd_cmd, rx_next, pbuf, pkt_len;
902
903		outw(rx_block, ioaddr + READ_PTR);
904		status = inw(ioaddr + DATAPORT);
905
906		if (FD_Done(status))
907		{
908			rfd_cmd = inw(ioaddr + DATAPORT);
909			rx_next = inw(ioaddr + DATAPORT);
910			pbuf = inw(ioaddr + DATAPORT);
911
912			outw(pbuf, ioaddr + READ_PTR);
913			pkt_len = inw(ioaddr + DATAPORT);
914
915			if (rfd_cmd!=0x0000)
916  			{
917				printk(KERN_WARNING "%s: rfd_cmd not zero:0x%04x\n",
918				       dev->name, rfd_cmd);
919				continue;
920			}
921			else if (pbuf!=rx_block+0x16)
922			{
923				printk(KERN_WARNING "%s: rfd and rbd out of sync 0x%04x 0x%04x\n",
924				       dev->name, rx_block+0x16, pbuf);
925				continue;
926			}
927			else if ((pkt_len & 0xc000)!=0xc000)
928			{
929				printk(KERN_WARNING "%s: EOF or F not set on received buffer (%04x)\n",
930				       dev->name, pkt_len & 0xc000);
931  				continue;
932  			}
933  			else if (!FD_OK(status))
934			{
935				lp->stats.rx_errors++;
936				if (FD_CRC(status))
937					lp->stats.rx_crc_errors++;
938				if (FD_Align(status))
939					lp->stats.rx_frame_errors++;
940				if (FD_Resrc(status))
941					lp->stats.rx_fifo_errors++;
942				if (FD_DMA(status))
943					lp->stats.rx_over_errors++;
944				if (FD_Short(status))
945					lp->stats.rx_length_errors++;
946			}
947			else
948			{
949				struct sk_buff *skb;
950				pkt_len &= 0x3fff;
951				skb = dev_alloc_skb(pkt_len+16);
952				if (skb == NULL)
953				{
954					printk(KERN_WARNING "%s: Memory squeeze, dropping packet\n",dev->name);
955					lp->stats.rx_dropped++;
956					break;
957				}
958				skb_reserve(skb, 2);
959				outw(pbuf+10, ioaddr+READ_PTR);
960			        insw(ioaddr+DATAPORT, skb_put(skb,pkt_len),(pkt_len+1)>>1);
961				skb->protocol = eth_type_trans(skb,dev);
962				netif_rx(skb);
963				dev->last_rx = jiffies;
964				lp->stats.rx_packets++;
965				lp->stats.rx_bytes += pkt_len;
966			}
967			outw(rx_block, ioaddr+WRITE_PTR);
968			outw(0, ioaddr+DATAPORT);
969			outw(0, ioaddr+DATAPORT);
970			rx_block = rx_next;
971		}
972	} while (FD_Done(status) && boguscount--);
973	lp->rx_ptr = rx_block;
974}
975
976/*
977 * Hand a packet to the card for transmission
978 * If we get here, we MUST have already checked
979 * to make sure there is room in the transmit
980 * buffer region.
981 */
982
983static void eexp_hw_tx_pio(struct net_device *dev, unsigned short *buf,
984		       unsigned short len)
985{
986	struct net_local *lp = netdev_priv(dev);
987	unsigned short ioaddr = dev->base_addr;
988
989	if (LOCKUP16 || lp->width) {
990		/* Stop the CU so that there is no chance that it
991		   jumps off to a bogus address while we are writing the
992		   pointer to the next transmit packet in 8-bit mode --
993		   this eliminates the "CU wedged" errors in 8-bit mode.
994		   (Zoltan Szilagyi 10-12-96) */
995		scb_command(dev, SCB_CUsuspend);
996		outw(0xFFFF, ioaddr+SIGNAL_CA);
997	}
998
999 	outw(lp->tx_head, ioaddr + WRITE_PTR);
1000
1001	outw(0x0000, ioaddr + DATAPORT);
1002        outw(Cmd_INT|Cmd_Xmit, ioaddr + DATAPORT);
1003	outw(lp->tx_head+0x08, ioaddr + DATAPORT);
1004	outw(lp->tx_head+0x0e, ioaddr + DATAPORT);
1005
1006	outw(0x0000, ioaddr + DATAPORT);
1007	outw(0x0000, ioaddr + DATAPORT);
1008	outw(lp->tx_head+0x08, ioaddr + DATAPORT);
1009
1010	outw(0x8000|len, ioaddr + DATAPORT);
1011	outw(-1, ioaddr + DATAPORT);
1012	outw(lp->tx_head+0x16, ioaddr + DATAPORT);
1013	outw(0, ioaddr + DATAPORT);
1014
1015        outsw(ioaddr + DATAPORT, buf, (len+1)>>1);
1016
1017	outw(lp->tx_tail+0xc, ioaddr + WRITE_PTR);
1018	outw(lp->tx_head, ioaddr + DATAPORT);
1019
1020	dev->trans_start = jiffies;
1021	lp->tx_tail = lp->tx_head;
1022	if (lp->tx_head==TX_BUF_START+((lp->num_tx_bufs-1)*TX_BUF_SIZE))
1023		lp->tx_head = TX_BUF_START;
1024	else
1025		lp->tx_head += TX_BUF_SIZE;
1026	if (lp->tx_head != lp->tx_reap)
1027		netif_wake_queue(dev);
1028
1029	if (LOCKUP16 || lp->width) {
1030		/* Restart the CU so that the packet can actually
1031		   be transmitted. (Zoltan Szilagyi 10-12-96) */
1032		scb_command(dev, SCB_CUresume);
1033		outw(0xFFFF, ioaddr+SIGNAL_CA);
1034	}
1035
1036	lp->stats.tx_packets++;
1037	lp->last_tx = jiffies;
1038}
1039
1040/*
1041 * Sanity check the suspected EtherExpress card
1042 * Read hardware address, reset card, size memory and initialize buffer
1043 * memory pointers. These are held in dev->priv, in case someone has more
1044 * than one card in a machine.
1045 */
1046
1047static int __init eexp_hw_probe(struct net_device *dev, unsigned short ioaddr)
1048{
1049	unsigned short hw_addr[3];
1050	unsigned char buswidth;
1051	unsigned int memory_size;
1052	int i;
1053	unsigned short xsum = 0;
1054	struct net_local *lp = netdev_priv(dev);
1055
1056	printk("%s: EtherExpress 16 at %#x ",dev->name,ioaddr);
1057
1058	outb(ASIC_RST, ioaddr+EEPROM_Ctrl);
1059	outb(0, ioaddr+EEPROM_Ctrl);
1060	udelay(500);
1061	outb(i586_RST, ioaddr+EEPROM_Ctrl);
1062
1063	hw_addr[0] = eexp_hw_readeeprom(ioaddr,2);
1064	hw_addr[1] = eexp_hw_readeeprom(ioaddr,3);
1065	hw_addr[2] = eexp_hw_readeeprom(ioaddr,4);
1066
1067	/* Standard Address or Compaq LTE Address */
1068	if (!((hw_addr[2]==0x00aa && ((hw_addr[1] & 0xff00)==0x0000)) ||
1069	      (hw_addr[2]==0x0080 && ((hw_addr[1] & 0xff00)==0x5F00))))
1070	{
1071		printk(" rejected: invalid address %04x%04x%04x\n",
1072			hw_addr[2],hw_addr[1],hw_addr[0]);
1073		return -ENODEV;
1074	}
1075
1076	/* Calculate the EEPROM checksum.  Carry on anyway if it's bad,
1077	 * though.
1078	 */
1079	for (i = 0; i < 64; i++)
1080		xsum += eexp_hw_readeeprom(ioaddr, i);
1081	if (xsum != 0xbaba)
1082		printk(" (bad EEPROM xsum 0x%02x)", xsum);
1083
1084	dev->base_addr = ioaddr;
1085	for ( i=0 ; i<6 ; i++ )
1086		dev->dev_addr[i] = ((unsigned char *)hw_addr)[5-i];
1087
1088	{
1089		static char irqmap[]={0, 9, 3, 4, 5, 10, 11, 0};
1090		unsigned short setupval = eexp_hw_readeeprom(ioaddr,0);
1091
1092		/* Use the IRQ from EEPROM if none was given */
1093		if (!dev->irq)
1094			dev->irq = irqmap[setupval>>13];
1095
1096		if (dev->if_port == 0xff) {
1097			dev->if_port = !(setupval & 0x1000) ? AUI :
1098				eexp_hw_readeeprom(ioaddr,5) & 0x1 ? TPE : BNC;
1099		}
1100
1101		buswidth = !((setupval & 0x400) >> 10);
1102	}
1103
1104	memset(lp, 0, sizeof(struct net_local));
1105	spin_lock_init(&lp->lock);
1106
1107 	printk("(IRQ %d, %s connector, %d-bit bus", dev->irq,
1108 	       eexp_ifmap[dev->if_port], buswidth?8:16);
1109
1110	if (!request_region(dev->base_addr + 0x300e, 1, "EtherExpress"))
1111		return -EBUSY;
1112
1113 	eexp_hw_set_interface(dev);
1114
1115	release_region(dev->base_addr + 0x300e, 1);
1116
1117	/* Find out how much RAM we have on the card */
1118	outw(0, dev->base_addr + WRITE_PTR);
1119	for (i = 0; i < 32768; i++)
1120		outw(0, dev->base_addr + DATAPORT);
1121
1122        for (memory_size = 0; memory_size < 64; memory_size++)
1123	{
1124		outw(memory_size<<10, dev->base_addr + READ_PTR);
1125		if (inw(dev->base_addr+DATAPORT))
1126			break;
1127		outw(memory_size<<10, dev->base_addr + WRITE_PTR);
1128		outw(memory_size | 0x5000, dev->base_addr+DATAPORT);
1129		outw(memory_size<<10, dev->base_addr + READ_PTR);
1130		if (inw(dev->base_addr+DATAPORT) != (memory_size | 0x5000))
1131			break;
1132	}
1133
1134	/* Sort out the number of buffers.  We may have 16, 32, 48 or 64k
1135	 * of RAM to play with.
1136	 */
1137	lp->num_tx_bufs = 4;
1138	lp->rx_buf_end = 0x3ff6;
1139	switch (memory_size)
1140	{
1141	case 64:
1142		lp->rx_buf_end += 0x4000;
1143	case 48:
1144		lp->num_tx_bufs += 4;
1145		lp->rx_buf_end += 0x4000;
1146	case 32:
1147		lp->rx_buf_end += 0x4000;
1148	case 16:
1149		printk(", %dk RAM)\n", memory_size);
1150		break;
1151	default:
1152		printk(") bad memory size (%dk).\n", memory_size);
1153		return -ENODEV;
1154		break;
1155	}
1156
1157	lp->rx_buf_start = TX_BUF_START + (lp->num_tx_bufs*TX_BUF_SIZE);
1158	lp->width = buswidth;
1159
1160	dev->open = eexp_open;
1161	dev->stop = eexp_close;
1162	dev->hard_start_xmit = eexp_xmit;
1163	dev->get_stats = eexp_stats;
1164	dev->set_multicast_list = &eexp_set_multicast;
1165	dev->tx_timeout = eexp_timeout;
1166	dev->watchdog_timeo = 2*HZ;
1167
1168	return register_netdev(dev);
1169}
1170
1171/*
1172 * Read a word from the EtherExpress on-board serial EEPROM.
1173 * The EEPROM contains 64 words of 16 bits.
1174 */
1175static unsigned short __init eexp_hw_readeeprom(unsigned short ioaddr,
1176						    unsigned char location)
1177{
1178	unsigned short cmd = 0x180|(location&0x7f);
1179	unsigned short rval = 0,wval = EC_CS|i586_RST;
1180	int i;
1181
1182	outb(EC_CS|i586_RST,ioaddr+EEPROM_Ctrl);
1183	for (i=0x100 ; i ; i>>=1 )
1184	{
1185		if (cmd&i)
1186			wval |= EC_Wr;
1187		else
1188			wval &= ~EC_Wr;
1189
1190		outb(wval,ioaddr+EEPROM_Ctrl);
1191		outb(wval|EC_Clk,ioaddr+EEPROM_Ctrl);
1192		eeprom_delay();
1193		outb(wval,ioaddr+EEPROM_Ctrl);
1194		eeprom_delay();
1195	}
1196	wval &= ~EC_Wr;
1197	outb(wval,ioaddr+EEPROM_Ctrl);
1198	for (i=0x8000 ; i ; i>>=1 )
1199	{
1200		outb(wval|EC_Clk,ioaddr+EEPROM_Ctrl);
1201		eeprom_delay();
1202		if (inb(ioaddr+EEPROM_Ctrl)&EC_Rd)
1203			rval |= i;
1204		outb(wval,ioaddr+EEPROM_Ctrl);
1205		eeprom_delay();
1206	}
1207	wval &= ~EC_CS;
1208	outb(wval|EC_Clk,ioaddr+EEPROM_Ctrl);
1209	eeprom_delay();
1210	outb(wval,ioaddr+EEPROM_Ctrl);
1211	eeprom_delay();
1212	return rval;
1213}
1214
1215/*
1216 * Reap tx buffers and return last transmit status.
1217 * if ==0 then either:
1218 *    a) we're not transmitting anything, so why are we here?
1219 *    b) we've died.
1220 * otherwise, Stat_Busy(return) means we've still got some packets
1221 * to transmit, Stat_Done(return) means our buffers should be empty
1222 * again
1223 */
1224
1225static unsigned short eexp_hw_lasttxstat(struct net_device *dev)
1226{
1227	struct net_local *lp = netdev_priv(dev);
1228	unsigned short tx_block = lp->tx_reap;
1229	unsigned short status;
1230
1231	if (!netif_queue_stopped(dev) && lp->tx_head==lp->tx_reap)
1232		return 0x0000;
1233
1234	do
1235	{
1236		outw(tx_block & ~31, dev->base_addr + SM_PTR);
1237		status = inw(dev->base_addr + SHADOW(tx_block));
1238		if (!Stat_Done(status))
1239		{
1240			lp->tx_link = tx_block;
1241			return status;
1242		}
1243		else
1244		{
1245			lp->last_tx_restart = 0;
1246			lp->stats.collisions += Stat_NoColl(status);
1247			if (!Stat_OK(status))
1248			{
1249				char *whatsup = NULL;
1250				lp->stats.tx_errors++;
1251  				if (Stat_Abort(status))
1252  					lp->stats.tx_aborted_errors++;
1253				if (Stat_TNoCar(status)) {
1254					whatsup = "aborted, no carrier";
1255					lp->stats.tx_carrier_errors++;
1256				}
1257				if (Stat_TNoCTS(status)) {
1258					whatsup = "aborted, lost CTS";
1259  					lp->stats.tx_carrier_errors++;
1260				}
1261				if (Stat_TNoDMA(status)) {
1262					whatsup = "FIFO underran";
1263  					lp->stats.tx_fifo_errors++;
1264				}
1265				if (Stat_TXColl(status)) {
1266					whatsup = "aborted, too many collisions";
1267					lp->stats.tx_aborted_errors++;
1268				}
1269				if (whatsup)
1270					printk(KERN_INFO "%s: transmit %s\n",
1271					       dev->name, whatsup);
1272			}
1273			else
1274				lp->stats.tx_packets++;
1275		}
1276		if (tx_block == TX_BUF_START+((lp->num_tx_bufs-1)*TX_BUF_SIZE))
1277			lp->tx_reap = tx_block = TX_BUF_START;
1278		else
1279			lp->tx_reap = tx_block += TX_BUF_SIZE;
1280		netif_wake_queue(dev);
1281	}
1282	while (lp->tx_reap != lp->tx_head);
1283
1284	lp->tx_link = lp->tx_tail + 0x08;
1285
1286	return status;
1287}
1288
1289/*
1290 * This should never happen. It is called when some higher routine detects
1291 * that the CU has stopped, to try to restart it from the last packet we knew
1292 * we were working on, or the idle loop if we had finished for the time.
1293 */
1294
1295static void eexp_hw_txrestart(struct net_device *dev)
1296{
1297	struct net_local *lp = netdev_priv(dev);
1298	unsigned short ioaddr = dev->base_addr;
1299
1300	lp->last_tx_restart = lp->tx_link;
1301	scb_wrcbl(dev, lp->tx_link);
1302	scb_command(dev, SCB_CUstart);
1303	outb(0,ioaddr+SIGNAL_CA);
1304
1305	{
1306		unsigned short boguscount=50,failcount=5;
1307		while (!scb_status(dev))
1308		{
1309			if (!--boguscount)
1310			{
1311				if (--failcount)
1312				{
1313					printk(KERN_WARNING "%s: CU start timed out, status %04x, cmd %04x\n", dev->name, scb_status(dev), scb_rdcmd(dev));
1314				        scb_wrcbl(dev, lp->tx_link);
1315					scb_command(dev, SCB_CUstart);
1316					outb(0,ioaddr+SIGNAL_CA);
1317					boguscount = 100;
1318				}
1319				else
1320				{
1321					printk(KERN_WARNING "%s: Failed to restart CU, resetting board...\n",dev->name);
1322					eexp_hw_init586(dev);
1323					netif_wake_queue(dev);
1324					return;
1325				}
1326			}
1327		}
1328	}
1329}
1330
1331/*
1332 * Writes down the list of transmit buffers into card memory.  Each
1333 * entry consists of an 82586 transmit command, followed by a jump
1334 * pointing to itself.  When we want to transmit a packet, we write
1335 * the data into the appropriate transmit buffer and then modify the
1336 * preceding jump to point at the new transmit command.  This means that
1337 * the 586 command unit is continuously active.
1338 */
1339
1340static void eexp_hw_txinit(struct net_device *dev)
1341{
1342	struct net_local *lp = netdev_priv(dev);
1343	unsigned short tx_block = TX_BUF_START;
1344	unsigned short curtbuf;
1345	unsigned short ioaddr = dev->base_addr;
1346
1347	for ( curtbuf=0 ; curtbuf<lp->num_tx_bufs ; curtbuf++ )
1348	{
1349		outw(tx_block, ioaddr + WRITE_PTR);
1350
1351	        outw(0x0000, ioaddr + DATAPORT);
1352		outw(Cmd_INT|Cmd_Xmit, ioaddr + DATAPORT);
1353		outw(tx_block+0x08, ioaddr + DATAPORT);
1354		outw(tx_block+0x0e, ioaddr + DATAPORT);
1355
1356		outw(0x0000, ioaddr + DATAPORT);
1357		outw(0x0000, ioaddr + DATAPORT);
1358		outw(tx_block+0x08, ioaddr + DATAPORT);
1359
1360		outw(0x8000, ioaddr + DATAPORT);
1361		outw(-1, ioaddr + DATAPORT);
1362		outw(tx_block+0x16, ioaddr + DATAPORT);
1363		outw(0x0000, ioaddr + DATAPORT);
1364
1365		tx_block += TX_BUF_SIZE;
1366	}
1367	lp->tx_head = TX_BUF_START;
1368	lp->tx_reap = TX_BUF_START;
1369	lp->tx_tail = tx_block - TX_BUF_SIZE;
1370	lp->tx_link = lp->tx_tail + 0x08;
1371	lp->rx_buf_start = tx_block;
1372
1373}
1374
1375/*
1376 * Write the circular list of receive buffer descriptors to card memory.
1377 * The end of the list isn't marked, which means that the 82586 receive
1378 * unit will loop until buffers become available (this avoids it giving us
1379 * "out of resources" messages).
1380 */
1381
1382static void eexp_hw_rxinit(struct net_device *dev)
1383{
1384	struct net_local *lp = netdev_priv(dev);
1385	unsigned short rx_block = lp->rx_buf_start;
1386	unsigned short ioaddr = dev->base_addr;
1387
1388	lp->num_rx_bufs = 0;
1389	lp->rx_first = lp->rx_ptr = rx_block;
1390	do
1391	{
1392		lp->num_rx_bufs++;
1393
1394		outw(rx_block, ioaddr + WRITE_PTR);
1395
1396		outw(0, ioaddr + DATAPORT);  outw(0, ioaddr+DATAPORT);
1397		outw(rx_block + RX_BUF_SIZE, ioaddr+DATAPORT);
1398		outw(0xffff, ioaddr+DATAPORT);
1399
1400		outw(0x0000, ioaddr+DATAPORT);
1401		outw(0xdead, ioaddr+DATAPORT);
1402		outw(0xdead, ioaddr+DATAPORT);
1403		outw(0xdead, ioaddr+DATAPORT);
1404		outw(0xdead, ioaddr+DATAPORT);
1405		outw(0xdead, ioaddr+DATAPORT);
1406		outw(0xdead, ioaddr+DATAPORT);
1407
1408		outw(0x0000, ioaddr+DATAPORT);
1409		outw(rx_block + RX_BUF_SIZE + 0x16, ioaddr+DATAPORT);
1410		outw(rx_block + 0x20, ioaddr+DATAPORT);
1411		outw(0, ioaddr+DATAPORT);
1412		outw(RX_BUF_SIZE-0x20, ioaddr+DATAPORT);
1413
1414		lp->rx_last = rx_block;
1415		rx_block += RX_BUF_SIZE;
1416	} while (rx_block <= lp->rx_buf_end-RX_BUF_SIZE);
1417
1418
1419	/* Make first Rx frame descriptor point to first Rx buffer
1420           descriptor */
1421	outw(lp->rx_first + 6, ioaddr+WRITE_PTR);
1422	outw(lp->rx_first + 0x16, ioaddr+DATAPORT);
1423
1424	/* Close Rx frame descriptor ring */
1425  	outw(lp->rx_last + 4, ioaddr+WRITE_PTR);
1426  	outw(lp->rx_first, ioaddr+DATAPORT);
1427
1428	/* Close Rx buffer descriptor ring */
1429	outw(lp->rx_last + 0x16 + 2, ioaddr+WRITE_PTR);
1430	outw(lp->rx_first + 0x16, ioaddr+DATAPORT);
1431
1432}
1433
1434/*
1435 * Un-reset the 586, and start the configuration sequence. We don't wait for
1436 * this to finish, but allow the interrupt handler to start the CU and RU for
1437 * us.  We can't start the receive/transmission system up before we know that
1438 * the hardware is configured correctly.
1439 */
1440
1441static void eexp_hw_init586(struct net_device *dev)
1442{
1443	struct net_local *lp = netdev_priv(dev);
1444	unsigned short ioaddr = dev->base_addr;
1445	int i;
1446
1447#if NET_DEBUG > 6
1448	printk("%s: eexp_hw_init586()\n", dev->name);
1449#endif
1450
1451	lp->started = 0;
1452
1453	set_loopback(dev);
1454
1455	outb(SIRQ_dis|irqrmap[dev->irq],ioaddr+SET_IRQ);
1456
1457	/* Download the startup code */
1458	outw(lp->rx_buf_end & ~31, ioaddr + SM_PTR);
1459	outw(lp->width?0x0001:0x0000, ioaddr + 0x8006);
1460	outw(0x0000, ioaddr + 0x8008);
1461	outw(0x0000, ioaddr + 0x800a);
1462	outw(0x0000, ioaddr + 0x800c);
1463	outw(0x0000, ioaddr + 0x800e);
1464
1465	for (i = 0; i < (sizeof(start_code)); i+=32) {
1466		int j;
1467		outw(i, ioaddr + SM_PTR);
1468		for (j = 0; j < 16; j+=2)
1469			outw(start_code[(i+j)/2],
1470			     ioaddr+0x4000+j);
1471		for (j = 0; j < 16; j+=2)
1472			outw(start_code[(i+j+16)/2],
1473			     ioaddr+0x8000+j);
1474	}
1475
1476	/* Do we want promiscuous mode or multicast? */
1477	outw(CONF_PROMISC & ~31, ioaddr+SM_PTR);
1478	i = inw(ioaddr+SHADOW(CONF_PROMISC));
1479	outw((dev->flags & IFF_PROMISC)?(i|1):(i & ~1),
1480	     ioaddr+SHADOW(CONF_PROMISC));
1481	lp->was_promisc = dev->flags & IFF_PROMISC;
1482
1483	/* Write our hardware address */
1484	outw(CONF_HWADDR & ~31, ioaddr+SM_PTR);
1485	outw(((unsigned short *)dev->dev_addr)[0], ioaddr+SHADOW(CONF_HWADDR));
1486	outw(((unsigned short *)dev->dev_addr)[1],
1487	     ioaddr+SHADOW(CONF_HWADDR+2));
1488	outw(((unsigned short *)dev->dev_addr)[2],
1489	     ioaddr+SHADOW(CONF_HWADDR+4));
1490
1491	eexp_hw_txinit(dev);
1492	eexp_hw_rxinit(dev);
1493
1494	outb(0,ioaddr+EEPROM_Ctrl);
1495	mdelay(5);
1496
1497	scb_command(dev, 0xf000);
1498	outb(0,ioaddr+SIGNAL_CA);
1499
1500	outw(0, ioaddr+SM_PTR);
1501
1502	{
1503		unsigned short rboguscount=50,rfailcount=5;
1504		while (inw(ioaddr+0x4000))
1505		{
1506			if (!--rboguscount)
1507			{
1508				printk(KERN_WARNING "%s: i82586 reset timed out, kicking...\n",
1509					dev->name);
1510				scb_command(dev, 0);
1511				outb(0,ioaddr+SIGNAL_CA);
1512				rboguscount = 100;
1513				if (!--rfailcount)
1514				{
1515					printk(KERN_WARNING "%s: i82586 not responding, giving up.\n",
1516						dev->name);
1517					return;
1518				}
1519			}
1520		}
1521	}
1522
1523        scb_wrcbl(dev, CONF_LINK);
1524	scb_command(dev, 0xf000|SCB_CUstart);
1525	outb(0,ioaddr+SIGNAL_CA);
1526
1527	{
1528		unsigned short iboguscount=50,ifailcount=5;
1529		while (!scb_status(dev))
1530		{
1531			if (!--iboguscount)
1532			{
1533				if (--ifailcount)
1534				{
1535					printk(KERN_WARNING "%s: i82586 initialization timed out, status %04x, cmd %04x\n",
1536						dev->name, scb_status(dev), scb_rdcmd(dev));
1537					scb_wrcbl(dev, CONF_LINK);
1538				        scb_command(dev, 0xf000|SCB_CUstart);
1539					outb(0,ioaddr+SIGNAL_CA);
1540					iboguscount = 100;
1541				}
1542				else
1543				{
1544					printk(KERN_WARNING "%s: Failed to initialize i82586, giving up.\n",dev->name);
1545					return;
1546				}
1547			}
1548		}
1549	}
1550
1551	clear_loopback(dev);
1552	outb(SIRQ_en|irqrmap[dev->irq],ioaddr+SET_IRQ);
1553
1554	lp->init_time = jiffies;
1555#if NET_DEBUG > 6
1556        printk("%s: leaving eexp_hw_init586()\n", dev->name);
1557#endif
1558	return;
1559}
1560
1561static void eexp_setup_filter(struct net_device *dev)
1562{
1563	struct dev_mc_list *dmi = dev->mc_list;
1564	unsigned short ioaddr = dev->base_addr;
1565	int count = dev->mc_count;
1566	int i;
1567	if (count > 8) {
1568		printk(KERN_INFO "%s: too many multicast addresses (%d)\n",
1569		       dev->name, count);
1570		count = 8;
1571	}
1572
1573	outw(CONF_NR_MULTICAST & ~31, ioaddr+SM_PTR);
1574	outw(count, ioaddr+SHADOW(CONF_NR_MULTICAST));
1575	for (i = 0; i < count; i++) {
1576		unsigned short *data = (unsigned short *)dmi->dmi_addr;
1577		if (!dmi) {
1578			printk(KERN_INFO "%s: too few multicast addresses\n", dev->name);
1579			break;
1580		}
1581		if (dmi->dmi_addrlen != ETH_ALEN) {
1582			printk(KERN_INFO "%s: invalid multicast address length given.\n", dev->name);
1583			continue;
1584		}
1585		outw((CONF_MULTICAST+(6*i)) & ~31, ioaddr+SM_PTR);
1586		outw(data[0], ioaddr+SHADOW(CONF_MULTICAST+(6*i)));
1587		outw((CONF_MULTICAST+(6*i)+2) & ~31, ioaddr+SM_PTR);
1588		outw(data[1], ioaddr+SHADOW(CONF_MULTICAST+(6*i)+2));
1589		outw((CONF_MULTICAST+(6*i)+4) & ~31, ioaddr+SM_PTR);
1590		outw(data[2], ioaddr+SHADOW(CONF_MULTICAST+(6*i)+4));
1591	}
1592}
1593
1594/*
1595 * Set or clear the multicast filter for this adaptor.
1596 */
1597static void
1598eexp_set_multicast(struct net_device *dev)
1599{
1600        unsigned short ioaddr = dev->base_addr;
1601        struct net_local *lp = netdev_priv(dev);
1602        int kick = 0, i;
1603        if ((dev->flags & IFF_PROMISC) != lp->was_promisc) {
1604                outw(CONF_PROMISC & ~31, ioaddr+SM_PTR);
1605                i = inw(ioaddr+SHADOW(CONF_PROMISC));
1606                outw((dev->flags & IFF_PROMISC)?(i|1):(i & ~1),
1607                     ioaddr+SHADOW(CONF_PROMISC));
1608                lp->was_promisc = dev->flags & IFF_PROMISC;
1609                kick = 1;
1610        }
1611        if (!(dev->flags & IFF_PROMISC)) {
1612                eexp_setup_filter(dev);
1613                if (lp->old_mc_count != dev->mc_count) {
1614                        kick = 1;
1615                        lp->old_mc_count = dev->mc_count;
1616                }
1617        }
1618        if (kick) {
1619                unsigned long oj;
1620                scb_command(dev, SCB_CUsuspend);
1621                outb(0, ioaddr+SIGNAL_CA);
1622                outb(0, ioaddr+SIGNAL_CA);
1623                oj = jiffies;
1624                while ((SCB_CUstat(scb_status(dev)) == 2) &&
1625                       (time_before(jiffies, oj + 2000)));
1626		if (SCB_CUstat(scb_status(dev)) == 2)
1627			printk("%s: warning, CU didn't stop\n", dev->name);
1628                lp->started &= ~(STARTED_CU);
1629                scb_wrcbl(dev, CONF_LINK);
1630                scb_command(dev, SCB_CUstart);
1631                outb(0, ioaddr+SIGNAL_CA);
1632        }
1633}
1634
1635
1636/*
1637 * MODULE stuff
1638 */
1639
1640#ifdef MODULE
1641
1642#define EEXP_MAX_CARDS     4    /* max number of cards to support */
1643
1644static struct net_device *dev_eexp[EEXP_MAX_CARDS];
1645static int irq[EEXP_MAX_CARDS];
1646static int io[EEXP_MAX_CARDS];
1647
1648module_param_array(io, int, NULL, 0);
1649module_param_array(irq, int, NULL, 0);
1650MODULE_PARM_DESC(io, "EtherExpress 16 I/O base address(es)");
1651MODULE_PARM_DESC(irq, "EtherExpress 16 IRQ number(s)");
1652MODULE_LICENSE("GPL");
1653
1654
1655/* Ideally the user would give us io=, irq= for every card.  If any parameters
1656 * are specified, we verify and then use them.  If no parameters are given, we
1657 * autoprobe for one card only.
1658 */
1659int __init init_module(void)
1660{
1661	struct net_device *dev;
1662	int this_dev, found = 0;
1663
1664	for (this_dev = 0; this_dev < EEXP_MAX_CARDS; this_dev++) {
1665		dev = alloc_etherdev(sizeof(struct net_local));
1666		dev->irq = irq[this_dev];
1667		dev->base_addr = io[this_dev];
1668		if (io[this_dev] == 0) {
1669			if (this_dev)
1670				break;
1671			printk(KERN_NOTICE "eexpress.c: Module autoprobe not recommended, give io=xx.\n");
1672		}
1673		if (do_express_probe(dev) == 0) {
1674			dev_eexp[this_dev] = dev;
1675			found++;
1676			continue;
1677		}
1678		printk(KERN_WARNING "eexpress.c: Failed to register card at 0x%x.\n", io[this_dev]);
1679		free_netdev(dev);
1680		break;
1681	}
1682	if (found)
1683		return 0;
1684	return -ENXIO;
1685}
1686
1687void __exit cleanup_module(void)
1688{
1689	int this_dev;
1690
1691	for (this_dev = 0; this_dev < EEXP_MAX_CARDS; this_dev++) {
1692		struct net_device *dev = dev_eexp[this_dev];
1693		if (dev) {
1694			unregister_netdev(dev);
1695			free_netdev(dev);
1696		}
1697	}
1698}
1699#endif
1700
1701/*
1702 * Local Variables:
1703 *  c-file-style: "linux"
1704 *  tab-width: 8
1705 * End:
1706 */
1707