xref: /openbsd/sys/dev/isa/if_ie.c (revision 78b63d65)
1 /*	$OpenBSD: if_ie.c,v 1.23 2001/11/06 19:53:19 miod Exp $	*/
2 /*	$NetBSD: if_ie.c,v 1.51 1996/05/12 23:52:48 mycroft Exp $	*/
3 
4 /*-
5  * Copyright (c) 1993, 1994, 1995 Charles Hannum.
6  * Copyright (c) 1992, 1993, University of Vermont and State
7  *  Agricultural College.
8  * Copyright (c) 1992, 1993, Garrett A. Wollman.
9  *
10  * Portions:
11  * Copyright (c) 1993, 1994, 1995, Rodney W. Grimes
12  * Copyright (c) 1994, 1995, Rafal K. Boni
13  * Copyright (c) 1990, 1991, William F. Jolitz
14  * Copyright (c) 1990, The Regents of the University of California
15  *
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  * 3. All advertising materials mentioning features or use of this software
27  *    must display the following acknowledgement:
28  *	This product includes software developed by Charles Hannum, by the
29  *	University of Vermont and State Agricultural College and Garrett A.
30  *	Wollman, by William F. Jolitz, and by the University of California,
31  *	Berkeley, Lawrence Berkeley Laboratory, and its contributors.
32  * 4. Neither the names of the Universities nor the names of the authors
33  *    may be used to endorse or promote products derived from this software
34  *    without specific prior written permission.
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
37  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR AUTHORS BE LIABLE
40  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  */
48 
49 /*
50  * Intel 82586 Ethernet chip
51  * Register, bit, and structure definitions.
52  *
53  * Original StarLAN driver written by Garrett Wollman with reference to the
54  * Clarkson Packet Driver code for this chip written by Russ Nelson and others.
55  *
56  * BPF support code taken from hpdev/if_le.c, supplied with tcpdump.
57  *
58  * 3C507 support is loosely based on code donated to NetBSD by Rafal Boni.
59  *
60  * Intel EtherExpress 16 support taken from FreeBSD's if_ix.c, written
61  * by Rodney W. Grimes.
62  *
63  * Majorly cleaned up and 3C507 code merged by Charles Hannum.
64  */
65 
66 /*
67  * The i82586 is a very versatile chip, found in many implementations.
68  * Programming this chip is mostly the same, but certain details differ
69  * from card to card.  This driver is written so that different cards
70  * can be automatically detected at run-time.
71  */
72 
73 /*
74 Mode of operation:
75 
76 We run the 82586 in a standard Ethernet mode.  We keep NFRAMES received frame
77 descriptors around for the receiver to use, and NRXBUF associated receive
78 buffer descriptors, both in a circular list.  Whenever a frame is received, we
79 rotate both lists as necessary.  (The 586 treats both lists as a simple
80 queue.)  We also keep a transmit command around so that packets can be sent
81 off quickly.
82 
83 We configure the adapter in AL-LOC = 1 mode, which means that the
84 Ethernet/802.3 MAC header is placed at the beginning of the receive buffer
85 rather than being split off into various fields in the RFD.  This also means
86 that we must include this header in the transmit buffer as well.
87 
88 By convention, all transmit commands, and only transmit commands, shall have
89 the I (IE_CMD_INTR) bit set in the command.  This way, when an interrupt
90 arrives at ieintr(), it is immediately possible to tell what precisely caused
91 it.  ANY OTHER command-sending routines should run at splnet(), and should
92 post an acknowledgement to every interrupt they generate.
93 
94 The 82586 has a 24-bit address space internally, and the adaptor's memory is
95 located at the top of this region.  However, the value we are given in
96 configuration is the CPU's idea of where the adaptor RAM is.  So, we must go
97 through a few gyrations to come up with a kernel virtual address which
98 represents the actual beginning of the 586 address space.  First, we autosize
99 the RAM by running through several possible sizes and trying to initialize the
100 adapter under the assumption that the selected size is correct.  Then, knowing
101 the correct RAM size, we set up our pointers in the softc.  `sc_maddr'
102 represents the computed base of the 586 address space.  `iomembot' represents
103 the actual configured base of adapter RAM.  Finally, `sc_msize' represents the
104 calculated size of 586 RAM.  Then, when laying out commands, we use the
105 interval [sc_maddr, sc_maddr + sc_msize); to make 24-pointers, we subtract
106 iomem, and to make 16-pointers, we subtract sc_maddr and and with 0xffff.
107 */
108 
109 #include "bpfilter.h"
110 
111 #include <sys/param.h>
112 #include <sys/systm.h>
113 #include <sys/mbuf.h>
114 #include <sys/buf.h>
115 #include <sys/protosw.h>
116 #include <sys/socket.h>
117 #include <sys/ioctl.h>
118 #include <sys/errno.h>
119 #include <sys/syslog.h>
120 #include <sys/device.h>
121 #include <sys/timeout.h>
122 
123 #include <net/if.h>
124 #include <net/if_types.h>
125 #include <net/if_dl.h>
126 #include <net/netisr.h>
127 #include <net/route.h>
128 
129 #if NBPFILTER > 0
130 #include <net/bpf.h>
131 #include <net/bpfdesc.h>
132 #endif
133 
134 #ifdef INET
135 #include <netinet/in.h>
136 #include <netinet/in_systm.h>
137 #include <netinet/in_var.h>
138 #include <netinet/ip.h>
139 #include <netinet/if_ether.h>
140 #endif
141 
142 #include <uvm/uvm_extern.h>
143 
144 #include <machine/cpu.h>
145 #include <machine/pio.h>		/* XXX convert this driver! */
146 #include <machine/bus.h>
147 #include <machine/intr.h>
148 
149 #include <dev/isa/isareg.h>
150 #include <dev/isa/isavar.h>
151 #include <i386/isa/isa_machdep.h>	/* XXX USES ISA HOLE DIRECTLY */
152 #include <dev/ic/i82586reg.h>
153 #include <dev/isa/if_ieatt.h>
154 #include <dev/isa/if_ie507.h>
155 #include <dev/isa/if_iee16.h>
156 #include <dev/isa/elink.h>
157 
158 #define	IED_RINT	0x01
159 #define	IED_TINT	0x02
160 #define	IED_RNR		0x04
161 #define	IED_CNA		0x08
162 #define	IED_READFRAME	0x10
163 #define	IED_ENQ		0x20
164 #define	IED_XMIT	0x40
165 #define	IED_ALL		0x7f
166 
167 /*
168 sizeof(iscp) == 1+1+2+4 == 8
169 sizeof(scb) == 2+2+2+2+2+2+2+2 == 16
170 NFRAMES * sizeof(rfd) == NFRAMES*(2+2+2+2+6+6+2+2) == NFRAMES*24 == 384
171 sizeof(xmit_cmd) == 2+2+2+2+6+2 == 18
172 sizeof(transmit buffer) == ETHER_MAX_LEN == 1518
173 sizeof(transmit buffer desc) == 8
174 -----
175 1952
176 
177 NRXBUF * sizeof(rbd) == NRXBUF*(2+2+4+2+2) == NRXBUF*12
178 NRXBUF * IE_RBUF_SIZE == NRXBUF*256
179 
180 NRXBUF should be (16384 - 1952) / (256 + 12) == 14432 / 268 == 53
181 
182 With NRXBUF == 48, this leaves us 1568 bytes for another command or
183 more buffers.  Another transmit command would be 18+8+1518 == 1544
184 ---just barely fits!
185 
186 Obviously all these would have to be reduced for smaller memory sizes.
187 With a larger memory, it would be possible to roughly double the number of
188 both transmit and receive buffers.
189 */
190 
191 #define	NFRAMES		16		/* number of receive frames */
192 #define	NRXBUF		48		/* number of buffers to allocate */
193 #define	IE_RBUF_SIZE	256		/* size of each receive buffer;
194 						MUST BE POWER OF TWO */
195 #define	NTXBUF		2		/* number of transmit commands */
196 #define	IE_TBUF_SIZE	ETHER_MAX_LEN	/* length of transmit buffer */
197 
198 
199 enum ie_hardware {
200 	IE_STARLAN10,
201 	IE_EN100,
202 	IE_SLFIBER,
203 	IE_3C507,
204 	IE_EE16,
205 	IE_UNKNOWN
206 };
207 
208 const char *ie_hardware_names[] = {
209 	"StarLAN 10",
210 	"EN100",
211 	"StarLAN Fiber",
212 	"3C507",
213 	"EtherExpress 16",
214 	"Unknown"
215 };
216 
217 /*
218  * Ethernet status, per interface.
219  */
220 struct ie_softc {
221 	struct device sc_dev;
222 	void *sc_ih;
223 
224 	int sc_iobase;
225 	caddr_t sc_maddr;
226 	u_int sc_msize;
227 
228 	struct arpcom sc_arpcom;
229 
230 	void (*reset_586) __P((struct ie_softc *));
231 	void (*chan_attn) __P((struct ie_softc *));
232 
233 	enum ie_hardware hard_type;
234 	int hard_vers;
235 
236 	int want_mcsetup;
237 	int promisc;
238 	volatile struct ie_int_sys_conf_ptr *iscp;
239 	volatile struct ie_sys_ctl_block *scb;
240 
241 	int rfhead, rftail, rbhead, rbtail;
242 	volatile struct ie_recv_frame_desc *rframes[NFRAMES];
243 	volatile struct ie_recv_buf_desc *rbuffs[NRXBUF];
244 	volatile char *cbuffs[NRXBUF];
245 
246 	int xmit_busy;
247 	int xchead, xctail;
248 	volatile struct ie_xmit_cmd *xmit_cmds[NTXBUF];
249 	volatile struct ie_xmit_buf *xmit_buffs[NTXBUF];
250 	u_char *xmit_cbuffs[NTXBUF];
251 
252 	struct ie_en_addr mcast_addrs[MAXMCAST + 1];
253 	int mcast_count;
254 
255 	u_short	irq_encoded;		/* encoded interrupt on IEE16 */
256 
257 #ifdef IEDEBUG
258 	int sc_debug;
259 #endif
260 };
261 
262 void iewatchdog __P((struct ifnet *));
263 int ieintr __P((void *));
264 void iestop __P((struct ie_softc *));
265 int ieinit __P((struct ie_softc *));
266 int ieioctl __P((struct ifnet *, u_long, caddr_t));
267 void iestart __P((struct ifnet *));
268 static void el_reset_586 __P((struct ie_softc *));
269 static void sl_reset_586 __P((struct ie_softc *));
270 static void el_chan_attn __P((struct ie_softc *));
271 static void sl_chan_attn __P((struct ie_softc *));
272 static void slel_get_address __P((struct ie_softc *));
273 
274 static void ee16_reset_586 __P((struct ie_softc *));
275 static void ee16_chan_attn __P((struct ie_softc *));
276 static void ee16_interrupt_enable __P((struct ie_softc *));
277 void ee16_eeprom_outbits __P((struct ie_softc *, int, int));
278 void ee16_eeprom_clock __P((struct ie_softc *, int));
279 u_short ee16_read_eeprom __P((struct ie_softc *, int));
280 int ee16_eeprom_inbits __P((struct ie_softc *));
281 
282 void iereset __P((struct ie_softc *));
283 void ie_readframe __P((struct ie_softc *, int));
284 void ie_drop_packet_buffer __P((struct ie_softc *));
285 void ie_find_mem_size __P((struct ie_softc *));
286 static int command_and_wait __P((struct ie_softc *, int,
287     void volatile *, int));
288 void ierint __P((struct ie_softc *));
289 void ietint __P((struct ie_softc *));
290 void iexmit __P((struct ie_softc *));
291 struct mbuf *ieget __P((struct ie_softc *,
292     struct ether_header *, int *));
293 void iememinit __P((void *, struct ie_softc *));
294 static int mc_setup __P((struct ie_softc *, void *));
295 static void mc_reset __P((struct ie_softc *));
296 
297 #ifdef IEDEBUG
298 void print_rbd __P((volatile struct ie_recv_buf_desc *));
299 
300 int in_ierint = 0;
301 int in_ietint = 0;
302 #endif
303 
304 int	ieprobe __P((struct device *, void *, void *));
305 void	ieattach __P((struct device *, struct device *, void *));
306 int	sl_probe __P((struct ie_softc *, struct isa_attach_args *));
307 int	el_probe __P((struct ie_softc *, struct isa_attach_args *));
308 int	ee16_probe __P((struct ie_softc *, struct isa_attach_args *));
309 int	check_ie_present __P((struct ie_softc *, caddr_t, u_int));
310 
311 static __inline void ie_setup_config __P((volatile struct ie_config_cmd *,
312     int, int));
313 static __inline void ie_ack __P((struct ie_softc *, u_int));
314 static __inline int ether_equal __P((u_char *, u_char *));
315 static __inline int check_eh __P((struct ie_softc *, struct ether_header *,
316     int *));
317 static __inline int ie_buflen __P((struct ie_softc *, int));
318 static __inline int ie_packet_len __P((struct ie_softc *));
319 
320 static void run_tdr __P((struct ie_softc *, struct ie_tdr_cmd *));
321 
322 struct cfattach ie_isa_ca = {
323 	sizeof(struct ie_softc), ieprobe, ieattach
324 };
325 
326 struct cfdriver ie_cd = {
327 	NULL, "ie", DV_IFNET
328 };
329 
330 #define MK_24(base, ptr) ((caddr_t)((u_long)ptr - (u_long)base))
331 #define MK_16(base, ptr) ((u_short)(u_long)MK_24(base, ptr))
332 
333 #define PORT	sc->sc_iobase
334 #define MEM 	sc->sc_maddr
335 
336 /*
337  * Here are a few useful functions.  We could have done these as macros, but
338  * since we have the inline facility, it makes sense to use that instead.
339  */
340 static __inline void
341 ie_setup_config(cmd, promiscuous, manchester)
342 	volatile struct ie_config_cmd *cmd;
343 	int promiscuous, manchester;
344 {
345 
346 	cmd->ie_config_count = 0x0c;
347 	cmd->ie_fifo = 8;
348 	cmd->ie_save_bad = 0x40;
349 	cmd->ie_addr_len = 0x2e;
350 	cmd->ie_priority = 0;
351 	cmd->ie_ifs = 0x60;
352 	cmd->ie_slot_low = 0;
353 	cmd->ie_slot_high = 0xf2;
354 	cmd->ie_promisc = promiscuous | manchester << 2;
355 	cmd->ie_crs_cdt = 0;
356 	cmd->ie_min_len = 64;
357 	cmd->ie_junk = 0xff;
358 }
359 
360 static __inline void
361 ie_ack(sc, mask)
362 	struct ie_softc *sc;
363 	u_int mask;
364 {
365 	volatile struct ie_sys_ctl_block *scb = sc->scb;
366 
367 	scb->ie_command = scb->ie_status & mask;
368 	(sc->chan_attn)(sc);
369 
370 	while (scb->ie_command)
371 		;		/* Spin Lock */
372 }
373 
374 int
375 ieprobe(parent, match, aux)
376 	struct device *parent;
377 	void *match, *aux;
378 {
379 	struct ie_softc *sc = match;
380 	struct isa_attach_args *ia = aux;
381 
382 	if (sl_probe(sc, ia))
383 		return 1;
384 	if (el_probe(sc, ia))
385 		return 1;
386 	if (ee16_probe(sc, ia))
387 		return 1;
388 	return 0;
389 }
390 
391 int
392 sl_probe(sc, ia)
393 	struct ie_softc *sc;
394 	struct isa_attach_args *ia;
395 {
396 	u_char c;
397 
398 	sc->sc_iobase = ia->ia_iobase;
399 
400 	/* Need this for part of the probe. */
401 	sc->reset_586 = sl_reset_586;
402 	sc->chan_attn = sl_chan_attn;
403 
404 	c = inb(PORT + IEATT_REVISION);
405 	switch (SL_BOARD(c)) {
406 	case SL10_BOARD:
407 		sc->hard_type = IE_STARLAN10;
408 		break;
409 	case EN100_BOARD:
410 		sc->hard_type = IE_EN100;
411 		break;
412 	case SLFIBER_BOARD:
413 		sc->hard_type = IE_SLFIBER;
414 		break;
415 
416 	default:
417 		/* Anything else is not recognized or cannot be used. */
418 #if 0
419 		printf("%s: unknown AT&T board type code %d\n",
420 		    sc->sc_dev.dv_xname, SL_BOARD(c));
421 #endif
422 		return 0;
423 	}
424 
425 	sc->hard_vers = SL_REV(c);
426 
427 	if (ia->ia_irq == IRQUNK || ia->ia_maddr == MADDRUNK) {
428 		printf("%s: %s does not have soft configuration\n",
429 		    sc->sc_dev.dv_xname, ie_hardware_names[sc->hard_type]);
430 		return 0;
431 	}
432 
433 	/*
434 	 * Divine memory size on-board the card.  Ususally 16k.
435 	 */
436 	sc->sc_maddr = ISA_HOLE_VADDR(ia->ia_maddr);
437 	ie_find_mem_size(sc);
438 
439 	if (!sc->sc_msize) {
440 		printf("%s: can't find shared memory\n", sc->sc_dev.dv_xname);
441 		return 0;
442 	}
443 
444 	if (!ia->ia_msize)
445 		ia->ia_msize = sc->sc_msize;
446 	else if (ia->ia_msize != sc->sc_msize) {
447 		printf("%s: msize mismatch; kernel configured %d != board configured %d\n",
448 		    sc->sc_dev.dv_xname, ia->ia_msize, sc->sc_msize);
449 		return 0;
450 	}
451 
452 	slel_get_address(sc);
453 
454 	ia->ia_iosize = 16;
455 	return 1;
456 }
457 
458 int
459 el_probe(sc, ia)
460 	struct ie_softc *sc;
461 	struct isa_attach_args *ia;
462 {
463 	bus_space_tag_t iot = ia->ia_iot;
464 	bus_space_handle_t ioh;
465 	u_char c;
466 	int i, rval = 0;
467 	u_char signature[] = "*3COM*";
468 
469 	sc->sc_iobase = ia->ia_iobase;
470 
471 	/* Need this for part of the probe. */
472 	sc->reset_586 = el_reset_586;
473 	sc->chan_attn = el_chan_attn;
474 
475 	/*
476 	 * Map the Etherlink ID port for the probe sequence.
477 	 */
478 	if (bus_space_map(iot, ELINK_ID_PORT, 1, 0, &ioh)) {
479 		printf("3c507 probe: can't map Etherlink ID port\n");
480 		return 0;
481 	}
482 
483 	/*
484 	 * Reset and put card in CONFIG state without changing address.
485 	 * XXX Indirect brokenness here!
486 	 */
487 	elink_reset(iot, ioh, sc->sc_dev.dv_parent->dv_unit);
488 	elink_idseq(iot, ioh, ELINK_507_POLY);
489 	elink_idseq(iot, ioh, ELINK_507_POLY);
490 	outb(ELINK_ID_PORT, 0xff);
491 
492 	/* Check for 3COM signature before proceeding. */
493 	outb(PORT + IE507_CTRL, inb(PORT + IE507_CTRL) & 0xfc);	/* XXX */
494 	for (i = 0; i < 6; i++)
495 		if (inb(PORT + i) != signature[i])
496 			goto out;
497 
498 	c = inb(PORT + IE507_MADDR);
499 	if (c & 0x20) {
500 		printf("%s: can't map 3C507 RAM in high memory\n",
501 		    sc->sc_dev.dv_xname);
502 		goto out;
503 	}
504 
505 	/* Go to RUN state. */
506 	outb(ELINK_ID_PORT, 0x00);
507 	elink_idseq(iot, ioh, ELINK_507_POLY);
508 	outb(ELINK_ID_PORT, 0x00);
509 
510 	/* Set bank 2 for version info and read BCD version byte. */
511 	outb(PORT + IE507_CTRL, EL_CTRL_NRST | EL_CTRL_BNK2);
512 	i = inb(PORT + 3);
513 
514 	sc->hard_type = IE_3C507;
515 	sc->hard_vers = 10*(i / 16) + (i % 16) - 1;
516 
517 	i = inb(PORT + IE507_IRQ) & 0x0f;
518 
519 	if (ia->ia_irq != IRQUNK) {
520 		if (ia->ia_irq != i) {
521 			printf("%s: irq mismatch; kernel configured %d != board configured %d\n",
522 			    sc->sc_dev.dv_xname, ia->ia_irq, i);
523 			goto out;
524 		}
525 	} else
526 		ia->ia_irq = i;
527 
528 	i = ((inb(PORT + IE507_MADDR) & 0x1c) << 12) + 0xc0000;
529 
530 	if (ia->ia_maddr != MADDRUNK) {
531 		if (ia->ia_maddr != i) {
532 			printf("%s: maddr mismatch; kernel configured %x != board configured %x\n",
533 			    sc->sc_dev.dv_xname, ia->ia_maddr, i);
534 			goto out;
535 		}
536 	} else
537 		ia->ia_maddr = i;
538 
539 	outb(PORT + IE507_CTRL, EL_CTRL_NORMAL);
540 
541 	/*
542 	 * Divine memory size on-board the card.
543 	 */
544 	sc->sc_maddr = ISA_HOLE_VADDR(ia->ia_maddr);
545 	ie_find_mem_size(sc);
546 
547 	if (!sc->sc_msize) {
548 		printf("%s: can't find shared memory\n", sc->sc_dev.dv_xname);
549 		outb(PORT + IE507_CTRL, EL_CTRL_NRST);
550 		goto out;
551 	}
552 
553 	if (!ia->ia_msize)
554 		ia->ia_msize = sc->sc_msize;
555 	else if (ia->ia_msize != sc->sc_msize) {
556 		printf("%s: msize mismatch; kernel configured %d != board configured %d\n",
557 		    sc->sc_dev.dv_xname, ia->ia_msize, sc->sc_msize);
558 		outb(PORT + IE507_CTRL, EL_CTRL_NRST);
559 		goto out;
560 	}
561 
562 	slel_get_address(sc);
563 
564 	/* Clear the interrupt latch just in case. */
565 	outb(PORT + IE507_ICTRL, 1);
566 
567 	ia->ia_iosize = 16;
568 	rval = 1;
569 
570  out:
571 	bus_space_unmap(iot, ioh, 1);
572 	return rval;
573 }
574 
575 /* Taken almost exactly from Rod's if_ix.c. */
576 
577 int
578 ee16_probe(sc, ia)
579 	struct ie_softc *sc;
580 	struct isa_attach_args *ia;
581 {
582 	int i;
583 	u_short board_id, id_var1, id_var2, checksum = 0;
584 	u_short eaddrtemp, irq;
585         u_short pg, adjust, decode, edecode;
586 	u_char	bart_config;
587 
588 	short	irq_translate[] = {0, 0x09, 0x03, 0x04, 0x05, 0x0a, 0x0b, 0};
589 
590 	/* Need this for part of the probe. */
591 	sc->reset_586 = ee16_reset_586;
592 	sc->chan_attn = ee16_chan_attn;
593 
594 	/* reset any ee16 at the current iobase */
595 	outb(ia->ia_iobase + IEE16_ECTRL, IEE16_RESET_ASIC);
596 	outb(ia->ia_iobase + IEE16_ECTRL, 0);
597 	delay(240);
598 
599 	/* now look for ee16. */
600 	board_id = id_var1 = id_var2 = 0;
601 	for (i=0; i<4 ; i++) {
602 		id_var1 = inb(ia->ia_iobase + IEE16_ID_PORT);
603 		id_var2 = ((id_var1 & 0x03) << 2);
604 		board_id |= (( id_var1 >> 4)  << id_var2);
605 		}
606 
607 	if (board_id != IEE16_ID)
608 		return 0;
609 
610 	/* need sc->sc_iobase for ee16_read_eeprom */
611 	sc->sc_iobase = ia->ia_iobase;
612 	sc->hard_type = IE_EE16;
613 
614 	/*
615 	 * If ia->maddr == MADDRUNK, use value in eeprom location 6.
616 	 *
617 	 * The shared RAM location on the EE16 is encoded into bits
618 	 * 3-7 of EEPROM location 6.  We zero the upper byte, and
619 	 * shift the 5 bits right 3.  The resulting number tells us
620 	 * the RAM location.  Because the EE16 supports either 16k or 32k
621 	 * of shared RAM, we only worry about the 32k locations.
622 	 *
623 	 * NOTE: if a 64k EE16 exists, it should be added to this switch.
624 	 *       then the ia->ia_msize would need to be set per case statement.
625 	 *
626 	 *	value	msize	location
627 	 *	=====	=====	========
628 	 *	0x03	0x8000	0xCC000
629 	 *	0x06	0x8000	0xD0000
630 	 *	0x0C	0x8000	0xD4000
631 	 *	0x18	0x8000	0xD8000
632 	 *
633 	 */
634 
635 	if ((ia->ia_maddr == MADDRUNK) || (ia->ia_msize == 0)) {
636 		i = (ee16_read_eeprom(sc, 6) & 0x00ff ) >> 3;
637 		switch(i) {
638 			case 0x03:
639 				ia->ia_maddr = 0xCC000;
640 				break;
641 			case 0x06:
642 				ia->ia_maddr = 0xD0000;
643 				break;
644 			case 0x0c:
645 				ia->ia_maddr = 0xD4000;
646 				break;
647 			case 0x18:
648 				ia->ia_maddr = 0xD8000;
649 				break;
650 			default:
651 				return 0 ;
652 				break; /* NOTREACHED */
653 		}
654 		ia->ia_msize = 0x8000;
655 	}
656 
657 	/* need to set these after checking for MADDRUNK */
658 	sc->sc_maddr = ISA_HOLE_VADDR(ia->ia_maddr);
659 	sc->sc_msize = ia->ia_msize;
660 
661 	/* need to put the 586 in RESET, and leave it */
662 	outb( PORT + IEE16_ECTRL, IEE16_RESET_586);
663 
664 	/* read the eeprom and checksum it, should == IEE16_ID */
665 	for(i=0 ; i< 0x40 ; i++)
666 		checksum += ee16_read_eeprom(sc, i);
667 
668 	if (checksum != IEE16_ID)
669 		return 0;
670 
671 	/*
672 	 * Size and test the memory on the board.  The size of the memory
673 	 * can be one of 16k, 32k, 48k or 64k.  It can be located in the
674 	 * address range 0xC0000 to 0xEFFFF on 16k boundaries.
675 	 *
676 	 * If the size does not match the passed in memory allocation size
677 	 * issue a warning, but continue with the minimum of the two sizes.
678 	 */
679 
680 	switch (ia->ia_msize) {
681 		case 65536:
682 		case 32768: /* XXX Only support 32k and 64k right now */
683 			break;
684 		case 16384:
685 		case 49512:
686 		default:
687 			printf("ieprobe mapped memory size out of range\n");
688 			return 0;
689 			break; /* NOTREACHED */
690 	}
691 
692 	if ((kvtop(sc->sc_maddr) < 0xC0000) ||
693 	    (kvtop(sc->sc_maddr) + sc->sc_msize > 0xF0000)) {
694 		printf("ieprobe mapped memory address out of range\n");
695 		return 0;
696 	}
697 
698 	pg = (kvtop(sc->sc_maddr) & 0x3C000) >> 14;
699 	adjust = IEE16_MCTRL_FMCS16 | (pg & 0x3) << 2;
700 	decode = ((1 << (sc->sc_msize / 16384)) - 1) << pg;
701 	edecode = ((~decode >> 4) & 0xF0) | (decode >> 8);
702 
703 	/* ZZZ This should be checked against eeprom location 6, low byte */
704 	outb(PORT + IEE16_MEMDEC, decode & 0xFF);
705 	/* ZZZ This should be checked against eeprom location 1, low byte */
706 	outb(PORT + IEE16_MCTRL, adjust);
707 	/* ZZZ Now if I could find this one I would have it made */
708 	outb(PORT + IEE16_MPCTRL, (~decode & 0xFF));
709 	/* ZZZ I think this is location 6, high byte */
710 	outb(PORT + IEE16_MECTRL, edecode); /*XXX disable Exxx */
711 
712 	/*
713 	 * first prime the stupid bart DRAM controller so that it
714 	 * works, then zero out all of memory.
715 	 */
716 	bzero(sc->sc_maddr, 32);
717 	bzero(sc->sc_maddr, sc->sc_msize);
718 
719 	/*
720 	 * Get the encoded interrupt number from the EEPROM, check it
721 	 * against the passed in IRQ.  Issue a warning if they do not
722 	 * match, and fail the probe.  If irq is 'IRQUNK' then we
723 	 * use the EEPROM irq, and continue.
724 	 */
725 	irq = ee16_read_eeprom(sc, IEE16_EEPROM_CONFIG1);
726 	irq = (irq & IEE16_EEPROM_IRQ) >> IEE16_EEPROM_IRQ_SHIFT;
727 	sc->irq_encoded = irq;
728 	irq = irq_translate[irq];
729 	if (ia->ia_irq != IRQUNK) {
730 		if (irq != ia->ia_irq) {
731 #ifdef DIAGNOSTIC
732 			printf("\nie%d: fatal: board IRQ %d does not match kernel\n", sc->sc_dev.dv_unit, irq);
733 #endif /* DIAGNOSTIC */
734 			return 0; 	/* _must_ match or probe fails */
735 		}
736 	} else
737 		ia->ia_irq = irq;
738 
739 	/*
740 	 * Get the hardware ethernet address from the EEPROM and
741 	 * save it in the softc for use by the 586 setup code.
742 	 */
743 	eaddrtemp = ee16_read_eeprom(sc, IEE16_EEPROM_ENET_HIGH);
744 	sc->sc_arpcom.ac_enaddr[1] = eaddrtemp & 0xFF;
745 	sc->sc_arpcom.ac_enaddr[0] = eaddrtemp >> 8;
746 	eaddrtemp = ee16_read_eeprom(sc, IEE16_EEPROM_ENET_MID);
747 	sc->sc_arpcom.ac_enaddr[3] = eaddrtemp & 0xFF;
748 	sc->sc_arpcom.ac_enaddr[2] = eaddrtemp >> 8;
749 	eaddrtemp = ee16_read_eeprom(sc, IEE16_EEPROM_ENET_LOW);
750 	sc->sc_arpcom.ac_enaddr[5] = eaddrtemp & 0xFF;
751 	sc->sc_arpcom.ac_enaddr[4] = eaddrtemp >> 8;
752 
753 	/* disable the board interrupts */
754 	outb(PORT + IEE16_IRQ, sc->irq_encoded);
755 
756 	/* enable loopback to keep bad packets off the wire */
757 	if(sc->hard_type == IE_EE16) {
758 		bart_config = inb(PORT + IEE16_CONFIG);
759 		bart_config |= IEE16_BART_LOOPBACK;
760 		bart_config |= IEE16_BART_MCS16_TEST; /* inb doesn't get bit! */
761 		outb(PORT + IEE16_CONFIG, bart_config);
762 		bart_config = inb(PORT + IEE16_CONFIG);
763 	}
764 
765 	outb(PORT + IEE16_ECTRL, 0);
766 	delay(100);
767 	if (!check_ie_present(sc, sc->sc_maddr, sc->sc_msize))
768 		return 0;
769 
770 	ia->ia_iosize = 16;	/* the number of I/O ports */
771 	return 1;		/* found */
772 }
773 
774 /*
775  * Taken almost exactly from Bill's if_is.c, then modified beyond recognition.
776  */
777 void
778 ieattach(parent, self, aux)
779 	struct device *parent, *self;
780 	void *aux;
781 {
782 	struct ie_softc *sc = (void *)self;
783 	struct isa_attach_args *ia = aux;
784 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
785 
786 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
787 	ifp->if_softc = sc;
788 	ifp->if_start = iestart;
789 	ifp->if_ioctl = ieioctl;
790 	ifp->if_watchdog = iewatchdog;
791 	ifp->if_flags =
792 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
793 	IFQ_SET_READY(&ifp->if_snd);
794 
795 	/* Attach the interface. */
796 	if_attach(ifp);
797 	ether_ifattach(ifp);
798 
799 	printf(": address %s, type %s R%d\n",
800 	    ether_sprintf(sc->sc_arpcom.ac_enaddr),
801 	    ie_hardware_names[sc->hard_type], sc->hard_vers + 1);
802 
803 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
804 	    IPL_NET, ieintr, sc, sc->sc_dev.dv_xname);
805 }
806 
807 /*
808  * Device timeout/watchdog routine.  Entered if the device neglects to generate
809  * an interrupt after a transmit has been started on it.
810  */
811 void
812 iewatchdog(ifp)
813 	struct ifnet *ifp;
814 {
815 	struct ie_softc *sc = ifp->if_softc;
816 
817 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
818 	++sc->sc_arpcom.ac_if.if_oerrors;
819 	iereset(sc);
820 }
821 
822 /*
823  * What to do upon receipt of an interrupt.
824  */
825 int
826 ieintr(arg)
827 	void *arg;
828 {
829 	struct ie_softc *sc = arg;
830 	register u_short status;
831 
832 	/* Clear the interrupt latch on the 3C507. */
833 	if (sc->hard_type == IE_3C507)
834 		outb(PORT + IE507_ICTRL, 1);
835 
836 	/* disable interrupts on the EE16. */
837 	if (sc->hard_type == IE_EE16)
838 		outb(PORT + IEE16_IRQ, sc->irq_encoded);
839 
840 	status = sc->scb->ie_status & IE_ST_WHENCE;
841 	if (status == 0)
842 		return 0;
843 
844 loop:
845 	/* Ack interrupts FIRST in case we receive more during the ISR. */
846 	ie_ack(sc, status);
847 
848 	if (status & (IE_ST_FR | IE_ST_RNR)) {
849 #ifdef IEDEBUG
850 		in_ierint++;
851 		if (sc->sc_debug & IED_RINT)
852 			printf("%s: rint\n", sc->sc_dev.dv_xname);
853 #endif
854 		ierint(sc);
855 #ifdef IEDEBUG
856 		in_ierint--;
857 #endif
858 	}
859 
860 	if (status & IE_ST_CX) {
861 #ifdef IEDEBUG
862 		in_ietint++;
863 		if (sc->sc_debug & IED_TINT)
864 			printf("%s: tint\n", sc->sc_dev.dv_xname);
865 #endif
866 		ietint(sc);
867 #ifdef IEDEBUG
868 		in_ietint--;
869 #endif
870 	}
871 
872 	if (status & IE_ST_RNR) {
873 		printf("%s: receiver not ready\n", sc->sc_dev.dv_xname);
874 		sc->sc_arpcom.ac_if.if_ierrors++;
875 		iereset(sc);
876 	}
877 
878 #ifdef IEDEBUG
879 	if ((status & IE_ST_CNA) && (sc->sc_debug & IED_CNA))
880 		printf("%s: cna\n", sc->sc_dev.dv_xname);
881 #endif
882 
883 	/* Clear the interrupt latch on the 3C507. */
884 	if (sc->hard_type == IE_3C507)
885 		outb(PORT + IE507_ICTRL, 1);
886 
887 	status = sc->scb->ie_status & IE_ST_WHENCE;
888 	if (status == 0) {
889 		/* enable interrupts on the EE16. */
890 		if (sc->hard_type == IE_EE16)
891 		    outb(PORT + IEE16_IRQ, sc->irq_encoded | IEE16_IRQ_ENABLE);
892 		return 1;
893 	}
894 
895 	goto loop;
896 }
897 
898 /*
899  * Process a received-frame interrupt.
900  */
901 void
902 ierint(sc)
903 	struct ie_softc *sc;
904 {
905 	volatile struct ie_sys_ctl_block *scb = sc->scb;
906 	int i, status;
907 	static int timesthru = 1024;
908 
909 	i = sc->rfhead;
910 	for (;;) {
911 		status = sc->rframes[i]->ie_fd_status;
912 
913 		if ((status & IE_FD_COMPLETE) && (status & IE_FD_OK)) {
914 			if (!--timesthru) {
915 				sc->sc_arpcom.ac_if.if_ierrors +=
916 				    scb->ie_err_crc + scb->ie_err_align +
917 				    scb->ie_err_resource + scb->ie_err_overrun;
918 				scb->ie_err_crc = scb->ie_err_align =
919 				    scb->ie_err_resource = scb->ie_err_overrun =
920 				    0;
921 				timesthru = 1024;
922 			}
923 			ie_readframe(sc, i);
924 		} else {
925 			if ((status & IE_FD_RNR) != 0 &&
926 			    (scb->ie_status & IE_RU_READY) == 0) {
927 				sc->rframes[0]->ie_fd_buf_desc =
928 						MK_16(MEM, sc->rbuffs[0]);
929 				scb->ie_recv_list = MK_16(MEM, sc->rframes[0]);
930 				command_and_wait(sc, IE_RU_START, 0, 0);
931 			}
932 			break;
933 		}
934 		i = (i + 1) % NFRAMES;
935 	}
936 }
937 
938 /*
939  * Process a command-complete interrupt.  These are only generated by the
940  * transmission of frames.  This routine is deceptively simple, since most of
941  * the real work is done by iestart().
942  */
943 void
944 ietint(sc)
945 	struct ie_softc *sc;
946 {
947 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
948 	int status;
949 
950 	ifp->if_timer = 0;
951 	ifp->if_flags &= ~IFF_OACTIVE;
952 
953 	status = sc->xmit_cmds[sc->xctail]->ie_xmit_status;
954 
955 	if (!(status & IE_STAT_COMPL) || (status & IE_STAT_BUSY))
956 		printf("ietint: command still busy!\n");
957 
958 	if (status & IE_STAT_OK) {
959 		ifp->if_opackets++;
960 		ifp->if_collisions += status & IE_XS_MAXCOLL;
961 	} else {
962 		ifp->if_oerrors++;
963 		/*
964 		 * XXX
965 		 * Check SQE and DEFERRED?
966 		 * What if more than one bit is set?
967 		 */
968 		if (status & IE_STAT_ABORT)
969 			printf("%s: send aborted\n", sc->sc_dev.dv_xname);
970 		else if (status & IE_XS_LATECOLL)
971 			printf("%s: late collision\n", sc->sc_dev.dv_xname);
972 		else if (status & IE_XS_NOCARRIER)
973 			printf("%s: no carrier\n", sc->sc_dev.dv_xname);
974 		else if (status & IE_XS_LOSTCTS)
975 			printf("%s: lost CTS\n", sc->sc_dev.dv_xname);
976 		else if (status & IE_XS_UNDERRUN)
977 			printf("%s: DMA underrun\n", sc->sc_dev.dv_xname);
978 		else if (status & IE_XS_EXCMAX) {
979 			printf("%s: too many collisions\n", sc->sc_dev.dv_xname);
980 			ifp->if_collisions += 16;
981 		}
982 	}
983 
984 	/*
985 	 * If multicast addresses were added or deleted while transmitting,
986 	 * mc_reset() set the want_mcsetup flag indicating that we should do
987 	 * it.
988 	 */
989 	if (sc->want_mcsetup) {
990 		mc_setup(sc, (caddr_t)sc->xmit_cbuffs[sc->xctail]);
991 		sc->want_mcsetup = 0;
992 	}
993 
994 	/* Done with the buffer. */
995 	sc->xmit_busy--;
996 	sc->xctail = (sc->xctail + 1) % NTXBUF;
997 
998 	/* Start the next packet, if any, transmitting. */
999 	if (sc->xmit_busy > 0)
1000 		iexmit(sc);
1001 
1002 	iestart(ifp);
1003 }
1004 
1005 /*
1006  * Compare two Ether/802 addresses for equality, inlined and unrolled for
1007  * speed.  I'd love to have an inline assembler version of this...
1008  */
1009 static __inline int
1010 ether_equal(one, two)
1011 	u_char *one, *two;
1012 {
1013 
1014 	if (one[0] != two[0] || one[1] != two[1] || one[2] != two[2] ||
1015 	    one[3] != two[3] || one[4] != two[4] || one[5] != two[5])
1016 		return 0;
1017 	return 1;
1018 }
1019 
1020 /*
1021  * Check for a valid address.  to_bpf is filled in with one of the following:
1022  *   0 -> BPF doesn't get this packet
1023  *   1 -> BPF does get this packet
1024  *   2 -> BPF does get this packet, but we don't
1025  * Return value is true if the packet is for us, and false otherwise.
1026  *
1027  * This routine is a mess, but it's also critical that it be as fast
1028  * as possible.  It could be made cleaner if we can assume that the
1029  * only client which will fiddle with IFF_PROMISC is BPF.  This is
1030  * probably a good assumption, but we do not make it here.  (Yet.)
1031  */
1032 static __inline int
1033 check_eh(sc, eh, to_bpf)
1034 	struct ie_softc *sc;
1035 	struct ether_header *eh;
1036 	int *to_bpf;
1037 {
1038 	int i;
1039 
1040 	switch (sc->promisc) {
1041 	case IFF_ALLMULTI:
1042 		/*
1043 		 * Receiving all multicasts, but no unicasts except those
1044 		 * destined for us.
1045 		 */
1046 #if NBPFILTER > 0
1047 		*to_bpf = (sc->sc_arpcom.ac_if.if_bpf != 0); /* BPF gets this packet if anybody cares */
1048 #endif
1049 		if (eh->ether_dhost[0] & 1)
1050 			return 1;
1051 		if (ether_equal(eh->ether_dhost, sc->sc_arpcom.ac_enaddr))
1052 			return 1;
1053 		return 0;
1054 
1055 	case IFF_PROMISC:
1056 		/*
1057 		 * Receiving all packets.  These need to be passed on to BPF.
1058 		 */
1059 #if NBPFILTER > 0
1060 		*to_bpf = (sc->sc_arpcom.ac_if.if_bpf != 0) ||
1061 		    (sc->sc_arpcom.ac_if.if_bridge != NULL);
1062 #else
1063 		*to_bpf = (sc->sc_arpcom.ac_if.if_bridge != NULL);
1064 #endif
1065 		/* If for us, accept and hand up to BPF */
1066 		if (ether_equal(eh->ether_dhost, sc->sc_arpcom.ac_enaddr))
1067 			return 1;
1068 
1069 #if NBPFILTER > 0
1070 		if (*to_bpf && sc->sc_arpcom.ac_if.if_bridge == NULL)
1071 			*to_bpf = 2; /* we don't need to see it */
1072 #endif
1073 
1074 		/*
1075 		 * Not a multicast, so BPF wants to see it but we don't.
1076 		 */
1077 		if (!(eh->ether_dhost[0] & 1))
1078 			return 1;
1079 
1080 		/*
1081 		 * If it's one of our multicast groups, accept it and pass it
1082 		 * up.
1083 		 */
1084 		for (i = 0; i < sc->mcast_count; i++) {
1085 			if (ether_equal(eh->ether_dhost, (u_char *)&sc->mcast_addrs[i])) {
1086 #if NBPFILTER > 0
1087 				if (*to_bpf)
1088 					*to_bpf = 1;
1089 #endif
1090 				return 1;
1091 			}
1092 		}
1093 		return 1;
1094 
1095 	case IFF_ALLMULTI | IFF_PROMISC:
1096 		/*
1097 		 * Acting as a multicast router, and BPF running at the same
1098 		 * time.  Whew!  (Hope this is a fast machine...)
1099 		 */
1100 #if NBPFILTER > 0
1101 		*to_bpf = (sc->sc_arpcom.ac_if.if_bpf != 0) ||
1102 		    (sc->sc_arpcom.ac_if.if_bridge != NULL);
1103 #else
1104 		*to_bpf = (sc->sc_arpcom.ac_if.if_bridge != NULL);
1105 #endif
1106 		/* We want to see multicasts. */
1107 		if (eh->ether_dhost[0] & 1)
1108 			return 1;
1109 
1110 		/* We want to see our own packets */
1111 		if (ether_equal(eh->ether_dhost, sc->sc_arpcom.ac_enaddr))
1112 			return 1;
1113 
1114 		/* Anything else goes to BPF but nothing else. */
1115 #if NBPFILTER > 0
1116 		if (*to_bpf && sc->sc_arpcom.ac_if.if_bridge == NULL)
1117 			*to_bpf = 2;
1118 #endif
1119 		return 1;
1120 
1121 	case 0:
1122 		/*
1123 		 * Only accept unicast packets destined for us, or multicasts
1124 		 * for groups that we belong to.  For now, we assume that the
1125 		 * '586 will only return packets that we asked it for.  This
1126 		 * isn't strictly true (it uses hashing for the multicast
1127 		 * filter), but it will do in this case, and we want to get out
1128 		 * of here as quickly as possible.
1129 		 */
1130 #if NBPFILTER > 0
1131 		*to_bpf = (sc->sc_arpcom.ac_if.if_bpf != 0);
1132 #endif
1133 		return 1;
1134 	}
1135 
1136 #ifdef DIAGNOSTIC
1137 	panic("check_eh: impossible");
1138 #endif
1139 	return 0;
1140 }
1141 
1142 /*
1143  * We want to isolate the bits that have meaning...  This assumes that
1144  * IE_RBUF_SIZE is an even power of two.  If somehow the act_len exceeds
1145  * the size of the buffer, then we are screwed anyway.
1146  */
1147 static __inline int
1148 ie_buflen(sc, head)
1149 	struct ie_softc *sc;
1150 	int head;
1151 {
1152 
1153 	return (sc->rbuffs[head]->ie_rbd_actual
1154 	    & (IE_RBUF_SIZE | (IE_RBUF_SIZE - 1)));
1155 }
1156 
1157 static __inline int
1158 ie_packet_len(sc)
1159 	struct ie_softc *sc;
1160 {
1161 	int i;
1162 	int head = sc->rbhead;
1163 	int acc = 0;
1164 
1165 	do {
1166 		if (!(sc->rbuffs[sc->rbhead]->ie_rbd_actual & IE_RBD_USED))
1167 			return -1;
1168 
1169 		i = sc->rbuffs[head]->ie_rbd_actual & IE_RBD_LAST;
1170 
1171 		acc += ie_buflen(sc, head);
1172 		head = (head + 1) % NRXBUF;
1173 	} while (!i);
1174 
1175 	return acc;
1176 }
1177 
1178 /*
1179  * Setup all necessary artifacts for an XMIT command, and then pass the XMIT
1180  * command to the chip to be executed.  On the way, if we have a BPF listener
1181  * also give him a copy.
1182  */
1183 void
1184 iexmit(sc)
1185 	struct ie_softc *sc;
1186 {
1187 
1188 #ifdef IEDEBUG
1189 	if (sc->sc_debug & IED_XMIT)
1190 		printf("%s: xmit buffer %d\n", sc->sc_dev.dv_xname,
1191 		    sc->xctail);
1192 #endif
1193 
1194 #if NBPFILTER > 0
1195 	/*
1196 	 * If BPF is listening on this interface, let it see the packet before
1197 	 * we push it on the wire.
1198 	 */
1199 	if (sc->sc_arpcom.ac_if.if_bpf)
1200 		bpf_tap(sc->sc_arpcom.ac_if.if_bpf,
1201 		    sc->xmit_cbuffs[sc->xctail],
1202 		    sc->xmit_buffs[sc->xctail]->ie_xmit_flags);
1203 #endif
1204 
1205 	sc->xmit_buffs[sc->xctail]->ie_xmit_flags |= IE_XMIT_LAST;
1206 	sc->xmit_buffs[sc->xctail]->ie_xmit_next = 0xffff;
1207 	sc->xmit_buffs[sc->xctail]->ie_xmit_buf =
1208 	    MK_24(MEM, sc->xmit_cbuffs[sc->xctail]);
1209 
1210 	sc->xmit_cmds[sc->xctail]->com.ie_cmd_link = 0xffff;
1211 	sc->xmit_cmds[sc->xctail]->com.ie_cmd_cmd =
1212 	    IE_CMD_XMIT | IE_CMD_INTR | IE_CMD_LAST;
1213 
1214 	sc->xmit_cmds[sc->xctail]->ie_xmit_status = 0;
1215 	sc->xmit_cmds[sc->xctail]->ie_xmit_desc =
1216 	    MK_16(MEM, sc->xmit_buffs[sc->xctail]);
1217 
1218 	sc->scb->ie_command_list = MK_16(MEM, sc->xmit_cmds[sc->xctail]);
1219 	command_and_wait(sc, IE_CU_START, 0, 0);
1220 
1221 	sc->sc_arpcom.ac_if.if_timer = 5;
1222 }
1223 
1224 /*
1225  * Read data off the interface, and turn it into an mbuf chain.
1226  *
1227  * This code is DRAMATICALLY different from the previous version; this version
1228  * tries to allocate the entire mbuf chain up front, given the length of the
1229  * data available.  This enables us to allocate mbuf clusters in many
1230  * situations where before we would have had a long chain of partially-full
1231  * mbufs.  This should help to speed up the operation considerably.  (Provided
1232  * that it works, of course.)
1233  */
1234 struct mbuf *
1235 ieget(sc, ehp, to_bpf)
1236 	struct ie_softc *sc;
1237 	struct ether_header *ehp;
1238 	int *to_bpf;
1239 {
1240 	struct mbuf *top, **mp, *m;
1241 	int len, totlen, resid;
1242 	int thisrboff, thismboff;
1243 	int head;
1244 
1245 	totlen = ie_packet_len(sc);
1246 	if (totlen <= 0)
1247 		return 0;
1248 
1249 	head = sc->rbhead;
1250 
1251 	/*
1252 	 * Snarf the Ethernet header.
1253 	 */
1254 	bcopy((caddr_t)sc->cbuffs[head], (caddr_t)ehp, sizeof *ehp);
1255 
1256 	/*
1257 	 * As quickly as possible, check if this packet is for us.
1258 	 * If not, don't waste a single cycle copying the rest of the
1259 	 * packet in.
1260 	 * This is only a consideration when FILTER is defined; i.e., when
1261 	 * we are either running BPF or doing multicasting.
1262 	 */
1263 	if (!check_eh(sc, ehp, to_bpf)) {
1264 		sc->sc_arpcom.ac_if.if_ierrors--; /* just this case, it's not an error */
1265 		return 0;
1266 	}
1267 
1268 	resid = totlen -= (thisrboff = sizeof *ehp);
1269 
1270 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1271 	if (m == 0)
1272 		return 0;
1273 	m->m_pkthdr.rcvif = &sc->sc_arpcom.ac_if;
1274 	m->m_pkthdr.len = totlen;
1275 	len = MHLEN;
1276 	top = 0;
1277 	mp = &top;
1278 
1279 	/*
1280 	 * This loop goes through and allocates mbufs for all the data we will
1281 	 * be copying in.  It does not actually do the copying yet.
1282 	 */
1283 	while (totlen > 0) {
1284 		if (top) {
1285 			MGET(m, M_DONTWAIT, MT_DATA);
1286 			if (m == 0) {
1287 				m_freem(top);
1288 				return 0;
1289 			}
1290 			len = MLEN;
1291 		}
1292 		if (totlen >= MINCLSIZE) {
1293 			MCLGET(m, M_DONTWAIT);
1294 			if (m->m_flags & M_EXT)
1295 				len = MCLBYTES;
1296 		}
1297 		m->m_len = len = min(totlen, len);
1298 		totlen -= len;
1299 		*mp = m;
1300 		mp = &m->m_next;
1301 	}
1302 
1303 	m = top;
1304 	thismboff = 0;
1305 
1306 	/*
1307 	 * Now we take the mbuf chain (hopefully only one mbuf most of the
1308 	 * time) and stuff the data into it.  There are no possible failures at
1309 	 * or after this point.
1310 	 */
1311 	while (resid > 0) {
1312 		int thisrblen = ie_buflen(sc, head) - thisrboff,
1313 		    thismblen = m->m_len - thismboff;
1314 		len = min(thisrblen, thismblen);
1315 
1316 		bcopy((caddr_t)(sc->cbuffs[head] + thisrboff),
1317 		    mtod(m, caddr_t) + thismboff, (u_int)len);
1318 		resid -= len;
1319 
1320 		if (len == thismblen) {
1321 			m = m->m_next;
1322 			thismboff = 0;
1323 		} else
1324 			thismboff += len;
1325 
1326 		if (len == thisrblen) {
1327 			head = (head + 1) % NRXBUF;
1328 			thisrboff = 0;
1329 		} else
1330 			thisrboff += len;
1331 	}
1332 
1333 	/*
1334 	 * Unless something changed strangely while we were doing the copy, we
1335 	 * have now copied everything in from the shared memory.
1336 	 * This means that we are done.
1337 	 */
1338 	return top;
1339 }
1340 
1341 /*
1342  * Read frame NUM from unit UNIT (pre-cached as IE).
1343  *
1344  * This routine reads the RFD at NUM, and copies in the buffers from the list
1345  * of RBD, then rotates the RBD and RFD lists so that the receiver doesn't
1346  * start complaining.  Trailers are DROPPED---there's no point in wasting time
1347  * on confusing code to deal with them.  Hopefully, this machine will never ARP
1348  * for trailers anyway.
1349  */
1350 void
1351 ie_readframe(sc, num)
1352 	struct ie_softc *sc;
1353 	int num;			/* frame number to read */
1354 {
1355 	int status;
1356 	struct mbuf *m = 0;
1357 	struct ether_header eh;
1358 #if NBPFILTER > 0
1359 	int bpf_gets_it = 0;
1360 #endif
1361 
1362 	status = sc->rframes[num]->ie_fd_status;
1363 
1364 	/* Advance the RFD list, since we're done with this descriptor. */
1365 	sc->rframes[num]->ie_fd_status = 0;
1366 	sc->rframes[num]->ie_fd_last |= IE_FD_LAST;
1367 	sc->rframes[sc->rftail]->ie_fd_last &= ~IE_FD_LAST;
1368 	sc->rftail = (sc->rftail + 1) % NFRAMES;
1369 	sc->rfhead = (sc->rfhead + 1) % NFRAMES;
1370 
1371 	if (status & IE_FD_OK) {
1372 #if NBPFILTER > 0
1373 		m = ieget(sc, &eh, &bpf_gets_it);
1374 #else
1375 		m = ieget(sc, &eh, 0);
1376 #endif
1377 		ie_drop_packet_buffer(sc);
1378 	}
1379 	if (m == 0) {
1380 		sc->sc_arpcom.ac_if.if_ierrors++;
1381 		return;
1382 	}
1383 
1384 #ifdef IEDEBUG
1385 	if (sc->sc_debug & IED_READFRAME)
1386 		printf("%s: frame from ether %s type %x\n", sc->sc_dev.dv_xname,
1387 		    ether_sprintf(eh.ether_shost), (u_int)eh.ether_type);
1388 #endif
1389 
1390 #if NBPFILTER > 0
1391 	/*
1392 	 * Check for a BPF filter; if so, hand it up.
1393 	 * Note that we have to stick an extra mbuf up front, because bpf_mtap
1394 	 * expects to have the ether header at the front.
1395 	 * It doesn't matter that this results in an ill-formatted mbuf chain,
1396 	 * since BPF just looks at the data.  (It doesn't try to free the mbuf,
1397 	 * tho' it will make a copy for tcpdump.)
1398 	 */
1399 	if (bpf_gets_it) {
1400 		struct mbuf m0;
1401 		m0.m_len = sizeof eh;
1402 		m0.m_data = (caddr_t)&eh;
1403 		m0.m_next = m;
1404 
1405 		/* Pass it up. */
1406 		bpf_mtap(sc->sc_arpcom.ac_if.if_bpf, &m0);
1407 
1408 		/*
1409 		 * A signal passed up from the filtering code indicating that
1410 		 * the packet is intended for BPF but not for the protocol
1411 		 * machinery.  We can save a few cycles by not handing it off
1412 		 * to them.
1413 		 */
1414 		if (bpf_gets_it == 2) {
1415 			m_freem(m);
1416 			return;
1417 		}
1418 	}
1419 #endif /* NBPFILTER > 0 */
1420 
1421 	/*
1422 	 * In here there used to be code to check destination addresses upon
1423 	 * receipt of a packet.  We have deleted that code, and replaced it
1424 	 * with code to check the address much earlier in the cycle, before
1425 	 * copying the data in; this saves us valuable cycles when operating
1426 	 * as a multicast router or when using BPF.
1427 	 */
1428 
1429 	/*
1430 	 * Finally pass this packet up to higher layers.
1431 	 */
1432 	ether_input(&sc->sc_arpcom.ac_if, &eh, m);
1433 	sc->sc_arpcom.ac_if.if_ipackets++;
1434 }
1435 
1436 void
1437 ie_drop_packet_buffer(sc)
1438 	struct ie_softc *sc;
1439 {
1440 	int i;
1441 
1442 	do {
1443 		/*
1444 		 * This means we are somehow out of sync.  So, we reset the
1445 		 * adapter.
1446 		 */
1447 		if (!(sc->rbuffs[sc->rbhead]->ie_rbd_actual & IE_RBD_USED)) {
1448 #ifdef IEDEBUG
1449 			print_rbd(sc->rbuffs[sc->rbhead]);
1450 #endif
1451 			log(LOG_ERR, "%s: receive descriptors out of sync at %d\n",
1452 			    sc->sc_dev.dv_xname, sc->rbhead);
1453 			iereset(sc);
1454 			return;
1455 		}
1456 
1457 		i = sc->rbuffs[sc->rbhead]->ie_rbd_actual & IE_RBD_LAST;
1458 
1459 		sc->rbuffs[sc->rbhead]->ie_rbd_length |= IE_RBD_LAST;
1460 		sc->rbuffs[sc->rbhead]->ie_rbd_actual = 0;
1461 		sc->rbhead = (sc->rbhead + 1) % NRXBUF;
1462 		sc->rbuffs[sc->rbtail]->ie_rbd_length &= ~IE_RBD_LAST;
1463 		sc->rbtail = (sc->rbtail + 1) % NRXBUF;
1464 	} while (!i);
1465 }
1466 
1467 /*
1468  * Start transmission on an interface.
1469  */
1470 void
1471 iestart(ifp)
1472 	struct ifnet *ifp;
1473 {
1474 	struct ie_softc *sc = ifp->if_softc;
1475 	struct mbuf *m0, *m;
1476 	u_char *buffer;
1477 	u_short len;
1478 
1479 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1480 		return;
1481 
1482 	for (;;) {
1483 		if (sc->xmit_busy == NTXBUF) {
1484 			ifp->if_flags |= IFF_OACTIVE;
1485 			break;
1486 		}
1487 
1488 		IFQ_DEQUEUE(&ifp->if_snd, m0);
1489 		if (m0 == 0)
1490 			break;
1491 
1492 		/* We need to use m->m_pkthdr.len, so require the header */
1493 		if ((m0->m_flags & M_PKTHDR) == 0)
1494 			panic("iestart: no header mbuf");
1495 
1496 #if NBPFILTER > 0
1497 		/* Tap off here if there is a BPF listener. */
1498 		if (ifp->if_bpf)
1499 			bpf_mtap(ifp->if_bpf, m0);
1500 #endif
1501 
1502 #ifdef IEDEBUG
1503 		if (sc->sc_debug & IED_ENQ)
1504 			printf("%s: fill buffer %d\n", sc->sc_dev.dv_xname,
1505 			    sc->xchead);
1506 #endif
1507 
1508 		buffer = sc->xmit_cbuffs[sc->xchead];
1509 		for (m = m0; m != 0; m = m->m_next) {
1510 			bcopy(mtod(m, caddr_t), buffer, m->m_len);
1511 			buffer += m->m_len;
1512 		}
1513 		len = max(m0->m_pkthdr.len, ETHER_MIN_LEN);
1514 
1515 		m_freem(m0);
1516 		sc->xmit_buffs[sc->xchead]->ie_xmit_flags = len;
1517 
1518 		/* Start the first packet transmitting. */
1519 		if (sc->xmit_busy == 0)
1520 			iexmit(sc);
1521 
1522 		sc->xchead = (sc->xchead + 1) % NTXBUF;
1523 		sc->xmit_busy++;
1524 	}
1525 }
1526 
1527 /*
1528  * Check to see if there's an 82586 out there.
1529  */
1530 int
1531 check_ie_present(sc, where, size)
1532 	struct ie_softc *sc;
1533 	caddr_t where;
1534 	u_int size;
1535 {
1536 	volatile struct ie_sys_conf_ptr *scp;
1537 	volatile struct ie_int_sys_conf_ptr *iscp;
1538 	volatile struct ie_sys_ctl_block *scb;
1539 	u_long realbase;
1540 	int s;
1541 
1542 	s = splnet();
1543 
1544 	realbase = (u_long)where + size - (1 << 24);
1545 
1546 	scp = (volatile struct ie_sys_conf_ptr *)(realbase + IE_SCP_ADDR);
1547 	bzero((char *)scp, sizeof *scp);
1548 
1549 	/*
1550 	 * First we put the ISCP at the bottom of memory; this tests to make
1551 	 * sure that our idea of the size of memory is the same as the
1552 	 * controller's.  This is NOT where the ISCP will be in normal
1553 	 * operation.
1554 	 */
1555 	iscp = (volatile struct ie_int_sys_conf_ptr *)where;
1556 	bzero((char *)iscp, sizeof *iscp);
1557 
1558 	scb = (volatile struct ie_sys_ctl_block *)where;
1559 	bzero((char *)scb, sizeof *scb);
1560 
1561 	scp->ie_bus_use = 0;		/* 16-bit */
1562 	scp->ie_iscp_ptr = (caddr_t)((volatile caddr_t)iscp -
1563 	    (volatile caddr_t)realbase);
1564 
1565 	iscp->ie_busy = 1;
1566 	iscp->ie_scb_offset = MK_16(realbase, scb) + 256;
1567 
1568 	(sc->reset_586)(sc);
1569 	(sc->chan_attn)(sc);
1570 
1571 	delay(100);			/* wait a while... */
1572 
1573 	if (iscp->ie_busy) {
1574 		splx(s);
1575 		return 0;
1576 	}
1577 
1578 	/*
1579 	 * Now relocate the ISCP to its real home, and reset the controller
1580 	 * again.
1581 	 */
1582 	iscp = (void *)ALIGN(realbase + IE_SCP_ADDR - sizeof(*iscp));
1583 	bzero((char *)iscp, sizeof *iscp);
1584 
1585 	scp->ie_iscp_ptr = (caddr_t)((caddr_t)iscp - (caddr_t)realbase);
1586 
1587 	iscp->ie_busy = 1;
1588 	iscp->ie_scb_offset = MK_16(realbase, scb);
1589 
1590 	(sc->reset_586)(sc);
1591 	(sc->chan_attn)(sc);
1592 
1593 	delay(100);
1594 
1595 	if (iscp->ie_busy) {
1596 		splx(s);
1597 		return 0;
1598 	}
1599 
1600 	sc->sc_msize = size;
1601 	sc->sc_maddr = (caddr_t)realbase;
1602 
1603 	sc->iscp = iscp;
1604 	sc->scb = scb;
1605 
1606 	/*
1607 	 * Acknowledge any interrupts we may have caused...
1608 	 */
1609 	ie_ack(sc, IE_ST_WHENCE);
1610 	splx(s);
1611 
1612 	return 1;
1613 }
1614 
1615 /*
1616  * Divine the memory size of ie board UNIT.
1617  * Better hope there's nothing important hiding just below the ie card...
1618  */
1619 void
1620 ie_find_mem_size(sc)
1621 	struct ie_softc *sc;
1622 {
1623 	u_int size;
1624 
1625 	sc->sc_msize = 0;
1626 
1627 	for (size = 65536; size >= 16384; size -= 16384)
1628 		if (check_ie_present(sc, sc->sc_maddr, size))
1629 			return;
1630 
1631 	return;
1632 }
1633 
1634 void
1635 el_reset_586(sc)
1636 	struct ie_softc *sc;
1637 {
1638 
1639 	outb(PORT + IE507_CTRL, EL_CTRL_RESET);
1640 	delay(100);
1641 	outb(PORT + IE507_CTRL, EL_CTRL_NORMAL);
1642 	delay(100);
1643 }
1644 
1645 void
1646 sl_reset_586(sc)
1647 	struct ie_softc *sc;
1648 {
1649 
1650 	outb(PORT + IEATT_RESET, 0);
1651 }
1652 
1653 void
1654 ee16_reset_586(sc)
1655 	struct ie_softc *sc;
1656 {
1657 
1658 	outb(PORT + IEE16_ECTRL, IEE16_RESET_586);
1659 	delay(100);
1660 	outb(PORT + IEE16_ECTRL, 0);
1661 	delay(100);
1662 }
1663 
1664 void
1665 el_chan_attn(sc)
1666 	struct ie_softc *sc;
1667 {
1668 
1669 	outb(PORT + IE507_ATTN, 1);
1670 }
1671 
1672 void
1673 sl_chan_attn(sc)
1674 	struct ie_softc *sc;
1675 {
1676 
1677 	outb(PORT + IEATT_ATTN, 0);
1678 }
1679 
1680 void
1681 ee16_chan_attn(sc)
1682 	struct ie_softc *sc;
1683 {
1684 	outb(PORT + IEE16_ATTN, 0);
1685 }
1686 
1687 u_short
1688 ee16_read_eeprom(sc, location)
1689 	struct ie_softc *sc;
1690 	int location;
1691 {
1692 	int ectrl, edata;
1693 
1694 	ectrl = inb(PORT + IEE16_ECTRL);
1695 	ectrl &= IEE16_ECTRL_MASK;
1696 	ectrl |= IEE16_ECTRL_EECS;
1697 	outb(PORT + IEE16_ECTRL, ectrl);
1698 
1699 	ee16_eeprom_outbits(sc, IEE16_EEPROM_READ, IEE16_EEPROM_OPSIZE1);
1700 	ee16_eeprom_outbits(sc, location, IEE16_EEPROM_ADDR_SIZE);
1701 	edata = ee16_eeprom_inbits(sc);
1702 	ectrl = inb(PORT + IEE16_ECTRL);
1703 	ectrl &= ~(IEE16_RESET_ASIC | IEE16_ECTRL_EEDI | IEE16_ECTRL_EECS);
1704 	outb(PORT + IEE16_ECTRL, ectrl);
1705 	ee16_eeprom_clock(sc, 1);
1706 	ee16_eeprom_clock(sc, 0);
1707 	return edata;
1708 }
1709 
1710 void
1711 ee16_eeprom_outbits(sc, edata, count)
1712 	struct ie_softc *sc;
1713 	int edata, count;
1714 {
1715 	int ectrl, i;
1716 
1717 	ectrl = inb(PORT + IEE16_ECTRL);
1718 	ectrl &= ~IEE16_RESET_ASIC;
1719 	for (i = count - 1; i >= 0; i--) {
1720 		ectrl &= ~IEE16_ECTRL_EEDI;
1721 		if (edata & (1 << i)) {
1722 			ectrl |= IEE16_ECTRL_EEDI;
1723 		}
1724 		outb(PORT + IEE16_ECTRL, ectrl);
1725 		delay(1);	/* eeprom data must be setup for 0.4 uSec */
1726 		ee16_eeprom_clock(sc, 1);
1727 		ee16_eeprom_clock(sc, 0);
1728 	}
1729 	ectrl &= ~IEE16_ECTRL_EEDI;
1730 	outb(PORT + IEE16_ECTRL, ectrl);
1731 	delay(1);		/* eeprom data must be held for 0.4 uSec */
1732 }
1733 
1734 int
1735 ee16_eeprom_inbits(sc)
1736 	struct ie_softc *sc;
1737 {
1738 	int ectrl, edata, i;
1739 
1740 	ectrl = inb(PORT + IEE16_ECTRL);
1741 	ectrl &= ~IEE16_RESET_ASIC;
1742 	for (edata = 0, i = 0; i < 16; i++) {
1743 		edata = edata << 1;
1744 		ee16_eeprom_clock(sc, 1);
1745 		ectrl = inb(PORT + IEE16_ECTRL);
1746 		if (ectrl & IEE16_ECTRL_EEDO) {
1747 			edata |= 1;
1748 		}
1749 		ee16_eeprom_clock(sc, 0);
1750 	}
1751 	return (edata);
1752 }
1753 
1754 void
1755 ee16_eeprom_clock(sc, state)
1756 	struct ie_softc *sc;
1757 	int state;
1758 {
1759 	int ectrl;
1760 
1761 	ectrl = inb(PORT + IEE16_ECTRL);
1762 	ectrl &= ~(IEE16_RESET_ASIC | IEE16_ECTRL_EESK);
1763 	if (state) {
1764 		ectrl |= IEE16_ECTRL_EESK;
1765 	}
1766 	outb(PORT + IEE16_ECTRL, ectrl);
1767 	delay(9);		/* EESK must be stable for 8.38 uSec */
1768 }
1769 
1770 static inline void
1771 ee16_interrupt_enable(sc)
1772 	struct ie_softc *sc;
1773 {
1774 	delay(100);
1775 	outb(PORT + IEE16_IRQ, sc->irq_encoded | IEE16_IRQ_ENABLE);
1776 	delay(100);
1777 }
1778 void
1779 slel_get_address(sc)
1780 	struct ie_softc *sc;
1781 {
1782 	u_char *addr = sc->sc_arpcom.ac_enaddr;
1783 	int i;
1784 
1785 	for (i = 0; i < ETHER_ADDR_LEN; i++)
1786 		addr[i] = inb(PORT + i);
1787 }
1788 
1789 void
1790 iereset(sc)
1791 	struct ie_softc *sc;
1792 {
1793 	int s = splnet();
1794 
1795 	iestop(sc);
1796 
1797 	/*
1798 	 * Stop i82586 dead in its tracks.
1799 	 */
1800 	if (command_and_wait(sc, IE_RU_ABORT | IE_CU_ABORT, 0, 0))
1801 		printf("%s: abort commands timed out\n", sc->sc_dev.dv_xname);
1802 
1803 	if (command_and_wait(sc, IE_RU_DISABLE | IE_CU_STOP, 0, 0))
1804 		printf("%s: disable commands timed out\n", sc->sc_dev.dv_xname);
1805 
1806 	ieinit(sc);
1807 
1808 	splx(s);
1809 }
1810 
1811 /*
1812  * Send a command to the controller and wait for it to either complete or be
1813  * accepted, depending on the command.  If the command pointer is null, then
1814  * pretend that the command is not an action command.  If the command pointer
1815  * is not null, and the command is an action command, wait for
1816  * ((volatile struct ie_cmd_common *)pcmd)->ie_cmd_status & MASK
1817  * to become true.
1818  */
1819 static int
1820 command_and_wait(sc, cmd, pcmd, mask)
1821 	struct ie_softc *sc;
1822 	int cmd;
1823 	volatile void *pcmd;
1824 	int mask;
1825 {
1826 	volatile struct ie_cmd_common *cc = pcmd;
1827 	volatile struct ie_sys_ctl_block *scb = sc->scb;
1828 	int i;
1829 
1830 	scb->ie_command = (u_short)cmd;
1831 
1832 	if (IE_ACTION_COMMAND(cmd) && pcmd) {
1833 		(sc->chan_attn)(sc);
1834 
1835 		/*
1836 		 * According to the packet driver, the minimum timeout should
1837 		 * be .369 seconds, which we round up to .4.
1838 		 *
1839 		 * Now spin-lock waiting for status.  This is not a very nice
1840 		 * thing to do, but I haven't figured out how, or indeed if, we
1841 		 * can put the process waiting for action to sleep.  (We may
1842 		 * be getting called through some other timeout running in the
1843 		 * kernel.)
1844 		 */
1845 		for (i = 36900; i--; DELAY(10))
1846 			if ((cc->ie_cmd_status & mask))
1847 				break;
1848 
1849 		return i < 0;
1850 	} else {
1851 		/*
1852 		 * Otherwise, just wait for the command to be accepted.
1853 		 */
1854 		(sc->chan_attn)(sc);
1855 
1856 		while (scb->ie_command)
1857 			;				/* spin lock */
1858 
1859 		return 0;
1860 	}
1861 }
1862 
1863 /*
1864  * Run the time-domain reflectometer.
1865  */
1866 static void
1867 run_tdr(sc, cmd)
1868 	struct ie_softc *sc;
1869 	struct ie_tdr_cmd *cmd;
1870 {
1871 	int result;
1872 
1873 	cmd->com.ie_cmd_status = 0;
1874 	cmd->com.ie_cmd_cmd = IE_CMD_TDR | IE_CMD_LAST;
1875 	cmd->com.ie_cmd_link = 0xffff;
1876 
1877 	sc->scb->ie_command_list = MK_16(MEM, cmd);
1878 	cmd->ie_tdr_time = 0;
1879 
1880 	if (command_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
1881 	    !(cmd->com.ie_cmd_status & IE_STAT_OK))
1882 		result = 0x10000;
1883 	else
1884 		result = cmd->ie_tdr_time;
1885 
1886 	ie_ack(sc, IE_ST_WHENCE);
1887 
1888 	if (result & IE_TDR_SUCCESS)
1889 		return;
1890 
1891 	if (result & 0x10000)
1892 		printf("%s: TDR command failed\n", sc->sc_dev.dv_xname);
1893 	else if (result & IE_TDR_XCVR)
1894 		printf("%s: transceiver problem\n", sc->sc_dev.dv_xname);
1895 	else if (result & IE_TDR_OPEN)
1896 		printf("%s: TDR detected an open %d clocks away\n",
1897 		    sc->sc_dev.dv_xname, result & IE_TDR_TIME);
1898 	else if (result & IE_TDR_SHORT)
1899 		printf("%s: TDR detected a short %d clocks away\n",
1900 		    sc->sc_dev.dv_xname, result & IE_TDR_TIME);
1901 	else
1902 		printf("%s: TDR returned unknown status %x\n",
1903 		    sc->sc_dev.dv_xname, result);
1904 }
1905 
1906 #define	_ALLOC(p, n)	(bzero(p, n), p += n, p - n)
1907 #define	ALLOC(p, n)	_ALLOC(p, ALIGN(n))
1908 
1909 /*
1910  * Here is a helper routine for ieinit().  This sets up the buffers.
1911  */
1912 void
1913 iememinit(ptr, sc)
1914 	void *ptr;
1915 	struct ie_softc *sc;
1916 {
1917 	int i;
1918 
1919 	/* First lay them out. */
1920 	for (i = 0; i < NFRAMES; i++)
1921 		sc->rframes[i] = ALLOC(ptr, sizeof(*sc->rframes[i]));
1922 
1923 	/* Now link them together. */
1924 	for (i = 0; i < NFRAMES; i++)
1925 		sc->rframes[i]->ie_fd_next =
1926 		    MK_16(MEM, sc->rframes[(i + 1) % NFRAMES]);
1927 
1928 	/* Finally, set the EOL bit on the last one. */
1929 	sc->rframes[NFRAMES - 1]->ie_fd_last |= IE_FD_LAST;
1930 
1931 	/*
1932 	 * Now lay out some buffers for the incoming frames.  Note that we set
1933 	 * aside a bit of slop in each buffer, to make sure that we have enough
1934 	 * space to hold a single frame in every buffer.
1935 	 */
1936 	for (i = 0; i < NRXBUF; i++) {
1937 		sc->rbuffs[i] = ALLOC(ptr, sizeof(*sc->rbuffs[i]));
1938 		sc->rbuffs[i]->ie_rbd_length = IE_RBUF_SIZE;
1939 		sc->rbuffs[i]->ie_rbd_buffer = MK_24(MEM, ptr);
1940 		sc->cbuffs[i] = ALLOC(ptr, IE_RBUF_SIZE);
1941 	}
1942 
1943 	/* Now link them together. */
1944 	for (i = 0; i < NRXBUF; i++)
1945 		sc->rbuffs[i]->ie_rbd_next =
1946 		    MK_16(MEM, sc->rbuffs[(i + 1) % NRXBUF]);
1947 
1948 	/* Tag EOF on the last one. */
1949 	sc->rbuffs[NRXBUF - 1]->ie_rbd_length |= IE_RBD_LAST;
1950 
1951 	/*
1952 	 * We use the head and tail pointers on receive to keep track of the
1953 	 * order in which RFDs and RBDs are used.
1954 	 */
1955 	sc->rfhead = 0;
1956 	sc->rftail = NFRAMES - 1;
1957 	sc->rbhead = 0;
1958 	sc->rbtail = NRXBUF - 1;
1959 
1960 	sc->scb->ie_recv_list = MK_16(MEM, sc->rframes[0]);
1961 	sc->rframes[0]->ie_fd_buf_desc = MK_16(MEM, sc->rbuffs[0]);
1962 
1963 	/*
1964 	 * Finally, the transmit command and buffer are the last little bit of
1965 	 * work.
1966 	 */
1967 	for (i = 0; i < NTXBUF; i++) {
1968 		sc->xmit_cmds[i] = ALLOC(ptr, sizeof(*sc->xmit_cmds[i]));
1969 		sc->xmit_buffs[i] = ALLOC(ptr, sizeof(*sc->xmit_buffs[i]));
1970 	}
1971 
1972 	for (i = 0; i < NTXBUF; i++)
1973 		sc->xmit_cbuffs[i] = ALLOC(ptr, IE_TBUF_SIZE);
1974 
1975 	/* Pointers to last packet sent and next available transmit buffer. */
1976 	sc->xchead = sc->xctail = 0;
1977 
1978 	/* Clear transmit-busy flag and set number of free transmit buffers. */
1979 	sc->xmit_busy = 0;
1980 }
1981 
1982 /*
1983  * Run the multicast setup command.
1984  * Called at splnet().
1985  */
1986 static int
1987 mc_setup(sc, ptr)
1988 	struct ie_softc *sc;
1989 	void *ptr;
1990 {
1991 	volatile struct ie_mcast_cmd *cmd = ptr;
1992 
1993 	cmd->com.ie_cmd_status = 0;
1994 	cmd->com.ie_cmd_cmd = IE_CMD_MCAST | IE_CMD_LAST;
1995 	cmd->com.ie_cmd_link = 0xffff;
1996 
1997 	bcopy((caddr_t)sc->mcast_addrs, (caddr_t)cmd->ie_mcast_addrs,
1998 	    sc->mcast_count * sizeof *sc->mcast_addrs);
1999 
2000 	cmd->ie_mcast_bytes = sc->mcast_count * ETHER_ADDR_LEN; /* grrr... */
2001 
2002 	sc->scb->ie_command_list = MK_16(MEM, cmd);
2003 	if (command_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
2004 	    !(cmd->com.ie_cmd_status & IE_STAT_OK)) {
2005 		printf("%s: multicast address setup command failed\n",
2006 		    sc->sc_dev.dv_xname);
2007 		return 0;
2008 	}
2009 	return 1;
2010 }
2011 
2012 /*
2013  * This routine takes the environment generated by check_ie_present() and adds
2014  * to it all the other structures we need to operate the adapter.  This
2015  * includes executing the CONFIGURE, IA-SETUP, and MC-SETUP commands, starting
2016  * the receiver unit, and clearing interrupts.
2017  *
2018  * THIS ROUTINE MUST BE CALLED AT splnet() OR HIGHER.
2019  */
2020 int
2021 ieinit(sc)
2022 	struct ie_softc *sc;
2023 {
2024 	volatile struct ie_sys_ctl_block *scb = sc->scb;
2025 	void *ptr;
2026 
2027 	ptr = (void *)ALIGN(scb + 1);
2028 
2029 	/*
2030 	 * Send the configure command first.
2031 	 */
2032 	{
2033 		volatile struct ie_config_cmd *cmd = ptr;
2034 
2035 		scb->ie_command_list = MK_16(MEM, cmd);
2036 		cmd->com.ie_cmd_status = 0;
2037 		cmd->com.ie_cmd_cmd = IE_CMD_CONFIG | IE_CMD_LAST;
2038 		cmd->com.ie_cmd_link = 0xffff;
2039 
2040 		ie_setup_config(cmd, sc->promisc != 0,
2041 		    sc->hard_type == IE_STARLAN10);
2042 
2043 		if (command_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
2044 		    !(cmd->com.ie_cmd_status & IE_STAT_OK)) {
2045 			printf("%s: configure command failed\n",
2046 			    sc->sc_dev.dv_xname);
2047 			return 0;
2048 		}
2049 	}
2050 
2051 	/*
2052 	 * Now send the Individual Address Setup command.
2053 	 */
2054 	{
2055 		volatile struct ie_iasetup_cmd *cmd = ptr;
2056 
2057 		scb->ie_command_list = MK_16(MEM, cmd);
2058 		cmd->com.ie_cmd_status = 0;
2059 		cmd->com.ie_cmd_cmd = IE_CMD_IASETUP | IE_CMD_LAST;
2060 		cmd->com.ie_cmd_link = 0xffff;
2061 
2062 		bcopy(sc->sc_arpcom.ac_enaddr, (caddr_t)&cmd->ie_address,
2063 		    sizeof cmd->ie_address);
2064 
2065 		if (command_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
2066 		    !(cmd->com.ie_cmd_status & IE_STAT_OK)) {
2067 			printf("%s: individual address setup command failed\n",
2068 			    sc->sc_dev.dv_xname);
2069 			return 0;
2070 		}
2071 	}
2072 
2073 	/*
2074 	 * Now run the time-domain reflectometer.
2075 	 */
2076 	run_tdr(sc, ptr);
2077 
2078 	/*
2079 	 * Acknowledge any interrupts we have generated thus far.
2080 	 */
2081 	ie_ack(sc, IE_ST_WHENCE);
2082 
2083 	/*
2084 	 * Set up the RFA.
2085 	 */
2086 	iememinit(ptr, sc);
2087 
2088 	sc->sc_arpcom.ac_if.if_flags |= IFF_RUNNING;
2089 	sc->sc_arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
2090 
2091 	sc->scb->ie_recv_list = MK_16(MEM, sc->rframes[0]);
2092 	command_and_wait(sc, IE_RU_START, 0, 0);
2093 
2094 	ie_ack(sc, IE_ST_WHENCE);
2095 
2096 	/* take the ee16 out of loopback */
2097 	{
2098 	u_char	bart_config;
2099 
2100 	if(sc->hard_type == IE_EE16) {
2101 		bart_config = inb(PORT + IEE16_CONFIG);
2102 		bart_config &= ~IEE16_BART_LOOPBACK;
2103 		bart_config |= IEE16_BART_MCS16_TEST; /* inb doesn't get bit! */
2104 		outb(PORT + IEE16_CONFIG, bart_config);
2105 		ee16_interrupt_enable(sc);
2106 		ee16_chan_attn(sc);
2107 		}
2108 	}
2109 	return 0;
2110 }
2111 
2112 void
2113 iestop(sc)
2114 	struct ie_softc *sc;
2115 {
2116 
2117 	command_and_wait(sc, IE_RU_DISABLE, 0, 0);
2118 }
2119 
2120 int
2121 ieioctl(ifp, cmd, data)
2122 	register struct ifnet *ifp;
2123 	u_long cmd;
2124 	caddr_t data;
2125 {
2126 	struct ie_softc *sc = ifp->if_softc;
2127 	struct ifaddr *ifa = (struct ifaddr *)data;
2128 	struct ifreq *ifr = (struct ifreq *)data;
2129 	int s, error = 0;
2130 
2131 	s = splnet();
2132 
2133 	if ((error = ether_ioctl(ifp, &sc->sc_arpcom, cmd, data)) > 0) {
2134 		splx(s);
2135 		return error;
2136 	}
2137 
2138 	switch (cmd) {
2139 
2140 	case SIOCSIFADDR:
2141 		ifp->if_flags |= IFF_UP;
2142 
2143 		switch (ifa->ifa_addr->sa_family) {
2144 #ifdef INET
2145 		case AF_INET:
2146 			ieinit(sc);
2147 			arp_ifinit(&sc->sc_arpcom, ifa);
2148 			break;
2149 #endif
2150 		default:
2151 			ieinit(sc);
2152 			break;
2153 		}
2154 		break;
2155 
2156 	case SIOCSIFFLAGS:
2157 		sc->promisc = ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI);
2158 		if ((ifp->if_flags & IFF_UP) == 0 &&
2159 		    (ifp->if_flags & IFF_RUNNING) != 0) {
2160 			/*
2161 			 * If interface is marked down and it is running, then
2162 			 * stop it.
2163 			 */
2164 			iestop(sc);
2165 			ifp->if_flags &= ~IFF_RUNNING;
2166 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
2167 			   (ifp->if_flags & IFF_RUNNING) == 0) {
2168 			/*
2169 			 * If interface is marked up and it is stopped, then
2170 			 * start it.
2171 			 */
2172 			ieinit(sc);
2173 		} else {
2174 			/*
2175 			 * Reset the interface to pick up changes in any other
2176 			 * flags that affect hardware registers.
2177 			 */
2178 			iestop(sc);
2179 			ieinit(sc);
2180 		}
2181 #ifdef IEDEBUG
2182 		if (ifp->if_flags & IFF_DEBUG)
2183 			sc->sc_debug = IED_ALL;
2184 		else
2185 			sc->sc_debug = 0;
2186 #endif
2187 		break;
2188 
2189 	case SIOCADDMULTI:
2190 	case SIOCDELMULTI:
2191 		error = (cmd == SIOCADDMULTI) ?
2192 		    ether_addmulti(ifr, &sc->sc_arpcom):
2193 		    ether_delmulti(ifr, &sc->sc_arpcom);
2194 
2195 		if (error == ENETRESET) {
2196 			/*
2197 			 * Multicast list has changed; set the hardware filter
2198 			 * accordingly.
2199 			 */
2200 			mc_reset(sc);
2201 			error = 0;
2202 		}
2203 		break;
2204 
2205 	default:
2206 		error = EINVAL;
2207 	}
2208 	splx(s);
2209 	return error;
2210 }
2211 
2212 static void
2213 mc_reset(sc)
2214 	struct ie_softc *sc;
2215 {
2216 	struct ether_multi *enm;
2217 	struct ether_multistep step;
2218 
2219 	/*
2220 	 * Step through the list of addresses.
2221 	 */
2222 	sc->mcast_count = 0;
2223 	ETHER_FIRST_MULTI(step, &sc->sc_arpcom, enm);
2224 	while (enm) {
2225 		if (sc->mcast_count >= MAXMCAST ||
2226 		    bcmp(enm->enm_addrlo, enm->enm_addrhi, 6) != 0) {
2227 			sc->sc_arpcom.ac_if.if_flags |= IFF_ALLMULTI;
2228 			ieioctl(&sc->sc_arpcom.ac_if, SIOCSIFFLAGS, (void *)0);
2229 			goto setflag;
2230 		}
2231 
2232 		bcopy(enm->enm_addrlo, &sc->mcast_addrs[sc->mcast_count], 6);
2233 		sc->mcast_count++;
2234 		ETHER_NEXT_MULTI(step, enm);
2235 	}
2236 setflag:
2237 	sc->want_mcsetup = 1;
2238 }
2239 
2240 #ifdef IEDEBUG
2241 void
2242 print_rbd(rbd)
2243 	volatile struct ie_recv_buf_desc *rbd;
2244 {
2245 
2246 	printf("RBD at %08lx:\nactual %04x, next %04x, buffer %08x\n"
2247 	    "length %04x, mbz %04x\n", (u_long)rbd, rbd->ie_rbd_actual,
2248 	    rbd->ie_rbd_next, rbd->ie_rbd_buffer, rbd->ie_rbd_length,
2249 	    rbd->mbz);
2250 }
2251 #endif
2252 
2253