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