if_ie.c revision 133689
1/*-
2 * Copyright (c) 1992, 1993, University of Vermont and State
3 *  Agricultural College.
4 * Copyright (c) 1992, 1993, Garrett A. Wollman.
5 *
6 * Portions:
7 * Copyright (c) 1990, 1991, William F. Jolitz
8 * Copyright (c) 1990, The Regents of the University of California
9 *
10 * 3Com 3C507 support:
11 * Copyright (c) 1993, 1994, Charles M. Hannum
12 *
13 * EtherExpress 16 support:
14 * Copyright (c) 1993, 1994, 1995, Rodney W. Grimes
15 * Copyright (c) 1997, Aaron C. Smith
16 *
17 * All rights reserved.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 *    notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 *    notice, this list of conditions and the following disclaimer in the
26 *    documentation and/or other materials provided with the distribution.
27 * 3. All advertising materials mentioning features or use of this software
28 *    must display the following acknowledgement:
29 *	This product includes software developed by the University of
30 *	Vermont and State Agricultural College and Garrett A. Wollman, by
31 *	William F. Jolitz, by the University of California, Berkeley,
32 *	Lawrence Berkeley Laboratory, and their contributors, by
33 *	Charles M. Hannum, by Rodney W. Grimes, and by Aaron C. Smith.
34 * 4. Neither the names of the Universities nor the names of the authors
35 *    may be used to endorse or promote products derived from this software
36 *    without specific prior written permission.
37 *
38 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
39 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41 * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR AUTHORS BE LIABLE
42 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 *
50 * MAINTAINER: Matthew N. Dodd <winter@jurai.net>
51 */
52
53#include <sys/cdefs.h>
54__FBSDID("$FreeBSD: head/sys/dev/ie/if_ie.c 133689 2004-08-13 23:15:44Z rwatson $");
55
56/*
57 * Intel 82586 Ethernet chip
58 * Register, bit, and structure definitions.
59 *
60 * Written by GAW with reference to the Clarkson Packet Driver code for this
61 * chip written by Russ Nelson and others.
62 *
63 * Intel EtherExpress 16 support from if_ix.c, written by Rodney W. Grimes.
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
77 * received frame descriptors around for the receiver to use, and
78 * NRXBUFS associated receive buffer descriptors, both in a circular
79 * list.  Whenever a frame is received, we rotate both lists as
80 * necessary.  (The 586 treats both lists as a simple queue.)  We also
81 * keep a transmit command around so that packets can be sent off
82 * quickly.
83 *
84 * We configure the adapter in AL-LOC = 1 mode, which means that the
85 * Ethernet/802.3 MAC header is placed at the beginning of the receive
86 * buffer rather than being split off into various fields in the RFD.
87 * This also means that we must include this header in the transmit
88 * buffer as well.
89 *
90 * By convention, all transmit commands, and only transmit commands,
91 * shall have the I (IE_CMD_INTR) bit set in the command.  This way,
92 * when an interrupt arrives at ieintr(), it is immediately possible
93 * to tell what precisely caused it.  ANY OTHER command-sending routines
94 * should run at splimp(), and should post an acknowledgement to every
95 * interrupt they generate.
96 *
97 * The 82586 has a 24-bit address space internally, and the adaptor's
98 * memory is located at the top of this region.  However, the value
99 * we are given in configuration is normally the *bottom* of the adaptor
100 * RAM.  So, we must go through a few gyrations to come up with a
101 * kernel virtual address which represents the actual beginning of the
102 * 586 address space.  First, we autosize the RAM by running through
103 * several possible sizes and trying to initialize the adapter under
104 * the assumption that the selected size is correct.  Then, knowing
105 * the correct RAM size, we set up our pointers in the softc `iomem'
106 * represents the computed base of the 586 address space.  `iomembot'
107 * represents the actual configured base of adapter RAM.  Finally,
108 * `iosize' represents the calculated size of 586 RAM.  Then, when
109 * laying out commands, we use the interval [iomembot, iomembot +
110 * iosize); to make 24-pointers, we subtract iomem, and to make
111 * 16-pointers, we subtract iomem and and with 0xffff.
112 */
113
114#include <sys/param.h>
115#include <sys/systm.h>
116#include <sys/eventhandler.h>
117#include <sys/kernel.h>
118#include <sys/malloc.h>
119#include <sys/mbuf.h>
120#include <sys/socket.h>
121#include <sys/sockio.h>
122#include <sys/syslog.h>
123
124#include <sys/module.h>
125#include <sys/bus.h>
126
127#include <machine/bus_pio.h>
128#include <machine/bus.h>
129#include <machine/resource.h>
130#include <sys/rman.h>
131
132#include <net/ethernet.h>
133#include <net/if.h>
134#include <net/if_types.h>
135#include <net/if_dl.h>
136
137#include <netinet/in.h>
138#include <netinet/if_ether.h>
139
140#include <dev/ic/i82586.h>
141#include <dev/ie/if_ievar.h>
142#include <dev/ie/if_iereg.h>
143#include <dev/ie/if_ie507.h>
144#include <dev/ie/if_iee16.h>
145#include <i386/isa/elink.h>
146
147#include <net/bpf.h>
148
149#ifdef DEBUG
150#define IED_RINT	0x01
151#define IED_TINT	0x02
152#define IED_RNR		0x04
153#define IED_CNA		0x08
154#define IED_READFRAME	0x10
155static int	ie_debug = IED_RNR;
156
157#endif
158
159#define IE_BUF_LEN	ETHER_MAX_LEN	/* length of transmit buffer */
160
161/* Forward declaration */
162struct ie_softc;
163
164static void	ieinit			(void *);
165static void	ie_stop			(struct ie_softc *);
166static int	ieioctl			(struct ifnet *, u_long, caddr_t);
167static void	iestart			(struct ifnet *);
168
169static __inline void
170		ee16_interrupt_enable	(struct ie_softc *);
171static void	ee16_eeprom_outbits	(struct ie_softc *, int, int);
172static void	ee16_eeprom_clock	(struct ie_softc *, int);
173static u_short	ee16_read_eeprom	(struct ie_softc *, int);
174static int	ee16_eeprom_inbits	(struct ie_softc *);
175static void	ee16_shutdown		(void *, int);
176
177static __inline void
178		ie_ack			(struct ie_softc *, u_int);
179static void	iereset			(struct ie_softc *);
180static void	ie_readframe		(struct ie_softc *, int);
181static void	ie_drop_packet_buffer	(struct ie_softc *);
182static void	find_ie_mem_size	(struct ie_softc *);
183static void	chan_attn_timeout	(void *);
184static int	command_and_wait	(struct ie_softc *,
185					 int, void volatile *, int);
186static void	run_tdr			(struct ie_softc *,
187					 volatile struct ie_tdr_cmd *);
188static int	ierint			(struct ie_softc *);
189static int	ietint			(struct ie_softc *);
190static int	iernr			(struct ie_softc *);
191static void	start_receiver		(struct ie_softc *);
192static __inline int
193		ieget			(struct ie_softc *, struct mbuf **);
194static v_caddr_t setup_rfa		(struct ie_softc *, v_caddr_t);
195static int	mc_setup		(struct ie_softc *);
196static void	ie_mc_reset		(struct ie_softc *);
197
198#ifdef DEBUG
199static void	print_rbd		(volatile struct ie_recv_buf_desc * rbd);
200static int	in_ierint = 0;
201static int	in_ietint = 0;
202#endif
203
204static const char *ie_hardware_names[] = {
205	"None",
206	"StarLAN 10",
207	"EN100",
208	"StarLAN Fiber",
209	"3C507",
210	"NI5210",
211	"EtherExpress 16",
212	"Unknown"
213};
214
215/*
216 * sizeof(iscp) == 1+1+2+4 == 8
217 * sizeof(scb) == 2+2+2+2+2+2+2+2 == 16
218 * NFRAMES * sizeof(rfd) == NFRAMES*(2+2+2+2+6+6+2+2) == NFRAMES*24 == 384
219 * sizeof(xmit_cmd) == 2+2+2+2+6+2 == 18
220 * sizeof(transmit buffer) == 1512
221 * sizeof(transmit buffer desc) == 8
222 * -----
223 * 1946
224 *
225 * NRXBUFS * sizeof(rbd) == NRXBUFS*(2+2+4+2+2) == NRXBUFS*12
226 * NRXBUFS * IE_RBUF_SIZE == NRXBUFS*256
227 *
228 * NRXBUFS should be (16384 - 1946) / (256 + 12) == 14438 / 268 == 53
229 *
230 * With NRXBUFS == 48, this leaves us 1574 bytes for another command or
231 * more buffers.  Another transmit command would be 18+8+1512 == 1538
232 * ---just barely fits!
233 *
234 * Obviously all these would have to be reduced for smaller memory sizes.
235 * With a larger memory, it would be possible to roughly double the number
236 * of both transmit and receive buffers.
237 */
238
239#define	NFRAMES		4	/* number of receive frames */
240#define	NRXBUFS		24	/* number of buffers to allocate */
241#define	IE_RBUF_SIZE	256	/* size of each buffer, MUST BE POWER OF TWO */
242#define	NTXBUFS		1	/* number of transmit commands */
243#define	IE_TBUF_SIZE	ETHER_MAX_LEN	/* size of transmit buffer */
244
245#define MK_24(base, ptr) ((caddr_t)((uintptr_t)ptr - (uintptr_t)base))
246#define MK_16(base, ptr) ((u_short)(uintptr_t)MK_24(base, ptr))
247
248static void
249ee16_shutdown(void *xsc, int howto)
250{
251	struct	ie_softc *sc = (struct ie_softc *)xsc;
252
253	ee16_reset_586(sc);
254	outb(PORT(sc) + IEE16_ECTRL, IEE16_RESET_ASIC);
255	outb(PORT(sc) + IEE16_ECTRL, 0);
256}
257
258/*
259 * Taken almost exactly from Bill's if_is.c, then modified beyond recognition.
260 */
261int
262ie_attach(device_t dev)
263{
264	struct ie_softc *       sc;
265	struct ifnet *          ifp;
266	size_t                  allocsize;
267	int                     factor;
268
269	sc = device_get_softc(dev);
270	ifp = &sc->arpcom.ac_if;
271
272	sc->dev = dev;
273	sc->unit = device_get_unit(dev);
274
275	/*
276	 * based on the amount of memory we have, allocate our tx and rx
277	 * resources.
278	 */
279	factor = rman_get_size(sc->mem_res) / 8192;
280	sc->nframes = factor * NFRAMES;
281	sc->nrxbufs = factor * NRXBUFS;
282	sc->ntxbufs = factor * NTXBUFS;
283
284	/*
285	 * Since all of these guys are arrays of pointers, allocate as one
286	 * big chunk and dole out accordingly.
287	 */
288	allocsize = sizeof(void *) * (sc->nframes
289				      + (sc->nrxbufs * 2)
290				      + (sc->ntxbufs * 3));
291	sc->rframes = (volatile struct ie_recv_frame_desc **) malloc(allocsize,
292								     M_DEVBUF,
293								   M_NOWAIT);
294	if (sc->rframes == NULL)
295		return (ENXIO);
296	sc->rbuffs =
297	    (volatile struct ie_recv_buf_desc **)&sc->rframes[sc->nframes];
298	sc->cbuffs = (volatile u_char **)&sc->rbuffs[sc->nrxbufs];
299	sc->xmit_cmds =
300	    (volatile struct ie_xmit_cmd **)&sc->cbuffs[sc->nrxbufs];
301	sc->xmit_buffs =
302	    (volatile struct ie_xmit_buf **)&sc->xmit_cmds[sc->ntxbufs];
303	sc->xmit_cbuffs = (volatile u_char **)&sc->xmit_buffs[sc->ntxbufs];
304
305	if (bootverbose)
306		device_printf(sc->dev, "hardware type %s, revision %d\n",
307			ie_hardware_names[sc->hard_type], sc->hard_vers + 1);
308
309	ifp->if_softc = sc;
310	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
311	ifp->if_mtu = ETHERMTU;
312	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
313	    IFF_NEEDSGIANT;
314	ifp->if_start = iestart;
315	ifp->if_ioctl = ieioctl;
316	ifp->if_init = ieinit;
317	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
318
319	if (sc->hard_type == IE_EE16)
320		EVENTHANDLER_REGISTER(shutdown_post_sync, ee16_shutdown,
321				      sc, SHUTDOWN_PRI_DEFAULT);
322
323	ether_ifattach(ifp, sc->arpcom.ac_enaddr);
324	return (0);
325}
326
327static __inline void
328ie_ack(struct ie_softc *sc, u_int mask)
329{
330
331	sc->scb->ie_command = sc->scb->ie_status & mask;
332	(*sc->ie_chan_attn) (sc);
333}
334
335/*
336 * What to do upon receipt of an interrupt.
337 */
338void
339ie_intr(void *xsc)
340{
341	struct ie_softc *sc = (struct ie_softc *)xsc;
342	u_short status;
343
344	/* Clear the interrupt latch on the 3C507. */
345	if (sc->hard_type == IE_3C507
346	 && (inb(PORT(sc) + IE507_CTRL) & EL_CTRL_INTL))
347		outb(PORT(sc) + IE507_ICTRL, 1);
348
349	/* disable interrupts on the EE16. */
350	if (sc->hard_type == IE_EE16)
351		outb(PORT(sc) + IEE16_IRQ, sc->irq_encoded);
352
353	status = sc->scb->ie_status;
354
355loop:
356
357	/* Don't ack interrupts which we didn't receive */
358	ie_ack(sc, IE_ST_WHENCE & status);
359
360	if (status & (IE_ST_RECV | IE_ST_RNR)) {
361#ifdef DEBUG
362		in_ierint++;
363		if (ie_debug & IED_RINT)
364			printf("ie%d: rint\n", sc->unit);
365#endif
366		ierint(sc);
367#ifdef DEBUG
368		in_ierint--;
369#endif
370	}
371	if (status & IE_ST_DONE) {
372#ifdef DEBUG
373		in_ietint++;
374		if (ie_debug & IED_TINT)
375			printf("ie%d: tint\n", sc->unit);
376#endif
377		ietint(sc);
378#ifdef DEBUG
379		in_ietint--;
380#endif
381	}
382	if (status & IE_ST_RNR) {
383#ifdef DEBUG
384		if (ie_debug & IED_RNR)
385			printf("ie%d: rnr\n", sc->unit);
386#endif
387		iernr(sc);
388	}
389#ifdef DEBUG
390	if ((status & IE_ST_ALLDONE) && (ie_debug & IED_CNA))
391		printf("ie%d: cna\n", sc->unit);
392#endif
393
394	if ((status = sc->scb->ie_status) & IE_ST_WHENCE)
395		goto loop;
396
397	/* Clear the interrupt latch on the 3C507. */
398	if (sc->hard_type == IE_3C507)
399		outb(PORT(sc) + IE507_ICTRL, 1);
400
401	/* enable interrupts on the EE16. */
402	if (sc->hard_type == IE_EE16)
403		outb(PORT(sc) + IEE16_IRQ, sc->irq_encoded | IEE16_IRQ_ENABLE);
404
405}
406
407/*
408 * Process a received-frame interrupt.
409 */
410static int
411ierint(struct ie_softc *sc)
412{
413	int	i, status;
414	static int timesthru = 1024;
415
416	i = sc->rfhead;
417	while (1) {
418		status = sc->rframes[i]->ie_fd_status;
419
420		if ((status & IE_FD_COMPLETE) && (status & IE_FD_OK)) {
421			sc->arpcom.ac_if.if_ipackets++;
422			if (!--timesthru) {
423				sc->arpcom.ac_if.if_ierrors +=
424				    sc->scb->ie_err_crc +
425				    sc->scb->ie_err_align +
426				    sc->scb->ie_err_resource +
427				    sc->scb->ie_err_overrun;
428				sc->scb->ie_err_crc = 0;
429				sc->scb->ie_err_align = 0;
430				sc->scb->ie_err_resource = 0;
431				sc->scb->ie_err_overrun = 0;
432				timesthru = 1024;
433			}
434			ie_readframe(sc, i);
435		} else {
436			if (status & IE_FD_RNR) {
437				if (!(sc->scb->ie_status & IE_RU_READY)) {
438					sc->rframes[0]->ie_fd_next =
439					    MK_16(MEM(sc), sc->rbuffs[0]);
440					sc->scb->ie_recv_list =
441					    MK_16(MEM(sc), sc->rframes[0]);
442					command_and_wait(sc, IE_RU_START, 0, 0);
443				}
444			}
445			break;
446		}
447		i = (i + 1) % sc->nframes;
448	}
449	return (0);
450}
451
452/*
453 * Process a command-complete interrupt.  These are only generated by
454 * the transmission of frames.	This routine is deceptively simple, since
455 * most of the real work is done by iestart().
456 */
457static int
458ietint(struct ie_softc *sc)
459{
460	int	status;
461	int	i;
462
463	sc->arpcom.ac_if.if_timer = 0;
464	sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
465
466	for (i = 0; i < sc->xmit_count; i++) {
467		status = sc->xmit_cmds[i]->ie_xmit_status;
468
469		if (status & IE_XS_LATECOLL) {
470			printf("ie%d: late collision\n", sc->unit);
471			sc->arpcom.ac_if.if_collisions++;
472			sc->arpcom.ac_if.if_oerrors++;
473		} else if (status & IE_XS_NOCARRIER) {
474			printf("ie%d: no carrier\n", sc->unit);
475			sc->arpcom.ac_if.if_oerrors++;
476		} else if (status & IE_XS_LOSTCTS) {
477			printf("ie%d: lost CTS\n", sc->unit);
478			sc->arpcom.ac_if.if_oerrors++;
479		} else if (status & IE_XS_UNDERRUN) {
480			printf("ie%d: DMA underrun\n", sc->unit);
481			sc->arpcom.ac_if.if_oerrors++;
482		} else if (status & IE_XS_EXCMAX) {
483			printf("ie%d: too many collisions\n", sc->unit);
484			sc->arpcom.ac_if.if_collisions += 16;
485			sc->arpcom.ac_if.if_oerrors++;
486		} else {
487			sc->arpcom.ac_if.if_opackets++;
488			sc->arpcom.ac_if.if_collisions += status & IE_XS_MAXCOLL;
489		}
490	}
491	sc->xmit_count = 0;
492
493	/*
494	 * If multicast addresses were added or deleted while we were
495	 * transmitting, ie_mc_reset() set the want_mcsetup flag indicating
496	 * that we should do it.
497	 */
498	if (sc->want_mcsetup) {
499		mc_setup(sc);
500		sc->want_mcsetup = 0;
501	}
502	/* Wish I knew why this seems to be necessary... */
503	sc->xmit_cmds[0]->ie_xmit_status |= IE_STAT_COMPL;
504
505	iestart(&sc->arpcom.ac_if);
506	return (0);		/* shouldn't be necessary */
507}
508
509/*
510 * Process a receiver-not-ready interrupt.  I believe that we get these
511 * when there aren't enough buffers to go around.  For now (FIXME), we
512 * just restart the receiver, and hope everything's ok.
513 */
514static int
515iernr(struct ie_softc *sc)
516{
517#ifdef doesnt_work
518	setup_rfa(sc, (v_caddr_t) sc->rframes[0]);
519
520	sc->scb->ie_recv_list = MK_16(MEM(sc), sc->rframes[0]);
521	command_and_wait(sc, IE_RU_START, 0, 0);
522#else
523	/* This doesn't work either, but it doesn't hang either. */
524	command_and_wait(sc, IE_RU_DISABLE, 0, 0);	/* just in case */
525	setup_rfa(sc, (v_caddr_t) sc->rframes[0]);	/* ignore cast-qual */
526
527	sc->scb->ie_recv_list = MK_16(MEM(sc), sc->rframes[0]);
528	command_and_wait(sc, IE_RU_START, 0, 0);	/* was ENABLE */
529
530#endif
531	ie_ack(sc, IE_ST_WHENCE);
532
533	sc->arpcom.ac_if.if_ierrors++;
534	return (0);
535}
536
537/*
538 * Compare two Ether/802 addresses for equality, inlined and
539 * unrolled for speed.	I'd love to have an inline assembler
540 * version of this...
541 */
542static __inline int
543ether_equal(u_char * one, u_char * two)
544{
545	if (one[0] != two[0])
546		return (0);
547	if (one[1] != two[1])
548		return (0);
549	if (one[2] != two[2])
550		return (0);
551	if (one[3] != two[3])
552		return (0);
553	if (one[4] != two[4])
554		return (0);
555	if (one[5] != two[5])
556		return (0);
557	return 1;
558}
559
560/*
561 * Determine quickly whether we should bother reading in this packet.
562 * This depends on whether BPF and/or bridging is enabled, whether we
563 * are receiving multicast address, and whether promiscuous mode is enabled.
564 * We assume that if IFF_PROMISC is set, then *somebody* wants to see
565 * all incoming packets.
566 */
567static __inline int
568check_eh(struct ie_softc *sc, struct ether_header *eh)
569{
570	/* Optimize the common case: normal operation. We've received
571	   either a unicast with our dest or a multicast packet. */
572	if (sc->promisc == 0) {
573		int i;
574
575		/* If not multicast, it's definitely for us */
576		if ((eh->ether_dhost[0] & 1) == 0)
577			return (1);
578
579		/* Accept broadcasts (loose but fast check) */
580		if (eh->ether_dhost[0] == 0xff)
581			return (1);
582
583		/* Compare against our multicast addresses */
584		for (i = 0; i < sc->mcast_count; i++) {
585			if (ether_equal(eh->ether_dhost,
586			    (u_char *)&sc->mcast_addrs[i]))
587				return (1);
588		}
589		return (0);
590	}
591
592	/* Always accept packets when in promiscuous mode */
593	if ((sc->promisc & IFF_PROMISC) != 0)
594		return (1);
595
596	/* Always accept packets directed at us */
597	if (ether_equal(eh->ether_dhost, sc->arpcom.ac_enaddr))
598		return (1);
599
600	/* Must have IFF_ALLMULTI but not IFF_PROMISC set. The chip is
601	   actually in promiscuous mode, so discard unicast packets. */
602	return((eh->ether_dhost[0] & 1) != 0);
603}
604
605/*
606 * We want to isolate the bits that have meaning...  This assumes that
607 * IE_RBUF_SIZE is an even power of two.  If somehow the act_len exceeds
608 * the size of the buffer, then we are screwed anyway.
609 */
610static __inline int
611ie_buflen(struct ie_softc *sc, int head)
612{
613	return (sc->rbuffs[head]->ie_rbd_actual
614		& (IE_RBUF_SIZE | (IE_RBUF_SIZE - 1)));
615}
616
617static __inline int
618ie_packet_len(struct ie_softc *sc)
619{
620	int	i;
621	int	head = sc->rbhead;
622	int	acc = 0;
623
624	do {
625		if (!(sc->rbuffs[sc->rbhead]->ie_rbd_actual & IE_RBD_USED)) {
626#ifdef DEBUG
627			print_rbd(sc->rbuffs[sc->rbhead]);
628#endif
629			log(LOG_ERR,
630			    "ie%d: receive descriptors out of sync at %d\n",
631			    sc->unit, sc->rbhead);
632			iereset(sc);
633			return (-1);
634		}
635		i = sc->rbuffs[head]->ie_rbd_actual & IE_RBD_LAST;
636
637		acc += ie_buflen(sc, head);
638		head = (head + 1) % sc->nrxbufs;
639	} while (!i);
640
641	return (acc);
642}
643
644/*
645 * Read data off the interface, and turn it into an mbuf chain.
646 *
647 * This code is DRAMATICALLY different from the previous version; this
648 * version tries to allocate the entire mbuf chain up front, given the
649 * length of the data available.  This enables us to allocate mbuf
650 * clusters in many situations where before we would have had a long
651 * chain of partially-full mbufs.  This should help to speed up the
652 * operation considerably.  (Provided that it works, of course.)
653 */
654static __inline int
655ieget(struct ie_softc *sc, struct mbuf **mp)
656{
657	struct	ether_header eh;
658	struct	mbuf *m, *top, **mymp;
659	int	offset;
660	int	totlen, resid;
661	int	thismboff;
662	int	head;
663
664	totlen = ie_packet_len(sc);
665	if (totlen <= 0)
666		return (-1);
667
668	/*
669	 * Snarf the Ethernet header.
670	 */
671	bcopy((caddr_t)sc->cbuffs[sc->rbhead], &eh, sizeof(struct ether_header));
672	/* ignore cast-qual warning here */
673
674	/*
675	 * As quickly as possible, check if this packet is for us. If not,
676	 * don't waste a single cycle copying the rest of the packet in.
677	 * This is only a consideration when FILTER is defined; i.e., when
678	 * we are either running BPF or doing multicasting.
679	 */
680	if (!check_eh(sc, &eh)) {
681		ie_drop_packet_buffer(sc);
682		sc->arpcom.ac_if.if_ierrors--;	/* just this case, it's not an
683						 * error
684						 */
685		return (-1);
686	}
687
688	MGETHDR(m, M_DONTWAIT, MT_DATA);
689	if (!m) {
690		ie_drop_packet_buffer(sc);
691		/* XXXX if_ierrors++; */
692		return (-1);
693	}
694
695	*mp = m;
696	m->m_pkthdr.rcvif = &sc->arpcom.ac_if;
697	m->m_len = MHLEN;
698	resid = m->m_pkthdr.len = totlen;
699	top = 0;
700
701	mymp = &top;
702
703	/*
704	 * This loop goes through and allocates mbufs for all the data we
705	 * will be copying in.	It does not actually do the copying yet.
706	 */
707	do {			/* while(resid > 0) */
708		/*
709		 * Try to allocate an mbuf to hold the data that we have.
710		 * If we already allocated one, just get another one and
711		 * stick it on the end (eventually).  If we don't already
712		 * have one, try to allocate an mbuf cluster big enough to
713		 * hold the whole packet, if we think it's reasonable, or a
714		 * single mbuf which may or may not be big enough. Got that?
715		 */
716		if (top) {
717			MGET(m, M_DONTWAIT, MT_DATA);
718			if (!m) {
719				m_freem(top);
720				ie_drop_packet_buffer(sc);
721				return (-1);
722			}
723			m->m_len = MLEN;
724		}
725		if (resid >= MINCLSIZE) {
726			MCLGET(m, M_DONTWAIT);
727			if (m->m_flags & M_EXT)
728				m->m_len = min(resid, MCLBYTES);
729		} else {
730			if (resid < m->m_len) {
731				if (!top && resid + max_linkhdr <= m->m_len)
732					m->m_data += max_linkhdr;
733				m->m_len = resid;
734			}
735		}
736		resid -= m->m_len;
737		*mymp = m;
738		mymp = &m->m_next;
739	} while (resid > 0);
740
741	resid = totlen;					/* remaining data */
742	offset = 0;					/* packet offset */
743	thismboff = 0;					/* offset in m */
744
745	m = top;					/* current mbuf */
746	head = sc->rbhead;				/* current rx buffer */
747
748	/*
749	 * Now we take the mbuf chain (hopefully only one mbuf most of the
750	 * time) and stuff the data into it.  There are no possible failures
751	 * at or after this point.
752	 */
753	while (resid > 0) {	/* while there's stuff left */
754		int	thislen = ie_buflen(sc, head) - offset;
755
756		/*
757		 * If too much data for the current mbuf, then fill the
758		 * current one up, go to the next one, and try again.
759		 */
760		if (thislen > m->m_len - thismboff) {
761			int	newlen = m->m_len - thismboff;
762
763			bcopy((v_caddr_t) (sc->cbuffs[head] + offset),
764			      mtod(m, caddr_t) +thismboff, (unsigned) newlen);
765			/* ignore cast-qual warning */
766			m = m->m_next;
767			thismboff = 0;		/* new mbuf, so no offset */
768			offset += newlen;	/* we are now this far into
769						 * the packet */
770			resid -= newlen;	/* so there is this much left
771						 * to get */
772			continue;
773		}
774		/*
775		 * If there is more than enough space in the mbuf to hold
776		 * the contents of this buffer, copy everything in, advance
777		 * pointers, and so on.
778		 */
779		if (thislen < m->m_len - thismboff) {
780			bcopy((v_caddr_t) (sc->cbuffs[head] + offset),
781			    mtod(m, caddr_t) +thismboff, (unsigned) thislen);
782			thismboff += thislen;	/* we are this far into the
783						 * mbuf */
784			resid -= thislen;	/* and this much is left */
785			goto nextbuf;
786		}
787		/*
788		 * Otherwise, there is exactly enough space to put this
789		 * buffer's contents into the current mbuf.  Do the
790		 * combination of the above actions.
791		 */
792		bcopy((v_caddr_t) (sc->cbuffs[head] + offset),
793		      mtod(m, caddr_t) + thismboff, (unsigned) thislen);
794		m = m->m_next;
795		thismboff = 0;		/* new mbuf, start at the beginning */
796		resid -= thislen;	/* and we are this far through */
797
798		/*
799		 * Advance all the pointers.  We can get here from either of
800		 * the last two cases, but never the first.
801		 */
802nextbuf:
803		offset = 0;
804		sc->rbuffs[head]->ie_rbd_actual = 0;
805		sc->rbuffs[head]->ie_rbd_length |= IE_RBD_LAST;
806		sc->rbhead = head = (head + 1) % sc->nrxbufs;
807		sc->rbuffs[sc->rbtail]->ie_rbd_length &= ~IE_RBD_LAST;
808		sc->rbtail = (sc->rbtail + 1) % sc->nrxbufs;
809	}
810
811	/*
812	 * Unless something changed strangely while we were doing the copy,
813	 * we have now copied everything in from the shared memory. This
814	 * means that we are done.
815	 */
816	return (0);
817}
818
819/*
820 * Read frame NUM from unit UNIT (pre-cached as IE).
821 *
822 * This routine reads the RFD at NUM, and copies in the buffers from
823 * the list of RBD, then rotates the RBD and RFD lists so that the receiver
824 * doesn't start complaining.  Trailers are DROPPED---there's no point
825 * in wasting time on confusing code to deal with them.	 Hopefully,
826 * this machine will never ARP for trailers anyway.
827 */
828static void
829ie_readframe(struct ie_softc *sc, int	num/* frame number to read */)
830{
831	struct ifnet *ifp = &sc->arpcom.ac_if;
832	struct ie_recv_frame_desc rfd;
833	struct mbuf *m = 0;
834#ifdef DEBUG
835	struct ether_header *eh;
836#endif
837
838	bcopy((v_caddr_t) (sc->rframes[num]), &rfd,
839	      sizeof(struct ie_recv_frame_desc));
840
841	/*
842	 * Immediately advance the RFD list, since we we have copied ours
843	 * now.
844	 */
845	sc->rframes[num]->ie_fd_status = 0;
846	sc->rframes[num]->ie_fd_last |= IE_FD_LAST;
847	sc->rframes[sc->rftail]->ie_fd_last &= ~IE_FD_LAST;
848	sc->rftail = (sc->rftail + 1) % sc->nframes;
849	sc->rfhead = (sc->rfhead + 1) % sc->nframes;
850
851	if (rfd.ie_fd_status & IE_FD_OK) {
852		if (ieget(sc, &m)) {
853			sc->arpcom.ac_if.if_ierrors++;	/* this counts as an
854							 * error */
855			return;
856		}
857	}
858#ifdef DEBUG
859	eh = mtod(m, struct ether_header *);
860	if (ie_debug & IED_READFRAME) {
861		printf("ie%d: frame from ether %6D type %x\n", sc->unit,
862		       eh->ether_shost, ":", (unsigned) eh->ether_type);
863	}
864	if (ntohs(eh->ether_type) > ETHERTYPE_TRAIL
865	    && ntohs(eh->ether_type) < (ETHERTYPE_TRAIL + ETHERTYPE_NTRAILER))
866		printf("received trailer!\n");
867#endif
868
869	if (!m)
870		return;
871
872	/*
873	 * Finally pass this packet up to higher layers.
874	 */
875	(*ifp->if_input)(ifp, m);
876}
877
878static void
879ie_drop_packet_buffer(struct ie_softc *sc)
880{
881	int	i;
882
883	do {
884		/*
885		 * This means we are somehow out of sync.  So, we reset the
886		 * adapter.
887		 */
888		if (!(sc->rbuffs[sc->rbhead]->ie_rbd_actual & IE_RBD_USED)) {
889#ifdef DEBUG
890			print_rbd(sc->rbuffs[sc->rbhead]);
891#endif
892			log(LOG_ERR, "ie%d: receive descriptors out of sync at %d\n",
893			    sc->unit, sc->rbhead);
894			iereset(sc);
895			return;
896		}
897		i = sc->rbuffs[sc->rbhead]->ie_rbd_actual & IE_RBD_LAST;
898
899		sc->rbuffs[sc->rbhead]->ie_rbd_length |= IE_RBD_LAST;
900		sc->rbuffs[sc->rbhead]->ie_rbd_actual = 0;
901		sc->rbhead = (sc->rbhead + 1) % sc->nrxbufs;
902		sc->rbuffs[sc->rbtail]->ie_rbd_length &= ~IE_RBD_LAST;
903		sc->rbtail = (sc->rbtail + 1) % sc->nrxbufs;
904	} while (!i);
905}
906
907
908/*
909 * Start transmission on an interface.
910 */
911static void
912iestart(struct ifnet *ifp)
913{
914	struct	 ie_softc *sc = ifp->if_softc;
915	struct	 mbuf *m0, *m;
916	volatile unsigned char *buffer;
917	u_short	 len;
918
919	/*
920	 * This is not really volatile, in this routine, but it makes gcc
921	 * happy.
922	 */
923	volatile u_short *bptr = &sc->scb->ie_command_list;
924
925	if (!(ifp->if_flags & IFF_RUNNING))
926		return;
927	if (ifp->if_flags & IFF_OACTIVE)
928		return;
929
930	do {
931		IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, m);
932		if (!m)
933			break;
934
935		buffer = sc->xmit_cbuffs[sc->xmit_count];
936		len = 0;
937
938		for (m0 = m; m && len < IE_BUF_LEN; m = m->m_next) {
939			bcopy(mtod(m, caddr_t), buffer, m->m_len);
940			buffer += m->m_len;
941			len += m->m_len;
942		}
943
944		m_freem(m0);
945		len = max(len, ETHER_MIN_LEN);
946
947		/*
948		 * See if bpf is listening on this interface, let it see the
949		 * packet before we commit it to the wire.
950		 */
951		BPF_TAP(&sc->arpcom.ac_if,
952			(void *)sc->xmit_cbuffs[sc->xmit_count], len);
953
954		sc->xmit_buffs[sc->xmit_count]->ie_xmit_flags =
955		    IE_XMIT_LAST|len;
956		sc->xmit_buffs[sc->xmit_count]->ie_xmit_next = 0xffff;
957		sc->xmit_buffs[sc->xmit_count]->ie_xmit_buf =
958		    MK_24(sc->iomem, sc->xmit_cbuffs[sc->xmit_count]);
959
960		sc->xmit_cmds[sc->xmit_count]->com.ie_cmd_cmd = IE_CMD_XMIT;
961		sc->xmit_cmds[sc->xmit_count]->ie_xmit_status = 0;
962		sc->xmit_cmds[sc->xmit_count]->ie_xmit_desc =
963		    MK_16(sc->iomem, sc->xmit_buffs[sc->xmit_count]);
964
965		*bptr = MK_16(sc->iomem, sc->xmit_cmds[sc->xmit_count]);
966		bptr = &sc->xmit_cmds[sc->xmit_count]->com.ie_cmd_link;
967		sc->xmit_count++;
968	} while (sc->xmit_count < sc->ntxbufs);
969
970	/*
971	 * If we queued up anything for transmission, send it.
972	 */
973	if (sc->xmit_count) {
974		sc->xmit_cmds[sc->xmit_count - 1]->com.ie_cmd_cmd |=
975		    IE_CMD_LAST | IE_CMD_INTR;
976
977		/*
978		 * By passing the command pointer as a null, we tell
979		 * command_and_wait() to pretend that this isn't an action
980		 * command.  I wish I understood what was happening here.
981		 */
982		command_and_wait(sc, IE_CU_START, 0, 0);
983		ifp->if_flags |= IFF_OACTIVE;
984	}
985	return;
986}
987
988/*
989 * Check to see if there's an 82586 out there.
990 */
991int
992check_ie_present(struct ie_softc *sc)
993{
994	volatile struct ie_sys_conf_ptr *scp;
995	volatile struct ie_int_sys_conf_ptr *iscp;
996	volatile struct ie_sys_ctl_block *scb;
997	u_long	realbase;
998	int	s;
999
1000	s = splimp();
1001
1002	realbase = (uintptr_t) sc->iomembot + sc->iosize  - (1 << 24);
1003
1004	scp = (volatile struct ie_sys_conf_ptr *) (uintptr_t)
1005	      (realbase + IE_SCP_ADDR);
1006	bzero((volatile char *) scp, sizeof *scp);
1007
1008	/*
1009	 * First we put the ISCP at the bottom of memory; this tests to make
1010	 * sure that our idea of the size of memory is the same as the
1011	 * controller's. This is NOT where the ISCP will be in normal
1012	 * operation.
1013	 */
1014	iscp = (volatile struct ie_int_sys_conf_ptr *) sc->iomembot;
1015	bzero((volatile char *)iscp, sizeof *iscp);
1016
1017	scb = (volatile struct ie_sys_ctl_block *) sc->iomembot;
1018	bzero((volatile char *)scb, sizeof *scb);
1019
1020	scp->ie_bus_use = sc->bus_use;	/* 8-bit or 16-bit */
1021	scp->ie_iscp_ptr = (caddr_t) (uintptr_t)
1022	    ((volatile char *) iscp - (volatile char *) (uintptr_t) realbase);
1023
1024	iscp->ie_busy = 1;
1025	iscp->ie_scb_offset = MK_16(realbase, scb) + 256;
1026
1027	(*sc->ie_reset_586) (sc);
1028	(*sc->ie_chan_attn) (sc);
1029
1030	DELAY(100);		/* wait a while... */
1031
1032	if (iscp->ie_busy) {
1033		splx(s);
1034		return (0);
1035	}
1036	/*
1037	 * Now relocate the ISCP to its real home, and reset the controller
1038	 * again.
1039	 */
1040	iscp = (void *) Align((caddr_t) (uintptr_t)
1041			      (realbase + IE_SCP_ADDR -
1042			       sizeof(struct ie_int_sys_conf_ptr)));
1043	bzero((volatile char *) iscp, sizeof *iscp);	/* ignore cast-qual */
1044
1045	scp->ie_iscp_ptr = (caddr_t) (uintptr_t)
1046	    ((volatile char *) iscp - (volatile char *) (uintptr_t) realbase);
1047
1048	iscp->ie_busy = 1;
1049	iscp->ie_scb_offset = MK_16(realbase, scb);
1050
1051	(*sc->ie_reset_586) (sc);
1052	(*sc->ie_chan_attn) (sc);
1053
1054	DELAY(100);
1055
1056	if (iscp->ie_busy) {
1057		splx(s);
1058		return (0);
1059	}
1060	sc->iomem = (caddr_t) (uintptr_t) realbase;
1061
1062	sc->iscp = iscp;
1063	sc->scb = scb;
1064
1065	/*
1066	 * Acknowledge any interrupts we may have caused...
1067	 */
1068	ie_ack(sc, IE_ST_WHENCE);
1069	splx(s);
1070
1071	return (1);
1072}
1073
1074/*
1075 * Divine the memory size of ie board UNIT.
1076 * Better hope there's nothing important hiding just below the ie card...
1077 */
1078static void
1079find_ie_mem_size(struct ie_softc *sc)
1080{
1081	unsigned size;
1082
1083	sc->iosize = 0;
1084
1085	for (size = 65536; size >= 8192; size -= 8192) {
1086		if (check_ie_present(sc)) {
1087			return;
1088		}
1089	}
1090
1091	return;
1092}
1093
1094void
1095el_reset_586(struct ie_softc *sc)
1096{
1097	outb(PORT(sc) + IE507_CTRL, EL_CTRL_RESET);
1098	DELAY(100);
1099	outb(PORT(sc) + IE507_CTRL, EL_CTRL_NORMAL);
1100	DELAY(100);
1101}
1102
1103void
1104sl_reset_586(struct ie_softc *sc)
1105{
1106	outb(PORT(sc) + IEATT_RESET, 0);
1107}
1108
1109void
1110ee16_reset_586(struct ie_softc *sc)
1111{
1112	outb(PORT(sc) + IEE16_ECTRL, IEE16_RESET_586);
1113	DELAY(100);
1114	outb(PORT(sc) + IEE16_ECTRL, 0);
1115	DELAY(100);
1116}
1117
1118void
1119el_chan_attn(struct ie_softc *sc)
1120{
1121	outb(PORT(sc) + IE507_ATTN, 1);
1122}
1123
1124void
1125sl_chan_attn(struct ie_softc *sc)
1126{
1127	outb(PORT(sc) + IEATT_ATTN, 0);
1128}
1129
1130void
1131ee16_chan_attn(struct ie_softc *sc)
1132{
1133	outb(PORT(sc) + IEE16_ATTN, 0);
1134}
1135
1136u_short
1137ee16_read_eeprom(struct ie_softc *sc, int location)
1138{
1139	int	ectrl, edata;
1140
1141	ectrl = inb(sc->port + IEE16_ECTRL);
1142	ectrl &= IEE16_ECTRL_MASK;
1143	ectrl |= IEE16_ECTRL_EECS;
1144	outb(sc->port + IEE16_ECTRL, ectrl);
1145
1146	ee16_eeprom_outbits(sc, IEE16_EEPROM_READ, IEE16_EEPROM_OPSIZE1);
1147	ee16_eeprom_outbits(sc, location, IEE16_EEPROM_ADDR_SIZE);
1148	edata = ee16_eeprom_inbits(sc);
1149	ectrl = inb(sc->port + IEE16_ECTRL);
1150	ectrl &= ~(IEE16_RESET_ASIC | IEE16_ECTRL_EEDI | IEE16_ECTRL_EECS);
1151	outb(sc->port + IEE16_ECTRL, ectrl);
1152	ee16_eeprom_clock(sc, 1);
1153	ee16_eeprom_clock(sc, 0);
1154	return edata;
1155}
1156
1157static void
1158ee16_eeprom_outbits(struct ie_softc *sc, int edata, int count)
1159{
1160	int	ectrl, i;
1161
1162	ectrl = inb(sc->port + IEE16_ECTRL);
1163	ectrl &= ~IEE16_RESET_ASIC;
1164	for (i = count - 1; i >= 0; i--) {
1165		ectrl &= ~IEE16_ECTRL_EEDI;
1166		if (edata & (1 << i)) {
1167			ectrl |= IEE16_ECTRL_EEDI;
1168		}
1169		outb(sc->port + IEE16_ECTRL, ectrl);
1170		DELAY(1);	/* eeprom data must be setup for 0.4 uSec */
1171		ee16_eeprom_clock(sc, 1);
1172		ee16_eeprom_clock(sc, 0);
1173	}
1174	ectrl &= ~IEE16_ECTRL_EEDI;
1175	outb(sc->port + IEE16_ECTRL, ectrl);
1176	DELAY(1);		/* eeprom data must be held for 0.4 uSec */
1177}
1178
1179static int
1180ee16_eeprom_inbits(struct ie_softc *sc)
1181{
1182	int	ectrl, edata, i;
1183
1184	ectrl = inb(sc->port + IEE16_ECTRL);
1185	ectrl &= ~IEE16_RESET_ASIC;
1186	for (edata = 0, i = 0; i < 16; i++) {
1187		edata = edata << 1;
1188		ee16_eeprom_clock(sc, 1);
1189		ectrl = inb(sc->port + IEE16_ECTRL);
1190		if (ectrl & IEE16_ECTRL_EEDO) {
1191			edata |= 1;
1192		}
1193		ee16_eeprom_clock(sc, 0);
1194	}
1195	return (edata);
1196}
1197
1198static void
1199ee16_eeprom_clock(struct ie_softc *sc, int state)
1200{
1201	int	ectrl;
1202
1203	ectrl = inb(sc->port + IEE16_ECTRL);
1204	ectrl &= ~(IEE16_RESET_ASIC | IEE16_ECTRL_EESK);
1205	if (state) {
1206		ectrl |= IEE16_ECTRL_EESK;
1207	}
1208	outb(sc->port + IEE16_ECTRL, ectrl);
1209	DELAY(9);		/* EESK must be stable for 8.38 uSec */
1210}
1211
1212static __inline void
1213ee16_interrupt_enable(struct ie_softc *sc)
1214{
1215	DELAY(100);
1216	outb(sc->port + IEE16_IRQ, sc->irq_encoded | IEE16_IRQ_ENABLE);
1217	DELAY(100);
1218}
1219
1220void
1221sl_read_ether(struct ie_softc *sc, unsigned char *addr)
1222{
1223	int	i;
1224
1225	for (i = 0; i < 6; i++)
1226		addr[i] = inb(PORT(sc) + i);
1227}
1228
1229static void
1230iereset(struct ie_softc *sc)
1231{
1232	int	s = splimp();
1233
1234	printf("ie%d: reset\n", sc->unit);
1235	sc->arpcom.ac_if.if_flags &= ~IFF_UP;
1236	ieioctl(&sc->arpcom.ac_if, SIOCSIFFLAGS, 0);
1237
1238	/*
1239	 * Stop i82586 dead in its tracks.
1240	 */
1241	if (command_and_wait(sc, IE_RU_ABORT | IE_CU_ABORT, 0, 0))
1242		printf("ie%d: abort commands timed out\n", sc->unit);
1243
1244	if (command_and_wait(sc, IE_RU_DISABLE | IE_CU_STOP, 0, 0))
1245		printf("ie%d: disable commands timed out\n", sc->unit);
1246
1247#ifdef notdef
1248	if (!check_ie_present(sc))
1249		panic("ie disappeared!");
1250#endif
1251
1252	sc->arpcom.ac_if.if_flags |= IFF_UP;
1253	ieioctl(&sc->arpcom.ac_if, SIOCSIFFLAGS, 0);
1254
1255	splx(s);
1256	return;
1257}
1258
1259/*
1260 * This is called if we time out.
1261 */
1262static void
1263chan_attn_timeout(void *rock)
1264{
1265	*(int *) rock = 1;
1266}
1267
1268/*
1269 * Send a command to the controller and wait for it to either
1270 * complete or be accepted, depending on the command.  If the
1271 * command pointer is null, then pretend that the command is
1272 * not an action command.  If the command pointer is not null,
1273 * and the command is an action command, wait for
1274 * ((volatile struct ie_cmd_common *)pcmd)->ie_cmd_status & MASK
1275 * to become true.
1276 */
1277static int
1278command_and_wait(struct ie_softc *sc, int cmd, volatile void *pcmd, int mask)
1279{
1280	volatile struct ie_cmd_common *cc = pcmd;
1281	volatile int timedout = 0;
1282	struct	 callout_handle ch;
1283
1284	sc->scb->ie_command = (u_short) cmd;
1285
1286	if (IE_ACTION_COMMAND(cmd) && pcmd) {
1287		(*sc->ie_chan_attn) (sc);
1288
1289		/*
1290		 * According to the packet driver, the minimum timeout
1291		 * should be .369 seconds, which we round up to .37.
1292		 */
1293		ch = timeout(chan_attn_timeout, (caddr_t)&timedout,
1294			     37 * hz / 100);
1295		/* ignore cast-qual */
1296
1297		/*
1298		 * Now spin-lock waiting for status.  This is not a very
1299		 * nice thing to do, but I haven't figured out how, or
1300		 * indeed if, we can put the process waiting for action to
1301		 * sleep.  (We may be getting called through some other
1302		 * timeout running in the kernel.)
1303		 */
1304		while (1) {
1305			if ((cc->ie_cmd_status & mask) || timedout)
1306				break;
1307		}
1308
1309		untimeout(chan_attn_timeout, (caddr_t)&timedout, ch);
1310		/* ignore cast-qual */
1311
1312		return (timedout);
1313	} else {
1314
1315		/*
1316		 * Otherwise, just wait for the command to be accepted.
1317		 */
1318		(*sc->ie_chan_attn) (sc);
1319
1320		while (sc->scb->ie_command);	/* spin lock */
1321
1322		return (0);
1323	}
1324}
1325
1326/*
1327 * Run the time-domain reflectometer...
1328 */
1329static void
1330run_tdr(struct ie_softc *sc, volatile struct ie_tdr_cmd *cmd)
1331{
1332	int	result;
1333
1334	cmd->com.ie_cmd_status = 0;
1335	cmd->com.ie_cmd_cmd = IE_CMD_TDR | IE_CMD_LAST;
1336	cmd->com.ie_cmd_link = 0xffff;
1337	cmd->ie_tdr_time = 0;
1338
1339	sc->scb->ie_command_list = MK_16(MEM(sc), cmd);
1340	cmd->ie_tdr_time = 0;
1341
1342	if (command_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL))
1343		result = 0x2000;
1344	else
1345		result = cmd->ie_tdr_time;
1346
1347	ie_ack(sc, IE_ST_WHENCE);
1348
1349	if (result & IE_TDR_SUCCESS)
1350		return;
1351
1352	if (result & IE_TDR_XCVR) {
1353		printf("ie%d: transceiver problem\n", sc->unit);
1354	} else if (result & IE_TDR_OPEN) {
1355		printf("ie%d: TDR detected an open %d clocks away\n", sc->unit,
1356		       result & IE_TDR_TIME);
1357	} else if (result & IE_TDR_SHORT) {
1358		printf("ie%d: TDR detected a short %d clocks away\n", sc->unit,
1359		       result & IE_TDR_TIME);
1360	} else {
1361		printf("ie%d: TDR returned unknown status %x\n", sc->unit, result);
1362	}
1363}
1364
1365static void
1366start_receiver(struct ie_softc *sc)
1367{
1368	int	s = splimp();
1369
1370	sc->scb->ie_recv_list = MK_16(MEM(sc), sc->rframes[0]);
1371	command_and_wait(sc, IE_RU_START, 0, 0);
1372
1373	ie_ack(sc, IE_ST_WHENCE);
1374
1375	splx(s);
1376}
1377
1378/*
1379 * Here is a helper routine for iernr() and ieinit().  This sets up
1380 * the RFA.
1381 */
1382static v_caddr_t
1383setup_rfa(struct ie_softc *sc, v_caddr_t ptr)
1384{
1385	volatile struct ie_recv_frame_desc *rfd = (volatile void *)ptr;
1386	volatile struct ie_recv_buf_desc *rbd;
1387	int	i;
1388
1389	/* First lay them out */
1390	for (i = 0; i < sc->nframes; i++) {
1391		sc->rframes[i] = rfd;
1392		bzero((volatile char *) rfd, sizeof *rfd);	/* ignore cast-qual */
1393		rfd++;
1394	}
1395
1396	ptr = Alignvol(rfd);		/* ignore cast-qual */
1397
1398	/* Now link them together */
1399	for (i = 0; i < sc->nframes; i++) {
1400		sc->rframes[i]->ie_fd_next =
1401		    MK_16(MEM(sc), sc->rframes[(i + 1) % sc->nframes]);
1402	}
1403
1404	/* Finally, set the EOL bit on the last one. */
1405	sc->rframes[sc->nframes - 1]->ie_fd_last |= IE_FD_LAST;
1406
1407	/*
1408	 * Now lay out some buffers for the incoming frames.  Note that we
1409	 * set aside a bit of slop in each buffer, to make sure that we have
1410	 * enough space to hold a single frame in every buffer.
1411	 */
1412	rbd = (volatile void *) ptr;
1413
1414	for (i = 0; i < sc->nrxbufs; i++) {
1415		sc->rbuffs[i] = rbd;
1416		bzero((volatile char *)rbd, sizeof *rbd);
1417		ptr = Alignvol(ptr + sizeof *rbd);
1418		rbd->ie_rbd_length = IE_RBUF_SIZE;
1419		rbd->ie_rbd_buffer = MK_24(MEM(sc), ptr);
1420		sc->cbuffs[i] = (volatile void *) ptr;
1421		ptr += IE_RBUF_SIZE;
1422		rbd = (volatile void *) ptr;
1423	}
1424
1425	/* Now link them together */
1426	for (i = 0; i < sc->nrxbufs; i++) {
1427		sc->rbuffs[i]->ie_rbd_next =
1428		    MK_16(MEM(sc), sc->rbuffs[(i + 1) % sc->nrxbufs]);
1429	}
1430
1431	/* Tag EOF on the last one */
1432	sc->rbuffs[sc->nrxbufs - 1]->ie_rbd_length |= IE_RBD_LAST;
1433
1434	/*
1435	 * We use the head and tail pointers on receive to keep track of the
1436	 * order in which RFDs and RBDs are used.
1437	 */
1438	sc->rfhead = 0;
1439	sc->rftail = sc->nframes - 1;
1440	sc->rbhead = 0;
1441	sc->rbtail = sc->nrxbufs - 1;
1442
1443	sc->scb->ie_recv_list = MK_16(MEM(sc), sc->rframes[0]);
1444	sc->rframes[0]->ie_fd_buf_desc = MK_16(MEM(sc), sc->rbuffs[0]);
1445
1446	ptr = Alignvol(ptr);
1447	return (ptr);
1448}
1449
1450/*
1451 * Run the multicast setup command.
1452 * Call at splimp().
1453 */
1454static int
1455mc_setup(struct ie_softc *sc)
1456{
1457	volatile struct ie_mcast_cmd *cmd = (volatile void *)sc->xmit_cbuffs[0];
1458
1459	cmd->com.ie_cmd_status = 0;
1460	cmd->com.ie_cmd_cmd = IE_CMD_MCAST | IE_CMD_LAST;
1461	cmd->com.ie_cmd_link = 0xffff;
1462
1463	/* ignore cast-qual */
1464	bcopy((v_caddr_t) sc->mcast_addrs, (v_caddr_t) cmd->ie_mcast_addrs,
1465	      sc->mcast_count * sizeof *sc->mcast_addrs);
1466
1467	cmd->ie_mcast_bytes = sc->mcast_count * 6;	/* grrr... */
1468
1469	sc->scb->ie_command_list = MK_16(MEM(sc), cmd);
1470	if (command_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL)
1471	    || !(cmd->com.ie_cmd_status & IE_STAT_OK)) {
1472		printf("ie%d: multicast address setup command failed\n", sc->unit);
1473		return (0);
1474	}
1475	return (1);
1476}
1477
1478/*
1479 * This routine takes the environment generated by check_ie_present()
1480 * and adds to it all the other structures we need to operate the adapter.
1481 * This includes executing the CONFIGURE, IA-SETUP, and MC-SETUP commands,
1482 * starting the receiver unit, and clearing interrupts.
1483 *
1484 * THIS ROUTINE MUST BE CALLED AT splimp() OR HIGHER.
1485 */
1486static void
1487ieinit(xsc)
1488	void *xsc;
1489{
1490	struct ie_softc *sc = xsc;
1491	volatile struct ie_sys_ctl_block *scb = sc->scb;
1492	caddr_t ptr;
1493	int	i;
1494	int	unit = sc->unit;
1495
1496	ptr = Alignvol((volatile char *) scb + sizeof *scb);
1497
1498	/*
1499	 * Send the configure command first.
1500	 */
1501	{
1502		volatile struct ie_config_cmd *cmd = (volatile void *) ptr;
1503
1504		ie_setup_config(cmd, sc->promisc,
1505				sc->hard_type == IE_STARLAN10);
1506		cmd->com.ie_cmd_status = 0;
1507		cmd->com.ie_cmd_cmd = IE_CMD_CONFIG | IE_CMD_LAST;
1508		cmd->com.ie_cmd_link = 0xffff;
1509
1510		scb->ie_command_list = MK_16(MEM(sc), cmd);
1511
1512		if (command_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL)
1513		 || !(cmd->com.ie_cmd_status & IE_STAT_OK)) {
1514			printf("ie%d: configure command failed\n", unit);
1515			return;
1516		}
1517	}
1518	/*
1519	 * Now send the Individual Address Setup command.
1520	 */
1521	{
1522		volatile struct ie_iasetup_cmd *cmd = (volatile void *) ptr;
1523
1524		cmd->com.ie_cmd_status = 0;
1525		cmd->com.ie_cmd_cmd = IE_CMD_IASETUP | IE_CMD_LAST;
1526		cmd->com.ie_cmd_link = 0xffff;
1527
1528		bcopy((volatile char *)sc->arpcom.ac_enaddr,
1529		      (volatile char *)&cmd->ie_address, sizeof cmd->ie_address);
1530		scb->ie_command_list = MK_16(MEM(sc), cmd);
1531		if (command_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL)
1532		    || !(cmd->com.ie_cmd_status & IE_STAT_OK)) {
1533			printf("ie%d: individual address "
1534			       "setup command failed\n", sc->unit);
1535			return;
1536		}
1537	}
1538
1539	/*
1540	 * Now run the time-domain reflectometer.
1541	 */
1542	run_tdr(sc, (volatile void *) ptr);
1543
1544	/*
1545	 * Acknowledge any interrupts we have generated thus far.
1546	 */
1547	ie_ack(sc, IE_ST_WHENCE);
1548
1549	/*
1550	 * Set up the RFA.
1551	 */
1552	ptr = setup_rfa(sc, ptr);
1553
1554	/*
1555	 * Finally, the transmit command and buffer are the last little bit
1556	 * of work.
1557	 */
1558
1559	/* transmit command buffers */
1560	for (i = 0; i < sc->ntxbufs; i++) {
1561		sc->xmit_cmds[i] = (volatile void *) ptr;
1562		ptr += sizeof *sc->xmit_cmds[i];
1563		ptr = Alignvol(ptr);
1564		sc->xmit_buffs[i] = (volatile void *)ptr;
1565		ptr += sizeof *sc->xmit_buffs[i];
1566		ptr = Alignvol(ptr);
1567	}
1568
1569	/* transmit buffers */
1570	for (i = 0; i < sc->ntxbufs - 1; i++) {
1571		sc->xmit_cbuffs[i] = (volatile void *)ptr;
1572		ptr += IE_BUF_LEN;
1573		ptr = Alignvol(ptr);
1574	}
1575	sc->xmit_cbuffs[sc->ntxbufs - 1] = (volatile void *) ptr;
1576
1577	for (i = 1; i < sc->ntxbufs; i++) {
1578		bzero((v_caddr_t) sc->xmit_cmds[i], sizeof *sc->xmit_cmds[i]);
1579		bzero((v_caddr_t) sc->xmit_buffs[i], sizeof *sc->xmit_buffs[i]);
1580	}
1581
1582	/*
1583	 * This must be coordinated with iestart() and ietint().
1584	 */
1585	sc->xmit_cmds[0]->ie_xmit_status = IE_STAT_COMPL;
1586
1587	/* take the ee16 out of loopback */
1588	if (sc->hard_type == IE_EE16) {
1589		u_int8_t bart_config;
1590
1591		bart_config = inb(PORT(sc) + IEE16_CONFIG);
1592		bart_config &= ~IEE16_BART_LOOPBACK;
1593		/* inb doesn't get bit! */
1594		bart_config |= IEE16_BART_MCS16_TEST;
1595		outb(PORT(sc) + IEE16_CONFIG, bart_config);
1596		ee16_interrupt_enable(sc);
1597		ee16_chan_attn(sc);
1598	}
1599	sc->arpcom.ac_if.if_flags |= IFF_RUNNING;	/* tell higher levels
1600							 * we're here */
1601	sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
1602
1603	start_receiver(sc);
1604
1605	return;
1606}
1607
1608static void
1609ie_stop(struct ie_softc *sc)
1610{
1611	command_and_wait(sc, IE_RU_DISABLE, 0, 0);
1612}
1613
1614static int
1615ieioctl(struct ifnet *ifp, u_long command, caddr_t data)
1616{
1617	int	s, error = 0;
1618	struct	 ie_softc *sc = ifp->if_softc;
1619
1620	s = splimp();
1621
1622	switch (command) {
1623	case SIOCSIFFLAGS:
1624		/*
1625		 * Note that this device doesn't have an "all multicast"
1626		 * mode, so we must turn on promiscuous mode and do the
1627		 * filtering manually.
1628		 */
1629		if ((ifp->if_flags & IFF_UP) == 0 &&
1630		    (ifp->if_flags & IFF_RUNNING)) {
1631			ifp->if_flags &= ~IFF_RUNNING;
1632			ie_stop(sc);
1633		} else if ((ifp->if_flags & IFF_UP) &&
1634			   (ifp->if_flags & IFF_RUNNING) == 0) {
1635			sc->promisc =
1636			    ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI);
1637			ieinit(sc);
1638		} else if (sc->promisc ^
1639			   (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))) {
1640			sc->promisc =
1641			    ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI);
1642			ieinit(sc);
1643		}
1644		break;
1645
1646	case SIOCADDMULTI:
1647	case SIOCDELMULTI:
1648		/*
1649		 * Update multicast listeners
1650		 */
1651		/* reset multicast filtering */
1652		ie_mc_reset(sc);
1653		error = 0;
1654		break;
1655
1656	default:
1657		error = ether_ioctl(ifp, command, data);
1658		break;
1659	}
1660
1661	splx(s);
1662	return (error);
1663}
1664
1665static void
1666ie_mc_reset(struct ie_softc *sc)
1667{
1668	struct ifmultiaddr *ifma;
1669
1670	/*
1671	 * Step through the list of addresses.
1672	 */
1673	sc->mcast_count = 0;
1674	TAILQ_FOREACH(ifma, &sc->arpcom.ac_if.if_multiaddrs, ifma_link) {
1675		if (ifma->ifma_addr->sa_family != AF_LINK)
1676			continue;
1677
1678		/* XXX - this is broken... */
1679		if (sc->mcast_count >= MAXMCAST) {
1680			sc->arpcom.ac_if.if_flags |= IFF_ALLMULTI;
1681			ieioctl(&sc->arpcom.ac_if, SIOCSIFFLAGS, (void *) 0);
1682			goto setflag;
1683		}
1684		bcopy(LLADDR((struct sockaddr_dl *) ifma->ifma_addr),
1685		      &(sc->mcast_addrs[sc->mcast_count]), 6);
1686		sc->mcast_count++;
1687	}
1688
1689setflag:
1690	sc->want_mcsetup = 1;
1691}
1692
1693
1694#ifdef DEBUG
1695static void
1696print_rbd(volatile struct ie_recv_buf_desc * rbd)
1697{
1698	printf("RBD at %p:\n"
1699	       "actual %04x, next %04x, buffer %p\n"
1700	       "length %04x, mbz %04x\n",
1701	       (volatile void *) rbd,
1702	       rbd->ie_rbd_actual, rbd->ie_rbd_next,
1703	       (void *) rbd->ie_rbd_buffer,
1704	       rbd->ie_rbd_length, rbd->mbz);
1705}
1706
1707#endif				/* DEBUG */
1708
1709int
1710ie_alloc_resources (device_t dev)
1711{
1712	struct ie_softc *       sc;
1713	int                     error;
1714
1715	error = 0;
1716	sc = device_get_softc(dev);
1717
1718	sc->io_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &sc->io_rid,
1719					    RF_ACTIVE);
1720	if (!sc->io_res) {
1721		device_printf(dev, "No I/O space?!\n");
1722		error = ENOMEM;
1723		goto bad;
1724	}
1725	sc->io_bt = rman_get_bustag(sc->io_res);
1726	sc->io_bh = rman_get_bushandle(sc->io_res);
1727
1728	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
1729					     RF_ACTIVE);
1730	if (!sc->mem_res) {
1731                device_printf(dev, "No Memory!\n");
1732		error = ENOMEM;
1733		goto bad;
1734	}
1735	sc->mem_bt = rman_get_bustag(sc->mem_res);
1736	sc->mem_bh = rman_get_bushandle(sc->mem_res);
1737
1738	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
1739					     RF_ACTIVE);
1740	if (!sc->irq_res) {
1741		device_printf(dev, "No IRQ!\n");
1742		error = ENOMEM;
1743		goto bad;
1744	}
1745
1746	sc->port = rman_get_start(sc->io_res);  /* XXX hack */
1747	sc->iomembot = rman_get_virtual(sc->mem_res);
1748	sc->iosize = rman_get_size(sc->mem_res);
1749
1750	return (0);
1751bad:
1752	return (error);
1753}
1754
1755void
1756ie_release_resources (device_t dev)
1757{
1758	struct ie_softc *       sc;
1759
1760	sc = device_get_softc(dev);
1761
1762	if (sc->irq_ih)
1763		bus_teardown_intr(dev, sc->irq_res, sc->irq_ih);
1764	if (sc->io_res)
1765		bus_release_resource(dev, SYS_RES_IOPORT,
1766				     sc->io_rid, sc->io_res);
1767	if (sc->irq_res)
1768		bus_release_resource(dev, SYS_RES_IRQ,
1769				     sc->irq_rid, sc->irq_res);
1770	if (sc->mem_res)
1771		bus_release_resource(dev, SYS_RES_MEMORY,
1772				     sc->mem_rid, sc->mem_res);
1773
1774	return;
1775}
1776
1777int
1778ie_detach (device_t dev)
1779{
1780	struct ie_softc *	sc;
1781	struct ifnet *		ifp;
1782
1783	sc = device_get_softc(dev);
1784	ifp = &sc->arpcom.ac_if;
1785
1786	if (sc->hard_type == IE_EE16)
1787		ee16_shutdown(sc, 0);
1788
1789	ie_stop(sc);
1790	ifp->if_flags &= ~IFF_RUNNING;
1791	ether_ifdetach(ifp);
1792	ie_release_resources(dev);
1793
1794	return (0);
1795}
1796