Home | History | Annotate | Line # | Download | only in ic
i82557.c revision 1.125
      1 /*	$NetBSD: i82557.c,v 1.125 2009/03/07 15:03:25 tsutsui Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 1999, 2001, 2002 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  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1995, David Greenman
     35  * Copyright (c) 2001 Jonathan Lemon <jlemon (at) freebsd.org>
     36  * All rights reserved.
     37  *
     38  * Redistribution and use in source and binary forms, with or without
     39  * modification, are permitted provided that the following conditions
     40  * are met:
     41  * 1. Redistributions of source code must retain the above copyright
     42  *    notice unmodified, this list of conditions, and the following
     43  *    disclaimer.
     44  * 2. Redistributions in binary form must reproduce the above copyright
     45  *    notice, this list of conditions and the following disclaimer in the
     46  *    documentation and/or other materials provided with the distribution.
     47  *
     48  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  *
     60  *	Id: if_fxp.c,v 1.113 2001/05/17 23:50:24 jlemon
     61  */
     62 
     63 /*
     64  * Device driver for the Intel i82557 fast Ethernet controller,
     65  * and its successors, the i82558 and i82559.
     66  */
     67 
     68 #include <sys/cdefs.h>
     69 __KERNEL_RCSID(0, "$NetBSD: i82557.c,v 1.125 2009/03/07 15:03:25 tsutsui Exp $");
     70 
     71 #include "bpfilter.h"
     72 #include "rnd.h"
     73 
     74 #include <sys/param.h>
     75 #include <sys/systm.h>
     76 #include <sys/callout.h>
     77 #include <sys/mbuf.h>
     78 #include <sys/malloc.h>
     79 #include <sys/kernel.h>
     80 #include <sys/socket.h>
     81 #include <sys/ioctl.h>
     82 #include <sys/errno.h>
     83 #include <sys/device.h>
     84 #include <sys/syslog.h>
     85 
     86 #include <machine/endian.h>
     87 
     88 #include <uvm/uvm_extern.h>
     89 
     90 #if NRND > 0
     91 #include <sys/rnd.h>
     92 #endif
     93 
     94 #include <net/if.h>
     95 #include <net/if_dl.h>
     96 #include <net/if_media.h>
     97 #include <net/if_ether.h>
     98 
     99 #include <netinet/in.h>
    100 #include <netinet/in_systm.h>
    101 #include <netinet/ip.h>
    102 #include <netinet/tcp.h>
    103 #include <netinet/udp.h>
    104 
    105 #if NBPFILTER > 0
    106 #include <net/bpf.h>
    107 #endif
    108 
    109 #include <sys/bus.h>
    110 #include <sys/intr.h>
    111 
    112 #include <dev/mii/miivar.h>
    113 
    114 #include <dev/ic/i82557reg.h>
    115 #include <dev/ic/i82557var.h>
    116 
    117 #include <dev/microcode/i8255x/rcvbundl.h>
    118 
    119 /*
    120  * NOTE!  On the Alpha, we have an alignment constraint.  The
    121  * card DMAs the packet immediately following the RFA.  However,
    122  * the first thing in the packet is a 14-byte Ethernet header.
    123  * This means that the packet is misaligned.  To compensate,
    124  * we actually offset the RFA 2 bytes into the cluster.  This
    125  * alignes the packet after the Ethernet header at a 32-bit
    126  * boundary.  HOWEVER!  This means that the RFA is misaligned!
    127  */
    128 #define	RFA_ALIGNMENT_FUDGE	2
    129 
    130 /*
    131  * The configuration byte map has several undefined fields which
    132  * must be one or must be zero.  Set up a template for these bits
    133  * only (assuming an i82557 chip), leaving the actual configuration
    134  * for fxp_init().
    135  *
    136  * See the definition of struct fxp_cb_config for the bit definitions.
    137  */
    138 const u_int8_t fxp_cb_config_template[] = {
    139 	0x0, 0x0,		/* cb_status */
    140 	0x0, 0x0,		/* cb_command */
    141 	0x0, 0x0, 0x0, 0x0,	/* link_addr */
    142 	0x0,	/*  0 */
    143 	0x0,	/*  1 */
    144 	0x0,	/*  2 */
    145 	0x0,	/*  3 */
    146 	0x0,	/*  4 */
    147 	0x0,	/*  5 */
    148 	0x32,	/*  6 */
    149 	0x0,	/*  7 */
    150 	0x0,	/*  8 */
    151 	0x0,	/*  9 */
    152 	0x6,	/* 10 */
    153 	0x0,	/* 11 */
    154 	0x0,	/* 12 */
    155 	0x0,	/* 13 */
    156 	0xf2,	/* 14 */
    157 	0x48,	/* 15 */
    158 	0x0,	/* 16 */
    159 	0x40,	/* 17 */
    160 	0xf0,	/* 18 */
    161 	0x0,	/* 19 */
    162 	0x3f,	/* 20 */
    163 	0x5,	/* 21 */
    164 	0x0,	/* 22 */
    165 	0x0,	/* 23 */
    166 	0x0,	/* 24 */
    167 	0x0,	/* 25 */
    168 	0x0,	/* 26 */
    169 	0x0,	/* 27 */
    170 	0x0,	/* 28 */
    171 	0x0,	/* 29 */
    172 	0x0,	/* 30 */
    173 	0x0,	/* 31 */
    174 };
    175 
    176 void	fxp_mii_initmedia(struct fxp_softc *);
    177 void	fxp_mii_mediastatus(struct ifnet *, struct ifmediareq *);
    178 
    179 void	fxp_80c24_initmedia(struct fxp_softc *);
    180 int	fxp_80c24_mediachange(struct ifnet *);
    181 void	fxp_80c24_mediastatus(struct ifnet *, struct ifmediareq *);
    182 
    183 void	fxp_start(struct ifnet *);
    184 int	fxp_ioctl(struct ifnet *, u_long, void *);
    185 void	fxp_watchdog(struct ifnet *);
    186 int	fxp_init(struct ifnet *);
    187 void	fxp_stop(struct ifnet *, int);
    188 
    189 void	fxp_txintr(struct fxp_softc *);
    190 int	fxp_rxintr(struct fxp_softc *);
    191 
    192 void	fxp_rx_hwcksum(struct fxp_softc *,struct mbuf *,
    193 	    const struct fxp_rfa *, u_int);
    194 
    195 void	fxp_rxdrain(struct fxp_softc *);
    196 int	fxp_add_rfabuf(struct fxp_softc *, bus_dmamap_t, int);
    197 int	fxp_mdi_read(device_t, int, int);
    198 void	fxp_statchg(device_t);
    199 void	fxp_mdi_write(device_t, int, int, int);
    200 void	fxp_autosize_eeprom(struct fxp_softc*);
    201 void	fxp_read_eeprom(struct fxp_softc *, u_int16_t *, int, int);
    202 void	fxp_write_eeprom(struct fxp_softc *, u_int16_t *, int, int);
    203 void	fxp_eeprom_update_cksum(struct fxp_softc *);
    204 void	fxp_get_info(struct fxp_softc *, u_int8_t *);
    205 void	fxp_tick(void *);
    206 void	fxp_mc_setup(struct fxp_softc *);
    207 void	fxp_load_ucode(struct fxp_softc *);
    208 
    209 int	fxp_copy_small = 0;
    210 
    211 /*
    212  * Variables for interrupt mitigating microcode.
    213  */
    214 int	fxp_int_delay = 1000;		/* usec */
    215 int	fxp_bundle_max = 6;		/* packets */
    216 
    217 struct fxp_phytype {
    218 	int	fp_phy;		/* type of PHY, -1 for MII at the end. */
    219 	void	(*fp_init)(struct fxp_softc *);
    220 } fxp_phytype_table[] = {
    221 	{ FXP_PHY_80C24,		fxp_80c24_initmedia },
    222 	{ -1,				fxp_mii_initmedia },
    223 };
    224 
    225 /*
    226  * Set initial transmit threshold at 64 (512 bytes). This is
    227  * increased by 64 (512 bytes) at a time, to maximum of 192
    228  * (1536 bytes), if an underrun occurs.
    229  */
    230 static int tx_threshold = 64;
    231 
    232 /*
    233  * Wait for the previous command to be accepted (but not necessarily
    234  * completed).
    235  */
    236 static inline void
    237 fxp_scb_wait(struct fxp_softc *sc)
    238 {
    239 	int i = 10000;
    240 
    241 	while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i)
    242 		delay(2);
    243 	if (i == 0)
    244 		log(LOG_WARNING,
    245 		    "%s: WARNING: SCB timed out!\n", device_xname(sc->sc_dev));
    246 }
    247 
    248 /*
    249  * Submit a command to the i82557.
    250  */
    251 static inline void
    252 fxp_scb_cmd(struct fxp_softc *sc, u_int8_t cmd)
    253 {
    254 
    255 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, cmd);
    256 }
    257 
    258 /*
    259  * Finish attaching an i82557 interface.  Called by bus-specific front-end.
    260  */
    261 void
    262 fxp_attach(struct fxp_softc *sc)
    263 {
    264 	u_int8_t enaddr[ETHER_ADDR_LEN];
    265 	struct ifnet *ifp;
    266 	bus_dma_segment_t seg;
    267 	int rseg, i, error;
    268 	struct fxp_phytype *fp;
    269 
    270 	callout_init(&sc->sc_callout, 0);
    271 
    272         /*
    273 	 * Enable use of extended RFDs and TCBs for 82550
    274 	 * and later chips. Note: we need extended TXCB support
    275 	 * too, but that's already enabled by the code above.
    276 	 * Be careful to do this only on the right devices.
    277 	 */
    278 	if (sc->sc_flags & FXPF_EXT_RFA)
    279 		sc->sc_txcmd = htole16(FXP_CB_COMMAND_IPCBXMIT);
    280 	else
    281 		sc->sc_txcmd = htole16(FXP_CB_COMMAND_XMIT);
    282 
    283 	sc->sc_rfa_size =
    284 	    (sc->sc_flags & FXPF_EXT_RFA) ? RFA_EXT_SIZE : RFA_SIZE;
    285 
    286 	/*
    287 	 * Allocate the control data structures, and create and load the
    288 	 * DMA map for it.
    289 	 */
    290 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    291 	    sizeof(struct fxp_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
    292 	    0)) != 0) {
    293 		aprint_error_dev(sc->sc_dev,
    294 		    "unable to allocate control data, error = %d\n",
    295 		    error);
    296 		goto fail_0;
    297 	}
    298 
    299 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    300 	    sizeof(struct fxp_control_data), (void **)&sc->sc_control_data,
    301 	    BUS_DMA_COHERENT)) != 0) {
    302 		aprint_error_dev(sc->sc_dev,
    303 		    "unable to map control data, error = %d\n", error);
    304 		goto fail_1;
    305 	}
    306 	sc->sc_cdseg = seg;
    307 	sc->sc_cdnseg = rseg;
    308 
    309 	memset(sc->sc_control_data, 0, sizeof(struct fxp_control_data));
    310 
    311 	if ((error = bus_dmamap_create(sc->sc_dmat,
    312 	    sizeof(struct fxp_control_data), 1,
    313 	    sizeof(struct fxp_control_data), 0, 0, &sc->sc_dmamap)) != 0) {
    314 		aprint_error_dev(sc->sc_dev,
    315 		    "unable to create control data DMA map, error = %d\n",
    316 		    error);
    317 		goto fail_2;
    318 	}
    319 
    320 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap,
    321 	    sc->sc_control_data, sizeof(struct fxp_control_data), NULL,
    322 	    0)) != 0) {
    323 		aprint_error_dev(sc->sc_dev,
    324 		    "can't load control data DMA map, error = %d\n",
    325 		    error);
    326 		goto fail_3;
    327 	}
    328 
    329 	/*
    330 	 * Create the transmit buffer DMA maps.
    331 	 */
    332 	for (i = 0; i < FXP_NTXCB; i++) {
    333 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    334 		    (sc->sc_flags & FXPF_EXT_RFA) ?
    335 		    FXP_IPCB_NTXSEG : FXP_NTXSEG,
    336 		    MCLBYTES, 0, 0, &FXP_DSTX(sc, i)->txs_dmamap)) != 0) {
    337 			aprint_error_dev(sc->sc_dev,
    338 			    "unable to create tx DMA map %d, error = %d\n",
    339 			    i, error);
    340 			goto fail_4;
    341 		}
    342 	}
    343 
    344 	/*
    345 	 * Create the receive buffer DMA maps.
    346 	 */
    347 	for (i = 0; i < FXP_NRFABUFS; i++) {
    348 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
    349 		    MCLBYTES, 0, 0, &sc->sc_rxmaps[i])) != 0) {
    350 			aprint_error_dev(sc->sc_dev,
    351 			    "unable to create rx DMA map %d, error = %d\n",
    352 			    i, error);
    353 			goto fail_5;
    354 		}
    355 	}
    356 
    357 	/* Initialize MAC address and media structures. */
    358 	fxp_get_info(sc, enaddr);
    359 
    360 	aprint_normal_dev(sc->sc_dev, "Ethernet address %s\n",
    361 	    ether_sprintf(enaddr));
    362 
    363 	ifp = &sc->sc_ethercom.ec_if;
    364 
    365 	/*
    366 	 * Get info about our media interface, and initialize it.  Note
    367 	 * the table terminates itself with a phy of -1, indicating
    368 	 * that we're using MII.
    369 	 */
    370 	for (fp = fxp_phytype_table; fp->fp_phy != -1; fp++)
    371 		if (fp->fp_phy == sc->phy_primary_device)
    372 			break;
    373 	(*fp->fp_init)(sc);
    374 
    375 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
    376 	ifp->if_softc = sc;
    377 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    378 	ifp->if_ioctl = fxp_ioctl;
    379 	ifp->if_start = fxp_start;
    380 	ifp->if_watchdog = fxp_watchdog;
    381 	ifp->if_init = fxp_init;
    382 	ifp->if_stop = fxp_stop;
    383 	IFQ_SET_READY(&ifp->if_snd);
    384 
    385 	if (sc->sc_flags & FXPF_EXT_RFA) {
    386 		/*
    387 		 * IFCAP_CSUM_IPv4_Tx seems to have a problem,
    388 		 * at least, on i82550 rev.12.
    389 		 * specifically, it doesn't set ipv4 checksum properly
    390 		 * when sending UDP (and probably TCP) packets with
    391 		 * 20 byte ipv4 header + 1 or 2 byte data,
    392 		 * though ICMP packets seem working.
    393 		 * FreeBSD driver has related comments.
    394 		 * We've added a workaround to handle the bug by padding
    395 		 * such packets manually.
    396 		 */
    397 		ifp->if_capabilities =
    398 		    IFCAP_CSUM_IPv4_Tx  | IFCAP_CSUM_IPv4_Rx  |
    399 		    IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
    400 		    IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;
    401 		sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_HWTAGGING;
    402 	} else if (sc->sc_flags & FXPF_82559_RXCSUM) {
    403 		ifp->if_capabilities =
    404 		    IFCAP_CSUM_TCPv4_Rx |
    405 		    IFCAP_CSUM_UDPv4_Rx;
    406 	}
    407 
    408 	/*
    409 	 * We can support 802.1Q VLAN-sized frames.
    410 	 */
    411 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    412 
    413 	/*
    414 	 * Attach the interface.
    415 	 */
    416 	if_attach(ifp);
    417 	ether_ifattach(ifp, enaddr);
    418 #if NRND > 0
    419 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
    420 	    RND_TYPE_NET, 0);
    421 #endif
    422 
    423 #ifdef FXP_EVENT_COUNTERS
    424 	evcnt_attach_dynamic(&sc->sc_ev_txstall, EVCNT_TYPE_MISC,
    425 	    NULL, device_xname(sc->sc_dev), "txstall");
    426 	evcnt_attach_dynamic(&sc->sc_ev_txintr, EVCNT_TYPE_INTR,
    427 	    NULL, device_xname(sc->sc_dev), "txintr");
    428 	evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_INTR,
    429 	    NULL, device_xname(sc->sc_dev), "rxintr");
    430 	if (sc->sc_flags & FXPF_FC) {
    431 		evcnt_attach_dynamic(&sc->sc_ev_txpause, EVCNT_TYPE_MISC,
    432 		    NULL, device_xname(sc->sc_dev), "txpause");
    433 		evcnt_attach_dynamic(&sc->sc_ev_rxpause, EVCNT_TYPE_MISC,
    434 		    NULL, device_xname(sc->sc_dev), "rxpause");
    435 	}
    436 #endif /* FXP_EVENT_COUNTERS */
    437 
    438 	/* The attach is successful. */
    439 	sc->sc_flags |= FXPF_ATTACHED;
    440 
    441 	return;
    442 
    443 	/*
    444 	 * Free any resources we've allocated during the failed attach
    445 	 * attempt.  Do this in reverse order and fall though.
    446 	 */
    447  fail_5:
    448 	for (i = 0; i < FXP_NRFABUFS; i++) {
    449 		if (sc->sc_rxmaps[i] != NULL)
    450 			bus_dmamap_destroy(sc->sc_dmat, sc->sc_rxmaps[i]);
    451 	}
    452  fail_4:
    453 	for (i = 0; i < FXP_NTXCB; i++) {
    454 		if (FXP_DSTX(sc, i)->txs_dmamap != NULL)
    455 			bus_dmamap_destroy(sc->sc_dmat,
    456 			    FXP_DSTX(sc, i)->txs_dmamap);
    457 	}
    458 	bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap);
    459  fail_3:
    460 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap);
    461  fail_2:
    462 	bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data,
    463 	    sizeof(struct fxp_control_data));
    464  fail_1:
    465 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    466  fail_0:
    467 	return;
    468 }
    469 
    470 void
    471 fxp_mii_initmedia(struct fxp_softc *sc)
    472 {
    473 	int flags;
    474 
    475 	sc->sc_flags |= FXPF_MII;
    476 
    477 	sc->sc_mii.mii_ifp = &sc->sc_ethercom.ec_if;
    478 	sc->sc_mii.mii_readreg = fxp_mdi_read;
    479 	sc->sc_mii.mii_writereg = fxp_mdi_write;
    480 	sc->sc_mii.mii_statchg = fxp_statchg;
    481 
    482 	sc->sc_ethercom.ec_mii = &sc->sc_mii;
    483 	ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, ether_mediachange,
    484 	    fxp_mii_mediastatus);
    485 
    486 	flags = MIIF_NOISOLATE;
    487 	if (sc->sc_flags & FXPF_FC)
    488 		flags |= MIIF_FORCEANEG|MIIF_DOPAUSE;
    489 	/*
    490 	 * The i82557 wedges if all of its PHYs are isolated!
    491 	 */
    492 	mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
    493 	    MII_OFFSET_ANY, flags);
    494 	if (LIST_EMPTY(&sc->sc_mii.mii_phys)) {
    495 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
    496 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
    497 	} else
    498 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
    499 }
    500 
    501 void
    502 fxp_80c24_initmedia(struct fxp_softc *sc)
    503 {
    504 
    505 	/*
    506 	 * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
    507 	 * doesn't have a programming interface of any sort.  The
    508 	 * media is sensed automatically based on how the link partner
    509 	 * is configured.  This is, in essence, manual configuration.
    510 	 */
    511 	aprint_normal_dev(sc->sc_dev,
    512 	    "Seeq 80c24 AutoDUPLEX media interface present\n");
    513 	ifmedia_init(&sc->sc_mii.mii_media, 0, fxp_80c24_mediachange,
    514 	    fxp_80c24_mediastatus);
    515 	ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
    516 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_MANUAL);
    517 }
    518 
    519 /*
    520  * Initialize the interface media.
    521  */
    522 void
    523 fxp_get_info(struct fxp_softc *sc, u_int8_t *enaddr)
    524 {
    525 	u_int16_t data, myea[ETHER_ADDR_LEN / 2];
    526 
    527 	/*
    528 	 * Reset to a stable state.
    529 	 */
    530 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
    531 	DELAY(100);
    532 
    533 	sc->sc_eeprom_size = 0;
    534 	fxp_autosize_eeprom(sc);
    535 	if (sc->sc_eeprom_size == 0) {
    536 		aprint_error_dev(sc->sc_dev, "failed to detect EEPROM size\n");
    537 		sc->sc_eeprom_size = 6; /* XXX panic here? */
    538 	}
    539 #ifdef DEBUG
    540 	aprint_debug_dev(sc->sc_dev, "detected %d word EEPROM\n",
    541 	    1 << sc->sc_eeprom_size);
    542 #endif
    543 
    544 	/*
    545 	 * Get info about the primary PHY
    546 	 */
    547 	fxp_read_eeprom(sc, &data, 6, 1);
    548 	sc->phy_primary_device =
    549 	    (data & FXP_PHY_DEVICE_MASK) >> FXP_PHY_DEVICE_SHIFT;
    550 
    551 	/*
    552 	 * Read MAC address.
    553 	 */
    554 	fxp_read_eeprom(sc, myea, 0, 3);
    555 	enaddr[0] = myea[0] & 0xff;
    556 	enaddr[1] = myea[0] >> 8;
    557 	enaddr[2] = myea[1] & 0xff;
    558 	enaddr[3] = myea[1] >> 8;
    559 	enaddr[4] = myea[2] & 0xff;
    560 	enaddr[5] = myea[2] >> 8;
    561 
    562 	/*
    563 	 * Systems based on the ICH2/ICH2-M chip from Intel, as well
    564 	 * as some i82559 designs, have a defect where the chip can
    565 	 * cause a PCI protocol violation if it receives a CU_RESUME
    566 	 * command when it is entering the IDLE state.
    567 	 *
    568 	 * The work-around is to disable Dynamic Standby Mode, so that
    569 	 * the chip never deasserts #CLKRUN, and always remains in the
    570 	 * active state.
    571 	 *
    572 	 * Unfortunately, the only way to disable Dynamic Standby is
    573 	 * to frob an EEPROM setting and reboot (the EEPROM setting
    574 	 * is only consulted when the PCI bus comes out of reset).
    575 	 *
    576 	 * See Intel 82801BA/82801BAM Specification Update, Errata #30.
    577 	 */
    578 	if (sc->sc_flags & FXPF_HAS_RESUME_BUG) {
    579 		fxp_read_eeprom(sc, &data, 10, 1);
    580 		if (data & 0x02) {		/* STB enable */
    581 			aprint_error_dev(sc->sc_dev, "WARNING: "
    582 			    "Disabling dynamic standby mode in EEPROM "
    583 			    "to work around a\n");
    584 			aprint_normal_dev(sc->sc_dev,
    585 			    "WARNING: hardware bug.  You must reset "
    586 			    "the system before using this\n");
    587 			aprint_normal_dev(sc->sc_dev, "WARNING: interface.\n");
    588 			data &= ~0x02;
    589 			fxp_write_eeprom(sc, &data, 10, 1);
    590 			aprint_normal_dev(sc->sc_dev, "new EEPROM ID: 0x%04x\n",
    591 			    data);
    592 			fxp_eeprom_update_cksum(sc);
    593 		}
    594 	}
    595 
    596 	/* Receiver lock-up workaround detection. (FXPF_RECV_WORKAROUND) */
    597 	/* Due to false positives we make it conditional on setting link1 */
    598 	fxp_read_eeprom(sc, &data, 3, 1);
    599 	if ((data & 0x03) != 0x03) {
    600 		aprint_verbose_dev(sc->sc_dev,
    601 		    "May need receiver lock-up workaround\n");
    602 	}
    603 }
    604 
    605 static void
    606 fxp_eeprom_shiftin(struct fxp_softc *sc, int data, int len)
    607 {
    608 	uint16_t reg;
    609 	int x;
    610 
    611 	for (x = 1 << (len - 1); x != 0; x >>= 1) {
    612 		DELAY(40);
    613 		if (data & x)
    614 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
    615 		else
    616 			reg = FXP_EEPROM_EECS;
    617 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
    618 		DELAY(40);
    619 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
    620 		    reg | FXP_EEPROM_EESK);
    621 		DELAY(40);
    622 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
    623 	}
    624 	DELAY(40);
    625 }
    626 
    627 /*
    628  * Figure out EEPROM size.
    629  *
    630  * 559's can have either 64-word or 256-word EEPROMs, the 558
    631  * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
    632  * talks about the existence of 16 to 256 word EEPROMs.
    633  *
    634  * The only known sizes are 64 and 256, where the 256 version is used
    635  * by CardBus cards to store CIS information.
    636  *
    637  * The address is shifted in msb-to-lsb, and after the last
    638  * address-bit the EEPROM is supposed to output a `dummy zero' bit,
    639  * after which follows the actual data. We try to detect this zero, by
    640  * probing the data-out bit in the EEPROM control register just after
    641  * having shifted in a bit. If the bit is zero, we assume we've
    642  * shifted enough address bits. The data-out should be tri-state,
    643  * before this, which should translate to a logical one.
    644  *
    645  * Other ways to do this would be to try to read a register with known
    646  * contents with a varying number of address bits, but no such
    647  * register seem to be available. The high bits of register 10 are 01
    648  * on the 558 and 559, but apparently not on the 557.
    649  *
    650  * The Linux driver computes a checksum on the EEPROM data, but the
    651  * value of this checksum is not very well documented.
    652  */
    653 
    654 void
    655 fxp_autosize_eeprom(struct fxp_softc *sc)
    656 {
    657 	int x;
    658 
    659 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
    660 	DELAY(40);
    661 
    662 	/* Shift in read opcode. */
    663 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3);
    664 
    665 	/*
    666 	 * Shift in address, wait for the dummy zero following a correct
    667 	 * address shift.
    668 	 */
    669 	for (x = 1; x <= 8; x++) {
    670 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
    671 		DELAY(40);
    672 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
    673 		    FXP_EEPROM_EECS | FXP_EEPROM_EESK);
    674 		DELAY(40);
    675 		if ((CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) &
    676 		    FXP_EEPROM_EEDO) == 0)
    677 			break;
    678 		DELAY(40);
    679 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
    680 		DELAY(40);
    681 	}
    682 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
    683 	DELAY(40);
    684 	if (x != 6 && x != 8) {
    685 #ifdef DEBUG
    686 		printf("%s: strange EEPROM size (%d)\n",
    687 		    device_xname(sc->sc_dev), 1 << x);
    688 #endif
    689 	} else
    690 		sc->sc_eeprom_size = x;
    691 }
    692 
    693 /*
    694  * Read from the serial EEPROM. Basically, you manually shift in
    695  * the read opcode (one bit at a time) and then shift in the address,
    696  * and then you shift out the data (all of this one bit at a time).
    697  * The word size is 16 bits, so you have to provide the address for
    698  * every 16 bits of data.
    699  */
    700 void
    701 fxp_read_eeprom(struct fxp_softc *sc, u_int16_t *data, int offset, int words)
    702 {
    703 	u_int16_t reg;
    704 	int i, x;
    705 
    706 	for (i = 0; i < words; i++) {
    707 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
    708 
    709 		/* Shift in read opcode. */
    710 		fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3);
    711 
    712 		/* Shift in address. */
    713 		fxp_eeprom_shiftin(sc, i + offset, sc->sc_eeprom_size);
    714 
    715 		reg = FXP_EEPROM_EECS;
    716 		data[i] = 0;
    717 
    718 		/* Shift out data. */
    719 		for (x = 16; x > 0; x--) {
    720 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
    721 			    reg | FXP_EEPROM_EESK);
    722 			DELAY(40);
    723 			if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) &
    724 			    FXP_EEPROM_EEDO)
    725 				data[i] |= (1 << (x - 1));
    726 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
    727 			DELAY(40);
    728 		}
    729 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
    730 		DELAY(40);
    731 	}
    732 }
    733 
    734 /*
    735  * Write data to the serial EEPROM.
    736  */
    737 void
    738 fxp_write_eeprom(struct fxp_softc *sc, u_int16_t *data, int offset, int words)
    739 {
    740 	int i, j;
    741 
    742 	for (i = 0; i < words; i++) {
    743 		/* Erase/write enable. */
    744 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
    745 		fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_ERASE, 3);
    746 		fxp_eeprom_shiftin(sc, 0x3 << (sc->sc_eeprom_size - 2),
    747 		    sc->sc_eeprom_size);
    748 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
    749 		DELAY(4);
    750 
    751 		/* Shift in write opcode, address, data. */
    752 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
    753 		fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_WRITE, 3);
    754 		fxp_eeprom_shiftin(sc, i + offset, sc->sc_eeprom_size);
    755 		fxp_eeprom_shiftin(sc, data[i], 16);
    756 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
    757 		DELAY(4);
    758 
    759 		/* Wait for the EEPROM to finish up. */
    760 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
    761 		DELAY(4);
    762 		for (j = 0; j < 1000; j++) {
    763 			if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) &
    764 			    FXP_EEPROM_EEDO)
    765 				break;
    766 			DELAY(50);
    767 		}
    768 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
    769 		DELAY(4);
    770 
    771 		/* Erase/write disable. */
    772 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
    773 		fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_ERASE, 3);
    774 		fxp_eeprom_shiftin(sc, 0, sc->sc_eeprom_size);
    775 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
    776 		DELAY(4);
    777 	}
    778 }
    779 
    780 /*
    781  * Update the checksum of the EEPROM.
    782  */
    783 void
    784 fxp_eeprom_update_cksum(struct fxp_softc *sc)
    785 {
    786 	int i;
    787 	uint16_t data, cksum;
    788 
    789 	cksum = 0;
    790 	for (i = 0; i < (1 << sc->sc_eeprom_size) - 1; i++) {
    791 		fxp_read_eeprom(sc, &data, i, 1);
    792 		cksum += data;
    793 	}
    794 	i = (1 << sc->sc_eeprom_size) - 1;
    795 	cksum = 0xbaba - cksum;
    796 	fxp_read_eeprom(sc, &data, i, 1);
    797 	fxp_write_eeprom(sc, &cksum, i, 1);
    798 	log(LOG_INFO, "%s: EEPROM checksum @ 0x%x: 0x%04x -> 0x%04x\n",
    799 	    device_xname(sc->sc_dev), i, data, cksum);
    800 }
    801 
    802 /*
    803  * Start packet transmission on the interface.
    804  */
    805 void
    806 fxp_start(struct ifnet *ifp)
    807 {
    808 	struct fxp_softc *sc = ifp->if_softc;
    809 	struct mbuf *m0, *m;
    810 	struct fxp_txdesc *txd;
    811 	struct fxp_txsoft *txs;
    812 	bus_dmamap_t dmamap;
    813 	int error, lasttx, nexttx, opending, seg, nsegs, len;
    814 
    815 	/*
    816 	 * If we want a re-init, bail out now.
    817 	 */
    818 	if (sc->sc_flags & FXPF_WANTINIT) {
    819 		ifp->if_flags |= IFF_OACTIVE;
    820 		return;
    821 	}
    822 
    823 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    824 		return;
    825 
    826 	/*
    827 	 * Remember the previous txpending and the current lasttx.
    828 	 */
    829 	opending = sc->sc_txpending;
    830 	lasttx = sc->sc_txlast;
    831 
    832 	/*
    833 	 * Loop through the send queue, setting up transmit descriptors
    834 	 * until we drain the queue, or use up all available transmit
    835 	 * descriptors.
    836 	 */
    837 	for (;;) {
    838 		struct fxp_tbd *tbdp;
    839 		int csum_flags;
    840 
    841 		/*
    842 		 * Grab a packet off the queue.
    843 		 */
    844 		IFQ_POLL(&ifp->if_snd, m0);
    845 		if (m0 == NULL)
    846 			break;
    847 		m = NULL;
    848 
    849 		if (sc->sc_txpending == FXP_NTXCB - 1) {
    850 			FXP_EVCNT_INCR(&sc->sc_ev_txstall);
    851 			break;
    852 		}
    853 
    854 		/*
    855 		 * Get the next available transmit descriptor.
    856 		 */
    857 		nexttx = FXP_NEXTTX(sc->sc_txlast);
    858 		txd = FXP_CDTX(sc, nexttx);
    859 		txs = FXP_DSTX(sc, nexttx);
    860 		dmamap = txs->txs_dmamap;
    861 
    862 		/*
    863 		 * Load the DMA map.  If this fails, the packet either
    864 		 * didn't fit in the allotted number of frags, or we were
    865 		 * short on resources.  In this case, we'll copy and try
    866 		 * again.
    867 		 */
    868 		if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
    869 		    BUS_DMA_WRITE|BUS_DMA_NOWAIT) != 0) {
    870 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    871 			if (m == NULL) {
    872 				log(LOG_ERR, "%s: unable to allocate Tx mbuf\n",
    873 				    device_xname(sc->sc_dev));
    874 				break;
    875 			}
    876 			MCLAIM(m, &sc->sc_ethercom.ec_tx_mowner);
    877 			if (m0->m_pkthdr.len > MHLEN) {
    878 				MCLGET(m, M_DONTWAIT);
    879 				if ((m->m_flags & M_EXT) == 0) {
    880 					log(LOG_ERR, "%s: unable to allocate "
    881 					    "Tx cluster\n",
    882 					    device_xname(sc->sc_dev));
    883 					m_freem(m);
    884 					break;
    885 				}
    886 			}
    887 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, void *));
    888 			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
    889 			error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
    890 			    m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
    891 			if (error) {
    892 				log(LOG_ERR, "%s: unable to load Tx buffer, "
    893 				    "error = %d\n",
    894 				    device_xname(sc->sc_dev), error);
    895 				break;
    896 			}
    897 		}
    898 
    899 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    900 		csum_flags = m0->m_pkthdr.csum_flags;
    901 		if (m != NULL) {
    902 			m_freem(m0);
    903 			m0 = m;
    904 		}
    905 
    906 		/* Initialize the fraglist. */
    907 		tbdp = txd->txd_tbd;
    908 		len = m0->m_pkthdr.len;
    909 		nsegs = dmamap->dm_nsegs;
    910 		if (sc->sc_flags & FXPF_EXT_RFA)
    911 			tbdp++;
    912 		for (seg = 0; seg < nsegs; seg++) {
    913 			tbdp[seg].tb_addr =
    914 			    htole32(dmamap->dm_segs[seg].ds_addr);
    915 			tbdp[seg].tb_size =
    916 			    htole32(dmamap->dm_segs[seg].ds_len);
    917 		}
    918 		if (__predict_false(len <= FXP_IP4CSUMTX_PADLEN &&
    919 		    (csum_flags & M_CSUM_IPv4) != 0)) {
    920 			/*
    921 			 * Pad short packets to avoid ip4csum-tx bug.
    922 			 *
    923 			 * XXX Should we still consider if such short
    924 			 *     (36 bytes or less) packets might already
    925 			 *     occupy FXP_IPCB_NTXSEG (15) fragments here?
    926 			 */
    927 			KASSERT(nsegs < FXP_IPCB_NTXSEG);
    928 			nsegs++;
    929 			tbdp[seg].tb_addr = htole32(FXP_CDTXPADADDR(sc));
    930 			tbdp[seg].tb_size =
    931 			    htole32(FXP_IP4CSUMTX_PADLEN + 1 - len);
    932 		}
    933 
    934 		/* Sync the DMA map. */
    935 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
    936 		    BUS_DMASYNC_PREWRITE);
    937 
    938 		/*
    939 		 * Store a pointer to the packet so we can free it later.
    940 		 */
    941 		txs->txs_mbuf = m0;
    942 
    943 		/*
    944 		 * Initialize the transmit descriptor.
    945 		 */
    946 		/* BIG_ENDIAN: no need to swap to store 0 */
    947 		txd->txd_txcb.cb_status = 0;
    948 		txd->txd_txcb.cb_command =
    949 		    sc->sc_txcmd | htole16(FXP_CB_COMMAND_SF);
    950 		txd->txd_txcb.tx_threshold = tx_threshold;
    951 		txd->txd_txcb.tbd_number = nsegs;
    952 
    953 		KASSERT((csum_flags & (M_CSUM_TCPv6 | M_CSUM_UDPv6)) == 0);
    954 		if (sc->sc_flags & FXPF_EXT_RFA) {
    955 			struct m_tag *vtag;
    956 			struct fxp_ipcb *ipcb;
    957 			/*
    958 			 * Deal with TCP/IP checksum offload. Note that
    959 			 * in order for TCP checksum offload to work,
    960 			 * the pseudo header checksum must have already
    961 			 * been computed and stored in the checksum field
    962 			 * in the TCP header. The stack should have
    963 			 * already done this for us.
    964 			 */
    965 			ipcb = &txd->txd_u.txdu_ipcb;
    966 			memset(ipcb, 0, sizeof(*ipcb));
    967 			/*
    968 			 * always do hardware parsing.
    969 			 */
    970 			ipcb->ipcb_ip_activation_high =
    971 			    FXP_IPCB_HARDWAREPARSING_ENABLE;
    972 			/*
    973 			 * ip checksum offloading.
    974 			 */
    975 			if (csum_flags & M_CSUM_IPv4) {
    976 				ipcb->ipcb_ip_schedule |=
    977 				    FXP_IPCB_IP_CHECKSUM_ENABLE;
    978 			}
    979 			/*
    980 			 * TCP/UDP checksum offloading.
    981 			 */
    982 			if (csum_flags & (M_CSUM_TCPv4 | M_CSUM_UDPv4)) {
    983 				ipcb->ipcb_ip_schedule |=
    984 				    FXP_IPCB_TCPUDP_CHECKSUM_ENABLE;
    985 			}
    986 
    987 			/*
    988 			 * request VLAN tag insertion if needed.
    989 			 */
    990 			vtag = VLAN_OUTPUT_TAG(&sc->sc_ethercom, m0);
    991 			if (vtag) {
    992 				ipcb->ipcb_vlan_id =
    993 				    htobe16(*(u_int *)(vtag + 1));
    994 				ipcb->ipcb_ip_activation_high |=
    995 				    FXP_IPCB_INSERTVLAN_ENABLE;
    996 			}
    997 		} else {
    998 			KASSERT((csum_flags &
    999 			    (M_CSUM_IPv4 | M_CSUM_TCPv4 | M_CSUM_UDPv4)) == 0);
   1000 		}
   1001 
   1002 		FXP_CDTXSYNC(sc, nexttx,
   1003 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1004 
   1005 		/* Advance the tx pointer. */
   1006 		sc->sc_txpending++;
   1007 		sc->sc_txlast = nexttx;
   1008 
   1009 #if NBPFILTER > 0
   1010 		/*
   1011 		 * Pass packet to bpf if there is a listener.
   1012 		 */
   1013 		if (ifp->if_bpf)
   1014 			bpf_mtap(ifp->if_bpf, m0);
   1015 #endif
   1016 	}
   1017 
   1018 	if (sc->sc_txpending == FXP_NTXCB - 1) {
   1019 		/* No more slots; notify upper layer. */
   1020 		ifp->if_flags |= IFF_OACTIVE;
   1021 	}
   1022 
   1023 	if (sc->sc_txpending != opending) {
   1024 		/*
   1025 		 * We enqueued packets.  If the transmitter was idle,
   1026 		 * reset the txdirty pointer.
   1027 		 */
   1028 		if (opending == 0)
   1029 			sc->sc_txdirty = FXP_NEXTTX(lasttx);
   1030 
   1031 		/*
   1032 		 * Cause the chip to interrupt and suspend command
   1033 		 * processing once the last packet we've enqueued
   1034 		 * has been transmitted.
   1035 		 *
   1036 		 * To avoid a race between updating status bits
   1037 		 * by the fxp chip and clearing command bits
   1038 		 * by this function on machines which don't have
   1039 		 * atomic methods to clear/set bits in memory
   1040 		 * smaller than 32bits (both cb_status and cb_command
   1041 		 * members are uint16_t and in the same 32bit word),
   1042 		 * we have to prepare a dummy TX descriptor which has
   1043 		 * NOP command and just causes a TX completion interrupt.
   1044 		 */
   1045 		sc->sc_txpending++;
   1046 		sc->sc_txlast = FXP_NEXTTX(sc->sc_txlast);
   1047 		txd = FXP_CDTX(sc, sc->sc_txlast);
   1048 		/* BIG_ENDIAN: no need to swap to store 0 */
   1049 		txd->txd_txcb.cb_status = 0;
   1050 		txd->txd_txcb.cb_command = htole16(FXP_CB_COMMAND_NOP |
   1051 		    FXP_CB_COMMAND_I | FXP_CB_COMMAND_S);
   1052 		FXP_CDTXSYNC(sc, sc->sc_txlast,
   1053 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1054 
   1055 		/*
   1056 		 * The entire packet chain is set up.  Clear the suspend bit
   1057 		 * on the command prior to the first packet we set up.
   1058 		 */
   1059 		FXP_CDTXSYNC(sc, lasttx,
   1060 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1061 		FXP_CDTX(sc, lasttx)->txd_txcb.cb_command &=
   1062 		    htole16(~FXP_CB_COMMAND_S);
   1063 		FXP_CDTXSYNC(sc, lasttx,
   1064 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1065 
   1066 		/*
   1067 		 * Issue a Resume command in case the chip was suspended.
   1068 		 */
   1069 		fxp_scb_wait(sc);
   1070 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
   1071 
   1072 		/* Set a watchdog timer in case the chip flakes out. */
   1073 		ifp->if_timer = 5;
   1074 	}
   1075 }
   1076 
   1077 /*
   1078  * Process interface interrupts.
   1079  */
   1080 int
   1081 fxp_intr(void *arg)
   1082 {
   1083 	struct fxp_softc *sc = arg;
   1084 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1085 	bus_dmamap_t rxmap;
   1086 	int claimed = 0, rnr;
   1087 	u_int8_t statack;
   1088 
   1089 	if (!device_is_active(sc->sc_dev) || sc->sc_enabled == 0)
   1090 		return (0);
   1091 	/*
   1092 	 * If the interface isn't running, don't try to
   1093 	 * service the interrupt.. just ack it and bail.
   1094 	 */
   1095 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
   1096 		statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK);
   1097 		if (statack) {
   1098 			claimed = 1;
   1099 			CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
   1100 		}
   1101 		return (claimed);
   1102 	}
   1103 
   1104 	while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
   1105 		claimed = 1;
   1106 
   1107 		/*
   1108 		 * First ACK all the interrupts in this pass.
   1109 		 */
   1110 		CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
   1111 
   1112 		/*
   1113 		 * Process receiver interrupts. If a no-resource (RNR)
   1114 		 * condition exists, get whatever packets we can and
   1115 		 * re-start the receiver.
   1116 		 */
   1117 		rnr = (statack & (FXP_SCB_STATACK_RNR | FXP_SCB_STATACK_SWI)) ?
   1118 		    1 : 0;
   1119 		if (statack & (FXP_SCB_STATACK_FR | FXP_SCB_STATACK_RNR |
   1120 		    FXP_SCB_STATACK_SWI)) {
   1121 			FXP_EVCNT_INCR(&sc->sc_ev_rxintr);
   1122 			rnr |= fxp_rxintr(sc);
   1123 		}
   1124 
   1125 		/*
   1126 		 * Free any finished transmit mbuf chains.
   1127 		 */
   1128 		if (statack & (FXP_SCB_STATACK_CXTNO|FXP_SCB_STATACK_CNA)) {
   1129 			FXP_EVCNT_INCR(&sc->sc_ev_txintr);
   1130 			fxp_txintr(sc);
   1131 
   1132 			/*
   1133 			 * Try to get more packets going.
   1134 			 */
   1135 			fxp_start(ifp);
   1136 
   1137 			if (sc->sc_txpending == 0) {
   1138 				/*
   1139 				 * Tell them that they can re-init now.
   1140 				 */
   1141 				if (sc->sc_flags & FXPF_WANTINIT)
   1142 					wakeup(sc);
   1143 			}
   1144 		}
   1145 
   1146 		if (rnr) {
   1147 			fxp_scb_wait(sc);
   1148 			fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_ABORT);
   1149 			rxmap = M_GETCTX(sc->sc_rxq.ifq_head, bus_dmamap_t);
   1150 			fxp_scb_wait(sc);
   1151 			CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
   1152 			    rxmap->dm_segs[0].ds_addr +
   1153 			    RFA_ALIGNMENT_FUDGE);
   1154 			fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
   1155 		}
   1156 	}
   1157 
   1158 #if NRND > 0
   1159 	if (claimed)
   1160 		rnd_add_uint32(&sc->rnd_source, statack);
   1161 #endif
   1162 	return (claimed);
   1163 }
   1164 
   1165 /*
   1166  * Handle transmit completion interrupts.
   1167  */
   1168 void
   1169 fxp_txintr(struct fxp_softc *sc)
   1170 {
   1171 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1172 	struct fxp_txdesc *txd;
   1173 	struct fxp_txsoft *txs;
   1174 	int i;
   1175 	u_int16_t txstat;
   1176 
   1177 	ifp->if_flags &= ~IFF_OACTIVE;
   1178 	for (i = sc->sc_txdirty; sc->sc_txpending != 0;
   1179 	    i = FXP_NEXTTX(i), sc->sc_txpending--) {
   1180 		txd = FXP_CDTX(sc, i);
   1181 		txs = FXP_DSTX(sc, i);
   1182 
   1183 		FXP_CDTXSYNC(sc, i,
   1184 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1185 
   1186 		/* skip dummy NOP TX descriptor */
   1187 		if ((le16toh(txd->txd_txcb.cb_command) & FXP_CB_COMMAND_CMD)
   1188 		    == FXP_CB_COMMAND_NOP)
   1189 			continue;
   1190 
   1191 		txstat = le16toh(txd->txd_txcb.cb_status);
   1192 
   1193 		if ((txstat & FXP_CB_STATUS_C) == 0)
   1194 			break;
   1195 
   1196 		bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
   1197 		    0, txs->txs_dmamap->dm_mapsize,
   1198 		    BUS_DMASYNC_POSTWRITE);
   1199 		bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   1200 		m_freem(txs->txs_mbuf);
   1201 		txs->txs_mbuf = NULL;
   1202 	}
   1203 
   1204 	/* Update the dirty transmit buffer pointer. */
   1205 	sc->sc_txdirty = i;
   1206 
   1207 	/*
   1208 	 * Cancel the watchdog timer if there are no pending
   1209 	 * transmissions.
   1210 	 */
   1211 	if (sc->sc_txpending == 0)
   1212 		ifp->if_timer = 0;
   1213 }
   1214 
   1215 /*
   1216  * fxp_rx_hwcksum: check status of H/W offloading for received packets.
   1217  */
   1218 
   1219 void
   1220 fxp_rx_hwcksum(struct fxp_softc *sc, struct mbuf *m, const struct fxp_rfa *rfa,
   1221     u_int len)
   1222 {
   1223 	uint32_t csum_data;
   1224 	int csum_flags;
   1225 
   1226 	/*
   1227 	 * check H/W Checksumming.
   1228 	 */
   1229 
   1230 	csum_flags = 0;
   1231 	csum_data = 0;
   1232 
   1233 	if ((sc->sc_flags & FXPF_EXT_RFA) != 0) {
   1234 		uint8_t rxparsestat;
   1235 		uint8_t csum_stat;
   1236 
   1237 		csum_stat = rfa->cksum_stat;
   1238 		rxparsestat = rfa->rx_parse_stat;
   1239 		if ((rfa->rfa_status & htole16(FXP_RFA_STATUS_PARSE)) == 0)
   1240 			goto out;
   1241 
   1242 		if (csum_stat & FXP_RFDX_CS_IP_CSUM_BIT_VALID) {
   1243 			csum_flags = M_CSUM_IPv4;
   1244 			if ((csum_stat & FXP_RFDX_CS_IP_CSUM_VALID) == 0)
   1245 				csum_flags |= M_CSUM_IPv4_BAD;
   1246 		}
   1247 
   1248 		if (csum_stat & FXP_RFDX_CS_TCPUDP_CSUM_BIT_VALID) {
   1249 			csum_flags |= (M_CSUM_TCPv4|M_CSUM_UDPv4); /* XXX */
   1250 			if ((csum_stat & FXP_RFDX_CS_TCPUDP_CSUM_VALID) == 0)
   1251 				csum_flags |= M_CSUM_TCP_UDP_BAD;
   1252 		}
   1253 
   1254 	} else if ((sc->sc_flags & FXPF_82559_RXCSUM) != 0) {
   1255 		struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1256 		struct ether_header *eh;
   1257 		struct ip *ip;
   1258 		struct udphdr *uh;
   1259 		u_int hlen, pktlen;
   1260 		uint32_t optsum, optlen;
   1261 		uint16_t *opts;
   1262 
   1263 		if (len < ETHER_HDR_LEN + sizeof(struct ip))
   1264 			goto out;
   1265 		pktlen = len - ETHER_HDR_LEN;
   1266 		eh = mtod(m, struct ether_header *);
   1267 		if (ntohs(eh->ether_type) != ETHERTYPE_IP)
   1268 			goto out;
   1269 		ip = (struct ip *)((uint8_t *)eh + ETHER_HDR_LEN);
   1270 		if (ip->ip_v != IPVERSION)
   1271 			goto out;
   1272 
   1273 		hlen = ip->ip_hl << 2;
   1274 		if (hlen < sizeof(struct ip))
   1275 			goto out;
   1276 
   1277 		/*
   1278 		 * Bail if too short, has random trailing garbage, truncated,
   1279 		 * fragment, or has ethernet pad.
   1280 		 */
   1281 		if (ntohs(ip->ip_len) < hlen ||
   1282 		    ntohs(ip->ip_len) != pktlen ||
   1283 		    (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK)) != 0)
   1284 			goto out;
   1285 
   1286 		switch (ip->ip_p) {
   1287 		case IPPROTO_TCP:
   1288 			if ((ifp->if_csum_flags_rx & M_CSUM_TCPv4) == 0 ||
   1289 			    pktlen < (hlen + sizeof(struct tcphdr)))
   1290 				goto out;
   1291 			csum_flags =
   1292 			    M_CSUM_TCPv4 | M_CSUM_DATA | M_CSUM_NO_PSEUDOHDR;
   1293 			break;
   1294 		case IPPROTO_UDP:
   1295 			if ((ifp->if_csum_flags_rx & M_CSUM_UDPv4) == 0 ||
   1296 			    pktlen < (hlen + sizeof(struct udphdr)))
   1297 				goto out;
   1298 			uh = (struct udphdr *)((uint8_t *)ip + hlen);
   1299 			if (uh->uh_sum == 0)
   1300 				goto out;	/* no checksum */
   1301 			csum_flags =
   1302 			    M_CSUM_UDPv4 | M_CSUM_DATA | M_CSUM_NO_PSEUDOHDR;
   1303 			break;
   1304 		default:
   1305 			goto out;
   1306 		}
   1307 
   1308 		/* Extract computed checksum. */
   1309 		csum_data = be16dec(mtod(m, uint8_t *) + len);
   1310 
   1311 		/* If the packet had IP options, we have to deduct them. */
   1312 		optlen = hlen - sizeof(struct ip);
   1313 		if (optlen > 0) {
   1314 			optsum = 0;
   1315 			opts = (uint16_t *)((uint8_t *)ip + sizeof(struct ip));
   1316 
   1317 			while (optlen > 1) {
   1318 				optsum += ntohs(*opts++);
   1319 				optlen -= sizeof(uint16_t);
   1320 			}
   1321 			while (optsum >> 16)
   1322 				optsum = (optsum >> 16) + (optsum & 0xffff);
   1323 
   1324 			/* Deduct the IP opts sum from the hwsum (RFC 1624). */
   1325 			csum_data = ~(~csum_data - ~optsum);
   1326 
   1327 			while (csum_data >> 16)
   1328 				csum_data =
   1329 				    (csum_data >> 16) + (csum_data & 0xffff);
   1330 		}
   1331 	}
   1332  out:
   1333 	m->m_pkthdr.csum_flags = csum_flags;
   1334 	m->m_pkthdr.csum_data = csum_data;
   1335 }
   1336 
   1337 /*
   1338  * Handle receive interrupts.
   1339  */
   1340 int
   1341 fxp_rxintr(struct fxp_softc *sc)
   1342 {
   1343 	struct ethercom *ec = &sc->sc_ethercom;
   1344 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1345 	struct mbuf *m, *m0;
   1346 	bus_dmamap_t rxmap;
   1347 	struct fxp_rfa *rfa;
   1348 	int rnr;
   1349 	u_int16_t len, rxstat;
   1350 
   1351 	rnr = 0;
   1352 
   1353 	for (;;) {
   1354 		m = sc->sc_rxq.ifq_head;
   1355 		rfa = FXP_MTORFA(m);
   1356 		rxmap = M_GETCTX(m, bus_dmamap_t);
   1357 
   1358 		FXP_RFASYNC(sc, m,
   1359 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1360 
   1361 		rxstat = le16toh(rfa->rfa_status);
   1362 
   1363 		if ((rxstat & FXP_RFA_STATUS_RNR) != 0)
   1364 			rnr = 1;
   1365 
   1366 		if ((rxstat & FXP_RFA_STATUS_C) == 0) {
   1367 			/*
   1368 			 * We have processed all of the
   1369 			 * receive buffers.
   1370 			 */
   1371 			FXP_RFASYNC(sc, m, BUS_DMASYNC_PREREAD);
   1372 			return rnr;
   1373 		}
   1374 
   1375 		IF_DEQUEUE(&sc->sc_rxq, m);
   1376 
   1377 		FXP_RXBUFSYNC(sc, m, BUS_DMASYNC_POSTREAD);
   1378 
   1379 		len = le16toh(rfa->actual_size) &
   1380 		    (m->m_ext.ext_size - 1);
   1381 		if ((sc->sc_flags & FXPF_82559_RXCSUM) != 0) {
   1382 			/* Adjust for appended checksum bytes. */
   1383 			len -= sizeof(uint16_t);
   1384 		}
   1385 
   1386 		if (len < sizeof(struct ether_header)) {
   1387 			/*
   1388 			 * Runt packet; drop it now.
   1389 			 */
   1390 			FXP_INIT_RFABUF(sc, m);
   1391 			continue;
   1392 		}
   1393 
   1394 		/*
   1395 		 * If support for 802.1Q VLAN sized frames is
   1396 		 * enabled, we need to do some additional error
   1397 		 * checking (as we are saving bad frames, in
   1398 		 * order to receive the larger ones).
   1399 		 */
   1400 		if ((ec->ec_capenable & ETHERCAP_VLAN_MTU) != 0 &&
   1401 		    (rxstat & (FXP_RFA_STATUS_OVERRUN|
   1402 			       FXP_RFA_STATUS_RNR|
   1403 			       FXP_RFA_STATUS_ALIGN|
   1404 			       FXP_RFA_STATUS_CRC)) != 0) {
   1405 			FXP_INIT_RFABUF(sc, m);
   1406 			continue;
   1407 		}
   1408 
   1409 		/*
   1410 		 * check VLAN tag stripping.
   1411 		 */
   1412 		if ((sc->sc_flags & FXPF_EXT_RFA) != 0 &&
   1413 		    (rfa->rfa_status & htole16(FXP_RFA_STATUS_VLAN)) != 0) {
   1414 			struct m_tag *vtag;
   1415 
   1416 			vtag = m_tag_get(PACKET_TAG_VLAN, sizeof(u_int),
   1417 			    M_NOWAIT);
   1418 			if (vtag == NULL)
   1419 				goto dropit;
   1420 			*(u_int *)(vtag + 1) = be16toh(rfa->vlan_id);
   1421 			m_tag_prepend(m, vtag);
   1422 		}
   1423 
   1424 		/* Do checksum checking. */
   1425 		if ((ifp->if_csum_flags_rx & (M_CSUM_TCPv4|M_CSUM_UDPv4)) != 0)
   1426 			fxp_rx_hwcksum(sc, m, rfa, len);
   1427 
   1428 		/*
   1429 		 * If the packet is small enough to fit in a
   1430 		 * single header mbuf, allocate one and copy
   1431 		 * the data into it.  This greatly reduces
   1432 		 * memory consumption when we receive lots
   1433 		 * of small packets.
   1434 		 *
   1435 		 * Otherwise, we add a new buffer to the receive
   1436 		 * chain.  If this fails, we drop the packet and
   1437 		 * recycle the old buffer.
   1438 		 */
   1439 		if (fxp_copy_small != 0 && len <= MHLEN) {
   1440 			MGETHDR(m0, M_DONTWAIT, MT_DATA);
   1441 			if (m0 == NULL)
   1442 				goto dropit;
   1443 			MCLAIM(m0, &sc->sc_ethercom.ec_rx_mowner);
   1444 			memcpy(mtod(m0, void *),
   1445 			    mtod(m, void *), len);
   1446 			m0->m_pkthdr.csum_flags = m->m_pkthdr.csum_flags;
   1447 			m0->m_pkthdr.csum_data = m->m_pkthdr.csum_data;
   1448 			FXP_INIT_RFABUF(sc, m);
   1449 			m = m0;
   1450 		} else {
   1451 			if (fxp_add_rfabuf(sc, rxmap, 1) != 0) {
   1452  dropit:
   1453 				ifp->if_ierrors++;
   1454 				FXP_INIT_RFABUF(sc, m);
   1455 				continue;
   1456 			}
   1457 		}
   1458 
   1459 		m->m_pkthdr.rcvif = ifp;
   1460 		m->m_pkthdr.len = m->m_len = len;
   1461 
   1462 #if NBPFILTER > 0
   1463 		/*
   1464 		 * Pass this up to any BPF listeners, but only
   1465 		 * pass it up the stack if it's for us.
   1466 		 */
   1467 		if (ifp->if_bpf)
   1468 			bpf_mtap(ifp->if_bpf, m);
   1469 #endif
   1470 
   1471 		/* Pass it on. */
   1472 		(*ifp->if_input)(ifp, m);
   1473 	}
   1474 }
   1475 
   1476 /*
   1477  * Update packet in/out/collision statistics. The i82557 doesn't
   1478  * allow you to access these counters without doing a fairly
   1479  * expensive DMA to get _all_ of the statistics it maintains, so
   1480  * we do this operation here only once per second. The statistics
   1481  * counters in the kernel are updated from the previous dump-stats
   1482  * DMA and then a new dump-stats DMA is started. The on-chip
   1483  * counters are zeroed when the DMA completes. If we can't start
   1484  * the DMA immediately, we don't wait - we just prepare to read
   1485  * them again next time.
   1486  */
   1487 void
   1488 fxp_tick(void *arg)
   1489 {
   1490 	struct fxp_softc *sc = arg;
   1491 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1492 	struct fxp_stats *sp = &sc->sc_control_data->fcd_stats;
   1493 	int s;
   1494 
   1495 	if (!device_is_active(sc->sc_dev))
   1496 		return;
   1497 
   1498 	s = splnet();
   1499 
   1500 	FXP_CDSTATSSYNC(sc, BUS_DMASYNC_POSTREAD);
   1501 
   1502 	ifp->if_opackets += le32toh(sp->tx_good);
   1503 	ifp->if_collisions += le32toh(sp->tx_total_collisions);
   1504 	if (sp->rx_good) {
   1505 		ifp->if_ipackets += le32toh(sp->rx_good);
   1506 		sc->sc_rxidle = 0;
   1507 	} else if (sc->sc_flags & FXPF_RECV_WORKAROUND) {
   1508 		sc->sc_rxidle++;
   1509 	}
   1510 	ifp->if_ierrors +=
   1511 	    le32toh(sp->rx_crc_errors) +
   1512 	    le32toh(sp->rx_alignment_errors) +
   1513 	    le32toh(sp->rx_rnr_errors) +
   1514 	    le32toh(sp->rx_overrun_errors);
   1515 	/*
   1516 	 * If any transmit underruns occurred, bump up the transmit
   1517 	 * threshold by another 512 bytes (64 * 8).
   1518 	 */
   1519 	if (sp->tx_underruns) {
   1520 		ifp->if_oerrors += le32toh(sp->tx_underruns);
   1521 		if (tx_threshold < 192)
   1522 			tx_threshold += 64;
   1523 	}
   1524 #ifdef FXP_EVENT_COUNTERS
   1525 	if (sc->sc_flags & FXPF_FC) {
   1526 		sc->sc_ev_txpause.ev_count += sp->tx_pauseframes;
   1527 		sc->sc_ev_rxpause.ev_count += sp->rx_pauseframes;
   1528 	}
   1529 #endif
   1530 
   1531 	/*
   1532 	 * If we haven't received any packets in FXP_MAX_RX_IDLE seconds,
   1533 	 * then assume the receiver has locked up and attempt to clear
   1534 	 * the condition by reprogramming the multicast filter (actually,
   1535 	 * resetting the interface). This is a work-around for a bug in
   1536 	 * the 82557 where the receiver locks up if it gets certain types
   1537 	 * of garbage in the synchronization bits prior to the packet header.
   1538 	 * This bug is supposed to only occur in 10Mbps mode, but has been
   1539 	 * seen to occur in 100Mbps mode as well (perhaps due to a 10/100
   1540 	 * speed transition).
   1541 	 */
   1542 	if (sc->sc_rxidle > FXP_MAX_RX_IDLE) {
   1543 		(void) fxp_init(ifp);
   1544 		splx(s);
   1545 		return;
   1546 	}
   1547 	/*
   1548 	 * If there is no pending command, start another stats
   1549 	 * dump. Otherwise punt for now.
   1550 	 */
   1551 	if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
   1552 		/*
   1553 		 * Start another stats dump.
   1554 		 */
   1555 		FXP_CDSTATSSYNC(sc, BUS_DMASYNC_PREREAD);
   1556 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMPRESET);
   1557 	} else {
   1558 		/*
   1559 		 * A previous command is still waiting to be accepted.
   1560 		 * Just zero our copy of the stats and wait for the
   1561 		 * next timer event to update them.
   1562 		 */
   1563 		/* BIG_ENDIAN: no swap required to store 0 */
   1564 		sp->tx_good = 0;
   1565 		sp->tx_underruns = 0;
   1566 		sp->tx_total_collisions = 0;
   1567 
   1568 		sp->rx_good = 0;
   1569 		sp->rx_crc_errors = 0;
   1570 		sp->rx_alignment_errors = 0;
   1571 		sp->rx_rnr_errors = 0;
   1572 		sp->rx_overrun_errors = 0;
   1573 		if (sc->sc_flags & FXPF_FC) {
   1574 			sp->tx_pauseframes = 0;
   1575 			sp->rx_pauseframes = 0;
   1576 		}
   1577 	}
   1578 
   1579 	if (sc->sc_flags & FXPF_MII) {
   1580 		/* Tick the MII clock. */
   1581 		mii_tick(&sc->sc_mii);
   1582 	}
   1583 
   1584 	splx(s);
   1585 
   1586 	/*
   1587 	 * Schedule another timeout one second from now.
   1588 	 */
   1589 	callout_reset(&sc->sc_callout, hz, fxp_tick, sc);
   1590 }
   1591 
   1592 /*
   1593  * Drain the receive queue.
   1594  */
   1595 void
   1596 fxp_rxdrain(struct fxp_softc *sc)
   1597 {
   1598 	bus_dmamap_t rxmap;
   1599 	struct mbuf *m;
   1600 
   1601 	for (;;) {
   1602 		IF_DEQUEUE(&sc->sc_rxq, m);
   1603 		if (m == NULL)
   1604 			break;
   1605 		rxmap = M_GETCTX(m, bus_dmamap_t);
   1606 		bus_dmamap_unload(sc->sc_dmat, rxmap);
   1607 		FXP_RXMAP_PUT(sc, rxmap);
   1608 		m_freem(m);
   1609 	}
   1610 }
   1611 
   1612 /*
   1613  * Stop the interface. Cancels the statistics updater and resets
   1614  * the interface.
   1615  */
   1616 void
   1617 fxp_stop(struct ifnet *ifp, int disable)
   1618 {
   1619 	struct fxp_softc *sc = ifp->if_softc;
   1620 	struct fxp_txsoft *txs;
   1621 	int i;
   1622 
   1623 	/*
   1624 	 * Turn down interface (done early to avoid bad interactions
   1625 	 * between panics, shutdown hooks, and the watchdog timer)
   1626 	 */
   1627 	ifp->if_timer = 0;
   1628 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1629 
   1630 	/*
   1631 	 * Cancel stats updater.
   1632 	 */
   1633 	callout_stop(&sc->sc_callout);
   1634 	if (sc->sc_flags & FXPF_MII) {
   1635 		/* Down the MII. */
   1636 		mii_down(&sc->sc_mii);
   1637 	}
   1638 
   1639 	/*
   1640 	 * Issue software reset.  This unloads any microcode that
   1641 	 * might already be loaded.
   1642 	 */
   1643 	sc->sc_flags &= ~FXPF_UCODE_LOADED;
   1644 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET);
   1645 	DELAY(50);
   1646 
   1647 	/*
   1648 	 * Release any xmit buffers.
   1649 	 */
   1650 	for (i = 0; i < FXP_NTXCB; i++) {
   1651 		txs = FXP_DSTX(sc, i);
   1652 		if (txs->txs_mbuf != NULL) {
   1653 			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   1654 			m_freem(txs->txs_mbuf);
   1655 			txs->txs_mbuf = NULL;
   1656 		}
   1657 	}
   1658 	sc->sc_txpending = 0;
   1659 
   1660 	if (disable) {
   1661 		fxp_rxdrain(sc);
   1662 		fxp_disable(sc);
   1663 	}
   1664 
   1665 }
   1666 
   1667 /*
   1668  * Watchdog/transmission transmit timeout handler. Called when a
   1669  * transmission is started on the interface, but no interrupt is
   1670  * received before the timeout. This usually indicates that the
   1671  * card has wedged for some reason.
   1672  */
   1673 void
   1674 fxp_watchdog(struct ifnet *ifp)
   1675 {
   1676 	struct fxp_softc *sc = ifp->if_softc;
   1677 
   1678 	log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
   1679 	ifp->if_oerrors++;
   1680 
   1681 	(void) fxp_init(ifp);
   1682 }
   1683 
   1684 /*
   1685  * Initialize the interface.  Must be called at splnet().
   1686  */
   1687 int
   1688 fxp_init(struct ifnet *ifp)
   1689 {
   1690 	struct fxp_softc *sc = ifp->if_softc;
   1691 	struct fxp_cb_config *cbp;
   1692 	struct fxp_cb_ias *cb_ias;
   1693 	struct fxp_txdesc *txd;
   1694 	bus_dmamap_t rxmap;
   1695 	int i, prm, save_bf, lrxen, vlan_drop, allm, error = 0;
   1696 	uint16_t status;
   1697 
   1698 	if ((error = fxp_enable(sc)) != 0)
   1699 		goto out;
   1700 
   1701 	/*
   1702 	 * Cancel any pending I/O
   1703 	 */
   1704 	fxp_stop(ifp, 0);
   1705 
   1706 	/*
   1707 	 * XXX just setting sc_flags to 0 here clears any FXPF_MII
   1708 	 * flag, and this prevents the MII from detaching resulting in
   1709 	 * a panic. The flags field should perhaps be split in runtime
   1710 	 * flags and more static information. For now, just clear the
   1711 	 * only other flag set.
   1712 	 */
   1713 
   1714 	sc->sc_flags &= ~FXPF_WANTINIT;
   1715 
   1716 	/*
   1717 	 * Initialize base of CBL and RFA memory. Loading with zero
   1718 	 * sets it up for regular linear addressing.
   1719 	 */
   1720 	fxp_scb_wait(sc);
   1721 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
   1722 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_BASE);
   1723 
   1724 	fxp_scb_wait(sc);
   1725 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_BASE);
   1726 
   1727 	/*
   1728 	 * Initialize the multicast filter.  Do this now, since we might
   1729 	 * have to setup the config block differently.
   1730 	 */
   1731 	fxp_mc_setup(sc);
   1732 
   1733 	prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
   1734 	allm = (ifp->if_flags & IFF_ALLMULTI) ? 1 : 0;
   1735 
   1736 	/*
   1737 	 * In order to support receiving 802.1Q VLAN frames, we have to
   1738 	 * enable "save bad frames", since they are 4 bytes larger than
   1739 	 * the normal Ethernet maximum frame length.  On i82558 and later,
   1740 	 * we have a better mechanism for this.
   1741 	 */
   1742 	save_bf = 0;
   1743 	lrxen = 0;
   1744 	vlan_drop = 0;
   1745 	if (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) {
   1746 		if (sc->sc_rev < FXP_REV_82558_A4)
   1747 			save_bf = 1;
   1748 		else
   1749 			lrxen = 1;
   1750 		if (sc->sc_rev >= FXP_REV_82550)
   1751 			vlan_drop = 1;
   1752 	}
   1753 
   1754 	/*
   1755 	 * Initialize base of dump-stats buffer.
   1756 	 */
   1757 	fxp_scb_wait(sc);
   1758 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
   1759 	    sc->sc_cddma + FXP_CDSTATSOFF);
   1760 	FXP_CDSTATSSYNC(sc, BUS_DMASYNC_PREREAD);
   1761 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMP_ADR);
   1762 
   1763 	cbp = &sc->sc_control_data->fcd_configcb;
   1764 	memset(cbp, 0, sizeof(struct fxp_cb_config));
   1765 
   1766 	/*
   1767 	 * Load microcode for this controller.
   1768 	 */
   1769 	fxp_load_ucode(sc);
   1770 
   1771 	if ((sc->sc_ethercom.ec_if.if_flags & IFF_LINK1))
   1772 		sc->sc_flags |= FXPF_RECV_WORKAROUND;
   1773 	else
   1774 		sc->sc_flags &= ~FXPF_RECV_WORKAROUND;
   1775 
   1776 	/*
   1777 	 * This copy is kind of disgusting, but there are a bunch of must be
   1778 	 * zero and must be one bits in this structure and this is the easiest
   1779 	 * way to initialize them all to proper values.
   1780 	 */
   1781 	memcpy(cbp, fxp_cb_config_template, sizeof(fxp_cb_config_template));
   1782 
   1783 	/* BIG_ENDIAN: no need to swap to store 0 */
   1784 	cbp->cb_status =	0;
   1785 	cbp->cb_command =	htole16(FXP_CB_COMMAND_CONFIG |
   1786 				    FXP_CB_COMMAND_EL);
   1787 	/* BIG_ENDIAN: no need to swap to store 0xffffffff */
   1788 	cbp->link_addr =	0xffffffff; /* (no) next command */
   1789 					/* bytes in config block */
   1790 	cbp->byte_count =	(sc->sc_flags & FXPF_EXT_RFA) ?
   1791 				FXP_EXT_CONFIG_LEN : FXP_CONFIG_LEN;
   1792 	cbp->rx_fifo_limit =	8;	/* rx fifo threshold (32 bytes) */
   1793 	cbp->tx_fifo_limit =	0;	/* tx fifo threshold (0 bytes) */
   1794 	cbp->adaptive_ifs =	0;	/* (no) adaptive interframe spacing */
   1795 	cbp->mwi_enable =	(sc->sc_flags & FXPF_MWI) ? 1 : 0;
   1796 	cbp->type_enable =	0;	/* actually reserved */
   1797 	cbp->read_align_en =	(sc->sc_flags & FXPF_READ_ALIGN) ? 1 : 0;
   1798 	cbp->end_wr_on_cl =	(sc->sc_flags & FXPF_WRITE_ALIGN) ? 1 : 0;
   1799 	cbp->rx_dma_bytecount =	0;	/* (no) rx DMA max */
   1800 	cbp->tx_dma_bytecount =	0;	/* (no) tx DMA max */
   1801 	cbp->dma_mbce =		0;	/* (disable) dma max counters */
   1802 	cbp->late_scb =		0;	/* (don't) defer SCB update */
   1803 	cbp->tno_int_or_tco_en =0;	/* (disable) tx not okay interrupt */
   1804 	cbp->ci_int =		1;	/* interrupt on CU idle */
   1805 	cbp->ext_txcb_dis =	(sc->sc_flags & FXPF_EXT_TXCB) ? 0 : 1;
   1806 	cbp->ext_stats_dis =	1;	/* disable extended counters */
   1807 	cbp->keep_overrun_rx =	0;	/* don't pass overrun frames to host */
   1808 	cbp->save_bf =		save_bf;/* save bad frames */
   1809 	cbp->disc_short_rx =	!prm;	/* discard short packets */
   1810 	cbp->underrun_retry =	1;	/* retry mode (1) on DMA underrun */
   1811 	cbp->ext_rfa =		(sc->sc_flags & FXPF_EXT_RFA) ? 1 : 0;
   1812 	cbp->two_frames =	0;	/* do not limit FIFO to 2 frames */
   1813 	cbp->dyn_tbd =		0;	/* (no) dynamic TBD mode */
   1814 					/* interface mode */
   1815 	cbp->mediatype =	(sc->sc_flags & FXPF_MII) ? 1 : 0;
   1816 	cbp->csma_dis =		0;	/* (don't) disable link */
   1817 	cbp->tcp_udp_cksum =	(sc->sc_flags & FXPF_82559_RXCSUM) ? 1 : 0;
   1818 					/* (don't) enable RX checksum */
   1819 	cbp->vlan_tco =		0;	/* (don't) enable vlan wakeup */
   1820 	cbp->link_wake_en =	0;	/* (don't) assert PME# on link change */
   1821 	cbp->arp_wake_en =	0;	/* (don't) assert PME# on arp */
   1822 	cbp->mc_wake_en =	0;	/* (don't) assert PME# on mcmatch */
   1823 	cbp->nsai =		1;	/* (don't) disable source addr insert */
   1824 	cbp->preamble_length =	2;	/* (7 byte) preamble */
   1825 	cbp->loopback =		0;	/* (don't) loopback */
   1826 	cbp->linear_priority =	0;	/* (normal CSMA/CD operation) */
   1827 	cbp->linear_pri_mode =	0;	/* (wait after xmit only) */
   1828 	cbp->interfrm_spacing =	6;	/* (96 bits of) interframe spacing */
   1829 	cbp->promiscuous =	prm;	/* promiscuous mode */
   1830 	cbp->bcast_disable =	0;	/* (don't) disable broadcasts */
   1831 	cbp->wait_after_win =	0;	/* (don't) enable modified backoff alg*/
   1832 	cbp->ignore_ul =	0;	/* consider U/L bit in IA matching */
   1833 	cbp->crc16_en =		0;	/* (don't) enable crc-16 algorithm */
   1834 	cbp->crscdt =		(sc->sc_flags & FXPF_MII) ? 0 : 1;
   1835 	cbp->stripping =	!prm;	/* truncate rx packet to byte count */
   1836 	cbp->padding =		1;	/* (do) pad short tx packets */
   1837 	cbp->rcv_crc_xfer =	0;	/* (don't) xfer CRC to host */
   1838 	cbp->long_rx_en =	lrxen;	/* long packet receive enable */
   1839 	cbp->ia_wake_en =	0;	/* (don't) wake up on address match */
   1840 	cbp->magic_pkt_dis =	0;	/* (don't) disable magic packet */
   1841 					/* must set wake_en in PMCSR also */
   1842 	cbp->force_fdx =	0;	/* (don't) force full duplex */
   1843 	cbp->fdx_pin_en =	1;	/* (enable) FDX# pin */
   1844 	cbp->multi_ia =		0;	/* (don't) accept multiple IAs */
   1845 	cbp->mc_all =		allm;	/* accept all multicasts */
   1846 	cbp->ext_rx_mode =	(sc->sc_flags & FXPF_EXT_RFA) ? 1 : 0;
   1847 	cbp->vlan_drop_en =	vlan_drop;
   1848 
   1849 	if (!(sc->sc_flags & FXPF_FC)) {
   1850 		/*
   1851 		 * The i82557 has no hardware flow control, the values
   1852 		 * here are the defaults for the chip.
   1853 		 */
   1854 		cbp->fc_delay_lsb =	0;
   1855 		cbp->fc_delay_msb =	0x40;
   1856 		cbp->pri_fc_thresh =	3;
   1857 		cbp->tx_fc_dis =	0;
   1858 		cbp->rx_fc_restop =	0;
   1859 		cbp->rx_fc_restart =	0;
   1860 		cbp->fc_filter =	0;
   1861 		cbp->pri_fc_loc =	1;
   1862 	} else {
   1863 		cbp->fc_delay_lsb =	0x1f;
   1864 		cbp->fc_delay_msb =	0x01;
   1865 		cbp->pri_fc_thresh =	3;
   1866 		cbp->tx_fc_dis =	0;	/* enable transmit FC */
   1867 		cbp->rx_fc_restop =	1;	/* enable FC restop frames */
   1868 		cbp->rx_fc_restart =	1;	/* enable FC restart frames */
   1869 		cbp->fc_filter =	!prm;	/* drop FC frames to host */
   1870 		cbp->pri_fc_loc =	1;	/* FC pri location (byte31) */
   1871 		cbp->ext_stats_dis =	0;	/* enable extended stats */
   1872 	}
   1873 
   1874 	FXP_CDCONFIGSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1875 
   1876 	/*
   1877 	 * Start the config command/DMA.
   1878 	 */
   1879 	fxp_scb_wait(sc);
   1880 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->sc_cddma + FXP_CDCONFIGOFF);
   1881 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
   1882 	/* ...and wait for it to complete. */
   1883 	for (i = 1000; i > 0; i--) {
   1884 		FXP_CDCONFIGSYNC(sc,
   1885 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1886 		status = le16toh(cbp->cb_status);
   1887 		FXP_CDCONFIGSYNC(sc, BUS_DMASYNC_PREREAD);
   1888 		if ((status & FXP_CB_STATUS_C) != 0)
   1889 			break;
   1890 		DELAY(1);
   1891 	}
   1892 	if (i == 0) {
   1893 		log(LOG_WARNING, "%s: line %d: dmasync timeout\n",
   1894 		    device_xname(sc->sc_dev), __LINE__);
   1895 		return (ETIMEDOUT);
   1896 	}
   1897 
   1898 	/*
   1899 	 * Initialize the station address.
   1900 	 */
   1901 	cb_ias = &sc->sc_control_data->fcd_iascb;
   1902 	/* BIG_ENDIAN: no need to swap to store 0 */
   1903 	cb_ias->cb_status = 0;
   1904 	cb_ias->cb_command = htole16(FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL);
   1905 	/* BIG_ENDIAN: no need to swap to store 0xffffffff */
   1906 	cb_ias->link_addr = 0xffffffff;
   1907 	memcpy(cb_ias->macaddr, CLLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
   1908 
   1909 	FXP_CDIASSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1910 
   1911 	/*
   1912 	 * Start the IAS (Individual Address Setup) command/DMA.
   1913 	 */
   1914 	fxp_scb_wait(sc);
   1915 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->sc_cddma + FXP_CDIASOFF);
   1916 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
   1917 	/* ...and wait for it to complete. */
   1918 	for (i = 1000; i > 0; i++) {
   1919 		FXP_CDIASSYNC(sc,
   1920 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1921 		status = le16toh(cb_ias->cb_status);
   1922 		FXP_CDIASSYNC(sc, BUS_DMASYNC_PREREAD);
   1923 		if ((status & FXP_CB_STATUS_C) != 0)
   1924 			break;
   1925 		DELAY(1);
   1926 	}
   1927 	if (i == 0) {
   1928 		log(LOG_WARNING, "%s: line %d: dmasync timeout\n",
   1929 		    device_xname(sc->sc_dev), __LINE__);
   1930 		return (ETIMEDOUT);
   1931 	}
   1932 
   1933 	/*
   1934 	 * Initialize the transmit descriptor ring.  txlast is initialized
   1935 	 * to the end of the list so that it will wrap around to the first
   1936 	 * descriptor when the first packet is transmitted.
   1937 	 */
   1938 	for (i = 0; i < FXP_NTXCB; i++) {
   1939 		txd = FXP_CDTX(sc, i);
   1940 		memset(txd, 0, sizeof(*txd));
   1941 		txd->txd_txcb.cb_command =
   1942 		    htole16(FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S);
   1943 		txd->txd_txcb.link_addr =
   1944 		    htole32(FXP_CDTXADDR(sc, FXP_NEXTTX(i)));
   1945 		if (sc->sc_flags & FXPF_EXT_TXCB)
   1946 			txd->txd_txcb.tbd_array_addr =
   1947 			    htole32(FXP_CDTBDADDR(sc, i) +
   1948 				    (2 * sizeof(struct fxp_tbd)));
   1949 		else
   1950 			txd->txd_txcb.tbd_array_addr =
   1951 			    htole32(FXP_CDTBDADDR(sc, i));
   1952 		FXP_CDTXSYNC(sc, i, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1953 	}
   1954 	sc->sc_txpending = 0;
   1955 	sc->sc_txdirty = 0;
   1956 	sc->sc_txlast = FXP_NTXCB - 1;
   1957 
   1958 	/*
   1959 	 * Initialize the receive buffer list.
   1960 	 */
   1961 	sc->sc_rxq.ifq_maxlen = FXP_NRFABUFS;
   1962 	while (sc->sc_rxq.ifq_len < FXP_NRFABUFS) {
   1963 		rxmap = FXP_RXMAP_GET(sc);
   1964 		if ((error = fxp_add_rfabuf(sc, rxmap, 0)) != 0) {
   1965 			log(LOG_ERR, "%s: unable to allocate or map rx "
   1966 			    "buffer %d, error = %d\n",
   1967 			    device_xname(sc->sc_dev),
   1968 			    sc->sc_rxq.ifq_len, error);
   1969 			/*
   1970 			 * XXX Should attempt to run with fewer receive
   1971 			 * XXX buffers instead of just failing.
   1972 			 */
   1973 			FXP_RXMAP_PUT(sc, rxmap);
   1974 			fxp_rxdrain(sc);
   1975 			goto out;
   1976 		}
   1977 	}
   1978 	sc->sc_rxidle = 0;
   1979 
   1980 	/*
   1981 	 * Give the transmit ring to the chip.  We do this by pointing
   1982 	 * the chip at the last descriptor (which is a NOP|SUSPEND), and
   1983 	 * issuing a start command.  It will execute the NOP and then
   1984 	 * suspend, pointing at the first descriptor.
   1985 	 */
   1986 	fxp_scb_wait(sc);
   1987 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, FXP_CDTXADDR(sc, sc->sc_txlast));
   1988 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
   1989 
   1990 	/*
   1991 	 * Initialize receiver buffer area - RFA.
   1992 	 */
   1993 #if 0	/* initialization will be done by FXP_SCB_INTRCNTL_REQUEST_SWI later */
   1994 	rxmap = M_GETCTX(sc->sc_rxq.ifq_head, bus_dmamap_t);
   1995 	fxp_scb_wait(sc);
   1996 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
   1997 	    rxmap->dm_segs[0].ds_addr + RFA_ALIGNMENT_FUDGE);
   1998 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
   1999 #endif
   2000 
   2001 	if (sc->sc_flags & FXPF_MII) {
   2002 		/*
   2003 		 * Set current media.
   2004 		 */
   2005 		if ((error = mii_ifmedia_change(&sc->sc_mii)) != 0)
   2006 			goto out;
   2007 	}
   2008 
   2009 	/*
   2010 	 * ...all done!
   2011 	 */
   2012 	ifp->if_flags |= IFF_RUNNING;
   2013 	ifp->if_flags &= ~IFF_OACTIVE;
   2014 
   2015 	/*
   2016 	 * Request a software generated interrupt that will be used to
   2017 	 * (re)start the RU processing.  If we direct the chip to start
   2018 	 * receiving from the start of queue now, instead of letting the
   2019 	 * interrupt handler first process all received packets, we run
   2020 	 * the risk of having it overwrite mbuf clusters while they are
   2021 	 * being processed or after they have been returned to the pool.
   2022 	 */
   2023 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTRCNTL_REQUEST_SWI);
   2024 
   2025 	/*
   2026 	 * Start the one second timer.
   2027 	 */
   2028 	callout_reset(&sc->sc_callout, hz, fxp_tick, sc);
   2029 
   2030 	/*
   2031 	 * Attempt to start output on the interface.
   2032 	 */
   2033 	fxp_start(ifp);
   2034 
   2035  out:
   2036 	if (error) {
   2037 		ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   2038 		ifp->if_timer = 0;
   2039 		log(LOG_ERR, "%s: interface not running\n",
   2040 		    device_xname(sc->sc_dev));
   2041 	}
   2042 	return (error);
   2043 }
   2044 
   2045 /*
   2046  * Notify the world which media we're using.
   2047  */
   2048 void
   2049 fxp_mii_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
   2050 {
   2051 	struct fxp_softc *sc = ifp->if_softc;
   2052 
   2053 	if (sc->sc_enabled == 0) {
   2054 		ifmr->ifm_active = IFM_ETHER | IFM_NONE;
   2055 		ifmr->ifm_status = 0;
   2056 		return;
   2057 	}
   2058 
   2059 	ether_mediastatus(ifp, ifmr);
   2060 }
   2061 
   2062 int
   2063 fxp_80c24_mediachange(struct ifnet *ifp)
   2064 {
   2065 
   2066 	/* Nothing to do here. */
   2067 	return (0);
   2068 }
   2069 
   2070 void
   2071 fxp_80c24_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
   2072 {
   2073 	struct fxp_softc *sc = ifp->if_softc;
   2074 
   2075 	/*
   2076 	 * Media is currently-selected media.  We cannot determine
   2077 	 * the link status.
   2078 	 */
   2079 	ifmr->ifm_status = 0;
   2080 	ifmr->ifm_active = sc->sc_mii.mii_media.ifm_cur->ifm_media;
   2081 }
   2082 
   2083 /*
   2084  * Add a buffer to the end of the RFA buffer list.
   2085  * Return 0 if successful, error code on failure.
   2086  *
   2087  * The RFA struct is stuck at the beginning of mbuf cluster and the
   2088  * data pointer is fixed up to point just past it.
   2089  */
   2090 int
   2091 fxp_add_rfabuf(struct fxp_softc *sc, bus_dmamap_t rxmap, int unload)
   2092 {
   2093 	struct mbuf *m;
   2094 	int error;
   2095 
   2096 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   2097 	if (m == NULL)
   2098 		return (ENOBUFS);
   2099 
   2100 	MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner);
   2101 	MCLGET(m, M_DONTWAIT);
   2102 	if ((m->m_flags & M_EXT) == 0) {
   2103 		m_freem(m);
   2104 		return (ENOBUFS);
   2105 	}
   2106 
   2107 	if (unload)
   2108 		bus_dmamap_unload(sc->sc_dmat, rxmap);
   2109 
   2110 	M_SETCTX(m, rxmap);
   2111 
   2112 	m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
   2113 	error = bus_dmamap_load_mbuf(sc->sc_dmat, rxmap, m,
   2114 	    BUS_DMA_READ|BUS_DMA_NOWAIT);
   2115 	if (error) {
   2116 		/* XXX XXX XXX */
   2117 		aprint_error_dev(sc->sc_dev,
   2118 		    "can't load rx DMA map %d, error = %d\n",
   2119 		    sc->sc_rxq.ifq_len, error);
   2120 		panic("fxp_add_rfabuf");
   2121 	}
   2122 
   2123 	FXP_INIT_RFABUF(sc, m);
   2124 
   2125 	return (0);
   2126 }
   2127 
   2128 int
   2129 fxp_mdi_read(device_t self, int phy, int reg)
   2130 {
   2131 	struct fxp_softc *sc = device_private(self);
   2132 	int count = 10000;
   2133 	int value;
   2134 
   2135 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
   2136 	    (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
   2137 
   2138 	while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) &
   2139 	    0x10000000) == 0 && count--)
   2140 		DELAY(10);
   2141 
   2142 	if (count <= 0)
   2143 		log(LOG_WARNING,
   2144 		    "%s: fxp_mdi_read: timed out\n", device_xname(self));
   2145 
   2146 	return (value & 0xffff);
   2147 }
   2148 
   2149 void
   2150 fxp_statchg(device_t self)
   2151 {
   2152 
   2153 	/* Nothing to do. */
   2154 }
   2155 
   2156 void
   2157 fxp_mdi_write(device_t self, int phy, int reg, int value)
   2158 {
   2159 	struct fxp_softc *sc = device_private(self);
   2160 	int count = 10000;
   2161 
   2162 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
   2163 	    (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
   2164 	    (value & 0xffff));
   2165 
   2166 	while ((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
   2167 	    count--)
   2168 		DELAY(10);
   2169 
   2170 	if (count <= 0)
   2171 		log(LOG_WARNING,
   2172 		    "%s: fxp_mdi_write: timed out\n", device_xname(self));
   2173 }
   2174 
   2175 int
   2176 fxp_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   2177 {
   2178 	struct fxp_softc *sc = ifp->if_softc;
   2179 	struct ifreq *ifr = (struct ifreq *)data;
   2180 	int s, error;
   2181 
   2182 	s = splnet();
   2183 
   2184 	switch (cmd) {
   2185 	case SIOCSIFMEDIA:
   2186 	case SIOCGIFMEDIA:
   2187 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
   2188 		break;
   2189 
   2190 	default:
   2191 		if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET)
   2192 			break;
   2193 
   2194 		error = 0;
   2195 
   2196 		if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
   2197 			;
   2198 		else if (ifp->if_flags & IFF_RUNNING) {
   2199 			/*
   2200 			 * Multicast list has changed; set the
   2201 			 * hardware filter accordingly.
   2202 			 */
   2203 			while (sc->sc_txpending) {
   2204 				sc->sc_flags |= FXPF_WANTINIT;
   2205 				tsleep(sc, PSOCK, "fxp_init", 0);
   2206 			}
   2207 			error = fxp_init(ifp);
   2208 		}
   2209 		break;
   2210 	}
   2211 
   2212 	/* Try to get more packets going. */
   2213 	if (sc->sc_enabled)
   2214 		fxp_start(ifp);
   2215 
   2216 	splx(s);
   2217 	return (error);
   2218 }
   2219 
   2220 /*
   2221  * Program the multicast filter.
   2222  *
   2223  * This function must be called at splnet().
   2224  */
   2225 void
   2226 fxp_mc_setup(struct fxp_softc *sc)
   2227 {
   2228 	struct fxp_cb_mcs *mcsp = &sc->sc_control_data->fcd_mcscb;
   2229 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2230 	struct ethercom *ec = &sc->sc_ethercom;
   2231 	struct ether_multi *enm;
   2232 	struct ether_multistep step;
   2233 	int count, nmcasts;
   2234 	uint16_t status;
   2235 
   2236 #ifdef DIAGNOSTIC
   2237 	if (sc->sc_txpending)
   2238 		panic("fxp_mc_setup: pending transmissions");
   2239 #endif
   2240 
   2241 	ifp->if_flags &= ~IFF_ALLMULTI;
   2242 
   2243 	/*
   2244 	 * Initialize multicast setup descriptor.
   2245 	 */
   2246 	nmcasts = 0;
   2247 	ETHER_FIRST_MULTI(step, ec, enm);
   2248 	while (enm != NULL) {
   2249 		/*
   2250 		 * Check for too many multicast addresses or if we're
   2251 		 * listening to a range.  Either way, we simply have
   2252 		 * to accept all multicasts.
   2253 		 */
   2254 		if (nmcasts >= MAXMCADDR ||
   2255 		    memcmp(enm->enm_addrlo, enm->enm_addrhi,
   2256 		    ETHER_ADDR_LEN) != 0) {
   2257 			/*
   2258 			 * Callers of this function must do the
   2259 			 * right thing with this.  If we're called
   2260 			 * from outside fxp_init(), the caller must
   2261 			 * detect if the state if IFF_ALLMULTI changes.
   2262 			 * If it does, the caller must then call
   2263 			 * fxp_init(), since allmulti is handled by
   2264 			 * the config block.
   2265 			 */
   2266 			ifp->if_flags |= IFF_ALLMULTI;
   2267 			return;
   2268 		}
   2269 		memcpy(&mcsp->mc_addr[nmcasts][0], enm->enm_addrlo,
   2270 		    ETHER_ADDR_LEN);
   2271 		nmcasts++;
   2272 		ETHER_NEXT_MULTI(step, enm);
   2273 	}
   2274 
   2275 	/* BIG_ENDIAN: no need to swap to store 0 */
   2276 	mcsp->cb_status = 0;
   2277 	mcsp->cb_command = htole16(FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_EL);
   2278 	mcsp->link_addr = htole32(FXP_CDTXADDR(sc, FXP_NEXTTX(sc->sc_txlast)));
   2279 	mcsp->mc_cnt = htole16(nmcasts * ETHER_ADDR_LEN);
   2280 
   2281 	FXP_CDMCSSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   2282 
   2283 	/*
   2284 	 * Wait until the command unit is not active.  This should never
   2285 	 * happen since nothing is queued, but make sure anyway.
   2286 	 */
   2287 	count = 100;
   2288 	while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) ==
   2289 	    FXP_SCB_CUS_ACTIVE && --count)
   2290 		DELAY(1);
   2291 	if (count == 0) {
   2292 		log(LOG_WARNING, "%s: line %d: command queue timeout\n",
   2293 		    device_xname(sc->sc_dev), __LINE__);
   2294 		return;
   2295 	}
   2296 
   2297 	/*
   2298 	 * Start the multicast setup command/DMA.
   2299 	 */
   2300 	fxp_scb_wait(sc);
   2301 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->sc_cddma + FXP_CDMCSOFF);
   2302 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
   2303 
   2304 	/* ...and wait for it to complete. */
   2305 	for (count = 1000; count > 0; count--) {
   2306 		FXP_CDMCSSYNC(sc,
   2307 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   2308 		status = le16toh(mcsp->cb_status);
   2309 		FXP_CDMCSSYNC(sc, BUS_DMASYNC_PREREAD);
   2310 		if ((status & FXP_CB_STATUS_C) != 0)
   2311 			break;
   2312 		DELAY(1);
   2313 	}
   2314 	if (count == 0) {
   2315 		log(LOG_WARNING, "%s: line %d: dmasync timeout\n",
   2316 		    device_xname(sc->sc_dev), __LINE__);
   2317 		return;
   2318 	}
   2319 }
   2320 
   2321 static const uint32_t fxp_ucode_d101a[] = D101_A_RCVBUNDLE_UCODE;
   2322 static const uint32_t fxp_ucode_d101b0[] = D101_B0_RCVBUNDLE_UCODE;
   2323 static const uint32_t fxp_ucode_d101ma[] = D101M_B_RCVBUNDLE_UCODE;
   2324 static const uint32_t fxp_ucode_d101s[] = D101S_RCVBUNDLE_UCODE;
   2325 static const uint32_t fxp_ucode_d102[] = D102_B_RCVBUNDLE_UCODE;
   2326 static const uint32_t fxp_ucode_d102c[] = D102_C_RCVBUNDLE_UCODE;
   2327 
   2328 #define	UCODE(x)	x, sizeof(x)/sizeof(uint32_t)
   2329 
   2330 static const struct ucode {
   2331 	int32_t		revision;
   2332 	const uint32_t	*ucode;
   2333 	size_t		length;
   2334 	uint16_t	int_delay_offset;
   2335 	uint16_t	bundle_max_offset;
   2336 } ucode_table[] = {
   2337 	{ FXP_REV_82558_A4, UCODE(fxp_ucode_d101a),
   2338 	  D101_CPUSAVER_DWORD, 0 },
   2339 
   2340 	{ FXP_REV_82558_B0, UCODE(fxp_ucode_d101b0),
   2341 	  D101_CPUSAVER_DWORD, 0 },
   2342 
   2343 	{ FXP_REV_82559_A0, UCODE(fxp_ucode_d101ma),
   2344 	  D101M_CPUSAVER_DWORD, D101M_CPUSAVER_BUNDLE_MAX_DWORD },
   2345 
   2346 	{ FXP_REV_82559S_A, UCODE(fxp_ucode_d101s),
   2347 	  D101S_CPUSAVER_DWORD, D101S_CPUSAVER_BUNDLE_MAX_DWORD },
   2348 
   2349 	{ FXP_REV_82550, UCODE(fxp_ucode_d102),
   2350 	  D102_B_CPUSAVER_DWORD, D102_B_CPUSAVER_BUNDLE_MAX_DWORD },
   2351 
   2352 	{ FXP_REV_82550_C, UCODE(fxp_ucode_d102c),
   2353 	  D102_C_CPUSAVER_DWORD, D102_C_CPUSAVER_BUNDLE_MAX_DWORD },
   2354 
   2355 	{ 0, NULL, 0, 0, 0 }
   2356 };
   2357 
   2358 void
   2359 fxp_load_ucode(struct fxp_softc *sc)
   2360 {
   2361 	const struct ucode *uc;
   2362 	struct fxp_cb_ucode *cbp = &sc->sc_control_data->fcd_ucode;
   2363 	int count, i;
   2364 	uint16_t status;
   2365 
   2366 	if (sc->sc_flags & FXPF_UCODE_LOADED)
   2367 		return;
   2368 
   2369 	/*
   2370 	 * Only load the uCode if the user has requested that
   2371 	 * we do so.
   2372 	 */
   2373 	if ((sc->sc_ethercom.ec_if.if_flags & IFF_LINK0) == 0) {
   2374 		sc->sc_int_delay = 0;
   2375 		sc->sc_bundle_max = 0;
   2376 		return;
   2377 	}
   2378 
   2379 	for (uc = ucode_table; uc->ucode != NULL; uc++) {
   2380 		if (sc->sc_rev == uc->revision)
   2381 			break;
   2382 	}
   2383 	if (uc->ucode == NULL)
   2384 		return;
   2385 
   2386 	/* BIG ENDIAN: no need to swap to store 0 */
   2387 	cbp->cb_status = 0;
   2388 	cbp->cb_command = htole16(FXP_CB_COMMAND_UCODE | FXP_CB_COMMAND_EL);
   2389 	cbp->link_addr = 0xffffffff;		/* (no) next command */
   2390 	for (i = 0; i < uc->length; i++)
   2391 		cbp->ucode[i] = htole32(uc->ucode[i]);
   2392 
   2393 	if (uc->int_delay_offset)
   2394 		*(volatile uint16_t *) &cbp->ucode[uc->int_delay_offset] =
   2395 		    htole16(fxp_int_delay + (fxp_int_delay / 2));
   2396 
   2397 	if (uc->bundle_max_offset)
   2398 		*(volatile uint16_t *) &cbp->ucode[uc->bundle_max_offset] =
   2399 		    htole16(fxp_bundle_max);
   2400 
   2401 	FXP_CDUCODESYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   2402 
   2403 	/*
   2404 	 * Download the uCode to the chip.
   2405 	 */
   2406 	fxp_scb_wait(sc);
   2407 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->sc_cddma + FXP_CDUCODEOFF);
   2408 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
   2409 
   2410 	/* ...and wait for it to complete. */
   2411 	for (count = 10000; count > 0; count--) {
   2412 		FXP_CDUCODESYNC(sc,
   2413 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   2414 		status = le16toh(cbp->cb_status);
   2415 		FXP_CDUCODESYNC(sc, BUS_DMASYNC_PREREAD);
   2416 		if ((status & FXP_CB_STATUS_C) != 0)
   2417 			break;
   2418 		DELAY(2);
   2419 	}
   2420 	if (count == 0) {
   2421 		sc->sc_int_delay = 0;
   2422 		sc->sc_bundle_max = 0;
   2423 		log(LOG_WARNING, "%s: timeout loading microcode\n",
   2424 		    device_xname(sc->sc_dev));
   2425 		return;
   2426 	}
   2427 
   2428 	if (sc->sc_int_delay != fxp_int_delay ||
   2429 	    sc->sc_bundle_max != fxp_bundle_max) {
   2430 		sc->sc_int_delay = fxp_int_delay;
   2431 		sc->sc_bundle_max = fxp_bundle_max;
   2432 		log(LOG_INFO, "%s: Microcode loaded: int delay: %d usec, "
   2433 		    "max bundle: %d\n", device_xname(sc->sc_dev),
   2434 		    sc->sc_int_delay,
   2435 		    uc->bundle_max_offset == 0 ? 0 : sc->sc_bundle_max);
   2436 	}
   2437 
   2438 	sc->sc_flags |= FXPF_UCODE_LOADED;
   2439 }
   2440 
   2441 int
   2442 fxp_enable(struct fxp_softc *sc)
   2443 {
   2444 
   2445 	if (sc->sc_enabled == 0 && sc->sc_enable != NULL) {
   2446 		if ((*sc->sc_enable)(sc) != 0) {
   2447 			log(LOG_ERR, "%s: device enable failed\n",
   2448 			    device_xname(sc->sc_dev));
   2449 			return (EIO);
   2450 		}
   2451 	}
   2452 
   2453 	sc->sc_enabled = 1;
   2454 	return (0);
   2455 }
   2456 
   2457 void
   2458 fxp_disable(struct fxp_softc *sc)
   2459 {
   2460 
   2461 	if (sc->sc_enabled != 0 && sc->sc_disable != NULL) {
   2462 		(*sc->sc_disable)(sc);
   2463 		sc->sc_enabled = 0;
   2464 	}
   2465 }
   2466 
   2467 /*
   2468  * fxp_activate:
   2469  *
   2470  *	Handle device activation/deactivation requests.
   2471  */
   2472 int
   2473 fxp_activate(device_t self, enum devact act)
   2474 {
   2475 	struct fxp_softc *sc = device_private(self);
   2476 	int s, error = 0;
   2477 
   2478 	s = splnet();
   2479 	switch (act) {
   2480 	case DVACT_ACTIVATE:
   2481 		error = EOPNOTSUPP;
   2482 		break;
   2483 
   2484 	case DVACT_DEACTIVATE:
   2485 		if (sc->sc_flags & FXPF_MII)
   2486 			mii_activate(&sc->sc_mii, act, MII_PHY_ANY,
   2487 			    MII_OFFSET_ANY);
   2488 		if_deactivate(&sc->sc_ethercom.ec_if);
   2489 		break;
   2490 	}
   2491 	splx(s);
   2492 
   2493 	return (error);
   2494 }
   2495 
   2496 /*
   2497  * fxp_detach:
   2498  *
   2499  *	Detach an i82557 interface.
   2500  */
   2501 int
   2502 fxp_detach(struct fxp_softc *sc)
   2503 {
   2504 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2505 	int i;
   2506 
   2507 	/* Succeed now if there's no work to do. */
   2508 	if ((sc->sc_flags & FXPF_ATTACHED) == 0)
   2509 		return (0);
   2510 
   2511 	/* Unhook our tick handler. */
   2512 	callout_stop(&sc->sc_callout);
   2513 
   2514 	if (sc->sc_flags & FXPF_MII) {
   2515 		/* Detach all PHYs */
   2516 		mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
   2517 	}
   2518 
   2519 	/* Delete all remaining media. */
   2520 	ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
   2521 
   2522 #if NRND > 0
   2523 	rnd_detach_source(&sc->rnd_source);
   2524 #endif
   2525 	ether_ifdetach(ifp);
   2526 	if_detach(ifp);
   2527 
   2528 	for (i = 0; i < FXP_NRFABUFS; i++) {
   2529 		bus_dmamap_unload(sc->sc_dmat, sc->sc_rxmaps[i]);
   2530 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_rxmaps[i]);
   2531 	}
   2532 
   2533 	for (i = 0; i < FXP_NTXCB; i++) {
   2534 		bus_dmamap_unload(sc->sc_dmat, FXP_DSTX(sc, i)->txs_dmamap);
   2535 		bus_dmamap_destroy(sc->sc_dmat, FXP_DSTX(sc, i)->txs_dmamap);
   2536 	}
   2537 
   2538 	bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap);
   2539 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap);
   2540 	bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data,
   2541 	    sizeof(struct fxp_control_data));
   2542 	bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg);
   2543 
   2544 	return (0);
   2545 }
   2546