Home | History | Annotate | Line # | Download | only in dev
pq3etsec.c revision 1.54
      1 /*	$NetBSD: pq3etsec.c,v 1.54 2021/04/24 23:36:46 thorpej Exp $	*/
      2 /*-
      3  * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by Raytheon BBN Technologies Corp and Defense Advanced Research Projects
      8  * Agency and which was developed by Matt Thomas of 3am Software Foundry.
      9  *
     10  * This material is based upon work supported by the Defense Advanced Research
     11  * Projects Agency and Space and Naval Warfare Systems Center, Pacific, under
     12  * Contract No. N66001-09-C-2073.
     13  * Approved for Public Release, Distribution Unlimited
     14  *
     15  * Redistribution and use in source and binary forms, with or without
     16  * modification, are permitted provided that the following conditions
     17  * are met:
     18  * 1. Redistributions of source code must retain the above copyright
     19  *    notice, this list of conditions and the following disclaimer.
     20  * 2. Redistributions in binary form must reproduce the above copyright
     21  *    notice, this list of conditions and the following disclaimer in the
     22  *    documentation and/or other materials provided with the distribution.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     27  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     28  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     34  * POSSIBILITY OF SUCH DAMAGE.
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: pq3etsec.c,v 1.54 2021/04/24 23:36:46 thorpej Exp $");
     39 
     40 #ifdef _KERNEL_OPT
     41 #include "opt_inet.h"
     42 #include "opt_mpc85xx.h"
     43 #include "opt_multiprocessor.h"
     44 #include "opt_net_mpsafe.h"
     45 #endif
     46 
     47 #include <sys/param.h>
     48 #include <sys/cpu.h>
     49 #include <sys/device.h>
     50 #include <sys/mbuf.h>
     51 #include <sys/ioctl.h>
     52 #include <sys/intr.h>
     53 #include <sys/bus.h>
     54 #include <sys/kernel.h>
     55 #include <sys/kmem.h>
     56 #include <sys/proc.h>
     57 #include <sys/atomic.h>
     58 #include <sys/callout.h>
     59 #include <sys/sysctl.h>
     60 
     61 #include <sys/rndsource.h>
     62 
     63 #include <net/if.h>
     64 #include <net/if_dl.h>
     65 #include <net/if_ether.h>
     66 #include <net/if_media.h>
     67 #include <net/bpf.h>
     68 
     69 #include <dev/mii/miivar.h>
     70 
     71 #ifdef INET
     72 #include <netinet/in.h>
     73 #include <netinet/in_systm.h>
     74 #include <netinet/ip.h>
     75 #include <netinet/in_offload.h>
     76 #endif /* INET */
     77 #ifdef INET6
     78 #include <netinet6/in6.h>
     79 #include <netinet/ip6.h>
     80 #endif
     81 #include <netinet6/in6_offload.h>
     82 
     83 #include <powerpc/spr.h>
     84 #include <powerpc/booke/spr.h>
     85 #include <powerpc/booke/cpuvar.h>
     86 #include <powerpc/booke/e500var.h>
     87 #include <powerpc/booke/e500reg.h>
     88 #include <powerpc/booke/etsecreg.h>
     89 
     90 #define	M_HASFCB		M_LINK2	/* tx packet has FCB prepended */
     91 
     92 #define	ETSEC_MAXTXMBUFS	30
     93 #define	ETSEC_NTXSEGS		30
     94 #define	ETSEC_MAXRXMBUFS	511
     95 #define	ETSEC_MINRXMBUFS	32
     96 #define	ETSEC_NRXSEGS		1
     97 
     98 #define	IFCAP_RCTRL_IPCSEN	IFCAP_CSUM_IPv4_Rx
     99 #define	IFCAP_RCTRL_TUCSEN	(IFCAP_CSUM_TCPv4_Rx	\
    100 				 | IFCAP_CSUM_UDPv4_Rx	\
    101 				 | IFCAP_CSUM_TCPv6_Rx	\
    102 				 | IFCAP_CSUM_UDPv6_Rx)
    103 
    104 #define	IFCAP_TCTRL_IPCSEN	IFCAP_CSUM_IPv4_Tx
    105 #define	IFCAP_TCTRL_TUCSEN	(IFCAP_CSUM_TCPv4_Tx	\
    106 				 | IFCAP_CSUM_UDPv4_Tx	\
    107 				 | IFCAP_CSUM_TCPv6_Tx	\
    108 				 | IFCAP_CSUM_UDPv6_Tx)
    109 
    110 #define	IFCAP_ETSEC		(IFCAP_RCTRL_IPCSEN | IFCAP_RCTRL_TUCSEN      \
    111 				 | IFCAP_TCTRL_IPCSEN | IFCAP_TCTRL_TUCSEN)
    112 
    113 #define	M_CSUM_IP   (M_CSUM_CIP | M_CSUM_CTU)
    114 #define	M_CSUM_IP6  (M_CSUM_TCPv6 | M_CSUM_UDPv6)
    115 #define	M_CSUM_TUP  (M_CSUM_TCPv4 | M_CSUM_UDPv4 | M_CSUM_TCPv6 | M_CSUM_UDPv6)
    116 #define	M_CSUM_UDP  (M_CSUM_UDPv4 | M_CSUM_UDPv6)
    117 #define	M_CSUM_IP4  (M_CSUM_IPv4 | M_CSUM_UDPv4 | M_CSUM_TCPv4)
    118 #define	M_CSUM_CIP  (M_CSUM_IPv4)
    119 #define	M_CSUM_CTU  (M_CSUM_TCPv4 | M_CSUM_UDPv4 | M_CSUM_TCPv6 | M_CSUM_UDPv6)
    120 
    121 struct pq3etsec_txqueue {
    122 	bus_dmamap_t txq_descmap;
    123 	volatile struct txbd *txq_consumer;
    124 	volatile struct txbd *txq_producer;
    125 	volatile struct txbd *txq_first;
    126 	volatile struct txbd *txq_last;
    127 	struct ifqueue txq_mbufs;
    128 	struct mbuf *txq_next;
    129 #ifdef ETSEC_DEBUG
    130 	struct mbuf *txq_lmbufs[512];
    131 #endif
    132 	uint32_t txq_qmask;
    133 	uint32_t txq_free;
    134 	uint32_t txq_threshold;
    135 	uint32_t txq_lastintr;
    136 	bus_size_t txq_reg_tbase;
    137 	bus_dma_segment_t txq_descmap_seg;
    138 };
    139 
    140 struct pq3etsec_rxqueue {
    141 	bus_dmamap_t rxq_descmap;
    142 	volatile struct rxbd *rxq_consumer;
    143 	volatile struct rxbd *rxq_producer;
    144 	volatile struct rxbd *rxq_first;
    145 	volatile struct rxbd *rxq_last;
    146 	struct mbuf *rxq_mhead;
    147 	struct mbuf **rxq_mtail;
    148 	struct mbuf *rxq_mconsumer;
    149 #ifdef ETSEC_DEBUG
    150 	struct mbuf *rxq_mbufs[512];
    151 #endif
    152 	uint32_t rxq_qmask;
    153 	uint32_t rxq_inuse;
    154 	uint32_t rxq_threshold;
    155 	bus_size_t rxq_reg_rbase;
    156 	bus_size_t rxq_reg_rbptr;
    157 	bus_dma_segment_t rxq_descmap_seg;
    158 };
    159 
    160 struct pq3etsec_mapcache {
    161 	u_int dmc_nmaps;
    162 	u_int dmc_maxseg;
    163 	u_int dmc_maxmaps;
    164 	u_int dmc_maxmapsize;
    165 	bus_dmamap_t dmc_maps[0];
    166 };
    167 
    168 struct pq3etsec_softc {
    169 	device_t sc_dev;
    170 	device_t sc_mdio_dev;
    171 	struct ethercom sc_ec;
    172 #define sc_if		sc_ec.ec_if
    173 	struct mii_data sc_mii;
    174 	bus_space_tag_t sc_bst;
    175 	bus_space_handle_t sc_bsh;
    176 	bus_space_handle_t sc_mdio_bsh;
    177 	bus_dma_tag_t sc_dmat;
    178 	int sc_phy_addr;
    179 	prop_dictionary_t sc_intrmap;
    180 	uint32_t sc_intrmask;
    181 
    182 	uint32_t sc_soft_flags;
    183 #define	SOFT_RESET		0x0001
    184 #define	SOFT_RXINTR		0x0010
    185 #define	SOFT_RXBSY		0x0020
    186 #define	SOFT_TXINTR		0x0100
    187 #define	SOFT_TXERROR		0x0200
    188 
    189 	struct pq3etsec_txqueue sc_txq;
    190 	struct pq3etsec_rxqueue sc_rxq;
    191 	uint32_t sc_txerrors;
    192 	uint32_t sc_rxerrors;
    193 
    194 	size_t sc_rx_adjlen;
    195 
    196 	/*
    197 	 * Copies of various ETSEC registers.
    198 	 */
    199 	uint32_t sc_imask;
    200 	uint32_t sc_maccfg1;
    201 	uint32_t sc_maccfg2;
    202 	uint32_t sc_maxfrm;
    203 	uint32_t sc_ecntrl;
    204 	uint32_t sc_dmactrl;
    205 	uint32_t sc_macstnaddr1;
    206 	uint32_t sc_macstnaddr2;
    207 	uint32_t sc_tctrl;
    208 	uint32_t sc_rctrl;
    209 	uint32_t sc_gaddr[16];
    210 	uint64_t sc_macaddrs[15];
    211 
    212 	void *sc_tx_ih;
    213 	void *sc_rx_ih;
    214 	void *sc_error_ih;
    215 	void *sc_soft_ih;
    216 
    217 	kmutex_t *sc_lock;
    218 	kmutex_t *sc_hwlock;
    219 
    220 	struct evcnt sc_ev_tx_stall;
    221 	struct evcnt sc_ev_tx_intr;
    222 	struct evcnt sc_ev_rx_stall;
    223 	struct evcnt sc_ev_rx_intr;
    224 	struct evcnt sc_ev_error_intr;
    225 	struct evcnt sc_ev_soft_intr;
    226 	struct evcnt sc_ev_tx_pause;
    227 	struct evcnt sc_ev_rx_pause;
    228 	struct evcnt sc_ev_mii_ticks;
    229 
    230 	struct callout sc_mii_callout;
    231 	uint64_t sc_mii_last_tick;
    232 
    233 	struct ifqueue sc_rx_bufcache;
    234 	struct pq3etsec_mapcache *sc_rx_mapcache;
    235 	struct pq3etsec_mapcache *sc_tx_mapcache;
    236 
    237 	/* Interrupt Coalescing parameters */
    238 	int sc_ic_rx_time;
    239 	int sc_ic_rx_count;
    240 	int sc_ic_tx_time;
    241 	int sc_ic_tx_count;
    242 
    243 	krndsource_t rnd_source;
    244 };
    245 
    246 #define	ETSEC_IC_RX_ENABLED(sc)						\
    247 	((sc)->sc_ic_rx_time != 0 && (sc)->sc_ic_rx_count != 0)
    248 #define	ETSEC_IC_TX_ENABLED(sc)						\
    249 	((sc)->sc_ic_tx_time != 0 && (sc)->sc_ic_tx_count != 0)
    250 
    251 struct pq3mdio_softc {
    252 	device_t mdio_dev;
    253 
    254 	kmutex_t *mdio_lock;
    255 
    256 	bus_space_tag_t mdio_bst;
    257 	bus_space_handle_t mdio_bsh;
    258 };
    259 
    260 static int pq3etsec_match(device_t, cfdata_t, void *);
    261 static void pq3etsec_attach(device_t, device_t, void *);
    262 
    263 static int pq3mdio_match(device_t, cfdata_t, void *);
    264 static void pq3mdio_attach(device_t, device_t, void *);
    265 
    266 static void pq3etsec_ifstart(struct ifnet *);
    267 static void pq3etsec_ifwatchdog(struct ifnet *);
    268 static int pq3etsec_ifinit(struct ifnet *);
    269 static void pq3etsec_ifstop(struct ifnet *, int);
    270 static int pq3etsec_ifioctl(struct ifnet *, u_long, void *);
    271 
    272 static int pq3etsec_mapcache_create(struct pq3etsec_softc *,
    273     struct pq3etsec_mapcache **, size_t, size_t, size_t);
    274 static void pq3etsec_mapcache_destroy(struct pq3etsec_softc *,
    275     struct pq3etsec_mapcache *);
    276 static bus_dmamap_t pq3etsec_mapcache_get(struct pq3etsec_softc *,
    277     struct pq3etsec_mapcache *);
    278 static void pq3etsec_mapcache_put(struct pq3etsec_softc *,
    279     struct pq3etsec_mapcache *, bus_dmamap_t);
    280 
    281 static int pq3etsec_txq_attach(struct pq3etsec_softc *,
    282     struct pq3etsec_txqueue *, u_int);
    283 static void pq3etsec_txq_purge(struct pq3etsec_softc *,
    284     struct pq3etsec_txqueue *);
    285 static void pq3etsec_txq_reset(struct pq3etsec_softc *,
    286     struct pq3etsec_txqueue *);
    287 static bool pq3etsec_txq_consume(struct pq3etsec_softc *,
    288     struct pq3etsec_txqueue *);
    289 static bool pq3etsec_txq_produce(struct pq3etsec_softc *,
    290     struct pq3etsec_txqueue *, struct mbuf *m);
    291 static bool pq3etsec_txq_active_p(struct pq3etsec_softc *,
    292     struct pq3etsec_txqueue *);
    293 
    294 static int pq3etsec_rxq_attach(struct pq3etsec_softc *,
    295     struct pq3etsec_rxqueue *, u_int);
    296 static bool pq3etsec_rxq_produce(struct pq3etsec_softc *,
    297     struct pq3etsec_rxqueue *);
    298 static void pq3etsec_rxq_purge(struct pq3etsec_softc *,
    299     struct pq3etsec_rxqueue *, bool);
    300 static void pq3etsec_rxq_reset(struct pq3etsec_softc *,
    301     struct pq3etsec_rxqueue *);
    302 
    303 static void pq3etsec_mc_setup(struct pq3etsec_softc *);
    304 
    305 static void pq3etsec_mii_tick(void *);
    306 static int pq3etsec_rx_intr(void *);
    307 static int pq3etsec_tx_intr(void *);
    308 static int pq3etsec_error_intr(void *);
    309 static void pq3etsec_soft_intr(void *);
    310 
    311 static void pq3etsec_set_ic_rx(struct pq3etsec_softc *);
    312 static void pq3etsec_set_ic_tx(struct pq3etsec_softc *);
    313 
    314 static void pq3etsec_sysctl_setup(struct sysctllog **, struct pq3etsec_softc *);
    315 
    316 CFATTACH_DECL_NEW(pq3etsec, sizeof(struct pq3etsec_softc),
    317     pq3etsec_match, pq3etsec_attach, NULL, NULL);
    318 
    319 CFATTACH_DECL_NEW(pq3mdio_tsec, sizeof(struct pq3mdio_softc),
    320     pq3mdio_match, pq3mdio_attach, NULL, NULL);
    321 
    322 CFATTACH_DECL_NEW(pq3mdio_cpunode, sizeof(struct pq3mdio_softc),
    323     pq3mdio_match, pq3mdio_attach, NULL, NULL);
    324 
    325 static inline uint32_t
    326 etsec_mdio_read(struct pq3mdio_softc *mdio, bus_size_t off)
    327 {
    328 	return bus_space_read_4(mdio->mdio_bst, mdio->mdio_bsh, off);
    329 }
    330 
    331 static inline void
    332 etsec_mdio_write(struct pq3mdio_softc *mdio, bus_size_t off, uint32_t data)
    333 {
    334 	bus_space_write_4(mdio->mdio_bst, mdio->mdio_bsh, off, data);
    335 }
    336 
    337 static inline uint32_t
    338 etsec_read(struct pq3etsec_softc *sc, bus_size_t off)
    339 {
    340 	return bus_space_read_4(sc->sc_bst, sc->sc_bsh, off);
    341 }
    342 
    343 static int
    344 pq3mdio_find(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    345 {
    346 	return strcmp(cf->cf_name, "mdio") == 0;
    347 }
    348 
    349 static int
    350 pq3mdio_match(device_t parent, cfdata_t cf, void *aux)
    351 {
    352 	const uint16_t svr = (mfspr(SPR_SVR) & ~0x80000) >> 16;
    353 	const bool p1025_p = (svr == (SVR_P1025v1 >> 16)
    354 	    || svr == (SVR_P1016v1 >> 16));
    355 
    356 	if (device_is_a(parent, "cpunode")) {
    357 		if (!p1025_p
    358 		    || !e500_cpunode_submatch(parent, cf, cf->cf_name, aux))
    359 			return 0;
    360 
    361 		return 1;
    362 	}
    363 
    364 	if (device_is_a(parent, "tsec")) {
    365 		if (p1025_p
    366 		    || !e500_cpunode_submatch(parent, cf, cf->cf_name, aux))
    367 			return 0;
    368 
    369 		return 1;
    370 	}
    371 
    372 	return 0;
    373 }
    374 
    375 static void
    376 pq3mdio_attach(device_t parent, device_t self, void *aux)
    377 {
    378 	struct pq3mdio_softc * const mdio = device_private(self);
    379 	struct cpunode_attach_args * const cna = aux;
    380 	struct cpunode_locators * const cnl = &cna->cna_locs;
    381 
    382 	mdio->mdio_dev = self;
    383 	mdio->mdio_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SOFTNET);
    384 
    385 	if (device_is_a(parent, "cpunode")) {
    386 		struct cpunode_softc * const psc = device_private(parent);
    387 		psc->sc_children |= cna->cna_childmask;
    388 
    389 		mdio->mdio_bst = cna->cna_memt;
    390 		if (bus_space_map(mdio->mdio_bst, cnl->cnl_addr,
    391 				cnl->cnl_size, 0, &mdio->mdio_bsh) != 0) {
    392 			aprint_error(": error mapping registers @ %#x\n",
    393 			    cnl->cnl_addr);
    394 			return;
    395 		}
    396 	} else {
    397 		struct pq3etsec_softc * const sc = device_private(parent);
    398 
    399 		KASSERT(device_is_a(parent, "tsec"));
    400 		KASSERTMSG(cnl->cnl_addr == ETSEC1_BASE
    401 		    || cnl->cnl_addr == ETSEC2_BASE
    402 		    || cnl->cnl_addr == ETSEC3_BASE
    403 		    || cnl->cnl_addr == ETSEC4_BASE,
    404 		    "unknown tsec addr %x", cnl->cnl_addr);
    405 
    406 		mdio->mdio_bst = sc->sc_bst;
    407 		mdio->mdio_bsh = sc->sc_bsh;
    408 	}
    409 
    410 	aprint_normal("\n");
    411 }
    412 
    413 static int
    414 pq3mdio_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
    415 {
    416 	struct pq3mdio_softc * const mdio = device_private(self);
    417 	uint32_t miimcom = etsec_mdio_read(mdio, MIIMCOM);
    418 
    419 	mutex_enter(mdio->mdio_lock);
    420 
    421 	etsec_mdio_write(mdio, MIIMADD,
    422 	    __SHIFTIN(phy, MIIMADD_PHY) | __SHIFTIN(reg, MIIMADD_REG));
    423 
    424 	etsec_mdio_write(mdio, MIIMCOM, 0);	/* clear any past bits */
    425 	etsec_mdio_write(mdio, MIIMCOM, MIIMCOM_READ);
    426 
    427 	while (etsec_mdio_read(mdio, MIIMIND) != 0) {
    428 			delay(1);
    429 	}
    430 	*val = etsec_mdio_read(mdio, MIIMSTAT) &0xffff;
    431 
    432 	if (miimcom == MIIMCOM_SCAN)
    433 		etsec_mdio_write(mdio, MIIMCOM, miimcom);
    434 
    435 #if 0
    436 	aprint_normal_dev(mdio->mdio_dev, "%s: phy %d reg %d: %#x\n",
    437 	    __func__, phy, reg, data);
    438 #endif
    439 	mutex_exit(mdio->mdio_lock);
    440 	return 0;
    441 }
    442 
    443 static int
    444 pq3mdio_mii_writereg(device_t self, int phy, int reg, uint16_t data)
    445 {
    446 	struct pq3mdio_softc * const mdio = device_private(self);
    447 	uint32_t miimcom = etsec_mdio_read(mdio, MIIMCOM);
    448 
    449 #if 0
    450 	aprint_normal_dev(mdio->mdio_dev, "%s: phy %d reg %d: %#x\n",
    451 	    __func__, phy, reg, data);
    452 #endif
    453 
    454 	mutex_enter(mdio->mdio_lock);
    455 
    456 	etsec_mdio_write(mdio, MIIMADD,
    457 	    __SHIFTIN(phy, MIIMADD_PHY) | __SHIFTIN(reg, MIIMADD_REG));
    458 	etsec_mdio_write(mdio, MIIMCOM, 0);	/* clear any past bits */
    459 	etsec_mdio_write(mdio, MIIMCON, data);
    460 
    461 	int timo = 1000;	/* 1ms */
    462 	while ((etsec_mdio_read(mdio, MIIMIND) & MIIMIND_BUSY) && --timo > 0) {
    463 			delay(1);
    464 	}
    465 
    466 	if (miimcom == MIIMCOM_SCAN)
    467 		etsec_mdio_write(mdio, MIIMCOM, miimcom);
    468 
    469 	mutex_exit(mdio->mdio_lock);
    470 
    471 	return 0;
    472 }
    473 
    474 static inline void
    475 etsec_write(struct pq3etsec_softc *sc, bus_size_t off, uint32_t data)
    476 {
    477 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, off, data);
    478 }
    479 
    480 static void
    481 pq3etsec_mii_statchg(struct ifnet *ifp)
    482 {
    483 	struct pq3etsec_softc * const sc = ifp->if_softc;
    484 	struct mii_data * const mii = &sc->sc_mii;
    485 
    486 	uint32_t maccfg1 = sc->sc_maccfg1;
    487 	uint32_t maccfg2 = sc->sc_maccfg2;
    488 	uint32_t ecntrl = sc->sc_ecntrl;
    489 
    490 	maccfg1 &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
    491 	maccfg2 &= ~(MACCFG2_IFMODE | MACCFG2_FD);
    492 
    493 	if (sc->sc_mii.mii_media_active & IFM_FDX) {
    494 		maccfg2 |= MACCFG2_FD;
    495 	}
    496 
    497 	/*
    498 	 * Now deal with the flow control bits.
    499 	 */
    500 	if (IFM_SUBTYPE(mii->mii_media.ifm_cur->ifm_media) == IFM_AUTO
    501 	    && (mii->mii_media_active & IFM_ETH_FMASK)) {
    502 		if (mii->mii_media_active & IFM_ETH_RXPAUSE)
    503 			maccfg1 |= MACCFG1_RX_FLOW;
    504 		if (mii->mii_media_active & IFM_ETH_TXPAUSE)
    505 			maccfg1 |= MACCFG1_TX_FLOW;
    506 	}
    507 
    508 	/*
    509 	 * Now deal with the speed.
    510 	 */
    511 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
    512 		maccfg2 |= MACCFG2_IFMODE_GMII;
    513 	} else {
    514 		maccfg2 |= MACCFG2_IFMODE_MII;
    515 		ecntrl &= ~ECNTRL_R100M;
    516 		if (IFM_SUBTYPE(mii->mii_media_active) != IFM_10_T) {
    517 			ecntrl |= ECNTRL_R100M;
    518 		}
    519 	}
    520 
    521 	/*
    522 	 * If things are different, re-init things.
    523 	 */
    524 	if (maccfg1 != sc->sc_maccfg1
    525 	    || maccfg2 != sc->sc_maccfg2
    526 	    || ecntrl != sc->sc_ecntrl) {
    527 		if (sc->sc_if.if_flags & IFF_RUNNING)
    528 			atomic_or_uint(&sc->sc_soft_flags, SOFT_RESET);
    529 		sc->sc_maccfg1 = maccfg1;
    530 		sc->sc_maccfg2 = maccfg2;
    531 		sc->sc_ecntrl = ecntrl;
    532 	}
    533 }
    534 
    535 #if 0
    536 static void
    537 pq3etsec_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
    538 {
    539 	struct pq3etsec_softc * const sc = ifp->if_softc;
    540 
    541 	mii_pollstat(&sc->sc_mii);
    542 	ether_mediastatus(ifp, ifmr);
    543 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
    544 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
    545 }
    546 
    547 static int
    548 pq3etsec_mediachange(struct ifnet *ifp)
    549 {
    550 	struct pq3etsec_softc * const sc = ifp->if_softc;
    551 
    552 	if ((ifp->if_flags & IFF_UP) == 0)
    553 		return 0;
    554 
    555 	int rv = mii_mediachg(&sc->sc_mii);
    556 	return (rv == ENXIO) ? 0 : rv;
    557 }
    558 #endif
    559 
    560 static int
    561 pq3etsec_match(device_t parent, cfdata_t cf, void *aux)
    562 {
    563 
    564 	if (!e500_cpunode_submatch(parent, cf, cf->cf_name, aux))
    565 		return 0;
    566 
    567 	return 1;
    568 }
    569 
    570 static void
    571 pq3etsec_attach(device_t parent, device_t self, void *aux)
    572 {
    573 	struct cpunode_softc * const psc = device_private(parent);
    574 	struct pq3etsec_softc * const sc = device_private(self);
    575 	struct mii_data * const mii = &sc->sc_mii;
    576 	struct cpunode_attach_args * const cna = aux;
    577 	struct cpunode_locators * const cnl = &cna->cna_locs;
    578 	cfdata_t cf = device_cfdata(self);
    579 	int error;
    580 
    581 	psc->sc_children |= cna->cna_childmask;
    582 	sc->sc_dev = self;
    583 	sc->sc_bst = cna->cna_memt;
    584 	sc->sc_dmat = &booke_bus_dma_tag;
    585 
    586 	/*
    587 	 * Pull out the mdio bus and phy we are supposed to use.
    588 	 */
    589 	const int mdio = cf->cf_loc[CPUNODECF_MDIO];
    590 	const int phy = cf->cf_loc[CPUNODECF_PHY];
    591 	if (mdio != CPUNODECF_MDIO_DEFAULT)
    592 		aprint_normal(" mdio %d", mdio);
    593 
    594 	/*
    595 	 * See if the phy is in the config file...
    596 	 */
    597 	if (phy != CPUNODECF_PHY_DEFAULT) {
    598 		sc->sc_phy_addr = phy;
    599 	} else {
    600 		unsigned char prop_name[20];
    601 		snprintf(prop_name, sizeof(prop_name), "tsec%u-phy-addr",
    602 		    cnl->cnl_instance);
    603 		sc->sc_phy_addr = board_info_get_number(prop_name);
    604 	}
    605 	if (sc->sc_phy_addr != MII_PHY_ANY)
    606 		aprint_normal(" phy %d", sc->sc_phy_addr);
    607 
    608 	error = bus_space_map(sc->sc_bst, cnl->cnl_addr, cnl->cnl_size, 0,
    609 	    &sc->sc_bsh);
    610 	if (error) {
    611 		aprint_error(": error mapping registers: %d\n", error);
    612 		return;
    613 	}
    614 
    615 	/*
    616 	 * Assume firmware has aready set the mac address and fetch it
    617 	 * before we reinit it.
    618 	 */
    619 	sc->sc_macstnaddr2 = etsec_read(sc, MACSTNADDR2);
    620 	sc->sc_macstnaddr1 = etsec_read(sc, MACSTNADDR1);
    621 	sc->sc_rctrl = RCTRL_DEFAULT;
    622 	sc->sc_ecntrl = etsec_read(sc, ECNTRL);
    623 	sc->sc_maccfg1 = etsec_read(sc, MACCFG1);
    624 	sc->sc_maccfg2 = etsec_read(sc, MACCFG2) | MACCFG2_DEFAULT;
    625 
    626 	if (sc->sc_macstnaddr1 == 0 && sc->sc_macstnaddr2 == 0) {
    627 		size_t len;
    628 		const uint8_t *mac_addr =
    629 		    board_info_get_data("tsec-mac-addr-base", &len);
    630 		KASSERT(len == ETHER_ADDR_LEN);
    631 		sc->sc_macstnaddr2 =
    632 		    (mac_addr[1] << 24)
    633 		    | (mac_addr[0] << 16);
    634 		sc->sc_macstnaddr1 =
    635 		    ((mac_addr[5] + cnl->cnl_instance - 1) << 24)
    636 		    | (mac_addr[4] << 16)
    637 		    | (mac_addr[3] << 8)
    638 		    | (mac_addr[2] << 0);
    639 #if 0
    640 		aprint_error(": mac-address unknown\n");
    641 		return;
    642 #endif
    643 	}
    644 
    645 	sc->sc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SOFTNET);
    646 	sc->sc_hwlock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_VM);
    647 
    648 	callout_init(&sc->sc_mii_callout, CALLOUT_MPSAFE);
    649 	callout_setfunc(&sc->sc_mii_callout, pq3etsec_mii_tick, sc);
    650 
    651 	/* Disable interrupts */
    652 	etsec_write(sc, IMASK, 0);
    653 
    654 	error = pq3etsec_rxq_attach(sc, &sc->sc_rxq, 0);
    655 	if (error) {
    656 		aprint_error(": failed to init rxq: %d\n", error);
    657 		goto fail_1;
    658 	}
    659 
    660 	error = pq3etsec_txq_attach(sc, &sc->sc_txq, 0);
    661 	if (error) {
    662 		aprint_error(": failed to init txq: %d\n", error);
    663 		goto fail_2;
    664 	}
    665 
    666 	error = pq3etsec_mapcache_create(sc, &sc->sc_rx_mapcache,
    667 	    ETSEC_MAXRXMBUFS, MCLBYTES, ETSEC_NRXSEGS);
    668 	if (error) {
    669 		aprint_error(": failed to allocate rx dmamaps: %d\n", error);
    670 		goto fail_3;
    671 	}
    672 
    673 	error = pq3etsec_mapcache_create(sc, &sc->sc_tx_mapcache,
    674 	    ETSEC_MAXTXMBUFS, MCLBYTES, ETSEC_NTXSEGS);
    675 	if (error) {
    676 		aprint_error(": failed to allocate tx dmamaps: %d\n", error);
    677 		goto fail_4;
    678 	}
    679 
    680 	sc->sc_tx_ih = intr_establish(cnl->cnl_intrs[0], IPL_VM, IST_ONCHIP,
    681 	    pq3etsec_tx_intr, sc);
    682 	if (sc->sc_tx_ih == NULL) {
    683 		aprint_error(": failed to establish tx interrupt: %d\n",
    684 		    cnl->cnl_intrs[0]);
    685 		goto fail_5;
    686 	}
    687 
    688 	sc->sc_rx_ih = intr_establish(cnl->cnl_intrs[1], IPL_VM, IST_ONCHIP,
    689 	    pq3etsec_rx_intr, sc);
    690 	if (sc->sc_rx_ih == NULL) {
    691 		aprint_error(": failed to establish rx interrupt: %d\n",
    692 		    cnl->cnl_intrs[1]);
    693 		goto fail_6;
    694 	}
    695 
    696 	sc->sc_error_ih = intr_establish(cnl->cnl_intrs[2], IPL_VM, IST_ONCHIP,
    697 	    pq3etsec_error_intr, sc);
    698 	if (sc->sc_error_ih == NULL) {
    699 		aprint_error(": failed to establish error interrupt: %d\n",
    700 		    cnl->cnl_intrs[2]);
    701 		goto fail_7;
    702 	}
    703 
    704 	int softint_flags = SOFTINT_NET;
    705 #if !defined(MULTIPROCESSOR) || defined(NET_MPSAFE)
    706 	softint_flags |= SOFTINT_MPSAFE;
    707 #endif	/* !MULTIPROCESSOR || NET_MPSAFE */
    708 	sc->sc_soft_ih = softint_establish(softint_flags,
    709 	    pq3etsec_soft_intr, sc);
    710 	if (sc->sc_soft_ih == NULL) {
    711 		aprint_error(": failed to establish soft interrupt\n");
    712 		goto fail_8;
    713 	}
    714 
    715 	/*
    716 	 * If there was no MDIO
    717 	 */
    718 	if (mdio == CPUNODECF_MDIO_DEFAULT) {
    719 		aprint_normal("\n");
    720 		cfdata_t mdio_cf = config_search(self, cna,
    721 						 CFARG_SUBMATCH, pq3mdio_find,
    722 						 CFARG_EOL);
    723 		if (mdio_cf != NULL) {
    724 			sc->sc_mdio_dev =
    725 			    config_attach(self, mdio_cf, cna, NULL, CFARG_EOL);
    726 		}
    727 	} else {
    728 		sc->sc_mdio_dev = device_find_by_driver_unit("mdio", mdio);
    729 		if (sc->sc_mdio_dev == NULL) {
    730 			aprint_error(": failed to locate mdio device\n");
    731 			goto fail_9;
    732 		}
    733 		aprint_normal("\n");
    734 	}
    735 
    736 	etsec_write(sc, ATTR, ATTR_DEFAULT);
    737 	etsec_write(sc, ATTRELI, ATTRELI_DEFAULT);
    738 
    739 	/* Enable interrupt coalesing */
    740 	sc->sc_ic_rx_time = 768;
    741 	sc->sc_ic_rx_count = 16;
    742 	sc->sc_ic_tx_time = 768;
    743 	sc->sc_ic_tx_count = 16;
    744 	pq3etsec_set_ic_rx(sc);
    745 	pq3etsec_set_ic_tx(sc);
    746 
    747 	char enaddr[ETHER_ADDR_LEN] = {
    748 	    [0] = sc->sc_macstnaddr2 >> 16,
    749 	    [1] = sc->sc_macstnaddr2 >> 24,
    750 	    [2] = sc->sc_macstnaddr1 >>	 0,
    751 	    [3] = sc->sc_macstnaddr1 >>	 8,
    752 	    [4] = sc->sc_macstnaddr1 >> 16,
    753 	    [5] = sc->sc_macstnaddr1 >> 24,
    754 	};
    755 	aprint_normal_dev(sc->sc_dev, "Ethernet address %s\n",
    756 	   ether_sprintf(enaddr));
    757 
    758 	const char * const xname = device_xname(sc->sc_dev);
    759 	struct ethercom * const ec = &sc->sc_ec;
    760 	struct ifnet * const ifp = &ec->ec_if;
    761 
    762 	ec->ec_mii = mii;
    763 
    764 	mii->mii_ifp = ifp;
    765 	mii->mii_readreg = pq3mdio_mii_readreg;
    766 	mii->mii_writereg = pq3mdio_mii_writereg;
    767 	mii->mii_statchg = pq3etsec_mii_statchg;
    768 
    769 	ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
    770 
    771 	if (sc->sc_mdio_dev != NULL && sc->sc_phy_addr < 32) {
    772 		mii_attach(sc->sc_mdio_dev, mii, 0xffffffff,
    773 		    sc->sc_phy_addr, MII_OFFSET_ANY, MIIF_DOPAUSE);
    774 
    775 		if (LIST_FIRST(&mii->mii_phys) == NULL) {
    776 			ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE,
    777 			    0, NULL);
    778 			ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
    779 		} else {
    780 			callout_schedule(&sc->sc_mii_callout, hz);
    781 			ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
    782 		}
    783 	} else {
    784 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_1000_T | IFM_FDX,
    785 		    0, NULL);
    786 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_1000_T | IFM_FDX);
    787 	}
    788 
    789 	ec->ec_capabilities = ETHERCAP_VLAN_MTU | ETHERCAP_VLAN_HWTAGGING
    790 	    | ETHERCAP_JUMBO_MTU;
    791 	ec->ec_capenable = ETHERCAP_VLAN_HWTAGGING;
    792 
    793 	strlcpy(ifp->if_xname, xname, IFNAMSIZ);
    794 	ifp->if_softc = sc;
    795 	ifp->if_capabilities = IFCAP_ETSEC;
    796 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    797 	ifp->if_ioctl = pq3etsec_ifioctl;
    798 	ifp->if_start = pq3etsec_ifstart;
    799 	ifp->if_watchdog = pq3etsec_ifwatchdog;
    800 	ifp->if_init = pq3etsec_ifinit;
    801 	ifp->if_stop = pq3etsec_ifstop;
    802 	IFQ_SET_READY(&ifp->if_snd);
    803 
    804 	/*
    805 	 * Attach the interface.
    806 	 */
    807 	error = if_initialize(ifp);
    808 	if (error != 0) {
    809 		aprint_error_dev(sc->sc_dev, "if_initialize failed(%d)\n",
    810 		    error);
    811 		goto fail_10;
    812 	}
    813 	pq3etsec_sysctl_setup(NULL, sc);
    814 	if_attach(ifp);
    815 	if_deferred_start_init(ifp, NULL);
    816 	ether_ifattach(ifp, enaddr);
    817 
    818 	rnd_attach_source(&sc->rnd_source, xname, RND_TYPE_NET,
    819 	    RND_FLAG_DEFAULT);
    820 
    821 	pq3etsec_ifstop(ifp, true);
    822 
    823 	evcnt_attach_dynamic(&sc->sc_ev_rx_stall, EVCNT_TYPE_MISC,
    824 	    NULL, xname, "rx stall");
    825 	evcnt_attach_dynamic(&sc->sc_ev_tx_stall, EVCNT_TYPE_MISC,
    826 	    NULL, xname, "tx stall");
    827 	evcnt_attach_dynamic(&sc->sc_ev_tx_intr, EVCNT_TYPE_INTR,
    828 	    NULL, xname, "tx intr");
    829 	evcnt_attach_dynamic(&sc->sc_ev_rx_intr, EVCNT_TYPE_INTR,
    830 	    NULL, xname, "rx intr");
    831 	evcnt_attach_dynamic(&sc->sc_ev_error_intr, EVCNT_TYPE_INTR,
    832 	    NULL, xname, "error intr");
    833 	evcnt_attach_dynamic(&sc->sc_ev_soft_intr, EVCNT_TYPE_INTR,
    834 	    NULL, xname, "soft intr");
    835 	evcnt_attach_dynamic(&sc->sc_ev_tx_pause, EVCNT_TYPE_MISC,
    836 	    NULL, xname, "tx pause");
    837 	evcnt_attach_dynamic(&sc->sc_ev_rx_pause, EVCNT_TYPE_MISC,
    838 	    NULL, xname, "rx pause");
    839 	evcnt_attach_dynamic(&sc->sc_ev_mii_ticks, EVCNT_TYPE_MISC,
    840 	    NULL, xname, "mii ticks");
    841 	return;
    842 
    843 fail_10:
    844 	ifmedia_removeall(&mii->mii_media);
    845 	mii_detach(mii, sc->sc_phy_addr, MII_OFFSET_ANY);
    846 fail_9:
    847 	softint_disestablish(sc->sc_soft_ih);
    848 fail_8:
    849 	intr_disestablish(sc->sc_error_ih);
    850 fail_7:
    851 	intr_disestablish(sc->sc_rx_ih);
    852 fail_6:
    853 	intr_disestablish(sc->sc_tx_ih);
    854 fail_5:
    855 	pq3etsec_mapcache_destroy(sc, sc->sc_tx_mapcache);
    856 fail_4:
    857 	pq3etsec_mapcache_destroy(sc, sc->sc_rx_mapcache);
    858 fail_3:
    859 #if 0 /* notyet */
    860 	pq3etsec_txq_detach(sc);
    861 #endif
    862 fail_2:
    863 #if 0 /* notyet */
    864 	pq3etsec_rxq_detach(sc);
    865 #endif
    866 fail_1:
    867 	callout_destroy(&sc->sc_mii_callout);
    868 	mutex_obj_free(sc->sc_lock);
    869 	mutex_obj_free(sc->sc_hwlock);
    870 	bus_space_unmap(sc->sc_bst, sc->sc_bsh, cnl->cnl_size);
    871 }
    872 
    873 static uint64_t
    874 pq3etsec_macaddr_create(const uint8_t *lladdr)
    875 {
    876 	uint64_t macaddr = 0;
    877 
    878 	lladdr += ETHER_ADDR_LEN;
    879 	for (u_int i = ETHER_ADDR_LEN; i-- > 0; ) {
    880 		macaddr = (macaddr << 8) | *--lladdr;
    881 	}
    882 	return macaddr << 16;
    883 }
    884 
    885 static int
    886 pq3etsec_ifinit(struct ifnet *ifp)
    887 {
    888 	struct pq3etsec_softc * const sc = ifp->if_softc;
    889 	int error = 0;
    890 
    891 	sc->sc_maxfrm = uimax(ifp->if_mtu + 32, MCLBYTES);
    892 	if (ifp->if_mtu > ETHERMTU_JUMBO)
    893 		return error;
    894 
    895 	KASSERT(ifp->if_flags & IFF_UP);
    896 
    897 	/*
    898 	 * Stop the interface (steps 1 to 4 in the Soft Reset and
    899 	 * Reconfigurating Procedure.
    900 	 */
    901 	pq3etsec_ifstop(ifp, 0);
    902 
    903 	/*
    904 	 * If our frame size has changed (or it's our first time through)
    905 	 * destroy the existing transmit mapcache.
    906 	 */
    907 	if (sc->sc_tx_mapcache != NULL
    908 	    && sc->sc_maxfrm != sc->sc_tx_mapcache->dmc_maxmapsize) {
    909 		pq3etsec_mapcache_destroy(sc, sc->sc_tx_mapcache);
    910 		sc->sc_tx_mapcache = NULL;
    911 	}
    912 
    913 	if (sc->sc_tx_mapcache == NULL) {
    914 		error = pq3etsec_mapcache_create(sc, &sc->sc_tx_mapcache,
    915 		    ETSEC_MAXTXMBUFS, sc->sc_maxfrm, ETSEC_NTXSEGS);
    916 		if (error)
    917 			return error;
    918 	}
    919 
    920 	sc->sc_ev_mii_ticks.ev_count++;
    921 	mii_tick(&sc->sc_mii);
    922 
    923 	if (ifp->if_flags & IFF_PROMISC) {
    924 		sc->sc_rctrl |= RCTRL_PROM;
    925 	} else {
    926 		sc->sc_rctrl &= ~RCTRL_PROM;
    927 	}
    928 
    929 	uint32_t rctrl_prsdep = 0;
    930 	sc->sc_rctrl &=
    931 	    ~(RCTRL_IPCSEN | RCTRL_TUCSEN | RCTRL_VLEX | RCTRL_PRSDEP);
    932 	if (VLAN_ATTACHED(&sc->sc_ec)) {
    933 		sc->sc_rctrl |= RCTRL_VLEX;
    934 		rctrl_prsdep = RCTRL_PRSDEP_L2;
    935 	}
    936 	if (ifp->if_capenable & IFCAP_RCTRL_IPCSEN) {
    937 		sc->sc_rctrl |= RCTRL_IPCSEN;
    938 		rctrl_prsdep = RCTRL_PRSDEP_L3;
    939 	}
    940 	if (ifp->if_capenable & IFCAP_RCTRL_TUCSEN) {
    941 		sc->sc_rctrl |= RCTRL_TUCSEN;
    942 		rctrl_prsdep = RCTRL_PRSDEP_L4;
    943 	}
    944 	sc->sc_rctrl |= rctrl_prsdep;
    945 #if 0
    946 	if (sc->sc_rctrl
    947 	    & (RCTRL_IPCSEN | RCTRL_TUCSEN | RCTRL_VLEX | RCTRL_PRSDEP))
    948 		aprint_normal_dev(sc->sc_dev,
    949 		    "rctrl=%#x ipcsen=%"PRIuMAX" tucsen=%"PRIuMAX" vlex=%"PRIuMAX" prsdep=%"PRIuMAX"\n",
    950 		    sc->sc_rctrl,
    951 		    __SHIFTOUT(sc->sc_rctrl, RCTRL_IPCSEN),
    952 		    __SHIFTOUT(sc->sc_rctrl, RCTRL_TUCSEN),
    953 		    __SHIFTOUT(sc->sc_rctrl, RCTRL_VLEX),
    954 		    __SHIFTOUT(sc->sc_rctrl, RCTRL_PRSDEP));
    955 #endif
    956 
    957 	sc->sc_tctrl &= ~(TCTRL_IPCSEN | TCTRL_TUCSEN | TCTRL_VLINS);
    958 	if (VLAN_ATTACHED(&sc->sc_ec))		/* is this really true */
    959 		sc->sc_tctrl |= TCTRL_VLINS;
    960 	if (ifp->if_capenable & IFCAP_TCTRL_IPCSEN)
    961 		sc->sc_tctrl |= TCTRL_IPCSEN;
    962 	if (ifp->if_capenable & IFCAP_TCTRL_TUCSEN)
    963 		sc->sc_tctrl |= TCTRL_TUCSEN;
    964 #if 0
    965 	if (sc->sc_tctrl & (TCTRL_IPCSEN | TCTRL_TUCSEN | TCTRL_VLINS))
    966 		aprint_normal_dev(sc->sc_dev,
    967 		    "tctrl=%#x ipcsen=%"PRIuMAX" tucsen=%"PRIuMAX" vlins=%"PRIuMAX"\n",
    968 		    sc->sc_tctrl,
    969 		    __SHIFTOUT(sc->sc_tctrl, TCTRL_IPCSEN),
    970 		    __SHIFTOUT(sc->sc_tctrl, TCTRL_TUCSEN),
    971 		    __SHIFTOUT(sc->sc_tctrl, TCTRL_VLINS));
    972 #endif
    973 
    974 	sc->sc_maccfg1 &= ~(MACCFG1_TX_EN | MACCFG1_RX_EN);
    975 
    976 	const uint64_t macstnaddr =
    977 	    pq3etsec_macaddr_create(CLLADDR(ifp->if_sadl));
    978 
    979 	sc->sc_imask = IEVENT_DPE;
    980 
    981 	/* 5. Load TDBPH, TBASEH, TBASE0-TBASE7 with new Tx BD pointers */
    982 	pq3etsec_rxq_reset(sc, &sc->sc_rxq);
    983 	pq3etsec_rxq_produce(sc, &sc->sc_rxq);	/* fill with rx buffers */
    984 
    985 	/* 6. Load RDBPH, RBASEH, RBASE0-RBASE7 with new Rx BD pointers */
    986 	pq3etsec_txq_reset(sc, &sc->sc_txq);
    987 
    988 	/* 7. Setup other MAC registers (MACCFG2, MAXFRM, etc.) */
    989 	KASSERT(MACCFG2_PADCRC & sc->sc_maccfg2);
    990 	etsec_write(sc, MAXFRM, sc->sc_maxfrm);
    991 	etsec_write(sc, MACSTNADDR1, (uint32_t)(macstnaddr >> 32));
    992 	etsec_write(sc, MACSTNADDR2, (uint32_t)(macstnaddr >>  0));
    993 	etsec_write(sc, MACCFG1, sc->sc_maccfg1);
    994 	etsec_write(sc, MACCFG2, sc->sc_maccfg2);
    995 	etsec_write(sc, ECNTRL, sc->sc_ecntrl);
    996 
    997 	/* 8. Setup group address hash table (GADDR0-GADDR15) */
    998 	pq3etsec_mc_setup(sc);
    999 
   1000 	/* 9. Setup receive frame filer table (via RQFAR, RQFCR, and RQFPR) */
   1001 	etsec_write(sc, MRBLR, MCLBYTES);
   1002 
   1003 	/* 10. Setup WWR, WOP, TOD bits in DMACTRL register */
   1004 	sc->sc_dmactrl |= DMACTRL_DEFAULT;
   1005 	etsec_write(sc, DMACTRL, sc->sc_dmactrl);
   1006 
   1007 	/* 11. Enable transmit queues in TQUEUE, and ensure that the transmit scheduling mode is correctly set in TCTRL. */
   1008 	etsec_write(sc, TQUEUE, TQUEUE_EN0);
   1009 	sc->sc_imask |= IEVENT_TXF | IEVENT_TXE | IEVENT_TXC;
   1010 
   1011 	etsec_write(sc, TCTRL, sc->sc_tctrl);	/* for TOE stuff */
   1012 
   1013 	/* 12. Enable receive queues in RQUEUE, */
   1014 	etsec_write(sc, RQUEUE, RQUEUE_EN0 | RQUEUE_EX0);
   1015 	sc->sc_imask |= IEVENT_RXF | IEVENT_BSY | IEVENT_RXC;
   1016 
   1017 	/*     and optionally set TOE functionality in RCTRL. */
   1018 	etsec_write(sc, RCTRL, sc->sc_rctrl);
   1019 	sc->sc_rx_adjlen = __SHIFTOUT(sc->sc_rctrl, RCTRL_PAL);
   1020 	if ((sc->sc_rctrl & RCTRL_PRSDEP) != RCTRL_PRSDEP_OFF)
   1021 		sc->sc_rx_adjlen += sizeof(struct rxfcb);
   1022 
   1023 	/* 13. Clear THLT and TXF bits in TSTAT register by writing 1 to them */
   1024 	etsec_write(sc, TSTAT, TSTAT_THLT | TSTAT_TXF);
   1025 
   1026 	/* 14. Clear QHLT and RXF bits in RSTAT register by writing 1 to them.*/
   1027 	etsec_write(sc, RSTAT, RSTAT_QHLT | RSTAT_RXF);
   1028 
   1029 	/* 15. Clear GRS/GTS bits in DMACTRL (do not change other bits) */
   1030 	sc->sc_dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
   1031 	etsec_write(sc, DMACTRL, sc->sc_dmactrl);
   1032 
   1033 	/* 16. Enable Tx_EN/Rx_EN in MACCFG1 register */
   1034 	etsec_write(sc, MACCFG1, sc->sc_maccfg1 | MACCFG1_TX_EN|MACCFG1_RX_EN);
   1035 	etsec_write(sc, MACCFG1, sc->sc_maccfg1 | MACCFG1_TX_EN|MACCFG1_RX_EN);
   1036 
   1037 	sc->sc_soft_flags = 0;
   1038 
   1039 	etsec_write(sc, IMASK, sc->sc_imask);
   1040 
   1041 	ifp->if_flags |= IFF_RUNNING;
   1042 
   1043 	return error;
   1044 }
   1045 
   1046 static void
   1047 pq3etsec_ifstop(struct ifnet *ifp, int disable)
   1048 {
   1049 	struct pq3etsec_softc * const sc = ifp->if_softc;
   1050 
   1051 	KASSERT(!cpu_intr_p());
   1052 	const uint32_t imask_gsc_mask = IEVENT_GTSC | IEVENT_GRSC;
   1053 	/*
   1054 	 * Clear the GTSC and GRSC from the interrupt mask until
   1055 	 * we are ready for them.  Then clear them from IEVENT,
   1056 	 * request the graceful shutdown, and then enable the
   1057 	 * GTSC and GRSC bits in the mask.  This should cause the
   1058 	 * error interrupt to fire which will issue a wakeup to
   1059 	 * allow us to resume.
   1060 	 */
   1061 
   1062 	/*
   1063 	 * 1. Set GRS/GTS bits in DMACTRL register
   1064 	 */
   1065 	sc->sc_dmactrl |= DMACTRL_GRS | DMACTRL_GTS;
   1066 	etsec_write(sc, IMASK, sc->sc_imask & ~imask_gsc_mask);
   1067 	etsec_write(sc, IEVENT, imask_gsc_mask);
   1068 	etsec_write(sc, DMACTRL, sc->sc_dmactrl);
   1069 
   1070 	if (etsec_read(sc, MACCFG1) & (MACCFG1_TX_EN | MACCFG1_RX_EN)) {
   1071 		/*
   1072 		 * 2. Poll GRSC/GTSC bits in IEVENT register until both are set
   1073 		 */
   1074 		etsec_write(sc, IMASK, sc->sc_imask | imask_gsc_mask);
   1075 
   1076 		u_int timo = 1000;
   1077 		uint32_t ievent = etsec_read(sc, IEVENT);
   1078 		while ((ievent & imask_gsc_mask) != imask_gsc_mask) {
   1079 			if (--timo == 0) {
   1080 				aprint_error_dev(sc->sc_dev,
   1081 				    "WARNING: "
   1082 				    "request to stop failed (IEVENT=%#x)\n",
   1083 				    ievent);
   1084 				break;
   1085 			}
   1086 			delay(10);
   1087 			ievent = etsec_read(sc, IEVENT);
   1088 		}
   1089 	}
   1090 
   1091 	/*
   1092 	 * Now reset the controller.
   1093 	 *
   1094 	 * 3. Set SOFT_RESET bit in MACCFG1 register
   1095 	 * 4. Clear SOFT_RESET bit in MACCFG1 register
   1096 	 */
   1097 	etsec_write(sc, MACCFG1, MACCFG1_SOFT_RESET);
   1098 	etsec_write(sc, MACCFG1, 0);
   1099 	etsec_write(sc, IMASK, 0);
   1100 	etsec_write(sc, IEVENT, ~0);
   1101 	sc->sc_imask = 0;
   1102 	ifp->if_flags &= ~IFF_RUNNING;
   1103 
   1104 	uint32_t tbipa = etsec_read(sc, TBIPA);
   1105 	if (tbipa == sc->sc_phy_addr) {
   1106 		aprint_normal_dev(sc->sc_dev, "relocating TBI\n");
   1107 		etsec_write(sc, TBIPA, 0x1f);
   1108 	}
   1109 	uint32_t miimcfg = etsec_read(sc, MIIMCFG);
   1110 	etsec_write(sc, MIIMCFG, MIIMCFG_RESET);
   1111 	etsec_write(sc, MIIMCFG, miimcfg);
   1112 
   1113 	/*
   1114 	 * Let's consume any remaing transmitted packets.  And if we are
   1115 	 * disabling the interface, purge ourselves of any untransmitted
   1116 	 * packets.  But don't consume any received packets, just drop them.
   1117 	 * If we aren't disabling the interface, save the mbufs in the
   1118 	 * receive queue for reuse.
   1119 	 */
   1120 	pq3etsec_rxq_purge(sc, &sc->sc_rxq, disable);
   1121 	pq3etsec_txq_consume(sc, &sc->sc_txq);
   1122 	if (disable) {
   1123 		pq3etsec_txq_purge(sc, &sc->sc_txq);
   1124 		IFQ_PURGE(&ifp->if_snd);
   1125 	}
   1126 }
   1127 
   1128 static void
   1129 pq3etsec_ifwatchdog(struct ifnet *ifp)
   1130 {
   1131 }
   1132 
   1133 static void
   1134 pq3etsec_mc_setup(
   1135 	struct pq3etsec_softc *sc)
   1136 {
   1137 	struct ethercom * const ec = &sc->sc_ec;
   1138 	struct ifnet * const ifp = &sc->sc_if;
   1139 	struct ether_multi *enm;
   1140 	struct ether_multistep step;
   1141 	uint32_t *gaddr = sc->sc_gaddr + ((sc->sc_rctrl & RCTRL_GHTX) ? 0 : 8);
   1142 	const uint32_t crc_shift = 32 - ((sc->sc_rctrl & RCTRL_GHTX) ? 9 : 8);
   1143 
   1144 	memset(sc->sc_gaddr, 0, sizeof(sc->sc_gaddr));
   1145 	memset(sc->sc_macaddrs, 0, sizeof(sc->sc_macaddrs));
   1146 
   1147 	ifp->if_flags &= ~IFF_ALLMULTI;
   1148 
   1149 	ETHER_LOCK(ec);
   1150 	ETHER_FIRST_MULTI(step, ec, enm);
   1151 	for (u_int i = 0; enm != NULL; ) {
   1152 		const char *addr = enm->enm_addrlo;
   1153 		if (memcmp(addr, enm->enm_addrhi, ETHER_ADDR_LEN) != 0) {
   1154 			ifp->if_flags |= IFF_ALLMULTI;
   1155 			memset(gaddr, 0xff, 32 << (crc_shift & 1));
   1156 			memset(sc->sc_macaddrs, 0, sizeof(sc->sc_macaddrs));
   1157 			break;
   1158 		}
   1159 		if ((sc->sc_rctrl & RCTRL_EMEN)
   1160 		    && i < __arraycount(sc->sc_macaddrs)) {
   1161 			sc->sc_macaddrs[i++] = pq3etsec_macaddr_create(addr);
   1162 		} else {
   1163 			uint32_t crc = ether_crc32_be(addr, ETHER_ADDR_LEN);
   1164 #if 0
   1165 			printf("%s: %s: crc=%#x: %#x: [%u,%u]=%#x\n", __func__,
   1166 			    ether_sprintf(addr), crc,
   1167 			    crc >> crc_shift,
   1168 			    crc >> (crc_shift + 5),
   1169 			    (crc >> crc_shift) & 31,
   1170 			    1 << (((crc >> crc_shift) & 31) ^ 31));
   1171 #endif
   1172 			/*
   1173 			 * The documentation doesn't completely follow PowerPC
   1174 			 * bit order.  The BE crc32 (H) for 01:00:5E:00:00:01
   1175 			 * is 0x7fa32d9b.  By empirical testing, the
   1176 			 * corresponding hash bit is word 3, bit 31 (ppc bit
   1177 			 * order).  Since 3 << 31 | 31 is 0x7f, we deduce
   1178 			 * H[0:2] selects the register while H[3:7] selects
   1179 			 * the bit (ppc bit order).
   1180 			 */
   1181 			crc >>= crc_shift;
   1182 			gaddr[crc / 32] |= 1 << ((crc & 31) ^ 31);
   1183 		}
   1184 		ETHER_NEXT_MULTI(step, enm);
   1185 	}
   1186 	ETHER_UNLOCK(ec);
   1187 	for (u_int i = 0; i < 8; i++) {
   1188 		etsec_write(sc, IGADDR(i), sc->sc_gaddr[i]);
   1189 		etsec_write(sc, GADDR(i), sc->sc_gaddr[i+8]);
   1190 #if 0
   1191 		if (sc->sc_gaddr[i] || sc->sc_gaddr[i+8])
   1192 		printf("%s: IGADDR%u(%#x)=%#x GADDR%u(%#x)=%#x\n", __func__,
   1193 		    i, IGADDR(i), etsec_read(sc, IGADDR(i)),
   1194 		    i, GADDR(i), etsec_read(sc, GADDR(i)));
   1195 #endif
   1196 	}
   1197 	for (u_int i = 0; i < __arraycount(sc->sc_macaddrs); i++) {
   1198 		uint64_t macaddr = sc->sc_macaddrs[i];
   1199 		etsec_write(sc, MACnADDR1(i), (uint32_t)(macaddr >> 32));
   1200 		etsec_write(sc, MACnADDR2(i), (uint32_t)(macaddr >>  0));
   1201 #if 0
   1202 		if (macaddr)
   1203 		printf("%s: MAC%02uADDR2(%08x)=%#x MAC%02uADDR2(%#x)=%08x\n", __func__,
   1204 		    i+1, MACnADDR1(i), etsec_read(sc, MACnADDR1(i)),
   1205 		    i+1, MACnADDR2(i), etsec_read(sc, MACnADDR2(i)));
   1206 #endif
   1207 	}
   1208 }
   1209 
   1210 static int
   1211 pq3etsec_ifioctl(struct ifnet *ifp, u_long cmd, void *data)
   1212 {
   1213 	struct pq3etsec_softc *sc  = ifp->if_softc;
   1214 	struct ifreq * const ifr = data;
   1215 	const int s = splnet();
   1216 	int error;
   1217 
   1218 	switch (cmd) {
   1219 	case SIOCSIFMEDIA:
   1220 		/* Flow control requires full-duplex mode. */
   1221 		if (IFM_SUBTYPE(ifr->ifr_media) == IFM_AUTO ||
   1222 		    (ifr->ifr_media & IFM_FDX) == 0)
   1223 			ifr->ifr_media &= ~IFM_ETH_FMASK;
   1224 		if (IFM_SUBTYPE(ifr->ifr_media) != IFM_AUTO) {
   1225 			if ((ifr->ifr_media & IFM_ETH_FMASK) == IFM_FLOW) {
   1226 				/* We can do both TXPAUSE and RXPAUSE. */
   1227 				ifr->ifr_media |=
   1228 				    IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE;
   1229 			}
   1230 		}
   1231 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
   1232 		break;
   1233 
   1234 	default:
   1235 		error = ether_ioctl(ifp, cmd, data);
   1236 		if (error != ENETRESET)
   1237 			break;
   1238 
   1239 		if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI) {
   1240 			error = 0;
   1241 			if (ifp->if_flags & IFF_RUNNING)
   1242 				pq3etsec_mc_setup(sc);
   1243 			break;
   1244 		}
   1245 		error = pq3etsec_ifinit(ifp);
   1246 		break;
   1247 	}
   1248 
   1249 	splx(s);
   1250 	return error;
   1251 }
   1252 
   1253 static void
   1254 pq3etsec_rxq_desc_presync(
   1255 	struct pq3etsec_softc *sc,
   1256 	struct pq3etsec_rxqueue *rxq,
   1257 	volatile struct rxbd *rxbd,
   1258 	size_t count)
   1259 {
   1260 	bus_dmamap_sync(sc->sc_dmat, rxq->rxq_descmap,
   1261 	    (rxbd - rxq->rxq_first) * sizeof(*rxbd), count * sizeof(*rxbd),
   1262 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1263 }
   1264 
   1265 static void
   1266 pq3etsec_rxq_desc_postsync(
   1267 	struct pq3etsec_softc *sc,
   1268 	struct pq3etsec_rxqueue *rxq,
   1269 	volatile struct rxbd *rxbd,
   1270 	size_t count)
   1271 {
   1272 	bus_dmamap_sync(sc->sc_dmat, rxq->rxq_descmap,
   1273 	    (rxbd - rxq->rxq_first) * sizeof(*rxbd), count * sizeof(*rxbd),
   1274 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1275 }
   1276 
   1277 static void
   1278 pq3etsec_txq_desc_presync(
   1279 	struct pq3etsec_softc *sc,
   1280 	struct pq3etsec_txqueue *txq,
   1281 	volatile struct txbd *txbd,
   1282 	size_t count)
   1283 {
   1284 	bus_dmamap_sync(sc->sc_dmat, txq->txq_descmap,
   1285 	    (txbd - txq->txq_first) * sizeof(*txbd), count * sizeof(*txbd),
   1286 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1287 }
   1288 
   1289 static void
   1290 pq3etsec_txq_desc_postsync(
   1291 	struct pq3etsec_softc *sc,
   1292 	struct pq3etsec_txqueue *txq,
   1293 	volatile struct txbd *txbd,
   1294 	size_t count)
   1295 {
   1296 	bus_dmamap_sync(sc->sc_dmat, txq->txq_descmap,
   1297 	    (txbd - txq->txq_first) * sizeof(*txbd), count * sizeof(*txbd),
   1298 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1299 }
   1300 
   1301 static bus_dmamap_t
   1302 pq3etsec_mapcache_get(
   1303 	struct pq3etsec_softc *sc,
   1304 	struct pq3etsec_mapcache *dmc)
   1305 {
   1306 	KASSERT(dmc->dmc_nmaps > 0);
   1307 	KASSERT(dmc->dmc_maps[dmc->dmc_nmaps-1] != NULL);
   1308 	return dmc->dmc_maps[--dmc->dmc_nmaps];
   1309 }
   1310 
   1311 static void
   1312 pq3etsec_mapcache_put(
   1313 	struct pq3etsec_softc *sc,
   1314 	struct pq3etsec_mapcache *dmc,
   1315 	bus_dmamap_t map)
   1316 {
   1317 	KASSERT(map != NULL);
   1318 	KASSERT(dmc->dmc_nmaps < dmc->dmc_maxmaps);
   1319 	dmc->dmc_maps[dmc->dmc_nmaps++] = map;
   1320 }
   1321 
   1322 static void
   1323 pq3etsec_mapcache_destroy(
   1324 	struct pq3etsec_softc *sc,
   1325 	struct pq3etsec_mapcache *dmc)
   1326 {
   1327 	const size_t dmc_size =
   1328 	    offsetof(struct pq3etsec_mapcache, dmc_maps[dmc->dmc_maxmaps]);
   1329 
   1330 	for (u_int i = 0; i < dmc->dmc_maxmaps; i++) {
   1331 		bus_dmamap_destroy(sc->sc_dmat, dmc->dmc_maps[i]);
   1332 	}
   1333 	kmem_intr_free(dmc, dmc_size);
   1334 }
   1335 
   1336 static int
   1337 pq3etsec_mapcache_create(
   1338 	struct pq3etsec_softc *sc,
   1339 	struct pq3etsec_mapcache **dmc_p,
   1340 	size_t maxmaps,
   1341 	size_t maxmapsize,
   1342 	size_t maxseg)
   1343 {
   1344 	const size_t dmc_size =
   1345 	    offsetof(struct pq3etsec_mapcache, dmc_maps[maxmaps]);
   1346 	struct pq3etsec_mapcache * const dmc =
   1347 		kmem_intr_zalloc(dmc_size, KM_NOSLEEP);
   1348 
   1349 	dmc->dmc_maxmaps = maxmaps;
   1350 	dmc->dmc_nmaps = maxmaps;
   1351 	dmc->dmc_maxmapsize = maxmapsize;
   1352 	dmc->dmc_maxseg = maxseg;
   1353 
   1354 	for (u_int i = 0; i < maxmaps; i++) {
   1355 		int error = bus_dmamap_create(sc->sc_dmat, dmc->dmc_maxmapsize,
   1356 		     dmc->dmc_maxseg, dmc->dmc_maxmapsize, 0,
   1357 		     BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW, &dmc->dmc_maps[i]);
   1358 		if (error) {
   1359 			aprint_error_dev(sc->sc_dev,
   1360 			    "failed to creat dma map cache "
   1361 			    "entry %u of %zu: %d\n",
   1362 			    i, maxmaps, error);
   1363 			while (i-- > 0) {
   1364 				bus_dmamap_destroy(sc->sc_dmat,
   1365 				    dmc->dmc_maps[i]);
   1366 			}
   1367 			kmem_intr_free(dmc, dmc_size);
   1368 			return error;
   1369 		}
   1370 		KASSERT(dmc->dmc_maps[i] != NULL);
   1371 	}
   1372 
   1373 	*dmc_p = dmc;
   1374 
   1375 	return 0;
   1376 }
   1377 
   1378 #if 0
   1379 static void
   1380 pq3etsec_dmamem_free(
   1381 	bus_dma_tag_t dmat,
   1382 	size_t map_size,
   1383 	bus_dma_segment_t *seg,
   1384 	bus_dmamap_t map,
   1385 	void *kvap)
   1386 {
   1387 	bus_dmamap_destroy(dmat, map);
   1388 	bus_dmamem_unmap(dmat, kvap, map_size);
   1389 	bus_dmamem_free(dmat, seg, 1);
   1390 }
   1391 #endif
   1392 
   1393 static int
   1394 pq3etsec_dmamem_alloc(
   1395 	bus_dma_tag_t dmat,
   1396 	size_t map_size,
   1397 	bus_dma_segment_t *seg,
   1398 	bus_dmamap_t *map,
   1399 	void **kvap)
   1400 {
   1401 	int error;
   1402 	int nseg;
   1403 
   1404 	*kvap = NULL;
   1405 	*map = NULL;
   1406 
   1407 	error = bus_dmamem_alloc(dmat, map_size, PAGE_SIZE, 0,
   1408 	   seg, 1, &nseg, 0);
   1409 	if (error)
   1410 		return error;
   1411 
   1412 	KASSERT(nseg == 1);
   1413 
   1414 	error = bus_dmamem_map(dmat, seg, nseg, map_size, (void **)kvap,
   1415 	    BUS_DMA_COHERENT);
   1416 	if (error == 0) {
   1417 		error = bus_dmamap_create(dmat, map_size, 1, map_size, 0, 0,
   1418 		    map);
   1419 		if (error == 0) {
   1420 			error = bus_dmamap_load(dmat, *map, *kvap, map_size,
   1421 			    NULL, 0);
   1422 			if (error == 0)
   1423 				return 0;
   1424 			bus_dmamap_destroy(dmat, *map);
   1425 			*map = NULL;
   1426 		}
   1427 		bus_dmamem_unmap(dmat, *kvap, map_size);
   1428 		*kvap = NULL;
   1429 	}
   1430 	bus_dmamem_free(dmat, seg, nseg);
   1431 	return 0;
   1432 }
   1433 
   1434 static struct mbuf *
   1435 pq3etsec_rx_buf_alloc(
   1436 	struct pq3etsec_softc *sc)
   1437 {
   1438 	struct mbuf *m = m_gethdr(M_DONTWAIT, MT_DATA);
   1439 	if (m == NULL) {
   1440 		printf("%s:%d: %s\n", __func__, __LINE__, "m_gethdr");
   1441 		return NULL;
   1442 	}
   1443 	MCLGET(m, M_DONTWAIT);
   1444 	if ((m->m_flags & M_EXT) == 0) {
   1445 		printf("%s:%d: %s\n", __func__, __LINE__, "MCLGET");
   1446 		m_freem(m);
   1447 		return NULL;
   1448 	}
   1449 	m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
   1450 
   1451 	bus_dmamap_t map = pq3etsec_mapcache_get(sc, sc->sc_rx_mapcache);
   1452 	if (map == NULL) {
   1453 		printf("%s:%d: %s\n", __func__, __LINE__, "map get");
   1454 		m_freem(m);
   1455 		return NULL;
   1456 	}
   1457 	M_SETCTX(m, map);
   1458 	m->m_len = m->m_pkthdr.len = MCLBYTES;
   1459 	int error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m,
   1460 	    BUS_DMA_READ | BUS_DMA_NOWAIT);
   1461 	if (error) {
   1462 		aprint_error_dev(sc->sc_dev, "fail to load rx dmamap: %d\n",
   1463 		    error);
   1464 		M_SETCTX(m, NULL);
   1465 		m_freem(m);
   1466 		pq3etsec_mapcache_put(sc, sc->sc_rx_mapcache, map);
   1467 		return NULL;
   1468 	}
   1469 	KASSERT(map->dm_mapsize == MCLBYTES);
   1470 	bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
   1471 	    BUS_DMASYNC_PREREAD);
   1472 
   1473 	return m;
   1474 }
   1475 
   1476 static void
   1477 pq3etsec_rx_map_unload(
   1478 	struct pq3etsec_softc *sc,
   1479 	struct mbuf *m)
   1480 {
   1481 	KASSERT(m);
   1482 	for (; m != NULL; m = m->m_next) {
   1483 		bus_dmamap_t map = M_GETCTX(m, bus_dmamap_t);
   1484 		KASSERT(map);
   1485 		KASSERT(map->dm_mapsize == MCLBYTES);
   1486 		bus_dmamap_sync(sc->sc_dmat, map, 0, m->m_len,
   1487 		    BUS_DMASYNC_POSTREAD);
   1488 		bus_dmamap_unload(sc->sc_dmat, map);
   1489 		pq3etsec_mapcache_put(sc, sc->sc_rx_mapcache, map);
   1490 		M_SETCTX(m, NULL);
   1491 	}
   1492 }
   1493 
   1494 static bool
   1495 pq3etsec_rxq_produce(
   1496 	struct pq3etsec_softc *sc,
   1497 	struct pq3etsec_rxqueue *rxq)
   1498 {
   1499 	volatile struct rxbd *producer = rxq->rxq_producer;
   1500 #if 0
   1501 	size_t inuse = rxq->rxq_inuse;
   1502 #endif
   1503 	while (rxq->rxq_inuse < rxq->rxq_threshold) {
   1504 		struct mbuf *m;
   1505 		IF_DEQUEUE(&sc->sc_rx_bufcache, m);
   1506 		if (m == NULL) {
   1507 			m = pq3etsec_rx_buf_alloc(sc);
   1508 			if (m == NULL) {
   1509 				printf("%s: pq3etsec_rx_buf_alloc failed\n", __func__);
   1510 				break;
   1511 			}
   1512 		}
   1513 		bus_dmamap_t map = M_GETCTX(m, bus_dmamap_t);
   1514 		KASSERT(map);
   1515 
   1516 #ifdef ETSEC_DEBUG
   1517 		KASSERT(rxq->rxq_mbufs[producer-rxq->rxq_first] == NULL);
   1518 		rxq->rxq_mbufs[producer-rxq->rxq_first] = m;
   1519 #endif
   1520 
   1521 		/* rxbd_len is write-only by the ETSEC */
   1522 		producer->rxbd_bufptr = map->dm_segs[0].ds_addr;
   1523 		membar_producer();
   1524 		producer->rxbd_flags |= RXBD_E;
   1525 		if (__predict_false(rxq->rxq_mhead == NULL)) {
   1526 			KASSERT(producer == rxq->rxq_consumer);
   1527 			rxq->rxq_mconsumer = m;
   1528 		}
   1529 		*rxq->rxq_mtail = m;
   1530 		rxq->rxq_mtail = &m->m_next;
   1531 		m->m_len = MCLBYTES;
   1532 		m->m_next = NULL;
   1533 		rxq->rxq_inuse++;
   1534 		if (++producer == rxq->rxq_last) {
   1535 			membar_producer();
   1536 			pq3etsec_rxq_desc_presync(sc, rxq, rxq->rxq_producer,
   1537 			    rxq->rxq_last - rxq->rxq_producer);
   1538 			producer = rxq->rxq_producer = rxq->rxq_first;
   1539 		}
   1540 	}
   1541 	if (producer != rxq->rxq_producer) {
   1542 		membar_producer();
   1543 		pq3etsec_rxq_desc_presync(sc, rxq, rxq->rxq_producer,
   1544 		    producer - rxq->rxq_producer);
   1545 		rxq->rxq_producer = producer;
   1546 	}
   1547 	uint32_t qhlt = etsec_read(sc, RSTAT) & RSTAT_QHLT;
   1548 	if (qhlt) {
   1549 		KASSERT(qhlt & rxq->rxq_qmask);
   1550 		sc->sc_ev_rx_stall.ev_count++;
   1551 		etsec_write(sc, RSTAT, RSTAT_QHLT & rxq->rxq_qmask);
   1552 	}
   1553 #if 0
   1554 	aprint_normal_dev(sc->sc_dev,
   1555 	    "%s: buffers inuse went from %zu to %zu\n",
   1556 	    __func__, inuse, rxq->rxq_inuse);
   1557 #endif
   1558 	return true;
   1559 }
   1560 
   1561 static bool
   1562 pq3etsec_rx_offload(
   1563 	struct pq3etsec_softc *sc,
   1564 	struct mbuf *m,
   1565 	const struct rxfcb *fcb)
   1566 {
   1567 	if (fcb->rxfcb_flags & RXFCB_VLN) {
   1568 		vlan_set_tag(m, fcb->rxfcb_vlctl);
   1569 	}
   1570 	if ((fcb->rxfcb_flags & RXFCB_IP) == 0
   1571 	    || (fcb->rxfcb_flags & (RXFCB_CIP | RXFCB_CTU)) == 0)
   1572 		return true;
   1573 	int csum_flags = 0;
   1574 	if ((fcb->rxfcb_flags & (RXFCB_IP6 | RXFCB_CIP)) == RXFCB_CIP) {
   1575 		csum_flags |= M_CSUM_IPv4;
   1576 		if (fcb->rxfcb_flags & RXFCB_EIP)
   1577 			csum_flags |= M_CSUM_IPv4_BAD;
   1578 	}
   1579 	if ((fcb->rxfcb_flags & RXFCB_CTU) == RXFCB_CTU) {
   1580 		int ipv_flags;
   1581 		if (fcb->rxfcb_flags & RXFCB_IP6)
   1582 			ipv_flags = M_CSUM_TCPv6 | M_CSUM_UDPv6;
   1583 		else
   1584 			ipv_flags = M_CSUM_TCPv4 | M_CSUM_UDPv4;
   1585 		if (fcb->rxfcb_pro == IPPROTO_TCP) {
   1586 			csum_flags |= (M_CSUM_TCPv4 |M_CSUM_TCPv6) & ipv_flags;
   1587 		} else {
   1588 			csum_flags |= (M_CSUM_UDPv4 |M_CSUM_UDPv6) & ipv_flags;
   1589 		}
   1590 		if (fcb->rxfcb_flags & RXFCB_ETU)
   1591 			csum_flags |= M_CSUM_TCP_UDP_BAD;
   1592 	}
   1593 
   1594 	m->m_pkthdr.csum_flags = csum_flags;
   1595 	return true;
   1596 }
   1597 
   1598 static void
   1599 pq3etsec_rx_input(
   1600 	struct pq3etsec_softc *sc,
   1601 	struct mbuf *m,
   1602 	uint16_t rxbd_flags)
   1603 {
   1604 	struct ifnet * const ifp = &sc->sc_if;
   1605 
   1606 	pq3etsec_rx_map_unload(sc, m);
   1607 
   1608 	if ((sc->sc_rctrl & RCTRL_PRSDEP) != RCTRL_PRSDEP_OFF) {
   1609 		struct rxfcb fcb = *mtod(m, struct rxfcb *);
   1610 		if (!pq3etsec_rx_offload(sc, m, &fcb))
   1611 			return;
   1612 	}
   1613 	m_adj(m, sc->sc_rx_adjlen);
   1614 
   1615 	if (rxbd_flags & RXBD_M)
   1616 		m->m_flags |= M_PROMISC;
   1617 	if (rxbd_flags & RXBD_BC)
   1618 		m->m_flags |= M_BCAST;
   1619 	if (rxbd_flags & RXBD_MC)
   1620 		m->m_flags |= M_MCAST;
   1621 	m->m_flags |= M_HASFCS;
   1622 	m_set_rcvif(m, &sc->sc_if);
   1623 
   1624 	/*
   1625 	 * Let's give it to the network subsystm to deal with.
   1626 	 */
   1627 	if_percpuq_enqueue(ifp->if_percpuq, m);
   1628 }
   1629 
   1630 static void
   1631 pq3etsec_rxq_consume(
   1632 	struct pq3etsec_softc *sc,
   1633 	struct pq3etsec_rxqueue *rxq)
   1634 {
   1635 	struct ifnet * const ifp = &sc->sc_if;
   1636 	volatile struct rxbd *consumer = rxq->rxq_consumer;
   1637 	size_t rxconsumed = 0;
   1638 
   1639 	etsec_write(sc, RSTAT, RSTAT_RXF & rxq->rxq_qmask);
   1640 
   1641 	for (;;) {
   1642 		if (consumer == rxq->rxq_producer) {
   1643 			rxq->rxq_consumer = consumer;
   1644 			rxq->rxq_inuse -= rxconsumed;
   1645 			KASSERT(rxq->rxq_inuse == 0);
   1646 			break;
   1647 		}
   1648 		pq3etsec_rxq_desc_postsync(sc, rxq, consumer, 1);
   1649 		const uint16_t rxbd_flags = consumer->rxbd_flags;
   1650 		if (rxbd_flags & RXBD_E) {
   1651 			rxq->rxq_consumer = consumer;
   1652 			rxq->rxq_inuse -= rxconsumed;
   1653 			break;
   1654 		}
   1655 		KASSERT(rxq->rxq_mconsumer != NULL);
   1656 #ifdef ETSEC_DEBUG
   1657 		KASSERT(rxq->rxq_mbufs[consumer - rxq->rxq_first] == rxq->rxq_mconsumer);
   1658 #endif
   1659 #if 0
   1660 		printf("%s: rxdb[%u]: flags=%#x len=%#x: %08x %08x %08x %08x\n",
   1661 		    __func__,
   1662 		    consumer - rxq->rxq_first, rxbd_flags, consumer->rxbd_len,
   1663 		    mtod(rxq->rxq_mconsumer, int *)[0],
   1664 		    mtod(rxq->rxq_mconsumer, int *)[1],
   1665 		    mtod(rxq->rxq_mconsumer, int *)[2],
   1666 		    mtod(rxq->rxq_mconsumer, int *)[3]);
   1667 #endif
   1668 		/*
   1669 		 * We own this packet again.  Clear all flags except wrap.
   1670 		 */
   1671 		rxconsumed++;
   1672 		consumer->rxbd_flags = rxbd_flags & (RXBD_W | RXBD_I);
   1673 
   1674 		/*
   1675 		 * If this descriptor has the LAST bit set and no errors,
   1676 		 * it's a valid input packet.
   1677 		 */
   1678 		if ((rxbd_flags & (RXBD_L | RXBD_ERRORS)) == RXBD_L) {
   1679 			size_t rxbd_len = consumer->rxbd_len;
   1680 			struct mbuf *m = rxq->rxq_mhead;
   1681 			struct mbuf *m_last = rxq->rxq_mconsumer;
   1682 			if ((rxq->rxq_mhead = m_last->m_next) == NULL)
   1683 				rxq->rxq_mtail = &rxq->rxq_mhead;
   1684 			rxq->rxq_mconsumer = rxq->rxq_mhead;
   1685 			m_last->m_next = NULL;
   1686 			m_last->m_len = rxbd_len & (MCLBYTES - 1);
   1687 			m->m_pkthdr.len = rxbd_len;
   1688 			pq3etsec_rx_input(sc, m, rxbd_flags);
   1689 		} else if (rxbd_flags & RXBD_L) {
   1690 			KASSERT(rxbd_flags & RXBD_ERRORS);
   1691 			struct mbuf *m;
   1692 			/*
   1693 			 * We encountered an error, take the mbufs and add
   1694 			 * then to the rx bufcache so we can reuse them.
   1695 			 */
   1696 			if_statinc(ifp, if_ierrors);
   1697 			for (m = rxq->rxq_mhead;
   1698 			     m != rxq->rxq_mconsumer;
   1699 			     m = m->m_next) {
   1700 				IF_ENQUEUE(&sc->sc_rx_bufcache, m);
   1701 			}
   1702 			m = rxq->rxq_mconsumer;
   1703 			if ((rxq->rxq_mhead = m->m_next) == NULL)
   1704 				rxq->rxq_mtail = &rxq->rxq_mhead;
   1705 			rxq->rxq_mconsumer = m->m_next;
   1706 			IF_ENQUEUE(&sc->sc_rx_bufcache, m);
   1707 		} else {
   1708 			rxq->rxq_mconsumer = rxq->rxq_mconsumer->m_next;
   1709 		}
   1710 #ifdef ETSEC_DEBUG
   1711 		rxq->rxq_mbufs[consumer - rxq->rxq_first] = NULL;
   1712 #endif
   1713 
   1714 		/*
   1715 		 * Wrap at the last entry!
   1716 		 */
   1717 		if (rxbd_flags & RXBD_W) {
   1718 			KASSERT(consumer + 1 == rxq->rxq_last);
   1719 			consumer = rxq->rxq_first;
   1720 		} else {
   1721 			consumer++;
   1722 		}
   1723 #ifdef ETSEC_DEBUG
   1724 		KASSERT(rxq->rxq_mbufs[consumer - rxq->rxq_first] == rxq->rxq_mconsumer);
   1725 #endif
   1726 	}
   1727 
   1728 	if (rxconsumed != 0)
   1729 		rnd_add_uint32(&sc->rnd_source, rxconsumed);
   1730 }
   1731 
   1732 static void
   1733 pq3etsec_rxq_purge(
   1734 	struct pq3etsec_softc *sc,
   1735 	struct pq3etsec_rxqueue *rxq,
   1736 	bool discard)
   1737 {
   1738 	struct mbuf *m;
   1739 
   1740 	if ((m = rxq->rxq_mhead) != NULL) {
   1741 #ifdef ETSEC_DEBUG
   1742 		memset(rxq->rxq_mbufs, 0, sizeof(rxq->rxq_mbufs));
   1743 #endif
   1744 
   1745 		if (discard) {
   1746 			pq3etsec_rx_map_unload(sc, m);
   1747 			m_freem(m);
   1748 		} else {
   1749 			while (m != NULL) {
   1750 				struct mbuf *m0 = m->m_next;
   1751 				m->m_next = NULL;
   1752 				IF_ENQUEUE(&sc->sc_rx_bufcache, m);
   1753 				m = m0;
   1754 			}
   1755 		}
   1756 	}
   1757 
   1758 	rxq->rxq_mconsumer = NULL;
   1759 	rxq->rxq_mhead = NULL;
   1760 	rxq->rxq_mtail = &rxq->rxq_mhead;
   1761 	rxq->rxq_inuse = 0;
   1762 }
   1763 
   1764 static void
   1765 pq3etsec_rxq_reset(
   1766 	struct pq3etsec_softc *sc,
   1767 	struct pq3etsec_rxqueue *rxq)
   1768 {
   1769 	/*
   1770 	 * sync all the descriptors
   1771 	 */
   1772 	pq3etsec_rxq_desc_postsync(sc, rxq, rxq->rxq_first,
   1773 	    rxq->rxq_last - rxq->rxq_first);
   1774 
   1775 	/*
   1776 	 * Make sure we own all descriptors in the ring.
   1777 	 */
   1778 	volatile struct rxbd *rxbd;
   1779 	for (rxbd = rxq->rxq_first; rxbd < rxq->rxq_last - 1; rxbd++) {
   1780 		rxbd->rxbd_flags = RXBD_I;
   1781 	}
   1782 
   1783 	/*
   1784 	 * Last descriptor has the wrap flag.
   1785 	 */
   1786 	rxbd->rxbd_flags = RXBD_W | RXBD_I;
   1787 
   1788 	/*
   1789 	 * Reset the producer consumer indexes.
   1790 	 */
   1791 	rxq->rxq_consumer = rxq->rxq_first;
   1792 	rxq->rxq_producer = rxq->rxq_first;
   1793 	rxq->rxq_inuse = 0;
   1794 	if (rxq->rxq_threshold < ETSEC_MINRXMBUFS)
   1795 		rxq->rxq_threshold = ETSEC_MINRXMBUFS;
   1796 
   1797 	sc->sc_imask |= IEVENT_RXF | IEVENT_BSY;
   1798 
   1799 	/*
   1800 	 * Restart the transmit at the first descriptor
   1801 	 */
   1802 	etsec_write(sc, rxq->rxq_reg_rbase, rxq->rxq_descmap->dm_segs->ds_addr);
   1803 }
   1804 
   1805 static int
   1806 pq3etsec_rxq_attach(
   1807 	struct pq3etsec_softc *sc,
   1808 	struct pq3etsec_rxqueue *rxq,
   1809 	u_int qno)
   1810 {
   1811 	size_t map_size = PAGE_SIZE;
   1812 	size_t desc_count = map_size / sizeof(struct rxbd);
   1813 	int error;
   1814 	void *descs;
   1815 
   1816 	error = pq3etsec_dmamem_alloc(sc->sc_dmat, map_size,
   1817 	   &rxq->rxq_descmap_seg, &rxq->rxq_descmap, &descs);
   1818 	if (error)
   1819 		return error;
   1820 
   1821 	memset(descs, 0, map_size);
   1822 	rxq->rxq_first = descs;
   1823 	rxq->rxq_last = rxq->rxq_first + desc_count;
   1824 	rxq->rxq_consumer = descs;
   1825 	rxq->rxq_producer = descs;
   1826 
   1827 	pq3etsec_rxq_purge(sc, rxq, true);
   1828 	pq3etsec_rxq_reset(sc, rxq);
   1829 
   1830 	rxq->rxq_reg_rbase = RBASEn(qno);
   1831 	rxq->rxq_qmask = RSTAT_QHLTn(qno) | RSTAT_RXFn(qno);
   1832 
   1833 	return 0;
   1834 }
   1835 
   1836 static bool
   1837 pq3etsec_txq_active_p(
   1838 	struct pq3etsec_softc * const sc,
   1839 	struct pq3etsec_txqueue *txq)
   1840 {
   1841 	return !IF_IS_EMPTY(&txq->txq_mbufs);
   1842 }
   1843 
   1844 static bool
   1845 pq3etsec_txq_fillable_p(
   1846 	struct pq3etsec_softc * const sc,
   1847 	struct pq3etsec_txqueue *txq)
   1848 {
   1849 	return txq->txq_free >= txq->txq_threshold;
   1850 }
   1851 
   1852 static int
   1853 pq3etsec_txq_attach(
   1854 	struct pq3etsec_softc *sc,
   1855 	struct pq3etsec_txqueue *txq,
   1856 	u_int qno)
   1857 {
   1858 	size_t map_size = PAGE_SIZE;
   1859 	size_t desc_count = map_size / sizeof(struct txbd);
   1860 	int error;
   1861 	void *descs;
   1862 
   1863 	error = pq3etsec_dmamem_alloc(sc->sc_dmat, map_size,
   1864 	   &txq->txq_descmap_seg, &txq->txq_descmap, &descs);
   1865 	if (error)
   1866 		return error;
   1867 
   1868 	memset(descs, 0, map_size);
   1869 	txq->txq_first = descs;
   1870 	txq->txq_last = txq->txq_first + desc_count;
   1871 	txq->txq_consumer = descs;
   1872 	txq->txq_producer = descs;
   1873 
   1874 	IFQ_SET_MAXLEN(&txq->txq_mbufs, ETSEC_MAXTXMBUFS);
   1875 
   1876 	txq->txq_reg_tbase = TBASEn(qno);
   1877 	txq->txq_qmask = TSTAT_THLTn(qno) | TSTAT_TXFn(qno);
   1878 
   1879 	pq3etsec_txq_reset(sc, txq);
   1880 
   1881 	return 0;
   1882 }
   1883 
   1884 static int
   1885 pq3etsec_txq_map_load(
   1886 	struct pq3etsec_softc *sc,
   1887 	struct pq3etsec_txqueue *txq,
   1888 	struct mbuf *m)
   1889 {
   1890 	bus_dmamap_t map;
   1891 	int error;
   1892 
   1893 	map = M_GETCTX(m, bus_dmamap_t);
   1894 	if (map != NULL)
   1895 		return 0;
   1896 
   1897 	map = pq3etsec_mapcache_get(sc, sc->sc_tx_mapcache);
   1898 	if (map == NULL)
   1899 		return ENOMEM;
   1900 
   1901 	error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m,
   1902 	    BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   1903 	if (error)
   1904 		return error;
   1905 
   1906 	bus_dmamap_sync(sc->sc_dmat, map, 0, m->m_pkthdr.len,
   1907 	    BUS_DMASYNC_PREWRITE);
   1908 	M_SETCTX(m, map);
   1909 	return 0;
   1910 }
   1911 
   1912 static void
   1913 pq3etsec_txq_map_unload(
   1914 	struct pq3etsec_softc *sc,
   1915 	struct pq3etsec_txqueue *txq,
   1916 	struct mbuf *m)
   1917 {
   1918 	KASSERT(m);
   1919 	bus_dmamap_t map = M_GETCTX(m, bus_dmamap_t);
   1920 	bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
   1921 	    BUS_DMASYNC_POSTWRITE);
   1922 	bus_dmamap_unload(sc->sc_dmat, map);
   1923 	pq3etsec_mapcache_put(sc, sc->sc_tx_mapcache, map);
   1924 }
   1925 
   1926 static bool
   1927 pq3etsec_txq_produce(
   1928 	struct pq3etsec_softc *sc,
   1929 	struct pq3etsec_txqueue *txq,
   1930 	struct mbuf *m)
   1931 {
   1932 	bus_dmamap_t map = M_GETCTX(m, bus_dmamap_t);
   1933 
   1934 	if (map->dm_nsegs > txq->txq_free)
   1935 		return false;
   1936 
   1937 	/*
   1938 	 * TCP Offload flag must be set in the first descriptor.
   1939 	 */
   1940 	volatile struct txbd *producer = txq->txq_producer;
   1941 	uint16_t last_flags = TXBD_L;
   1942 	uint16_t first_flags = TXBD_R
   1943 	    | ((m->m_flags & M_HASFCB) ? TXBD_TOE : 0);
   1944 
   1945 	/*
   1946 	 * If we've produced enough descriptors without consuming any
   1947 	 * we need to ask for an interrupt to reclaim some.
   1948 	 */
   1949 	txq->txq_lastintr += map->dm_nsegs;
   1950 	if (ETSEC_IC_TX_ENABLED(sc)
   1951 	    || txq->txq_lastintr >= txq->txq_threshold
   1952 	    || txq->txq_mbufs.ifq_len + 1 == txq->txq_mbufs.ifq_maxlen) {
   1953 		txq->txq_lastintr = 0;
   1954 		last_flags |= TXBD_I;
   1955 	}
   1956 
   1957 #ifdef ETSEC_DEBUG
   1958 	KASSERT(txq->txq_lmbufs[producer - txq->txq_first] == NULL);
   1959 #endif
   1960 	KASSERT(producer != txq->txq_last);
   1961 	producer->txbd_bufptr = map->dm_segs[0].ds_addr;
   1962 	producer->txbd_len = map->dm_segs[0].ds_len;
   1963 
   1964 	if (map->dm_nsegs > 1) {
   1965 		volatile struct txbd *start = producer + 1;
   1966 		size_t count = map->dm_nsegs - 1;
   1967 		for (u_int i = 1; i < map->dm_nsegs; i++) {
   1968 			if (__predict_false(++producer == txq->txq_last)) {
   1969 				producer = txq->txq_first;
   1970 				if (start < txq->txq_last) {
   1971 					pq3etsec_txq_desc_presync(sc, txq,
   1972 					    start, txq->txq_last - start);
   1973 					count -= txq->txq_last - start;
   1974 				}
   1975 				start = txq->txq_first;
   1976 			}
   1977 #ifdef ETSEC_DEBUG
   1978 			KASSERT(txq->txq_lmbufs[producer - txq->txq_first] == NULL);
   1979 #endif
   1980 			producer->txbd_bufptr = map->dm_segs[i].ds_addr;
   1981 			producer->txbd_len = map->dm_segs[i].ds_len;
   1982 			producer->txbd_flags = TXBD_R
   1983 			    | (producer->txbd_flags & TXBD_W)
   1984 			    | (i == map->dm_nsegs - 1 ? last_flags : 0);
   1985 #if 0
   1986 			printf("%s: txbd[%u]=%#x/%u/%#x\n", __func__, producer - txq->txq_first,
   1987 			    producer->txbd_flags, producer->txbd_len, producer->txbd_bufptr);
   1988 #endif
   1989 		}
   1990 		pq3etsec_txq_desc_presync(sc, txq, start, count);
   1991 	} else {
   1992 		first_flags |= last_flags;
   1993 	}
   1994 
   1995 	membar_producer();
   1996 	txq->txq_producer->txbd_flags =
   1997 	    first_flags | (txq->txq_producer->txbd_flags & TXBD_W);
   1998 #if 0
   1999 	printf("%s: txbd[%u]=%#x/%u/%#x\n", __func__,
   2000 	    txq->txq_producer - txq->txq_first, txq->txq_producer->txbd_flags,
   2001 	    txq->txq_producer->txbd_len, txq->txq_producer->txbd_bufptr);
   2002 #endif
   2003 	pq3etsec_txq_desc_presync(sc, txq, txq->txq_producer, 1);
   2004 
   2005 	/*
   2006 	 * Reduce free count by the number of segments we consumed.
   2007 	 */
   2008 	txq->txq_free -= map->dm_nsegs;
   2009 	KASSERT(map->dm_nsegs == 1 || txq->txq_producer != producer);
   2010 	KASSERT(map->dm_nsegs == 1 || (txq->txq_producer->txbd_flags & TXBD_L) == 0);
   2011 	KASSERT(producer->txbd_flags & TXBD_L);
   2012 #ifdef ETSEC_DEBUG
   2013 	txq->txq_lmbufs[producer - txq->txq_first] = m;
   2014 #endif
   2015 
   2016 #if 0
   2017 	printf("%s: mbuf %p: produced a %u byte packet in %u segments (%u..%u)\n",
   2018 	    __func__, m, m->m_pkthdr.len, map->dm_nsegs,
   2019 	    txq->txq_producer - txq->txq_first, producer - txq->txq_first);
   2020 #endif
   2021 
   2022 	if (++producer == txq->txq_last)
   2023 		txq->txq_producer = txq->txq_first;
   2024 	else
   2025 		txq->txq_producer = producer;
   2026 	IF_ENQUEUE(&txq->txq_mbufs, m);
   2027 
   2028 	/*
   2029 	 * Restart the transmitter.
   2030 	 */
   2031 	etsec_write(sc, TSTAT, txq->txq_qmask & TSTAT_THLT);	/* W1C */
   2032 
   2033 	return true;
   2034 }
   2035 
   2036 static void
   2037 pq3etsec_tx_offload(
   2038 	struct pq3etsec_softc *sc,
   2039 	struct pq3etsec_txqueue *txq,
   2040 	struct mbuf **mp)
   2041 {
   2042 	struct mbuf *m = *mp;
   2043 	u_int csum_flags = m->m_pkthdr.csum_flags;
   2044 	bool have_vtag;
   2045 	uint16_t vtag;
   2046 
   2047 	KASSERT(m->m_flags & M_PKTHDR);
   2048 
   2049 	have_vtag = vlan_has_tag(m);
   2050 	vtag = (have_vtag) ? vlan_get_tag(m) : 0;
   2051 
   2052 	/*
   2053 	 * Let see if we are doing any offload first.
   2054 	 */
   2055 	if (csum_flags == 0 && !have_vtag) {
   2056 		m->m_flags &= ~M_HASFCB;
   2057 		return;
   2058 	}
   2059 
   2060 	uint16_t flags = 0;
   2061 	if (csum_flags & M_CSUM_IP) {
   2062 		flags |= TXFCB_IP
   2063 		    | ((csum_flags & M_CSUM_IP6) ? TXFCB_IP6 : 0)
   2064 		    | ((csum_flags & M_CSUM_TUP) ? TXFCB_TUP : 0)
   2065 		    | ((csum_flags & M_CSUM_UDP) ? TXFCB_UDP : 0)
   2066 		    | ((csum_flags & M_CSUM_CIP) ? TXFCB_CIP : 0)
   2067 		    | ((csum_flags & M_CSUM_CTU) ? TXFCB_CTU : 0);
   2068 	}
   2069 	if (have_vtag) {
   2070 		flags |= TXFCB_VLN;
   2071 	}
   2072 	if (flags == 0) {
   2073 		m->m_flags &= ~M_HASFCB;
   2074 		return;
   2075 	}
   2076 
   2077 	struct txfcb fcb;
   2078 	fcb.txfcb_flags = flags;
   2079 	if (csum_flags & M_CSUM_IPv4)
   2080 		fcb.txfcb_l4os = M_CSUM_DATA_IPv4_IPHL(m->m_pkthdr.csum_data);
   2081 	else
   2082 		fcb.txfcb_l4os = M_CSUM_DATA_IPv6_IPHL(m->m_pkthdr.csum_data);
   2083 	fcb.txfcb_l3os = ETHER_HDR_LEN;
   2084 	fcb.txfcb_phcs = 0;
   2085 	fcb.txfcb_vlctl = vtag;
   2086 
   2087 #if 0
   2088 	printf("%s: csum_flags=%#x: txfcb flags=%#x lsos=%u l4os=%u phcs=%u vlctl=%#x\n",
   2089 	    __func__, csum_flags, fcb.txfcb_flags, fcb.txfcb_l3os, fcb.txfcb_l4os,
   2090 	    fcb.txfcb_phcs, fcb.txfcb_vlctl);
   2091 #endif
   2092 
   2093 	if (M_LEADINGSPACE(m) >= sizeof(fcb)) {
   2094 		m->m_data -= sizeof(fcb);
   2095 		m->m_len += sizeof(fcb);
   2096 	} else if (!(m->m_flags & M_EXT) && MHLEN - m->m_len >= sizeof(fcb)) {
   2097 		memmove(m->m_pktdat + sizeof(fcb), m->m_data, m->m_len);
   2098 		m->m_data = m->m_pktdat;
   2099 		m->m_len += sizeof(fcb);
   2100 	} else {
   2101 		struct mbuf *mn;
   2102 		MGET(mn, M_DONTWAIT, m->m_type);
   2103 		if (mn == NULL) {
   2104 			if (csum_flags & M_CSUM_IP4) {
   2105 #ifdef INET
   2106 				in_undefer_cksum(m, ETHER_HDR_LEN,
   2107 				    csum_flags & M_CSUM_IP4);
   2108 #else
   2109 				panic("%s: impossible M_CSUM flags %#x",
   2110 				    device_xname(sc->sc_dev), csum_flags);
   2111 #endif
   2112 			} else if (csum_flags & M_CSUM_IP6) {
   2113 #ifdef INET6
   2114 				in6_undefer_cksum(m, ETHER_HDR_LEN,
   2115 				    csum_flags & M_CSUM_IP6);
   2116 #else
   2117 				panic("%s: impossible M_CSUM flags %#x",
   2118 				    device_xname(sc->sc_dev), csum_flags);
   2119 #endif
   2120 			}
   2121 
   2122 			m->m_flags &= ~M_HASFCB;
   2123 			return;
   2124 		}
   2125 
   2126 		m_move_pkthdr(mn, m);
   2127 		mn->m_next = m;
   2128 		m = mn;
   2129 		m_align(m, sizeof(fcb));
   2130 		m->m_len = sizeof(fcb);
   2131 		*mp = m;
   2132 	}
   2133 	m->m_pkthdr.len += sizeof(fcb);
   2134 	m->m_flags |= M_HASFCB;
   2135 	*mtod(m, struct txfcb *) = fcb;
   2136 	return;
   2137 }
   2138 
   2139 static bool
   2140 pq3etsec_txq_enqueue(
   2141 	struct pq3etsec_softc *sc,
   2142 	struct pq3etsec_txqueue *txq)
   2143 {
   2144 	for (;;) {
   2145 		if (IF_QFULL(&txq->txq_mbufs))
   2146 			return false;
   2147 		struct mbuf *m = txq->txq_next;
   2148 		if (m == NULL) {
   2149 			int s = splnet();
   2150 			IFQ_DEQUEUE(&sc->sc_if.if_snd, m);
   2151 			splx(s);
   2152 			if (m == NULL)
   2153 				return true;
   2154 			M_SETCTX(m, NULL);
   2155 			pq3etsec_tx_offload(sc, txq, &m);
   2156 		} else {
   2157 			txq->txq_next = NULL;
   2158 		}
   2159 		int error = pq3etsec_txq_map_load(sc, txq, m);
   2160 		if (error) {
   2161 			aprint_error_dev(sc->sc_dev,
   2162 			    "discarded packet due to "
   2163 			    "dmamap load failure: %d\n", error);
   2164 			m_freem(m);
   2165 			continue;
   2166 		}
   2167 		KASSERT(txq->txq_next == NULL);
   2168 		if (!pq3etsec_txq_produce(sc, txq, m)) {
   2169 			txq->txq_next = m;
   2170 			return false;
   2171 		}
   2172 		KASSERT(txq->txq_next == NULL);
   2173 	}
   2174 }
   2175 
   2176 static bool
   2177 pq3etsec_txq_consume(
   2178 	struct pq3etsec_softc *sc,
   2179 	struct pq3etsec_txqueue *txq)
   2180 {
   2181 	struct ifnet * const ifp = &sc->sc_if;
   2182 	volatile struct txbd *consumer = txq->txq_consumer;
   2183 	size_t txfree = 0;
   2184 	bool ret;
   2185 
   2186 #if 0
   2187 	printf("%s: entry: free=%zu\n", __func__, txq->txq_free);
   2188 #endif
   2189 	etsec_write(sc, TSTAT, TSTAT_TXF & txq->txq_qmask);
   2190 
   2191 	for (;;) {
   2192 		if (consumer == txq->txq_producer) {
   2193 			txq->txq_consumer = consumer;
   2194 			txq->txq_free += txfree;
   2195 			txq->txq_lastintr -= uimin(txq->txq_lastintr, txfree);
   2196 			KASSERT(txq->txq_lastintr == 0);
   2197 			KASSERT(txq->txq_free ==
   2198 			    txq->txq_last - txq->txq_first - 1);
   2199 			ret = true;
   2200 			break;
   2201 		}
   2202 		pq3etsec_txq_desc_postsync(sc, txq, consumer, 1);
   2203 		const uint16_t txbd_flags = consumer->txbd_flags;
   2204 		if (txbd_flags & TXBD_R) {
   2205 			txq->txq_consumer = consumer;
   2206 			txq->txq_free += txfree;
   2207 			txq->txq_lastintr -= uimin(txq->txq_lastintr, txfree);
   2208 			ret = pq3etsec_txq_fillable_p(sc, txq);
   2209 			break;
   2210 		}
   2211 
   2212 		/*
   2213 		 * If this is the last descriptor in the chain, get the
   2214 		 * mbuf, free its dmamap, and free the mbuf chain itself.
   2215 		 */
   2216 		if (txbd_flags & TXBD_L) {
   2217 			struct mbuf *m;
   2218 
   2219 			IF_DEQUEUE(&txq->txq_mbufs, m);
   2220 #ifdef ETSEC_DEBUG
   2221 			KASSERTMSG(
   2222 			    m == txq->txq_lmbufs[consumer-txq->txq_first],
   2223 			    "%s: %p [%u]: flags %#x m (%p) != %p (%p)",
   2224 			    __func__, consumer, consumer - txq->txq_first,
   2225 			    txbd_flags, m,
   2226 			    &txq->txq_lmbufs[consumer-txq->txq_first],
   2227 			    txq->txq_lmbufs[consumer-txq->txq_first]);
   2228 #endif
   2229 			KASSERT(m);
   2230 			pq3etsec_txq_map_unload(sc, txq, m);
   2231 #if 0
   2232 			printf("%s: mbuf %p: consumed a %u byte packet\n",
   2233 			    __func__, m, m->m_pkthdr.len);
   2234 #endif
   2235 			if (m->m_flags & M_HASFCB)
   2236 				m_adj(m, sizeof(struct txfcb));
   2237 			bpf_mtap(ifp, m, BPF_D_OUT);
   2238 			net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
   2239 			if_statinc_ref(nsr, if_opackets);
   2240 			if_statadd_ref(nsr, if_obytes, m->m_pkthdr.len);
   2241 			if (m->m_flags & M_MCAST)
   2242 				if_statinc_ref(nsr, if_omcasts);
   2243 			if (txbd_flags & TXBD_ERRORS)
   2244 				if_statinc_ref(nsr, if_oerrors);
   2245 			IF_STAT_PUTREF(ifp);
   2246 			m_freem(m);
   2247 #ifdef ETSEC_DEBUG
   2248 			txq->txq_lmbufs[consumer - txq->txq_first] = NULL;
   2249 #endif
   2250 		} else {
   2251 #ifdef ETSEC_DEBUG
   2252 			KASSERT(txq->txq_lmbufs[consumer-txq->txq_first] == NULL);
   2253 #endif
   2254 		}
   2255 
   2256 		/*
   2257 		 * We own this packet again.  Clear all flags except wrap.
   2258 		 */
   2259 		txfree++;
   2260 		//consumer->txbd_flags = txbd_flags & TXBD_W;
   2261 
   2262 		/*
   2263 		 * Wrap at the last entry!
   2264 		 */
   2265 		if (txbd_flags & TXBD_W) {
   2266 			KASSERT(consumer + 1 == txq->txq_last);
   2267 			consumer = txq->txq_first;
   2268 		} else {
   2269 			consumer++;
   2270 			KASSERT(consumer < txq->txq_last);
   2271 		}
   2272 	}
   2273 
   2274 	if (txfree != 0)
   2275 		rnd_add_uint32(&sc->rnd_source, txfree);
   2276 	return ret;
   2277 }
   2278 
   2279 static void
   2280 pq3etsec_txq_purge(
   2281 	struct pq3etsec_softc *sc,
   2282 	struct pq3etsec_txqueue *txq)
   2283 {
   2284 	struct mbuf *m;
   2285 	KASSERT((etsec_read(sc, MACCFG1) & MACCFG1_TX_EN) == 0);
   2286 
   2287 	for (;;) {
   2288 		IF_DEQUEUE(&txq->txq_mbufs, m);
   2289 		if (m == NULL)
   2290 			break;
   2291 		pq3etsec_txq_map_unload(sc, txq, m);
   2292 		m_freem(m);
   2293 	}
   2294 	if ((m = txq->txq_next) != NULL) {
   2295 		txq->txq_next = NULL;
   2296 		pq3etsec_txq_map_unload(sc, txq, m);
   2297 		m_freem(m);
   2298 	}
   2299 #ifdef ETSEC_DEBUG
   2300 	memset(txq->txq_lmbufs, 0, sizeof(txq->txq_lmbufs));
   2301 #endif
   2302 }
   2303 
   2304 static void
   2305 pq3etsec_txq_reset(
   2306 	struct pq3etsec_softc *sc,
   2307 	struct pq3etsec_txqueue *txq)
   2308 {
   2309 	/*
   2310 	 * sync all the descriptors
   2311 	 */
   2312 	pq3etsec_txq_desc_postsync(sc, txq, txq->txq_first,
   2313 	    txq->txq_last - txq->txq_first);
   2314 
   2315 	/*
   2316 	 * Make sure we own all descriptors in the ring.
   2317 	 */
   2318 	volatile struct txbd *txbd;
   2319 	for (txbd = txq->txq_first; txbd < txq->txq_last - 1; txbd++) {
   2320 		txbd->txbd_flags = 0;
   2321 	}
   2322 
   2323 	/*
   2324 	 * Last descriptor has the wrap flag.
   2325 	 */
   2326 	txbd->txbd_flags = TXBD_W;
   2327 
   2328 	/*
   2329 	 * Reset the producer consumer indexes.
   2330 	 */
   2331 	txq->txq_consumer = txq->txq_first;
   2332 	txq->txq_producer = txq->txq_first;
   2333 	txq->txq_free = txq->txq_last - txq->txq_first - 1;
   2334 	txq->txq_threshold = txq->txq_free / 2;
   2335 	txq->txq_lastintr = 0;
   2336 
   2337 	/*
   2338 	 * What do we want to get interrupted on?
   2339 	 */
   2340 	sc->sc_imask |= IEVENT_TXF | IEVENT_TXE;
   2341 
   2342 	/*
   2343 	 * Restart the transmit at the first descriptor
   2344 	 */
   2345 	etsec_write(sc, txq->txq_reg_tbase, txq->txq_descmap->dm_segs->ds_addr);
   2346 }
   2347 
   2348 static void
   2349 pq3etsec_ifstart(struct ifnet *ifp)
   2350 {
   2351 	struct pq3etsec_softc * const sc = ifp->if_softc;
   2352 
   2353 	if (__predict_false((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)) {
   2354 		return;
   2355 	}
   2356 
   2357 	atomic_or_uint(&sc->sc_soft_flags, SOFT_TXINTR);
   2358 	softint_schedule(sc->sc_soft_ih);
   2359 }
   2360 
   2361 static void
   2362 pq3etsec_tx_error(
   2363 	struct pq3etsec_softc * const sc)
   2364 {
   2365 	struct pq3etsec_txqueue * const txq = &sc->sc_txq;
   2366 
   2367 	pq3etsec_txq_consume(sc, txq);
   2368 
   2369 	if (pq3etsec_txq_fillable_p(sc, txq))
   2370 		sc->sc_if.if_flags &= ~IFF_OACTIVE;
   2371 	if (sc->sc_txerrors
   2372 	    & (IEVENT_LC | IEVENT_CRL | IEVENT_XFUN | IEVENT_BABT)) {
   2373 	} else if (sc->sc_txerrors & IEVENT_EBERR) {
   2374 	}
   2375 
   2376 	if (pq3etsec_txq_active_p(sc, txq))
   2377 		etsec_write(sc, TSTAT, TSTAT_THLT & txq->txq_qmask);
   2378 	if (!pq3etsec_txq_enqueue(sc, txq)) {
   2379 		sc->sc_ev_tx_stall.ev_count++;
   2380 		sc->sc_if.if_flags |= IFF_OACTIVE;
   2381 	}
   2382 
   2383 	sc->sc_txerrors = 0;
   2384 }
   2385 
   2386 int
   2387 pq3etsec_tx_intr(void *arg)
   2388 {
   2389 	struct pq3etsec_softc * const sc = arg;
   2390 
   2391 	mutex_enter(sc->sc_hwlock);
   2392 
   2393 	sc->sc_ev_tx_intr.ev_count++;
   2394 
   2395 	uint32_t ievent = etsec_read(sc, IEVENT);
   2396 	ievent &= IEVENT_TXF | IEVENT_TXB;
   2397 	etsec_write(sc, IEVENT, ievent);	/* write 1 to clear */
   2398 
   2399 #if 0
   2400 	aprint_normal_dev(sc->sc_dev, "%s: ievent=%#x imask=%#x\n",
   2401 	    __func__, ievent, etsec_read(sc, IMASK));
   2402 #endif
   2403 
   2404 	if (ievent == 0) {
   2405 		mutex_exit(sc->sc_hwlock);
   2406 		return 0;
   2407 	}
   2408 
   2409 	sc->sc_imask &= ~(IEVENT_TXF | IEVENT_TXB);
   2410 	atomic_or_uint(&sc->sc_soft_flags, SOFT_TXINTR);
   2411 	etsec_write(sc, IMASK, sc->sc_imask);
   2412 	softint_schedule(sc->sc_soft_ih);
   2413 
   2414 	mutex_exit(sc->sc_hwlock);
   2415 
   2416 	return 1;
   2417 }
   2418 
   2419 int
   2420 pq3etsec_rx_intr(void *arg)
   2421 {
   2422 	struct pq3etsec_softc * const sc = arg;
   2423 
   2424 	mutex_enter(sc->sc_hwlock);
   2425 
   2426 	sc->sc_ev_rx_intr.ev_count++;
   2427 
   2428 	uint32_t ievent = etsec_read(sc, IEVENT);
   2429 	ievent &= IEVENT_RXF | IEVENT_RXB;
   2430 	etsec_write(sc, IEVENT, ievent);	/* write 1 to clear */
   2431 	if (ievent == 0) {
   2432 		mutex_exit(sc->sc_hwlock);
   2433 		return 0;
   2434 	}
   2435 
   2436 #if 0
   2437 	aprint_normal_dev(sc->sc_dev, "%s: ievent=%#x\n", __func__, ievent);
   2438 #endif
   2439 
   2440 	sc->sc_imask &= ~(IEVENT_RXF | IEVENT_RXB);
   2441 	atomic_or_uint(&sc->sc_soft_flags, SOFT_RXINTR);
   2442 	etsec_write(sc, IMASK, sc->sc_imask);
   2443 	softint_schedule(sc->sc_soft_ih);
   2444 
   2445 	mutex_exit(sc->sc_hwlock);
   2446 
   2447 	return 1;
   2448 }
   2449 
   2450 int
   2451 pq3etsec_error_intr(void *arg)
   2452 {
   2453 	struct pq3etsec_softc * const sc = arg;
   2454 
   2455 	mutex_enter(sc->sc_hwlock);
   2456 
   2457 	sc->sc_ev_error_intr.ev_count++;
   2458 
   2459 	for (int rv = 0, soft_flags = 0;; rv = 1) {
   2460 		uint32_t ievent = etsec_read(sc, IEVENT);
   2461 		ievent &= ~(IEVENT_RXF | IEVENT_RXB | IEVENT_TXF | IEVENT_TXB);
   2462 		etsec_write(sc, IEVENT, ievent);	/* write 1 to clear */
   2463 		if (ievent == 0) {
   2464 			if (soft_flags) {
   2465 				atomic_or_uint(&sc->sc_soft_flags, soft_flags);
   2466 				softint_schedule(sc->sc_soft_ih);
   2467 			}
   2468 			mutex_exit(sc->sc_hwlock);
   2469 			return rv;
   2470 		}
   2471 #if 0
   2472 		aprint_normal_dev(sc->sc_dev, "%s: ievent=%#x imask=%#x\n",
   2473 		    __func__, ievent, etsec_read(sc, IMASK));
   2474 #endif
   2475 
   2476 		if (ievent & (IEVENT_GRSC | IEVENT_GTSC)) {
   2477 			sc->sc_imask &= ~(IEVENT_GRSC | IEVENT_GTSC);
   2478 			etsec_write(sc, IMASK, sc->sc_imask);
   2479 			wakeup(sc);
   2480 		}
   2481 		if (ievent & (IEVENT_MMRD | IEVENT_MMWR)) {
   2482 			sc->sc_imask &= ~(IEVENT_MMRD | IEVENT_MMWR);
   2483 			etsec_write(sc, IMASK, sc->sc_imask);
   2484 			wakeup(&sc->sc_mii);
   2485 		}
   2486 		if (ievent & IEVENT_BSY) {
   2487 			soft_flags |= SOFT_RXBSY;
   2488 			sc->sc_imask &= ~IEVENT_BSY;
   2489 			etsec_write(sc, IMASK, sc->sc_imask);
   2490 		}
   2491 		if (ievent & IEVENT_TXE) {
   2492 			soft_flags |= SOFT_TXERROR;
   2493 			sc->sc_imask &= ~IEVENT_TXE;
   2494 			sc->sc_txerrors |= ievent;
   2495 		}
   2496 		if (ievent & IEVENT_TXC) {
   2497 			sc->sc_ev_tx_pause.ev_count++;
   2498 		}
   2499 		if (ievent & IEVENT_RXC) {
   2500 			sc->sc_ev_rx_pause.ev_count++;
   2501 		}
   2502 		if (ievent & IEVENT_DPE) {
   2503 			soft_flags |= SOFT_RESET;
   2504 			sc->sc_imask &= ~IEVENT_DPE;
   2505 			etsec_write(sc, IMASK, sc->sc_imask);
   2506 		}
   2507 	}
   2508 }
   2509 
   2510 void
   2511 pq3etsec_soft_intr(void *arg)
   2512 {
   2513 	struct pq3etsec_softc * const sc = arg;
   2514 	struct ifnet * const ifp = &sc->sc_if;
   2515 	uint32_t imask = 0;
   2516 
   2517 	mutex_enter(sc->sc_lock);
   2518 
   2519 	u_int soft_flags = atomic_swap_uint(&sc->sc_soft_flags, 0);
   2520 
   2521 	sc->sc_ev_soft_intr.ev_count++;
   2522 
   2523 	if (soft_flags & SOFT_RESET) {
   2524 		int s = splnet();
   2525 		pq3etsec_ifinit(ifp);
   2526 		splx(s);
   2527 		soft_flags = 0;
   2528 	}
   2529 
   2530 	if (soft_flags & SOFT_RXBSY) {
   2531 		struct pq3etsec_rxqueue * const rxq = &sc->sc_rxq;
   2532 		size_t threshold = 5 * rxq->rxq_threshold / 4;
   2533 		if (threshold >= rxq->rxq_last - rxq->rxq_first) {
   2534 			threshold = rxq->rxq_last - rxq->rxq_first - 1;
   2535 		} else {
   2536 			imask |= IEVENT_BSY;
   2537 		}
   2538 		aprint_normal_dev(sc->sc_dev,
   2539 		    "increasing receive buffers from %zu to %zu\n",
   2540 		    rxq->rxq_threshold, threshold);
   2541 		rxq->rxq_threshold = threshold;
   2542 	}
   2543 
   2544 	if ((soft_flags & SOFT_TXINTR)
   2545 	    || pq3etsec_txq_active_p(sc, &sc->sc_txq)) {
   2546 		/*
   2547 		 * Let's do what we came here for.  Consume transmitted
   2548 		 * packets off the transmit ring.
   2549 		 */
   2550 		if (!pq3etsec_txq_consume(sc, &sc->sc_txq)
   2551 		    || !pq3etsec_txq_enqueue(sc, &sc->sc_txq)) {
   2552 			sc->sc_ev_tx_stall.ev_count++;
   2553 			ifp->if_flags |= IFF_OACTIVE;
   2554 		} else {
   2555 			ifp->if_flags &= ~IFF_OACTIVE;
   2556 		}
   2557 		imask |= IEVENT_TXF;
   2558 	}
   2559 
   2560 	if (soft_flags & (SOFT_RXINTR | SOFT_RXBSY)) {
   2561 		/* Let's consume */
   2562 		pq3etsec_rxq_consume(sc, &sc->sc_rxq);
   2563 		imask |= IEVENT_RXF;
   2564 	}
   2565 
   2566 	if (soft_flags & SOFT_TXERROR) {
   2567 		pq3etsec_tx_error(sc);
   2568 		imask |= IEVENT_TXE;
   2569 	}
   2570 
   2571 	if (ifp->if_flags & IFF_RUNNING) {
   2572 		pq3etsec_rxq_produce(sc, &sc->sc_rxq);
   2573 		mutex_spin_enter(sc->sc_hwlock);
   2574 		sc->sc_imask |= imask;
   2575 		etsec_write(sc, IMASK, sc->sc_imask);
   2576 		mutex_spin_exit(sc->sc_hwlock);
   2577 	} else {
   2578 		KASSERT((soft_flags & SOFT_RXBSY) == 0);
   2579 	}
   2580 
   2581 	mutex_exit(sc->sc_lock);
   2582 }
   2583 
   2584 static void
   2585 pq3etsec_mii_tick(void *arg)
   2586 {
   2587 	struct pq3etsec_softc * const sc = arg;
   2588 	mutex_enter(sc->sc_lock);
   2589 	callout_ack(&sc->sc_mii_callout);
   2590 	sc->sc_ev_mii_ticks.ev_count++;
   2591 #ifdef DEBUG
   2592 	uint64_t now = mftb();
   2593 	if (now - sc->sc_mii_last_tick < cpu_timebase - 5000) {
   2594 		aprint_debug_dev(sc->sc_dev, "%s: diff=%"PRIu64"\n",
   2595 		    __func__, now - sc->sc_mii_last_tick);
   2596 		callout_stop(&sc->sc_mii_callout);
   2597 	}
   2598 #endif
   2599 	mii_tick(&sc->sc_mii);
   2600 	int s = splnet();
   2601 	if (sc->sc_soft_flags & SOFT_RESET)
   2602 		softint_schedule(sc->sc_soft_ih);
   2603 	splx(s);
   2604 	callout_schedule(&sc->sc_mii_callout, hz);
   2605 #ifdef DEBUG
   2606 	sc->sc_mii_last_tick = now;
   2607 #endif
   2608 	mutex_exit(sc->sc_lock);
   2609 }
   2610 
   2611 static void
   2612 pq3etsec_set_ic_rx(struct pq3etsec_softc *sc)
   2613 {
   2614 	uint32_t reg;
   2615 
   2616 	if (ETSEC_IC_RX_ENABLED(sc)) {
   2617 		reg = RXIC_ICEN;
   2618 		reg |= RXIC_ICFT_SET(sc->sc_ic_rx_count);
   2619 		reg |= RXIC_ICTT_SET(sc->sc_ic_rx_time);
   2620 	} else {
   2621 		/* Disable RX interrupt coalescing */
   2622 		reg = 0;
   2623 	}
   2624 
   2625 	etsec_write(sc, RXIC, reg);
   2626 }
   2627 
   2628 static void
   2629 pq3etsec_set_ic_tx(struct pq3etsec_softc *sc)
   2630 {
   2631 	uint32_t reg;
   2632 
   2633 	if (ETSEC_IC_TX_ENABLED(sc)) {
   2634 		reg = TXIC_ICEN;
   2635 		reg |= TXIC_ICFT_SET(sc->sc_ic_tx_count);
   2636 		reg |= TXIC_ICTT_SET(sc->sc_ic_tx_time);
   2637 	} else {
   2638 		/* Disable TX interrupt coalescing */
   2639 		reg = 0;
   2640 	}
   2641 
   2642 	etsec_write(sc, TXIC, reg);
   2643 }
   2644 
   2645 /*
   2646  * sysctl
   2647  */
   2648 static int
   2649 pq3etsec_sysctl_ic_time_helper(SYSCTLFN_ARGS, int *valuep)
   2650 {
   2651 	struct sysctlnode node = *rnode;
   2652 	struct pq3etsec_softc *sc = rnode->sysctl_data;
   2653 	int value = *valuep;
   2654 	int error;
   2655 
   2656 	node.sysctl_data = &value;
   2657 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2658 	if (error != 0 || newp == NULL)
   2659 		return error;
   2660 
   2661 	if (value < 0 || value > 65535)
   2662 		return EINVAL;
   2663 
   2664 	mutex_enter(sc->sc_lock);
   2665 	*valuep = value;
   2666 	if (valuep == &sc->sc_ic_rx_time)
   2667 		pq3etsec_set_ic_rx(sc);
   2668 	else
   2669 		pq3etsec_set_ic_tx(sc);
   2670 	mutex_exit(sc->sc_lock);
   2671 
   2672 	return 0;
   2673 }
   2674 
   2675 static int
   2676 pq3etsec_sysctl_ic_count_helper(SYSCTLFN_ARGS, int *valuep)
   2677 {
   2678 	struct sysctlnode node = *rnode;
   2679 	struct pq3etsec_softc *sc = rnode->sysctl_data;
   2680 	int value = *valuep;
   2681 	int error;
   2682 
   2683 	node.sysctl_data = &value;
   2684 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2685 	if (error != 0 || newp == NULL)
   2686 		return error;
   2687 
   2688 	if (value < 0 || value > 255)
   2689 		return EINVAL;
   2690 
   2691 	mutex_enter(sc->sc_lock);
   2692 	*valuep = value;
   2693 	if (valuep == &sc->sc_ic_rx_count)
   2694 		pq3etsec_set_ic_rx(sc);
   2695 	else
   2696 		pq3etsec_set_ic_tx(sc);
   2697 	mutex_exit(sc->sc_lock);
   2698 
   2699 	return 0;
   2700 }
   2701 
   2702 static int
   2703 pq3etsec_sysctl_ic_rx_time_helper(SYSCTLFN_ARGS)
   2704 {
   2705 	struct pq3etsec_softc *sc = rnode->sysctl_data;
   2706 
   2707 	return pq3etsec_sysctl_ic_time_helper(SYSCTLFN_CALL(rnode),
   2708 	    &sc->sc_ic_rx_time);
   2709 }
   2710 
   2711 static int
   2712 pq3etsec_sysctl_ic_rx_count_helper(SYSCTLFN_ARGS)
   2713 {
   2714 	struct pq3etsec_softc *sc = rnode->sysctl_data;
   2715 
   2716 	return pq3etsec_sysctl_ic_count_helper(SYSCTLFN_CALL(rnode),
   2717 	    &sc->sc_ic_rx_count);
   2718 }
   2719 
   2720 static int
   2721 pq3etsec_sysctl_ic_tx_time_helper(SYSCTLFN_ARGS)
   2722 {
   2723 	struct pq3etsec_softc *sc = rnode->sysctl_data;
   2724 
   2725 	return pq3etsec_sysctl_ic_time_helper(SYSCTLFN_CALL(rnode),
   2726 	    &sc->sc_ic_tx_time);
   2727 }
   2728 
   2729 static int
   2730 pq3etsec_sysctl_ic_tx_count_helper(SYSCTLFN_ARGS)
   2731 {
   2732 	struct pq3etsec_softc *sc = rnode->sysctl_data;
   2733 
   2734 	return pq3etsec_sysctl_ic_count_helper(SYSCTLFN_CALL(rnode),
   2735 	    &sc->sc_ic_tx_count);
   2736 }
   2737 
   2738 static void pq3etsec_sysctl_setup(struct sysctllog **clog,
   2739     struct pq3etsec_softc *sc)
   2740 {
   2741 	const struct sysctlnode *cnode, *rnode;
   2742 
   2743 	if (sysctl_createv(clog, 0, NULL, &rnode,
   2744 	    CTLFLAG_PERMANENT,
   2745 	    CTLTYPE_NODE, device_xname(sc->sc_dev),
   2746 	    SYSCTL_DESCR("TSEC interface"),
   2747 	    NULL, 0, NULL, 0,
   2748 	    CTL_HW, CTL_CREATE, CTL_EOL) != 0)
   2749 		goto bad;
   2750 
   2751 	if (sysctl_createv(clog, 0, &rnode, &rnode,
   2752 	    CTLFLAG_PERMANENT,
   2753 	    CTLTYPE_NODE, "int_coal",
   2754 	    SYSCTL_DESCR("Interrupts coalescing"),
   2755 	    NULL, 0, NULL, 0,
   2756 	    CTL_CREATE, CTL_EOL) != 0)
   2757 		goto bad;
   2758 
   2759 	if (sysctl_createv(clog, 0, &rnode, &cnode,
   2760 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   2761 	    CTLTYPE_INT, "rx_time",
   2762 	    SYSCTL_DESCR("RX time threshold (0-65535)"),
   2763 	    pq3etsec_sysctl_ic_rx_time_helper, 0, (void *)sc, 0,
   2764 	    CTL_CREATE, CTL_EOL) != 0)
   2765 		goto bad;
   2766 
   2767 	if (sysctl_createv(clog, 0, &rnode, &cnode,
   2768 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   2769 	    CTLTYPE_INT, "rx_count",
   2770 	    SYSCTL_DESCR("RX frame count threshold (0-255)"),
   2771 	    pq3etsec_sysctl_ic_rx_count_helper, 0, (void *)sc, 0,
   2772 	    CTL_CREATE, CTL_EOL) != 0)
   2773 		goto bad;
   2774 
   2775 	if (sysctl_createv(clog, 0, &rnode, &cnode,
   2776 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   2777 	    CTLTYPE_INT, "tx_time",
   2778 	    SYSCTL_DESCR("TX time threshold (0-65535)"),
   2779 	    pq3etsec_sysctl_ic_tx_time_helper, 0, (void *)sc, 0,
   2780 	    CTL_CREATE, CTL_EOL) != 0)
   2781 		goto bad;
   2782 
   2783 	if (sysctl_createv(clog, 0, &rnode, &cnode,
   2784 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   2785 	    CTLTYPE_INT, "tx_count",
   2786 	    SYSCTL_DESCR("TX frame count threshold (0-255)"),
   2787 	    pq3etsec_sysctl_ic_tx_count_helper, 0, (void *)sc, 0,
   2788 	    CTL_CREATE, CTL_EOL) != 0)
   2789 		goto bad;
   2790 
   2791 	return;
   2792 
   2793  bad:
   2794 	aprint_error_dev(sc->sc_dev, "could not attach sysctl nodes\n");
   2795 }
   2796