2 * Copyright (c) 2010 Hans Petter Selasky. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * USB eXtensible Host Controller Interface, a.k.a. USB 3.0 controller.
29 * The XHCI 1.0 spec can be found at
30 * http://www.intel.com/technology/usb/download/xHCI_Specification_for_USB.pdf
31 * and the USB 3.0 spec at
32 * http://www.usb.org/developers/docs/usb_30_spec_060910.zip
36 * A few words about the design implementation: This driver emulates
37 * the concept about TDs which is found in EHCI specification. This
38 * way we avoid too much diveration among USB drivers.
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/stdint.h>
45 #include <sys/stddef.h>
46 #include <sys/param.h>
47 #include <sys/queue.h>
48 #include <sys/types.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
52 #include <sys/module.h>
54 #include <sys/mutex.h>
55 #include <sys/condvar.h>
56 #include <sys/sysctl.h>
58 #include <sys/unistd.h>
59 #include <sys/callout.h>
60 #include <sys/malloc.h>
63 #include <dev/usb/usb.h>
64 #include <dev/usb/usbdi.h>
66 #define USB_DEBUG_VAR xhcidebug
68 #include <dev/usb/usb_core.h>
69 #include <dev/usb/usb_debug.h>
70 #include <dev/usb/usb_busdma.h>
71 #include <dev/usb/usb_process.h>
72 #include <dev/usb/usb_transfer.h>
73 #include <dev/usb/usb_device.h>
74 #include <dev/usb/usb_hub.h>
75 #include <dev/usb/usb_util.h>
77 #include <dev/usb/usb_controller.h>
78 #include <dev/usb/usb_bus.h>
79 #include <dev/usb/controller/xhci.h>
80 #include <dev/usb/controller/xhcireg.h>
82 #define XHCI_BUS2SC(bus) \
83 ((struct xhci_softc *)(((uint8_t *)(bus)) - \
84 ((uint8_t *)&(((struct xhci_softc *)0)->sc_bus))))
90 SYSCTL_NODE(_hw_usb, OID_AUTO, xhci, CTLFLAG_RW, 0, "USB XHCI");
91 SYSCTL_INT(_hw_usb_xhci, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN,
92 &xhcidebug, 0, "Debug level");
93 TUNABLE_INT("hw.usb.xhci.debug", &xhcidebug);
94 SYSCTL_INT(_hw_usb_xhci, OID_AUTO, xhci_port_route, CTLFLAG_RW | CTLFLAG_TUN,
95 &xhciroute, 0, "Routing bitmap for switching EHCI ports to XHCI controller");
96 TUNABLE_INT("hw.usb.xhci.xhci_port_route", &xhciroute);
101 #define XHCI_INTR_ENDPT 1
103 struct xhci_std_temp {
104 struct xhci_softc *sc;
105 struct usb_page_cache *pc;
107 struct xhci_td *td_next;
110 uint32_t max_packet_size;
122 uint8_t do_isoc_sync;
125 static void xhci_do_poll(struct usb_bus *);
126 static void xhci_device_done(struct usb_xfer *, usb_error_t);
127 static void xhci_root_intr(struct xhci_softc *);
128 static void xhci_free_device_ext(struct usb_device *);
129 static struct xhci_endpoint_ext *xhci_get_endpoint_ext(struct usb_device *,
130 struct usb_endpoint_descriptor *);
131 static usb_proc_callback_t xhci_configure_msg;
132 static usb_error_t xhci_configure_device(struct usb_device *);
133 static usb_error_t xhci_configure_endpoint(struct usb_device *,
134 struct usb_endpoint_descriptor *, uint64_t, uint16_t,
135 uint8_t, uint8_t, uint8_t, uint16_t, uint16_t);
136 static usb_error_t xhci_configure_mask(struct usb_device *,
138 static usb_error_t xhci_cmd_evaluate_ctx(struct xhci_softc *,
140 static void xhci_endpoint_doorbell(struct usb_xfer *);
141 static void xhci_ctx_set_le32(struct xhci_softc *sc, volatile uint32_t *ptr, uint32_t val);
142 static uint32_t xhci_ctx_get_le32(struct xhci_softc *sc, volatile uint32_t *ptr);
143 static void xhci_ctx_set_le64(struct xhci_softc *sc, volatile uint64_t *ptr, uint64_t val);
145 static uint64_t xhci_ctx_get_le64(struct xhci_softc *sc, volatile uint64_t *ptr);
148 extern struct usb_bus_methods xhci_bus_methods;
152 xhci_dump_trb(struct xhci_trb *trb)
154 DPRINTFN(5, "trb = %p\n", trb);
155 DPRINTFN(5, "qwTrb0 = 0x%016llx\n", (long long)le64toh(trb->qwTrb0));
156 DPRINTFN(5, "dwTrb2 = 0x%08x\n", le32toh(trb->dwTrb2));
157 DPRINTFN(5, "dwTrb3 = 0x%08x\n", le32toh(trb->dwTrb3));
161 xhci_dump_endpoint(struct xhci_softc *sc, struct xhci_endp_ctx *pep)
163 DPRINTFN(5, "pep = %p\n", pep);
164 DPRINTFN(5, "dwEpCtx0=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx0));
165 DPRINTFN(5, "dwEpCtx1=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx1));
166 DPRINTFN(5, "qwEpCtx2=0x%016llx\n", (long long)xhci_ctx_get_le64(sc, &pep->qwEpCtx2));
167 DPRINTFN(5, "dwEpCtx4=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx4));
168 DPRINTFN(5, "dwEpCtx5=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx5));
169 DPRINTFN(5, "dwEpCtx6=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx6));
170 DPRINTFN(5, "dwEpCtx7=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx7));
174 xhci_dump_device(struct xhci_softc *sc, struct xhci_slot_ctx *psl)
176 DPRINTFN(5, "psl = %p\n", psl);
177 DPRINTFN(5, "dwSctx0=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx0));
178 DPRINTFN(5, "dwSctx1=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx1));
179 DPRINTFN(5, "dwSctx2=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx2));
180 DPRINTFN(5, "dwSctx3=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx3));
185 xhci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
187 struct xhci_softc *sc = XHCI_BUS2SC(bus);
190 cb(bus, &sc->sc_hw.root_pc, &sc->sc_hw.root_pg,
191 sizeof(struct xhci_hw_root), XHCI_PAGE_SIZE);
193 cb(bus, &sc->sc_hw.ctx_pc, &sc->sc_hw.ctx_pg,
194 sizeof(struct xhci_dev_ctx_addr), XHCI_PAGE_SIZE);
196 for (i = 0; i != XHCI_MAX_SCRATCHPADS; i++) {
197 cb(bus, &sc->sc_hw.scratch_pc[i], &sc->sc_hw.scratch_pg[i],
198 XHCI_PAGE_SIZE, XHCI_PAGE_SIZE);
203 xhci_ctx_set_le32(struct xhci_softc *sc, volatile uint32_t *ptr, uint32_t val)
205 if (sc->sc_ctx_is_64_byte) {
207 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
208 /* all contexts are initially 32-bytes */
209 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
210 ptr = (volatile uint32_t *)(((volatile uint8_t *)ptr) + offset);
216 xhci_ctx_get_le32(struct xhci_softc *sc, volatile uint32_t *ptr)
218 if (sc->sc_ctx_is_64_byte) {
220 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
221 /* all contexts are initially 32-bytes */
222 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
223 ptr = (volatile uint32_t *)(((volatile uint8_t *)ptr) + offset);
225 return (le32toh(*ptr));
229 xhci_ctx_set_le64(struct xhci_softc *sc, volatile uint64_t *ptr, uint64_t val)
231 if (sc->sc_ctx_is_64_byte) {
233 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
234 /* all contexts are initially 32-bytes */
235 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
236 ptr = (volatile uint64_t *)(((volatile uint8_t *)ptr) + offset);
243 xhci_ctx_get_le64(struct xhci_softc *sc, volatile uint64_t *ptr)
245 if (sc->sc_ctx_is_64_byte) {
247 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
248 /* all contexts are initially 32-bytes */
249 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
250 ptr = (volatile uint64_t *)(((volatile uint8_t *)ptr) + offset);
252 return (le64toh(*ptr));
257 xhci_start_controller(struct xhci_softc *sc)
259 struct usb_page_search buf_res;
260 struct xhci_hw_root *phwr;
261 struct xhci_dev_ctx_addr *pdctxa;
269 sc->sc_oper_off = XREAD1(sc, capa, XHCI_CAPLENGTH);
270 sc->sc_runt_off = XREAD4(sc, capa, XHCI_RTSOFF) & ~0x1F;
271 sc->sc_door_off = XREAD4(sc, capa, XHCI_DBOFF) & ~0x3;
273 DPRINTF("CAPLENGTH=0x%x\n", sc->sc_oper_off);
274 DPRINTF("RUNTIMEOFFSET=0x%x\n", sc->sc_runt_off);
275 DPRINTF("DOOROFFSET=0x%x\n", sc->sc_door_off);
277 sc->sc_event_ccs = 1;
278 sc->sc_event_idx = 0;
279 sc->sc_command_ccs = 1;
280 sc->sc_command_idx = 0;
282 DPRINTF("xHCI version = 0x%04x\n", XREAD2(sc, capa, XHCI_HCIVERSION));
284 temp = XREAD4(sc, capa, XHCI_HCSPARAMS0);
286 DPRINTF("HCS0 = 0x%08x\n", temp);
288 if (XHCI_HCS0_CSZ(temp)) {
289 sc->sc_ctx_is_64_byte = 1;
290 device_printf(sc->sc_bus.parent, "64 byte context size.\n");
292 sc->sc_ctx_is_64_byte = 0;
293 device_printf(sc->sc_bus.parent, "32 byte context size.\n");
296 /* Reset controller */
297 XWRITE4(sc, oper, XHCI_USBCMD, XHCI_CMD_HCRST);
299 for (i = 0; i != 100; i++) {
300 usb_pause_mtx(NULL, hz / 100);
301 temp = XREAD4(sc, oper, XHCI_USBCMD) &
302 (XHCI_CMD_HCRST | XHCI_STS_CNR);
308 device_printf(sc->sc_bus.parent, "Controller "
310 return (USB_ERR_IOERROR);
313 if (!(XREAD4(sc, oper, XHCI_PAGESIZE) & XHCI_PAGESIZE_4K)) {
314 device_printf(sc->sc_bus.parent, "Controller does "
315 "not support 4K page size.\n");
316 return (USB_ERR_IOERROR);
319 temp = XREAD4(sc, capa, XHCI_HCSPARAMS1);
321 i = XHCI_HCS1_N_PORTS(temp);
324 device_printf(sc->sc_bus.parent, "Invalid number "
325 "of ports: %u\n", i);
326 return (USB_ERR_IOERROR);
330 sc->sc_noslot = XHCI_HCS1_DEVSLOT_MAX(temp);
332 if (sc->sc_noslot > XHCI_MAX_DEVICES)
333 sc->sc_noslot = XHCI_MAX_DEVICES;
335 /* setup number of device slots */
337 DPRINTF("CONFIG=0x%08x -> 0x%08x\n",
338 XREAD4(sc, oper, XHCI_CONFIG), sc->sc_noslot);
340 XWRITE4(sc, oper, XHCI_CONFIG, sc->sc_noslot);
342 DPRINTF("Max slots: %u\n", sc->sc_noslot);
344 temp = XREAD4(sc, capa, XHCI_HCSPARAMS2);
346 sc->sc_noscratch = XHCI_HCS2_SPB_MAX(temp);
348 if (sc->sc_noscratch > XHCI_MAX_SCRATCHPADS) {
349 device_printf(sc->sc_bus.parent, "XHCI request "
350 "too many scratchpads\n");
351 return (USB_ERR_NOMEM);
354 DPRINTF("Max scratch: %u\n", sc->sc_noscratch);
356 temp = XREAD4(sc, capa, XHCI_HCSPARAMS3);
358 sc->sc_exit_lat_max = XHCI_HCS3_U1_DEL(temp) +
359 XHCI_HCS3_U2_DEL(temp) + 250 /* us */;
361 temp = XREAD4(sc, oper, XHCI_USBSTS);
363 /* clear interrupts */
364 XWRITE4(sc, oper, XHCI_USBSTS, temp);
365 /* disable all device notifications */
366 XWRITE4(sc, oper, XHCI_DNCTRL, 0);
368 /* setup device context base address */
369 usbd_get_page(&sc->sc_hw.ctx_pc, 0, &buf_res);
370 pdctxa = buf_res.buffer;
371 memset(pdctxa, 0, sizeof(*pdctxa));
373 addr = buf_res.physaddr;
374 addr += (uintptr_t)&((struct xhci_dev_ctx_addr *)0)->qwSpBufPtr[0];
376 /* slot 0 points to the table of scratchpad pointers */
377 pdctxa->qwBaaDevCtxAddr[0] = htole64(addr);
379 for (i = 0; i != sc->sc_noscratch; i++) {
380 struct usb_page_search buf_scp;
381 usbd_get_page(&sc->sc_hw.scratch_pc[i], 0, &buf_scp);
382 pdctxa->qwSpBufPtr[i] = htole64((uint64_t)buf_scp.physaddr);
385 addr = buf_res.physaddr;
387 XWRITE4(sc, oper, XHCI_DCBAAP_LO, (uint32_t)addr);
388 XWRITE4(sc, oper, XHCI_DCBAAP_HI, (uint32_t)(addr >> 32));
389 XWRITE4(sc, oper, XHCI_DCBAAP_LO, (uint32_t)addr);
390 XWRITE4(sc, oper, XHCI_DCBAAP_HI, (uint32_t)(addr >> 32));
392 /* Setup event table size */
394 temp = XREAD4(sc, capa, XHCI_HCSPARAMS2);
396 DPRINTF("HCS2=0x%08x\n", temp);
398 temp = XHCI_HCS2_ERST_MAX(temp);
400 if (temp > XHCI_MAX_RSEG)
401 temp = XHCI_MAX_RSEG;
403 sc->sc_erst_max = temp;
405 DPRINTF("ERSTSZ=0x%08x -> 0x%08x\n",
406 XREAD4(sc, runt, XHCI_ERSTSZ(0)), temp);
408 XWRITE4(sc, runt, XHCI_ERSTSZ(0), XHCI_ERSTS_SET(temp));
410 /* Setup interrupt rate */
411 XWRITE4(sc, runt, XHCI_IMOD(0), XHCI_IMOD_DEFAULT);
413 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
415 phwr = buf_res.buffer;
416 addr = buf_res.physaddr;
417 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_events[0];
419 /* reset hardware root structure */
420 memset(phwr, 0, sizeof(*phwr));
422 phwr->hwr_ring_seg[0].qwEvrsTablePtr = htole64(addr);
423 phwr->hwr_ring_seg[0].dwEvrsTableSize = htole32(XHCI_MAX_EVENTS);
425 DPRINTF("ERDP(0)=0x%016llx\n", (unsigned long long)addr);
427 XWRITE4(sc, runt, XHCI_ERDP_LO(0), (uint32_t)addr);
428 XWRITE4(sc, runt, XHCI_ERDP_HI(0), (uint32_t)(addr >> 32));
430 addr = (uint64_t)buf_res.physaddr;
432 DPRINTF("ERSTBA(0)=0x%016llx\n", (unsigned long long)addr);
434 XWRITE4(sc, runt, XHCI_ERSTBA_LO(0), (uint32_t)addr);
435 XWRITE4(sc, runt, XHCI_ERSTBA_HI(0), (uint32_t)(addr >> 32));
437 /* Setup interrupter registers */
439 temp = XREAD4(sc, runt, XHCI_IMAN(0));
440 temp |= XHCI_IMAN_INTR_ENA;
441 XWRITE4(sc, runt, XHCI_IMAN(0), temp);
443 /* setup command ring control base address */
444 addr = buf_res.physaddr;
445 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_commands[0];
447 DPRINTF("CRCR=0x%016llx\n", (unsigned long long)addr);
449 XWRITE4(sc, oper, XHCI_CRCR_LO, ((uint32_t)addr) | XHCI_CRCR_LO_RCS);
450 XWRITE4(sc, oper, XHCI_CRCR_HI, (uint32_t)(addr >> 32));
452 phwr->hwr_commands[XHCI_MAX_COMMANDS - 1].qwTrb0 = htole64(addr);
454 usb_bus_mem_flush_all(&sc->sc_bus, &xhci_iterate_hw_softc);
457 XWRITE4(sc, oper, XHCI_USBCMD, XHCI_CMD_RS |
458 XHCI_CMD_INTE | XHCI_CMD_HSEE);
460 for (i = 0; i != 100; i++) {
461 usb_pause_mtx(NULL, hz / 100);
462 temp = XREAD4(sc, oper, XHCI_USBSTS) & XHCI_STS_HCH;
467 XWRITE4(sc, oper, XHCI_USBCMD, 0);
468 device_printf(sc->sc_bus.parent, "Run timeout.\n");
469 return (USB_ERR_IOERROR);
472 /* catch any lost interrupts */
473 xhci_do_poll(&sc->sc_bus);
475 if (sc->sc_port_route != NULL) {
476 /* Route all ports to the XHCI by default */
477 sc->sc_port_route(sc->sc_bus.parent,
478 ~xhciroute, xhciroute);
484 xhci_halt_controller(struct xhci_softc *sc)
492 sc->sc_oper_off = XREAD1(sc, capa, XHCI_CAPLENGTH);
493 sc->sc_runt_off = XREAD4(sc, capa, XHCI_RTSOFF) & ~0xF;
494 sc->sc_door_off = XREAD4(sc, capa, XHCI_DBOFF) & ~0x3;
496 /* Halt controller */
497 XWRITE4(sc, oper, XHCI_USBCMD, 0);
499 for (i = 0; i != 100; i++) {
500 usb_pause_mtx(NULL, hz / 100);
501 temp = XREAD4(sc, oper, XHCI_USBSTS) & XHCI_STS_HCH;
507 device_printf(sc->sc_bus.parent, "Controller halt timeout.\n");
508 return (USB_ERR_IOERROR);
514 xhci_init(struct xhci_softc *sc, device_t self)
516 /* initialise some bus fields */
517 sc->sc_bus.parent = self;
519 /* set the bus revision */
520 sc->sc_bus.usbrev = USB_REV_3_0;
522 /* set up the bus struct */
523 sc->sc_bus.methods = &xhci_bus_methods;
525 /* setup devices array */
526 sc->sc_bus.devices = sc->sc_devices;
527 sc->sc_bus.devices_max = XHCI_MAX_DEVICES;
529 /* setup command queue mutex and condition varible */
530 cv_init(&sc->sc_cmd_cv, "CMDQ");
531 sx_init(&sc->sc_cmd_sx, "CMDQ lock");
533 /* get all DMA memory */
534 if (usb_bus_mem_alloc_all(&sc->sc_bus,
535 USB_GET_DMA_TAG(self), &xhci_iterate_hw_softc)) {
539 sc->sc_config_msg[0].hdr.pm_callback = &xhci_configure_msg;
540 sc->sc_config_msg[0].bus = &sc->sc_bus;
541 sc->sc_config_msg[1].hdr.pm_callback = &xhci_configure_msg;
542 sc->sc_config_msg[1].bus = &sc->sc_bus;
544 if (usb_proc_create(&sc->sc_config_proc,
545 &sc->sc_bus.bus_mtx, device_get_nameunit(self), USB_PRI_MED)) {
546 printf("WARNING: Creation of XHCI configure "
547 "callback process failed.\n");
553 xhci_uninit(struct xhci_softc *sc)
555 usb_proc_free(&sc->sc_config_proc);
557 usb_bus_mem_free_all(&sc->sc_bus, &xhci_iterate_hw_softc);
559 cv_destroy(&sc->sc_cmd_cv);
560 sx_destroy(&sc->sc_cmd_sx);
564 xhci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
566 struct xhci_softc *sc = XHCI_BUS2SC(bus);
569 case USB_HW_POWER_SUSPEND:
570 DPRINTF("Stopping the XHCI\n");
571 xhci_halt_controller(sc);
573 case USB_HW_POWER_SHUTDOWN:
574 DPRINTF("Stopping the XHCI\n");
575 xhci_halt_controller(sc);
577 case USB_HW_POWER_RESUME:
578 DPRINTF("Starting the XHCI\n");
579 xhci_start_controller(sc);
587 xhci_generic_done_sub(struct usb_xfer *xfer)
590 struct xhci_td *td_alt_next;
594 td = xfer->td_transfer_cache;
595 td_alt_next = td->alt_next;
597 if (xfer->aframes != xfer->nframes)
598 usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
602 usb_pc_cpu_invalidate(td->page_cache);
607 DPRINTFN(4, "xfer=%p[%u/%u] rem=%u/%u status=%u\n",
608 xfer, (unsigned int)xfer->aframes,
609 (unsigned int)xfer->nframes,
610 (unsigned int)len, (unsigned int)td->len,
611 (unsigned int)status);
614 * Verify the status length and
615 * add the length to "frlengths[]":
618 /* should not happen */
619 DPRINTF("Invalid status length, "
620 "0x%04x/0x%04x bytes\n", len, td->len);
621 status = XHCI_TRB_ERROR_LENGTH;
622 } else if (xfer->aframes != xfer->nframes) {
623 xfer->frlengths[xfer->aframes] += td->len - len;
625 /* Check for last transfer */
626 if (((void *)td) == xfer->td_transfer_last) {
630 /* Check for transfer error */
631 if (status != XHCI_TRB_ERROR_SHORT_PKT &&
632 status != XHCI_TRB_ERROR_SUCCESS) {
633 /* the transfer is finished */
637 /* Check for short transfer */
639 if (xfer->flags_int.short_frames_ok ||
640 xfer->flags_int.isochronous_xfr ||
641 xfer->flags_int.control_xfr) {
642 /* follow alt next */
645 /* the transfer is finished */
652 if (td->alt_next != td_alt_next) {
653 /* this USB frame is complete */
658 /* update transfer cache */
660 xfer->td_transfer_cache = td;
662 return ((status == XHCI_TRB_ERROR_STALL) ? USB_ERR_STALLED :
663 (status != XHCI_TRB_ERROR_SHORT_PKT &&
664 status != XHCI_TRB_ERROR_SUCCESS) ? USB_ERR_IOERROR :
665 USB_ERR_NORMAL_COMPLETION);
669 xhci_generic_done(struct usb_xfer *xfer)
673 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
674 xfer, xfer->endpoint);
678 xfer->td_transfer_cache = xfer->td_transfer_first;
680 if (xfer->flags_int.control_xfr) {
682 if (xfer->flags_int.control_hdr)
683 err = xhci_generic_done_sub(xfer);
687 if (xfer->td_transfer_cache == NULL)
691 while (xfer->aframes != xfer->nframes) {
693 err = xhci_generic_done_sub(xfer);
696 if (xfer->td_transfer_cache == NULL)
700 if (xfer->flags_int.control_xfr &&
701 !xfer->flags_int.control_act)
702 err = xhci_generic_done_sub(xfer);
704 /* transfer is complete */
705 xhci_device_done(xfer, err);
709 xhci_activate_transfer(struct usb_xfer *xfer)
713 td = xfer->td_transfer_cache;
715 usb_pc_cpu_invalidate(td->page_cache);
717 if (!(td->td_trb[0].dwTrb3 & htole32(XHCI_TRB_3_CYCLE_BIT))) {
719 /* activate the transfer */
721 td->td_trb[0].dwTrb3 |= htole32(XHCI_TRB_3_CYCLE_BIT);
722 usb_pc_cpu_flush(td->page_cache);
724 xhci_endpoint_doorbell(xfer);
729 xhci_skip_transfer(struct usb_xfer *xfer)
732 struct xhci_td *td_last;
734 td = xfer->td_transfer_cache;
735 td_last = xfer->td_transfer_last;
739 usb_pc_cpu_invalidate(td->page_cache);
741 if (!(td->td_trb[0].dwTrb3 & htole32(XHCI_TRB_3_CYCLE_BIT))) {
743 usb_pc_cpu_invalidate(td_last->page_cache);
745 /* copy LINK TRB to current waiting location */
747 td->td_trb[0].qwTrb0 = td_last->td_trb[td_last->ntrb].qwTrb0;
748 td->td_trb[0].dwTrb2 = td_last->td_trb[td_last->ntrb].dwTrb2;
749 usb_pc_cpu_flush(td->page_cache);
751 td->td_trb[0].dwTrb3 = td_last->td_trb[td_last->ntrb].dwTrb3;
752 usb_pc_cpu_flush(td->page_cache);
754 xhci_endpoint_doorbell(xfer);
758 /*------------------------------------------------------------------------*
759 * xhci_check_transfer
760 *------------------------------------------------------------------------*/
762 xhci_check_transfer(struct xhci_softc *sc, struct xhci_trb *trb)
775 td_event = le64toh(trb->qwTrb0);
776 temp = le32toh(trb->dwTrb2);
778 remainder = XHCI_TRB_2_REM_GET(temp);
779 status = XHCI_TRB_2_ERROR_GET(temp);
781 temp = le32toh(trb->dwTrb3);
782 epno = XHCI_TRB_3_EP_GET(temp);
783 index = XHCI_TRB_3_SLOT_GET(temp);
785 /* check if error means halted */
786 halted = (status != XHCI_TRB_ERROR_SHORT_PKT &&
787 status != XHCI_TRB_ERROR_SUCCESS);
789 DPRINTF("slot=%u epno=%u remainder=%u status=%u\n",
790 index, epno, remainder, status);
792 if (index > sc->sc_noslot) {
793 DPRINTF("Invalid slot.\n");
797 if ((epno == 0) || (epno >= XHCI_MAX_ENDPOINTS)) {
798 DPRINTF("Invalid endpoint.\n");
802 /* try to find the USB transfer that generated the event */
803 for (i = 0; i != (XHCI_MAX_TRANSFERS - 1); i++) {
804 struct usb_xfer *xfer;
806 struct xhci_endpoint_ext *pepext;
808 pepext = &sc->sc_hw.devs[index].endp[epno];
810 xfer = pepext->xfer[i];
814 td = xfer->td_transfer_cache;
816 DPRINTFN(5, "Checking if 0x%016llx == (0x%016llx .. 0x%016llx)\n",
818 (long long)td->td_self,
819 (long long)td->td_self + sizeof(td->td_trb));
822 * NOTE: Some XHCI implementations might not trigger
823 * an event on the last LINK TRB so we need to
824 * consider both the last and second last event
825 * address as conditions for a successful transfer.
827 * NOTE: We assume that the XHCI will only trigger one
828 * event per chain of TRBs.
831 offset = td_event - td->td_self;
834 offset < (int64_t)sizeof(td->td_trb)) {
836 usb_pc_cpu_invalidate(td->page_cache);
838 /* compute rest of remainder, if any */
839 for (i = (offset / 16) + 1; i < td->ntrb; i++) {
840 temp = le32toh(td->td_trb[i].dwTrb2);
841 remainder += XHCI_TRB_2_BYTES_GET(temp);
844 DPRINTFN(5, "New remainder: %u\n", remainder);
846 /* clear isochronous transfer errors */
847 if (xfer->flags_int.isochronous_xfr) {
850 status = XHCI_TRB_ERROR_SUCCESS;
855 /* "td->remainder" is verified later */
856 td->remainder = remainder;
859 usb_pc_cpu_flush(td->page_cache);
862 * 1) Last transfer descriptor makes the
865 if (((void *)td) == xfer->td_transfer_last) {
866 DPRINTF("TD is last\n");
867 xhci_generic_done(xfer);
872 * 2) Any kind of error makes the transfer
876 DPRINTF("TD has I/O error\n");
877 xhci_generic_done(xfer);
882 * 3) If there is no alternate next transfer,
883 * a short packet also makes the transfer done
885 if (td->remainder > 0) {
886 if (td->alt_next == NULL) {
888 "short TD has no alternate next\n");
889 xhci_generic_done(xfer);
892 DPRINTF("TD has short pkt\n");
893 if (xfer->flags_int.short_frames_ok ||
894 xfer->flags_int.isochronous_xfr ||
895 xfer->flags_int.control_xfr) {
896 /* follow the alt next */
897 xfer->td_transfer_cache = td->alt_next;
898 xhci_activate_transfer(xfer);
901 xhci_skip_transfer(xfer);
902 xhci_generic_done(xfer);
907 * 4) Transfer complete - go to next TD
909 DPRINTF("Following next TD\n");
910 xfer->td_transfer_cache = td->obj_next;
911 xhci_activate_transfer(xfer);
912 break; /* there should only be one match */
918 xhci_check_command(struct xhci_softc *sc, struct xhci_trb *trb)
920 if (sc->sc_cmd_addr == trb->qwTrb0) {
921 DPRINTF("Received command event\n");
922 sc->sc_cmd_result[0] = trb->dwTrb2;
923 sc->sc_cmd_result[1] = trb->dwTrb3;
924 cv_signal(&sc->sc_cmd_cv);
925 return (1); /* command match */
931 xhci_interrupt_poll(struct xhci_softc *sc)
933 struct usb_page_search buf_res;
934 struct xhci_hw_root *phwr;
944 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
946 phwr = buf_res.buffer;
948 /* Receive any events */
950 usb_pc_cpu_invalidate(&sc->sc_hw.root_pc);
952 i = sc->sc_event_idx;
953 j = sc->sc_event_ccs;
958 temp = le32toh(phwr->hwr_events[i].dwTrb3);
960 k = (temp & XHCI_TRB_3_CYCLE_BIT) ? 1 : 0;
965 event = XHCI_TRB_3_TYPE_GET(temp);
967 DPRINTFN(10, "event[%u] = %u (0x%016llx 0x%08lx 0x%08lx)\n",
968 i, event, (long long)le64toh(phwr->hwr_events[i].qwTrb0),
969 (long)le32toh(phwr->hwr_events[i].dwTrb2),
970 (long)le32toh(phwr->hwr_events[i].dwTrb3));
973 case XHCI_TRB_EVENT_TRANSFER:
974 xhci_check_transfer(sc, &phwr->hwr_events[i]);
976 case XHCI_TRB_EVENT_CMD_COMPLETE:
977 retval |= xhci_check_command(sc, &phwr->hwr_events[i]);
980 DPRINTF("Unhandled event = %u\n", event);
986 if (i == XHCI_MAX_EVENTS) {
990 /* check for timeout */
996 sc->sc_event_idx = i;
997 sc->sc_event_ccs = j;
1000 * NOTE: The Event Ring Dequeue Pointer Register is 64-bit
1001 * latched. That means to activate the register we need to
1002 * write both the low and high double word of the 64-bit
1006 addr = (uint32_t)buf_res.physaddr;
1007 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_events[i];
1009 /* try to clear busy bit */
1010 addr |= XHCI_ERDP_LO_BUSY;
1012 XWRITE4(sc, runt, XHCI_ERDP_LO(0), (uint32_t)addr);
1013 XWRITE4(sc, runt, XHCI_ERDP_HI(0), (uint32_t)(addr >> 32));
1019 xhci_do_command(struct xhci_softc *sc, struct xhci_trb *trb,
1020 uint16_t timeout_ms)
1022 struct usb_page_search buf_res;
1023 struct xhci_hw_root *phwr;
1030 XHCI_CMD_ASSERT_LOCKED(sc);
1032 /* get hardware root structure */
1034 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
1036 phwr = buf_res.buffer;
1040 USB_BUS_LOCK(&sc->sc_bus);
1042 i = sc->sc_command_idx;
1043 j = sc->sc_command_ccs;
1045 DPRINTFN(10, "command[%u] = %u (0x%016llx, 0x%08lx, 0x%08lx)\n",
1046 i, XHCI_TRB_3_TYPE_GET(le32toh(trb->dwTrb3)),
1047 (long long)le64toh(trb->qwTrb0),
1048 (long)le32toh(trb->dwTrb2),
1049 (long)le32toh(trb->dwTrb3));
1051 phwr->hwr_commands[i].qwTrb0 = trb->qwTrb0;
1052 phwr->hwr_commands[i].dwTrb2 = trb->dwTrb2;
1054 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1059 temp |= htole32(XHCI_TRB_3_CYCLE_BIT);
1061 temp &= ~htole32(XHCI_TRB_3_CYCLE_BIT);
1063 temp &= ~htole32(XHCI_TRB_3_TC_BIT);
1065 phwr->hwr_commands[i].dwTrb3 = temp;
1067 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1069 addr = buf_res.physaddr;
1070 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_commands[i];
1072 sc->sc_cmd_addr = htole64(addr);
1076 if (i == (XHCI_MAX_COMMANDS - 1)) {
1079 temp = htole32(XHCI_TRB_3_TC_BIT |
1080 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) |
1081 XHCI_TRB_3_CYCLE_BIT);
1083 temp = htole32(XHCI_TRB_3_TC_BIT |
1084 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
1087 phwr->hwr_commands[i].dwTrb3 = temp;
1089 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1095 sc->sc_command_idx = i;
1096 sc->sc_command_ccs = j;
1098 XWRITE4(sc, door, XHCI_DOORBELL(0), 0);
1100 err = cv_timedwait(&sc->sc_cmd_cv, &sc->sc_bus.bus_mtx,
1101 USB_MS_TO_TICKS(timeout_ms));
1104 * In some error cases event interrupts are not generated.
1105 * Poll one time to see if the command has completed.
1107 if (err != 0 && xhci_interrupt_poll(sc) != 0) {
1108 DPRINTF("Command was completed when polling\n");
1112 DPRINTFN(0, "Command timeout!\n");
1115 * Try to abort the last command as per section
1116 * 4.6.1.2 "Aborting a Command" of the XHCI
1119 temp = XREAD4(sc, oper, XHCI_CRCR_LO);
1120 XWRITE4(sc, oper, XHCI_CRCR_LO, temp | XHCI_CRCR_LO_CA);
1122 /* wait for abort event, if any */
1123 err = cv_timedwait(&sc->sc_cmd_cv, &sc->sc_bus.bus_mtx, hz / 16);
1125 if (err != 0 && xhci_interrupt_poll(sc) != 0) {
1126 DPRINTF("Command was completed when polling\n");
1130 DPRINTF("Command abort timeout!\n");
1132 err = USB_ERR_TIMEOUT;
1136 temp = le32toh(sc->sc_cmd_result[0]);
1137 if (XHCI_TRB_2_ERROR_GET(temp) != XHCI_TRB_ERROR_SUCCESS)
1138 err = USB_ERR_IOERROR;
1140 trb->dwTrb2 = sc->sc_cmd_result[0];
1141 trb->dwTrb3 = sc->sc_cmd_result[1];
1144 USB_BUS_UNLOCK(&sc->sc_bus);
1151 xhci_cmd_nop(struct xhci_softc *sc)
1153 struct xhci_trb trb;
1160 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NOOP);
1162 trb.dwTrb3 = htole32(temp);
1164 return (xhci_do_command(sc, &trb, 100 /* ms */));
1169 xhci_cmd_enable_slot(struct xhci_softc *sc, uint8_t *pslot)
1171 struct xhci_trb trb;
1179 trb.dwTrb3 = htole32(XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ENABLE_SLOT));
1181 err = xhci_do_command(sc, &trb, 100 /* ms */);
1185 temp = le32toh(trb.dwTrb3);
1187 *pslot = XHCI_TRB_3_SLOT_GET(temp);
1194 xhci_cmd_disable_slot(struct xhci_softc *sc, uint8_t slot_id)
1196 struct xhci_trb trb;
1203 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DISABLE_SLOT) |
1204 XHCI_TRB_3_SLOT_SET(slot_id);
1206 trb.dwTrb3 = htole32(temp);
1208 return (xhci_do_command(sc, &trb, 100 /* ms */));
1212 xhci_cmd_set_address(struct xhci_softc *sc, uint64_t input_ctx,
1213 uint8_t bsr, uint8_t slot_id)
1215 struct xhci_trb trb;
1220 trb.qwTrb0 = htole64(input_ctx);
1222 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ADDRESS_DEVICE) |
1223 XHCI_TRB_3_SLOT_SET(slot_id);
1226 temp |= XHCI_TRB_3_BSR_BIT;
1228 trb.dwTrb3 = htole32(temp);
1230 return (xhci_do_command(sc, &trb, 500 /* ms */));
1234 xhci_set_address(struct usb_device *udev, struct mtx *mtx, uint16_t address)
1236 struct usb_page_search buf_inp;
1237 struct usb_page_search buf_dev;
1238 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
1239 struct xhci_hw_dev *hdev;
1240 struct xhci_dev_ctx *pdev;
1241 struct xhci_endpoint_ext *pepext;
1247 /* the root HUB case is not handled here */
1248 if (udev->parent_hub == NULL)
1249 return (USB_ERR_INVAL);
1251 index = udev->controller_slot_id;
1253 hdev = &sc->sc_hw.devs[index];
1260 switch (hdev->state) {
1261 case XHCI_ST_DEFAULT:
1262 case XHCI_ST_ENABLED:
1264 hdev->state = XHCI_ST_ENABLED;
1266 /* set configure mask to slot and EP0 */
1267 xhci_configure_mask(udev, 3, 0);
1269 /* configure input slot context structure */
1270 err = xhci_configure_device(udev);
1273 DPRINTF("Could not configure device\n");
1277 /* configure input endpoint context structure */
1278 switch (udev->speed) {
1280 case USB_SPEED_FULL:
1283 case USB_SPEED_HIGH:
1291 pepext = xhci_get_endpoint_ext(udev,
1292 &udev->ctrl_ep_desc);
1293 err = xhci_configure_endpoint(udev,
1294 &udev->ctrl_ep_desc, pepext->physaddr,
1295 0, 1, 1, 0, mps, mps);
1298 DPRINTF("Could not configure default endpoint\n");
1302 /* execute set address command */
1303 usbd_get_page(&hdev->input_pc, 0, &buf_inp);
1305 err = xhci_cmd_set_address(sc, buf_inp.physaddr,
1306 (address == 0), index);
1309 temp = le32toh(sc->sc_cmd_result[0]);
1310 if (address == 0 && sc->sc_port_route != NULL &&
1311 XHCI_TRB_2_ERROR_GET(temp) ==
1312 XHCI_TRB_ERROR_PARAMETER) {
1313 /* LynxPoint XHCI - ports are not switchable */
1314 /* Un-route all ports from the XHCI */
1315 sc->sc_port_route(sc->sc_bus.parent, 0, ~0);
1317 DPRINTF("Could not set address "
1318 "for slot %u.\n", index);
1323 /* update device address to new value */
1325 usbd_get_page(&hdev->device_pc, 0, &buf_dev);
1326 pdev = buf_dev.buffer;
1327 usb_pc_cpu_invalidate(&hdev->device_pc);
1329 temp = xhci_ctx_get_le32(sc, &pdev->ctx_slot.dwSctx3);
1330 udev->address = XHCI_SCTX_3_DEV_ADDR_GET(temp);
1332 /* update device state to new value */
1335 hdev->state = XHCI_ST_ADDRESSED;
1337 hdev->state = XHCI_ST_DEFAULT;
1341 DPRINTF("Wrong state for set address.\n");
1342 err = USB_ERR_IOERROR;
1345 XHCI_CMD_UNLOCK(sc);
1354 xhci_cmd_configure_ep(struct xhci_softc *sc, uint64_t input_ctx,
1355 uint8_t deconfigure, uint8_t slot_id)
1357 struct xhci_trb trb;
1362 trb.qwTrb0 = htole64(input_ctx);
1364 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_CONFIGURE_EP) |
1365 XHCI_TRB_3_SLOT_SET(slot_id);
1368 temp |= XHCI_TRB_3_DCEP_BIT;
1370 trb.dwTrb3 = htole32(temp);
1372 return (xhci_do_command(sc, &trb, 100 /* ms */));
1376 xhci_cmd_evaluate_ctx(struct xhci_softc *sc, uint64_t input_ctx,
1379 struct xhci_trb trb;
1384 trb.qwTrb0 = htole64(input_ctx);
1386 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVALUATE_CTX) |
1387 XHCI_TRB_3_SLOT_SET(slot_id);
1388 trb.dwTrb3 = htole32(temp);
1390 return (xhci_do_command(sc, &trb, 100 /* ms */));
1394 xhci_cmd_reset_ep(struct xhci_softc *sc, uint8_t preserve,
1395 uint8_t ep_id, uint8_t slot_id)
1397 struct xhci_trb trb;
1404 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_EP) |
1405 XHCI_TRB_3_SLOT_SET(slot_id) |
1406 XHCI_TRB_3_EP_SET(ep_id);
1409 temp |= XHCI_TRB_3_PRSV_BIT;
1411 trb.dwTrb3 = htole32(temp);
1413 return (xhci_do_command(sc, &trb, 100 /* ms */));
1417 xhci_cmd_set_tr_dequeue_ptr(struct xhci_softc *sc, uint64_t dequeue_ptr,
1418 uint16_t stream_id, uint8_t ep_id, uint8_t slot_id)
1420 struct xhci_trb trb;
1425 trb.qwTrb0 = htole64(dequeue_ptr);
1427 temp = XHCI_TRB_2_STREAM_SET(stream_id);
1428 trb.dwTrb2 = htole32(temp);
1430 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SET_TR_DEQUEUE) |
1431 XHCI_TRB_3_SLOT_SET(slot_id) |
1432 XHCI_TRB_3_EP_SET(ep_id);
1433 trb.dwTrb3 = htole32(temp);
1435 return (xhci_do_command(sc, &trb, 100 /* ms */));
1439 xhci_cmd_stop_ep(struct xhci_softc *sc, uint8_t suspend,
1440 uint8_t ep_id, uint8_t slot_id)
1442 struct xhci_trb trb;
1449 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STOP_EP) |
1450 XHCI_TRB_3_SLOT_SET(slot_id) |
1451 XHCI_TRB_3_EP_SET(ep_id);
1454 temp |= XHCI_TRB_3_SUSP_EP_BIT;
1456 trb.dwTrb3 = htole32(temp);
1458 return (xhci_do_command(sc, &trb, 100 /* ms */));
1462 xhci_cmd_reset_dev(struct xhci_softc *sc, uint8_t slot_id)
1464 struct xhci_trb trb;
1471 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_DEVICE) |
1472 XHCI_TRB_3_SLOT_SET(slot_id);
1474 trb.dwTrb3 = htole32(temp);
1476 return (xhci_do_command(sc, &trb, 100 /* ms */));
1479 /*------------------------------------------------------------------------*
1480 * xhci_interrupt - XHCI interrupt handler
1481 *------------------------------------------------------------------------*/
1483 xhci_interrupt(struct xhci_softc *sc)
1487 USB_BUS_LOCK(&sc->sc_bus);
1489 status = XREAD4(sc, oper, XHCI_USBSTS);
1493 /* acknowledge interrupts */
1495 XWRITE4(sc, oper, XHCI_USBSTS, status);
1497 DPRINTFN(16, "real interrupt (status=0x%08x)\n", status);
1499 if (status & XHCI_STS_EINT) {
1500 /* check for event(s) */
1501 xhci_interrupt_poll(sc);
1504 if (status & (XHCI_STS_PCD | XHCI_STS_HCH |
1505 XHCI_STS_HSE | XHCI_STS_HCE)) {
1507 if (status & XHCI_STS_PCD) {
1511 if (status & XHCI_STS_HCH) {
1512 printf("%s: host controller halted\n",
1516 if (status & XHCI_STS_HSE) {
1517 printf("%s: host system error\n",
1521 if (status & XHCI_STS_HCE) {
1522 printf("%s: host controller error\n",
1527 USB_BUS_UNLOCK(&sc->sc_bus);
1530 /*------------------------------------------------------------------------*
1531 * xhci_timeout - XHCI timeout handler
1532 *------------------------------------------------------------------------*/
1534 xhci_timeout(void *arg)
1536 struct usb_xfer *xfer = arg;
1538 DPRINTF("xfer=%p\n", xfer);
1540 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1542 /* transfer is transferred */
1543 xhci_device_done(xfer, USB_ERR_TIMEOUT);
1547 xhci_do_poll(struct usb_bus *bus)
1549 struct xhci_softc *sc = XHCI_BUS2SC(bus);
1551 USB_BUS_LOCK(&sc->sc_bus);
1552 xhci_interrupt_poll(sc);
1553 USB_BUS_UNLOCK(&sc->sc_bus);
1557 xhci_setup_generic_chain_sub(struct xhci_std_temp *temp)
1559 struct usb_page_search buf_res;
1561 struct xhci_td *td_next;
1562 struct xhci_td *td_alt_next;
1563 struct xhci_td *td_first;
1564 uint32_t buf_offset;
1569 uint8_t shortpkt_old;
1575 shortpkt_old = temp->shortpkt;
1576 len_old = temp->len;
1583 td_next = td_first = temp->td_next;
1587 if (temp->len == 0) {
1592 /* send a Zero Length Packet, ZLP, last */
1599 average = temp->average;
1601 if (temp->len < average) {
1602 if (temp->len % temp->max_packet_size) {
1605 average = temp->len;
1609 if (td_next == NULL)
1610 panic("%s: out of XHCI transfer descriptors!", __FUNCTION__);
1615 td_next = td->obj_next;
1617 /* check if we are pre-computing */
1621 /* update remaining length */
1623 temp->len -= average;
1627 /* fill out current TD */
1633 /* update remaining length */
1635 temp->len -= average;
1637 /* reset TRB index */
1641 if (temp->trb_type == XHCI_TRB_TYPE_SETUP_STAGE) {
1642 /* immediate data */
1647 td->td_trb[0].qwTrb0 = 0;
1649 usbd_copy_out(temp->pc, temp->offset + buf_offset,
1650 (uint8_t *)(uintptr_t)&td->td_trb[0].qwTrb0,
1653 dword = XHCI_TRB_2_BYTES_SET(8) |
1654 XHCI_TRB_2_TDSZ_SET(0) |
1655 XHCI_TRB_2_IRQ_SET(0);
1657 td->td_trb[0].dwTrb2 = htole32(dword);
1659 dword = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SETUP_STAGE) |
1660 XHCI_TRB_3_IDT_BIT | XHCI_TRB_3_CYCLE_BIT;
1663 if (td->td_trb[0].qwTrb0 &
1664 htole64(XHCI_TRB_0_WLENGTH_MASK)) {
1665 if (td->td_trb[0].qwTrb0 & htole64(1))
1666 dword |= XHCI_TRB_3_TRT_IN;
1668 dword |= XHCI_TRB_3_TRT_OUT;
1671 td->td_trb[0].dwTrb3 = htole32(dword);
1673 xhci_dump_trb(&td->td_trb[x]);
1681 /* fill out buffer pointers */
1684 memset(&buf_res, 0, sizeof(buf_res));
1686 usbd_get_page(temp->pc, temp->offset +
1687 buf_offset, &buf_res);
1689 /* get length to end of page */
1690 if (buf_res.length > average)
1691 buf_res.length = average;
1693 /* check for maximum length */
1694 if (buf_res.length > XHCI_TD_PAGE_SIZE)
1695 buf_res.length = XHCI_TD_PAGE_SIZE;
1697 npkt_off += buf_res.length;
1701 npkt = (len_old - npkt_off + temp->max_packet_size - 1) /
1702 temp->max_packet_size;
1709 /* fill out TRB's */
1710 td->td_trb[x].qwTrb0 =
1711 htole64((uint64_t)buf_res.physaddr);
1714 XHCI_TRB_2_BYTES_SET(buf_res.length) |
1715 XHCI_TRB_2_TDSZ_SET(npkt) |
1716 XHCI_TRB_2_IRQ_SET(0);
1718 td->td_trb[x].dwTrb2 = htole32(dword);
1720 switch (temp->trb_type) {
1721 case XHCI_TRB_TYPE_ISOCH:
1722 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1723 XHCI_TRB_3_TBC_SET(temp->tbc) |
1724 XHCI_TRB_3_TLBPC_SET(temp->tlbpc);
1725 if (td != td_first) {
1726 dword |= XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL);
1727 } else if (temp->do_isoc_sync != 0) {
1728 temp->do_isoc_sync = 0;
1729 /* wait until "isoc_frame" */
1730 dword |= XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ISOCH) |
1731 XHCI_TRB_3_FRID_SET(temp->isoc_frame / 8);
1733 /* start data transfer at next interval */
1734 dword |= XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ISOCH) |
1735 XHCI_TRB_3_ISO_SIA_BIT;
1737 if (temp->direction == UE_DIR_IN)
1738 dword |= XHCI_TRB_3_DIR_IN | XHCI_TRB_3_ISP_BIT;
1740 case XHCI_TRB_TYPE_DATA_STAGE:
1741 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1742 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DATA_STAGE) |
1743 XHCI_TRB_3_TBC_SET(temp->tbc) |
1744 XHCI_TRB_3_TLBPC_SET(temp->tlbpc);
1745 if (temp->direction == UE_DIR_IN)
1746 dword |= XHCI_TRB_3_DIR_IN | XHCI_TRB_3_ISP_BIT;
1748 case XHCI_TRB_TYPE_STATUS_STAGE:
1749 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1750 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STATUS_STAGE) |
1751 XHCI_TRB_3_TBC_SET(temp->tbc) |
1752 XHCI_TRB_3_TLBPC_SET(temp->tlbpc);
1753 if (temp->direction == UE_DIR_IN)
1754 dword |= XHCI_TRB_3_DIR_IN;
1756 default: /* XHCI_TRB_TYPE_NORMAL */
1757 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1758 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL) |
1759 XHCI_TRB_3_TBC_SET(temp->tbc) |
1760 XHCI_TRB_3_TLBPC_SET(temp->tlbpc);
1761 if (temp->direction == UE_DIR_IN)
1762 dword |= XHCI_TRB_3_DIR_IN | XHCI_TRB_3_ISP_BIT;
1765 td->td_trb[x].dwTrb3 = htole32(dword);
1767 average -= buf_res.length;
1768 buf_offset += buf_res.length;
1770 xhci_dump_trb(&td->td_trb[x]);
1774 } while (average != 0);
1776 td->td_trb[x-1].dwTrb3 |= htole32(XHCI_TRB_3_IOC_BIT);
1778 /* store number of data TRB's */
1782 DPRINTF("NTRB=%u\n", x);
1784 /* fill out link TRB */
1786 if (td_next != NULL) {
1787 /* link the current TD with the next one */
1788 td->td_trb[x].qwTrb0 = htole64((uint64_t)td_next->td_self);
1789 DPRINTF("LINK=0x%08llx\n", (long long)td_next->td_self);
1791 /* this field will get updated later */
1792 DPRINTF("NOLINK\n");
1795 dword = XHCI_TRB_2_IRQ_SET(0);
1797 td->td_trb[x].dwTrb2 = htole32(dword);
1799 dword = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) |
1800 XHCI_TRB_3_CYCLE_BIT | XHCI_TRB_3_IOC_BIT;
1802 td->td_trb[x].dwTrb3 = htole32(dword);
1804 td->alt_next = td_alt_next;
1806 xhci_dump_trb(&td->td_trb[x]);
1808 usb_pc_cpu_flush(td->page_cache);
1814 /* setup alt next pointer, if any */
1815 if (temp->last_frame) {
1818 /* we use this field internally */
1819 td_alt_next = td_next;
1823 temp->shortpkt = shortpkt_old;
1824 temp->len = len_old;
1829 * Remove cycle bit from the first TRB if we are
1832 if (temp->step_td != 0) {
1833 td_first->td_trb[0].dwTrb3 &= ~htole32(XHCI_TRB_3_CYCLE_BIT);
1834 usb_pc_cpu_flush(td_first->page_cache);
1837 /* clear TD SIZE to zero, hence this is the last TRB */
1838 /* remove chain bit because this is the last TRB in the chain */
1839 td->td_trb[td->ntrb - 1].dwTrb2 &= ~htole32(XHCI_TRB_2_TDSZ_SET(15));
1840 td->td_trb[td->ntrb - 1].dwTrb3 &= ~htole32(XHCI_TRB_3_CHAIN_BIT);
1842 usb_pc_cpu_flush(td->page_cache);
1845 temp->td_next = td_next;
1849 xhci_setup_generic_chain(struct usb_xfer *xfer)
1851 struct xhci_std_temp temp;
1857 temp.do_isoc_sync = 0;
1861 temp.average = xfer->max_hc_frame_size;
1862 temp.max_packet_size = xfer->max_packet_size;
1863 temp.sc = XHCI_BUS2SC(xfer->xroot->bus);
1865 temp.last_frame = 0;
1867 temp.multishort = xfer->flags_int.isochronous_xfr ||
1868 xfer->flags_int.control_xfr ||
1869 xfer->flags_int.short_frames_ok;
1871 /* toggle the DMA set we are using */
1872 xfer->flags_int.curr_dma_set ^= 1;
1874 /* get next DMA set */
1875 td = xfer->td_start[xfer->flags_int.curr_dma_set];
1880 xfer->td_transfer_first = td;
1881 xfer->td_transfer_cache = td;
1883 if (xfer->flags_int.isochronous_xfr) {
1886 /* compute multiplier for ISOCHRONOUS transfers */
1887 mult = xfer->endpoint->ecomp ?
1888 (xfer->endpoint->ecomp->bmAttributes & 3) : 0;
1889 /* check for USB 2.0 multiplier */
1891 mult = (xfer->endpoint->edesc->
1892 wMaxPacketSize[1] >> 3) & 3;
1900 x = XREAD4(temp.sc, runt, XHCI_MFINDEX);
1902 DPRINTF("MFINDEX=0x%08x\n", x);
1904 switch (usbd_get_speed(xfer->xroot->udev)) {
1905 case USB_SPEED_FULL:
1907 temp.isoc_delta = 8; /* 1ms */
1908 x += temp.isoc_delta - 1;
1909 x &= ~(temp.isoc_delta - 1);
1912 shift = usbd_xfer_get_fps_shift(xfer);
1913 temp.isoc_delta = 1U << shift;
1914 x += temp.isoc_delta - 1;
1915 x &= ~(temp.isoc_delta - 1);
1916 /* simple frame load balancing */
1917 x += xfer->endpoint->usb_uframe;
1921 y = XHCI_MFINDEX_GET(x - xfer->endpoint->isoc_next);
1923 if ((xfer->endpoint->is_synced == 0) ||
1924 (y < (xfer->nframes << shift)) ||
1925 (XHCI_MFINDEX_GET(-y) >= (128 * 8))) {
1927 * If there is data underflow or the pipe
1928 * queue is empty we schedule the transfer a
1929 * few frames ahead of the current frame
1930 * position. Else two isochronous transfers
1933 xfer->endpoint->isoc_next = XHCI_MFINDEX_GET(x + (3 * 8));
1934 xfer->endpoint->is_synced = 1;
1935 temp.do_isoc_sync = 1;
1937 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
1940 /* compute isochronous completion time */
1942 y = XHCI_MFINDEX_GET(xfer->endpoint->isoc_next - (x & ~7));
1944 xfer->isoc_time_complete =
1945 usb_isoc_time_expand(&temp.sc->sc_bus, x / 8) +
1946 (y / 8) + (((xfer->nframes << shift) + 7) / 8);
1949 temp.isoc_frame = xfer->endpoint->isoc_next;
1950 temp.trb_type = XHCI_TRB_TYPE_ISOCH;
1952 xfer->endpoint->isoc_next += xfer->nframes << shift;
1954 } else if (xfer->flags_int.control_xfr) {
1956 /* check if we should prepend a setup message */
1958 if (xfer->flags_int.control_hdr) {
1960 temp.len = xfer->frlengths[0];
1961 temp.pc = xfer->frbuffers + 0;
1962 temp.shortpkt = temp.len ? 1 : 0;
1963 temp.trb_type = XHCI_TRB_TYPE_SETUP_STAGE;
1966 /* check for last frame */
1967 if (xfer->nframes == 1) {
1968 /* no STATUS stage yet, SETUP is last */
1969 if (xfer->flags_int.control_act)
1970 temp.last_frame = 1;
1973 xhci_setup_generic_chain_sub(&temp);
1977 temp.isoc_delta = 0;
1978 temp.isoc_frame = 0;
1979 temp.trb_type = XHCI_TRB_TYPE_DATA_STAGE;
1983 temp.isoc_delta = 0;
1984 temp.isoc_frame = 0;
1985 temp.trb_type = XHCI_TRB_TYPE_NORMAL;
1988 if (x != xfer->nframes) {
1989 /* setup page_cache pointer */
1990 temp.pc = xfer->frbuffers + x;
1991 /* set endpoint direction */
1992 temp.direction = UE_GET_DIR(xfer->endpointno);
1995 while (x != xfer->nframes) {
1997 /* DATA0 / DATA1 message */
1999 temp.len = xfer->frlengths[x];
2000 temp.step_td = ((xfer->endpointno & UE_DIR_IN) &&
2001 x != 0 && temp.multishort == 0);
2005 if (x == xfer->nframes) {
2006 if (xfer->flags_int.control_xfr) {
2007 /* no STATUS stage yet, DATA is last */
2008 if (xfer->flags_int.control_act)
2009 temp.last_frame = 1;
2011 temp.last_frame = 1;
2014 if (temp.len == 0) {
2016 /* make sure that we send an USB packet */
2021 temp.tlbpc = mult - 1;
2023 } else if (xfer->flags_int.isochronous_xfr) {
2028 * Isochronous transfers don't have short
2029 * packet termination:
2034 /* isochronous transfers have a transfer limit */
2036 if (temp.len > xfer->max_frame_size)
2037 temp.len = xfer->max_frame_size;
2039 /* compute TD packet count */
2040 tdpc = (temp.len + xfer->max_packet_size - 1) /
2041 xfer->max_packet_size;
2043 temp.tbc = ((tdpc + mult - 1) / mult) - 1;
2044 temp.tlbpc = (tdpc % mult);
2046 if (temp.tlbpc == 0)
2047 temp.tlbpc = mult - 1;
2052 /* regular data transfer */
2054 temp.shortpkt = xfer->flags.force_short_xfer ? 0 : 1;
2057 xhci_setup_generic_chain_sub(&temp);
2059 if (xfer->flags_int.isochronous_xfr) {
2060 temp.offset += xfer->frlengths[x - 1];
2061 temp.isoc_frame += temp.isoc_delta;
2063 /* get next Page Cache pointer */
2064 temp.pc = xfer->frbuffers + x;
2068 /* check if we should append a status stage */
2070 if (xfer->flags_int.control_xfr &&
2071 !xfer->flags_int.control_act) {
2074 * Send a DATA1 message and invert the current
2075 * endpoint direction.
2077 temp.step_td = (xfer->nframes != 0);
2078 temp.direction = UE_GET_DIR(xfer->endpointno) ^ UE_DIR_IN;
2082 temp.last_frame = 1;
2083 temp.trb_type = XHCI_TRB_TYPE_STATUS_STAGE;
2085 xhci_setup_generic_chain_sub(&temp);
2090 /* must have at least one frame! */
2092 xfer->td_transfer_last = td;
2094 DPRINTF("first=%p last=%p\n", xfer->td_transfer_first, td);
2098 xhci_set_slot_pointer(struct xhci_softc *sc, uint8_t index, uint64_t dev_addr)
2100 struct usb_page_search buf_res;
2101 struct xhci_dev_ctx_addr *pdctxa;
2103 usbd_get_page(&sc->sc_hw.ctx_pc, 0, &buf_res);
2105 pdctxa = buf_res.buffer;
2107 DPRINTF("addr[%u]=0x%016llx\n", index, (long long)dev_addr);
2109 pdctxa->qwBaaDevCtxAddr[index] = htole64(dev_addr);
2111 usb_pc_cpu_flush(&sc->sc_hw.ctx_pc);
2115 xhci_configure_mask(struct usb_device *udev, uint32_t mask, uint8_t drop)
2117 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2118 struct usb_page_search buf_inp;
2119 struct xhci_input_dev_ctx *pinp;
2124 index = udev->controller_slot_id;
2126 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
2128 pinp = buf_inp.buffer;
2131 mask &= XHCI_INCTX_NON_CTRL_MASK;
2132 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx0, mask);
2133 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx1, 0);
2135 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx0, 0);
2136 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx1, mask);
2138 /* find most significant set bit */
2139 for (x = 31; x != 1; x--) {
2140 if (mask & (1 << x))
2147 /* figure out maximum */
2148 if (x > sc->sc_hw.devs[index].context_num) {
2149 sc->sc_hw.devs[index].context_num = x;
2150 temp = xhci_ctx_get_le32(sc, &pinp->ctx_slot.dwSctx0);
2151 temp &= ~XHCI_SCTX_0_CTX_NUM_SET(31);
2152 temp |= XHCI_SCTX_0_CTX_NUM_SET(x + 1);
2153 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx0, temp);
2160 xhci_configure_endpoint(struct usb_device *udev,
2161 struct usb_endpoint_descriptor *edesc, uint64_t ring_addr,
2162 uint16_t interval, uint8_t max_packet_count, uint8_t mult,
2163 uint8_t fps_shift, uint16_t max_packet_size, uint16_t max_frame_size)
2165 struct usb_page_search buf_inp;
2166 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2167 struct xhci_input_dev_ctx *pinp;
2173 index = udev->controller_slot_id;
2175 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
2177 pinp = buf_inp.buffer;
2179 epno = edesc->bEndpointAddress;
2180 type = edesc->bmAttributes & UE_XFERTYPE;
2182 if (type == UE_CONTROL)
2185 epno = XHCI_EPNO2EPID(epno);
2188 return (USB_ERR_NO_PIPE); /* invalid */
2190 if (max_packet_count == 0)
2191 return (USB_ERR_BAD_BUFSIZE);
2196 return (USB_ERR_BAD_BUFSIZE);
2198 temp = XHCI_EPCTX_0_EPSTATE_SET(0) |
2199 XHCI_EPCTX_0_MAXP_STREAMS_SET(0) |
2200 XHCI_EPCTX_0_LSA_SET(0);
2202 switch (udev->speed) {
2203 case USB_SPEED_FULL:
2216 temp |= XHCI_EPCTX_0_IVAL_SET(fps_shift);
2218 case UE_ISOCHRONOUS:
2219 temp |= XHCI_EPCTX_0_IVAL_SET(fps_shift);
2221 switch (udev->speed) {
2222 case USB_SPEED_SUPER:
2225 temp |= XHCI_EPCTX_0_MULT_SET(mult - 1);
2226 max_packet_count /= mult;
2236 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx0, temp);
2239 XHCI_EPCTX_1_HID_SET(0) |
2240 XHCI_EPCTX_1_MAXB_SET(max_packet_count) |
2241 XHCI_EPCTX_1_MAXP_SIZE_SET(max_packet_size);
2243 if ((udev->parent_hs_hub != NULL) || (udev->address != 0)) {
2244 if (type != UE_ISOCHRONOUS)
2245 temp |= XHCI_EPCTX_1_CERR_SET(3);
2250 temp |= XHCI_EPCTX_1_EPTYPE_SET(4);
2252 case UE_ISOCHRONOUS:
2253 temp |= XHCI_EPCTX_1_EPTYPE_SET(1);
2256 temp |= XHCI_EPCTX_1_EPTYPE_SET(2);
2259 temp |= XHCI_EPCTX_1_EPTYPE_SET(3);
2263 /* check for IN direction */
2265 temp |= XHCI_EPCTX_1_EPTYPE_SET(4);
2267 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx1, temp);
2269 ring_addr |= XHCI_EPCTX_2_DCS_SET(1);
2271 xhci_ctx_set_le64(sc, &pinp->ctx_ep[epno - 1].qwEpCtx2, ring_addr);
2273 switch (edesc->bmAttributes & UE_XFERTYPE) {
2275 case UE_ISOCHRONOUS:
2276 temp = XHCI_EPCTX_4_MAX_ESIT_PAYLOAD_SET(max_frame_size) |
2277 XHCI_EPCTX_4_AVG_TRB_LEN_SET(MIN(XHCI_PAGE_SIZE,
2281 temp = XHCI_EPCTX_4_AVG_TRB_LEN_SET(8);
2284 temp = XHCI_EPCTX_4_AVG_TRB_LEN_SET(XHCI_PAGE_SIZE);
2288 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx4, temp);
2291 xhci_dump_endpoint(sc, &pinp->ctx_ep[epno - 1]);
2293 usb_pc_cpu_flush(&sc->sc_hw.devs[index].input_pc);
2295 return (0); /* success */
2299 xhci_configure_endpoint_by_xfer(struct usb_xfer *xfer)
2301 struct xhci_endpoint_ext *pepext;
2302 struct usb_endpoint_ss_comp_descriptor *ecomp;
2304 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2305 xfer->endpoint->edesc);
2307 ecomp = xfer->endpoint->ecomp;
2309 pepext->trb[0].dwTrb3 = 0; /* halt any transfers */
2310 usb_pc_cpu_flush(pepext->page_cache);
2312 return (xhci_configure_endpoint(xfer->xroot->udev,
2313 xfer->endpoint->edesc, pepext->physaddr,
2314 xfer->interval, xfer->max_packet_count,
2315 (ecomp != NULL) ? (ecomp->bmAttributes & 3) + 1 : 1,
2316 usbd_xfer_get_fps_shift(xfer), xfer->max_packet_size,
2317 xfer->max_frame_size));
2321 xhci_configure_device(struct usb_device *udev)
2323 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2324 struct usb_page_search buf_inp;
2325 struct usb_page_cache *pcinp;
2326 struct xhci_input_dev_ctx *pinp;
2327 struct usb_device *hubdev;
2335 index = udev->controller_slot_id;
2337 DPRINTF("index=%u\n", index);
2339 pcinp = &sc->sc_hw.devs[index].input_pc;
2341 usbd_get_page(pcinp, 0, &buf_inp);
2343 pinp = buf_inp.buffer;
2348 /* figure out route string and root HUB port number */
2350 for (hubdev = udev; hubdev != NULL; hubdev = hubdev->parent_hub) {
2352 if (hubdev->parent_hub == NULL)
2355 depth = hubdev->parent_hub->depth;
2358 * NOTE: HS/FS/LS devices and the SS root HUB can have
2359 * more than 15 ports
2362 rh_port = hubdev->port_no;
2371 route |= rh_port << (4 * (depth - 1));
2374 DPRINTF("Route=0x%08x\n", route);
2376 temp = XHCI_SCTX_0_ROUTE_SET(route) |
2377 XHCI_SCTX_0_CTX_NUM_SET(
2378 sc->sc_hw.devs[index].context_num + 1);
2380 switch (udev->speed) {
2382 temp |= XHCI_SCTX_0_SPEED_SET(2);
2383 if (udev->parent_hs_hub != NULL &&
2384 udev->parent_hs_hub->ddesc.bDeviceProtocol ==
2386 DPRINTF("Device inherits MTT\n");
2387 temp |= XHCI_SCTX_0_MTT_SET(1);
2390 case USB_SPEED_HIGH:
2391 temp |= XHCI_SCTX_0_SPEED_SET(3);
2392 if (sc->sc_hw.devs[index].nports != 0 &&
2393 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) {
2394 DPRINTF("HUB supports MTT\n");
2395 temp |= XHCI_SCTX_0_MTT_SET(1);
2398 case USB_SPEED_FULL:
2399 temp |= XHCI_SCTX_0_SPEED_SET(1);
2400 if (udev->parent_hs_hub != NULL &&
2401 udev->parent_hs_hub->ddesc.bDeviceProtocol ==
2403 DPRINTF("Device inherits MTT\n");
2404 temp |= XHCI_SCTX_0_MTT_SET(1);
2408 temp |= XHCI_SCTX_0_SPEED_SET(4);
2412 is_hub = sc->sc_hw.devs[index].nports != 0 &&
2413 (udev->speed == USB_SPEED_SUPER ||
2414 udev->speed == USB_SPEED_HIGH);
2417 temp |= XHCI_SCTX_0_HUB_SET(1);
2419 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx0, temp);
2421 temp = XHCI_SCTX_1_RH_PORT_SET(rh_port);
2424 temp |= XHCI_SCTX_1_NUM_PORTS_SET(
2425 sc->sc_hw.devs[index].nports);
2428 switch (udev->speed) {
2429 case USB_SPEED_SUPER:
2430 switch (sc->sc_hw.devs[index].state) {
2431 case XHCI_ST_ADDRESSED:
2432 case XHCI_ST_CONFIGURED:
2433 /* enable power save */
2434 temp |= XHCI_SCTX_1_MAX_EL_SET(sc->sc_exit_lat_max);
2437 /* disable power save */
2445 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx1, temp);
2447 temp = XHCI_SCTX_2_IRQ_TARGET_SET(0);
2450 temp |= XHCI_SCTX_2_TT_THINK_TIME_SET(
2451 sc->sc_hw.devs[index].tt);
2454 hubdev = udev->parent_hs_hub;
2456 /* check if we should activate the transaction translator */
2457 switch (udev->speed) {
2458 case USB_SPEED_FULL:
2460 if (hubdev != NULL) {
2461 temp |= XHCI_SCTX_2_TT_HUB_SID_SET(
2462 hubdev->controller_slot_id);
2463 temp |= XHCI_SCTX_2_TT_PORT_NUM_SET(
2471 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx2, temp);
2473 temp = XHCI_SCTX_3_DEV_ADDR_SET(udev->address) |
2474 XHCI_SCTX_3_SLOT_STATE_SET(0);
2476 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx3, temp);
2479 xhci_dump_device(sc, &pinp->ctx_slot);
2481 usb_pc_cpu_flush(pcinp);
2483 return (0); /* success */
2487 xhci_alloc_device_ext(struct usb_device *udev)
2489 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2490 struct usb_page_search buf_dev;
2491 struct usb_page_search buf_ep;
2492 struct xhci_trb *trb;
2493 struct usb_page_cache *pc;
2494 struct usb_page *pg;
2499 index = udev->controller_slot_id;
2501 pc = &sc->sc_hw.devs[index].device_pc;
2502 pg = &sc->sc_hw.devs[index].device_pg;
2504 /* need to initialize the page cache */
2505 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2507 if (usb_pc_alloc_mem(pc, pg, sc->sc_ctx_is_64_byte ?
2508 (2 * sizeof(struct xhci_dev_ctx)) :
2509 sizeof(struct xhci_dev_ctx), XHCI_PAGE_SIZE))
2512 usbd_get_page(pc, 0, &buf_dev);
2514 pc = &sc->sc_hw.devs[index].input_pc;
2515 pg = &sc->sc_hw.devs[index].input_pg;
2517 /* need to initialize the page cache */
2518 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2520 if (usb_pc_alloc_mem(pc, pg, sc->sc_ctx_is_64_byte ?
2521 (2 * sizeof(struct xhci_input_dev_ctx)) :
2522 sizeof(struct xhci_input_dev_ctx), XHCI_PAGE_SIZE)) {
2526 pc = &sc->sc_hw.devs[index].endpoint_pc;
2527 pg = &sc->sc_hw.devs[index].endpoint_pg;
2529 /* need to initialize the page cache */
2530 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2532 if (usb_pc_alloc_mem(pc, pg,
2533 sizeof(struct xhci_dev_endpoint_trbs), XHCI_PAGE_SIZE)) {
2537 /* initialise all endpoint LINK TRBs */
2539 for (i = 0; i != XHCI_MAX_ENDPOINTS; i++) {
2541 /* lookup endpoint TRB ring */
2542 usbd_get_page(pc, (uintptr_t)&
2543 ((struct xhci_dev_endpoint_trbs *)0)->trb[i][0], &buf_ep);
2545 /* get TRB pointer */
2546 trb = buf_ep.buffer;
2547 trb += XHCI_MAX_TRANSFERS - 1;
2549 /* get TRB start address */
2550 addr = buf_ep.physaddr;
2552 /* create LINK TRB */
2553 trb->qwTrb0 = htole64(addr);
2554 trb->dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
2555 trb->dwTrb3 = htole32(XHCI_TRB_3_CYCLE_BIT |
2556 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
2559 usb_pc_cpu_flush(pc);
2561 xhci_set_slot_pointer(sc, index, buf_dev.physaddr);
2566 xhci_free_device_ext(udev);
2568 return (USB_ERR_NOMEM);
2572 xhci_free_device_ext(struct usb_device *udev)
2574 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2577 index = udev->controller_slot_id;
2578 xhci_set_slot_pointer(sc, index, 0);
2580 usb_pc_free_mem(&sc->sc_hw.devs[index].device_pc);
2581 usb_pc_free_mem(&sc->sc_hw.devs[index].input_pc);
2582 usb_pc_free_mem(&sc->sc_hw.devs[index].endpoint_pc);
2585 static struct xhci_endpoint_ext *
2586 xhci_get_endpoint_ext(struct usb_device *udev, struct usb_endpoint_descriptor *edesc)
2588 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2589 struct xhci_endpoint_ext *pepext;
2590 struct usb_page_cache *pc;
2591 struct usb_page_search buf_ep;
2595 epno = edesc->bEndpointAddress;
2596 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL)
2599 epno = XHCI_EPNO2EPID(epno);
2601 index = udev->controller_slot_id;
2603 pc = &sc->sc_hw.devs[index].endpoint_pc;
2605 usbd_get_page(pc, (uintptr_t)&((struct xhci_dev_endpoint_trbs *)0)->trb[epno][0], &buf_ep);
2607 pepext = &sc->sc_hw.devs[index].endp[epno];
2608 pepext->page_cache = pc;
2609 pepext->trb = buf_ep.buffer;
2610 pepext->physaddr = buf_ep.physaddr;
2616 xhci_endpoint_doorbell(struct usb_xfer *xfer)
2618 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
2622 epno = xfer->endpointno;
2623 if (xfer->flags_int.control_xfr)
2626 epno = XHCI_EPNO2EPID(epno);
2627 index = xfer->xroot->udev->controller_slot_id;
2629 if (xfer->xroot->udev->flags.self_suspended == 0) {
2630 XWRITE4(sc, door, XHCI_DOORBELL(index),
2631 epno | XHCI_DB_SID_SET(/*xfer->stream_id*/ 0));
2636 xhci_transfer_remove(struct usb_xfer *xfer, usb_error_t error)
2638 struct xhci_endpoint_ext *pepext;
2640 if (xfer->flags_int.bandwidth_reclaimed) {
2641 xfer->flags_int.bandwidth_reclaimed = 0;
2643 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2644 xfer->endpoint->edesc);
2648 pepext->xfer[xfer->qh_pos] = NULL;
2650 if (error && pepext->trb_running != 0) {
2651 pepext->trb_halted = 1;
2652 pepext->trb_running = 0;
2658 xhci_transfer_insert(struct usb_xfer *xfer)
2660 struct xhci_td *td_first;
2661 struct xhci_td *td_last;
2662 struct xhci_trb *trb_link;
2663 struct xhci_endpoint_ext *pepext;
2671 /* check if already inserted */
2672 if (xfer->flags_int.bandwidth_reclaimed) {
2673 DPRINTFN(8, "Already in schedule\n");
2677 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2678 xfer->endpoint->edesc);
2680 td_first = xfer->td_transfer_first;
2681 td_last = xfer->td_transfer_last;
2682 addr = pepext->physaddr;
2684 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
2687 /* single buffered */
2691 /* multi buffered */
2692 trb_limit = (XHCI_MAX_TRANSFERS - 2);
2696 if (pepext->trb_used >= trb_limit) {
2697 DPRINTFN(8, "Too many TDs queued.\n");
2698 return (USB_ERR_NOMEM);
2701 /* check for stopped condition, after putting transfer on interrupt queue */
2702 if (pepext->trb_running == 0) {
2703 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
2705 DPRINTFN(8, "Not running\n");
2707 /* start configuration */
2708 (void)usb_proc_msignal(&sc->sc_config_proc,
2709 &sc->sc_config_msg[0], &sc->sc_config_msg[1]);
2715 /* get current TRB index */
2716 i = pepext->trb_index;
2718 /* get next TRB index */
2721 /* the last entry of the ring is a hardcoded link TRB */
2722 if (inext >= (XHCI_MAX_TRANSFERS - 1))
2725 /* compute terminating return address */
2726 addr += inext * sizeof(struct xhci_trb);
2728 /* compute link TRB pointer */
2729 trb_link = td_last->td_trb + td_last->ntrb;
2731 /* update next pointer of last link TRB */
2732 trb_link->qwTrb0 = htole64(addr);
2733 trb_link->dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
2734 trb_link->dwTrb3 = htole32(XHCI_TRB_3_IOC_BIT |
2735 XHCI_TRB_3_CYCLE_BIT |
2736 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
2739 xhci_dump_trb(&td_last->td_trb[td_last->ntrb]);
2741 usb_pc_cpu_flush(td_last->page_cache);
2743 /* write ahead chain end marker */
2745 pepext->trb[inext].qwTrb0 = 0;
2746 pepext->trb[inext].dwTrb2 = 0;
2747 pepext->trb[inext].dwTrb3 = 0;
2749 /* update next pointer of link TRB */
2751 pepext->trb[i].qwTrb0 = htole64((uint64_t)td_first->td_self);
2752 pepext->trb[i].dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
2755 xhci_dump_trb(&pepext->trb[i]);
2757 usb_pc_cpu_flush(pepext->page_cache);
2759 /* toggle cycle bit which activates the transfer chain */
2761 pepext->trb[i].dwTrb3 = htole32(XHCI_TRB_3_CYCLE_BIT |
2762 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
2764 usb_pc_cpu_flush(pepext->page_cache);
2766 DPRINTF("qh_pos = %u\n", i);
2768 pepext->xfer[i] = xfer;
2772 xfer->flags_int.bandwidth_reclaimed = 1;
2774 pepext->trb_index = inext;
2776 xhci_endpoint_doorbell(xfer);
2782 xhci_root_intr(struct xhci_softc *sc)
2786 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2788 /* clear any old interrupt data */
2789 memset(sc->sc_hub_idata, 0, sizeof(sc->sc_hub_idata));
2791 for (i = 1; i <= sc->sc_noport; i++) {
2792 /* pick out CHANGE bits from the status register */
2793 if (XREAD4(sc, oper, XHCI_PORTSC(i)) & (
2794 XHCI_PS_CSC | XHCI_PS_PEC |
2795 XHCI_PS_OCC | XHCI_PS_WRC |
2796 XHCI_PS_PRC | XHCI_PS_PLC |
2798 sc->sc_hub_idata[i / 8] |= 1 << (i % 8);
2799 DPRINTF("port %d changed\n", i);
2802 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
2803 sizeof(sc->sc_hub_idata));
2806 /*------------------------------------------------------------------------*
2807 * xhci_device_done - XHCI done handler
2809 * NOTE: This function can be called two times in a row on
2810 * the same USB transfer. From close and from interrupt.
2811 *------------------------------------------------------------------------*/
2813 xhci_device_done(struct usb_xfer *xfer, usb_error_t error)
2815 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
2816 xfer, xfer->endpoint, error);
2818 /* remove transfer from HW queue */
2819 xhci_transfer_remove(xfer, error);
2821 /* dequeue transfer and start next transfer */
2822 usbd_transfer_done(xfer, error);
2825 /*------------------------------------------------------------------------*
2826 * XHCI data transfer support (generic type)
2827 *------------------------------------------------------------------------*/
2829 xhci_device_generic_open(struct usb_xfer *xfer)
2831 if (xfer->flags_int.isochronous_xfr) {
2832 switch (xfer->xroot->udev->speed) {
2833 case USB_SPEED_FULL:
2836 usb_hs_bandwidth_alloc(xfer);
2843 xhci_device_generic_close(struct usb_xfer *xfer)
2847 xhci_device_done(xfer, USB_ERR_CANCELLED);
2849 if (xfer->flags_int.isochronous_xfr) {
2850 switch (xfer->xroot->udev->speed) {
2851 case USB_SPEED_FULL:
2854 usb_hs_bandwidth_free(xfer);
2861 xhci_device_generic_multi_enter(struct usb_endpoint *ep,
2862 struct usb_xfer *enter_xfer)
2864 struct usb_xfer *xfer;
2866 /* check if there is a current transfer */
2867 xfer = ep->endpoint_q.curr;
2872 * Check if the current transfer is started and then pickup
2873 * the next one, if any. Else wait for next start event due to
2874 * block on failure feature.
2876 if (!xfer->flags_int.bandwidth_reclaimed)
2879 xfer = TAILQ_FIRST(&ep->endpoint_q.head);
2882 * In case of enter we have to consider that the
2883 * transfer is queued by the USB core after the enter
2892 /* try to multi buffer */
2893 xhci_transfer_insert(xfer);
2897 xhci_device_generic_enter(struct usb_xfer *xfer)
2901 /* setup TD's and QH */
2902 xhci_setup_generic_chain(xfer);
2904 xhci_device_generic_multi_enter(xfer->endpoint, xfer);
2908 xhci_device_generic_start(struct usb_xfer *xfer)
2912 /* try to insert xfer on HW queue */
2913 xhci_transfer_insert(xfer);
2915 /* try to multi buffer */
2916 xhci_device_generic_multi_enter(xfer->endpoint, NULL);
2918 /* add transfer last on interrupt queue */
2919 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
2921 /* start timeout, if any */
2922 if (xfer->timeout != 0)
2923 usbd_transfer_timeout_ms(xfer, &xhci_timeout, xfer->timeout);
2926 struct usb_pipe_methods xhci_device_generic_methods =
2928 .open = xhci_device_generic_open,
2929 .close = xhci_device_generic_close,
2930 .enter = xhci_device_generic_enter,
2931 .start = xhci_device_generic_start,
2934 /*------------------------------------------------------------------------*
2935 * xhci root HUB support
2936 *------------------------------------------------------------------------*
2937 * Simulate a hardware HUB by handling all the necessary requests.
2938 *------------------------------------------------------------------------*/
2940 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
2943 struct usb_device_descriptor xhci_devd =
2945 .bLength = sizeof(xhci_devd),
2946 .bDescriptorType = UDESC_DEVICE, /* type */
2947 HSETW(.bcdUSB, 0x0300), /* USB version */
2948 .bDeviceClass = UDCLASS_HUB, /* class */
2949 .bDeviceSubClass = UDSUBCLASS_HUB, /* subclass */
2950 .bDeviceProtocol = UDPROTO_SSHUB, /* protocol */
2951 .bMaxPacketSize = 9, /* max packet size */
2952 HSETW(.idVendor, 0x0000), /* vendor */
2953 HSETW(.idProduct, 0x0000), /* product */
2954 HSETW(.bcdDevice, 0x0100), /* device version */
2958 .bNumConfigurations = 1, /* # of configurations */
2962 struct xhci_bos_desc xhci_bosd = {
2964 .bLength = sizeof(xhci_bosd.bosd),
2965 .bDescriptorType = UDESC_BOS,
2966 HSETW(.wTotalLength, sizeof(xhci_bosd)),
2967 .bNumDeviceCaps = 3,
2970 .bLength = sizeof(xhci_bosd.usb2extd),
2971 .bDescriptorType = 1,
2972 .bDevCapabilityType = 2,
2973 .bmAttributes[0] = 2,
2976 .bLength = sizeof(xhci_bosd.usbdcd),
2977 .bDescriptorType = UDESC_DEVICE_CAPABILITY,
2978 .bDevCapabilityType = 3,
2979 .bmAttributes = 0, /* XXX */
2980 HSETW(.wSpeedsSupported, 0x000C),
2981 .bFunctionalitySupport = 8,
2982 .bU1DevExitLat = 255, /* dummy - not used */
2983 .wU2DevExitLat = { 0x00, 0x08 },
2986 .bLength = sizeof(xhci_bosd.cidd),
2987 .bDescriptorType = 1,
2988 .bDevCapabilityType = 4,
2990 .bContainerID = 0, /* XXX */
2995 struct xhci_config_desc xhci_confd = {
2997 .bLength = sizeof(xhci_confd.confd),
2998 .bDescriptorType = UDESC_CONFIG,
2999 .wTotalLength[0] = sizeof(xhci_confd),
3001 .bConfigurationValue = 1,
3002 .iConfiguration = 0,
3003 .bmAttributes = UC_SELF_POWERED,
3004 .bMaxPower = 0 /* max power */
3007 .bLength = sizeof(xhci_confd.ifcd),
3008 .bDescriptorType = UDESC_INTERFACE,
3010 .bInterfaceClass = UICLASS_HUB,
3011 .bInterfaceSubClass = UISUBCLASS_HUB,
3012 .bInterfaceProtocol = 0,
3015 .bLength = sizeof(xhci_confd.endpd),
3016 .bDescriptorType = UDESC_ENDPOINT,
3017 .bEndpointAddress = UE_DIR_IN | XHCI_INTR_ENDPT,
3018 .bmAttributes = UE_INTERRUPT,
3019 .wMaxPacketSize[0] = 2, /* max 15 ports */
3023 .bLength = sizeof(xhci_confd.endpcd),
3024 .bDescriptorType = UDESC_ENDPOINT_SS_COMP,
3031 struct usb_hub_ss_descriptor xhci_hubd = {
3032 .bLength = sizeof(xhci_hubd),
3033 .bDescriptorType = UDESC_SS_HUB,
3037 xhci_roothub_exec(struct usb_device *udev,
3038 struct usb_device_request *req, const void **pptr, uint16_t *plength)
3040 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3041 const char *str_ptr;
3052 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
3055 ptr = (const void *)&sc->sc_hub_desc;
3059 value = UGETW(req->wValue);
3060 index = UGETW(req->wIndex);
3062 DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
3063 "wValue=0x%04x wIndex=0x%04x\n",
3064 req->bmRequestType, req->bRequest,
3065 UGETW(req->wLength), value, index);
3067 #define C(x,y) ((x) | ((y) << 8))
3068 switch (C(req->bRequest, req->bmRequestType)) {
3069 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
3070 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
3071 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
3073 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
3074 * for the integrated root hub.
3077 case C(UR_GET_CONFIG, UT_READ_DEVICE):
3079 sc->sc_hub_desc.temp[0] = sc->sc_conf;
3081 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
3082 switch (value >> 8) {
3084 if ((value & 0xff) != 0) {
3085 err = USB_ERR_IOERROR;
3088 len = sizeof(xhci_devd);
3089 ptr = (const void *)&xhci_devd;
3093 if ((value & 0xff) != 0) {
3094 err = USB_ERR_IOERROR;
3097 len = sizeof(xhci_bosd);
3098 ptr = (const void *)&xhci_bosd;
3102 if ((value & 0xff) != 0) {
3103 err = USB_ERR_IOERROR;
3106 len = sizeof(xhci_confd);
3107 ptr = (const void *)&xhci_confd;
3111 switch (value & 0xff) {
3112 case 0: /* Language table */
3116 case 1: /* Vendor */
3117 str_ptr = sc->sc_vendor;
3120 case 2: /* Product */
3121 str_ptr = "XHCI root HUB";
3129 len = usb_make_str_desc(
3130 sc->sc_hub_desc.temp,
3131 sizeof(sc->sc_hub_desc.temp),
3136 err = USB_ERR_IOERROR;
3140 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3142 sc->sc_hub_desc.temp[0] = 0;
3144 case C(UR_GET_STATUS, UT_READ_DEVICE):
3146 USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
3148 case C(UR_GET_STATUS, UT_READ_INTERFACE):
3149 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3151 USETW(sc->sc_hub_desc.stat.wStatus, 0);
3153 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3154 if (value >= XHCI_MAX_DEVICES) {
3155 err = USB_ERR_IOERROR;
3159 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
3160 if (value != 0 && value != 1) {
3161 err = USB_ERR_IOERROR;
3164 sc->sc_conf = value;
3166 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
3168 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
3169 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
3170 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3171 err = USB_ERR_IOERROR;
3173 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
3175 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
3178 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3180 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3181 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE\n");
3184 (index > sc->sc_noport)) {
3185 err = USB_ERR_IOERROR;
3188 port = XHCI_PORTSC(index);
3190 v = XREAD4(sc, oper, port);
3191 i = XHCI_PS_PLS_GET(v);
3192 v &= ~XHCI_PS_CLEAR;
3195 case UHF_C_BH_PORT_RESET:
3196 XWRITE4(sc, oper, port, v | XHCI_PS_WRC);
3198 case UHF_C_PORT_CONFIG_ERROR:
3199 XWRITE4(sc, oper, port, v | XHCI_PS_CEC);
3201 case UHF_C_PORT_SUSPEND:
3202 case UHF_C_PORT_LINK_STATE:
3203 XWRITE4(sc, oper, port, v | XHCI_PS_PLC);
3205 case UHF_C_PORT_CONNECTION:
3206 XWRITE4(sc, oper, port, v | XHCI_PS_CSC);
3208 case UHF_C_PORT_ENABLE:
3209 XWRITE4(sc, oper, port, v | XHCI_PS_PEC);
3211 case UHF_C_PORT_OVER_CURRENT:
3212 XWRITE4(sc, oper, port, v | XHCI_PS_OCC);
3214 case UHF_C_PORT_RESET:
3215 XWRITE4(sc, oper, port, v | XHCI_PS_PRC);
3217 case UHF_PORT_ENABLE:
3218 XWRITE4(sc, oper, port, v | XHCI_PS_PED);
3220 case UHF_PORT_POWER:
3221 XWRITE4(sc, oper, port, v & ~XHCI_PS_PP);
3223 case UHF_PORT_INDICATOR:
3224 XWRITE4(sc, oper, port, v & ~XHCI_PS_PIC_SET(3));
3226 case UHF_PORT_SUSPEND:
3230 XWRITE4(sc, oper, port, v |
3231 XHCI_PS_PLS_SET(0xF) | XHCI_PS_LWS);
3234 /* wait 20ms for resume sequence to complete */
3235 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
3238 XWRITE4(sc, oper, port, v |
3239 XHCI_PS_PLS_SET(0) | XHCI_PS_LWS);
3242 err = USB_ERR_IOERROR;
3247 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3248 if ((value & 0xff) != 0) {
3249 err = USB_ERR_IOERROR;
3253 v = XREAD4(sc, capa, XHCI_HCSPARAMS0);
3255 sc->sc_hub_desc.hubd = xhci_hubd;
3257 sc->sc_hub_desc.hubd.bNbrPorts = sc->sc_noport;
3259 if (XHCI_HCS0_PPC(v))
3260 i = UHD_PWR_INDIVIDUAL;
3264 if (XHCI_HCS0_PIND(v))
3267 i |= UHD_OC_INDIVIDUAL;
3269 USETW(sc->sc_hub_desc.hubd.wHubCharacteristics, i);
3271 /* see XHCI section 5.4.9: */
3272 sc->sc_hub_desc.hubd.bPwrOn2PwrGood = 10;
3274 for (j = 1; j <= sc->sc_noport; j++) {
3276 v = XREAD4(sc, oper, XHCI_PORTSC(j));
3277 if (v & XHCI_PS_DR) {
3278 sc->sc_hub_desc.hubd.
3279 DeviceRemovable[j / 8] |= 1U << (j % 8);
3282 len = sc->sc_hub_desc.hubd.bLength;
3285 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3287 memset(sc->sc_hub_desc.temp, 0, 16);
3290 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3291 DPRINTFN(9, "UR_GET_STATUS i=%d\n", index);
3294 (index > sc->sc_noport)) {
3295 err = USB_ERR_IOERROR;
3299 v = XREAD4(sc, oper, XHCI_PORTSC(index));
3301 DPRINTFN(9, "port status=0x%08x\n", v);
3303 i = UPS_PORT_LINK_STATE_SET(XHCI_PS_PLS_GET(v));
3305 switch (XHCI_PS_SPEED_GET(v)) {
3307 i |= UPS_HIGH_SPEED;
3316 i |= UPS_OTHER_SPEED;
3320 if (v & XHCI_PS_CCS)
3321 i |= UPS_CURRENT_CONNECT_STATUS;
3322 if (v & XHCI_PS_PED)
3323 i |= UPS_PORT_ENABLED;
3324 if (v & XHCI_PS_OCA)
3325 i |= UPS_OVERCURRENT_INDICATOR;
3328 if (v & XHCI_PS_PP) {
3330 * The USB 3.0 RH is using the
3331 * USB 2.0's power bit
3333 i |= UPS_PORT_POWER;
3335 USETW(sc->sc_hub_desc.ps.wPortStatus, i);
3338 if (v & XHCI_PS_CSC)
3339 i |= UPS_C_CONNECT_STATUS;
3340 if (v & XHCI_PS_PEC)
3341 i |= UPS_C_PORT_ENABLED;
3342 if (v & XHCI_PS_OCC)
3343 i |= UPS_C_OVERCURRENT_INDICATOR;
3344 if (v & XHCI_PS_WRC)
3345 i |= UPS_C_BH_PORT_RESET;
3346 if (v & XHCI_PS_PRC)
3347 i |= UPS_C_PORT_RESET;
3348 if (v & XHCI_PS_PLC)
3349 i |= UPS_C_PORT_LINK_STATE;
3350 if (v & XHCI_PS_CEC)
3351 i |= UPS_C_PORT_CONFIG_ERROR;
3353 USETW(sc->sc_hub_desc.ps.wPortChange, i);
3354 len = sizeof(sc->sc_hub_desc.ps);
3357 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3358 err = USB_ERR_IOERROR;
3361 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3364 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3370 (index > sc->sc_noport)) {
3371 err = USB_ERR_IOERROR;
3375 port = XHCI_PORTSC(index);
3376 v = XREAD4(sc, oper, port) & ~XHCI_PS_CLEAR;
3379 case UHF_PORT_U1_TIMEOUT:
3380 if (XHCI_PS_SPEED_GET(v) != 4) {
3381 err = USB_ERR_IOERROR;
3384 port = XHCI_PORTPMSC(index);
3385 v = XREAD4(sc, oper, port);
3386 v &= ~XHCI_PM3_U1TO_SET(0xFF);
3387 v |= XHCI_PM3_U1TO_SET(i);
3388 XWRITE4(sc, oper, port, v);
3390 case UHF_PORT_U2_TIMEOUT:
3391 if (XHCI_PS_SPEED_GET(v) != 4) {
3392 err = USB_ERR_IOERROR;
3395 port = XHCI_PORTPMSC(index);
3396 v = XREAD4(sc, oper, port);
3397 v &= ~XHCI_PM3_U2TO_SET(0xFF);
3398 v |= XHCI_PM3_U2TO_SET(i);
3399 XWRITE4(sc, oper, port, v);
3401 case UHF_BH_PORT_RESET:
3402 XWRITE4(sc, oper, port, v | XHCI_PS_WPR);
3404 case UHF_PORT_LINK_STATE:
3405 XWRITE4(sc, oper, port, v |
3406 XHCI_PS_PLS_SET(i) | XHCI_PS_LWS);
3407 /* 4ms settle time */
3408 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
3410 case UHF_PORT_ENABLE:
3411 DPRINTFN(3, "set port enable %d\n", index);
3413 case UHF_PORT_SUSPEND:
3414 DPRINTFN(6, "suspend port %u (LPM=%u)\n", index, i);
3415 j = XHCI_PS_SPEED_GET(v);
3416 if ((j < 1) || (j > 3)) {
3417 /* non-supported speed */
3418 err = USB_ERR_IOERROR;
3421 XWRITE4(sc, oper, port, v |
3422 XHCI_PS_PLS_SET(i ? 2 /* LPM */ : 3) | XHCI_PS_LWS);
3424 case UHF_PORT_RESET:
3425 DPRINTFN(6, "reset port %d\n", index);
3426 XWRITE4(sc, oper, port, v | XHCI_PS_PR);
3428 case UHF_PORT_POWER:
3429 DPRINTFN(3, "set port power %d\n", index);
3430 XWRITE4(sc, oper, port, v | XHCI_PS_PP);
3433 DPRINTFN(3, "set port test %d\n", index);
3435 case UHF_PORT_INDICATOR:
3436 DPRINTFN(3, "set port indicator %d\n", index);
3438 v &= ~XHCI_PS_PIC_SET(3);
3439 v |= XHCI_PS_PIC_SET(1);
3441 XWRITE4(sc, oper, port, v);
3444 err = USB_ERR_IOERROR;
3449 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
3450 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
3451 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
3452 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
3455 err = USB_ERR_IOERROR;
3465 xhci_xfer_setup(struct usb_setup_params *parm)
3467 struct usb_page_search page_info;
3468 struct usb_page_cache *pc;
3469 struct xhci_softc *sc;
3470 struct usb_xfer *xfer;
3475 sc = XHCI_BUS2SC(parm->udev->bus);
3476 xfer = parm->curr_xfer;
3479 * The proof for the "ntd" formula is illustrated like this:
3481 * +------------------------------------+
3485 * | | xxx | x | frm 0 |
3487 * | | xxx | xx | frm 1 |
3490 * +------------------------------------+
3492 * "xxx" means a completely full USB transfer descriptor
3494 * "x" and "xx" means a short USB packet
3496 * For the remainder of an USB transfer modulo
3497 * "max_data_length" we need two USB transfer descriptors.
3498 * One to transfer the remaining data and one to finalise with
3499 * a zero length packet in case the "force_short_xfer" flag is
3500 * set. We only need two USB transfer descriptors in the case
3501 * where the transfer length of the first one is a factor of
3502 * "max_frame_size". The rest of the needed USB transfer
3503 * descriptors is given by the buffer size divided by the
3504 * maximum data payload.
3506 parm->hc_max_packet_size = 0x400;
3507 parm->hc_max_packet_count = 16 * 3;
3508 parm->hc_max_frame_size = XHCI_TD_PAYLOAD_MAX;
3510 xfer->flags_int.bdma_enable = 1;
3512 usbd_transfer_setup_sub(parm);
3514 if (xfer->flags_int.isochronous_xfr) {
3515 ntd = ((1 * xfer->nframes)
3516 + (xfer->max_data_length / xfer->max_hc_frame_size));
3517 } else if (xfer->flags_int.control_xfr) {
3518 ntd = ((2 * xfer->nframes) + 1 /* STATUS */
3519 + (xfer->max_data_length / xfer->max_hc_frame_size));
3521 ntd = ((2 * xfer->nframes)
3522 + (xfer->max_data_length / xfer->max_hc_frame_size));
3531 * Allocate queue heads and transfer descriptors
3535 if (usbd_transfer_setup_sub_malloc(
3536 parm, &pc, sizeof(struct xhci_td),
3537 XHCI_TD_ALIGN, ntd)) {
3538 parm->err = USB_ERR_NOMEM;
3542 for (n = 0; n != ntd; n++) {
3545 usbd_get_page(pc + n, 0, &page_info);
3547 td = page_info.buffer;
3550 td->td_self = page_info.physaddr;
3551 td->obj_next = last_obj;
3552 td->page_cache = pc + n;
3556 usb_pc_cpu_flush(pc + n);
3559 xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
3561 if (!xfer->flags_int.curr_dma_set) {
3562 xfer->flags_int.curr_dma_set = 1;
3568 xhci_configure_reset_endpoint(struct usb_xfer *xfer)
3570 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
3571 struct usb_page_search buf_inp;
3572 struct usb_device *udev;
3573 struct xhci_endpoint_ext *pepext;
3574 struct usb_endpoint_descriptor *edesc;
3575 struct usb_page_cache *pcinp;
3580 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
3581 xfer->endpoint->edesc);
3583 udev = xfer->xroot->udev;
3584 index = udev->controller_slot_id;
3586 pcinp = &sc->sc_hw.devs[index].input_pc;
3588 usbd_get_page(pcinp, 0, &buf_inp);
3590 edesc = xfer->endpoint->edesc;
3592 epno = edesc->bEndpointAddress;
3594 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL)
3597 epno = XHCI_EPNO2EPID(epno);
3600 return (USB_ERR_NO_PIPE); /* invalid */
3604 /* configure endpoint */
3606 err = xhci_configure_endpoint_by_xfer(xfer);
3609 XHCI_CMD_UNLOCK(sc);
3614 * Get the endpoint into the stopped state according to the
3615 * endpoint context state diagram in the XHCI specification:
3618 err = xhci_cmd_stop_ep(sc, 0, epno, index);
3621 DPRINTF("Could not stop endpoint %u\n", epno);
3623 err = xhci_cmd_reset_ep(sc, 0, epno, index);
3626 DPRINTF("Could not reset endpoint %u\n", epno);
3628 err = xhci_cmd_set_tr_dequeue_ptr(sc, pepext->physaddr |
3629 XHCI_EPCTX_2_DCS_SET(1), 0, epno, index);
3632 DPRINTF("Could not set dequeue ptr for endpoint %u\n", epno);
3635 * Get the endpoint into the running state according to the
3636 * endpoint context state diagram in the XHCI specification:
3639 xhci_configure_mask(udev, (1U << epno) | 1U, 0);
3641 err = xhci_cmd_evaluate_ctx(sc, buf_inp.physaddr, index);
3644 DPRINTF("Could not configure endpoint %u\n", epno);
3646 err = xhci_cmd_configure_ep(sc, buf_inp.physaddr, 0, index);
3649 DPRINTF("Could not configure endpoint %u\n", epno);
3651 XHCI_CMD_UNLOCK(sc);
3657 xhci_xfer_unsetup(struct usb_xfer *xfer)
3663 xhci_start_dma_delay(struct usb_xfer *xfer)
3665 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
3667 /* put transfer on interrupt queue (again) */
3668 usbd_transfer_enqueue(&sc->sc_bus.intr_q, xfer);
3670 (void)usb_proc_msignal(&sc->sc_config_proc,
3671 &sc->sc_config_msg[0], &sc->sc_config_msg[1]);
3675 xhci_configure_msg(struct usb_proc_msg *pm)
3677 struct xhci_softc *sc;
3678 struct xhci_endpoint_ext *pepext;
3679 struct usb_xfer *xfer;
3681 sc = XHCI_BUS2SC(((struct usb_bus_msg *)pm)->bus);
3684 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3686 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
3687 xfer->endpoint->edesc);
3689 if ((pepext->trb_halted != 0) ||
3690 (pepext->trb_running == 0)) {
3694 /* clear halted and running */
3695 pepext->trb_halted = 0;
3696 pepext->trb_running = 0;
3698 /* nuke remaining buffered transfers */
3700 for (i = 0; i != (XHCI_MAX_TRANSFERS - 1); i++) {
3702 * NOTE: We need to use the timeout
3703 * error code here else existing
3704 * isochronous clients can get
3707 if (pepext->xfer[i] != NULL) {
3708 xhci_device_done(pepext->xfer[i],
3714 * NOTE: The USB transfer cannot vanish in
3718 USB_BUS_UNLOCK(&sc->sc_bus);
3720 xhci_configure_reset_endpoint(xfer);
3722 USB_BUS_LOCK(&sc->sc_bus);
3724 /* check if halted is still cleared */
3725 if (pepext->trb_halted == 0) {
3726 pepext->trb_running = 1;
3727 pepext->trb_index = 0;
3732 if (xfer->flags_int.did_dma_delay) {
3734 /* remove transfer from interrupt queue (again) */
3735 usbd_transfer_dequeue(xfer);
3737 /* we are finally done */
3738 usb_dma_delay_done_cb(xfer);
3740 /* queue changed - restart */
3745 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3747 /* try to insert xfer on HW queue */
3748 xhci_transfer_insert(xfer);
3750 /* try to multi buffer */
3751 xhci_device_generic_multi_enter(xfer->endpoint, NULL);
3756 xhci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
3757 struct usb_endpoint *ep)
3759 struct xhci_endpoint_ext *pepext;
3761 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d\n",
3762 ep, udev->address, edesc->bEndpointAddress, udev->flags.usb_mode);
3764 if (udev->flags.usb_mode != USB_MODE_HOST) {
3768 if (udev->parent_hub == NULL) {
3769 /* root HUB has special endpoint handling */
3773 ep->methods = &xhci_device_generic_methods;
3775 pepext = xhci_get_endpoint_ext(udev, edesc);
3777 USB_BUS_LOCK(udev->bus);
3778 pepext->trb_halted = 1;
3779 pepext->trb_running = 0;
3780 USB_BUS_UNLOCK(udev->bus);
3784 xhci_ep_uninit(struct usb_device *udev, struct usb_endpoint *ep)
3790 xhci_ep_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
3792 struct xhci_endpoint_ext *pepext;
3796 if (udev->flags.usb_mode != USB_MODE_HOST) {
3800 if (udev->parent_hub == NULL) {
3801 /* root HUB has special endpoint handling */
3805 pepext = xhci_get_endpoint_ext(udev, ep->edesc);
3807 USB_BUS_LOCK(udev->bus);
3808 pepext->trb_halted = 1;
3809 pepext->trb_running = 0;
3810 USB_BUS_UNLOCK(udev->bus);
3814 xhci_device_init(struct usb_device *udev)
3816 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3820 /* no init for root HUB */
3821 if (udev->parent_hub == NULL)
3826 /* set invalid default */
3828 udev->controller_slot_id = sc->sc_noslot + 1;
3830 /* try to get a new slot ID from the XHCI */
3832 err = xhci_cmd_enable_slot(sc, &temp);
3835 XHCI_CMD_UNLOCK(sc);
3839 if (temp > sc->sc_noslot) {
3840 XHCI_CMD_UNLOCK(sc);
3841 return (USB_ERR_BAD_ADDRESS);
3844 if (sc->sc_hw.devs[temp].state != XHCI_ST_DISABLED) {
3845 DPRINTF("slot %u already allocated.\n", temp);
3846 XHCI_CMD_UNLOCK(sc);
3847 return (USB_ERR_BAD_ADDRESS);
3850 /* store slot ID for later reference */
3852 udev->controller_slot_id = temp;
3854 /* reset data structure */
3856 memset(&sc->sc_hw.devs[temp], 0, sizeof(sc->sc_hw.devs[0]));
3858 /* set mark slot allocated */
3860 sc->sc_hw.devs[temp].state = XHCI_ST_ENABLED;
3862 err = xhci_alloc_device_ext(udev);
3864 XHCI_CMD_UNLOCK(sc);
3866 /* get device into default state */
3869 err = xhci_set_address(udev, NULL, 0);
3875 xhci_device_uninit(struct usb_device *udev)
3877 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3880 /* no init for root HUB */
3881 if (udev->parent_hub == NULL)
3886 index = udev->controller_slot_id;
3888 if (index <= sc->sc_noslot) {
3889 xhci_cmd_disable_slot(sc, index);
3890 sc->sc_hw.devs[index].state = XHCI_ST_DISABLED;
3892 /* free device extension */
3893 xhci_free_device_ext(udev);
3896 XHCI_CMD_UNLOCK(sc);
3900 xhci_get_dma_delay(struct usb_device *udev, uint32_t *pus)
3903 * Wait until the hardware has finished any possible use of
3904 * the transfer descriptor(s)
3906 *pus = 2048; /* microseconds */
3910 xhci_device_resume(struct usb_device *udev)
3912 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3919 /* check for root HUB */
3920 if (udev->parent_hub == NULL)
3923 index = udev->controller_slot_id;
3927 /* blindly resume all endpoints */
3929 USB_BUS_LOCK(udev->bus);
3931 for (n = 1; n != XHCI_MAX_ENDPOINTS; n++) {
3932 for (p = 0; p != 1 /*XHCI_MAX_STREAMS*/; p++) {
3933 XWRITE4(sc, door, XHCI_DOORBELL(index),
3934 n | XHCI_DB_SID_SET(p));
3938 USB_BUS_UNLOCK(udev->bus);
3940 XHCI_CMD_UNLOCK(sc);
3944 xhci_device_suspend(struct usb_device *udev)
3946 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3953 /* check for root HUB */
3954 if (udev->parent_hub == NULL)
3957 index = udev->controller_slot_id;
3961 /* blindly suspend all endpoints */
3963 for (n = 1; n != XHCI_MAX_ENDPOINTS; n++) {
3964 err = xhci_cmd_stop_ep(sc, 1, n, index);
3966 DPRINTF("Failed to suspend endpoint "
3967 "%u on slot %u (ignored).\n", n, index);
3971 XHCI_CMD_UNLOCK(sc);
3975 xhci_set_hw_power(struct usb_bus *bus)
3981 xhci_device_state_change(struct usb_device *udev)
3983 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3984 struct usb_page_search buf_inp;
3988 /* check for root HUB */
3989 if (udev->parent_hub == NULL)
3992 index = udev->controller_slot_id;
3996 if (usb_get_device_state(udev) == USB_STATE_CONFIGURED) {
3997 err = uhub_query_info(udev, &sc->sc_hw.devs[index].nports,
3998 &sc->sc_hw.devs[index].tt);
4000 sc->sc_hw.devs[index].nports = 0;
4005 switch (usb_get_device_state(udev)) {
4006 case USB_STATE_POWERED:
4007 if (sc->sc_hw.devs[index].state == XHCI_ST_DEFAULT)
4010 /* set default state */
4011 sc->sc_hw.devs[index].state = XHCI_ST_DEFAULT;
4013 /* reset number of contexts */
4014 sc->sc_hw.devs[index].context_num = 0;
4016 err = xhci_cmd_reset_dev(sc, index);
4019 DPRINTF("Device reset failed "
4020 "for slot %u.\n", index);
4024 case USB_STATE_ADDRESSED:
4025 if (sc->sc_hw.devs[index].state == XHCI_ST_ADDRESSED)
4028 sc->sc_hw.devs[index].state = XHCI_ST_ADDRESSED;
4030 err = xhci_cmd_configure_ep(sc, 0, 1, index);
4033 DPRINTF("Failed to deconfigure "
4034 "slot %u.\n", index);
4038 case USB_STATE_CONFIGURED:
4039 if (sc->sc_hw.devs[index].state == XHCI_ST_CONFIGURED)
4042 /* set configured state */
4043 sc->sc_hw.devs[index].state = XHCI_ST_CONFIGURED;
4045 /* reset number of contexts */
4046 sc->sc_hw.devs[index].context_num = 0;
4048 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
4050 xhci_configure_mask(udev, 3, 0);
4052 err = xhci_configure_device(udev);
4054 DPRINTF("Could not configure device "
4055 "at slot %u.\n", index);
4058 err = xhci_cmd_evaluate_ctx(sc, buf_inp.physaddr, index);
4060 DPRINTF("Could not evaluate device "
4061 "context at slot %u.\n", index);
4068 XHCI_CMD_UNLOCK(sc);
4071 struct usb_bus_methods xhci_bus_methods = {
4072 .endpoint_init = xhci_ep_init,
4073 .endpoint_uninit = xhci_ep_uninit,
4074 .xfer_setup = xhci_xfer_setup,
4075 .xfer_unsetup = xhci_xfer_unsetup,
4076 .get_dma_delay = xhci_get_dma_delay,
4077 .device_init = xhci_device_init,
4078 .device_uninit = xhci_device_uninit,
4079 .device_resume = xhci_device_resume,
4080 .device_suspend = xhci_device_suspend,
4081 .set_hw_power = xhci_set_hw_power,
4082 .roothub_exec = xhci_roothub_exec,
4083 .xfer_poll = xhci_do_poll,
4084 .start_dma_delay = xhci_start_dma_delay,
4085 .set_address = xhci_set_address,
4086 .clear_stall = xhci_ep_clear_stall,
4087 .device_state_change = xhci_device_state_change,
4088 .set_hw_power_sleep = xhci_set_hw_power_sleep,