Home | History | Annotate | Line # | Download | only in isa
if_ate.c revision 1.16
      1 /*
      2  * All Rights Reserved, Copyright (C) Fujitsu Limited 1995
      3  *
      4  * This software may be used, modified, copied, distributed, and sold, in
      5  * both source and binary form provided that the above copyright, these
      6  * terms and the following disclaimer are retained.  The name of the author
      7  * and/or the contributor may not be used to endorse or promote products
      8  * derived from this software without specific prior written permission.
      9  *
     10  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND THE CONTRIBUTOR ``AS IS'' AND
     11  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     12  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     13  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR THE CONTRIBUTOR BE LIABLE
     14  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     15  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     16  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION.
     17  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     18  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     19  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     20  * SUCH DAMAGE.
     21  */
     22 
     23 /*
     24  * Portions copyright (C) 1993, David Greenman.  This software may be used,
     25  * modified, copied, distributed, and sold, in both source and binary form
     26  * provided that the above copyright and these terms are retained.  Under no
     27  * circumstances is the author responsible for the proper functioning of this
     28  * software, nor does the author assume any responsibility for damages
     29  * incurred with its use.
     30  */
     31 
     32 #define FE_VERSION "if_fe.c ver. 0.8"
     33 
     34 /*
     35  * Device driver for Fujitsu MB86960A/MB86965A based Ethernet cards.
     36  * Contributed by M.S. <seki (at) sysrap.cs.fujitsu.co.jp>
     37  *
     38  * This version is intended to be a generic template for various
     39  * MB86960A/MB86965A based Ethernet cards.  It currently supports
     40  * Fujitsu FMV-180 series (i.e., FMV-181 and FMV-182) and Allied-
     41  * Telesis AT1700 series and RE2000 series.  There are some
     42  * unnecessary hooks embedded, which are primarily intended to support
     43  * other types of Ethernet cards, but the author is not sure whether
     44  * they are useful.
     45  */
     46 
     47 #include "bpfilter.h"
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/errno.h>
     52 #include <sys/ioctl.h>
     53 #include <sys/mbuf.h>
     54 #include <sys/socket.h>
     55 #include <sys/syslog.h>
     56 #include <sys/device.h>
     57 
     58 #include <net/if.h>
     59 #include <net/if_dl.h>
     60 #include <net/if_types.h>
     61 #include <net/netisr.h>
     62 
     63 #ifdef INET
     64 #include <netinet/in.h>
     65 #include <netinet/in_systm.h>
     66 #include <netinet/in_var.h>
     67 #include <netinet/ip.h>
     68 #include <netinet/if_ether.h>
     69 #endif
     70 
     71 #ifdef NS
     72 #include <netns/ns.h>
     73 #include <netns/ns_if.h>
     74 #endif
     75 
     76 #if NBPFILTER > 0
     77 #include <net/bpf.h>
     78 #include <net/bpfdesc.h>
     79 #endif
     80 
     81 #include <machine/cpu.h>
     82 #include <machine/intr.h>
     83 #include <machine/pio.h>
     84 
     85 #include <dev/isa/isareg.h>
     86 #include <dev/isa/isavar.h>
     87 #include <dev/ic/mb86960reg.h>
     88 #include <dev/isa/if_fereg.h>
     89 
     90 /*
     91  * Default settings for fe driver specific options.
     92  * They can be set in config file by "options" statements.
     93  */
     94 
     95 /*
     96  * Debug control.
     97  * 0: No debug at all.  All debug specific codes are stripped off.
     98  * 1: Silent.  No debug messages are logged except emergent ones.
     99  * 2: Brief.  Lair events and/or important information are logged.
    100  * 3: Detailed.  Logs all information which *may* be useful for debugging.
    101  * 4: Trace.  All actions in the driver is logged.  Super verbose.
    102  */
    103 #ifndef FE_DEBUG
    104 #define FE_DEBUG		1
    105 #endif
    106 
    107 /*
    108  * Delay padding of short transmission packets to minimum Ethernet size.
    109  * This may or may not gain performance.  An EXPERIMENTAL option.
    110  */
    111 #ifndef FE_DELAYED_PADDING
    112 #define FE_DELAYED_PADDING	0
    113 #endif
    114 
    115 /*
    116  * Transmit just one packet per a "send" command to 86960.
    117  * This option is intended for performance test.  An EXPERIMENTAL option.
    118  */
    119 #ifndef FE_SINGLE_TRANSMISSION
    120 #define FE_SINGLE_TRANSMISSION	0
    121 #endif
    122 
    123 /*
    124  * Device configuration flags.
    125  */
    126 
    127 /* DLCR6 settings. */
    128 #define FE_FLAGS_DLCR6_VALUE	0x007F
    129 
    130 /* Force DLCR6 override. */
    131 #define FE_FLAGS_OVERRIDE_DLCR6	0x0080
    132 
    133 /* A cludge for PCMCIA support. */
    134 #define FE_FLAGS_PCMCIA		0x8000
    135 
    136 /* Identification of the driver version. */
    137 static char const fe_version[] = FE_VERSION " / " FE_REG_VERSION;
    138 
    139 /*
    140  * Supported hardware (Ethernet card) types
    141  * This information is currently used only for debugging
    142  */
    143 enum fe_type {
    144 	/* For cards which are successfully probed but not identified. */
    145 	FE_TYPE_UNKNOWN,
    146 
    147 	/* Fujitsu FMV-180 series. */
    148 	FE_TYPE_FMV181,
    149 	FE_TYPE_FMV182,
    150 
    151 	/* Allied-Telesis AT1700 series and RE2000 series. */
    152 	FE_TYPE_AT1700T,
    153 	FE_TYPE_AT1700BT,
    154 	FE_TYPE_AT1700FT,
    155 	FE_TYPE_AT1700AT,
    156 	FE_TYPE_RE2000,
    157 
    158 	/* PCMCIA by Fujitsu. */
    159 	FE_TYPE_MBH10302,
    160 	FE_TYPE_MBH10304,
    161 };
    162 
    163 /*
    164  * fe_softc: per line info and status
    165  */
    166 struct fe_softc {
    167 	struct	device sc_dev;
    168 	void	*sc_ih;
    169 
    170 	struct	arpcom sc_arpcom;	/* ethernet common */
    171 
    172 	/* Set by probe() and not modified in later phases. */
    173 	enum	fe_type type;	/* interface type code */
    174 	char	*typestr;	/* printable name of the interface. */
    175 	int	sc_iobase;	/* MB86960A I/O base address */
    176 
    177 	u_char	proto_dlcr4;	/* DLCR4 prototype. */
    178 	u_char	proto_dlcr5;	/* DLCR5 prototype. */
    179 	u_char	proto_dlcr6;	/* DLCR6 prototype. */
    180 	u_char	proto_dlcr7;	/* DLCR7 prototype. */
    181 	u_char	proto_bmpr13;	/* BMPR13 prototype. */
    182 
    183 	/* Vendor specific hooks. */
    184 	void	(*init) __P((struct fe_softc *)); /* Just before fe_init(). */
    185 	void	(*stop) __P((struct fe_softc *)); /* Just after fe_stop(). */
    186 
    187 	/* Transmission buffer management. */
    188 	u_short	txb_size;	/* total bytes in TX buffer */
    189 	u_short	txb_free;	/* free bytes in TX buffer */
    190 	u_char	txb_count;	/* number of packets in TX buffer */
    191 	u_char	txb_sched;	/* number of scheduled packets */
    192 	u_char	txb_padding;	/* number of delayed padding bytes */
    193 
    194 	/* Multicast address filter management. */
    195 	u_char	filter_change;	/* MARs must be changed ASAP. */
    196 	u_char	filter[FE_FILTER_LEN];	/* new filter value. */
    197 };
    198 
    199 /* Frequently accessed members in arpcom. */
    200 #define sc_enaddr	sc_arpcom.ac_enaddr
    201 
    202 /* Standard driver entry points.  These can be static. */
    203 int	feprobe		__P((struct device *, void *, void *));
    204 void	feattach	__P((struct device *, struct device *, void *));
    205 int	feintr		__P((void *));
    206 void	fe_init		__P((struct fe_softc *));
    207 int	fe_ioctl	__P((struct ifnet *, u_long, caddr_t));
    208 void	fe_start	__P((struct ifnet *));
    209 void	fe_reset	__P((struct fe_softc *));
    210 void	fe_watchdog	__P((struct ifnet *));
    211 
    212 /* Local functions.  Order of declaration is confused.  FIXME. */
    213 int	fe_probe_fmv	__P((struct fe_softc *, struct isa_attach_args *));
    214 int	fe_probe_ati	__P((struct fe_softc *, struct isa_attach_args *));
    215 int	fe_probe_mbh	__P((struct fe_softc *, struct isa_attach_args *));
    216 void	fe_read_eeprom	__P((struct fe_softc *, u_char *));
    217 void	fe_init_mbh	__P((struct fe_softc *));
    218 int	fe_get_packet	__P((struct fe_softc *, int));
    219 void	fe_stop		__P((struct fe_softc *));
    220 void	fe_tint		__P((struct fe_softc *, u_char));
    221 void	fe_rint		__P((struct fe_softc *, u_char));
    222 static inline
    223 void	fe_xmit		__P((struct fe_softc *));
    224 void	fe_write_mbufs	__P((struct fe_softc *, struct mbuf *));
    225 static inline
    226 void	fe_droppacket	__P((struct fe_softc *));
    227 void	fe_getmcaf	__P((struct arpcom *, u_char *));
    228 void	fe_setmode	__P((struct fe_softc *));
    229 void	fe_loadmar	__P((struct fe_softc *));
    230 #if FE_DEBUG >= 1
    231 void	fe_dump		__P((int, struct fe_softc *));
    232 #endif
    233 
    234 struct cfattach fe_ca = {
    235 	sizeof(struct fe_softc), feprobe, feattach
    236 };
    237 
    238 struct cfdriver fe_cd = {
    239 	NULL, "fe", DV_IFNET
    240 };
    241 
    242 /* Ethernet constants.  To be defined in if_ehter.h?  FIXME. */
    243 #define ETHER_MIN_LEN	60	/* with header, without CRC. */
    244 #define ETHER_MAX_LEN	1514	/* with header, without CRC. */
    245 #define ETHER_ADDR_LEN	6	/* number of bytes in an address. */
    246 #define ETHER_HDR_SIZE	14	/* src addr, dst addr, and data type. */
    247 
    248 /*
    249  * Fe driver specific constants which relate to 86960/86965.
    250  */
    251 
    252 /* Interrupt masks. */
    253 #define FE_TMASK (FE_D2_COLL16 | FE_D2_TXDONE)
    254 #define FE_RMASK (FE_D3_OVRFLO | FE_D3_CRCERR | \
    255 		  FE_D3_ALGERR | FE_D3_SRTPKT | FE_D3_PKTRDY)
    256 
    257 /* Maximum number of iterrations for a receive interrupt. */
    258 #define FE_MAX_RECV_COUNT ((65536 - 2048 * 2) / 64)
    259 	/* Maximum size of SRAM is 65536,
    260 	 * minimum size of transmission buffer in fe is 2x2KB,
    261 	 * and minimum amount of received packet including headers
    262 	 * added by the chip is 64 bytes.
    263 	 * Hence FE_MAX_RECV_COUNT is the upper limit for number
    264 	 * of packets in the receive buffer. */
    265 
    266 /*
    267  * Convenient routines to access contiguous I/O ports.
    268  */
    269 
    270 static inline void
    271 inblk (int addr, u_char * mem, int len)
    272 {
    273 	while (--len >= 0) {
    274 		*mem++ = inb(addr++);
    275 	}
    276 }
    277 
    278 static inline void
    279 outblk (int addr, u_char const * mem, int len)
    280 {
    281 	while (--len >= 0) {
    282 		outb(addr++, *mem++);
    283 	}
    284 }
    285 
    286 /*
    287  * Hardware probe routines.
    288  */
    289 
    290 /*
    291  * Determine if the device is present.
    292  */
    293 int
    294 feprobe(parent, match, aux)
    295 	struct device *parent;
    296 	void *match, *aux;
    297 {
    298 	struct fe_softc *sc = match;
    299 	struct isa_attach_args *ia = aux;
    300 
    301 #if FE_DEBUG >= 2
    302 	log(LOG_INFO, "%s: %s\n", sc->sc_dev.dv_xname, fe_version);
    303 #endif
    304 
    305 	/* Probe an address. */
    306 	sc->sc_iobase = ia->ia_iobase;
    307 
    308 	if (fe_probe_fmv(sc, ia))
    309 		return (1);
    310 	if (fe_probe_ati(sc, ia))
    311 		return (1);
    312 	if (fe_probe_mbh(sc, ia))
    313 		return (1);
    314 	return (0);
    315 }
    316 
    317 /*
    318  * Check for specific bits in specific registers have specific values.
    319  */
    320 struct fe_simple_probe_struct {
    321 	u_char port;	/* Offset from the base I/O address. */
    322 	u_char mask;	/* Bits to be checked. */
    323 	u_char bits;	/* Values to be compared against. */
    324 };
    325 
    326 static inline int
    327 fe_simple_probe (int addr, struct fe_simple_probe_struct const * sp)
    328 {
    329 	struct fe_simple_probe_struct const * p;
    330 
    331 	for (p = sp; p->mask != 0; p++) {
    332 		if ((inb(addr + p->port) & p->mask) != p->bits) {
    333 			return (0);
    334 		}
    335 	}
    336 	return (1);
    337 }
    338 
    339 /*
    340  * Routines to read all bytes from the config EEPROM through MB86965A.
    341  * I'm not sure what exactly I'm doing here...  I was told just to follow
    342  * the steps, and it worked.  Could someone tell me why the following
    343  * code works?  (Or, why all similar codes I tried previously doesn't
    344  * work.)  FIXME.
    345  */
    346 
    347 static inline void
    348 strobe (int bmpr16)
    349 {
    350 	/*
    351 	 * Output same value twice.  To speed-down execution?
    352 	 */
    353 	outb(bmpr16, FE_B16_SELECT);
    354 	outb(bmpr16, FE_B16_SELECT);
    355 	outb(bmpr16, FE_B16_SELECT | FE_B16_CLOCK);
    356 	outb(bmpr16, FE_B16_SELECT | FE_B16_CLOCK);
    357 	outb(bmpr16, FE_B16_SELECT);
    358 	outb(bmpr16, FE_B16_SELECT);
    359 }
    360 
    361 void
    362 fe_read_eeprom(sc, data)
    363 	struct fe_softc *sc;
    364 	u_char *data;
    365 {
    366 	int iobase = sc->sc_iobase;
    367 	int bmpr16 = iobase + FE_BMPR16;
    368 	int bmpr17 = iobase + FE_BMPR17;
    369 	u_char n, val, bit;
    370 
    371 	/* Read bytes from EEPROM; two bytes per an iterration. */
    372 	for (n = 0; n < FE_EEPROM_SIZE / 2; n++) {
    373 		/* Reset the EEPROM interface. */
    374 		outb(bmpr16, 0x00);
    375 		outb(bmpr17, 0x00);
    376 		outb(bmpr16, FE_B16_SELECT);
    377 
    378 		/* Start EEPROM access. */
    379 		outb(bmpr17, FE_B17_DATA);
    380 		strobe(bmpr16);
    381 
    382 		/* Pass the iterration count to the chip. */
    383 		val = 0x80 | n;
    384 		for (bit = 0x80; bit != 0x00; bit >>= 1) {
    385 			outb(bmpr17, (val & bit) ? FE_B17_DATA : 0);
    386 			strobe(bmpr16);
    387 		}
    388 		outb(bmpr17, 0x00);
    389 
    390 		/* Read a byte. */
    391 		val = 0;
    392 		for (bit = 0x80; bit != 0x00; bit >>= 1) {
    393 			strobe(bmpr16);
    394 			if (inb(bmpr17) & FE_B17_DATA)
    395 				val |= bit;
    396 		}
    397 		*data++ = val;
    398 
    399 		/* Read one more byte. */
    400 		val = 0;
    401 		for (bit = 0x80; bit != 0x00; bit >>= 1) {
    402 			strobe(bmpr16);
    403 			if (inb(bmpr17) & FE_B17_DATA)
    404 				val |= bit;
    405 		}
    406 		*data++ = val;
    407 	}
    408 
    409 #if FE_DEBUG >= 3
    410 	/* Report what we got. */
    411 	data -= FE_EEPROM_SIZE;
    412 	log(LOG_INFO, "%s: EEPROM at %04x:"
    413 	    " %02x%02x%02x%02x %02x%02x%02x%02x -"
    414 	    " %02x%02x%02x%02x %02x%02x%02x%02x -"
    415 	    " %02x%02x%02x%02x %02x%02x%02x%02x -"
    416 	    " %02x%02x%02x%02x %02x%02x%02x%02x\n",
    417 	    sc->sc_dev.dv_xname, iobase,
    418 	    data[ 0], data[ 1], data[ 2], data[ 3],
    419 	    data[ 4], data[ 5], data[ 6], data[ 7],
    420 	    data[ 8], data[ 9], data[10], data[11],
    421 	    data[12], data[13], data[14], data[15],
    422 	    data[16], data[17], data[18], data[19],
    423 	    data[20], data[21], data[22], data[23],
    424 	    data[24], data[25], data[26], data[27],
    425 	    data[28], data[29], data[30], data[31]);
    426 #endif
    427 }
    428 
    429 /*
    430  * Hardware (vendor) specific probe routines.
    431  */
    432 
    433 /*
    434  * Probe and initialization for Fujitsu FMV-180 series boards
    435  */
    436 int
    437 fe_probe_fmv(sc, ia)
    438 	struct fe_softc *sc;
    439 	struct isa_attach_args *ia;
    440 {
    441 	int i, n;
    442 	int iobase = sc->sc_iobase;
    443 	int irq;
    444 
    445 	static int const iomap[8] =
    446 		{ 0x220, 0x240, 0x260, 0x280, 0x2A0, 0x2C0, 0x300, 0x340 };
    447 	static int const irqmap[4] =
    448 		{ 3, 7, 10, 15 };
    449 
    450 	static struct fe_simple_probe_struct const probe_table[] = {
    451 		{ FE_DLCR2, 0x70, 0x00 },
    452 		{ FE_DLCR4, 0x08, 0x00 },
    453 	    /*	{ FE_DLCR5, 0x80, 0x00 },	Doesn't work. */
    454 
    455 		{ FE_FMV0, FE_FMV0_MAGIC_MASK,  FE_FMV0_MAGIC_VALUE },
    456 		{ FE_FMV1, FE_FMV1_CARDID_MASK, FE_FMV1_CARDID_ID   },
    457 		{ FE_FMV3, FE_FMV3_EXTRA_MASK,  FE_FMV3_EXTRA_VALUE },
    458 #if 1
    459 	/*
    460 	 * Test *vendor* part of the station address for Fujitsu.
    461 	 * The test will gain reliability of probe process, but
    462 	 * it rejects FMV-180 clone boards manufactured by other vendors.
    463 	 * We have to turn the test off when such cards are made available.
    464 	 */
    465 		{ FE_FMV4, 0xFF, 0x00 },
    466 		{ FE_FMV5, 0xFF, 0x00 },
    467 		{ FE_FMV6, 0xFF, 0x0E },
    468 #else
    469 	/*
    470 	 * We can always verify the *first* 2 bits (in Ehternet
    471 	 * bit order) are "no multicast" and "no local" even for
    472 	 * unknown vendors.
    473 	 */
    474 		{ FE_FMV4, 0x03, 0x00 },
    475 #endif
    476 		{ 0 }
    477 	};
    478 
    479 #if 0
    480 	/*
    481 	 * Dont probe at all if the config says we are PCMCIA...
    482 	 */
    483 	if ((cf->cf_flags & FE_FLAGS_PCMCIA) != 0)
    484 		return (0);
    485 #endif
    486 
    487 	/*
    488 	 * See if the sepcified address is possible for FMV-180 series.
    489 	 */
    490 	for (i = 0; i < 8; i++) {
    491 		if (iomap[i] == iobase)
    492 			break;
    493 	}
    494 	if (i == 8)
    495 		return (0);
    496 
    497 	/* Simple probe. */
    498 	if (!fe_simple_probe(iobase, probe_table))
    499 		return (0);
    500 
    501 	/* Check if our I/O address matches config info on EEPROM. */
    502 	n = (inb(iobase + FE_FMV2) & FE_FMV2_ADDR) >> FE_FMV2_ADDR_SHIFT;
    503 	if (iomap[n] != iobase)
    504 		return (0);
    505 
    506 	/* Determine the card type. */
    507 	switch (inb(iobase + FE_FMV0) & FE_FMV0_MODEL) {
    508 	case FE_FMV0_MODEL_FMV181:
    509 		sc->type = FE_TYPE_FMV181;
    510 		sc->typestr = "FMV-181";
    511 		break;
    512 	case FE_FMV0_MODEL_FMV182:
    513 		sc->type = FE_TYPE_FMV182;
    514 		sc->typestr = "FMV-182";
    515 		break;
    516 	default:
    517 	  	/* Unknown card type: maybe a new model, but... */
    518 		return (0);
    519 	}
    520 
    521 	/*
    522 	 * An FMV-180 has successfully been proved.
    523 	 * Determine which IRQ to be used.
    524 	 *
    525 	 * In this version, we always get an IRQ assignment from the
    526 	 * FMV-180's configuration EEPROM, ignoring that specified in
    527 	 * config file.
    528 	 */
    529 	n = (inb(iobase + FE_FMV2) & FE_FMV2_IRQ) >> FE_FMV2_IRQ_SHIFT;
    530 	irq = irqmap[n];
    531 
    532 	if (ia->ia_irq != IRQUNK) {
    533 		if (ia->ia_irq != irq) {
    534 			printf("%s: irq mismatch; kernel configured %d != board configured %d\n",
    535 			    sc->sc_dev.dv_xname, ia->ia_irq, irq);
    536 			return (0);
    537 		}
    538 	} else
    539 		ia->ia_irq = irq;
    540 
    541 	/*
    542 	 * Initialize constants in the per-line structure.
    543 	 */
    544 
    545 	/* Get our station address from EEPROM. */
    546 	inblk(iobase + FE_FMV4, sc->sc_enaddr, ETHER_ADDR_LEN);
    547 
    548 	/* Make sure we got a valid station address. */
    549 	if ((sc->sc_enaddr[0] & 0x03) != 0x00
    550 	  || (sc->sc_enaddr[0] == 0x00
    551 	    && sc->sc_enaddr[1] == 0x00
    552 	    && sc->sc_enaddr[2] == 0x00))
    553 		return (0);
    554 
    555 	/* Register values which depend on board design. */
    556 	sc->proto_dlcr4 = FE_D4_LBC_DISABLE | FE_D4_CNTRL;
    557 	sc->proto_dlcr5 = 0;
    558 	sc->proto_dlcr7 = FE_D7_BYTSWP_LH | FE_D7_IDENT_EC;
    559 	sc->proto_bmpr13 = FE_B13_TPTYPE_UTP | FE_B13_PORT_AUTO;
    560 
    561 	/*
    562 	 * Program the 86960 as follows:
    563 	 *	SRAM: 32KB, 100ns, byte-wide access.
    564 	 *	Transmission buffer: 4KB x 2.
    565 	 *	System bus interface: 16 bits.
    566 	 * We cannot change these values but TXBSIZE, because they
    567 	 * are hard-wired on the board.  Modifying TXBSIZE will affect
    568 	 * the driver performance.
    569 	 */
    570 	sc->proto_dlcr6 = FE_D6_BUFSIZ_32KB | FE_D6_TXBSIZ_2x4KB
    571 		| FE_D6_BBW_BYTE | FE_D6_SBW_WORD | FE_D6_SRAM_100ns;
    572 
    573 	/*
    574 	 * Minimum initialization of the hardware.
    575 	 * We write into registers; hope I/O ports have no
    576 	 * overlap with other boards.
    577 	 */
    578 
    579 	/* Initialize ASIC. */
    580 	outb(iobase + FE_FMV3, 0);
    581 	outb(iobase + FE_FMV10, 0);
    582 
    583 	/* Wait for a while.  I'm not sure this is necessary.  FIXME. */
    584 	delay(200);
    585 
    586 	/* Initialize 86960. */
    587 	outb(iobase + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
    588 	delay(200);
    589 
    590 	/* Disable all interrupts. */
    591 	outb(iobase + FE_DLCR2, 0);
    592 	outb(iobase + FE_DLCR3, 0);
    593 
    594 	/* Turn the "master interrupt control" flag of ASIC on. */
    595 	outb(iobase + FE_FMV3, FE_FMV3_ENABLE_FLAG);
    596 
    597 	/*
    598 	 * That's all.  FMV-180 occupies 32 I/O addresses, by the way.
    599 	 */
    600 	ia->ia_iosize = 32;
    601 	ia->ia_msize = 0;
    602 	return (1);
    603 }
    604 
    605 /*
    606  * Probe and initialization for Allied-Telesis AT1700/RE2000 series.
    607  */
    608 int
    609 fe_probe_ati(sc, ia)
    610 	struct fe_softc *sc;
    611 	struct isa_attach_args *ia;
    612 {
    613 	int i, n;
    614 	int iobase = sc->sc_iobase;
    615 	u_char eeprom[FE_EEPROM_SIZE];
    616 	u_char save16, save17;
    617 	int irq;
    618 
    619 	static int const iomap[8] =
    620 		{ 0x260, 0x280, 0x2A0, 0x240, 0x340, 0x320, 0x380, 0x300 };
    621 	static int const irqmap[4][4] = {
    622 		{  3,  4,  5,  9 },
    623 		{ 10, 11, 12, 15 },
    624 		{  3, 11,  5, 15 },
    625 		{ 10, 11, 14, 15 },
    626 	};
    627 	static struct fe_simple_probe_struct const probe_table[] = {
    628 		{ FE_DLCR2,  0x70, 0x00 },
    629 		{ FE_DLCR4,  0x08, 0x00 },
    630 		{ FE_DLCR5,  0x80, 0x00 },
    631 #if 0
    632 		{ FE_BMPR16, 0x1B, 0x00 },
    633 		{ FE_BMPR17, 0x7F, 0x00 },
    634 #endif
    635 		{ 0 }
    636 	};
    637 
    638 #if 0
    639 	/*
    640 	 * Don't probe at all if the config says we are PCMCIA...
    641 	 */
    642 	if ((cf->cf_flags & FE_FLAGS_PCMCIA) != 0)
    643 		return (0);
    644 #endif
    645 
    646 #if FE_DEBUG >= 4
    647 	log(LOG_INFO, "%s: probe (0x%x) for ATI\n", sc->sc_dev.dv_xname, iobase);
    648 	fe_dump(LOG_INFO, sc);
    649 #endif
    650 
    651 	/*
    652 	 * See if the sepcified address is possible for MB86965A JLI mode.
    653 	 */
    654 	for (i = 0; i < 8; i++) {
    655 		if (iomap[i] == iobase)
    656 			break;
    657 	}
    658 	if (i == 8)
    659 		return (0);
    660 
    661 	/*
    662 	 * We should test if MB86965A is on the base address now.
    663 	 * Unfortunately, it is very hard to probe it reliably, since
    664 	 * we have no way to reset the chip under software control.
    665 	 * On cold boot, we could check the "signature" bit patterns
    666 	 * described in the Fujitsu document.  On warm boot, however,
    667 	 * we can predict almost nothing about register values.
    668 	 */
    669 	if (!fe_simple_probe(iobase, probe_table))
    670 		return (0);
    671 
    672 	/* Save old values of the registers. */
    673 	save16 = inb(iobase + FE_BMPR16);
    674 	save17 = inb(iobase + FE_BMPR17);
    675 
    676 	/* Check if our I/O address matches config info on 86965. */
    677 	n = (inb(iobase + FE_BMPR19) & FE_B19_ADDR) >> FE_B19_ADDR_SHIFT;
    678 	if (iomap[n] != iobase)
    679 		goto fail;
    680 
    681 	/*
    682 	 * We are now almost sure we have an AT1700 at the given
    683 	 * address.  So, read EEPROM through 86965.  We have to write
    684 	 * into LSI registers to read from EEPROM.  I want to avoid it
    685 	 * at this stage, but I cannot test the presense of the chip
    686 	 * any further without reading EEPROM.  FIXME.
    687 	 */
    688 	fe_read_eeprom(sc, eeprom);
    689 
    690 	/* Make sure the EEPROM is turned off. */
    691 	outb(iobase + FE_BMPR16, 0);
    692 	outb(iobase + FE_BMPR17, 0);
    693 
    694 	/* Make sure that config info in EEPROM and 86965 agree. */
    695 	if (eeprom[FE_EEPROM_CONF] != inb(iobase + FE_BMPR19))
    696 		goto fail;
    697 
    698 	/*
    699 	 * Determine the card type.
    700 	 */
    701 	switch (eeprom[FE_ATI_EEP_MODEL]) {
    702 	case FE_ATI_MODEL_AT1700T:
    703 		sc->type = FE_TYPE_AT1700T;
    704 		sc->typestr = "AT-1700T";
    705 		break;
    706 	case FE_ATI_MODEL_AT1700BT:
    707 		sc->type = FE_TYPE_AT1700BT;
    708 		sc->typestr = "AT-1700BT";
    709 		break;
    710 	case FE_ATI_MODEL_AT1700FT:
    711 		sc->type = FE_TYPE_AT1700FT;
    712 		sc->typestr = "AT-1700FT";
    713 		break;
    714 	case FE_ATI_MODEL_AT1700AT:
    715 		sc->type = FE_TYPE_AT1700AT;
    716 		sc->typestr = "AT-1700AT";
    717 		break;
    718 	default:
    719 		sc->type = FE_TYPE_RE2000;
    720 		sc->typestr = "unknown (RE-2000?)";
    721 		break;
    722 	}
    723 
    724 	/*
    725 	 * Try to determine IRQ settings.
    726 	 * Different models use different ranges of IRQs.
    727 	 */
    728 	n = (inb(iobase + FE_BMPR19) & FE_B19_IRQ) >> FE_B19_IRQ_SHIFT;
    729 	switch (eeprom[FE_ATI_EEP_REVISION] & 0xf0) {
    730 	case 0x30:
    731 		irq = irqmap[3][n];
    732 		break;
    733 	case 0x10:
    734 	case 0x50:
    735 		irq = irqmap[2][n];
    736 		break;
    737 	case 0x40:
    738 	case 0x60:
    739 		if (eeprom[FE_ATI_EEP_MAGIC] & 0x04) {
    740 			irq = irqmap[1][n];
    741 			break;
    742 		}
    743 	default:
    744 		irq = irqmap[0][n];
    745 		break;
    746 	}
    747 
    748 	if (ia->ia_irq != IRQUNK) {
    749 		if (ia->ia_irq != irq) {
    750 			printf("%s: irq mismatch; kernel configured %d != board configured %d\n",
    751 			    sc->sc_dev.dv_xname, ia->ia_irq, irq);
    752 			return (0);
    753 		}
    754 	} else
    755 		ia->ia_irq = irq;
    756 
    757 	/*
    758 	 * Initialize constants in the per-line structure.
    759 	 */
    760 
    761 	/* Get our station address from EEPROM. */
    762 	bcopy(eeprom + FE_ATI_EEP_ADDR, sc->sc_enaddr, ETHER_ADDR_LEN);
    763 
    764 	/* Make sure we got a valid station address. */
    765 	if ((sc->sc_enaddr[0] & 0x03) != 0x00
    766 	  || (sc->sc_enaddr[0] == 0x00
    767 	    && sc->sc_enaddr[1] == 0x00
    768 	    && sc->sc_enaddr[2] == 0x00))
    769 		goto fail;
    770 
    771 	/* Should find all register prototypes here.  FIXME. */
    772 	sc->proto_dlcr4 = FE_D4_LBC_DISABLE | FE_D4_CNTRL;  /* FIXME */
    773 	sc->proto_dlcr5 = 0;
    774 	sc->proto_dlcr7 = FE_D7_BYTSWP_LH | FE_D7_IDENT_EC;
    775 #if 0	/* XXXX Should we use this? */
    776 	sc->proto_bmpr13 = eeprom[FE_ATI_EEP_MEDIA];
    777 #else
    778 	sc->proto_bmpr13 = FE_B13_TPTYPE_UTP | FE_B13_PORT_AUTO;
    779 #endif
    780 
    781 	/*
    782 	 * Program the 86965 as follows:
    783 	 *	SRAM: 32KB, 100ns, byte-wide access.
    784 	 *	Transmission buffer: 4KB x 2.
    785 	 *	System bus interface: 16 bits.
    786 	 * We cannot change these values but TXBSIZE, because they
    787 	 * are hard-wired on the board.  Modifying TXBSIZE will affect
    788 	 * the driver performance.
    789 	 */
    790 	sc->proto_dlcr6 = FE_D6_BUFSIZ_32KB | FE_D6_TXBSIZ_2x4KB
    791 		| FE_D6_BBW_BYTE | FE_D6_SBW_WORD | FE_D6_SRAM_100ns;
    792 
    793 #if FE_DEBUG >= 3
    794 	log(LOG_INFO, "%s: ATI found\n", sc->sc_dev.dv_xname);
    795 	fe_dump(LOG_INFO, sc);
    796 #endif
    797 
    798 	/* Initialize 86965. */
    799 	outb(iobase + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
    800 	delay(200);
    801 
    802 	/* Disable all interrupts. */
    803 	outb(iobase + FE_DLCR2, 0);
    804 	outb(iobase + FE_DLCR3, 0);
    805 
    806 #if FE_DEBUG >= 3
    807 	log(LOG_INFO, "%s: end of fe_probe_ati()\n", sc->sc_dev.dv_xname);
    808 	fe_dump(LOG_INFO, sc);
    809 #endif
    810 
    811 	/*
    812 	 * That's all.  AT1700 occupies 32 I/O addresses, by the way.
    813 	 */
    814 	ia->ia_iosize = 32;
    815 	ia->ia_msize = 0;
    816 	return (1);
    817 
    818 fail:
    819 	/* Restore register values, in the case we had no 86965. */
    820 	outb(iobase + FE_BMPR16, save16);
    821 	outb(iobase + FE_BMPR17, save17);
    822 	return (0);
    823 }
    824 
    825 /*
    826  * Probe and initialization for Fujitsu MBH10302 PCMCIA Ethernet interface.
    827  */
    828 int
    829 fe_probe_mbh(sc, ia)
    830 	struct fe_softc *sc;
    831 	struct isa_attach_args *ia;
    832 {
    833 	int iobase = sc->sc_iobase;
    834 
    835 	static struct fe_simple_probe_struct probe_table[] = {
    836 		{ FE_DLCR2, 0x70, 0x00 },
    837 		{ FE_DLCR4, 0x08, 0x00 },
    838 	    /*	{ FE_DLCR5, 0x80, 0x00 },	Does not work well. */
    839 #if 0
    840 	/*
    841 	 * Test *vendor* part of the address for Fujitsu.
    842 	 * The test will gain reliability of probe process, but
    843 	 * it rejects clones by other vendors, or OEM product
    844 	 * supplied by resalers other than Fujitsu.
    845 	 */
    846 		{ FE_MBH10, 0xFF, 0x00 },
    847 		{ FE_MBH11, 0xFF, 0x00 },
    848 		{ FE_MBH12, 0xFF, 0x0E },
    849 #else
    850 	/*
    851 	 * We can always verify the *first* 2 bits (in Ehternet
    852 	 * bit order) are "global" and "unicast" even for
    853 	 * unknown vendors.
    854 	 */
    855 		{ FE_MBH10, 0x03, 0x00 },
    856 #endif
    857         /* Just a gap?  Seems reliable, anyway. */
    858 		{ 0x12, 0xFF, 0x00 },
    859 		{ 0x13, 0xFF, 0x00 },
    860 		{ 0x14, 0xFF, 0x00 },
    861 		{ 0x15, 0xFF, 0x00 },
    862 		{ 0x16, 0xFF, 0x00 },
    863 		{ 0x17, 0xFF, 0x00 },
    864 		{ 0x18, 0xFF, 0xFF },
    865 		{ 0x19, 0xFF, 0xFF },
    866 
    867 		{ 0 }
    868 	};
    869 
    870 #if 0
    871 	/*
    872 	 * We need a PCMCIA flag.
    873 	 */
    874 	if ((cf->cf_flags & FE_FLAGS_PCMCIA) == 0)
    875 		return (0);
    876 #endif
    877 
    878 	/*
    879 	 * We need explicit IRQ and supported address.
    880 	 */
    881 	if (ia->ia_irq == IRQUNK || (iobase & ~0x3E0) != 0)
    882 		return (0);
    883 
    884 #if FE_DEBUG >= 3
    885 	log(LOG_INFO, "%s: top of fe_probe_mbh()\n", sc->sc_dev.dv_xname);
    886 	fe_dump(LOG_INFO, sc);
    887 #endif
    888 
    889 	/*
    890 	 * See if MBH10302 is on its address.
    891 	 * I'm not sure the following probe code works.  FIXME.
    892 	 */
    893 	if (!fe_simple_probe(iobase, probe_table))
    894 		return (0);
    895 
    896 	/* Determine the card type. */
    897 	sc->type = FE_TYPE_MBH10302;
    898 	sc->typestr = "MBH10302 (PCMCIA)";
    899 
    900 	/*
    901 	 * Initialize constants in the per-line structure.
    902 	 */
    903 
    904 	/* Get our station address from EEPROM. */
    905 	inblk(iobase + FE_MBH10, sc->sc_enaddr, ETHER_ADDR_LEN);
    906 
    907 	/* Make sure we got a valid station address. */
    908 	if ((sc->sc_enaddr[0] & 0x03) != 0x00
    909 	  || (sc->sc_enaddr[0] == 0x00
    910 	    && sc->sc_enaddr[1] == 0x00
    911 	    && sc->sc_enaddr[2] == 0x00))
    912 		return (0);
    913 
    914 	/* Should find all register prototypes here.  FIXME. */
    915 	sc->proto_dlcr4 = FE_D4_LBC_DISABLE | FE_D4_CNTRL;
    916 	sc->proto_dlcr5 = 0;
    917 	sc->proto_dlcr7 = FE_D7_BYTSWP_LH | FE_D7_IDENT_NICE;
    918 	sc->proto_bmpr13 = FE_B13_TPTYPE_UTP | FE_B13_PORT_AUTO;
    919 
    920 	/*
    921 	 * Program the 86960 as follows:
    922 	 *	SRAM: 32KB, 100ns, byte-wide access.
    923 	 *	Transmission buffer: 4KB x 2.
    924 	 *	System bus interface: 16 bits.
    925 	 * We cannot change these values but TXBSIZE, because they
    926 	 * are hard-wired on the board.  Modifying TXBSIZE will affect
    927 	 * the driver performance.
    928 	 */
    929 	sc->proto_dlcr6 = FE_D6_BUFSIZ_32KB | FE_D6_TXBSIZ_2x4KB
    930 		| FE_D6_BBW_BYTE | FE_D6_SBW_WORD | FE_D6_SRAM_100ns;
    931 
    932 	/* Setup hooks.  We need a special initialization procedure. */
    933 	sc->init = fe_init_mbh;
    934 
    935 	/*
    936 	 * Minimum initialization.
    937 	 */
    938 
    939 	/* Wait for a while.  I'm not sure this is necessary.  FIXME. */
    940 	delay(200);
    941 
    942 	/* Minimul initialization of 86960. */
    943 	outb(iobase + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
    944 	delay(200);
    945 
    946 	/* Disable all interrupts. */
    947 	outb(iobase + FE_DLCR2, 0);
    948 	outb(iobase + FE_DLCR3, 0);
    949 
    950 #if 1	/* FIXME. */
    951 	/* Initialize system bus interface and encoder/decoder operation. */
    952 	outb(iobase + FE_MBH0, FE_MBH0_MAGIC | FE_MBH0_INTR_DISABLE);
    953 #endif
    954 
    955 	/*
    956 	 * That's all.  MBH10302 occupies 32 I/O addresses, by the way.
    957 	 */
    958 	ia->ia_iosize = 32;
    959 	ia->ia_msize = 0;
    960 	return (1);
    961 }
    962 
    963 /* MBH specific initialization routine. */
    964 void
    965 fe_init_mbh(sc)
    966 	struct fe_softc *sc;
    967 {
    968 
    969 	/* Probably required after hot-insertion... */
    970 
    971 	/* Wait for a while.  I'm not sure this is necessary.  FIXME. */
    972 	delay(200);
    973 
    974 	/* Minimul initialization of 86960. */
    975 	outb(sc->sc_iobase + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
    976 	delay(200);
    977 
    978 	/* Disable all interrupts. */
    979 	outb(sc->sc_iobase + FE_DLCR2, 0);
    980 	outb(sc->sc_iobase + FE_DLCR3, 0);
    981 
    982 	/* Enable master interrupt flag. */
    983 	outb(sc->sc_iobase + FE_MBH0, FE_MBH0_MAGIC | FE_MBH0_INTR_ENABLE);
    984 }
    985 
    986 /*
    987  * Install interface into kernel networking data structures
    988  */
    989 void
    990 feattach(parent, self, aux)
    991 	struct device *parent, *self;
    992 	void *aux;
    993 {
    994 	struct fe_softc *sc = (void *)self;
    995 	struct isa_attach_args *ia = aux;
    996 	struct cfdata *cf = sc->sc_dev.dv_cfdata;
    997 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    998 
    999 	/* Stop the 86960. */
   1000 	fe_stop(sc);
   1001 
   1002 	/* Initialize ifnet structure. */
   1003 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
   1004 	ifp->if_softc = sc;
   1005 	ifp->if_start = fe_start;
   1006 	ifp->if_ioctl = fe_ioctl;
   1007 	ifp->if_watchdog = fe_watchdog;
   1008 	ifp->if_flags =
   1009 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
   1010 
   1011 	/*
   1012 	 * Set maximum size of output queue, if it has not been set.
   1013 	 * It is done here as this driver may be started after the
   1014 	 * system intialization (i.e., the interface is PCMCIA.)
   1015 	 *
   1016 	 * I'm not sure this is really necessary, but, even if it is,
   1017 	 * it should be done somewhere else, e.g., in if_attach(),
   1018 	 * since it must be a common workaround for all network drivers.
   1019 	 * FIXME.
   1020 	 */
   1021 	if (ifp->if_snd.ifq_maxlen == 0) {
   1022 		extern int ifqmaxlen;		/* Don't be so shocked... */
   1023 		ifp->if_snd.ifq_maxlen = ifqmaxlen;
   1024 	}
   1025 
   1026 #if FE_DEBUG >= 3
   1027 	log(LOG_INFO, "%s: feattach()\n", sc->sc_dev.dv_xname);
   1028 	fe_dump(LOG_INFO, sc);
   1029 #endif
   1030 
   1031 #if FE_SINGLE_TRANSMISSION
   1032 	/* Override txb config to allocate minimum. */
   1033 	sc->proto_dlcr6 &= ~FE_D6_TXBSIZ
   1034 	sc->proto_dlcr6 |=  FE_D6_TXBSIZ_2x2KB;
   1035 #endif
   1036 
   1037 	/* Modify hardware config if it is requested. */
   1038 	if ((cf->cf_flags & FE_FLAGS_OVERRIDE_DLCR6) != 0)
   1039 		sc->proto_dlcr6 = cf->cf_flags & FE_FLAGS_DLCR6_VALUE;
   1040 
   1041 	/* Find TX buffer size, based on the hardware dependent proto. */
   1042 	switch (sc->proto_dlcr6 & FE_D6_TXBSIZ) {
   1043 	case FE_D6_TXBSIZ_2x2KB:
   1044 		sc->txb_size = 2048;
   1045 		break;
   1046 	case FE_D6_TXBSIZ_2x4KB:
   1047 		sc->txb_size = 4096;
   1048 		break;
   1049 	case FE_D6_TXBSIZ_2x8KB:
   1050 		sc->txb_size = 8192;
   1051 		break;
   1052 	default:
   1053 		/* Oops, we can't work with single buffer configuration. */
   1054 #if FE_DEBUG >= 2
   1055 		log(LOG_WARNING, "%s: strange TXBSIZ config; fixing\n",
   1056 		    sc->sc_dev.dv_xname);
   1057 #endif
   1058 		sc->proto_dlcr6 &= ~FE_D6_TXBSIZ;
   1059 		sc->proto_dlcr6 |=  FE_D6_TXBSIZ_2x2KB;
   1060 		sc->txb_size = 2048;
   1061 		break;
   1062 	}
   1063 
   1064 	/* Attach the interface. */
   1065 	if_attach(ifp);
   1066 	ether_ifattach(ifp);
   1067 
   1068 	/* Print additional info when attached. */
   1069 	printf(": address %s, type %s\n",
   1070 	    ether_sprintf(sc->sc_arpcom.ac_enaddr), sc->typestr);
   1071 #if FE_DEBUG >= 3
   1072 	{
   1073 		int buf, txb, bbw, sbw, ram;
   1074 
   1075 		buf = txb = bbw = sbw = ram = -1;
   1076 		switch (sc->proto_dlcr6 & FE_D6_BUFSIZ) {
   1077 		case FE_D6_BUFSIZ_8KB:
   1078 			buf = 8;
   1079 			break;
   1080 		case FE_D6_BUFSIZ_16KB:
   1081 			buf = 16;
   1082 			break;
   1083 		case FE_D6_BUFSIZ_32KB:
   1084 			buf = 32;
   1085 			break;
   1086 		case FE_D6_BUFSIZ_64KB:
   1087 			buf = 64;
   1088 			break;
   1089 		}
   1090 		switch (sc->proto_dlcr6 & FE_D6_TXBSIZ) {
   1091 		case FE_D6_TXBSIZ_2x2KB:
   1092 			txb = 2;
   1093 			break;
   1094 		case FE_D6_TXBSIZ_2x4KB:
   1095 			txb = 4;
   1096 			break;
   1097 		case FE_D6_TXBSIZ_2x8KB:
   1098 			txb = 8;
   1099 			break;
   1100 		}
   1101 		switch (sc->proto_dlcr6 & FE_D6_BBW) {
   1102 		case FE_D6_BBW_BYTE:
   1103 			bbw = 8;
   1104 			break;
   1105 		case FE_D6_BBW_WORD:
   1106 			bbw = 16;
   1107 			break;
   1108 		}
   1109 		switch (sc->proto_dlcr6 & FE_D6_SBW) {
   1110 		case FE_D6_SBW_BYTE:
   1111 			sbw = 8;
   1112 			break;
   1113 		case FE_D6_SBW_WORD:
   1114 			sbw = 16;
   1115 			break;
   1116 		}
   1117 		switch (sc->proto_dlcr6 & FE_D6_SRAM) {
   1118 		case FE_D6_SRAM_100ns:
   1119 			ram = 100;
   1120 			break;
   1121 		case FE_D6_SRAM_150ns:
   1122 			ram = 150;
   1123 			break;
   1124 		}
   1125 		printf("%s: SRAM %dKB %dbit %dns, TXB %dKBx2, %dbit I/O\n",
   1126 		    sc->sc_dev.dv_xname, buf, bbw, ram, txb, sbw);
   1127 	}
   1128 #endif
   1129 
   1130 #if NBPFILTER > 0
   1131 	/* If BPF is in the kernel, call the attach for it. */
   1132 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
   1133 #endif
   1134 
   1135 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
   1136 	    IPL_NET, feintr, sc);
   1137 }
   1138 
   1139 /*
   1140  * Reset interface.
   1141  */
   1142 void
   1143 fe_reset(sc)
   1144 	struct fe_softc *sc;
   1145 {
   1146 	int s;
   1147 
   1148 	s = splnet();
   1149 	fe_stop(sc);
   1150 	fe_init(sc);
   1151 	splx(s);
   1152 }
   1153 
   1154 /*
   1155  * Stop everything on the interface.
   1156  *
   1157  * All buffered packets, both transmitting and receiving,
   1158  * if any, will be lost by stopping the interface.
   1159  */
   1160 void
   1161 fe_stop(sc)
   1162 	struct fe_softc *sc;
   1163 {
   1164 
   1165 #if FE_DEBUG >= 3
   1166 	log(LOG_INFO, "%s: top of fe_stop()\n", sc->sc_dev.dv_xname);
   1167 	fe_dump(LOG_INFO, sc);
   1168 #endif
   1169 
   1170 	/* Disable interrupts. */
   1171 	outb(sc->sc_iobase + FE_DLCR2, 0x00);
   1172 	outb(sc->sc_iobase + FE_DLCR3, 0x00);
   1173 
   1174 	/* Stop interface hardware. */
   1175 	delay(200);
   1176 	outb(sc->sc_iobase + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
   1177 	delay(200);
   1178 
   1179 	/* Clear all interrupt status. */
   1180 	outb(sc->sc_iobase + FE_DLCR0, 0xFF);
   1181 	outb(sc->sc_iobase + FE_DLCR1, 0xFF);
   1182 
   1183 	/* Put the chip in stand-by mode. */
   1184 	delay(200);
   1185 	outb(sc->sc_iobase + FE_DLCR7, sc->proto_dlcr7 | FE_D7_POWER_DOWN);
   1186 	delay(200);
   1187 
   1188 	/* MAR loading can be delayed. */
   1189 	sc->filter_change = 0;
   1190 
   1191 	/* Call a hook. */
   1192 	if (sc->stop)
   1193 		sc->stop(sc);
   1194 
   1195 #if DEBUG >= 3
   1196 	log(LOG_INFO, "%s: end of fe_stop()\n", sc->sc_dev.dv_xname);
   1197 	fe_dump(LOG_INFO, sc);
   1198 #endif
   1199 }
   1200 
   1201 /*
   1202  * Device timeout/watchdog routine. Entered if the device neglects to
   1203  * generate an interrupt after a transmit has been started on it.
   1204  */
   1205 void
   1206 fe_watchdog(ifp)
   1207 	struct ifnet *ifp;
   1208 {
   1209 	struct fe_softc *sc = ifp->if_softc;
   1210 
   1211 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
   1212 #if FE_DEBUG >= 3
   1213 	fe_dump(LOG_INFO, sc);
   1214 #endif
   1215 
   1216 	/* Record how many packets are lost by this accident. */
   1217 	sc->sc_arpcom.ac_if.if_oerrors += sc->txb_sched + sc->txb_count;
   1218 
   1219 	fe_reset(sc);
   1220 }
   1221 
   1222 /*
   1223  * Drop (skip) a packet from receive buffer in 86960 memory.
   1224  */
   1225 static inline void
   1226 fe_droppacket(sc)
   1227 	struct fe_softc *sc;
   1228 {
   1229 
   1230 	outb(sc->sc_iobase + FE_BMPR14, FE_B14_FILTER | FE_B14_SKIP);
   1231 }
   1232 
   1233 /*
   1234  * Initialize device.
   1235  */
   1236 void
   1237 fe_init(sc)
   1238 	struct fe_softc *sc;
   1239 {
   1240 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
   1241 	int i;
   1242 
   1243 #if FE_DEBUG >= 3
   1244 	log(LOG_INFO, "%s: top of fe_init()\n", sc->sc_dev.dv_xname);
   1245 	fe_dump(LOG_INFO, sc);
   1246 #endif
   1247 
   1248 	/* Reset transmitter flags. */
   1249 	ifp->if_flags &= ~IFF_OACTIVE;
   1250 	ifp->if_timer = 0;
   1251 
   1252 	sc->txb_free = sc->txb_size;
   1253 	sc->txb_count = 0;
   1254 	sc->txb_sched = 0;
   1255 
   1256 	/* Call a hook. */
   1257 	if (sc->init)
   1258 		sc->init(sc);
   1259 
   1260 #if FE_DEBUG >= 3
   1261 	log(LOG_INFO, "%s: after init hook\n", sc->sc_dev.dv_xname);
   1262 	fe_dump(LOG_INFO, sc);
   1263 #endif
   1264 
   1265 	/*
   1266 	 * Make sure to disable the chip, also.
   1267 	 * This may also help re-programming the chip after
   1268 	 * hot insertion of PCMCIAs.
   1269 	 */
   1270 	outb(sc->sc_iobase + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
   1271 
   1272 	/* Power up the chip and select register bank for DLCRs. */
   1273 	delay(200);
   1274 	outb(sc->sc_iobase + FE_DLCR7,
   1275 	    sc->proto_dlcr7 | FE_D7_RBS_DLCR | FE_D7_POWER_UP);
   1276 	delay(200);
   1277 
   1278 	/* Feed the station address. */
   1279 	outblk(sc->sc_iobase + FE_DLCR8, sc->sc_enaddr, ETHER_ADDR_LEN);
   1280 
   1281 	/* Select the BMPR bank for runtime register access. */
   1282 	outb(sc->sc_iobase + FE_DLCR7,
   1283 	    sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP);
   1284 
   1285 	/* Initialize registers. */
   1286 	outb(sc->sc_iobase + FE_DLCR0, 0xFF);	/* Clear all bits. */
   1287 	outb(sc->sc_iobase + FE_DLCR1, 0xFF);	/* ditto. */
   1288 	outb(sc->sc_iobase + FE_DLCR2, 0x00);
   1289 	outb(sc->sc_iobase + FE_DLCR3, 0x00);
   1290 	outb(sc->sc_iobase + FE_DLCR4, sc->proto_dlcr4);
   1291 	outb(sc->sc_iobase + FE_DLCR5, sc->proto_dlcr5);
   1292 	outb(sc->sc_iobase + FE_BMPR10, 0x00);
   1293 	outb(sc->sc_iobase + FE_BMPR11, FE_B11_CTRL_SKIP);
   1294 	outb(sc->sc_iobase + FE_BMPR12, 0x00);
   1295 	outb(sc->sc_iobase + FE_BMPR13, sc->proto_bmpr13);
   1296 	outb(sc->sc_iobase + FE_BMPR14, FE_B14_FILTER);
   1297 	outb(sc->sc_iobase + FE_BMPR15, 0x00);
   1298 
   1299 #if FE_DEBUG >= 3
   1300 	log(LOG_INFO, "%s: just before enabling DLC\n", sc->sc_dev.dv_xname);
   1301 	fe_dump(LOG_INFO, sc);
   1302 #endif
   1303 
   1304 	/* Enable interrupts. */
   1305 	outb(sc->sc_iobase + FE_DLCR2, FE_TMASK);
   1306 	outb(sc->sc_iobase + FE_DLCR3, FE_RMASK);
   1307 
   1308 	/* Enable transmitter and receiver. */
   1309 	delay(200);
   1310 	outb(sc->sc_iobase + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_ENABLE);
   1311 	delay(200);
   1312 
   1313 #if FE_DEBUG >= 3
   1314 	log(LOG_INFO, "%s: just after enabling DLC\n", sc->sc_dev.dv_xname);
   1315 	fe_dump(LOG_INFO, sc);
   1316 #endif
   1317 
   1318 	/*
   1319 	 * Make sure to empty the receive buffer.
   1320 	 *
   1321 	 * This may be redundant, but *if* the receive buffer were full
   1322 	 * at this point, the driver would hang.  I have experienced
   1323 	 * some strange hangups just after UP.  I hope the following
   1324 	 * code solve the problem.
   1325 	 *
   1326 	 * I have changed the order of hardware initialization.
   1327 	 * I think the receive buffer cannot have any packets at this
   1328 	 * point in this version.  The following code *must* be
   1329 	 * redundant now.  FIXME.
   1330 	 */
   1331 	for (i = 0; i < FE_MAX_RECV_COUNT; i++) {
   1332 		if (inb(sc->sc_iobase + FE_DLCR5) & FE_D5_BUFEMP)
   1333 			break;
   1334 		fe_droppacket(sc);
   1335 	}
   1336 #if FE_DEBUG >= 1
   1337 	if (i >= FE_MAX_RECV_COUNT) {
   1338 		log(LOG_ERR, "%s: cannot empty receive buffer\n",
   1339 		    sc->sc_dev.dv_xname);
   1340 	}
   1341 #endif
   1342 #if FE_DEBUG >= 3
   1343 	if (i < FE_MAX_RECV_COUNT) {
   1344 		log(LOG_INFO, "%s: receive buffer emptied (%d)\n",
   1345 		    sc->sc_dev.dv_xname, i);
   1346 	}
   1347 #endif
   1348 
   1349 #if FE_DEBUG >= 3
   1350 	log(LOG_INFO, "%s: after ERB loop\n", sc->sc_dev.dv_xname);
   1351 	fe_dump(LOG_INFO, sc);
   1352 #endif
   1353 
   1354 	/* Do we need this here? */
   1355 	outb(sc->sc_iobase + FE_DLCR0, 0xFF);	/* Clear all bits. */
   1356 	outb(sc->sc_iobase + FE_DLCR1, 0xFF);	/* ditto. */
   1357 
   1358 #if FE_DEBUG >= 3
   1359 	log(LOG_INFO, "%s: after FIXME\n", sc->sc_dev.dv_xname);
   1360 	fe_dump(LOG_INFO, sc);
   1361 #endif
   1362 
   1363 	/* Set 'running' flag. */
   1364 	ifp->if_flags |= IFF_RUNNING;
   1365 
   1366 	/*
   1367 	 * At this point, the interface is runnung properly,
   1368 	 * except that it receives *no* packets.  we then call
   1369 	 * fe_setmode() to tell the chip what packets to be
   1370 	 * received, based on the if_flags and multicast group
   1371 	 * list.  It completes the initialization process.
   1372 	 */
   1373 	fe_setmode(sc);
   1374 
   1375 #if FE_DEBUG >= 3
   1376 	log(LOG_INFO, "%s: after setmode\n", sc->sc_dev.dv_xname);
   1377 	fe_dump(LOG_INFO, sc);
   1378 #endif
   1379 
   1380 	/* ...and attempt to start output. */
   1381 	fe_start(ifp);
   1382 
   1383 #if FE_DEBUG >= 3
   1384 	log(LOG_INFO, "%s: end of fe_init()\n", sc->sc_dev.dv_xname);
   1385 	fe_dump(LOG_INFO, sc);
   1386 #endif
   1387 }
   1388 
   1389 /*
   1390  * This routine actually starts the transmission on the interface
   1391  */
   1392 static inline void
   1393 fe_xmit(sc)
   1394 	struct fe_softc *sc;
   1395 {
   1396 
   1397 	/*
   1398 	 * Set a timer just in case we never hear from the board again.
   1399 	 * We use longer timeout for multiple packet transmission.
   1400 	 * I'm not sure this timer value is appropriate.  FIXME.
   1401 	 */
   1402 	sc->sc_arpcom.ac_if.if_timer = 1 + sc->txb_count;
   1403 
   1404 	/* Update txb variables. */
   1405 	sc->txb_sched = sc->txb_count;
   1406 	sc->txb_count = 0;
   1407 	sc->txb_free = sc->txb_size;
   1408 
   1409 #if FE_DELAYED_PADDING
   1410 	/* Omit the postponed padding process. */
   1411 	sc->txb_padding = 0;
   1412 #endif
   1413 
   1414 	/* Start transmitter, passing packets in TX buffer. */
   1415 	outb(sc->sc_iobase + FE_BMPR10, sc->txb_sched | FE_B10_START);
   1416 }
   1417 
   1418 /*
   1419  * Start output on interface.
   1420  * We make two assumptions here:
   1421  *  1) that the current priority is set to splnet _before_ this code
   1422  *     is called *and* is returned to the appropriate priority after
   1423  *     return
   1424  *  2) that the IFF_OACTIVE flag is checked before this code is called
   1425  *     (i.e. that the output part of the interface is idle)
   1426  */
   1427 void
   1428 fe_start(ifp)
   1429 	struct ifnet *ifp;
   1430 {
   1431 	struct fe_softc *sc = ifp->if_softc;
   1432 	struct mbuf *m;
   1433 
   1434 #if FE_DEBUG >= 1
   1435 	/* Just a sanity check. */
   1436 	if ((sc->txb_count == 0) != (sc->txb_free == sc->txb_size)) {
   1437 		/*
   1438 		 * Txb_count and txb_free co-works to manage the
   1439 		 * transmission buffer.  Txb_count keeps track of the
   1440 		 * used potion of the buffer, while txb_free does unused
   1441 		 * potion.  So, as long as the driver runs properly,
   1442 		 * txb_count is zero if and only if txb_free is same
   1443 		 * as txb_size (which represents whole buffer.)
   1444 		 */
   1445 		log(LOG_ERR, "%s: inconsistent txb variables (%d, %d)\n",
   1446 		    sc->sc_dev.dv_xname, sc->txb_count, sc->txb_free);
   1447 		/*
   1448 		 * So, what should I do, then?
   1449 		 *
   1450 		 * We now know txb_count and txb_free contradicts.  We
   1451 		 * cannot, however, tell which is wrong.  More
   1452 		 * over, we cannot peek 86960 transmission buffer or
   1453 		 * reset the transmission buffer.  (In fact, we can
   1454 		 * reset the entire interface.  I don't want to do it.)
   1455 		 *
   1456 		 * If txb_count is incorrect, leaving it as is will cause
   1457 		 * sending of gabages after next interrupt.  We have to
   1458 		 * avoid it.  Hence, we reset the txb_count here.  If
   1459 		 * txb_free was incorrect, resetting txb_count just loose
   1460 		 * some packets.  We can live with it.
   1461 		 */
   1462 		sc->txb_count = 0;
   1463 	}
   1464 #endif
   1465 
   1466 #if FE_DEBUG >= 1
   1467 	/*
   1468 	 * First, see if there are buffered packets and an idle
   1469 	 * transmitter - should never happen at this point.
   1470 	 */
   1471 	if ((sc->txb_count > 0) && (sc->txb_sched == 0)) {
   1472 		log(LOG_ERR, "%s: transmitter idle with %d buffered packets\n",
   1473 		    sc->sc_dev.dv_xname, sc->txb_count);
   1474 		fe_xmit(sc);
   1475 	}
   1476 #endif
   1477 
   1478 	/*
   1479 	 * Stop accepting more transmission packets temporarily, when
   1480 	 * a filter change request is delayed.  Updating the MARs on
   1481 	 * 86960 flushes the transmisstion buffer, so it is delayed
   1482 	 * until all buffered transmission packets have been sent
   1483 	 * out.
   1484 	 */
   1485 	if (sc->filter_change) {
   1486 		/*
   1487 		 * Filter change requst is delayed only when the DLC is
   1488 		 * working.  DLC soon raise an interrupt after finishing
   1489 		 * the work.
   1490 		 */
   1491 		goto indicate_active;
   1492 	}
   1493 
   1494 	for (;;) {
   1495 		/*
   1496 		 * See if there is room to put another packet in the buffer.
   1497 		 * We *could* do better job by peeking the send queue to
   1498 		 * know the length of the next packet.  Current version just
   1499 		 * tests against the worst case (i.e., longest packet).  FIXME.
   1500 		 *
   1501 		 * When adding the packet-peek feature, don't forget adding a
   1502 		 * test on txb_count against QUEUEING_MAX.
   1503 		 * There is a little chance the packet count exceeds
   1504 		 * the limit.  Assume transmission buffer is 8KB (2x8KB
   1505 		 * configuration) and an application sends a bunch of small
   1506 		 * (i.e., minimum packet sized) packets rapidly.  An 8KB
   1507 		 * buffer can hold 130 blocks of 62 bytes long...
   1508 		 */
   1509 		if (sc->txb_free < ETHER_MAX_LEN + FE_DATA_LEN_LEN) {
   1510 			/* No room. */
   1511 			goto indicate_active;
   1512 		}
   1513 
   1514 #if FE_SINGLE_TRANSMISSION
   1515 		if (sc->txb_count > 0) {
   1516 			/* Just one packet per a transmission buffer. */
   1517 			goto indicate_active;
   1518 		}
   1519 #endif
   1520 
   1521 		/*
   1522 		 * Get the next mbuf chain for a packet to send.
   1523 		 */
   1524 		IF_DEQUEUE(&ifp->if_snd, m);
   1525 		if (m == 0) {
   1526 			/* No more packets to send. */
   1527 			goto indicate_inactive;
   1528 		}
   1529 
   1530 #if NBPFILTER > 0
   1531 		/* Tap off here if there is a BPF listener. */
   1532 		if (ifp->if_bpf)
   1533 			bpf_mtap(ifp->if_bpf, m);
   1534 #endif
   1535 
   1536 		/*
   1537 		 * Copy the mbuf chain into the transmission buffer.
   1538 		 * txb_* variables are updated as necessary.
   1539 		 */
   1540 		fe_write_mbufs(sc, m);
   1541 
   1542 		m_freem(m);
   1543 
   1544 		/* Start transmitter if it's idle. */
   1545 		if (sc->txb_sched == 0)
   1546 			fe_xmit(sc);
   1547 	}
   1548 
   1549 indicate_inactive:
   1550 	/*
   1551 	 * We are using the !OACTIVE flag to indicate to
   1552 	 * the outside world that we can accept an
   1553 	 * additional packet rather than that the
   1554 	 * transmitter is _actually_ active.  Indeed, the
   1555 	 * transmitter may be active, but if we haven't
   1556 	 * filled all the buffers with data then we still
   1557 	 * want to accept more.
   1558 	 */
   1559 	ifp->if_flags &= ~IFF_OACTIVE;
   1560 	return;
   1561 
   1562 indicate_active:
   1563 	/*
   1564 	 * The transmitter is active, and there are no room for
   1565 	 * more outgoing packets in the transmission buffer.
   1566 	 */
   1567 	ifp->if_flags |= IFF_OACTIVE;
   1568 	return;
   1569 }
   1570 
   1571 /*
   1572  * Transmission interrupt handler
   1573  * The control flow of this function looks silly.  FIXME.
   1574  */
   1575 void
   1576 fe_tint(sc, tstat)
   1577 	struct fe_softc *sc;
   1578 	u_char tstat;
   1579 {
   1580 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
   1581 	int left;
   1582 	int col;
   1583 
   1584 	/*
   1585 	 * Handle "excessive collision" interrupt.
   1586 	 */
   1587 	if (tstat & FE_D0_COLL16) {
   1588 		/*
   1589 		 * Find how many packets (including this collided one)
   1590 		 * are left unsent in transmission buffer.
   1591 		 */
   1592 		left = inb(sc->sc_iobase + FE_BMPR10);
   1593 
   1594 #if FE_DEBUG >= 2
   1595 		log(LOG_WARNING, "%s: excessive collision (%d/%d)\n",
   1596 		    sc->sc_dev.dv_xname, left, sc->txb_sched);
   1597 #endif
   1598 #if FE_DEBUG >= 3
   1599 		fe_dump(LOG_INFO, sc);
   1600 #endif
   1601 
   1602 		/*
   1603 		 * Update statistics.
   1604 		 */
   1605 		ifp->if_collisions += 16;
   1606 		ifp->if_oerrors++;
   1607 		ifp->if_opackets += sc->txb_sched - left;
   1608 
   1609 		/*
   1610 		 * Collision statistics has been updated.
   1611 		 * Clear the collision flag on 86960 now to avoid confusion.
   1612 		 */
   1613 		outb(sc->sc_iobase + FE_DLCR0, FE_D0_COLLID);
   1614 
   1615 		/*
   1616 		 * Restart transmitter, skipping the
   1617 		 * collided packet.
   1618 		 *
   1619 		 * We *must* skip the packet to keep network running
   1620 		 * properly.  Excessive collision error is an
   1621 		 * indication of the network overload.  If we
   1622 		 * tried sending the same packet after excessive
   1623 		 * collision, the network would be filled with
   1624 		 * out-of-time packets.  Packets belonging
   1625 		 * to reliable transport (such as TCP) are resent
   1626 		 * by some upper layer.
   1627 		 */
   1628 		outb(sc->sc_iobase + FE_BMPR11,
   1629 		    FE_B11_CTRL_SKIP | FE_B11_MODE1);
   1630 		sc->txb_sched = left - 1;
   1631 	}
   1632 
   1633 	/*
   1634 	 * Handle "transmission complete" interrupt.
   1635 	 */
   1636 	if (tstat & FE_D0_TXDONE) {
   1637 		/*
   1638 		 * Add in total number of collisions on last
   1639 		 * transmission.  We also clear "collision occurred" flag
   1640 		 * here.
   1641 		 *
   1642 		 * 86960 has a design flow on collision count on multiple
   1643 		 * packet transmission.  When we send two or more packets
   1644 		 * with one start command (that's what we do when the
   1645 		 * transmission queue is clauded), 86960 informs us number
   1646 		 * of collisions occured on the last packet on the
   1647 		 * transmission only.  Number of collisions on previous
   1648 		 * packets are lost.  I have told that the fact is clearly
   1649 		 * stated in the Fujitsu document.
   1650 		 *
   1651 		 * I considered not to mind it seriously.  Collision
   1652 		 * count is not so important, anyway.  Any comments?  FIXME.
   1653 		 */
   1654 
   1655 		if (inb(sc->sc_iobase + FE_DLCR0) & FE_D0_COLLID) {
   1656 			/* Clear collision flag. */
   1657 			outb(sc->sc_iobase + FE_DLCR0, FE_D0_COLLID);
   1658 
   1659 			/* Extract collision count from 86960. */
   1660 			col = inb(sc->sc_iobase + FE_DLCR4) & FE_D4_COL;
   1661 			if (col == 0) {
   1662 				/*
   1663 				 * Status register indicates collisions,
   1664 				 * while the collision count is zero.
   1665 				 * This can happen after multiple packet
   1666 				 * transmission, indicating that one or more
   1667 				 * previous packet(s) had been collided.
   1668 				 *
   1669 				 * Since the accurate number of collisions
   1670 				 * has been lost, we just guess it as 1;
   1671 				 * Am I too optimistic?  FIXME.
   1672 				 */
   1673 				col = 1;
   1674 			} else
   1675 				col >>= FE_D4_COL_SHIFT;
   1676 			ifp->if_collisions += col;
   1677 #if FE_DEBUG >= 4
   1678 			log(LOG_WARNING, "%s: %d collision%s (%d)\n",
   1679 			    sc->sc_dev.dv_xname, col, col == 1 ? "" : "s",
   1680 			    sc->txb_sched);
   1681 #endif
   1682 		}
   1683 
   1684 		/*
   1685 		 * Update total number of successfully
   1686 		 * transmitted packets.
   1687 		 */
   1688 		ifp->if_opackets += sc->txb_sched;
   1689 		sc->txb_sched = 0;
   1690 	}
   1691 
   1692 	if (sc->txb_sched == 0) {
   1693 		/*
   1694 		 * The transmitter is no more active.
   1695 		 * Reset output active flag and watchdog timer.
   1696 		 */
   1697 		ifp->if_flags &= ~IFF_OACTIVE;
   1698 		ifp->if_timer = 0;
   1699 
   1700 		/*
   1701 		 * If more data is ready to transmit in the buffer, start
   1702 		 * transmitting them.  Otherwise keep transmitter idle,
   1703 		 * even if more data is queued.  This gives receive
   1704 		 * process a slight priority.
   1705 		 */
   1706 		if (sc->txb_count > 0)
   1707 			fe_xmit(sc);
   1708 	}
   1709 }
   1710 
   1711 /*
   1712  * Ethernet interface receiver interrupt.
   1713  */
   1714 void
   1715 fe_rint(sc, rstat)
   1716 	struct fe_softc *sc;
   1717 	u_char rstat;
   1718 {
   1719 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
   1720 	int len;
   1721 	u_char status;
   1722 	int i;
   1723 
   1724 	/*
   1725 	 * Update statistics if this interrupt is caused by an error.
   1726 	 */
   1727 	if (rstat & (FE_D1_OVRFLO | FE_D1_CRCERR |
   1728 		     FE_D1_ALGERR | FE_D1_SRTPKT)) {
   1729 #if FE_DEBUG >= 3
   1730 		log(LOG_WARNING, "%s: receive error: %b\n",
   1731 		    sc->sc_dev.dv_xname, rstat, FE_D1_ERRBITS);
   1732 #endif
   1733 		ifp->if_ierrors++;
   1734 	}
   1735 
   1736 	/*
   1737 	 * MB86960 has a flag indicating "receive queue empty."
   1738 	 * We just loop cheking the flag to pull out all received
   1739 	 * packets.
   1740 	 *
   1741 	 * We limit the number of iterrations to avoid infinite loop.
   1742 	 * It can be caused by a very slow CPU (some broken
   1743 	 * peripheral may insert incredible number of wait cycles)
   1744 	 * or, worse, by a broken MB86960 chip.
   1745 	 */
   1746 	for (i = 0; i < FE_MAX_RECV_COUNT; i++) {
   1747 		/* Stop the iterration if 86960 indicates no packets. */
   1748 		if (inb(sc->sc_iobase + FE_DLCR5) & FE_D5_BUFEMP)
   1749 			break;
   1750 
   1751 		/*
   1752 		 * Extract A receive status byte.
   1753 		 * As our 86960 is in 16 bit bus access mode, we have to
   1754 		 * use inw() to get the status byte.  The significant
   1755 		 * value is returned in lower 8 bits.
   1756 		 */
   1757 		status = (u_char)inw(sc->sc_iobase + FE_BMPR8);
   1758 #if FE_DEBUG >= 4
   1759 		log(LOG_INFO, "%s: receive status = %02x\n",
   1760 		    sc->sc_dev.dv_xname, status);
   1761 #endif
   1762 
   1763 		/*
   1764 		 * If there was an error, update statistics and drop
   1765 		 * the packet, unless the interface is in promiscuous
   1766 		 * mode.
   1767 		 */
   1768 		if ((status & 0xF0) != 0x20) {	/* XXXX ? */
   1769 			if ((ifp->if_flags & IFF_PROMISC) == 0) {
   1770 				ifp->if_ierrors++;
   1771 				fe_droppacket(sc);
   1772 				continue;
   1773 			}
   1774 		}
   1775 
   1776 		/*
   1777 		 * Extract the packet length.
   1778 		 * It is a sum of a header (14 bytes) and a payload.
   1779 		 * CRC has been stripped off by the 86960.
   1780 		 */
   1781 		len = inw(sc->sc_iobase + FE_BMPR8);
   1782 
   1783 		/*
   1784 		 * MB86965 checks the packet length and drop big packet
   1785 		 * before passing it to us.  There are no chance we can
   1786 		 * get [crufty] packets.  Hence, if the length exceeds
   1787 		 * the specified limit, it means some serious failure,
   1788 		 * such as out-of-sync on receive buffer management.
   1789 		 *
   1790 		 * Is this statement true?  FIXME.
   1791 		 */
   1792 		if (len > ETHER_MAX_LEN || len < ETHER_HDR_SIZE) {
   1793 #if FE_DEBUG >= 2
   1794 			log(LOG_WARNING,
   1795 			    "%s: received a %s packet? (%u bytes)\n",
   1796 			    sc->sc_dev.dv_xname,
   1797 			    len < ETHER_HDR_SIZE ? "partial" : "big", len);
   1798 #endif
   1799 			ifp->if_ierrors++;
   1800 			fe_droppacket(sc);
   1801 			continue;
   1802 		}
   1803 
   1804 		/*
   1805 		 * Check for a short (RUNT) packet.  We *do* check
   1806 		 * but do nothing other than print a message.
   1807 		 * Short packets are illegal, but does nothing bad
   1808 		 * if it carries data for upper layer.
   1809 		 */
   1810 #if FE_DEBUG >= 2
   1811 		if (len < ETHER_MIN_LEN) {
   1812 			log(LOG_WARNING,
   1813 			     "%s: received a short packet? (%u bytes)\n",
   1814 			     sc->sc_dev.dv_xname, len);
   1815 		}
   1816 #endif
   1817 
   1818 		/*
   1819 		 * Go get a packet.
   1820 		 */
   1821 		if (!fe_get_packet(sc, len)) {
   1822 			/* Skip a packet, updating statistics. */
   1823 #if FE_DEBUG >= 2
   1824 			log(LOG_WARNING,
   1825 			    "%s: out of mbufs; dropping packet (%u bytes)\n",
   1826 			    sc->sc_dev.dv_xname, len);
   1827 #endif
   1828 			ifp->if_ierrors++;
   1829 			fe_droppacket(sc);
   1830 
   1831 			/*
   1832 			 * We stop receiving packets, even if there are
   1833 			 * more in the buffer.  We hope we can get more
   1834 			 * mbufs next time.
   1835 			 */
   1836 			return;
   1837 		}
   1838 
   1839 		/* Successfully received a packet.  Update stat. */
   1840 		ifp->if_ipackets++;
   1841 	}
   1842 }
   1843 
   1844 /*
   1845  * Ethernet interface interrupt processor
   1846  */
   1847 int
   1848 feintr(arg)
   1849 	void *arg;
   1850 {
   1851 	struct fe_softc *sc = arg;
   1852 	u_char tstat, rstat;
   1853 
   1854 #if FE_DEBUG >= 4
   1855 	log(LOG_INFO, "%s: feintr()\n", sc->sc_dev.dv_xname);
   1856 	fe_dump(LOG_INFO, sc);
   1857 #endif
   1858 
   1859 	/*
   1860 	 * Get interrupt conditions, masking unneeded flags.
   1861 	 */
   1862 	tstat = inb(sc->sc_iobase + FE_DLCR0) & FE_TMASK;
   1863 	rstat = inb(sc->sc_iobase + FE_DLCR1) & FE_RMASK;
   1864 	if (tstat == 0 && rstat == 0)
   1865 		return (0);
   1866 
   1867 	/*
   1868 	 * Loop until there are no more new interrupt conditions.
   1869 	 */
   1870 	for (;;) {
   1871 		/*
   1872 		 * Reset the conditions we are acknowledging.
   1873 		 */
   1874 		outb(sc->sc_iobase + FE_DLCR0, tstat);
   1875 		outb(sc->sc_iobase + FE_DLCR1, rstat);
   1876 
   1877 		/*
   1878 		 * Handle transmitter interrupts. Handle these first because
   1879 		 * the receiver will reset the board under some conditions.
   1880 		 */
   1881 		if (tstat != 0)
   1882 			fe_tint(sc, tstat);
   1883 
   1884 		/*
   1885 		 * Handle receiver interrupts.
   1886 		 */
   1887 		if (rstat != 0)
   1888 			fe_rint(sc, rstat);
   1889 
   1890 		/*
   1891 		 * Update the multicast address filter if it is
   1892 		 * needed and possible.  We do it now, because
   1893 		 * we can make sure the transmission buffer is empty,
   1894 		 * and there is a good chance that the receive queue
   1895 		 * is empty.  It will minimize the possibility of
   1896 		 * packet lossage.
   1897 		 */
   1898 		if (sc->filter_change &&
   1899 		    sc->txb_count == 0 && sc->txb_sched == 0) {
   1900 			fe_loadmar(sc);
   1901 			sc->sc_arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
   1902 		}
   1903 
   1904 		/*
   1905 		 * If it looks like the transmitter can take more data,
   1906 		 * attempt to start output on the interface. This is done
   1907 		 * after handling the receiver interrupt to give the
   1908 		 * receive operation priority.
   1909 		 */
   1910 		if ((sc->sc_arpcom.ac_if.if_flags & IFF_OACTIVE) == 0)
   1911 			fe_start(&sc->sc_arpcom.ac_if);
   1912 
   1913 		/*
   1914 		 * Get interrupt conditions, masking unneeded flags.
   1915 		 */
   1916 		tstat = inb(sc->sc_iobase + FE_DLCR0) & FE_TMASK;
   1917 		rstat = inb(sc->sc_iobase + FE_DLCR1) & FE_RMASK;
   1918 		if (tstat == 0 && rstat == 0)
   1919 			return (1);
   1920 	}
   1921 }
   1922 
   1923 /*
   1924  * Process an ioctl request.  This code needs some work - it looks pretty ugly.
   1925  */
   1926 int
   1927 fe_ioctl(ifp, command, data)
   1928 	register struct ifnet *ifp;
   1929 	u_long command;
   1930 	caddr_t data;
   1931 {
   1932 	struct fe_softc *sc = ifp->if_softc;
   1933 	register struct ifaddr *ifa = (struct ifaddr *)data;
   1934 	struct ifreq *ifr = (struct ifreq *)data;
   1935 	int s, error = 0;
   1936 
   1937 #if FE_DEBUG >= 3
   1938 	log(LOG_INFO, "%s: ioctl(%x)\n", sc->sc_dev.dv_xname, command);
   1939 #endif
   1940 
   1941 	s = splnet();
   1942 
   1943 	switch (command) {
   1944 
   1945 	case SIOCSIFADDR:
   1946 		ifp->if_flags |= IFF_UP;
   1947 
   1948 		switch (ifa->ifa_addr->sa_family) {
   1949 #ifdef INET
   1950 		case AF_INET:
   1951 			fe_init(sc);
   1952 			arp_ifinit(&sc->sc_arpcom, ifa);
   1953 			break;
   1954 #endif
   1955 #ifdef NS
   1956 		case AF_NS:
   1957 		    {
   1958 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1959 
   1960 			if (ns_nullhost(*ina))
   1961 				ina->x_host =
   1962 				    *(union ns_host *)(sc->sc_arpcom.ac_enaddr);
   1963 			else
   1964 				bcopy(ina->x_host.c_host,
   1965 				    sc->sc_arpcom.ac_enaddr,
   1966 				    sizeof(sc->sc_arpcom.ac_enaddr));
   1967 			/* Set new address. */
   1968 			fe_init(sc);
   1969 			break;
   1970 		    }
   1971 #endif
   1972 		default:
   1973 			fe_init(sc);
   1974 			break;
   1975 		}
   1976 		break;
   1977 
   1978 	case SIOCSIFFLAGS:
   1979 		if ((ifp->if_flags & IFF_UP) == 0 &&
   1980 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   1981 			/*
   1982 			 * If interface is marked down and it is running, then
   1983 			 * stop it.
   1984 			 */
   1985 			fe_stop(sc);
   1986 			ifp->if_flags &= ~IFF_RUNNING;
   1987 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
   1988 			   (ifp->if_flags & IFF_RUNNING) == 0) {
   1989 			/*
   1990 			 * If interface is marked up and it is stopped, then
   1991 			 * start it.
   1992 			 */
   1993 			fe_init(sc);
   1994 		} else {
   1995 			/*
   1996 			 * Reset the interface to pick up changes in any other
   1997 			 * flags that affect hardware registers.
   1998 			 */
   1999 			fe_setmode(sc);
   2000 		}
   2001 #if DEBUG >= 1
   2002 		/* "ifconfig fe0 debug" to print register dump. */
   2003 		if (ifp->if_flags & IFF_DEBUG) {
   2004 			log(LOG_INFO, "%s: SIOCSIFFLAGS(DEBUG)\n", sc->sc_dev.dv_xname);
   2005 			fe_dump(LOG_DEBUG, sc);
   2006 		}
   2007 #endif
   2008 		break;
   2009 
   2010 	case SIOCADDMULTI:
   2011 	case SIOCDELMULTI:
   2012 		/* Update our multicast list. */
   2013 		error = (command == SIOCADDMULTI) ?
   2014 		    ether_addmulti(ifr, &sc->sc_arpcom) :
   2015 		    ether_delmulti(ifr, &sc->sc_arpcom);
   2016 
   2017 		if (error == ENETRESET) {
   2018 			/*
   2019 			 * Multicast list has changed; set the hardware filter
   2020 			 * accordingly.
   2021 			 */
   2022 			fe_setmode(sc);
   2023 			error = 0;
   2024 		}
   2025 		break;
   2026 
   2027 	default:
   2028 		error = EINVAL;
   2029 	}
   2030 
   2031 	splx(s);
   2032 	return (error);
   2033 }
   2034 
   2035 /*
   2036  * Retreive packet from receive buffer and send to the next level up via
   2037  * ether_input(). If there is a BPF listener, give a copy to BPF, too.
   2038  * Returns 0 if success, -1 if error (i.e., mbuf allocation failure).
   2039  */
   2040 int
   2041 fe_get_packet(sc, len)
   2042 	struct fe_softc *sc;
   2043 	int len;
   2044 {
   2045 	struct ether_header *eh;
   2046 	struct mbuf *m;
   2047 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
   2048 
   2049 	/* Allocate a header mbuf. */
   2050 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   2051 	if (m == 0)
   2052 		return (0);
   2053 	m->m_pkthdr.rcvif = ifp;
   2054 	m->m_pkthdr.len = len;
   2055 
   2056 	/* The following silliness is to make NFS happy. */
   2057 #define	EROUND	((sizeof(struct ether_header) + 3) & ~3)
   2058 #define	EOFF	(EROUND - sizeof(struct ether_header))
   2059 
   2060 	/*
   2061 	 * Our strategy has one more problem.  There is a policy on
   2062 	 * mbuf cluster allocation.  It says that we must have at
   2063 	 * least MINCLSIZE (208 bytes) to allocate a cluster.  For a
   2064 	 * packet of a size between (MHLEN - 2) to (MINCLSIZE - 2),
   2065 	 * our code violates the rule...
   2066 	 * On the other hand, the current code is short, simle,
   2067 	 * and fast, however.  It does no harmful thing, just waists
   2068 	 * some memory.  Any comments?  FIXME.
   2069 	 */
   2070 
   2071 	/* Attach a cluster if this packet doesn't fit in a normal mbuf. */
   2072 	if (len > MHLEN - EOFF) {
   2073 		MCLGET(m, M_DONTWAIT);
   2074 		if ((m->m_flags & M_EXT) == 0) {
   2075 			m_freem(m);
   2076 			return (0);
   2077 		}
   2078 	}
   2079 
   2080 	/*
   2081 	 * The following assumes there is room for the ether header in the
   2082 	 * header mbuf.
   2083 	 */
   2084 	m->m_data += EOFF;
   2085 	eh = mtod(m, struct ether_header *);
   2086 
   2087 	/* Set the length of this packet. */
   2088 	m->m_len = len;
   2089 
   2090 	/* Get a packet. */
   2091 	insw(sc->sc_iobase + FE_BMPR8, m->m_data, (len + 1) >> 1);
   2092 
   2093 #if NBPFILTER > 0
   2094 	/*
   2095 	 * Check if there's a BPF listener on this interface.  If so, hand off
   2096 	 * the raw packet to bpf.
   2097 	 */
   2098 	if (ifp->if_bpf) {
   2099 		bpf_mtap(ifp->if_bpf, m);
   2100 
   2101 		/*
   2102 		 * Note that the interface cannot be in promiscuous mode if
   2103 		 * there are no BPF listeners.  And if we are in promiscuous
   2104 		 * mode, we have to check if this packet is really ours.
   2105 		 */
   2106 		if ((ifp->if_flags & IFF_PROMISC) != 0 &&
   2107 		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
   2108 	  	    bcmp(eh->ether_dhost, sc->sc_arpcom.ac_enaddr,
   2109 			    sizeof(eh->ether_dhost)) != 0) {
   2110 			m_freem(m);
   2111 			return (1);
   2112 		}
   2113 	}
   2114 #endif
   2115 
   2116 	/* Fix up data start offset in mbuf to point past ether header. */
   2117 	m_adj(m, sizeof(struct ether_header));
   2118 	ether_input(ifp, eh, m);
   2119 	return (1);
   2120 }
   2121 
   2122 /*
   2123  * Write an mbuf chain to the transmission buffer memory using 16 bit PIO.
   2124  * Returns number of bytes actually written, including length word.
   2125  *
   2126  * If an mbuf chain is too long for an Ethernet frame, it is not sent.
   2127  * Packets shorter than Ethernet minimum are legal, and we pad them
   2128  * before sending out.  An exception is "partial" packets which are
   2129  * shorter than mandatory Ethernet header.
   2130  *
   2131  * I wrote a code for an experimental "delayed padding" technique.
   2132  * When employed, it postpones the padding process for short packets.
   2133  * If xmit() occured at the moment, the padding process is omitted, and
   2134  * garbages are sent as pad data.  If next packet is stored in the
   2135  * transmission buffer before xmit(), write_mbuf() pads the previous
   2136  * packet before transmitting new packet.  This *may* gain the
   2137  * system performance (slightly).
   2138  */
   2139 void
   2140 fe_write_mbufs(sc, m)
   2141 	struct fe_softc *sc;
   2142 	struct mbuf *m;
   2143 {
   2144 	int bmpr8 = sc->sc_iobase + FE_BMPR8;
   2145 	u_char *data;
   2146 	u_short savebyte;	/* WARNING: Architecture dependent! */
   2147 	int totlen, len, wantbyte;
   2148 
   2149 	/* XXX thorpej 960116 - quiet bogus compiler warning. */
   2150 	savebyte = 0;
   2151 
   2152 #if FE_DELAYED_PADDING
   2153 	/* Do the "delayed padding." */
   2154 	len = sc->txb_padding >> 1;
   2155 	if (len > 0) {
   2156 		while (--len >= 0)
   2157 			outw(bmpr8, 0);
   2158 		sc->txb_padding = 0;
   2159 	}
   2160 #endif
   2161 
   2162 	/* We need to use m->m_pkthdr.len, so require the header */
   2163 	if ((m->m_flags & M_PKTHDR) == 0)
   2164 	  	panic("fe_write_mbufs: no header mbuf");
   2165 
   2166 #if FE_DEBUG >= 2
   2167 	/* First, count up the total number of bytes to copy. */
   2168 	for (totlen = 0, mp = m; mp != 0; mp = mp->m_next)
   2169 		totlen += mp->m_len;
   2170 	/* Check if this matches the one in the packet header. */
   2171 	if (totlen != m->m_pkthdr.len)
   2172 		log(LOG_WARNING, "%s: packet length mismatch? (%d/%d)\n",
   2173 		    sc->sc_dev.dv_xname, totlen, m->m_pkthdr.len);
   2174 #else
   2175 	/* Just use the length value in the packet header. */
   2176 	totlen = m->m_pkthdr.len;
   2177 #endif
   2178 
   2179 #if FE_DEBUG >= 1
   2180 	/*
   2181 	 * Should never send big packets.  If such a packet is passed,
   2182 	 * it should be a bug of upper layer.  We just ignore it.
   2183 	 * ... Partial (too short) packets, neither.
   2184 	 */
   2185 	if (totlen > ETHER_MAX_LEN || totlen < ETHER_HDR_SIZE) {
   2186 		log(LOG_ERR, "%s: got a %s packet (%u bytes) to send\n",
   2187 		    sc->sc_dev.dv_xname,
   2188 		    totlen < ETHER_HDR_SIZE ? "partial" : "big", totlen);
   2189 		sc->sc_arpcom.ac_if.if_oerrors++;
   2190 		return;
   2191 	}
   2192 #endif
   2193 
   2194 	/*
   2195 	 * Put the length word for this frame.
   2196 	 * Does 86960 accept odd length?  -- Yes.
   2197 	 * Do we need to pad the length to minimum size by ourselves?
   2198 	 * -- Generally yes.  But for (or will be) the last
   2199 	 * packet in the transmission buffer, we can skip the
   2200 	 * padding process.  It may gain performance slightly.  FIXME.
   2201 	 */
   2202 	outw(bmpr8, max(totlen, ETHER_MIN_LEN));
   2203 
   2204 	/*
   2205 	 * Update buffer status now.
   2206 	 * Truncate the length up to an even number, since we use outw().
   2207 	 */
   2208 	totlen = (totlen + 1) & ~1;
   2209 	sc->txb_free -= FE_DATA_LEN_LEN + max(totlen, ETHER_MIN_LEN);
   2210 	sc->txb_count++;
   2211 
   2212 #if FE_DELAYED_PADDING
   2213 	/* Postpone the packet padding if necessary. */
   2214 	if (totlen < ETHER_MIN_LEN)
   2215 		sc->txb_padding = ETHER_MIN_LEN - totlen;
   2216 #endif
   2217 
   2218 	/*
   2219 	 * Transfer the data from mbuf chain to the transmission buffer.
   2220 	 * MB86960 seems to require that data be transferred as words, and
   2221 	 * only words.  So that we require some extra code to patch
   2222 	 * over odd-length mbufs.
   2223 	 */
   2224 	wantbyte = 0;
   2225 	for (; m != 0; m = m->m_next) {
   2226 		/* Ignore empty mbuf. */
   2227 		len = m->m_len;
   2228 		if (len == 0)
   2229 			continue;
   2230 
   2231 		/* Find the actual data to send. */
   2232 		data = mtod(m, caddr_t);
   2233 
   2234 		/* Finish the last byte. */
   2235 		if (wantbyte) {
   2236 			outw(bmpr8, savebyte | (*data << 8));
   2237 			data++;
   2238 			len--;
   2239 			wantbyte = 0;
   2240 		}
   2241 
   2242 		/* Output contiguous words. */
   2243 		if (len > 1)
   2244 			outsw(bmpr8, data, len >> 1);
   2245 
   2246 		/* Save remaining byte, if there is one. */
   2247 		if (len & 1) {
   2248 			data += len & ~1;
   2249 			savebyte = *data;
   2250 			wantbyte = 1;
   2251 		}
   2252 	}
   2253 
   2254 	/* Spit the last byte, if the length is odd. */
   2255 	if (wantbyte)
   2256 		outw(bmpr8, savebyte);
   2257 
   2258 #if ! FE_DELAYED_PADDING
   2259 	/*
   2260 	 * Pad the packet to the minimum length if necessary.
   2261 	 */
   2262 	len = (ETHER_MIN_LEN >> 1) - (totlen >> 1);
   2263 	while (--len >= 0)
   2264 		outw(bmpr8, 0);
   2265 #endif
   2266 }
   2267 
   2268 /*
   2269  * Compute the multicast address filter from the
   2270  * list of multicast addresses we need to listen to.
   2271  */
   2272 void
   2273 fe_getmcaf(ac, af)
   2274 	struct arpcom *ac;
   2275 	u_char *af;
   2276 {
   2277 	struct ifnet *ifp = &ac->ac_if;
   2278 	struct ether_multi *enm;
   2279 	register u_char *cp, c;
   2280 	register u_long crc;
   2281 	register int i, len;
   2282 	struct ether_multistep step;
   2283 
   2284 	/*
   2285 	 * Set up multicast address filter by passing all multicast addresses
   2286 	 * through a crc generator, and then using the high order 6 bits as an
   2287 	 * index into the 64 bit logical address filter.  The high order bit
   2288 	 * selects the word, while the rest of the bits select the bit within
   2289 	 * the word.
   2290 	 */
   2291 
   2292 	if ((ifp->if_flags & IFF_PROMISC) != 0)
   2293 		goto allmulti;
   2294 
   2295 	af[0] = af[1] = af[2] = af[3] = af[4] = af[5] = af[6] = af[7] = 0x00;
   2296 	ETHER_FIRST_MULTI(step, ac, enm);
   2297 	while (enm != NULL) {
   2298 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
   2299 		    sizeof(enm->enm_addrlo)) != 0) {
   2300 			/*
   2301 			 * We must listen to a range of multicast addresses.
   2302 			 * For now, just accept all multicasts, rather than
   2303 			 * trying to set only those filter bits needed to match
   2304 			 * the range.  (At this time, the only use of address
   2305 			 * ranges is for IP multicast routing, for which the
   2306 			 * range is big enough to require all bits set.)
   2307 			 */
   2308 			goto allmulti;
   2309 		}
   2310 
   2311 		cp = enm->enm_addrlo;
   2312 		crc = 0xffffffff;
   2313 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
   2314 			c = *cp++;
   2315 			for (i = 8; --i >= 0;) {
   2316 				if ((crc & 0x01) ^ (c & 0x01)) {
   2317 					crc >>= 1;
   2318 					crc ^= 0xedb88320;
   2319 				} else
   2320 					crc >>= 1;
   2321 				c >>= 1;
   2322 			}
   2323 		}
   2324 		/* Just want the 6 most significant bits. */
   2325 		crc >>= 26;
   2326 
   2327 		/* Turn on the corresponding bit in the filter. */
   2328 		af[crc >> 3] |= 1 << (crc & 7);
   2329 
   2330 		ETHER_NEXT_MULTI(step, enm);
   2331 	}
   2332 	ifp->if_flags &= ~IFF_ALLMULTI;
   2333 	return;
   2334 
   2335 allmulti:
   2336 	ifp->if_flags |= IFF_ALLMULTI;
   2337 	af[0] = af[1] = af[2] = af[3] = af[4] = af[5] = af[6] = af[7] = 0xff;
   2338 }
   2339 
   2340 /*
   2341  * Calculate a new "multicast packet filter" and put the 86960
   2342  * receiver in appropriate mode.
   2343  */
   2344 void
   2345 fe_setmode(sc)
   2346 	struct fe_softc *sc;
   2347 {
   2348 	int flags = sc->sc_arpcom.ac_if.if_flags;
   2349 
   2350 	/*
   2351 	 * If the interface is not running, we postpone the update
   2352 	 * process for receive modes and multicast address filter
   2353 	 * until the interface is restarted.  It reduces some
   2354 	 * complicated job on maintaining chip states.  (Earlier versions
   2355 	 * of this driver had a bug on that point...)
   2356 	 *
   2357 	 * To complete the trick, fe_init() calls fe_setmode() after
   2358 	 * restarting the interface.
   2359 	 */
   2360 	if ((flags & IFF_RUNNING) == 0)
   2361 		return;
   2362 
   2363 	/*
   2364 	 * Promiscuous mode is handled separately.
   2365 	 */
   2366 	if ((flags & IFF_PROMISC) != 0) {
   2367 		/*
   2368 		 * Program 86960 to receive all packets on the segment
   2369 		 * including those directed to other stations.
   2370 		 * Multicast filter stored in MARs are ignored
   2371 		 * under this setting, so we don't need to update it.
   2372 		 *
   2373 		 * Promiscuous mode is used solely by BPF, and BPF only
   2374 		 * listens to valid (no error) packets.  So, we ignore
   2375 		 * errornous ones even in this mode.
   2376 		 */
   2377 		outb(sc->sc_iobase + FE_DLCR5,
   2378 		    sc->proto_dlcr5 | FE_D5_AFM0 | FE_D5_AFM1);
   2379 		sc->filter_change = 0;
   2380 
   2381 #if FE_DEBUG >= 3
   2382 		log(LOG_INFO, "%s: promiscuous mode\n", sc->sc_dev.dv_xname);
   2383 #endif
   2384 		return;
   2385 	}
   2386 
   2387 	/*
   2388 	 * Turn the chip to the normal (non-promiscuous) mode.
   2389 	 */
   2390 	outb(sc->sc_iobase + FE_DLCR5, sc->proto_dlcr5 | FE_D5_AFM1);
   2391 
   2392 	/*
   2393 	 * Find the new multicast filter value.
   2394 	 */
   2395 	fe_getmcaf(&sc->sc_arpcom, sc->filter);
   2396 	sc->filter_change = 1;
   2397 
   2398 #if FE_DEBUG >= 3
   2399 	log(LOG_INFO,
   2400 	    "%s: address filter: [%02x %02x %02x %02x %02x %02x %02x %02x]\n",
   2401 	    sc->sc_dev.dv_xname,
   2402 	    sc->filter[0], sc->filter[1], sc->filter[2], sc->filter[3],
   2403 	    sc->filter[4], sc->filter[5], sc->filter[6], sc->filter[7]);
   2404 #endif
   2405 
   2406 	/*
   2407 	 * We have to update the multicast filter in the 86960, A.S.A.P.
   2408 	 *
   2409 	 * Note that the DLC (Data Linc Control unit, i.e. transmitter
   2410 	 * and receiver) must be stopped when feeding the filter, and
   2411 	 * DLC trushes all packets in both transmission and receive
   2412 	 * buffers when stopped.
   2413 	 *
   2414 	 * ... Are the above sentenses correct?  I have to check the
   2415 	 *     manual of the MB86960A.  FIXME.
   2416 	 *
   2417 	 * To reduce the packet lossage, we delay the filter update
   2418 	 * process until buffers are empty.
   2419 	 */
   2420 	if (sc->txb_sched == 0 && sc->txb_count == 0 &&
   2421 	    (inb(sc->sc_iobase + FE_DLCR1) & FE_D1_PKTRDY) == 0) {
   2422 		/*
   2423 		 * Buffers are (apparently) empty.  Load
   2424 		 * the new filter value into MARs now.
   2425 		 */
   2426 		fe_loadmar(sc);
   2427 	} else {
   2428 		/*
   2429 		 * Buffers are not empty.  Mark that we have to update
   2430 		 * the MARs.  The new filter will be loaded by feintr()
   2431 		 * later.
   2432 		 */
   2433 #if FE_DEBUG >= 4
   2434 		log(LOG_INFO, "%s: filter change delayed\n", sc->sc_dev.dv_xname);
   2435 #endif
   2436 	}
   2437 }
   2438 
   2439 /*
   2440  * Load a new multicast address filter into MARs.
   2441  *
   2442  * The caller must have splnet'ed befor fe_loadmar.
   2443  * This function starts the DLC upon return.  So it can be called only
   2444  * when the chip is working, i.e., from the driver's point of view, when
   2445  * a device is RUNNING.  (I mistook the point in previous versions.)
   2446  */
   2447 void
   2448 fe_loadmar(sc)
   2449 	struct fe_softc *sc;
   2450 {
   2451 
   2452 	/* Stop the DLC (transmitter and receiver). */
   2453 	outb(sc->sc_iobase + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
   2454 
   2455 	/* Select register bank 1 for MARs. */
   2456 	outb(sc->sc_iobase + FE_DLCR7,
   2457 	    sc->proto_dlcr7 | FE_D7_RBS_MAR | FE_D7_POWER_UP);
   2458 
   2459 	/* Copy filter value into the registers. */
   2460 	outblk(sc->sc_iobase + FE_MAR8, sc->filter, FE_FILTER_LEN);
   2461 
   2462 	/* Restore the bank selection for BMPRs (i.e., runtime registers). */
   2463 	outb(sc->sc_iobase + FE_DLCR7,
   2464 	    sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP);
   2465 
   2466 	/* Restart the DLC. */
   2467 	outb(sc->sc_iobase + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_ENABLE);
   2468 
   2469 	/* We have just updated the filter. */
   2470 	sc->filter_change = 0;
   2471 
   2472 #if FE_DEBUG >= 3
   2473 	log(LOG_INFO, "%s: address filter changed\n", sc->sc_dev.dv_xname);
   2474 #endif
   2475 }
   2476 
   2477 #if FE_DEBUG >= 1
   2478 void
   2479 fe_dump(level, sc)
   2480 	int level;
   2481 	struct fe_softc *sc;
   2482 {
   2483 	int iobase = sc->sc_iobase;
   2484 	u_char save_dlcr7;
   2485 
   2486 	save_dlcr7 = inb(iobase + FE_DLCR7);
   2487 
   2488 	log(level, "\tDLCR = %02x %02x %02x %02x %02x %02x %02x %02x",
   2489 	    inb(iobase + FE_DLCR0),  inb(iobase + FE_DLCR1),
   2490 	    inb(iobase + FE_DLCR2),  inb(iobase + FE_DLCR3),
   2491 	    inb(iobase + FE_DLCR4),  inb(iobase + FE_DLCR5),
   2492 	    inb(iobase + FE_DLCR6),  inb(iobase + FE_DLCR7));
   2493 
   2494 	outb(iobase + FE_DLCR7, (save_dlcr7 & ~FE_D7_RBS) | FE_D7_RBS_DLCR);
   2495 	log(level, "\t       %02x %02x %02x %02x %02x %02x %02x %02x,",
   2496 	    inb(iobase + FE_DLCR8),  inb(iobase + FE_DLCR9),
   2497 	    inb(iobase + FE_DLCR10), inb(iobase + FE_DLCR11),
   2498 	    inb(iobase + FE_DLCR12), inb(iobase + FE_DLCR13),
   2499 	    inb(iobase + FE_DLCR14), inb(iobase + FE_DLCR15));
   2500 
   2501 	outb(iobase + FE_DLCR7, (save_dlcr7 & ~FE_D7_RBS) | FE_D7_RBS_MAR);
   2502 	log(level, "\tMAR  = %02x %02x %02x %02x %02x %02x %02x %02x,",
   2503 	    inb(iobase + FE_MAR8),   inb(iobase + FE_MAR9),
   2504 	    inb(iobase + FE_MAR10),  inb(iobase + FE_MAR11),
   2505 	    inb(iobase + FE_MAR12),  inb(iobase + FE_MAR13),
   2506 	    inb(iobase + FE_MAR14),  inb(iobase + FE_MAR15));
   2507 
   2508 	outb(iobase + FE_DLCR7, (save_dlcr7 & ~FE_D7_RBS) | FE_D7_RBS_BMPR);
   2509 	log(level, "\tBMPR = xx xx %02x %02x %02x %02x %02x %02x %02x %02x xx %02x.",
   2510 	    inb(iobase + FE_BMPR10), inb(iobase + FE_BMPR11),
   2511 	    inb(iobase + FE_BMPR12), inb(iobase + FE_BMPR13),
   2512 	    inb(iobase + FE_BMPR14), inb(iobase + FE_BMPR15),
   2513 	    inb(iobase + FE_BMPR16), inb(iobase + FE_BMPR17),
   2514 	    inb(iobase + FE_BMPR19));
   2515 
   2516 	outb(iobase + FE_DLCR7, save_dlcr7);
   2517 }
   2518 #endif
   2519