xref: /netbsd-src/sys/dev/usb/xhci.c (revision 63aea4bd5b445e491ff0389fe27ec78b3099dba3)
1 /*	$NetBSD: xhci.c,v 1.32 2015/12/10 17:07:07 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 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: xhci.c,v 1.32 2015/12/10 17:07:07 skrll Exp $");
31 
32 #include "opt_usb.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/kmem.h>
38 #include <sys/malloc.h>
39 #include <sys/device.h>
40 #include <sys/select.h>
41 #include <sys/proc.h>
42 #include <sys/queue.h>
43 #include <sys/mutex.h>
44 #include <sys/condvar.h>
45 #include <sys/bus.h>
46 #include <sys/cpu.h>
47 #include <sys/sysctl.h>
48 
49 #include <machine/endian.h>
50 
51 #include <dev/usb/usb.h>
52 #include <dev/usb/usbdi.h>
53 #include <dev/usb/usbdivar.h>
54 #include <dev/usb/usbhist.h>
55 #include <dev/usb/usb_mem.h>
56 #include <dev/usb/usb_quirks.h>
57 
58 #include <dev/usb/xhcireg.h>
59 #include <dev/usb/xhcivar.h>
60 #include <dev/usb/usbroothub_subr.h>
61 
62 
63 #ifdef USB_DEBUG
64 #ifndef XHCI_DEBUG
65 #define xhcidebug 0
66 #else
67 static int xhcidebug = 0;
68 
69 SYSCTL_SETUP(sysctl_hw_xhci_setup, "sysctl hw.xhci setup")
70 {
71 	int err;
72 	const struct sysctlnode *rnode;
73 	const struct sysctlnode *cnode;
74 
75 	err = sysctl_createv(clog, 0, NULL, &rnode,
76 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "xhci",
77 	    SYSCTL_DESCR("xhci global controls"),
78 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
79 
80 	if (err)
81 		goto fail;
82 
83 	/* control debugging printfs */
84 	err = sysctl_createv(clog, 0, &rnode, &cnode,
85 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
86 	    "debug", SYSCTL_DESCR("Enable debugging output"),
87 	    NULL, 0, &xhcidebug, sizeof(xhcidebug), CTL_CREATE, CTL_EOL);
88 	if (err)
89 		goto fail;
90 
91 	return;
92 fail:
93 	aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
94 }
95 
96 #endif /* XHCI_DEBUG */
97 #endif /* USB_DEBUG */
98 
99 #define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(xhcidebug,N,FMT,A,B,C,D)
100 #define XHCIHIST_FUNC() USBHIST_FUNC()
101 #define XHCIHIST_CALLED(name) USBHIST_CALLED(xhcidebug)
102 
103 #define XHCI_DCI_SLOT 0
104 #define XHCI_DCI_EP_CONTROL 1
105 
106 #define XHCI_ICI_INPUT_CONTROL 0
107 
108 struct xhci_pipe {
109 	struct usbd_pipe xp_pipe;
110 };
111 
112 #define XHCI_INTR_ENDPT 1
113 #define XHCI_COMMAND_RING_TRBS 256
114 #define XHCI_EVENT_RING_TRBS 256
115 #define XHCI_EVENT_RING_SEGMENTS 1
116 #define XHCI_TRB_3_ED_BIT XHCI_TRB_3_ISP_BIT
117 
118 static usbd_status xhci_open(usbd_pipe_handle);
119 static int xhci_intr1(struct xhci_softc * const);
120 static void xhci_softintr(void *);
121 static void xhci_poll(struct usbd_bus *);
122 static usbd_status xhci_allocm(struct usbd_bus *, usb_dma_t *, uint32_t);
123 static void xhci_freem(struct usbd_bus *, usb_dma_t *);
124 static usbd_xfer_handle xhci_allocx(struct usbd_bus *);
125 static void xhci_freex(struct usbd_bus *, usbd_xfer_handle);
126 static void xhci_get_lock(struct usbd_bus *, kmutex_t **);
127 static usbd_status xhci_new_device(device_t, usbd_bus_handle, int, int, int,
128     struct usbd_port *);
129 
130 static usbd_status xhci_configure_endpoint(usbd_pipe_handle);
131 static usbd_status xhci_unconfigure_endpoint(usbd_pipe_handle);
132 static usbd_status xhci_reset_endpoint(usbd_pipe_handle);
133 //static usbd_status xhci_stop_endpoint(usbd_pipe_handle);
134 
135 static usbd_status xhci_set_dequeue(usbd_pipe_handle);
136 
137 static usbd_status xhci_do_command(struct xhci_softc * const,
138     struct xhci_trb * const, int);
139 static usbd_status xhci_init_slot(struct xhci_softc * const, uint32_t,
140     int, int, int, int);
141 static usbd_status xhci_enable_slot(struct xhci_softc * const,
142     uint8_t * const);
143 static usbd_status xhci_address_device(struct xhci_softc * const,
144     uint64_t, uint8_t, bool);
145 static usbd_status xhci_update_ep0_mps(struct xhci_softc * const,
146     struct xhci_slot * const, u_int);
147 static usbd_status xhci_ring_init(struct xhci_softc * const,
148     struct xhci_ring * const, size_t, size_t);
149 static void xhci_ring_free(struct xhci_softc * const, struct xhci_ring * const);
150 
151 static void xhci_noop(usbd_pipe_handle);
152 
153 static usbd_status xhci_root_ctrl_transfer(usbd_xfer_handle);
154 static usbd_status xhci_root_ctrl_start(usbd_xfer_handle);
155 static void xhci_root_ctrl_abort(usbd_xfer_handle);
156 static void xhci_root_ctrl_close(usbd_pipe_handle);
157 static void xhci_root_ctrl_done(usbd_xfer_handle);
158 
159 static usbd_status xhci_root_intr_transfer(usbd_xfer_handle);
160 static usbd_status xhci_root_intr_start(usbd_xfer_handle);
161 static void xhci_root_intr_abort(usbd_xfer_handle);
162 static void xhci_root_intr_close(usbd_pipe_handle);
163 static void xhci_root_intr_done(usbd_xfer_handle);
164 
165 static usbd_status xhci_device_ctrl_transfer(usbd_xfer_handle);
166 static usbd_status xhci_device_ctrl_start(usbd_xfer_handle);
167 static void xhci_device_ctrl_abort(usbd_xfer_handle);
168 static void xhci_device_ctrl_close(usbd_pipe_handle);
169 static void xhci_device_ctrl_done(usbd_xfer_handle);
170 
171 static usbd_status xhci_device_intr_transfer(usbd_xfer_handle);
172 static usbd_status xhci_device_intr_start(usbd_xfer_handle);
173 static void xhci_device_intr_abort(usbd_xfer_handle);
174 static void xhci_device_intr_close(usbd_pipe_handle);
175 static void xhci_device_intr_done(usbd_xfer_handle);
176 
177 static usbd_status xhci_device_bulk_transfer(usbd_xfer_handle);
178 static usbd_status xhci_device_bulk_start(usbd_xfer_handle);
179 static void xhci_device_bulk_abort(usbd_xfer_handle);
180 static void xhci_device_bulk_close(usbd_pipe_handle);
181 static void xhci_device_bulk_done(usbd_xfer_handle);
182 
183 static void xhci_timeout(void *);
184 static void xhci_timeout_task(void *);
185 
186 static const struct usbd_bus_methods xhci_bus_methods = {
187 	.open_pipe = xhci_open,
188 	.soft_intr = xhci_softintr,
189 	.do_poll = xhci_poll,
190 	.allocm = xhci_allocm,
191 	.freem = xhci_freem,
192 	.allocx = xhci_allocx,
193 	.freex = xhci_freex,
194 	.get_lock = xhci_get_lock,
195 	.new_device = xhci_new_device,
196 };
197 
198 static const struct usbd_pipe_methods xhci_root_ctrl_methods = {
199 	.transfer = xhci_root_ctrl_transfer,
200 	.start = xhci_root_ctrl_start,
201 	.abort = xhci_root_ctrl_abort,
202 	.close = xhci_root_ctrl_close,
203 	.cleartoggle = xhci_noop,
204 	.done = xhci_root_ctrl_done,
205 };
206 
207 static const struct usbd_pipe_methods xhci_root_intr_methods = {
208 	.transfer = xhci_root_intr_transfer,
209 	.start = xhci_root_intr_start,
210 	.abort = xhci_root_intr_abort,
211 	.close = xhci_root_intr_close,
212 	.cleartoggle = xhci_noop,
213 	.done = xhci_root_intr_done,
214 };
215 
216 
217 static const struct usbd_pipe_methods xhci_device_ctrl_methods = {
218 	.transfer = xhci_device_ctrl_transfer,
219 	.start = xhci_device_ctrl_start,
220 	.abort = xhci_device_ctrl_abort,
221 	.close = xhci_device_ctrl_close,
222 	.cleartoggle = xhci_noop,
223 	.done = xhci_device_ctrl_done,
224 };
225 
226 static const struct usbd_pipe_methods xhci_device_isoc_methods = {
227 	.cleartoggle = xhci_noop,
228 };
229 
230 static const struct usbd_pipe_methods xhci_device_bulk_methods = {
231 	.transfer = xhci_device_bulk_transfer,
232 	.start = xhci_device_bulk_start,
233 	.abort = xhci_device_bulk_abort,
234 	.close = xhci_device_bulk_close,
235 	.cleartoggle = xhci_noop,
236 	.done = xhci_device_bulk_done,
237 };
238 
239 static const struct usbd_pipe_methods xhci_device_intr_methods = {
240 	.transfer = xhci_device_intr_transfer,
241 	.start = xhci_device_intr_start,
242 	.abort = xhci_device_intr_abort,
243 	.close = xhci_device_intr_close,
244 	.cleartoggle = xhci_noop,
245 	.done = xhci_device_intr_done,
246 };
247 
248 static inline uint32_t
249 xhci_read_4(const struct xhci_softc * const sc, bus_size_t offset)
250 {
251 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh, offset);
252 }
253 
254 #if 0 /* unused */
255 static inline void
256 xhci_write_4(const struct xhci_softc * const sc, bus_size_t offset,
257     uint32_t value)
258 {
259 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, offset, value);
260 }
261 #endif /* unused */
262 
263 static inline uint32_t
264 xhci_cap_read_4(const struct xhci_softc * const sc, bus_size_t offset)
265 {
266 	return bus_space_read_4(sc->sc_iot, sc->sc_cbh, offset);
267 }
268 
269 static inline uint32_t
270 xhci_op_read_4(const struct xhci_softc * const sc, bus_size_t offset)
271 {
272 	return bus_space_read_4(sc->sc_iot, sc->sc_obh, offset);
273 }
274 
275 static inline void
276 xhci_op_write_4(const struct xhci_softc * const sc, bus_size_t offset,
277     uint32_t value)
278 {
279 	bus_space_write_4(sc->sc_iot, sc->sc_obh, offset, value);
280 }
281 
282 #if 0 /* unused */
283 static inline uint64_t
284 xhci_op_read_8(const struct xhci_softc * const sc, bus_size_t offset)
285 {
286 	uint64_t value;
287 
288 	if (sc->sc_ac64) {
289 #ifdef XHCI_USE_BUS_SPACE_8
290 		value = bus_space_read_8(sc->sc_iot, sc->sc_obh, offset);
291 #else
292 		value = bus_space_read_4(sc->sc_iot, sc->sc_obh, offset);
293 		value |= (uint64_t)bus_space_read_4(sc->sc_iot, sc->sc_obh,
294 		    offset + 4) << 32;
295 #endif
296 	} else {
297 		value = bus_space_read_4(sc->sc_iot, sc->sc_obh, offset);
298 	}
299 
300 	return value;
301 }
302 #endif /* unused */
303 
304 static inline void
305 xhci_op_write_8(const struct xhci_softc * const sc, bus_size_t offset,
306     uint64_t value)
307 {
308 	if (sc->sc_ac64) {
309 #ifdef XHCI_USE_BUS_SPACE_8
310 		bus_space_write_8(sc->sc_iot, sc->sc_obh, offset, value);
311 #else
312 		bus_space_write_4(sc->sc_iot, sc->sc_obh, offset + 0,
313 		    (value >> 0) & 0xffffffff);
314 		bus_space_write_4(sc->sc_iot, sc->sc_obh, offset + 4,
315 		    (value >> 32) & 0xffffffff);
316 #endif
317 	} else {
318 		bus_space_write_4(sc->sc_iot, sc->sc_obh, offset, value);
319 	}
320 }
321 
322 static inline uint32_t
323 xhci_rt_read_4(const struct xhci_softc * const sc, bus_size_t offset)
324 {
325 	return bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset);
326 }
327 
328 static inline void
329 xhci_rt_write_4(const struct xhci_softc * const sc, bus_size_t offset,
330     uint32_t value)
331 {
332 	bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset, value);
333 }
334 
335 #if 0 /* unused */
336 static inline uint64_t
337 xhci_rt_read_8(const struct xhci_softc * const sc, bus_size_t offset)
338 {
339 	uint64_t value;
340 
341 	if (sc->sc_ac64) {
342 #ifdef XHCI_USE_BUS_SPACE_8
343 		value = bus_space_read_8(sc->sc_iot, sc->sc_rbh, offset);
344 #else
345 		value = bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset);
346 		value |= (uint64_t)bus_space_read_4(sc->sc_iot, sc->sc_rbh,
347 		    offset + 4) << 32;
348 #endif
349 	} else {
350 		value = bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset);
351 	}
352 
353 	return value;
354 }
355 #endif /* unused */
356 
357 static inline void
358 xhci_rt_write_8(const struct xhci_softc * const sc, bus_size_t offset,
359     uint64_t value)
360 {
361 	if (sc->sc_ac64) {
362 #ifdef XHCI_USE_BUS_SPACE_8
363 		bus_space_write_8(sc->sc_iot, sc->sc_rbh, offset, value);
364 #else
365 		bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset + 0,
366 		    (value >> 0) & 0xffffffff);
367 		bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset + 4,
368 		    (value >> 32) & 0xffffffff);
369 #endif
370 	} else {
371 		bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset, value);
372 	}
373 }
374 
375 #if 0 /* unused */
376 static inline uint32_t
377 xhci_db_read_4(const struct xhci_softc * const sc, bus_size_t offset)
378 {
379 	return bus_space_read_4(sc->sc_iot, sc->sc_dbh, offset);
380 }
381 #endif /* unused */
382 
383 static inline void
384 xhci_db_write_4(const struct xhci_softc * const sc, bus_size_t offset,
385     uint32_t value)
386 {
387 	bus_space_write_4(sc->sc_iot, sc->sc_dbh, offset, value);
388 }
389 
390 /* --- */
391 
392 static inline uint8_t
393 xhci_ep_get_type(usb_endpoint_descriptor_t * const ed)
394 {
395 	u_int eptype;
396 
397 	switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
398 	case UE_CONTROL:
399 		eptype = 0x0;
400 		break;
401 	case UE_ISOCHRONOUS:
402 		eptype = 0x1;
403 		break;
404 	case UE_BULK:
405 		eptype = 0x2;
406 		break;
407 	case UE_INTERRUPT:
408 		eptype = 0x3;
409 		break;
410 	}
411 
412 	if ((UE_GET_XFERTYPE(ed->bmAttributes) == UE_CONTROL) ||
413 	    (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN))
414 		return eptype | 0x4;
415 	else
416 		return eptype;
417 }
418 
419 static u_int
420 xhci_ep_get_dci(usb_endpoint_descriptor_t * const ed)
421 {
422 	/* xHCI 1.0 section 4.5.1 */
423 	u_int epaddr = UE_GET_ADDR(ed->bEndpointAddress);
424 	u_int in = 0;
425 
426 	if ((UE_GET_XFERTYPE(ed->bmAttributes) == UE_CONTROL) ||
427 	    (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN))
428 		in = 1;
429 
430 	return epaddr * 2 + in;
431 }
432 
433 static inline u_int
434 xhci_dci_to_ici(const u_int i)
435 {
436 	return i + 1;
437 }
438 
439 static inline void *
440 xhci_slot_get_dcv(struct xhci_softc * const sc, struct xhci_slot * const xs,
441     const u_int dci)
442 {
443 	return KERNADDR(&xs->xs_dc_dma, sc->sc_ctxsz * dci);
444 }
445 
446 #if 0 /* unused */
447 static inline bus_addr_t
448 xhci_slot_get_dcp(struct xhci_softc * const sc, struct xhci_slot * const xs,
449     const u_int dci)
450 {
451 	return DMAADDR(&xs->xs_dc_dma, sc->sc_ctxsz * dci);
452 }
453 #endif /* unused */
454 
455 static inline void *
456 xhci_slot_get_icv(struct xhci_softc * const sc, struct xhci_slot * const xs,
457     const u_int ici)
458 {
459 	return KERNADDR(&xs->xs_ic_dma, sc->sc_ctxsz * ici);
460 }
461 
462 static inline bus_addr_t
463 xhci_slot_get_icp(struct xhci_softc * const sc, struct xhci_slot * const xs,
464     const u_int ici)
465 {
466 	return DMAADDR(&xs->xs_ic_dma, sc->sc_ctxsz * ici);
467 }
468 
469 static inline struct xhci_trb *
470 xhci_ring_trbv(struct xhci_ring * const xr, u_int idx)
471 {
472 	return KERNADDR(&xr->xr_dma, XHCI_TRB_SIZE * idx);
473 }
474 
475 static inline bus_addr_t
476 xhci_ring_trbp(struct xhci_ring * const xr, u_int idx)
477 {
478 	return DMAADDR(&xr->xr_dma, XHCI_TRB_SIZE * idx);
479 }
480 
481 static inline void
482 xhci_trb_put(struct xhci_trb * const trb, uint64_t parameter, uint32_t status,
483     uint32_t control)
484 {
485 	trb->trb_0 = parameter;
486 	trb->trb_2 = status;
487 	trb->trb_3 = control;
488 }
489 
490 /* --- */
491 
492 void
493 xhci_childdet(device_t self, device_t child)
494 {
495 	struct xhci_softc * const sc = device_private(self);
496 
497 	KASSERT(sc->sc_child == child);
498 	if (child == sc->sc_child)
499 		sc->sc_child = NULL;
500 }
501 
502 int
503 xhci_detach(struct xhci_softc *sc, int flags)
504 {
505 	int rv = 0;
506 
507 	if (sc->sc_child != NULL)
508 		rv = config_detach(sc->sc_child, flags);
509 
510 	if (rv != 0)
511 		return (rv);
512 
513 	/* XXX unconfigure/free slots */
514 
515 	/* verify: */
516 	xhci_rt_write_4(sc, XHCI_IMAN(0), 0);
517 	xhci_op_write_4(sc, XHCI_USBCMD, 0);
518 	/* do we need to wait for stop? */
519 
520 	xhci_op_write_8(sc, XHCI_CRCR, 0);
521 	xhci_ring_free(sc, &sc->sc_cr);
522 	cv_destroy(&sc->sc_command_cv);
523 
524 	xhci_rt_write_4(sc, XHCI_ERSTSZ(0), 0);
525 	xhci_rt_write_8(sc, XHCI_ERSTBA(0), 0);
526 	xhci_rt_write_8(sc, XHCI_ERDP(0), 0|XHCI_ERDP_LO_BUSY);
527 	xhci_ring_free(sc, &sc->sc_er);
528 
529 	usb_freemem(&sc->sc_bus, &sc->sc_eventst_dma);
530 
531 	xhci_op_write_8(sc, XHCI_DCBAAP, 0);
532 	usb_freemem(&sc->sc_bus, &sc->sc_dcbaa_dma);
533 
534 	kmem_free(sc->sc_slots, sizeof(*sc->sc_slots) * sc->sc_maxslots);
535 
536 	mutex_destroy(&sc->sc_lock);
537 	mutex_destroy(&sc->sc_intr_lock);
538 
539 	pool_cache_destroy(sc->sc_xferpool);
540 
541 	return rv;
542 }
543 
544 int
545 xhci_activate(device_t self, enum devact act)
546 {
547 	struct xhci_softc * const sc = device_private(self);
548 
549 	switch (act) {
550 	case DVACT_DEACTIVATE:
551 		sc->sc_dying = true;
552 		return 0;
553 	default:
554 		return EOPNOTSUPP;
555 	}
556 }
557 
558 bool
559 xhci_suspend(device_t dv, const pmf_qual_t *qual)
560 {
561 	return false;
562 }
563 
564 bool
565 xhci_resume(device_t dv, const pmf_qual_t *qual)
566 {
567 	return false;
568 }
569 
570 bool
571 xhci_shutdown(device_t self, int flags)
572 {
573 	return false;
574 }
575 
576 
577 static void
578 hexdump(const char *msg, const void *base, size_t len)
579 {
580 #if 0
581 	size_t cnt;
582 	const uint32_t *p;
583 	extern paddr_t vtophys(vaddr_t);
584 
585 	p = base;
586 	cnt = 0;
587 
588 	printf("*** %s (%zu bytes @ %p %p)\n", msg, len, base,
589 	    (void *)vtophys((vaddr_t)base));
590 
591 	while (cnt < len) {
592 		if (cnt % 16 == 0)
593 			printf("%p: ", p);
594 		else if (cnt % 8 == 0)
595 			printf(" |");
596 		printf(" %08x", *p++);
597 		cnt += 4;
598 		if (cnt % 16 == 0)
599 			printf("\n");
600 	}
601 #endif
602 }
603 
604 
605 int
606 xhci_init(struct xhci_softc *sc)
607 {
608 	bus_size_t bsz;
609 	uint32_t cap, hcs1, hcs2, hcc, dboff, rtsoff;
610 	uint32_t ecp, ecr;
611 	uint32_t usbcmd, usbsts, pagesize, config;
612 	int i;
613 	uint16_t hciversion;
614 	uint8_t caplength;
615 
616 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
617 
618 	/* XXX Low/Full/High speeds for now */
619 	sc->sc_bus.usbrev = USBREV_2_0;
620 
621 	cap = xhci_read_4(sc, XHCI_CAPLENGTH);
622 	caplength = XHCI_CAP_CAPLENGTH(cap);
623 	hciversion = XHCI_CAP_HCIVERSION(cap);
624 
625 	if ((hciversion < 0x0096) || (hciversion > 0x0100)) {
626 		aprint_normal_dev(sc->sc_dev,
627 		    "xHCI version %x.%x not known to be supported\n",
628 		    (hciversion >> 8) & 0xff, (hciversion >> 0) & 0xff);
629 	} else {
630 		aprint_verbose_dev(sc->sc_dev, "xHCI version %x.%x\n",
631 		    (hciversion >> 8) & 0xff, (hciversion >> 0) & 0xff);
632 	}
633 
634 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0, caplength,
635 	    &sc->sc_cbh) != 0) {
636 		aprint_error_dev(sc->sc_dev, "capability subregion failure\n");
637 		return ENOMEM;
638 	}
639 
640 	hcs1 = xhci_cap_read_4(sc, XHCI_HCSPARAMS1);
641 	sc->sc_maxslots = XHCI_HCS1_MAXSLOTS(hcs1);
642 	sc->sc_maxintrs = XHCI_HCS1_MAXINTRS(hcs1);
643 	sc->sc_maxports = XHCI_HCS1_MAXPORTS(hcs1);
644 	hcs2 = xhci_cap_read_4(sc, XHCI_HCSPARAMS2);
645 	(void)xhci_cap_read_4(sc, XHCI_HCSPARAMS3);
646 	hcc = xhci_cap_read_4(sc, XHCI_HCCPARAMS);
647 
648 	sc->sc_ac64 = XHCI_HCC_AC64(hcc);
649 	sc->sc_ctxsz = XHCI_HCC_CSZ(hcc) ? 64 : 32;
650 	aprint_debug_dev(sc->sc_dev, "ac64 %d ctxsz %d\n", sc->sc_ac64,
651 	    sc->sc_ctxsz);
652 
653 	aprint_debug_dev(sc->sc_dev, "xECP %x\n", XHCI_HCC_XECP(hcc) * 4);
654 	ecp = XHCI_HCC_XECP(hcc) * 4;
655 	while (ecp != 0) {
656 		ecr = xhci_read_4(sc, ecp);
657 		aprint_debug_dev(sc->sc_dev, "ECR %x: %08x\n", ecp, ecr);
658 		switch (XHCI_XECP_ID(ecr)) {
659 		case XHCI_ID_PROTOCOLS: {
660 			uint32_t w0, w4, w8;
661 			uint16_t w2;
662 			w0 = xhci_read_4(sc, ecp + 0);
663 			w2 = (w0 >> 16) & 0xffff;
664 			w4 = xhci_read_4(sc, ecp + 4);
665 			w8 = xhci_read_4(sc, ecp + 8);
666 			aprint_debug_dev(sc->sc_dev, "SP: %08x %08x %08x\n",
667 			    w0, w4, w8);
668 			if (w4 == 0x20425355 && w2 == 0x0300) {
669 				sc->sc_ss_port_start = (w8 >> 0) & 0xff;;
670 				sc->sc_ss_port_count = (w8 >> 8) & 0xff;;
671 			}
672 			if (w4 == 0x20425355 && w2 == 0x0200) {
673 				sc->sc_hs_port_start = (w8 >> 0) & 0xff;
674 				sc->sc_hs_port_count = (w8 >> 8) & 0xff;
675 			}
676 			break;
677 		}
678 		default:
679 			break;
680 		}
681 		ecr = xhci_read_4(sc, ecp);
682 		if (XHCI_XECP_NEXT(ecr) == 0) {
683 			ecp = 0;
684 		} else {
685 			ecp += XHCI_XECP_NEXT(ecr) * 4;
686 		}
687 	}
688 
689 	bsz = XHCI_PORTSC(sc->sc_maxports + 1);
690 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, caplength, bsz,
691 	    &sc->sc_obh) != 0) {
692 		aprint_error_dev(sc->sc_dev, "operational subregion failure\n");
693 		return ENOMEM;
694 	}
695 
696 	dboff = xhci_cap_read_4(sc, XHCI_DBOFF);
697 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, dboff,
698 	    sc->sc_maxslots * 4, &sc->sc_dbh) != 0) {
699 		aprint_error_dev(sc->sc_dev, "doorbell subregion failure\n");
700 		return ENOMEM;
701 	}
702 
703 	rtsoff = xhci_cap_read_4(sc, XHCI_RTSOFF);
704 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, rtsoff,
705 	    sc->sc_maxintrs * 0x20, &sc->sc_rbh) != 0) {
706 		aprint_error_dev(sc->sc_dev, "runtime subregion failure\n");
707 		return ENOMEM;
708 	}
709 
710 	for (i = 0; i < 100; i++) {
711 		usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
712 		if ((usbsts & XHCI_STS_CNR) == 0)
713 			break;
714 		usb_delay_ms(&sc->sc_bus, 1);
715 	}
716 	if (i >= 100)
717 		return EIO;
718 
719 	usbcmd = 0;
720 	xhci_op_write_4(sc, XHCI_USBCMD, usbcmd);
721 	usb_delay_ms(&sc->sc_bus, 1);
722 
723 	usbcmd = XHCI_CMD_HCRST;
724 	xhci_op_write_4(sc, XHCI_USBCMD, usbcmd);
725 	for (i = 0; i < 100; i++) {
726 		usbcmd = xhci_op_read_4(sc, XHCI_USBCMD);
727 		if ((usbcmd & XHCI_CMD_HCRST) == 0)
728 			break;
729 		usb_delay_ms(&sc->sc_bus, 1);
730 	}
731 	if (i >= 100)
732 		return EIO;
733 
734 	for (i = 0; i < 100; i++) {
735 		usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
736 		if ((usbsts & XHCI_STS_CNR) == 0)
737 			break;
738 		usb_delay_ms(&sc->sc_bus, 1);
739 	}
740 	if (i >= 100)
741 		return EIO;
742 
743 	pagesize = xhci_op_read_4(sc, XHCI_PAGESIZE);
744 	aprint_debug_dev(sc->sc_dev, "PAGESIZE 0x%08x\n", pagesize);
745 	pagesize = ffs(pagesize);
746 	if (pagesize == 0)
747 		return EIO;
748 	sc->sc_pgsz = 1 << (12 + (pagesize - 1));
749 	aprint_debug_dev(sc->sc_dev, "sc_pgsz 0x%08x\n", (uint32_t)sc->sc_pgsz);
750 	aprint_debug_dev(sc->sc_dev, "sc_maxslots 0x%08x\n",
751 	    (uint32_t)sc->sc_maxslots);
752 
753 	usbd_status err;
754 
755 	sc->sc_maxspbuf = XHCI_HCS2_MAXSPBUF(hcs2);
756 	aprint_debug_dev(sc->sc_dev, "sc_maxspbuf %d\n", sc->sc_maxspbuf);
757 	if (sc->sc_maxspbuf != 0) {
758 		err = usb_allocmem(&sc->sc_bus,
759 		    sizeof(uint64_t) * sc->sc_maxspbuf, sizeof(uint64_t),
760 		    &sc->sc_spbufarray_dma);
761 		if (err)
762 			return err;
763 
764 		sc->sc_spbuf_dma = kmem_zalloc(sizeof(*sc->sc_spbuf_dma) * sc->sc_maxspbuf, KM_SLEEP);
765 		uint64_t *spbufarray = KERNADDR(&sc->sc_spbufarray_dma, 0);
766 		for (i = 0; i < sc->sc_maxspbuf; i++) {
767 			usb_dma_t * const dma = &sc->sc_spbuf_dma[i];
768 			/* allocate contexts */
769 			err = usb_allocmem(&sc->sc_bus, sc->sc_pgsz,
770 			    sc->sc_pgsz, dma);
771 			if (err)
772 				return err;
773 			spbufarray[i] = htole64(DMAADDR(dma, 0));
774 			usb_syncmem(dma, 0, sc->sc_pgsz,
775 			    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
776 		}
777 
778 		usb_syncmem(&sc->sc_spbufarray_dma, 0,
779 		    sizeof(uint64_t) * sc->sc_maxspbuf, BUS_DMASYNC_PREWRITE);
780 	}
781 
782 	config = xhci_op_read_4(sc, XHCI_CONFIG);
783 	config &= ~0xFF;
784 	config |= sc->sc_maxslots & 0xFF;
785 	xhci_op_write_4(sc, XHCI_CONFIG, config);
786 
787 	err = xhci_ring_init(sc, &sc->sc_cr, XHCI_COMMAND_RING_TRBS,
788 	    XHCI_COMMAND_RING_SEGMENTS_ALIGN);
789 	if (err) {
790 		aprint_error_dev(sc->sc_dev, "command ring init fail\n");
791 		return err;
792 	}
793 
794 	err = xhci_ring_init(sc, &sc->sc_er, XHCI_EVENT_RING_TRBS,
795 	    XHCI_EVENT_RING_SEGMENTS_ALIGN);
796 	if (err) {
797 		aprint_error_dev(sc->sc_dev, "event ring init fail\n");
798 		return err;
799 	}
800 
801 	usb_dma_t *dma;
802 	size_t size;
803 	size_t align;
804 
805 	dma = &sc->sc_eventst_dma;
806 	size = roundup2(XHCI_EVENT_RING_SEGMENTS * XHCI_ERSTE_SIZE,
807 	    XHCI_EVENT_RING_SEGMENT_TABLE_ALIGN);
808 	KASSERT(size <= (512 * 1024));
809 	align = XHCI_EVENT_RING_SEGMENT_TABLE_ALIGN;
810 	err = usb_allocmem(&sc->sc_bus, size, align, dma);
811 
812 	memset(KERNADDR(dma, 0), 0, size);
813 	usb_syncmem(dma, 0, size, BUS_DMASYNC_PREWRITE);
814 	aprint_debug_dev(sc->sc_dev, "eventst: %s %016jx %p %zx\n",
815 	    usbd_errstr(err),
816 	    (uintmax_t)DMAADDR(&sc->sc_eventst_dma, 0),
817 	    KERNADDR(&sc->sc_eventst_dma, 0),
818 	    sc->sc_eventst_dma.block->size);
819 
820 	dma = &sc->sc_dcbaa_dma;
821 	size = (1 + sc->sc_maxslots) * sizeof(uint64_t);
822 	KASSERT(size <= 2048);
823 	align = XHCI_DEVICE_CONTEXT_BASE_ADDRESS_ARRAY_ALIGN;
824 	err = usb_allocmem(&sc->sc_bus, size, align, dma);
825 
826 	memset(KERNADDR(dma, 0), 0, size);
827 	if (sc->sc_maxspbuf != 0) {
828 		/*
829 		 * DCBA entry 0 hold the scratchbuf array pointer.
830 		 */
831 		*(uint64_t *)KERNADDR(dma, 0) =
832 		    htole64(DMAADDR(&sc->sc_spbufarray_dma, 0));
833 	}
834 	usb_syncmem(dma, 0, size, BUS_DMASYNC_PREWRITE);
835 	aprint_debug_dev(sc->sc_dev, "dcbaa: %s %016jx %p %zx\n",
836 	    usbd_errstr(err),
837 	    (uintmax_t)DMAADDR(&sc->sc_dcbaa_dma, 0),
838 	    KERNADDR(&sc->sc_dcbaa_dma, 0),
839 	    sc->sc_dcbaa_dma.block->size);
840 
841 	sc->sc_slots = kmem_zalloc(sizeof(*sc->sc_slots) * sc->sc_maxslots,
842 	    KM_SLEEP);
843 
844 	cv_init(&sc->sc_command_cv, "xhcicmd");
845 
846 	struct xhci_erste *erst;
847 	erst = KERNADDR(&sc->sc_eventst_dma, 0);
848 	erst[0].erste_0 = htole64(xhci_ring_trbp(&sc->sc_er, 0));
849 	erst[0].erste_2 = htole32(XHCI_EVENT_RING_TRBS);
850 	erst[0].erste_3 = htole32(0);
851 	usb_syncmem(&sc->sc_eventst_dma, 0,
852 	    XHCI_ERSTE_SIZE * XHCI_EVENT_RING_SEGMENTS, BUS_DMASYNC_PREWRITE);
853 
854 	xhci_rt_write_4(sc, XHCI_ERSTSZ(0), XHCI_EVENT_RING_SEGMENTS);
855 	xhci_rt_write_8(sc, XHCI_ERSTBA(0), DMAADDR(&sc->sc_eventst_dma, 0));
856 	xhci_rt_write_8(sc, XHCI_ERDP(0), xhci_ring_trbp(&sc->sc_er, 0) |
857 	    XHCI_ERDP_LO_BUSY);
858 	xhci_op_write_8(sc, XHCI_DCBAAP, DMAADDR(&sc->sc_dcbaa_dma, 0));
859 	xhci_op_write_8(sc, XHCI_CRCR, xhci_ring_trbp(&sc->sc_cr, 0) |
860 	    sc->sc_cr.xr_cs);
861 
862 #if 0
863 	hexdump("eventst", KERNADDR(&sc->sc_eventst_dma, 0),
864 	    XHCI_ERSTE_SIZE * XHCI_EVENT_RING_SEGMENTS);
865 #endif
866 
867 	xhci_rt_write_4(sc, XHCI_IMAN(0), XHCI_IMAN_INTR_ENA);
868 	xhci_rt_write_4(sc, XHCI_IMOD(0), 0);
869 
870 	xhci_op_write_4(sc, XHCI_USBCMD, XHCI_CMD_INTE|XHCI_CMD_RS); /* Go! */
871 	aprint_debug_dev(sc->sc_dev, "USBCMD %08"PRIx32"\n",
872 	    xhci_op_read_4(sc, XHCI_USBCMD));
873 
874 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
875 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_USB);
876 	cv_init(&sc->sc_softwake_cv, "xhciab");
877 
878 	sc->sc_xferpool = pool_cache_init(sizeof(struct xhci_xfer), 0, 0, 0,
879 	    "xhcixfer", NULL, IPL_USB, NULL, NULL, NULL);
880 
881 	/* Set up the bus struct. */
882 	sc->sc_bus.methods = &xhci_bus_methods;
883 	sc->sc_bus.pipe_size = sizeof(struct xhci_pipe);
884 
885 	return USBD_NORMAL_COMPLETION;
886 }
887 
888 int
889 xhci_intr(void *v)
890 {
891 	struct xhci_softc * const sc = v;
892 	int ret = 0;
893 
894 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
895 
896 	if (sc == NULL)
897 		return 0;
898 
899 	mutex_spin_enter(&sc->sc_intr_lock);
900 
901 	if (sc->sc_dying || !device_has_power(sc->sc_dev))
902 		goto done;
903 
904 	/* If we get an interrupt while polling, then just ignore it. */
905 	if (sc->sc_bus.use_polling) {
906 #ifdef DIAGNOSTIC
907 		DPRINTFN(16, "ignored interrupt while polling", 0, 0, 0, 0);
908 #endif
909 		goto done;
910 	}
911 
912 	ret = xhci_intr1(sc);
913 done:
914 	mutex_spin_exit(&sc->sc_intr_lock);
915 	return ret;
916 }
917 
918 int
919 xhci_intr1(struct xhci_softc * const sc)
920 {
921 	uint32_t usbsts;
922 	uint32_t iman;
923 
924 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
925 
926 	usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
927 	DPRINTFN(16, "USBSTS %08x", usbsts, 0, 0, 0);
928 #if 0
929 	if ((usbsts & (XHCI_STS_EINT|XHCI_STS_PCD)) == 0) {
930 		return 0;
931 	}
932 #endif
933 	xhci_op_write_4(sc, XHCI_USBSTS,
934 	    usbsts & (2|XHCI_STS_EINT|XHCI_STS_PCD)); /* XXX */
935 	usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
936 	DPRINTFN(16, "USBSTS %08x", usbsts, 0, 0, 0);
937 
938 	iman = xhci_rt_read_4(sc, XHCI_IMAN(0));
939 	DPRINTFN(16, "IMAN0 %08x", iman, 0, 0, 0);
940 	if ((iman & XHCI_IMAN_INTR_PEND) == 0) {
941 		return 0;
942 	}
943 	xhci_rt_write_4(sc, XHCI_IMAN(0), iman);
944 	iman = xhci_rt_read_4(sc, XHCI_IMAN(0));
945 	DPRINTFN(16, "IMAN0 %08x", iman, 0, 0, 0);
946 	usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
947 	DPRINTFN(16, "USBSTS %08x", usbsts, 0, 0, 0);
948 
949 	sc->sc_bus.no_intrs++;
950 	usb_schedsoftintr(&sc->sc_bus);
951 
952 	return 1;
953 }
954 
955 static usbd_status
956 xhci_configure_endpoint(usbd_pipe_handle pipe)
957 {
958 	struct xhci_softc * const sc = pipe->device->bus->hci_private;
959 	struct xhci_slot * const xs = pipe->device->hci_private;
960 	const u_int dci = xhci_ep_get_dci(pipe->endpoint->edesc);
961 	usb_endpoint_descriptor_t * const ed = pipe->endpoint->edesc;
962 	const uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
963 	struct xhci_trb trb;
964 	usbd_status err;
965 	uint32_t *cp;
966 
967 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
968 	DPRINTFN(4, "dci %u epaddr 0x%02x attr 0x%02x",
969 	    dci, ed->bEndpointAddress, ed->bmAttributes, 0);
970 
971 	/* XXX ensure input context is available? */
972 
973 	memset(xhci_slot_get_icv(sc, xs, 0), 0, sc->sc_pgsz);
974 
975 	cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL);
976 	cp[0] = htole32(0);
977 	cp[1] = htole32(XHCI_INCTX_1_ADD_MASK(dci));
978 
979 	/* set up input slot context */
980 	cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_SLOT));
981 	cp[0] = htole32(XHCI_SCTX_0_CTX_NUM_SET(dci));
982 	cp[1] = htole32(0);
983 	cp[2] = htole32(0);
984 	cp[3] = htole32(0);
985 
986 	cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(dci));
987 	uint8_t eptype = xhci_ep_get_type(pipe->endpoint->edesc);
988 	if (xfertype == UE_INTERRUPT) {
989 		cp[0] = htole32(
990 		    XHCI_EPCTX_0_IVAL_SET(3) /* XXX */
991 		    );
992 		cp[1] = htole32(
993 		    XHCI_EPCTX_1_CERR_SET(3) |
994 		    XHCI_EPCTX_1_EPTYPE_SET(eptype) |
995 		    XHCI_EPCTX_1_MAXB_SET(0) |
996 		    XHCI_EPCTX_1_MAXP_SIZE_SET(8) /* XXX */
997 		    );
998 		cp[4] = htole32(
999 		    XHCI_EPCTX_4_AVG_TRB_LEN_SET(8)
1000 		    );
1001 	} else {
1002 		cp[0] = htole32(0);
1003 		cp[1] = htole32(
1004 		    XHCI_EPCTX_1_CERR_SET(3) |
1005 		    XHCI_EPCTX_1_EPTYPE_SET(eptype) |
1006 		    XHCI_EPCTX_1_MAXB_SET(0) |
1007 		    XHCI_EPCTX_1_MAXP_SIZE_SET(512) /* XXX */
1008 		    );
1009 	}
1010 	*(uint64_t *)(&cp[2]) = htole64(
1011 	    xhci_ring_trbp(&xs->xs_ep[dci].xe_tr, 0) |
1012 	    XHCI_EPCTX_2_DCS_SET(1));
1013 
1014 	/* sync input contexts before they are read from memory */
1015 	usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE);
1016 	hexdump("input control context", xhci_slot_get_icv(sc, xs, 0),
1017 	    sc->sc_ctxsz * 1);
1018 	hexdump("input endpoint context", xhci_slot_get_icv(sc, xs,
1019 	    xhci_dci_to_ici(dci)), sc->sc_ctxsz * 1);
1020 
1021 	trb.trb_0 = xhci_slot_get_icp(sc, xs, 0);
1022 	trb.trb_2 = 0;
1023 	trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
1024 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_CONFIGURE_EP);
1025 
1026 	err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1027 
1028 	usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
1029 	hexdump("output context", xhci_slot_get_dcv(sc, xs, dci),
1030 	    sc->sc_ctxsz * 1);
1031 
1032 	return err;
1033 }
1034 
1035 static usbd_status
1036 xhci_unconfigure_endpoint(usbd_pipe_handle pipe)
1037 {
1038 #ifdef USB_DEBUG
1039 	struct xhci_slot * const xs = pipe->device->hci_private;
1040 #endif
1041 
1042 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
1043 	DPRINTFN(4, "slot %u", xs->xs_idx, 0, 0, 0);
1044 
1045 	return USBD_NORMAL_COMPLETION;
1046 }
1047 
1048 static usbd_status
1049 xhci_reset_endpoint(usbd_pipe_handle pipe)
1050 {
1051 	struct xhci_softc * const sc = pipe->device->bus->hci_private;
1052 	struct xhci_slot * const xs = pipe->device->hci_private;
1053 	const u_int dci = xhci_ep_get_dci(pipe->endpoint->edesc);
1054 	struct xhci_trb trb;
1055 	usbd_status err;
1056 
1057 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
1058 	DPRINTFN(4, "dci %u", dci, 0, 0, 0);
1059 
1060 	trb.trb_0 = 0;
1061 	trb.trb_2 = 0;
1062 	trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
1063 	    XHCI_TRB_3_EP_SET(dci) |
1064 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_EP);
1065 
1066 	err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1067 
1068 	return err;
1069 }
1070 
1071 #if 0
1072 static usbd_status
1073 xhci_stop_endpoint(usbd_pipe_handle pipe)
1074 {
1075 	struct xhci_softc * const sc = pipe->device->bus->hci_private;
1076 	struct xhci_slot * const xs = pipe->device->hci_private;
1077 	struct xhci_trb trb;
1078 	usbd_status err;
1079 	const u_int dci = xhci_ep_get_dci(pipe->endpoint->edesc);
1080 
1081 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
1082 	DPRINTFN(4, "dci %u", dci, 0, 0, 0);
1083 
1084 	trb.trb_0 = 0;
1085 	trb.trb_2 = 0;
1086 	trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
1087 	    XHCI_TRB_3_EP_SET(dci) |
1088 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STOP_EP);
1089 
1090 	err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1091 
1092 	return err;
1093 }
1094 #endif
1095 
1096 static usbd_status
1097 xhci_set_dequeue(usbd_pipe_handle pipe)
1098 {
1099 	struct xhci_softc * const sc = pipe->device->bus->hci_private;
1100 	struct xhci_slot * const xs = pipe->device->hci_private;
1101 	const u_int dci = xhci_ep_get_dci(pipe->endpoint->edesc);
1102 	struct xhci_ring * const xr = &xs->xs_ep[dci].xe_tr;
1103 	struct xhci_trb trb;
1104 	usbd_status err;
1105 
1106 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
1107 	DPRINTFN(4, "slot %u dci %u", xs->xs_idx, dci, 0, 0);
1108 
1109 	memset(xr->xr_trb, 0, xr->xr_ntrb * XHCI_TRB_SIZE);
1110 	usb_syncmem(&xr->xr_dma, 0, xr->xr_ntrb * XHCI_TRB_SIZE,
1111 	    BUS_DMASYNC_PREWRITE);
1112 
1113 	xr->xr_ep = 0;
1114 	xr->xr_cs = 1;
1115 
1116 	trb.trb_0 = xhci_ring_trbp(xr, 0) | 1; /* XXX */
1117 	trb.trb_2 = 0;
1118 	trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
1119 	    XHCI_TRB_3_EP_SET(dci) |
1120 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SET_TR_DEQUEUE);
1121 
1122 	err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1123 
1124 	return err;
1125 }
1126 
1127 static usbd_status
1128 xhci_open(usbd_pipe_handle pipe)
1129 {
1130 	usbd_device_handle const dev = pipe->device;
1131 	struct xhci_softc * const sc = dev->bus->hci_private;
1132 	usb_endpoint_descriptor_t * const ed = pipe->endpoint->edesc;
1133 	const uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
1134 
1135 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
1136 	DPRINTFN(1, "addr %d depth %d port %d speed %d",
1137 	    dev->address, dev->depth, dev->powersrc->portno, dev->speed);
1138 
1139 	if (sc->sc_dying)
1140 		return USBD_IOERROR;
1141 
1142 	/* Root Hub */
1143 	if (dev->depth == 0 && dev->powersrc->portno == 0 &&
1144 	    dev->speed != USB_SPEED_SUPER) {
1145 		switch (ed->bEndpointAddress) {
1146 		case USB_CONTROL_ENDPOINT:
1147 			pipe->methods = &xhci_root_ctrl_methods;
1148 			break;
1149 		case UE_DIR_IN | XHCI_INTR_ENDPT:
1150 			pipe->methods = &xhci_root_intr_methods;
1151 			break;
1152 		default:
1153 			pipe->methods = NULL;
1154 			DPRINTFN(0, "bad bEndpointAddress 0x%02x",
1155 			    ed->bEndpointAddress, 0, 0, 0);
1156 			return USBD_INVAL;
1157 		}
1158 		return USBD_NORMAL_COMPLETION;
1159 	}
1160 
1161 	switch (xfertype) {
1162 	case UE_CONTROL:
1163 		pipe->methods = &xhci_device_ctrl_methods;
1164 		break;
1165 	case UE_ISOCHRONOUS:
1166 		pipe->methods = &xhci_device_isoc_methods;
1167 		return USBD_INVAL;
1168 		break;
1169 	case UE_BULK:
1170 		pipe->methods = &xhci_device_bulk_methods;
1171 		break;
1172 	case UE_INTERRUPT:
1173 		pipe->methods = &xhci_device_intr_methods;
1174 		break;
1175 	default:
1176 		return USBD_IOERROR;
1177 		break;
1178 	}
1179 
1180 	if (ed->bEndpointAddress != USB_CONTROL_ENDPOINT)
1181 		xhci_configure_endpoint(pipe);
1182 
1183 	return USBD_NORMAL_COMPLETION;
1184 }
1185 
1186 static void
1187 xhci_rhpsc(struct xhci_softc * const sc, u_int port)
1188 {
1189 	usbd_xfer_handle const xfer = sc->sc_intrxfer;
1190 	uint8_t *p;
1191 
1192 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
1193 	DPRINTFN(4, "port %u status change", port, 0, 0, 0);
1194 
1195 	if (xfer == NULL)
1196 		return;
1197 
1198 	if (!(port >= sc->sc_hs_port_start &&
1199 	    port < sc->sc_hs_port_start + sc->sc_hs_port_count))
1200 		return;
1201 
1202 	port -= sc->sc_hs_port_start;
1203 	port += 1;
1204 	DPRINTFN(4, "hs port %u status change", port, 0, 0, 0);
1205 
1206 	p = KERNADDR(&xfer->dmabuf, 0);
1207 	memset(p, 0, xfer->length);
1208 	p[port/NBBY] |= 1 << (port%NBBY);
1209 	xfer->actlen = xfer->length;
1210 	xfer->status = USBD_NORMAL_COMPLETION;
1211 	usb_transfer_complete(xfer);
1212 }
1213 
1214 static void
1215 xhci_handle_event(struct xhci_softc * const sc,
1216     const struct xhci_trb * const trb)
1217 {
1218 	uint64_t trb_0;
1219 	uint32_t trb_2, trb_3;
1220 
1221 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
1222 
1223 	trb_0 = le64toh(trb->trb_0);
1224 	trb_2 = le32toh(trb->trb_2);
1225 	trb_3 = le32toh(trb->trb_3);
1226 
1227 	DPRINTFN(14, "event: %p 0x%016"PRIx64" 0x%08"PRIx32" 0x%08"PRIx32,
1228 	    trb, trb_0, trb_2, trb_3);
1229 
1230 	switch (XHCI_TRB_3_TYPE_GET(trb_3)){
1231 	case XHCI_TRB_EVENT_TRANSFER: {
1232 		u_int slot, dci;
1233 		struct xhci_slot *xs;
1234 		struct xhci_ring *xr;
1235 		struct xhci_xfer *xx;
1236 		usbd_xfer_handle xfer;
1237 		usbd_status err;
1238 
1239 		slot = XHCI_TRB_3_SLOT_GET(trb_3);
1240 		dci = XHCI_TRB_3_EP_GET(trb_3);
1241 
1242 		xs = &sc->sc_slots[slot];
1243 		xr = &xs->xs_ep[dci].xe_tr;
1244 
1245 		if ((trb_3 & XHCI_TRB_3_ED_BIT) == 0) {
1246 			xx = xr->xr_cookies[(trb_0 - xhci_ring_trbp(xr, 0))/
1247 			    sizeof(struct xhci_trb)];
1248 		} else {
1249 			xx = (void *)(uintptr_t)(trb_0 & ~0x3);
1250 		}
1251 		xfer = &xx->xx_xfer;
1252 		DPRINTFN(14, "xfer %p", xfer, 0, 0, 0);
1253 
1254 		if ((trb_3 & XHCI_TRB_3_ED_BIT) != 0) {
1255 			DPRINTFN(14, "transfer event data: "
1256 			    "0x%016"PRIx64" 0x%08"PRIx32" %02x",
1257 			    trb_0, XHCI_TRB_2_REM_GET(trb_2),
1258 			    XHCI_TRB_2_ERROR_GET(trb_2), 0);
1259 			if ((trb_0 & 0x3) == 0x3) {
1260 				xfer->actlen = XHCI_TRB_2_REM_GET(trb_2);
1261 			}
1262 		}
1263 
1264 		if (XHCI_TRB_2_ERROR_GET(trb_2) ==
1265 		    XHCI_TRB_ERROR_SUCCESS) {
1266 			xfer->actlen = xfer->length - XHCI_TRB_2_REM_GET(trb_2);
1267 			err = USBD_NORMAL_COMPLETION;
1268 		} else if (XHCI_TRB_2_ERROR_GET(trb_2) ==
1269 		    XHCI_TRB_ERROR_SHORT_PKT) {
1270 			xfer->actlen = xfer->length - XHCI_TRB_2_REM_GET(trb_2);
1271 			err = USBD_NORMAL_COMPLETION;
1272 		} else if (XHCI_TRB_2_ERROR_GET(trb_2) ==
1273 		    XHCI_TRB_ERROR_STALL) {
1274 			err = USBD_STALLED;
1275 			xr->is_halted = true;
1276 			DPRINTFN(1, "ev: xfer done: err %u slot %u dci %u",
1277 			    XHCI_TRB_2_ERROR_GET(trb_2), slot, dci, 0);
1278 		} else {
1279 			err = USBD_IOERROR;
1280 		}
1281 		xfer->status = err;
1282 
1283 		//mutex_enter(&sc->sc_lock); /* XXX ??? */
1284 		if ((trb_3 & XHCI_TRB_3_ED_BIT) != 0) {
1285 			if ((trb_0 & 0x3) == 0x0) {
1286 				usb_transfer_complete(xfer);
1287 			}
1288 		} else {
1289 			usb_transfer_complete(xfer);
1290 		}
1291 		//mutex_exit(&sc->sc_lock); /* XXX ??? */
1292 
1293 		}
1294 		break;
1295 	case XHCI_TRB_EVENT_CMD_COMPLETE:
1296 		if (trb_0 == sc->sc_command_addr) {
1297 			sc->sc_result_trb.trb_0 = trb_0;
1298 			sc->sc_result_trb.trb_2 = trb_2;
1299 			sc->sc_result_trb.trb_3 = trb_3;
1300 			if (XHCI_TRB_2_ERROR_GET(trb_2) !=
1301 			    XHCI_TRB_ERROR_SUCCESS) {
1302 				DPRINTFN(1, "command completion "
1303 				    "failure: 0x%016"PRIx64" 0x%08"PRIx32" "
1304 				    "0x%08"PRIx32, trb_0, trb_2, trb_3, 0);
1305 			}
1306 			cv_signal(&sc->sc_command_cv);
1307 		} else {
1308 			DPRINTFN(1, "event: %p 0x%016"PRIx64" "
1309 			    "0x%08"PRIx32" 0x%08"PRIx32, trb, trb_0,
1310 			    trb_2, trb_3);
1311 		}
1312 		break;
1313 	case XHCI_TRB_EVENT_PORT_STS_CHANGE:
1314 		xhci_rhpsc(sc, (uint32_t)((trb_0 >> 24) & 0xff));
1315 		break;
1316 	default:
1317 		break;
1318 	}
1319 }
1320 
1321 static void
1322 xhci_softintr(void *v)
1323 {
1324 	struct usbd_bus * const bus = v;
1325 	struct xhci_softc * const sc = bus->hci_private;
1326 	struct xhci_ring * const er = &sc->sc_er;
1327 	struct xhci_trb *trb;
1328 	int i, j, k;
1329 
1330 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
1331 
1332 	i = er->xr_ep;
1333 	j = er->xr_cs;
1334 
1335 	DPRINTFN(16, "xr_ep %d xr_cs %d", i, j, 0, 0);
1336 
1337 	while (1) {
1338 		usb_syncmem(&er->xr_dma, XHCI_TRB_SIZE * i, XHCI_TRB_SIZE,
1339 		    BUS_DMASYNC_POSTREAD);
1340 		trb = &er->xr_trb[i];
1341 		k = (le32toh(trb->trb_3) & XHCI_TRB_3_CYCLE_BIT) ? 1 : 0;
1342 
1343 		if (j != k)
1344 			break;
1345 
1346 		xhci_handle_event(sc, trb);
1347 
1348 		i++;
1349 		if (i == XHCI_EVENT_RING_TRBS) {
1350 			i = 0;
1351 			j ^= 1;
1352 		}
1353 	}
1354 
1355 	er->xr_ep = i;
1356 	er->xr_cs = j;
1357 
1358 	xhci_rt_write_8(sc, XHCI_ERDP(0), xhci_ring_trbp(er, er->xr_ep) |
1359 	    XHCI_ERDP_LO_BUSY);
1360 
1361 	DPRINTFN(16, "ends", 0, 0, 0, 0);
1362 
1363 	return;
1364 }
1365 
1366 static void
1367 xhci_poll(struct usbd_bus *bus)
1368 {
1369 	struct xhci_softc * const sc = bus->hci_private;
1370 
1371 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
1372 
1373 	mutex_spin_enter(&sc->sc_intr_lock);
1374 	xhci_intr1(sc);
1375 	mutex_spin_exit(&sc->sc_intr_lock);
1376 
1377 	return;
1378 }
1379 
1380 static usbd_status
1381 xhci_allocm(struct usbd_bus *bus, usb_dma_t *dma, uint32_t size)
1382 {
1383 	struct xhci_softc * const sc = bus->hci_private;
1384 	usbd_status err;
1385 
1386 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
1387 
1388 	err = usb_allocmem(&sc->sc_bus, size, 0, dma);
1389 #if 0
1390 	if (err == USBD_NOMEM)
1391 		err = usb_reserve_allocm(&sc->sc_dma_reserve, dma, size);
1392 #endif
1393 #ifdef XHCI_DEBUG
1394 	if (err)
1395 		DPRINTFN(1, "usb_allocmem(%u)=%d", err, size, 0, 0);
1396 #endif
1397 
1398 	return err;
1399 }
1400 
1401 static void
1402 xhci_freem(struct usbd_bus *bus, usb_dma_t *dma)
1403 {
1404 	struct xhci_softc * const sc = bus->hci_private;
1405 
1406 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
1407 
1408 #if 0
1409 	if (dma->block->flags & USB_DMA_RESERVE) {
1410 		usb_reserve_freem(&sc->sc_dma_reserve, dma);
1411 		return;
1412 	}
1413 #endif
1414 	usb_freemem(&sc->sc_bus, dma);
1415 }
1416 
1417 static usbd_xfer_handle
1418 xhci_allocx(struct usbd_bus *bus)
1419 {
1420 	struct xhci_softc * const sc = bus->hci_private;
1421 	usbd_xfer_handle xfer;
1422 
1423 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
1424 
1425 	xfer = pool_cache_get(sc->sc_xferpool, PR_NOWAIT);
1426 	if (xfer != NULL) {
1427 		memset(xfer, 0, sizeof(struct xhci_xfer));
1428 #ifdef DIAGNOSTIC
1429 		xfer->busy_free = XFER_BUSY;
1430 #endif
1431 	}
1432 
1433 	return xfer;
1434 }
1435 
1436 static void
1437 xhci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
1438 {
1439 	struct xhci_softc * const sc = bus->hci_private;
1440 
1441 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
1442 
1443 #ifdef DIAGNOSTIC
1444 	if (xfer->busy_free != XFER_BUSY) {
1445 		DPRINTFN(0, "xfer=%p not busy, 0x%08x",
1446 		    xfer, xfer->busy_free, 0, 0);
1447 	}
1448 	xfer->busy_free = XFER_FREE;
1449 #endif
1450 	pool_cache_put(sc->sc_xferpool, xfer);
1451 }
1452 
1453 static void
1454 xhci_get_lock(struct usbd_bus *bus, kmutex_t **lock)
1455 {
1456 	struct xhci_softc * const sc = bus->hci_private;
1457 
1458 	*lock = &sc->sc_lock;
1459 }
1460 
1461 extern u_int32_t usb_cookie_no;
1462 
1463 static usbd_status
1464 xhci_new_device(device_t parent, usbd_bus_handle bus, int depth,
1465     int speed, int port, struct usbd_port *up)
1466 {
1467 	struct xhci_softc * const sc = bus->hci_private;
1468 	usbd_device_handle dev;
1469 	usbd_status err;
1470 	usb_device_descriptor_t *dd;
1471 	struct usbd_device *hub;
1472 	struct usbd_device *adev;
1473 	int rhport = 0;
1474 	struct xhci_slot *xs;
1475 	uint32_t *cp;
1476 	uint8_t slot;
1477 	uint8_t addr;
1478 
1479 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
1480 	DPRINTFN(4, "port=%d depth=%d speed=%d upport %d",
1481 		 port, depth, speed, up->portno);
1482 
1483 	dev = malloc(sizeof *dev, M_USB, M_NOWAIT|M_ZERO);
1484 	if (dev == NULL)
1485 		return USBD_NOMEM;
1486 
1487 	dev->bus = bus;
1488 
1489 	/* Set up default endpoint handle. */
1490 	dev->def_ep.edesc = &dev->def_ep_desc;
1491 
1492 	/* Set up default endpoint descriptor. */
1493 	dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
1494 	dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
1495 	dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1496 	dev->def_ep_desc.bmAttributes = UE_CONTROL;
1497 	/* XXX */
1498 	if (speed == USB_SPEED_LOW)
1499 		USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
1500 	else
1501 		USETW(dev->def_ep_desc.wMaxPacketSize, 64);
1502 	dev->def_ep_desc.bInterval = 0;
1503 
1504 	/* doesn't matter, just don't let it uninitialized */
1505 	dev->def_ep.datatoggle = 0;
1506 
1507 	DPRINTFN(4, "up %p portno %d", up, up->portno, 0, 0);
1508 
1509 	dev->quirks = &usbd_no_quirk;
1510 	dev->address = 0;
1511 	dev->ddesc.bMaxPacketSize = 0;
1512 	dev->depth = depth;
1513 	dev->powersrc = up;
1514 	dev->myhub = up->parent;
1515 
1516 	up->device = dev;
1517 
1518 	/* Locate root hub port */
1519 	for (adev = dev, hub = dev;
1520 	    hub != NULL;
1521 	    adev = hub, hub = hub->myhub) {
1522 		DPRINTFN(4, "hub %p", hub, 0, 0, 0);
1523 	}
1524 	DPRINTFN(4, "hub %p", hub, 0, 0, 0);
1525 
1526 	if (hub != NULL) {
1527 		for (int p = 0; p < hub->hub->hubdesc.bNbrPorts; p++) {
1528 			if (hub->hub->ports[p].device == adev) {
1529 				rhport = p;
1530 			}
1531 		}
1532 	} else {
1533 		rhport = port;
1534 	}
1535 	if (speed == USB_SPEED_SUPER) {
1536 		rhport += sc->sc_ss_port_start - 1;
1537 	} else {
1538 		rhport += sc->sc_hs_port_start - 1;
1539 	}
1540 	DPRINTFN(4, "rhport %d", rhport, 0, 0, 0);
1541 
1542 	dev->speed = speed;
1543 	dev->langid = USBD_NOLANG;
1544 	dev->cookie.cookie = ++usb_cookie_no;
1545 
1546 	/* Establish the default pipe. */
1547 	err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1548 	    &dev->default_pipe);
1549 	if (err) {
1550 		usbd_remove_device(dev, up);
1551 		return (err);
1552 	}
1553 
1554 	dd = &dev->ddesc;
1555 
1556 	if ((depth == 0) && (port == 0)) {
1557 		KASSERT(bus->devices[dev->address] == NULL);
1558 		bus->devices[dev->address] = dev;
1559 		err = usbd_get_initial_ddesc(dev, dd);
1560 		if (err)
1561 			return err;
1562 		err = usbd_reload_device_desc(dev);
1563 		if (err)
1564 			return err;
1565 	} else {
1566 		err = xhci_enable_slot(sc, &slot);
1567 		if (err)
1568 			return err;
1569 		err = xhci_init_slot(sc, slot, depth, speed, port, rhport);
1570 		if (err)
1571 			return err;
1572 		xs = &sc->sc_slots[slot];
1573 		dev->hci_private = xs;
1574 		cp = xhci_slot_get_dcv(sc, xs, XHCI_DCI_SLOT);
1575 		//hexdump("slot context", cp, sc->sc_ctxsz);
1576 		addr = XHCI_SCTX_3_DEV_ADDR_GET(cp[3]);
1577 		DPRINTFN(4, "device address %u", addr, 0, 0, 0);
1578 		/* XXX ensure we know when the hardware does something
1579 		   we can't yet cope with */
1580 		KASSERT(addr >= 1 && addr <= 127);
1581 		dev->address = addr;
1582 		/* XXX dev->address not necessarily unique on bus */
1583 		KASSERT(bus->devices[dev->address] == NULL);
1584 		bus->devices[dev->address] = dev;
1585 
1586 		err = usbd_get_initial_ddesc(dev, dd);
1587 		if (err)
1588 			return err;
1589 		/* 4.8.2.1 */
1590 		if (speed == USB_SPEED_SUPER)
1591 			USETW(dev->def_ep_desc.wMaxPacketSize,
1592 			    (1 << dd->bMaxPacketSize));
1593 		else
1594 	 		USETW(dev->def_ep_desc.wMaxPacketSize,
1595 			    dd->bMaxPacketSize);
1596 		DPRINTFN(4, "bMaxPacketSize %u", dd->bMaxPacketSize, 0, 0, 0);
1597 		xhci_update_ep0_mps(sc, xs,
1598 		    UGETW(dev->def_ep_desc.wMaxPacketSize));
1599 		err = usbd_reload_device_desc(dev);
1600 		if (err)
1601 			return err;
1602 
1603 		usbd_kill_pipe(dev->default_pipe);
1604 		err = usbd_setup_pipe(dev, 0, &dev->def_ep,
1605 		    USBD_DEFAULT_INTERVAL, &dev->default_pipe);
1606 	}
1607 
1608 	DPRINTFN(1, "adding unit addr=%d, rev=%02x,",
1609 		dev->address, UGETW(dd->bcdUSB), 0, 0);
1610 	DPRINTFN(1, " class=%d, subclass=%d, protocol=%d,",
1611 		dd->bDeviceClass, dd->bDeviceSubClass,
1612 		dd->bDeviceProtocol, 0);
1613 	DPRINTFN(1, " mps=%d, len=%d, noconf=%d, speed=%d",
1614 		dd->bMaxPacketSize, dd->bLength, dd->bNumConfigurations,
1615 		dev->speed);
1616 
1617 	usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
1618 
1619 	if ((depth == 0) && (port == 0)) {
1620 		usbd_attach_roothub(parent, dev);
1621 		DPRINTFN(1, "root_hub %p", bus->root_hub, 0, 0, 0);
1622 		return USBD_NORMAL_COMPLETION;
1623 	}
1624 
1625 
1626 	err = usbd_probe_and_attach(parent, dev, port, dev->address);
1627 	if (err) {
1628 		usbd_remove_device(dev, up);
1629 		return (err);
1630 	}
1631 
1632 	return USBD_NORMAL_COMPLETION;
1633 }
1634 
1635 static usbd_status
1636 xhci_ring_init(struct xhci_softc * const sc, struct xhci_ring * const xr,
1637     size_t ntrb, size_t align)
1638 {
1639 	usbd_status err;
1640 	size_t size = ntrb * XHCI_TRB_SIZE;
1641 
1642 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
1643 
1644 	err = usb_allocmem(&sc->sc_bus, size, align, &xr->xr_dma);
1645 	if (err)
1646 		return err;
1647 	mutex_init(&xr->xr_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
1648 	xr->xr_cookies = kmem_zalloc(sizeof(*xr->xr_cookies) * ntrb, KM_SLEEP);
1649 	xr->xr_trb = xhci_ring_trbv(xr, 0);
1650 	xr->xr_ntrb = ntrb;
1651 	xr->xr_ep = 0;
1652 	xr->xr_cs = 1;
1653 	memset(xr->xr_trb, 0, size);
1654 	usb_syncmem(&xr->xr_dma, 0, size, BUS_DMASYNC_PREWRITE);
1655 	xr->is_halted = false;
1656 
1657 	return USBD_NORMAL_COMPLETION;
1658 }
1659 
1660 static void
1661 xhci_ring_free(struct xhci_softc * const sc, struct xhci_ring * const xr)
1662 {
1663 	usb_freemem(&sc->sc_bus, &xr->xr_dma);
1664 	mutex_destroy(&xr->xr_lock);
1665 	kmem_free(xr->xr_cookies, sizeof(*xr->xr_cookies) * xr->xr_ntrb);
1666 }
1667 
1668 static void
1669 xhci_ring_put(struct xhci_softc * const sc, struct xhci_ring * const xr,
1670     void *cookie, struct xhci_trb * const trbs, size_t ntrbs)
1671 {
1672 	size_t i;
1673 	u_int ri;
1674 	u_int cs;
1675 	uint64_t parameter;
1676 	uint32_t status;
1677 	uint32_t control;
1678 
1679 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
1680 
1681 	for (i = 0; i < ntrbs; i++) {
1682 		DPRINTFN(12, "xr %p trbs %p num %zu", xr, trbs, i, 0);
1683 		DPRINTFN(12, " %016"PRIx64" %08"PRIx32" %08"PRIx32,
1684 		    trbs[i].trb_0, trbs[i].trb_2, trbs[i].trb_3, 0);
1685 		KASSERT(XHCI_TRB_3_TYPE_GET(trbs[i].trb_3) !=
1686 		    XHCI_TRB_TYPE_LINK);
1687 	}
1688 
1689 	DPRINTFN(12, "%p xr_ep 0x%x xr_cs %u", xr, xr->xr_ep, xr->xr_cs, 0);
1690 
1691 	ri = xr->xr_ep;
1692 	cs = xr->xr_cs;
1693 
1694 	/*
1695 	 * Although the xhci hardware can do scatter/gather dma from
1696 	 * arbitrary sized buffers, there is a non-obvious restriction
1697 	 * that a LINK trb is only allowed at the end of a burst of
1698 	 * transfers - which might be 16kB.
1699 	 * Arbitrary aligned LINK trb definitely fail on Ivy bridge.
1700 	 * The simple solution is not to allow a LINK trb in the middle
1701 	 * of anything - as here.
1702 	 * XXX: (dsl) There are xhci controllers out there (eg some made by
1703 	 * ASMedia) that seem to lock up if they process a LINK trb but
1704 	 * cannot process the linked-to trb yet.
1705 	 * The code should write the 'cycle' bit on the link trb AFTER
1706 	 * adding the other trb.
1707 	 */
1708 	if (ri + ntrbs >= (xr->xr_ntrb - 1)) {
1709 		parameter = xhci_ring_trbp(xr, 0);
1710 		status = 0;
1711 		control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) |
1712 		    XHCI_TRB_3_TC_BIT | (cs ? XHCI_TRB_3_CYCLE_BIT : 0);
1713 		xhci_trb_put(&xr->xr_trb[ri], htole64(parameter),
1714 		    htole32(status), htole32(control));
1715 		usb_syncmem(&xr->xr_dma, XHCI_TRB_SIZE * ri, XHCI_TRB_SIZE * 1,
1716 		    BUS_DMASYNC_PREWRITE);
1717 		xr->xr_cookies[ri] = NULL;
1718 		xr->xr_ep = 0;
1719 		xr->xr_cs ^= 1;
1720 		ri = xr->xr_ep;
1721 		cs = xr->xr_cs;
1722 	}
1723 
1724 	ri++;
1725 
1726 	/* Write any subsequent TRB first */
1727 	for (i = 1; i < ntrbs; i++) {
1728 		parameter = trbs[i].trb_0;
1729 		status = trbs[i].trb_2;
1730 		control = trbs[i].trb_3;
1731 
1732 		if (cs) {
1733 			control |= XHCI_TRB_3_CYCLE_BIT;
1734 		} else {
1735 			control &= ~XHCI_TRB_3_CYCLE_BIT;
1736 		}
1737 
1738 		xhci_trb_put(&xr->xr_trb[ri], htole64(parameter),
1739 		    htole32(status), htole32(control));
1740 		usb_syncmem(&xr->xr_dma, XHCI_TRB_SIZE * ri, XHCI_TRB_SIZE * 1,
1741 		    BUS_DMASYNC_PREWRITE);
1742 		xr->xr_cookies[ri] = cookie;
1743 		ri++;
1744 	}
1745 
1746 	/* Write the first TRB last */
1747 	i = 0;
1748 	{
1749 		parameter = trbs[i].trb_0;
1750 		status = trbs[i].trb_2;
1751 		control = trbs[i].trb_3;
1752 
1753 		if (xr->xr_cs) {
1754 			control |= XHCI_TRB_3_CYCLE_BIT;
1755 		} else {
1756 			control &= ~XHCI_TRB_3_CYCLE_BIT;
1757 		}
1758 
1759 		xhci_trb_put(&xr->xr_trb[xr->xr_ep], htole64(parameter),
1760 		    htole32(status), htole32(control));
1761 		usb_syncmem(&xr->xr_dma, XHCI_TRB_SIZE * ri, XHCI_TRB_SIZE * 1,
1762 		    BUS_DMASYNC_PREWRITE);
1763 		xr->xr_cookies[xr->xr_ep] = cookie;
1764 	}
1765 
1766 	xr->xr_ep = ri;
1767 	xr->xr_cs = cs;
1768 
1769 	DPRINTFN(12, "%p xr_ep 0x%x xr_cs %u", xr, xr->xr_ep, xr->xr_cs, 0);
1770 }
1771 
1772 static usbd_status
1773 xhci_do_command(struct xhci_softc * const sc, struct xhci_trb * const trb,
1774     int timeout)
1775 {
1776 	struct xhci_ring * const cr = &sc->sc_cr;
1777 	usbd_status err;
1778 
1779 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
1780 	DPRINTFN(12, "input: 0x%016"PRIx64" 0x%08"PRIx32" 0x%08"PRIx32,
1781 	    trb->trb_0, trb->trb_2, trb->trb_3, 0);
1782 
1783 	mutex_enter(&sc->sc_lock);
1784 
1785 	KASSERT(sc->sc_command_addr == 0);
1786 	sc->sc_command_addr = xhci_ring_trbp(cr, cr->xr_ep);
1787 
1788 	mutex_enter(&cr->xr_lock);
1789 	xhci_ring_put(sc, cr, NULL, trb, 1);
1790 	mutex_exit(&cr->xr_lock);
1791 
1792 	xhci_db_write_4(sc, XHCI_DOORBELL(0), 0);
1793 
1794 	if (cv_timedwait(&sc->sc_command_cv, &sc->sc_lock,
1795 	    MAX(1, mstohz(timeout))) == EWOULDBLOCK) {
1796 		err = USBD_TIMEOUT;
1797 		goto timedout;
1798 	}
1799 
1800 	trb->trb_0 = sc->sc_result_trb.trb_0;
1801 	trb->trb_2 = sc->sc_result_trb.trb_2;
1802 	trb->trb_3 = sc->sc_result_trb.trb_3;
1803 
1804 	DPRINTFN(12, "output: 0x%016"PRIx64" 0x%08"PRIx32" 0x%08"PRIx32"",
1805 	    trb->trb_0, trb->trb_2, trb->trb_3, 0);
1806 
1807 	switch (XHCI_TRB_2_ERROR_GET(trb->trb_2)) {
1808 	case XHCI_TRB_ERROR_SUCCESS:
1809 		err = USBD_NORMAL_COMPLETION;
1810 		break;
1811 	default:
1812 	case 192 ... 223:
1813 		err = USBD_IOERROR;
1814 		break;
1815 	case 224 ... 255:
1816 		err = USBD_NORMAL_COMPLETION;
1817 		break;
1818 	}
1819 
1820 timedout:
1821 	sc->sc_command_addr = 0;
1822 	mutex_exit(&sc->sc_lock);
1823 	return err;
1824 }
1825 
1826 static usbd_status
1827 xhci_enable_slot(struct xhci_softc * const sc, uint8_t * const slotp)
1828 {
1829 	struct xhci_trb trb;
1830 	usbd_status err;
1831 
1832 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
1833 
1834 	trb.trb_0 = 0;
1835 	trb.trb_2 = 0;
1836 	trb.trb_3 = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ENABLE_SLOT);
1837 
1838 	err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1839 	if (err != USBD_NORMAL_COMPLETION) {
1840 		return err;
1841 	}
1842 
1843 	*slotp = XHCI_TRB_3_SLOT_GET(trb.trb_3);
1844 
1845 	return err;
1846 }
1847 
1848 static usbd_status
1849 xhci_address_device(struct xhci_softc * const sc,
1850     uint64_t icp, uint8_t slot_id, bool bsr)
1851 {
1852 	struct xhci_trb trb;
1853 	usbd_status err;
1854 
1855 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
1856 
1857 	trb.trb_0 = icp;
1858 	trb.trb_2 = 0;
1859 	trb.trb_3 = XHCI_TRB_3_SLOT_SET(slot_id) |
1860 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ADDRESS_DEVICE) |
1861 	    (bsr ? XHCI_TRB_3_BSR_BIT : 0);
1862 
1863 	err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1864 	return err;
1865 }
1866 
1867 static usbd_status
1868 xhci_update_ep0_mps(struct xhci_softc * const sc,
1869     struct xhci_slot * const xs, u_int mps)
1870 {
1871 	struct xhci_trb trb;
1872 	usbd_status err;
1873 	uint32_t * cp;
1874 
1875 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
1876 	DPRINTFN(4, "slot %u mps %u", xs->xs_idx, mps, 0, 0);
1877 
1878 	cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL);
1879 	cp[0] = htole32(0);
1880 	cp[1] = htole32(XHCI_INCTX_1_ADD_MASK(XHCI_DCI_EP_CONTROL));
1881 
1882 	cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_EP_CONTROL));
1883 	cp[1] = htole32(XHCI_EPCTX_1_MAXP_SIZE_SET(mps));
1884 
1885 	/* sync input contexts before they are read from memory */
1886 	usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE);
1887 	hexdump("input context", xhci_slot_get_icv(sc, xs, 0),
1888 	    sc->sc_ctxsz * 4);
1889 
1890 	trb.trb_0 = xhci_slot_get_icp(sc, xs, 0);
1891 	trb.trb_2 = 0;
1892 	trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
1893 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVALUATE_CTX);
1894 
1895 	err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1896 	KASSERT(err == USBD_NORMAL_COMPLETION); /* XXX */
1897 	return err;
1898 }
1899 
1900 static void
1901 xhci_set_dcba(struct xhci_softc * const sc, uint64_t dcba, int si)
1902 {
1903 	uint64_t * const dcbaa = KERNADDR(&sc->sc_dcbaa_dma, 0);
1904 
1905 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
1906 	DPRINTFN(4, "dcbaa %p dc %016"PRIx64" slot %d",
1907 	    &dcbaa[si], dcba, si, 0);
1908 
1909 	dcbaa[si] = htole64(dcba);
1910 	usb_syncmem(&sc->sc_dcbaa_dma, si * sizeof(uint64_t), sizeof(uint64_t),
1911 	    BUS_DMASYNC_PREWRITE);
1912 }
1913 
1914 static usbd_status
1915 xhci_init_slot(struct xhci_softc * const sc, uint32_t slot, int depth,
1916     int speed, int port, int rhport)
1917 {
1918 	struct xhci_slot *xs;
1919 	usbd_status err;
1920 	u_int dci;
1921 	uint32_t *cp;
1922 	uint32_t mps;
1923 	uint32_t xspeed;
1924 
1925 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
1926 	DPRINTFN(4, "slot %u depth %d speed %d", slot, depth, speed, 0);
1927 	DPRINTFN(4, " port %d rhport %d", port, rhport, 0, 0);
1928 
1929 	switch (speed) {
1930 	case USB_SPEED_LOW:
1931 		xspeed = 2;
1932 		mps = USB_MAX_IPACKET;
1933 		break;
1934 	case USB_SPEED_FULL:
1935 		xspeed = 1;
1936 		mps = 64;
1937 		break;
1938 	case USB_SPEED_HIGH:
1939 		xspeed = 3;
1940 		mps = USB_2_MAX_CTRL_PACKET;
1941 		break;
1942 	case USB_SPEED_SUPER:
1943 		xspeed = 4;
1944 		mps = USB_3_MAX_CTRL_PACKET;
1945 		break;
1946 	default:
1947 		DPRINTFN(0, "impossible speed: %x", speed, 0, 0, 0);
1948 		return USBD_INVAL;
1949 	}
1950 
1951 	xs = &sc->sc_slots[slot];
1952 	xs->xs_idx = slot;
1953 
1954 	/* allocate contexts */
1955 	err = usb_allocmem(&sc->sc_bus, sc->sc_pgsz, sc->sc_pgsz,
1956 	    &xs->xs_dc_dma);
1957 	if (err)
1958 		return err;
1959 	memset(KERNADDR(&xs->xs_dc_dma, 0), 0, sc->sc_pgsz);
1960 
1961 	err = usb_allocmem(&sc->sc_bus, sc->sc_pgsz, sc->sc_pgsz,
1962 	    &xs->xs_ic_dma);
1963 	if (err)
1964 		return err;
1965 	memset(KERNADDR(&xs->xs_ic_dma, 0), 0, sc->sc_pgsz);
1966 
1967 	for (dci = 0; dci < 32; dci++) {
1968 		//CTASSERT(sizeof(xs->xs_ep[dci]) == sizeof(struct xhci_endpoint));
1969 		memset(&xs->xs_ep[dci], 0, sizeof(xs->xs_ep[dci]));
1970 		if (dci == XHCI_DCI_SLOT)
1971 			continue;
1972 		err = xhci_ring_init(sc, &xs->xs_ep[dci].xe_tr,
1973 		    XHCI_TRANSFER_RING_TRBS, XHCI_TRB_ALIGN);
1974 		if (err) {
1975 			DPRINTFN(0, "ring init failure", 0, 0, 0, 0);
1976 			return err;
1977 		}
1978 	}
1979 
1980 	/* set up initial input control context */
1981 	cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL);
1982 	cp[0] = htole32(0);
1983 	cp[1] = htole32(XHCI_INCTX_1_ADD_MASK(XHCI_DCI_EP_CONTROL)|
1984 	    XHCI_INCTX_1_ADD_MASK(XHCI_DCI_SLOT));
1985 
1986 	/* set up input slot context */
1987 	cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_SLOT));
1988 	cp[0] = htole32(
1989 		XHCI_SCTX_0_CTX_NUM_SET(1) |
1990 		XHCI_SCTX_0_SPEED_SET(xspeed)
1991 		);
1992 	cp[1] = htole32(
1993 		XHCI_SCTX_1_RH_PORT_SET(rhport)
1994 		);
1995 	cp[2] = htole32(
1996 		XHCI_SCTX_2_IRQ_TARGET_SET(0)
1997 		);
1998 	cp[3] = htole32(0);
1999 
2000 	/* set up input EP0 context */
2001 	cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_EP_CONTROL));
2002 	cp[0] = htole32(0);
2003 	cp[1] = htole32(
2004 		XHCI_EPCTX_1_MAXP_SIZE_SET(mps) |
2005 		XHCI_EPCTX_1_EPTYPE_SET(4) |
2006 		XHCI_EPCTX_1_CERR_SET(3)
2007 		);
2008 	/* can't use xhci_ep_get_dci() yet? */
2009 	*(uint64_t *)(&cp[2]) = htole64(
2010 	    xhci_ring_trbp(&xs->xs_ep[XHCI_DCI_EP_CONTROL].xe_tr, 0) |
2011 	    XHCI_EPCTX_2_DCS_SET(1));
2012 	cp[4] = htole32(
2013 		XHCI_EPCTX_4_AVG_TRB_LEN_SET(8)
2014 		);
2015 
2016 	/* sync input contexts before they are read from memory */
2017 	usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE);
2018 	hexdump("input context", xhci_slot_get_icv(sc, xs, 0),
2019 	    sc->sc_ctxsz * 3);
2020 
2021 	xhci_set_dcba(sc, DMAADDR(&xs->xs_dc_dma, 0), slot);
2022 
2023 	err = xhci_address_device(sc, xhci_slot_get_icp(sc, xs, 0), slot,
2024 	    false);
2025 
2026 	usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
2027 	hexdump("output context", xhci_slot_get_dcv(sc, xs, 0),
2028 	    sc->sc_ctxsz * 2);
2029 
2030 	return err;
2031 }
2032 
2033 /* ----- */
2034 
2035 static void
2036 xhci_noop(usbd_pipe_handle pipe)
2037 {
2038 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
2039 }
2040 
2041 /* root hub descriptors */
2042 
2043 static const usb_device_descriptor_t xhci_devd = {
2044 	USB_DEVICE_DESCRIPTOR_SIZE,
2045 	UDESC_DEVICE,		/* type */
2046 	{0x00, 0x02},		/* USB version */
2047 	UDCLASS_HUB,		/* class */
2048 	UDSUBCLASS_HUB,		/* subclass */
2049 	UDPROTO_HSHUBSTT,	/* protocol */
2050 	64,			/* max packet */
2051 	{0},{0},{0x00,0x01},	/* device id */
2052 	1,2,0,			/* string indexes */
2053 	1			/* # of configurations */
2054 };
2055 
2056 static const usb_device_qualifier_t xhci_odevd = {
2057 	USB_DEVICE_DESCRIPTOR_SIZE,
2058 	UDESC_DEVICE_QUALIFIER,	/* type */
2059 	{0x00, 0x02},		/* USB version */
2060 	UDCLASS_HUB,		/* class */
2061 	UDSUBCLASS_HUB,		/* subclass */
2062 	UDPROTO_FSHUB,		/* protocol */
2063 	64,                     /* max packet */
2064 	1,                      /* # of configurations */
2065 	0
2066 };
2067 
2068 static const usb_config_descriptor_t xhci_confd = {
2069 	USB_CONFIG_DESCRIPTOR_SIZE,
2070 	UDESC_CONFIG,
2071 	{USB_CONFIG_DESCRIPTOR_SIZE +
2072 	 USB_INTERFACE_DESCRIPTOR_SIZE +
2073 	 USB_ENDPOINT_DESCRIPTOR_SIZE},
2074 	1,
2075 	1,
2076 	0,
2077 	UC_ATTR_MBO | UC_SELF_POWERED,
2078 	0                      /* max power */
2079 };
2080 
2081 static const usb_interface_descriptor_t xhci_ifcd = {
2082 	USB_INTERFACE_DESCRIPTOR_SIZE,
2083 	UDESC_INTERFACE,
2084 	0,
2085 	0,
2086 	1,
2087 	UICLASS_HUB,
2088 	UISUBCLASS_HUB,
2089 	UIPROTO_HSHUBSTT,
2090 	0
2091 };
2092 
2093 static const usb_endpoint_descriptor_t xhci_endpd = {
2094 	USB_ENDPOINT_DESCRIPTOR_SIZE,
2095 	UDESC_ENDPOINT,
2096 	UE_DIR_IN | XHCI_INTR_ENDPT,
2097 	UE_INTERRUPT,
2098 	{8, 0},                 /* max packet */
2099 	12
2100 };
2101 
2102 static const usb_hub_descriptor_t xhci_hubd = {
2103 	USB_HUB_DESCRIPTOR_SIZE,
2104 	UDESC_HUB,
2105 	0,
2106 	{0,0},
2107 	0,
2108 	0,
2109 	{""},
2110 	{""},
2111 };
2112 
2113 /* root hub control */
2114 
2115 static usbd_status
2116 xhci_root_ctrl_transfer(usbd_xfer_handle xfer)
2117 {
2118 	struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private;
2119 	usbd_status err;
2120 
2121 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
2122 
2123 	/* Insert last in queue. */
2124 	mutex_enter(&sc->sc_lock);
2125 	err = usb_insert_transfer(xfer);
2126 	mutex_exit(&sc->sc_lock);
2127 	if (err)
2128 		return err;
2129 
2130 	/* Pipe isn't running, start first */
2131 	return (xhci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2132 }
2133 
2134 static usbd_status
2135 xhci_root_ctrl_start(usbd_xfer_handle xfer)
2136 {
2137 	struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private;
2138 	usb_port_status_t ps;
2139 	usb_device_request_t *req;
2140 	void *buf = NULL;
2141 	usb_hub_descriptor_t hubd;
2142 	usbd_status err;
2143 	int len, value, index;
2144 	int l, totlen = 0;
2145 	int port, i;
2146 	uint32_t v;
2147 
2148 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
2149 
2150 	if (sc->sc_dying)
2151 		return USBD_IOERROR;
2152 
2153 	req = &xfer->request;
2154 
2155 	value = UGETW(req->wValue);
2156 	index = UGETW(req->wIndex);
2157 	len = UGETW(req->wLength);
2158 
2159 	if (len != 0)
2160 		buf = KERNADDR(&xfer->dmabuf, 0);
2161 
2162 	DPRINTFN(12, "rhreq: %04x %04x %04x %04x",
2163 	    req->bmRequestType | (req->bRequest << 8), value, index, len);
2164 
2165 #define C(x,y) ((x) | ((y) << 8))
2166 	switch(C(req->bRequest, req->bmRequestType)) {
2167 	case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2168 	case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2169 	case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2170 		/*
2171 		 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2172 		 * for the integrated root hub.
2173 		 */
2174 		break;
2175 	case C(UR_GET_CONFIG, UT_READ_DEVICE):
2176 		if (len > 0) {
2177 			*(uint8_t *)buf = sc->sc_conf;
2178 			totlen = 1;
2179 		}
2180 		break;
2181 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2182 		DPRINTFN(8, "getdesc: wValue=0x%04x", value, 0, 0, 0);
2183 		if (len == 0)
2184 			break;
2185 		switch(value >> 8) {
2186 		case UDESC_DEVICE:
2187 			if ((value & 0xff) != 0) {
2188 				err = USBD_IOERROR;
2189 				goto ret;
2190 			}
2191 			totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
2192 			memcpy(buf, &xhci_devd, min(l, sizeof(xhci_devd)));
2193 			break;
2194 		case UDESC_DEVICE_QUALIFIER:
2195 			if ((value & 0xff) != 0) {
2196 			}
2197 			totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
2198 			memcpy(buf, &xhci_odevd, min(l, sizeof(xhci_odevd)));
2199 			break;
2200 		case UDESC_OTHER_SPEED_CONFIGURATION:
2201 		case UDESC_CONFIG:
2202 			if ((value & 0xff) != 0) {
2203 				err = USBD_IOERROR;
2204 				goto ret;
2205 			}
2206 			totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
2207 			memcpy(buf, &xhci_confd, min(l, sizeof(xhci_confd)));
2208 			((usb_config_descriptor_t *)buf)->bDescriptorType =
2209 			    value >> 8;
2210 			buf = (char *)buf + l;
2211 			len -= l;
2212 			l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
2213 			totlen += l;
2214 			memcpy(buf, &xhci_ifcd, min(l, sizeof(xhci_ifcd)));
2215 			buf = (char *)buf + l;
2216 			len -= l;
2217 			l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
2218 			totlen += l;
2219 			memcpy(buf, &xhci_endpd, min(l, sizeof(xhci_endpd)));
2220 			break;
2221 		case UDESC_STRING:
2222 #define sd ((usb_string_descriptor_t *)buf)
2223 			switch (value & 0xff) {
2224 			case 0: /* Language table */
2225 				totlen = usb_makelangtbl(sd, len);
2226 				break;
2227 			case 1: /* Vendor */
2228 				totlen = usb_makestrdesc(sd, len, "NetBSD");
2229 				break;
2230 			case 2: /* Product */
2231 				totlen = usb_makestrdesc(sd, len,
2232 				    "xHCI Root Hub");
2233 				break;
2234 			}
2235 #undef sd
2236 			break;
2237 		default:
2238 			err = USBD_IOERROR;
2239 			goto ret;
2240 		}
2241 		break;
2242 	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2243 		if (len > 0) {
2244 			*(uint8_t *)buf = 0;
2245 			totlen = 1;
2246 		}
2247 		break;
2248 	case C(UR_GET_STATUS, UT_READ_DEVICE):
2249 		if (len > 1) {
2250 			USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
2251 			totlen = 2;
2252 		}
2253 		break;
2254 	case C(UR_GET_STATUS, UT_READ_INTERFACE):
2255 	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2256 		if (len > 1) {
2257 			USETW(((usb_status_t *)buf)->wStatus, 0);
2258 			totlen = 2;
2259 		}
2260 		break;
2261 	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2262 		if (value >= USB_MAX_DEVICES) {
2263 			err = USBD_IOERROR;
2264 			goto ret;
2265 		}
2266 		//sc->sc_addr = value;
2267 		break;
2268 	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2269 		if (value != 0 && value != 1) {
2270 			err = USBD_IOERROR;
2271 			goto ret;
2272 		}
2273 		sc->sc_conf = value;
2274 		break;
2275 	case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2276 		break;
2277 	case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2278 	case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2279 	case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2280 		err = USBD_IOERROR;
2281 		goto ret;
2282 	case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2283 		break;
2284 	case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2285 		break;
2286 	/* Hub requests */
2287 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2288 		break;
2289 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2290 		DPRINTFN(4, "UR_CLEAR_PORT_FEATURE port=%d feature=%d",
2291 			     index, value, 0, 0);
2292 		if (index < 1 || index > sc->sc_hs_port_count) {
2293 			err = USBD_IOERROR;
2294 			goto ret;
2295 		}
2296 		port = XHCI_PORTSC(sc->sc_hs_port_start - 1 + index);
2297 		v = xhci_op_read_4(sc, port);
2298 		DPRINTFN(4, "portsc=0x%08x", v, 0, 0, 0);
2299 		v &= ~XHCI_PS_CLEAR;
2300 		switch (value) {
2301 		case UHF_PORT_ENABLE:
2302 			xhci_op_write_4(sc, port, v &~ XHCI_PS_PED);
2303 			break;
2304 		case UHF_PORT_SUSPEND:
2305 			err = USBD_IOERROR;
2306 			goto ret;
2307 		case UHF_PORT_POWER:
2308 			break;
2309 		case UHF_PORT_TEST:
2310 		case UHF_PORT_INDICATOR:
2311 			err = USBD_IOERROR;
2312 			goto ret;
2313 		case UHF_C_PORT_CONNECTION:
2314 			xhci_op_write_4(sc, port, v | XHCI_PS_CSC);
2315 			break;
2316 		case UHF_C_PORT_ENABLE:
2317 		case UHF_C_PORT_SUSPEND:
2318 		case UHF_C_PORT_OVER_CURRENT:
2319 			err = USBD_IOERROR;
2320 			goto ret;
2321 		case UHF_C_PORT_RESET:
2322 			xhci_op_write_4(sc, port, v | XHCI_PS_PRC);
2323 			break;
2324 		default:
2325 			err = USBD_IOERROR;
2326 			goto ret;
2327 		}
2328 
2329 		break;
2330 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2331 		if (len == 0)
2332 			break;
2333 		if ((value & 0xff) != 0) {
2334 			err = USBD_IOERROR;
2335 			goto ret;
2336 		}
2337 		hubd = xhci_hubd;
2338 		hubd.bNbrPorts = sc->sc_hs_port_count;
2339 		USETW(hubd.wHubCharacteristics, UHD_PWR_NO_SWITCH);
2340 		hubd.bPwrOn2PwrGood = 200;
2341 		for (i = 0, l = sc->sc_maxports; l > 0; i++, l -= 8)
2342 			hubd.DeviceRemovable[i++] = 0; /* XXX can't find out? */
2343 		hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
2344 		l = min(len, hubd.bDescLength);
2345 		totlen = l;
2346 		memcpy(buf, &hubd, l);
2347 		break;
2348 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2349 		if (len != 4) {
2350 			err = USBD_IOERROR;
2351 			goto ret;
2352 		}
2353 		memset(buf, 0, len); /* ? XXX */
2354 		totlen = len;
2355 		break;
2356 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2357 		DPRINTFN(8, "get port status i=%d", index, 0, 0, 0);
2358 		if (index < 1 || index > sc->sc_maxports) {
2359 			err = USBD_IOERROR;
2360 			goto ret;
2361 		}
2362 		if (len != 4) {
2363 			err = USBD_IOERROR;
2364 			goto ret;
2365 		}
2366 		v = xhci_op_read_4(sc, XHCI_PORTSC(sc->sc_hs_port_start - 1 +
2367 		    index));
2368 		DPRINTFN(4, "READ_CLASS_OTHER GET_STATUS PORTSC %d (%d) %08x",
2369 		    index, sc->sc_hs_port_start - 1 + index, v, 0);
2370 		switch (XHCI_PS_SPEED_GET(v)) {
2371 		case 1:
2372 			i = UPS_FULL_SPEED;
2373 			break;
2374 		case 2:
2375 			i = UPS_LOW_SPEED;
2376 			break;
2377 		case 3:
2378 			i = UPS_HIGH_SPEED;
2379 			break;
2380 		default:
2381 			i = 0;
2382 			break;
2383 		}
2384 		if (v & XHCI_PS_CCS)	i |= UPS_CURRENT_CONNECT_STATUS;
2385 		if (v & XHCI_PS_PED)	i |= UPS_PORT_ENABLED;
2386 		if (v & XHCI_PS_OCA)	i |= UPS_OVERCURRENT_INDICATOR;
2387 		//if (v & XHCI_PS_SUSP)	i |= UPS_SUSPEND;
2388 		if (v & XHCI_PS_PR)	i |= UPS_RESET;
2389 		if (v & XHCI_PS_PP)	i |= UPS_PORT_POWER;
2390 		USETW(ps.wPortStatus, i);
2391 		i = 0;
2392 		if (v & XHCI_PS_CSC)    i |= UPS_C_CONNECT_STATUS;
2393 		if (v & XHCI_PS_PEC)    i |= UPS_C_PORT_ENABLED;
2394 		if (v & XHCI_PS_OCC)    i |= UPS_C_OVERCURRENT_INDICATOR;
2395 		if (v & XHCI_PS_PRC)	i |= UPS_C_PORT_RESET;
2396 		USETW(ps.wPortChange, i);
2397 		l = min(len, sizeof ps);
2398 		memcpy(buf, &ps, l);
2399 		totlen = l;
2400 		break;
2401 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2402 		err = USBD_IOERROR;
2403 		goto ret;
2404 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2405 		break;
2406 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2407 		if (index < 1 || index > sc->sc_hs_port_count) {
2408 			err = USBD_IOERROR;
2409 			goto ret;
2410 		}
2411 		port = XHCI_PORTSC(sc->sc_hs_port_start - 1 + index);
2412 		v = xhci_op_read_4(sc, port);
2413 		DPRINTFN(4, "portsc=0x%08x", v, 0, 0, 0);
2414 		v &= ~XHCI_PS_CLEAR;
2415 		switch (value) {
2416 		case UHF_PORT_ENABLE:
2417 			xhci_op_write_4(sc, port, v | XHCI_PS_PED);
2418 			break;
2419 		case UHF_PORT_SUSPEND:
2420 			/* XXX suspend */
2421 			break;
2422 		case UHF_PORT_RESET:
2423 			v &= ~ (XHCI_PS_PED | XHCI_PS_PR);
2424 			xhci_op_write_4(sc, port, v | XHCI_PS_PR);
2425 			/* Wait for reset to complete. */
2426 			usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
2427 			if (sc->sc_dying) {
2428 				err = USBD_IOERROR;
2429 				goto ret;
2430 			}
2431 			v = xhci_op_read_4(sc, port);
2432 			if (v & XHCI_PS_PR) {
2433 				xhci_op_write_4(sc, port, v & ~XHCI_PS_PR);
2434 				usb_delay_ms(&sc->sc_bus, 10);
2435 				/* XXX */
2436 			}
2437 			break;
2438 		case UHF_PORT_POWER:
2439 			/* XXX power control */
2440 			break;
2441 		/* XXX more */
2442 		case UHF_C_PORT_RESET:
2443 			xhci_op_write_4(sc, port, v | XHCI_PS_PRC);
2444 			break;
2445 		default:
2446 			err = USBD_IOERROR;
2447 			goto ret;
2448 		}
2449 		break;
2450 	case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
2451 	case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
2452 	case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
2453 	case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
2454 		break;
2455 	default:
2456 		err = USBD_IOERROR;
2457 		goto ret;
2458 	}
2459 	xfer->actlen = totlen;
2460 	err = USBD_NORMAL_COMPLETION;
2461 ret:
2462 	xfer->status = err;
2463 	mutex_enter(&sc->sc_lock);
2464 	usb_transfer_complete(xfer);
2465 	mutex_exit(&sc->sc_lock);
2466 	return USBD_IN_PROGRESS;
2467 }
2468 
2469 
2470 static void
2471 xhci_root_ctrl_abort(usbd_xfer_handle xfer)
2472 {
2473 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
2474 	/* Nothing to do, all transfers are synchronous. */
2475 }
2476 
2477 
2478 static void
2479 xhci_root_ctrl_close(usbd_pipe_handle pipe)
2480 {
2481 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
2482 	/* Nothing to do. */
2483 }
2484 
2485 static void
2486 xhci_root_ctrl_done(usbd_xfer_handle xfer)
2487 {
2488 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
2489 
2490 	xfer->hcpriv = NULL;
2491 }
2492 
2493 /* root hub interrupt */
2494 
2495 static usbd_status
2496 xhci_root_intr_transfer(usbd_xfer_handle xfer)
2497 {
2498 	struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private;
2499 	usbd_status err;
2500 
2501 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
2502 
2503 	/* Insert last in queue. */
2504 	mutex_enter(&sc->sc_lock);
2505 	err = usb_insert_transfer(xfer);
2506 	mutex_exit(&sc->sc_lock);
2507 	if (err)
2508 		return err;
2509 
2510 	/* Pipe isn't running, start first */
2511 	return (xhci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2512 }
2513 
2514 static usbd_status
2515 xhci_root_intr_start(usbd_xfer_handle xfer)
2516 {
2517 	struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private;
2518 
2519 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
2520 
2521 	if (sc->sc_dying)
2522 		return USBD_IOERROR;
2523 
2524 	mutex_enter(&sc->sc_lock);
2525 	sc->sc_intrxfer = xfer;
2526 	mutex_exit(&sc->sc_lock);
2527 
2528 	return USBD_IN_PROGRESS;
2529 }
2530 
2531 static void
2532 xhci_root_intr_abort(usbd_xfer_handle xfer)
2533 {
2534 	struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private;
2535 
2536 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
2537 
2538 	KASSERT(mutex_owned(&sc->sc_lock));
2539 	KASSERT(xfer->pipe->intrxfer == xfer);
2540 
2541 	DPRINTFN(1, "remove", 0, 0, 0, 0);
2542 
2543 	sc->sc_intrxfer = NULL;
2544 
2545 	xfer->status = USBD_CANCELLED;
2546 	usb_transfer_complete(xfer);
2547 }
2548 
2549 static void
2550 xhci_root_intr_close(usbd_pipe_handle pipe)
2551 {
2552 	struct xhci_softc * const sc = pipe->device->bus->hci_private;
2553 
2554 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
2555 
2556 	KASSERT(mutex_owned(&sc->sc_lock));
2557 
2558 	sc->sc_intrxfer = NULL;
2559 }
2560 
2561 static void
2562 xhci_root_intr_done(usbd_xfer_handle xfer)
2563 {
2564 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
2565 
2566 	xfer->hcpriv = NULL;
2567 }
2568 
2569 /* -------------- */
2570 /* device control */
2571 
2572 static usbd_status
2573 xhci_device_ctrl_transfer(usbd_xfer_handle xfer)
2574 {
2575 	struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private;
2576 	usbd_status err;
2577 
2578 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
2579 
2580 	/* Insert last in queue. */
2581 	mutex_enter(&sc->sc_lock);
2582 	err = usb_insert_transfer(xfer);
2583 	mutex_exit(&sc->sc_lock);
2584 	if (err)
2585 		return (err);
2586 
2587 	/* Pipe isn't running, start first */
2588 	return (xhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2589 }
2590 
2591 static usbd_status
2592 xhci_device_ctrl_start(usbd_xfer_handle xfer)
2593 {
2594 	struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private;
2595 	struct xhci_slot * const xs = xfer->pipe->device->hci_private;
2596 	const u_int dci = xhci_ep_get_dci(xfer->pipe->endpoint->edesc);
2597 	struct xhci_ring * const tr = &xs->xs_ep[dci].xe_tr;
2598 	struct xhci_xfer * const xx = (void *)xfer;
2599 	usb_device_request_t * const req = &xfer->request;
2600 	const bool isread = UT_GET_DIR(req->bmRequestType) == UT_READ;
2601 	const uint32_t len = UGETW(req->wLength);
2602 	usb_dma_t * const dma = &xfer->dmabuf;
2603 	uint64_t parameter;
2604 	uint32_t status;
2605 	uint32_t control;
2606 	u_int i;
2607 
2608 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
2609 	DPRINTFN(12, "req: %04x %04x %04x %04x",
2610 	    req->bmRequestType | (req->bRequest << 8), UGETW(req->wValue),
2611 	    UGETW(req->wIndex), UGETW(req->wLength));
2612 
2613 	/* XXX */
2614 	if (tr->is_halted) {
2615 		xhci_reset_endpoint(xfer->pipe);
2616 		tr->is_halted = false;
2617 		xhci_set_dequeue(xfer->pipe);
2618 	}
2619 
2620 	/* we rely on the bottom bits for extra info */
2621 	KASSERT(((uintptr_t)xfer & 0x3) == 0x0);
2622 
2623 	KASSERT((xfer->rqflags & URQ_REQUEST) != 0);
2624 
2625 	i = 0;
2626 
2627 	/* setup phase */
2628 	memcpy(&parameter, req, sizeof(*req));
2629 	parameter = le64toh(parameter);
2630 	status = XHCI_TRB_2_IRQ_SET(0) | XHCI_TRB_2_BYTES_SET(sizeof(*req));
2631 	control = ((len == 0) ? XHCI_TRB_3_TRT_NONE :
2632 	     (isread ? XHCI_TRB_3_TRT_IN : XHCI_TRB_3_TRT_OUT)) |
2633 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SETUP_STAGE) |
2634 	    XHCI_TRB_3_IDT_BIT;
2635 	xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
2636 
2637 	if (len == 0)
2638 		goto no_data;
2639 
2640 	/* data phase */
2641 	parameter = DMAADDR(dma, 0);
2642 	KASSERT(len <= 0x10000);
2643 	status = XHCI_TRB_2_IRQ_SET(0) |
2644 	    XHCI_TRB_2_TDSZ_SET(1) |
2645 	    XHCI_TRB_2_BYTES_SET(len);
2646 	control = (isread ? XHCI_TRB_3_DIR_IN : 0) |
2647 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DATA_STAGE) |
2648 	    XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_ENT_BIT;
2649 	xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
2650 
2651 	parameter = (uintptr_t)xfer | 0x3;
2652 	status = XHCI_TRB_2_IRQ_SET(0);
2653 	control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVENT_DATA) |
2654 	    XHCI_TRB_3_IOC_BIT;
2655 	xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
2656 
2657 no_data:
2658 	parameter = 0;
2659 	status = XHCI_TRB_2_IRQ_SET(0);
2660 	/* the status stage has inverted direction */
2661 	control = ((isread && (len > 0)) ? 0 : XHCI_TRB_3_DIR_IN) |
2662 	    XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STATUS_STAGE) |
2663 	    XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_ENT_BIT;
2664 	xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
2665 
2666 	parameter = (uintptr_t)xfer | 0x0;
2667 	status = XHCI_TRB_2_IRQ_SET(0);
2668 	control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVENT_DATA) |
2669 	    XHCI_TRB_3_IOC_BIT;
2670 	xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
2671 
2672 	mutex_enter(&tr->xr_lock);
2673 	xhci_ring_put(sc, tr, xfer, xx->xx_trb, i);
2674 	mutex_exit(&tr->xr_lock);
2675 
2676 	xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci);
2677 
2678 	if (xfer->timeout && !sc->sc_bus.use_polling) {
2679 		callout_reset(&xfer->timeout_handle, mstohz(xfer->timeout),
2680 		    xhci_timeout, xfer);
2681 	}
2682 
2683 	if (sc->sc_bus.use_polling) {
2684 		DPRINTFN(1, "polling", 0, 0, 0, 0);
2685 		//xhci_waitintr(sc, xfer);
2686 	}
2687 
2688 	return USBD_IN_PROGRESS;
2689 }
2690 
2691 static void
2692 xhci_device_ctrl_done(usbd_xfer_handle xfer)
2693 {
2694 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
2695 
2696 	callout_stop(&xfer->timeout_handle); /* XXX wrong place */
2697 
2698 }
2699 
2700 static void
2701 xhci_device_ctrl_abort(usbd_xfer_handle xfer)
2702 {
2703 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
2704 }
2705 
2706 static void
2707 xhci_device_ctrl_close(usbd_pipe_handle pipe)
2708 {
2709 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
2710 }
2711 
2712 /* ----------------- */
2713 /* device isochronus */
2714 
2715 /* ----------- */
2716 /* device bulk */
2717 
2718 static usbd_status
2719 xhci_device_bulk_transfer(usbd_xfer_handle xfer)
2720 {
2721 	struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private;
2722 	usbd_status err;
2723 
2724 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
2725 
2726 	/* Insert last in queue. */
2727 	mutex_enter(&sc->sc_lock);
2728 	err = usb_insert_transfer(xfer);
2729 	mutex_exit(&sc->sc_lock);
2730 	if (err)
2731 		return err;
2732 
2733 	/*
2734 	 * Pipe isn't running (otherwise err would be USBD_INPROG),
2735 	 * so start it first.
2736 	 */
2737 	return (xhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2738 }
2739 
2740 static usbd_status
2741 xhci_device_bulk_start(usbd_xfer_handle xfer)
2742 {
2743 	struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private;
2744 	struct xhci_slot * const xs = xfer->pipe->device->hci_private;
2745 	const u_int dci = xhci_ep_get_dci(xfer->pipe->endpoint->edesc);
2746 	struct xhci_ring * const tr = &xs->xs_ep[dci].xe_tr;
2747 	struct xhci_xfer * const xx = (void *)xfer;
2748 	const uint32_t len = xfer->length;
2749 	usb_dma_t * const dma = &xfer->dmabuf;
2750 	uint64_t parameter;
2751 	uint32_t status;
2752 	uint32_t control;
2753 	u_int i = 0;
2754 
2755 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
2756 
2757 	DPRINTFN(15, "%p slot %u dci %u", xfer, xs->xs_idx, dci, 0);
2758 
2759 	if (sc->sc_dying)
2760 		return USBD_IOERROR;
2761 
2762 	KASSERT((xfer->rqflags & URQ_REQUEST) == 0);
2763 
2764 	parameter = DMAADDR(dma, 0);
2765 	/*
2766 	 * XXX: (dsl) The physical buffer must not cross a 64k boundary.
2767 	 * If the user supplied buffer crosses such a boundary then 2
2768 	 * (or more) TRB should be used.
2769 	 * If multiple TRB are used the td_size field must be set correctly.
2770 	 * For v1.0 devices (like ivy bridge) this is the number of usb data
2771 	 * blocks needed to complete the transfer.
2772 	 * Setting it to 1 in the last TRB causes an extra zero-length
2773 	 * data block be sent.
2774 	 * The earlier documentation differs, I don't know how it behaves.
2775 	 */
2776 	KASSERT(len <= 0x10000);
2777 	status = XHCI_TRB_2_IRQ_SET(0) |
2778 	    XHCI_TRB_2_TDSZ_SET(1) |
2779 	    XHCI_TRB_2_BYTES_SET(len);
2780 	control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL) |
2781 	    XHCI_TRB_3_ISP_BIT | XHCI_TRB_3_IOC_BIT;
2782 	xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
2783 
2784 	mutex_enter(&tr->xr_lock);
2785 	xhci_ring_put(sc, tr, xfer, xx->xx_trb, i);
2786 	mutex_exit(&tr->xr_lock);
2787 
2788 	xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci);
2789 
2790 	if (sc->sc_bus.use_polling) {
2791 		DPRINTFN(1, "polling", 0, 0, 0, 0);
2792 		//xhci_waitintr(sc, xfer);
2793 	}
2794 
2795 	return USBD_IN_PROGRESS;
2796 }
2797 
2798 static void
2799 xhci_device_bulk_done(usbd_xfer_handle xfer)
2800 {
2801 #ifdef USB_DEBUG
2802 	struct xhci_slot * const xs = xfer->pipe->device->hci_private;
2803 	const u_int dci = xhci_ep_get_dci(xfer->pipe->endpoint->edesc);
2804 #endif
2805 	const u_int endpt = xfer->pipe->endpoint->edesc->bEndpointAddress;
2806 	const bool isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2807 
2808 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
2809 
2810 	DPRINTFN(15, "%p slot %u dci %u", xfer, xs->xs_idx, dci, 0);
2811 
2812 	callout_stop(&xfer->timeout_handle); /* XXX wrong place */
2813 
2814 	usb_syncmem(&xfer->dmabuf, 0, xfer->length,
2815 	    isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
2816 
2817 
2818 }
2819 
2820 static void
2821 xhci_device_bulk_abort(usbd_xfer_handle xfer)
2822 {
2823 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
2824 }
2825 
2826 static void
2827 xhci_device_bulk_close(usbd_pipe_handle pipe)
2828 {
2829 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
2830 }
2831 
2832 /* --------------- */
2833 /* device intrrupt */
2834 
2835 static usbd_status
2836 xhci_device_intr_transfer(usbd_xfer_handle xfer)
2837 {
2838 	struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private;
2839 	usbd_status err;
2840 
2841 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
2842 
2843 	/* Insert last in queue. */
2844 	mutex_enter(&sc->sc_lock);
2845 	err = usb_insert_transfer(xfer);
2846 	mutex_exit(&sc->sc_lock);
2847 	if (err)
2848 		return err;
2849 
2850 	/*
2851 	 * Pipe isn't running (otherwise err would be USBD_INPROG),
2852 	 * so start it first.
2853 	 */
2854 	return (xhci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2855 }
2856 
2857 static usbd_status
2858 xhci_device_intr_start(usbd_xfer_handle xfer)
2859 {
2860 	struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private;
2861 	struct xhci_slot * const xs = xfer->pipe->device->hci_private;
2862 	const u_int dci = xhci_ep_get_dci(xfer->pipe->endpoint->edesc);
2863 	struct xhci_ring * const tr = &xs->xs_ep[dci].xe_tr;
2864 	struct xhci_xfer * const xx = (void *)xfer;
2865 	const uint32_t len = xfer->length;
2866 	usb_dma_t * const dma = &xfer->dmabuf;
2867 	uint64_t parameter;
2868 	uint32_t status;
2869 	uint32_t control;
2870 	u_int i = 0;
2871 
2872 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
2873 
2874 	DPRINTFN(15, "%p slot %u dci %u", xfer, xs->xs_idx, dci, 0);
2875 
2876 	if (sc->sc_dying)
2877 		return USBD_IOERROR;
2878 
2879 	KASSERT((xfer->rqflags & URQ_REQUEST) == 0);
2880 
2881 	parameter = DMAADDR(dma, 0);
2882 	KASSERT(len <= 0x10000);
2883 	status = XHCI_TRB_2_IRQ_SET(0) |
2884 	    XHCI_TRB_2_TDSZ_SET(1) |
2885 	    XHCI_TRB_2_BYTES_SET(len);
2886 	control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL) |
2887 	    XHCI_TRB_3_ISP_BIT | XHCI_TRB_3_IOC_BIT;
2888 	xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
2889 
2890 	mutex_enter(&tr->xr_lock);
2891 	xhci_ring_put(sc, tr, xfer, xx->xx_trb, i);
2892 	mutex_exit(&tr->xr_lock);
2893 
2894 	xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci);
2895 
2896 	if (sc->sc_bus.use_polling) {
2897 		DPRINTFN(1, "polling", 0, 0, 0, 0);
2898 		//xhci_waitintr(sc, xfer);
2899 	}
2900 
2901 	return USBD_IN_PROGRESS;
2902 }
2903 
2904 static void
2905 xhci_device_intr_done(usbd_xfer_handle xfer)
2906 {
2907 	struct xhci_softc * const sc __diagused =
2908 		xfer->pipe->device->bus->hci_private;
2909 #ifdef USB_DEBUG
2910 	struct xhci_slot * const xs = xfer->pipe->device->hci_private;
2911 	const u_int dci = xhci_ep_get_dci(xfer->pipe->endpoint->edesc);
2912 #endif
2913 	const u_int endpt = xfer->pipe->endpoint->edesc->bEndpointAddress;
2914 	const bool isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2915 
2916 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
2917 
2918 	DPRINTFN(15, "%p slot %u dci %u", xfer, xs->xs_idx, dci, 0);
2919 
2920 	KASSERT(sc->sc_bus.use_polling || mutex_owned(&sc->sc_lock));
2921 
2922 	usb_syncmem(&xfer->dmabuf, 0, xfer->length,
2923 	    isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
2924 
2925 #if 0
2926 	device_printf(sc->sc_dev, "");
2927 	for (size_t i = 0; i < xfer->length; i++) {
2928 		printf(" %02x", ((uint8_t const *)xfer->buffer)[i]);
2929 	}
2930 	printf("\n");
2931 #endif
2932 
2933 	if (xfer->pipe->repeat) {
2934 		xfer->status = xhci_device_intr_start(xfer);
2935 	} else {
2936 		callout_stop(&xfer->timeout_handle); /* XXX */
2937 	}
2938 
2939 }
2940 
2941 static void
2942 xhci_device_intr_abort(usbd_xfer_handle xfer)
2943 {
2944 	struct xhci_softc * const sc __diagused =
2945 				    xfer->pipe->device->bus->hci_private;
2946 
2947 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
2948 
2949 	KASSERT(mutex_owned(&sc->sc_lock));
2950 	DPRINTFN(15, "%p", xfer, 0, 0, 0);
2951 	KASSERT(xfer->pipe->intrxfer == xfer);
2952 	xfer->status = USBD_CANCELLED;
2953 	usb_transfer_complete(xfer);
2954 }
2955 
2956 static void
2957 xhci_device_intr_close(usbd_pipe_handle pipe)
2958 {
2959 	//struct xhci_softc * const sc = pipe->device->bus->hci_private;
2960 
2961 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
2962 	DPRINTFN(15, "%p", pipe, 0, 0, 0);
2963 
2964 	xhci_unconfigure_endpoint(pipe);
2965 }
2966 
2967 /* ------------ */
2968 
2969 static void
2970 xhci_timeout(void *addr)
2971 {
2972 	struct xhci_xfer * const xx = addr;
2973 	usbd_xfer_handle const xfer = &xx->xx_xfer;
2974 	struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private;
2975 
2976 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
2977 
2978 	if (sc->sc_dying) {
2979 		return;
2980 	}
2981 
2982 	usb_init_task(&xx->xx_abort_task, xhci_timeout_task, addr,
2983 	    USB_TASKQ_MPSAFE);
2984 	usb_add_task(xx->xx_xfer.pipe->device, &xx->xx_abort_task,
2985 	    USB_TASKQ_HC);
2986 }
2987 
2988 static void
2989 xhci_timeout_task(void *addr)
2990 {
2991 	usbd_xfer_handle const xfer = addr;
2992 	struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private;
2993 
2994 	XHCIHIST_FUNC(); XHCIHIST_CALLED();
2995 
2996 	mutex_enter(&sc->sc_lock);
2997 #if 0
2998 	xhci_abort_xfer(xfer, USBD_TIMEOUT);
2999 #else
3000 	xfer->status = USBD_TIMEOUT;
3001 	usb_transfer_complete(xfer);
3002 #endif
3003 	mutex_exit(&sc->sc_lock);
3004 }
3005