Home | History | Annotate | Line # | Download | only in ic
mb86960.c revision 1.1
      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/mb86960.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 	/* We need an address. */
   1236 	if (ifp->if_addrlist == 0) {
   1237 #if FE_DEBUG >= 1
   1238 		log(LOG_ERR, "%s: init() without any address\n",
   1239 		    sc->sc_dev.dv_xname);
   1240 #endif
   1241 		return;
   1242 	}
   1243 
   1244 	/* Start initializing 86960. */
   1245 	s = splimp();
   1246 
   1247 	/* Reset transmitter flags. */
   1248 	ifp->if_flags &= ~IFF_OACTIVE;
   1249 	ifp->if_timer = 0;
   1250 
   1251 	sc->txb_free = sc->txb_size;
   1252 	sc->txb_count = 0;
   1253 	sc->txb_sched = 0;
   1254 
   1255 	/* Call a hook. */
   1256 	if (sc->init)
   1257 		sc->init(sc);
   1258 
   1259 #if FE_DEBUG >= 3
   1260 	log(LOG_INFO, "%s: after init hook\n", sc->sc_dev.dv_xname);
   1261 	fe_dump(LOG_INFO, sc);
   1262 #endif
   1263 
   1264 	/*
   1265 	 * Make sure to disable the chip, also.
   1266 	 * This may also help re-programming the chip after
   1267 	 * hot insertion of PCMCIAs.
   1268 	 */
   1269 	outb(sc->sc_iobase + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
   1270 
   1271 	/* Power up the chip and select register bank for DLCRs. */
   1272 	delay(200);
   1273 	outb(sc->sc_iobase + FE_DLCR7,
   1274 	    sc->proto_dlcr7 | FE_D7_RBS_DLCR | FE_D7_POWER_UP);
   1275 	delay(200);
   1276 
   1277 	/* Feed the station address. */
   1278 	outblk(sc->sc_iobase + FE_DLCR8, sc->sc_enaddr, ETHER_ADDR_LEN);
   1279 
   1280 	/* Select the BMPR bank for runtime register access. */
   1281 	outb(sc->sc_iobase + FE_DLCR7,
   1282 	    sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP);
   1283 
   1284 	/* Initialize registers. */
   1285 	outb(sc->sc_iobase + FE_DLCR0, 0xFF);	/* Clear all bits. */
   1286 	outb(sc->sc_iobase + FE_DLCR1, 0xFF);	/* ditto. */
   1287 	outb(sc->sc_iobase + FE_DLCR2, 0x00);
   1288 	outb(sc->sc_iobase + FE_DLCR3, 0x00);
   1289 	outb(sc->sc_iobase + FE_DLCR4, sc->proto_dlcr4);
   1290 	outb(sc->sc_iobase + FE_DLCR5, sc->proto_dlcr5);
   1291 	outb(sc->sc_iobase + FE_BMPR10, 0x00);
   1292 	outb(sc->sc_iobase + FE_BMPR11, FE_B11_CTRL_SKIP);
   1293 	outb(sc->sc_iobase + FE_BMPR12, 0x00);
   1294 	outb(sc->sc_iobase + FE_BMPR13, sc->proto_bmpr13);
   1295 	outb(sc->sc_iobase + FE_BMPR14, 0x00);
   1296 	outb(sc->sc_iobase + FE_BMPR15, 0x00);
   1297 
   1298 #if FE_DEBUG >= 3
   1299 	log(LOG_INFO, "%s: just before enabling DLC\n", sc->sc_dev.dv_xname);
   1300 	fe_dump(LOG_INFO, sc);
   1301 #endif
   1302 
   1303 	/* Enable interrupts. */
   1304 	outb(sc->sc_iobase + FE_DLCR2, FE_TMASK);
   1305 	outb(sc->sc_iobase + FE_DLCR3, FE_RMASK);
   1306 
   1307 	/* Enable transmitter and receiver. */
   1308 	delay(200);
   1309 	outb(sc->sc_iobase + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_ENABLE);
   1310 	delay(200);
   1311 
   1312 #if FE_DEBUG >= 3
   1313 	log(LOG_INFO, "%s: just after enabling DLC\n", sc->sc_dev.dv_xname);
   1314 	fe_dump(LOG_INFO, sc);
   1315 #endif
   1316 
   1317 	/*
   1318 	 * Make sure to empty the receive buffer.
   1319 	 *
   1320 	 * This may be redundant, but *if* the receive buffer were full
   1321 	 * at this point, the driver would hang.  I have experienced
   1322 	 * some strange hangups just after UP.  I hope the following
   1323 	 * code solve the problem.
   1324 	 *
   1325 	 * I have changed the order of hardware initialization.
   1326 	 * I think the receive buffer cannot have any packets at this
   1327 	 * point in this version.  The following code *must* be
   1328 	 * redundant now.  FIXME.
   1329 	 */
   1330 	for (i = 0; i < FE_MAX_RECV_COUNT; i++) {
   1331 		if (inb(sc->sc_iobase + FE_DLCR5) & FE_D5_BUFEMP)
   1332 			break;
   1333 		outb(sc->sc_iobase + FE_BMPR14, FE_B14_SKIP);
   1334 	}
   1335 #if FE_DEBUG >= 1
   1336 	if (i >= FE_MAX_RECV_COUNT) {
   1337 		log(LOG_ERR, "%s: cannot empty receive buffer\n",
   1338 		    sc->sc_dev.dv_xname);
   1339 	}
   1340 #endif
   1341 #if FE_DEBUG >= 3
   1342 	if (i < FE_MAX_RECV_COUNT) {
   1343 		log(LOG_INFO, "%s: receive buffer emptied (%d)\n",
   1344 		    sc->sc_dev.dv_xname, i);
   1345 	}
   1346 #endif
   1347 
   1348 #if FE_DEBUG >= 3
   1349 	log(LOG_INFO, "%s: after ERB loop\n", sc->sc_dev.dv_xname);
   1350 	fe_dump(LOG_INFO, sc);
   1351 #endif
   1352 
   1353 	/* Do we need this here? */
   1354 	outb(sc->sc_iobase + FE_DLCR0, 0xFF);	/* Clear all bits. */
   1355 	outb(sc->sc_iobase + FE_DLCR1, 0xFF);	/* ditto. */
   1356 
   1357 #if FE_DEBUG >= 3
   1358 	log(LOG_INFO, "%s: after FIXME\n", sc->sc_dev.dv_xname);
   1359 	fe_dump(LOG_INFO, sc);
   1360 #endif
   1361 
   1362 	/* Set 'running' flag. */
   1363 	ifp->if_flags |= IFF_RUNNING;
   1364 
   1365 	/*
   1366 	 * At this point, the interface is runnung properly,
   1367 	 * except that it receives *no* packets.  we then call
   1368 	 * fe_setmode() to tell the chip what packets to be
   1369 	 * received, based on the if_flags and multicast group
   1370 	 * list.  It completes the initialization process.
   1371 	 */
   1372 	fe_setmode(sc);
   1373 
   1374 #if FE_DEBUG >= 3
   1375 	log(LOG_INFO, "%s: after setmode\n", sc->sc_dev.dv_xname);
   1376 	fe_dump(LOG_INFO, sc);
   1377 #endif
   1378 
   1379 	/* ...and attempt to start output. */
   1380 	fe_start(ifp);
   1381 
   1382 #if FE_DEBUG >= 3
   1383 	log(LOG_INFO, "%s: end of fe_init()\n", sc->sc_dev.dv_xname);
   1384 	fe_dump(LOG_INFO, sc);
   1385 #endif
   1386 
   1387 	splx(s);
   1388 }
   1389 
   1390 /*
   1391  * This routine actually starts the transmission on the interface
   1392  */
   1393 static inline void
   1394 fe_xmit(sc)
   1395 	struct fe_softc *sc;
   1396 {
   1397 
   1398 	/*
   1399 	 * Set a timer just in case we never hear from the board again.
   1400 	 * We use longer timeout for multiple packet transmission.
   1401 	 * I'm not sure this timer value is appropriate.  FIXME.
   1402 	 */
   1403 	sc->sc_arpcom.ac_if.if_timer = 1 + sc->txb_count;
   1404 
   1405 	/* Update txb variables. */
   1406 	sc->txb_sched = sc->txb_count;
   1407 	sc->txb_count = 0;
   1408 	sc->txb_free = sc->txb_size;
   1409 
   1410 #if FE_DELAYED_PADDING
   1411 	/* Omit the postponed padding process. */
   1412 	sc->txb_padding = 0;
   1413 #endif
   1414 
   1415 	/* Start transmitter, passing packets in TX buffer. */
   1416 	outb(sc->sc_iobase + FE_BMPR10, sc->txb_sched | FE_B10_START);
   1417 }
   1418 
   1419 /*
   1420  * Start output on interface.
   1421  * We make two assumptions here:
   1422  *  1) that the current priority is set to splimp _before_ this code
   1423  *     is called *and* is returned to the appropriate priority after
   1424  *     return
   1425  *  2) that the IFF_OACTIVE flag is checked before this code is called
   1426  *     (i.e. that the output part of the interface is idle)
   1427  */
   1428 void
   1429 fe_start(ifp)
   1430 	struct ifnet *ifp;
   1431 {
   1432 	struct fe_softc *sc = fecd.cd_devs[ifp->if_unit];
   1433 	struct mbuf *m;
   1434 
   1435 #if FE_DEBUG >= 1
   1436 	/* Just a sanity check. */
   1437 	if ((sc->txb_count == 0) != (sc->txb_free == sc->txb_size)) {
   1438 		/*
   1439 		 * Txb_count and txb_free co-works to manage the
   1440 		 * transmission buffer.  Txb_count keeps track of the
   1441 		 * used potion of the buffer, while txb_free does unused
   1442 		 * potion.  So, as long as the driver runs properly,
   1443 		 * txb_count is zero if and only if txb_free is same
   1444 		 * as txb_size (which represents whole buffer.)
   1445 		 */
   1446 		log(LOG_ERR, "%s: inconsistent txb variables (%d, %d)\n",
   1447 		    sc->sc_dev.dv_xname, sc->txb_count, sc->txb_free);
   1448 		/*
   1449 		 * So, what should I do, then?
   1450 		 *
   1451 		 * We now know txb_count and txb_free contradicts.  We
   1452 		 * cannot, however, tell which is wrong.  More
   1453 		 * over, we cannot peek 86960 transmission buffer or
   1454 		 * reset the transmission buffer.  (In fact, we can
   1455 		 * reset the entire interface.  I don't want to do it.)
   1456 		 *
   1457 		 * If txb_count is incorrect, leaving it as is will cause
   1458 		 * sending of gabages after next interrupt.  We have to
   1459 		 * avoid it.  Hence, we reset the txb_count here.  If
   1460 		 * txb_free was incorrect, resetting txb_count just loose
   1461 		 * some packets.  We can live with it.
   1462 		 */
   1463 		sc->txb_count = 0;
   1464 	}
   1465 #endif
   1466 
   1467 #if FE_DEBUG >= 1
   1468 	/*
   1469 	 * First, see if there are buffered packets and an idle
   1470 	 * transmitter - should never happen at this point.
   1471 	 */
   1472 	if ((sc->txb_count > 0) && (sc->txb_sched == 0)) {
   1473 		log(LOG_ERR, "%s: transmitter idle with %d buffered packets\n",
   1474 		    sc->sc_dev.dv_xname, sc->txb_count);
   1475 		fe_xmit(sc);
   1476 	}
   1477 #endif
   1478 
   1479 	/*
   1480 	 * Stop accepting more transmission packets temporarily, when
   1481 	 * a filter change request is delayed.  Updating the MARs on
   1482 	 * 86960 flushes the transmisstion buffer, so it is delayed
   1483 	 * until all buffered transmission packets have been sent
   1484 	 * out.
   1485 	 */
   1486 	if (sc->filter_change) {
   1487 		/*
   1488 		 * Filter change requst is delayed only when the DLC is
   1489 		 * working.  DLC soon raise an interrupt after finishing
   1490 		 * the work.
   1491 		 */
   1492 		goto indicate_active;
   1493 	}
   1494 
   1495 	for (;;) {
   1496 		/*
   1497 		 * See if there is room to put another packet in the buffer.
   1498 		 * We *could* do better job by peeking the send queue to
   1499 		 * know the length of the next packet.  Current version just
   1500 		 * tests against the worst case (i.e., longest packet).  FIXME.
   1501 		 *
   1502 		 * When adding the packet-peek feature, don't forget adding a
   1503 		 * test on txb_count against QUEUEING_MAX.
   1504 		 * There is a little chance the packet count exceeds
   1505 		 * the limit.  Assume transmission buffer is 8KB (2x8KB
   1506 		 * configuration) and an application sends a bunch of small
   1507 		 * (i.e., minimum packet sized) packets rapidly.  An 8KB
   1508 		 * buffer can hold 130 blocks of 62 bytes long...
   1509 		 */
   1510 		if (sc->txb_free < ETHER_MAX_LEN + FE_DATA_LEN_LEN) {
   1511 			/* No room. */
   1512 			goto indicate_active;
   1513 		}
   1514 
   1515 #if FE_SINGLE_TRANSMISSION
   1516 		if (sc->txb_count > 0) {
   1517 			/* Just one packet per a transmission buffer. */
   1518 			goto indicate_active;
   1519 		}
   1520 #endif
   1521 
   1522 		/*
   1523 		 * Get the next mbuf chain for a packet to send.
   1524 		 */
   1525 		IF_DEQUEUE(&ifp->if_snd, m);
   1526 		if (m == 0) {
   1527 			/* No more packets to send. */
   1528 			goto indicate_inactive;
   1529 		}
   1530 
   1531 		/*
   1532 		 * Copy the mbuf chain into the transmission buffer.
   1533 		 * txb_* variables are updated as necessary.
   1534 		 */
   1535 		fe_write_mbufs(sc, m);
   1536 
   1537 		/* Start transmitter if it's idle. */
   1538 		if (sc->txb_sched == 0)
   1539 			fe_xmit(sc);
   1540 
   1541 #if 0 /* Turned of, since our interface is now duplex. */
   1542 		/*
   1543 		 * Tap off here if there is a bpf listener.
   1544 		 */
   1545 #if NBPFILTER > 0
   1546 		if (ifp->if_bpf)
   1547 			bpf_mtap(ifp->if_bpf, m);
   1548 #endif
   1549 #endif
   1550 
   1551 		m_freem(m);
   1552 	}
   1553 
   1554 indicate_inactive:
   1555 	/*
   1556 	 * We are using the !OACTIVE flag to indicate to
   1557 	 * the outside world that we can accept an
   1558 	 * additional packet rather than that the
   1559 	 * transmitter is _actually_ active.  Indeed, the
   1560 	 * transmitter may be active, but if we haven't
   1561 	 * filled all the buffers with data then we still
   1562 	 * want to accept more.
   1563 	 */
   1564 	ifp->if_flags &= ~IFF_OACTIVE;
   1565 	return;
   1566 
   1567 indicate_active:
   1568 	/*
   1569 	 * The transmitter is active, and there are no room for
   1570 	 * more outgoing packets in the transmission buffer.
   1571 	 */
   1572 	ifp->if_flags |= IFF_OACTIVE;
   1573 	return;
   1574 }
   1575 
   1576 /*
   1577  * Drop (skip) a packet from receive buffer in 86960 memory.
   1578  */
   1579 static inline void
   1580 fe_droppacket (struct fe_softc * sc)
   1581 {
   1582 	outb(sc->sc_iobase + FE_BMPR14, FE_B14_SKIP);
   1583 }
   1584 
   1585 /*
   1586  * Transmission interrupt handler
   1587  * The control flow of this function looks silly.  FIXME.
   1588  */
   1589 void
   1590 fe_tint(sc, tstat)
   1591 	struct fe_softc *sc;
   1592 	u_char tstat;
   1593 {
   1594 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
   1595 	int left;
   1596 	int col;
   1597 
   1598 	/*
   1599 	 * Handle "excessive collision" interrupt.
   1600 	 */
   1601 	if (tstat & FE_D0_COLL16) {
   1602 		/*
   1603 		 * Find how many packets (including this collided one)
   1604 		 * are left unsent in transmission buffer.
   1605 		 */
   1606 		left = inb(sc->sc_iobase + FE_BMPR10);
   1607 
   1608 #if FE_DEBUG >= 2
   1609 		log(LOG_WARNING, "%s: excessive collision (%d/%d)\n",
   1610 		    sc->sc_dev.dv_xname, left, sc->txb_sched);
   1611 #endif
   1612 #if FE_DEBUG >= 3
   1613 		fe_dump(LOG_INFO, sc);
   1614 #endif
   1615 
   1616 		/*
   1617 		 * Update statistics.
   1618 		 */
   1619 		ifp->if_collisions += 16;
   1620 		ifp->if_oerrors++;
   1621 		ifp->if_opackets += sc->txb_sched - left;
   1622 
   1623 		/*
   1624 		 * Collision statistics has been updated.
   1625 		 * Clear the collision flag on 86960 now to avoid confusion.
   1626 		 */
   1627 		outb(sc->sc_iobase + FE_DLCR0, FE_D0_COLLID);
   1628 
   1629 		/*
   1630 		 * Restart transmitter, skipping the
   1631 		 * collided packet.
   1632 		 *
   1633 		 * We *must* skip the packet to keep network running
   1634 		 * properly.  Excessive collision error is an
   1635 		 * indication of the network overload.  If we
   1636 		 * tried sending the same packet after excessive
   1637 		 * collision, the network would be filled with
   1638 		 * out-of-time packets.  Packets belonging
   1639 		 * to reliable transport (such as TCP) are resent
   1640 		 * by some upper layer.
   1641 		 */
   1642 		outb(sc->sc_iobase + FE_BMPR11,
   1643 		    FE_B11_CTRL_SKIP | FE_B11_MODE1);
   1644 		sc->txb_sched = left - 1;
   1645 	}
   1646 
   1647 	/*
   1648 	 * Handle "transmission complete" interrupt.
   1649 	 */
   1650 	if (tstat & FE_D0_TXDONE) {
   1651 		/*
   1652 		 * Add in total number of collisions on last
   1653 		 * transmission.  We also clear "collision occurred" flag
   1654 		 * here.
   1655 		 *
   1656 		 * 86960 has a design flow on collision count on multiple
   1657 		 * packet transmission.  When we send two or more packets
   1658 		 * with one start command (that's what we do when the
   1659 		 * transmission queue is clauded), 86960 informs us number
   1660 		 * of collisions occured on the last packet on the
   1661 		 * transmission only.  Number of collisions on previous
   1662 		 * packets are lost.  I have told that the fact is clearly
   1663 		 * stated in the Fujitsu document.
   1664 		 *
   1665 		 * I considered not to mind it seriously.  Collision
   1666 		 * count is not so important, anyway.  Any comments?  FIXME.
   1667 		 */
   1668 
   1669 		if (inb(sc->sc_iobase + FE_DLCR0) & FE_D0_COLLID) {
   1670 			/* Clear collision flag. */
   1671 			outb(sc->sc_iobase + FE_DLCR0, FE_D0_COLLID);
   1672 
   1673 			/* Extract collision count from 86960. */
   1674 			col = inb(sc->sc_iobase + FE_DLCR4) & FE_D4_COL;
   1675 			if (col == 0) {
   1676 				/*
   1677 				 * Status register indicates collisions,
   1678 				 * while the collision count is zero.
   1679 				 * This can happen after multiple packet
   1680 				 * transmission, indicating that one or more
   1681 				 * previous packet(s) had been collided.
   1682 				 *
   1683 				 * Since the accurate number of collisions
   1684 				 * has been lost, we just guess it as 1;
   1685 				 * Am I too optimistic?  FIXME.
   1686 				 */
   1687 				col = 1;
   1688 			} else
   1689 				col >>= FE_D4_COL_SHIFT;
   1690 			ifp->if_collisions += col;
   1691 #if FE_DEBUG >= 4
   1692 			log(LOG_WARNING, "%s: %d collision%s (%d)\n",
   1693 			    sc->sc_dev.dv_xname, col, col == 1 ? "" : "s",
   1694 			    sc->txb_sched);
   1695 #endif
   1696 		}
   1697 
   1698 		/*
   1699 		 * Update total number of successfully
   1700 		 * transmitted packets.
   1701 		 */
   1702 		ifp->if_opackets += sc->txb_sched;
   1703 		sc->txb_sched = 0;
   1704 
   1705 		/*
   1706 		 * The transmitter is no more active.
   1707 		 * Reset output active flag and watchdog timer.
   1708 		 */
   1709 		ifp->if_flags &= ~IFF_OACTIVE;
   1710 		ifp->if_timer = 0;
   1711 
   1712 		/*
   1713 		 * If more data is ready to transmit in the buffer, start
   1714 		 * transmitting them.  Otherwise keep transmitter idle,
   1715 		 * even if more data is queued.  This gives receive
   1716 		 * process a slight priority.
   1717 		 */
   1718 		if (sc->txb_count > 0)
   1719 			fe_xmit(sc);
   1720 	}
   1721 }
   1722 
   1723 /*
   1724  * Ethernet interface receiver interrupt.
   1725  */
   1726 void
   1727 fe_rint(sc, rstat)
   1728 	struct fe_softc *sc;
   1729 	u_char rstat;
   1730 {
   1731 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
   1732 	int len;
   1733 	u_char status;
   1734 	int i;
   1735 
   1736 	/*
   1737 	 * Update statistics if this interrupt is caused by an error.
   1738 	 */
   1739 	if (rstat & (FE_D1_OVRFLO | FE_D1_CRCERR |
   1740 		     FE_D1_ALGERR | FE_D1_SRTPKT)) {
   1741 #if FE_DEBUG >= 3
   1742 		log(LOG_WARNING, "%s: receive error: %b\n",
   1743 		    sc->sc_dev.dv_xname, rstat, FE_D1_ERRBITS);
   1744 #endif
   1745 		ifp->if_ierrors++;
   1746 	}
   1747 
   1748 	/*
   1749 	 * MB86960 has a flag indicating "receive queue empty."
   1750 	 * We just loop cheking the flag to pull out all received
   1751 	 * packets.
   1752 	 *
   1753 	 * We limit the number of iterrations to avoid infinite loop.
   1754 	 * It can be caused by a very slow CPU (some broken
   1755 	 * peripheral may insert incredible number of wait cycles)
   1756 	 * or, worse, by a broken MB86960 chip.
   1757 	 */
   1758 	for (i = 0; i < FE_MAX_RECV_COUNT; i++) {
   1759 		/* Stop the iterration if 86960 indicates no packets. */
   1760 		if (inb(sc->sc_iobase + FE_DLCR5) & FE_D5_BUFEMP)
   1761 			break;
   1762 
   1763 		/*
   1764 		 * Extract A receive status byte.
   1765 		 * As our 86960 is in 16 bit bus access mode, we have to
   1766 		 * use inw() to get the status byte.  The significant
   1767 		 * value is returned in lower 8 bits.
   1768 		 */
   1769 		status = (u_char)inw(sc->sc_iobase + FE_BMPR8);
   1770 #if FE_DEBUG >= 4
   1771 		log(LOG_INFO, "%s: receive status = %02x\n",
   1772 		    sc->sc_dev.dv_xname, status);
   1773 #endif
   1774 
   1775 		/*
   1776 		 * If there was an error, update statistics and drop
   1777 		 * the packet, unless the interface is in promiscuous
   1778 		 * mode.
   1779 		 */
   1780 		if ((status & 0xF0) != 0x20) {	/* XXXX ? */
   1781 			if ((ifp->if_flags & IFF_PROMISC) == 0) {
   1782 				ifp->if_ierrors++;
   1783 				fe_droppacket(sc);
   1784 				continue;
   1785 			}
   1786 		}
   1787 
   1788 		/*
   1789 		 * Extract the packet length.
   1790 		 * It is a sum of a header (14 bytes) and a payload.
   1791 		 * CRC has been stripped off by the 86960.
   1792 		 */
   1793 		len = inw(sc->sc_iobase + FE_BMPR8);
   1794 
   1795 		/*
   1796 		 * MB86965 checks the packet length and drop big packet
   1797 		 * before passing it to us.  There are no chance we can
   1798 		 * get [crufty] packets.  Hence, if the length exceeds
   1799 		 * the specified limit, it means some serious failure,
   1800 		 * such as out-of-sync on receive buffer management.
   1801 		 *
   1802 		 * Is this statement true?  FIXME.
   1803 		 */
   1804 		if (len > ETHER_MAX_LEN || len < ETHER_HDR_SIZE) {
   1805 #if FE_DEBUG >= 2
   1806 			log(LOG_WARNING,
   1807 			    "%s: received a %s packet? (%u bytes)\n",
   1808 			    sc->sc_dev.dv_xname,
   1809 			    len < ETHER_HDR_SIZE ? "partial" : "big", len);
   1810 #endif
   1811 			ifp->if_ierrors++;
   1812 			fe_droppacket(sc);
   1813 			continue;
   1814 		}
   1815 
   1816 		/*
   1817 		 * Check for a short (RUNT) packet.  We *do* check
   1818 		 * but do nothing other than print a message.
   1819 		 * Short packets are illegal, but does nothing bad
   1820 		 * if it carries data for upper layer.
   1821 		 */
   1822 #if FE_DEBUG >= 2
   1823 		if (len < ETHER_MIN_LEN) {
   1824 			log(LOG_WARNING,
   1825 			     "%s: received a short packet? (%u bytes)\n",
   1826 			     sc->sc_dev.dv_xname, len);
   1827 		}
   1828 #endif
   1829 
   1830 		/*
   1831 		 * Go get a packet.
   1832 		 */
   1833 		if (!fe_get_packet(sc, len)) {
   1834 			/* Skip a packet, updating statistics. */
   1835 #if FE_DEBUG >= 2
   1836 			log(LOG_WARNING,
   1837 			    "%s: out of mbufs; dropping packet (%u bytes)\n",
   1838 			    sc->sc_dev.dv_xname, len);
   1839 #endif
   1840 			ifp->if_ierrors++;
   1841 			fe_droppacket(sc);
   1842 
   1843 			/*
   1844 			 * We stop receiving packets, even if there are
   1845 			 * more in the buffer.  We hope we can get more
   1846 			 * mbufs next time.
   1847 			 */
   1848 			return;
   1849 		}
   1850 
   1851 		/* Successfully received a packet.  Update stat. */
   1852 		ifp->if_ipackets++;
   1853 	}
   1854 }
   1855 
   1856 /*
   1857  * Ethernet interface interrupt processor
   1858  */
   1859 int
   1860 feintr(arg)
   1861 	void *arg;
   1862 {
   1863 	struct fe_softc *sc = arg;
   1864 	u_char tstat, rstat;
   1865 
   1866 #if FE_DEBUG >= 4
   1867 	log(LOG_INFO, "%s: feintr()\n", sc->sc_dev.dv_xname);
   1868 	fe_dump(LOG_INFO, sc);
   1869 #endif
   1870 
   1871 	/*
   1872 	 * Get interrupt conditions, masking unneeded flags.
   1873 	 */
   1874 	tstat = inb(sc->sc_iobase + FE_DLCR0) & FE_TMASK;
   1875 	rstat = inb(sc->sc_iobase + FE_DLCR1) & FE_RMASK;
   1876 	if (tstat == 0 && rstat == 0)
   1877 		return (0);
   1878 
   1879 	/*
   1880 	 * Loop until there are no more new interrupt conditions.
   1881 	 */
   1882 	for (;;) {
   1883 		/*
   1884 		 * Reset the conditions we are acknowledging.
   1885 		 */
   1886 		outb(sc->sc_iobase + FE_DLCR0, tstat);
   1887 		outb(sc->sc_iobase + FE_DLCR1, rstat);
   1888 
   1889 		/*
   1890 		 * Handle transmitter interrupts. Handle these first because
   1891 		 * the receiver will reset the board under some conditions.
   1892 		 */
   1893 		if (tstat != 0)
   1894 			fe_tint(sc, tstat);
   1895 
   1896 		/*
   1897 		 * Handle receiver interrupts.
   1898 		 */
   1899 		if (rstat != 0)
   1900 			fe_rint(sc, rstat);
   1901 
   1902 		/*
   1903 		 * Update the multicast address filter if it is
   1904 		 * needed and possible.  We do it now, because
   1905 		 * we can make sure the transmission buffer is empty,
   1906 		 * and there is a good chance that the receive queue
   1907 		 * is empty.  It will minimize the possibility of
   1908 		 * packet lossage.
   1909 		 */
   1910 		if (sc->filter_change &&
   1911 		    sc->txb_count == 0 && sc->txb_sched == 0) {
   1912 			fe_loadmar(sc);
   1913 			sc->sc_arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
   1914 		}
   1915 
   1916 		/*
   1917 		 * If it looks like the transmitter can take more data,
   1918 		 * attempt to start output on the interface. This is done
   1919 		 * after handling the receiver interrupt to give the
   1920 		 * receive operation priority.
   1921 		 */
   1922 		if ((sc->sc_arpcom.ac_if.if_flags & IFF_OACTIVE) == 0)
   1923 			fe_start(&sc->sc_arpcom.ac_if);
   1924 
   1925 		/*
   1926 		 * Get interrupt conditions, masking unneeded flags.
   1927 		 */
   1928 		tstat = inb(sc->sc_iobase + FE_DLCR0) & FE_TMASK;
   1929 		rstat = inb(sc->sc_iobase + FE_DLCR1) & FE_RMASK;
   1930 		if (tstat == 0 && rstat == 0)
   1931 			return (1);
   1932 	}
   1933 }
   1934 
   1935 /*
   1936  * Process an ioctl request.  This code needs some work - it looks pretty ugly.
   1937  */
   1938 int
   1939 fe_ioctl(ifp, command, data)
   1940 	register struct ifnet *ifp;
   1941 	u_long command;
   1942 	caddr_t data;
   1943 {
   1944 	struct fe_softc *sc = fecd.cd_devs[ifp->if_unit];
   1945 	register struct ifaddr *ifa = (struct ifaddr *)data;
   1946 	struct ifreq *ifr = (struct ifreq *)data;
   1947 	int s, error = 0;
   1948 
   1949 #if FE_DEBUG >= 3
   1950 	log(LOG_INFO, "%s: ioctl(%x)\n", sc->sc_dev.dv_xname, command);
   1951 #endif
   1952 
   1953 	s = splimp();
   1954 
   1955 	switch (command) {
   1956 
   1957 	case SIOCSIFADDR:
   1958 		ifp->if_flags |= IFF_UP;
   1959 
   1960 		switch (ifa->ifa_addr->sa_family) {
   1961 #ifdef INET
   1962 		case AF_INET:
   1963 			fe_init(sc);
   1964 			arp_ifinit(&sc->sc_arpcom, ifa);
   1965 			break;
   1966 #endif
   1967 #ifdef NS
   1968 		case AF_NS:
   1969 		    {
   1970 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1971 
   1972 			if (ns_nullhost(*ina))
   1973 				ina->x_host =
   1974 				    *(union ns_host *)(sc->sc_arpcom.ac_enaddr);
   1975 			else
   1976 				bcopy(ina->x_host.c_host,
   1977 				    sc->sc_arpcom.ac_enaddr,
   1978 				    sizeof(sc->sc_arpcom.ac_enaddr));
   1979 			/* Set new address. */
   1980 			fe_init(sc);
   1981 			break;
   1982 		    }
   1983 #endif
   1984 		default:
   1985 			fe_init(sc);
   1986 			break;
   1987 		}
   1988 		break;
   1989 
   1990 	case SIOCSIFFLAGS:
   1991 		if ((ifp->if_flags & IFF_UP) == 0 &&
   1992 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   1993 			/*
   1994 			 * If interface is marked down and it is running, then
   1995 			 * stop it.
   1996 			 */
   1997 			fe_stop(sc);
   1998 			ifp->if_flags &= ~IFF_RUNNING;
   1999 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
   2000 			   (ifp->if_flags & IFF_RUNNING) == 0) {
   2001 			/*
   2002 			 * If interface is marked up and it is stopped, then
   2003 			 * start it.
   2004 			 */
   2005 			fe_init(sc);
   2006 		} else {
   2007 			/*
   2008 			 * Reset the interface to pick up changes in any other
   2009 			 * flags that affect hardware registers.
   2010 			 */
   2011 			fe_setmode(sc);
   2012 		}
   2013 #if DEBUG >= 1
   2014 		/* "ifconfig fe0 debug" to print register dump. */
   2015 		if (ifp->if_flags & IFF_DEBUG) {
   2016 			log(LOG_INFO, "%s: SIOCSIFFLAGS(DEBUG)\n", sc->sc_dev.dv_xname);
   2017 			fe_dump(LOG_DEBUG, sc);
   2018 		}
   2019 #endif
   2020 		break;
   2021 
   2022 	case SIOCADDMULTI:
   2023 	case SIOCDELMULTI:
   2024 		/* Update our multicast list. */
   2025 		error = (command == SIOCADDMULTI) ?
   2026 		    ether_addmulti(ifr, &sc->sc_arpcom) :
   2027 		    ether_delmulti(ifr, &sc->sc_arpcom);
   2028 
   2029 		if (error == ENETRESET) {
   2030 			/*
   2031 			 * Multicast list has changed; set the hardware filter
   2032 			 * accordingly.
   2033 			 */
   2034 			fe_setmode(sc);
   2035 			error = 0;
   2036 		}
   2037 		break;
   2038 
   2039 	default:
   2040 		error = EINVAL;
   2041 	}
   2042 
   2043 	splx(s);
   2044 	return (error);
   2045 }
   2046 
   2047 /*
   2048  * Retreive packet from receive buffer and send to the next level up via
   2049  * ether_input(). If there is a BPF listener, give a copy to BPF, too.
   2050  * Returns 0 if success, -1 if error (i.e., mbuf allocation failure).
   2051  */
   2052 int
   2053 fe_get_packet(sc, len)
   2054 	struct fe_softc *sc;
   2055 	int len;
   2056 {
   2057 	struct ether_header *eh;
   2058 	struct mbuf *m;
   2059 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
   2060 
   2061 	/* Allocate a header mbuf. */
   2062 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   2063 	if (m == 0)
   2064 		return (0);
   2065 	m->m_pkthdr.rcvif = ifp;
   2066 	m->m_pkthdr.len = len;
   2067 
   2068 	/* The following silliness is to make NFS happy. */
   2069 #define	EROUND	((sizeof(struct ether_header) + 3) & ~3)
   2070 #define	EOFF	(EROUND - sizeof(struct ether_header))
   2071 
   2072 #if 0
   2073 	/*
   2074 	 * This function assumes that an Ethernet packet fits in an
   2075 	 * mbuf (with a cluster attached when necessary.)  On FreeBSD
   2076 	 * 2.0 for x86, which is the primary target of this driver, an
   2077 	 * mbuf cluster has 4096 bytes, and we are happy.  On ancient
   2078 	 * BSDs, such as vanilla 4.3 for 386, a cluster size was 1024,
   2079 	 * however.  If the following #error message were printed upon
   2080 	 * compile, you need to rewrite this function.
   2081 	 */
   2082 #if (MCLBYTES < ETHER_MAX_LEN + EOFF)
   2083 #error "Too small MCLBYTES to use fe driver."
   2084 #endif
   2085 #endif
   2086 
   2087 	/*
   2088 	 * Our strategy has one more problem.  There is a policy on
   2089 	 * mbuf cluster allocation.  It says that we must have at
   2090 	 * least MINCLSIZE (208 bytes on FreeBSD 2.0 for x86) to
   2091 	 * allocate a cluster.  For a packet of a size between
   2092 	 * (MHLEN - 2) to (MINCLSIZE - 2), our code violates the rule...
   2093 	 * On the other hand, the current code is short, simle,
   2094 	 * and fast, however.  It does no harmful thing, just waists
   2095 	 * some memory.  Any comments?  FIXME.
   2096 	 */
   2097 
   2098 	/* Attach a cluster if this packet doesn't fit in a normal mbuf. */
   2099 	if (len > MHLEN - EOFF) {
   2100 		MCLGET(m, M_DONTWAIT);
   2101 		if ((m->m_flags & M_EXT) == 0) {
   2102 			m_freem(m);
   2103 			return (0);
   2104 		}
   2105 	}
   2106 
   2107 	/*
   2108 	 * The following assumes there is room for the ether header in the
   2109 	 * header mbuf.
   2110 	 */
   2111 	m->m_data += EOFF;
   2112 	eh = mtod(m, struct ether_header *);
   2113 
   2114 	/* Set the length of this packet. */
   2115 	m->m_len = len;
   2116 
   2117 	/* Get a packet. */
   2118 	insw(sc->sc_iobase + FE_BMPR8, m->m_data, (len + 1) >> 1);
   2119 
   2120 #if NBPFILTER > 0
   2121 	/*
   2122 	 * Check if there's a BPF listener on this interface.  If so, hand off
   2123 	 * the raw packet to bpf.
   2124 	 */
   2125 	if (ifp->if_bpf) {
   2126 		bpf_mtap(ifp->if_bpf, m);
   2127 
   2128 		/*
   2129 		 * Note that the interface cannot be in promiscuous mode if
   2130 		 * there are no BPF listeners.  And if we are in promiscuous
   2131 		 * mode, we have to check if this packet is really ours.
   2132 		 */
   2133 		if ((ifp->if_flags & IFF_PROMISC) != 0 &&
   2134 		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
   2135 	  	    bcmp(eh->ether_dhost, sc->sc_arpcom.ac_enaddr,
   2136 			    sizeof(eh->ether_dhost)) != 0) {
   2137 			m_freem(m);
   2138 			return (1);
   2139 		}
   2140 	}
   2141 #endif
   2142 
   2143 	/* Fix up data start offset in mbuf to point past ether header. */
   2144 	m_adj(m, sizeof(struct ether_header));
   2145 	ether_input(ifp, eh, m);
   2146 	return (1);
   2147 }
   2148 
   2149 /*
   2150  * Write an mbuf chain to the transmission buffer memory using 16 bit PIO.
   2151  * Returns number of bytes actually written, including length word.
   2152  *
   2153  * If an mbuf chain is too long for an Ethernet frame, it is not sent.
   2154  * Packets shorter than Ethernet minimum are legal, and we pad them
   2155  * before sending out.  An exception is "partial" packets which are
   2156  * shorter than mandatory Ethernet header.
   2157  *
   2158  * I wrote a code for an experimental "delayed padding" technique.
   2159  * When employed, it postpones the padding process for short packets.
   2160  * If xmit() occured at the moment, the padding process is omitted, and
   2161  * garbages are sent as pad data.  If next packet is stored in the
   2162  * transmission buffer before xmit(), write_mbuf() pads the previous
   2163  * packet before transmitting new packet.  This *may* gain the
   2164  * system performance (slightly).
   2165  */
   2166 void
   2167 fe_write_mbufs(sc, m)
   2168 	struct fe_softc *sc;
   2169 	struct mbuf *m;
   2170 {
   2171 	int bmpr8 = sc->sc_iobase + FE_BMPR8;
   2172 	struct mbuf *mp;
   2173 	u_char *data;
   2174 	u_short savebyte;	/* WARNING: Architecture dependent! */
   2175 	int totlen, len, wantbyte;
   2176 
   2177 #if FE_DELAYED_PADDING
   2178 	/* Do the "delayed padding." */
   2179 	len = sc->txb_padding >> 1;
   2180 	if (len > 0) {
   2181 		while (--len >= 0)
   2182 			outw(bmpr8, 0);
   2183 		sc->txb_padding = 0;
   2184 	}
   2185 #endif
   2186 
   2187 #if FE_DEBUG >= 2
   2188 	/* First, count up the total number of bytes to copy. */
   2189 	for (totlen = 0, mp = m; mp != 0; mp = mp->m_next)
   2190 		totlen += mp->m_len;
   2191 	/* Check if this matches the one in the packet header. */
   2192 	if (totlen != m->m_pkthdr.len)
   2193 		log(LOG_WARNING, "%s: packet length mismatch? (%d/%d)\n",
   2194 		    sc->sc_dev.dv_xname, totlen, m->m_pkthdr.len);
   2195 #else
   2196 	/* Just use the length value in the packet header. */
   2197 	totlen = m->m_pkthdr.len;
   2198 #endif
   2199 
   2200 #if FE_DEBUG >= 1
   2201 	/*
   2202 	 * Should never send big packets.  If such a packet is passed,
   2203 	 * it should be a bug of upper layer.  We just ignore it.
   2204 	 * ... Partial (too short) packets, neither.
   2205 	 */
   2206 	if (totlen > ETHER_MAX_LEN || totlen < ETHER_HDR_SIZE) {
   2207 		log(LOG_ERR, "%s: got a %s packet (%u bytes) to send\n",
   2208 		    sc->sc_dev.dv_xname,
   2209 		    totlen < ETHER_HDR_SIZE ? "partial" : "big", totlen);
   2210 		sc->sc_arpcom.ac_if.if_oerrors++;
   2211 		return;
   2212 	}
   2213 #endif
   2214 
   2215 	/*
   2216 	 * Put the length word for this frame.
   2217 	 * Does 86960 accept odd length?  -- Yes.
   2218 	 * Do we need to pad the length to minimum size by ourselves?
   2219 	 * -- Generally yes.  But for (or will be) the last
   2220 	 * packet in the transmission buffer, we can skip the
   2221 	 * padding process.  It may gain performance slightly.  FIXME.
   2222 	 */
   2223 	outw(bmpr8, max(totlen, ETHER_MIN_LEN));
   2224 
   2225 	/*
   2226 	 * Update buffer status now.
   2227 	 * Truncate the length up to an even number, since we use outw().
   2228 	 */
   2229 	totlen = (totlen + 1) & ~1;
   2230 	sc->txb_free -= FE_DATA_LEN_LEN + max(totlen, ETHER_MIN_LEN);
   2231 	sc->txb_count++;
   2232 
   2233 #if FE_DELAYED_PADDING
   2234 	/* Postpone the packet padding if necessary. */
   2235 	if (totlen < ETHER_MIN_LEN)
   2236 		sc->txb_padding = ETHER_MIN_LEN - totlen;
   2237 #endif
   2238 
   2239 	/*
   2240 	 * Transfer the data from mbuf chain to the transmission buffer.
   2241 	 * MB86960 seems to require that data be transferred as words, and
   2242 	 * only words.  So that we require some extra code to patch
   2243 	 * over odd-length mbufs.
   2244 	 */
   2245 	wantbyte = 0;
   2246 	for (; m != 0; m = m->m_next) {
   2247 		/* Ignore empty mbuf. */
   2248 		len = m->m_len;
   2249 		if (len == 0)
   2250 			continue;
   2251 
   2252 		/* Find the actual data to send. */
   2253 		data = mtod(m, caddr_t);
   2254 
   2255 		/* Finish the last byte. */
   2256 		if (wantbyte) {
   2257 			outw(bmpr8, savebyte | (*data << 8));
   2258 			data++;
   2259 			len--;
   2260 			wantbyte = 0;
   2261 		}
   2262 
   2263 		/* Output contiguous words. */
   2264 		if (len > 1)
   2265 			outsw(bmpr8, data, len >> 1);
   2266 
   2267 		/* Save remaining byte, if there is one. */
   2268 		if (len & 1) {
   2269 			data += len & ~1;
   2270 			savebyte = *data;
   2271 			wantbyte = 1;
   2272 		}
   2273 	}
   2274 
   2275 	/* Spit the last byte, if the length is odd. */
   2276 	if (wantbyte)
   2277 		outw(bmpr8, savebyte);
   2278 
   2279 #if ! FE_DELAYED_PADDING
   2280 	/*
   2281 	 * Pad the packet to the minimum length if necessary.
   2282 	 */
   2283 	len = (ETHER_MIN_LEN >> 1) - (totlen >> 1);
   2284 	while (--len >= 0)
   2285 		outw(bmpr8, 0);
   2286 #endif
   2287 }
   2288 
   2289 /*
   2290  * Compute the multicast address filter from the
   2291  * list of multicast addresses we need to listen to.
   2292  */
   2293 void
   2294 fe_getmcaf(ac, af)
   2295 	struct arpcom *ac;
   2296 	u_char *af;
   2297 {
   2298 	struct ifnet *ifp = &ac->ac_if;
   2299 	struct ether_multi *enm;
   2300 	register u_char *cp, c;
   2301 	register u_long crc;
   2302 	register int i, len;
   2303 	struct ether_multistep step;
   2304 
   2305 	/*
   2306 	 * Set up multicast address filter by passing all multicast addresses
   2307 	 * through a crc generator, and then using the high order 6 bits as an
   2308 	 * index into the 64 bit logical address filter.  The high order bit
   2309 	 * selects the word, while the rest of the bits select the bit within
   2310 	 * the word.
   2311 	 */
   2312 
   2313 	if ((ifp->if_flags & IFF_PROMISC) != 0)
   2314 		goto allmulti;
   2315 
   2316 	af[0] = af[1] = af[2] = af[3] = af[4] = af[5] = af[6] = af[7] = 0x00;
   2317 	ETHER_FIRST_MULTI(step, ac, enm);
   2318 	while (enm != NULL) {
   2319 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
   2320 		    sizeof(enm->enm_addrlo)) != 0) {
   2321 			/*
   2322 			 * We must listen to a range of multicast addresses.
   2323 			 * For now, just accept all multicasts, rather than
   2324 			 * trying to set only those filter bits needed to match
   2325 			 * the range.  (At this time, the only use of address
   2326 			 * ranges is for IP multicast routing, for which the
   2327 			 * range is big enough to require all bits set.)
   2328 			 */
   2329 			goto allmulti;
   2330 		}
   2331 
   2332 		cp = enm->enm_addrlo;
   2333 		crc = 0xffffffff;
   2334 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
   2335 			c = *cp++;
   2336 			for (i = 8; --i >= 0;) {
   2337 				if ((crc & 0x01) ^ (c & 0x01)) {
   2338 					crc >>= 1;
   2339 					crc ^= 0xedb88320;
   2340 				} else
   2341 					crc >>= 1;
   2342 				c >>= 1;
   2343 			}
   2344 		}
   2345 		/* Just want the 6 most significant bits. */
   2346 		crc >>= 26;
   2347 
   2348 		/* Turn on the corresponding bit in the filter. */
   2349 		af[crc >> 3] |= 1 << (crc & 7);
   2350 
   2351 		ETHER_NEXT_MULTI(step, enm);
   2352 	}
   2353 	ifp->if_flags &= ~IFF_ALLMULTI;
   2354 	return;
   2355 
   2356 allmulti:
   2357 	ifp->if_flags |= IFF_ALLMULTI;
   2358 	af[0] = af[1] = af[2] = af[3] = af[4] = af[5] = af[6] = af[7] = 0xff;
   2359 }
   2360 
   2361 /*
   2362  * Calculate a new "multicast packet filter" and put the 86960
   2363  * receiver in appropriate mode.
   2364  */
   2365 void
   2366 fe_setmode(sc)
   2367 	struct fe_softc *sc;
   2368 {
   2369 	int flags = sc->sc_arpcom.ac_if.if_flags;
   2370 
   2371 	/*
   2372 	 * If the interface is not running, we postpone the update
   2373 	 * process for receive modes and multicast address filter
   2374 	 * until the interface is restarted.  It reduces some
   2375 	 * complicated job on maintaining chip states.  (Earlier versions
   2376 	 * of this driver had a bug on that point...)
   2377 	 *
   2378 	 * To complete the trick, fe_init() calls fe_setmode() after
   2379 	 * restarting the interface.
   2380 	 */
   2381 	if ((flags & IFF_RUNNING) == 0)
   2382 		return;
   2383 
   2384 	/*
   2385 	 * Promiscuous mode is handled separately.
   2386 	 */
   2387 	if ((flags & IFF_PROMISC) != 0) {
   2388 		/*
   2389 		 * Program 86960 to receive all packets on the segment
   2390 		 * including those directed to other stations.
   2391 		 * Multicast filter stored in MARs are ignored
   2392 		 * under this setting, so we don't need to update it.
   2393 		 *
   2394 		 * Promiscuous mode in FreeBSD 2 is used solely by
   2395 		 * BPF, and BPF only listens to valid (no error) packets.
   2396 		 * So, we ignore errornous ones even in this mode.
   2397 		 * (Older versions of fe driver mistook the point.)
   2398 		 */
   2399 		outb(sc->sc_iobase + FE_DLCR5,
   2400 		    sc->proto_dlcr5 | FE_D5_AFM0 | FE_D5_AFM1);
   2401 		sc->filter_change = 0;
   2402 
   2403 #if FE_DEBUG >= 3
   2404 		log(LOG_INFO, "%s: promiscuous mode\n", sc->sc_dev.dv_xname);
   2405 #endif
   2406 		return;
   2407 	}
   2408 
   2409 	/*
   2410 	 * Turn the chip to the normal (non-promiscuous) mode.
   2411 	 */
   2412 	outb(sc->sc_iobase + FE_DLCR5, sc->proto_dlcr5 | FE_D5_AFM1);
   2413 
   2414 	/*
   2415 	 * Find the new multicast filter value.
   2416 	 */
   2417 	fe_getmcaf(&sc->sc_arpcom, sc->filter);
   2418 	sc->filter_change = 1;
   2419 
   2420 #if FE_DEBUG >= 3
   2421 	log(LOG_INFO,
   2422 	    "%s: address filter: [%02x %02x %02x %02x %02x %02x %02x %02x]\n",
   2423 	    sc->sc_dev.dv_xname,
   2424 	    sc->filter[0], sc->filter[1], sc->filter[2], sc->filter[3],
   2425 	    sc->filter[4], sc->filter[5], sc->filter[6], sc->filter[7]);
   2426 #endif
   2427 
   2428 	/*
   2429 	 * We have to update the multicast filter in the 86960, A.S.A.P.
   2430 	 *
   2431 	 * Note that the DLC (Data Linc Control unit, i.e. transmitter
   2432 	 * and receiver) must be stopped when feeding the filter, and
   2433 	 * DLC trushes all packets in both transmission and receive
   2434 	 * buffers when stopped.
   2435 	 *
   2436 	 * ... Are the above sentenses correct?  I have to check the
   2437 	 *     manual of the MB86960A.  FIXME.
   2438 	 *
   2439 	 * To reduce the packet lossage, we delay the filter update
   2440 	 * process until buffers are empty.
   2441 	 */
   2442 	if (sc->txb_sched == 0 && sc->txb_count == 0 &&
   2443 	    (inb(sc->sc_iobase + FE_DLCR1) & FE_D1_PKTRDY) == 0) {
   2444 		/*
   2445 		 * Buffers are (apparently) empty.  Load
   2446 		 * the new filter value into MARs now.
   2447 		 */
   2448 		fe_loadmar(sc);
   2449 	} else {
   2450 		/*
   2451 		 * Buffers are not empty.  Mark that we have to update
   2452 		 * the MARs.  The new filter will be loaded by feintr()
   2453 		 * later.
   2454 		 */
   2455 #if FE_DEBUG >= 4
   2456 		log(LOG_INFO, "%s: filter change delayed\n", sc->sc_dev.dv_xname);
   2457 #endif
   2458 	}
   2459 }
   2460 
   2461 /*
   2462  * Load a new multicast address filter into MARs.
   2463  *
   2464  * The caller must have splimp'ed befor fe_loadmar.
   2465  * This function starts the DLC upon return.  So it can be called only
   2466  * when the chip is working, i.e., from the driver's point of view, when
   2467  * a device is RUNNING.  (I mistook the point in previous versions.)
   2468  */
   2469 void
   2470 fe_loadmar(sc)
   2471 	struct fe_softc *sc;
   2472 {
   2473 
   2474 	/* Stop the DLC (transmitter and receiver). */
   2475 	outb(sc->sc_iobase + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
   2476 
   2477 	/* Select register bank 1 for MARs. */
   2478 	outb(sc->sc_iobase + FE_DLCR7,
   2479 	    sc->proto_dlcr7 | FE_D7_RBS_MAR | FE_D7_POWER_UP);
   2480 
   2481 	/* Copy filter value into the registers. */
   2482 	outblk(sc->sc_iobase + FE_MAR8, sc->filter, FE_FILTER_LEN);
   2483 
   2484 	/* Restore the bank selection for BMPRs (i.e., runtime registers). */
   2485 	outb(sc->sc_iobase + FE_DLCR7,
   2486 	    sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP);
   2487 
   2488 	/* Restart the DLC. */
   2489 	outb(sc->sc_iobase + FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_ENABLE);
   2490 
   2491 	/* We have just updated the filter. */
   2492 	sc->filter_change = 0;
   2493 
   2494 #if FE_DEBUG >= 3
   2495 	log(LOG_INFO, "%s: address filter changed\n", sc->sc_dev.dv_xname);
   2496 #endif
   2497 }
   2498 
   2499 #if FE_DEBUG >= 1
   2500 void
   2501 fe_dump(level, sc)
   2502 	int level;
   2503 	struct fe_softc *sc;
   2504 {
   2505 	int iobase = sc->sc_iobase;
   2506 	u_char save_dlcr7;
   2507 
   2508 	save_dlcr7 = inb(iobase + FE_DLCR7);
   2509 
   2510 	log(level, "\tDLCR = %02x %02x %02x %02x %02x %02x %02x %02x",
   2511 	    inb(iobase + FE_DLCR0),  inb(iobase + FE_DLCR1),
   2512 	    inb(iobase + FE_DLCR2),  inb(iobase + FE_DLCR3),
   2513 	    inb(iobase + FE_DLCR4),  inb(iobase + FE_DLCR5),
   2514 	    inb(iobase + FE_DLCR6),  inb(iobase + FE_DLCR7));
   2515 
   2516 	outb(iobase + FE_DLCR7, (save_dlcr7 & ~FE_D7_RBS) | FE_D7_RBS_DLCR);
   2517 	log(level, "\t       %02x %02x %02x %02x %02x %02x %02x %02x,",
   2518 	    inb(iobase + FE_DLCR8),  inb(iobase + FE_DLCR9),
   2519 	    inb(iobase + FE_DLCR10), inb(iobase + FE_DLCR11),
   2520 	    inb(iobase + FE_DLCR12), inb(iobase + FE_DLCR13),
   2521 	    inb(iobase + FE_DLCR14), inb(iobase + FE_DLCR15));
   2522 
   2523 	outb(iobase + FE_DLCR7, (save_dlcr7 & ~FE_D7_RBS) | FE_D7_RBS_MAR);
   2524 	log(level, "\tMAR  = %02x %02x %02x %02x %02x %02x %02x %02x,",
   2525 	    inb(iobase + FE_MAR8),   inb(iobase + FE_MAR9),
   2526 	    inb(iobase + FE_MAR10),  inb(iobase + FE_MAR11),
   2527 	    inb(iobase + FE_MAR12),  inb(iobase + FE_MAR13),
   2528 	    inb(iobase + FE_MAR14),  inb(iobase + FE_MAR15));
   2529 
   2530 	outb(iobase + FE_DLCR7, (save_dlcr7 & ~FE_D7_RBS) | FE_D7_RBS_BMPR);
   2531 	log(level, "\tBMPR = xx xx %02x %02x %02x %02x %02x %02x %02x %02x xx %02x.",
   2532 	    inb(iobase + FE_BMPR10), inb(iobase + FE_BMPR11),
   2533 	    inb(iobase + FE_BMPR12), inb(iobase + FE_BMPR13),
   2534 	    inb(iobase + FE_BMPR14), inb(iobase + FE_BMPR15),
   2535 	    inb(iobase + FE_BMPR16), inb(iobase + FE_BMPR17),
   2536 	    inb(iobase + FE_BMPR19));
   2537 
   2538 	outb(iobase + FE_DLCR7, save_dlcr7);
   2539 }
   2540 #endif
   2541