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