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