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