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