Home | History | Annotate | Line # | Download | only in usb
xhci.c revision 1.95
      1 /*	$NetBSD: xhci.c,v 1.95 2018/07/18 10:44:17 msaitoh 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.95 2018/07/18 10:44:17 msaitoh 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 DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(xhcidebug,N,FMT,A,B,C,D)
    118 #define XHCIHIST_FUNC() USBHIST_FUNC()
    119 #define XHCIHIST_CALLED(name) USBHIST_CALLED(xhcidebug)
    120 
    121 #define XHCI_DCI_SLOT 0
    122 #define XHCI_DCI_EP_CONTROL 1
    123 
    124 #define XHCI_ICI_INPUT_CONTROL 0
    125 
    126 struct xhci_pipe {
    127 	struct usbd_pipe xp_pipe;
    128 	struct usb_task xp_async_task;
    129 };
    130 
    131 #define XHCI_COMMAND_RING_TRBS 256
    132 #define XHCI_EVENT_RING_TRBS 256
    133 #define XHCI_EVENT_RING_SEGMENTS 1
    134 #define XHCI_TRB_3_ED_BIT XHCI_TRB_3_ISP_BIT
    135 
    136 static usbd_status xhci_open(struct usbd_pipe *);
    137 static void xhci_close_pipe(struct usbd_pipe *);
    138 static int xhci_intr1(struct xhci_softc * const);
    139 static void xhci_softintr(void *);
    140 static void xhci_poll(struct usbd_bus *);
    141 static struct usbd_xfer *xhci_allocx(struct usbd_bus *, unsigned int);
    142 static void xhci_freex(struct usbd_bus *, struct usbd_xfer *);
    143 static void xhci_get_lock(struct usbd_bus *, kmutex_t **);
    144 static usbd_status xhci_new_device(device_t, struct usbd_bus *, int, int, int,
    145     struct usbd_port *);
    146 static int xhci_roothub_ctrl(struct usbd_bus *, usb_device_request_t *,
    147     void *, int);
    148 
    149 static usbd_status xhci_configure_endpoint(struct usbd_pipe *);
    150 //static usbd_status xhci_unconfigure_endpoint(struct usbd_pipe *);
    151 static usbd_status xhci_reset_endpoint(struct usbd_pipe *);
    152 static usbd_status xhci_stop_endpoint(struct usbd_pipe *);
    153 
    154 static void xhci_host_dequeue(struct xhci_ring * const);
    155 static usbd_status xhci_set_dequeue(struct usbd_pipe *);
    156 
    157 static usbd_status xhci_do_command(struct xhci_softc * const,
    158     struct xhci_trb * const, int);
    159 static usbd_status xhci_do_command_locked(struct xhci_softc * const,
    160     struct xhci_trb * const, int);
    161 static usbd_status xhci_init_slot(struct usbd_device *, uint32_t);
    162 static void xhci_free_slot(struct xhci_softc *, struct xhci_slot *, int, int);
    163 static usbd_status xhci_set_address(struct usbd_device *, uint32_t, bool);
    164 static usbd_status xhci_enable_slot(struct xhci_softc * const,
    165     uint8_t * const);
    166 static usbd_status xhci_disable_slot(struct xhci_softc * const, uint8_t);
    167 static usbd_status xhci_address_device(struct xhci_softc * const,
    168     uint64_t, uint8_t, bool);
    169 static void xhci_set_dcba(struct xhci_softc * const, uint64_t, int);
    170 static usbd_status xhci_update_ep0_mps(struct xhci_softc * const,
    171     struct xhci_slot * const, u_int);
    172 static usbd_status xhci_ring_init(struct xhci_softc * const,
    173     struct xhci_ring * const, size_t, size_t);
    174 static void xhci_ring_free(struct xhci_softc * const, struct xhci_ring * const);
    175 
    176 static void xhci_setup_ctx(struct usbd_pipe *);
    177 static void xhci_setup_route(struct usbd_pipe *, uint32_t *);
    178 static void xhci_setup_tthub(struct usbd_pipe *, uint32_t *);
    179 static void xhci_setup_maxburst(struct usbd_pipe *, uint32_t *);
    180 static uint32_t xhci_bival2ival(uint32_t, uint32_t);
    181 
    182 static void xhci_noop(struct usbd_pipe *);
    183 
    184 static usbd_status xhci_root_intr_transfer(struct usbd_xfer *);
    185 static usbd_status xhci_root_intr_start(struct usbd_xfer *);
    186 static void xhci_root_intr_abort(struct usbd_xfer *);
    187 static void xhci_root_intr_close(struct usbd_pipe *);
    188 static void xhci_root_intr_done(struct usbd_xfer *);
    189 
    190 static usbd_status xhci_device_ctrl_transfer(struct usbd_xfer *);
    191 static usbd_status xhci_device_ctrl_start(struct usbd_xfer *);
    192 static void xhci_device_ctrl_abort(struct usbd_xfer *);
    193 static void xhci_device_ctrl_close(struct usbd_pipe *);
    194 static void xhci_device_ctrl_done(struct usbd_xfer *);
    195 
    196 static usbd_status xhci_device_intr_transfer(struct usbd_xfer *);
    197 static usbd_status xhci_device_intr_start(struct usbd_xfer *);
    198 static void xhci_device_intr_abort(struct usbd_xfer *);
    199 static void xhci_device_intr_close(struct usbd_pipe *);
    200 static void xhci_device_intr_done(struct usbd_xfer *);
    201 
    202 static usbd_status xhci_device_bulk_transfer(struct usbd_xfer *);
    203 static usbd_status xhci_device_bulk_start(struct usbd_xfer *);
    204 static void xhci_device_bulk_abort(struct usbd_xfer *);
    205 static void xhci_device_bulk_close(struct usbd_pipe *);
    206 static void xhci_device_bulk_done(struct usbd_xfer *);
    207 
    208 static void xhci_timeout(void *);
    209 static void xhci_timeout_task(void *);
    210 
    211 static const struct usbd_bus_methods xhci_bus_methods = {
    212 	.ubm_open = xhci_open,
    213 	.ubm_softint = xhci_softintr,
    214 	.ubm_dopoll = xhci_poll,
    215 	.ubm_allocx = xhci_allocx,
    216 	.ubm_freex = xhci_freex,
    217 	.ubm_getlock = xhci_get_lock,
    218 	.ubm_newdev = xhci_new_device,
    219 	.ubm_rhctrl = xhci_roothub_ctrl,
    220 };
    221 
    222 static const struct usbd_pipe_methods xhci_root_intr_methods = {
    223 	.upm_transfer = xhci_root_intr_transfer,
    224 	.upm_start = xhci_root_intr_start,
    225 	.upm_abort = xhci_root_intr_abort,
    226 	.upm_close = xhci_root_intr_close,
    227 	.upm_cleartoggle = xhci_noop,
    228 	.upm_done = xhci_root_intr_done,
    229 };
    230 
    231 
    232 static const struct usbd_pipe_methods xhci_device_ctrl_methods = {
    233 	.upm_transfer = xhci_device_ctrl_transfer,
    234 	.upm_start = xhci_device_ctrl_start,
    235 	.upm_abort = xhci_device_ctrl_abort,
    236 	.upm_close = xhci_device_ctrl_close,
    237 	.upm_cleartoggle = xhci_noop,
    238 	.upm_done = xhci_device_ctrl_done,
    239 };
    240 
    241 static const struct usbd_pipe_methods xhci_device_isoc_methods = {
    242 	.upm_cleartoggle = xhci_noop,
    243 };
    244 
    245 static const struct usbd_pipe_methods xhci_device_bulk_methods = {
    246 	.upm_transfer = xhci_device_bulk_transfer,
    247 	.upm_start = xhci_device_bulk_start,
    248 	.upm_abort = xhci_device_bulk_abort,
    249 	.upm_close = xhci_device_bulk_close,
    250 	.upm_cleartoggle = xhci_noop,
    251 	.upm_done = xhci_device_bulk_done,
    252 };
    253 
    254 static const struct usbd_pipe_methods xhci_device_intr_methods = {
    255 	.upm_transfer = xhci_device_intr_transfer,
    256 	.upm_start = xhci_device_intr_start,
    257 	.upm_abort = xhci_device_intr_abort,
    258 	.upm_close = xhci_device_intr_close,
    259 	.upm_cleartoggle = xhci_noop,
    260 	.upm_done = xhci_device_intr_done,
    261 };
    262 
    263 static inline uint32_t
    264 xhci_read_1(const struct xhci_softc * const sc, bus_size_t offset)
    265 {
    266 	return bus_space_read_1(sc->sc_iot, sc->sc_ioh, offset);
    267 }
    268 
    269 static inline uint32_t
    270 xhci_read_4(const struct xhci_softc * const sc, bus_size_t offset)
    271 {
    272 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh, offset);
    273 }
    274 
    275 static inline void
    276 xhci_write_1(const struct xhci_softc * const sc, bus_size_t offset,
    277     uint32_t value)
    278 {
    279 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, offset, value);
    280 }
    281 
    282 #if 0 /* unused */
    283 static inline void
    284 xhci_write_4(const struct xhci_softc * const sc, bus_size_t offset,
    285     uint32_t value)
    286 {
    287 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, offset, value);
    288 }
    289 #endif /* unused */
    290 
    291 static inline uint32_t
    292 xhci_cap_read_4(const struct xhci_softc * const sc, bus_size_t offset)
    293 {
    294 	return bus_space_read_4(sc->sc_iot, sc->sc_cbh, offset);
    295 }
    296 
    297 static inline uint32_t
    298 xhci_op_read_4(const struct xhci_softc * const sc, bus_size_t offset)
    299 {
    300 	return bus_space_read_4(sc->sc_iot, sc->sc_obh, offset);
    301 }
    302 
    303 static inline void
    304 xhci_op_write_4(const struct xhci_softc * const sc, bus_size_t offset,
    305     uint32_t value)
    306 {
    307 	bus_space_write_4(sc->sc_iot, sc->sc_obh, offset, value);
    308 }
    309 
    310 static inline uint64_t
    311 xhci_op_read_8(const struct xhci_softc * const sc, bus_size_t offset)
    312 {
    313 	uint64_t value;
    314 
    315 	if (sc->sc_ac64) {
    316 #ifdef XHCI_USE_BUS_SPACE_8
    317 		value = bus_space_read_8(sc->sc_iot, sc->sc_obh, offset);
    318 #else
    319 		value = bus_space_read_4(sc->sc_iot, sc->sc_obh, offset);
    320 		value |= (uint64_t)bus_space_read_4(sc->sc_iot, sc->sc_obh,
    321 		    offset + 4) << 32;
    322 #endif
    323 	} else {
    324 		value = bus_space_read_4(sc->sc_iot, sc->sc_obh, offset);
    325 	}
    326 
    327 	return value;
    328 }
    329 
    330 static inline void
    331 xhci_op_write_8(const struct xhci_softc * const sc, bus_size_t offset,
    332     uint64_t value)
    333 {
    334 	if (sc->sc_ac64) {
    335 #ifdef XHCI_USE_BUS_SPACE_8
    336 		bus_space_write_8(sc->sc_iot, sc->sc_obh, offset, value);
    337 #else
    338 		bus_space_write_4(sc->sc_iot, sc->sc_obh, offset + 0,
    339 		    (value >> 0) & 0xffffffff);
    340 		bus_space_write_4(sc->sc_iot, sc->sc_obh, offset + 4,
    341 		    (value >> 32) & 0xffffffff);
    342 #endif
    343 	} else {
    344 		bus_space_write_4(sc->sc_iot, sc->sc_obh, offset, value);
    345 	}
    346 }
    347 
    348 static inline uint32_t
    349 xhci_rt_read_4(const struct xhci_softc * const sc, bus_size_t offset)
    350 {
    351 	return bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset);
    352 }
    353 
    354 static inline void
    355 xhci_rt_write_4(const struct xhci_softc * const sc, bus_size_t offset,
    356     uint32_t value)
    357 {
    358 	bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset, value);
    359 }
    360 
    361 #if 0 /* unused */
    362 static inline uint64_t
    363 xhci_rt_read_8(const struct xhci_softc * const sc, bus_size_t offset)
    364 {
    365 	uint64_t value;
    366 
    367 	if (sc->sc_ac64) {
    368 #ifdef XHCI_USE_BUS_SPACE_8
    369 		value = bus_space_read_8(sc->sc_iot, sc->sc_rbh, offset);
    370 #else
    371 		value = bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset);
    372 		value |= (uint64_t)bus_space_read_4(sc->sc_iot, sc->sc_rbh,
    373 		    offset + 4) << 32;
    374 #endif
    375 	} else {
    376 		value = bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset);
    377 	}
    378 
    379 	return value;
    380 }
    381 #endif /* unused */
    382 
    383 static inline void
    384 xhci_rt_write_8(const struct xhci_softc * const sc, bus_size_t offset,
    385     uint64_t value)
    386 {
    387 	if (sc->sc_ac64) {
    388 #ifdef XHCI_USE_BUS_SPACE_8
    389 		bus_space_write_8(sc->sc_iot, sc->sc_rbh, offset, value);
    390 #else
    391 		bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset + 0,
    392 		    (value >> 0) & 0xffffffff);
    393 		bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset + 4,
    394 		    (value >> 32) & 0xffffffff);
    395 #endif
    396 	} else {
    397 		bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset, value);
    398 	}
    399 }
    400 
    401 #if 0 /* unused */
    402 static inline uint32_t
    403 xhci_db_read_4(const struct xhci_softc * const sc, bus_size_t offset)
    404 {
    405 	return bus_space_read_4(sc->sc_iot, sc->sc_dbh, offset);
    406 }
    407 #endif /* unused */
    408 
    409 static inline void
    410 xhci_db_write_4(const struct xhci_softc * const sc, bus_size_t offset,
    411     uint32_t value)
    412 {
    413 	bus_space_write_4(sc->sc_iot, sc->sc_dbh, offset, value);
    414 }
    415 
    416 /* --- */
    417 
    418 static inline uint8_t
    419 xhci_ep_get_type(usb_endpoint_descriptor_t * const ed)
    420 {
    421 	u_int eptype = 0;
    422 
    423 	switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
    424 	case UE_CONTROL:
    425 		eptype = 0x0;
    426 		break;
    427 	case UE_ISOCHRONOUS:
    428 		eptype = 0x1;
    429 		break;
    430 	case UE_BULK:
    431 		eptype = 0x2;
    432 		break;
    433 	case UE_INTERRUPT:
    434 		eptype = 0x3;
    435 		break;
    436 	}
    437 
    438 	if ((UE_GET_XFERTYPE(ed->bmAttributes) == UE_CONTROL) ||
    439 	    (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN))
    440 		return eptype | 0x4;
    441 	else
    442 		return eptype;
    443 }
    444 
    445 static u_int
    446 xhci_ep_get_dci(usb_endpoint_descriptor_t * const ed)
    447 {
    448 	/* xHCI 1.0 section 4.5.1 */
    449 	u_int epaddr = UE_GET_ADDR(ed->bEndpointAddress);
    450 	u_int in = 0;
    451 
    452 	if ((UE_GET_XFERTYPE(ed->bmAttributes) == UE_CONTROL) ||
    453 	    (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN))
    454 		in = 1;
    455 
    456 	return epaddr * 2 + in;
    457 }
    458 
    459 static inline u_int
    460 xhci_dci_to_ici(const u_int i)
    461 {
    462 	return i + 1;
    463 }
    464 
    465 static inline void *
    466 xhci_slot_get_dcv(struct xhci_softc * const sc, struct xhci_slot * const xs,
    467     const u_int dci)
    468 {
    469 	return KERNADDR(&xs->xs_dc_dma, sc->sc_ctxsz * dci);
    470 }
    471 
    472 #if 0 /* unused */
    473 static inline bus_addr_t
    474 xhci_slot_get_dcp(struct xhci_softc * const sc, struct xhci_slot * const xs,
    475     const u_int dci)
    476 {
    477 	return DMAADDR(&xs->xs_dc_dma, sc->sc_ctxsz * dci);
    478 }
    479 #endif /* unused */
    480 
    481 static inline void *
    482 xhci_slot_get_icv(struct xhci_softc * const sc, struct xhci_slot * const xs,
    483     const u_int ici)
    484 {
    485 	return KERNADDR(&xs->xs_ic_dma, sc->sc_ctxsz * ici);
    486 }
    487 
    488 static inline bus_addr_t
    489 xhci_slot_get_icp(struct xhci_softc * const sc, struct xhci_slot * const xs,
    490     const u_int ici)
    491 {
    492 	return DMAADDR(&xs->xs_ic_dma, sc->sc_ctxsz * ici);
    493 }
    494 
    495 static inline struct xhci_trb *
    496 xhci_ring_trbv(struct xhci_ring * const xr, u_int idx)
    497 {
    498 	return KERNADDR(&xr->xr_dma, XHCI_TRB_SIZE * idx);
    499 }
    500 
    501 static inline bus_addr_t
    502 xhci_ring_trbp(struct xhci_ring * const xr, u_int idx)
    503 {
    504 	return DMAADDR(&xr->xr_dma, XHCI_TRB_SIZE * idx);
    505 }
    506 
    507 static inline void
    508 xhci_trb_put(struct xhci_trb * const trb, uint64_t parameter, uint32_t status,
    509     uint32_t control)
    510 {
    511 	trb->trb_0 = htole64(parameter);
    512 	trb->trb_2 = htole32(status);
    513 	trb->trb_3 = htole32(control);
    514 }
    515 
    516 static int
    517 xhci_trb_get_idx(struct xhci_ring *xr, uint64_t trb_0, int *idx)
    518 {
    519 	/* base address of TRBs */
    520 	bus_addr_t trbp = xhci_ring_trbp(xr, 0);
    521 
    522 	/* trb_0 range sanity check */
    523 	if (trb_0 == 0 || trb_0 < trbp ||
    524 	    (trb_0 - trbp) % sizeof(struct xhci_trb) != 0 ||
    525 	    (trb_0 - trbp) / sizeof(struct xhci_trb) >= xr->xr_ntrb) {
    526 		return 1;
    527 	}
    528 	*idx = (trb_0 - trbp) / sizeof(struct xhci_trb);
    529 	return 0;
    530 }
    531 
    532 static unsigned int
    533 xhci_get_epstate(struct xhci_softc * const sc, struct xhci_slot * const xs,
    534     u_int dci)
    535 {
    536 	uint32_t *cp;
    537 
    538 	usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
    539 	cp = xhci_slot_get_dcv(sc, xs, dci);
    540 	return XHCI_EPCTX_0_EPSTATE_GET(le32toh(cp[0]));
    541 }
    542 
    543 static inline unsigned int
    544 xhci_ctlrport2bus(struct xhci_softc * const sc, unsigned int ctlrport)
    545 {
    546 	const unsigned int port = ctlrport - 1;
    547 	const uint8_t bit = __BIT(port % NBBY);
    548 
    549 	return __SHIFTOUT(sc->sc_ctlrportbus[port / NBBY], bit);
    550 }
    551 
    552 /*
    553  * Return the roothub port for a controller port.  Both are 1..n.
    554  */
    555 static inline unsigned int
    556 xhci_ctlrport2rhport(struct xhci_softc * const sc, unsigned int ctrlport)
    557 {
    558 
    559 	return sc->sc_ctlrportmap[ctrlport - 1];
    560 }
    561 
    562 /*
    563  * Return the controller port for a bus roothub port.  Both are 1..n.
    564  */
    565 static inline unsigned int
    566 xhci_rhport2ctlrport(struct xhci_softc * const sc, unsigned int bn,
    567     unsigned int rhport)
    568 {
    569 
    570 	return sc->sc_rhportmap[bn][rhport - 1];
    571 }
    572 
    573 /* --- */
    574 
    575 void
    576 xhci_childdet(device_t self, device_t child)
    577 {
    578 	struct xhci_softc * const sc = device_private(self);
    579 
    580 	KASSERT((sc->sc_child == child) || (sc->sc_child2 == child));
    581 	if (child == sc->sc_child2)
    582 		sc->sc_child2 = NULL;
    583 	else if (child == sc->sc_child)
    584 		sc->sc_child = NULL;
    585 }
    586 
    587 int
    588 xhci_detach(struct xhci_softc *sc, int flags)
    589 {
    590 	int rv = 0;
    591 
    592 	if (sc->sc_child2 != NULL) {
    593 		rv = config_detach(sc->sc_child2, flags);
    594 		if (rv != 0)
    595 			return rv;
    596 		KASSERT(sc->sc_child2 == NULL);
    597 	}
    598 
    599 	if (sc->sc_child != NULL) {
    600 		rv = config_detach(sc->sc_child, flags);
    601 		if (rv != 0)
    602 			return rv;
    603 		KASSERT(sc->sc_child == NULL);
    604 	}
    605 
    606 	/* XXX unconfigure/free slots */
    607 
    608 	/* verify: */
    609 	xhci_rt_write_4(sc, XHCI_IMAN(0), 0);
    610 	xhci_op_write_4(sc, XHCI_USBCMD, 0);
    611 	/* do we need to wait for stop? */
    612 
    613 	xhci_op_write_8(sc, XHCI_CRCR, 0);
    614 	xhci_ring_free(sc, &sc->sc_cr);
    615 	cv_destroy(&sc->sc_command_cv);
    616 	cv_destroy(&sc->sc_cmdbusy_cv);
    617 
    618 	xhci_rt_write_4(sc, XHCI_ERSTSZ(0), 0);
    619 	xhci_rt_write_8(sc, XHCI_ERSTBA(0), 0);
    620 	xhci_rt_write_8(sc, XHCI_ERDP(0), 0|XHCI_ERDP_LO_BUSY);
    621 	xhci_ring_free(sc, &sc->sc_er);
    622 
    623 	usb_freemem(&sc->sc_bus, &sc->sc_eventst_dma);
    624 
    625 	xhci_op_write_8(sc, XHCI_DCBAAP, 0);
    626 	usb_freemem(&sc->sc_bus, &sc->sc_dcbaa_dma);
    627 
    628 	kmem_free(sc->sc_slots, sizeof(*sc->sc_slots) * sc->sc_maxslots);
    629 
    630 	kmem_free(sc->sc_ctlrportbus,
    631 	    howmany(sc->sc_maxports * sizeof(uint8_t), NBBY));
    632 	kmem_free(sc->sc_ctlrportmap, sc->sc_maxports * sizeof(int));
    633 
    634 	for (size_t j = 0; j < __arraycount(sc->sc_rhportmap); j++) {
    635 		kmem_free(sc->sc_rhportmap[j], sc->sc_maxports * sizeof(int));
    636 	}
    637 
    638 	mutex_destroy(&sc->sc_lock);
    639 	mutex_destroy(&sc->sc_intr_lock);
    640 
    641 	pool_cache_destroy(sc->sc_xferpool);
    642 
    643 	return rv;
    644 }
    645 
    646 int
    647 xhci_activate(device_t self, enum devact act)
    648 {
    649 	struct xhci_softc * const sc = device_private(self);
    650 
    651 	switch (act) {
    652 	case DVACT_DEACTIVATE:
    653 		sc->sc_dying = true;
    654 		return 0;
    655 	default:
    656 		return EOPNOTSUPP;
    657 	}
    658 }
    659 
    660 bool
    661 xhci_suspend(device_t dv, const pmf_qual_t *qual)
    662 {
    663 	return false;
    664 }
    665 
    666 bool
    667 xhci_resume(device_t dv, const pmf_qual_t *qual)
    668 {
    669 	return false;
    670 }
    671 
    672 bool
    673 xhci_shutdown(device_t self, int flags)
    674 {
    675 	return false;
    676 }
    677 
    678 static int
    679 xhci_hc_reset(struct xhci_softc * const sc)
    680 {
    681 	uint32_t usbcmd, usbsts;
    682 	int i;
    683 
    684 	/* Check controller not ready */
    685 	for (i = 0; i < XHCI_WAIT_CNR; i++) {
    686 		usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
    687 		if ((usbsts & XHCI_STS_CNR) == 0)
    688 			break;
    689 		usb_delay_ms(&sc->sc_bus, 1);
    690 	}
    691 	if (i >= XHCI_WAIT_CNR) {
    692 		aprint_error_dev(sc->sc_dev, "controller not ready timeout\n");
    693 		return EIO;
    694 	}
    695 
    696 	/* Halt controller */
    697 	usbcmd = 0;
    698 	xhci_op_write_4(sc, XHCI_USBCMD, usbcmd);
    699 	usb_delay_ms(&sc->sc_bus, 1);
    700 
    701 	/* Reset controller */
    702 	usbcmd = XHCI_CMD_HCRST;
    703 	xhci_op_write_4(sc, XHCI_USBCMD, usbcmd);
    704 	for (i = 0; i < XHCI_WAIT_HCRST; i++) {
    705 		/*
    706 		 * Wait 1ms first. Existing Intel xHCI requies 1ms delay to
    707 		 * prevent system hang (Errata).
    708 		 */
    709 		usb_delay_ms(&sc->sc_bus, 1);
    710 		usbcmd = xhci_op_read_4(sc, XHCI_USBCMD);
    711 		if ((usbcmd & XHCI_CMD_HCRST) == 0)
    712 			break;
    713 	}
    714 	if (i >= XHCI_WAIT_HCRST) {
    715 		aprint_error_dev(sc->sc_dev, "host controller reset timeout\n");
    716 		return EIO;
    717 	}
    718 
    719 	/* Check controller not ready */
    720 	for (i = 0; i < XHCI_WAIT_CNR; i++) {
    721 		usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
    722 		if ((usbsts & XHCI_STS_CNR) == 0)
    723 			break;
    724 		usb_delay_ms(&sc->sc_bus, 1);
    725 	}
    726 	if (i >= XHCI_WAIT_CNR) {
    727 		aprint_error_dev(sc->sc_dev,
    728 		    "controller not ready timeout after reset\n");
    729 		return EIO;
    730 	}
    731 
    732 	return 0;
    733 }
    734 
    735 
    736 /* 7.2 xHCI Support Protocol Capability */
    737 static void
    738 xhci_id_protocols(struct xhci_softc *sc, bus_size_t ecp)
    739 {
    740 	/* XXX Cache this lot */
    741 
    742 	const uint32_t w0 = xhci_read_4(sc, ecp);
    743 	const uint32_t w4 = xhci_read_4(sc, ecp + 4);
    744 	const uint32_t w8 = xhci_read_4(sc, ecp + 8);
    745 	const uint32_t wc = xhci_read_4(sc, ecp + 0xc);
    746 
    747 	aprint_debug_dev(sc->sc_dev,
    748 	    " SP: %08x %08x %08x %08x\n", w0, w4, w8, wc);
    749 
    750 	if (w4 != XHCI_XECP_USBID)
    751 		return;
    752 
    753 	const int major = XHCI_XECP_SP_W0_MAJOR(w0);
    754 	const int minor = XHCI_XECP_SP_W0_MINOR(w0);
    755 	const uint8_t cpo = XHCI_XECP_SP_W8_CPO(w8);
    756 	const uint8_t cpc = XHCI_XECP_SP_W8_CPC(w8);
    757 
    758 	const uint16_t mm = __SHIFTOUT(w0, __BITS(31, 16));
    759 	switch (mm) {
    760 	case 0x0200:
    761 	case 0x0300:
    762 	case 0x0301:
    763 		aprint_debug_dev(sc->sc_dev, " %s ports %d - %d\n",
    764 		    major == 3 ? "ss" : "hs", cpo, cpo + cpc -1);
    765 		break;
    766 	default:
    767 		aprint_debug_dev(sc->sc_dev, " unknown major/minor (%d/%d)\n",
    768 		    major, minor);
    769 		return;
    770 	}
    771 
    772 	const size_t bus = (major == 3) ? 0 : 1;
    773 
    774 	/* Index arrays with 0..n-1 where ports are numbered 1..n */
    775 	for (size_t cp = cpo - 1; cp < cpo + cpc - 1; cp++) {
    776 		if (sc->sc_ctlrportmap[cp] != 0) {
    777 			aprint_error_dev(sc->sc_dev, "contoller port %zu "
    778 			    "already assigned", cp);
    779 			continue;
    780 		}
    781 
    782 		sc->sc_ctlrportbus[cp / NBBY] |=
    783 		    bus == 0 ? 0 : __BIT(cp % NBBY);
    784 
    785 		const size_t rhp = sc->sc_rhportcount[bus]++;
    786 
    787 		KASSERTMSG(sc->sc_rhportmap[bus][rhp] == 0,
    788 		    "bus %zu rhp %zu is %d", bus, rhp,
    789 		    sc->sc_rhportmap[bus][rhp]);
    790 
    791 		sc->sc_rhportmap[bus][rhp] = cp + 1;
    792 		sc->sc_ctlrportmap[cp] = rhp + 1;
    793 	}
    794 }
    795 
    796 /* Process extended capabilities */
    797 static void
    798 xhci_ecp(struct xhci_softc *sc, uint32_t hcc)
    799 {
    800 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
    801 
    802 	bus_size_t ecp = XHCI_HCC_XECP(hcc) * 4;
    803 	while (ecp != 0) {
    804 		uint32_t ecr = xhci_read_4(sc, ecp);
    805 		aprint_debug_dev(sc->sc_dev, "ECR: 0x%08x\n", ecr);
    806 		switch (XHCI_XECP_ID(ecr)) {
    807 		case XHCI_ID_PROTOCOLS: {
    808 			xhci_id_protocols(sc, ecp);
    809 			break;
    810 		}
    811 		case XHCI_ID_USB_LEGACY: {
    812 			uint8_t bios_sem;
    813 
    814 			/* Take host controller ownership from BIOS */
    815 			bios_sem = xhci_read_1(sc, ecp + XHCI_XECP_BIOS_SEM);
    816 			if (bios_sem) {
    817 				/* sets xHCI to be owned by OS */
    818 				xhci_write_1(sc, ecp + XHCI_XECP_OS_SEM, 1);
    819 				aprint_debug_dev(sc->sc_dev,
    820 				    "waiting for BIOS to give up control\n");
    821 				for (int i = 0; i < 5000; i++) {
    822 					bios_sem = xhci_read_1(sc, ecp +
    823 					    XHCI_XECP_BIOS_SEM);
    824 					if (bios_sem == 0)
    825 						break;
    826 					DELAY(1000);
    827 				}
    828 				if (bios_sem) {
    829 					aprint_error_dev(sc->sc_dev,
    830 					    "timed out waiting for BIOS\n");
    831 				}
    832 			}
    833 			break;
    834 		}
    835 		default:
    836 			break;
    837 		}
    838 		ecr = xhci_read_4(sc, ecp);
    839 		if (XHCI_XECP_NEXT(ecr) == 0) {
    840 			ecp = 0;
    841 		} else {
    842 			ecp += XHCI_XECP_NEXT(ecr) * 4;
    843 		}
    844 	}
    845 }
    846 
    847 #define XHCI_HCCPREV1_BITS	\
    848 	"\177\020"	/* New bitmask */			\
    849 	"f\020\020XECP\0"					\
    850 	"f\014\4MAXPSA\0"					\
    851 	"b\013CFC\0"						\
    852 	"b\012SEC\0"						\
    853 	"b\011SBD\0"						\
    854 	"b\010FSE\0"						\
    855 	"b\7NSS\0"						\
    856 	"b\6LTC\0"						\
    857 	"b\5LHRC\0"						\
    858 	"b\4PIND\0"						\
    859 	"b\3PPC\0"						\
    860 	"b\2CZC\0"						\
    861 	"b\1BNC\0"						\
    862 	"b\0AC64\0"						\
    863 	"\0"
    864 #define XHCI_HCCV1_x_BITS	\
    865 	"\177\020"	/* New bitmask */			\
    866 	"f\020\020XECP\0"					\
    867 	"f\014\4MAXPSA\0"					\
    868 	"b\013CFC\0"						\
    869 	"b\012SEC\0"						\
    870 	"b\011SPC\0"						\
    871 	"b\010PAE\0"						\
    872 	"b\7NSS\0"						\
    873 	"b\6LTC\0"						\
    874 	"b\5LHRC\0"						\
    875 	"b\4PIND\0"						\
    876 	"b\3PPC\0"						\
    877 	"b\2CSZ\0"						\
    878 	"b\1BNC\0"						\
    879 	"b\0AC64\0"						\
    880 	"\0"
    881 
    882 #define XHCI_HCC2_BITS	\
    883 	"\177\020"	/* New bitmask */			\
    884 	"b\7ETC_TSC\0"						\
    885 	"b\6ETC\0"						\
    886 	"b\5CIC\0"						\
    887 	"b\4LEC\0"						\
    888 	"b\3CTC\0"						\
    889 	"b\2FSC\0"						\
    890 	"b\1CMC\0"						\
    891 	"b\0U3C\0"						\
    892 	"\0"
    893 
    894 void
    895 xhci_start(struct xhci_softc *sc)
    896 {
    897 	xhci_rt_write_4(sc, XHCI_IMAN(0), XHCI_IMAN_INTR_ENA);
    898 	if ((sc->sc_quirks & XHCI_QUIRK_INTEL) != 0)
    899 		/* Intel xhci needs interrupt rate moderated. */
    900 		xhci_rt_write_4(sc, XHCI_IMOD(0), XHCI_IMOD_DEFAULT_LP);
    901 	else
    902 		xhci_rt_write_4(sc, XHCI_IMOD(0), 0);
    903 	aprint_debug_dev(sc->sc_dev, "current IMOD %u\n",
    904 	    xhci_rt_read_4(sc, XHCI_IMOD(0)));
    905 
    906 	xhci_op_write_4(sc, XHCI_USBCMD, XHCI_CMD_INTE|XHCI_CMD_RS); /* Go! */
    907 	aprint_debug_dev(sc->sc_dev, "USBCMD %08"PRIx32"\n",
    908 	    xhci_op_read_4(sc, XHCI_USBCMD));
    909 }
    910 
    911 int
    912 xhci_init(struct xhci_softc *sc)
    913 {
    914 	bus_size_t bsz;
    915 	uint32_t cap, hcs1, hcs2, hcs3, hcc, dboff, rtsoff, hcc2;
    916 	uint32_t pagesize, config;
    917 	int i = 0;
    918 	uint16_t hciversion;
    919 	uint8_t caplength;
    920 
    921 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
    922 
    923 	/* Set up the bus struct for the usb 3 and usb 2 buses */
    924 	sc->sc_bus.ub_methods = &xhci_bus_methods;
    925 	sc->sc_bus.ub_pipesize = sizeof(struct xhci_pipe);
    926 	sc->sc_bus.ub_usedma = true;
    927 	sc->sc_bus.ub_hcpriv = sc;
    928 
    929 	sc->sc_bus2.ub_methods = &xhci_bus_methods;
    930 	sc->sc_bus2.ub_pipesize = sizeof(struct xhci_pipe);
    931 	sc->sc_bus2.ub_revision = USBREV_2_0;
    932 	sc->sc_bus2.ub_usedma = true;
    933 	sc->sc_bus2.ub_hcpriv = sc;
    934 	sc->sc_bus2.ub_dmatag = sc->sc_bus.ub_dmatag;
    935 
    936 	cap = xhci_read_4(sc, XHCI_CAPLENGTH);
    937 	caplength = XHCI_CAP_CAPLENGTH(cap);
    938 	hciversion = XHCI_CAP_HCIVERSION(cap);
    939 
    940 	if (hciversion < XHCI_HCIVERSION_0_96 ||
    941 	    hciversion > XHCI_HCIVERSION_1_0) {
    942 		aprint_normal_dev(sc->sc_dev,
    943 		    "xHCI version %x.%x not known to be supported\n",
    944 		    (hciversion >> 8) & 0xff, (hciversion >> 0) & 0xff);
    945 	} else {
    946 		aprint_verbose_dev(sc->sc_dev, "xHCI version %x.%x\n",
    947 		    (hciversion >> 8) & 0xff, (hciversion >> 0) & 0xff);
    948 	}
    949 
    950 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0, caplength,
    951 	    &sc->sc_cbh) != 0) {
    952 		aprint_error_dev(sc->sc_dev, "capability subregion failure\n");
    953 		return ENOMEM;
    954 	}
    955 
    956 	hcs1 = xhci_cap_read_4(sc, XHCI_HCSPARAMS1);
    957 	sc->sc_maxslots = XHCI_HCS1_MAXSLOTS(hcs1);
    958 	sc->sc_maxintrs = XHCI_HCS1_MAXINTRS(hcs1);
    959 	sc->sc_maxports = XHCI_HCS1_MAXPORTS(hcs1);
    960 	hcs2 = xhci_cap_read_4(sc, XHCI_HCSPARAMS2);
    961 	hcs3 = xhci_cap_read_4(sc, XHCI_HCSPARAMS3);
    962 	aprint_debug_dev(sc->sc_dev,
    963 	    "hcs1=%"PRIx32" hcs2=%"PRIx32" hcs3=%"PRIx32"\n", hcs1, hcs2, hcs3);
    964 
    965 	hcc = xhci_cap_read_4(sc, XHCI_HCCPARAMS);
    966 	sc->sc_ac64 = XHCI_HCC_AC64(hcc);
    967 	sc->sc_ctxsz = XHCI_HCC_CSZ(hcc) ? 64 : 32;
    968 
    969 	char sbuf[128];
    970 	if (hciversion < XHCI_HCIVERSION_1_0)
    971 		snprintb(sbuf, sizeof(sbuf), XHCI_HCCPREV1_BITS, hcc);
    972 	else
    973 		snprintb(sbuf, sizeof(sbuf), XHCI_HCCV1_x_BITS, hcc);
    974 	aprint_debug_dev(sc->sc_dev, "hcc=%s\n", sbuf);
    975 	aprint_debug_dev(sc->sc_dev, "xECP %x\n", XHCI_HCC_XECP(hcc) * 4);
    976 	if (hciversion >= XHCI_HCIVERSION_1_1) {
    977 		hcc2 = xhci_cap_read_4(sc, XHCI_HCCPARAMS2);
    978 		snprintb(sbuf, sizeof(sbuf), XHCI_HCC2_BITS, hcc2);
    979 		aprint_debug_dev(sc->sc_dev, "hcc2=%s\n", sbuf);
    980 	}
    981 
    982 	/* default all ports to bus 0, i.e. usb 3 */
    983 	sc->sc_ctlrportbus = kmem_zalloc(
    984 	    howmany(sc->sc_maxports * sizeof(uint8_t), NBBY), KM_SLEEP);
    985 	sc->sc_ctlrportmap = kmem_zalloc(sc->sc_maxports * sizeof(int), KM_SLEEP);
    986 
    987 	/* controller port to bus roothub port map */
    988 	for (size_t j = 0; j < __arraycount(sc->sc_rhportmap); j++) {
    989 		sc->sc_rhportmap[j] = kmem_zalloc(sc->sc_maxports * sizeof(int), KM_SLEEP);
    990 	}
    991 
    992 	/*
    993 	 * Process all Extended Capabilities
    994 	 */
    995 	xhci_ecp(sc, hcc);
    996 
    997 	bsz = XHCI_PORTSC(sc->sc_maxports);
    998 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, caplength, bsz,
    999 	    &sc->sc_obh) != 0) {
   1000 		aprint_error_dev(sc->sc_dev, "operational subregion failure\n");
   1001 		return ENOMEM;
   1002 	}
   1003 
   1004 	dboff = xhci_cap_read_4(sc, XHCI_DBOFF);
   1005 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, dboff,
   1006 	    sc->sc_maxslots * 4, &sc->sc_dbh) != 0) {
   1007 		aprint_error_dev(sc->sc_dev, "doorbell subregion failure\n");
   1008 		return ENOMEM;
   1009 	}
   1010 
   1011 	rtsoff = xhci_cap_read_4(sc, XHCI_RTSOFF);
   1012 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, rtsoff,
   1013 	    sc->sc_maxintrs * 0x20, &sc->sc_rbh) != 0) {
   1014 		aprint_error_dev(sc->sc_dev, "runtime subregion failure\n");
   1015 		return ENOMEM;
   1016 	}
   1017 
   1018 	int rv;
   1019 	rv = xhci_hc_reset(sc);
   1020 	if (rv != 0) {
   1021 		return rv;
   1022 	}
   1023 
   1024 	if (sc->sc_vendor_init)
   1025 		sc->sc_vendor_init(sc);
   1026 
   1027 	pagesize = xhci_op_read_4(sc, XHCI_PAGESIZE);
   1028 	aprint_debug_dev(sc->sc_dev, "PAGESIZE 0x%08x\n", pagesize);
   1029 	pagesize = ffs(pagesize);
   1030 	if (pagesize == 0) {
   1031 		aprint_error_dev(sc->sc_dev, "pagesize is 0\n");
   1032 		return EIO;
   1033 	}
   1034 	sc->sc_pgsz = 1 << (12 + (pagesize - 1));
   1035 	aprint_debug_dev(sc->sc_dev, "sc_pgsz 0x%08x\n", (uint32_t)sc->sc_pgsz);
   1036 	aprint_debug_dev(sc->sc_dev, "sc_maxslots 0x%08x\n",
   1037 	    (uint32_t)sc->sc_maxslots);
   1038 	aprint_debug_dev(sc->sc_dev, "sc_maxports %d\n", sc->sc_maxports);
   1039 
   1040 	usbd_status err;
   1041 
   1042 	sc->sc_maxspbuf = XHCI_HCS2_MAXSPBUF(hcs2);
   1043 	aprint_debug_dev(sc->sc_dev, "sc_maxspbuf %d\n", sc->sc_maxspbuf);
   1044 	if (sc->sc_maxspbuf != 0) {
   1045 		err = usb_allocmem(&sc->sc_bus,
   1046 		    sizeof(uint64_t) * sc->sc_maxspbuf, sizeof(uint64_t),
   1047 		    &sc->sc_spbufarray_dma);
   1048 		if (err) {
   1049 			aprint_error_dev(sc->sc_dev,
   1050 			    "spbufarray init fail, err %d\n", err);
   1051 			return ENOMEM;
   1052 		}
   1053 
   1054 		sc->sc_spbuf_dma = kmem_zalloc(sizeof(*sc->sc_spbuf_dma) *
   1055 		    sc->sc_maxspbuf, KM_SLEEP);
   1056 		uint64_t *spbufarray = KERNADDR(&sc->sc_spbufarray_dma, 0);
   1057 		for (i = 0; i < sc->sc_maxspbuf; i++) {
   1058 			usb_dma_t * const dma = &sc->sc_spbuf_dma[i];
   1059 			/* allocate contexts */
   1060 			err = usb_allocmem(&sc->sc_bus, sc->sc_pgsz,
   1061 			    sc->sc_pgsz, dma);
   1062 			if (err) {
   1063 				aprint_error_dev(sc->sc_dev,
   1064 				    "spbufarray_dma init fail, err %d\n", err);
   1065 				rv = ENOMEM;
   1066 				goto bad1;
   1067 			}
   1068 			spbufarray[i] = htole64(DMAADDR(dma, 0));
   1069 			usb_syncmem(dma, 0, sc->sc_pgsz,
   1070 			    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1071 		}
   1072 
   1073 		usb_syncmem(&sc->sc_spbufarray_dma, 0,
   1074 		    sizeof(uint64_t) * sc->sc_maxspbuf, BUS_DMASYNC_PREWRITE);
   1075 	}
   1076 
   1077 	config = xhci_op_read_4(sc, XHCI_CONFIG);
   1078 	config &= ~0xFF;
   1079 	config |= sc->sc_maxslots & 0xFF;
   1080 	xhci_op_write_4(sc, XHCI_CONFIG, config);
   1081 
   1082 	err = xhci_ring_init(sc, &sc->sc_cr, XHCI_COMMAND_RING_TRBS,
   1083 	    XHCI_COMMAND_RING_SEGMENTS_ALIGN);
   1084 	if (err) {
   1085 		aprint_error_dev(sc->sc_dev, "command ring init fail, err %d\n",
   1086 		    err);
   1087 		rv = ENOMEM;
   1088 		goto bad1;
   1089 	}
   1090 
   1091 	err = xhci_ring_init(sc, &sc->sc_er, XHCI_EVENT_RING_TRBS,
   1092 	    XHCI_EVENT_RING_SEGMENTS_ALIGN);
   1093 	if (err) {
   1094 		aprint_error_dev(sc->sc_dev, "event ring init fail, err %d\n",
   1095 		    err);
   1096 		rv = ENOMEM;
   1097 		goto bad2;
   1098 	}
   1099 
   1100 	usb_dma_t *dma;
   1101 	size_t size;
   1102 	size_t align;
   1103 
   1104 	dma = &sc->sc_eventst_dma;
   1105 	size = roundup2(XHCI_EVENT_RING_SEGMENTS * XHCI_ERSTE_SIZE,
   1106 	    XHCI_EVENT_RING_SEGMENT_TABLE_ALIGN);
   1107 	KASSERTMSG(size <= (512 * 1024), "eventst size %zu too large", size);
   1108 	align = XHCI_EVENT_RING_SEGMENT_TABLE_ALIGN;
   1109 	err = usb_allocmem(&sc->sc_bus, size, align, dma);
   1110 	if (err) {
   1111 		aprint_error_dev(sc->sc_dev, "eventst init fail, err %d\n",
   1112 		    err);
   1113 		rv = ENOMEM;
   1114 		goto bad3;
   1115 	}
   1116 
   1117 	memset(KERNADDR(dma, 0), 0, size);
   1118 	usb_syncmem(dma, 0, size, BUS_DMASYNC_PREWRITE);
   1119 	aprint_debug_dev(sc->sc_dev, "eventst: %016jx %p %zx\n",
   1120 	    (uintmax_t)DMAADDR(&sc->sc_eventst_dma, 0),
   1121 	    KERNADDR(&sc->sc_eventst_dma, 0),
   1122 	    sc->sc_eventst_dma.udma_block->size);
   1123 
   1124 	dma = &sc->sc_dcbaa_dma;
   1125 	size = (1 + sc->sc_maxslots) * sizeof(uint64_t);
   1126 	KASSERTMSG(size <= 2048, "dcbaa size %zu too large", size);
   1127 	align = XHCI_DEVICE_CONTEXT_BASE_ADDRESS_ARRAY_ALIGN;
   1128 	err = usb_allocmem(&sc->sc_bus, size, align, dma);
   1129 	if (err) {
   1130 		aprint_error_dev(sc->sc_dev, "dcbaa init fail, err %d\n", err);
   1131 		rv = ENOMEM;
   1132 		goto bad4;
   1133 	}
   1134 	aprint_debug_dev(sc->sc_dev, "dcbaa: %016jx %p %zx\n",
   1135 	    (uintmax_t)DMAADDR(&sc->sc_dcbaa_dma, 0),
   1136 	    KERNADDR(&sc->sc_dcbaa_dma, 0),
   1137 	    sc->sc_dcbaa_dma.udma_block->size);
   1138 
   1139 	memset(KERNADDR(dma, 0), 0, size);
   1140 	if (sc->sc_maxspbuf != 0) {
   1141 		/*
   1142 		 * DCBA entry 0 hold the scratchbuf array pointer.
   1143 		 */
   1144 		*(uint64_t *)KERNADDR(dma, 0) =
   1145 		    htole64(DMAADDR(&sc->sc_spbufarray_dma, 0));
   1146 	}
   1147 	usb_syncmem(dma, 0, size, BUS_DMASYNC_PREWRITE);
   1148 
   1149 	sc->sc_slots = kmem_zalloc(sizeof(*sc->sc_slots) * sc->sc_maxslots,
   1150 	    KM_SLEEP);
   1151 	if (sc->sc_slots == NULL) {
   1152 		aprint_error_dev(sc->sc_dev, "slots init fail, err %d\n", err);
   1153 		rv = ENOMEM;
   1154 		goto bad;
   1155 	}
   1156 
   1157 	sc->sc_xferpool = pool_cache_init(sizeof(struct xhci_xfer), 0, 0, 0,
   1158 	    "xhcixfer", NULL, IPL_USB, NULL, NULL, NULL);
   1159 	if (sc->sc_xferpool == NULL) {
   1160 		aprint_error_dev(sc->sc_dev, "pool_cache init fail, err %d\n",
   1161 		    err);
   1162 		rv = ENOMEM;
   1163 		goto bad;
   1164 	}
   1165 
   1166 	cv_init(&sc->sc_command_cv, "xhcicmd");
   1167 	cv_init(&sc->sc_cmdbusy_cv, "xhcicmdq");
   1168 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
   1169 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_USB);
   1170 
   1171 	struct xhci_erste *erst;
   1172 	erst = KERNADDR(&sc->sc_eventst_dma, 0);
   1173 	erst[0].erste_0 = htole64(xhci_ring_trbp(&sc->sc_er, 0));
   1174 	erst[0].erste_2 = htole32(sc->sc_er.xr_ntrb);
   1175 	erst[0].erste_3 = htole32(0);
   1176 	usb_syncmem(&sc->sc_eventst_dma, 0,
   1177 	    XHCI_ERSTE_SIZE * XHCI_EVENT_RING_SEGMENTS, BUS_DMASYNC_PREWRITE);
   1178 
   1179 	xhci_rt_write_4(sc, XHCI_ERSTSZ(0), XHCI_EVENT_RING_SEGMENTS);
   1180 	xhci_rt_write_8(sc, XHCI_ERSTBA(0), DMAADDR(&sc->sc_eventst_dma, 0));
   1181 	xhci_rt_write_8(sc, XHCI_ERDP(0), xhci_ring_trbp(&sc->sc_er, 0) |
   1182 	    XHCI_ERDP_LO_BUSY);
   1183 	xhci_op_write_8(sc, XHCI_DCBAAP, DMAADDR(&sc->sc_dcbaa_dma, 0));
   1184 	xhci_op_write_8(sc, XHCI_CRCR, xhci_ring_trbp(&sc->sc_cr, 0) |
   1185 	    sc->sc_cr.xr_cs);
   1186 
   1187 	HEXDUMP("eventst", KERNADDR(&sc->sc_eventst_dma, 0),
   1188 	    XHCI_ERSTE_SIZE * XHCI_EVENT_RING_SEGMENTS);
   1189 
   1190 	if ((sc->sc_quirks & XHCI_DEFERRED_START) == 0)
   1191 		xhci_start(sc);
   1192 
   1193 	return 0;
   1194 
   1195  bad:
   1196 	if (sc->sc_xferpool) {
   1197 		pool_cache_destroy(sc->sc_xferpool);
   1198 		sc->sc_xferpool = NULL;
   1199 	}
   1200 
   1201 	if (sc->sc_slots) {
   1202 		kmem_free(sc->sc_slots, sizeof(*sc->sc_slots) *
   1203 		    sc->sc_maxslots);
   1204 		sc->sc_slots = NULL;
   1205 	}
   1206 
   1207 	usb_freemem(&sc->sc_bus, &sc->sc_dcbaa_dma);
   1208  bad4:
   1209 	usb_freemem(&sc->sc_bus, &sc->sc_eventst_dma);
   1210  bad3:
   1211 	xhci_ring_free(sc, &sc->sc_er);
   1212  bad2:
   1213 	xhci_ring_free(sc, &sc->sc_cr);
   1214 	i = sc->sc_maxspbuf;
   1215  bad1:
   1216 	for (int j = 0; j < i; j++)
   1217 		usb_freemem(&sc->sc_bus, &sc->sc_spbuf_dma[j]);
   1218 	usb_freemem(&sc->sc_bus, &sc->sc_spbufarray_dma);
   1219 
   1220 	return rv;
   1221 }
   1222 
   1223 static inline bool
   1224 xhci_polling_p(struct xhci_softc * const sc)
   1225 {
   1226 	return sc->sc_bus.ub_usepolling || sc->sc_bus2.ub_usepolling;
   1227 }
   1228 
   1229 int
   1230 xhci_intr(void *v)
   1231 {
   1232 	struct xhci_softc * const sc = v;
   1233 	int ret = 0;
   1234 
   1235 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1236 
   1237 	if (sc == NULL)
   1238 		return 0;
   1239 
   1240 	mutex_spin_enter(&sc->sc_intr_lock);
   1241 
   1242 	if (sc->sc_dying || !device_has_power(sc->sc_dev))
   1243 		goto done;
   1244 
   1245 	/* If we get an interrupt while polling, then just ignore it. */
   1246 	if (xhci_polling_p(sc)) {
   1247 #ifdef DIAGNOSTIC
   1248 		DPRINTFN(16, "ignored interrupt while polling", 0, 0, 0, 0);
   1249 #endif
   1250 		goto done;
   1251 	}
   1252 
   1253 	ret = xhci_intr1(sc);
   1254 	if (ret) {
   1255 		KASSERT(sc->sc_child || sc->sc_child2);
   1256 
   1257 		/*
   1258 		 * One of child busses could be already detached. It doesn't
   1259 		 * matter on which of the two the softintr is scheduled.
   1260 		 */
   1261 		if (sc->sc_child)
   1262 			usb_schedsoftintr(&sc->sc_bus);
   1263 		else
   1264 			usb_schedsoftintr(&sc->sc_bus2);
   1265 	}
   1266 done:
   1267 	mutex_spin_exit(&sc->sc_intr_lock);
   1268 	return ret;
   1269 }
   1270 
   1271 int
   1272 xhci_intr1(struct xhci_softc * const sc)
   1273 {
   1274 	uint32_t usbsts;
   1275 	uint32_t iman;
   1276 
   1277 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1278 
   1279 	usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
   1280 	DPRINTFN(16, "USBSTS %08jx", usbsts, 0, 0, 0);
   1281 	if ((usbsts & (XHCI_STS_HSE | XHCI_STS_EINT | XHCI_STS_PCD |
   1282 	    XHCI_STS_HCE)) == 0) {
   1283 		DPRINTFN(16, "ignored intr not for %s",
   1284 		    device_xname(sc->sc_dev), 0, 0, 0);
   1285 		return 0;
   1286 	}
   1287 
   1288 	/*
   1289 	 * Clear EINT and other transient flags, to not misenterpret
   1290 	 * next shared interrupt. Also, to avoid race, EINT must be cleared
   1291 	 * before XHCI_IMAN_INTR_PEND is cleared.
   1292 	 */
   1293 	xhci_op_write_4(sc, XHCI_USBSTS, usbsts & XHCI_STS_RSVDP0);
   1294 
   1295 #ifdef XHCI_DEBUG
   1296 	usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
   1297 	DPRINTFN(16, "USBSTS %08jx", usbsts, 0, 0, 0);
   1298 #endif
   1299 
   1300 	iman = xhci_rt_read_4(sc, XHCI_IMAN(0));
   1301 	DPRINTFN(16, "IMAN0 %08jx", iman, 0, 0, 0);
   1302 	iman |= XHCI_IMAN_INTR_PEND;
   1303 	xhci_rt_write_4(sc, XHCI_IMAN(0), iman);
   1304 
   1305 #ifdef XHCI_DEBUG
   1306 	iman = xhci_rt_read_4(sc, XHCI_IMAN(0));
   1307 	DPRINTFN(16, "IMAN0 %08jx", iman, 0, 0, 0);
   1308 	usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
   1309 	DPRINTFN(16, "USBSTS %08jx", usbsts, 0, 0, 0);
   1310 #endif
   1311 
   1312 	return 1;
   1313 }
   1314 
   1315 /*
   1316  * 3 port speed types used in USB stack
   1317  *
   1318  * usbdi speed
   1319  *	definition: USB_SPEED_* in usb.h
   1320  *	They are used in struct usbd_device in USB stack.
   1321  *	ioctl interface uses these values too.
   1322  * port_status speed
   1323  *	definition: UPS_*_SPEED in usb.h
   1324  *	They are used in usb_port_status_t and valid only for USB 2.0.
   1325  *	Speed value is always 0 for Super Speed or more, and dwExtPortStatus
   1326  *	of usb_port_status_ext_t indicates port speed.
   1327  *	Note that some 3.0 values overlap with 2.0 values.
   1328  *	(e.g. 0x200 means UPS_POER_POWER_SS in SS and
   1329  *	            means UPS_LOW_SPEED in HS.)
   1330  *	port status returned from hub also uses these values.
   1331  *	On NetBSD UPS_OTHER_SPEED indicates port speed is super speed
   1332  *	or more.
   1333  * xspeed:
   1334  *	definition: Protocol Speed ID (PSI) (xHCI 1.1 7.2.1)
   1335  *	They are used in only slot context and PORTSC reg of xhci.
   1336  *	The difference between usbdi speed and xspeed is
   1337  *	that FS and LS values are swapped.
   1338  */
   1339 
   1340 /* convert usbdi speed to xspeed */
   1341 static int
   1342 xhci_speed2xspeed(int speed)
   1343 {
   1344 	switch (speed) {
   1345 	case USB_SPEED_LOW:	return 2;
   1346 	case USB_SPEED_FULL:	return 1;
   1347 	default:		return speed;
   1348 	}
   1349 }
   1350 
   1351 #if 0
   1352 /* convert xspeed to usbdi speed */
   1353 static int
   1354 xhci_xspeed2speed(int xspeed)
   1355 {
   1356 	switch (xspeed) {
   1357 	case 1: return USB_SPEED_FULL;
   1358 	case 2: return USB_SPEED_LOW;
   1359 	default: return xspeed;
   1360 	}
   1361 }
   1362 #endif
   1363 
   1364 /* convert xspeed to port status speed */
   1365 static int
   1366 xhci_xspeed2psspeed(int xspeed)
   1367 {
   1368 	switch (xspeed) {
   1369 	case 0: return 0;
   1370 	case 1: return UPS_FULL_SPEED;
   1371 	case 2: return UPS_LOW_SPEED;
   1372 	case 3: return UPS_HIGH_SPEED;
   1373 	default: return UPS_OTHER_SPEED;
   1374 	}
   1375 }
   1376 
   1377 /*
   1378  * Construct input contexts and issue TRB to open pipe.
   1379  */
   1380 static usbd_status
   1381 xhci_configure_endpoint(struct usbd_pipe *pipe)
   1382 {
   1383 	struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
   1384 	struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
   1385 #ifdef USB_DEBUG
   1386 	const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
   1387 #endif
   1388 	struct xhci_trb trb;
   1389 	usbd_status err;
   1390 
   1391 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1392 	DPRINTFN(4, "slot %ju dci %ju epaddr 0x%02jx attr 0x%02jx",
   1393 	    xs->xs_idx, dci, pipe->up_endpoint->ue_edesc->bEndpointAddress,
   1394 	    pipe->up_endpoint->ue_edesc->bmAttributes);
   1395 
   1396 	/* XXX ensure input context is available? */
   1397 
   1398 	memset(xhci_slot_get_icv(sc, xs, 0), 0, sc->sc_pgsz);
   1399 
   1400 	/* set up context */
   1401 	xhci_setup_ctx(pipe);
   1402 
   1403 	HEXDUMP("input control context", xhci_slot_get_icv(sc, xs, 0),
   1404 	    sc->sc_ctxsz * 1);
   1405 	HEXDUMP("input endpoint context", xhci_slot_get_icv(sc, xs,
   1406 	    xhci_dci_to_ici(dci)), sc->sc_ctxsz * 1);
   1407 
   1408 	trb.trb_0 = xhci_slot_get_icp(sc, xs, 0);
   1409 	trb.trb_2 = 0;
   1410 	trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
   1411 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_CONFIGURE_EP);
   1412 
   1413 	err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
   1414 
   1415 	usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
   1416 	HEXDUMP("output context", xhci_slot_get_dcv(sc, xs, dci),
   1417 	    sc->sc_ctxsz * 1);
   1418 
   1419 	return err;
   1420 }
   1421 
   1422 #if 0
   1423 static usbd_status
   1424 xhci_unconfigure_endpoint(struct usbd_pipe *pipe)
   1425 {
   1426 #ifdef USB_DEBUG
   1427 	struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
   1428 #endif
   1429 
   1430 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1431 	DPRINTFN(4, "slot %ju", xs->xs_idx, 0, 0, 0);
   1432 
   1433 	return USBD_NORMAL_COMPLETION;
   1434 }
   1435 #endif
   1436 
   1437 /* 4.6.8, 6.4.3.7 */
   1438 static usbd_status
   1439 xhci_reset_endpoint_locked(struct usbd_pipe *pipe)
   1440 {
   1441 	struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
   1442 	struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
   1443 	const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
   1444 	struct xhci_trb trb;
   1445 	usbd_status err;
   1446 
   1447 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1448 	DPRINTFN(4, "slot %ju dci %ju", xs->xs_idx, dci, 0, 0);
   1449 
   1450 	KASSERT(mutex_owned(&sc->sc_lock));
   1451 
   1452 	trb.trb_0 = 0;
   1453 	trb.trb_2 = 0;
   1454 	trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
   1455 	    XHCI_TRB_3_EP_SET(dci) |
   1456 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_EP);
   1457 
   1458 	err = xhci_do_command_locked(sc, &trb, USBD_DEFAULT_TIMEOUT);
   1459 
   1460 	return err;
   1461 }
   1462 
   1463 static usbd_status
   1464 xhci_reset_endpoint(struct usbd_pipe *pipe)
   1465 {
   1466 	struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
   1467 
   1468 	mutex_enter(&sc->sc_lock);
   1469 	usbd_status ret = xhci_reset_endpoint_locked(pipe);
   1470 	mutex_exit(&sc->sc_lock);
   1471 
   1472 	return ret;
   1473 }
   1474 
   1475 /*
   1476  * 4.6.9, 6.4.3.8
   1477  * Stop execution of TDs on xfer ring.
   1478  * Should be called with sc_lock held.
   1479  */
   1480 static usbd_status
   1481 xhci_stop_endpoint(struct usbd_pipe *pipe)
   1482 {
   1483 	struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
   1484 	struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
   1485 	struct xhci_trb trb;
   1486 	usbd_status err;
   1487 	const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
   1488 
   1489 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1490 	DPRINTFN(4, "slot %ju dci %ju", xs->xs_idx, dci, 0, 0);
   1491 
   1492 	KASSERT(mutex_owned(&sc->sc_lock));
   1493 
   1494 	trb.trb_0 = 0;
   1495 	trb.trb_2 = 0;
   1496 	trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
   1497 	    XHCI_TRB_3_EP_SET(dci) |
   1498 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STOP_EP);
   1499 
   1500 	err = xhci_do_command_locked(sc, &trb, USBD_DEFAULT_TIMEOUT);
   1501 
   1502 	return err;
   1503 }
   1504 
   1505 /*
   1506  * Set TR Dequeue Pointer.
   1507  * xHCI 1.1  4.6.10  6.4.3.9
   1508  * Purge all of the TRBs on ring and reinitialize ring.
   1509  * Set TR dequeue Pointr to 0 and Cycle State to 1.
   1510  * EPSTATE of endpoint must be ERROR or STOPPED, otherwise CONTEXT_STATE
   1511  * error will be generated.
   1512  */
   1513 static usbd_status
   1514 xhci_set_dequeue_locked(struct usbd_pipe *pipe)
   1515 {
   1516 	struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
   1517 	struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
   1518 	const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
   1519 	struct xhci_ring * const xr = &xs->xs_ep[dci].xe_tr;
   1520 	struct xhci_trb trb;
   1521 	usbd_status err;
   1522 
   1523 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1524 	DPRINTFN(4, "slot %ju dci %ju", xs->xs_idx, dci, 0, 0);
   1525 
   1526 	KASSERT(mutex_owned(&sc->sc_lock));
   1527 
   1528 	xhci_host_dequeue(xr);
   1529 
   1530 	/* set DCS */
   1531 	trb.trb_0 = xhci_ring_trbp(xr, 0) | 1; /* XXX */
   1532 	trb.trb_2 = 0;
   1533 	trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
   1534 	    XHCI_TRB_3_EP_SET(dci) |
   1535 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SET_TR_DEQUEUE);
   1536 
   1537 	err = xhci_do_command_locked(sc, &trb, USBD_DEFAULT_TIMEOUT);
   1538 
   1539 	return err;
   1540 }
   1541 
   1542 static usbd_status
   1543 xhci_set_dequeue(struct usbd_pipe *pipe)
   1544 {
   1545 	struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
   1546 
   1547 	mutex_enter(&sc->sc_lock);
   1548 	usbd_status ret = xhci_set_dequeue_locked(pipe);
   1549 	mutex_exit(&sc->sc_lock);
   1550 
   1551 	return ret;
   1552 }
   1553 
   1554 /*
   1555  * Open new pipe: called from usbd_setup_pipe_flags.
   1556  * Fills methods of pipe.
   1557  * If pipe is not for ep0, calls configure_endpoint.
   1558  */
   1559 static usbd_status
   1560 xhci_open(struct usbd_pipe *pipe)
   1561 {
   1562 	struct usbd_device * const dev = pipe->up_dev;
   1563 	struct xhci_softc * const sc = XHCI_BUS2SC(dev->ud_bus);
   1564 	usb_endpoint_descriptor_t * const ed = pipe->up_endpoint->ue_edesc;
   1565 	const uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
   1566 
   1567 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1568 	DPRINTFN(1, "addr %jd depth %jd port %jd speed %jd", dev->ud_addr,
   1569 	    dev->ud_depth, dev->ud_powersrc->up_portno, dev->ud_speed);
   1570 	DPRINTFN(1, " dci %ju type 0x%02jx epaddr 0x%02jx attr 0x%02jx",
   1571 	    xhci_ep_get_dci(ed), ed->bDescriptorType, ed->bEndpointAddress,
   1572 	    ed->bmAttributes);
   1573 	DPRINTFN(1, " mps %ju ival %ju", UGETW(ed->wMaxPacketSize),
   1574 	    ed->bInterval, 0, 0);
   1575 
   1576 	if (sc->sc_dying)
   1577 		return USBD_IOERROR;
   1578 
   1579 	/* Root Hub */
   1580 	if (dev->ud_depth == 0 && dev->ud_powersrc->up_portno == 0) {
   1581 		switch (ed->bEndpointAddress) {
   1582 		case USB_CONTROL_ENDPOINT:
   1583 			pipe->up_methods = &roothub_ctrl_methods;
   1584 			break;
   1585 		case UE_DIR_IN | USBROOTHUB_INTR_ENDPT:
   1586 			pipe->up_methods = &xhci_root_intr_methods;
   1587 			break;
   1588 		default:
   1589 			pipe->up_methods = NULL;
   1590 			DPRINTFN(0, "bad bEndpointAddress 0x%02jx",
   1591 			    ed->bEndpointAddress, 0, 0, 0);
   1592 			return USBD_INVAL;
   1593 		}
   1594 		return USBD_NORMAL_COMPLETION;
   1595 	}
   1596 
   1597 	switch (xfertype) {
   1598 	case UE_CONTROL:
   1599 		pipe->up_methods = &xhci_device_ctrl_methods;
   1600 		break;
   1601 	case UE_ISOCHRONOUS:
   1602 		pipe->up_methods = &xhci_device_isoc_methods;
   1603 		return USBD_INVAL;
   1604 		break;
   1605 	case UE_BULK:
   1606 		pipe->up_methods = &xhci_device_bulk_methods;
   1607 		break;
   1608 	case UE_INTERRUPT:
   1609 		pipe->up_methods = &xhci_device_intr_methods;
   1610 		break;
   1611 	default:
   1612 		return USBD_IOERROR;
   1613 		break;
   1614 	}
   1615 
   1616 	if (ed->bEndpointAddress != USB_CONTROL_ENDPOINT)
   1617 		return xhci_configure_endpoint(pipe);
   1618 
   1619 	return USBD_NORMAL_COMPLETION;
   1620 }
   1621 
   1622 /*
   1623  * Closes pipe, called from usbd_kill_pipe via close methods.
   1624  * If the endpoint to be closed is ep0, disable_slot.
   1625  * Should be called with sc_lock held.
   1626  */
   1627 static void
   1628 xhci_close_pipe(struct usbd_pipe *pipe)
   1629 {
   1630 	struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
   1631 	struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
   1632 	usb_endpoint_descriptor_t * const ed = pipe->up_endpoint->ue_edesc;
   1633 	const u_int dci = xhci_ep_get_dci(ed);
   1634 	struct xhci_trb trb;
   1635 	uint32_t *cp;
   1636 
   1637 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1638 
   1639 	if (sc->sc_dying)
   1640 		return;
   1641 
   1642 	/* xs is uninitialized before xhci_init_slot */
   1643 	if (xs == NULL || xs->xs_idx == 0)
   1644 		return;
   1645 
   1646 	DPRINTFN(4, "pipe %#jx slot %ju dci %ju", (uintptr_t)pipe, xs->xs_idx,
   1647 	    dci, 0);
   1648 
   1649 	KASSERTMSG(!cpu_intr_p() && !cpu_softintr_p(), "called from intr ctx");
   1650 	KASSERT(mutex_owned(&sc->sc_lock));
   1651 
   1652 	if (pipe->up_dev->ud_depth == 0)
   1653 		return;
   1654 
   1655 	if (dci == XHCI_DCI_EP_CONTROL) {
   1656 		DPRINTFN(4, "closing ep0", 0, 0, 0, 0);
   1657 		xhci_disable_slot(sc, xs->xs_idx);
   1658 		return;
   1659 	}
   1660 
   1661 	if (xhci_get_epstate(sc, xs, dci) != XHCI_EPSTATE_STOPPED)
   1662 		(void)xhci_stop_endpoint(pipe);
   1663 
   1664 	/*
   1665 	 * set appropriate bit to be dropped.
   1666 	 * don't set DC bit to 1, otherwise all endpoints
   1667 	 * would be deconfigured.
   1668 	 */
   1669 	cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL);
   1670 	cp[0] = htole32(XHCI_INCTX_0_DROP_MASK(dci));
   1671 	cp[1] = htole32(0);
   1672 
   1673 	/* XXX should be most significant one, not dci? */
   1674 	cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_SLOT));
   1675 	cp[0] = htole32(XHCI_SCTX_0_CTX_NUM_SET(dci));
   1676 
   1677 	/* configure ep context performs an implicit dequeue */
   1678 	xhci_host_dequeue(&xs->xs_ep[dci].xe_tr);
   1679 
   1680 	/* sync input contexts before they are read from memory */
   1681 	usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE);
   1682 
   1683 	trb.trb_0 = xhci_slot_get_icp(sc, xs, 0);
   1684 	trb.trb_2 = 0;
   1685 	trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
   1686 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_CONFIGURE_EP);
   1687 
   1688 	(void)xhci_do_command_locked(sc, &trb, USBD_DEFAULT_TIMEOUT);
   1689 	usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
   1690 }
   1691 
   1692 /*
   1693  * Abort transfer.
   1694  * Should be called with sc_lock held.
   1695  */
   1696 static void
   1697 xhci_abort_xfer(struct usbd_xfer *xfer, usbd_status status)
   1698 {
   1699 	struct xhci_softc * const sc = XHCI_XFER2SC(xfer);
   1700 	struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
   1701 	const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
   1702 
   1703 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1704 	DPRINTFN(4, "xfer %#jx pipe %#jx status %jd",
   1705 	    (uintptr_t)xfer, (uintptr_t)xfer->ux_pipe, status, 0);
   1706 
   1707 	KASSERT(mutex_owned(&sc->sc_lock));
   1708 
   1709 	if (sc->sc_dying) {
   1710 		/* If we're dying, just do the software part. */
   1711 		DPRINTFN(4, "xfer %#jx dying %ju", (uintptr_t)xfer,
   1712 		    xfer->ux_status, 0, 0);
   1713 		xfer->ux_status = status;
   1714 		callout_stop(&xfer->ux_callout);
   1715 		usb_transfer_complete(xfer);
   1716 		return;
   1717 	}
   1718 
   1719 	/*
   1720 	 * If an abort is already in progress then just wait for it to
   1721 	 * complete and return.
   1722 	 */
   1723 	if (xfer->ux_hcflags & UXFER_ABORTING) {
   1724 		DPRINTFN(4, "already aborting", 0, 0, 0, 0);
   1725 #ifdef DIAGNOSTIC
   1726 		if (status == USBD_TIMEOUT)
   1727 			DPRINTFN(4, "TIMEOUT while aborting", 0, 0, 0, 0);
   1728 #endif
   1729 		/* Override the status which might be USBD_TIMEOUT. */
   1730 		xfer->ux_status = status;
   1731 		DPRINTFN(4, "xfer %#jx waiting for abort to finish",
   1732 		    (uintptr_t)xfer, 0, 0, 0);
   1733 		xfer->ux_hcflags |= UXFER_ABORTWAIT;
   1734 		while (xfer->ux_hcflags & UXFER_ABORTING)
   1735 			cv_wait(&xfer->ux_hccv, &sc->sc_lock);
   1736 		return;
   1737 	}
   1738 	xfer->ux_hcflags |= UXFER_ABORTING;
   1739 
   1740 	/*
   1741 	 * Step 1: Stop xfer timeout timer.
   1742 	 */
   1743 	xfer->ux_status = status;
   1744 	callout_stop(&xfer->ux_callout);
   1745 
   1746 	/*
   1747 	 * Step 2: Stop execution of TD on the ring.
   1748 	 */
   1749 	switch (xhci_get_epstate(sc, xs, dci)) {
   1750 	case XHCI_EPSTATE_HALTED:
   1751 		(void)xhci_reset_endpoint_locked(xfer->ux_pipe);
   1752 		break;
   1753 	case XHCI_EPSTATE_STOPPED:
   1754 		break;
   1755 	default:
   1756 		(void)xhci_stop_endpoint(xfer->ux_pipe);
   1757 		break;
   1758 	}
   1759 #ifdef DIAGNOSTIC
   1760 	uint32_t epst = xhci_get_epstate(sc, xs, dci);
   1761 	if (epst != XHCI_EPSTATE_STOPPED)
   1762 		DPRINTFN(4, "dci %ju not stopped %ju", dci, epst, 0, 0);
   1763 #endif
   1764 
   1765 	/*
   1766 	 * Step 3: Remove any vestiges of the xfer from the ring.
   1767 	 */
   1768 	xhci_set_dequeue_locked(xfer->ux_pipe);
   1769 
   1770 	/*
   1771 	 * Step 4: Notify completion to waiting xfers.
   1772 	 */
   1773 	int wake = xfer->ux_hcflags & UXFER_ABORTWAIT;
   1774 	xfer->ux_hcflags &= ~(UXFER_ABORTING | UXFER_ABORTWAIT);
   1775 	usb_transfer_complete(xfer);
   1776 	if (wake) {
   1777 		cv_broadcast(&xfer->ux_hccv);
   1778 	}
   1779 	DPRINTFN(14, "end", 0, 0, 0, 0);
   1780 
   1781 	KASSERT(mutex_owned(&sc->sc_lock));
   1782 }
   1783 
   1784 static void
   1785 xhci_host_dequeue(struct xhci_ring * const xr)
   1786 {
   1787 	/* When dequeueing the controller, update our struct copy too */
   1788 	memset(xr->xr_trb, 0, xr->xr_ntrb * XHCI_TRB_SIZE);
   1789 	usb_syncmem(&xr->xr_dma, 0, xr->xr_ntrb * XHCI_TRB_SIZE,
   1790 	    BUS_DMASYNC_PREWRITE);
   1791 	memset(xr->xr_cookies, 0, xr->xr_ntrb * sizeof(*xr->xr_cookies));
   1792 
   1793 	xr->xr_ep = 0;
   1794 	xr->xr_cs = 1;
   1795 }
   1796 
   1797 /*
   1798  * Recover STALLed endpoint.
   1799  * xHCI 1.1 sect 4.10.2.1
   1800  * Issue RESET_EP to recover halt condition and SET_TR_DEQUEUE to remove
   1801  * all transfers on transfer ring.
   1802  * These are done in thread context asynchronously.
   1803  */
   1804 static void
   1805 xhci_clear_endpoint_stall_async_task(void *cookie)
   1806 {
   1807 	struct usbd_xfer * const xfer = cookie;
   1808 	struct xhci_softc * const sc = XHCI_XFER2SC(xfer);
   1809 	struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
   1810 	const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
   1811 	struct xhci_ring * const tr = &xs->xs_ep[dci].xe_tr;
   1812 
   1813 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1814 	DPRINTFN(4, "xfer %#jx slot %ju dci %ju", (uintptr_t)xfer, xs->xs_idx,
   1815 	    dci, 0);
   1816 
   1817 	xhci_reset_endpoint(xfer->ux_pipe);
   1818 	xhci_set_dequeue(xfer->ux_pipe);
   1819 
   1820 	mutex_enter(&sc->sc_lock);
   1821 	tr->is_halted = false;
   1822 	usb_transfer_complete(xfer);
   1823 	mutex_exit(&sc->sc_lock);
   1824 	DPRINTFN(4, "ends", 0, 0, 0, 0);
   1825 }
   1826 
   1827 static usbd_status
   1828 xhci_clear_endpoint_stall_async(struct usbd_xfer *xfer)
   1829 {
   1830 	struct xhci_softc * const sc = XHCI_XFER2SC(xfer);
   1831 	struct xhci_pipe * const xp = (struct xhci_pipe *)xfer->ux_pipe;
   1832 
   1833 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1834 	DPRINTFN(4, "xfer %#jx", (uintptr_t)xfer, 0, 0, 0);
   1835 
   1836 	if (sc->sc_dying) {
   1837 		return USBD_IOERROR;
   1838 	}
   1839 
   1840 	usb_init_task(&xp->xp_async_task,
   1841 	    xhci_clear_endpoint_stall_async_task, xfer, USB_TASKQ_MPSAFE);
   1842 	usb_add_task(xfer->ux_pipe->up_dev, &xp->xp_async_task, USB_TASKQ_HC);
   1843 	DPRINTFN(4, "ends", 0, 0, 0, 0);
   1844 
   1845 	return USBD_NORMAL_COMPLETION;
   1846 }
   1847 
   1848 /* Process roothub port status/change events and notify to uhub_intr. */
   1849 static void
   1850 xhci_rhpsc(struct xhci_softc * const sc, u_int ctlrport)
   1851 {
   1852 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1853 	DPRINTFN(4, "xhci%jd: port %ju status change", device_unit(sc->sc_dev),
   1854 	   ctlrport, 0, 0);
   1855 
   1856 	if (ctlrport > sc->sc_maxports)
   1857 		return;
   1858 
   1859 	const size_t bn = xhci_ctlrport2bus(sc, ctlrport);
   1860 	const size_t rhp = xhci_ctlrport2rhport(sc, ctlrport);
   1861 	struct usbd_xfer * const xfer = sc->sc_intrxfer[bn];
   1862 
   1863 	DPRINTFN(4, "xhci%jd: bus %jd bp %ju xfer %#jx status change",
   1864 	    device_unit(sc->sc_dev), bn, rhp, (uintptr_t)xfer);
   1865 
   1866 	if (xfer == NULL)
   1867 		return;
   1868 
   1869 	uint8_t *p = xfer->ux_buf;
   1870 	memset(p, 0, xfer->ux_length);
   1871 	p[rhp / NBBY] |= 1 << (rhp % NBBY);
   1872 	xfer->ux_actlen = xfer->ux_length;
   1873 	xfer->ux_status = USBD_NORMAL_COMPLETION;
   1874 	usb_transfer_complete(xfer);
   1875 }
   1876 
   1877 /* Process Transfer Events */
   1878 static void
   1879 xhci_event_transfer(struct xhci_softc * const sc,
   1880     const struct xhci_trb * const trb)
   1881 {
   1882 	uint64_t trb_0;
   1883 	uint32_t trb_2, trb_3;
   1884 	uint8_t trbcode;
   1885 	u_int slot, dci;
   1886 	struct xhci_slot *xs;
   1887 	struct xhci_ring *xr;
   1888 	struct xhci_xfer *xx;
   1889 	struct usbd_xfer *xfer;
   1890 	usbd_status err;
   1891 
   1892 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1893 
   1894 	trb_0 = le64toh(trb->trb_0);
   1895 	trb_2 = le32toh(trb->trb_2);
   1896 	trb_3 = le32toh(trb->trb_3);
   1897 	trbcode = XHCI_TRB_2_ERROR_GET(trb_2);
   1898 	slot = XHCI_TRB_3_SLOT_GET(trb_3);
   1899 	dci = XHCI_TRB_3_EP_GET(trb_3);
   1900 	xs = &sc->sc_slots[slot];
   1901 	xr = &xs->xs_ep[dci].xe_tr;
   1902 
   1903 	/* sanity check */
   1904 	KASSERTMSG(xs->xs_idx != 0 && xs->xs_idx <= sc->sc_maxslots,
   1905 	    "invalid xs_idx %u slot %u", xs->xs_idx, slot);
   1906 
   1907 	int idx = 0;
   1908 	if ((trb_3 & XHCI_TRB_3_ED_BIT) == 0) {
   1909 		if (xhci_trb_get_idx(xr, trb_0, &idx)) {
   1910 			DPRINTFN(0, "invalid trb_0 0x%jx", trb_0, 0, 0, 0);
   1911 			return;
   1912 		}
   1913 		xx = xr->xr_cookies[idx];
   1914 
   1915 		/* clear cookie of consumed TRB */
   1916 		xr->xr_cookies[idx] = NULL;
   1917 
   1918 		/*
   1919 		 * xx is NULL if pipe is opened but xfer is not started.
   1920 		 * It happens when stopping idle pipe.
   1921 		 */
   1922 		if (xx == NULL || trbcode == XHCI_TRB_ERROR_LENGTH) {
   1923 			DPRINTFN(1, "Ignore #%ju: cookie %#jx cc %ju dci %ju",
   1924 			    idx, (uintptr_t)xx, trbcode, dci);
   1925 			DPRINTFN(1, " orig TRB %jx type %ju", trb_0,
   1926 			    XHCI_TRB_3_TYPE_GET(le32toh(xr->xr_trb[idx].trb_3)),
   1927 			    0, 0);
   1928 			return;
   1929 		}
   1930 	} else {
   1931 		/* When ED != 0, trb_0 is virtual addr of struct xhci_xfer. */
   1932 		xx = (void *)(uintptr_t)(trb_0 & ~0x3);
   1933 	}
   1934 	/* XXX this may not happen */
   1935 	if (xx == NULL) {
   1936 		DPRINTFN(1, "xfer done: xx is NULL", 0, 0, 0, 0);
   1937 		return;
   1938 	}
   1939 	xfer = &xx->xx_xfer;
   1940 	/* XXX this may happen when detaching */
   1941 	if (xfer == NULL) {
   1942 		DPRINTFN(1, "xx(%#jx)->xx_xfer is NULL trb_0 %#jx",
   1943 		    (uintptr_t)xx, trb_0, 0, 0);
   1944 		return;
   1945 	}
   1946 	DPRINTFN(14, "xfer %#jx", (uintptr_t)xfer, 0, 0, 0);
   1947 	/* XXX I dunno why this happens */
   1948 	KASSERTMSG(xfer->ux_pipe != NULL, "xfer(%p)->ux_pipe is NULL", xfer);
   1949 
   1950 	if (!xfer->ux_pipe->up_repeat &&
   1951 	    SIMPLEQ_EMPTY(&xfer->ux_pipe->up_queue)) {
   1952 		DPRINTFN(1, "xfer(%#jx)->pipe not queued", (uintptr_t)xfer,
   1953 		    0, 0, 0);
   1954 		return;
   1955 	}
   1956 
   1957 	/* 4.11.5.2 Event Data TRB */
   1958 	if ((trb_3 & XHCI_TRB_3_ED_BIT) != 0) {
   1959 		DPRINTFN(14, "transfer Event Data: 0x%016jx 0x%08jx"
   1960 		    " %02jx", trb_0, XHCI_TRB_2_REM_GET(trb_2), trbcode, 0);
   1961 		if ((trb_0 & 0x3) == 0x3) {
   1962 			xfer->ux_actlen = XHCI_TRB_2_REM_GET(trb_2);
   1963 		}
   1964 	}
   1965 
   1966 	switch (trbcode) {
   1967 	case XHCI_TRB_ERROR_SHORT_PKT:
   1968 	case XHCI_TRB_ERROR_SUCCESS:
   1969 		/*
   1970 		 * A ctrl transfer can generate two events if it has a Data
   1971 		 * stage.  A short data stage can be OK and should not
   1972 		 * complete the transfer as the status stage needs to be
   1973 		 * performed.
   1974 		 *
   1975 		 * Note: Data and Status stage events point at same xfer.
   1976 		 * ux_actlen and ux_dmabuf will be passed to
   1977 		 * usb_transfer_complete after the Status stage event.
   1978 		 *
   1979 		 * It can be distingished which stage generates the event:
   1980 		 * + by checking least 3 bits of trb_0 if ED==1.
   1981 		 *   (see xhci_device_ctrl_start).
   1982 		 * + by checking the type of original TRB if ED==0.
   1983 		 *
   1984 		 * In addition, intr, bulk, and isoc transfer currently
   1985 		 * consists of single TD, so the "skip" is not needed.
   1986 		 * ctrl xfer uses EVENT_DATA, and others do not.
   1987 		 * Thus driver can switch the flow by checking ED bit.
   1988 		 */
   1989 		if ((trb_3 & XHCI_TRB_3_ED_BIT) == 0) {
   1990 			if (xfer->ux_actlen == 0)
   1991 				xfer->ux_actlen = xfer->ux_length -
   1992 				    XHCI_TRB_2_REM_GET(trb_2);
   1993 			if (XHCI_TRB_3_TYPE_GET(le32toh(xr->xr_trb[idx].trb_3))
   1994 			    == XHCI_TRB_TYPE_DATA_STAGE) {
   1995 				return;
   1996 			}
   1997 		} else if ((trb_0 & 0x3) == 0x3) {
   1998 			return;
   1999 		}
   2000 		err = USBD_NORMAL_COMPLETION;
   2001 		break;
   2002 	case XHCI_TRB_ERROR_STOPPED:
   2003 	case XHCI_TRB_ERROR_LENGTH:
   2004 	case XHCI_TRB_ERROR_STOPPED_SHORT:
   2005 		/*
   2006 		 * don't complete the transfer being aborted
   2007 		 * as abort_xfer does instead.
   2008 		 */
   2009 		if (xfer->ux_hcflags & UXFER_ABORTING) {
   2010 			DPRINTFN(14, "ignore aborting xfer %#jx",
   2011 			    (uintptr_t)xfer, 0, 0, 0);
   2012 			return;
   2013 		}
   2014 		err = USBD_CANCELLED;
   2015 		break;
   2016 	case XHCI_TRB_ERROR_STALL:
   2017 	case XHCI_TRB_ERROR_BABBLE:
   2018 		DPRINTFN(1, "ERR %ju slot %ju dci %ju", trbcode, slot, dci, 0);
   2019 		xr->is_halted = true;
   2020 		err = USBD_STALLED;
   2021 		/*
   2022 		 * Stalled endpoints can be recoverd by issuing
   2023 		 * command TRB TYPE_RESET_EP on xHCI instead of
   2024 		 * issuing request CLEAR_FEATURE UF_ENDPOINT_HALT
   2025 		 * on the endpoint. However, this function may be
   2026 		 * called from softint context (e.g. from umass),
   2027 		 * in that case driver gets KASSERT in cv_timedwait
   2028 		 * in xhci_do_command.
   2029 		 * To avoid this, this runs reset_endpoint and
   2030 		 * usb_transfer_complete in usb task thread
   2031 		 * asynchronously (and then umass issues clear
   2032 		 * UF_ENDPOINT_HALT).
   2033 		 */
   2034 		xfer->ux_status = err;
   2035 		callout_stop(&xfer->ux_callout);
   2036 		xhci_clear_endpoint_stall_async(xfer);
   2037 		return;
   2038 	default:
   2039 		DPRINTFN(1, "ERR %ju slot %ju dci %ju", trbcode, slot, dci, 0);
   2040 		err = USBD_IOERROR;
   2041 		break;
   2042 	}
   2043 	xfer->ux_status = err;
   2044 
   2045 	if ((trb_3 & XHCI_TRB_3_ED_BIT) != 0) {
   2046 		if ((trb_0 & 0x3) == 0x0) {
   2047 			callout_stop(&xfer->ux_callout);
   2048 			usb_transfer_complete(xfer);
   2049 		}
   2050 	} else {
   2051 		callout_stop(&xfer->ux_callout);
   2052 		usb_transfer_complete(xfer);
   2053 	}
   2054 }
   2055 
   2056 /* Process Command complete events */
   2057 static void
   2058 xhci_event_cmd(struct xhci_softc * const sc, const struct xhci_trb * const trb)
   2059 {
   2060 	uint64_t trb_0;
   2061 	uint32_t trb_2, trb_3;
   2062 
   2063 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2064 
   2065 	KASSERT(mutex_owned(&sc->sc_lock));
   2066 
   2067 	trb_0 = le64toh(trb->trb_0);
   2068 	trb_2 = le32toh(trb->trb_2);
   2069 	trb_3 = le32toh(trb->trb_3);
   2070 
   2071 	if (trb_0 == sc->sc_command_addr) {
   2072 		sc->sc_resultpending = false;
   2073 
   2074 		sc->sc_result_trb.trb_0 = trb_0;
   2075 		sc->sc_result_trb.trb_2 = trb_2;
   2076 		sc->sc_result_trb.trb_3 = trb_3;
   2077 		if (XHCI_TRB_2_ERROR_GET(trb_2) !=
   2078 		    XHCI_TRB_ERROR_SUCCESS) {
   2079 			DPRINTFN(1, "command completion "
   2080 			    "failure: 0x%016jx 0x%08jx 0x%08jx",
   2081 			    trb_0, trb_2, trb_3, 0);
   2082 		}
   2083 		cv_signal(&sc->sc_command_cv);
   2084 	} else {
   2085 		DPRINTFN(1, "spurious event: %#jx 0x%016jx "
   2086 		    "0x%08jx 0x%08jx", (uintptr_t)trb, trb_0, trb_2, trb_3);
   2087 	}
   2088 }
   2089 
   2090 /*
   2091  * Process events.
   2092  * called from xhci_softintr
   2093  */
   2094 static void
   2095 xhci_handle_event(struct xhci_softc * const sc,
   2096     const struct xhci_trb * const trb)
   2097 {
   2098 	uint64_t trb_0;
   2099 	uint32_t trb_2, trb_3;
   2100 
   2101 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2102 
   2103 	trb_0 = le64toh(trb->trb_0);
   2104 	trb_2 = le32toh(trb->trb_2);
   2105 	trb_3 = le32toh(trb->trb_3);
   2106 
   2107 	DPRINTFN(14, "event: %#jx 0x%016jx 0x%08jx 0x%08jx",
   2108 	    (uintptr_t)trb, trb_0, trb_2, trb_3);
   2109 
   2110 	/*
   2111 	 * 4.11.3.1, 6.4.2.1
   2112 	 * TRB Pointer is invalid for these completion codes.
   2113 	 */
   2114 	switch (XHCI_TRB_2_ERROR_GET(trb_2)) {
   2115 	case XHCI_TRB_ERROR_RING_UNDERRUN:
   2116 	case XHCI_TRB_ERROR_RING_OVERRUN:
   2117 	case XHCI_TRB_ERROR_VF_RING_FULL:
   2118 		return;
   2119 	default:
   2120 		if (trb_0 == 0) {
   2121 			return;
   2122 		}
   2123 		break;
   2124 	}
   2125 
   2126 	switch (XHCI_TRB_3_TYPE_GET(trb_3)) {
   2127 	case XHCI_TRB_EVENT_TRANSFER:
   2128 		xhci_event_transfer(sc, trb);
   2129 		break;
   2130 	case XHCI_TRB_EVENT_CMD_COMPLETE:
   2131 		xhci_event_cmd(sc, trb);
   2132 		break;
   2133 	case XHCI_TRB_EVENT_PORT_STS_CHANGE:
   2134 		xhci_rhpsc(sc, (uint32_t)((trb_0 >> 24) & 0xff));
   2135 		break;
   2136 	default:
   2137 		break;
   2138 	}
   2139 }
   2140 
   2141 static void
   2142 xhci_softintr(void *v)
   2143 {
   2144 	struct usbd_bus * const bus = v;
   2145 	struct xhci_softc * const sc = XHCI_BUS2SC(bus);
   2146 	struct xhci_ring * const er = &sc->sc_er;
   2147 	struct xhci_trb *trb;
   2148 	int i, j, k;
   2149 
   2150 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2151 
   2152 	KASSERT(xhci_polling_p(sc) || mutex_owned(&sc->sc_lock));
   2153 
   2154 	i = er->xr_ep;
   2155 	j = er->xr_cs;
   2156 
   2157 	DPRINTFN(16, "er: xr_ep %jd xr_cs %jd", i, j, 0, 0);
   2158 
   2159 	while (1) {
   2160 		usb_syncmem(&er->xr_dma, XHCI_TRB_SIZE * i, XHCI_TRB_SIZE,
   2161 		    BUS_DMASYNC_POSTREAD);
   2162 		trb = &er->xr_trb[i];
   2163 		k = (le32toh(trb->trb_3) & XHCI_TRB_3_CYCLE_BIT) ? 1 : 0;
   2164 
   2165 		if (j != k)
   2166 			break;
   2167 
   2168 		xhci_handle_event(sc, trb);
   2169 
   2170 		i++;
   2171 		if (i == er->xr_ntrb) {
   2172 			i = 0;
   2173 			j ^= 1;
   2174 		}
   2175 	}
   2176 
   2177 	er->xr_ep = i;
   2178 	er->xr_cs = j;
   2179 
   2180 	xhci_rt_write_8(sc, XHCI_ERDP(0), xhci_ring_trbp(er, er->xr_ep) |
   2181 	    XHCI_ERDP_LO_BUSY);
   2182 
   2183 	DPRINTFN(16, "ends", 0, 0, 0, 0);
   2184 
   2185 	return;
   2186 }
   2187 
   2188 static void
   2189 xhci_poll(struct usbd_bus *bus)
   2190 {
   2191 	struct xhci_softc * const sc = XHCI_BUS2SC(bus);
   2192 
   2193 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2194 
   2195 	mutex_enter(&sc->sc_intr_lock);
   2196 	int ret = xhci_intr1(sc);
   2197 	if (ret) {
   2198 		xhci_softintr(bus);
   2199 	}
   2200 	mutex_exit(&sc->sc_intr_lock);
   2201 
   2202 	return;
   2203 }
   2204 
   2205 static struct usbd_xfer *
   2206 xhci_allocx(struct usbd_bus *bus, unsigned int nframes)
   2207 {
   2208 	struct xhci_softc * const sc = XHCI_BUS2SC(bus);
   2209 	struct usbd_xfer *xfer;
   2210 
   2211 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2212 
   2213 	xfer = pool_cache_get(sc->sc_xferpool, PR_WAITOK);
   2214 	if (xfer != NULL) {
   2215 		memset(xfer, 0, sizeof(struct xhci_xfer));
   2216 #ifdef DIAGNOSTIC
   2217 		xfer->ux_state = XFER_BUSY;
   2218 #endif
   2219 	}
   2220 
   2221 	return xfer;
   2222 }
   2223 
   2224 static void
   2225 xhci_freex(struct usbd_bus *bus, struct usbd_xfer *xfer)
   2226 {
   2227 	struct xhci_softc * const sc = XHCI_BUS2SC(bus);
   2228 
   2229 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2230 
   2231 #ifdef DIAGNOSTIC
   2232 	if (xfer->ux_state != XFER_BUSY) {
   2233 		DPRINTFN(0, "xfer=%#jx not busy, 0x%08jx",
   2234 		    (uintptr_t)xfer, xfer->ux_state, 0, 0);
   2235 	}
   2236 	xfer->ux_state = XFER_FREE;
   2237 #endif
   2238 	pool_cache_put(sc->sc_xferpool, xfer);
   2239 }
   2240 
   2241 static void
   2242 xhci_get_lock(struct usbd_bus *bus, kmutex_t **lock)
   2243 {
   2244 	struct xhci_softc * const sc = XHCI_BUS2SC(bus);
   2245 
   2246 	*lock = &sc->sc_lock;
   2247 }
   2248 
   2249 extern uint32_t usb_cookie_no;
   2250 
   2251 /*
   2252  * xHCI 4.3
   2253  * Called when uhub_explore finds a new device (via usbd_new_device).
   2254  * Port initialization and speed detection (4.3.1) are already done in uhub.c.
   2255  * This function does:
   2256  *   Allocate and construct dev structure of default endpoint (ep0).
   2257  *   Allocate and open pipe of ep0.
   2258  *   Enable slot and initialize slot context.
   2259  *   Set Address.
   2260  *   Read initial device descriptor.
   2261  *   Determine initial MaxPacketSize (mps) by speed.
   2262  *   Read full device descriptor.
   2263  *   Register this device.
   2264  * Finally state of device transitions ADDRESSED.
   2265  */
   2266 static usbd_status
   2267 xhci_new_device(device_t parent, struct usbd_bus *bus, int depth,
   2268     int speed, int port, struct usbd_port *up)
   2269 {
   2270 	struct xhci_softc * const sc = XHCI_BUS2SC(bus);
   2271 	struct usbd_device *dev;
   2272 	usbd_status err;
   2273 	usb_device_descriptor_t *dd;
   2274 	struct xhci_slot *xs;
   2275 	uint32_t *cp;
   2276 
   2277 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2278 	DPRINTFN(4, "port %ju depth %ju speed %ju up %#jx",
   2279 	    port, depth, speed, (uintptr_t)up);
   2280 
   2281 	dev = kmem_zalloc(sizeof(*dev), KM_SLEEP);
   2282 	dev->ud_bus = bus;
   2283 	dev->ud_quirks = &usbd_no_quirk;
   2284 	dev->ud_addr = 0;
   2285 	dev->ud_ddesc.bMaxPacketSize = 0;
   2286 	dev->ud_depth = depth;
   2287 	dev->ud_powersrc = up;
   2288 	dev->ud_myhub = up->up_parent;
   2289 	dev->ud_speed = speed;
   2290 	dev->ud_langid = USBD_NOLANG;
   2291 	dev->ud_cookie.cookie = ++usb_cookie_no;
   2292 
   2293 	/* Set up default endpoint handle. */
   2294 	dev->ud_ep0.ue_edesc = &dev->ud_ep0desc;
   2295 	/* doesn't matter, just don't let it uninitialized */
   2296 	dev->ud_ep0.ue_toggle = 0;
   2297 
   2298 	/* Set up default endpoint descriptor. */
   2299 	dev->ud_ep0desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
   2300 	dev->ud_ep0desc.bDescriptorType = UDESC_ENDPOINT;
   2301 	dev->ud_ep0desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
   2302 	dev->ud_ep0desc.bmAttributes = UE_CONTROL;
   2303 	dev->ud_ep0desc.bInterval = 0;
   2304 
   2305 	/* 4.3,  4.8.2.1 */
   2306 	switch (speed) {
   2307 	case USB_SPEED_SUPER:
   2308 	case USB_SPEED_SUPER_PLUS:
   2309 		USETW(dev->ud_ep0desc.wMaxPacketSize, USB_3_MAX_CTRL_PACKET);
   2310 		break;
   2311 	case USB_SPEED_FULL:
   2312 		/* XXX using 64 as initial mps of ep0 in FS */
   2313 	case USB_SPEED_HIGH:
   2314 		USETW(dev->ud_ep0desc.wMaxPacketSize, USB_2_MAX_CTRL_PACKET);
   2315 		break;
   2316 	case USB_SPEED_LOW:
   2317 	default:
   2318 		USETW(dev->ud_ep0desc.wMaxPacketSize, USB_MAX_IPACKET);
   2319 		break;
   2320 	}
   2321 
   2322 	up->up_dev = dev;
   2323 
   2324 	/* Establish the default pipe. */
   2325 	err = usbd_setup_pipe(dev, 0, &dev->ud_ep0, USBD_DEFAULT_INTERVAL,
   2326 	    &dev->ud_pipe0);
   2327 	if (err) {
   2328 		goto bad;
   2329 	}
   2330 
   2331 	dd = &dev->ud_ddesc;
   2332 
   2333 	if (depth == 0 && port == 0) {
   2334 		KASSERT(bus->ub_devices[USB_ROOTHUB_INDEX] == NULL);
   2335 		bus->ub_devices[USB_ROOTHUB_INDEX] = dev;
   2336 		err = usbd_get_initial_ddesc(dev, dd);
   2337 		if (err) {
   2338 			DPRINTFN(1, "get_initial_ddesc %ju", err, 0, 0, 0);
   2339 			goto bad;
   2340 		}
   2341 
   2342 		err = usbd_reload_device_desc(dev);
   2343 		if (err) {
   2344 			DPRINTFN(1, "reload desc %ju", err, 0, 0, 0);
   2345 			goto bad;
   2346 		}
   2347 	} else {
   2348 		uint8_t slot = 0;
   2349 
   2350 		/* 4.3.2 */
   2351 		err = xhci_enable_slot(sc, &slot);
   2352 		if (err) {
   2353 			DPRINTFN(1, "enable slot %ju", err, 0, 0, 0);
   2354 			goto bad;
   2355 		}
   2356 
   2357 		xs = &sc->sc_slots[slot];
   2358 		dev->ud_hcpriv = xs;
   2359 
   2360 		/* 4.3.3 initialize slot structure */
   2361 		err = xhci_init_slot(dev, slot);
   2362 		if (err) {
   2363 			DPRINTFN(1, "init slot %ju", err, 0, 0, 0);
   2364 			dev->ud_hcpriv = NULL;
   2365 			/*
   2366 			 * We have to disable_slot here because
   2367 			 * xs->xs_idx == 0 when xhci_init_slot fails,
   2368 			 * in that case usbd_remove_dev won't work.
   2369 			 */
   2370 			mutex_enter(&sc->sc_lock);
   2371 			xhci_disable_slot(sc, slot);
   2372 			mutex_exit(&sc->sc_lock);
   2373 			goto bad;
   2374 		}
   2375 
   2376 		/* 4.3.4 Address Assignment */
   2377 		err = xhci_set_address(dev, slot, false);
   2378 		if (err) {
   2379 			DPRINTFN(1, "set address w/o bsr %ju", err, 0, 0, 0);
   2380 			goto bad;
   2381 		}
   2382 
   2383 		/* Allow device time to set new address */
   2384 		usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
   2385 
   2386 		usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
   2387 		cp = xhci_slot_get_dcv(sc, xs, XHCI_DCI_SLOT);
   2388 		HEXDUMP("slot context", cp, sc->sc_ctxsz);
   2389 		uint8_t addr = XHCI_SCTX_3_DEV_ADDR_GET(le32toh(cp[3]));
   2390 		DPRINTFN(4, "device address %ju", addr, 0, 0, 0);
   2391 		/*
   2392 		 * XXX ensure we know when the hardware does something
   2393 		 * we can't yet cope with
   2394 		 */
   2395 		KASSERTMSG(addr >= 1 && addr <= 127, "addr %d", addr);
   2396 		dev->ud_addr = addr;
   2397 
   2398 		KASSERTMSG(bus->ub_devices[usb_addr2dindex(dev->ud_addr)] == NULL,
   2399 		    "addr %d already allocated", dev->ud_addr);
   2400 		/*
   2401 		 * The root hub is given its own slot
   2402 		 */
   2403 		bus->ub_devices[usb_addr2dindex(dev->ud_addr)] = dev;
   2404 
   2405 		err = usbd_get_initial_ddesc(dev, dd);
   2406 		if (err) {
   2407 			DPRINTFN(1, "get_initial_ddesc %ju", err, 0, 0, 0);
   2408 			goto bad;
   2409 		}
   2410 
   2411 		/* 4.8.2.1 */
   2412 		if (USB_IS_SS(speed)) {
   2413 			if (dd->bMaxPacketSize != 9) {
   2414 				printf("%s: invalid mps 2^%u for SS ep0,"
   2415 				    " using 512\n",
   2416 				    device_xname(sc->sc_dev),
   2417 				    dd->bMaxPacketSize);
   2418 				dd->bMaxPacketSize = 9;
   2419 			}
   2420 			USETW(dev->ud_ep0desc.wMaxPacketSize,
   2421 			    (1 << dd->bMaxPacketSize));
   2422 		} else
   2423 			USETW(dev->ud_ep0desc.wMaxPacketSize,
   2424 			    dd->bMaxPacketSize);
   2425 		DPRINTFN(4, "bMaxPacketSize %ju", dd->bMaxPacketSize, 0, 0, 0);
   2426 		err = xhci_update_ep0_mps(sc, xs,
   2427 		    UGETW(dev->ud_ep0desc.wMaxPacketSize));
   2428 		if (err) {
   2429 			DPRINTFN(1, "update mps of ep0 %ju", err, 0, 0, 0);
   2430 			goto bad;
   2431 		}
   2432 
   2433 		err = usbd_reload_device_desc(dev);
   2434 		if (err) {
   2435 			DPRINTFN(1, "reload desc %ju", err, 0, 0, 0);
   2436 			goto bad;
   2437 		}
   2438 	}
   2439 
   2440 	DPRINTFN(1, "adding unit addr=%jd, rev=%02jx,",
   2441 		dev->ud_addr, UGETW(dd->bcdUSB), 0, 0);
   2442 	DPRINTFN(1, " class=%jd, subclass=%jd, protocol=%jd,",
   2443 		dd->bDeviceClass, dd->bDeviceSubClass,
   2444 		dd->bDeviceProtocol, 0);
   2445 	DPRINTFN(1, " mps=%jd, len=%jd, noconf=%jd, speed=%jd",
   2446 		dd->bMaxPacketSize, dd->bLength, dd->bNumConfigurations,
   2447 		dev->ud_speed);
   2448 
   2449 	usbd_get_device_strings(dev);
   2450 
   2451 	usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
   2452 
   2453 	if (depth == 0 && port == 0) {
   2454 		usbd_attach_roothub(parent, dev);
   2455 		DPRINTFN(1, "root hub %#jx", (uintptr_t)dev, 0, 0, 0);
   2456 		return USBD_NORMAL_COMPLETION;
   2457 	}
   2458 
   2459 	err = usbd_probe_and_attach(parent, dev, port, dev->ud_addr);
   2460  bad:
   2461 	if (err != USBD_NORMAL_COMPLETION) {
   2462 		usbd_remove_device(dev, up);
   2463 	}
   2464 
   2465 	return err;
   2466 }
   2467 
   2468 static usbd_status
   2469 xhci_ring_init(struct xhci_softc * const sc, struct xhci_ring * const xr,
   2470     size_t ntrb, size_t align)
   2471 {
   2472 	usbd_status err;
   2473 	size_t size = ntrb * XHCI_TRB_SIZE;
   2474 
   2475 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2476 
   2477 	err = usb_allocmem(&sc->sc_bus, size, align, &xr->xr_dma);
   2478 	if (err)
   2479 		return err;
   2480 	mutex_init(&xr->xr_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
   2481 	xr->xr_cookies = kmem_zalloc(sizeof(*xr->xr_cookies) * ntrb, KM_SLEEP);
   2482 	xr->xr_trb = xhci_ring_trbv(xr, 0);
   2483 	xr->xr_ntrb = ntrb;
   2484 	xr->is_halted = false;
   2485 	xhci_host_dequeue(xr);
   2486 
   2487 	return USBD_NORMAL_COMPLETION;
   2488 }
   2489 
   2490 static void
   2491 xhci_ring_free(struct xhci_softc * const sc, struct xhci_ring * const xr)
   2492 {
   2493 	usb_freemem(&sc->sc_bus, &xr->xr_dma);
   2494 	mutex_destroy(&xr->xr_lock);
   2495 	kmem_free(xr->xr_cookies, sizeof(*xr->xr_cookies) * xr->xr_ntrb);
   2496 }
   2497 
   2498 static void
   2499 xhci_ring_put(struct xhci_softc * const sc, struct xhci_ring * const xr,
   2500     void *cookie, struct xhci_trb * const trbs, size_t ntrbs)
   2501 {
   2502 	size_t i;
   2503 	u_int ri;
   2504 	u_int cs;
   2505 	uint64_t parameter;
   2506 	uint32_t status;
   2507 	uint32_t control;
   2508 
   2509 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2510 
   2511 	KASSERTMSG(ntrbs <= XHCI_XFER_NTRB, "ntrbs %zu", ntrbs);
   2512 	for (i = 0; i < ntrbs; i++) {
   2513 		DPRINTFN(12, "xr %#jx trbs %#jx num %ju", (uintptr_t)xr,
   2514 		    (uintptr_t)trbs, i, 0);
   2515 		DPRINTFN(12, " %016jx %08jx %08jx",
   2516 		    trbs[i].trb_0, trbs[i].trb_2, trbs[i].trb_3, 0);
   2517 		KASSERTMSG(XHCI_TRB_3_TYPE_GET(trbs[i].trb_3) !=
   2518 		    XHCI_TRB_TYPE_LINK, "trbs[%zu].trb3 %#x", i, trbs[i].trb_3);
   2519 	}
   2520 
   2521 	DPRINTFN(12, "%#jx xr_ep 0x%jx xr_cs %ju", (uintptr_t)xr, xr->xr_ep,
   2522 	    xr->xr_cs, 0);
   2523 
   2524 	ri = xr->xr_ep;
   2525 	cs = xr->xr_cs;
   2526 
   2527 	/*
   2528 	 * Although the xhci hardware can do scatter/gather dma from
   2529 	 * arbitrary sized buffers, there is a non-obvious restriction
   2530 	 * that a LINK trb is only allowed at the end of a burst of
   2531 	 * transfers - which might be 16kB.
   2532 	 * Arbitrary aligned LINK trb definitely fail on Ivy bridge.
   2533 	 * The simple solution is not to allow a LINK trb in the middle
   2534 	 * of anything - as here.
   2535 	 * XXX: (dsl) There are xhci controllers out there (eg some made by
   2536 	 * ASMedia) that seem to lock up if they process a LINK trb but
   2537 	 * cannot process the linked-to trb yet.
   2538 	 * The code should write the 'cycle' bit on the link trb AFTER
   2539 	 * adding the other trb.
   2540 	 */
   2541 	u_int firstep = xr->xr_ep;
   2542 	u_int firstcs = xr->xr_cs;
   2543 
   2544 	for (i = 0; i < ntrbs; ) {
   2545 		u_int oldri = ri;
   2546 		u_int oldcs = cs;
   2547 
   2548 		if (ri >= (xr->xr_ntrb - 1)) {
   2549 			/* Put Link TD at the end of ring */
   2550 			parameter = xhci_ring_trbp(xr, 0);
   2551 			status = 0;
   2552 			control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) |
   2553 			    XHCI_TRB_3_TC_BIT;
   2554 			xr->xr_cookies[ri] = NULL;
   2555 			xr->xr_ep = 0;
   2556 			xr->xr_cs ^= 1;
   2557 			ri = xr->xr_ep;
   2558 			cs = xr->xr_cs;
   2559 		} else {
   2560 			parameter = trbs[i].trb_0;
   2561 			status = trbs[i].trb_2;
   2562 			control = trbs[i].trb_3;
   2563 
   2564 			xr->xr_cookies[ri] = cookie;
   2565 			ri++;
   2566 			i++;
   2567 		}
   2568 		/*
   2569 		 * If this is a first TRB, mark it invalid to prevent
   2570 		 * xHC from running it immediately.
   2571 		 */
   2572 		if (oldri == firstep) {
   2573 			if (oldcs) {
   2574 				control &= ~XHCI_TRB_3_CYCLE_BIT;
   2575 			} else {
   2576 				control |= XHCI_TRB_3_CYCLE_BIT;
   2577 			}
   2578 		} else {
   2579 			if (oldcs) {
   2580 				control |= XHCI_TRB_3_CYCLE_BIT;
   2581 			} else {
   2582 				control &= ~XHCI_TRB_3_CYCLE_BIT;
   2583 			}
   2584 		}
   2585 		xhci_trb_put(&xr->xr_trb[oldri], parameter, status, control);
   2586 		usb_syncmem(&xr->xr_dma, XHCI_TRB_SIZE * oldri,
   2587 		    XHCI_TRB_SIZE * 1, BUS_DMASYNC_PREWRITE);
   2588 	}
   2589 
   2590 	/* Now invert cycle bit of first TRB */
   2591 	if (firstcs) {
   2592 		xr->xr_trb[firstep].trb_3 |= htole32(XHCI_TRB_3_CYCLE_BIT);
   2593 	} else {
   2594 		xr->xr_trb[firstep].trb_3 &= ~htole32(XHCI_TRB_3_CYCLE_BIT);
   2595 	}
   2596 	usb_syncmem(&xr->xr_dma, XHCI_TRB_SIZE * firstep,
   2597 	    XHCI_TRB_SIZE * 1, BUS_DMASYNC_PREWRITE);
   2598 
   2599 	xr->xr_ep = ri;
   2600 	xr->xr_cs = cs;
   2601 
   2602 	DPRINTFN(12, "%#jx xr_ep 0x%jx xr_cs %ju", (uintptr_t)xr, xr->xr_ep,
   2603 	    xr->xr_cs, 0);
   2604 }
   2605 
   2606 /*
   2607  * Stop execution commands, purge all commands on command ring, and
   2608  * rewind dequeue pointer.
   2609  */
   2610 static void
   2611 xhci_abort_command(struct xhci_softc *sc)
   2612 {
   2613 	struct xhci_ring * const cr = &sc->sc_cr;
   2614 	uint64_t crcr;
   2615 	int i;
   2616 
   2617 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2618 	DPRINTFN(14, "command %#jx timeout, aborting",
   2619 	    sc->sc_command_addr, 0, 0, 0);
   2620 
   2621 	mutex_enter(&cr->xr_lock);
   2622 
   2623 	/* 4.6.1.2 Aborting a Command */
   2624 	crcr = xhci_op_read_8(sc, XHCI_CRCR);
   2625 	xhci_op_write_8(sc, XHCI_CRCR, crcr | XHCI_CRCR_LO_CA);
   2626 
   2627 	for (i = 0; i < 500; i++) {
   2628 		crcr = xhci_op_read_8(sc, XHCI_CRCR);
   2629 		if ((crcr & XHCI_CRCR_LO_CRR) == 0)
   2630 			break;
   2631 		usb_delay_ms(&sc->sc_bus, 1);
   2632 	}
   2633 	if ((crcr & XHCI_CRCR_LO_CRR) != 0) {
   2634 		DPRINTFN(1, "Command Abort timeout", 0, 0, 0, 0);
   2635 		/* reset HC here? */
   2636 	}
   2637 
   2638 	/* reset command ring dequeue pointer */
   2639 	cr->xr_ep = 0;
   2640 	cr->xr_cs = 1;
   2641 	xhci_op_write_8(sc, XHCI_CRCR, xhci_ring_trbp(cr, 0) | cr->xr_cs);
   2642 
   2643 	mutex_exit(&cr->xr_lock);
   2644 }
   2645 
   2646 /*
   2647  * Put a command on command ring, ring bell, set timer, and cv_timedwait.
   2648  * Command completion is notified by cv_signal from xhci_event_cmd()
   2649  * (called from xhci_softint), or timed-out.
   2650  * The completion code is copied to sc->sc_result_trb in xhci_event_cmd(),
   2651  * then do_command examines it.
   2652  */
   2653 static usbd_status
   2654 xhci_do_command_locked(struct xhci_softc * const sc,
   2655     struct xhci_trb * const trb, int timeout)
   2656 {
   2657 	struct xhci_ring * const cr = &sc->sc_cr;
   2658 	usbd_status err;
   2659 
   2660 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2661 	DPRINTFN(12, "input: 0x%016jx 0x%08jx 0x%08jx",
   2662 	    trb->trb_0, trb->trb_2, trb->trb_3, 0);
   2663 
   2664 	KASSERTMSG(!cpu_intr_p() && !cpu_softintr_p(), "called from intr ctx");
   2665 	KASSERT(mutex_owned(&sc->sc_lock));
   2666 
   2667 	while (sc->sc_command_addr != 0)
   2668 		cv_wait(&sc->sc_cmdbusy_cv, &sc->sc_lock);
   2669 
   2670 	/*
   2671 	 * If enqueue pointer points at last of ring, it's Link TRB,
   2672 	 * command TRB will be stored in 0th TRB.
   2673 	 */
   2674 	if (cr->xr_ep == cr->xr_ntrb - 1)
   2675 		sc->sc_command_addr = xhci_ring_trbp(cr, 0);
   2676 	else
   2677 		sc->sc_command_addr = xhci_ring_trbp(cr, cr->xr_ep);
   2678 
   2679 	sc->sc_resultpending = true;
   2680 
   2681 	mutex_enter(&cr->xr_lock);
   2682 	xhci_ring_put(sc, cr, NULL, trb, 1);
   2683 	mutex_exit(&cr->xr_lock);
   2684 
   2685 	xhci_db_write_4(sc, XHCI_DOORBELL(0), 0);
   2686 
   2687 	while (sc->sc_resultpending) {
   2688 		if (cv_timedwait(&sc->sc_command_cv, &sc->sc_lock,
   2689 		    MAX(1, mstohz(timeout))) == EWOULDBLOCK) {
   2690 			xhci_abort_command(sc);
   2691 			err = USBD_TIMEOUT;
   2692 			goto timedout;
   2693 		}
   2694 	}
   2695 
   2696 	trb->trb_0 = sc->sc_result_trb.trb_0;
   2697 	trb->trb_2 = sc->sc_result_trb.trb_2;
   2698 	trb->trb_3 = sc->sc_result_trb.trb_3;
   2699 
   2700 	DPRINTFN(12, "output: 0x%016jx 0x%08jx 0x%08jx",
   2701 	    trb->trb_0, trb->trb_2, trb->trb_3, 0);
   2702 
   2703 	switch (XHCI_TRB_2_ERROR_GET(trb->trb_2)) {
   2704 	case XHCI_TRB_ERROR_SUCCESS:
   2705 		err = USBD_NORMAL_COMPLETION;
   2706 		break;
   2707 	default:
   2708 	case 192 ... 223:
   2709 		err = USBD_IOERROR;
   2710 		break;
   2711 	case 224 ... 255:
   2712 		err = USBD_NORMAL_COMPLETION;
   2713 		break;
   2714 	}
   2715 
   2716 timedout:
   2717 	sc->sc_resultpending = false;
   2718 	sc->sc_command_addr = 0;
   2719 	cv_broadcast(&sc->sc_cmdbusy_cv);
   2720 
   2721 	return err;
   2722 }
   2723 
   2724 static usbd_status
   2725 xhci_do_command(struct xhci_softc * const sc, struct xhci_trb * const trb,
   2726     int timeout)
   2727 {
   2728 
   2729 	mutex_enter(&sc->sc_lock);
   2730 	usbd_status ret = xhci_do_command_locked(sc, trb, timeout);
   2731 	mutex_exit(&sc->sc_lock);
   2732 
   2733 	return ret;
   2734 }
   2735 
   2736 static usbd_status
   2737 xhci_enable_slot(struct xhci_softc * const sc, uint8_t * const slotp)
   2738 {
   2739 	struct xhci_trb trb;
   2740 	usbd_status err;
   2741 
   2742 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2743 
   2744 	trb.trb_0 = 0;
   2745 	trb.trb_2 = 0;
   2746 	trb.trb_3 = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ENABLE_SLOT);
   2747 
   2748 	err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
   2749 	if (err != USBD_NORMAL_COMPLETION) {
   2750 		return err;
   2751 	}
   2752 
   2753 	*slotp = XHCI_TRB_3_SLOT_GET(trb.trb_3);
   2754 
   2755 	return err;
   2756 }
   2757 
   2758 /*
   2759  * xHCI 4.6.4
   2760  * Deallocate ring and device/input context DMA buffers, and disable_slot.
   2761  * All endpoints in the slot should be stopped.
   2762  * Should be called with sc_lock held.
   2763  */
   2764 static usbd_status
   2765 xhci_disable_slot(struct xhci_softc * const sc, uint8_t slot)
   2766 {
   2767 	struct xhci_trb trb;
   2768 	struct xhci_slot *xs;
   2769 	usbd_status err;
   2770 
   2771 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2772 
   2773 	if (sc->sc_dying)
   2774 		return USBD_IOERROR;
   2775 
   2776 	trb.trb_0 = 0;
   2777 	trb.trb_2 = 0;
   2778 	trb.trb_3 = htole32(
   2779 		XHCI_TRB_3_SLOT_SET(slot) |
   2780 		XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DISABLE_SLOT));
   2781 
   2782 	err = xhci_do_command_locked(sc, &trb, USBD_DEFAULT_TIMEOUT);
   2783 
   2784 	if (!err) {
   2785 		xs = &sc->sc_slots[slot];
   2786 		if (xs->xs_idx != 0) {
   2787 			xhci_free_slot(sc, xs, XHCI_DCI_SLOT + 1, 32);
   2788 			xhci_set_dcba(sc, 0, slot);
   2789 			memset(xs, 0, sizeof(*xs));
   2790 		}
   2791 	}
   2792 
   2793 	return err;
   2794 }
   2795 
   2796 /*
   2797  * Set address of device and transition slot state from ENABLED to ADDRESSED
   2798  * if Block Setaddress Request (BSR) is false.
   2799  * If BSR==true, transition slot state from ENABLED to DEFAULT.
   2800  * see xHCI 1.1  4.5.3, 3.3.4
   2801  * Should be called without sc_lock held.
   2802  */
   2803 static usbd_status
   2804 xhci_address_device(struct xhci_softc * const sc,
   2805     uint64_t icp, uint8_t slot_id, bool bsr)
   2806 {
   2807 	struct xhci_trb trb;
   2808 	usbd_status err;
   2809 
   2810 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2811 
   2812 	trb.trb_0 = icp;
   2813 	trb.trb_2 = 0;
   2814 	trb.trb_3 = XHCI_TRB_3_SLOT_SET(slot_id) |
   2815 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ADDRESS_DEVICE) |
   2816 	    (bsr ? XHCI_TRB_3_BSR_BIT : 0);
   2817 
   2818 	err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
   2819 
   2820 	if (XHCI_TRB_2_ERROR_GET(trb.trb_2) == XHCI_TRB_ERROR_NO_SLOTS)
   2821 		err = USBD_NO_ADDR;
   2822 
   2823 	return err;
   2824 }
   2825 
   2826 static usbd_status
   2827 xhci_update_ep0_mps(struct xhci_softc * const sc,
   2828     struct xhci_slot * const xs, u_int mps)
   2829 {
   2830 	struct xhci_trb trb;
   2831 	usbd_status err;
   2832 	uint32_t * cp;
   2833 
   2834 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2835 	DPRINTFN(4, "slot %ju mps %ju", xs->xs_idx, mps, 0, 0);
   2836 
   2837 	cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL);
   2838 	cp[0] = htole32(0);
   2839 	cp[1] = htole32(XHCI_INCTX_1_ADD_MASK(XHCI_DCI_EP_CONTROL));
   2840 
   2841 	cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_EP_CONTROL));
   2842 	cp[1] = htole32(XHCI_EPCTX_1_MAXP_SIZE_SET(mps));
   2843 
   2844 	/* sync input contexts before they are read from memory */
   2845 	usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE);
   2846 	HEXDUMP("input context", xhci_slot_get_icv(sc, xs, 0),
   2847 	    sc->sc_ctxsz * 4);
   2848 
   2849 	trb.trb_0 = xhci_slot_get_icp(sc, xs, 0);
   2850 	trb.trb_2 = 0;
   2851 	trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
   2852 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVALUATE_CTX);
   2853 
   2854 	err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
   2855 	return err;
   2856 }
   2857 
   2858 static void
   2859 xhci_set_dcba(struct xhci_softc * const sc, uint64_t dcba, int si)
   2860 {
   2861 	uint64_t * const dcbaa = KERNADDR(&sc->sc_dcbaa_dma, 0);
   2862 
   2863 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2864 	DPRINTFN(4, "dcbaa %#jx dc %016jx slot %jd",
   2865 	    (uintptr_t)&dcbaa[si], dcba, si, 0);
   2866 
   2867 	dcbaa[si] = htole64(dcba);
   2868 	usb_syncmem(&sc->sc_dcbaa_dma, si * sizeof(uint64_t), sizeof(uint64_t),
   2869 	    BUS_DMASYNC_PREWRITE);
   2870 }
   2871 
   2872 /*
   2873  * Allocate device and input context DMA buffer, and
   2874  * TRB DMA buffer for each endpoint.
   2875  */
   2876 static usbd_status
   2877 xhci_init_slot(struct usbd_device *dev, uint32_t slot)
   2878 {
   2879 	struct xhci_softc * const sc = XHCI_BUS2SC(dev->ud_bus);
   2880 	struct xhci_slot *xs;
   2881 	usbd_status err;
   2882 	u_int dci;
   2883 
   2884 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2885 	DPRINTFN(4, "slot %ju", slot, 0, 0, 0);
   2886 
   2887 	xs = &sc->sc_slots[slot];
   2888 
   2889 	/* allocate contexts */
   2890 	err = usb_allocmem(&sc->sc_bus, sc->sc_pgsz, sc->sc_pgsz,
   2891 	    &xs->xs_dc_dma);
   2892 	if (err)
   2893 		return err;
   2894 	memset(KERNADDR(&xs->xs_dc_dma, 0), 0, sc->sc_pgsz);
   2895 
   2896 	err = usb_allocmem(&sc->sc_bus, sc->sc_pgsz, sc->sc_pgsz,
   2897 	    &xs->xs_ic_dma);
   2898 	if (err)
   2899 		goto bad1;
   2900 	memset(KERNADDR(&xs->xs_ic_dma, 0), 0, sc->sc_pgsz);
   2901 
   2902 	for (dci = 0; dci < 32; dci++) {
   2903 		//CTASSERT(sizeof(xs->xs_ep[dci]) == sizeof(struct xhci_endpoint));
   2904 		memset(&xs->xs_ep[dci], 0, sizeof(xs->xs_ep[dci]));
   2905 		if (dci == XHCI_DCI_SLOT)
   2906 			continue;
   2907 		err = xhci_ring_init(sc, &xs->xs_ep[dci].xe_tr,
   2908 		    XHCI_TRANSFER_RING_TRBS, XHCI_TRB_ALIGN);
   2909 		if (err) {
   2910 			DPRINTFN(0, "ring init failure", 0, 0, 0, 0);
   2911 			goto bad2;
   2912 		}
   2913 	}
   2914 
   2915  bad2:
   2916 	if (err == USBD_NORMAL_COMPLETION) {
   2917 		xs->xs_idx = slot;
   2918 	} else {
   2919 		xhci_free_slot(sc, xs, XHCI_DCI_SLOT + 1, dci);
   2920 	}
   2921 
   2922 	return err;
   2923 
   2924  bad1:
   2925 	usb_freemem(&sc->sc_bus, &xs->xs_dc_dma);
   2926 	xs->xs_idx = 0;
   2927 	return err;
   2928 }
   2929 
   2930 static void
   2931 xhci_free_slot(struct xhci_softc *sc, struct xhci_slot *xs, int start_dci,
   2932     int end_dci)
   2933 {
   2934 	u_int dci;
   2935 
   2936 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2937 	DPRINTFN(4, "slot %ju start %ju end %ju", xs->xs_idx, start_dci,
   2938 	    end_dci, 0);
   2939 
   2940 	for (dci = start_dci; dci < end_dci; dci++) {
   2941 		xhci_ring_free(sc, &xs->xs_ep[dci].xe_tr);
   2942 		memset(&xs->xs_ep[dci], 0, sizeof(xs->xs_ep[dci]));
   2943 	}
   2944 	usb_freemem(&sc->sc_bus, &xs->xs_ic_dma);
   2945 	usb_freemem(&sc->sc_bus, &xs->xs_dc_dma);
   2946 	xs->xs_idx = 0;
   2947 }
   2948 
   2949 /*
   2950  * Setup slot context, set Device Context Base Address, and issue
   2951  * Set Address Device command.
   2952  */
   2953 static usbd_status
   2954 xhci_set_address(struct usbd_device *dev, uint32_t slot, bool bsr)
   2955 {
   2956 	struct xhci_softc * const sc = XHCI_BUS2SC(dev->ud_bus);
   2957 	struct xhci_slot *xs;
   2958 	usbd_status err;
   2959 
   2960 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2961 	DPRINTFN(4, "slot %ju bsr %ju", slot, bsr, 0, 0);
   2962 
   2963 	xs = &sc->sc_slots[slot];
   2964 
   2965 	xhci_setup_ctx(dev->ud_pipe0);
   2966 
   2967 	HEXDUMP("input context", xhci_slot_get_icv(sc, xs, 0),
   2968 	    sc->sc_ctxsz * 3);
   2969 
   2970 	xhci_set_dcba(sc, DMAADDR(&xs->xs_dc_dma, 0), slot);
   2971 
   2972 	err = xhci_address_device(sc, xhci_slot_get_icp(sc, xs, 0), slot, bsr);
   2973 
   2974 	usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
   2975 	HEXDUMP("output context", xhci_slot_get_dcv(sc, xs, 0),
   2976 	    sc->sc_ctxsz * 2);
   2977 
   2978 	return err;
   2979 }
   2980 
   2981 /*
   2982  * 4.8.2, 6.2.3.2
   2983  * construct slot/endpoint context parameters and do syncmem
   2984  */
   2985 static void
   2986 xhci_setup_ctx(struct usbd_pipe *pipe)
   2987 {
   2988 	struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
   2989 	struct usbd_device *dev = pipe->up_dev;
   2990 	struct xhci_slot * const xs = dev->ud_hcpriv;
   2991 	usb_endpoint_descriptor_t * const ed = pipe->up_endpoint->ue_edesc;
   2992 	const u_int dci = xhci_ep_get_dci(ed);
   2993 	const uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
   2994 	uint32_t *cp;
   2995 	uint16_t mps = UGETW(ed->wMaxPacketSize);
   2996 	uint8_t speed = dev->ud_speed;
   2997 	uint8_t ival = ed->bInterval;
   2998 
   2999 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   3000 	DPRINTFN(4, "pipe %#jx: slot %ju dci %ju speed %ju",
   3001 	    (uintptr_t)pipe, xs->xs_idx, dci, speed);
   3002 
   3003 	/* set up initial input control context */
   3004 	cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL);
   3005 	cp[0] = htole32(0);
   3006 	cp[1] = htole32(XHCI_INCTX_1_ADD_MASK(dci));
   3007 	cp[1] |= htole32(XHCI_INCTX_1_ADD_MASK(XHCI_DCI_SLOT));
   3008 	cp[7] = htole32(0);
   3009 
   3010 	/* set up input slot context */
   3011 	cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_SLOT));
   3012 	cp[0] =
   3013 	    XHCI_SCTX_0_CTX_NUM_SET(dci) |
   3014 	    XHCI_SCTX_0_SPEED_SET(xhci_speed2xspeed(speed));
   3015 	cp[1] = 0;
   3016 	cp[2] = XHCI_SCTX_2_IRQ_TARGET_SET(0);
   3017 	cp[3] = 0;
   3018 	xhci_setup_route(pipe, cp);
   3019 	xhci_setup_tthub(pipe, cp);
   3020 
   3021 	cp[0] = htole32(cp[0]);
   3022 	cp[1] = htole32(cp[1]);
   3023 	cp[2] = htole32(cp[2]);
   3024 	cp[3] = htole32(cp[3]);
   3025 
   3026 	/* set up input endpoint context */
   3027 	cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(dci));
   3028 	cp[0] =
   3029 	    XHCI_EPCTX_0_EPSTATE_SET(0) |
   3030 	    XHCI_EPCTX_0_MULT_SET(0) |
   3031 	    XHCI_EPCTX_0_MAXP_STREAMS_SET(0) |
   3032 	    XHCI_EPCTX_0_LSA_SET(0) |
   3033 	    XHCI_EPCTX_0_MAX_ESIT_PAYLOAD_HI_SET(0);
   3034 	cp[1] =
   3035 	    XHCI_EPCTX_1_EPTYPE_SET(xhci_ep_get_type(ed)) |
   3036 	    XHCI_EPCTX_1_HID_SET(0) |
   3037 	    XHCI_EPCTX_1_MAXB_SET(0);
   3038 
   3039 	if (xfertype != UE_ISOCHRONOUS)
   3040 		cp[1] |= XHCI_EPCTX_1_CERR_SET(3);
   3041 
   3042 	if (xfertype == UE_CONTROL)
   3043 		cp[4] = XHCI_EPCTX_4_AVG_TRB_LEN_SET(8); /* 6.2.3 */
   3044 	else if (USB_IS_SS(speed))
   3045 		cp[4] = XHCI_EPCTX_4_AVG_TRB_LEN_SET(mps);
   3046 	else
   3047 		cp[4] = XHCI_EPCTX_4_AVG_TRB_LEN_SET(UE_GET_SIZE(mps));
   3048 
   3049 	xhci_setup_maxburst(pipe, cp);
   3050 
   3051 	switch (xfertype) {
   3052 	case UE_CONTROL:
   3053 		break;
   3054 	case UE_BULK:
   3055 		/* XXX Set MaxPStreams, HID, and LSA if streams enabled */
   3056 		break;
   3057 	case UE_INTERRUPT:
   3058 		if (pipe->up_interval != USBD_DEFAULT_INTERVAL)
   3059 			ival = pipe->up_interval;
   3060 
   3061 		ival = xhci_bival2ival(ival, speed);
   3062 		cp[0] |= XHCI_EPCTX_0_IVAL_SET(ival);
   3063 		break;
   3064 	case UE_ISOCHRONOUS:
   3065 		if (pipe->up_interval != USBD_DEFAULT_INTERVAL)
   3066 			ival = pipe->up_interval;
   3067 
   3068 		/* xHCI 6.2.3.6 Table 65, USB 2.0 9.6.6 */
   3069 		if (speed == USB_SPEED_FULL)
   3070 			ival += 3; /* 1ms -> 125us */
   3071 		ival--;
   3072 		cp[0] |= XHCI_EPCTX_0_IVAL_SET(ival);
   3073 		break;
   3074 	default:
   3075 		break;
   3076 	}
   3077 	DPRINTFN(4, "setting ival %ju MaxBurst %#jx",
   3078 	    XHCI_EPCTX_0_IVAL_GET(cp[0]), XHCI_EPCTX_1_MAXB_GET(cp[1]), 0, 0);
   3079 
   3080 	/* rewind TR dequeue pointer in xHC */
   3081 	/* can't use xhci_ep_get_dci() yet? */
   3082 	*(uint64_t *)(&cp[2]) = htole64(
   3083 	    xhci_ring_trbp(&xs->xs_ep[dci].xe_tr, 0) |
   3084 	    XHCI_EPCTX_2_DCS_SET(1));
   3085 
   3086 	cp[0] = htole32(cp[0]);
   3087 	cp[1] = htole32(cp[1]);
   3088 	cp[4] = htole32(cp[4]);
   3089 
   3090 	/* rewind TR dequeue pointer in driver */
   3091 	struct xhci_ring *xr = &xs->xs_ep[dci].xe_tr;
   3092 	mutex_enter(&xr->xr_lock);
   3093 	xhci_host_dequeue(xr);
   3094 	mutex_exit(&xr->xr_lock);
   3095 
   3096 	/* sync input contexts before they are read from memory */
   3097 	usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE);
   3098 }
   3099 
   3100 /*
   3101  * Setup route string and roothub port of given device for slot context
   3102  */
   3103 static void
   3104 xhci_setup_route(struct usbd_pipe *pipe, uint32_t *cp)
   3105 {
   3106 	struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
   3107 	struct usbd_device *dev = pipe->up_dev;
   3108 	struct usbd_port *up = dev->ud_powersrc;
   3109 	struct usbd_device *hub;
   3110 	struct usbd_device *adev;
   3111 	uint8_t rhport = 0;
   3112 	uint32_t route = 0;
   3113 
   3114 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   3115 
   3116 	/* Locate root hub port and Determine route string */
   3117 	/* 4.3.3 route string does not include roothub port */
   3118 	for (hub = dev; hub != NULL; hub = hub->ud_myhub) {
   3119 		uint32_t dep;
   3120 
   3121 		DPRINTFN(4, "hub %#jx depth %jd upport %jp upportno %jd",
   3122 		    (uintptr_t)hub, hub->ud_depth, (uintptr_t)hub->ud_powersrc,
   3123 		    hub->ud_powersrc ? (uintptr_t)hub->ud_powersrc->up_portno :
   3124 			 -1);
   3125 
   3126 		if (hub->ud_powersrc == NULL)
   3127 			break;
   3128 		dep = hub->ud_depth;
   3129 		if (dep == 0)
   3130 			break;
   3131 		rhport = hub->ud_powersrc->up_portno;
   3132 		if (dep > USB_HUB_MAX_DEPTH)
   3133 			continue;
   3134 
   3135 		route |=
   3136 		    (rhport > UHD_SS_NPORTS_MAX ? UHD_SS_NPORTS_MAX : rhport)
   3137 		    << ((dep - 1) * 4);
   3138 	}
   3139 	route = route >> 4;
   3140 	size_t bn = hub == sc->sc_bus.ub_roothub ? 0 : 1;
   3141 
   3142 	/* Locate port on upstream high speed hub */
   3143 	for (adev = dev, hub = up->up_parent;
   3144 	     hub != NULL && hub->ud_speed != USB_SPEED_HIGH;
   3145 	     adev = hub, hub = hub->ud_myhub)
   3146 		;
   3147 	if (hub) {
   3148 		int p;
   3149 		for (p = 0; p < hub->ud_hub->uh_hubdesc.bNbrPorts; p++) {
   3150 			if (hub->ud_hub->uh_ports[p].up_dev == adev) {
   3151 				dev->ud_myhsport = &hub->ud_hub->uh_ports[p];
   3152 				goto found;
   3153 			}
   3154 		}
   3155 		panic("%s: cannot find HS port", __func__);
   3156 	found:
   3157 		DPRINTFN(4, "high speed port %jd", p, 0, 0, 0);
   3158 	} else {
   3159 		dev->ud_myhsport = NULL;
   3160 	}
   3161 
   3162 	const size_t ctlrport = xhci_rhport2ctlrport(sc, bn, rhport);
   3163 
   3164 	DPRINTFN(4, "rhport %ju ctlrport %ju Route %05jx hub %#jx", rhport,
   3165 	    ctlrport, route, (uintptr_t)hub);
   3166 
   3167 	cp[0] |= XHCI_SCTX_0_ROUTE_SET(route);
   3168 	cp[1] |= XHCI_SCTX_1_RH_PORT_SET(ctlrport);
   3169 }
   3170 
   3171 /*
   3172  * Setup whether device is hub, whether device uses MTT, and
   3173  * TT informations if it uses MTT.
   3174  */
   3175 static void
   3176 xhci_setup_tthub(struct usbd_pipe *pipe, uint32_t *cp)
   3177 {
   3178 	struct usbd_device *dev = pipe->up_dev;
   3179 	struct usbd_port *myhsport = dev->ud_myhsport;
   3180 	usb_device_descriptor_t * const dd = &dev->ud_ddesc;
   3181 	uint32_t speed = dev->ud_speed;
   3182 	uint8_t rhaddr = dev->ud_bus->ub_rhaddr;
   3183 	uint8_t tthubslot, ttportnum;
   3184 	bool ishub;
   3185 	bool usemtt;
   3186 
   3187 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   3188 
   3189 	/*
   3190 	 * 6.2.2, Table 57-60, 6.2.2.1, 6.2.2.2
   3191 	 * tthubslot:
   3192 	 *   This is the slot ID of parent HS hub
   3193 	 *   if LS/FS device is connected && connected through HS hub.
   3194 	 *   This is 0 if device is not LS/FS device ||
   3195 	 *   parent hub is not HS hub ||
   3196 	 *   attached to root hub.
   3197 	 * ttportnum:
   3198 	 *   This is the downstream facing port of parent HS hub
   3199 	 *   if LS/FS device is connected.
   3200 	 *   This is 0 if device is not LS/FS device ||
   3201 	 *   parent hub is not HS hub ||
   3202 	 *   attached to root hub.
   3203 	 */
   3204 	if (myhsport &&
   3205 	    myhsport->up_parent->ud_addr != rhaddr &&
   3206 	    (speed == USB_SPEED_LOW || speed == USB_SPEED_FULL)) {
   3207 		ttportnum = myhsport->up_portno;
   3208 		tthubslot = myhsport->up_parent->ud_addr;
   3209 	} else {
   3210 		ttportnum = 0;
   3211 		tthubslot = 0;
   3212 	}
   3213 	DPRINTFN(4, "myhsport %#jx ttportnum=%jd tthubslot=%jd",
   3214 	    (uintptr_t)myhsport, ttportnum, tthubslot, 0);
   3215 
   3216 	/* ishub is valid after reading UDESC_DEVICE */
   3217 	ishub = (dd->bDeviceClass == UDCLASS_HUB);
   3218 
   3219 	/* dev->ud_hub is valid after reading UDESC_HUB */
   3220 	if (ishub && dev->ud_hub) {
   3221 		usb_hub_descriptor_t *hd = &dev->ud_hub->uh_hubdesc;
   3222 		uint8_t ttt =
   3223 		    __SHIFTOUT(UGETW(hd->wHubCharacteristics), UHD_TT_THINK);
   3224 
   3225 		cp[1] |= XHCI_SCTX_1_NUM_PORTS_SET(hd->bNbrPorts);
   3226 		cp[2] |= XHCI_SCTX_2_TT_THINK_TIME_SET(ttt);
   3227 		DPRINTFN(4, "nports=%jd ttt=%jd", hd->bNbrPorts, ttt, 0, 0);
   3228 	}
   3229 
   3230 #define IS_MTTHUB(dd) \
   3231      ((dd)->bDeviceProtocol == UDPROTO_HSHUBMTT)
   3232 
   3233 	/*
   3234 	 * MTT flag is set if
   3235 	 * 1. this is HS hub && MTTs are supported and enabled;  or
   3236 	 * 2. this is LS or FS device && there is a parent HS hub where MTTs
   3237 	 *    are supported and enabled.
   3238 	 *
   3239 	 * XXX enabled is not tested yet
   3240 	 */
   3241 	if (ishub && speed == USB_SPEED_HIGH && IS_MTTHUB(dd))
   3242 		usemtt = true;
   3243 	else if ((speed == USB_SPEED_LOW || speed == USB_SPEED_FULL) &&
   3244 	    myhsport &&
   3245 	    myhsport->up_parent->ud_addr != rhaddr &&
   3246 	    IS_MTTHUB(&myhsport->up_parent->ud_ddesc))
   3247 		usemtt = true;
   3248 	else
   3249 		usemtt = false;
   3250 	DPRINTFN(4, "class %ju proto %ju ishub %jd usemtt %jd",
   3251 	    dd->bDeviceClass, dd->bDeviceProtocol, ishub, usemtt);
   3252 
   3253 #undef IS_MTTHUB
   3254 
   3255 	cp[0] |=
   3256 	    XHCI_SCTX_0_HUB_SET(ishub ? 1 : 0) |
   3257 	    XHCI_SCTX_0_MTT_SET(usemtt ? 1 : 0);
   3258 	cp[2] |=
   3259 	    XHCI_SCTX_2_TT_HUB_SID_SET(tthubslot) |
   3260 	    XHCI_SCTX_2_TT_PORT_NUM_SET(ttportnum);
   3261 }
   3262 
   3263 /* set up params for periodic endpoint */
   3264 static void
   3265 xhci_setup_maxburst(struct usbd_pipe *pipe, uint32_t *cp)
   3266 {
   3267 	struct usbd_device *dev = pipe->up_dev;
   3268 	usb_endpoint_descriptor_t * const ed = pipe->up_endpoint->ue_edesc;
   3269 	const uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
   3270 	usbd_desc_iter_t iter;
   3271 	const usb_cdc_descriptor_t *cdcd;
   3272 	uint32_t maxb = 0;
   3273 	uint16_t mps = UGETW(ed->wMaxPacketSize);
   3274 	uint8_t speed = dev->ud_speed;
   3275 	uint8_t ep;
   3276 
   3277 	/* config desc is NULL when opening ep0 */
   3278 	if (dev == NULL || dev->ud_cdesc == NULL)
   3279 		goto no_cdcd;
   3280 	cdcd = (const usb_cdc_descriptor_t *)usb_find_desc(dev,
   3281 	    UDESC_INTERFACE, USBD_CDCSUBTYPE_ANY);
   3282 	if (cdcd == NULL)
   3283 		goto no_cdcd;
   3284 	usb_desc_iter_init(dev, &iter);
   3285 	iter.cur = (const void *)cdcd;
   3286 
   3287 	/* find endpoint_ss_comp desc for ep of this pipe */
   3288 	for (ep = 0;;) {
   3289 		cdcd = (const usb_cdc_descriptor_t *)usb_desc_iter_next(&iter);
   3290 		if (cdcd == NULL)
   3291 			break;
   3292 		if (ep == 0 && cdcd->bDescriptorType == UDESC_ENDPOINT) {
   3293 			ep = ((const usb_endpoint_descriptor_t *)cdcd)->
   3294 			    bEndpointAddress;
   3295 			if (UE_GET_ADDR(ep) ==
   3296 			    UE_GET_ADDR(ed->bEndpointAddress)) {
   3297 				cdcd = (const usb_cdc_descriptor_t *)
   3298 				    usb_desc_iter_next(&iter);
   3299 				break;
   3300 			}
   3301 			ep = 0;
   3302 		}
   3303 	}
   3304 	if (cdcd != NULL && cdcd->bDescriptorType == UDESC_ENDPOINT_SS_COMP) {
   3305 		const usb_endpoint_ss_comp_descriptor_t * esscd =
   3306 		    (const usb_endpoint_ss_comp_descriptor_t *)cdcd;
   3307 		maxb = esscd->bMaxBurst;
   3308 	}
   3309 
   3310  no_cdcd:
   3311 	/* 6.2.3.4,  4.8.2.4 */
   3312 	if (USB_IS_SS(speed)) {
   3313 		/* USB 3.1  9.6.6 */
   3314 		cp[1] |= XHCI_EPCTX_1_MAXP_SIZE_SET(mps);
   3315 		/* USB 3.1  9.6.7 */
   3316 		cp[1] |= XHCI_EPCTX_1_MAXB_SET(maxb);
   3317 #ifdef notyet
   3318 		if (xfertype == UE_ISOCHRONOUS) {
   3319 		}
   3320 		if (XHCI_HCC2_LEC(sc->sc_hcc2) != 0) {
   3321 			/* use ESIT */
   3322 			cp[4] |= XHCI_EPCTX_4_MAX_ESIT_PAYLOAD_SET(x);
   3323 			cp[0] |= XHCI_EPCTX_0_MAX_ESIT_PAYLOAD_HI_SET(x);
   3324 
   3325 			/* XXX if LEC = 1, set ESIT instead */
   3326 			cp[0] |= XHCI_EPCTX_0_MULT_SET(0);
   3327 		} else {
   3328 			/* use ival */
   3329 		}
   3330 #endif
   3331 	} else {
   3332 		/* USB 2.0  9.6.6 */
   3333 		cp[1] |= XHCI_EPCTX_1_MAXP_SIZE_SET(UE_GET_SIZE(mps));
   3334 
   3335 		/* 6.2.3.4 */
   3336 		if (speed == USB_SPEED_HIGH &&
   3337 		   (xfertype == UE_ISOCHRONOUS || xfertype == UE_INTERRUPT)) {
   3338 			maxb = UE_GET_TRANS(mps);
   3339 		} else {
   3340 			/* LS/FS or HS CTRL or HS BULK */
   3341 			maxb = 0;
   3342 		}
   3343 		cp[1] |= XHCI_EPCTX_1_MAXB_SET(maxb);
   3344 	}
   3345 }
   3346 
   3347 /*
   3348  * Convert endpoint bInterval value to endpoint context interval value
   3349  * for Interrupt pipe.
   3350  * xHCI 6.2.3.6 Table 65, USB 2.0 9.6.6
   3351  */
   3352 static uint32_t
   3353 xhci_bival2ival(uint32_t ival, uint32_t speed)
   3354 {
   3355 	if (speed == USB_SPEED_LOW || speed == USB_SPEED_FULL) {
   3356 		int i;
   3357 
   3358 		/*
   3359 		 * round ival down to "the nearest base 2 multiple of
   3360 		 * bInterval * 8".
   3361 		 * bInterval is at most 255 as its type is uByte.
   3362 		 * 255(ms) = 2040(x 125us) < 2^11, so start with 10.
   3363 		 */
   3364 		for (i = 10; i > 0; i--) {
   3365 			if ((ival * 8) >= (1 << i))
   3366 				break;
   3367 		}
   3368 		ival = i;
   3369 	} else {
   3370 		/* Interval = bInterval-1 for SS/HS */
   3371 		ival--;
   3372 	}
   3373 
   3374 	return ival;
   3375 }
   3376 
   3377 /* ----- */
   3378 
   3379 static void
   3380 xhci_noop(struct usbd_pipe *pipe)
   3381 {
   3382 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   3383 }
   3384 
   3385 /*
   3386  * Process root hub request.
   3387  */
   3388 static int
   3389 xhci_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
   3390     void *buf, int buflen)
   3391 {
   3392 	struct xhci_softc * const sc = XHCI_BUS2SC(bus);
   3393 	usb_port_status_t ps;
   3394 	int l, totlen = 0;
   3395 	uint16_t len, value, index;
   3396 	int port, i;
   3397 	uint32_t v;
   3398 
   3399 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   3400 
   3401 	if (sc->sc_dying)
   3402 		return -1;
   3403 
   3404 	size_t bn = bus == &sc->sc_bus ? 0 : 1;
   3405 
   3406 	len = UGETW(req->wLength);
   3407 	value = UGETW(req->wValue);
   3408 	index = UGETW(req->wIndex);
   3409 
   3410 	DPRINTFN(12, "rhreq: %04jx %04jx %04jx %04jx",
   3411 	    req->bmRequestType | (req->bRequest << 8), value, index, len);
   3412 
   3413 #define C(x,y) ((x) | ((y) << 8))
   3414 	switch (C(req->bRequest, req->bmRequestType)) {
   3415 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
   3416 		DPRINTFN(8, "getdesc: wValue=0x%04jx", value, 0, 0, 0);
   3417 		if (len == 0)
   3418 			break;
   3419 		switch (value) {
   3420 #define sd ((usb_string_descriptor_t *)buf)
   3421 		case C(2, UDESC_STRING):
   3422 			/* Product */
   3423 			totlen = usb_makestrdesc(sd, len, "xHCI root hub");
   3424 			break;
   3425 #undef sd
   3426 		default:
   3427 			/* default from usbroothub */
   3428 			return buflen;
   3429 		}
   3430 		break;
   3431 
   3432 	/* Hub requests */
   3433 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
   3434 		break;
   3435 	/* Clear Port Feature request */
   3436 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER): {
   3437 		const size_t cp = xhci_rhport2ctlrport(sc, bn, index);
   3438 
   3439 		DPRINTFN(4, "UR_CLEAR_PORT_FEAT bp=%jd feat=%jd bus=%jd cp=%jd",
   3440 		    index, value, bn, cp);
   3441 		if (index < 1 || index > sc->sc_rhportcount[bn]) {
   3442 			return -1;
   3443 		}
   3444 		port = XHCI_PORTSC(cp);
   3445 		v = xhci_op_read_4(sc, port);
   3446 		DPRINTFN(4, "portsc=0x%08jx", v, 0, 0, 0);
   3447 		v &= ~XHCI_PS_CLEAR;
   3448 		switch (value) {
   3449 		case UHF_PORT_ENABLE:
   3450 			xhci_op_write_4(sc, port, v & ~XHCI_PS_PED);
   3451 			break;
   3452 		case UHF_PORT_SUSPEND:
   3453 			return -1;
   3454 		case UHF_PORT_POWER:
   3455 			break;
   3456 		case UHF_PORT_TEST:
   3457 		case UHF_PORT_INDICATOR:
   3458 			return -1;
   3459 		case UHF_C_PORT_CONNECTION:
   3460 			xhci_op_write_4(sc, port, v | XHCI_PS_CSC);
   3461 			break;
   3462 		case UHF_C_PORT_ENABLE:
   3463 		case UHF_C_PORT_SUSPEND:
   3464 		case UHF_C_PORT_OVER_CURRENT:
   3465 			return -1;
   3466 		case UHF_C_BH_PORT_RESET:
   3467 			xhci_op_write_4(sc, port, v | XHCI_PS_WRC);
   3468 			break;
   3469 		case UHF_C_PORT_RESET:
   3470 			xhci_op_write_4(sc, port, v | XHCI_PS_PRC);
   3471 			break;
   3472 		case UHF_C_PORT_LINK_STATE:
   3473 			xhci_op_write_4(sc, port, v | XHCI_PS_PLC);
   3474 			break;
   3475 		case UHF_C_PORT_CONFIG_ERROR:
   3476 			xhci_op_write_4(sc, port, v | XHCI_PS_CEC);
   3477 			break;
   3478 		default:
   3479 			return -1;
   3480 		}
   3481 		break;
   3482 	}
   3483 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
   3484 		if (len == 0)
   3485 			break;
   3486 		if ((value & 0xff) != 0) {
   3487 			return -1;
   3488 		}
   3489 		usb_hub_descriptor_t hubd;
   3490 
   3491 		totlen = min(buflen, sizeof(hubd));
   3492 		memcpy(&hubd, buf, totlen);
   3493 		hubd.bNbrPorts = sc->sc_rhportcount[bn];
   3494 		USETW(hubd.wHubCharacteristics, UHD_PWR_NO_SWITCH);
   3495 		hubd.bPwrOn2PwrGood = 200;
   3496 		for (i = 0, l = sc->sc_rhportcount[bn]; l > 0; i++, l -= 8) {
   3497 			/* XXX can't find out? */
   3498 			hubd.DeviceRemovable[i++] = 0;
   3499 		}
   3500 		hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
   3501 		totlen = min(totlen, hubd.bDescLength);
   3502 		memcpy(buf, &hubd, totlen);
   3503 		break;
   3504 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
   3505 		if (len != 4) {
   3506 			return -1;
   3507 		}
   3508 		memset(buf, 0, len); /* ? XXX */
   3509 		totlen = len;
   3510 		break;
   3511 	/* Get Port Status request */
   3512 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER): {
   3513 		const size_t cp = xhci_rhport2ctlrport(sc, bn, index);
   3514 
   3515 		DPRINTFN(8, "get port status bn=%jd i=%jd cp=%ju",
   3516 		    bn, index, cp, 0);
   3517 		if (index < 1 || index > sc->sc_rhportcount[bn]) {
   3518 			return -1;
   3519 		}
   3520 		if (len != 4) {
   3521 			return -1;
   3522 		}
   3523 		v = xhci_op_read_4(sc, XHCI_PORTSC(cp));
   3524 		DPRINTFN(4, "getrhportsc %jd %08jx", cp, v, 0, 0);
   3525 		i = xhci_xspeed2psspeed(XHCI_PS_SPEED_GET(v));
   3526 		if (v & XHCI_PS_CCS)	i |= UPS_CURRENT_CONNECT_STATUS;
   3527 		if (v & XHCI_PS_PED)	i |= UPS_PORT_ENABLED;
   3528 		if (v & XHCI_PS_OCA)	i |= UPS_OVERCURRENT_INDICATOR;
   3529 		//if (v & XHCI_PS_SUSP)	i |= UPS_SUSPEND;
   3530 		if (v & XHCI_PS_PR)	i |= UPS_RESET;
   3531 		if (v & XHCI_PS_PP) {
   3532 			if (i & UPS_OTHER_SPEED)
   3533 					i |= UPS_PORT_POWER_SS;
   3534 			else
   3535 					i |= UPS_PORT_POWER;
   3536 		}
   3537 		if (i & UPS_OTHER_SPEED)
   3538 			i |= UPS_PORT_LS_SET(XHCI_PS_PLS_GET(v));
   3539 		if (sc->sc_vendor_port_status)
   3540 			i = sc->sc_vendor_port_status(sc, v, i);
   3541 		USETW(ps.wPortStatus, i);
   3542 		i = 0;
   3543 		if (v & XHCI_PS_CSC)    i |= UPS_C_CONNECT_STATUS;
   3544 		if (v & XHCI_PS_PEC)    i |= UPS_C_PORT_ENABLED;
   3545 		if (v & XHCI_PS_OCC)    i |= UPS_C_OVERCURRENT_INDICATOR;
   3546 		if (v & XHCI_PS_PRC)	i |= UPS_C_PORT_RESET;
   3547 		if (v & XHCI_PS_WRC)	i |= UPS_C_BH_PORT_RESET;
   3548 		if (v & XHCI_PS_PLC)	i |= UPS_C_PORT_LINK_STATE;
   3549 		if (v & XHCI_PS_CEC)	i |= UPS_C_PORT_CONFIG_ERROR;
   3550 		USETW(ps.wPortChange, i);
   3551 		totlen = min(len, sizeof(ps));
   3552 		memcpy(buf, &ps, totlen);
   3553 		break;
   3554 	}
   3555 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
   3556 		return -1;
   3557 	case C(UR_SET_HUB_DEPTH, UT_WRITE_CLASS_DEVICE):
   3558 		break;
   3559 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
   3560 		break;
   3561 	/* Set Port Feature request */
   3562 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER): {
   3563 		int optval = (index >> 8) & 0xff;
   3564 		index &= 0xff;
   3565 		if (index < 1 || index > sc->sc_rhportcount[bn]) {
   3566 			return -1;
   3567 		}
   3568 
   3569 		const size_t cp = xhci_rhport2ctlrport(sc, bn, index);
   3570 
   3571 		port = XHCI_PORTSC(cp);
   3572 		v = xhci_op_read_4(sc, port);
   3573 		DPRINTFN(4, "index %jd cp %jd portsc=0x%08jx", index, cp, v, 0);
   3574 		v &= ~XHCI_PS_CLEAR;
   3575 		switch (value) {
   3576 		case UHF_PORT_ENABLE:
   3577 			xhci_op_write_4(sc, port, v | XHCI_PS_PED);
   3578 			break;
   3579 		case UHF_PORT_SUSPEND:
   3580 			/* XXX suspend */
   3581 			break;
   3582 		case UHF_PORT_RESET:
   3583 			v &= ~(XHCI_PS_PED | XHCI_PS_PR);
   3584 			xhci_op_write_4(sc, port, v | XHCI_PS_PR);
   3585 			/* Wait for reset to complete. */
   3586 			usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
   3587 			if (sc->sc_dying) {
   3588 				return -1;
   3589 			}
   3590 			v = xhci_op_read_4(sc, port);
   3591 			if (v & XHCI_PS_PR) {
   3592 				xhci_op_write_4(sc, port, v & ~XHCI_PS_PR);
   3593 				usb_delay_ms(&sc->sc_bus, 10);
   3594 				/* XXX */
   3595 			}
   3596 			break;
   3597 		case UHF_PORT_POWER:
   3598 			/* XXX power control */
   3599 			break;
   3600 		/* XXX more */
   3601 		case UHF_C_PORT_RESET:
   3602 			xhci_op_write_4(sc, port, v | XHCI_PS_PRC);
   3603 			break;
   3604 		case UHF_PORT_U1_TIMEOUT:
   3605 			if (XHCI_PS_SPEED_GET(v) < XHCI_PS_SPEED_SS) {
   3606 				return -1;
   3607 			}
   3608 			port = XHCI_PORTPMSC(cp);
   3609 			v = xhci_op_read_4(sc, port);
   3610 			DPRINTFN(4, "index %jd cp %jd portpmsc=0x%08jx",
   3611 			    index, cp, v, 0);
   3612 			v &= ~XHCI_PM3_U1TO_SET(0xff);
   3613 			v |= XHCI_PM3_U1TO_SET(optval);
   3614 			xhci_op_write_4(sc, port, v);
   3615 			break;
   3616 		case UHF_PORT_U2_TIMEOUT:
   3617 			if (XHCI_PS_SPEED_GET(v) < XHCI_PS_SPEED_SS) {
   3618 				return -1;
   3619 			}
   3620 			port = XHCI_PORTPMSC(cp);
   3621 			v = xhci_op_read_4(sc, port);
   3622 			DPRINTFN(4, "index %jd cp %jd portpmsc=0x%08jx",
   3623 			    index, cp, v, 0);
   3624 			v &= ~XHCI_PM3_U2TO_SET(0xff);
   3625 			v |= XHCI_PM3_U2TO_SET(optval);
   3626 			xhci_op_write_4(sc, port, v);
   3627 			break;
   3628 		default:
   3629 			return -1;
   3630 		}
   3631 	}
   3632 		break;
   3633 	case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
   3634 	case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
   3635 	case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
   3636 	case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
   3637 		break;
   3638 	default:
   3639 		/* default from usbroothub */
   3640 		return buflen;
   3641 	}
   3642 
   3643 	return totlen;
   3644 }
   3645 
   3646 /* root hub interrupt */
   3647 
   3648 static usbd_status
   3649 xhci_root_intr_transfer(struct usbd_xfer *xfer)
   3650 {
   3651 	struct xhci_softc * const sc = XHCI_XFER2SC(xfer);
   3652 	usbd_status err;
   3653 
   3654 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   3655 
   3656 	/* Insert last in queue. */
   3657 	mutex_enter(&sc->sc_lock);
   3658 	err = usb_insert_transfer(xfer);
   3659 	mutex_exit(&sc->sc_lock);
   3660 	if (err)
   3661 		return err;
   3662 
   3663 	/* Pipe isn't running, start first */
   3664 	return xhci_root_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
   3665 }
   3666 
   3667 /* Wait for roothub port status/change */
   3668 static usbd_status
   3669 xhci_root_intr_start(struct usbd_xfer *xfer)
   3670 {
   3671 	struct xhci_softc * const sc = XHCI_XFER2SC(xfer);
   3672 	const size_t bn = XHCI_XFER2BUS(xfer) == &sc->sc_bus ? 0 : 1;
   3673 
   3674 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   3675 
   3676 	if (sc->sc_dying)
   3677 		return USBD_IOERROR;
   3678 
   3679 	mutex_enter(&sc->sc_lock);
   3680 	sc->sc_intrxfer[bn] = xfer;
   3681 	mutex_exit(&sc->sc_lock);
   3682 
   3683 	return USBD_IN_PROGRESS;
   3684 }
   3685 
   3686 static void
   3687 xhci_root_intr_abort(struct usbd_xfer *xfer)
   3688 {
   3689 	struct xhci_softc * const sc __diagused = XHCI_XFER2SC(xfer);
   3690 
   3691 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   3692 
   3693 	KASSERT(mutex_owned(&sc->sc_lock));
   3694 	KASSERT(xfer->ux_pipe->up_intrxfer == xfer);
   3695 
   3696 	xfer->ux_status = USBD_CANCELLED;
   3697 	usb_transfer_complete(xfer);
   3698 }
   3699 
   3700 static void
   3701 xhci_root_intr_close(struct usbd_pipe *pipe)
   3702 {
   3703 	struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
   3704 	const struct usbd_xfer *xfer = pipe->up_intrxfer;
   3705 	const size_t bn = XHCI_XFER2BUS(xfer) == &sc->sc_bus ? 0 : 1;
   3706 
   3707 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   3708 
   3709 	KASSERT(mutex_owned(&sc->sc_lock));
   3710 
   3711 	sc->sc_intrxfer[bn] = NULL;
   3712 }
   3713 
   3714 static void
   3715 xhci_root_intr_done(struct usbd_xfer *xfer)
   3716 {
   3717 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   3718 
   3719 }
   3720 
   3721 /* -------------- */
   3722 /* device control */
   3723 
   3724 static usbd_status
   3725 xhci_device_ctrl_transfer(struct usbd_xfer *xfer)
   3726 {
   3727 	struct xhci_softc * const sc = XHCI_XFER2SC(xfer);
   3728 	usbd_status err;
   3729 
   3730 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   3731 
   3732 	/* Insert last in queue. */
   3733 	mutex_enter(&sc->sc_lock);
   3734 	err = usb_insert_transfer(xfer);
   3735 	mutex_exit(&sc->sc_lock);
   3736 	if (err)
   3737 		return err;
   3738 
   3739 	/* Pipe isn't running, start first */
   3740 	return xhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
   3741 }
   3742 
   3743 static usbd_status
   3744 xhci_device_ctrl_start(struct usbd_xfer *xfer)
   3745 {
   3746 	struct xhci_softc * const sc = XHCI_XFER2SC(xfer);
   3747 	struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
   3748 	const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
   3749 	struct xhci_ring * const tr = &xs->xs_ep[dci].xe_tr;
   3750 	struct xhci_xfer * const xx = XHCI_XFER2XXFER(xfer);
   3751 	usb_device_request_t * const req = &xfer->ux_request;
   3752 	const int isread = usbd_xfer_isread(xfer);
   3753 	const uint32_t len = UGETW(req->wLength);
   3754 	usb_dma_t * const dma = &xfer->ux_dmabuf;
   3755 	uint64_t parameter;
   3756 	uint32_t status;
   3757 	uint32_t control;
   3758 	u_int i;
   3759 
   3760 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   3761 	DPRINTFN(12, "req: %04jx %04jx %04jx %04jx",
   3762 	    req->bmRequestType | (req->bRequest << 8), UGETW(req->wValue),
   3763 	    UGETW(req->wIndex), UGETW(req->wLength));
   3764 
   3765 	/* we rely on the bottom bits for extra info */
   3766 	KASSERTMSG(((uintptr_t)xfer & 0x3) == 0x0, "xfer %zx",
   3767 	    (uintptr_t) xfer);
   3768 
   3769 	KASSERT((xfer->ux_rqflags & URQ_REQUEST) != 0);
   3770 
   3771 	i = 0;
   3772 
   3773 	/* setup phase */
   3774 	memcpy(&parameter, req, sizeof(parameter));
   3775 	status = XHCI_TRB_2_IRQ_SET(0) | XHCI_TRB_2_BYTES_SET(sizeof(*req));
   3776 	control = ((len == 0) ? XHCI_TRB_3_TRT_NONE :
   3777 	     (isread ? XHCI_TRB_3_TRT_IN : XHCI_TRB_3_TRT_OUT)) |
   3778 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SETUP_STAGE) |
   3779 	    XHCI_TRB_3_IDT_BIT;
   3780 	xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
   3781 
   3782 	if (len != 0) {
   3783 		/* data phase */
   3784 		parameter = DMAADDR(dma, 0);
   3785 		KASSERTMSG(len <= 0x10000, "len %d", len);
   3786 		status = XHCI_TRB_2_IRQ_SET(0) |
   3787 		    XHCI_TRB_2_TDSZ_SET(1) |
   3788 		    XHCI_TRB_2_BYTES_SET(len);
   3789 		control = (isread ? XHCI_TRB_3_DIR_IN : 0) |
   3790 		    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DATA_STAGE) |
   3791 		    (usbd_xfer_isread(xfer) ? XHCI_TRB_3_ISP_BIT : 0) |
   3792 		    XHCI_TRB_3_IOC_BIT;
   3793 		xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
   3794 	}
   3795 
   3796 	parameter = 0;
   3797 	status = XHCI_TRB_2_IRQ_SET(0);
   3798 	/* the status stage has inverted direction */
   3799 	control = ((isread && (len > 0)) ? 0 : XHCI_TRB_3_DIR_IN) |
   3800 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STATUS_STAGE) |
   3801 	    XHCI_TRB_3_IOC_BIT;
   3802 	xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
   3803 
   3804 	mutex_enter(&tr->xr_lock);
   3805 	xhci_ring_put(sc, tr, xfer, xx->xx_trb, i);
   3806 	mutex_exit(&tr->xr_lock);
   3807 
   3808 	xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci);
   3809 
   3810 	if (xfer->ux_timeout && !xhci_polling_p(sc)) {
   3811 		callout_reset(&xfer->ux_callout, mstohz(xfer->ux_timeout),
   3812 		    xhci_timeout, xfer);
   3813 	}
   3814 
   3815 	return USBD_IN_PROGRESS;
   3816 }
   3817 
   3818 static void
   3819 xhci_device_ctrl_done(struct usbd_xfer *xfer)
   3820 {
   3821 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   3822 	usb_device_request_t *req = &xfer->ux_request;
   3823 	int len = UGETW(req->wLength);
   3824 	int rd = req->bmRequestType & UT_READ;
   3825 
   3826 	if (len)
   3827 		usb_syncmem(&xfer->ux_dmabuf, 0, len,
   3828 		    rd ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   3829 }
   3830 
   3831 static void
   3832 xhci_device_ctrl_abort(struct usbd_xfer *xfer)
   3833 {
   3834 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   3835 
   3836 	xhci_abort_xfer(xfer, USBD_CANCELLED);
   3837 }
   3838 
   3839 static void
   3840 xhci_device_ctrl_close(struct usbd_pipe *pipe)
   3841 {
   3842 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   3843 
   3844 	xhci_close_pipe(pipe);
   3845 }
   3846 
   3847 /* ------------------ */
   3848 /* device isochronous */
   3849 
   3850 /* ----------- */
   3851 /* device bulk */
   3852 
   3853 static usbd_status
   3854 xhci_device_bulk_transfer(struct usbd_xfer *xfer)
   3855 {
   3856 	struct xhci_softc * const sc = XHCI_XFER2SC(xfer);
   3857 	usbd_status err;
   3858 
   3859 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   3860 
   3861 	/* Insert last in queue. */
   3862 	mutex_enter(&sc->sc_lock);
   3863 	err = usb_insert_transfer(xfer);
   3864 	mutex_exit(&sc->sc_lock);
   3865 	if (err)
   3866 		return err;
   3867 
   3868 	/*
   3869 	 * Pipe isn't running (otherwise err would be USBD_INPROG),
   3870 	 * so start it first.
   3871 	 */
   3872 	return xhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
   3873 }
   3874 
   3875 static usbd_status
   3876 xhci_device_bulk_start(struct usbd_xfer *xfer)
   3877 {
   3878 	struct xhci_softc * const sc = XHCI_XFER2SC(xfer);
   3879 	struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
   3880 	const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
   3881 	struct xhci_ring * const tr = &xs->xs_ep[dci].xe_tr;
   3882 	struct xhci_xfer * const xx = XHCI_XFER2XXFER(xfer);
   3883 	const uint32_t len = xfer->ux_length;
   3884 	usb_dma_t * const dma = &xfer->ux_dmabuf;
   3885 	uint64_t parameter;
   3886 	uint32_t status;
   3887 	uint32_t control;
   3888 	u_int i = 0;
   3889 
   3890 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   3891 
   3892 	DPRINTFN(15, "%#jx slot %ju dci %ju", (uintptr_t)xfer, xs->xs_idx, dci,
   3893 	    0);
   3894 
   3895 	if (sc->sc_dying)
   3896 		return USBD_IOERROR;
   3897 
   3898 	KASSERT((xfer->ux_rqflags & URQ_REQUEST) == 0);
   3899 
   3900 	parameter = DMAADDR(dma, 0);
   3901 	/*
   3902 	 * XXX: (dsl) The physical buffer must not cross a 64k boundary.
   3903 	 * If the user supplied buffer crosses such a boundary then 2
   3904 	 * (or more) TRB should be used.
   3905 	 * If multiple TRB are used the td_size field must be set correctly.
   3906 	 * For v1.0 devices (like ivy bridge) this is the number of usb data
   3907 	 * blocks needed to complete the transfer.
   3908 	 * Setting it to 1 in the last TRB causes an extra zero-length
   3909 	 * data block be sent.
   3910 	 * The earlier documentation differs, I don't know how it behaves.
   3911 	 */
   3912 	KASSERTMSG(len <= 0x10000, "len %d", len);
   3913 	status = XHCI_TRB_2_IRQ_SET(0) |
   3914 	    XHCI_TRB_2_TDSZ_SET(1) |
   3915 	    XHCI_TRB_2_BYTES_SET(len);
   3916 	control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL) |
   3917 	    (usbd_xfer_isread(xfer) ? XHCI_TRB_3_ISP_BIT : 0) |
   3918 	    XHCI_TRB_3_IOC_BIT;
   3919 	xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
   3920 
   3921 	mutex_enter(&tr->xr_lock);
   3922 	xhci_ring_put(sc, tr, xfer, xx->xx_trb, i);
   3923 	mutex_exit(&tr->xr_lock);
   3924 
   3925 	xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci);
   3926 
   3927 	if (xfer->ux_timeout && !xhci_polling_p(sc)) {
   3928 		callout_reset(&xfer->ux_callout, mstohz(xfer->ux_timeout),
   3929 		    xhci_timeout, xfer);
   3930 	}
   3931 
   3932 	return USBD_IN_PROGRESS;
   3933 }
   3934 
   3935 static void
   3936 xhci_device_bulk_done(struct usbd_xfer *xfer)
   3937 {
   3938 #ifdef USB_DEBUG
   3939 	struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
   3940 	const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
   3941 #endif
   3942 	const int isread = usbd_xfer_isread(xfer);
   3943 
   3944 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   3945 
   3946 	DPRINTFN(15, "%#jx slot %ju dci %ju", (uintptr_t)xfer, xs->xs_idx, dci,
   3947 	    0);
   3948 
   3949 	usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
   3950 	    isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   3951 }
   3952 
   3953 static void
   3954 xhci_device_bulk_abort(struct usbd_xfer *xfer)
   3955 {
   3956 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   3957 
   3958 	xhci_abort_xfer(xfer, USBD_CANCELLED);
   3959 }
   3960 
   3961 static void
   3962 xhci_device_bulk_close(struct usbd_pipe *pipe)
   3963 {
   3964 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   3965 
   3966 	xhci_close_pipe(pipe);
   3967 }
   3968 
   3969 /* ---------------- */
   3970 /* device interrupt */
   3971 
   3972 static usbd_status
   3973 xhci_device_intr_transfer(struct usbd_xfer *xfer)
   3974 {
   3975 	struct xhci_softc * const sc = XHCI_XFER2SC(xfer);
   3976 	usbd_status err;
   3977 
   3978 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   3979 
   3980 	/* Insert last in queue. */
   3981 	mutex_enter(&sc->sc_lock);
   3982 	err = usb_insert_transfer(xfer);
   3983 	mutex_exit(&sc->sc_lock);
   3984 	if (err)
   3985 		return err;
   3986 
   3987 	/*
   3988 	 * Pipe isn't running (otherwise err would be USBD_INPROG),
   3989 	 * so start it first.
   3990 	 */
   3991 	return xhci_device_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
   3992 }
   3993 
   3994 static usbd_status
   3995 xhci_device_intr_start(struct usbd_xfer *xfer)
   3996 {
   3997 	struct xhci_softc * const sc = XHCI_XFER2SC(xfer);
   3998 	struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
   3999 	const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
   4000 	struct xhci_ring * const tr = &xs->xs_ep[dci].xe_tr;
   4001 	struct xhci_xfer * const xx = XHCI_XFER2XXFER(xfer);
   4002 	const uint32_t len = xfer->ux_length;
   4003 	const bool polling = xhci_polling_p(sc);
   4004 	usb_dma_t * const dma = &xfer->ux_dmabuf;
   4005 	uint64_t parameter;
   4006 	uint32_t status;
   4007 	uint32_t control;
   4008 	u_int i = 0;
   4009 
   4010 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   4011 
   4012 	DPRINTFN(15, "%#jx slot %ju dci %ju", (uintptr_t)xfer, xs->xs_idx, dci,
   4013 	    0);
   4014 
   4015 	if (sc->sc_dying)
   4016 		return USBD_IOERROR;
   4017 
   4018 	KASSERT((xfer->ux_rqflags & URQ_REQUEST) == 0);
   4019 
   4020 	parameter = DMAADDR(dma, 0);
   4021 	KASSERTMSG(len <= 0x10000, "len %d", len);
   4022 	status = XHCI_TRB_2_IRQ_SET(0) |
   4023 	    XHCI_TRB_2_TDSZ_SET(1) |
   4024 	    XHCI_TRB_2_BYTES_SET(len);
   4025 	control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL) |
   4026 	    (usbd_xfer_isread(xfer) ? XHCI_TRB_3_ISP_BIT : 0) |
   4027 	    XHCI_TRB_3_IOC_BIT;
   4028 	xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
   4029 
   4030 	if (!polling)
   4031 		mutex_enter(&tr->xr_lock);
   4032 	xhci_ring_put(sc, tr, xfer, xx->xx_trb, i);
   4033 	if (!polling)
   4034 		mutex_exit(&tr->xr_lock);
   4035 
   4036 	xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci);
   4037 
   4038 	if (xfer->ux_timeout && !polling) {
   4039 		callout_reset(&xfer->ux_callout, mstohz(xfer->ux_timeout),
   4040 		    xhci_timeout, xfer);
   4041 	}
   4042 
   4043 	return USBD_IN_PROGRESS;
   4044 }
   4045 
   4046 static void
   4047 xhci_device_intr_done(struct usbd_xfer *xfer)
   4048 {
   4049 	struct xhci_softc * const sc __diagused = XHCI_XFER2SC(xfer);
   4050 #ifdef USB_DEBUG
   4051 	struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
   4052 	const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
   4053 #endif
   4054 	const int isread = usbd_xfer_isread(xfer);
   4055 
   4056 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   4057 
   4058 	DPRINTFN(15, "%#jx slot %ju dci %ju", (uintptr_t)xfer, xs->xs_idx, dci,
   4059 	    0);
   4060 
   4061 	KASSERT(xhci_polling_p(sc) || mutex_owned(&sc->sc_lock));
   4062 
   4063 	usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
   4064 	    isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   4065 }
   4066 
   4067 static void
   4068 xhci_device_intr_abort(struct usbd_xfer *xfer)
   4069 {
   4070 	struct xhci_softc * const sc __diagused = XHCI_XFER2SC(xfer);
   4071 
   4072 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   4073 
   4074 	KASSERT(mutex_owned(&sc->sc_lock));
   4075 	DPRINTFN(15, "%#jx", (uintptr_t)xfer, 0, 0, 0);
   4076 	KASSERT(xfer->ux_pipe->up_intrxfer == xfer);
   4077 	xhci_abort_xfer(xfer, USBD_CANCELLED);
   4078 }
   4079 
   4080 static void
   4081 xhci_device_intr_close(struct usbd_pipe *pipe)
   4082 {
   4083 	//struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
   4084 
   4085 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   4086 	DPRINTFN(15, "%#jx", (uintptr_t)pipe, 0, 0, 0);
   4087 
   4088 	xhci_close_pipe(pipe);
   4089 }
   4090 
   4091 /* ------------ */
   4092 
   4093 static void
   4094 xhci_timeout(void *addr)
   4095 {
   4096 	struct xhci_xfer * const xx = addr;
   4097 	struct usbd_xfer * const xfer = &xx->xx_xfer;
   4098 	struct xhci_softc * const sc = XHCI_XFER2SC(xfer);
   4099 
   4100 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   4101 
   4102 	if (sc->sc_dying) {
   4103 		return;
   4104 	}
   4105 
   4106 	usb_init_task(&xx->xx_abort_task, xhci_timeout_task, addr,
   4107 	    USB_TASKQ_MPSAFE);
   4108 	usb_add_task(xx->xx_xfer.ux_pipe->up_dev, &xx->xx_abort_task,
   4109 	    USB_TASKQ_HC);
   4110 }
   4111 
   4112 static void
   4113 xhci_timeout_task(void *addr)
   4114 {
   4115 	struct usbd_xfer * const xfer = addr;
   4116 	struct xhci_softc * const sc = XHCI_XFER2SC(xfer);
   4117 
   4118 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   4119 
   4120 	mutex_enter(&sc->sc_lock);
   4121 	xhci_abort_xfer(xfer, USBD_TIMEOUT);
   4122 	mutex_exit(&sc->sc_lock);
   4123 }
   4124