Home | History | Annotate | Line # | Download | only in hpc
if_sq.c revision 1.23
      1  1.22   rumble /*	$NetBSD: if_sq.c,v 1.23 2004/12/29 06:57:52 rumble Exp $	*/
      2   1.1  thorpej 
      3   1.1  thorpej /*
      4   1.1  thorpej  * Copyright (c) 2001 Rafal K. Boni
      5   1.1  thorpej  * Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
      6   1.1  thorpej  * All rights reserved.
      7   1.1  thorpej  *
      8  1.10   simonb  * Portions of this code are derived from software contributed to The
      9  1.10   simonb  * NetBSD Foundation by Jason R. Thorpe of the Numerical Aerospace
     10   1.1  thorpej  * Simulation Facility, NASA Ames Research Center.
     11  1.10   simonb  *
     12   1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     13   1.1  thorpej  * modification, are permitted provided that the following conditions
     14   1.1  thorpej  * are met:
     15   1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     16   1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     17   1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     18   1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     19   1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     20   1.1  thorpej  * 3. The name of the author may not be used to endorse or promote products
     21   1.1  thorpej  *    derived from this software without specific prior written permission.
     22  1.10   simonb  *
     23   1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24   1.1  thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25   1.1  thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26   1.1  thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27   1.1  thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28   1.1  thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29   1.1  thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30   1.1  thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31   1.1  thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32   1.1  thorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33   1.1  thorpej  */
     34  1.17    lukem 
     35  1.17    lukem #include <sys/cdefs.h>
     36  1.22   rumble __KERNEL_RCSID(0, "$NetBSD: if_sq.c,v 1.23 2004/12/29 06:57:52 rumble Exp $");
     37   1.1  thorpej 
     38   1.1  thorpej #include "bpfilter.h"
     39   1.1  thorpej 
     40   1.1  thorpej #include <sys/param.h>
     41  1.10   simonb #include <sys/systm.h>
     42   1.1  thorpej #include <sys/device.h>
     43   1.1  thorpej #include <sys/callout.h>
     44  1.10   simonb #include <sys/mbuf.h>
     45   1.1  thorpej #include <sys/malloc.h>
     46   1.1  thorpej #include <sys/kernel.h>
     47   1.1  thorpej #include <sys/socket.h>
     48   1.1  thorpej #include <sys/ioctl.h>
     49   1.1  thorpej #include <sys/errno.h>
     50   1.1  thorpej #include <sys/syslog.h>
     51   1.1  thorpej 
     52   1.1  thorpej #include <uvm/uvm_extern.h>
     53   1.1  thorpej 
     54   1.1  thorpej #include <machine/endian.h>
     55   1.1  thorpej 
     56   1.1  thorpej #include <net/if.h>
     57   1.1  thorpej #include <net/if_dl.h>
     58   1.1  thorpej #include <net/if_media.h>
     59   1.1  thorpej #include <net/if_ether.h>
     60   1.1  thorpej 
     61  1.10   simonb #if NBPFILTER > 0
     62   1.1  thorpej #include <net/bpf.h>
     63  1.10   simonb #endif
     64   1.1  thorpej 
     65   1.1  thorpej #include <machine/bus.h>
     66   1.1  thorpej #include <machine/intr.h>
     67   1.1  thorpej 
     68   1.1  thorpej #include <dev/ic/seeq8003reg.h>
     69   1.1  thorpej 
     70   1.1  thorpej #include <sgimips/hpc/sqvar.h>
     71   1.1  thorpej #include <sgimips/hpc/hpcvar.h>
     72   1.1  thorpej #include <sgimips/hpc/hpcreg.h>
     73   1.1  thorpej 
     74   1.5  thorpej #include <dev/arcbios/arcbios.h>
     75   1.5  thorpej #include <dev/arcbios/arcbiosvar.h>
     76   1.5  thorpej 
     77   1.1  thorpej #define static
     78   1.1  thorpej 
     79   1.1  thorpej /*
     80   1.1  thorpej  * Short TODO list:
     81   1.1  thorpej  *	(1) Do counters for bad-RX packets.
     82   1.9    rafal  *	(2) Allow multi-segment transmits, instead of copying to a single,
     83   1.1  thorpej  *	    contiguous mbuf.
     84   1.9    rafal  *	(3) Verify sq_stop() turns off enough stuff; I was still getting
     85   1.1  thorpej  *	    seeq interrupts after sq_stop().
     86  1.20   sekiya  *	(4) Implement EDLC modes: especially packet auto-pad and simplex
     87   1.1  thorpej  *	    mode.
     88  1.20   sekiya  *	(5) Should the driver filter out its own transmissions in non-EDLC
     89   1.1  thorpej  *	    mode?
     90  1.20   sekiya  *	(6) Multicast support -- multicast filter, address management, ...
     91  1.20   sekiya  *	(7) Deal with RB0 (recv buffer overflow) on reception.  Will need
     92   1.1  thorpej  *	    to figure out if RB0 is read-only as stated in one spot in the
     93   1.1  thorpej  *	    HPC spec or read-write (ie, is the 'write a one to clear it')
     94   1.1  thorpej  *	    the correct thing?
     95   1.1  thorpej  */
     96   1.1  thorpej 
     97  1.20   sekiya #if defined(SQ_DEBUG)
     98  1.20   sekiya  int sq_debug = 0;
     99  1.20   sekiya  #define SQ_DPRINTF(x) if (sq_debug) printf x
    100  1.20   sekiya #else
    101  1.20   sekiya  #define SQ_DPRINTF(x)
    102  1.20   sekiya #endif
    103  1.20   sekiya 
    104   1.1  thorpej static int	sq_match(struct device *, struct cfdata *, void *);
    105   1.1  thorpej static void	sq_attach(struct device *, struct device *, void *);
    106   1.1  thorpej static int	sq_init(struct ifnet *);
    107   1.1  thorpej static void	sq_start(struct ifnet *);
    108   1.1  thorpej static void	sq_stop(struct ifnet *, int);
    109   1.1  thorpej static void	sq_watchdog(struct ifnet *);
    110   1.1  thorpej static int	sq_ioctl(struct ifnet *, u_long, caddr_t);
    111   1.1  thorpej 
    112   1.3  thorpej static void	sq_set_filter(struct sq_softc *);
    113   1.1  thorpej static int	sq_intr(void *);
    114   1.1  thorpej static int	sq_rxintr(struct sq_softc *);
    115   1.1  thorpej static int	sq_txintr(struct sq_softc *);
    116  1.23   rumble static void	sq_txring_hpc1(struct sq_softc *);
    117  1.23   rumble static void	sq_txring_hpc3(struct sq_softc *);
    118   1.1  thorpej static void	sq_reset(struct sq_softc *);
    119   1.1  thorpej static int 	sq_add_rxbuf(struct sq_softc *, int);
    120   1.1  thorpej static void 	sq_dump_buffer(u_int32_t addr, u_int32_t len);
    121  1.22   rumble static void	sq_trace_dump(struct sq_softc *);
    122   1.1  thorpej 
    123   1.1  thorpej static void	enaddr_aton(const char*, u_int8_t*);
    124   1.1  thorpej 
    125  1.14  thorpej CFATTACH_DECL(sq, sizeof(struct sq_softc),
    126  1.14  thorpej     sq_match, sq_attach, NULL, NULL);
    127   1.1  thorpej 
    128  1.16   bouyer #define        ETHER_PAD_LEN (ETHER_MIN_LEN - ETHER_CRC_LEN)
    129  1.16   bouyer 
    130   1.1  thorpej static int
    131   1.8  thorpej sq_match(struct device *parent, struct cfdata *cf, void *aux)
    132   1.1  thorpej {
    133   1.8  thorpej 	struct hpc_attach_args *ha = aux;
    134   1.8  thorpej 
    135  1.12  thorpej 	if (strcmp(ha->ha_name, cf->cf_name) == 0)
    136   1.8  thorpej 		return (1);
    137   1.8  thorpej 
    138   1.8  thorpej 	return (0);
    139   1.1  thorpej }
    140   1.1  thorpej 
    141   1.1  thorpej static void
    142   1.1  thorpej sq_attach(struct device *parent, struct device *self, void *aux)
    143   1.1  thorpej {
    144   1.1  thorpej 	int i, err;
    145   1.1  thorpej 	char* macaddr;
    146   1.1  thorpej 	struct sq_softc *sc = (void *)self;
    147   1.1  thorpej 	struct hpc_attach_args *haa = aux;
    148  1.10   simonb 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    149   1.1  thorpej 
    150   1.8  thorpej 	sc->sc_hpct = haa->ha_st;
    151  1.20   sekiya 	sc->hpc_regs = haa->hpc_regs;      /* HPC register definitions */
    152  1.20   sekiya 
    153   1.8  thorpej 	if ((err = bus_space_subregion(haa->ha_st, haa->ha_sh,
    154  1.10   simonb 				       haa->ha_dmaoff,
    155  1.20   sekiya 				       sc->hpc_regs->enet_regs_size,
    156   1.1  thorpej 				       &sc->sc_hpch)) != 0) {
    157   1.1  thorpej 		printf(": unable to map HPC DMA registers, error = %d\n", err);
    158   1.1  thorpej 		goto fail_0;
    159   1.1  thorpej 	}
    160   1.1  thorpej 
    161   1.8  thorpej 	sc->sc_regt = haa->ha_st;
    162   1.8  thorpej 	if ((err = bus_space_subregion(haa->ha_st, haa->ha_sh,
    163  1.10   simonb 				       haa->ha_devoff,
    164  1.20   sekiya 				       sc->hpc_regs->enet_devregs_size,
    165   1.1  thorpej 				       &sc->sc_regh)) != 0) {
    166   1.1  thorpej 		printf(": unable to map Seeq registers, error = %d\n", err);
    167   1.1  thorpej 		goto fail_0;
    168   1.1  thorpej 	}
    169   1.1  thorpej 
    170   1.8  thorpej 	sc->sc_dmat = haa->ha_dmat;
    171   1.1  thorpej 
    172  1.10   simonb 	if ((err = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct sq_control),
    173  1.10   simonb 				    PAGE_SIZE, PAGE_SIZE, &sc->sc_cdseg,
    174   1.1  thorpej 				    1, &sc->sc_ncdseg, BUS_DMA_NOWAIT)) != 0) {
    175   1.1  thorpej 		printf(": unable to allocate control data, error = %d\n", err);
    176   1.1  thorpej 		goto fail_0;
    177   1.1  thorpej 	}
    178   1.1  thorpej 
    179   1.1  thorpej 	if ((err = bus_dmamem_map(sc->sc_dmat, &sc->sc_cdseg, sc->sc_ncdseg,
    180  1.10   simonb 				  sizeof(struct sq_control),
    181  1.10   simonb 				  (caddr_t *)&sc->sc_control,
    182   1.1  thorpej 				  BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    183   1.1  thorpej 		printf(": unable to map control data, error = %d\n", err);
    184   1.1  thorpej 		goto fail_1;
    185   1.1  thorpej 	}
    186   1.1  thorpej 
    187   1.1  thorpej 	if ((err = bus_dmamap_create(sc->sc_dmat, sizeof(struct sq_control),
    188   1.1  thorpej 				     1, sizeof(struct sq_control), PAGE_SIZE,
    189   1.1  thorpej 				     BUS_DMA_NOWAIT, &sc->sc_cdmap)) != 0) {
    190   1.1  thorpej 		printf(": unable to create DMA map for control data, error "
    191   1.1  thorpej 			"= %d\n", err);
    192   1.1  thorpej 		goto fail_2;
    193   1.1  thorpej 	}
    194   1.1  thorpej 
    195   1.1  thorpej 	if ((err = bus_dmamap_load(sc->sc_dmat, sc->sc_cdmap, sc->sc_control,
    196  1.10   simonb 				   sizeof(struct sq_control),
    197   1.1  thorpej 				   NULL, BUS_DMA_NOWAIT)) != 0) {
    198   1.1  thorpej 		printf(": unable to load DMA map for control data, error "
    199   1.1  thorpej 			"= %d\n", err);
    200   1.1  thorpej 		goto fail_3;
    201   1.1  thorpej 	}
    202   1.1  thorpej 
    203   1.7  thorpej 	memset(sc->sc_control, 0, sizeof(struct sq_control));
    204   1.1  thorpej 
    205   1.1  thorpej 	/* Create transmit buffer DMA maps */
    206   1.1  thorpej 	for (i = 0; i < SQ_NTXDESC; i++) {
    207  1.10   simonb 	    if ((err = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
    208  1.10   simonb 					 0, BUS_DMA_NOWAIT,
    209   1.1  thorpej 					 &sc->sc_txmap[i])) != 0) {
    210  1.10   simonb 		    printf(": unable to create tx DMA map %d, error = %d\n",
    211   1.1  thorpej 			   i, err);
    212   1.1  thorpej 		    goto fail_4;
    213   1.1  thorpej 	    }
    214   1.1  thorpej 	}
    215   1.1  thorpej 
    216  1.20   sekiya 	/* Create receive buffer DMA maps */
    217   1.1  thorpej 	for (i = 0; i < SQ_NRXDESC; i++) {
    218  1.10   simonb 	    if ((err = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
    219  1.10   simonb 					 0, BUS_DMA_NOWAIT,
    220   1.1  thorpej 					 &sc->sc_rxmap[i])) != 0) {
    221  1.10   simonb 		    printf(": unable to create rx DMA map %d, error = %d\n",
    222   1.1  thorpej 			   i, err);
    223   1.1  thorpej 		    goto fail_5;
    224   1.1  thorpej 	    }
    225   1.1  thorpej 	}
    226   1.1  thorpej 
    227   1.1  thorpej 	/* Pre-allocate the receive buffers.  */
    228   1.1  thorpej 	for (i = 0; i < SQ_NRXDESC; i++) {
    229   1.1  thorpej 		if ((err = sq_add_rxbuf(sc, i)) != 0) {
    230   1.1  thorpej 			printf(": unable to allocate or map rx buffer %d\n,"
    231   1.1  thorpej 			       " error = %d\n", i, err);
    232   1.1  thorpej 			goto fail_6;
    233   1.1  thorpej 		}
    234   1.1  thorpej 	}
    235   1.1  thorpej 
    236   1.5  thorpej 	if ((macaddr = ARCBIOS->GetEnvironmentVariable("eaddr")) == NULL) {
    237   1.1  thorpej 		printf(": unable to get MAC address!\n");
    238   1.1  thorpej 		goto fail_6;
    239   1.1  thorpej 	}
    240   1.1  thorpej 
    241  1.11    rafal 	evcnt_attach_dynamic(&sc->sq_intrcnt, EVCNT_TYPE_INTR, NULL,
    242  1.11    rafal 					      self->dv_xname, "intr");
    243  1.11    rafal 
    244   1.8  thorpej 	if ((cpu_intr_establish(haa->ha_irq, IPL_NET, sq_intr, sc)) == NULL) {
    245   1.1  thorpej 		printf(": unable to establish interrupt!\n");
    246   1.1  thorpej 		goto fail_6;
    247   1.1  thorpej 	}
    248   1.1  thorpej 
    249   1.3  thorpej 	/* Reset the chip to a known state. */
    250   1.3  thorpej 	sq_reset(sc);
    251   1.3  thorpej 
    252   1.3  thorpej 	/*
    253   1.3  thorpej 	 * Determine if we're an 8003 or 80c03 by setting the first
    254   1.3  thorpej 	 * MAC address register to non-zero, and then reading it back.
    255   1.3  thorpej 	 * If it's zero, we have an 80c03, because we will have read
    256   1.3  thorpej 	 * the TxCollLSB register.
    257   1.3  thorpej 	 */
    258   1.3  thorpej 	bus_space_write_1(sc->sc_regt, sc->sc_regh, SEEQ_TXCOLLS0, 0xa5);
    259   1.3  thorpej 	if (bus_space_read_1(sc->sc_regt, sc->sc_regh, SEEQ_TXCOLLS0) == 0)
    260   1.3  thorpej 		sc->sc_type = SQ_TYPE_80C03;
    261   1.3  thorpej 	else
    262   1.3  thorpej 		sc->sc_type = SQ_TYPE_8003;
    263   1.3  thorpej 	bus_space_write_1(sc->sc_regt, sc->sc_regh, SEEQ_TXCOLLS0, 0x00);
    264   1.1  thorpej 
    265   1.3  thorpej 	printf(": SGI Seeq %s\n",
    266   1.3  thorpej 	    sc->sc_type == SQ_TYPE_80C03 ? "80c03" : "8003");
    267   1.1  thorpej 
    268   1.1  thorpej 	enaddr_aton(macaddr, sc->sc_enaddr);
    269   1.1  thorpej 
    270  1.10   simonb 	printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
    271   1.1  thorpej 					   ether_sprintf(sc->sc_enaddr));
    272   1.1  thorpej 
    273   1.7  thorpej 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    274   1.1  thorpej 	ifp->if_softc = sc;
    275   1.1  thorpej 	ifp->if_mtu = ETHERMTU;
    276   1.1  thorpej 	ifp->if_init = sq_init;
    277   1.1  thorpej 	ifp->if_stop = sq_stop;
    278   1.1  thorpej 	ifp->if_start = sq_start;
    279   1.1  thorpej 	ifp->if_ioctl = sq_ioctl;
    280   1.1  thorpej 	ifp->if_watchdog = sq_watchdog;
    281   1.3  thorpej 	ifp->if_flags = IFF_BROADCAST | IFF_NOTRAILERS | IFF_MULTICAST;
    282   1.1  thorpej 	IFQ_SET_READY(&ifp->if_snd);
    283   1.1  thorpej 
    284   1.1  thorpej 	if_attach(ifp);
    285   1.1  thorpej 	ether_ifattach(ifp, sc->sc_enaddr);
    286   1.1  thorpej 
    287  1.22   rumble 	memset(&sc->sq_trace, 0, sizeof(sc->sq_trace));
    288   1.1  thorpej 	/* Done! */
    289   1.1  thorpej 	return;
    290   1.1  thorpej 
    291   1.1  thorpej 	/*
    292   1.1  thorpej 	 * Free any resources we've allocated during the failed attach
    293   1.1  thorpej 	 * attempt.  Do this in reverse order and fall through.
    294   1.1  thorpej 	 */
    295   1.1  thorpej fail_6:
    296   1.1  thorpej 	for (i = 0; i < SQ_NRXDESC; i++) {
    297   1.1  thorpej 		if (sc->sc_rxmbuf[i] != NULL) {
    298   1.1  thorpej 			bus_dmamap_unload(sc->sc_dmat, sc->sc_rxmap[i]);
    299   1.1  thorpej 			m_freem(sc->sc_rxmbuf[i]);
    300   1.1  thorpej 		}
    301   1.1  thorpej 	}
    302   1.1  thorpej fail_5:
    303   1.1  thorpej 	for (i = 0; i < SQ_NRXDESC; i++) {
    304  1.10   simonb 	    if (sc->sc_rxmap[i] != NULL)
    305   1.1  thorpej 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_rxmap[i]);
    306   1.1  thorpej 	}
    307   1.1  thorpej fail_4:
    308   1.1  thorpej 	for (i = 0; i < SQ_NTXDESC; i++) {
    309   1.1  thorpej 	    if (sc->sc_txmap[i] !=  NULL)
    310   1.1  thorpej 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_txmap[i]);
    311   1.1  thorpej 	}
    312   1.1  thorpej 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cdmap);
    313   1.1  thorpej fail_3:
    314   1.1  thorpej 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cdmap);
    315   1.1  thorpej fail_2:
    316  1.10   simonb 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t) sc->sc_control,
    317   1.1  thorpej 				      sizeof(struct sq_control));
    318   1.1  thorpej fail_1:
    319   1.1  thorpej 	bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_ncdseg);
    320   1.1  thorpej fail_0:
    321   1.1  thorpej 	return;
    322   1.1  thorpej }
    323   1.1  thorpej 
    324   1.1  thorpej /* Set up data to get the interface up and running. */
    325   1.1  thorpej int
    326   1.1  thorpej sq_init(struct ifnet *ifp)
    327   1.1  thorpej {
    328   1.1  thorpej 	int i;
    329   1.1  thorpej 	u_int32_t reg;
    330   1.1  thorpej 	struct sq_softc *sc = ifp->if_softc;
    331   1.1  thorpej 
    332   1.1  thorpej 	/* Cancel any in-progress I/O */
    333   1.1  thorpej 	sq_stop(ifp, 0);
    334   1.1  thorpej 
    335   1.1  thorpej 	sc->sc_nextrx = 0;
    336   1.1  thorpej 
    337   1.1  thorpej 	sc->sc_nfreetx = SQ_NTXDESC;
    338   1.1  thorpej 	sc->sc_nexttx = sc->sc_prevtx = 0;
    339   1.1  thorpej 
    340  1.22   rumble 	SQ_TRACE(SQ_RESET, sc, 0, 0);
    341   1.1  thorpej 
    342   1.1  thorpej 	/* Set into 8003 mode, bank 0 to program ethernet address */
    343   1.1  thorpej 	bus_space_write_1(sc->sc_regt, sc->sc_regh, SEEQ_TXCMD, TXCMD_BANK0);
    344   1.1  thorpej 
    345   1.1  thorpej 	/* Now write the address */
    346   1.1  thorpej 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    347   1.3  thorpej 		bus_space_write_1(sc->sc_regt, sc->sc_regh, i,
    348   1.3  thorpej 		    sc->sc_enaddr[i]);
    349   1.3  thorpej 
    350   1.3  thorpej 	sc->sc_rxcmd = RXCMD_IE_CRC |
    351   1.3  thorpej 		       RXCMD_IE_DRIB |
    352   1.3  thorpej 		       RXCMD_IE_SHORT |
    353   1.3  thorpej 		       RXCMD_IE_END |
    354   1.3  thorpej 		       RXCMD_IE_GOOD;
    355   1.3  thorpej 
    356   1.3  thorpej 	/*
    357   1.3  thorpej 	 * Set the receive filter -- this will add some bits to the
    358   1.3  thorpej 	 * prototype RXCMD register.  Do this before setting the
    359   1.3  thorpej 	 * transmit config register, since we might need to switch
    360   1.3  thorpej 	 * banks.
    361   1.3  thorpej 	 */
    362   1.3  thorpej 	sq_set_filter(sc);
    363   1.1  thorpej 
    364   1.1  thorpej 	/* Set up Seeq transmit command register */
    365  1.10   simonb 	bus_space_write_1(sc->sc_regt, sc->sc_regh, SEEQ_TXCMD,
    366   1.1  thorpej 						    TXCMD_IE_UFLOW |
    367   1.1  thorpej 						    TXCMD_IE_COLL |
    368   1.1  thorpej 						    TXCMD_IE_16COLL |
    369   1.1  thorpej 						    TXCMD_IE_GOOD);
    370   1.1  thorpej 
    371   1.3  thorpej 	/* Now write the receive command register. */
    372   1.3  thorpej 	bus_space_write_1(sc->sc_regt, sc->sc_regh, SEEQ_RXCMD, sc->sc_rxcmd);
    373   1.1  thorpej 
    374   1.1  thorpej 	/* Set up HPC ethernet DMA config */
    375  1.20   sekiya 	if (sc->hpc_regs->revision == 3) {
    376  1.20   sekiya 		reg = bus_space_read_4(sc->sc_hpct, sc->sc_hpch,
    377  1.23   rumble 			 	HPC_ENETR_DMACFG);
    378  1.20   sekiya 		bus_space_write_4(sc->sc_hpct, sc->sc_hpch,
    379  1.23   rumble 				HPC_ENETR_DMACFG,
    380  1.10   simonb 			    	reg | ENETR_DMACFG_FIX_RXDC |
    381  1.10   simonb 				ENETR_DMACFG_FIX_INTR |
    382   1.2    rafal 				ENETR_DMACFG_FIX_EOP);
    383  1.20   sekiya 	}
    384   1.1  thorpej 
    385   1.1  thorpej 	/* Pass the start of the receive ring to the HPC */
    386  1.20   sekiya 	bus_space_write_4(sc->sc_hpct, sc->sc_hpch, sc->hpc_regs->enetr_ndbp,
    387   1.1  thorpej 						    SQ_CDRXADDR(sc, 0));
    388   1.1  thorpej 
    389   1.1  thorpej 	/* And turn on the HPC ethernet receive channel */
    390  1.20   sekiya 	bus_space_write_4(sc->sc_hpct, sc->sc_hpch, sc->hpc_regs->enetr_ctl,
    391  1.20   sekiya 				       sc->hpc_regs->enetr_ctl_active);
    392   1.1  thorpej 
    393  1.23   rumble 	/*
    394  1.23   rumble 	 * Turn off delayed receive interrupts on HPC1.
    395  1.23   rumble 	 * (see Hollywood HPC Specification 2.1.4.3)
    396  1.23   rumble 	 */
    397  1.23   rumble 	if (sc->hpc_regs->revision != 3)
    398  1.23   rumble 		bus_space_write_4(sc->sc_hpct, sc->sc_hpch, HPC1_ENET_INTDELAY,
    399  1.23   rumble 		    HPC1_ENET_INTDELAYVAL);
    400  1.23   rumble 
    401  1.10   simonb 	ifp->if_flags |= IFF_RUNNING;
    402   1.1  thorpej 	ifp->if_flags &= ~IFF_OACTIVE;
    403   1.1  thorpej 
    404   1.1  thorpej 	return 0;
    405   1.1  thorpej }
    406   1.1  thorpej 
    407   1.3  thorpej static void
    408   1.3  thorpej sq_set_filter(struct sq_softc *sc)
    409   1.3  thorpej {
    410   1.3  thorpej 	struct ethercom *ec = &sc->sc_ethercom;
    411   1.3  thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    412   1.3  thorpej 	struct ether_multi *enm;
    413   1.3  thorpej 	struct ether_multistep step;
    414   1.3  thorpej 
    415   1.3  thorpej 	/*
    416   1.3  thorpej 	 * Check for promiscuous mode.  Also implies
    417   1.3  thorpej 	 * all-multicast.
    418   1.3  thorpej 	 */
    419   1.3  thorpej 	if (ifp->if_flags & IFF_PROMISC) {
    420   1.3  thorpej 		sc->sc_rxcmd |= RXCMD_REC_ALL;
    421   1.3  thorpej 		ifp->if_flags |= IFF_ALLMULTI;
    422   1.3  thorpej 		return;
    423   1.3  thorpej 	}
    424   1.3  thorpej 
    425   1.3  thorpej 	/*
    426   1.3  thorpej 	 * The 8003 has no hash table.  If we have any multicast
    427   1.3  thorpej 	 * addresses on the list, enable reception of all multicast
    428   1.3  thorpej 	 * frames.
    429   1.3  thorpej 	 *
    430   1.3  thorpej 	 * XXX The 80c03 has a hash table.  We should use it.
    431   1.3  thorpej 	 */
    432   1.3  thorpej 
    433   1.3  thorpej 	ETHER_FIRST_MULTI(step, ec, enm);
    434   1.3  thorpej 
    435   1.3  thorpej 	if (enm == NULL) {
    436  1.11    rafal 		sc->sc_rxcmd &= ~RXCMD_REC_MASK;
    437   1.3  thorpej 		sc->sc_rxcmd |= RXCMD_REC_BROAD;
    438  1.11    rafal 
    439  1.11    rafal 		ifp->if_flags &= ~IFF_ALLMULTI;
    440   1.3  thorpej 		return;
    441   1.3  thorpej 	}
    442   1.3  thorpej 
    443   1.3  thorpej 	sc->sc_rxcmd |= RXCMD_REC_MULTI;
    444   1.3  thorpej 	ifp->if_flags |= IFF_ALLMULTI;
    445   1.3  thorpej }
    446   1.3  thorpej 
    447   1.1  thorpej int
    448   1.1  thorpej sq_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    449   1.1  thorpej {
    450   1.1  thorpej 	int s, error = 0;
    451   1.1  thorpej 
    452  1.22   rumble 	SQ_TRACE(SQ_IOCTL, (struct sq_softc *)ifp->if_softc, 0, 0);
    453  1.22   rumble 
    454   1.1  thorpej 	s = splnet();
    455   1.1  thorpej 
    456   1.1  thorpej 	error = ether_ioctl(ifp, cmd, data);
    457   1.1  thorpej 	if (error == ENETRESET) {
    458   1.1  thorpej 		/*
    459   1.1  thorpej 		 * Multicast list has changed; set the hardware filter
    460   1.1  thorpej 		 * accordingly.
    461   1.1  thorpej 		 */
    462  1.21  thorpej 		if (ifp->if_flags & IFF_RUNNING)
    463  1.21  thorpej 			error = sq_init(ifp);
    464  1.21  thorpej 		else
    465  1.21  thorpej 			error = 0;
    466   1.1  thorpej 	}
    467   1.1  thorpej 
    468   1.1  thorpej 	splx(s);
    469   1.1  thorpej 	return (error);
    470   1.1  thorpej }
    471   1.1  thorpej 
    472   1.1  thorpej void
    473   1.1  thorpej sq_start(struct ifnet *ifp)
    474   1.1  thorpej {
    475   1.1  thorpej 	struct sq_softc *sc = ifp->if_softc;
    476   1.1  thorpej 	u_int32_t status;
    477   1.1  thorpej 	struct mbuf *m0, *m;
    478   1.1  thorpej 	bus_dmamap_t dmamap;
    479  1.19     matt 	int err, totlen, nexttx, firsttx, lasttx = -1, ofree, seg;
    480   1.1  thorpej 
    481   1.1  thorpej 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    482   1.1  thorpej 		return;
    483   1.1  thorpej 
    484   1.1  thorpej 	/*
    485   1.1  thorpej 	 * Remember the previous number of free descriptors and
    486   1.1  thorpej 	 * the first descriptor we'll use.
    487   1.1  thorpej 	 */
    488   1.1  thorpej 	ofree = sc->sc_nfreetx;
    489   1.1  thorpej 	firsttx = sc->sc_nexttx;
    490   1.1  thorpej 
    491   1.1  thorpej 	/*
    492   1.1  thorpej 	 * Loop through the send queue, setting up transmit descriptors
    493   1.1  thorpej 	 * until we drain the queue, or use up all available transmit
    494   1.1  thorpej 	 * descriptors.
    495   1.1  thorpej 	 */
    496   1.1  thorpej 	while (sc->sc_nfreetx != 0) {
    497   1.1  thorpej 		/*
    498   1.1  thorpej 		 * Grab a packet off the queue.
    499   1.1  thorpej 		 */
    500   1.1  thorpej 		IFQ_POLL(&ifp->if_snd, m0);
    501   1.1  thorpej 		if (m0 == NULL)
    502   1.1  thorpej 			break;
    503   1.1  thorpej 		m = NULL;
    504   1.1  thorpej 
    505   1.1  thorpej 		dmamap = sc->sc_txmap[sc->sc_nexttx];
    506   1.1  thorpej 
    507   1.1  thorpej 		/*
    508   1.1  thorpej 		 * Load the DMA map.  If this fails, the packet either
    509   1.1  thorpej 		 * didn't fit in the alloted number of segments, or we were
    510   1.1  thorpej 		 * short on resources.  In this case, we'll copy and try
    511   1.1  thorpej 		 * again.
    512  1.16   bouyer 		 * Also copy it if we need to pad, so that we are sure there
    513  1.16   bouyer 		 * is room for the pad buffer.
    514  1.16   bouyer 		 * XXX the right way of doing this is to use a static buffer
    515  1.16   bouyer 		 * for padding and adding it to the transmit descriptor (see
    516  1.16   bouyer 		 * sys/dev/pci/if_tl.c for example). We can't do this here yet
    517  1.16   bouyer 		 * because we can't send packets with more than one fragment.
    518   1.1  thorpej 		 */
    519  1.16   bouyer 		if (m0->m_pkthdr.len < ETHER_PAD_LEN ||
    520  1.16   bouyer 		    bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
    521   1.1  thorpej 						      BUS_DMA_NOWAIT) != 0) {
    522   1.1  thorpej 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    523   1.1  thorpej 			if (m == NULL) {
    524   1.1  thorpej 				printf("%s: unable to allocate Tx mbuf\n",
    525   1.1  thorpej 				    sc->sc_dev.dv_xname);
    526   1.1  thorpej 				break;
    527   1.1  thorpej 			}
    528   1.1  thorpej 			if (m0->m_pkthdr.len > MHLEN) {
    529   1.1  thorpej 				MCLGET(m, M_DONTWAIT);
    530   1.1  thorpej 				if ((m->m_flags & M_EXT) == 0) {
    531   1.1  thorpej 					printf("%s: unable to allocate Tx "
    532   1.1  thorpej 					    "cluster\n", sc->sc_dev.dv_xname);
    533   1.1  thorpej 					m_freem(m);
    534   1.1  thorpej 					break;
    535   1.1  thorpej 				}
    536   1.1  thorpej 			}
    537   1.1  thorpej 
    538   1.1  thorpej 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
    539  1.16   bouyer 			if (m0->m_pkthdr.len < ETHER_PAD_LEN) {
    540  1.16   bouyer 				memset(mtod(m, char *) + m0->m_pkthdr.len, 0,
    541  1.16   bouyer 				    ETHER_PAD_LEN - m0->m_pkthdr.len);
    542  1.16   bouyer 				m->m_pkthdr.len = m->m_len = ETHER_PAD_LEN;
    543  1.18  tsutsui 			} else
    544  1.16   bouyer 				m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
    545   1.1  thorpej 
    546  1.10   simonb 			if ((err = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
    547   1.1  thorpej 						m, BUS_DMA_NOWAIT)) != 0) {
    548   1.1  thorpej 				printf("%s: unable to load Tx buffer, "
    549   1.1  thorpej 				    "error = %d\n", sc->sc_dev.dv_xname, err);
    550   1.1  thorpej 				break;
    551   1.1  thorpej 			}
    552   1.1  thorpej 		}
    553   1.1  thorpej 
    554   1.1  thorpej 		/*
    555   1.1  thorpej 		 * Ensure we have enough descriptors free to describe
    556   1.1  thorpej 		 * the packet.
    557   1.1  thorpej 		 */
    558   1.1  thorpej 		if (dmamap->dm_nsegs > sc->sc_nfreetx) {
    559   1.1  thorpej 			/*
    560   1.1  thorpej 			 * Not enough free descriptors to transmit this
    561   1.1  thorpej 			 * packet.  We haven't committed to anything yet,
    562   1.1  thorpej 			 * so just unload the DMA map, put the packet
    563   1.1  thorpej 			 * back on the queue, and punt.  Notify the upper
    564   1.1  thorpej 			 * layer that there are no more slots left.
    565   1.1  thorpej 			 *
    566   1.1  thorpej 			 * XXX We could allocate an mbuf and copy, but
    567   1.1  thorpej 			 * XXX it is worth it?
    568   1.1  thorpej 			 */
    569   1.1  thorpej 			ifp->if_flags |= IFF_OACTIVE;
    570   1.1  thorpej 			bus_dmamap_unload(sc->sc_dmat, dmamap);
    571   1.1  thorpej 			if (m != NULL)
    572   1.1  thorpej 				m_freem(m);
    573   1.1  thorpej 			break;
    574   1.1  thorpej 		}
    575   1.1  thorpej 
    576   1.1  thorpej 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    577  1.16   bouyer #if NBPFILTER > 0
    578  1.16   bouyer 		/*
    579  1.16   bouyer 		 * Pass the packet to any BPF listeners.
    580  1.16   bouyer 		 */
    581  1.16   bouyer 		if (ifp->if_bpf)
    582  1.16   bouyer 			bpf_mtap(ifp->if_bpf, m0);
    583  1.16   bouyer #endif /* NBPFILTER > 0 */
    584   1.1  thorpej 		if (m != NULL) {
    585   1.1  thorpej 			m_freem(m0);
    586   1.1  thorpej 			m0 = m;
    587   1.1  thorpej 		}
    588   1.1  thorpej 
    589   1.1  thorpej 		/*
    590   1.1  thorpej 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
    591   1.1  thorpej 		 */
    592   1.1  thorpej 
    593  1.22   rumble 		SQ_TRACE(SQ_ENQUEUE, sc, sc->sc_nexttx, 0);
    594  1.22   rumble 
    595   1.1  thorpej 		/* Sync the DMA map. */
    596   1.1  thorpej 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
    597   1.1  thorpej 		    BUS_DMASYNC_PREWRITE);
    598   1.1  thorpej 
    599   1.1  thorpej 		/*
    600   1.1  thorpej 		 * Initialize the transmit descriptors.
    601   1.1  thorpej 		 */
    602   1.1  thorpej 		for (nexttx = sc->sc_nexttx, seg = 0, totlen = 0;
    603   1.1  thorpej 		     seg < dmamap->dm_nsegs;
    604   1.1  thorpej 		     seg++, nexttx = SQ_NEXTTX(nexttx)) {
    605  1.20   sekiya 			if (sc->hpc_regs->revision == 3) {
    606  1.20   sekiya 				sc->sc_txdesc[nexttx].hpc3_hdd_bufptr =
    607  1.20   sekiya 					    dmamap->dm_segs[seg].ds_addr;
    608  1.20   sekiya 				sc->sc_txdesc[nexttx].hpc3_hdd_ctl =
    609  1.20   sekiya 					    dmamap->dm_segs[seg].ds_len;
    610  1.20   sekiya 			} else {
    611  1.20   sekiya 				sc->sc_txdesc[nexttx].hpc1_hdd_bufptr =
    612   1.1  thorpej 					    dmamap->dm_segs[seg].ds_addr;
    613  1.20   sekiya 				sc->sc_txdesc[nexttx].hpc1_hdd_ctl =
    614   1.1  thorpej 					    dmamap->dm_segs[seg].ds_len;
    615  1.20   sekiya 			}
    616  1.10   simonb 			sc->sc_txdesc[nexttx].hdd_descptr=
    617   1.1  thorpej 					    SQ_CDTXADDR(sc, SQ_NEXTTX(nexttx));
    618  1.10   simonb 			lasttx = nexttx;
    619   1.1  thorpej 			totlen += dmamap->dm_segs[seg].ds_len;
    620   1.1  thorpej 		}
    621   1.1  thorpej 
    622   1.1  thorpej 		/* Last descriptor gets end-of-packet */
    623  1.19     matt 		KASSERT(lasttx != -1);
    624  1.20   sekiya 		if (sc->hpc_regs->revision == 3)
    625  1.20   sekiya 			sc->sc_txdesc[lasttx].hpc3_hdd_ctl |= HDD_CTL_EOPACKET;
    626  1.20   sekiya 		else
    627  1.20   sekiya 			sc->sc_txdesc[lasttx].hpc1_hdd_ctl |=
    628  1.20   sekiya 							HPC1_HDD_CTL_EOPACKET;
    629   1.1  thorpej 
    630  1.20   sekiya 		SQ_DPRINTF(("%s: transmit %d-%d, len %d\n", sc->sc_dev.dv_xname,
    631   1.1  thorpej 						       sc->sc_nexttx, lasttx,
    632  1.20   sekiya 						       totlen));
    633   1.1  thorpej 
    634   1.1  thorpej 		if (ifp->if_flags & IFF_DEBUG) {
    635   1.1  thorpej 			printf("     transmit chain:\n");
    636   1.1  thorpej 			for (seg = sc->sc_nexttx;; seg = SQ_NEXTTX(seg)) {
    637   1.1  thorpej 				printf("     descriptor %d:\n", seg);
    638   1.1  thorpej 				printf("       hdd_bufptr:      0x%08x\n",
    639  1.20   sekiya 					(sc->hpc_regs->revision == 3) ?
    640  1.20   sekiya 					    sc->sc_txdesc[seg].hpc3_hdd_bufptr :
    641  1.20   sekiya 					    sc->sc_txdesc[seg].hpc1_hdd_bufptr);
    642   1.1  thorpej 				printf("       hdd_ctl: 0x%08x\n",
    643  1.20   sekiya 					(sc->hpc_regs->revision == 3) ?
    644  1.20   sekiya 					    sc->sc_txdesc[seg].hpc3_hdd_ctl:
    645  1.20   sekiya 					    sc->sc_txdesc[seg].hpc1_hdd_ctl);
    646   1.1  thorpej 				printf("       hdd_descptr:      0x%08x\n",
    647   1.1  thorpej 					sc->sc_txdesc[seg].hdd_descptr);
    648   1.1  thorpej 
    649   1.1  thorpej 				if (seg == lasttx)
    650   1.1  thorpej 					break;
    651   1.1  thorpej 			}
    652   1.1  thorpej 		}
    653   1.1  thorpej 
    654   1.1  thorpej 		/* Sync the descriptors we're using. */
    655   1.1  thorpej 		SQ_CDTXSYNC(sc, sc->sc_nexttx, dmamap->dm_nsegs,
    656   1.1  thorpej 				BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    657   1.1  thorpej 
    658   1.1  thorpej 		/* Store a pointer to the packet so we can free it later */
    659   1.1  thorpej 		sc->sc_txmbuf[sc->sc_nexttx] = m0;
    660   1.1  thorpej 
    661   1.1  thorpej 		/* Advance the tx pointer. */
    662   1.1  thorpej 		sc->sc_nfreetx -= dmamap->dm_nsegs;
    663   1.1  thorpej 		sc->sc_nexttx = nexttx;
    664   1.1  thorpej 	}
    665   1.1  thorpej 
    666   1.1  thorpej 	/* All transmit descriptors used up, let upper layers know */
    667   1.1  thorpej 	if (sc->sc_nfreetx == 0)
    668   1.1  thorpej 		ifp->if_flags |= IFF_OACTIVE;
    669   1.1  thorpej 
    670   1.1  thorpej 	if (sc->sc_nfreetx != ofree) {
    671  1.20   sekiya 		SQ_DPRINTF(("%s: %d packets enqueued, first %d, INTR on %d\n",
    672   1.1  thorpej 			    sc->sc_dev.dv_xname, lasttx - firsttx + 1,
    673  1.20   sekiya 			    firsttx, lasttx));
    674   1.1  thorpej 
    675   1.1  thorpej 		/*
    676   1.1  thorpej 		 * Cause a transmit interrupt to happen on the
    677   1.1  thorpej 		 * last packet we enqueued, mark it as the last
    678   1.1  thorpej 		 * descriptor.
    679  1.20   sekiya 		 *
    680  1.23   rumble 		 * HDD_CTL_INTR will generate an interrupt on
    681  1.23   rumble 		 * HPC1 by itself. HPC3 will not interrupt unless
    682  1.23   rumble 		 * HDD_CTL_EOPACKET is set as well.
    683   1.1  thorpej 		 */
    684  1.19     matt 		KASSERT(lasttx != -1);
    685  1.20   sekiya 		if (sc->hpc_regs->revision == 3) {
    686  1.20   sekiya 			sc->sc_txdesc[lasttx].hpc3_hdd_ctl |= HDD_CTL_INTR |
    687  1.20   sekiya 							HDD_CTL_EOCHAIN;
    688  1.20   sekiya 		} else {
    689  1.20   sekiya 			sc->sc_txdesc[lasttx].hpc1_hdd_ctl |= HPC1_HDD_CTL_INTR;
    690  1.20   sekiya 			sc->sc_txdesc[lasttx].hpc1_hdd_bufptr |=
    691  1.20   sekiya 							HPC1_HDD_CTL_EOCHAIN;
    692  1.20   sekiya 		}
    693  1.20   sekiya 
    694  1.10   simonb 		SQ_CDTXSYNC(sc, lasttx, 1,
    695   1.1  thorpej 				BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    696   1.1  thorpej 
    697  1.10   simonb 		/*
    698   1.1  thorpej 		 * There is a potential race condition here if the HPC
    699  1.10   simonb 		 * DMA channel is active and we try and either update
    700  1.10   simonb 		 * the 'next descriptor' pointer in the HPC PIO space
    701   1.1  thorpej 		 * or the 'next descriptor' pointer in a previous desc-
    702   1.1  thorpej 		 * riptor.
    703   1.1  thorpej 		 *
    704  1.10   simonb 		 * To avoid this, if the channel is active, we rely on
    705   1.1  thorpej 		 * the transmit interrupt routine noticing that there
    706  1.10   simonb 		 * are more packets to send and restarting the HPC DMA
    707   1.1  thorpej 		 * engine, rather than mucking with the DMA state here.
    708   1.1  thorpej 		 */
    709  1.10   simonb 		status = bus_space_read_4(sc->sc_hpct, sc->sc_hpch,
    710  1.20   sekiya 				sc->hpc_regs->enetx_ctl);
    711   1.1  thorpej 
    712  1.20   sekiya 		if ((status & sc->hpc_regs->enetx_ctl_active) != 0) {
    713  1.22   rumble 			SQ_TRACE(SQ_ADD_TO_DMA, sc, firsttx, status);
    714  1.20   sekiya 
    715  1.20   sekiya 			/* NB: hpc3_hdd_ctl is also hpc1_hdd_bufptr */
    716  1.20   sekiya 			sc->sc_txdesc[SQ_PREVTX(firsttx)].hpc3_hdd_ctl &=
    717   1.6  thorpej 			    ~HDD_CTL_EOCHAIN;
    718  1.20   sekiya 
    719  1.23   rumble 			if (sc->hpc_regs->revision != 3)
    720  1.23   rumble 				sc->sc_txdesc[SQ_PREVTX(firsttx)].hpc1_hdd_ctl
    721  1.23   rumble 				    &= ~HPC1_HDD_CTL_INTR;
    722  1.23   rumble 
    723   1.6  thorpej 			SQ_CDTXSYNC(sc, SQ_PREVTX(firsttx),  1,
    724   1.6  thorpej 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    725  1.23   rumble 		} else if (sc->hpc_regs->revision == 3) {
    726  1.22   rumble 			SQ_TRACE(SQ_START_DMA, sc, firsttx, status);
    727   1.1  thorpej 
    728   1.6  thorpej 			bus_space_write_4(sc->sc_hpct, sc->sc_hpch,
    729  1.23   rumble 			    HPC_ENETX_NDBP, SQ_CDTXADDR(sc, firsttx));
    730  1.23   rumble 
    731  1.23   rumble 			/* Kick DMA channel into life */
    732  1.23   rumble 			bus_space_write_4(sc->sc_hpct, sc->sc_hpch,
    733  1.23   rumble 			    HPC_ENETX_CTL, ENETX_CTL_ACTIVE);
    734  1.23   rumble 		} else {
    735  1.23   rumble 			/*
    736  1.23   rumble 			 * In the HPC1 case where transmit DMA is
    737  1.23   rumble 			 * inactive, we can either kick off if
    738  1.23   rumble 			 * the ring was previously empty, or call
    739  1.23   rumble 			 * our transmit interrupt handler to
    740  1.23   rumble 			 * figure out if the ring stopped short
    741  1.23   rumble 			 * and restart at the right place.
    742  1.23   rumble 			 */
    743  1.23   rumble 			if (ofree == SQ_NTXDESC) {
    744  1.23   rumble 				SQ_TRACE(SQ_START_DMA, sc, firsttx, status);
    745  1.20   sekiya 
    746  1.20   sekiya 				bus_space_write_4(sc->sc_hpct, sc->sc_hpch,
    747  1.23   rumble 				    HPC1_ENETX_NDBP,
    748  1.23   rumble 				    SQ_CDTXADDR(sc, firsttx));
    749  1.20   sekiya 				bus_space_write_4(sc->sc_hpct, sc->sc_hpch,
    750  1.23   rumble 				    HPC1_ENETX_CFXBP, SQ_CDTXADDR(sc, firsttx));
    751  1.23   rumble 				bus_space_write_4(sc->sc_hpct, sc->sc_hpch,
    752  1.23   rumble 				    HPC1_ENETX_CBP, SQ_CDTXADDR(sc, firsttx));
    753   1.1  thorpej 
    754  1.23   rumble 				/* Kick DMA channel into life */
    755  1.23   rumble 				bus_space_write_4(sc->sc_hpct, sc->sc_hpch,
    756  1.23   rumble 				    HPC1_ENETX_CTL, HPC1_ENETX_CTL_ACTIVE);
    757  1.23   rumble 			} else
    758  1.23   rumble 				sq_txring_hpc1(sc);
    759   1.2    rafal 		}
    760   1.1  thorpej 
    761   1.6  thorpej 		/* Set a watchdog timer in case the chip flakes out. */
    762   1.6  thorpej 		ifp->if_timer = 5;
    763   1.6  thorpej 	}
    764   1.1  thorpej }
    765   1.1  thorpej 
    766   1.1  thorpej void
    767   1.1  thorpej sq_stop(struct ifnet *ifp, int disable)
    768   1.1  thorpej {
    769   1.1  thorpej 	int i;
    770   1.1  thorpej 	struct sq_softc *sc = ifp->if_softc;
    771   1.1  thorpej 
    772   1.1  thorpej 	for (i =0; i < SQ_NTXDESC; i++) {
    773   1.1  thorpej 		if (sc->sc_txmbuf[i] != NULL) {
    774   1.1  thorpej 			bus_dmamap_unload(sc->sc_dmat, sc->sc_txmap[i]);
    775   1.1  thorpej 			m_freem(sc->sc_txmbuf[i]);
    776   1.1  thorpej 			sc->sc_txmbuf[i] = NULL;
    777   1.1  thorpej 		}
    778   1.1  thorpej 	}
    779   1.1  thorpej 
    780   1.1  thorpej 	/* Clear Seeq transmit/receive command registers */
    781   1.1  thorpej 	bus_space_write_1(sc->sc_regt, sc->sc_regh, SEEQ_TXCMD, 0);
    782  1.10   simonb 	bus_space_write_1(sc->sc_regt, sc->sc_regh, SEEQ_RXCMD, 0);
    783   1.1  thorpej 
    784   1.1  thorpej 	sq_reset(sc);
    785   1.1  thorpej 
    786  1.10   simonb 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    787   1.1  thorpej 	ifp->if_timer = 0;
    788   1.1  thorpej }
    789   1.1  thorpej 
    790   1.1  thorpej /* Device timeout/watchdog routine. */
    791   1.1  thorpej void
    792   1.1  thorpej sq_watchdog(struct ifnet *ifp)
    793   1.1  thorpej {
    794   1.1  thorpej 	u_int32_t status;
    795   1.1  thorpej 	struct sq_softc *sc = ifp->if_softc;
    796   1.1  thorpej 
    797  1.20   sekiya 	status = bus_space_read_4(sc->sc_hpct, sc->sc_hpch,
    798  1.20   sekiya 				  sc->hpc_regs->enetx_ctl);
    799   1.1  thorpej 	log(LOG_ERR, "%s: device timeout (prev %d, next %d, free %d, "
    800  1.10   simonb 		     "status %08x)\n", sc->sc_dev.dv_xname, sc->sc_prevtx,
    801   1.1  thorpej 				       sc->sc_nexttx, sc->sc_nfreetx, status);
    802   1.1  thorpej 
    803   1.1  thorpej 	sq_trace_dump(sc);
    804   1.1  thorpej 
    805  1.22   rumble 	memset(&sc->sq_trace, 0, sizeof(sc->sq_trace));
    806  1.22   rumble 	sc->sq_trace_idx = 0;
    807   1.1  thorpej 
    808   1.1  thorpej 	++ifp->if_oerrors;
    809   1.1  thorpej 
    810   1.1  thorpej 	sq_init(ifp);
    811   1.1  thorpej }
    812   1.1  thorpej 
    813  1.22   rumble static void
    814  1.22   rumble sq_trace_dump(struct sq_softc *sc)
    815   1.1  thorpej {
    816   1.1  thorpej 	int i;
    817  1.22   rumble 	char *act;
    818  1.22   rumble 
    819  1.22   rumble 	for (i = 0; i < sc->sq_trace_idx; i++) {
    820  1.22   rumble 		switch (sc->sq_trace[i].action) {
    821  1.22   rumble 		case SQ_RESET:		act = "SQ_RESET";		break;
    822  1.22   rumble 		case SQ_ADD_TO_DMA:	act = "SQ_ADD_TO_DMA";		break;
    823  1.22   rumble 		case SQ_START_DMA:	act = "SQ_START_DMA";		break;
    824  1.22   rumble 		case SQ_DONE_DMA:	act = "SQ_DONE_DMA";		break;
    825  1.22   rumble 		case SQ_RESTART_DMA:	act = "SQ_RESTART_DMA";		break;
    826  1.22   rumble 		case SQ_TXINTR_ENTER:	act = "SQ_TXINTR_ENTER";	break;
    827  1.22   rumble 		case SQ_TXINTR_EXIT:	act = "SQ_TXINTR_EXIT";		break;
    828  1.22   rumble 		case SQ_TXINTR_BUSY:	act = "SQ_TXINTR_BUSY";		break;
    829  1.22   rumble 		case SQ_IOCTL:		act = "SQ_IOCTL";		break;
    830  1.22   rumble 		case SQ_ENQUEUE:	act = "SQ_ENQUEUE";		break;
    831  1.22   rumble 		default:		act = "UNKNOWN";
    832  1.22   rumble 		}
    833   1.1  thorpej 
    834  1.22   rumble 		printf("%s: [%03d] action %-16s buf %03d free %03d "
    835  1.22   rumble 		    "status %08x line %d\n", sc->sc_dev.dv_xname, i, act,
    836  1.22   rumble 		    sc->sq_trace[i].bufno, sc->sq_trace[i].freebuf,
    837  1.22   rumble 		    sc->sq_trace[i].status, sc->sq_trace[i].line);
    838   1.1  thorpej 	}
    839   1.1  thorpej }
    840   1.1  thorpej 
    841   1.1  thorpej static int
    842   1.1  thorpej sq_intr(void * arg)
    843   1.1  thorpej {
    844   1.1  thorpej 	struct sq_softc *sc = arg;
    845   1.1  thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    846   1.1  thorpej 	int handled = 0;
    847   1.1  thorpej 	u_int32_t stat;
    848   1.1  thorpej 
    849  1.20   sekiya 	stat = bus_space_read_4(sc->sc_hpct, sc->sc_hpch,
    850  1.20   sekiya 				sc->hpc_regs->enetr_reset);
    851   1.1  thorpej 
    852   1.1  thorpej 	if ((stat & 2) == 0) {
    853   1.1  thorpej 		printf("%s: Unexpected interrupt!\n", sc->sc_dev.dv_xname);
    854   1.1  thorpej 		return 0;
    855   1.1  thorpej 	}
    856   1.1  thorpej 
    857  1.20   sekiya 	bus_space_write_4(sc->sc_hpct, sc->sc_hpch,
    858  1.20   sekiya 			  sc->hpc_regs->enetr_reset, (stat | 2));
    859   1.1  thorpej 
    860   1.1  thorpej 	/*
    861   1.1  thorpej 	 * If the interface isn't running, the interrupt couldn't
    862   1.1  thorpej 	 * possibly have come from us.
    863   1.1  thorpej 	 */
    864   1.1  thorpej 	if ((ifp->if_flags & IFF_RUNNING) == 0)
    865   1.1  thorpej 		return 0;
    866  1.11    rafal 
    867  1.11    rafal 	sc->sq_intrcnt.ev_count++;
    868   1.1  thorpej 
    869   1.1  thorpej 	/* Always check for received packets */
    870   1.1  thorpej 	if (sq_rxintr(sc) != 0)
    871   1.1  thorpej 		handled++;
    872   1.1  thorpej 
    873   1.1  thorpej 	/* Only handle transmit interrupts if we actually sent something */
    874   1.1  thorpej 	if (sc->sc_nfreetx < SQ_NTXDESC) {
    875   1.1  thorpej 		sq_txintr(sc);
    876   1.1  thorpej 		handled++;
    877   1.1  thorpej 	}
    878   1.1  thorpej 
    879   1.1  thorpej #if NRND > 0
    880   1.1  thorpej 	if (handled)
    881   1.3  thorpej 		rnd_add_uint32(&sc->rnd_source, stat);
    882   1.1  thorpej #endif
    883   1.1  thorpej 	return (handled);
    884   1.1  thorpej }
    885   1.1  thorpej 
    886   1.1  thorpej static int
    887   1.1  thorpej sq_rxintr(struct sq_softc *sc)
    888   1.1  thorpej {
    889   1.1  thorpej 	int count = 0;
    890   1.1  thorpej 	struct mbuf* m;
    891   1.1  thorpej 	int i, framelen;
    892   1.1  thorpej 	u_int8_t pktstat;
    893   1.1  thorpej 	u_int32_t status;
    894  1.20   sekiya 	u_int32_t ctl_reg;
    895   1.1  thorpej 	int new_end, orig_end;
    896   1.1  thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    897   1.1  thorpej 
    898   1.1  thorpej 	for(i = sc->sc_nextrx;; i = SQ_NEXTRX(i)) {
    899  1.10   simonb 		SQ_CDRXSYNC(sc, i, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
    900   1.1  thorpej 
    901  1.10   simonb 		/* If this is a CPU-owned buffer, we're at the end of the list */
    902  1.20   sekiya 		if (sc->hpc_regs->revision == 3)
    903  1.20   sekiya 			ctl_reg = sc->sc_rxdesc[i].hpc3_hdd_ctl & HDD_CTL_OWN;
    904  1.20   sekiya 		else
    905  1.20   sekiya 			ctl_reg = sc->sc_rxdesc[i].hpc1_hdd_ctl &
    906  1.20   sekiya 							HPC1_HDD_CTL_OWN;
    907  1.20   sekiya 
    908  1.20   sekiya 		if (ctl_reg) {
    909  1.20   sekiya #if defined(SQ_DEBUG)
    910  1.10   simonb 			u_int32_t reg;
    911   1.1  thorpej 
    912  1.10   simonb 			reg = bus_space_read_4(sc->sc_hpct, sc->sc_hpch,
    913  1.20   sekiya 			    sc->hpc_regs->enetr_ctl);
    914  1.20   sekiya 			SQ_DPRINTF(("%s: rxintr: done at %d (ctl %08x)\n",
    915  1.20   sekiya 			    sc->sc_dev.dv_xname, i, reg));
    916   1.1  thorpej #endif
    917  1.10   simonb 			break;
    918  1.10   simonb 		}
    919   1.1  thorpej 
    920  1.10   simonb 		count++;
    921   1.1  thorpej 
    922  1.10   simonb 		m = sc->sc_rxmbuf[i];
    923  1.20   sekiya 		framelen = m->m_ext.ext_size - 3;
    924  1.20   sekiya 		if (sc->hpc_regs->revision == 3)
    925  1.20   sekiya 		    framelen -=
    926  1.20   sekiya 			HDD_CTL_BYTECNT(sc->sc_rxdesc[i].hpc3_hdd_ctl);
    927  1.20   sekiya 		else
    928  1.20   sekiya 		    framelen -=
    929  1.20   sekiya 			HPC1_HDD_CTL_BYTECNT(sc->sc_rxdesc[i].hpc1_hdd_ctl);
    930   1.1  thorpej 
    931  1.10   simonb 		/* Now sync the actual packet data */
    932  1.10   simonb 		bus_dmamap_sync(sc->sc_dmat, sc->sc_rxmap[i], 0,
    933  1.10   simonb 		    sc->sc_rxmap[i]->dm_mapsize, BUS_DMASYNC_POSTREAD);
    934   1.1  thorpej 
    935  1.10   simonb 		pktstat = *((u_int8_t*)m->m_data + framelen + 2);
    936   1.1  thorpej 
    937  1.10   simonb 		if ((pktstat & RXSTAT_GOOD) == 0) {
    938  1.10   simonb 			ifp->if_ierrors++;
    939   1.2    rafal 
    940  1.10   simonb 			if (pktstat & RXSTAT_OFLOW)
    941  1.10   simonb 				printf("%s: receive FIFO overflow\n",
    942  1.10   simonb 				    sc->sc_dev.dv_xname);
    943   1.1  thorpej 
    944  1.10   simonb 			bus_dmamap_sync(sc->sc_dmat, sc->sc_rxmap[i], 0,
    945  1.10   simonb 			    sc->sc_rxmap[i]->dm_mapsize,
    946  1.10   simonb 			    BUS_DMASYNC_PREREAD);
    947  1.10   simonb 			SQ_INIT_RXDESC(sc, i);
    948  1.23   rumble 			SQ_DPRINTF(("%s: sq_rxintr: buf %d no RXSTAT_GOOD\n",
    949  1.23   rumble 			    sc->sc_dev.dv_xname, i));
    950  1.10   simonb 			continue;
    951  1.10   simonb 		}
    952   1.1  thorpej 
    953  1.10   simonb 		if (sq_add_rxbuf(sc, i) != 0) {
    954  1.10   simonb 			ifp->if_ierrors++;
    955  1.10   simonb 			bus_dmamap_sync(sc->sc_dmat, sc->sc_rxmap[i], 0,
    956  1.10   simonb 			    sc->sc_rxmap[i]->dm_mapsize,
    957  1.10   simonb 			    BUS_DMASYNC_PREREAD);
    958  1.10   simonb 			SQ_INIT_RXDESC(sc, i);
    959  1.23   rumble 			SQ_DPRINTF(("%s: sq_rxintr: buf %d sq_add_rxbuf() "
    960  1.23   rumble 			    "failed\n", sc->sc_dev.dv_xname, i));
    961  1.10   simonb 			continue;
    962  1.10   simonb 		}
    963   1.1  thorpej 
    964   1.1  thorpej 
    965  1.10   simonb 		m->m_data += 2;
    966  1.10   simonb 		m->m_pkthdr.rcvif = ifp;
    967  1.10   simonb 		m->m_pkthdr.len = m->m_len = framelen;
    968   1.1  thorpej 
    969  1.10   simonb 		ifp->if_ipackets++;
    970   1.1  thorpej 
    971  1.20   sekiya 		SQ_DPRINTF(("%s: sq_rxintr: buf %d len %d\n",
    972  1.20   sekiya 			    sc->sc_dev.dv_xname, i, framelen));
    973   1.1  thorpej 
    974   1.1  thorpej #if NBPFILTER > 0
    975  1.10   simonb 		if (ifp->if_bpf)
    976  1.10   simonb 			bpf_mtap(ifp->if_bpf, m);
    977   1.1  thorpej #endif
    978  1.10   simonb 		(*ifp->if_input)(ifp, m);
    979   1.1  thorpej 	}
    980   1.1  thorpej 
    981   1.1  thorpej 
    982   1.1  thorpej 	/* If anything happened, move ring start/end pointers to new spot */
    983   1.1  thorpej 	if (i != sc->sc_nextrx) {
    984  1.20   sekiya 		/* NB: hpc3_hdd_ctl is also hpc1_hdd_bufptr */
    985  1.20   sekiya 
    986  1.10   simonb 		new_end = SQ_PREVRX(i);
    987  1.20   sekiya 		sc->sc_rxdesc[new_end].hpc3_hdd_ctl |= HDD_CTL_EOCHAIN;
    988  1.10   simonb 		SQ_CDRXSYNC(sc, new_end, BUS_DMASYNC_PREREAD |
    989  1.10   simonb 		    BUS_DMASYNC_PREWRITE);
    990   1.1  thorpej 
    991  1.10   simonb 		orig_end = SQ_PREVRX(sc->sc_nextrx);
    992  1.20   sekiya 		sc->sc_rxdesc[orig_end].hpc3_hdd_ctl &= ~HDD_CTL_EOCHAIN;
    993  1.10   simonb 		SQ_CDRXSYNC(sc, orig_end, BUS_DMASYNC_PREREAD |
    994  1.10   simonb 		    BUS_DMASYNC_PREWRITE);
    995   1.1  thorpej 
    996  1.10   simonb 		sc->sc_nextrx = i;
    997   1.1  thorpej 	}
    998   1.1  thorpej 
    999  1.20   sekiya 	status = bus_space_read_4(sc->sc_hpct, sc->sc_hpch,
   1000  1.20   sekiya 				  sc->hpc_regs->enetr_ctl);
   1001   1.1  thorpej 
   1002   1.1  thorpej 	/* If receive channel is stopped, restart it... */
   1003  1.20   sekiya 	if ((status & sc->hpc_regs->enetr_ctl_active) == 0) {
   1004  1.10   simonb 		/* Pass the start of the receive ring to the HPC */
   1005  1.10   simonb 		bus_space_write_4(sc->sc_hpct, sc->sc_hpch,
   1006  1.20   sekiya 		    sc->hpc_regs->enetr_ndbp, SQ_CDRXADDR(sc, sc->sc_nextrx));
   1007  1.10   simonb 
   1008  1.10   simonb 		/* And turn on the HPC ethernet receive channel */
   1009  1.20   sekiya 		bus_space_write_4(sc->sc_hpct, sc->sc_hpch,
   1010  1.20   sekiya 		    sc->hpc_regs->enetr_ctl, sc->hpc_regs->enetr_ctl_active);
   1011   1.1  thorpej 	}
   1012   1.1  thorpej 
   1013   1.1  thorpej 	return count;
   1014   1.1  thorpej }
   1015   1.1  thorpej 
   1016   1.1  thorpej static int
   1017   1.1  thorpej sq_txintr(struct sq_softc *sc)
   1018   1.1  thorpej {
   1019  1.20   sekiya 	int shift = 0;
   1020   1.1  thorpej 	u_int32_t status;
   1021   1.1  thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1022   1.1  thorpej 
   1023  1.20   sekiya 	if (sc->hpc_regs->revision != 3)
   1024  1.20   sekiya 		shift = 16;
   1025  1.20   sekiya 
   1026  1.20   sekiya 	status = bus_space_read_4(sc->sc_hpct, sc->sc_hpch,
   1027  1.20   sekiya 				  sc->hpc_regs->enetx_ctl) >> shift;
   1028   1.1  thorpej 
   1029  1.22   rumble 	SQ_TRACE(SQ_TXINTR_ENTER, sc, sc->sc_prevtx, status);
   1030   1.1  thorpej 
   1031  1.20   sekiya 	if ((status & ( (sc->hpc_regs->enetx_ctl_active >> shift) | TXSTAT_GOOD)) == 0) {
   1032  1.10   simonb 		if (status & TXSTAT_COLL)
   1033  1.10   simonb 			ifp->if_collisions++;
   1034   1.1  thorpej 
   1035   1.1  thorpej 		if (status & TXSTAT_UFLOW) {
   1036  1.10   simonb 			printf("%s: transmit underflow\n", sc->sc_dev.dv_xname);
   1037  1.10   simonb 			ifp->if_oerrors++;
   1038   1.1  thorpej 		}
   1039   1.1  thorpej 
   1040   1.1  thorpej 		if (status & TXSTAT_16COLL) {
   1041  1.23   rumble 			printf("%s: max collisions reached\n",
   1042  1.23   rumble 			    sc->sc_dev.dv_xname);
   1043  1.10   simonb 			ifp->if_oerrors++;
   1044  1.10   simonb 			ifp->if_collisions += 16;
   1045   1.1  thorpej 		}
   1046   1.1  thorpej 	}
   1047   1.1  thorpej 
   1048  1.23   rumble 	/* prevtx now points to next xmit packet not yet finished */
   1049  1.23   rumble 	if (sc->hpc_regs->revision == 3)
   1050  1.23   rumble 		sq_txring_hpc3(sc);
   1051  1.23   rumble 	else
   1052  1.23   rumble 		sq_txring_hpc1(sc);
   1053  1.23   rumble 
   1054  1.23   rumble 	/* If we have buffers free, let upper layers know */
   1055  1.23   rumble 	if (sc->sc_nfreetx > 0)
   1056  1.23   rumble 		ifp->if_flags &= ~IFF_OACTIVE;
   1057  1.23   rumble 
   1058  1.23   rumble 	/* If all packets have left the coop, cancel watchdog */
   1059  1.23   rumble 	if (sc->sc_nfreetx == SQ_NTXDESC)
   1060  1.23   rumble 		ifp->if_timer = 0;
   1061  1.23   rumble 
   1062  1.23   rumble 	SQ_TRACE(SQ_TXINTR_EXIT, sc, sc->sc_prevtx, status);
   1063  1.23   rumble 	sq_start(ifp);
   1064  1.23   rumble 
   1065  1.23   rumble 	return 1;
   1066  1.23   rumble }
   1067  1.23   rumble 
   1068  1.23   rumble /*
   1069  1.23   rumble  * Reclaim used transmit descriptors and restart the transmit DMA
   1070  1.23   rumble  * engine if necessary.
   1071  1.23   rumble  */
   1072  1.23   rumble static void
   1073  1.23   rumble sq_txring_hpc1(struct sq_softc *sc)
   1074  1.23   rumble {
   1075  1.23   rumble 	/*
   1076  1.23   rumble 	 * HPC1 doesn't tag transmitted descriptors, however,
   1077  1.23   rumble 	 * the NDBP register points to the next descriptor that
   1078  1.23   rumble 	 * has not yet been processed. If DMA is not in progress,
   1079  1.23   rumble 	 * we can safely reclaim all descriptors up to NDBP, and,
   1080  1.23   rumble 	 * if necessary, restart DMA at NDBP. Otherwise, if DMA
   1081  1.23   rumble 	 * is active, we can only safely reclaim up to CBP.
   1082  1.23   rumble 	 *
   1083  1.23   rumble 	 * For now, we'll only reclaim on inactive DMA and assume
   1084  1.23   rumble 	 * that a sufficiently large ring keeps us out of trouble.
   1085  1.23   rumble 	 */
   1086  1.23   rumble 	u_int32_t reclaimto, status;
   1087  1.23   rumble 	int reclaimall, i = sc->sc_prevtx;
   1088  1.23   rumble 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1089  1.23   rumble 
   1090  1.23   rumble 	status = bus_space_read_4(sc->sc_hpct, sc->sc_hpch, HPC1_ENETX_CTL);
   1091  1.23   rumble 	if (status & HPC1_ENETX_CTL_ACTIVE) {
   1092  1.23   rumble 		SQ_TRACE(SQ_TXINTR_BUSY, sc, i, status);
   1093  1.23   rumble 		return;
   1094  1.23   rumble 	} else {
   1095  1.23   rumble 		reclaimto = bus_space_read_4(sc->sc_hpct, sc->sc_hpch,
   1096  1.23   rumble 		    HPC1_ENETX_NDBP);
   1097  1.23   rumble 	}
   1098  1.23   rumble 
   1099  1.23   rumble 	if (sc->sc_nfreetx == 0 && SQ_CDTXADDR(sc, i) == reclaimto)
   1100  1.23   rumble 		reclaimall = 1;
   1101  1.23   rumble 	else
   1102  1.23   rumble 		reclaimall = 0;
   1103  1.23   rumble 
   1104  1.23   rumble 	while (sc->sc_nfreetx < SQ_NTXDESC) {
   1105  1.23   rumble 		if (SQ_CDTXADDR(sc, i) == reclaimto && !reclaimall)
   1106  1.23   rumble 			break;
   1107  1.23   rumble 
   1108  1.23   rumble 		SQ_CDTXSYNC(sc, i, sc->sc_txmap[i]->dm_nsegs,
   1109  1.23   rumble 				BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1110  1.23   rumble 
   1111  1.23   rumble 		/* Sync the packet data, unload DMA map, free mbuf */
   1112  1.23   rumble 		bus_dmamap_sync(sc->sc_dmat, sc->sc_txmap[i], 0,
   1113  1.23   rumble 				sc->sc_txmap[i]->dm_mapsize,
   1114  1.23   rumble 				BUS_DMASYNC_POSTWRITE);
   1115  1.23   rumble 		bus_dmamap_unload(sc->sc_dmat, sc->sc_txmap[i]);
   1116  1.23   rumble 		m_freem(sc->sc_txmbuf[i]);
   1117  1.23   rumble 		sc->sc_txmbuf[i] = NULL;
   1118  1.23   rumble 
   1119  1.23   rumble 		ifp->if_opackets++;
   1120  1.23   rumble 		sc->sc_nfreetx++;
   1121  1.23   rumble 
   1122  1.23   rumble 		SQ_TRACE(SQ_DONE_DMA, sc, i, status);
   1123  1.23   rumble 
   1124  1.23   rumble 		i = SQ_NEXTTX(i);
   1125  1.23   rumble 	}
   1126  1.23   rumble 
   1127  1.23   rumble 	if (sc->sc_nfreetx < SQ_NTXDESC) {
   1128  1.23   rumble 		SQ_TRACE(SQ_RESTART_DMA, sc, i, status);
   1129  1.23   rumble 
   1130  1.23   rumble 		KASSERT(reclaimto == SQ_CDTXADDR(sc, i));
   1131  1.23   rumble 
   1132  1.23   rumble 		bus_space_write_4(sc->sc_hpct, sc->sc_hpch, HPC1_ENETX_CFXBP,
   1133  1.23   rumble 		    reclaimto);
   1134  1.23   rumble 		bus_space_write_4(sc->sc_hpct, sc->sc_hpch, HPC1_ENETX_CBP,
   1135  1.23   rumble 		    reclaimto);
   1136  1.23   rumble 
   1137  1.23   rumble 		/* Kick DMA channel into life */
   1138  1.23   rumble 		bus_space_write_4(sc->sc_hpct, sc->sc_hpch, HPC1_ENETX_CTL,
   1139  1.23   rumble 		    HPC1_ENETX_CTL_ACTIVE);
   1140  1.23   rumble 
   1141  1.23   rumble 		/*
   1142  1.23   rumble 		 * Set a watchdog timer in case the chip
   1143  1.23   rumble 		 * flakes out.
   1144  1.23   rumble 		 */
   1145  1.23   rumble 		ifp->if_timer = 5;
   1146  1.23   rumble 	}
   1147  1.23   rumble 
   1148  1.23   rumble 	sc->sc_prevtx = i;
   1149  1.23   rumble }
   1150  1.23   rumble 
   1151  1.23   rumble /*
   1152  1.23   rumble  * Reclaim used transmit descriptors and restart the transmit DMA
   1153  1.23   rumble  * engine if necessary.
   1154  1.23   rumble  */
   1155  1.23   rumble static void
   1156  1.23   rumble sq_txring_hpc3(struct sq_softc *sc)
   1157  1.23   rumble {
   1158  1.23   rumble 	/*
   1159  1.23   rumble 	 * HPC3 tags descriptors with a bit once they've been
   1160  1.23   rumble 	 * transmitted. We need only free each XMITDONE'd
   1161  1.23   rumble 	 * descriptor, and restart the DMA engine if any
   1162  1.23   rumble 	 * descriptors are left over.
   1163  1.23   rumble 	 */
   1164  1.23   rumble 	int i;
   1165  1.23   rumble 	u_int32_t status = 0;
   1166  1.23   rumble 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1167  1.23   rumble 
   1168   1.1  thorpej 	i = sc->sc_prevtx;
   1169   1.1  thorpej 	while (sc->sc_nfreetx < SQ_NTXDESC) {
   1170  1.10   simonb 		/*
   1171  1.10   simonb 		 * Check status first so we don't end up with a case of
   1172   1.2    rafal 		 * the buffer not being finished while the DMA channel
   1173   1.2    rafal 		 * has gone idle.
   1174   1.2    rafal 		 */
   1175  1.10   simonb 		status = bus_space_read_4(sc->sc_hpct, sc->sc_hpch,
   1176  1.23   rumble 		    HPC_ENETX_CTL);
   1177   1.2    rafal 
   1178   1.1  thorpej 		SQ_CDTXSYNC(sc, i, sc->sc_txmap[i]->dm_nsegs,
   1179   1.1  thorpej 				BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1180   1.1  thorpej 
   1181  1.23   rumble 		/* Check for used descriptor and restart DMA chain if needed */
   1182  1.23   rumble 		if ((sc->sc_txdesc[i].hpc3_hdd_ctl & HDD_CTL_XMITDONE) == 0) {
   1183  1.23   rumble 			if ((status & ENETX_CTL_ACTIVE) == 0) {
   1184  1.22   rumble 				SQ_TRACE(SQ_RESTART_DMA, sc, i, status);
   1185   1.1  thorpej 
   1186  1.10   simonb 				bus_space_write_4(sc->sc_hpct, sc->sc_hpch,
   1187  1.23   rumble 				  HPC_ENETX_NDBP, SQ_CDTXADDR(sc, i));
   1188   1.1  thorpej 
   1189  1.10   simonb 				/* Kick DMA channel into life */
   1190  1.10   simonb 				bus_space_write_4(sc->sc_hpct, sc->sc_hpch,
   1191  1.23   rumble 					  HPC_ENETX_CTL, ENETX_CTL_ACTIVE);
   1192   1.1  thorpej 
   1193  1.10   simonb 				/*
   1194  1.10   simonb 				 * Set a watchdog timer in case the chip
   1195  1.10   simonb 				 * flakes out.
   1196  1.10   simonb 				 */
   1197  1.10   simonb 				ifp->if_timer = 5;
   1198  1.23   rumble 			} else
   1199  1.22   rumble 				SQ_TRACE(SQ_TXINTR_BUSY, sc, i, status);
   1200  1.10   simonb 			break;
   1201   1.1  thorpej 		}
   1202   1.1  thorpej 
   1203   1.1  thorpej 		/* Sync the packet data, unload DMA map, free mbuf */
   1204  1.10   simonb 		bus_dmamap_sync(sc->sc_dmat, sc->sc_txmap[i], 0,
   1205  1.10   simonb 				sc->sc_txmap[i]->dm_mapsize,
   1206   1.1  thorpej 				BUS_DMASYNC_POSTWRITE);
   1207   1.1  thorpej 		bus_dmamap_unload(sc->sc_dmat, sc->sc_txmap[i]);
   1208   1.1  thorpej 		m_freem(sc->sc_txmbuf[i]);
   1209   1.1  thorpej 		sc->sc_txmbuf[i] = NULL;
   1210   1.1  thorpej 
   1211   1.1  thorpej 		ifp->if_opackets++;
   1212   1.1  thorpej 		sc->sc_nfreetx++;
   1213   1.1  thorpej 
   1214  1.22   rumble 		SQ_TRACE(SQ_DONE_DMA, sc, i, status);
   1215   1.1  thorpej 		i = SQ_NEXTTX(i);
   1216   1.1  thorpej 	}
   1217   1.1  thorpej 
   1218  1.23   rumble 	sc->sc_prevtx = i;
   1219   1.1  thorpej }
   1220   1.1  thorpej 
   1221  1.10   simonb void
   1222   1.1  thorpej sq_reset(struct sq_softc *sc)
   1223   1.1  thorpej {
   1224   1.1  thorpej 	/* Stop HPC dma channels */
   1225  1.20   sekiya 	bus_space_write_4(sc->sc_hpct, sc->sc_hpch, sc->hpc_regs->enetr_ctl, 0);
   1226  1.20   sekiya 	bus_space_write_4(sc->sc_hpct, sc->sc_hpch, sc->hpc_regs->enetx_ctl, 0);
   1227   1.1  thorpej 
   1228  1.20   sekiya 	bus_space_write_4(sc->sc_hpct, sc->sc_hpch, sc->hpc_regs->enetr_reset, 3);
   1229  1.10   simonb 	delay(20);
   1230  1.20   sekiya 	bus_space_write_4(sc->sc_hpct, sc->sc_hpch, sc->hpc_regs->enetr_reset, 0);
   1231   1.1  thorpej }
   1232   1.1  thorpej 
   1233  1.10   simonb /* sq_add_rxbuf: Add a receive buffer to the indicated descriptor. */
   1234   1.1  thorpej int
   1235   1.1  thorpej sq_add_rxbuf(struct sq_softc *sc, int idx)
   1236   1.1  thorpej {
   1237   1.1  thorpej 	int err;
   1238   1.1  thorpej 	struct mbuf *m;
   1239   1.1  thorpej 
   1240   1.1  thorpej 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1241   1.1  thorpej 	if (m == NULL)
   1242   1.1  thorpej 		return (ENOBUFS);
   1243   1.1  thorpej 
   1244   1.1  thorpej 	MCLGET(m, M_DONTWAIT);
   1245   1.1  thorpej 	if ((m->m_flags & M_EXT) == 0) {
   1246   1.1  thorpej 		m_freem(m);
   1247   1.1  thorpej 		return (ENOBUFS);
   1248   1.1  thorpej 	}
   1249   1.1  thorpej 
   1250   1.1  thorpej 	if (sc->sc_rxmbuf[idx] != NULL)
   1251   1.1  thorpej 		bus_dmamap_unload(sc->sc_dmat, sc->sc_rxmap[idx]);
   1252   1.1  thorpej 
   1253   1.1  thorpej 	sc->sc_rxmbuf[idx] = m;
   1254   1.1  thorpej 
   1255  1.10   simonb 	if ((err = bus_dmamap_load(sc->sc_dmat, sc->sc_rxmap[idx],
   1256  1.10   simonb 				   m->m_ext.ext_buf, m->m_ext.ext_size,
   1257   1.1  thorpej 				   NULL, BUS_DMA_NOWAIT)) != 0) {
   1258   1.1  thorpej 		printf("%s: can't load rx DMA map %d, error = %d\n",
   1259   1.1  thorpej 		    sc->sc_dev.dv_xname, idx, err);
   1260   1.1  thorpej 		panic("sq_add_rxbuf");	/* XXX */
   1261   1.1  thorpej 	}
   1262   1.1  thorpej 
   1263  1.10   simonb 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rxmap[idx], 0,
   1264   1.1  thorpej 			sc->sc_rxmap[idx]->dm_mapsize, BUS_DMASYNC_PREREAD);
   1265   1.1  thorpej 
   1266   1.1  thorpej 	SQ_INIT_RXDESC(sc, idx);
   1267   1.1  thorpej 
   1268   1.1  thorpej 	return 0;
   1269   1.1  thorpej }
   1270   1.1  thorpej 
   1271  1.10   simonb void
   1272   1.1  thorpej sq_dump_buffer(u_int32_t addr, u_int32_t len)
   1273   1.1  thorpej {
   1274  1.15  thorpej 	u_int i;
   1275   1.1  thorpej 	u_char* physaddr = (char*) MIPS_PHYS_TO_KSEG1((caddr_t)addr);
   1276   1.1  thorpej 
   1277  1.10   simonb 	if (len == 0)
   1278   1.1  thorpej 		return;
   1279   1.1  thorpej 
   1280   1.1  thorpej 	printf("%p: ", physaddr);
   1281   1.1  thorpej 
   1282   1.1  thorpej 	for(i = 0; i < len; i++) {
   1283   1.1  thorpej 		printf("%02x ", *(physaddr + i) & 0xff);
   1284   1.1  thorpej 		if ((i % 16) ==  15 && i != len - 1)
   1285   1.1  thorpej 		    printf("\n%p: ", physaddr + i);
   1286   1.1  thorpej 	}
   1287   1.1  thorpej 
   1288   1.1  thorpej 	printf("\n");
   1289   1.1  thorpej }
   1290   1.1  thorpej 
   1291  1.10   simonb void
   1292   1.1  thorpej enaddr_aton(const char* str, u_int8_t* eaddr)
   1293   1.1  thorpej {
   1294   1.1  thorpej 	int i;
   1295   1.1  thorpej 	char c;
   1296   1.1  thorpej 
   1297   1.1  thorpej 	for(i = 0; i < ETHER_ADDR_LEN; i++) {
   1298   1.1  thorpej 		if (*str == ':')
   1299   1.1  thorpej 			str++;
   1300   1.1  thorpej 
   1301   1.1  thorpej 		c = *str++;
   1302   1.1  thorpej 		if (isdigit(c)) {
   1303   1.1  thorpej 			eaddr[i] = (c - '0');
   1304   1.1  thorpej 		} else if (isxdigit(c)) {
   1305   1.1  thorpej 			eaddr[i] = (toupper(c) + 10 - 'A');
   1306   1.1  thorpej 		}
   1307   1.1  thorpej 
   1308   1.1  thorpej 		c = *str++;
   1309   1.1  thorpej 		if (isdigit(c)) {
   1310   1.1  thorpej 			eaddr[i] = (eaddr[i] << 4) | (c - '0');
   1311   1.1  thorpej 		} else if (isxdigit(c)) {
   1312   1.1  thorpej 			eaddr[i] = (eaddr[i] << 4) | (toupper(c) + 10 - 'A');
   1313   1.1  thorpej 		}
   1314   1.1  thorpej 	}
   1315   1.1  thorpej }
   1316