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