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