Home | History | Annotate | Line # | Download | only in usb
if_ure.c revision 1.12
      1 /*	$NetBSD: if_ure.c,v 1.12 2019/06/24 04:42:06 mrg Exp $	*/
      2 
      3 /*	$OpenBSD: if_ure.c,v 1.10 2018/11/02 21:32:30 jcs Exp $	*/
      4 /*-
      5  * Copyright (c) 2015-2016 Kevin Lo <kevlo (at) FreeBSD.org>
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  */
     29 
     30 /* RealTek RTL8152/RTL8153 10/100/Gigabit USB Ethernet device */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: if_ure.c,v 1.12 2019/06/24 04:42:06 mrg Exp $");
     34 
     35 #ifdef _KERNEL_OPT
     36 #include "opt_usb.h"
     37 #include "opt_inet.h"
     38 #endif
     39 
     40 #include <sys/param.h>
     41 #include <sys/bus.h>
     42 #include <sys/systm.h>
     43 #include <sys/sockio.h>
     44 #include <sys/mbuf.h>
     45 #include <sys/mutex.h>
     46 #include <sys/kernel.h>
     47 #include <sys/socket.h>
     48 #include <sys/device.h>
     49 
     50 #include <sys/rndsource.h>
     51 
     52 #include <net/if.h>
     53 #include <net/if_dl.h>
     54 #include <net/if_ether.h>
     55 #include <net/if_media.h>
     56 
     57 #include <net/bpf.h>
     58 
     59 #include <netinet/in.h>
     60 
     61 #include <netinet/in_offload.h>		/* XXX for in_undefer_cksum() */
     62 #ifdef INET6
     63 #include <netinet6/in6_offload.h>	/* XXX for in6_undefer_cksum() */
     64 #endif
     65 
     66 #include <dev/mii/mii.h>
     67 #include <dev/mii/miivar.h>
     68 
     69 #include <dev/usb/usb.h>
     70 #include <dev/usb/usbdi.h>
     71 #include <dev/usb/usbdi_util.h>
     72 #include <dev/usb/usbdivar.h>
     73 #include <dev/usb/usbdevs.h>
     74 
     75 #include <dev/ic/rtl81x9reg.h>		/* XXX for RTK_GMEDIASTAT */
     76 #include <dev/usb/if_urereg.h>
     77 #include <dev/usb/if_urevar.h>
     78 
     79 #define URE_PRINTF(sc, fmt, args...) \
     80 	device_printf((sc)->ure_dev, "%s: " fmt, __func__, ##args);
     81 
     82 #define URE_DEBUG
     83 #ifdef URE_DEBUG
     84 #define DPRINTF(x)	do { if (uredebug) printf x; } while (0)
     85 #define DPRINTFN(n, x)	do { if (uredebug >= (n)) printf x; } while (0)
     86 int	uredebug = 1;
     87 #else
     88 #define DPRINTF(x)
     89 #define DPRINTFN(n, x)
     90 #endif
     91 
     92 static const struct usb_devno ure_devs[] = {
     93 	{ USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_RTL8152 },
     94 	{ USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_RTL8153 }
     95 };
     96 
     97 static int	ure_match(device_t, cfdata_t, void *);
     98 static void	ure_attach(device_t, device_t, void *);
     99 static int	ure_detach(device_t, int);
    100 static int	ure_activate(device_t, enum devact);
    101 
    102 static int	ure_ctl(struct ure_softc *, uint8_t, uint16_t, uint16_t,
    103 		    void *, int);
    104 static int	ure_read_mem(struct ure_softc *, uint16_t, uint16_t, void *,
    105 		    int);
    106 static int	ure_write_mem(struct ure_softc *, uint16_t, uint16_t, void *,
    107 		    int);
    108 static uint8_t	ure_read_1(struct ure_softc *, uint16_t, uint16_t);
    109 static uint16_t	ure_read_2(struct ure_softc *, uint16_t, uint16_t);
    110 static uint32_t ure_read_4(struct ure_softc *, uint16_t, uint16_t);
    111 static int	ure_write_1(struct ure_softc *, uint16_t, uint16_t, uint32_t);
    112 static int	ure_write_2(struct ure_softc *, uint16_t, uint16_t, uint32_t);
    113 static int	ure_write_4(struct ure_softc *, uint16_t, uint16_t, uint32_t);
    114 static uint16_t	ure_ocp_reg_read(struct ure_softc *, uint16_t);
    115 static void	ure_ocp_reg_write(struct ure_softc *, uint16_t, uint16_t);
    116 
    117 static int	ure_init(struct ifnet *);
    118 static void	ure_stop(struct ifnet *, int);
    119 static void	ure_start(struct ifnet *);
    120 static void	ure_reset(struct ure_softc *);
    121 static void	ure_miibus_statchg(struct ifnet *);
    122 static int	ure_miibus_readreg(device_t, int, int, uint16_t *);
    123 static int	ure_miibus_writereg(device_t, int, int, uint16_t);
    124 static void	ure_lock_mii(struct ure_softc *);
    125 static void	ure_unlock_mii(struct ure_softc *);
    126 
    127 static int	ure_encap(struct ure_softc *, struct mbuf *, int);
    128 static uint32_t	ure_txcsum(struct mbuf *);
    129 static void	ure_rxeof(struct usbd_xfer *, void *, usbd_status);
    130 static int	ure_rxcsum(struct ifnet *, struct ure_rxpkt *);
    131 static void	ure_txeof(struct usbd_xfer *, void *, usbd_status);
    132 static int	ure_rx_list_init(struct ure_softc *);
    133 static int	ure_tx_list_init(struct ure_softc *);
    134 
    135 static void	ure_tick_task(void *);
    136 static void	ure_tick(void *);
    137 
    138 static int	ure_ifmedia_upd(struct ifnet *);
    139 static void	ure_ifmedia_sts(struct ifnet *, struct ifmediareq *);
    140 static int	ure_ioctl(struct ifnet *, u_long, void *);
    141 static void	ure_rtl8152_init(struct ure_softc *);
    142 static void	ure_rtl8153_init(struct ure_softc *);
    143 static void	ure_disable_teredo(struct ure_softc *);
    144 static void	ure_init_fifo(struct ure_softc *);
    145 
    146 CFATTACH_DECL_NEW(ure, sizeof(struct ure_softc), ure_match, ure_attach,
    147     ure_detach, ure_activate);
    148 
    149 static int
    150 ure_ctl(struct ure_softc *sc, uint8_t rw, uint16_t val, uint16_t index,
    151     void *buf, int len)
    152 {
    153 	usb_device_request_t req;
    154 	usbd_status err;
    155 
    156 	if (sc->ure_dying)
    157 		return 0;
    158 
    159 	if (rw == URE_CTL_WRITE)
    160 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    161 	else
    162 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
    163 	req.bRequest = UR_SET_ADDRESS;
    164 	USETW(req.wValue, val);
    165 	USETW(req.wIndex, index);
    166 	USETW(req.wLength, len);
    167 
    168 	DPRINTFN(5, ("ure_ctl: rw %d, val 0x%04hu, index 0x%04hu, len %d\n",
    169 	    rw, val, index, len));
    170 	err = usbd_do_request(sc->ure_udev, &req, buf);
    171 	if (err) {
    172 		DPRINTF(("ure_ctl: error %d\n", err));
    173 		return -1;
    174 	}
    175 
    176 	return 0;
    177 }
    178 
    179 static int
    180 ure_read_mem(struct ure_softc *sc, uint16_t addr, uint16_t index,
    181     void *buf, int len)
    182 {
    183 
    184 	return ure_ctl(sc, URE_CTL_READ, addr, index, buf, len);
    185 }
    186 
    187 static int
    188 ure_write_mem(struct ure_softc *sc, uint16_t addr, uint16_t index,
    189     void *buf, int len)
    190 {
    191 
    192 	return ure_ctl(sc, URE_CTL_WRITE, addr, index, buf, len);
    193 }
    194 
    195 static uint8_t
    196 ure_read_1(struct ure_softc *sc, uint16_t reg, uint16_t index)
    197 {
    198 	uint32_t val;
    199 	uint8_t temp[4];
    200 	uint8_t shift;
    201 
    202 	shift = (reg & 3) << 3;
    203 	reg &= ~3;
    204 
    205 	ure_read_mem(sc, reg, index, &temp, 4);
    206 	val = UGETDW(temp);
    207 	val >>= shift;
    208 
    209 	return val & 0xff;
    210 }
    211 
    212 static uint16_t
    213 ure_read_2(struct ure_softc *sc, uint16_t reg, uint16_t index)
    214 {
    215 	uint32_t val;
    216 	uint8_t temp[4];
    217 	uint8_t shift;
    218 
    219 	shift = (reg & 2) << 3;
    220 	reg &= ~3;
    221 
    222 	ure_read_mem(sc, reg, index, &temp, 4);
    223 	val = UGETDW(temp);
    224 	val >>= shift;
    225 
    226 	return val & 0xffff;
    227 }
    228 
    229 static uint32_t
    230 ure_read_4(struct ure_softc *sc, uint16_t reg, uint16_t index)
    231 {
    232 	uint8_t temp[4];
    233 
    234 	ure_read_mem(sc, reg, index, &temp, 4);
    235 	return UGETDW(temp);
    236 }
    237 
    238 static int
    239 ure_write_1(struct ure_softc *sc, uint16_t reg, uint16_t index, uint32_t val)
    240 {
    241 	uint16_t byen;
    242 	uint8_t temp[4];
    243 	uint8_t shift;
    244 
    245 	byen = URE_BYTE_EN_BYTE;
    246 	shift = reg & 3;
    247 	val &= 0xff;
    248 
    249 	if (reg & 3) {
    250 		byen <<= shift;
    251 		val <<= (shift << 3);
    252 		reg &= ~3;
    253 	}
    254 
    255 	USETDW(temp, val);
    256 	return ure_write_mem(sc, reg, index | byen, &temp, 4);
    257 }
    258 
    259 static int
    260 ure_write_2(struct ure_softc *sc, uint16_t reg, uint16_t index, uint32_t val)
    261 {
    262 	uint16_t byen;
    263 	uint8_t temp[4];
    264 	uint8_t shift;
    265 
    266 	byen = URE_BYTE_EN_WORD;
    267 	shift = reg & 2;
    268 	val &= 0xffff;
    269 
    270 	if (reg & 2) {
    271 		byen <<= shift;
    272 		val <<= (shift << 3);
    273 		reg &= ~3;
    274 	}
    275 
    276 	USETDW(temp, val);
    277 	return ure_write_mem(sc, reg, index | byen, &temp, 4);
    278 }
    279 
    280 static int
    281 ure_write_4(struct ure_softc *sc, uint16_t reg, uint16_t index, uint32_t val)
    282 {
    283 	uint8_t temp[4];
    284 
    285 	USETDW(temp, val);
    286 	return ure_write_mem(sc, reg, index | URE_BYTE_EN_DWORD, &temp, 4);
    287 }
    288 
    289 static uint16_t
    290 ure_ocp_reg_read(struct ure_softc *sc, uint16_t addr)
    291 {
    292 	uint16_t reg;
    293 
    294 	ure_write_2(sc, URE_PLA_OCP_GPHY_BASE, URE_MCU_TYPE_PLA, addr & 0xf000);
    295 	reg = (addr & 0x0fff) | 0xb000;
    296 
    297 	return ure_read_2(sc, reg, URE_MCU_TYPE_PLA);
    298 }
    299 
    300 static void
    301 ure_ocp_reg_write(struct ure_softc *sc, uint16_t addr, uint16_t data)
    302 {
    303 	uint16_t reg;
    304 
    305 	ure_write_2(sc, URE_PLA_OCP_GPHY_BASE, URE_MCU_TYPE_PLA, addr & 0xf000);
    306 	reg = (addr & 0x0fff) | 0xb000;
    307 
    308 	ure_write_2(sc, reg, URE_MCU_TYPE_PLA, data);
    309 }
    310 
    311 static int
    312 ure_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val)
    313 {
    314 	struct ure_softc *sc = device_private(dev);
    315 
    316 	mutex_enter(&sc->ure_lock);
    317 	if (sc->ure_dying || sc->ure_phyno != phy) {
    318 		mutex_exit(&sc->ure_lock);
    319 		return -1;
    320 	}
    321 	mutex_exit(&sc->ure_lock);
    322 
    323 	/* Let the rgephy driver read the URE_PLA_PHYSTATUS register. */
    324 	if (reg == RTK_GMEDIASTAT) {
    325 		*val = ure_read_1(sc, URE_PLA_PHYSTATUS, URE_MCU_TYPE_PLA);
    326 		return 0;
    327 	}
    328 
    329 	ure_lock_mii(sc);
    330 	*val = ure_ocp_reg_read(sc, URE_OCP_BASE_MII + reg * 2);
    331 	ure_unlock_mii(sc);
    332 
    333 	return 0;
    334 }
    335 
    336 static int
    337 ure_miibus_writereg(device_t dev, int phy, int reg, uint16_t val)
    338 {
    339 	struct ure_softc *sc = device_private(dev);
    340 
    341 	mutex_enter(&sc->ure_lock);
    342 	if (sc->ure_dying || sc->ure_phyno != phy) {
    343 		mutex_exit(&sc->ure_lock);
    344 		return -1;
    345 	}
    346 	mutex_exit(&sc->ure_lock);
    347 
    348 	ure_lock_mii(sc);
    349 	ure_ocp_reg_write(sc, URE_OCP_BASE_MII + reg * 2, val);
    350 	ure_unlock_mii(sc);
    351 
    352 	return 0;
    353 }
    354 
    355 static void
    356 ure_miibus_statchg(struct ifnet *ifp)
    357 {
    358 	struct ure_softc *sc;
    359 	struct mii_data *mii;
    360 
    361 	if (ifp == NULL || (ifp->if_flags & IFF_RUNNING) == 0)
    362 		return;
    363 
    364 	sc = ifp->if_softc;
    365 	mii = GET_MII(sc);
    366 
    367 	if (mii == NULL)
    368 		return;
    369 
    370 	sc->ure_flags &= ~URE_FLAG_LINK;
    371 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
    372 	    (IFM_ACTIVE | IFM_AVALID)) {
    373 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
    374 		case IFM_10_T:
    375 		case IFM_100_TX:
    376 			sc->ure_flags |= URE_FLAG_LINK;
    377 			break;
    378 		case IFM_1000_T:
    379 			if ((sc->ure_flags & URE_FLAG_8152) != 0)
    380 				break;
    381 			sc->ure_flags |= URE_FLAG_LINK;
    382 			break;
    383 		default:
    384 			break;
    385 		}
    386 	}
    387 }
    388 
    389 static int
    390 ure_ifmedia_upd(struct ifnet *ifp)
    391 {
    392 	struct ure_softc *sc = ifp->if_softc;
    393 	struct mii_data *mii = GET_MII(sc);
    394 	int err;
    395 
    396 	sc->ure_flags &= ~URE_FLAG_LINK;
    397 	if (mii->mii_instance) {
    398 		struct mii_softc *miisc;
    399 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
    400 			mii_phy_reset(miisc);
    401 	}
    402 
    403 	err = mii_mediachg(mii);
    404 	if (err == ENXIO)
    405 		return 0;	/* XXX */
    406 	else
    407 		return err;
    408 }
    409 
    410 static void
    411 ure_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
    412 {
    413 	struct ure_softc *sc = ifp->if_softc;
    414 	struct mii_data *mii = GET_MII(sc);
    415 
    416 	mii_pollstat(mii);
    417 	ifmr->ifm_active = mii->mii_media_active;
    418 	ifmr->ifm_status = mii->mii_media_status;
    419 }
    420 
    421 static void
    422 ure_iff_locked(struct ure_softc *sc)
    423 {
    424 	struct ethercom *ec = &sc->ure_ec;
    425 	struct ifnet *ifp = GET_IFP(sc);
    426 	struct ether_multi *enm;
    427 	struct ether_multistep step;
    428 	uint32_t hashes[2] = { 0, 0 };
    429 	uint32_t hash;
    430 	uint32_t rxmode;
    431 
    432 	KASSERT(mutex_owned(&sc->ure_lock));
    433 
    434 	if (sc->ure_dying)
    435 		return;
    436 
    437 	rxmode = ure_read_4(sc, URE_PLA_RCR, URE_MCU_TYPE_PLA);
    438 	rxmode &= ~URE_RCR_ACPT_ALL;
    439 	ifp->if_flags &= ~IFF_ALLMULTI;
    440 
    441 	/*
    442 	 * Always accept frames destined to our station address.
    443 	 * Always accept broadcast frames.
    444 	 */
    445 	rxmode |= URE_RCR_APM | URE_RCR_AB;
    446 
    447 	if (ifp->if_flags & IFF_PROMISC) {
    448 		rxmode |= URE_RCR_AAP;
    449 allmulti:	ifp->if_flags |= IFF_ALLMULTI;
    450 		rxmode |= URE_RCR_AM;
    451 		hashes[0] = hashes[1] = 0xffffffff;
    452 	} else {
    453 		rxmode |= URE_RCR_AM;
    454 
    455 		ETHER_LOCK(ec);
    456 		ETHER_FIRST_MULTI(step, ec, enm);
    457 		while (enm != NULL) {
    458 			if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    459 			    ETHER_ADDR_LEN)) {
    460 				ETHER_UNLOCK(ec);
    461 				goto allmulti;
    462 			}
    463 
    464 			hash = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN)
    465 			    >> 26;
    466 			if (hash < 32)
    467 				hashes[0] |= (1 << hash);
    468 			else
    469 				hashes[1] |= (1 << (hash - 32));
    470 
    471 			ETHER_NEXT_MULTI(step, enm);
    472 		}
    473 		ETHER_UNLOCK(ec);
    474 
    475 		hash = bswap32(hashes[0]);
    476 		hashes[0] = bswap32(hashes[1]);
    477 		hashes[1] = hash;
    478 	}
    479 
    480 	ure_write_4(sc, URE_PLA_MAR0, URE_MCU_TYPE_PLA, hashes[0]);
    481 	ure_write_4(sc, URE_PLA_MAR4, URE_MCU_TYPE_PLA, hashes[1]);
    482 	ure_write_4(sc, URE_PLA_RCR, URE_MCU_TYPE_PLA, rxmode);
    483 }
    484 
    485 static void
    486 ure_iff(struct ure_softc *sc)
    487 {
    488 
    489 	mutex_enter(&sc->ure_lock);
    490 	ure_iff_locked(sc);
    491 	mutex_exit(&sc->ure_lock);
    492 }
    493 
    494 static void
    495 ure_reset(struct ure_softc *sc)
    496 {
    497 	int i;
    498 
    499 	KASSERT(mutex_owned(&sc->ure_lock));
    500 
    501 	ure_write_1(sc, URE_PLA_CR, URE_MCU_TYPE_PLA, URE_CR_RST);
    502 
    503 	for (i = 0; i < URE_TIMEOUT; i++) {
    504 		if (!(ure_read_1(sc, URE_PLA_CR, URE_MCU_TYPE_PLA) &
    505 		    URE_CR_RST))
    506 			break;
    507 		usbd_delay_ms(sc->ure_udev, 10);
    508 	}
    509 	if (i == URE_TIMEOUT)
    510 		URE_PRINTF(sc, "reset never completed\n");
    511 }
    512 
    513 static int
    514 ure_init_locked(struct ifnet *ifp)
    515 {
    516 	struct ure_softc * const sc = ifp->if_softc;
    517 	struct ure_chain *c;
    518 	usbd_status err;
    519 	int i;
    520 	uint8_t eaddr[8];
    521 
    522 	KASSERT(mutex_owned(&sc->ure_lock));
    523 
    524 	if (sc->ure_dying)
    525 		return EIO;
    526 
    527 	/* Cancel pending I/O. */
    528 	if (ifp->if_flags & IFF_RUNNING)
    529 		ure_stop(ifp, 1);
    530 
    531 	/* Set MAC address. */
    532 	memset(eaddr, 0, sizeof(eaddr));
    533 	memcpy(eaddr, CLLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
    534 	ure_write_1(sc, URE_PLA_CRWECR, URE_MCU_TYPE_PLA, URE_CRWECR_CONFIG);
    535 	ure_write_mem(sc, URE_PLA_IDR, URE_MCU_TYPE_PLA | URE_BYTE_EN_SIX_BYTES,
    536 	    eaddr, 8);
    537 	ure_write_1(sc, URE_PLA_CRWECR, URE_MCU_TYPE_PLA, URE_CRWECR_NORAML);
    538 
    539 	/* Reset the packet filter. */
    540 	ure_write_2(sc, URE_PLA_FMC, URE_MCU_TYPE_PLA,
    541 	    ure_read_2(sc, URE_PLA_FMC, URE_MCU_TYPE_PLA) &
    542 	    ~URE_FMC_FCR_MCU_EN);
    543 	ure_write_2(sc, URE_PLA_FMC, URE_MCU_TYPE_PLA,
    544 	    ure_read_2(sc, URE_PLA_FMC, URE_MCU_TYPE_PLA) |
    545 	    URE_FMC_FCR_MCU_EN);
    546 
    547 	/* Enable transmit and receive. */
    548 	ure_write_1(sc, URE_PLA_CR, URE_MCU_TYPE_PLA,
    549 	    ure_read_1(sc, URE_PLA_CR, URE_MCU_TYPE_PLA) | URE_CR_RE |
    550 	    URE_CR_TE);
    551 
    552 	ure_write_2(sc, URE_PLA_MISC_1, URE_MCU_TYPE_PLA,
    553 	    ure_read_2(sc, URE_PLA_MISC_1, URE_MCU_TYPE_PLA) &
    554 	    ~URE_RXDY_GATED_EN);
    555 
    556 	/* Load the multicast filter. */
    557 	ure_iff_locked(sc);
    558 
    559 	/* Open RX and TX pipes. */
    560 	err = usbd_open_pipe(sc->ure_iface, sc->ure_ed[URE_ENDPT_RX],
    561 	    USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->ure_ep[URE_ENDPT_RX]);
    562 	if (err) {
    563 		URE_PRINTF(sc, "open rx pipe failed: %s\n", usbd_errstr(err));
    564 		return EIO;
    565 	}
    566 
    567 	err = usbd_open_pipe(sc->ure_iface, sc->ure_ed[URE_ENDPT_TX],
    568 	    USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->ure_ep[URE_ENDPT_TX]);
    569 	if (err) {
    570 		URE_PRINTF(sc, "open tx pipe failed: %s\n", usbd_errstr(err));
    571 		return EIO;
    572 	}
    573 
    574 	if (ure_rx_list_init(sc)) {
    575 		URE_PRINTF(sc, "rx list init failed\n");
    576 		return ENOBUFS;
    577 	}
    578 
    579 	if (ure_tx_list_init(sc)) {
    580 		URE_PRINTF(sc, "tx list init failed\n");
    581 		return ENOBUFS;
    582 	}
    583 
    584 	mutex_enter(&sc->ure_rxlock);
    585 	mutex_enter(&sc->ure_txlock);
    586 	sc->ure_stopping = false;
    587 
    588 	/* Start up the receive pipe. */
    589 	for (i = 0; i < URE_RX_LIST_CNT; i++) {
    590 		c = &sc->ure_cdata.rx_chain[i];
    591 		usbd_setup_xfer(c->uc_xfer, c, c->uc_buf, sc->ure_bufsz,
    592 		    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ure_rxeof);
    593 		usbd_transfer(c->uc_xfer);
    594 	}
    595 
    596 	mutex_exit(&sc->ure_txlock);
    597 	mutex_exit(&sc->ure_rxlock);
    598 
    599 	/* Indicate we are up and running. */
    600 	ifp->if_flags |= IFF_RUNNING;
    601 	ifp->if_flags &= ~IFF_OACTIVE;
    602 
    603 	callout_reset(&sc->ure_stat_ch, hz, ure_tick, sc);
    604 
    605 	return 0;
    606 }
    607 
    608 static int
    609 ure_init(struct ifnet *ifp)
    610 {
    611 	struct ure_softc * const sc = ifp->if_softc;
    612 
    613 	mutex_enter(&sc->ure_lock);
    614 	int ret = ure_init_locked(ifp);
    615 	mutex_exit(&sc->ure_lock);
    616 
    617 	return ret;
    618 }
    619 
    620 static void
    621 ure_start_locked(struct ifnet *ifp)
    622 {
    623 	struct ure_softc *sc = ifp->if_softc;
    624 	struct mbuf *m;
    625 	struct ure_cdata *cd = &sc->ure_cdata;
    626 	int idx;
    627 
    628 	if (sc->ure_dying || sc->ure_stopping ||
    629 	    (sc->ure_flags & URE_FLAG_LINK) == 0 ||
    630 	    (ifp->if_flags & (IFF_OACTIVE | IFF_RUNNING)) != IFF_RUNNING) {
    631 		return;
    632 	}
    633 
    634 	idx = cd->tx_prod;
    635 	while (cd->tx_cnt < URE_TX_LIST_CNT) {
    636 		IFQ_POLL(&ifp->if_snd, m);
    637 		if (m == NULL)
    638 			break;
    639 
    640 		if (ure_encap(sc, m, idx)) {
    641 			ifp->if_oerrors++;
    642 			break;
    643 		}
    644 		IFQ_DEQUEUE(&ifp->if_snd, m);
    645 
    646 		bpf_mtap(ifp, m, BPF_D_OUT);
    647 		m_freem(m);
    648 
    649 		idx = (idx + 1) % URE_TX_LIST_CNT;
    650 		cd->tx_cnt++;
    651 	}
    652 	cd->tx_prod = idx;
    653 
    654 	if (cd->tx_cnt >= URE_TX_LIST_CNT)
    655 		ifp->if_flags |= IFF_OACTIVE;
    656 }
    657 
    658 static void
    659 ure_start(struct ifnet *ifp)
    660 {
    661 	struct ure_softc * const sc = ifp->if_softc;
    662 
    663 	mutex_enter(&sc->ure_txlock);
    664 	ure_start_locked(ifp);
    665 	mutex_exit(&sc->ure_txlock);
    666 }
    667 
    668 static void
    669 ure_tick(void *xsc)
    670 {
    671 	struct ure_softc *sc = xsc;
    672 
    673 	if (sc == NULL)
    674 		return;
    675 
    676 	mutex_enter(&sc->ure_lock);
    677 	if (!sc->ure_stopping && !sc->ure_dying) {
    678 		/* Perform periodic stuff in process context */
    679 		usb_add_task(sc->ure_udev, &sc->ure_tick_task, USB_TASKQ_DRIVER);
    680 	}
    681 	mutex_exit(&sc->ure_lock);
    682 }
    683 
    684 static void
    685 ure_stop_locked(struct ifnet *ifp, int disable __unused)
    686 {
    687 	struct ure_softc *sc = ifp->if_softc;
    688 	struct ure_chain *c;
    689 	usbd_status err;
    690 	int i;
    691 
    692 	KASSERT(mutex_owned(&sc->ure_lock));
    693 	mutex_enter(&sc->ure_rxlock);
    694 	mutex_enter(&sc->ure_txlock);
    695 	sc->ure_stopping = true;
    696 	mutex_exit(&sc->ure_txlock);
    697 	mutex_exit(&sc->ure_rxlock);
    698 
    699 	ure_reset(sc);
    700 
    701 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    702 
    703 	callout_stop(&sc->ure_stat_ch);
    704 
    705 	sc->ure_flags &= ~URE_FLAG_LINK; /* XXX */
    706 
    707 	if (sc->ure_ep[URE_ENDPT_RX] != NULL) {
    708 		err = usbd_abort_pipe(sc->ure_ep[URE_ENDPT_RX]);
    709 		if (err)
    710 			URE_PRINTF(sc, "abort rx pipe failed: %s\n",
    711 			    usbd_errstr(err));
    712 	}
    713 
    714 	if (sc->ure_ep[URE_ENDPT_TX] != NULL) {
    715 		err = usbd_abort_pipe(sc->ure_ep[URE_ENDPT_TX]);
    716 		if (err)
    717 			URE_PRINTF(sc, "abort tx pipe failed: %s\n",
    718 			    usbd_errstr(err));
    719 	}
    720 
    721 	for (i = 0; i < URE_RX_LIST_CNT; i++) {
    722 		c = &sc->ure_cdata.rx_chain[i];
    723 		if (c->uc_xfer != NULL) {
    724 			usbd_destroy_xfer(c->uc_xfer);
    725 			c->uc_xfer = NULL;
    726 		}
    727 	}
    728 
    729 	for (i = 0; i < URE_TX_LIST_CNT; i++) {
    730 		c = &sc->ure_cdata.tx_chain[i];
    731 		if (c->uc_xfer != NULL) {
    732 			usbd_destroy_xfer(c->uc_xfer);
    733 			c->uc_xfer = NULL;
    734 		}
    735 	}
    736 
    737 	if (sc->ure_ep[URE_ENDPT_RX] != NULL) {
    738 		err = usbd_close_pipe(sc->ure_ep[URE_ENDPT_RX]);
    739 		if (err)
    740 			URE_PRINTF(sc, "close rx pipe failed: %s\n",
    741 			    usbd_errstr(err));
    742 		sc->ure_ep[URE_ENDPT_RX] = NULL;
    743 	}
    744 
    745 	if (sc->ure_ep[URE_ENDPT_TX] != NULL) {
    746 		err = usbd_close_pipe(sc->ure_ep[URE_ENDPT_TX]);
    747 		if (err)
    748 			URE_PRINTF(sc, "close tx pipe failed: %s\n",
    749 			    usbd_errstr(err));
    750 		sc->ure_ep[URE_ENDPT_TX] = NULL;
    751 	}
    752 }
    753 
    754 static void
    755 ure_stop(struct ifnet *ifp, int disable __unused)
    756 {
    757 	struct ure_softc * const sc = ifp->if_softc;
    758 
    759 	mutex_enter(&sc->ure_lock);
    760 	ure_stop_locked(ifp, disable);
    761 	mutex_exit(&sc->ure_lock);
    762 }
    763 
    764 static void
    765 ure_rtl8152_init(struct ure_softc *sc)
    766 {
    767 	uint32_t pwrctrl;
    768 
    769 	/* Disable ALDPS. */
    770 	ure_ocp_reg_write(sc, URE_OCP_ALDPS_CONFIG, URE_ENPDNPS | URE_LINKENA |
    771 	    URE_DIS_SDSAVE);
    772 	usbd_delay_ms(sc->ure_udev, 20);
    773 
    774 	if (sc->ure_chip & URE_CHIP_VER_4C00) {
    775 		ure_write_2(sc, URE_PLA_LED_FEATURE, URE_MCU_TYPE_PLA,
    776 		    ure_read_2(sc, URE_PLA_LED_FEATURE, URE_MCU_TYPE_PLA) &
    777 		    ~URE_LED_MODE_MASK);
    778 	}
    779 
    780 	ure_write_2(sc, URE_USB_UPS_CTRL, URE_MCU_TYPE_USB,
    781 	    ure_read_2(sc, URE_USB_UPS_CTRL, URE_MCU_TYPE_USB) &
    782 	    ~URE_POWER_CUT);
    783 	ure_write_2(sc, URE_USB_PM_CTRL_STATUS, URE_MCU_TYPE_USB,
    784 	    ure_read_2(sc, URE_USB_PM_CTRL_STATUS, URE_MCU_TYPE_USB) &
    785 	    ~URE_RESUME_INDICATE);
    786 
    787 	ure_write_2(sc, URE_PLA_PHY_PWR, URE_MCU_TYPE_PLA,
    788 	    ure_read_2(sc, URE_PLA_PHY_PWR, URE_MCU_TYPE_PLA) |
    789 	    URE_TX_10M_IDLE_EN | URE_PFM_PWM_SWITCH);
    790 	pwrctrl = ure_read_4(sc, URE_PLA_MAC_PWR_CTRL, URE_MCU_TYPE_PLA);
    791 	pwrctrl &= ~URE_MCU_CLK_RATIO_MASK;
    792 	pwrctrl |= URE_MCU_CLK_RATIO | URE_D3_CLK_GATED_EN;
    793 	ure_write_4(sc, URE_PLA_MAC_PWR_CTRL, URE_MCU_TYPE_PLA, pwrctrl);
    794 	ure_write_2(sc, URE_PLA_GPHY_INTR_IMR, URE_MCU_TYPE_PLA,
    795 	    URE_GPHY_STS_MSK | URE_SPEED_DOWN_MSK | URE_SPDWN_RXDV_MSK |
    796 	    URE_SPDWN_LINKCHG_MSK);
    797 
    798 	/* Enable Rx aggregation. */
    799 	ure_write_2(sc, URE_USB_USB_CTRL, URE_MCU_TYPE_USB,
    800 	    ure_read_2(sc, URE_USB_USB_CTRL, URE_MCU_TYPE_USB) &
    801 	    ~URE_RX_AGG_DISABLE);
    802 
    803 	/* Disable ALDPS. */
    804 	ure_ocp_reg_write(sc, URE_OCP_ALDPS_CONFIG, URE_ENPDNPS | URE_LINKENA |
    805 	    URE_DIS_SDSAVE);
    806 	usbd_delay_ms(sc->ure_udev, 20);
    807 
    808 	ure_init_fifo(sc);
    809 
    810 	ure_write_1(sc, URE_USB_TX_AGG, URE_MCU_TYPE_USB,
    811 	    URE_TX_AGG_MAX_THRESHOLD);
    812 	ure_write_4(sc, URE_USB_RX_BUF_TH, URE_MCU_TYPE_USB, URE_RX_THR_HIGH);
    813 	ure_write_4(sc, URE_USB_TX_DMA, URE_MCU_TYPE_USB,
    814 	    URE_TEST_MODE_DISABLE | URE_TX_SIZE_ADJUST1);
    815 }
    816 
    817 static void
    818 ure_rtl8153_init(struct ure_softc *sc)
    819 {
    820 	uint16_t val;
    821 	uint8_t u1u2[8];
    822 	int i;
    823 
    824 	/* Disable ALDPS. */
    825 	ure_ocp_reg_write(sc, URE_OCP_POWER_CFG,
    826 	    ure_ocp_reg_read(sc, URE_OCP_POWER_CFG) & ~URE_EN_ALDPS);
    827 	usbd_delay_ms(sc->ure_udev, 20);
    828 
    829 	memset(u1u2, 0x00, sizeof(u1u2));
    830 	ure_write_mem(sc, URE_USB_TOLERANCE,
    831 	    URE_MCU_TYPE_USB | URE_BYTE_EN_SIX_BYTES, u1u2, sizeof(u1u2));
    832 
    833 	for (i = 0; i < URE_TIMEOUT; i++) {
    834 		if (ure_read_2(sc, URE_PLA_BOOT_CTRL, URE_MCU_TYPE_PLA) &
    835 		    URE_AUTOLOAD_DONE)
    836 			break;
    837 		usbd_delay_ms(sc->ure_udev, 10);
    838 	}
    839 	if (i == URE_TIMEOUT)
    840 		URE_PRINTF(sc, "timeout waiting for chip autoload\n");
    841 
    842 	for (i = 0; i < URE_TIMEOUT; i++) {
    843 		val = ure_ocp_reg_read(sc, URE_OCP_PHY_STATUS) &
    844 		    URE_PHY_STAT_MASK;
    845 		if (val == URE_PHY_STAT_LAN_ON || val == URE_PHY_STAT_PWRDN)
    846 			break;
    847 		usbd_delay_ms(sc->ure_udev, 10);
    848 	}
    849 	if (i == URE_TIMEOUT)
    850 		URE_PRINTF(sc, "timeout waiting for phy to stabilize\n");
    851 
    852 	ure_write_2(sc, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB,
    853 	    ure_read_2(sc, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB) &
    854 	    ~URE_U2P3_ENABLE);
    855 
    856 	if (sc->ure_chip & URE_CHIP_VER_5C10) {
    857 		val = ure_read_2(sc, URE_USB_SSPHYLINK2, URE_MCU_TYPE_USB);
    858 		val &= ~URE_PWD_DN_SCALE_MASK;
    859 		val |= URE_PWD_DN_SCALE(96);
    860 		ure_write_2(sc, URE_USB_SSPHYLINK2, URE_MCU_TYPE_USB, val);
    861 
    862 		ure_write_1(sc, URE_USB_USB2PHY, URE_MCU_TYPE_USB,
    863 		    ure_read_1(sc, URE_USB_USB2PHY, URE_MCU_TYPE_USB) |
    864 		    URE_USB2PHY_L1 | URE_USB2PHY_SUSPEND);
    865 	} else if (sc->ure_chip & URE_CHIP_VER_5C20) {
    866 		ure_write_1(sc, URE_PLA_DMY_REG0, URE_MCU_TYPE_PLA,
    867 		    ure_read_1(sc, URE_PLA_DMY_REG0, URE_MCU_TYPE_PLA) &
    868 		    ~URE_ECM_ALDPS);
    869 	}
    870 	if (sc->ure_chip & (URE_CHIP_VER_5C20 | URE_CHIP_VER_5C30)) {
    871 		val = ure_read_1(sc, URE_USB_CSR_DUMMY1, URE_MCU_TYPE_USB);
    872 		if (ure_read_2(sc, URE_USB_BURST_SIZE, URE_MCU_TYPE_USB) ==
    873 		    0)
    874 			val &= ~URE_DYNAMIC_BURST;
    875 		else
    876 			val |= URE_DYNAMIC_BURST;
    877 		ure_write_1(sc, URE_USB_CSR_DUMMY1, URE_MCU_TYPE_USB, val);
    878 	}
    879 
    880 	ure_write_1(sc, URE_USB_CSR_DUMMY2, URE_MCU_TYPE_USB,
    881 	    ure_read_1(sc, URE_USB_CSR_DUMMY2, URE_MCU_TYPE_USB) |
    882 	    URE_EP4_FULL_FC);
    883 
    884 	ure_write_2(sc, URE_USB_WDT11_CTRL, URE_MCU_TYPE_USB,
    885 	    ure_read_2(sc, URE_USB_WDT11_CTRL, URE_MCU_TYPE_USB) &
    886 	    ~URE_TIMER11_EN);
    887 
    888 	ure_write_2(sc, URE_PLA_LED_FEATURE, URE_MCU_TYPE_PLA,
    889 	    ure_read_2(sc, URE_PLA_LED_FEATURE, URE_MCU_TYPE_PLA) &
    890 	    ~URE_LED_MODE_MASK);
    891 
    892 	if ((sc->ure_chip & URE_CHIP_VER_5C10) &&
    893 	    sc->ure_udev->ud_speed != USB_SPEED_SUPER)
    894 		val = URE_LPM_TIMER_500MS;
    895 	else
    896 		val = URE_LPM_TIMER_500US;
    897 	ure_write_1(sc, URE_USB_LPM_CTRL, URE_MCU_TYPE_USB,
    898 	    val | URE_FIFO_EMPTY_1FB | URE_ROK_EXIT_LPM);
    899 
    900 	val = ure_read_2(sc, URE_USB_AFE_CTRL2, URE_MCU_TYPE_USB);
    901 	val &= ~URE_SEN_VAL_MASK;
    902 	val |= URE_SEN_VAL_NORMAL | URE_SEL_RXIDLE;
    903 	ure_write_2(sc, URE_USB_AFE_CTRL2, URE_MCU_TYPE_USB, val);
    904 
    905 	ure_write_2(sc, URE_USB_CONNECT_TIMER, URE_MCU_TYPE_USB, 0x0001);
    906 
    907 	ure_write_2(sc, URE_USB_POWER_CUT, URE_MCU_TYPE_USB,
    908 	    ure_read_2(sc, URE_USB_POWER_CUT, URE_MCU_TYPE_USB) &
    909 	    ~(URE_PWR_EN | URE_PHASE2_EN));
    910 	ure_write_2(sc, URE_USB_MISC_0, URE_MCU_TYPE_USB,
    911 	    ure_read_2(sc, URE_USB_MISC_0, URE_MCU_TYPE_USB) &
    912 	    ~URE_PCUT_STATUS);
    913 
    914 	memset(u1u2, 0xff, sizeof(u1u2));
    915 	ure_write_mem(sc, URE_USB_TOLERANCE,
    916 	    URE_MCU_TYPE_USB | URE_BYTE_EN_SIX_BYTES, u1u2, sizeof(u1u2));
    917 
    918 	ure_write_2(sc, URE_PLA_MAC_PWR_CTRL, URE_MCU_TYPE_PLA,
    919 	    URE_ALDPS_SPDWN_RATIO);
    920 	ure_write_2(sc, URE_PLA_MAC_PWR_CTRL2, URE_MCU_TYPE_PLA,
    921 	    URE_EEE_SPDWN_RATIO);
    922 	ure_write_2(sc, URE_PLA_MAC_PWR_CTRL3, URE_MCU_TYPE_PLA,
    923 	    URE_PKT_AVAIL_SPDWN_EN | URE_SUSPEND_SPDWN_EN |
    924 	    URE_U1U2_SPDWN_EN | URE_L1_SPDWN_EN);
    925 	ure_write_2(sc, URE_PLA_MAC_PWR_CTRL4, URE_MCU_TYPE_PLA,
    926 	    URE_PWRSAVE_SPDWN_EN | URE_RXDV_SPDWN_EN | URE_TX10MIDLE_EN |
    927 	    URE_TP100_SPDWN_EN | URE_TP500_SPDWN_EN | URE_TP1000_SPDWN_EN |
    928 	    URE_EEE_SPDWN_EN);
    929 
    930 	val = ure_read_2(sc, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB);
    931 	if (!(sc->ure_chip & (URE_CHIP_VER_5C00 | URE_CHIP_VER_5C10)))
    932 		val |= URE_U2P3_ENABLE;
    933 	else
    934 		val &= ~URE_U2P3_ENABLE;
    935 	ure_write_2(sc, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB, val);
    936 
    937 	memset(u1u2, 0x00, sizeof(u1u2));
    938 	ure_write_mem(sc, URE_USB_TOLERANCE,
    939 	    URE_MCU_TYPE_USB | URE_BYTE_EN_SIX_BYTES, u1u2, sizeof(u1u2));
    940 
    941 	/* Disable ALDPS. */
    942 	ure_ocp_reg_write(sc, URE_OCP_POWER_CFG,
    943 	    ure_ocp_reg_read(sc, URE_OCP_POWER_CFG) & ~URE_EN_ALDPS);
    944 	usbd_delay_ms(sc->ure_udev, 20);
    945 
    946 	ure_init_fifo(sc);
    947 
    948 	/* Enable Rx aggregation. */
    949 	ure_write_2(sc, URE_USB_USB_CTRL, URE_MCU_TYPE_USB,
    950 	    ure_read_2(sc, URE_USB_USB_CTRL, URE_MCU_TYPE_USB) &
    951 	    ~URE_RX_AGG_DISABLE);
    952 
    953 	val = ure_read_2(sc, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB);
    954 	if (!(sc->ure_chip & (URE_CHIP_VER_5C00 | URE_CHIP_VER_5C10)))
    955 		val |= URE_U2P3_ENABLE;
    956 	else
    957 		val &= ~URE_U2P3_ENABLE;
    958 	ure_write_2(sc, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB, val);
    959 
    960 	memset(u1u2, 0xff, sizeof(u1u2));
    961 	ure_write_mem(sc, URE_USB_TOLERANCE,
    962 	    URE_MCU_TYPE_USB | URE_BYTE_EN_SIX_BYTES, u1u2, sizeof(u1u2));
    963 }
    964 
    965 static void
    966 ure_disable_teredo(struct ure_softc *sc)
    967 {
    968 
    969 	ure_write_4(sc, URE_PLA_TEREDO_CFG, URE_MCU_TYPE_PLA,
    970 	    ure_read_4(sc, URE_PLA_TEREDO_CFG, URE_MCU_TYPE_PLA) &
    971 	    ~(URE_TEREDO_SEL | URE_TEREDO_RS_EVENT_MASK | URE_OOB_TEREDO_EN));
    972 	ure_write_2(sc, URE_PLA_WDT6_CTRL, URE_MCU_TYPE_PLA,
    973 	    URE_WDT6_SET_MODE);
    974 	ure_write_2(sc, URE_PLA_REALWOW_TIMER, URE_MCU_TYPE_PLA, 0);
    975 	ure_write_4(sc, URE_PLA_TEREDO_TIMER, URE_MCU_TYPE_PLA, 0);
    976 }
    977 
    978 static void
    979 ure_init_fifo(struct ure_softc *sc)
    980 {
    981 	uint32_t rx_fifo1, rx_fifo2;
    982 	int i;
    983 
    984 	ure_write_2(sc, URE_PLA_MISC_1, URE_MCU_TYPE_PLA,
    985 	    ure_read_2(sc, URE_PLA_MISC_1, URE_MCU_TYPE_PLA) |
    986 	    URE_RXDY_GATED_EN);
    987 
    988 	ure_disable_teredo(sc);
    989 
    990 	ure_write_4(sc, URE_PLA_RCR, URE_MCU_TYPE_PLA,
    991 	    ure_read_4(sc, URE_PLA_RCR, URE_MCU_TYPE_PLA) &
    992 	    ~URE_RCR_ACPT_ALL);
    993 
    994 	if (!(sc->ure_flags & URE_FLAG_8152)) {
    995 		if (sc->ure_chip & (URE_CHIP_VER_5C00 | URE_CHIP_VER_5C10 |
    996 		    URE_CHIP_VER_5C20))
    997 			ure_ocp_reg_write(sc, URE_OCP_ADC_CFG,
    998 			    URE_CKADSEL_L | URE_ADC_EN | URE_EN_EMI_L);
    999 		if (sc->ure_chip & URE_CHIP_VER_5C00)
   1000 			ure_ocp_reg_write(sc, URE_OCP_EEE_CFG,
   1001 			    ure_ocp_reg_read(sc, URE_OCP_EEE_CFG) &
   1002 			    ~URE_CTAP_SHORT_EN);
   1003 		ure_ocp_reg_write(sc, URE_OCP_POWER_CFG,
   1004 		    ure_ocp_reg_read(sc, URE_OCP_POWER_CFG) |
   1005 		    URE_EEE_CLKDIV_EN);
   1006 		ure_ocp_reg_write(sc, URE_OCP_DOWN_SPEED,
   1007 		    ure_ocp_reg_read(sc, URE_OCP_DOWN_SPEED) |
   1008 		    URE_EN_10M_BGOFF);
   1009 		ure_ocp_reg_write(sc, URE_OCP_POWER_CFG,
   1010 		    ure_ocp_reg_read(sc, URE_OCP_POWER_CFG) |
   1011 		    URE_EN_10M_PLLOFF);
   1012 		ure_ocp_reg_write(sc, URE_OCP_SRAM_ADDR, URE_SRAM_IMPEDANCE);
   1013 		ure_ocp_reg_write(sc, URE_OCP_SRAM_DATA, 0x0b13);
   1014 		ure_write_2(sc, URE_PLA_PHY_PWR, URE_MCU_TYPE_PLA,
   1015 		    ure_read_2(sc, URE_PLA_PHY_PWR, URE_MCU_TYPE_PLA) |
   1016 		    URE_PFM_PWM_SWITCH);
   1017 
   1018 		/* Enable LPF corner auto tune. */
   1019 		ure_ocp_reg_write(sc, URE_OCP_SRAM_ADDR, URE_SRAM_LPF_CFG);
   1020 		ure_ocp_reg_write(sc, URE_OCP_SRAM_DATA, 0xf70f);
   1021 
   1022 		/* Adjust 10M amplitude. */
   1023 		ure_ocp_reg_write(sc, URE_OCP_SRAM_ADDR, URE_SRAM_10M_AMP1);
   1024 		ure_ocp_reg_write(sc, URE_OCP_SRAM_DATA, 0x00af);
   1025 		ure_ocp_reg_write(sc, URE_OCP_SRAM_ADDR, URE_SRAM_10M_AMP2);
   1026 		ure_ocp_reg_write(sc, URE_OCP_SRAM_DATA, 0x0208);
   1027 	}
   1028 
   1029 	ure_reset(sc);
   1030 
   1031 	ure_write_1(sc, URE_PLA_CR, URE_MCU_TYPE_PLA, 0);
   1032 
   1033 	ure_write_1(sc, URE_PLA_OOB_CTRL, URE_MCU_TYPE_PLA,
   1034 	    ure_read_1(sc, URE_PLA_OOB_CTRL, URE_MCU_TYPE_PLA) &
   1035 	    ~URE_NOW_IS_OOB);
   1036 
   1037 	ure_write_2(sc, URE_PLA_SFF_STS_7, URE_MCU_TYPE_PLA,
   1038 	    ure_read_2(sc, URE_PLA_SFF_STS_7, URE_MCU_TYPE_PLA) &
   1039 	    ~URE_MCU_BORW_EN);
   1040 	for (i = 0; i < URE_TIMEOUT; i++) {
   1041 		if (ure_read_1(sc, URE_PLA_OOB_CTRL, URE_MCU_TYPE_PLA) &
   1042 		    URE_LINK_LIST_READY)
   1043 			break;
   1044 		usbd_delay_ms(sc->ure_udev, 10);
   1045 	}
   1046 	if (i == URE_TIMEOUT)
   1047 		URE_PRINTF(sc, "timeout waiting for OOB control\n");
   1048 	ure_write_2(sc, URE_PLA_SFF_STS_7, URE_MCU_TYPE_PLA,
   1049 	    ure_read_2(sc, URE_PLA_SFF_STS_7, URE_MCU_TYPE_PLA) |
   1050 	    URE_RE_INIT_LL);
   1051 	for (i = 0; i < URE_TIMEOUT; i++) {
   1052 		if (ure_read_1(sc, URE_PLA_OOB_CTRL, URE_MCU_TYPE_PLA) &
   1053 		    URE_LINK_LIST_READY)
   1054 			break;
   1055 		usbd_delay_ms(sc->ure_udev, 10);
   1056 	}
   1057 	if (i == URE_TIMEOUT)
   1058 		URE_PRINTF(sc, "timeout waiting for OOB control\n");
   1059 
   1060 	ure_write_2(sc, URE_PLA_CPCR, URE_MCU_TYPE_PLA,
   1061 	    ure_read_2(sc, URE_PLA_CPCR, URE_MCU_TYPE_PLA) &
   1062 	    ~URE_CPCR_RX_VLAN);
   1063 	ure_write_2(sc, URE_PLA_TCR0, URE_MCU_TYPE_PLA,
   1064 	    ure_read_2(sc, URE_PLA_TCR0, URE_MCU_TYPE_PLA) |
   1065 	    URE_TCR0_AUTO_FIFO);
   1066 
   1067 	/* Configure Rx FIFO threshold and coalescing. */
   1068 	ure_write_4(sc, URE_PLA_RXFIFO_CTRL0, URE_MCU_TYPE_PLA,
   1069 	    URE_RXFIFO_THR1_NORMAL);
   1070 	if (sc->ure_udev->ud_speed == USB_SPEED_FULL) {
   1071 		rx_fifo1 = URE_RXFIFO_THR2_FULL;
   1072 		rx_fifo2 = URE_RXFIFO_THR3_FULL;
   1073 	} else {
   1074 		rx_fifo1 = URE_RXFIFO_THR2_HIGH;
   1075 		rx_fifo2 = URE_RXFIFO_THR3_HIGH;
   1076 	}
   1077 	ure_write_4(sc, URE_PLA_RXFIFO_CTRL1, URE_MCU_TYPE_PLA, rx_fifo1);
   1078 	ure_write_4(sc, URE_PLA_RXFIFO_CTRL2, URE_MCU_TYPE_PLA, rx_fifo2);
   1079 
   1080 	/* Configure Tx FIFO threshold. */
   1081 	ure_write_4(sc, URE_PLA_TXFIFO_CTRL, URE_MCU_TYPE_PLA,
   1082 	    URE_TXFIFO_THR_NORMAL);
   1083 }
   1084 
   1085 int
   1086 ure_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1087 {
   1088 	struct ure_softc *sc = ifp->if_softc;
   1089 	int error = 0, oflags = ifp->if_flags;
   1090 
   1091 	switch (cmd) {
   1092 	case SIOCSIFFLAGS:
   1093 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   1094 			break;
   1095 		switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
   1096 		case IFF_RUNNING:
   1097 			ure_stop(ifp, 1);
   1098 			break;
   1099 		case IFF_UP:
   1100 			ure_init(ifp);
   1101 			break;
   1102 		case IFF_UP | IFF_RUNNING:
   1103 			if ((ifp->if_flags ^ oflags) == IFF_PROMISC)
   1104 				ure_iff(sc);
   1105 			else
   1106 				ure_init(ifp);
   1107 		}
   1108 		break;
   1109 	default:
   1110 		if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET)
   1111 			break;
   1112 		error = 0;
   1113 		if ((ifp->if_flags & IFF_RUNNING) == 0)
   1114 			break;
   1115 		switch (cmd) {
   1116 		case SIOCADDMULTI:
   1117 		case SIOCDELMULTI:
   1118 			ure_iff(sc);
   1119 			break;
   1120 		default:
   1121 			break;
   1122 		}
   1123 	}
   1124 
   1125 	return error;
   1126 }
   1127 
   1128 static int
   1129 ure_match(device_t parent, cfdata_t match, void *aux)
   1130 {
   1131 	struct usb_attach_arg *uaa = aux;
   1132 
   1133 	return usb_lookup(ure_devs, uaa->uaa_vendor, uaa->uaa_product) != NULL ?
   1134 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
   1135 }
   1136 
   1137 static void
   1138 ure_attach(device_t parent, device_t self, void *aux)
   1139 {
   1140 	struct ure_softc *sc = device_private(self);
   1141 	struct usb_attach_arg *uaa = aux;
   1142 	struct usbd_device *dev = uaa->uaa_device;
   1143 	usb_interface_descriptor_t *id;
   1144 	usb_endpoint_descriptor_t *ed;
   1145 	struct ifnet *ifp;
   1146 	struct mii_data *mii;
   1147 	int error, i;
   1148 	uint16_t ver;
   1149 	uint8_t eaddr[8]; /* 2byte padded */
   1150 	char *devinfop;
   1151 
   1152 	aprint_naive("\n");
   1153 	aprint_normal("\n");
   1154 
   1155 	sc->ure_dev = self;
   1156 	sc->ure_udev = dev;
   1157 
   1158 	devinfop = usbd_devinfo_alloc(sc->ure_udev, 0);
   1159 	aprint_normal_dev(self, "%s\n", devinfop);
   1160 	usbd_devinfo_free(devinfop);
   1161 
   1162 	callout_init(&sc->ure_stat_ch, CALLOUT_MPSAFE);
   1163 	usb_init_task(&sc->ure_tick_task, ure_tick_task, sc, USB_TASKQ_MPSAFE);
   1164 	mutex_init(&sc->ure_mii_lock, MUTEX_DEFAULT, IPL_NONE);
   1165 	mutex_init(&sc->ure_txlock, MUTEX_DEFAULT, IPL_SOFTUSB);
   1166 	mutex_init(&sc->ure_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB);
   1167 	mutex_init(&sc->ure_lock, MUTEX_DEFAULT, IPL_NONE);
   1168 	cv_init(&sc->ure_detachcv, "uredet");
   1169 
   1170 	/*
   1171 	 * ure_phyno is set to 0 below when configuration has succeeded.
   1172 	 * if it is still -1 in detach, then ifmedia/mii/etc was not
   1173 	 * setup and should not be torn down.
   1174 	 */
   1175 	sc->ure_phyno = -1;
   1176 
   1177 #define URE_CONFIG_NO	1 /* XXX */
   1178 	error = usbd_set_config_no(dev, URE_CONFIG_NO, 1);
   1179 	if (error) {
   1180 		aprint_error_dev(self, "failed to set configuration: %s\n",
   1181 		    usbd_errstr(error));
   1182 		return; /* XXX */
   1183 	}
   1184 
   1185 	if (uaa->uaa_product == USB_PRODUCT_REALTEK_RTL8152)
   1186 		sc->ure_flags |= URE_FLAG_8152;
   1187 
   1188 #define URE_IFACE_IDX  0 /* XXX */
   1189 	error = usbd_device2interface_handle(dev, URE_IFACE_IDX, &sc->ure_iface);
   1190 	if (error) {
   1191 		aprint_error_dev(self, "failed to get interface handle: %s\n",
   1192 		    usbd_errstr(error));
   1193 		return; /* XXX */
   1194 	}
   1195 
   1196 	sc->ure_bufsz = 16 * 1024;
   1197 
   1198 	id = usbd_get_interface_descriptor(sc->ure_iface);
   1199 	for (i = 0; i < id->bNumEndpoints; i++) {
   1200 		ed = usbd_interface2endpoint_descriptor(sc->ure_iface, i);
   1201 		if (ed == NULL) {
   1202 			aprint_error_dev(self, "couldn't get ep %d\n", i);
   1203 			return; /* XXX */
   1204 		}
   1205 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
   1206 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
   1207 			sc->ure_ed[URE_ENDPT_RX] = ed->bEndpointAddress;
   1208 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
   1209 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
   1210 			sc->ure_ed[URE_ENDPT_TX] = ed->bEndpointAddress;
   1211 		}
   1212 	}
   1213 
   1214 	sc->ure_phyno = 0;
   1215 
   1216 	ver = ure_read_2(sc, URE_PLA_TCR1, URE_MCU_TYPE_PLA) & URE_VERSION_MASK;
   1217 	switch (ver) {
   1218 	case 0x4c00:
   1219 		sc->ure_chip |= URE_CHIP_VER_4C00;
   1220 		break;
   1221 	case 0x4c10:
   1222 		sc->ure_chip |= URE_CHIP_VER_4C10;
   1223 		break;
   1224 	case 0x5c00:
   1225 		sc->ure_chip |= URE_CHIP_VER_5C00;
   1226 		break;
   1227 	case 0x5c10:
   1228 		sc->ure_chip |= URE_CHIP_VER_5C10;
   1229 		break;
   1230 	case 0x5c20:
   1231 		sc->ure_chip |= URE_CHIP_VER_5C20;
   1232 		break;
   1233 	case 0x5c30:
   1234 		sc->ure_chip |= URE_CHIP_VER_5C30;
   1235 		break;
   1236 	default:
   1237 		/* fake addr?  or just fail? */
   1238 		break;
   1239 	}
   1240 	aprint_normal_dev(self, "RTL%d %sver %04x\n",
   1241 	    (sc->ure_flags & URE_FLAG_8152) ? 8152 : 8153,
   1242 	    (sc->ure_chip != 0) ? "" : "unknown ",
   1243 	    ver);
   1244 
   1245 	mutex_enter(&sc->ure_lock);
   1246 	if (sc->ure_flags & URE_FLAG_8152)
   1247 		ure_rtl8152_init(sc);
   1248 	else
   1249 		ure_rtl8153_init(sc);
   1250 
   1251 	if (sc->ure_chip & URE_CHIP_VER_4C00)
   1252 		ure_read_mem(sc, URE_PLA_IDR, URE_MCU_TYPE_PLA, eaddr,
   1253 		    sizeof(eaddr));
   1254 	else
   1255 		ure_read_mem(sc, URE_PLA_BACKUP, URE_MCU_TYPE_PLA, eaddr,
   1256 		    sizeof(eaddr));
   1257 	mutex_exit(&sc->ure_lock);
   1258 
   1259 	aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
   1260 
   1261 	ifp = GET_IFP(sc);
   1262 	ifp->if_softc = sc;
   1263 	strlcpy(ifp->if_xname, device_xname(sc->ure_dev), IFNAMSIZ);
   1264 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   1265 	ifp->if_extflags = IFEF_MPSAFE;
   1266 	ifp->if_init = ure_init;
   1267 	ifp->if_ioctl = ure_ioctl;
   1268 	ifp->if_start = ure_start;
   1269 	ifp->if_stop = ure_stop;
   1270 
   1271 	/*
   1272 	 * We don't support TSOv4 and v6 for now, that are required to
   1273 	 * be handled in software for some cases.
   1274 	 */
   1275 	ifp->if_capabilities = IFCAP_CSUM_IPv4_Tx |
   1276 	    IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_UDPv4_Tx;
   1277 #ifdef INET6
   1278 	ifp->if_capabilities |= IFCAP_CSUM_TCPv6_Tx | IFCAP_CSUM_UDPv6_Tx;
   1279 #endif
   1280 	if (sc->ure_chip & ~URE_CHIP_VER_4C00) {
   1281 		ifp->if_capabilities |= IFCAP_CSUM_IPv4_Rx |
   1282 		    IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx |
   1283 		    IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_UDPv6_Rx;
   1284 	}
   1285 	sc->ure_ec.ec_capabilities = ETHERCAP_VLAN_MTU;
   1286 #ifdef notyet
   1287 	sc->ure_ec.ec_capabilities |= ETHERCAP_JUMBO_MTU;
   1288 #endif
   1289 
   1290 	IFQ_SET_READY(&ifp->if_snd);
   1291 
   1292 	mii = GET_MII(sc);
   1293 	mii->mii_ifp = ifp;
   1294 	mii->mii_readreg = ure_miibus_readreg;
   1295 	mii->mii_writereg = ure_miibus_writereg;
   1296 	mii->mii_statchg = ure_miibus_statchg;
   1297 	mii->mii_flags = MIIF_AUTOTSLEEP;
   1298 
   1299 	sc->ure_ec.ec_mii = mii;
   1300 	ifmedia_init(&mii->mii_media, 0, ure_ifmedia_upd, ure_ifmedia_sts);
   1301 	mii_attach(self, mii, 0xffffffff, sc->ure_phyno, MII_OFFSET_ANY, 0);
   1302 
   1303 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
   1304 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
   1305 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
   1306 	} else
   1307 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
   1308 
   1309 	if_attach(ifp);
   1310 	ether_ifattach(ifp, eaddr);
   1311 
   1312 	rnd_attach_source(&sc->ure_rnd_source, device_xname(sc->ure_dev),
   1313 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
   1314 
   1315 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->ure_udev, sc->ure_dev);
   1316 
   1317 	if (!pmf_device_register(self, NULL, NULL))
   1318 		aprint_error_dev(self, "couldn't establish power handler\n");
   1319 }
   1320 
   1321 static int
   1322 ure_detach(device_t self, int flags)
   1323 {
   1324 	struct ure_softc *sc = device_private(self);
   1325 	struct ifnet *ifp = GET_IFP(sc);
   1326 
   1327 	pmf_device_deregister(self);
   1328 
   1329 	mutex_enter(&sc->ure_lock);
   1330 	sc->ure_dying = true;
   1331 	mutex_exit(&sc->ure_lock);
   1332 
   1333 	callout_halt(&sc->ure_stat_ch, NULL);
   1334 
   1335 	usb_rem_task_wait(sc->ure_udev, &sc->ure_tick_task, USB_TASKQ_DRIVER,
   1336 	    NULL);
   1337 
   1338 	if (sc->ure_ep[URE_ENDPT_TX] != NULL)
   1339 		usbd_abort_pipe(sc->ure_ep[URE_ENDPT_TX]);
   1340 	if (sc->ure_ep[URE_ENDPT_RX] != NULL)
   1341 		usbd_abort_pipe(sc->ure_ep[URE_ENDPT_RX]);
   1342 
   1343 	mutex_enter(&sc->ure_lock);
   1344 	sc->ure_refcnt--;
   1345 	while (sc->ure_refcnt > 0) {
   1346 		/* Wait for processes to go away */
   1347 		cv_wait(&sc->ure_detachcv, &sc->ure_lock);
   1348 	}
   1349 	mutex_exit(&sc->ure_lock);
   1350 
   1351 	/* partial-attach, below items weren't configured. */
   1352 	if (sc->ure_phyno != -1) {
   1353 		if (ifp->if_flags & IFF_RUNNING)
   1354 			ure_stop(ifp, 1);
   1355 
   1356 		rnd_detach_source(&sc->ure_rnd_source);
   1357 		mii_detach(&sc->ure_mii, MII_PHY_ANY, MII_OFFSET_ANY);
   1358 		ifmedia_delete_instance(&sc->ure_mii.mii_media, IFM_INST_ANY);
   1359 		if (ifp->if_softc != NULL) {
   1360 			ether_ifdetach(ifp);
   1361 			if_detach(ifp);
   1362 		}
   1363 	}
   1364 
   1365 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->ure_udev, sc->ure_dev);
   1366 
   1367 	callout_destroy(&sc->ure_stat_ch);
   1368 	cv_destroy(&sc->ure_detachcv);
   1369 	mutex_destroy(&sc->ure_lock);
   1370 	mutex_destroy(&sc->ure_rxlock);
   1371 	mutex_destroy(&sc->ure_txlock);
   1372 	mutex_destroy(&sc->ure_mii_lock);
   1373 
   1374 	return 0;
   1375 }
   1376 
   1377 static int
   1378 ure_activate(device_t self, enum devact act)
   1379 {
   1380 	struct ure_softc *sc = device_private(self);
   1381 	struct ifnet *ifp = GET_IFP(sc);
   1382 
   1383 	switch (act) {
   1384 	case DVACT_DEACTIVATE:
   1385 		if_deactivate(ifp);
   1386 
   1387 		mutex_enter(&sc->ure_lock);
   1388 		sc->ure_dying = true;
   1389 		mutex_exit(&sc->ure_lock);
   1390 
   1391 		mutex_enter(&sc->ure_rxlock);
   1392 		mutex_enter(&sc->ure_txlock);
   1393 		sc->ure_stopping = true;
   1394 		mutex_exit(&sc->ure_txlock);
   1395 		mutex_exit(&sc->ure_rxlock);
   1396 
   1397 		return 0;
   1398 	default:
   1399 		return EOPNOTSUPP;
   1400 	}
   1401 	return 0;
   1402 }
   1403 
   1404 static void
   1405 ure_tick_task(void *xsc)
   1406 {
   1407 	struct ure_softc *sc = xsc;
   1408 	struct ifnet *ifp;
   1409 	struct mii_data *mii;
   1410 
   1411 	if (sc == NULL)
   1412 		return;
   1413 
   1414 	mutex_enter(&sc->ure_lock);
   1415 	if (sc->ure_stopping || sc->ure_dying) {
   1416 		mutex_exit(&sc->ure_lock);
   1417 		return;
   1418 	}
   1419 
   1420 	ifp = GET_IFP(sc);
   1421 	mii = GET_MII(sc);
   1422 	if (mii == NULL) {
   1423 		mutex_exit(&sc->ure_lock);
   1424 		return;
   1425 	}
   1426 
   1427 	sc->ure_refcnt++;
   1428 	mutex_exit(&sc->ure_lock);
   1429 
   1430 	mii_tick(mii);
   1431 
   1432 	if ((sc->ure_flags & URE_FLAG_LINK) == 0)
   1433 		ure_miibus_statchg(ifp);
   1434 
   1435 	mutex_enter(&sc->ure_lock);
   1436 	if (--sc->ure_refcnt < 0)
   1437 		cv_broadcast(&sc->ure_detachcv);
   1438 	if (!sc->ure_stopping && !sc->ure_dying)
   1439 		callout_schedule(&sc->ure_stat_ch, hz);
   1440 	mutex_exit(&sc->ure_lock);
   1441 }
   1442 
   1443 static void
   1444 ure_lock_mii(struct ure_softc *sc)
   1445 {
   1446 
   1447 	mutex_enter(&sc->ure_lock);
   1448 	sc->ure_refcnt++;
   1449 	mutex_exit(&sc->ure_lock);
   1450 
   1451 	mutex_enter(&sc->ure_mii_lock);
   1452 }
   1453 
   1454 static void
   1455 ure_unlock_mii(struct ure_softc *sc)
   1456 {
   1457 
   1458 	mutex_exit(&sc->ure_mii_lock);
   1459 	mutex_enter(&sc->ure_lock);
   1460 	if (--sc->ure_refcnt < 0)
   1461 		cv_broadcast(&sc->ure_detachcv);
   1462 	mutex_exit(&sc->ure_lock);
   1463 }
   1464 
   1465 static void
   1466 ure_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
   1467 {
   1468 	struct ure_chain *c = (struct ure_chain *)priv;
   1469 	struct ure_softc *sc = c->uc_sc;
   1470 	struct ifnet *ifp = GET_IFP(sc);
   1471 	uint8_t *buf = c->uc_buf;
   1472 	uint32_t total_len;
   1473 	uint16_t pktlen = 0;
   1474 	struct mbuf *m;
   1475 	struct ure_rxpkt rxhdr;
   1476 
   1477 	mutex_enter(&sc->ure_rxlock);
   1478 
   1479 	if (sc->ure_dying || sc->ure_stopping ||
   1480 	    status == USBD_INVAL || status == USBD_NOT_STARTED ||
   1481 	    status == USBD_CANCELLED || !(ifp->if_flags & IFF_RUNNING)) {
   1482 		mutex_exit(&sc->ure_rxlock);
   1483 		return;
   1484 	}
   1485 
   1486 	if (status != USBD_NORMAL_COMPLETION) {
   1487 		if (usbd_ratecheck(&sc->ure_rx_notice))
   1488 			URE_PRINTF(sc, "usb errors on rx: %s\n",
   1489 			    usbd_errstr(status));
   1490 		if (status == USBD_STALLED)
   1491 			usbd_clear_endpoint_stall_async(
   1492 			    sc->ure_ep[URE_ENDPT_RX]);
   1493 		goto done;
   1494 	}
   1495 
   1496 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
   1497 	DPRINTFN(3, ("received %d bytes\n", total_len));
   1498 
   1499 	KASSERTMSG(total_len <= sc->ure_bufsz, "%u vs %u",
   1500 	    total_len, sc->ure_bufsz);
   1501 
   1502 	do {
   1503 		if (total_len < sizeof(rxhdr)) {
   1504 			DPRINTF(("too few bytes left for a packet header\n"));
   1505 			ifp->if_ierrors++;
   1506 			goto done;
   1507 		}
   1508 
   1509 		buf += roundup(pktlen, 8);
   1510 
   1511 		memcpy(&rxhdr, buf, sizeof(rxhdr));
   1512 		total_len -= sizeof(rxhdr);
   1513 
   1514 		pktlen = le32toh(rxhdr.ure_pktlen) & URE_RXPKT_LEN_MASK;
   1515 		DPRINTFN(4, ("next packet is %d bytes\n", pktlen));
   1516 		if (pktlen > total_len) {
   1517 			DPRINTF(("not enough bytes left for next packet\n"));
   1518 			ifp->if_ierrors++;
   1519 			goto done;
   1520 		}
   1521 
   1522 		total_len -= roundup(pktlen, 8);
   1523 		buf += sizeof(rxhdr);
   1524 
   1525 		m = m_devget(buf, pktlen - ETHER_CRC_LEN, 0, ifp);
   1526 		if (m == NULL) {
   1527 			DPRINTF(("unable to allocate mbuf for next packet\n"));
   1528 			ifp->if_ierrors++;
   1529 			goto done;
   1530 		}
   1531 
   1532 		m->m_pkthdr.csum_flags = ure_rxcsum(ifp, &rxhdr);
   1533 
   1534 		mutex_exit(&sc->ure_rxlock);
   1535 		if_percpuq_enqueue(ifp->if_percpuq, m);
   1536 		mutex_enter(&sc->ure_rxlock);
   1537 
   1538 		if (sc->ure_stopping) {
   1539 			mutex_exit(&sc->ure_rxlock);
   1540 			return;
   1541 		}
   1542 
   1543 	} while (total_len > 0);
   1544 
   1545 done:
   1546 	mutex_exit(&sc->ure_rxlock);
   1547 
   1548 	/* Setup new transfer. */
   1549 	usbd_setup_xfer(xfer, c, c->uc_buf, sc->ure_bufsz,
   1550 	    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ure_rxeof);
   1551 	usbd_transfer(xfer);
   1552 }
   1553 
   1554 static int
   1555 ure_rxcsum(struct ifnet *ifp, struct ure_rxpkt *rp)
   1556 {
   1557 	int enabled = ifp->if_csum_flags_rx, flags = 0;
   1558 	uint32_t csum, misc;
   1559 
   1560 	if (enabled == 0)
   1561 		return 0;
   1562 
   1563 	csum = le32toh(rp->ure_csum);
   1564 	misc = le32toh(rp->ure_misc);
   1565 
   1566 	if (csum & URE_RXPKT_IPV4_CS) {
   1567 		flags |= M_CSUM_IPv4;
   1568 		if (csum & URE_RXPKT_TCP_CS)
   1569 			flags |= M_CSUM_TCPv4;
   1570 		if (csum & URE_RXPKT_UDP_CS)
   1571 			flags |= M_CSUM_UDPv4;
   1572 	} else if (csum & URE_RXPKT_IPV6_CS) {
   1573 		flags = 0;
   1574 		if (csum & URE_RXPKT_TCP_CS)
   1575 			flags |= M_CSUM_TCPv6;
   1576 		if (csum & URE_RXPKT_UDP_CS)
   1577 			flags |= M_CSUM_UDPv6;
   1578 	}
   1579 
   1580 	flags &= enabled;
   1581 	if (__predict_false((flags & M_CSUM_IPv4) &&
   1582 	    (misc & URE_RXPKT_IP_F)))
   1583 		flags |= M_CSUM_IPv4_BAD;
   1584 	if (__predict_false(
   1585 	   ((flags & (M_CSUM_TCPv4 | M_CSUM_TCPv6)) && (misc & URE_RXPKT_TCP_F))
   1586 	|| ((flags & (M_CSUM_UDPv4 | M_CSUM_UDPv6)) && (misc & URE_RXPKT_UDP_F))
   1587 	))
   1588 		flags |= M_CSUM_TCP_UDP_BAD;
   1589 
   1590 	return flags;
   1591 }
   1592 
   1593 static void
   1594 ure_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
   1595 {
   1596 	struct ure_chain *c = priv;
   1597 	struct ure_softc *sc = c->uc_sc;
   1598 	struct ure_cdata *cd = &sc->ure_cdata;
   1599 	struct ifnet *ifp = GET_IFP(sc);
   1600 
   1601 	mutex_enter(&sc->ure_txlock);
   1602 	if (sc->ure_stopping || sc->ure_dying) {
   1603 		mutex_exit(&sc->ure_txlock);
   1604 		return;
   1605 	}
   1606 
   1607 	DPRINTFN(2, ("tx completion\n"));
   1608 
   1609 	KASSERT(cd->tx_cnt > 0);
   1610 	cd->tx_cnt--;
   1611 
   1612 	ifp->if_flags &= ~IFF_OACTIVE;
   1613 
   1614 	switch (status) {
   1615 	case USBD_NOT_STARTED:
   1616 	case USBD_CANCELLED:
   1617 		break;
   1618 
   1619 	case USBD_NORMAL_COMPLETION:
   1620 		ifp->if_opackets++;
   1621 
   1622 		if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
   1623 			ure_start_locked(ifp);
   1624 		}
   1625 		break;
   1626 
   1627 	default:
   1628 		ifp->if_oerrors++;
   1629 		if (usbd_ratecheck(&sc->ure_tx_notice))
   1630 			URE_PRINTF(sc, "usb error on tx: %s\n",
   1631 			    usbd_errstr(status));
   1632 		if (status == USBD_STALLED)
   1633 			usbd_clear_endpoint_stall_async(
   1634 			    sc->ure_ep[URE_ENDPT_TX]);
   1635 		break;
   1636 	}
   1637 
   1638 	mutex_exit(&sc->ure_txlock);
   1639 }
   1640 
   1641 static int
   1642 ure_tx_list_init(struct ure_softc *sc)
   1643 {
   1644 	struct ure_cdata *cd;
   1645 	struct ure_chain *c;
   1646 	int i, error;
   1647 
   1648 	cd = &sc->ure_cdata;
   1649 	for (i = 0; i < URE_TX_LIST_CNT; i++) {
   1650 		c = &cd->tx_chain[i];
   1651 		c->uc_sc = sc;
   1652 		if (c->uc_xfer == NULL) {
   1653 			error = usbd_create_xfer(sc->ure_ep[URE_ENDPT_TX],
   1654 			    sc->ure_bufsz, USBD_FORCE_SHORT_XFER, 0,
   1655 			    &c->uc_xfer);
   1656 			if (error)
   1657 				return error;
   1658 			c->uc_buf = usbd_get_buffer(c->uc_xfer);
   1659 		}
   1660 	}
   1661 
   1662 	cd->tx_prod = cd->tx_cnt = 0;
   1663 
   1664 	return 0;
   1665 }
   1666 
   1667 static int
   1668 ure_rx_list_init(struct ure_softc *sc)
   1669 {
   1670 	struct ure_cdata *cd;
   1671 	struct ure_chain *c;
   1672 	int i, error;
   1673 
   1674 	cd = &sc->ure_cdata;
   1675 	for (i = 0; i < URE_RX_LIST_CNT; i++) {
   1676 		c = &cd->rx_chain[i];
   1677 		c->uc_sc = sc;
   1678 		error = usbd_create_xfer(sc->ure_ep[URE_ENDPT_RX],
   1679 		    sc->ure_bufsz, 0, 0, &c->uc_xfer);
   1680 		if (error)
   1681 			return error;
   1682 		c->uc_buf = usbd_get_buffer(c->uc_xfer);
   1683 	}
   1684 
   1685 	return 0;
   1686 }
   1687 
   1688 static int
   1689 ure_encap(struct ure_softc *sc, struct mbuf *m, int idx)
   1690 {
   1691 	struct ifnet *ifp = GET_IFP(sc);
   1692 	struct ure_chain *c;
   1693 	usbd_status err;
   1694 	struct ure_txpkt txhdr;
   1695 	uint32_t frm_len = 0;
   1696 	uint8_t *buf;
   1697 
   1698 	KASSERT(mutex_owned(&sc->ure_txlock));
   1699 
   1700 	c = &sc->ure_cdata.tx_chain[idx];
   1701 	buf = c->uc_buf;
   1702 
   1703 	/* header */
   1704 	txhdr.ure_pktlen = htole32(m->m_pkthdr.len | URE_TXPKT_TX_FS |
   1705 	    URE_TXPKT_TX_LS);
   1706 	txhdr.ure_csum = htole32(ure_txcsum(m));
   1707 	memcpy(buf, &txhdr, sizeof(txhdr));
   1708 	buf += sizeof(txhdr);
   1709 	frm_len = sizeof(txhdr);
   1710 
   1711 	/* packet */
   1712 	m_copydata(m, 0, m->m_pkthdr.len, buf);
   1713 	frm_len += m->m_pkthdr.len;
   1714 
   1715 	if (__predict_false(c->uc_xfer == NULL))
   1716 		return EIO;	/* XXX plugged out or down */
   1717 
   1718 	DPRINTFN(2, ("tx %d bytes\n", frm_len));
   1719 	usbd_setup_xfer(c->uc_xfer, c, c->uc_buf, frm_len,
   1720 	    USBD_FORCE_SHORT_XFER, 10000, ure_txeof);
   1721 
   1722 	err = usbd_transfer(c->uc_xfer);
   1723 	if (err != USBD_IN_PROGRESS) {
   1724 		ure_stop(ifp, 0);
   1725 		return EIO;
   1726 	}
   1727 
   1728 	return 0;
   1729 }
   1730 
   1731 /*
   1732  * We need to calculate L4 checksum in software, if the offset of
   1733  * L4 header is larger than 0x7ff = 2047.
   1734  */
   1735 static uint32_t
   1736 ure_txcsum(struct mbuf *m)
   1737 {
   1738 	struct ether_header *eh;
   1739 	int flags = m->m_pkthdr.csum_flags;
   1740 	uint32_t data = m->m_pkthdr.csum_data;
   1741 	uint32_t reg = 0;
   1742 	int l3off, l4off;
   1743 	uint16_t type;
   1744 
   1745 	if (flags == 0)
   1746 		return 0;
   1747 
   1748 	if (__predict_true(m->m_len >= (int)sizeof(*eh))) {
   1749 		eh = mtod(m, struct ether_header *);
   1750 		type = eh->ether_type;
   1751 	} else
   1752 		m_copydata(m, offsetof(struct ether_header, ether_type),
   1753 		    sizeof(type), &type);
   1754 	switch (type = htons(type)) {
   1755 	case ETHERTYPE_IP:
   1756 	case ETHERTYPE_IPV6:
   1757 		l3off = ETHER_HDR_LEN;
   1758 		break;
   1759 	case ETHERTYPE_VLAN:
   1760 		l3off = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
   1761 		break;
   1762 	default:
   1763 		return 0;
   1764 	}
   1765 
   1766 	if (flags & (M_CSUM_TCPv4 | M_CSUM_UDPv4)) {
   1767 		l4off = l3off + M_CSUM_DATA_IPv4_IPHL(data);
   1768 		if (__predict_false(l4off > URE_L4_OFFSET_MAX)) {
   1769 			in_undefer_cksum(m, l3off, flags);
   1770 			return 0;
   1771 		}
   1772 		reg |= URE_TXPKT_IPV4_CS;
   1773 		if (flags & M_CSUM_TCPv4)
   1774 			reg |= URE_TXPKT_TCP_CS;
   1775 		else
   1776 			reg |= URE_TXPKT_UDP_CS;
   1777 		reg |= l4off << URE_L4_OFFSET_SHIFT;
   1778 	}
   1779 #ifdef INET6
   1780 	else if (flags & (M_CSUM_TCPv6 | M_CSUM_UDPv6)) {
   1781 		l4off = l3off + M_CSUM_DATA_IPv6_IPHL(data);
   1782 		if (__predict_false(l4off > URE_L4_OFFSET_MAX)) {
   1783 			in6_undefer_cksum(m, l3off, flags);
   1784 			return 0;
   1785 		}
   1786 		reg |= URE_TXPKT_IPV6_CS;
   1787 		if (flags & M_CSUM_TCPv6)
   1788 			reg |= URE_TXPKT_TCP_CS;
   1789 		else
   1790 			reg |= URE_TXPKT_UDP_CS;
   1791 		reg |= l4off << URE_L4_OFFSET_SHIFT;
   1792 	}
   1793 #endif
   1794 	else if (flags & M_CSUM_IPv4)
   1795 		reg |= URE_TXPKT_IPV4_CS;
   1796 
   1797 	return reg;
   1798 }
   1799