Home | History | Annotate | Line # | Download | only in ic
mb86960.c revision 1.93
      1 /*	$NetBSD: mb86960.c,v 1.93 2019/05/29 10:07:29 msaitoh Exp $	*/
      2 
      3 /*
      4  * All Rights Reserved, Copyright (C) Fujitsu Limited 1995
      5  *
      6  * This software may be used, modified, copied, distributed, and sold, in
      7  * both source and binary form provided that the above copyright, these
      8  * terms and the following disclaimer are retained.  The name of the author
      9  * and/or the contributor may not be used to endorse or promote products
     10  * derived from this software without specific prior written permission.
     11  *
     12  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND THE CONTRIBUTOR ``AS IS'' AND
     13  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     14  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     15  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR THE CONTRIBUTOR BE LIABLE
     16  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     17  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     18  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION.
     19  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     20  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     21  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     22  * SUCH DAMAGE.
     23  */
     24 
     25 /*
     26  * Portions copyright (C) 1993, David Greenman.  This software may be used,
     27  * modified, copied, distributed, and sold, in both source and binary form
     28  * provided that the above copyright and these terms are retained.  Under no
     29  * circumstances is the author responsible for the proper functioning of this
     30  * software, nor does the author assume any responsibility for damages
     31  * incurred with its use.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: mb86960.c,v 1.93 2019/05/29 10:07:29 msaitoh Exp $");
     36 
     37 /*
     38  * Device driver for Fujitsu MB86960A/MB86965A based Ethernet cards.
     39  * Contributed by M.S. <seki (at) sysrap.cs.fujitsu.co.jp>
     40  *
     41  * This version is intended to be a generic template for various
     42  * MB86960A/MB86965A based Ethernet cards.  It currently supports
     43  * Fujitsu FMV-180 series (i.e., FMV-181 and FMV-182) and Allied-
     44  * Telesis AT1700 series and RE2000 series.  There are some
     45  * unnecessary hooks embedded, which are primarily intended to support
     46  * other types of Ethernet cards, but the author is not sure whether
     47  * they are useful.
     48  */
     49 
     50 #include "opt_inet.h"
     51 
     52 #include <sys/param.h>
     53 #include <sys/systm.h>
     54 #include <sys/errno.h>
     55 #include <sys/ioctl.h>
     56 #include <sys/mbuf.h>
     57 #include <sys/socket.h>
     58 #include <sys/syslog.h>
     59 #include <sys/device.h>
     60 #include <sys/rndsource.h>
     61 #include <sys/bus.h>
     62 
     63 #include <net/if.h>
     64 #include <net/if_dl.h>
     65 #include <net/if_types.h>
     66 #include <net/if_media.h>
     67 #include <net/if_ether.h>
     68 #include <net/bpf.h>
     69 
     70 #ifdef INET
     71 #include <netinet/in.h>
     72 #include <netinet/in_systm.h>
     73 #include <netinet/in_var.h>
     74 #include <netinet/ip.h>
     75 #include <netinet/if_inarp.h>
     76 #endif
     77 
     78 #include <dev/ic/mb86960reg.h>
     79 #include <dev/ic/mb86960var.h>
     80 
     81 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
     82 #define bus_space_write_stream_2	bus_space_write_2
     83 #define bus_space_write_multi_stream_2	bus_space_write_multi_2
     84 #define bus_space_read_multi_stream_2	bus_space_read_multi_2
     85 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
     86 
     87 /* Standard driver entry points.  These can be static. */
     88 void	mb86960_init(struct mb86960_softc *);
     89 int	mb86960_ioctl(struct ifnet *, u_long, void *);
     90 void	mb86960_start(struct ifnet *);
     91 void	mb86960_reset(struct mb86960_softc *);
     92 void	mb86960_watchdog(struct ifnet *);
     93 
     94 /* Local functions.  Order of declaration is confused.  FIXME. */
     95 int	mb86960_get_packet(struct mb86960_softc *, u_int);
     96 void	mb86960_stop(struct mb86960_softc *);
     97 void	mb86960_tint(struct mb86960_softc *, uint8_t);
     98 void	mb86960_rint(struct mb86960_softc *, uint8_t);
     99 static inline
    100 void	mb86960_xmit(struct mb86960_softc *);
    101 void	mb86960_write_mbufs(struct mb86960_softc *, struct mbuf *);
    102 static inline
    103 void	mb86960_droppacket(struct mb86960_softc *);
    104 void	mb86960_getmcaf(struct ethercom *, uint8_t *);
    105 void	mb86960_setmode(struct mb86960_softc *);
    106 void	mb86960_loadmar(struct mb86960_softc *);
    107 
    108 int	mb86960_mediachange(struct ifnet *);
    109 void	mb86960_mediastatus(struct ifnet *, struct ifmediareq *);
    110 
    111 #if FE_DEBUG >= 1
    112 void	mb86960_dump(int, struct mb86960_softc *);
    113 #endif
    114 
    115 void
    116 mb86960_attach(struct mb86960_softc *sc, uint8_t *myea)
    117 {
    118 	bus_space_tag_t bst = sc->sc_bst;
    119 	bus_space_handle_t bsh = sc->sc_bsh;
    120 
    121 	/* Register values which depend on board design. */
    122 	sc->proto_dlcr4 = FE_D4_LBC_DISABLE | FE_D4_CNTRL;
    123 	sc->proto_dlcr5 = 0;
    124 	sc->proto_dlcr7 = FE_D7_BYTSWP_LH;
    125 	if ((sc->sc_flags & FE_FLAGS_MB86960) != 0)
    126 		sc->proto_dlcr7 |= FE_D7_ED_TEST; /* XXX */
    127 	sc->proto_bmpr13 = FE_B13_TPTYPE_UTP | FE_B13_PORT_AUTO;
    128 
    129 	/*
    130 	 * Program the 86960 as following defaults:
    131 	 *	SRAM: 32KB, 100ns, byte-wide access.
    132 	 *	Transmission buffer: 4KB x 2.
    133 	 *	System bus interface: 16 bits.
    134 	 * These values except TXBSIZE should be modified as per
    135 	 * sc_flags which is set in MD attachments, because they
    136 	 * are hard-wired on the board. Modifying TXBSIZE will affect
    137 	 * the driver performance.
    138 	 */
    139 	sc->proto_dlcr6 = FE_D6_BUFSIZ_32KB | FE_D6_TXBSIZ_2x4KB |
    140 	    FE_D6_BBW_BYTE | FE_D6_SRAM_100ns;
    141 	if (sc->sc_flags & FE_FLAGS_SBW_BYTE)
    142 		sc->proto_dlcr6 |= FE_D6_SBW_BYTE;
    143 	if (sc->sc_flags & FE_FLAGS_SRAM_150ns)
    144 		sc->proto_dlcr6 &= ~FE_D6_SRAM_100ns;
    145 
    146 	/*
    147 	 * Minimum initialization of the hardware.
    148 	 * We write into registers; hope I/O ports have no
    149 	 * overlap with other boards.
    150 	 */
    151 
    152 	/* Initialize 86960. */
    153 	bus_space_write_1(bst, bsh, FE_DLCR6,
    154 	    sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
    155 	delay(200);
    156 
    157 #ifdef DIAGNOSTIC
    158 	if (myea == NULL) {
    159 		aprint_error_dev(sc->sc_dev,
    160 		    "ethernet address shouldn't be NULL\n");
    161 		panic("NULL ethernet address");
    162 	}
    163 #endif
    164 	memcpy(sc->sc_enaddr, myea, sizeof(sc->sc_enaddr));
    165 
    166 	/* Disable all interrupts. */
    167 	bus_space_write_1(bst, bsh, FE_DLCR2, 0);
    168 	bus_space_write_1(bst, bsh, FE_DLCR3, 0);
    169 }
    170 
    171 /*
    172  * Install interface into kernel networking data structures
    173  */
    174 void
    175 mb86960_config(struct mb86960_softc *sc, int *media, int nmedia, int defmedia)
    176 {
    177 	cfdata_t cf = device_cfdata(sc->sc_dev);
    178 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    179 	int i;
    180 
    181 	/* Stop the 86960. */
    182 	mb86960_stop(sc);
    183 
    184 	/* Initialize ifnet structure. */
    185 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
    186 	ifp->if_softc = sc;
    187 	ifp->if_start = mb86960_start;
    188 	ifp->if_ioctl = mb86960_ioctl;
    189 	ifp->if_watchdog = mb86960_watchdog;
    190 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    191 	IFQ_SET_READY(&ifp->if_snd);
    192 
    193 #if FE_DEBUG >= 3
    194 	log(LOG_INFO, "%s: mb86960_config()\n", device_xname(sc->sc_dev));
    195 	mb86960_dump(LOG_INFO, sc);
    196 #endif
    197 
    198 #if FE_SINGLE_TRANSMISSION
    199 	/* Override txb config to allocate minimum. */
    200 	sc->proto_dlcr6 &= ~FE_D6_TXBSIZ;
    201 	sc->proto_dlcr6 |=  FE_D6_TXBSIZ_2x2KB;
    202 #endif
    203 
    204 	/* Modify hardware config if it is requested. */
    205 	if ((cf->cf_flags & FE_FLAGS_OVERRIDE_DLCR6) != 0)
    206 		sc->proto_dlcr6 = cf->cf_flags & FE_FLAGS_DLCR6_VALUE;
    207 
    208 	/* Find TX buffer size, based on the hardware dependent proto. */
    209 	switch (sc->proto_dlcr6 & FE_D6_TXBSIZ) {
    210 	case FE_D6_TXBSIZ_2x2KB:
    211 		sc->txb_size = 2048;
    212 		break;
    213 	case FE_D6_TXBSIZ_2x4KB:
    214 		sc->txb_size = 4096;
    215 		break;
    216 	case FE_D6_TXBSIZ_2x8KB:
    217 		sc->txb_size = 8192;
    218 		break;
    219 	default:
    220 		/* Oops, we can't work with single buffer configuration. */
    221 #if FE_DEBUG >= 2
    222 		log(LOG_WARNING, "%s: strange TXBSIZ config; fixing\n",
    223 		    device_xname(sc->sc_dev));
    224 #endif
    225 		sc->proto_dlcr6 &= ~FE_D6_TXBSIZ;
    226 		sc->proto_dlcr6 |=  FE_D6_TXBSIZ_2x2KB;
    227 		sc->txb_size = 2048;
    228 		break;
    229 	}
    230 
    231 	/* Initialize media goo. */
    232 	sc->sc_ec.ec_ifmedia = &sc->sc_media;
    233 	ifmedia_init(&sc->sc_media, 0, mb86960_mediachange,
    234 	    mb86960_mediastatus);
    235 	if (media != NULL) {
    236 		for (i = 0; i < nmedia; i++)
    237 			ifmedia_add(&sc->sc_media, media[i], 0, NULL);
    238 		ifmedia_set(&sc->sc_media, defmedia);
    239 	} else {
    240 		ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_MANUAL, 0, NULL);
    241 		ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_MANUAL);
    242 	}
    243 
    244 	/* Attach the interface. */
    245 	if_attach(ifp);
    246 	if_deferred_start_init(ifp, NULL);
    247 	ether_ifattach(ifp, sc->sc_enaddr);
    248 
    249 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
    250 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
    251 
    252 	/* Print additional info when attached. */
    253 	aprint_normal_dev(sc->sc_dev, "Ethernet address %s\n",
    254 	    ether_sprintf(sc->sc_enaddr));
    255 
    256 #if FE_DEBUG >= 3
    257 	{
    258 		int buf, txb, bbw, sbw, ram;
    259 
    260 		buf = txb = bbw = sbw = ram = -1;
    261 		switch (sc->proto_dlcr6 & FE_D6_BUFSIZ) {
    262 		case FE_D6_BUFSIZ_8KB:
    263 			buf = 8;
    264 			break;
    265 		case FE_D6_BUFSIZ_16KB:
    266 			buf = 16;
    267 			break;
    268 		case FE_D6_BUFSIZ_32KB:
    269 			buf = 32;
    270 			break;
    271 		case FE_D6_BUFSIZ_64KB:
    272 			buf = 64;
    273 			break;
    274 		}
    275 		switch (sc->proto_dlcr6 & FE_D6_TXBSIZ) {
    276 		case FE_D6_TXBSIZ_2x2KB:
    277 			txb = 2;
    278 			break;
    279 		case FE_D6_TXBSIZ_2x4KB:
    280 			txb = 4;
    281 			break;
    282 		case FE_D6_TXBSIZ_2x8KB:
    283 			txb = 8;
    284 			break;
    285 		}
    286 		switch (sc->proto_dlcr6 & FE_D6_BBW) {
    287 		case FE_D6_BBW_BYTE:
    288 			bbw = 8;
    289 			break;
    290 		case FE_D6_BBW_WORD:
    291 			bbw = 16;
    292 			break;
    293 		}
    294 		switch (sc->proto_dlcr6 & FE_D6_SBW) {
    295 		case FE_D6_SBW_BYTE:
    296 			sbw = 8;
    297 			break;
    298 		case FE_D6_SBW_WORD:
    299 			sbw = 16;
    300 			break;
    301 		}
    302 		switch (sc->proto_dlcr6 & FE_D6_SRAM) {
    303 		case FE_D6_SRAM_100ns:
    304 			ram = 100;
    305 			break;
    306 		case FE_D6_SRAM_150ns:
    307 			ram = 150;
    308 			break;
    309 		}
    310 		aprint_debug_dev(sc->sc_dev,
    311 		    "SRAM %dKB %dbit %dns, TXB %dKBx2, %dbit I/O\n",
    312 		    buf, bbw, ram, txb, sbw);
    313 	}
    314 #endif
    315 
    316 	/* The attach is successful. */
    317 	sc->sc_stat |= FE_STAT_ATTACHED;
    318 }
    319 
    320 /*
    321  * Media change callback.
    322  */
    323 int
    324 mb86960_mediachange(struct ifnet *ifp)
    325 {
    326 	struct mb86960_softc *sc = ifp->if_softc;
    327 
    328 	if (sc->sc_mediachange)
    329 		return (*sc->sc_mediachange)(sc);
    330 	return 0;
    331 }
    332 
    333 /*
    334  * Media status callback.
    335  */
    336 void
    337 mb86960_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
    338 {
    339 	struct mb86960_softc *sc = ifp->if_softc;
    340 
    341 	if ((sc->sc_stat & FE_STAT_ENABLED) == 0) {
    342 		ifmr->ifm_active = IFM_ETHER | IFM_NONE;
    343 		ifmr->ifm_status = 0;
    344 		return;
    345 	}
    346 
    347 	if (sc->sc_mediastatus)
    348 		(*sc->sc_mediastatus)(sc, ifmr);
    349 }
    350 
    351 /*
    352  * Reset interface.
    353  */
    354 void
    355 mb86960_reset(struct mb86960_softc *sc)
    356 {
    357 	int s;
    358 
    359 	s = splnet();
    360 	mb86960_stop(sc);
    361 	mb86960_init(sc);
    362 	splx(s);
    363 }
    364 
    365 /*
    366  * Stop everything on the interface.
    367  *
    368  * All buffered packets, both transmitting and receiving,
    369  * if any, will be lost by stopping the interface.
    370  */
    371 void
    372 mb86960_stop(struct mb86960_softc *sc)
    373 {
    374 	bus_space_tag_t bst = sc->sc_bst;
    375 	bus_space_handle_t bsh = sc->sc_bsh;
    376 
    377 #if FE_DEBUG >= 3
    378 	log(LOG_INFO, "%s: top of mb86960_stop()\n", device_xname(sc->sc_dev));
    379 	mb86960_dump(LOG_INFO, sc);
    380 #endif
    381 
    382 	/* Disable interrupts. */
    383 	bus_space_write_1(bst, bsh, FE_DLCR2, 0x00);
    384 	bus_space_write_1(bst, bsh, FE_DLCR3, 0x00);
    385 
    386 	/* Stop interface hardware. */
    387 	delay(200);
    388 	bus_space_write_1(bst, bsh, FE_DLCR6,
    389 	    sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
    390 	delay(200);
    391 
    392 	/* Clear all interrupt status. */
    393 	bus_space_write_1(bst, bsh, FE_DLCR0, 0xFF);
    394 	bus_space_write_1(bst, bsh, FE_DLCR1, 0xFF);
    395 
    396 	/* Put the chip in stand-by mode. */
    397 	delay(200);
    398 	bus_space_write_1(bst, bsh, FE_DLCR7,
    399 	    sc->proto_dlcr7 | FE_D7_POWER_DOWN);
    400 	delay(200);
    401 
    402 	/* MAR loading can be delayed. */
    403 	sc->filter_change = 0;
    404 
    405 	/* Call a hook. */
    406 	if (sc->stop_card)
    407 		(*sc->stop_card)(sc);
    408 
    409 #if FE_DEBUG >= 3
    410 	log(LOG_INFO, "%s: end of mb86960_stop()\n", device_xname(sc->sc_dev));
    411 	mb86960_dump(LOG_INFO, sc);
    412 #endif
    413 }
    414 
    415 /*
    416  * Device timeout/watchdog routine. Entered if the device neglects to
    417  * generate an interrupt after a transmit has been started on it.
    418  */
    419 void
    420 mb86960_watchdog(struct ifnet *ifp)
    421 {
    422 	struct mb86960_softc *sc = ifp->if_softc;
    423 
    424 	log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
    425 #if FE_DEBUG >= 3
    426 	mb86960_dump(LOG_INFO, sc);
    427 #endif
    428 
    429 	/* Record how many packets are lost by this accident. */
    430 	sc->sc_ec.ec_if.if_oerrors += sc->txb_sched + sc->txb_count;
    431 
    432 	mb86960_reset(sc);
    433 }
    434 
    435 /*
    436  * Drop (skip) a packet from receive buffer in 86960 memory.
    437  */
    438 static inline void
    439 mb86960_droppacket(struct mb86960_softc *sc)
    440 {
    441 	bus_space_tag_t bst = sc->sc_bst;
    442 	bus_space_handle_t bsh = sc->sc_bsh;
    443 
    444 	bus_space_write_1(bst, bsh, FE_BMPR14, FE_B14_FILTER | FE_B14_SKIP);
    445 }
    446 
    447 /*
    448  * Initialize device.
    449  */
    450 void
    451 mb86960_init(struct mb86960_softc *sc)
    452 {
    453 	bus_space_tag_t bst = sc->sc_bst;
    454 	bus_space_handle_t bsh = sc->sc_bsh;
    455 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    456 	int i;
    457 
    458 #if FE_DEBUG >= 3
    459 	log(LOG_INFO, "%s: top of mb86960_init()\n", device_xname(sc->sc_dev));
    460 	mb86960_dump(LOG_INFO, sc);
    461 #endif
    462 
    463 	/* Reset transmitter flags. */
    464 	ifp->if_flags &= ~IFF_OACTIVE;
    465 	ifp->if_timer = 0;
    466 
    467 	sc->txb_free = sc->txb_size;
    468 	sc->txb_count = 0;
    469 	sc->txb_sched = 0;
    470 
    471 	/* Do any card-specific initialization, if applicable. */
    472 	if (sc->init_card)
    473 		(*sc->init_card)(sc);
    474 
    475 #if FE_DEBUG >= 3
    476 	log(LOG_INFO, "%s: after init hook\n", device_xname(sc->sc_dev));
    477 	mb86960_dump(LOG_INFO, sc);
    478 #endif
    479 
    480 	/*
    481 	 * Make sure to disable the chip, also.
    482 	 * This may also help re-programming the chip after
    483 	 * hot insertion of PCMCIAs.
    484 	 */
    485 	bus_space_write_1(bst, bsh, FE_DLCR6,
    486 	    sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
    487 	delay(200);
    488 
    489 	/* Power up the chip and select register bank for DLCRs. */
    490 	bus_space_write_1(bst, bsh, FE_DLCR7,
    491 	    sc->proto_dlcr7 | FE_D7_RBS_DLCR | FE_D7_POWER_UP);
    492 	delay(200);
    493 
    494 	/* Feed the station address. */
    495 	bus_space_write_region_1(bst, bsh, FE_DLCR8,
    496 	    sc->sc_enaddr, ETHER_ADDR_LEN);
    497 
    498 	/* Select the BMPR bank for runtime register access. */
    499 	bus_space_write_1(bst, bsh, FE_DLCR7,
    500 	    sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP);
    501 
    502 	/* Initialize registers. */
    503 	bus_space_write_1(bst, bsh, FE_DLCR0, 0xFF);	/* Clear all bits. */
    504 	bus_space_write_1(bst, bsh, FE_DLCR1, 0xFF);	/* ditto. */
    505 	bus_space_write_1(bst, bsh, FE_DLCR2, 0x00);
    506 	bus_space_write_1(bst, bsh, FE_DLCR3, 0x00);
    507 	bus_space_write_1(bst, bsh, FE_DLCR4, sc->proto_dlcr4);
    508 	bus_space_write_1(bst, bsh, FE_DLCR5, sc->proto_dlcr5);
    509 	bus_space_write_1(bst, bsh, FE_BMPR10, 0x00);
    510 	bus_space_write_1(bst, bsh, FE_BMPR11, FE_B11_CTRL_SKIP);
    511 	bus_space_write_1(bst, bsh, FE_BMPR12, 0x00);
    512 	bus_space_write_1(bst, bsh, FE_BMPR13, sc->proto_bmpr13);
    513 	bus_space_write_1(bst, bsh, FE_BMPR14, FE_B14_FILTER);
    514 	bus_space_write_1(bst, bsh, FE_BMPR15, 0x00);
    515 
    516 #if FE_DEBUG >= 3
    517 	log(LOG_INFO, "%s: just before enabling DLC\n",
    518 	    device_xname(sc->sc_dev));
    519 	mb86960_dump(LOG_INFO, sc);
    520 #endif
    521 
    522 	/* Enable interrupts. */
    523 	bus_space_write_1(bst, bsh, FE_DLCR2, FE_TMASK);
    524 	bus_space_write_1(bst, bsh, FE_DLCR3, FE_RMASK);
    525 
    526 	/* Enable transmitter and receiver. */
    527 	delay(200);
    528 	bus_space_write_1(bst, bsh, FE_DLCR6,
    529 	    sc->proto_dlcr6 | FE_D6_DLC_ENABLE);
    530 	delay(200);
    531 
    532 #if FE_DEBUG >= 3
    533 	log(LOG_INFO, "%s: just after enabling DLC\n",
    534 	    device_xname(sc->sc_dev));
    535 	mb86960_dump(LOG_INFO, sc);
    536 #endif
    537 
    538 	/*
    539 	 * Make sure to empty the receive buffer.
    540 	 *
    541 	 * This may be redundant, but *if* the receive buffer were full
    542 	 * at this point, the driver would hang.  I have experienced
    543 	 * some strange hangups just after UP.  I hope the following
    544 	 * code solve the problem.
    545 	 *
    546 	 * I have changed the order of hardware initialization.
    547 	 * I think the receive buffer cannot have any packets at this
    548 	 * point in this version.  The following code *must* be
    549 	 * redundant now.  FIXME.
    550 	 */
    551 	for (i = 0; i < FE_MAX_RECV_COUNT; i++) {
    552 		if (bus_space_read_1(bst, bsh, FE_DLCR5) & FE_D5_BUFEMP)
    553 			break;
    554 		mb86960_droppacket(sc);
    555 	}
    556 #if FE_DEBUG >= 1
    557 	if (i >= FE_MAX_RECV_COUNT)
    558 		log(LOG_ERR, "%s: cannot empty receive buffer\n",
    559 		    device_xname(sc->sc_dev));
    560 #endif
    561 #if FE_DEBUG >= 3
    562 	if (i < FE_MAX_RECV_COUNT)
    563 		log(LOG_INFO, "%s: receive buffer emptied (%d)\n",
    564 		    device_xname(sc->sc_dev), i);
    565 #endif
    566 
    567 #if FE_DEBUG >= 3
    568 	log(LOG_INFO, "%s: after ERB loop\n", device_xname(sc->sc_dev));
    569 	mb86960_dump(LOG_INFO, sc);
    570 #endif
    571 
    572 	/* Do we need this here? */
    573 	bus_space_write_1(bst, bsh, FE_DLCR0, 0xFF);	/* Clear all bits. */
    574 	bus_space_write_1(bst, bsh, FE_DLCR1, 0xFF);	/* ditto. */
    575 
    576 #if FE_DEBUG >= 3
    577 	log(LOG_INFO, "%s: after FIXME\n", device_xname(sc->sc_dev));
    578 	mb86960_dump(LOG_INFO, sc);
    579 #endif
    580 
    581 	/* Set 'running' flag. */
    582 	ifp->if_flags |= IFF_RUNNING;
    583 
    584 	/*
    585 	 * At this point, the interface is runnung properly,
    586 	 * except that it receives *no* packets.  we then call
    587 	 * mb86960_setmode() to tell the chip what packets to be
    588 	 * received, based on the if_flags and multicast group
    589 	 * list.  It completes the initialization process.
    590 	 */
    591 	mb86960_setmode(sc);
    592 
    593 #if FE_DEBUG >= 3
    594 	log(LOG_INFO, "%s: after setmode\n", device_xname(sc->sc_dev));
    595 	mb86960_dump(LOG_INFO, sc);
    596 #endif
    597 
    598 	/* ...and attempt to start output. */
    599 	mb86960_start(ifp);
    600 
    601 #if FE_DEBUG >= 3
    602 	log(LOG_INFO, "%s: end of mb86960_init()\n", device_xname(sc->sc_dev));
    603 	mb86960_dump(LOG_INFO, sc);
    604 #endif
    605 }
    606 
    607 /*
    608  * This routine actually starts the transmission on the interface
    609  */
    610 static inline void
    611 mb86960_xmit(struct mb86960_softc *sc)
    612 {
    613 	bus_space_tag_t bst = sc->sc_bst;
    614 	bus_space_handle_t bsh = sc->sc_bsh;
    615 
    616 	/*
    617 	 * Set a timer just in case we never hear from the board again.
    618 	 * We use longer timeout for multiple packet transmission.
    619 	 * I'm not sure this timer value is appropriate.  FIXME.
    620 	 */
    621 	sc->sc_ec.ec_if.if_timer = 1 + sc->txb_count;
    622 
    623 	/* Update txb variables. */
    624 	sc->txb_sched = sc->txb_count;
    625 	sc->txb_count = 0;
    626 	sc->txb_free = sc->txb_size;
    627 
    628 #if FE_DELAYED_PADDING
    629 	/* Omit the postponed padding process. */
    630 	sc->txb_padding = 0;
    631 #endif
    632 
    633 	/* Start transmitter, passing packets in TX buffer. */
    634 	bus_space_write_1(bst, bsh, FE_BMPR10, sc->txb_sched | FE_B10_START);
    635 }
    636 
    637 /*
    638  * Start output on interface.
    639  * We make two assumptions here:
    640  *  1) that the current priority is set to splnet _before_ this code
    641  *     is called *and* is returned to the appropriate priority after
    642  *     return
    643  *  2) that the IFF_OACTIVE flag is checked before this code is called
    644  *     (i.e. that the output part of the interface is idle)
    645  */
    646 void
    647 mb86960_start(struct ifnet *ifp)
    648 {
    649 	struct mb86960_softc *sc = ifp->if_softc;
    650 	struct mbuf *m;
    651 
    652 #if FE_DEBUG >= 1
    653 	/* Just a sanity check. */
    654 	if ((sc->txb_count == 0) != (sc->txb_free == sc->txb_size)) {
    655 		/*
    656 		 * Txb_count and txb_free co-works to manage the
    657 		 * transmission buffer.  Txb_count keeps track of the
    658 		 * used potion of the buffer, while txb_free does unused
    659 		 * potion.  So, as long as the driver runs properly,
    660 		 * txb_count is zero if and only if txb_free is same
    661 		 * as txb_size (which represents whole buffer.)
    662 		 */
    663 		log(LOG_ERR, "%s: inconsistent txb variables (%d, %d)\n",
    664 		    device_xname(sc->sc_dev), sc->txb_count, sc->txb_free);
    665 		/*
    666 		 * So, what should I do, then?
    667 		 *
    668 		 * We now know txb_count and txb_free contradicts.  We
    669 		 * cannot, however, tell which is wrong.  More
    670 		 * over, we cannot peek 86960 transmission buffer or
    671 		 * reset the transmission buffer.  (In fact, we can
    672 		 * reset the entire interface.  I don't want to do it.)
    673 		 *
    674 		 * If txb_count is incorrect, leaving it as is will cause
    675 		 * sending of garbage after the next interrupt.  We have to
    676 		 * avoid it.  Hence, we reset the txb_count here.  If
    677 		 * txb_free was incorrect, resetting txb_count just loose
    678 		 * some packets.  We can live with it.
    679 		 */
    680 		sc->txb_count = 0;
    681 	}
    682 #endif
    683 
    684 #if FE_DEBUG >= 1
    685 	/*
    686 	 * First, see if there are buffered packets and an idle
    687 	 * transmitter - should never happen at this point.
    688 	 */
    689 	if ((sc->txb_count > 0) && (sc->txb_sched == 0)) {
    690 		log(LOG_ERR, "%s: transmitter idle with %d buffered packets\n",
    691 		    device_xname(sc->sc_dev), sc->txb_count);
    692 		mb86960_xmit(sc);
    693 	}
    694 #endif
    695 
    696 	/*
    697 	 * Stop accepting more transmission packets temporarily, when
    698 	 * a filter change request is delayed.  Updating the MARs on
    699 	 * 86960 flushes the transmisstion buffer, so it is delayed
    700 	 * until all buffered transmission packets have been sent
    701 	 * out.
    702 	 */
    703 	if (sc->filter_change) {
    704 		/*
    705 		 * Filter change request is delayed only when the DLC is
    706 		 * working.  DLC soon raise an interrupt after finishing
    707 		 * the work.
    708 		 */
    709 		goto indicate_active;
    710 	}
    711 
    712 	for (;;) {
    713 		/*
    714 		 * See if there is room to put another packet in the buffer.
    715 		 * We *could* do better job by peeking the send queue to
    716 		 * know the length of the next packet.  Current version just
    717 		 * tests against the worst case (i.e., longest packet).  FIXME.
    718 		 *
    719 		 * When adding the packet-peek feature, don't forget adding a
    720 		 * test on txb_count against QUEUEING_MAX.
    721 		 * There is a little chance the packet count exceeds
    722 		 * the limit.  Assume transmission buffer is 8KB (2x8KB
    723 		 * configuration) and an application sends a bunch of small
    724 		 * (i.e., minimum packet sized) packets rapidly.  An 8KB
    725 		 * buffer can hold 130 blocks of 62 bytes long...
    726 		 */
    727 		if (sc->txb_free <
    728 		    (ETHER_MAX_LEN - ETHER_CRC_LEN) + FE_TXLEN_SIZE) {
    729 			/* No room. */
    730 			goto indicate_active;
    731 		}
    732 
    733 #if FE_SINGLE_TRANSMISSION
    734 		if (sc->txb_count > 0) {
    735 			/* Just one packet per a transmission buffer. */
    736 			goto indicate_active;
    737 		}
    738 #endif
    739 
    740 		/*
    741 		 * Get the next mbuf chain for a packet to send.
    742 		 */
    743 		IFQ_DEQUEUE(&ifp->if_snd, m);
    744 		if (m == 0) {
    745 			/* No more packets to send. */
    746 			goto indicate_inactive;
    747 		}
    748 
    749 		/* Tap off here if there is a BPF listener. */
    750 		bpf_mtap(ifp, m, BPF_D_OUT);
    751 
    752 		/*
    753 		 * Copy the mbuf chain into the transmission buffer.
    754 		 * txb_* variables are updated as necessary.
    755 		 */
    756 		mb86960_write_mbufs(sc, m);
    757 
    758 		m_freem(m);
    759 
    760 		/* Start transmitter if it's idle. */
    761 		if (sc->txb_sched == 0)
    762 			mb86960_xmit(sc);
    763 	}
    764 
    765 indicate_inactive:
    766 	/*
    767 	 * We are using the !OACTIVE flag to indicate to
    768 	 * the outside world that we can accept an
    769 	 * additional packet rather than that the
    770 	 * transmitter is _actually_ active.  Indeed, the
    771 	 * transmitter may be active, but if we haven't
    772 	 * filled all the buffers with data then we still
    773 	 * want to accept more.
    774 	 */
    775 	ifp->if_flags &= ~IFF_OACTIVE;
    776 	return;
    777 
    778 indicate_active:
    779 	/*
    780 	 * The transmitter is active, and there are no room for
    781 	 * more outgoing packets in the transmission buffer.
    782 	 */
    783 	ifp->if_flags |= IFF_OACTIVE;
    784 	return;
    785 }
    786 
    787 /*
    788  * Transmission interrupt handler
    789  * The control flow of this function looks silly.  FIXME.
    790  */
    791 void
    792 mb86960_tint(struct mb86960_softc *sc, uint8_t tstat)
    793 {
    794 	bus_space_tag_t bst = sc->sc_bst;
    795 	bus_space_handle_t bsh = sc->sc_bsh;
    796 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    797 	int left;
    798 	int col;
    799 
    800 	/*
    801 	 * Handle "excessive collision" interrupt.
    802 	 */
    803 	if (tstat & FE_D0_COLL16) {
    804 		/*
    805 		 * Find how many packets (including this collided one)
    806 		 * are left unsent in transmission buffer.
    807 		 */
    808 		left = bus_space_read_1(bst, bsh, FE_BMPR10);
    809 
    810 #if FE_DEBUG >= 2
    811 		log(LOG_WARNING, "%s: excessive collision (%d/%d)\n",
    812 		    device_xname(sc->sc_dev), left, sc->txb_sched);
    813 #endif
    814 #if FE_DEBUG >= 3
    815 		mb86960_dump(LOG_INFO, sc);
    816 #endif
    817 
    818 		/*
    819 		 * Update statistics.
    820 		 */
    821 		ifp->if_collisions += 16;
    822 		ifp->if_oerrors++;
    823 		ifp->if_opackets += sc->txb_sched - left;
    824 
    825 		/*
    826 		 * Collision statistics has been updated.
    827 		 * Clear the collision flag on 86960 now to avoid confusion.
    828 		 */
    829 		bus_space_write_1(bst, bsh, FE_DLCR0, FE_D0_COLLID);
    830 
    831 		/*
    832 		 * Restart transmitter, skipping the
    833 		 * collided packet.
    834 		 *
    835 		 * We *must* skip the packet to keep network running
    836 		 * properly.  Excessive collision error is an
    837 		 * indication of the network overload.  If we
    838 		 * tried sending the same packet after excessive
    839 		 * collision, the network would be filled with
    840 		 * out-of-time packets.  Packets belonging
    841 		 * to reliable transport (such as TCP) are resent
    842 		 * by some upper layer.
    843 		 */
    844 		bus_space_write_1(bst, bsh, FE_BMPR11,
    845 		    FE_B11_CTRL_SKIP | FE_B11_MODE1);
    846 		sc->txb_sched = left - 1;
    847 	}
    848 
    849 	/*
    850 	 * Handle "transmission complete" interrupt.
    851 	 */
    852 	if (tstat & FE_D0_TXDONE) {
    853 		/*
    854 		 * Add in total number of collisions on last
    855 		 * transmission.  We also clear "collision occurred" flag
    856 		 * here.
    857 		 *
    858 		 * 86960 has a design flow on collision count on multiple
    859 		 * packet transmission.  When we send two or more packets
    860 		 * with one start command (that's what we do when the
    861 		 * transmission queue is clauded), 86960 informs us number
    862 		 * of collisions occurred on the last packet on the
    863 		 * transmission only.  Number of collisions on previous
    864 		 * packets are lost.  I have told that the fact is clearly
    865 		 * stated in the Fujitsu document.
    866 		 *
    867 		 * I considered not to mind it seriously.  Collision
    868 		 * count is not so important, anyway.  Any comments?  FIXME.
    869 		 */
    870 
    871 		if (bus_space_read_1(bst, bsh, FE_DLCR0) & FE_D0_COLLID) {
    872 			/* Clear collision flag. */
    873 			bus_space_write_1(bst, bsh, FE_DLCR0, FE_D0_COLLID);
    874 
    875 			/* Extract collision count from 86960. */
    876 			col = bus_space_read_1(bst, bsh, FE_DLCR4) & FE_D4_COL;
    877 			if (col == 0) {
    878 				/*
    879 				 * Status register indicates collisions,
    880 				 * while the collision count is zero.
    881 				 * This can happen after multiple packet
    882 				 * transmission, indicating that one or more
    883 				 * previous packet(s) had been collided.
    884 				 *
    885 				 * Since the accurate number of collisions
    886 				 * has been lost, we just guess it as 1;
    887 				 * Am I too optimistic?  FIXME.
    888 				 */
    889 				col = 1;
    890 			} else
    891 				col >>= FE_D4_COL_SHIFT;
    892 			ifp->if_collisions += col;
    893 #if FE_DEBUG >= 4
    894 			log(LOG_WARNING, "%s: %d collision%s (%d)\n",
    895 			    device_xname(sc->sc_dev), col, col == 1 ? "" : "s",
    896 			    sc->txb_sched);
    897 #endif
    898 		}
    899 
    900 		/*
    901 		 * Update total number of successfully
    902 		 * transmitted packets.
    903 		 */
    904 		ifp->if_opackets += sc->txb_sched;
    905 		sc->txb_sched = 0;
    906 	}
    907 
    908 	if (sc->txb_sched == 0) {
    909 		/*
    910 		 * The transmitter is no more active.
    911 		 * Reset output active flag and watchdog timer.
    912 		 */
    913 		ifp->if_flags &= ~IFF_OACTIVE;
    914 		ifp->if_timer = 0;
    915 
    916 		/*
    917 		 * If more data is ready to transmit in the buffer, start
    918 		 * transmitting them.  Otherwise keep transmitter idle,
    919 		 * even if more data is queued.  This gives receive
    920 		 * process a slight priority.
    921 		 */
    922 		if (sc->txb_count > 0)
    923 			mb86960_xmit(sc);
    924 	}
    925 }
    926 
    927 /*
    928  * Ethernet interface receiver interrupt.
    929  */
    930 void
    931 mb86960_rint(struct mb86960_softc *sc, uint8_t rstat)
    932 {
    933 	bus_space_tag_t bst = sc->sc_bst;
    934 	bus_space_handle_t bsh = sc->sc_bsh;
    935 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    936 	u_int status, len;
    937 	int i;
    938 
    939 	/*
    940 	 * Update statistics if this interrupt is caused by an error.
    941 	 */
    942 	if (rstat & (FE_D1_OVRFLO | FE_D1_CRCERR | FE_D1_ALGERR |
    943 	    FE_D1_SRTPKT)) {
    944 #if FE_DEBUG >= 3
    945 		char sbuf[sizeof(FE_D1_ERRBITS) + 64];
    946 
    947 		snprintb(sbuf, sizeof(sbuf), FE_D1_ERRBITS, rstat);
    948 		log(LOG_WARNING, "%s: receive error: %s\n",
    949 		    device_xname(sc->sc_dev), sbuf);
    950 #endif
    951 		ifp->if_ierrors++;
    952 	}
    953 
    954 	/*
    955 	 * MB86960 has a flag indicating "receive queue empty."
    956 	 * We just loop checking the flag to pull out all received
    957 	 * packets.
    958 	 *
    959 	 * We limit the number of iterrations to avoid infinite loop.
    960 	 * It can be caused by a very slow CPU (some broken
    961 	 * peripheral may insert incredible number of wait cycles)
    962 	 * or, worse, by a broken MB86960 chip.
    963 	 */
    964 	for (i = 0; i < FE_MAX_RECV_COUNT; i++) {
    965 		/* Stop the iterration if 86960 indicates no packets. */
    966 		if (bus_space_read_1(bst, bsh, FE_DLCR5) & FE_D5_BUFEMP)
    967 			break;
    968 
    969 		/*
    970 		 * Extract receive packet status from the receive
    971 		 * packet header.
    972 		 */
    973 		if (sc->sc_flags & FE_FLAGS_SBW_BYTE) {
    974 			status = bus_space_read_1(bst, bsh, FE_BMPR8);
    975 			(void)bus_space_read_1(bst, bsh, FE_BMPR8);
    976 		} else
    977 			status = bus_space_read_2(bst, bsh, FE_BMPR8);
    978 
    979 #if FE_DEBUG >= 4
    980 		log(LOG_INFO, "%s: receive status = %02x\n",
    981 		    device_xname(sc->sc_dev), status);
    982 #endif
    983 
    984 		/*
    985 		 * If there was an error, update statistics and drop
    986 		 * the packet, unless the interface is in promiscuous
    987 		 * mode.
    988 		 */
    989 		if ((status & FE_RXSTAT_GOODPKT) == 0) {
    990 			if ((ifp->if_flags & IFF_PROMISC) == 0) {
    991 				ifp->if_ierrors++;
    992 				mb86960_droppacket(sc);
    993 				continue;
    994 			}
    995 		}
    996 
    997 		/*
    998 		 * Extract the packet length from the receive packet header.
    999 		 * It is a sum of a header (14 bytes) and a payload.
   1000 		 * CRC has been stripped off by the 86960.
   1001 		 */
   1002 		if (sc->sc_flags & FE_FLAGS_SBW_BYTE) {
   1003 			len  = bus_space_read_1(bst, bsh, FE_BMPR8);
   1004 			len |= bus_space_read_1(bst, bsh, FE_BMPR8) << 8;
   1005 		} else
   1006 			len = bus_space_read_2(bst, bsh, FE_BMPR8);
   1007 
   1008 		/*
   1009 		 * MB86965 checks the packet length and drop big packet
   1010 		 * before passing it to us.  There are no chance we can
   1011 		 * get [crufty] packets.  Hence, if the length exceeds
   1012 		 * the specified limit, it means some serious failure,
   1013 		 * such as out-of-sync on receive buffer management.
   1014 		 *
   1015 		 * Is this statement true?  FIXME.
   1016 		 */
   1017 		if (len > (ETHER_MAX_LEN - ETHER_CRC_LEN) ||
   1018 		    len < ETHER_HDR_LEN) {
   1019 #if FE_DEBUG >= 2
   1020 			log(LOG_WARNING,
   1021 			    "%s: received a %s packet? (%u bytes)\n",
   1022 			    device_xname(sc->sc_dev),
   1023 			    len < ETHER_HDR_LEN ? "partial" : "big", len);
   1024 #endif
   1025 			ifp->if_ierrors++;
   1026 			mb86960_droppacket(sc);
   1027 			continue;
   1028 		}
   1029 
   1030 		/*
   1031 		 * Check for a short (RUNT) packet.  We *do* check
   1032 		 * but do nothing other than print a message.
   1033 		 * Short packets are illegal, but does nothing bad
   1034 		 * if it carries data for upper layer.
   1035 		 */
   1036 #if FE_DEBUG >= 2
   1037 		if (len < (ETHER_MIN_LEN - ETHER_CRC_LEN)) {
   1038 			log(LOG_WARNING,
   1039 			    "%s: received a short packet? (%u bytes)\n",
   1040 			    device_xname(sc->sc_dev), len);
   1041 		}
   1042 #endif
   1043 
   1044 		/*
   1045 		 * Go get a packet.
   1046 		 */
   1047 		if (mb86960_get_packet(sc, len) == 0) {
   1048 			/* Skip a packet, updating statistics. */
   1049 #if FE_DEBUG >= 2
   1050 			log(LOG_WARNING,
   1051 			    "%s: out of mbufs; dropping packet (%u bytes)\n",
   1052 			    device_xname(sc->sc_dev), len);
   1053 #endif
   1054 			ifp->if_ierrors++;
   1055 			mb86960_droppacket(sc);
   1056 
   1057 			/*
   1058 			 * We stop receiving packets, even if there are
   1059 			 * more in the buffer.  We hope we can get more
   1060 			 * mbufs next time.
   1061 			 */
   1062 			return;
   1063 		}
   1064 	}
   1065 }
   1066 
   1067 /*
   1068  * Ethernet interface interrupt processor
   1069  */
   1070 int
   1071 mb86960_intr(void *arg)
   1072 {
   1073 	struct mb86960_softc *sc = arg;
   1074 	bus_space_tag_t bst = sc->sc_bst;
   1075 	bus_space_handle_t bsh = sc->sc_bsh;
   1076 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1077 	uint8_t tstat, rstat;
   1078 
   1079 	if ((sc->sc_stat & FE_STAT_ENABLED) == 0 ||
   1080 	    !device_is_active(sc->sc_dev))
   1081 		return 0;
   1082 
   1083 #if FE_DEBUG >= 4
   1084 	log(LOG_INFO, "%s: mb86960_intr()\n", device_xname(sc->sc_dev));
   1085 	mb86960_dump(LOG_INFO, sc);
   1086 #endif
   1087 
   1088 	/*
   1089 	 * Get interrupt conditions, masking unneeded flags.
   1090 	 */
   1091 	tstat = bus_space_read_1(bst, bsh, FE_DLCR0) & FE_TMASK;
   1092 	rstat = bus_space_read_1(bst, bsh, FE_DLCR1) & FE_RMASK;
   1093 	if (tstat == 0 && rstat == 0)
   1094 		return 0;
   1095 
   1096 	/*
   1097 	 * Loop until there are no more new interrupt conditions.
   1098 	 */
   1099 	for (;;) {
   1100 		/*
   1101 		 * Reset the conditions we are acknowledging.
   1102 		 */
   1103 		bus_space_write_1(bst, bsh, FE_DLCR0, tstat);
   1104 		bus_space_write_1(bst, bsh, FE_DLCR1, rstat);
   1105 
   1106 		/*
   1107 		 * Handle transmitter interrupts. Handle these first because
   1108 		 * the receiver will reset the board under some conditions.
   1109 		 */
   1110 		if (tstat != 0)
   1111 			mb86960_tint(sc, tstat);
   1112 
   1113 		/*
   1114 		 * Handle receiver interrupts.
   1115 		 */
   1116 		if (rstat != 0)
   1117 			mb86960_rint(sc, rstat);
   1118 
   1119 		/*
   1120 		 * Update the multicast address filter if it is
   1121 		 * needed and possible.  We do it now, because
   1122 		 * we can make sure the transmission buffer is empty,
   1123 		 * and there is a good chance that the receive queue
   1124 		 * is empty.  It will minimize the possibility of
   1125 		 * packet lossage.
   1126 		 */
   1127 		if (sc->filter_change &&
   1128 		    sc->txb_count == 0 && sc->txb_sched == 0) {
   1129 			mb86960_loadmar(sc);
   1130 			ifp->if_flags &= ~IFF_OACTIVE;
   1131 		}
   1132 
   1133 		/*
   1134 		 * If it looks like the transmitter can take more data,
   1135 		 * attempt to start output on the interface. This is done
   1136 		 * after handling the receiver interrupt to give the
   1137 		 * receive operation priority.
   1138 		 */
   1139 		if ((ifp->if_flags & IFF_OACTIVE) == 0)
   1140 			if_schedule_deferred_start(ifp);
   1141 
   1142 		if (rstat != 0 || tstat != 0)
   1143 			rnd_add_uint32(&sc->rnd_source, rstat + tstat);
   1144 
   1145 		/*
   1146 		 * Get interrupt conditions, masking unneeded flags.
   1147 		 */
   1148 		tstat = bus_space_read_1(bst, bsh, FE_DLCR0) & FE_TMASK;
   1149 		rstat = bus_space_read_1(bst, bsh, FE_DLCR1) & FE_RMASK;
   1150 		if (tstat == 0 && rstat == 0)
   1151 			return 1;
   1152 	}
   1153 }
   1154 
   1155 /*
   1156  * Process an ioctl request.  This code needs some work - it looks pretty ugly.
   1157  */
   1158 int
   1159 mb86960_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1160 {
   1161 	struct mb86960_softc *sc = ifp->if_softc;
   1162 	struct ifaddr *ifa = (struct ifaddr *)data;
   1163 	int s, error = 0;
   1164 
   1165 #if FE_DEBUG >= 3
   1166 	log(LOG_INFO, "%s: ioctl(%lx)\n", device_xname(sc->sc_dev), cmd);
   1167 #endif
   1168 
   1169 	s = splnet();
   1170 
   1171 	switch (cmd) {
   1172 	case SIOCINITIFADDR:
   1173 		if ((error = mb86960_enable(sc)) != 0)
   1174 			break;
   1175 		ifp->if_flags |= IFF_UP;
   1176 
   1177 		mb86960_init(sc);
   1178 		switch (ifa->ifa_addr->sa_family) {
   1179 #ifdef INET
   1180 		case AF_INET:
   1181 			arp_ifinit(ifp, ifa);
   1182 			break;
   1183 #endif
   1184 		default:
   1185 			break;
   1186 		}
   1187 		break;
   1188 
   1189 	case SIOCSIFFLAGS:
   1190 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   1191 			break;
   1192 		/* XXX re-use ether_ioctl() */
   1193 		switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
   1194 		case IFF_RUNNING:
   1195 			/*
   1196 			 * If interface is marked down and it is running, then
   1197 			 * stop it.
   1198 			 */
   1199 			mb86960_stop(sc);
   1200 			ifp->if_flags &= ~IFF_RUNNING;
   1201 			mb86960_disable(sc);
   1202 			break;
   1203 		case IFF_UP:
   1204 			/*
   1205 			 * If interface is marked up and it is stopped, then
   1206 			 * start it.
   1207 			 */
   1208 			if ((error = mb86960_enable(sc)) != 0)
   1209 				break;
   1210 			mb86960_init(sc);
   1211 			break;
   1212 		case IFF_UP | IFF_RUNNING:
   1213 			/*
   1214 			 * Reset the interface to pick up changes in any other
   1215 			 * flags that affect hardware registers.
   1216 			 */
   1217 			mb86960_setmode(sc);
   1218 			break;
   1219 		case 0:
   1220 			break;
   1221 		}
   1222 #if FE_DEBUG >= 1
   1223 		/* "ifconfig fe0 debug" to print register dump. */
   1224 		if (ifp->if_flags & IFF_DEBUG) {
   1225 			log(LOG_INFO, "%s: SIOCSIFFLAGS(DEBUG)\n",
   1226 			    device_xname(sc->sc_dev));
   1227 			mb86960_dump(LOG_DEBUG, sc);
   1228 		}
   1229 #endif
   1230 		break;
   1231 
   1232 	case SIOCADDMULTI:
   1233 	case SIOCDELMULTI:
   1234 		if ((sc->sc_stat & FE_STAT_ENABLED) == 0) {
   1235 			error = EIO;
   1236 			break;
   1237 		}
   1238 
   1239 		/* Update our multicast list. */
   1240 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
   1241 			/*
   1242 			 * Multicast list has changed; set the hardware filter
   1243 			 * accordingly.
   1244 			 */
   1245 			if (ifp->if_flags & IFF_RUNNING)
   1246 				mb86960_setmode(sc);
   1247 			error = 0;
   1248 		}
   1249 		break;
   1250 
   1251 	default:
   1252 		error = ether_ioctl(ifp, cmd, data);
   1253 		break;
   1254 	}
   1255 
   1256 	splx(s);
   1257 	return error;
   1258 }
   1259 
   1260 /*
   1261  * Retrieve packet from receive buffer and send to the next level up via
   1262  * ether_input(). If there is a BPF listener, give a copy to BPF, too.
   1263  * Returns 0 if success, -1 if error (i.e., mbuf allocation failure).
   1264  */
   1265 int
   1266 mb86960_get_packet(struct mb86960_softc *sc, u_int len)
   1267 {
   1268 	bus_space_tag_t bst = sc->sc_bst;
   1269 	bus_space_handle_t bsh = sc->sc_bsh;
   1270 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1271 	struct mbuf *m;
   1272 
   1273 	/* Allocate a header mbuf. */
   1274 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1275 	if (m == 0)
   1276 		return 0;
   1277 	m_set_rcvif(m, ifp);
   1278 	m->m_pkthdr.len = len;
   1279 
   1280 	/* The following silliness is to make NFS happy. */
   1281 #define	EROUND	((sizeof(struct ether_header) + 3) & ~3)
   1282 #define	EOFF	(EROUND - sizeof(struct ether_header))
   1283 
   1284 	/*
   1285 	 * Our strategy has one more problem.  There is a policy on
   1286 	 * mbuf cluster allocation.  It says that we must have at
   1287 	 * least MINCLSIZE (208 bytes) to allocate a cluster.  For a
   1288 	 * packet of a size between (MHLEN - 2) to (MINCLSIZE - 2),
   1289 	 * our code violates the rule...
   1290 	 * On the other hand, the current code is short, simple,
   1291 	 * and fast, however.  It does no harmful thing, just waists
   1292 	 * some memory.  Any comments?  FIXME.
   1293 	 */
   1294 
   1295 	/* Attach a cluster if this packet doesn't fit in a normal mbuf. */
   1296 	if (len > MHLEN - EOFF) {
   1297 		MCLGET(m, M_DONTWAIT);
   1298 		if ((m->m_flags & M_EXT) == 0) {
   1299 			m_freem(m);
   1300 			return 0;
   1301 		}
   1302 	}
   1303 
   1304 	/*
   1305 	 * The following assumes there is room for the ether header in the
   1306 	 * header mbuf.
   1307 	 */
   1308 	m->m_data += EOFF;
   1309 
   1310 	/* Set the length of this packet. */
   1311 	m->m_len = len;
   1312 
   1313 	/* Get a packet. */
   1314 	if (sc->sc_flags & FE_FLAGS_SBW_BYTE)
   1315 		bus_space_read_multi_1(bst, bsh, FE_BMPR8,
   1316 		    mtod(m, uint8_t *), len);
   1317 	else
   1318 		bus_space_read_multi_stream_2(bst, bsh, FE_BMPR8,
   1319 		    mtod(m, uint16_t *), (len + 1) >> 1);
   1320 
   1321 	if_percpuq_enqueue(ifp->if_percpuq, m);
   1322 	return 1;
   1323 }
   1324 
   1325 /*
   1326  * Write an mbuf chain to the transmission buffer memory using 16 bit PIO.
   1327  * Returns number of bytes actually written, including length word.
   1328  *
   1329  * If an mbuf chain is too long for an Ethernet frame, it is not sent.
   1330  * Packets shorter than Ethernet minimum are legal, and we pad them
   1331  * before sending out.  An exception is "partial" packets which are
   1332  * shorter than mandatory Ethernet header.
   1333  *
   1334  * I wrote a code for an experimental "delayed padding" technique.
   1335  * When employed, it postpones the padding process for short packets.
   1336  * If xmit() occurred at the moment, the padding process is omitted, and
   1337  * garbages are sent as pad data.  If next packet is stored in the
   1338  * transmission buffer before xmit(), write_mbuf() pads the previous
   1339  * packet before transmitting new packet.  This *may* gain the
   1340  * system performance (slightly).
   1341  */
   1342 void
   1343 mb86960_write_mbufs(struct mb86960_softc *sc, struct mbuf *m)
   1344 {
   1345 	bus_space_tag_t bst = sc->sc_bst;
   1346 	bus_space_handle_t bsh = sc->sc_bsh;
   1347 	int totlen, len;
   1348 #if FE_DEBUG >= 2
   1349 	struct mbuf *mp;
   1350 #endif
   1351 
   1352 #if FE_DELAYED_PADDING
   1353 	/* Do the "delayed padding." */
   1354 	if (sc->txb_padding > 0) {
   1355 		if (sc->sc_flags & FE_FLAGS_SBW_BYTE) {
   1356 			for (len = sc->txb_padding; len > 0; len--)
   1357 				bus_space_write_1(bst, bsh, FE_BMPR8, 0);
   1358 		} else {
   1359 			for (len = sc->txb_padding >> 1; len > 0; len--)
   1360 				bus_space_write_2(bst, bsh, FE_BMPR8, 0);
   1361 		}
   1362 		sc->txb_padding = 0;
   1363 	}
   1364 #endif
   1365 
   1366 	/* We need to use m->m_pkthdr.len, so require the header */
   1367 	if ((m->m_flags & M_PKTHDR) == 0)
   1368 		panic("mb86960_write_mbufs: no header mbuf");
   1369 
   1370 #if FE_DEBUG >= 2
   1371 	/* First, count up the total number of bytes to copy. */
   1372 	for (totlen = 0, mp = m; mp != 0; mp = mp->m_next)
   1373 		totlen += mp->m_len;
   1374 	/* Check if this matches the one in the packet header. */
   1375 	if (totlen != m->m_pkthdr.len)
   1376 		log(LOG_WARNING, "%s: packet length mismatch? (%d/%d)\n",
   1377 		    device_xname(sc->sc_dev), totlen, m->m_pkthdr.len);
   1378 #else
   1379 	/* Just use the length value in the packet header. */
   1380 	totlen = m->m_pkthdr.len;
   1381 #endif
   1382 
   1383 #if FE_DEBUG >= 1
   1384 	/*
   1385 	 * Should never send big packets.  If such a packet is passed,
   1386 	 * it should be a bug of upper layer.  We just ignore it.
   1387 	 * ... Partial (too short) packets, neither.
   1388 	 */
   1389 	if (totlen > (ETHER_MAX_LEN - ETHER_CRC_LEN) ||
   1390 	    totlen < ETHER_HDR_LEN) {
   1391 		log(LOG_ERR, "%s: got a %s packet (%u bytes) to send\n",
   1392 		    device_xname(sc->sc_dev),
   1393 		    totlen < ETHER_HDR_LEN ? "partial" : "big", totlen);
   1394 		sc->sc_ec.ec_if.if_oerrors++;
   1395 		return;
   1396 	}
   1397 #endif
   1398 
   1399 	/*
   1400 	 * Put the length word for this frame.
   1401 	 * Does 86960 accept odd length?  -- Yes.
   1402 	 * Do we need to pad the length to minimum size by ourselves?
   1403 	 * -- Generally yes.  But for (or will be) the last
   1404 	 * packet in the transmission buffer, we can skip the
   1405 	 * padding process.  It may gain performance slightly.  FIXME.
   1406 	 */
   1407 	len = uimax(totlen, (ETHER_MIN_LEN - ETHER_CRC_LEN));
   1408 	if (sc->sc_flags & FE_FLAGS_SBW_BYTE) {
   1409 		bus_space_write_1(bst, bsh, FE_BMPR8, len);
   1410 		bus_space_write_1(bst, bsh, FE_BMPR8, len >> 8);
   1411 	} else {
   1412 		bus_space_write_2(bst, bsh, FE_BMPR8, len);
   1413 		/* roundup packet length since we will use word access */
   1414 		totlen = (totlen + 1) & ~1;
   1415 	}
   1416 
   1417 	/*
   1418 	 * Update buffer status now.
   1419 	 * Truncate the length up to an even number
   1420 	 * if the chip is set in SBW_WORD mode.
   1421 	 */
   1422 	sc->txb_free -= FE_TXLEN_SIZE +
   1423 	    uimax(totlen, (ETHER_MIN_LEN - ETHER_CRC_LEN));
   1424 	sc->txb_count++;
   1425 
   1426 #if FE_DELAYED_PADDING
   1427 	/* Postpone the packet padding if necessary. */
   1428 	if (totlen < (ETHER_MIN_LEN - ETHER_CRC_LEN))
   1429 		sc->txb_padding = (ETHER_MIN_LEN - ETHER_CRC_LEN) - totlen;
   1430 #endif
   1431 
   1432 	/*
   1433 	 * Transfer the data from mbuf chain to the transmission buffer.
   1434 	 * If the MB86960 is configured in word mode, data needs to be
   1435 	 * transferred as words, and only words.
   1436 	 * So that we require some extra code to patch over odd-length
   1437 	 * or unaligned mbufs.
   1438 	 */
   1439 	if (sc->sc_flags & FE_FLAGS_SBW_BYTE) {
   1440 		/* It's simple in byte mode. */
   1441 		for (; m != NULL; m = m->m_next) {
   1442 			if (m->m_len) {
   1443 				bus_space_write_multi_1(bst, bsh, FE_BMPR8,
   1444 				    mtod(m, uint8_t *), m->m_len);
   1445 			}
   1446 		}
   1447 	} else {
   1448 		/* a bit trickier in word mode. */
   1449 		uint8_t *data, savebyte[2];
   1450 		int leftover;
   1451 
   1452 		leftover = 0;
   1453 		savebyte[0] = savebyte[1] = 0;
   1454 
   1455 		for (; m != NULL; m = m->m_next) {
   1456 			len = m->m_len;
   1457 			if (len == 0)
   1458 				continue;
   1459 			data = mtod(m, uint8_t *);
   1460 			while (len > 0) {
   1461 				if (leftover) {
   1462 					/*
   1463 					 * Data left over (from mbuf or
   1464 					 * realignment). Buffer the next
   1465 					 * byte, and write it and the
   1466 					 * leftover data out.
   1467 					 */
   1468 					savebyte[1] = *data++;
   1469 					len--;
   1470 					bus_space_write_stream_2(bst, bsh,
   1471 					   FE_BMPR8, *(uint16_t *)savebyte);
   1472 					leftover = 0;
   1473 				} else if (BUS_SPACE_ALIGNED_POINTER(data,
   1474 				    uint16_t) == 0) {
   1475 					/*
   1476 					 * Unaligned data; buffer the next byte.
   1477 					 */
   1478 					savebyte[0] = *data++;
   1479 					len--;
   1480 					leftover = 1;
   1481 				} else {
   1482 					/*
   1483 					 * Aligned data; output contiguous
   1484 					 * words as much as we can, then
   1485 					 * buffer the remaining byte, if any.
   1486 					 */
   1487 					leftover = len & 1;
   1488 					len &= ~1;
   1489 					bus_space_write_multi_stream_2(bst,
   1490 					    bsh, FE_BMPR8, (uint16_t *)data,
   1491 					    len >> 1);
   1492 					data += len;
   1493 					if (leftover)
   1494 						savebyte[0] = *data++;
   1495 					len = 0;
   1496 				}
   1497 			}
   1498 			if (len < 0)
   1499 				panic("mb86960_write_mbufs: negative len");
   1500 		}
   1501 		if (leftover) {
   1502 			savebyte[1] = 0;
   1503 			bus_space_write_stream_2(bst, bsh, FE_BMPR8,
   1504 			    *(uint16_t *)savebyte);
   1505 		}
   1506 	}
   1507 #if FE_DELAYED_PADDING == 0
   1508 	/*
   1509 	 * Pad the packet to the minimum length if necessary.
   1510 	 */
   1511 	len = (ETHER_MIN_LEN - ETHER_CRC_LEN) - totlen;
   1512 	if (len > 0) {
   1513 		if (sc->sc_flags & FE_FLAGS_SBW_BYTE) {
   1514 			while (len-- > 0)
   1515 				bus_space_write_1(bst, bsh, FE_BMPR8, 0);
   1516 		} else {
   1517 			len >>= 1;
   1518 			while (len-- > 0)
   1519 				bus_space_write_2(bst, bsh, FE_BMPR8, 0);
   1520 		}
   1521 	}
   1522 #endif
   1523 }
   1524 
   1525 /*
   1526  * Compute the multicast address filter from the
   1527  * list of multicast addresses we need to listen to.
   1528  */
   1529 void
   1530 mb86960_getmcaf(struct ethercom *ec, uint8_t *af)
   1531 {
   1532 	struct ifnet *ifp = &ec->ec_if;
   1533 	struct ether_multi *enm;
   1534 	uint32_t crc;
   1535 	struct ether_multistep step;
   1536 
   1537 	/*
   1538 	 * Set up multicast address filter by passing all multicast addresses
   1539 	 * through a crc generator, and then using the high order 6 bits as an
   1540 	 * index into the 64 bit logical address filter.  The high order bit
   1541 	 * selects the word, while the rest of the bits select the bit within
   1542 	 * the word.
   1543 	 */
   1544 
   1545 	if ((ifp->if_flags & IFF_PROMISC) != 0)
   1546 		goto allmulti;
   1547 
   1548 	memset(af, 0, FE_FILTER_LEN);
   1549 	ETHER_LOCK(ec);
   1550 	ETHER_FIRST_MULTI(step, ec, enm);
   1551 	while (enm != NULL) {
   1552 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
   1553 		    sizeof(enm->enm_addrlo)) != 0) {
   1554 			/*
   1555 			 * We must listen to a range of multicast addresses.
   1556 			 * For now, just accept all multicasts, rather than
   1557 			 * trying to set only those filter bits needed to match
   1558 			 * the range.  (At this time, the only use of address
   1559 			 * ranges is for IP multicast routing, for which the
   1560 			 * range is big enough to require all bits set.)
   1561 			 */
   1562 			ETHER_UNLOCK(ec);
   1563 			goto allmulti;
   1564 		}
   1565 
   1566 		crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
   1567 
   1568 		/* Just want the 6 most significant bits. */
   1569 		crc >>= 26;
   1570 
   1571 		/* Turn on the corresponding bit in the filter. */
   1572 		af[crc >> 3] |= 1 << (crc & 7);
   1573 
   1574 		ETHER_NEXT_MULTI(step, enm);
   1575 	}
   1576 	ETHER_UNLOCK(ec);
   1577 	ifp->if_flags &= ~IFF_ALLMULTI;
   1578 	return;
   1579 
   1580 allmulti:
   1581 	ifp->if_flags |= IFF_ALLMULTI;
   1582 	memset(af, 0xff, FE_FILTER_LEN);
   1583 }
   1584 
   1585 /*
   1586  * Calculate a new "multicast packet filter" and put the 86960
   1587  * receiver in appropriate mode.
   1588  */
   1589 void
   1590 mb86960_setmode(struct mb86960_softc *sc)
   1591 {
   1592 	bus_space_tag_t bst = sc->sc_bst;
   1593 	bus_space_handle_t bsh = sc->sc_bsh;
   1594 	int flags = sc->sc_ec.ec_if.if_flags;
   1595 
   1596 	/*
   1597 	 * If the interface is not running, we postpone the update
   1598 	 * process for receive modes and multicast address filter
   1599 	 * until the interface is restarted.  It reduces some
   1600 	 * complicated job on maintaining chip states.  (Earlier versions
   1601 	 * of this driver had a bug on that point...)
   1602 	 *
   1603 	 * To complete the trick, mb86960_init() calls mb86960_setmode() after
   1604 	 * restarting the interface.
   1605 	 */
   1606 	if ((flags & IFF_RUNNING) == 0)
   1607 		return;
   1608 
   1609 	/*
   1610 	 * Promiscuous mode is handled separately.
   1611 	 */
   1612 	if ((flags & IFF_PROMISC) != 0) {
   1613 		/*
   1614 		 * Program 86960 to receive all packets on the segment
   1615 		 * including those directed to other stations.
   1616 		 * Multicast filter stored in MARs are ignored
   1617 		 * under this setting, so we don't need to update it.
   1618 		 *
   1619 		 * Promiscuous mode is used solely by BPF, and BPF only
   1620 		 * listens to valid (no error) packets.  So, we ignore
   1621 		 * errornous ones even in this mode.
   1622 		 */
   1623 		bus_space_write_1(bst, bsh, FE_DLCR5,
   1624 		    sc->proto_dlcr5 | FE_D5_AFM0 | FE_D5_AFM1);
   1625 		sc->filter_change = 0;
   1626 
   1627 #if FE_DEBUG >= 3
   1628 		log(LOG_INFO, "%s: promiscuous mode\n",
   1629 		    device_xname(sc->sc_dev));
   1630 #endif
   1631 		return;
   1632 	}
   1633 
   1634 	/*
   1635 	 * Turn the chip to the normal (non-promiscuous) mode.
   1636 	 */
   1637 	bus_space_write_1(bst, bsh, FE_DLCR5, sc->proto_dlcr5 | FE_D5_AFM1);
   1638 
   1639 	/*
   1640 	 * Find the new multicast filter value.
   1641 	 */
   1642 	mb86960_getmcaf(&sc->sc_ec, sc->filter);
   1643 	sc->filter_change = 1;
   1644 
   1645 #if FE_DEBUG >= 3
   1646 	log(LOG_INFO,
   1647 	    "%s: address filter: [%02x %02x %02x %02x %02x %02x %02x %02x]\n",
   1648 	    device_xname(sc->sc_dev),
   1649 	    sc->filter[0], sc->filter[1], sc->filter[2], sc->filter[3],
   1650 	    sc->filter[4], sc->filter[5], sc->filter[6], sc->filter[7]);
   1651 #endif
   1652 
   1653 	/*
   1654 	 * We have to update the multicast filter in the 86960, A.S.A.P.
   1655 	 *
   1656 	 * Note that the DLC (Data Linc Control unit, i.e. transmitter
   1657 	 * and receiver) must be stopped when feeding the filter, and
   1658 	 * DLC trashes all packets in both transmission and receive
   1659 	 * buffers when stopped.
   1660 	 *
   1661 	 * ... Are the above sentenses correct?  I have to check the
   1662 	 *     manual of the MB86960A.  FIXME.
   1663 	 *
   1664 	 * To reduce the packet lossage, we delay the filter update
   1665 	 * process until buffers are empty.
   1666 	 */
   1667 	if (sc->txb_sched == 0 && sc->txb_count == 0 &&
   1668 	    (bus_space_read_1(bst, bsh, FE_DLCR1) & FE_D1_PKTRDY) == 0) {
   1669 		/*
   1670 		 * Buffers are (apparently) empty.  Load
   1671 		 * the new filter value into MARs now.
   1672 		 */
   1673 		mb86960_loadmar(sc);
   1674 	} else {
   1675 		/*
   1676 		 * Buffers are not empty.  Mark that we have to update
   1677 		 * the MARs.  The new filter will be loaded by mb86960_intr()
   1678 		 * later.
   1679 		 */
   1680 #if FE_DEBUG >= 4
   1681 		log(LOG_INFO, "%s: filter change delayed\n",
   1682 		    device_xname(sc->sc_dev));
   1683 #endif
   1684 	}
   1685 }
   1686 
   1687 /*
   1688  * Load a new multicast address filter into MARs.
   1689  *
   1690  * The caller must have splnet'ed befor mb86960_loadmar.
   1691  * This function starts the DLC upon return.  So it can be called only
   1692  * when the chip is working, i.e., from the driver's point of view, when
   1693  * a device is RUNNING.  (I mistook the point in previous versions.)
   1694  */
   1695 void
   1696 mb86960_loadmar(struct mb86960_softc *sc)
   1697 {
   1698 	bus_space_tag_t bst = sc->sc_bst;
   1699 	bus_space_handle_t bsh = sc->sc_bsh;
   1700 
   1701 	/* Stop the DLC (transmitter and receiver). */
   1702 	bus_space_write_1(bst, bsh, FE_DLCR6,
   1703 	    sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
   1704 
   1705 	/* Select register bank 1 for MARs. */
   1706 	bus_space_write_1(bst, bsh, FE_DLCR7,
   1707 	    sc->proto_dlcr7 | FE_D7_RBS_MAR | FE_D7_POWER_UP);
   1708 
   1709 	/* Copy filter value into the registers. */
   1710 	bus_space_write_region_1(bst, bsh, FE_MAR8, sc->filter, FE_FILTER_LEN);
   1711 
   1712 	/* Restore the bank selection for BMPRs (i.e., runtime registers). */
   1713 	bus_space_write_1(bst, bsh, FE_DLCR7,
   1714 	    sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP);
   1715 
   1716 	/* Restart the DLC. */
   1717 	bus_space_write_1(bst, bsh, FE_DLCR6,
   1718 	    sc->proto_dlcr6 | FE_D6_DLC_ENABLE);
   1719 
   1720 	/* We have just updated the filter. */
   1721 	sc->filter_change = 0;
   1722 
   1723 #if FE_DEBUG >= 3
   1724 	log(LOG_INFO, "%s: address filter changed\n", device_xname(sc->sc_dev));
   1725 #endif
   1726 }
   1727 
   1728 /*
   1729  * Enable power on the interface.
   1730  */
   1731 int
   1732 mb86960_enable(struct mb86960_softc *sc)
   1733 {
   1734 
   1735 #if FE_DEBUG >= 3
   1736 	log(LOG_INFO, "%s: mb86960_enable()\n", device_xname(sc->sc_dev));
   1737 #endif
   1738 
   1739 	if ((sc->sc_stat & FE_STAT_ENABLED) == 0 && sc->sc_enable != NULL) {
   1740 		if ((*sc->sc_enable)(sc) != 0) {
   1741 			aprint_error_dev(sc->sc_dev, "device enable failed\n");
   1742 			return EIO;
   1743 		}
   1744 	}
   1745 
   1746 	sc->sc_stat |= FE_STAT_ENABLED;
   1747 	return 0;
   1748 }
   1749 
   1750 /*
   1751  * Disable power on the interface.
   1752  */
   1753 void
   1754 mb86960_disable(struct mb86960_softc *sc)
   1755 {
   1756 
   1757 #if FE_DEBUG >= 3
   1758 	log(LOG_INFO, "%s: mb86960_disable()\n", device_xname(sc->sc_dev));
   1759 #endif
   1760 
   1761 	if ((sc->sc_stat & FE_STAT_ENABLED) != 0 && sc->sc_disable != NULL) {
   1762 		(*sc->sc_disable)(sc);
   1763 		sc->sc_stat &= ~FE_STAT_ENABLED;
   1764 	}
   1765 }
   1766 
   1767 /*
   1768  * mbe_activate:
   1769  *
   1770  *	Handle device activation/deactivation requests.
   1771  */
   1772 int
   1773 mb86960_activate(device_t self, enum devact act)
   1774 {
   1775 	struct mb86960_softc *sc = device_private(self);
   1776 
   1777 	switch (act) {
   1778 	case DVACT_DEACTIVATE:
   1779 		if_deactivate(&sc->sc_ec.ec_if);
   1780 		return 0;
   1781 	default:
   1782 		return EOPNOTSUPP;
   1783 	}
   1784 }
   1785 
   1786 /*
   1787  * mb86960_detach:
   1788  *
   1789  *	Detach a MB86960 interface.
   1790  */
   1791 int
   1792 mb86960_detach(struct mb86960_softc *sc)
   1793 {
   1794 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1795 
   1796 	/* Succeed now if there's no work to do. */
   1797 	if ((sc->sc_stat & FE_STAT_ATTACHED) == 0)
   1798 		return 0;
   1799 
   1800 	/* Delete all media. */
   1801 	ifmedia_delete_instance(&sc->sc_media, IFM_INST_ANY);
   1802 
   1803 	/* Unhook the entropy source. */
   1804 	rnd_detach_source(&sc->rnd_source);
   1805 
   1806 	ether_ifdetach(ifp);
   1807 	if_detach(ifp);
   1808 
   1809 	mb86960_disable(sc);
   1810 	return 0;
   1811 }
   1812 
   1813 /*
   1814  * Routines to read all bytes from the config EEPROM (93C06) through MB86965A.
   1815  */
   1816 void
   1817 mb86965_read_eeprom(bus_space_tag_t iot, bus_space_handle_t ioh, uint8_t *data)
   1818 {
   1819 	int addr, op, bit;
   1820 	uint16_t val;
   1821 
   1822 	/* Read bytes from EEPROM; two bytes per an iteration. */
   1823 	for (addr = 0; addr < FE_EEPROM_SIZE / 2; addr++) {
   1824 		/* Reset the EEPROM interface. */
   1825 		bus_space_write_1(iot, ioh, FE_BMPR16, 0x00);
   1826 		bus_space_write_1(iot, ioh, FE_BMPR17, 0x00);
   1827 		bus_space_write_1(iot, ioh, FE_BMPR16, FE_B16_SELECT);
   1828 
   1829 		/* Send start bit. */
   1830 		bus_space_write_1(iot, ioh, FE_BMPR17, FE_B17_DATA);
   1831 		FE_EEPROM_DELAY();
   1832 		bus_space_write_1(iot, ioh,
   1833 		    FE_BMPR16, FE_B16_SELECT | FE_B16_CLOCK);
   1834 		FE_EEPROM_DELAY();
   1835 		bus_space_write_1(iot, ioh, FE_BMPR16, FE_B16_SELECT);
   1836 
   1837 		/* Send read command and read address. */
   1838 		op = 0x80 | addr;	/* READ instruction */
   1839 		for (bit = 8; bit > 0; bit--) {
   1840 			bus_space_write_1(iot, ioh, FE_BMPR17,
   1841 			    (op & (1 << (bit - 1))) ? FE_B17_DATA : 0);
   1842 			FE_EEPROM_DELAY();
   1843 			bus_space_write_1(iot, ioh,
   1844 			    FE_BMPR16, FE_B16_SELECT | FE_B16_CLOCK);
   1845 			FE_EEPROM_DELAY();
   1846 			bus_space_write_1(iot, ioh, FE_BMPR16, FE_B16_SELECT);
   1847 		}
   1848 		bus_space_write_1(iot, ioh, FE_BMPR17, 0x00);
   1849 
   1850 		/* Read two bytes in each address */
   1851 		val = 0;
   1852 		for (bit = 16; bit > 0; bit--) {
   1853 			FE_EEPROM_DELAY();
   1854 			bus_space_write_1(iot, ioh,
   1855 			    FE_BMPR16, FE_B16_SELECT | FE_B16_CLOCK);
   1856 			FE_EEPROM_DELAY();
   1857 			if (bus_space_read_1(iot, ioh, FE_BMPR17) &
   1858 			    FE_B17_DATA)
   1859 				val |= 1 << (bit - 1);
   1860 			bus_space_write_1(iot, ioh,
   1861 			    FE_BMPR16, FE_B16_SELECT);
   1862 		}
   1863 		data[addr * 2]	   = val >> 8;
   1864 		data[addr * 2 + 1] = val & 0xff;
   1865 	}
   1866 
   1867 	/* Make sure the EEPROM is turned off. */
   1868 	bus_space_write_1(iot, ioh, FE_BMPR16, 0);
   1869 	bus_space_write_1(iot, ioh, FE_BMPR17, 0);
   1870 
   1871 #if FE_DEBUG >= 3
   1872 	/* Report what we got. */
   1873 	log(LOG_INFO, "mb86965_read_eeprom: "
   1874 	    " %02x%02x%02x%02x %02x%02x%02x%02x -"
   1875 	    " %02x%02x%02x%02x %02x%02x%02x%02x -"
   1876 	    " %02x%02x%02x%02x %02x%02x%02x%02x -"
   1877 	    " %02x%02x%02x%02x %02x%02x%02x%02x\n",
   1878 	    data[ 0], data[ 1], data[ 2], data[ 3],
   1879 	    data[ 4], data[ 5], data[ 6], data[ 7],
   1880 	    data[ 8], data[ 9], data[10], data[11],
   1881 	    data[12], data[13], data[14], data[15],
   1882 	    data[16], data[17], data[18], data[19],
   1883 	    data[20], data[21], data[22], data[23],
   1884 	    data[24], data[25], data[26], data[27],
   1885 	    data[28], data[29], data[30], data[31]);
   1886 #endif
   1887 }
   1888 
   1889 #if FE_DEBUG >= 1
   1890 void
   1891 mb86960_dump(int level, struct mb86960_softc *sc)
   1892 {
   1893 	bus_space_tag_t bst = sc->sc_bst;
   1894 	bus_space_handle_t bsh = sc->sc_bsh;
   1895 	uint8_t save_dlcr7;
   1896 
   1897 	save_dlcr7 = bus_space_read_1(bst, bsh, FE_DLCR7);
   1898 
   1899 	log(level, "\tDLCR = %02x %02x %02x %02x %02x %02x %02x %02x\n",
   1900 	    bus_space_read_1(bst, bsh, FE_DLCR0),
   1901 	    bus_space_read_1(bst, bsh, FE_DLCR1),
   1902 	    bus_space_read_1(bst, bsh, FE_DLCR2),
   1903 	    bus_space_read_1(bst, bsh, FE_DLCR3),
   1904 	    bus_space_read_1(bst, bsh, FE_DLCR4),
   1905 	    bus_space_read_1(bst, bsh, FE_DLCR5),
   1906 	    bus_space_read_1(bst, bsh, FE_DLCR6),
   1907 	    bus_space_read_1(bst, bsh, FE_DLCR7));
   1908 
   1909 	bus_space_write_1(bst, bsh, FE_DLCR7,
   1910 	    (save_dlcr7 & ~FE_D7_RBS) | FE_D7_RBS_DLCR);
   1911 	log(level, "\t       %02x %02x %02x %02x %02x %02x %02x %02x\n",
   1912 	    bus_space_read_1(bst, bsh, FE_DLCR8),
   1913 	    bus_space_read_1(bst, bsh, FE_DLCR9),
   1914 	    bus_space_read_1(bst, bsh, FE_DLCR10),
   1915 	    bus_space_read_1(bst, bsh, FE_DLCR11),
   1916 	    bus_space_read_1(bst, bsh, FE_DLCR12),
   1917 	    bus_space_read_1(bst, bsh, FE_DLCR13),
   1918 	    bus_space_read_1(bst, bsh, FE_DLCR14),
   1919 	    bus_space_read_1(bst, bsh, FE_DLCR15));
   1920 
   1921 	bus_space_write_1(bst, bsh, FE_DLCR7,
   1922 	    (save_dlcr7 & ~FE_D7_RBS) | FE_D7_RBS_MAR);
   1923 	log(level, "\tMAR  = %02x %02x %02x %02x %02x %02x %02x %02x\n",
   1924 	    bus_space_read_1(bst, bsh, FE_MAR8),
   1925 	    bus_space_read_1(bst, bsh, FE_MAR9),
   1926 	    bus_space_read_1(bst, bsh, FE_MAR10),
   1927 	    bus_space_read_1(bst, bsh, FE_MAR11),
   1928 	    bus_space_read_1(bst, bsh, FE_MAR12),
   1929 	    bus_space_read_1(bst, bsh, FE_MAR13),
   1930 	    bus_space_read_1(bst, bsh, FE_MAR14),
   1931 	    bus_space_read_1(bst, bsh, FE_MAR15));
   1932 
   1933 	bus_space_write_1(bst, bsh, FE_DLCR7,
   1934 	    (save_dlcr7 & ~FE_D7_RBS) | FE_D7_RBS_BMPR);
   1935 	log(level,
   1936 	    "\tBMPR = xx xx %02x %02x %02x %02x %02x %02x %02x %02x xx %02x\n",
   1937 	    bus_space_read_1(bst, bsh, FE_BMPR10),
   1938 	    bus_space_read_1(bst, bsh, FE_BMPR11),
   1939 	    bus_space_read_1(bst, bsh, FE_BMPR12),
   1940 	    bus_space_read_1(bst, bsh, FE_BMPR13),
   1941 	    bus_space_read_1(bst, bsh, FE_BMPR14),
   1942 	    bus_space_read_1(bst, bsh, FE_BMPR15),
   1943 	    bus_space_read_1(bst, bsh, FE_BMPR16),
   1944 	    bus_space_read_1(bst, bsh, FE_BMPR17),
   1945 	    bus_space_read_1(bst, bsh, FE_BMPR19));
   1946 
   1947 	bus_space_write_1(bst, bsh, FE_DLCR7, save_dlcr7);
   1948 }
   1949 #endif
   1950