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