1 /* ne2.c: A NE/2 Ethernet Driver for Linux. */
2 /*
3    Based on the NE2000 driver written by Donald Becker (1992-94).
4    modified by Wim Dumon (Apr 1996)
5 
6    This software may be used and distributed according to the terms
7    of the GNU General Public License, incorporated herein by reference.
8 
9    The author may be reached as wimpie@linux.cc.kuleuven.ac.be
10 
11    Currently supported: NE/2
12    This patch was never tested on other MCA-ethernet adapters, but it
13    might work. Just give it a try and let me know if you have problems.
14    Also mail me if it really works, please!
15 
16    Changelog:
17    Mon Feb  3 16:26:02 MET 1997
18    - adapted the driver to work with the 2.1.25 kernel
19    - multiple ne2 support (untested)
20    - module support (untested)
21 
22    Fri Aug 28 00:18:36 CET 1998 (David Weinehall)
23    - fixed a few minor typos
24    - made the MODULE_PARM conditional (it only works with the v2.1.x kernels)
25    - fixed the module support (Now it's working...)
26 
27    Mon Sep  7 19:01:44 CET 1998 (David Weinehall)
28    - added support for Arco Electronics AE/2-card (experimental)
29 
30    Mon Sep 14 09:53:42 CET 1998 (David Weinehall)
31    - added support for Compex ENET-16MC/P (experimental)
32 
33    Tue Sep 15 16:21:12 CET 1998 (David Weinehall, Magnus Jonsson, Tomas Ogren)
34    - Miscellaneous bugfixes
35 
36    Tue Sep 19 16:21:12 CET 1998 (Magnus Jonsson)
37    - Cleanup
38 
39    Wed Sep 23 14:33:34 CET 1998 (David Weinehall)
40    - Restructuring and rewriting for v2.1.x compliance
41 
42    Wed Oct 14 17:19:21 CET 1998 (David Weinehall)
43    - Added code that unregisters irq and proc-info
44    - Version# bump
45 
46    Mon Nov 16 15:28:23 CET 1998 (Wim Dumon)
47    - pass 'dev' as last parameter of request_irq in stead of 'NULL'
48 
49    Wed Feb  7 21:24:00 CET 2001 (Alfred Arnold)
50    - added support for the D-Link DE-320CT
51 
52    *    WARNING
53 	-------
54 	This is alpha-test software.  It is not guaranteed to work. As a
55 	matter of fact, I'm quite sure there are *LOTS* of bugs in here. I
56 	would like to hear from you if you use this driver, even if it works.
57 	If it doesn't work, be sure to send me a mail with the problems !
58 */
59 
60 static const char *version = "ne2.c:v0.91 Nov 16 1998 Wim Dumon <wimpie@kotnet.org>\n";
61 
62 #include <linux/module.h>
63 #include <linux/version.h>
64 
65 #include <linux/kernel.h>
66 #include <linux/sched.h>
67 #include <linux/types.h>
68 #include <linux/fcntl.h>
69 #include <linux/interrupt.h>
70 #include <linux/ptrace.h>
71 #include <linux/ioport.h>
72 #include <linux/in.h>
73 #include <linux/slab.h>
74 #include <linux/string.h>
75 #include <asm/system.h>
76 #include <asm/bitops.h>
77 #include <asm/io.h>
78 #include <asm/dma.h>
79 #include <linux/errno.h>
80 #include <linux/init.h>
81 #include <linux/mca.h>
82 
83 #include <linux/netdevice.h>
84 #include <linux/etherdevice.h>
85 #include <linux/skbuff.h>
86 #include "8390.h"
87 
88 
89 
90 /* Some defines that people can play with if so inclined. */
91 
92 /* Do we perform extra sanity checks on stuff ? */
93 /* #define NE_SANITY_CHECK */
94 
95 /* Do we implement the read before write bugfix ? */
96 /* #define NE_RW_BUGFIX */
97 
98 /* Do we have a non std. amount of memory? (in units of 256 byte pages) */
99 /* #define PACKETBUF_MEMSIZE	0x40 */
100 
101 
102 /* ---- No user-serviceable parts below ---- */
103 
104 #define NE_BASE	 (dev->base_addr)
105 #define NE_CMD	 	0x00
106 #define NE_DATAPORT	0x10	/* NatSemi-defined port window offset. */
107 #define NE_RESET	0x20	/* Issue a read to reset, a write to clear. */
108 #define NE_IO_EXTENT	0x30
109 
110 #define NE1SM_START_PG	0x20	/* First page of TX buffer */
111 #define NE1SM_STOP_PG 	0x40	/* Last page +1 of RX ring */
112 #define NESM_START_PG	0x40	/* First page of TX buffer */
113 #define NESM_STOP_PG	0x80	/* Last page +1 of RX ring */
114 
115 /* From the .ADF file: */
116 static unsigned int addresses[7] __initdata =
117 		{0x1000, 0x2020, 0x8020, 0xa0a0, 0xb0b0, 0xc0c0, 0xc3d0};
118 static int irqs[4] __initdata = {3, 4, 5, 9};
119 
120 /* From the D-Link ADF file: */
121 static unsigned int dlink_addresses[4] __initdata =
122                 {0x300, 0x320, 0x340, 0x360};
123 static int dlink_irqs[8] __initdata = {3, 4, 5, 9, 10, 11, 14, 15};
124 
125 struct ne2_adapters_t {
126 	unsigned int	id;
127 	char		*name;
128 };
129 
130 static struct ne2_adapters_t ne2_adapters[] __initdata = {
131 	{ 0x6354, "Arco Ethernet Adapter AE/2" },
132 	{ 0x70DE, "Compex ENET-16 MC/P" },
133 	{ 0x7154, "Novell Ethernet Adapter NE/2" },
134         { 0x56ea, "D-Link DE-320CT" },
135 	{ 0x0000, NULL }
136 };
137 
138 extern int netcard_probe(struct net_device *dev);
139 
140 static int ne2_probe1(struct net_device *dev, int slot);
141 
142 static int ne_open(struct net_device *dev);
143 static int ne_close(struct net_device *dev);
144 
145 static void ne_reset_8390(struct net_device *dev);
146 static void ne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
147 		int ring_page);
148 static void ne_block_input(struct net_device *dev, int count,
149 		struct sk_buff *skb, int ring_offset);
150 static void ne_block_output(struct net_device *dev, const int count,
151 		const unsigned char *buf, const int start_page);
152 
153 
154 /*
155  * special code to read the DE-320's MAC address EEPROM.  In contrast to a
156  * standard NE design, this is a serial EEPROM (93C46) that has to be read
157  * bit by bit.  The EEPROM cotrol port at base + 0x1e has the following
158  * layout:
159  *
160  * Bit 0 = Data out (read from EEPROM)
161  * Bit 1 = Data in  (write to EEPROM)
162  * Bit 2 = Clock
163  * Bit 3 = Chip Select
164  * Bit 7 = ~50 kHz clock for defined delays
165  *
166  */
167 
dlink_put_eeprom(unsigned char value,unsigned int addr)168 static void __init dlink_put_eeprom(unsigned char value, unsigned int addr)
169 {
170 	int z;
171 	unsigned char v1, v2;
172 
173 	/* write the value to the NIC EEPROM register */
174 
175 	outb(value, addr + 0x1e);
176 
177 	/* now wait the clock line to toggle twice.  Effectively, we are
178 	   waiting (at least) for one clock cycle */
179 
180 	for (z = 0; z < 2; z++) {
181 		do {
182 			v1 = inb(addr + 0x1e);
183 			v2 = inb(addr + 0x1e);
184 		}
185 		while (!((v1 ^ v2) & 0x80));
186 	}
187 }
188 
dlink_send_eeprom_bit(unsigned int bit,unsigned int addr)189 static void __init dlink_send_eeprom_bit(unsigned int bit, unsigned int addr)
190 {
191 	/* shift data bit into correct position */
192 
193 	bit = bit << 1;
194 
195 	/* write value, keep clock line high for two cycles */
196 
197 	dlink_put_eeprom(0x09 | bit, addr);
198 	dlink_put_eeprom(0x0d | bit, addr);
199 	dlink_put_eeprom(0x0d | bit, addr);
200 	dlink_put_eeprom(0x09 | bit, addr);
201 }
202 
dlink_send_eeprom_word(unsigned int value,unsigned int len,unsigned int addr)203 static void __init dlink_send_eeprom_word(unsigned int value, unsigned int len, unsigned int addr)
204 {
205 	int z;
206 
207 	/* adjust bits so that they are left-aligned in a 16-bit-word */
208 
209 	value = value << (16 - len);
210 
211 	/* shift bits out to the EEPROM */
212 
213 	for (z = 0; z < len; z++) {
214 		dlink_send_eeprom_bit((value & 0x8000) >> 15, addr);
215 		value = value << 1;
216 	}
217 }
218 
dlink_get_eeprom(unsigned int eeaddr,unsigned int addr)219 static unsigned int __init dlink_get_eeprom(unsigned int eeaddr, unsigned int addr)
220 {
221 	int z;
222 	unsigned int value = 0;
223 
224 	/* pull the CS line low for a moment.  This resets the EEPROM-
225 	   internal logic, and makes it ready for a new command. */
226 
227 	dlink_put_eeprom(0x01, addr);
228 	dlink_put_eeprom(0x09, addr);
229 
230 	/* send one start bit, read command (1 - 0), plus the address to
231            the EEPROM */
232 
233 	dlink_send_eeprom_word(0x0180 | (eeaddr & 0x3f), 9, addr);
234 
235 	/* get the data word.  We clock by sending 0s to the EEPROM, which
236 	   get ignored during the read process */
237 
238 	for (z = 0; z < 16; z++) {
239 		dlink_send_eeprom_bit(0, addr);
240 		value = (value << 1) | (inb(addr + 0x1e) & 0x01);
241 	}
242 
243 	return value;
244 }
245 
246 /*
247  * Note that at boot, this probe only picks up one card at a time.
248  */
249 
ne2_probe(struct net_device * dev)250 int __init ne2_probe(struct net_device *dev)
251 {
252 	static int current_mca_slot = -1;
253 	int i;
254 	int adapter_found = 0;
255 
256 	SET_MODULE_OWNER(dev);
257 
258 	/* Do not check any supplied i/o locations.
259 	   POS registers usually don't fail :) */
260 
261 	/* MCA cards have POS registers.
262 	   Autodetecting MCA cards is extremely simple.
263 	   Just search for the card. */
264 
265 	for(i = 0; (ne2_adapters[i].name != NULL) && !adapter_found; i++) {
266 		current_mca_slot =
267 			mca_find_unused_adapter(ne2_adapters[i].id, 0);
268 
269 		if((current_mca_slot != MCA_NOTFOUND) && !adapter_found) {
270 			mca_set_adapter_name(current_mca_slot,
271 					ne2_adapters[i].name);
272 			mca_mark_as_used(current_mca_slot);
273 
274 			return ne2_probe1(dev, current_mca_slot);
275 		}
276 	}
277 	return -ENODEV;
278 }
279 
280 
ne2_procinfo(char * buf,int slot,struct net_device * dev)281 static int ne2_procinfo(char *buf, int slot, struct net_device *dev)
282 {
283 	int len=0;
284 
285 	len += sprintf(buf+len, "The NE/2 Ethernet Adapter\n" );
286 	len += sprintf(buf+len, "Driver written by Wim Dumon ");
287 	len += sprintf(buf+len, "<wimpie@kotnet.org>\n");
288 	len += sprintf(buf+len, "Modified by ");
289 	len += sprintf(buf+len, "David Weinehall <tao@acc.umu.se>\n");
290 	len += sprintf(buf+len, "and by Magnus Jonsson <bigfoot@acc.umu.se>\n");
291 	len += sprintf(buf+len, "Based on the original NE2000 drivers\n" );
292 	len += sprintf(buf+len, "Base IO: %#x\n", (unsigned int)dev->base_addr);
293 	len += sprintf(buf+len, "IRQ    : %d\n", dev->irq);
294 
295 #define HW_ADDR(i) dev->dev_addr[i]
296 	len += sprintf(buf+len, "HW addr : %x:%x:%x:%x:%x:%x\n",
297 			HW_ADDR(0), HW_ADDR(1), HW_ADDR(2),
298 			HW_ADDR(3), HW_ADDR(4), HW_ADDR(5) );
299 #undef HW_ADDR
300 
301 	return len;
302 }
303 
ne2_probe1(struct net_device * dev,int slot)304 static int __init ne2_probe1(struct net_device *dev, int slot)
305 {
306 	int i, base_addr, irq, retval;
307 	unsigned char POS;
308 	unsigned char SA_prom[32];
309 	const char *name = "NE/2";
310 	int start_page, stop_page;
311 	static unsigned version_printed;
312 
313 	if (ei_debug && version_printed++ == 0)
314 		printk(version);
315 
316 	printk("NE/2 ethercard found in slot %d:", slot);
317 
318 	/* Read base IO and IRQ from the POS-registers */
319 	POS = mca_read_stored_pos(slot, 2);
320 	if(!(POS % 2)) {
321 		printk(" disabled.\n");
322 		return -ENODEV;
323 	}
324 
325 	/* handle different POS register structure for D-Link card */
326 
327 	if (mca_read_stored_pos(slot, 0) == 0xea) {
328 		base_addr = dlink_addresses[(POS >> 5) & 0x03];
329 		irq = dlink_irqs[(POS >> 2) & 0x07];
330 	}
331         else {
332 		i = (POS & 0xE)>>1;
333 		/* printk("Halleluja sdog, als er na de pijl een 1 staat is 1 - 1 == 0"
334 	   	" en zou het moeten werken -> %d\n", i);
335 	   	The above line was for remote testing, thanx to sdog ... */
336 		base_addr = addresses[i - 1];
337 		irq = irqs[(POS & 0x60)>>5];
338 	}
339 
340 	if (!request_region(base_addr, NE_IO_EXTENT, dev->name))
341 		return -EBUSY;
342 
343 #ifdef DEBUG
344 	printk("POS info : pos 2 = %#x ; base = %#x ; irq = %ld\n", POS,
345 			base_addr, irq);
346 #endif
347 
348 #ifndef CRYNWR_WAY
349 	/* Reset the card the way they do it in the Crynwr packet driver */
350 	for (i=0; i<8; i++)
351 		outb(0x0, base_addr + NE_RESET);
352 	inb(base_addr + NE_RESET);
353 	outb(0x21, base_addr + NE_CMD);
354 	if (inb(base_addr + NE_CMD) != 0x21) {
355 		printk("NE/2 adapter not responding\n");
356 		retval = -ENODEV;
357 		goto out;
358 	}
359 
360 	/* In the crynwr sources they do a RAM-test here. I skip it. I suppose
361 	   my RAM is okay.  Suppose your memory is broken.  Then this test
362 	   should fail and you won't be able to use your card.  But if I do not
363 	   test, you won't be able to use your card, neither.  So this test
364 	   won't help you. */
365 
366 #else  /* _I_ never tested it this way .. Go ahead and try ...*/
367 	/* Reset card. Who knows what dain-bramaged state it was left in. */
368 	{
369 		unsigned long reset_start_time = jiffies;
370 
371 		/* DON'T change these to inb_p/outb_p or reset will fail on
372 		   clones.. */
373 		outb(inb(base_addr + NE_RESET), base_addr + NE_RESET);
374 
375 		while ((inb_p(base_addr + EN0_ISR) & ENISR_RESET) == 0)
376 			if (jiffies - reset_start_time > 2*HZ/100) {
377 				printk(" not found (no reset ack).\n");
378 				retval = -ENODEV;
379 				goto out;
380 			}
381 
382 		outb_p(0xff, base_addr + EN0_ISR);         /* Ack all intr. */
383 	}
384 #endif
385 
386 
387 	/* Read the 16 bytes of station address PROM.
388 	   We must first initialize registers, similar to
389 	   NS8390_init(eifdev, 0).
390 	   We can't reliably read the SAPROM address without this.
391 	   (I learned the hard way!). */
392 	{
393 		struct {
394 			unsigned char value, offset;
395 		} program_seq[] = {
396 						/* Select page 0 */
397 			{E8390_NODMA+E8390_PAGE0+E8390_STOP, E8390_CMD},
398 			{0x49,	EN0_DCFG},  /* Set WORD-wide (0x49) access. */
399 			{0x00,	EN0_RCNTLO},  /* Clear the count regs. */
400 			{0x00,	EN0_RCNTHI},
401 			{0x00,	EN0_IMR},  /* Mask completion irq. */
402 			{0xFF,	EN0_ISR},
403 			{E8390_RXOFF, EN0_RXCR},  /* 0x20  Set to monitor */
404 			{E8390_TXOFF, EN0_TXCR},  /* 0x02  and loopback mode. */
405 			{32,	EN0_RCNTLO},
406 			{0x00,	EN0_RCNTHI},
407 			{0x00,	EN0_RSARLO},  /* DMA starting at 0x0000. */
408 			{0x00,	EN0_RSARHI},
409 			{E8390_RREAD+E8390_START, E8390_CMD},
410 		};
411 
412 		for (i = 0; i < sizeof(program_seq)/sizeof(program_seq[0]); i++)
413 			outb_p(program_seq[i].value, base_addr +
414 				program_seq[i].offset);
415 
416 	}
417 	for(i = 0; i < 6 /*sizeof(SA_prom)*/; i+=1) {
418 		SA_prom[i] = inb(base_addr + NE_DATAPORT);
419 	}
420 
421 	/* I don't know whether the previous sequence includes the general
422            board reset procedure, so better don't omit it and just overwrite
423            the garbage read from a DE-320 with correct stuff. */
424 
425 	if (mca_read_stored_pos(slot, 0) == 0xea) {
426 		unsigned int v;
427 
428 		for (i = 0; i < 3; i++) {
429  			v = dlink_get_eeprom(i, base_addr);
430 			SA_prom[(i << 1)    ] = v & 0xff;
431 			SA_prom[(i << 1) + 1] = (v >> 8) & 0xff;
432 		}
433 	}
434 
435 	start_page = NESM_START_PG;
436 	stop_page = NESM_STOP_PG;
437 
438 	dev->irq=irq;
439 
440 	/* Snarf the interrupt now.  There's no point in waiting since we cannot
441 	   share and the board will usually be enabled. */
442 	retval = request_irq(dev->irq, ei_interrupt, 0, dev->name, dev);
443 	if (retval) {
444 		printk (" unable to get IRQ %d (irqval=%d).\n",
445 				dev->irq, retval);
446 		goto out;
447 	}
448 
449 	dev->base_addr = base_addr;
450 
451 	/* Allocate dev->priv and fill in 8390 specific dev fields. */
452 	if (ethdev_init(dev)) {
453 		printk (" unable to get memory for dev->priv.\n");
454 		free_irq(dev->irq, dev);
455 		retval = -ENOMEM;
456 		goto out;
457 	}
458 
459 	for(i = 0; i < ETHER_ADDR_LEN; i++) {
460 		printk(" %2.2x", SA_prom[i]);
461 		dev->dev_addr[i] = SA_prom[i];
462 	}
463 
464 	printk("\n%s: %s found at %#x, using IRQ %d.\n",
465 			dev->name, name, base_addr, dev->irq);
466 
467 	mca_set_adapter_procfn(slot, (MCA_ProcFn) ne2_procinfo, dev);
468 
469 	ei_status.name = name;
470 	ei_status.tx_start_page = start_page;
471 	ei_status.stop_page = stop_page;
472 	ei_status.word16 = (2 == 2);
473 
474 	ei_status.rx_start_page = start_page + TX_PAGES;
475 #ifdef PACKETBUF_MEMSIZE
476 	/* Allow the packet buffer size to be overridden by know-it-alls. */
477 	ei_status.stop_page = ei_status.tx_start_page + PACKETBUF_MEMSIZE;
478 #endif
479 
480 	ei_status.reset_8390 = &ne_reset_8390;
481 	ei_status.block_input = &ne_block_input;
482 	ei_status.block_output = &ne_block_output;
483 	ei_status.get_8390_hdr = &ne_get_8390_hdr;
484 
485 	ei_status.priv = slot;
486 
487 	dev->open = &ne_open;
488 	dev->stop = &ne_close;
489 	NS8390_init(dev, 0);
490 	return 0;
491 out:
492 	release_region(base_addr, NE_IO_EXTENT);
493 	return retval;
494 }
495 
ne_open(struct net_device * dev)496 static int ne_open(struct net_device *dev)
497 {
498 	ei_open(dev);
499 	return 0;
500 }
501 
ne_close(struct net_device * dev)502 static int ne_close(struct net_device *dev)
503 {
504 	if (ei_debug > 1)
505 		printk("%s: Shutting down ethercard.\n", dev->name);
506 	ei_close(dev);
507 	return 0;
508 }
509 
510 /* Hard reset the card.  This used to pause for the same period that a
511    8390 reset command required, but that shouldn't be necessary. */
ne_reset_8390(struct net_device * dev)512 static void ne_reset_8390(struct net_device *dev)
513 {
514 	unsigned long reset_start_time = jiffies;
515 
516 	if (ei_debug > 1)
517 		printk("resetting the 8390 t=%ld...", jiffies);
518 
519 	/* DON'T change these to inb_p/outb_p or reset will fail on clones. */
520 	outb(inb(NE_BASE + NE_RESET), NE_BASE + NE_RESET);
521 
522 	ei_status.txing = 0;
523 	ei_status.dmaing = 0;
524 
525 	/* This check _should_not_ be necessary, omit eventually. */
526 	while ((inb_p(NE_BASE+EN0_ISR) & ENISR_RESET) == 0)
527 		if (jiffies - reset_start_time > 2*HZ/100) {
528 			printk("%s: ne_reset_8390() did not complete.\n",
529 					dev->name);
530 			break;
531 		}
532 	outb_p(ENISR_RESET, NE_BASE + EN0_ISR);	/* Ack intr. */
533 }
534 
535 /* Grab the 8390 specific header. Similar to the block_input routine, but
536    we don't need to be concerned with ring wrap as the header will be at
537    the start of a page, so we optimize accordingly. */
538 
ne_get_8390_hdr(struct net_device * dev,struct e8390_pkt_hdr * hdr,int ring_page)539 static void ne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
540 		int ring_page)
541 {
542 
543 	int nic_base = dev->base_addr;
544 
545 	/* This *shouldn't* happen.
546 	   If it does, it's the last thing you'll see */
547 	if (ei_status.dmaing) {
548 		printk("%s: DMAing conflict in ne_get_8390_hdr "
549 				"[DMAstat:%d][irqlock:%d].\n",
550 				dev->name, ei_status.dmaing, ei_status.irqlock);
551 		return;
552 	}
553 
554 	ei_status.dmaing |= 0x01;
555 	outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
556 	outb_p(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
557 	outb_p(0, nic_base + EN0_RCNTHI);
558 	outb_p(0, nic_base + EN0_RSARLO);		/* On page boundary */
559 	outb_p(ring_page, nic_base + EN0_RSARHI);
560 	outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
561 
562 	if (ei_status.word16)
563 		insw(NE_BASE + NE_DATAPORT, hdr,
564 				sizeof(struct e8390_pkt_hdr)>>1);
565 	else
566 		insb(NE_BASE + NE_DATAPORT, hdr,
567 				sizeof(struct e8390_pkt_hdr));
568 
569 	outb_p(ENISR_RDC, nic_base + EN0_ISR);	/* Ack intr. */
570 	ei_status.dmaing &= ~0x01;
571 }
572 
573 /* Block input and output, similar to the Crynwr packet driver.  If you
574    are porting to a new ethercard, look at the packet driver source for
575    hints. The NEx000 doesn't share the on-board packet memory -- you have
576    to put the packet out through the "remote DMA" dataport using outb. */
577 
ne_block_input(struct net_device * dev,int count,struct sk_buff * skb,int ring_offset)578 static void ne_block_input(struct net_device *dev, int count, struct sk_buff *skb,
579 		int ring_offset)
580 {
581 #ifdef NE_SANITY_CHECK
582 	int xfer_count = count;
583 #endif
584 	int nic_base = dev->base_addr;
585 	char *buf = skb->data;
586 
587 	/* This *shouldn't* happen.
588 	   If it does, it's the last thing you'll see */
589 	if (ei_status.dmaing) {
590 		printk("%s: DMAing conflict in ne_block_input "
591 				"[DMAstat:%d][irqlock:%d].\n",
592 				dev->name, ei_status.dmaing, ei_status.irqlock);
593 		return;
594 	}
595 	ei_status.dmaing |= 0x01;
596 	outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
597 	outb_p(count & 0xff, nic_base + EN0_RCNTLO);
598 	outb_p(count >> 8, nic_base + EN0_RCNTHI);
599 	outb_p(ring_offset & 0xff, nic_base + EN0_RSARLO);
600 	outb_p(ring_offset >> 8, nic_base + EN0_RSARHI);
601 	outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
602 	if (ei_status.word16) {
603 		insw(NE_BASE + NE_DATAPORT,buf,count>>1);
604 		if (count & 0x01) {
605 			buf[count-1] = inb(NE_BASE + NE_DATAPORT);
606 #ifdef NE_SANITY_CHECK
607 			xfer_count++;
608 #endif
609 		}
610 	} else {
611 		insb(NE_BASE + NE_DATAPORT, buf, count);
612 	}
613 
614 #ifdef NE_SANITY_CHECK
615 	/* This was for the ALPHA version only, but enough people have
616 	   been encountering problems so it is still here.  If you see
617 	   this message you either 1) have a slightly incompatible clone
618 	   or 2) have noise/speed problems with your bus. */
619 	if (ei_debug > 1) {	/* DMA termination address check... */
620 		int addr, tries = 20;
621 		do {
622 			/* DON'T check for 'inb_p(EN0_ISR) & ENISR_RDC' here
623 			   -- it's broken for Rx on some cards! */
624 			int high = inb_p(nic_base + EN0_RSARHI);
625 			int low = inb_p(nic_base + EN0_RSARLO);
626 			addr = (high << 8) + low;
627 			if (((ring_offset + xfer_count) & 0xff) == low)
628 				break;
629 		} while (--tries > 0);
630 		if (tries <= 0)
631 			printk("%s: RX transfer address mismatch,"
632 				"%#4.4x (expected) vs. %#4.4x (actual).\n",
633 				dev->name, ring_offset + xfer_count, addr);
634 	}
635 #endif
636 	outb_p(ENISR_RDC, nic_base + EN0_ISR);	/* Ack intr. */
637 	ei_status.dmaing &= ~0x01;
638 }
639 
ne_block_output(struct net_device * dev,int count,const unsigned char * buf,const int start_page)640 static void ne_block_output(struct net_device *dev, int count,
641 		const unsigned char *buf, const int start_page)
642 {
643 	int nic_base = NE_BASE;
644 	unsigned long dma_start;
645 #ifdef NE_SANITY_CHECK
646 	int retries = 0;
647 #endif
648 
649 	/* Round the count up for word writes. Do we need to do this?
650 	   What effect will an odd byte count have on the 8390?
651 	   I should check someday. */
652 	if (ei_status.word16 && (count & 0x01))
653 		count++;
654 
655 	/* This *shouldn't* happen.
656 	   If it does, it's the last thing you'll see */
657 	if (ei_status.dmaing) {
658 		printk("%s: DMAing conflict in ne_block_output."
659 				"[DMAstat:%d][irqlock:%d]\n",
660 				dev->name, ei_status.dmaing, ei_status.irqlock);
661 		return;
662 	}
663 	ei_status.dmaing |= 0x01;
664 	/* We should already be in page 0, but to be safe... */
665 	outb_p(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
666 
667 #ifdef NE_SANITY_CHECK
668 retry:
669 #endif
670 
671 #ifdef NE8390_RW_BUGFIX
672 	/* Handle the read-before-write bug the same way as the
673 	   Crynwr packet driver -- the NatSemi method doesn't work.
674 	   Actually this doesn't always work either, but if you have
675 	   problems with your NEx000 this is better than nothing! */
676 	outb_p(0x42, nic_base + EN0_RCNTLO);
677 	outb_p(0x00, nic_base + EN0_RCNTHI);
678 	outb_p(0x42, nic_base + EN0_RSARLO);
679 	outb_p(0x00, nic_base + EN0_RSARHI);
680 	outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
681 	/* Make certain that the dummy read has occurred. */
682 	SLOW_DOWN_IO;
683 	SLOW_DOWN_IO;
684 	SLOW_DOWN_IO;
685 #endif
686 
687 	outb_p(ENISR_RDC, nic_base + EN0_ISR);
688 
689 	/* Now the normal output. */
690 	outb_p(count & 0xff, nic_base + EN0_RCNTLO);
691 	outb_p(count >> 8,   nic_base + EN0_RCNTHI);
692 	outb_p(0x00, nic_base + EN0_RSARLO);
693 	outb_p(start_page, nic_base + EN0_RSARHI);
694 
695 	outb_p(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
696 	if (ei_status.word16) {
697 		outsw(NE_BASE + NE_DATAPORT, buf, count>>1);
698 	} else {
699 		outsb(NE_BASE + NE_DATAPORT, buf, count);
700 	}
701 
702 	dma_start = jiffies;
703 
704 #ifdef NE_SANITY_CHECK
705 	/* This was for the ALPHA version only, but enough people have
706 	   been encountering problems so it is still here. */
707 
708 	if (ei_debug > 1) {		/* DMA termination address check... */
709 		int addr, tries = 20;
710 		do {
711 			int high = inb_p(nic_base + EN0_RSARHI);
712 			int low = inb_p(nic_base + EN0_RSARLO);
713 			addr = (high << 8) + low;
714 			if ((start_page << 8) + count == addr)
715 				break;
716 		} while (--tries > 0);
717 		if (tries <= 0) {
718 			printk("%s: Tx packet transfer address mismatch,"
719 					"%#4.4x (expected) vs. %#4.4x (actual).\n",
720 					dev->name, (start_page << 8) + count, addr);
721 			if (retries++ == 0)
722 				goto retry;
723 		}
724 	}
725 #endif
726 
727 	while ((inb_p(nic_base + EN0_ISR) & ENISR_RDC) == 0)
728 		if (jiffies - dma_start > 2*HZ/100) {		/* 20ms */
729 			printk("%s: timeout waiting for Tx RDC.\n", dev->name);
730 			ne_reset_8390(dev);
731 			NS8390_init(dev,1);
732 			break;
733 		}
734 
735 	outb_p(ENISR_RDC, nic_base + EN0_ISR);	/* Ack intr. */
736 	ei_status.dmaing &= ~0x01;
737 	return;
738 }
739 
740 
741 #ifdef MODULE
742 #define MAX_NE_CARDS	4	/* Max number of NE cards per module */
743 static struct net_device dev_ne[MAX_NE_CARDS];
744 static int io[MAX_NE_CARDS];
745 static int irq[MAX_NE_CARDS];
746 static int bad[MAX_NE_CARDS];	/* 0xbad = bad sig or no reset ack */
747 MODULE_LICENSE("GPL");
748 
749 #ifdef MODULE_PARM
750 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_NE_CARDS) "i");
751 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_NE_CARDS) "i");
752 MODULE_PARM(bad, "1-" __MODULE_STRING(MAX_NE_CARDS) "i");
753 MODULE_PARM_DESC(io, "(ignored)");
754 MODULE_PARM_DESC(irq, "(ignored)");
755 MODULE_PARM_DESC(bad, "(ignored)");
756 #endif
757 
758 /* Module code fixed by David Weinehall */
759 
init_module(void)760 int init_module(void)
761 {
762 	int this_dev, found = 0;
763 
764 	for (this_dev = 0; this_dev < MAX_NE_CARDS; this_dev++) {
765 		struct net_device *dev = &dev_ne[this_dev];
766 		dev->irq = irq[this_dev];
767 		dev->mem_end = bad[this_dev];
768 		dev->base_addr = io[this_dev];
769 		dev->init = ne2_probe;
770 		if (register_netdev(dev) != 0) {
771 			if (found != 0) return 0;   /* Got at least one. */
772 
773 			printk(KERN_WARNING "ne2.c: No NE/2 card found.\n");
774 			return -ENXIO;
775 		}
776 		found++;
777 	}
778 	return 0;
779 }
780 
cleanup_module(void)781 void cleanup_module(void)
782 {
783 	int this_dev;
784 
785 	for (this_dev = 0; this_dev < MAX_NE_CARDS; this_dev++) {
786 		struct net_device *dev = &dev_ne[this_dev];
787 		if (dev->priv != NULL) {
788 			mca_mark_as_unused(ei_status.priv);
789 			mca_set_adapter_procfn( ei_status.priv, NULL, NULL);
790 			kfree(dev->priv);
791 			free_irq(dev->irq, dev);
792 			release_region(dev->base_addr, NE_IO_EXTENT);
793 			unregister_netdev(dev);
794 		}
795 	}
796 }
797 #endif /* MODULE */
798 
799 /*
800  * Local variables:
801  *  compile-command: "gcc -DKERNEL -Wall -O6 -fomit-frame-pointer -I/usr/src/linux/net/tcp -c ne2.c"
802  *  version-control: t
803  *  kept-new-versions: 5
804  * End:
805  */
806