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