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))))
89 static int xhcipolling;
91 static SYSCTL_NODE(_hw_usb, OID_AUTO, xhci, CTLFLAG_RW, 0, "USB XHCI");
92 SYSCTL_INT(_hw_usb_xhci, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN,
93 &xhcidebug, 0, "Debug level");
94 TUNABLE_INT("hw.usb.xhci.debug", &xhcidebug);
95 SYSCTL_INT(_hw_usb_xhci, OID_AUTO, xhci_port_route, CTLFLAG_RW | CTLFLAG_TUN,
96 &xhciroute, 0, "Routing bitmap for switching EHCI ports to XHCI controller");
97 TUNABLE_INT("hw.usb.xhci.xhci_port_route", &xhciroute);
98 SYSCTL_INT(_hw_usb_xhci, OID_AUTO, use_polling, CTLFLAG_RW | CTLFLAG_TUN,
99 &xhcipolling, 0, "Set to enable software interrupt polling for XHCI controller");
100 TUNABLE_INT("hw.usb.xhci.use_polling", &xhcipolling);
105 #define XHCI_INTR_ENDPT 1
107 struct xhci_std_temp {
108 struct xhci_softc *sc;
109 struct usb_page_cache *pc;
111 struct xhci_td *td_next;
114 uint32_t max_packet_size;
126 uint8_t do_isoc_sync;
129 static void xhci_do_poll(struct usb_bus *);
130 static void xhci_device_done(struct usb_xfer *, usb_error_t);
131 static void xhci_root_intr(struct xhci_softc *);
132 static void xhci_free_device_ext(struct usb_device *);
133 static struct xhci_endpoint_ext *xhci_get_endpoint_ext(struct usb_device *,
134 struct usb_endpoint_descriptor *);
135 static usb_proc_callback_t xhci_configure_msg;
136 static usb_error_t xhci_configure_device(struct usb_device *);
137 static usb_error_t xhci_configure_endpoint(struct usb_device *,
138 struct usb_endpoint_descriptor *, uint64_t, uint16_t,
139 uint8_t, uint8_t, uint8_t, uint16_t, uint16_t);
140 static usb_error_t xhci_configure_mask(struct usb_device *,
142 static usb_error_t xhci_cmd_evaluate_ctx(struct xhci_softc *,
144 static void xhci_endpoint_doorbell(struct usb_xfer *);
145 static void xhci_ctx_set_le32(struct xhci_softc *sc, volatile uint32_t *ptr, uint32_t val);
146 static uint32_t xhci_ctx_get_le32(struct xhci_softc *sc, volatile uint32_t *ptr);
147 static void xhci_ctx_set_le64(struct xhci_softc *sc, volatile uint64_t *ptr, uint64_t val);
149 static uint64_t xhci_ctx_get_le64(struct xhci_softc *sc, volatile uint64_t *ptr);
152 extern struct usb_bus_methods xhci_bus_methods;
156 xhci_dump_trb(struct xhci_trb *trb)
158 DPRINTFN(5, "trb = %p\n", trb);
159 DPRINTFN(5, "qwTrb0 = 0x%016llx\n", (long long)le64toh(trb->qwTrb0));
160 DPRINTFN(5, "dwTrb2 = 0x%08x\n", le32toh(trb->dwTrb2));
161 DPRINTFN(5, "dwTrb3 = 0x%08x\n", le32toh(trb->dwTrb3));
165 xhci_dump_endpoint(struct xhci_softc *sc, struct xhci_endp_ctx *pep)
167 DPRINTFN(5, "pep = %p\n", pep);
168 DPRINTFN(5, "dwEpCtx0=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx0));
169 DPRINTFN(5, "dwEpCtx1=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx1));
170 DPRINTFN(5, "qwEpCtx2=0x%016llx\n", (long long)xhci_ctx_get_le64(sc, &pep->qwEpCtx2));
171 DPRINTFN(5, "dwEpCtx4=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx4));
172 DPRINTFN(5, "dwEpCtx5=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx5));
173 DPRINTFN(5, "dwEpCtx6=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx6));
174 DPRINTFN(5, "dwEpCtx7=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx7));
178 xhci_dump_device(struct xhci_softc *sc, struct xhci_slot_ctx *psl)
180 DPRINTFN(5, "psl = %p\n", psl);
181 DPRINTFN(5, "dwSctx0=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx0));
182 DPRINTFN(5, "dwSctx1=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx1));
183 DPRINTFN(5, "dwSctx2=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx2));
184 DPRINTFN(5, "dwSctx3=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx3));
189 xhci_use_polling(void)
192 return (xhcipolling != 0);
199 xhci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
201 struct xhci_softc *sc = XHCI_BUS2SC(bus);
204 cb(bus, &sc->sc_hw.root_pc, &sc->sc_hw.root_pg,
205 sizeof(struct xhci_hw_root), XHCI_PAGE_SIZE);
207 cb(bus, &sc->sc_hw.ctx_pc, &sc->sc_hw.ctx_pg,
208 sizeof(struct xhci_dev_ctx_addr), XHCI_PAGE_SIZE);
210 for (i = 0; i != XHCI_MAX_SCRATCHPADS; i++) {
211 cb(bus, &sc->sc_hw.scratch_pc[i], &sc->sc_hw.scratch_pg[i],
212 XHCI_PAGE_SIZE, XHCI_PAGE_SIZE);
217 xhci_ctx_set_le32(struct xhci_softc *sc, volatile uint32_t *ptr, uint32_t val)
219 if (sc->sc_ctx_is_64_byte) {
221 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
222 /* all contexts are initially 32-bytes */
223 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
224 ptr = (volatile uint32_t *)(((volatile uint8_t *)ptr) + offset);
230 xhci_ctx_get_le32(struct xhci_softc *sc, volatile uint32_t *ptr)
232 if (sc->sc_ctx_is_64_byte) {
234 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
235 /* all contexts are initially 32-bytes */
236 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
237 ptr = (volatile uint32_t *)(((volatile uint8_t *)ptr) + offset);
239 return (le32toh(*ptr));
243 xhci_ctx_set_le64(struct xhci_softc *sc, volatile uint64_t *ptr, uint64_t val)
245 if (sc->sc_ctx_is_64_byte) {
247 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
248 /* all contexts are initially 32-bytes */
249 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
250 ptr = (volatile uint64_t *)(((volatile uint8_t *)ptr) + offset);
257 xhci_ctx_get_le64(struct xhci_softc *sc, volatile uint64_t *ptr)
259 if (sc->sc_ctx_is_64_byte) {
261 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
262 /* all contexts are initially 32-bytes */
263 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
264 ptr = (volatile uint64_t *)(((volatile uint8_t *)ptr) + offset);
266 return (le64toh(*ptr));
271 xhci_reset_command_queue_locked(struct xhci_softc *sc)
273 struct usb_page_search buf_res;
274 struct xhci_hw_root *phwr;
280 temp = XREAD4(sc, oper, XHCI_CRCR_LO);
281 if (temp & XHCI_CRCR_LO_CRR) {
282 DPRINTF("Command ring running\n");
283 temp &= ~(XHCI_CRCR_LO_CS | XHCI_CRCR_LO_CA);
286 * Try to abort the last command as per section
287 * 4.6.1.2 "Aborting a Command" of the XHCI
291 /* stop and cancel */
292 XWRITE4(sc, oper, XHCI_CRCR_LO, temp | XHCI_CRCR_LO_CS);
293 XWRITE4(sc, oper, XHCI_CRCR_HI, 0);
295 XWRITE4(sc, oper, XHCI_CRCR_LO, temp | XHCI_CRCR_LO_CA);
296 XWRITE4(sc, oper, XHCI_CRCR_HI, 0);
299 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 4);
301 /* check if command ring is still running */
302 temp = XREAD4(sc, oper, XHCI_CRCR_LO);
303 if (temp & XHCI_CRCR_LO_CRR) {
304 DPRINTF("Comand ring still running\n");
305 return (USB_ERR_IOERROR);
309 /* reset command ring */
310 sc->sc_command_ccs = 1;
311 sc->sc_command_idx = 0;
313 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
315 /* set up command ring control base address */
316 addr = buf_res.physaddr;
317 phwr = buf_res.buffer;
318 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_commands[0];
320 DPRINTF("CRCR=0x%016llx\n", (unsigned long long)addr);
322 memset(phwr->hwr_commands, 0, sizeof(phwr->hwr_commands));
323 phwr->hwr_commands[XHCI_MAX_COMMANDS - 1].qwTrb0 = htole64(addr);
325 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
327 XWRITE4(sc, oper, XHCI_CRCR_LO, ((uint32_t)addr) | XHCI_CRCR_LO_RCS);
328 XWRITE4(sc, oper, XHCI_CRCR_HI, (uint32_t)(addr >> 32));
334 xhci_start_controller(struct xhci_softc *sc)
336 struct usb_page_search buf_res;
337 struct xhci_hw_root *phwr;
338 struct xhci_dev_ctx_addr *pdctxa;
345 sc->sc_event_ccs = 1;
346 sc->sc_event_idx = 0;
347 sc->sc_command_ccs = 1;
348 sc->sc_command_idx = 0;
350 /* Reset controller */
351 XWRITE4(sc, oper, XHCI_USBCMD, XHCI_CMD_HCRST);
353 for (i = 0; i != 100; i++) {
354 usb_pause_mtx(NULL, hz / 100);
355 temp = (XREAD4(sc, oper, XHCI_USBCMD) & XHCI_CMD_HCRST) |
356 (XREAD4(sc, oper, XHCI_USBSTS) & XHCI_STS_CNR);
362 device_printf(sc->sc_bus.parent, "Controller "
364 return (USB_ERR_IOERROR);
367 if (!(XREAD4(sc, oper, XHCI_PAGESIZE) & XHCI_PAGESIZE_4K)) {
368 device_printf(sc->sc_bus.parent, "Controller does "
369 "not support 4K page size.\n");
370 return (USB_ERR_IOERROR);
373 temp = XREAD4(sc, capa, XHCI_HCSPARAMS1);
375 i = XHCI_HCS1_N_PORTS(temp);
378 device_printf(sc->sc_bus.parent, "Invalid number "
379 "of ports: %u\n", i);
380 return (USB_ERR_IOERROR);
384 sc->sc_noslot = XHCI_HCS1_DEVSLOT_MAX(temp);
386 if (sc->sc_noslot > XHCI_MAX_DEVICES)
387 sc->sc_noslot = XHCI_MAX_DEVICES;
389 /* set up number of device slots */
391 DPRINTF("CONFIG=0x%08x -> 0x%08x\n",
392 XREAD4(sc, oper, XHCI_CONFIG), sc->sc_noslot);
394 XWRITE4(sc, oper, XHCI_CONFIG, sc->sc_noslot);
396 DPRINTF("Max slots: %u\n", sc->sc_noslot);
398 temp = XREAD4(sc, capa, XHCI_HCSPARAMS2);
400 sc->sc_noscratch = XHCI_HCS2_SPB_MAX(temp);
402 if (sc->sc_noscratch > XHCI_MAX_SCRATCHPADS) {
403 device_printf(sc->sc_bus.parent, "XHCI request "
404 "too many scratchpads\n");
405 return (USB_ERR_NOMEM);
408 DPRINTF("Max scratch: %u\n", sc->sc_noscratch);
410 temp = XREAD4(sc, capa, XHCI_HCSPARAMS3);
412 sc->sc_exit_lat_max = XHCI_HCS3_U1_DEL(temp) +
413 XHCI_HCS3_U2_DEL(temp) + 250 /* us */;
415 temp = XREAD4(sc, oper, XHCI_USBSTS);
417 /* clear interrupts */
418 XWRITE4(sc, oper, XHCI_USBSTS, temp);
419 /* disable all device notifications */
420 XWRITE4(sc, oper, XHCI_DNCTRL, 0);
422 /* set up device context base address */
423 usbd_get_page(&sc->sc_hw.ctx_pc, 0, &buf_res);
424 pdctxa = buf_res.buffer;
425 memset(pdctxa, 0, sizeof(*pdctxa));
427 addr = buf_res.physaddr;
428 addr += (uintptr_t)&((struct xhci_dev_ctx_addr *)0)->qwSpBufPtr[0];
430 /* slot 0 points to the table of scratchpad pointers */
431 pdctxa->qwBaaDevCtxAddr[0] = htole64(addr);
433 for (i = 0; i != sc->sc_noscratch; i++) {
434 struct usb_page_search buf_scp;
435 usbd_get_page(&sc->sc_hw.scratch_pc[i], 0, &buf_scp);
436 pdctxa->qwSpBufPtr[i] = htole64((uint64_t)buf_scp.physaddr);
439 addr = buf_res.physaddr;
441 XWRITE4(sc, oper, XHCI_DCBAAP_LO, (uint32_t)addr);
442 XWRITE4(sc, oper, XHCI_DCBAAP_HI, (uint32_t)(addr >> 32));
443 XWRITE4(sc, oper, XHCI_DCBAAP_LO, (uint32_t)addr);
444 XWRITE4(sc, oper, XHCI_DCBAAP_HI, (uint32_t)(addr >> 32));
446 /* Setup event table size */
448 temp = XREAD4(sc, capa, XHCI_HCSPARAMS2);
450 DPRINTF("HCS2=0x%08x\n", temp);
452 temp = XHCI_HCS2_ERST_MAX(temp);
454 if (temp > XHCI_MAX_RSEG)
455 temp = XHCI_MAX_RSEG;
457 sc->sc_erst_max = temp;
459 DPRINTF("ERSTSZ=0x%08x -> 0x%08x\n",
460 XREAD4(sc, runt, XHCI_ERSTSZ(0)), temp);
462 XWRITE4(sc, runt, XHCI_ERSTSZ(0), XHCI_ERSTS_SET(temp));
464 /* Check if we should use the default IMOD value */
465 if (sc->sc_imod_default == 0)
466 sc->sc_imod_default = XHCI_IMOD_DEFAULT;
468 /* Setup interrupt rate */
469 XWRITE4(sc, runt, XHCI_IMOD(0), sc->sc_imod_default);
471 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
473 phwr = buf_res.buffer;
474 addr = buf_res.physaddr;
475 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_events[0];
477 /* reset hardware root structure */
478 memset(phwr, 0, sizeof(*phwr));
480 phwr->hwr_ring_seg[0].qwEvrsTablePtr = htole64(addr);
481 phwr->hwr_ring_seg[0].dwEvrsTableSize = htole32(XHCI_MAX_EVENTS);
483 DPRINTF("ERDP(0)=0x%016llx\n", (unsigned long long)addr);
485 XWRITE4(sc, runt, XHCI_ERDP_LO(0), (uint32_t)addr);
486 XWRITE4(sc, runt, XHCI_ERDP_HI(0), (uint32_t)(addr >> 32));
488 addr = buf_res.physaddr;
490 DPRINTF("ERSTBA(0)=0x%016llx\n", (unsigned long long)addr);
492 XWRITE4(sc, runt, XHCI_ERSTBA_LO(0), (uint32_t)addr);
493 XWRITE4(sc, runt, XHCI_ERSTBA_HI(0), (uint32_t)(addr >> 32));
495 /* Setup interrupter registers */
497 temp = XREAD4(sc, runt, XHCI_IMAN(0));
498 temp |= XHCI_IMAN_INTR_ENA;
499 XWRITE4(sc, runt, XHCI_IMAN(0), temp);
501 /* set up command ring control base address */
502 addr = buf_res.physaddr;
503 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_commands[0];
505 DPRINTF("CRCR=0x%016llx\n", (unsigned long long)addr);
507 XWRITE4(sc, oper, XHCI_CRCR_LO, ((uint32_t)addr) | XHCI_CRCR_LO_RCS);
508 XWRITE4(sc, oper, XHCI_CRCR_HI, (uint32_t)(addr >> 32));
510 phwr->hwr_commands[XHCI_MAX_COMMANDS - 1].qwTrb0 = htole64(addr);
512 usb_bus_mem_flush_all(&sc->sc_bus, &xhci_iterate_hw_softc);
515 XWRITE4(sc, oper, XHCI_USBCMD, XHCI_CMD_RS |
516 XHCI_CMD_INTE | XHCI_CMD_HSEE);
518 for (i = 0; i != 100; i++) {
519 usb_pause_mtx(NULL, hz / 100);
520 temp = XREAD4(sc, oper, XHCI_USBSTS) & XHCI_STS_HCH;
525 XWRITE4(sc, oper, XHCI_USBCMD, 0);
526 device_printf(sc->sc_bus.parent, "Run timeout.\n");
527 return (USB_ERR_IOERROR);
530 /* catch any lost interrupts */
531 xhci_do_poll(&sc->sc_bus);
533 if (sc->sc_port_route != NULL) {
534 /* Route all ports to the XHCI by default */
535 sc->sc_port_route(sc->sc_bus.parent,
536 ~xhciroute, xhciroute);
542 xhci_halt_controller(struct xhci_softc *sc)
550 sc->sc_oper_off = XREAD1(sc, capa, XHCI_CAPLENGTH);
551 sc->sc_runt_off = XREAD4(sc, capa, XHCI_RTSOFF) & ~0xF;
552 sc->sc_door_off = XREAD4(sc, capa, XHCI_DBOFF) & ~0x3;
554 /* Halt controller */
555 XWRITE4(sc, oper, XHCI_USBCMD, 0);
557 for (i = 0; i != 100; i++) {
558 usb_pause_mtx(NULL, hz / 100);
559 temp = XREAD4(sc, oper, XHCI_USBSTS) & XHCI_STS_HCH;
565 device_printf(sc->sc_bus.parent, "Controller halt timeout.\n");
566 return (USB_ERR_IOERROR);
572 xhci_init(struct xhci_softc *sc, device_t self)
578 /* initialize some bus fields */
579 sc->sc_bus.parent = self;
581 /* set the bus revision */
582 sc->sc_bus.usbrev = USB_REV_3_0;
584 /* set up the bus struct */
585 sc->sc_bus.methods = &xhci_bus_methods;
587 /* set up devices array */
588 sc->sc_bus.devices = sc->sc_devices;
589 sc->sc_bus.devices_max = XHCI_MAX_DEVICES;
591 /* set default cycle state in case of early interrupts */
592 sc->sc_event_ccs = 1;
593 sc->sc_command_ccs = 1;
595 /* set up bus space offsets */
597 sc->sc_oper_off = XREAD1(sc, capa, XHCI_CAPLENGTH);
598 sc->sc_runt_off = XREAD4(sc, capa, XHCI_RTSOFF) & ~0x1F;
599 sc->sc_door_off = XREAD4(sc, capa, XHCI_DBOFF) & ~0x3;
601 DPRINTF("CAPLENGTH=0x%x\n", sc->sc_oper_off);
602 DPRINTF("RUNTIMEOFFSET=0x%x\n", sc->sc_runt_off);
603 DPRINTF("DOOROFFSET=0x%x\n", sc->sc_door_off);
605 DPRINTF("xHCI version = 0x%04x\n", XREAD2(sc, capa, XHCI_HCIVERSION));
607 temp = XREAD4(sc, capa, XHCI_HCSPARAMS0);
609 DPRINTF("HCS0 = 0x%08x\n", temp);
611 /* set up context size */
612 if (XHCI_HCS0_CSZ(temp)) {
613 sc->sc_ctx_is_64_byte = 1;
615 sc->sc_ctx_is_64_byte = 0;
619 sc->sc_bus.dma_bits = XHCI_HCS0_AC64(temp) ? 64 : 32;
621 device_printf(self, "%d bytes context size, %d-bit DMA\n",
622 sc->sc_ctx_is_64_byte ? 64 : 32, (int)sc->sc_bus.dma_bits);
624 /* get all DMA memory */
625 if (usb_bus_mem_alloc_all(&sc->sc_bus,
626 USB_GET_DMA_TAG(self), &xhci_iterate_hw_softc)) {
630 /* set up command queue mutex and condition varible */
631 cv_init(&sc->sc_cmd_cv, "CMDQ");
632 sx_init(&sc->sc_cmd_sx, "CMDQ lock");
634 if (usb_proc_create(&sc->sc_config_proc,
635 &sc->sc_bus.bus_mtx, device_get_nameunit(self), USB_PRI_MED)) {
636 printf("WARNING: Creation of XHCI configure "
637 "callback process failed.\n");
639 sc->sc_config_msg[0].hdr.pm_callback = &xhci_configure_msg;
640 sc->sc_config_msg[0].bus = &sc->sc_bus;
641 sc->sc_config_msg[1].hdr.pm_callback = &xhci_configure_msg;
642 sc->sc_config_msg[1].bus = &sc->sc_bus;
648 xhci_uninit(struct xhci_softc *sc)
650 usb_proc_free(&sc->sc_config_proc);
652 usb_bus_mem_free_all(&sc->sc_bus, &xhci_iterate_hw_softc);
654 cv_destroy(&sc->sc_cmd_cv);
655 sx_destroy(&sc->sc_cmd_sx);
659 xhci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
661 struct xhci_softc *sc = XHCI_BUS2SC(bus);
664 case USB_HW_POWER_SUSPEND:
665 DPRINTF("Stopping the XHCI\n");
666 xhci_halt_controller(sc);
668 case USB_HW_POWER_SHUTDOWN:
669 DPRINTF("Stopping the XHCI\n");
670 xhci_halt_controller(sc);
672 case USB_HW_POWER_RESUME:
673 DPRINTF("Starting the XHCI\n");
674 xhci_start_controller(sc);
682 xhci_generic_done_sub(struct usb_xfer *xfer)
685 struct xhci_td *td_alt_next;
689 td = xfer->td_transfer_cache;
690 td_alt_next = td->alt_next;
692 if (xfer->aframes != xfer->nframes)
693 usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
697 usb_pc_cpu_invalidate(td->page_cache);
702 DPRINTFN(4, "xfer=%p[%u/%u] rem=%u/%u status=%u\n",
703 xfer, (unsigned int)xfer->aframes,
704 (unsigned int)xfer->nframes,
705 (unsigned int)len, (unsigned int)td->len,
706 (unsigned int)status);
709 * Verify the status length and
710 * add the length to "frlengths[]":
713 /* should not happen */
714 DPRINTF("Invalid status length, "
715 "0x%04x/0x%04x bytes\n", len, td->len);
716 status = XHCI_TRB_ERROR_LENGTH;
717 } else if (xfer->aframes != xfer->nframes) {
718 xfer->frlengths[xfer->aframes] += td->len - len;
720 /* Check for last transfer */
721 if (((void *)td) == xfer->td_transfer_last) {
725 /* Check for transfer error */
726 if (status != XHCI_TRB_ERROR_SHORT_PKT &&
727 status != XHCI_TRB_ERROR_SUCCESS) {
728 /* the transfer is finished */
732 /* Check for short transfer */
734 if (xfer->flags_int.short_frames_ok ||
735 xfer->flags_int.isochronous_xfr ||
736 xfer->flags_int.control_xfr) {
737 /* follow alt next */
740 /* the transfer is finished */
747 if (td->alt_next != td_alt_next) {
748 /* this USB frame is complete */
753 /* update transfer cache */
755 xfer->td_transfer_cache = td;
757 return ((status == XHCI_TRB_ERROR_STALL) ? USB_ERR_STALLED :
758 (status != XHCI_TRB_ERROR_SHORT_PKT &&
759 status != XHCI_TRB_ERROR_SUCCESS) ? USB_ERR_IOERROR :
760 USB_ERR_NORMAL_COMPLETION);
764 xhci_generic_done(struct usb_xfer *xfer)
768 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
769 xfer, xfer->endpoint);
773 xfer->td_transfer_cache = xfer->td_transfer_first;
775 if (xfer->flags_int.control_xfr) {
777 if (xfer->flags_int.control_hdr)
778 err = xhci_generic_done_sub(xfer);
782 if (xfer->td_transfer_cache == NULL)
786 while (xfer->aframes != xfer->nframes) {
788 err = xhci_generic_done_sub(xfer);
791 if (xfer->td_transfer_cache == NULL)
795 if (xfer->flags_int.control_xfr &&
796 !xfer->flags_int.control_act)
797 err = xhci_generic_done_sub(xfer);
799 /* transfer is complete */
800 xhci_device_done(xfer, err);
804 xhci_activate_transfer(struct usb_xfer *xfer)
808 td = xfer->td_transfer_cache;
810 usb_pc_cpu_invalidate(td->page_cache);
812 if (!(td->td_trb[0].dwTrb3 & htole32(XHCI_TRB_3_CYCLE_BIT))) {
814 /* activate the transfer */
816 td->td_trb[0].dwTrb3 |= htole32(XHCI_TRB_3_CYCLE_BIT);
817 usb_pc_cpu_flush(td->page_cache);
819 xhci_endpoint_doorbell(xfer);
824 xhci_skip_transfer(struct usb_xfer *xfer)
827 struct xhci_td *td_last;
829 td = xfer->td_transfer_cache;
830 td_last = xfer->td_transfer_last;
834 usb_pc_cpu_invalidate(td->page_cache);
836 if (!(td->td_trb[0].dwTrb3 & htole32(XHCI_TRB_3_CYCLE_BIT))) {
838 usb_pc_cpu_invalidate(td_last->page_cache);
840 /* copy LINK TRB to current waiting location */
842 td->td_trb[0].qwTrb0 = td_last->td_trb[td_last->ntrb].qwTrb0;
843 td->td_trb[0].dwTrb2 = td_last->td_trb[td_last->ntrb].dwTrb2;
844 usb_pc_cpu_flush(td->page_cache);
846 td->td_trb[0].dwTrb3 = td_last->td_trb[td_last->ntrb].dwTrb3;
847 usb_pc_cpu_flush(td->page_cache);
849 xhci_endpoint_doorbell(xfer);
853 /*------------------------------------------------------------------------*
854 * xhci_check_transfer
855 *------------------------------------------------------------------------*/
857 xhci_check_transfer(struct xhci_softc *sc, struct xhci_trb *trb)
870 td_event = le64toh(trb->qwTrb0);
871 temp = le32toh(trb->dwTrb2);
873 remainder = XHCI_TRB_2_REM_GET(temp);
874 status = XHCI_TRB_2_ERROR_GET(temp);
876 temp = le32toh(trb->dwTrb3);
877 epno = XHCI_TRB_3_EP_GET(temp);
878 index = XHCI_TRB_3_SLOT_GET(temp);
880 /* check if error means halted */
881 halted = (status != XHCI_TRB_ERROR_SHORT_PKT &&
882 status != XHCI_TRB_ERROR_SUCCESS);
884 DPRINTF("slot=%u epno=%u remainder=%u status=%u\n",
885 index, epno, remainder, status);
887 if (index > sc->sc_noslot) {
888 DPRINTF("Invalid slot.\n");
892 if ((epno == 0) || (epno >= XHCI_MAX_ENDPOINTS)) {
893 DPRINTF("Invalid endpoint.\n");
897 /* try to find the USB transfer that generated the event */
898 for (i = 0; i != (XHCI_MAX_TRANSFERS - 1); i++) {
899 struct usb_xfer *xfer;
901 struct xhci_endpoint_ext *pepext;
903 pepext = &sc->sc_hw.devs[index].endp[epno];
905 xfer = pepext->xfer[i];
909 td = xfer->td_transfer_cache;
911 DPRINTFN(5, "Checking if 0x%016llx == (0x%016llx .. 0x%016llx)\n",
913 (long long)td->td_self,
914 (long long)td->td_self + sizeof(td->td_trb));
917 * NOTE: Some XHCI implementations might not trigger
918 * an event on the last LINK TRB so we need to
919 * consider both the last and second last event
920 * address as conditions for a successful transfer.
922 * NOTE: We assume that the XHCI will only trigger one
923 * event per chain of TRBs.
926 offset = td_event - td->td_self;
929 offset < (int64_t)sizeof(td->td_trb)) {
931 usb_pc_cpu_invalidate(td->page_cache);
933 /* compute rest of remainder, if any */
934 for (i = (offset / 16) + 1; i < td->ntrb; i++) {
935 temp = le32toh(td->td_trb[i].dwTrb2);
936 remainder += XHCI_TRB_2_BYTES_GET(temp);
939 DPRINTFN(5, "New remainder: %u\n", remainder);
941 /* clear isochronous transfer errors */
942 if (xfer->flags_int.isochronous_xfr) {
945 status = XHCI_TRB_ERROR_SUCCESS;
950 /* "td->remainder" is verified later */
951 td->remainder = remainder;
954 usb_pc_cpu_flush(td->page_cache);
957 * 1) Last transfer descriptor makes the
960 if (((void *)td) == xfer->td_transfer_last) {
961 DPRINTF("TD is last\n");
962 xhci_generic_done(xfer);
967 * 2) Any kind of error makes the transfer
971 DPRINTF("TD has I/O error\n");
972 xhci_generic_done(xfer);
977 * 3) If there is no alternate next transfer,
978 * a short packet also makes the transfer done
980 if (td->remainder > 0) {
981 if (td->alt_next == NULL) {
983 "short TD has no alternate next\n");
984 xhci_generic_done(xfer);
987 DPRINTF("TD has short pkt\n");
988 if (xfer->flags_int.short_frames_ok ||
989 xfer->flags_int.isochronous_xfr ||
990 xfer->flags_int.control_xfr) {
991 /* follow the alt next */
992 xfer->td_transfer_cache = td->alt_next;
993 xhci_activate_transfer(xfer);
996 xhci_skip_transfer(xfer);
997 xhci_generic_done(xfer);
1002 * 4) Transfer complete - go to next TD
1004 DPRINTF("Following next TD\n");
1005 xfer->td_transfer_cache = td->obj_next;
1006 xhci_activate_transfer(xfer);
1007 break; /* there should only be one match */
1013 xhci_check_command(struct xhci_softc *sc, struct xhci_trb *trb)
1015 if (sc->sc_cmd_addr == trb->qwTrb0) {
1016 DPRINTF("Received command event\n");
1017 sc->sc_cmd_result[0] = trb->dwTrb2;
1018 sc->sc_cmd_result[1] = trb->dwTrb3;
1019 cv_signal(&sc->sc_cmd_cv);
1020 return (1); /* command match */
1026 xhci_interrupt_poll(struct xhci_softc *sc)
1028 struct usb_page_search buf_res;
1029 struct xhci_hw_root *phwr;
1039 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
1041 phwr = buf_res.buffer;
1043 /* Receive any events */
1045 usb_pc_cpu_invalidate(&sc->sc_hw.root_pc);
1047 i = sc->sc_event_idx;
1048 j = sc->sc_event_ccs;
1053 temp = le32toh(phwr->hwr_events[i].dwTrb3);
1055 k = (temp & XHCI_TRB_3_CYCLE_BIT) ? 1 : 0;
1060 event = XHCI_TRB_3_TYPE_GET(temp);
1062 DPRINTFN(10, "event[%u] = %u (0x%016llx 0x%08lx 0x%08lx)\n",
1063 i, event, (long long)le64toh(phwr->hwr_events[i].qwTrb0),
1064 (long)le32toh(phwr->hwr_events[i].dwTrb2),
1065 (long)le32toh(phwr->hwr_events[i].dwTrb3));
1068 case XHCI_TRB_EVENT_TRANSFER:
1069 xhci_check_transfer(sc, &phwr->hwr_events[i]);
1071 case XHCI_TRB_EVENT_CMD_COMPLETE:
1072 retval |= xhci_check_command(sc, &phwr->hwr_events[i]);
1075 DPRINTF("Unhandled event = %u\n", event);
1081 if (i == XHCI_MAX_EVENTS) {
1085 /* check for timeout */
1091 sc->sc_event_idx = i;
1092 sc->sc_event_ccs = j;
1095 * NOTE: The Event Ring Dequeue Pointer Register is 64-bit
1096 * latched. That means to activate the register we need to
1097 * write both the low and high double word of the 64-bit
1101 addr = buf_res.physaddr;
1102 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_events[i];
1104 /* try to clear busy bit */
1105 addr |= XHCI_ERDP_LO_BUSY;
1107 XWRITE4(sc, runt, XHCI_ERDP_LO(0), (uint32_t)addr);
1108 XWRITE4(sc, runt, XHCI_ERDP_HI(0), (uint32_t)(addr >> 32));
1114 xhci_do_command(struct xhci_softc *sc, struct xhci_trb *trb,
1115 uint16_t timeout_ms)
1117 struct usb_page_search buf_res;
1118 struct xhci_hw_root *phwr;
1123 uint8_t timeout = 0;
1126 XHCI_CMD_ASSERT_LOCKED(sc);
1128 /* get hardware root structure */
1130 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
1132 phwr = buf_res.buffer;
1136 USB_BUS_LOCK(&sc->sc_bus);
1138 i = sc->sc_command_idx;
1139 j = sc->sc_command_ccs;
1141 DPRINTFN(10, "command[%u] = %u (0x%016llx, 0x%08lx, 0x%08lx)\n",
1142 i, XHCI_TRB_3_TYPE_GET(le32toh(trb->dwTrb3)),
1143 (long long)le64toh(trb->qwTrb0),
1144 (long)le32toh(trb->dwTrb2),
1145 (long)le32toh(trb->dwTrb3));
1147 phwr->hwr_commands[i].qwTrb0 = trb->qwTrb0;
1148 phwr->hwr_commands[i].dwTrb2 = trb->dwTrb2;
1150 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1155 temp |= htole32(XHCI_TRB_3_CYCLE_BIT);
1157 temp &= ~htole32(XHCI_TRB_3_CYCLE_BIT);
1159 temp &= ~htole32(XHCI_TRB_3_TC_BIT);
1161 phwr->hwr_commands[i].dwTrb3 = temp;
1163 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1165 addr = buf_res.physaddr;
1166 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_commands[i];
1168 sc->sc_cmd_addr = htole64(addr);
1172 if (i == (XHCI_MAX_COMMANDS - 1)) {
1175 temp = htole32(XHCI_TRB_3_TC_BIT |
1176 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) |
1177 XHCI_TRB_3_CYCLE_BIT);
1179 temp = htole32(XHCI_TRB_3_TC_BIT |
1180 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
1183 phwr->hwr_commands[i].dwTrb3 = temp;
1185 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1191 sc->sc_command_idx = i;
1192 sc->sc_command_ccs = j;
1194 XWRITE4(sc, door, XHCI_DOORBELL(0), 0);
1196 err = cv_timedwait(&sc->sc_cmd_cv, &sc->sc_bus.bus_mtx,
1197 USB_MS_TO_TICKS(timeout_ms));
1200 * In some error cases event interrupts are not generated.
1201 * Poll one time to see if the command has completed.
1203 if (err != 0 && xhci_interrupt_poll(sc) != 0) {
1204 DPRINTF("Command was completed when polling\n");
1208 DPRINTF("Command timeout!\n");
1210 * After some weeks of continuous operation, it has
1211 * been observed that the ASMedia Technology, ASM1042
1212 * SuperSpeed USB Host Controller can suddenly stop
1213 * accepting commands via the command queue. Try to
1214 * first reset the command queue. If that fails do a
1215 * host controller reset.
1218 xhci_reset_command_queue_locked(sc) == 0) {
1219 temp = le32toh(trb->dwTrb3);
1222 * Avoid infinite XHCI reset loops if the set
1223 * address command fails to respond due to a
1224 * non-enumerating device:
1226 if (XHCI_TRB_3_TYPE_GET(temp) == XHCI_TRB_TYPE_ADDRESS_DEVICE &&
1227 (temp & XHCI_TRB_3_BSR_BIT) == 0) {
1228 DPRINTF("Set address timeout\n");
1234 DPRINTF("Controller reset!\n");
1235 usb_bus_reset_async_locked(&sc->sc_bus);
1237 err = USB_ERR_TIMEOUT;
1241 temp = le32toh(sc->sc_cmd_result[0]);
1242 if (XHCI_TRB_2_ERROR_GET(temp) != XHCI_TRB_ERROR_SUCCESS)
1243 err = USB_ERR_IOERROR;
1245 trb->dwTrb2 = sc->sc_cmd_result[0];
1246 trb->dwTrb3 = sc->sc_cmd_result[1];
1249 USB_BUS_UNLOCK(&sc->sc_bus);
1256 xhci_cmd_nop(struct xhci_softc *sc)
1258 struct xhci_trb trb;
1265 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NOOP);
1267 trb.dwTrb3 = htole32(temp);
1269 return (xhci_do_command(sc, &trb, 100 /* ms */));
1274 xhci_cmd_enable_slot(struct xhci_softc *sc, uint8_t *pslot)
1276 struct xhci_trb trb;
1284 trb.dwTrb3 = htole32(XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ENABLE_SLOT));
1286 err = xhci_do_command(sc, &trb, 100 /* ms */);
1290 temp = le32toh(trb.dwTrb3);
1292 *pslot = XHCI_TRB_3_SLOT_GET(temp);
1299 xhci_cmd_disable_slot(struct xhci_softc *sc, uint8_t slot_id)
1301 struct xhci_trb trb;
1308 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DISABLE_SLOT) |
1309 XHCI_TRB_3_SLOT_SET(slot_id);
1311 trb.dwTrb3 = htole32(temp);
1313 return (xhci_do_command(sc, &trb, 100 /* ms */));
1317 xhci_cmd_set_address(struct xhci_softc *sc, uint64_t input_ctx,
1318 uint8_t bsr, 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_ADDRESS_DEVICE) |
1328 XHCI_TRB_3_SLOT_SET(slot_id);
1331 temp |= XHCI_TRB_3_BSR_BIT;
1333 trb.dwTrb3 = htole32(temp);
1335 return (xhci_do_command(sc, &trb, 500 /* ms */));
1339 xhci_set_address(struct usb_device *udev, struct mtx *mtx, uint16_t address)
1341 struct usb_page_search buf_inp;
1342 struct usb_page_search buf_dev;
1343 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
1344 struct xhci_hw_dev *hdev;
1345 struct xhci_dev_ctx *pdev;
1346 struct xhci_endpoint_ext *pepext;
1352 /* the root HUB case is not handled here */
1353 if (udev->parent_hub == NULL)
1354 return (USB_ERR_INVAL);
1356 index = udev->controller_slot_id;
1358 hdev = &sc->sc_hw.devs[index];
1365 switch (hdev->state) {
1366 case XHCI_ST_DEFAULT:
1367 case XHCI_ST_ENABLED:
1369 hdev->state = XHCI_ST_ENABLED;
1371 /* set configure mask to slot and EP0 */
1372 xhci_configure_mask(udev, 3, 0);
1374 /* configure input slot context structure */
1375 err = xhci_configure_device(udev);
1378 DPRINTF("Could not configure device\n");
1382 /* configure input endpoint context structure */
1383 switch (udev->speed) {
1385 case USB_SPEED_FULL:
1388 case USB_SPEED_HIGH:
1396 pepext = xhci_get_endpoint_ext(udev,
1397 &udev->ctrl_ep_desc);
1399 /* ensure the control endpoint is setup again */
1400 USB_BUS_LOCK(udev->bus);
1401 pepext->trb_halted = 1;
1402 pepext->trb_running = 0;
1403 USB_BUS_UNLOCK(udev->bus);
1405 err = xhci_configure_endpoint(udev,
1406 &udev->ctrl_ep_desc, pepext->physaddr,
1407 0, 1, 1, 0, mps, mps);
1410 DPRINTF("Could not configure default endpoint\n");
1414 /* execute set address command */
1415 usbd_get_page(&hdev->input_pc, 0, &buf_inp);
1417 err = xhci_cmd_set_address(sc, buf_inp.physaddr,
1418 (address == 0), index);
1421 temp = le32toh(sc->sc_cmd_result[0]);
1422 if (address == 0 && sc->sc_port_route != NULL &&
1423 XHCI_TRB_2_ERROR_GET(temp) ==
1424 XHCI_TRB_ERROR_PARAMETER) {
1425 /* LynxPoint XHCI - ports are not switchable */
1426 /* Un-route all ports from the XHCI */
1427 sc->sc_port_route(sc->sc_bus.parent, 0, ~0);
1429 DPRINTF("Could not set address "
1430 "for slot %u.\n", index);
1435 /* update device address to new value */
1437 usbd_get_page(&hdev->device_pc, 0, &buf_dev);
1438 pdev = buf_dev.buffer;
1439 usb_pc_cpu_invalidate(&hdev->device_pc);
1441 temp = xhci_ctx_get_le32(sc, &pdev->ctx_slot.dwSctx3);
1442 udev->address = XHCI_SCTX_3_DEV_ADDR_GET(temp);
1444 /* update device state to new value */
1447 hdev->state = XHCI_ST_ADDRESSED;
1449 hdev->state = XHCI_ST_DEFAULT;
1453 DPRINTF("Wrong state for set address.\n");
1454 err = USB_ERR_IOERROR;
1457 XHCI_CMD_UNLOCK(sc);
1466 xhci_cmd_configure_ep(struct xhci_softc *sc, uint64_t input_ctx,
1467 uint8_t deconfigure, uint8_t slot_id)
1469 struct xhci_trb trb;
1474 trb.qwTrb0 = htole64(input_ctx);
1476 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_CONFIGURE_EP) |
1477 XHCI_TRB_3_SLOT_SET(slot_id);
1480 temp |= XHCI_TRB_3_DCEP_BIT;
1482 trb.dwTrb3 = htole32(temp);
1484 return (xhci_do_command(sc, &trb, 100 /* ms */));
1488 xhci_cmd_evaluate_ctx(struct xhci_softc *sc, uint64_t input_ctx,
1491 struct xhci_trb trb;
1496 trb.qwTrb0 = htole64(input_ctx);
1498 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVALUATE_CTX) |
1499 XHCI_TRB_3_SLOT_SET(slot_id);
1500 trb.dwTrb3 = htole32(temp);
1502 return (xhci_do_command(sc, &trb, 100 /* ms */));
1506 xhci_cmd_reset_ep(struct xhci_softc *sc, uint8_t preserve,
1507 uint8_t ep_id, uint8_t slot_id)
1509 struct xhci_trb trb;
1516 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_EP) |
1517 XHCI_TRB_3_SLOT_SET(slot_id) |
1518 XHCI_TRB_3_EP_SET(ep_id);
1521 temp |= XHCI_TRB_3_PRSV_BIT;
1523 trb.dwTrb3 = htole32(temp);
1525 return (xhci_do_command(sc, &trb, 100 /* ms */));
1529 xhci_cmd_set_tr_dequeue_ptr(struct xhci_softc *sc, uint64_t dequeue_ptr,
1530 uint16_t stream_id, uint8_t ep_id, uint8_t slot_id)
1532 struct xhci_trb trb;
1537 trb.qwTrb0 = htole64(dequeue_ptr);
1539 temp = XHCI_TRB_2_STREAM_SET(stream_id);
1540 trb.dwTrb2 = htole32(temp);
1542 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SET_TR_DEQUEUE) |
1543 XHCI_TRB_3_SLOT_SET(slot_id) |
1544 XHCI_TRB_3_EP_SET(ep_id);
1545 trb.dwTrb3 = htole32(temp);
1547 return (xhci_do_command(sc, &trb, 100 /* ms */));
1551 xhci_cmd_stop_ep(struct xhci_softc *sc, uint8_t suspend,
1552 uint8_t ep_id, uint8_t slot_id)
1554 struct xhci_trb trb;
1561 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STOP_EP) |
1562 XHCI_TRB_3_SLOT_SET(slot_id) |
1563 XHCI_TRB_3_EP_SET(ep_id);
1566 temp |= XHCI_TRB_3_SUSP_EP_BIT;
1568 trb.dwTrb3 = htole32(temp);
1570 return (xhci_do_command(sc, &trb, 100 /* ms */));
1574 xhci_cmd_reset_dev(struct xhci_softc *sc, uint8_t slot_id)
1576 struct xhci_trb trb;
1583 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_DEVICE) |
1584 XHCI_TRB_3_SLOT_SET(slot_id);
1586 trb.dwTrb3 = htole32(temp);
1588 return (xhci_do_command(sc, &trb, 100 /* ms */));
1591 /*------------------------------------------------------------------------*
1592 * xhci_interrupt - XHCI interrupt handler
1593 *------------------------------------------------------------------------*/
1595 xhci_interrupt(struct xhci_softc *sc)
1600 USB_BUS_LOCK(&sc->sc_bus);
1602 status = XREAD4(sc, oper, XHCI_USBSTS);
1604 /* acknowledge interrupts, if any */
1606 XWRITE4(sc, oper, XHCI_USBSTS, status);
1607 DPRINTFN(16, "real interrupt (status=0x%08x)\n", status);
1610 temp = XREAD4(sc, runt, XHCI_IMAN(0));
1612 /* force clearing of pending interrupts */
1613 if (temp & XHCI_IMAN_INTR_PEND)
1614 XWRITE4(sc, runt, XHCI_IMAN(0), temp);
1616 /* check for event(s) */
1617 xhci_interrupt_poll(sc);
1619 if (status & (XHCI_STS_PCD | XHCI_STS_HCH |
1620 XHCI_STS_HSE | XHCI_STS_HCE)) {
1622 if (status & XHCI_STS_PCD) {
1626 if (status & XHCI_STS_HCH) {
1627 printf("%s: host controller halted\n",
1631 if (status & XHCI_STS_HSE) {
1632 printf("%s: host system error\n",
1636 if (status & XHCI_STS_HCE) {
1637 printf("%s: host controller error\n",
1641 USB_BUS_UNLOCK(&sc->sc_bus);
1644 /*------------------------------------------------------------------------*
1645 * xhci_timeout - XHCI timeout handler
1646 *------------------------------------------------------------------------*/
1648 xhci_timeout(void *arg)
1650 struct usb_xfer *xfer = arg;
1652 DPRINTF("xfer=%p\n", xfer);
1654 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1656 /* transfer is transferred */
1657 xhci_device_done(xfer, USB_ERR_TIMEOUT);
1661 xhci_do_poll(struct usb_bus *bus)
1663 struct xhci_softc *sc = XHCI_BUS2SC(bus);
1665 USB_BUS_LOCK(&sc->sc_bus);
1666 xhci_interrupt_poll(sc);
1667 USB_BUS_UNLOCK(&sc->sc_bus);
1671 xhci_setup_generic_chain_sub(struct xhci_std_temp *temp)
1673 struct usb_page_search buf_res;
1675 struct xhci_td *td_next;
1676 struct xhci_td *td_alt_next;
1677 struct xhci_td *td_first;
1678 uint32_t buf_offset;
1683 uint8_t shortpkt_old;
1689 shortpkt_old = temp->shortpkt;
1690 len_old = temp->len;
1697 td_next = td_first = temp->td_next;
1701 if (temp->len == 0) {
1706 /* send a Zero Length Packet, ZLP, last */
1713 average = temp->average;
1715 if (temp->len < average) {
1716 if (temp->len % temp->max_packet_size) {
1719 average = temp->len;
1723 if (td_next == NULL)
1724 panic("%s: out of XHCI transfer descriptors!", __FUNCTION__);
1729 td_next = td->obj_next;
1731 /* check if we are pre-computing */
1735 /* update remaining length */
1737 temp->len -= average;
1741 /* fill out current TD */
1747 /* update remaining length */
1749 temp->len -= average;
1751 /* reset TRB index */
1755 if (temp->trb_type == XHCI_TRB_TYPE_SETUP_STAGE) {
1756 /* immediate data */
1761 td->td_trb[0].qwTrb0 = 0;
1763 usbd_copy_out(temp->pc, temp->offset + buf_offset,
1764 (uint8_t *)(uintptr_t)&td->td_trb[0].qwTrb0,
1767 dword = XHCI_TRB_2_BYTES_SET(8) |
1768 XHCI_TRB_2_TDSZ_SET(0) |
1769 XHCI_TRB_2_IRQ_SET(0);
1771 td->td_trb[0].dwTrb2 = htole32(dword);
1773 dword = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SETUP_STAGE) |
1774 XHCI_TRB_3_IDT_BIT | XHCI_TRB_3_CYCLE_BIT;
1777 if (td->td_trb[0].qwTrb0 &
1778 htole64(XHCI_TRB_0_WLENGTH_MASK)) {
1779 if (td->td_trb[0].qwTrb0 &
1780 htole64(XHCI_TRB_0_DIR_IN_MASK))
1781 dword |= XHCI_TRB_3_TRT_IN;
1783 dword |= XHCI_TRB_3_TRT_OUT;
1786 td->td_trb[0].dwTrb3 = htole32(dword);
1788 xhci_dump_trb(&td->td_trb[x]);
1796 /* fill out buffer pointers */
1799 memset(&buf_res, 0, sizeof(buf_res));
1801 usbd_get_page(temp->pc, temp->offset +
1802 buf_offset, &buf_res);
1804 /* get length to end of page */
1805 if (buf_res.length > average)
1806 buf_res.length = average;
1808 /* check for maximum length */
1809 if (buf_res.length > XHCI_TD_PAGE_SIZE)
1810 buf_res.length = XHCI_TD_PAGE_SIZE;
1812 npkt_off += buf_res.length;
1816 npkt = (len_old - npkt_off + temp->max_packet_size - 1) /
1817 temp->max_packet_size;
1824 /* fill out TRB's */
1825 td->td_trb[x].qwTrb0 =
1826 htole64((uint64_t)buf_res.physaddr);
1829 XHCI_TRB_2_BYTES_SET(buf_res.length) |
1830 XHCI_TRB_2_TDSZ_SET(npkt) |
1831 XHCI_TRB_2_IRQ_SET(0);
1833 td->td_trb[x].dwTrb2 = htole32(dword);
1835 switch (temp->trb_type) {
1836 case XHCI_TRB_TYPE_ISOCH:
1837 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1838 XHCI_TRB_3_TBC_SET(temp->tbc) |
1839 XHCI_TRB_3_TLBPC_SET(temp->tlbpc);
1840 if (td != td_first) {
1841 dword |= XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL);
1842 } else if (temp->do_isoc_sync != 0) {
1843 temp->do_isoc_sync = 0;
1844 /* wait until "isoc_frame" */
1845 dword |= XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ISOCH) |
1846 XHCI_TRB_3_FRID_SET(temp->isoc_frame / 8);
1848 /* start data transfer at next interval */
1849 dword |= XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ISOCH) |
1850 XHCI_TRB_3_ISO_SIA_BIT;
1852 if (temp->direction == UE_DIR_IN)
1853 dword |= XHCI_TRB_3_ISP_BIT;
1855 case XHCI_TRB_TYPE_DATA_STAGE:
1856 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1857 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DATA_STAGE);
1858 if (temp->direction == UE_DIR_IN)
1859 dword |= XHCI_TRB_3_DIR_IN | XHCI_TRB_3_ISP_BIT;
1861 * Section 3.2.9 in the XHCI
1862 * specification about control
1863 * transfers says that we should use a
1864 * normal-TRB if there are more TRBs
1865 * extending the data-stage
1866 * TRB. Update the "trb_type".
1868 temp->trb_type = XHCI_TRB_TYPE_NORMAL;
1870 case XHCI_TRB_TYPE_STATUS_STAGE:
1871 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1872 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STATUS_STAGE);
1873 if (temp->direction == UE_DIR_IN)
1874 dword |= XHCI_TRB_3_DIR_IN;
1876 default: /* XHCI_TRB_TYPE_NORMAL */
1877 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1878 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL);
1879 if (temp->direction == UE_DIR_IN)
1880 dword |= XHCI_TRB_3_ISP_BIT;
1883 td->td_trb[x].dwTrb3 = htole32(dword);
1885 average -= buf_res.length;
1886 buf_offset += buf_res.length;
1888 xhci_dump_trb(&td->td_trb[x]);
1892 } while (average != 0);
1894 td->td_trb[x-1].dwTrb3 |= htole32(XHCI_TRB_3_IOC_BIT);
1896 /* store number of data TRB's */
1900 DPRINTF("NTRB=%u\n", x);
1902 /* fill out link TRB */
1904 if (td_next != NULL) {
1905 /* link the current TD with the next one */
1906 td->td_trb[x].qwTrb0 = htole64((uint64_t)td_next->td_self);
1907 DPRINTF("LINK=0x%08llx\n", (long long)td_next->td_self);
1909 /* this field will get updated later */
1910 DPRINTF("NOLINK\n");
1913 dword = XHCI_TRB_2_IRQ_SET(0);
1915 td->td_trb[x].dwTrb2 = htole32(dword);
1917 dword = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) |
1918 XHCI_TRB_3_CYCLE_BIT | XHCI_TRB_3_IOC_BIT |
1920 * CHAIN-BIT: Ensure that a multi-TRB IN-endpoint
1921 * frame only receives a single short packet event
1922 * by setting the CHAIN bit in the LINK field. In
1923 * addition some XHCI controllers have problems
1924 * sending a ZLP unless the CHAIN-BIT is set in
1927 XHCI_TRB_3_CHAIN_BIT;
1929 td->td_trb[x].dwTrb3 = htole32(dword);
1931 td->alt_next = td_alt_next;
1933 xhci_dump_trb(&td->td_trb[x]);
1935 usb_pc_cpu_flush(td->page_cache);
1941 /* set up alt next pointer, if any */
1942 if (temp->last_frame) {
1945 /* we use this field internally */
1946 td_alt_next = td_next;
1950 temp->shortpkt = shortpkt_old;
1951 temp->len = len_old;
1956 * Remove cycle bit from the first TRB if we are
1959 if (temp->step_td != 0) {
1960 td_first->td_trb[0].dwTrb3 &= ~htole32(XHCI_TRB_3_CYCLE_BIT);
1961 usb_pc_cpu_flush(td_first->page_cache);
1964 /* clear TD SIZE to zero, hence this is the last TRB */
1965 /* remove chain bit because this is the last data TRB in the chain */
1966 td->td_trb[td->ntrb - 1].dwTrb2 &= ~htole32(XHCI_TRB_2_TDSZ_SET(15));
1967 td->td_trb[td->ntrb - 1].dwTrb3 &= ~htole32(XHCI_TRB_3_CHAIN_BIT);
1968 /* remove CHAIN-BIT from last LINK TRB */
1969 td->td_trb[td->ntrb].dwTrb3 &= ~htole32(XHCI_TRB_3_CHAIN_BIT);
1971 usb_pc_cpu_flush(td->page_cache);
1974 temp->td_next = td_next;
1978 xhci_setup_generic_chain(struct usb_xfer *xfer)
1980 struct xhci_std_temp temp;
1986 temp.do_isoc_sync = 0;
1990 temp.average = xfer->max_hc_frame_size;
1991 temp.max_packet_size = xfer->max_packet_size;
1992 temp.sc = XHCI_BUS2SC(xfer->xroot->bus);
1994 temp.last_frame = 0;
1996 temp.multishort = xfer->flags_int.isochronous_xfr ||
1997 xfer->flags_int.control_xfr ||
1998 xfer->flags_int.short_frames_ok;
2000 /* toggle the DMA set we are using */
2001 xfer->flags_int.curr_dma_set ^= 1;
2003 /* get next DMA set */
2004 td = xfer->td_start[xfer->flags_int.curr_dma_set];
2009 xfer->td_transfer_first = td;
2010 xfer->td_transfer_cache = td;
2012 if (xfer->flags_int.isochronous_xfr) {
2015 /* compute multiplier for ISOCHRONOUS transfers */
2016 mult = xfer->endpoint->ecomp ?
2017 (xfer->endpoint->ecomp->bmAttributes & 3) : 0;
2018 /* check for USB 2.0 multiplier */
2020 mult = (xfer->endpoint->edesc->
2021 wMaxPacketSize[1] >> 3) & 3;
2029 x = XREAD4(temp.sc, runt, XHCI_MFINDEX);
2031 DPRINTF("MFINDEX=0x%08x\n", x);
2033 switch (usbd_get_speed(xfer->xroot->udev)) {
2034 case USB_SPEED_FULL:
2036 temp.isoc_delta = 8; /* 1ms */
2037 x += temp.isoc_delta - 1;
2038 x &= ~(temp.isoc_delta - 1);
2041 shift = usbd_xfer_get_fps_shift(xfer);
2042 temp.isoc_delta = 1U << shift;
2043 x += temp.isoc_delta - 1;
2044 x &= ~(temp.isoc_delta - 1);
2045 /* simple frame load balancing */
2046 x += xfer->endpoint->usb_uframe;
2050 y = XHCI_MFINDEX_GET(x - xfer->endpoint->isoc_next);
2052 if ((xfer->endpoint->is_synced == 0) ||
2053 (y < (xfer->nframes << shift)) ||
2054 (XHCI_MFINDEX_GET(-y) >= (128 * 8))) {
2056 * If there is data underflow or the pipe
2057 * queue is empty we schedule the transfer a
2058 * few frames ahead of the current frame
2059 * position. Else two isochronous transfers
2062 xfer->endpoint->isoc_next = XHCI_MFINDEX_GET(x + (3 * 8));
2063 xfer->endpoint->is_synced = 1;
2064 temp.do_isoc_sync = 1;
2066 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
2069 /* compute isochronous completion time */
2071 y = XHCI_MFINDEX_GET(xfer->endpoint->isoc_next - (x & ~7));
2073 xfer->isoc_time_complete =
2074 usb_isoc_time_expand(&temp.sc->sc_bus, x / 8) +
2075 (y / 8) + (((xfer->nframes << shift) + 7) / 8);
2078 temp.isoc_frame = xfer->endpoint->isoc_next;
2079 temp.trb_type = XHCI_TRB_TYPE_ISOCH;
2081 xfer->endpoint->isoc_next += xfer->nframes << shift;
2083 } else if (xfer->flags_int.control_xfr) {
2085 /* check if we should prepend a setup message */
2087 if (xfer->flags_int.control_hdr) {
2089 temp.len = xfer->frlengths[0];
2090 temp.pc = xfer->frbuffers + 0;
2091 temp.shortpkt = temp.len ? 1 : 0;
2092 temp.trb_type = XHCI_TRB_TYPE_SETUP_STAGE;
2095 /* check for last frame */
2096 if (xfer->nframes == 1) {
2097 /* no STATUS stage yet, SETUP is last */
2098 if (xfer->flags_int.control_act)
2099 temp.last_frame = 1;
2102 xhci_setup_generic_chain_sub(&temp);
2106 temp.isoc_delta = 0;
2107 temp.isoc_frame = 0;
2108 temp.trb_type = xfer->flags_int.control_did_data ?
2109 XHCI_TRB_TYPE_NORMAL : XHCI_TRB_TYPE_DATA_STAGE;
2113 temp.isoc_delta = 0;
2114 temp.isoc_frame = 0;
2115 temp.trb_type = XHCI_TRB_TYPE_NORMAL;
2118 if (x != xfer->nframes) {
2119 /* set up page_cache pointer */
2120 temp.pc = xfer->frbuffers + x;
2121 /* set endpoint direction */
2122 temp.direction = UE_GET_DIR(xfer->endpointno);
2125 while (x != xfer->nframes) {
2127 /* DATA0 / DATA1 message */
2129 temp.len = xfer->frlengths[x];
2130 temp.step_td = ((xfer->endpointno & UE_DIR_IN) &&
2131 x != 0 && temp.multishort == 0);
2135 if (x == xfer->nframes) {
2136 if (xfer->flags_int.control_xfr) {
2137 /* no STATUS stage yet, DATA is last */
2138 if (xfer->flags_int.control_act)
2139 temp.last_frame = 1;
2141 temp.last_frame = 1;
2144 if (temp.len == 0) {
2146 /* make sure that we send an USB packet */
2151 temp.tlbpc = mult - 1;
2153 } else if (xfer->flags_int.isochronous_xfr) {
2158 * Isochronous transfers don't have short
2159 * packet termination:
2164 /* isochronous transfers have a transfer limit */
2166 if (temp.len > xfer->max_frame_size)
2167 temp.len = xfer->max_frame_size;
2169 /* compute TD packet count */
2170 tdpc = (temp.len + xfer->max_packet_size - 1) /
2171 xfer->max_packet_size;
2173 temp.tbc = ((tdpc + mult - 1) / mult) - 1;
2174 temp.tlbpc = (tdpc % mult);
2176 if (temp.tlbpc == 0)
2177 temp.tlbpc = mult - 1;
2182 /* regular data transfer */
2184 temp.shortpkt = xfer->flags.force_short_xfer ? 0 : 1;
2187 xhci_setup_generic_chain_sub(&temp);
2189 if (xfer->flags_int.isochronous_xfr) {
2190 temp.offset += xfer->frlengths[x - 1];
2191 temp.isoc_frame += temp.isoc_delta;
2193 /* get next Page Cache pointer */
2194 temp.pc = xfer->frbuffers + x;
2198 /* check if we should append a status stage */
2200 if (xfer->flags_int.control_xfr &&
2201 !xfer->flags_int.control_act) {
2204 * Send a DATA1 message and invert the current
2205 * endpoint direction.
2207 temp.step_td = (xfer->nframes != 0);
2208 temp.direction = UE_GET_DIR(xfer->endpointno) ^ UE_DIR_IN;
2212 temp.last_frame = 1;
2213 temp.trb_type = XHCI_TRB_TYPE_STATUS_STAGE;
2215 xhci_setup_generic_chain_sub(&temp);
2220 /* must have at least one frame! */
2222 xfer->td_transfer_last = td;
2224 DPRINTF("first=%p last=%p\n", xfer->td_transfer_first, td);
2228 xhci_set_slot_pointer(struct xhci_softc *sc, uint8_t index, uint64_t dev_addr)
2230 struct usb_page_search buf_res;
2231 struct xhci_dev_ctx_addr *pdctxa;
2233 usbd_get_page(&sc->sc_hw.ctx_pc, 0, &buf_res);
2235 pdctxa = buf_res.buffer;
2237 DPRINTF("addr[%u]=0x%016llx\n", index, (long long)dev_addr);
2239 pdctxa->qwBaaDevCtxAddr[index] = htole64(dev_addr);
2241 usb_pc_cpu_flush(&sc->sc_hw.ctx_pc);
2245 xhci_configure_mask(struct usb_device *udev, uint32_t mask, uint8_t drop)
2247 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2248 struct usb_page_search buf_inp;
2249 struct xhci_input_dev_ctx *pinp;
2254 index = udev->controller_slot_id;
2256 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
2258 pinp = buf_inp.buffer;
2261 mask &= XHCI_INCTX_NON_CTRL_MASK;
2262 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx0, mask);
2263 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx1, 0);
2266 * Some hardware requires that we drop the endpoint
2267 * context before adding it again:
2269 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx0,
2270 mask & XHCI_INCTX_NON_CTRL_MASK);
2272 /* Add new endpoint context */
2273 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx1, mask);
2275 /* find most significant set bit */
2276 for (x = 31; x != 1; x--) {
2277 if (mask & (1 << x))
2284 /* figure out the maximum number of contexts */
2285 if (x > sc->sc_hw.devs[index].context_num)
2286 sc->sc_hw.devs[index].context_num = x;
2288 x = sc->sc_hw.devs[index].context_num;
2290 /* update number of contexts */
2291 temp = xhci_ctx_get_le32(sc, &pinp->ctx_slot.dwSctx0);
2292 temp &= ~XHCI_SCTX_0_CTX_NUM_SET(31);
2293 temp |= XHCI_SCTX_0_CTX_NUM_SET(x + 1);
2294 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx0, temp);
2296 usb_pc_cpu_flush(&sc->sc_hw.devs[index].input_pc);
2301 xhci_configure_endpoint(struct usb_device *udev,
2302 struct usb_endpoint_descriptor *edesc, uint64_t ring_addr,
2303 uint16_t interval, uint8_t max_packet_count, uint8_t mult,
2304 uint8_t fps_shift, uint16_t max_packet_size, uint16_t max_frame_size)
2306 struct usb_page_search buf_inp;
2307 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2308 struct xhci_input_dev_ctx *pinp;
2314 index = udev->controller_slot_id;
2316 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
2318 pinp = buf_inp.buffer;
2320 epno = edesc->bEndpointAddress;
2321 type = edesc->bmAttributes & UE_XFERTYPE;
2323 if (type == UE_CONTROL)
2326 epno = XHCI_EPNO2EPID(epno);
2329 return (USB_ERR_NO_PIPE); /* invalid */
2331 if (max_packet_count == 0)
2332 return (USB_ERR_BAD_BUFSIZE);
2337 return (USB_ERR_BAD_BUFSIZE);
2339 temp = XHCI_EPCTX_0_EPSTATE_SET(0) |
2340 XHCI_EPCTX_0_MAXP_STREAMS_SET(0) |
2341 XHCI_EPCTX_0_LSA_SET(0);
2343 switch (udev->speed) {
2344 case USB_SPEED_FULL:
2357 temp |= XHCI_EPCTX_0_IVAL_SET(fps_shift);
2359 case UE_ISOCHRONOUS:
2360 temp |= XHCI_EPCTX_0_IVAL_SET(fps_shift);
2362 switch (udev->speed) {
2363 case USB_SPEED_SUPER:
2366 temp |= XHCI_EPCTX_0_MULT_SET(mult - 1);
2367 max_packet_count /= mult;
2377 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx0, temp);
2380 XHCI_EPCTX_1_HID_SET(0) |
2381 XHCI_EPCTX_1_MAXB_SET(max_packet_count) |
2382 XHCI_EPCTX_1_MAXP_SIZE_SET(max_packet_size);
2385 * Always enable the "three strikes and you are gone" feature
2386 * except for ISOCHRONOUS endpoints. This is suggested by
2387 * section 4.3.3 in the XHCI specification about device slot
2390 if (type != UE_ISOCHRONOUS)
2391 temp |= XHCI_EPCTX_1_CERR_SET(3);
2395 temp |= XHCI_EPCTX_1_EPTYPE_SET(4);
2397 case UE_ISOCHRONOUS:
2398 temp |= XHCI_EPCTX_1_EPTYPE_SET(1);
2401 temp |= XHCI_EPCTX_1_EPTYPE_SET(2);
2404 temp |= XHCI_EPCTX_1_EPTYPE_SET(3);
2408 /* check for IN direction */
2410 temp |= XHCI_EPCTX_1_EPTYPE_SET(4);
2412 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx1, temp);
2414 ring_addr |= XHCI_EPCTX_2_DCS_SET(1);
2416 xhci_ctx_set_le64(sc, &pinp->ctx_ep[epno - 1].qwEpCtx2, ring_addr);
2418 switch (edesc->bmAttributes & UE_XFERTYPE) {
2420 case UE_ISOCHRONOUS:
2421 temp = XHCI_EPCTX_4_MAX_ESIT_PAYLOAD_SET(max_frame_size) |
2422 XHCI_EPCTX_4_AVG_TRB_LEN_SET(MIN(XHCI_PAGE_SIZE,
2426 temp = XHCI_EPCTX_4_AVG_TRB_LEN_SET(8);
2429 temp = XHCI_EPCTX_4_AVG_TRB_LEN_SET(XHCI_PAGE_SIZE);
2433 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx4, temp);
2436 xhci_dump_endpoint(sc, &pinp->ctx_ep[epno - 1]);
2438 usb_pc_cpu_flush(&sc->sc_hw.devs[index].input_pc);
2440 return (0); /* success */
2444 xhci_configure_endpoint_by_xfer(struct usb_xfer *xfer)
2446 struct xhci_endpoint_ext *pepext;
2447 struct usb_endpoint_ss_comp_descriptor *ecomp;
2449 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2450 xfer->endpoint->edesc);
2452 ecomp = xfer->endpoint->ecomp;
2454 pepext->trb[0].dwTrb3 = 0; /* halt any transfers */
2455 usb_pc_cpu_flush(pepext->page_cache);
2457 return (xhci_configure_endpoint(xfer->xroot->udev,
2458 xfer->endpoint->edesc, pepext->physaddr,
2459 xfer->interval, xfer->max_packet_count,
2460 (ecomp != NULL) ? (ecomp->bmAttributes & 3) + 1 : 1,
2461 usbd_xfer_get_fps_shift(xfer), xfer->max_packet_size,
2462 xfer->max_frame_size));
2466 xhci_configure_device(struct usb_device *udev)
2468 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2469 struct usb_page_search buf_inp;
2470 struct usb_page_cache *pcinp;
2471 struct xhci_input_dev_ctx *pinp;
2472 struct usb_device *hubdev;
2480 index = udev->controller_slot_id;
2482 DPRINTF("index=%u\n", index);
2484 pcinp = &sc->sc_hw.devs[index].input_pc;
2486 usbd_get_page(pcinp, 0, &buf_inp);
2488 pinp = buf_inp.buffer;
2493 /* figure out route string and root HUB port number */
2495 for (hubdev = udev; hubdev != NULL; hubdev = hubdev->parent_hub) {
2497 if (hubdev->parent_hub == NULL)
2500 depth = hubdev->parent_hub->depth;
2503 * NOTE: HS/FS/LS devices and the SS root HUB can have
2504 * more than 15 ports
2507 rh_port = hubdev->port_no;
2516 route |= rh_port << (4 * (depth - 1));
2519 DPRINTF("Route=0x%08x\n", route);
2521 temp = XHCI_SCTX_0_ROUTE_SET(route) |
2522 XHCI_SCTX_0_CTX_NUM_SET(
2523 sc->sc_hw.devs[index].context_num + 1);
2525 switch (udev->speed) {
2527 temp |= XHCI_SCTX_0_SPEED_SET(2);
2528 if (udev->parent_hs_hub != NULL &&
2529 udev->parent_hs_hub->ddesc.bDeviceProtocol ==
2531 DPRINTF("Device inherits MTT\n");
2532 temp |= XHCI_SCTX_0_MTT_SET(1);
2535 case USB_SPEED_HIGH:
2536 temp |= XHCI_SCTX_0_SPEED_SET(3);
2537 if (sc->sc_hw.devs[index].nports != 0 &&
2538 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) {
2539 DPRINTF("HUB supports MTT\n");
2540 temp |= XHCI_SCTX_0_MTT_SET(1);
2543 case USB_SPEED_FULL:
2544 temp |= XHCI_SCTX_0_SPEED_SET(1);
2545 if (udev->parent_hs_hub != NULL &&
2546 udev->parent_hs_hub->ddesc.bDeviceProtocol ==
2548 DPRINTF("Device inherits MTT\n");
2549 temp |= XHCI_SCTX_0_MTT_SET(1);
2553 temp |= XHCI_SCTX_0_SPEED_SET(4);
2557 is_hub = sc->sc_hw.devs[index].nports != 0 &&
2558 (udev->speed == USB_SPEED_SUPER ||
2559 udev->speed == USB_SPEED_HIGH);
2562 temp |= XHCI_SCTX_0_HUB_SET(1);
2564 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx0, temp);
2566 temp = XHCI_SCTX_1_RH_PORT_SET(rh_port);
2569 temp |= XHCI_SCTX_1_NUM_PORTS_SET(
2570 sc->sc_hw.devs[index].nports);
2573 switch (udev->speed) {
2574 case USB_SPEED_SUPER:
2575 switch (sc->sc_hw.devs[index].state) {
2576 case XHCI_ST_ADDRESSED:
2577 case XHCI_ST_CONFIGURED:
2578 /* enable power save */
2579 temp |= XHCI_SCTX_1_MAX_EL_SET(sc->sc_exit_lat_max);
2582 /* disable power save */
2590 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx1, temp);
2592 temp = XHCI_SCTX_2_IRQ_TARGET_SET(0);
2595 temp |= XHCI_SCTX_2_TT_THINK_TIME_SET(
2596 sc->sc_hw.devs[index].tt);
2599 hubdev = udev->parent_hs_hub;
2601 /* check if we should activate the transaction translator */
2602 switch (udev->speed) {
2603 case USB_SPEED_FULL:
2605 if (hubdev != NULL) {
2606 temp |= XHCI_SCTX_2_TT_HUB_SID_SET(
2607 hubdev->controller_slot_id);
2608 temp |= XHCI_SCTX_2_TT_PORT_NUM_SET(
2616 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx2, temp);
2619 * These fields should be initialized to zero, according to
2620 * XHCI section 6.2.2 - slot context:
2622 temp = XHCI_SCTX_3_DEV_ADDR_SET(0) |
2623 XHCI_SCTX_3_SLOT_STATE_SET(0);
2625 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx3, temp);
2628 xhci_dump_device(sc, &pinp->ctx_slot);
2630 usb_pc_cpu_flush(pcinp);
2632 return (0); /* success */
2636 xhci_alloc_device_ext(struct usb_device *udev)
2638 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2639 struct usb_page_search buf_dev;
2640 struct usb_page_search buf_ep;
2641 struct xhci_trb *trb;
2642 struct usb_page_cache *pc;
2643 struct usb_page *pg;
2648 index = udev->controller_slot_id;
2650 pc = &sc->sc_hw.devs[index].device_pc;
2651 pg = &sc->sc_hw.devs[index].device_pg;
2653 /* need to initialize the page cache */
2654 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2656 if (usb_pc_alloc_mem(pc, pg, sc->sc_ctx_is_64_byte ?
2657 (2 * sizeof(struct xhci_dev_ctx)) :
2658 sizeof(struct xhci_dev_ctx), XHCI_PAGE_SIZE))
2661 usbd_get_page(pc, 0, &buf_dev);
2663 pc = &sc->sc_hw.devs[index].input_pc;
2664 pg = &sc->sc_hw.devs[index].input_pg;
2666 /* need to initialize the page cache */
2667 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2669 if (usb_pc_alloc_mem(pc, pg, sc->sc_ctx_is_64_byte ?
2670 (2 * sizeof(struct xhci_input_dev_ctx)) :
2671 sizeof(struct xhci_input_dev_ctx), XHCI_PAGE_SIZE)) {
2675 pc = &sc->sc_hw.devs[index].endpoint_pc;
2676 pg = &sc->sc_hw.devs[index].endpoint_pg;
2678 /* need to initialize the page cache */
2679 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2681 if (usb_pc_alloc_mem(pc, pg,
2682 sizeof(struct xhci_dev_endpoint_trbs), XHCI_PAGE_SIZE)) {
2686 /* initialise all endpoint LINK TRBs */
2688 for (i = 0; i != XHCI_MAX_ENDPOINTS; i++) {
2690 /* lookup endpoint TRB ring */
2691 usbd_get_page(pc, (uintptr_t)&
2692 ((struct xhci_dev_endpoint_trbs *)0)->trb[i][0], &buf_ep);
2694 /* get TRB pointer */
2695 trb = buf_ep.buffer;
2696 trb += XHCI_MAX_TRANSFERS - 1;
2698 /* get TRB start address */
2699 addr = buf_ep.physaddr;
2701 /* create LINK TRB */
2702 trb->qwTrb0 = htole64(addr);
2703 trb->dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
2704 trb->dwTrb3 = htole32(XHCI_TRB_3_CYCLE_BIT |
2705 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
2708 usb_pc_cpu_flush(pc);
2710 xhci_set_slot_pointer(sc, index, buf_dev.physaddr);
2715 xhci_free_device_ext(udev);
2717 return (USB_ERR_NOMEM);
2721 xhci_free_device_ext(struct usb_device *udev)
2723 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2726 index = udev->controller_slot_id;
2727 xhci_set_slot_pointer(sc, index, 0);
2729 usb_pc_free_mem(&sc->sc_hw.devs[index].device_pc);
2730 usb_pc_free_mem(&sc->sc_hw.devs[index].input_pc);
2731 usb_pc_free_mem(&sc->sc_hw.devs[index].endpoint_pc);
2734 static struct xhci_endpoint_ext *
2735 xhci_get_endpoint_ext(struct usb_device *udev, struct usb_endpoint_descriptor *edesc)
2737 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2738 struct xhci_endpoint_ext *pepext;
2739 struct usb_page_cache *pc;
2740 struct usb_page_search buf_ep;
2744 epno = edesc->bEndpointAddress;
2745 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL)
2748 epno = XHCI_EPNO2EPID(epno);
2750 index = udev->controller_slot_id;
2752 pc = &sc->sc_hw.devs[index].endpoint_pc;
2754 usbd_get_page(pc, (uintptr_t)&((struct xhci_dev_endpoint_trbs *)0)->trb[epno][0], &buf_ep);
2756 pepext = &sc->sc_hw.devs[index].endp[epno];
2757 pepext->page_cache = pc;
2758 pepext->trb = buf_ep.buffer;
2759 pepext->physaddr = buf_ep.physaddr;
2765 xhci_endpoint_doorbell(struct usb_xfer *xfer)
2767 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
2771 epno = xfer->endpointno;
2772 if (xfer->flags_int.control_xfr)
2775 epno = XHCI_EPNO2EPID(epno);
2776 index = xfer->xroot->udev->controller_slot_id;
2778 if (xfer->xroot->udev->flags.self_suspended == 0) {
2779 XWRITE4(sc, door, XHCI_DOORBELL(index),
2780 epno | XHCI_DB_SID_SET(/*xfer->stream_id*/ 0));
2785 xhci_transfer_remove(struct usb_xfer *xfer, usb_error_t error)
2787 struct xhci_endpoint_ext *pepext;
2789 if (xfer->flags_int.bandwidth_reclaimed) {
2790 xfer->flags_int.bandwidth_reclaimed = 0;
2792 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2793 xfer->endpoint->edesc);
2797 pepext->xfer[xfer->qh_pos] = NULL;
2799 if (error && pepext->trb_running != 0) {
2800 pepext->trb_halted = 1;
2801 pepext->trb_running = 0;
2807 xhci_transfer_insert(struct usb_xfer *xfer)
2809 struct xhci_td *td_first;
2810 struct xhci_td *td_last;
2811 struct xhci_trb *trb_link;
2812 struct xhci_endpoint_ext *pepext;
2820 /* check if already inserted */
2821 if (xfer->flags_int.bandwidth_reclaimed) {
2822 DPRINTFN(8, "Already in schedule\n");
2826 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2827 xfer->endpoint->edesc);
2829 td_first = xfer->td_transfer_first;
2830 td_last = xfer->td_transfer_last;
2831 addr = pepext->physaddr;
2833 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
2836 /* single buffered */
2840 /* multi buffered */
2841 trb_limit = (XHCI_MAX_TRANSFERS - 2);
2845 if (pepext->trb_used >= trb_limit) {
2846 DPRINTFN(8, "Too many TDs queued.\n");
2847 return (USB_ERR_NOMEM);
2850 /* check for stopped condition, after putting transfer on interrupt queue */
2851 if (pepext->trb_running == 0) {
2852 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
2854 DPRINTFN(8, "Not running\n");
2856 /* start configuration */
2857 (void)usb_proc_msignal(&sc->sc_config_proc,
2858 &sc->sc_config_msg[0], &sc->sc_config_msg[1]);
2864 /* get current TRB index */
2865 i = pepext->trb_index;
2867 /* get next TRB index */
2870 /* the last entry of the ring is a hardcoded link TRB */
2871 if (inext >= (XHCI_MAX_TRANSFERS - 1))
2874 /* compute terminating return address */
2875 addr += inext * sizeof(struct xhci_trb);
2877 /* compute link TRB pointer */
2878 trb_link = td_last->td_trb + td_last->ntrb;
2880 /* update next pointer of last link TRB */
2881 trb_link->qwTrb0 = htole64(addr);
2882 trb_link->dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
2883 trb_link->dwTrb3 = htole32(XHCI_TRB_3_IOC_BIT |
2884 XHCI_TRB_3_CYCLE_BIT |
2885 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
2888 xhci_dump_trb(&td_last->td_trb[td_last->ntrb]);
2890 usb_pc_cpu_flush(td_last->page_cache);
2892 /* write ahead chain end marker */
2894 pepext->trb[inext].qwTrb0 = 0;
2895 pepext->trb[inext].dwTrb2 = 0;
2896 pepext->trb[inext].dwTrb3 = 0;
2898 /* update next pointer of link TRB */
2900 pepext->trb[i].qwTrb0 = htole64((uint64_t)td_first->td_self);
2901 pepext->trb[i].dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
2904 xhci_dump_trb(&pepext->trb[i]);
2906 usb_pc_cpu_flush(pepext->page_cache);
2908 /* toggle cycle bit which activates the transfer chain */
2910 pepext->trb[i].dwTrb3 = htole32(XHCI_TRB_3_CYCLE_BIT |
2911 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
2913 usb_pc_cpu_flush(pepext->page_cache);
2915 DPRINTF("qh_pos = %u\n", i);
2917 pepext->xfer[i] = xfer;
2921 xfer->flags_int.bandwidth_reclaimed = 1;
2923 pepext->trb_index = inext;
2925 xhci_endpoint_doorbell(xfer);
2931 xhci_root_intr(struct xhci_softc *sc)
2935 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2937 /* clear any old interrupt data */
2938 memset(sc->sc_hub_idata, 0, sizeof(sc->sc_hub_idata));
2940 for (i = 1; i <= sc->sc_noport; i++) {
2941 /* pick out CHANGE bits from the status register */
2942 if (XREAD4(sc, oper, XHCI_PORTSC(i)) & (
2943 XHCI_PS_CSC | XHCI_PS_PEC |
2944 XHCI_PS_OCC | XHCI_PS_WRC |
2945 XHCI_PS_PRC | XHCI_PS_PLC |
2947 sc->sc_hub_idata[i / 8] |= 1 << (i % 8);
2948 DPRINTF("port %d changed\n", i);
2951 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
2952 sizeof(sc->sc_hub_idata));
2955 /*------------------------------------------------------------------------*
2956 * xhci_device_done - XHCI done handler
2958 * NOTE: This function can be called two times in a row on
2959 * the same USB transfer. From close and from interrupt.
2960 *------------------------------------------------------------------------*/
2962 xhci_device_done(struct usb_xfer *xfer, usb_error_t error)
2964 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
2965 xfer, xfer->endpoint, error);
2967 /* remove transfer from HW queue */
2968 xhci_transfer_remove(xfer, error);
2970 /* dequeue transfer and start next transfer */
2971 usbd_transfer_done(xfer, error);
2974 /*------------------------------------------------------------------------*
2975 * XHCI data transfer support (generic type)
2976 *------------------------------------------------------------------------*/
2978 xhci_device_generic_open(struct usb_xfer *xfer)
2980 if (xfer->flags_int.isochronous_xfr) {
2981 switch (xfer->xroot->udev->speed) {
2982 case USB_SPEED_FULL:
2985 usb_hs_bandwidth_alloc(xfer);
2992 xhci_device_generic_close(struct usb_xfer *xfer)
2996 xhci_device_done(xfer, USB_ERR_CANCELLED);
2998 if (xfer->flags_int.isochronous_xfr) {
2999 switch (xfer->xroot->udev->speed) {
3000 case USB_SPEED_FULL:
3003 usb_hs_bandwidth_free(xfer);
3010 xhci_device_generic_multi_enter(struct usb_endpoint *ep,
3011 struct usb_xfer *enter_xfer)
3013 struct usb_xfer *xfer;
3015 /* check if there is a current transfer */
3016 xfer = ep->endpoint_q.curr;
3021 * Check if the current transfer is started and then pickup
3022 * the next one, if any. Else wait for next start event due to
3023 * block on failure feature.
3025 if (!xfer->flags_int.bandwidth_reclaimed)
3028 xfer = TAILQ_FIRST(&ep->endpoint_q.head);
3031 * In case of enter we have to consider that the
3032 * transfer is queued by the USB core after the enter
3041 /* try to multi buffer */
3042 xhci_transfer_insert(xfer);
3046 xhci_device_generic_enter(struct usb_xfer *xfer)
3050 /* set up TD's and QH */
3051 xhci_setup_generic_chain(xfer);
3053 xhci_device_generic_multi_enter(xfer->endpoint, xfer);
3057 xhci_device_generic_start(struct usb_xfer *xfer)
3061 /* try to insert xfer on HW queue */
3062 xhci_transfer_insert(xfer);
3064 /* try to multi buffer */
3065 xhci_device_generic_multi_enter(xfer->endpoint, NULL);
3067 /* add transfer last on interrupt queue */
3068 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
3070 /* start timeout, if any */
3071 if (xfer->timeout != 0)
3072 usbd_transfer_timeout_ms(xfer, &xhci_timeout, xfer->timeout);
3075 struct usb_pipe_methods xhci_device_generic_methods =
3077 .open = xhci_device_generic_open,
3078 .close = xhci_device_generic_close,
3079 .enter = xhci_device_generic_enter,
3080 .start = xhci_device_generic_start,
3083 /*------------------------------------------------------------------------*
3084 * xhci root HUB support
3085 *------------------------------------------------------------------------*
3086 * Simulate a hardware HUB by handling all the necessary requests.
3087 *------------------------------------------------------------------------*/
3089 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
3092 struct usb_device_descriptor xhci_devd =
3094 .bLength = sizeof(xhci_devd),
3095 .bDescriptorType = UDESC_DEVICE, /* type */
3096 HSETW(.bcdUSB, 0x0300), /* USB version */
3097 .bDeviceClass = UDCLASS_HUB, /* class */
3098 .bDeviceSubClass = UDSUBCLASS_HUB, /* subclass */
3099 .bDeviceProtocol = UDPROTO_SSHUB, /* protocol */
3100 .bMaxPacketSize = 9, /* max packet size */
3101 HSETW(.idVendor, 0x0000), /* vendor */
3102 HSETW(.idProduct, 0x0000), /* product */
3103 HSETW(.bcdDevice, 0x0100), /* device version */
3107 .bNumConfigurations = 1, /* # of configurations */
3111 struct xhci_bos_desc xhci_bosd = {
3113 .bLength = sizeof(xhci_bosd.bosd),
3114 .bDescriptorType = UDESC_BOS,
3115 HSETW(.wTotalLength, sizeof(xhci_bosd)),
3116 .bNumDeviceCaps = 3,
3119 .bLength = sizeof(xhci_bosd.usb2extd),
3120 .bDescriptorType = 1,
3121 .bDevCapabilityType = 2,
3122 .bmAttributes[0] = 2,
3125 .bLength = sizeof(xhci_bosd.usbdcd),
3126 .bDescriptorType = UDESC_DEVICE_CAPABILITY,
3127 .bDevCapabilityType = 3,
3128 .bmAttributes = 0, /* XXX */
3129 HSETW(.wSpeedsSupported, 0x000C),
3130 .bFunctionalitySupport = 8,
3131 .bU1DevExitLat = 255, /* dummy - not used */
3132 .wU2DevExitLat = { 0x00, 0x08 },
3135 .bLength = sizeof(xhci_bosd.cidd),
3136 .bDescriptorType = 1,
3137 .bDevCapabilityType = 4,
3139 .bContainerID = 0, /* XXX */
3144 struct xhci_config_desc xhci_confd = {
3146 .bLength = sizeof(xhci_confd.confd),
3147 .bDescriptorType = UDESC_CONFIG,
3148 .wTotalLength[0] = sizeof(xhci_confd),
3150 .bConfigurationValue = 1,
3151 .iConfiguration = 0,
3152 .bmAttributes = UC_SELF_POWERED,
3153 .bMaxPower = 0 /* max power */
3156 .bLength = sizeof(xhci_confd.ifcd),
3157 .bDescriptorType = UDESC_INTERFACE,
3159 .bInterfaceClass = UICLASS_HUB,
3160 .bInterfaceSubClass = UISUBCLASS_HUB,
3161 .bInterfaceProtocol = 0,
3164 .bLength = sizeof(xhci_confd.endpd),
3165 .bDescriptorType = UDESC_ENDPOINT,
3166 .bEndpointAddress = UE_DIR_IN | XHCI_INTR_ENDPT,
3167 .bmAttributes = UE_INTERRUPT,
3168 .wMaxPacketSize[0] = 2, /* max 15 ports */
3172 .bLength = sizeof(xhci_confd.endpcd),
3173 .bDescriptorType = UDESC_ENDPOINT_SS_COMP,
3180 struct usb_hub_ss_descriptor xhci_hubd = {
3181 .bLength = sizeof(xhci_hubd),
3182 .bDescriptorType = UDESC_SS_HUB,
3186 xhci_roothub_exec(struct usb_device *udev,
3187 struct usb_device_request *req, const void **pptr, uint16_t *plength)
3189 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3190 const char *str_ptr;
3201 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
3204 ptr = (const void *)&sc->sc_hub_desc;
3208 value = UGETW(req->wValue);
3209 index = UGETW(req->wIndex);
3211 DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
3212 "wValue=0x%04x wIndex=0x%04x\n",
3213 req->bmRequestType, req->bRequest,
3214 UGETW(req->wLength), value, index);
3216 #define C(x,y) ((x) | ((y) << 8))
3217 switch (C(req->bRequest, req->bmRequestType)) {
3218 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
3219 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
3220 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
3222 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
3223 * for the integrated root hub.
3226 case C(UR_GET_CONFIG, UT_READ_DEVICE):
3228 sc->sc_hub_desc.temp[0] = sc->sc_conf;
3230 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
3231 switch (value >> 8) {
3233 if ((value & 0xff) != 0) {
3234 err = USB_ERR_IOERROR;
3237 len = sizeof(xhci_devd);
3238 ptr = (const void *)&xhci_devd;
3242 if ((value & 0xff) != 0) {
3243 err = USB_ERR_IOERROR;
3246 len = sizeof(xhci_bosd);
3247 ptr = (const void *)&xhci_bosd;
3251 if ((value & 0xff) != 0) {
3252 err = USB_ERR_IOERROR;
3255 len = sizeof(xhci_confd);
3256 ptr = (const void *)&xhci_confd;
3260 switch (value & 0xff) {
3261 case 0: /* Language table */
3265 case 1: /* Vendor */
3266 str_ptr = sc->sc_vendor;
3269 case 2: /* Product */
3270 str_ptr = "XHCI root HUB";
3278 len = usb_make_str_desc(
3279 sc->sc_hub_desc.temp,
3280 sizeof(sc->sc_hub_desc.temp),
3285 err = USB_ERR_IOERROR;
3289 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3291 sc->sc_hub_desc.temp[0] = 0;
3293 case C(UR_GET_STATUS, UT_READ_DEVICE):
3295 USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
3297 case C(UR_GET_STATUS, UT_READ_INTERFACE):
3298 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3300 USETW(sc->sc_hub_desc.stat.wStatus, 0);
3302 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3303 if (value >= XHCI_MAX_DEVICES) {
3304 err = USB_ERR_IOERROR;
3308 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
3309 if (value != 0 && value != 1) {
3310 err = USB_ERR_IOERROR;
3313 sc->sc_conf = value;
3315 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
3317 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
3318 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
3319 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3320 err = USB_ERR_IOERROR;
3322 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
3324 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
3327 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3329 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3330 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE\n");
3333 (index > sc->sc_noport)) {
3334 err = USB_ERR_IOERROR;
3337 port = XHCI_PORTSC(index);
3339 v = XREAD4(sc, oper, port);
3340 i = XHCI_PS_PLS_GET(v);
3341 v &= ~XHCI_PS_CLEAR;
3344 case UHF_C_BH_PORT_RESET:
3345 XWRITE4(sc, oper, port, v | XHCI_PS_WRC);
3347 case UHF_C_PORT_CONFIG_ERROR:
3348 XWRITE4(sc, oper, port, v | XHCI_PS_CEC);
3350 case UHF_C_PORT_SUSPEND:
3351 case UHF_C_PORT_LINK_STATE:
3352 XWRITE4(sc, oper, port, v | XHCI_PS_PLC);
3354 case UHF_C_PORT_CONNECTION:
3355 XWRITE4(sc, oper, port, v | XHCI_PS_CSC);
3357 case UHF_C_PORT_ENABLE:
3358 XWRITE4(sc, oper, port, v | XHCI_PS_PEC);
3360 case UHF_C_PORT_OVER_CURRENT:
3361 XWRITE4(sc, oper, port, v | XHCI_PS_OCC);
3363 case UHF_C_PORT_RESET:
3364 XWRITE4(sc, oper, port, v | XHCI_PS_PRC);
3366 case UHF_PORT_ENABLE:
3367 XWRITE4(sc, oper, port, v | XHCI_PS_PED);
3369 case UHF_PORT_POWER:
3370 XWRITE4(sc, oper, port, v & ~XHCI_PS_PP);
3372 case UHF_PORT_INDICATOR:
3373 XWRITE4(sc, oper, port, v & ~XHCI_PS_PIC_SET(3));
3375 case UHF_PORT_SUSPEND:
3379 XWRITE4(sc, oper, port, v |
3380 XHCI_PS_PLS_SET(0xF) | XHCI_PS_LWS);
3383 /* wait 20ms for resume sequence to complete */
3384 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
3387 XWRITE4(sc, oper, port, v |
3388 XHCI_PS_PLS_SET(0) | XHCI_PS_LWS);
3391 err = USB_ERR_IOERROR;
3396 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3397 if ((value & 0xff) != 0) {
3398 err = USB_ERR_IOERROR;
3402 v = XREAD4(sc, capa, XHCI_HCSPARAMS0);
3404 sc->sc_hub_desc.hubd = xhci_hubd;
3406 sc->sc_hub_desc.hubd.bNbrPorts = sc->sc_noport;
3408 if (XHCI_HCS0_PPC(v))
3409 i = UHD_PWR_INDIVIDUAL;
3413 if (XHCI_HCS0_PIND(v))
3416 i |= UHD_OC_INDIVIDUAL;
3418 USETW(sc->sc_hub_desc.hubd.wHubCharacteristics, i);
3420 /* see XHCI section 5.4.9: */
3421 sc->sc_hub_desc.hubd.bPwrOn2PwrGood = 10;
3423 for (j = 1; j <= sc->sc_noport; j++) {
3425 v = XREAD4(sc, oper, XHCI_PORTSC(j));
3426 if (v & XHCI_PS_DR) {
3427 sc->sc_hub_desc.hubd.
3428 DeviceRemovable[j / 8] |= 1U << (j % 8);
3431 len = sc->sc_hub_desc.hubd.bLength;
3434 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3436 memset(sc->sc_hub_desc.temp, 0, 16);
3439 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3440 DPRINTFN(9, "UR_GET_STATUS i=%d\n", index);
3443 (index > sc->sc_noport)) {
3444 err = USB_ERR_IOERROR;
3448 v = XREAD4(sc, oper, XHCI_PORTSC(index));
3450 DPRINTFN(9, "port status=0x%08x\n", v);
3452 i = UPS_PORT_LINK_STATE_SET(XHCI_PS_PLS_GET(v));
3454 switch (XHCI_PS_SPEED_GET(v)) {
3456 i |= UPS_HIGH_SPEED;
3465 i |= UPS_OTHER_SPEED;
3469 if (v & XHCI_PS_CCS)
3470 i |= UPS_CURRENT_CONNECT_STATUS;
3471 if (v & XHCI_PS_PED)
3472 i |= UPS_PORT_ENABLED;
3473 if (v & XHCI_PS_OCA)
3474 i |= UPS_OVERCURRENT_INDICATOR;
3477 if (v & XHCI_PS_PP) {
3479 * The USB 3.0 RH is using the
3480 * USB 2.0's power bit
3482 i |= UPS_PORT_POWER;
3484 USETW(sc->sc_hub_desc.ps.wPortStatus, i);
3487 if (v & XHCI_PS_CSC)
3488 i |= UPS_C_CONNECT_STATUS;
3489 if (v & XHCI_PS_PEC)
3490 i |= UPS_C_PORT_ENABLED;
3491 if (v & XHCI_PS_OCC)
3492 i |= UPS_C_OVERCURRENT_INDICATOR;
3493 if (v & XHCI_PS_WRC)
3494 i |= UPS_C_BH_PORT_RESET;
3495 if (v & XHCI_PS_PRC)
3496 i |= UPS_C_PORT_RESET;
3497 if (v & XHCI_PS_PLC)
3498 i |= UPS_C_PORT_LINK_STATE;
3499 if (v & XHCI_PS_CEC)
3500 i |= UPS_C_PORT_CONFIG_ERROR;
3502 USETW(sc->sc_hub_desc.ps.wPortChange, i);
3503 len = sizeof(sc->sc_hub_desc.ps);
3506 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3507 err = USB_ERR_IOERROR;
3510 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3513 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3519 (index > sc->sc_noport)) {
3520 err = USB_ERR_IOERROR;
3524 port = XHCI_PORTSC(index);
3525 v = XREAD4(sc, oper, port) & ~XHCI_PS_CLEAR;
3528 case UHF_PORT_U1_TIMEOUT:
3529 if (XHCI_PS_SPEED_GET(v) != 4) {
3530 err = USB_ERR_IOERROR;
3533 port = XHCI_PORTPMSC(index);
3534 v = XREAD4(sc, oper, port);
3535 v &= ~XHCI_PM3_U1TO_SET(0xFF);
3536 v |= XHCI_PM3_U1TO_SET(i);
3537 XWRITE4(sc, oper, port, v);
3539 case UHF_PORT_U2_TIMEOUT:
3540 if (XHCI_PS_SPEED_GET(v) != 4) {
3541 err = USB_ERR_IOERROR;
3544 port = XHCI_PORTPMSC(index);
3545 v = XREAD4(sc, oper, port);
3546 v &= ~XHCI_PM3_U2TO_SET(0xFF);
3547 v |= XHCI_PM3_U2TO_SET(i);
3548 XWRITE4(sc, oper, port, v);
3550 case UHF_BH_PORT_RESET:
3551 XWRITE4(sc, oper, port, v | XHCI_PS_WPR);
3553 case UHF_PORT_LINK_STATE:
3554 XWRITE4(sc, oper, port, v |
3555 XHCI_PS_PLS_SET(i) | XHCI_PS_LWS);
3556 /* 4ms settle time */
3557 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
3559 case UHF_PORT_ENABLE:
3560 DPRINTFN(3, "set port enable %d\n", index);
3562 case UHF_PORT_SUSPEND:
3563 DPRINTFN(6, "suspend port %u (LPM=%u)\n", index, i);
3564 j = XHCI_PS_SPEED_GET(v);
3565 if ((j < 1) || (j > 3)) {
3566 /* non-supported speed */
3567 err = USB_ERR_IOERROR;
3570 XWRITE4(sc, oper, port, v |
3571 XHCI_PS_PLS_SET(i ? 2 /* LPM */ : 3) | XHCI_PS_LWS);
3573 case UHF_PORT_RESET:
3574 DPRINTFN(6, "reset port %d\n", index);
3575 XWRITE4(sc, oper, port, v | XHCI_PS_PR);
3577 case UHF_PORT_POWER:
3578 DPRINTFN(3, "set port power %d\n", index);
3579 XWRITE4(sc, oper, port, v | XHCI_PS_PP);
3582 DPRINTFN(3, "set port test %d\n", index);
3584 case UHF_PORT_INDICATOR:
3585 DPRINTFN(3, "set port indicator %d\n", index);
3587 v &= ~XHCI_PS_PIC_SET(3);
3588 v |= XHCI_PS_PIC_SET(1);
3590 XWRITE4(sc, oper, port, v);
3593 err = USB_ERR_IOERROR;
3598 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
3599 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
3600 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
3601 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
3604 err = USB_ERR_IOERROR;
3614 xhci_xfer_setup(struct usb_setup_params *parm)
3616 struct usb_page_search page_info;
3617 struct usb_page_cache *pc;
3618 struct xhci_softc *sc;
3619 struct usb_xfer *xfer;
3624 sc = XHCI_BUS2SC(parm->udev->bus);
3625 xfer = parm->curr_xfer;
3628 * The proof for the "ntd" formula is illustrated like this:
3630 * +------------------------------------+
3634 * | | xxx | x | frm 0 |
3636 * | | xxx | xx | frm 1 |
3639 * +------------------------------------+
3641 * "xxx" means a completely full USB transfer descriptor
3643 * "x" and "xx" means a short USB packet
3645 * For the remainder of an USB transfer modulo
3646 * "max_data_length" we need two USB transfer descriptors.
3647 * One to transfer the remaining data and one to finalise with
3648 * a zero length packet in case the "force_short_xfer" flag is
3649 * set. We only need two USB transfer descriptors in the case
3650 * where the transfer length of the first one is a factor of
3651 * "max_frame_size". The rest of the needed USB transfer
3652 * descriptors is given by the buffer size divided by the
3653 * maximum data payload.
3655 parm->hc_max_packet_size = 0x400;
3656 parm->hc_max_packet_count = 16 * 3;
3657 parm->hc_max_frame_size = XHCI_TD_PAYLOAD_MAX;
3659 xfer->flags_int.bdma_enable = 1;
3661 usbd_transfer_setup_sub(parm);
3663 if (xfer->flags_int.isochronous_xfr) {
3664 ntd = ((1 * xfer->nframes)
3665 + (xfer->max_data_length / xfer->max_hc_frame_size));
3666 } else if (xfer->flags_int.control_xfr) {
3667 ntd = ((2 * xfer->nframes) + 1 /* STATUS */
3668 + (xfer->max_data_length / xfer->max_hc_frame_size));
3670 ntd = ((2 * xfer->nframes)
3671 + (xfer->max_data_length / xfer->max_hc_frame_size));
3680 * Allocate queue heads and transfer descriptors
3684 if (usbd_transfer_setup_sub_malloc(
3685 parm, &pc, sizeof(struct xhci_td),
3686 XHCI_TD_ALIGN, ntd)) {
3687 parm->err = USB_ERR_NOMEM;
3691 for (n = 0; n != ntd; n++) {
3694 usbd_get_page(pc + n, 0, &page_info);
3696 td = page_info.buffer;
3699 td->td_self = page_info.physaddr;
3700 td->obj_next = last_obj;
3701 td->page_cache = pc + n;
3705 usb_pc_cpu_flush(pc + n);
3708 xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
3710 if (!xfer->flags_int.curr_dma_set) {
3711 xfer->flags_int.curr_dma_set = 1;
3717 xhci_configure_reset_endpoint(struct usb_xfer *xfer)
3719 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
3720 struct usb_page_search buf_inp;
3721 struct usb_device *udev;
3722 struct xhci_endpoint_ext *pepext;
3723 struct usb_endpoint_descriptor *edesc;
3724 struct usb_page_cache *pcinp;
3729 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
3730 xfer->endpoint->edesc);
3732 udev = xfer->xroot->udev;
3733 index = udev->controller_slot_id;
3735 pcinp = &sc->sc_hw.devs[index].input_pc;
3737 usbd_get_page(pcinp, 0, &buf_inp);
3739 edesc = xfer->endpoint->edesc;
3741 epno = edesc->bEndpointAddress;
3743 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL)
3746 epno = XHCI_EPNO2EPID(epno);
3749 return (USB_ERR_NO_PIPE); /* invalid */
3753 /* configure endpoint */
3755 err = xhci_configure_endpoint_by_xfer(xfer);
3758 XHCI_CMD_UNLOCK(sc);
3763 * Get the endpoint into the stopped state according to the
3764 * endpoint context state diagram in the XHCI specification:
3767 err = xhci_cmd_stop_ep(sc, 0, epno, index);
3770 DPRINTF("Could not stop endpoint %u\n", epno);
3772 err = xhci_cmd_reset_ep(sc, 0, epno, index);
3775 DPRINTF("Could not reset endpoint %u\n", epno);
3777 err = xhci_cmd_set_tr_dequeue_ptr(sc, pepext->physaddr |
3778 XHCI_EPCTX_2_DCS_SET(1), 0, epno, index);
3781 DPRINTF("Could not set dequeue ptr for endpoint %u\n", epno);
3784 * Get the endpoint into the running state according to the
3785 * endpoint context state diagram in the XHCI specification:
3788 xhci_configure_mask(udev, (1U << epno) | 1U, 0);
3790 err = xhci_cmd_evaluate_ctx(sc, buf_inp.physaddr, index);
3793 DPRINTF("Could not configure endpoint %u\n", epno);
3795 err = xhci_cmd_configure_ep(sc, buf_inp.physaddr, 0, index);
3798 DPRINTF("Could not configure endpoint %u\n", epno);
3800 XHCI_CMD_UNLOCK(sc);
3806 xhci_xfer_unsetup(struct usb_xfer *xfer)
3812 xhci_start_dma_delay(struct usb_xfer *xfer)
3814 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
3816 /* put transfer on interrupt queue (again) */
3817 usbd_transfer_enqueue(&sc->sc_bus.intr_q, xfer);
3819 (void)usb_proc_msignal(&sc->sc_config_proc,
3820 &sc->sc_config_msg[0], &sc->sc_config_msg[1]);
3824 xhci_configure_msg(struct usb_proc_msg *pm)
3826 struct xhci_softc *sc;
3827 struct xhci_endpoint_ext *pepext;
3828 struct usb_xfer *xfer;
3830 sc = XHCI_BUS2SC(((struct usb_bus_msg *)pm)->bus);
3833 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3835 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
3836 xfer->endpoint->edesc);
3838 if ((pepext->trb_halted != 0) ||
3839 (pepext->trb_running == 0)) {
3843 /* clear halted and running */
3844 pepext->trb_halted = 0;
3845 pepext->trb_running = 0;
3847 /* nuke remaining buffered transfers */
3849 for (i = 0; i != (XHCI_MAX_TRANSFERS - 1); i++) {
3851 * NOTE: We need to use the timeout
3852 * error code here else existing
3853 * isochronous clients can get
3856 if (pepext->xfer[i] != NULL) {
3857 xhci_device_done(pepext->xfer[i],
3863 * NOTE: The USB transfer cannot vanish in
3867 USB_BUS_UNLOCK(&sc->sc_bus);
3869 xhci_configure_reset_endpoint(xfer);
3871 USB_BUS_LOCK(&sc->sc_bus);
3873 /* check if halted is still cleared */
3874 if (pepext->trb_halted == 0) {
3875 pepext->trb_running = 1;
3876 pepext->trb_index = 0;
3881 if (xfer->flags_int.did_dma_delay) {
3883 /* remove transfer from interrupt queue (again) */
3884 usbd_transfer_dequeue(xfer);
3886 /* we are finally done */
3887 usb_dma_delay_done_cb(xfer);
3889 /* queue changed - restart */
3894 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3896 /* try to insert xfer on HW queue */
3897 xhci_transfer_insert(xfer);
3899 /* try to multi buffer */
3900 xhci_device_generic_multi_enter(xfer->endpoint, NULL);
3905 xhci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
3906 struct usb_endpoint *ep)
3908 struct xhci_endpoint_ext *pepext;
3910 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d\n",
3911 ep, udev->address, edesc->bEndpointAddress, udev->flags.usb_mode);
3913 if (udev->parent_hub == NULL) {
3914 /* root HUB has special endpoint handling */
3918 ep->methods = &xhci_device_generic_methods;
3920 pepext = xhci_get_endpoint_ext(udev, edesc);
3922 USB_BUS_LOCK(udev->bus);
3923 pepext->trb_halted = 1;
3924 pepext->trb_running = 0;
3925 USB_BUS_UNLOCK(udev->bus);
3929 xhci_ep_uninit(struct usb_device *udev, struct usb_endpoint *ep)
3935 xhci_ep_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
3937 struct xhci_endpoint_ext *pepext;
3941 if (udev->flags.usb_mode != USB_MODE_HOST) {
3945 if (udev->parent_hub == NULL) {
3946 /* root HUB has special endpoint handling */
3950 pepext = xhci_get_endpoint_ext(udev, ep->edesc);
3952 USB_BUS_LOCK(udev->bus);
3953 pepext->trb_halted = 1;
3954 pepext->trb_running = 0;
3955 USB_BUS_UNLOCK(udev->bus);
3959 xhci_device_init(struct usb_device *udev)
3961 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3965 /* no init for root HUB */
3966 if (udev->parent_hub == NULL)
3971 /* set invalid default */
3973 udev->controller_slot_id = sc->sc_noslot + 1;
3975 /* try to get a new slot ID from the XHCI */
3977 err = xhci_cmd_enable_slot(sc, &temp);
3980 XHCI_CMD_UNLOCK(sc);
3984 if (temp > sc->sc_noslot) {
3985 XHCI_CMD_UNLOCK(sc);
3986 return (USB_ERR_BAD_ADDRESS);
3989 if (sc->sc_hw.devs[temp].state != XHCI_ST_DISABLED) {
3990 DPRINTF("slot %u already allocated.\n", temp);
3991 XHCI_CMD_UNLOCK(sc);
3992 return (USB_ERR_BAD_ADDRESS);
3995 /* store slot ID for later reference */
3997 udev->controller_slot_id = temp;
3999 /* reset data structure */
4001 memset(&sc->sc_hw.devs[temp], 0, sizeof(sc->sc_hw.devs[0]));
4003 /* set mark slot allocated */
4005 sc->sc_hw.devs[temp].state = XHCI_ST_ENABLED;
4007 err = xhci_alloc_device_ext(udev);
4009 XHCI_CMD_UNLOCK(sc);
4011 /* get device into default state */
4014 err = xhci_set_address(udev, NULL, 0);
4020 xhci_device_uninit(struct usb_device *udev)
4022 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
4025 /* no init for root HUB */
4026 if (udev->parent_hub == NULL)
4031 index = udev->controller_slot_id;
4033 if (index <= sc->sc_noslot) {
4034 xhci_cmd_disable_slot(sc, index);
4035 sc->sc_hw.devs[index].state = XHCI_ST_DISABLED;
4037 /* free device extension */
4038 xhci_free_device_ext(udev);
4041 XHCI_CMD_UNLOCK(sc);
4045 xhci_get_dma_delay(struct usb_device *udev, uint32_t *pus)
4048 * Wait until the hardware has finished any possible use of
4049 * the transfer descriptor(s)
4051 *pus = 2048; /* microseconds */
4055 xhci_device_resume(struct usb_device *udev)
4057 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
4064 /* check for root HUB */
4065 if (udev->parent_hub == NULL)
4068 index = udev->controller_slot_id;
4072 /* blindly resume all endpoints */
4074 USB_BUS_LOCK(udev->bus);
4076 for (n = 1; n != XHCI_MAX_ENDPOINTS; n++) {
4077 for (p = 0; p != 1 /*XHCI_MAX_STREAMS*/; p++) {
4078 XWRITE4(sc, door, XHCI_DOORBELL(index),
4079 n | XHCI_DB_SID_SET(p));
4083 USB_BUS_UNLOCK(udev->bus);
4085 XHCI_CMD_UNLOCK(sc);
4089 xhci_device_suspend(struct usb_device *udev)
4091 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
4098 /* check for root HUB */
4099 if (udev->parent_hub == NULL)
4102 index = udev->controller_slot_id;
4106 /* blindly suspend all endpoints */
4108 for (n = 1; n != XHCI_MAX_ENDPOINTS; n++) {
4109 err = xhci_cmd_stop_ep(sc, 1, n, index);
4111 DPRINTF("Failed to suspend endpoint "
4112 "%u on slot %u (ignored).\n", n, index);
4116 XHCI_CMD_UNLOCK(sc);
4120 xhci_set_hw_power(struct usb_bus *bus)
4126 xhci_device_state_change(struct usb_device *udev)
4128 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
4129 struct usb_page_search buf_inp;
4133 /* check for root HUB */
4134 if (udev->parent_hub == NULL)
4137 index = udev->controller_slot_id;
4141 if (usb_get_device_state(udev) == USB_STATE_CONFIGURED) {
4142 err = uhub_query_info(udev, &sc->sc_hw.devs[index].nports,
4143 &sc->sc_hw.devs[index].tt);
4145 sc->sc_hw.devs[index].nports = 0;
4150 switch (usb_get_device_state(udev)) {
4151 case USB_STATE_POWERED:
4152 if (sc->sc_hw.devs[index].state == XHCI_ST_DEFAULT)
4155 /* set default state */
4156 sc->sc_hw.devs[index].state = XHCI_ST_DEFAULT;
4158 /* reset number of contexts */
4159 sc->sc_hw.devs[index].context_num = 0;
4161 err = xhci_cmd_reset_dev(sc, index);
4164 DPRINTF("Device reset failed "
4165 "for slot %u.\n", index);
4169 case USB_STATE_ADDRESSED:
4170 if (sc->sc_hw.devs[index].state == XHCI_ST_ADDRESSED)
4173 sc->sc_hw.devs[index].state = XHCI_ST_ADDRESSED;
4175 err = xhci_cmd_configure_ep(sc, 0, 1, index);
4178 DPRINTF("Failed to deconfigure "
4179 "slot %u.\n", index);
4183 case USB_STATE_CONFIGURED:
4184 if (sc->sc_hw.devs[index].state == XHCI_ST_CONFIGURED)
4187 /* set configured state */
4188 sc->sc_hw.devs[index].state = XHCI_ST_CONFIGURED;
4190 /* reset number of contexts */
4191 sc->sc_hw.devs[index].context_num = 0;
4193 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
4195 xhci_configure_mask(udev, 3, 0);
4197 err = xhci_configure_device(udev);
4199 DPRINTF("Could not configure device "
4200 "at slot %u.\n", index);
4203 err = xhci_cmd_evaluate_ctx(sc, buf_inp.physaddr, index);
4205 DPRINTF("Could not evaluate device "
4206 "context at slot %u.\n", index);
4213 XHCI_CMD_UNLOCK(sc);
4216 struct usb_bus_methods xhci_bus_methods = {
4217 .endpoint_init = xhci_ep_init,
4218 .endpoint_uninit = xhci_ep_uninit,
4219 .xfer_setup = xhci_xfer_setup,
4220 .xfer_unsetup = xhci_xfer_unsetup,
4221 .get_dma_delay = xhci_get_dma_delay,
4222 .device_init = xhci_device_init,
4223 .device_uninit = xhci_device_uninit,
4224 .device_resume = xhci_device_resume,
4225 .device_suspend = xhci_device_suspend,
4226 .set_hw_power = xhci_set_hw_power,
4227 .roothub_exec = xhci_roothub_exec,
4228 .xfer_poll = xhci_do_poll,
4229 .start_dma_delay = xhci_start_dma_delay,
4230 .set_address = xhci_set_address,
4231 .clear_stall = xhci_ep_clear_stall,
4232 .device_state_change = xhci_device_state_change,
4233 .set_hw_power_sleep = xhci_set_hw_power_sleep,