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