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 #ifdef USB_GLOBAL_INCLUDE_FILE
44 #include USB_GLOBAL_INCLUDE_FILE
46 #include <sys/stdint.h>
47 #include <sys/stddef.h>
48 #include <sys/param.h>
49 #include <sys/queue.h>
50 #include <sys/types.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
54 #include <sys/module.h>
56 #include <sys/mutex.h>
57 #include <sys/condvar.h>
58 #include <sys/sysctl.h>
60 #include <sys/unistd.h>
61 #include <sys/callout.h>
62 #include <sys/malloc.h>
65 #include <dev/usb/usb.h>
66 #include <dev/usb/usbdi.h>
68 #define USB_DEBUG_VAR xhcidebug
70 #include <dev/usb/usb_core.h>
71 #include <dev/usb/usb_debug.h>
72 #include <dev/usb/usb_busdma.h>
73 #include <dev/usb/usb_process.h>
74 #include <dev/usb/usb_transfer.h>
75 #include <dev/usb/usb_device.h>
76 #include <dev/usb/usb_hub.h>
77 #include <dev/usb/usb_util.h>
79 #include <dev/usb/usb_controller.h>
80 #include <dev/usb/usb_bus.h>
81 #endif /* USB_GLOBAL_INCLUDE_FILE */
83 #include <dev/usb/controller/xhci.h>
84 #include <dev/usb/controller/xhcireg.h>
86 #define XHCI_BUS2SC(bus) \
87 ((struct xhci_softc *)(((uint8_t *)(bus)) - \
88 ((uint8_t *)&(((struct xhci_softc *)0)->sc_bus))))
90 static SYSCTL_NODE(_hw_usb, OID_AUTO, xhci, CTLFLAG_RW, 0, "USB XHCI");
92 static int xhcistreams;
93 SYSCTL_INT(_hw_usb_xhci, OID_AUTO, streams, CTLFLAG_RWTUN,
94 &xhcistreams, 0, "Set to enable streams mode support");
99 static int xhcipolling;
100 static int xhcidma32;
101 static int xhcictlstep;
103 SYSCTL_INT(_hw_usb_xhci, OID_AUTO, debug, CTLFLAG_RWTUN,
104 &xhcidebug, 0, "Debug level");
105 SYSCTL_INT(_hw_usb_xhci, OID_AUTO, xhci_port_route, CTLFLAG_RWTUN,
106 &xhciroute, 0, "Routing bitmap for switching EHCI ports to the XHCI controller");
107 SYSCTL_INT(_hw_usb_xhci, OID_AUTO, use_polling, CTLFLAG_RWTUN,
108 &xhcipolling, 0, "Set to enable software interrupt polling for the XHCI controller");
109 SYSCTL_INT(_hw_usb_xhci, OID_AUTO, dma32, CTLFLAG_RWTUN,
110 &xhcidma32, 0, "Set to only use 32-bit DMA for the XHCI controller");
111 SYSCTL_INT(_hw_usb_xhci, OID_AUTO, ctlstep, CTLFLAG_RWTUN,
112 &xhcictlstep, 0, "Set to enable control endpoint status stage stepping");
116 #define xhcictlstep 0
119 #define XHCI_INTR_ENDPT 1
121 struct xhci_std_temp {
122 struct xhci_softc *sc;
123 struct usb_page_cache *pc;
125 struct xhci_td *td_next;
128 uint32_t max_packet_size;
140 uint8_t do_isoc_sync;
143 static void xhci_do_poll(struct usb_bus *);
144 static void xhci_device_done(struct usb_xfer *, usb_error_t);
145 static void xhci_root_intr(struct xhci_softc *);
146 static void xhci_free_device_ext(struct usb_device *);
147 static struct xhci_endpoint_ext *xhci_get_endpoint_ext(struct usb_device *,
148 struct usb_endpoint_descriptor *);
149 static usb_proc_callback_t xhci_configure_msg;
150 static usb_error_t xhci_configure_device(struct usb_device *);
151 static usb_error_t xhci_configure_endpoint(struct usb_device *,
152 struct usb_endpoint_descriptor *, struct xhci_endpoint_ext *,
153 uint16_t, uint8_t, uint8_t, uint8_t, uint16_t, uint16_t,
155 static usb_error_t xhci_configure_mask(struct usb_device *,
157 static usb_error_t xhci_cmd_evaluate_ctx(struct xhci_softc *,
159 static void xhci_endpoint_doorbell(struct usb_xfer *);
160 static void xhci_ctx_set_le32(struct xhci_softc *sc, volatile uint32_t *ptr, uint32_t val);
161 static uint32_t xhci_ctx_get_le32(struct xhci_softc *sc, volatile uint32_t *ptr);
162 static void xhci_ctx_set_le64(struct xhci_softc *sc, volatile uint64_t *ptr, uint64_t val);
164 static uint64_t xhci_ctx_get_le64(struct xhci_softc *sc, volatile uint64_t *ptr);
167 static const struct usb_bus_methods xhci_bus_methods;
171 xhci_dump_trb(struct xhci_trb *trb)
173 DPRINTFN(5, "trb = %p\n", trb);
174 DPRINTFN(5, "qwTrb0 = 0x%016llx\n", (long long)le64toh(trb->qwTrb0));
175 DPRINTFN(5, "dwTrb2 = 0x%08x\n", le32toh(trb->dwTrb2));
176 DPRINTFN(5, "dwTrb3 = 0x%08x\n", le32toh(trb->dwTrb3));
180 xhci_dump_endpoint(struct xhci_softc *sc, struct xhci_endp_ctx *pep)
182 DPRINTFN(5, "pep = %p\n", pep);
183 DPRINTFN(5, "dwEpCtx0=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx0));
184 DPRINTFN(5, "dwEpCtx1=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx1));
185 DPRINTFN(5, "qwEpCtx2=0x%016llx\n", (long long)xhci_ctx_get_le64(sc, &pep->qwEpCtx2));
186 DPRINTFN(5, "dwEpCtx4=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx4));
187 DPRINTFN(5, "dwEpCtx5=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx5));
188 DPRINTFN(5, "dwEpCtx6=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx6));
189 DPRINTFN(5, "dwEpCtx7=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx7));
193 xhci_dump_device(struct xhci_softc *sc, struct xhci_slot_ctx *psl)
195 DPRINTFN(5, "psl = %p\n", psl);
196 DPRINTFN(5, "dwSctx0=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx0));
197 DPRINTFN(5, "dwSctx1=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx1));
198 DPRINTFN(5, "dwSctx2=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx2));
199 DPRINTFN(5, "dwSctx3=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx3));
204 xhci_use_polling(void)
207 return (xhcipolling != 0);
214 xhci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
216 struct xhci_softc *sc = XHCI_BUS2SC(bus);
219 cb(bus, &sc->sc_hw.root_pc, &sc->sc_hw.root_pg,
220 sizeof(struct xhci_hw_root), XHCI_PAGE_SIZE);
222 cb(bus, &sc->sc_hw.ctx_pc, &sc->sc_hw.ctx_pg,
223 sizeof(struct xhci_dev_ctx_addr), XHCI_PAGE_SIZE);
225 for (i = 0; i != sc->sc_noscratch; i++) {
226 cb(bus, &sc->sc_hw.scratch_pc[i], &sc->sc_hw.scratch_pg[i],
227 XHCI_PAGE_SIZE, XHCI_PAGE_SIZE);
232 xhci_ctx_set_le32(struct xhci_softc *sc, volatile uint32_t *ptr, uint32_t val)
234 if (sc->sc_ctx_is_64_byte) {
236 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
237 /* all contexts are initially 32-bytes */
238 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
239 ptr = (volatile uint32_t *)(((volatile uint8_t *)ptr) + offset);
245 xhci_ctx_get_le32(struct xhci_softc *sc, volatile uint32_t *ptr)
247 if (sc->sc_ctx_is_64_byte) {
249 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
250 /* all contexts are initially 32-bytes */
251 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
252 ptr = (volatile uint32_t *)(((volatile uint8_t *)ptr) + offset);
254 return (le32toh(*ptr));
258 xhci_ctx_set_le64(struct xhci_softc *sc, volatile uint64_t *ptr, uint64_t val)
260 if (sc->sc_ctx_is_64_byte) {
262 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
263 /* all contexts are initially 32-bytes */
264 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
265 ptr = (volatile uint64_t *)(((volatile uint8_t *)ptr) + offset);
272 xhci_ctx_get_le64(struct xhci_softc *sc, volatile uint64_t *ptr)
274 if (sc->sc_ctx_is_64_byte) {
276 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
277 /* all contexts are initially 32-bytes */
278 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
279 ptr = (volatile uint64_t *)(((volatile uint8_t *)ptr) + offset);
281 return (le64toh(*ptr));
286 xhci_reset_command_queue_locked(struct xhci_softc *sc)
288 struct usb_page_search buf_res;
289 struct xhci_hw_root *phwr;
295 temp = XREAD4(sc, oper, XHCI_CRCR_LO);
296 if (temp & XHCI_CRCR_LO_CRR) {
297 DPRINTF("Command ring running\n");
298 temp &= ~(XHCI_CRCR_LO_CS | XHCI_CRCR_LO_CA);
301 * Try to abort the last command as per section
302 * 4.6.1.2 "Aborting a Command" of the XHCI
306 /* stop and cancel */
307 XWRITE4(sc, oper, XHCI_CRCR_LO, temp | XHCI_CRCR_LO_CS);
308 XWRITE4(sc, oper, XHCI_CRCR_HI, 0);
310 XWRITE4(sc, oper, XHCI_CRCR_LO, temp | XHCI_CRCR_LO_CA);
311 XWRITE4(sc, oper, XHCI_CRCR_HI, 0);
314 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 4);
316 /* check if command ring is still running */
317 temp = XREAD4(sc, oper, XHCI_CRCR_LO);
318 if (temp & XHCI_CRCR_LO_CRR) {
319 DPRINTF("Comand ring still running\n");
320 return (USB_ERR_IOERROR);
324 /* reset command ring */
325 sc->sc_command_ccs = 1;
326 sc->sc_command_idx = 0;
328 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
330 /* set up command ring control base address */
331 addr = buf_res.physaddr;
332 phwr = buf_res.buffer;
333 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_commands[0];
335 DPRINTF("CRCR=0x%016llx\n", (unsigned long long)addr);
337 memset(phwr->hwr_commands, 0, sizeof(phwr->hwr_commands));
338 phwr->hwr_commands[XHCI_MAX_COMMANDS - 1].qwTrb0 = htole64(addr);
340 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
342 XWRITE4(sc, oper, XHCI_CRCR_LO, ((uint32_t)addr) | XHCI_CRCR_LO_RCS);
343 XWRITE4(sc, oper, XHCI_CRCR_HI, (uint32_t)(addr >> 32));
349 xhci_start_controller(struct xhci_softc *sc)
351 struct usb_page_search buf_res;
352 struct xhci_hw_root *phwr;
353 struct xhci_dev_ctx_addr *pdctxa;
361 sc->sc_event_ccs = 1;
362 sc->sc_event_idx = 0;
363 sc->sc_command_ccs = 1;
364 sc->sc_command_idx = 0;
366 err = xhci_reset_controller(sc);
370 /* set up number of device slots */
371 DPRINTF("CONFIG=0x%08x -> 0x%08x\n",
372 XREAD4(sc, oper, XHCI_CONFIG), sc->sc_noslot);
374 XWRITE4(sc, oper, XHCI_CONFIG, sc->sc_noslot);
376 temp = XREAD4(sc, oper, XHCI_USBSTS);
378 /* clear interrupts */
379 XWRITE4(sc, oper, XHCI_USBSTS, temp);
380 /* disable all device notifications */
381 XWRITE4(sc, oper, XHCI_DNCTRL, 0);
383 /* set up device context base address */
384 usbd_get_page(&sc->sc_hw.ctx_pc, 0, &buf_res);
385 pdctxa = buf_res.buffer;
386 memset(pdctxa, 0, sizeof(*pdctxa));
388 addr = buf_res.physaddr;
389 addr += (uintptr_t)&((struct xhci_dev_ctx_addr *)0)->qwSpBufPtr[0];
391 /* slot 0 points to the table of scratchpad pointers */
392 pdctxa->qwBaaDevCtxAddr[0] = htole64(addr);
394 for (i = 0; i != sc->sc_noscratch; i++) {
395 struct usb_page_search buf_scp;
396 usbd_get_page(&sc->sc_hw.scratch_pc[i], 0, &buf_scp);
397 pdctxa->qwSpBufPtr[i] = htole64((uint64_t)buf_scp.physaddr);
400 addr = buf_res.physaddr;
402 XWRITE4(sc, oper, XHCI_DCBAAP_LO, (uint32_t)addr);
403 XWRITE4(sc, oper, XHCI_DCBAAP_HI, (uint32_t)(addr >> 32));
404 XWRITE4(sc, oper, XHCI_DCBAAP_LO, (uint32_t)addr);
405 XWRITE4(sc, oper, XHCI_DCBAAP_HI, (uint32_t)(addr >> 32));
407 /* set up event table size */
408 DPRINTF("ERSTSZ=0x%08x -> 0x%08x\n",
409 XREAD4(sc, runt, XHCI_ERSTSZ(0)), sc->sc_erst_max);
411 XWRITE4(sc, runt, XHCI_ERSTSZ(0), XHCI_ERSTS_SET(sc->sc_erst_max));
413 /* set up interrupt rate */
414 XWRITE4(sc, runt, XHCI_IMOD(0), sc->sc_imod_default);
416 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
418 phwr = buf_res.buffer;
419 addr = buf_res.physaddr;
420 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_events[0];
422 /* reset hardware root structure */
423 memset(phwr, 0, sizeof(*phwr));
425 phwr->hwr_ring_seg[0].qwEvrsTablePtr = htole64(addr);
426 phwr->hwr_ring_seg[0].dwEvrsTableSize = htole32(XHCI_MAX_EVENTS);
428 DPRINTF("ERDP(0)=0x%016llx\n", (unsigned long long)addr);
430 XWRITE4(sc, runt, XHCI_ERDP_LO(0), (uint32_t)addr);
431 XWRITE4(sc, runt, XHCI_ERDP_HI(0), (uint32_t)(addr >> 32));
433 addr = buf_res.physaddr;
435 DPRINTF("ERSTBA(0)=0x%016llx\n", (unsigned long long)addr);
437 XWRITE4(sc, runt, XHCI_ERSTBA_LO(0), (uint32_t)addr);
438 XWRITE4(sc, runt, XHCI_ERSTBA_HI(0), (uint32_t)(addr >> 32));
440 /* set up interrupter registers */
441 temp = XREAD4(sc, runt, XHCI_IMAN(0));
442 temp |= XHCI_IMAN_INTR_ENA;
443 XWRITE4(sc, runt, XHCI_IMAN(0), temp);
445 /* set up command ring control base address */
446 addr = buf_res.physaddr;
447 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_commands[0];
449 DPRINTF("CRCR=0x%016llx\n", (unsigned long long)addr);
451 XWRITE4(sc, oper, XHCI_CRCR_LO, ((uint32_t)addr) | XHCI_CRCR_LO_RCS);
452 XWRITE4(sc, oper, XHCI_CRCR_HI, (uint32_t)(addr >> 32));
454 phwr->hwr_commands[XHCI_MAX_COMMANDS - 1].qwTrb0 = htole64(addr);
456 usb_bus_mem_flush_all(&sc->sc_bus, &xhci_iterate_hw_softc);
459 XWRITE4(sc, oper, XHCI_USBCMD, XHCI_CMD_RS |
460 XHCI_CMD_INTE | XHCI_CMD_HSEE);
462 for (i = 0; i != 100; i++) {
463 usb_pause_mtx(NULL, hz / 100);
464 temp = XREAD4(sc, oper, XHCI_USBSTS) & XHCI_STS_HCH;
469 XWRITE4(sc, oper, XHCI_USBCMD, 0);
470 device_printf(sc->sc_bus.parent, "Run timeout.\n");
471 return (USB_ERR_IOERROR);
474 /* catch any lost interrupts */
475 xhci_do_poll(&sc->sc_bus);
477 if (sc->sc_port_route != NULL) {
478 /* Route all ports to the XHCI by default */
479 sc->sc_port_route(sc->sc_bus.parent,
480 ~xhciroute, xhciroute);
486 xhci_halt_controller(struct xhci_softc *sc)
494 sc->sc_oper_off = XREAD1(sc, capa, XHCI_CAPLENGTH);
495 sc->sc_runt_off = XREAD4(sc, capa, XHCI_RTSOFF) & ~0xF;
496 sc->sc_door_off = XREAD4(sc, capa, XHCI_DBOFF) & ~0x3;
498 /* Halt controller */
499 XWRITE4(sc, oper, XHCI_USBCMD, 0);
501 for (i = 0; i != 100; i++) {
502 usb_pause_mtx(NULL, hz / 100);
503 temp = XREAD4(sc, oper, XHCI_USBSTS) & XHCI_STS_HCH;
509 device_printf(sc->sc_bus.parent, "Controller halt timeout.\n");
510 return (USB_ERR_IOERROR);
516 xhci_reset_controller(struct xhci_softc *sc)
523 /* Reset controller */
524 XWRITE4(sc, oper, XHCI_USBCMD, XHCI_CMD_HCRST);
526 for (i = 0; i != 100; i++) {
527 usb_pause_mtx(NULL, hz / 100);
528 temp = (XREAD4(sc, oper, XHCI_USBCMD) & XHCI_CMD_HCRST) |
529 (XREAD4(sc, oper, XHCI_USBSTS) & XHCI_STS_CNR);
535 device_printf(sc->sc_bus.parent, "Controller "
537 return (USB_ERR_IOERROR);
543 xhci_init(struct xhci_softc *sc, device_t self, uint8_t dma32)
549 /* initialize some bus fields */
550 sc->sc_bus.parent = self;
552 /* set the bus revision */
553 sc->sc_bus.usbrev = USB_REV_3_0;
555 /* set up the bus struct */
556 sc->sc_bus.methods = &xhci_bus_methods;
558 /* set up devices array */
559 sc->sc_bus.devices = sc->sc_devices;
560 sc->sc_bus.devices_max = XHCI_MAX_DEVICES;
562 /* set default cycle state in case of early interrupts */
563 sc->sc_event_ccs = 1;
564 sc->sc_command_ccs = 1;
566 /* set up bus space offsets */
568 sc->sc_oper_off = XREAD1(sc, capa, XHCI_CAPLENGTH);
569 sc->sc_runt_off = XREAD4(sc, capa, XHCI_RTSOFF) & ~0x1F;
570 sc->sc_door_off = XREAD4(sc, capa, XHCI_DBOFF) & ~0x3;
572 DPRINTF("CAPLENGTH=0x%x\n", sc->sc_oper_off);
573 DPRINTF("RUNTIMEOFFSET=0x%x\n", sc->sc_runt_off);
574 DPRINTF("DOOROFFSET=0x%x\n", sc->sc_door_off);
576 DPRINTF("xHCI version = 0x%04x\n", XREAD2(sc, capa, XHCI_HCIVERSION));
578 if (!(XREAD4(sc, oper, XHCI_PAGESIZE) & XHCI_PAGESIZE_4K)) {
579 device_printf(sc->sc_bus.parent, "Controller does "
580 "not support 4K page size.\n");
584 temp = XREAD4(sc, capa, XHCI_HCSPARAMS0);
586 DPRINTF("HCS0 = 0x%08x\n", temp);
588 /* set up context size */
589 if (XHCI_HCS0_CSZ(temp)) {
590 sc->sc_ctx_is_64_byte = 1;
592 sc->sc_ctx_is_64_byte = 0;
596 sc->sc_bus.dma_bits = (XHCI_HCS0_AC64(temp) &&
597 xhcidma32 == 0 && dma32 == 0) ? 64 : 32;
599 device_printf(self, "%d bytes context size, %d-bit DMA\n",
600 sc->sc_ctx_is_64_byte ? 64 : 32, (int)sc->sc_bus.dma_bits);
602 temp = XREAD4(sc, capa, XHCI_HCSPARAMS1);
604 /* get number of device slots */
605 sc->sc_noport = XHCI_HCS1_N_PORTS(temp);
607 if (sc->sc_noport == 0) {
608 device_printf(sc->sc_bus.parent, "Invalid number "
609 "of ports: %u\n", sc->sc_noport);
613 sc->sc_noport = sc->sc_noport;
614 sc->sc_noslot = XHCI_HCS1_DEVSLOT_MAX(temp);
616 DPRINTF("Max slots: %u\n", sc->sc_noslot);
618 if (sc->sc_noslot > XHCI_MAX_DEVICES)
619 sc->sc_noslot = XHCI_MAX_DEVICES;
621 temp = XREAD4(sc, capa, XHCI_HCSPARAMS2);
623 DPRINTF("HCS2=0x%08x\n", temp);
625 /* get number of scratchpads */
626 sc->sc_noscratch = XHCI_HCS2_SPB_MAX(temp);
628 if (sc->sc_noscratch > XHCI_MAX_SCRATCHPADS) {
629 device_printf(sc->sc_bus.parent, "XHCI request "
630 "too many scratchpads\n");
634 DPRINTF("Max scratch: %u\n", sc->sc_noscratch);
636 /* get event table size */
637 sc->sc_erst_max = 1U << XHCI_HCS2_ERST_MAX(temp);
638 if (sc->sc_erst_max > XHCI_MAX_RSEG)
639 sc->sc_erst_max = XHCI_MAX_RSEG;
641 temp = XREAD4(sc, capa, XHCI_HCSPARAMS3);
643 /* get maximum exit latency */
644 sc->sc_exit_lat_max = XHCI_HCS3_U1_DEL(temp) +
645 XHCI_HCS3_U2_DEL(temp) + 250 /* us */;
647 /* Check if we should use the default IMOD value. */
648 if (sc->sc_imod_default == 0)
649 sc->sc_imod_default = XHCI_IMOD_DEFAULT;
651 /* get all DMA memory */
652 if (usb_bus_mem_alloc_all(&sc->sc_bus,
653 USB_GET_DMA_TAG(self), &xhci_iterate_hw_softc)) {
657 /* set up command queue mutex and condition varible */
658 cv_init(&sc->sc_cmd_cv, "CMDQ");
659 sx_init(&sc->sc_cmd_sx, "CMDQ lock");
661 sc->sc_config_msg[0].hdr.pm_callback = &xhci_configure_msg;
662 sc->sc_config_msg[0].bus = &sc->sc_bus;
663 sc->sc_config_msg[1].hdr.pm_callback = &xhci_configure_msg;
664 sc->sc_config_msg[1].bus = &sc->sc_bus;
670 xhci_uninit(struct xhci_softc *sc)
673 * NOTE: At this point the control transfer process is gone
674 * and "xhci_configure_msg" is no longer called. Consequently
675 * waiting for the configuration messages to complete is not
678 usb_bus_mem_free_all(&sc->sc_bus, &xhci_iterate_hw_softc);
680 cv_destroy(&sc->sc_cmd_cv);
681 sx_destroy(&sc->sc_cmd_sx);
685 xhci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
687 struct xhci_softc *sc = XHCI_BUS2SC(bus);
690 case USB_HW_POWER_SUSPEND:
691 DPRINTF("Stopping the XHCI\n");
692 xhci_halt_controller(sc);
693 xhci_reset_controller(sc);
695 case USB_HW_POWER_SHUTDOWN:
696 DPRINTF("Stopping the XHCI\n");
697 xhci_halt_controller(sc);
698 xhci_reset_controller(sc);
700 case USB_HW_POWER_RESUME:
701 DPRINTF("Starting the XHCI\n");
702 xhci_start_controller(sc);
710 xhci_generic_done_sub(struct usb_xfer *xfer)
713 struct xhci_td *td_alt_next;
717 td = xfer->td_transfer_cache;
718 td_alt_next = td->alt_next;
720 if (xfer->aframes != xfer->nframes)
721 usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
725 usb_pc_cpu_invalidate(td->page_cache);
730 DPRINTFN(4, "xfer=%p[%u/%u] rem=%u/%u status=%u\n",
731 xfer, (unsigned int)xfer->aframes,
732 (unsigned int)xfer->nframes,
733 (unsigned int)len, (unsigned int)td->len,
734 (unsigned int)status);
737 * Verify the status length and
738 * add the length to "frlengths[]":
741 /* should not happen */
742 DPRINTF("Invalid status length, "
743 "0x%04x/0x%04x bytes\n", len, td->len);
744 status = XHCI_TRB_ERROR_LENGTH;
745 } else if (xfer->aframes != xfer->nframes) {
746 xfer->frlengths[xfer->aframes] += td->len - len;
748 /* Check for last transfer */
749 if (((void *)td) == xfer->td_transfer_last) {
753 /* Check for transfer error */
754 if (status != XHCI_TRB_ERROR_SHORT_PKT &&
755 status != XHCI_TRB_ERROR_SUCCESS) {
756 /* the transfer is finished */
760 /* Check for short transfer */
762 if (xfer->flags_int.short_frames_ok ||
763 xfer->flags_int.isochronous_xfr ||
764 xfer->flags_int.control_xfr) {
765 /* follow alt next */
768 /* the transfer is finished */
775 if (td->alt_next != td_alt_next) {
776 /* this USB frame is complete */
781 /* update transfer cache */
783 xfer->td_transfer_cache = td;
785 return ((status == XHCI_TRB_ERROR_STALL) ? USB_ERR_STALLED :
786 (status != XHCI_TRB_ERROR_SHORT_PKT &&
787 status != XHCI_TRB_ERROR_SUCCESS) ? USB_ERR_IOERROR :
788 USB_ERR_NORMAL_COMPLETION);
792 xhci_generic_done(struct usb_xfer *xfer)
796 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
797 xfer, xfer->endpoint);
801 xfer->td_transfer_cache = xfer->td_transfer_first;
803 if (xfer->flags_int.control_xfr) {
805 if (xfer->flags_int.control_hdr)
806 err = xhci_generic_done_sub(xfer);
810 if (xfer->td_transfer_cache == NULL)
814 while (xfer->aframes != xfer->nframes) {
816 err = xhci_generic_done_sub(xfer);
819 if (xfer->td_transfer_cache == NULL)
823 if (xfer->flags_int.control_xfr &&
824 !xfer->flags_int.control_act)
825 err = xhci_generic_done_sub(xfer);
827 /* transfer is complete */
828 xhci_device_done(xfer, err);
832 xhci_activate_transfer(struct usb_xfer *xfer)
836 td = xfer->td_transfer_cache;
838 usb_pc_cpu_invalidate(td->page_cache);
840 if (!(td->td_trb[0].dwTrb3 & htole32(XHCI_TRB_3_CYCLE_BIT))) {
842 /* activate the transfer */
844 td->td_trb[0].dwTrb3 |= htole32(XHCI_TRB_3_CYCLE_BIT);
845 usb_pc_cpu_flush(td->page_cache);
847 xhci_endpoint_doorbell(xfer);
852 xhci_skip_transfer(struct usb_xfer *xfer)
855 struct xhci_td *td_last;
857 td = xfer->td_transfer_cache;
858 td_last = xfer->td_transfer_last;
862 usb_pc_cpu_invalidate(td->page_cache);
864 if (!(td->td_trb[0].dwTrb3 & htole32(XHCI_TRB_3_CYCLE_BIT))) {
866 usb_pc_cpu_invalidate(td_last->page_cache);
868 /* copy LINK TRB to current waiting location */
870 td->td_trb[0].qwTrb0 = td_last->td_trb[td_last->ntrb].qwTrb0;
871 td->td_trb[0].dwTrb2 = td_last->td_trb[td_last->ntrb].dwTrb2;
872 usb_pc_cpu_flush(td->page_cache);
874 td->td_trb[0].dwTrb3 = td_last->td_trb[td_last->ntrb].dwTrb3;
875 usb_pc_cpu_flush(td->page_cache);
877 xhci_endpoint_doorbell(xfer);
881 /*------------------------------------------------------------------------*
882 * xhci_check_transfer
883 *------------------------------------------------------------------------*/
885 xhci_check_transfer(struct xhci_softc *sc, struct xhci_trb *trb)
887 struct xhci_endpoint_ext *pepext;
892 uint16_t stream_id = 0;
900 td_event = le64toh(trb->qwTrb0);
901 temp = le32toh(trb->dwTrb2);
903 remainder = XHCI_TRB_2_REM_GET(temp);
904 status = XHCI_TRB_2_ERROR_GET(temp);
906 temp = le32toh(trb->dwTrb3);
907 epno = XHCI_TRB_3_EP_GET(temp);
908 index = XHCI_TRB_3_SLOT_GET(temp);
910 /* check if error means halted */
911 halted = (status != XHCI_TRB_ERROR_SHORT_PKT &&
912 status != XHCI_TRB_ERROR_SUCCESS);
914 DPRINTF("slot=%u epno=%u remainder=%u status=%u\n",
915 index, epno, remainder, status);
917 if (index > sc->sc_noslot) {
918 DPRINTF("Invalid slot.\n");
922 if ((epno == 0) || (epno >= XHCI_MAX_ENDPOINTS)) {
923 DPRINTF("Invalid endpoint.\n");
927 pepext = &sc->sc_hw.devs[index].endp[epno];
929 /* try to find the USB transfer that generated the event */
931 struct usb_xfer *xfer;
934 if (i == (XHCI_MAX_TRANSFERS - 1)) {
935 if (pepext->trb_ep_mode != USB_EP_MODE_STREAMS ||
936 stream_id == (XHCI_MAX_STREAMS - 1))
940 DPRINTFN(5, "stream_id=%u\n", stream_id);
943 xfer = pepext->xfer[i + (XHCI_MAX_TRANSFERS * stream_id)];
947 td = xfer->td_transfer_cache;
949 DPRINTFN(5, "Checking if 0x%016llx == (0x%016llx .. 0x%016llx)\n",
951 (long long)td->td_self,
952 (long long)td->td_self + sizeof(td->td_trb));
955 * NOTE: Some XHCI implementations might not trigger
956 * an event on the last LINK TRB so we need to
957 * consider both the last and second last event
958 * address as conditions for a successful transfer.
960 * NOTE: We assume that the XHCI will only trigger one
961 * event per chain of TRBs.
964 offset = td_event - td->td_self;
967 offset < (int64_t)sizeof(td->td_trb)) {
969 usb_pc_cpu_invalidate(td->page_cache);
971 /* compute rest of remainder, if any */
972 for (i = (offset / 16) + 1; i < td->ntrb; i++) {
973 temp = le32toh(td->td_trb[i].dwTrb2);
974 remainder += XHCI_TRB_2_BYTES_GET(temp);
977 DPRINTFN(5, "New remainder: %u\n", remainder);
979 /* clear isochronous transfer errors */
980 if (xfer->flags_int.isochronous_xfr) {
983 status = XHCI_TRB_ERROR_SUCCESS;
988 /* "td->remainder" is verified later */
989 td->remainder = remainder;
992 usb_pc_cpu_flush(td->page_cache);
995 * 1) Last transfer descriptor makes the
998 if (((void *)td) == xfer->td_transfer_last) {
999 DPRINTF("TD is last\n");
1000 xhci_generic_done(xfer);
1005 * 2) Any kind of error makes the transfer
1009 DPRINTF("TD has I/O error\n");
1010 xhci_generic_done(xfer);
1015 * 3) If there is no alternate next transfer,
1016 * a short packet also makes the transfer done
1018 if (td->remainder > 0) {
1019 if (td->alt_next == NULL) {
1021 "short TD has no alternate next\n");
1022 xhci_generic_done(xfer);
1025 DPRINTF("TD has short pkt\n");
1026 if (xfer->flags_int.short_frames_ok ||
1027 xfer->flags_int.isochronous_xfr ||
1028 xfer->flags_int.control_xfr) {
1029 /* follow the alt next */
1030 xfer->td_transfer_cache = td->alt_next;
1031 xhci_activate_transfer(xfer);
1034 xhci_skip_transfer(xfer);
1035 xhci_generic_done(xfer);
1040 * 4) Transfer complete - go to next TD
1042 DPRINTF("Following next TD\n");
1043 xfer->td_transfer_cache = td->obj_next;
1044 xhci_activate_transfer(xfer);
1045 break; /* there should only be one match */
1051 xhci_check_command(struct xhci_softc *sc, struct xhci_trb *trb)
1053 if (sc->sc_cmd_addr == trb->qwTrb0) {
1054 DPRINTF("Received command event\n");
1055 sc->sc_cmd_result[0] = trb->dwTrb2;
1056 sc->sc_cmd_result[1] = trb->dwTrb3;
1057 cv_signal(&sc->sc_cmd_cv);
1058 return (1); /* command match */
1064 xhci_interrupt_poll(struct xhci_softc *sc)
1066 struct usb_page_search buf_res;
1067 struct xhci_hw_root *phwr;
1077 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
1079 phwr = buf_res.buffer;
1081 /* Receive any events */
1083 usb_pc_cpu_invalidate(&sc->sc_hw.root_pc);
1085 i = sc->sc_event_idx;
1086 j = sc->sc_event_ccs;
1091 temp = le32toh(phwr->hwr_events[i].dwTrb3);
1093 k = (temp & XHCI_TRB_3_CYCLE_BIT) ? 1 : 0;
1098 event = XHCI_TRB_3_TYPE_GET(temp);
1100 DPRINTFN(10, "event[%u] = %u (0x%016llx 0x%08lx 0x%08lx)\n",
1101 i, event, (long long)le64toh(phwr->hwr_events[i].qwTrb0),
1102 (long)le32toh(phwr->hwr_events[i].dwTrb2),
1103 (long)le32toh(phwr->hwr_events[i].dwTrb3));
1106 case XHCI_TRB_EVENT_TRANSFER:
1107 xhci_check_transfer(sc, &phwr->hwr_events[i]);
1109 case XHCI_TRB_EVENT_CMD_COMPLETE:
1110 retval |= xhci_check_command(sc, &phwr->hwr_events[i]);
1113 DPRINTF("Unhandled event = %u\n", event);
1119 if (i == XHCI_MAX_EVENTS) {
1123 /* check for timeout */
1129 sc->sc_event_idx = i;
1130 sc->sc_event_ccs = j;
1133 * NOTE: The Event Ring Dequeue Pointer Register is 64-bit
1134 * latched. That means to activate the register we need to
1135 * write both the low and high double word of the 64-bit
1139 addr = buf_res.physaddr;
1140 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_events[i];
1142 /* try to clear busy bit */
1143 addr |= XHCI_ERDP_LO_BUSY;
1145 XWRITE4(sc, runt, XHCI_ERDP_LO(0), (uint32_t)addr);
1146 XWRITE4(sc, runt, XHCI_ERDP_HI(0), (uint32_t)(addr >> 32));
1152 xhci_do_command(struct xhci_softc *sc, struct xhci_trb *trb,
1153 uint16_t timeout_ms)
1155 struct usb_page_search buf_res;
1156 struct xhci_hw_root *phwr;
1161 uint8_t timeout = 0;
1164 XHCI_CMD_ASSERT_LOCKED(sc);
1166 /* get hardware root structure */
1168 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
1170 phwr = buf_res.buffer;
1174 USB_BUS_LOCK(&sc->sc_bus);
1176 i = sc->sc_command_idx;
1177 j = sc->sc_command_ccs;
1179 DPRINTFN(10, "command[%u] = %u (0x%016llx, 0x%08lx, 0x%08lx)\n",
1180 i, XHCI_TRB_3_TYPE_GET(le32toh(trb->dwTrb3)),
1181 (long long)le64toh(trb->qwTrb0),
1182 (long)le32toh(trb->dwTrb2),
1183 (long)le32toh(trb->dwTrb3));
1185 phwr->hwr_commands[i].qwTrb0 = trb->qwTrb0;
1186 phwr->hwr_commands[i].dwTrb2 = trb->dwTrb2;
1188 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1193 temp |= htole32(XHCI_TRB_3_CYCLE_BIT);
1195 temp &= ~htole32(XHCI_TRB_3_CYCLE_BIT);
1197 temp &= ~htole32(XHCI_TRB_3_TC_BIT);
1199 phwr->hwr_commands[i].dwTrb3 = temp;
1201 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1203 addr = buf_res.physaddr;
1204 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_commands[i];
1206 sc->sc_cmd_addr = htole64(addr);
1210 if (i == (XHCI_MAX_COMMANDS - 1)) {
1213 temp = htole32(XHCI_TRB_3_TC_BIT |
1214 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) |
1215 XHCI_TRB_3_CYCLE_BIT);
1217 temp = htole32(XHCI_TRB_3_TC_BIT |
1218 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
1221 phwr->hwr_commands[i].dwTrb3 = temp;
1223 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1229 sc->sc_command_idx = i;
1230 sc->sc_command_ccs = j;
1232 XWRITE4(sc, door, XHCI_DOORBELL(0), 0);
1234 err = cv_timedwait(&sc->sc_cmd_cv, &sc->sc_bus.bus_mtx,
1235 USB_MS_TO_TICKS(timeout_ms));
1238 * In some error cases event interrupts are not generated.
1239 * Poll one time to see if the command has completed.
1241 if (err != 0 && xhci_interrupt_poll(sc) != 0) {
1242 DPRINTF("Command was completed when polling\n");
1246 DPRINTF("Command timeout!\n");
1248 * After some weeks of continuous operation, it has
1249 * been observed that the ASMedia Technology, ASM1042
1250 * SuperSpeed USB Host Controller can suddenly stop
1251 * accepting commands via the command queue. Try to
1252 * first reset the command queue. If that fails do a
1253 * host controller reset.
1256 xhci_reset_command_queue_locked(sc) == 0) {
1257 temp = le32toh(trb->dwTrb3);
1260 * Avoid infinite XHCI reset loops if the set
1261 * address command fails to respond due to a
1262 * non-enumerating device:
1264 if (XHCI_TRB_3_TYPE_GET(temp) == XHCI_TRB_TYPE_ADDRESS_DEVICE &&
1265 (temp & XHCI_TRB_3_BSR_BIT) == 0) {
1266 DPRINTF("Set address timeout\n");
1272 DPRINTF("Controller reset!\n");
1273 usb_bus_reset_async_locked(&sc->sc_bus);
1275 err = USB_ERR_TIMEOUT;
1279 temp = le32toh(sc->sc_cmd_result[0]);
1280 if (XHCI_TRB_2_ERROR_GET(temp) != XHCI_TRB_ERROR_SUCCESS)
1281 err = USB_ERR_IOERROR;
1283 trb->dwTrb2 = sc->sc_cmd_result[0];
1284 trb->dwTrb3 = sc->sc_cmd_result[1];
1287 USB_BUS_UNLOCK(&sc->sc_bus);
1294 xhci_cmd_nop(struct xhci_softc *sc)
1296 struct xhci_trb trb;
1303 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NOOP);
1305 trb.dwTrb3 = htole32(temp);
1307 return (xhci_do_command(sc, &trb, 100 /* ms */));
1312 xhci_cmd_enable_slot(struct xhci_softc *sc, uint8_t *pslot)
1314 struct xhci_trb trb;
1322 trb.dwTrb3 = htole32(XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ENABLE_SLOT));
1324 err = xhci_do_command(sc, &trb, 100 /* ms */);
1328 temp = le32toh(trb.dwTrb3);
1330 *pslot = XHCI_TRB_3_SLOT_GET(temp);
1337 xhci_cmd_disable_slot(struct xhci_softc *sc, uint8_t slot_id)
1339 struct xhci_trb trb;
1346 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DISABLE_SLOT) |
1347 XHCI_TRB_3_SLOT_SET(slot_id);
1349 trb.dwTrb3 = htole32(temp);
1351 return (xhci_do_command(sc, &trb, 100 /* ms */));
1355 xhci_cmd_set_address(struct xhci_softc *sc, uint64_t input_ctx,
1356 uint8_t bsr, uint8_t slot_id)
1358 struct xhci_trb trb;
1363 trb.qwTrb0 = htole64(input_ctx);
1365 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ADDRESS_DEVICE) |
1366 XHCI_TRB_3_SLOT_SET(slot_id);
1369 temp |= XHCI_TRB_3_BSR_BIT;
1371 trb.dwTrb3 = htole32(temp);
1373 return (xhci_do_command(sc, &trb, 500 /* ms */));
1377 xhci_set_address(struct usb_device *udev, struct mtx *mtx, uint16_t address)
1379 struct usb_page_search buf_inp;
1380 struct usb_page_search buf_dev;
1381 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
1382 struct xhci_hw_dev *hdev;
1383 struct xhci_dev_ctx *pdev;
1384 struct xhci_endpoint_ext *pepext;
1390 /* the root HUB case is not handled here */
1391 if (udev->parent_hub == NULL)
1392 return (USB_ERR_INVAL);
1394 index = udev->controller_slot_id;
1396 hdev = &sc->sc_hw.devs[index];
1403 switch (hdev->state) {
1404 case XHCI_ST_DEFAULT:
1405 case XHCI_ST_ENABLED:
1407 hdev->state = XHCI_ST_ENABLED;
1409 /* set configure mask to slot and EP0 */
1410 xhci_configure_mask(udev, 3, 0);
1412 /* configure input slot context structure */
1413 err = xhci_configure_device(udev);
1416 DPRINTF("Could not configure device\n");
1420 /* configure input endpoint context structure */
1421 switch (udev->speed) {
1423 case USB_SPEED_FULL:
1426 case USB_SPEED_HIGH:
1434 pepext = xhci_get_endpoint_ext(udev,
1435 &udev->ctrl_ep_desc);
1437 /* ensure the control endpoint is setup again */
1438 USB_BUS_LOCK(udev->bus);
1439 pepext->trb_halted = 1;
1440 pepext->trb_running = 0;
1441 USB_BUS_UNLOCK(udev->bus);
1443 err = xhci_configure_endpoint(udev,
1444 &udev->ctrl_ep_desc, pepext,
1445 0, 1, 1, 0, mps, mps, USB_EP_MODE_DEFAULT);
1448 DPRINTF("Could not configure default endpoint\n");
1452 /* execute set address command */
1453 usbd_get_page(&hdev->input_pc, 0, &buf_inp);
1455 err = xhci_cmd_set_address(sc, buf_inp.physaddr,
1456 (address == 0), index);
1459 temp = le32toh(sc->sc_cmd_result[0]);
1460 if (address == 0 && sc->sc_port_route != NULL &&
1461 XHCI_TRB_2_ERROR_GET(temp) ==
1462 XHCI_TRB_ERROR_PARAMETER) {
1463 /* LynxPoint XHCI - ports are not switchable */
1464 /* Un-route all ports from the XHCI */
1465 sc->sc_port_route(sc->sc_bus.parent, 0, ~0);
1467 DPRINTF("Could not set address "
1468 "for slot %u.\n", index);
1473 /* update device address to new value */
1475 usbd_get_page(&hdev->device_pc, 0, &buf_dev);
1476 pdev = buf_dev.buffer;
1477 usb_pc_cpu_invalidate(&hdev->device_pc);
1479 temp = xhci_ctx_get_le32(sc, &pdev->ctx_slot.dwSctx3);
1480 udev->address = XHCI_SCTX_3_DEV_ADDR_GET(temp);
1482 /* update device state to new value */
1485 hdev->state = XHCI_ST_ADDRESSED;
1487 hdev->state = XHCI_ST_DEFAULT;
1491 DPRINTF("Wrong state for set address.\n");
1492 err = USB_ERR_IOERROR;
1495 XHCI_CMD_UNLOCK(sc);
1504 xhci_cmd_configure_ep(struct xhci_softc *sc, uint64_t input_ctx,
1505 uint8_t deconfigure, uint8_t slot_id)
1507 struct xhci_trb trb;
1512 trb.qwTrb0 = htole64(input_ctx);
1514 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_CONFIGURE_EP) |
1515 XHCI_TRB_3_SLOT_SET(slot_id);
1518 temp |= XHCI_TRB_3_DCEP_BIT;
1520 trb.dwTrb3 = htole32(temp);
1522 return (xhci_do_command(sc, &trb, 100 /* ms */));
1526 xhci_cmd_evaluate_ctx(struct xhci_softc *sc, uint64_t input_ctx,
1529 struct xhci_trb trb;
1534 trb.qwTrb0 = htole64(input_ctx);
1536 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVALUATE_CTX) |
1537 XHCI_TRB_3_SLOT_SET(slot_id);
1538 trb.dwTrb3 = htole32(temp);
1540 return (xhci_do_command(sc, &trb, 100 /* ms */));
1544 xhci_cmd_reset_ep(struct xhci_softc *sc, uint8_t preserve,
1545 uint8_t ep_id, uint8_t slot_id)
1547 struct xhci_trb trb;
1554 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_EP) |
1555 XHCI_TRB_3_SLOT_SET(slot_id) |
1556 XHCI_TRB_3_EP_SET(ep_id);
1559 temp |= XHCI_TRB_3_PRSV_BIT;
1561 trb.dwTrb3 = htole32(temp);
1563 return (xhci_do_command(sc, &trb, 100 /* ms */));
1567 xhci_cmd_set_tr_dequeue_ptr(struct xhci_softc *sc, uint64_t dequeue_ptr,
1568 uint16_t stream_id, uint8_t ep_id, uint8_t slot_id)
1570 struct xhci_trb trb;
1575 trb.qwTrb0 = htole64(dequeue_ptr);
1577 temp = XHCI_TRB_2_STREAM_SET(stream_id);
1578 trb.dwTrb2 = htole32(temp);
1580 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SET_TR_DEQUEUE) |
1581 XHCI_TRB_3_SLOT_SET(slot_id) |
1582 XHCI_TRB_3_EP_SET(ep_id);
1583 trb.dwTrb3 = htole32(temp);
1585 return (xhci_do_command(sc, &trb, 100 /* ms */));
1589 xhci_cmd_stop_ep(struct xhci_softc *sc, uint8_t suspend,
1590 uint8_t ep_id, uint8_t slot_id)
1592 struct xhci_trb trb;
1599 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STOP_EP) |
1600 XHCI_TRB_3_SLOT_SET(slot_id) |
1601 XHCI_TRB_3_EP_SET(ep_id);
1604 temp |= XHCI_TRB_3_SUSP_EP_BIT;
1606 trb.dwTrb3 = htole32(temp);
1608 return (xhci_do_command(sc, &trb, 100 /* ms */));
1612 xhci_cmd_reset_dev(struct xhci_softc *sc, uint8_t slot_id)
1614 struct xhci_trb trb;
1621 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_DEVICE) |
1622 XHCI_TRB_3_SLOT_SET(slot_id);
1624 trb.dwTrb3 = htole32(temp);
1626 return (xhci_do_command(sc, &trb, 100 /* ms */));
1629 /*------------------------------------------------------------------------*
1630 * xhci_interrupt - XHCI interrupt handler
1631 *------------------------------------------------------------------------*/
1633 xhci_interrupt(struct xhci_softc *sc)
1638 USB_BUS_LOCK(&sc->sc_bus);
1640 status = XREAD4(sc, oper, XHCI_USBSTS);
1642 /* acknowledge interrupts, if any */
1644 XWRITE4(sc, oper, XHCI_USBSTS, status);
1645 DPRINTFN(16, "real interrupt (status=0x%08x)\n", status);
1648 temp = XREAD4(sc, runt, XHCI_IMAN(0));
1650 /* force clearing of pending interrupts */
1651 if (temp & XHCI_IMAN_INTR_PEND)
1652 XWRITE4(sc, runt, XHCI_IMAN(0), temp);
1654 /* check for event(s) */
1655 xhci_interrupt_poll(sc);
1657 if (status & (XHCI_STS_PCD | XHCI_STS_HCH |
1658 XHCI_STS_HSE | XHCI_STS_HCE)) {
1660 if (status & XHCI_STS_PCD) {
1664 if (status & XHCI_STS_HCH) {
1665 printf("%s: host controller halted\n",
1669 if (status & XHCI_STS_HSE) {
1670 printf("%s: host system error\n",
1674 if (status & XHCI_STS_HCE) {
1675 printf("%s: host controller error\n",
1679 USB_BUS_UNLOCK(&sc->sc_bus);
1682 /*------------------------------------------------------------------------*
1683 * xhci_timeout - XHCI timeout handler
1684 *------------------------------------------------------------------------*/
1686 xhci_timeout(void *arg)
1688 struct usb_xfer *xfer = arg;
1690 DPRINTF("xfer=%p\n", xfer);
1692 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1694 /* transfer is transferred */
1695 xhci_device_done(xfer, USB_ERR_TIMEOUT);
1699 xhci_do_poll(struct usb_bus *bus)
1701 struct xhci_softc *sc = XHCI_BUS2SC(bus);
1703 USB_BUS_LOCK(&sc->sc_bus);
1704 xhci_interrupt_poll(sc);
1705 USB_BUS_UNLOCK(&sc->sc_bus);
1709 xhci_setup_generic_chain_sub(struct xhci_std_temp *temp)
1711 struct usb_page_search buf_res;
1713 struct xhci_td *td_next;
1714 struct xhci_td *td_alt_next;
1715 struct xhci_td *td_first;
1716 uint32_t buf_offset;
1721 uint8_t shortpkt_old;
1727 shortpkt_old = temp->shortpkt;
1728 len_old = temp->len;
1735 td_next = td_first = temp->td_next;
1739 if (temp->len == 0) {
1744 /* send a Zero Length Packet, ZLP, last */
1751 average = temp->average;
1753 if (temp->len < average) {
1754 if (temp->len % temp->max_packet_size) {
1757 average = temp->len;
1761 if (td_next == NULL)
1762 panic("%s: out of XHCI transfer descriptors!", __FUNCTION__);
1767 td_next = td->obj_next;
1769 /* check if we are pre-computing */
1773 /* update remaining length */
1775 temp->len -= average;
1779 /* fill out current TD */
1785 /* update remaining length */
1787 temp->len -= average;
1789 /* reset TRB index */
1793 if (temp->trb_type == XHCI_TRB_TYPE_SETUP_STAGE) {
1794 /* immediate data */
1799 td->td_trb[0].qwTrb0 = 0;
1801 usbd_copy_out(temp->pc, temp->offset + buf_offset,
1802 (uint8_t *)(uintptr_t)&td->td_trb[0].qwTrb0,
1805 dword = XHCI_TRB_2_BYTES_SET(8) |
1806 XHCI_TRB_2_TDSZ_SET(0) |
1807 XHCI_TRB_2_IRQ_SET(0);
1809 td->td_trb[0].dwTrb2 = htole32(dword);
1811 dword = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SETUP_STAGE) |
1812 XHCI_TRB_3_IDT_BIT | XHCI_TRB_3_CYCLE_BIT;
1815 if (td->td_trb[0].qwTrb0 &
1816 htole64(XHCI_TRB_0_WLENGTH_MASK)) {
1817 if (td->td_trb[0].qwTrb0 &
1818 htole64(XHCI_TRB_0_DIR_IN_MASK))
1819 dword |= XHCI_TRB_3_TRT_IN;
1821 dword |= XHCI_TRB_3_TRT_OUT;
1824 td->td_trb[0].dwTrb3 = htole32(dword);
1826 xhci_dump_trb(&td->td_trb[x]);
1834 /* fill out buffer pointers */
1837 memset(&buf_res, 0, sizeof(buf_res));
1839 usbd_get_page(temp->pc, temp->offset +
1840 buf_offset, &buf_res);
1842 /* get length to end of page */
1843 if (buf_res.length > average)
1844 buf_res.length = average;
1846 /* check for maximum length */
1847 if (buf_res.length > XHCI_TD_PAGE_SIZE)
1848 buf_res.length = XHCI_TD_PAGE_SIZE;
1850 npkt_off += buf_res.length;
1854 npkt = howmany(len_old - npkt_off,
1855 temp->max_packet_size);
1862 /* fill out TRB's */
1863 td->td_trb[x].qwTrb0 =
1864 htole64((uint64_t)buf_res.physaddr);
1867 XHCI_TRB_2_BYTES_SET(buf_res.length) |
1868 XHCI_TRB_2_TDSZ_SET(npkt) |
1869 XHCI_TRB_2_IRQ_SET(0);
1871 td->td_trb[x].dwTrb2 = htole32(dword);
1873 switch (temp->trb_type) {
1874 case XHCI_TRB_TYPE_ISOCH:
1875 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1876 XHCI_TRB_3_TBC_SET(temp->tbc) |
1877 XHCI_TRB_3_TLBPC_SET(temp->tlbpc);
1878 if (td != td_first) {
1879 dword |= XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL);
1880 } else if (temp->do_isoc_sync != 0) {
1881 temp->do_isoc_sync = 0;
1882 /* wait until "isoc_frame" */
1883 dword |= XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ISOCH) |
1884 XHCI_TRB_3_FRID_SET(temp->isoc_frame / 8);
1886 /* start data transfer at next interval */
1887 dword |= XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ISOCH) |
1888 XHCI_TRB_3_ISO_SIA_BIT;
1890 if (temp->direction == UE_DIR_IN)
1891 dword |= XHCI_TRB_3_ISP_BIT;
1893 case XHCI_TRB_TYPE_DATA_STAGE:
1894 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1895 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DATA_STAGE);
1896 if (temp->direction == UE_DIR_IN)
1897 dword |= XHCI_TRB_3_DIR_IN | XHCI_TRB_3_ISP_BIT;
1899 * Section 3.2.9 in the XHCI
1900 * specification about control
1901 * transfers says that we should use a
1902 * normal-TRB if there are more TRBs
1903 * extending the data-stage
1904 * TRB. Update the "trb_type".
1906 temp->trb_type = XHCI_TRB_TYPE_NORMAL;
1908 case XHCI_TRB_TYPE_STATUS_STAGE:
1909 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1910 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STATUS_STAGE);
1911 if (temp->direction == UE_DIR_IN)
1912 dword |= XHCI_TRB_3_DIR_IN;
1914 default: /* XHCI_TRB_TYPE_NORMAL */
1915 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1916 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL);
1917 if (temp->direction == UE_DIR_IN)
1918 dword |= XHCI_TRB_3_ISP_BIT;
1921 td->td_trb[x].dwTrb3 = htole32(dword);
1923 average -= buf_res.length;
1924 buf_offset += buf_res.length;
1926 xhci_dump_trb(&td->td_trb[x]);
1930 } while (average != 0);
1932 td->td_trb[x-1].dwTrb3 |= htole32(XHCI_TRB_3_IOC_BIT);
1934 /* store number of data TRB's */
1938 DPRINTF("NTRB=%u\n", x);
1940 /* fill out link TRB */
1942 if (td_next != NULL) {
1943 /* link the current TD with the next one */
1944 td->td_trb[x].qwTrb0 = htole64((uint64_t)td_next->td_self);
1945 DPRINTF("LINK=0x%08llx\n", (long long)td_next->td_self);
1947 /* this field will get updated later */
1948 DPRINTF("NOLINK\n");
1951 dword = XHCI_TRB_2_IRQ_SET(0);
1953 td->td_trb[x].dwTrb2 = htole32(dword);
1955 dword = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) |
1956 XHCI_TRB_3_CYCLE_BIT | XHCI_TRB_3_IOC_BIT |
1958 * CHAIN-BIT: Ensure that a multi-TRB IN-endpoint
1959 * frame only receives a single short packet event
1960 * by setting the CHAIN bit in the LINK field. In
1961 * addition some XHCI controllers have problems
1962 * sending a ZLP unless the CHAIN-BIT is set in
1965 XHCI_TRB_3_CHAIN_BIT;
1967 td->td_trb[x].dwTrb3 = htole32(dword);
1969 td->alt_next = td_alt_next;
1971 xhci_dump_trb(&td->td_trb[x]);
1973 usb_pc_cpu_flush(td->page_cache);
1979 /* set up alt next pointer, if any */
1980 if (temp->last_frame) {
1983 /* we use this field internally */
1984 td_alt_next = td_next;
1988 temp->shortpkt = shortpkt_old;
1989 temp->len = len_old;
1994 * Remove cycle bit from the first TRB if we are
1997 if (temp->step_td != 0) {
1998 td_first->td_trb[0].dwTrb3 &= ~htole32(XHCI_TRB_3_CYCLE_BIT);
1999 usb_pc_cpu_flush(td_first->page_cache);
2002 /* clear TD SIZE to zero, hence this is the last TRB */
2003 /* remove chain bit because this is the last data TRB in the chain */
2004 td->td_trb[td->ntrb - 1].dwTrb2 &= ~htole32(XHCI_TRB_2_TDSZ_SET(15));
2005 td->td_trb[td->ntrb - 1].dwTrb3 &= ~htole32(XHCI_TRB_3_CHAIN_BIT);
2006 /* remove CHAIN-BIT from last LINK TRB */
2007 td->td_trb[td->ntrb].dwTrb3 &= ~htole32(XHCI_TRB_3_CHAIN_BIT);
2009 usb_pc_cpu_flush(td->page_cache);
2012 temp->td_next = td_next;
2016 xhci_setup_generic_chain(struct usb_xfer *xfer)
2018 struct xhci_std_temp temp;
2024 temp.do_isoc_sync = 0;
2028 temp.average = xfer->max_hc_frame_size;
2029 temp.max_packet_size = xfer->max_packet_size;
2030 temp.sc = XHCI_BUS2SC(xfer->xroot->bus);
2032 temp.last_frame = 0;
2034 temp.multishort = xfer->flags_int.isochronous_xfr ||
2035 xfer->flags_int.control_xfr ||
2036 xfer->flags_int.short_frames_ok;
2038 /* toggle the DMA set we are using */
2039 xfer->flags_int.curr_dma_set ^= 1;
2041 /* get next DMA set */
2042 td = xfer->td_start[xfer->flags_int.curr_dma_set];
2047 xfer->td_transfer_first = td;
2048 xfer->td_transfer_cache = td;
2050 if (xfer->flags_int.isochronous_xfr) {
2053 /* compute multiplier for ISOCHRONOUS transfers */
2054 mult = xfer->endpoint->ecomp ?
2055 UE_GET_SS_ISO_MULT(xfer->endpoint->ecomp->bmAttributes)
2057 /* check for USB 2.0 multiplier */
2059 mult = (xfer->endpoint->edesc->
2060 wMaxPacketSize[1] >> 3) & 3;
2068 x = XREAD4(temp.sc, runt, XHCI_MFINDEX);
2070 DPRINTF("MFINDEX=0x%08x\n", x);
2072 switch (usbd_get_speed(xfer->xroot->udev)) {
2073 case USB_SPEED_FULL:
2075 temp.isoc_delta = 8; /* 1ms */
2076 x += temp.isoc_delta - 1;
2077 x &= ~(temp.isoc_delta - 1);
2080 shift = usbd_xfer_get_fps_shift(xfer);
2081 temp.isoc_delta = 1U << shift;
2082 x += temp.isoc_delta - 1;
2083 x &= ~(temp.isoc_delta - 1);
2084 /* simple frame load balancing */
2085 x += xfer->endpoint->usb_uframe;
2089 y = XHCI_MFINDEX_GET(x - xfer->endpoint->isoc_next);
2091 if ((xfer->endpoint->is_synced == 0) ||
2092 (y < (xfer->nframes << shift)) ||
2093 (XHCI_MFINDEX_GET(-y) >= (128 * 8))) {
2095 * If there is data underflow or the pipe
2096 * queue is empty we schedule the transfer a
2097 * few frames ahead of the current frame
2098 * position. Else two isochronous transfers
2101 xfer->endpoint->isoc_next = XHCI_MFINDEX_GET(x + (3 * 8));
2102 xfer->endpoint->is_synced = 1;
2103 temp.do_isoc_sync = 1;
2105 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
2108 /* compute isochronous completion time */
2110 y = XHCI_MFINDEX_GET(xfer->endpoint->isoc_next - (x & ~7));
2112 xfer->isoc_time_complete =
2113 usb_isoc_time_expand(&temp.sc->sc_bus, x / 8) +
2114 (y / 8) + (((xfer->nframes << shift) + 7) / 8);
2117 temp.isoc_frame = xfer->endpoint->isoc_next;
2118 temp.trb_type = XHCI_TRB_TYPE_ISOCH;
2120 xfer->endpoint->isoc_next += xfer->nframes << shift;
2122 } else if (xfer->flags_int.control_xfr) {
2124 /* check if we should prepend a setup message */
2126 if (xfer->flags_int.control_hdr) {
2128 temp.len = xfer->frlengths[0];
2129 temp.pc = xfer->frbuffers + 0;
2130 temp.shortpkt = temp.len ? 1 : 0;
2131 temp.trb_type = XHCI_TRB_TYPE_SETUP_STAGE;
2134 /* check for last frame */
2135 if (xfer->nframes == 1) {
2136 /* no STATUS stage yet, SETUP is last */
2137 if (xfer->flags_int.control_act)
2138 temp.last_frame = 1;
2141 xhci_setup_generic_chain_sub(&temp);
2145 temp.isoc_delta = 0;
2146 temp.isoc_frame = 0;
2147 temp.trb_type = xfer->flags_int.control_did_data ?
2148 XHCI_TRB_TYPE_NORMAL : XHCI_TRB_TYPE_DATA_STAGE;
2152 temp.isoc_delta = 0;
2153 temp.isoc_frame = 0;
2154 temp.trb_type = XHCI_TRB_TYPE_NORMAL;
2157 if (x != xfer->nframes) {
2158 /* set up page_cache pointer */
2159 temp.pc = xfer->frbuffers + x;
2160 /* set endpoint direction */
2161 temp.direction = UE_GET_DIR(xfer->endpointno);
2164 while (x != xfer->nframes) {
2166 /* DATA0 / DATA1 message */
2168 temp.len = xfer->frlengths[x];
2169 temp.step_td = ((xfer->endpointno & UE_DIR_IN) &&
2170 x != 0 && temp.multishort == 0);
2174 if (x == xfer->nframes) {
2175 if (xfer->flags_int.control_xfr) {
2176 /* no STATUS stage yet, DATA is last */
2177 if (xfer->flags_int.control_act)
2178 temp.last_frame = 1;
2180 temp.last_frame = 1;
2183 if (temp.len == 0) {
2185 /* make sure that we send an USB packet */
2190 temp.tlbpc = mult - 1;
2192 } else if (xfer->flags_int.isochronous_xfr) {
2197 * Isochronous transfers don't have short
2198 * packet termination:
2203 /* isochronous transfers have a transfer limit */
2205 if (temp.len > xfer->max_frame_size)
2206 temp.len = xfer->max_frame_size;
2208 /* compute TD packet count */
2209 tdpc = howmany(temp.len, xfer->max_packet_size);
2211 temp.tbc = howmany(tdpc, mult) - 1;
2212 temp.tlbpc = (tdpc % mult);
2214 if (temp.tlbpc == 0)
2215 temp.tlbpc = mult - 1;
2220 /* regular data transfer */
2222 temp.shortpkt = xfer->flags.force_short_xfer ? 0 : 1;
2225 xhci_setup_generic_chain_sub(&temp);
2227 if (xfer->flags_int.isochronous_xfr) {
2228 temp.offset += xfer->frlengths[x - 1];
2229 temp.isoc_frame += temp.isoc_delta;
2231 /* get next Page Cache pointer */
2232 temp.pc = xfer->frbuffers + x;
2236 /* check if we should append a status stage */
2238 if (xfer->flags_int.control_xfr &&
2239 !xfer->flags_int.control_act) {
2242 * Send a DATA1 message and invert the current
2243 * endpoint direction.
2245 if (xhcictlstep || temp.sc->sc_ctlstep) {
2247 * Some XHCI controllers will not delay the
2248 * status stage until the next SOF. Force this
2249 * behaviour to avoid failed control
2252 temp.step_td = (xfer->nframes != 0);
2256 temp.direction = UE_GET_DIR(xfer->endpointno) ^ UE_DIR_IN;
2260 temp.last_frame = 1;
2261 temp.trb_type = XHCI_TRB_TYPE_STATUS_STAGE;
2263 xhci_setup_generic_chain_sub(&temp);
2268 /* must have at least one frame! */
2270 xfer->td_transfer_last = td;
2272 DPRINTF("first=%p last=%p\n", xfer->td_transfer_first, td);
2276 xhci_set_slot_pointer(struct xhci_softc *sc, uint8_t index, uint64_t dev_addr)
2278 struct usb_page_search buf_res;
2279 struct xhci_dev_ctx_addr *pdctxa;
2281 usbd_get_page(&sc->sc_hw.ctx_pc, 0, &buf_res);
2283 pdctxa = buf_res.buffer;
2285 DPRINTF("addr[%u]=0x%016llx\n", index, (long long)dev_addr);
2287 pdctxa->qwBaaDevCtxAddr[index] = htole64(dev_addr);
2289 usb_pc_cpu_flush(&sc->sc_hw.ctx_pc);
2293 xhci_configure_mask(struct usb_device *udev, uint32_t mask, uint8_t drop)
2295 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2296 struct usb_page_search buf_inp;
2297 struct xhci_input_dev_ctx *pinp;
2302 index = udev->controller_slot_id;
2304 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
2306 pinp = buf_inp.buffer;
2309 mask &= XHCI_INCTX_NON_CTRL_MASK;
2310 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx0, mask);
2311 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx1, 0);
2314 * Some hardware requires that we drop the endpoint
2315 * context before adding it again:
2317 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx0,
2318 mask & XHCI_INCTX_NON_CTRL_MASK);
2320 /* Add new endpoint context */
2321 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx1, mask);
2323 /* find most significant set bit */
2324 for (x = 31; x != 1; x--) {
2325 if (mask & (1 << x))
2332 /* figure out the maximum number of contexts */
2333 if (x > sc->sc_hw.devs[index].context_num)
2334 sc->sc_hw.devs[index].context_num = x;
2336 x = sc->sc_hw.devs[index].context_num;
2338 /* update number of contexts */
2339 temp = xhci_ctx_get_le32(sc, &pinp->ctx_slot.dwSctx0);
2340 temp &= ~XHCI_SCTX_0_CTX_NUM_SET(31);
2341 temp |= XHCI_SCTX_0_CTX_NUM_SET(x + 1);
2342 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx0, temp);
2344 usb_pc_cpu_flush(&sc->sc_hw.devs[index].input_pc);
2349 xhci_configure_endpoint(struct usb_device *udev,
2350 struct usb_endpoint_descriptor *edesc, struct xhci_endpoint_ext *pepext,
2351 uint16_t interval, uint8_t max_packet_count,
2352 uint8_t mult, uint8_t fps_shift, uint16_t max_packet_size,
2353 uint16_t max_frame_size, uint8_t ep_mode)
2355 struct usb_page_search buf_inp;
2356 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2357 struct xhci_input_dev_ctx *pinp;
2358 uint64_t ring_addr = pepext->physaddr;
2364 index = udev->controller_slot_id;
2366 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
2368 pinp = buf_inp.buffer;
2370 epno = edesc->bEndpointAddress;
2371 type = edesc->bmAttributes & UE_XFERTYPE;
2373 if (type == UE_CONTROL)
2376 epno = XHCI_EPNO2EPID(epno);
2379 return (USB_ERR_NO_PIPE); /* invalid */
2381 if (max_packet_count == 0)
2382 return (USB_ERR_BAD_BUFSIZE);
2387 return (USB_ERR_BAD_BUFSIZE);
2389 /* store endpoint mode */
2390 pepext->trb_ep_mode = ep_mode;
2391 /* store bMaxPacketSize for control endpoints */
2392 pepext->trb_ep_maxp = edesc->wMaxPacketSize[0];
2393 usb_pc_cpu_flush(pepext->page_cache);
2395 if (ep_mode == USB_EP_MODE_STREAMS) {
2396 temp = XHCI_EPCTX_0_EPSTATE_SET(0) |
2397 XHCI_EPCTX_0_MAXP_STREAMS_SET(XHCI_MAX_STREAMS_LOG - 1) |
2398 XHCI_EPCTX_0_LSA_SET(1);
2400 ring_addr += sizeof(struct xhci_trb) *
2401 XHCI_MAX_TRANSFERS * XHCI_MAX_STREAMS;
2403 temp = XHCI_EPCTX_0_EPSTATE_SET(0) |
2404 XHCI_EPCTX_0_MAXP_STREAMS_SET(0) |
2405 XHCI_EPCTX_0_LSA_SET(0);
2407 ring_addr |= XHCI_EPCTX_2_DCS_SET(1);
2410 switch (udev->speed) {
2411 case USB_SPEED_FULL:
2424 temp |= XHCI_EPCTX_0_IVAL_SET(fps_shift);
2426 case UE_ISOCHRONOUS:
2427 temp |= XHCI_EPCTX_0_IVAL_SET(fps_shift);
2429 switch (udev->speed) {
2430 case USB_SPEED_SUPER:
2433 temp |= XHCI_EPCTX_0_MULT_SET(mult - 1);
2434 max_packet_count /= mult;
2444 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx0, temp);
2447 XHCI_EPCTX_1_HID_SET(0) |
2448 XHCI_EPCTX_1_MAXB_SET(max_packet_count) |
2449 XHCI_EPCTX_1_MAXP_SIZE_SET(max_packet_size);
2452 * Always enable the "three strikes and you are gone" feature
2453 * except for ISOCHRONOUS endpoints. This is suggested by
2454 * section 4.3.3 in the XHCI specification about device slot
2457 if (type != UE_ISOCHRONOUS)
2458 temp |= XHCI_EPCTX_1_CERR_SET(3);
2462 temp |= XHCI_EPCTX_1_EPTYPE_SET(4);
2464 case UE_ISOCHRONOUS:
2465 temp |= XHCI_EPCTX_1_EPTYPE_SET(1);
2468 temp |= XHCI_EPCTX_1_EPTYPE_SET(2);
2471 temp |= XHCI_EPCTX_1_EPTYPE_SET(3);
2475 /* check for IN direction */
2477 temp |= XHCI_EPCTX_1_EPTYPE_SET(4);
2479 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx1, temp);
2480 xhci_ctx_set_le64(sc, &pinp->ctx_ep[epno - 1].qwEpCtx2, ring_addr);
2482 switch (edesc->bmAttributes & UE_XFERTYPE) {
2484 case UE_ISOCHRONOUS:
2485 temp = XHCI_EPCTX_4_MAX_ESIT_PAYLOAD_SET(max_frame_size) |
2486 XHCI_EPCTX_4_AVG_TRB_LEN_SET(MIN(XHCI_PAGE_SIZE,
2490 temp = XHCI_EPCTX_4_AVG_TRB_LEN_SET(8);
2493 temp = XHCI_EPCTX_4_AVG_TRB_LEN_SET(XHCI_PAGE_SIZE);
2497 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx4, temp);
2500 xhci_dump_endpoint(sc, &pinp->ctx_ep[epno - 1]);
2502 usb_pc_cpu_flush(&sc->sc_hw.devs[index].input_pc);
2504 return (0); /* success */
2508 xhci_configure_endpoint_by_xfer(struct usb_xfer *xfer)
2510 struct xhci_endpoint_ext *pepext;
2511 struct usb_endpoint_ss_comp_descriptor *ecomp;
2514 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2515 xfer->endpoint->edesc);
2517 ecomp = xfer->endpoint->ecomp;
2519 for (x = 0; x != XHCI_MAX_STREAMS; x++) {
2522 /* halt any transfers */
2523 pepext->trb[x * XHCI_MAX_TRANSFERS].dwTrb3 = 0;
2525 /* compute start of TRB ring for stream "x" */
2526 temp = pepext->physaddr +
2527 (x * XHCI_MAX_TRANSFERS * sizeof(struct xhci_trb)) +
2528 XHCI_SCTX_0_SCT_SEC_TR_RING;
2530 /* make tree structure */
2531 pepext->trb[(XHCI_MAX_TRANSFERS *
2532 XHCI_MAX_STREAMS) + x].qwTrb0 = htole64(temp);
2534 /* reserved fields */
2535 pepext->trb[(XHCI_MAX_TRANSFERS *
2536 XHCI_MAX_STREAMS) + x].dwTrb2 = 0;
2537 pepext->trb[(XHCI_MAX_TRANSFERS *
2538 XHCI_MAX_STREAMS) + x].dwTrb3 = 0;
2540 usb_pc_cpu_flush(pepext->page_cache);
2542 return (xhci_configure_endpoint(xfer->xroot->udev,
2543 xfer->endpoint->edesc, pepext,
2544 xfer->interval, xfer->max_packet_count,
2545 (ecomp != NULL) ? UE_GET_SS_ISO_MULT(ecomp->bmAttributes) + 1 : 1,
2546 usbd_xfer_get_fps_shift(xfer), xfer->max_packet_size,
2547 xfer->max_frame_size, xfer->endpoint->ep_mode));
2551 xhci_configure_device(struct usb_device *udev)
2553 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2554 struct usb_page_search buf_inp;
2555 struct usb_page_cache *pcinp;
2556 struct xhci_input_dev_ctx *pinp;
2557 struct usb_device *hubdev;
2565 index = udev->controller_slot_id;
2567 DPRINTF("index=%u\n", index);
2569 pcinp = &sc->sc_hw.devs[index].input_pc;
2571 usbd_get_page(pcinp, 0, &buf_inp);
2573 pinp = buf_inp.buffer;
2578 /* figure out route string and root HUB port number */
2580 for (hubdev = udev; hubdev != NULL; hubdev = hubdev->parent_hub) {
2582 if (hubdev->parent_hub == NULL)
2585 depth = hubdev->parent_hub->depth;
2588 * NOTE: HS/FS/LS devices and the SS root HUB can have
2589 * more than 15 ports
2592 rh_port = hubdev->port_no;
2601 route |= rh_port << (4 * (depth - 1));
2604 DPRINTF("Route=0x%08x\n", route);
2606 temp = XHCI_SCTX_0_ROUTE_SET(route) |
2607 XHCI_SCTX_0_CTX_NUM_SET(
2608 sc->sc_hw.devs[index].context_num + 1);
2610 switch (udev->speed) {
2612 temp |= XHCI_SCTX_0_SPEED_SET(2);
2613 if (udev->parent_hs_hub != NULL &&
2614 udev->parent_hs_hub->ddesc.bDeviceProtocol ==
2616 DPRINTF("Device inherits MTT\n");
2617 temp |= XHCI_SCTX_0_MTT_SET(1);
2620 case USB_SPEED_HIGH:
2621 temp |= XHCI_SCTX_0_SPEED_SET(3);
2622 if (sc->sc_hw.devs[index].nports != 0 &&
2623 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) {
2624 DPRINTF("HUB supports MTT\n");
2625 temp |= XHCI_SCTX_0_MTT_SET(1);
2628 case USB_SPEED_FULL:
2629 temp |= XHCI_SCTX_0_SPEED_SET(1);
2630 if (udev->parent_hs_hub != NULL &&
2631 udev->parent_hs_hub->ddesc.bDeviceProtocol ==
2633 DPRINTF("Device inherits MTT\n");
2634 temp |= XHCI_SCTX_0_MTT_SET(1);
2638 temp |= XHCI_SCTX_0_SPEED_SET(4);
2642 is_hub = sc->sc_hw.devs[index].nports != 0 &&
2643 (udev->speed == USB_SPEED_SUPER ||
2644 udev->speed == USB_SPEED_HIGH);
2647 temp |= XHCI_SCTX_0_HUB_SET(1);
2649 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx0, temp);
2651 temp = XHCI_SCTX_1_RH_PORT_SET(rh_port);
2654 temp |= XHCI_SCTX_1_NUM_PORTS_SET(
2655 sc->sc_hw.devs[index].nports);
2658 switch (udev->speed) {
2659 case USB_SPEED_SUPER:
2660 switch (sc->sc_hw.devs[index].state) {
2661 case XHCI_ST_ADDRESSED:
2662 case XHCI_ST_CONFIGURED:
2663 /* enable power save */
2664 temp |= XHCI_SCTX_1_MAX_EL_SET(sc->sc_exit_lat_max);
2667 /* disable power save */
2675 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx1, temp);
2677 temp = XHCI_SCTX_2_IRQ_TARGET_SET(0);
2680 temp |= XHCI_SCTX_2_TT_THINK_TIME_SET(
2681 sc->sc_hw.devs[index].tt);
2684 hubdev = udev->parent_hs_hub;
2686 /* check if we should activate the transaction translator */
2687 switch (udev->speed) {
2688 case USB_SPEED_FULL:
2690 if (hubdev != NULL) {
2691 temp |= XHCI_SCTX_2_TT_HUB_SID_SET(
2692 hubdev->controller_slot_id);
2693 temp |= XHCI_SCTX_2_TT_PORT_NUM_SET(
2701 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx2, temp);
2704 * These fields should be initialized to zero, according to
2705 * XHCI section 6.2.2 - slot context:
2707 temp = XHCI_SCTX_3_DEV_ADDR_SET(0) |
2708 XHCI_SCTX_3_SLOT_STATE_SET(0);
2710 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx3, temp);
2713 xhci_dump_device(sc, &pinp->ctx_slot);
2715 usb_pc_cpu_flush(pcinp);
2717 return (0); /* success */
2721 xhci_alloc_device_ext(struct usb_device *udev)
2723 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2724 struct usb_page_search buf_dev;
2725 struct usb_page_search buf_ep;
2726 struct xhci_trb *trb;
2727 struct usb_page_cache *pc;
2728 struct usb_page *pg;
2733 index = udev->controller_slot_id;
2735 pc = &sc->sc_hw.devs[index].device_pc;
2736 pg = &sc->sc_hw.devs[index].device_pg;
2738 /* need to initialize the page cache */
2739 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2741 if (usb_pc_alloc_mem(pc, pg, sc->sc_ctx_is_64_byte ?
2742 (2 * sizeof(struct xhci_dev_ctx)) :
2743 sizeof(struct xhci_dev_ctx), XHCI_PAGE_SIZE))
2746 usbd_get_page(pc, 0, &buf_dev);
2748 pc = &sc->sc_hw.devs[index].input_pc;
2749 pg = &sc->sc_hw.devs[index].input_pg;
2751 /* need to initialize the page cache */
2752 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2754 if (usb_pc_alloc_mem(pc, pg, sc->sc_ctx_is_64_byte ?
2755 (2 * sizeof(struct xhci_input_dev_ctx)) :
2756 sizeof(struct xhci_input_dev_ctx), XHCI_PAGE_SIZE)) {
2760 /* initialize all endpoint LINK TRBs */
2762 for (i = 0; i != XHCI_MAX_ENDPOINTS; i++) {
2764 pc = &sc->sc_hw.devs[index].endpoint_pc[i];
2765 pg = &sc->sc_hw.devs[index].endpoint_pg[i];
2767 /* need to initialize the page cache */
2768 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2770 if (usb_pc_alloc_mem(pc, pg,
2771 sizeof(struct xhci_dev_endpoint_trbs), XHCI_TRB_ALIGN)) {
2775 /* lookup endpoint TRB ring */
2776 usbd_get_page(pc, 0, &buf_ep);
2778 /* get TRB pointer */
2779 trb = buf_ep.buffer;
2780 trb += XHCI_MAX_TRANSFERS - 1;
2782 /* get TRB start address */
2783 addr = buf_ep.physaddr;
2785 /* create LINK TRB */
2786 trb->qwTrb0 = htole64(addr);
2787 trb->dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
2788 trb->dwTrb3 = htole32(XHCI_TRB_3_CYCLE_BIT |
2789 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
2791 usb_pc_cpu_flush(pc);
2794 xhci_set_slot_pointer(sc, index, buf_dev.physaddr);
2799 xhci_free_device_ext(udev);
2801 return (USB_ERR_NOMEM);
2805 xhci_free_device_ext(struct usb_device *udev)
2807 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2811 index = udev->controller_slot_id;
2812 xhci_set_slot_pointer(sc, index, 0);
2814 usb_pc_free_mem(&sc->sc_hw.devs[index].device_pc);
2815 usb_pc_free_mem(&sc->sc_hw.devs[index].input_pc);
2816 for (i = 0; i != XHCI_MAX_ENDPOINTS; i++)
2817 usb_pc_free_mem(&sc->sc_hw.devs[index].endpoint_pc[i]);
2820 static struct xhci_endpoint_ext *
2821 xhci_get_endpoint_ext(struct usb_device *udev, struct usb_endpoint_descriptor *edesc)
2823 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2824 struct xhci_endpoint_ext *pepext;
2825 struct usb_page_cache *pc;
2826 struct usb_page_search buf_ep;
2830 epno = edesc->bEndpointAddress;
2831 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL)
2834 epno = XHCI_EPNO2EPID(epno);
2836 index = udev->controller_slot_id;
2838 pc = &sc->sc_hw.devs[index].endpoint_pc[epno];
2840 usbd_get_page(pc, 0, &buf_ep);
2842 pepext = &sc->sc_hw.devs[index].endp[epno];
2843 pepext->page_cache = pc;
2844 pepext->trb = buf_ep.buffer;
2845 pepext->physaddr = buf_ep.physaddr;
2851 xhci_endpoint_doorbell(struct usb_xfer *xfer)
2853 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
2857 epno = xfer->endpointno;
2858 if (xfer->flags_int.control_xfr)
2861 epno = XHCI_EPNO2EPID(epno);
2862 index = xfer->xroot->udev->controller_slot_id;
2864 if (xfer->xroot->udev->flags.self_suspended == 0) {
2865 XWRITE4(sc, door, XHCI_DOORBELL(index),
2866 epno | XHCI_DB_SID_SET(xfer->stream_id));
2871 xhci_transfer_remove(struct usb_xfer *xfer, usb_error_t error)
2873 struct xhci_endpoint_ext *pepext;
2875 if (xfer->flags_int.bandwidth_reclaimed) {
2876 xfer->flags_int.bandwidth_reclaimed = 0;
2878 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2879 xfer->endpoint->edesc);
2881 pepext->trb_used[xfer->stream_id]--;
2883 pepext->xfer[xfer->qh_pos] = NULL;
2885 if (error && pepext->trb_running != 0) {
2886 pepext->trb_halted = 1;
2887 pepext->trb_running = 0;
2893 xhci_transfer_insert(struct usb_xfer *xfer)
2895 struct xhci_td *td_first;
2896 struct xhci_td *td_last;
2897 struct xhci_trb *trb_link;
2898 struct xhci_endpoint_ext *pepext;
2907 id = xfer->stream_id;
2909 /* check if already inserted */
2910 if (xfer->flags_int.bandwidth_reclaimed) {
2911 DPRINTFN(8, "Already in schedule\n");
2915 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2916 xfer->endpoint->edesc);
2918 td_first = xfer->td_transfer_first;
2919 td_last = xfer->td_transfer_last;
2920 addr = pepext->physaddr;
2922 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
2925 /* single buffered */
2929 /* multi buffered */
2930 trb_limit = (XHCI_MAX_TRANSFERS - 2);
2934 if (pepext->trb_used[id] >= trb_limit) {
2935 DPRINTFN(8, "Too many TDs queued.\n");
2936 return (USB_ERR_NOMEM);
2939 /* check if bMaxPacketSize changed */
2940 if (xfer->flags_int.control_xfr != 0 &&
2941 pepext->trb_ep_maxp != xfer->endpoint->edesc->wMaxPacketSize[0]) {
2943 DPRINTFN(8, "Reconfigure control endpoint\n");
2945 /* force driver to reconfigure endpoint */
2946 pepext->trb_halted = 1;
2947 pepext->trb_running = 0;
2950 /* check for stopped condition, after putting transfer on interrupt queue */
2951 if (pepext->trb_running == 0) {
2952 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
2954 DPRINTFN(8, "Not running\n");
2956 /* start configuration */
2957 (void)usb_proc_msignal(USB_BUS_CONTROL_XFER_PROC(&sc->sc_bus),
2958 &sc->sc_config_msg[0], &sc->sc_config_msg[1]);
2962 pepext->trb_used[id]++;
2964 /* get current TRB index */
2965 i = pepext->trb_index[id];
2967 /* get next TRB index */
2970 /* the last entry of the ring is a hardcoded link TRB */
2971 if (inext >= (XHCI_MAX_TRANSFERS - 1))
2974 /* store next TRB index, before stream ID offset is added */
2975 pepext->trb_index[id] = inext;
2977 /* offset for stream */
2978 i += id * XHCI_MAX_TRANSFERS;
2979 inext += id * XHCI_MAX_TRANSFERS;
2981 /* compute terminating return address */
2982 addr += (inext * sizeof(struct xhci_trb));
2984 /* compute link TRB pointer */
2985 trb_link = td_last->td_trb + td_last->ntrb;
2987 /* update next pointer of last link TRB */
2988 trb_link->qwTrb0 = htole64(addr);
2989 trb_link->dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
2990 trb_link->dwTrb3 = htole32(XHCI_TRB_3_IOC_BIT |
2991 XHCI_TRB_3_CYCLE_BIT |
2992 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
2995 xhci_dump_trb(&td_last->td_trb[td_last->ntrb]);
2997 usb_pc_cpu_flush(td_last->page_cache);
2999 /* write ahead chain end marker */
3001 pepext->trb[inext].qwTrb0 = 0;
3002 pepext->trb[inext].dwTrb2 = 0;
3003 pepext->trb[inext].dwTrb3 = 0;
3005 /* update next pointer of link TRB */
3007 pepext->trb[i].qwTrb0 = htole64((uint64_t)td_first->td_self);
3008 pepext->trb[i].dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
3011 xhci_dump_trb(&pepext->trb[i]);
3013 usb_pc_cpu_flush(pepext->page_cache);
3015 /* toggle cycle bit which activates the transfer chain */
3017 pepext->trb[i].dwTrb3 = htole32(XHCI_TRB_3_CYCLE_BIT |
3018 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
3020 usb_pc_cpu_flush(pepext->page_cache);
3022 DPRINTF("qh_pos = %u\n", i);
3024 pepext->xfer[i] = xfer;
3028 xfer->flags_int.bandwidth_reclaimed = 1;
3030 xhci_endpoint_doorbell(xfer);
3036 xhci_root_intr(struct xhci_softc *sc)
3040 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
3042 /* clear any old interrupt data */
3043 memset(sc->sc_hub_idata, 0, sizeof(sc->sc_hub_idata));
3045 for (i = 1; i <= sc->sc_noport; i++) {
3046 /* pick out CHANGE bits from the status register */
3047 if (XREAD4(sc, oper, XHCI_PORTSC(i)) & (
3048 XHCI_PS_CSC | XHCI_PS_PEC |
3049 XHCI_PS_OCC | XHCI_PS_WRC |
3050 XHCI_PS_PRC | XHCI_PS_PLC |
3052 sc->sc_hub_idata[i / 8] |= 1 << (i % 8);
3053 DPRINTF("port %d changed\n", i);
3056 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
3057 sizeof(sc->sc_hub_idata));
3060 /*------------------------------------------------------------------------*
3061 * xhci_device_done - XHCI done handler
3063 * NOTE: This function can be called two times in a row on
3064 * the same USB transfer. From close and from interrupt.
3065 *------------------------------------------------------------------------*/
3067 xhci_device_done(struct usb_xfer *xfer, usb_error_t error)
3069 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
3070 xfer, xfer->endpoint, error);
3072 /* remove transfer from HW queue */
3073 xhci_transfer_remove(xfer, error);
3075 /* dequeue transfer and start next transfer */
3076 usbd_transfer_done(xfer, error);
3079 /*------------------------------------------------------------------------*
3080 * XHCI data transfer support (generic type)
3081 *------------------------------------------------------------------------*/
3083 xhci_device_generic_open(struct usb_xfer *xfer)
3085 if (xfer->flags_int.isochronous_xfr) {
3086 switch (xfer->xroot->udev->speed) {
3087 case USB_SPEED_FULL:
3090 usb_hs_bandwidth_alloc(xfer);
3097 xhci_device_generic_close(struct usb_xfer *xfer)
3101 xhci_device_done(xfer, USB_ERR_CANCELLED);
3103 if (xfer->flags_int.isochronous_xfr) {
3104 switch (xfer->xroot->udev->speed) {
3105 case USB_SPEED_FULL:
3108 usb_hs_bandwidth_free(xfer);
3115 xhci_device_generic_multi_enter(struct usb_endpoint *ep,
3116 usb_stream_t stream_id, struct usb_xfer *enter_xfer)
3118 struct usb_xfer *xfer;
3120 /* check if there is a current transfer */
3121 xfer = ep->endpoint_q[stream_id].curr;
3126 * Check if the current transfer is started and then pickup
3127 * the next one, if any. Else wait for next start event due to
3128 * block on failure feature.
3130 if (!xfer->flags_int.bandwidth_reclaimed)
3133 xfer = TAILQ_FIRST(&ep->endpoint_q[stream_id].head);
3136 * In case of enter we have to consider that the
3137 * transfer is queued by the USB core after the enter
3146 /* try to multi buffer */
3147 xhci_transfer_insert(xfer);
3151 xhci_device_generic_enter(struct usb_xfer *xfer)
3155 /* set up TD's and QH */
3156 xhci_setup_generic_chain(xfer);
3158 xhci_device_generic_multi_enter(xfer->endpoint,
3159 xfer->stream_id, xfer);
3163 xhci_device_generic_start(struct usb_xfer *xfer)
3167 /* try to insert xfer on HW queue */
3168 xhci_transfer_insert(xfer);
3170 /* try to multi buffer */
3171 xhci_device_generic_multi_enter(xfer->endpoint,
3172 xfer->stream_id, NULL);
3174 /* add transfer last on interrupt queue */
3175 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
3177 /* start timeout, if any */
3178 if (xfer->timeout != 0)
3179 usbd_transfer_timeout_ms(xfer, &xhci_timeout, xfer->timeout);
3182 static const struct usb_pipe_methods xhci_device_generic_methods =
3184 .open = xhci_device_generic_open,
3185 .close = xhci_device_generic_close,
3186 .enter = xhci_device_generic_enter,
3187 .start = xhci_device_generic_start,
3190 /*------------------------------------------------------------------------*
3191 * xhci root HUB support
3192 *------------------------------------------------------------------------*
3193 * Simulate a hardware HUB by handling all the necessary requests.
3194 *------------------------------------------------------------------------*/
3196 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
3199 struct usb_device_descriptor xhci_devd =
3201 .bLength = sizeof(xhci_devd),
3202 .bDescriptorType = UDESC_DEVICE, /* type */
3203 HSETW(.bcdUSB, 0x0300), /* USB version */
3204 .bDeviceClass = UDCLASS_HUB, /* class */
3205 .bDeviceSubClass = UDSUBCLASS_HUB, /* subclass */
3206 .bDeviceProtocol = UDPROTO_SSHUB, /* protocol */
3207 .bMaxPacketSize = 9, /* max packet size */
3208 HSETW(.idVendor, 0x0000), /* vendor */
3209 HSETW(.idProduct, 0x0000), /* product */
3210 HSETW(.bcdDevice, 0x0100), /* device version */
3214 .bNumConfigurations = 1, /* # of configurations */
3218 struct xhci_bos_desc xhci_bosd = {
3220 .bLength = sizeof(xhci_bosd.bosd),
3221 .bDescriptorType = UDESC_BOS,
3222 HSETW(.wTotalLength, sizeof(xhci_bosd)),
3223 .bNumDeviceCaps = 3,
3226 .bLength = sizeof(xhci_bosd.usb2extd),
3227 .bDescriptorType = 1,
3228 .bDevCapabilityType = 2,
3229 .bmAttributes[0] = 2,
3232 .bLength = sizeof(xhci_bosd.usbdcd),
3233 .bDescriptorType = UDESC_DEVICE_CAPABILITY,
3234 .bDevCapabilityType = 3,
3235 .bmAttributes = 0, /* XXX */
3236 HSETW(.wSpeedsSupported, 0x000C),
3237 .bFunctionalitySupport = 8,
3238 .bU1DevExitLat = 255, /* dummy - not used */
3239 .wU2DevExitLat = { 0x00, 0x08 },
3242 .bLength = sizeof(xhci_bosd.cidd),
3243 .bDescriptorType = 1,
3244 .bDevCapabilityType = 4,
3246 .bContainerID = 0, /* XXX */
3251 struct xhci_config_desc xhci_confd = {
3253 .bLength = sizeof(xhci_confd.confd),
3254 .bDescriptorType = UDESC_CONFIG,
3255 .wTotalLength[0] = sizeof(xhci_confd),
3257 .bConfigurationValue = 1,
3258 .iConfiguration = 0,
3259 .bmAttributes = UC_SELF_POWERED,
3260 .bMaxPower = 0 /* max power */
3263 .bLength = sizeof(xhci_confd.ifcd),
3264 .bDescriptorType = UDESC_INTERFACE,
3266 .bInterfaceClass = UICLASS_HUB,
3267 .bInterfaceSubClass = UISUBCLASS_HUB,
3268 .bInterfaceProtocol = 0,
3271 .bLength = sizeof(xhci_confd.endpd),
3272 .bDescriptorType = UDESC_ENDPOINT,
3273 .bEndpointAddress = UE_DIR_IN | XHCI_INTR_ENDPT,
3274 .bmAttributes = UE_INTERRUPT,
3275 .wMaxPacketSize[0] = 2, /* max 15 ports */
3279 .bLength = sizeof(xhci_confd.endpcd),
3280 .bDescriptorType = UDESC_ENDPOINT_SS_COMP,
3287 struct usb_hub_ss_descriptor xhci_hubd = {
3288 .bLength = sizeof(xhci_hubd),
3289 .bDescriptorType = UDESC_SS_HUB,
3293 xhci_roothub_exec(struct usb_device *udev,
3294 struct usb_device_request *req, const void **pptr, uint16_t *plength)
3296 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3297 const char *str_ptr;
3308 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
3311 ptr = (const void *)&sc->sc_hub_desc;
3315 value = UGETW(req->wValue);
3316 index = UGETW(req->wIndex);
3318 DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
3319 "wValue=0x%04x wIndex=0x%04x\n",
3320 req->bmRequestType, req->bRequest,
3321 UGETW(req->wLength), value, index);
3323 #define C(x,y) ((x) | ((y) << 8))
3324 switch (C(req->bRequest, req->bmRequestType)) {
3325 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
3326 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
3327 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
3329 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
3330 * for the integrated root hub.
3333 case C(UR_GET_CONFIG, UT_READ_DEVICE):
3335 sc->sc_hub_desc.temp[0] = sc->sc_conf;
3337 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
3338 switch (value >> 8) {
3340 if ((value & 0xff) != 0) {
3341 err = USB_ERR_IOERROR;
3344 len = sizeof(xhci_devd);
3345 ptr = (const void *)&xhci_devd;
3349 if ((value & 0xff) != 0) {
3350 err = USB_ERR_IOERROR;
3353 len = sizeof(xhci_bosd);
3354 ptr = (const void *)&xhci_bosd;
3358 if ((value & 0xff) != 0) {
3359 err = USB_ERR_IOERROR;
3362 len = sizeof(xhci_confd);
3363 ptr = (const void *)&xhci_confd;
3367 switch (value & 0xff) {
3368 case 0: /* Language table */
3372 case 1: /* Vendor */
3373 str_ptr = sc->sc_vendor;
3376 case 2: /* Product */
3377 str_ptr = "XHCI root HUB";
3385 len = usb_make_str_desc(
3386 sc->sc_hub_desc.temp,
3387 sizeof(sc->sc_hub_desc.temp),
3392 err = USB_ERR_IOERROR;
3396 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3398 sc->sc_hub_desc.temp[0] = 0;
3400 case C(UR_GET_STATUS, UT_READ_DEVICE):
3402 USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
3404 case C(UR_GET_STATUS, UT_READ_INTERFACE):
3405 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3407 USETW(sc->sc_hub_desc.stat.wStatus, 0);
3409 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3410 if (value >= XHCI_MAX_DEVICES) {
3411 err = USB_ERR_IOERROR;
3415 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
3416 if (value != 0 && value != 1) {
3417 err = USB_ERR_IOERROR;
3420 sc->sc_conf = value;
3422 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
3424 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
3425 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
3426 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3427 err = USB_ERR_IOERROR;
3429 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
3431 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
3434 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3436 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3437 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE\n");
3440 (index > sc->sc_noport)) {
3441 err = USB_ERR_IOERROR;
3444 port = XHCI_PORTSC(index);
3446 v = XREAD4(sc, oper, port);
3447 i = XHCI_PS_PLS_GET(v);
3448 v &= ~XHCI_PS_CLEAR;
3451 case UHF_C_BH_PORT_RESET:
3452 XWRITE4(sc, oper, port, v | XHCI_PS_WRC);
3454 case UHF_C_PORT_CONFIG_ERROR:
3455 XWRITE4(sc, oper, port, v | XHCI_PS_CEC);
3457 case UHF_C_PORT_SUSPEND:
3458 case UHF_C_PORT_LINK_STATE:
3459 XWRITE4(sc, oper, port, v | XHCI_PS_PLC);
3461 case UHF_C_PORT_CONNECTION:
3462 XWRITE4(sc, oper, port, v | XHCI_PS_CSC);
3464 case UHF_C_PORT_ENABLE:
3465 XWRITE4(sc, oper, port, v | XHCI_PS_PEC);
3467 case UHF_C_PORT_OVER_CURRENT:
3468 XWRITE4(sc, oper, port, v | XHCI_PS_OCC);
3470 case UHF_C_PORT_RESET:
3471 XWRITE4(sc, oper, port, v | XHCI_PS_PRC);
3473 case UHF_PORT_ENABLE:
3474 XWRITE4(sc, oper, port, v | XHCI_PS_PED);
3476 case UHF_PORT_POWER:
3477 XWRITE4(sc, oper, port, v & ~XHCI_PS_PP);
3479 case UHF_PORT_INDICATOR:
3480 XWRITE4(sc, oper, port, v & ~XHCI_PS_PIC_SET(3));
3482 case UHF_PORT_SUSPEND:
3486 XWRITE4(sc, oper, port, v |
3487 XHCI_PS_PLS_SET(0xF) | XHCI_PS_LWS);
3490 /* wait 20ms for resume sequence to complete */
3491 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
3494 XWRITE4(sc, oper, port, v |
3495 XHCI_PS_PLS_SET(0) | XHCI_PS_LWS);
3498 err = USB_ERR_IOERROR;
3503 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3504 if ((value & 0xff) != 0) {
3505 err = USB_ERR_IOERROR;
3509 v = XREAD4(sc, capa, XHCI_HCSPARAMS0);
3511 sc->sc_hub_desc.hubd = xhci_hubd;
3513 sc->sc_hub_desc.hubd.bNbrPorts = sc->sc_noport;
3515 if (XHCI_HCS0_PPC(v))
3516 i = UHD_PWR_INDIVIDUAL;
3520 if (XHCI_HCS0_PIND(v))
3523 i |= UHD_OC_INDIVIDUAL;
3525 USETW(sc->sc_hub_desc.hubd.wHubCharacteristics, i);
3527 /* see XHCI section 5.4.9: */
3528 sc->sc_hub_desc.hubd.bPwrOn2PwrGood = 10;
3530 for (j = 1; j <= sc->sc_noport; j++) {
3532 v = XREAD4(sc, oper, XHCI_PORTSC(j));
3533 if (v & XHCI_PS_DR) {
3534 sc->sc_hub_desc.hubd.
3535 DeviceRemovable[j / 8] |= 1U << (j % 8);
3538 len = sc->sc_hub_desc.hubd.bLength;
3541 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3543 memset(sc->sc_hub_desc.temp, 0, 16);
3546 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3547 DPRINTFN(9, "UR_GET_STATUS i=%d\n", index);
3550 (index > sc->sc_noport)) {
3551 err = USB_ERR_IOERROR;
3555 v = XREAD4(sc, oper, XHCI_PORTSC(index));
3557 DPRINTFN(9, "port status=0x%08x\n", v);
3559 i = UPS_PORT_LINK_STATE_SET(XHCI_PS_PLS_GET(v));
3561 switch (XHCI_PS_SPEED_GET(v)) {
3563 i |= UPS_HIGH_SPEED;
3572 i |= UPS_OTHER_SPEED;
3576 if (v & XHCI_PS_CCS)
3577 i |= UPS_CURRENT_CONNECT_STATUS;
3578 if (v & XHCI_PS_PED)
3579 i |= UPS_PORT_ENABLED;
3580 if (v & XHCI_PS_OCA)
3581 i |= UPS_OVERCURRENT_INDICATOR;
3584 if (v & XHCI_PS_PP) {
3586 * The USB 3.0 RH is using the
3587 * USB 2.0's power bit
3589 i |= UPS_PORT_POWER;
3591 USETW(sc->sc_hub_desc.ps.wPortStatus, i);
3594 if (v & XHCI_PS_CSC)
3595 i |= UPS_C_CONNECT_STATUS;
3596 if (v & XHCI_PS_PEC)
3597 i |= UPS_C_PORT_ENABLED;
3598 if (v & XHCI_PS_OCC)
3599 i |= UPS_C_OVERCURRENT_INDICATOR;
3600 if (v & XHCI_PS_WRC)
3601 i |= UPS_C_BH_PORT_RESET;
3602 if (v & XHCI_PS_PRC)
3603 i |= UPS_C_PORT_RESET;
3604 if (v & XHCI_PS_PLC)
3605 i |= UPS_C_PORT_LINK_STATE;
3606 if (v & XHCI_PS_CEC)
3607 i |= UPS_C_PORT_CONFIG_ERROR;
3609 USETW(sc->sc_hub_desc.ps.wPortChange, i);
3610 len = sizeof(sc->sc_hub_desc.ps);
3613 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3614 err = USB_ERR_IOERROR;
3617 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3620 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3626 (index > sc->sc_noport)) {
3627 err = USB_ERR_IOERROR;
3631 port = XHCI_PORTSC(index);
3632 v = XREAD4(sc, oper, port) & ~XHCI_PS_CLEAR;
3635 case UHF_PORT_U1_TIMEOUT:
3636 if (XHCI_PS_SPEED_GET(v) != 4) {
3637 err = USB_ERR_IOERROR;
3640 port = XHCI_PORTPMSC(index);
3641 v = XREAD4(sc, oper, port);
3642 v &= ~XHCI_PM3_U1TO_SET(0xFF);
3643 v |= XHCI_PM3_U1TO_SET(i);
3644 XWRITE4(sc, oper, port, v);
3646 case UHF_PORT_U2_TIMEOUT:
3647 if (XHCI_PS_SPEED_GET(v) != 4) {
3648 err = USB_ERR_IOERROR;
3651 port = XHCI_PORTPMSC(index);
3652 v = XREAD4(sc, oper, port);
3653 v &= ~XHCI_PM3_U2TO_SET(0xFF);
3654 v |= XHCI_PM3_U2TO_SET(i);
3655 XWRITE4(sc, oper, port, v);
3657 case UHF_BH_PORT_RESET:
3658 XWRITE4(sc, oper, port, v | XHCI_PS_WPR);
3660 case UHF_PORT_LINK_STATE:
3661 XWRITE4(sc, oper, port, v |
3662 XHCI_PS_PLS_SET(i) | XHCI_PS_LWS);
3663 /* 4ms settle time */
3664 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
3666 case UHF_PORT_ENABLE:
3667 DPRINTFN(3, "set port enable %d\n", index);
3669 case UHF_PORT_SUSPEND:
3670 DPRINTFN(6, "suspend port %u (LPM=%u)\n", index, i);
3671 j = XHCI_PS_SPEED_GET(v);
3672 if ((j < 1) || (j > 3)) {
3673 /* non-supported speed */
3674 err = USB_ERR_IOERROR;
3677 XWRITE4(sc, oper, port, v |
3678 XHCI_PS_PLS_SET(i ? 2 /* LPM */ : 3) | XHCI_PS_LWS);
3680 case UHF_PORT_RESET:
3681 DPRINTFN(6, "reset port %d\n", index);
3682 XWRITE4(sc, oper, port, v | XHCI_PS_PR);
3684 case UHF_PORT_POWER:
3685 DPRINTFN(3, "set port power %d\n", index);
3686 XWRITE4(sc, oper, port, v | XHCI_PS_PP);
3689 DPRINTFN(3, "set port test %d\n", index);
3691 case UHF_PORT_INDICATOR:
3692 DPRINTFN(3, "set port indicator %d\n", index);
3694 v &= ~XHCI_PS_PIC_SET(3);
3695 v |= XHCI_PS_PIC_SET(1);
3697 XWRITE4(sc, oper, port, v);
3700 err = USB_ERR_IOERROR;
3705 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
3706 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
3707 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
3708 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
3711 err = USB_ERR_IOERROR;
3721 xhci_xfer_setup(struct usb_setup_params *parm)
3723 struct usb_page_search page_info;
3724 struct usb_page_cache *pc;
3725 struct xhci_softc *sc;
3726 struct usb_xfer *xfer;
3731 sc = XHCI_BUS2SC(parm->udev->bus);
3732 xfer = parm->curr_xfer;
3735 * The proof for the "ntd" formula is illustrated like this:
3737 * +------------------------------------+
3741 * | | xxx | x | frm 0 |
3743 * | | xxx | xx | frm 1 |
3746 * +------------------------------------+
3748 * "xxx" means a completely full USB transfer descriptor
3750 * "x" and "xx" means a short USB packet
3752 * For the remainder of an USB transfer modulo
3753 * "max_data_length" we need two USB transfer descriptors.
3754 * One to transfer the remaining data and one to finalise with
3755 * a zero length packet in case the "force_short_xfer" flag is
3756 * set. We only need two USB transfer descriptors in the case
3757 * where the transfer length of the first one is a factor of
3758 * "max_frame_size". The rest of the needed USB transfer
3759 * descriptors is given by the buffer size divided by the
3760 * maximum data payload.
3762 parm->hc_max_packet_size = 0x400;
3763 parm->hc_max_packet_count = 16 * 3;
3764 parm->hc_max_frame_size = XHCI_TD_PAYLOAD_MAX;
3766 xfer->flags_int.bdma_enable = 1;
3768 usbd_transfer_setup_sub(parm);
3770 if (xfer->flags_int.isochronous_xfr) {
3771 ntd = ((1 * xfer->nframes)
3772 + (xfer->max_data_length / xfer->max_hc_frame_size));
3773 } else if (xfer->flags_int.control_xfr) {
3774 ntd = ((2 * xfer->nframes) + 1 /* STATUS */
3775 + (xfer->max_data_length / xfer->max_hc_frame_size));
3777 ntd = ((2 * xfer->nframes)
3778 + (xfer->max_data_length / xfer->max_hc_frame_size));
3787 * Allocate queue heads and transfer descriptors
3791 if (usbd_transfer_setup_sub_malloc(
3792 parm, &pc, sizeof(struct xhci_td),
3793 XHCI_TD_ALIGN, ntd)) {
3794 parm->err = USB_ERR_NOMEM;
3798 for (n = 0; n != ntd; n++) {
3801 usbd_get_page(pc + n, 0, &page_info);
3803 td = page_info.buffer;
3806 td->td_self = page_info.physaddr;
3807 td->obj_next = last_obj;
3808 td->page_cache = pc + n;
3812 usb_pc_cpu_flush(pc + n);
3815 xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
3817 if (!xfer->flags_int.curr_dma_set) {
3818 xfer->flags_int.curr_dma_set = 1;
3824 xhci_configure_reset_endpoint(struct usb_xfer *xfer)
3826 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
3827 struct usb_page_search buf_inp;
3828 struct usb_device *udev;
3829 struct xhci_endpoint_ext *pepext;
3830 struct usb_endpoint_descriptor *edesc;
3831 struct usb_page_cache *pcinp;
3833 usb_stream_t stream_id;
3837 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
3838 xfer->endpoint->edesc);
3840 udev = xfer->xroot->udev;
3841 index = udev->controller_slot_id;
3843 pcinp = &sc->sc_hw.devs[index].input_pc;
3845 usbd_get_page(pcinp, 0, &buf_inp);
3847 edesc = xfer->endpoint->edesc;
3849 epno = edesc->bEndpointAddress;
3850 stream_id = xfer->stream_id;
3852 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL)
3855 epno = XHCI_EPNO2EPID(epno);
3858 return (USB_ERR_NO_PIPE); /* invalid */
3862 /* configure endpoint */
3864 err = xhci_configure_endpoint_by_xfer(xfer);
3867 XHCI_CMD_UNLOCK(sc);
3872 * Get the endpoint into the stopped state according to the
3873 * endpoint context state diagram in the XHCI specification:
3876 err = xhci_cmd_stop_ep(sc, 0, epno, index);
3879 DPRINTF("Could not stop endpoint %u\n", epno);
3881 err = xhci_cmd_reset_ep(sc, 0, epno, index);
3884 DPRINTF("Could not reset endpoint %u\n", epno);
3886 err = xhci_cmd_set_tr_dequeue_ptr(sc,
3887 (pepext->physaddr + (stream_id * sizeof(struct xhci_trb) *
3888 XHCI_MAX_TRANSFERS)) | XHCI_EPCTX_2_DCS_SET(1),
3889 stream_id, epno, index);
3892 DPRINTF("Could not set dequeue ptr for endpoint %u\n", epno);
3895 * Get the endpoint into the running state according to the
3896 * endpoint context state diagram in the XHCI specification:
3899 xhci_configure_mask(udev, (1U << epno) | 1U, 0);
3902 err = xhci_cmd_configure_ep(sc, buf_inp.physaddr, 0, index);
3904 err = xhci_cmd_evaluate_ctx(sc, buf_inp.physaddr, index);
3907 DPRINTF("Could not configure endpoint %u\n", epno);
3909 XHCI_CMD_UNLOCK(sc);
3915 xhci_xfer_unsetup(struct usb_xfer *xfer)
3921 xhci_start_dma_delay(struct usb_xfer *xfer)
3923 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
3925 /* put transfer on interrupt queue (again) */
3926 usbd_transfer_enqueue(&sc->sc_bus.intr_q, xfer);
3928 (void)usb_proc_msignal(USB_BUS_CONTROL_XFER_PROC(&sc->sc_bus),
3929 &sc->sc_config_msg[0], &sc->sc_config_msg[1]);
3933 xhci_configure_msg(struct usb_proc_msg *pm)
3935 struct xhci_softc *sc;
3936 struct xhci_endpoint_ext *pepext;
3937 struct usb_xfer *xfer;
3939 sc = XHCI_BUS2SC(((struct usb_bus_msg *)pm)->bus);
3942 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3944 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
3945 xfer->endpoint->edesc);
3947 if ((pepext->trb_halted != 0) ||
3948 (pepext->trb_running == 0)) {
3952 /* clear halted and running */
3953 pepext->trb_halted = 0;
3954 pepext->trb_running = 0;
3956 /* nuke remaining buffered transfers */
3958 for (i = 0; i != (XHCI_MAX_TRANSFERS *
3959 XHCI_MAX_STREAMS); i++) {
3961 * NOTE: We need to use the timeout
3962 * error code here else existing
3963 * isochronous clients can get
3966 if (pepext->xfer[i] != NULL) {
3967 xhci_device_done(pepext->xfer[i],
3973 * NOTE: The USB transfer cannot vanish in
3977 USB_BUS_UNLOCK(&sc->sc_bus);
3979 xhci_configure_reset_endpoint(xfer);
3981 USB_BUS_LOCK(&sc->sc_bus);
3983 /* check if halted is still cleared */
3984 if (pepext->trb_halted == 0) {
3985 pepext->trb_running = 1;
3986 memset(pepext->trb_index, 0,
3987 sizeof(pepext->trb_index));
3992 if (xfer->flags_int.did_dma_delay) {
3994 /* remove transfer from interrupt queue (again) */
3995 usbd_transfer_dequeue(xfer);
3997 /* we are finally done */
3998 usb_dma_delay_done_cb(xfer);
4000 /* queue changed - restart */
4005 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
4007 /* try to insert xfer on HW queue */
4008 xhci_transfer_insert(xfer);
4010 /* try to multi buffer */
4011 xhci_device_generic_multi_enter(xfer->endpoint,
4012 xfer->stream_id, NULL);
4017 xhci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
4018 struct usb_endpoint *ep)
4020 struct xhci_endpoint_ext *pepext;
4022 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d\n",
4023 ep, udev->address, edesc->bEndpointAddress, udev->flags.usb_mode);
4025 if (udev->parent_hub == NULL) {
4026 /* root HUB has special endpoint handling */
4030 ep->methods = &xhci_device_generic_methods;
4032 pepext = xhci_get_endpoint_ext(udev, edesc);
4034 USB_BUS_LOCK(udev->bus);
4035 pepext->trb_halted = 1;
4036 pepext->trb_running = 0;
4037 USB_BUS_UNLOCK(udev->bus);
4041 xhci_ep_uninit(struct usb_device *udev, struct usb_endpoint *ep)
4047 xhci_ep_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
4049 struct xhci_endpoint_ext *pepext;
4053 if (udev->flags.usb_mode != USB_MODE_HOST) {
4057 if (udev->parent_hub == NULL) {
4058 /* root HUB has special endpoint handling */
4062 pepext = xhci_get_endpoint_ext(udev, ep->edesc);
4064 USB_BUS_LOCK(udev->bus);
4065 pepext->trb_halted = 1;
4066 pepext->trb_running = 0;
4067 USB_BUS_UNLOCK(udev->bus);
4071 xhci_device_init(struct usb_device *udev)
4073 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
4077 /* no init for root HUB */
4078 if (udev->parent_hub == NULL)
4083 /* set invalid default */
4085 udev->controller_slot_id = sc->sc_noslot + 1;
4087 /* try to get a new slot ID from the XHCI */
4089 err = xhci_cmd_enable_slot(sc, &temp);
4092 XHCI_CMD_UNLOCK(sc);
4096 if (temp > sc->sc_noslot) {
4097 XHCI_CMD_UNLOCK(sc);
4098 return (USB_ERR_BAD_ADDRESS);
4101 if (sc->sc_hw.devs[temp].state != XHCI_ST_DISABLED) {
4102 DPRINTF("slot %u already allocated.\n", temp);
4103 XHCI_CMD_UNLOCK(sc);
4104 return (USB_ERR_BAD_ADDRESS);
4107 /* store slot ID for later reference */
4109 udev->controller_slot_id = temp;
4111 /* reset data structure */
4113 memset(&sc->sc_hw.devs[temp], 0, sizeof(sc->sc_hw.devs[0]));
4115 /* set mark slot allocated */
4117 sc->sc_hw.devs[temp].state = XHCI_ST_ENABLED;
4119 err = xhci_alloc_device_ext(udev);
4121 XHCI_CMD_UNLOCK(sc);
4123 /* get device into default state */
4126 err = xhci_set_address(udev, NULL, 0);
4132 xhci_device_uninit(struct usb_device *udev)
4134 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
4137 /* no init for root HUB */
4138 if (udev->parent_hub == NULL)
4143 index = udev->controller_slot_id;
4145 if (index <= sc->sc_noslot) {
4146 xhci_cmd_disable_slot(sc, index);
4147 sc->sc_hw.devs[index].state = XHCI_ST_DISABLED;
4149 /* free device extension */
4150 xhci_free_device_ext(udev);
4153 XHCI_CMD_UNLOCK(sc);
4157 xhci_get_dma_delay(struct usb_device *udev, uint32_t *pus)
4160 * Wait until the hardware has finished any possible use of
4161 * the transfer descriptor(s)
4163 *pus = 2048; /* microseconds */
4167 xhci_device_resume(struct usb_device *udev)
4169 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
4176 /* check for root HUB */
4177 if (udev->parent_hub == NULL)
4180 index = udev->controller_slot_id;
4184 /* blindly resume all endpoints */
4186 USB_BUS_LOCK(udev->bus);
4188 for (n = 1; n != XHCI_MAX_ENDPOINTS; n++) {
4189 for (p = 0; p != XHCI_MAX_STREAMS; p++) {
4190 XWRITE4(sc, door, XHCI_DOORBELL(index),
4191 n | XHCI_DB_SID_SET(p));
4195 USB_BUS_UNLOCK(udev->bus);
4197 XHCI_CMD_UNLOCK(sc);
4201 xhci_device_suspend(struct usb_device *udev)
4203 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
4210 /* check for root HUB */
4211 if (udev->parent_hub == NULL)
4214 index = udev->controller_slot_id;
4218 /* blindly suspend all endpoints */
4220 for (n = 1; n != XHCI_MAX_ENDPOINTS; n++) {
4221 err = xhci_cmd_stop_ep(sc, 1, n, index);
4223 DPRINTF("Failed to suspend endpoint "
4224 "%u on slot %u (ignored).\n", n, index);
4228 XHCI_CMD_UNLOCK(sc);
4232 xhci_set_hw_power(struct usb_bus *bus)
4238 xhci_device_state_change(struct usb_device *udev)
4240 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
4241 struct usb_page_search buf_inp;
4245 /* check for root HUB */
4246 if (udev->parent_hub == NULL)
4249 index = udev->controller_slot_id;
4253 if (usb_get_device_state(udev) == USB_STATE_CONFIGURED) {
4254 err = uhub_query_info(udev, &sc->sc_hw.devs[index].nports,
4255 &sc->sc_hw.devs[index].tt);
4257 sc->sc_hw.devs[index].nports = 0;
4262 switch (usb_get_device_state(udev)) {
4263 case USB_STATE_POWERED:
4264 if (sc->sc_hw.devs[index].state == XHCI_ST_DEFAULT)
4267 /* set default state */
4268 sc->sc_hw.devs[index].state = XHCI_ST_DEFAULT;
4270 /* reset number of contexts */
4271 sc->sc_hw.devs[index].context_num = 0;
4273 err = xhci_cmd_reset_dev(sc, index);
4276 DPRINTF("Device reset failed "
4277 "for slot %u.\n", index);
4281 case USB_STATE_ADDRESSED:
4282 if (sc->sc_hw.devs[index].state == XHCI_ST_ADDRESSED)
4285 sc->sc_hw.devs[index].state = XHCI_ST_ADDRESSED;
4287 /* set configure mask to slot only */
4288 xhci_configure_mask(udev, 1, 0);
4290 /* deconfigure all endpoints, except EP0 */
4291 err = xhci_cmd_configure_ep(sc, 0, 1, index);
4294 DPRINTF("Failed to deconfigure "
4295 "slot %u.\n", index);
4299 case USB_STATE_CONFIGURED:
4300 if (sc->sc_hw.devs[index].state == XHCI_ST_CONFIGURED)
4303 /* set configured state */
4304 sc->sc_hw.devs[index].state = XHCI_ST_CONFIGURED;
4306 /* reset number of contexts */
4307 sc->sc_hw.devs[index].context_num = 0;
4309 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
4311 xhci_configure_mask(udev, 3, 0);
4313 err = xhci_configure_device(udev);
4315 DPRINTF("Could not configure device "
4316 "at slot %u.\n", index);
4319 err = xhci_cmd_evaluate_ctx(sc, buf_inp.physaddr, index);
4321 DPRINTF("Could not evaluate device "
4322 "context at slot %u.\n", index);
4329 XHCI_CMD_UNLOCK(sc);
4333 xhci_set_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep,
4337 case USB_EP_MODE_DEFAULT:
4339 case USB_EP_MODE_STREAMS:
4340 if (xhcistreams == 0 ||
4341 (ep->edesc->bmAttributes & UE_XFERTYPE) != UE_BULK ||
4342 udev->speed != USB_SPEED_SUPER)
4343 return (USB_ERR_INVAL);
4346 return (USB_ERR_INVAL);
4350 static const struct usb_bus_methods xhci_bus_methods = {
4351 .endpoint_init = xhci_ep_init,
4352 .endpoint_uninit = xhci_ep_uninit,
4353 .xfer_setup = xhci_xfer_setup,
4354 .xfer_unsetup = xhci_xfer_unsetup,
4355 .get_dma_delay = xhci_get_dma_delay,
4356 .device_init = xhci_device_init,
4357 .device_uninit = xhci_device_uninit,
4358 .device_resume = xhci_device_resume,
4359 .device_suspend = xhci_device_suspend,
4360 .set_hw_power = xhci_set_hw_power,
4361 .roothub_exec = xhci_roothub_exec,
4362 .xfer_poll = xhci_do_poll,
4363 .start_dma_delay = xhci_start_dma_delay,
4364 .set_address = xhci_set_address,
4365 .clear_stall = xhci_ep_clear_stall,
4366 .device_state_change = xhci_device_state_change,
4367 .set_hw_power_sleep = xhci_set_hw_power_sleep,
4368 .set_endpoint_mode = xhci_set_endpoint_mode,