Home | History | Annotate | Line # | Download | only in ic
i82586.c revision 1.4
      1 /*	$NetBSD: i82586.c,v 1.4 1997/07/28 22:35:49 pk Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 Paul Kranenburg.
      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) 1994, 1995, Rafal K. Boni
     12  * Copyright (c) 1990, 1991, William F. Jolitz
     13  * Copyright (c) 1990, The Regents of the University of California
     14  *
     15  * All rights reserved.
     16  *
     17  * Redistribution and use in source and binary forms, with or without
     18  * modification, are permitted provided that the following conditions
     19  * are met:
     20  * 1. Redistributions of source code must retain the above copyright
     21  *    notice, this list of conditions and the following disclaimer.
     22  * 2. Redistributions in binary form must reproduce the above copyright
     23  *    notice, this list of conditions and the following disclaimer in the
     24  *    documentation and/or other materials provided with the distribution.
     25  * 3. All advertising materials mentioning features or use of this software
     26  *    must display the following acknowledgement:
     27  *	This product includes software developed by Charles Hannum, by the
     28  *	University of Vermont and State Agricultural College and Garrett A.
     29  *	Wollman, by William F. Jolitz, and by the University of California,
     30  *	Berkeley, Lawrence Berkeley Laboratory, and its contributors.
     31  * 4. Neither the names of the Universities nor the names of the authors
     32  *    may be used to endorse or promote products derived from this software
     33  *    without specific prior written permission.
     34  *
     35  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     36  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     37  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     38  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR AUTHORS BE LIABLE
     39  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     40  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     41  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     42  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     43  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     44  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     45  * SUCH DAMAGE.
     46  */
     47 
     48 /*
     49  * Intel 82586 Ethernet chip
     50  * Register, bit, and structure definitions.
     51  *
     52  * Original StarLAN driver written by Garrett Wollman with reference to the
     53  * Clarkson Packet Driver code for this chip written by Russ Nelson and others.
     54  *
     55  * BPF support code taken from hpdev/if_le.c, supplied with tcpdump.
     56  *
     57  * 3C507 support is loosely based on code donated to NetBSD by Rafal Boni.
     58  *
     59  * Majorly cleaned up and 3C507 code merged by Charles Hannum.
     60  *
     61  * Converted to SUN ie driver by Charles D. Cranor,
     62  *		October 1994, January 1995.
     63  * This sun version based on i386 version 1.30.
     64  */
     65 
     66 /*
     67  * The i82586 is a very painful chip, found in sun3's, sun-4/100's
     68  * sun-4/200's, and VME based suns.  The byte order is all wrong for a
     69  * SUN, making life difficult.  Programming this chip is mostly the same,
     70  * but certain details differ from system to system.  This driver is
     71  * written so that different "ie" interfaces can be controled by the same
     72  * driver.
     73  */
     74 
     75 /*
     76 Mode of operation:
     77 
     78    We run the 82586 in a standard Ethernet mode.  We keep NFRAMES
     79    received frame descriptors around for the receiver to use, and
     80    NRXBUF associated receive buffer descriptors, both in a circular
     81    list.  Whenever a frame is received, we rotate both lists as
     82    necessary.  (The 586 treats both lists as a simple queue.)  We also
     83    keep a transmit command around so that packets can be sent off
     84    quickly.
     85 
     86    We configure the adapter in AL-LOC = 1 mode, which means that the
     87    Ethernet/802.3 MAC header is placed at the beginning of the receive
     88    buffer rather than being split off into various fields in the RFD.
     89    This also means that we must include this header in the transmit
     90    buffer as well.
     91 
     92    By convention, all transmit commands, and only transmit commands,
     93    shall have the I (IE_CMD_INTR) bit set in the command.  This way,
     94    when an interrupt arrives at ieintr(), it is immediately possible
     95    to tell what precisely caused it.  ANY OTHER command-sending
     96    routines should run at splnet(), and should post an acknowledgement
     97    to every interrupt they generate.
     98 
     99 */
    100 
    101 #include "bpfilter.h"
    102 
    103 #include <sys/param.h>
    104 #include <sys/systm.h>
    105 #include <sys/mbuf.h>
    106 #include <sys/buf.h>
    107 #include <sys/protosw.h>
    108 #include <sys/socket.h>
    109 #include <sys/ioctl.h>
    110 #include <sys/errno.h>
    111 #include <sys/syslog.h>
    112 #include <sys/device.h>
    113 
    114 #include <net/if.h>
    115 #include <net/if_types.h>
    116 #include <net/if_dl.h>
    117 #include <net/if_ether.h>
    118 
    119 #if NBPFILTER > 0
    120 #include <net/bpf.h>
    121 #include <net/bpfdesc.h>
    122 #endif
    123 
    124 #ifdef INET
    125 #include <netinet/in.h>
    126 #include <netinet/in_systm.h>
    127 #include <netinet/in_var.h>
    128 #include <netinet/ip.h>
    129 #include <netinet/if_inarp.h>
    130 #endif
    131 
    132 #ifdef NS
    133 #include <netns/ns.h>
    134 #include <netns/ns_if.h>
    135 #endif
    136 
    137 #include <dev/ic/i82586reg.h>
    138 #include <dev/ic/i82586var.h>
    139 
    140 void iewatchdog __P((struct ifnet *));
    141 int ieinit __P((struct ie_softc *));
    142 int ieioctl __P((struct ifnet *, u_long, caddr_t));
    143 void iestart __P((struct ifnet *));
    144 void iereset __P((struct ie_softc *));
    145 static void ie_readframe __P((struct ie_softc *, int));
    146 static void ie_drop_packet_buffer __P((struct ie_softc *));
    147 int ie_setupram __P((struct ie_softc *));
    148 static int command_and_wait __P((struct ie_softc *, int,
    149     void volatile *, int));
    150 /*static*/ void ierint __P((struct ie_softc *));
    151 /*static*/ void ietint __P((struct ie_softc *));
    152 static struct mbuf *ieget __P((struct ie_softc *,
    153 		      struct ether_header *, int *));
    154 static void setup_bufs __P((struct ie_softc *));
    155 static int mc_setup __P((struct ie_softc *, void *));
    156 static void mc_reset __P((struct ie_softc *));
    157 static __inline int ether_equal __P((u_char *, u_char *));
    158 static __inline void ie_ack __P((struct ie_softc *, u_int));
    159 static __inline void ie_setup_config __P((volatile struct ie_config_cmd *,
    160 					  int, int));
    161 static __inline int check_eh __P((struct ie_softc *, struct ether_header *,
    162 				  int *));
    163 static __inline int ie_buflen __P((struct ie_softc *, int));
    164 static __inline int ie_packet_len __P((struct ie_softc *));
    165 static __inline void iexmit __P((struct ie_softc *));
    166 
    167 static void run_tdr __P((struct ie_softc *, struct ie_tdr_cmd *));
    168 static void iestop __P((struct ie_softc *));
    169 
    170 #ifdef IEDEBUG
    171 void print_rbd __P((volatile struct ie_recv_buf_desc *));
    172 
    173 int in_ierint = 0;
    174 int in_ietint = 0;
    175 #endif
    176 
    177 struct cfdriver ie_cd = {
    178 	NULL, "ie", DV_IFNET
    179 };
    180 
    181 /*
    182  * Address generation macros:
    183  *   MK_24 = KVA -> 24 bit address in native byte order
    184  *   MK_16 = KVA -> 16 bit address in INTEL byte order
    185  *   ST_24 = store a 24 bit address in native byte order to INTEL byte order
    186  */
    187 #define MK_24(base, ptr) ((caddr_t)((u_long)ptr - (u_long)base))
    188 
    189 #if BYTE_ORDER == BIG_ENDIAN
    190 #define XSWAP(y)	( ((y) >> 8) | ((y) << 8) )
    191 #define SWAP(x)		({u_short _z=(x); (u_short)XSWAP(_z);})
    192 
    193 #define MK_16(base, ptr) SWAP((u_short)( ((u_long)(ptr)) - ((u_long)(base)) ))
    194 #define ST_24(to, from) { \
    195 	u_long fval = (u_long)(from); \
    196 	u_char *t = (u_char *)&(to), *f = (u_char *)&fval; \
    197 	t[0] = f[3]; t[1] = f[2]; t[2] = f[1]; /*t[3] = f[0] ;*/ \
    198 }
    199 #else
    200 #define SWAP(x) x
    201 #define MK_16(base, ptr) ((u_short)(u_long)MK_24(base, ptr))
    202 #define ST_24(to, from) {to = (from);}
    203 #endif
    204 
    205 /*
    206  * Here are a few useful functions.  We could have done these as macros, but
    207  * since we have the inline facility, it makes sense to use that instead.
    208  */
    209 static __inline void
    210 ie_setup_config(cmd, promiscuous, manchester)
    211 	volatile struct ie_config_cmd *cmd;
    212 	int promiscuous, manchester;
    213 {
    214 
    215 	cmd->ie_config_count = 0x0c;
    216 	cmd->ie_fifo = 8;
    217 	cmd->ie_save_bad = 0x40;
    218 	cmd->ie_addr_len = 0x2e;
    219 	cmd->ie_priority = 0;
    220 	cmd->ie_ifs = 0x60;
    221 	cmd->ie_slot_low = 0;
    222 	cmd->ie_slot_high = 0xf2;
    223 	cmd->ie_promisc = !!promiscuous | manchester << 2;
    224 	cmd->ie_crs_cdt = 0;
    225 	cmd->ie_min_len = 64;
    226 	cmd->ie_junk = 0xff;
    227 }
    228 
    229 static __inline void
    230 ie_ack(sc, mask)
    231 	struct ie_softc *sc;
    232 	u_int mask;	/* in native byte-order */
    233 {
    234 	volatile struct ie_sys_ctl_block *scb = sc->scb;
    235 
    236 	command_and_wait(sc, SWAP(scb->ie_status) & mask, 0, 0);
    237 }
    238 
    239 
    240 /*
    241  * Taken almost exactly from Bill's if_is.c, then modified beyond recognition.
    242  */
    243 void
    244 ie_attach(sc, name, etheraddr)
    245 	struct ie_softc *sc;
    246 	char *name;
    247 	u_int8_t *etheraddr;
    248 {
    249 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    250 
    251 	if (ie_setupram(sc) == 0) { /* XXX - ISA version? */
    252 		printf(": RAM CONFIG FAILED!\n");
    253 		/* XXX should reclaim resources? */
    254 		return;
    255 	}
    256 
    257 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    258 	ifp->if_softc = sc;
    259 	ifp->if_start = iestart;
    260 	ifp->if_ioctl = ieioctl;
    261 	ifp->if_watchdog = iewatchdog;
    262 	ifp->if_flags =
    263 		IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    264 
    265 	/* Attach the interface. */
    266 	if_attach(ifp);
    267 	ether_ifattach(ifp, etheraddr);
    268 
    269 	printf(" address %s, type %s\n", ether_sprintf(etheraddr), name);
    270 
    271 #if NBPFILTER > 0
    272 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    273 #endif
    274 }
    275 
    276 
    277 /*
    278  * Device timeout/watchdog routine.  Entered if the device neglects to generate
    279  * an interrupt after a transmit has been started on it.
    280  */
    281 void
    282 iewatchdog(ifp)
    283 	struct ifnet *ifp;
    284 {
    285 	struct ie_softc *sc = ifp->if_softc;
    286 
    287 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    288 	++ifp->if_oerrors;
    289 
    290 	iereset(sc);
    291 }
    292 
    293 /*
    294  * What to do upon receipt of an interrupt.
    295  */
    296 int
    297 ieintr(v)
    298 void *v;
    299 {
    300 	struct ie_softc *sc = v;
    301 	register u_short status;
    302 
    303 	status = SWAP(sc->scb->ie_status);
    304 
    305         /*
    306          * Implementation dependent interrupt handling.
    307          */
    308 	if (sc->intrhook)
    309 		(*sc->intrhook)(sc);
    310 
    311 loop:
    312 	/* Ack interrupts FIRST in case we receive more during the ISR. */
    313 	ie_ack(sc, IE_ST_WHENCE & status);
    314 
    315 	if (status & (IE_ST_FR | IE_ST_RNR)) {
    316 #ifdef IEDEBUG
    317 		in_ierint++;
    318 		if (sc->sc_debug & IED_RINT)
    319 			printf("%s: rint\n", sc->sc_dev.dv_xname);
    320 #endif
    321 		ierint(sc);
    322 #ifdef IEDEBUG
    323 		in_ierint--;
    324 #endif
    325 	}
    326 
    327 	if (status & IE_ST_CX) {
    328 #ifdef IEDEBUG
    329 		in_ietint++;
    330 		if (sc->sc_debug & IED_TINT)
    331 			printf("%s: tint\n", sc->sc_dev.dv_xname);
    332 #endif
    333 		ietint(sc);
    334 #ifdef IEDEBUG
    335 		in_ietint--;
    336 #endif
    337 	}
    338 
    339 	if (status & IE_ST_RNR) {
    340 		printf("%s: receiver not ready\n", sc->sc_dev.dv_xname);
    341 		sc->sc_ethercom.ec_if.if_ierrors++;
    342 		iereset(sc);
    343 		return (1);
    344 	}
    345 
    346 #ifdef IEDEBUG
    347 	if ((status & IE_ST_CNA) && (sc->sc_debug & IED_CNA))
    348 		printf("%s: cna\n", sc->sc_dev.dv_xname);
    349 #endif
    350 
    351 	status = SWAP(sc->scb->ie_status);
    352 	if (status & IE_ST_WHENCE)
    353 		goto loop;
    354 
    355 	return (1);
    356 }
    357 
    358 /*
    359  * Process a received-frame interrupt.
    360  */
    361 void
    362 ierint(sc)
    363 	struct ie_softc *sc;
    364 {
    365 	volatile struct ie_sys_ctl_block *scb = sc->scb;
    366 	int i, status;
    367 	static int timesthru = 1024;
    368 
    369 	i = sc->rfhead;
    370 	for (;;) {
    371 		status = SWAP(sc->rframes[i]->ie_fd_status);
    372 
    373 		if ((status & IE_FD_COMPLETE) && (status & IE_FD_OK)) {
    374 			if (--timesthru == 0) {
    375 				sc->sc_ethercom.ec_if.if_ierrors +=
    376 				    SWAP(scb->ie_err_crc) +
    377 				    SWAP(scb->ie_err_align) +
    378 				    SWAP(scb->ie_err_resource) +
    379 				    SWAP(scb->ie_err_overrun);
    380 				scb->ie_err_crc = scb->ie_err_align =
    381 				scb->ie_err_resource = scb->ie_err_overrun =
    382 				    SWAP(0);
    383 				timesthru = 1024;
    384 			}
    385 			ie_readframe(sc, i);
    386 		} else {
    387 			if ((status & IE_FD_RNR) != 0 &&
    388 			    (SWAP(scb->ie_status) & IE_RU_READY) == 0) {
    389 				sc->rframes[0]->ie_fd_buf_desc =
    390 					MK_16(sc->sc_maddr, sc->rbuffs[0]);
    391 				scb->ie_recv_list =
    392 					MK_16(sc->sc_maddr, sc->rframes[0]);
    393 				command_and_wait(sc, IE_RU_START, 0, 0);
    394 			}
    395 			break;
    396 		}
    397 		i = (i + 1) % sc->nframes;
    398 	}
    399 }
    400 
    401 /*
    402  * Process a command-complete interrupt.  These are only generated by the
    403  * transmission of frames.  This routine is deceptively simple, since most of
    404  * the real work is done by iestart().
    405  */
    406 void
    407 ietint(sc)
    408 	struct ie_softc *sc;
    409 {
    410 	int status;
    411 
    412 	sc->sc_ethercom.ec_if.if_timer = 0;
    413 	sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
    414 
    415 	status = SWAP(sc->xmit_cmds[sc->xctail]->ie_xmit_status);
    416 
    417 	if ((status & IE_STAT_COMPL) == 0 || (status & IE_STAT_BUSY))
    418 		printf("ietint: command still busy!\n");
    419 
    420 	if (status & IE_STAT_OK) {
    421 		sc->sc_ethercom.ec_if.if_opackets++;
    422 		sc->sc_ethercom.ec_if.if_collisions += (status & IE_XS_MAXCOLL);
    423 	} else if (status & IE_STAT_ABORT) {
    424 		printf("%s: send aborted\n", sc->sc_dev.dv_xname);
    425 		sc->sc_ethercom.ec_if.if_oerrors++;
    426 	} else if (status & IE_XS_NOCARRIER) {
    427 		printf("%s: no carrier\n", sc->sc_dev.dv_xname);
    428 		sc->sc_ethercom.ec_if.if_oerrors++;
    429 	} else if (status & IE_XS_LOSTCTS) {
    430 		printf("%s: lost CTS\n", sc->sc_dev.dv_xname);
    431 		sc->sc_ethercom.ec_if.if_oerrors++;
    432 	} else if (status & IE_XS_UNDERRUN) {
    433 		printf("%s: DMA underrun\n", sc->sc_dev.dv_xname);
    434 		sc->sc_ethercom.ec_if.if_oerrors++;
    435 	} else if (status & IE_XS_EXCMAX) {
    436 		printf("%s: too many collisions\n", sc->sc_dev.dv_xname);
    437 		sc->sc_ethercom.ec_if.if_collisions += 16;
    438 		sc->sc_ethercom.ec_if.if_oerrors++;
    439 	}
    440 
    441 	/*
    442 	 * If multicast addresses were added or deleted while transmitting,
    443 	 * mc_reset() set the want_mcsetup flag indicating that we should do
    444 	 * it.
    445 	 */
    446 	if (sc->want_mcsetup) {
    447 		mc_setup(sc, (caddr_t)sc->xmit_cbuffs[sc->xctail]);
    448 		sc->want_mcsetup = 0;
    449 	}
    450 
    451 	/* Done with the buffer. */
    452 	sc->xmit_busy--;
    453 	sc->xctail = (sc->xctail + 1) % NTXBUF;
    454 
    455 	/* Start the next packet, if any, transmitting. */
    456 	if (sc->xmit_busy > 0)
    457 		iexmit(sc);
    458 
    459 	iestart(&sc->sc_ethercom.ec_if);
    460 }
    461 
    462 /*
    463  * Compare two Ether/802 addresses for equality, inlined and unrolled for
    464  * speed.
    465  */
    466 static __inline int
    467 ether_equal(one, two)
    468 	u_char *one, *two;
    469 {
    470 
    471 	if (one[5] != two[5] || one[4] != two[4] || one[3] != two[3] ||
    472 	    one[2] != two[2] || one[1] != two[1] || one[0] != two[0])
    473 		return 0;
    474 	return 1;
    475 }
    476 
    477 /*
    478  * Check for a valid address.  to_bpf is filled in with one of the following:
    479  *   0 -> BPF doesn't get this packet
    480  *   1 -> BPF does get this packet
    481  *   2 -> BPF does get this packet, but we don't
    482  * Return value is true if the packet is for us, and false otherwise.
    483  *
    484  * This routine is a mess, but it's also critical that it be as fast
    485  * as possible.  It could be made cleaner if we can assume that the
    486  * only client which will fiddle with IFF_PROMISC is BPF.  This is
    487  * probably a good assumption, but we do not make it here.  (Yet.)
    488  */
    489 static __inline int
    490 check_eh(sc, eh, to_bpf)
    491 	struct ie_softc *sc;
    492 	struct ether_header *eh;
    493 	int *to_bpf;
    494 {
    495 	struct ifnet *ifp;
    496 	int i;
    497 
    498 	ifp = &sc->sc_ethercom.ec_if;
    499 
    500 	switch(sc->promisc) {
    501 	case IFF_ALLMULTI:
    502 		/*
    503 		 * Receiving all multicasts, but no unicasts except those
    504 		 * destined for us.
    505 		 */
    506 #if NBPFILTER > 0
    507 		/* BPF gets this packet if anybody cares */
    508 		*to_bpf = (ifp->if_bpf != 0);
    509 #endif
    510 		if (eh->ether_dhost[0] & 1)
    511 			return 1;
    512 		if (ether_equal(eh->ether_dhost, LLADDR(ifp->if_sadl)))
    513 			return 1;
    514 		return 0;
    515 
    516 	case IFF_PROMISC:
    517 		/*
    518 		 * Receiving all packets.  These need to be passed on to BPF.
    519 		 */
    520 #if NBPFILTER > 0
    521 		*to_bpf = (ifp->if_bpf != 0);
    522 #endif
    523 		/* If for us, accept and hand up to BPF */
    524 		if (ether_equal(eh->ether_dhost, LLADDR(ifp->if_sadl)))
    525 			return 1;
    526 
    527 #if NBPFILTER > 0
    528 		if (*to_bpf)
    529 			*to_bpf = 2; /* we don't need to see it */
    530 #endif
    531 
    532 		/*
    533 		 * Not a multicast, so BPF wants to see it but we don't.
    534 		 */
    535 		if ((eh->ether_dhost[0] & 1) == 0)
    536 			return 1;
    537 
    538 		/*
    539 		 * If it's one of our multicast groups, accept it and pass it
    540 		 * up.
    541 		 */
    542 		for (i = 0; i < sc->mcast_count; i++) {
    543 			if (ether_equal(eh->ether_dhost,
    544 					(u_char *)&sc->mcast_addrs[i])) {
    545 #if NBPFILTER > 0
    546 				if (*to_bpf)
    547 					*to_bpf = 1;
    548 #endif
    549 				return 1;
    550 			}
    551 		}
    552 		return 1;
    553 
    554 	case IFF_ALLMULTI | IFF_PROMISC:
    555 		/*
    556 		 * Acting as a multicast router, and BPF running at the same
    557 		 * time.  Whew!  (Hope this is a fast machine...)
    558 		 */
    559 #if NBPFILTER > 0
    560 		*to_bpf = (ifp->if_bpf != 0);
    561 #endif
    562 		/* We want to see multicasts. */
    563 		if (eh->ether_dhost[0] & 1)
    564 			return 1;
    565 
    566 		/* We want to see our own packets */
    567 		if (ether_equal(eh->ether_dhost, LLADDR(ifp->if_sadl)))
    568 			return 1;
    569 
    570 		/* Anything else goes to BPF but nothing else. */
    571 #if NBPFILTER > 0
    572 		if (*to_bpf)
    573 			*to_bpf = 2;
    574 #endif
    575 		return 1;
    576 
    577 	default:
    578 		/*
    579 		 * Only accept unicast packets destined for us, or multicasts
    580 		 * for groups that we belong to.  For now, we assume that the
    581 		 * '586 will only return packets that we asked it for.  This
    582 		 * isn't strictly true (it uses hashing for the multicast
    583 		 * filter), but it will do in this case, and we want to get
    584 		 * out of here as quickly as possible.
    585 		 */
    586 #if NBPFILTER > 0
    587 		*to_bpf = (ifp->if_bpf != 0);
    588 #endif
    589 		return 1;
    590 	}
    591 	return 0;
    592 }
    593 
    594 /*
    595  * We want to isolate the bits that have meaning...  This assumes that
    596  * IE_RBUF_SIZE is an even power of two.  If somehow the act_len exceeds
    597  * the size of the buffer, then we are screwed anyway.
    598  */
    599 static __inline int
    600 ie_buflen(sc, head)
    601 	struct ie_softc *sc;
    602 	int head;
    603 {
    604 
    605 	return (SWAP(sc->rbuffs[head]->ie_rbd_actual)
    606 		& (IE_RBUF_SIZE | (IE_RBUF_SIZE - 1)));
    607 }
    608 
    609 
    610 static __inline int
    611 ie_packet_len(sc)
    612 	struct ie_softc *sc;
    613 {
    614 	int i;
    615 	int head = sc->rbhead;
    616 	int acc = 0;
    617 	int oldhead = head;
    618 
    619 	do {
    620 		i = SWAP(sc->rbuffs[head]->ie_rbd_actual);
    621 		if ((i & IE_RBD_USED) == 0) {
    622 #ifdef IEDEBUG
    623 			print_rbd(sc->rbuffs[head]);
    624 #endif
    625 			log(LOG_ERR, "%s: receive descriptors out of sync at %d\n",
    626 			    sc->sc_dev.dv_xname, sc->rbhead);
    627 			iereset(sc);
    628 			return -1;
    629 		}
    630 
    631 		i = (i & IE_RBD_LAST) != 0;
    632 
    633 		acc += ie_buflen(sc, head);
    634 		head = (head + 1) % sc->nrxbuf;
    635 		if (oldhead == head) {
    636 			printf("ie: packet len: looping: acc = %d (head=%d)\n",
    637 				acc, head);
    638 			iereset(sc);
    639 			return -1;
    640 		}
    641 	} while (!i);
    642 
    643 	return acc;
    644 }
    645 
    646 /*
    647  * Setup all necessary artifacts for an XMIT command, and then pass the XMIT
    648  * command to the chip to be executed.  On the way, if we have a BPF listener
    649  * also give him a copy.
    650  */
    651 static __inline void
    652 iexmit(sc)
    653 	struct ie_softc *sc;
    654 {
    655 
    656 #ifdef IEDEBUG
    657 	if (sc->sc_debug & IED_XMIT)
    658 		printf("%s: xmit buffer %d\n", sc->sc_dev.dv_xname,
    659 			sc->xctail);
    660 #endif
    661 
    662 	sc->xmit_buffs[sc->xctail]->ie_xmit_flags |= SWAP(IE_XMIT_LAST);
    663 	sc->xmit_buffs[sc->xctail]->ie_xmit_next = SWAP(0xffff);
    664 	ST_24(sc->xmit_buffs[sc->xctail]->ie_xmit_buf,
    665 	      MK_24(sc->sc_iobase, sc->xmit_cbuffs[sc->xctail]));
    666 
    667 	sc->xmit_cmds[sc->xctail]->com.ie_cmd_link = SWAP(0xffff);
    668 	sc->xmit_cmds[sc->xctail]->com.ie_cmd_cmd =
    669 		SWAP(IE_CMD_XMIT | IE_CMD_INTR | IE_CMD_LAST);
    670 
    671 	sc->xmit_cmds[sc->xctail]->ie_xmit_status = SWAP(0);
    672 	sc->xmit_cmds[sc->xctail]->ie_xmit_desc =
    673 		MK_16(sc->sc_maddr, sc->xmit_buffs[sc->xctail]);
    674 
    675 	sc->scb->ie_command_list =
    676 		MK_16(sc->sc_maddr, sc->xmit_cmds[sc->xctail]);
    677 
    678 	command_and_wait(sc, IE_CU_START, 0, 0);
    679 
    680 	sc->sc_ethercom.ec_if.if_timer = 5;
    681 }
    682 
    683 /*
    684  * Read data off the interface, and turn it into an mbuf chain.
    685  *
    686  * This code is DRAMATICALLY different from the previous version; this
    687  * version tries to allocate the entire mbuf chain up front, given the
    688  * length of the data available.  This enables us to allocate mbuf
    689  * clusters in many situations where before we would have had a long
    690  * chain of partially-full mbufs.  This should help to speed up the
    691  * operation considerably.  (Provided that it works, of course.)
    692  */
    693 struct mbuf *
    694 ieget(sc, ehp, to_bpf)
    695 	struct ie_softc *sc;
    696 	struct ether_header *ehp;
    697 	int *to_bpf;
    698 {
    699 	struct mbuf *top, **mp, *m;
    700 	int len, totlen, resid;
    701 	int thisrboff, thismboff;
    702 	int head;
    703 
    704 	totlen = ie_packet_len(sc);
    705 	if (totlen <= 0)
    706 		return 0;
    707 
    708 	head = sc->rbhead;
    709 
    710 	/*
    711 	 * Snarf the Ethernet header.
    712 	 */
    713 	bcopy((caddr_t)sc->cbuffs[head], (caddr_t)ehp, sizeof *ehp);
    714 
    715 	/*
    716 	 * As quickly as possible, check if this packet is for us.
    717 	 * If not, don't waste a single cycle copying the rest of the
    718 	 * packet in.
    719 	 * This is only a consideration when FILTER is defined; i.e., when
    720 	 * we are either running BPF or doing multicasting.
    721 	 */
    722 	if (!check_eh(sc, ehp, to_bpf)) {
    723 		/* just this case, it's not an error */
    724 		sc->sc_ethercom.ec_if.if_ierrors--;
    725 		return 0;
    726 	}
    727 
    728 	resid = totlen -= (thisrboff = sizeof *ehp);
    729 
    730 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    731 	if (m == 0)
    732 		return 0;
    733 	m->m_pkthdr.rcvif = &sc->sc_ethercom.ec_if;
    734 	m->m_pkthdr.len = totlen;
    735 	len = MHLEN;
    736 	top = 0;
    737 	mp = &top;
    738 
    739 	/*
    740 	 * This loop goes through and allocates mbufs for all the data we will
    741 	 * be copying in.  It does not actually do the copying yet.
    742 	 */
    743 	while (totlen > 0) {
    744 		if (top) {
    745 			MGET(m, M_DONTWAIT, MT_DATA);
    746 			if (m == 0) {
    747 				m_freem(top);
    748 				return 0;
    749 			}
    750 			len = MLEN;
    751 		}
    752 		if (totlen >= MINCLSIZE) {
    753 			MCLGET(m, M_DONTWAIT);
    754 			if ((m->m_flags & M_EXT) == 0) {
    755 				m_freem(top);
    756 				return 0;
    757 			}
    758 			len = MCLBYTES;
    759 		}
    760 		m->m_len = len = min(totlen, len);
    761 		totlen -= len;
    762 		*mp = m;
    763 		mp = &m->m_next;
    764 	}
    765 
    766 	m = top;
    767 	thismboff = 0;
    768 
    769 	/*
    770 	 * Now we take the mbuf chain (hopefully only one mbuf most of the
    771 	 * time) and stuff the data into it.  There are no possible failures at
    772 	 * or after this point.
    773 	 */
    774 	while (resid > 0) {
    775 		int thisrblen = ie_buflen(sc, head) - thisrboff,
    776 		    thismblen = m->m_len - thismboff;
    777 		len = min(thisrblen, thismblen);
    778 
    779 		bcopy((caddr_t)(sc->cbuffs[head] + thisrboff),
    780 		    mtod(m, caddr_t) + thismboff, (u_int)len);
    781 		resid -= len;
    782 
    783 		if (len == thismblen) {
    784 			m = m->m_next;
    785 			thismboff = 0;
    786 		} else
    787 			thismboff += len;
    788 
    789 		if (len == thisrblen) {
    790 			head = (head + 1) % sc->nrxbuf;
    791 			thisrboff = 0;
    792 		} else
    793 			thisrboff += len;
    794 	}
    795 
    796 	/*
    797 	 * Unless something changed strangely while we were doing the copy, we
    798 	 * have now copied everything in from the shared memory.
    799 	 * This means that we are done.
    800 	 */
    801 	return top;
    802 }
    803 
    804 /*
    805  * Read frame NUM from unit UNIT (pre-cached as IE).
    806  *
    807  * This routine reads the RFD at NUM, and copies in the buffers from the list
    808  * of RBD, then rotates the RBD and RFD lists so that the receiver doesn't
    809  * start complaining.  Trailers are DROPPED---there's no point in wasting time
    810  * on confusing code to deal with them.  Hopefully, this machine will never ARP
    811  * for trailers anyway.
    812  */
    813 static void
    814 ie_readframe(sc, num)
    815 	struct ie_softc *sc;
    816 	int num;			/* frame number to read */
    817 {
    818 	int status;
    819 	struct mbuf *m = 0;
    820 	struct ether_header eh;
    821 #if NBPFILTER > 0
    822 	int bpf_gets_it = 0;
    823 #endif
    824 
    825 	status = SWAP(sc->rframes[num]->ie_fd_status);
    826 
    827 	/* Immediately advance the RFD list, since we have copied ours now. */
    828 	sc->rframes[num]->ie_fd_status = SWAP(0);
    829 	sc->rframes[num]->ie_fd_last |= SWAP(IE_FD_LAST);
    830 	sc->rframes[sc->rftail]->ie_fd_last &= ~SWAP(IE_FD_LAST);
    831 	sc->rftail = (sc->rftail + 1) % sc->nframes;
    832 	sc->rfhead = (sc->rfhead + 1) % sc->nframes;
    833 
    834 	if (status & IE_FD_OK) {
    835 #if NBPFILTER > 0
    836 		m = ieget(sc, &eh, &bpf_gets_it);
    837 #else
    838 		m = ieget(sc, &eh, 0);
    839 #endif
    840 		ie_drop_packet_buffer(sc);
    841 	}
    842 	if (m == 0) {
    843 		sc->sc_ethercom.ec_if.if_ierrors++;
    844 		return;
    845 	}
    846 
    847 #ifdef IEDEBUG
    848 	if (sc->sc_debug & IED_READFRAME)
    849 		printf("%s: frame from ether %s type %x\n", sc->sc_dev.dv_xname,
    850 		    ether_sprintf(eh.ether_shost), (u_int)eh.ether_type);
    851 #endif
    852 
    853 #if NBPFILTER > 0
    854 	/*
    855 	 * Check for a BPF filter; if so, hand it up.
    856 	 * Note that we have to stick an extra mbuf up front, because bpf_mtap
    857 	 * expects to have the ether header at the front.
    858 	 * It doesn't matter that this results in an ill-formatted mbuf chain,
    859 	 * since BPF just looks at the data.  (It doesn't try to free the mbuf,
    860 	 * tho' it will make a copy for tcpdump.)
    861 	 */
    862 	if (bpf_gets_it) {
    863 		struct mbuf m0;
    864 		m0.m_len = sizeof eh;
    865 		m0.m_data = (caddr_t)&eh;
    866 		m0.m_next = m;
    867 
    868 		/* Pass it up. */
    869 		bpf_mtap(sc->sc_ethercom.ec_if.if_bpf, &m0);
    870 
    871 		/*
    872 		 * A signal passed up from the filtering code indicating that
    873 		 * the packet is intended for BPF but not for the protocol
    874 		 * machinery.  We can save a few cycles by not handing it off
    875 		 * to them.
    876 		 */
    877 		if (bpf_gets_it == 2) {
    878 			m_freem(m);
    879 			return;
    880 		}
    881 	}
    882 #endif /* NBPFILTER > 0 */
    883 
    884 	/*
    885 	 * In here there used to be code to check destination addresses upon
    886 	 * receipt of a packet.  We have deleted that code, and replaced it
    887 	 * with code to check the address much earlier in the cycle, before
    888 	 * copying the data in; this saves us valuable cycles when operating
    889 	 * as a multicast router or when using BPF.
    890 	 */
    891 
    892 	/*
    893 	 * Finally pass this packet up to higher layers.
    894 	 */
    895 	ether_input(&sc->sc_ethercom.ec_if, &eh, m);
    896 	sc->sc_ethercom.ec_if.if_ipackets++;
    897 }
    898 
    899 static void
    900 ie_drop_packet_buffer(sc)
    901 	struct ie_softc *sc;
    902 {
    903 	int i;
    904 
    905 	do {
    906 		i = SWAP(sc->rbuffs[sc->rbhead]->ie_rbd_actual);
    907 		if ((i & IE_RBD_USED) == 0) {
    908 			/*
    909 			 * This means we are somehow out of sync.  So, we
    910 			 * reset the adapter.
    911 			 */
    912 #ifdef IEDEBUG
    913 			print_rbd(sc->rbuffs[sc->rbhead]);
    914 #endif
    915 			log(LOG_ERR, "%s: receive descriptors out of sync at %d\n",
    916 			    sc->sc_dev.dv_xname, sc->rbhead);
    917 			iereset(sc);
    918 			return;
    919 		}
    920 
    921 		i = (i & IE_RBD_LAST) != 0;
    922 
    923 		sc->rbuffs[sc->rbhead]->ie_rbd_length |= SWAP(IE_RBD_LAST);
    924 		sc->rbuffs[sc->rbhead]->ie_rbd_actual = SWAP(0);
    925 		sc->rbhead = (sc->rbhead + 1) % sc->nrxbuf;
    926 		sc->rbuffs[sc->rbtail]->ie_rbd_length &= ~SWAP(IE_RBD_LAST);
    927 		sc->rbtail = (sc->rbtail + 1) % sc->nrxbuf;
    928 	} while (!i);
    929 }
    930 
    931 
    932 /*
    933  * Start transmission on an interface.
    934  */
    935 void
    936 iestart(ifp)
    937 	struct ifnet *ifp;
    938 {
    939 	struct ie_softc *sc = ifp->if_softc;
    940 	struct mbuf *m0, *m;
    941 	u_char *buffer;
    942 	u_short len;
    943 
    944 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    945 		return;
    946 
    947 	for (;;) {
    948 		if (sc->xmit_busy == NTXBUF) {
    949 			ifp->if_flags |= IFF_OACTIVE;
    950 			break;
    951 		}
    952 
    953 		IF_DEQUEUE(&ifp->if_snd, m0);
    954 		if (m0 == 0)
    955 			break;
    956 
    957 		/* We need to use m->m_pkthdr.len, so require the header */
    958 		if ((m0->m_flags & M_PKTHDR) == 0)
    959 			panic("iestart: no header mbuf");
    960 
    961 #if NBPFILTER > 0
    962 		/* Tap off here if there is a BPF listener. */
    963 		if (ifp->if_bpf)
    964 			bpf_mtap(ifp->if_bpf, m0);
    965 #endif
    966 
    967 #ifdef IEDEBUG
    968 		if (sc->sc_debug & IED_ENQ)
    969 			printf("%s: fill buffer %d\n", sc->sc_dev.dv_xname,
    970 				sc->xchead);
    971 #endif
    972 
    973 		if (m0->m_pkthdr.len > IE_TBUF_SIZE)
    974 			printf("%s: tbuf overflow\n", sc->sc_dev.dv_xname);
    975 
    976 		buffer = sc->xmit_cbuffs[sc->xchead];
    977 		for (m = m0; m != 0; m = m->m_next) {
    978 			bcopy(mtod(m, caddr_t), buffer, m->m_len);
    979 			buffer += m->m_len;
    980 		}
    981 
    982 		len = max(m0->m_pkthdr.len, ETHER_MIN_LEN);
    983 		m_freem(m0);
    984 
    985 		sc->xmit_buffs[sc->xchead]->ie_xmit_flags = SWAP(len);
    986 
    987 		/* Start the first packet transmitting. */
    988 		if (sc->xmit_busy == 0)
    989 			iexmit(sc);
    990 
    991 		sc->xchead = (sc->xchead + 1) % NTXBUF;
    992 		sc->xmit_busy++;
    993 	}
    994 }
    995 
    996 /*
    997  * set up IE's ram space
    998  */
    999 int
   1000 ie_setupram(sc)
   1001 	struct ie_softc *sc;
   1002 {
   1003 	volatile struct ie_sys_conf_ptr *scp;
   1004 	volatile struct ie_int_sys_conf_ptr *iscp;
   1005 	volatile struct ie_sys_ctl_block *scb;
   1006 	int     s;
   1007 
   1008 	s = splnet();
   1009 
   1010 	scp = sc->scp;
   1011 	(sc->memzero)((char *) scp, sizeof *scp);
   1012 
   1013 	iscp = sc->iscp;
   1014 	(sc->memzero)((char *) iscp, sizeof *iscp);
   1015 
   1016 	scb = sc->scb;
   1017 	(sc->memzero)((char *) scb, sizeof *scb);
   1018 
   1019 	scp->ie_bus_use = 0;	/* 16-bit */
   1020 	ST_24(scp->ie_iscp_ptr, MK_24(sc->sc_iobase, iscp));
   1021 
   1022 	iscp->ie_busy = 1;	/* ie_busy == char */
   1023 	iscp->ie_scb_offset = MK_16(sc->sc_maddr, scb);
   1024 	ST_24(iscp->ie_base, MK_24(sc->sc_iobase, sc->sc_maddr));
   1025 
   1026 	if (sc->hwreset)
   1027 		(sc->hwreset)(sc);
   1028 
   1029 	(sc->chan_attn) (sc);
   1030 
   1031 	delay(100);		/* wait a while... */
   1032 
   1033 	if (iscp->ie_busy) {
   1034 		splx(s);
   1035 		return 0;
   1036 	}
   1037 	/*
   1038 	 * Acknowledge any interrupts we may have caused...
   1039 	 */
   1040 	ie_ack(sc, IE_ST_WHENCE);
   1041 	splx(s);
   1042 
   1043 	return 1;
   1044 }
   1045 
   1046 void
   1047 iereset(sc)
   1048 	struct ie_softc *sc;
   1049 {
   1050 	int s = splnet();
   1051 
   1052 	printf("%s: reset\n", sc->sc_dev.dv_xname);
   1053 
   1054 	/* Clear OACTIVE in case we're called from watchdog (frozen xmit). */
   1055 	sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
   1056 
   1057 	/*
   1058 	 * Stop i82586 dead in its tracks.
   1059 	 */
   1060 	if (command_and_wait(sc, IE_RU_ABORT | IE_CU_ABORT, 0, 0))
   1061 		printf("%s: abort commands timed out\n", sc->sc_dev.dv_xname);
   1062 
   1063 	if (command_and_wait(sc, IE_RU_DISABLE | IE_CU_STOP, 0, 0))
   1064 		printf("%s: disable commands timed out\n", sc->sc_dev.dv_xname);
   1065 
   1066 
   1067 #if notdef
   1068 	if (sc->hwreset)
   1069 		(sc->hwreset)(sc);
   1070 #endif
   1071 #ifdef notdef
   1072 	if (!check_ie_present(sc, sc->sc_maddr, sc->sc_msize))
   1073 		panic("ie disappeared!\n");
   1074 #endif
   1075 
   1076 	ieinit(sc);
   1077 
   1078 	splx(s);
   1079 }
   1080 
   1081 /*
   1082  * Send a command to the controller and wait for it to either complete
   1083  * or be accepted, depending on the command.  If the command pointer
   1084  * is null, then pretend that the command is not an action command.
   1085  * If the command pointer is not null, and the command is an action
   1086  * command, wait for
   1087  * ((volatile struct ie_cmd_common *)pcmd)->ie_cmd_status & MASK
   1088  * to become true.
   1089  */
   1090 static int
   1091 command_and_wait(sc, cmd, pcmd, mask)
   1092 	struct ie_softc *sc;
   1093 	int cmd;	/* native byte-order */
   1094 	volatile void *pcmd;
   1095 	int mask;	/* native byte-order */
   1096 {
   1097 	volatile struct ie_cmd_common *cc = pcmd;
   1098 	volatile struct ie_sys_ctl_block *scb = sc->scb;
   1099 	int i;
   1100 
   1101 	scb->ie_command = (u_short)SWAP(cmd);
   1102 	(sc->chan_attn)(sc);
   1103 
   1104 	if (IE_ACTION_COMMAND(cmd) && pcmd) {
   1105 		/*
   1106 		 * According to the packet driver, the minimum timeout should
   1107 		 * be .369 seconds, which we round up to .4.
   1108 		 */
   1109 
   1110 		/*
   1111 		 * Now spin-lock waiting for status.  This is not a very nice
   1112 		 * thing to do, but I haven't figured out how, or indeed if, we
   1113 		 * can put the process waiting for action to sleep.  (We may
   1114 		 * be getting called through some other timeout running in the
   1115 		 * kernel.)
   1116 		 */
   1117 		for (i = 0; i < 369000; i++) {
   1118 			delay(1);
   1119 			if ((SWAP(cc->ie_cmd_status) & mask))
   1120 				return (0);
   1121 		}
   1122 
   1123 	} else {
   1124 		/*
   1125 		 * Otherwise, just wait for the command to be accepted.
   1126 		 */
   1127 
   1128 		/* XXX spin lock; wait at most 0.1 seconds */
   1129 		for (i = 0; i < 100000; i++) {
   1130 			if (scb->ie_command)
   1131 				return (0);
   1132 			delay(1);
   1133 		}
   1134 	}
   1135 
   1136 	/* Timeout */
   1137 	return (1);
   1138 }
   1139 
   1140 /*
   1141  * Run the time-domain reflectometer.
   1142  */
   1143 static void
   1144 run_tdr(sc, cmd)
   1145 	struct ie_softc *sc;
   1146 	struct ie_tdr_cmd *cmd;
   1147 {
   1148 	int result;
   1149 
   1150 	cmd->com.ie_cmd_status = SWAP(0);
   1151 	cmd->com.ie_cmd_cmd = SWAP(IE_CMD_TDR | IE_CMD_LAST);
   1152 	cmd->com.ie_cmd_link = SWAP(0xffff);
   1153 
   1154 	sc->scb->ie_command_list = MK_16(sc->sc_maddr, cmd);
   1155 	cmd->ie_tdr_time = SWAP(0);
   1156 
   1157 	if (command_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
   1158 	    (SWAP(cmd->com.ie_cmd_status) & IE_STAT_OK) == 0)
   1159 		result = 0x10000; /* XXX */
   1160 	else
   1161 		result = SWAP(cmd->ie_tdr_time);
   1162 
   1163 	ie_ack(sc, IE_ST_WHENCE);
   1164 
   1165 	if (result & IE_TDR_SUCCESS)
   1166 		return;
   1167 
   1168 	if (result & 0x10000)
   1169 		printf("%s: TDR command failed\n", sc->sc_dev.dv_xname);
   1170 	else if (result & IE_TDR_XCVR)
   1171 		printf("%s: transceiver problem\n", sc->sc_dev.dv_xname);
   1172 	else if (result & IE_TDR_OPEN)
   1173 		printf("%s: TDR detected an open %d clocks away\n",
   1174 		    sc->sc_dev.dv_xname, result & IE_TDR_TIME);
   1175 	else if (result & IE_TDR_SHORT)
   1176 		printf("%s: TDR detected a short %d clocks away\n",
   1177 		    sc->sc_dev.dv_xname, result & IE_TDR_TIME);
   1178 	else
   1179 		printf("%s: TDR returned unknown status %x\n",
   1180 		    sc->sc_dev.dv_xname, result);
   1181 }
   1182 
   1183 #ifdef notdef
   1184 /* ALIGN works on 8 byte boundaries.... but 4 byte boundaries are ok for sun */
   1185 #define	_ALLOC(p, n)	(bzero(p, n), p += n, p - n)
   1186 #define	ALLOC(p, n)	_ALLOC(p, ALIGN(n)) /* XXX convert to this? */
   1187 #endif
   1188 
   1189 /*
   1190  * setup_bufs: set up the buffers
   1191  *
   1192  * we have a block of KVA at sc->buf_area which is of size sc->buf_area_sz.
   1193  * this is to be used for the buffers.  the chip indexs its control data
   1194  * structures with 16 bit offsets, and it indexes actual buffers with
   1195  * 24 bit addresses.   so we should allocate control buffers first so that
   1196  * we don't overflow the 16 bit offset field.   The number of transmit
   1197  * buffers is fixed at compile time.
   1198  *
   1199  * note: this function was written to be easy to understand, rather than
   1200  *       highly efficient (it isn't in the critical path).
   1201  */
   1202 static void
   1203 setup_bufs(sc)
   1204 	struct ie_softc *sc;
   1205 {
   1206 	caddr_t ptr = sc->buf_area;	/* memory pool */
   1207 	int     n, r;
   1208 
   1209 	/*
   1210 	 * step 0: zero memory and figure out how many recv buffers and
   1211 	 * frames we can have.
   1212 	 */
   1213 	(sc->memzero)(ptr, sc->buf_area_sz);
   1214 	ptr = (sc->align)(ptr);	/* set alignment and stick with it */
   1215 
   1216 	n = (int)(sc->align)((caddr_t) sizeof(struct ie_xmit_cmd)) +
   1217 	    (int)(sc->align)((caddr_t) sizeof(struct ie_xmit_buf)) + IE_TBUF_SIZE;
   1218 	n *= NTXBUF;		/* n = total size of xmit area */
   1219 
   1220 	n = sc->buf_area_sz - n;/* n = free space for recv stuff */
   1221 
   1222 	r = (int)(sc->align)((caddr_t) sizeof(struct ie_recv_frame_desc)) +
   1223 	    (((int)(sc->align)((caddr_t) sizeof(struct ie_recv_buf_desc)) +
   1224 		IE_RBUF_SIZE) * B_PER_F);
   1225 
   1226 	/* r = size of one R frame */
   1227 
   1228 	sc->nframes = n / r;
   1229 	if (sc->nframes <= 0)
   1230 		panic("ie: bogus buffer calc\n");
   1231 	if (sc->nframes > MAXFRAMES)
   1232 		sc->nframes = MAXFRAMES;
   1233 
   1234 	sc->nrxbuf = sc->nframes * B_PER_F;
   1235 
   1236 #ifdef IEDEBUG
   1237 	printf("IEDEBUG: %d frames %d bufs\n", sc->nframes, sc->nrxbuf);
   1238 #endif
   1239 
   1240 	/*
   1241 	 *  step 1a: lay out and zero frame data structures for transmit and recv
   1242 	 */
   1243 	for (n = 0; n < NTXBUF; n++) {
   1244 		sc->xmit_cmds[n] = (volatile struct ie_xmit_cmd *) ptr;
   1245 		ptr = (sc->align)(ptr + sizeof(struct ie_xmit_cmd));
   1246 	}
   1247 
   1248 	for (n = 0; n < sc->nframes; n++) {
   1249 		sc->rframes[n] = (volatile struct ie_recv_frame_desc *) ptr;
   1250 		ptr = (sc->align)(ptr + sizeof(struct ie_recv_frame_desc));
   1251 	}
   1252 
   1253 	/*
   1254 	 * step 1b: link together the recv frames and set EOL on last one
   1255 	 */
   1256 	for (n = 0; n < sc->nframes; n++) {
   1257 		sc->rframes[n]->ie_fd_next =
   1258 		    MK_16(sc->sc_maddr, sc->rframes[(n + 1) % sc->nframes]);
   1259 	}
   1260 	sc->rframes[sc->nframes - 1]->ie_fd_last |= SWAP(IE_FD_LAST);
   1261 
   1262 	/*
   1263 	 * step 2a: lay out and zero frame buffer structures for xmit and recv
   1264 	 */
   1265 	for (n = 0; n < NTXBUF; n++) {
   1266 		sc->xmit_buffs[n] = (volatile struct ie_xmit_buf *) ptr;
   1267 		ptr = (sc->align)(ptr + sizeof(struct ie_xmit_buf));
   1268 	}
   1269 
   1270 	for (n = 0; n < sc->nrxbuf; n++) {
   1271 		sc->rbuffs[n] = (volatile struct ie_recv_buf_desc *) ptr;
   1272 		ptr = (sc->align)(ptr + sizeof(struct ie_recv_buf_desc));
   1273 	}
   1274 
   1275 	/*
   1276 	 * step 2b: link together recv bufs and set EOL on last one
   1277 	 */
   1278 	for (n = 0; n < sc->nrxbuf; n++) {
   1279 		sc->rbuffs[n]->ie_rbd_next =
   1280 		    MK_16(sc->sc_maddr, sc->rbuffs[(n + 1) % sc->nrxbuf]);
   1281 	}
   1282 	sc->rbuffs[sc->nrxbuf - 1]->ie_rbd_length |= SWAP(IE_RBD_LAST);
   1283 
   1284 	/*
   1285 	 * step 3: allocate the actual data buffers for xmit and recv
   1286 	 * recv buffer gets linked into recv_buf_desc list here
   1287 	 */
   1288 	for (n = 0; n < NTXBUF; n++) {
   1289 		sc->xmit_cbuffs[n] = (u_char *) ptr;
   1290 		ptr = (sc->align)(ptr + IE_TBUF_SIZE);
   1291 	}
   1292 
   1293 	/* Pointers to last packet sent and next available transmit buffer. */
   1294 	sc->xchead = sc->xctail = 0;
   1295 
   1296 	/* Clear transmit-busy flag and set number of free transmit buffers. */
   1297 	sc->xmit_busy = 0;
   1298 
   1299 	for (n = 0; n < sc->nrxbuf; n++) {
   1300 		sc->cbuffs[n] = (char *) ptr;	/* XXX why char vs uchar? */
   1301 		sc->rbuffs[n]->ie_rbd_length = SWAP(IE_RBUF_SIZE);
   1302 		ST_24(sc->rbuffs[n]->ie_rbd_buffer, MK_24(sc->sc_iobase, ptr));
   1303 		ptr = (sc->align)(ptr + IE_RBUF_SIZE);
   1304 	}
   1305 
   1306 	/*
   1307 	 * step 4: set the head and tail pointers on receive to keep track of
   1308 	 * the order in which RFDs and RBDs are used.   link in recv frames
   1309 	 * and buffer into the scb.
   1310 	 */
   1311 
   1312 	sc->rfhead = 0;
   1313 	sc->rftail = sc->nframes - 1;
   1314 	sc->rbhead = 0;
   1315 	sc->rbtail = sc->nrxbuf - 1;
   1316 
   1317 	sc->scb->ie_recv_list = MK_16(sc->sc_maddr, sc->rframes[0]);
   1318 	sc->rframes[0]->ie_fd_buf_desc = MK_16(sc->sc_maddr, sc->rbuffs[0]);
   1319 
   1320 #ifdef IEDEBUG
   1321 	printf("IE_DEBUG: reserved %d bytes\n", ptr - sc->buf_area);
   1322 #endif
   1323 }
   1324 
   1325 /*
   1326  * Run the multicast setup command.
   1327  * Called at splnet().
   1328  */
   1329 static int
   1330 mc_setup(sc, ptr)
   1331 	struct ie_softc *sc;
   1332 	void *ptr;
   1333 {
   1334 	volatile struct ie_mcast_cmd *cmd = ptr;
   1335 
   1336 	cmd->com.ie_cmd_status = SWAP(0);
   1337 	cmd->com.ie_cmd_cmd = SWAP(IE_CMD_MCAST | IE_CMD_LAST);
   1338 	cmd->com.ie_cmd_link = SWAP(0xffff);
   1339 
   1340 	(sc->memcopy)((caddr_t)sc->mcast_addrs, (caddr_t)cmd->ie_mcast_addrs,
   1341 	    sc->mcast_count * sizeof *sc->mcast_addrs);
   1342 
   1343 	cmd->ie_mcast_bytes =
   1344 	  SWAP(sc->mcast_count * ETHER_ADDR_LEN); /* grrr... */
   1345 
   1346 	sc->scb->ie_command_list = MK_16(sc->sc_maddr, cmd);
   1347 	if (command_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
   1348 	    (SWAP(cmd->com.ie_cmd_status) & IE_STAT_OK) == 0) {
   1349 		printf("%s: multicast address setup command failed\n",
   1350 		    sc->sc_dev.dv_xname);
   1351 		return 0;
   1352 	}
   1353 	return 1;
   1354 }
   1355 
   1356 /*
   1357  * This routine takes the environment generated by check_ie_present() and adds
   1358  * to it all the other structures we need to operate the adapter.  This
   1359  * includes executing the CONFIGURE, IA-SETUP, and MC-SETUP commands, starting
   1360  * the receiver unit, and clearing interrupts.
   1361  *
   1362  * THIS ROUTINE MUST BE CALLED AT splnet() OR HIGHER.
   1363  */
   1364 int
   1365 ieinit(sc)
   1366 	struct ie_softc *sc;
   1367 {
   1368 	volatile struct ie_sys_ctl_block *scb = sc->scb;
   1369 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1370 	void *ptr;
   1371 
   1372 	ptr = sc->buf_area;
   1373 
   1374 	/*
   1375 	 * Send the configure command first.
   1376 	 */
   1377 	{
   1378 		volatile struct ie_config_cmd *cmd = ptr;
   1379 
   1380 		scb->ie_command_list = MK_16(sc->sc_maddr, cmd);
   1381 		cmd->com.ie_cmd_status = SWAP(0);
   1382 		cmd->com.ie_cmd_cmd = SWAP(IE_CMD_CONFIG | IE_CMD_LAST);
   1383 		cmd->com.ie_cmd_link = SWAP(0xffff);
   1384 
   1385 		ie_setup_config(cmd, sc->promisc, 0);
   1386 
   1387 		if (command_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
   1388 		    (SWAP(cmd->com.ie_cmd_status) & IE_STAT_OK) == 0) {
   1389 			printf("%s: configure command failed\n",
   1390 			    sc->sc_dev.dv_xname);
   1391 			return 0;
   1392 		}
   1393 	}
   1394 
   1395 	/*
   1396 	 * Now send the Individual Address Setup command.
   1397 	 */
   1398 	{
   1399 		volatile struct ie_iasetup_cmd *cmd = ptr;
   1400 
   1401 		scb->ie_command_list = MK_16(sc->sc_maddr, cmd);
   1402 		cmd->com.ie_cmd_status = SWAP(0);
   1403 		cmd->com.ie_cmd_cmd = SWAP(IE_CMD_IASETUP | IE_CMD_LAST);
   1404 		cmd->com.ie_cmd_link = SWAP(0xffff);
   1405 
   1406 		(sc->memcopy)(LLADDR(ifp->if_sadl),
   1407 		      (caddr_t)&cmd->ie_address, sizeof cmd->ie_address);
   1408 
   1409 		if (command_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
   1410 		    (SWAP(cmd->com.ie_cmd_status) & IE_STAT_OK) == 0) {
   1411 			printf("%s: individual address setup command failed\n",
   1412 			    sc->sc_dev.dv_xname);
   1413 			return 0;
   1414 		}
   1415 	}
   1416 
   1417 	/*
   1418 	 * Now run the time-domain reflectometer.
   1419 	 */
   1420 	run_tdr(sc, ptr);
   1421 
   1422 	/*
   1423 	 * Acknowledge any interrupts we have generated thus far.
   1424 	 */
   1425 	ie_ack(sc, IE_ST_WHENCE);
   1426 
   1427 	/*
   1428 	 * Set up the transmit and recv buffers.
   1429 	 */
   1430 	setup_bufs(sc);
   1431 
   1432 	ifp->if_flags |= IFF_RUNNING;
   1433 	ifp->if_flags &= ~IFF_OACTIVE;
   1434 
   1435 	sc->scb->ie_recv_list = MK_16(sc->sc_maddr, sc->rframes[0]);
   1436 	command_and_wait(sc, IE_RU_START, 0, 0);
   1437 
   1438 	ie_ack(sc, IE_ST_WHENCE);
   1439 
   1440 	if (sc->hwinit)
   1441 		(sc->hwinit)(sc);
   1442 
   1443 	return 0;
   1444 }
   1445 
   1446 static void
   1447 iestop(sc)
   1448 	struct ie_softc *sc;
   1449 {
   1450 
   1451 	command_and_wait(sc, IE_RU_DISABLE, 0, 0);
   1452 }
   1453 
   1454 int
   1455 ieioctl(ifp, cmd, data)
   1456 	register struct ifnet *ifp;
   1457 	u_long cmd;
   1458 	caddr_t data;
   1459 {
   1460 	struct ie_softc *sc = ifp->if_softc;
   1461 	struct ifaddr *ifa = (struct ifaddr *)data;
   1462 	struct ifreq *ifr = (struct ifreq *)data;
   1463 	int s, error = 0;
   1464 
   1465 	s = splnet();
   1466 
   1467 	switch(cmd) {
   1468 
   1469 	case SIOCSIFADDR:
   1470 		ifp->if_flags |= IFF_UP;
   1471 
   1472 		switch(ifa->ifa_addr->sa_family) {
   1473 #ifdef INET
   1474 		case AF_INET:
   1475 			ieinit(sc);
   1476 			arp_ifinit(ifp, ifa);
   1477 			break;
   1478 #endif
   1479 #ifdef NS
   1480 		/* XXX - This code is probably wrong. */
   1481 		case AF_NS:
   1482 		    {
   1483 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1484 
   1485 			if (ns_nullhost(*ina))
   1486 				ina->x_host =
   1487 				    *(union ns_host *)LLADDR(ifp->if_sadl);
   1488 			else
   1489 				bcopy(ina->x_host.c_host,
   1490 				    LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
   1491 			/* Set new address. */
   1492 			ieinit(sc);
   1493 			break;
   1494 		    }
   1495 #endif /* NS */
   1496 		default:
   1497 			ieinit(sc);
   1498 			break;
   1499 		}
   1500 		break;
   1501 
   1502 	case SIOCSIFFLAGS:
   1503 		sc->promisc = ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI);
   1504 		if ((ifp->if_flags & IFF_UP) == 0 &&
   1505 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   1506 			/*
   1507 			 * If interface is marked down and it is running, then
   1508 			 * stop it.
   1509 			 */
   1510 			iestop(sc);
   1511 			ifp->if_flags &= ~IFF_RUNNING;
   1512 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
   1513 			   (ifp->if_flags & IFF_RUNNING) == 0) {
   1514 			/*
   1515 			 * If interface is marked up and it is stopped, then
   1516 			 * start it.
   1517 			 */
   1518 			ieinit(sc);
   1519 		} else {
   1520 			/*
   1521 			 * Reset the interface to pick up changes in any other
   1522 			 * flags that affect hardware registers.
   1523 			 */
   1524 			iestop(sc);
   1525 			ieinit(sc);
   1526 		}
   1527 #ifdef IEDEBUG
   1528 		if (ifp->if_flags & IFF_DEBUG)
   1529 			sc->sc_debug = IED_ALL;
   1530 		else
   1531 			sc->sc_debug = 0;
   1532 #endif
   1533 		break;
   1534 
   1535 	case SIOCADDMULTI:
   1536 	case SIOCDELMULTI:
   1537 		error = (cmd == SIOCADDMULTI) ?
   1538 		    ether_addmulti(ifr, &sc->sc_ethercom):
   1539 		    ether_delmulti(ifr, &sc->sc_ethercom);
   1540 
   1541 		if (error == ENETRESET) {
   1542 			/*
   1543 			 * Multicast list has changed; set the hardware filter
   1544 			 * accordingly.
   1545 			 */
   1546 			mc_reset(sc);
   1547 			error = 0;
   1548 		}
   1549 		break;
   1550 
   1551 	default:
   1552 		error = EINVAL;
   1553 	}
   1554 	splx(s);
   1555 	return error;
   1556 }
   1557 
   1558 static void
   1559 mc_reset(sc)
   1560 	struct ie_softc *sc;
   1561 {
   1562 	struct ether_multi *enm;
   1563 	struct ether_multistep step;
   1564 
   1565 	/*
   1566 	 * Step through the list of addresses.
   1567 	 */
   1568 	sc->mcast_count = 0;
   1569 	ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
   1570 	while (enm) {
   1571 		if (sc->mcast_count >= MAXMCAST ||
   1572 		    bcmp(enm->enm_addrlo, enm->enm_addrhi, 6) != 0) {
   1573 			sc->sc_ethercom.ec_if.if_flags |= IFF_ALLMULTI;
   1574 			ieioctl(&sc->sc_ethercom.ec_if, SIOCSIFFLAGS, (void *)0);
   1575 			goto setflag;
   1576 		}
   1577 
   1578 		bcopy(enm->enm_addrlo, &sc->mcast_addrs[sc->mcast_count], 6);
   1579 		sc->mcast_count++;
   1580 		ETHER_NEXT_MULTI(step, enm);
   1581 	}
   1582 setflag:
   1583 	sc->want_mcsetup = 1;
   1584 }
   1585 
   1586 #ifdef IEDEBUG
   1587 void
   1588 print_rbd(rbd)
   1589 	volatile struct ie_recv_buf_desc *rbd;
   1590 {
   1591 	u_long bufval;
   1592 
   1593 	bcopy((char *)&rbd->ie_rbd_buffer, &bufval, 4); /*XXX*/
   1594 
   1595 	printf("RBD at %08lx:\nactual %04x, next %04x, buffer %lx\n"
   1596 		"length %04x, mbz %04x\n", (u_long)rbd,
   1597 		SWAP(rbd->ie_rbd_actual),
   1598 		SWAP(rbd->ie_rbd_next),
   1599 		bufval,
   1600 		SWAP(rbd->ie_rbd_length),
   1601 		rbd->mbz);
   1602 }
   1603 #endif
   1604