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