Sat May 29 16:49:30 2021 UTC ()
xhci(4): Wait USB_RESUME_WAIT ms, not 20 ms.

Better to use the named constant, and although the spec says 20 ms is
enough, apparently for some devices it's not.


(riastradh)
diff -r1.142 -r1.143 src/sys/dev/usb/xhci.c

cvs diff -r1.142 -r1.143 src/sys/dev/usb/xhci.c (switch to unified diff)

--- src/sys/dev/usb/xhci.c 2021/05/27 11:09:15 1.142
+++ src/sys/dev/usb/xhci.c 2021/05/29 16:49:30 1.143
@@ -1,2029 +1,2029 @@ @@ -1,2029 +1,2029 @@
1/* $NetBSD: xhci.c,v 1.142 2021/05/27 11:09:15 skrll Exp $ */ 1/* $NetBSD: xhci.c,v 1.143 2021/05/29 16:49:30 riastradh Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2013 Jonathan A. Kollasch 4 * Copyright (c) 2013 Jonathan A. Kollasch
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
15 * 15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29/* 29/*
30 * USB rev 2.0 and rev 3.1 specification 30 * USB rev 2.0 and rev 3.1 specification
31 * http://www.usb.org/developers/docs/ 31 * http://www.usb.org/developers/docs/
32 * xHCI rev 1.1 specification 32 * xHCI rev 1.1 specification
33 * http://www.intel.com/technology/usb/spec.htm 33 * http://www.intel.com/technology/usb/spec.htm
34 */ 34 */
35 35
36#include <sys/cdefs.h> 36#include <sys/cdefs.h>
37__KERNEL_RCSID(0, "$NetBSD: xhci.c,v 1.142 2021/05/27 11:09:15 skrll Exp $"); 37__KERNEL_RCSID(0, "$NetBSD: xhci.c,v 1.143 2021/05/29 16:49:30 riastradh Exp $");
38 38
39#ifdef _KERNEL_OPT 39#ifdef _KERNEL_OPT
40#include "opt_usb.h" 40#include "opt_usb.h"
41#endif 41#endif
42 42
43#include <sys/param.h> 43#include <sys/param.h>
44#include <sys/systm.h> 44#include <sys/systm.h>
45#include <sys/kernel.h> 45#include <sys/kernel.h>
46#include <sys/kmem.h> 46#include <sys/kmem.h>
47#include <sys/device.h> 47#include <sys/device.h>
48#include <sys/select.h> 48#include <sys/select.h>
49#include <sys/proc.h> 49#include <sys/proc.h>
50#include <sys/queue.h> 50#include <sys/queue.h>
51#include <sys/mutex.h> 51#include <sys/mutex.h>
52#include <sys/condvar.h> 52#include <sys/condvar.h>
53#include <sys/bus.h> 53#include <sys/bus.h>
54#include <sys/cpu.h> 54#include <sys/cpu.h>
55#include <sys/sysctl.h> 55#include <sys/sysctl.h>
56 56
57#include <machine/endian.h> 57#include <machine/endian.h>
58 58
59#include <dev/usb/usb.h> 59#include <dev/usb/usb.h>
60#include <dev/usb/usbdi.h> 60#include <dev/usb/usbdi.h>
61#include <dev/usb/usbdivar.h> 61#include <dev/usb/usbdivar.h>
62#include <dev/usb/usbdi_util.h> 62#include <dev/usb/usbdi_util.h>
63#include <dev/usb/usbhist.h> 63#include <dev/usb/usbhist.h>
64#include <dev/usb/usb_mem.h> 64#include <dev/usb/usb_mem.h>
65#include <dev/usb/usb_quirks.h> 65#include <dev/usb/usb_quirks.h>
66 66
67#include <dev/usb/xhcireg.h> 67#include <dev/usb/xhcireg.h>
68#include <dev/usb/xhcivar.h> 68#include <dev/usb/xhcivar.h>
69#include <dev/usb/usbroothub.h> 69#include <dev/usb/usbroothub.h>
70 70
71 71
72#ifdef USB_DEBUG 72#ifdef USB_DEBUG
73#ifndef XHCI_DEBUG 73#ifndef XHCI_DEBUG
74#define xhcidebug 0 74#define xhcidebug 0
75#else /* !XHCI_DEBUG */ 75#else /* !XHCI_DEBUG */
76#define HEXDUMP(a, b, c) \ 76#define HEXDUMP(a, b, c) \
77 do { \ 77 do { \
78 if (xhcidebug > 0) \ 78 if (xhcidebug > 0) \
79 hexdump(printf, a, b, c); \ 79 hexdump(printf, a, b, c); \
80 } while (/*CONSTCOND*/0) 80 } while (/*CONSTCOND*/0)
81static int xhcidebug = 0; 81static int xhcidebug = 0;
82 82
83SYSCTL_SETUP(sysctl_hw_xhci_setup, "sysctl hw.xhci setup") 83SYSCTL_SETUP(sysctl_hw_xhci_setup, "sysctl hw.xhci setup")
84{ 84{
85 int err; 85 int err;
86 const struct sysctlnode *rnode; 86 const struct sysctlnode *rnode;
87 const struct sysctlnode *cnode; 87 const struct sysctlnode *cnode;
88 88
89 err = sysctl_createv(clog, 0, NULL, &rnode, 89 err = sysctl_createv(clog, 0, NULL, &rnode,
90 CTLFLAG_PERMANENT, CTLTYPE_NODE, "xhci", 90 CTLFLAG_PERMANENT, CTLTYPE_NODE, "xhci",
91 SYSCTL_DESCR("xhci global controls"), 91 SYSCTL_DESCR("xhci global controls"),
92 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL); 92 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
93 93
94 if (err) 94 if (err)
95 goto fail; 95 goto fail;
96 96
97 /* control debugging printfs */ 97 /* control debugging printfs */
98 err = sysctl_createv(clog, 0, &rnode, &cnode, 98 err = sysctl_createv(clog, 0, &rnode, &cnode,
99 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, 99 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
100 "debug", SYSCTL_DESCR("Enable debugging output"), 100 "debug", SYSCTL_DESCR("Enable debugging output"),
101 NULL, 0, &xhcidebug, sizeof(xhcidebug), CTL_CREATE, CTL_EOL); 101 NULL, 0, &xhcidebug, sizeof(xhcidebug), CTL_CREATE, CTL_EOL);
102 if (err) 102 if (err)
103 goto fail; 103 goto fail;
104 104
105 return; 105 return;
106fail: 106fail:
107 aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err); 107 aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
108} 108}
109 109
110#endif /* !XHCI_DEBUG */ 110#endif /* !XHCI_DEBUG */
111#endif /* USB_DEBUG */ 111#endif /* USB_DEBUG */
112 112
113#ifndef HEXDUMP 113#ifndef HEXDUMP
114#define HEXDUMP(a, b, c) 114#define HEXDUMP(a, b, c)
115#endif 115#endif
116 116
117#define DPRINTF(FMT,A,B,C,D) USBHIST_LOG(xhcidebug,FMT,A,B,C,D) 117#define DPRINTF(FMT,A,B,C,D) USBHIST_LOG(xhcidebug,FMT,A,B,C,D)
118#define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(xhcidebug,N,FMT,A,B,C,D) 118#define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(xhcidebug,N,FMT,A,B,C,D)
119#define XHCIHIST_FUNC() USBHIST_FUNC() 119#define XHCIHIST_FUNC() USBHIST_FUNC()
120#define XHCIHIST_CALLED(name) USBHIST_CALLED(xhcidebug) 120#define XHCIHIST_CALLED(name) USBHIST_CALLED(xhcidebug)
121#define XHCIHIST_CALLARGS(FMT,A,B,C,D) \ 121#define XHCIHIST_CALLARGS(FMT,A,B,C,D) \
122 USBHIST_CALLARGS(xhcidebug,FMT,A,B,C,D) 122 USBHIST_CALLARGS(xhcidebug,FMT,A,B,C,D)
123 123
124#define XHCI_DCI_SLOT 0 124#define XHCI_DCI_SLOT 0
125#define XHCI_DCI_EP_CONTROL 1 125#define XHCI_DCI_EP_CONTROL 1
126 126
127#define XHCI_ICI_INPUT_CONTROL 0 127#define XHCI_ICI_INPUT_CONTROL 0
128 128
129struct xhci_pipe { 129struct xhci_pipe {
130 struct usbd_pipe xp_pipe; 130 struct usbd_pipe xp_pipe;
131 struct usb_task xp_async_task; 131 struct usb_task xp_async_task;
132 int16_t xp_isoc_next; /* next frame */ 132 int16_t xp_isoc_next; /* next frame */
133 uint8_t xp_maxb; /* max burst */ 133 uint8_t xp_maxb; /* max burst */
134 uint8_t xp_mult; 134 uint8_t xp_mult;
135}; 135};
136 136
137#define XHCI_COMMAND_RING_TRBS 256 137#define XHCI_COMMAND_RING_TRBS 256
138#define XHCI_EVENT_RING_TRBS 256 138#define XHCI_EVENT_RING_TRBS 256
139#define XHCI_EVENT_RING_SEGMENTS 1 139#define XHCI_EVENT_RING_SEGMENTS 1
140#define XHCI_TRB_3_ED_BIT XHCI_TRB_3_ISP_BIT 140#define XHCI_TRB_3_ED_BIT XHCI_TRB_3_ISP_BIT
141 141
142static usbd_status xhci_open(struct usbd_pipe *); 142static usbd_status xhci_open(struct usbd_pipe *);
143static void xhci_close_pipe(struct usbd_pipe *); 143static void xhci_close_pipe(struct usbd_pipe *);
144static int xhci_intr1(struct xhci_softc * const); 144static int xhci_intr1(struct xhci_softc * const);
145static void xhci_softintr(void *); 145static void xhci_softintr(void *);
146static void xhci_poll(struct usbd_bus *); 146static void xhci_poll(struct usbd_bus *);
147static struct usbd_xfer *xhci_allocx(struct usbd_bus *, unsigned int); 147static struct usbd_xfer *xhci_allocx(struct usbd_bus *, unsigned int);
148static void xhci_freex(struct usbd_bus *, struct usbd_xfer *); 148static void xhci_freex(struct usbd_bus *, struct usbd_xfer *);
149static void xhci_abortx(struct usbd_xfer *); 149static void xhci_abortx(struct usbd_xfer *);
150static bool xhci_dying(struct usbd_bus *); 150static bool xhci_dying(struct usbd_bus *);
151static void xhci_get_lock(struct usbd_bus *, kmutex_t **); 151static void xhci_get_lock(struct usbd_bus *, kmutex_t **);
152static usbd_status xhci_new_device(device_t, struct usbd_bus *, int, int, int, 152static usbd_status xhci_new_device(device_t, struct usbd_bus *, int, int, int,
153 struct usbd_port *); 153 struct usbd_port *);
154static int xhci_roothub_ctrl(struct usbd_bus *, usb_device_request_t *, 154static int xhci_roothub_ctrl(struct usbd_bus *, usb_device_request_t *,
155 void *, int); 155 void *, int);
156 156
157static usbd_status xhci_configure_endpoint(struct usbd_pipe *); 157static usbd_status xhci_configure_endpoint(struct usbd_pipe *);
158//static usbd_status xhci_unconfigure_endpoint(struct usbd_pipe *); 158//static usbd_status xhci_unconfigure_endpoint(struct usbd_pipe *);
159static usbd_status xhci_reset_endpoint(struct usbd_pipe *); 159static usbd_status xhci_reset_endpoint(struct usbd_pipe *);
160static usbd_status xhci_stop_endpoint_cmd(struct xhci_softc *, 160static usbd_status xhci_stop_endpoint_cmd(struct xhci_softc *,
161 struct xhci_slot *, u_int, uint32_t); 161 struct xhci_slot *, u_int, uint32_t);
162static usbd_status xhci_stop_endpoint(struct usbd_pipe *); 162static usbd_status xhci_stop_endpoint(struct usbd_pipe *);
163 163
164static void xhci_host_dequeue(struct xhci_ring * const); 164static void xhci_host_dequeue(struct xhci_ring * const);
165static usbd_status xhci_set_dequeue(struct usbd_pipe *); 165static usbd_status xhci_set_dequeue(struct usbd_pipe *);
166 166
167static usbd_status xhci_do_command(struct xhci_softc * const, 167static usbd_status xhci_do_command(struct xhci_softc * const,
168 struct xhci_soft_trb * const, int); 168 struct xhci_soft_trb * const, int);
169static usbd_status xhci_do_command_locked(struct xhci_softc * const, 169static usbd_status xhci_do_command_locked(struct xhci_softc * const,
170 struct xhci_soft_trb * const, int); 170 struct xhci_soft_trb * const, int);
171static usbd_status xhci_init_slot(struct usbd_device *, uint32_t); 171static usbd_status xhci_init_slot(struct usbd_device *, uint32_t);
172static void xhci_free_slot(struct xhci_softc *, struct xhci_slot *); 172static void xhci_free_slot(struct xhci_softc *, struct xhci_slot *);
173static usbd_status xhci_set_address(struct usbd_device *, uint32_t, bool); 173static usbd_status xhci_set_address(struct usbd_device *, uint32_t, bool);
174static usbd_status xhci_enable_slot(struct xhci_softc * const, 174static usbd_status xhci_enable_slot(struct xhci_softc * const,
175 uint8_t * const); 175 uint8_t * const);
176static usbd_status xhci_disable_slot(struct xhci_softc * const, uint8_t); 176static usbd_status xhci_disable_slot(struct xhci_softc * const, uint8_t);
177static usbd_status xhci_address_device(struct xhci_softc * const, 177static usbd_status xhci_address_device(struct xhci_softc * const,
178 uint64_t, uint8_t, bool); 178 uint64_t, uint8_t, bool);
179static void xhci_set_dcba(struct xhci_softc * const, uint64_t, int); 179static void xhci_set_dcba(struct xhci_softc * const, uint64_t, int);
180static usbd_status xhci_update_ep0_mps(struct xhci_softc * const, 180static usbd_status xhci_update_ep0_mps(struct xhci_softc * const,
181 struct xhci_slot * const, u_int); 181 struct xhci_slot * const, u_int);
182static usbd_status xhci_ring_init(struct xhci_softc * const, 182static usbd_status xhci_ring_init(struct xhci_softc * const,
183 struct xhci_ring **, size_t, size_t); 183 struct xhci_ring **, size_t, size_t);
184static void xhci_ring_free(struct xhci_softc * const, 184static void xhci_ring_free(struct xhci_softc * const,
185 struct xhci_ring ** const); 185 struct xhci_ring ** const);
186 186
187static void xhci_setup_ctx(struct usbd_pipe *); 187static void xhci_setup_ctx(struct usbd_pipe *);
188static void xhci_setup_route(struct usbd_pipe *, uint32_t *); 188static void xhci_setup_route(struct usbd_pipe *, uint32_t *);
189static void xhci_setup_tthub(struct usbd_pipe *, uint32_t *); 189static void xhci_setup_tthub(struct usbd_pipe *, uint32_t *);
190static void xhci_setup_maxburst(struct usbd_pipe *, uint32_t *); 190static void xhci_setup_maxburst(struct usbd_pipe *, uint32_t *);
191static uint32_t xhci_bival2ival(uint32_t, uint32_t); 191static uint32_t xhci_bival2ival(uint32_t, uint32_t);
192 192
193static void xhci_noop(struct usbd_pipe *); 193static void xhci_noop(struct usbd_pipe *);
194 194
195static usbd_status xhci_root_intr_transfer(struct usbd_xfer *); 195static usbd_status xhci_root_intr_transfer(struct usbd_xfer *);
196static usbd_status xhci_root_intr_start(struct usbd_xfer *); 196static usbd_status xhci_root_intr_start(struct usbd_xfer *);
197static void xhci_root_intr_abort(struct usbd_xfer *); 197static void xhci_root_intr_abort(struct usbd_xfer *);
198static void xhci_root_intr_close(struct usbd_pipe *); 198static void xhci_root_intr_close(struct usbd_pipe *);
199static void xhci_root_intr_done(struct usbd_xfer *); 199static void xhci_root_intr_done(struct usbd_xfer *);
200 200
201static usbd_status xhci_device_ctrl_transfer(struct usbd_xfer *); 201static usbd_status xhci_device_ctrl_transfer(struct usbd_xfer *);
202static usbd_status xhci_device_ctrl_start(struct usbd_xfer *); 202static usbd_status xhci_device_ctrl_start(struct usbd_xfer *);
203static void xhci_device_ctrl_abort(struct usbd_xfer *); 203static void xhci_device_ctrl_abort(struct usbd_xfer *);
204static void xhci_device_ctrl_close(struct usbd_pipe *); 204static void xhci_device_ctrl_close(struct usbd_pipe *);
205static void xhci_device_ctrl_done(struct usbd_xfer *); 205static void xhci_device_ctrl_done(struct usbd_xfer *);
206 206
207static usbd_status xhci_device_isoc_transfer(struct usbd_xfer *); 207static usbd_status xhci_device_isoc_transfer(struct usbd_xfer *);
208static usbd_status xhci_device_isoc_enter(struct usbd_xfer *); 208static usbd_status xhci_device_isoc_enter(struct usbd_xfer *);
209static void xhci_device_isoc_abort(struct usbd_xfer *); 209static void xhci_device_isoc_abort(struct usbd_xfer *);
210static void xhci_device_isoc_close(struct usbd_pipe *); 210static void xhci_device_isoc_close(struct usbd_pipe *);
211static void xhci_device_isoc_done(struct usbd_xfer *); 211static void xhci_device_isoc_done(struct usbd_xfer *);
212 212
213static usbd_status xhci_device_intr_transfer(struct usbd_xfer *); 213static usbd_status xhci_device_intr_transfer(struct usbd_xfer *);
214static usbd_status xhci_device_intr_start(struct usbd_xfer *); 214static usbd_status xhci_device_intr_start(struct usbd_xfer *);
215static void xhci_device_intr_abort(struct usbd_xfer *); 215static void xhci_device_intr_abort(struct usbd_xfer *);
216static void xhci_device_intr_close(struct usbd_pipe *); 216static void xhci_device_intr_close(struct usbd_pipe *);
217static void xhci_device_intr_done(struct usbd_xfer *); 217static void xhci_device_intr_done(struct usbd_xfer *);
218 218
219static usbd_status xhci_device_bulk_transfer(struct usbd_xfer *); 219static usbd_status xhci_device_bulk_transfer(struct usbd_xfer *);
220static usbd_status xhci_device_bulk_start(struct usbd_xfer *); 220static usbd_status xhci_device_bulk_start(struct usbd_xfer *);
221static void xhci_device_bulk_abort(struct usbd_xfer *); 221static void xhci_device_bulk_abort(struct usbd_xfer *);
222static void xhci_device_bulk_close(struct usbd_pipe *); 222static void xhci_device_bulk_close(struct usbd_pipe *);
223static void xhci_device_bulk_done(struct usbd_xfer *); 223static void xhci_device_bulk_done(struct usbd_xfer *);
224 224
225static const struct usbd_bus_methods xhci_bus_methods = { 225static const struct usbd_bus_methods xhci_bus_methods = {
226 .ubm_open = xhci_open, 226 .ubm_open = xhci_open,
227 .ubm_softint = xhci_softintr, 227 .ubm_softint = xhci_softintr,
228 .ubm_dopoll = xhci_poll, 228 .ubm_dopoll = xhci_poll,
229 .ubm_allocx = xhci_allocx, 229 .ubm_allocx = xhci_allocx,
230 .ubm_freex = xhci_freex, 230 .ubm_freex = xhci_freex,
231 .ubm_abortx = xhci_abortx, 231 .ubm_abortx = xhci_abortx,
232 .ubm_dying = xhci_dying, 232 .ubm_dying = xhci_dying,
233 .ubm_getlock = xhci_get_lock, 233 .ubm_getlock = xhci_get_lock,
234 .ubm_newdev = xhci_new_device, 234 .ubm_newdev = xhci_new_device,
235 .ubm_rhctrl = xhci_roothub_ctrl, 235 .ubm_rhctrl = xhci_roothub_ctrl,
236}; 236};
237 237
238static const struct usbd_pipe_methods xhci_root_intr_methods = { 238static const struct usbd_pipe_methods xhci_root_intr_methods = {
239 .upm_transfer = xhci_root_intr_transfer, 239 .upm_transfer = xhci_root_intr_transfer,
240 .upm_start = xhci_root_intr_start, 240 .upm_start = xhci_root_intr_start,
241 .upm_abort = xhci_root_intr_abort, 241 .upm_abort = xhci_root_intr_abort,
242 .upm_close = xhci_root_intr_close, 242 .upm_close = xhci_root_intr_close,
243 .upm_cleartoggle = xhci_noop, 243 .upm_cleartoggle = xhci_noop,
244 .upm_done = xhci_root_intr_done, 244 .upm_done = xhci_root_intr_done,
245}; 245};
246 246
247 247
248static const struct usbd_pipe_methods xhci_device_ctrl_methods = { 248static const struct usbd_pipe_methods xhci_device_ctrl_methods = {
249 .upm_transfer = xhci_device_ctrl_transfer, 249 .upm_transfer = xhci_device_ctrl_transfer,
250 .upm_start = xhci_device_ctrl_start, 250 .upm_start = xhci_device_ctrl_start,
251 .upm_abort = xhci_device_ctrl_abort, 251 .upm_abort = xhci_device_ctrl_abort,
252 .upm_close = xhci_device_ctrl_close, 252 .upm_close = xhci_device_ctrl_close,
253 .upm_cleartoggle = xhci_noop, 253 .upm_cleartoggle = xhci_noop,
254 .upm_done = xhci_device_ctrl_done, 254 .upm_done = xhci_device_ctrl_done,
255}; 255};
256 256
257static const struct usbd_pipe_methods xhci_device_isoc_methods = { 257static const struct usbd_pipe_methods xhci_device_isoc_methods = {
258 .upm_transfer = xhci_device_isoc_transfer, 258 .upm_transfer = xhci_device_isoc_transfer,
259 .upm_abort = xhci_device_isoc_abort, 259 .upm_abort = xhci_device_isoc_abort,
260 .upm_close = xhci_device_isoc_close, 260 .upm_close = xhci_device_isoc_close,
261 .upm_cleartoggle = xhci_noop, 261 .upm_cleartoggle = xhci_noop,
262 .upm_done = xhci_device_isoc_done, 262 .upm_done = xhci_device_isoc_done,
263}; 263};
264 264
265static const struct usbd_pipe_methods xhci_device_bulk_methods = { 265static const struct usbd_pipe_methods xhci_device_bulk_methods = {
266 .upm_transfer = xhci_device_bulk_transfer, 266 .upm_transfer = xhci_device_bulk_transfer,
267 .upm_start = xhci_device_bulk_start, 267 .upm_start = xhci_device_bulk_start,
268 .upm_abort = xhci_device_bulk_abort, 268 .upm_abort = xhci_device_bulk_abort,
269 .upm_close = xhci_device_bulk_close, 269 .upm_close = xhci_device_bulk_close,
270 .upm_cleartoggle = xhci_noop, 270 .upm_cleartoggle = xhci_noop,
271 .upm_done = xhci_device_bulk_done, 271 .upm_done = xhci_device_bulk_done,
272}; 272};
273 273
274static const struct usbd_pipe_methods xhci_device_intr_methods = { 274static const struct usbd_pipe_methods xhci_device_intr_methods = {
275 .upm_transfer = xhci_device_intr_transfer, 275 .upm_transfer = xhci_device_intr_transfer,
276 .upm_start = xhci_device_intr_start, 276 .upm_start = xhci_device_intr_start,
277 .upm_abort = xhci_device_intr_abort, 277 .upm_abort = xhci_device_intr_abort,
278 .upm_close = xhci_device_intr_close, 278 .upm_close = xhci_device_intr_close,
279 .upm_cleartoggle = xhci_noop, 279 .upm_cleartoggle = xhci_noop,
280 .upm_done = xhci_device_intr_done, 280 .upm_done = xhci_device_intr_done,
281}; 281};
282 282
283static inline uint32_t 283static inline uint32_t
284xhci_read_1(const struct xhci_softc * const sc, bus_size_t offset) 284xhci_read_1(const struct xhci_softc * const sc, bus_size_t offset)
285{ 285{
286 return bus_space_read_1(sc->sc_iot, sc->sc_ioh, offset); 286 return bus_space_read_1(sc->sc_iot, sc->sc_ioh, offset);
287} 287}
288 288
289static inline uint32_t 289static inline uint32_t
290xhci_read_2(const struct xhci_softc * const sc, bus_size_t offset) 290xhci_read_2(const struct xhci_softc * const sc, bus_size_t offset)
291{ 291{
292 return bus_space_read_2(sc->sc_iot, sc->sc_ioh, offset); 292 return bus_space_read_2(sc->sc_iot, sc->sc_ioh, offset);
293} 293}
294 294
295static inline uint32_t 295static inline uint32_t
296xhci_read_4(const struct xhci_softc * const sc, bus_size_t offset) 296xhci_read_4(const struct xhci_softc * const sc, bus_size_t offset)
297{ 297{
298 return bus_space_read_4(sc->sc_iot, sc->sc_ioh, offset); 298 return bus_space_read_4(sc->sc_iot, sc->sc_ioh, offset);
299} 299}
300 300
301static inline void 301static inline void
302xhci_write_1(const struct xhci_softc * const sc, bus_size_t offset, 302xhci_write_1(const struct xhci_softc * const sc, bus_size_t offset,
303 uint32_t value) 303 uint32_t value)
304{ 304{
305 bus_space_write_1(sc->sc_iot, sc->sc_ioh, offset, value); 305 bus_space_write_1(sc->sc_iot, sc->sc_ioh, offset, value);
306} 306}
307 307
308#if 0 /* unused */ 308#if 0 /* unused */
309static inline void 309static inline void
310xhci_write_4(const struct xhci_softc * const sc, bus_size_t offset, 310xhci_write_4(const struct xhci_softc * const sc, bus_size_t offset,
311 uint32_t value) 311 uint32_t value)
312{ 312{
313 bus_space_write_4(sc->sc_iot, sc->sc_ioh, offset, value); 313 bus_space_write_4(sc->sc_iot, sc->sc_ioh, offset, value);
314} 314}
315#endif /* unused */ 315#endif /* unused */
316 316
317static inline void 317static inline void
318xhci_barrier(const struct xhci_softc * const sc, int flags) 318xhci_barrier(const struct xhci_softc * const sc, int flags)
319{ 319{
320 bus_space_barrier(sc->sc_iot, sc->sc_ioh, 0, sc->sc_ios, flags); 320 bus_space_barrier(sc->sc_iot, sc->sc_ioh, 0, sc->sc_ios, flags);
321} 321}
322 322
323static inline uint32_t 323static inline uint32_t
324xhci_cap_read_4(const struct xhci_softc * const sc, bus_size_t offset) 324xhci_cap_read_4(const struct xhci_softc * const sc, bus_size_t offset)
325{ 325{
326 return bus_space_read_4(sc->sc_iot, sc->sc_cbh, offset); 326 return bus_space_read_4(sc->sc_iot, sc->sc_cbh, offset);
327} 327}
328 328
329static inline uint32_t 329static inline uint32_t
330xhci_op_read_4(const struct xhci_softc * const sc, bus_size_t offset) 330xhci_op_read_4(const struct xhci_softc * const sc, bus_size_t offset)
331{ 331{
332 return bus_space_read_4(sc->sc_iot, sc->sc_obh, offset); 332 return bus_space_read_4(sc->sc_iot, sc->sc_obh, offset);
333} 333}
334 334
335static inline void 335static inline void
336xhci_op_write_4(const struct xhci_softc * const sc, bus_size_t offset, 336xhci_op_write_4(const struct xhci_softc * const sc, bus_size_t offset,
337 uint32_t value) 337 uint32_t value)
338{ 338{
339 bus_space_write_4(sc->sc_iot, sc->sc_obh, offset, value); 339 bus_space_write_4(sc->sc_iot, sc->sc_obh, offset, value);
340} 340}
341 341
342static inline uint64_t 342static inline uint64_t
343xhci_op_read_8(const struct xhci_softc * const sc, bus_size_t offset) 343xhci_op_read_8(const struct xhci_softc * const sc, bus_size_t offset)
344{ 344{
345 uint64_t value; 345 uint64_t value;
346 346
347 if (XHCI_HCC_AC64(sc->sc_hcc)) { 347 if (XHCI_HCC_AC64(sc->sc_hcc)) {
348#ifdef XHCI_USE_BUS_SPACE_8 348#ifdef XHCI_USE_BUS_SPACE_8
349 value = bus_space_read_8(sc->sc_iot, sc->sc_obh, offset); 349 value = bus_space_read_8(sc->sc_iot, sc->sc_obh, offset);
350#else 350#else
351 value = bus_space_read_4(sc->sc_iot, sc->sc_obh, offset); 351 value = bus_space_read_4(sc->sc_iot, sc->sc_obh, offset);
352 value |= (uint64_t)bus_space_read_4(sc->sc_iot, sc->sc_obh, 352 value |= (uint64_t)bus_space_read_4(sc->sc_iot, sc->sc_obh,
353 offset + 4) << 32; 353 offset + 4) << 32;
354#endif 354#endif
355 } else { 355 } else {
356 value = bus_space_read_4(sc->sc_iot, sc->sc_obh, offset); 356 value = bus_space_read_4(sc->sc_iot, sc->sc_obh, offset);
357 } 357 }
358 358
359 return value; 359 return value;
360} 360}
361 361
362static inline void 362static inline void
363xhci_op_write_8(const struct xhci_softc * const sc, bus_size_t offset, 363xhci_op_write_8(const struct xhci_softc * const sc, bus_size_t offset,
364 uint64_t value) 364 uint64_t value)
365{ 365{
366 if (XHCI_HCC_AC64(sc->sc_hcc)) { 366 if (XHCI_HCC_AC64(sc->sc_hcc)) {
367#ifdef XHCI_USE_BUS_SPACE_8 367#ifdef XHCI_USE_BUS_SPACE_8
368 bus_space_write_8(sc->sc_iot, sc->sc_obh, offset, value); 368 bus_space_write_8(sc->sc_iot, sc->sc_obh, offset, value);
369#else 369#else
370 bus_space_write_4(sc->sc_iot, sc->sc_obh, offset + 0, 370 bus_space_write_4(sc->sc_iot, sc->sc_obh, offset + 0,
371 (value >> 0) & 0xffffffff); 371 (value >> 0) & 0xffffffff);
372 bus_space_write_4(sc->sc_iot, sc->sc_obh, offset + 4, 372 bus_space_write_4(sc->sc_iot, sc->sc_obh, offset + 4,
373 (value >> 32) & 0xffffffff); 373 (value >> 32) & 0xffffffff);
374#endif 374#endif
375 } else { 375 } else {
376 bus_space_write_4(sc->sc_iot, sc->sc_obh, offset, value); 376 bus_space_write_4(sc->sc_iot, sc->sc_obh, offset, value);
377 } 377 }
378} 378}
379 379
380static inline uint32_t 380static inline uint32_t
381xhci_rt_read_4(const struct xhci_softc * const sc, bus_size_t offset) 381xhci_rt_read_4(const struct xhci_softc * const sc, bus_size_t offset)
382{ 382{
383 return bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset); 383 return bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset);
384} 384}
385 385
386static inline void 386static inline void
387xhci_rt_write_4(const struct xhci_softc * const sc, bus_size_t offset, 387xhci_rt_write_4(const struct xhci_softc * const sc, bus_size_t offset,
388 uint32_t value) 388 uint32_t value)
389{ 389{
390 bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset, value); 390 bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset, value);
391} 391}
392 392
393static inline uint64_t 393static inline uint64_t
394xhci_rt_read_8(const struct xhci_softc * const sc, bus_size_t offset) 394xhci_rt_read_8(const struct xhci_softc * const sc, bus_size_t offset)
395{ 395{
396 uint64_t value; 396 uint64_t value;
397 397
398 if (XHCI_HCC_AC64(sc->sc_hcc)) { 398 if (XHCI_HCC_AC64(sc->sc_hcc)) {
399#ifdef XHCI_USE_BUS_SPACE_8 399#ifdef XHCI_USE_BUS_SPACE_8
400 value = bus_space_read_8(sc->sc_iot, sc->sc_rbh, offset); 400 value = bus_space_read_8(sc->sc_iot, sc->sc_rbh, offset);
401#else 401#else
402 value = bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset); 402 value = bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset);
403 value |= (uint64_t)bus_space_read_4(sc->sc_iot, sc->sc_rbh, 403 value |= (uint64_t)bus_space_read_4(sc->sc_iot, sc->sc_rbh,
404 offset + 4) << 32; 404 offset + 4) << 32;
405#endif 405#endif
406 } else { 406 } else {
407 value = bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset); 407 value = bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset);
408 } 408 }
409 409
410 return value; 410 return value;
411} 411}
412 412
413static inline void 413static inline void
414xhci_rt_write_8(const struct xhci_softc * const sc, bus_size_t offset, 414xhci_rt_write_8(const struct xhci_softc * const sc, bus_size_t offset,
415 uint64_t value) 415 uint64_t value)
416{ 416{
417 if (XHCI_HCC_AC64(sc->sc_hcc)) { 417 if (XHCI_HCC_AC64(sc->sc_hcc)) {
418#ifdef XHCI_USE_BUS_SPACE_8 418#ifdef XHCI_USE_BUS_SPACE_8
419 bus_space_write_8(sc->sc_iot, sc->sc_rbh, offset, value); 419 bus_space_write_8(sc->sc_iot, sc->sc_rbh, offset, value);
420#else 420#else
421 bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset + 0, 421 bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset + 0,
422 (value >> 0) & 0xffffffff); 422 (value >> 0) & 0xffffffff);
423 bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset + 4, 423 bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset + 4,
424 (value >> 32) & 0xffffffff); 424 (value >> 32) & 0xffffffff);
425#endif 425#endif
426 } else { 426 } else {
427 bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset, value); 427 bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset, value);
428 } 428 }
429} 429}
430 430
431#if 0 /* unused */ 431#if 0 /* unused */
432static inline uint32_t 432static inline uint32_t
433xhci_db_read_4(const struct xhci_softc * const sc, bus_size_t offset) 433xhci_db_read_4(const struct xhci_softc * const sc, bus_size_t offset)
434{ 434{
435 return bus_space_read_4(sc->sc_iot, sc->sc_dbh, offset); 435 return bus_space_read_4(sc->sc_iot, sc->sc_dbh, offset);
436} 436}
437#endif /* unused */ 437#endif /* unused */
438 438
439static inline void 439static inline void
440xhci_db_write_4(const struct xhci_softc * const sc, bus_size_t offset, 440xhci_db_write_4(const struct xhci_softc * const sc, bus_size_t offset,
441 uint32_t value) 441 uint32_t value)
442{ 442{
443 bus_space_write_4(sc->sc_iot, sc->sc_dbh, offset, value); 443 bus_space_write_4(sc->sc_iot, sc->sc_dbh, offset, value);
444} 444}
445 445
446/* --- */ 446/* --- */
447 447
448static inline uint8_t 448static inline uint8_t
449xhci_ep_get_type(usb_endpoint_descriptor_t * const ed) 449xhci_ep_get_type(usb_endpoint_descriptor_t * const ed)
450{ 450{
451 u_int eptype = 0; 451 u_int eptype = 0;
452 452
453 switch (UE_GET_XFERTYPE(ed->bmAttributes)) { 453 switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
454 case UE_CONTROL: 454 case UE_CONTROL:
455 eptype = 0x0; 455 eptype = 0x0;
456 break; 456 break;
457 case UE_ISOCHRONOUS: 457 case UE_ISOCHRONOUS:
458 eptype = 0x1; 458 eptype = 0x1;
459 break; 459 break;
460 case UE_BULK: 460 case UE_BULK:
461 eptype = 0x2; 461 eptype = 0x2;
462 break; 462 break;
463 case UE_INTERRUPT: 463 case UE_INTERRUPT:
464 eptype = 0x3; 464 eptype = 0x3;
465 break; 465 break;
466 } 466 }
467 467
468 if ((UE_GET_XFERTYPE(ed->bmAttributes) == UE_CONTROL) || 468 if ((UE_GET_XFERTYPE(ed->bmAttributes) == UE_CONTROL) ||
469 (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)) 469 (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN))
470 return eptype | 0x4; 470 return eptype | 0x4;
471 else 471 else
472 return eptype; 472 return eptype;
473} 473}
474 474
475static u_int 475static u_int
476xhci_ep_get_dci(usb_endpoint_descriptor_t * const ed) 476xhci_ep_get_dci(usb_endpoint_descriptor_t * const ed)
477{ 477{
478 /* xHCI 1.0 section 4.5.1 */ 478 /* xHCI 1.0 section 4.5.1 */
479 u_int epaddr = UE_GET_ADDR(ed->bEndpointAddress); 479 u_int epaddr = UE_GET_ADDR(ed->bEndpointAddress);
480 u_int in = 0; 480 u_int in = 0;
481 481
482 if ((UE_GET_XFERTYPE(ed->bmAttributes) == UE_CONTROL) || 482 if ((UE_GET_XFERTYPE(ed->bmAttributes) == UE_CONTROL) ||
483 (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)) 483 (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN))
484 in = 1; 484 in = 1;
485 485
486 return epaddr * 2 + in; 486 return epaddr * 2 + in;
487} 487}
488 488
489static inline u_int 489static inline u_int
490xhci_dci_to_ici(const u_int i) 490xhci_dci_to_ici(const u_int i)
491{ 491{
492 return i + 1; 492 return i + 1;
493} 493}
494 494
495static inline void * 495static inline void *
496xhci_slot_get_dcv(struct xhci_softc * const sc, struct xhci_slot * const xs, 496xhci_slot_get_dcv(struct xhci_softc * const sc, struct xhci_slot * const xs,
497 const u_int dci) 497 const u_int dci)
498{ 498{
499 return KERNADDR(&xs->xs_dc_dma, sc->sc_ctxsz * dci); 499 return KERNADDR(&xs->xs_dc_dma, sc->sc_ctxsz * dci);
500} 500}
501 501
502#if 0 /* unused */ 502#if 0 /* unused */
503static inline bus_addr_t 503static inline bus_addr_t
504xhci_slot_get_dcp(struct xhci_softc * const sc, struct xhci_slot * const xs, 504xhci_slot_get_dcp(struct xhci_softc * const sc, struct xhci_slot * const xs,
505 const u_int dci) 505 const u_int dci)
506{ 506{
507 return DMAADDR(&xs->xs_dc_dma, sc->sc_ctxsz * dci); 507 return DMAADDR(&xs->xs_dc_dma, sc->sc_ctxsz * dci);
508} 508}
509#endif /* unused */ 509#endif /* unused */
510 510
511static inline void * 511static inline void *
512xhci_slot_get_icv(struct xhci_softc * const sc, struct xhci_slot * const xs, 512xhci_slot_get_icv(struct xhci_softc * const sc, struct xhci_slot * const xs,
513 const u_int ici) 513 const u_int ici)
514{ 514{
515 return KERNADDR(&xs->xs_ic_dma, sc->sc_ctxsz * ici); 515 return KERNADDR(&xs->xs_ic_dma, sc->sc_ctxsz * ici);
516} 516}
517 517
518static inline bus_addr_t 518static inline bus_addr_t
519xhci_slot_get_icp(struct xhci_softc * const sc, struct xhci_slot * const xs, 519xhci_slot_get_icp(struct xhci_softc * const sc, struct xhci_slot * const xs,
520 const u_int ici) 520 const u_int ici)
521{ 521{
522 return DMAADDR(&xs->xs_ic_dma, sc->sc_ctxsz * ici); 522 return DMAADDR(&xs->xs_ic_dma, sc->sc_ctxsz * ici);
523} 523}
524 524
525static inline struct xhci_trb * 525static inline struct xhci_trb *
526xhci_ring_trbv(struct xhci_ring * const xr, u_int idx) 526xhci_ring_trbv(struct xhci_ring * const xr, u_int idx)
527{ 527{
528 return KERNADDR(&xr->xr_dma, XHCI_TRB_SIZE * idx); 528 return KERNADDR(&xr->xr_dma, XHCI_TRB_SIZE * idx);
529} 529}
530 530
531static inline bus_addr_t 531static inline bus_addr_t
532xhci_ring_trbp(struct xhci_ring * const xr, u_int idx) 532xhci_ring_trbp(struct xhci_ring * const xr, u_int idx)
533{ 533{
534 return DMAADDR(&xr->xr_dma, XHCI_TRB_SIZE * idx); 534 return DMAADDR(&xr->xr_dma, XHCI_TRB_SIZE * idx);
535} 535}
536 536
537static inline void 537static inline void
538xhci_xfer_put_trb(struct xhci_xfer * const xx, u_int idx, 538xhci_xfer_put_trb(struct xhci_xfer * const xx, u_int idx,
539 uint64_t parameter, uint32_t status, uint32_t control) 539 uint64_t parameter, uint32_t status, uint32_t control)
540{ 540{
541 KASSERTMSG(idx < xx->xx_ntrb, "idx=%u xx_ntrb=%u", idx, xx->xx_ntrb); 541 KASSERTMSG(idx < xx->xx_ntrb, "idx=%u xx_ntrb=%u", idx, xx->xx_ntrb);
542 xx->xx_trb[idx].trb_0 = parameter; 542 xx->xx_trb[idx].trb_0 = parameter;
543 xx->xx_trb[idx].trb_2 = status; 543 xx->xx_trb[idx].trb_2 = status;
544 xx->xx_trb[idx].trb_3 = control; 544 xx->xx_trb[idx].trb_3 = control;
545} 545}
546 546
547static inline void 547static inline void
548xhci_trb_put(struct xhci_trb * const trb, uint64_t parameter, uint32_t status, 548xhci_trb_put(struct xhci_trb * const trb, uint64_t parameter, uint32_t status,
549 uint32_t control) 549 uint32_t control)
550{ 550{
551 trb->trb_0 = htole64(parameter); 551 trb->trb_0 = htole64(parameter);
552 trb->trb_2 = htole32(status); 552 trb->trb_2 = htole32(status);
553 trb->trb_3 = htole32(control); 553 trb->trb_3 = htole32(control);
554} 554}
555 555
556static int 556static int
557xhci_trb_get_idx(struct xhci_ring *xr, uint64_t trb_0, int *idx) 557xhci_trb_get_idx(struct xhci_ring *xr, uint64_t trb_0, int *idx)
558{ 558{
559 /* base address of TRBs */ 559 /* base address of TRBs */
560 bus_addr_t trbp = xhci_ring_trbp(xr, 0); 560 bus_addr_t trbp = xhci_ring_trbp(xr, 0);
561 561
562 /* trb_0 range sanity check */ 562 /* trb_0 range sanity check */
563 if (trb_0 == 0 || trb_0 < trbp || 563 if (trb_0 == 0 || trb_0 < trbp ||
564 (trb_0 - trbp) % sizeof(struct xhci_trb) != 0 || 564 (trb_0 - trbp) % sizeof(struct xhci_trb) != 0 ||
565 (trb_0 - trbp) / sizeof(struct xhci_trb) >= xr->xr_ntrb) { 565 (trb_0 - trbp) / sizeof(struct xhci_trb) >= xr->xr_ntrb) {
566 return 1; 566 return 1;
567 } 567 }
568 *idx = (trb_0 - trbp) / sizeof(struct xhci_trb); 568 *idx = (trb_0 - trbp) / sizeof(struct xhci_trb);
569 return 0; 569 return 0;
570} 570}
571 571
572static unsigned int 572static unsigned int
573xhci_get_epstate(struct xhci_softc * const sc, struct xhci_slot * const xs, 573xhci_get_epstate(struct xhci_softc * const sc, struct xhci_slot * const xs,
574 u_int dci) 574 u_int dci)
575{ 575{
576 uint32_t *cp; 576 uint32_t *cp;
577 577
578 usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD); 578 usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
579 cp = xhci_slot_get_dcv(sc, xs, dci); 579 cp = xhci_slot_get_dcv(sc, xs, dci);
580 return XHCI_EPCTX_0_EPSTATE_GET(le32toh(cp[0])); 580 return XHCI_EPCTX_0_EPSTATE_GET(le32toh(cp[0]));
581} 581}
582 582
583static inline unsigned int 583static inline unsigned int
584xhci_ctlrport2bus(struct xhci_softc * const sc, unsigned int ctlrport) 584xhci_ctlrport2bus(struct xhci_softc * const sc, unsigned int ctlrport)
585{ 585{
586 const unsigned int port = ctlrport - 1; 586 const unsigned int port = ctlrport - 1;
587 const uint8_t bit = __BIT(port % NBBY); 587 const uint8_t bit = __BIT(port % NBBY);
588 588
589 return __SHIFTOUT(sc->sc_ctlrportbus[port / NBBY], bit); 589 return __SHIFTOUT(sc->sc_ctlrportbus[port / NBBY], bit);
590} 590}
591 591
592/* 592/*
593 * Return the roothub port for a controller port. Both are 1..n. 593 * Return the roothub port for a controller port. Both are 1..n.
594 */ 594 */
595static inline unsigned int 595static inline unsigned int
596xhci_ctlrport2rhport(struct xhci_softc * const sc, unsigned int ctrlport) 596xhci_ctlrport2rhport(struct xhci_softc * const sc, unsigned int ctrlport)
597{ 597{
598 598
599 return sc->sc_ctlrportmap[ctrlport - 1]; 599 return sc->sc_ctlrportmap[ctrlport - 1];
600} 600}
601 601
602/* 602/*
603 * Return the controller port for a bus roothub port. Both are 1..n. 603 * Return the controller port for a bus roothub port. Both are 1..n.
604 */ 604 */
605static inline unsigned int 605static inline unsigned int
606xhci_rhport2ctlrport(struct xhci_softc * const sc, unsigned int bn, 606xhci_rhport2ctlrport(struct xhci_softc * const sc, unsigned int bn,
607 unsigned int rhport) 607 unsigned int rhport)
608{ 608{
609 609
610 return sc->sc_rhportmap[bn][rhport - 1]; 610 return sc->sc_rhportmap[bn][rhport - 1];
611} 611}
612 612
613/* --- */ 613/* --- */
614 614
615void 615void
616xhci_childdet(device_t self, device_t child) 616xhci_childdet(device_t self, device_t child)
617{ 617{
618 struct xhci_softc * const sc = device_private(self); 618 struct xhci_softc * const sc = device_private(self);
619 619
620 KASSERT((sc->sc_child == child) || (sc->sc_child2 == child)); 620 KASSERT((sc->sc_child == child) || (sc->sc_child2 == child));
621 if (child == sc->sc_child2) 621 if (child == sc->sc_child2)
622 sc->sc_child2 = NULL; 622 sc->sc_child2 = NULL;
623 else if (child == sc->sc_child) 623 else if (child == sc->sc_child)
624 sc->sc_child = NULL; 624 sc->sc_child = NULL;
625} 625}
626 626
627int 627int
628xhci_detach(struct xhci_softc *sc, int flags) 628xhci_detach(struct xhci_softc *sc, int flags)
629{ 629{
630 int rv = 0; 630 int rv = 0;
631 631
632 if (sc->sc_child2 != NULL) { 632 if (sc->sc_child2 != NULL) {
633 rv = config_detach(sc->sc_child2, flags); 633 rv = config_detach(sc->sc_child2, flags);
634 if (rv != 0) 634 if (rv != 0)
635 return rv; 635 return rv;
636 KASSERT(sc->sc_child2 == NULL); 636 KASSERT(sc->sc_child2 == NULL);
637 } 637 }
638 638
639 if (sc->sc_child != NULL) { 639 if (sc->sc_child != NULL) {
640 rv = config_detach(sc->sc_child, flags); 640 rv = config_detach(sc->sc_child, flags);
641 if (rv != 0) 641 if (rv != 0)
642 return rv; 642 return rv;
643 KASSERT(sc->sc_child == NULL); 643 KASSERT(sc->sc_child == NULL);
644 } 644 }
645 645
646 /* XXX unconfigure/free slots */ 646 /* XXX unconfigure/free slots */
647 647
648 /* verify: */ 648 /* verify: */
649 xhci_rt_write_4(sc, XHCI_IMAN(0), 0); 649 xhci_rt_write_4(sc, XHCI_IMAN(0), 0);
650 xhci_op_write_4(sc, XHCI_USBCMD, 0); 650 xhci_op_write_4(sc, XHCI_USBCMD, 0);
651 /* do we need to wait for stop? */ 651 /* do we need to wait for stop? */
652 652
653 xhci_op_write_8(sc, XHCI_CRCR, 0); 653 xhci_op_write_8(sc, XHCI_CRCR, 0);
654 xhci_ring_free(sc, &sc->sc_cr); 654 xhci_ring_free(sc, &sc->sc_cr);
655 cv_destroy(&sc->sc_command_cv); 655 cv_destroy(&sc->sc_command_cv);
656 cv_destroy(&sc->sc_cmdbusy_cv); 656 cv_destroy(&sc->sc_cmdbusy_cv);
657 657
658 xhci_rt_write_4(sc, XHCI_ERSTSZ(0), 0); 658 xhci_rt_write_4(sc, XHCI_ERSTSZ(0), 0);
659 xhci_rt_write_8(sc, XHCI_ERSTBA(0), 0); 659 xhci_rt_write_8(sc, XHCI_ERSTBA(0), 0);
660 xhci_rt_write_8(sc, XHCI_ERDP(0), 0 | XHCI_ERDP_BUSY); 660 xhci_rt_write_8(sc, XHCI_ERDP(0), 0 | XHCI_ERDP_BUSY);
661 xhci_ring_free(sc, &sc->sc_er); 661 xhci_ring_free(sc, &sc->sc_er);
662 662
663 usb_freemem(&sc->sc_bus, &sc->sc_eventst_dma); 663 usb_freemem(&sc->sc_bus, &sc->sc_eventst_dma);
664 664
665 xhci_op_write_8(sc, XHCI_DCBAAP, 0); 665 xhci_op_write_8(sc, XHCI_DCBAAP, 0);
666 usb_freemem(&sc->sc_bus, &sc->sc_dcbaa_dma); 666 usb_freemem(&sc->sc_bus, &sc->sc_dcbaa_dma);
667 667
668 kmem_free(sc->sc_slots, sizeof(*sc->sc_slots) * sc->sc_maxslots); 668 kmem_free(sc->sc_slots, sizeof(*sc->sc_slots) * sc->sc_maxslots);
669 669
670 kmem_free(sc->sc_ctlrportbus, 670 kmem_free(sc->sc_ctlrportbus,
671 howmany(sc->sc_maxports * sizeof(uint8_t), NBBY)); 671 howmany(sc->sc_maxports * sizeof(uint8_t), NBBY));
672 kmem_free(sc->sc_ctlrportmap, sc->sc_maxports * sizeof(int)); 672 kmem_free(sc->sc_ctlrportmap, sc->sc_maxports * sizeof(int));
673 673
674 for (size_t j = 0; j < __arraycount(sc->sc_rhportmap); j++) { 674 for (size_t j = 0; j < __arraycount(sc->sc_rhportmap); j++) {
675 kmem_free(sc->sc_rhportmap[j], sc->sc_maxports * sizeof(int)); 675 kmem_free(sc->sc_rhportmap[j], sc->sc_maxports * sizeof(int));
676 } 676 }
677 677
678 mutex_destroy(&sc->sc_lock); 678 mutex_destroy(&sc->sc_lock);
679 mutex_destroy(&sc->sc_intr_lock); 679 mutex_destroy(&sc->sc_intr_lock);
680 680
681 pool_cache_destroy(sc->sc_xferpool); 681 pool_cache_destroy(sc->sc_xferpool);
682 682
683 return rv; 683 return rv;
684} 684}
685 685
686int 686int
687xhci_activate(device_t self, enum devact act) 687xhci_activate(device_t self, enum devact act)
688{ 688{
689 struct xhci_softc * const sc = device_private(self); 689 struct xhci_softc * const sc = device_private(self);
690 690
691 switch (act) { 691 switch (act) {
692 case DVACT_DEACTIVATE: 692 case DVACT_DEACTIVATE:
693 sc->sc_dying = true; 693 sc->sc_dying = true;
694 return 0; 694 return 0;
695 default: 695 default:
696 return EOPNOTSUPP; 696 return EOPNOTSUPP;
697 } 697 }
698} 698}
699 699
700bool 700bool
701xhci_suspend(device_t self, const pmf_qual_t *qual) 701xhci_suspend(device_t self, const pmf_qual_t *qual)
702{ 702{
703 struct xhci_softc * const sc = device_private(self); 703 struct xhci_softc * const sc = device_private(self);
704 size_t i, j, bn, dci; 704 size_t i, j, bn, dci;
705 int port; 705 int port;
706 uint32_t v; 706 uint32_t v;
707 usbd_status err; 707 usbd_status err;
708 bool ok = false; 708 bool ok = false;
709 709
710 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 710 XHCIHIST_FUNC(); XHCIHIST_CALLED();
711 711
712 mutex_enter(&sc->sc_lock); 712 mutex_enter(&sc->sc_lock);
713 713
714 /* 714 /*
715 * Block issuance of new commands, and wait for all pending 715 * Block issuance of new commands, and wait for all pending
716 * commands to complete. 716 * commands to complete.
717 */ 717 */
718 KASSERT(sc->sc_suspender == NULL); 718 KASSERT(sc->sc_suspender == NULL);
719 sc->sc_suspender = curlwp; 719 sc->sc_suspender = curlwp;
720 while (sc->sc_command_addr != 0) 720 while (sc->sc_command_addr != 0)
721 cv_wait(&sc->sc_cmdbusy_cv, &sc->sc_lock); 721 cv_wait(&sc->sc_cmdbusy_cv, &sc->sc_lock);
722 722
723 /* 723 /*
724 * xHCI Requirements Specification 1.2, May 2019, Sec. 4.23.2: 724 * xHCI Requirements Specification 1.2, May 2019, Sec. 4.23.2:
725 * xHCI Power Management, p. 342 725 * xHCI Power Management, p. 342
726 * https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/extensible-host-controler-interface-usb-xhci.pdf#page=342 726 * https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/extensible-host-controler-interface-usb-xhci.pdf#page=342
727 */ 727 */
728 728
729 /* 729 /*
730 * `1. Stop all USB activity by issuing Stop Endpoint Commands 730 * `1. Stop all USB activity by issuing Stop Endpoint Commands
731 * for Busy endpoints in the Running state. If the Force 731 * for Busy endpoints in the Running state. If the Force
732 * Save Context Capability (FSC = ``0'') is not supported, 732 * Save Context Capability (FSC = ``0'') is not supported,
733 * then Stop Endpoint Commands shall be issued for all idle 733 * then Stop Endpoint Commands shall be issued for all idle
734 * endpoints in the Running state as well. The Stop 734 * endpoints in the Running state as well. The Stop
735 * Endpoint Command causes the xHC to update the respective 735 * Endpoint Command causes the xHC to update the respective
736 * Endpoint or Stream Contexts in system memory, e.g. the 736 * Endpoint or Stream Contexts in system memory, e.g. the
737 * TR Dequeue Pointer, DCS, etc. fields. Refer to 737 * TR Dequeue Pointer, DCS, etc. fields. Refer to
738 * Implementation Note "0".' 738 * Implementation Note "0".'
739 */ 739 */
740 for (i = 0; i < sc->sc_maxslots; i++) { 740 for (i = 0; i < sc->sc_maxslots; i++) {
741 struct xhci_slot *xs = &sc->sc_slots[i]; 741 struct xhci_slot *xs = &sc->sc_slots[i];
742 742
743 /* Skip if the slot is not in use. */ 743 /* Skip if the slot is not in use. */
744 if (xs->xs_idx == 0) 744 if (xs->xs_idx == 0)
745 continue; 745 continue;
746 746
747 for (dci = XHCI_DCI_SLOT; dci <= XHCI_MAX_DCI; dci++) { 747 for (dci = XHCI_DCI_SLOT; dci <= XHCI_MAX_DCI; dci++) {
748 /* Skip if the endpoint is not Running. */ 748 /* Skip if the endpoint is not Running. */
749 /* XXX What about Busy? */ 749 /* XXX What about Busy? */
750 if (xhci_get_epstate(sc, xs, dci) != 750 if (xhci_get_epstate(sc, xs, dci) !=
751 XHCI_EPSTATE_RUNNING) 751 XHCI_EPSTATE_RUNNING)
752 continue; 752 continue;
753 753
754 /* Stop endpoint. */ 754 /* Stop endpoint. */
755 err = xhci_stop_endpoint_cmd(sc, xs, dci, 755 err = xhci_stop_endpoint_cmd(sc, xs, dci,
756 XHCI_TRB_3_SUSP_EP_BIT); 756 XHCI_TRB_3_SUSP_EP_BIT);
757 if (err) { 757 if (err) {
758 device_printf(self, "failed to stop endpoint" 758 device_printf(self, "failed to stop endpoint"
759 " slot %zu dci %zu err %d\n", 759 " slot %zu dci %zu err %d\n",
760 i, dci, err); 760 i, dci, err);
761 goto out; 761 goto out;
762 } 762 }
763 } 763 }
764 } 764 }
765 765
766 /* 766 /*
767 * Next, suspend all the ports: 767 * Next, suspend all the ports:
768 * 768 *
769 * xHCI Requirements Specification 1.2, May 2019, Sec. 4.15: 769 * xHCI Requirements Specification 1.2, May 2019, Sec. 4.15:
770 * Suspend-Resume, pp. 276-283 770 * Suspend-Resume, pp. 276-283
771 * https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/extensible-host-controler-interface-usb-xhci.pdf#page=276 771 * https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/extensible-host-controler-interface-usb-xhci.pdf#page=276
772 */ 772 */
773 for (bn = 0; bn < 2; bn++) { 773 for (bn = 0; bn < 2; bn++) {
774 for (i = 1; i <= sc->sc_rhportcount[bn]; i++) { 774 for (i = 1; i <= sc->sc_rhportcount[bn]; i++) {
775 /* 4.15.1: Port Suspend. */ 775 /* 4.15.1: Port Suspend. */
776 port = XHCI_PORTSC(xhci_rhport2ctlrport(sc, bn, i)); 776 port = XHCI_PORTSC(xhci_rhport2ctlrport(sc, bn, i));
777 777
778 /* 778 /*
779 * `System software places individual ports 779 * `System software places individual ports
780 * into suspend mode by writing a ``3'' into 780 * into suspend mode by writing a ``3'' into
781 * the appropriate PORTSC register Port Link 781 * the appropriate PORTSC register Port Link
782 * State (PLS) field (refer to Section 5.4.8). 782 * State (PLS) field (refer to Section 5.4.8).
783 * Software should only set the PLS field to 783 * Software should only set the PLS field to
784 * ``3'' when the port is in the Enabled 784 * ``3'' when the port is in the Enabled
785 * state.' 785 * state.'
786 * 786 *
787 * `Software should not attempt to suspend a 787 * `Software should not attempt to suspend a
788 * port unless the port reports that it is in 788 * port unless the port reports that it is in
789 * the enabled (PED = ``1''; PLS < ``3'') 789 * the enabled (PED = ``1''; PLS < ``3'')
790 * state (refer to Section 5.4.8 for more 790 * state (refer to Section 5.4.8 for more
791 * information about PED and PLS).' 791 * information about PED and PLS).'
792 */ 792 */
793 v = xhci_op_read_4(sc, port); 793 v = xhci_op_read_4(sc, port);
794 if (((v & XHCI_PS_PED) == 0) || 794 if (((v & XHCI_PS_PED) == 0) ||
795 XHCI_PS_PLS_GET(v) >= XHCI_PS_PLS_U3) 795 XHCI_PS_PLS_GET(v) >= XHCI_PS_PLS_U3)
796 continue; 796 continue;
797 v &= ~(XHCI_PS_PLS_MASK | XHCI_PS_CLEAR); 797 v &= ~(XHCI_PS_PLS_MASK | XHCI_PS_CLEAR);
798 v |= XHCI_PS_LWS | XHCI_PS_PLS_SET(XHCI_PS_PLS_SETU3); 798 v |= XHCI_PS_LWS | XHCI_PS_PLS_SET(XHCI_PS_PLS_SETU3);
799 xhci_op_write_4(sc, port, v); 799 xhci_op_write_4(sc, port, v);
800 800
801 /* 801 /*
802 * `When the PLS field is written with U3 802 * `When the PLS field is written with U3
803 * (``3''), the status of the PLS bit will not 803 * (``3''), the status of the PLS bit will not
804 * change to the target U state U3 until the 804 * change to the target U state U3 until the
805 * suspend signaling has completed to the 805 * suspend signaling has completed to the
806 * attached device (which may be as long as 806 * attached device (which may be as long as
807 * 10ms.).' 807 * 10ms.).'
808 * 808 *
809 * `Software is required to wait for U3 809 * `Software is required to wait for U3
810 * transitions to complete before it puts the 810 * transitions to complete before it puts the
811 * xHC into a low power state, and before 811 * xHC into a low power state, and before
812 * resuming the port.' 812 * resuming the port.'
813 * 813 *
814 * XXX Take advantage of the technique to 814 * XXX Take advantage of the technique to
815 * reduce polling on host controllers that 815 * reduce polling on host controllers that
816 * support the U3C capability. 816 * support the U3C capability.
817 */ 817 */
818 for (j = 0; j < XHCI_WAIT_PLS_U3; j++) { 818 for (j = 0; j < XHCI_WAIT_PLS_U3; j++) {
819 v = xhci_op_read_4(sc, port); 819 v = xhci_op_read_4(sc, port);
820 if (XHCI_PS_PLS_GET(v) == XHCI_PS_PLS_U3) 820 if (XHCI_PS_PLS_GET(v) == XHCI_PS_PLS_U3)
821 break; 821 break;
822 usb_delay_ms(&sc->sc_bus, 1); 822 usb_delay_ms(&sc->sc_bus, 1);
823 } 823 }
824 if (j == XHCI_WAIT_PLS_U3) { 824 if (j == XHCI_WAIT_PLS_U3) {
825 device_printf(self, 825 device_printf(self,
826 "suspend timeout on bus %zu port %zu\n", 826 "suspend timeout on bus %zu port %zu\n",
827 bn, i); 827 bn, i);
828 goto out; 828 goto out;
829 } 829 }
830 } 830 }
831 } 831 }
832 832
833 /* 833 /*
834 * `2. Ensure that the Command Ring is in the Stopped state 834 * `2. Ensure that the Command Ring is in the Stopped state
835 * (CRR = ``0'') or Idle (i.e. the Command Transfer Ring is 835 * (CRR = ``0'') or Idle (i.e. the Command Transfer Ring is
836 * empty), and all Command Completion Events associated 836 * empty), and all Command Completion Events associated
837 * with them have been received.' 837 * with them have been received.'
838 * 838 *
839 * XXX 839 * XXX
840 */ 840 */
841 841
842 /* `3. Stop the controller by setting Run/Stop (R/S) = ``0''.' */ 842 /* `3. Stop the controller by setting Run/Stop (R/S) = ``0''.' */
843 xhci_op_write_4(sc, XHCI_USBCMD, 843 xhci_op_write_4(sc, XHCI_USBCMD,
844 xhci_op_read_4(sc, XHCI_USBCMD) & ~XHCI_CMD_RS); 844 xhci_op_read_4(sc, XHCI_USBCMD) & ~XHCI_CMD_RS);
845 845
846 /* 846 /*
847 * `4. Read the Operational Runtime, and VTIO registers in the 847 * `4. Read the Operational Runtime, and VTIO registers in the
848 * following order: USBCMD, DNCTRL, DCBAAP, CONFIG, ERSTSZ, 848 * following order: USBCMD, DNCTRL, DCBAAP, CONFIG, ERSTSZ,
849 * ERSTBA, ERDP, IMAN, IMOD, and VTIO and save their 849 * ERSTBA, ERDP, IMAN, IMOD, and VTIO and save their
850 * state.' 850 * state.'
851 * 851 *
852 * (We don't use VTIO here (XXX for now?).) 852 * (We don't use VTIO here (XXX for now?).)
853 */ 853 */
854 sc->sc_regs.usbcmd = xhci_op_read_4(sc, XHCI_USBCMD); 854 sc->sc_regs.usbcmd = xhci_op_read_4(sc, XHCI_USBCMD);
855 sc->sc_regs.dnctrl = xhci_op_read_4(sc, XHCI_DNCTRL); 855 sc->sc_regs.dnctrl = xhci_op_read_4(sc, XHCI_DNCTRL);
856 sc->sc_regs.dcbaap = xhci_op_read_8(sc, XHCI_DCBAAP); 856 sc->sc_regs.dcbaap = xhci_op_read_8(sc, XHCI_DCBAAP);
857 sc->sc_regs.config = xhci_op_read_4(sc, XHCI_CONFIG); 857 sc->sc_regs.config = xhci_op_read_4(sc, XHCI_CONFIG);
858 sc->sc_regs.erstsz0 = xhci_rt_read_4(sc, XHCI_ERSTSZ(0)); 858 sc->sc_regs.erstsz0 = xhci_rt_read_4(sc, XHCI_ERSTSZ(0));
859 sc->sc_regs.erstba0 = xhci_rt_read_8(sc, XHCI_ERSTBA(0)); 859 sc->sc_regs.erstba0 = xhci_rt_read_8(sc, XHCI_ERSTBA(0));
860 sc->sc_regs.erdp0 = xhci_rt_read_8(sc, XHCI_ERDP(0)); 860 sc->sc_regs.erdp0 = xhci_rt_read_8(sc, XHCI_ERDP(0));
861 sc->sc_regs.iman0 = xhci_rt_read_4(sc, XHCI_IMAN(0)); 861 sc->sc_regs.iman0 = xhci_rt_read_4(sc, XHCI_IMAN(0));
862 sc->sc_regs.imod0 = xhci_rt_read_4(sc, XHCI_IMOD(0)); 862 sc->sc_regs.imod0 = xhci_rt_read_4(sc, XHCI_IMOD(0));
863 863
864 /* 864 /*
865 * `5. Set the Controller Save State (CSS) flag in the USBCMD 865 * `5. Set the Controller Save State (CSS) flag in the USBCMD
866 * register (5.4.1)...' 866 * register (5.4.1)...'
867 */ 867 */
868 xhci_op_write_4(sc, XHCI_USBCMD, 868 xhci_op_write_4(sc, XHCI_USBCMD,
869 xhci_op_read_4(sc, XHCI_USBCMD) | XHCI_CMD_CSS); 869 xhci_op_read_4(sc, XHCI_USBCMD) | XHCI_CMD_CSS);
870 870
871 /* 871 /*
872 * `...and wait for the Save State Status (SSS) flag in the 872 * `...and wait for the Save State Status (SSS) flag in the
873 * USBSTS register (5.4.2) to transition to ``0''.' 873 * USBSTS register (5.4.2) to transition to ``0''.'
874 */ 874 */
875 for (i = 0; i < XHCI_WAIT_SSS; i++) { 875 for (i = 0; i < XHCI_WAIT_SSS; i++) {
876 if ((xhci_op_read_4(sc, XHCI_USBSTS) & XHCI_STS_SSS) == 0) 876 if ((xhci_op_read_4(sc, XHCI_USBSTS) & XHCI_STS_SSS) == 0)
877 break; 877 break;
878 usb_delay_ms(&sc->sc_bus, 1); 878 usb_delay_ms(&sc->sc_bus, 1);
879 } 879 }
880 if (i >= XHCI_WAIT_SSS) { 880 if (i >= XHCI_WAIT_SSS) {
881 device_printf(self, "suspend timeout, USBSTS.SSS\n"); 881 device_printf(self, "suspend timeout, USBSTS.SSS\n");
882 /* 882 /*
883 * Just optimistically go on and check SRE anyway -- 883 * Just optimistically go on and check SRE anyway --
884 * what's the worst that could happen? 884 * what's the worst that could happen?
885 */ 885 */
886 } 886 }
887 887
888 /* 888 /*
889 * `Note: After a Save or Restore operation completes, the 889 * `Note: After a Save or Restore operation completes, the
890 * Save/Restore Error (SRE) flag in the USBSTS register should 890 * Save/Restore Error (SRE) flag in the USBSTS register should
891 * be checked to ensure that the operation completed 891 * be checked to ensure that the operation completed
892 * successfully.' 892 * successfully.'
893 */ 893 */
894 if (xhci_op_read_4(sc, XHCI_USBSTS) & XHCI_STS_SRE) { 894 if (xhci_op_read_4(sc, XHCI_USBSTS) & XHCI_STS_SRE) {
895 device_printf(self, "suspend error, USBSTS.SRE\n"); 895 device_printf(self, "suspend error, USBSTS.SRE\n");
896 goto out; 896 goto out;
897 } 897 }
898 898
899 /* Success! */ 899 /* Success! */
900 ok = true; 900 ok = true;
901 901
902out: mutex_exit(&sc->sc_lock); 902out: mutex_exit(&sc->sc_lock);
903 return ok; 903 return ok;
904} 904}
905 905
906bool 906bool
907xhci_resume(device_t self, const pmf_qual_t *qual) 907xhci_resume(device_t self, const pmf_qual_t *qual)
908{ 908{
909 struct xhci_softc * const sc = device_private(self); 909 struct xhci_softc * const sc = device_private(self);
910 size_t i, j, bn, dci; 910 size_t i, j, bn, dci;
911 int port; 911 int port;
912 uint32_t v; 912 uint32_t v;
913 bool ok = false; 913 bool ok = false;
914 914
915 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 915 XHCIHIST_FUNC(); XHCIHIST_CALLED();
916 916
917 mutex_enter(&sc->sc_lock); 917 mutex_enter(&sc->sc_lock);
918 KASSERT(sc->sc_suspender); 918 KASSERT(sc->sc_suspender);
919 919
920 /* 920 /*
921 * xHCI Requirements Specification 1.2, May 2019, Sec. 4.23.2: 921 * xHCI Requirements Specification 1.2, May 2019, Sec. 4.23.2:
922 * xHCI Power Management, p. 343 922 * xHCI Power Management, p. 343
923 * https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/extensible-host-controler-interface-usb-xhci.pdf#page=343 923 * https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/extensible-host-controler-interface-usb-xhci.pdf#page=343
924 */ 924 */
925 925
926 /* 926 /*
927 * `4. Restore the Operational Runtime, and VTIO registers with 927 * `4. Restore the Operational Runtime, and VTIO registers with
928 * their previously saved state in the following order: 928 * their previously saved state in the following order:
929 * DNCTRL, DCBAAP, CONFIG, ERSTSZ, ERSTBA, ERDP, IMAN, 929 * DNCTRL, DCBAAP, CONFIG, ERSTSZ, ERSTBA, ERDP, IMAN,
930 * IMOD, and VTIO.' 930 * IMOD, and VTIO.'
931 * 931 *
932 * (We don't use VTIO here (for now?).) 932 * (We don't use VTIO here (for now?).)
933 */ 933 */
934 xhci_op_write_4(sc, XHCI_USBCMD, sc->sc_regs.usbcmd); 934 xhci_op_write_4(sc, XHCI_USBCMD, sc->sc_regs.usbcmd);
935 xhci_op_write_4(sc, XHCI_DNCTRL, sc->sc_regs.dnctrl); 935 xhci_op_write_4(sc, XHCI_DNCTRL, sc->sc_regs.dnctrl);
936 xhci_op_write_8(sc, XHCI_DCBAAP, sc->sc_regs.dcbaap); 936 xhci_op_write_8(sc, XHCI_DCBAAP, sc->sc_regs.dcbaap);
937 xhci_op_write_4(sc, XHCI_CONFIG, sc->sc_regs.config); 937 xhci_op_write_4(sc, XHCI_CONFIG, sc->sc_regs.config);
938 xhci_rt_write_4(sc, XHCI_ERSTSZ(0), sc->sc_regs.erstsz0); 938 xhci_rt_write_4(sc, XHCI_ERSTSZ(0), sc->sc_regs.erstsz0);
939 xhci_rt_write_8(sc, XHCI_ERSTBA(0), sc->sc_regs.erstba0); 939 xhci_rt_write_8(sc, XHCI_ERSTBA(0), sc->sc_regs.erstba0);
940 xhci_rt_write_8(sc, XHCI_ERDP(0), sc->sc_regs.erdp0); 940 xhci_rt_write_8(sc, XHCI_ERDP(0), sc->sc_regs.erdp0);
941 xhci_rt_write_4(sc, XHCI_IMAN(0), sc->sc_regs.iman0); 941 xhci_rt_write_4(sc, XHCI_IMAN(0), sc->sc_regs.iman0);
942 xhci_rt_write_4(sc, XHCI_IMOD(0), sc->sc_regs.imod0); 942 xhci_rt_write_4(sc, XHCI_IMOD(0), sc->sc_regs.imod0);
943 943
944 memset(&sc->sc_regs, 0, sizeof(sc->sc_regs)); /* paranoia */ 944 memset(&sc->sc_regs, 0, sizeof(sc->sc_regs)); /* paranoia */
945 945
946 /* 946 /*
947 * `5. Set the Controller Restore State (CRS) flag in the 947 * `5. Set the Controller Restore State (CRS) flag in the
948 * USBCMD register (5.4.1) to ``1''...' 948 * USBCMD register (5.4.1) to ``1''...'
949 */ 949 */
950 xhci_op_write_4(sc, XHCI_USBCMD, 950 xhci_op_write_4(sc, XHCI_USBCMD,
951 xhci_op_read_4(sc, XHCI_USBCMD) | XHCI_CMD_CRS); 951 xhci_op_read_4(sc, XHCI_USBCMD) | XHCI_CMD_CRS);
952 952
953 /* 953 /*
954 * `...and wait for the Restore State Status (RSS) in the 954 * `...and wait for the Restore State Status (RSS) in the
955 * USBSTS register (5.4.2) to transition to ``0''.' 955 * USBSTS register (5.4.2) to transition to ``0''.'
956 */ 956 */
957 for (i = 0; i < XHCI_WAIT_RSS; i++) { 957 for (i = 0; i < XHCI_WAIT_RSS; i++) {
958 if ((xhci_op_read_4(sc, XHCI_USBSTS) & XHCI_STS_RSS) == 0) 958 if ((xhci_op_read_4(sc, XHCI_USBSTS) & XHCI_STS_RSS) == 0)
959 break; 959 break;
960 usb_delay_ms(&sc->sc_bus, 1); 960 usb_delay_ms(&sc->sc_bus, 1);
961 } 961 }
962 if (i >= XHCI_WAIT_RSS) { 962 if (i >= XHCI_WAIT_RSS) {
963 device_printf(self, "suspend timeout, USBSTS.RSS\n"); 963 device_printf(self, "suspend timeout, USBSTS.RSS\n");
964 goto out; 964 goto out;
965 } 965 }
966 966
967 /* 967 /*
968 * `6. Reinitialize the Command Ring, i.e. so its Cycle bits 968 * `6. Reinitialize the Command Ring, i.e. so its Cycle bits
969 * are consistent with the RCS values to be written to the 969 * are consistent with the RCS values to be written to the
970 * CRCR.' 970 * CRCR.'
971 * 971 *
972 * XXX Hope just zeroing it is good enough! 972 * XXX Hope just zeroing it is good enough!
973 */ 973 */
974 xhci_host_dequeue(sc->sc_cr); 974 xhci_host_dequeue(sc->sc_cr);
975 975
976 /* 976 /*
977 * `7. Write the CRCR with the address and RCS value of the 977 * `7. Write the CRCR with the address and RCS value of the
978 * reinitialized Command Ring. Note that this write will 978 * reinitialized Command Ring. Note that this write will
979 * cause the Command Ring to restart at the address 979 * cause the Command Ring to restart at the address
980 * specified by the CRCR.' 980 * specified by the CRCR.'
981 */ 981 */
982 xhci_op_write_8(sc, XHCI_CRCR, xhci_ring_trbp(sc->sc_cr, 0) | 982 xhci_op_write_8(sc, XHCI_CRCR, xhci_ring_trbp(sc->sc_cr, 0) |
983 sc->sc_cr->xr_cs); 983 sc->sc_cr->xr_cs);
984 984
985 /* 985 /*
986 * `8. Enable the controller by setting Run/Stop (R/S) = 986 * `8. Enable the controller by setting Run/Stop (R/S) =
987 * ``1''.' 987 * ``1''.'
988 */ 988 */
989 xhci_op_write_4(sc, XHCI_USBCMD, 989 xhci_op_write_4(sc, XHCI_USBCMD,
990 xhci_op_read_4(sc, XHCI_USBCMD) | XHCI_CMD_RS); 990 xhci_op_read_4(sc, XHCI_USBCMD) | XHCI_CMD_RS);
991 991
992 /* 992 /*
993 * `9. Software shall walk the USB topology and initialize each 993 * `9. Software shall walk the USB topology and initialize each
994 * of the xHC PORTSC, PORTPMSC, and PORTLI registers, and 994 * of the xHC PORTSC, PORTPMSC, and PORTLI registers, and
995 * external hub ports attached to USB devices.' 995 * external hub ports attached to USB devices.'
996 * 996 *
997 * This follows the procedure in 4.15 `Suspend-Resume', 4.15.2 997 * This follows the procedure in 4.15 `Suspend-Resume', 4.15.2
998 * `Port Resume', 4.15.2.1 `Host Initiated'. 998 * `Port Resume', 4.15.2.1 `Host Initiated'.
999 * 999 *
1000 * XXX We should maybe batch up initiating the state 1000 * XXX We should maybe batch up initiating the state
1001 * transitions, and then wait for them to complete all at once. 1001 * transitions, and then wait for them to complete all at once.
1002 */ 1002 */
1003 for (bn = 0; bn < 2; bn++) { 1003 for (bn = 0; bn < 2; bn++) {
1004 for (i = 1; i <= sc->sc_rhportcount[bn]; i++) { 1004 for (i = 1; i <= sc->sc_rhportcount[bn]; i++) {
1005 port = XHCI_PORTSC(xhci_rhport2ctlrport(sc, bn, i)); 1005 port = XHCI_PORTSC(xhci_rhport2ctlrport(sc, bn, i));
1006 1006
1007 /* `When a port is in the U3 state: ...' */ 1007 /* `When a port is in the U3 state: ...' */
1008 v = xhci_op_read_4(sc, port); 1008 v = xhci_op_read_4(sc, port);
1009 if (XHCI_PS_PLS_GET(v) != XHCI_PS_PLS_U3) 1009 if (XHCI_PS_PLS_GET(v) != XHCI_PS_PLS_U3)
1010 continue; 1010 continue;
1011 1011
1012 /* 1012 /*
1013 * `For a USB2 protocol port, software shall 1013 * `For a USB2 protocol port, software shall
1014 * write a ``15'' (Resume) to the PLS field to 1014 * write a ``15'' (Resume) to the PLS field to
1015 * initiate resume signaling. The port shall 1015 * initiate resume signaling. The port shall
1016 * transition to the Resume substate and the 1016 * transition to the Resume substate and the
1017 * xHC shall transmit the resume signaling 1017 * xHC shall transmit the resume signaling
1018 * within 1ms (T_URSM). Software shall ensure 1018 * within 1ms (T_URSM). Software shall ensure
1019 * that resume is signaled for at least 20ms 1019 * that resume is signaled for at least 20ms
1020 * (T_DRSMDN). Software shall start timing 1020 * (T_DRSMDN). Software shall start timing
1021 * T_DRSMDN from the write of ``15'' (Resume) 1021 * T_DRSMDN from the write of ``15'' (Resume)
1022 * to PLS.' 1022 * to PLS.'
1023 */ 1023 */
1024 if (bn == 1) { 1024 if (bn == 1) {
1025 KASSERT(sc->sc_bus2.ub_revision == USBREV_2_0); 1025 KASSERT(sc->sc_bus2.ub_revision == USBREV_2_0);
1026 v &= ~(XHCI_PS_PLS_MASK | XHCI_PS_CLEAR); 1026 v &= ~(XHCI_PS_PLS_MASK | XHCI_PS_CLEAR);
1027 v |= XHCI_PS_LWS; 1027 v |= XHCI_PS_LWS;
1028 v |= XHCI_PS_PLS_SET(XHCI_PS_PLS_SETRESUME); 1028 v |= XHCI_PS_PLS_SET(XHCI_PS_PLS_SETRESUME);
1029 xhci_op_write_4(sc, port, v); 1029 xhci_op_write_4(sc, port, v);
1030 usb_delay_ms(&sc->sc_bus, 20); 1030 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
1031 } else { 1031 } else {
1032 KASSERT(sc->sc_bus.ub_revision > USBREV_2_0); 1032 KASSERT(sc->sc_bus.ub_revision > USBREV_2_0);
1033 } 1033 }
1034 1034
1035 /* 1035 /*
1036 * `For a USB3 protocol port [and a USB2 1036 * `For a USB3 protocol port [and a USB2
1037 * protocol port after transitioning to 1037 * protocol port after transitioning to
1038 * Resume], software shall write a ``0'' (U0) 1038 * Resume], software shall write a ``0'' (U0)
1039 * to the PLS field...' 1039 * to the PLS field...'
1040 */ 1040 */
1041 v = xhci_op_read_4(sc, port); 1041 v = xhci_op_read_4(sc, port);
1042 v &= ~(XHCI_PS_PLS_MASK | XHCI_PS_CLEAR); 1042 v &= ~(XHCI_PS_PLS_MASK | XHCI_PS_CLEAR);
1043 v |= XHCI_PS_LWS | XHCI_PS_PLS_SET(XHCI_PS_PLS_SETU0); 1043 v |= XHCI_PS_LWS | XHCI_PS_PLS_SET(XHCI_PS_PLS_SETU0);
1044 xhci_op_write_4(sc, port, v); 1044 xhci_op_write_4(sc, port, v);
1045 1045
1046 for (j = 0; j < XHCI_WAIT_PLS_U0; j++) { 1046 for (j = 0; j < XHCI_WAIT_PLS_U0; j++) {
1047 v = xhci_op_read_4(sc, port); 1047 v = xhci_op_read_4(sc, port);
1048 if (XHCI_PS_PLS_GET(v) == XHCI_PS_PLS_U0) 1048 if (XHCI_PS_PLS_GET(v) == XHCI_PS_PLS_U0)
1049 break; 1049 break;
1050 usb_delay_ms(&sc->sc_bus, 1); 1050 usb_delay_ms(&sc->sc_bus, 1);
1051 } 1051 }
1052 if (j == XHCI_WAIT_PLS_U0) { 1052 if (j == XHCI_WAIT_PLS_U0) {
1053 device_printf(self, 1053 device_printf(self,
1054 "resume timeout on bus %zu port %zu\n", 1054 "resume timeout on bus %zu port %zu\n",
1055 bn, i); 1055 bn, i);
1056 goto out; 1056 goto out;
1057 } 1057 }
1058 } 1058 }
1059 } 1059 }
1060 1060
1061 /* 1061 /*
1062 * `10. Restart each of the previously Running endpoints by 1062 * `10. Restart each of the previously Running endpoints by
1063 * ringing their doorbells.' 1063 * ringing their doorbells.'
1064 */ 1064 */
1065 for (i = 0; i < sc->sc_maxslots; i++) { 1065 for (i = 0; i < sc->sc_maxslots; i++) {
1066 struct xhci_slot *xs = &sc->sc_slots[i]; 1066 struct xhci_slot *xs = &sc->sc_slots[i];
1067 1067
1068 /* Skip if the slot is not in use. */ 1068 /* Skip if the slot is not in use. */
1069 if (xs->xs_idx == 0) 1069 if (xs->xs_idx == 0)
1070 continue; 1070 continue;
1071 1071
1072 for (dci = XHCI_DCI_SLOT; dci <= XHCI_MAX_DCI; dci++) { 1072 for (dci = XHCI_DCI_SLOT; dci <= XHCI_MAX_DCI; dci++) {
1073 /* Skip if the endpoint is not Running. */ 1073 /* Skip if the endpoint is not Running. */
1074 if (xhci_get_epstate(sc, xs, dci) != 1074 if (xhci_get_epstate(sc, xs, dci) !=
1075 XHCI_EPSTATE_RUNNING) 1075 XHCI_EPSTATE_RUNNING)
1076 continue; 1076 continue;
1077 1077
1078 /* Ring the doorbell. */ 1078 /* Ring the doorbell. */
1079 xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci); 1079 xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci);
1080 } 1080 }
1081 } 1081 }
1082 1082
1083 /* 1083 /*
1084 * `Note: After a Save or Restore operation completes, the 1084 * `Note: After a Save or Restore operation completes, the
1085 * Save/Restore Error (SRE) flag in the USBSTS register should 1085 * Save/Restore Error (SRE) flag in the USBSTS register should
1086 * be checked to ensure that the operation completed 1086 * be checked to ensure that the operation completed
1087 * successfully.' 1087 * successfully.'
1088 */ 1088 */
1089 if (xhci_op_read_4(sc, XHCI_USBSTS) & XHCI_STS_SRE) { 1089 if (xhci_op_read_4(sc, XHCI_USBSTS) & XHCI_STS_SRE) {
1090 device_printf(self, "resume error, USBSTS.SRE\n"); 1090 device_printf(self, "resume error, USBSTS.SRE\n");
1091 goto out; 1091 goto out;
1092 } 1092 }
1093 1093
1094 /* Resume command issuance. */ 1094 /* Resume command issuance. */
1095 sc->sc_suspender = NULL; 1095 sc->sc_suspender = NULL;
1096 cv_broadcast(&sc->sc_cmdbusy_cv); 1096 cv_broadcast(&sc->sc_cmdbusy_cv);
1097 1097
1098 /* Success! */ 1098 /* Success! */
1099 ok = true; 1099 ok = true;
1100 1100
1101out: mutex_exit(&sc->sc_lock); 1101out: mutex_exit(&sc->sc_lock);
1102 return ok; 1102 return ok;
1103} 1103}
1104 1104
1105bool 1105bool
1106xhci_shutdown(device_t self, int flags) 1106xhci_shutdown(device_t self, int flags)
1107{ 1107{
1108 return false; 1108 return false;
1109} 1109}
1110 1110
1111static int 1111static int
1112xhci_hc_reset(struct xhci_softc * const sc) 1112xhci_hc_reset(struct xhci_softc * const sc)
1113{ 1113{
1114 uint32_t usbcmd, usbsts; 1114 uint32_t usbcmd, usbsts;
1115 int i; 1115 int i;
1116 1116
1117 /* Check controller not ready */ 1117 /* Check controller not ready */
1118 for (i = 0; i < XHCI_WAIT_CNR; i++) { 1118 for (i = 0; i < XHCI_WAIT_CNR; i++) {
1119 usbsts = xhci_op_read_4(sc, XHCI_USBSTS); 1119 usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
1120 if ((usbsts & XHCI_STS_CNR) == 0) 1120 if ((usbsts & XHCI_STS_CNR) == 0)
1121 break; 1121 break;
1122 usb_delay_ms(&sc->sc_bus, 1); 1122 usb_delay_ms(&sc->sc_bus, 1);
1123 } 1123 }
1124 if (i >= XHCI_WAIT_CNR) { 1124 if (i >= XHCI_WAIT_CNR) {
1125 aprint_error_dev(sc->sc_dev, "controller not ready timeout\n"); 1125 aprint_error_dev(sc->sc_dev, "controller not ready timeout\n");
1126 return EIO; 1126 return EIO;
1127 } 1127 }
1128 1128
1129 /* Halt controller */ 1129 /* Halt controller */
1130 usbcmd = 0; 1130 usbcmd = 0;
1131 xhci_op_write_4(sc, XHCI_USBCMD, usbcmd); 1131 xhci_op_write_4(sc, XHCI_USBCMD, usbcmd);
1132 usb_delay_ms(&sc->sc_bus, 1); 1132 usb_delay_ms(&sc->sc_bus, 1);
1133 1133
1134 /* Reset controller */ 1134 /* Reset controller */
1135 usbcmd = XHCI_CMD_HCRST; 1135 usbcmd = XHCI_CMD_HCRST;
1136 xhci_op_write_4(sc, XHCI_USBCMD, usbcmd); 1136 xhci_op_write_4(sc, XHCI_USBCMD, usbcmd);
1137 for (i = 0; i < XHCI_WAIT_HCRST; i++) { 1137 for (i = 0; i < XHCI_WAIT_HCRST; i++) {
1138 /* 1138 /*
1139 * Wait 1ms first. Existing Intel xHCI requies 1ms delay to 1139 * Wait 1ms first. Existing Intel xHCI requies 1ms delay to
1140 * prevent system hang (Errata). 1140 * prevent system hang (Errata).
1141 */ 1141 */
1142 usb_delay_ms(&sc->sc_bus, 1); 1142 usb_delay_ms(&sc->sc_bus, 1);
1143 usbcmd = xhci_op_read_4(sc, XHCI_USBCMD); 1143 usbcmd = xhci_op_read_4(sc, XHCI_USBCMD);
1144 if ((usbcmd & XHCI_CMD_HCRST) == 0) 1144 if ((usbcmd & XHCI_CMD_HCRST) == 0)
1145 break; 1145 break;
1146 } 1146 }
1147 if (i >= XHCI_WAIT_HCRST) { 1147 if (i >= XHCI_WAIT_HCRST) {
1148 aprint_error_dev(sc->sc_dev, "host controller reset timeout\n"); 1148 aprint_error_dev(sc->sc_dev, "host controller reset timeout\n");
1149 return EIO; 1149 return EIO;
1150 } 1150 }
1151 1151
1152 /* Check controller not ready */ 1152 /* Check controller not ready */
1153 for (i = 0; i < XHCI_WAIT_CNR; i++) { 1153 for (i = 0; i < XHCI_WAIT_CNR; i++) {
1154 usbsts = xhci_op_read_4(sc, XHCI_USBSTS); 1154 usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
1155 if ((usbsts & XHCI_STS_CNR) == 0) 1155 if ((usbsts & XHCI_STS_CNR) == 0)
1156 break; 1156 break;
1157 usb_delay_ms(&sc->sc_bus, 1); 1157 usb_delay_ms(&sc->sc_bus, 1);
1158 } 1158 }
1159 if (i >= XHCI_WAIT_CNR) { 1159 if (i >= XHCI_WAIT_CNR) {
1160 aprint_error_dev(sc->sc_dev, 1160 aprint_error_dev(sc->sc_dev,
1161 "controller not ready timeout after reset\n"); 1161 "controller not ready timeout after reset\n");
1162 return EIO; 1162 return EIO;
1163 } 1163 }
1164 1164
1165 return 0; 1165 return 0;
1166} 1166}
1167 1167
1168/* 7.2 xHCI Support Protocol Capability */ 1168/* 7.2 xHCI Support Protocol Capability */
1169static void 1169static void
1170xhci_id_protocols(struct xhci_softc *sc, bus_size_t ecp) 1170xhci_id_protocols(struct xhci_softc *sc, bus_size_t ecp)
1171{ 1171{
1172 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1172 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1173 1173
1174 /* XXX Cache this lot */ 1174 /* XXX Cache this lot */
1175 1175
1176 const uint32_t w0 = xhci_read_4(sc, ecp); 1176 const uint32_t w0 = xhci_read_4(sc, ecp);
1177 const uint32_t w4 = xhci_read_4(sc, ecp + 4); 1177 const uint32_t w4 = xhci_read_4(sc, ecp + 4);
1178 const uint32_t w8 = xhci_read_4(sc, ecp + 8); 1178 const uint32_t w8 = xhci_read_4(sc, ecp + 8);
1179 const uint32_t wc = xhci_read_4(sc, ecp + 0xc); 1179 const uint32_t wc = xhci_read_4(sc, ecp + 0xc);
1180 1180
1181 aprint_debug_dev(sc->sc_dev, 1181 aprint_debug_dev(sc->sc_dev,
1182 " SP: 0x%08x 0x%08x 0x%08x 0x%08x\n", w0, w4, w8, wc); 1182 " SP: 0x%08x 0x%08x 0x%08x 0x%08x\n", w0, w4, w8, wc);
1183 1183
1184 if (w4 != XHCI_XECP_USBID) 1184 if (w4 != XHCI_XECP_USBID)
1185 return; 1185 return;
1186 1186
1187 const int major = XHCI_XECP_SP_W0_MAJOR(w0); 1187 const int major = XHCI_XECP_SP_W0_MAJOR(w0);
1188 const int minor = XHCI_XECP_SP_W0_MINOR(w0); 1188 const int minor = XHCI_XECP_SP_W0_MINOR(w0);
1189 const uint8_t cpo = XHCI_XECP_SP_W8_CPO(w8); 1189 const uint8_t cpo = XHCI_XECP_SP_W8_CPO(w8);
1190 const uint8_t cpc = XHCI_XECP_SP_W8_CPC(w8); 1190 const uint8_t cpc = XHCI_XECP_SP_W8_CPC(w8);
1191 1191
1192 const uint16_t mm = __SHIFTOUT(w0, __BITS(31, 16)); 1192 const uint16_t mm = __SHIFTOUT(w0, __BITS(31, 16));
1193 switch (mm) { 1193 switch (mm) {
1194 case 0x0200: 1194 case 0x0200:
1195 case 0x0300: 1195 case 0x0300:
1196 case 0x0301: 1196 case 0x0301:
1197 case 0x0310: 1197 case 0x0310:
1198 aprint_debug_dev(sc->sc_dev, " %s ports %d - %d\n", 1198 aprint_debug_dev(sc->sc_dev, " %s ports %d - %d\n",
1199 major == 3 ? "ss" : "hs", cpo, cpo + cpc -1); 1199 major == 3 ? "ss" : "hs", cpo, cpo + cpc -1);
1200 break; 1200 break;
1201 default: 1201 default:
1202 aprint_error_dev(sc->sc_dev, " unknown major/minor (%d/%d)\n", 1202 aprint_error_dev(sc->sc_dev, " unknown major/minor (%d/%d)\n",
1203 major, minor); 1203 major, minor);
1204 return; 1204 return;
1205 } 1205 }
1206 1206
1207 const size_t bus = (major == 3) ? 0 : 1; 1207 const size_t bus = (major == 3) ? 0 : 1;
1208 1208
1209 /* Index arrays with 0..n-1 where ports are numbered 1..n */ 1209 /* Index arrays with 0..n-1 where ports are numbered 1..n */
1210 for (size_t cp = cpo - 1; cp < cpo + cpc - 1; cp++) { 1210 for (size_t cp = cpo - 1; cp < cpo + cpc - 1; cp++) {
1211 if (sc->sc_ctlrportmap[cp] != 0) { 1211 if (sc->sc_ctlrportmap[cp] != 0) {
1212 aprint_error_dev(sc->sc_dev, "controller port %zu " 1212 aprint_error_dev(sc->sc_dev, "controller port %zu "
1213 "already assigned", cp); 1213 "already assigned", cp);
1214 continue; 1214 continue;
1215 } 1215 }
1216 1216
1217 sc->sc_ctlrportbus[cp / NBBY] |= 1217 sc->sc_ctlrportbus[cp / NBBY] |=
1218 bus == 0 ? 0 : __BIT(cp % NBBY); 1218 bus == 0 ? 0 : __BIT(cp % NBBY);
1219 1219
1220 const size_t rhp = sc->sc_rhportcount[bus]++; 1220 const size_t rhp = sc->sc_rhportcount[bus]++;
1221 1221
1222 KASSERTMSG(sc->sc_rhportmap[bus][rhp] == 0, 1222 KASSERTMSG(sc->sc_rhportmap[bus][rhp] == 0,
1223 "bus %zu rhp %zu is %d", bus, rhp, 1223 "bus %zu rhp %zu is %d", bus, rhp,
1224 sc->sc_rhportmap[bus][rhp]); 1224 sc->sc_rhportmap[bus][rhp]);
1225 1225
1226 sc->sc_rhportmap[bus][rhp] = cp + 1; 1226 sc->sc_rhportmap[bus][rhp] = cp + 1;
1227 sc->sc_ctlrportmap[cp] = rhp + 1; 1227 sc->sc_ctlrportmap[cp] = rhp + 1;
1228 } 1228 }
1229} 1229}
1230 1230
1231/* Process extended capabilities */ 1231/* Process extended capabilities */
1232static void 1232static void
1233xhci_ecp(struct xhci_softc *sc) 1233xhci_ecp(struct xhci_softc *sc)
1234{ 1234{
1235 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1235 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1236 1236
1237 bus_size_t ecp = XHCI_HCC_XECP(sc->sc_hcc) * 4; 1237 bus_size_t ecp = XHCI_HCC_XECP(sc->sc_hcc) * 4;
1238 while (ecp != 0) { 1238 while (ecp != 0) {
1239 uint32_t ecr = xhci_read_4(sc, ecp); 1239 uint32_t ecr = xhci_read_4(sc, ecp);
1240 aprint_debug_dev(sc->sc_dev, "ECR: 0x%08x\n", ecr); 1240 aprint_debug_dev(sc->sc_dev, "ECR: 0x%08x\n", ecr);
1241 switch (XHCI_XECP_ID(ecr)) { 1241 switch (XHCI_XECP_ID(ecr)) {
1242 case XHCI_ID_PROTOCOLS: { 1242 case XHCI_ID_PROTOCOLS: {
1243 xhci_id_protocols(sc, ecp); 1243 xhci_id_protocols(sc, ecp);
1244 break; 1244 break;
1245 } 1245 }
1246 case XHCI_ID_USB_LEGACY: { 1246 case XHCI_ID_USB_LEGACY: {
1247 uint8_t bios_sem; 1247 uint8_t bios_sem;
1248 1248
1249 /* Take host controller ownership from BIOS */ 1249 /* Take host controller ownership from BIOS */
1250 bios_sem = xhci_read_1(sc, ecp + XHCI_XECP_BIOS_SEM); 1250 bios_sem = xhci_read_1(sc, ecp + XHCI_XECP_BIOS_SEM);
1251 if (bios_sem) { 1251 if (bios_sem) {
1252 /* sets xHCI to be owned by OS */ 1252 /* sets xHCI to be owned by OS */
1253 xhci_write_1(sc, ecp + XHCI_XECP_OS_SEM, 1); 1253 xhci_write_1(sc, ecp + XHCI_XECP_OS_SEM, 1);
1254 aprint_debug_dev(sc->sc_dev, 1254 aprint_debug_dev(sc->sc_dev,
1255 "waiting for BIOS to give up control\n"); 1255 "waiting for BIOS to give up control\n");
1256 for (int i = 0; i < 5000; i++) { 1256 for (int i = 0; i < 5000; i++) {
1257 bios_sem = xhci_read_1(sc, ecp + 1257 bios_sem = xhci_read_1(sc, ecp +
1258 XHCI_XECP_BIOS_SEM); 1258 XHCI_XECP_BIOS_SEM);
1259 if (bios_sem == 0) 1259 if (bios_sem == 0)
1260 break; 1260 break;
1261 DELAY(1000); 1261 DELAY(1000);
1262 } 1262 }
1263 if (bios_sem) { 1263 if (bios_sem) {
1264 aprint_error_dev(sc->sc_dev, 1264 aprint_error_dev(sc->sc_dev,
1265 "timed out waiting for BIOS\n"); 1265 "timed out waiting for BIOS\n");
1266 } 1266 }
1267 } 1267 }
1268 break; 1268 break;
1269 } 1269 }
1270 default: 1270 default:
1271 break; 1271 break;
1272 } 1272 }
1273 ecr = xhci_read_4(sc, ecp); 1273 ecr = xhci_read_4(sc, ecp);
1274 if (XHCI_XECP_NEXT(ecr) == 0) { 1274 if (XHCI_XECP_NEXT(ecr) == 0) {
1275 ecp = 0; 1275 ecp = 0;
1276 } else { 1276 } else {
1277 ecp += XHCI_XECP_NEXT(ecr) * 4; 1277 ecp += XHCI_XECP_NEXT(ecr) * 4;
1278 } 1278 }
1279 } 1279 }
1280} 1280}
1281 1281
1282#define XHCI_HCCPREV1_BITS \ 1282#define XHCI_HCCPREV1_BITS \
1283 "\177\020" /* New bitmask */ \ 1283 "\177\020" /* New bitmask */ \
1284 "f\020\020XECP\0" \ 1284 "f\020\020XECP\0" \
1285 "f\014\4MAXPSA\0" \ 1285 "f\014\4MAXPSA\0" \
1286 "b\013CFC\0" \ 1286 "b\013CFC\0" \
1287 "b\012SEC\0" \ 1287 "b\012SEC\0" \
1288 "b\011SBD\0" \ 1288 "b\011SBD\0" \
1289 "b\010FSE\0" \ 1289 "b\010FSE\0" \
1290 "b\7NSS\0" \ 1290 "b\7NSS\0" \
1291 "b\6LTC\0" \ 1291 "b\6LTC\0" \
1292 "b\5LHRC\0" \ 1292 "b\5LHRC\0" \
1293 "b\4PIND\0" \ 1293 "b\4PIND\0" \
1294 "b\3PPC\0" \ 1294 "b\3PPC\0" \
1295 "b\2CZC\0" \ 1295 "b\2CZC\0" \
1296 "b\1BNC\0" \ 1296 "b\1BNC\0" \
1297 "b\0AC64\0" \ 1297 "b\0AC64\0" \
1298 "\0" 1298 "\0"
1299#define XHCI_HCCV1_x_BITS \ 1299#define XHCI_HCCV1_x_BITS \
1300 "\177\020" /* New bitmask */ \ 1300 "\177\020" /* New bitmask */ \
1301 "f\020\020XECP\0" \ 1301 "f\020\020XECP\0" \
1302 "f\014\4MAXPSA\0" \ 1302 "f\014\4MAXPSA\0" \
1303 "b\013CFC\0" \ 1303 "b\013CFC\0" \
1304 "b\012SEC\0" \ 1304 "b\012SEC\0" \
1305 "b\011SPC\0" \ 1305 "b\011SPC\0" \
1306 "b\010PAE\0" \ 1306 "b\010PAE\0" \
1307 "b\7NSS\0" \ 1307 "b\7NSS\0" \
1308 "b\6LTC\0" \ 1308 "b\6LTC\0" \
1309 "b\5LHRC\0" \ 1309 "b\5LHRC\0" \
1310 "b\4PIND\0" \ 1310 "b\4PIND\0" \
1311 "b\3PPC\0" \ 1311 "b\3PPC\0" \
1312 "b\2CSZ\0" \ 1312 "b\2CSZ\0" \
1313 "b\1BNC\0" \ 1313 "b\1BNC\0" \
1314 "b\0AC64\0" \ 1314 "b\0AC64\0" \
1315 "\0" 1315 "\0"
1316 1316
1317#define XHCI_HCC2_BITS \ 1317#define XHCI_HCC2_BITS \
1318 "\177\020" /* New bitmask */ \ 1318 "\177\020" /* New bitmask */ \
1319 "b\7ETC_TSC\0" \ 1319 "b\7ETC_TSC\0" \
1320 "b\6ETC\0" \ 1320 "b\6ETC\0" \
1321 "b\5CIC\0" \ 1321 "b\5CIC\0" \
1322 "b\4LEC\0" \ 1322 "b\4LEC\0" \
1323 "b\3CTC\0" \ 1323 "b\3CTC\0" \
1324 "b\2FSC\0" \ 1324 "b\2FSC\0" \
1325 "b\1CMC\0" \ 1325 "b\1CMC\0" \
1326 "b\0U3C\0" \ 1326 "b\0U3C\0" \
1327 "\0" 1327 "\0"
1328 1328
1329void 1329void
1330xhci_start(struct xhci_softc *sc) 1330xhci_start(struct xhci_softc *sc)
1331{ 1331{
1332 xhci_rt_write_4(sc, XHCI_IMAN(0), XHCI_IMAN_INTR_ENA); 1332 xhci_rt_write_4(sc, XHCI_IMAN(0), XHCI_IMAN_INTR_ENA);
1333 if ((sc->sc_quirks & XHCI_QUIRK_INTEL) != 0) 1333 if ((sc->sc_quirks & XHCI_QUIRK_INTEL) != 0)
1334 /* Intel xhci needs interrupt rate moderated. */ 1334 /* Intel xhci needs interrupt rate moderated. */
1335 xhci_rt_write_4(sc, XHCI_IMOD(0), XHCI_IMOD_DEFAULT_LP); 1335 xhci_rt_write_4(sc, XHCI_IMOD(0), XHCI_IMOD_DEFAULT_LP);
1336 else 1336 else
1337 xhci_rt_write_4(sc, XHCI_IMOD(0), 0); 1337 xhci_rt_write_4(sc, XHCI_IMOD(0), 0);
1338 aprint_debug_dev(sc->sc_dev, "current IMOD %u\n", 1338 aprint_debug_dev(sc->sc_dev, "current IMOD %u\n",
1339 xhci_rt_read_4(sc, XHCI_IMOD(0))); 1339 xhci_rt_read_4(sc, XHCI_IMOD(0)));
1340 1340
1341 /* Go! */ 1341 /* Go! */
1342 xhci_op_write_4(sc, XHCI_USBCMD, XHCI_CMD_INTE|XHCI_CMD_RS); 1342 xhci_op_write_4(sc, XHCI_USBCMD, XHCI_CMD_INTE|XHCI_CMD_RS);
1343 aprint_debug_dev(sc->sc_dev, "USBCMD 0x%08"PRIx32"\n", 1343 aprint_debug_dev(sc->sc_dev, "USBCMD 0x%08"PRIx32"\n",
1344 xhci_op_read_4(sc, XHCI_USBCMD)); 1344 xhci_op_read_4(sc, XHCI_USBCMD));
1345} 1345}
1346 1346
1347int 1347int
1348xhci_init(struct xhci_softc *sc) 1348xhci_init(struct xhci_softc *sc)
1349{ 1349{
1350 bus_size_t bsz; 1350 bus_size_t bsz;
1351 uint32_t hcs1, hcs2, hcs3, dboff, rtsoff; 1351 uint32_t hcs1, hcs2, hcs3, dboff, rtsoff;
1352 uint32_t pagesize, config; 1352 uint32_t pagesize, config;
1353 int i = 0; 1353 int i = 0;
1354 uint16_t hciversion; 1354 uint16_t hciversion;
1355 uint8_t caplength; 1355 uint8_t caplength;
1356 1356
1357 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1357 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1358 1358
1359 /* Set up the bus struct for the usb 3 and usb 2 buses */ 1359 /* Set up the bus struct for the usb 3 and usb 2 buses */
1360 sc->sc_bus.ub_methods = &xhci_bus_methods; 1360 sc->sc_bus.ub_methods = &xhci_bus_methods;
1361 sc->sc_bus.ub_pipesize = sizeof(struct xhci_pipe); 1361 sc->sc_bus.ub_pipesize = sizeof(struct xhci_pipe);
1362 sc->sc_bus.ub_usedma = true; 1362 sc->sc_bus.ub_usedma = true;
1363 sc->sc_bus.ub_hcpriv = sc; 1363 sc->sc_bus.ub_hcpriv = sc;
1364 1364
1365 sc->sc_bus2.ub_methods = &xhci_bus_methods; 1365 sc->sc_bus2.ub_methods = &xhci_bus_methods;
1366 sc->sc_bus2.ub_pipesize = sizeof(struct xhci_pipe); 1366 sc->sc_bus2.ub_pipesize = sizeof(struct xhci_pipe);
1367 sc->sc_bus2.ub_revision = USBREV_2_0; 1367 sc->sc_bus2.ub_revision = USBREV_2_0;
1368 sc->sc_bus2.ub_usedma = true; 1368 sc->sc_bus2.ub_usedma = true;
1369 sc->sc_bus2.ub_hcpriv = sc; 1369 sc->sc_bus2.ub_hcpriv = sc;
1370 sc->sc_bus2.ub_dmatag = sc->sc_bus.ub_dmatag; 1370 sc->sc_bus2.ub_dmatag = sc->sc_bus.ub_dmatag;
1371 1371
1372 caplength = xhci_read_1(sc, XHCI_CAPLENGTH); 1372 caplength = xhci_read_1(sc, XHCI_CAPLENGTH);
1373 hciversion = xhci_read_2(sc, XHCI_HCIVERSION); 1373 hciversion = xhci_read_2(sc, XHCI_HCIVERSION);
1374 1374
1375 if (hciversion < XHCI_HCIVERSION_0_96 || 1375 if (hciversion < XHCI_HCIVERSION_0_96 ||
1376 hciversion >= 0x0200) { 1376 hciversion >= 0x0200) {
1377 aprint_normal_dev(sc->sc_dev, 1377 aprint_normal_dev(sc->sc_dev,
1378 "xHCI version %x.%x not known to be supported\n", 1378 "xHCI version %x.%x not known to be supported\n",
1379 (hciversion >> 8) & 0xff, (hciversion >> 0) & 0xff); 1379 (hciversion >> 8) & 0xff, (hciversion >> 0) & 0xff);
1380 } else { 1380 } else {
1381 aprint_verbose_dev(sc->sc_dev, "xHCI version %x.%x\n", 1381 aprint_verbose_dev(sc->sc_dev, "xHCI version %x.%x\n",
1382 (hciversion >> 8) & 0xff, (hciversion >> 0) & 0xff); 1382 (hciversion >> 8) & 0xff, (hciversion >> 0) & 0xff);
1383 } 1383 }
1384 1384
1385 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0, caplength, 1385 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0, caplength,
1386 &sc->sc_cbh) != 0) { 1386 &sc->sc_cbh) != 0) {
1387 aprint_error_dev(sc->sc_dev, "capability subregion failure\n"); 1387 aprint_error_dev(sc->sc_dev, "capability subregion failure\n");
1388 return ENOMEM; 1388 return ENOMEM;
1389 } 1389 }
1390 1390
1391 hcs1 = xhci_cap_read_4(sc, XHCI_HCSPARAMS1); 1391 hcs1 = xhci_cap_read_4(sc, XHCI_HCSPARAMS1);
1392 sc->sc_maxslots = XHCI_HCS1_MAXSLOTS(hcs1); 1392 sc->sc_maxslots = XHCI_HCS1_MAXSLOTS(hcs1);
1393 sc->sc_maxintrs = XHCI_HCS1_MAXINTRS(hcs1); 1393 sc->sc_maxintrs = XHCI_HCS1_MAXINTRS(hcs1);
1394 sc->sc_maxports = XHCI_HCS1_MAXPORTS(hcs1); 1394 sc->sc_maxports = XHCI_HCS1_MAXPORTS(hcs1);
1395 hcs2 = xhci_cap_read_4(sc, XHCI_HCSPARAMS2); 1395 hcs2 = xhci_cap_read_4(sc, XHCI_HCSPARAMS2);
1396 hcs3 = xhci_cap_read_4(sc, XHCI_HCSPARAMS3); 1396 hcs3 = xhci_cap_read_4(sc, XHCI_HCSPARAMS3);
1397 aprint_debug_dev(sc->sc_dev, 1397 aprint_debug_dev(sc->sc_dev,
1398 "hcs1=%"PRIx32" hcs2=%"PRIx32" hcs3=%"PRIx32"\n", hcs1, hcs2, hcs3); 1398 "hcs1=%"PRIx32" hcs2=%"PRIx32" hcs3=%"PRIx32"\n", hcs1, hcs2, hcs3);
1399 1399
1400 sc->sc_hcc = xhci_cap_read_4(sc, XHCI_HCCPARAMS); 1400 sc->sc_hcc = xhci_cap_read_4(sc, XHCI_HCCPARAMS);
1401 sc->sc_ctxsz = XHCI_HCC_CSZ(sc->sc_hcc) ? 64 : 32; 1401 sc->sc_ctxsz = XHCI_HCC_CSZ(sc->sc_hcc) ? 64 : 32;
1402 1402
1403 char sbuf[128]; 1403 char sbuf[128];
1404 if (hciversion < XHCI_HCIVERSION_1_0) 1404 if (hciversion < XHCI_HCIVERSION_1_0)
1405 snprintb(sbuf, sizeof(sbuf), XHCI_HCCPREV1_BITS, sc->sc_hcc); 1405 snprintb(sbuf, sizeof(sbuf), XHCI_HCCPREV1_BITS, sc->sc_hcc);
1406 else 1406 else
1407 snprintb(sbuf, sizeof(sbuf), XHCI_HCCV1_x_BITS, sc->sc_hcc); 1407 snprintb(sbuf, sizeof(sbuf), XHCI_HCCV1_x_BITS, sc->sc_hcc);
1408 aprint_debug_dev(sc->sc_dev, "hcc=%s\n", sbuf); 1408 aprint_debug_dev(sc->sc_dev, "hcc=%s\n", sbuf);
1409 aprint_debug_dev(sc->sc_dev, "xECP %" __PRIxBITS "\n", 1409 aprint_debug_dev(sc->sc_dev, "xECP %" __PRIxBITS "\n",
1410 XHCI_HCC_XECP(sc->sc_hcc) * 4); 1410 XHCI_HCC_XECP(sc->sc_hcc) * 4);
1411 if (hciversion >= XHCI_HCIVERSION_1_1) { 1411 if (hciversion >= XHCI_HCIVERSION_1_1) {
1412 sc->sc_hcc2 = xhci_cap_read_4(sc, XHCI_HCCPARAMS2); 1412 sc->sc_hcc2 = xhci_cap_read_4(sc, XHCI_HCCPARAMS2);
1413 snprintb(sbuf, sizeof(sbuf), XHCI_HCC2_BITS, sc->sc_hcc2); 1413 snprintb(sbuf, sizeof(sbuf), XHCI_HCC2_BITS, sc->sc_hcc2);
1414 aprint_debug_dev(sc->sc_dev, "hcc2=%s\n", sbuf); 1414 aprint_debug_dev(sc->sc_dev, "hcc2=%s\n", sbuf);
1415 } 1415 }
1416 1416
1417 /* default all ports to bus 0, i.e. usb 3 */ 1417 /* default all ports to bus 0, i.e. usb 3 */
1418 sc->sc_ctlrportbus = kmem_zalloc( 1418 sc->sc_ctlrportbus = kmem_zalloc(
1419 howmany(sc->sc_maxports * sizeof(uint8_t), NBBY), KM_SLEEP); 1419 howmany(sc->sc_maxports * sizeof(uint8_t), NBBY), KM_SLEEP);
1420 sc->sc_ctlrportmap = kmem_zalloc(sc->sc_maxports * sizeof(int), KM_SLEEP); 1420 sc->sc_ctlrportmap = kmem_zalloc(sc->sc_maxports * sizeof(int), KM_SLEEP);
1421 1421
1422 /* controller port to bus roothub port map */ 1422 /* controller port to bus roothub port map */
1423 for (size_t j = 0; j < __arraycount(sc->sc_rhportmap); j++) { 1423 for (size_t j = 0; j < __arraycount(sc->sc_rhportmap); j++) {
1424 sc->sc_rhportmap[j] = kmem_zalloc(sc->sc_maxports * sizeof(int), KM_SLEEP); 1424 sc->sc_rhportmap[j] = kmem_zalloc(sc->sc_maxports * sizeof(int), KM_SLEEP);
1425 } 1425 }
1426 1426
1427 /* 1427 /*
1428 * Process all Extended Capabilities 1428 * Process all Extended Capabilities
1429 */ 1429 */
1430 xhci_ecp(sc); 1430 xhci_ecp(sc);
1431 1431
1432 bsz = XHCI_PORTSC(sc->sc_maxports); 1432 bsz = XHCI_PORTSC(sc->sc_maxports);
1433 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, caplength, bsz, 1433 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, caplength, bsz,
1434 &sc->sc_obh) != 0) { 1434 &sc->sc_obh) != 0) {
1435 aprint_error_dev(sc->sc_dev, "operational subregion failure\n"); 1435 aprint_error_dev(sc->sc_dev, "operational subregion failure\n");
1436 return ENOMEM; 1436 return ENOMEM;
1437 } 1437 }
1438 1438
1439 dboff = xhci_cap_read_4(sc, XHCI_DBOFF); 1439 dboff = xhci_cap_read_4(sc, XHCI_DBOFF);
1440 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, dboff, 1440 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, dboff,
1441 sc->sc_maxslots * 4, &sc->sc_dbh) != 0) { 1441 sc->sc_maxslots * 4, &sc->sc_dbh) != 0) {
1442 aprint_error_dev(sc->sc_dev, "doorbell subregion failure\n"); 1442 aprint_error_dev(sc->sc_dev, "doorbell subregion failure\n");
1443 return ENOMEM; 1443 return ENOMEM;
1444 } 1444 }
1445 1445
1446 rtsoff = xhci_cap_read_4(sc, XHCI_RTSOFF); 1446 rtsoff = xhci_cap_read_4(sc, XHCI_RTSOFF);
1447 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, rtsoff, 1447 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, rtsoff,
1448 sc->sc_maxintrs * 0x20, &sc->sc_rbh) != 0) { 1448 sc->sc_maxintrs * 0x20, &sc->sc_rbh) != 0) {
1449 aprint_error_dev(sc->sc_dev, "runtime subregion failure\n"); 1449 aprint_error_dev(sc->sc_dev, "runtime subregion failure\n");
1450 return ENOMEM; 1450 return ENOMEM;
1451 } 1451 }
1452 1452
1453 int rv; 1453 int rv;
1454 rv = xhci_hc_reset(sc); 1454 rv = xhci_hc_reset(sc);
1455 if (rv != 0) { 1455 if (rv != 0) {
1456 return rv; 1456 return rv;
1457 } 1457 }
1458 1458
1459 if (sc->sc_vendor_init) 1459 if (sc->sc_vendor_init)
1460 sc->sc_vendor_init(sc); 1460 sc->sc_vendor_init(sc);
1461 1461
1462 pagesize = xhci_op_read_4(sc, XHCI_PAGESIZE); 1462 pagesize = xhci_op_read_4(sc, XHCI_PAGESIZE);
1463 aprint_debug_dev(sc->sc_dev, "PAGESIZE 0x%08x\n", pagesize); 1463 aprint_debug_dev(sc->sc_dev, "PAGESIZE 0x%08x\n", pagesize);
1464 pagesize = ffs(pagesize); 1464 pagesize = ffs(pagesize);
1465 if (pagesize == 0) { 1465 if (pagesize == 0) {
1466 aprint_error_dev(sc->sc_dev, "pagesize is 0\n"); 1466 aprint_error_dev(sc->sc_dev, "pagesize is 0\n");
1467 return EIO; 1467 return EIO;
1468 } 1468 }
1469 sc->sc_pgsz = 1 << (12 + (pagesize - 1)); 1469 sc->sc_pgsz = 1 << (12 + (pagesize - 1));
1470 aprint_debug_dev(sc->sc_dev, "sc_pgsz 0x%08x\n", (uint32_t)sc->sc_pgsz); 1470 aprint_debug_dev(sc->sc_dev, "sc_pgsz 0x%08x\n", (uint32_t)sc->sc_pgsz);
1471 aprint_debug_dev(sc->sc_dev, "sc_maxslots 0x%08x\n", 1471 aprint_debug_dev(sc->sc_dev, "sc_maxslots 0x%08x\n",
1472 (uint32_t)sc->sc_maxslots); 1472 (uint32_t)sc->sc_maxslots);
1473 aprint_debug_dev(sc->sc_dev, "sc_maxports %d\n", sc->sc_maxports); 1473 aprint_debug_dev(sc->sc_dev, "sc_maxports %d\n", sc->sc_maxports);
1474 1474
1475 int err; 1475 int err;
1476 sc->sc_maxspbuf = XHCI_HCS2_MAXSPBUF(hcs2); 1476 sc->sc_maxspbuf = XHCI_HCS2_MAXSPBUF(hcs2);
1477 aprint_debug_dev(sc->sc_dev, "sc_maxspbuf %d\n", sc->sc_maxspbuf); 1477 aprint_debug_dev(sc->sc_dev, "sc_maxspbuf %d\n", sc->sc_maxspbuf);
1478 if (sc->sc_maxspbuf != 0) { 1478 if (sc->sc_maxspbuf != 0) {
1479 err = usb_allocmem(&sc->sc_bus, 1479 err = usb_allocmem(&sc->sc_bus,
1480 sizeof(uint64_t) * sc->sc_maxspbuf, sizeof(uint64_t), 1480 sizeof(uint64_t) * sc->sc_maxspbuf, sizeof(uint64_t),
1481 USBMALLOC_COHERENT | USBMALLOC_ZERO, 1481 USBMALLOC_COHERENT | USBMALLOC_ZERO,
1482 &sc->sc_spbufarray_dma); 1482 &sc->sc_spbufarray_dma);
1483 if (err) { 1483 if (err) {
1484 aprint_error_dev(sc->sc_dev, 1484 aprint_error_dev(sc->sc_dev,
1485 "spbufarray init fail, err %d\n", err); 1485 "spbufarray init fail, err %d\n", err);
1486 return ENOMEM; 1486 return ENOMEM;
1487 } 1487 }
1488 1488
1489 sc->sc_spbuf_dma = kmem_zalloc(sizeof(*sc->sc_spbuf_dma) * 1489 sc->sc_spbuf_dma = kmem_zalloc(sizeof(*sc->sc_spbuf_dma) *
1490 sc->sc_maxspbuf, KM_SLEEP); 1490 sc->sc_maxspbuf, KM_SLEEP);
1491 uint64_t *spbufarray = KERNADDR(&sc->sc_spbufarray_dma, 0); 1491 uint64_t *spbufarray = KERNADDR(&sc->sc_spbufarray_dma, 0);
1492 for (i = 0; i < sc->sc_maxspbuf; i++) { 1492 for (i = 0; i < sc->sc_maxspbuf; i++) {
1493 usb_dma_t * const dma = &sc->sc_spbuf_dma[i]; 1493 usb_dma_t * const dma = &sc->sc_spbuf_dma[i];
1494 /* allocate contexts */ 1494 /* allocate contexts */
1495 err = usb_allocmem(&sc->sc_bus, sc->sc_pgsz, 1495 err = usb_allocmem(&sc->sc_bus, sc->sc_pgsz,
1496 sc->sc_pgsz, USBMALLOC_COHERENT | USBMALLOC_ZERO, 1496 sc->sc_pgsz, USBMALLOC_COHERENT | USBMALLOC_ZERO,
1497 dma); 1497 dma);
1498 if (err) { 1498 if (err) {
1499 aprint_error_dev(sc->sc_dev, 1499 aprint_error_dev(sc->sc_dev,
1500 "spbufarray_dma init fail, err %d\n", err); 1500 "spbufarray_dma init fail, err %d\n", err);
1501 rv = ENOMEM; 1501 rv = ENOMEM;
1502 goto bad1; 1502 goto bad1;
1503 } 1503 }
1504 spbufarray[i] = htole64(DMAADDR(dma, 0)); 1504 spbufarray[i] = htole64(DMAADDR(dma, 0));
1505 usb_syncmem(dma, 0, sc->sc_pgsz, 1505 usb_syncmem(dma, 0, sc->sc_pgsz,
1506 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 1506 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1507 } 1507 }
1508 1508
1509 usb_syncmem(&sc->sc_spbufarray_dma, 0, 1509 usb_syncmem(&sc->sc_spbufarray_dma, 0,
1510 sizeof(uint64_t) * sc->sc_maxspbuf, BUS_DMASYNC_PREWRITE); 1510 sizeof(uint64_t) * sc->sc_maxspbuf, BUS_DMASYNC_PREWRITE);
1511 } 1511 }
1512 1512
1513 config = xhci_op_read_4(sc, XHCI_CONFIG); 1513 config = xhci_op_read_4(sc, XHCI_CONFIG);
1514 config &= ~0xFF; 1514 config &= ~0xFF;
1515 config |= sc->sc_maxslots & 0xFF; 1515 config |= sc->sc_maxslots & 0xFF;
1516 xhci_op_write_4(sc, XHCI_CONFIG, config); 1516 xhci_op_write_4(sc, XHCI_CONFIG, config);
1517 1517
1518 err = xhci_ring_init(sc, &sc->sc_cr, XHCI_COMMAND_RING_TRBS, 1518 err = xhci_ring_init(sc, &sc->sc_cr, XHCI_COMMAND_RING_TRBS,
1519 XHCI_COMMAND_RING_SEGMENTS_ALIGN); 1519 XHCI_COMMAND_RING_SEGMENTS_ALIGN);
1520 if (err) { 1520 if (err) {
1521 aprint_error_dev(sc->sc_dev, "command ring init fail, err %d\n", 1521 aprint_error_dev(sc->sc_dev, "command ring init fail, err %d\n",
1522 err); 1522 err);
1523 rv = ENOMEM; 1523 rv = ENOMEM;
1524 goto bad1; 1524 goto bad1;
1525 } 1525 }
1526 1526
1527 err = xhci_ring_init(sc, &sc->sc_er, XHCI_EVENT_RING_TRBS, 1527 err = xhci_ring_init(sc, &sc->sc_er, XHCI_EVENT_RING_TRBS,
1528 XHCI_EVENT_RING_SEGMENTS_ALIGN); 1528 XHCI_EVENT_RING_SEGMENTS_ALIGN);
1529 if (err) { 1529 if (err) {
1530 aprint_error_dev(sc->sc_dev, "event ring init fail, err %d\n", 1530 aprint_error_dev(sc->sc_dev, "event ring init fail, err %d\n",
1531 err); 1531 err);
1532 rv = ENOMEM; 1532 rv = ENOMEM;
1533 goto bad2; 1533 goto bad2;
1534 } 1534 }
1535 1535
1536 usb_dma_t *dma; 1536 usb_dma_t *dma;
1537 size_t size; 1537 size_t size;
1538 size_t align; 1538 size_t align;
1539 1539
1540 dma = &sc->sc_eventst_dma; 1540 dma = &sc->sc_eventst_dma;
1541 size = roundup2(XHCI_EVENT_RING_SEGMENTS * XHCI_ERSTE_SIZE, 1541 size = roundup2(XHCI_EVENT_RING_SEGMENTS * XHCI_ERSTE_SIZE,
1542 XHCI_EVENT_RING_SEGMENT_TABLE_ALIGN); 1542 XHCI_EVENT_RING_SEGMENT_TABLE_ALIGN);
1543 KASSERTMSG(size <= (512 * 1024), "eventst size %zu too large", size); 1543 KASSERTMSG(size <= (512 * 1024), "eventst size %zu too large", size);
1544 align = XHCI_EVENT_RING_SEGMENT_TABLE_ALIGN; 1544 align = XHCI_EVENT_RING_SEGMENT_TABLE_ALIGN;
1545 err = usb_allocmem(&sc->sc_bus, size, align, 1545 err = usb_allocmem(&sc->sc_bus, size, align,
1546 USBMALLOC_COHERENT | USBMALLOC_ZERO, dma); 1546 USBMALLOC_COHERENT | USBMALLOC_ZERO, dma);
1547 if (err) { 1547 if (err) {
1548 aprint_error_dev(sc->sc_dev, "eventst init fail, err %d\n", 1548 aprint_error_dev(sc->sc_dev, "eventst init fail, err %d\n",
1549 err); 1549 err);
1550 rv = ENOMEM; 1550 rv = ENOMEM;
1551 goto bad3; 1551 goto bad3;
1552 } 1552 }
1553 1553
1554 aprint_debug_dev(sc->sc_dev, "eventst: 0x%016jx %p %zx\n", 1554 aprint_debug_dev(sc->sc_dev, "eventst: 0x%016jx %p %zx\n",
1555 (uintmax_t)DMAADDR(&sc->sc_eventst_dma, 0), 1555 (uintmax_t)DMAADDR(&sc->sc_eventst_dma, 0),
1556 KERNADDR(&sc->sc_eventst_dma, 0), 1556 KERNADDR(&sc->sc_eventst_dma, 0),
1557 sc->sc_eventst_dma.udma_block->size); 1557 sc->sc_eventst_dma.udma_block->size);
1558 1558
1559 dma = &sc->sc_dcbaa_dma; 1559 dma = &sc->sc_dcbaa_dma;
1560 size = (1 + sc->sc_maxslots) * sizeof(uint64_t); 1560 size = (1 + sc->sc_maxslots) * sizeof(uint64_t);
1561 KASSERTMSG(size <= 2048, "dcbaa size %zu too large", size); 1561 KASSERTMSG(size <= 2048, "dcbaa size %zu too large", size);
1562 align = XHCI_DEVICE_CONTEXT_BASE_ADDRESS_ARRAY_ALIGN; 1562 align = XHCI_DEVICE_CONTEXT_BASE_ADDRESS_ARRAY_ALIGN;
1563 err = usb_allocmem(&sc->sc_bus, size, align, 1563 err = usb_allocmem(&sc->sc_bus, size, align,
1564 USBMALLOC_COHERENT | USBMALLOC_ZERO, dma); 1564 USBMALLOC_COHERENT | USBMALLOC_ZERO, dma);
1565 if (err) { 1565 if (err) {
1566 aprint_error_dev(sc->sc_dev, "dcbaa init fail, err %d\n", err); 1566 aprint_error_dev(sc->sc_dev, "dcbaa init fail, err %d\n", err);
1567 rv = ENOMEM; 1567 rv = ENOMEM;
1568 goto bad4; 1568 goto bad4;
1569 } 1569 }
1570 aprint_debug_dev(sc->sc_dev, "dcbaa: 0x%016jx %p %zx\n", 1570 aprint_debug_dev(sc->sc_dev, "dcbaa: 0x%016jx %p %zx\n",
1571 (uintmax_t)DMAADDR(&sc->sc_dcbaa_dma, 0), 1571 (uintmax_t)DMAADDR(&sc->sc_dcbaa_dma, 0),
1572 KERNADDR(&sc->sc_dcbaa_dma, 0), 1572 KERNADDR(&sc->sc_dcbaa_dma, 0),
1573 sc->sc_dcbaa_dma.udma_block->size); 1573 sc->sc_dcbaa_dma.udma_block->size);
1574 1574
1575 if (sc->sc_maxspbuf != 0) { 1575 if (sc->sc_maxspbuf != 0) {
1576 /* 1576 /*
1577 * DCBA entry 0 hold the scratchbuf array pointer. 1577 * DCBA entry 0 hold the scratchbuf array pointer.
1578 */ 1578 */
1579 *(uint64_t *)KERNADDR(dma, 0) = 1579 *(uint64_t *)KERNADDR(dma, 0) =
1580 htole64(DMAADDR(&sc->sc_spbufarray_dma, 0)); 1580 htole64(DMAADDR(&sc->sc_spbufarray_dma, 0));
1581 usb_syncmem(dma, 0, size, BUS_DMASYNC_PREWRITE); 1581 usb_syncmem(dma, 0, size, BUS_DMASYNC_PREWRITE);
1582 } 1582 }
1583 1583
1584 sc->sc_slots = kmem_zalloc(sizeof(*sc->sc_slots) * sc->sc_maxslots, 1584 sc->sc_slots = kmem_zalloc(sizeof(*sc->sc_slots) * sc->sc_maxslots,
1585 KM_SLEEP); 1585 KM_SLEEP);
1586 if (sc->sc_slots == NULL) { 1586 if (sc->sc_slots == NULL) {
1587 aprint_error_dev(sc->sc_dev, "slots init fail, err %d\n", err); 1587 aprint_error_dev(sc->sc_dev, "slots init fail, err %d\n", err);
1588 rv = ENOMEM; 1588 rv = ENOMEM;
1589 goto bad; 1589 goto bad;
1590 } 1590 }
1591 1591
1592 sc->sc_xferpool = pool_cache_init(sizeof(struct xhci_xfer), 0, 0, 0, 1592 sc->sc_xferpool = pool_cache_init(sizeof(struct xhci_xfer), 0, 0, 0,
1593 "xhcixfer", NULL, IPL_USB, NULL, NULL, NULL); 1593 "xhcixfer", NULL, IPL_USB, NULL, NULL, NULL);
1594 if (sc->sc_xferpool == NULL) { 1594 if (sc->sc_xferpool == NULL) {
1595 aprint_error_dev(sc->sc_dev, "pool_cache init fail, err %d\n", 1595 aprint_error_dev(sc->sc_dev, "pool_cache init fail, err %d\n",
1596 err); 1596 err);
1597 rv = ENOMEM; 1597 rv = ENOMEM;
1598 goto bad; 1598 goto bad;
1599 } 1599 }
1600 1600
1601 cv_init(&sc->sc_command_cv, "xhcicmd"); 1601 cv_init(&sc->sc_command_cv, "xhcicmd");
1602 cv_init(&sc->sc_cmdbusy_cv, "xhcicmdq"); 1602 cv_init(&sc->sc_cmdbusy_cv, "xhcicmdq");
1603 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB); 1603 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
1604 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_USB); 1604 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_USB);
1605 1605
1606 struct xhci_erste *erst; 1606 struct xhci_erste *erst;
1607 erst = KERNADDR(&sc->sc_eventst_dma, 0); 1607 erst = KERNADDR(&sc->sc_eventst_dma, 0);
1608 erst[0].erste_0 = htole64(xhci_ring_trbp(sc->sc_er, 0)); 1608 erst[0].erste_0 = htole64(xhci_ring_trbp(sc->sc_er, 0));
1609 erst[0].erste_2 = htole32(sc->sc_er->xr_ntrb); 1609 erst[0].erste_2 = htole32(sc->sc_er->xr_ntrb);
1610 erst[0].erste_3 = htole32(0); 1610 erst[0].erste_3 = htole32(0);
1611 usb_syncmem(&sc->sc_eventst_dma, 0, 1611 usb_syncmem(&sc->sc_eventst_dma, 0,
1612 XHCI_ERSTE_SIZE * XHCI_EVENT_RING_SEGMENTS, BUS_DMASYNC_PREWRITE); 1612 XHCI_ERSTE_SIZE * XHCI_EVENT_RING_SEGMENTS, BUS_DMASYNC_PREWRITE);
1613 1613
1614 xhci_rt_write_4(sc, XHCI_ERSTSZ(0), XHCI_EVENT_RING_SEGMENTS); 1614 xhci_rt_write_4(sc, XHCI_ERSTSZ(0), XHCI_EVENT_RING_SEGMENTS);
1615 xhci_rt_write_8(sc, XHCI_ERSTBA(0), DMAADDR(&sc->sc_eventst_dma, 0)); 1615 xhci_rt_write_8(sc, XHCI_ERSTBA(0), DMAADDR(&sc->sc_eventst_dma, 0));
1616 xhci_rt_write_8(sc, XHCI_ERDP(0), xhci_ring_trbp(sc->sc_er, 0) | 1616 xhci_rt_write_8(sc, XHCI_ERDP(0), xhci_ring_trbp(sc->sc_er, 0) |
1617 XHCI_ERDP_BUSY); 1617 XHCI_ERDP_BUSY);
1618 1618
1619 xhci_op_write_8(sc, XHCI_DCBAAP, DMAADDR(&sc->sc_dcbaa_dma, 0)); 1619 xhci_op_write_8(sc, XHCI_DCBAAP, DMAADDR(&sc->sc_dcbaa_dma, 0));
1620 xhci_op_write_8(sc, XHCI_CRCR, xhci_ring_trbp(sc->sc_cr, 0) | 1620 xhci_op_write_8(sc, XHCI_CRCR, xhci_ring_trbp(sc->sc_cr, 0) |
1621 sc->sc_cr->xr_cs); 1621 sc->sc_cr->xr_cs);
1622 1622
1623 xhci_barrier(sc, BUS_SPACE_BARRIER_WRITE); 1623 xhci_barrier(sc, BUS_SPACE_BARRIER_WRITE);
1624 1624
1625 HEXDUMP("eventst", KERNADDR(&sc->sc_eventst_dma, 0), 1625 HEXDUMP("eventst", KERNADDR(&sc->sc_eventst_dma, 0),
1626 XHCI_ERSTE_SIZE * XHCI_EVENT_RING_SEGMENTS); 1626 XHCI_ERSTE_SIZE * XHCI_EVENT_RING_SEGMENTS);
1627 1627
1628 if ((sc->sc_quirks & XHCI_DEFERRED_START) == 0) 1628 if ((sc->sc_quirks & XHCI_DEFERRED_START) == 0)
1629 xhci_start(sc); 1629 xhci_start(sc);
1630 1630
1631 return 0; 1631 return 0;
1632 1632
1633 bad: 1633 bad:
1634 if (sc->sc_xferpool) { 1634 if (sc->sc_xferpool) {
1635 pool_cache_destroy(sc->sc_xferpool); 1635 pool_cache_destroy(sc->sc_xferpool);
1636 sc->sc_xferpool = NULL; 1636 sc->sc_xferpool = NULL;
1637 } 1637 }
1638 1638
1639 if (sc->sc_slots) { 1639 if (sc->sc_slots) {
1640 kmem_free(sc->sc_slots, sizeof(*sc->sc_slots) * 1640 kmem_free(sc->sc_slots, sizeof(*sc->sc_slots) *
1641 sc->sc_maxslots); 1641 sc->sc_maxslots);
1642 sc->sc_slots = NULL; 1642 sc->sc_slots = NULL;
1643 } 1643 }
1644 1644
1645 usb_freemem(&sc->sc_bus, &sc->sc_dcbaa_dma); 1645 usb_freemem(&sc->sc_bus, &sc->sc_dcbaa_dma);
1646 bad4: 1646 bad4:
1647 usb_freemem(&sc->sc_bus, &sc->sc_eventst_dma); 1647 usb_freemem(&sc->sc_bus, &sc->sc_eventst_dma);
1648 bad3: 1648 bad3:
1649 xhci_ring_free(sc, &sc->sc_er); 1649 xhci_ring_free(sc, &sc->sc_er);
1650 bad2: 1650 bad2:
1651 xhci_ring_free(sc, &sc->sc_cr); 1651 xhci_ring_free(sc, &sc->sc_cr);
1652 i = sc->sc_maxspbuf; 1652 i = sc->sc_maxspbuf;
1653 bad1: 1653 bad1:
1654 for (int j = 0; j < i; j++) 1654 for (int j = 0; j < i; j++)
1655 usb_freemem(&sc->sc_bus, &sc->sc_spbuf_dma[j]); 1655 usb_freemem(&sc->sc_bus, &sc->sc_spbuf_dma[j]);
1656 usb_freemem(&sc->sc_bus, &sc->sc_spbufarray_dma); 1656 usb_freemem(&sc->sc_bus, &sc->sc_spbufarray_dma);
1657 1657
1658 return rv; 1658 return rv;
1659} 1659}
1660 1660
1661static inline bool 1661static inline bool
1662xhci_polling_p(struct xhci_softc * const sc) 1662xhci_polling_p(struct xhci_softc * const sc)
1663{ 1663{
1664 return sc->sc_bus.ub_usepolling || sc->sc_bus2.ub_usepolling; 1664 return sc->sc_bus.ub_usepolling || sc->sc_bus2.ub_usepolling;
1665} 1665}
1666 1666
1667int 1667int
1668xhci_intr(void *v) 1668xhci_intr(void *v)
1669{ 1669{
1670 struct xhci_softc * const sc = v; 1670 struct xhci_softc * const sc = v;
1671 int ret = 0; 1671 int ret = 0;
1672 1672
1673 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1673 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1674 1674
1675 if (sc == NULL) 1675 if (sc == NULL)
1676 return 0; 1676 return 0;
1677 1677
1678 mutex_spin_enter(&sc->sc_intr_lock); 1678 mutex_spin_enter(&sc->sc_intr_lock);
1679 1679
1680 if (sc->sc_dying || !device_has_power(sc->sc_dev)) 1680 if (sc->sc_dying || !device_has_power(sc->sc_dev))
1681 goto done; 1681 goto done;
1682 1682
1683 /* If we get an interrupt while polling, then just ignore it. */ 1683 /* If we get an interrupt while polling, then just ignore it. */
1684 if (xhci_polling_p(sc)) { 1684 if (xhci_polling_p(sc)) {
1685#ifdef DIAGNOSTIC 1685#ifdef DIAGNOSTIC
1686 DPRINTFN(16, "ignored interrupt while polling", 0, 0, 0, 0); 1686 DPRINTFN(16, "ignored interrupt while polling", 0, 0, 0, 0);
1687#endif 1687#endif
1688 goto done; 1688 goto done;
1689 } 1689 }
1690 1690
1691 ret = xhci_intr1(sc); 1691 ret = xhci_intr1(sc);
1692 if (ret) { 1692 if (ret) {
1693 KASSERT(sc->sc_child || sc->sc_child2); 1693 KASSERT(sc->sc_child || sc->sc_child2);
1694 1694
1695 /* 1695 /*
1696 * One of child busses could be already detached. It doesn't 1696 * One of child busses could be already detached. It doesn't
1697 * matter on which of the two the softintr is scheduled. 1697 * matter on which of the two the softintr is scheduled.
1698 */ 1698 */
1699 if (sc->sc_child) 1699 if (sc->sc_child)
1700 usb_schedsoftintr(&sc->sc_bus); 1700 usb_schedsoftintr(&sc->sc_bus);
1701 else 1701 else
1702 usb_schedsoftintr(&sc->sc_bus2); 1702 usb_schedsoftintr(&sc->sc_bus2);
1703 } 1703 }
1704done: 1704done:
1705 mutex_spin_exit(&sc->sc_intr_lock); 1705 mutex_spin_exit(&sc->sc_intr_lock);
1706 return ret; 1706 return ret;
1707} 1707}
1708 1708
1709int 1709int
1710xhci_intr1(struct xhci_softc * const sc) 1710xhci_intr1(struct xhci_softc * const sc)
1711{ 1711{
1712 uint32_t usbsts; 1712 uint32_t usbsts;
1713 uint32_t iman; 1713 uint32_t iman;
1714 1714
1715 XHCIHIST_FUNC(); 1715 XHCIHIST_FUNC();
1716 1716
1717 usbsts = xhci_op_read_4(sc, XHCI_USBSTS); 1717 usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
1718 XHCIHIST_CALLARGS("USBSTS 0x%08jx", usbsts, 0, 0, 0); 1718 XHCIHIST_CALLARGS("USBSTS 0x%08jx", usbsts, 0, 0, 0);
1719 if ((usbsts & (XHCI_STS_HSE | XHCI_STS_EINT | XHCI_STS_PCD | 1719 if ((usbsts & (XHCI_STS_HSE | XHCI_STS_EINT | XHCI_STS_PCD |
1720 XHCI_STS_HCE)) == 0) { 1720 XHCI_STS_HCE)) == 0) {
1721 DPRINTFN(16, "ignored intr not for %jd", 1721 DPRINTFN(16, "ignored intr not for %jd",
1722 device_unit(sc->sc_dev), 0, 0, 0); 1722 device_unit(sc->sc_dev), 0, 0, 0);
1723 return 0; 1723 return 0;
1724 } 1724 }
1725 1725
1726 /* 1726 /*
1727 * Clear EINT and other transient flags, to not misenterpret 1727 * Clear EINT and other transient flags, to not misenterpret
1728 * next shared interrupt. Also, to avoid race, EINT must be cleared 1728 * next shared interrupt. Also, to avoid race, EINT must be cleared
1729 * before XHCI_IMAN_INTR_PEND is cleared. 1729 * before XHCI_IMAN_INTR_PEND is cleared.
1730 */ 1730 */
1731 xhci_op_write_4(sc, XHCI_USBSTS, usbsts & XHCI_STS_RSVDP0); 1731 xhci_op_write_4(sc, XHCI_USBSTS, usbsts & XHCI_STS_RSVDP0);
1732 1732
1733#ifdef XHCI_DEBUG 1733#ifdef XHCI_DEBUG
1734 usbsts = xhci_op_read_4(sc, XHCI_USBSTS); 1734 usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
1735 DPRINTFN(16, "USBSTS 0x%08jx", usbsts, 0, 0, 0); 1735 DPRINTFN(16, "USBSTS 0x%08jx", usbsts, 0, 0, 0);
1736#endif 1736#endif
1737 1737
1738 iman = xhci_rt_read_4(sc, XHCI_IMAN(0)); 1738 iman = xhci_rt_read_4(sc, XHCI_IMAN(0));
1739 DPRINTFN(16, "IMAN0 0x%08jx", iman, 0, 0, 0); 1739 DPRINTFN(16, "IMAN0 0x%08jx", iman, 0, 0, 0);
1740 iman |= XHCI_IMAN_INTR_PEND; 1740 iman |= XHCI_IMAN_INTR_PEND;
1741 xhci_rt_write_4(sc, XHCI_IMAN(0), iman); 1741 xhci_rt_write_4(sc, XHCI_IMAN(0), iman);
1742 1742
1743#ifdef XHCI_DEBUG 1743#ifdef XHCI_DEBUG
1744 iman = xhci_rt_read_4(sc, XHCI_IMAN(0)); 1744 iman = xhci_rt_read_4(sc, XHCI_IMAN(0));
1745 DPRINTFN(16, "IMAN0 0x%08jx", iman, 0, 0, 0); 1745 DPRINTFN(16, "IMAN0 0x%08jx", iman, 0, 0, 0);
1746 usbsts = xhci_op_read_4(sc, XHCI_USBSTS); 1746 usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
1747 DPRINTFN(16, "USBSTS 0x%08jx", usbsts, 0, 0, 0); 1747 DPRINTFN(16, "USBSTS 0x%08jx", usbsts, 0, 0, 0);
1748#endif 1748#endif
1749 1749
1750 return 1; 1750 return 1;
1751} 1751}
1752 1752
1753/* 1753/*
1754 * 3 port speed types used in USB stack 1754 * 3 port speed types used in USB stack
1755 * 1755 *
1756 * usbdi speed 1756 * usbdi speed
1757 * definition: USB_SPEED_* in usb.h 1757 * definition: USB_SPEED_* in usb.h
1758 * They are used in struct usbd_device in USB stack. 1758 * They are used in struct usbd_device in USB stack.
1759 * ioctl interface uses these values too. 1759 * ioctl interface uses these values too.
1760 * port_status speed 1760 * port_status speed
1761 * definition: UPS_*_SPEED in usb.h 1761 * definition: UPS_*_SPEED in usb.h
1762 * They are used in usb_port_status_t and valid only for USB 2.0. 1762 * They are used in usb_port_status_t and valid only for USB 2.0.
1763 * Speed value is always 0 for Super Speed or more, and dwExtPortStatus 1763 * Speed value is always 0 for Super Speed or more, and dwExtPortStatus
1764 * of usb_port_status_ext_t indicates port speed. 1764 * of usb_port_status_ext_t indicates port speed.
1765 * Note that some 3.0 values overlap with 2.0 values. 1765 * Note that some 3.0 values overlap with 2.0 values.
1766 * (e.g. 0x200 means UPS_POER_POWER_SS in SS and 1766 * (e.g. 0x200 means UPS_POER_POWER_SS in SS and
1767 * means UPS_LOW_SPEED in HS.) 1767 * means UPS_LOW_SPEED in HS.)
1768 * port status returned from hub also uses these values. 1768 * port status returned from hub also uses these values.
1769 * On NetBSD UPS_OTHER_SPEED indicates port speed is super speed 1769 * On NetBSD UPS_OTHER_SPEED indicates port speed is super speed
1770 * or more. 1770 * or more.
1771 * xspeed: 1771 * xspeed:
1772 * definition: Protocol Speed ID (PSI) (xHCI 1.1 7.2.1) 1772 * definition: Protocol Speed ID (PSI) (xHCI 1.1 7.2.1)
1773 * They are used in only slot context and PORTSC reg of xhci. 1773 * They are used in only slot context and PORTSC reg of xhci.
1774 * The difference between usbdi speed and xspeed is 1774 * The difference between usbdi speed and xspeed is
1775 * that FS and LS values are swapped. 1775 * that FS and LS values are swapped.
1776 */ 1776 */
1777 1777
1778/* convert usbdi speed to xspeed */ 1778/* convert usbdi speed to xspeed */
1779static int 1779static int
1780xhci_speed2xspeed(int speed) 1780xhci_speed2xspeed(int speed)
1781{ 1781{
1782 switch (speed) { 1782 switch (speed) {
1783 case USB_SPEED_LOW: return 2; 1783 case USB_SPEED_LOW: return 2;
1784 case USB_SPEED_FULL: return 1; 1784 case USB_SPEED_FULL: return 1;
1785 default: return speed; 1785 default: return speed;
1786 } 1786 }
1787} 1787}
1788 1788
1789#if 0 1789#if 0
1790/* convert xspeed to usbdi speed */ 1790/* convert xspeed to usbdi speed */
1791static int 1791static int
1792xhci_xspeed2speed(int xspeed) 1792xhci_xspeed2speed(int xspeed)
1793{ 1793{
1794 switch (xspeed) { 1794 switch (xspeed) {
1795 case 1: return USB_SPEED_FULL; 1795 case 1: return USB_SPEED_FULL;
1796 case 2: return USB_SPEED_LOW; 1796 case 2: return USB_SPEED_LOW;
1797 default: return xspeed; 1797 default: return xspeed;
1798 } 1798 }
1799} 1799}
1800#endif 1800#endif
1801 1801
1802/* convert xspeed to port status speed */ 1802/* convert xspeed to port status speed */
1803static int 1803static int
1804xhci_xspeed2psspeed(int xspeed) 1804xhci_xspeed2psspeed(int xspeed)
1805{ 1805{
1806 switch (xspeed) { 1806 switch (xspeed) {
1807 case 0: return 0; 1807 case 0: return 0;
1808 case 1: return UPS_FULL_SPEED; 1808 case 1: return UPS_FULL_SPEED;
1809 case 2: return UPS_LOW_SPEED; 1809 case 2: return UPS_LOW_SPEED;
1810 case 3: return UPS_HIGH_SPEED; 1810 case 3: return UPS_HIGH_SPEED;
1811 default: return UPS_OTHER_SPEED; 1811 default: return UPS_OTHER_SPEED;
1812 } 1812 }
1813} 1813}
1814 1814
1815/* 1815/*
1816 * Construct input contexts and issue TRB to open pipe. 1816 * Construct input contexts and issue TRB to open pipe.
1817 */ 1817 */
1818static usbd_status 1818static usbd_status
1819xhci_configure_endpoint(struct usbd_pipe *pipe) 1819xhci_configure_endpoint(struct usbd_pipe *pipe)
1820{ 1820{
1821 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe); 1821 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
1822 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv; 1822 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
1823#ifdef USB_DEBUG 1823#ifdef USB_DEBUG
1824 const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc); 1824 const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
1825#endif 1825#endif
1826 struct xhci_soft_trb trb; 1826 struct xhci_soft_trb trb;
1827 usbd_status err; 1827 usbd_status err;
1828 1828
1829 XHCIHIST_FUNC(); 1829 XHCIHIST_FUNC();
1830 XHCIHIST_CALLARGS("slot %ju dci %ju epaddr 0x%02jx attr 0x%02jx", 1830 XHCIHIST_CALLARGS("slot %ju dci %ju epaddr 0x%02jx attr 0x%02jx",
1831 xs->xs_idx, dci, pipe->up_endpoint->ue_edesc->bEndpointAddress, 1831 xs->xs_idx, dci, pipe->up_endpoint->ue_edesc->bEndpointAddress,
1832 pipe->up_endpoint->ue_edesc->bmAttributes); 1832 pipe->up_endpoint->ue_edesc->bmAttributes);
1833 1833
1834 /* XXX ensure input context is available? */ 1834 /* XXX ensure input context is available? */
1835 1835
1836 memset(xhci_slot_get_icv(sc, xs, 0), 0, sc->sc_pgsz); 1836 memset(xhci_slot_get_icv(sc, xs, 0), 0, sc->sc_pgsz);
1837 1837
1838 /* set up context */ 1838 /* set up context */
1839 xhci_setup_ctx(pipe); 1839 xhci_setup_ctx(pipe);
1840 1840
1841 HEXDUMP("input control context", xhci_slot_get_icv(sc, xs, 0), 1841 HEXDUMP("input control context", xhci_slot_get_icv(sc, xs, 0),
1842 sc->sc_ctxsz * 1); 1842 sc->sc_ctxsz * 1);
1843 HEXDUMP("input endpoint context", xhci_slot_get_icv(sc, xs, 1843 HEXDUMP("input endpoint context", xhci_slot_get_icv(sc, xs,
1844 xhci_dci_to_ici(dci)), sc->sc_ctxsz * 1); 1844 xhci_dci_to_ici(dci)), sc->sc_ctxsz * 1);
1845 1845
1846 trb.trb_0 = xhci_slot_get_icp(sc, xs, 0); 1846 trb.trb_0 = xhci_slot_get_icp(sc, xs, 0);
1847 trb.trb_2 = 0; 1847 trb.trb_2 = 0;
1848 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) | 1848 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
1849 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_CONFIGURE_EP); 1849 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_CONFIGURE_EP);
1850 1850
1851 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT); 1851 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1852 1852
1853 usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD); 1853 usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
1854 HEXDUMP("output context", xhci_slot_get_dcv(sc, xs, dci), 1854 HEXDUMP("output context", xhci_slot_get_dcv(sc, xs, dci),
1855 sc->sc_ctxsz * 1); 1855 sc->sc_ctxsz * 1);
1856 1856
1857 return err; 1857 return err;
1858} 1858}
1859 1859
1860#if 0 1860#if 0
1861static usbd_status 1861static usbd_status
1862xhci_unconfigure_endpoint(struct usbd_pipe *pipe) 1862xhci_unconfigure_endpoint(struct usbd_pipe *pipe)
1863{ 1863{
1864#ifdef USB_DEBUG 1864#ifdef USB_DEBUG
1865 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv; 1865 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
1866#endif 1866#endif
1867 1867
1868 XHCIHIST_FUNC(); 1868 XHCIHIST_FUNC();
1869 XHCIHIST_CALLARGS("slot %ju", xs->xs_idx, 0, 0, 0); 1869 XHCIHIST_CALLARGS("slot %ju", xs->xs_idx, 0, 0, 0);
1870 1870
1871 return USBD_NORMAL_COMPLETION; 1871 return USBD_NORMAL_COMPLETION;
1872} 1872}
1873#endif 1873#endif
1874 1874
1875/* 4.6.8, 6.4.3.7 */ 1875/* 4.6.8, 6.4.3.7 */
1876static usbd_status 1876static usbd_status
1877xhci_reset_endpoint_locked(struct usbd_pipe *pipe) 1877xhci_reset_endpoint_locked(struct usbd_pipe *pipe)
1878{ 1878{
1879 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe); 1879 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
1880 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv; 1880 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
1881 const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc); 1881 const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
1882 struct xhci_soft_trb trb; 1882 struct xhci_soft_trb trb;
1883 usbd_status err; 1883 usbd_status err;
1884 1884
1885 XHCIHIST_FUNC(); 1885 XHCIHIST_FUNC();
1886 XHCIHIST_CALLARGS("slot %ju dci %ju", xs->xs_idx, dci, 0, 0); 1886 XHCIHIST_CALLARGS("slot %ju dci %ju", xs->xs_idx, dci, 0, 0);
1887 1887
1888 KASSERT(mutex_owned(&sc->sc_lock)); 1888 KASSERT(mutex_owned(&sc->sc_lock));
1889 1889
1890 trb.trb_0 = 0; 1890 trb.trb_0 = 0;
1891 trb.trb_2 = 0; 1891 trb.trb_2 = 0;
1892 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) | 1892 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
1893 XHCI_TRB_3_EP_SET(dci) | 1893 XHCI_TRB_3_EP_SET(dci) |
1894 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_EP); 1894 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_EP);
1895 1895
1896 err = xhci_do_command_locked(sc, &trb, USBD_DEFAULT_TIMEOUT); 1896 err = xhci_do_command_locked(sc, &trb, USBD_DEFAULT_TIMEOUT);
1897 1897
1898 return err; 1898 return err;
1899} 1899}
1900 1900
1901static usbd_status 1901static usbd_status
1902xhci_reset_endpoint(struct usbd_pipe *pipe) 1902xhci_reset_endpoint(struct usbd_pipe *pipe)
1903{ 1903{
1904 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe); 1904 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
1905 1905
1906 mutex_enter(&sc->sc_lock); 1906 mutex_enter(&sc->sc_lock);
1907 usbd_status ret = xhci_reset_endpoint_locked(pipe); 1907 usbd_status ret = xhci_reset_endpoint_locked(pipe);
1908 mutex_exit(&sc->sc_lock); 1908 mutex_exit(&sc->sc_lock);
1909 1909
1910 return ret; 1910 return ret;
1911} 1911}
1912 1912
1913/* 1913/*
1914 * 4.6.9, 6.4.3.8 1914 * 4.6.9, 6.4.3.8
1915 * Stop execution of TDs on xfer ring. 1915 * Stop execution of TDs on xfer ring.
1916 * Should be called with sc_lock held. 1916 * Should be called with sc_lock held.
1917 */ 1917 */
1918static usbd_status 1918static usbd_status
1919xhci_stop_endpoint_cmd(struct xhci_softc *sc, struct xhci_slot *xs, u_int dci, 1919xhci_stop_endpoint_cmd(struct xhci_softc *sc, struct xhci_slot *xs, u_int dci,
1920 uint32_t trb3flags) 1920 uint32_t trb3flags)
1921{ 1921{
1922 struct xhci_soft_trb trb; 1922 struct xhci_soft_trb trb;
1923 usbd_status err; 1923 usbd_status err;
1924 1924
1925 XHCIHIST_FUNC(); 1925 XHCIHIST_FUNC();
1926 XHCIHIST_CALLARGS("slot %ju dci %ju", xs->xs_idx, dci, 0, 0); 1926 XHCIHIST_CALLARGS("slot %ju dci %ju", xs->xs_idx, dci, 0, 0);
1927 1927
1928 KASSERT(mutex_owned(&sc->sc_lock)); 1928 KASSERT(mutex_owned(&sc->sc_lock));
1929 1929
1930 trb.trb_0 = 0; 1930 trb.trb_0 = 0;
1931 trb.trb_2 = 0; 1931 trb.trb_2 = 0;
1932 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) | 1932 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
1933 XHCI_TRB_3_EP_SET(dci) | 1933 XHCI_TRB_3_EP_SET(dci) |
1934 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STOP_EP) | 1934 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STOP_EP) |
1935 trb3flags; 1935 trb3flags;
1936 1936
1937 err = xhci_do_command_locked(sc, &trb, USBD_DEFAULT_TIMEOUT); 1937 err = xhci_do_command_locked(sc, &trb, USBD_DEFAULT_TIMEOUT);
1938 1938
1939 return err; 1939 return err;
1940} 1940}
1941 1941
1942static usbd_status 1942static usbd_status
1943xhci_stop_endpoint(struct usbd_pipe *pipe) 1943xhci_stop_endpoint(struct usbd_pipe *pipe)
1944{ 1944{
1945 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe); 1945 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
1946 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv; 1946 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
1947 const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc); 1947 const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
1948 1948
1949 XHCIHIST_FUNC(); 1949 XHCIHIST_FUNC();
1950 XHCIHIST_CALLARGS("slot %ju dci %ju", xs->xs_idx, dci, 0, 0); 1950 XHCIHIST_CALLARGS("slot %ju dci %ju", xs->xs_idx, dci, 0, 0);
1951 1951
1952 KASSERT(mutex_owned(&sc->sc_lock)); 1952 KASSERT(mutex_owned(&sc->sc_lock));
1953 1953
1954 return xhci_stop_endpoint_cmd(sc, xs, dci, 0); 1954 return xhci_stop_endpoint_cmd(sc, xs, dci, 0);
1955} 1955}
1956 1956
1957/* 1957/*
1958 * Set TR Dequeue Pointer. 1958 * Set TR Dequeue Pointer.
1959 * xHCI 1.1 4.6.10 6.4.3.9 1959 * xHCI 1.1 4.6.10 6.4.3.9
1960 * Purge all of the TRBs on ring and reinitialize ring. 1960 * Purge all of the TRBs on ring and reinitialize ring.
1961 * Set TR dequeue Pointr to 0 and Cycle State to 1. 1961 * Set TR dequeue Pointr to 0 and Cycle State to 1.
1962 * EPSTATE of endpoint must be ERROR or STOPPED, otherwise CONTEXT_STATE 1962 * EPSTATE of endpoint must be ERROR or STOPPED, otherwise CONTEXT_STATE
1963 * error will be generated. 1963 * error will be generated.
1964 */ 1964 */
1965static usbd_status 1965static usbd_status
1966xhci_set_dequeue_locked(struct usbd_pipe *pipe) 1966xhci_set_dequeue_locked(struct usbd_pipe *pipe)
1967{ 1967{
1968 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe); 1968 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
1969 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv; 1969 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
1970 const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc); 1970 const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
1971 struct xhci_ring * const xr = xs->xs_xr[dci]; 1971 struct xhci_ring * const xr = xs->xs_xr[dci];
1972 struct xhci_soft_trb trb; 1972 struct xhci_soft_trb trb;
1973 usbd_status err; 1973 usbd_status err;
1974 1974
1975 XHCIHIST_FUNC(); 1975 XHCIHIST_FUNC();
1976 XHCIHIST_CALLARGS("slot %ju dci %ju", xs->xs_idx, dci, 0, 0); 1976 XHCIHIST_CALLARGS("slot %ju dci %ju", xs->xs_idx, dci, 0, 0);
1977 1977
1978 KASSERT(mutex_owned(&sc->sc_lock)); 1978 KASSERT(mutex_owned(&sc->sc_lock));
1979 KASSERT(xr != NULL); 1979 KASSERT(xr != NULL);
1980 1980
1981 xhci_host_dequeue(xr); 1981 xhci_host_dequeue(xr);
1982 1982
1983 /* set DCS */ 1983 /* set DCS */
1984 trb.trb_0 = xhci_ring_trbp(xr, 0) | 1; /* XXX */ 1984 trb.trb_0 = xhci_ring_trbp(xr, 0) | 1; /* XXX */
1985 trb.trb_2 = 0; 1985 trb.trb_2 = 0;
1986 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) | 1986 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
1987 XHCI_TRB_3_EP_SET(dci) | 1987 XHCI_TRB_3_EP_SET(dci) |
1988 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SET_TR_DEQUEUE); 1988 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SET_TR_DEQUEUE);
1989 1989
1990 err = xhci_do_command_locked(sc, &trb, USBD_DEFAULT_TIMEOUT); 1990 err = xhci_do_command_locked(sc, &trb, USBD_DEFAULT_TIMEOUT);
1991 1991
1992 return err; 1992 return err;
1993} 1993}
1994 1994
1995static usbd_status 1995static usbd_status
1996xhci_set_dequeue(struct usbd_pipe *pipe) 1996xhci_set_dequeue(struct usbd_pipe *pipe)
1997{ 1997{
1998 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe); 1998 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
1999 1999
2000 mutex_enter(&sc->sc_lock); 2000 mutex_enter(&sc->sc_lock);
2001 usbd_status ret = xhci_set_dequeue_locked(pipe); 2001 usbd_status ret = xhci_set_dequeue_locked(pipe);
2002 mutex_exit(&sc->sc_lock); 2002 mutex_exit(&sc->sc_lock);
2003 2003
2004 return ret; 2004 return ret;
2005} 2005}
2006 2006
2007/* 2007/*
2008 * Open new pipe: called from usbd_setup_pipe_flags. 2008 * Open new pipe: called from usbd_setup_pipe_flags.
2009 * Fills methods of pipe. 2009 * Fills methods of pipe.
2010 * If pipe is not for ep0, calls configure_endpoint. 2010 * If pipe is not for ep0, calls configure_endpoint.
2011 */ 2011 */
2012static usbd_status 2012static usbd_status
2013xhci_open(struct usbd_pipe *pipe) 2013xhci_open(struct usbd_pipe *pipe)
2014{ 2014{
2015 struct usbd_device * const dev = pipe->up_dev; 2015 struct usbd_device * const dev = pipe->up_dev;
2016 struct xhci_pipe * const xpipe = (struct xhci_pipe *)pipe; 2016 struct xhci_pipe * const xpipe = (struct xhci_pipe *)pipe;
2017 struct xhci_softc * const sc = XHCI_BUS2SC(dev->ud_bus); 2017 struct xhci_softc * const sc = XHCI_BUS2SC(dev->ud_bus);
2018 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv; 2018 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
2019 usb_endpoint_descriptor_t * const ed = pipe->up_endpoint->ue_edesc; 2019 usb_endpoint_descriptor_t * const ed = pipe->up_endpoint->ue_edesc;
2020 const u_int dci = xhci_ep_get_dci(ed); 2020 const u_int dci = xhci_ep_get_dci(ed);
2021 const uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes); 2021 const uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
2022 usbd_status err; 2022 usbd_status err;
2023 2023
2024 XHCIHIST_FUNC(); 2024 XHCIHIST_FUNC();
2025 XHCIHIST_CALLARGS("addr %jd depth %jd port %jd speed %jd", dev->ud_addr, 2025 XHCIHIST_CALLARGS("addr %jd depth %jd port %jd speed %jd", dev->ud_addr,
2026 dev->ud_depth, dev->ud_powersrc->up_portno, dev->ud_speed); 2026 dev->ud_depth, dev->ud_powersrc->up_portno, dev->ud_speed);
2027 DPRINTFN(1, " dci %ju type 0x%02jx epaddr 0x%02jx attr 0x%02jx", 2027 DPRINTFN(1, " dci %ju type 0x%02jx epaddr 0x%02jx attr 0x%02jx",
2028 xhci_ep_get_dci(ed), ed->bDescriptorType, ed->bEndpointAddress, 2028 xhci_ep_get_dci(ed), ed->bDescriptorType, ed->bEndpointAddress,
2029 ed->bmAttributes); 2029 ed->bmAttributes);