Home | History | Annotate | Line # | Download | only in usb
xhci.c revision 1.28.2.9
      1 /*	$NetBSD: xhci.c,v 1.28.2.9 2014/12/03 22:33:56 skrll 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 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: xhci.c,v 1.28.2.9 2014/12/03 22:33:56 skrll Exp $");
     31 
     32 #include "opt_usb.h"
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/kernel.h>
     37 #include <sys/kmem.h>
     38 #include <sys/device.h>
     39 #include <sys/select.h>
     40 #include <sys/proc.h>
     41 #include <sys/queue.h>
     42 #include <sys/mutex.h>
     43 #include <sys/condvar.h>
     44 #include <sys/bus.h>
     45 #include <sys/cpu.h>
     46 #include <sys/sysctl.h>
     47 
     48 #include <machine/endian.h>
     49 
     50 #include <dev/usb/usb.h>
     51 #include <dev/usb/usbdi.h>
     52 #include <dev/usb/usbdivar.h>
     53 #include <dev/usb/usbhist.h>
     54 #include <dev/usb/usb_mem.h>
     55 #include <dev/usb/usb_quirks.h>
     56 
     57 #include <dev/usb/xhcireg.h>
     58 #include <dev/usb/xhcivar.h>
     59 #include <dev/usb/usbroothub_subr.h>
     60 
     61 
     62 #ifdef USB_DEBUG
     63 #ifndef XHCI_DEBUG
     64 #define xhcidebug 0
     65 #else
     66 static int xhcidebug = 0;
     67 
     68 SYSCTL_SETUP(sysctl_hw_xhci_setup, "sysctl hw.xhci setup")
     69 {
     70 	int err;
     71 	const struct sysctlnode *rnode;
     72 	const struct sysctlnode *cnode;
     73 
     74 	err = sysctl_createv(clog, 0, NULL, &rnode,
     75 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "xhci",
     76 	    SYSCTL_DESCR("xhci global controls"),
     77 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
     78 
     79 	if (err)
     80 		goto fail;
     81 
     82 	/* control debugging printfs */
     83 	err = sysctl_createv(clog, 0, &rnode, &cnode,
     84 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
     85 	    "debug", SYSCTL_DESCR("Enable debugging output"),
     86 	    NULL, 0, &xhcidebug, sizeof(xhcidebug), CTL_CREATE, CTL_EOL);
     87 	if (err)
     88 		goto fail;
     89 
     90 	return;
     91 fail:
     92 	aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
     93 }
     94 
     95 #endif /* XHCI_DEBUG */
     96 #endif /* USB_DEBUG */
     97 
     98 #define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(xhcidebug,N,FMT,A,B,C,D)
     99 #define XHCIHIST_FUNC() USBHIST_FUNC()
    100 #define XHCIHIST_CALLED(name) USBHIST_CALLED(xhcidebug)
    101 
    102 #define XHCI_DCI_SLOT 0
    103 #define XHCI_DCI_EP_CONTROL 1
    104 
    105 #define XHCI_ICI_INPUT_CONTROL 0
    106 
    107 struct xhci_pipe {
    108 	struct usbd_pipe xp_pipe;
    109 };
    110 
    111 #define XHCI_INTR_ENDPT 1
    112 #define XHCI_COMMAND_RING_TRBS 256
    113 #define XHCI_EVENT_RING_TRBS 256
    114 #define XHCI_EVENT_RING_SEGMENTS 1
    115 #define XHCI_TRB_3_ED_BIT XHCI_TRB_3_ISP_BIT
    116 
    117 static usbd_status xhci_open(usbd_pipe_handle);
    118 static int xhci_intr1(struct xhci_softc * const);
    119 static void xhci_softintr(void *);
    120 static void xhci_poll(struct usbd_bus *);
    121 static usbd_xfer_handle xhci_allocx(struct usbd_bus *);
    122 static void xhci_freex(struct usbd_bus *, usbd_xfer_handle);
    123 static void xhci_get_lock(struct usbd_bus *, kmutex_t **);
    124 static usbd_status xhci_new_device(device_t, usbd_bus_handle, int, int, int,
    125     struct usbd_port *);
    126 
    127 static usbd_status xhci_configure_endpoint(usbd_pipe_handle);
    128 static usbd_status xhci_unconfigure_endpoint(usbd_pipe_handle);
    129 static usbd_status xhci_reset_endpoint(usbd_pipe_handle);
    130 //static usbd_status xhci_stop_endpoint(usbd_pipe_handle);
    131 
    132 static usbd_status xhci_set_dequeue(usbd_pipe_handle);
    133 
    134 static usbd_status xhci_do_command(struct xhci_softc * const,
    135     struct xhci_trb * const, int);
    136 static usbd_status xhci_init_slot(struct xhci_softc * const, uint32_t,
    137     int, int, int, int);
    138 static usbd_status xhci_enable_slot(struct xhci_softc * const,
    139     uint8_t * const);
    140 static usbd_status xhci_address_device(struct xhci_softc * const,
    141     uint64_t, uint8_t, bool);
    142 static usbd_status xhci_update_ep0_mps(struct xhci_softc * const,
    143     struct xhci_slot * const, u_int);
    144 static usbd_status xhci_ring_init(struct xhci_softc * const,
    145     struct xhci_ring * const, size_t, size_t);
    146 static void xhci_ring_free(struct xhci_softc * const, struct xhci_ring * const);
    147 
    148 static void xhci_noop(usbd_pipe_handle);
    149 
    150 static usbd_status xhci_root_ctrl_transfer(usbd_xfer_handle);
    151 static usbd_status xhci_root_ctrl_start(usbd_xfer_handle);
    152 static void xhci_root_ctrl_abort(usbd_xfer_handle);
    153 static void xhci_root_ctrl_close(usbd_pipe_handle);
    154 static void xhci_root_ctrl_done(usbd_xfer_handle);
    155 
    156 static usbd_status xhci_root_intr_transfer(usbd_xfer_handle);
    157 static usbd_status xhci_root_intr_start(usbd_xfer_handle);
    158 static void xhci_root_intr_abort(usbd_xfer_handle);
    159 static void xhci_root_intr_close(usbd_pipe_handle);
    160 static void xhci_root_intr_done(usbd_xfer_handle);
    161 
    162 static usbd_status xhci_device_ctrl_transfer(usbd_xfer_handle);
    163 static usbd_status xhci_device_ctrl_start(usbd_xfer_handle);
    164 static void xhci_device_ctrl_abort(usbd_xfer_handle);
    165 static void xhci_device_ctrl_close(usbd_pipe_handle);
    166 static void xhci_device_ctrl_done(usbd_xfer_handle);
    167 
    168 static usbd_status xhci_device_intr_transfer(usbd_xfer_handle);
    169 static usbd_status xhci_device_intr_start(usbd_xfer_handle);
    170 static void xhci_device_intr_abort(usbd_xfer_handle);
    171 static void xhci_device_intr_close(usbd_pipe_handle);
    172 static void xhci_device_intr_done(usbd_xfer_handle);
    173 
    174 static usbd_status xhci_device_bulk_transfer(usbd_xfer_handle);
    175 static usbd_status xhci_device_bulk_start(usbd_xfer_handle);
    176 static void xhci_device_bulk_abort(usbd_xfer_handle);
    177 static void xhci_device_bulk_close(usbd_pipe_handle);
    178 static void xhci_device_bulk_done(usbd_xfer_handle);
    179 
    180 static void xhci_timeout(void *);
    181 static void xhci_timeout_task(void *);
    182 
    183 static const struct usbd_bus_methods xhci_bus_methods = {
    184 	.ubm_open = xhci_open,
    185 	.ubm_softint = xhci_softintr,
    186 	.ubm_dopoll = xhci_poll,
    187 	.ubm_allocx = xhci_allocx,
    188 	.ubm_freex = xhci_freex,
    189 	.ubm_getlock = xhci_get_lock,
    190 	.ubm_newdev = xhci_new_device,
    191 };
    192 
    193 static const struct usbd_pipe_methods xhci_root_ctrl_methods = {
    194 	.upm_transfer = xhci_root_ctrl_transfer,
    195 	.upm_start = xhci_root_ctrl_start,
    196 	.upm_abort = xhci_root_ctrl_abort,
    197 	.upm_close = xhci_root_ctrl_close,
    198 	.upm_cleartoggle = xhci_noop,
    199 	.upm_done = xhci_root_ctrl_done,
    200 };
    201 
    202 static const struct usbd_pipe_methods xhci_root_intr_methods = {
    203 	.upm_transfer = xhci_root_intr_transfer,
    204 	.upm_start = xhci_root_intr_start,
    205 	.upm_abort = xhci_root_intr_abort,
    206 	.upm_close = xhci_root_intr_close,
    207 	.upm_cleartoggle = xhci_noop,
    208 	.upm_done = xhci_root_intr_done,
    209 };
    210 
    211 
    212 static const struct usbd_pipe_methods xhci_device_ctrl_methods = {
    213 	.upm_transfer = xhci_device_ctrl_transfer,
    214 	.upm_start = xhci_device_ctrl_start,
    215 	.upm_abort = xhci_device_ctrl_abort,
    216 	.upm_close = xhci_device_ctrl_close,
    217 	.upm_cleartoggle = xhci_noop,
    218 	.upm_done = xhci_device_ctrl_done,
    219 };
    220 
    221 static const struct usbd_pipe_methods xhci_device_isoc_methods = {
    222 	.upm_cleartoggle = xhci_noop,
    223 };
    224 
    225 static const struct usbd_pipe_methods xhci_device_bulk_methods = {
    226 	.upm_transfer = xhci_device_bulk_transfer,
    227 	.upm_start = xhci_device_bulk_start,
    228 	.upm_abort = xhci_device_bulk_abort,
    229 	.upm_close = xhci_device_bulk_close,
    230 	.upm_cleartoggle = xhci_noop,
    231 	.upm_done = xhci_device_bulk_done,
    232 };
    233 
    234 static const struct usbd_pipe_methods xhci_device_intr_methods = {
    235 	.upm_transfer = xhci_device_intr_transfer,
    236 	.upm_start = xhci_device_intr_start,
    237 	.upm_abort = xhci_device_intr_abort,
    238 	.upm_close = xhci_device_intr_close,
    239 	.upm_cleartoggle = xhci_noop,
    240 	.upm_done = xhci_device_intr_done,
    241 };
    242 
    243 static inline uint32_t
    244 xhci_read_4(const struct xhci_softc * const sc, bus_size_t offset)
    245 {
    246 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh, offset);
    247 }
    248 
    249 #if 0 /* unused */
    250 static inline void
    251 xhci_write_4(const struct xhci_softc * const sc, bus_size_t offset,
    252     uint32_t value)
    253 {
    254 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, offset, value);
    255 }
    256 #endif /* unused */
    257 
    258 static inline uint32_t
    259 xhci_cap_read_4(const struct xhci_softc * const sc, bus_size_t offset)
    260 {
    261 	return bus_space_read_4(sc->sc_iot, sc->sc_cbh, offset);
    262 }
    263 
    264 static inline uint32_t
    265 xhci_op_read_4(const struct xhci_softc * const sc, bus_size_t offset)
    266 {
    267 	return bus_space_read_4(sc->sc_iot, sc->sc_obh, offset);
    268 }
    269 
    270 static inline void
    271 xhci_op_write_4(const struct xhci_softc * const sc, bus_size_t offset,
    272     uint32_t value)
    273 {
    274 	bus_space_write_4(sc->sc_iot, sc->sc_obh, offset, value);
    275 }
    276 
    277 #if 0 /* unused */
    278 static inline uint64_t
    279 xhci_op_read_8(const struct xhci_softc * const sc, bus_size_t offset)
    280 {
    281 	uint64_t value;
    282 
    283 	if (sc->sc_ac64) {
    284 #ifdef XHCI_USE_BUS_SPACE_8
    285 		value = bus_space_read_8(sc->sc_iot, sc->sc_obh, offset);
    286 #else
    287 		value = bus_space_read_4(sc->sc_iot, sc->sc_obh, offset);
    288 		value |= (uint64_t)bus_space_read_4(sc->sc_iot, sc->sc_obh,
    289 		    offset + 4) << 32;
    290 #endif
    291 	} else {
    292 		value = bus_space_read_4(sc->sc_iot, sc->sc_obh, offset);
    293 	}
    294 
    295 	return value;
    296 }
    297 #endif /* unused */
    298 
    299 static inline void
    300 xhci_op_write_8(const struct xhci_softc * const sc, bus_size_t offset,
    301     uint64_t value)
    302 {
    303 	if (sc->sc_ac64) {
    304 #ifdef XHCI_USE_BUS_SPACE_8
    305 		bus_space_write_8(sc->sc_iot, sc->sc_obh, offset, value);
    306 #else
    307 		bus_space_write_4(sc->sc_iot, sc->sc_obh, offset + 0,
    308 		    (value >> 0) & 0xffffffff);
    309 		bus_space_write_4(sc->sc_iot, sc->sc_obh, offset + 4,
    310 		    (value >> 32) & 0xffffffff);
    311 #endif
    312 	} else {
    313 		bus_space_write_4(sc->sc_iot, sc->sc_obh, offset, value);
    314 	}
    315 }
    316 
    317 static inline uint32_t
    318 xhci_rt_read_4(const struct xhci_softc * const sc, bus_size_t offset)
    319 {
    320 	return bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset);
    321 }
    322 
    323 static inline void
    324 xhci_rt_write_4(const struct xhci_softc * const sc, bus_size_t offset,
    325     uint32_t value)
    326 {
    327 	bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset, value);
    328 }
    329 
    330 #if 0 /* unused */
    331 static inline uint64_t
    332 xhci_rt_read_8(const struct xhci_softc * const sc, bus_size_t offset)
    333 {
    334 	uint64_t value;
    335 
    336 	if (sc->sc_ac64) {
    337 #ifdef XHCI_USE_BUS_SPACE_8
    338 		value = bus_space_read_8(sc->sc_iot, sc->sc_rbh, offset);
    339 #else
    340 		value = bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset);
    341 		value |= (uint64_t)bus_space_read_4(sc->sc_iot, sc->sc_rbh,
    342 		    offset + 4) << 32;
    343 #endif
    344 	} else {
    345 		value = bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset);
    346 	}
    347 
    348 	return value;
    349 }
    350 #endif /* unused */
    351 
    352 static inline void
    353 xhci_rt_write_8(const struct xhci_softc * const sc, bus_size_t offset,
    354     uint64_t value)
    355 {
    356 	if (sc->sc_ac64) {
    357 #ifdef XHCI_USE_BUS_SPACE_8
    358 		bus_space_write_8(sc->sc_iot, sc->sc_rbh, offset, value);
    359 #else
    360 		bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset + 0,
    361 		    (value >> 0) & 0xffffffff);
    362 		bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset + 4,
    363 		    (value >> 32) & 0xffffffff);
    364 #endif
    365 	} else {
    366 		bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset, value);
    367 	}
    368 }
    369 
    370 #if 0 /* unused */
    371 static inline uint32_t
    372 xhci_db_read_4(const struct xhci_softc * const sc, bus_size_t offset)
    373 {
    374 	return bus_space_read_4(sc->sc_iot, sc->sc_dbh, offset);
    375 }
    376 #endif /* unused */
    377 
    378 static inline void
    379 xhci_db_write_4(const struct xhci_softc * const sc, bus_size_t offset,
    380     uint32_t value)
    381 {
    382 	bus_space_write_4(sc->sc_iot, sc->sc_dbh, offset, value);
    383 }
    384 
    385 /* --- */
    386 
    387 static inline uint8_t
    388 xhci_ep_get_type(usb_endpoint_descriptor_t * const ed)
    389 {
    390 	u_int eptype;
    391 
    392 	switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
    393 	case UE_CONTROL:
    394 		eptype = 0x0;
    395 		break;
    396 	case UE_ISOCHRONOUS:
    397 		eptype = 0x1;
    398 		break;
    399 	case UE_BULK:
    400 		eptype = 0x2;
    401 		break;
    402 	case UE_INTERRUPT:
    403 		eptype = 0x3;
    404 		break;
    405 	}
    406 
    407 	if ((UE_GET_XFERTYPE(ed->bmAttributes) == UE_CONTROL) ||
    408 	    (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN))
    409 		return eptype | 0x4;
    410 	else
    411 		return eptype;
    412 }
    413 
    414 static u_int
    415 xhci_ep_get_dci(usb_endpoint_descriptor_t * const ed)
    416 {
    417 	/* xHCI 1.0 section 4.5.1 */
    418 	u_int epaddr = UE_GET_ADDR(ed->bEndpointAddress);
    419 	u_int in = 0;
    420 
    421 	if ((UE_GET_XFERTYPE(ed->bmAttributes) == UE_CONTROL) ||
    422 	    (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN))
    423 		in = 1;
    424 
    425 	return epaddr * 2 + in;
    426 }
    427 
    428 static inline u_int
    429 xhci_dci_to_ici(const u_int i)
    430 {
    431 	return i + 1;
    432 }
    433 
    434 static inline void *
    435 xhci_slot_get_dcv(struct xhci_softc * const sc, struct xhci_slot * const xs,
    436     const u_int dci)
    437 {
    438 	return KERNADDR(&xs->xs_dc_dma, sc->sc_ctxsz * dci);
    439 }
    440 
    441 #if 0 /* unused */
    442 static inline bus_addr_t
    443 xhci_slot_get_dcp(struct xhci_softc * const sc, struct xhci_slot * const xs,
    444     const u_int dci)
    445 {
    446 	return DMAADDR(&xs->xs_dc_dma, sc->sc_ctxsz * dci);
    447 }
    448 #endif /* unused */
    449 
    450 static inline void *
    451 xhci_slot_get_icv(struct xhci_softc * const sc, struct xhci_slot * const xs,
    452     const u_int ici)
    453 {
    454 	return KERNADDR(&xs->xs_ic_dma, sc->sc_ctxsz * ici);
    455 }
    456 
    457 static inline bus_addr_t
    458 xhci_slot_get_icp(struct xhci_softc * const sc, struct xhci_slot * const xs,
    459     const u_int ici)
    460 {
    461 	return DMAADDR(&xs->xs_ic_dma, sc->sc_ctxsz * ici);
    462 }
    463 
    464 static inline struct xhci_trb *
    465 xhci_ring_trbv(struct xhci_ring * const xr, u_int idx)
    466 {
    467 	return KERNADDR(&xr->xr_dma, XHCI_TRB_SIZE * idx);
    468 }
    469 
    470 static inline bus_addr_t
    471 xhci_ring_trbp(struct xhci_ring * const xr, u_int idx)
    472 {
    473 	return DMAADDR(&xr->xr_dma, XHCI_TRB_SIZE * idx);
    474 }
    475 
    476 static inline void
    477 xhci_trb_put(struct xhci_trb * const trb, uint64_t parameter, uint32_t status,
    478     uint32_t control)
    479 {
    480 	trb->trb_0 = parameter;
    481 	trb->trb_2 = status;
    482 	trb->trb_3 = control;
    483 }
    484 
    485 /* --- */
    486 
    487 void
    488 xhci_childdet(device_t self, device_t child)
    489 {
    490 	struct xhci_softc * const sc = device_private(self);
    491 
    492 	KASSERT(sc->sc_child == child);
    493 	if (child == sc->sc_child)
    494 		sc->sc_child = NULL;
    495 }
    496 
    497 int
    498 xhci_detach(struct xhci_softc *sc, int flags)
    499 {
    500 	int rv = 0;
    501 
    502 	if (sc->sc_child != NULL)
    503 		rv = config_detach(sc->sc_child, flags);
    504 
    505 	if (rv != 0)
    506 		return (rv);
    507 
    508 	/* XXX unconfigure/free slots */
    509 
    510 	/* verify: */
    511 	xhci_rt_write_4(sc, XHCI_IMAN(0), 0);
    512 	xhci_op_write_4(sc, XHCI_USBCMD, 0);
    513 	/* do we need to wait for stop? */
    514 
    515 	xhci_op_write_8(sc, XHCI_CRCR, 0);
    516 	xhci_ring_free(sc, &sc->sc_cr);
    517 	cv_destroy(&sc->sc_command_cv);
    518 
    519 	xhci_rt_write_4(sc, XHCI_ERSTSZ(0), 0);
    520 	xhci_rt_write_8(sc, XHCI_ERSTBA(0), 0);
    521 	xhci_rt_write_8(sc, XHCI_ERDP(0), 0|XHCI_ERDP_LO_BUSY);
    522 	xhci_ring_free(sc, &sc->sc_er);
    523 
    524 	usb_freemem(&sc->sc_bus, &sc->sc_eventst_dma);
    525 
    526 	xhci_op_write_8(sc, XHCI_DCBAAP, 0);
    527 	usb_freemem(&sc->sc_bus, &sc->sc_dcbaa_dma);
    528 
    529 	kmem_free(sc->sc_slots, sizeof(*sc->sc_slots) * sc->sc_maxslots);
    530 
    531 	mutex_destroy(&sc->sc_lock);
    532 	mutex_destroy(&sc->sc_intr_lock);
    533 
    534 	pool_cache_destroy(sc->sc_xferpool);
    535 
    536 	return rv;
    537 }
    538 
    539 int
    540 xhci_activate(device_t self, enum devact act)
    541 {
    542 	struct xhci_softc * const sc = device_private(self);
    543 
    544 	switch (act) {
    545 	case DVACT_DEACTIVATE:
    546 		sc->sc_dying = true;
    547 		return 0;
    548 	default:
    549 		return EOPNOTSUPP;
    550 	}
    551 }
    552 
    553 bool
    554 xhci_suspend(device_t dv, const pmf_qual_t *qual)
    555 {
    556 	return false;
    557 }
    558 
    559 bool
    560 xhci_resume(device_t dv, const pmf_qual_t *qual)
    561 {
    562 	return false;
    563 }
    564 
    565 bool
    566 xhci_shutdown(device_t self, int flags)
    567 {
    568 	return false;
    569 }
    570 
    571 
    572 static void
    573 hexdump(const char *msg, const void *base, size_t len)
    574 {
    575 #if 0
    576 	size_t cnt;
    577 	const uint32_t *p;
    578 	extern paddr_t vtophys(vaddr_t);
    579 
    580 	p = base;
    581 	cnt = 0;
    582 
    583 	printf("*** %s (%zu bytes @ %p %p)\n", msg, len, base,
    584 	    (void *)vtophys((vaddr_t)base));
    585 
    586 	while (cnt < len) {
    587 		if (cnt % 16 == 0)
    588 			printf("%p: ", p);
    589 		else if (cnt % 8 == 0)
    590 			printf(" |");
    591 		printf(" %08x", *p++);
    592 		cnt += 4;
    593 		if (cnt % 16 == 0)
    594 			printf("\n");
    595 	}
    596 #endif
    597 }
    598 
    599 
    600 int
    601 xhci_init(struct xhci_softc *sc)
    602 {
    603 	bus_size_t bsz;
    604 	uint32_t cap, hcs1, hcs2, hcc, dboff, rtsoff;
    605 	uint32_t ecp, ecr;
    606 	uint32_t usbcmd, usbsts, pagesize, config;
    607 	int i;
    608 	uint16_t hciversion;
    609 	uint8_t caplength;
    610 
    611 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
    612 
    613 	/* XXX Low/Full/High speeds for now */
    614 	sc->sc_bus.ub_revision = USBREV_2_0;
    615 	sc->sc_bus.ub_usedma = true;
    616 
    617 	cap = xhci_read_4(sc, XHCI_CAPLENGTH);
    618 	caplength = XHCI_CAP_CAPLENGTH(cap);
    619 	hciversion = XHCI_CAP_HCIVERSION(cap);
    620 
    621 	if ((hciversion < 0x0096) || (hciversion > 0x0100)) {
    622 		aprint_normal_dev(sc->sc_dev,
    623 		    "xHCI version %x.%x not known to be supported\n",
    624 		    (hciversion >> 8) & 0xff, (hciversion >> 0) & 0xff);
    625 	} else {
    626 		aprint_verbose_dev(sc->sc_dev, "xHCI version %x.%x\n",
    627 		    (hciversion >> 8) & 0xff, (hciversion >> 0) & 0xff);
    628 	}
    629 
    630 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0, caplength,
    631 	    &sc->sc_cbh) != 0) {
    632 		aprint_error_dev(sc->sc_dev, "capability subregion failure\n");
    633 		return ENOMEM;
    634 	}
    635 
    636 	hcs1 = xhci_cap_read_4(sc, XHCI_HCSPARAMS1);
    637 	sc->sc_maxslots = XHCI_HCS1_MAXSLOTS(hcs1);
    638 	sc->sc_maxintrs = XHCI_HCS1_MAXINTRS(hcs1);
    639 	sc->sc_maxports = XHCI_HCS1_MAXPORTS(hcs1);
    640 	hcs2 = xhci_cap_read_4(sc, XHCI_HCSPARAMS2);
    641 	(void)xhci_cap_read_4(sc, XHCI_HCSPARAMS3);
    642 	hcc = xhci_cap_read_4(sc, XHCI_HCCPARAMS);
    643 
    644 	sc->sc_ac64 = XHCI_HCC_AC64(hcc);
    645 	sc->sc_ctxsz = XHCI_HCC_CSZ(hcc) ? 64 : 32;
    646 	aprint_debug_dev(sc->sc_dev, "ac64 %d ctxsz %d\n", sc->sc_ac64,
    647 	    sc->sc_ctxsz);
    648 
    649 	aprint_debug_dev(sc->sc_dev, "xECP %x\n", XHCI_HCC_XECP(hcc) * 4);
    650 	ecp = XHCI_HCC_XECP(hcc) * 4;
    651 	while (ecp != 0) {
    652 		ecr = xhci_read_4(sc, ecp);
    653 		aprint_debug_dev(sc->sc_dev, "ECR %x: %08x\n", ecp, ecr);
    654 		switch (XHCI_XECP_ID(ecr)) {
    655 		case XHCI_ID_PROTOCOLS: {
    656 			uint32_t w0, w4, w8;
    657 			uint16_t w2;
    658 			w0 = xhci_read_4(sc, ecp + 0);
    659 			w2 = (w0 >> 16) & 0xffff;
    660 			w4 = xhci_read_4(sc, ecp + 4);
    661 			w8 = xhci_read_4(sc, ecp + 8);
    662 			aprint_debug_dev(sc->sc_dev, "SP: %08x %08x %08x\n",
    663 			    w0, w4, w8);
    664 			if (w4 == 0x20425355 && w2 == 0x0300) {
    665 				sc->sc_ss_port_start = (w8 >> 0) & 0xff;;
    666 				sc->sc_ss_port_count = (w8 >> 8) & 0xff;;
    667 			}
    668 			if (w4 == 0x20425355 && w2 == 0x0200) {
    669 				sc->sc_hs_port_start = (w8 >> 0) & 0xff;
    670 				sc->sc_hs_port_count = (w8 >> 8) & 0xff;
    671 			}
    672 			break;
    673 		}
    674 		default:
    675 			break;
    676 		}
    677 		ecr = xhci_read_4(sc, ecp);
    678 		if (XHCI_XECP_NEXT(ecr) == 0) {
    679 			ecp = 0;
    680 		} else {
    681 			ecp += XHCI_XECP_NEXT(ecr) * 4;
    682 		}
    683 	}
    684 
    685 	bsz = XHCI_PORTSC(sc->sc_maxports + 1);
    686 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, caplength, bsz,
    687 	    &sc->sc_obh) != 0) {
    688 		aprint_error_dev(sc->sc_dev, "operational subregion failure\n");
    689 		return ENOMEM;
    690 	}
    691 
    692 	dboff = xhci_cap_read_4(sc, XHCI_DBOFF);
    693 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, dboff,
    694 	    sc->sc_maxslots * 4, &sc->sc_dbh) != 0) {
    695 		aprint_error_dev(sc->sc_dev, "doorbell subregion failure\n");
    696 		return ENOMEM;
    697 	}
    698 
    699 	rtsoff = xhci_cap_read_4(sc, XHCI_RTSOFF);
    700 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, rtsoff,
    701 	    sc->sc_maxintrs * 0x20, &sc->sc_rbh) != 0) {
    702 		aprint_error_dev(sc->sc_dev, "runtime subregion failure\n");
    703 		return ENOMEM;
    704 	}
    705 
    706 	for (i = 0; i < 100; i++) {
    707 		usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
    708 		if ((usbsts & XHCI_STS_CNR) == 0)
    709 			break;
    710 		usb_delay_ms(&sc->sc_bus, 1);
    711 	}
    712 	if (i >= 100)
    713 		return EIO;
    714 
    715 	usbcmd = 0;
    716 	xhci_op_write_4(sc, XHCI_USBCMD, usbcmd);
    717 	usb_delay_ms(&sc->sc_bus, 1);
    718 
    719 	usbcmd = XHCI_CMD_HCRST;
    720 	xhci_op_write_4(sc, XHCI_USBCMD, usbcmd);
    721 	for (i = 0; i < 100; i++) {
    722 		usbcmd = xhci_op_read_4(sc, XHCI_USBCMD);
    723 		if ((usbcmd & XHCI_CMD_HCRST) == 0)
    724 			break;
    725 		usb_delay_ms(&sc->sc_bus, 1);
    726 	}
    727 	if (i >= 100)
    728 		return EIO;
    729 
    730 	for (i = 0; i < 100; i++) {
    731 		usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
    732 		if ((usbsts & XHCI_STS_CNR) == 0)
    733 			break;
    734 		usb_delay_ms(&sc->sc_bus, 1);
    735 	}
    736 	if (i >= 100)
    737 		return EIO;
    738 
    739 	pagesize = xhci_op_read_4(sc, XHCI_PAGESIZE);
    740 	aprint_debug_dev(sc->sc_dev, "PAGESIZE 0x%08x\n", pagesize);
    741 	pagesize = ffs(pagesize);
    742 	if (pagesize == 0)
    743 		return EIO;
    744 	sc->sc_pgsz = 1 << (12 + (pagesize - 1));
    745 	aprint_debug_dev(sc->sc_dev, "sc_pgsz 0x%08x\n", (uint32_t)sc->sc_pgsz);
    746 	aprint_debug_dev(sc->sc_dev, "sc_maxslots 0x%08x\n",
    747 	    (uint32_t)sc->sc_maxslots);
    748 
    749 	usbd_status err;
    750 
    751 	sc->sc_maxspbuf = XHCI_HCS2_MAXSPBUF(hcs2);
    752 	aprint_debug_dev(sc->sc_dev, "sc_maxspbuf %d\n", sc->sc_maxspbuf);
    753 	if (sc->sc_maxspbuf != 0) {
    754 		err = usb_allocmem(&sc->sc_bus,
    755 		    sizeof(uint64_t) * sc->sc_maxspbuf, sizeof(uint64_t),
    756 		    &sc->sc_spbufarray_dma);
    757 		if (err)
    758 			return err;
    759 
    760 		sc->sc_spbuf_dma = kmem_zalloc(sizeof(*sc->sc_spbuf_dma) * sc->sc_maxspbuf, KM_SLEEP);
    761 		uint64_t *spbufarray = KERNADDR(&sc->sc_spbufarray_dma, 0);
    762 		for (i = 0; i < sc->sc_maxspbuf; i++) {
    763 			usb_dma_t * const dma = &sc->sc_spbuf_dma[i];
    764 			/* allocate contexts */
    765 			err = usb_allocmem(&sc->sc_bus, sc->sc_pgsz,
    766 			    sc->sc_pgsz, dma);
    767 			if (err)
    768 				return err;
    769 			spbufarray[i] = htole64(DMAADDR(dma, 0));
    770 			usb_syncmem(dma, 0, sc->sc_pgsz,
    771 			    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    772 		}
    773 
    774 		usb_syncmem(&sc->sc_spbufarray_dma, 0,
    775 		    sizeof(uint64_t) * sc->sc_maxspbuf, BUS_DMASYNC_PREWRITE);
    776 	}
    777 
    778 	config = xhci_op_read_4(sc, XHCI_CONFIG);
    779 	config &= ~0xFF;
    780 	config |= sc->sc_maxslots & 0xFF;
    781 	xhci_op_write_4(sc, XHCI_CONFIG, config);
    782 
    783 	err = xhci_ring_init(sc, &sc->sc_cr, XHCI_COMMAND_RING_TRBS,
    784 	    XHCI_COMMAND_RING_SEGMENTS_ALIGN);
    785 	if (err) {
    786 		aprint_error_dev(sc->sc_dev, "command ring init fail\n");
    787 		return err;
    788 	}
    789 
    790 	err = xhci_ring_init(sc, &sc->sc_er, XHCI_EVENT_RING_TRBS,
    791 	    XHCI_EVENT_RING_SEGMENTS_ALIGN);
    792 	if (err) {
    793 		aprint_error_dev(sc->sc_dev, "event ring init fail\n");
    794 		return err;
    795 	}
    796 
    797 	usb_dma_t *dma;
    798 	size_t size;
    799 	size_t align;
    800 
    801 	dma = &sc->sc_eventst_dma;
    802 	size = roundup2(XHCI_EVENT_RING_SEGMENTS * XHCI_ERSTE_SIZE,
    803 	    XHCI_EVENT_RING_SEGMENT_TABLE_ALIGN);
    804 	KASSERT(size <= (512 * 1024));
    805 	align = XHCI_EVENT_RING_SEGMENT_TABLE_ALIGN;
    806 	err = usb_allocmem(&sc->sc_bus, size, align, dma);
    807 
    808 	memset(KERNADDR(dma, 0), 0, size);
    809 	usb_syncmem(dma, 0, size, BUS_DMASYNC_PREWRITE);
    810 	aprint_debug_dev(sc->sc_dev, "eventst: %s %016jx %p %zx\n",
    811 	    usbd_errstr(err),
    812 	    (uintmax_t)DMAADDR(&sc->sc_eventst_dma, 0),
    813 	    KERNADDR(&sc->sc_eventst_dma, 0),
    814 	    sc->sc_eventst_dma.udma_block->size);
    815 
    816 	dma = &sc->sc_dcbaa_dma;
    817 	size = (1 + sc->sc_maxslots) * sizeof(uint64_t);
    818 	KASSERT(size <= 2048);
    819 	align = XHCI_DEVICE_CONTEXT_BASE_ADDRESS_ARRAY_ALIGN;
    820 	err = usb_allocmem(&sc->sc_bus, size, align, dma);
    821 
    822 	memset(KERNADDR(dma, 0), 0, size);
    823 	if (sc->sc_maxspbuf != 0) {
    824 		/*
    825 		 * DCBA entry 0 hold the scratchbuf array pointer.
    826 		 */
    827 		*(uint64_t *)KERNADDR(dma, 0) =
    828 		    htole64(DMAADDR(&sc->sc_spbufarray_dma, 0));
    829 	}
    830 	usb_syncmem(dma, 0, size, BUS_DMASYNC_PREWRITE);
    831 	aprint_debug_dev(sc->sc_dev, "dcbaa: %s %016jx %p %zx\n",
    832 	    usbd_errstr(err),
    833 	    (uintmax_t)DMAADDR(&sc->sc_dcbaa_dma, 0),
    834 	    KERNADDR(&sc->sc_dcbaa_dma, 0),
    835 	    sc->sc_dcbaa_dma.udma_block->size);
    836 
    837 	sc->sc_slots = kmem_zalloc(sizeof(*sc->sc_slots) * sc->sc_maxslots,
    838 	    KM_SLEEP);
    839 
    840 	cv_init(&sc->sc_command_cv, "xhcicmd");
    841 
    842 	struct xhci_erste *erst;
    843 	erst = KERNADDR(&sc->sc_eventst_dma, 0);
    844 	erst[0].erste_0 = htole64(xhci_ring_trbp(&sc->sc_er, 0));
    845 	erst[0].erste_2 = htole32(XHCI_EVENT_RING_TRBS);
    846 	erst[0].erste_3 = htole32(0);
    847 	usb_syncmem(&sc->sc_eventst_dma, 0,
    848 	    XHCI_ERSTE_SIZE * XHCI_EVENT_RING_SEGMENTS, BUS_DMASYNC_PREWRITE);
    849 
    850 	xhci_rt_write_4(sc, XHCI_ERSTSZ(0), XHCI_EVENT_RING_SEGMENTS);
    851 	xhci_rt_write_8(sc, XHCI_ERSTBA(0), DMAADDR(&sc->sc_eventst_dma, 0));
    852 	xhci_rt_write_8(sc, XHCI_ERDP(0), xhci_ring_trbp(&sc->sc_er, 0) |
    853 	    XHCI_ERDP_LO_BUSY);
    854 	xhci_op_write_8(sc, XHCI_DCBAAP, DMAADDR(&sc->sc_dcbaa_dma, 0));
    855 	xhci_op_write_8(sc, XHCI_CRCR, xhci_ring_trbp(&sc->sc_cr, 0) |
    856 	    sc->sc_cr.xr_cs);
    857 
    858 #if 0
    859 	hexdump("eventst", KERNADDR(&sc->sc_eventst_dma, 0),
    860 	    XHCI_ERSTE_SIZE * XHCI_EVENT_RING_SEGMENTS);
    861 #endif
    862 
    863 	xhci_rt_write_4(sc, XHCI_IMAN(0), XHCI_IMAN_INTR_ENA);
    864 	xhci_rt_write_4(sc, XHCI_IMOD(0), 0);
    865 
    866 	xhci_op_write_4(sc, XHCI_USBCMD, XHCI_CMD_INTE|XHCI_CMD_RS); /* Go! */
    867 	aprint_debug_dev(sc->sc_dev, "USBCMD %08"PRIx32"\n",
    868 	    xhci_op_read_4(sc, XHCI_USBCMD));
    869 
    870 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
    871 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED);
    872 	cv_init(&sc->sc_softwake_cv, "xhciab");
    873 
    874 	sc->sc_xferpool = pool_cache_init(sizeof(struct xhci_xfer), 0, 0, 0,
    875 	    "xhcixfer", NULL, IPL_USB, NULL, NULL, NULL);
    876 
    877 	/* Set up the bus struct. */
    878 	sc->sc_bus.ub_methods = &xhci_bus_methods;
    879 	sc->sc_bus.ub_pipesize = sizeof(struct xhci_pipe);
    880 
    881 	return USBD_NORMAL_COMPLETION;
    882 }
    883 
    884 int
    885 xhci_intr(void *v)
    886 {
    887 	struct xhci_softc * const sc = v;
    888 	int ret = 0;
    889 
    890 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
    891 
    892 	if (sc == NULL)
    893 		return 0;
    894 
    895 	mutex_spin_enter(&sc->sc_intr_lock);
    896 
    897 	if (sc->sc_dying || !device_has_power(sc->sc_dev))
    898 		goto done;
    899 
    900 	/* If we get an interrupt while polling, then just ignore it. */
    901 	if (sc->sc_bus.ub_usepolling) {
    902 #ifdef DIAGNOSTIC
    903 		DPRINTFN(16, "ignored interrupt while polling", 0, 0, 0, 0);
    904 #endif
    905 		goto done;
    906 	}
    907 
    908 	ret = xhci_intr1(sc);
    909 done:
    910 	mutex_spin_exit(&sc->sc_intr_lock);
    911 	return ret;
    912 }
    913 
    914 int
    915 xhci_intr1(struct xhci_softc * const sc)
    916 {
    917 	uint32_t usbsts;
    918 	uint32_t iman;
    919 
    920 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
    921 
    922 	usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
    923 	DPRINTFN(16, "USBSTS %08x", usbsts, 0, 0, 0);
    924 #if 0
    925 	if ((usbsts & (XHCI_STS_EINT|XHCI_STS_PCD)) == 0) {
    926 		return 0;
    927 	}
    928 #endif
    929 	xhci_op_write_4(sc, XHCI_USBSTS,
    930 	    usbsts & (2|XHCI_STS_EINT|XHCI_STS_PCD)); /* XXX */
    931 	usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
    932 	DPRINTFN(16, "USBSTS %08x", usbsts, 0, 0, 0);
    933 
    934 	iman = xhci_rt_read_4(sc, XHCI_IMAN(0));
    935 	DPRINTFN(16, "IMAN0 %08x", iman, 0, 0, 0);
    936 	if ((iman & XHCI_IMAN_INTR_PEND) == 0) {
    937 		return 0;
    938 	}
    939 	xhci_rt_write_4(sc, XHCI_IMAN(0), iman);
    940 	iman = xhci_rt_read_4(sc, XHCI_IMAN(0));
    941 	DPRINTFN(16, "IMAN0 %08x", iman, 0, 0, 0);
    942 	usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
    943 	DPRINTFN(16, "USBSTS %08x", usbsts, 0, 0, 0);
    944 
    945 	usb_schedsoftintr(&sc->sc_bus);
    946 
    947 	return 1;
    948 }
    949 
    950 static usbd_status
    951 xhci_configure_endpoint(usbd_pipe_handle pipe)
    952 {
    953 	struct xhci_softc * const sc = pipe->up_dev->ud_bus->ub_hcpriv;
    954 	struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
    955 	const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
    956 	usb_endpoint_descriptor_t * const ed = pipe->up_endpoint->ue_edesc;
    957 	const uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
    958 	struct xhci_trb trb;
    959 	usbd_status err;
    960 	uint32_t *cp;
    961 
    962 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
    963 	DPRINTFN(4, "dci %u epaddr 0x%02x attr 0x%02x",
    964 	    dci, ed->bEndpointAddress, ed->bmAttributes, 0);
    965 
    966 	/* XXX ensure input context is available? */
    967 
    968 	memset(xhci_slot_get_icv(sc, xs, 0), 0, sc->sc_pgsz);
    969 
    970 	cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL);
    971 	cp[0] = htole32(0);
    972 	cp[1] = htole32(XHCI_INCTX_1_ADD_MASK(dci));
    973 
    974 	/* set up input slot context */
    975 	cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_SLOT));
    976 	cp[0] = htole32(XHCI_SCTX_0_CTX_NUM_SET(dci));
    977 	cp[1] = htole32(0);
    978 	cp[2] = htole32(0);
    979 	cp[3] = htole32(0);
    980 
    981 	cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(dci));
    982 	if (xfertype == UE_INTERRUPT) {
    983 	cp[0] = htole32(
    984 	    XHCI_EPCTX_0_IVAL_SET(3) /* XXX */
    985 	    );
    986 	cp[1] = htole32(
    987 	    XHCI_EPCTX_1_CERR_SET(3) |
    988 	    XHCI_EPCTX_1_EPTYPE_SET(xhci_ep_get_type(pipe->up_endpoint->ue_edesc)) |
    989 	    XHCI_EPCTX_1_MAXB_SET(0) |
    990 	    XHCI_EPCTX_1_MAXP_SIZE_SET(8) /* XXX */
    991 	    );
    992 	cp[4] = htole32(
    993 		XHCI_EPCTX_4_AVG_TRB_LEN_SET(8)
    994 		);
    995 	} else {
    996 	cp[0] = htole32(0);
    997 	cp[1] = htole32(
    998 	    XHCI_EPCTX_1_CERR_SET(3) |
    999 	    XHCI_EPCTX_1_EPTYPE_SET(xhci_ep_get_type(pipe->up_endpoint->ue_edesc)) |
   1000 	    XHCI_EPCTX_1_MAXB_SET(0) |
   1001 	    XHCI_EPCTX_1_MAXP_SIZE_SET(512) /* XXX */
   1002 	    );
   1003 	}
   1004 	*(uint64_t *)(&cp[2]) = htole64(
   1005 	    xhci_ring_trbp(&xs->xs_ep[dci].xe_tr, 0) |
   1006 	    XHCI_EPCTX_2_DCS_SET(1));
   1007 
   1008 	/* sync input contexts before they are read from memory */
   1009 	usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE);
   1010 	hexdump("input control context", xhci_slot_get_icv(sc, xs, 0),
   1011 	    sc->sc_ctxsz * 1);
   1012 	hexdump("input endpoint context", xhci_slot_get_icv(sc, xs,
   1013 	    xhci_dci_to_ici(dci)), sc->sc_ctxsz * 1);
   1014 
   1015 	trb.trb_0 = xhci_slot_get_icp(sc, xs, 0);
   1016 	trb.trb_2 = 0;
   1017 	trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
   1018 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_CONFIGURE_EP);
   1019 
   1020 	err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
   1021 
   1022 	usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
   1023 	hexdump("output context", xhci_slot_get_dcv(sc, xs, dci),
   1024 	    sc->sc_ctxsz * 1);
   1025 
   1026 	return err;
   1027 }
   1028 
   1029 static usbd_status
   1030 xhci_unconfigure_endpoint(usbd_pipe_handle pipe)
   1031 {
   1032 #ifdef USB_DEBUG
   1033 	struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
   1034 #endif
   1035 
   1036 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1037 	DPRINTFN(4, "slot %u", xs->xs_idx, 0, 0, 0);
   1038 
   1039 	return USBD_NORMAL_COMPLETION;
   1040 }
   1041 
   1042 static usbd_status
   1043 xhci_reset_endpoint(usbd_pipe_handle pipe)
   1044 {
   1045 	struct xhci_softc * const sc = pipe->up_dev->ud_bus->ub_hcpriv;
   1046 	struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
   1047 	const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
   1048 	struct xhci_trb trb;
   1049 	usbd_status err;
   1050 
   1051 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1052 	DPRINTFN(4, "dci %u", dci, 0, 0, 0);
   1053 
   1054 	trb.trb_0 = 0;
   1055 	trb.trb_2 = 0;
   1056 	trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
   1057 	    XHCI_TRB_3_EP_SET(dci) |
   1058 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_EP);
   1059 
   1060 	err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
   1061 
   1062 	return err;
   1063 }
   1064 
   1065 #if 0
   1066 static usbd_status
   1067 xhci_stop_endpoint(usbd_pipe_handle pipe)
   1068 {
   1069 	struct xhci_softc * const sc = pipe->up_dev->ud_bus->ub_hcpriv;
   1070 	struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
   1071 	struct xhci_trb trb;
   1072 	usbd_status err;
   1073 	const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
   1074 
   1075 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1076 	DPRINTFN(4, "dci %u", dci, 0, 0, 0);
   1077 
   1078 	trb.trb_0 = 0;
   1079 	trb.trb_2 = 0;
   1080 	trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
   1081 	    XHCI_TRB_3_EP_SET(dci) |
   1082 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STOP_EP);
   1083 
   1084 	err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
   1085 
   1086 	return err;
   1087 }
   1088 #endif
   1089 
   1090 static usbd_status
   1091 xhci_set_dequeue(usbd_pipe_handle pipe)
   1092 {
   1093 	struct xhci_softc * const sc = pipe->up_dev->ud_bus->ub_hcpriv;
   1094 	struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
   1095 	const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
   1096 	struct xhci_ring * const xr = &xs->xs_ep[dci].xe_tr;
   1097 	struct xhci_trb trb;
   1098 	usbd_status err;
   1099 
   1100 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1101 	DPRINTFN(4, "slot %u dci %u", xs->xs_idx, dci, 0, 0);
   1102 
   1103 	memset(xr->xr_trb, 0, xr->xr_ntrb * XHCI_TRB_SIZE);
   1104 	usb_syncmem(&xr->xr_dma, 0, xr->xr_ntrb * XHCI_TRB_SIZE,
   1105 	    BUS_DMASYNC_PREWRITE);
   1106 
   1107 	xr->xr_ep = 0;
   1108 	xr->xr_cs = 1;
   1109 
   1110 	trb.trb_0 = xhci_ring_trbp(xr, 0) | 1; /* XXX */
   1111 	trb.trb_2 = 0;
   1112 	trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
   1113 	    XHCI_TRB_3_EP_SET(dci) |
   1114 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SET_TR_DEQUEUE);
   1115 
   1116 	err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
   1117 
   1118 	return err;
   1119 }
   1120 
   1121 static usbd_status
   1122 xhci_open(usbd_pipe_handle pipe)
   1123 {
   1124 	usbd_device_handle const dev = pipe->up_dev;
   1125 	struct xhci_softc * const sc = dev->ud_bus->ub_hcpriv;
   1126 	usb_endpoint_descriptor_t * const ed = pipe->up_endpoint->ue_edesc;
   1127 	const uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
   1128 
   1129 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1130 	DPRINTFN(1, "addr %d depth %d port %d speed %d",
   1131 	    dev->ud_addr, dev->ud_depth, dev->ud_powersrc->up_portno, dev->ud_speed);
   1132 
   1133 	if (sc->sc_dying)
   1134 		return USBD_IOERROR;
   1135 
   1136 	/* Root Hub */
   1137 	if (dev->ud_depth == 0 && dev->ud_powersrc->up_portno == 0 &&
   1138 	    dev->ud_speed != USB_SPEED_SUPER) {
   1139 		switch (ed->bEndpointAddress) {
   1140 		case USB_CONTROL_ENDPOINT:
   1141 			pipe->up_methods = &xhci_root_ctrl_methods;
   1142 			break;
   1143 		case UE_DIR_IN | XHCI_INTR_ENDPT:
   1144 			pipe->up_methods = &xhci_root_intr_methods;
   1145 			break;
   1146 		default:
   1147 			pipe->up_methods = NULL;
   1148 			DPRINTFN(0, "bad bEndpointAddress 0x%02x",
   1149 			    ed->bEndpointAddress, 0, 0, 0);
   1150 			return USBD_INVAL;
   1151 		}
   1152 		return USBD_NORMAL_COMPLETION;
   1153 	}
   1154 
   1155 	switch (xfertype) {
   1156 	case UE_CONTROL:
   1157 		pipe->up_methods = &xhci_device_ctrl_methods;
   1158 		break;
   1159 	case UE_ISOCHRONOUS:
   1160 		pipe->up_methods = &xhci_device_isoc_methods;
   1161 		return USBD_INVAL;
   1162 		break;
   1163 	case UE_BULK:
   1164 		pipe->up_methods = &xhci_device_bulk_methods;
   1165 		break;
   1166 	case UE_INTERRUPT:
   1167 		pipe->up_methods = &xhci_device_intr_methods;
   1168 		break;
   1169 	default:
   1170 		return USBD_IOERROR;
   1171 		break;
   1172 	}
   1173 
   1174 	if (ed->bEndpointAddress != USB_CONTROL_ENDPOINT)
   1175 		xhci_configure_endpoint(pipe);
   1176 
   1177 	return USBD_NORMAL_COMPLETION;
   1178 }
   1179 
   1180 static void
   1181 xhci_rhpsc(struct xhci_softc * const sc, u_int port)
   1182 {
   1183 	usbd_xfer_handle const xfer = sc->sc_intrxfer;
   1184 	uint8_t *p;
   1185 
   1186 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1187 	DPRINTFN(4, "port %u status change", port, 0, 0, 0);
   1188 
   1189 	if (xfer == NULL)
   1190 		return;
   1191 
   1192 	if (!(port >= sc->sc_hs_port_start &&
   1193 	    port < sc->sc_hs_port_start + sc->sc_hs_port_count))
   1194 		return;
   1195 
   1196 	port -= sc->sc_hs_port_start;
   1197 	port += 1;
   1198 	DPRINTFN(4, "hs port %u status change", port, 0, 0, 0);
   1199 
   1200 	p = xfer->ux_buf;
   1201 	memset(p, 0, xfer->ux_length);
   1202 	p[port/NBBY] |= 1 << (port%NBBY);
   1203 	xfer->ux_actlen = xfer->ux_length;
   1204 	xfer->ux_status = USBD_NORMAL_COMPLETION;
   1205 	usb_transfer_complete(xfer);
   1206 }
   1207 
   1208 static void
   1209 xhci_handle_event(struct xhci_softc * const sc,
   1210     const struct xhci_trb * const trb)
   1211 {
   1212 	uint64_t trb_0;
   1213 	uint32_t trb_2, trb_3;
   1214 
   1215 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1216 
   1217 	trb_0 = le64toh(trb->trb_0);
   1218 	trb_2 = le32toh(trb->trb_2);
   1219 	trb_3 = le32toh(trb->trb_3);
   1220 
   1221 	DPRINTFN(14, "event: %p 0x%016"PRIx64" 0x%08"PRIx32" 0x%08"PRIx32,
   1222 	    trb, trb_0, trb_2, trb_3);
   1223 
   1224 	switch (XHCI_TRB_3_TYPE_GET(trb_3)){
   1225 	case XHCI_TRB_EVENT_TRANSFER: {
   1226 		u_int slot, dci;
   1227 		struct xhci_slot *xs;
   1228 		struct xhci_ring *xr;
   1229 		struct xhci_xfer *xx;
   1230 		usbd_xfer_handle xfer;
   1231 		usbd_status err;
   1232 
   1233 		slot = XHCI_TRB_3_SLOT_GET(trb_3);
   1234 		dci = XHCI_TRB_3_EP_GET(trb_3);
   1235 
   1236 		xs = &sc->sc_slots[slot];
   1237 		xr = &xs->xs_ep[dci].xe_tr;
   1238 
   1239 		if ((trb_3 & XHCI_TRB_3_ED_BIT) == 0) {
   1240 			xx = xr->xr_cookies[(trb_0 - xhci_ring_trbp(xr, 0))/
   1241 			    sizeof(struct xhci_trb)];
   1242 		} else {
   1243 			xx = (void *)(uintptr_t)(trb_0 & ~0x3);
   1244 		}
   1245 		xfer = &xx->xx_xfer;
   1246 		DPRINTFN(14, "xfer %p", xfer, 0, 0, 0);
   1247 
   1248 		if ((trb_3 & XHCI_TRB_3_ED_BIT) != 0) {
   1249 			DPRINTFN(14, "transfer event data: "
   1250 			    "0x%016"PRIx64" 0x%08"PRIx32" %02x",
   1251 			    trb_0, XHCI_TRB_2_REM_GET(trb_2),
   1252 			    XHCI_TRB_2_ERROR_GET(trb_2), 0);
   1253 			if ((trb_0 & 0x3) == 0x3) {
   1254 				xfer->ux_actlen = XHCI_TRB_2_REM_GET(trb_2);
   1255 			}
   1256 		}
   1257 
   1258 		if (XHCI_TRB_2_ERROR_GET(trb_2) ==
   1259 		    XHCI_TRB_ERROR_SUCCESS) {
   1260 			xfer->ux_actlen = xfer->ux_length - XHCI_TRB_2_REM_GET(trb_2);
   1261 			err = USBD_NORMAL_COMPLETION;
   1262 		} else if (XHCI_TRB_2_ERROR_GET(trb_2) ==
   1263 		    XHCI_TRB_ERROR_SHORT_PKT) {
   1264 			xfer->ux_actlen = xfer->ux_length - XHCI_TRB_2_REM_GET(trb_2);
   1265 			err = USBD_NORMAL_COMPLETION;
   1266 		} else if (XHCI_TRB_2_ERROR_GET(trb_2) ==
   1267 		    XHCI_TRB_ERROR_STALL) {
   1268 			err = USBD_STALLED;
   1269 			xr->is_halted = true;
   1270 			DPRINTFN(1, "ev: xfer done: err %u slot %u dci %u",
   1271 			    XHCI_TRB_2_ERROR_GET(trb_2), slot, dci, 0);
   1272 		} else {
   1273 			err = USBD_IOERROR;
   1274 		}
   1275 		xfer->ux_status = err;
   1276 
   1277 		//mutex_enter(&sc->sc_lock); /* XXX ??? */
   1278 		if ((trb_3 & XHCI_TRB_3_ED_BIT) != 0) {
   1279 			if ((trb_0 & 0x3) == 0x0) {
   1280 				usb_transfer_complete(xfer);
   1281 			}
   1282 		} else {
   1283 			usb_transfer_complete(xfer);
   1284 		}
   1285 		//mutex_exit(&sc->sc_lock); /* XXX ??? */
   1286 
   1287 		}
   1288 		break;
   1289 	case XHCI_TRB_EVENT_CMD_COMPLETE:
   1290 		if (trb_0 == sc->sc_command_addr) {
   1291 			sc->sc_result_trb.trb_0 = trb_0;
   1292 			sc->sc_result_trb.trb_2 = trb_2;
   1293 			sc->sc_result_trb.trb_3 = trb_3;
   1294 			if (XHCI_TRB_2_ERROR_GET(trb_2) !=
   1295 			    XHCI_TRB_ERROR_SUCCESS) {
   1296 				DPRINTFN(1, "command completion "
   1297 				    "failure: 0x%016"PRIx64" 0x%08"PRIx32" "
   1298 				    "0x%08"PRIx32, trb_0, trb_2, trb_3, 0);
   1299 			}
   1300 			cv_signal(&sc->sc_command_cv);
   1301 		} else {
   1302 			DPRINTFN(1, "event: %p 0x%016"PRIx64" "
   1303 			    "0x%08"PRIx32" 0x%08"PRIx32, trb, trb_0,
   1304 			    trb_2, trb_3);
   1305 		}
   1306 		break;
   1307 	case XHCI_TRB_EVENT_PORT_STS_CHANGE:
   1308 		xhci_rhpsc(sc, (uint32_t)((trb_0 >> 24) & 0xff));
   1309 		break;
   1310 	default:
   1311 		break;
   1312 	}
   1313 }
   1314 
   1315 static void
   1316 xhci_softintr(void *v)
   1317 {
   1318 	struct usbd_bus * const bus = v;
   1319 	struct xhci_softc * const sc = bus->ub_hcpriv;
   1320 	struct xhci_ring * const er = &sc->sc_er;
   1321 	struct xhci_trb *trb;
   1322 	int i, j, k;
   1323 
   1324 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1325 
   1326 	i = er->xr_ep;
   1327 	j = er->xr_cs;
   1328 
   1329 	DPRINTFN(16, "xr_ep %d xr_cs %d", i, j, 0, 0);
   1330 
   1331 	while (1) {
   1332 		usb_syncmem(&er->xr_dma, XHCI_TRB_SIZE * i, XHCI_TRB_SIZE,
   1333 		    BUS_DMASYNC_POSTREAD);
   1334 		trb = &er->xr_trb[i];
   1335 		k = (le32toh(trb->trb_3) & XHCI_TRB_3_CYCLE_BIT) ? 1 : 0;
   1336 
   1337 		if (j != k)
   1338 			break;
   1339 
   1340 		xhci_handle_event(sc, trb);
   1341 
   1342 		i++;
   1343 		if (i == XHCI_EVENT_RING_TRBS) {
   1344 			i = 0;
   1345 			j ^= 1;
   1346 		}
   1347 	}
   1348 
   1349 	er->xr_ep = i;
   1350 	er->xr_cs = j;
   1351 
   1352 	xhci_rt_write_8(sc, XHCI_ERDP(0), xhci_ring_trbp(er, er->xr_ep) |
   1353 	    XHCI_ERDP_LO_BUSY);
   1354 
   1355 	DPRINTFN(16, "ends", 0, 0, 0, 0);
   1356 
   1357 	return;
   1358 }
   1359 
   1360 static void
   1361 xhci_poll(struct usbd_bus *bus)
   1362 {
   1363 	struct xhci_softc * const sc = bus->ub_hcpriv;
   1364 
   1365 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1366 
   1367 	mutex_spin_enter(&sc->sc_intr_lock);
   1368 	xhci_intr1(sc);
   1369 	mutex_spin_exit(&sc->sc_intr_lock);
   1370 
   1371 	return;
   1372 }
   1373 
   1374 static usbd_xfer_handle
   1375 xhci_allocx(struct usbd_bus *bus)
   1376 {
   1377 	struct xhci_softc * const sc = bus->ub_hcpriv;
   1378 	usbd_xfer_handle xfer;
   1379 
   1380 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1381 
   1382 	xfer = pool_cache_get(sc->sc_xferpool, PR_NOWAIT);
   1383 	if (xfer != NULL) {
   1384 		memset(xfer, 0, sizeof(struct xhci_xfer));
   1385 #ifdef DIAGNOSTIC
   1386 		xfer->ux_state = XFER_BUSY;
   1387 #endif
   1388 	}
   1389 
   1390 	return xfer;
   1391 }
   1392 
   1393 static void
   1394 xhci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
   1395 {
   1396 	struct xhci_softc * const sc = bus->ub_hcpriv;
   1397 
   1398 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1399 
   1400 #ifdef DIAGNOSTIC
   1401 	if (xfer->ux_state != XFER_BUSY) {
   1402 		DPRINTFN(0, "xfer=%p not busy, 0x%08x",
   1403 		    xfer, xfer->ux_state, 0, 0);
   1404 	}
   1405 	xfer->ux_state = XFER_FREE;
   1406 #endif
   1407 	pool_cache_put(sc->sc_xferpool, xfer);
   1408 }
   1409 
   1410 static void
   1411 xhci_get_lock(struct usbd_bus *bus, kmutex_t **lock)
   1412 {
   1413 	struct xhci_softc * const sc = bus->ub_hcpriv;
   1414 
   1415 	*lock = &sc->sc_lock;
   1416 }
   1417 
   1418 extern uint32_t usb_cookie_no;
   1419 
   1420 static usbd_status
   1421 xhci_new_device(device_t parent, usbd_bus_handle bus, int depth,
   1422     int speed, int port, struct usbd_port *up)
   1423 {
   1424 	struct xhci_softc * const sc = bus->ub_hcpriv;
   1425 	usbd_device_handle dev;
   1426 	usbd_status err;
   1427 	usb_device_descriptor_t *dd;
   1428 	struct usbd_device *hub;
   1429 	struct usbd_device *adev;
   1430 	int rhport = 0;
   1431 	struct xhci_slot *xs;
   1432 	uint32_t *cp;
   1433 	uint8_t slot;
   1434 	uint8_t addr;
   1435 
   1436 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1437 	DPRINTFN(4, "port=%d depth=%d speed=%d upport %d",
   1438 		 port, depth, speed, up->up_portno);
   1439 
   1440 	dev = kmem_zalloc(sizeof(*dev), KM_SLEEP);
   1441 	if (dev == NULL)
   1442 		return USBD_NOMEM;
   1443 
   1444 	dev->ud_bus = bus;
   1445 
   1446 	/* Set up default endpoint handle. */
   1447 	dev->ud_ep0.ue_edesc = &dev->ud_ep0desc;
   1448 
   1449 	/* Set up default endpoint descriptor. */
   1450 	dev->ud_ep0desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
   1451 	dev->ud_ep0desc.bDescriptorType = UDESC_ENDPOINT;
   1452 	dev->ud_ep0desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
   1453 	dev->ud_ep0desc.bmAttributes = UE_CONTROL;
   1454 	/* XXX */
   1455 	if (speed == USB_SPEED_LOW)
   1456 		USETW(dev->ud_ep0desc.wMaxPacketSize, USB_MAX_IPACKET);
   1457 	else
   1458 		USETW(dev->ud_ep0desc.wMaxPacketSize, 64);
   1459 	dev->ud_ep0desc.bInterval = 0;
   1460 
   1461 	/* doesn't matter, just don't let it uninitialized */
   1462 	dev->ud_ep0.ue_toggle = 0;
   1463 
   1464 	DPRINTFN(4, "up %p portno %d", up, up->up_portno, 0, 0);
   1465 
   1466 	dev->ud_quirks = &usbd_no_quirk;
   1467 	dev->ud_addr = 0;
   1468 	dev->ud_ddesc.bMaxPacketSize = 0;
   1469 	dev->ud_depth = depth;
   1470 	dev->ud_powersrc = up;
   1471 	dev->ud_myhub = up->up_parent;
   1472 
   1473 	up->up_dev = dev;
   1474 
   1475 	/* Locate root hub port */
   1476 	for (adev = dev, hub = dev;
   1477 	    hub != NULL;
   1478 	    adev = hub, hub = hub->ud_myhub) {
   1479 		DPRINTFN(4, "hub %p", hub, 0, 0, 0);
   1480 	}
   1481 	DPRINTFN(4, "hub %p", hub, 0, 0, 0);
   1482 
   1483 	if (hub != NULL) {
   1484 		for (int p = 0; p < hub->ud_hub->uh_hubdesc.bNbrPorts; p++) {
   1485 			if (hub->ud_hub->uh_ports[p].up_dev == adev) {
   1486 				rhport = p;
   1487 			}
   1488 		}
   1489 	} else {
   1490 		rhport = port;
   1491 	}
   1492 	if (speed == USB_SPEED_SUPER) {
   1493 		rhport += sc->sc_ss_port_start - 1;
   1494 	} else {
   1495 		rhport += sc->sc_hs_port_start - 1;
   1496 	}
   1497 	DPRINTFN(4, "rhport %d", rhport, 0, 0, 0);
   1498 
   1499 	dev->ud_speed = speed;
   1500 	dev->ud_langid = USBD_NOLANG;
   1501 	dev->ud_cookie.cookie = ++usb_cookie_no;
   1502 
   1503 	/* Establish the default pipe. */
   1504 	err = usbd_setup_pipe(dev, 0, &dev->ud_ep0, USBD_DEFAULT_INTERVAL,
   1505 	    &dev->ud_pipe0);
   1506 	if (err) {
   1507 		usbd_remove_device(dev, up);
   1508 		return (err);
   1509 	}
   1510 
   1511 	dd = &dev->ud_ddesc;
   1512 
   1513 	if ((depth == 0) && (port == 0)) {
   1514 		KASSERT(bus->ub_devices[dev->ud_addr] == NULL);
   1515 		bus->ub_devices[dev->ud_addr] = dev;
   1516 		err = usbd_get_initial_ddesc(dev, dd);
   1517 		if (err)
   1518 			return err;
   1519 		err = usbd_reload_device_desc(dev);
   1520 		if (err)
   1521 			return err;
   1522 	} else {
   1523 		err = xhci_enable_slot(sc, &slot);
   1524 		if (err)
   1525 			return err;
   1526 		err = xhci_init_slot(sc, slot, depth, speed, port, rhport);
   1527 		if (err)
   1528 			return err;
   1529 		xs = &sc->sc_slots[slot];
   1530 		dev->ud_hcpriv = xs;
   1531 		cp = xhci_slot_get_dcv(sc, xs, XHCI_DCI_SLOT);
   1532 		//hexdump("slot context", cp, sc->sc_ctxsz);
   1533 		addr = XHCI_SCTX_3_DEV_ADDR_GET(cp[3]);
   1534 		DPRINTFN(4, "device address %u", addr, 0, 0, 0);
   1535 		/* XXX ensure we know when the hardware does something
   1536 		   we can't yet cope with */
   1537 		KASSERT(addr >= 1 && addr <= 127);
   1538 		dev->ud_addr = addr;
   1539 		/* XXX dev->ud_addr not necessarily unique on bus */
   1540 		KASSERT(bus->ub_devices[dev->ud_addr] == NULL);
   1541 		bus->ub_devices[dev->ud_addr] = dev;
   1542 
   1543 		err = usbd_get_initial_ddesc(dev, dd);
   1544 		if (err)
   1545 			return err;
   1546 		/* 4.8.2.1 */
   1547 		if (speed == USB_SPEED_SUPER)
   1548 			USETW(dev->ud_ep0desc.wMaxPacketSize,
   1549 			    (1 << dd->bMaxPacketSize));
   1550 		else
   1551 	 		USETW(dev->ud_ep0desc.wMaxPacketSize,
   1552 			    dd->bMaxPacketSize);
   1553 		DPRINTFN(4, "bMaxPacketSize %u", dd->bMaxPacketSize, 0, 0, 0);
   1554 		xhci_update_ep0_mps(sc, xs,
   1555 		    UGETW(dev->ud_ep0desc.wMaxPacketSize));
   1556 		err = usbd_reload_device_desc(dev);
   1557 		if (err)
   1558 			return err;
   1559 
   1560 		usbd_kill_pipe(dev->ud_pipe0);
   1561 		err = usbd_setup_pipe(dev, 0, &dev->ud_ep0,
   1562 		    USBD_DEFAULT_INTERVAL, &dev->ud_pipe0);
   1563 	}
   1564 
   1565 	DPRINTFN(1, "adding unit addr=%d, rev=%02x,",
   1566 		dev->ud_addr, UGETW(dd->bcdUSB), 0, 0);
   1567 	DPRINTFN(1, " class=%d, subclass=%d, protocol=%d,",
   1568 		dd->bDeviceClass, dd->bDeviceSubClass,
   1569 		dd->bDeviceProtocol, 0);
   1570 	DPRINTFN(1, " mps=%d, len=%d, noconf=%d, speed=%d",
   1571 		dd->bMaxPacketSize, dd->bLength, dd->bNumConfigurations,
   1572 		dev->ud_speed);
   1573 
   1574 	usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
   1575 
   1576 	if ((depth == 0) && (port == 0)) {
   1577 		usbd_attach_roothub(parent, dev);
   1578 		DPRINTFN(1, "root_hub %p", bus->ub_roothub, 0, 0, 0);
   1579 		return USBD_NORMAL_COMPLETION;
   1580 	}
   1581 
   1582 
   1583 	err = usbd_probe_and_attach(parent, dev, port, dev->ud_addr);
   1584 	if (err) {
   1585 		usbd_remove_device(dev, up);
   1586 		return (err);
   1587 	}
   1588 
   1589 	return USBD_NORMAL_COMPLETION;
   1590 }
   1591 
   1592 static usbd_status
   1593 xhci_ring_init(struct xhci_softc * const sc, struct xhci_ring * const xr,
   1594     size_t ntrb, size_t align)
   1595 {
   1596 	usbd_status err;
   1597 	size_t size = ntrb * XHCI_TRB_SIZE;
   1598 
   1599 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1600 
   1601 	err = usb_allocmem(&sc->sc_bus, size, align, &xr->xr_dma);
   1602 	if (err)
   1603 		return err;
   1604 	mutex_init(&xr->xr_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
   1605 	xr->xr_cookies = kmem_zalloc(sizeof(*xr->xr_cookies) * ntrb, KM_SLEEP);
   1606 	xr->xr_trb = xhci_ring_trbv(xr, 0);
   1607 	xr->xr_ntrb = ntrb;
   1608 	xr->xr_ep = 0;
   1609 	xr->xr_cs = 1;
   1610 	memset(xr->xr_trb, 0, size);
   1611 	usb_syncmem(&xr->xr_dma, 0, size, BUS_DMASYNC_PREWRITE);
   1612 	xr->is_halted = false;
   1613 
   1614 	return USBD_NORMAL_COMPLETION;
   1615 }
   1616 
   1617 static void
   1618 xhci_ring_free(struct xhci_softc * const sc, struct xhci_ring * const xr)
   1619 {
   1620 	usb_freemem(&sc->sc_bus, &xr->xr_dma);
   1621 	mutex_destroy(&xr->xr_lock);
   1622 	kmem_free(xr->xr_cookies, sizeof(*xr->xr_cookies) * xr->xr_ntrb);
   1623 }
   1624 
   1625 static void
   1626 xhci_ring_put(struct xhci_softc * const sc, struct xhci_ring * const xr,
   1627     void *cookie, struct xhci_trb * const trbs, size_t ntrbs)
   1628 {
   1629 	size_t i;
   1630 	u_int ri;
   1631 	u_int cs;
   1632 	uint64_t parameter;
   1633 	uint32_t status;
   1634 	uint32_t control;
   1635 
   1636 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1637 
   1638 	for (i = 0; i < ntrbs; i++) {
   1639 		DPRINTFN(12, "xr %p trbs %p num %zu", xr, trbs, i, 0);
   1640 		DPRINTFN(12, " %016"PRIx64" %08"PRIx32" %08"PRIx32,
   1641 		    trbs[i].trb_0, trbs[i].trb_2, trbs[i].trb_3, 0);
   1642 		KASSERT(XHCI_TRB_3_TYPE_GET(trbs[i].trb_3) !=
   1643 		    XHCI_TRB_TYPE_LINK);
   1644 	}
   1645 
   1646 	DPRINTFN(12, "%p xr_ep 0x%x xr_cs %u", xr, xr->xr_ep, xr->xr_cs, 0);
   1647 
   1648 	ri = xr->xr_ep;
   1649 	cs = xr->xr_cs;
   1650 
   1651 	/*
   1652 	 * Although the xhci hardware can do scatter/gather dma from
   1653 	 * arbitrary sized buffers, there is a non-obvious restriction
   1654 	 * that a LINK trb is only allowed at the end of a burst of
   1655 	 * transfers - which might be 16kB.
   1656 	 * Arbitrary aligned LINK trb definitely fail on Ivy bridge.
   1657 	 * The simple solution is not to allow a LINK trb in the middle
   1658 	 * of anything - as here.
   1659 	 * XXX: (dsl) There are xhci controllers out there (eg some made by
   1660 	 * ASMedia) that seem to lock up if they process a LINK trb but
   1661 	 * cannot process the linked-to trb yet.
   1662 	 * The code should write the 'cycle' bit on the link trb AFTER
   1663 	 * adding the other trb.
   1664 	 */
   1665 	if (ri + ntrbs >= (xr->xr_ntrb - 1)) {
   1666 		parameter = xhci_ring_trbp(xr, 0);
   1667 		status = 0;
   1668 		control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) |
   1669 		    XHCI_TRB_3_TC_BIT | (cs ? XHCI_TRB_3_CYCLE_BIT : 0);
   1670 		xhci_trb_put(&xr->xr_trb[ri], htole64(parameter),
   1671 		    htole32(status), htole32(control));
   1672 		usb_syncmem(&xr->xr_dma, XHCI_TRB_SIZE * ri, XHCI_TRB_SIZE * 1,
   1673 		    BUS_DMASYNC_PREWRITE);
   1674 		xr->xr_cookies[ri] = NULL;
   1675 		xr->xr_ep = 0;
   1676 		xr->xr_cs ^= 1;
   1677 		ri = xr->xr_ep;
   1678 		cs = xr->xr_cs;
   1679 	}
   1680 
   1681 	ri++;
   1682 
   1683 	/* Write any subsequent TRB first */
   1684 	for (i = 1; i < ntrbs; i++) {
   1685 		parameter = trbs[i].trb_0;
   1686 		status = trbs[i].trb_2;
   1687 		control = trbs[i].trb_3;
   1688 
   1689 		if (cs) {
   1690 			control |= XHCI_TRB_3_CYCLE_BIT;
   1691 		} else {
   1692 			control &= ~XHCI_TRB_3_CYCLE_BIT;
   1693 		}
   1694 
   1695 		xhci_trb_put(&xr->xr_trb[ri], htole64(parameter),
   1696 		    htole32(status), htole32(control));
   1697 		usb_syncmem(&xr->xr_dma, XHCI_TRB_SIZE * ri, XHCI_TRB_SIZE * 1,
   1698 		    BUS_DMASYNC_PREWRITE);
   1699 		xr->xr_cookies[ri] = cookie;
   1700 		ri++;
   1701 	}
   1702 
   1703 	/* Write the first TRB last */
   1704 	i = 0;
   1705 	{
   1706 		parameter = trbs[i].trb_0;
   1707 		status = trbs[i].trb_2;
   1708 		control = trbs[i].trb_3;
   1709 
   1710 		if (xr->xr_cs) {
   1711 			control |= XHCI_TRB_3_CYCLE_BIT;
   1712 		} else {
   1713 			control &= ~XHCI_TRB_3_CYCLE_BIT;
   1714 		}
   1715 
   1716 		xhci_trb_put(&xr->xr_trb[xr->xr_ep], htole64(parameter),
   1717 		    htole32(status), htole32(control));
   1718 		usb_syncmem(&xr->xr_dma, XHCI_TRB_SIZE * ri, XHCI_TRB_SIZE * 1,
   1719 		    BUS_DMASYNC_PREWRITE);
   1720 		xr->xr_cookies[xr->xr_ep] = cookie;
   1721 	}
   1722 
   1723 	xr->xr_ep = ri;
   1724 	xr->xr_cs = cs;
   1725 
   1726 	DPRINTFN(12, "%p xr_ep 0x%x xr_cs %u", xr, xr->xr_ep, xr->xr_cs, 0);
   1727 }
   1728 
   1729 static usbd_status
   1730 xhci_do_command(struct xhci_softc * const sc, struct xhci_trb * const trb,
   1731     int timeout)
   1732 {
   1733 	struct xhci_ring * const cr = &sc->sc_cr;
   1734 	usbd_status err;
   1735 
   1736 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1737 	DPRINTFN(12, "input: 0x%016"PRIx64" 0x%08"PRIx32" 0x%08"PRIx32,
   1738 	    trb->trb_0, trb->trb_2, trb->trb_3, 0);
   1739 
   1740 	mutex_enter(&sc->sc_lock);
   1741 
   1742 	KASSERT(sc->sc_command_addr == 0);
   1743 	sc->sc_command_addr = xhci_ring_trbp(cr, cr->xr_ep);
   1744 
   1745 	mutex_enter(&cr->xr_lock);
   1746 	xhci_ring_put(sc, cr, NULL, trb, 1);
   1747 	mutex_exit(&cr->xr_lock);
   1748 
   1749 	xhci_db_write_4(sc, XHCI_DOORBELL(0), 0);
   1750 
   1751 	if (cv_timedwait(&sc->sc_command_cv, &sc->sc_lock,
   1752 	    MAX(1, mstohz(timeout))) == EWOULDBLOCK) {
   1753 		err = USBD_TIMEOUT;
   1754 		goto timedout;
   1755 	}
   1756 
   1757 	trb->trb_0 = sc->sc_result_trb.trb_0;
   1758 	trb->trb_2 = sc->sc_result_trb.trb_2;
   1759 	trb->trb_3 = sc->sc_result_trb.trb_3;
   1760 
   1761 	DPRINTFN(12, "output: 0x%016"PRIx64" 0x%08"PRIx32" 0x%08"PRIx32"",
   1762 	    trb->trb_0, trb->trb_2, trb->trb_3, 0);
   1763 
   1764 	switch (XHCI_TRB_2_ERROR_GET(trb->trb_2)) {
   1765 	case XHCI_TRB_ERROR_SUCCESS:
   1766 		err = USBD_NORMAL_COMPLETION;
   1767 		break;
   1768 	default:
   1769 	case 192 ... 223:
   1770 		err = USBD_IOERROR;
   1771 		break;
   1772 	case 224 ... 255:
   1773 		err = USBD_NORMAL_COMPLETION;
   1774 		break;
   1775 	}
   1776 
   1777 timedout:
   1778 	sc->sc_command_addr = 0;
   1779 	mutex_exit(&sc->sc_lock);
   1780 	return err;
   1781 }
   1782 
   1783 static usbd_status
   1784 xhci_enable_slot(struct xhci_softc * const sc, uint8_t * const slotp)
   1785 {
   1786 	struct xhci_trb trb;
   1787 	usbd_status err;
   1788 
   1789 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1790 
   1791 	trb.trb_0 = 0;
   1792 	trb.trb_2 = 0;
   1793 	trb.trb_3 = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ENABLE_SLOT);
   1794 
   1795 	err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
   1796 	if (err != USBD_NORMAL_COMPLETION) {
   1797 		return err;
   1798 	}
   1799 
   1800 	*slotp = XHCI_TRB_3_SLOT_GET(trb.trb_3);
   1801 
   1802 	return err;
   1803 }
   1804 
   1805 static usbd_status
   1806 xhci_address_device(struct xhci_softc * const sc,
   1807     uint64_t icp, uint8_t slot_id, bool bsr)
   1808 {
   1809 	struct xhci_trb trb;
   1810 	usbd_status err;
   1811 
   1812 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1813 
   1814 	trb.trb_0 = icp;
   1815 	trb.trb_2 = 0;
   1816 	trb.trb_3 = XHCI_TRB_3_SLOT_SET(slot_id) |
   1817 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ADDRESS_DEVICE) |
   1818 	    (bsr ? XHCI_TRB_3_BSR_BIT : 0);
   1819 
   1820 	err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
   1821 	return err;
   1822 }
   1823 
   1824 static usbd_status
   1825 xhci_update_ep0_mps(struct xhci_softc * const sc,
   1826     struct xhci_slot * const xs, u_int mps)
   1827 {
   1828 	struct xhci_trb trb;
   1829 	usbd_status err;
   1830 	uint32_t * cp;
   1831 
   1832 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1833 	DPRINTFN(4, "slot %u mps %u", xs->xs_idx, mps, 0, 0);
   1834 
   1835 	cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL);
   1836 	cp[0] = htole32(0);
   1837 	cp[1] = htole32(XHCI_INCTX_1_ADD_MASK(XHCI_DCI_EP_CONTROL));
   1838 
   1839 	cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_EP_CONTROL));
   1840 	cp[1] = htole32(XHCI_EPCTX_1_MAXP_SIZE_SET(mps));
   1841 
   1842 	/* sync input contexts before they are read from memory */
   1843 	usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE);
   1844 	hexdump("input context", xhci_slot_get_icv(sc, xs, 0),
   1845 	    sc->sc_ctxsz * 4);
   1846 
   1847 	trb.trb_0 = xhci_slot_get_icp(sc, xs, 0);
   1848 	trb.trb_2 = 0;
   1849 	trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
   1850 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVALUATE_CTX);
   1851 
   1852 	err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
   1853 	KASSERT(err == USBD_NORMAL_COMPLETION); /* XXX */
   1854 	return err;
   1855 }
   1856 
   1857 static void
   1858 xhci_set_dcba(struct xhci_softc * const sc, uint64_t dcba, int si)
   1859 {
   1860 	uint64_t * const dcbaa = KERNADDR(&sc->sc_dcbaa_dma, 0);
   1861 
   1862 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1863 	DPRINTFN(4, "dcbaa %p dc %016"PRIx64" slot %d",
   1864 	    &dcbaa[si], dcba, si, 0);
   1865 
   1866 	dcbaa[si] = htole64(dcba);
   1867 	usb_syncmem(&sc->sc_dcbaa_dma, si * sizeof(uint64_t), sizeof(uint64_t),
   1868 	    BUS_DMASYNC_PREWRITE);
   1869 }
   1870 
   1871 static usbd_status
   1872 xhci_init_slot(struct xhci_softc * const sc, uint32_t slot, int depth,
   1873     int speed, int port, int rhport)
   1874 {
   1875 	struct xhci_slot *xs;
   1876 	usbd_status err;
   1877 	u_int dci;
   1878 	uint32_t *cp;
   1879 	uint32_t mps;
   1880 	uint32_t xspeed;
   1881 
   1882 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1883 	DPRINTFN(4, "slot %u depth %d speed %d",
   1884 	    slot, depth, speed, 0);
   1885 	DPRINTFN(4, " port %d rhport %d",
   1886 	    port, rhport, 0, 0);
   1887 
   1888 	switch (speed) {
   1889 	case USB_SPEED_LOW:
   1890 		xspeed = 2;
   1891 		mps = USB_MAX_IPACKET;
   1892 		break;
   1893 	case USB_SPEED_FULL:
   1894 		xspeed = 1;
   1895 		mps = 64;
   1896 		break;
   1897 	case USB_SPEED_HIGH:
   1898 		xspeed = 3;
   1899 		mps = USB_2_MAX_CTRL_PACKET;
   1900 		break;
   1901 	case USB_SPEED_SUPER:
   1902 		xspeed = 4;
   1903 		mps = USB_3_MAX_CTRL_PACKET;
   1904 		break;
   1905 	default:
   1906 		DPRINTFN(0, "impossible speed: %x", speed, 0, 0, 0);
   1907 		return USBD_INVAL;
   1908 	}
   1909 
   1910 	xs = &sc->sc_slots[slot];
   1911 	xs->xs_idx = slot;
   1912 
   1913 	/* allocate contexts */
   1914 	err = usb_allocmem(&sc->sc_bus, sc->sc_pgsz, sc->sc_pgsz,
   1915 	    &xs->xs_dc_dma);
   1916 	if (err)
   1917 		return err;
   1918 	memset(KERNADDR(&xs->xs_dc_dma, 0), 0, sc->sc_pgsz);
   1919 
   1920 	err = usb_allocmem(&sc->sc_bus, sc->sc_pgsz, sc->sc_pgsz,
   1921 	    &xs->xs_ic_dma);
   1922 	if (err)
   1923 		return err;
   1924 	memset(KERNADDR(&xs->xs_ic_dma, 0), 0, sc->sc_pgsz);
   1925 
   1926 	for (dci = 0; dci < 32; dci++) {
   1927 		//CTASSERT(sizeof(xs->xs_ep[dci]) == sizeof(struct xhci_endpoint));
   1928 		memset(&xs->xs_ep[dci], 0, sizeof(xs->xs_ep[dci]));
   1929 		if (dci == XHCI_DCI_SLOT)
   1930 			continue;
   1931 		err = xhci_ring_init(sc, &xs->xs_ep[dci].xe_tr,
   1932 		    XHCI_TRANSFER_RING_TRBS, XHCI_TRB_ALIGN);
   1933 		if (err) {
   1934 			DPRINTFN(0, "ring init failure", 0, 0, 0, 0);
   1935 			return err;
   1936 		}
   1937 	}
   1938 
   1939 	/* set up initial input control context */
   1940 	cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL);
   1941 	cp[0] = htole32(0);
   1942 	cp[1] = htole32(XHCI_INCTX_1_ADD_MASK(XHCI_DCI_EP_CONTROL)|
   1943 	    XHCI_INCTX_1_ADD_MASK(XHCI_DCI_SLOT));
   1944 
   1945 	/* set up input slot context */
   1946 	cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_SLOT));
   1947 	cp[0] = htole32(
   1948 		XHCI_SCTX_0_CTX_NUM_SET(1) |
   1949 		XHCI_SCTX_0_SPEED_SET(xspeed)
   1950 		);
   1951 	cp[1] = htole32(
   1952 		XHCI_SCTX_1_RH_PORT_SET(rhport)
   1953 		);
   1954 	cp[2] = htole32(
   1955 		XHCI_SCTX_2_IRQ_TARGET_SET(0)
   1956 		);
   1957 	cp[3] = htole32(0);
   1958 
   1959 	/* set up input EP0 context */
   1960 	cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_EP_CONTROL));
   1961 	cp[0] = htole32(0);
   1962 	cp[1] = htole32(
   1963 		XHCI_EPCTX_1_MAXP_SIZE_SET(mps) |
   1964 		XHCI_EPCTX_1_EPTYPE_SET(4) |
   1965 		XHCI_EPCTX_1_CERR_SET(3)
   1966 		);
   1967 	/* can't use xhci_ep_get_dci() yet? */
   1968 	*(uint64_t *)(&cp[2]) = htole64(
   1969 	    xhci_ring_trbp(&xs->xs_ep[XHCI_DCI_EP_CONTROL].xe_tr, 0) |
   1970 	    XHCI_EPCTX_2_DCS_SET(1));
   1971 	cp[4] = htole32(
   1972 		XHCI_EPCTX_4_AVG_TRB_LEN_SET(8)
   1973 		);
   1974 
   1975 	/* sync input contexts before they are read from memory */
   1976 	usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE);
   1977 	hexdump("input context", xhci_slot_get_icv(sc, xs, 0),
   1978 	    sc->sc_ctxsz * 3);
   1979 
   1980 	xhci_set_dcba(sc, DMAADDR(&xs->xs_dc_dma, 0), slot);
   1981 
   1982 	err = xhci_address_device(sc, xhci_slot_get_icp(sc, xs, 0), slot,
   1983 	    false);
   1984 
   1985 	usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
   1986 	hexdump("output context", xhci_slot_get_dcv(sc, xs, 0),
   1987 	    sc->sc_ctxsz * 2);
   1988 
   1989 	return err;
   1990 }
   1991 
   1992 /* ----- */
   1993 
   1994 static void
   1995 xhci_noop(usbd_pipe_handle pipe)
   1996 {
   1997 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   1998 }
   1999 
   2000 /* root hub descriptors */
   2001 
   2002 static const usb_device_descriptor_t xhci_devd = {
   2003 	.bLength = USB_DEVICE_DESCRIPTOR_SIZE,
   2004 	.bDescriptorType = UDESC_DEVICE,
   2005 	.bcdUSB = {0x00, 0x02},
   2006 	.bDeviceClass = UDCLASS_HUB,
   2007 	.bDeviceSubClass = UDSUBCLASS_HUB,
   2008 	.bDeviceProtocol = UDPROTO_HSHUBSTT,
   2009 	.bMaxPacketSize = 64,
   2010 	.idVendor = {0},
   2011 	.idProduct = {0},
   2012 	.bcdDevice = {0x00,0x01},
   2013 	.iManufacturer = 1,
   2014 	.iProduct = 2,
   2015 	.iSerialNumber = 0,
   2016 	.bNumConfigurations = 1
   2017 };
   2018 
   2019 static const usb_device_qualifier_t xhci_odevd = {
   2020 	.bLength = USB_DEVICE_DESCRIPTOR_SIZE,
   2021 	.bDescriptorType = UDESC_DEVICE_QUALIFIER,
   2022 	.bcdUSB = {0x00, 0x02},
   2023 	.bDeviceClass = UDCLASS_HUB,
   2024 	.bDeviceSubClass = UDSUBCLASS_HUB,
   2025 	.bDeviceProtocol = UDPROTO_FSHUB,
   2026 	.bMaxPacketSize0 = 64,
   2027 	.bNumConfigurations = 1,
   2028 };
   2029 
   2030 static const usb_config_descriptor_t xhci_confd = {
   2031 	.bLength = USB_CONFIG_DESCRIPTOR_SIZE,
   2032 	.bDescriptorType = UDESC_CONFIG,
   2033 	.wTotalLength = {
   2034 		USB_CONFIG_DESCRIPTOR_SIZE +
   2035 		USB_INTERFACE_DESCRIPTOR_SIZE +
   2036 		USB_ENDPOINT_DESCRIPTOR_SIZE
   2037 	},
   2038 	.bNumInterface = 1,
   2039 	.bConfigurationValue = 1,
   2040 	.iConfiguration = 0,
   2041 	.bmAttributes = UC_ATTR_MBO | UC_SELF_POWERED,
   2042 	.bMaxPower = 0
   2043 };
   2044 
   2045 static const usb_interface_descriptor_t xhci_ifcd = {
   2046 	.bLength = USB_INTERFACE_DESCRIPTOR_SIZE,
   2047 	.bDescriptorType = UDESC_INTERFACE,
   2048 	.bInterfaceNumber = 0,
   2049 	.bAlternateSetting = 0,
   2050 	.bNumEndpoints = 1,
   2051 	.bInterfaceClass = UICLASS_HUB,
   2052 	.bInterfaceSubClass = UISUBCLASS_HUB,
   2053 	.bInterfaceProtocol = UIPROTO_HSHUBSTT,
   2054 	.iInterface = 0
   2055 };
   2056 
   2057 static const usb_endpoint_descriptor_t xhci_endpd = {
   2058 	.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE,
   2059 	.bDescriptorType = UDESC_ENDPOINT,
   2060 	.bEndpointAddress = UE_DIR_IN | XHCI_INTR_ENDPT,
   2061 	.bmAttributes = UE_INTERRUPT,
   2062 	.wMaxPacketSize = {8, 0},
   2063 	.bInterval = 12
   2064 };
   2065 
   2066 static const usb_hub_descriptor_t xhci_hubd = {
   2067 	.bDescLength = USB_HUB_DESCRIPTOR_SIZE,
   2068 	.bDescriptorType = UDESC_HUB,
   2069 	.bNbrPorts = 0,
   2070 	.wHubCharacteristics = {0,0},
   2071 	.bPwrOn2PwrGood = 0,
   2072 	.bHubContrCurrent = 0,
   2073 	.DeviceRemovable = {""},
   2074 	.PortPowerCtrlMask = {""},
   2075 };
   2076 
   2077 /* root hub control */
   2078 
   2079 static usbd_status
   2080 xhci_root_ctrl_transfer(usbd_xfer_handle xfer)
   2081 {
   2082 	struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   2083 	usbd_status err;
   2084 
   2085 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2086 
   2087 	/* Insert last in queue. */
   2088 	mutex_enter(&sc->sc_lock);
   2089 	err = usb_insert_transfer(xfer);
   2090 	mutex_exit(&sc->sc_lock);
   2091 	if (err)
   2092 		return err;
   2093 
   2094 	/* Pipe isn't running, start first */
   2095 	return (xhci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue)));
   2096 }
   2097 
   2098 static usbd_status
   2099 xhci_root_ctrl_start(usbd_xfer_handle xfer)
   2100 {
   2101 	struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   2102 	usb_port_status_t ps;
   2103 	usb_device_request_t *req;
   2104 	void *buf = NULL;
   2105 	usb_hub_descriptor_t hubd;
   2106 	usbd_status err;
   2107 	int len, value, index;
   2108 	int l, totlen = 0;
   2109 	int port, i;
   2110 	uint32_t v;
   2111 
   2112 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2113 
   2114 	if (sc->sc_dying)
   2115 		return USBD_IOERROR;
   2116 
   2117 	req = &xfer->ux_request;
   2118 
   2119 	value = UGETW(req->wValue);
   2120 	index = UGETW(req->wIndex);
   2121 	len = UGETW(req->wLength);
   2122 
   2123 	if (len != 0)
   2124 		buf = xfer->ux_buf;
   2125 
   2126 	DPRINTFN(12, "rhreq: %04x %04x %04x %04x",
   2127 	    req->bmRequestType | (req->bRequest << 8), value, index, len);
   2128 
   2129 #define C(x,y) ((x) | ((y) << 8))
   2130 	switch(C(req->bRequest, req->bmRequestType)) {
   2131 	case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
   2132 	case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
   2133 	case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
   2134 		/*
   2135 		 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
   2136 		 * for the integrated root hub.
   2137 		 */
   2138 		break;
   2139 	case C(UR_GET_CONFIG, UT_READ_DEVICE):
   2140 		if (len > 0) {
   2141 			*(uint8_t *)buf = sc->sc_conf;
   2142 			totlen = 1;
   2143 		}
   2144 		break;
   2145 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
   2146 		DPRINTFN(8, "getdesc: wValue=0x%04x", value, 0, 0, 0);
   2147 		if (len == 0)
   2148 			break;
   2149 		switch(value >> 8) {
   2150 		case UDESC_DEVICE:
   2151 			if ((value & 0xff) != 0) {
   2152 				err = USBD_IOERROR;
   2153 				goto ret;
   2154 			}
   2155 			totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
   2156 			memcpy(buf, &xhci_devd, min(l, sizeof(xhci_devd)));
   2157 			break;
   2158 		case UDESC_DEVICE_QUALIFIER:
   2159 			if ((value & 0xff) != 0) {
   2160 			}
   2161 			totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
   2162 			memcpy(buf, &xhci_odevd, min(l, sizeof(xhci_odevd)));
   2163 			break;
   2164 		case UDESC_OTHER_SPEED_CONFIGURATION:
   2165 		case UDESC_CONFIG:
   2166 			if ((value & 0xff) != 0) {
   2167 				err = USBD_IOERROR;
   2168 				goto ret;
   2169 			}
   2170 			totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
   2171 			memcpy(buf, &xhci_confd, min(l, sizeof(xhci_confd)));
   2172 			((usb_config_descriptor_t *)buf)->bDescriptorType =
   2173 			    value >> 8;
   2174 			buf = (char *)buf + l;
   2175 			len -= l;
   2176 			l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
   2177 			totlen += l;
   2178 			memcpy(buf, &xhci_ifcd, min(l, sizeof(xhci_ifcd)));
   2179 			buf = (char *)buf + l;
   2180 			len -= l;
   2181 			l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
   2182 			totlen += l;
   2183 			memcpy(buf, &xhci_endpd, min(l, sizeof(xhci_endpd)));
   2184 			break;
   2185 		case UDESC_STRING:
   2186 #define sd ((usb_string_descriptor_t *)buf)
   2187 			switch (value & 0xff) {
   2188 			case 0: /* Language table */
   2189 				totlen = usb_makelangtbl(sd, len);
   2190 				break;
   2191 			case 1: /* Vendor */
   2192 				totlen = usb_makestrdesc(sd, len, "NetBSD");
   2193 				break;
   2194 			case 2: /* Product */
   2195 				totlen = usb_makestrdesc(sd, len,
   2196 				    "xHCI Root Hub");
   2197 				break;
   2198 			}
   2199 #undef sd
   2200 			break;
   2201 		default:
   2202 			err = USBD_IOERROR;
   2203 			goto ret;
   2204 		}
   2205 		break;
   2206 	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
   2207 		if (len > 0) {
   2208 			*(uint8_t *)buf = 0;
   2209 			totlen = 1;
   2210 		}
   2211 		break;
   2212 	case C(UR_GET_STATUS, UT_READ_DEVICE):
   2213 		if (len > 1) {
   2214 			USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
   2215 			totlen = 2;
   2216 		}
   2217 		break;
   2218 	case C(UR_GET_STATUS, UT_READ_INTERFACE):
   2219 	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
   2220 		if (len > 1) {
   2221 			USETW(((usb_status_t *)buf)->wStatus, 0);
   2222 			totlen = 2;
   2223 		}
   2224 		break;
   2225 	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
   2226 		if (value >= USB_MAX_DEVICES) {
   2227 			err = USBD_IOERROR;
   2228 			goto ret;
   2229 		}
   2230 		//sc->sc_addr = value;
   2231 		break;
   2232 	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
   2233 		if (value != 0 && value != 1) {
   2234 			err = USBD_IOERROR;
   2235 			goto ret;
   2236 		}
   2237 		sc->sc_conf = value;
   2238 		break;
   2239 	case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
   2240 		break;
   2241 	case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
   2242 	case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
   2243 	case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
   2244 		err = USBD_IOERROR;
   2245 		goto ret;
   2246 	case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
   2247 		break;
   2248 	case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
   2249 		break;
   2250 	/* Hub requests */
   2251 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
   2252 		break;
   2253 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
   2254 		DPRINTFN(4, "UR_CLEAR_PORT_FEATURE port=%d feature=%d",
   2255 			     index, value, 0, 0);
   2256 		if (index < 1 || index > sc->sc_hs_port_count) {
   2257 			err = USBD_IOERROR;
   2258 			goto ret;
   2259 		}
   2260 		port = XHCI_PORTSC(sc->sc_hs_port_start - 1 + index);
   2261 		v = xhci_op_read_4(sc, port);
   2262 		DPRINTFN(4, "portsc=0x%08x", v, 0, 0, 0);
   2263 		v &= ~XHCI_PS_CLEAR;
   2264 		switch (value) {
   2265 		case UHF_PORT_ENABLE:
   2266 			xhci_op_write_4(sc, port, v &~ XHCI_PS_PED);
   2267 			break;
   2268 		case UHF_PORT_SUSPEND:
   2269 			err = USBD_IOERROR;
   2270 			goto ret;
   2271 		case UHF_PORT_POWER:
   2272 			break;
   2273 		case UHF_PORT_TEST:
   2274 		case UHF_PORT_INDICATOR:
   2275 			err = USBD_IOERROR;
   2276 			goto ret;
   2277 		case UHF_C_PORT_CONNECTION:
   2278 			xhci_op_write_4(sc, port, v | XHCI_PS_CSC);
   2279 			break;
   2280 		case UHF_C_PORT_ENABLE:
   2281 		case UHF_C_PORT_SUSPEND:
   2282 		case UHF_C_PORT_OVER_CURRENT:
   2283 			err = USBD_IOERROR;
   2284 			goto ret;
   2285 		case UHF_C_PORT_RESET:
   2286 			xhci_op_write_4(sc, port, v | XHCI_PS_PRC);
   2287 			break;
   2288 		default:
   2289 			err = USBD_IOERROR;
   2290 			goto ret;
   2291 		}
   2292 
   2293 		break;
   2294 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
   2295 		if (len == 0)
   2296 			break;
   2297 		if ((value & 0xff) != 0) {
   2298 			err = USBD_IOERROR;
   2299 			goto ret;
   2300 		}
   2301 		hubd = xhci_hubd;
   2302 		hubd.bNbrPorts = sc->sc_hs_port_count;
   2303 		USETW(hubd.wHubCharacteristics, UHD_PWR_NO_SWITCH);
   2304 		hubd.bPwrOn2PwrGood = 200;
   2305 		for (i = 0, l = sc->sc_maxports; l > 0; i++, l -= 8)
   2306 			hubd.DeviceRemovable[i++] = 0; /* XXX can't find out? */
   2307 		hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
   2308 		l = min(len, hubd.bDescLength);
   2309 		totlen = l;
   2310 		memcpy(buf, &hubd, l);
   2311 		break;
   2312 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
   2313 		if (len != 4) {
   2314 			err = USBD_IOERROR;
   2315 			goto ret;
   2316 		}
   2317 		memset(buf, 0, len); /* ? XXX */
   2318 		totlen = len;
   2319 		break;
   2320 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
   2321 		DPRINTFN(8, "get port status i=%d", index, 0, 0, 0);
   2322 		if (index < 1 || index > sc->sc_maxports) {
   2323 			err = USBD_IOERROR;
   2324 			goto ret;
   2325 		}
   2326 		if (len != 4) {
   2327 			err = USBD_IOERROR;
   2328 			goto ret;
   2329 		}
   2330 		v = xhci_op_read_4(sc, XHCI_PORTSC(sc->sc_hs_port_start - 1 +
   2331 		    index));
   2332 		DPRINTFN(4, "READ_CLASS_OTHER GET_STATUS PORTSC %d (%d) %08x",
   2333 		    index, sc->sc_hs_port_start - 1 + index, v, 0);
   2334 		switch (XHCI_PS_SPEED_GET(v)) {
   2335 		case 1:
   2336 			i = UPS_FULL_SPEED;
   2337 			break;
   2338 		case 2:
   2339 			i = UPS_LOW_SPEED;
   2340 			break;
   2341 		case 3:
   2342 			i = UPS_HIGH_SPEED;
   2343 			break;
   2344 		default:
   2345 			i = 0;
   2346 			break;
   2347 		}
   2348 		if (v & XHCI_PS_CCS)	i |= UPS_CURRENT_CONNECT_STATUS;
   2349 		if (v & XHCI_PS_PED)	i |= UPS_PORT_ENABLED;
   2350 		if (v & XHCI_PS_OCA)	i |= UPS_OVERCURRENT_INDICATOR;
   2351 		//if (v & XHCI_PS_SUSP)	i |= UPS_SUSPEND;
   2352 		if (v & XHCI_PS_PR)	i |= UPS_RESET;
   2353 		if (v & XHCI_PS_PP)	i |= UPS_PORT_POWER;
   2354 		USETW(ps.wPortStatus, i);
   2355 		i = 0;
   2356 		if (v & XHCI_PS_CSC)    i |= UPS_C_CONNECT_STATUS;
   2357 		if (v & XHCI_PS_PEC)    i |= UPS_C_PORT_ENABLED;
   2358 		if (v & XHCI_PS_OCC)    i |= UPS_C_OVERCURRENT_INDICATOR;
   2359 		if (v & XHCI_PS_PRC)	i |= UPS_C_PORT_RESET;
   2360 		USETW(ps.wPortChange, i);
   2361 		l = min(len, sizeof ps);
   2362 		memcpy(buf, &ps, l);
   2363 		totlen = l;
   2364 		break;
   2365 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
   2366 		err = USBD_IOERROR;
   2367 		goto ret;
   2368 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
   2369 		break;
   2370 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
   2371 		if (index < 1 || index > sc->sc_hs_port_count) {
   2372 			err = USBD_IOERROR;
   2373 			goto ret;
   2374 		}
   2375 		port = XHCI_PORTSC(sc->sc_hs_port_start - 1 + index);
   2376 		v = xhci_op_read_4(sc, port);
   2377 		DPRINTFN(4, "portsc=0x%08x", v, 0, 0, 0);
   2378 		v &= ~XHCI_PS_CLEAR;
   2379 		switch (value) {
   2380 		case UHF_PORT_ENABLE:
   2381 			xhci_op_write_4(sc, port, v | XHCI_PS_PED);
   2382 			break;
   2383 		case UHF_PORT_SUSPEND:
   2384 			/* XXX suspend */
   2385 			break;
   2386 		case UHF_PORT_RESET:
   2387 			v &= ~ (XHCI_PS_PED | XHCI_PS_PR);
   2388 			xhci_op_write_4(sc, port, v | XHCI_PS_PR);
   2389 			/* Wait for reset to complete. */
   2390 			usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
   2391 			if (sc->sc_dying) {
   2392 				err = USBD_IOERROR;
   2393 				goto ret;
   2394 			}
   2395 			v = xhci_op_read_4(sc, port);
   2396 			if (v & XHCI_PS_PR) {
   2397 				xhci_op_write_4(sc, port, v & ~XHCI_PS_PR);
   2398 				usb_delay_ms(&sc->sc_bus, 10);
   2399 				/* XXX */
   2400 			}
   2401 			break;
   2402 		case UHF_PORT_POWER:
   2403 			/* XXX power control */
   2404 			break;
   2405 		/* XXX more */
   2406 		case UHF_C_PORT_RESET:
   2407 			xhci_op_write_4(sc, port, v | XHCI_PS_PRC);
   2408 			break;
   2409 		default:
   2410 			err = USBD_IOERROR;
   2411 			goto ret;
   2412 		}
   2413 		break;
   2414 	case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
   2415 	case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
   2416 	case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
   2417 	case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
   2418 		break;
   2419 	default:
   2420 		err = USBD_IOERROR;
   2421 		goto ret;
   2422 	}
   2423 	xfer->ux_actlen = totlen;
   2424 	err = USBD_NORMAL_COMPLETION;
   2425 ret:
   2426 	xfer->ux_status = err;
   2427 	mutex_enter(&sc->sc_lock);
   2428 	usb_transfer_complete(xfer);
   2429 	mutex_exit(&sc->sc_lock);
   2430 	return USBD_IN_PROGRESS;
   2431 }
   2432 
   2433 
   2434 static void
   2435 xhci_root_ctrl_abort(usbd_xfer_handle xfer)
   2436 {
   2437 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2438 	/* Nothing to do, all transfers are synchronous. */
   2439 }
   2440 
   2441 
   2442 static void
   2443 xhci_root_ctrl_close(usbd_pipe_handle pipe)
   2444 {
   2445 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2446 	/* Nothing to do. */
   2447 }
   2448 
   2449 static void
   2450 xhci_root_ctrl_done(usbd_xfer_handle xfer)
   2451 {
   2452 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2453 
   2454 	xfer->ux_hcpriv = NULL;
   2455 }
   2456 
   2457 /* root hub interrupt */
   2458 
   2459 static usbd_status
   2460 xhci_root_intr_transfer(usbd_xfer_handle xfer)
   2461 {
   2462 	struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   2463 	usbd_status err;
   2464 
   2465 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2466 
   2467 	/* Insert last in queue. */
   2468 	mutex_enter(&sc->sc_lock);
   2469 	err = usb_insert_transfer(xfer);
   2470 	mutex_exit(&sc->sc_lock);
   2471 	if (err)
   2472 		return err;
   2473 
   2474 	/* Pipe isn't running, start first */
   2475 	return (xhci_root_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue)));
   2476 }
   2477 
   2478 static usbd_status
   2479 xhci_root_intr_start(usbd_xfer_handle xfer)
   2480 {
   2481 	struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   2482 
   2483 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2484 
   2485 	if (sc->sc_dying)
   2486 		return USBD_IOERROR;
   2487 
   2488 	mutex_enter(&sc->sc_lock);
   2489 	sc->sc_intrxfer = xfer;
   2490 	mutex_exit(&sc->sc_lock);
   2491 
   2492 	return USBD_IN_PROGRESS;
   2493 }
   2494 
   2495 static void
   2496 xhci_root_intr_abort(usbd_xfer_handle xfer)
   2497 {
   2498 	struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   2499 
   2500 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2501 
   2502 	KASSERT(mutex_owned(&sc->sc_lock));
   2503 	KASSERT(xfer->ux_pipe->up_intrxfer == xfer);
   2504 
   2505 	DPRINTFN(1, "remove", 0, 0, 0, 0);
   2506 
   2507 	sc->sc_intrxfer = NULL;
   2508 
   2509 	xfer->ux_status = USBD_CANCELLED;
   2510 	usb_transfer_complete(xfer);
   2511 }
   2512 
   2513 static void
   2514 xhci_root_intr_close(usbd_pipe_handle pipe)
   2515 {
   2516 	struct xhci_softc * const sc = pipe->up_dev->ud_bus->ub_hcpriv;
   2517 
   2518 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2519 
   2520 	KASSERT(mutex_owned(&sc->sc_lock));
   2521 
   2522 	sc->sc_intrxfer = NULL;
   2523 }
   2524 
   2525 static void
   2526 xhci_root_intr_done(usbd_xfer_handle xfer)
   2527 {
   2528 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2529 
   2530 	xfer->ux_hcpriv = NULL;
   2531 }
   2532 
   2533 /* -------------- */
   2534 /* device control */
   2535 
   2536 static usbd_status
   2537 xhci_device_ctrl_transfer(usbd_xfer_handle xfer)
   2538 {
   2539 	struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   2540 	usbd_status err;
   2541 
   2542 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2543 
   2544 	/* Insert last in queue. */
   2545 	mutex_enter(&sc->sc_lock);
   2546 	err = usb_insert_transfer(xfer);
   2547 	mutex_exit(&sc->sc_lock);
   2548 	if (err)
   2549 		return (err);
   2550 
   2551 	/* Pipe isn't running, start first */
   2552 	return (xhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue)));
   2553 }
   2554 
   2555 static usbd_status
   2556 xhci_device_ctrl_start(usbd_xfer_handle xfer)
   2557 {
   2558 	struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   2559 	struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
   2560 	const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
   2561 	struct xhci_ring * const tr = &xs->xs_ep[dci].xe_tr;
   2562 	struct xhci_xfer * const xx = (void *)xfer;
   2563 	usb_device_request_t * const req = &xfer->ux_request;
   2564 	const bool isread = UT_GET_DIR(req->bmRequestType) == UT_READ;
   2565 	const uint32_t len = UGETW(req->wLength);
   2566 	usb_dma_t * const dma = &xfer->ux_dmabuf;
   2567 	uint64_t parameter;
   2568 	uint32_t status;
   2569 	uint32_t control;
   2570 	u_int i;
   2571 
   2572 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2573 	DPRINTFN(12, "req: %04x %04x %04x %04x",
   2574 	    req->bmRequestType | (req->bRequest << 8), UGETW(req->wValue),
   2575 	    UGETW(req->wIndex), UGETW(req->wLength));
   2576 
   2577 	/* XXX */
   2578 	if (tr->is_halted) {
   2579 		xhci_reset_endpoint(xfer->ux_pipe);
   2580 		tr->is_halted = false;
   2581 		xhci_set_dequeue(xfer->ux_pipe);
   2582 	}
   2583 
   2584 	/* we rely on the bottom bits for extra info */
   2585 	KASSERT(((uintptr_t)xfer & 0x3) == 0x0);
   2586 
   2587 	KASSERT((xfer->ux_rqflags & URQ_REQUEST) != 0);
   2588 
   2589 	i = 0;
   2590 
   2591 	/* setup phase */
   2592 	memcpy(&parameter, req, sizeof(*req));
   2593 	parameter = le64toh(parameter);
   2594 	status = XHCI_TRB_2_IRQ_SET(0) | XHCI_TRB_2_BYTES_SET(sizeof(*req));
   2595 	control = ((len == 0) ? XHCI_TRB_3_TRT_NONE :
   2596 	     (isread ? XHCI_TRB_3_TRT_IN : XHCI_TRB_3_TRT_OUT)) |
   2597 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SETUP_STAGE) |
   2598 	    XHCI_TRB_3_IDT_BIT;
   2599 	xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
   2600 
   2601 	if (len == 0)
   2602 		goto no_data;
   2603 
   2604 	/* data phase */
   2605 	parameter = DMAADDR(dma, 0);
   2606 	KASSERT(len <= 0x10000);
   2607 	status = XHCI_TRB_2_IRQ_SET(0) |
   2608 	    XHCI_TRB_2_TDSZ_SET(1) |
   2609 	    XHCI_TRB_2_BYTES_SET(len);
   2610 	control = (isread ? XHCI_TRB_3_DIR_IN : 0) |
   2611 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DATA_STAGE) |
   2612 	    XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_ENT_BIT;
   2613 	xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
   2614 
   2615 	parameter = (uintptr_t)xfer | 0x3;
   2616 	status = XHCI_TRB_2_IRQ_SET(0);
   2617 	control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVENT_DATA) |
   2618 	    XHCI_TRB_3_IOC_BIT;
   2619 	xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
   2620 
   2621 no_data:
   2622 	parameter = 0;
   2623 	status = XHCI_TRB_2_IRQ_SET(0);
   2624 	/* the status stage has inverted direction */
   2625 	control = ((isread && (len > 0)) ? 0 : XHCI_TRB_3_DIR_IN) |
   2626 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STATUS_STAGE) |
   2627 	    XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_ENT_BIT;
   2628 	xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
   2629 
   2630 	parameter = (uintptr_t)xfer | 0x0;
   2631 	status = XHCI_TRB_2_IRQ_SET(0);
   2632 	control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVENT_DATA) |
   2633 	    XHCI_TRB_3_IOC_BIT;
   2634 	xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
   2635 
   2636 	mutex_enter(&tr->xr_lock);
   2637 	xhci_ring_put(sc, tr, xfer, xx->xx_trb, i);
   2638 	mutex_exit(&tr->xr_lock);
   2639 
   2640 	xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci);
   2641 
   2642 	if (xfer->ux_timeout && !sc->sc_bus.ub_usepolling) {
   2643 		callout_reset(&xfer->ux_callout, mstohz(xfer->ux_timeout),
   2644 		    xhci_timeout, xfer);
   2645 	}
   2646 
   2647 	if (sc->sc_bus.ub_usepolling) {
   2648 		DPRINTFN(1, "polling", 0, 0, 0, 0);
   2649 		//xhci_waitintr(sc, xfer);
   2650 	}
   2651 
   2652 	return USBD_IN_PROGRESS;
   2653 }
   2654 
   2655 static void
   2656 xhci_device_ctrl_done(usbd_xfer_handle xfer)
   2657 {
   2658 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2659 
   2660 	callout_stop(&xfer->ux_callout); /* XXX wrong place */
   2661 
   2662 }
   2663 
   2664 static void
   2665 xhci_device_ctrl_abort(usbd_xfer_handle xfer)
   2666 {
   2667 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2668 }
   2669 
   2670 static void
   2671 xhci_device_ctrl_close(usbd_pipe_handle pipe)
   2672 {
   2673 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2674 }
   2675 
   2676 /* ----------------- */
   2677 /* device isochronus */
   2678 
   2679 /* ----------- */
   2680 /* device bulk */
   2681 
   2682 static usbd_status
   2683 xhci_device_bulk_transfer(usbd_xfer_handle xfer)
   2684 {
   2685 	struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   2686 	usbd_status err;
   2687 
   2688 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2689 
   2690 	/* Insert last in queue. */
   2691 	mutex_enter(&sc->sc_lock);
   2692 	err = usb_insert_transfer(xfer);
   2693 	mutex_exit(&sc->sc_lock);
   2694 	if (err)
   2695 		return err;
   2696 
   2697 	/*
   2698 	 * Pipe isn't running (otherwise err would be USBD_INPROG),
   2699 	 * so start it first.
   2700 	 */
   2701 	return (xhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue)));
   2702 }
   2703 
   2704 static usbd_status
   2705 xhci_device_bulk_start(usbd_xfer_handle xfer)
   2706 {
   2707 	struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   2708 	struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
   2709 	const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
   2710 	struct xhci_ring * const tr = &xs->xs_ep[dci].xe_tr;
   2711 	struct xhci_xfer * const xx = (void *)xfer;
   2712 	const uint32_t len = xfer->ux_length;
   2713 	usb_dma_t * const dma = &xfer->ux_dmabuf;
   2714 	uint64_t parameter;
   2715 	uint32_t status;
   2716 	uint32_t control;
   2717 	u_int i = 0;
   2718 
   2719 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2720 
   2721 	DPRINTFN(15, "%p slot %u dci %u", xfer, xs->xs_idx, dci, 0);
   2722 
   2723 	if (sc->sc_dying)
   2724 		return USBD_IOERROR;
   2725 
   2726 	KASSERT((xfer->ux_rqflags & URQ_REQUEST) == 0);
   2727 
   2728 	parameter = DMAADDR(dma, 0);
   2729 	/*
   2730 	 * XXX: (dsl) The physical buffer must not cross a 64k boundary.
   2731 	 * If the user supplied buffer crosses such a boundary then 2
   2732 	 * (or more) TRB should be used.
   2733 	 * If multiple TRB are used the td_size field must be set correctly.
   2734 	 * For v1.0 devices (like ivy bridge) this is the number of usb data
   2735 	 * blocks needed to complete the transfer.
   2736 	 * Setting it to 1 in the last TRB causes an extra zero-length
   2737 	 * data block be sent.
   2738 	 * The earlier documentation differs, I don't know how it behaves.
   2739 	 */
   2740 	KASSERT(len <= 0x10000);
   2741 	status = XHCI_TRB_2_IRQ_SET(0) |
   2742 	    XHCI_TRB_2_TDSZ_SET(1) |
   2743 	    XHCI_TRB_2_BYTES_SET(len);
   2744 	control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL) |
   2745 	    XHCI_TRB_3_ISP_BIT | XHCI_TRB_3_IOC_BIT;
   2746 	xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
   2747 
   2748 	mutex_enter(&tr->xr_lock);
   2749 	xhci_ring_put(sc, tr, xfer, xx->xx_trb, i);
   2750 	mutex_exit(&tr->xr_lock);
   2751 
   2752 	xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci);
   2753 
   2754 	if (sc->sc_bus.ub_usepolling) {
   2755 		DPRINTFN(1, "polling", 0, 0, 0, 0);
   2756 		//xhci_waitintr(sc, xfer);
   2757 	}
   2758 
   2759 	return USBD_IN_PROGRESS;
   2760 }
   2761 
   2762 static void
   2763 xhci_device_bulk_done(usbd_xfer_handle xfer)
   2764 {
   2765 #ifdef USB_DEBUG
   2766 	struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
   2767 	const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
   2768 #endif
   2769 	const u_int endpt = xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress;
   2770 	const bool isread = UE_GET_DIR(endpt) == UE_DIR_IN;
   2771 
   2772 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2773 
   2774 	DPRINTFN(15, "%p slot %u dci %u", xfer, xs->xs_idx, dci, 0);
   2775 
   2776 	callout_stop(&xfer->ux_callout); /* XXX wrong place */
   2777 
   2778 	usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
   2779 	    isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   2780 
   2781 
   2782 }
   2783 
   2784 static void
   2785 xhci_device_bulk_abort(usbd_xfer_handle xfer)
   2786 {
   2787 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2788 }
   2789 
   2790 static void
   2791 xhci_device_bulk_close(usbd_pipe_handle pipe)
   2792 {
   2793 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2794 }
   2795 
   2796 /* --------------- */
   2797 /* device intrrupt */
   2798 
   2799 static usbd_status
   2800 xhci_device_intr_transfer(usbd_xfer_handle xfer)
   2801 {
   2802 	struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   2803 	usbd_status err;
   2804 
   2805 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2806 
   2807 	/* Insert last in queue. */
   2808 	mutex_enter(&sc->sc_lock);
   2809 	err = usb_insert_transfer(xfer);
   2810 	mutex_exit(&sc->sc_lock);
   2811 	if (err)
   2812 		return err;
   2813 
   2814 	/*
   2815 	 * Pipe isn't running (otherwise err would be USBD_INPROG),
   2816 	 * so start it first.
   2817 	 */
   2818 	return (xhci_device_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue)));
   2819 }
   2820 
   2821 static usbd_status
   2822 xhci_device_intr_start(usbd_xfer_handle xfer)
   2823 {
   2824 	struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   2825 	struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
   2826 	const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
   2827 	struct xhci_ring * const tr = &xs->xs_ep[dci].xe_tr;
   2828 	struct xhci_xfer * const xx = (void *)xfer;
   2829 	const uint32_t len = xfer->ux_length;
   2830 	usb_dma_t * const dma = &xfer->ux_dmabuf;
   2831 	uint64_t parameter;
   2832 	uint32_t status;
   2833 	uint32_t control;
   2834 	u_int i = 0;
   2835 
   2836 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2837 
   2838 	DPRINTFN(15, "%p slot %u dci %u", xfer, xs->xs_idx, dci, 0);
   2839 
   2840 	if (sc->sc_dying)
   2841 		return USBD_IOERROR;
   2842 
   2843 	KASSERT((xfer->ux_rqflags & URQ_REQUEST) == 0);
   2844 
   2845 	parameter = DMAADDR(dma, 0);
   2846 	KASSERT(len <= 0x10000);
   2847 	status = XHCI_TRB_2_IRQ_SET(0) |
   2848 	    XHCI_TRB_2_TDSZ_SET(1) |
   2849 	    XHCI_TRB_2_BYTES_SET(len);
   2850 	control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL) |
   2851 	    XHCI_TRB_3_ISP_BIT | XHCI_TRB_3_IOC_BIT;
   2852 	xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
   2853 
   2854 	mutex_enter(&tr->xr_lock);
   2855 	xhci_ring_put(sc, tr, xfer, xx->xx_trb, i);
   2856 	mutex_exit(&tr->xr_lock);
   2857 
   2858 	xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci);
   2859 
   2860 	if (sc->sc_bus.ub_usepolling) {
   2861 		DPRINTFN(1, "polling", 0, 0, 0, 0);
   2862 		//xhci_waitintr(sc, xfer);
   2863 	}
   2864 
   2865 	return USBD_IN_PROGRESS;
   2866 }
   2867 
   2868 static void
   2869 xhci_device_intr_done(usbd_xfer_handle xfer)
   2870 {
   2871 	struct xhci_softc * const sc __diagused =
   2872 		xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   2873 #ifdef USB_DEBUG
   2874 	struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
   2875 	const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
   2876 #endif
   2877 	const u_int endpt = xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress;
   2878 	const bool isread = UE_GET_DIR(endpt) == UE_DIR_IN;
   2879 
   2880 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2881 
   2882 	DPRINTFN(15, "%p slot %u dci %u", xfer, xs->xs_idx, dci, 0);
   2883 
   2884 	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
   2885 
   2886 	usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
   2887 	    isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   2888 
   2889 #if 0
   2890 	device_printf(sc->sc_dev, "");
   2891 	for (size_t i = 0; i < xfer->ux_length; i++) {
   2892 		printf(" %02x", ((uint8_t const *)xfer->ux_buffer)[i]);
   2893 	}
   2894 	printf("\n");
   2895 #endif
   2896 
   2897 	if (xfer->ux_pipe->up_repeat) {
   2898 		xfer->ux_status = xhci_device_intr_start(xfer);
   2899 	} else {
   2900 		callout_stop(&xfer->ux_callout); /* XXX */
   2901 	}
   2902 
   2903 }
   2904 
   2905 static void
   2906 xhci_device_intr_abort(usbd_xfer_handle xfer)
   2907 {
   2908 	struct xhci_softc * const sc __diagused =
   2909 				    xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   2910 
   2911 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2912 
   2913 	KASSERT(mutex_owned(&sc->sc_lock));
   2914 	DPRINTFN(15, "%p", xfer, 0, 0, 0);
   2915 	KASSERT(xfer->ux_pipe->up_intrxfer == xfer);
   2916 	xfer->ux_status = USBD_CANCELLED;
   2917 	usb_transfer_complete(xfer);
   2918 }
   2919 
   2920 static void
   2921 xhci_device_intr_close(usbd_pipe_handle pipe)
   2922 {
   2923 	//struct xhci_softc * const sc = pipe->up_dev->ud_bus->ub_hcpriv;
   2924 
   2925 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2926 	DPRINTFN(15, "%p", pipe, 0, 0, 0);
   2927 
   2928 	xhci_unconfigure_endpoint(pipe);
   2929 }
   2930 
   2931 /* ------------ */
   2932 
   2933 static void
   2934 xhci_timeout(void *addr)
   2935 {
   2936 	struct xhci_xfer * const xx = addr;
   2937 	usbd_xfer_handle const xfer = &xx->xx_xfer;
   2938 	struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   2939 
   2940 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2941 
   2942 	if (sc->sc_dying) {
   2943 		return;
   2944 	}
   2945 
   2946 	usb_init_task(&xx->xx_abort_task, xhci_timeout_task, addr,
   2947 	    USB_TASKQ_MPSAFE);
   2948 	usb_add_task(xx->xx_xfer.ux_pipe->up_dev, &xx->xx_abort_task,
   2949 	    USB_TASKQ_HC);
   2950 }
   2951 
   2952 static void
   2953 xhci_timeout_task(void *addr)
   2954 {
   2955 	usbd_xfer_handle const xfer = addr;
   2956 	struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   2957 
   2958 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
   2959 
   2960 	mutex_enter(&sc->sc_lock);
   2961 #if 0
   2962 	xhci_abort_xfer(xfer, USBD_TIMEOUT);
   2963 #else
   2964 	xfer->ux_status = USBD_TIMEOUT;
   2965 	usb_transfer_complete(xfer);
   2966 #endif
   2967 	mutex_exit(&sc->sc_lock);
   2968 }
   2969