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