• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6.36/drivers/net/
1/* znet.c: An Zenith Z-Note ethernet driver for linux. */
2
3/*
4	Written by Donald Becker.
5
6	The author may be reached as becker@scyld.com.
7	This driver is based on the Linux skeleton driver.  The copyright of the
8	skeleton driver is held by the United States Government, as represented
9	by DIRNSA, and it is released under the GPL.
10
11	Thanks to Mike Hollick for alpha testing and suggestions.
12
13  References:
14	   The Crynwr packet driver.
15
16	  "82593 CSMA/CD Core LAN Controller" Intel datasheet, 1992
17	  Intel Microcommunications Databook, Vol. 1, 1990.
18    As usual with Intel, the documentation is incomplete and inaccurate.
19	I had to read the Crynwr packet driver to figure out how to actually
20	use the i82593, and guess at what register bits matched the loosely
21	related i82586.
22
23					Theory of Operation
24
25	The i82593 used in the Zenith Z-Note series operates using two(!) slave
26	DMA	channels, one interrupt, and one 8-bit I/O port.
27
28	While there	several ways to configure '593 DMA system, I chose the one
29	that seemed commensurate with the highest system performance in the face
30	of moderate interrupt latency: Both DMA channels are configured as
31	recirculating ring buffers, with one channel (#0) dedicated to Rx and
32	the other channel (#1) to Tx and configuration.  (Note that this is
33	different than the Crynwr driver, where the Tx DMA channel is initialized
34	before each operation.  That approach simplifies operation and Tx error
35	recovery, but requires additional I/O in normal operation and precludes
36	transmit buffer	chaining.)
37
38	Both rings are set to 8192 bytes using {TX,RX}_RING_SIZE.  This provides
39	a reasonable ring size for Rx, while simplifying DMA buffer allocation --
40	DMA buffers must not cross a 128K boundary.  (In truth the size selection
41	was influenced by my lack of '593 documentation.  I thus was constrained
42	to use the Crynwr '593 initialization table, which sets the Rx ring size
43	to 8K.)
44
45	Despite my usual low opinion about Intel-designed parts, I must admit
46	that the bulk data handling of the i82593 is a good design for
47	an integrated system, like a laptop, where using two slave DMA channels
48	doesn't pose a problem.  I still take issue with using only a single I/O
49	port.  In the same controlled environment there are essentially no
50	limitations on I/O space, and using multiple locations would eliminate
51	the	need for multiple operations when looking at status registers,
52	setting the Rx ring boundary, or switching to promiscuous mode.
53
54	I also question Zenith's selection of the '593: one of the advertised
55	advantages of earlier Intel parts was that if you figured out the magic
56	initialization incantation you could use the same part on many different
57	network types.  Zenith's use of the "FriendlyNet" (sic) connector rather
58	than an	on-board transceiver leads me to believe that they were planning
59	to take advantage of this.  But, uhmmm, the '593 omits all but ethernet
60	functionality from the serial subsystem.
61 */
62
63/* 10/2002
64
65   o Resurected for Linux 2.5+ by Marc Zyngier <maz@wild-wind.fr.eu.org> :
66
67   - Removed strange DMA snooping in znet_sent_packet, which lead to
68     TX buffer corruption on my laptop.
69   - Use init_etherdev stuff.
70   - Use kmalloc-ed DMA buffers.
71   - Use as few global variables as possible.
72   - Use proper resources management.
73   - Use wireless/i82593.h as much as possible (structure, constants)
74   - Compiles as module or build-in.
75   - Now survives unplugging/replugging cable.
76
77   Some code was taken from wavelan_cs.
78
79   Tested on a vintage Zenith Z-Note 433Lnp+. Probably broken on
80   anything else. Testers (and detailed bug reports) are welcome :-).
81
82   o TODO :
83
84   - Properly handle multicast
85   - Understand why some traffic patterns add a 1s latency...
86 */
87
88#include <linux/module.h>
89#include <linux/kernel.h>
90#include <linux/string.h>
91#include <linux/slab.h>
92#include <linux/errno.h>
93#include <linux/interrupt.h>
94#include <linux/ioport.h>
95#include <linux/init.h>
96#include <linux/delay.h>
97#include <linux/netdevice.h>
98#include <linux/etherdevice.h>
99#include <linux/skbuff.h>
100#include <linux/if_arp.h>
101#include <linux/bitops.h>
102
103#include <asm/system.h>
104#include <asm/io.h>
105#include <asm/dma.h>
106
107#include <linux/i82593.h>
108
109static char version[] __initdata = "znet.c:v1.02 9/23/94 becker@scyld.com\n";
110
111#ifndef ZNET_DEBUG
112#define ZNET_DEBUG 1
113#endif
114static unsigned int znet_debug = ZNET_DEBUG;
115module_param (znet_debug, int, 0);
116MODULE_PARM_DESC (znet_debug, "ZNet debug level");
117MODULE_LICENSE("GPL");
118
119/* The DMA modes we need aren't in <dma.h>. */
120#define DMA_RX_MODE		0x14	/* Auto init, I/O to mem, ++, demand. */
121#define DMA_TX_MODE		0x18	/* Auto init, Mem to I/O, ++, demand. */
122#define dma_page_eq(ptr1, ptr2) ((long)(ptr1)>>17 == (long)(ptr2)>>17)
123#define RX_BUF_SIZE 8192
124#define TX_BUF_SIZE 8192
125#define DMA_BUF_SIZE (RX_BUF_SIZE + 16)	/* 8k + 16 bytes for trailers */
126
127#define TX_TIMEOUT	10
128
129struct znet_private {
130	int rx_dma, tx_dma;
131	spinlock_t lock;
132	short sia_base, sia_size, io_size;
133	struct i82593_conf_block i593_init;
134	/* The starting, current, and end pointers for the packet buffers. */
135	ushort *rx_start, *rx_cur, *rx_end;
136	ushort *tx_start, *tx_cur, *tx_end;
137	ushort tx_buf_len;			/* Tx buffer length, in words. */
138};
139
140/* Only one can be built-in;-> */
141static struct net_device *znet_dev;
142
143struct netidblk {
144	char magic[8];		/* The magic number (string) "NETIDBLK" */
145	unsigned char netid[8]; /* The physical station address */
146	char nettype, globalopt;
147	char vendor[8];		/* The machine vendor and product name. */
148	char product[8];
149	char irq1, irq2;		/* Interrupts, only one is currently used.	*/
150	char dma1, dma2;
151	short dma_mem_misc[8];		/* DMA buffer locations (unused in Linux). */
152	short iobase1, iosize1;
153	short iobase2, iosize2;		/* Second iobase unused. */
154	char driver_options;			/* Misc. bits */
155	char pad;
156};
157
158static int	znet_open(struct net_device *dev);
159static netdev_tx_t znet_send_packet(struct sk_buff *skb,
160				    struct net_device *dev);
161static irqreturn_t znet_interrupt(int irq, void *dev_id);
162static void	znet_rx(struct net_device *dev);
163static int	znet_close(struct net_device *dev);
164static void hardware_init(struct net_device *dev);
165static void update_stop_hit(short ioaddr, unsigned short rx_stop_offset);
166static void znet_tx_timeout (struct net_device *dev);
167
168/* Request needed resources */
169static int znet_request_resources (struct net_device *dev)
170{
171	struct znet_private *znet = netdev_priv(dev);
172
173	if (request_irq (dev->irq, znet_interrupt, 0, "ZNet", dev))
174		goto failed;
175	if (request_dma (znet->rx_dma, "ZNet rx"))
176		goto free_irq;
177	if (request_dma (znet->tx_dma, "ZNet tx"))
178		goto free_rx_dma;
179	if (!request_region (znet->sia_base, znet->sia_size, "ZNet SIA"))
180		goto free_tx_dma;
181	if (!request_region (dev->base_addr, znet->io_size, "ZNet I/O"))
182		goto free_sia;
183
184	return 0;				/* Happy ! */
185
186 free_sia:
187	release_region (znet->sia_base, znet->sia_size);
188 free_tx_dma:
189	free_dma (znet->tx_dma);
190 free_rx_dma:
191	free_dma (znet->rx_dma);
192 free_irq:
193	free_irq (dev->irq, dev);
194 failed:
195	return -1;
196}
197
198static void znet_release_resources (struct net_device *dev)
199{
200	struct znet_private *znet = netdev_priv(dev);
201
202	release_region (znet->sia_base, znet->sia_size);
203	release_region (dev->base_addr, znet->io_size);
204	free_dma (znet->tx_dma);
205	free_dma (znet->rx_dma);
206	free_irq (dev->irq, dev);
207}
208
209/* Keep the magical SIA stuff in a single function... */
210static void znet_transceiver_power (struct net_device *dev, int on)
211{
212	struct znet_private *znet = netdev_priv(dev);
213	unsigned char v;
214
215	/* Turn on/off the 82501 SIA, using zenith-specific magic. */
216	/* Select LAN control register */
217	outb(0x10, znet->sia_base);
218
219	if (on)
220		v = inb(znet->sia_base + 1) | 0x84;
221	else
222		v = inb(znet->sia_base + 1) & ~0x84;
223
224	outb(v, znet->sia_base+1); /* Turn on/off LAN power (bit 2). */
225}
226
227/* Init the i82593, with current promisc/mcast configuration.
228   Also used from hardware_init. */
229static void znet_set_multicast_list (struct net_device *dev)
230{
231	struct znet_private *znet = netdev_priv(dev);
232	short ioaddr = dev->base_addr;
233	struct i82593_conf_block *cfblk = &znet->i593_init;
234
235	memset(cfblk, 0x00, sizeof(struct i82593_conf_block));
236
237        /* The configuration block.  What an undocumented nightmare.
238	   The first set of values are those suggested (without explanation)
239	   for ethernet in the Intel 82586 databook.  The rest appear to be
240	   completely undocumented, except for cryptic notes in the Crynwr
241	   packet driver.  This driver uses the Crynwr values verbatim. */
242
243	/* maz : Rewritten to take advantage of the wanvelan includes.
244	   At least we have names, not just blind values */
245
246	/* Byte 0 */
247	cfblk->fifo_limit = 10;	/* = 16 B rx and 80 B tx fifo thresholds */
248	cfblk->forgnesi = 0;	/* 0=82C501, 1=AMD7992B compatibility */
249	cfblk->fifo_32 = 1;
250	cfblk->d6mod = 0;  	/* Run in i82593 advanced mode */
251	cfblk->throttle_enb = 1;
252
253	/* Byte 1 */
254	cfblk->throttle = 8;	/* Continuous w/interrupts, 128-clock DMA. */
255	cfblk->cntrxint = 0;	/* enable continuous mode receive interrupts */
256	cfblk->contin = 1;	/* enable continuous mode */
257
258	/* Byte 2 */
259	cfblk->addr_len = ETH_ALEN;
260	cfblk->acloc = 1;	/* Disable source addr insertion by i82593 */
261	cfblk->preamb_len = 2;	/* 8 bytes preamble */
262	cfblk->loopback = 0;	/* Loopback off */
263
264	/* Byte 3 */
265	cfblk->lin_prio = 0;	/* Default priorities & backoff methods. */
266	cfblk->tbofstop = 0;
267	cfblk->exp_prio = 0;
268	cfblk->bof_met = 0;
269
270	/* Byte 4 */
271	cfblk->ifrm_spc = 6;	/* 96 bit times interframe spacing */
272
273	/* Byte 5 */
274	cfblk->slottim_low = 0; /* 512 bit times slot time (low) */
275
276	/* Byte 6 */
277	cfblk->slottim_hi = 2;	/* 512 bit times slot time (high) */
278	cfblk->max_retr = 15;	/* 15 collisions retries */
279
280	/* Byte 7 */
281	cfblk->prmisc = ((dev->flags & IFF_PROMISC) ? 1 : 0); /* Promiscuous mode */
282	cfblk->bc_dis = 0;	/* Enable broadcast reception */
283	cfblk->crs_1 = 0;	/* Don't transmit without carrier sense */
284	cfblk->nocrc_ins = 0;	/* i82593 generates CRC */
285	cfblk->crc_1632 = 0;	/* 32-bit Autodin-II CRC */
286	cfblk->crs_cdt = 0;	/* CD not to be interpreted as CS */
287
288	/* Byte 8 */
289	cfblk->cs_filter = 0;  	/* CS is recognized immediately */
290	cfblk->crs_src = 0;	/* External carrier sense */
291	cfblk->cd_filter = 0;  	/* CD is recognized immediately */
292
293	/* Byte 9 */
294	cfblk->min_fr_len = ETH_ZLEN >> 2; /* Minimum frame length */
295
296	/* Byte A */
297	cfblk->lng_typ = 1;	/* Type/length checks OFF */
298	cfblk->lng_fld = 1; 	/* Disable 802.3 length field check */
299	cfblk->rxcrc_xf = 1;	/* Don't transfer CRC to memory */
300	cfblk->artx = 1;	/* Disable automatic retransmission */
301	cfblk->sarec = 1;	/* Disable source addr trig of CD */
302	cfblk->tx_jabber = 0;	/* Disable jabber jam sequence */
303	cfblk->hash_1 = 1; 	/* Use bits 0-5 in mc address hash */
304	cfblk->lbpkpol = 0; 	/* Loopback pin active high */
305
306	/* Byte B */
307	cfblk->fdx = 0;		/* Disable full duplex operation */
308
309	/* Byte C */
310	cfblk->dummy_6 = 0x3f; 	/* all ones, Default multicast addresses & backoff. */
311	cfblk->mult_ia = 0;	/* No multiple individual addresses */
312	cfblk->dis_bof = 0;	/* Disable the backoff algorithm ?! */
313
314	/* Byte D */
315	cfblk->dummy_1 = 1; 	/* set to 1 */
316	cfblk->tx_ifs_retrig = 3; /* Hmm... Disabled */
317	cfblk->mc_all = (!netdev_mc_empty(dev) ||
318			(dev->flags & IFF_ALLMULTI)); /* multicast all mode */
319	cfblk->rcv_mon = 0;	/* Monitor mode disabled */
320	cfblk->frag_acpt = 0;	/* Do not accept fragments */
321	cfblk->tstrttrs = 0;	/* No start transmission threshold */
322
323	/* Byte E */
324	cfblk->fretx = 1;	/* FIFO automatic retransmission */
325	cfblk->runt_eop = 0;	/* drop "runt" packets */
326	cfblk->hw_sw_pin = 0;	/* ?? */
327	cfblk->big_endn = 0;	/* Big Endian ? no... */
328	cfblk->syncrqs = 1;	/* Synchronous DRQ deassertion... */
329	cfblk->sttlen = 1;  	/* 6 byte status registers */
330	cfblk->rx_eop = 0;  	/* Signal EOP on packet reception */
331	cfblk->tx_eop = 0;  	/* Signal EOP on packet transmission */
332
333	/* Byte F */
334	cfblk->rbuf_size = RX_BUF_SIZE >> 12; /* Set receive buffer size */
335	cfblk->rcvstop = 1; 	/* Enable Receive Stop Register */
336
337	if (znet_debug > 2) {
338		int i;
339		unsigned char *c;
340
341		for (i = 0, c = (char *) cfblk; i < sizeof (*cfblk); i++)
342			printk ("%02X ", c[i]);
343		printk ("\n");
344	}
345
346	*znet->tx_cur++ = sizeof(struct i82593_conf_block);
347	memcpy(znet->tx_cur, cfblk, sizeof(struct i82593_conf_block));
348	znet->tx_cur += sizeof(struct i82593_conf_block)/2;
349	outb(OP0_CONFIGURE | CR0_CHNL, ioaddr);
350
351}
352
353static const struct net_device_ops znet_netdev_ops = {
354	.ndo_open		= znet_open,
355	.ndo_stop		= znet_close,
356	.ndo_start_xmit		= znet_send_packet,
357	.ndo_set_multicast_list = znet_set_multicast_list,
358	.ndo_tx_timeout		= znet_tx_timeout,
359	.ndo_change_mtu		= eth_change_mtu,
360	.ndo_set_mac_address 	= eth_mac_addr,
361	.ndo_validate_addr	= eth_validate_addr,
362};
363
364/* The Z-Note probe is pretty easy.  The NETIDBLK exists in the safe-to-probe
365   BIOS area.  We just scan for the signature, and pull the vital parameters
366   out of the structure. */
367
368static int __init znet_probe (void)
369{
370	int i;
371	struct netidblk *netinfo;
372	struct znet_private *znet;
373	struct net_device *dev;
374	char *p;
375	int err = -ENOMEM;
376
377	/* This code scans the region 0xf0000 to 0xfffff for a "NETIDBLK". */
378	for(p = (char *)phys_to_virt(0xf0000); p < (char *)phys_to_virt(0x100000); p++)
379		if (*p == 'N'  &&  strncmp(p, "NETIDBLK", 8) == 0)
380			break;
381
382	if (p >= (char *)phys_to_virt(0x100000)) {
383		if (znet_debug > 1)
384			printk(KERN_INFO "No Z-Note ethernet adaptor found.\n");
385		return -ENODEV;
386	}
387
388	dev = alloc_etherdev(sizeof(struct znet_private));
389	if (!dev)
390		return -ENOMEM;
391
392	znet = netdev_priv(dev);
393
394	netinfo = (struct netidblk *)p;
395	dev->base_addr = netinfo->iobase1;
396	dev->irq = netinfo->irq1;
397
398	/* The station address is in the "netidblk" at 0x0f0000. */
399	for (i = 0; i < 6; i++)
400		dev->dev_addr[i] = netinfo->netid[i];
401
402	printk(KERN_INFO "%s: ZNET at %#3lx, %pM"
403	       ", using IRQ %d DMA %d and %d.\n",
404	       dev->name, dev->base_addr, dev->dev_addr,
405	       dev->irq, netinfo->dma1, netinfo->dma2);
406
407	if (znet_debug > 1) {
408		printk(KERN_INFO "%s: vendor '%16.16s' IRQ1 %d IRQ2 %d DMA1 %d DMA2 %d.\n",
409		       dev->name, netinfo->vendor,
410		       netinfo->irq1, netinfo->irq2,
411		       netinfo->dma1, netinfo->dma2);
412		printk(KERN_INFO "%s: iobase1 %#x size %d iobase2 %#x size %d net type %2.2x.\n",
413		       dev->name, netinfo->iobase1, netinfo->iosize1,
414		       netinfo->iobase2, netinfo->iosize2, netinfo->nettype);
415	}
416
417	if (znet_debug > 0)
418		printk(KERN_INFO "%s", version);
419
420	znet->rx_dma = netinfo->dma1;
421	znet->tx_dma = netinfo->dma2;
422	spin_lock_init(&znet->lock);
423	znet->sia_base = 0xe6;	/* Magic address for the 82501 SIA */
424	znet->sia_size = 2;
425	/* maz: Despite the '593 being advertised above as using a
426	 * single 8bits I/O port, this driver does many 16bits
427	 * access. So set io_size accordingly */
428	znet->io_size  = 2;
429
430	if (!(znet->rx_start = kmalloc (DMA_BUF_SIZE, GFP_KERNEL | GFP_DMA)))
431		goto free_dev;
432	if (!(znet->tx_start = kmalloc (DMA_BUF_SIZE, GFP_KERNEL | GFP_DMA)))
433		goto free_rx;
434
435	if (!dma_page_eq (znet->rx_start, znet->rx_start + (RX_BUF_SIZE/2-1)) ||
436	    !dma_page_eq (znet->tx_start, znet->tx_start + (TX_BUF_SIZE/2-1))) {
437		printk (KERN_WARNING "tx/rx crossing DMA frontiers, giving up\n");
438		goto free_tx;
439	}
440
441	znet->rx_end = znet->rx_start + RX_BUF_SIZE/2;
442	znet->tx_buf_len = TX_BUF_SIZE/2;
443	znet->tx_end = znet->tx_start + znet->tx_buf_len;
444
445	/* The ZNET-specific entries in the device structure. */
446	dev->netdev_ops = &znet_netdev_ops;
447	dev->watchdog_timeo = TX_TIMEOUT;
448	err = register_netdev(dev);
449	if (err)
450		goto free_tx;
451	znet_dev = dev;
452	return 0;
453
454 free_tx:
455	kfree(znet->tx_start);
456 free_rx:
457	kfree(znet->rx_start);
458 free_dev:
459	free_netdev(dev);
460	return err;
461}
462
463
464static int znet_open(struct net_device *dev)
465{
466	int ioaddr = dev->base_addr;
467
468	if (znet_debug > 2)
469		printk(KERN_DEBUG "%s: znet_open() called.\n", dev->name);
470
471	/* These should never fail.  You can't add devices to a sealed box! */
472	if (znet_request_resources (dev)) {
473		printk(KERN_WARNING "%s: Not opened -- resource busy?!?\n", dev->name);
474		return -EBUSY;
475	}
476
477	znet_transceiver_power (dev, 1);
478
479	/* According to the Crynwr driver we should wait 50 msec. for the
480	   LAN clock to stabilize.  My experiments indicates that the '593 can
481	   be initialized immediately.  The delay is probably needed for the
482	   DC-to-DC converter to come up to full voltage, and for the oscillator
483	   to be spot-on at 20Mhz before transmitting.
484	   Until this proves to be a problem we rely on the higher layers for the
485	   delay and save allocating a timer entry. */
486
487	/* maz : Well, I'm getting every time the following message
488	 * without the delay on a 486@33. This machine is much too
489	 * fast... :-) So maybe the Crynwr driver wasn't wrong after
490	 * all, even if the message is completly harmless on my
491	 * setup. */
492	mdelay (50);
493
494	/* This follows the packet driver's lead, and checks for success. */
495	if (inb(ioaddr) != 0x10 && inb(ioaddr) != 0x00)
496		printk(KERN_WARNING "%s: Problem turning on the transceiver power.\n",
497		       dev->name);
498
499	hardware_init(dev);
500	netif_start_queue (dev);
501
502	return 0;
503}
504
505
506static void znet_tx_timeout (struct net_device *dev)
507{
508	int ioaddr = dev->base_addr;
509	ushort event, tx_status, rx_offset, state;
510
511	outb (CR0_STATUS_0, ioaddr);
512	event = inb (ioaddr);
513	outb (CR0_STATUS_1, ioaddr);
514	tx_status = inw (ioaddr);
515	outb (CR0_STATUS_2, ioaddr);
516	rx_offset = inw (ioaddr);
517	outb (CR0_STATUS_3, ioaddr);
518	state = inb (ioaddr);
519	printk (KERN_WARNING "%s: transmit timed out, status %02x %04x %04x %02x,"
520	 " resetting.\n", dev->name, event, tx_status, rx_offset, state);
521	if (tx_status == TX_LOST_CRS)
522		printk (KERN_WARNING "%s: Tx carrier error, check transceiver cable.\n",
523			dev->name);
524	outb (OP0_RESET, ioaddr);
525	hardware_init (dev);
526	netif_wake_queue (dev);
527}
528
529static netdev_tx_t znet_send_packet(struct sk_buff *skb, struct net_device *dev)
530{
531	int ioaddr = dev->base_addr;
532	struct znet_private *znet = netdev_priv(dev);
533	unsigned long flags;
534	short length = skb->len;
535
536	if (znet_debug > 4)
537		printk(KERN_DEBUG "%s: ZNet_send_packet.\n", dev->name);
538
539	if (length < ETH_ZLEN) {
540		if (skb_padto(skb, ETH_ZLEN))
541			return NETDEV_TX_OK;
542		length = ETH_ZLEN;
543	}
544
545	netif_stop_queue (dev);
546
547	/* Check that the part hasn't reset itself, probably from suspend. */
548	outb(CR0_STATUS_0, ioaddr);
549	if (inw(ioaddr) == 0x0010 &&
550	    inw(ioaddr) == 0x0000 &&
551	    inw(ioaddr) == 0x0010) {
552		if (znet_debug > 1)
553			printk (KERN_WARNING "%s : waking up\n", dev->name);
554		hardware_init(dev);
555		znet_transceiver_power (dev, 1);
556	}
557
558	if (1) {
559		unsigned char *buf = (void *)skb->data;
560		ushort *tx_link = znet->tx_cur - 1;
561		ushort rnd_len = (length + 1)>>1;
562
563		dev->stats.tx_bytes+=length;
564
565		if (znet->tx_cur >= znet->tx_end)
566		  znet->tx_cur = znet->tx_start;
567		*znet->tx_cur++ = length;
568		if (znet->tx_cur + rnd_len + 1 > znet->tx_end) {
569			int semi_cnt = (znet->tx_end - znet->tx_cur)<<1; /* Cvrt to byte cnt. */
570			memcpy(znet->tx_cur, buf, semi_cnt);
571			rnd_len -= semi_cnt>>1;
572			memcpy(znet->tx_start, buf + semi_cnt, length - semi_cnt);
573			znet->tx_cur = znet->tx_start + rnd_len;
574		} else {
575			memcpy(znet->tx_cur, buf, skb->len);
576			znet->tx_cur += rnd_len;
577		}
578		*znet->tx_cur++ = 0;
579
580		spin_lock_irqsave(&znet->lock, flags);
581		{
582			*tx_link = OP0_TRANSMIT | CR0_CHNL;
583			/* Is this always safe to do? */
584			outb(OP0_TRANSMIT | CR0_CHNL, ioaddr);
585		}
586		spin_unlock_irqrestore (&znet->lock, flags);
587
588		netif_start_queue (dev);
589
590		if (znet_debug > 4)
591		  printk(KERN_DEBUG "%s: Transmitter queued, length %d.\n", dev->name, length);
592	}
593	dev_kfree_skb(skb);
594	return NETDEV_TX_OK;
595}
596
597/* The ZNET interrupt handler. */
598static irqreturn_t znet_interrupt(int irq, void *dev_id)
599{
600	struct net_device *dev = dev_id;
601	struct znet_private *znet = netdev_priv(dev);
602	int ioaddr;
603	int boguscnt = 20;
604	int handled = 0;
605
606	spin_lock (&znet->lock);
607
608	ioaddr = dev->base_addr;
609
610	outb(CR0_STATUS_0, ioaddr);
611	do {
612		ushort status = inb(ioaddr);
613		if (znet_debug > 5) {
614			ushort result, rx_ptr, running;
615			outb(CR0_STATUS_1, ioaddr);
616			result = inw(ioaddr);
617			outb(CR0_STATUS_2, ioaddr);
618			rx_ptr = inw(ioaddr);
619			outb(CR0_STATUS_3, ioaddr);
620			running = inb(ioaddr);
621			printk(KERN_DEBUG "%s: interrupt, status %02x, %04x %04x %02x serial %d.\n",
622				 dev->name, status, result, rx_ptr, running, boguscnt);
623		}
624		if ((status & SR0_INTERRUPT) == 0)
625			break;
626
627		handled = 1;
628
629		if ((status & SR0_EVENT_MASK) == SR0_TRANSMIT_DONE ||
630		    (status & SR0_EVENT_MASK) == SR0_RETRANSMIT_DONE ||
631		    (status & SR0_EVENT_MASK) == SR0_TRANSMIT_NO_CRC_DONE) {
632			int tx_status;
633			outb(CR0_STATUS_1, ioaddr);
634			tx_status = inw(ioaddr);
635			/* It's undocumented, but tx_status seems to match the i82586. */
636			if (tx_status & TX_OK) {
637				dev->stats.tx_packets++;
638				dev->stats.collisions += tx_status & TX_NCOL_MASK;
639			} else {
640				if (tx_status & (TX_LOST_CTS | TX_LOST_CRS))
641					dev->stats.tx_carrier_errors++;
642				if (tx_status & TX_UND_RUN)
643					dev->stats.tx_fifo_errors++;
644				if (!(tx_status & TX_HRT_BEAT))
645					dev->stats.tx_heartbeat_errors++;
646				if (tx_status & TX_MAX_COL)
647					dev->stats.tx_aborted_errors++;
648				/* ...and the catch-all. */
649				if ((tx_status | (TX_LOST_CRS | TX_LOST_CTS | TX_UND_RUN | TX_HRT_BEAT | TX_MAX_COL)) != (TX_LOST_CRS | TX_LOST_CTS | TX_UND_RUN | TX_HRT_BEAT | TX_MAX_COL))
650					dev->stats.tx_errors++;
651
652				/* Transceiver may be stuck if cable
653				 * was removed while emiting a
654				 * packet. Flip it off, then on to
655				 * reset it. This is very empirical,
656				 * but it seems to work. */
657
658				znet_transceiver_power (dev, 0);
659				znet_transceiver_power (dev, 1);
660			}
661			netif_wake_queue (dev);
662		}
663
664		if ((status & SR0_RECEPTION) ||
665		    (status & SR0_EVENT_MASK) == SR0_STOP_REG_HIT) {
666			znet_rx(dev);
667		}
668		/* Clear the interrupts we've handled. */
669		outb(CR0_INT_ACK, ioaddr);
670	} while (boguscnt--);
671
672	spin_unlock (&znet->lock);
673
674	return IRQ_RETVAL(handled);
675}
676
677static void znet_rx(struct net_device *dev)
678{
679	struct znet_private *znet = netdev_priv(dev);
680	int ioaddr = dev->base_addr;
681	int boguscount = 1;
682	short next_frame_end_offset = 0; 		/* Offset of next frame start. */
683	short *cur_frame_end;
684	short cur_frame_end_offset;
685
686	outb(CR0_STATUS_2, ioaddr);
687	cur_frame_end_offset = inw(ioaddr);
688
689	if (cur_frame_end_offset == znet->rx_cur - znet->rx_start) {
690		printk(KERN_WARNING "%s: Interrupted, but nothing to receive, offset %03x.\n",
691			   dev->name, cur_frame_end_offset);
692		return;
693	}
694
695	/* Use same method as the Crynwr driver: construct a forward list in
696	   the same area of the backwards links we now have.  This allows us to
697	   pass packets to the upper layers in the order they were received --
698	   important for fast-path sequential operations. */
699	while (znet->rx_start + cur_frame_end_offset != znet->rx_cur &&
700	       ++boguscount < 5) {
701		unsigned short hi_cnt, lo_cnt, hi_status, lo_status;
702		int count, status;
703
704		if (cur_frame_end_offset < 4) {
705			/* Oh no, we have a special case: the frame trailer wraps around
706			   the end of the ring buffer.  We've saved space at the end of
707			   the ring buffer for just this problem. */
708			memcpy(znet->rx_end, znet->rx_start, 8);
709			cur_frame_end_offset += (RX_BUF_SIZE/2);
710		}
711		cur_frame_end = znet->rx_start + cur_frame_end_offset - 4;
712
713		lo_status = *cur_frame_end++;
714		hi_status = *cur_frame_end++;
715		status = ((hi_status & 0xff) << 8) + (lo_status & 0xff);
716		lo_cnt = *cur_frame_end++;
717		hi_cnt = *cur_frame_end++;
718		count = ((hi_cnt & 0xff) << 8) + (lo_cnt & 0xff);
719
720		if (znet_debug > 5)
721		  printk(KERN_DEBUG "Constructing trailer at location %03x, %04x %04x %04x %04x"
722				 " count %#x status %04x.\n",
723				 cur_frame_end_offset<<1, lo_status, hi_status, lo_cnt, hi_cnt,
724				 count, status);
725		cur_frame_end[-4] = status;
726		cur_frame_end[-3] = next_frame_end_offset;
727		cur_frame_end[-2] = count;
728		next_frame_end_offset = cur_frame_end_offset;
729		cur_frame_end_offset -= ((count + 1)>>1) + 3;
730		if (cur_frame_end_offset < 0)
731		  cur_frame_end_offset += RX_BUF_SIZE/2;
732	};
733
734	/* Now step  forward through the list. */
735	do {
736		ushort *this_rfp_ptr = znet->rx_start + next_frame_end_offset;
737		int status = this_rfp_ptr[-4];
738		int pkt_len = this_rfp_ptr[-2];
739
740		if (znet_debug > 5)
741		  printk(KERN_DEBUG "Looking at trailer ending at %04x status %04x length %03x"
742				 " next %04x.\n", next_frame_end_offset<<1, status, pkt_len,
743				 this_rfp_ptr[-3]<<1);
744		/* Once again we must assume that the i82586 docs apply. */
745		if ( ! (status & RX_RCV_OK)) { /* There was an error. */
746			dev->stats.rx_errors++;
747			if (status & RX_CRC_ERR) dev->stats.rx_crc_errors++;
748			if (status & RX_ALG_ERR) dev->stats.rx_frame_errors++;
749			/* maz : Wild guess... */
750			if (status & RX_OVRRUN) dev->stats.rx_over_errors++;
751			if (status & RX_SRT_FRM) dev->stats.rx_length_errors++;
752		} else if (pkt_len > 1536) {
753			dev->stats.rx_length_errors++;
754		} else {
755			/* Malloc up new buffer. */
756			struct sk_buff *skb;
757
758			skb = dev_alloc_skb(pkt_len);
759			if (skb == NULL) {
760				if (znet_debug)
761				  printk(KERN_WARNING "%s: Memory squeeze, dropping packet.\n", dev->name);
762				dev->stats.rx_dropped++;
763				break;
764			}
765
766			if (&znet->rx_cur[(pkt_len+1)>>1] > znet->rx_end) {
767				int semi_cnt = (znet->rx_end - znet->rx_cur)<<1;
768				memcpy(skb_put(skb,semi_cnt), znet->rx_cur, semi_cnt);
769				memcpy(skb_put(skb,pkt_len-semi_cnt), znet->rx_start,
770					   pkt_len - semi_cnt);
771			} else {
772				memcpy(skb_put(skb,pkt_len), znet->rx_cur, pkt_len);
773				if (znet_debug > 6) {
774					unsigned int *packet = (unsigned int *) skb->data;
775					printk(KERN_DEBUG "Packet data is %08x %08x %08x %08x.\n", packet[0],
776						   packet[1], packet[2], packet[3]);
777				}
778		  }
779		  skb->protocol=eth_type_trans(skb,dev);
780		  netif_rx(skb);
781		  dev->stats.rx_packets++;
782		  dev->stats.rx_bytes += pkt_len;
783		}
784		znet->rx_cur = this_rfp_ptr;
785		if (znet->rx_cur >= znet->rx_end)
786			znet->rx_cur -= RX_BUF_SIZE/2;
787		update_stop_hit(ioaddr, (znet->rx_cur - znet->rx_start)<<1);
788		next_frame_end_offset = this_rfp_ptr[-3];
789		if (next_frame_end_offset == 0)		/* Read all the frames? */
790			break;			/* Done for now */
791		this_rfp_ptr = znet->rx_start + next_frame_end_offset;
792	} while (--boguscount);
793
794	/* If any worth-while packets have been received, dev_rint()
795	   has done a mark_bh(INET_BH) for us and will work on them
796	   when we get to the bottom-half routine. */
797}
798
799/* The inverse routine to znet_open(). */
800static int znet_close(struct net_device *dev)
801{
802	int ioaddr = dev->base_addr;
803
804	netif_stop_queue (dev);
805
806	outb(OP0_RESET, ioaddr);			/* CMD0_RESET */
807
808	if (znet_debug > 1)
809		printk(KERN_DEBUG "%s: Shutting down ethercard.\n", dev->name);
810	/* Turn off transceiver power. */
811	znet_transceiver_power (dev, 0);
812
813	znet_release_resources (dev);
814
815	return 0;
816}
817
818static void show_dma(struct net_device *dev)
819{
820	short ioaddr = dev->base_addr;
821	unsigned char stat = inb (ioaddr);
822	struct znet_private *znet = netdev_priv(dev);
823	unsigned long flags;
824	short dma_port = ((znet->tx_dma&3)<<2) + IO_DMA2_BASE;
825	unsigned addr = inb(dma_port);
826	short residue;
827
828	addr |= inb(dma_port) << 8;
829	residue = get_dma_residue(znet->tx_dma);
830
831	if (znet_debug > 1) {
832		flags=claim_dma_lock();
833		printk(KERN_DEBUG "Stat:%02x Addr: %04x cnt:%3x\n",
834		       stat, addr<<1, residue);
835		release_dma_lock(flags);
836	}
837}
838
839/* Initialize the hardware.  We have to do this when the board is open()ed
840   or when we come out of suspend mode. */
841static void hardware_init(struct net_device *dev)
842{
843	unsigned long flags;
844	short ioaddr = dev->base_addr;
845	struct znet_private *znet = netdev_priv(dev);
846
847	znet->rx_cur = znet->rx_start;
848	znet->tx_cur = znet->tx_start;
849
850	/* Reset the chip, and start it up. */
851	outb(OP0_RESET, ioaddr);
852
853	flags=claim_dma_lock();
854	disable_dma(znet->rx_dma); 		/* reset by an interrupting task. */
855	clear_dma_ff(znet->rx_dma);
856	set_dma_mode(znet->rx_dma, DMA_RX_MODE);
857	set_dma_addr(znet->rx_dma, (unsigned int) znet->rx_start);
858	set_dma_count(znet->rx_dma, RX_BUF_SIZE);
859	enable_dma(znet->rx_dma);
860	/* Now set up the Tx channel. */
861	disable_dma(znet->tx_dma);
862	clear_dma_ff(znet->tx_dma);
863	set_dma_mode(znet->tx_dma, DMA_TX_MODE);
864	set_dma_addr(znet->tx_dma, (unsigned int) znet->tx_start);
865	set_dma_count(znet->tx_dma, znet->tx_buf_len<<1);
866	enable_dma(znet->tx_dma);
867	release_dma_lock(flags);
868
869	if (znet_debug > 1)
870	  printk(KERN_DEBUG "%s: Initializing the i82593, rx buf %p tx buf %p\n",
871			 dev->name, znet->rx_start,znet->tx_start);
872	/* Do an empty configure command, just like the Crynwr driver.  This
873	   resets to chip to its default values. */
874	*znet->tx_cur++ = 0;
875	*znet->tx_cur++ = 0;
876	show_dma(dev);
877	outb(OP0_CONFIGURE | CR0_CHNL, ioaddr);
878
879	znet_set_multicast_list (dev);
880
881	*znet->tx_cur++ = 6;
882	memcpy(znet->tx_cur, dev->dev_addr, 6);
883	znet->tx_cur += 3;
884	show_dma(dev);
885	outb(OP0_IA_SETUP | CR0_CHNL, ioaddr);
886	show_dma(dev);
887
888	update_stop_hit(ioaddr, 8192);
889	if (znet_debug > 1)  printk(KERN_DEBUG "enabling Rx.\n");
890	outb(OP0_RCV_ENABLE, ioaddr);
891	netif_start_queue (dev);
892}
893
894static void update_stop_hit(short ioaddr, unsigned short rx_stop_offset)
895{
896	outb(OP0_SWIT_TO_PORT_1 | CR0_CHNL, ioaddr);
897	if (znet_debug > 5)
898	  printk(KERN_DEBUG "Updating stop hit with value %02x.\n",
899			 (rx_stop_offset >> 6) | CR1_STOP_REG_UPDATE);
900	outb((rx_stop_offset >> 6) | CR1_STOP_REG_UPDATE, ioaddr);
901	outb(OP1_SWIT_TO_PORT_0, ioaddr);
902}
903
904static __exit void znet_cleanup (void)
905{
906	if (znet_dev) {
907		struct znet_private *znet = netdev_priv(znet_dev);
908
909		unregister_netdev (znet_dev);
910		kfree (znet->rx_start);
911		kfree (znet->tx_start);
912		free_netdev (znet_dev);
913	}
914}
915
916module_init (znet_probe);
917module_exit (znet_cleanup);
918