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,
92 &xhcidebug, 0, "Debug level");
93 SYSCTL_INT(_hw_usb_xhci, OID_AUTO, xhci_port_route, CTLFLAG_RW,
94 &xhciroute, 0, "Routing bitmap for switching EHCI ports to XHCI controller");
96 TUNABLE_INT("hw.usb.xhci.debug", &xhcidebug);
97 TUNABLE_INT("hw.usb.xhci.xhci_port_route", &xhciroute);
100 #define XHCI_INTR_ENDPT 1
102 struct xhci_std_temp {
103 struct xhci_softc *sc;
104 struct usb_page_cache *pc;
106 struct xhci_td *td_next;
109 uint32_t max_packet_size;
121 uint8_t do_isoc_sync;
124 static void xhci_do_poll(struct usb_bus *);
125 static void xhci_device_done(struct usb_xfer *, usb_error_t);
126 static void xhci_root_intr(struct xhci_softc *);
127 static void xhci_free_device_ext(struct usb_device *);
128 static struct xhci_endpoint_ext *xhci_get_endpoint_ext(struct usb_device *,
129 struct usb_endpoint_descriptor *);
130 static usb_proc_callback_t xhci_configure_msg;
131 static usb_error_t xhci_configure_device(struct usb_device *);
132 static usb_error_t xhci_configure_endpoint(struct usb_device *,
133 struct usb_endpoint_descriptor *, uint64_t, uint16_t,
134 uint8_t, uint8_t, uint8_t, uint16_t, uint16_t);
135 static usb_error_t xhci_configure_mask(struct usb_device *,
137 static usb_error_t xhci_cmd_evaluate_ctx(struct xhci_softc *,
139 static void xhci_endpoint_doorbell(struct usb_xfer *);
140 static void xhci_ctx_set_le32(struct xhci_softc *sc, volatile uint32_t *ptr, uint32_t val);
141 static uint32_t xhci_ctx_get_le32(struct xhci_softc *sc, volatile uint32_t *ptr);
142 static void xhci_ctx_set_le64(struct xhci_softc *sc, volatile uint64_t *ptr, uint64_t val);
144 static uint64_t xhci_ctx_get_le64(struct xhci_softc *sc, volatile uint64_t *ptr);
147 extern struct usb_bus_methods xhci_bus_methods;
151 xhci_dump_trb(struct xhci_trb *trb)
153 DPRINTFN(5, "trb = %p\n", trb);
154 DPRINTFN(5, "qwTrb0 = 0x%016llx\n", (long long)le64toh(trb->qwTrb0));
155 DPRINTFN(5, "dwTrb2 = 0x%08x\n", le32toh(trb->dwTrb2));
156 DPRINTFN(5, "dwTrb3 = 0x%08x\n", le32toh(trb->dwTrb3));
160 xhci_dump_endpoint(struct xhci_softc *sc, struct xhci_endp_ctx *pep)
162 DPRINTFN(5, "pep = %p\n", pep);
163 DPRINTFN(5, "dwEpCtx0=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx0));
164 DPRINTFN(5, "dwEpCtx1=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx1));
165 DPRINTFN(5, "qwEpCtx2=0x%016llx\n", (long long)xhci_ctx_get_le64(sc, &pep->qwEpCtx2));
166 DPRINTFN(5, "dwEpCtx4=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx4));
167 DPRINTFN(5, "dwEpCtx5=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx5));
168 DPRINTFN(5, "dwEpCtx6=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx6));
169 DPRINTFN(5, "dwEpCtx7=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx7));
173 xhci_dump_device(struct xhci_softc *sc, struct xhci_slot_ctx *psl)
175 DPRINTFN(5, "psl = %p\n", psl);
176 DPRINTFN(5, "dwSctx0=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx0));
177 DPRINTFN(5, "dwSctx1=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx1));
178 DPRINTFN(5, "dwSctx2=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx2));
179 DPRINTFN(5, "dwSctx3=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx3));
184 xhci_get_port_route(void)
187 return (0xFFFFFFFFU ^ ((uint32_t)xhciroute));
189 return (0xFFFFFFFFU);
194 xhci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
196 struct xhci_softc *sc = XHCI_BUS2SC(bus);
199 cb(bus, &sc->sc_hw.root_pc, &sc->sc_hw.root_pg,
200 sizeof(struct xhci_hw_root), XHCI_PAGE_SIZE);
202 cb(bus, &sc->sc_hw.ctx_pc, &sc->sc_hw.ctx_pg,
203 sizeof(struct xhci_dev_ctx_addr), XHCI_PAGE_SIZE);
205 for (i = 0; i != XHCI_MAX_SCRATCHPADS; i++) {
206 cb(bus, &sc->sc_hw.scratch_pc[i], &sc->sc_hw.scratch_pg[i],
207 XHCI_PAGE_SIZE, XHCI_PAGE_SIZE);
212 xhci_ctx_set_le32(struct xhci_softc *sc, volatile uint32_t *ptr, uint32_t val)
214 if (sc->sc_ctx_is_64_byte) {
216 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
217 /* all contexts are initially 32-bytes */
218 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
219 ptr = (volatile uint32_t *)(((volatile uint8_t *)ptr) + offset);
225 xhci_ctx_get_le32(struct xhci_softc *sc, volatile uint32_t *ptr)
227 if (sc->sc_ctx_is_64_byte) {
229 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
230 /* all contexts are initially 32-bytes */
231 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
232 ptr = (volatile uint32_t *)(((volatile uint8_t *)ptr) + offset);
234 return (le32toh(*ptr));
238 xhci_ctx_set_le64(struct xhci_softc *sc, volatile uint64_t *ptr, uint64_t val)
240 if (sc->sc_ctx_is_64_byte) {
242 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
243 /* all contexts are initially 32-bytes */
244 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
245 ptr = (volatile uint64_t *)(((volatile uint8_t *)ptr) + offset);
252 xhci_ctx_get_le64(struct xhci_softc *sc, volatile uint64_t *ptr)
254 if (sc->sc_ctx_is_64_byte) {
256 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
257 /* all contexts are initially 32-bytes */
258 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
259 ptr = (volatile uint64_t *)(((volatile uint8_t *)ptr) + offset);
261 return (le64toh(*ptr));
266 xhci_start_controller(struct xhci_softc *sc)
268 struct usb_page_search buf_res;
269 struct xhci_hw_root *phwr;
270 struct xhci_dev_ctx_addr *pdctxa;
278 sc->sc_oper_off = XREAD1(sc, capa, XHCI_CAPLENGTH);
279 sc->sc_runt_off = XREAD4(sc, capa, XHCI_RTSOFF) & ~0x1F;
280 sc->sc_door_off = XREAD4(sc, capa, XHCI_DBOFF) & ~0x3;
282 DPRINTF("CAPLENGTH=0x%x\n", sc->sc_oper_off);
283 DPRINTF("RUNTIMEOFFSET=0x%x\n", sc->sc_runt_off);
284 DPRINTF("DOOROFFSET=0x%x\n", sc->sc_door_off);
286 sc->sc_event_ccs = 1;
287 sc->sc_event_idx = 0;
288 sc->sc_command_ccs = 1;
289 sc->sc_command_idx = 0;
291 DPRINTF("xHCI version = 0x%04x\n", XREAD2(sc, capa, XHCI_HCIVERSION));
293 temp = XREAD4(sc, capa, XHCI_HCSPARAMS0);
295 DPRINTF("HCS0 = 0x%08x\n", temp);
297 if (XHCI_HCS0_CSZ(temp)) {
298 sc->sc_ctx_is_64_byte = 1;
299 device_printf(sc->sc_bus.parent, "64 byte context size.\n");
301 sc->sc_ctx_is_64_byte = 0;
302 device_printf(sc->sc_bus.parent, "32 byte context size.\n");
305 /* Reset controller */
306 XWRITE4(sc, oper, XHCI_USBCMD, XHCI_CMD_HCRST);
308 for (i = 0; i != 100; i++) {
309 usb_pause_mtx(NULL, hz / 100);
310 temp = XREAD4(sc, oper, XHCI_USBCMD) &
311 (XHCI_CMD_HCRST | XHCI_STS_CNR);
317 device_printf(sc->sc_bus.parent, "Controller "
319 return (USB_ERR_IOERROR);
322 if (!(XREAD4(sc, oper, XHCI_PAGESIZE) & XHCI_PAGESIZE_4K)) {
323 device_printf(sc->sc_bus.parent, "Controller does "
324 "not support 4K page size.\n");
325 return (USB_ERR_IOERROR);
328 temp = XREAD4(sc, capa, XHCI_HCSPARAMS1);
330 i = XHCI_HCS1_N_PORTS(temp);
333 device_printf(sc->sc_bus.parent, "Invalid number "
334 "of ports: %u\n", i);
335 return (USB_ERR_IOERROR);
339 sc->sc_noslot = XHCI_HCS1_DEVSLOT_MAX(temp);
341 if (sc->sc_noslot > XHCI_MAX_DEVICES)
342 sc->sc_noslot = XHCI_MAX_DEVICES;
344 /* setup number of device slots */
346 DPRINTF("CONFIG=0x%08x -> 0x%08x\n",
347 XREAD4(sc, oper, XHCI_CONFIG), sc->sc_noslot);
349 XWRITE4(sc, oper, XHCI_CONFIG, sc->sc_noslot);
351 DPRINTF("Max slots: %u\n", sc->sc_noslot);
353 temp = XREAD4(sc, capa, XHCI_HCSPARAMS2);
355 sc->sc_noscratch = XHCI_HCS2_SPB_MAX(temp);
357 if (sc->sc_noscratch > XHCI_MAX_SCRATCHPADS) {
358 device_printf(sc->sc_bus.parent, "XHCI request "
359 "too many scratchpads\n");
360 return (USB_ERR_NOMEM);
363 DPRINTF("Max scratch: %u\n", sc->sc_noscratch);
365 temp = XREAD4(sc, capa, XHCI_HCSPARAMS3);
367 sc->sc_exit_lat_max = XHCI_HCS3_U1_DEL(temp) +
368 XHCI_HCS3_U2_DEL(temp) + 250 /* us */;
370 temp = XREAD4(sc, oper, XHCI_USBSTS);
372 /* clear interrupts */
373 XWRITE4(sc, oper, XHCI_USBSTS, temp);
374 /* disable all device notifications */
375 XWRITE4(sc, oper, XHCI_DNCTRL, 0);
377 /* setup device context base address */
378 usbd_get_page(&sc->sc_hw.ctx_pc, 0, &buf_res);
379 pdctxa = buf_res.buffer;
380 memset(pdctxa, 0, sizeof(*pdctxa));
382 addr = buf_res.physaddr;
383 addr += (uintptr_t)&((struct xhci_dev_ctx_addr *)0)->qwSpBufPtr[0];
385 /* slot 0 points to the table of scratchpad pointers */
386 pdctxa->qwBaaDevCtxAddr[0] = htole64(addr);
388 for (i = 0; i != sc->sc_noscratch; i++) {
389 struct usb_page_search buf_scp;
390 usbd_get_page(&sc->sc_hw.scratch_pc[i], 0, &buf_scp);
391 pdctxa->qwSpBufPtr[i] = htole64((uint64_t)buf_scp.physaddr);
394 addr = buf_res.physaddr;
396 XWRITE4(sc, oper, XHCI_DCBAAP_LO, (uint32_t)addr);
397 XWRITE4(sc, oper, XHCI_DCBAAP_HI, (uint32_t)(addr >> 32));
398 XWRITE4(sc, oper, XHCI_DCBAAP_LO, (uint32_t)addr);
399 XWRITE4(sc, oper, XHCI_DCBAAP_HI, (uint32_t)(addr >> 32));
401 /* Setup event table size */
403 temp = XREAD4(sc, capa, XHCI_HCSPARAMS2);
405 DPRINTF("HCS2=0x%08x\n", temp);
407 temp = XHCI_HCS2_ERST_MAX(temp);
409 if (temp > XHCI_MAX_RSEG)
410 temp = XHCI_MAX_RSEG;
412 sc->sc_erst_max = temp;
414 DPRINTF("ERSTSZ=0x%08x -> 0x%08x\n",
415 XREAD4(sc, runt, XHCI_ERSTSZ(0)), temp);
417 XWRITE4(sc, runt, XHCI_ERSTSZ(0), XHCI_ERSTS_SET(temp));
419 /* Setup interrupt rate */
420 XWRITE4(sc, runt, XHCI_IMOD(0), XHCI_IMOD_DEFAULT);
422 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
424 phwr = buf_res.buffer;
425 addr = buf_res.physaddr;
426 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_events[0];
428 /* reset hardware root structure */
429 memset(phwr, 0, sizeof(*phwr));
431 phwr->hwr_ring_seg[0].qwEvrsTablePtr = htole64(addr);
432 phwr->hwr_ring_seg[0].dwEvrsTableSize = htole32(XHCI_MAX_EVENTS);
434 DPRINTF("ERDP(0)=0x%016llx\n", (unsigned long long)addr);
436 XWRITE4(sc, runt, XHCI_ERDP_LO(0), (uint32_t)addr);
437 XWRITE4(sc, runt, XHCI_ERDP_HI(0), (uint32_t)(addr >> 32));
439 addr = (uint64_t)buf_res.physaddr;
441 DPRINTF("ERSTBA(0)=0x%016llx\n", (unsigned long long)addr);
443 XWRITE4(sc, runt, XHCI_ERSTBA_LO(0), (uint32_t)addr);
444 XWRITE4(sc, runt, XHCI_ERSTBA_HI(0), (uint32_t)(addr >> 32));
446 /* Setup interrupter registers */
448 temp = XREAD4(sc, runt, XHCI_IMAN(0));
449 temp |= XHCI_IMAN_INTR_ENA;
450 XWRITE4(sc, runt, XHCI_IMAN(0), temp);
452 /* setup command ring control base address */
453 addr = buf_res.physaddr;
454 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_commands[0];
456 DPRINTF("CRCR=0x%016llx\n", (unsigned long long)addr);
458 XWRITE4(sc, oper, XHCI_CRCR_LO, ((uint32_t)addr) | XHCI_CRCR_LO_RCS);
459 XWRITE4(sc, oper, XHCI_CRCR_HI, (uint32_t)(addr >> 32));
461 phwr->hwr_commands[XHCI_MAX_COMMANDS - 1].qwTrb0 = htole64(addr);
463 usb_bus_mem_flush_all(&sc->sc_bus, &xhci_iterate_hw_softc);
466 XWRITE4(sc, oper, XHCI_USBCMD, XHCI_CMD_RS |
467 XHCI_CMD_INTE | XHCI_CMD_HSEE);
469 for (i = 0; i != 100; i++) {
470 usb_pause_mtx(NULL, hz / 100);
471 temp = XREAD4(sc, oper, XHCI_USBSTS) & XHCI_STS_HCH;
476 XWRITE4(sc, oper, XHCI_USBCMD, 0);
477 device_printf(sc->sc_bus.parent, "Run timeout.\n");
478 return (USB_ERR_IOERROR);
481 /* catch any lost interrupts */
482 xhci_do_poll(&sc->sc_bus);
488 xhci_halt_controller(struct xhci_softc *sc)
496 sc->sc_oper_off = XREAD1(sc, capa, XHCI_CAPLENGTH);
497 sc->sc_runt_off = XREAD4(sc, capa, XHCI_RTSOFF) & ~0xF;
498 sc->sc_door_off = XREAD4(sc, capa, XHCI_DBOFF) & ~0x3;
500 /* Halt controller */
501 XWRITE4(sc, oper, XHCI_USBCMD, 0);
503 for (i = 0; i != 100; i++) {
504 usb_pause_mtx(NULL, hz / 100);
505 temp = XREAD4(sc, oper, XHCI_USBSTS) & XHCI_STS_HCH;
511 device_printf(sc->sc_bus.parent, "Controller halt timeout.\n");
512 return (USB_ERR_IOERROR);
518 xhci_init(struct xhci_softc *sc, device_t self)
520 /* initialise some bus fields */
521 sc->sc_bus.parent = self;
523 /* set the bus revision */
524 sc->sc_bus.usbrev = USB_REV_3_0;
526 /* set up the bus struct */
527 sc->sc_bus.methods = &xhci_bus_methods;
529 /* setup devices array */
530 sc->sc_bus.devices = sc->sc_devices;
531 sc->sc_bus.devices_max = XHCI_MAX_DEVICES;
533 /* setup command queue mutex and condition varible */
534 cv_init(&sc->sc_cmd_cv, "CMDQ");
535 sx_init(&sc->sc_cmd_sx, "CMDQ lock");
537 /* get all DMA memory */
538 if (usb_bus_mem_alloc_all(&sc->sc_bus,
539 USB_GET_DMA_TAG(self), &xhci_iterate_hw_softc)) {
543 sc->sc_config_msg[0].hdr.pm_callback = &xhci_configure_msg;
544 sc->sc_config_msg[0].bus = &sc->sc_bus;
545 sc->sc_config_msg[1].hdr.pm_callback = &xhci_configure_msg;
546 sc->sc_config_msg[1].bus = &sc->sc_bus;
548 if (usb_proc_create(&sc->sc_config_proc,
549 &sc->sc_bus.bus_mtx, device_get_nameunit(self), USB_PRI_MED)) {
550 printf("WARNING: Creation of XHCI configure "
551 "callback process failed.\n");
557 xhci_uninit(struct xhci_softc *sc)
559 usb_proc_free(&sc->sc_config_proc);
561 usb_bus_mem_free_all(&sc->sc_bus, &xhci_iterate_hw_softc);
563 cv_destroy(&sc->sc_cmd_cv);
564 sx_destroy(&sc->sc_cmd_sx);
568 xhci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
570 struct xhci_softc *sc = XHCI_BUS2SC(bus);
573 case USB_HW_POWER_SUSPEND:
574 DPRINTF("Stopping the XHCI\n");
575 xhci_halt_controller(sc);
577 case USB_HW_POWER_SHUTDOWN:
578 DPRINTF("Stopping the XHCI\n");
579 xhci_halt_controller(sc);
581 case USB_HW_POWER_RESUME:
582 DPRINTF("Starting the XHCI\n");
583 xhci_start_controller(sc);
591 xhci_generic_done_sub(struct usb_xfer *xfer)
594 struct xhci_td *td_alt_next;
598 td = xfer->td_transfer_cache;
599 td_alt_next = td->alt_next;
601 if (xfer->aframes != xfer->nframes)
602 usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
606 usb_pc_cpu_invalidate(td->page_cache);
611 DPRINTFN(4, "xfer=%p[%u/%u] rem=%u/%u status=%u\n",
612 xfer, (unsigned int)xfer->aframes,
613 (unsigned int)xfer->nframes,
614 (unsigned int)len, (unsigned int)td->len,
615 (unsigned int)status);
618 * Verify the status length and
619 * add the length to "frlengths[]":
622 /* should not happen */
623 DPRINTF("Invalid status length, "
624 "0x%04x/0x%04x bytes\n", len, td->len);
625 status = XHCI_TRB_ERROR_LENGTH;
626 } else if (xfer->aframes != xfer->nframes) {
627 xfer->frlengths[xfer->aframes] += td->len - len;
629 /* Check for last transfer */
630 if (((void *)td) == xfer->td_transfer_last) {
634 /* Check for transfer error */
635 if (status != XHCI_TRB_ERROR_SHORT_PKT &&
636 status != XHCI_TRB_ERROR_SUCCESS) {
637 /* the transfer is finished */
641 /* Check for short transfer */
643 if (xfer->flags_int.short_frames_ok ||
644 xfer->flags_int.isochronous_xfr ||
645 xfer->flags_int.control_xfr) {
646 /* follow alt next */
649 /* the transfer is finished */
656 if (td->alt_next != td_alt_next) {
657 /* this USB frame is complete */
662 /* update transfer cache */
664 xfer->td_transfer_cache = td;
666 return ((status == XHCI_TRB_ERROR_STALL) ? USB_ERR_STALLED :
667 (status != XHCI_TRB_ERROR_SHORT_PKT &&
668 status != XHCI_TRB_ERROR_SUCCESS) ? USB_ERR_IOERROR :
669 USB_ERR_NORMAL_COMPLETION);
673 xhci_generic_done(struct usb_xfer *xfer)
677 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
678 xfer, xfer->endpoint);
682 xfer->td_transfer_cache = xfer->td_transfer_first;
684 if (xfer->flags_int.control_xfr) {
686 if (xfer->flags_int.control_hdr)
687 err = xhci_generic_done_sub(xfer);
691 if (xfer->td_transfer_cache == NULL)
695 while (xfer->aframes != xfer->nframes) {
697 err = xhci_generic_done_sub(xfer);
700 if (xfer->td_transfer_cache == NULL)
704 if (xfer->flags_int.control_xfr &&
705 !xfer->flags_int.control_act)
706 err = xhci_generic_done_sub(xfer);
708 /* transfer is complete */
709 xhci_device_done(xfer, err);
713 xhci_activate_transfer(struct usb_xfer *xfer)
717 td = xfer->td_transfer_cache;
719 usb_pc_cpu_invalidate(td->page_cache);
721 if (!(td->td_trb[0].dwTrb3 & htole32(XHCI_TRB_3_CYCLE_BIT))) {
723 /* activate the transfer */
725 td->td_trb[0].dwTrb3 |= htole32(XHCI_TRB_3_CYCLE_BIT);
726 usb_pc_cpu_flush(td->page_cache);
728 xhci_endpoint_doorbell(xfer);
733 xhci_skip_transfer(struct usb_xfer *xfer)
736 struct xhci_td *td_last;
738 td = xfer->td_transfer_cache;
739 td_last = xfer->td_transfer_last;
743 usb_pc_cpu_invalidate(td->page_cache);
745 if (!(td->td_trb[0].dwTrb3 & htole32(XHCI_TRB_3_CYCLE_BIT))) {
747 usb_pc_cpu_invalidate(td_last->page_cache);
749 /* copy LINK TRB to current waiting location */
751 td->td_trb[0].qwTrb0 = td_last->td_trb[td_last->ntrb].qwTrb0;
752 td->td_trb[0].dwTrb2 = td_last->td_trb[td_last->ntrb].dwTrb2;
753 usb_pc_cpu_flush(td->page_cache);
755 td->td_trb[0].dwTrb3 = td_last->td_trb[td_last->ntrb].dwTrb3;
756 usb_pc_cpu_flush(td->page_cache);
758 xhci_endpoint_doorbell(xfer);
762 /*------------------------------------------------------------------------*
763 * xhci_check_transfer
764 *------------------------------------------------------------------------*/
766 xhci_check_transfer(struct xhci_softc *sc, struct xhci_trb *trb)
779 td_event = le64toh(trb->qwTrb0);
780 temp = le32toh(trb->dwTrb2);
782 remainder = XHCI_TRB_2_REM_GET(temp);
783 status = XHCI_TRB_2_ERROR_GET(temp);
785 temp = le32toh(trb->dwTrb3);
786 epno = XHCI_TRB_3_EP_GET(temp);
787 index = XHCI_TRB_3_SLOT_GET(temp);
789 /* check if error means halted */
790 halted = (status != XHCI_TRB_ERROR_SHORT_PKT &&
791 status != XHCI_TRB_ERROR_SUCCESS);
793 DPRINTF("slot=%u epno=%u remainder=%u status=%u\n",
794 index, epno, remainder, status);
796 if (index > sc->sc_noslot) {
797 DPRINTF("Invalid slot.\n");
801 if ((epno == 0) || (epno >= XHCI_MAX_ENDPOINTS)) {
802 DPRINTF("Invalid endpoint.\n");
806 /* try to find the USB transfer that generated the event */
807 for (i = 0; i != (XHCI_MAX_TRANSFERS - 1); i++) {
808 struct usb_xfer *xfer;
810 struct xhci_endpoint_ext *pepext;
812 pepext = &sc->sc_hw.devs[index].endp[epno];
814 xfer = pepext->xfer[i];
818 td = xfer->td_transfer_cache;
820 DPRINTFN(5, "Checking if 0x%016llx == (0x%016llx .. 0x%016llx)\n",
822 (long long)td->td_self,
823 (long long)td->td_self + sizeof(td->td_trb));
826 * NOTE: Some XHCI implementations might not trigger
827 * an event on the last LINK TRB so we need to
828 * consider both the last and second last event
829 * address as conditions for a successful transfer.
831 * NOTE: We assume that the XHCI will only trigger one
832 * event per chain of TRBs.
835 offset = td_event - td->td_self;
838 offset < (int64_t)sizeof(td->td_trb)) {
840 usb_pc_cpu_invalidate(td->page_cache);
842 /* compute rest of remainder, if any */
843 for (i = (offset / 16) + 1; i < td->ntrb; i++) {
844 temp = le32toh(td->td_trb[i].dwTrb2);
845 remainder += XHCI_TRB_2_BYTES_GET(temp);
848 DPRINTFN(5, "New remainder: %u\n", remainder);
850 /* clear isochronous transfer errors */
851 if (xfer->flags_int.isochronous_xfr) {
854 status = XHCI_TRB_ERROR_SUCCESS;
859 /* "td->remainder" is verified later */
860 td->remainder = remainder;
863 usb_pc_cpu_flush(td->page_cache);
866 * 1) Last transfer descriptor makes the
869 if (((void *)td) == xfer->td_transfer_last) {
870 DPRINTF("TD is last\n");
871 xhci_generic_done(xfer);
876 * 2) Any kind of error makes the transfer
880 DPRINTF("TD has I/O error\n");
881 xhci_generic_done(xfer);
886 * 3) If there is no alternate next transfer,
887 * a short packet also makes the transfer done
889 if (td->remainder > 0) {
890 DPRINTF("TD has short pkt\n");
891 if (xfer->flags_int.short_frames_ok ||
892 xfer->flags_int.isochronous_xfr ||
893 xfer->flags_int.control_xfr) {
894 /* follow the alt next */
895 xfer->td_transfer_cache = td->alt_next;
896 xhci_activate_transfer(xfer);
899 xhci_skip_transfer(xfer);
900 xhci_generic_done(xfer);
905 * 4) Transfer complete - go to next TD
907 DPRINTF("Following next TD\n");
908 xfer->td_transfer_cache = td->obj_next;
909 xhci_activate_transfer(xfer);
910 break; /* there should only be one match */
916 xhci_check_command(struct xhci_softc *sc, struct xhci_trb *trb)
918 if (sc->sc_cmd_addr == trb->qwTrb0) {
919 DPRINTF("Received command event\n");
920 sc->sc_cmd_result[0] = trb->dwTrb2;
921 sc->sc_cmd_result[1] = trb->dwTrb3;
922 cv_signal(&sc->sc_cmd_cv);
927 xhci_interrupt_poll(struct xhci_softc *sc)
929 struct usb_page_search buf_res;
930 struct xhci_hw_root *phwr;
939 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
941 phwr = buf_res.buffer;
943 /* Receive any events */
945 usb_pc_cpu_invalidate(&sc->sc_hw.root_pc);
947 i = sc->sc_event_idx;
948 j = sc->sc_event_ccs;
953 temp = le32toh(phwr->hwr_events[i].dwTrb3);
955 k = (temp & XHCI_TRB_3_CYCLE_BIT) ? 1 : 0;
960 event = XHCI_TRB_3_TYPE_GET(temp);
962 DPRINTFN(10, "event[%u] = %u (0x%016llx 0x%08lx 0x%08lx)\n",
963 i, event, (long long)le64toh(phwr->hwr_events[i].qwTrb0),
964 (long)le32toh(phwr->hwr_events[i].dwTrb2),
965 (long)le32toh(phwr->hwr_events[i].dwTrb3));
968 case XHCI_TRB_EVENT_TRANSFER:
969 xhci_check_transfer(sc, &phwr->hwr_events[i]);
971 case XHCI_TRB_EVENT_CMD_COMPLETE:
972 xhci_check_command(sc, &phwr->hwr_events[i]);
975 DPRINTF("Unhandled event = %u\n", event);
981 if (i == XHCI_MAX_EVENTS) {
985 /* check for timeout */
991 sc->sc_event_idx = i;
992 sc->sc_event_ccs = j;
995 * NOTE: The Event Ring Dequeue Pointer Register is 64-bit
996 * latched. That means to activate the register we need to
997 * write both the low and high double word of the 64-bit
1001 addr = (uint32_t)buf_res.physaddr;
1002 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_events[i];
1004 /* try to clear busy bit */
1005 addr |= XHCI_ERDP_LO_BUSY;
1007 XWRITE4(sc, runt, XHCI_ERDP_LO(0), (uint32_t)addr);
1008 XWRITE4(sc, runt, XHCI_ERDP_HI(0), (uint32_t)(addr >> 32));
1012 xhci_do_command(struct xhci_softc *sc, struct xhci_trb *trb,
1013 uint16_t timeout_ms)
1015 struct usb_page_search buf_res;
1016 struct xhci_hw_root *phwr;
1023 XHCI_CMD_ASSERT_LOCKED(sc);
1025 /* get hardware root structure */
1027 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
1029 phwr = buf_res.buffer;
1033 USB_BUS_LOCK(&sc->sc_bus);
1035 i = sc->sc_command_idx;
1036 j = sc->sc_command_ccs;
1038 DPRINTFN(10, "command[%u] = %u (0x%016llx, 0x%08lx, 0x%08lx)\n",
1039 i, XHCI_TRB_3_TYPE_GET(le32toh(trb->dwTrb3)),
1040 (long long)le64toh(trb->qwTrb0),
1041 (long)le32toh(trb->dwTrb2),
1042 (long)le32toh(trb->dwTrb3));
1044 phwr->hwr_commands[i].qwTrb0 = trb->qwTrb0;
1045 phwr->hwr_commands[i].dwTrb2 = trb->dwTrb2;
1047 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1052 temp |= htole32(XHCI_TRB_3_CYCLE_BIT);
1054 temp &= ~htole32(XHCI_TRB_3_CYCLE_BIT);
1056 temp &= ~htole32(XHCI_TRB_3_TC_BIT);
1058 phwr->hwr_commands[i].dwTrb3 = temp;
1060 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1062 addr = buf_res.physaddr;
1063 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_commands[i];
1065 sc->sc_cmd_addr = htole64(addr);
1069 if (i == (XHCI_MAX_COMMANDS - 1)) {
1072 temp = htole32(XHCI_TRB_3_TC_BIT |
1073 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) |
1074 XHCI_TRB_3_CYCLE_BIT);
1076 temp = htole32(XHCI_TRB_3_TC_BIT |
1077 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
1080 phwr->hwr_commands[i].dwTrb3 = temp;
1082 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1088 sc->sc_command_idx = i;
1089 sc->sc_command_ccs = j;
1091 XWRITE4(sc, door, XHCI_DOORBELL(0), 0);
1093 err = cv_timedwait(&sc->sc_cmd_cv, &sc->sc_bus.bus_mtx,
1094 USB_MS_TO_TICKS(timeout_ms));
1097 DPRINTFN(0, "Command timeout!\n");
1098 err = USB_ERR_TIMEOUT;
1102 temp = le32toh(sc->sc_cmd_result[0]);
1103 if (XHCI_TRB_2_ERROR_GET(temp) != XHCI_TRB_ERROR_SUCCESS)
1104 err = USB_ERR_IOERROR;
1106 trb->dwTrb2 = sc->sc_cmd_result[0];
1107 trb->dwTrb3 = sc->sc_cmd_result[1];
1110 USB_BUS_UNLOCK(&sc->sc_bus);
1117 xhci_cmd_nop(struct xhci_softc *sc)
1119 struct xhci_trb trb;
1126 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NOOP);
1128 trb.dwTrb3 = htole32(temp);
1130 return (xhci_do_command(sc, &trb, 100 /* ms */));
1135 xhci_cmd_enable_slot(struct xhci_softc *sc, uint8_t *pslot)
1137 struct xhci_trb trb;
1145 trb.dwTrb3 = htole32(XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ENABLE_SLOT));
1147 err = xhci_do_command(sc, &trb, 100 /* ms */);
1151 temp = le32toh(trb.dwTrb3);
1153 *pslot = XHCI_TRB_3_SLOT_GET(temp);
1160 xhci_cmd_disable_slot(struct xhci_softc *sc, uint8_t slot_id)
1162 struct xhci_trb trb;
1169 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DISABLE_SLOT) |
1170 XHCI_TRB_3_SLOT_SET(slot_id);
1172 trb.dwTrb3 = htole32(temp);
1174 return (xhci_do_command(sc, &trb, 100 /* ms */));
1178 xhci_cmd_set_address(struct xhci_softc *sc, uint64_t input_ctx,
1179 uint8_t bsr, uint8_t slot_id)
1181 struct xhci_trb trb;
1186 trb.qwTrb0 = htole64(input_ctx);
1188 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ADDRESS_DEVICE) |
1189 XHCI_TRB_3_SLOT_SET(slot_id);
1192 temp |= XHCI_TRB_3_BSR_BIT;
1194 trb.dwTrb3 = htole32(temp);
1196 return (xhci_do_command(sc, &trb, 500 /* ms */));
1200 xhci_set_address(struct usb_device *udev, struct mtx *mtx, uint16_t address)
1202 struct usb_page_search buf_inp;
1203 struct usb_page_search buf_dev;
1204 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
1205 struct xhci_hw_dev *hdev;
1206 struct xhci_dev_ctx *pdev;
1207 struct xhci_endpoint_ext *pepext;
1213 /* the root HUB case is not handled here */
1214 if (udev->parent_hub == NULL)
1215 return (USB_ERR_INVAL);
1217 index = udev->controller_slot_id;
1219 hdev = &sc->sc_hw.devs[index];
1226 switch (hdev->state) {
1227 case XHCI_ST_DEFAULT:
1228 case XHCI_ST_ENABLED:
1230 hdev->state = XHCI_ST_ENABLED;
1232 /* set configure mask to slot and EP0 */
1233 xhci_configure_mask(udev, 3, 0);
1235 /* configure input slot context structure */
1236 err = xhci_configure_device(udev);
1239 DPRINTF("Could not configure device\n");
1243 /* configure input endpoint context structure */
1244 switch (udev->speed) {
1246 case USB_SPEED_FULL:
1249 case USB_SPEED_HIGH:
1257 pepext = xhci_get_endpoint_ext(udev,
1258 &udev->ctrl_ep_desc);
1259 err = xhci_configure_endpoint(udev,
1260 &udev->ctrl_ep_desc, pepext->physaddr,
1261 0, 1, 1, 0, mps, mps);
1264 DPRINTF("Could not configure default endpoint\n");
1268 /* execute set address command */
1269 usbd_get_page(&hdev->input_pc, 0, &buf_inp);
1271 err = xhci_cmd_set_address(sc, buf_inp.physaddr,
1272 (address == 0), index);
1275 DPRINTF("Could not set address "
1276 "for slot %u.\n", index);
1281 /* update device address to new value */
1283 usbd_get_page(&hdev->device_pc, 0, &buf_dev);
1284 pdev = buf_dev.buffer;
1285 usb_pc_cpu_invalidate(&hdev->device_pc);
1287 temp = xhci_ctx_get_le32(sc, &pdev->ctx_slot.dwSctx3);
1288 udev->address = XHCI_SCTX_3_DEV_ADDR_GET(temp);
1290 /* update device state to new value */
1293 hdev->state = XHCI_ST_ADDRESSED;
1295 hdev->state = XHCI_ST_DEFAULT;
1299 DPRINTF("Wrong state for set address.\n");
1300 err = USB_ERR_IOERROR;
1303 XHCI_CMD_UNLOCK(sc);
1312 xhci_cmd_configure_ep(struct xhci_softc *sc, uint64_t input_ctx,
1313 uint8_t deconfigure, uint8_t slot_id)
1315 struct xhci_trb trb;
1320 trb.qwTrb0 = htole64(input_ctx);
1322 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_CONFIGURE_EP) |
1323 XHCI_TRB_3_SLOT_SET(slot_id);
1326 temp |= XHCI_TRB_3_DCEP_BIT;
1328 trb.dwTrb3 = htole32(temp);
1330 return (xhci_do_command(sc, &trb, 100 /* ms */));
1334 xhci_cmd_evaluate_ctx(struct xhci_softc *sc, uint64_t input_ctx,
1337 struct xhci_trb trb;
1342 trb.qwTrb0 = htole64(input_ctx);
1344 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVALUATE_CTX) |
1345 XHCI_TRB_3_SLOT_SET(slot_id);
1346 trb.dwTrb3 = htole32(temp);
1348 return (xhci_do_command(sc, &trb, 100 /* ms */));
1352 xhci_cmd_reset_ep(struct xhci_softc *sc, uint8_t preserve,
1353 uint8_t ep_id, uint8_t slot_id)
1355 struct xhci_trb trb;
1362 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_EP) |
1363 XHCI_TRB_3_SLOT_SET(slot_id) |
1364 XHCI_TRB_3_EP_SET(ep_id);
1367 temp |= XHCI_TRB_3_PRSV_BIT;
1369 trb.dwTrb3 = htole32(temp);
1371 return (xhci_do_command(sc, &trb, 100 /* ms */));
1375 xhci_cmd_set_tr_dequeue_ptr(struct xhci_softc *sc, uint64_t dequeue_ptr,
1376 uint16_t stream_id, uint8_t ep_id, uint8_t slot_id)
1378 struct xhci_trb trb;
1383 trb.qwTrb0 = htole64(dequeue_ptr);
1385 temp = XHCI_TRB_2_STREAM_SET(stream_id);
1386 trb.dwTrb2 = htole32(temp);
1388 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SET_TR_DEQUEUE) |
1389 XHCI_TRB_3_SLOT_SET(slot_id) |
1390 XHCI_TRB_3_EP_SET(ep_id);
1391 trb.dwTrb3 = htole32(temp);
1393 return (xhci_do_command(sc, &trb, 100 /* ms */));
1397 xhci_cmd_stop_ep(struct xhci_softc *sc, uint8_t suspend,
1398 uint8_t ep_id, uint8_t slot_id)
1400 struct xhci_trb trb;
1407 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STOP_EP) |
1408 XHCI_TRB_3_SLOT_SET(slot_id) |
1409 XHCI_TRB_3_EP_SET(ep_id);
1412 temp |= XHCI_TRB_3_SUSP_EP_BIT;
1414 trb.dwTrb3 = htole32(temp);
1416 return (xhci_do_command(sc, &trb, 100 /* ms */));
1420 xhci_cmd_reset_dev(struct xhci_softc *sc, uint8_t slot_id)
1422 struct xhci_trb trb;
1429 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_DEVICE) |
1430 XHCI_TRB_3_SLOT_SET(slot_id);
1432 trb.dwTrb3 = htole32(temp);
1434 return (xhci_do_command(sc, &trb, 100 /* ms */));
1437 /*------------------------------------------------------------------------*
1438 * xhci_interrupt - XHCI interrupt handler
1439 *------------------------------------------------------------------------*/
1441 xhci_interrupt(struct xhci_softc *sc)
1446 USB_BUS_LOCK(&sc->sc_bus);
1448 status = XREAD4(sc, oper, XHCI_USBSTS);
1450 /* acknowledge interrupts */
1452 XWRITE4(sc, oper, XHCI_USBSTS, status);
1454 temp = XREAD4(sc, runt, XHCI_IMAN(0));
1456 /* acknowledge pending event */
1458 XWRITE4(sc, runt, XHCI_IMAN(0), temp);
1460 DPRINTFN(16, "real interrupt (sts=0x%08x, "
1461 "iman=0x%08x)\n", status, temp);
1464 if (status & XHCI_STS_PCD) {
1468 if (status & XHCI_STS_HCH) {
1469 printf("%s: host controller halted\n",
1473 if (status & XHCI_STS_HSE) {
1474 printf("%s: host system error\n",
1478 if (status & XHCI_STS_HCE) {
1479 printf("%s: host controller error\n",
1484 xhci_interrupt_poll(sc);
1486 USB_BUS_UNLOCK(&sc->sc_bus);
1489 /*------------------------------------------------------------------------*
1490 * xhci_timeout - XHCI timeout handler
1491 *------------------------------------------------------------------------*/
1493 xhci_timeout(void *arg)
1495 struct usb_xfer *xfer = arg;
1497 DPRINTF("xfer=%p\n", xfer);
1499 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1501 /* transfer is transferred */
1502 xhci_device_done(xfer, USB_ERR_TIMEOUT);
1506 xhci_do_poll(struct usb_bus *bus)
1508 struct xhci_softc *sc = XHCI_BUS2SC(bus);
1510 USB_BUS_LOCK(&sc->sc_bus);
1511 xhci_interrupt_poll(sc);
1512 USB_BUS_UNLOCK(&sc->sc_bus);
1516 xhci_setup_generic_chain_sub(struct xhci_std_temp *temp)
1518 struct usb_page_search buf_res;
1520 struct xhci_td *td_next;
1521 struct xhci_td *td_alt_next;
1522 uint32_t buf_offset;
1526 uint8_t shortpkt_old;
1532 shortpkt_old = temp->shortpkt;
1533 len_old = temp->len;
1539 td_next = temp->td_next;
1543 if (temp->len == 0) {
1548 /* send a Zero Length Packet, ZLP, last */
1555 average = temp->average;
1557 if (temp->len < average) {
1558 if (temp->len % temp->max_packet_size) {
1561 average = temp->len;
1565 if (td_next == NULL)
1566 panic("%s: out of XHCI transfer descriptors!", __FUNCTION__);
1571 td_next = td->obj_next;
1573 /* check if we are pre-computing */
1577 /* update remaining length */
1579 temp->len -= average;
1583 /* fill out current TD */
1589 /* update remaining length */
1591 temp->len -= average;
1593 /* reset TRB index */
1597 if (temp->trb_type == XHCI_TRB_TYPE_SETUP_STAGE) {
1598 /* immediate data */
1603 td->td_trb[0].qwTrb0 = 0;
1605 usbd_copy_out(temp->pc, temp->offset + buf_offset,
1606 (uint8_t *)(uintptr_t)&td->td_trb[0].qwTrb0,
1609 dword = XHCI_TRB_2_BYTES_SET(8) |
1610 XHCI_TRB_2_TDSZ_SET(0) |
1611 XHCI_TRB_2_IRQ_SET(0);
1613 td->td_trb[0].dwTrb2 = htole32(dword);
1615 dword = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SETUP_STAGE) |
1616 XHCI_TRB_3_IDT_BIT | XHCI_TRB_3_CYCLE_BIT;
1619 if (td->td_trb[0].qwTrb0 &
1620 htole64(XHCI_TRB_0_WLENGTH_MASK)) {
1621 if (td->td_trb[0].qwTrb0 & htole64(1))
1622 dword |= XHCI_TRB_3_TRT_IN;
1624 dword |= XHCI_TRB_3_TRT_OUT;
1627 td->td_trb[0].dwTrb3 = htole32(dword);
1629 xhci_dump_trb(&td->td_trb[x]);
1637 /* fill out buffer pointers */
1641 memset(&buf_res, 0, sizeof(buf_res));
1643 usbd_get_page(temp->pc, temp->offset +
1644 buf_offset, &buf_res);
1646 /* get length to end of page */
1647 if (buf_res.length > average)
1648 buf_res.length = average;
1650 /* check for maximum length */
1651 if (buf_res.length > XHCI_TD_PAGE_SIZE)
1652 buf_res.length = XHCI_TD_PAGE_SIZE;
1655 npkt = (average + temp->max_packet_size - 1) /
1656 temp->max_packet_size;
1662 /* fill out TRB's */
1663 td->td_trb[x].qwTrb0 =
1664 htole64((uint64_t)buf_res.physaddr);
1667 XHCI_TRB_2_BYTES_SET(buf_res.length) |
1668 XHCI_TRB_2_TDSZ_SET(npkt) |
1669 XHCI_TRB_2_IRQ_SET(0);
1671 td->td_trb[x].dwTrb2 = htole32(dword);
1673 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1674 XHCI_TRB_3_TYPE_SET(temp->trb_type) |
1675 (temp->do_isoc_sync ?
1676 XHCI_TRB_3_FRID_SET(temp->isoc_frame / 8) :
1677 XHCI_TRB_3_ISO_SIA_BIT) |
1678 XHCI_TRB_3_TBC_SET(temp->tbc) |
1679 XHCI_TRB_3_TLBPC_SET(temp->tlbpc);
1681 temp->do_isoc_sync = 0;
1683 if (temp->direction == UE_DIR_IN) {
1684 dword |= XHCI_TRB_3_DIR_IN;
1687 * NOTE: Only the SETUP stage should
1688 * use the IDT bit. Else transactions
1689 * can be sent using the wrong data
1692 if (temp->trb_type !=
1693 XHCI_TRB_TYPE_SETUP_STAGE &&
1695 XHCI_TRB_TYPE_STATUS_STAGE)
1696 dword |= XHCI_TRB_3_ISP_BIT;
1699 td->td_trb[x].dwTrb3 = htole32(dword);
1701 average -= buf_res.length;
1702 buf_offset += buf_res.length;
1704 xhci_dump_trb(&td->td_trb[x]);
1708 } while (average != 0);
1710 td->td_trb[x-1].dwTrb3 |= htole32(XHCI_TRB_3_IOC_BIT);
1712 /* store number of data TRB's */
1716 DPRINTF("NTRB=%u\n", x);
1718 /* fill out link TRB */
1720 if (td_next != NULL) {
1721 /* link the current TD with the next one */
1722 td->td_trb[x].qwTrb0 = htole64((uint64_t)td_next->td_self);
1723 DPRINTF("LINK=0x%08llx\n", (long long)td_next->td_self);
1725 /* this field will get updated later */
1726 DPRINTF("NOLINK\n");
1729 dword = XHCI_TRB_2_IRQ_SET(0);
1731 td->td_trb[x].dwTrb2 = htole32(dword);
1733 dword = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) |
1734 XHCI_TRB_3_CYCLE_BIT | XHCI_TRB_3_IOC_BIT;
1736 td->td_trb[x].dwTrb3 = htole32(dword);
1738 td->alt_next = td_alt_next;
1740 xhci_dump_trb(&td->td_trb[x]);
1742 usb_pc_cpu_flush(td->page_cache);
1748 /* setup alt next pointer, if any */
1749 if (temp->last_frame) {
1752 /* we use this field internally */
1753 td_alt_next = td_next;
1757 temp->shortpkt = shortpkt_old;
1758 temp->len = len_old;
1762 /* remove cycle bit from first if we are stepping the TRBs */
1764 td->td_trb[0].dwTrb3 &= ~htole32(XHCI_TRB_3_CYCLE_BIT);
1766 /* remove chain bit because this is the last TRB in the chain */
1767 td->td_trb[td->ntrb - 1].dwTrb2 &= ~htole32(XHCI_TRB_2_TDSZ_SET(15));
1768 td->td_trb[td->ntrb - 1].dwTrb3 &= ~htole32(XHCI_TRB_3_CHAIN_BIT);
1770 usb_pc_cpu_flush(td->page_cache);
1773 temp->td_next = td_next;
1777 xhci_setup_generic_chain(struct usb_xfer *xfer)
1779 struct xhci_std_temp temp;
1785 temp.do_isoc_sync = 0;
1789 temp.average = xfer->max_hc_frame_size;
1790 temp.max_packet_size = xfer->max_packet_size;
1791 temp.sc = XHCI_BUS2SC(xfer->xroot->bus);
1793 temp.last_frame = 0;
1795 temp.multishort = xfer->flags_int.isochronous_xfr ||
1796 xfer->flags_int.control_xfr ||
1797 xfer->flags_int.short_frames_ok;
1799 /* toggle the DMA set we are using */
1800 xfer->flags_int.curr_dma_set ^= 1;
1802 /* get next DMA set */
1803 td = xfer->td_start[xfer->flags_int.curr_dma_set];
1808 xfer->td_transfer_first = td;
1809 xfer->td_transfer_cache = td;
1811 if (xfer->flags_int.isochronous_xfr) {
1814 /* compute multiplier for ISOCHRONOUS transfers */
1815 mult = xfer->endpoint->ecomp ?
1816 (xfer->endpoint->ecomp->bmAttributes & 3) : 0;
1817 /* check for USB 2.0 multiplier */
1819 mult = (xfer->endpoint->edesc->
1820 wMaxPacketSize[1] >> 3) & 3;
1828 x = XREAD4(temp.sc, runt, XHCI_MFINDEX);
1830 DPRINTF("MFINDEX=0x%08x\n", x);
1832 switch (usbd_get_speed(xfer->xroot->udev)) {
1833 case USB_SPEED_FULL:
1835 temp.isoc_delta = 8; /* 1ms */
1836 x += temp.isoc_delta - 1;
1837 x &= ~(temp.isoc_delta - 1);
1840 shift = usbd_xfer_get_fps_shift(xfer);
1841 temp.isoc_delta = 1U << shift;
1842 x += temp.isoc_delta - 1;
1843 x &= ~(temp.isoc_delta - 1);
1844 /* simple frame load balancing */
1845 x += xfer->endpoint->usb_uframe;
1849 y = XHCI_MFINDEX_GET(x - xfer->endpoint->isoc_next);
1851 if ((xfer->endpoint->is_synced == 0) ||
1852 (y < (xfer->nframes << shift)) ||
1853 (XHCI_MFINDEX_GET(-y) >= (128 * 8))) {
1855 * If there is data underflow or the pipe
1856 * queue is empty we schedule the transfer a
1857 * few frames ahead of the current frame
1858 * position. Else two isochronous transfers
1861 xfer->endpoint->isoc_next = XHCI_MFINDEX_GET(x + (3 * 8));
1862 xfer->endpoint->is_synced = 1;
1863 temp.do_isoc_sync = 1;
1865 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
1868 /* compute isochronous completion time */
1870 y = XHCI_MFINDEX_GET(xfer->endpoint->isoc_next - (x & ~7));
1872 xfer->isoc_time_complete =
1873 usb_isoc_time_expand(&temp.sc->sc_bus, x / 8) +
1874 (y / 8) + (((xfer->nframes << shift) + 7) / 8);
1877 temp.isoc_frame = xfer->endpoint->isoc_next;
1878 temp.trb_type = XHCI_TRB_TYPE_ISOCH;
1880 xfer->endpoint->isoc_next += xfer->nframes << shift;
1882 } else if (xfer->flags_int.control_xfr) {
1884 /* check if we should prepend a setup message */
1886 if (xfer->flags_int.control_hdr) {
1888 temp.len = xfer->frlengths[0];
1889 temp.pc = xfer->frbuffers + 0;
1890 temp.shortpkt = temp.len ? 1 : 0;
1891 temp.trb_type = XHCI_TRB_TYPE_SETUP_STAGE;
1894 /* check for last frame */
1895 if (xfer->nframes == 1) {
1896 /* no STATUS stage yet, SETUP is last */
1897 if (xfer->flags_int.control_act)
1898 temp.last_frame = 1;
1901 xhci_setup_generic_chain_sub(&temp);
1905 temp.isoc_delta = 0;
1906 temp.isoc_frame = 0;
1907 temp.trb_type = XHCI_TRB_TYPE_DATA_STAGE;
1911 temp.isoc_delta = 0;
1912 temp.isoc_frame = 0;
1913 temp.trb_type = XHCI_TRB_TYPE_NORMAL;
1916 if (x != xfer->nframes) {
1917 /* setup page_cache pointer */
1918 temp.pc = xfer->frbuffers + x;
1919 /* set endpoint direction */
1920 temp.direction = UE_GET_DIR(xfer->endpointno);
1923 while (x != xfer->nframes) {
1925 /* DATA0 / DATA1 message */
1927 temp.len = xfer->frlengths[x];
1928 temp.step_td = ((xfer->endpointno & UE_DIR_IN) &&
1929 x != 0 && temp.multishort == 0);
1933 if (x == xfer->nframes) {
1934 if (xfer->flags_int.control_xfr) {
1935 /* no STATUS stage yet, DATA is last */
1936 if (xfer->flags_int.control_act)
1937 temp.last_frame = 1;
1939 temp.last_frame = 1;
1942 if (temp.len == 0) {
1944 /* make sure that we send an USB packet */
1949 temp.tlbpc = mult - 1;
1951 } else if (xfer->flags_int.isochronous_xfr) {
1956 * Isochronous transfers don't have short
1957 * packet termination:
1962 /* isochronous transfers have a transfer limit */
1964 if (temp.len > xfer->max_frame_size)
1965 temp.len = xfer->max_frame_size;
1967 /* compute TD packet count */
1968 tdpc = (temp.len + xfer->max_packet_size - 1) /
1969 xfer->max_packet_size;
1971 temp.tbc = ((tdpc + mult - 1) / mult) - 1;
1972 temp.tlbpc = (tdpc % mult);
1974 if (temp.tlbpc == 0)
1975 temp.tlbpc = mult - 1;
1980 /* regular data transfer */
1982 temp.shortpkt = xfer->flags.force_short_xfer ? 0 : 1;
1985 xhci_setup_generic_chain_sub(&temp);
1987 if (xfer->flags_int.isochronous_xfr) {
1988 temp.offset += xfer->frlengths[x - 1];
1989 temp.isoc_frame += temp.isoc_delta;
1991 /* get next Page Cache pointer */
1992 temp.pc = xfer->frbuffers + x;
1996 /* check if we should append a status stage */
1998 if (xfer->flags_int.control_xfr &&
1999 !xfer->flags_int.control_act) {
2002 * Send a DATA1 message and invert the current
2003 * endpoint direction.
2005 temp.step_td = (xfer->nframes != 0);
2006 temp.direction = UE_GET_DIR(xfer->endpointno) ^ UE_DIR_IN;
2010 temp.last_frame = 1;
2011 temp.trb_type = XHCI_TRB_TYPE_STATUS_STAGE;
2013 xhci_setup_generic_chain_sub(&temp);
2018 /* must have at least one frame! */
2020 xfer->td_transfer_last = td;
2022 DPRINTF("first=%p last=%p\n", xfer->td_transfer_first, td);
2026 xhci_set_slot_pointer(struct xhci_softc *sc, uint8_t index, uint64_t dev_addr)
2028 struct usb_page_search buf_res;
2029 struct xhci_dev_ctx_addr *pdctxa;
2031 usbd_get_page(&sc->sc_hw.ctx_pc, 0, &buf_res);
2033 pdctxa = buf_res.buffer;
2035 DPRINTF("addr[%u]=0x%016llx\n", index, (long long)dev_addr);
2037 pdctxa->qwBaaDevCtxAddr[index] = htole64(dev_addr);
2039 usb_pc_cpu_flush(&sc->sc_hw.ctx_pc);
2043 xhci_configure_mask(struct usb_device *udev, uint32_t mask, uint8_t drop)
2045 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2046 struct usb_page_search buf_inp;
2047 struct xhci_input_dev_ctx *pinp;
2050 index = udev->controller_slot_id;
2052 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
2054 pinp = buf_inp.buffer;
2057 mask &= XHCI_INCTX_NON_CTRL_MASK;
2058 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx0, mask);
2059 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx1, 0);
2061 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx0, 0);
2062 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx1, mask);
2068 xhci_configure_endpoint(struct usb_device *udev,
2069 struct usb_endpoint_descriptor *edesc, uint64_t ring_addr,
2070 uint16_t interval, uint8_t max_packet_count, uint8_t mult,
2071 uint8_t fps_shift, uint16_t max_packet_size, uint16_t max_frame_size)
2073 struct usb_page_search buf_inp;
2074 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2075 struct xhci_input_dev_ctx *pinp;
2081 index = udev->controller_slot_id;
2083 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
2085 pinp = buf_inp.buffer;
2087 epno = edesc->bEndpointAddress;
2088 type = edesc->bmAttributes & UE_XFERTYPE;
2090 if (type == UE_CONTROL)
2093 epno = XHCI_EPNO2EPID(epno);
2096 return (USB_ERR_NO_PIPE); /* invalid */
2098 if (max_packet_count == 0)
2099 return (USB_ERR_BAD_BUFSIZE);
2104 return (USB_ERR_BAD_BUFSIZE);
2106 temp = XHCI_EPCTX_0_EPSTATE_SET(0) |
2107 XHCI_EPCTX_0_MAXP_STREAMS_SET(0) |
2108 XHCI_EPCTX_0_LSA_SET(0);
2110 switch (udev->speed) {
2111 case USB_SPEED_FULL:
2124 temp |= XHCI_EPCTX_0_IVAL_SET(fps_shift);
2126 case UE_ISOCHRONOUS:
2127 temp |= XHCI_EPCTX_0_IVAL_SET(fps_shift);
2129 switch (udev->speed) {
2130 case USB_SPEED_SUPER:
2133 temp |= XHCI_EPCTX_0_MULT_SET(mult - 1);
2134 max_packet_count /= mult;
2144 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx0, temp);
2147 XHCI_EPCTX_1_HID_SET(0) |
2148 XHCI_EPCTX_1_MAXB_SET(max_packet_count) |
2149 XHCI_EPCTX_1_MAXP_SIZE_SET(max_packet_size);
2151 if ((udev->parent_hs_hub != NULL) || (udev->address != 0)) {
2152 if (type != UE_ISOCHRONOUS)
2153 temp |= XHCI_EPCTX_1_CERR_SET(3);
2158 temp |= XHCI_EPCTX_1_EPTYPE_SET(4);
2160 case UE_ISOCHRONOUS:
2161 temp |= XHCI_EPCTX_1_EPTYPE_SET(1);
2164 temp |= XHCI_EPCTX_1_EPTYPE_SET(2);
2167 temp |= XHCI_EPCTX_1_EPTYPE_SET(3);
2171 /* check for IN direction */
2173 temp |= XHCI_EPCTX_1_EPTYPE_SET(4);
2175 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx1, temp);
2177 ring_addr |= XHCI_EPCTX_2_DCS_SET(1);
2179 xhci_ctx_set_le64(sc, &pinp->ctx_ep[epno - 1].qwEpCtx2, ring_addr);
2181 switch (edesc->bmAttributes & UE_XFERTYPE) {
2183 case UE_ISOCHRONOUS:
2184 temp = XHCI_EPCTX_4_MAX_ESIT_PAYLOAD_SET(max_frame_size) |
2185 XHCI_EPCTX_4_AVG_TRB_LEN_SET(MIN(XHCI_PAGE_SIZE,
2189 temp = XHCI_EPCTX_4_AVG_TRB_LEN_SET(8);
2192 temp = XHCI_EPCTX_4_AVG_TRB_LEN_SET(XHCI_PAGE_SIZE);
2196 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx4, temp);
2199 xhci_dump_endpoint(sc, &pinp->ctx_ep[epno - 1]);
2201 usb_pc_cpu_flush(&sc->sc_hw.devs[index].input_pc);
2203 return (0); /* success */
2207 xhci_configure_endpoint_by_xfer(struct usb_xfer *xfer)
2209 struct xhci_endpoint_ext *pepext;
2210 struct usb_endpoint_ss_comp_descriptor *ecomp;
2212 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2213 xfer->endpoint->edesc);
2215 ecomp = xfer->endpoint->ecomp;
2217 pepext->trb[0].dwTrb3 = 0; /* halt any transfers */
2218 usb_pc_cpu_flush(pepext->page_cache);
2220 return (xhci_configure_endpoint(xfer->xroot->udev,
2221 xfer->endpoint->edesc, pepext->physaddr,
2222 xfer->interval, xfer->max_packet_count,
2223 (ecomp != NULL) ? (ecomp->bmAttributes & 3) + 1 : 1,
2224 usbd_xfer_get_fps_shift(xfer), xfer->max_packet_size,
2225 xfer->max_frame_size));
2229 xhci_configure_device(struct usb_device *udev)
2231 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2232 struct usb_page_search buf_inp;
2233 struct usb_page_cache *pcinp;
2234 struct xhci_input_dev_ctx *pinp;
2235 struct usb_device *hubdev;
2243 index = udev->controller_slot_id;
2245 DPRINTF("index=%u\n", index);
2247 pcinp = &sc->sc_hw.devs[index].input_pc;
2249 usbd_get_page(pcinp, 0, &buf_inp);
2251 pinp = buf_inp.buffer;
2256 /* figure out route string and root HUB port number */
2258 for (hubdev = udev; hubdev != NULL; hubdev = hubdev->parent_hub) {
2260 if (hubdev->parent_hub == NULL)
2263 depth = hubdev->parent_hub->depth;
2266 * NOTE: HS/FS/LS devices and the SS root HUB can have
2267 * more than 15 ports
2270 rh_port = hubdev->port_no;
2279 route |= rh_port << (4 * (depth - 1));
2282 DPRINTF("Route=0x%08x\n", route);
2284 temp = XHCI_SCTX_0_ROUTE_SET(route);
2286 switch (sc->sc_hw.devs[index].state) {
2287 case XHCI_ST_CONFIGURED:
2288 temp |= XHCI_SCTX_0_CTX_NUM_SET(XHCI_MAX_ENDPOINTS - 1);
2291 temp |= XHCI_SCTX_0_CTX_NUM_SET(1);
2295 switch (udev->speed) {
2297 temp |= XHCI_SCTX_0_SPEED_SET(2);
2298 if (udev->parent_hs_hub != NULL &&
2299 udev->parent_hs_hub->ddesc.bDeviceProtocol ==
2301 DPRINTF("Device inherits MTT\n");
2302 temp |= XHCI_SCTX_0_MTT_SET(1);
2305 case USB_SPEED_HIGH:
2306 temp |= XHCI_SCTX_0_SPEED_SET(3);
2307 if (sc->sc_hw.devs[index].nports != 0 &&
2308 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) {
2309 DPRINTF("HUB supports MTT\n");
2310 temp |= XHCI_SCTX_0_MTT_SET(1);
2313 case USB_SPEED_FULL:
2314 temp |= XHCI_SCTX_0_SPEED_SET(1);
2315 if (udev->parent_hs_hub != NULL &&
2316 udev->parent_hs_hub->ddesc.bDeviceProtocol ==
2318 DPRINTF("Device inherits MTT\n");
2319 temp |= XHCI_SCTX_0_MTT_SET(1);
2323 temp |= XHCI_SCTX_0_SPEED_SET(4);
2327 is_hub = sc->sc_hw.devs[index].nports != 0 &&
2328 (udev->speed == USB_SPEED_SUPER ||
2329 udev->speed == USB_SPEED_HIGH);
2332 temp |= XHCI_SCTX_0_HUB_SET(1);
2334 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx0, temp);
2336 temp = XHCI_SCTX_1_RH_PORT_SET(rh_port);
2339 temp |= XHCI_SCTX_1_NUM_PORTS_SET(
2340 sc->sc_hw.devs[index].nports);
2343 switch (udev->speed) {
2344 case USB_SPEED_SUPER:
2345 switch (sc->sc_hw.devs[index].state) {
2346 case XHCI_ST_ADDRESSED:
2347 case XHCI_ST_CONFIGURED:
2348 /* enable power save */
2349 temp |= XHCI_SCTX_1_MAX_EL_SET(sc->sc_exit_lat_max);
2352 /* disable power save */
2360 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx1, temp);
2362 temp = XHCI_SCTX_2_IRQ_TARGET_SET(0);
2365 temp |= XHCI_SCTX_2_TT_THINK_TIME_SET(
2366 sc->sc_hw.devs[index].tt);
2369 hubdev = udev->parent_hs_hub;
2371 /* check if we should activate the transaction translator */
2372 switch (udev->speed) {
2373 case USB_SPEED_FULL:
2375 if (hubdev != NULL) {
2376 temp |= XHCI_SCTX_2_TT_HUB_SID_SET(
2377 hubdev->controller_slot_id);
2378 temp |= XHCI_SCTX_2_TT_PORT_NUM_SET(
2386 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx2, temp);
2388 temp = XHCI_SCTX_3_DEV_ADDR_SET(udev->address) |
2389 XHCI_SCTX_3_SLOT_STATE_SET(0);
2391 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx3, temp);
2394 xhci_dump_device(sc, &pinp->ctx_slot);
2396 usb_pc_cpu_flush(pcinp);
2398 return (0); /* success */
2402 xhci_alloc_device_ext(struct usb_device *udev)
2404 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2405 struct usb_page_search buf_dev;
2406 struct usb_page_search buf_ep;
2407 struct xhci_trb *trb;
2408 struct usb_page_cache *pc;
2409 struct usb_page *pg;
2414 index = udev->controller_slot_id;
2416 pc = &sc->sc_hw.devs[index].device_pc;
2417 pg = &sc->sc_hw.devs[index].device_pg;
2419 /* need to initialize the page cache */
2420 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2422 if (usb_pc_alloc_mem(pc, pg, sc->sc_ctx_is_64_byte ?
2423 (2 * sizeof(struct xhci_dev_ctx)) :
2424 sizeof(struct xhci_dev_ctx), XHCI_PAGE_SIZE))
2427 usbd_get_page(pc, 0, &buf_dev);
2429 pc = &sc->sc_hw.devs[index].input_pc;
2430 pg = &sc->sc_hw.devs[index].input_pg;
2432 /* need to initialize the page cache */
2433 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2435 if (usb_pc_alloc_mem(pc, pg, sc->sc_ctx_is_64_byte ?
2436 (2 * sizeof(struct xhci_input_dev_ctx)) :
2437 sizeof(struct xhci_input_dev_ctx), XHCI_PAGE_SIZE))
2440 pc = &sc->sc_hw.devs[index].endpoint_pc;
2441 pg = &sc->sc_hw.devs[index].endpoint_pg;
2443 /* need to initialize the page cache */
2444 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2446 if (usb_pc_alloc_mem(pc, pg, sizeof(struct xhci_dev_endpoint_trbs), XHCI_PAGE_SIZE))
2449 /* initialise all endpoint LINK TRBs */
2451 for (i = 0; i != XHCI_MAX_ENDPOINTS; i++) {
2453 /* lookup endpoint TRB ring */
2454 usbd_get_page(pc, (uintptr_t)&((struct xhci_dev_endpoint_trbs *)0)->trb[i][0], &buf_ep);
2456 /* get TRB pointer */
2457 trb = buf_ep.buffer;
2458 trb += XHCI_MAX_TRANSFERS - 1;
2460 /* get TRB start address */
2461 addr = buf_ep.physaddr;
2463 /* create LINK TRB */
2464 trb->qwTrb0 = htole64(addr);
2465 trb->dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
2466 trb->dwTrb3 = htole32(XHCI_TRB_3_CYCLE_BIT |
2467 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
2470 usb_pc_cpu_flush(pc);
2472 xhci_set_slot_pointer(sc, index, buf_dev.physaddr);
2477 xhci_free_device_ext(udev);
2479 return (USB_ERR_NOMEM);
2483 xhci_free_device_ext(struct usb_device *udev)
2485 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2488 index = udev->controller_slot_id;
2489 xhci_set_slot_pointer(sc, index, 0);
2491 usb_pc_free_mem(&sc->sc_hw.devs[index].device_pc);
2492 usb_pc_free_mem(&sc->sc_hw.devs[index].input_pc);
2493 usb_pc_free_mem(&sc->sc_hw.devs[index].endpoint_pc);
2496 static struct xhci_endpoint_ext *
2497 xhci_get_endpoint_ext(struct usb_device *udev, struct usb_endpoint_descriptor *edesc)
2499 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2500 struct xhci_endpoint_ext *pepext;
2501 struct usb_page_cache *pc;
2502 struct usb_page_search buf_ep;
2506 epno = edesc->bEndpointAddress;
2507 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL)
2510 epno = XHCI_EPNO2EPID(epno);
2512 index = udev->controller_slot_id;
2514 pc = &sc->sc_hw.devs[index].endpoint_pc;
2516 usbd_get_page(pc, (uintptr_t)&((struct xhci_dev_endpoint_trbs *)0)->trb[epno][0], &buf_ep);
2518 pepext = &sc->sc_hw.devs[index].endp[epno];
2519 pepext->page_cache = pc;
2520 pepext->trb = buf_ep.buffer;
2521 pepext->physaddr = buf_ep.physaddr;
2527 xhci_endpoint_doorbell(struct usb_xfer *xfer)
2529 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
2533 epno = xfer->endpointno;
2534 if (xfer->flags_int.control_xfr)
2537 epno = XHCI_EPNO2EPID(epno);
2538 index = xfer->xroot->udev->controller_slot_id;
2540 if (xfer->xroot->udev->flags.self_suspended == 0)
2541 XWRITE4(sc, door, XHCI_DOORBELL(index), epno | XHCI_DB_SID_SET(0));
2545 xhci_transfer_remove(struct usb_xfer *xfer, usb_error_t error)
2547 struct xhci_endpoint_ext *pepext;
2549 if (xfer->flags_int.bandwidth_reclaimed) {
2550 xfer->flags_int.bandwidth_reclaimed = 0;
2552 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2553 xfer->endpoint->edesc);
2557 pepext->xfer[xfer->qh_pos] = NULL;
2559 if (error && pepext->trb_running != 0) {
2560 pepext->trb_halted = 1;
2561 pepext->trb_running = 0;
2567 xhci_transfer_insert(struct usb_xfer *xfer)
2569 struct xhci_td *td_first;
2570 struct xhci_td *td_last;
2571 struct xhci_endpoint_ext *pepext;
2579 /* check if already inserted */
2580 if (xfer->flags_int.bandwidth_reclaimed) {
2581 DPRINTFN(8, "Already in schedule\n");
2585 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2586 xfer->endpoint->edesc);
2588 td_first = xfer->td_transfer_first;
2589 td_last = xfer->td_transfer_last;
2590 addr = pepext->physaddr;
2592 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
2595 /* single buffered */
2599 /* multi buffered */
2600 trb_limit = (XHCI_MAX_TRANSFERS - 2);
2604 if (pepext->trb_used >= trb_limit) {
2605 DPRINTFN(8, "Too many TDs queued.\n");
2606 return (USB_ERR_NOMEM);
2609 /* check for stopped condition, after putting transfer on interrupt queue */
2610 if (pepext->trb_running == 0) {
2611 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
2613 DPRINTFN(8, "Not running\n");
2615 /* start configuration */
2616 (void)usb_proc_msignal(&sc->sc_config_proc,
2617 &sc->sc_config_msg[0], &sc->sc_config_msg[1]);
2623 /* get current TRB index */
2624 i = pepext->trb_index;
2626 /* get next TRB index */
2629 /* the last entry of the ring is a hardcoded link TRB */
2630 if (inext >= (XHCI_MAX_TRANSFERS - 1))
2633 /* compute terminating return address */
2634 addr += inext * sizeof(struct xhci_trb);
2636 /* update next pointer of last link TRB */
2637 td_last->td_trb[td_last->ntrb].qwTrb0 = htole64(addr);
2638 td_last->td_trb[td_last->ntrb].dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
2639 td_last->td_trb[td_last->ntrb].dwTrb3 = htole32(XHCI_TRB_3_IOC_BIT |
2640 XHCI_TRB_3_CYCLE_BIT | XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
2643 xhci_dump_trb(&td_last->td_trb[td_last->ntrb]);
2645 usb_pc_cpu_flush(td_last->page_cache);
2647 /* write ahead chain end marker */
2649 pepext->trb[inext].qwTrb0 = 0;
2650 pepext->trb[inext].dwTrb2 = 0;
2651 pepext->trb[inext].dwTrb3 = 0;
2653 /* update next pointer of link TRB */
2655 pepext->trb[i].qwTrb0 = htole64((uint64_t)td_first->td_self);
2656 pepext->trb[i].dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
2659 xhci_dump_trb(&pepext->trb[i]);
2661 usb_pc_cpu_flush(pepext->page_cache);
2663 /* toggle cycle bit which activates the transfer chain */
2665 pepext->trb[i].dwTrb3 = htole32(XHCI_TRB_3_CYCLE_BIT |
2666 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
2668 usb_pc_cpu_flush(pepext->page_cache);
2670 DPRINTF("qh_pos = %u\n", i);
2672 pepext->xfer[i] = xfer;
2676 xfer->flags_int.bandwidth_reclaimed = 1;
2678 pepext->trb_index = inext;
2680 xhci_endpoint_doorbell(xfer);
2686 xhci_root_intr(struct xhci_softc *sc)
2690 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2692 /* clear any old interrupt data */
2693 memset(sc->sc_hub_idata, 0, sizeof(sc->sc_hub_idata));
2695 for (i = 1; i <= sc->sc_noport; i++) {
2696 /* pick out CHANGE bits from the status register */
2697 if (XREAD4(sc, oper, XHCI_PORTSC(i)) & (
2698 XHCI_PS_CSC | XHCI_PS_PEC |
2699 XHCI_PS_OCC | XHCI_PS_WRC |
2700 XHCI_PS_PRC | XHCI_PS_PLC |
2702 sc->sc_hub_idata[i / 8] |= 1 << (i % 8);
2703 DPRINTF("port %d changed\n", i);
2706 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
2707 sizeof(sc->sc_hub_idata));
2710 /*------------------------------------------------------------------------*
2711 * xhci_device_done - XHCI done handler
2713 * NOTE: This function can be called two times in a row on
2714 * the same USB transfer. From close and from interrupt.
2715 *------------------------------------------------------------------------*/
2717 xhci_device_done(struct usb_xfer *xfer, usb_error_t error)
2719 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
2720 xfer, xfer->endpoint, error);
2722 /* remove transfer from HW queue */
2723 xhci_transfer_remove(xfer, error);
2725 /* dequeue transfer and start next transfer */
2726 usbd_transfer_done(xfer, error);
2729 /*------------------------------------------------------------------------*
2730 * XHCI data transfer support (generic type)
2731 *------------------------------------------------------------------------*/
2733 xhci_device_generic_open(struct usb_xfer *xfer)
2735 if (xfer->flags_int.isochronous_xfr) {
2736 switch (xfer->xroot->udev->speed) {
2737 case USB_SPEED_FULL:
2740 usb_hs_bandwidth_alloc(xfer);
2747 xhci_device_generic_close(struct usb_xfer *xfer)
2751 xhci_device_done(xfer, USB_ERR_CANCELLED);
2753 if (xfer->flags_int.isochronous_xfr) {
2754 switch (xfer->xroot->udev->speed) {
2755 case USB_SPEED_FULL:
2758 usb_hs_bandwidth_free(xfer);
2765 xhci_device_generic_multi_enter(struct usb_endpoint *ep,
2766 struct usb_xfer *enter_xfer)
2768 struct usb_xfer *xfer;
2770 /* check if there is a current transfer */
2771 xfer = ep->endpoint_q.curr;
2776 * Check if the current transfer is started and then pickup
2777 * the next one, if any. Else wait for next start event due to
2778 * block on failure feature.
2780 if (!xfer->flags_int.bandwidth_reclaimed)
2783 xfer = TAILQ_FIRST(&ep->endpoint_q.head);
2786 * In case of enter we have to consider that the
2787 * transfer is queued by the USB core after the enter
2796 /* try to multi buffer */
2797 xhci_transfer_insert(xfer);
2801 xhci_device_generic_enter(struct usb_xfer *xfer)
2805 /* setup TD's and QH */
2806 xhci_setup_generic_chain(xfer);
2808 xhci_device_generic_multi_enter(xfer->endpoint, xfer);
2812 xhci_device_generic_start(struct usb_xfer *xfer)
2816 /* try to insert xfer on HW queue */
2817 xhci_transfer_insert(xfer);
2819 /* try to multi buffer */
2820 xhci_device_generic_multi_enter(xfer->endpoint, NULL);
2822 /* add transfer last on interrupt queue */
2823 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
2825 /* start timeout, if any */
2826 if (xfer->timeout != 0)
2827 usbd_transfer_timeout_ms(xfer, &xhci_timeout, xfer->timeout);
2830 struct usb_pipe_methods xhci_device_generic_methods =
2832 .open = xhci_device_generic_open,
2833 .close = xhci_device_generic_close,
2834 .enter = xhci_device_generic_enter,
2835 .start = xhci_device_generic_start,
2838 /*------------------------------------------------------------------------*
2839 * xhci root HUB support
2840 *------------------------------------------------------------------------*
2841 * Simulate a hardware HUB by handling all the necessary requests.
2842 *------------------------------------------------------------------------*/
2844 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
2847 struct usb_device_descriptor xhci_devd =
2849 .bLength = sizeof(xhci_devd),
2850 .bDescriptorType = UDESC_DEVICE, /* type */
2851 HSETW(.bcdUSB, 0x0300), /* USB version */
2852 .bDeviceClass = UDCLASS_HUB, /* class */
2853 .bDeviceSubClass = UDSUBCLASS_HUB, /* subclass */
2854 .bDeviceProtocol = UDPROTO_SSHUB, /* protocol */
2855 .bMaxPacketSize = 9, /* max packet size */
2856 HSETW(.idVendor, 0x0000), /* vendor */
2857 HSETW(.idProduct, 0x0000), /* product */
2858 HSETW(.bcdDevice, 0x0100), /* device version */
2862 .bNumConfigurations = 1, /* # of configurations */
2866 struct xhci_bos_desc xhci_bosd = {
2868 .bLength = sizeof(xhci_bosd.bosd),
2869 .bDescriptorType = UDESC_BOS,
2870 HSETW(.wTotalLength, sizeof(xhci_bosd)),
2871 .bNumDeviceCaps = 3,
2874 .bLength = sizeof(xhci_bosd.usb2extd),
2875 .bDescriptorType = 1,
2876 .bDevCapabilityType = 2,
2877 .bmAttributes[0] = 2,
2880 .bLength = sizeof(xhci_bosd.usbdcd),
2881 .bDescriptorType = UDESC_DEVICE_CAPABILITY,
2882 .bDevCapabilityType = 3,
2883 .bmAttributes = 0, /* XXX */
2884 HSETW(.wSpeedsSupported, 0x000C),
2885 .bFunctionalitySupport = 8,
2886 .bU1DevExitLat = 255, /* dummy - not used */
2887 .wU2DevExitLat = { 0x00, 0x08 },
2890 .bLength = sizeof(xhci_bosd.cidd),
2891 .bDescriptorType = 1,
2892 .bDevCapabilityType = 4,
2894 .bContainerID = 0, /* XXX */
2899 struct xhci_config_desc xhci_confd = {
2901 .bLength = sizeof(xhci_confd.confd),
2902 .bDescriptorType = UDESC_CONFIG,
2903 .wTotalLength[0] = sizeof(xhci_confd),
2905 .bConfigurationValue = 1,
2906 .iConfiguration = 0,
2907 .bmAttributes = UC_SELF_POWERED,
2908 .bMaxPower = 0 /* max power */
2911 .bLength = sizeof(xhci_confd.ifcd),
2912 .bDescriptorType = UDESC_INTERFACE,
2914 .bInterfaceClass = UICLASS_HUB,
2915 .bInterfaceSubClass = UISUBCLASS_HUB,
2916 .bInterfaceProtocol = 0,
2919 .bLength = sizeof(xhci_confd.endpd),
2920 .bDescriptorType = UDESC_ENDPOINT,
2921 .bEndpointAddress = UE_DIR_IN | XHCI_INTR_ENDPT,
2922 .bmAttributes = UE_INTERRUPT,
2923 .wMaxPacketSize[0] = 2, /* max 15 ports */
2927 .bLength = sizeof(xhci_confd.endpcd),
2928 .bDescriptorType = UDESC_ENDPOINT_SS_COMP,
2935 struct usb_hub_ss_descriptor xhci_hubd = {
2936 .bLength = sizeof(xhci_hubd),
2937 .bDescriptorType = UDESC_SS_HUB,
2941 xhci_roothub_exec(struct usb_device *udev,
2942 struct usb_device_request *req, const void **pptr, uint16_t *plength)
2944 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2945 const char *str_ptr;
2956 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2959 ptr = (const void *)&sc->sc_hub_desc;
2963 value = UGETW(req->wValue);
2964 index = UGETW(req->wIndex);
2966 DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
2967 "wValue=0x%04x wIndex=0x%04x\n",
2968 req->bmRequestType, req->bRequest,
2969 UGETW(req->wLength), value, index);
2971 #define C(x,y) ((x) | ((y) << 8))
2972 switch (C(req->bRequest, req->bmRequestType)) {
2973 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2974 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2975 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2977 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2978 * for the integrated root hub.
2981 case C(UR_GET_CONFIG, UT_READ_DEVICE):
2983 sc->sc_hub_desc.temp[0] = sc->sc_conf;
2985 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2986 switch (value >> 8) {
2988 if ((value & 0xff) != 0) {
2989 err = USB_ERR_IOERROR;
2992 len = sizeof(xhci_devd);
2993 ptr = (const void *)&xhci_devd;
2997 if ((value & 0xff) != 0) {
2998 err = USB_ERR_IOERROR;
3001 len = sizeof(xhci_bosd);
3002 ptr = (const void *)&xhci_bosd;
3006 if ((value & 0xff) != 0) {
3007 err = USB_ERR_IOERROR;
3010 len = sizeof(xhci_confd);
3011 ptr = (const void *)&xhci_confd;
3015 switch (value & 0xff) {
3016 case 0: /* Language table */
3020 case 1: /* Vendor */
3021 str_ptr = sc->sc_vendor;
3024 case 2: /* Product */
3025 str_ptr = "XHCI root HUB";
3033 len = usb_make_str_desc(
3034 sc->sc_hub_desc.temp,
3035 sizeof(sc->sc_hub_desc.temp),
3040 err = USB_ERR_IOERROR;
3044 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3046 sc->sc_hub_desc.temp[0] = 0;
3048 case C(UR_GET_STATUS, UT_READ_DEVICE):
3050 USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
3052 case C(UR_GET_STATUS, UT_READ_INTERFACE):
3053 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3055 USETW(sc->sc_hub_desc.stat.wStatus, 0);
3057 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3058 if (value >= XHCI_MAX_DEVICES) {
3059 err = USB_ERR_IOERROR;
3063 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
3064 if (value != 0 && value != 1) {
3065 err = USB_ERR_IOERROR;
3068 sc->sc_conf = value;
3070 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
3072 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
3073 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
3074 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3075 err = USB_ERR_IOERROR;
3077 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
3079 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
3082 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3084 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3085 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE\n");
3088 (index > sc->sc_noport)) {
3089 err = USB_ERR_IOERROR;
3092 port = XHCI_PORTSC(index);
3094 v = XREAD4(sc, oper, port);
3095 i = XHCI_PS_PLS_GET(v);
3096 v &= ~XHCI_PS_CLEAR;
3099 case UHF_C_BH_PORT_RESET:
3100 XWRITE4(sc, oper, port, v | XHCI_PS_WRC);
3102 case UHF_C_PORT_CONFIG_ERROR:
3103 XWRITE4(sc, oper, port, v | XHCI_PS_CEC);
3105 case UHF_C_PORT_SUSPEND:
3106 case UHF_C_PORT_LINK_STATE:
3107 XWRITE4(sc, oper, port, v | XHCI_PS_PLC);
3109 case UHF_C_PORT_CONNECTION:
3110 XWRITE4(sc, oper, port, v | XHCI_PS_CSC);
3112 case UHF_C_PORT_ENABLE:
3113 XWRITE4(sc, oper, port, v | XHCI_PS_PEC);
3115 case UHF_C_PORT_OVER_CURRENT:
3116 XWRITE4(sc, oper, port, v | XHCI_PS_OCC);
3118 case UHF_C_PORT_RESET:
3119 XWRITE4(sc, oper, port, v | XHCI_PS_PRC);
3121 case UHF_PORT_ENABLE:
3122 XWRITE4(sc, oper, port, v | XHCI_PS_PED);
3124 case UHF_PORT_POWER:
3125 XWRITE4(sc, oper, port, v & ~XHCI_PS_PP);
3127 case UHF_PORT_INDICATOR:
3128 XWRITE4(sc, oper, port, v & ~XHCI_PS_PIC_SET(3));
3130 case UHF_PORT_SUSPEND:
3134 XWRITE4(sc, oper, port, v |
3135 XHCI_PS_PLS_SET(0xF) | XHCI_PS_LWS);
3138 /* wait 20ms for resume sequence to complete */
3139 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
3142 XWRITE4(sc, oper, port, v |
3143 XHCI_PS_PLS_SET(0) | XHCI_PS_LWS);
3146 err = USB_ERR_IOERROR;
3151 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3152 if ((value & 0xff) != 0) {
3153 err = USB_ERR_IOERROR;
3157 v = XREAD4(sc, capa, XHCI_HCSPARAMS0);
3159 sc->sc_hub_desc.hubd = xhci_hubd;
3161 sc->sc_hub_desc.hubd.bNbrPorts = sc->sc_noport;
3163 if (XHCI_HCS0_PPC(v))
3164 i = UHD_PWR_INDIVIDUAL;
3168 if (XHCI_HCS0_PIND(v))
3171 i |= UHD_OC_INDIVIDUAL;
3173 USETW(sc->sc_hub_desc.hubd.wHubCharacteristics, i);
3175 /* see XHCI section 5.4.9: */
3176 sc->sc_hub_desc.hubd.bPwrOn2PwrGood = 10;
3178 for (j = 1; j <= sc->sc_noport; j++) {
3180 v = XREAD4(sc, oper, XHCI_PORTSC(j));
3181 if (v & XHCI_PS_DR) {
3182 sc->sc_hub_desc.hubd.
3183 DeviceRemovable[j / 8] |= 1U << (j % 8);
3186 len = sc->sc_hub_desc.hubd.bLength;
3189 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3191 memset(sc->sc_hub_desc.temp, 0, 16);
3194 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3195 DPRINTFN(9, "UR_GET_STATUS i=%d\n", index);
3198 (index > sc->sc_noport)) {
3199 err = USB_ERR_IOERROR;
3203 v = XREAD4(sc, oper, XHCI_PORTSC(index));
3205 DPRINTFN(9, "port status=0x%08x\n", v);
3207 i = UPS_PORT_LINK_STATE_SET(XHCI_PS_PLS_GET(v));
3209 switch (XHCI_PS_SPEED_GET(v)) {
3211 i |= UPS_HIGH_SPEED;
3220 i |= UPS_OTHER_SPEED;
3224 if (v & XHCI_PS_CCS)
3225 i |= UPS_CURRENT_CONNECT_STATUS;
3226 if (v & XHCI_PS_PED)
3227 i |= UPS_PORT_ENABLED;
3228 if (v & XHCI_PS_OCA)
3229 i |= UPS_OVERCURRENT_INDICATOR;
3232 if (v & XHCI_PS_PP) {
3234 * The USB 3.0 RH is using the
3235 * USB 2.0's power bit
3237 i |= UPS_PORT_POWER;
3239 USETW(sc->sc_hub_desc.ps.wPortStatus, i);
3242 if (v & XHCI_PS_CSC)
3243 i |= UPS_C_CONNECT_STATUS;
3244 if (v & XHCI_PS_PEC)
3245 i |= UPS_C_PORT_ENABLED;
3246 if (v & XHCI_PS_OCC)
3247 i |= UPS_C_OVERCURRENT_INDICATOR;
3248 if (v & XHCI_PS_WRC)
3249 i |= UPS_C_BH_PORT_RESET;
3250 if (v & XHCI_PS_PRC)
3251 i |= UPS_C_PORT_RESET;
3252 if (v & XHCI_PS_PLC)
3253 i |= UPS_C_PORT_LINK_STATE;
3254 if (v & XHCI_PS_CEC)
3255 i |= UPS_C_PORT_CONFIG_ERROR;
3257 USETW(sc->sc_hub_desc.ps.wPortChange, i);
3258 len = sizeof(sc->sc_hub_desc.ps);
3261 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3262 err = USB_ERR_IOERROR;
3265 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3268 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3274 (index > sc->sc_noport)) {
3275 err = USB_ERR_IOERROR;
3279 port = XHCI_PORTSC(index);
3280 v = XREAD4(sc, oper, port) & ~XHCI_PS_CLEAR;
3283 case UHF_PORT_U1_TIMEOUT:
3284 if (XHCI_PS_SPEED_GET(v) != 4) {
3285 err = USB_ERR_IOERROR;
3288 port = XHCI_PORTPMSC(index);
3289 v = XREAD4(sc, oper, port);
3290 v &= ~XHCI_PM3_U1TO_SET(0xFF);
3291 v |= XHCI_PM3_U1TO_SET(i);
3292 XWRITE4(sc, oper, port, v);
3294 case UHF_PORT_U2_TIMEOUT:
3295 if (XHCI_PS_SPEED_GET(v) != 4) {
3296 err = USB_ERR_IOERROR;
3299 port = XHCI_PORTPMSC(index);
3300 v = XREAD4(sc, oper, port);
3301 v &= ~XHCI_PM3_U2TO_SET(0xFF);
3302 v |= XHCI_PM3_U2TO_SET(i);
3303 XWRITE4(sc, oper, port, v);
3305 case UHF_BH_PORT_RESET:
3306 XWRITE4(sc, oper, port, v | XHCI_PS_WPR);
3308 case UHF_PORT_LINK_STATE:
3309 XWRITE4(sc, oper, port, v |
3310 XHCI_PS_PLS_SET(i) | XHCI_PS_LWS);
3311 /* 4ms settle time */
3312 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
3314 case UHF_PORT_ENABLE:
3315 DPRINTFN(3, "set port enable %d\n", index);
3317 case UHF_PORT_SUSPEND:
3318 DPRINTFN(6, "suspend port %u (LPM=%u)\n", index, i);
3319 j = XHCI_PS_SPEED_GET(v);
3320 if ((j < 1) || (j > 3)) {
3321 /* non-supported speed */
3322 err = USB_ERR_IOERROR;
3325 XWRITE4(sc, oper, port, v |
3326 XHCI_PS_PLS_SET(i ? 2 /* LPM */ : 3) | XHCI_PS_LWS);
3328 case UHF_PORT_RESET:
3329 DPRINTFN(6, "reset port %d\n", index);
3330 XWRITE4(sc, oper, port, v | XHCI_PS_PR);
3332 case UHF_PORT_POWER:
3333 DPRINTFN(3, "set port power %d\n", index);
3334 XWRITE4(sc, oper, port, v | XHCI_PS_PP);
3337 DPRINTFN(3, "set port test %d\n", index);
3339 case UHF_PORT_INDICATOR:
3340 DPRINTFN(3, "set port indicator %d\n", index);
3342 v &= ~XHCI_PS_PIC_SET(3);
3343 v |= XHCI_PS_PIC_SET(1);
3345 XWRITE4(sc, oper, port, v);
3348 err = USB_ERR_IOERROR;
3353 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
3354 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
3355 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
3356 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
3359 err = USB_ERR_IOERROR;
3369 xhci_xfer_setup(struct usb_setup_params *parm)
3371 struct usb_page_search page_info;
3372 struct usb_page_cache *pc;
3373 struct xhci_softc *sc;
3374 struct usb_xfer *xfer;
3379 sc = XHCI_BUS2SC(parm->udev->bus);
3380 xfer = parm->curr_xfer;
3383 * The proof for the "ntd" formula is illustrated like this:
3385 * +------------------------------------+
3389 * | | xxx | x | frm 0 |
3391 * | | xxx | xx | frm 1 |
3394 * +------------------------------------+
3396 * "xxx" means a completely full USB transfer descriptor
3398 * "x" and "xx" means a short USB packet
3400 * For the remainder of an USB transfer modulo
3401 * "max_data_length" we need two USB transfer descriptors.
3402 * One to transfer the remaining data and one to finalise with
3403 * a zero length packet in case the "force_short_xfer" flag is
3404 * set. We only need two USB transfer descriptors in the case
3405 * where the transfer length of the first one is a factor of
3406 * "max_frame_size". The rest of the needed USB transfer
3407 * descriptors is given by the buffer size divided by the
3408 * maximum data payload.
3410 parm->hc_max_packet_size = 0x400;
3411 parm->hc_max_packet_count = 16 * 3;
3412 parm->hc_max_frame_size = XHCI_TD_PAYLOAD_MAX;
3414 xfer->flags_int.bdma_enable = 1;
3416 usbd_transfer_setup_sub(parm);
3418 if (xfer->flags_int.isochronous_xfr) {
3419 ntd = ((1 * xfer->nframes)
3420 + (xfer->max_data_length / xfer->max_hc_frame_size));
3421 } else if (xfer->flags_int.control_xfr) {
3422 ntd = ((2 * xfer->nframes) + 1 /* STATUS */
3423 + (xfer->max_data_length / xfer->max_hc_frame_size));
3425 ntd = ((2 * xfer->nframes)
3426 + (xfer->max_data_length / xfer->max_hc_frame_size));
3435 * Allocate queue heads and transfer descriptors
3439 if (usbd_transfer_setup_sub_malloc(
3440 parm, &pc, sizeof(struct xhci_td),
3441 XHCI_TD_ALIGN, ntd)) {
3442 parm->err = USB_ERR_NOMEM;
3446 for (n = 0; n != ntd; n++) {
3449 usbd_get_page(pc + n, 0, &page_info);
3451 td = page_info.buffer;
3454 td->td_self = page_info.physaddr;
3455 td->obj_next = last_obj;
3456 td->page_cache = pc + n;
3460 usb_pc_cpu_flush(pc + n);
3463 xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
3465 if (!xfer->flags_int.curr_dma_set) {
3466 xfer->flags_int.curr_dma_set = 1;
3472 xhci_configure_reset_endpoint(struct usb_xfer *xfer)
3474 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
3475 struct usb_page_search buf_inp;
3476 struct usb_device *udev;
3477 struct xhci_endpoint_ext *pepext;
3478 struct usb_endpoint_descriptor *edesc;
3479 struct usb_page_cache *pcinp;
3484 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
3485 xfer->endpoint->edesc);
3487 udev = xfer->xroot->udev;
3488 index = udev->controller_slot_id;
3490 pcinp = &sc->sc_hw.devs[index].input_pc;
3492 usbd_get_page(pcinp, 0, &buf_inp);
3494 edesc = xfer->endpoint->edesc;
3496 epno = edesc->bEndpointAddress;
3498 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL)
3501 epno = XHCI_EPNO2EPID(epno);
3504 return (USB_ERR_NO_PIPE); /* invalid */
3508 /* configure endpoint */
3510 err = xhci_configure_endpoint_by_xfer(xfer);
3513 XHCI_CMD_UNLOCK(sc);
3518 * Get the endpoint into the stopped state according to the
3519 * endpoint context state diagram in the XHCI specification:
3522 err = xhci_cmd_stop_ep(sc, 0, epno, index);
3525 DPRINTF("Could not stop endpoint %u\n", epno);
3527 err = xhci_cmd_reset_ep(sc, 0, epno, index);
3530 DPRINTF("Could not reset endpoint %u\n", epno);
3532 err = xhci_cmd_set_tr_dequeue_ptr(sc, pepext->physaddr |
3533 XHCI_EPCTX_2_DCS_SET(1), 0, epno, index);
3536 DPRINTF("Could not set dequeue ptr for endpoint %u\n", epno);
3539 * Get the endpoint into the running state according to the
3540 * endpoint context state diagram in the XHCI specification:
3543 xhci_configure_mask(udev, 1U << epno, 0);
3545 err = xhci_cmd_evaluate_ctx(sc, buf_inp.physaddr, index);
3548 DPRINTF("Could not configure endpoint %u\n", epno);
3550 err = xhci_cmd_configure_ep(sc, buf_inp.physaddr, 0, index);
3553 DPRINTF("Could not configure endpoint %u\n", epno);
3555 XHCI_CMD_UNLOCK(sc);
3561 xhci_xfer_unsetup(struct usb_xfer *xfer)
3567 xhci_start_dma_delay(struct usb_xfer *xfer)
3569 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
3571 /* put transfer on interrupt queue (again) */
3572 usbd_transfer_enqueue(&sc->sc_bus.intr_q, xfer);
3574 (void)usb_proc_msignal(&sc->sc_config_proc,
3575 &sc->sc_config_msg[0], &sc->sc_config_msg[1]);
3579 xhci_configure_msg(struct usb_proc_msg *pm)
3581 struct xhci_softc *sc;
3582 struct xhci_endpoint_ext *pepext;
3583 struct usb_xfer *xfer;
3585 sc = XHCI_BUS2SC(((struct usb_bus_msg *)pm)->bus);
3588 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3590 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
3591 xfer->endpoint->edesc);
3593 if ((pepext->trb_halted != 0) ||
3594 (pepext->trb_running == 0)) {
3598 /* clear halted and running */
3599 pepext->trb_halted = 0;
3600 pepext->trb_running = 0;
3602 /* nuke remaining buffered transfers */
3604 for (i = 0; i != (XHCI_MAX_TRANSFERS - 1); i++) {
3606 * NOTE: We need to use the timeout
3607 * error code here else existing
3608 * isochronous clients can get
3611 if (pepext->xfer[i] != NULL) {
3612 xhci_device_done(pepext->xfer[i],
3618 * NOTE: The USB transfer cannot vanish in
3622 USB_BUS_UNLOCK(&sc->sc_bus);
3624 xhci_configure_reset_endpoint(xfer);
3626 USB_BUS_LOCK(&sc->sc_bus);
3628 /* check if halted is still cleared */
3629 if (pepext->trb_halted == 0) {
3630 pepext->trb_running = 1;
3631 pepext->trb_index = 0;
3636 if (xfer->flags_int.did_dma_delay) {
3638 /* remove transfer from interrupt queue (again) */
3639 usbd_transfer_dequeue(xfer);
3641 /* we are finally done */
3642 usb_dma_delay_done_cb(xfer);
3644 /* queue changed - restart */
3649 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3651 /* try to insert xfer on HW queue */
3652 xhci_transfer_insert(xfer);
3654 /* try to multi buffer */
3655 xhci_device_generic_multi_enter(xfer->endpoint, NULL);
3660 xhci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
3661 struct usb_endpoint *ep)
3663 struct xhci_endpoint_ext *pepext;
3665 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d\n",
3666 ep, udev->address, edesc->bEndpointAddress, udev->flags.usb_mode);
3668 if (udev->flags.usb_mode != USB_MODE_HOST) {
3672 if (udev->parent_hub == NULL) {
3673 /* root HUB has special endpoint handling */
3677 ep->methods = &xhci_device_generic_methods;
3679 pepext = xhci_get_endpoint_ext(udev, edesc);
3681 USB_BUS_LOCK(udev->bus);
3682 pepext->trb_halted = 1;
3683 pepext->trb_running = 0;
3684 USB_BUS_UNLOCK(udev->bus);
3688 xhci_ep_uninit(struct usb_device *udev, struct usb_endpoint *ep)
3694 xhci_ep_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
3696 struct xhci_endpoint_ext *pepext;
3700 if (udev->flags.usb_mode != USB_MODE_HOST) {
3704 if (udev->parent_hub == NULL) {
3705 /* root HUB has special endpoint handling */
3709 pepext = xhci_get_endpoint_ext(udev, ep->edesc);
3711 USB_BUS_LOCK(udev->bus);
3712 pepext->trb_halted = 1;
3713 pepext->trb_running = 0;
3714 USB_BUS_UNLOCK(udev->bus);
3718 xhci_device_init(struct usb_device *udev)
3720 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3724 /* no init for root HUB */
3725 if (udev->parent_hub == NULL)
3730 /* set invalid default */
3732 udev->controller_slot_id = sc->sc_noslot + 1;
3734 /* try to get a new slot ID from the XHCI */
3736 err = xhci_cmd_enable_slot(sc, &temp);
3739 XHCI_CMD_UNLOCK(sc);
3743 if (temp > sc->sc_noslot) {
3744 XHCI_CMD_UNLOCK(sc);
3745 return (USB_ERR_BAD_ADDRESS);
3748 if (sc->sc_hw.devs[temp].state != XHCI_ST_DISABLED) {
3749 DPRINTF("slot %u already allocated.\n", temp);
3750 XHCI_CMD_UNLOCK(sc);
3751 return (USB_ERR_BAD_ADDRESS);
3754 /* store slot ID for later reference */
3756 udev->controller_slot_id = temp;
3758 /* reset data structure */
3760 memset(&sc->sc_hw.devs[temp], 0, sizeof(sc->sc_hw.devs[0]));
3762 /* set mark slot allocated */
3764 sc->sc_hw.devs[temp].state = XHCI_ST_ENABLED;
3766 err = xhci_alloc_device_ext(udev);
3768 XHCI_CMD_UNLOCK(sc);
3770 /* get device into default state */
3773 err = xhci_set_address(udev, NULL, 0);
3779 xhci_device_uninit(struct usb_device *udev)
3781 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3784 /* no init for root HUB */
3785 if (udev->parent_hub == NULL)
3790 index = udev->controller_slot_id;
3792 if (index <= sc->sc_noslot) {
3793 xhci_cmd_disable_slot(sc, index);
3794 sc->sc_hw.devs[index].state = XHCI_ST_DISABLED;
3796 /* free device extension */
3797 xhci_free_device_ext(udev);
3800 XHCI_CMD_UNLOCK(sc);
3804 xhci_get_dma_delay(struct usb_device *udev, uint32_t *pus)
3807 * Wait until the hardware has finished any possible use of
3808 * the transfer descriptor(s)
3810 *pus = 2048; /* microseconds */
3814 xhci_device_resume(struct usb_device *udev)
3816 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3822 /* check for root HUB */
3823 if (udev->parent_hub == NULL)
3826 index = udev->controller_slot_id;
3830 /* blindly resume all endpoints */
3832 USB_BUS_LOCK(udev->bus);
3834 for (n = 1; n != XHCI_MAX_ENDPOINTS; n++)
3835 XWRITE4(sc, door, XHCI_DOORBELL(index), n | XHCI_DB_SID_SET(0));
3837 USB_BUS_UNLOCK(udev->bus);
3839 XHCI_CMD_UNLOCK(sc);
3843 xhci_device_suspend(struct usb_device *udev)
3845 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3852 /* check for root HUB */
3853 if (udev->parent_hub == NULL)
3856 index = udev->controller_slot_id;
3860 /* blindly suspend all endpoints */
3862 for (n = 1; n != XHCI_MAX_ENDPOINTS; n++) {
3863 err = xhci_cmd_stop_ep(sc, 1, n, index);
3865 DPRINTF("Failed to suspend endpoint "
3866 "%u on slot %u (ignored).\n", n, index);
3870 XHCI_CMD_UNLOCK(sc);
3874 xhci_set_hw_power(struct usb_bus *bus)
3880 xhci_device_state_change(struct usb_device *udev)
3882 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3883 struct usb_page_search buf_inp;
3887 /* check for root HUB */
3888 if (udev->parent_hub == NULL)
3891 index = udev->controller_slot_id;
3895 if (usb_get_device_state(udev) == USB_STATE_CONFIGURED) {
3896 err = uhub_query_info(udev, &sc->sc_hw.devs[index].nports,
3897 &sc->sc_hw.devs[index].tt);
3899 sc->sc_hw.devs[index].nports = 0;
3904 switch (usb_get_device_state(udev)) {
3905 case USB_STATE_POWERED:
3906 if (sc->sc_hw.devs[index].state == XHCI_ST_DEFAULT)
3909 sc->sc_hw.devs[index].state = XHCI_ST_DEFAULT;
3911 err = xhci_cmd_reset_dev(sc, index);
3914 DPRINTF("Device reset failed "
3915 "for slot %u.\n", index);
3919 case USB_STATE_ADDRESSED:
3920 if (sc->sc_hw.devs[index].state == XHCI_ST_ADDRESSED)
3923 sc->sc_hw.devs[index].state = XHCI_ST_ADDRESSED;
3925 err = xhci_cmd_configure_ep(sc, 0, 1, index);
3928 DPRINTF("Failed to deconfigure "
3929 "slot %u.\n", index);
3933 case USB_STATE_CONFIGURED:
3934 if (sc->sc_hw.devs[index].state == XHCI_ST_CONFIGURED)
3937 sc->sc_hw.devs[index].state = XHCI_ST_CONFIGURED;
3939 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
3941 xhci_configure_mask(udev, 1, 0);
3943 err = xhci_configure_device(udev);
3945 DPRINTF("Could not configure device "
3946 "at slot %u.\n", index);
3949 err = xhci_cmd_evaluate_ctx(sc, buf_inp.physaddr, index);
3951 DPRINTF("Could not evaluate device "
3952 "context at slot %u.\n", index);
3959 XHCI_CMD_UNLOCK(sc);
3962 struct usb_bus_methods xhci_bus_methods = {
3963 .endpoint_init = xhci_ep_init,
3964 .endpoint_uninit = xhci_ep_uninit,
3965 .xfer_setup = xhci_xfer_setup,
3966 .xfer_unsetup = xhci_xfer_unsetup,
3967 .get_dma_delay = xhci_get_dma_delay,
3968 .device_init = xhci_device_init,
3969 .device_uninit = xhci_device_uninit,
3970 .device_resume = xhci_device_resume,
3971 .device_suspend = xhci_device_suspend,
3972 .set_hw_power = xhci_set_hw_power,
3973 .roothub_exec = xhci_roothub_exec,
3974 .xfer_poll = xhci_do_poll,
3975 .start_dma_delay = xhci_start_dma_delay,
3976 .set_address = xhci_set_address,
3977 .clear_stall = xhci_ep_clear_stall,
3978 .device_state_change = xhci_device_state_change,
3979 .set_hw_power_sleep = xhci_set_hw_power_sleep,