Home | History | Annotate | Line # | Download | only in usb
      1 /*	$NetBSD: xhci.c,v 1.192 2025/10/10 20:21:05 nat Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2013 Jonathan A. Kollasch
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     23  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     25  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     26  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * USB rev 2.0 and rev 3.1 specification
     31  *  http://www.usb.org/developers/docs/
     32  * xHCI rev 1.1 specification
     33  *  http://www.intel.com/technology/usb/spec.htm
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: xhci.c,v 1.192 2025/10/10 20:21:05 nat Exp $");
     38 
     39 #ifdef _KERNEL_OPT
     40 #include "opt_usb.h"
     41 #endif
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/kmem.h>
     47 #include <sys/device.h>
     48 #include <sys/select.h>
     49 #include <sys/proc.h>
     50 #include <sys/queue.h>
     51 #include <sys/mutex.h>
     52 #include <sys/condvar.h>
     53 #include <sys/bus.h>
     54 #include <sys/cpu.h>
     55 #include <sys/sysctl.h>
     56 
     57 #include <machine/endian.h>
     58 
     59 #include <dev/usb/usb.h>
     60 #include <dev/usb/usbdi.h>
     61 #include <dev/usb/usbdivar.h>
     62 #include <dev/usb/usbdi_util.h>
     63 #include <dev/usb/usbhist.h>
     64 #include <dev/usb/usb_mem.h>
     65 #include <dev/usb/usb_quirks.h>
     66 
     67 #include <dev/usb/xhcireg.h>
     68 #include <dev/usb/xhcivar.h>
     69 #include <dev/usb/usbroothub.h>
     70 
     71 
     72 #ifdef USB_DEBUG
     73 #ifndef XHCI_DEBUG
     74 #define xhcidebug 0
     75 #else /* !XHCI_DEBUG */
     76 #define HEXDUMP(a, b, c) \
     77     do { \
     78 	    if (xhcidebug > 0) \
     79 		    hexdump(printf, a, b, c); \
     80     } while (/*CONSTCOND*/0)
     81 static int xhcidebug = 0;
     82 
     83 SYSCTL_SETUP(sysctl_hw_xhci_setup, "sysctl hw.xhci setup")
     84 {
     85 	int err;
     86 	const struct sysctlnode *rnode;
     87 	const struct sysctlnode *cnode;
     88 
     89 	err = sysctl_createv(clog, 0, NULL, &rnode,
     90 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "xhci",
     91 	    SYSCTL_DESCR("xhci global controls"),
     92 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
     93 
     94 	if (err)
     95 		goto fail;
     96 
     97 	/* control debugging printfs */
     98 	err = sysctl_createv(clog, 0, &rnode, &cnode,
     99 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    100 	    "debug", SYSCTL_DESCR("Enable debugging output"),
    101 	    NULL, 0, &xhcidebug, sizeof(xhcidebug), CTL_CREATE, CTL_EOL);
    102 	if (err)
    103 		goto fail;
    104 
    105 	return;
    106 fail:
    107 	aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
    108 }
    109 
    110 #endif /* !XHCI_DEBUG */
    111 #endif /* USB_DEBUG */
    112 
    113 #ifndef HEXDUMP
    114 #define HEXDUMP(a, b, c)
    115 #endif
    116 
    117 #define DPRINTF(FMT,A,B,C,D)	USBHIST_LOG(xhcidebug,FMT,A,B,C,D)
    118 #define DPRINTFN(N,FMT,A,B,C,D)	USBHIST_LOGN(xhcidebug,N,FMT,A,B,C,D)
    119 #define XHCIHIST_FUNC()		USBHIST_FUNC()
    120 #define XHCIHIST_CALLED(name)	USBHIST_CALLED(xhcidebug)
    121 #define XHCIHIST_CALLARGS(FMT,A,B,C,D) \
    122 				USBHIST_CALLARGS(xhcidebug,FMT,A,B,C,D)
    123 
    124 #define XHCI_DCI_SLOT 0
    125 #define XHCI_DCI_EP_CONTROL 1
    126 
    127 #define XHCI_ICI_INPUT_CONTROL 0
    128 
    129 struct xhci_pipe {
    130 	struct usbd_pipe xp_pipe;
    131 	struct usb_task xp_async_task;
    132 	int16_t xp_isoc_next; /* next micro frame */
    133 	uint8_t xp_maxb; /* max burst */
    134 	uint8_t xp_mult;
    135 	uint8_t xp_ival;
    136 };
    137 
    138 #define XHCI_COMMAND_RING_TRBS 256
    139 #define XHCI_EVENT_RING_TRBS 256
    140 #define XHCI_EVENT_RING_SEGMENTS 1
    141 #define XHCI_TRB_3_ED_BIT XHCI_TRB_3_ISP_BIT
    142 
    143 static usbd_status xhci_open(struct usbd_pipe *);
    144 static void xhci_close_pipe(struct usbd_pipe *);
    145 static int xhci_intr1(struct xhci_softc * const);
    146 static void xhci_softintr(void *);
    147 static void xhci_poll(struct usbd_bus *);
    148 static struct usbd_xfer *xhci_allocx(struct usbd_bus *, unsigned int);
    149 static void xhci_freex(struct usbd_bus *, struct usbd_xfer *);
    150 static void xhci_abortx(struct usbd_xfer *);
    151 static bool xhci_dying(struct usbd_bus *);
    152 static void xhci_get_lock(struct usbd_bus *, kmutex_t **);
    153 static usbd_status xhci_new_device(device_t, struct usbd_bus *, int, int, int,
    154     struct usbd_port *);
    155 static int xhci_roothub_ctrl(struct usbd_bus *, usb_device_request_t *,
    156     void *, int);
    157 
    158 static void xhci_pipe_restart(struct usbd_pipe *);
    159 static void xhci_pipe_restart_async_task(void *);
    160 static void xhci_pipe_restart_async(struct usbd_pipe *);
    161 
    162 static usbd_status xhci_configure_endpoint(struct usbd_pipe *);
    163 //static usbd_status xhci_unconfigure_endpoint(struct usbd_pipe *);
    164 static void xhci_reset_endpoint(struct usbd_pipe *);
    165 static usbd_status xhci_stop_endpoint_cmd(struct xhci_softc *,
    166     struct xhci_slot *, u_int, uint32_t);
    167 static usbd_status xhci_stop_endpoint(struct usbd_pipe *);
    168 
    169 static void xhci_host_dequeue(struct xhci_ring * const);
    170 static void xhci_set_dequeue(struct usbd_pipe *);
    171 
    172 static usbd_status xhci_do_command(struct xhci_softc * const,
    173     struct xhci_soft_trb * const, int);
    174 static usbd_status xhci_do_command_locked(struct xhci_softc * const,
    175     struct xhci_soft_trb * const, int);
    176 static usbd_status xhci_init_slot(struct usbd_device *, uint32_t);
    177 static void xhci_free_slot(struct xhci_softc *, struct xhci_slot *);
    178 static usbd_status xhci_set_address(struct usbd_device *, uint32_t, bool);
    179 static usbd_status xhci_enable_slot(struct xhci_softc * const,
    180     uint8_t * const);
    181 static usbd_status xhci_disable_slot(struct xhci_softc * const, uint8_t);
    182 static usbd_status xhci_address_device(struct xhci_softc * const,
    183     uint64_t, uint8_t, bool);
    184 static void xhci_set_dcba(struct xhci_softc * const, uint64_t, int);
    185 static usbd_status xhci_update_ep0_mps(struct xhci_softc * const,
    186     struct xhci_slot * const, u_int);
    187 static usbd_status xhci_ring_init(struct xhci_softc * const,
    188     struct xhci_ring **, size_t, size_t);
    189 static void xhci_ring_free(struct xhci_softc * const,
    190     struct xhci_ring ** const);
    191 
    192 static void xhci_setup_ctx(struct usbd_pipe *);
    193 static void xhci_setup_route(struct usbd_pipe *, uint32_t *);
    194 static void xhci_setup_tthub(struct usbd_pipe *, uint32_t *);
    195 static void xhci_setup_maxburst(struct usbd_pipe *, uint32_t *);
    196 static uint32_t xhci_bival2ival(uint32_t, uint32_t, uint32_t);
    197 
    198 static void xhci_noop(struct usbd_pipe *);
    199 
    200 static usbd_status xhci_root_intr_transfer(struct usbd_xfer *);
    201 static usbd_status xhci_root_intr_start(struct usbd_xfer *);
    202 static void xhci_root_intr_abort(struct usbd_xfer *);
    203 static void xhci_root_intr_close(struct usbd_pipe *);
    204 static void xhci_root_intr_done(struct usbd_xfer *);
    205 
    206 static usbd_status xhci_device_ctrl_transfer(struct usbd_xfer *);
    207 static usbd_status xhci_device_ctrl_start(struct usbd_xfer *);
    208 static void xhci_device_ctrl_abort(struct usbd_xfer *);
    209 static void xhci_device_ctrl_close(struct usbd_pipe *);
    210 static void xhci_device_ctrl_done(struct usbd_xfer *);
    211 
    212 static usbd_status xhci_device_isoc_transfer(struct usbd_xfer *);
    213 static usbd_status xhci_device_isoc_enter(struct usbd_xfer *);
    214 static void xhci_device_isoc_abort(struct usbd_xfer *);
    215 static void xhci_device_isoc_close(struct usbd_pipe *);
    216 static void xhci_device_isoc_done(struct usbd_xfer *);
    217 
    218 static usbd_status xhci_device_intr_transfer(struct usbd_xfer *);
    219 static usbd_status xhci_device_intr_start(struct usbd_xfer *);
    220 static void xhci_device_intr_abort(struct usbd_xfer *);
    221 static void xhci_device_intr_close(struct usbd_pipe *);
    222 static void xhci_device_intr_done(struct usbd_xfer *);
    223 
    224 static usbd_status xhci_device_bulk_transfer(struct usbd_xfer *);
    225 static usbd_status xhci_device_bulk_start(struct usbd_xfer *);
    226 static void xhci_device_bulk_abort(struct usbd_xfer *);
    227 static void xhci_device_bulk_close(struct usbd_pipe *);
    228 static void xhci_device_bulk_done(struct usbd_xfer *);
    229 
    230 static const struct usbd_bus_methods xhci_bus_methods = {
    231 	.ubm_open = xhci_open,
    232 	.ubm_softint = xhci_softintr,
    233 	.ubm_dopoll = xhci_poll,
    234 	.ubm_allocx = xhci_allocx,
    235 	.ubm_freex = xhci_freex,
    236 	.ubm_abortx = xhci_abortx,
    237 	.ubm_dying = xhci_dying,
    238 	.ubm_getlock = xhci_get_lock,
    239 	.ubm_newdev = xhci_new_device,
    240 	.ubm_rhctrl = xhci_roothub_ctrl,
    241 };
    242 
    243 static const struct usbd_pipe_methods xhci_root_intr_methods = {
    244 	.upm_transfer = xhci_root_intr_transfer,
    245 	.upm_start = xhci_root_intr_start,
    246 	.upm_abort = xhci_root_intr_abort,
    247 	.upm_close = xhci_root_intr_close,
    248 	.upm_cleartoggle = xhci_noop,
    249 	.upm_done = xhci_root_intr_done,
    250 };
    251 
    252 
    253 static const struct usbd_pipe_methods xhci_device_ctrl_methods = {
    254 	.upm_transfer = xhci_device_ctrl_transfer,
    255 	.upm_start = xhci_device_ctrl_start,
    256 	.upm_abort = xhci_device_ctrl_abort,
    257 	.upm_close = xhci_device_ctrl_close,
    258 	.upm_cleartoggle = xhci_noop,
    259 	.upm_done = xhci_device_ctrl_done,
    260 };
    261 
    262 static const struct usbd_pipe_methods xhci_device_isoc_methods = {
    263 	.upm_transfer = xhci_device_isoc_transfer,
    264 	.upm_abort = xhci_device_isoc_abort,
    265 	.upm_close = xhci_device_isoc_close,
    266 	.upm_cleartoggle = xhci_noop,
    267 	.upm_done = xhci_device_isoc_done,
    268 };
    269 
    270 static const struct usbd_pipe_methods xhci_device_bulk_methods = {
    271 	.upm_transfer = xhci_device_bulk_transfer,
    272 	.upm_start = xhci_device_bulk_start,
    273 	.upm_abort = xhci_device_bulk_abort,
    274 	.upm_close = xhci_device_bulk_close,
    275 	.upm_cleartoggle = xhci_noop,
    276 	.upm_done = xhci_device_bulk_done,
    277 };
    278 
    279 static const struct usbd_pipe_methods xhci_device_intr_methods = {
    280 	.upm_transfer = xhci_device_intr_transfer,
    281 	.upm_start = xhci_device_intr_start,
    282 	.upm_abort = xhci_device_intr_abort,
    283 	.upm_close = xhci_device_intr_close,
    284 	.upm_cleartoggle = xhci_noop,
    285 	.upm_done = xhci_device_intr_done,
    286 };
    287 
    288 static inline uint32_t
    289 xhci_read_1(const struct xhci_softc * const sc, bus_size_t offset)
    290 {
    291 	if (ISSET(sc->sc_quirks, XHCI_32BIT_ACCESS)) {
    292 		uint32_t val;
    293 		val = bus_space_read_4(sc->sc_iot, sc->sc_ioh, offset & ~3);
    294 		return (val >> ((offset & 3) * NBBY)) & 0xff;
    295 	} else {
    296 		return bus_space_read_1(sc->sc_iot, sc->sc_ioh, offset);
    297 	}
    298 }
    299 
    300 static inline uint32_t
    301 xhci_read_2(const struct xhci_softc * const sc, bus_size_t offset)
    302 {
    303 	if (ISSET(sc->sc_quirks, XHCI_32BIT_ACCESS)) {
    304 		uint32_t val;
    305 		val = bus_space_read_4(sc->sc_iot, sc->sc_ioh, offset & ~3);
    306 		return (val >> ((offset & 3) * NBBY)) & 0xffff;
    307 	} else {
    308 		return bus_space_read_2(sc->sc_iot, sc->sc_ioh, offset);
    309 	}
    310 }
    311 
    312 static inline uint32_t
    313 xhci_read_4(const struct xhci_softc * const sc, bus_size_t offset)
    314 {
    315 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh, offset);
    316 }
    317 
    318 static inline void
    319 xhci_write_1(const struct xhci_softc * const sc, bus_size_t offset,
    320     uint32_t value)
    321 {
    322 	if (ISSET(sc->sc_quirks, XHCI_32BIT_ACCESS)) {
    323 		const uint32_t mask = 0xffU << ((offset & 3) * NBBY);
    324 		uint32_t val;
    325 		val = bus_space_read_4(sc->sc_iot, sc->sc_ioh, offset & ~3);
    326 		val &= ~mask;
    327 		val |= __SHIFTIN(value, mask);
    328 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, offset & ~3, val);
    329 	} else {
    330 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, offset, value);
    331 	}
    332 }
    333 
    334 #if 0 /* unused */
    335 static inline void
    336 xhci_write_4(const struct xhci_softc * const sc, bus_size_t offset,
    337     uint32_t value)
    338 {
    339 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, offset, value);
    340 }
    341 #endif /* unused */
    342 
    343 static inline uint32_t
    344 xhci_cap_read_4(const struct xhci_softc * const sc, bus_size_t offset)
    345 {
    346 	return bus_space_read_4(sc->sc_iot, sc->sc_cbh, offset);
    347 }
    348 
    349 static inline uint32_t
    350 xhci_op_read_4(const struct xhci_softc * const sc, bus_size_t offset)
    351 {
    352 	return bus_space_read_4(sc->sc_iot, sc->sc_obh, offset);
    353 }
    354 
    355 static inline void
    356 xhci_op_write_4(const struct xhci_softc * const sc, bus_size_t offset,
    357     uint32_t value)
    358 {
    359 	bus_space_write_4(sc->sc_iot, sc->sc_obh, offset, value);
    360 }
    361 
    362 static inline uint64_t
    363 xhci_op_read_8(const struct xhci_softc * const sc, bus_size_t offset)
    364 {
    365 	uint64_t value;
    366 
    367 #ifdef XHCI_USE_BUS_SPACE_8
    368 	value = bus_space_read_8(sc->sc_iot, sc->sc_obh, offset);
    369 #else
    370 	value = bus_space_read_4(sc->sc_iot, sc->sc_obh, offset);
    371 	value |= (uint64_t)bus_space_read_4(sc->sc_iot, sc->sc_obh,
    372 	    offset + 4) << 32;
    373 #endif
    374 
    375 	return value;
    376 }
    377 
    378 static inline void
    379 xhci_op_write_8(const struct xhci_softc * const sc, bus_size_t offset,
    380     uint64_t value)
    381 {
    382 #ifdef XHCI_USE_BUS_SPACE_8
    383 	bus_space_write_8(sc->sc_iot, sc->sc_obh, offset, value);
    384 #else
    385 	bus_space_write_4(sc->sc_iot, sc->sc_obh, offset + 0,
    386 	    (value >> 0) & 0xffffffff);
    387 	bus_space_write_4(sc->sc_iot, sc->sc_obh, offset + 4,
    388 	    (value >> 32) & 0xffffffff);
    389 #endif
    390 }
    391 
    392 static inline uint32_t
    393 xhci_rt_read_4(const struct xhci_softc * const sc, bus_size_t offset)
    394 {
    395 	return bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset);
    396 }
    397 
    398 static inline void
    399 xhci_rt_write_4(const struct xhci_softc * const sc, bus_size_t offset,
    400     uint32_t value)
    401 {
    402 	bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset, value);
    403 }
    404 
    405 static inline uint64_t
    406 xhci_rt_read_8(const struct xhci_softc * const sc, bus_size_t offset)
    407 {
    408 	uint64_t value;
    409 
    410 #ifdef XHCI_USE_BUS_SPACE_8
    411 	value = bus_space_read_8(sc->sc_iot, sc->sc_rbh, offset);
    412 #else
    413 	value = bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset);
    414 	value |= (uint64_t)bus_space_read_4(sc->sc_iot, sc->sc_rbh,
    415 	    offset + 4) << 32;
    416 #endif
    417 
    418 	return value;
    419 }
    420 
    421 static inline void
    422 xhci_rt_write_8(const struct xhci_softc * const sc, bus_size_t offset,
    423     uint64_t value)
    424 {
    425 #ifdef XHCI_USE_BUS_SPACE_8
    426 	bus_space_write_8(sc->sc_iot, sc->sc_rbh, offset, value);
    427 #else
    428 	bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset + 0,
    429 	    (value >> 0) & 0xffffffff);
    430 	bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset + 4,
    431 	    (value >> 32) & 0xffffffff);
    432 #endif
    433 }
    434 
    435 #if 0 /* unused */
    436 static inline uint32_t
    437 xhci_db_read_4(const struct xhci_softc * const sc, bus_size_t offset)
    438 {
    439 	return bus_space_read_4(sc->sc_iot, sc->sc_dbh, offset);
    440 }
    441 #endif /* unused */
    442 
    443 static inline void
    444 xhci_db_write_4(const struct xhci_softc * const sc, bus_size_t offset,
    445     uint32_t value)
    446 {
    447 	bus_space_write_4(sc->sc_iot, sc->sc_dbh, offset, value);
    448 }
    449 
    450 /* --- */
    451 
    452 static inline uint8_t
    453 xhci_ep_get_type(usb_endpoint_descriptor_t * const ed)
    454 {
    455 	u_int eptype = 0;
    456 
    457 	switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
    458 	case UE_CONTROL:
    459 		eptype = 0x0;
    460 		break;
    461 	case UE_ISOCHRONOUS:
    462 		eptype = 0x1;
    463 		break;
    464 	case UE_BULK:
    465 		eptype = 0x2;
    466 		break;
    467 	case UE_INTERRUPT:
    468 		eptype = 0x3;
    469 		break;
    470 	}
    471 
    472 	if ((UE_GET_XFERTYPE(ed->bmAttributes) == UE_CONTROL) ||
    473 	    (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN))
    474 		return eptype | 0x4;
    475 	else
    476 		return eptype;
    477 }
    478 
    479 static u_int
    480 xhci_ep_get_dci(usb_endpoint_descriptor_t * const ed)
    481 {
    482 	/* xHCI 1.0 section 4.5.1 */
    483 	u_int epaddr = UE_GET_ADDR(ed->bEndpointAddress);
    484 	u_int in = 0;
    485 
    486 	if ((UE_GET_XFERTYPE(ed->bmAttributes) == UE_CONTROL) ||
    487 	    (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN))
    488 		in = 1;
    489 
    490 	return epaddr * 2 + in;
    491 }
    492 
    493 static inline u_int
    494 xhci_dci_to_ici(const u_int i)
    495 {
    496 	return i + 1;
    497 }
    498 
    499 static inline void *
    500 xhci_slot_get_dcv(struct xhci_softc * const sc, struct xhci_slot * const xs,
    501     const u_int dci)
    502 {
    503 	return KERNADDR(&xs->xs_dc_dma, sc->sc_ctxsz * dci);
    504 }
    505 
    506 #if 0 /* unused */
    507 static inline bus_addr_t
    508 xhci_slot_get_dcp(struct xhci_softc * const sc, struct xhci_slot * const xs,
    509     const u_int dci)
    510 {
    511 	return DMAADDR(&xs->xs_dc_dma, sc->sc_ctxsz * dci);
    512 }
    513 #endif /* unused */
    514 
    515 static inline void *
    516 xhci_slot_get_icv(struct xhci_softc * const sc, struct xhci_slot * const xs,
    517     const u_int ici)
    518 {
    519 	return KERNADDR(&xs->xs_ic_dma, sc->sc_ctxsz * ici);
    520 }
    521 
    522 static inline bus_addr_t
    523 xhci_slot_get_icp(struct xhci_softc * const sc, struct xhci_slot * const xs,
    524     const u_int ici)
    525 {
    526 	return DMAADDR(&xs->xs_ic_dma, sc->sc_ctxsz * ici);
    527 }
    528 
    529 static inline struct xhci_trb *
    530 xhci_ring_trbv(struct xhci_ring * const xr, u_int idx)
    531 {
    532 	return KERNADDR(&xr->xr_dma, XHCI_TRB_SIZE * idx);
    533 }
    534 
    535 static inline bus_addr_t
    536 xhci_ring_trbp(struct xhci_ring * const xr, u_int idx)
    537 {
    538 	return DMAADDR(&xr->xr_dma, XHCI_TRB_SIZE * idx);
    539 }
    540 
    541 static inline void
    542 xhci_xfer_put_trb(struct xhci_xfer * const xx, u_int idx,
    543     uint64_t parameter, uint32_t status, uint32_t control)
    544 {
    545 	KASSERTMSG(idx < xx->xx_ntrb, "idx=%u xx_ntrb=%u", idx, xx->xx_ntrb);
    546 	xx->xx_trb[idx].trb_0 = parameter;
    547 	xx->xx_trb[idx].trb_2 = status;
    548 	xx->xx_trb[idx].trb_3 = control;
    549 }
    550 
    551 static inline void
    552 xhci_trb_put(struct xhci_trb * const trb, uint64_t parameter, uint32_t status,
    553     uint32_t control)
    554 {
    555 	trb->trb_0 = htole64(parameter);
    556 	trb->trb_2 = htole32(status);
    557 	trb->trb_3 = htole32(control);
    558 }
    559 
    560 static int
    561 xhci_trb_get_idx(struct xhci_ring *xr, uint64_t trb_0, int *idx)
    562 {
    563 	/* base address of TRBs */
    564 	bus_addr_t trbp = xhci_ring_trbp(xr, 0);
    565 
    566 	/* trb_0 range sanity check */
    567 	if (trb_0 == 0 || trb_0 < trbp ||
    568 	    (trb_0 - trbp) % sizeof(struct xhci_trb) != 0 ||
    569 	    (trb_0 - trbp) / sizeof(struct xhci_trb) >= xr->xr_ntrb) {
    570 		return 1;
    571 	}
    572 	*idx = (trb_0 - trbp) / sizeof(struct xhci_trb);
    573 	return 0;
    574 }
    575 
    576 static unsigned int
    577 xhci_get_epstate(struct xhci_softc * const sc, struct xhci_slot * const xs,
    578     u_int dci)
    579 {
    580 	uint32_t *cp;
    581 
    582 	usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
    583 	cp = xhci_slot_get_dcv(sc, xs, dci);
    584 	return XHCI_EPCTX_0_EPSTATE_GET(le32toh(cp[0]));
    585 }
    586 
    587 static inline unsigned int
    588 xhci_ctlrport2bus(struct xhci_softc * const sc, unsigned int ctlrport)
    589 {
    590 	const unsigned int port = ctlrport - 1;
    591 	const uint8_t bit = __BIT(port % NBBY);
    592 
    593 	return __SHIFTOUT(sc->sc_ctlrportbus[port / NBBY], bit);
    594 }
    595 
    596 /*
    597  * Return the roothub port for a controller port.  Both are 1..n.
    598  */
    599 static inline unsigned int
    600 xhci_ctlrport2rhport(struct xhci_softc * const sc, unsigned int ctrlport)
    601 {
    602 
    603 	return sc->sc_ctlrportmap[ctrlport - 1];
    604 }
    605 
    606 /*
    607  * Return the controller port for a bus roothub port.  Both are 1..n.
    608  */
    609 static inline unsigned int
    610 xhci_rhport2ctlrport(struct xhci_softc * const sc, unsigned int bn,
    611     unsigned int rhport)
    612 {
    613 
    614 	return sc->sc_rhportmap[bn][rhport - 1];
    615 }
    616 
    617 /* --- */
    618 
    619 void
    620 xhci_childdet(device_t self, device_t child)
    621 {
    622 	struct xhci_softc * const sc = device_private(self);
    623 
    624 	mutex_enter(&sc->sc_intr_lock);
    625 	KASSERT((sc->sc_child == child) || (sc->sc_child2 == child));
    626 	if (child == sc->sc_child2)
    627 		sc->sc_child2 = NULL;
    628 	else if (child == sc->sc_child)
    629 		sc->sc_child = NULL;
    630 	mutex_exit(&sc->sc_intr_lock);
    631 }
    632 
    633 int
    634 xhci_detach(struct xhci_softc *sc, int flags)
    635 {
    636 	int rv = 0;
    637 
    638 	if (sc->sc_child2 != NULL) {
    639 		rv = config_detach(sc->sc_child2, flags);
    640 		if (rv != 0)
    641 			return rv;
    642 		KASSERT(sc->sc_child2 == NULL);
    643 	}
    644 
    645 	if (sc->sc_child != NULL) {
    646 		rv = config_detach(sc->sc_child, flags);
    647 		if (rv != 0)
    648 			return rv;
    649 		KASSERT(sc->sc_child == NULL);
    650 	}
    651 
    652 	/* XXX unconfigure/free slots */
    653 
    654 	/* verify: */
    655 	xhci_rt_write_4(sc, XHCI_IMAN(0), 0);
    656 	xhci_op_write_4(sc, XHCI_USBCMD, 0);
    657 	/* do we need to wait for stop? */
    658 
    659 	xhci_op_write_8(sc, XHCI_CRCR, 0);
    660 	xhci_ring_free(sc, &sc->sc_cr);
    661 	cv_destroy(&sc->sc_command_cv);
    662 	cv_destroy(&sc->sc_cmdbusy_cv);
    663 
    664 	xhci_rt_write_4(sc, XHCI_ERSTSZ(0), 0);
    665 	xhci_rt_write_8(sc, XHCI_ERSTBA(0), 0);
    666 	xhci_rt_write_8(sc, XHCI_ERDP(0), 0 | XHCI_ERDP_BUSY);
    667 	xhci_ring_free(sc, &sc->sc_er);
    668 
    669 	usb_freemem(&sc->sc_eventst_dma);
    670 
    671 	xhci_op_write_8(sc, XHCI_DCBAAP, 0);
    672 	usb_freemem(&sc->sc_dcbaa_dma);
    673 
    674 	kmem_free(sc->sc_slots, sizeof(*sc->sc_slots) * sc->sc_maxslots);
    675 
    676 	kmem_free(sc->sc_ctlrportbus,
    677 	    howmany(sc->sc_maxports * sizeof(uint8_t), NBBY));
    678 	kmem_free(sc->sc_ctlrportmap, sc->sc_maxports * sizeof(int));
    679 
    680 	for (size_t j = 0; j < __arraycount(sc->sc_rhportmap); j++) {
    681 		kmem_free(sc->sc_rhportmap[j], sc->sc_maxports * sizeof(int));
    682 	}
    683 
    684 	mutex_destroy(&sc->sc_rhlock);
    685 	mutex_destroy(&sc->sc_lock);
    686 	mutex_destroy(&sc->sc_intr_lock);
    687 
    688 	pool_cache_destroy(sc->sc_xferpool);
    689 
    690 	return rv;
    691 }
    692 
    693 int
    694 xhci_activate(device_t self, enum devact act)
    695 {
    696 	struct xhci_softc * const sc = device_private(self);
    697 
    698 	switch (act) {
    699 	case DVACT_DEACTIVATE:
    700 		sc->sc_dying = true;
    701 		return 0;
    702 	default:
    703 		return EOPNOTSUPP;
    704 	}
    705 }
    706 
    707 bool
    708 xhci_suspend(device_t self, const pmf_qual_t *qual)
    709 {
    710 	struct xhci_softc * const sc = device_private(self);
    711 	size_t i, j, bn, dci;
    712 	int port;
    713 	uint32_t v;
    714 	usbd_status err;
    715 	bool ok = false;
    716 
    717 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
    718 
    719 	/*
    720 	 * Block issuance of new commands, and wait for all pending
    721 	 * commands to complete.
    722 	 */
    723 	mutex_enter(&sc->sc_lock);
    724 	KASSERT(sc->sc_suspender == NULL);
    725 	sc->sc_suspender = curlwp;
    726 	while (sc->sc_command_addr != 0)
    727 		cv_wait(&sc->sc_cmdbusy_cv, &sc->sc_lock);
    728 	mutex_exit(&sc->sc_lock);
    729 
    730 	/*
    731 	 * Block roothub xfers which might touch portsc registers until
    732 	 * we're done suspending.
    733 	 */
    734 	mutex_enter(&sc->sc_rhlock);
    735 
    736 	/*
    737 	 * xHCI Requirements Specification 1.2, May 2019, Sec. 4.23.2:
    738 	 * xHCI Power Management, p. 342
    739 	 * https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/extensible-host-controler-interface-usb-xhci.pdf#page=342
    740 	 */
    741 
    742 	/*
    743 	 * `1. Stop all USB activity by issuing Stop Endpoint Commands
    744 	 *     for Busy endpoints in the Running state.  If the Force
    745 	 *     Save Context Capability (FSC = ``0'') is not supported,
    746 	 *     then Stop Endpoint Commands shall be issued for all idle
    747 	 *     endpoints in the Running state as well.  The Stop
    748 	 *     Endpoint Command causes the xHC to update the respective
    749 	 *     Endpoint or Stream Contexts in system memory, e.g. the
    750 	 *     TR Dequeue Pointer, DCS, etc. fields.  Refer to
    751 	 *     Implementation Note "0".'
    752 	 */
    753 	for (i = 0; i < sc->sc_maxslots; i++) {
    754 		struct xhci_slot *xs = &sc->sc_slots[i];
    755 
    756 		/* Skip if the slot is not in use.  */
    757 		if (xs->xs_idx == 0)
    758 			continue;
    759 
    760 		for (dci = XHCI_DCI_SLOT; dci <= XHCI_MAX_DCI; dci++) {
    761 			/* Skip if the endpoint is not Running.  */
    762 			/* XXX What about Busy?  */
    763 			if (xhci_get_epstate(sc, xs, dci) !=
    764 			    XHCI_EPSTATE_RUNNING)
    765 				continue;
    766 
    767 			/* Stop endpoint.  */
    768 			mutex_enter(&sc->sc_lock);
    769 			err = xhci_stop_endpoint_cmd(sc, xs, dci,
    770 			    XHCI_TRB_3_SUSP_EP_BIT);
    771 			mutex_exit(&sc->sc_lock);
    772 			if (err) {
    773 				device_printf(self, "failed to stop endpoint"
    774 				    " slot %zu dci %zu err %d\n",
    775 				    i, dci, err);
    776 				goto out;
    777 			}
    778 		}
    779 	}
    780 
    781 	/*
    782 	 * Next, suspend all the ports:
    783 	 *
    784 	 * xHCI Requirements Specification 1.2, May 2019, Sec. 4.15:
    785 	 * Suspend-Resume, pp. 276-283
    786 	 * https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/extensible-host-controler-interface-usb-xhci.pdf#page=276
    787 	 */
    788 	for (bn = 0; bn < 2; bn++) {
    789 		for (i = 1; i <= sc->sc_rhportcount[bn]; i++) {
    790 			/* 4.15.1: Port Suspend.  */
    791 			port = XHCI_PORTSC(xhci_rhport2ctlrport(sc, bn, i));
    792 
    793 			/*
    794 			 * `System software places individual ports
    795 			 *  into suspend mode by writing a ``3'' into
    796 			 *  the appropriate PORTSC register Port Link
    797 			 *  State (PLS) field (refer to Section 5.4.8).
    798 			 *  Software should only set the PLS field to
    799 			 *  ``3'' when the port is in the Enabled
    800 			 *  state.'
    801 			 *
    802 			 * `Software should not attempt to suspend a
    803 			 *  port unless the port reports that it is in
    804 			 *  the enabled (PED = ``1''; PLS < ``3'')
    805 			 *  state (refer to Section 5.4.8 for more
    806 			 *  information about PED and PLS).'
    807 			 */
    808 			v = xhci_op_read_4(sc, port);
    809 			if (((v & XHCI_PS_PED) == 0) ||
    810 			    XHCI_PS_PLS_GET(v) >= XHCI_PS_PLS_U3)
    811 				continue;
    812 			v &= ~(XHCI_PS_PLS_MASK | XHCI_PS_CLEAR);
    813 			v |= XHCI_PS_LWS | XHCI_PS_PLS_SET(XHCI_PS_PLS_SETU3);
    814 			xhci_op_write_4(sc, port, v);
    815 
    816 			/*
    817 			 * `When the PLS field is written with U3
    818 			 *  (``3''), the status of the PLS bit will not
    819 			 *  change to the target U state U3 until the
    820 			 *  suspend signaling has completed to the
    821 			 *  attached device (which may be as long as
    822 			 *  10ms.).'
    823 			 *
    824 			 * `Software is required to wait for U3
    825 			 *  transitions to complete before it puts the
    826 			 *  xHC into a low power state, and before
    827 			 *  resuming the port.'
    828 			 *
    829 			 * XXX Take advantage of the technique to
    830 			 * reduce polling on host controllers that
    831 			 * support the U3C capability.
    832 			 */
    833 			for (j = 0; j < XHCI_WAIT_PLS_U3; j++) {
    834 				v = xhci_op_read_4(sc, port);
    835 				if (XHCI_PS_PLS_GET(v) == XHCI_PS_PLS_U3)
    836 					break;
    837 				usb_delay_ms(&sc->sc_bus, 1);
    838 			}
    839 			if (j == XHCI_WAIT_PLS_U3) {
    840 				device_printf(self,
    841 				    "suspend timeout on bus %zu port %zu\n",
    842 				    bn, i);
    843 				goto out;
    844 			}
    845 		}
    846 	}
    847 
    848 	/*
    849 	 * `2. Ensure that the Command Ring is in the Stopped state
    850 	 *     (CRR = ``0'') or Idle (i.e. the Command Transfer Ring is
    851 	 *     empty), and all Command Completion Events associated
    852 	 *     with them have been received.'
    853 	 *
    854 	 * XXX
    855 	 */
    856 
    857 	/* `3. Stop the controller by setting Run/Stop (R/S) = ``0''.'  */
    858 	xhci_op_write_4(sc, XHCI_USBCMD,
    859 	    xhci_op_read_4(sc, XHCI_USBCMD) & ~XHCI_CMD_RS);
    860 
    861 	/*
    862 	 * `4. Read the Operational Runtime, and VTIO registers in the
    863 	 *     following order: USBCMD, DNCTRL, DCBAAP, CONFIG, ERSTSZ,
    864 	 *     ERSTBA, ERDP, IMAN, IMOD, and VTIO and save their
    865 	 *     state.'
    866 	 *
    867 	 * (We don't use VTIO here (XXX for now?).)
    868 	 */
    869 	sc->sc_regs.usbcmd = xhci_op_read_4(sc, XHCI_USBCMD);
    870 	sc->sc_regs.dnctrl = xhci_op_read_4(sc, XHCI_DNCTRL);
    871 	sc->sc_regs.dcbaap = xhci_op_read_8(sc, XHCI_DCBAAP);
    872 	sc->sc_regs.config = xhci_op_read_4(sc, XHCI_CONFIG);
    873 	sc->sc_regs.erstsz0 = xhci_rt_read_4(sc, XHCI_ERSTSZ(0));
    874 	sc->sc_regs.erstba0 = xhci_rt_read_8(sc, XHCI_ERSTBA(0));
    875 	sc->sc_regs.erdp0 = xhci_rt_read_8(sc, XHCI_ERDP(0));
    876 	sc->sc_regs.iman0 = xhci_rt_read_4(sc, XHCI_IMAN(0));
    877 	sc->sc_regs.imod0 = xhci_rt_read_4(sc, XHCI_IMOD(0));
    878 
    879 	/*
    880 	 * `5. Set the Controller Save State (CSS) flag in the USBCMD
    881 	 *     register (5.4.1)...'
    882 	 */
    883 	xhci_op_write_4(sc, XHCI_USBCMD,
    884 	    xhci_op_read_4(sc, XHCI_USBCMD) | XHCI_CMD_CSS);
    885 
    886 	/*
    887 	 *    `...and wait for the Save State Status (SSS) flag in the
    888 	 *     USBSTS register (5.4.2) to transition to ``0''.'
    889 	 */
    890 	for (i = 0; i < XHCI_WAIT_SSS; i++) {
    891 		if ((xhci_op_read_4(sc, XHCI_USBSTS) & XHCI_STS_SSS) == 0)
    892 			break;
    893 		usb_delay_ms(&sc->sc_bus, 1);
    894 	}
    895 	if (i >= XHCI_WAIT_SSS) {
    896 		device_printf(self, "suspend timeout, USBSTS.SSS\n");
    897 		/*
    898 		 * Just optimistically go on and check SRE anyway --
    899 		 * what's the worst that could happen?
    900 		 */
    901 	}
    902 
    903 	/*
    904 	 * `Note: After a Save or Restore operation completes, the
    905 	 *  Save/Restore Error (SRE) flag in the USBSTS register should
    906 	 *  be checked to ensure that the operation completed
    907 	 *  successfully.'
    908 	 */
    909 	if (xhci_op_read_4(sc, XHCI_USBSTS) & XHCI_STS_SRE) {
    910 		device_printf(self, "suspend error, USBSTS.SRE\n");
    911 		goto out;
    912 	}
    913 
    914 	/* Success!  */
    915 	ok = true;
    916 
    917 out:	mutex_exit(&sc->sc_rhlock);
    918 	if (!ok) {
    919 		/*
    920 		 * If suspend failed, stop holding up command issuance
    921 		 * and make it fail instead.
    922 		 */
    923 		mutex_enter(&sc->sc_lock);
    924 		KASSERT(sc->sc_suspender == curlwp);
    925 		sc->sc_suspender = NULL;
    926 		sc->sc_suspendresume_failed = true;
    927 		cv_broadcast(&sc->sc_cmdbusy_cv);
    928 		mutex_exit(&sc->sc_lock);
    929 	}
    930 	return ok;
    931 }
    932 
    933 bool
    934 xhci_resume(device_t self, const pmf_qual_t *qual)
    935 {
    936 	struct xhci_softc * const sc = device_private(self);
    937 	size_t i, j, bn, dci;
    938 	int port;
    939 	uint32_t v;
    940 	bool ok = false;
    941 
    942 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
    943 
    944 	/*
    945 	 * If resume had previously failed, just try again.  Can't make
    946 	 * things worse, probably.
    947 	 */
    948 	mutex_enter(&sc->sc_lock);
    949 	if (sc->sc_suspendresume_failed) {
    950 		KASSERT(sc->sc_suspender == NULL);
    951 		sc->sc_suspender = curlwp;
    952 		sc->sc_suspendresume_failed = false;
    953 	}
    954 	KASSERT(sc->sc_suspender);
    955 	mutex_exit(&sc->sc_lock);
    956 
    957 	/*
    958 	 * Block roothub xfers which might touch portsc registers until
    959 	 * we're done resuming.
    960 	 */
    961 	mutex_enter(&sc->sc_rhlock);
    962 
    963 	/*
    964 	 * xHCI Requirements Specification 1.2, May 2019, Sec. 4.23.2:
    965 	 * xHCI Power Management, p. 343
    966 	 * https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/extensible-host-controler-interface-usb-xhci.pdf#page=343
    967 	 */
    968 
    969 	/*
    970 	 * `4. Restore the Operational Runtime, and VTIO registers with
    971 	 *     their previously saved state in the following order:
    972 	 *     DNCTRL, DCBAAP, CONFIG, ERSTSZ, ERSTBA, ERDP, IMAN,
    973 	 *     IMOD, and VTIO.'
    974 	 *
    975 	 * (We don't use VTIO here (for now?).)
    976 	 */
    977 	xhci_op_write_4(sc, XHCI_USBCMD, sc->sc_regs.usbcmd);
    978 	xhci_op_write_4(sc, XHCI_DNCTRL, sc->sc_regs.dnctrl);
    979 	xhci_op_write_8(sc, XHCI_DCBAAP, sc->sc_regs.dcbaap);
    980 	xhci_op_write_4(sc, XHCI_CONFIG, sc->sc_regs.config);
    981 	xhci_rt_write_4(sc, XHCI_ERSTSZ(0), sc->sc_regs.erstsz0);
    982 	xhci_rt_write_8(sc, XHCI_ERSTBA(0), sc->sc_regs.erstba0);
    983 	xhci_rt_write_8(sc, XHCI_ERDP(0), sc->sc_regs.erdp0);
    984 	xhci_rt_write_4(sc, XHCI_IMAN(0), sc->sc_regs.iman0);
    985 	xhci_rt_write_4(sc, XHCI_IMOD(0), sc->sc_regs.imod0);
    986 
    987 	memset(&sc->sc_regs, 0, sizeof(sc->sc_regs)); /* paranoia */
    988 
    989 	/*
    990 	 * `5. Set the Controller Restore State (CRS) flag in the
    991 	 *     USBCMD register (5.4.1) to ``1''...'
    992 	 */
    993 	xhci_op_write_4(sc, XHCI_USBCMD,
    994 	    xhci_op_read_4(sc, XHCI_USBCMD) | XHCI_CMD_CRS);
    995 
    996 	/*
    997 	 *    `...and wait for the Restore State Status (RSS) in the
    998 	 *     USBSTS register (5.4.2) to transition to ``0''.'
    999 	 */
   1000 	for (i = 0; i < XHCI_WAIT_RSS; i++) {
   1001 		if ((xhci_op_read_4(sc, XHCI_USBSTS) & XHCI_STS_RSS) == 0)
   1002 			break;
   1003 		usb_delay_ms(&sc->sc_bus, 1);
   1004 	}
   1005 	if (i >= XHCI_WAIT_RSS) {
   1006 		device_printf(self, "resume timeout, USBSTS.RSS\n");
   1007 		goto out;
   1008 	}
   1009 
   1010 	/*
   1011 	 * `6. Reinitialize the Command Ring, i.e. so its Cycle bits
   1012 	 *     are consistent with the RCS values to be written to the
   1013 	 *     CRCR.'
   1014 	 *
   1015 	 * XXX Hope just zeroing it is good enough!
   1016 	 */
   1017 	xhci_host_dequeue(sc->sc_cr);
   1018 
   1019 	/*
   1020 	 * `7. Write the CRCR with the address and RCS value of the
   1021 	 *     reinitialized Command Ring.  Note that this write will
   1022 	 *     cause the Command Ring to restart at the address
   1023 	 *     specified by the CRCR.'
   1024 	 */
   1025 	xhci_op_write_8(sc, XHCI_CRCR, xhci_ring_trbp(sc->sc_cr, 0) |
   1026 	    sc->sc_cr->xr_cs);
   1027 
   1028 	/*
   1029 	 * `8. Enable the controller by setting Run/Stop (R/S) =
   1030 	 *     ``1''.'
   1031 	 */
   1032 	xhci_op_write_4(sc, XHCI_USBCMD,
   1033 	    xhci_op_read_4(sc, XHCI_USBCMD) | XHCI_CMD_RS);
   1034 
   1035 	/*
   1036 	 * `9. Software shall walk the USB topology and initialize each
   1037 	 *     of the xHC PORTSC, PORTPMSC, and PORTLI registers, and
   1038 	 *     external hub ports attached to USB devices.'
   1039 	 *
   1040 	 * This follows the procedure in 4.15 `Suspend-Resume', 4.15.2
   1041 	 * `Port Resume', 4.15.2.2 `Host Initiated'.
   1042 	 *
   1043 	 * XXX We should maybe batch up initiating the state
   1044 	 * transitions, and then wait for them to complete all at once.
   1045 	 */
   1046 	for (bn = 0; bn < 2; bn++) {
   1047 		for (i = 1; i <= sc->sc_rhportcount[bn]; i++) {
   1048 			port = XHCI_PORTSC(xhci_rhport2ctlrport(sc, bn, i));
   1049 
   1050 			/* `When a port is in the U3 state: ...' */
   1051 			v = xhci_op_read_4(sc, port);
   1052 			if (XHCI_PS_PLS_GET(v) != XHCI_PS_PLS_U3)
   1053 				continue;
   1054 
   1055 			/*
   1056 			 * `For a USB2 protocol port, software shall
   1057 			 *  write a ``15'' (Resume) to the PLS field to
   1058 			 *  initiate resume signaling.  The port shall
   1059 			 *  transition to the Resume substate and the
   1060 			 *  xHC shall transmit the resume signaling
   1061 			 *  within 1ms (T_URSM).  Software shall ensure
   1062 			 *  that resume is signaled for at least 20ms
   1063 			 *  (T_DRSMDN).  Software shall start timing
   1064 			 *  T_DRSMDN from the write of ``15'' (Resume)
   1065 			 *  to PLS.'
   1066 			 */
   1067 			if (bn == 1) {
   1068 				KASSERT(sc->sc_bus2.ub_revision == USBREV_2_0);
   1069 				v &= ~(XHCI_PS_PLS_MASK | XHCI_PS_CLEAR);
   1070 				v |= XHCI_PS_LWS;
   1071 				v |= XHCI_PS_PLS_SET(XHCI_PS_PLS_SETRESUME);
   1072 				xhci_op_write_4(sc, port, v);
   1073 				usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
   1074 			} else {
   1075 				KASSERT(sc->sc_bus.ub_revision > USBREV_2_0);
   1076 			}
   1077 
   1078 			/*
   1079 			 * `For a USB3 protocol port [and a USB2
   1080 			 *  protocol port after transitioning to
   1081 			 *  Resume], software shall write a ``0'' (U0)
   1082 			 *  to the PLS field...'
   1083 			 */
   1084 			v = xhci_op_read_4(sc, port);
   1085 			v &= ~(XHCI_PS_PLS_MASK | XHCI_PS_CLEAR);
   1086 			v |= XHCI_PS_LWS | XHCI_PS_PLS_SET(XHCI_PS_PLS_SETU0);
   1087 			xhci_op_write_4(sc, port, v);
   1088 
   1089 			for (j = 0; j < XHCI_WAIT_PLS_U0; j++) {
   1090 				v = xhci_op_read_4(sc, port);
   1091 				if (XHCI_PS_PLS_GET(v) == XHCI_PS_PLS_U0)
   1092 					break;
   1093 				usb_delay_ms(&sc->sc_bus, 1);
   1094 			}
   1095 			if (j == XHCI_WAIT_PLS_U0) {
   1096 				device_printf(self,
   1097 				    "resume timeout on bus %zu port %zu\n",
   1098 				    bn, i);
   1099 				goto out;
   1100 			}
   1101 		}
   1102 	}
   1103 
   1104 	/*
   1105 	 * `10. Restart each of the previously Running endpoints by
   1106 	 *      ringing their doorbells.'
   1107 	 */
   1108 	for (i = 0; i < sc->sc_maxslots; i++) {
   1109 		struct xhci_slot *xs = &sc->sc_slots[i];
   1110 
   1111 		/* Skip if the slot is not in use.  */
   1112 		if (xs->xs_idx == 0)
   1113 			continue;
   1114 
   1115 		for (dci = XHCI_DCI_SLOT; dci <= XHCI_MAX_DCI; dci++) {
   1116 			/* Skip if the endpoint is not Running.  */
   1117 			if (xhci_get_epstate(sc, xs, dci) !=
   1118 			    XHCI_EPSTATE_RUNNING)
   1119 				continue;
   1120 
   1121 			/* Ring the doorbell.  */
   1122 			xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci);
   1123 		}
   1124 	}
   1125 
   1126 	/*
   1127 	 * `Note: After a Save or Restore operation completes, the
   1128 	 *  Save/Restore Error (SRE) flag in the USBSTS register should
   1129 	 *  be checked to ensure that the operation completed
   1130 	 *  successfully.'
   1131 	 */
   1132 	if (xhci_op_read_4(sc, XHCI_USBSTS) & XHCI_STS_SRE) {
   1133 		device_printf(self, "resume error, USBSTS.SRE\n");
   1134 		goto out;
   1135 	}
   1136 
   1137 	/* Success!  */
   1138 	ok = true;
   1139 
   1140 out:	/*
   1141 	 * Resume command issuance.  If the hardware failed to resume,
   1142 	 * well, tough -- deadlocking because everything is held up on
   1143 	 * the suspension, with no opportunity to detach, isn't better
   1144 	 * than timing out waiting for dead hardware.
   1145 	 */
   1146 	mutex_enter(&sc->sc_lock);
   1147 	KASSERT(sc->sc_suspender);
   1148 	sc->sc_suspender = NULL;
   1149 	sc->sc_suspendresume_failed = !ok;
   1150 	cv_broadcast(&sc->sc_cmdbusy_cv);
   1151 	mutex_exit(&sc->sc_lock);
   1152 
   1153 	mutex_exit(&sc->sc_rhlock);
   1154 	return ok;
   1155 }
   1156 
   1157 bool
   1158 xhci_shutdown(device_t self, int flags)
   1159 {
   1160 	return false;
   1161 }
   1162 
   1163 static int
   1164 xhci_hc_reset(struct xhci_softc * const sc)
   1165 {
   1166 	uint32_t usbcmd, usbsts;
   1167 	int i;
   1168 
   1169 	/* Check controller not ready */
   1170 	for (i = 0; i < XHCI_WAIT_CNR; i++) {
   1171 		usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
   1172 		if ((usbsts & XHCI_STS_CNR) == 0)
   1173 			break;
   1174 		usb_delay_ms(&sc->sc_bus, 1);
   1175 	}
   1176 	if (i >= XHCI_WAIT_CNR) {
   1177 		aprint_error_dev(sc->sc_dev, "controller not ready timeout\n");
   1178 		return EIO;
   1179 	}
   1180 
   1181 	/* Halt controller */
   1182 	usbcmd = 0;
   1183 	xhci_op_write_4(sc, XHCI_USBCMD, usbcmd);
   1184 	usb_delay_ms(&sc->sc_bus, 1);
   1185 
   1186 	/* Reset controller */
   1187 	usbcmd = XHCI_CMD_HCRST;
   1188 	xhci_op_write_4(sc, XHCI_USBCMD, usbcmd);
   1189 	for (i = 0; i < XHCI_WAIT_HCRST; i++) {
   1190 		/*
   1191 		 * Wait 1ms first. Existing Intel xHCI requires 1ms delay to
   1192 		 * prevent system hang (Errata).
   1193 		 */
   1194 		usb_delay_ms(&sc->sc_bus, 1);
   1195 		usbcmd = xhci_op_read_4(sc, XHCI_USBCMD);
   1196 		if ((usbcmd & XHCI_CMD_HCRST) == 0)
   1197 			break;
   1198 	}
   1199 	if (i >= XHCI_WAIT_HCRST) {
   1200 		aprint_error_dev(sc->sc_dev, "host controller reset timeout\n");
   1201 		return EIO;
   1202 	}
   1203 
   1204 	/* Check controller not ready */
   1205 	for (i = 0; i < XHCI_WAIT_CNR; i++) {
   1206 		usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
   1207 		if ((usbsts & XHCI_STS_CNR) == 0)
   1208 			break;
   1209 		usb_delay_ms(&sc->sc_bus, 1);
   1210 	}
   1211 	if (i >= XHCI_WAIT_CNR) {
   1212 		aprint_error_dev(sc->sc_dev,
   1213 		    "controller not ready timeout after reset\n");
   1214 		return EIO;
   1215 	}
   1216 
   1217 	return 0;
   1218 }
   1219 
   1220 /* 7.2 xHCI Support Protocol Capability */
   1221 static void
   1222 xhci_id_protocols(struct xhci_softc *sc, bus_size_t ecp)
   1223 {
   1224 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1225 
   1226 	/* XXX Cache this lot */
   1227 
   1228 	const uint32_t w0 = xhci_read_4(sc, ecp);
   1229 	const uint32_t w4 = xhci_read_4(sc, ecp + 4);
   1230 	const uint32_t w8 = xhci_read_4(sc, ecp + 8);
   1231 	const uint32_t wc = xhci_read_4(sc, ecp + 0xc);
   1232 
   1233 	aprint_debug_dev(sc->sc_dev,
   1234 	    " SP: 0x%08x 0x%08x 0x%08x 0x%08x\n", w0, w4, w8, wc);
   1235 
   1236 	if (w4 != XHCI_XECP_USBID)
   1237 		return;
   1238 
   1239 	const int major = XHCI_XECP_SP_W0_MAJOR(w0);
   1240 	const int minor = XHCI_XECP_SP_W0_MINOR(w0);
   1241 	const uint8_t cpo = XHCI_XECP_SP_W8_CPO(w8);
   1242 	const uint8_t cpc = XHCI_XECP_SP_W8_CPC(w8);
   1243 
   1244 	const uint16_t mm = __SHIFTOUT(w0, __BITS(31, 16));
   1245 	switch (mm) {
   1246 	case 0x0200:
   1247 	case 0x0300:
   1248 	case 0x0301:
   1249 	case 0x0310:
   1250 	case 0x0320:
   1251 		aprint_debug_dev(sc->sc_dev, " %s ports %d - %d\n",
   1252 		    major == 3 ? "ss" : "hs", cpo, cpo + cpc - 1);
   1253 		if (major == 3)
   1254 			sc->sc_usb3nports += cpo + cpc - 1;
   1255 		else
   1256 			sc->sc_usb2nports += cpo + cpc - 1;
   1257 		break;
   1258 	default:
   1259 		aprint_error_dev(sc->sc_dev, " unknown major/minor (%d/%d)\n",
   1260 		    major, minor);
   1261 		return;
   1262 	}
   1263 
   1264 	const size_t bus = (major == 3) ? 0 : 1;
   1265 
   1266 	/* Index arrays with 0..n-1 where ports are numbered 1..n */
   1267 	for (size_t cp = cpo - 1; cp < cpo + cpc - 1; cp++) {
   1268 		if (sc->sc_ctlrportmap[cp] != 0) {
   1269 			aprint_error_dev(sc->sc_dev, "controller port %zu "
   1270 			    "already assigned", cp);
   1271 			continue;
   1272 		}
   1273 
   1274 		sc->sc_ctlrportbus[cp / NBBY] |=
   1275 		    bus == 0 ? 0 : __BIT(cp % NBBY);
   1276 
   1277 		const size_t rhp = sc->sc_rhportcount[bus]++;
   1278 
   1279 		KASSERTMSG(sc->sc_rhportmap[bus][rhp] == 0,
   1280 		    "bus %zu rhp %zu is %d", bus, rhp,
   1281 		    sc->sc_rhportmap[bus][rhp]);
   1282 
   1283 		sc->sc_rhportmap[bus][rhp] = cp + 1;
   1284 		sc->sc_ctlrportmap[cp] = rhp + 1;
   1285 	}
   1286 }
   1287 
   1288 /* Process extended capabilities */
   1289 static void
   1290 xhci_ecp(struct xhci_softc *sc)
   1291 {
   1292 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1293 
   1294 	bus_size_t ecp = XHCI_HCC_XECP(sc->sc_hcc) * 4;
   1295 	while (ecp != 0) {
   1296 		uint32_t ecr = xhci_read_4(sc, ecp);
   1297 		aprint_debug_dev(sc->sc_dev, "ECR: 0x%08x\n", ecr);
   1298 		switch (XHCI_XECP_ID(ecr)) {
   1299 		case XHCI_ID_PROTOCOLS: {
   1300 			xhci_id_protocols(sc, ecp);
   1301 			break;
   1302 		}
   1303 		case XHCI_ID_USB_LEGACY: {
   1304 			uint8_t bios_sem;
   1305 
   1306 			/* Take host controller ownership from BIOS */
   1307 			bios_sem = xhci_read_1(sc, ecp + XHCI_XECP_BIOS_SEM);
   1308 			if (bios_sem) {
   1309 				/* sets xHCI to be owned by OS */
   1310 				xhci_write_1(sc, ecp + XHCI_XECP_OS_SEM, 1);
   1311 				aprint_debug_dev(sc->sc_dev,
   1312 				    "waiting for BIOS to give up control\n");
   1313 				for (int i = 0; i < 5000; i++) {
   1314 					bios_sem = xhci_read_1(sc, ecp +
   1315 					    XHCI_XECP_BIOS_SEM);
   1316 					if (bios_sem == 0)
   1317 						break;
   1318 					DELAY(1000);
   1319 				}
   1320 				if (bios_sem) {
   1321 					aprint_error_dev(sc->sc_dev,
   1322 					    "timed out waiting for BIOS\n");
   1323 				}
   1324 			}
   1325 			break;
   1326 		}
   1327 		default:
   1328 			break;
   1329 		}
   1330 		ecr = xhci_read_4(sc, ecp);
   1331 		if (XHCI_XECP_NEXT(ecr) == 0) {
   1332 			ecp = 0;
   1333 		} else {
   1334 			ecp += XHCI_XECP_NEXT(ecr) * 4;
   1335 		}
   1336 	}
   1337 }
   1338 
   1339 #define XHCI_HCCPREV1_BITS	\
   1340 	"\177\020"	/* New bitmask */			\
   1341 	"f\020\020XECP\0"					\
   1342 	"f\014\4MAXPSA\0"					\
   1343 	"b\013CFC\0"						\
   1344 	"b\012SEC\0"						\
   1345 	"b\011SBD\0"						\
   1346 	"b\010FSE\0"						\
   1347 	"b\7NSS\0"						\
   1348 	"b\6LTC\0"						\
   1349 	"b\5LHRC\0"						\
   1350 	"b\4PIND\0"						\
   1351 	"b\3PPC\0"						\
   1352 	"b\2CZC\0"						\
   1353 	"b\1BNC\0"						\
   1354 	"b\0AC64\0"						\
   1355 	"\0"
   1356 #define XHCI_HCCV1_x_BITS	\
   1357 	"\177\020"	/* New bitmask */			\
   1358 	"f\020\020XECP\0"					\
   1359 	"f\014\4MAXPSA\0"					\
   1360 	"b\013CFC\0"						\
   1361 	"b\012SEC\0"						\
   1362 	"b\011SPC\0"						\
   1363 	"b\010PAE\0"						\
   1364 	"b\7NSS\0"						\
   1365 	"b\6LTC\0"						\
   1366 	"b\5LHRC\0"						\
   1367 	"b\4PIND\0"						\
   1368 	"b\3PPC\0"						\
   1369 	"b\2CSZ\0"						\
   1370 	"b\1BNC\0"						\
   1371 	"b\0AC64\0"						\
   1372 	"\0"
   1373 
   1374 #define XHCI_HCC2_BITS	\
   1375 	"\177\020"	/* New bitmask */			\
   1376 	"b\7ETC_TSC\0"						\
   1377 	"b\6ETC\0"						\
   1378 	"b\5CIC\0"						\
   1379 	"b\4LEC\0"						\
   1380 	"b\3CTC\0"						\
   1381 	"b\2FSC\0"						\
   1382 	"b\1CMC\0"						\
   1383 	"b\0U3C\0"						\
   1384 	"\0"
   1385 
   1386 void
   1387 xhci_start(struct xhci_softc *sc)
   1388 {
   1389 	xhci_rt_write_4(sc, XHCI_IMAN(0), XHCI_IMAN_INTR_ENA);
   1390 	if ((sc->sc_quirks & XHCI_QUIRK_INTEL) != 0)
   1391 		/* Intel xhci needs interrupt rate moderated. */
   1392 		xhci_rt_write_4(sc, XHCI_IMOD(0), XHCI_IMOD_DEFAULT_LP);
   1393 	else
   1394 		xhci_rt_write_4(sc, XHCI_IMOD(0), 0);
   1395 	aprint_debug_dev(sc->sc_dev, "current IMOD %u\n",
   1396 	    xhci_rt_read_4(sc, XHCI_IMOD(0)));
   1397 
   1398 	/* Go! */
   1399 	xhci_op_write_4(sc, XHCI_USBCMD, XHCI_CMD_INTE|XHCI_CMD_RS);
   1400 	aprint_debug_dev(sc->sc_dev, "USBCMD 0x%08"PRIx32"\n",
   1401 	    xhci_op_read_4(sc, XHCI_USBCMD));
   1402 }
   1403 
   1404 int
   1405 xhci_init(struct xhci_softc *sc)
   1406 {
   1407 	bus_size_t bsz;
   1408 	uint32_t hcs1, hcs2, hcs3, dboff, rtsoff;
   1409 	uint32_t pagesize, config;
   1410 	int i = 0;
   1411 	uint16_t hciversion;
   1412 	uint8_t caplength;
   1413 
   1414 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1415 
   1416 	/* Set up the bus struct for the usb 3 and usb 2 buses */
   1417 	sc->sc_bus.ub_methods = &xhci_bus_methods;
   1418 	sc->sc_bus.ub_pipesize = sizeof(struct xhci_pipe);
   1419 	sc->sc_bus.ub_usedma = true;
   1420 	sc->sc_bus.ub_hcpriv = sc;
   1421 
   1422 	sc->sc_bus2.ub_methods = &xhci_bus_methods;
   1423 	sc->sc_bus2.ub_pipesize = sizeof(struct xhci_pipe);
   1424 	sc->sc_bus2.ub_revision = USBREV_2_0;
   1425 	sc->sc_bus2.ub_usedma = true;
   1426 	sc->sc_bus2.ub_hcpriv = sc;
   1427 	sc->sc_bus2.ub_dmatag = sc->sc_bus.ub_dmatag;
   1428 
   1429 	caplength = xhci_read_1(sc, XHCI_CAPLENGTH);
   1430 	hciversion = xhci_read_2(sc, XHCI_HCIVERSION);
   1431 
   1432 	if (hciversion < XHCI_HCIVERSION_0_96 ||
   1433 	    hciversion >= 0x0200) {
   1434 		aprint_normal_dev(sc->sc_dev,
   1435 		    "xHCI version %x.%x not known to be supported\n",
   1436 		    (hciversion >> 8) & 0xff, (hciversion >> 0) & 0xff);
   1437 	} else {
   1438 		aprint_verbose_dev(sc->sc_dev, "xHCI version %x.%x\n",
   1439 		    (hciversion >> 8) & 0xff, (hciversion >> 0) & 0xff);
   1440 	}
   1441 
   1442 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0, caplength,
   1443 	    &sc->sc_cbh) != 0) {
   1444 		aprint_error_dev(sc->sc_dev, "capability subregion failure\n");
   1445 		return ENOMEM;
   1446 	}
   1447 
   1448 	hcs1 = xhci_cap_read_4(sc, XHCI_HCSPARAMS1);
   1449 	sc->sc_maxslots = XHCI_HCS1_MAXSLOTS(hcs1);
   1450 	sc->sc_maxintrs = XHCI_HCS1_MAXINTRS(hcs1);
   1451 	sc->sc_maxports = XHCI_HCS1_MAXPORTS(hcs1);
   1452 	hcs2 = xhci_cap_read_4(sc, XHCI_HCSPARAMS2);
   1453 	hcs3 = xhci_cap_read_4(sc, XHCI_HCSPARAMS3);
   1454 	aprint_debug_dev(sc->sc_dev,
   1455 	    "hcs1=%"PRIx32" hcs2=%"PRIx32" hcs3=%"PRIx32"\n", hcs1, hcs2, hcs3);
   1456 
   1457 	sc->sc_hcc = xhci_cap_read_4(sc, XHCI_HCCPARAMS);
   1458 	sc->sc_ctxsz = XHCI_HCC_CSZ(sc->sc_hcc) ? 64 : 32;
   1459 
   1460 	char sbuf[128];
   1461 	if (hciversion < XHCI_HCIVERSION_1_0)
   1462 		snprintb(sbuf, sizeof(sbuf), XHCI_HCCPREV1_BITS, sc->sc_hcc);
   1463 	else
   1464 		snprintb(sbuf, sizeof(sbuf), XHCI_HCCV1_x_BITS, sc->sc_hcc);
   1465 	aprint_debug_dev(sc->sc_dev, "hcc=%s\n", sbuf);
   1466 	aprint_debug_dev(sc->sc_dev, "xECP %" __PRIxBITS "\n",
   1467 	    XHCI_HCC_XECP(sc->sc_hcc) * 4);
   1468 	if (hciversion >= XHCI_HCIVERSION_1_1) {
   1469 		sc->sc_hcc2 = xhci_cap_read_4(sc, XHCI_HCCPARAMS2);
   1470 		snprintb(sbuf, sizeof(sbuf), XHCI_HCC2_BITS, sc->sc_hcc2);
   1471 		aprint_debug_dev(sc->sc_dev, "hcc2=%s\n", sbuf);
   1472 	}
   1473 
   1474 	/* default all ports to bus 0, i.e. usb 3 */
   1475 	sc->sc_ctlrportbus = kmem_zalloc(
   1476 	    howmany(sc->sc_maxports * sizeof(uint8_t), NBBY), KM_SLEEP);
   1477 	sc->sc_ctlrportmap =
   1478 	    kmem_zalloc(sc->sc_maxports * sizeof(int), KM_SLEEP);
   1479 
   1480 	/* controller port to bus roothub port map */
   1481 	for (size_t j = 0; j < __arraycount(sc->sc_rhportmap); j++) {
   1482 		sc->sc_rhportmap[j] =
   1483 		    kmem_zalloc(sc->sc_maxports * sizeof(int), KM_SLEEP);
   1484 	}
   1485 
   1486 	/*
   1487 	 * Process all Extended Capabilities
   1488 	 */
   1489 	xhci_ecp(sc);
   1490 
   1491 	bsz = XHCI_PORTSC(sc->sc_maxports);
   1492 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, caplength, bsz,
   1493 	    &sc->sc_obh) != 0) {
   1494 		aprint_error_dev(sc->sc_dev, "operational subregion failure\n");
   1495 		return ENOMEM;
   1496 	}
   1497 
   1498 	dboff = xhci_cap_read_4(sc, XHCI_DBOFF);
   1499 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, dboff,
   1500 	    sc->sc_maxslots * 4, &sc->sc_dbh) != 0) {
   1501 		aprint_error_dev(sc->sc_dev, "doorbell subregion failure\n");
   1502 		return ENOMEM;
   1503 	}
   1504 
   1505 	rtsoff = xhci_cap_read_4(sc, XHCI_RTSOFF);
   1506 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, rtsoff,
   1507 	    sc->sc_maxintrs * 0x20, &sc->sc_rbh) != 0) {
   1508 		aprint_error_dev(sc->sc_dev, "runtime subregion failure\n");
   1509 		return ENOMEM;
   1510 	}
   1511 
   1512 	int rv;
   1513 	rv = xhci_hc_reset(sc);
   1514 	if (rv != 0) {
   1515 		return rv;
   1516 	}
   1517 
   1518 	if (sc->sc_vendor_init)
   1519 		sc->sc_vendor_init(sc);
   1520 
   1521 	pagesize = xhci_op_read_4(sc, XHCI_PAGESIZE);
   1522 	aprint_debug_dev(sc->sc_dev, "PAGESIZE 0x%08x\n", pagesize);
   1523 	pagesize = ffs(pagesize);
   1524 	if (pagesize == 0) {
   1525 		aprint_error_dev(sc->sc_dev, "pagesize is 0\n");
   1526 		return EIO;
   1527 	}
   1528 	sc->sc_pgsz = 1 << (12 + (pagesize - 1));
   1529 	aprint_debug_dev(sc->sc_dev, "sc_pgsz 0x%08x\n", (uint32_t)sc->sc_pgsz);
   1530 	aprint_debug_dev(sc->sc_dev, "sc_maxslots 0x%08x\n",
   1531 	    (uint32_t)sc->sc_maxslots);
   1532 	aprint_debug_dev(sc->sc_dev, "sc_maxports %d\n", sc->sc_maxports);
   1533 
   1534 	int err;
   1535 	sc->sc_maxspbuf = XHCI_HCS2_MAXSPBUF(hcs2);
   1536 	aprint_debug_dev(sc->sc_dev, "sc_maxspbuf %d\n", sc->sc_maxspbuf);
   1537 	if (sc->sc_maxspbuf != 0) {
   1538 		err = usb_allocmem(sc->sc_bus.ub_dmatag,
   1539 		    sizeof(uint64_t) * sc->sc_maxspbuf, sizeof(uint64_t),
   1540 		    USBMALLOC_ZERO, &sc->sc_spbufarray_dma);
   1541 		if (err) {
   1542 			aprint_error_dev(sc->sc_dev,
   1543 			    "spbufarray init fail, err %d\n", err);
   1544 			return ENOMEM;
   1545 		}
   1546 
   1547 		sc->sc_spbuf_dma = kmem_zalloc(sizeof(*sc->sc_spbuf_dma) *
   1548 		    sc->sc_maxspbuf, KM_SLEEP);
   1549 		uint64_t *spbufarray = KERNADDR(&sc->sc_spbufarray_dma, 0);
   1550 		for (i = 0; i < sc->sc_maxspbuf; i++) {
   1551 			usb_dma_t * const dma = &sc->sc_spbuf_dma[i];
   1552 			/* allocate contexts */
   1553 			err = usb_allocmem(sc->sc_bus.ub_dmatag, sc->sc_pgsz,
   1554 			    sc->sc_pgsz, USBMALLOC_ZERO, dma);
   1555 			if (err) {
   1556 				aprint_error_dev(sc->sc_dev,
   1557 				    "spbufarray_dma init fail, err %d\n", err);
   1558 				rv = ENOMEM;
   1559 				goto bad1;
   1560 			}
   1561 			spbufarray[i] = htole64(DMAADDR(dma, 0));
   1562 			usb_syncmem(dma, 0, sc->sc_pgsz,
   1563 			    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1564 		}
   1565 
   1566 		usb_syncmem(&sc->sc_spbufarray_dma, 0,
   1567 		    sizeof(uint64_t) * sc->sc_maxspbuf, BUS_DMASYNC_PREWRITE);
   1568 	}
   1569 
   1570 	sc->sc_isthresh = XHCI_HCS2_IST(hcs2);
   1571 	aprint_debug_dev(sc->sc_dev, "sc_isthresh %d\n", sc->sc_isthresh);
   1572 
   1573 	/*
   1574 	 * xHI 5.3.4
   1575 	 * If bit[3] is 0, IST is number of microframes in bit[2:0]
   1576 	 * If bit[3] is 1, IST is number of frames in bit[2:0]
   1577 	 */
   1578 	if (sc->sc_isthresh & 0x8) {
   1579 		sc->sc_isthresh = (sc->sc_isthresh & 0x7) *
   1580 		    USB_UFRAMES_PER_FRAME;
   1581 	}
   1582 
   1583 	config = xhci_op_read_4(sc, XHCI_CONFIG);
   1584 	config &= ~0xFF;
   1585 	config |= sc->sc_maxslots & 0xFF;
   1586 	xhci_op_write_4(sc, XHCI_CONFIG, config);
   1587 
   1588 	err = xhci_ring_init(sc, &sc->sc_cr, XHCI_COMMAND_RING_TRBS,
   1589 	    XHCI_COMMAND_RING_SEGMENTS_ALIGN);
   1590 	if (err) {
   1591 		aprint_error_dev(sc->sc_dev, "command ring init fail, err %d\n",
   1592 		    err);
   1593 		rv = ENOMEM;
   1594 		goto bad1;
   1595 	}
   1596 
   1597 	err = xhci_ring_init(sc, &sc->sc_er, XHCI_EVENT_RING_TRBS,
   1598 	    XHCI_EVENT_RING_SEGMENTS_ALIGN);
   1599 	if (err) {
   1600 		aprint_error_dev(sc->sc_dev, "event ring init fail, err %d\n",
   1601 		    err);
   1602 		rv = ENOMEM;
   1603 		goto bad2;
   1604 	}
   1605 
   1606 	usb_dma_t *dma;
   1607 	size_t size;
   1608 	size_t align;
   1609 
   1610 	dma = &sc->sc_eventst_dma;
   1611 	size = roundup2(XHCI_EVENT_RING_SEGMENTS * XHCI_ERSTE_SIZE,
   1612 	    XHCI_EVENT_RING_SEGMENT_TABLE_ALIGN);
   1613 	KASSERTMSG(size <= (512 * 1024), "eventst size %zu too large", size);
   1614 	align = XHCI_EVENT_RING_SEGMENT_TABLE_ALIGN;
   1615 	err = usb_allocmem(sc->sc_bus.ub_dmatag, size, align,
   1616 	    USBMALLOC_ZERO, dma);
   1617 	if (err) {
   1618 		aprint_error_dev(sc->sc_dev, "eventst init fail, err %d\n",
   1619 		    err);
   1620 		rv = ENOMEM;
   1621 		goto bad3;
   1622 	}
   1623 
   1624 	aprint_debug_dev(sc->sc_dev, "eventst: 0x%016jx %p %zx\n",
   1625 	    (uintmax_t)DMAADDR(&sc->sc_eventst_dma, 0),
   1626 	    KERNADDR(&sc->sc_eventst_dma, 0),
   1627 	    sc->sc_eventst_dma.udma_block->size);
   1628 
   1629 	dma = &sc->sc_dcbaa_dma;
   1630 	size = (1 + sc->sc_maxslots) * sizeof(uint64_t);
   1631 	KASSERTMSG(size <= 2048, "dcbaa size %zu too large", size);
   1632 	align = XHCI_DEVICE_CONTEXT_BASE_ADDRESS_ARRAY_ALIGN;
   1633 	err = usb_allocmem(sc->sc_bus.ub_dmatag, size, align,
   1634 	    USBMALLOC_ZERO, dma);
   1635 	if (err) {
   1636 		aprint_error_dev(sc->sc_dev, "dcbaa init fail, err %d\n", err);
   1637 		rv = ENOMEM;
   1638 		goto bad4;
   1639 	}
   1640 	aprint_debug_dev(sc->sc_dev, "dcbaa: 0x%016jx %p %zx\n",
   1641 	    (uintmax_t)DMAADDR(&sc->sc_dcbaa_dma, 0),
   1642 	    KERNADDR(&sc->sc_dcbaa_dma, 0),
   1643 	    sc->sc_dcbaa_dma.udma_block->size);
   1644 
   1645 	if (sc->sc_maxspbuf != 0) {
   1646 		/*
   1647 		 * DCBA entry 0 hold the scratchbuf array pointer.
   1648 		 */
   1649 		*(uint64_t *)KERNADDR(dma, 0) =
   1650 		    htole64(DMAADDR(&sc->sc_spbufarray_dma, 0));
   1651 		usb_syncmem(dma, 0, size, BUS_DMASYNC_PREWRITE);
   1652 	}
   1653 
   1654 	sc->sc_slots = kmem_zalloc(sizeof(*sc->sc_slots) * sc->sc_maxslots,
   1655 	    KM_SLEEP);
   1656 	if (sc->sc_slots == NULL) {
   1657 		aprint_error_dev(sc->sc_dev, "slots init fail, err %d\n", err);
   1658 		rv = ENOMEM;
   1659 		goto bad;
   1660 	}
   1661 
   1662 	sc->sc_xferpool = pool_cache_init(sizeof(struct xhci_xfer), 0, 0, 0,
   1663 	    "xhcixfer", NULL, IPL_USB, NULL, NULL, NULL);
   1664 	if (sc->sc_xferpool == NULL) {
   1665 		aprint_error_dev(sc->sc_dev, "pool_cache init fail, err %d\n",
   1666 		    err);
   1667 		rv = ENOMEM;
   1668 		goto bad;
   1669 	}
   1670 
   1671 	cv_init(&sc->sc_command_cv, "xhcicmd");
   1672 	cv_init(&sc->sc_cmdbusy_cv, "xhcicmdq");
   1673 	mutex_init(&sc->sc_rhlock, MUTEX_DEFAULT, IPL_NONE);
   1674 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
   1675 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_USB);
   1676 
   1677 	struct xhci_erste *erst;
   1678 	erst = KERNADDR(&sc->sc_eventst_dma, 0);
   1679 	erst[0].erste_0 = htole64(xhci_ring_trbp(sc->sc_er, 0));
   1680 	erst[0].erste_2 = htole32(sc->sc_er->xr_ntrb);
   1681 	erst[0].erste_3 = htole32(0);
   1682 	usb_syncmem(&sc->sc_eventst_dma, 0,
   1683 	    XHCI_ERSTE_SIZE * XHCI_EVENT_RING_SEGMENTS, BUS_DMASYNC_PREWRITE);
   1684 
   1685 	xhci_rt_write_4(sc, XHCI_ERSTSZ(0), XHCI_EVENT_RING_SEGMENTS);
   1686 	xhci_rt_write_8(sc, XHCI_ERSTBA(0), DMAADDR(&sc->sc_eventst_dma, 0));
   1687 	xhci_rt_write_8(sc, XHCI_ERDP(0), xhci_ring_trbp(sc->sc_er, 0) |
   1688 	    XHCI_ERDP_BUSY);
   1689 
   1690 	xhci_op_write_8(sc, XHCI_DCBAAP, DMAADDR(&sc->sc_dcbaa_dma, 0));
   1691 	xhci_op_write_8(sc, XHCI_CRCR, xhci_ring_trbp(sc->sc_cr, 0) |
   1692 	    sc->sc_cr->xr_cs);
   1693 
   1694 	HEXDUMP("eventst", KERNADDR(&sc->sc_eventst_dma, 0),
   1695 	    XHCI_ERSTE_SIZE * XHCI_EVENT_RING_SEGMENTS);
   1696 
   1697 	if ((sc->sc_quirks & XHCI_DEFERRED_START) == 0)
   1698 		xhci_start(sc);
   1699 
   1700 	return 0;
   1701 
   1702  bad:
   1703 	if (sc->sc_xferpool) {
   1704 		pool_cache_destroy(sc->sc_xferpool);
   1705 		sc->sc_xferpool = NULL;
   1706 	}
   1707 
   1708 	if (sc->sc_slots) {
   1709 		kmem_free(sc->sc_slots, sizeof(*sc->sc_slots) *
   1710 		    sc->sc_maxslots);
   1711 		sc->sc_slots = NULL;
   1712 	}
   1713 
   1714 	usb_freemem(&sc->sc_dcbaa_dma);
   1715  bad4:
   1716 	usb_freemem(&sc->sc_eventst_dma);
   1717  bad3:
   1718 	xhci_ring_free(sc, &sc->sc_er);
   1719  bad2:
   1720 	xhci_ring_free(sc, &sc->sc_cr);
   1721 	i = sc->sc_maxspbuf;
   1722  bad1:
   1723 	for (int j = 0; j < i; j++)
   1724 		usb_freemem(&sc->sc_spbuf_dma[j]);
   1725 	usb_freemem(&sc->sc_spbufarray_dma);
   1726 
   1727 	return rv;
   1728 }
   1729 
   1730 static inline bool
   1731 xhci_polling_p(struct xhci_softc * const sc)
   1732 {
   1733 	return sc->sc_bus.ub_usepolling || sc->sc_bus2.ub_usepolling;
   1734 }
   1735 
   1736 int
   1737 xhci_intr(void *v)
   1738 {
   1739 	struct xhci_softc * const sc = v;
   1740 	int ret = 0;
   1741 
   1742 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1743 
   1744 	if (sc == NULL)
   1745 		return 0;
   1746 
   1747 	mutex_spin_enter(&sc->sc_intr_lock);
   1748 
   1749 	if (sc->sc_dying || !device_has_power(sc->sc_dev))
   1750 		goto done;
   1751 
   1752 	/* If we get an interrupt while polling, then just ignore it. */
   1753 	if (xhci_polling_p(sc)) {
   1754 #ifdef DIAGNOSTIC
   1755 		DPRINTFN(16, "ignored interrupt while polling", 0, 0, 0, 0);
   1756 #endif
   1757 		goto done;
   1758 	}
   1759 
   1760 	ret = xhci_intr1(sc);
   1761 	if (ret) {
   1762 		KASSERT(sc->sc_child || sc->sc_child2);
   1763 
   1764 		/*
   1765 		 * One of child busses could be already detached. It doesn't
   1766 		 * matter on which of the two the softintr is scheduled.
   1767 		 */
   1768 		if (sc->sc_child)
   1769 			usb_schedsoftintr(&sc->sc_bus);
   1770 		else
   1771 			usb_schedsoftintr(&sc->sc_bus2);
   1772 	}
   1773 done:
   1774 	mutex_spin_exit(&sc->sc_intr_lock);
   1775 	return ret;
   1776 }
   1777 
   1778 int
   1779 xhci_intr1(struct xhci_softc * const sc)
   1780 {
   1781 	uint32_t usbsts;
   1782 	uint32_t iman;
   1783 
   1784 	XHCIHIST_FUNC();
   1785 
   1786 	usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
   1787 	XHCIHIST_CALLARGS("USBSTS 0x%08jx", usbsts, 0, 0, 0);
   1788 	if ((usbsts & (XHCI_STS_HSE | XHCI_STS_EINT | XHCI_STS_PCD |
   1789 	    XHCI_STS_HCE)) == 0) {
   1790 		DPRINTFN(16, "ignored intr not for %jd",
   1791 		    device_unit(sc->sc_dev), 0, 0, 0);
   1792 		return 0;
   1793 	}
   1794 
   1795 	/*
   1796 	 * Clear EINT and other transient flags, to not misenterpret
   1797 	 * next shared interrupt. Also, to avoid race, EINT must be cleared
   1798 	 * before XHCI_IMAN_INTR_PEND is cleared.
   1799 	 */
   1800 	xhci_op_write_4(sc, XHCI_USBSTS, usbsts & ~XHCI_STS_RSVDP0);
   1801 
   1802 #ifdef XHCI_DEBUG
   1803 	usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
   1804 	DPRINTFN(16, "USBSTS 0x%08jx", usbsts, 0, 0, 0);
   1805 #endif
   1806 
   1807 	iman = xhci_rt_read_4(sc, XHCI_IMAN(0));
   1808 	DPRINTFN(16, "IMAN0 0x%08jx", iman, 0, 0, 0);
   1809 	iman |= XHCI_IMAN_INTR_PEND;
   1810 	xhci_rt_write_4(sc, XHCI_IMAN(0), iman);
   1811 
   1812 #ifdef XHCI_DEBUG
   1813 	iman = xhci_rt_read_4(sc, XHCI_IMAN(0));
   1814 	DPRINTFN(16, "IMAN0 0x%08jx", iman, 0, 0, 0);
   1815 	usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
   1816 	DPRINTFN(16, "USBSTS 0x%08jx", usbsts, 0, 0, 0);
   1817 #endif
   1818 
   1819 	return 1;
   1820 }
   1821 
   1822 /*
   1823  * 3 port speed types used in USB stack
   1824  *
   1825  * usbdi speed
   1826  *	definition: USB_SPEED_* in usb.h
   1827  *	They are used in struct usbd_device in USB stack.
   1828  *	ioctl interface uses these values too.
   1829  * port_status speed
   1830  *	definition: UPS_*_SPEED in usb.h
   1831  *	They are used in usb_port_status_t and valid only for USB 2.0.
   1832  *	Speed value is always 0 for Super Speed or more, and dwExtPortStatus
   1833  *	of usb_port_status_ext_t indicates port speed.
   1834  *	Note that some 3.0 values overlap with 2.0 values.
   1835  *	(e.g. 0x200 means UPS_POER_POWER_SS in SS and
   1836  *	            means UPS_LOW_SPEED in HS.)
   1837  *	port status returned from hub also uses these values.
   1838  *	On NetBSD UPS_OTHER_SPEED indicates port speed is super speed
   1839  *	or more.
   1840  * xspeed:
   1841  *	definition: Protocol Speed ID (PSI) (xHCI 1.1 7.2.1)
   1842  *	They are used in only slot context and PORTSC reg of xhci.
   1843  *	The difference between usbdi speed and xspeed is
   1844  *	that FS and LS values are swapped.
   1845  */
   1846 
   1847 /* convert usbdi speed to xspeed */
   1848 static int
   1849 xhci_speed2xspeed(int speed)
   1850 {
   1851 	switch (speed) {
   1852 	case USB_SPEED_LOW:	return 2;
   1853 	case USB_SPEED_FULL:	return 1;
   1854 	default:		return speed;
   1855 	}
   1856 }
   1857 
   1858 #if 0
   1859 /* convert xspeed to usbdi speed */
   1860 static int
   1861 xhci_xspeed2speed(int xspeed)
   1862 {
   1863 	switch (xspeed) {
   1864 	case 1: return USB_SPEED_FULL;
   1865 	case 2: return USB_SPEED_LOW;
   1866 	default: return xspeed;
   1867 	}
   1868 }
   1869 #endif
   1870 
   1871 /* convert xspeed to port status speed */
   1872 static int
   1873 xhci_xspeed2psspeed(int xspeed)
   1874 {
   1875 	switch (xspeed) {
   1876 	case 0: return 0;
   1877 	case 1: return UPS_FULL_SPEED;
   1878 	case 2: return UPS_LOW_SPEED;
   1879 	case 3: return UPS_HIGH_SPEED;
   1880 	default: return UPS_OTHER_SPEED;
   1881 	}
   1882 }
   1883 
   1884 /*
   1885  * Construct input contexts and issue TRB to open pipe.
   1886  */
   1887 static usbd_status
   1888 xhci_configure_endpoint(struct usbd_pipe *pipe)
   1889 {
   1890 	struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
   1891 	struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
   1892 #ifdef USB_DEBUG
   1893 	const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
   1894 #endif
   1895 	struct xhci_soft_trb trb;
   1896 	usbd_status err;
   1897 
   1898 	XHCIHIST_FUNC();
   1899 	XHCIHIST_CALLARGS("slot %ju dci %ju epaddr 0x%02jx attr 0x%02jx",
   1900 	    xs->xs_idx, dci, pipe->up_endpoint->ue_edesc->bEndpointAddress,
   1901 	    pipe->up_endpoint->ue_edesc->bmAttributes);
   1902 
   1903 	/* XXX ensure input context is available? */
   1904 
   1905 	memset(xhci_slot_get_icv(sc, xs, 0), 0, sc->sc_pgsz);
   1906 
   1907 	/* set up context */
   1908 	xhci_setup_ctx(pipe);
   1909 
   1910 	HEXDUMP("input control context", xhci_slot_get_icv(sc, xs, 0),
   1911 	    sc->sc_ctxsz * 1);
   1912 	HEXDUMP("input endpoint context", xhci_slot_get_icv(sc, xs,
   1913 	    xhci_dci_to_ici(dci)), sc->sc_ctxsz * 1);
   1914 
   1915 	trb.trb_0 = xhci_slot_get_icp(sc, xs, 0);
   1916 	trb.trb_2 = 0;
   1917 	trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
   1918 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_CONFIGURE_EP);
   1919 
   1920 	err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
   1921 
   1922 	usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
   1923 	HEXDUMP("output context", xhci_slot_get_dcv(sc, xs, dci),
   1924 	    sc->sc_ctxsz * 1);
   1925 
   1926 	return err;
   1927 }
   1928 
   1929 #if 0
   1930 static usbd_status
   1931 xhci_unconfigure_endpoint(struct usbd_pipe *pipe)
   1932 {
   1933 #ifdef USB_DEBUG
   1934 	struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
   1935 #endif
   1936 
   1937 	XHCIHIST_FUNC();
   1938 	XHCIHIST_CALLARGS("slot %ju", xs->xs_idx, 0, 0, 0);
   1939 
   1940 	return USBD_NORMAL_COMPLETION;
   1941 }
   1942 #endif
   1943 
   1944 /* 4.6.8, 6.4.3.7 */
   1945 static void
   1946 xhci_reset_endpoint(struct usbd_pipe *pipe)
   1947 {
   1948 	struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
   1949 	struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
   1950 	const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
   1951 	struct xhci_soft_trb trb;
   1952 
   1953 	XHCIHIST_FUNC();
   1954 	XHCIHIST_CALLARGS("slot %ju dci %ju", xs->xs_idx, dci, 0, 0);
   1955 
   1956 	KASSERT(mutex_owned(&sc->sc_lock));
   1957 
   1958 	trb.trb_0 = 0;
   1959 	trb.trb_2 = 0;
   1960 	trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
   1961 	    XHCI_TRB_3_EP_SET(dci) |
   1962 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_EP);
   1963 
   1964 	if (xhci_do_command_locked(sc, &trb, USBD_DEFAULT_TIMEOUT)) {
   1965 		device_printf(sc->sc_dev, "%s: endpoint 0x%x: timed out\n",
   1966 		    __func__, pipe->up_endpoint->ue_edesc->bEndpointAddress);
   1967 	}
   1968 }
   1969 
   1970 /*
   1971  * 4.6.9, 6.4.3.8
   1972  * Stop execution of TDs on xfer ring.
   1973  * Should be called with sc_lock held.
   1974  */
   1975 static usbd_status
   1976 xhci_stop_endpoint_cmd(struct xhci_softc *sc, struct xhci_slot *xs, u_int dci,
   1977     uint32_t trb3flags)
   1978 {
   1979 	struct xhci_soft_trb trb;
   1980 	usbd_status err;
   1981 
   1982 	XHCIHIST_FUNC();
   1983 	XHCIHIST_CALLARGS("slot %ju dci %ju", xs->xs_idx, dci, 0, 0);
   1984 
   1985 	KASSERT(mutex_owned(&sc->sc_lock));
   1986 
   1987 	trb.trb_0 = 0;
   1988 	trb.trb_2 = 0;
   1989 	trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
   1990 	    XHCI_TRB_3_EP_SET(dci) |
   1991 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STOP_EP) |
   1992 	    trb3flags;
   1993 
   1994 	err = xhci_do_command_locked(sc, &trb, USBD_DEFAULT_TIMEOUT);
   1995 
   1996 	return err;
   1997 }
   1998 
   1999 static usbd_status
   2000 xhci_stop_endpoint(struct usbd_pipe *pipe)
   2001 {
   2002 	struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
   2003 	struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
   2004 	const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
   2005 
   2006 	XHCIHIST_FUNC();
   2007 	XHCIHIST_CALLARGS("slot %ju dci %ju", xs->xs_idx, dci, 0, 0);
   2008 
   2009 	KASSERT(mutex_owned(&sc->sc_lock));
   2010 
   2011 	return xhci_stop_endpoint_cmd(sc, xs, dci, 0);
   2012 }
   2013 
   2014 /*
   2015  * Set TR Dequeue Pointer.
   2016  * xHCI 1.1  4.6.10  6.4.3.9
   2017  * Purge all of the TRBs on ring and reinitialize ring.
   2018  * Set TR dequeue Pointer to 0 and Cycle State to 1.
   2019  * EPSTATE of endpoint must be ERROR or STOPPED, otherwise CONTEXT_STATE
   2020  * error will be generated.
   2021  */
   2022 static void
   2023 xhci_set_dequeue(struct usbd_pipe *pipe)
   2024 {
   2025 	struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
   2026 	struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
   2027 	const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
   2028 	struct xhci_ring * const xr = xs->xs_xr[dci];
   2029 	struct xhci_soft_trb trb;
   2030 
   2031 	XHCIHIST_FUNC();
   2032 	XHCIHIST_CALLARGS("slot %ju dci %ju", xs->xs_idx, dci, 0, 0);
   2033 
   2034 	KASSERT(mutex_owned(&sc->sc_lock));
   2035 	KASSERT(xr != NULL);
   2036 
   2037 	xhci_host_dequeue(xr);
   2038 
   2039 	/* set DCS */
   2040 	trb.trb_0 = xhci_ring_trbp(xr, 0) | 1; /* XXX */
   2041 	trb.trb_2 = 0;
   2042 	trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
   2043 	    XHCI_TRB_3_EP_SET(dci) |
   2044 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SET_TR_DEQUEUE);
   2045 
   2046 	if (xhci_do_command_locked(sc, &trb, USBD_DEFAULT_TIMEOUT)) {
   2047 		device_printf(sc->sc_dev, "%s: endpoint 0x%x: timed out\n",
   2048 		    __func__, pipe->up_endpoint->ue_edesc->bEndpointAddress);
   2049 	}
   2050 }
   2051 
   2052 /*
   2053  * Open new pipe: called from usbd_setup_pipe_flags.
   2054  * Fills methods of pipe.
   2055  * If pipe is not for ep0, calls configure_endpoint.
   2056  */
   2057 static usbd_status
   2058 xhci_open(struct usbd_pipe *pipe)
   2059 {
   2060 	struct usbd_device * const dev = pipe->up_dev;
   2061 	struct xhci_pipe * const xpipe = (struct xhci_pipe *)pipe;
   2062 	struct xhci_softc * const sc = XHCI_BUS2SC(dev->ud_bus);
   2063 	struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
   2064 	usb_endpoint_descriptor_t * const ed = pipe->up_endpoint->ue_edesc;
   2065 	const u_int dci = xhci_ep_get_dci(ed);
   2066 	const uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
   2067 	usbd_status err;
   2068 
   2069 	XHCIHIST_FUNC();
   2070 	XHCIHIST_CALLARGS("addr %jd depth %jd port %jd speed %jd", dev->ud_addr,
   2071 	    dev->ud_depth, dev->ud_powersrc->up_portno, dev->ud_speed);
   2072 	DPRINTFN(1, " dci %ju type 0x%02jx epaddr 0x%02jx attr 0x%02jx",
   2073 	    xhci_ep_get_dci(ed), ed->bDescriptorType, ed->bEndpointAddress,
   2074 	    ed->bmAttributes);
   2075 	DPRINTFN(1, " mps %ju ival %ju", UGETW(ed->wMaxPacketSize),
   2076 	    ed->bInterval, 0, 0);
   2077 
   2078 	if (sc->sc_dying)
   2079 		return USBD_IOERROR;
   2080 
   2081 	/* Root Hub */
   2082 	if (dev->ud_depth == 0 && dev->ud_powersrc->up_portno == 0) {
   2083 		switch (ed->bEndpointAddress) {
   2084 		case USB_CONTROL_ENDPOINT:
   2085 			pipe->up_methods = &roothub_ctrl_methods;
   2086 			break;
   2087 		case UE_DIR_IN | USBROOTHUB_INTR_ENDPT:
   2088 			pipe->up_methods = &xhci_root_intr_methods;
   2089 			break;
   2090 		default:
   2091 			pipe->up_methods = NULL;
   2092 			DPRINTFN(0, "bad bEndpointAddress 0x%02jx",
   2093 			    ed->bEndpointAddress, 0, 0, 0);
   2094 			return USBD_INVAL;
   2095 		}
   2096 		return USBD_NORMAL_COMPLETION;
   2097 	}
   2098 
   2099 	usb_init_task(&xpipe->xp_async_task, xhci_pipe_restart_async_task,
   2100 	    pipe, USB_TASKQ_MPSAFE);
   2101 
   2102 	switch (xfertype) {
   2103 	case UE_CONTROL:
   2104 		pipe->up_methods = &xhci_device_ctrl_methods;
   2105 		break;
   2106 	case UE_ISOCHRONOUS:
   2107 		pipe->up_methods = &xhci_device_isoc_methods;
   2108 		pipe->up_serialise = false;
   2109 		xpipe->xp_isoc_next = -1;
   2110 		break;
   2111 	case UE_BULK:
   2112 		pipe->up_methods = &xhci_device_bulk_methods;
   2113 		break;
   2114 	case UE_INTERRUPT:
   2115 		pipe->up_methods = &xhci_device_intr_methods;
   2116 		break;
   2117 	default:
   2118 		return USBD_IOERROR;
   2119 		break;
   2120 	}
   2121 
   2122 	KASSERT(xs != NULL);
   2123 	KASSERT(xs->xs_xr[dci] == NULL);
   2124 
   2125 	/* allocate transfer ring */
   2126 	err = xhci_ring_init(sc, &xs->xs_xr[dci], XHCI_TRANSFER_RING_TRBS,
   2127 	    XHCI_TRB_ALIGN);
   2128 	if (err) {
   2129 		DPRINTFN(1, "ring alloc failed %jd", err, 0, 0, 0);
   2130 		return err;
   2131 	}
   2132 
   2133 	if (ed->bEndpointAddress != USB_CONTROL_ENDPOINT)
   2134 		return xhci_configure_endpoint(pipe);
   2135 
   2136 	return USBD_NORMAL_COMPLETION;
   2137 }
   2138 
   2139 /*
   2140  * Closes pipe, called from usbd_kill_pipe via close methods.
   2141  * If the endpoint to be closed is ep0, disable_slot.
   2142  * Should be called with sc_lock held.
   2143  */
   2144 static void
   2145 xhci_close_pipe(struct usbd_pipe *pipe)
   2146 {
   2147 	struct xhci_pipe * const xp =
   2148 	    container_of(pipe, struct xhci_pipe, xp_pipe);
   2149 	struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
   2150 	struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
   2151 	usb_endpoint_descriptor_t * const ed = pipe->up_endpoint->ue_edesc;
   2152 	const u_int dci = xhci_ep_get_dci(ed);
   2153 	struct xhci_soft_trb trb;
   2154 	uint32_t *cp;
   2155 
   2156 	XHCIHIST_FUNC();
   2157 
   2158 	usb_rem_task_wait(pipe->up_dev, &xp->xp_async_task, USB_TASKQ_HC,
   2159 	    &sc->sc_lock);
   2160 
   2161 	if (sc->sc_dying)
   2162 		return;
   2163 
   2164 	/* xs is uninitialized before xhci_init_slot */
   2165 	if (xs == NULL || xs->xs_idx == 0)
   2166 		return;
   2167 
   2168 	XHCIHIST_CALLARGS("pipe %#jx slot %ju dci %ju",
   2169 	    (uintptr_t)pipe, xs->xs_idx, dci, 0);
   2170 
   2171 	KASSERTMSG(!cpu_intr_p() && !cpu_softintr_p(), "called from intr ctx");
   2172 	KASSERT(mutex_owned(&sc->sc_lock));
   2173 
   2174 	if (pipe->up_dev->ud_depth == 0)
   2175 		return;
   2176 
   2177 	if (dci == XHCI_DCI_EP_CONTROL) {
   2178 		DPRINTFN(4, "closing ep0", 0, 0, 0, 0);
   2179 		/* This frees all rings */
   2180 		xhci_disable_slot(sc, xs->xs_idx);
   2181 		return;
   2182 	}
   2183 
   2184 	if (xhci_get_epstate(sc, xs, dci) != XHCI_EPSTATE_STOPPED)
   2185 		(void)xhci_stop_endpoint(pipe);
   2186 
   2187 	/*
   2188 	 * set appropriate bit to be dropped.
   2189 	 * don't set DC bit to 1, otherwise all endpoints
   2190 	 * would be deconfigured.
   2191 	 */
   2192 	cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL);
   2193 	cp[0] = htole32(XHCI_INCTX_0_DROP_MASK(dci));
   2194 	cp[1] = htole32(0);
   2195 
   2196 	/* XXX should be most significant one, not dci? */
   2197 	cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_SLOT));
   2198 	cp[0] = htole32(XHCI_SCTX_0_CTX_NUM_SET(dci));
   2199 
   2200 	/* configure ep context performs an implicit dequeue */
   2201 	xhci_host_dequeue(xs->xs_xr[dci]);
   2202 
   2203 	/* sync input contexts before they are read from memory */
   2204 	usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE);
   2205 
   2206 	trb.trb_0 = xhci_slot_get_icp(sc, xs, 0);
   2207 	trb.trb_2 = 0;
   2208 	trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
   2209 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_CONFIGURE_EP);
   2210 
   2211 	(void)xhci_do_command_locked(sc, &trb, USBD_DEFAULT_TIMEOUT);
   2212 	usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
   2213 
   2214 	xhci_ring_free(sc, &xs->xs_xr[dci]);
   2215 	xs->xs_xr[dci] = NULL;
   2216 }
   2217 
   2218 /*
   2219  * Abort transfer.  Must be called with sc_lock held.  Releases and
   2220  * reacquires sc_lock to sleep until hardware acknowledges abort.
   2221  */
   2222 static void
   2223 xhci_abortx(struct usbd_xfer *xfer)
   2224 {
   2225 	XHCIHIST_FUNC();
   2226 	struct xhci_softc * const sc = XHCI_XFER2SC(xfer);
   2227 
   2228 	XHCIHIST_CALLARGS("xfer %#jx pipe %#jx",
   2229 	    (uintptr_t)xfer, (uintptr_t)xfer->ux_pipe, 0, 0);
   2230 
   2231 	KASSERT(mutex_owned(&sc->sc_lock));
   2232 	KASSERTMSG((xfer->ux_status == USBD_CANCELLED ||
   2233 		xfer->ux_status == USBD_TIMEOUT),
   2234 	    "bad abort status: %d", xfer->ux_status);
   2235 
   2236 	xhci_pipe_restart(xfer->ux_pipe);
   2237 
   2238 	DPRINTFN(14, "end", 0, 0, 0, 0);
   2239 }
   2240 
   2241 static void
   2242 xhci_host_dequeue(struct xhci_ring * const xr)
   2243 {
   2244 	/* When dequeueing the controller, update our struct copy too */
   2245 	memset(xr->xr_trb, 0, xr->xr_ntrb * XHCI_TRB_SIZE);
   2246 	usb_syncmem(&xr->xr_dma, 0, xr->xr_ntrb * XHCI_TRB_SIZE,
   2247 	    BUS_DMASYNC_PREWRITE);
   2248 	memset(xr->xr_cookies, 0, xr->xr_ntrb * sizeof(*xr->xr_cookies));
   2249 
   2250 	xr->xr_ep = 0;
   2251 	xr->xr_cs = 1;
   2252 }
   2253 
   2254 /*
   2255  * Recover STALLed endpoint, or stop endpoint to abort a pipe.
   2256  * xHCI 1.1 sect 4.10.2.1
   2257  * Issue RESET_EP to recover halt condition and SET_TR_DEQUEUE to remove
   2258  * all transfers on transfer ring.
   2259  */
   2260 static void
   2261 xhci_pipe_restart(struct usbd_pipe *pipe)
   2262 {
   2263 	struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
   2264 	struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
   2265 	const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
   2266 
   2267 	XHCIHIST_FUNC();
   2268 	XHCIHIST_CALLARGS("pipe %#jx slot %ju dci %ju",
   2269 	    (uintptr_t)pipe, xs->xs_idx, dci, 0);
   2270 
   2271 	KASSERT(xhci_polling_p(sc) || mutex_owned(&sc->sc_lock));
   2272 
   2273 	/*
   2274 	 * - If the endpoint is halted, indicating a stall, reset it.
   2275 	 * - If the endpoint is stopped, we're already good.
   2276 	 * - Otherwise, someone wanted to abort the pipe, so stop the
   2277 	 *   endpoint.
   2278 	 *
   2279 	 * In any case, clear the ring.
   2280 	 */
   2281 	switch (xhci_get_epstate(sc, xs, dci)) {
   2282 	case XHCI_EPSTATE_HALTED:
   2283 		xhci_reset_endpoint(pipe);
   2284 		break;
   2285 	case XHCI_EPSTATE_STOPPED:
   2286 		break;
   2287 	default:
   2288 		xhci_stop_endpoint(pipe);
   2289 		break;
   2290 	}
   2291 
   2292 	switch (xhci_get_epstate(sc, xs, dci)) {
   2293 	case XHCI_EPSTATE_STOPPED:
   2294 		break;
   2295 	case XHCI_EPSTATE_ERROR:
   2296 		device_printf(sc->sc_dev, "endpoint 0x%x error\n",
   2297 		    pipe->up_endpoint->ue_edesc->bEndpointAddress);
   2298 		break;
   2299 	default:
   2300 		device_printf(sc->sc_dev, "endpoint 0x%x failed to stop\n",
   2301 		    pipe->up_endpoint->ue_edesc->bEndpointAddress);
   2302 	}
   2303 
   2304 	xhci_set_dequeue(pipe);
   2305 
   2306 	DPRINTFN(4, "ends", 0, 0, 0, 0);
   2307 }
   2308 
   2309 static void
   2310 xhci_pipe_restart_async_task(void *cookie)
   2311 {
   2312 	struct usbd_pipe * const pipe = cookie;
   2313 	struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
   2314 	struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
   2315 	const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
   2316 	struct xhci_ring * const tr = xs->xs_xr[dci];
   2317 	struct usbd_xfer *xfer;
   2318 
   2319 	XHCIHIST_FUNC();
   2320 	XHCIHIST_CALLARGS("sc=%#jx pipe=%#jx",
   2321 	    (uintptr_t)sc, (uintptr_t)pipe, 0, 0);
   2322 
   2323 	mutex_enter(&sc->sc_lock);
   2324 
   2325 	xhci_pipe_restart(pipe);
   2326 
   2327 	/*
   2328 	 * We halted our own queue because it stalled.  Mark it no
   2329 	 * longer halted and start issuing queued transfers again.
   2330 	 */
   2331 	tr->is_halted = false;
   2332 	xfer = SIMPLEQ_FIRST(&pipe->up_queue);
   2333 	if (xfer) {
   2334 		/*
   2335 		 * If the first xfer of the queue is not in progress,
   2336 		 * though, there may be a concurrent software abort
   2337 		 * that has already cancelled it and is now in the
   2338 		 * middle of a concurrent xhci_pipe_restart waiting to
   2339 		 * reacquire the pipe (bus) lock.  So only restart the
   2340 		 * xfer if it's still USBD_IN_PROGRESS.
   2341 		 *
   2342 		 * Either way, xfers on the queue can't be in
   2343 		 * USBD_NOT_STARTED.
   2344 		 */
   2345 		KASSERT(xfer->ux_status != USBD_NOT_STARTED);
   2346 		if (xfer->ux_status == USBD_IN_PROGRESS) {
   2347 			if (pipe->up_methods->upm_start != NULL)
   2348 				(*pipe->up_methods->upm_start)(xfer);
   2349 		} else {
   2350 			DPRINTF("pipe restart race xfer=%#jx status=%jd",
   2351 			    (uintptr_t)xfer, xfer->ux_status, 0, 0);
   2352 		}
   2353 	}
   2354 
   2355 	mutex_exit(&sc->sc_lock);
   2356 }
   2357 
   2358 static void
   2359 xhci_pipe_restart_async(struct usbd_pipe *pipe)
   2360 {
   2361 	struct xhci_pipe * const xp =
   2362 	    container_of(pipe, struct xhci_pipe, xp_pipe);
   2363 	struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
   2364 	struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
   2365 	const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
   2366 	struct xhci_ring * const tr = xs->xs_xr[dci];
   2367 
   2368 	XHCIHIST_FUNC();
   2369 	XHCIHIST_CALLARGS("pipe %#jx", (uintptr_t)pipe, 0, 0, 0);
   2370 
   2371 	KASSERT(xhci_polling_p(sc) || mutex_owned(&sc->sc_lock));
   2372 
   2373 	tr->is_halted = true;
   2374 	usb_add_task(pipe->up_dev, &xp->xp_async_task, USB_TASKQ_HC);
   2375 
   2376 	DPRINTFN(4, "ends", 0, 0, 0, 0);
   2377 }
   2378 
   2379 /* Process roothub port status/change events and notify to uhub_intr. */
   2380 static void
   2381 xhci_rhpsc(struct xhci_softc * const sc, u_int ctlrport)
   2382 {
   2383 	XHCIHIST_FUNC();
   2384 	XHCIHIST_CALLARGS("xhci%jd: port %ju status change",
   2385 	   device_unit(sc->sc_dev), ctlrport, 0, 0);
   2386 
   2387 	if (ctlrport > sc->sc_maxports)
   2388 		return;
   2389 
   2390 	const size_t bn = xhci_ctlrport2bus(sc, ctlrport);
   2391 	const size_t rhp = xhci_ctlrport2rhport(sc, ctlrport);
   2392 	struct usbd_xfer * const xfer = sc->sc_intrxfer[bn];
   2393 
   2394 	DPRINTFN(4, "xhci%jd: bus %jd bp %ju xfer %#jx status change",
   2395 	    device_unit(sc->sc_dev), bn, rhp, (uintptr_t)xfer);
   2396 
   2397 	if (xfer == NULL)
   2398 		return;
   2399 	KASSERT(xfer->ux_status == USBD_IN_PROGRESS);
   2400 
   2401 	uint8_t *p = xfer->ux_buf;
   2402 	if (!xhci_polling_p(sc) || !sc->sc_intrxfer_deferred[bn])
   2403 		memset(p, 0, xfer->ux_length);
   2404 	p[rhp / NBBY] |= 1 << (rhp % NBBY);
   2405 	xfer->ux_actlen = xfer->ux_length;
   2406 	xfer->ux_status = USBD_NORMAL_COMPLETION;
   2407 	if (xhci_polling_p(sc))
   2408 		sc->sc_intrxfer_deferred[bn] = true;
   2409 	else
   2410 		usb_transfer_complete(xfer);
   2411 }
   2412 
   2413 /* Process Transfer Events */
   2414 static void
   2415 xhci_event_transfer(struct xhci_softc * const sc,
   2416     const struct xhci_trb * const trb)
   2417 {
   2418 	uint64_t trb_0;
   2419 	uint32_t trb_2, trb_3;
   2420 	uint8_t trbcode;
   2421 	u_int slot, dci;
   2422 	struct xhci_slot *xs;
   2423 	struct xhci_ring *xr;
   2424 	struct xhci_xfer *xx;
   2425 	struct usbd_xfer *xfer;
   2426 	usbd_status err;
   2427 
   2428 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2429 
   2430 	trb_0 = le64toh(trb->trb_0);
   2431 	trb_2 = le32toh(trb->trb_2);
   2432 	trb_3 = le32toh(trb->trb_3);
   2433 	trbcode = XHCI_TRB_2_ERROR_GET(trb_2);
   2434 	slot = XHCI_TRB_3_SLOT_GET(trb_3);
   2435 	dci = XHCI_TRB_3_EP_GET(trb_3);
   2436 	xs = &sc->sc_slots[slot];
   2437 	xr = xs->xs_xr[dci];
   2438 
   2439 	/* sanity check */
   2440 	KASSERT(xr != NULL);
   2441 	KASSERTMSG(xs->xs_idx != 0 && xs->xs_idx <= sc->sc_maxslots,
   2442 	    "invalid xs_idx %u slot %u", xs->xs_idx, slot);
   2443 
   2444 	int idx = 0;
   2445 	if ((trb_3 & XHCI_TRB_3_ED_BIT) == 0) {
   2446 		if (xhci_trb_get_idx(xr, trb_0, &idx)) {
   2447 			DPRINTFN(0, "invalid trb_0 %#jx", trb_0, 0, 0, 0);
   2448 			return;
   2449 		}
   2450 		xx = xr->xr_cookies[idx];
   2451 
   2452 		/* clear cookie of consumed TRB */
   2453 		xr->xr_cookies[idx] = NULL;
   2454 
   2455 		/*
   2456 		 * xx is NULL if pipe is opened but xfer is not started.
   2457 		 * It happens when stopping idle pipe.
   2458 		 */
   2459 		if (xx == NULL || trbcode == XHCI_TRB_ERROR_LENGTH) {
   2460 			DPRINTFN(1, "Ignore #%ju: cookie %#jx cc %ju dci %ju",
   2461 			    idx, (uintptr_t)xx, trbcode, dci);
   2462 			DPRINTFN(1, " orig TRB %#jx type %ju", trb_0,
   2463 			    XHCI_TRB_3_TYPE_GET(le32toh(xr->xr_trb[idx].trb_3)),
   2464 			    0, 0);
   2465 			return;
   2466 		}
   2467 	} else {
   2468 		/* When ED != 0, trb_0 is virtual addr of struct xhci_xfer. */
   2469 		xx = (void *)(uintptr_t)(trb_0 & ~0x3);
   2470 	}
   2471 	/* XXX this may not happen */
   2472 	if (xx == NULL) {
   2473 		DPRINTFN(1, "xfer done: xx is NULL", 0, 0, 0, 0);
   2474 		return;
   2475 	}
   2476 	xfer = &xx->xx_xfer;
   2477 	/* XXX this may happen when detaching */
   2478 	if (xfer == NULL) {
   2479 		DPRINTFN(1, "xx(%#jx)->xx_xfer is NULL trb_0 %#jx",
   2480 		    (uintptr_t)xx, trb_0, 0, 0);
   2481 		return;
   2482 	}
   2483 	DPRINTFN(14, "xfer %#jx", (uintptr_t)xfer, 0, 0, 0);
   2484 	/* XXX I dunno why this happens */
   2485 	KASSERTMSG(xfer->ux_pipe != NULL, "xfer(%p)->ux_pipe is NULL", xfer);
   2486 
   2487 	if (!xfer->ux_pipe->up_repeat &&
   2488 	    SIMPLEQ_EMPTY(&xfer->ux_pipe->up_queue)) {
   2489 		DPRINTFN(1, "xfer(%#jx)->pipe not queued", (uintptr_t)xfer,
   2490 		    0, 0, 0);
   2491 		return;
   2492 	}
   2493 
   2494 	const uint8_t xfertype =
   2495 	    UE_GET_XFERTYPE(xfer->ux_pipe->up_endpoint->ue_edesc->bmAttributes);
   2496 
   2497 	/* 4.11.5.2 Event Data TRB */
   2498 	if ((trb_3 & XHCI_TRB_3_ED_BIT) != 0) {
   2499 		DPRINTFN(14, "transfer Event Data: 0x%016jx 0x%08jx"
   2500 		    " %02jx", trb_0, XHCI_TRB_2_REM_GET(trb_2), trbcode, 0);
   2501 		if ((trb_0 & 0x3) == 0x3) {
   2502 			xfer->ux_actlen = XHCI_TRB_2_REM_GET(trb_2);
   2503 		}
   2504 	}
   2505 
   2506 	switch (trbcode) {
   2507 	case XHCI_TRB_ERROR_SHORT_PKT:
   2508 	case XHCI_TRB_ERROR_SUCCESS:
   2509 		/*
   2510 		 * A ctrl transfer can generate two events if it has a Data
   2511 		 * stage.  A short data stage can be OK and should not
   2512 		 * complete the transfer as the status stage needs to be
   2513 		 * performed.
   2514 		 *
   2515 		 * Note: Data and Status stage events point at same xfer.
   2516 		 * ux_actlen and ux_dmabuf will be passed to
   2517 		 * usb_transfer_complete after the Status stage event.
   2518 		 *
   2519 		 * It can be distinguished which stage generates the event:
   2520 		 * + by checking least 3 bits of trb_0 if ED==1.
   2521 		 *   (see xhci_device_ctrl_start).
   2522 		 * + by checking the type of original TRB if ED==0.
   2523 		 *
   2524 		 * In addition, intr, bulk, and isoc transfer currently
   2525 		 * consists of single TD, so the "skip" is not needed.
   2526 		 * ctrl xfer uses EVENT_DATA, and others do not.
   2527 		 * Thus driver can switch the flow by checking ED bit.
   2528 		 */
   2529 		if (xfertype == UE_ISOCHRONOUS) {
   2530 			xfer->ux_frlengths[xx->xx_isoc_done] -=
   2531 			    XHCI_TRB_2_REM_GET(trb_2);
   2532 			xfer->ux_actlen += xfer->ux_frlengths[xx->xx_isoc_done];
   2533 		} else if ((trb_3 & XHCI_TRB_3_ED_BIT) == 0) {
   2534 			if (xfer->ux_actlen == 0)
   2535 				xfer->ux_actlen = xfer->ux_length -
   2536 				    XHCI_TRB_2_REM_GET(trb_2);
   2537 			if (XHCI_TRB_3_TYPE_GET(le32toh(xr->xr_trb[idx].trb_3))
   2538 			    == XHCI_TRB_TYPE_DATA_STAGE) {
   2539 				return;
   2540 			}
   2541 		} else if ((trb_0 & 0x3) == 0x3) {
   2542 			return;
   2543 		}
   2544 		err = USBD_NORMAL_COMPLETION;
   2545 		break;
   2546 	case XHCI_TRB_ERROR_STOPPED:
   2547 	case XHCI_TRB_ERROR_LENGTH:
   2548 	case XHCI_TRB_ERROR_STOPPED_SHORT:
   2549 		err = USBD_IOERROR;
   2550 		break;
   2551 	case XHCI_TRB_ERROR_STALL:
   2552 	case XHCI_TRB_ERROR_BABBLE:
   2553 		DPRINTFN(1, "ERR %ju slot %ju dci %ju", trbcode, slot, dci, 0);
   2554 		xhci_pipe_restart_async(xfer->ux_pipe);
   2555 		err = USBD_STALLED;
   2556 		break;
   2557 	default:
   2558 		DPRINTFN(1, "ERR %ju slot %ju dci %ju", trbcode, slot, dci, 0);
   2559 		err = USBD_IOERROR;
   2560 		break;
   2561 	}
   2562 
   2563 	if (xfertype == UE_ISOCHRONOUS) {
   2564 		switch (trbcode) {
   2565 		case XHCI_TRB_ERROR_SHORT_PKT:
   2566 		case XHCI_TRB_ERROR_SUCCESS:
   2567 			break;
   2568 		case XHCI_TRB_ERROR_MISSED_SERVICE:
   2569 		case XHCI_TRB_ERROR_RING_UNDERRUN:
   2570 		case XHCI_TRB_ERROR_RING_OVERRUN:
   2571 		default:
   2572 			xfer->ux_frlengths[xx->xx_isoc_done] = 0;
   2573 			break;
   2574 		}
   2575 		if (++xx->xx_isoc_done < xfer->ux_nframes)
   2576 			return;
   2577 	}
   2578 
   2579 	/*
   2580 	 * If next event will be from zero-length packet,
   2581 	 * suppress notification of first event.
   2582 	 */
   2583 	if (xfertype == UE_BULK &&
   2584 	    err == USBD_NORMAL_COMPLETION &&
   2585 	    (xfer->ux_flags & USBD_FORCE_SHORT_XFER) &&
   2586 	    XHCI_TRB_2_REM_GET(le32toh(xr->xr_trb[idx].trb_2)) != 0) {
   2587 		DPRINTFN(100, "short xfer %#jx: suppress notification status "
   2588 		    "%ju pipe %#jx", (uintptr_t)xfer, xfer->ux_status,
   2589 		    (uintptr_t)xfer->ux_pipe, 0);
   2590 		return;
   2591 	}
   2592 
   2593 	if ((trb_3 & XHCI_TRB_3_ED_BIT) == 0 ||
   2594 	    (trb_0 & 0x3) == 0x0) {
   2595 		/*
   2596 		 * Try to claim this xfer for completion.  If it has
   2597 		 * already completed or aborted, drop it on the floor.
   2598 		 */
   2599 		if (!usbd_xfer_trycomplete(xfer))
   2600 			return;
   2601 
   2602 		/* Set the status.  */
   2603 		xfer->ux_status = err;
   2604 
   2605 		usb_transfer_complete(xfer);
   2606 	}
   2607 }
   2608 
   2609 /* Process Command complete events */
   2610 static void
   2611 xhci_event_cmd(struct xhci_softc * const sc, const struct xhci_trb * const trb)
   2612 {
   2613 	uint64_t trb_0;
   2614 	uint32_t trb_2, trb_3;
   2615 
   2616 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2617 
   2618 	KASSERT(xhci_polling_p(sc) || mutex_owned(&sc->sc_lock));
   2619 
   2620 	trb_0 = le64toh(trb->trb_0);
   2621 	trb_2 = le32toh(trb->trb_2);
   2622 	trb_3 = le32toh(trb->trb_3);
   2623 
   2624 	if (trb_0 == sc->sc_command_addr) {
   2625 		sc->sc_resultpending = false;
   2626 
   2627 		sc->sc_result_trb.trb_0 = trb_0;
   2628 		sc->sc_result_trb.trb_2 = trb_2;
   2629 		sc->sc_result_trb.trb_3 = trb_3;
   2630 		if (XHCI_TRB_2_ERROR_GET(trb_2) !=
   2631 		    XHCI_TRB_ERROR_SUCCESS) {
   2632 			DPRINTFN(1, "command completion "
   2633 			    "failure: 0x%016jx 0x%08jx 0x%08jx",
   2634 			    trb_0, trb_2, trb_3, 0);
   2635 		}
   2636 		cv_signal(&sc->sc_command_cv);
   2637 	} else {
   2638 		DPRINTFN(1, "spurious event: %#jx 0x%016jx "
   2639 		    "0x%08jx 0x%08jx", (uintptr_t)trb, trb_0, trb_2, trb_3);
   2640 	}
   2641 }
   2642 
   2643 /*
   2644  * Process events.
   2645  * called from xhci_softintr
   2646  */
   2647 static void
   2648 xhci_handle_event(struct xhci_softc * const sc,
   2649     const struct xhci_trb * const trb)
   2650 {
   2651 	uint64_t trb_0;
   2652 	uint32_t trb_2, trb_3;
   2653 
   2654 	XHCIHIST_FUNC();
   2655 
   2656 	trb_0 = le64toh(trb->trb_0);
   2657 	trb_2 = le32toh(trb->trb_2);
   2658 	trb_3 = le32toh(trb->trb_3);
   2659 
   2660 	XHCIHIST_CALLARGS("event: %#jx 0x%016jx 0x%08jx 0x%08jx",
   2661 	    (uintptr_t)trb, trb_0, trb_2, trb_3);
   2662 
   2663 	/*
   2664 	 * 4.11.3.1, 6.4.2.1
   2665 	 * TRB Pointer is invalid for these completion codes.
   2666 	 */
   2667 	switch (XHCI_TRB_2_ERROR_GET(trb_2)) {
   2668 	case XHCI_TRB_ERROR_RING_UNDERRUN:
   2669 	case XHCI_TRB_ERROR_RING_OVERRUN:
   2670 	case XHCI_TRB_ERROR_VF_RING_FULL:
   2671 		return;
   2672 	default:
   2673 		if (trb_0 == 0) {
   2674 			return;
   2675 		}
   2676 		break;
   2677 	}
   2678 
   2679 	switch (XHCI_TRB_3_TYPE_GET(trb_3)) {
   2680 	case XHCI_TRB_EVENT_TRANSFER:
   2681 		xhci_event_transfer(sc, trb);
   2682 		break;
   2683 	case XHCI_TRB_EVENT_CMD_COMPLETE:
   2684 		xhci_event_cmd(sc, trb);
   2685 		break;
   2686 	case XHCI_TRB_EVENT_PORT_STS_CHANGE:
   2687 		xhci_rhpsc(sc, (uint32_t)((trb_0 >> 24) & 0xff));
   2688 		break;
   2689 	default:
   2690 		break;
   2691 	}
   2692 }
   2693 
   2694 static void
   2695 xhci_softintr(void *v)
   2696 {
   2697 	struct usbd_bus * const bus = v;
   2698 	struct xhci_softc * const sc = XHCI_BUS2SC(bus);
   2699 	struct xhci_ring * const er = sc->sc_er;
   2700 	struct xhci_trb *trb;
   2701 	int i, j, k, bn;
   2702 
   2703 	XHCIHIST_FUNC();
   2704 
   2705 	KASSERT(xhci_polling_p(sc) || mutex_owned(&sc->sc_lock));
   2706 
   2707 	i = er->xr_ep;
   2708 	j = er->xr_cs;
   2709 
   2710 	XHCIHIST_CALLARGS("er: xr_ep %jd xr_cs %jd", i, j, 0, 0);
   2711 
   2712 	/*
   2713 	 * Handle deferred root intr xfer, in case we just switched off
   2714 	 * polling.  It's not safe to complete root intr xfers while
   2715 	 * polling -- too much kernel machinery gets involved.
   2716 	 */
   2717 	if (!xhci_polling_p(sc)) {
   2718 		for (bn = 0; bn < 2; bn++) {
   2719 			if (__predict_false(sc->sc_intrxfer_deferred[bn])) {
   2720 				sc->sc_intrxfer_deferred[bn] = false;
   2721 				usb_transfer_complete(sc->sc_intrxfer[bn]);
   2722 			}
   2723 		}
   2724 	}
   2725 
   2726 	while (1) {
   2727 		usb_syncmem(&er->xr_dma, XHCI_TRB_SIZE * i, XHCI_TRB_SIZE,
   2728 		    BUS_DMASYNC_POSTREAD);
   2729 		trb = &er->xr_trb[i];
   2730 		k = (le32toh(trb->trb_3) & XHCI_TRB_3_CYCLE_BIT) ? 1 : 0;
   2731 
   2732 		if (j != k)
   2733 			break;
   2734 
   2735 		xhci_handle_event(sc, trb);
   2736 
   2737 		i++;
   2738 		if (i == er->xr_ntrb) {
   2739 			i = 0;
   2740 			j ^= 1;
   2741 		}
   2742 	}
   2743 
   2744 	er->xr_ep = i;
   2745 	er->xr_cs = j;
   2746 
   2747 	xhci_rt_write_8(sc, XHCI_ERDP(0), xhci_ring_trbp(er, er->xr_ep) |
   2748 	    XHCI_ERDP_BUSY);
   2749 
   2750 	DPRINTFN(16, "ends", 0, 0, 0, 0);
   2751 
   2752 	return;
   2753 }
   2754 
   2755 static void
   2756 xhci_poll(struct usbd_bus *bus)
   2757 {
   2758 	struct xhci_softc * const sc = XHCI_BUS2SC(bus);
   2759 
   2760 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2761 
   2762 	mutex_enter(&sc->sc_intr_lock);
   2763 	int ret = xhci_intr1(sc);
   2764 	if (ret) {
   2765 		xhci_softintr(bus);
   2766 	}
   2767 	mutex_exit(&sc->sc_intr_lock);
   2768 
   2769 	return;
   2770 }
   2771 
   2772 static struct usbd_xfer *
   2773 xhci_allocx(struct usbd_bus *bus, unsigned int nframes)
   2774 {
   2775 	struct xhci_softc * const sc = XHCI_BUS2SC(bus);
   2776 	struct xhci_xfer *xx;
   2777 	u_int ntrbs;
   2778 
   2779 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2780 
   2781 	ntrbs = uimax(3, nframes);
   2782 	const size_t trbsz = sizeof(*xx->xx_trb) * ntrbs;
   2783 
   2784 	xx = pool_cache_get(sc->sc_xferpool, PR_WAITOK);
   2785 	if (xx != NULL) {
   2786 		memset(xx, 0, sizeof(*xx));
   2787 		if (ntrbs > 0) {
   2788 			xx->xx_trb = kmem_alloc(trbsz, KM_SLEEP);
   2789 			xx->xx_ntrb = ntrbs;
   2790 		}
   2791 #ifdef DIAGNOSTIC
   2792 		xx->xx_xfer.ux_state = XFER_BUSY;
   2793 #endif
   2794 	}
   2795 
   2796 	return &xx->xx_xfer;
   2797 }
   2798 
   2799 static void
   2800 xhci_freex(struct usbd_bus *bus, struct usbd_xfer *xfer)
   2801 {
   2802 	struct xhci_softc * const sc = XHCI_BUS2SC(bus);
   2803 	struct xhci_xfer * const xx = XHCI_XFER2XXFER(xfer);
   2804 
   2805 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2806 
   2807 #ifdef DIAGNOSTIC
   2808 	if (xfer->ux_state != XFER_BUSY &&
   2809 	    xfer->ux_status != USBD_NOT_STARTED) {
   2810 		DPRINTFN(0, "xfer=%#jx not busy, 0x%08jx",
   2811 		    (uintptr_t)xfer, xfer->ux_state, 0, 0);
   2812 	}
   2813 	xfer->ux_state = XFER_FREE;
   2814 #endif
   2815 	if (xx->xx_ntrb > 0) {
   2816 		kmem_free(xx->xx_trb, xx->xx_ntrb * sizeof(*xx->xx_trb));
   2817 		xx->xx_trb = NULL;
   2818 		xx->xx_ntrb = 0;
   2819 	}
   2820 	pool_cache_put(sc->sc_xferpool, xx);
   2821 }
   2822 
   2823 static bool
   2824 xhci_dying(struct usbd_bus *bus)
   2825 {
   2826 	struct xhci_softc * const sc = XHCI_BUS2SC(bus);
   2827 
   2828 	return sc->sc_dying;
   2829 }
   2830 
   2831 static void
   2832 xhci_get_lock(struct usbd_bus *bus, kmutex_t **lock)
   2833 {
   2834 	struct xhci_softc * const sc = XHCI_BUS2SC(bus);
   2835 
   2836 	*lock = &sc->sc_lock;
   2837 }
   2838 
   2839 extern uint32_t usb_cookie_no;
   2840 
   2841 /*
   2842  * xHCI 4.3
   2843  * Called when uhub_explore finds a new device (via usbd_new_device).
   2844  * Port initialization and speed detection (4.3.1) are already done in uhub.c.
   2845  * This function does:
   2846  *   Allocate and construct dev structure of default endpoint (ep0).
   2847  *   Allocate and open pipe of ep0.
   2848  *   Enable slot and initialize slot context.
   2849  *   Set Address.
   2850  *   Read initial device descriptor.
   2851  *   Determine initial MaxPacketSize (mps) by speed.
   2852  *   Read full device descriptor.
   2853  *   Register this device.
   2854  * Finally state of device transitions ADDRESSED.
   2855  */
   2856 static usbd_status
   2857 xhci_new_device(device_t parent, struct usbd_bus *bus, int depth,
   2858     int speed, int port, struct usbd_port *up)
   2859 {
   2860 	struct xhci_softc * const sc = XHCI_BUS2SC(bus);
   2861 	struct usbd_device *dev;
   2862 	usbd_status err;
   2863 	usb_device_descriptor_t *dd;
   2864 	struct xhci_slot *xs;
   2865 	uint32_t *cp;
   2866 
   2867 	XHCIHIST_FUNC();
   2868 	XHCIHIST_CALLARGS("port %ju depth %ju speed %ju up %#jx",
   2869 	    port, depth, speed, (uintptr_t)up);
   2870 
   2871 	KASSERT(KERNEL_LOCKED_P());
   2872 
   2873 	dev = kmem_zalloc(sizeof(*dev), KM_SLEEP);
   2874 	dev->ud_bus = bus;
   2875 	dev->ud_quirks = &usbd_no_quirk;
   2876 	dev->ud_addr = 0;
   2877 	dev->ud_ddesc.bMaxPacketSize = 0;
   2878 	dev->ud_config = USB_UNCONFIG_NO;
   2879 	dev->ud_configidx = USB_UNCONFIG_INDEX;
   2880 	dev->ud_depth = depth;
   2881 	dev->ud_powersrc = up;
   2882 	dev->ud_myhub = up->up_parent;
   2883 	dev->ud_speed = speed;
   2884 	dev->ud_langid = USBD_NOLANG;
   2885 	dev->ud_cookie.cookie = ++usb_cookie_no;
   2886 
   2887 	/* Set up default endpoint handle. */
   2888 	dev->ud_ep0.ue_edesc = &dev->ud_ep0desc;
   2889 	/* doesn't matter, just don't let it uninitialized */
   2890 	dev->ud_ep0.ue_toggle = 0;
   2891 
   2892 	/* Set up default endpoint descriptor. */
   2893 	dev->ud_ep0desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
   2894 	dev->ud_ep0desc.bDescriptorType = UDESC_ENDPOINT;
   2895 	dev->ud_ep0desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
   2896 	dev->ud_ep0desc.bmAttributes = UE_CONTROL;
   2897 	dev->ud_ep0desc.bInterval = 0;
   2898 
   2899 	/* 4.3,  4.8.2.1 */
   2900 	switch (speed) {
   2901 	case USB_SPEED_SUPER:
   2902 	case USB_SPEED_SUPER_PLUS:
   2903 		USETW(dev->ud_ep0desc.wMaxPacketSize, USB_3_MAX_CTRL_PACKET);
   2904 		break;
   2905 	case USB_SPEED_FULL:
   2906 		/* XXX using 64 as initial mps of ep0 in FS */
   2907 	case USB_SPEED_HIGH:
   2908 		USETW(dev->ud_ep0desc.wMaxPacketSize, USB_2_MAX_CTRL_PACKET);
   2909 		break;
   2910 	case USB_SPEED_LOW:
   2911 	default:
   2912 		USETW(dev->ud_ep0desc.wMaxPacketSize, USB_MAX_IPACKET);
   2913 		break;
   2914 	}
   2915 
   2916 	up->up_dev = dev;
   2917 
   2918 	dd = &dev->ud_ddesc;
   2919 
   2920 	if (depth == 0 && port == 0) {
   2921 		KASSERT(bus->ub_devices[USB_ROOTHUB_INDEX] == NULL);
   2922 		bus->ub_devices[USB_ROOTHUB_INDEX] = dev;
   2923 
   2924 		/* Establish the default pipe. */
   2925 		err = usbd_setup_pipe(dev, 0, &dev->ud_ep0,
   2926 		    USBD_DEFAULT_INTERVAL, &dev->ud_pipe0);
   2927 		if (err) {
   2928 			DPRINTFN(1, "setup default pipe failed %jd", err,0,0,0);
   2929 			goto bad;
   2930 		}
   2931 		err = usbd_get_initial_ddesc(dev, dd);
   2932 		if (err) {
   2933 			DPRINTFN(1, "get_initial_ddesc %ju", err, 0, 0, 0);
   2934 			goto bad;
   2935 		}
   2936 	} else {
   2937 		uint8_t slot = 0;
   2938 
   2939 		/* 4.3.2 */
   2940 		err = xhci_enable_slot(sc, &slot);
   2941 		if (err) {
   2942 			DPRINTFN(1, "enable slot %ju", err, 0, 0, 0);
   2943 			goto bad;
   2944 		}
   2945 
   2946 		xs = &sc->sc_slots[slot];
   2947 		dev->ud_hcpriv = xs;
   2948 
   2949 		/* 4.3.3 initialize slot structure */
   2950 		err = xhci_init_slot(dev, slot);
   2951 		if (err) {
   2952 			DPRINTFN(1, "init slot %ju", err, 0, 0, 0);
   2953 			dev->ud_hcpriv = NULL;
   2954 			/*
   2955 			 * We have to disable_slot here because
   2956 			 * xs->xs_idx == 0 when xhci_init_slot fails,
   2957 			 * in that case usbd_remove_dev won't work.
   2958 			 */
   2959 			mutex_enter(&sc->sc_lock);
   2960 			xhci_disable_slot(sc, slot);
   2961 			mutex_exit(&sc->sc_lock);
   2962 			goto bad;
   2963 		}
   2964 
   2965 		/*
   2966 		 * We have to establish the default pipe _after_ slot
   2967 		 * structure has been prepared.
   2968 		 */
   2969 		err = usbd_setup_pipe(dev, 0, &dev->ud_ep0,
   2970 		    USBD_DEFAULT_INTERVAL, &dev->ud_pipe0);
   2971 		if (err) {
   2972 			DPRINTFN(1, "setup default pipe failed %jd", err, 0, 0,
   2973 			    0);
   2974 			goto bad;
   2975 		}
   2976 
   2977 		/* 4.3.4 Address Assignment */
   2978 		err = xhci_set_address(dev, slot, false);
   2979 		if (err) {
   2980 			DPRINTFN(1, "failed! to set address: %ju", err, 0, 0, 0);
   2981 			goto bad;
   2982 		}
   2983 
   2984 		/* Allow device time to set new address */
   2985 		usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
   2986 
   2987 		usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
   2988 		cp = xhci_slot_get_dcv(sc, xs, XHCI_DCI_SLOT);
   2989 		HEXDUMP("slot context", cp, sc->sc_ctxsz);
   2990 		uint8_t addr = XHCI_SCTX_3_DEV_ADDR_GET(le32toh(cp[3]));
   2991 		DPRINTFN(4, "device address %ju", addr, 0, 0, 0);
   2992 		/*
   2993 		 * XXX ensure we know when the hardware does something
   2994 		 * we can't yet cope with
   2995 		 */
   2996 		KASSERTMSG(addr >= 1 && addr <= 127, "addr %d", addr);
   2997 		dev->ud_addr = addr;
   2998 
   2999 		KASSERTMSG(bus->ub_devices[usb_addr2dindex(dev->ud_addr)] == NULL,
   3000 		    "addr %d already allocated", dev->ud_addr);
   3001 		/*
   3002 		 * The root hub is given its own slot
   3003 		 */
   3004 		bus->ub_devices[usb_addr2dindex(dev->ud_addr)] = dev;
   3005 
   3006 		err = usbd_get_initial_ddesc(dev, dd);
   3007 		if (err) {
   3008 			DPRINTFN(1, "get_initial_ddesc %ju", err, 0, 0, 0);
   3009 			goto bad;
   3010 		}
   3011 
   3012 		/* 4.8.2.1 */
   3013 		if (USB_IS_SS(speed)) {
   3014 			if (dd->bMaxPacketSize != 9) {
   3015 				printf("%s: invalid mps 2^%u for SS ep0,"
   3016 				    " using 512\n",
   3017 				    device_xname(sc->sc_dev),
   3018 				    dd->bMaxPacketSize);
   3019 				dd->bMaxPacketSize = 9;
   3020 			}
   3021 			USETW(dev->ud_ep0desc.wMaxPacketSize,
   3022 			    (1 << dd->bMaxPacketSize));
   3023 		} else
   3024 			USETW(dev->ud_ep0desc.wMaxPacketSize,
   3025 			    dd->bMaxPacketSize);
   3026 		DPRINTFN(4, "bMaxPacketSize %ju", dd->bMaxPacketSize, 0, 0, 0);
   3027 		err = xhci_update_ep0_mps(sc, xs,
   3028 		    UGETW(dev->ud_ep0desc.wMaxPacketSize));
   3029 		if (err) {
   3030 			DPRINTFN(1, "update mps of ep0 %ju", err, 0, 0, 0);
   3031 			goto bad;
   3032 		}
   3033 	}
   3034 
   3035 	err = usbd_reload_device_desc(dev);
   3036 	if (err) {
   3037 		DPRINTFN(1, "reload desc %ju", err, 0, 0, 0);
   3038 		goto bad;
   3039 	}
   3040 
   3041 	DPRINTFN(1, "adding unit addr=%jd, rev=%02jx,",
   3042 		dev->ud_addr, UGETW(dd->bcdUSB), 0, 0);
   3043 	DPRINTFN(1, " class=%jd, subclass=%jd, protocol=%jd,",
   3044 		dd->bDeviceClass, dd->bDeviceSubClass,
   3045 		dd->bDeviceProtocol, 0);
   3046 	DPRINTFN(1, " mps=%jd, len=%jd, noconf=%jd, speed=%jd",
   3047 		dd->bMaxPacketSize, dd->bLength, dd->bNumConfigurations,
   3048 		dev->ud_speed);
   3049 
   3050 	usbd_get_device_strings(dev);
   3051 
   3052 	usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
   3053 
   3054 	if (depth == 0 && port == 0) {
   3055 		usbd_attach_roothub(parent, dev);
   3056 		DPRINTFN(1, "root hub %#jx", (uintptr_t)dev, 0, 0, 0);
   3057 		return USBD_NORMAL_COMPLETION;
   3058 	}
   3059 
   3060 	err = usbd_probe_and_attach(parent, dev, port, dev->ud_addr);
   3061  bad:
   3062 	if (err != USBD_NORMAL_COMPLETION) {
   3063 		if (depth == 0 && port == 0 && dev->ud_pipe0)
   3064 			usbd_kill_pipe(dev->ud_pipe0);
   3065 		usbd_remove_device(dev, up);
   3066 	}
   3067 
   3068 	return err;
   3069 }
   3070 
   3071 static usbd_status
   3072 xhci_ring_init(struct xhci_softc * const sc, struct xhci_ring **xrp,
   3073     size_t ntrb, size_t align)
   3074 {
   3075 	size_t size = ntrb * XHCI_TRB_SIZE;
   3076 	struct xhci_ring *xr;
   3077 
   3078 	XHCIHIST_FUNC();
   3079 	XHCIHIST_CALLARGS("xr %#jx ntrb %#jx align %#jx",
   3080 	    (uintptr_t)*xrp, ntrb, align, 0);
   3081 
   3082 	xr = kmem_zalloc(sizeof(struct xhci_ring), KM_SLEEP);
   3083 	DPRINTFN(1, "ring %#jx", (uintptr_t)xr, 0, 0, 0);
   3084 
   3085 	int err = usb_allocmem(sc->sc_bus.ub_dmatag, size, align,
   3086 	    USBMALLOC_ZERO, &xr->xr_dma);
   3087 	if (err) {
   3088 		kmem_free(xr, sizeof(struct xhci_ring));
   3089 		DPRINTFN(1, "alloc xr_dma failed %jd", err, 0, 0, 0);
   3090 		return err;
   3091 	}
   3092 	mutex_init(&xr->xr_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
   3093 	xr->xr_cookies = kmem_zalloc(sizeof(*xr->xr_cookies) * ntrb, KM_SLEEP);
   3094 	xr->xr_trb = xhci_ring_trbv(xr, 0);
   3095 	xr->xr_ntrb = ntrb;
   3096 	xr->is_halted = false;
   3097 	xhci_host_dequeue(xr);
   3098 	*xrp = xr;
   3099 
   3100 	return USBD_NORMAL_COMPLETION;
   3101 }
   3102 
   3103 static void
   3104 xhci_ring_free(struct xhci_softc * const sc, struct xhci_ring ** const xr)
   3105 {
   3106 	if (*xr == NULL)
   3107 		return;
   3108 
   3109 	usb_freemem(&(*xr)->xr_dma);
   3110 	mutex_destroy(&(*xr)->xr_lock);
   3111 	kmem_free((*xr)->xr_cookies,
   3112 	    sizeof(*(*xr)->xr_cookies) * (*xr)->xr_ntrb);
   3113 	kmem_free(*xr, sizeof(struct xhci_ring));
   3114 	*xr = NULL;
   3115 }
   3116 
   3117 static void
   3118 xhci_ring_put(struct xhci_softc * const sc, struct xhci_ring * const xr,
   3119     void *cookie, struct xhci_soft_trb * const trbs, size_t ntrbs)
   3120 {
   3121 	size_t i;
   3122 	u_int ri;
   3123 	u_int cs;
   3124 	uint64_t parameter;
   3125 	uint32_t status;
   3126 	uint32_t control;
   3127 
   3128 	XHCIHIST_FUNC();
   3129 	XHCIHIST_CALLARGS("%#jx xr_ep %#jx xr_cs %ju",
   3130 	    (uintptr_t)xr, xr->xr_ep, xr->xr_cs, 0);
   3131 
   3132 	KASSERTMSG(ntrbs < xr->xr_ntrb, "ntrbs %zu, xr->xr_ntrb %u",
   3133 	    ntrbs, xr->xr_ntrb);
   3134 	for (i = 0; i < ntrbs; i++) {
   3135 		DPRINTFN(12, "xr %#jx trbs %#jx num %ju", (uintptr_t)xr,
   3136 		    (uintptr_t)trbs, i, 0);
   3137 		DPRINTFN(12, " 0x%016jx 0x%08jx 0x%08jx",
   3138 		    trbs[i].trb_0, trbs[i].trb_2, trbs[i].trb_3, 0);
   3139 		KASSERTMSG(XHCI_TRB_3_TYPE_GET(trbs[i].trb_3) !=
   3140 		    XHCI_TRB_TYPE_LINK, "trbs[%zu].trb3 %#x", i, trbs[i].trb_3);
   3141 	}
   3142 
   3143 	ri = xr->xr_ep;
   3144 	cs = xr->xr_cs;
   3145 
   3146 	/*
   3147 	 * Although the xhci hardware can do scatter/gather dma from
   3148 	 * arbitrary sized buffers, there is a non-obvious restriction
   3149 	 * that a LINK trb is only allowed at the end of a burst of
   3150 	 * transfers - which might be 16kB.
   3151 	 * Arbitrary aligned LINK trb definitely fail on Ivy bridge.
   3152 	 * The simple solution is not to allow a LINK trb in the middle
   3153 	 * of anything - as here.
   3154 	 * XXX: (dsl) There are xhci controllers out there (eg some made by
   3155 	 * ASMedia) that seem to lock up if they process a LINK trb but
   3156 	 * cannot process the linked-to trb yet.
   3157 	 * The code should write the 'cycle' bit on the link trb AFTER
   3158 	 * adding the other trb.
   3159 	 */
   3160 	u_int firstep = xr->xr_ep;
   3161 	u_int firstcs = xr->xr_cs;
   3162 
   3163 	for (i = 0; i < ntrbs; ) {
   3164 		u_int oldri = ri;
   3165 		u_int oldcs = cs;
   3166 
   3167 		if (ri >= (xr->xr_ntrb - 1)) {
   3168 			/* Put Link TD at the end of ring */
   3169 			parameter = xhci_ring_trbp(xr, 0);
   3170 			status = 0;
   3171 			control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) |
   3172 			    XHCI_TRB_3_TC_BIT;
   3173 			xr->xr_cookies[ri] = NULL;
   3174 			xr->xr_ep = 0;
   3175 			xr->xr_cs ^= 1;
   3176 			ri = xr->xr_ep;
   3177 			cs = xr->xr_cs;
   3178 		} else {
   3179 			parameter = trbs[i].trb_0;
   3180 			status = trbs[i].trb_2;
   3181 			control = trbs[i].trb_3;
   3182 
   3183 			xr->xr_cookies[ri] = cookie;
   3184 			ri++;
   3185 			i++;
   3186 		}
   3187 		/*
   3188 		 * If this is a first TRB, mark it invalid to prevent
   3189 		 * xHC from running it immediately.
   3190 		 */
   3191 		if (oldri == firstep) {
   3192 			if (oldcs) {
   3193 				control &= ~XHCI_TRB_3_CYCLE_BIT;
   3194 			} else {
   3195 				control |= XHCI_TRB_3_CYCLE_BIT;
   3196 			}
   3197 		} else {
   3198 			if (oldcs) {
   3199 				control |= XHCI_TRB_3_CYCLE_BIT;
   3200 			} else {
   3201 				control &= ~XHCI_TRB_3_CYCLE_BIT;
   3202 			}
   3203 		}
   3204 		xhci_trb_put(&xr->xr_trb[oldri], parameter, status, control);
   3205 		usb_syncmem(&xr->xr_dma, XHCI_TRB_SIZE * oldri,
   3206 		    XHCI_TRB_SIZE * 1, BUS_DMASYNC_PREWRITE);
   3207 	}
   3208 
   3209 	/* Now invert cycle bit of first TRB */
   3210 	if (firstcs) {
   3211 		xr->xr_trb[firstep].trb_3 |= htole32(XHCI_TRB_3_CYCLE_BIT);
   3212 	} else {
   3213 		xr->xr_trb[firstep].trb_3 &= ~htole32(XHCI_TRB_3_CYCLE_BIT);
   3214 	}
   3215 	usb_syncmem(&xr->xr_dma, XHCI_TRB_SIZE * firstep,
   3216 	    XHCI_TRB_SIZE * 1, BUS_DMASYNC_PREWRITE);
   3217 
   3218 	xr->xr_ep = ri;
   3219 	xr->xr_cs = cs;
   3220 
   3221 	DPRINTFN(12, "%#jx xr_ep %#jx xr_cs %ju", (uintptr_t)xr, xr->xr_ep,
   3222 	    xr->xr_cs, 0);
   3223 }
   3224 
   3225 static inline void
   3226 xhci_ring_put_xfer(struct xhci_softc * const sc, struct xhci_ring * const tr,
   3227     struct xhci_xfer *xx, u_int ntrb)
   3228 {
   3229 	KASSERT(ntrb <= xx->xx_ntrb);
   3230 	xhci_ring_put(sc, tr, xx, xx->xx_trb, ntrb);
   3231 }
   3232 
   3233 /*
   3234  * Stop execution commands, purge all commands on command ring, and
   3235  * rewind dequeue pointer.
   3236  */
   3237 static void
   3238 xhci_abort_command(struct xhci_softc *sc)
   3239 {
   3240 	struct xhci_ring * const cr = sc->sc_cr;
   3241 	uint64_t crcr;
   3242 	int i;
   3243 
   3244 	XHCIHIST_FUNC();
   3245 	XHCIHIST_CALLARGS("command %#jx timeout, aborting",
   3246 	    sc->sc_command_addr, 0, 0, 0);
   3247 
   3248 	mutex_enter(&cr->xr_lock);
   3249 
   3250 	/* 4.6.1.2 Aborting a Command */
   3251 	crcr = xhci_op_read_8(sc, XHCI_CRCR);
   3252 	xhci_op_write_8(sc, XHCI_CRCR, crcr | XHCI_CRCR_LO_CA);
   3253 
   3254 	for (i = 0; i < 500; i++) {
   3255 		crcr = xhci_op_read_8(sc, XHCI_CRCR);
   3256 		if ((crcr & XHCI_CRCR_LO_CRR) == 0)
   3257 			break;
   3258 		usb_delay_ms(&sc->sc_bus, 1);
   3259 	}
   3260 	if ((crcr & XHCI_CRCR_LO_CRR) != 0) {
   3261 		DPRINTFN(1, "Command Abort timeout", 0, 0, 0, 0);
   3262 		/* reset HC here? */
   3263 	}
   3264 
   3265 	/* reset command ring dequeue pointer */
   3266 	cr->xr_ep = 0;
   3267 	cr->xr_cs = 1;
   3268 	xhci_op_write_8(sc, XHCI_CRCR, xhci_ring_trbp(cr, 0) | cr->xr_cs);
   3269 
   3270 	mutex_exit(&cr->xr_lock);
   3271 }
   3272 
   3273 /*
   3274  * Put a command on command ring, ring bell, set timer, and cv_timedwait.
   3275  * Command completion is notified by cv_signal from xhci_event_cmd()
   3276  * (called from xhci_softint), or timed-out.
   3277  * The completion code is copied to sc->sc_result_trb in xhci_event_cmd(),
   3278  * then do_command examines it.
   3279  */
   3280 static usbd_status
   3281 xhci_do_command_locked(struct xhci_softc * const sc,
   3282     struct xhci_soft_trb * const trb, int timeout)
   3283 {
   3284 	struct xhci_ring * const cr = sc->sc_cr;
   3285 	usbd_status err;
   3286 
   3287 	XHCIHIST_FUNC();
   3288 	XHCIHIST_CALLARGS("input: 0x%016jx 0x%08jx 0x%08jx",
   3289 	    trb->trb_0, trb->trb_2, trb->trb_3, 0);
   3290 
   3291 	KASSERTMSG(!cpu_intr_p() && !cpu_softintr_p(), "called from intr ctx");
   3292 	KASSERT(mutex_owned(&sc->sc_lock));
   3293 
   3294 	while (sc->sc_command_addr != 0 ||
   3295 	    (sc->sc_suspender != NULL && sc->sc_suspender != curlwp))
   3296 		cv_wait(&sc->sc_cmdbusy_cv, &sc->sc_lock);
   3297 	if (sc->sc_suspendresume_failed)
   3298 		return USBD_IOERROR;
   3299 
   3300 	/*
   3301 	 * If enqueue pointer points at last of ring, it's Link TRB,
   3302 	 * command TRB will be stored in 0th TRB.
   3303 	 */
   3304 	if (cr->xr_ep == cr->xr_ntrb - 1)
   3305 		sc->sc_command_addr = xhci_ring_trbp(cr, 0);
   3306 	else
   3307 		sc->sc_command_addr = xhci_ring_trbp(cr, cr->xr_ep);
   3308 
   3309 	sc->sc_resultpending = true;
   3310 
   3311 	mutex_enter(&cr->xr_lock);
   3312 	xhci_ring_put(sc, cr, NULL, trb, 1);
   3313 	mutex_exit(&cr->xr_lock);
   3314 
   3315 	xhci_db_write_4(sc, XHCI_DOORBELL(0), 0);
   3316 
   3317 	while (sc->sc_resultpending) {
   3318 		if (cv_timedwait(&sc->sc_command_cv, &sc->sc_lock,
   3319 		    MAX(1, mstohz(timeout))) == EWOULDBLOCK) {
   3320 			xhci_abort_command(sc);
   3321 			err = USBD_TIMEOUT;
   3322 			goto timedout;
   3323 		}
   3324 	}
   3325 
   3326 	trb->trb_0 = sc->sc_result_trb.trb_0;
   3327 	trb->trb_2 = sc->sc_result_trb.trb_2;
   3328 	trb->trb_3 = sc->sc_result_trb.trb_3;
   3329 
   3330 	DPRINTFN(12, "output: 0x%016jx 0x%08jx 0x%08jx",
   3331 	    trb->trb_0, trb->trb_2, trb->trb_3, 0);
   3332 
   3333 	switch (XHCI_TRB_2_ERROR_GET(trb->trb_2)) {
   3334 	case XHCI_TRB_ERROR_SUCCESS:
   3335 		err = USBD_NORMAL_COMPLETION;
   3336 		break;
   3337 	default:
   3338 	case 192 ... 223:
   3339 		DPRINTFN(5, "error %#jx",
   3340 		    XHCI_TRB_2_ERROR_GET(trb->trb_2), 0, 0, 0);
   3341 		err = USBD_IOERROR;
   3342 		break;
   3343 	case 224 ... 255:
   3344 		err = USBD_NORMAL_COMPLETION;
   3345 		break;
   3346 	}
   3347 
   3348 timedout:
   3349 	sc->sc_resultpending = false;
   3350 	sc->sc_command_addr = 0;
   3351 	cv_broadcast(&sc->sc_cmdbusy_cv);
   3352 
   3353 	return err;
   3354 }
   3355 
   3356 static usbd_status
   3357 xhci_do_command(struct xhci_softc * const sc, struct xhci_soft_trb * const trb,
   3358     int timeout)
   3359 {
   3360 
   3361 	mutex_enter(&sc->sc_lock);
   3362 	usbd_status ret = xhci_do_command_locked(sc, trb, timeout);
   3363 	mutex_exit(&sc->sc_lock);
   3364 
   3365 	return ret;
   3366 }
   3367 
   3368 static usbd_status
   3369 xhci_enable_slot(struct xhci_softc * const sc, uint8_t * const slotp)
   3370 {
   3371 	struct xhci_soft_trb trb;
   3372 	usbd_status err;
   3373 
   3374 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   3375 
   3376 	trb.trb_0 = 0;
   3377 	trb.trb_2 = 0;
   3378 	trb.trb_3 = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ENABLE_SLOT);
   3379 
   3380 	err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
   3381 	if (err != USBD_NORMAL_COMPLETION) {
   3382 		return err;
   3383 	}
   3384 
   3385 	*slotp = XHCI_TRB_3_SLOT_GET(trb.trb_3);
   3386 
   3387 	return err;
   3388 }
   3389 
   3390 /*
   3391  * xHCI 4.6.4
   3392  * Deallocate ring and device/input context DMA buffers, and disable_slot.
   3393  * All endpoints in the slot should be stopped.
   3394  * Should be called with sc_lock held.
   3395  */
   3396 static usbd_status
   3397 xhci_disable_slot(struct xhci_softc * const sc, uint8_t slot)
   3398 {
   3399 	struct xhci_soft_trb trb;
   3400 	struct xhci_slot *xs;
   3401 	usbd_status err;
   3402 
   3403 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   3404 
   3405 	if (sc->sc_dying)
   3406 		return USBD_IOERROR;
   3407 
   3408 	trb.trb_0 = 0;
   3409 	trb.trb_2 = 0;
   3410 	trb.trb_3 = XHCI_TRB_3_SLOT_SET(slot) |
   3411 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DISABLE_SLOT);
   3412 
   3413 	err = xhci_do_command_locked(sc, &trb, USBD_DEFAULT_TIMEOUT);
   3414 
   3415 	if (!err) {
   3416 		xs = &sc->sc_slots[slot];
   3417 		if (xs->xs_idx != 0) {
   3418 			xhci_free_slot(sc, xs);
   3419 			xhci_set_dcba(sc, 0, slot);
   3420 			memset(xs, 0, sizeof(*xs));
   3421 		}
   3422 	}
   3423 
   3424 	return err;
   3425 }
   3426 
   3427 /*
   3428  * Set address of device and transition slot state from ENABLED to ADDRESSED
   3429  * if Block Setaddress Request (BSR) is false.
   3430  * If BSR==true, transition slot state from ENABLED to DEFAULT.
   3431  * see xHCI 1.1  4.5.3, 3.3.4
   3432  * Should be called without sc_lock held.
   3433  */
   3434 static usbd_status
   3435 xhci_address_device(struct xhci_softc * const sc,
   3436     uint64_t icp, uint8_t slot_id, bool bsr)
   3437 {
   3438 	struct xhci_soft_trb trb;
   3439 	usbd_status err;
   3440 
   3441 	XHCIHIST_FUNC();
   3442 	if (bsr) {
   3443 		XHCIHIST_CALLARGS("icp %#jx slot %#jx with bsr",
   3444 		    icp, slot_id, 0, 0);
   3445 	} else {
   3446 		XHCIHIST_CALLARGS("icp %#jx slot %#jx nobsr",
   3447 		    icp, slot_id, 0, 0);
   3448 	}
   3449 
   3450 	trb.trb_0 = icp;
   3451 	trb.trb_2 = 0;
   3452 	trb.trb_3 = XHCI_TRB_3_SLOT_SET(slot_id) |
   3453 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ADDRESS_DEVICE) |
   3454 	    (bsr ? XHCI_TRB_3_BSR_BIT : 0);
   3455 
   3456 	err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
   3457 
   3458 	if (XHCI_TRB_2_ERROR_GET(trb.trb_2) == XHCI_TRB_ERROR_NO_SLOTS)
   3459 		err = USBD_NO_ADDR;
   3460 
   3461 	return err;
   3462 }
   3463 
   3464 static usbd_status
   3465 xhci_update_ep0_mps(struct xhci_softc * const sc,
   3466     struct xhci_slot * const xs, u_int mps)
   3467 {
   3468 	struct xhci_soft_trb trb;
   3469 	usbd_status err;
   3470 	uint32_t * cp;
   3471 
   3472 	XHCIHIST_FUNC();
   3473 	XHCIHIST_CALLARGS("slot %ju mps %ju", xs->xs_idx, mps, 0, 0);
   3474 
   3475 	cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL);
   3476 	cp[0] = htole32(0);
   3477 	cp[1] = htole32(XHCI_INCTX_1_ADD_MASK(XHCI_DCI_EP_CONTROL));
   3478 
   3479 	cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_EP_CONTROL));
   3480 	cp[1] &= ~htole32(XHCI_EPCTX_1_MAXP_SIZE_MASK);
   3481 	cp[1] |= htole32(XHCI_EPCTX_1_MAXP_SIZE_SET(mps));
   3482 
   3483 	/* sync input contexts before they are read from memory */
   3484 	usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE);
   3485 	HEXDUMP("input context", xhci_slot_get_icv(sc, xs, 0),
   3486 	    sc->sc_ctxsz * 4);
   3487 
   3488 	trb.trb_0 = xhci_slot_get_icp(sc, xs, 0);
   3489 	trb.trb_2 = 0;
   3490 	trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
   3491 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVALUATE_CTX);
   3492 
   3493 	err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
   3494 	return err;
   3495 }
   3496 
   3497 static void
   3498 xhci_set_dcba(struct xhci_softc * const sc, uint64_t dcba, int si)
   3499 {
   3500 	uint64_t * const dcbaa = KERNADDR(&sc->sc_dcbaa_dma, 0);
   3501 
   3502 	XHCIHIST_FUNC();
   3503 	XHCIHIST_CALLARGS("dcbaa %#jx dc 0x%016jx slot %jd",
   3504 	    (uintptr_t)&dcbaa[si], dcba, si, 0);
   3505 
   3506 	dcbaa[si] = htole64(dcba);
   3507 	usb_syncmem(&sc->sc_dcbaa_dma, si * sizeof(uint64_t), sizeof(uint64_t),
   3508 	    BUS_DMASYNC_PREWRITE);
   3509 }
   3510 
   3511 /*
   3512  * Allocate device and input context DMA buffer, and
   3513  * TRB DMA buffer for each endpoint.
   3514  */
   3515 static usbd_status
   3516 xhci_init_slot(struct usbd_device *dev, uint32_t slot)
   3517 {
   3518 	struct xhci_softc * const sc = XHCI_BUS2SC(dev->ud_bus);
   3519 	struct xhci_slot *xs;
   3520 
   3521 	XHCIHIST_FUNC();
   3522 	XHCIHIST_CALLARGS("slot %ju", slot, 0, 0, 0);
   3523 
   3524 	xs = &sc->sc_slots[slot];
   3525 
   3526 	/* allocate contexts */
   3527 	int err = usb_allocmem(sc->sc_bus.ub_dmatag, sc->sc_pgsz, sc->sc_pgsz,
   3528 	    USBMALLOC_ZERO, &xs->xs_dc_dma);
   3529 	if (err) {
   3530 		DPRINTFN(1, "failed to allocmem output device context %jd",
   3531 		    err, 0, 0, 0);
   3532 		return USBD_NOMEM;
   3533 	}
   3534 
   3535 	err = usb_allocmem(sc->sc_bus.ub_dmatag, sc->sc_pgsz, sc->sc_pgsz,
   3536 	    USBMALLOC_ZERO, &xs->xs_ic_dma);
   3537 	if (err) {
   3538 		DPRINTFN(1, "failed to allocmem input device context %jd",
   3539 		    err, 0, 0, 0);
   3540 		goto bad1;
   3541 	}
   3542 
   3543 	memset(&xs->xs_xr[0], 0, sizeof(xs->xs_xr));
   3544 	xs->xs_idx = slot;
   3545 
   3546 	return USBD_NORMAL_COMPLETION;
   3547 
   3548 bad1:
   3549 	usb_freemem(&xs->xs_dc_dma);
   3550 	xs->xs_idx = 0;
   3551 	return USBD_NOMEM;
   3552 }
   3553 
   3554 static void
   3555 xhci_free_slot(struct xhci_softc *sc, struct xhci_slot *xs)
   3556 {
   3557 	u_int dci;
   3558 
   3559 	XHCIHIST_FUNC();
   3560 	XHCIHIST_CALLARGS("slot %ju", xs->xs_idx, 0, 0, 0);
   3561 
   3562 	/* deallocate all allocated rings in the slot */
   3563 	for (dci = XHCI_DCI_SLOT; dci <= XHCI_MAX_DCI; dci++) {
   3564 		if (xs->xs_xr[dci] != NULL)
   3565 			xhci_ring_free(sc, &xs->xs_xr[dci]);
   3566 	}
   3567 	usb_freemem(&xs->xs_ic_dma);
   3568 	usb_freemem(&xs->xs_dc_dma);
   3569 	xs->xs_idx = 0;
   3570 }
   3571 
   3572 /*
   3573  * Setup slot context, set Device Context Base Address, and issue
   3574  * Set Address Device command.
   3575  */
   3576 static usbd_status
   3577 xhci_set_address(struct usbd_device *dev, uint32_t slot, bool bsr)
   3578 {
   3579 	struct xhci_softc * const sc = XHCI_BUS2SC(dev->ud_bus);
   3580 	struct xhci_slot *xs;
   3581 	usbd_status err;
   3582 
   3583 	XHCIHIST_FUNC();
   3584 	XHCIHIST_CALLARGS("slot %ju bsr %ju", slot, bsr, 0, 0);
   3585 
   3586 	xs = &sc->sc_slots[slot];
   3587 
   3588 	xhci_setup_ctx(dev->ud_pipe0);
   3589 
   3590 	HEXDUMP("input context", xhci_slot_get_icv(sc, xs, 0),
   3591 	    sc->sc_ctxsz * 3);
   3592 
   3593 	xhci_set_dcba(sc, DMAADDR(&xs->xs_dc_dma, 0), slot);
   3594 
   3595 	err = xhci_address_device(sc, xhci_slot_get_icp(sc, xs, 0), slot, bsr);
   3596 
   3597 	usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
   3598 	HEXDUMP("output context", xhci_slot_get_dcv(sc, xs, 0),
   3599 	    sc->sc_ctxsz * 2);
   3600 
   3601 	return err;
   3602 }
   3603 
   3604 /*
   3605  * 4.8.2, 6.2.3.2
   3606  * construct slot/endpoint context parameters and do syncmem
   3607  */
   3608 static void
   3609 xhci_setup_ctx(struct usbd_pipe *pipe)
   3610 {
   3611 	struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
   3612 	struct usbd_device *dev = pipe->up_dev;
   3613 	struct xhci_slot * const xs = dev->ud_hcpriv;
   3614 	usb_endpoint_descriptor_t * const ed = pipe->up_endpoint->ue_edesc;
   3615 	const u_int dci = xhci_ep_get_dci(ed);
   3616 	const uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
   3617 	uint32_t *cp;
   3618 	uint8_t speed = dev->ud_speed;
   3619 
   3620 	XHCIHIST_FUNC();
   3621 	XHCIHIST_CALLARGS("pipe %#jx: slot %ju dci %ju speed %ju",
   3622 	    (uintptr_t)pipe, xs->xs_idx, dci, speed);
   3623 
   3624 	/* set up initial input control context */
   3625 	cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL);
   3626 	cp[0] = htole32(0);
   3627 	cp[1] = htole32(XHCI_INCTX_1_ADD_MASK(dci));
   3628 	cp[1] |= htole32(XHCI_INCTX_1_ADD_MASK(XHCI_DCI_SLOT));
   3629 	cp[7] = htole32(0);
   3630 
   3631 	/* set up input slot context */
   3632 	cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_SLOT));
   3633 	cp[0] =
   3634 	    XHCI_SCTX_0_CTX_NUM_SET(dci) |
   3635 	    XHCI_SCTX_0_SPEED_SET(xhci_speed2xspeed(speed));
   3636 	cp[1] = 0;
   3637 	cp[2] = XHCI_SCTX_2_IRQ_TARGET_SET(0);
   3638 	cp[3] = 0;
   3639 	xhci_setup_route(pipe, cp);
   3640 	xhci_setup_tthub(pipe, cp);
   3641 
   3642 	cp[0] = htole32(cp[0]);
   3643 	cp[1] = htole32(cp[1]);
   3644 	cp[2] = htole32(cp[2]);
   3645 	cp[3] = htole32(cp[3]);
   3646 
   3647 	/* set up input endpoint context */
   3648 	cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(dci));
   3649 	cp[0] =
   3650 	    XHCI_EPCTX_0_EPSTATE_SET(0) |
   3651 	    XHCI_EPCTX_0_MULT_SET(0) |
   3652 	    XHCI_EPCTX_0_MAXP_STREAMS_SET(0) |
   3653 	    XHCI_EPCTX_0_LSA_SET(0) |
   3654 	    XHCI_EPCTX_0_MAX_ESIT_PAYLOAD_HI_SET(0);
   3655 	cp[1] =
   3656 	    XHCI_EPCTX_1_EPTYPE_SET(xhci_ep_get_type(ed)) |
   3657 	    XHCI_EPCTX_1_HID_SET(0) |
   3658 	    XHCI_EPCTX_1_MAXB_SET(0);
   3659 
   3660 	if (xfertype != UE_ISOCHRONOUS)
   3661 		cp[1] |= XHCI_EPCTX_1_CERR_SET(3);
   3662 
   3663 	xhci_setup_maxburst(pipe, cp);
   3664 
   3665 	DPRINTFN(4, "setting on dci %ju ival %ju mult %ju mps %#jx",
   3666 	    dci, XHCI_EPCTX_0_IVAL_GET(cp[0]), XHCI_EPCTX_0_MULT_GET(cp[0]),
   3667 	    XHCI_EPCTX_1_MAXP_SIZE_GET(cp[1]));
   3668 	DPRINTFN(4, " maxburst %ju mep %#jx atl %#jx",
   3669 	    XHCI_EPCTX_1_MAXB_GET(cp[1]),
   3670 	    (XHCI_EPCTX_0_MAX_ESIT_PAYLOAD_HI_GET(cp[0]) << 16) +
   3671 	    XHCI_EPCTX_4_MAX_ESIT_PAYLOAD_GET(cp[4]),
   3672 	    XHCI_EPCTX_4_AVG_TRB_LEN_GET(cp[4]), 0);
   3673 
   3674 	/* rewind TR dequeue pointer in xHC */
   3675 	/* can't use xhci_ep_get_dci() yet? */
   3676 	*(uint64_t *)(&cp[2]) = htole64(
   3677 	    xhci_ring_trbp(xs->xs_xr[dci], 0) |
   3678 	    XHCI_EPCTX_2_DCS_SET(1));
   3679 
   3680 	cp[0] = htole32(cp[0]);
   3681 	cp[1] = htole32(cp[1]);
   3682 	cp[4] = htole32(cp[4]);
   3683 
   3684 	/* rewind TR dequeue pointer in driver */
   3685 	struct xhci_ring *xr = xs->xs_xr[dci];
   3686 	mutex_enter(&xr->xr_lock);
   3687 	xhci_host_dequeue(xr);
   3688 	mutex_exit(&xr->xr_lock);
   3689 
   3690 	/* sync input contexts before they are read from memory */
   3691 	usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE);
   3692 }
   3693 
   3694 /*
   3695  * Setup route string and roothub port of given device for slot context
   3696  */
   3697 static void
   3698 xhci_setup_route(struct usbd_pipe *pipe, uint32_t *cp)
   3699 {
   3700 	struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
   3701 	struct usbd_device *dev = pipe->up_dev;
   3702 	struct usbd_port *up = dev->ud_powersrc;
   3703 	struct usbd_device *hub;
   3704 	struct usbd_device *adev;
   3705 	uint8_t rhport = 0;
   3706 	uint32_t route = 0;
   3707 
   3708 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   3709 
   3710 	/* Locate root hub port and Determine route string */
   3711 	/* 4.3.3 route string does not include roothub port */
   3712 	for (hub = dev; hub != NULL; hub = hub->ud_myhub) {
   3713 		uint32_t dep;
   3714 
   3715 		DPRINTFN(4, "hub %#jx depth %jd upport %#jx upportno %jd",
   3716 		    (uintptr_t)hub, hub->ud_depth, (uintptr_t)hub->ud_powersrc,
   3717 		    hub->ud_powersrc ? (uintptr_t)hub->ud_powersrc->up_portno :
   3718 			 -1);
   3719 
   3720 		if (hub->ud_powersrc == NULL)
   3721 			break;
   3722 		dep = hub->ud_depth;
   3723 		if (dep == 0)
   3724 			break;
   3725 		rhport = hub->ud_powersrc->up_portno;
   3726 		if (dep > USB_HUB_MAX_DEPTH)
   3727 			continue;
   3728 
   3729 		route |=
   3730 		    (rhport > UHD_SS_NPORTS_MAX ? UHD_SS_NPORTS_MAX : rhport)
   3731 		    << ((dep - 1) * 4);
   3732 	}
   3733 	route = route >> 4;
   3734 	size_t bn = hub == sc->sc_bus.ub_roothub ? 0 : 1;
   3735 
   3736 	/* Locate port on upstream high speed hub */
   3737 	for (adev = dev, hub = up->up_parent;
   3738 	     hub != NULL && hub->ud_speed != USB_SPEED_HIGH;
   3739 	     adev = hub, hub = hub->ud_myhub)
   3740 		;
   3741 	if (hub) {
   3742 		int p;
   3743 		for (p = 1; p <= hub->ud_hub->uh_hubdesc.bNbrPorts; p++) {
   3744 			if (hub->ud_hub->uh_ports[p - 1].up_dev == adev) {
   3745 				dev->ud_myhsport = &hub->ud_hub->uh_ports[p - 1];
   3746 				goto found;
   3747 			}
   3748 		}
   3749 		panic("%s: cannot find HS port", __func__);
   3750 	found:
   3751 		DPRINTFN(4, "high speed port %jd", p, 0, 0, 0);
   3752 	} else {
   3753 		dev->ud_myhsport = NULL;
   3754 	}
   3755 
   3756 	const size_t ctlrport = xhci_rhport2ctlrport(sc, bn, rhport);
   3757 
   3758 	DPRINTFN(4, "rhport %ju ctlrport %ju Route %05jx hub %#jx", rhport,
   3759 	    ctlrport, route, (uintptr_t)hub);
   3760 
   3761 	cp[0] |= XHCI_SCTX_0_ROUTE_SET(route);
   3762 	cp[1] |= XHCI_SCTX_1_RH_PORT_SET(ctlrport);
   3763 }
   3764 
   3765 /*
   3766  * Setup whether device is hub, whether device uses MTT, and
   3767  * TT informations if it uses MTT.
   3768  */
   3769 static void
   3770 xhci_setup_tthub(struct usbd_pipe *pipe, uint32_t *cp)
   3771 {
   3772 	struct usbd_device *dev = pipe->up_dev;
   3773 	struct usbd_port *myhsport = dev->ud_myhsport;
   3774 	usb_device_descriptor_t * const dd = &dev->ud_ddesc;
   3775 	uint32_t speed = dev->ud_speed;
   3776 	uint8_t rhaddr = dev->ud_bus->ub_rhaddr;
   3777 	uint8_t tthubslot, ttportnum;
   3778 	bool ishub;
   3779 	bool usemtt;
   3780 
   3781 	XHCIHIST_FUNC();
   3782 
   3783 	/*
   3784 	 * 6.2.2, Table 57-60, 6.2.2.1, 6.2.2.2
   3785 	 * tthubslot:
   3786 	 *   This is the slot ID of parent HS hub
   3787 	 *   if LS/FS device is connected && connected through HS hub.
   3788 	 *   This is 0 if device is not LS/FS device ||
   3789 	 *   parent hub is not HS hub ||
   3790 	 *   attached to root hub.
   3791 	 * ttportnum:
   3792 	 *   This is the downstream facing port of parent HS hub
   3793 	 *   if LS/FS device is connected.
   3794 	 *   This is 0 if device is not LS/FS device ||
   3795 	 *   parent hub is not HS hub ||
   3796 	 *   attached to root hub.
   3797 	 */
   3798 	if (myhsport &&
   3799 	    myhsport->up_parent->ud_addr != rhaddr &&
   3800 	    (speed == USB_SPEED_LOW || speed == USB_SPEED_FULL)) {
   3801 		ttportnum = myhsport->up_portno;
   3802 		tthubslot = myhsport->up_parent->ud_addr;
   3803 	} else {
   3804 		ttportnum = 0;
   3805 		tthubslot = 0;
   3806 	}
   3807 	XHCIHIST_CALLARGS("myhsport %#jx ttportnum=%jd tthubslot=%jd",
   3808 	    (uintptr_t)myhsport, ttportnum, tthubslot, 0);
   3809 
   3810 	/* ishub is valid after reading UDESC_DEVICE */
   3811 	ishub = (dd->bDeviceClass == UDCLASS_HUB);
   3812 
   3813 	/* dev->ud_hub is valid after reading UDESC_HUB */
   3814 	if (ishub && dev->ud_hub) {
   3815 		usb_hub_descriptor_t *hd = &dev->ud_hub->uh_hubdesc;
   3816 		uint8_t ttt =
   3817 		    __SHIFTOUT(UGETW(hd->wHubCharacteristics), UHD_TT_THINK);
   3818 
   3819 		cp[1] |= XHCI_SCTX_1_NUM_PORTS_SET(hd->bNbrPorts);
   3820 		cp[2] |= XHCI_SCTX_2_TT_THINK_TIME_SET(ttt);
   3821 		DPRINTFN(4, "nports=%jd ttt=%jd", hd->bNbrPorts, ttt, 0, 0);
   3822 	}
   3823 
   3824 #define IS_MTTHUB(dd) \
   3825      ((dd)->bDeviceProtocol == UDPROTO_HSHUBMTT)
   3826 
   3827 	/*
   3828 	 * MTT flag is set if
   3829 	 * 1. this is HS hub && MTTs are supported and enabled;  or
   3830 	 * 2. this is LS or FS device && there is a parent HS hub where MTTs
   3831 	 *    are supported and enabled.
   3832 	 *
   3833 	 * XXX enabled is not tested yet
   3834 	 */
   3835 	if (ishub && speed == USB_SPEED_HIGH && IS_MTTHUB(dd))
   3836 		usemtt = true;
   3837 	else if ((speed == USB_SPEED_LOW || speed == USB_SPEED_FULL) &&
   3838 	    myhsport &&
   3839 	    myhsport->up_parent->ud_addr != rhaddr &&
   3840 	    IS_MTTHUB(&myhsport->up_parent->ud_ddesc))
   3841 		usemtt = true;
   3842 	else
   3843 		usemtt = false;
   3844 	DPRINTFN(4, "class %ju proto %ju ishub %jd usemtt %jd",
   3845 	    dd->bDeviceClass, dd->bDeviceProtocol, ishub, usemtt);
   3846 
   3847 #undef IS_MTTHUB
   3848 
   3849 	cp[0] |=
   3850 	    XHCI_SCTX_0_HUB_SET(ishub ? 1 : 0) |
   3851 	    XHCI_SCTX_0_MTT_SET(usemtt ? 1 : 0);
   3852 	cp[2] |=
   3853 	    XHCI_SCTX_2_TT_HUB_SID_SET(tthubslot) |
   3854 	    XHCI_SCTX_2_TT_PORT_NUM_SET(ttportnum);
   3855 }
   3856 
   3857 static const usb_endpoint_ss_comp_descriptor_t *
   3858 xhci_get_essc_desc(struct usbd_pipe *pipe)
   3859 {
   3860 	struct usbd_device *dev = pipe->up_dev;
   3861 	usb_endpoint_descriptor_t * const ed = pipe->up_endpoint->ue_edesc;
   3862 	const usb_cdc_descriptor_t *cdcd;
   3863 	usbd_desc_iter_t iter;
   3864 	uint8_t ep;
   3865 
   3866 	/* config desc is NULL when opening ep0 */
   3867 	if (dev == NULL || dev->ud_cdesc == NULL)
   3868 		return NULL;
   3869 
   3870 	cdcd = (const usb_cdc_descriptor_t *)usb_find_desc(dev,
   3871 	    UDESC_INTERFACE, USBD_CDCSUBTYPE_ANY);
   3872 	if (cdcd == NULL)
   3873 		return NULL;
   3874 
   3875 	usb_desc_iter_init(dev, &iter);
   3876 	iter.cur = (const void *)cdcd;
   3877 
   3878 	/* find endpoint_ss_comp desc for ep of this pipe */
   3879 	for (ep = 0;;) {
   3880 		cdcd = (const usb_cdc_descriptor_t *)usb_desc_iter_next(&iter);
   3881 		if (cdcd == NULL)
   3882 			break;
   3883 		if (ep == 0 && cdcd->bDescriptorType == UDESC_ENDPOINT) {
   3884 			ep = ((const usb_endpoint_descriptor_t *)cdcd)->
   3885 			    bEndpointAddress;
   3886 			if (UE_GET_ADDR(ep) ==
   3887 			    UE_GET_ADDR(ed->bEndpointAddress)) {
   3888 				cdcd = (const usb_cdc_descriptor_t *)
   3889 				    usb_desc_iter_next(&iter);
   3890 				break;
   3891 			}
   3892 			ep = 0;
   3893 		}
   3894 	}
   3895 	if (cdcd != NULL && cdcd->bDescriptorType == UDESC_ENDPOINT_SS_COMP) {
   3896 		return (const usb_endpoint_ss_comp_descriptor_t *)cdcd;
   3897 	}
   3898 	return NULL;
   3899 }
   3900 
   3901 /* set up params for periodic endpoint */
   3902 static void
   3903 xhci_setup_maxburst(struct usbd_pipe *pipe, uint32_t *cp)
   3904 {
   3905 	struct xhci_pipe * const xpipe = (struct xhci_pipe *)pipe;
   3906 	struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
   3907 	struct usbd_device * const dev = pipe->up_dev;
   3908 	usb_endpoint_descriptor_t * const ed = pipe->up_endpoint->ue_edesc;
   3909 	const uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
   3910 	uint16_t mps = UGETW(ed->wMaxPacketSize);
   3911 	uint8_t speed = dev->ud_speed;
   3912 	uint32_t maxb, mep, atl;
   3913 	uint8_t ival, mult;
   3914 
   3915 	const usb_endpoint_ss_comp_descriptor_t * esscd =
   3916 	    xhci_get_essc_desc(pipe);
   3917 
   3918 	/* USB 2.0  9.6.6, xHCI 4.8.2.4, 6.2.3.2 - 6.2.3.8 */
   3919 	switch (xfertype) {
   3920 	case UE_ISOCHRONOUS:
   3921 	case UE_INTERRUPT:
   3922 		if (USB_IS_SS(speed)) {
   3923 			maxb = esscd ? esscd->bMaxBurst : UE_GET_TRANS(mps);
   3924 			mep = esscd ? UGETW(esscd->wBytesPerInterval) :
   3925 			    UE_GET_SIZE(mps) * (maxb + 1);
   3926 			if (esscd && xfertype == UE_ISOCHRONOUS &&
   3927 			    XHCI_HCC2_LEC(sc->sc_hcc2) == 0) {
   3928 				mult = UE_GET_SS_ISO_MULT(esscd->bmAttributes);
   3929 				mult = (mult > 2) ? 2 : mult;
   3930 			} else
   3931 				mult = 0;
   3932 
   3933 		} else {
   3934 			switch (speed) {
   3935 			case USB_SPEED_HIGH:
   3936 				maxb = UE_GET_TRANS(mps);
   3937 				mep = UE_GET_SIZE(mps) * (maxb + 1);
   3938 				break;
   3939 			case USB_SPEED_FULL:
   3940 				maxb = 0;
   3941 				mep = UE_GET_SIZE(mps);
   3942 				break;
   3943 			default:
   3944 				maxb = 0;
   3945 				mep = 0;
   3946 				break;
   3947 			}
   3948 			mult = 0;
   3949 		}
   3950 		mps = UE_GET_SIZE(mps);
   3951 
   3952 		if (pipe->up_interval == USBD_DEFAULT_INTERVAL)
   3953 			ival = ed->bInterval;
   3954 		else
   3955 			ival = pipe->up_interval;
   3956 
   3957 		ival = xhci_bival2ival(ival, speed, xfertype);
   3958 		atl = mep;
   3959 		break;
   3960 	case UE_CONTROL:
   3961 	case UE_BULK:
   3962 	default:
   3963 		if (USB_IS_SS(speed)) {
   3964 			maxb = esscd ? esscd->bMaxBurst : 0;
   3965 		} else
   3966 			maxb = 0;
   3967 
   3968 		mps = UE_GET_SIZE(mps);
   3969 		mep = 0;
   3970 		mult = 0;
   3971 		ival = 0;
   3972 		if (xfertype == UE_CONTROL)
   3973 			atl = 8;		/* 6.2.3 */
   3974 		else
   3975 			atl = mps;
   3976 		break;
   3977 	}
   3978 
   3979 	switch (speed) {
   3980 	case USB_SPEED_LOW:
   3981 		break;
   3982 	case USB_SPEED_FULL:
   3983 		if (xfertype == UE_INTERRUPT)
   3984 			if (mep > XHCI_EPCTX_MEP_FS_INTR)
   3985 				mep = XHCI_EPCTX_MEP_FS_INTR;
   3986 		if (xfertype == UE_ISOCHRONOUS)
   3987 			if (mep > XHCI_EPCTX_MEP_FS_ISOC)
   3988 				mep = XHCI_EPCTX_MEP_FS_ISOC;
   3989 		break;
   3990 	case USB_SPEED_HIGH:
   3991 		if (xfertype == UE_INTERRUPT)
   3992 			if (mep > XHCI_EPCTX_MEP_HS_INTR)
   3993 				mep = XHCI_EPCTX_MEP_HS_INTR;
   3994 		if (xfertype == UE_ISOCHRONOUS)
   3995 			if (mep > XHCI_EPCTX_MEP_HS_ISOC)
   3996 				mep = XHCI_EPCTX_MEP_HS_ISOC;
   3997 		break;
   3998 	case USB_SPEED_SUPER:
   3999 	case USB_SPEED_SUPER_PLUS:
   4000 	default:
   4001 		if (xfertype == UE_INTERRUPT)
   4002 			if (mep > XHCI_EPCTX_MEP_SS_INTR)
   4003 				mep = XHCI_EPCTX_MEP_SS_INTR;
   4004 		if (xfertype == UE_ISOCHRONOUS) {
   4005 			if (speed == USB_SPEED_SUPER ||
   4006 			    XHCI_HCC2_LEC(sc->sc_hcc2) == 0) {
   4007 				if (mep > XHCI_EPCTX_MEP_SS_ISOC)
   4008 					mep = XHCI_EPCTX_MEP_SS_ISOC;
   4009 			} else {
   4010 				if (mep > XHCI_EPCTX_MEP_SS_ISOC_LEC)
   4011 					mep = XHCI_EPCTX_MEP_SS_ISOC_LEC;
   4012 			}
   4013 		}
   4014 		break;
   4015 	}
   4016 
   4017 	xpipe->xp_ival = ival;
   4018 	xpipe->xp_maxb = maxb + 1;
   4019 	xpipe->xp_mult = mult + 1;
   4020 
   4021 	cp[0] |= XHCI_EPCTX_0_MAX_ESIT_PAYLOAD_HI_SET(mep >> 16);
   4022 	cp[0] |= XHCI_EPCTX_0_IVAL_SET(ival);
   4023 	cp[0] |= XHCI_EPCTX_0_MULT_SET(mult);
   4024 	cp[1] |= XHCI_EPCTX_1_MAXP_SIZE_SET(mps);
   4025 	cp[1] |= XHCI_EPCTX_1_MAXB_SET(maxb);
   4026 	cp[4] |= XHCI_EPCTX_4_MAX_ESIT_PAYLOAD_SET(mep & 0xffff);
   4027 	cp[4] |= XHCI_EPCTX_4_AVG_TRB_LEN_SET(atl);
   4028 }
   4029 
   4030 /*
   4031  * Convert usbdi bInterval value to xhci endpoint context interval value
   4032  * for periodic pipe.
   4033  * xHCI 6.2.3.6 Table 65, USB 2.0 9.6.6
   4034  */
   4035 static uint32_t
   4036 xhci_bival2ival(uint32_t ival, uint32_t speed, uint32_t xfertype)
   4037 {
   4038 	if (xfertype != UE_INTERRUPT && xfertype != UE_ISOCHRONOUS)
   4039 		return 0;
   4040 
   4041 	if (xfertype == UE_INTERRUPT &&
   4042 	    (speed == USB_SPEED_LOW || speed == USB_SPEED_FULL)) {
   4043 		u_int i;
   4044 
   4045 		/*
   4046 		 * round ival down to "the nearest base 2 multiple of
   4047 		 * bInterval * 8".
   4048 		 * bInterval is at most 255 as its type is uByte.
   4049 		 * 255(ms) = 2040(x 125us) < 2^11, so start with 10.
   4050 		 */
   4051 		for (i = 10; i > 0; i--) {
   4052 			if ((ival * 8) >= (1 << i))
   4053 				break;
   4054 		}
   4055 		ival = i;
   4056 
   4057 		/* 3 - 10 */
   4058 		ival = (ival < 3) ? 3 : ival;
   4059 	} else if (speed == USB_SPEED_FULL) {
   4060 		/* FS isoc */
   4061 		ival += 3;			/* 1ms -> 125us */
   4062 		ival--;				/* Interval = bInterval-1 */
   4063 		/* 3 - 18 */
   4064 		ival = (ival > 18) ? 18 : ival;
   4065 		ival = (ival < 3) ? 3 : ival;
   4066 	} else {
   4067 		/* SS/HS intr/isoc */
   4068 		if (ival > 0)
   4069 			ival--;			/* Interval = bInterval-1 */
   4070 		/* 0 - 15 */
   4071 		ival = (ival > 15) ? 15 : ival;
   4072 	}
   4073 
   4074 	return ival;
   4075 }
   4076 
   4077 /* ----- */
   4078 
   4079 static void
   4080 xhci_noop(struct usbd_pipe *pipe)
   4081 {
   4082 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   4083 }
   4084 
   4085 /*
   4086  * Process root hub request.
   4087  */
   4088 static int
   4089 xhci_roothub_ctrl_locked(struct usbd_bus *bus, usb_device_request_t *req,
   4090     void *buf, int buflen)
   4091 {
   4092 	struct xhci_softc * const sc = XHCI_BUS2SC(bus);
   4093 	usb_port_status_t ps;
   4094 	int l, totlen = 0;
   4095 	uint16_t len, value, index;
   4096 	int port, i;
   4097 	uint32_t v;
   4098 
   4099 	XHCIHIST_FUNC();
   4100 
   4101 	KASSERT(mutex_owned(&sc->sc_rhlock));
   4102 
   4103 	if (sc->sc_dying)
   4104 		return -1;
   4105 
   4106 	size_t bn = bus == &sc->sc_bus ? 0 : 1;
   4107 
   4108 	len = UGETW(req->wLength);
   4109 	value = UGETW(req->wValue);
   4110 	index = UGETW(req->wIndex);
   4111 
   4112 	XHCIHIST_CALLARGS("rhreq: %04jx %04jx %04jx %04jx",
   4113 	    req->bmRequestType | (req->bRequest << 8), value, index, len);
   4114 
   4115 #define C(x,y) ((x) | ((y) << 8))
   4116 	switch (C(req->bRequest, req->bmRequestType)) {
   4117 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
   4118 		DPRINTFN(8, "getdesc: wValue=0x%04jx", value, 0, 0, 0);
   4119 		if (len == 0)
   4120 			break;
   4121 		switch (value) {
   4122 #define sd ((usb_string_descriptor_t *)buf)
   4123 		case C(2, UDESC_STRING):
   4124 			/* Product */
   4125 			totlen = usb_makestrdesc(sd, len, "xHCI root hub");
   4126 			break;
   4127 #undef sd
   4128 		default:
   4129 			/* default from usbroothub */
   4130 			return buflen;
   4131 		}
   4132 		break;
   4133 
   4134 	/* Hub requests */
   4135 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
   4136 		break;
   4137 	/* Clear Port Feature request */
   4138 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER): {
   4139 		const size_t cp = xhci_rhport2ctlrport(sc, bn, index);
   4140 
   4141 		DPRINTFN(4, "UR_CLEAR_PORT_FEAT bp=%jd feat=%jd bus=%jd cp=%jd",
   4142 		    index, value, bn, cp);
   4143 		if (index < 1 || index > sc->sc_rhportcount[bn]) {
   4144 			return -1;
   4145 		}
   4146 		port = XHCI_PORTSC(cp);
   4147 		v = xhci_op_read_4(sc, port);
   4148 		DPRINTFN(4, "portsc=0x%08jx", v, 0, 0, 0);
   4149 		v &= ~XHCI_PS_CLEAR;
   4150 		switch (value) {
   4151 		case UHF_PORT_ENABLE:
   4152 			xhci_op_write_4(sc, port, v & ~XHCI_PS_PED);
   4153 			break;
   4154 		case UHF_PORT_SUSPEND:
   4155 			return -1;
   4156 		case UHF_PORT_POWER:
   4157 			break;
   4158 		case UHF_PORT_TEST:
   4159 		case UHF_PORT_INDICATOR:
   4160 			return -1;
   4161 		case UHF_C_PORT_CONNECTION:
   4162 			xhci_op_write_4(sc, port, v | XHCI_PS_CSC);
   4163 			break;
   4164 		case UHF_C_PORT_ENABLE:
   4165 		case UHF_C_PORT_SUSPEND:
   4166 		case UHF_C_PORT_OVER_CURRENT:
   4167 			return -1;
   4168 		case UHF_C_BH_PORT_RESET:
   4169 			xhci_op_write_4(sc, port, v | XHCI_PS_WRC);
   4170 			break;
   4171 		case UHF_C_PORT_RESET:
   4172 			xhci_op_write_4(sc, port, v | XHCI_PS_PRC);
   4173 			break;
   4174 		case UHF_C_PORT_LINK_STATE:
   4175 			xhci_op_write_4(sc, port, v | XHCI_PS_PLC);
   4176 			break;
   4177 		case UHF_C_PORT_CONFIG_ERROR:
   4178 			xhci_op_write_4(sc, port, v | XHCI_PS_CEC);
   4179 			break;
   4180 		default:
   4181 			return -1;
   4182 		}
   4183 		break;
   4184 	}
   4185 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
   4186 		if (len == 0)
   4187 			break;
   4188 		if ((value & 0xff) != 0) {
   4189 			return -1;
   4190 		}
   4191 		usb_hub_descriptor_t hubd;
   4192 
   4193 		totlen = uimin(buflen, sizeof(hubd));
   4194 		memcpy(&hubd, buf, totlen);
   4195 		hubd.bNbrPorts = sc->sc_rhportcount[bn];
   4196 		USETW(hubd.wHubCharacteristics, UHD_PWR_NO_SWITCH);
   4197 		hubd.bPwrOn2PwrGood = 200;
   4198 		for (i = 0, l = sc->sc_rhportcount[bn]; l > 0; i++, l -= 8) {
   4199 			/* XXX can't find out? */
   4200 			hubd.DeviceRemovable[i++] = 0;
   4201 		}
   4202 		hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
   4203 		totlen = uimin(totlen, hubd.bDescLength);
   4204 		memcpy(buf, &hubd, totlen);
   4205 		break;
   4206 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
   4207 		if (len != 4) {
   4208 			return -1;
   4209 		}
   4210 		memset(buf, 0, len); /* ? XXX */
   4211 		totlen = len;
   4212 		break;
   4213 	/* Get Port Status request */
   4214 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER): {
   4215 		const size_t cp = xhci_rhport2ctlrport(sc, bn, index);
   4216 
   4217 		DPRINTFN(8, "get port status bn=%jd i=%jd cp=%ju",
   4218 		    bn, index, cp, 0);
   4219 		if (index < 1 || index > sc->sc_rhportcount[bn]) {
   4220 			DPRINTFN(5, "bad get port status: index=%jd bn=%jd "
   4221 				    "portcount=%jd",
   4222 			    index, bn, sc->sc_rhportcount[bn], 0);
   4223 			return -1;
   4224 		}
   4225 		if (len != 4) {
   4226 			DPRINTFN(5, "bad get port status: len %jd != 4",
   4227 			    len, 0, 0, 0);
   4228 			return -1;
   4229 		}
   4230 		v = xhci_op_read_4(sc, XHCI_PORTSC(cp));
   4231 		DPRINTFN(4, "getrhportsc %jd 0x%08jx", cp, v, 0, 0);
   4232 		i = xhci_xspeed2psspeed(XHCI_PS_SPEED_GET(v));
   4233 		if (v & XHCI_PS_CCS)	i |= UPS_CURRENT_CONNECT_STATUS;
   4234 		if (v & XHCI_PS_PED)	i |= UPS_PORT_ENABLED;
   4235 		if (v & XHCI_PS_OCA)	i |= UPS_OVERCURRENT_INDICATOR;
   4236 		//if (v & XHCI_PS_SUSP)	i |= UPS_SUSPEND;
   4237 		if (v & XHCI_PS_PR)	i |= UPS_RESET;
   4238 		if (v & XHCI_PS_PP) {
   4239 			if (i & UPS_OTHER_SPEED)
   4240 					i |= UPS_PORT_POWER_SS;
   4241 			else
   4242 					i |= UPS_PORT_POWER;
   4243 		}
   4244 		if (i & UPS_OTHER_SPEED)
   4245 			i |= UPS_PORT_LS_SET(XHCI_PS_PLS_GET(v));
   4246 		if (sc->sc_vendor_port_status)
   4247 			i = sc->sc_vendor_port_status(sc, v, i);
   4248 		USETW(ps.wPortStatus, i);
   4249 		i = 0;
   4250 		if (v & XHCI_PS_CSC)    i |= UPS_C_CONNECT_STATUS;
   4251 		if (v & XHCI_PS_PEC)    i |= UPS_C_PORT_ENABLED;
   4252 		if (v & XHCI_PS_OCC)    i |= UPS_C_OVERCURRENT_INDICATOR;
   4253 		if (v & XHCI_PS_PRC)	i |= UPS_C_PORT_RESET;
   4254 		if (v & XHCI_PS_WRC)	i |= UPS_C_BH_PORT_RESET;
   4255 		if (v & XHCI_PS_PLC)	i |= UPS_C_PORT_LINK_STATE;
   4256 		if (v & XHCI_PS_CEC)	i |= UPS_C_PORT_CONFIG_ERROR;
   4257 		USETW(ps.wPortChange, i);
   4258 		totlen = uimin(len, sizeof(ps));
   4259 		memcpy(buf, &ps, totlen);
   4260 		DPRINTFN(5, "get port status: wPortStatus %#jx wPortChange %#jx"
   4261 			    " totlen %jd",
   4262 		    UGETW(ps.wPortStatus), UGETW(ps.wPortChange), totlen, 0);
   4263 		break;
   4264 	}
   4265 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
   4266 		return -1;
   4267 	case C(UR_SET_HUB_DEPTH, UT_WRITE_CLASS_DEVICE):
   4268 		break;
   4269 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
   4270 		break;
   4271 	/* Set Port Feature request */
   4272 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER): {
   4273 		int optval = (index >> 8) & 0xff;
   4274 		index &= 0xff;
   4275 		if (index < 1 || index > sc->sc_rhportcount[bn]) {
   4276 			return -1;
   4277 		}
   4278 
   4279 		const size_t cp = xhci_rhport2ctlrport(sc, bn, index);
   4280 
   4281 		port = XHCI_PORTSC(cp);
   4282 		v = xhci_op_read_4(sc, port);
   4283 		DPRINTFN(4, "index %jd cp %jd portsc=0x%08jx", index, cp, v, 0);
   4284 		v &= ~XHCI_PS_CLEAR;
   4285 		switch (value) {
   4286 		case UHF_PORT_ENABLE:
   4287 			xhci_op_write_4(sc, port, v | XHCI_PS_PED);
   4288 			break;
   4289 		case UHF_PORT_SUSPEND:
   4290 			/* XXX suspend */
   4291 			break;
   4292 		case UHF_PORT_RESET:
   4293 			xhci_op_write_4(sc, port, v | XHCI_PS_PR);
   4294 			/* Wait for reset to complete. */
   4295 			for (i = 0; i < USB_PORT_ROOT_RESET_DELAY / 10; i++) {
   4296 				if (sc->sc_dying) {
   4297 					return -1;
   4298 				}
   4299 				v = xhci_op_read_4(sc, port);
   4300 				if ((v & XHCI_PS_PR) == 0) {
   4301 					break;
   4302 				}
   4303 				usb_delay_ms(&sc->sc_bus, 10);
   4304 			}
   4305 			break;
   4306 		case UHF_PORT_POWER:
   4307 			/* XXX power control */
   4308 			break;
   4309 		/* XXX more */
   4310 		case UHF_C_PORT_RESET:
   4311 			xhci_op_write_4(sc, port, v | XHCI_PS_PRC);
   4312 			break;
   4313 		case UHF_PORT_U1_TIMEOUT:
   4314 			if (XHCI_PS_SPEED_GET(v) < XHCI_PS_SPEED_SS) {
   4315 				return -1;
   4316 			}
   4317 			port = XHCI_PORTPMSC(cp);
   4318 			v = xhci_op_read_4(sc, port);
   4319 			DPRINTFN(4, "index %jd cp %jd portpmsc=0x%08jx",
   4320 			    index, cp, v, 0);
   4321 			v &= ~XHCI_PM3_U1TO_SET(0xff);
   4322 			v |= XHCI_PM3_U1TO_SET(optval);
   4323 			xhci_op_write_4(sc, port, v);
   4324 			break;
   4325 		case UHF_PORT_U2_TIMEOUT:
   4326 			if (XHCI_PS_SPEED_GET(v) < XHCI_PS_SPEED_SS) {
   4327 				return -1;
   4328 			}
   4329 			port = XHCI_PORTPMSC(cp);
   4330 			v = xhci_op_read_4(sc, port);
   4331 			DPRINTFN(4, "index %jd cp %jd portpmsc=0x%08jx",
   4332 			    index, cp, v, 0);
   4333 			v &= ~XHCI_PM3_U2TO_SET(0xff);
   4334 			v |= XHCI_PM3_U2TO_SET(optval);
   4335 			xhci_op_write_4(sc, port, v);
   4336 			break;
   4337 		default:
   4338 			return -1;
   4339 		}
   4340 	}
   4341 		break;
   4342 	case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
   4343 	case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
   4344 	case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
   4345 	case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
   4346 		break;
   4347 	default:
   4348 		/* default from usbroothub */
   4349 		return buflen;
   4350 	}
   4351 
   4352 	return totlen;
   4353 }
   4354 
   4355 static int
   4356 xhci_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
   4357     void *buf, int buflen)
   4358 {
   4359 	struct xhci_softc *sc = XHCI_BUS2SC(bus);
   4360 	int actlen;
   4361 
   4362 	mutex_enter(&sc->sc_rhlock);
   4363 	actlen = xhci_roothub_ctrl_locked(bus, req, buf, buflen);
   4364 	mutex_exit(&sc->sc_rhlock);
   4365 
   4366 	return actlen;
   4367 }
   4368 
   4369 /* root hub interrupt */
   4370 
   4371 static usbd_status
   4372 xhci_root_intr_transfer(struct usbd_xfer *xfer)
   4373 {
   4374 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   4375 
   4376 	/* Pipe isn't running, start first */
   4377 	return xhci_root_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
   4378 }
   4379 
   4380 /* Wait for roothub port status/change */
   4381 static usbd_status
   4382 xhci_root_intr_start(struct usbd_xfer *xfer)
   4383 {
   4384 	struct xhci_softc * const sc = XHCI_XFER2SC(xfer);
   4385 	const size_t bn = XHCI_XFER2BUS(xfer) == &sc->sc_bus ? 0 : 1;
   4386 
   4387 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   4388 
   4389 	KASSERT(xhci_polling_p(sc) || mutex_owned(&sc->sc_lock));
   4390 
   4391 	if (sc->sc_dying)
   4392 		return USBD_IOERROR;
   4393 
   4394 	KASSERT(sc->sc_intrxfer[bn] == NULL);
   4395 	sc->sc_intrxfer[bn] = xfer;
   4396 	xfer->ux_status = USBD_IN_PROGRESS;
   4397 
   4398 	return USBD_IN_PROGRESS;
   4399 }
   4400 
   4401 static void
   4402 xhci_root_intr_abort(struct usbd_xfer *xfer)
   4403 {
   4404 	struct xhci_softc * const sc = XHCI_XFER2SC(xfer);
   4405 	const size_t bn = XHCI_XFER2BUS(xfer) == &sc->sc_bus ? 0 : 1;
   4406 
   4407 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   4408 
   4409 	KASSERT(mutex_owned(&sc->sc_lock));
   4410 	KASSERT(xfer->ux_pipe->up_intrxfer == xfer);
   4411 
   4412 	/* If xfer has already completed, nothing to do here.  */
   4413 	if (sc->sc_intrxfer[bn] == NULL)
   4414 		return;
   4415 
   4416 	/*
   4417 	 * Otherwise, sc->sc_intrxfer[bn] had better be this transfer.
   4418 	 * Cancel it.
   4419 	 */
   4420 	KASSERT(sc->sc_intrxfer[bn] == xfer);
   4421 	xfer->ux_status = USBD_CANCELLED;
   4422 	usb_transfer_complete(xfer);
   4423 }
   4424 
   4425 static void
   4426 xhci_root_intr_close(struct usbd_pipe *pipe)
   4427 {
   4428 	struct xhci_softc * const sc __diagused = XHCI_PIPE2SC(pipe);
   4429 	const struct usbd_xfer *xfer __diagused = pipe->up_intrxfer;
   4430 	const size_t bn __diagused = XHCI_XFER2BUS(xfer) == &sc->sc_bus ? 0 : 1;
   4431 
   4432 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   4433 
   4434 	KASSERT(mutex_owned(&sc->sc_lock));
   4435 
   4436 	/*
   4437 	 * Caller must guarantee the xfer has completed first, by
   4438 	 * closing the pipe only after normal completion or an abort.
   4439 	 */
   4440 	KASSERT(sc->sc_intrxfer[bn] == NULL);
   4441 }
   4442 
   4443 static void
   4444 xhci_root_intr_done(struct usbd_xfer *xfer)
   4445 {
   4446 	struct xhci_softc * const sc = XHCI_XFER2SC(xfer);
   4447 	const size_t bn = XHCI_XFER2BUS(xfer) == &sc->sc_bus ? 0 : 1;
   4448 
   4449 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   4450 
   4451 	KASSERT(mutex_owned(&sc->sc_lock));
   4452 
   4453 	/* Claim the xfer so it doesn't get completed again.  */
   4454 	KASSERT(sc->sc_intrxfer[bn] == xfer);
   4455 	KASSERT(xfer->ux_status != USBD_IN_PROGRESS);
   4456 	sc->sc_intrxfer[bn] = NULL;
   4457 }
   4458 
   4459 /* -------------- */
   4460 /* device control */
   4461 
   4462 static usbd_status
   4463 xhci_device_ctrl_transfer(struct usbd_xfer *xfer)
   4464 {
   4465 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   4466 
   4467 	/* Pipe isn't running, start first */
   4468 	return xhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
   4469 }
   4470 
   4471 static usbd_status
   4472 xhci_device_ctrl_start(struct usbd_xfer *xfer)
   4473 {
   4474 	struct xhci_softc * const sc = XHCI_XFER2SC(xfer);
   4475 	struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
   4476 	const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
   4477 	struct xhci_ring * const tr = xs->xs_xr[dci];
   4478 	struct xhci_xfer * const xx = XHCI_XFER2XXFER(xfer);
   4479 	usb_device_request_t * const req = &xfer->ux_request;
   4480 	const bool isread = usbd_xfer_isread(xfer);
   4481 	const uint32_t len = UGETW(req->wLength);
   4482 	usb_dma_t * const dma = &xfer->ux_dmabuf;
   4483 	uint64_t parameter;
   4484 	uint32_t status;
   4485 	uint32_t control;
   4486 	u_int i;
   4487 	const bool polling = xhci_polling_p(sc);
   4488 
   4489 	XHCIHIST_FUNC();
   4490 	XHCIHIST_CALLARGS("req: %04jx %04jx %04jx %04jx",
   4491 	    req->bmRequestType | (req->bRequest << 8), UGETW(req->wValue),
   4492 	    UGETW(req->wIndex), UGETW(req->wLength));
   4493 
   4494 	KASSERT(polling || mutex_owned(&sc->sc_lock));
   4495 
   4496 	/* we rely on the bottom bits for extra info */
   4497 	KASSERTMSG(((uintptr_t)xfer & 0x3) == 0x0, "xfer %p", xfer);
   4498 
   4499 	KASSERT((xfer->ux_rqflags & URQ_REQUEST) != 0);
   4500 
   4501 	if (tr->is_halted)
   4502 		goto out;
   4503 
   4504 	i = 0;
   4505 
   4506 	/* setup phase */
   4507 	parameter = le64dec(req); /* to keep USB endian after xhci_trb_put() */
   4508 	status = XHCI_TRB_2_IRQ_SET(0) | XHCI_TRB_2_BYTES_SET(sizeof(*req));
   4509 	control = ((len == 0) ? XHCI_TRB_3_TRT_NONE :
   4510 	     (isread ? XHCI_TRB_3_TRT_IN : XHCI_TRB_3_TRT_OUT)) |
   4511 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SETUP_STAGE) |
   4512 	    XHCI_TRB_3_IDT_BIT;
   4513 	xhci_xfer_put_trb(xx, i++, parameter, status, control);
   4514 
   4515 	if (len != 0) {
   4516 		/* data phase */
   4517 		parameter = DMAADDR(dma, 0);
   4518 		KASSERTMSG(len <= 0x10000, "len %d", len);
   4519 		status = XHCI_TRB_2_IRQ_SET(0) |
   4520 		    XHCI_TRB_2_TDSZ_SET(0) |
   4521 		    XHCI_TRB_2_BYTES_SET(len);
   4522 		control = (isread ? XHCI_TRB_3_DIR_IN : 0) |
   4523 		    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DATA_STAGE) |
   4524 		    (isread ? XHCI_TRB_3_ISP_BIT : 0) |
   4525 		    XHCI_TRB_3_IOC_BIT;
   4526 		xhci_xfer_put_trb(xx, i++, parameter, status, control);
   4527 
   4528 		usb_syncmem(dma, 0, len,
   4529 		    isread ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
   4530 	}
   4531 
   4532 	parameter = 0;
   4533 	status = XHCI_TRB_2_IRQ_SET(0);
   4534 	/* the status stage has inverted direction */
   4535 	control = ((isread && (len > 0)) ? 0 : XHCI_TRB_3_DIR_IN) |
   4536 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STATUS_STAGE) |
   4537 	    XHCI_TRB_3_IOC_BIT;
   4538 	xhci_xfer_put_trb(xx, i++, parameter, status, control);
   4539 
   4540 	if (!polling)
   4541 		mutex_enter(&tr->xr_lock);
   4542 	xhci_ring_put_xfer(sc, tr, xx, i);
   4543 	if (!polling)
   4544 		mutex_exit(&tr->xr_lock);
   4545 
   4546 	xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci);
   4547 
   4548 out:	if (xfer->ux_status == USBD_NOT_STARTED) {
   4549 		xfer->ux_status = USBD_IN_PROGRESS;
   4550 		usbd_xfer_schedule_timeout(xfer);
   4551 	} else {
   4552 		/*
   4553 		 * We must be coming from xhci_pipe_restart -- timeout
   4554 		 * already set up, nothing to do.
   4555 		 */
   4556 	}
   4557 	KASSERT(xfer->ux_status == USBD_IN_PROGRESS);
   4558 
   4559 	return USBD_IN_PROGRESS;
   4560 }
   4561 
   4562 static void
   4563 xhci_device_ctrl_done(struct usbd_xfer *xfer)
   4564 {
   4565 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   4566 	usb_device_request_t *req = &xfer->ux_request;
   4567 	int len = UGETW(req->wLength);
   4568 	int rd = req->bmRequestType & UT_READ;
   4569 
   4570 	if (len)
   4571 		usb_syncmem(&xfer->ux_dmabuf, 0, len,
   4572 		    rd ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   4573 }
   4574 
   4575 static void
   4576 xhci_device_ctrl_abort(struct usbd_xfer *xfer)
   4577 {
   4578 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   4579 
   4580 	usbd_xfer_abort(xfer);
   4581 }
   4582 
   4583 static void
   4584 xhci_device_ctrl_close(struct usbd_pipe *pipe)
   4585 {
   4586 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   4587 
   4588 	xhci_close_pipe(pipe);
   4589 }
   4590 
   4591 /* ------------------ */
   4592 /* device isochronous */
   4593 
   4594 static usbd_status
   4595 xhci_device_isoc_transfer(struct usbd_xfer *xfer)
   4596 {
   4597 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   4598 
   4599 	return xhci_device_isoc_enter(xfer);
   4600 }
   4601 
   4602 static usbd_status
   4603 xhci_device_isoc_enter(struct usbd_xfer *xfer)
   4604 {
   4605 	struct xhci_softc * const sc = XHCI_XFER2SC(xfer);
   4606 	struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
   4607 	const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
   4608 	struct xhci_ring * const tr = xs->xs_xr[dci];
   4609 	struct xhci_xfer * const xx = XHCI_XFER2XXFER(xfer);
   4610 	struct xhci_pipe * const xpipe = (struct xhci_pipe *)xfer->ux_pipe;
   4611 	usb_dma_t * const dma = &xfer->ux_dmabuf;
   4612 	uint64_t parameter;
   4613 	uint32_t status;
   4614 	uint32_t control;
   4615 	uint32_t offs;
   4616 	int i, ival;
   4617 	const bool polling = xhci_polling_p(sc);
   4618 	const uint16_t MPS = UGETW(xfer->ux_pipe->up_endpoint->ue_edesc->wMaxPacketSize);
   4619 	const uint16_t mps = UE_GET_SIZE(MPS);
   4620 	const uint8_t maxb = xpipe->xp_maxb;
   4621 
   4622 	XHCIHIST_FUNC();
   4623 	XHCIHIST_CALLARGS("%#jx slot %ju dci %ju",
   4624 	    (uintptr_t)xfer, xs->xs_idx, dci, 0);
   4625 
   4626 	KASSERT(polling || mutex_owned(&sc->sc_lock));
   4627 
   4628 	if (sc->sc_dying)
   4629 		return USBD_IOERROR;
   4630 
   4631 	KASSERT(xfer->ux_nframes != 0 && xfer->ux_frlengths);
   4632 	KASSERT((xfer->ux_rqflags & URQ_REQUEST) == 0);
   4633 
   4634 	const bool isread = usbd_xfer_isread(xfer);
   4635 	if (xfer->ux_length)
   4636 		usb_syncmem(dma, 0, xfer->ux_length,
   4637 		    isread ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
   4638 
   4639 	ival = xpipe->xp_ival;
   4640 	if (ival >= 0 && ival <= 15)
   4641 		ival = 1 << ival;
   4642 	else
   4643 		ival = 1; /* fake something up */
   4644 
   4645 	const unsigned mfmask = XHCI_MFINDEX_GET(~(uint32_t)0);
   4646 
   4647 	if (xpipe->xp_isoc_next == -1) {
   4648 		uint32_t mfindex = xhci_rt_read_4(sc, XHCI_MFINDEX);
   4649 
   4650 		DPRINTF("mfindex %jx", (uintmax_t)mfindex, 0, 0, 0);
   4651 		mfindex = XHCI_MFINDEX_GET(mfindex);
   4652 
   4653 		/* Start Frame = MFINDEX + IST + 1 */
   4654 		mfindex += sc->sc_isthresh + 1;
   4655 		xpipe->xp_isoc_next = roundup2(mfindex, ival) & mfmask;
   4656 	}
   4657 
   4658 	offs = 0;
   4659 	for (i = 0; i < xfer->ux_nframes; i++) {
   4660 		const uint32_t len = xfer->ux_frlengths[i];
   4661 		const unsigned tdpc = howmany(len, mps);
   4662 		const unsigned tbc = howmany(tdpc, maxb) - 1;
   4663 		const unsigned tlbpc1 = tdpc % maxb;
   4664 		const unsigned tlbpc = tlbpc1 ? tlbpc1 - 1 : maxb - 1;
   4665 		const unsigned frid = xpipe->xp_isoc_next /
   4666 		    USB_UFRAMES_PER_FRAME;
   4667 
   4668 		KASSERTMSG(len <= 0x10000, "len %d", len);
   4669 		parameter = DMAADDR(dma, offs);
   4670 		status = XHCI_TRB_2_IRQ_SET(0) |
   4671 		    XHCI_TRB_2_TDSZ_SET(0) |
   4672 		    XHCI_TRB_2_BYTES_SET(len);
   4673 		control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ISOCH) |
   4674 		    (isread ? XHCI_TRB_3_ISP_BIT : 0) |
   4675 		    XHCI_TRB_3_TBC_SET(tbc) |
   4676 		    XHCI_TRB_3_TLBPC_SET(tlbpc) |
   4677 		    XHCI_TRB_3_IOC_BIT;
   4678 		if (XHCI_HCC_CFC(sc->sc_hcc)) {
   4679 			control |= XHCI_TRB_3_FRID_SET(frid);
   4680 #if 0
   4681 		} else if (xpipe->xp_isoc_next == -1) {
   4682 			control |= XHCI_TRB_3_FRID_SET(frid);
   4683 #endif
   4684 		} else {
   4685 			control |= XHCI_TRB_3_ISO_SIA_BIT;
   4686 		}
   4687 #if 0
   4688 		if (i != xfer->ux_nframes - 1)
   4689 			control |= XHCI_TRB_3_BEI_BIT;
   4690 #endif
   4691 		xhci_xfer_put_trb(xx, i, parameter, status, control);
   4692 
   4693 		xpipe->xp_isoc_next = (xpipe->xp_isoc_next + ival) & mfmask;
   4694 		offs += len;
   4695 	}
   4696 
   4697 	xx->xx_isoc_done = 0;
   4698 
   4699 	if (!polling)
   4700 		mutex_enter(&tr->xr_lock);
   4701 	xhci_ring_put_xfer(sc, tr, xx, i);
   4702 	if (!polling)
   4703 		mutex_exit(&tr->xr_lock);
   4704 
   4705 	xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci);
   4706 	xfer->ux_status = USBD_IN_PROGRESS;
   4707 	usbd_xfer_schedule_timeout(xfer);
   4708 
   4709 	return USBD_IN_PROGRESS;
   4710 }
   4711 
   4712 static void
   4713 xhci_device_isoc_abort(struct usbd_xfer *xfer)
   4714 {
   4715 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   4716 
   4717 	usbd_xfer_abort(xfer);
   4718 }
   4719 
   4720 static void
   4721 xhci_device_isoc_close(struct usbd_pipe *pipe)
   4722 {
   4723 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   4724 
   4725 	xhci_close_pipe(pipe);
   4726 }
   4727 
   4728 static void
   4729 xhci_device_isoc_done(struct usbd_xfer *xfer)
   4730 {
   4731 #ifdef USB_DEBUG
   4732 	struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
   4733 	const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
   4734 #endif
   4735 	const bool isread = usbd_xfer_isread(xfer);
   4736 
   4737 	XHCIHIST_FUNC();
   4738 	XHCIHIST_CALLARGS("%#jx slot %ju dci %ju",
   4739 	    (uintptr_t)xfer, xs->xs_idx, dci, 0);
   4740 
   4741 	usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
   4742 	    isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   4743 }
   4744 
   4745 /* ----------- */
   4746 /* device bulk */
   4747 
   4748 static usbd_status
   4749 xhci_device_bulk_transfer(struct usbd_xfer *xfer)
   4750 {
   4751 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   4752 
   4753 	/* Pipe isn't running, so start it first.  */
   4754 	return xhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
   4755 }
   4756 
   4757 static usbd_status
   4758 xhci_device_bulk_start(struct usbd_xfer *xfer)
   4759 {
   4760 	struct xhci_softc * const sc = XHCI_XFER2SC(xfer);
   4761 	struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
   4762 	const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
   4763 	struct xhci_ring * const tr = xs->xs_xr[dci];
   4764 	struct xhci_xfer * const xx = XHCI_XFER2XXFER(xfer);
   4765 	const uint32_t len = xfer->ux_length;
   4766 	usb_dma_t * const dma = &xfer->ux_dmabuf;
   4767 	uint64_t parameter;
   4768 	uint32_t status;
   4769 	uint32_t control;
   4770 	u_int i = 0;
   4771 	const bool polling = xhci_polling_p(sc);
   4772 
   4773 	XHCIHIST_FUNC();
   4774 	XHCIHIST_CALLARGS("%#jx slot %ju dci %ju",
   4775 	    (uintptr_t)xfer, xs->xs_idx, dci, 0);
   4776 
   4777 	KASSERT(polling || mutex_owned(&sc->sc_lock));
   4778 
   4779 	if (sc->sc_dying)
   4780 		return USBD_IOERROR;
   4781 
   4782 	KASSERT((xfer->ux_rqflags & URQ_REQUEST) == 0);
   4783 
   4784 	if (tr->is_halted)
   4785 		goto out;
   4786 
   4787 	parameter = DMAADDR(dma, 0);
   4788 	const bool isread = usbd_xfer_isread(xfer);
   4789 	if (len)
   4790 		usb_syncmem(dma, 0, len,
   4791 		    isread ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
   4792 
   4793 	/*
   4794 	 * XXX: (dsl) The physical buffer must not cross a 64k boundary.
   4795 	 * If the user supplied buffer crosses such a boundary then 2
   4796 	 * (or more) TRB should be used.
   4797 	 * If multiple TRB are used the td_size field must be set correctly.
   4798 	 * For v1.0 devices (like ivy bridge) this is the number of usb data
   4799 	 * blocks needed to complete the transfer.
   4800 	 * Setting it to 1 in the last TRB causes an extra zero-length
   4801 	 * data block be sent.
   4802 	 * The earlier documentation differs, I don't know how it behaves.
   4803 	 */
   4804 	KASSERTMSG(len <= 0x10000, "len %d", len);
   4805 	status = XHCI_TRB_2_IRQ_SET(0) |
   4806 	    XHCI_TRB_2_TDSZ_SET(0) |
   4807 	    XHCI_TRB_2_BYTES_SET(len);
   4808 	control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL) |
   4809 	    (isread ? XHCI_TRB_3_ISP_BIT : 0) |
   4810 	    XHCI_TRB_3_IOC_BIT;
   4811 	xhci_xfer_put_trb(xx, i++, parameter, status, control);
   4812 
   4813 	if (!isread && (xfer->ux_flags & USBD_FORCE_SHORT_XFER)) {
   4814 		status = XHCI_TRB_2_IRQ_SET(0) |
   4815 		    XHCI_TRB_2_TDSZ_SET(0) |
   4816 		    XHCI_TRB_2_BYTES_SET(0);
   4817 		xhci_xfer_put_trb(xx, i++, parameter, status, control);
   4818 	}
   4819 
   4820 	if (!polling)
   4821 		mutex_enter(&tr->xr_lock);
   4822 	xhci_ring_put_xfer(sc, tr, xx, i);
   4823 	if (!polling)
   4824 		mutex_exit(&tr->xr_lock);
   4825 
   4826 	xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci);
   4827 
   4828 out:	if (xfer->ux_status == USBD_NOT_STARTED) {
   4829 		xfer->ux_status = USBD_IN_PROGRESS;
   4830 		usbd_xfer_schedule_timeout(xfer);
   4831 	} else {
   4832 		/*
   4833 		 * We must be coming from xhci_pipe_restart -- timeout
   4834 		 * already set up, nothing to do.
   4835 		 */
   4836 	}
   4837 	KASSERT(xfer->ux_status == USBD_IN_PROGRESS);
   4838 
   4839 	return USBD_IN_PROGRESS;
   4840 }
   4841 
   4842 static void
   4843 xhci_device_bulk_done(struct usbd_xfer *xfer)
   4844 {
   4845 #ifdef USB_DEBUG
   4846 	struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
   4847 	const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
   4848 #endif
   4849 	const bool isread = usbd_xfer_isread(xfer);
   4850 
   4851 	XHCIHIST_FUNC();
   4852 	XHCIHIST_CALLARGS("%#jx slot %ju dci %ju",
   4853 	    (uintptr_t)xfer, xs->xs_idx, dci, 0);
   4854 
   4855 	usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
   4856 	    isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   4857 }
   4858 
   4859 static void
   4860 xhci_device_bulk_abort(struct usbd_xfer *xfer)
   4861 {
   4862 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   4863 
   4864 	usbd_xfer_abort(xfer);
   4865 }
   4866 
   4867 static void
   4868 xhci_device_bulk_close(struct usbd_pipe *pipe)
   4869 {
   4870 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   4871 
   4872 	xhci_close_pipe(pipe);
   4873 }
   4874 
   4875 /* ---------------- */
   4876 /* device interrupt */
   4877 
   4878 static usbd_status
   4879 xhci_device_intr_transfer(struct usbd_xfer *xfer)
   4880 {
   4881 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   4882 
   4883 	/* Pipe isn't running, so start it first.  */
   4884 	return xhci_device_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
   4885 }
   4886 
   4887 static usbd_status
   4888 xhci_device_intr_start(struct usbd_xfer *xfer)
   4889 {
   4890 	struct xhci_softc * const sc = XHCI_XFER2SC(xfer);
   4891 	struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
   4892 	const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
   4893 	struct xhci_ring * const tr = xs->xs_xr[dci];
   4894 	struct xhci_xfer * const xx = XHCI_XFER2XXFER(xfer);
   4895 	const uint32_t len = xfer->ux_length;
   4896 	const bool polling = xhci_polling_p(sc);
   4897 	usb_dma_t * const dma = &xfer->ux_dmabuf;
   4898 	uint64_t parameter;
   4899 	uint32_t status;
   4900 	uint32_t control;
   4901 	u_int i = 0;
   4902 
   4903 	XHCIHIST_FUNC();
   4904 	XHCIHIST_CALLARGS("%#jx slot %ju dci %ju",
   4905 	    (uintptr_t)xfer, xs->xs_idx, dci, 0);
   4906 
   4907 	KASSERT(polling || mutex_owned(&sc->sc_lock));
   4908 
   4909 	if (sc->sc_dying)
   4910 		return USBD_IOERROR;
   4911 
   4912 	if (tr->is_halted)
   4913 		goto out;
   4914 
   4915 	KASSERT((xfer->ux_rqflags & URQ_REQUEST) == 0);
   4916 
   4917 	const bool isread = usbd_xfer_isread(xfer);
   4918 	if (len)
   4919 		usb_syncmem(dma, 0, len,
   4920 		    isread ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
   4921 
   4922 	parameter = DMAADDR(dma, 0);
   4923 	KASSERTMSG(len <= 0x10000, "len %d", len);
   4924 	status = XHCI_TRB_2_IRQ_SET(0) |
   4925 	    XHCI_TRB_2_TDSZ_SET(0) |
   4926 	    XHCI_TRB_2_BYTES_SET(len);
   4927 	control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL) |
   4928 	    (isread ? XHCI_TRB_3_ISP_BIT : 0) | XHCI_TRB_3_IOC_BIT;
   4929 	xhci_xfer_put_trb(xx, i++, parameter, status, control);
   4930 
   4931 	if (!polling)
   4932 		mutex_enter(&tr->xr_lock);
   4933 	xhci_ring_put_xfer(sc, tr, xx, i);
   4934 	if (!polling)
   4935 		mutex_exit(&tr->xr_lock);
   4936 
   4937 	xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci);
   4938 
   4939 out:	if (xfer->ux_status == USBD_NOT_STARTED) {
   4940 		xfer->ux_status = USBD_IN_PROGRESS;
   4941 		usbd_xfer_schedule_timeout(xfer);
   4942 	} else {
   4943 		/*
   4944 		 * We must be coming from xhci_pipe_restart -- timeout
   4945 		 * already set up, nothing to do.
   4946 		 */
   4947 	}
   4948 	KASSERT(xfer->ux_status == USBD_IN_PROGRESS);
   4949 
   4950 	return USBD_IN_PROGRESS;
   4951 }
   4952 
   4953 static void
   4954 xhci_device_intr_done(struct usbd_xfer *xfer)
   4955 {
   4956 	struct xhci_softc * const sc __diagused = XHCI_XFER2SC(xfer);
   4957 #ifdef USB_DEBUG
   4958 	struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
   4959 	const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
   4960 #endif
   4961 	const bool isread = usbd_xfer_isread(xfer);
   4962 
   4963 	XHCIHIST_FUNC();
   4964 	XHCIHIST_CALLARGS("%#jx slot %ju dci %ju",
   4965 	    (uintptr_t)xfer, xs->xs_idx, dci, 0);
   4966 
   4967 	KASSERT(xhci_polling_p(sc) || mutex_owned(&sc->sc_lock));
   4968 
   4969 	usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
   4970 	    isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   4971 }
   4972 
   4973 static void
   4974 xhci_device_intr_abort(struct usbd_xfer *xfer)
   4975 {
   4976 	struct xhci_softc * const sc __diagused = XHCI_XFER2SC(xfer);
   4977 
   4978 	XHCIHIST_FUNC();
   4979 	XHCIHIST_CALLARGS("%#jx", (uintptr_t)xfer, 0, 0, 0);
   4980 
   4981 	KASSERT(mutex_owned(&sc->sc_lock));
   4982 	usbd_xfer_abort(xfer);
   4983 }
   4984 
   4985 static void
   4986 xhci_device_intr_close(struct usbd_pipe *pipe)
   4987 {
   4988 	//struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
   4989 
   4990 	XHCIHIST_FUNC();
   4991 	XHCIHIST_CALLARGS("%#jx", (uintptr_t)pipe, 0, 0, 0);
   4992 
   4993 	xhci_close_pipe(pipe);
   4994 }
   4995