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);
99 #define XHCI_INTR_ENDPT 1
101 struct xhci_std_temp {
102 struct xhci_softc *sc;
103 struct usb_page_cache *pc;
105 struct xhci_td *td_next;
108 uint32_t max_packet_size;
120 uint8_t do_isoc_sync;
123 static void xhci_do_poll(struct usb_bus *);
124 static void xhci_device_done(struct usb_xfer *, usb_error_t);
125 static void xhci_root_intr(struct xhci_softc *);
126 static void xhci_free_device_ext(struct usb_device *);
127 static struct xhci_endpoint_ext *xhci_get_endpoint_ext(struct usb_device *,
128 struct usb_endpoint_descriptor *);
129 static usb_proc_callback_t xhci_configure_msg;
130 static usb_error_t xhci_configure_device(struct usb_device *);
131 static usb_error_t xhci_configure_endpoint(struct usb_device *,
132 struct usb_endpoint_descriptor *, uint64_t, uint16_t,
133 uint8_t, uint8_t, uint8_t, uint16_t, uint16_t);
134 static usb_error_t xhci_configure_mask(struct usb_device *,
136 static usb_error_t xhci_cmd_evaluate_ctx(struct xhci_softc *,
138 static void xhci_endpoint_doorbell(struct usb_xfer *);
139 static void xhci_ctx_set_le32(struct xhci_softc *sc, volatile uint32_t *ptr, uint32_t val);
140 static uint32_t xhci_ctx_get_le32(struct xhci_softc *sc, volatile uint32_t *ptr);
141 static void xhci_ctx_set_le64(struct xhci_softc *sc, volatile uint64_t *ptr, uint64_t val);
143 static uint64_t xhci_ctx_get_le64(struct xhci_softc *sc, volatile uint64_t *ptr);
146 extern struct usb_bus_methods xhci_bus_methods;
150 xhci_dump_trb(struct xhci_trb *trb)
152 DPRINTFN(5, "trb = %p\n", trb);
153 DPRINTFN(5, "qwTrb0 = 0x%016llx\n", (long long)le64toh(trb->qwTrb0));
154 DPRINTFN(5, "dwTrb2 = 0x%08x\n", le32toh(trb->dwTrb2));
155 DPRINTFN(5, "dwTrb3 = 0x%08x\n", le32toh(trb->dwTrb3));
159 xhci_dump_endpoint(struct xhci_softc *sc, struct xhci_endp_ctx *pep)
161 DPRINTFN(5, "pep = %p\n", pep);
162 DPRINTFN(5, "dwEpCtx0=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx0));
163 DPRINTFN(5, "dwEpCtx1=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx1));
164 DPRINTFN(5, "qwEpCtx2=0x%016llx\n", (long long)xhci_ctx_get_le64(sc, &pep->qwEpCtx2));
165 DPRINTFN(5, "dwEpCtx4=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx4));
166 DPRINTFN(5, "dwEpCtx5=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx5));
167 DPRINTFN(5, "dwEpCtx6=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx6));
168 DPRINTFN(5, "dwEpCtx7=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx7));
172 xhci_dump_device(struct xhci_softc *sc, struct xhci_slot_ctx *psl)
174 DPRINTFN(5, "psl = %p\n", psl);
175 DPRINTFN(5, "dwSctx0=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx0));
176 DPRINTFN(5, "dwSctx1=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx1));
177 DPRINTFN(5, "dwSctx2=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx2));
178 DPRINTFN(5, "dwSctx3=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx3));
183 xhci_get_port_route(void)
186 return (0xFFFFFFFFU ^ ((uint32_t)xhciroute));
188 return (0xFFFFFFFFU);
193 xhci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
195 struct xhci_softc *sc = XHCI_BUS2SC(bus);
198 cb(bus, &sc->sc_hw.root_pc, &sc->sc_hw.root_pg,
199 sizeof(struct xhci_hw_root), XHCI_PAGE_SIZE);
201 cb(bus, &sc->sc_hw.ctx_pc, &sc->sc_hw.ctx_pg,
202 sizeof(struct xhci_dev_ctx_addr), XHCI_PAGE_SIZE);
204 for (i = 0; i != XHCI_MAX_SCRATCHPADS; i++) {
205 cb(bus, &sc->sc_hw.scratch_pc[i], &sc->sc_hw.scratch_pg[i],
206 XHCI_PAGE_SIZE, XHCI_PAGE_SIZE);
211 xhci_ctx_set_le32(struct xhci_softc *sc, volatile uint32_t *ptr, uint32_t val)
213 if (sc->sc_ctx_is_64_byte) {
215 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
216 /* all contexts are initially 32-bytes */
217 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
218 ptr = (volatile uint32_t *)(((volatile uint8_t *)ptr) + offset);
224 xhci_ctx_get_le32(struct xhci_softc *sc, volatile uint32_t *ptr)
226 if (sc->sc_ctx_is_64_byte) {
228 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
229 /* all contexts are initially 32-bytes */
230 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
231 ptr = (volatile uint32_t *)(((volatile uint8_t *)ptr) + offset);
233 return (le32toh(*ptr));
237 xhci_ctx_set_le64(struct xhci_softc *sc, volatile uint64_t *ptr, uint64_t val)
239 if (sc->sc_ctx_is_64_byte) {
241 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
242 /* all contexts are initially 32-bytes */
243 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
244 ptr = (volatile uint64_t *)(((volatile uint8_t *)ptr) + offset);
251 xhci_ctx_get_le64(struct xhci_softc *sc, volatile uint64_t *ptr)
253 if (sc->sc_ctx_is_64_byte) {
255 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
256 /* all contexts are initially 32-bytes */
257 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
258 ptr = (volatile uint64_t *)(((volatile uint8_t *)ptr) + offset);
260 return (le64toh(*ptr));
265 xhci_start_controller(struct xhci_softc *sc)
267 struct usb_page_search buf_res;
268 struct xhci_hw_root *phwr;
269 struct xhci_dev_ctx_addr *pdctxa;
277 sc->sc_oper_off = XREAD1(sc, capa, XHCI_CAPLENGTH);
278 sc->sc_runt_off = XREAD4(sc, capa, XHCI_RTSOFF) & ~0x1F;
279 sc->sc_door_off = XREAD4(sc, capa, XHCI_DBOFF) & ~0x3;
281 DPRINTF("CAPLENGTH=0x%x\n", sc->sc_oper_off);
282 DPRINTF("RUNTIMEOFFSET=0x%x\n", sc->sc_runt_off);
283 DPRINTF("DOOROFFSET=0x%x\n", sc->sc_door_off);
285 sc->sc_event_ccs = 1;
286 sc->sc_event_idx = 0;
287 sc->sc_command_ccs = 1;
288 sc->sc_command_idx = 0;
290 DPRINTF("xHCI version = 0x%04x\n", XREAD2(sc, capa, XHCI_HCIVERSION));
292 temp = XREAD4(sc, capa, XHCI_HCSPARAMS0);
294 DPRINTF("HCS0 = 0x%08x\n", temp);
296 if (XHCI_HCS0_CSZ(temp)) {
297 sc->sc_ctx_is_64_byte = 1;
298 device_printf(sc->sc_bus.parent, "64 byte context size.\n");
300 sc->sc_ctx_is_64_byte = 0;
301 device_printf(sc->sc_bus.parent, "32 byte context size.\n");
304 /* Reset controller */
305 XWRITE4(sc, oper, XHCI_USBCMD, XHCI_CMD_HCRST);
307 for (i = 0; i != 100; i++) {
308 usb_pause_mtx(NULL, hz / 100);
309 temp = XREAD4(sc, oper, XHCI_USBCMD) &
310 (XHCI_CMD_HCRST | XHCI_STS_CNR);
316 device_printf(sc->sc_bus.parent, "Controller "
318 return (USB_ERR_IOERROR);
321 if (!(XREAD4(sc, oper, XHCI_PAGESIZE) & XHCI_PAGESIZE_4K)) {
322 device_printf(sc->sc_bus.parent, "Controller does "
323 "not support 4K page size.\n");
324 return (USB_ERR_IOERROR);
327 temp = XREAD4(sc, capa, XHCI_HCSPARAMS1);
329 i = XHCI_HCS1_N_PORTS(temp);
332 device_printf(sc->sc_bus.parent, "Invalid number "
333 "of ports: %u\n", i);
334 return (USB_ERR_IOERROR);
338 sc->sc_noslot = XHCI_HCS1_DEVSLOT_MAX(temp);
340 if (sc->sc_noslot > XHCI_MAX_DEVICES)
341 sc->sc_noslot = XHCI_MAX_DEVICES;
343 /* setup number of device slots */
345 DPRINTF("CONFIG=0x%08x -> 0x%08x\n",
346 XREAD4(sc, oper, XHCI_CONFIG), sc->sc_noslot);
348 XWRITE4(sc, oper, XHCI_CONFIG, sc->sc_noslot);
350 DPRINTF("Max slots: %u\n", sc->sc_noslot);
352 temp = XREAD4(sc, capa, XHCI_HCSPARAMS2);
354 sc->sc_noscratch = XHCI_HCS2_SPB_MAX(temp);
356 if (sc->sc_noscratch > XHCI_MAX_SCRATCHPADS) {
357 device_printf(sc->sc_bus.parent, "XHCI request "
358 "too many scratchpads\n");
359 return (USB_ERR_NOMEM);
362 DPRINTF("Max scratch: %u\n", sc->sc_noscratch);
364 temp = XREAD4(sc, capa, XHCI_HCSPARAMS3);
366 sc->sc_exit_lat_max = XHCI_HCS3_U1_DEL(temp) +
367 XHCI_HCS3_U2_DEL(temp) + 250 /* us */;
369 temp = XREAD4(sc, oper, XHCI_USBSTS);
371 /* clear interrupts */
372 XWRITE4(sc, oper, XHCI_USBSTS, temp);
373 /* disable all device notifications */
374 XWRITE4(sc, oper, XHCI_DNCTRL, 0);
376 /* setup device context base address */
377 usbd_get_page(&sc->sc_hw.ctx_pc, 0, &buf_res);
378 pdctxa = buf_res.buffer;
379 memset(pdctxa, 0, sizeof(*pdctxa));
381 addr = buf_res.physaddr;
382 addr += (uintptr_t)&((struct xhci_dev_ctx_addr *)0)->qwSpBufPtr[0];
384 /* slot 0 points to the table of scratchpad pointers */
385 pdctxa->qwBaaDevCtxAddr[0] = htole64(addr);
387 for (i = 0; i != sc->sc_noscratch; i++) {
388 struct usb_page_search buf_scp;
389 usbd_get_page(&sc->sc_hw.scratch_pc[i], 0, &buf_scp);
390 pdctxa->qwSpBufPtr[i] = htole64((uint64_t)buf_scp.physaddr);
393 addr = buf_res.physaddr;
395 XWRITE4(sc, oper, XHCI_DCBAAP_LO, (uint32_t)addr);
396 XWRITE4(sc, oper, XHCI_DCBAAP_HI, (uint32_t)(addr >> 32));
397 XWRITE4(sc, oper, XHCI_DCBAAP_LO, (uint32_t)addr);
398 XWRITE4(sc, oper, XHCI_DCBAAP_HI, (uint32_t)(addr >> 32));
400 /* Setup event table size */
402 temp = XREAD4(sc, capa, XHCI_HCSPARAMS2);
404 DPRINTF("HCS2=0x%08x\n", temp);
406 temp = XHCI_HCS2_ERST_MAX(temp);
408 if (temp > XHCI_MAX_RSEG)
409 temp = XHCI_MAX_RSEG;
411 sc->sc_erst_max = temp;
413 DPRINTF("ERSTSZ=0x%08x -> 0x%08x\n",
414 XREAD4(sc, runt, XHCI_ERSTSZ(0)), temp);
416 XWRITE4(sc, runt, XHCI_ERSTSZ(0), XHCI_ERSTS_SET(temp));
418 /* Setup interrupt rate */
419 XWRITE4(sc, runt, XHCI_IMOD(0), XHCI_IMOD_DEFAULT);
421 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
423 phwr = buf_res.buffer;
424 addr = buf_res.physaddr;
425 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_events[0];
427 /* reset hardware root structure */
428 memset(phwr, 0, sizeof(*phwr));
430 phwr->hwr_ring_seg[0].qwEvrsTablePtr = htole64(addr);
431 phwr->hwr_ring_seg[0].dwEvrsTableSize = htole32(XHCI_MAX_EVENTS);
433 DPRINTF("ERDP(0)=0x%016llx\n", (unsigned long long)addr);
435 XWRITE4(sc, runt, XHCI_ERDP_LO(0), (uint32_t)addr);
436 XWRITE4(sc, runt, XHCI_ERDP_HI(0), (uint32_t)(addr >> 32));
438 addr = (uint64_t)buf_res.physaddr;
440 DPRINTF("ERSTBA(0)=0x%016llx\n", (unsigned long long)addr);
442 XWRITE4(sc, runt, XHCI_ERSTBA_LO(0), (uint32_t)addr);
443 XWRITE4(sc, runt, XHCI_ERSTBA_HI(0), (uint32_t)(addr >> 32));
445 /* Setup interrupter registers */
447 temp = XREAD4(sc, runt, XHCI_IMAN(0));
448 temp |= XHCI_IMAN_INTR_ENA;
449 XWRITE4(sc, runt, XHCI_IMAN(0), temp);
451 /* setup command ring control base address */
452 addr = buf_res.physaddr;
453 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_commands[0];
455 DPRINTF("CRCR=0x%016llx\n", (unsigned long long)addr);
457 XWRITE4(sc, oper, XHCI_CRCR_LO, ((uint32_t)addr) | XHCI_CRCR_LO_RCS);
458 XWRITE4(sc, oper, XHCI_CRCR_HI, (uint32_t)(addr >> 32));
460 phwr->hwr_commands[XHCI_MAX_COMMANDS - 1].qwTrb0 = htole64(addr);
462 usb_bus_mem_flush_all(&sc->sc_bus, &xhci_iterate_hw_softc);
465 XWRITE4(sc, oper, XHCI_USBCMD, XHCI_CMD_RS |
466 XHCI_CMD_INTE | XHCI_CMD_HSEE);
468 for (i = 0; i != 100; i++) {
469 usb_pause_mtx(NULL, hz / 100);
470 temp = XREAD4(sc, oper, XHCI_USBSTS) & XHCI_STS_HCH;
475 XWRITE4(sc, oper, XHCI_USBCMD, 0);
476 device_printf(sc->sc_bus.parent, "Run timeout.\n");
477 return (USB_ERR_IOERROR);
480 /* catch any lost interrupts */
481 xhci_do_poll(&sc->sc_bus);
487 xhci_halt_controller(struct xhci_softc *sc)
495 sc->sc_oper_off = XREAD1(sc, capa, XHCI_CAPLENGTH);
496 sc->sc_runt_off = XREAD4(sc, capa, XHCI_RTSOFF) & ~0xF;
497 sc->sc_door_off = XREAD4(sc, capa, XHCI_DBOFF) & ~0x3;
499 /* Halt controller */
500 XWRITE4(sc, oper, XHCI_USBCMD, 0);
502 for (i = 0; i != 100; i++) {
503 usb_pause_mtx(NULL, hz / 100);
504 temp = XREAD4(sc, oper, XHCI_USBSTS) & XHCI_STS_HCH;
510 device_printf(sc->sc_bus.parent, "Controller halt timeout.\n");
511 return (USB_ERR_IOERROR);
517 xhci_init(struct xhci_softc *sc, device_t self)
519 /* initialise some bus fields */
520 sc->sc_bus.parent = self;
522 /* set the bus revision */
523 sc->sc_bus.usbrev = USB_REV_3_0;
525 /* set up the bus struct */
526 sc->sc_bus.methods = &xhci_bus_methods;
528 /* setup devices array */
529 sc->sc_bus.devices = sc->sc_devices;
530 sc->sc_bus.devices_max = XHCI_MAX_DEVICES;
532 /* setup command queue mutex and condition varible */
533 cv_init(&sc->sc_cmd_cv, "CMDQ");
534 sx_init(&sc->sc_cmd_sx, "CMDQ lock");
536 /* get all DMA memory */
537 if (usb_bus_mem_alloc_all(&sc->sc_bus,
538 USB_GET_DMA_TAG(self), &xhci_iterate_hw_softc)) {
542 sc->sc_config_msg[0].hdr.pm_callback = &xhci_configure_msg;
543 sc->sc_config_msg[0].bus = &sc->sc_bus;
544 sc->sc_config_msg[1].hdr.pm_callback = &xhci_configure_msg;
545 sc->sc_config_msg[1].bus = &sc->sc_bus;
547 if (usb_proc_create(&sc->sc_config_proc,
548 &sc->sc_bus.bus_mtx, device_get_nameunit(self), USB_PRI_MED)) {
549 printf("WARNING: Creation of XHCI configure "
550 "callback process failed.\n");
556 xhci_uninit(struct xhci_softc *sc)
558 usb_proc_free(&sc->sc_config_proc);
560 usb_bus_mem_free_all(&sc->sc_bus, &xhci_iterate_hw_softc);
562 cv_destroy(&sc->sc_cmd_cv);
563 sx_destroy(&sc->sc_cmd_sx);
567 xhci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
569 struct xhci_softc *sc = XHCI_BUS2SC(bus);
572 case USB_HW_POWER_SUSPEND:
573 DPRINTF("Stopping the XHCI\n");
574 xhci_halt_controller(sc);
576 case USB_HW_POWER_SHUTDOWN:
577 DPRINTF("Stopping the XHCI\n");
578 xhci_halt_controller(sc);
580 case USB_HW_POWER_RESUME:
581 DPRINTF("Starting the XHCI\n");
582 xhci_start_controller(sc);
590 xhci_generic_done_sub(struct usb_xfer *xfer)
593 struct xhci_td *td_alt_next;
597 td = xfer->td_transfer_cache;
598 td_alt_next = td->alt_next;
600 if (xfer->aframes != xfer->nframes)
601 usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
605 usb_pc_cpu_invalidate(td->page_cache);
610 DPRINTFN(4, "xfer=%p[%u/%u] rem=%u/%u status=%u\n",
611 xfer, (unsigned int)xfer->aframes,
612 (unsigned int)xfer->nframes,
613 (unsigned int)len, (unsigned int)td->len,
614 (unsigned int)status);
617 * Verify the status length and
618 * add the length to "frlengths[]":
621 /* should not happen */
622 DPRINTF("Invalid status length, "
623 "0x%04x/0x%04x bytes\n", len, td->len);
624 status = XHCI_TRB_ERROR_LENGTH;
625 } else if (xfer->aframes != xfer->nframes) {
626 xfer->frlengths[xfer->aframes] += td->len - len;
628 /* Check for last transfer */
629 if (((void *)td) == xfer->td_transfer_last) {
633 /* Check for transfer error */
634 if (status != XHCI_TRB_ERROR_SHORT_PKT &&
635 status != XHCI_TRB_ERROR_SUCCESS) {
636 /* the transfer is finished */
640 /* Check for short transfer */
642 if (xfer->flags_int.short_frames_ok ||
643 xfer->flags_int.isochronous_xfr ||
644 xfer->flags_int.control_xfr) {
645 /* follow alt next */
648 /* the transfer is finished */
655 if (td->alt_next != td_alt_next) {
656 /* this USB frame is complete */
661 /* update transfer cache */
663 xfer->td_transfer_cache = td;
665 return ((status == XHCI_TRB_ERROR_STALL) ? USB_ERR_STALLED :
666 (status != XHCI_TRB_ERROR_SHORT_PKT &&
667 status != XHCI_TRB_ERROR_SUCCESS) ? USB_ERR_IOERROR :
668 USB_ERR_NORMAL_COMPLETION);
672 xhci_generic_done(struct usb_xfer *xfer)
676 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
677 xfer, xfer->endpoint);
681 xfer->td_transfer_cache = xfer->td_transfer_first;
683 if (xfer->flags_int.control_xfr) {
685 if (xfer->flags_int.control_hdr)
686 err = xhci_generic_done_sub(xfer);
690 if (xfer->td_transfer_cache == NULL)
694 while (xfer->aframes != xfer->nframes) {
696 err = xhci_generic_done_sub(xfer);
699 if (xfer->td_transfer_cache == NULL)
703 if (xfer->flags_int.control_xfr &&
704 !xfer->flags_int.control_act)
705 err = xhci_generic_done_sub(xfer);
707 /* transfer is complete */
708 xhci_device_done(xfer, err);
712 xhci_activate_transfer(struct usb_xfer *xfer)
716 td = xfer->td_transfer_cache;
718 usb_pc_cpu_invalidate(td->page_cache);
720 if (!(td->td_trb[0].dwTrb3 & htole32(XHCI_TRB_3_CYCLE_BIT))) {
722 /* activate the transfer */
724 td->td_trb[0].dwTrb3 |= htole32(XHCI_TRB_3_CYCLE_BIT);
725 usb_pc_cpu_flush(td->page_cache);
727 xhci_endpoint_doorbell(xfer);
732 xhci_skip_transfer(struct usb_xfer *xfer)
735 struct xhci_td *td_last;
737 td = xfer->td_transfer_cache;
738 td_last = xfer->td_transfer_last;
742 usb_pc_cpu_invalidate(td->page_cache);
744 if (!(td->td_trb[0].dwTrb3 & htole32(XHCI_TRB_3_CYCLE_BIT))) {
746 usb_pc_cpu_invalidate(td_last->page_cache);
748 /* copy LINK TRB to current waiting location */
750 td->td_trb[0].qwTrb0 = td_last->td_trb[td_last->ntrb].qwTrb0;
751 td->td_trb[0].dwTrb2 = td_last->td_trb[td_last->ntrb].dwTrb2;
752 usb_pc_cpu_flush(td->page_cache);
754 td->td_trb[0].dwTrb3 = td_last->td_trb[td_last->ntrb].dwTrb3;
755 usb_pc_cpu_flush(td->page_cache);
757 xhci_endpoint_doorbell(xfer);
761 /*------------------------------------------------------------------------*
762 * xhci_check_transfer
763 *------------------------------------------------------------------------*/
765 xhci_check_transfer(struct xhci_softc *sc, struct xhci_trb *trb)
778 td_event = le64toh(trb->qwTrb0);
779 temp = le32toh(trb->dwTrb2);
781 remainder = XHCI_TRB_2_REM_GET(temp);
782 status = XHCI_TRB_2_ERROR_GET(temp);
784 temp = le32toh(trb->dwTrb3);
785 epno = XHCI_TRB_3_EP_GET(temp);
786 index = XHCI_TRB_3_SLOT_GET(temp);
788 /* check if error means halted */
789 halted = (status != XHCI_TRB_ERROR_SHORT_PKT &&
790 status != XHCI_TRB_ERROR_SUCCESS);
792 DPRINTF("slot=%u epno=%u remainder=%u status=%u\n",
793 index, epno, remainder, status);
795 if (index > sc->sc_noslot) {
796 DPRINTF("Invalid slot.\n");
800 if ((epno == 0) || (epno >= XHCI_MAX_ENDPOINTS)) {
801 DPRINTF("Invalid endpoint.\n");
805 /* try to find the USB transfer that generated the event */
806 for (i = 0; i != (XHCI_MAX_TRANSFERS - 1); i++) {
807 struct usb_xfer *xfer;
809 struct xhci_endpoint_ext *pepext;
811 pepext = &sc->sc_hw.devs[index].endp[epno];
813 xfer = pepext->xfer[i];
817 td = xfer->td_transfer_cache;
819 DPRINTFN(5, "Checking if 0x%016llx == (0x%016llx .. 0x%016llx)\n",
821 (long long)td->td_self,
822 (long long)td->td_self + sizeof(td->td_trb));
825 * NOTE: Some XHCI implementations might not trigger
826 * an event on the last LINK TRB so we need to
827 * consider both the last and second last event
828 * address as conditions for a successful transfer.
830 * NOTE: We assume that the XHCI will only trigger one
831 * event per chain of TRBs.
834 offset = td_event - td->td_self;
837 offset < (int64_t)sizeof(td->td_trb)) {
839 usb_pc_cpu_invalidate(td->page_cache);
841 /* compute rest of remainder, if any */
842 for (i = (offset / 16) + 1; i < td->ntrb; i++) {
843 temp = le32toh(td->td_trb[i].dwTrb2);
844 remainder += XHCI_TRB_2_BYTES_GET(temp);
847 DPRINTFN(5, "New remainder: %u\n", remainder);
849 /* clear isochronous transfer errors */
850 if (xfer->flags_int.isochronous_xfr) {
853 status = XHCI_TRB_ERROR_SUCCESS;
858 /* "td->remainder" is verified later */
859 td->remainder = remainder;
862 usb_pc_cpu_flush(td->page_cache);
865 * 1) Last transfer descriptor makes the
868 if (((void *)td) == xfer->td_transfer_last) {
869 DPRINTF("TD is last\n");
870 xhci_generic_done(xfer);
875 * 2) Any kind of error makes the transfer
879 DPRINTF("TD has I/O error\n");
880 xhci_generic_done(xfer);
885 * 3) If there is no alternate next transfer,
886 * a short packet also makes the transfer done
888 if (td->remainder > 0) {
889 if (td->alt_next == NULL) {
891 "short TD has no alternate next\n");
892 xhci_generic_done(xfer);
895 DPRINTF("TD has short pkt\n");
896 if (xfer->flags_int.short_frames_ok ||
897 xfer->flags_int.isochronous_xfr ||
898 xfer->flags_int.control_xfr) {
899 /* follow the alt next */
900 xfer->td_transfer_cache = td->alt_next;
901 xhci_activate_transfer(xfer);
904 xhci_skip_transfer(xfer);
905 xhci_generic_done(xfer);
910 * 4) Transfer complete - go to next TD
912 DPRINTF("Following next TD\n");
913 xfer->td_transfer_cache = td->obj_next;
914 xhci_activate_transfer(xfer);
915 break; /* there should only be one match */
921 xhci_check_command(struct xhci_softc *sc, struct xhci_trb *trb)
923 if (sc->sc_cmd_addr == trb->qwTrb0) {
924 DPRINTF("Received command event\n");
925 sc->sc_cmd_result[0] = trb->dwTrb2;
926 sc->sc_cmd_result[1] = trb->dwTrb3;
927 cv_signal(&sc->sc_cmd_cv);
932 xhci_interrupt_poll(struct xhci_softc *sc)
934 struct usb_page_search buf_res;
935 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 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));
1017 xhci_do_command(struct xhci_softc *sc, struct xhci_trb *trb,
1018 uint16_t timeout_ms)
1020 struct usb_page_search buf_res;
1021 struct xhci_hw_root *phwr;
1028 XHCI_CMD_ASSERT_LOCKED(sc);
1030 /* get hardware root structure */
1032 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
1034 phwr = buf_res.buffer;
1038 USB_BUS_LOCK(&sc->sc_bus);
1040 i = sc->sc_command_idx;
1041 j = sc->sc_command_ccs;
1043 DPRINTFN(10, "command[%u] = %u (0x%016llx, 0x%08lx, 0x%08lx)\n",
1044 i, XHCI_TRB_3_TYPE_GET(le32toh(trb->dwTrb3)),
1045 (long long)le64toh(trb->qwTrb0),
1046 (long)le32toh(trb->dwTrb2),
1047 (long)le32toh(trb->dwTrb3));
1049 phwr->hwr_commands[i].qwTrb0 = trb->qwTrb0;
1050 phwr->hwr_commands[i].dwTrb2 = trb->dwTrb2;
1052 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1057 temp |= htole32(XHCI_TRB_3_CYCLE_BIT);
1059 temp &= ~htole32(XHCI_TRB_3_CYCLE_BIT);
1061 temp &= ~htole32(XHCI_TRB_3_TC_BIT);
1063 phwr->hwr_commands[i].dwTrb3 = temp;
1065 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1067 addr = buf_res.physaddr;
1068 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_commands[i];
1070 sc->sc_cmd_addr = htole64(addr);
1074 if (i == (XHCI_MAX_COMMANDS - 1)) {
1077 temp = htole32(XHCI_TRB_3_TC_BIT |
1078 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) |
1079 XHCI_TRB_3_CYCLE_BIT);
1081 temp = htole32(XHCI_TRB_3_TC_BIT |
1082 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
1085 phwr->hwr_commands[i].dwTrb3 = temp;
1087 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1093 sc->sc_command_idx = i;
1094 sc->sc_command_ccs = j;
1096 XWRITE4(sc, door, XHCI_DOORBELL(0), 0);
1098 err = cv_timedwait(&sc->sc_cmd_cv, &sc->sc_bus.bus_mtx,
1099 USB_MS_TO_TICKS(timeout_ms));
1102 DPRINTFN(0, "Command timeout!\n");
1103 err = USB_ERR_TIMEOUT;
1107 temp = le32toh(sc->sc_cmd_result[0]);
1108 if (XHCI_TRB_2_ERROR_GET(temp) != XHCI_TRB_ERROR_SUCCESS)
1109 err = USB_ERR_IOERROR;
1111 trb->dwTrb2 = sc->sc_cmd_result[0];
1112 trb->dwTrb3 = sc->sc_cmd_result[1];
1115 USB_BUS_UNLOCK(&sc->sc_bus);
1122 xhci_cmd_nop(struct xhci_softc *sc)
1124 struct xhci_trb trb;
1131 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NOOP);
1133 trb.dwTrb3 = htole32(temp);
1135 return (xhci_do_command(sc, &trb, 100 /* ms */));
1140 xhci_cmd_enable_slot(struct xhci_softc *sc, uint8_t *pslot)
1142 struct xhci_trb trb;
1150 trb.dwTrb3 = htole32(XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ENABLE_SLOT));
1152 err = xhci_do_command(sc, &trb, 100 /* ms */);
1156 temp = le32toh(trb.dwTrb3);
1158 *pslot = XHCI_TRB_3_SLOT_GET(temp);
1165 xhci_cmd_disable_slot(struct xhci_softc *sc, uint8_t slot_id)
1167 struct xhci_trb trb;
1174 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DISABLE_SLOT) |
1175 XHCI_TRB_3_SLOT_SET(slot_id);
1177 trb.dwTrb3 = htole32(temp);
1179 return (xhci_do_command(sc, &trb, 100 /* ms */));
1183 xhci_cmd_set_address(struct xhci_softc *sc, uint64_t input_ctx,
1184 uint8_t bsr, uint8_t slot_id)
1186 struct xhci_trb trb;
1191 trb.qwTrb0 = htole64(input_ctx);
1193 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ADDRESS_DEVICE) |
1194 XHCI_TRB_3_SLOT_SET(slot_id);
1197 temp |= XHCI_TRB_3_BSR_BIT;
1199 trb.dwTrb3 = htole32(temp);
1201 return (xhci_do_command(sc, &trb, 500 /* ms */));
1205 xhci_set_address(struct usb_device *udev, struct mtx *mtx, uint16_t address)
1207 struct usb_page_search buf_inp;
1208 struct usb_page_search buf_dev;
1209 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
1210 struct xhci_hw_dev *hdev;
1211 struct xhci_dev_ctx *pdev;
1212 struct xhci_endpoint_ext *pepext;
1218 /* the root HUB case is not handled here */
1219 if (udev->parent_hub == NULL)
1220 return (USB_ERR_INVAL);
1222 index = udev->controller_slot_id;
1224 hdev = &sc->sc_hw.devs[index];
1231 switch (hdev->state) {
1232 case XHCI_ST_DEFAULT:
1233 case XHCI_ST_ENABLED:
1235 hdev->state = XHCI_ST_ENABLED;
1237 /* set configure mask to slot and EP0 */
1238 xhci_configure_mask(udev, 3, 0);
1240 /* configure input slot context structure */
1241 err = xhci_configure_device(udev);
1244 DPRINTF("Could not configure device\n");
1248 /* configure input endpoint context structure */
1249 switch (udev->speed) {
1251 case USB_SPEED_FULL:
1254 case USB_SPEED_HIGH:
1262 pepext = xhci_get_endpoint_ext(udev,
1263 &udev->ctrl_ep_desc);
1264 err = xhci_configure_endpoint(udev,
1265 &udev->ctrl_ep_desc, pepext->physaddr,
1266 0, 1, 1, 0, mps, mps);
1269 DPRINTF("Could not configure default endpoint\n");
1273 /* execute set address command */
1274 usbd_get_page(&hdev->input_pc, 0, &buf_inp);
1276 err = xhci_cmd_set_address(sc, buf_inp.physaddr,
1277 (address == 0), index);
1280 DPRINTF("Could not set address "
1281 "for slot %u.\n", index);
1286 /* update device address to new value */
1288 usbd_get_page(&hdev->device_pc, 0, &buf_dev);
1289 pdev = buf_dev.buffer;
1290 usb_pc_cpu_invalidate(&hdev->device_pc);
1292 temp = xhci_ctx_get_le32(sc, &pdev->ctx_slot.dwSctx3);
1293 udev->address = XHCI_SCTX_3_DEV_ADDR_GET(temp);
1295 /* update device state to new value */
1298 hdev->state = XHCI_ST_ADDRESSED;
1300 hdev->state = XHCI_ST_DEFAULT;
1304 DPRINTF("Wrong state for set address.\n");
1305 err = USB_ERR_IOERROR;
1308 XHCI_CMD_UNLOCK(sc);
1317 xhci_cmd_configure_ep(struct xhci_softc *sc, uint64_t input_ctx,
1318 uint8_t deconfigure, uint8_t slot_id)
1320 struct xhci_trb trb;
1325 trb.qwTrb0 = htole64(input_ctx);
1327 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_CONFIGURE_EP) |
1328 XHCI_TRB_3_SLOT_SET(slot_id);
1331 temp |= XHCI_TRB_3_DCEP_BIT;
1333 trb.dwTrb3 = htole32(temp);
1335 return (xhci_do_command(sc, &trb, 100 /* ms */));
1339 xhci_cmd_evaluate_ctx(struct xhci_softc *sc, uint64_t input_ctx,
1342 struct xhci_trb trb;
1347 trb.qwTrb0 = htole64(input_ctx);
1349 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVALUATE_CTX) |
1350 XHCI_TRB_3_SLOT_SET(slot_id);
1351 trb.dwTrb3 = htole32(temp);
1353 return (xhci_do_command(sc, &trb, 100 /* ms */));
1357 xhci_cmd_reset_ep(struct xhci_softc *sc, uint8_t preserve,
1358 uint8_t ep_id, uint8_t slot_id)
1360 struct xhci_trb trb;
1367 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_EP) |
1368 XHCI_TRB_3_SLOT_SET(slot_id) |
1369 XHCI_TRB_3_EP_SET(ep_id);
1372 temp |= XHCI_TRB_3_PRSV_BIT;
1374 trb.dwTrb3 = htole32(temp);
1376 return (xhci_do_command(sc, &trb, 100 /* ms */));
1380 xhci_cmd_set_tr_dequeue_ptr(struct xhci_softc *sc, uint64_t dequeue_ptr,
1381 uint16_t stream_id, uint8_t ep_id, uint8_t slot_id)
1383 struct xhci_trb trb;
1388 trb.qwTrb0 = htole64(dequeue_ptr);
1390 temp = XHCI_TRB_2_STREAM_SET(stream_id);
1391 trb.dwTrb2 = htole32(temp);
1393 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SET_TR_DEQUEUE) |
1394 XHCI_TRB_3_SLOT_SET(slot_id) |
1395 XHCI_TRB_3_EP_SET(ep_id);
1396 trb.dwTrb3 = htole32(temp);
1398 return (xhci_do_command(sc, &trb, 100 /* ms */));
1402 xhci_cmd_stop_ep(struct xhci_softc *sc, uint8_t suspend,
1403 uint8_t ep_id, uint8_t slot_id)
1405 struct xhci_trb trb;
1412 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STOP_EP) |
1413 XHCI_TRB_3_SLOT_SET(slot_id) |
1414 XHCI_TRB_3_EP_SET(ep_id);
1417 temp |= XHCI_TRB_3_SUSP_EP_BIT;
1419 trb.dwTrb3 = htole32(temp);
1421 return (xhci_do_command(sc, &trb, 100 /* ms */));
1425 xhci_cmd_reset_dev(struct xhci_softc *sc, uint8_t slot_id)
1427 struct xhci_trb trb;
1434 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_DEVICE) |
1435 XHCI_TRB_3_SLOT_SET(slot_id);
1437 trb.dwTrb3 = htole32(temp);
1439 return (xhci_do_command(sc, &trb, 100 /* ms */));
1442 /*------------------------------------------------------------------------*
1443 * xhci_interrupt - XHCI interrupt handler
1444 *------------------------------------------------------------------------*/
1446 xhci_interrupt(struct xhci_softc *sc)
1450 USB_BUS_LOCK(&sc->sc_bus);
1452 status = XREAD4(sc, oper, XHCI_USBSTS);
1456 /* acknowledge interrupts */
1458 XWRITE4(sc, oper, XHCI_USBSTS, status);
1460 DPRINTFN(16, "real interrupt (status=0x%08x)\n", status);
1462 if (status & XHCI_STS_EINT) {
1463 /* check for event(s) */
1464 xhci_interrupt_poll(sc);
1467 if (status & (XHCI_STS_PCD | XHCI_STS_HCH |
1468 XHCI_STS_HSE | XHCI_STS_HCE)) {
1470 if (status & XHCI_STS_PCD) {
1474 if (status & XHCI_STS_HCH) {
1475 printf("%s: host controller halted\n",
1479 if (status & XHCI_STS_HSE) {
1480 printf("%s: host system error\n",
1484 if (status & XHCI_STS_HCE) {
1485 printf("%s: host controller error\n",
1490 USB_BUS_UNLOCK(&sc->sc_bus);
1493 /*------------------------------------------------------------------------*
1494 * xhci_timeout - XHCI timeout handler
1495 *------------------------------------------------------------------------*/
1497 xhci_timeout(void *arg)
1499 struct usb_xfer *xfer = arg;
1501 DPRINTF("xfer=%p\n", xfer);
1503 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1505 /* transfer is transferred */
1506 xhci_device_done(xfer, USB_ERR_TIMEOUT);
1510 xhci_do_poll(struct usb_bus *bus)
1512 struct xhci_softc *sc = XHCI_BUS2SC(bus);
1514 USB_BUS_LOCK(&sc->sc_bus);
1515 xhci_interrupt_poll(sc);
1516 USB_BUS_UNLOCK(&sc->sc_bus);
1520 xhci_setup_generic_chain_sub(struct xhci_std_temp *temp)
1522 struct usb_page_search buf_res;
1524 struct xhci_td *td_next;
1525 struct xhci_td *td_alt_next;
1526 struct xhci_td *td_first;
1527 uint32_t buf_offset;
1532 uint8_t shortpkt_old;
1538 shortpkt_old = temp->shortpkt;
1539 len_old = temp->len;
1546 td_next = td_first = temp->td_next;
1550 if (temp->len == 0) {
1555 /* send a Zero Length Packet, ZLP, last */
1562 average = temp->average;
1564 if (temp->len < average) {
1565 if (temp->len % temp->max_packet_size) {
1568 average = temp->len;
1572 if (td_next == NULL)
1573 panic("%s: out of XHCI transfer descriptors!", __FUNCTION__);
1578 td_next = td->obj_next;
1580 /* check if we are pre-computing */
1584 /* update remaining length */
1586 temp->len -= average;
1590 /* fill out current TD */
1596 /* update remaining length */
1598 temp->len -= average;
1600 /* reset TRB index */
1604 if (temp->trb_type == XHCI_TRB_TYPE_SETUP_STAGE) {
1605 /* immediate data */
1610 td->td_trb[0].qwTrb0 = 0;
1612 usbd_copy_out(temp->pc, temp->offset + buf_offset,
1613 (uint8_t *)(uintptr_t)&td->td_trb[0].qwTrb0,
1616 dword = XHCI_TRB_2_BYTES_SET(8) |
1617 XHCI_TRB_2_TDSZ_SET(0) |
1618 XHCI_TRB_2_IRQ_SET(0);
1620 td->td_trb[0].dwTrb2 = htole32(dword);
1622 dword = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SETUP_STAGE) |
1623 XHCI_TRB_3_IDT_BIT | XHCI_TRB_3_CYCLE_BIT;
1626 if (td->td_trb[0].qwTrb0 &
1627 htole64(XHCI_TRB_0_WLENGTH_MASK)) {
1628 if (td->td_trb[0].qwTrb0 & htole64(1))
1629 dword |= XHCI_TRB_3_TRT_IN;
1631 dword |= XHCI_TRB_3_TRT_OUT;
1634 td->td_trb[0].dwTrb3 = htole32(dword);
1636 xhci_dump_trb(&td->td_trb[x]);
1644 /* fill out buffer pointers */
1647 memset(&buf_res, 0, sizeof(buf_res));
1649 usbd_get_page(temp->pc, temp->offset +
1650 buf_offset, &buf_res);
1652 /* get length to end of page */
1653 if (buf_res.length > average)
1654 buf_res.length = average;
1656 /* check for maximum length */
1657 if (buf_res.length > XHCI_TD_PAGE_SIZE)
1658 buf_res.length = XHCI_TD_PAGE_SIZE;
1660 npkt_off += buf_res.length;
1664 npkt = (len_old - npkt_off + temp->max_packet_size - 1) /
1665 temp->max_packet_size;
1672 /* fill out TRB's */
1673 td->td_trb[x].qwTrb0 =
1674 htole64((uint64_t)buf_res.physaddr);
1677 XHCI_TRB_2_BYTES_SET(buf_res.length) |
1678 XHCI_TRB_2_TDSZ_SET(npkt) |
1679 XHCI_TRB_2_IRQ_SET(0);
1681 td->td_trb[x].dwTrb2 = htole32(dword);
1683 switch (temp->trb_type) {
1684 case XHCI_TRB_TYPE_ISOCH:
1685 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1686 XHCI_TRB_3_TBC_SET(temp->tbc) |
1687 XHCI_TRB_3_TLBPC_SET(temp->tlbpc);
1688 if (td != td_first) {
1689 dword |= XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL);
1690 } else if (temp->do_isoc_sync != 0) {
1691 temp->do_isoc_sync = 0;
1692 /* wait until "isoc_frame" */
1693 dword |= XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ISOCH) |
1694 XHCI_TRB_3_FRID_SET(temp->isoc_frame / 8);
1696 /* start data transfer at next interval */
1697 dword |= XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ISOCH) |
1698 XHCI_TRB_3_ISO_SIA_BIT;
1700 if (temp->direction == UE_DIR_IN)
1701 dword |= XHCI_TRB_3_DIR_IN | XHCI_TRB_3_ISP_BIT;
1703 case XHCI_TRB_TYPE_DATA_STAGE:
1704 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1705 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DATA_STAGE) |
1706 XHCI_TRB_3_TBC_SET(temp->tbc) |
1707 XHCI_TRB_3_TLBPC_SET(temp->tlbpc);
1708 if (temp->direction == UE_DIR_IN)
1709 dword |= XHCI_TRB_3_DIR_IN | XHCI_TRB_3_ISP_BIT;
1711 case XHCI_TRB_TYPE_STATUS_STAGE:
1712 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1713 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STATUS_STAGE) |
1714 XHCI_TRB_3_TBC_SET(temp->tbc) |
1715 XHCI_TRB_3_TLBPC_SET(temp->tlbpc);
1716 if (temp->direction == UE_DIR_IN)
1717 dword |= XHCI_TRB_3_DIR_IN;
1719 default: /* XHCI_TRB_TYPE_NORMAL */
1720 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1721 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL) |
1722 XHCI_TRB_3_TBC_SET(temp->tbc) |
1723 XHCI_TRB_3_TLBPC_SET(temp->tlbpc);
1724 if (temp->direction == UE_DIR_IN)
1725 dword |= XHCI_TRB_3_DIR_IN | XHCI_TRB_3_ISP_BIT;
1728 td->td_trb[x].dwTrb3 = htole32(dword);
1730 average -= buf_res.length;
1731 buf_offset += buf_res.length;
1733 xhci_dump_trb(&td->td_trb[x]);
1737 } while (average != 0);
1739 td->td_trb[x-1].dwTrb3 |= htole32(XHCI_TRB_3_IOC_BIT);
1741 /* store number of data TRB's */
1745 DPRINTF("NTRB=%u\n", x);
1747 /* fill out link TRB */
1749 if (td_next != NULL) {
1750 /* link the current TD with the next one */
1751 td->td_trb[x].qwTrb0 = htole64((uint64_t)td_next->td_self);
1752 DPRINTF("LINK=0x%08llx\n", (long long)td_next->td_self);
1754 /* this field will get updated later */
1755 DPRINTF("NOLINK\n");
1758 dword = XHCI_TRB_2_IRQ_SET(0);
1760 td->td_trb[x].dwTrb2 = htole32(dword);
1762 dword = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) |
1763 XHCI_TRB_3_CYCLE_BIT | XHCI_TRB_3_IOC_BIT;
1765 td->td_trb[x].dwTrb3 = htole32(dword);
1767 td->alt_next = td_alt_next;
1769 xhci_dump_trb(&td->td_trb[x]);
1771 usb_pc_cpu_flush(td->page_cache);
1777 /* setup alt next pointer, if any */
1778 if (temp->last_frame) {
1781 /* we use this field internally */
1782 td_alt_next = td_next;
1786 temp->shortpkt = shortpkt_old;
1787 temp->len = len_old;
1792 * Remove cycle bit from the first TRB if we are
1795 if (temp->step_td != 0) {
1796 td_first->td_trb[0].dwTrb3 &= ~htole32(XHCI_TRB_3_CYCLE_BIT);
1797 usb_pc_cpu_flush(td_first->page_cache);
1800 /* clear TD SIZE to zero, hence this is the last TRB */
1801 /* remove chain bit because this is the last TRB in the chain */
1802 td->td_trb[td->ntrb - 1].dwTrb2 &= ~htole32(XHCI_TRB_2_TDSZ_SET(15));
1803 td->td_trb[td->ntrb - 1].dwTrb3 &= ~htole32(XHCI_TRB_3_CHAIN_BIT);
1805 usb_pc_cpu_flush(td->page_cache);
1808 temp->td_next = td_next;
1812 xhci_setup_generic_chain(struct usb_xfer *xfer)
1814 struct xhci_std_temp temp;
1820 temp.do_isoc_sync = 0;
1824 temp.average = xfer->max_hc_frame_size;
1825 temp.max_packet_size = xfer->max_packet_size;
1826 temp.sc = XHCI_BUS2SC(xfer->xroot->bus);
1828 temp.last_frame = 0;
1830 temp.multishort = xfer->flags_int.isochronous_xfr ||
1831 xfer->flags_int.control_xfr ||
1832 xfer->flags_int.short_frames_ok;
1834 /* toggle the DMA set we are using */
1835 xfer->flags_int.curr_dma_set ^= 1;
1837 /* get next DMA set */
1838 td = xfer->td_start[xfer->flags_int.curr_dma_set];
1843 xfer->td_transfer_first = td;
1844 xfer->td_transfer_cache = td;
1846 if (xfer->flags_int.isochronous_xfr) {
1849 /* compute multiplier for ISOCHRONOUS transfers */
1850 mult = xfer->endpoint->ecomp ?
1851 (xfer->endpoint->ecomp->bmAttributes & 3) : 0;
1852 /* check for USB 2.0 multiplier */
1854 mult = (xfer->endpoint->edesc->
1855 wMaxPacketSize[1] >> 3) & 3;
1863 x = XREAD4(temp.sc, runt, XHCI_MFINDEX);
1865 DPRINTF("MFINDEX=0x%08x\n", x);
1867 switch (usbd_get_speed(xfer->xroot->udev)) {
1868 case USB_SPEED_FULL:
1870 temp.isoc_delta = 8; /* 1ms */
1871 x += temp.isoc_delta - 1;
1872 x &= ~(temp.isoc_delta - 1);
1875 shift = usbd_xfer_get_fps_shift(xfer);
1876 temp.isoc_delta = 1U << shift;
1877 x += temp.isoc_delta - 1;
1878 x &= ~(temp.isoc_delta - 1);
1879 /* simple frame load balancing */
1880 x += xfer->endpoint->usb_uframe;
1884 y = XHCI_MFINDEX_GET(x - xfer->endpoint->isoc_next);
1886 if ((xfer->endpoint->is_synced == 0) ||
1887 (y < (xfer->nframes << shift)) ||
1888 (XHCI_MFINDEX_GET(-y) >= (128 * 8))) {
1890 * If there is data underflow or the pipe
1891 * queue is empty we schedule the transfer a
1892 * few frames ahead of the current frame
1893 * position. Else two isochronous transfers
1896 xfer->endpoint->isoc_next = XHCI_MFINDEX_GET(x + (3 * 8));
1897 xfer->endpoint->is_synced = 1;
1898 temp.do_isoc_sync = 1;
1900 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
1903 /* compute isochronous completion time */
1905 y = XHCI_MFINDEX_GET(xfer->endpoint->isoc_next - (x & ~7));
1907 xfer->isoc_time_complete =
1908 usb_isoc_time_expand(&temp.sc->sc_bus, x / 8) +
1909 (y / 8) + (((xfer->nframes << shift) + 7) / 8);
1912 temp.isoc_frame = xfer->endpoint->isoc_next;
1913 temp.trb_type = XHCI_TRB_TYPE_ISOCH;
1915 xfer->endpoint->isoc_next += xfer->nframes << shift;
1917 } else if (xfer->flags_int.control_xfr) {
1919 /* check if we should prepend a setup message */
1921 if (xfer->flags_int.control_hdr) {
1923 temp.len = xfer->frlengths[0];
1924 temp.pc = xfer->frbuffers + 0;
1925 temp.shortpkt = temp.len ? 1 : 0;
1926 temp.trb_type = XHCI_TRB_TYPE_SETUP_STAGE;
1929 /* check for last frame */
1930 if (xfer->nframes == 1) {
1931 /* no STATUS stage yet, SETUP is last */
1932 if (xfer->flags_int.control_act)
1933 temp.last_frame = 1;
1936 xhci_setup_generic_chain_sub(&temp);
1940 temp.isoc_delta = 0;
1941 temp.isoc_frame = 0;
1942 temp.trb_type = XHCI_TRB_TYPE_DATA_STAGE;
1946 temp.isoc_delta = 0;
1947 temp.isoc_frame = 0;
1948 temp.trb_type = XHCI_TRB_TYPE_NORMAL;
1951 if (x != xfer->nframes) {
1952 /* setup page_cache pointer */
1953 temp.pc = xfer->frbuffers + x;
1954 /* set endpoint direction */
1955 temp.direction = UE_GET_DIR(xfer->endpointno);
1958 while (x != xfer->nframes) {
1960 /* DATA0 / DATA1 message */
1962 temp.len = xfer->frlengths[x];
1963 temp.step_td = ((xfer->endpointno & UE_DIR_IN) &&
1964 x != 0 && temp.multishort == 0);
1968 if (x == xfer->nframes) {
1969 if (xfer->flags_int.control_xfr) {
1970 /* no STATUS stage yet, DATA is last */
1971 if (xfer->flags_int.control_act)
1972 temp.last_frame = 1;
1974 temp.last_frame = 1;
1977 if (temp.len == 0) {
1979 /* make sure that we send an USB packet */
1984 temp.tlbpc = mult - 1;
1986 } else if (xfer->flags_int.isochronous_xfr) {
1991 * Isochronous transfers don't have short
1992 * packet termination:
1997 /* isochronous transfers have a transfer limit */
1999 if (temp.len > xfer->max_frame_size)
2000 temp.len = xfer->max_frame_size;
2002 /* compute TD packet count */
2003 tdpc = (temp.len + xfer->max_packet_size - 1) /
2004 xfer->max_packet_size;
2006 temp.tbc = ((tdpc + mult - 1) / mult) - 1;
2007 temp.tlbpc = (tdpc % mult);
2009 if (temp.tlbpc == 0)
2010 temp.tlbpc = mult - 1;
2015 /* regular data transfer */
2017 temp.shortpkt = xfer->flags.force_short_xfer ? 0 : 1;
2020 xhci_setup_generic_chain_sub(&temp);
2022 if (xfer->flags_int.isochronous_xfr) {
2023 temp.offset += xfer->frlengths[x - 1];
2024 temp.isoc_frame += temp.isoc_delta;
2026 /* get next Page Cache pointer */
2027 temp.pc = xfer->frbuffers + x;
2031 /* check if we should append a status stage */
2033 if (xfer->flags_int.control_xfr &&
2034 !xfer->flags_int.control_act) {
2037 * Send a DATA1 message and invert the current
2038 * endpoint direction.
2040 temp.step_td = (xfer->nframes != 0);
2041 temp.direction = UE_GET_DIR(xfer->endpointno) ^ UE_DIR_IN;
2045 temp.last_frame = 1;
2046 temp.trb_type = XHCI_TRB_TYPE_STATUS_STAGE;
2048 xhci_setup_generic_chain_sub(&temp);
2053 /* must have at least one frame! */
2055 xfer->td_transfer_last = td;
2057 DPRINTF("first=%p last=%p\n", xfer->td_transfer_first, td);
2061 xhci_set_slot_pointer(struct xhci_softc *sc, uint8_t index, uint64_t dev_addr)
2063 struct usb_page_search buf_res;
2064 struct xhci_dev_ctx_addr *pdctxa;
2066 usbd_get_page(&sc->sc_hw.ctx_pc, 0, &buf_res);
2068 pdctxa = buf_res.buffer;
2070 DPRINTF("addr[%u]=0x%016llx\n", index, (long long)dev_addr);
2072 pdctxa->qwBaaDevCtxAddr[index] = htole64(dev_addr);
2074 usb_pc_cpu_flush(&sc->sc_hw.ctx_pc);
2078 xhci_configure_mask(struct usb_device *udev, uint32_t mask, uint8_t drop)
2080 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2081 struct usb_page_search buf_inp;
2082 struct xhci_input_dev_ctx *pinp;
2087 index = udev->controller_slot_id;
2089 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
2091 pinp = buf_inp.buffer;
2094 mask &= XHCI_INCTX_NON_CTRL_MASK;
2095 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx0, mask);
2096 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx1, 0);
2098 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx0, 0);
2099 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx1, mask);
2101 /* find most significant set bit */
2102 for (x = 31; x != 1; x--) {
2103 if (mask & (1 << x))
2110 /* figure out maximum */
2111 if (x > sc->sc_hw.devs[index].context_num) {
2112 sc->sc_hw.devs[index].context_num = x;
2113 temp = xhci_ctx_get_le32(sc, &pinp->ctx_slot.dwSctx0);
2114 temp &= ~XHCI_SCTX_0_CTX_NUM_SET(31);
2115 temp |= XHCI_SCTX_0_CTX_NUM_SET(x + 1);
2116 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx0, temp);
2123 xhci_configure_endpoint(struct usb_device *udev,
2124 struct usb_endpoint_descriptor *edesc, uint64_t ring_addr,
2125 uint16_t interval, uint8_t max_packet_count, uint8_t mult,
2126 uint8_t fps_shift, uint16_t max_packet_size, uint16_t max_frame_size)
2128 struct usb_page_search buf_inp;
2129 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2130 struct xhci_input_dev_ctx *pinp;
2136 index = udev->controller_slot_id;
2138 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
2140 pinp = buf_inp.buffer;
2142 epno = edesc->bEndpointAddress;
2143 type = edesc->bmAttributes & UE_XFERTYPE;
2145 if (type == UE_CONTROL)
2148 epno = XHCI_EPNO2EPID(epno);
2151 return (USB_ERR_NO_PIPE); /* invalid */
2153 if (max_packet_count == 0)
2154 return (USB_ERR_BAD_BUFSIZE);
2159 return (USB_ERR_BAD_BUFSIZE);
2161 temp = XHCI_EPCTX_0_EPSTATE_SET(0) |
2162 XHCI_EPCTX_0_MAXP_STREAMS_SET(0) |
2163 XHCI_EPCTX_0_LSA_SET(0);
2165 switch (udev->speed) {
2166 case USB_SPEED_FULL:
2179 temp |= XHCI_EPCTX_0_IVAL_SET(fps_shift);
2181 case UE_ISOCHRONOUS:
2182 temp |= XHCI_EPCTX_0_IVAL_SET(fps_shift);
2184 switch (udev->speed) {
2185 case USB_SPEED_SUPER:
2188 temp |= XHCI_EPCTX_0_MULT_SET(mult - 1);
2189 max_packet_count /= mult;
2199 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx0, temp);
2202 XHCI_EPCTX_1_HID_SET(0) |
2203 XHCI_EPCTX_1_MAXB_SET(max_packet_count) |
2204 XHCI_EPCTX_1_MAXP_SIZE_SET(max_packet_size);
2206 if ((udev->parent_hs_hub != NULL) || (udev->address != 0)) {
2207 if (type != UE_ISOCHRONOUS)
2208 temp |= XHCI_EPCTX_1_CERR_SET(3);
2213 temp |= XHCI_EPCTX_1_EPTYPE_SET(4);
2215 case UE_ISOCHRONOUS:
2216 temp |= XHCI_EPCTX_1_EPTYPE_SET(1);
2219 temp |= XHCI_EPCTX_1_EPTYPE_SET(2);
2222 temp |= XHCI_EPCTX_1_EPTYPE_SET(3);
2226 /* check for IN direction */
2228 temp |= XHCI_EPCTX_1_EPTYPE_SET(4);
2230 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx1, temp);
2232 ring_addr |= XHCI_EPCTX_2_DCS_SET(1);
2234 xhci_ctx_set_le64(sc, &pinp->ctx_ep[epno - 1].qwEpCtx2, ring_addr);
2236 switch (edesc->bmAttributes & UE_XFERTYPE) {
2238 case UE_ISOCHRONOUS:
2239 temp = XHCI_EPCTX_4_MAX_ESIT_PAYLOAD_SET(max_frame_size) |
2240 XHCI_EPCTX_4_AVG_TRB_LEN_SET(MIN(XHCI_PAGE_SIZE,
2244 temp = XHCI_EPCTX_4_AVG_TRB_LEN_SET(8);
2247 temp = XHCI_EPCTX_4_AVG_TRB_LEN_SET(XHCI_PAGE_SIZE);
2251 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx4, temp);
2254 xhci_dump_endpoint(sc, &pinp->ctx_ep[epno - 1]);
2256 usb_pc_cpu_flush(&sc->sc_hw.devs[index].input_pc);
2258 return (0); /* success */
2262 xhci_configure_endpoint_by_xfer(struct usb_xfer *xfer)
2264 struct xhci_endpoint_ext *pepext;
2265 struct usb_endpoint_ss_comp_descriptor *ecomp;
2267 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2268 xfer->endpoint->edesc);
2270 ecomp = xfer->endpoint->ecomp;
2272 pepext->trb[0].dwTrb3 = 0; /* halt any transfers */
2273 usb_pc_cpu_flush(pepext->page_cache);
2275 return (xhci_configure_endpoint(xfer->xroot->udev,
2276 xfer->endpoint->edesc, pepext->physaddr,
2277 xfer->interval, xfer->max_packet_count,
2278 (ecomp != NULL) ? (ecomp->bmAttributes & 3) + 1 : 1,
2279 usbd_xfer_get_fps_shift(xfer), xfer->max_packet_size,
2280 xfer->max_frame_size));
2284 xhci_configure_device(struct usb_device *udev)
2286 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2287 struct usb_page_search buf_inp;
2288 struct usb_page_cache *pcinp;
2289 struct xhci_input_dev_ctx *pinp;
2290 struct usb_device *hubdev;
2298 index = udev->controller_slot_id;
2300 DPRINTF("index=%u\n", index);
2302 pcinp = &sc->sc_hw.devs[index].input_pc;
2304 usbd_get_page(pcinp, 0, &buf_inp);
2306 pinp = buf_inp.buffer;
2311 /* figure out route string and root HUB port number */
2313 for (hubdev = udev; hubdev != NULL; hubdev = hubdev->parent_hub) {
2315 if (hubdev->parent_hub == NULL)
2318 depth = hubdev->parent_hub->depth;
2321 * NOTE: HS/FS/LS devices and the SS root HUB can have
2322 * more than 15 ports
2325 rh_port = hubdev->port_no;
2334 route |= rh_port << (4 * (depth - 1));
2337 DPRINTF("Route=0x%08x\n", route);
2339 temp = XHCI_SCTX_0_ROUTE_SET(route) |
2340 XHCI_SCTX_0_CTX_NUM_SET(
2341 sc->sc_hw.devs[index].context_num + 1);
2343 switch (udev->speed) {
2345 temp |= XHCI_SCTX_0_SPEED_SET(2);
2346 if (udev->parent_hs_hub != NULL &&
2347 udev->parent_hs_hub->ddesc.bDeviceProtocol ==
2349 DPRINTF("Device inherits MTT\n");
2350 temp |= XHCI_SCTX_0_MTT_SET(1);
2353 case USB_SPEED_HIGH:
2354 temp |= XHCI_SCTX_0_SPEED_SET(3);
2355 if (sc->sc_hw.devs[index].nports != 0 &&
2356 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) {
2357 DPRINTF("HUB supports MTT\n");
2358 temp |= XHCI_SCTX_0_MTT_SET(1);
2361 case USB_SPEED_FULL:
2362 temp |= XHCI_SCTX_0_SPEED_SET(1);
2363 if (udev->parent_hs_hub != NULL &&
2364 udev->parent_hs_hub->ddesc.bDeviceProtocol ==
2366 DPRINTF("Device inherits MTT\n");
2367 temp |= XHCI_SCTX_0_MTT_SET(1);
2371 temp |= XHCI_SCTX_0_SPEED_SET(4);
2375 is_hub = sc->sc_hw.devs[index].nports != 0 &&
2376 (udev->speed == USB_SPEED_SUPER ||
2377 udev->speed == USB_SPEED_HIGH);
2380 temp |= XHCI_SCTX_0_HUB_SET(1);
2382 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx0, temp);
2384 temp = XHCI_SCTX_1_RH_PORT_SET(rh_port);
2387 temp |= XHCI_SCTX_1_NUM_PORTS_SET(
2388 sc->sc_hw.devs[index].nports);
2391 switch (udev->speed) {
2392 case USB_SPEED_SUPER:
2393 switch (sc->sc_hw.devs[index].state) {
2394 case XHCI_ST_ADDRESSED:
2395 case XHCI_ST_CONFIGURED:
2396 /* enable power save */
2397 temp |= XHCI_SCTX_1_MAX_EL_SET(sc->sc_exit_lat_max);
2400 /* disable power save */
2408 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx1, temp);
2410 temp = XHCI_SCTX_2_IRQ_TARGET_SET(0);
2413 temp |= XHCI_SCTX_2_TT_THINK_TIME_SET(
2414 sc->sc_hw.devs[index].tt);
2417 hubdev = udev->parent_hs_hub;
2419 /* check if we should activate the transaction translator */
2420 switch (udev->speed) {
2421 case USB_SPEED_FULL:
2423 if (hubdev != NULL) {
2424 temp |= XHCI_SCTX_2_TT_HUB_SID_SET(
2425 hubdev->controller_slot_id);
2426 temp |= XHCI_SCTX_2_TT_PORT_NUM_SET(
2434 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx2, temp);
2436 temp = XHCI_SCTX_3_DEV_ADDR_SET(udev->address) |
2437 XHCI_SCTX_3_SLOT_STATE_SET(0);
2439 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx3, temp);
2442 xhci_dump_device(sc, &pinp->ctx_slot);
2444 usb_pc_cpu_flush(pcinp);
2446 return (0); /* success */
2450 xhci_alloc_device_ext(struct usb_device *udev)
2452 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2453 struct usb_page_search buf_dev;
2454 struct usb_page_search buf_ep;
2455 struct xhci_trb *trb;
2456 struct usb_page_cache *pc;
2457 struct usb_page *pg;
2462 index = udev->controller_slot_id;
2464 pc = &sc->sc_hw.devs[index].device_pc;
2465 pg = &sc->sc_hw.devs[index].device_pg;
2467 /* need to initialize the page cache */
2468 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2470 if (usb_pc_alloc_mem(pc, pg, sc->sc_ctx_is_64_byte ?
2471 (2 * sizeof(struct xhci_dev_ctx)) :
2472 sizeof(struct xhci_dev_ctx), XHCI_PAGE_SIZE))
2475 usbd_get_page(pc, 0, &buf_dev);
2477 pc = &sc->sc_hw.devs[index].input_pc;
2478 pg = &sc->sc_hw.devs[index].input_pg;
2480 /* need to initialize the page cache */
2481 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2483 if (usb_pc_alloc_mem(pc, pg, sc->sc_ctx_is_64_byte ?
2484 (2 * sizeof(struct xhci_input_dev_ctx)) :
2485 sizeof(struct xhci_input_dev_ctx), XHCI_PAGE_SIZE)) {
2489 pc = &sc->sc_hw.devs[index].endpoint_pc;
2490 pg = &sc->sc_hw.devs[index].endpoint_pg;
2492 /* need to initialize the page cache */
2493 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2495 if (usb_pc_alloc_mem(pc, pg,
2496 sizeof(struct xhci_dev_endpoint_trbs), XHCI_PAGE_SIZE)) {
2500 /* initialise all endpoint LINK TRBs */
2502 for (i = 0; i != XHCI_MAX_ENDPOINTS; i++) {
2504 /* lookup endpoint TRB ring */
2505 usbd_get_page(pc, (uintptr_t)&
2506 ((struct xhci_dev_endpoint_trbs *)0)->trb[i][0], &buf_ep);
2508 /* get TRB pointer */
2509 trb = buf_ep.buffer;
2510 trb += XHCI_MAX_TRANSFERS - 1;
2512 /* get TRB start address */
2513 addr = buf_ep.physaddr;
2515 /* create LINK TRB */
2516 trb->qwTrb0 = htole64(addr);
2517 trb->dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
2518 trb->dwTrb3 = htole32(XHCI_TRB_3_CYCLE_BIT |
2519 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
2522 usb_pc_cpu_flush(pc);
2524 xhci_set_slot_pointer(sc, index, buf_dev.physaddr);
2529 xhci_free_device_ext(udev);
2531 return (USB_ERR_NOMEM);
2535 xhci_free_device_ext(struct usb_device *udev)
2537 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2540 index = udev->controller_slot_id;
2541 xhci_set_slot_pointer(sc, index, 0);
2543 usb_pc_free_mem(&sc->sc_hw.devs[index].device_pc);
2544 usb_pc_free_mem(&sc->sc_hw.devs[index].input_pc);
2545 usb_pc_free_mem(&sc->sc_hw.devs[index].endpoint_pc);
2548 static struct xhci_endpoint_ext *
2549 xhci_get_endpoint_ext(struct usb_device *udev, struct usb_endpoint_descriptor *edesc)
2551 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2552 struct xhci_endpoint_ext *pepext;
2553 struct usb_page_cache *pc;
2554 struct usb_page_search buf_ep;
2558 epno = edesc->bEndpointAddress;
2559 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL)
2562 epno = XHCI_EPNO2EPID(epno);
2564 index = udev->controller_slot_id;
2566 pc = &sc->sc_hw.devs[index].endpoint_pc;
2568 usbd_get_page(pc, (uintptr_t)&((struct xhci_dev_endpoint_trbs *)0)->trb[epno][0], &buf_ep);
2570 pepext = &sc->sc_hw.devs[index].endp[epno];
2571 pepext->page_cache = pc;
2572 pepext->trb = buf_ep.buffer;
2573 pepext->physaddr = buf_ep.physaddr;
2579 xhci_endpoint_doorbell(struct usb_xfer *xfer)
2581 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
2585 epno = xfer->endpointno;
2586 if (xfer->flags_int.control_xfr)
2589 epno = XHCI_EPNO2EPID(epno);
2590 index = xfer->xroot->udev->controller_slot_id;
2592 if (xfer->xroot->udev->flags.self_suspended == 0) {
2593 XWRITE4(sc, door, XHCI_DOORBELL(index),
2594 epno | XHCI_DB_SID_SET(/*xfer->stream_id*/ 0));
2599 xhci_transfer_remove(struct usb_xfer *xfer, usb_error_t error)
2601 struct xhci_endpoint_ext *pepext;
2603 if (xfer->flags_int.bandwidth_reclaimed) {
2604 xfer->flags_int.bandwidth_reclaimed = 0;
2606 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2607 xfer->endpoint->edesc);
2611 pepext->xfer[xfer->qh_pos] = NULL;
2613 if (error && pepext->trb_running != 0) {
2614 pepext->trb_halted = 1;
2615 pepext->trb_running = 0;
2621 xhci_transfer_insert(struct usb_xfer *xfer)
2623 struct xhci_td *td_first;
2624 struct xhci_td *td_last;
2625 struct xhci_trb *trb_link;
2626 struct xhci_endpoint_ext *pepext;
2634 /* check if already inserted */
2635 if (xfer->flags_int.bandwidth_reclaimed) {
2636 DPRINTFN(8, "Already in schedule\n");
2640 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2641 xfer->endpoint->edesc);
2643 td_first = xfer->td_transfer_first;
2644 td_last = xfer->td_transfer_last;
2645 addr = pepext->physaddr;
2647 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
2650 /* single buffered */
2654 /* multi buffered */
2655 trb_limit = (XHCI_MAX_TRANSFERS - 2);
2659 if (pepext->trb_used >= trb_limit) {
2660 DPRINTFN(8, "Too many TDs queued.\n");
2661 return (USB_ERR_NOMEM);
2664 /* check for stopped condition, after putting transfer on interrupt queue */
2665 if (pepext->trb_running == 0) {
2666 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
2668 DPRINTFN(8, "Not running\n");
2670 /* start configuration */
2671 (void)usb_proc_msignal(&sc->sc_config_proc,
2672 &sc->sc_config_msg[0], &sc->sc_config_msg[1]);
2678 /* get current TRB index */
2679 i = pepext->trb_index;
2681 /* get next TRB index */
2684 /* the last entry of the ring is a hardcoded link TRB */
2685 if (inext >= (XHCI_MAX_TRANSFERS - 1))
2688 /* compute terminating return address */
2689 addr += inext * sizeof(struct xhci_trb);
2691 /* compute link TRB pointer */
2692 trb_link = td_last->td_trb + td_last->ntrb;
2694 /* update next pointer of last link TRB */
2695 trb_link->qwTrb0 = htole64(addr);
2696 trb_link->dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
2697 trb_link->dwTrb3 = htole32(XHCI_TRB_3_IOC_BIT |
2698 XHCI_TRB_3_CYCLE_BIT |
2699 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
2702 xhci_dump_trb(&td_last->td_trb[td_last->ntrb]);
2704 usb_pc_cpu_flush(td_last->page_cache);
2706 /* write ahead chain end marker */
2708 pepext->trb[inext].qwTrb0 = 0;
2709 pepext->trb[inext].dwTrb2 = 0;
2710 pepext->trb[inext].dwTrb3 = 0;
2712 /* update next pointer of link TRB */
2714 pepext->trb[i].qwTrb0 = htole64((uint64_t)td_first->td_self);
2715 pepext->trb[i].dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
2718 xhci_dump_trb(&pepext->trb[i]);
2720 usb_pc_cpu_flush(pepext->page_cache);
2722 /* toggle cycle bit which activates the transfer chain */
2724 pepext->trb[i].dwTrb3 = htole32(XHCI_TRB_3_CYCLE_BIT |
2725 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
2727 usb_pc_cpu_flush(pepext->page_cache);
2729 DPRINTF("qh_pos = %u\n", i);
2731 pepext->xfer[i] = xfer;
2735 xfer->flags_int.bandwidth_reclaimed = 1;
2737 pepext->trb_index = inext;
2739 xhci_endpoint_doorbell(xfer);
2745 xhci_root_intr(struct xhci_softc *sc)
2749 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2751 /* clear any old interrupt data */
2752 memset(sc->sc_hub_idata, 0, sizeof(sc->sc_hub_idata));
2754 for (i = 1; i <= sc->sc_noport; i++) {
2755 /* pick out CHANGE bits from the status register */
2756 if (XREAD4(sc, oper, XHCI_PORTSC(i)) & (
2757 XHCI_PS_CSC | XHCI_PS_PEC |
2758 XHCI_PS_OCC | XHCI_PS_WRC |
2759 XHCI_PS_PRC | XHCI_PS_PLC |
2761 sc->sc_hub_idata[i / 8] |= 1 << (i % 8);
2762 DPRINTF("port %d changed\n", i);
2765 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
2766 sizeof(sc->sc_hub_idata));
2769 /*------------------------------------------------------------------------*
2770 * xhci_device_done - XHCI done handler
2772 * NOTE: This function can be called two times in a row on
2773 * the same USB transfer. From close and from interrupt.
2774 *------------------------------------------------------------------------*/
2776 xhci_device_done(struct usb_xfer *xfer, usb_error_t error)
2778 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
2779 xfer, xfer->endpoint, error);
2781 /* remove transfer from HW queue */
2782 xhci_transfer_remove(xfer, error);
2784 /* dequeue transfer and start next transfer */
2785 usbd_transfer_done(xfer, error);
2788 /*------------------------------------------------------------------------*
2789 * XHCI data transfer support (generic type)
2790 *------------------------------------------------------------------------*/
2792 xhci_device_generic_open(struct usb_xfer *xfer)
2794 if (xfer->flags_int.isochronous_xfr) {
2795 switch (xfer->xroot->udev->speed) {
2796 case USB_SPEED_FULL:
2799 usb_hs_bandwidth_alloc(xfer);
2806 xhci_device_generic_close(struct usb_xfer *xfer)
2810 xhci_device_done(xfer, USB_ERR_CANCELLED);
2812 if (xfer->flags_int.isochronous_xfr) {
2813 switch (xfer->xroot->udev->speed) {
2814 case USB_SPEED_FULL:
2817 usb_hs_bandwidth_free(xfer);
2824 xhci_device_generic_multi_enter(struct usb_endpoint *ep,
2825 struct usb_xfer *enter_xfer)
2827 struct usb_xfer *xfer;
2829 /* check if there is a current transfer */
2830 xfer = ep->endpoint_q.curr;
2835 * Check if the current transfer is started and then pickup
2836 * the next one, if any. Else wait for next start event due to
2837 * block on failure feature.
2839 if (!xfer->flags_int.bandwidth_reclaimed)
2842 xfer = TAILQ_FIRST(&ep->endpoint_q.head);
2845 * In case of enter we have to consider that the
2846 * transfer is queued by the USB core after the enter
2855 /* try to multi buffer */
2856 xhci_transfer_insert(xfer);
2860 xhci_device_generic_enter(struct usb_xfer *xfer)
2864 /* setup TD's and QH */
2865 xhci_setup_generic_chain(xfer);
2867 xhci_device_generic_multi_enter(xfer->endpoint, xfer);
2871 xhci_device_generic_start(struct usb_xfer *xfer)
2875 /* try to insert xfer on HW queue */
2876 xhci_transfer_insert(xfer);
2878 /* try to multi buffer */
2879 xhci_device_generic_multi_enter(xfer->endpoint, NULL);
2881 /* add transfer last on interrupt queue */
2882 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
2884 /* start timeout, if any */
2885 if (xfer->timeout != 0)
2886 usbd_transfer_timeout_ms(xfer, &xhci_timeout, xfer->timeout);
2889 struct usb_pipe_methods xhci_device_generic_methods =
2891 .open = xhci_device_generic_open,
2892 .close = xhci_device_generic_close,
2893 .enter = xhci_device_generic_enter,
2894 .start = xhci_device_generic_start,
2897 /*------------------------------------------------------------------------*
2898 * xhci root HUB support
2899 *------------------------------------------------------------------------*
2900 * Simulate a hardware HUB by handling all the necessary requests.
2901 *------------------------------------------------------------------------*/
2903 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
2906 struct usb_device_descriptor xhci_devd =
2908 .bLength = sizeof(xhci_devd),
2909 .bDescriptorType = UDESC_DEVICE, /* type */
2910 HSETW(.bcdUSB, 0x0300), /* USB version */
2911 .bDeviceClass = UDCLASS_HUB, /* class */
2912 .bDeviceSubClass = UDSUBCLASS_HUB, /* subclass */
2913 .bDeviceProtocol = UDPROTO_SSHUB, /* protocol */
2914 .bMaxPacketSize = 9, /* max packet size */
2915 HSETW(.idVendor, 0x0000), /* vendor */
2916 HSETW(.idProduct, 0x0000), /* product */
2917 HSETW(.bcdDevice, 0x0100), /* device version */
2921 .bNumConfigurations = 1, /* # of configurations */
2925 struct xhci_bos_desc xhci_bosd = {
2927 .bLength = sizeof(xhci_bosd.bosd),
2928 .bDescriptorType = UDESC_BOS,
2929 HSETW(.wTotalLength, sizeof(xhci_bosd)),
2930 .bNumDeviceCaps = 3,
2933 .bLength = sizeof(xhci_bosd.usb2extd),
2934 .bDescriptorType = 1,
2935 .bDevCapabilityType = 2,
2936 .bmAttributes[0] = 2,
2939 .bLength = sizeof(xhci_bosd.usbdcd),
2940 .bDescriptorType = UDESC_DEVICE_CAPABILITY,
2941 .bDevCapabilityType = 3,
2942 .bmAttributes = 0, /* XXX */
2943 HSETW(.wSpeedsSupported, 0x000C),
2944 .bFunctionalitySupport = 8,
2945 .bU1DevExitLat = 255, /* dummy - not used */
2946 .wU2DevExitLat = { 0x00, 0x08 },
2949 .bLength = sizeof(xhci_bosd.cidd),
2950 .bDescriptorType = 1,
2951 .bDevCapabilityType = 4,
2953 .bContainerID = 0, /* XXX */
2958 struct xhci_config_desc xhci_confd = {
2960 .bLength = sizeof(xhci_confd.confd),
2961 .bDescriptorType = UDESC_CONFIG,
2962 .wTotalLength[0] = sizeof(xhci_confd),
2964 .bConfigurationValue = 1,
2965 .iConfiguration = 0,
2966 .bmAttributes = UC_SELF_POWERED,
2967 .bMaxPower = 0 /* max power */
2970 .bLength = sizeof(xhci_confd.ifcd),
2971 .bDescriptorType = UDESC_INTERFACE,
2973 .bInterfaceClass = UICLASS_HUB,
2974 .bInterfaceSubClass = UISUBCLASS_HUB,
2975 .bInterfaceProtocol = 0,
2978 .bLength = sizeof(xhci_confd.endpd),
2979 .bDescriptorType = UDESC_ENDPOINT,
2980 .bEndpointAddress = UE_DIR_IN | XHCI_INTR_ENDPT,
2981 .bmAttributes = UE_INTERRUPT,
2982 .wMaxPacketSize[0] = 2, /* max 15 ports */
2986 .bLength = sizeof(xhci_confd.endpcd),
2987 .bDescriptorType = UDESC_ENDPOINT_SS_COMP,
2994 struct usb_hub_ss_descriptor xhci_hubd = {
2995 .bLength = sizeof(xhci_hubd),
2996 .bDescriptorType = UDESC_SS_HUB,
3000 xhci_roothub_exec(struct usb_device *udev,
3001 struct usb_device_request *req, const void **pptr, uint16_t *plength)
3003 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3004 const char *str_ptr;
3015 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
3018 ptr = (const void *)&sc->sc_hub_desc;
3022 value = UGETW(req->wValue);
3023 index = UGETW(req->wIndex);
3025 DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
3026 "wValue=0x%04x wIndex=0x%04x\n",
3027 req->bmRequestType, req->bRequest,
3028 UGETW(req->wLength), value, index);
3030 #define C(x,y) ((x) | ((y) << 8))
3031 switch (C(req->bRequest, req->bmRequestType)) {
3032 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
3033 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
3034 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
3036 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
3037 * for the integrated root hub.
3040 case C(UR_GET_CONFIG, UT_READ_DEVICE):
3042 sc->sc_hub_desc.temp[0] = sc->sc_conf;
3044 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
3045 switch (value >> 8) {
3047 if ((value & 0xff) != 0) {
3048 err = USB_ERR_IOERROR;
3051 len = sizeof(xhci_devd);
3052 ptr = (const void *)&xhci_devd;
3056 if ((value & 0xff) != 0) {
3057 err = USB_ERR_IOERROR;
3060 len = sizeof(xhci_bosd);
3061 ptr = (const void *)&xhci_bosd;
3065 if ((value & 0xff) != 0) {
3066 err = USB_ERR_IOERROR;
3069 len = sizeof(xhci_confd);
3070 ptr = (const void *)&xhci_confd;
3074 switch (value & 0xff) {
3075 case 0: /* Language table */
3079 case 1: /* Vendor */
3080 str_ptr = sc->sc_vendor;
3083 case 2: /* Product */
3084 str_ptr = "XHCI root HUB";
3092 len = usb_make_str_desc(
3093 sc->sc_hub_desc.temp,
3094 sizeof(sc->sc_hub_desc.temp),
3099 err = USB_ERR_IOERROR;
3103 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3105 sc->sc_hub_desc.temp[0] = 0;
3107 case C(UR_GET_STATUS, UT_READ_DEVICE):
3109 USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
3111 case C(UR_GET_STATUS, UT_READ_INTERFACE):
3112 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3114 USETW(sc->sc_hub_desc.stat.wStatus, 0);
3116 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3117 if (value >= XHCI_MAX_DEVICES) {
3118 err = USB_ERR_IOERROR;
3122 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
3123 if (value != 0 && value != 1) {
3124 err = USB_ERR_IOERROR;
3127 sc->sc_conf = value;
3129 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
3131 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
3132 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
3133 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3134 err = USB_ERR_IOERROR;
3136 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
3138 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
3141 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3143 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3144 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE\n");
3147 (index > sc->sc_noport)) {
3148 err = USB_ERR_IOERROR;
3151 port = XHCI_PORTSC(index);
3153 v = XREAD4(sc, oper, port);
3154 i = XHCI_PS_PLS_GET(v);
3155 v &= ~XHCI_PS_CLEAR;
3158 case UHF_C_BH_PORT_RESET:
3159 XWRITE4(sc, oper, port, v | XHCI_PS_WRC);
3161 case UHF_C_PORT_CONFIG_ERROR:
3162 XWRITE4(sc, oper, port, v | XHCI_PS_CEC);
3164 case UHF_C_PORT_SUSPEND:
3165 case UHF_C_PORT_LINK_STATE:
3166 XWRITE4(sc, oper, port, v | XHCI_PS_PLC);
3168 case UHF_C_PORT_CONNECTION:
3169 XWRITE4(sc, oper, port, v | XHCI_PS_CSC);
3171 case UHF_C_PORT_ENABLE:
3172 XWRITE4(sc, oper, port, v | XHCI_PS_PEC);
3174 case UHF_C_PORT_OVER_CURRENT:
3175 XWRITE4(sc, oper, port, v | XHCI_PS_OCC);
3177 case UHF_C_PORT_RESET:
3178 XWRITE4(sc, oper, port, v | XHCI_PS_PRC);
3180 case UHF_PORT_ENABLE:
3181 XWRITE4(sc, oper, port, v | XHCI_PS_PED);
3183 case UHF_PORT_POWER:
3184 XWRITE4(sc, oper, port, v & ~XHCI_PS_PP);
3186 case UHF_PORT_INDICATOR:
3187 XWRITE4(sc, oper, port, v & ~XHCI_PS_PIC_SET(3));
3189 case UHF_PORT_SUSPEND:
3193 XWRITE4(sc, oper, port, v |
3194 XHCI_PS_PLS_SET(0xF) | XHCI_PS_LWS);
3197 /* wait 20ms for resume sequence to complete */
3198 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
3201 XWRITE4(sc, oper, port, v |
3202 XHCI_PS_PLS_SET(0) | XHCI_PS_LWS);
3205 err = USB_ERR_IOERROR;
3210 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3211 if ((value & 0xff) != 0) {
3212 err = USB_ERR_IOERROR;
3216 v = XREAD4(sc, capa, XHCI_HCSPARAMS0);
3218 sc->sc_hub_desc.hubd = xhci_hubd;
3220 sc->sc_hub_desc.hubd.bNbrPorts = sc->sc_noport;
3222 if (XHCI_HCS0_PPC(v))
3223 i = UHD_PWR_INDIVIDUAL;
3227 if (XHCI_HCS0_PIND(v))
3230 i |= UHD_OC_INDIVIDUAL;
3232 USETW(sc->sc_hub_desc.hubd.wHubCharacteristics, i);
3234 /* see XHCI section 5.4.9: */
3235 sc->sc_hub_desc.hubd.bPwrOn2PwrGood = 10;
3237 for (j = 1; j <= sc->sc_noport; j++) {
3239 v = XREAD4(sc, oper, XHCI_PORTSC(j));
3240 if (v & XHCI_PS_DR) {
3241 sc->sc_hub_desc.hubd.
3242 DeviceRemovable[j / 8] |= 1U << (j % 8);
3245 len = sc->sc_hub_desc.hubd.bLength;
3248 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3250 memset(sc->sc_hub_desc.temp, 0, 16);
3253 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3254 DPRINTFN(9, "UR_GET_STATUS i=%d\n", index);
3257 (index > sc->sc_noport)) {
3258 err = USB_ERR_IOERROR;
3262 v = XREAD4(sc, oper, XHCI_PORTSC(index));
3264 DPRINTFN(9, "port status=0x%08x\n", v);
3266 i = UPS_PORT_LINK_STATE_SET(XHCI_PS_PLS_GET(v));
3268 switch (XHCI_PS_SPEED_GET(v)) {
3270 i |= UPS_HIGH_SPEED;
3279 i |= UPS_OTHER_SPEED;
3283 if (v & XHCI_PS_CCS)
3284 i |= UPS_CURRENT_CONNECT_STATUS;
3285 if (v & XHCI_PS_PED)
3286 i |= UPS_PORT_ENABLED;
3287 if (v & XHCI_PS_OCA)
3288 i |= UPS_OVERCURRENT_INDICATOR;
3291 if (v & XHCI_PS_PP) {
3293 * The USB 3.0 RH is using the
3294 * USB 2.0's power bit
3296 i |= UPS_PORT_POWER;
3298 USETW(sc->sc_hub_desc.ps.wPortStatus, i);
3301 if (v & XHCI_PS_CSC)
3302 i |= UPS_C_CONNECT_STATUS;
3303 if (v & XHCI_PS_PEC)
3304 i |= UPS_C_PORT_ENABLED;
3305 if (v & XHCI_PS_OCC)
3306 i |= UPS_C_OVERCURRENT_INDICATOR;
3307 if (v & XHCI_PS_WRC)
3308 i |= UPS_C_BH_PORT_RESET;
3309 if (v & XHCI_PS_PRC)
3310 i |= UPS_C_PORT_RESET;
3311 if (v & XHCI_PS_PLC)
3312 i |= UPS_C_PORT_LINK_STATE;
3313 if (v & XHCI_PS_CEC)
3314 i |= UPS_C_PORT_CONFIG_ERROR;
3316 USETW(sc->sc_hub_desc.ps.wPortChange, i);
3317 len = sizeof(sc->sc_hub_desc.ps);
3320 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3321 err = USB_ERR_IOERROR;
3324 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3327 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3333 (index > sc->sc_noport)) {
3334 err = USB_ERR_IOERROR;
3338 port = XHCI_PORTSC(index);
3339 v = XREAD4(sc, oper, port) & ~XHCI_PS_CLEAR;
3342 case UHF_PORT_U1_TIMEOUT:
3343 if (XHCI_PS_SPEED_GET(v) != 4) {
3344 err = USB_ERR_IOERROR;
3347 port = XHCI_PORTPMSC(index);
3348 v = XREAD4(sc, oper, port);
3349 v &= ~XHCI_PM3_U1TO_SET(0xFF);
3350 v |= XHCI_PM3_U1TO_SET(i);
3351 XWRITE4(sc, oper, port, v);
3353 case UHF_PORT_U2_TIMEOUT:
3354 if (XHCI_PS_SPEED_GET(v) != 4) {
3355 err = USB_ERR_IOERROR;
3358 port = XHCI_PORTPMSC(index);
3359 v = XREAD4(sc, oper, port);
3360 v &= ~XHCI_PM3_U2TO_SET(0xFF);
3361 v |= XHCI_PM3_U2TO_SET(i);
3362 XWRITE4(sc, oper, port, v);
3364 case UHF_BH_PORT_RESET:
3365 XWRITE4(sc, oper, port, v | XHCI_PS_WPR);
3367 case UHF_PORT_LINK_STATE:
3368 XWRITE4(sc, oper, port, v |
3369 XHCI_PS_PLS_SET(i) | XHCI_PS_LWS);
3370 /* 4ms settle time */
3371 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
3373 case UHF_PORT_ENABLE:
3374 DPRINTFN(3, "set port enable %d\n", index);
3376 case UHF_PORT_SUSPEND:
3377 DPRINTFN(6, "suspend port %u (LPM=%u)\n", index, i);
3378 j = XHCI_PS_SPEED_GET(v);
3379 if ((j < 1) || (j > 3)) {
3380 /* non-supported speed */
3381 err = USB_ERR_IOERROR;
3384 XWRITE4(sc, oper, port, v |
3385 XHCI_PS_PLS_SET(i ? 2 /* LPM */ : 3) | XHCI_PS_LWS);
3387 case UHF_PORT_RESET:
3388 DPRINTFN(6, "reset port %d\n", index);
3389 XWRITE4(sc, oper, port, v | XHCI_PS_PR);
3391 case UHF_PORT_POWER:
3392 DPRINTFN(3, "set port power %d\n", index);
3393 XWRITE4(sc, oper, port, v | XHCI_PS_PP);
3396 DPRINTFN(3, "set port test %d\n", index);
3398 case UHF_PORT_INDICATOR:
3399 DPRINTFN(3, "set port indicator %d\n", index);
3401 v &= ~XHCI_PS_PIC_SET(3);
3402 v |= XHCI_PS_PIC_SET(1);
3404 XWRITE4(sc, oper, port, v);
3407 err = USB_ERR_IOERROR;
3412 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
3413 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
3414 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
3415 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
3418 err = USB_ERR_IOERROR;
3428 xhci_xfer_setup(struct usb_setup_params *parm)
3430 struct usb_page_search page_info;
3431 struct usb_page_cache *pc;
3432 struct xhci_softc *sc;
3433 struct usb_xfer *xfer;
3438 sc = XHCI_BUS2SC(parm->udev->bus);
3439 xfer = parm->curr_xfer;
3442 * The proof for the "ntd" formula is illustrated like this:
3444 * +------------------------------------+
3448 * | | xxx | x | frm 0 |
3450 * | | xxx | xx | frm 1 |
3453 * +------------------------------------+
3455 * "xxx" means a completely full USB transfer descriptor
3457 * "x" and "xx" means a short USB packet
3459 * For the remainder of an USB transfer modulo
3460 * "max_data_length" we need two USB transfer descriptors.
3461 * One to transfer the remaining data and one to finalise with
3462 * a zero length packet in case the "force_short_xfer" flag is
3463 * set. We only need two USB transfer descriptors in the case
3464 * where the transfer length of the first one is a factor of
3465 * "max_frame_size". The rest of the needed USB transfer
3466 * descriptors is given by the buffer size divided by the
3467 * maximum data payload.
3469 parm->hc_max_packet_size = 0x400;
3470 parm->hc_max_packet_count = 16 * 3;
3471 parm->hc_max_frame_size = XHCI_TD_PAYLOAD_MAX;
3473 xfer->flags_int.bdma_enable = 1;
3475 usbd_transfer_setup_sub(parm);
3477 if (xfer->flags_int.isochronous_xfr) {
3478 ntd = ((1 * xfer->nframes)
3479 + (xfer->max_data_length / xfer->max_hc_frame_size));
3480 } else if (xfer->flags_int.control_xfr) {
3481 ntd = ((2 * xfer->nframes) + 1 /* STATUS */
3482 + (xfer->max_data_length / xfer->max_hc_frame_size));
3484 ntd = ((2 * xfer->nframes)
3485 + (xfer->max_data_length / xfer->max_hc_frame_size));
3494 * Allocate queue heads and transfer descriptors
3498 if (usbd_transfer_setup_sub_malloc(
3499 parm, &pc, sizeof(struct xhci_td),
3500 XHCI_TD_ALIGN, ntd)) {
3501 parm->err = USB_ERR_NOMEM;
3505 for (n = 0; n != ntd; n++) {
3508 usbd_get_page(pc + n, 0, &page_info);
3510 td = page_info.buffer;
3513 td->td_self = page_info.physaddr;
3514 td->obj_next = last_obj;
3515 td->page_cache = pc + n;
3519 usb_pc_cpu_flush(pc + n);
3522 xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
3524 if (!xfer->flags_int.curr_dma_set) {
3525 xfer->flags_int.curr_dma_set = 1;
3531 xhci_configure_reset_endpoint(struct usb_xfer *xfer)
3533 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
3534 struct usb_page_search buf_inp;
3535 struct usb_device *udev;
3536 struct xhci_endpoint_ext *pepext;
3537 struct usb_endpoint_descriptor *edesc;
3538 struct usb_page_cache *pcinp;
3543 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
3544 xfer->endpoint->edesc);
3546 udev = xfer->xroot->udev;
3547 index = udev->controller_slot_id;
3549 pcinp = &sc->sc_hw.devs[index].input_pc;
3551 usbd_get_page(pcinp, 0, &buf_inp);
3553 edesc = xfer->endpoint->edesc;
3555 epno = edesc->bEndpointAddress;
3557 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL)
3560 epno = XHCI_EPNO2EPID(epno);
3563 return (USB_ERR_NO_PIPE); /* invalid */
3567 /* configure endpoint */
3569 err = xhci_configure_endpoint_by_xfer(xfer);
3572 XHCI_CMD_UNLOCK(sc);
3577 * Get the endpoint into the stopped state according to the
3578 * endpoint context state diagram in the XHCI specification:
3581 err = xhci_cmd_stop_ep(sc, 0, epno, index);
3584 DPRINTF("Could not stop endpoint %u\n", epno);
3586 err = xhci_cmd_reset_ep(sc, 0, epno, index);
3589 DPRINTF("Could not reset endpoint %u\n", epno);
3591 err = xhci_cmd_set_tr_dequeue_ptr(sc, pepext->physaddr |
3592 XHCI_EPCTX_2_DCS_SET(1), 0, epno, index);
3595 DPRINTF("Could not set dequeue ptr for endpoint %u\n", epno);
3598 * Get the endpoint into the running state according to the
3599 * endpoint context state diagram in the XHCI specification:
3602 xhci_configure_mask(udev, (1U << epno) | 1U, 0);
3604 err = xhci_cmd_evaluate_ctx(sc, buf_inp.physaddr, index);
3607 DPRINTF("Could not configure endpoint %u\n", epno);
3609 err = xhci_cmd_configure_ep(sc, buf_inp.physaddr, 0, index);
3612 DPRINTF("Could not configure endpoint %u\n", epno);
3614 XHCI_CMD_UNLOCK(sc);
3620 xhci_xfer_unsetup(struct usb_xfer *xfer)
3626 xhci_start_dma_delay(struct usb_xfer *xfer)
3628 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
3630 /* put transfer on interrupt queue (again) */
3631 usbd_transfer_enqueue(&sc->sc_bus.intr_q, xfer);
3633 (void)usb_proc_msignal(&sc->sc_config_proc,
3634 &sc->sc_config_msg[0], &sc->sc_config_msg[1]);
3638 xhci_configure_msg(struct usb_proc_msg *pm)
3640 struct xhci_softc *sc;
3641 struct xhci_endpoint_ext *pepext;
3642 struct usb_xfer *xfer;
3644 sc = XHCI_BUS2SC(((struct usb_bus_msg *)pm)->bus);
3647 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3649 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
3650 xfer->endpoint->edesc);
3652 if ((pepext->trb_halted != 0) ||
3653 (pepext->trb_running == 0)) {
3657 /* clear halted and running */
3658 pepext->trb_halted = 0;
3659 pepext->trb_running = 0;
3661 /* nuke remaining buffered transfers */
3663 for (i = 0; i != (XHCI_MAX_TRANSFERS - 1); i++) {
3665 * NOTE: We need to use the timeout
3666 * error code here else existing
3667 * isochronous clients can get
3670 if (pepext->xfer[i] != NULL) {
3671 xhci_device_done(pepext->xfer[i],
3677 * NOTE: The USB transfer cannot vanish in
3681 USB_BUS_UNLOCK(&sc->sc_bus);
3683 xhci_configure_reset_endpoint(xfer);
3685 USB_BUS_LOCK(&sc->sc_bus);
3687 /* check if halted is still cleared */
3688 if (pepext->trb_halted == 0) {
3689 pepext->trb_running = 1;
3690 pepext->trb_index = 0;
3695 if (xfer->flags_int.did_dma_delay) {
3697 /* remove transfer from interrupt queue (again) */
3698 usbd_transfer_dequeue(xfer);
3700 /* we are finally done */
3701 usb_dma_delay_done_cb(xfer);
3703 /* queue changed - restart */
3708 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3710 /* try to insert xfer on HW queue */
3711 xhci_transfer_insert(xfer);
3713 /* try to multi buffer */
3714 xhci_device_generic_multi_enter(xfer->endpoint, NULL);
3719 xhci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
3720 struct usb_endpoint *ep)
3722 struct xhci_endpoint_ext *pepext;
3724 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d\n",
3725 ep, udev->address, edesc->bEndpointAddress, udev->flags.usb_mode);
3727 if (udev->flags.usb_mode != USB_MODE_HOST) {
3731 if (udev->parent_hub == NULL) {
3732 /* root HUB has special endpoint handling */
3736 ep->methods = &xhci_device_generic_methods;
3738 pepext = xhci_get_endpoint_ext(udev, edesc);
3740 USB_BUS_LOCK(udev->bus);
3741 pepext->trb_halted = 1;
3742 pepext->trb_running = 0;
3743 USB_BUS_UNLOCK(udev->bus);
3747 xhci_ep_uninit(struct usb_device *udev, struct usb_endpoint *ep)
3753 xhci_ep_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
3755 struct xhci_endpoint_ext *pepext;
3759 if (udev->flags.usb_mode != USB_MODE_HOST) {
3763 if (udev->parent_hub == NULL) {
3764 /* root HUB has special endpoint handling */
3768 pepext = xhci_get_endpoint_ext(udev, ep->edesc);
3770 USB_BUS_LOCK(udev->bus);
3771 pepext->trb_halted = 1;
3772 pepext->trb_running = 0;
3773 USB_BUS_UNLOCK(udev->bus);
3777 xhci_device_init(struct usb_device *udev)
3779 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3783 /* no init for root HUB */
3784 if (udev->parent_hub == NULL)
3789 /* set invalid default */
3791 udev->controller_slot_id = sc->sc_noslot + 1;
3793 /* try to get a new slot ID from the XHCI */
3795 err = xhci_cmd_enable_slot(sc, &temp);
3798 XHCI_CMD_UNLOCK(sc);
3802 if (temp > sc->sc_noslot) {
3803 XHCI_CMD_UNLOCK(sc);
3804 return (USB_ERR_BAD_ADDRESS);
3807 if (sc->sc_hw.devs[temp].state != XHCI_ST_DISABLED) {
3808 DPRINTF("slot %u already allocated.\n", temp);
3809 XHCI_CMD_UNLOCK(sc);
3810 return (USB_ERR_BAD_ADDRESS);
3813 /* store slot ID for later reference */
3815 udev->controller_slot_id = temp;
3817 /* reset data structure */
3819 memset(&sc->sc_hw.devs[temp], 0, sizeof(sc->sc_hw.devs[0]));
3821 /* set mark slot allocated */
3823 sc->sc_hw.devs[temp].state = XHCI_ST_ENABLED;
3825 err = xhci_alloc_device_ext(udev);
3827 XHCI_CMD_UNLOCK(sc);
3829 /* get device into default state */
3832 err = xhci_set_address(udev, NULL, 0);
3838 xhci_device_uninit(struct usb_device *udev)
3840 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3843 /* no init for root HUB */
3844 if (udev->parent_hub == NULL)
3849 index = udev->controller_slot_id;
3851 if (index <= sc->sc_noslot) {
3852 xhci_cmd_disable_slot(sc, index);
3853 sc->sc_hw.devs[index].state = XHCI_ST_DISABLED;
3855 /* free device extension */
3856 xhci_free_device_ext(udev);
3859 XHCI_CMD_UNLOCK(sc);
3863 xhci_get_dma_delay(struct usb_device *udev, uint32_t *pus)
3866 * Wait until the hardware has finished any possible use of
3867 * the transfer descriptor(s)
3869 *pus = 2048; /* microseconds */
3873 xhci_device_resume(struct usb_device *udev)
3875 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3882 /* check for root HUB */
3883 if (udev->parent_hub == NULL)
3886 index = udev->controller_slot_id;
3890 /* blindly resume all endpoints */
3892 USB_BUS_LOCK(udev->bus);
3894 for (n = 1; n != XHCI_MAX_ENDPOINTS; n++) {
3895 for (p = 0; p != 1 /*XHCI_MAX_STREAMS*/; p++) {
3896 XWRITE4(sc, door, XHCI_DOORBELL(index),
3897 n | XHCI_DB_SID_SET(p));
3901 USB_BUS_UNLOCK(udev->bus);
3903 XHCI_CMD_UNLOCK(sc);
3907 xhci_device_suspend(struct usb_device *udev)
3909 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3916 /* check for root HUB */
3917 if (udev->parent_hub == NULL)
3920 index = udev->controller_slot_id;
3924 /* blindly suspend all endpoints */
3926 for (n = 1; n != XHCI_MAX_ENDPOINTS; n++) {
3927 err = xhci_cmd_stop_ep(sc, 1, n, index);
3929 DPRINTF("Failed to suspend endpoint "
3930 "%u on slot %u (ignored).\n", n, index);
3934 XHCI_CMD_UNLOCK(sc);
3938 xhci_set_hw_power(struct usb_bus *bus)
3944 xhci_device_state_change(struct usb_device *udev)
3946 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3947 struct usb_page_search buf_inp;
3951 /* check for root HUB */
3952 if (udev->parent_hub == NULL)
3955 index = udev->controller_slot_id;
3959 if (usb_get_device_state(udev) == USB_STATE_CONFIGURED) {
3960 err = uhub_query_info(udev, &sc->sc_hw.devs[index].nports,
3961 &sc->sc_hw.devs[index].tt);
3963 sc->sc_hw.devs[index].nports = 0;
3968 switch (usb_get_device_state(udev)) {
3969 case USB_STATE_POWERED:
3970 if (sc->sc_hw.devs[index].state == XHCI_ST_DEFAULT)
3973 /* set default state */
3974 sc->sc_hw.devs[index].state = XHCI_ST_DEFAULT;
3976 /* reset number of contexts */
3977 sc->sc_hw.devs[index].context_num = 0;
3979 err = xhci_cmd_reset_dev(sc, index);
3982 DPRINTF("Device reset failed "
3983 "for slot %u.\n", index);
3987 case USB_STATE_ADDRESSED:
3988 if (sc->sc_hw.devs[index].state == XHCI_ST_ADDRESSED)
3991 sc->sc_hw.devs[index].state = XHCI_ST_ADDRESSED;
3993 err = xhci_cmd_configure_ep(sc, 0, 1, index);
3996 DPRINTF("Failed to deconfigure "
3997 "slot %u.\n", index);
4001 case USB_STATE_CONFIGURED:
4002 if (sc->sc_hw.devs[index].state == XHCI_ST_CONFIGURED)
4005 /* set configured state */
4006 sc->sc_hw.devs[index].state = XHCI_ST_CONFIGURED;
4008 /* reset number of contexts */
4009 sc->sc_hw.devs[index].context_num = 0;
4011 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
4013 xhci_configure_mask(udev, 3, 0);
4015 err = xhci_configure_device(udev);
4017 DPRINTF("Could not configure device "
4018 "at slot %u.\n", index);
4021 err = xhci_cmd_evaluate_ctx(sc, buf_inp.physaddr, index);
4023 DPRINTF("Could not evaluate device "
4024 "context at slot %u.\n", index);
4031 XHCI_CMD_UNLOCK(sc);
4034 struct usb_bus_methods xhci_bus_methods = {
4035 .endpoint_init = xhci_ep_init,
4036 .endpoint_uninit = xhci_ep_uninit,
4037 .xfer_setup = xhci_xfer_setup,
4038 .xfer_unsetup = xhci_xfer_unsetup,
4039 .get_dma_delay = xhci_get_dma_delay,
4040 .device_init = xhci_device_init,
4041 .device_uninit = xhci_device_uninit,
4042 .device_resume = xhci_device_resume,
4043 .device_suspend = xhci_device_suspend,
4044 .set_hw_power = xhci_set_hw_power,
4045 .roothub_exec = xhci_roothub_exec,
4046 .xfer_poll = xhci_do_poll,
4047 .start_dma_delay = xhci_start_dma_delay,
4048 .set_address = xhci_set_address,
4049 .clear_stall = xhci_ep_clear_stall,
4050 .device_state_change = xhci_device_state_change,
4051 .set_hw_power_sleep = xhci_set_hw_power_sleep,