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