Home | History | Annotate | Line # | Download | only in ic
i82557.c revision 1.60
      1 /*	$NetBSD: i82557.c,v 1.60 2001/09/16 16:34:37 wiz Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 1999, 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1995, David Greenman
     42  * Copyright (c) 2001 Jonathan Lemon <jlemon (at) freebsd.org>
     43  * All rights reserved.
     44  *
     45  * Redistribution and use in source and binary forms, with or without
     46  * modification, are permitted provided that the following conditions
     47  * are met:
     48  * 1. Redistributions of source code must retain the above copyright
     49  *    notice unmodified, this list of conditions, and the following
     50  *    disclaimer.
     51  * 2. Redistributions in binary form must reproduce the above copyright
     52  *    notice, this list of conditions and the following disclaimer in the
     53  *    documentation and/or other materials provided with the distribution.
     54  *
     55  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65  * SUCH DAMAGE.
     66  *
     67  *	Id: if_fxp.c,v 1.113 2001/05/17 23:50:24 jlemon
     68  */
     69 
     70 /*
     71  * Device driver for the Intel i82557 fast Ethernet controller,
     72  * and its successors, the i82558 and i82559.
     73  */
     74 
     75 #include "bpfilter.h"
     76 #include "rnd.h"
     77 
     78 #include <sys/param.h>
     79 #include <sys/systm.h>
     80 #include <sys/callout.h>
     81 #include <sys/mbuf.h>
     82 #include <sys/malloc.h>
     83 #include <sys/kernel.h>
     84 #include <sys/socket.h>
     85 #include <sys/ioctl.h>
     86 #include <sys/errno.h>
     87 #include <sys/device.h>
     88 
     89 #include <machine/endian.h>
     90 
     91 #include <uvm/uvm_extern.h>
     92 
     93 #if NRND > 0
     94 #include <sys/rnd.h>
     95 #endif
     96 
     97 #include <net/if.h>
     98 #include <net/if_dl.h>
     99 #include <net/if_media.h>
    100 #include <net/if_ether.h>
    101 
    102 #if NBPFILTER > 0
    103 #include <net/bpf.h>
    104 #endif
    105 
    106 #include <machine/bus.h>
    107 #include <machine/intr.h>
    108 
    109 #include <dev/mii/miivar.h>
    110 
    111 #include <dev/ic/i82557reg.h>
    112 #include <dev/ic/i82557var.h>
    113 
    114 /*
    115  * NOTE!  On the Alpha, we have an alignment constraint.  The
    116  * card DMAs the packet immediately following the RFA.  However,
    117  * the first thing in the packet is a 14-byte Ethernet header.
    118  * This means that the packet is misaligned.  To compensate,
    119  * we actually offset the RFA 2 bytes into the cluster.  This
    120  * alignes the packet after the Ethernet header at a 32-bit
    121  * boundary.  HOWEVER!  This means that the RFA is misaligned!
    122  */
    123 #define	RFA_ALIGNMENT_FUDGE	2
    124 
    125 /*
    126  * The configuration byte map has several undefined fields which
    127  * must be one or must be zero.  Set up a template for these bits
    128  * only (assuming an i82557 chip), leaving the actual configuration
    129  * for fxp_init().
    130  *
    131  * See the definition of struct fxp_cb_config for the bit definitions.
    132  */
    133 const u_int8_t fxp_cb_config_template[] = {
    134 	0x0, 0x0,		/* cb_status */
    135 	0x0, 0x0,		/* cb_command */
    136 	0x0, 0x0, 0x0, 0x0,	/* link_addr */
    137 	0x0,	/*  0 */
    138 	0x0,	/*  1 */
    139 	0x0,	/*  2 */
    140 	0x0,	/*  3 */
    141 	0x0,	/*  4 */
    142 	0x0,	/*  5 */
    143 	0x32,	/*  6 */
    144 	0x0,	/*  7 */
    145 	0x0,	/*  8 */
    146 	0x0,	/*  9 */
    147 	0x6,	/* 10 */
    148 	0x0,	/* 11 */
    149 	0x0,	/* 12 */
    150 	0x0,	/* 13 */
    151 	0xf2,	/* 14 */
    152 	0x48,	/* 15 */
    153 	0x0,	/* 16 */
    154 	0x40,	/* 17 */
    155 	0xf0,	/* 18 */
    156 	0x0,	/* 19 */
    157 	0x3f,	/* 20 */
    158 	0x5,	/* 21 */
    159 	0x0,	/* 22 */
    160 	0x0,	/* 23 */
    161 	0x0,	/* 24 */
    162 	0x0,	/* 25 */
    163 	0x0,	/* 26 */
    164 	0x0,	/* 27 */
    165 	0x0,	/* 28 */
    166 	0x0,	/* 29 */
    167 	0x0,	/* 30 */
    168 	0x0,	/* 31 */
    169 };
    170 
    171 void	fxp_mii_initmedia(struct fxp_softc *);
    172 int	fxp_mii_mediachange(struct ifnet *);
    173 void	fxp_mii_mediastatus(struct ifnet *, struct ifmediareq *);
    174 
    175 void	fxp_80c24_initmedia(struct fxp_softc *);
    176 int	fxp_80c24_mediachange(struct ifnet *);
    177 void	fxp_80c24_mediastatus(struct ifnet *, struct ifmediareq *);
    178 
    179 void	fxp_start(struct ifnet *);
    180 int	fxp_ioctl(struct ifnet *, u_long, caddr_t);
    181 void	fxp_watchdog(struct ifnet *);
    182 int	fxp_init(struct ifnet *);
    183 void	fxp_stop(struct ifnet *, int);
    184 
    185 void	fxp_txintr(struct fxp_softc *);
    186 void	fxp_rxintr(struct fxp_softc *);
    187 
    188 void	fxp_rxdrain(struct fxp_softc *);
    189 int	fxp_add_rfabuf(struct fxp_softc *, bus_dmamap_t, int);
    190 int	fxp_mdi_read(struct device *, int, int);
    191 void	fxp_statchg(struct device *);
    192 void	fxp_mdi_write(struct device *, int, int, int);
    193 void	fxp_autosize_eeprom(struct fxp_softc*);
    194 void	fxp_read_eeprom(struct fxp_softc *, u_int16_t *, int, int);
    195 void	fxp_get_info(struct fxp_softc *, u_int8_t *);
    196 void	fxp_tick(void *);
    197 void	fxp_mc_setup(struct fxp_softc *);
    198 
    199 void	fxp_shutdown(void *);
    200 void	fxp_power(int, void *);
    201 
    202 int	fxp_copy_small = 0;
    203 
    204 struct fxp_phytype {
    205 	int	fp_phy;		/* type of PHY, -1 for MII at the end. */
    206 	void	(*fp_init)(struct fxp_softc *);
    207 } fxp_phytype_table[] = {
    208 	{ FXP_PHY_80C24,		fxp_80c24_initmedia },
    209 	{ -1,				fxp_mii_initmedia },
    210 };
    211 
    212 /*
    213  * Set initial transmit threshold at 64 (512 bytes). This is
    214  * increased by 64 (512 bytes) at a time, to maximum of 192
    215  * (1536 bytes), if an underrun occurs.
    216  */
    217 static int tx_threshold = 64;
    218 
    219 /*
    220  * Wait for the previous command to be accepted (but not necessarily
    221  * completed).
    222  */
    223 static __inline void
    224 fxp_scb_wait(struct fxp_softc *sc)
    225 {
    226 	int i = 10000;
    227 
    228 	while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i)
    229 		delay(2);
    230 	if (i == 0)
    231 		printf("%s: WARNING: SCB timed out!\n", sc->sc_dev.dv_xname);
    232 }
    233 
    234 /*
    235  * Submit a command to the i82557.
    236  */
    237 static __inline void
    238 fxp_scb_cmd(struct fxp_softc *sc, u_int8_t cmd)
    239 {
    240 
    241 	if (cmd == FXP_SCB_COMMAND_CU_RESUME &&
    242 	    (sc->sc_flags & FXPF_FIX_RESUME_BUG) != 0) {
    243 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_NOP);
    244 		fxp_scb_wait(sc);
    245 	}
    246 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, cmd);
    247 }
    248 
    249 /*
    250  * Finish attaching an i82557 interface.  Called by bus-specific front-end.
    251  */
    252 void
    253 fxp_attach(struct fxp_softc *sc)
    254 {
    255 	u_int8_t enaddr[ETHER_ADDR_LEN];
    256 	struct ifnet *ifp;
    257 	bus_dma_segment_t seg;
    258 	int rseg, i, error;
    259 	struct fxp_phytype *fp;
    260 
    261 	callout_init(&sc->sc_callout);
    262 
    263 	/* Start out using the standard RFA. */
    264 	sc->sc_rfa_size = RFA_SIZE;
    265 
    266 	/*
    267 	 * Enable some good stuff on i82558 and later.
    268 	 */
    269 	if (sc->sc_rev >= FXP_REV_82558_A4) {
    270 		/* Enable the extended TxCB. */
    271 		sc->sc_flags |= FXPF_EXT_TXCB;
    272 	}
    273 
    274 	/*
    275 	 * Allocate the control data structures, and create and load the
    276 	 * DMA map for it.
    277 	 */
    278 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    279 	    sizeof(struct fxp_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
    280 	    0)) != 0) {
    281 		printf("%s: unable to allocate control data, error = %d\n",
    282 		    sc->sc_dev.dv_xname, error);
    283 		goto fail_0;
    284 	}
    285 
    286 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    287 	    sizeof(struct fxp_control_data), (caddr_t *)&sc->sc_control_data,
    288 	    BUS_DMA_COHERENT)) != 0) {
    289 		printf("%s: unable to map control data, error = %d\n",
    290 		    sc->sc_dev.dv_xname, error);
    291 		goto fail_1;
    292 	}
    293 	sc->sc_cdseg = seg;
    294 	sc->sc_cdnseg = rseg;
    295 
    296 	memset(sc->sc_control_data, 0, sizeof(struct fxp_control_data));
    297 
    298 	if ((error = bus_dmamap_create(sc->sc_dmat,
    299 	    sizeof(struct fxp_control_data), 1,
    300 	    sizeof(struct fxp_control_data), 0, 0, &sc->sc_dmamap)) != 0) {
    301 		printf("%s: unable to create control data DMA map, "
    302 		    "error = %d\n", sc->sc_dev.dv_xname, error);
    303 		goto fail_2;
    304 	}
    305 
    306 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap,
    307 	    sc->sc_control_data, sizeof(struct fxp_control_data), NULL,
    308 	    0)) != 0) {
    309 		printf("%s: can't load control data DMA map, error = %d\n",
    310 		    sc->sc_dev.dv_xname, error);
    311 		goto fail_3;
    312 	}
    313 
    314 	/*
    315 	 * Create the transmit buffer DMA maps.
    316 	 */
    317 	for (i = 0; i < FXP_NTXCB; i++) {
    318 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    319 		    FXP_NTXSEG, MCLBYTES, 0, 0,
    320 		    &FXP_DSTX(sc, i)->txs_dmamap)) != 0) {
    321 			printf("%s: unable to create tx DMA map %d, "
    322 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    323 			goto fail_4;
    324 		}
    325 	}
    326 
    327 	/*
    328 	 * Create the receive buffer DMA maps.
    329 	 */
    330 	for (i = 0; i < FXP_NRFABUFS; i++) {
    331 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
    332 		    MCLBYTES, 0, 0, &sc->sc_rxmaps[i])) != 0) {
    333 			printf("%s: unable to create rx DMA map %d, "
    334 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    335 			goto fail_5;
    336 		}
    337 	}
    338 
    339 	/* Initialize MAC address and media structures. */
    340 	fxp_get_info(sc, enaddr);
    341 
    342 	printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
    343 	    ether_sprintf(enaddr));
    344 
    345 	ifp = &sc->sc_ethercom.ec_if;
    346 
    347 	/*
    348 	 * Get info about our media interface, and initialize it.  Note
    349 	 * the table terminates itself with a phy of -1, indicating
    350 	 * that we're using MII.
    351 	 */
    352 	for (fp = fxp_phytype_table; fp->fp_phy != -1; fp++)
    353 		if (fp->fp_phy == sc->phy_primary_device)
    354 			break;
    355 	(*fp->fp_init)(sc);
    356 
    357 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    358 	ifp->if_softc = sc;
    359 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    360 	ifp->if_ioctl = fxp_ioctl;
    361 	ifp->if_start = fxp_start;
    362 	ifp->if_watchdog = fxp_watchdog;
    363 	ifp->if_init = fxp_init;
    364 	ifp->if_stop = fxp_stop;
    365 	IFQ_SET_READY(&ifp->if_snd);
    366 
    367 	/*
    368 	 * We can support 802.1Q VLAN-sized frames.
    369 	 */
    370 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    371 
    372 	/*
    373 	 * Attach the interface.
    374 	 */
    375 	if_attach(ifp);
    376 	ether_ifattach(ifp, enaddr);
    377 #if NRND > 0
    378 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    379 	    RND_TYPE_NET, 0);
    380 #endif
    381 
    382 #ifdef FXP_EVENT_COUNTERS
    383 	evcnt_attach_dynamic(&sc->sc_ev_txstall, EVCNT_TYPE_MISC,
    384 	    NULL, sc->sc_dev.dv_xname, "txstall");
    385 	evcnt_attach_dynamic(&sc->sc_ev_txintr, EVCNT_TYPE_INTR,
    386 	    NULL, sc->sc_dev.dv_xname, "txintr");
    387 	evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_INTR,
    388 	    NULL, sc->sc_dev.dv_xname, "rxintr");
    389 #endif /* FXP_EVENT_COUNTERS */
    390 
    391 	/*
    392 	 * Add shutdown hook so that DMA is disabled prior to reboot. Not
    393 	 * doing do could allow DMA to corrupt kernel memory during the
    394 	 * reboot before the driver initializes.
    395 	 */
    396 	sc->sc_sdhook = shutdownhook_establish(fxp_shutdown, sc);
    397 	if (sc->sc_sdhook == NULL)
    398 		printf("%s: WARNING: unable to establish shutdown hook\n",
    399 		    sc->sc_dev.dv_xname);
    400 	/*
    401   	 * Add suspend hook, for similar reasons..
    402 	 */
    403 	sc->sc_powerhook = powerhook_establish(fxp_power, sc);
    404 	if (sc->sc_powerhook == NULL)
    405 		printf("%s: WARNING: unable to establish power hook\n",
    406 		    sc->sc_dev.dv_xname);
    407 
    408 	/* The attach is successful. */
    409 	sc->sc_flags |= FXPF_ATTACHED;
    410 
    411 	return;
    412 
    413 	/*
    414 	 * Free any resources we've allocated during the failed attach
    415 	 * attempt.  Do this in reverse order and fall though.
    416 	 */
    417  fail_5:
    418 	for (i = 0; i < FXP_NRFABUFS; i++) {
    419 		if (sc->sc_rxmaps[i] != NULL)
    420 			bus_dmamap_destroy(sc->sc_dmat, sc->sc_rxmaps[i]);
    421 	}
    422  fail_4:
    423 	for (i = 0; i < FXP_NTXCB; i++) {
    424 		if (FXP_DSTX(sc, i)->txs_dmamap != NULL)
    425 			bus_dmamap_destroy(sc->sc_dmat,
    426 			    FXP_DSTX(sc, i)->txs_dmamap);
    427 	}
    428 	bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap);
    429  fail_3:
    430 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap);
    431  fail_2:
    432 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
    433 	    sizeof(struct fxp_control_data));
    434  fail_1:
    435 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    436  fail_0:
    437 	return;
    438 }
    439 
    440 void
    441 fxp_mii_initmedia(struct fxp_softc *sc)
    442 {
    443 	int flags;
    444 
    445 	sc->sc_flags |= FXPF_MII;
    446 
    447 	sc->sc_mii.mii_ifp = &sc->sc_ethercom.ec_if;
    448 	sc->sc_mii.mii_readreg = fxp_mdi_read;
    449 	sc->sc_mii.mii_writereg = fxp_mdi_write;
    450 	sc->sc_mii.mii_statchg = fxp_statchg;
    451 	ifmedia_init(&sc->sc_mii.mii_media, 0, fxp_mii_mediachange,
    452 	    fxp_mii_mediastatus);
    453 
    454 	flags = MIIF_NOISOLATE;
    455 	if (sc->sc_rev >= FXP_REV_82558_A4)
    456 		flags |= MIIF_DOPAUSE;
    457 	/*
    458 	 * The i82557 wedges if all of its PHYs are isolated!
    459 	 */
    460 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
    461 	    MII_OFFSET_ANY, flags);
    462 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
    463 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
    464 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
    465 	} else
    466 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
    467 }
    468 
    469 void
    470 fxp_80c24_initmedia(struct fxp_softc *sc)
    471 {
    472 
    473 	/*
    474 	 * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
    475 	 * doesn't have a programming interface of any sort.  The
    476 	 * media is sensed automatically based on how the link partner
    477 	 * is configured.  This is, in essence, manual configuration.
    478 	 */
    479 	printf("%s: Seeq 80c24 AutoDUPLEX media interface present\n",
    480 	    sc->sc_dev.dv_xname);
    481 	ifmedia_init(&sc->sc_mii.mii_media, 0, fxp_80c24_mediachange,
    482 	    fxp_80c24_mediastatus);
    483 	ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
    484 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_MANUAL);
    485 }
    486 
    487 /*
    488  * Device shutdown routine. Called at system shutdown after sync. The
    489  * main purpose of this routine is to shut off receiver DMA so that
    490  * kernel memory doesn't get clobbered during warmboot.
    491  */
    492 void
    493 fxp_shutdown(void *arg)
    494 {
    495 	struct fxp_softc *sc = arg;
    496 
    497 	/*
    498 	 * Since the system's going to halt shortly, don't bother
    499 	 * freeing mbufs.
    500 	 */
    501 	fxp_stop(&sc->sc_ethercom.ec_if, 0);
    502 }
    503 /*
    504  * Power handler routine. Called when the system is transitioning
    505  * into/out of power save modes.  As with fxp_shutdown, the main
    506  * purpose of this routine is to shut off receiver DMA so it doesn't
    507  * clobber kernel memory at the wrong time.
    508  */
    509 void
    510 fxp_power(int why, void *arg)
    511 {
    512 	struct fxp_softc *sc = arg;
    513 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    514 	int s;
    515 
    516 	s = splnet();
    517 	switch (why) {
    518 	case PWR_SUSPEND:
    519 	case PWR_STANDBY:
    520 		fxp_stop(ifp, 0);
    521 		break;
    522 	case PWR_RESUME:
    523 		if (ifp->if_flags & IFF_UP)
    524 			fxp_init(ifp);
    525 		break;
    526 	case PWR_SOFTSUSPEND:
    527 	case PWR_SOFTSTANDBY:
    528 	case PWR_SOFTRESUME:
    529 		break;
    530 	}
    531 	splx(s);
    532 }
    533 
    534 /*
    535  * Initialize the interface media.
    536  */
    537 void
    538 fxp_get_info(struct fxp_softc *sc, u_int8_t *enaddr)
    539 {
    540 	u_int16_t data, myea[ETHER_ADDR_LEN / 2];
    541 
    542 	/*
    543 	 * Reset to a stable state.
    544 	 */
    545 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
    546 	DELAY(10);
    547 
    548 	sc->sc_eeprom_size = 0;
    549 	fxp_autosize_eeprom(sc);
    550 	if(sc->sc_eeprom_size == 0) {
    551 	    printf("%s: failed to detect EEPROM size\n", sc->sc_dev.dv_xname);
    552 	    sc->sc_eeprom_size = 6; /* XXX panic here? */
    553 	}
    554 #ifdef DEBUG
    555 	printf("%s: detected %d word EEPROM\n",
    556 	       sc->sc_dev.dv_xname,
    557 	       1 << sc->sc_eeprom_size);
    558 #endif
    559 
    560 	/*
    561 	 * Get info about the primary PHY
    562 	 */
    563 	fxp_read_eeprom(sc, &data, 6, 1);
    564 	sc->phy_primary_device =
    565 	    (data & FXP_PHY_DEVICE_MASK) >> FXP_PHY_DEVICE_SHIFT;
    566 
    567 	/*
    568 	 * Read MAC address.
    569 	 */
    570 	fxp_read_eeprom(sc, myea, 0, 3);
    571 	enaddr[0] = myea[0] & 0xff;
    572 	enaddr[1] = myea[0] >> 8;
    573 	enaddr[2] = myea[1] & 0xff;
    574 	enaddr[3] = myea[1] >> 8;
    575 	enaddr[4] = myea[2] & 0xff;
    576 	enaddr[5] = myea[2] >> 8;
    577 }
    578 
    579 /*
    580  * Figure out EEPROM size.
    581  *
    582  * 559's can have either 64-word or 256-word EEPROMs, the 558
    583  * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
    584  * talks about the existance of 16 to 256 word EEPROMs.
    585  *
    586  * The only known sizes are 64 and 256, where the 256 version is used
    587  * by CardBus cards to store CIS information.
    588  *
    589  * The address is shifted in msb-to-lsb, and after the last
    590  * address-bit the EEPROM is supposed to output a `dummy zero' bit,
    591  * after which follows the actual data. We try to detect this zero, by
    592  * probing the data-out bit in the EEPROM control register just after
    593  * having shifted in a bit. If the bit is zero, we assume we've
    594  * shifted enough address bits. The data-out should be tri-state,
    595  * before this, which should translate to a logical one.
    596  *
    597  * Other ways to do this would be to try to read a register with known
    598  * contents with a varying number of address bits, but no such
    599  * register seem to be available. The high bits of register 10 are 01
    600  * on the 558 and 559, but apparently not on the 557.
    601  *
    602  * The Linux driver computes a checksum on the EEPROM data, but the
    603  * value of this checksum is not very well documented.
    604  */
    605 
    606 void
    607 fxp_autosize_eeprom(struct fxp_softc *sc)
    608 {
    609 	u_int16_t reg;
    610 	int x;
    611 
    612 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
    613 	/*
    614 	 * Shift in read opcode.
    615 	 */
    616 	for (x = 3; x > 0; x--) {
    617 		if (FXP_EEPROM_OPC_READ & (1 << (x - 1))) {
    618 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
    619 		} else {
    620 			reg = FXP_EEPROM_EECS;
    621 		}
    622 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
    623 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
    624 			    reg | FXP_EEPROM_EESK);
    625 		DELAY(4);
    626 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
    627 		DELAY(4);
    628 	}
    629 	/*
    630 	 * Shift in address, wait for the dummy zero following a correct
    631 	 * address shift.
    632 	 */
    633 	for (x = 1; x <=  8; x++) {
    634 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
    635 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
    636 		    FXP_EEPROM_EECS | FXP_EEPROM_EESK);
    637 		DELAY(4);
    638 		if((CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) &
    639 		    FXP_EEPROM_EEDO) == 0)
    640 			break;
    641 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
    642 		DELAY(4);
    643 	}
    644 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
    645 	DELAY(4);
    646 	if(x != 6 && x != 8) {
    647 #ifdef DEBUG
    648 		printf("%s: strange EEPROM size (%d)\n",
    649 		       sc->sc_dev.dv_xname, 1 << x);
    650 #endif
    651 	} else
    652 		sc->sc_eeprom_size = x;
    653 }
    654 
    655 /*
    656  * Read from the serial EEPROM. Basically, you manually shift in
    657  * the read opcode (one bit at a time) and then shift in the address,
    658  * and then you shift out the data (all of this one bit at a time).
    659  * The word size is 16 bits, so you have to provide the address for
    660  * every 16 bits of data.
    661  */
    662 void
    663 fxp_read_eeprom(struct fxp_softc *sc, u_int16_t *data, int offset, int words)
    664 {
    665 	u_int16_t reg;
    666 	int i, x;
    667 
    668 	for (i = 0; i < words; i++) {
    669 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
    670 		/*
    671 		 * Shift in read opcode.
    672 		 */
    673 		for (x = 3; x > 0; x--) {
    674 			if (FXP_EEPROM_OPC_READ & (1 << (x - 1))) {
    675 				reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
    676 			} else {
    677 				reg = FXP_EEPROM_EECS;
    678 			}
    679 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
    680 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
    681 			    reg | FXP_EEPROM_EESK);
    682 			DELAY(4);
    683 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
    684 			DELAY(4);
    685 		}
    686 		/*
    687 		 * Shift in address.
    688 		 */
    689 		for (x = sc->sc_eeprom_size; x > 0; x--) {
    690 			if ((i + offset) & (1 << (x - 1))) {
    691 			    reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
    692 			} else {
    693 			    reg = FXP_EEPROM_EECS;
    694 			}
    695 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
    696 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
    697 			    reg | FXP_EEPROM_EESK);
    698 			DELAY(4);
    699 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
    700 			DELAY(4);
    701 		}
    702 		reg = FXP_EEPROM_EECS;
    703 		data[i] = 0;
    704 		/*
    705 		 * Shift out data.
    706 		 */
    707 		for (x = 16; x > 0; x--) {
    708 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
    709 			    reg | FXP_EEPROM_EESK);
    710 			DELAY(4);
    711 			if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) &
    712 			    FXP_EEPROM_EEDO)
    713 				data[i] |= (1 << (x - 1));
    714 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
    715 			DELAY(4);
    716 		}
    717 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
    718 		DELAY(4);
    719 	}
    720 }
    721 
    722 /*
    723  * Start packet transmission on the interface.
    724  */
    725 void
    726 fxp_start(struct ifnet *ifp)
    727 {
    728 	struct fxp_softc *sc = ifp->if_softc;
    729 	struct mbuf *m0, *m;
    730 	struct fxp_txdesc *txd;
    731 	struct fxp_txsoft *txs;
    732 	bus_dmamap_t dmamap;
    733 	int error, lasttx, nexttx, opending, seg;
    734 
    735 	/*
    736 	 * If we want a re-init, bail out now.
    737 	 */
    738 	if (sc->sc_flags & FXPF_WANTINIT) {
    739 		ifp->if_flags |= IFF_OACTIVE;
    740 		return;
    741 	}
    742 
    743 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    744 		return;
    745 
    746 	/*
    747 	 * Remember the previous txpending and the current lasttx.
    748 	 */
    749 	opending = sc->sc_txpending;
    750 	lasttx = sc->sc_txlast;
    751 
    752 	/*
    753 	 * Loop through the send queue, setting up transmit descriptors
    754 	 * until we drain the queue, or use up all available transmit
    755 	 * descriptors.
    756 	 */
    757 	for (;;) {
    758 		/*
    759 		 * Grab a packet off the queue.
    760 		 */
    761 		IFQ_POLL(&ifp->if_snd, m0);
    762 		if (m0 == NULL)
    763 			break;
    764 		m = NULL;
    765 
    766 		if (sc->sc_txpending == FXP_NTXCB) {
    767 			FXP_EVCNT_INCR(&sc->sc_ev_txstall);
    768 			break;
    769 		}
    770 
    771 		/*
    772 		 * Get the next available transmit descriptor.
    773 		 */
    774 		nexttx = FXP_NEXTTX(sc->sc_txlast);
    775 		txd = FXP_CDTX(sc, nexttx);
    776 		txs = FXP_DSTX(sc, nexttx);
    777 		dmamap = txs->txs_dmamap;
    778 
    779 		/*
    780 		 * Load the DMA map.  If this fails, the packet either
    781 		 * didn't fit in the allotted number of frags, or we were
    782 		 * short on resources.  In this case, we'll copy and try
    783 		 * again.
    784 		 */
    785 		if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
    786 		    BUS_DMA_WRITE|BUS_DMA_NOWAIT) != 0) {
    787 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    788 			if (m == NULL) {
    789 				printf("%s: unable to allocate Tx mbuf\n",
    790 				    sc->sc_dev.dv_xname);
    791 				break;
    792 			}
    793 			if (m0->m_pkthdr.len > MHLEN) {
    794 				MCLGET(m, M_DONTWAIT);
    795 				if ((m->m_flags & M_EXT) == 0) {
    796 					printf("%s: unable to allocate Tx "
    797 					    "cluster\n", sc->sc_dev.dv_xname);
    798 					m_freem(m);
    799 					break;
    800 				}
    801 			}
    802 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
    803 			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
    804 			error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
    805 			    m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
    806 			if (error) {
    807 				printf("%s: unable to load Tx buffer, "
    808 				    "error = %d\n", sc->sc_dev.dv_xname, error);
    809 				break;
    810 			}
    811 		}
    812 
    813 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    814 		if (m != NULL) {
    815 			m_freem(m0);
    816 			m0 = m;
    817 		}
    818 
    819 		/* Initialize the fraglist. */
    820 		for (seg = 0; seg < dmamap->dm_nsegs; seg++) {
    821 			txd->txd_tbd[seg].tb_addr =
    822 			    htole32(dmamap->dm_segs[seg].ds_addr);
    823 			txd->txd_tbd[seg].tb_size =
    824 			    htole32(dmamap->dm_segs[seg].ds_len);
    825 		}
    826 
    827 		/* Sync the DMA map. */
    828 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
    829 		    BUS_DMASYNC_PREWRITE);
    830 
    831 		/*
    832 		 * Store a pointer to the packet so we can free it later.
    833 		 */
    834 		txs->txs_mbuf = m0;
    835 
    836 		/*
    837 		 * Initialize the transmit descriptor.
    838 		 */
    839 		/* BIG_ENDIAN: no need to swap to store 0 */
    840 		txd->txd_txcb.cb_status = 0;
    841 		txd->txd_txcb.cb_command =
    842 		    htole16(FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF);
    843 		txd->txd_txcb.tx_threshold = tx_threshold;
    844 		txd->txd_txcb.tbd_number = dmamap->dm_nsegs;
    845 
    846 		FXP_CDTXSYNC(sc, nexttx,
    847 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    848 
    849 		/* Advance the tx pointer. */
    850 		sc->sc_txpending++;
    851 		sc->sc_txlast = nexttx;
    852 
    853 #if NBPFILTER > 0
    854 		/*
    855 		 * Pass packet to bpf if there is a listener.
    856 		 */
    857 		if (ifp->if_bpf)
    858 			bpf_mtap(ifp->if_bpf, m0);
    859 #endif
    860 	}
    861 
    862 	if (sc->sc_txpending == FXP_NTXCB) {
    863 		/* No more slots; notify upper layer. */
    864 		ifp->if_flags |= IFF_OACTIVE;
    865 	}
    866 
    867 	if (sc->sc_txpending != opending) {
    868 		/*
    869 		 * We enqueued packets.  If the transmitter was idle,
    870 		 * reset the txdirty pointer.
    871 		 */
    872 		if (opending == 0)
    873 			sc->sc_txdirty = FXP_NEXTTX(lasttx);
    874 
    875 		/*
    876 		 * Cause the chip to interrupt and suspend command
    877 		 * processing once the last packet we've enqueued
    878 		 * has been transmitted.
    879 		 */
    880 		FXP_CDTX(sc, sc->sc_txlast)->txd_txcb.cb_command |=
    881 		    htole16(FXP_CB_COMMAND_I | FXP_CB_COMMAND_S);
    882 		FXP_CDTXSYNC(sc, sc->sc_txlast,
    883 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    884 
    885 		/*
    886 		 * The entire packet chain is set up.  Clear the suspend bit
    887 		 * on the command prior to the first packet we set up.
    888 		 */
    889 		FXP_CDTXSYNC(sc, lasttx,
    890 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    891 		FXP_CDTX(sc, lasttx)->txd_txcb.cb_command &=
    892 		    htole16(~FXP_CB_COMMAND_S);
    893 		FXP_CDTXSYNC(sc, lasttx,
    894 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    895 
    896 		/*
    897 		 * Issue a Resume command in case the chip was suspended.
    898 		 */
    899 		fxp_scb_wait(sc);
    900 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
    901 
    902 		/* Set a watchdog timer in case the chip flakes out. */
    903 		ifp->if_timer = 5;
    904 	}
    905 }
    906 
    907 /*
    908  * Process interface interrupts.
    909  */
    910 int
    911 fxp_intr(void *arg)
    912 {
    913 	struct fxp_softc *sc = arg;
    914 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    915 	bus_dmamap_t rxmap;
    916 	int claimed = 0;
    917 	u_int8_t statack;
    918 
    919 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    920 		return (0);
    921 	/*
    922 	 * If the interface isn't running, don't try to
    923 	 * service the interrupt.. just ack it and bail.
    924 	 */
    925 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
    926 		statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK);
    927 		if (statack) {
    928 			claimed = 1;
    929 			CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
    930 		}
    931 		return (claimed);
    932 	}
    933 
    934 	while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
    935 		claimed = 1;
    936 
    937 		/*
    938 		 * First ACK all the interrupts in this pass.
    939 		 */
    940 		CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
    941 
    942 		/*
    943 		 * Process receiver interrupts. If a no-resource (RNR)
    944 		 * condition exists, get whatever packets we can and
    945 		 * re-start the receiver.
    946 		 */
    947 		if (statack & (FXP_SCB_STATACK_FR | FXP_SCB_STATACK_RNR)) {
    948 			FXP_EVCNT_INCR(&sc->sc_ev_rxintr);
    949 			fxp_rxintr(sc);
    950 		}
    951 
    952 		if (statack & FXP_SCB_STATACK_RNR) {
    953 			rxmap = M_GETCTX(sc->sc_rxq.ifq_head, bus_dmamap_t);
    954 			fxp_scb_wait(sc);
    955 			CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
    956 			    rxmap->dm_segs[0].ds_addr +
    957 			    RFA_ALIGNMENT_FUDGE);
    958 			fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
    959 		}
    960 
    961 		/*
    962 		 * Free any finished transmit mbuf chains.
    963 		 */
    964 		if (statack & (FXP_SCB_STATACK_CXTNO|FXP_SCB_STATACK_CNA)) {
    965 			FXP_EVCNT_INCR(&sc->sc_ev_txintr);
    966 			fxp_txintr(sc);
    967 
    968 			/*
    969 			 * Try to get more packets going.
    970 			 */
    971 			fxp_start(ifp);
    972 
    973 			if (sc->sc_txpending == 0) {
    974 				/*
    975 				 * If we want a re-init, do that now.
    976 				 */
    977 				if (sc->sc_flags & FXPF_WANTINIT)
    978 					(void) fxp_init(ifp);
    979 			}
    980 		}
    981 	}
    982 
    983 #if NRND > 0
    984 	if (claimed)
    985 		rnd_add_uint32(&sc->rnd_source, statack);
    986 #endif
    987 	return (claimed);
    988 }
    989 
    990 /*
    991  * Handle transmit completion interrupts.
    992  */
    993 void
    994 fxp_txintr(struct fxp_softc *sc)
    995 {
    996 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    997 	struct fxp_txdesc *txd;
    998 	struct fxp_txsoft *txs;
    999 	int i;
   1000 	u_int16_t txstat;
   1001 
   1002 	ifp->if_flags &= ~IFF_OACTIVE;
   1003 	for (i = sc->sc_txdirty; sc->sc_txpending != 0;
   1004 	     i = FXP_NEXTTX(i), sc->sc_txpending--) {
   1005 		txd = FXP_CDTX(sc, i);
   1006 		txs = FXP_DSTX(sc, i);
   1007 
   1008 		FXP_CDTXSYNC(sc, i,
   1009 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1010 
   1011 		txstat = le16toh(txd->txd_txcb.cb_status);
   1012 
   1013 		if ((txstat & FXP_CB_STATUS_C) == 0)
   1014 			break;
   1015 
   1016 		bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
   1017 		    0, txs->txs_dmamap->dm_mapsize,
   1018 		    BUS_DMASYNC_POSTWRITE);
   1019 		bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   1020 		m_freem(txs->txs_mbuf);
   1021 		txs->txs_mbuf = NULL;
   1022 	}
   1023 
   1024 	/* Update the dirty transmit buffer pointer. */
   1025 	sc->sc_txdirty = i;
   1026 
   1027 	/*
   1028 	 * Cancel the watchdog timer if there are no pending
   1029 	 * transmissions.
   1030 	 */
   1031 	if (sc->sc_txpending == 0)
   1032 		ifp->if_timer = 0;
   1033 }
   1034 
   1035 /*
   1036  * Handle receive interrupts.
   1037  */
   1038 void
   1039 fxp_rxintr(struct fxp_softc *sc)
   1040 {
   1041 	struct ethercom *ec = &sc->sc_ethercom;
   1042 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1043 	struct mbuf *m, *m0;
   1044 	bus_dmamap_t rxmap;
   1045 	struct fxp_rfa *rfa;
   1046 	u_int16_t len, rxstat;
   1047 
   1048 	for (;;) {
   1049 		m = sc->sc_rxq.ifq_head;
   1050 		rfa = FXP_MTORFA(m);
   1051 		rxmap = M_GETCTX(m, bus_dmamap_t);
   1052 
   1053 		FXP_RFASYNC(sc, m,
   1054 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1055 
   1056 		rxstat = le16toh(rfa->rfa_status);
   1057 
   1058 		if ((rxstat & FXP_RFA_STATUS_C) == 0) {
   1059 			/*
   1060 			 * We have processed all of the
   1061 			 * receive buffers.
   1062 			 */
   1063 			FXP_RFASYNC(sc, m, BUS_DMASYNC_PREREAD);
   1064 			return;
   1065 		}
   1066 
   1067 		IF_DEQUEUE(&sc->sc_rxq, m);
   1068 
   1069 		FXP_RXBUFSYNC(sc, m, BUS_DMASYNC_POSTREAD);
   1070 
   1071 		len = le16toh(rfa->actual_size) &
   1072 		    (m->m_ext.ext_size - 1);
   1073 
   1074 		if (len < sizeof(struct ether_header)) {
   1075 			/*
   1076 			 * Runt packet; drop it now.
   1077 			 */
   1078 			FXP_INIT_RFABUF(sc, m);
   1079 			continue;
   1080 		}
   1081 
   1082 		/*
   1083 		 * If support for 802.1Q VLAN sized frames is
   1084 		 * enabled, we need to do some additional error
   1085 		 * checking (as we are saving bad frames, in
   1086 		 * order to receive the larger ones).
   1087 		 */
   1088 		if ((ec->ec_capenable & ETHERCAP_VLAN_MTU) != 0 &&
   1089 		    (rxstat & (FXP_RFA_STATUS_OVERRUN|
   1090 			       FXP_RFA_STATUS_RNR|
   1091 			       FXP_RFA_STATUS_ALIGN|
   1092 			       FXP_RFA_STATUS_CRC)) != 0) {
   1093 			FXP_INIT_RFABUF(sc, m);
   1094 			continue;
   1095 		}
   1096 
   1097 		/*
   1098 		 * If the packet is small enough to fit in a
   1099 		 * single header mbuf, allocate one and copy
   1100 		 * the data into it.  This greatly reduces
   1101 		 * memory consumption when we receive lots
   1102 		 * of small packets.
   1103 		 *
   1104 		 * Otherwise, we add a new buffer to the receive
   1105 		 * chain.  If this fails, we drop the packet and
   1106 		 * recycle the old buffer.
   1107 		 */
   1108 		if (fxp_copy_small != 0 && len <= MHLEN) {
   1109 			MGETHDR(m0, M_DONTWAIT, MT_DATA);
   1110 			if (m == NULL)
   1111 				goto dropit;
   1112 			memcpy(mtod(m0, caddr_t),
   1113 			    mtod(m, caddr_t), len);
   1114 			FXP_INIT_RFABUF(sc, m);
   1115 			m = m0;
   1116 		} else {
   1117 			if (fxp_add_rfabuf(sc, rxmap, 1) != 0) {
   1118  dropit:
   1119 				ifp->if_ierrors++;
   1120 				FXP_INIT_RFABUF(sc, m);
   1121 				continue;
   1122 			}
   1123 		}
   1124 
   1125 		m->m_pkthdr.rcvif = ifp;
   1126 		m->m_pkthdr.len = m->m_len = len;
   1127 
   1128 #if NBPFILTER > 0
   1129 		/*
   1130 		 * Pass this up to any BPF listeners, but only
   1131 		 * pass it up the stack it its for us.
   1132 		 */
   1133 		if (ifp->if_bpf)
   1134 			bpf_mtap(ifp->if_bpf, m);
   1135 #endif
   1136 
   1137 		/* Pass it on. */
   1138 		(*ifp->if_input)(ifp, m);
   1139 	}
   1140 }
   1141 
   1142 /*
   1143  * Update packet in/out/collision statistics. The i82557 doesn't
   1144  * allow you to access these counters without doing a fairly
   1145  * expensive DMA to get _all_ of the statistics it maintains, so
   1146  * we do this operation here only once per second. The statistics
   1147  * counters in the kernel are updated from the previous dump-stats
   1148  * DMA and then a new dump-stats DMA is started. The on-chip
   1149  * counters are zeroed when the DMA completes. If we can't start
   1150  * the DMA immediately, we don't wait - we just prepare to read
   1151  * them again next time.
   1152  */
   1153 void
   1154 fxp_tick(void *arg)
   1155 {
   1156 	struct fxp_softc *sc = arg;
   1157 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1158 	struct fxp_stats *sp = &sc->sc_control_data->fcd_stats;
   1159 	int s;
   1160 
   1161 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
   1162 		return;
   1163 
   1164 	s = splnet();
   1165 
   1166 	FXP_CDSTATSSYNC(sc, BUS_DMASYNC_POSTREAD);
   1167 
   1168 	ifp->if_opackets += le32toh(sp->tx_good);
   1169 	ifp->if_collisions += le32toh(sp->tx_total_collisions);
   1170 	if (sp->rx_good) {
   1171 		ifp->if_ipackets += le32toh(sp->rx_good);
   1172 		sc->sc_rxidle = 0;
   1173 	} else {
   1174 		sc->sc_rxidle++;
   1175 	}
   1176 	ifp->if_ierrors +=
   1177 	    le32toh(sp->rx_crc_errors) +
   1178 	    le32toh(sp->rx_alignment_errors) +
   1179 	    le32toh(sp->rx_rnr_errors) +
   1180 	    le32toh(sp->rx_overrun_errors);
   1181 	/*
   1182 	 * If any transmit underruns occurred, bump up the transmit
   1183 	 * threshold by another 512 bytes (64 * 8).
   1184 	 */
   1185 	if (sp->tx_underruns) {
   1186 		ifp->if_oerrors += le32toh(sp->tx_underruns);
   1187 		if (tx_threshold < 192)
   1188 			tx_threshold += 64;
   1189 	}
   1190 
   1191 	/*
   1192 	 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds,
   1193 	 * then assume the receiver has locked up and attempt to clear
   1194 	 * the condition by reprogramming the multicast filter (actually,
   1195 	 * resetting the interface). This is a work-around for a bug in
   1196 	 * the 82557 where the receiver locks up if it gets certain types
   1197 	 * of garbage in the syncronization bits prior to the packet header.
   1198 	 * This bug is supposed to only occur in 10Mbps mode, but has been
   1199 	 * seen to occur in 100Mbps mode as well (perhaps due to a 10/100
   1200 	 * speed transition).
   1201 	 */
   1202 	if (sc->sc_rxidle > FXP_MAX_RX_IDLE) {
   1203 		(void) fxp_init(ifp);
   1204 		splx(s);
   1205 		return;
   1206 	}
   1207 	/*
   1208 	 * If there is no pending command, start another stats
   1209 	 * dump. Otherwise punt for now.
   1210 	 */
   1211 	if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
   1212 		/*
   1213 		 * Start another stats dump.
   1214 		 */
   1215 		FXP_CDSTATSSYNC(sc, BUS_DMASYNC_PREREAD);
   1216 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMPRESET);
   1217 	} else {
   1218 		/*
   1219 		 * A previous command is still waiting to be accepted.
   1220 		 * Just zero our copy of the stats and wait for the
   1221 		 * next timer event to update them.
   1222 		 */
   1223 		/* BIG_ENDIAN: no swap required to store 0 */
   1224 		sp->tx_good = 0;
   1225 		sp->tx_underruns = 0;
   1226 		sp->tx_total_collisions = 0;
   1227 
   1228 		sp->rx_good = 0;
   1229 		sp->rx_crc_errors = 0;
   1230 		sp->rx_alignment_errors = 0;
   1231 		sp->rx_rnr_errors = 0;
   1232 		sp->rx_overrun_errors = 0;
   1233 	}
   1234 
   1235 	if (sc->sc_flags & FXPF_MII) {
   1236 		/* Tick the MII clock. */
   1237 		mii_tick(&sc->sc_mii);
   1238 	}
   1239 
   1240 	splx(s);
   1241 
   1242 	/*
   1243 	 * Schedule another timeout one second from now.
   1244 	 */
   1245 	callout_reset(&sc->sc_callout, hz, fxp_tick, sc);
   1246 }
   1247 
   1248 /*
   1249  * Drain the receive queue.
   1250  */
   1251 void
   1252 fxp_rxdrain(struct fxp_softc *sc)
   1253 {
   1254 	bus_dmamap_t rxmap;
   1255 	struct mbuf *m;
   1256 
   1257 	for (;;) {
   1258 		IF_DEQUEUE(&sc->sc_rxq, m);
   1259 		if (m == NULL)
   1260 			break;
   1261 		rxmap = M_GETCTX(m, bus_dmamap_t);
   1262 		bus_dmamap_unload(sc->sc_dmat, rxmap);
   1263 		FXP_RXMAP_PUT(sc, rxmap);
   1264 		m_freem(m);
   1265 	}
   1266 }
   1267 
   1268 /*
   1269  * Stop the interface. Cancels the statistics updater and resets
   1270  * the interface.
   1271  */
   1272 void
   1273 fxp_stop(struct ifnet *ifp, int disable)
   1274 {
   1275 	struct fxp_softc *sc = ifp->if_softc;
   1276 	struct fxp_txsoft *txs;
   1277 	int i;
   1278 
   1279 	/*
   1280 	 * Turn down interface (done early to avoid bad interactions
   1281 	 * between panics, shutdown hooks, and the watchdog timer)
   1282 	 */
   1283 	ifp->if_timer = 0;
   1284 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1285 
   1286 	/*
   1287 	 * Cancel stats updater.
   1288 	 */
   1289 	callout_stop(&sc->sc_callout);
   1290 	if (sc->sc_flags & FXPF_MII) {
   1291 		/* Down the MII. */
   1292 		mii_down(&sc->sc_mii);
   1293 	}
   1294 
   1295 	/*
   1296 	 * Issue software reset
   1297 	 */
   1298 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
   1299 	DELAY(10);
   1300 
   1301 	/*
   1302 	 * Release any xmit buffers.
   1303 	 */
   1304 	for (i = 0; i < FXP_NTXCB; i++) {
   1305 		txs = FXP_DSTX(sc, i);
   1306 		if (txs->txs_mbuf != NULL) {
   1307 			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   1308 			m_freem(txs->txs_mbuf);
   1309 			txs->txs_mbuf = NULL;
   1310 		}
   1311 	}
   1312 	sc->sc_txpending = 0;
   1313 
   1314 	if (disable) {
   1315 		fxp_rxdrain(sc);
   1316 		fxp_disable(sc);
   1317 	}
   1318 
   1319 }
   1320 
   1321 /*
   1322  * Watchdog/transmission transmit timeout handler. Called when a
   1323  * transmission is started on the interface, but no interrupt is
   1324  * received before the timeout. This usually indicates that the
   1325  * card has wedged for some reason.
   1326  */
   1327 void
   1328 fxp_watchdog(struct ifnet *ifp)
   1329 {
   1330 	struct fxp_softc *sc = ifp->if_softc;
   1331 
   1332 	printf("%s: device timeout\n", sc->sc_dev.dv_xname);
   1333 	ifp->if_oerrors++;
   1334 
   1335 	(void) fxp_init(ifp);
   1336 }
   1337 
   1338 /*
   1339  * Initialize the interface.  Must be called at splnet().
   1340  */
   1341 int
   1342 fxp_init(struct ifnet *ifp)
   1343 {
   1344 	struct fxp_softc *sc = ifp->if_softc;
   1345 	struct fxp_cb_config *cbp;
   1346 	struct fxp_cb_ias *cb_ias;
   1347 	struct fxp_txdesc *txd;
   1348 	bus_dmamap_t rxmap;
   1349 	int i, prm, save_bf, lrxen, allm, error = 0;
   1350 
   1351 	if ((error = fxp_enable(sc)) != 0)
   1352 		goto out;
   1353 
   1354 	/*
   1355 	 * Cancel any pending I/O
   1356 	 */
   1357 	fxp_stop(ifp, 0);
   1358 
   1359 	/*
   1360 	 * XXX just setting sc_flags to 0 here clears any FXPF_MII
   1361 	 * flag, and this prevents the MII from detaching resulting in
   1362 	 * a panic. The flags field should perhaps be split in runtime
   1363 	 * flags and more static information. For now, just clear the
   1364 	 * only other flag set.
   1365 	 */
   1366 
   1367 	sc->sc_flags &= ~FXPF_WANTINIT;
   1368 
   1369 	/*
   1370 	 * Initialize base of CBL and RFA memory. Loading with zero
   1371 	 * sets it up for regular linear addressing.
   1372 	 */
   1373 	fxp_scb_wait(sc);
   1374 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
   1375 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_BASE);
   1376 
   1377 	fxp_scb_wait(sc);
   1378 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_BASE);
   1379 
   1380 	/*
   1381 	 * Initialize the multicast filter.  Do this now, since we might
   1382 	 * have to setup the config block differently.
   1383 	 */
   1384 	fxp_mc_setup(sc);
   1385 
   1386 	prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
   1387 	allm = (ifp->if_flags & IFF_ALLMULTI) ? 1 : 0;
   1388 
   1389 	/*
   1390 	 * In order to support receiving 802.1Q VLAN frames, we have to
   1391 	 * enable "save bad frames", since they are 4 bytes larger than
   1392 	 * the normal Ethernet maximum frame length.  On i82558 and later,
   1393 	 * we have a better mechanism for this.
   1394 	 */
   1395 	save_bf = 0;
   1396 	lrxen = 0;
   1397 	if (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) {
   1398 		if (sc->sc_rev < FXP_REV_82558_A4)
   1399 			save_bf = 1;
   1400 		else
   1401 			lrxen = 1;
   1402 	}
   1403 
   1404 	/*
   1405 	 * Initialize base of dump-stats buffer.
   1406 	 */
   1407 	fxp_scb_wait(sc);
   1408 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
   1409 	    sc->sc_cddma + FXP_CDSTATSOFF);
   1410 	FXP_CDSTATSSYNC(sc, BUS_DMASYNC_PREREAD);
   1411 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMP_ADR);
   1412 
   1413 	cbp = &sc->sc_control_data->fcd_configcb;
   1414 	memset(cbp, 0, sizeof(struct fxp_cb_config));
   1415 
   1416 	/*
   1417 	 * This copy is kind of disgusting, but there are a bunch of must be
   1418 	 * zero and must be one bits in this structure and this is the easiest
   1419 	 * way to initialize them all to proper values.
   1420 	 */
   1421 	memcpy(cbp, fxp_cb_config_template, sizeof(fxp_cb_config_template));
   1422 
   1423 	/* BIG_ENDIAN: no need to swap to store 0 */
   1424 	cbp->cb_status =	0;
   1425 	cbp->cb_command =	htole16(FXP_CB_COMMAND_CONFIG |
   1426 				    FXP_CB_COMMAND_EL);
   1427 	/* BIG_ENDIAN: no need to swap to store 0xffffffff */
   1428 	cbp->link_addr =	0xffffffff; /* (no) next command */
   1429 					/* bytes in config block */
   1430 	cbp->byte_count =	FXP_CONFIG_LEN;
   1431 	cbp->rx_fifo_limit =	8;	/* rx fifo threshold (32 bytes) */
   1432 	cbp->tx_fifo_limit =	0;	/* tx fifo threshold (0 bytes) */
   1433 	cbp->adaptive_ifs =	0;	/* (no) adaptive interframe spacing */
   1434 	cbp->mwi_enable =	(sc->sc_flags & FXPF_MWI) ? 1 : 0;
   1435 	cbp->type_enable =	0;	/* actually reserved */
   1436 	cbp->read_align_en =	(sc->sc_flags & FXPF_READ_ALIGN) ? 1 : 0;
   1437 	cbp->end_wr_on_cl =	(sc->sc_flags & FXPF_WRITE_ALIGN) ? 1 : 0;
   1438 	cbp->rx_dma_bytecount =	0;	/* (no) rx DMA max */
   1439 	cbp->tx_dma_bytecount =	0;	/* (no) tx DMA max */
   1440 	cbp->dma_mbce =		0;	/* (disable) dma max counters */
   1441 	cbp->late_scb =		0;	/* (don't) defer SCB update */
   1442 	cbp->tno_int_or_tco_en =0;	/* (disable) tx not okay interrupt */
   1443 	cbp->ci_int =		1;	/* interrupt on CU idle */
   1444 	cbp->ext_txcb_dis =	(sc->sc_flags & FXPF_EXT_TXCB) ? 0 : 1;
   1445 	cbp->ext_stats_dis =	1;	/* disable extended counters */
   1446 	cbp->keep_overrun_rx =	0;	/* don't pass overrun frames to host */
   1447 	cbp->save_bf =		save_bf;/* save bad frames */
   1448 	cbp->disc_short_rx =	!prm;	/* discard short packets */
   1449 	cbp->underrun_retry =	1;	/* retry mode (1) on DMA underrun */
   1450 	cbp->two_frames =	0;	/* do not limit FIFO to 2 frames */
   1451 	cbp->dyn_tbd =		0;	/* (no) dynamic TBD mode */
   1452 					/* interface mode */
   1453 	cbp->mediatype =	(sc->sc_flags & FXPF_MII) ? 1 : 0;
   1454 	cbp->csma_dis =		0;	/* (don't) disable link */
   1455 	cbp->tcp_udp_cksum =	0;	/* (don't) enable checksum */
   1456 	cbp->vlan_tco =		0;	/* (don't) enable vlan wakeup */
   1457 	cbp->link_wake_en =	0;	/* (don't) assert PME# on link change */
   1458 	cbp->arp_wake_en =	0;	/* (don't) assert PME# on arp */
   1459 	cbp->mc_wake_en =	0;	/* (don't) assert PME# on mcmatch */
   1460 	cbp->nsai =		1;	/* (don't) disable source addr insert */
   1461 	cbp->preamble_length =	2;	/* (7 byte) preamble */
   1462 	cbp->loopback =		0;	/* (don't) loopback */
   1463 	cbp->linear_priority =	0;	/* (normal CSMA/CD operation) */
   1464 	cbp->linear_pri_mode =	0;	/* (wait after xmit only) */
   1465 	cbp->interfrm_spacing =	6;	/* (96 bits of) interframe spacing */
   1466 	cbp->promiscuous =	prm;	/* promiscuous mode */
   1467 	cbp->bcast_disable =	0;	/* (don't) disable broadcasts */
   1468 	cbp->wait_after_win =	0;	/* (don't) enable modified backoff alg*/
   1469 	cbp->ignore_ul =	0;	/* consider U/L bit in IA matching */
   1470 	cbp->crc16_en =		0;	/* (don't) enable crc-16 algorithm */
   1471 	cbp->crscdt =		(sc->sc_flags & FXPF_MII) ? 0 : 1;
   1472 	cbp->stripping =	!prm;	/* truncate rx packet to byte count */
   1473 	cbp->padding =		1;	/* (do) pad short tx packets */
   1474 	cbp->rcv_crc_xfer =	0;	/* (don't) xfer CRC to host */
   1475 	cbp->long_rx_en =	lrxen;	/* long packet receive enable */
   1476 	cbp->ia_wake_en =	0;	/* (don't) wake up on address match */
   1477 	cbp->magic_pkt_dis =	0;	/* (don't) disable magic packet */
   1478 					/* must set wake_en in PMCSR also */
   1479 	cbp->force_fdx =	0;	/* (don't) force full duplex */
   1480 	cbp->fdx_pin_en =	1;	/* (enable) FDX# pin */
   1481 	cbp->multi_ia =		0;	/* (don't) accept multiple IAs */
   1482 	cbp->mc_all =		allm;	/* accept all multicasts */
   1483 
   1484 	if (sc->sc_rev < FXP_REV_82558_A4) {
   1485 		/*
   1486 		 * The i82557 has no hardware flow control, the values
   1487 		 * here are the defaults for the chip.
   1488 		 */
   1489 		cbp->fc_delay_lsb =	0;
   1490 		cbp->fc_delay_msb =	0x40;
   1491 		cbp->pri_fc_thresh =	3;
   1492 		cbp->tx_fc_dis =	0;
   1493 		cbp->rx_fc_restop =	0;
   1494 		cbp->rx_fc_restart =	0;
   1495 		cbp->fc_filter =	0;
   1496 		cbp->pri_fc_loc =	1;
   1497 	} else {
   1498 		cbp->fc_delay_lsb =	0x1f;
   1499 		cbp->fc_delay_msb =	0x01;
   1500 		cbp->pri_fc_thresh =	3;
   1501 		cbp->tx_fc_dis =	0;	/* enable transmit FC */
   1502 		cbp->rx_fc_restop =	1;	/* enable FC restop frames */
   1503 		cbp->rx_fc_restart =	1;	/* enable FC restart frames */
   1504 		cbp->fc_filter =	!prm;	/* drop FC frames to host */
   1505 		cbp->pri_fc_loc =	1;	/* FC pri location (byte31) */
   1506 	}
   1507 
   1508 	FXP_CDCONFIGSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1509 
   1510 	/*
   1511 	 * Start the config command/DMA.
   1512 	 */
   1513 	fxp_scb_wait(sc);
   1514 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->sc_cddma + FXP_CDCONFIGOFF);
   1515 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
   1516 	/* ...and wait for it to complete. */
   1517 	i = 1000;
   1518 	do {
   1519 		FXP_CDCONFIGSYNC(sc,
   1520 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1521 		DELAY(1);
   1522 	} while ((le16toh(cbp->cb_status) & FXP_CB_STATUS_C) == 0 && --i);
   1523 	if (i == 0) {
   1524 		printf("%s at line %d: dmasync timeout\n",
   1525 		    sc->sc_dev.dv_xname, __LINE__);
   1526 		return ETIMEDOUT;
   1527 	}
   1528 
   1529 	/*
   1530 	 * Initialize the station address.
   1531 	 */
   1532 	cb_ias = &sc->sc_control_data->fcd_iascb;
   1533 	/* BIG_ENDIAN: no need to swap to store 0 */
   1534 	cb_ias->cb_status = 0;
   1535 	cb_ias->cb_command = htole16(FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL);
   1536 	/* BIG_ENDIAN: no need to swap to store 0xffffffff */
   1537 	cb_ias->link_addr = 0xffffffff;
   1538 	memcpy((void *)cb_ias->macaddr, LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
   1539 
   1540 	FXP_CDIASSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1541 
   1542 	/*
   1543 	 * Start the IAS (Individual Address Setup) command/DMA.
   1544 	 */
   1545 	fxp_scb_wait(sc);
   1546 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->sc_cddma + FXP_CDIASOFF);
   1547 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
   1548 	/* ...and wait for it to complete. */
   1549 	i = 1000;
   1550 	do {
   1551 		FXP_CDIASSYNC(sc,
   1552 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1553 		DELAY(1);
   1554 	} while ((le16toh(cb_ias->cb_status) & FXP_CB_STATUS_C) == 0 && --i);
   1555 	if (i == 0) {
   1556 		printf("%s at line %d: dmasync timeout\n",
   1557 		    sc->sc_dev.dv_xname, __LINE__);
   1558 		return ETIMEDOUT;
   1559 	}
   1560 
   1561 	/*
   1562 	 * Initialize the transmit descriptor ring.  txlast is initialized
   1563 	 * to the end of the list so that it will wrap around to the first
   1564 	 * descriptor when the first packet is transmitted.
   1565 	 */
   1566 	for (i = 0; i < FXP_NTXCB; i++) {
   1567 		txd = FXP_CDTX(sc, i);
   1568 		memset(txd, 0, sizeof(*txd));
   1569 		txd->txd_txcb.cb_command =
   1570 		    htole16(FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S);
   1571 		txd->txd_txcb.link_addr =
   1572 		    htole32(FXP_CDTXADDR(sc, FXP_NEXTTX(i)));
   1573 		if (sc->sc_flags & FXPF_EXT_TXCB)
   1574 			txd->txd_txcb.tbd_array_addr =
   1575 			    htole32(FXP_CDTBDADDR(sc, i) +
   1576 				    (2 * sizeof(struct fxp_tbd)));
   1577 		else
   1578 			txd->txd_txcb.tbd_array_addr =
   1579 			    htole32(FXP_CDTBDADDR(sc, i));
   1580 		FXP_CDTXSYNC(sc, i, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1581 	}
   1582 	sc->sc_txpending = 0;
   1583 	sc->sc_txdirty = 0;
   1584 	sc->sc_txlast = FXP_NTXCB - 1;
   1585 
   1586 	/*
   1587 	 * Initialize the receive buffer list.
   1588 	 */
   1589 	sc->sc_rxq.ifq_maxlen = FXP_NRFABUFS;
   1590 	while (sc->sc_rxq.ifq_len < FXP_NRFABUFS) {
   1591 		rxmap = FXP_RXMAP_GET(sc);
   1592 		if ((error = fxp_add_rfabuf(sc, rxmap, 0)) != 0) {
   1593 			printf("%s: unable to allocate or map rx "
   1594 			    "buffer %d, error = %d\n",
   1595 			    sc->sc_dev.dv_xname,
   1596 			    sc->sc_rxq.ifq_len, error);
   1597 			/*
   1598 			 * XXX Should attempt to run with fewer receive
   1599 			 * XXX buffers instead of just failing.
   1600 			 */
   1601 			FXP_RXMAP_PUT(sc, rxmap);
   1602 			fxp_rxdrain(sc);
   1603 			goto out;
   1604 		}
   1605 	}
   1606 	sc->sc_rxidle = 0;
   1607 
   1608 	/*
   1609 	 * Give the transmit ring to the chip.  We do this by pointing
   1610 	 * the chip at the last descriptor (which is a NOP|SUSPEND), and
   1611 	 * issuing a start command.  It will execute the NOP and then
   1612 	 * suspend, pointing at the first descriptor.
   1613 	 */
   1614 	fxp_scb_wait(sc);
   1615 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, FXP_CDTXADDR(sc, sc->sc_txlast));
   1616 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
   1617 
   1618 	/*
   1619 	 * Initialize receiver buffer area - RFA.
   1620 	 */
   1621 	rxmap = M_GETCTX(sc->sc_rxq.ifq_head, bus_dmamap_t);
   1622 	fxp_scb_wait(sc);
   1623 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
   1624 	    rxmap->dm_segs[0].ds_addr + RFA_ALIGNMENT_FUDGE);
   1625 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
   1626 
   1627 	if (sc->sc_flags & FXPF_MII) {
   1628 		/*
   1629 		 * Set current media.
   1630 		 */
   1631 		mii_mediachg(&sc->sc_mii);
   1632 	}
   1633 
   1634 	/*
   1635 	 * ...all done!
   1636 	 */
   1637 	ifp->if_flags |= IFF_RUNNING;
   1638 	ifp->if_flags &= ~IFF_OACTIVE;
   1639 
   1640 	/*
   1641 	 * Start the one second timer.
   1642 	 */
   1643 	callout_reset(&sc->sc_callout, hz, fxp_tick, sc);
   1644 
   1645 	/*
   1646 	 * Attempt to start output on the interface.
   1647 	 */
   1648 	fxp_start(ifp);
   1649 
   1650  out:
   1651 	if (error) {
   1652 		ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1653 		ifp->if_timer = 0;
   1654 		printf("%s: interface not running\n", sc->sc_dev.dv_xname);
   1655 	}
   1656 	return (error);
   1657 }
   1658 
   1659 /*
   1660  * Change media according to request.
   1661  */
   1662 int
   1663 fxp_mii_mediachange(struct ifnet *ifp)
   1664 {
   1665 	struct fxp_softc *sc = ifp->if_softc;
   1666 
   1667 	if (ifp->if_flags & IFF_UP)
   1668 		mii_mediachg(&sc->sc_mii);
   1669 	return (0);
   1670 }
   1671 
   1672 /*
   1673  * Notify the world which media we're using.
   1674  */
   1675 void
   1676 fxp_mii_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
   1677 {
   1678 	struct fxp_softc *sc = ifp->if_softc;
   1679 
   1680 	if(sc->sc_enabled == 0) {
   1681 		ifmr->ifm_active = IFM_ETHER | IFM_NONE;
   1682 		ifmr->ifm_status = 0;
   1683 		return;
   1684 	}
   1685 
   1686 	mii_pollstat(&sc->sc_mii);
   1687 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
   1688 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
   1689 }
   1690 
   1691 int
   1692 fxp_80c24_mediachange(struct ifnet *ifp)
   1693 {
   1694 
   1695 	/* Nothing to do here. */
   1696 	return (0);
   1697 }
   1698 
   1699 void
   1700 fxp_80c24_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
   1701 {
   1702 	struct fxp_softc *sc = ifp->if_softc;
   1703 
   1704 	/*
   1705 	 * Media is currently-selected media.  We cannot determine
   1706 	 * the link status.
   1707 	 */
   1708 	ifmr->ifm_status = 0;
   1709 	ifmr->ifm_active = sc->sc_mii.mii_media.ifm_cur->ifm_media;
   1710 }
   1711 
   1712 /*
   1713  * Add a buffer to the end of the RFA buffer list.
   1714  * Return 0 if successful, error code on failure.
   1715  *
   1716  * The RFA struct is stuck at the beginning of mbuf cluster and the
   1717  * data pointer is fixed up to point just past it.
   1718  */
   1719 int
   1720 fxp_add_rfabuf(struct fxp_softc *sc, bus_dmamap_t rxmap, int unload)
   1721 {
   1722 	struct mbuf *m;
   1723 	int error;
   1724 
   1725 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1726 	if (m == NULL)
   1727 		return (ENOBUFS);
   1728 
   1729 	MCLGET(m, M_DONTWAIT);
   1730 	if ((m->m_flags & M_EXT) == 0) {
   1731 		m_freem(m);
   1732 		return (ENOBUFS);
   1733 	}
   1734 
   1735 	if (unload)
   1736 		bus_dmamap_unload(sc->sc_dmat, rxmap);
   1737 
   1738 	M_SETCTX(m, rxmap);
   1739 
   1740 	error = bus_dmamap_load(sc->sc_dmat, rxmap,
   1741 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
   1742 	    BUS_DMA_READ|BUS_DMA_NOWAIT);
   1743 	if (error) {
   1744 		printf("%s: can't load rx DMA map %d, error = %d\n",
   1745 		    sc->sc_dev.dv_xname, sc->sc_rxq.ifq_len, error);
   1746 		panic("fxp_add_rfabuf");		/* XXX */
   1747 	}
   1748 
   1749 	FXP_INIT_RFABUF(sc, m);
   1750 
   1751 	return (0);
   1752 }
   1753 
   1754 int
   1755 fxp_mdi_read(struct device *self, int phy, int reg)
   1756 {
   1757 	struct fxp_softc *sc = (struct fxp_softc *)self;
   1758 	int count = 10000;
   1759 	int value;
   1760 
   1761 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
   1762 	    (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
   1763 
   1764 	while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0
   1765 	    && count--)
   1766 		DELAY(10);
   1767 
   1768 	if (count <= 0)
   1769 		printf("%s: fxp_mdi_read: timed out\n", sc->sc_dev.dv_xname);
   1770 
   1771 	return (value & 0xffff);
   1772 }
   1773 
   1774 void
   1775 fxp_statchg(struct device *self)
   1776 {
   1777 	struct fxp_softc *sc = (void *) self;
   1778 
   1779 	/*
   1780 	 * Determine whether or not we have to work-around the
   1781 	 * Resume Bug.
   1782 	 */
   1783 	if (sc->sc_flags & FXPF_HAS_RESUME_BUG) {
   1784 		if (IFM_TYPE(sc->sc_mii.mii_media_active) == IFM_10_T)
   1785 			sc->sc_flags |= FXPF_FIX_RESUME_BUG;
   1786 		else
   1787 			sc->sc_flags &= ~FXPF_FIX_RESUME_BUG;
   1788 	}
   1789 }
   1790 
   1791 void
   1792 fxp_mdi_write(struct device *self, int phy, int reg, int value)
   1793 {
   1794 	struct fxp_softc *sc = (struct fxp_softc *)self;
   1795 	int count = 10000;
   1796 
   1797 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
   1798 	    (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
   1799 	    (value & 0xffff));
   1800 
   1801 	while((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
   1802 	    count--)
   1803 		DELAY(10);
   1804 
   1805 	if (count <= 0)
   1806 		printf("%s: fxp_mdi_write: timed out\n", sc->sc_dev.dv_xname);
   1807 }
   1808 
   1809 int
   1810 fxp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
   1811 {
   1812 	struct fxp_softc *sc = ifp->if_softc;
   1813 	struct ifreq *ifr = (struct ifreq *)data;
   1814 	int s, error;
   1815 
   1816 	s = splnet();
   1817 
   1818 	switch (cmd) {
   1819 	case SIOCSIFMEDIA:
   1820 	case SIOCGIFMEDIA:
   1821 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
   1822 		break;
   1823 
   1824 	default:
   1825 		error = ether_ioctl(ifp, cmd, data);
   1826 		if (error == ENETRESET) {
   1827 			if (sc->sc_enabled) {
   1828 				/*
   1829 				 * Multicast list has changed; set the
   1830 				 * hardware filter accordingly.
   1831 				 */
   1832 				if (sc->sc_txpending) {
   1833 					sc->sc_flags |= FXPF_WANTINIT;
   1834 					error = 0;
   1835 				} else
   1836 					error = fxp_init(ifp);
   1837 			} else
   1838 				error = 0;
   1839 		}
   1840 		break;
   1841 	}
   1842 
   1843 	/* Try to get more packets going. */
   1844 	if (sc->sc_enabled)
   1845 		fxp_start(ifp);
   1846 
   1847 	splx(s);
   1848 	return (error);
   1849 }
   1850 
   1851 /*
   1852  * Program the multicast filter.
   1853  *
   1854  * This function must be called at splnet().
   1855  */
   1856 void
   1857 fxp_mc_setup(struct fxp_softc *sc)
   1858 {
   1859 	struct fxp_cb_mcs *mcsp = &sc->sc_control_data->fcd_mcscb;
   1860 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1861 	struct ethercom *ec = &sc->sc_ethercom;
   1862 	struct ether_multi *enm;
   1863 	struct ether_multistep step;
   1864 	int count, nmcasts;
   1865 
   1866 #ifdef DIAGNOSTIC
   1867 	if (sc->sc_txpending)
   1868 		panic("fxp_mc_setup: pending transmissions");
   1869 #endif
   1870 
   1871 	ifp->if_flags &= ~IFF_ALLMULTI;
   1872 
   1873 	/*
   1874 	 * Initialize multicast setup descriptor.
   1875 	 */
   1876 	nmcasts = 0;
   1877 	ETHER_FIRST_MULTI(step, ec, enm);
   1878 	while (enm != NULL) {
   1879 		/*
   1880 		 * Check for too many multicast addresses or if we're
   1881 		 * listening to a range.  Either way, we simply have
   1882 		 * to accept all multicasts.
   1883 		 */
   1884 		if (nmcasts >= MAXMCADDR ||
   1885 		    memcmp(enm->enm_addrlo, enm->enm_addrhi,
   1886 		    ETHER_ADDR_LEN) != 0) {
   1887 			/*
   1888 			 * Callers of this function must do the
   1889 			 * right thing with this.  If we're called
   1890 			 * from outside fxp_init(), the caller must
   1891 			 * detect if the state if IFF_ALLMULTI changes.
   1892 			 * If it does, the caller must then call
   1893 			 * fxp_init(), since allmulti is handled by
   1894 			 * the config block.
   1895 			 */
   1896 			ifp->if_flags |= IFF_ALLMULTI;
   1897 			return;
   1898 		}
   1899 		memcpy((void *)&mcsp->mc_addr[nmcasts][0], enm->enm_addrlo,
   1900 		    ETHER_ADDR_LEN);
   1901 		nmcasts++;
   1902 		ETHER_NEXT_MULTI(step, enm);
   1903 	}
   1904 
   1905 	/* BIG_ENDIAN: no need to swap to store 0 */
   1906 	mcsp->cb_status = 0;
   1907 	mcsp->cb_command = htole16(FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_EL);
   1908 	mcsp->link_addr = htole32(FXP_CDTXADDR(sc, FXP_NEXTTX(sc->sc_txlast)));
   1909 	mcsp->mc_cnt = htole16(nmcasts * ETHER_ADDR_LEN);
   1910 
   1911 	FXP_CDMCSSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1912 
   1913 	/*
   1914 	 * Wait until the command unit is not active.  This should never
   1915 	 * happen since nothing is queued, but make sure anyway.
   1916 	 */
   1917 	count = 100;
   1918 	while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) ==
   1919 	    FXP_SCB_CUS_ACTIVE && --count)
   1920 		DELAY(1);
   1921 	if (count == 0) {
   1922 		printf("%s at line %d: command queue timeout\n",
   1923 		    sc->sc_dev.dv_xname, __LINE__);
   1924 		return;
   1925 	}
   1926 
   1927 	/*
   1928 	 * Start the multicast setup command/DMA.
   1929 	 */
   1930 	fxp_scb_wait(sc);
   1931 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->sc_cddma + FXP_CDMCSOFF);
   1932 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
   1933 
   1934 	/* ...and wait for it to complete. */
   1935 	count = 1000;
   1936 	do {
   1937 		FXP_CDMCSSYNC(sc,
   1938 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1939 		DELAY(1);
   1940 	} while ((le16toh(mcsp->cb_status) & FXP_CB_STATUS_C) == 0 && --count);
   1941 	if (count == 0) {
   1942 		printf("%s at line %d: dmasync timeout\n",
   1943 		    sc->sc_dev.dv_xname, __LINE__);
   1944 		return;
   1945 	}
   1946 }
   1947 
   1948 int
   1949 fxp_enable(struct fxp_softc *sc)
   1950 {
   1951 
   1952 	if (sc->sc_enabled == 0 && sc->sc_enable != NULL) {
   1953 		if ((*sc->sc_enable)(sc) != 0) {
   1954 			printf("%s: device enable failed\n",
   1955 			    sc->sc_dev.dv_xname);
   1956 			return (EIO);
   1957 		}
   1958 	}
   1959 
   1960 	sc->sc_enabled = 1;
   1961 	return (0);
   1962 }
   1963 
   1964 void
   1965 fxp_disable(struct fxp_softc *sc)
   1966 {
   1967 
   1968 	if (sc->sc_enabled != 0 && sc->sc_disable != NULL) {
   1969 		(*sc->sc_disable)(sc);
   1970 		sc->sc_enabled = 0;
   1971 	}
   1972 }
   1973 
   1974 /*
   1975  * fxp_activate:
   1976  *
   1977  *	Handle device activation/deactivation requests.
   1978  */
   1979 int
   1980 fxp_activate(struct device *self, enum devact act)
   1981 {
   1982 	struct fxp_softc *sc = (void *) self;
   1983 	int s, error = 0;
   1984 
   1985 	s = splnet();
   1986 	switch (act) {
   1987 	case DVACT_ACTIVATE:
   1988 		error = EOPNOTSUPP;
   1989 		break;
   1990 
   1991 	case DVACT_DEACTIVATE:
   1992 		if (sc->sc_flags & FXPF_MII)
   1993 			mii_activate(&sc->sc_mii, act, MII_PHY_ANY,
   1994 			    MII_OFFSET_ANY);
   1995 		if_deactivate(&sc->sc_ethercom.ec_if);
   1996 		break;
   1997 	}
   1998 	splx(s);
   1999 
   2000 	return (error);
   2001 }
   2002 
   2003 /*
   2004  * fxp_detach:
   2005  *
   2006  *	Detach an i82557 interface.
   2007  */
   2008 int
   2009 fxp_detach(struct fxp_softc *sc)
   2010 {
   2011 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2012 	int i;
   2013 
   2014 	/* Succeed now if there's no work to do. */
   2015 	if ((sc->sc_flags & FXPF_ATTACHED) == 0)
   2016 		return (0);
   2017 
   2018 	/* Unhook our tick handler. */
   2019 	callout_stop(&sc->sc_callout);
   2020 
   2021 	if (sc->sc_flags & FXPF_MII) {
   2022 		/* Detach all PHYs */
   2023 		mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
   2024 	}
   2025 
   2026 	/* Delete all remaining media. */
   2027 	ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
   2028 
   2029 #if NRND > 0
   2030 	rnd_detach_source(&sc->rnd_source);
   2031 #endif
   2032 	ether_ifdetach(ifp);
   2033 	if_detach(ifp);
   2034 
   2035 	for (i = 0; i < FXP_NRFABUFS; i++) {
   2036 		bus_dmamap_unload(sc->sc_dmat, sc->sc_rxmaps[i]);
   2037 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_rxmaps[i]);
   2038 	}
   2039 
   2040 	for (i = 0; i < FXP_NTXCB; i++) {
   2041 		bus_dmamap_unload(sc->sc_dmat, FXP_DSTX(sc, i)->txs_dmamap);
   2042 		bus_dmamap_destroy(sc->sc_dmat, FXP_DSTX(sc, i)->txs_dmamap);
   2043 	}
   2044 
   2045 	bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap);
   2046 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap);
   2047 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
   2048 	    sizeof(struct fxp_control_data));
   2049 	bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg);
   2050 
   2051 	shutdownhook_disestablish(sc->sc_sdhook);
   2052 	powerhook_disestablish(sc->sc_powerhook);
   2053 
   2054 	return (0);
   2055 }
   2056