1 /* 3c507.c: An EtherLink16 device driver for Linux. */
2 /*
3 	Written 1993,1994 by Donald Becker.
4 
5 	Copyright 1993 United States Government as represented by the
6 	Director, National Security Agency.
7 
8 	This software may be used and distributed according to the terms
9 	of the GNU General Public License, incorporated herein by reference.
10 
11 	The author may be reached as becker@scyld.com, or C/O
12 	Scyld Computing Corporation
13 	410 Severn Ave., Suite 210
14 	Annapolis MD 21403
15 
16 
17 	Thanks go to jennings@Montrouge.SMR.slb.com ( Patrick Jennings)
18 	and jrs@world.std.com (Rick Sladkey) for testing and bugfixes.
19 	Mark Salazar <leslie@access.digex.net> made the changes for cards with
20 	only 16K packet buffers.
21 
22 	Things remaining to do:
23 	Verify that the tx and rx buffers don't have fencepost errors.
24 	Move the theory of operation and memory map documentation.
25 	The statistics need to be updated correctly.
26 */
27 
28 #define DRV_NAME		"3c507"
29 #define DRV_VERSION		"1.10a"
30 #define DRV_RELDATE		"11/17/2001"
31 
32 static const char version[] =
33 	DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " Donald Becker (becker@scyld.com)\n";
34 
35 
36 #include <linux/module.h>
37 
38 /*
39   Sources:
40 	This driver wouldn't have been written with the availability of the
41 	Crynwr driver source code.	It provided a known-working implementation
42 	that filled in the gaping holes of the Intel documentation.  Three cheers
43 	for Russ Nelson.
44 
45 	Intel Microcommunications Databook, Vol. 1, 1990.  It provides just enough
46 	info that the casual reader might think that it documents the i82586 :-<.
47 */
48 
49 #include <linux/kernel.h>
50 #include <linux/sched.h>
51 #include <linux/types.h>
52 #include <linux/fcntl.h>
53 #include <linux/interrupt.h>
54 #include <linux/ptrace.h>
55 #include <linux/ioport.h>
56 #include <linux/in.h>
57 #include <linux/string.h>
58 #include <linux/spinlock.h>
59 #include <linux/ethtool.h>
60 
61 #include <asm/uaccess.h>
62 #include <asm/system.h>
63 #include <asm/bitops.h>
64 #include <asm/io.h>
65 #include <asm/dma.h>
66 #include <linux/errno.h>
67 
68 #include <linux/netdevice.h>
69 #include <linux/etherdevice.h>
70 #include <linux/skbuff.h>
71 #include <linux/slab.h>
72 #include <linux/init.h>
73 
74 
75 /* use 0 for production, 1 for verification, 2..7 for debug */
76 #ifndef NET_DEBUG
77 #define NET_DEBUG 1
78 #endif
79 static unsigned int net_debug = NET_DEBUG;
80 #define debug net_debug
81 
82 
83 /* A zero-terminated list of common I/O addresses to be probed. */
84 static unsigned int netcard_portlist[] __initdata =
85 	{ 0x300, 0x320, 0x340, 0x280, 0};
86 
87 /*
88   			Details of the i82586.
89 
90    You'll really need the databook to understand the details of this part,
91    but the outline is that the i82586 has two separate processing units.
92    Both are started from a list of three configuration tables, of which only
93    the last, the System Control Block (SCB), is used after reset-time.  The SCB
94    has the following fields:
95 		Status word
96 		Command word
97 		Tx/Command block addr.
98 		Rx block addr.
99    The command word accepts the following controls for the Tx and Rx units:
100   */
101 
102 #define	 CUC_START	 0x0100
103 #define	 CUC_RESUME	 0x0200
104 #define	 CUC_SUSPEND 0x0300
105 #define	 RX_START	 0x0010
106 #define	 RX_RESUME	 0x0020
107 #define	 RX_SUSPEND	 0x0030
108 
109 /* The Rx unit uses a list of frame descriptors and a list of data buffer
110    descriptors.  We use full-sized (1518 byte) data buffers, so there is
111    a one-to-one pairing of frame descriptors to buffer descriptors.
112 
113    The Tx ("command") unit executes a list of commands that look like:
114 		Status word		Written by the 82586 when the command is done.
115 		Command word	Command in lower 3 bits, post-command action in upper 3
116 		Link word		The address of the next command.
117 		Parameters		(as needed).
118 
119 	Some definitions related to the Command Word are:
120  */
121 #define CMD_EOL		0x8000			/* The last command of the list, stop. */
122 #define CMD_SUSP	0x4000			/* Suspend after doing cmd. */
123 #define CMD_INTR	0x2000			/* Interrupt after doing cmd. */
124 
125 enum commands {
126 	CmdNOp = 0, CmdSASetup = 1, CmdConfigure = 2, CmdMulticastList = 3,
127 	CmdTx = 4, CmdTDR = 5, CmdDump = 6, CmdDiagnose = 7};
128 
129 /* Information that need to be kept for each board. */
130 struct net_local {
131 	struct net_device_stats stats;
132 	int last_restart;
133 	ushort rx_head;
134 	ushort rx_tail;
135 	ushort tx_head;
136 	ushort tx_cmd_link;
137 	ushort tx_reap;
138 	ushort tx_pkts_in_ring;
139 	spinlock_t lock;
140 };
141 
142 /*
143   		Details of the EtherLink16 Implementation
144   The 3c507 is a generic shared-memory i82586 implementation.
145   The host can map 16K, 32K, 48K, or 64K of the 64K memory into
146   0x0[CD][08]0000, or all 64K into 0xF[02468]0000.
147   */
148 
149 /* Offsets from the base I/O address. */
150 #define	SA_DATA		0	/* Station address data, or 3Com signature. */
151 #define MISC_CTRL	6	/* Switch the SA_DATA banks, and bus config bits. */
152 #define RESET_IRQ	10	/* Reset the latched IRQ line. */
153 #define SIGNAL_CA	11	/* Frob the 82586 Channel Attention line. */
154 #define ROM_CONFIG	13
155 #define MEM_CONFIG	14
156 #define IRQ_CONFIG	15
157 #define EL16_IO_EXTENT 16
158 
159 /* The ID port is used at boot-time to locate the ethercard. */
160 #define ID_PORT		0x100
161 
162 /* Offsets to registers in the mailbox (SCB). */
163 #define iSCB_STATUS	0x8
164 #define iSCB_CMD		0xA
165 #define iSCB_CBL		0xC	/* Command BLock offset. */
166 #define iSCB_RFA		0xE	/* Rx Frame Area offset. */
167 
168 /*  Since the 3c507 maps the shared memory window so that the last byte is
169 	at 82586 address FFFF, the first byte is at 82586 address 0, 16K, 32K, or
170 	48K corresponding to window sizes of 64K, 48K, 32K and 16K respectively.
171 	We can account for this be setting the 'SBC Base' entry in the ISCP table
172 	below for all the 16 bit offset addresses, and also adding the 'SCB Base'
173 	value to all 24 bit physical addresses (in the SCP table and the TX and RX
174 	Buffer Descriptors).
175 					-Mark
176 	*/
177 #define SCB_BASE		((unsigned)64*1024 - (dev->mem_end - dev->mem_start))
178 
179 /*
180   What follows in 'init_words[]' is the "program" that is downloaded to the
181   82586 memory.	 It's mostly tables and command blocks, and starts at the
182   reset address 0xfffff6.  This is designed to be similar to the EtherExpress,
183   thus the unusual location of the SCB at 0x0008.
184 
185   Even with the additional "don't care" values, doing it this way takes less
186   program space than initializing the individual tables, and I feel it's much
187   cleaner.
188 
189   The databook is particularly useless for the first two structures, I had
190   to use the Crynwr driver as an example.
191 
192    The memory setup is as follows:
193    */
194 
195 #define CONFIG_CMD	0x0018
196 #define SET_SA_CMD	0x0024
197 #define SA_OFFSET	0x002A
198 #define IDLELOOP	0x30
199 #define TDR_CMD		0x38
200 #define TDR_TIME	0x3C
201 #define DUMP_CMD	0x40
202 #define DIAG_CMD	0x48
203 #define SET_MC_CMD	0x4E
204 #define DUMP_DATA	0x56	/* A 170 byte buffer for dump and Set-MC into. */
205 
206 #define TX_BUF_START	0x0100
207 #define NUM_TX_BUFS 	5
208 #define TX_BUF_SIZE 	(1518+14+20+16) /* packet+header+TBD */
209 
210 #define RX_BUF_START	0x2000
211 #define RX_BUF_SIZE 	(1518+14+18)	/* packet+header+RBD */
212 #define RX_BUF_END		(dev->mem_end - dev->mem_start)
213 
214 #define TX_TIMEOUT 5
215 
216 /*
217   That's it: only 86 bytes to set up the beast, including every extra
218   command available.  The 170 byte buffer at DUMP_DATA is shared between the
219   Dump command (called only by the diagnostic program) and the SetMulticastList
220   command.
221 
222   To complete the memory setup you only have to write the station address at
223   SA_OFFSET and create the Tx & Rx buffer lists.
224 
225   The Tx command chain and buffer list is setup as follows:
226   A Tx command table, with the data buffer pointing to...
227   A Tx data buffer descriptor.  The packet is in a single buffer, rather than
228 	chaining together several smaller buffers.
229   A NoOp command, which initially points to itself,
230   And the packet data.
231 
232   A transmit is done by filling in the Tx command table and data buffer,
233   re-writing the NoOp command, and finally changing the offset of the last
234   command to point to the current Tx command.  When the Tx command is finished,
235   it jumps to the NoOp, when it loops until the next Tx command changes the
236   "link offset" in the NoOp.  This way the 82586 never has to go through the
237   slow restart sequence.
238 
239   The Rx buffer list is set up in the obvious ring structure.  We have enough
240   memory (and low enough interrupt latency) that we can avoid the complicated
241   Rx buffer linked lists by alway associating a full-size Rx data buffer with
242   each Rx data frame.
243 
244   I current use four transmit buffers starting at TX_BUF_START (0x0100), and
245   use the rest of memory, from RX_BUF_START to RX_BUF_END, for Rx buffers.
246 
247   */
248 
249 static unsigned short init_words[] = {
250 	/*	System Configuration Pointer (SCP). */
251 	0x0000,					/* Set bus size to 16 bits. */
252 	0,0,					/* pad words. */
253 	0x0000,0x0000,			/* ISCP phys addr, set in init_82586_mem(). */
254 
255 	/*	Intermediate System Configuration Pointer (ISCP). */
256 	0x0001,					/* Status word that's cleared when init is done. */
257 	0x0008,0,0,				/* SCB offset, (skip, skip) */
258 
259 	/* System Control Block (SCB). */
260 	0,0xf000|RX_START|CUC_START,	/* SCB status and cmd. */
261 	CONFIG_CMD,				/* Command list pointer, points to Configure. */
262 	RX_BUF_START,				/* Rx block list. */
263 	0,0,0,0,				/* Error count: CRC, align, buffer, overrun. */
264 
265 	/* 0x0018: Configure command.  Change to put MAC data with packet. */
266 	0, CmdConfigure,		/* Status, command.		*/
267 	SET_SA_CMD,				/* Next command is Set Station Addr. */
268 	0x0804,					/* "4" bytes of config data, 8 byte FIFO. */
269 	0x2e40,					/* Magic values, including MAC data location. */
270 	0,						/* Unused pad word. */
271 
272 	/* 0x0024: Setup station address command. */
273 	0, CmdSASetup,
274 	SET_MC_CMD,				/* Next command. */
275 	0xaa00,0xb000,0x0bad,	/* Station address (to be filled in) */
276 
277 	/* 0x0030: NOP, looping back to itself.	 Point to first Tx buffer to Tx. */
278 	0, CmdNOp, IDLELOOP, 0 /* pad */,
279 
280 	/* 0x0038: A unused Time-Domain Reflectometer command. */
281 	0, CmdTDR, IDLELOOP, 0,
282 
283 	/* 0x0040: An unused Dump State command. */
284 	0, CmdDump, IDLELOOP, DUMP_DATA,
285 
286 	/* 0x0048: An unused Diagnose command. */
287 	0, CmdDiagnose, IDLELOOP,
288 
289 	/* 0x004E: An empty set-multicast-list command. */
290 	0, CmdMulticastList, IDLELOOP, 0,
291 };
292 
293 /* Index to functions, as function prototypes. */
294 
295 extern int el16_probe(struct net_device *dev);	/* Called from Space.c */
296 
297 static int	el16_probe1(struct net_device *dev, int ioaddr);
298 static int	el16_open(struct net_device *dev);
299 static int	el16_send_packet(struct sk_buff *skb, struct net_device *dev);
300 static void	el16_interrupt(int irq, void *dev_id, struct pt_regs *regs);
301 static void el16_rx(struct net_device *dev);
302 static int	el16_close(struct net_device *dev);
303 static struct net_device_stats *el16_get_stats(struct net_device *dev);
304 static void el16_tx_timeout (struct net_device *dev);
305 
306 static void hardware_send_packet(struct net_device *dev, void *buf, short length, short pad);
307 static void init_82586_mem(struct net_device *dev);
308 static struct ethtool_ops netdev_ethtool_ops;
309 static void init_rx_bufs(struct net_device *);
310 
311 
312 /* Check for a network adaptor of this type, and return '0' iff one exists.
313 	If dev->base_addr == 0, probe all likely locations.
314 	If dev->base_addr == 1, always return failure.
315 	If dev->base_addr == 2, (detachable devices only) allocate space for the
316 	device and return success.
317 	*/
318 
el16_probe(struct net_device * dev)319 int __init el16_probe(struct net_device *dev)
320 {
321 	int base_addr = dev->base_addr;
322 	int i;
323 
324 	SET_MODULE_OWNER(dev);
325 
326 	if (base_addr > 0x1ff)	/* Check a single specified location. */
327 		return el16_probe1(dev, base_addr);
328 	else if (base_addr != 0)
329 		return -ENXIO;		/* Don't probe at all. */
330 
331 	for (i = 0; netcard_portlist[i]; i++)
332 		if (el16_probe1(dev, netcard_portlist[i]) == 0)
333 			return 0;
334 
335 	return -ENODEV;
336 }
337 
el16_probe1(struct net_device * dev,int ioaddr)338 static int __init el16_probe1(struct net_device *dev, int ioaddr)
339 {
340 	static unsigned char init_ID_done, version_printed;
341 	int i, irq, irqval, retval;
342 	struct net_local *lp;
343 
344 	if (init_ID_done == 0) {
345 		ushort lrs_state = 0xff;
346 		/* Send the ID sequence to the ID_PORT to enable the board(s). */
347 		outb(0x00, ID_PORT);
348 		for(i = 0; i < 255; i++) {
349 			outb(lrs_state, ID_PORT);
350 			lrs_state <<= 1;
351 			if (lrs_state & 0x100)
352 				lrs_state ^= 0xe7;
353 		}
354 		outb(0x00, ID_PORT);
355 		init_ID_done = 1;
356 	}
357 
358 	if (!request_region(ioaddr, EL16_IO_EXTENT, dev->name))
359 		return -ENODEV;
360 
361 	if ((inb(ioaddr) != '*') || (inb(ioaddr + 1) != '3') ||
362 	    (inb(ioaddr + 2) != 'C') || (inb(ioaddr + 3) != 'O')) {
363 		retval = -ENODEV;
364 		goto out;
365 	}
366 
367 	if (net_debug  &&  version_printed++ == 0)
368 		printk(version);
369 
370 	printk("%s: 3c507 at %#x,", dev->name, ioaddr);
371 
372 	/* We should make a few more checks here, like the first three octets of
373 	   the S.A. for the manufacturer's code. */
374 
375 	irq = inb(ioaddr + IRQ_CONFIG) & 0x0f;
376 
377 	irqval = request_irq(irq, &el16_interrupt, 0, dev->name, dev);
378 	if (irqval) {
379 		printk ("unable to get IRQ %d (irqval=%d).\n", irq, irqval);
380 		retval = -EAGAIN;
381 		goto out;
382 	}
383 
384 	/* We've committed to using the board, and can start filling in *dev. */
385 	dev->base_addr = ioaddr;
386 
387 	outb(0x01, ioaddr + MISC_CTRL);
388 	for (i = 0; i < 6; i++) {
389 		dev->dev_addr[i] = inb(ioaddr + i);
390 		printk(" %02x", dev->dev_addr[i]);
391 	}
392 
393 	if ((dev->mem_start & 0xf) > 0)
394 		net_debug = dev->mem_start & 7;
395 
396 #ifdef MEM_BASE
397 	dev->mem_start = MEM_BASE;
398 	dev->mem_end = dev->mem_start + 0x10000;
399 #else
400 	{
401 		int base;
402 		int size;
403 		char mem_config = inb(ioaddr + MEM_CONFIG);
404 		if (mem_config & 0x20) {
405 			size = 64*1024;
406 			base = 0xf00000 + (mem_config & 0x08 ? 0x080000
407 							   : ((mem_config & 3) << 17));
408 		} else {
409 			size = ((mem_config & 3) + 1) << 14;
410 			base = 0x0c0000 + ( (mem_config & 0x18) << 12);
411 		}
412 		dev->mem_start = base;
413 		dev->mem_end = base + size;
414 	}
415 #endif
416 
417 	dev->if_port = (inb(ioaddr + ROM_CONFIG) & 0x80) ? 1 : 0;
418 	dev->irq = inb(ioaddr + IRQ_CONFIG) & 0x0f;
419 
420 	printk(", IRQ %d, %sternal xcvr, memory %#lx-%#lx.\n", dev->irq,
421 		   dev->if_port ? "ex" : "in", dev->mem_start, dev->mem_end-1);
422 
423 	if (net_debug)
424 		printk(version);
425 
426 	/* Initialize the device structure. */
427 	lp = dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
428 	if (dev->priv == NULL) {
429 		retval = -ENOMEM;
430 		goto out;
431 	}
432 	memset(dev->priv, 0, sizeof(struct net_local));
433 	spin_lock_init(&lp->lock);
434 
435 	dev->open		= el16_open;
436 	dev->stop		= el16_close;
437 	dev->hard_start_xmit = el16_send_packet;
438 	dev->get_stats	= el16_get_stats;
439 	dev->tx_timeout = el16_tx_timeout;
440 	dev->watchdog_timeo = TX_TIMEOUT;
441 	dev->ethtool_ops = &netdev_ethtool_ops;
442 
443 	ether_setup(dev);	/* Generic ethernet behaviour */
444 
445 	dev->flags&=~IFF_MULTICAST;	/* Multicast doesn't work */
446 
447 	return 0;
448 out:
449 	release_region(ioaddr, EL16_IO_EXTENT);
450 	return retval;
451 }
452 
el16_open(struct net_device * dev)453 static int el16_open(struct net_device *dev)
454 {
455 	/* Initialize the 82586 memory and start it. */
456 	init_82586_mem(dev);
457 
458 	netif_start_queue(dev);
459 	return 0;
460 }
461 
462 
el16_tx_timeout(struct net_device * dev)463 static void el16_tx_timeout (struct net_device *dev)
464 {
465 	struct net_local *lp = (struct net_local *) dev->priv;
466 	int ioaddr = dev->base_addr;
467 	unsigned long shmem = dev->mem_start;
468 
469 	if (net_debug > 1)
470 		printk ("%s: transmit timed out, %s?  ", dev->name,
471 			isa_readw (shmem + iSCB_STATUS) & 0x8000 ? "IRQ conflict" :
472 			"network cable problem");
473 	/* Try to restart the adaptor. */
474 	if (lp->last_restart == lp->stats.tx_packets) {
475 		if (net_debug > 1)
476 			printk ("Resetting board.\n");
477 		/* Completely reset the adaptor. */
478 		init_82586_mem (dev);
479 		lp->tx_pkts_in_ring = 0;
480 	} else {
481 		/* Issue the channel attention signal and hope it "gets better". */
482 		if (net_debug > 1)
483 			printk ("Kicking board.\n");
484 		isa_writew (0xf000 | CUC_START | RX_START, shmem + iSCB_CMD);
485 		outb (0, ioaddr + SIGNAL_CA);	/* Issue channel-attn. */
486 		lp->last_restart = lp->stats.tx_packets;
487 	}
488 	dev->trans_start = jiffies;
489 	netif_wake_queue (dev);
490 }
491 
492 
el16_send_packet(struct sk_buff * skb,struct net_device * dev)493 static int el16_send_packet (struct sk_buff *skb, struct net_device *dev)
494 {
495 	struct net_local *lp = (struct net_local *) dev->priv;
496 	int ioaddr = dev->base_addr;
497 	unsigned long flags;
498 	short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
499 	unsigned char *buf = skb->data;
500 
501 	netif_stop_queue (dev);
502 
503 	spin_lock_irqsave (&lp->lock, flags);
504 
505 	lp->stats.tx_bytes += length;
506 	/* Disable the 82586's input to the interrupt line. */
507 	outb (0x80, ioaddr + MISC_CTRL);
508 
509 	hardware_send_packet (dev, buf, skb->len, length - skb->len);
510 
511 	dev->trans_start = jiffies;
512 	/* Enable the 82586 interrupt input. */
513 	outb (0x84, ioaddr + MISC_CTRL);
514 
515 	spin_unlock_irqrestore (&lp->lock, flags);
516 
517 	dev_kfree_skb (skb);
518 
519 	/* You might need to clean up and record Tx statistics here. */
520 
521 	return 0;
522 }
523 
524 /*	The typical workload of the driver:
525 	Handle the network interface interrupts. */
el16_interrupt(int irq,void * dev_id,struct pt_regs * regs)526 static void el16_interrupt(int irq, void *dev_id, struct pt_regs *regs)
527 {
528 	struct net_device *dev = dev_id;
529 	struct net_local *lp;
530 	int ioaddr, status, boguscount = 0;
531 	ushort ack_cmd = 0;
532 	unsigned long shmem;
533 
534 	if (dev == NULL) {
535 		printk ("net_interrupt(): irq %d for unknown device.\n", irq);
536 		return;
537 	}
538 
539 	ioaddr = dev->base_addr;
540 	lp = (struct net_local *)dev->priv;
541 	shmem = dev->mem_start;
542 
543 	spin_lock(&lp->lock);
544 
545 	status = isa_readw(shmem+iSCB_STATUS);
546 
547 	if (net_debug > 4) {
548 		printk("%s: 3c507 interrupt, status %4.4x.\n", dev->name, status);
549 	}
550 
551 	/* Disable the 82586's input to the interrupt line. */
552 	outb(0x80, ioaddr + MISC_CTRL);
553 
554 	/* Reap the Tx packet buffers. */
555 	while (lp->tx_pkts_in_ring) {
556 	  unsigned short tx_status = isa_readw(shmem+lp->tx_reap);
557 	  if (!(tx_status & 0x8000)) {
558 		if (net_debug > 5)
559 			printk("Tx command incomplete (%#x).\n", lp->tx_reap);
560 		break;
561 	  }
562 	  /* Tx unsuccessful or some interesting status bit set. */
563 	  if (!(tx_status & 0x2000) || (tx_status & 0x0f3f)) {
564 		lp->stats.tx_errors++;
565 		if (tx_status & 0x0600)  lp->stats.tx_carrier_errors++;
566 		if (tx_status & 0x0100)  lp->stats.tx_fifo_errors++;
567 		if (!(tx_status & 0x0040))  lp->stats.tx_heartbeat_errors++;
568 		if (tx_status & 0x0020)  lp->stats.tx_aborted_errors++;
569 		lp->stats.collisions += tx_status & 0xf;
570 	  }
571 	  lp->stats.tx_packets++;
572 	  if (net_debug > 5)
573 		  printk("Reaped %x, Tx status %04x.\n" , lp->tx_reap, tx_status);
574 	  lp->tx_reap += TX_BUF_SIZE;
575 	  if (lp->tx_reap > RX_BUF_START - TX_BUF_SIZE)
576 		lp->tx_reap = TX_BUF_START;
577 
578 	  lp->tx_pkts_in_ring--;
579 	  /* There is always more space in the Tx ring buffer now. */
580 	  netif_wake_queue(dev);
581 
582 	  if (++boguscount > 10)
583 		break;
584 	}
585 
586 	if (status & 0x4000) { /* Packet received. */
587 		if (net_debug > 5)
588 			printk("Received packet, rx_head %04x.\n", lp->rx_head);
589 		el16_rx(dev);
590 	}
591 
592 	/* Acknowledge the interrupt sources. */
593 	ack_cmd = status & 0xf000;
594 
595 	if ((status & 0x0700) != 0x0200 && netif_running(dev)) {
596 		if (net_debug)
597 			printk("%s: Command unit stopped, status %04x, restarting.\n",
598 				   dev->name, status);
599 		/* If this ever occurs we should really re-write the idle loop, reset
600 		   the Tx list, and do a complete restart of the command unit.
601 		   For now we rely on the Tx timeout if the resume doesn't work. */
602 		ack_cmd |= CUC_RESUME;
603 	}
604 
605 	if ((status & 0x0070) != 0x0040 && netif_running(dev)) {
606 		/* The Rx unit is not ready, it must be hung.  Restart the receiver by
607 		   initializing the rx buffers, and issuing an Rx start command. */
608 		if (net_debug)
609 			printk("%s: Rx unit stopped, status %04x, restarting.\n",
610 				   dev->name, status);
611 		init_rx_bufs(dev);
612 		isa_writew(RX_BUF_START,shmem+iSCB_RFA);
613 		ack_cmd |= RX_START;
614 	}
615 
616 	isa_writew(ack_cmd,shmem+iSCB_CMD);
617 	outb(0, ioaddr + SIGNAL_CA);			/* Issue channel-attn. */
618 
619 	/* Clear the latched interrupt. */
620 	outb(0, ioaddr + RESET_IRQ);
621 
622 	/* Enable the 82586's interrupt input. */
623 	outb(0x84, ioaddr + MISC_CTRL);
624 	spin_unlock(&lp->lock);
625 }
626 
el16_close(struct net_device * dev)627 static int el16_close(struct net_device *dev)
628 {
629 	int ioaddr = dev->base_addr;
630 	unsigned long shmem = dev->mem_start;
631 
632 	netif_stop_queue(dev);
633 
634 	/* Flush the Tx and disable Rx. */
635 	isa_writew(RX_SUSPEND | CUC_SUSPEND,shmem+iSCB_CMD);
636 	outb(0, ioaddr + SIGNAL_CA);
637 
638 	/* Disable the 82586's input to the interrupt line. */
639 	outb(0x80, ioaddr + MISC_CTRL);
640 
641 	/* We always physically use the IRQ line, so we don't do free_irq(). */
642 
643 	/* Update the statistics here. */
644 
645 	return 0;
646 }
647 
648 /* Get the current statistics.	This may be called with the card open or
649    closed. */
el16_get_stats(struct net_device * dev)650 static struct net_device_stats *el16_get_stats(struct net_device *dev)
651 {
652 	struct net_local *lp = (struct net_local *)dev->priv;
653 
654 	/* ToDo: decide if there are any useful statistics from the SCB. */
655 
656 	return &lp->stats;
657 }
658 
659 /* Initialize the Rx-block list. */
init_rx_bufs(struct net_device * dev)660 static void init_rx_bufs(struct net_device *dev)
661 {
662 	struct net_local *lp = (struct net_local *)dev->priv;
663 	unsigned long write_ptr;
664 	unsigned short SCB_base = SCB_BASE;
665 
666 	int cur_rxbuf = lp->rx_head = RX_BUF_START;
667 
668 	/* Initialize each Rx frame + data buffer. */
669 	do {	/* While there is room for one more. */
670 
671 	  write_ptr = dev->mem_start + cur_rxbuf;
672 
673 		isa_writew(0x0000,write_ptr);			/* Status */
674 		isa_writew(0x0000,write_ptr+=2);			/* Command */
675 		isa_writew(cur_rxbuf + RX_BUF_SIZE,write_ptr+=2);	/* Link */
676 		isa_writew(cur_rxbuf + 22,write_ptr+=2);		/* Buffer offset */
677 		isa_writew(0x0000,write_ptr+=2);			/* Pad for dest addr. */
678 		isa_writew(0x0000,write_ptr+=2);
679 		isa_writew(0x0000,write_ptr+=2);
680 		isa_writew(0x0000,write_ptr+=2);			/* Pad for source addr. */
681 		isa_writew(0x0000,write_ptr+=2);
682 		isa_writew(0x0000,write_ptr+=2);
683 		isa_writew(0x0000,write_ptr+=2);			/* Pad for protocol. */
684 
685 		isa_writew(0x0000,write_ptr+=2);			/* Buffer: Actual count */
686 		isa_writew(-1,write_ptr+=2);			/* Buffer: Next (none). */
687 		isa_writew(cur_rxbuf + 0x20 + SCB_base,write_ptr+=2);/* Buffer: Address low */
688 		isa_writew(0x0000,write_ptr+=2);
689 		/* Finally, the number of bytes in the buffer. */
690 		isa_writew(0x8000 + RX_BUF_SIZE-0x20,write_ptr+=2);
691 
692 		lp->rx_tail = cur_rxbuf;
693 		cur_rxbuf += RX_BUF_SIZE;
694 	} while (cur_rxbuf <= RX_BUF_END - RX_BUF_SIZE);
695 
696 	/* Terminate the list by setting the EOL bit, and wrap the pointer to make
697 	   the list a ring. */
698 	write_ptr = dev->mem_start + lp->rx_tail + 2;
699 	isa_writew(0xC000,write_ptr);				/* Command, mark as last. */
700 	isa_writew(lp->rx_head,write_ptr+2);			/* Link */
701 }
702 
init_82586_mem(struct net_device * dev)703 static void init_82586_mem(struct net_device *dev)
704 {
705 	struct net_local *lp = (struct net_local *)dev->priv;
706 	short ioaddr = dev->base_addr;
707 	unsigned long shmem = dev->mem_start;
708 
709 	/* Enable loopback to protect the wire while starting up,
710 	   and hold the 586 in reset during the memory initialization. */
711 	outb(0x20, ioaddr + MISC_CTRL);
712 
713 	/* Fix the ISCP address and base. */
714 	init_words[3] = SCB_BASE;
715 	init_words[7] = SCB_BASE;
716 
717 	/* Write the words at 0xfff6 (address-aliased to 0xfffff6). */
718 	isa_memcpy_toio(dev->mem_end-10, init_words, 10);
719 
720 	/* Write the words at 0x0000. */
721 	isa_memcpy_toio(dev->mem_start, init_words + 5, sizeof(init_words) - 10);
722 
723 	/* Fill in the station address. */
724 	isa_memcpy_toio(dev->mem_start+SA_OFFSET, dev->dev_addr,
725 		   sizeof(dev->dev_addr));
726 
727 	/* The Tx-block list is written as needed.  We just set up the values. */
728 	lp->tx_cmd_link = IDLELOOP + 4;
729 	lp->tx_head = lp->tx_reap = TX_BUF_START;
730 
731 	init_rx_bufs(dev);
732 
733 	/* Start the 586 by releasing the reset line, but leave loopback. */
734 	outb(0xA0, ioaddr + MISC_CTRL);
735 
736 	/* This was time consuming to track down: you need to give two channel
737 	   attention signals to reliably start up the i82586. */
738 	outb(0, ioaddr + SIGNAL_CA);
739 
740 	{
741 		int boguscnt = 50;
742 		while (isa_readw(shmem+iSCB_STATUS) == 0)
743 			if (--boguscnt == 0) {
744 				printk("%s: i82586 initialization timed out with status %04x,"
745 					   "cmd %04x.\n", dev->name,
746 					   isa_readw(shmem+iSCB_STATUS), isa_readw(shmem+iSCB_CMD));
747 				break;
748 			}
749 		/* Issue channel-attn -- the 82586 won't start. */
750 		outb(0, ioaddr + SIGNAL_CA);
751 	}
752 
753 	/* Disable loopback and enable interrupts. */
754 	outb(0x84, ioaddr + MISC_CTRL);
755 	if (net_debug > 4)
756 		printk("%s: Initialized 82586, status %04x.\n", dev->name,
757 			   isa_readw(shmem+iSCB_STATUS));
758 	return;
759 }
760 
hardware_send_packet(struct net_device * dev,void * buf,short length,short pad)761 static void hardware_send_packet(struct net_device *dev, void *buf, short length, short pad)
762 {
763 	struct net_local *lp = (struct net_local *)dev->priv;
764 	short ioaddr = dev->base_addr;
765 	ushort tx_block = lp->tx_head;
766 	unsigned long write_ptr = dev->mem_start + tx_block;
767 	static char padding[ETH_ZLEN];
768 
769 	/* Set the write pointer to the Tx block, and put out the header. */
770 	isa_writew(0x0000,write_ptr);			/* Tx status */
771 	isa_writew(CMD_INTR|CmdTx,write_ptr+=2);		/* Tx command */
772 	isa_writew(tx_block+16,write_ptr+=2);		/* Next command is a NoOp. */
773 	isa_writew(tx_block+8,write_ptr+=2);			/* Data Buffer offset. */
774 
775 	/* Output the data buffer descriptor. */
776 	isa_writew((pad + length) | 0x8000,write_ptr+=2);		/* Byte count parameter. */
777 	isa_writew(-1,write_ptr+=2);			/* No next data buffer. */
778 	isa_writew(tx_block+22+SCB_BASE,write_ptr+=2);	/* Buffer follows the NoOp command. */
779 	isa_writew(0x0000,write_ptr+=2);			/* Buffer address high bits (always zero). */
780 
781 	/* Output the Loop-back NoOp command. */
782 	isa_writew(0x0000,write_ptr+=2);			/* Tx status */
783 	isa_writew(CmdNOp,write_ptr+=2);			/* Tx command */
784 	isa_writew(tx_block+16,write_ptr+=2);		/* Next is myself. */
785 
786 	/* Output the packet at the write pointer. */
787 	isa_memcpy_toio(write_ptr+2, buf, length);
788 	if(pad)
789 		isa_memcpy_toio(write_ptr+length+2, padding, pad);
790 
791 	/* Set the old command link pointing to this send packet. */
792 	isa_writew(tx_block,dev->mem_start + lp->tx_cmd_link);
793 	lp->tx_cmd_link = tx_block + 20;
794 
795 	/* Set the next free tx region. */
796 	lp->tx_head = tx_block + TX_BUF_SIZE;
797 	if (lp->tx_head > RX_BUF_START - TX_BUF_SIZE)
798 		lp->tx_head = TX_BUF_START;
799 
800 	if (net_debug > 4) {
801 		printk("%s: 3c507 @%x send length = %d, tx_block %3x, next %3x.\n",
802 			   dev->name, ioaddr, length, tx_block, lp->tx_head);
803 	}
804 
805 	/* Grimly block further packets if there has been insufficient reaping. */
806 	if (++lp->tx_pkts_in_ring < NUM_TX_BUFS)
807 		netif_wake_queue(dev);
808 }
809 
el16_rx(struct net_device * dev)810 static void el16_rx(struct net_device *dev)
811 {
812 	struct net_local *lp = (struct net_local *)dev->priv;
813 	unsigned long shmem = dev->mem_start;
814 	ushort rx_head = lp->rx_head;
815 	ushort rx_tail = lp->rx_tail;
816 	ushort boguscount = 10;
817 	short frame_status;
818 
819 	while ((frame_status = isa_readw(shmem+rx_head)) < 0) {   /* Command complete */
820 		unsigned long read_frame = dev->mem_start + rx_head;
821 		ushort rfd_cmd = isa_readw(read_frame+2);
822 		ushort next_rx_frame = isa_readw(read_frame+4);
823 		ushort data_buffer_addr = isa_readw(read_frame+6);
824 		unsigned long data_frame = dev->mem_start + data_buffer_addr;
825 		ushort pkt_len = isa_readw(data_frame);
826 
827 		if (rfd_cmd != 0 || data_buffer_addr != rx_head + 22
828 			|| (pkt_len & 0xC000) != 0xC000) {
829 			printk("%s: Rx frame at %#x corrupted, status %04x cmd %04x"
830 				   "next %04x data-buf @%04x %04x.\n", dev->name, rx_head,
831 				   frame_status, rfd_cmd, next_rx_frame, data_buffer_addr,
832 				   pkt_len);
833 		} else if ((frame_status & 0x2000) == 0) {
834 			/* Frame Rxed, but with error. */
835 			lp->stats.rx_errors++;
836 			if (frame_status & 0x0800) lp->stats.rx_crc_errors++;
837 			if (frame_status & 0x0400) lp->stats.rx_frame_errors++;
838 			if (frame_status & 0x0200) lp->stats.rx_fifo_errors++;
839 			if (frame_status & 0x0100) lp->stats.rx_over_errors++;
840 			if (frame_status & 0x0080) lp->stats.rx_length_errors++;
841 		} else {
842 			/* Malloc up new buffer. */
843 			struct sk_buff *skb;
844 
845 			pkt_len &= 0x3fff;
846 			skb = dev_alloc_skb(pkt_len+2);
847 			if (skb == NULL) {
848 				printk("%s: Memory squeeze, dropping packet.\n", dev->name);
849 				lp->stats.rx_dropped++;
850 				break;
851 			}
852 
853 			skb_reserve(skb,2);
854 			skb->dev = dev;
855 
856 			/* 'skb->data' points to the start of sk_buff data area. */
857 			isa_memcpy_fromio(skb_put(skb,pkt_len), data_frame + 10, pkt_len);
858 
859 			skb->protocol=eth_type_trans(skb,dev);
860 			netif_rx(skb);
861 			dev->last_rx = jiffies;
862 			lp->stats.rx_packets++;
863 			lp->stats.rx_bytes += pkt_len;
864 		}
865 
866 		/* Clear the status word and set End-of-List on the rx frame. */
867 		isa_writew(0,read_frame);
868 		isa_writew(0xC000,read_frame+2);
869 		/* Clear the end-of-list on the prev. RFD. */
870 		isa_writew(0x0000,dev->mem_start + rx_tail + 2);
871 
872 		rx_tail = rx_head;
873 		rx_head = next_rx_frame;
874 		if (--boguscount == 0)
875 			break;
876 	}
877 
878 	lp->rx_head = rx_head;
879 	lp->rx_tail = rx_tail;
880 }
881 
netdev_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)882 static void netdev_get_drvinfo(struct net_device *dev,
883 			       struct ethtool_drvinfo *info)
884 {
885 	strcpy(info->driver, DRV_NAME);
886 	strcpy(info->version, DRV_VERSION);
887 	sprintf(info->bus_info, "ISA 0x%lx", dev->base_addr);
888 }
889 
netdev_get_msglevel(struct net_device * dev)890 static u32 netdev_get_msglevel(struct net_device *dev)
891 {
892 	return debug;
893 }
894 
netdev_set_msglevel(struct net_device * dev,u32 level)895 static void netdev_set_msglevel(struct net_device *dev, u32 level)
896 {
897 	debug = level;
898 }
899 
900 static struct ethtool_ops netdev_ethtool_ops = {
901 	.get_drvinfo		= netdev_get_drvinfo,
902 	.get_msglevel		= netdev_get_msglevel,
903 	.set_msglevel		= netdev_set_msglevel,
904 };
905 
906 #ifdef MODULE
907 static struct net_device dev_3c507;
908 static int io = 0x300;
909 static int irq;
910 MODULE_PARM(io, "i");
911 MODULE_PARM(irq, "i");
912 MODULE_PARM_DESC(io, "EtherLink16 I/O base address");
913 MODULE_PARM_DESC(irq, "(ignored)");
914 
init_module(void)915 int init_module(void)
916 {
917 	if (io == 0)
918 		printk("3c507: You should not use auto-probing with insmod!\n");
919 	dev_3c507.base_addr = io;
920 	dev_3c507.irq       = irq;
921 	dev_3c507.init	    = el16_probe;
922 	if (register_netdev(&dev_3c507) != 0) {
923 		printk("3c507: register_netdev() returned non-zero.\n");
924 		return -EIO;
925 	}
926 	return 0;
927 }
928 
929 void
cleanup_module(void)930 cleanup_module(void)
931 {
932 	unregister_netdev(&dev_3c507);
933 	kfree(dev_3c507.priv);
934 	dev_3c507.priv = NULL;
935 
936 	/* If we don't do this, we can't re-insmod it later. */
937 	free_irq(dev_3c507.irq, &dev_3c507);
938 	release_region(dev_3c507.base_addr, EL16_IO_EXTENT);
939 }
940 #endif /* MODULE */
941 MODULE_LICENSE("GPL");
942 
943 
944 /*
945  * Local variables:
946  *  compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -I/usr/src/linux/drivers/net -Wall -Wstrict-prototypes -O6 -m486 -c 3c507.c"
947  *  version-control: t
948  *  kept-new-versions: 5
949  *  tab-width: 4
950  *  c-indent-level: 4
951  * End:
952  */
953