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