1 /* $FreeBSD: head/sys/dev/usb/controller/xhci.c 278477 2015-02-09 21:47:12Z hselasky $ */
3 * Copyright (c) 2010 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * USB eXtensible Host Controller Interface, a.k.a. USB 3.0 controller.
30 * The XHCI 1.0 spec can be found at
31 * http://www.intel.com/technology/usb/download/xHCI_Specification_for_USB.pdf
32 * and the USB 3.0 spec at
33 * http://www.usb.org/developers/docs/usb_30_spec_060910.zip
37 * A few words about the design implementation: This driver emulates
38 * the concept about TDs which is found in EHCI specification. This
39 * way we achieve that the USB controller drivers look similar to
40 * eachother which makes it easier to understand the code.
43 #include <sys/stdint.h>
44 #include <sys/param.h>
45 #include <sys/queue.h>
46 #include <sys/types.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
50 #include <sys/module.h>
52 #include <sys/condvar.h>
53 #include <sys/sysctl.h>
54 #include <sys/unistd.h>
55 #include <sys/callout.h>
56 #include <sys/malloc.h>
59 #include <bus/u4b/usb.h>
60 #include <bus/u4b/usbdi.h>
62 #define USB_DEBUG_VAR xhcidebug
64 #include <bus/u4b/usb_core.h>
65 #include <bus/u4b/usb_debug.h>
66 #include <bus/u4b/usb_busdma.h>
67 #include <bus/u4b/usb_process.h>
68 #include <bus/u4b/usb_transfer.h>
69 #include <bus/u4b/usb_device.h>
70 #include <bus/u4b/usb_hub.h>
71 #include <bus/u4b/usb_util.h>
73 #include <bus/u4b/usb_controller.h>
74 #include <bus/u4b/usb_bus.h>
75 #include <bus/u4b/controller/xhci.h>
76 #include <bus/u4b/controller/xhcireg.h>
78 #define XHCI_BUS2SC(bus) \
79 ((struct xhci_softc *)(((uint8_t *)(bus)) - \
80 ((uint8_t *)&(((struct xhci_softc *)0)->sc_bus))))
82 static SYSCTL_NODE(_hw_usb, OID_AUTO, xhci, CTLFLAG_RW, 0, "USB XHCI");
84 static int xhcistreams;
85 SYSCTL_INT(_hw_usb_xhci, OID_AUTO, streams, CTLFLAG_RW,
86 &xhcistreams, 0, "Set to enable streams mode support");
87 TUNABLE_INT("hw.usb.xhci.streams", &xhcistreams);
91 static int xhcidebug = 0;
92 static int xhciroute = 0;
93 static int xhcipolling = 0;
95 SYSCTL_INT(_hw_usb_xhci, OID_AUTO, debug, CTLFLAG_RW,
96 &xhcidebug, 0, "Debug level");
97 TUNABLE_INT("hw.usb.xhci.debug", &xhcidebug);
98 SYSCTL_INT(_hw_usb_xhci, OID_AUTO, xhci_port_route, CTLFLAG_RW,
99 &xhciroute, 0, "Routing bitmap for switching EHCI ports to XHCI controller");
100 TUNABLE_INT("hw.usb.xhci.xhci_port_route", &xhciroute);
101 SYSCTL_INT(_hw_usb_xhci, OID_AUTO, use_polling, CTLFLAG_RW,
102 &xhcipolling, 0, "Set to enable software interrupt polling for XHCI controller");
103 TUNABLE_INT("hw.usb.xhci.use_polling", &xhcipolling);
108 #define XHCI_INTR_ENDPT 1
110 struct xhci_std_temp {
111 struct xhci_softc *sc;
112 struct usb_page_cache *pc;
114 struct xhci_td *td_next;
117 uint32_t max_packet_size;
129 uint8_t do_isoc_sync;
132 static void xhci_do_poll(struct usb_bus *);
133 static void xhci_device_done(struct usb_xfer *, usb_error_t);
134 static void xhci_root_intr(struct xhci_softc *);
135 static void xhci_free_device_ext(struct usb_device *);
136 static struct xhci_endpoint_ext *xhci_get_endpoint_ext(struct usb_device *,
137 struct usb_endpoint_descriptor *);
138 static usb_proc_callback_t xhci_configure_msg;
139 static usb_error_t xhci_configure_device(struct usb_device *);
140 static usb_error_t xhci_configure_endpoint(struct usb_device *,
141 struct usb_endpoint_descriptor *, struct xhci_endpoint_ext *,
142 uint16_t, uint8_t, uint8_t, uint8_t, uint16_t, uint16_t,
144 static usb_error_t xhci_configure_mask(struct usb_device *,
146 static usb_error_t xhci_cmd_evaluate_ctx(struct xhci_softc *,
148 static void xhci_endpoint_doorbell(struct usb_xfer *);
149 static void xhci_ctx_set_le32(struct xhci_softc *sc, volatile uint32_t *ptr, uint32_t val);
150 static uint32_t xhci_ctx_get_le32(struct xhci_softc *sc, volatile uint32_t *ptr);
151 static void xhci_ctx_set_le64(struct xhci_softc *sc, volatile uint64_t *ptr, uint64_t val);
153 static uint64_t xhci_ctx_get_le64(struct xhci_softc *sc, volatile uint64_t *ptr);
156 static const struct usb_bus_methods xhci_bus_methods;
160 xhci_dump_trb(struct xhci_trb *trb)
162 DPRINTFN(5, "trb = %p\n", trb);
163 DPRINTFN(5, "qwTrb0 = 0x%016llx\n", (long long)le64toh(trb->qwTrb0));
164 DPRINTFN(5, "dwTrb2 = 0x%08x\n", le32toh(trb->dwTrb2));
165 DPRINTFN(5, "dwTrb3 = 0x%08x\n", le32toh(trb->dwTrb3));
169 xhci_dump_endpoint(struct xhci_softc *sc, struct xhci_endp_ctx *pep)
171 DPRINTFN(5, "pep = %p\n", pep);
172 DPRINTFN(5, "dwEpCtx0=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx0));
173 DPRINTFN(5, "dwEpCtx1=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx1));
174 DPRINTFN(5, "qwEpCtx2=0x%016llx\n", (long long)xhci_ctx_get_le64(sc, &pep->qwEpCtx2));
175 DPRINTFN(5, "dwEpCtx4=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx4));
176 DPRINTFN(5, "dwEpCtx5=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx5));
177 DPRINTFN(5, "dwEpCtx6=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx6));
178 DPRINTFN(5, "dwEpCtx7=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx7));
182 xhci_dump_device(struct xhci_softc *sc, struct xhci_slot_ctx *psl)
184 DPRINTFN(5, "psl = %p\n", psl);
185 DPRINTFN(5, "dwSctx0=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx0));
186 DPRINTFN(5, "dwSctx1=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx1));
187 DPRINTFN(5, "dwSctx2=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx2));
188 DPRINTFN(5, "dwSctx3=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx3));
193 xhci_use_polling(void)
196 return (xhcipolling != 0);
203 xhci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
205 struct xhci_softc *sc = XHCI_BUS2SC(bus);
208 cb(bus, &sc->sc_hw.root_pc, &sc->sc_hw.root_pg,
209 sizeof(struct xhci_hw_root), XHCI_PAGE_SIZE);
211 cb(bus, &sc->sc_hw.ctx_pc, &sc->sc_hw.ctx_pg,
212 sizeof(struct xhci_dev_ctx_addr), XHCI_PAGE_SIZE);
214 for (i = 0; i != XHCI_MAX_SCRATCHPADS; i++) {
215 cb(bus, &sc->sc_hw.scratch_pc[i], &sc->sc_hw.scratch_pg[i],
216 XHCI_PAGE_SIZE, XHCI_PAGE_SIZE);
221 xhci_ctx_set_le32(struct xhci_softc *sc, volatile uint32_t *ptr, uint32_t val)
223 if (sc->sc_ctx_is_64_byte) {
225 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
226 /* all contexts are initially 32-bytes */
227 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
228 ptr = (volatile uint32_t *)(((volatile uint8_t *)ptr) + offset);
234 xhci_ctx_get_le32(struct xhci_softc *sc, volatile uint32_t *ptr)
236 if (sc->sc_ctx_is_64_byte) {
238 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
239 /* all contexts are initially 32-bytes */
240 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
241 ptr = (volatile uint32_t *)(((volatile uint8_t *)ptr) + offset);
243 return (le32toh(*ptr));
247 xhci_ctx_set_le64(struct xhci_softc *sc, volatile uint64_t *ptr, uint64_t val)
249 if (sc->sc_ctx_is_64_byte) {
251 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
252 /* all contexts are initially 32-bytes */
253 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
254 ptr = (volatile uint64_t *)(((volatile uint8_t *)ptr) + offset);
261 xhci_ctx_get_le64(struct xhci_softc *sc, volatile uint64_t *ptr)
263 if (sc->sc_ctx_is_64_byte) {
265 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
266 /* all contexts are initially 32-bytes */
267 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
268 ptr = (volatile uint64_t *)(((volatile uint8_t *)ptr) + offset);
270 return (le64toh(*ptr));
275 xhci_reset_command_queue_locked(struct xhci_softc *sc)
277 struct usb_page_search buf_res;
278 struct xhci_hw_root *phwr;
284 temp = XREAD4(sc, oper, XHCI_CRCR_LO);
285 if (temp & XHCI_CRCR_LO_CRR) {
286 DPRINTF("Command ring running\n");
287 temp &= ~(XHCI_CRCR_LO_CS | XHCI_CRCR_LO_CA);
290 * Try to abort the last command as per section
291 * 4.6.1.2 "Aborting a Command" of the XHCI
295 /* stop and cancel */
296 XWRITE4(sc, oper, XHCI_CRCR_LO, temp | XHCI_CRCR_LO_CS);
297 XWRITE4(sc, oper, XHCI_CRCR_HI, 0);
299 XWRITE4(sc, oper, XHCI_CRCR_LO, temp | XHCI_CRCR_LO_CA);
300 XWRITE4(sc, oper, XHCI_CRCR_HI, 0);
303 usb_pause_mtx(&sc->sc_bus.bus_lock, hz / 4);
305 /* check if command ring is still running */
306 temp = XREAD4(sc, oper, XHCI_CRCR_LO);
307 if (temp & XHCI_CRCR_LO_CRR) {
308 DPRINTF("Command ring still running\n");
309 return (USB_ERR_IOERROR);
313 /* reset command ring */
314 sc->sc_command_ccs = 1;
315 sc->sc_command_idx = 0;
317 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
319 /* setup command ring control base address */
320 addr = buf_res.physaddr;
321 phwr = buf_res.buffer;
322 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_commands[0];
324 DPRINTF("CRCR=0x%016llx\n", (unsigned long long)addr);
326 memset(phwr->hwr_commands, 0, sizeof(phwr->hwr_commands));
327 phwr->hwr_commands[XHCI_MAX_COMMANDS - 1].qwTrb0 = htole64(addr);
329 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
331 XWRITE4(sc, oper, XHCI_CRCR_LO, ((uint32_t)addr) | XHCI_CRCR_LO_RCS);
332 XWRITE4(sc, oper, XHCI_CRCR_HI, (uint32_t)(addr >> 32));
338 xhci_start_controller(struct xhci_softc *sc)
340 struct usb_page_search buf_res;
341 struct xhci_hw_root *phwr;
342 struct xhci_dev_ctx_addr *pdctxa;
349 sc->sc_event_ccs = 1;
350 sc->sc_event_idx = 0;
351 sc->sc_command_ccs = 1;
352 sc->sc_command_idx = 0;
354 /* Reset controller */
355 XWRITE4(sc, oper, XHCI_USBCMD, XHCI_CMD_HCRST);
357 for (i = 0; i != 100; i++) {
358 usb_pause_mtx(NULL, hz / 100);
359 temp = (XREAD4(sc, oper, XHCI_USBCMD) & XHCI_CMD_HCRST) |
360 (XREAD4(sc, oper, XHCI_USBSTS) & XHCI_STS_CNR);
366 device_printf(sc->sc_bus.parent, "Controller "
368 return (USB_ERR_IOERROR);
371 if (!(XREAD4(sc, oper, XHCI_PAGESIZE) & XHCI_PAGESIZE_4K)) {
372 device_printf(sc->sc_bus.parent, "Controller does "
373 "not support 4K page size.\n");
374 return (USB_ERR_IOERROR);
377 temp = XREAD4(sc, capa, XHCI_HCSPARAMS1);
379 i = XHCI_HCS1_N_PORTS(temp);
382 device_printf(sc->sc_bus.parent, "Invalid number "
383 "of ports: %u\n", i);
384 return (USB_ERR_IOERROR);
388 sc->sc_noslot = XHCI_HCS1_DEVSLOT_MAX(temp);
390 if (sc->sc_noslot > XHCI_MAX_DEVICES)
391 sc->sc_noslot = XHCI_MAX_DEVICES;
393 /* setup number of device slots */
395 DPRINTF("CONFIG=0x%08x -> 0x%08x\n",
396 XREAD4(sc, oper, XHCI_CONFIG), sc->sc_noslot);
398 XWRITE4(sc, oper, XHCI_CONFIG, sc->sc_noslot);
400 DPRINTF("Max slots: %u\n", sc->sc_noslot);
402 temp = XREAD4(sc, capa, XHCI_HCSPARAMS2);
404 sc->sc_noscratch = XHCI_HCS2_SPB_MAX(temp);
406 if (sc->sc_noscratch > XHCI_MAX_SCRATCHPADS) {
407 device_printf(sc->sc_bus.parent, "XHCI request "
408 "too many scratchpads\n");
409 return (USB_ERR_NOMEM);
412 DPRINTF("Max scratch: %u\n", sc->sc_noscratch);
414 temp = XREAD4(sc, capa, XHCI_HCSPARAMS3);
416 sc->sc_exit_lat_max = XHCI_HCS3_U1_DEL(temp) +
417 XHCI_HCS3_U2_DEL(temp) + 250 /* us */;
419 temp = XREAD4(sc, oper, XHCI_USBSTS);
421 /* clear interrupts */
422 XWRITE4(sc, oper, XHCI_USBSTS, temp);
423 /* disable all device notifications */
424 XWRITE4(sc, oper, XHCI_DNCTRL, 0);
426 /* setup device context base address */
427 usbd_get_page(&sc->sc_hw.ctx_pc, 0, &buf_res);
428 pdctxa = buf_res.buffer;
429 memset(pdctxa, 0, sizeof(*pdctxa));
431 addr = buf_res.physaddr;
432 addr += (uintptr_t)&((struct xhci_dev_ctx_addr *)0)->qwSpBufPtr[0];
434 /* slot 0 points to the table of scratchpad pointers */
435 pdctxa->qwBaaDevCtxAddr[0] = htole64(addr);
437 for (i = 0; i != sc->sc_noscratch; i++) {
438 struct usb_page_search buf_scp;
439 usbd_get_page(&sc->sc_hw.scratch_pc[i], 0, &buf_scp);
440 pdctxa->qwSpBufPtr[i] = htole64((uint64_t)buf_scp.physaddr);
443 addr = buf_res.physaddr;
445 XWRITE4(sc, oper, XHCI_DCBAAP_LO, (uint32_t)addr);
446 XWRITE4(sc, oper, XHCI_DCBAAP_HI, (uint32_t)(addr >> 32));
447 XWRITE4(sc, oper, XHCI_DCBAAP_LO, (uint32_t)addr);
448 XWRITE4(sc, oper, XHCI_DCBAAP_HI, (uint32_t)(addr >> 32));
450 /* Setup event table size */
452 temp = XREAD4(sc, capa, XHCI_HCSPARAMS2);
454 DPRINTF("HCS2=0x%08x\n", temp);
456 temp = XHCI_HCS2_ERST_MAX(temp);
458 if (temp > XHCI_MAX_RSEG)
459 temp = XHCI_MAX_RSEG;
461 sc->sc_erst_max = temp;
463 DPRINTF("ERSTSZ=0x%08x -> 0x%08x\n",
464 XREAD4(sc, runt, XHCI_ERSTSZ(0)), temp);
466 XWRITE4(sc, runt, XHCI_ERSTSZ(0), XHCI_ERSTS_SET(temp));
468 /* Check if we should use the default IMOD value */
469 if (sc->sc_imod_default == 0)
470 sc->sc_imod_default = XHCI_IMOD_DEFAULT;
472 /* Setup interrupt rate */
473 XWRITE4(sc, runt, XHCI_IMOD(0), sc->sc_imod_default);
475 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
477 phwr = buf_res.buffer;
478 addr = buf_res.physaddr;
479 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_events[0];
481 /* reset hardware root structure */
482 memset(phwr, 0, sizeof(*phwr));
484 phwr->hwr_ring_seg[0].qwEvrsTablePtr = htole64(addr);
485 phwr->hwr_ring_seg[0].dwEvrsTableSize = htole32(XHCI_MAX_EVENTS);
487 DPRINTF("ERDP(0)=0x%016llx\n", (unsigned long long)addr);
489 XWRITE4(sc, runt, XHCI_ERDP_LO(0), (uint32_t)addr);
490 XWRITE4(sc, runt, XHCI_ERDP_HI(0), (uint32_t)(addr >> 32));
492 addr = buf_res.physaddr;
494 DPRINTF("ERSTBA(0)=0x%016llx\n", (unsigned long long)addr);
496 XWRITE4(sc, runt, XHCI_ERSTBA_LO(0), (uint32_t)addr);
497 XWRITE4(sc, runt, XHCI_ERSTBA_HI(0), (uint32_t)(addr >> 32));
499 /* Setup interrupter registers */
501 temp = XREAD4(sc, runt, XHCI_IMAN(0));
502 temp |= XHCI_IMAN_INTR_ENA;
503 XWRITE4(sc, runt, XHCI_IMAN(0), temp);
505 /* setup command ring control base address */
506 addr = buf_res.physaddr;
507 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_commands[0];
509 DPRINTF("CRCR=0x%016llx\n", (unsigned long long)addr);
511 XWRITE4(sc, oper, XHCI_CRCR_LO, ((uint32_t)addr) | XHCI_CRCR_LO_RCS);
512 XWRITE4(sc, oper, XHCI_CRCR_HI, (uint32_t)(addr >> 32));
514 phwr->hwr_commands[XHCI_MAX_COMMANDS - 1].qwTrb0 = htole64(addr);
516 usb_bus_mem_flush_all(&sc->sc_bus, &xhci_iterate_hw_softc);
519 XWRITE4(sc, oper, XHCI_USBCMD, XHCI_CMD_RS |
520 XHCI_CMD_INTE | XHCI_CMD_HSEE);
522 for (i = 0; i != 100; i++) {
523 usb_pause_mtx(NULL, hz / 100);
524 temp = XREAD4(sc, oper, XHCI_USBSTS) & XHCI_STS_HCH;
529 XWRITE4(sc, oper, XHCI_USBCMD, 0);
530 device_printf(sc->sc_bus.parent, "Run timeout.\n");
531 return (USB_ERR_IOERROR);
534 /* catch any lost interrupts */
535 xhci_do_poll(&sc->sc_bus);
537 if (sc->sc_port_route != NULL) {
538 /* Route all ports to the XHCI by default */
539 sc->sc_port_route(sc->sc_bus.parent,
540 ~xhciroute, xhciroute);
546 xhci_halt_controller(struct xhci_softc *sc)
554 sc->sc_oper_off = XREAD1(sc, capa, XHCI_CAPLENGTH);
555 sc->sc_runt_off = XREAD4(sc, capa, XHCI_RTSOFF) & ~0xF;
556 sc->sc_door_off = XREAD4(sc, capa, XHCI_DBOFF) & ~0x3;
558 /* Halt controller */
559 XWRITE4(sc, oper, XHCI_USBCMD, 0);
561 for (i = 0; i != 100; i++) {
562 usb_pause_mtx(NULL, hz / 100);
563 temp = XREAD4(sc, oper, XHCI_USBSTS) & XHCI_STS_HCH;
569 device_printf(sc->sc_bus.parent, "Controller halt timeout.\n");
570 return (USB_ERR_IOERROR);
576 xhci_init(struct xhci_softc *sc, device_t self)
582 /* initialise some bus fields */
583 sc->sc_bus.parent = self;
585 /* set the bus revision */
586 sc->sc_bus.usbrev = USB_REV_3_0;
588 /* set up the bus struct */
589 sc->sc_bus.methods = &xhci_bus_methods;
591 /* setup devices array */
592 sc->sc_bus.devices = sc->sc_devices;
593 sc->sc_bus.devices_max = XHCI_MAX_DEVICES;
595 /* set default cycle state in case of early interrupts */
596 sc->sc_event_ccs = 1;
597 sc->sc_command_ccs = 1;
599 /* set up bus space offsets */
601 sc->sc_oper_off = XREAD1(sc, capa, XHCI_CAPLENGTH);
602 sc->sc_runt_off = XREAD4(sc, capa, XHCI_RTSOFF) & ~0x1F;
603 sc->sc_door_off = XREAD4(sc, capa, XHCI_DBOFF) & ~0x3;
605 DPRINTF("CAPLENGTH=0x%x\n", sc->sc_oper_off);
606 DPRINTF("RUNTIMEOFFSET=0x%x\n", sc->sc_runt_off);
607 DPRINTF("DOOROFFSET=0x%x\n", sc->sc_door_off);
609 DPRINTF("xHCI version = 0x%04x\n", XREAD2(sc, capa, XHCI_HCIVERSION));
611 temp = XREAD4(sc, capa, XHCI_HCSPARAMS0);
613 DPRINTF("HCS0 = 0x%08x\n", temp);
615 /* set up context size */
616 if (XHCI_HCS0_CSZ(temp)) {
617 sc->sc_ctx_is_64_byte = 1;
619 sc->sc_ctx_is_64_byte = 0;
623 sc->sc_bus.dma_bits = XHCI_HCS0_AC64(temp) ? 64 : 32;
625 device_printf(self, "%d bytes context size, %d-bit DMA\n",
626 sc->sc_ctx_is_64_byte ? 64 : 32, (int)sc->sc_bus.dma_bits);
628 /* get all DMA memory */
629 if (usb_bus_mem_alloc_all(&sc->sc_bus,
630 USB_GET_DMA_TAG(self), &xhci_iterate_hw_softc)) {
634 /* setup command queue mutex and condition varible */
635 cv_init(&sc->sc_cmd_cv, "CMDQ");
636 lockinit(&sc->sc_cmd_lock, "CMDQ lock", 0, LK_CANRECURSE);
638 sc->sc_config_msg[0].hdr.pm_callback = &xhci_configure_msg;
639 sc->sc_config_msg[0].bus = &sc->sc_bus;
640 sc->sc_config_msg[1].hdr.pm_callback = &xhci_configure_msg;
641 sc->sc_config_msg[1].bus = &sc->sc_bus;
647 xhci_uninit(struct xhci_softc *sc)
650 * NOTE: At this point the control transfer process is gone
651 * and "xhci_configure_msg" is no longer called. Consequently
652 * waiting for the configuration messages to complete is not
655 usb_bus_mem_free_all(&sc->sc_bus, &xhci_iterate_hw_softc);
657 cv_destroy(&sc->sc_cmd_cv);
658 lockuninit(&sc->sc_cmd_lock);
662 xhci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
664 struct xhci_softc *sc = XHCI_BUS2SC(bus);
667 case USB_HW_POWER_SUSPEND:
668 DPRINTF("Stopping the XHCI\n");
669 xhci_halt_controller(sc);
671 case USB_HW_POWER_SHUTDOWN:
672 DPRINTF("Stopping the XHCI\n");
673 xhci_halt_controller(sc);
675 case USB_HW_POWER_RESUME:
676 DPRINTF("Starting the XHCI\n");
677 xhci_start_controller(sc);
685 xhci_generic_done_sub(struct usb_xfer *xfer)
688 struct xhci_td *td_alt_next;
692 td = xfer->td_transfer_cache;
693 td_alt_next = td->alt_next;
695 if (xfer->aframes != xfer->nframes)
696 usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
700 usb_pc_cpu_invalidate(td->page_cache);
705 DPRINTFN(4, "xfer=%p[%u/%u] rem=%u/%u status=%u\n",
706 xfer, (unsigned int)xfer->aframes,
707 (unsigned int)xfer->nframes,
708 (unsigned int)len, (unsigned int)td->len,
709 (unsigned int)status);
712 * Verify the status length and
713 * add the length to "frlengths[]":
716 /* should not happen */
717 DPRINTF("Invalid status length, "
718 "0x%04x/0x%04x bytes\n", len, td->len);
719 status = XHCI_TRB_ERROR_LENGTH;
720 } else if (xfer->aframes != xfer->nframes) {
721 xfer->frlengths[xfer->aframes] += td->len - len;
723 /* Check for last transfer */
724 if (((void *)td) == xfer->td_transfer_last) {
728 /* Check for transfer error */
729 if (status != XHCI_TRB_ERROR_SHORT_PKT &&
730 status != XHCI_TRB_ERROR_SUCCESS) {
731 /* the transfer is finished */
735 /* Check for short transfer */
737 if (xfer->flags_int.short_frames_ok ||
738 xfer->flags_int.isochronous_xfr ||
739 xfer->flags_int.control_xfr) {
740 /* follow alt next */
743 /* the transfer is finished */
750 if (td->alt_next != td_alt_next) {
751 /* this USB frame is complete */
756 /* update transfer cache */
758 xfer->td_transfer_cache = td;
760 return ((status == XHCI_TRB_ERROR_STALL) ? USB_ERR_STALLED :
761 (status != XHCI_TRB_ERROR_SHORT_PKT &&
762 status != XHCI_TRB_ERROR_SUCCESS) ? USB_ERR_IOERROR :
763 USB_ERR_NORMAL_COMPLETION);
767 xhci_generic_done(struct usb_xfer *xfer)
771 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
772 xfer, xfer->endpoint);
776 xfer->td_transfer_cache = xfer->td_transfer_first;
778 if (xfer->flags_int.control_xfr) {
780 if (xfer->flags_int.control_hdr)
781 err = xhci_generic_done_sub(xfer);
785 if (xfer->td_transfer_cache == NULL)
789 while (xfer->aframes != xfer->nframes) {
791 err = xhci_generic_done_sub(xfer);
794 if (xfer->td_transfer_cache == NULL)
798 if (xfer->flags_int.control_xfr &&
799 !xfer->flags_int.control_act)
800 err = xhci_generic_done_sub(xfer);
802 /* transfer is complete */
803 xhci_device_done(xfer, err);
807 xhci_activate_transfer(struct usb_xfer *xfer)
811 td = xfer->td_transfer_cache;
813 usb_pc_cpu_invalidate(td->page_cache);
815 if (!(td->td_trb[0].dwTrb3 & htole32(XHCI_TRB_3_CYCLE_BIT))) {
817 /* activate the transfer */
819 td->td_trb[0].dwTrb3 |= htole32(XHCI_TRB_3_CYCLE_BIT);
820 usb_pc_cpu_flush(td->page_cache);
822 xhci_endpoint_doorbell(xfer);
827 xhci_skip_transfer(struct usb_xfer *xfer)
830 struct xhci_td *td_last;
832 td = xfer->td_transfer_cache;
833 td_last = xfer->td_transfer_last;
837 usb_pc_cpu_invalidate(td->page_cache);
839 if (!(td->td_trb[0].dwTrb3 & htole32(XHCI_TRB_3_CYCLE_BIT))) {
841 usb_pc_cpu_invalidate(td_last->page_cache);
843 /* copy LINK TRB to current waiting location */
845 td->td_trb[0].qwTrb0 = td_last->td_trb[td_last->ntrb].qwTrb0;
846 td->td_trb[0].dwTrb2 = td_last->td_trb[td_last->ntrb].dwTrb2;
847 usb_pc_cpu_flush(td->page_cache);
849 td->td_trb[0].dwTrb3 = td_last->td_trb[td_last->ntrb].dwTrb3;
850 usb_pc_cpu_flush(td->page_cache);
852 xhci_endpoint_doorbell(xfer);
856 /*------------------------------------------------------------------------*
857 * xhci_check_transfer
858 *------------------------------------------------------------------------*/
860 xhci_check_transfer(struct xhci_softc *sc, struct xhci_trb *trb)
862 struct xhci_endpoint_ext *pepext;
875 td_event = le64toh(trb->qwTrb0);
876 temp = le32toh(trb->dwTrb2);
878 remainder = XHCI_TRB_2_REM_GET(temp);
879 status = XHCI_TRB_2_ERROR_GET(temp);
880 stream_id = XHCI_TRB_2_STREAM_GET(temp);
882 temp = le32toh(trb->dwTrb3);
883 epno = XHCI_TRB_3_EP_GET(temp);
884 index = XHCI_TRB_3_SLOT_GET(temp);
886 /* check if error means halted */
887 halted = (status != XHCI_TRB_ERROR_SHORT_PKT &&
888 status != XHCI_TRB_ERROR_SUCCESS);
890 DPRINTF("slot=%u epno=%u stream=%u remainder=%u status=%u\n",
891 index, epno, stream_id, remainder, status);
893 if (index > sc->sc_noslot) {
894 DPRINTF("Invalid slot.\n");
898 if ((epno == 0) || (epno >= XHCI_MAX_ENDPOINTS)) {
899 DPRINTF("Invalid endpoint.\n");
903 pepext = &sc->sc_hw.devs[index].endp[epno];
905 if (pepext->trb_ep_mode != USB_EP_MODE_STREAMS) {
907 DPRINTF("stream_id=0\n");
908 } else if (stream_id >= XHCI_MAX_STREAMS) {
909 DPRINTF("Invalid stream ID.\n");
913 /* try to find the USB transfer that generated the event */
914 for (i = 0; i != (XHCI_MAX_TRANSFERS - 1); i++) {
915 struct usb_xfer *xfer;
918 xfer = pepext->xfer[i + (XHCI_MAX_TRANSFERS * stream_id)];
922 td = xfer->td_transfer_cache;
924 DPRINTFN(5, "Checking if 0x%016llx == (0x%016llx .. 0x%016llx)\n",
926 (long long)td->td_self,
927 (long long)td->td_self + sizeof(td->td_trb));
930 * NOTE: Some XHCI implementations might not trigger
931 * an event on the last LINK TRB so we need to
932 * consider both the last and second last event
933 * address as conditions for a successful transfer.
935 * NOTE: We assume that the XHCI will only trigger one
936 * event per chain of TRBs.
939 offset = td_event - td->td_self;
942 offset < (int64_t)sizeof(td->td_trb)) {
944 usb_pc_cpu_invalidate(td->page_cache);
946 /* compute rest of remainder, if any */
947 for (i = (offset / 16) + 1; i < td->ntrb; i++) {
948 temp = le32toh(td->td_trb[i].dwTrb2);
949 remainder += XHCI_TRB_2_BYTES_GET(temp);
952 DPRINTFN(5, "New remainder: %u\n", remainder);
954 /* clear isochronous transfer errors */
955 if (xfer->flags_int.isochronous_xfr) {
958 status = XHCI_TRB_ERROR_SUCCESS;
963 /* "td->remainder" is verified later */
964 td->remainder = remainder;
967 usb_pc_cpu_flush(td->page_cache);
970 * 1) Last transfer descriptor makes the
973 if (((void *)td) == xfer->td_transfer_last) {
974 DPRINTF("TD is last\n");
975 xhci_generic_done(xfer);
980 * 2) Any kind of error makes the transfer
984 DPRINTF("TD has I/O error\n");
985 xhci_generic_done(xfer);
990 * 3) If there is no alternate next transfer,
991 * a short packet also makes the transfer done
993 if (td->remainder > 0) {
994 if (td->alt_next == NULL) {
996 "short TD has no alternate next\n");
997 xhci_generic_done(xfer);
1000 DPRINTF("TD has short pkt\n");
1001 if (xfer->flags_int.short_frames_ok ||
1002 xfer->flags_int.isochronous_xfr ||
1003 xfer->flags_int.control_xfr) {
1004 /* follow the alt next */
1005 xfer->td_transfer_cache = td->alt_next;
1006 xhci_activate_transfer(xfer);
1009 xhci_skip_transfer(xfer);
1010 xhci_generic_done(xfer);
1015 * 4) Transfer complete - go to next TD
1017 DPRINTF("Following next TD\n");
1018 xfer->td_transfer_cache = td->obj_next;
1019 xhci_activate_transfer(xfer);
1020 break; /* there should only be one match */
1026 xhci_check_command(struct xhci_softc *sc, struct xhci_trb *trb)
1028 if (sc->sc_cmd_addr == trb->qwTrb0) {
1029 DPRINTF("Received command event\n");
1030 sc->sc_cmd_result[0] = trb->dwTrb2;
1031 sc->sc_cmd_result[1] = trb->dwTrb3;
1032 cv_signal(&sc->sc_cmd_cv);
1033 return (1); /* command match */
1039 xhci_interrupt_poll(struct xhci_softc *sc)
1041 struct usb_page_search buf_res;
1042 struct xhci_hw_root *phwr;
1052 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
1054 phwr = buf_res.buffer;
1056 /* Receive any events */
1058 usb_pc_cpu_invalidate(&sc->sc_hw.root_pc);
1060 i = sc->sc_event_idx;
1061 j = sc->sc_event_ccs;
1066 temp = le32toh(phwr->hwr_events[i].dwTrb3);
1068 k = (temp & XHCI_TRB_3_CYCLE_BIT) ? 1 : 0;
1073 event = XHCI_TRB_3_TYPE_GET(temp);
1075 DPRINTFN(10, "event[%u] = %u (0x%016llx 0x%08lx 0x%08lx)\n",
1076 i, event, (long long)le64toh(phwr->hwr_events[i].qwTrb0),
1077 (long)le32toh(phwr->hwr_events[i].dwTrb2),
1078 (long)le32toh(phwr->hwr_events[i].dwTrb3));
1081 case XHCI_TRB_EVENT_TRANSFER:
1082 xhci_check_transfer(sc, &phwr->hwr_events[i]);
1084 case XHCI_TRB_EVENT_CMD_COMPLETE:
1085 retval |= xhci_check_command(sc, &phwr->hwr_events[i]);
1088 DPRINTF("Unhandled event = %u\n", event);
1094 if (i == XHCI_MAX_EVENTS) {
1098 /* check for timeout */
1104 sc->sc_event_idx = i;
1105 sc->sc_event_ccs = j;
1108 * NOTE: The Event Ring Dequeue Pointer Register is 64-bit
1109 * latched. That means to activate the register we need to
1110 * write both the low and high double word of the 64-bit
1114 addr = buf_res.physaddr;
1115 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_events[i];
1117 /* try to clear busy bit */
1118 addr |= XHCI_ERDP_LO_BUSY;
1120 XWRITE4(sc, runt, XHCI_ERDP_LO(0), (uint32_t)addr);
1121 XWRITE4(sc, runt, XHCI_ERDP_HI(0), (uint32_t)(addr >> 32));
1127 xhci_do_command(struct xhci_softc *sc, struct xhci_trb *trb,
1128 uint16_t timeout_ms)
1130 struct usb_page_search buf_res;
1131 struct xhci_hw_root *phwr;
1136 uint8_t timeout = 0;
1139 XHCI_CMD_ASSERT_LOCKED(sc);
1141 /* get hardware root structure */
1143 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
1145 phwr = buf_res.buffer;
1149 USB_BUS_LOCK(&sc->sc_bus);
1151 i = sc->sc_command_idx;
1152 j = sc->sc_command_ccs;
1154 DPRINTFN(10, "command[%u] = %u (0x%016llx, 0x%08lx, 0x%08lx)\n",
1155 i, XHCI_TRB_3_TYPE_GET(le32toh(trb->dwTrb3)),
1156 (long long)le64toh(trb->qwTrb0),
1157 (long)le32toh(trb->dwTrb2),
1158 (long)le32toh(trb->dwTrb3));
1160 phwr->hwr_commands[i].qwTrb0 = trb->qwTrb0;
1161 phwr->hwr_commands[i].dwTrb2 = trb->dwTrb2;
1163 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1168 temp |= htole32(XHCI_TRB_3_CYCLE_BIT);
1170 temp &= ~htole32(XHCI_TRB_3_CYCLE_BIT);
1172 temp &= ~htole32(XHCI_TRB_3_TC_BIT);
1174 phwr->hwr_commands[i].dwTrb3 = temp;
1176 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1178 addr = buf_res.physaddr;
1179 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_commands[i];
1181 sc->sc_cmd_addr = htole64(addr);
1185 if (i == (XHCI_MAX_COMMANDS - 1)) {
1188 temp = htole32(XHCI_TRB_3_TC_BIT |
1189 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) |
1190 XHCI_TRB_3_CYCLE_BIT);
1192 temp = htole32(XHCI_TRB_3_TC_BIT |
1193 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
1196 phwr->hwr_commands[i].dwTrb3 = temp;
1198 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1204 sc->sc_command_idx = i;
1205 sc->sc_command_ccs = j;
1207 XWRITE4(sc, door, XHCI_DOORBELL(0), 0);
1209 err = cv_timedwait(&sc->sc_cmd_cv, &sc->sc_bus.bus_lock,
1210 USB_MS_TO_TICKS(timeout_ms));
1213 * In some error cases event interrupts are not generated.
1214 * Poll one time to see if the command has completed.
1216 if (err != 0 && xhci_interrupt_poll(sc) != 0) {
1217 DPRINTF("Command was completed when polling\n");
1221 DPRINTF("Command timeout!\n");
1223 * After some weeks of continuous operation, it has
1224 * been observed that the ASMedia Technology, ASM1042
1225 * SuperSpeed USB Host Controller can suddenly stop
1226 * accepting commands via the command queue. Try to
1227 * first reset the command queue. If that fails do a
1228 * host controller reset.
1231 xhci_reset_command_queue_locked(sc) == 0) {
1232 temp = le32toh(trb->dwTrb3);
1235 * Avoid infinite XHCI reset loops if the set
1236 * address command fails to respond due to a
1237 * non-enumerating device:
1239 if (XHCI_TRB_3_TYPE_GET(temp) == XHCI_TRB_TYPE_ADDRESS_DEVICE &&
1240 (temp & XHCI_TRB_3_BSR_BIT) == 0) {
1241 DPRINTF("Set address timeout\n");
1247 DPRINTF("Controller reset!\n");
1248 usb_bus_reset_async_locked(&sc->sc_bus);
1250 err = USB_ERR_TIMEOUT;
1254 temp = le32toh(sc->sc_cmd_result[0]);
1255 if (XHCI_TRB_2_ERROR_GET(temp) != XHCI_TRB_ERROR_SUCCESS)
1256 err = USB_ERR_IOERROR;
1258 trb->dwTrb2 = sc->sc_cmd_result[0];
1259 trb->dwTrb3 = sc->sc_cmd_result[1];
1262 USB_BUS_UNLOCK(&sc->sc_bus);
1269 xhci_cmd_nop(struct xhci_softc *sc)
1271 struct xhci_trb trb;
1278 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NOOP);
1280 trb.dwTrb3 = htole32(temp);
1282 return (xhci_do_command(sc, &trb, 100 /* ms */));
1287 xhci_cmd_enable_slot(struct xhci_softc *sc, uint8_t *pslot)
1289 struct xhci_trb trb;
1297 trb.dwTrb3 = htole32(XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ENABLE_SLOT));
1299 err = xhci_do_command(sc, &trb, 100 /* ms */);
1303 temp = le32toh(trb.dwTrb3);
1305 *pslot = XHCI_TRB_3_SLOT_GET(temp);
1312 xhci_cmd_disable_slot(struct xhci_softc *sc, uint8_t slot_id)
1314 struct xhci_trb trb;
1321 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DISABLE_SLOT) |
1322 XHCI_TRB_3_SLOT_SET(slot_id);
1324 trb.dwTrb3 = htole32(temp);
1326 return (xhci_do_command(sc, &trb, 100 /* ms */));
1330 xhci_cmd_set_address(struct xhci_softc *sc, uint64_t input_ctx,
1331 uint8_t bsr, uint8_t slot_id)
1333 struct xhci_trb trb;
1338 trb.qwTrb0 = htole64(input_ctx);
1340 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ADDRESS_DEVICE) |
1341 XHCI_TRB_3_SLOT_SET(slot_id);
1344 temp |= XHCI_TRB_3_BSR_BIT;
1346 trb.dwTrb3 = htole32(temp);
1348 return (xhci_do_command(sc, &trb, 500 /* ms */));
1352 xhci_set_address(struct usb_device *udev, struct lock *lock, uint16_t address)
1354 struct usb_page_search buf_inp;
1355 struct usb_page_search buf_dev;
1356 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
1357 struct xhci_hw_dev *hdev;
1358 struct xhci_dev_ctx *pdev;
1359 struct xhci_endpoint_ext *pepext;
1365 /* the root HUB case is not handled here */
1366 if (udev->parent_hub == NULL)
1367 return (USB_ERR_INVAL);
1369 index = udev->controller_slot_id;
1371 hdev = &sc->sc_hw.devs[index];
1374 lockmgr(lock, LK_RELEASE);
1378 switch (hdev->state) {
1379 case XHCI_ST_DEFAULT:
1380 case XHCI_ST_ENABLED:
1382 hdev->state = XHCI_ST_ENABLED;
1384 /* set configure mask to slot and EP0 */
1385 xhci_configure_mask(udev, 3, 0);
1387 /* configure input slot context structure */
1388 err = xhci_configure_device(udev);
1391 DPRINTF("Could not configure device\n");
1395 /* configure input endpoint context structure */
1396 switch (udev->speed) {
1398 case USB_SPEED_FULL:
1401 case USB_SPEED_HIGH:
1409 pepext = xhci_get_endpoint_ext(udev,
1410 &udev->ctrl_ep_desc);
1411 err = xhci_configure_endpoint(udev,
1412 &udev->ctrl_ep_desc, pepext,
1413 0, 1, 1, 0, mps, mps, USB_EP_MODE_DEFAULT);
1416 DPRINTF("Could not configure default endpoint\n");
1420 /* execute set address command */
1421 usbd_get_page(&hdev->input_pc, 0, &buf_inp);
1423 err = xhci_cmd_set_address(sc, buf_inp.physaddr,
1424 (address == 0), index);
1427 temp = le32toh(sc->sc_cmd_result[0]);
1428 if (address == 0 && sc->sc_port_route != NULL &&
1429 XHCI_TRB_2_ERROR_GET(temp) ==
1430 XHCI_TRB_ERROR_PARAMETER) {
1431 /* LynxPoint XHCI - ports are not switchable */
1432 /* Un-route all ports from the XHCI */
1433 sc->sc_port_route(sc->sc_bus.parent, 0, ~0);
1435 DPRINTF("Could not set address "
1436 "for slot %u.\n", index);
1441 /* update device address to new value */
1443 usbd_get_page(&hdev->device_pc, 0, &buf_dev);
1444 pdev = buf_dev.buffer;
1445 usb_pc_cpu_invalidate(&hdev->device_pc);
1447 temp = xhci_ctx_get_le32(sc, &pdev->ctx_slot.dwSctx3);
1448 udev->address = XHCI_SCTX_3_DEV_ADDR_GET(temp);
1450 /* update device state to new value */
1453 hdev->state = XHCI_ST_ADDRESSED;
1455 hdev->state = XHCI_ST_DEFAULT;
1459 DPRINTF("Wrong state for set address.\n");
1460 err = USB_ERR_IOERROR;
1463 XHCI_CMD_UNLOCK(sc);
1466 lockmgr(lock, LK_EXCLUSIVE);
1472 xhci_cmd_configure_ep(struct xhci_softc *sc, uint64_t input_ctx,
1473 uint8_t deconfigure, uint8_t slot_id)
1475 struct xhci_trb trb;
1480 trb.qwTrb0 = htole64(input_ctx);
1482 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_CONFIGURE_EP) |
1483 XHCI_TRB_3_SLOT_SET(slot_id);
1486 temp |= XHCI_TRB_3_DCEP_BIT;
1488 trb.dwTrb3 = htole32(temp);
1490 return (xhci_do_command(sc, &trb, 100 /* ms */));
1494 xhci_cmd_evaluate_ctx(struct xhci_softc *sc, uint64_t input_ctx,
1497 struct xhci_trb trb;
1502 trb.qwTrb0 = htole64(input_ctx);
1504 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVALUATE_CTX) |
1505 XHCI_TRB_3_SLOT_SET(slot_id);
1506 trb.dwTrb3 = htole32(temp);
1508 return (xhci_do_command(sc, &trb, 100 /* ms */));
1512 xhci_cmd_reset_ep(struct xhci_softc *sc, uint8_t preserve,
1513 uint8_t ep_id, uint8_t slot_id)
1515 struct xhci_trb trb;
1522 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_EP) |
1523 XHCI_TRB_3_SLOT_SET(slot_id) |
1524 XHCI_TRB_3_EP_SET(ep_id);
1527 temp |= XHCI_TRB_3_PRSV_BIT;
1529 trb.dwTrb3 = htole32(temp);
1531 return (xhci_do_command(sc, &trb, 100 /* ms */));
1535 xhci_cmd_set_tr_dequeue_ptr(struct xhci_softc *sc, uint64_t dequeue_ptr,
1536 uint16_t stream_id, uint8_t ep_id, uint8_t slot_id)
1538 struct xhci_trb trb;
1543 trb.qwTrb0 = htole64(dequeue_ptr);
1545 temp = XHCI_TRB_2_STREAM_SET(stream_id);
1546 trb.dwTrb2 = htole32(temp);
1548 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SET_TR_DEQUEUE) |
1549 XHCI_TRB_3_SLOT_SET(slot_id) |
1550 XHCI_TRB_3_EP_SET(ep_id);
1551 trb.dwTrb3 = htole32(temp);
1553 return (xhci_do_command(sc, &trb, 100 /* ms */));
1557 xhci_cmd_stop_ep(struct xhci_softc *sc, uint8_t suspend,
1558 uint8_t ep_id, uint8_t slot_id)
1560 struct xhci_trb trb;
1567 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STOP_EP) |
1568 XHCI_TRB_3_SLOT_SET(slot_id) |
1569 XHCI_TRB_3_EP_SET(ep_id);
1572 temp |= XHCI_TRB_3_SUSP_EP_BIT;
1574 trb.dwTrb3 = htole32(temp);
1576 return (xhci_do_command(sc, &trb, 100 /* ms */));
1580 xhci_cmd_reset_dev(struct xhci_softc *sc, uint8_t slot_id)
1582 struct xhci_trb trb;
1589 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_DEVICE) |
1590 XHCI_TRB_3_SLOT_SET(slot_id);
1592 trb.dwTrb3 = htole32(temp);
1594 return (xhci_do_command(sc, &trb, 100 /* ms */));
1597 /*------------------------------------------------------------------------*
1598 * xhci_interrupt - XHCI interrupt handler
1599 *------------------------------------------------------------------------*/
1601 xhci_interrupt(struct xhci_softc *sc)
1606 USB_BUS_LOCK(&sc->sc_bus);
1608 status = XREAD4(sc, oper, XHCI_USBSTS);
1610 /* acknowledge interrupts, if any */
1612 XWRITE4(sc, oper, XHCI_USBSTS, status);
1613 DPRINTFN(16, "real interrupt (status=0x%08x)\n", status);
1616 temp = XREAD4(sc, runt, XHCI_IMAN(0));
1618 /* force clearing of pending interrupts */
1619 if (temp & XHCI_IMAN_INTR_PEND)
1620 XWRITE4(sc, runt, XHCI_IMAN(0), temp);
1622 /* check for event(s) */
1623 xhci_interrupt_poll(sc);
1625 if (status & (XHCI_STS_PCD | XHCI_STS_HCH |
1626 XHCI_STS_HSE | XHCI_STS_HCE)) {
1628 if (status & XHCI_STS_PCD) {
1632 if (status & XHCI_STS_HCH) {
1633 kprintf("%s: host controller halted\n",
1637 if (status & XHCI_STS_HSE) {
1638 kprintf("%s: host system error\n",
1642 if (status & XHCI_STS_HCE) {
1643 kprintf("%s: host controller error\n",
1647 USB_BUS_UNLOCK(&sc->sc_bus);
1650 /*------------------------------------------------------------------------*
1651 * xhci_timeout - XHCI timeout handler
1652 *------------------------------------------------------------------------*/
1654 xhci_timeout(void *arg)
1656 struct usb_xfer *xfer = arg;
1658 DPRINTF("xfer=%p\n", xfer);
1660 USB_BUS_LOCK_ASSERT(xfer->xroot->bus);
1662 /* transfer is transferred */
1663 xhci_device_done(xfer, USB_ERR_TIMEOUT);
1667 xhci_do_poll(struct usb_bus *bus)
1669 struct xhci_softc *sc = XHCI_BUS2SC(bus);
1671 USB_BUS_LOCK(&sc->sc_bus);
1672 xhci_interrupt_poll(sc);
1673 USB_BUS_UNLOCK(&sc->sc_bus);
1677 xhci_setup_generic_chain_sub(struct xhci_std_temp *temp)
1679 struct usb_page_search buf_res;
1681 struct xhci_td *td_next;
1682 struct xhci_td *td_alt_next;
1683 struct xhci_td *td_first;
1684 uint32_t buf_offset;
1689 uint8_t shortpkt_old;
1695 shortpkt_old = temp->shortpkt;
1696 len_old = temp->len;
1703 td_next = td_first = temp->td_next;
1707 if (temp->len == 0) {
1712 /* send a Zero Length Packet, ZLP, last */
1719 average = temp->average;
1721 if (temp->len < average) {
1722 if (temp->len % temp->max_packet_size) {
1725 average = temp->len;
1729 if (td_next == NULL)
1730 panic("%s: out of XHCI transfer descriptors!", __func__);
1735 td_next = td->obj_next;
1737 /* check if we are pre-computing */
1741 /* update remaining length */
1743 temp->len -= average;
1747 /* fill out current TD */
1753 /* update remaining length */
1755 temp->len -= average;
1757 /* reset TRB index */
1761 if (temp->trb_type == XHCI_TRB_TYPE_SETUP_STAGE) {
1762 /* immediate data */
1767 td->td_trb[0].qwTrb0 = 0;
1769 usbd_copy_out(temp->pc, temp->offset + buf_offset,
1770 (uint8_t *)(uintptr_t)&td->td_trb[0].qwTrb0,
1773 dword = XHCI_TRB_2_BYTES_SET(8) |
1774 XHCI_TRB_2_TDSZ_SET(0) |
1775 XHCI_TRB_2_IRQ_SET(0);
1777 td->td_trb[0].dwTrb2 = htole32(dword);
1779 dword = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SETUP_STAGE) |
1780 XHCI_TRB_3_IDT_BIT | XHCI_TRB_3_CYCLE_BIT;
1783 if (td->td_trb[0].qwTrb0 &
1784 htole64(XHCI_TRB_0_WLENGTH_MASK)) {
1785 if (td->td_trb[0].qwTrb0 &
1786 htole64(XHCI_TRB_0_DIR_IN_MASK))
1787 dword |= XHCI_TRB_3_TRT_IN;
1789 dword |= XHCI_TRB_3_TRT_OUT;
1792 td->td_trb[0].dwTrb3 = htole32(dword);
1794 xhci_dump_trb(&td->td_trb[x]);
1802 /* fill out buffer pointers */
1805 memset(&buf_res, 0, sizeof(buf_res));
1807 usbd_get_page(temp->pc, temp->offset +
1808 buf_offset, &buf_res);
1810 /* get length to end of page */
1811 if (buf_res.length > average)
1812 buf_res.length = average;
1814 /* check for maximum length */
1815 if (buf_res.length > XHCI_TD_PAGE_SIZE)
1816 buf_res.length = XHCI_TD_PAGE_SIZE;
1818 npkt_off += buf_res.length;
1822 npkt = (len_old - npkt_off + temp->max_packet_size - 1) /
1823 temp->max_packet_size;
1830 /* fill out TRB's */
1831 td->td_trb[x].qwTrb0 =
1832 htole64((uint64_t)buf_res.physaddr);
1835 XHCI_TRB_2_BYTES_SET(buf_res.length) |
1836 XHCI_TRB_2_TDSZ_SET(npkt) |
1837 XHCI_TRB_2_IRQ_SET(0);
1839 td->td_trb[x].dwTrb2 = htole32(dword);
1841 switch (temp->trb_type) {
1842 case XHCI_TRB_TYPE_ISOCH:
1843 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1844 XHCI_TRB_3_TBC_SET(temp->tbc) |
1845 XHCI_TRB_3_TLBPC_SET(temp->tlbpc);
1846 if (td != td_first) {
1847 dword |= XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL);
1848 } else if (temp->do_isoc_sync != 0) {
1849 temp->do_isoc_sync = 0;
1850 /* wait until "isoc_frame" */
1851 dword |= XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ISOCH) |
1852 XHCI_TRB_3_FRID_SET(temp->isoc_frame / 8);
1854 /* start data transfer at next interval */
1855 dword |= XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ISOCH) |
1856 XHCI_TRB_3_ISO_SIA_BIT;
1858 if (temp->direction == UE_DIR_IN)
1859 dword |= XHCI_TRB_3_ISP_BIT;
1861 case XHCI_TRB_TYPE_DATA_STAGE:
1862 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1863 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DATA_STAGE);
1864 if (temp->direction == UE_DIR_IN)
1865 dword |= XHCI_TRB_3_DIR_IN | XHCI_TRB_3_ISP_BIT;
1867 * Section 3.2.9 in the XHCI
1868 * specification about control
1869 * transfers says that we should use a
1870 * normal-TRB if there are more TRBs
1871 * extending the data-stage
1872 * TRB. Update the "trb_type".
1874 temp->trb_type = XHCI_TRB_TYPE_NORMAL;
1876 case XHCI_TRB_TYPE_STATUS_STAGE:
1877 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1878 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STATUS_STAGE);
1879 if (temp->direction == UE_DIR_IN)
1880 dword |= XHCI_TRB_3_DIR_IN;
1882 default: /* XHCI_TRB_TYPE_NORMAL */
1883 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1884 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL);
1885 if (temp->direction == UE_DIR_IN)
1886 dword |= XHCI_TRB_3_ISP_BIT;
1889 td->td_trb[x].dwTrb3 = htole32(dword);
1891 average -= buf_res.length;
1892 buf_offset += buf_res.length;
1894 xhci_dump_trb(&td->td_trb[x]);
1898 } while (average != 0);
1900 td->td_trb[x-1].dwTrb3 |= htole32(XHCI_TRB_3_IOC_BIT);
1902 /* store number of data TRB's */
1906 DPRINTF("NTRB=%u\n", x);
1908 /* fill out link TRB */
1910 if (td_next != NULL) {
1911 /* link the current TD with the next one */
1912 td->td_trb[x].qwTrb0 = htole64((uint64_t)td_next->td_self);
1913 DPRINTF("LINK=0x%08llx\n", (long long)td_next->td_self);
1915 /* this field will get updated later */
1916 DPRINTF("NOLINK\n");
1919 dword = XHCI_TRB_2_IRQ_SET(0);
1921 td->td_trb[x].dwTrb2 = htole32(dword);
1923 dword = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) |
1924 XHCI_TRB_3_CYCLE_BIT | XHCI_TRB_3_IOC_BIT |
1926 * CHAIN-BIT: Ensure that a multi-TRB IN-endpoint
1927 * frame only receives a single short packet event
1928 * by setting the CHAIN bit in the LINK field. In
1929 * addition some XHCI controllers have problems
1930 * sending a ZLP unless the CHAIN-BIT is set in
1933 XHCI_TRB_3_CHAIN_BIT;
1935 td->td_trb[x].dwTrb3 = htole32(dword);
1937 td->alt_next = td_alt_next;
1939 xhci_dump_trb(&td->td_trb[x]);
1941 usb_pc_cpu_flush(td->page_cache);
1947 /* setup alt next pointer, if any */
1948 if (temp->last_frame) {
1951 /* we use this field internally */
1952 td_alt_next = td_next;
1956 temp->shortpkt = shortpkt_old;
1957 temp->len = len_old;
1962 * Remove cycle bit from the first TRB if we are
1965 if (temp->step_td != 0) {
1966 td_first->td_trb[0].dwTrb3 &= ~htole32(XHCI_TRB_3_CYCLE_BIT);
1967 usb_pc_cpu_flush(td_first->page_cache);
1970 /* clear TD SIZE to zero, hence this is the last TRB */
1971 /* remove chain bit because this is the last data TRB in the chain */
1972 td->td_trb[td->ntrb - 1].dwTrb2 &= ~htole32(XHCI_TRB_2_TDSZ_SET(15));
1973 td->td_trb[td->ntrb - 1].dwTrb3 &= ~htole32(XHCI_TRB_3_CHAIN_BIT);
1974 /* remove CHAIN-BIT from last LINK TRB */
1975 td->td_trb[td->ntrb].dwTrb3 &= ~htole32(XHCI_TRB_3_CHAIN_BIT);
1977 usb_pc_cpu_flush(td->page_cache);
1980 temp->td_next = td_next;
1984 xhci_setup_generic_chain(struct usb_xfer *xfer)
1986 struct xhci_std_temp temp;
1992 temp.do_isoc_sync = 0;
1996 temp.average = xfer->max_hc_frame_size;
1997 temp.max_packet_size = xfer->max_packet_size;
1998 temp.sc = XHCI_BUS2SC(xfer->xroot->bus);
2000 temp.last_frame = 0;
2002 temp.multishort = xfer->flags_int.isochronous_xfr ||
2003 xfer->flags_int.control_xfr ||
2004 xfer->flags_int.short_frames_ok;
2006 /* toggle the DMA set we are using */
2007 xfer->flags_int.curr_dma_set ^= 1;
2009 /* get next DMA set */
2010 td = xfer->td_start[xfer->flags_int.curr_dma_set];
2015 xfer->td_transfer_first = td;
2016 xfer->td_transfer_cache = td;
2018 if (xfer->flags_int.isochronous_xfr) {
2021 /* compute multiplier for ISOCHRONOUS transfers */
2022 mult = xfer->endpoint->ecomp ?
2023 UE_GET_SS_ISO_MULT(xfer->endpoint->ecomp->bmAttributes)
2025 /* check for USB 2.0 multiplier */
2027 mult = (xfer->endpoint->edesc->
2028 wMaxPacketSize[1] >> 3) & 3;
2036 x = XREAD4(temp.sc, runt, XHCI_MFINDEX);
2038 DPRINTF("MFINDEX=0x%08x\n", x);
2040 switch (usbd_get_speed(xfer->xroot->udev)) {
2041 case USB_SPEED_FULL:
2043 temp.isoc_delta = 8; /* 1ms */
2044 x += temp.isoc_delta - 1;
2045 x &= ~(temp.isoc_delta - 1);
2048 shift = usbd_xfer_get_fps_shift(xfer);
2049 temp.isoc_delta = 1U << shift;
2050 x += temp.isoc_delta - 1;
2051 x &= ~(temp.isoc_delta - 1);
2052 /* simple frame load balancing */
2053 x += xfer->endpoint->usb_uframe;
2057 y = XHCI_MFINDEX_GET(x - xfer->endpoint->isoc_next);
2059 if ((xfer->endpoint->is_synced == 0) ||
2060 (y < (xfer->nframes << shift)) ||
2061 (XHCI_MFINDEX_GET(-y) >= (128 * 8))) {
2063 * If there is data underflow or the pipe
2064 * queue is empty we schedule the transfer a
2065 * few frames ahead of the current frame
2066 * position. Else two isochronous transfers
2069 xfer->endpoint->isoc_next = XHCI_MFINDEX_GET(x + (3 * 8));
2070 xfer->endpoint->is_synced = 1;
2071 temp.do_isoc_sync = 1;
2073 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
2076 /* compute isochronous completion time */
2078 y = XHCI_MFINDEX_GET(xfer->endpoint->isoc_next - (x & ~7));
2080 xfer->isoc_time_complete =
2081 usb_isoc_time_expand(&temp.sc->sc_bus, x / 8) +
2082 (y / 8) + (((xfer->nframes << shift) + 7) / 8);
2085 temp.isoc_frame = xfer->endpoint->isoc_next;
2086 temp.trb_type = XHCI_TRB_TYPE_ISOCH;
2088 xfer->endpoint->isoc_next += xfer->nframes << shift;
2090 } else if (xfer->flags_int.control_xfr) {
2092 /* check if we should prepend a setup message */
2094 if (xfer->flags_int.control_hdr) {
2096 temp.len = xfer->frlengths[0];
2097 temp.pc = xfer->frbuffers + 0;
2098 temp.shortpkt = temp.len ? 1 : 0;
2099 temp.trb_type = XHCI_TRB_TYPE_SETUP_STAGE;
2102 /* check for last frame */
2103 if (xfer->nframes == 1) {
2104 /* no STATUS stage yet, SETUP is last */
2105 if (xfer->flags_int.control_act)
2106 temp.last_frame = 1;
2109 xhci_setup_generic_chain_sub(&temp);
2113 temp.isoc_delta = 0;
2114 temp.isoc_frame = 0;
2115 temp.trb_type = xfer->flags_int.control_did_data ?
2116 XHCI_TRB_TYPE_NORMAL : XHCI_TRB_TYPE_DATA_STAGE;
2120 temp.isoc_delta = 0;
2121 temp.isoc_frame = 0;
2122 temp.trb_type = XHCI_TRB_TYPE_NORMAL;
2125 if (x != xfer->nframes) {
2126 /* setup page_cache pointer */
2127 temp.pc = xfer->frbuffers + x;
2128 /* set endpoint direction */
2129 temp.direction = UE_GET_DIR(xfer->endpointno);
2132 while (x != xfer->nframes) {
2134 /* DATA0 / DATA1 message */
2136 temp.len = xfer->frlengths[x];
2137 temp.step_td = ((xfer->endpointno & UE_DIR_IN) &&
2138 x != 0 && temp.multishort == 0);
2142 if (x == xfer->nframes) {
2143 if (xfer->flags_int.control_xfr) {
2144 /* no STATUS stage yet, DATA is last */
2145 if (xfer->flags_int.control_act)
2146 temp.last_frame = 1;
2148 temp.last_frame = 1;
2151 if (temp.len == 0) {
2153 /* make sure that we send an USB packet */
2158 temp.tlbpc = mult - 1;
2160 } else if (xfer->flags_int.isochronous_xfr) {
2165 * Isochronous transfers don't have short
2166 * packet termination:
2171 /* isochronous transfers have a transfer limit */
2173 if (temp.len > xfer->max_frame_size)
2174 temp.len = xfer->max_frame_size;
2176 /* compute TD packet count */
2177 tdpc = (temp.len + xfer->max_packet_size - 1) /
2178 xfer->max_packet_size;
2180 temp.tbc = ((tdpc + mult - 1) / mult) - 1;
2181 temp.tlbpc = (tdpc % mult);
2183 if (temp.tlbpc == 0)
2184 temp.tlbpc = mult - 1;
2189 /* regular data transfer */
2191 temp.shortpkt = xfer->flags.force_short_xfer ? 0 : 1;
2194 xhci_setup_generic_chain_sub(&temp);
2196 if (xfer->flags_int.isochronous_xfr) {
2197 temp.offset += xfer->frlengths[x - 1];
2198 temp.isoc_frame += temp.isoc_delta;
2200 /* get next Page Cache pointer */
2201 temp.pc = xfer->frbuffers + x;
2205 /* check if we should append a status stage */
2207 if (xfer->flags_int.control_xfr &&
2208 !xfer->flags_int.control_act) {
2211 * Send a DATA1 message and invert the current
2212 * endpoint direction.
2214 temp.step_td = (xfer->nframes != 0);
2215 temp.direction = UE_GET_DIR(xfer->endpointno) ^ UE_DIR_IN;
2219 temp.last_frame = 1;
2220 temp.trb_type = XHCI_TRB_TYPE_STATUS_STAGE;
2222 xhci_setup_generic_chain_sub(&temp);
2227 /* must have at least one frame! */
2229 xfer->td_transfer_last = td;
2231 DPRINTF("first=%p last=%p\n", xfer->td_transfer_first, td);
2235 xhci_set_slot_pointer(struct xhci_softc *sc, uint8_t index, uint64_t dev_addr)
2237 struct usb_page_search buf_res;
2238 struct xhci_dev_ctx_addr *pdctxa;
2240 usbd_get_page(&sc->sc_hw.ctx_pc, 0, &buf_res);
2242 pdctxa = buf_res.buffer;
2244 DPRINTF("addr[%u]=0x%016llx\n", index, (long long)dev_addr);
2246 pdctxa->qwBaaDevCtxAddr[index] = htole64(dev_addr);
2248 usb_pc_cpu_flush(&sc->sc_hw.ctx_pc);
2252 xhci_configure_mask(struct usb_device *udev, uint32_t mask, uint8_t drop)
2254 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2255 struct usb_page_search buf_inp;
2256 struct xhci_input_dev_ctx *pinp;
2261 index = udev->controller_slot_id;
2263 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
2265 pinp = buf_inp.buffer;
2268 mask &= XHCI_INCTX_NON_CTRL_MASK;
2269 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx0, mask);
2270 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx1, 0);
2273 * Some hardware requires that we drop the endpoint
2274 * context before adding it again:
2276 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx0,
2277 mask & XHCI_INCTX_NON_CTRL_MASK);
2279 /* Add new endpoint context */
2280 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx1, mask);
2282 /* find most significant set bit */
2283 for (x = 31; x != 1; x--) {
2284 if (mask & (1 << x))
2291 /* figure out the maximum number of contexts */
2292 if (x > sc->sc_hw.devs[index].context_num)
2293 sc->sc_hw.devs[index].context_num = x;
2295 x = sc->sc_hw.devs[index].context_num;
2297 /* update number of contexts */
2298 temp = xhci_ctx_get_le32(sc, &pinp->ctx_slot.dwSctx0);
2299 temp &= ~XHCI_SCTX_0_CTX_NUM_SET(31);
2300 temp |= XHCI_SCTX_0_CTX_NUM_SET(x + 1);
2301 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx0, temp);
2303 usb_pc_cpu_flush(&sc->sc_hw.devs[index].input_pc);
2308 xhci_configure_endpoint(struct usb_device *udev,
2309 struct usb_endpoint_descriptor *edesc, struct xhci_endpoint_ext *pepext,
2310 uint16_t interval, uint8_t max_packet_count,
2311 uint8_t mult, uint8_t fps_shift, uint16_t max_packet_size,
2312 uint16_t max_frame_size, uint8_t ep_mode)
2314 struct usb_page_search buf_inp;
2315 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2316 struct xhci_input_dev_ctx *pinp;
2317 uint64_t ring_addr = pepext->physaddr;
2323 index = udev->controller_slot_id;
2325 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
2327 pinp = buf_inp.buffer;
2329 epno = edesc->bEndpointAddress;
2330 type = edesc->bmAttributes & UE_XFERTYPE;
2332 if (type == UE_CONTROL)
2335 epno = XHCI_EPNO2EPID(epno);
2338 return (USB_ERR_NO_PIPE); /* invalid */
2340 if (max_packet_count == 0)
2341 return (USB_ERR_BAD_BUFSIZE);
2346 return (USB_ERR_BAD_BUFSIZE);
2348 /* store endpoint mode */
2349 pepext->trb_ep_mode = ep_mode;
2350 usb_pc_cpu_flush(pepext->page_cache);
2352 if (ep_mode == USB_EP_MODE_STREAMS) {
2353 temp = XHCI_EPCTX_0_EPSTATE_SET(0) |
2354 XHCI_EPCTX_0_MAXP_STREAMS_SET(XHCI_MAX_STREAMS_LOG - 1) |
2355 XHCI_EPCTX_0_LSA_SET(1);
2357 ring_addr += sizeof(struct xhci_trb) *
2358 XHCI_MAX_TRANSFERS * XHCI_MAX_STREAMS;
2360 temp = XHCI_EPCTX_0_EPSTATE_SET(0) |
2361 XHCI_EPCTX_0_MAXP_STREAMS_SET(0) |
2362 XHCI_EPCTX_0_LSA_SET(0);
2364 ring_addr |= XHCI_EPCTX_2_DCS_SET(1);
2367 switch (udev->speed) {
2368 case USB_SPEED_FULL:
2381 temp |= XHCI_EPCTX_0_IVAL_SET(fps_shift);
2383 case UE_ISOCHRONOUS:
2384 temp |= XHCI_EPCTX_0_IVAL_SET(fps_shift);
2386 switch (udev->speed) {
2387 case USB_SPEED_SUPER:
2390 temp |= XHCI_EPCTX_0_MULT_SET(mult - 1);
2391 max_packet_count /= mult;
2401 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx0, temp);
2404 XHCI_EPCTX_1_HID_SET(0) |
2405 XHCI_EPCTX_1_MAXB_SET(max_packet_count) |
2406 XHCI_EPCTX_1_MAXP_SIZE_SET(max_packet_size);
2409 * Always enable the "three strikes and you are gone" feature
2410 * except for ISOCHRONOUS endpoints. This is suggested by
2411 * section 4.3.3 in the XHCI specification about device slot
2414 if (type != UE_ISOCHRONOUS)
2415 temp |= XHCI_EPCTX_1_CERR_SET(3);
2419 temp |= XHCI_EPCTX_1_EPTYPE_SET(4);
2421 case UE_ISOCHRONOUS:
2422 temp |= XHCI_EPCTX_1_EPTYPE_SET(1);
2425 temp |= XHCI_EPCTX_1_EPTYPE_SET(2);
2428 temp |= XHCI_EPCTX_1_EPTYPE_SET(3);
2432 /* check for IN direction */
2434 temp |= XHCI_EPCTX_1_EPTYPE_SET(4);
2436 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx1, temp);
2437 xhci_ctx_set_le64(sc, &pinp->ctx_ep[epno - 1].qwEpCtx2, ring_addr);
2439 switch (edesc->bmAttributes & UE_XFERTYPE) {
2441 case UE_ISOCHRONOUS:
2442 temp = XHCI_EPCTX_4_MAX_ESIT_PAYLOAD_SET(max_frame_size) |
2443 XHCI_EPCTX_4_AVG_TRB_LEN_SET(MIN(XHCI_PAGE_SIZE,
2447 temp = XHCI_EPCTX_4_AVG_TRB_LEN_SET(8);
2450 temp = XHCI_EPCTX_4_AVG_TRB_LEN_SET(XHCI_PAGE_SIZE);
2454 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx4, temp);
2457 xhci_dump_endpoint(sc, &pinp->ctx_ep[epno - 1]);
2459 usb_pc_cpu_flush(&sc->sc_hw.devs[index].input_pc);
2461 return (0); /* success */
2465 xhci_configure_endpoint_by_xfer(struct usb_xfer *xfer)
2467 struct xhci_endpoint_ext *pepext;
2468 struct usb_endpoint_ss_comp_descriptor *ecomp;
2471 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2472 xfer->endpoint->edesc);
2474 ecomp = xfer->endpoint->ecomp;
2476 for (x = 0; x != XHCI_MAX_STREAMS; x++) {
2479 /* halt any transfers */
2480 pepext->trb[x * XHCI_MAX_TRANSFERS].dwTrb3 = 0;
2482 /* compute start of TRB ring for stream "x" */
2483 temp = pepext->physaddr +
2484 (x * XHCI_MAX_TRANSFERS * sizeof(struct xhci_trb)) +
2485 XHCI_SCTX_0_SCT_SEC_TR_RING;
2487 /* make tree structure */
2488 pepext->trb[(XHCI_MAX_TRANSFERS *
2489 XHCI_MAX_STREAMS) + x].qwTrb0 = htole64(temp);
2491 /* reserved fields */
2492 pepext->trb[(XHCI_MAX_TRANSFERS *
2493 XHCI_MAX_STREAMS) + x].dwTrb2 = 0;
2494 pepext->trb[(XHCI_MAX_TRANSFERS *
2495 XHCI_MAX_STREAMS) + x].dwTrb3 = 0;
2497 usb_pc_cpu_flush(pepext->page_cache);
2499 return (xhci_configure_endpoint(xfer->xroot->udev,
2500 xfer->endpoint->edesc, pepext,
2501 xfer->interval, xfer->max_packet_count,
2502 (ecomp != NULL) ? UE_GET_SS_ISO_MULT(ecomp->bmAttributes) + 1 : 1,
2503 usbd_xfer_get_fps_shift(xfer), xfer->max_packet_size,
2504 xfer->max_frame_size, xfer->endpoint->ep_mode));
2508 xhci_configure_device(struct usb_device *udev)
2510 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2511 struct usb_page_search buf_inp;
2512 struct usb_page_cache *pcinp;
2513 struct xhci_input_dev_ctx *pinp;
2514 struct usb_device *hubdev;
2522 index = udev->controller_slot_id;
2524 DPRINTF("index=%u\n", index);
2526 pcinp = &sc->sc_hw.devs[index].input_pc;
2528 usbd_get_page(pcinp, 0, &buf_inp);
2530 pinp = buf_inp.buffer;
2535 /* figure out route string and root HUB port number */
2537 for (hubdev = udev; hubdev != NULL; hubdev = hubdev->parent_hub) {
2539 if (hubdev->parent_hub == NULL)
2542 depth = hubdev->parent_hub->depth;
2545 * NOTE: HS/FS/LS devices and the SS root HUB can have
2546 * more than 15 ports
2549 rh_port = hubdev->port_no;
2558 route |= rh_port << (4 * (depth - 1));
2561 DPRINTF("Route=0x%08x\n", route);
2563 temp = XHCI_SCTX_0_ROUTE_SET(route) |
2564 XHCI_SCTX_0_CTX_NUM_SET(
2565 sc->sc_hw.devs[index].context_num + 1);
2567 switch (udev->speed) {
2569 temp |= XHCI_SCTX_0_SPEED_SET(2);
2570 if (udev->parent_hs_hub != NULL &&
2571 udev->parent_hs_hub->ddesc.bDeviceProtocol ==
2573 DPRINTF("Device inherits MTT\n");
2574 temp |= XHCI_SCTX_0_MTT_SET(1);
2577 case USB_SPEED_HIGH:
2578 temp |= XHCI_SCTX_0_SPEED_SET(3);
2579 if (sc->sc_hw.devs[index].nports != 0 &&
2580 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) {
2581 DPRINTF("HUB supports MTT\n");
2582 temp |= XHCI_SCTX_0_MTT_SET(1);
2585 case USB_SPEED_FULL:
2586 temp |= XHCI_SCTX_0_SPEED_SET(1);
2587 if (udev->parent_hs_hub != NULL &&
2588 udev->parent_hs_hub->ddesc.bDeviceProtocol ==
2590 DPRINTF("Device inherits MTT\n");
2591 temp |= XHCI_SCTX_0_MTT_SET(1);
2595 temp |= XHCI_SCTX_0_SPEED_SET(4);
2599 is_hub = sc->sc_hw.devs[index].nports != 0 &&
2600 (udev->speed == USB_SPEED_SUPER ||
2601 udev->speed == USB_SPEED_HIGH);
2604 temp |= XHCI_SCTX_0_HUB_SET(1);
2607 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx0, temp);
2609 temp = XHCI_SCTX_1_RH_PORT_SET(rh_port);
2612 temp |= XHCI_SCTX_1_NUM_PORTS_SET(
2613 sc->sc_hw.devs[index].nports);
2616 switch (udev->speed) {
2617 case USB_SPEED_SUPER:
2618 switch (sc->sc_hw.devs[index].state) {
2619 case XHCI_ST_ADDRESSED:
2620 case XHCI_ST_CONFIGURED:
2621 /* enable power save */
2622 temp |= XHCI_SCTX_1_MAX_EL_SET(sc->sc_exit_lat_max);
2625 /* disable power save */
2633 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx1, temp);
2635 temp = XHCI_SCTX_2_IRQ_TARGET_SET(0);
2638 temp |= XHCI_SCTX_2_TT_THINK_TIME_SET(
2639 sc->sc_hw.devs[index].tt);
2642 hubdev = udev->parent_hs_hub;
2644 /* check if we should activate the transaction translator */
2645 switch (udev->speed) {
2646 case USB_SPEED_FULL:
2648 if (hubdev != NULL) {
2649 temp |= XHCI_SCTX_2_TT_HUB_SID_SET(
2650 hubdev->controller_slot_id);
2651 temp |= XHCI_SCTX_2_TT_PORT_NUM_SET(
2659 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx2, temp);
2662 * These fields should be initialized to zero, according to
2663 * XHCI section 6.2.2 - slot context:
2665 temp = XHCI_SCTX_3_DEV_ADDR_SET(0) |
2666 XHCI_SCTX_3_SLOT_STATE_SET(0);
2668 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx3, temp);
2671 xhci_dump_device(sc, &pinp->ctx_slot);
2673 usb_pc_cpu_flush(pcinp);
2675 return (0); /* success */
2679 xhci_alloc_device_ext(struct usb_device *udev)
2681 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2682 struct usb_page_search buf_dev;
2683 struct usb_page_search buf_ep;
2684 struct xhci_trb *trb;
2685 struct usb_page_cache *pc;
2686 struct usb_page *pg;
2691 index = udev->controller_slot_id;
2693 pc = &sc->sc_hw.devs[index].device_pc;
2694 pg = &sc->sc_hw.devs[index].device_pg;
2696 /* need to initialize the page cache */
2697 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2699 if (usb_pc_alloc_mem(pc, pg, sc->sc_ctx_is_64_byte ?
2700 (2 * sizeof(struct xhci_dev_ctx)) :
2701 sizeof(struct xhci_dev_ctx), XHCI_PAGE_SIZE))
2704 usbd_get_page(pc, 0, &buf_dev);
2706 pc = &sc->sc_hw.devs[index].input_pc;
2707 pg = &sc->sc_hw.devs[index].input_pg;
2709 /* need to initialize the page cache */
2710 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2712 if (usb_pc_alloc_mem(pc, pg, sc->sc_ctx_is_64_byte ?
2713 (2 * sizeof(struct xhci_input_dev_ctx)) :
2714 sizeof(struct xhci_input_dev_ctx), XHCI_PAGE_SIZE)) {
2718 /* initialize all endpoint LINK TRBs */
2720 for (i = 0; i != XHCI_MAX_ENDPOINTS; i++) {
2722 pc = &sc->sc_hw.devs[index].endpoint_pc[i];
2723 pg = &sc->sc_hw.devs[index].endpoint_pg[i];
2725 /* need to initialize the page cache */
2726 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2728 if (usb_pc_alloc_mem(pc, pg,
2729 sizeof(struct xhci_dev_endpoint_trbs), XHCI_TRB_ALIGN)) {
2733 /* lookup endpoint TRB ring */
2734 usbd_get_page(pc, 0, &buf_ep);
2736 /* get TRB pointer */
2737 trb = buf_ep.buffer;
2738 trb += XHCI_MAX_TRANSFERS - 1;
2740 /* get TRB start address */
2741 addr = buf_ep.physaddr;
2743 /* create LINK TRB */
2744 trb->qwTrb0 = htole64(addr);
2745 trb->dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
2746 trb->dwTrb3 = htole32(XHCI_TRB_3_CYCLE_BIT |
2747 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
2749 usb_pc_cpu_flush(pc);
2752 xhci_set_slot_pointer(sc, index, buf_dev.physaddr);
2757 xhci_free_device_ext(udev);
2759 return (USB_ERR_NOMEM);
2763 xhci_free_device_ext(struct usb_device *udev)
2765 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2769 index = udev->controller_slot_id;
2770 xhci_set_slot_pointer(sc, index, 0);
2772 usb_pc_free_mem(&sc->sc_hw.devs[index].device_pc);
2773 usb_pc_free_mem(&sc->sc_hw.devs[index].input_pc);
2774 for (i = 0; i != XHCI_MAX_ENDPOINTS; i++)
2775 usb_pc_free_mem(&sc->sc_hw.devs[index].endpoint_pc[i]);
2778 static struct xhci_endpoint_ext *
2779 xhci_get_endpoint_ext(struct usb_device *udev, struct usb_endpoint_descriptor *edesc)
2781 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2782 struct xhci_endpoint_ext *pepext;
2783 struct usb_page_cache *pc;
2784 struct usb_page_search buf_ep;
2788 epno = edesc->bEndpointAddress;
2789 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL)
2792 epno = XHCI_EPNO2EPID(epno);
2794 index = udev->controller_slot_id;
2796 pc = &sc->sc_hw.devs[index].endpoint_pc[epno];
2798 usbd_get_page(pc, 0, &buf_ep);
2800 pepext = &sc->sc_hw.devs[index].endp[epno];
2801 pepext->page_cache = pc;
2802 pepext->trb = buf_ep.buffer;
2803 pepext->physaddr = buf_ep.physaddr;
2809 xhci_endpoint_doorbell(struct usb_xfer *xfer)
2811 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
2815 epno = xfer->endpointno;
2816 if (xfer->flags_int.control_xfr)
2819 epno = XHCI_EPNO2EPID(epno);
2820 index = xfer->xroot->udev->controller_slot_id;
2822 if (xfer->xroot->udev->flags.self_suspended == 0) {
2823 XWRITE4(sc, door, XHCI_DOORBELL(index),
2824 epno | XHCI_DB_SID_SET(xfer->stream_id));
2829 xhci_transfer_remove(struct usb_xfer *xfer, usb_error_t error)
2831 struct xhci_endpoint_ext *pepext;
2833 if (xfer->flags_int.bandwidth_reclaimed) {
2834 xfer->flags_int.bandwidth_reclaimed = 0;
2836 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2837 xfer->endpoint->edesc);
2839 pepext->trb_used[xfer->stream_id]--;
2841 pepext->xfer[xfer->qh_pos] = NULL;
2843 if (error && pepext->trb_running != 0) {
2844 pepext->trb_halted = 1;
2845 pepext->trb_running = 0;
2851 xhci_transfer_insert(struct usb_xfer *xfer)
2853 struct xhci_td *td_first;
2854 struct xhci_td *td_last;
2855 struct xhci_trb *trb_link;
2856 struct xhci_endpoint_ext *pepext;
2865 id = xfer->stream_id;
2867 /* check if already inserted */
2868 if (xfer->flags_int.bandwidth_reclaimed) {
2869 DPRINTFN(8, "Already in schedule\n");
2873 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2874 xfer->endpoint->edesc);
2876 td_first = xfer->td_transfer_first;
2877 td_last = xfer->td_transfer_last;
2878 addr = pepext->physaddr;
2880 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
2883 /* single buffered */
2887 /* multi buffered */
2888 trb_limit = (XHCI_MAX_TRANSFERS - 2);
2892 if (pepext->trb_used[id] >= trb_limit) {
2893 DPRINTFN(8, "Too many TDs queued.\n");
2894 return (USB_ERR_NOMEM);
2897 /* check for stopped condition, after putting transfer on interrupt queue */
2898 if (pepext->trb_running == 0) {
2899 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
2901 DPRINTFN(8, "Not running\n");
2903 /* start configuration */
2904 (void)usb_proc_msignal(USB_BUS_CONTROL_XFER_PROC(&sc->sc_bus),
2905 &sc->sc_config_msg[0], &sc->sc_config_msg[1]);
2909 pepext->trb_used[id]++;
2911 /* get current TRB index */
2912 i = pepext->trb_index[id];
2914 /* get next TRB index */
2917 /* the last entry of the ring is a hardcoded link TRB */
2918 if (inext >= (XHCI_MAX_TRANSFERS - 1))
2921 /* store next TRB index, before stream ID offset is added */
2922 pepext->trb_index[id] = inext;
2924 /* offset for stream */
2925 i += id * XHCI_MAX_TRANSFERS;
2926 inext += id * XHCI_MAX_TRANSFERS;
2928 /* compute terminating return address */
2929 addr += (inext * sizeof(struct xhci_trb));
2931 /* compute link TRB pointer */
2932 trb_link = td_last->td_trb + td_last->ntrb;
2934 /* update next pointer of last link TRB */
2935 trb_link->qwTrb0 = htole64(addr);
2936 trb_link->dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
2937 trb_link->dwTrb3 = htole32(XHCI_TRB_3_IOC_BIT |
2938 XHCI_TRB_3_CYCLE_BIT |
2939 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
2942 xhci_dump_trb(&td_last->td_trb[td_last->ntrb]);
2944 usb_pc_cpu_flush(td_last->page_cache);
2946 /* write ahead chain end marker */
2948 pepext->trb[inext].qwTrb0 = 0;
2949 pepext->trb[inext].dwTrb2 = 0;
2950 pepext->trb[inext].dwTrb3 = 0;
2952 /* update next pointer of link TRB */
2954 pepext->trb[i].qwTrb0 = htole64((uint64_t)td_first->td_self);
2955 pepext->trb[i].dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
2958 xhci_dump_trb(&pepext->trb[i]);
2960 usb_pc_cpu_flush(pepext->page_cache);
2962 /* toggle cycle bit which activates the transfer chain */
2964 pepext->trb[i].dwTrb3 = htole32(XHCI_TRB_3_CYCLE_BIT |
2965 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
2967 usb_pc_cpu_flush(pepext->page_cache);
2969 DPRINTF("qh_pos = %u\n", i);
2971 pepext->xfer[i] = xfer;
2975 xfer->flags_int.bandwidth_reclaimed = 1;
2977 xhci_endpoint_doorbell(xfer);
2983 xhci_root_intr(struct xhci_softc *sc)
2987 USB_BUS_LOCK_ASSERT(&sc->sc_bus);
2989 /* clear any old interrupt data */
2990 memset(sc->sc_hub_idata, 0, sizeof(sc->sc_hub_idata));
2992 for (i = 1; i <= sc->sc_noport; i++) {
2993 /* pick out CHANGE bits from the status register */
2994 if (XREAD4(sc, oper, XHCI_PORTSC(i)) & (
2995 XHCI_PS_CSC | XHCI_PS_PEC |
2996 XHCI_PS_OCC | XHCI_PS_WRC |
2997 XHCI_PS_PRC | XHCI_PS_PLC |
2999 sc->sc_hub_idata[i / 8] |= 1 << (i % 8);
3000 DPRINTF("port %d changed\n", i);
3003 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
3004 sizeof(sc->sc_hub_idata));
3007 /*------------------------------------------------------------------------*
3008 * xhci_device_done - XHCI done handler
3010 * NOTE: This function can be called two times in a row on
3011 * the same USB transfer. From close and from interrupt.
3012 *------------------------------------------------------------------------*/
3014 xhci_device_done(struct usb_xfer *xfer, usb_error_t error)
3016 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
3017 xfer, xfer->endpoint, error);
3019 /* remove transfer from HW queue */
3020 xhci_transfer_remove(xfer, error);
3022 /* dequeue transfer and start next transfer */
3023 usbd_transfer_done(xfer, error);
3026 /*------------------------------------------------------------------------*
3027 * XHCI data transfer support (generic type)
3028 *------------------------------------------------------------------------*/
3030 xhci_device_generic_open(struct usb_xfer *xfer)
3032 if (xfer->flags_int.isochronous_xfr) {
3033 switch (xfer->xroot->udev->speed) {
3034 case USB_SPEED_FULL:
3037 usb_hs_bandwidth_alloc(xfer);
3044 xhci_device_generic_close(struct usb_xfer *xfer)
3048 xhci_device_done(xfer, USB_ERR_CANCELLED);
3050 if (xfer->flags_int.isochronous_xfr) {
3051 switch (xfer->xroot->udev->speed) {
3052 case USB_SPEED_FULL:
3055 usb_hs_bandwidth_free(xfer);
3062 xhci_device_generic_multi_enter(struct usb_endpoint *ep,
3063 usb_stream_t stream_id, struct usb_xfer *enter_xfer)
3065 struct usb_xfer *xfer;
3067 /* check if there is a current transfer */
3068 xfer = ep->endpoint_q[stream_id].curr;
3073 * Check if the current transfer is started and then pickup
3074 * the next one, if any. Else wait for next start event due to
3075 * block on failure feature.
3077 if (!xfer->flags_int.bandwidth_reclaimed)
3080 xfer = TAILQ_FIRST(&ep->endpoint_q[stream_id].head);
3083 * In case of enter we have to consider that the
3084 * transfer is queued by the USB core after the enter
3093 /* try to multi buffer */
3094 xhci_transfer_insert(xfer);
3098 xhci_device_generic_enter(struct usb_xfer *xfer)
3102 /* setup TD's and QH */
3103 xhci_setup_generic_chain(xfer);
3105 xhci_device_generic_multi_enter(xfer->endpoint,
3106 xfer->stream_id, xfer);
3110 xhci_device_generic_start(struct usb_xfer *xfer)
3114 /* try to insert xfer on HW queue */
3115 xhci_transfer_insert(xfer);
3117 /* try to multi buffer */
3118 xhci_device_generic_multi_enter(xfer->endpoint,
3119 xfer->stream_id, NULL);
3121 /* add transfer last on interrupt queue */
3122 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
3124 /* start timeout, if any */
3125 if (xfer->timeout != 0)
3126 usbd_transfer_timeout_ms(xfer, &xhci_timeout, xfer->timeout);
3129 static const struct usb_pipe_methods xhci_device_generic_methods =
3131 .open = xhci_device_generic_open,
3132 .close = xhci_device_generic_close,
3133 .enter = xhci_device_generic_enter,
3134 .start = xhci_device_generic_start,
3137 /*------------------------------------------------------------------------*
3138 * xhci root HUB support
3139 *------------------------------------------------------------------------*
3140 * Simulate a hardware HUB by handling all the necessary requests.
3141 *------------------------------------------------------------------------*/
3143 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
3146 struct usb_device_descriptor xhci_devd =
3148 .bLength = sizeof(xhci_devd),
3149 .bDescriptorType = UDESC_DEVICE, /* type */
3150 HSETW(.bcdUSB, 0x0300), /* USB version */
3151 .bDeviceClass = UDCLASS_HUB, /* class */
3152 .bDeviceSubClass = UDSUBCLASS_HUB, /* subclass */
3153 .bDeviceProtocol = UDPROTO_SSHUB, /* protocol */
3154 .bMaxPacketSize = 9, /* max packet size */
3155 HSETW(.idVendor, 0x0000), /* vendor */
3156 HSETW(.idProduct, 0x0000), /* product */
3157 HSETW(.bcdDevice, 0x0100), /* device version */
3161 .bNumConfigurations = 1, /* # of configurations */
3165 struct xhci_bos_desc xhci_bosd = {
3167 .bLength = sizeof(xhci_bosd.bosd),
3168 .bDescriptorType = UDESC_BOS,
3169 HSETW(.wTotalLength, sizeof(xhci_bosd)),
3170 .bNumDeviceCaps = 3,
3173 .bLength = sizeof(xhci_bosd.usb2extd),
3174 .bDescriptorType = 1,
3175 .bDevCapabilityType = 2,
3176 .bmAttributes[0] = 2,
3179 .bLength = sizeof(xhci_bosd.usbdcd),
3180 .bDescriptorType = UDESC_DEVICE_CAPABILITY,
3181 .bDevCapabilityType = 3,
3182 .bmAttributes = 0, /* XXX */
3183 HSETW(.wSpeedsSupported, 0x000C),
3184 .bFunctionalitySupport = 8,
3185 .bU1DevExitLat = 255, /* dummy - not used */
3186 .wU2DevExitLat = { 0x00, 0x08 },
3189 .bLength = sizeof(xhci_bosd.cidd),
3190 .bDescriptorType = 1,
3191 .bDevCapabilityType = 4,
3193 .bContainerID = 0, /* XXX */
3198 struct xhci_config_desc xhci_confd = {
3200 .bLength = sizeof(xhci_confd.confd),
3201 .bDescriptorType = UDESC_CONFIG,
3202 .wTotalLength[0] = sizeof(xhci_confd),
3204 .bConfigurationValue = 1,
3205 .iConfiguration = 0,
3206 .bmAttributes = UC_SELF_POWERED,
3207 .bMaxPower = 0 /* max power */
3210 .bLength = sizeof(xhci_confd.ifcd),
3211 .bDescriptorType = UDESC_INTERFACE,
3213 .bInterfaceClass = UICLASS_HUB,
3214 .bInterfaceSubClass = UISUBCLASS_HUB,
3215 .bInterfaceProtocol = 0,
3218 .bLength = sizeof(xhci_confd.endpd),
3219 .bDescriptorType = UDESC_ENDPOINT,
3220 .bEndpointAddress = UE_DIR_IN | XHCI_INTR_ENDPT,
3221 .bmAttributes = UE_INTERRUPT,
3222 .wMaxPacketSize[0] = 2, /* max 15 ports */
3226 .bLength = sizeof(xhci_confd.endpcd),
3227 .bDescriptorType = UDESC_ENDPOINT_SS_COMP,
3234 struct usb_hub_ss_descriptor xhci_hubd = {
3235 .bLength = sizeof(xhci_hubd),
3236 .bDescriptorType = UDESC_SS_HUB,
3240 xhci_roothub_exec(struct usb_device *udev,
3241 struct usb_device_request *req, const void **pptr, uint16_t *plength)
3243 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3244 const char *str_ptr;
3255 USB_BUS_LOCK_ASSERT(&sc->sc_bus);
3258 ptr = (const void *)&sc->sc_hub_desc;
3262 value = UGETW(req->wValue);
3263 index = UGETW(req->wIndex);
3265 DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
3266 "wValue=0x%04x wIndex=0x%04x\n",
3267 req->bmRequestType, req->bRequest,
3268 UGETW(req->wLength), value, index);
3270 #define C(x,y) ((x) | ((y) << 8))
3271 switch (C(req->bRequest, req->bmRequestType)) {
3272 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
3273 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
3274 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
3276 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
3277 * for the integrated root hub.
3280 case C(UR_GET_CONFIG, UT_READ_DEVICE):
3282 sc->sc_hub_desc.temp[0] = sc->sc_conf;
3284 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
3285 switch (value >> 8) {
3287 if ((value & 0xff) != 0) {
3288 err = USB_ERR_IOERROR;
3291 len = sizeof(xhci_devd);
3292 ptr = (const void *)&xhci_devd;
3296 if ((value & 0xff) != 0) {
3297 err = USB_ERR_IOERROR;
3300 len = sizeof(xhci_bosd);
3301 ptr = (const void *)&xhci_bosd;
3305 if ((value & 0xff) != 0) {
3306 err = USB_ERR_IOERROR;
3309 len = sizeof(xhci_confd);
3310 ptr = (const void *)&xhci_confd;
3314 switch (value & 0xff) {
3315 case 0: /* Language table */
3319 case 1: /* Vendor */
3320 str_ptr = sc->sc_vendor;
3323 case 2: /* Product */
3324 str_ptr = "XHCI root HUB";
3332 len = usb_make_str_desc(
3333 sc->sc_hub_desc.temp,
3334 sizeof(sc->sc_hub_desc.temp),
3339 err = USB_ERR_IOERROR;
3343 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3345 sc->sc_hub_desc.temp[0] = 0;
3347 case C(UR_GET_STATUS, UT_READ_DEVICE):
3349 USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
3351 case C(UR_GET_STATUS, UT_READ_INTERFACE):
3352 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3354 USETW(sc->sc_hub_desc.stat.wStatus, 0);
3356 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3357 if (value >= XHCI_MAX_DEVICES) {
3358 err = USB_ERR_IOERROR;
3362 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
3363 if (value != 0 && value != 1) {
3364 err = USB_ERR_IOERROR;
3367 sc->sc_conf = value;
3369 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
3371 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
3372 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
3373 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3374 err = USB_ERR_IOERROR;
3376 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
3378 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
3381 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3383 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3384 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE\n");
3387 (index > sc->sc_noport)) {
3388 err = USB_ERR_IOERROR;
3391 port = XHCI_PORTSC(index);
3393 v = XREAD4(sc, oper, port);
3394 i = XHCI_PS_PLS_GET(v);
3395 v &= ~XHCI_PS_CLEAR;
3398 case UHF_C_BH_PORT_RESET:
3399 XWRITE4(sc, oper, port, v | XHCI_PS_WRC);
3401 case UHF_C_PORT_CONFIG_ERROR:
3402 XWRITE4(sc, oper, port, v | XHCI_PS_CEC);
3404 case UHF_C_PORT_SUSPEND:
3405 case UHF_C_PORT_LINK_STATE:
3406 XWRITE4(sc, oper, port, v | XHCI_PS_PLC);
3408 case UHF_C_PORT_CONNECTION:
3409 XWRITE4(sc, oper, port, v | XHCI_PS_CSC);
3411 case UHF_C_PORT_ENABLE:
3412 XWRITE4(sc, oper, port, v | XHCI_PS_PEC);
3414 case UHF_C_PORT_OVER_CURRENT:
3415 XWRITE4(sc, oper, port, v | XHCI_PS_OCC);
3417 case UHF_C_PORT_RESET:
3418 XWRITE4(sc, oper, port, v | XHCI_PS_PRC);
3420 case UHF_PORT_ENABLE:
3421 XWRITE4(sc, oper, port, v | XHCI_PS_PED);
3423 case UHF_PORT_POWER:
3424 XWRITE4(sc, oper, port, v & ~XHCI_PS_PP);
3426 case UHF_PORT_INDICATOR:
3427 XWRITE4(sc, oper, port, v & ~XHCI_PS_PIC_SET(3));
3429 case UHF_PORT_SUSPEND:
3433 XWRITE4(sc, oper, port, v |
3434 XHCI_PS_PLS_SET(0xF) | XHCI_PS_LWS);
3437 /* wait 20ms for resume sequence to complete */
3438 usb_pause_mtx(&sc->sc_bus.bus_lock, hz / 50);
3441 XWRITE4(sc, oper, port, v |
3442 XHCI_PS_PLS_SET(0) | XHCI_PS_LWS);
3445 err = USB_ERR_IOERROR;
3450 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3451 if ((value & 0xff) != 0) {
3452 err = USB_ERR_IOERROR;
3456 v = XREAD4(sc, capa, XHCI_HCSPARAMS0);
3458 sc->sc_hub_desc.hubd = xhci_hubd;
3460 sc->sc_hub_desc.hubd.bNbrPorts = sc->sc_noport;
3462 if (XHCI_HCS0_PPC(v))
3463 i = UHD_PWR_INDIVIDUAL;
3467 if (XHCI_HCS0_PIND(v))
3470 i |= UHD_OC_INDIVIDUAL;
3472 USETW(sc->sc_hub_desc.hubd.wHubCharacteristics, i);
3474 /* see XHCI section 5.4.9: */
3475 sc->sc_hub_desc.hubd.bPwrOn2PwrGood = 10;
3477 for (j = 1; j <= sc->sc_noport; j++) {
3479 v = XREAD4(sc, oper, XHCI_PORTSC(j));
3480 if (v & XHCI_PS_DR) {
3481 sc->sc_hub_desc.hubd.
3482 DeviceRemovable[j / 8] |= 1U << (j % 8);
3485 len = sc->sc_hub_desc.hubd.bLength;
3488 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3490 memset(sc->sc_hub_desc.temp, 0, 16);
3493 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3494 DPRINTFN(9, "UR_GET_STATUS i=%d\n", index);
3497 (index > sc->sc_noport)) {
3498 err = USB_ERR_IOERROR;
3502 v = XREAD4(sc, oper, XHCI_PORTSC(index));
3504 DPRINTFN(9, "port status=0x%08x\n", v);
3506 i = UPS_PORT_LINK_STATE_SET(XHCI_PS_PLS_GET(v));
3508 switch (XHCI_PS_SPEED_GET(v)) {
3510 i |= UPS_HIGH_SPEED;
3519 i |= UPS_OTHER_SPEED;
3523 if (v & XHCI_PS_CCS)
3524 i |= UPS_CURRENT_CONNECT_STATUS;
3525 if (v & XHCI_PS_PED)
3526 i |= UPS_PORT_ENABLED;
3527 if (v & XHCI_PS_OCA)
3528 i |= UPS_OVERCURRENT_INDICATOR;
3531 if (v & XHCI_PS_PP) {
3533 * The USB 3.0 RH is using the
3534 * USB 2.0's power bit
3536 i |= UPS_PORT_POWER;
3538 USETW(sc->sc_hub_desc.ps.wPortStatus, i);
3541 if (v & XHCI_PS_CSC)
3542 i |= UPS_C_CONNECT_STATUS;
3543 if (v & XHCI_PS_PEC)
3544 i |= UPS_C_PORT_ENABLED;
3545 if (v & XHCI_PS_OCC)
3546 i |= UPS_C_OVERCURRENT_INDICATOR;
3547 if (v & XHCI_PS_WRC)
3548 i |= UPS_C_BH_PORT_RESET;
3549 if (v & XHCI_PS_PRC)
3550 i |= UPS_C_PORT_RESET;
3551 if (v & XHCI_PS_PLC)
3552 i |= UPS_C_PORT_LINK_STATE;
3553 if (v & XHCI_PS_CEC)
3554 i |= UPS_C_PORT_CONFIG_ERROR;
3556 USETW(sc->sc_hub_desc.ps.wPortChange, i);
3557 len = sizeof(sc->sc_hub_desc.ps);
3560 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3561 err = USB_ERR_IOERROR;
3564 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3567 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3573 (index > sc->sc_noport)) {
3574 err = USB_ERR_IOERROR;
3578 port = XHCI_PORTSC(index);
3579 v = XREAD4(sc, oper, port) & ~XHCI_PS_CLEAR;
3582 case UHF_PORT_U1_TIMEOUT:
3583 if (XHCI_PS_SPEED_GET(v) != 4) {
3584 err = USB_ERR_IOERROR;
3587 port = XHCI_PORTPMSC(index);
3588 v = XREAD4(sc, oper, port);
3589 v &= ~XHCI_PM3_U1TO_SET(0xFF);
3590 v |= XHCI_PM3_U1TO_SET(i);
3591 XWRITE4(sc, oper, port, v);
3593 case UHF_PORT_U2_TIMEOUT:
3594 if (XHCI_PS_SPEED_GET(v) != 4) {
3595 err = USB_ERR_IOERROR;
3598 port = XHCI_PORTPMSC(index);
3599 v = XREAD4(sc, oper, port);
3600 v &= ~XHCI_PM3_U2TO_SET(0xFF);
3601 v |= XHCI_PM3_U2TO_SET(i);
3602 XWRITE4(sc, oper, port, v);
3604 case UHF_BH_PORT_RESET:
3605 XWRITE4(sc, oper, port, v | XHCI_PS_WPR);
3607 case UHF_PORT_LINK_STATE:
3608 XWRITE4(sc, oper, port, v |
3609 XHCI_PS_PLS_SET(i) | XHCI_PS_LWS);
3610 /* 4ms settle time */
3611 usb_pause_mtx(&sc->sc_bus.bus_lock, hz / 250);
3613 case UHF_PORT_ENABLE:
3614 DPRINTFN(3, "set port enable %d\n", index);
3616 case UHF_PORT_SUSPEND:
3617 DPRINTFN(6, "suspend port %u (LPM=%u)\n", index, i);
3618 j = XHCI_PS_SPEED_GET(v);
3619 if ((j < 1) || (j > 3)) {
3620 /* non-supported speed */
3621 err = USB_ERR_IOERROR;
3624 XWRITE4(sc, oper, port, v |
3625 XHCI_PS_PLS_SET(i ? 2 /* LPM */ : 3) | XHCI_PS_LWS);
3627 case UHF_PORT_RESET:
3628 DPRINTFN(6, "reset port %d\n", index);
3629 XWRITE4(sc, oper, port, v | XHCI_PS_PR);
3631 case UHF_PORT_POWER:
3632 DPRINTFN(3, "set port power %d\n", index);
3633 XWRITE4(sc, oper, port, v | XHCI_PS_PP);
3636 DPRINTFN(3, "set port test %d\n", index);
3638 case UHF_PORT_INDICATOR:
3639 DPRINTFN(3, "set port indicator %d\n", index);
3641 v &= ~XHCI_PS_PIC_SET(3);
3642 v |= XHCI_PS_PIC_SET(1);
3644 XWRITE4(sc, oper, port, v);
3647 err = USB_ERR_IOERROR;
3652 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
3653 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
3654 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
3655 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
3658 err = USB_ERR_IOERROR;
3668 xhci_xfer_setup(struct usb_setup_params *parm)
3670 struct usb_page_search page_info;
3671 struct usb_page_cache *pc;
3672 struct xhci_softc *sc;
3673 struct usb_xfer *xfer;
3678 sc = XHCI_BUS2SC(parm->udev->bus);
3679 xfer = parm->curr_xfer;
3682 * The proof for the "ntd" formula is illustrated like this:
3684 * +------------------------------------+
3688 * | | xxx | x | frm 0 |
3690 * | | xxx | xx | frm 1 |
3693 * +------------------------------------+
3695 * "xxx" means a completely full USB transfer descriptor
3697 * "x" and "xx" means a short USB packet
3699 * For the remainder of an USB transfer modulo
3700 * "max_data_length" we need two USB transfer descriptors.
3701 * One to transfer the remaining data and one to finalise with
3702 * a zero length packet in case the "force_short_xfer" flag is
3703 * set. We only need two USB transfer descriptors in the case
3704 * where the transfer length of the first one is a factor of
3705 * "max_frame_size". The rest of the needed USB transfer
3706 * descriptors is given by the buffer size divided by the
3707 * maximum data payload.
3709 parm->hc_max_packet_size = 0x400;
3710 parm->hc_max_packet_count = 16 * 3;
3711 parm->hc_max_frame_size = XHCI_TD_PAYLOAD_MAX;
3713 xfer->flags_int.bdma_enable = 1;
3715 usbd_transfer_setup_sub(parm);
3717 if (xfer->flags_int.isochronous_xfr) {
3718 ntd = ((1 * xfer->nframes)
3719 + (xfer->max_data_length / xfer->max_hc_frame_size));
3720 } else if (xfer->flags_int.control_xfr) {
3721 ntd = ((2 * xfer->nframes) + 1 /* STATUS */
3722 + (xfer->max_data_length / xfer->max_hc_frame_size));
3724 ntd = ((2 * xfer->nframes)
3725 + (xfer->max_data_length / xfer->max_hc_frame_size));
3734 * Allocate queue heads and transfer descriptors
3738 if (usbd_transfer_setup_sub_malloc(
3739 parm, &pc, sizeof(struct xhci_td),
3740 XHCI_TD_ALIGN, ntd)) {
3741 parm->err = USB_ERR_NOMEM;
3745 for (n = 0; n != ntd; n++) {
3748 usbd_get_page(pc + n, 0, &page_info);
3750 td = page_info.buffer;
3753 td->td_self = page_info.physaddr;
3754 td->obj_next = last_obj;
3755 td->page_cache = pc + n;
3759 usb_pc_cpu_flush(pc + n);
3762 xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
3764 if (!xfer->flags_int.curr_dma_set) {
3765 xfer->flags_int.curr_dma_set = 1;
3771 xhci_configure_reset_endpoint(struct usb_xfer *xfer)
3773 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
3774 struct usb_page_search buf_inp;
3775 struct usb_device *udev;
3776 struct xhci_endpoint_ext *pepext;
3777 struct usb_endpoint_descriptor *edesc;
3778 struct usb_page_cache *pcinp;
3780 usb_stream_t stream_id;
3784 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
3785 xfer->endpoint->edesc);
3787 udev = xfer->xroot->udev;
3788 index = udev->controller_slot_id;
3790 pcinp = &sc->sc_hw.devs[index].input_pc;
3792 usbd_get_page(pcinp, 0, &buf_inp);
3794 edesc = xfer->endpoint->edesc;
3796 epno = edesc->bEndpointAddress;
3797 stream_id = xfer->stream_id;
3799 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL)
3802 epno = XHCI_EPNO2EPID(epno);
3805 return (USB_ERR_NO_PIPE); /* invalid */
3809 /* configure endpoint */
3811 err = xhci_configure_endpoint_by_xfer(xfer);
3814 XHCI_CMD_UNLOCK(sc);
3819 * Get the endpoint into the stopped state according to the
3820 * endpoint context state diagram in the XHCI specification:
3823 err = xhci_cmd_stop_ep(sc, 0, epno, index);
3826 DPRINTF("Could not stop endpoint %u\n", epno);
3828 err = xhci_cmd_reset_ep(sc, 0, epno, index);
3831 DPRINTF("Could not reset endpoint %u\n", epno);
3833 err = xhci_cmd_set_tr_dequeue_ptr(sc,
3834 (pepext->physaddr + (stream_id * sizeof(struct xhci_trb) *
3835 XHCI_MAX_TRANSFERS)) | XHCI_EPCTX_2_DCS_SET(1),
3836 stream_id, epno, index);
3839 DPRINTF("Could not set dequeue ptr for endpoint %u\n", epno);
3842 * Get the endpoint into the running state according to the
3843 * endpoint context state diagram in the XHCI specification:
3846 xhci_configure_mask(udev, (1U << epno) | 1U, 0);
3848 err = xhci_cmd_evaluate_ctx(sc, buf_inp.physaddr, index);
3851 DPRINTF("Could not configure endpoint %u\n", epno);
3853 err = xhci_cmd_configure_ep(sc, buf_inp.physaddr, 0, index);
3856 DPRINTF("Could not configure endpoint %u\n", epno);
3858 XHCI_CMD_UNLOCK(sc);
3864 xhci_xfer_unsetup(struct usb_xfer *xfer)
3870 xhci_start_dma_delay(struct usb_xfer *xfer)
3872 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
3874 /* put transfer on interrupt queue (again) */
3875 usbd_transfer_enqueue(&sc->sc_bus.intr_q, xfer);
3877 (void)usb_proc_msignal(USB_BUS_CONTROL_XFER_PROC(&sc->sc_bus),
3878 &sc->sc_config_msg[0], &sc->sc_config_msg[1]);
3882 xhci_configure_msg(struct usb_proc_msg *pm)
3884 struct xhci_softc *sc;
3885 struct xhci_endpoint_ext *pepext;
3886 struct usb_xfer *xfer;
3888 sc = XHCI_BUS2SC(((struct usb_bus_msg *)pm)->bus);
3891 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3893 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
3894 xfer->endpoint->edesc);
3896 if ((pepext->trb_halted != 0) ||
3897 (pepext->trb_running == 0)) {
3901 /* clear halted and running */
3902 pepext->trb_halted = 0;
3903 pepext->trb_running = 0;
3905 /* nuke remaining buffered transfers */
3907 for (i = 0; i != (XHCI_MAX_TRANSFERS *
3908 XHCI_MAX_STREAMS); i++) {
3910 * NOTE: We need to use the timeout
3911 * error code here else existing
3912 * isochronous clients can get
3915 if (pepext->xfer[i] != NULL) {
3916 xhci_device_done(pepext->xfer[i],
3922 * NOTE: The USB transfer cannot vanish in
3926 USB_BUS_UNLOCK(&sc->sc_bus);
3928 xhci_configure_reset_endpoint(xfer);
3930 USB_BUS_LOCK(&sc->sc_bus);
3932 /* check if halted is still cleared */
3933 if (pepext->trb_halted == 0) {
3934 pepext->trb_running = 1;
3935 memset(pepext->trb_index, 0,
3936 sizeof(pepext->trb_index));
3941 if (xfer->flags_int.did_dma_delay) {
3943 /* remove transfer from interrupt queue (again) */
3944 usbd_transfer_dequeue(xfer);
3946 /* we are finally done */
3947 usb_dma_delay_done_cb(xfer);
3949 /* queue changed - restart */
3954 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3956 /* try to insert xfer on HW queue */
3957 xhci_transfer_insert(xfer);
3959 /* try to multi buffer */
3960 xhci_device_generic_multi_enter(xfer->endpoint,
3961 xfer->stream_id, NULL);
3966 xhci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
3967 struct usb_endpoint *ep)
3969 struct xhci_endpoint_ext *pepext;
3971 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d\n",
3972 ep, udev->address, edesc->bEndpointAddress, udev->flags.usb_mode);
3974 if (udev->parent_hub == NULL) {
3975 /* root HUB has special endpoint handling */
3979 ep->methods = &xhci_device_generic_methods;
3981 pepext = xhci_get_endpoint_ext(udev, edesc);
3983 USB_BUS_LOCK(udev->bus);
3984 pepext->trb_halted = 1;
3985 pepext->trb_running = 0;
3986 USB_BUS_UNLOCK(udev->bus);
3990 xhci_ep_uninit(struct usb_device *udev, struct usb_endpoint *ep)
3996 xhci_ep_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
3998 struct xhci_endpoint_ext *pepext;
4002 if (udev->flags.usb_mode != USB_MODE_HOST) {
4006 if (udev->parent_hub == NULL) {
4007 /* root HUB has special endpoint handling */
4011 pepext = xhci_get_endpoint_ext(udev, ep->edesc);
4013 USB_BUS_LOCK(udev->bus);
4014 pepext->trb_halted = 1;
4015 pepext->trb_running = 0;
4016 USB_BUS_UNLOCK(udev->bus);
4020 xhci_device_init(struct usb_device *udev)
4022 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
4026 /* no init for root HUB */
4027 if (udev->parent_hub == NULL)
4032 /* set invalid default */
4034 udev->controller_slot_id = sc->sc_noslot + 1;
4036 /* try to get a new slot ID from the XHCI */
4038 err = xhci_cmd_enable_slot(sc, &temp);
4041 XHCI_CMD_UNLOCK(sc);
4045 if (temp > sc->sc_noslot) {
4046 XHCI_CMD_UNLOCK(sc);
4047 return (USB_ERR_BAD_ADDRESS);
4050 if (sc->sc_hw.devs[temp].state != XHCI_ST_DISABLED) {
4051 DPRINTF("slot %u already allocated.\n", temp);
4052 XHCI_CMD_UNLOCK(sc);
4053 return (USB_ERR_BAD_ADDRESS);
4056 /* store slot ID for later reference */
4058 udev->controller_slot_id = temp;
4060 /* reset data structure */
4062 memset(&sc->sc_hw.devs[temp], 0, sizeof(sc->sc_hw.devs[0]));
4064 /* set mark slot allocated */
4066 sc->sc_hw.devs[temp].state = XHCI_ST_ENABLED;
4068 err = xhci_alloc_device_ext(udev);
4070 XHCI_CMD_UNLOCK(sc);
4072 /* get device into default state */
4075 err = xhci_set_address(udev, NULL, 0);
4081 xhci_device_uninit(struct usb_device *udev)
4083 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
4086 /* no init for root HUB */
4087 if (udev->parent_hub == NULL)
4092 index = udev->controller_slot_id;
4094 if (index <= sc->sc_noslot) {
4095 xhci_cmd_disable_slot(sc, index);
4096 sc->sc_hw.devs[index].state = XHCI_ST_DISABLED;
4098 /* free device extension */
4099 xhci_free_device_ext(udev);
4102 XHCI_CMD_UNLOCK(sc);
4106 xhci_get_dma_delay(struct usb_device *udev, uint32_t *pus)
4109 * Wait until the hardware has finished any possible use of
4110 * the transfer descriptor(s)
4112 *pus = 2048; /* microseconds */
4116 xhci_device_resume(struct usb_device *udev)
4118 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
4125 /* check for root HUB */
4126 if (udev->parent_hub == NULL)
4129 index = udev->controller_slot_id;
4133 /* blindly resume all endpoints */
4135 USB_BUS_LOCK(udev->bus);
4137 for (n = 1; n != XHCI_MAX_ENDPOINTS; n++) {
4138 for (p = 0; p != XHCI_MAX_STREAMS; p++) {
4139 XWRITE4(sc, door, XHCI_DOORBELL(index),
4140 n | XHCI_DB_SID_SET(p));
4144 USB_BUS_UNLOCK(udev->bus);
4146 XHCI_CMD_UNLOCK(sc);
4150 xhci_device_suspend(struct usb_device *udev)
4152 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
4159 /* check for root HUB */
4160 if (udev->parent_hub == NULL)
4163 index = udev->controller_slot_id;
4167 /* blindly suspend all endpoints */
4169 for (n = 1; n != XHCI_MAX_ENDPOINTS; n++) {
4170 err = xhci_cmd_stop_ep(sc, 1, n, index);
4172 DPRINTF("Failed to suspend endpoint "
4173 "%u on slot %u (ignored).\n", n, index);
4177 XHCI_CMD_UNLOCK(sc);
4181 xhci_set_hw_power(struct usb_bus *bus)
4187 xhci_device_state_change(struct usb_device *udev)
4189 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
4190 struct usb_page_search buf_inp;
4194 /* check for root HUB */
4195 if (udev->parent_hub == NULL)
4198 index = udev->controller_slot_id;
4202 if (usb_get_device_state(udev) == USB_STATE_CONFIGURED) {
4203 err = uhub_query_info(udev, &sc->sc_hw.devs[index].nports,
4204 &sc->sc_hw.devs[index].tt);
4206 sc->sc_hw.devs[index].nports = 0;
4211 switch (usb_get_device_state(udev)) {
4212 case USB_STATE_POWERED:
4213 if (sc->sc_hw.devs[index].state == XHCI_ST_DEFAULT)
4216 /* set default state */
4217 sc->sc_hw.devs[index].state = XHCI_ST_DEFAULT;
4219 /* reset number of contexts */
4220 sc->sc_hw.devs[index].context_num = 0;
4222 err = xhci_cmd_reset_dev(sc, index);
4225 DPRINTF("Device reset failed "
4226 "for slot %u.\n", index);
4230 case USB_STATE_ADDRESSED:
4231 if (sc->sc_hw.devs[index].state == XHCI_ST_ADDRESSED)
4234 sc->sc_hw.devs[index].state = XHCI_ST_ADDRESSED;
4236 err = xhci_cmd_configure_ep(sc, 0, 1, index);
4239 DPRINTF("Failed to deconfigure "
4240 "slot %u.\n", index);
4244 case USB_STATE_CONFIGURED:
4245 if (sc->sc_hw.devs[index].state == XHCI_ST_CONFIGURED)
4248 /* set configured state */
4249 sc->sc_hw.devs[index].state = XHCI_ST_CONFIGURED;
4251 /* reset number of contexts */
4252 sc->sc_hw.devs[index].context_num = 0;
4254 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
4256 xhci_configure_mask(udev, 3, 0);
4258 err = xhci_configure_device(udev);
4260 DPRINTF("Could not configure device "
4261 "at slot %u.\n", index);
4264 err = xhci_cmd_evaluate_ctx(sc, buf_inp.physaddr, index);
4266 DPRINTF("Could not evaluate device "
4267 "context at slot %u.\n", index);
4274 XHCI_CMD_UNLOCK(sc);
4278 xhci_set_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep,
4282 case USB_EP_MODE_DEFAULT:
4284 case USB_EP_MODE_STREAMS:
4285 if (xhcistreams == 0 ||
4286 (ep->edesc->bmAttributes & UE_XFERTYPE) != UE_BULK ||
4287 udev->speed != USB_SPEED_SUPER)
4288 return (USB_ERR_INVAL);
4291 return (USB_ERR_INVAL);
4295 static const struct usb_bus_methods xhci_bus_methods = {
4296 .endpoint_init = xhci_ep_init,
4297 .endpoint_uninit = xhci_ep_uninit,
4298 .xfer_setup = xhci_xfer_setup,
4299 .xfer_unsetup = xhci_xfer_unsetup,
4300 .get_dma_delay = xhci_get_dma_delay,
4301 .device_init = xhci_device_init,
4302 .device_uninit = xhci_device_uninit,
4303 .device_resume = xhci_device_resume,
4304 .device_suspend = xhci_device_suspend,
4305 .set_hw_power = xhci_set_hw_power,
4306 .roothub_exec = xhci_roothub_exec,
4307 .xfer_poll = xhci_do_poll,
4308 .start_dma_delay = xhci_start_dma_delay,
4309 .set_address = xhci_set_address,
4310 .clear_stall = xhci_ep_clear_stall,
4311 .device_state_change = xhci_device_state_change,
4312 .set_hw_power_sleep = xhci_set_hw_power_sleep,
4313 .set_endpoint_mode = xhci_set_endpoint_mode,