Home | History | Annotate | Line # | Download | only in pci
if_txp.c revision 1.65
      1 /* $NetBSD: if_txp.c,v 1.65 2020/03/08 19:02:03 thorpej Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2001
      5  *	Jason L. Wright <jason (at) thought.net>, Theo de Raadt, and
      6  *	Aaron Campbell <aaron (at) monkey.org>.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR THE VOICES IN THEIR HEADS
     21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     27  * THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 /*
     31  * Driver for 3c990 (Typhoon) Ethernet ASIC
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: if_txp.c,v 1.65 2020/03/08 19:02:03 thorpej Exp $");
     36 
     37 #include "opt_inet.h"
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/sockio.h>
     42 #include <sys/mbuf.h>
     43 #include <sys/malloc.h>
     44 #include <sys/kernel.h>
     45 #include <sys/socket.h>
     46 #include <sys/device.h>
     47 #include <sys/callout.h>
     48 #include <sys/bus.h>
     49 
     50 #include <net/if.h>
     51 #include <net/if_dl.h>
     52 #include <net/if_types.h>
     53 #include <net/if_ether.h>
     54 #include <net/if_arp.h>
     55 #include <net/if_media.h>
     56 #include <net/bpf.h>
     57 
     58 #ifdef INET
     59 #include <netinet/in.h>
     60 #include <netinet/in_systm.h>
     61 #include <netinet/in_var.h>
     62 #include <netinet/ip.h>
     63 #include <netinet/if_inarp.h>
     64 #endif
     65 
     66 #include <dev/mii/mii.h>
     67 #include <dev/mii/miivar.h>
     68 #include <dev/pci/pcireg.h>
     69 #include <dev/pci/pcivar.h>
     70 #include <dev/pci/pcidevs.h>
     71 
     72 #include <dev/pci/if_txpreg.h>
     73 
     74 #include <dev/microcode/typhoon/3c990img.h>
     75 
     76 /*
     77  * These currently break the 3c990 firmware, hopefully will be resolved
     78  * at some point.
     79  */
     80 #undef	TRY_TX_UDP_CSUM
     81 #undef	TRY_TX_TCP_CSUM
     82 
     83 static int txp_probe(device_t, cfdata_t, void *);
     84 static void txp_attach(device_t, device_t, void *);
     85 static int txp_intr(void *);
     86 static void txp_tick(void *);
     87 static bool txp_shutdown(device_t, int);
     88 static int txp_ioctl(struct ifnet *, u_long, void *);
     89 static void txp_start(struct ifnet *);
     90 static void txp_stop(struct txp_softc *);
     91 static void txp_init(struct txp_softc *);
     92 static void txp_watchdog(struct ifnet *);
     93 
     94 static int txp_chip_init(struct txp_softc *);
     95 static int txp_reset_adapter(struct txp_softc *);
     96 static int txp_download_fw(struct txp_softc *);
     97 static int txp_download_fw_wait(struct txp_softc *);
     98 static int txp_download_fw_section(struct txp_softc *,
     99     const struct txp_fw_section_header *, int);
    100 static int txp_alloc_rings(struct txp_softc *);
    101 static void txp_dma_free(struct txp_softc *, struct txp_dma_alloc *);
    102 static int txp_dma_malloc(struct txp_softc *, bus_size_t, struct txp_dma_alloc *, int);
    103 static void txp_set_filter(struct txp_softc *);
    104 
    105 static int txp_cmd_desc_numfree(struct txp_softc *);
    106 static int txp_command(struct txp_softc *, uint16_t, uint16_t, uint32_t,
    107     uint32_t, uint16_t *, uint32_t *, uint32_t *, int);
    108 static int txp_command2(struct txp_softc *, uint16_t, uint16_t,
    109     uint32_t, uint32_t, struct txp_ext_desc *, uint8_t,
    110     struct txp_rsp_desc **, int);
    111 static int txp_response(struct txp_softc *, uint32_t, uint16_t, uint16_t,
    112     struct txp_rsp_desc **);
    113 static void txp_rsp_fixup(struct txp_softc *, struct txp_rsp_desc *,
    114     struct txp_rsp_desc *);
    115 static void txp_capabilities(struct txp_softc *);
    116 
    117 static void txp_ifmedia_sts(struct ifnet *, struct ifmediareq *);
    118 static int txp_ifmedia_upd(struct ifnet *);
    119 static void txp_tx_reclaim(struct txp_softc *, struct txp_tx_ring *,
    120     struct txp_dma_alloc *);
    121 static void txp_rxbuf_reclaim(struct txp_softc *);
    122 static void txp_rx_reclaim(struct txp_softc *, struct txp_rx_ring *,
    123     struct txp_dma_alloc *);
    124 
    125 CFATTACH_DECL_NEW(txp, sizeof(struct txp_softc), txp_probe, txp_attach,
    126 	      NULL, NULL);
    127 
    128 static const struct txp_pci_match {
    129 	int vid, did, flags;
    130 } txp_devices[] = {
    131 	{ PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3CR990, 0 },
    132 	{ PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3CR990TX95, 0 },
    133 	{ PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3CR990TX97, 0 },
    134 	{ PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3CR990SVR95, TXP_SERVERVERSION },
    135 	{ PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3CR990SVR97, TXP_SERVERVERSION },
    136 	{ PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3C990B, TXP_USESUBSYSTEM },
    137 	{ PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3C990BSVR, TXP_SERVERVERSION },
    138 	{ PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3CR990FX, TXP_USESUBSYSTEM },
    139 };
    140 
    141 static const struct txp_pci_match *txp_pcilookup(pcireg_t);
    142 
    143 static const struct {
    144 	uint16_t mask, value;
    145 	int flags;
    146 } txp_subsysinfo[] = {
    147 	{0xf000, 0x2000, TXP_SERVERVERSION},
    148 	{0x0100, 0x0100, TXP_FIBER},
    149 #if 0 /* information from 3com header, unused */
    150 	{0x0010, 0x0010, /* secured firmware */},
    151 	{0x0003, 0x0000, /* variable DES */},
    152 	{0x0003, 0x0001, /* single DES - "95" */},
    153 	{0x0003, 0x0002, /* triple DES - "97" */},
    154 #endif
    155 };
    156 
    157 static const struct txp_pci_match *
    158 txp_pcilookup(pcireg_t id)
    159 {
    160 	int i;
    161 
    162 	for (i = 0; i < __arraycount(txp_devices); i++)
    163 		if (PCI_VENDOR(id) == txp_devices[i].vid &&
    164 		    PCI_PRODUCT(id) == txp_devices[i].did)
    165 			return &txp_devices[i];
    166 	return (0);
    167 }
    168 
    169 static int
    170 txp_probe(device_t parent, cfdata_t match, void *aux)
    171 {
    172 	struct pci_attach_args *pa = aux;
    173 
    174 	if (txp_pcilookup(pa->pa_id))
    175 			return (1);
    176 	return (0);
    177 }
    178 
    179 static void
    180 txp_attach(device_t parent, device_t self, void *aux)
    181 {
    182 	struct txp_softc *sc = device_private(self);
    183 	struct pci_attach_args *pa = aux;
    184 	pci_chipset_tag_t pc = pa->pa_pc;
    185 	pci_intr_handle_t ih;
    186 	const char *intrstr = NULL;
    187 	struct ifnet *ifp = &sc->sc_arpcom.ec_if;
    188 	uint32_t command;
    189 	uint16_t p1;
    190 	uint32_t p2;
    191 	u_char enaddr[6];
    192 	const struct txp_pci_match *match;
    193 	uint16_t subsys;
    194 	int i, flags;
    195 	char devinfo[256];
    196 	char intrbuf[PCI_INTRSTR_LEN];
    197 
    198 	sc->sc_dev = self;
    199 	sc->sc_cold = 1;
    200 
    201 	match = txp_pcilookup(pa->pa_id);
    202 	flags = match->flags;
    203 	if (match->flags & TXP_USESUBSYSTEM) {
    204 		subsys = PCI_PRODUCT(pci_conf_read(pc, pa->pa_tag,
    205 						   PCI_SUBSYS_ID_REG));
    206 		for (i = 0;
    207 		     i < sizeof(txp_subsysinfo)/sizeof(txp_subsysinfo[0]);
    208 		     i++)
    209 			if ((subsys & txp_subsysinfo[i].mask) ==
    210 			    txp_subsysinfo[i].value)
    211 				flags |= txp_subsysinfo[i].flags;
    212 	}
    213 	sc->sc_flags = flags;
    214 
    215 	aprint_naive("\n");
    216 	pci_devinfo(pa->pa_id, 0, 0, devinfo, sizeof(devinfo));
    217 #define TXP_EXTRAINFO ((flags & (TXP_USESUBSYSTEM | TXP_SERVERVERSION)) == \
    218   (TXP_USESUBSYSTEM | TXP_SERVERVERSION) ? " (SVR)" : "")
    219 	aprint_normal(": %s%s\n%s", devinfo, TXP_EXTRAINFO,
    220 	    device_xname(sc->sc_dev));
    221 
    222 	command = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    223 
    224 	if (!(command & PCI_COMMAND_MASTER_ENABLE)) {
    225 		aprint_error(": failed to enable bus mastering\n");
    226 		return;
    227 	}
    228 
    229 	if (!(command & PCI_COMMAND_MEM_ENABLE)) {
    230 		aprint_error(": failed to enable memory mapping\n");
    231 		return;
    232 	}
    233 	if (pci_mapreg_map(pa, TXP_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
    234 	    &sc->sc_bt, &sc->sc_bh, NULL, NULL)) {
    235 		aprint_error(": can't map mem space %d\n", 0);
    236 		return;
    237 	}
    238 
    239 	if (pci_dma64_available(pa))
    240 		sc->sc_dmat = pa->pa_dmat64;
    241 	else
    242 		sc->sc_dmat = pa->pa_dmat;
    243 
    244 	/*
    245 	 * Allocate our interrupt.
    246 	 */
    247 	if (pci_intr_map(pa, &ih)) {
    248 		aprint_error(": couldn't map interrupt\n");
    249 		return;
    250 	}
    251 
    252 	intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf));
    253 	sc->sc_ih = pci_intr_establish_xname(pc, ih, IPL_NET, txp_intr, sc,
    254 	    device_xname(self));
    255 	if (sc->sc_ih == NULL) {
    256 		aprint_error(": couldn't establish interrupt");
    257 		if (intrstr != NULL)
    258 			aprint_normal(" at %s", intrstr);
    259 		aprint_normal("\n");
    260 		return;
    261 	}
    262 	aprint_normal(": interrupting at %s\n", intrstr);
    263 
    264 	if (txp_chip_init(sc))
    265 		goto cleanupintr;
    266 
    267 	if (txp_download_fw(sc))
    268 		goto cleanupintr;
    269 
    270 	if (txp_alloc_rings(sc))
    271 		goto cleanupintr;
    272 
    273 	if (txp_command(sc, TXP_CMD_MAX_PKT_SIZE_WRITE, TXP_MAX_PKTLEN, 0, 0,
    274 	    NULL, NULL, NULL, 1))
    275 		goto cleanupintr;
    276 
    277 	if (txp_command(sc, TXP_CMD_STATION_ADDRESS_READ, 0, 0, 0,
    278 	    &p1, &p2, NULL, 1))
    279 		goto cleanupintr;
    280 
    281 	p1 = htole16(p1);
    282 	enaddr[0] = ((uint8_t *)&p1)[1];
    283 	enaddr[1] = ((uint8_t *)&p1)[0];
    284 	p2 = htole32(p2);
    285 	enaddr[2] = ((uint8_t *)&p2)[3];
    286 	enaddr[3] = ((uint8_t *)&p2)[2];
    287 	enaddr[4] = ((uint8_t *)&p2)[1];
    288 	enaddr[5] = ((uint8_t *)&p2)[0];
    289 
    290 	aprint_normal_dev(self, "Ethernet address %s\n",
    291 	    ether_sprintf(enaddr));
    292 	sc->sc_cold = 0;
    293 
    294 	/* Initialize ifmedia structures. */
    295 	sc->sc_arpcom.ec_ifmedia = &sc->sc_ifmedia;
    296 	ifmedia_init(&sc->sc_ifmedia, 0, txp_ifmedia_upd, txp_ifmedia_sts);
    297 	if (flags & TXP_FIBER) {
    298 		ifmedia_add(&sc->sc_ifmedia, IFM_ETHER | IFM_100_FX,
    299 			    0, NULL);
    300 		ifmedia_add(&sc->sc_ifmedia, IFM_ETHER | IFM_100_FX | IFM_HDX,
    301 			    0, NULL);
    302 		ifmedia_add(&sc->sc_ifmedia, IFM_ETHER | IFM_100_FX | IFM_FDX,
    303 			    0, NULL);
    304 	} else {
    305 		ifmedia_add(&sc->sc_ifmedia, IFM_ETHER | IFM_10_T,
    306 			    0, NULL);
    307 		ifmedia_add(&sc->sc_ifmedia, IFM_ETHER | IFM_10_T | IFM_HDX,
    308 			    0, NULL);
    309 		ifmedia_add(&sc->sc_ifmedia, IFM_ETHER | IFM_10_T | IFM_FDX,
    310 			    0, NULL);
    311 		ifmedia_add(&sc->sc_ifmedia, IFM_ETHER | IFM_100_TX,
    312 			    0, NULL);
    313 		ifmedia_add(&sc->sc_ifmedia, IFM_ETHER | IFM_100_TX | IFM_HDX,
    314 			    0, NULL);
    315 		ifmedia_add(&sc->sc_ifmedia, IFM_ETHER | IFM_100_TX | IFM_FDX,
    316 			    0, NULL);
    317 	}
    318 	ifmedia_add(&sc->sc_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
    319 
    320 	sc->sc_xcvr = TXP_XCVR_AUTO;
    321 	txp_command(sc, TXP_CMD_XCVR_SELECT, TXP_XCVR_AUTO, 0, 0,
    322 	    NULL, NULL, NULL, 0);
    323 	ifmedia_set(&sc->sc_ifmedia, IFM_ETHER | IFM_AUTO);
    324 
    325 	ifp->if_softc = sc;
    326 	ifp->if_mtu = ETHERMTU;
    327 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    328 	ifp->if_ioctl = txp_ioctl;
    329 	ifp->if_start = txp_start;
    330 	ifp->if_watchdog = txp_watchdog;
    331 	ifp->if_baudrate = 10000000;
    332 	IFQ_SET_MAXLEN(&ifp->if_snd, TX_ENTRIES);
    333 	IFQ_SET_READY(&ifp->if_snd);
    334 	ifp->if_capabilities = 0;
    335 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
    336 
    337 	txp_capabilities(sc);
    338 
    339 	callout_init(&sc->sc_tick, 0);
    340 	callout_setfunc(&sc->sc_tick, txp_tick, sc);
    341 
    342 	/*
    343 	 * Attach us everywhere
    344 	 */
    345 	if_attach(ifp);
    346 	if_deferred_start_init(ifp, NULL);
    347 	ether_ifattach(ifp, enaddr);
    348 
    349 	if (pmf_device_register1(self, NULL, NULL, txp_shutdown))
    350 		pmf_class_network_register(self, ifp);
    351 	else
    352 		aprint_error_dev(self, "couldn't establish power handler\n");
    353 
    354 	return;
    355 
    356 cleanupintr:
    357 	pci_intr_disestablish(pc, sc->sc_ih);
    358 
    359 	return;
    360 
    361 }
    362 
    363 static int
    364 txp_chip_init(struct txp_softc *sc)
    365 {
    366 	/* disable interrupts */
    367 	WRITE_REG(sc, TXP_IER, 0);
    368 	WRITE_REG(sc, TXP_IMR,
    369 	    TXP_INT_SELF | TXP_INT_PCI_TABORT | TXP_INT_PCI_MABORT |
    370 	    TXP_INT_DMA3 | TXP_INT_DMA2 | TXP_INT_DMA1 | TXP_INT_DMA0 |
    371 	    TXP_INT_LATCH);
    372 
    373 	/* ack all interrupts */
    374 	WRITE_REG(sc, TXP_ISR, TXP_INT_RESERVED | TXP_INT_LATCH |
    375 	    TXP_INT_A2H_7 | TXP_INT_A2H_6 | TXP_INT_A2H_5 | TXP_INT_A2H_4 |
    376 	    TXP_INT_SELF | TXP_INT_PCI_TABORT | TXP_INT_PCI_MABORT |
    377 	    TXP_INT_DMA3 | TXP_INT_DMA2 | TXP_INT_DMA1 | TXP_INT_DMA0 |
    378 	    TXP_INT_A2H_3 | TXP_INT_A2H_2 | TXP_INT_A2H_1 | TXP_INT_A2H_0);
    379 
    380 	if (txp_reset_adapter(sc))
    381 		return (-1);
    382 
    383 	/* disable interrupts */
    384 	WRITE_REG(sc, TXP_IER, 0);
    385 	WRITE_REG(sc, TXP_IMR,
    386 	    TXP_INT_SELF | TXP_INT_PCI_TABORT | TXP_INT_PCI_MABORT |
    387 	    TXP_INT_DMA3 | TXP_INT_DMA2 | TXP_INT_DMA1 | TXP_INT_DMA0 |
    388 	    TXP_INT_LATCH);
    389 
    390 	/* ack all interrupts */
    391 	WRITE_REG(sc, TXP_ISR, TXP_INT_RESERVED | TXP_INT_LATCH |
    392 	    TXP_INT_A2H_7 | TXP_INT_A2H_6 | TXP_INT_A2H_5 | TXP_INT_A2H_4 |
    393 	    TXP_INT_SELF | TXP_INT_PCI_TABORT | TXP_INT_PCI_MABORT |
    394 	    TXP_INT_DMA3 | TXP_INT_DMA2 | TXP_INT_DMA1 | TXP_INT_DMA0 |
    395 	    TXP_INT_A2H_3 | TXP_INT_A2H_2 | TXP_INT_A2H_1 | TXP_INT_A2H_0);
    396 
    397 	return (0);
    398 }
    399 
    400 static int
    401 txp_reset_adapter(struct txp_softc *sc)
    402 {
    403 	uint32_t r;
    404 	int i;
    405 
    406 	WRITE_REG(sc, TXP_SRR, TXP_SRR_ALL);
    407 	DELAY(1000);
    408 	WRITE_REG(sc, TXP_SRR, 0);
    409 
    410 	/* Should wait max 6 seconds */
    411 	for (i = 0; i < 6000; i++) {
    412 		r = READ_REG(sc, TXP_A2H_0);
    413 		if (r == STAT_WAITING_FOR_HOST_REQUEST)
    414 			break;
    415 		DELAY(1000);
    416 	}
    417 
    418 	if (r != STAT_WAITING_FOR_HOST_REQUEST) {
    419 		printf("%s: reset hung\n", TXP_DEVNAME(sc));
    420 		return (-1);
    421 	}
    422 
    423 	return (0);
    424 }
    425 
    426 static int
    427 txp_download_fw(struct txp_softc *sc)
    428 {
    429 	const struct txp_fw_file_header *fileheader;
    430 	const struct txp_fw_section_header *secthead;
    431 	int sect;
    432 	uint32_t r, i, ier, imr;
    433 
    434 	ier = READ_REG(sc, TXP_IER);
    435 	WRITE_REG(sc, TXP_IER, ier | TXP_INT_A2H_0);
    436 
    437 	imr = READ_REG(sc, TXP_IMR);
    438 	WRITE_REG(sc, TXP_IMR, imr | TXP_INT_A2H_0);
    439 
    440 	for (i = 0; i < 10000; i++) {
    441 		r = READ_REG(sc, TXP_A2H_0);
    442 		if (r == STAT_WAITING_FOR_HOST_REQUEST)
    443 			break;
    444 		DELAY(50);
    445 	}
    446 	if (r != STAT_WAITING_FOR_HOST_REQUEST) {
    447 		printf(": not waiting for host request\n");
    448 		return (-1);
    449 	}
    450 
    451 	/* Ack the status */
    452 	WRITE_REG(sc, TXP_ISR, TXP_INT_A2H_0);
    453 
    454 	fileheader = (const struct txp_fw_file_header *)tc990image;
    455 	if (memcmp("TYPHOON", fileheader->magicid,
    456 	    sizeof(fileheader->magicid))) {
    457 		printf(": fw invalid magic\n");
    458 		return (-1);
    459 	}
    460 
    461 	/* Tell boot firmware to get ready for image */
    462 	WRITE_REG(sc, TXP_H2A_1, le32toh(fileheader->addr));
    463 	WRITE_REG(sc, TXP_H2A_0, TXP_BOOTCMD_RUNTIME_IMAGE);
    464 
    465 	if (txp_download_fw_wait(sc)) {
    466 		printf("%s: fw wait failed, initial\n",
    467 		    device_xname(sc->sc_dev));
    468 		return (-1);
    469 	}
    470 
    471 	secthead = (const struct txp_fw_section_header *)
    472 		(((const uint8_t *)tc990image) +
    473 		 sizeof(struct txp_fw_file_header));
    474 
    475 	for (sect = 0; sect < le32toh(fileheader->nsections); sect++) {
    476 		if (txp_download_fw_section(sc, secthead, sect))
    477 			return (-1);
    478 		secthead = (const struct txp_fw_section_header *)
    479 		    (((const uint8_t *)secthead) + le32toh(secthead->nbytes) +
    480 			sizeof(*secthead));
    481 	}
    482 
    483 	WRITE_REG(sc, TXP_H2A_0, TXP_BOOTCMD_DOWNLOAD_COMPLETE);
    484 
    485 	for (i = 0; i < 10000; i++) {
    486 		r = READ_REG(sc, TXP_A2H_0);
    487 		if (r == STAT_WAITING_FOR_BOOT)
    488 			break;
    489 		DELAY(50);
    490 	}
    491 	if (r != STAT_WAITING_FOR_BOOT) {
    492 		printf(": not waiting for boot\n");
    493 		return (-1);
    494 	}
    495 
    496 	WRITE_REG(sc, TXP_IER, ier);
    497 	WRITE_REG(sc, TXP_IMR, imr);
    498 
    499 	return (0);
    500 }
    501 
    502 static int
    503 txp_download_fw_wait(struct txp_softc *sc)
    504 {
    505 	uint32_t i, r;
    506 
    507 	for (i = 0; i < 10000; i++) {
    508 		r = READ_REG(sc, TXP_ISR);
    509 		if (r & TXP_INT_A2H_0)
    510 			break;
    511 		DELAY(50);
    512 	}
    513 
    514 	if (!(r & TXP_INT_A2H_0)) {
    515 		printf(": fw wait failed comm0\n");
    516 		return (-1);
    517 	}
    518 
    519 	WRITE_REG(sc, TXP_ISR, TXP_INT_A2H_0);
    520 
    521 	r = READ_REG(sc, TXP_A2H_0);
    522 	if (r != STAT_WAITING_FOR_SEGMENT) {
    523 		printf(": fw not waiting for segment\n");
    524 		return (-1);
    525 	}
    526 	return (0);
    527 }
    528 
    529 static int
    530 txp_download_fw_section(struct txp_softc *sc,
    531     const struct txp_fw_section_header *sect, int sectnum)
    532 {
    533 	struct txp_dma_alloc dma;
    534 	int rseg, err = 0;
    535 	struct mbuf m;
    536 #ifdef INET
    537 	uint16_t csum;
    538 #endif
    539 
    540 	/* Skip zero length sections */
    541 	if (sect->nbytes == 0)
    542 		return (0);
    543 
    544 	/* Make sure we aren't past the end of the image */
    545 	rseg = ((const uint8_t *)sect) - ((const uint8_t *)tc990image);
    546 	if (rseg >= sizeof(tc990image)) {
    547 		printf(": fw invalid section address, section %d\n", sectnum);
    548 		return (-1);
    549 	}
    550 
    551 	/* Make sure this section doesn't go past the end */
    552 	rseg += le32toh(sect->nbytes);
    553 	if (rseg >= sizeof(tc990image)) {
    554 		printf(": fw truncated section %d\n", sectnum);
    555 		return (-1);
    556 	}
    557 
    558 	/* map a buffer, copy segment to it, get physaddr */
    559 	if (txp_dma_malloc(sc, le32toh(sect->nbytes), &dma, 0)) {
    560 		printf(": fw dma malloc failed, section %d\n", sectnum);
    561 		return (-1);
    562 	}
    563 
    564 	memcpy(dma.dma_vaddr, ((const uint8_t *)sect) + sizeof(*sect),
    565 	    le32toh(sect->nbytes));
    566 
    567 	/*
    568 	 * dummy up mbuf and verify section checksum
    569 	 */
    570 	m.m_type = MT_DATA;
    571 	m.m_next = m.m_nextpkt = NULL;
    572 	m.m_owner = NULL;
    573 	m.m_len = le32toh(sect->nbytes);
    574 	m.m_data = dma.dma_vaddr;
    575 	m.m_flags = 0;
    576 #ifdef INET
    577 	csum = in_cksum(&m, le32toh(sect->nbytes));
    578 	if (csum != sect->cksum) {
    579 		printf(": fw section %d, bad cksum (expected 0x%x got 0x%x)\n",
    580 		    sectnum, sect->cksum, csum);
    581 		txp_dma_free(sc, &dma);
    582 		return -1;
    583 	}
    584 #endif
    585 
    586 	bus_dmamap_sync(sc->sc_dmat, dma.dma_map, 0,
    587 	    dma.dma_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
    588 
    589 	WRITE_REG(sc, TXP_H2A_1, le32toh(sect->nbytes));
    590 	WRITE_REG(sc, TXP_H2A_2, le32toh(sect->cksum));
    591 	WRITE_REG(sc, TXP_H2A_3, le32toh(sect->addr));
    592 	WRITE_REG(sc, TXP_H2A_4, BUS_ADDR_HI32(dma.dma_paddr));
    593 	WRITE_REG(sc, TXP_H2A_5, BUS_ADDR_LO32(dma.dma_paddr));
    594 	WRITE_REG(sc, TXP_H2A_0, TXP_BOOTCMD_SEGMENT_AVAILABLE);
    595 
    596 	if (txp_download_fw_wait(sc)) {
    597 		printf("%s: fw wait failed, section %d\n",
    598 		    device_xname(sc->sc_dev), sectnum);
    599 		err = -1;
    600 	}
    601 
    602 	bus_dmamap_sync(sc->sc_dmat, dma.dma_map, 0,
    603 	    dma.dma_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
    604 
    605 	txp_dma_free(sc, &dma);
    606 	return (err);
    607 }
    608 
    609 static int
    610 txp_intr(void *vsc)
    611 {
    612 	struct txp_softc *sc = vsc;
    613 	struct txp_hostvar *hv = sc->sc_hostvar;
    614 	uint32_t isr;
    615 	int claimed = 0;
    616 
    617 	/* mask all interrupts */
    618 	WRITE_REG(sc, TXP_IMR, TXP_INT_RESERVED | TXP_INT_SELF |
    619 	    TXP_INT_A2H_7 | TXP_INT_A2H_6 | TXP_INT_A2H_5 | TXP_INT_A2H_4 |
    620 	    TXP_INT_A2H_2 | TXP_INT_A2H_1 | TXP_INT_A2H_0 |
    621 	    TXP_INT_DMA3 | TXP_INT_DMA2 | TXP_INT_DMA1 | TXP_INT_DMA0 |
    622 	    TXP_INT_PCI_TABORT | TXP_INT_PCI_MABORT |  TXP_INT_LATCH);
    623 
    624 	bus_dmamap_sync(sc->sc_dmat, sc->sc_host_dma.dma_map, 0,
    625 	    sizeof(struct txp_hostvar),
    626 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
    627 
    628 	isr = READ_REG(sc, TXP_ISR);
    629 	while (isr) {
    630 		claimed = 1;
    631 		WRITE_REG(sc, TXP_ISR, isr);
    632 
    633 		if ((*sc->sc_rxhir.r_roff) != (*sc->sc_rxhir.r_woff))
    634 			txp_rx_reclaim(sc, &sc->sc_rxhir, &sc->sc_rxhiring_dma);
    635 		if ((*sc->sc_rxlor.r_roff) != (*sc->sc_rxlor.r_woff))
    636 			txp_rx_reclaim(sc, &sc->sc_rxlor, &sc->sc_rxloring_dma);
    637 
    638 		if (hv->hv_rx_buf_write_idx == hv->hv_rx_buf_read_idx)
    639 			txp_rxbuf_reclaim(sc);
    640 
    641 		if (sc->sc_txhir.r_cnt && (sc->sc_txhir.r_cons !=
    642 		    TXP_OFFSET2IDX(le32toh(*(sc->sc_txhir.r_off)))))
    643 			txp_tx_reclaim(sc, &sc->sc_txhir, &sc->sc_txhiring_dma);
    644 
    645 		if (sc->sc_txlor.r_cnt && (sc->sc_txlor.r_cons !=
    646 		    TXP_OFFSET2IDX(le32toh(*(sc->sc_txlor.r_off)))))
    647 			txp_tx_reclaim(sc, &sc->sc_txlor, &sc->sc_txloring_dma);
    648 
    649 		isr = READ_REG(sc, TXP_ISR);
    650 	}
    651 
    652 	bus_dmamap_sync(sc->sc_dmat, sc->sc_host_dma.dma_map, 0,
    653 	    sizeof(struct txp_hostvar),
    654 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
    655 
    656 	/* unmask all interrupts */
    657 	WRITE_REG(sc, TXP_IMR, TXP_INT_A2H_3);
    658 
    659 	if_schedule_deferred_start(&sc->sc_arpcom.ec_if);
    660 
    661 	return (claimed);
    662 }
    663 
    664 static void
    665 txp_rx_reclaim(struct txp_softc *sc, struct txp_rx_ring *r,
    666     struct txp_dma_alloc *dma)
    667 {
    668 	struct ifnet *ifp = &sc->sc_arpcom.ec_if;
    669 	struct txp_rx_desc *rxd;
    670 	struct mbuf *m;
    671 	struct txp_swdesc *sd;
    672 	uint32_t roff, woff;
    673 	int sumflags = 0;
    674 	int idx;
    675 
    676 	roff = le32toh(*r->r_roff);
    677 	woff = le32toh(*r->r_woff);
    678 	idx = roff / sizeof(struct txp_rx_desc);
    679 	rxd = r->r_desc + idx;
    680 
    681 	while (roff != woff) {
    682 
    683 		bus_dmamap_sync(sc->sc_dmat, dma->dma_map,
    684 		    idx * sizeof(struct txp_rx_desc),
    685 		    sizeof(struct txp_rx_desc), BUS_DMASYNC_POSTREAD);
    686 
    687 		if (rxd->rx_flags & RX_FLAGS_ERROR) {
    688 			printf("%s: error 0x%x\n", device_xname(sc->sc_dev),
    689 			    le32toh(rxd->rx_stat));
    690 			if_statinc(ifp, if_ierrors);
    691 			goto next;
    692 		}
    693 
    694 		/* retrieve stashed pointer */
    695 		memcpy(&sd, __UNVOLATILE(&rxd->rx_vaddrlo), sizeof(sd));
    696 
    697 		bus_dmamap_sync(sc->sc_dmat, sd->sd_map, 0,
    698 		    sd->sd_map->dm_mapsize, BUS_DMASYNC_POSTREAD);
    699 		bus_dmamap_unload(sc->sc_dmat, sd->sd_map);
    700 		bus_dmamap_destroy(sc->sc_dmat, sd->sd_map);
    701 		m = sd->sd_mbuf;
    702 		free(sd, M_DEVBUF);
    703 		m->m_pkthdr.len = m->m_len = le16toh(rxd->rx_len);
    704 
    705 #ifdef __STRICT_ALIGNMENT
    706 		{
    707 			/*
    708 			 * XXX Nice chip, except it won't accept "off by 2"
    709 			 * buffers, so we're force to copy.  Supposedly
    710 			 * this will be fixed in a newer firmware rev
    711 			 * and this will be temporary.
    712 			 */
    713 			struct mbuf *mnew;
    714 
    715 			MGETHDR(mnew, M_DONTWAIT, MT_DATA);
    716 			if (mnew == NULL) {
    717 				m_freem(m);
    718 				goto next;
    719 			}
    720 			if (m->m_len > (MHLEN - 2)) {
    721 				MCLGET(mnew, M_DONTWAIT);
    722 				if (!(mnew->m_flags & M_EXT)) {
    723 					m_freem(mnew);
    724 					m_freem(m);
    725 					goto next;
    726 				}
    727 			}
    728 			m_set_rcvif(mnew, ifp);
    729 			mnew->m_pkthdr.len = mnew->m_len = m->m_len;
    730 			mnew->m_data += 2;
    731 			memcpy(mnew->m_data, m->m_data, m->m_len);
    732 			m_freem(m);
    733 			m = mnew;
    734 		}
    735 #endif
    736 
    737 		if (rxd->rx_stat & htole32(RX_STAT_IPCKSUMBAD))
    738 			sumflags |= (M_CSUM_IPv4 | M_CSUM_IPv4_BAD);
    739 		else if (rxd->rx_stat & htole32(RX_STAT_IPCKSUMGOOD))
    740 			sumflags |= M_CSUM_IPv4;
    741 
    742 		if (rxd->rx_stat & htole32(RX_STAT_TCPCKSUMBAD))
    743 			sumflags |= (M_CSUM_TCPv4 | M_CSUM_TCP_UDP_BAD);
    744 		else if (rxd->rx_stat & htole32(RX_STAT_TCPCKSUMGOOD))
    745 			sumflags |= M_CSUM_TCPv4;
    746 
    747 		if (rxd->rx_stat & htole32(RX_STAT_UDPCKSUMBAD))
    748 			sumflags |= (M_CSUM_UDPv4 | M_CSUM_TCP_UDP_BAD);
    749 		else if (rxd->rx_stat & htole32(RX_STAT_UDPCKSUMGOOD))
    750 			sumflags |= M_CSUM_UDPv4;
    751 
    752 		m->m_pkthdr.csum_flags = sumflags;
    753 
    754 		if (rxd->rx_stat & htole32(RX_STAT_VLAN)) {
    755 			vlan_set_tag(m, htons(rxd->rx_vlan >> 16));
    756 		}
    757 
    758 		if_percpuq_enqueue(ifp->if_percpuq, m);
    759 
    760 next:
    761 		bus_dmamap_sync(sc->sc_dmat, dma->dma_map,
    762 		    idx * sizeof(struct txp_rx_desc),
    763 		    sizeof(struct txp_rx_desc), BUS_DMASYNC_PREREAD);
    764 
    765 		roff += sizeof(struct txp_rx_desc);
    766 		if (roff == (RX_ENTRIES * sizeof(struct txp_rx_desc))) {
    767 			idx = 0;
    768 			roff = 0;
    769 			rxd = r->r_desc;
    770 		} else {
    771 			idx++;
    772 			rxd++;
    773 		}
    774 		woff = le32toh(*r->r_woff);
    775 	}
    776 
    777 	*r->r_roff = htole32(woff);
    778 }
    779 
    780 static void
    781 txp_rxbuf_reclaim(struct txp_softc *sc)
    782 {
    783 	struct ifnet *ifp = &sc->sc_arpcom.ec_if;
    784 	struct txp_hostvar *hv = sc->sc_hostvar;
    785 	struct txp_rxbuf_desc *rbd;
    786 	struct txp_swdesc *sd;
    787 	uint32_t i, end;
    788 
    789 	end = TXP_OFFSET2IDX(le32toh(hv->hv_rx_buf_read_idx));
    790 	i = TXP_OFFSET2IDX(le32toh(hv->hv_rx_buf_write_idx));
    791 
    792 	if (++i == RXBUF_ENTRIES)
    793 		i = 0;
    794 
    795 	rbd = sc->sc_rxbufs + i;
    796 
    797 	while (i != end) {
    798 		sd = (struct txp_swdesc *)malloc(sizeof(struct txp_swdesc),
    799 		    M_DEVBUF, M_NOWAIT);
    800 		if (sd == NULL)
    801 			break;
    802 
    803 		MGETHDR(sd->sd_mbuf, M_DONTWAIT, MT_DATA);
    804 		if (sd->sd_mbuf == NULL)
    805 			goto err_sd;
    806 
    807 		MCLGET(sd->sd_mbuf, M_DONTWAIT);
    808 		if ((sd->sd_mbuf->m_flags & M_EXT) == 0)
    809 			goto err_mbuf;
    810 		m_set_rcvif(sd->sd_mbuf, ifp);
    811 		sd->sd_mbuf->m_pkthdr.len = sd->sd_mbuf->m_len = MCLBYTES;
    812 		if (bus_dmamap_create(sc->sc_dmat, TXP_MAX_PKTLEN, 1,
    813 		    TXP_MAX_PKTLEN, 0, BUS_DMA_NOWAIT, &sd->sd_map))
    814 			goto err_mbuf;
    815 		if (bus_dmamap_load_mbuf(sc->sc_dmat, sd->sd_map, sd->sd_mbuf,
    816 		    BUS_DMA_NOWAIT)) {
    817 			bus_dmamap_destroy(sc->sc_dmat, sd->sd_map);
    818 			goto err_mbuf;
    819 		}
    820 
    821 		bus_dmamap_sync(sc->sc_dmat, sc->sc_rxbufring_dma.dma_map,
    822 		    i * sizeof(struct txp_rxbuf_desc),
    823 		    sizeof(struct txp_rxbuf_desc), BUS_DMASYNC_POSTWRITE);
    824 
    825 		/* stash away pointer */
    826 		memcpy(__UNVOLATILE(&rbd->rb_vaddrlo), &sd, sizeof(sd));
    827 
    828 		rbd->rb_paddrlo =
    829 		    htole32(BUS_ADDR_LO32(sd->sd_map->dm_segs[0].ds_addr));
    830 		rbd->rb_paddrhi =
    831 		    htole32(BUS_ADDR_HI32(sd->sd_map->dm_segs[0].ds_addr));
    832 
    833 		bus_dmamap_sync(sc->sc_dmat, sd->sd_map, 0,
    834 		    sd->sd_map->dm_mapsize, BUS_DMASYNC_PREREAD);
    835 
    836 		bus_dmamap_sync(sc->sc_dmat, sc->sc_rxbufring_dma.dma_map,
    837 		    i * sizeof(struct txp_rxbuf_desc),
    838 		    sizeof(struct txp_rxbuf_desc), BUS_DMASYNC_PREWRITE);
    839 
    840 		hv->hv_rx_buf_write_idx = htole32(TXP_IDX2OFFSET(i));
    841 
    842 		if (++i == RXBUF_ENTRIES) {
    843 			i = 0;
    844 			rbd = sc->sc_rxbufs;
    845 		} else
    846 			rbd++;
    847 	}
    848 	return;
    849 
    850 err_mbuf:
    851 	m_freem(sd->sd_mbuf);
    852 err_sd:
    853 	free(sd, M_DEVBUF);
    854 }
    855 
    856 /*
    857  * Reclaim mbufs and entries from a transmit ring.
    858  */
    859 static void
    860 txp_tx_reclaim(struct txp_softc *sc, struct txp_tx_ring *r,
    861     struct txp_dma_alloc *dma)
    862 {
    863 	struct ifnet *ifp = &sc->sc_arpcom.ec_if;
    864 	uint32_t idx = TXP_OFFSET2IDX(le32toh(*(r->r_off)));
    865 	uint32_t cons = r->r_cons, cnt = r->r_cnt;
    866 	struct txp_tx_desc *txd = r->r_desc + cons;
    867 	struct txp_swdesc *sd = sc->sc_txd + cons;
    868 	struct mbuf *m;
    869 
    870 	while (cons != idx) {
    871 		if (cnt == 0)
    872 			break;
    873 
    874 		bus_dmamap_sync(sc->sc_dmat, dma->dma_map,
    875 		    cons * sizeof(struct txp_tx_desc),
    876 		    sizeof(struct txp_tx_desc),
    877 		    BUS_DMASYNC_POSTWRITE);
    878 
    879 		if ((txd->tx_flags & TX_FLAGS_TYPE_M) ==
    880 		    TX_FLAGS_TYPE_DATA) {
    881 			bus_dmamap_sync(sc->sc_dmat, sd->sd_map, 0,
    882 			    sd->sd_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
    883 			bus_dmamap_unload(sc->sc_dmat, sd->sd_map);
    884 			m = sd->sd_mbuf;
    885 			if (m != NULL) {
    886 				m_freem(m);
    887 				txd->tx_addrlo = 0;
    888 				txd->tx_addrhi = 0;
    889 				if_statinc(ifp, if_opackets);
    890 			}
    891 		}
    892 		ifp->if_flags &= ~IFF_OACTIVE;
    893 
    894 		if (++cons == TX_ENTRIES) {
    895 			txd = r->r_desc;
    896 			cons = 0;
    897 			sd = sc->sc_txd;
    898 		} else {
    899 			txd++;
    900 			sd++;
    901 		}
    902 
    903 		cnt--;
    904 	}
    905 
    906 	r->r_cons = cons;
    907 	r->r_cnt = cnt;
    908 	if (cnt == 0)
    909 		ifp->if_timer = 0;
    910 }
    911 
    912 static bool
    913 txp_shutdown(device_t self, int howto)
    914 {
    915 	struct txp_softc *sc;
    916 
    917 	sc = device_private(self);
    918 
    919 	/* mask all interrupts */
    920 	WRITE_REG(sc, TXP_IMR,
    921 	    TXP_INT_SELF | TXP_INT_PCI_TABORT | TXP_INT_PCI_MABORT |
    922 	    TXP_INT_DMA3 | TXP_INT_DMA2 | TXP_INT_DMA1 | TXP_INT_DMA0 |
    923 	    TXP_INT_LATCH);
    924 
    925 	txp_command(sc, TXP_CMD_TX_DISABLE, 0, 0, 0, NULL, NULL, NULL, 0);
    926 	txp_command(sc, TXP_CMD_RX_DISABLE, 0, 0, 0, NULL, NULL, NULL, 0);
    927 	txp_command(sc, TXP_CMD_HALT, 0, 0, 0, NULL, NULL, NULL, 0);
    928 
    929 	return true;
    930 }
    931 
    932 static int
    933 txp_alloc_rings(struct txp_softc *sc)
    934 {
    935 	struct ifnet *ifp = &sc->sc_arpcom.ec_if;
    936 	struct txp_boot_record *boot;
    937 	struct txp_swdesc *sd;
    938 	uint32_t r;
    939 	int i, j, nb;
    940 
    941 	/* boot record */
    942 	if (txp_dma_malloc(sc, sizeof(struct txp_boot_record),
    943 	    &sc->sc_boot_dma, BUS_DMA_COHERENT)) {
    944 		printf(": can't allocate boot record\n");
    945 		return (-1);
    946 	}
    947 	boot = (struct txp_boot_record *)sc->sc_boot_dma.dma_vaddr;
    948 	memset(boot, 0, sizeof(*boot));
    949 	sc->sc_boot = boot;
    950 
    951 	/* host variables */
    952 	if (txp_dma_malloc(sc, sizeof(struct txp_hostvar), &sc->sc_host_dma,
    953 	    BUS_DMA_COHERENT)) {
    954 		printf(": can't allocate host ring\n");
    955 		goto bail_boot;
    956 	}
    957 	memset(sc->sc_host_dma.dma_vaddr, 0, sizeof(struct txp_hostvar));
    958 	boot->br_hostvar_lo = htole32(BUS_ADDR_LO32(sc->sc_host_dma.dma_paddr));
    959 	boot->br_hostvar_hi = htole32(BUS_ADDR_HI32(sc->sc_host_dma.dma_paddr));
    960 	sc->sc_hostvar = (struct txp_hostvar *)sc->sc_host_dma.dma_vaddr;
    961 
    962 	/* high priority tx ring */
    963 	if (txp_dma_malloc(sc, sizeof(struct txp_tx_desc) * TX_ENTRIES,
    964 	    &sc->sc_txhiring_dma, BUS_DMA_COHERENT)) {
    965 		printf(": can't allocate high tx ring\n");
    966 		goto bail_host;
    967 	}
    968 	memset(sc->sc_txhiring_dma.dma_vaddr, 0,
    969 	    sizeof(struct txp_tx_desc) * TX_ENTRIES);
    970 	boot->br_txhipri_lo =
    971 	    htole32(BUS_ADDR_LO32(sc->sc_txhiring_dma.dma_paddr));
    972 	boot->br_txhipri_hi =
    973 	    htole32(BUS_ADDR_HI32(sc->sc_txhiring_dma.dma_paddr));
    974 	boot->br_txhipri_siz = htole32(TX_ENTRIES * sizeof(struct txp_tx_desc));
    975 	sc->sc_txhir.r_reg = TXP_H2A_1;
    976 	sc->sc_txhir.r_desc =
    977 	    (struct txp_tx_desc *)sc->sc_txhiring_dma.dma_vaddr;
    978 	sc->sc_txhir.r_cons = sc->sc_txhir.r_prod = sc->sc_txhir.r_cnt = 0;
    979 	sc->sc_txhir.r_off = &sc->sc_hostvar->hv_tx_hi_desc_read_idx;
    980 	for (i = 0; i < TX_ENTRIES; i++) {
    981 		if (bus_dmamap_create(sc->sc_dmat, TXP_MAX_PKTLEN,
    982 		    TX_ENTRIES - 4, TXP_MAX_SEGLEN, 0,
    983 		    BUS_DMA_NOWAIT, &sc->sc_txd[i].sd_map) != 0) {
    984 			for (j = 0; j < i; j++) {
    985 				bus_dmamap_destroy(sc->sc_dmat,
    986 				    sc->sc_txd[j].sd_map);
    987 				sc->sc_txd[j].sd_map = NULL;
    988 			}
    989 			goto bail_txhiring;
    990 		}
    991 	}
    992 
    993 	/* low priority tx ring */
    994 	if (txp_dma_malloc(sc, sizeof(struct txp_tx_desc) * TX_ENTRIES,
    995 	    &sc->sc_txloring_dma, BUS_DMA_COHERENT)) {
    996 		printf(": can't allocate low tx ring\n");
    997 		goto bail_txhiring;
    998 	}
    999 	memset(sc->sc_txloring_dma.dma_vaddr, 0,
   1000 	    sizeof(struct txp_tx_desc) * TX_ENTRIES);
   1001 	boot->br_txlopri_lo =
   1002 	    htole32(BUS_ADDR_LO32(sc->sc_txloring_dma.dma_paddr));
   1003 	boot->br_txlopri_hi =
   1004 	    htole32(BUS_ADDR_HI32(sc->sc_txloring_dma.dma_paddr));
   1005 	boot->br_txlopri_siz = htole32(TX_ENTRIES * sizeof(struct txp_tx_desc));
   1006 	sc->sc_txlor.r_reg = TXP_H2A_3;
   1007 	sc->sc_txlor.r_desc =
   1008 	    (struct txp_tx_desc *)sc->sc_txloring_dma.dma_vaddr;
   1009 	sc->sc_txlor.r_cons = sc->sc_txlor.r_prod = sc->sc_txlor.r_cnt = 0;
   1010 	sc->sc_txlor.r_off = &sc->sc_hostvar->hv_tx_lo_desc_read_idx;
   1011 
   1012 	/* high priority rx ring */
   1013 	if (txp_dma_malloc(sc, sizeof(struct txp_rx_desc) * RX_ENTRIES,
   1014 	    &sc->sc_rxhiring_dma, BUS_DMA_COHERENT)) {
   1015 		printf(": can't allocate high rx ring\n");
   1016 		goto bail_txloring;
   1017 	}
   1018 	memset(sc->sc_rxhiring_dma.dma_vaddr, 0,
   1019 	    sizeof(struct txp_rx_desc) * RX_ENTRIES);
   1020 	boot->br_rxhipri_lo =
   1021 	    htole32(BUS_ADDR_LO32(sc->sc_rxhiring_dma.dma_paddr));
   1022 	boot->br_rxhipri_hi =
   1023 	    htole32(BUS_ADDR_HI32(sc->sc_rxhiring_dma.dma_paddr));
   1024 	boot->br_rxhipri_siz = htole32(RX_ENTRIES * sizeof(struct txp_rx_desc));
   1025 	sc->sc_rxhir.r_desc =
   1026 	    (struct txp_rx_desc *)sc->sc_rxhiring_dma.dma_vaddr;
   1027 	sc->sc_rxhir.r_roff = &sc->sc_hostvar->hv_rx_hi_read_idx;
   1028 	sc->sc_rxhir.r_woff = &sc->sc_hostvar->hv_rx_hi_write_idx;
   1029 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rxhiring_dma.dma_map,
   1030 	    0, sc->sc_rxhiring_dma.dma_map->dm_mapsize, BUS_DMASYNC_PREREAD);
   1031 
   1032 	/* low priority ring */
   1033 	if (txp_dma_malloc(sc, sizeof(struct txp_rx_desc) * RX_ENTRIES,
   1034 	    &sc->sc_rxloring_dma, BUS_DMA_COHERENT)) {
   1035 		printf(": can't allocate low rx ring\n");
   1036 		goto bail_rxhiring;
   1037 	}
   1038 	memset(sc->sc_rxloring_dma.dma_vaddr, 0,
   1039 	    sizeof(struct txp_rx_desc) * RX_ENTRIES);
   1040 	boot->br_rxlopri_lo =
   1041 	    htole32(BUS_ADDR_LO32(sc->sc_rxloring_dma.dma_paddr));
   1042 	boot->br_rxlopri_hi =
   1043 	    htole32(BUS_ADDR_HI32(sc->sc_rxloring_dma.dma_paddr));
   1044 	boot->br_rxlopri_siz = htole32(RX_ENTRIES * sizeof(struct txp_rx_desc));
   1045 	sc->sc_rxlor.r_desc =
   1046 	    (struct txp_rx_desc *)sc->sc_rxloring_dma.dma_vaddr;
   1047 	sc->sc_rxlor.r_roff = &sc->sc_hostvar->hv_rx_lo_read_idx;
   1048 	sc->sc_rxlor.r_woff = &sc->sc_hostvar->hv_rx_lo_write_idx;
   1049 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rxloring_dma.dma_map,
   1050 	    0, sc->sc_rxloring_dma.dma_map->dm_mapsize, BUS_DMASYNC_PREREAD);
   1051 
   1052 	/* command ring */
   1053 	if (txp_dma_malloc(sc, sizeof(struct txp_cmd_desc) * CMD_ENTRIES,
   1054 	    &sc->sc_cmdring_dma, BUS_DMA_COHERENT)) {
   1055 		printf(": can't allocate command ring\n");
   1056 		goto bail_rxloring;
   1057 	}
   1058 	memset(sc->sc_cmdring_dma.dma_vaddr, 0,
   1059 	    sizeof(struct txp_cmd_desc) * CMD_ENTRIES);
   1060 	boot->br_cmd_lo = htole32(BUS_ADDR_LO32(sc->sc_cmdring_dma.dma_paddr));
   1061 	boot->br_cmd_hi = htole32(BUS_ADDR_HI32(sc->sc_cmdring_dma.dma_paddr));
   1062 	boot->br_cmd_siz = htole32(CMD_ENTRIES * sizeof(struct txp_cmd_desc));
   1063 	sc->sc_cmdring.base = (struct txp_cmd_desc *)sc->sc_cmdring_dma.dma_vaddr;
   1064 	sc->sc_cmdring.size = CMD_ENTRIES * sizeof(struct txp_cmd_desc);
   1065 	sc->sc_cmdring.lastwrite = 0;
   1066 
   1067 	/* response ring */
   1068 	if (txp_dma_malloc(sc, sizeof(struct txp_rsp_desc) * RSP_ENTRIES,
   1069 	    &sc->sc_rspring_dma, BUS_DMA_COHERENT)) {
   1070 		printf(": can't allocate response ring\n");
   1071 		goto bail_cmdring;
   1072 	}
   1073 	memset(sc->sc_rspring_dma.dma_vaddr, 0,
   1074 	    sizeof(struct txp_rsp_desc) * RSP_ENTRIES);
   1075 	boot->br_resp_lo = htole32(BUS_ADDR_LO32(sc->sc_rspring_dma.dma_paddr));
   1076 	boot->br_resp_hi = htole32(BUS_ADDR_HI32(sc->sc_rspring_dma.dma_paddr));
   1077 	boot->br_resp_siz = htole32(CMD_ENTRIES * sizeof(struct txp_rsp_desc));
   1078 	sc->sc_rspring.base = (struct txp_rsp_desc *)sc->sc_rspring_dma.dma_vaddr;
   1079 	sc->sc_rspring.size = RSP_ENTRIES * sizeof(struct txp_rsp_desc);
   1080 	sc->sc_rspring.lastwrite = 0;
   1081 
   1082 	/* receive buffer ring */
   1083 	if (txp_dma_malloc(sc, sizeof(struct txp_rxbuf_desc) * RXBUF_ENTRIES,
   1084 	    &sc->sc_rxbufring_dma, BUS_DMA_COHERENT)) {
   1085 		printf(": can't allocate rx buffer ring\n");
   1086 		goto bail_rspring;
   1087 	}
   1088 	memset(sc->sc_rxbufring_dma.dma_vaddr, 0,
   1089 	    sizeof(struct txp_rxbuf_desc) * RXBUF_ENTRIES);
   1090 	boot->br_rxbuf_lo = htole32(BUS_ADDR_LO32(sc->sc_rxbufring_dma.dma_paddr));
   1091 	boot->br_rxbuf_hi = htole32(BUS_ADDR_HI32(sc->sc_rxbufring_dma.dma_paddr));
   1092 	boot->br_rxbuf_siz = htole32(RXBUF_ENTRIES * sizeof(struct txp_rxbuf_desc));
   1093 	sc->sc_rxbufs = (struct txp_rxbuf_desc *)sc->sc_rxbufring_dma.dma_vaddr;
   1094 	for (nb = 0; nb < RXBUF_ENTRIES; nb++) {
   1095 		sd = malloc(sizeof(struct txp_swdesc), M_DEVBUF, M_WAITOK);
   1096 		/* stash away pointer */
   1097 		memcpy(__UNVOLATILE(&sc->sc_rxbufs[nb].rb_vaddrlo), &sd,
   1098 		    sizeof(sd));
   1099 
   1100 		MGETHDR(sd->sd_mbuf, M_DONTWAIT, MT_DATA);
   1101 		if (sd->sd_mbuf == NULL) {
   1102 			goto bail_rxbufring;
   1103 		}
   1104 
   1105 		MCLGET(sd->sd_mbuf, M_DONTWAIT);
   1106 		if ((sd->sd_mbuf->m_flags & M_EXT) == 0) {
   1107 			goto bail_rxbufring;
   1108 		}
   1109 		sd->sd_mbuf->m_pkthdr.len = sd->sd_mbuf->m_len = MCLBYTES;
   1110 		m_set_rcvif(sd->sd_mbuf, ifp);
   1111 		if (bus_dmamap_create(sc->sc_dmat, TXP_MAX_PKTLEN, 1,
   1112 		    TXP_MAX_PKTLEN, 0, BUS_DMA_NOWAIT, &sd->sd_map)) {
   1113 			goto bail_rxbufring;
   1114 		}
   1115 		if (bus_dmamap_load_mbuf(sc->sc_dmat, sd->sd_map, sd->sd_mbuf,
   1116 		    BUS_DMA_NOWAIT)) {
   1117 			bus_dmamap_destroy(sc->sc_dmat, sd->sd_map);
   1118 			goto bail_rxbufring;
   1119 		}
   1120 		bus_dmamap_sync(sc->sc_dmat, sd->sd_map, 0,
   1121 		    sd->sd_map->dm_mapsize, BUS_DMASYNC_PREREAD);
   1122 
   1123 
   1124 		sc->sc_rxbufs[nb].rb_paddrlo =
   1125 		    htole32(BUS_ADDR_LO32(sd->sd_map->dm_segs[0].ds_addr));
   1126 		sc->sc_rxbufs[nb].rb_paddrhi =
   1127 		    htole32(BUS_ADDR_HI32(sd->sd_map->dm_segs[0].ds_addr));
   1128 	}
   1129 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rxbufring_dma.dma_map,
   1130 	    0, sc->sc_rxbufring_dma.dma_map->dm_mapsize,
   1131 	    BUS_DMASYNC_PREWRITE);
   1132 	sc->sc_hostvar->hv_rx_buf_write_idx = htole32((RXBUF_ENTRIES - 1) *
   1133 	    sizeof(struct txp_rxbuf_desc));
   1134 
   1135 	/* zero dma */
   1136 	if (txp_dma_malloc(sc, sizeof(uint32_t), &sc->sc_zero_dma,
   1137 	    BUS_DMA_COHERENT)) {
   1138 		printf(": can't allocate response ring\n");
   1139 		goto bail_rxbufring;
   1140 	}
   1141 	memset(sc->sc_zero_dma.dma_vaddr, 0, sizeof(uint32_t));
   1142 	boot->br_zero_lo = htole32(BUS_ADDR_LO32(sc->sc_zero_dma.dma_paddr));
   1143 	boot->br_zero_hi = htole32(BUS_ADDR_HI32(sc->sc_zero_dma.dma_paddr));
   1144 
   1145 	/* See if it's waiting for boot, and try to boot it */
   1146 	for (i = 0; i < 10000; i++) {
   1147 		r = READ_REG(sc, TXP_A2H_0);
   1148 		if (r == STAT_WAITING_FOR_BOOT)
   1149 			break;
   1150 		DELAY(50);
   1151 	}
   1152 	if (r != STAT_WAITING_FOR_BOOT) {
   1153 		printf(": not waiting for boot\n");
   1154 		goto bail;
   1155 	}
   1156 	WRITE_REG(sc, TXP_H2A_2, BUS_ADDR_HI32(sc->sc_boot_dma.dma_paddr));
   1157 	WRITE_REG(sc, TXP_H2A_1, BUS_ADDR_LO32(sc->sc_boot_dma.dma_paddr));
   1158 	WRITE_REG(sc, TXP_H2A_0, TXP_BOOTCMD_REGISTER_BOOT_RECORD);
   1159 
   1160 	/* See if it booted */
   1161 	for (i = 0; i < 10000; i++) {
   1162 		r = READ_REG(sc, TXP_A2H_0);
   1163 		if (r == STAT_RUNNING)
   1164 			break;
   1165 		DELAY(50);
   1166 	}
   1167 	if (r != STAT_RUNNING) {
   1168 		printf(": fw not running\n");
   1169 		goto bail;
   1170 	}
   1171 
   1172 	/* Clear TX and CMD ring write registers */
   1173 	WRITE_REG(sc, TXP_H2A_1, TXP_BOOTCMD_NULL);
   1174 	WRITE_REG(sc, TXP_H2A_2, TXP_BOOTCMD_NULL);
   1175 	WRITE_REG(sc, TXP_H2A_3, TXP_BOOTCMD_NULL);
   1176 	WRITE_REG(sc, TXP_H2A_0, TXP_BOOTCMD_NULL);
   1177 
   1178 	return (0);
   1179 
   1180 bail:
   1181 	txp_dma_free(sc, &sc->sc_zero_dma);
   1182 bail_rxbufring:
   1183 	if (nb == RXBUF_ENTRIES)
   1184 		nb--;
   1185 	for (i = 0; i <= nb; i++) {
   1186 		memcpy(&sd, __UNVOLATILE(&sc->sc_rxbufs[i].rb_vaddrlo),
   1187 		    sizeof(sd));
   1188 		if (sd)
   1189 			free(sd, M_DEVBUF);
   1190 	}
   1191 	txp_dma_free(sc, &sc->sc_rxbufring_dma);
   1192 bail_rspring:
   1193 	txp_dma_free(sc, &sc->sc_rspring_dma);
   1194 bail_cmdring:
   1195 	txp_dma_free(sc, &sc->sc_cmdring_dma);
   1196 bail_rxloring:
   1197 	txp_dma_free(sc, &sc->sc_rxloring_dma);
   1198 bail_rxhiring:
   1199 	txp_dma_free(sc, &sc->sc_rxhiring_dma);
   1200 bail_txloring:
   1201 	txp_dma_free(sc, &sc->sc_txloring_dma);
   1202 bail_txhiring:
   1203 	txp_dma_free(sc, &sc->sc_txhiring_dma);
   1204 bail_host:
   1205 	txp_dma_free(sc, &sc->sc_host_dma);
   1206 bail_boot:
   1207 	txp_dma_free(sc, &sc->sc_boot_dma);
   1208 	return (-1);
   1209 }
   1210 
   1211 static int
   1212 txp_dma_malloc(struct txp_softc *sc, bus_size_t size,
   1213     struct txp_dma_alloc *dma, int mapflags)
   1214 {
   1215 	int r;
   1216 
   1217 	if ((r = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0,
   1218 	    &dma->dma_seg, 1, &dma->dma_nseg, 0)) != 0)
   1219 		goto fail_0;
   1220 
   1221 	if ((r = bus_dmamem_map(sc->sc_dmat, &dma->dma_seg, dma->dma_nseg,
   1222 	    size, &dma->dma_vaddr, mapflags | BUS_DMA_NOWAIT)) != 0)
   1223 		goto fail_1;
   1224 
   1225 	if ((r = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
   1226 	    BUS_DMA_NOWAIT, &dma->dma_map)) != 0)
   1227 		goto fail_2;
   1228 
   1229 	if ((r = bus_dmamap_load(sc->sc_dmat, dma->dma_map, dma->dma_vaddr,
   1230 	    size, NULL, BUS_DMA_NOWAIT)) != 0)
   1231 		goto fail_3;
   1232 
   1233 	dma->dma_paddr = dma->dma_map->dm_segs[0].ds_addr;
   1234 	return (0);
   1235 
   1236 fail_3:
   1237 	bus_dmamap_destroy(sc->sc_dmat, dma->dma_map);
   1238 fail_2:
   1239 	bus_dmamem_unmap(sc->sc_dmat, dma->dma_vaddr, size);
   1240 fail_1:
   1241 	bus_dmamem_free(sc->sc_dmat, &dma->dma_seg, dma->dma_nseg);
   1242 fail_0:
   1243 	return (r);
   1244 }
   1245 
   1246 static void
   1247 txp_dma_free(struct txp_softc *sc, struct txp_dma_alloc *dma)
   1248 {
   1249 	bus_size_t mapsize = dma->dma_map->dm_mapsize;
   1250 
   1251 	bus_dmamap_unload(sc->sc_dmat, dma->dma_map);
   1252 	bus_dmamem_unmap(sc->sc_dmat, dma->dma_vaddr, mapsize);
   1253 	bus_dmamem_free(sc->sc_dmat, &dma->dma_seg, dma->dma_nseg);
   1254 	bus_dmamap_destroy(sc->sc_dmat, dma->dma_map);
   1255 }
   1256 
   1257 static int
   1258 txp_ioctl(struct ifnet *ifp, u_long command, void *data)
   1259 {
   1260 	struct txp_softc *sc = ifp->if_softc;
   1261 	struct ifaddr *ifa = (struct ifaddr *)data;
   1262 	int s, error = 0;
   1263 
   1264 	s = splnet();
   1265 
   1266 #if 0
   1267 	if ((error = ether_ioctl(ifp, &sc->sc_arpcom, command, data)) > 0) {
   1268 		splx(s);
   1269 		return error;
   1270 	}
   1271 #endif
   1272 
   1273 	switch (command) {
   1274 	case SIOCINITIFADDR:
   1275 		ifp->if_flags |= IFF_UP;
   1276 		txp_init(sc);
   1277 		switch (ifa->ifa_addr->sa_family) {
   1278 #ifdef INET
   1279 		case AF_INET:
   1280 			arp_ifinit(ifp, ifa);
   1281 			break;
   1282 #endif /* INET */
   1283 		default:
   1284 			break;
   1285 		}
   1286 		break;
   1287 	case SIOCSIFFLAGS:
   1288 		if ((error = ifioctl_common(ifp, command, data)) != 0)
   1289 			break;
   1290 		if (ifp->if_flags & IFF_UP) {
   1291 			txp_init(sc);
   1292 		} else {
   1293 			if (ifp->if_flags & IFF_RUNNING)
   1294 				txp_stop(sc);
   1295 		}
   1296 		break;
   1297 	case SIOCADDMULTI:
   1298 	case SIOCDELMULTI:
   1299 		if ((error = ether_ioctl(ifp, command, data)) != ENETRESET)
   1300 			break;
   1301 
   1302 		error = 0;
   1303 
   1304 		if (command != SIOCADDMULTI && command != SIOCDELMULTI)
   1305 			;
   1306 		else if (ifp->if_flags & IFF_RUNNING) {
   1307 			/*
   1308 			 * Multicast list has changed; set the hardware
   1309 			 * filter accordingly.
   1310 			 */
   1311 			txp_set_filter(sc);
   1312 		}
   1313 		break;
   1314 	default:
   1315 		error = ether_ioctl(ifp, command, data);
   1316 		break;
   1317 	}
   1318 
   1319 	splx(s);
   1320 
   1321 	return (error);
   1322 }
   1323 
   1324 static void
   1325 txp_init(struct txp_softc *sc)
   1326 {
   1327 	struct ifnet *ifp = &sc->sc_arpcom.ec_if;
   1328 	int s;
   1329 
   1330 	txp_stop(sc);
   1331 
   1332 	s = splnet();
   1333 
   1334 	txp_set_filter(sc);
   1335 
   1336 	txp_command(sc, TXP_CMD_TX_ENABLE, 0, 0, 0, NULL, NULL, NULL, 1);
   1337 	txp_command(sc, TXP_CMD_RX_ENABLE, 0, 0, 0, NULL, NULL, NULL, 1);
   1338 
   1339 	WRITE_REG(sc, TXP_IER, TXP_INT_RESERVED | TXP_INT_SELF |
   1340 	    TXP_INT_A2H_7 | TXP_INT_A2H_6 | TXP_INT_A2H_5 | TXP_INT_A2H_4 |
   1341 	    TXP_INT_A2H_2 | TXP_INT_A2H_1 | TXP_INT_A2H_0 |
   1342 	    TXP_INT_DMA3 | TXP_INT_DMA2 | TXP_INT_DMA1 | TXP_INT_DMA0 |
   1343 	    TXP_INT_PCI_TABORT | TXP_INT_PCI_MABORT |  TXP_INT_LATCH);
   1344 	WRITE_REG(sc, TXP_IMR, TXP_INT_A2H_3);
   1345 
   1346 	ifp->if_flags |= IFF_RUNNING;
   1347 	ifp->if_flags &= ~IFF_OACTIVE;
   1348 	ifp->if_timer = 0;
   1349 
   1350 	if (!callout_pending(&sc->sc_tick))
   1351 		callout_schedule(&sc->sc_tick, hz);
   1352 
   1353 	splx(s);
   1354 }
   1355 
   1356 static void
   1357 txp_tick(void *vsc)
   1358 {
   1359 	struct txp_softc *sc = vsc;
   1360 	struct ifnet *ifp = &sc->sc_arpcom.ec_if;
   1361 	struct txp_rsp_desc *rsp = NULL;
   1362 	struct txp_ext_desc *ext;
   1363 	int s;
   1364 
   1365 	s = splnet();
   1366 	txp_rxbuf_reclaim(sc);
   1367 
   1368 	if (txp_command2(sc, TXP_CMD_READ_STATISTICS, 0, 0, 0, NULL, 0,
   1369 	    &rsp, 1))
   1370 		goto out;
   1371 	if (rsp->rsp_numdesc != 6)
   1372 		goto out;
   1373 	if (txp_command(sc, TXP_CMD_CLEAR_STATISTICS, 0, 0, 0,
   1374 	    NULL, NULL, NULL, 1))
   1375 		goto out;
   1376 	ext = (struct txp_ext_desc *)(rsp + 1);
   1377 
   1378 	net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
   1379 	if_statadd_ref(nsr, if_ierrors,
   1380 	    ext[3].ext_2 + ext[3].ext_3 + ext[3].ext_4 +
   1381 	    ext[4].ext_1 + ext[4].ext_4);
   1382 	if_statadd_ref(nsr, if_oerrors,
   1383 	    ext[0].ext_1 + ext[1].ext_1 + ext[1].ext_4 + ext[2].ext_1);
   1384 	if_statadd_ref(nsr, if_collisions,
   1385 	    ext[0].ext_2 + ext[0].ext_3 + ext[1].ext_2 + ext[1].ext_3);
   1386 	if_statadd_ref(nsr, if_opackets, rsp->rsp_par2);
   1387 	IF_STAT_PUTREF(ifp);
   1388 
   1389 out:
   1390 	if (rsp != NULL)
   1391 		free(rsp, M_DEVBUF);
   1392 
   1393 	splx(s);
   1394 	callout_schedule(&sc->sc_tick, hz);
   1395 }
   1396 
   1397 static void
   1398 txp_start(struct ifnet *ifp)
   1399 {
   1400 	struct txp_softc *sc = ifp->if_softc;
   1401 	struct txp_tx_ring *r = &sc->sc_txhir;
   1402 	struct txp_tx_desc *txd;
   1403 	int txdidx;
   1404 	struct txp_frag_desc *fxd;
   1405 	struct mbuf *m, *mnew;
   1406 	struct txp_swdesc *sd;
   1407 	uint32_t firstprod, firstcnt, prod, cnt, i;
   1408 
   1409 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
   1410 		return;
   1411 
   1412 	prod = r->r_prod;
   1413 	cnt = r->r_cnt;
   1414 
   1415 	while (1) {
   1416 		IFQ_POLL(&ifp->if_snd, m);
   1417 		if (m == NULL)
   1418 			break;
   1419 		mnew = NULL;
   1420 
   1421 		firstprod = prod;
   1422 		firstcnt = cnt;
   1423 
   1424 		sd = sc->sc_txd + prod;
   1425 		sd->sd_mbuf = m;
   1426 
   1427 		if (bus_dmamap_load_mbuf(sc->sc_dmat, sd->sd_map, m,
   1428 		    BUS_DMA_NOWAIT)) {
   1429 			MGETHDR(mnew, M_DONTWAIT, MT_DATA);
   1430 			if (mnew == NULL)
   1431 				goto oactive1;
   1432 			if (m->m_pkthdr.len > MHLEN) {
   1433 				MCLGET(mnew, M_DONTWAIT);
   1434 				if ((mnew->m_flags & M_EXT) == 0) {
   1435 					m_freem(mnew);
   1436 					goto oactive1;
   1437 				}
   1438 			}
   1439 			m_copydata(m, 0, m->m_pkthdr.len, mtod(mnew, void *));
   1440 			mnew->m_pkthdr.len = mnew->m_len = m->m_pkthdr.len;
   1441 			IFQ_DEQUEUE(&ifp->if_snd, m);
   1442 			m_freem(m);
   1443 			m = mnew;
   1444 			if (bus_dmamap_load_mbuf(sc->sc_dmat, sd->sd_map, m,
   1445 			    BUS_DMA_NOWAIT))
   1446 				goto oactive1;
   1447 		}
   1448 
   1449 		if ((TX_ENTRIES - cnt) < 4)
   1450 			goto oactive;
   1451 
   1452 		txd = r->r_desc + prod;
   1453 		txdidx = prod;
   1454 		txd->tx_flags = TX_FLAGS_TYPE_DATA;
   1455 		txd->tx_numdesc = 0;
   1456 		txd->tx_addrlo = 0;
   1457 		txd->tx_addrhi = 0;
   1458 		txd->tx_totlen = m->m_pkthdr.len;
   1459 		txd->tx_pflags = 0;
   1460 		txd->tx_numdesc = sd->sd_map->dm_nsegs;
   1461 
   1462 		if (++prod == TX_ENTRIES)
   1463 			prod = 0;
   1464 
   1465 		if (++cnt >= (TX_ENTRIES - 4))
   1466 			goto oactive;
   1467 
   1468 		if (vlan_has_tag(m))
   1469 			txd->tx_pflags = TX_PFLAGS_VLAN |
   1470 			  (htons(vlan_get_tag(m)) << TX_PFLAGS_VLANTAG_S);
   1471 
   1472 		if (m->m_pkthdr.csum_flags & M_CSUM_IPv4)
   1473 			txd->tx_pflags |= TX_PFLAGS_IPCKSUM;
   1474 #ifdef TRY_TX_TCP_CSUM
   1475 		if (m->m_pkthdr.csum_flags & M_CSUM_TCPv4)
   1476 			txd->tx_pflags |= TX_PFLAGS_TCPCKSUM;
   1477 #endif
   1478 #ifdef TRY_TX_UDP_CSUM
   1479 		if (m->m_pkthdr.csum_flags & M_CSUM_UDPv4)
   1480 			txd->tx_pflags |= TX_PFLAGS_UDPCKSUM;
   1481 #endif
   1482 
   1483 		bus_dmamap_sync(sc->sc_dmat, sd->sd_map, 0,
   1484 		    sd->sd_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   1485 
   1486 		fxd = (struct txp_frag_desc *)(r->r_desc + prod);
   1487 		for (i = 0; i < sd->sd_map->dm_nsegs; i++) {
   1488 			if (++cnt >= (TX_ENTRIES - 4)) {
   1489 				bus_dmamap_sync(sc->sc_dmat, sd->sd_map,
   1490 				    0, sd->sd_map->dm_mapsize,
   1491 				    BUS_DMASYNC_POSTWRITE);
   1492 				goto oactive;
   1493 			}
   1494 
   1495 			fxd->frag_flags = FRAG_FLAGS_TYPE_FRAG |
   1496 			    FRAG_FLAGS_VALID;
   1497 			fxd->frag_rsvd1 = 0;
   1498 			fxd->frag_len = htole16(sd->sd_map->dm_segs[i].ds_len);
   1499 			fxd->frag_addrlo =
   1500 			    htole32(BUS_ADDR_LO32(sd->sd_map->dm_segs[i].ds_addr));
   1501 			fxd->frag_addrhi =
   1502 			    htole32(BUS_ADDR_HI32(sd->sd_map->dm_segs[i].ds_addr));
   1503 			fxd->frag_rsvd2 = 0;
   1504 
   1505 			bus_dmamap_sync(sc->sc_dmat,
   1506 			    sc->sc_txhiring_dma.dma_map,
   1507 			    prod * sizeof(struct txp_frag_desc),
   1508 			    sizeof(struct txp_frag_desc), BUS_DMASYNC_PREWRITE);
   1509 
   1510 			if (++prod == TX_ENTRIES) {
   1511 				fxd = (struct txp_frag_desc *)r->r_desc;
   1512 				prod = 0;
   1513 			} else
   1514 				fxd++;
   1515 
   1516 		}
   1517 
   1518 		/*
   1519 		 * if mnew isn't NULL, we already dequeued and copied
   1520 		 * the packet.
   1521 		 */
   1522 		if (mnew == NULL)
   1523 			IFQ_DEQUEUE(&ifp->if_snd, m);
   1524 
   1525 		ifp->if_timer = 5;
   1526 
   1527 		bpf_mtap(ifp, m, BPF_D_OUT);
   1528 
   1529 		txd->tx_flags |= TX_FLAGS_VALID;
   1530 		bus_dmamap_sync(sc->sc_dmat, sc->sc_txhiring_dma.dma_map,
   1531 		    txdidx * sizeof(struct txp_tx_desc),
   1532 		    sizeof(struct txp_tx_desc), BUS_DMASYNC_PREWRITE);
   1533 
   1534 #if 0
   1535 		{
   1536 			struct mbuf *mx;
   1537 			int i;
   1538 
   1539 			printf("txd: flags 0x%x ndesc %d totlen %d pflags 0x%x\n",
   1540 			    txd->tx_flags, txd->tx_numdesc, txd->tx_totlen,
   1541 			    txd->tx_pflags);
   1542 			for (mx = m; mx != NULL; mx = mx->m_next) {
   1543 				for (i = 0; i < mx->m_len; i++) {
   1544 					printf(":%02x",
   1545 					    (uint8_t)m->m_data[i]);
   1546 				}
   1547 			}
   1548 			printf("\n");
   1549 		}
   1550 #endif
   1551 
   1552 		WRITE_REG(sc, r->r_reg, TXP_IDX2OFFSET(prod));
   1553 	}
   1554 
   1555 	r->r_prod = prod;
   1556 	r->r_cnt = cnt;
   1557 	return;
   1558 
   1559 oactive:
   1560 	bus_dmamap_unload(sc->sc_dmat, sd->sd_map);
   1561 oactive1:
   1562 	ifp->if_flags |= IFF_OACTIVE;
   1563 	r->r_prod = firstprod;
   1564 	r->r_cnt = firstcnt;
   1565 }
   1566 
   1567 /*
   1568  * Handle simple commands sent to the typhoon
   1569  */
   1570 static int
   1571 txp_command(struct txp_softc *sc, uint16_t id, uint16_t in1, uint32_t in2,
   1572     uint32_t in3, uint16_t *out1, uint32_t *out2, uint32_t *out3, int wait)
   1573 {
   1574 	struct txp_rsp_desc *rsp = NULL;
   1575 
   1576 	if (txp_command2(sc, id, in1, in2, in3, NULL, 0, &rsp, wait))
   1577 		return (-1);
   1578 
   1579 	if (!wait)
   1580 		return (0);
   1581 
   1582 	if (out1 != NULL)
   1583 		*out1 = le16toh(rsp->rsp_par1);
   1584 	if (out2 != NULL)
   1585 		*out2 = le32toh(rsp->rsp_par2);
   1586 	if (out3 != NULL)
   1587 		*out3 = le32toh(rsp->rsp_par3);
   1588 	free(rsp, M_DEVBUF);
   1589 	return (0);
   1590 }
   1591 
   1592 static int
   1593 txp_command2(struct txp_softc *sc, uint16_t id, uint16_t in1, uint32_t in2,
   1594     uint32_t in3, struct txp_ext_desc *in_extp, uint8_t in_extn,
   1595     struct txp_rsp_desc **rspp, int wait)
   1596 {
   1597 	struct txp_hostvar *hv = sc->sc_hostvar;
   1598 	struct txp_cmd_desc *cmd;
   1599 	struct txp_ext_desc *ext;
   1600 	uint32_t idx, i;
   1601 	uint16_t seq;
   1602 
   1603 	if (txp_cmd_desc_numfree(sc) < (in_extn + 1)) {
   1604 		printf("%s: no free cmd descriptors\n", TXP_DEVNAME(sc));
   1605 		return (-1);
   1606 	}
   1607 
   1608 	idx = sc->sc_cmdring.lastwrite;
   1609 	cmd = (struct txp_cmd_desc *)(((uint8_t *)sc->sc_cmdring.base) + idx);
   1610 	memset(cmd, 0, sizeof(*cmd));
   1611 
   1612 	cmd->cmd_numdesc = in_extn;
   1613 	seq = sc->sc_seq++;
   1614 	cmd->cmd_seq = htole16(seq);
   1615 	cmd->cmd_id = htole16(id);
   1616 	cmd->cmd_par1 = htole16(in1);
   1617 	cmd->cmd_par2 = htole32(in2);
   1618 	cmd->cmd_par3 = htole32(in3);
   1619 	cmd->cmd_flags = CMD_FLAGS_TYPE_CMD |
   1620 	    (wait ? CMD_FLAGS_RESP : 0) | CMD_FLAGS_VALID;
   1621 
   1622 	idx += sizeof(struct txp_cmd_desc);
   1623 	if (idx == sc->sc_cmdring.size)
   1624 		idx = 0;
   1625 
   1626 	for (i = 0; i < in_extn; i++) {
   1627 		ext = (struct txp_ext_desc *)(((uint8_t *)sc->sc_cmdring.base) + idx);
   1628 		memcpy(ext, in_extp, sizeof(struct txp_ext_desc));
   1629 		in_extp++;
   1630 		idx += sizeof(struct txp_cmd_desc);
   1631 		if (idx == sc->sc_cmdring.size)
   1632 			idx = 0;
   1633 	}
   1634 
   1635 	sc->sc_cmdring.lastwrite = idx;
   1636 
   1637 	WRITE_REG(sc, TXP_H2A_2, sc->sc_cmdring.lastwrite);
   1638 	bus_dmamap_sync(sc->sc_dmat, sc->sc_host_dma.dma_map, 0,
   1639 	    sizeof(struct txp_hostvar), BUS_DMASYNC_PREREAD);
   1640 
   1641 	if (!wait)
   1642 		return (0);
   1643 
   1644 	for (i = 0; i < 10000; i++) {
   1645 		bus_dmamap_sync(sc->sc_dmat, sc->sc_host_dma.dma_map, 0,
   1646 		    sizeof(struct txp_hostvar), BUS_DMASYNC_POSTREAD);
   1647 		idx = le32toh(hv->hv_resp_read_idx);
   1648 		if (idx != le32toh(hv->hv_resp_write_idx)) {
   1649 			*rspp = NULL;
   1650 			if (txp_response(sc, idx, id, seq, rspp))
   1651 				return (-1);
   1652 			if (*rspp != NULL)
   1653 				break;
   1654 		}
   1655 		bus_dmamap_sync(sc->sc_dmat, sc->sc_host_dma.dma_map, 0,
   1656 		    sizeof(struct txp_hostvar), BUS_DMASYNC_PREREAD);
   1657 		DELAY(50);
   1658 	}
   1659 	if (i == 1000 || (*rspp) == NULL) {
   1660 		printf("%s: 0x%x command failed\n", TXP_DEVNAME(sc), id);
   1661 		return (-1);
   1662 	}
   1663 
   1664 	return (0);
   1665 }
   1666 
   1667 static int
   1668 txp_response(struct txp_softc *sc, uint32_t ridx, uint16_t id, uint16_t seq,
   1669     struct txp_rsp_desc **rspp)
   1670 {
   1671 	struct txp_hostvar *hv = sc->sc_hostvar;
   1672 	struct txp_rsp_desc *rsp;
   1673 
   1674 	while (ridx != le32toh(hv->hv_resp_write_idx)) {
   1675 		rsp = (struct txp_rsp_desc *)(((uint8_t *)sc->sc_rspring.base) + ridx);
   1676 
   1677 		if (id == le16toh(rsp->rsp_id) && le16toh(rsp->rsp_seq) == seq) {
   1678 			*rspp = (struct txp_rsp_desc *)malloc(
   1679 			    sizeof(struct txp_rsp_desc) * (rsp->rsp_numdesc + 1),
   1680 			    M_DEVBUF, M_NOWAIT);
   1681 			if ((*rspp) == NULL)
   1682 				return (-1);
   1683 			txp_rsp_fixup(sc, rsp, *rspp);
   1684 			return (0);
   1685 		}
   1686 
   1687 		if (rsp->rsp_flags & RSP_FLAGS_ERROR) {
   1688 			printf("%s: response error: id 0x%x\n",
   1689 			    TXP_DEVNAME(sc), le16toh(rsp->rsp_id));
   1690 			txp_rsp_fixup(sc, rsp, NULL);
   1691 			ridx = le32toh(hv->hv_resp_read_idx);
   1692 			continue;
   1693 		}
   1694 
   1695 		switch (le16toh(rsp->rsp_id)) {
   1696 		case TXP_CMD_CYCLE_STATISTICS:
   1697 		case TXP_CMD_MEDIA_STATUS_READ:
   1698 			break;
   1699 		case TXP_CMD_HELLO_RESPONSE:
   1700 			printf("%s: hello\n", TXP_DEVNAME(sc));
   1701 			break;
   1702 		default:
   1703 			printf("%s: unknown id(0x%x)\n", TXP_DEVNAME(sc),
   1704 			    le16toh(rsp->rsp_id));
   1705 		}
   1706 
   1707 		txp_rsp_fixup(sc, rsp, NULL);
   1708 		ridx = le32toh(hv->hv_resp_read_idx);
   1709 		hv->hv_resp_read_idx = le32toh(ridx);
   1710 	}
   1711 
   1712 	return (0);
   1713 }
   1714 
   1715 static void
   1716 txp_rsp_fixup(struct txp_softc *sc, struct txp_rsp_desc *rsp,
   1717     struct txp_rsp_desc *dst)
   1718 {
   1719 	struct txp_rsp_desc *src = rsp;
   1720 	struct txp_hostvar *hv = sc->sc_hostvar;
   1721 	uint32_t i, ridx;
   1722 
   1723 	ridx = le32toh(hv->hv_resp_read_idx);
   1724 
   1725 	for (i = 0; i < rsp->rsp_numdesc + 1; i++) {
   1726 		if (dst != NULL)
   1727 			memcpy(dst++, src, sizeof(struct txp_rsp_desc));
   1728 		ridx += sizeof(struct txp_rsp_desc);
   1729 		if (ridx == sc->sc_rspring.size) {
   1730 			src = sc->sc_rspring.base;
   1731 			ridx = 0;
   1732 		} else
   1733 			src++;
   1734 		sc->sc_rspring.lastwrite = ridx;
   1735 		hv->hv_resp_read_idx = htole32(ridx);
   1736 	}
   1737 
   1738 	hv->hv_resp_read_idx = htole32(ridx);
   1739 }
   1740 
   1741 static int
   1742 txp_cmd_desc_numfree(struct txp_softc *sc)
   1743 {
   1744 	struct txp_hostvar *hv = sc->sc_hostvar;
   1745 	struct txp_boot_record *br = sc->sc_boot;
   1746 	uint32_t widx, ridx, nfree;
   1747 
   1748 	widx = sc->sc_cmdring.lastwrite;
   1749 	ridx = le32toh(hv->hv_cmd_read_idx);
   1750 
   1751 	if (widx == ridx) {
   1752 		/* Ring is completely free */
   1753 		nfree = le32toh(br->br_cmd_siz) - sizeof(struct txp_cmd_desc);
   1754 	} else {
   1755 		if (widx > ridx)
   1756 			nfree = le32toh(br->br_cmd_siz) -
   1757 			    (widx - ridx + sizeof(struct txp_cmd_desc));
   1758 		else
   1759 			nfree = ridx - widx - sizeof(struct txp_cmd_desc);
   1760 	}
   1761 
   1762 	return (nfree / sizeof(struct txp_cmd_desc));
   1763 }
   1764 
   1765 static void
   1766 txp_stop(struct txp_softc *sc)
   1767 {
   1768 	txp_command(sc, TXP_CMD_TX_DISABLE, 0, 0, 0, NULL, NULL, NULL, 1);
   1769 	txp_command(sc, TXP_CMD_RX_DISABLE, 0, 0, 0, NULL, NULL, NULL, 1);
   1770 
   1771 	if (callout_pending(&sc->sc_tick))
   1772 		callout_stop(&sc->sc_tick);
   1773 }
   1774 
   1775 static void
   1776 txp_watchdog(struct ifnet *ifp)
   1777 {
   1778 }
   1779 
   1780 static int
   1781 txp_ifmedia_upd(struct ifnet *ifp)
   1782 {
   1783 	struct txp_softc *sc = ifp->if_softc;
   1784 	struct ifmedia *ifm = &sc->sc_ifmedia;
   1785 	uint16_t new_xcvr;
   1786 
   1787 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
   1788 		return (EINVAL);
   1789 
   1790 	if (IFM_SUBTYPE(ifm->ifm_media) == IFM_10_T) {
   1791 		if ((ifm->ifm_media & IFM_FDX) != 0)
   1792 			new_xcvr = TXP_XCVR_10_FDX;
   1793 		else
   1794 			new_xcvr = TXP_XCVR_10_HDX;
   1795 	} else if ((IFM_SUBTYPE(ifm->ifm_media) == IFM_100_TX) ||
   1796 		   (IFM_SUBTYPE(ifm->ifm_media) == IFM_100_FX)) {
   1797 		if ((ifm->ifm_media & IFM_FDX) != 0)
   1798 			new_xcvr = TXP_XCVR_100_FDX;
   1799 		else
   1800 			new_xcvr = TXP_XCVR_100_HDX;
   1801 	} else if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO) {
   1802 		new_xcvr = TXP_XCVR_AUTO;
   1803 	} else
   1804 		return (EINVAL);
   1805 
   1806 	/* nothing to do */
   1807 	if (sc->sc_xcvr == new_xcvr)
   1808 		return (0);
   1809 
   1810 	txp_command(sc, TXP_CMD_XCVR_SELECT, new_xcvr, 0, 0,
   1811 	    NULL, NULL, NULL, 0);
   1812 	sc->sc_xcvr = new_xcvr;
   1813 
   1814 	return (0);
   1815 }
   1816 
   1817 static void
   1818 txp_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
   1819 {
   1820 	struct txp_softc *sc = ifp->if_softc;
   1821 	struct ifmedia *ifm = &sc->sc_ifmedia;
   1822 	uint16_t bmsr, bmcr, anlpar;
   1823 
   1824 	ifmr->ifm_status = IFM_AVALID;
   1825 	ifmr->ifm_active = IFM_ETHER;
   1826 
   1827 	if (txp_command(sc, TXP_CMD_PHY_MGMT_READ, 0, MII_BMSR, 0,
   1828 	    &bmsr, NULL, NULL, 1))
   1829 		goto bail;
   1830 	if (txp_command(sc, TXP_CMD_PHY_MGMT_READ, 0, MII_BMSR, 0,
   1831 	    &bmsr, NULL, NULL, 1))
   1832 		goto bail;
   1833 
   1834 	if (txp_command(sc, TXP_CMD_PHY_MGMT_READ, 0, MII_BMCR, 0,
   1835 	    &bmcr, NULL, NULL, 1))
   1836 		goto bail;
   1837 
   1838 	if (txp_command(sc, TXP_CMD_PHY_MGMT_READ, 0, MII_ANLPAR, 0,
   1839 	    &anlpar, NULL, NULL, 1))
   1840 		goto bail;
   1841 
   1842 	if (bmsr & BMSR_LINK)
   1843 		ifmr->ifm_status |= IFM_ACTIVE;
   1844 
   1845 	if (bmcr & BMCR_ISO) {
   1846 		ifmr->ifm_active |= IFM_NONE;
   1847 		ifmr->ifm_status = 0;
   1848 		return;
   1849 	}
   1850 
   1851 	if (bmcr & BMCR_LOOP)
   1852 		ifmr->ifm_active |= IFM_LOOP;
   1853 
   1854 	if (!(sc->sc_flags & TXP_FIBER) && (bmcr & BMCR_AUTOEN)) {
   1855 		if ((bmsr & BMSR_ACOMP) == 0) {
   1856 			ifmr->ifm_active |= IFM_NONE;
   1857 			return;
   1858 		}
   1859 
   1860 		if (anlpar & ANLPAR_TX_FD)
   1861 			ifmr->ifm_active |= IFM_100_TX | IFM_FDX;
   1862 		else if (anlpar & ANLPAR_T4)
   1863 			ifmr->ifm_active |= IFM_100_T4 | IFM_HDX;
   1864 		else if (anlpar & ANLPAR_TX)
   1865 			ifmr->ifm_active |= IFM_100_TX | IFM_HDX;
   1866 		else if (anlpar & ANLPAR_10_FD)
   1867 			ifmr->ifm_active |= IFM_10_T | IFM_FDX;
   1868 		else if (anlpar & ANLPAR_10)
   1869 			ifmr->ifm_active |= IFM_10_T | IFM_HDX;
   1870 		else
   1871 			ifmr->ifm_active |= IFM_NONE;
   1872 	} else
   1873 		ifmr->ifm_active = ifm->ifm_cur->ifm_media;
   1874 	return;
   1875 
   1876 bail:
   1877 	ifmr->ifm_active |= IFM_NONE;
   1878 	ifmr->ifm_status &= ~IFM_AVALID;
   1879 }
   1880 
   1881 #if 0 /* XXX XXX XXX UNUSED */
   1882 static void
   1883 txp_show_descriptor(void *d)
   1884 {
   1885 	struct txp_cmd_desc *cmd = d;
   1886 	struct txp_rsp_desc *rsp = d;
   1887 	struct txp_tx_desc *txd = d;
   1888 	struct txp_frag_desc *frgd = d;
   1889 
   1890 	switch (cmd->cmd_flags & CMD_FLAGS_TYPE_M) {
   1891 	case CMD_FLAGS_TYPE_CMD:
   1892 		/* command descriptor */
   1893 		printf("[cmd flags 0x%x num %d id %d seq %d par1 0x%x par2 "
   1894 		    "0x%x par3 0x%x]\n",
   1895 		    cmd->cmd_flags, cmd->cmd_numdesc, le16toh(cmd->cmd_id),
   1896 		    le16toh(cmd->cmd_seq), le16toh(cmd->cmd_par1),
   1897 		    le32toh(cmd->cmd_par2), le32toh(cmd->cmd_par3));
   1898 		break;
   1899 	case CMD_FLAGS_TYPE_RESP:
   1900 		/* response descriptor */
   1901 		printf("[rsp flags 0x%x num %d id %d seq %d par1 0x%x par2 "
   1902 		    "0x%x par3 0x%x]\n",
   1903 		    rsp->rsp_flags, rsp->rsp_numdesc, le16toh(rsp->rsp_id),
   1904 		    le16toh(rsp->rsp_seq), le16toh(rsp->rsp_par1),
   1905 		    le32toh(rsp->rsp_par2), le32toh(rsp->rsp_par3));
   1906 		break;
   1907 	case CMD_FLAGS_TYPE_DATA:
   1908 		/* data header (assuming tx for now) */
   1909 		printf("[data flags 0x%x num %d totlen %d addr 0x%x/0x%x "
   1910 		    "pflags 0x%x]",
   1911 		    txd->tx_flags, txd->tx_numdesc, txd->tx_totlen,
   1912 		    txd->tx_addrlo, txd->tx_addrhi, txd->tx_pflags);
   1913 		break;
   1914 	case CMD_FLAGS_TYPE_FRAG:
   1915 		/* fragment descriptor */
   1916 		printf("[frag flags 0x%x rsvd1 0x%x len %d addr 0x%x/0x%x "
   1917 		    "rsvd2 0x%x]",
   1918 		    frgd->frag_flags, frgd->frag_rsvd1, frgd->frag_len,
   1919 		    frgd->frag_addrlo, frgd->frag_addrhi, frgd->frag_rsvd2);
   1920 		break;
   1921 	default:
   1922 		printf("[unknown(%x) flags 0x%x num %d id %d seq %d par1 "
   1923 		    "0x%x par2 0x%x par3 0x%x]\n",
   1924 		    cmd->cmd_flags & CMD_FLAGS_TYPE_M,
   1925 		    cmd->cmd_flags, cmd->cmd_numdesc, le16toh(cmd->cmd_id),
   1926 		    le16toh(cmd->cmd_seq), le16toh(cmd->cmd_par1),
   1927 		    le32toh(cmd->cmd_par2), le32toh(cmd->cmd_par3));
   1928 		break;
   1929 	}
   1930 }
   1931 #endif
   1932 
   1933 static void
   1934 txp_set_filter(struct txp_softc *sc)
   1935 {
   1936 	struct ethercom *ec = &sc->sc_arpcom;
   1937 	struct ifnet *ifp = &sc->sc_arpcom.ec_if;
   1938 	uint32_t crc, carry, hashbit, hash[2];
   1939 	uint16_t filter;
   1940 	uint8_t octet;
   1941 	int i, j, mcnt = 0;
   1942 	struct ether_multi *enm;
   1943 	struct ether_multistep step;
   1944 
   1945 	if (ifp->if_flags & IFF_PROMISC) {
   1946 		filter = TXP_RXFILT_PROMISC;
   1947 		goto setit;
   1948 	}
   1949 
   1950 again:
   1951 	filter = TXP_RXFILT_DIRECT;
   1952 
   1953 	if (ifp->if_flags & IFF_BROADCAST)
   1954 		filter |= TXP_RXFILT_BROADCAST;
   1955 
   1956 	if (ifp->if_flags & IFF_ALLMULTI)
   1957 		filter |= TXP_RXFILT_ALLMULTI;
   1958 	else {
   1959 		hash[0] = hash[1] = 0;
   1960 
   1961 		ETHER_LOCK(ec);
   1962 		ETHER_FIRST_MULTI(step, ec, enm);
   1963 		while (enm != NULL) {
   1964 			if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
   1965 			    ETHER_ADDR_LEN)) {
   1966 				/*
   1967 				 * We must listen to a range of multicast
   1968 				 * addresses.  For now, just accept all
   1969 				 * multicasts, rather than trying to set only
   1970 				 * those filter bits needed to match the range.
   1971 				 * (At this time, the only use of address
   1972 				 * ranges is for IP multicast routing, for
   1973 				 * which the range is big enough to require
   1974 				 * all bits set.)
   1975 				 */
   1976 				ifp->if_flags |= IFF_ALLMULTI;
   1977 				ETHER_UNLOCK(ec);
   1978 				goto again;
   1979 			}
   1980 
   1981 			mcnt++;
   1982 			crc = 0xffffffff;
   1983 
   1984 			for (i = 0; i < ETHER_ADDR_LEN; i++) {
   1985 				octet = enm->enm_addrlo[i];
   1986 				for (j = 0; j < 8; j++) {
   1987 					carry = ((crc & 0x80000000) ? 1 : 0) ^
   1988 					    (octet & 1);
   1989 					crc <<= 1;
   1990 					octet >>= 1;
   1991 					if (carry)
   1992 						crc = (crc ^ TXP_POLYNOMIAL) |
   1993 						    carry;
   1994 				}
   1995 			}
   1996 			hashbit = (uint16_t)(crc & (64 - 1));
   1997 			hash[hashbit / 32] |= (1 << hashbit % 32);
   1998 			ETHER_NEXT_MULTI(step, enm);
   1999 		}
   2000 		ETHER_UNLOCK(ec);
   2001 
   2002 		if (mcnt > 0) {
   2003 			filter |= TXP_RXFILT_HASHMULTI;
   2004 			txp_command(sc, TXP_CMD_MCAST_HASH_MASK_WRITE,
   2005 			    2, hash[0], hash[1], NULL, NULL, NULL, 0);
   2006 		}
   2007 	}
   2008 
   2009 setit:
   2010 	txp_command(sc, TXP_CMD_RX_FILTER_WRITE, filter, 0, 0,
   2011 	    NULL, NULL, NULL, 1);
   2012 }
   2013 
   2014 static void
   2015 txp_capabilities(struct txp_softc *sc)
   2016 {
   2017 	struct ifnet *ifp = &sc->sc_arpcom.ec_if;
   2018 	struct txp_rsp_desc *rsp = NULL;
   2019 	struct txp_ext_desc *ext;
   2020 
   2021 	if (txp_command2(sc, TXP_CMD_OFFLOAD_READ, 0, 0, 0, NULL, 0, &rsp, 1))
   2022 		goto out;
   2023 
   2024 	if (rsp->rsp_numdesc != 1)
   2025 		goto out;
   2026 	ext = (struct txp_ext_desc *)(rsp + 1);
   2027 
   2028 	sc->sc_tx_capability = ext->ext_1 & OFFLOAD_MASK;
   2029 	sc->sc_rx_capability = ext->ext_2 & OFFLOAD_MASK;
   2030 
   2031 	sc->sc_arpcom.ec_capabilities |= ETHERCAP_VLAN_MTU;
   2032 	if (rsp->rsp_par2 & rsp->rsp_par3 & OFFLOAD_VLAN) {
   2033 		sc->sc_tx_capability |= OFFLOAD_VLAN;
   2034 		sc->sc_rx_capability |= OFFLOAD_VLAN;
   2035 		sc->sc_arpcom.ec_capabilities |= ETHERCAP_VLAN_HWTAGGING;
   2036 		sc->sc_arpcom.ec_capenable |= ETHERCAP_VLAN_HWTAGGING;
   2037 	}
   2038 
   2039 #if 0
   2040 	/* not ready yet */
   2041 	if (rsp->rsp_par2 & rsp->rsp_par3 & OFFLOAD_IPSEC) {
   2042 		sc->sc_tx_capability |= OFFLOAD_IPSEC;
   2043 		sc->sc_rx_capability |= OFFLOAD_IPSEC;
   2044 		ifp->if_capabilities |= IFCAP_IPSEC;
   2045 	}
   2046 #endif
   2047 
   2048 	if (rsp->rsp_par2 & rsp->rsp_par3 & OFFLOAD_IPCKSUM) {
   2049 		sc->sc_tx_capability |= OFFLOAD_IPCKSUM;
   2050 		sc->sc_rx_capability |= OFFLOAD_IPCKSUM;
   2051 		ifp->if_capabilities |= IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx;
   2052 	}
   2053 
   2054 	if (rsp->rsp_par2 & rsp->rsp_par3 & OFFLOAD_TCPCKSUM) {
   2055 		sc->sc_rx_capability |= OFFLOAD_TCPCKSUM;
   2056 #ifdef TRY_TX_TCP_CSUM
   2057 		sc->sc_tx_capability |= OFFLOAD_TCPCKSUM;
   2058 		ifp->if_capabilities |=
   2059 		    IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx;
   2060 #endif
   2061 	}
   2062 
   2063 	if (rsp->rsp_par2 & rsp->rsp_par3 & OFFLOAD_UDPCKSUM) {
   2064 		sc->sc_rx_capability |= OFFLOAD_UDPCKSUM;
   2065 #ifdef TRY_TX_UDP_CSUM
   2066 		sc->sc_tx_capability |= OFFLOAD_UDPCKSUM;
   2067 		ifp->if_capabilities |=
   2068 		    IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;
   2069 #endif
   2070 	}
   2071 
   2072 	if (txp_command(sc, TXP_CMD_OFFLOAD_WRITE, 0,
   2073 	    sc->sc_tx_capability, sc->sc_rx_capability, NULL, NULL, NULL, 1))
   2074 		goto out;
   2075 
   2076 out:
   2077 	if (rsp != NULL)
   2078 		free(rsp, M_DEVBUF);
   2079 }
   2080