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