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