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