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_RW | CTLFLAG_TUN,
94 &xhcistreams, 0, "Set to enable streams mode support");
95 TUNABLE_INT("hw.usb.xhci.streams", &xhcistreams);
100 static int xhcipolling;
101 static int xhcidma32;
103 SYSCTL_INT(_hw_usb_xhci, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN,
104 &xhcidebug, 0, "Debug level");
105 TUNABLE_INT("hw.usb.xhci.debug", &xhcidebug);
106 SYSCTL_INT(_hw_usb_xhci, OID_AUTO, xhci_port_route, CTLFLAG_RW | CTLFLAG_TUN,
107 &xhciroute, 0, "Routing bitmap for switching EHCI ports to the XHCI controller");
108 TUNABLE_INT("hw.usb.xhci.xhci_port_route", &xhciroute);
109 SYSCTL_INT(_hw_usb_xhci, OID_AUTO, use_polling, CTLFLAG_RW | CTLFLAG_TUN,
110 &xhcipolling, 0, "Set to enable software interrupt polling for the XHCI controller");
111 TUNABLE_INT("hw.usb.xhci.use_polling", &xhcipolling);
112 SYSCTL_INT(_hw_usb_xhci, OID_AUTO, dma32, CTLFLAG_RWTUN,
113 &xhcidma32, 0, "Set to only use 32-bit DMA for the XHCI controller");
114 TUNABLE_INT("hw.usb.xhci.dma32", &xhcidma32);
120 #define XHCI_INTR_ENDPT 1
122 struct xhci_std_temp {
123 struct xhci_softc *sc;
124 struct usb_page_cache *pc;
126 struct xhci_td *td_next;
129 uint32_t max_packet_size;
141 uint8_t do_isoc_sync;
144 static void xhci_do_poll(struct usb_bus *);
145 static void xhci_device_done(struct usb_xfer *, usb_error_t);
146 static void xhci_root_intr(struct xhci_softc *);
147 static void xhci_free_device_ext(struct usb_device *);
148 static struct xhci_endpoint_ext *xhci_get_endpoint_ext(struct usb_device *,
149 struct usb_endpoint_descriptor *);
150 static usb_proc_callback_t xhci_configure_msg;
151 static usb_error_t xhci_configure_device(struct usb_device *);
152 static usb_error_t xhci_configure_endpoint(struct usb_device *,
153 struct usb_endpoint_descriptor *, struct xhci_endpoint_ext *,
154 uint16_t, uint8_t, uint8_t, uint8_t, uint16_t, uint16_t,
156 static usb_error_t xhci_configure_mask(struct usb_device *,
158 static usb_error_t xhci_cmd_evaluate_ctx(struct xhci_softc *,
160 static void xhci_endpoint_doorbell(struct usb_xfer *);
161 static void xhci_ctx_set_le32(struct xhci_softc *sc, volatile uint32_t *ptr, uint32_t val);
162 static uint32_t xhci_ctx_get_le32(struct xhci_softc *sc, volatile uint32_t *ptr);
163 static void xhci_ctx_set_le64(struct xhci_softc *sc, volatile uint64_t *ptr, uint64_t val);
165 static uint64_t xhci_ctx_get_le64(struct xhci_softc *sc, volatile uint64_t *ptr);
168 extern struct usb_bus_methods xhci_bus_methods;
172 xhci_dump_trb(struct xhci_trb *trb)
174 DPRINTFN(5, "trb = %p\n", trb);
175 DPRINTFN(5, "qwTrb0 = 0x%016llx\n", (long long)le64toh(trb->qwTrb0));
176 DPRINTFN(5, "dwTrb2 = 0x%08x\n", le32toh(trb->dwTrb2));
177 DPRINTFN(5, "dwTrb3 = 0x%08x\n", le32toh(trb->dwTrb3));
181 xhci_dump_endpoint(struct xhci_softc *sc, struct xhci_endp_ctx *pep)
183 DPRINTFN(5, "pep = %p\n", pep);
184 DPRINTFN(5, "dwEpCtx0=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx0));
185 DPRINTFN(5, "dwEpCtx1=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx1));
186 DPRINTFN(5, "qwEpCtx2=0x%016llx\n", (long long)xhci_ctx_get_le64(sc, &pep->qwEpCtx2));
187 DPRINTFN(5, "dwEpCtx4=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx4));
188 DPRINTFN(5, "dwEpCtx5=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx5));
189 DPRINTFN(5, "dwEpCtx6=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx6));
190 DPRINTFN(5, "dwEpCtx7=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx7));
194 xhci_dump_device(struct xhci_softc *sc, struct xhci_slot_ctx *psl)
196 DPRINTFN(5, "psl = %p\n", psl);
197 DPRINTFN(5, "dwSctx0=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx0));
198 DPRINTFN(5, "dwSctx1=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx1));
199 DPRINTFN(5, "dwSctx2=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx2));
200 DPRINTFN(5, "dwSctx3=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx3));
205 xhci_use_polling(void)
208 return (xhcipolling != 0);
215 xhci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
217 struct xhci_softc *sc = XHCI_BUS2SC(bus);
220 cb(bus, &sc->sc_hw.root_pc, &sc->sc_hw.root_pg,
221 sizeof(struct xhci_hw_root), XHCI_PAGE_SIZE);
223 cb(bus, &sc->sc_hw.ctx_pc, &sc->sc_hw.ctx_pg,
224 sizeof(struct xhci_dev_ctx_addr), XHCI_PAGE_SIZE);
226 for (i = 0; i != sc->sc_noscratch; i++) {
227 cb(bus, &sc->sc_hw.scratch_pc[i], &sc->sc_hw.scratch_pg[i],
228 XHCI_PAGE_SIZE, XHCI_PAGE_SIZE);
233 xhci_ctx_set_le32(struct xhci_softc *sc, volatile uint32_t *ptr, uint32_t val)
235 if (sc->sc_ctx_is_64_byte) {
237 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
238 /* all contexts are initially 32-bytes */
239 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
240 ptr = (volatile uint32_t *)(((volatile uint8_t *)ptr) + offset);
246 xhci_ctx_get_le32(struct xhci_softc *sc, volatile uint32_t *ptr)
248 if (sc->sc_ctx_is_64_byte) {
250 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
251 /* all contexts are initially 32-bytes */
252 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
253 ptr = (volatile uint32_t *)(((volatile uint8_t *)ptr) + offset);
255 return (le32toh(*ptr));
259 xhci_ctx_set_le64(struct xhci_softc *sc, volatile uint64_t *ptr, uint64_t val)
261 if (sc->sc_ctx_is_64_byte) {
263 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
264 /* all contexts are initially 32-bytes */
265 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
266 ptr = (volatile uint64_t *)(((volatile uint8_t *)ptr) + offset);
273 xhci_ctx_get_le64(struct xhci_softc *sc, volatile uint64_t *ptr)
275 if (sc->sc_ctx_is_64_byte) {
277 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
278 /* all contexts are initially 32-bytes */
279 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
280 ptr = (volatile uint64_t *)(((volatile uint8_t *)ptr) + offset);
282 return (le64toh(*ptr));
287 xhci_reset_command_queue_locked(struct xhci_softc *sc)
289 struct usb_page_search buf_res;
290 struct xhci_hw_root *phwr;
296 temp = XREAD4(sc, oper, XHCI_CRCR_LO);
297 if (temp & XHCI_CRCR_LO_CRR) {
298 DPRINTF("Command ring running\n");
299 temp &= ~(XHCI_CRCR_LO_CS | XHCI_CRCR_LO_CA);
302 * Try to abort the last command as per section
303 * 4.6.1.2 "Aborting a Command" of the XHCI
307 /* stop and cancel */
308 XWRITE4(sc, oper, XHCI_CRCR_LO, temp | XHCI_CRCR_LO_CS);
309 XWRITE4(sc, oper, XHCI_CRCR_HI, 0);
311 XWRITE4(sc, oper, XHCI_CRCR_LO, temp | XHCI_CRCR_LO_CA);
312 XWRITE4(sc, oper, XHCI_CRCR_HI, 0);
315 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 4);
317 /* check if command ring is still running */
318 temp = XREAD4(sc, oper, XHCI_CRCR_LO);
319 if (temp & XHCI_CRCR_LO_CRR) {
320 DPRINTF("Comand ring still running\n");
321 return (USB_ERR_IOERROR);
325 /* reset command ring */
326 sc->sc_command_ccs = 1;
327 sc->sc_command_idx = 0;
329 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
331 /* set up command ring control base address */
332 addr = buf_res.physaddr;
333 phwr = buf_res.buffer;
334 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_commands[0];
336 DPRINTF("CRCR=0x%016llx\n", (unsigned long long)addr);
338 memset(phwr->hwr_commands, 0, sizeof(phwr->hwr_commands));
339 phwr->hwr_commands[XHCI_MAX_COMMANDS - 1].qwTrb0 = htole64(addr);
341 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
343 XWRITE4(sc, oper, XHCI_CRCR_LO, ((uint32_t)addr) | XHCI_CRCR_LO_RCS);
344 XWRITE4(sc, oper, XHCI_CRCR_HI, (uint32_t)(addr >> 32));
350 xhci_start_controller(struct xhci_softc *sc)
352 struct usb_page_search buf_res;
353 struct xhci_hw_root *phwr;
354 struct xhci_dev_ctx_addr *pdctxa;
362 sc->sc_event_ccs = 1;
363 sc->sc_event_idx = 0;
364 sc->sc_command_ccs = 1;
365 sc->sc_command_idx = 0;
367 err = xhci_reset_controller(sc);
371 /* set up number of device slots */
372 DPRINTF("CONFIG=0x%08x -> 0x%08x\n",
373 XREAD4(sc, oper, XHCI_CONFIG), sc->sc_noslot);
375 XWRITE4(sc, oper, XHCI_CONFIG, sc->sc_noslot);
377 temp = XREAD4(sc, oper, XHCI_USBSTS);
379 /* clear interrupts */
380 XWRITE4(sc, oper, XHCI_USBSTS, temp);
381 /* disable all device notifications */
382 XWRITE4(sc, oper, XHCI_DNCTRL, 0);
384 /* set up device context base address */
385 usbd_get_page(&sc->sc_hw.ctx_pc, 0, &buf_res);
386 pdctxa = buf_res.buffer;
387 memset(pdctxa, 0, sizeof(*pdctxa));
389 addr = buf_res.physaddr;
390 addr += (uintptr_t)&((struct xhci_dev_ctx_addr *)0)->qwSpBufPtr[0];
392 /* slot 0 points to the table of scratchpad pointers */
393 pdctxa->qwBaaDevCtxAddr[0] = htole64(addr);
395 for (i = 0; i != sc->sc_noscratch; i++) {
396 struct usb_page_search buf_scp;
397 usbd_get_page(&sc->sc_hw.scratch_pc[i], 0, &buf_scp);
398 pdctxa->qwSpBufPtr[i] = htole64((uint64_t)buf_scp.physaddr);
401 addr = buf_res.physaddr;
403 XWRITE4(sc, oper, XHCI_DCBAAP_LO, (uint32_t)addr);
404 XWRITE4(sc, oper, XHCI_DCBAAP_HI, (uint32_t)(addr >> 32));
405 XWRITE4(sc, oper, XHCI_DCBAAP_LO, (uint32_t)addr);
406 XWRITE4(sc, oper, XHCI_DCBAAP_HI, (uint32_t)(addr >> 32));
408 /* set up event table size */
409 DPRINTF("ERSTSZ=0x%08x -> 0x%08x\n",
410 XREAD4(sc, runt, XHCI_ERSTSZ(0)), sc->sc_erst_max);
412 XWRITE4(sc, runt, XHCI_ERSTSZ(0), XHCI_ERSTS_SET(sc->sc_erst_max));
414 /* set up interrupt rate */
415 XWRITE4(sc, runt, XHCI_IMOD(0), sc->sc_imod_default);
417 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
419 phwr = buf_res.buffer;
420 addr = buf_res.physaddr;
421 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_events[0];
423 /* reset hardware root structure */
424 memset(phwr, 0, sizeof(*phwr));
426 phwr->hwr_ring_seg[0].qwEvrsTablePtr = htole64(addr);
427 phwr->hwr_ring_seg[0].dwEvrsTableSize = htole32(XHCI_MAX_EVENTS);
429 DPRINTF("ERDP(0)=0x%016llx\n", (unsigned long long)addr);
431 XWRITE4(sc, runt, XHCI_ERDP_LO(0), (uint32_t)addr);
432 XWRITE4(sc, runt, XHCI_ERDP_HI(0), (uint32_t)(addr >> 32));
434 addr = buf_res.physaddr;
436 DPRINTF("ERSTBA(0)=0x%016llx\n", (unsigned long long)addr);
438 XWRITE4(sc, runt, XHCI_ERSTBA_LO(0), (uint32_t)addr);
439 XWRITE4(sc, runt, XHCI_ERSTBA_HI(0), (uint32_t)(addr >> 32));
441 /* set up interrupter registers */
442 temp = XREAD4(sc, runt, XHCI_IMAN(0));
443 temp |= XHCI_IMAN_INTR_ENA;
444 XWRITE4(sc, runt, XHCI_IMAN(0), temp);
446 /* set up command ring control base address */
447 addr = buf_res.physaddr;
448 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_commands[0];
450 DPRINTF("CRCR=0x%016llx\n", (unsigned long long)addr);
452 XWRITE4(sc, oper, XHCI_CRCR_LO, ((uint32_t)addr) | XHCI_CRCR_LO_RCS);
453 XWRITE4(sc, oper, XHCI_CRCR_HI, (uint32_t)(addr >> 32));
455 phwr->hwr_commands[XHCI_MAX_COMMANDS - 1].qwTrb0 = htole64(addr);
457 usb_bus_mem_flush_all(&sc->sc_bus, &xhci_iterate_hw_softc);
460 XWRITE4(sc, oper, XHCI_USBCMD, XHCI_CMD_RS |
461 XHCI_CMD_INTE | XHCI_CMD_HSEE);
463 for (i = 0; i != 100; i++) {
464 usb_pause_mtx(NULL, hz / 100);
465 temp = XREAD4(sc, oper, XHCI_USBSTS) & XHCI_STS_HCH;
470 XWRITE4(sc, oper, XHCI_USBCMD, 0);
471 device_printf(sc->sc_bus.parent, "Run timeout.\n");
472 return (USB_ERR_IOERROR);
475 /* catch any lost interrupts */
476 xhci_do_poll(&sc->sc_bus);
478 if (sc->sc_port_route != NULL) {
479 /* Route all ports to the XHCI by default */
480 sc->sc_port_route(sc->sc_bus.parent,
481 ~xhciroute, xhciroute);
487 xhci_halt_controller(struct xhci_softc *sc)
495 sc->sc_oper_off = XREAD1(sc, capa, XHCI_CAPLENGTH);
496 sc->sc_runt_off = XREAD4(sc, capa, XHCI_RTSOFF) & ~0xF;
497 sc->sc_door_off = XREAD4(sc, capa, XHCI_DBOFF) & ~0x3;
499 /* Halt controller */
500 XWRITE4(sc, oper, XHCI_USBCMD, 0);
502 for (i = 0; i != 100; i++) {
503 usb_pause_mtx(NULL, hz / 100);
504 temp = XREAD4(sc, oper, XHCI_USBSTS) & XHCI_STS_HCH;
510 device_printf(sc->sc_bus.parent, "Controller halt timeout.\n");
511 return (USB_ERR_IOERROR);
517 xhci_reset_controller(struct xhci_softc *sc)
524 /* Reset controller */
525 XWRITE4(sc, oper, XHCI_USBCMD, XHCI_CMD_HCRST);
527 for (i = 0; i != 100; i++) {
528 usb_pause_mtx(NULL, hz / 100);
529 temp = (XREAD4(sc, oper, XHCI_USBCMD) & XHCI_CMD_HCRST) |
530 (XREAD4(sc, oper, XHCI_USBSTS) & XHCI_STS_CNR);
536 device_printf(sc->sc_bus.parent, "Controller "
538 return (USB_ERR_IOERROR);
544 xhci_init(struct xhci_softc *sc, device_t self, uint8_t dma32)
550 /* initialize some bus fields */
551 sc->sc_bus.parent = self;
553 /* set the bus revision */
554 sc->sc_bus.usbrev = USB_REV_3_0;
556 /* set up the bus struct */
557 sc->sc_bus.methods = &xhci_bus_methods;
559 /* set up devices array */
560 sc->sc_bus.devices = sc->sc_devices;
561 sc->sc_bus.devices_max = XHCI_MAX_DEVICES;
563 /* set default cycle state in case of early interrupts */
564 sc->sc_event_ccs = 1;
565 sc->sc_command_ccs = 1;
567 /* set up bus space offsets */
569 sc->sc_oper_off = XREAD1(sc, capa, XHCI_CAPLENGTH);
570 sc->sc_runt_off = XREAD4(sc, capa, XHCI_RTSOFF) & ~0x1F;
571 sc->sc_door_off = XREAD4(sc, capa, XHCI_DBOFF) & ~0x3;
573 DPRINTF("CAPLENGTH=0x%x\n", sc->sc_oper_off);
574 DPRINTF("RUNTIMEOFFSET=0x%x\n", sc->sc_runt_off);
575 DPRINTF("DOOROFFSET=0x%x\n", sc->sc_door_off);
577 DPRINTF("xHCI version = 0x%04x\n", XREAD2(sc, capa, XHCI_HCIVERSION));
579 if (!(XREAD4(sc, oper, XHCI_PAGESIZE) & XHCI_PAGESIZE_4K)) {
580 device_printf(sc->sc_bus.parent, "Controller does "
581 "not support 4K page size.\n");
585 temp = XREAD4(sc, capa, XHCI_HCSPARAMS0);
587 DPRINTF("HCS0 = 0x%08x\n", temp);
589 /* set up context size */
590 if (XHCI_HCS0_CSZ(temp)) {
591 sc->sc_ctx_is_64_byte = 1;
593 sc->sc_ctx_is_64_byte = 0;
597 sc->sc_bus.dma_bits = (XHCI_HCS0_AC64(temp) &&
598 xhcidma32 == 0 && dma32 == 0) ? 64 : 32;
600 device_printf(self, "%d bytes context size, %d-bit DMA\n",
601 sc->sc_ctx_is_64_byte ? 64 : 32, (int)sc->sc_bus.dma_bits);
603 temp = XREAD4(sc, capa, XHCI_HCSPARAMS1);
605 /* get number of device slots */
606 sc->sc_noport = XHCI_HCS1_N_PORTS(temp);
608 if (sc->sc_noport == 0) {
609 device_printf(sc->sc_bus.parent, "Invalid number "
610 "of ports: %u\n", sc->sc_noport);
614 sc->sc_noport = sc->sc_noport;
615 sc->sc_noslot = XHCI_HCS1_DEVSLOT_MAX(temp);
617 DPRINTF("Max slots: %u\n", sc->sc_noslot);
619 if (sc->sc_noslot > XHCI_MAX_DEVICES)
620 sc->sc_noslot = XHCI_MAX_DEVICES;
622 temp = XREAD4(sc, capa, XHCI_HCSPARAMS2);
624 DPRINTF("HCS2=0x%08x\n", temp);
626 /* get number of scratchpads */
627 sc->sc_noscratch = XHCI_HCS2_SPB_MAX(temp);
629 if (sc->sc_noscratch > XHCI_MAX_SCRATCHPADS) {
630 device_printf(sc->sc_bus.parent, "XHCI request "
631 "too many scratchpads\n");
635 DPRINTF("Max scratch: %u\n", sc->sc_noscratch);
637 /* get event table size */
638 sc->sc_erst_max = 1U << XHCI_HCS2_ERST_MAX(temp);
639 if (sc->sc_erst_max > XHCI_MAX_RSEG)
640 sc->sc_erst_max = XHCI_MAX_RSEG;
642 temp = XREAD4(sc, capa, XHCI_HCSPARAMS3);
644 /* get maximum exit latency */
645 sc->sc_exit_lat_max = XHCI_HCS3_U1_DEL(temp) +
646 XHCI_HCS3_U2_DEL(temp) + 250 /* us */;
648 /* Check if we should use the default IMOD value. */
649 if (sc->sc_imod_default == 0)
650 sc->sc_imod_default = XHCI_IMOD_DEFAULT;
652 /* get all DMA memory */
653 if (usb_bus_mem_alloc_all(&sc->sc_bus,
654 USB_GET_DMA_TAG(self), &xhci_iterate_hw_softc)) {
658 /* set up command queue mutex and condition varible */
659 cv_init(&sc->sc_cmd_cv, "CMDQ");
660 sx_init(&sc->sc_cmd_sx, "CMDQ lock");
662 sc->sc_config_msg[0].hdr.pm_callback = &xhci_configure_msg;
663 sc->sc_config_msg[0].bus = &sc->sc_bus;
664 sc->sc_config_msg[1].hdr.pm_callback = &xhci_configure_msg;
665 sc->sc_config_msg[1].bus = &sc->sc_bus;
671 xhci_uninit(struct xhci_softc *sc)
674 * NOTE: At this point the control transfer process is gone
675 * and "xhci_configure_msg" is no longer called. Consequently
676 * waiting for the configuration messages to complete is not
679 usb_bus_mem_free_all(&sc->sc_bus, &xhci_iterate_hw_softc);
681 cv_destroy(&sc->sc_cmd_cv);
682 sx_destroy(&sc->sc_cmd_sx);
686 xhci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
688 struct xhci_softc *sc = XHCI_BUS2SC(bus);
691 case USB_HW_POWER_SUSPEND:
692 DPRINTF("Stopping the XHCI\n");
693 xhci_halt_controller(sc);
694 xhci_reset_controller(sc);
696 case USB_HW_POWER_SHUTDOWN:
697 DPRINTF("Stopping the XHCI\n");
698 xhci_halt_controller(sc);
699 xhci_reset_controller(sc);
701 case USB_HW_POWER_RESUME:
702 DPRINTF("Starting the XHCI\n");
703 xhci_start_controller(sc);
711 xhci_generic_done_sub(struct usb_xfer *xfer)
714 struct xhci_td *td_alt_next;
718 td = xfer->td_transfer_cache;
719 td_alt_next = td->alt_next;
721 if (xfer->aframes != xfer->nframes)
722 usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
726 usb_pc_cpu_invalidate(td->page_cache);
731 DPRINTFN(4, "xfer=%p[%u/%u] rem=%u/%u status=%u\n",
732 xfer, (unsigned int)xfer->aframes,
733 (unsigned int)xfer->nframes,
734 (unsigned int)len, (unsigned int)td->len,
735 (unsigned int)status);
738 * Verify the status length and
739 * add the length to "frlengths[]":
742 /* should not happen */
743 DPRINTF("Invalid status length, "
744 "0x%04x/0x%04x bytes\n", len, td->len);
745 status = XHCI_TRB_ERROR_LENGTH;
746 } else if (xfer->aframes != xfer->nframes) {
747 xfer->frlengths[xfer->aframes] += td->len - len;
749 /* Check for last transfer */
750 if (((void *)td) == xfer->td_transfer_last) {
754 /* Check for transfer error */
755 if (status != XHCI_TRB_ERROR_SHORT_PKT &&
756 status != XHCI_TRB_ERROR_SUCCESS) {
757 /* the transfer is finished */
761 /* Check for short transfer */
763 if (xfer->flags_int.short_frames_ok ||
764 xfer->flags_int.isochronous_xfr ||
765 xfer->flags_int.control_xfr) {
766 /* follow alt next */
769 /* the transfer is finished */
776 if (td->alt_next != td_alt_next) {
777 /* this USB frame is complete */
782 /* update transfer cache */
784 xfer->td_transfer_cache = td;
786 return ((status == XHCI_TRB_ERROR_STALL) ? USB_ERR_STALLED :
787 (status != XHCI_TRB_ERROR_SHORT_PKT &&
788 status != XHCI_TRB_ERROR_SUCCESS) ? USB_ERR_IOERROR :
789 USB_ERR_NORMAL_COMPLETION);
793 xhci_generic_done(struct usb_xfer *xfer)
797 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
798 xfer, xfer->endpoint);
802 xfer->td_transfer_cache = xfer->td_transfer_first;
804 if (xfer->flags_int.control_xfr) {
806 if (xfer->flags_int.control_hdr)
807 err = xhci_generic_done_sub(xfer);
811 if (xfer->td_transfer_cache == NULL)
815 while (xfer->aframes != xfer->nframes) {
817 err = xhci_generic_done_sub(xfer);
820 if (xfer->td_transfer_cache == NULL)
824 if (xfer->flags_int.control_xfr &&
825 !xfer->flags_int.control_act)
826 err = xhci_generic_done_sub(xfer);
828 /* transfer is complete */
829 xhci_device_done(xfer, err);
833 xhci_activate_transfer(struct usb_xfer *xfer)
837 td = xfer->td_transfer_cache;
839 usb_pc_cpu_invalidate(td->page_cache);
841 if (!(td->td_trb[0].dwTrb3 & htole32(XHCI_TRB_3_CYCLE_BIT))) {
843 /* activate the transfer */
845 td->td_trb[0].dwTrb3 |= htole32(XHCI_TRB_3_CYCLE_BIT);
846 usb_pc_cpu_flush(td->page_cache);
848 xhci_endpoint_doorbell(xfer);
853 xhci_skip_transfer(struct usb_xfer *xfer)
856 struct xhci_td *td_last;
858 td = xfer->td_transfer_cache;
859 td_last = xfer->td_transfer_last;
863 usb_pc_cpu_invalidate(td->page_cache);
865 if (!(td->td_trb[0].dwTrb3 & htole32(XHCI_TRB_3_CYCLE_BIT))) {
867 usb_pc_cpu_invalidate(td_last->page_cache);
869 /* copy LINK TRB to current waiting location */
871 td->td_trb[0].qwTrb0 = td_last->td_trb[td_last->ntrb].qwTrb0;
872 td->td_trb[0].dwTrb2 = td_last->td_trb[td_last->ntrb].dwTrb2;
873 usb_pc_cpu_flush(td->page_cache);
875 td->td_trb[0].dwTrb3 = td_last->td_trb[td_last->ntrb].dwTrb3;
876 usb_pc_cpu_flush(td->page_cache);
878 xhci_endpoint_doorbell(xfer);
882 /*------------------------------------------------------------------------*
883 * xhci_check_transfer
884 *------------------------------------------------------------------------*/
886 xhci_check_transfer(struct xhci_softc *sc, struct xhci_trb *trb)
888 struct xhci_endpoint_ext *pepext;
901 td_event = le64toh(trb->qwTrb0);
902 temp = le32toh(trb->dwTrb2);
904 remainder = XHCI_TRB_2_REM_GET(temp);
905 status = XHCI_TRB_2_ERROR_GET(temp);
906 stream_id = XHCI_TRB_2_STREAM_GET(temp);
908 temp = le32toh(trb->dwTrb3);
909 epno = XHCI_TRB_3_EP_GET(temp);
910 index = XHCI_TRB_3_SLOT_GET(temp);
912 /* check if error means halted */
913 halted = (status != XHCI_TRB_ERROR_SHORT_PKT &&
914 status != XHCI_TRB_ERROR_SUCCESS);
916 DPRINTF("slot=%u epno=%u stream=%u remainder=%u status=%u\n",
917 index, epno, stream_id, remainder, status);
919 if (index > sc->sc_noslot) {
920 DPRINTF("Invalid slot.\n");
924 if ((epno == 0) || (epno >= XHCI_MAX_ENDPOINTS)) {
925 DPRINTF("Invalid endpoint.\n");
929 pepext = &sc->sc_hw.devs[index].endp[epno];
931 if (pepext->trb_ep_mode != USB_EP_MODE_STREAMS) {
933 DPRINTF("stream_id=0\n");
934 } else if (stream_id >= XHCI_MAX_STREAMS) {
935 DPRINTF("Invalid stream ID.\n");
939 /* try to find the USB transfer that generated the event */
940 for (i = 0; i != (XHCI_MAX_TRANSFERS - 1); i++) {
941 struct usb_xfer *xfer;
944 xfer = pepext->xfer[i + (XHCI_MAX_TRANSFERS * stream_id)];
948 td = xfer->td_transfer_cache;
950 DPRINTFN(5, "Checking if 0x%016llx == (0x%016llx .. 0x%016llx)\n",
952 (long long)td->td_self,
953 (long long)td->td_self + sizeof(td->td_trb));
956 * NOTE: Some XHCI implementations might not trigger
957 * an event on the last LINK TRB so we need to
958 * consider both the last and second last event
959 * address as conditions for a successful transfer.
961 * NOTE: We assume that the XHCI will only trigger one
962 * event per chain of TRBs.
965 offset = td_event - td->td_self;
968 offset < (int64_t)sizeof(td->td_trb)) {
970 usb_pc_cpu_invalidate(td->page_cache);
972 /* compute rest of remainder, if any */
973 for (i = (offset / 16) + 1; i < td->ntrb; i++) {
974 temp = le32toh(td->td_trb[i].dwTrb2);
975 remainder += XHCI_TRB_2_BYTES_GET(temp);
978 DPRINTFN(5, "New remainder: %u\n", remainder);
980 /* clear isochronous transfer errors */
981 if (xfer->flags_int.isochronous_xfr) {
984 status = XHCI_TRB_ERROR_SUCCESS;
989 /* "td->remainder" is verified later */
990 td->remainder = remainder;
993 usb_pc_cpu_flush(td->page_cache);
996 * 1) Last transfer descriptor makes the
999 if (((void *)td) == xfer->td_transfer_last) {
1000 DPRINTF("TD is last\n");
1001 xhci_generic_done(xfer);
1006 * 2) Any kind of error makes the transfer
1010 DPRINTF("TD has I/O error\n");
1011 xhci_generic_done(xfer);
1016 * 3) If there is no alternate next transfer,
1017 * a short packet also makes the transfer done
1019 if (td->remainder > 0) {
1020 if (td->alt_next == NULL) {
1022 "short TD has no alternate next\n");
1023 xhci_generic_done(xfer);
1026 DPRINTF("TD has short pkt\n");
1027 if (xfer->flags_int.short_frames_ok ||
1028 xfer->flags_int.isochronous_xfr ||
1029 xfer->flags_int.control_xfr) {
1030 /* follow the alt next */
1031 xfer->td_transfer_cache = td->alt_next;
1032 xhci_activate_transfer(xfer);
1035 xhci_skip_transfer(xfer);
1036 xhci_generic_done(xfer);
1041 * 4) Transfer complete - go to next TD
1043 DPRINTF("Following next TD\n");
1044 xfer->td_transfer_cache = td->obj_next;
1045 xhci_activate_transfer(xfer);
1046 break; /* there should only be one match */
1052 xhci_check_command(struct xhci_softc *sc, struct xhci_trb *trb)
1054 if (sc->sc_cmd_addr == trb->qwTrb0) {
1055 DPRINTF("Received command event\n");
1056 sc->sc_cmd_result[0] = trb->dwTrb2;
1057 sc->sc_cmd_result[1] = trb->dwTrb3;
1058 cv_signal(&sc->sc_cmd_cv);
1059 return (1); /* command match */
1065 xhci_interrupt_poll(struct xhci_softc *sc)
1067 struct usb_page_search buf_res;
1068 struct xhci_hw_root *phwr;
1078 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
1080 phwr = buf_res.buffer;
1082 /* Receive any events */
1084 usb_pc_cpu_invalidate(&sc->sc_hw.root_pc);
1086 i = sc->sc_event_idx;
1087 j = sc->sc_event_ccs;
1092 temp = le32toh(phwr->hwr_events[i].dwTrb3);
1094 k = (temp & XHCI_TRB_3_CYCLE_BIT) ? 1 : 0;
1099 event = XHCI_TRB_3_TYPE_GET(temp);
1101 DPRINTFN(10, "event[%u] = %u (0x%016llx 0x%08lx 0x%08lx)\n",
1102 i, event, (long long)le64toh(phwr->hwr_events[i].qwTrb0),
1103 (long)le32toh(phwr->hwr_events[i].dwTrb2),
1104 (long)le32toh(phwr->hwr_events[i].dwTrb3));
1107 case XHCI_TRB_EVENT_TRANSFER:
1108 xhci_check_transfer(sc, &phwr->hwr_events[i]);
1110 case XHCI_TRB_EVENT_CMD_COMPLETE:
1111 retval |= xhci_check_command(sc, &phwr->hwr_events[i]);
1114 DPRINTF("Unhandled event = %u\n", event);
1120 if (i == XHCI_MAX_EVENTS) {
1124 /* check for timeout */
1130 sc->sc_event_idx = i;
1131 sc->sc_event_ccs = j;
1134 * NOTE: The Event Ring Dequeue Pointer Register is 64-bit
1135 * latched. That means to activate the register we need to
1136 * write both the low and high double word of the 64-bit
1140 addr = buf_res.physaddr;
1141 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_events[i];
1143 /* try to clear busy bit */
1144 addr |= XHCI_ERDP_LO_BUSY;
1146 XWRITE4(sc, runt, XHCI_ERDP_LO(0), (uint32_t)addr);
1147 XWRITE4(sc, runt, XHCI_ERDP_HI(0), (uint32_t)(addr >> 32));
1153 xhci_do_command(struct xhci_softc *sc, struct xhci_trb *trb,
1154 uint16_t timeout_ms)
1156 struct usb_page_search buf_res;
1157 struct xhci_hw_root *phwr;
1162 uint8_t timeout = 0;
1165 XHCI_CMD_ASSERT_LOCKED(sc);
1167 /* get hardware root structure */
1169 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
1171 phwr = buf_res.buffer;
1175 USB_BUS_LOCK(&sc->sc_bus);
1177 i = sc->sc_command_idx;
1178 j = sc->sc_command_ccs;
1180 DPRINTFN(10, "command[%u] = %u (0x%016llx, 0x%08lx, 0x%08lx)\n",
1181 i, XHCI_TRB_3_TYPE_GET(le32toh(trb->dwTrb3)),
1182 (long long)le64toh(trb->qwTrb0),
1183 (long)le32toh(trb->dwTrb2),
1184 (long)le32toh(trb->dwTrb3));
1186 phwr->hwr_commands[i].qwTrb0 = trb->qwTrb0;
1187 phwr->hwr_commands[i].dwTrb2 = trb->dwTrb2;
1189 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1194 temp |= htole32(XHCI_TRB_3_CYCLE_BIT);
1196 temp &= ~htole32(XHCI_TRB_3_CYCLE_BIT);
1198 temp &= ~htole32(XHCI_TRB_3_TC_BIT);
1200 phwr->hwr_commands[i].dwTrb3 = temp;
1202 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1204 addr = buf_res.physaddr;
1205 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_commands[i];
1207 sc->sc_cmd_addr = htole64(addr);
1211 if (i == (XHCI_MAX_COMMANDS - 1)) {
1214 temp = htole32(XHCI_TRB_3_TC_BIT |
1215 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) |
1216 XHCI_TRB_3_CYCLE_BIT);
1218 temp = htole32(XHCI_TRB_3_TC_BIT |
1219 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
1222 phwr->hwr_commands[i].dwTrb3 = temp;
1224 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1230 sc->sc_command_idx = i;
1231 sc->sc_command_ccs = j;
1233 XWRITE4(sc, door, XHCI_DOORBELL(0), 0);
1235 err = cv_timedwait(&sc->sc_cmd_cv, &sc->sc_bus.bus_mtx,
1236 USB_MS_TO_TICKS(timeout_ms));
1239 * In some error cases event interrupts are not generated.
1240 * Poll one time to see if the command has completed.
1242 if (err != 0 && xhci_interrupt_poll(sc) != 0) {
1243 DPRINTF("Command was completed when polling\n");
1247 DPRINTF("Command timeout!\n");
1249 * After some weeks of continuous operation, it has
1250 * been observed that the ASMedia Technology, ASM1042
1251 * SuperSpeed USB Host Controller can suddenly stop
1252 * accepting commands via the command queue. Try to
1253 * first reset the command queue. If that fails do a
1254 * host controller reset.
1257 xhci_reset_command_queue_locked(sc) == 0) {
1258 temp = le32toh(trb->dwTrb3);
1261 * Avoid infinite XHCI reset loops if the set
1262 * address command fails to respond due to a
1263 * non-enumerating device:
1265 if (XHCI_TRB_3_TYPE_GET(temp) == XHCI_TRB_TYPE_ADDRESS_DEVICE &&
1266 (temp & XHCI_TRB_3_BSR_BIT) == 0) {
1267 DPRINTF("Set address timeout\n");
1273 DPRINTF("Controller reset!\n");
1274 usb_bus_reset_async_locked(&sc->sc_bus);
1276 err = USB_ERR_TIMEOUT;
1280 temp = le32toh(sc->sc_cmd_result[0]);
1281 if (XHCI_TRB_2_ERROR_GET(temp) != XHCI_TRB_ERROR_SUCCESS)
1282 err = USB_ERR_IOERROR;
1284 trb->dwTrb2 = sc->sc_cmd_result[0];
1285 trb->dwTrb3 = sc->sc_cmd_result[1];
1288 USB_BUS_UNLOCK(&sc->sc_bus);
1295 xhci_cmd_nop(struct xhci_softc *sc)
1297 struct xhci_trb trb;
1304 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NOOP);
1306 trb.dwTrb3 = htole32(temp);
1308 return (xhci_do_command(sc, &trb, 100 /* ms */));
1313 xhci_cmd_enable_slot(struct xhci_softc *sc, uint8_t *pslot)
1315 struct xhci_trb trb;
1323 trb.dwTrb3 = htole32(XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ENABLE_SLOT));
1325 err = xhci_do_command(sc, &trb, 100 /* ms */);
1329 temp = le32toh(trb.dwTrb3);
1331 *pslot = XHCI_TRB_3_SLOT_GET(temp);
1338 xhci_cmd_disable_slot(struct xhci_softc *sc, uint8_t slot_id)
1340 struct xhci_trb trb;
1347 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DISABLE_SLOT) |
1348 XHCI_TRB_3_SLOT_SET(slot_id);
1350 trb.dwTrb3 = htole32(temp);
1352 return (xhci_do_command(sc, &trb, 100 /* ms */));
1356 xhci_cmd_set_address(struct xhci_softc *sc, uint64_t input_ctx,
1357 uint8_t bsr, uint8_t slot_id)
1359 struct xhci_trb trb;
1364 trb.qwTrb0 = htole64(input_ctx);
1366 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ADDRESS_DEVICE) |
1367 XHCI_TRB_3_SLOT_SET(slot_id);
1370 temp |= XHCI_TRB_3_BSR_BIT;
1372 trb.dwTrb3 = htole32(temp);
1374 return (xhci_do_command(sc, &trb, 500 /* ms */));
1378 xhci_set_address(struct usb_device *udev, struct mtx *mtx, uint16_t address)
1380 struct usb_page_search buf_inp;
1381 struct usb_page_search buf_dev;
1382 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
1383 struct xhci_hw_dev *hdev;
1384 struct xhci_dev_ctx *pdev;
1385 struct xhci_endpoint_ext *pepext;
1391 /* the root HUB case is not handled here */
1392 if (udev->parent_hub == NULL)
1393 return (USB_ERR_INVAL);
1395 index = udev->controller_slot_id;
1397 hdev = &sc->sc_hw.devs[index];
1404 switch (hdev->state) {
1405 case XHCI_ST_DEFAULT:
1406 case XHCI_ST_ENABLED:
1408 hdev->state = XHCI_ST_ENABLED;
1410 /* set configure mask to slot and EP0 */
1411 xhci_configure_mask(udev, 3, 0);
1413 /* configure input slot context structure */
1414 err = xhci_configure_device(udev);
1417 DPRINTF("Could not configure device\n");
1421 /* configure input endpoint context structure */
1422 switch (udev->speed) {
1424 case USB_SPEED_FULL:
1427 case USB_SPEED_HIGH:
1435 pepext = xhci_get_endpoint_ext(udev,
1436 &udev->ctrl_ep_desc);
1438 /* ensure the control endpoint is setup again */
1439 USB_BUS_LOCK(udev->bus);
1440 pepext->trb_halted = 1;
1441 pepext->trb_running = 0;
1442 USB_BUS_UNLOCK(udev->bus);
1444 err = xhci_configure_endpoint(udev,
1445 &udev->ctrl_ep_desc, pepext,
1446 0, 1, 1, 0, mps, mps, USB_EP_MODE_DEFAULT);
1449 DPRINTF("Could not configure default endpoint\n");
1453 /* execute set address command */
1454 usbd_get_page(&hdev->input_pc, 0, &buf_inp);
1456 err = xhci_cmd_set_address(sc, buf_inp.physaddr,
1457 (address == 0), index);
1460 temp = le32toh(sc->sc_cmd_result[0]);
1461 if (address == 0 && sc->sc_port_route != NULL &&
1462 XHCI_TRB_2_ERROR_GET(temp) ==
1463 XHCI_TRB_ERROR_PARAMETER) {
1464 /* LynxPoint XHCI - ports are not switchable */
1465 /* Un-route all ports from the XHCI */
1466 sc->sc_port_route(sc->sc_bus.parent, 0, ~0);
1468 DPRINTF("Could not set address "
1469 "for slot %u.\n", index);
1474 /* update device address to new value */
1476 usbd_get_page(&hdev->device_pc, 0, &buf_dev);
1477 pdev = buf_dev.buffer;
1478 usb_pc_cpu_invalidate(&hdev->device_pc);
1480 temp = xhci_ctx_get_le32(sc, &pdev->ctx_slot.dwSctx3);
1481 udev->address = XHCI_SCTX_3_DEV_ADDR_GET(temp);
1483 /* update device state to new value */
1486 hdev->state = XHCI_ST_ADDRESSED;
1488 hdev->state = XHCI_ST_DEFAULT;
1492 DPRINTF("Wrong state for set address.\n");
1493 err = USB_ERR_IOERROR;
1496 XHCI_CMD_UNLOCK(sc);
1505 xhci_cmd_configure_ep(struct xhci_softc *sc, uint64_t input_ctx,
1506 uint8_t deconfigure, uint8_t slot_id)
1508 struct xhci_trb trb;
1513 trb.qwTrb0 = htole64(input_ctx);
1515 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_CONFIGURE_EP) |
1516 XHCI_TRB_3_SLOT_SET(slot_id);
1519 temp |= XHCI_TRB_3_DCEP_BIT;
1521 trb.dwTrb3 = htole32(temp);
1523 return (xhci_do_command(sc, &trb, 100 /* ms */));
1527 xhci_cmd_evaluate_ctx(struct xhci_softc *sc, uint64_t input_ctx,
1530 struct xhci_trb trb;
1535 trb.qwTrb0 = htole64(input_ctx);
1537 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVALUATE_CTX) |
1538 XHCI_TRB_3_SLOT_SET(slot_id);
1539 trb.dwTrb3 = htole32(temp);
1541 return (xhci_do_command(sc, &trb, 100 /* ms */));
1545 xhci_cmd_reset_ep(struct xhci_softc *sc, uint8_t preserve,
1546 uint8_t ep_id, uint8_t slot_id)
1548 struct xhci_trb trb;
1555 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_EP) |
1556 XHCI_TRB_3_SLOT_SET(slot_id) |
1557 XHCI_TRB_3_EP_SET(ep_id);
1560 temp |= XHCI_TRB_3_PRSV_BIT;
1562 trb.dwTrb3 = htole32(temp);
1564 return (xhci_do_command(sc, &trb, 100 /* ms */));
1568 xhci_cmd_set_tr_dequeue_ptr(struct xhci_softc *sc, uint64_t dequeue_ptr,
1569 uint16_t stream_id, uint8_t ep_id, uint8_t slot_id)
1571 struct xhci_trb trb;
1576 trb.qwTrb0 = htole64(dequeue_ptr);
1578 temp = XHCI_TRB_2_STREAM_SET(stream_id);
1579 trb.dwTrb2 = htole32(temp);
1581 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SET_TR_DEQUEUE) |
1582 XHCI_TRB_3_SLOT_SET(slot_id) |
1583 XHCI_TRB_3_EP_SET(ep_id);
1584 trb.dwTrb3 = htole32(temp);
1586 return (xhci_do_command(sc, &trb, 100 /* ms */));
1590 xhci_cmd_stop_ep(struct xhci_softc *sc, uint8_t suspend,
1591 uint8_t ep_id, uint8_t slot_id)
1593 struct xhci_trb trb;
1600 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STOP_EP) |
1601 XHCI_TRB_3_SLOT_SET(slot_id) |
1602 XHCI_TRB_3_EP_SET(ep_id);
1605 temp |= XHCI_TRB_3_SUSP_EP_BIT;
1607 trb.dwTrb3 = htole32(temp);
1609 return (xhci_do_command(sc, &trb, 100 /* ms */));
1613 xhci_cmd_reset_dev(struct xhci_softc *sc, uint8_t slot_id)
1615 struct xhci_trb trb;
1622 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_DEVICE) |
1623 XHCI_TRB_3_SLOT_SET(slot_id);
1625 trb.dwTrb3 = htole32(temp);
1627 return (xhci_do_command(sc, &trb, 100 /* ms */));
1630 /*------------------------------------------------------------------------*
1631 * xhci_interrupt - XHCI interrupt handler
1632 *------------------------------------------------------------------------*/
1634 xhci_interrupt(struct xhci_softc *sc)
1639 USB_BUS_LOCK(&sc->sc_bus);
1641 status = XREAD4(sc, oper, XHCI_USBSTS);
1643 /* acknowledge interrupts, if any */
1645 XWRITE4(sc, oper, XHCI_USBSTS, status);
1646 DPRINTFN(16, "real interrupt (status=0x%08x)\n", status);
1649 temp = XREAD4(sc, runt, XHCI_IMAN(0));
1651 /* force clearing of pending interrupts */
1652 if (temp & XHCI_IMAN_INTR_PEND)
1653 XWRITE4(sc, runt, XHCI_IMAN(0), temp);
1655 /* check for event(s) */
1656 xhci_interrupt_poll(sc);
1658 if (status & (XHCI_STS_PCD | XHCI_STS_HCH |
1659 XHCI_STS_HSE | XHCI_STS_HCE)) {
1661 if (status & XHCI_STS_PCD) {
1665 if (status & XHCI_STS_HCH) {
1666 printf("%s: host controller halted\n",
1670 if (status & XHCI_STS_HSE) {
1671 printf("%s: host system error\n",
1675 if (status & XHCI_STS_HCE) {
1676 printf("%s: host controller error\n",
1680 USB_BUS_UNLOCK(&sc->sc_bus);
1683 /*------------------------------------------------------------------------*
1684 * xhci_timeout - XHCI timeout handler
1685 *------------------------------------------------------------------------*/
1687 xhci_timeout(void *arg)
1689 struct usb_xfer *xfer = arg;
1691 DPRINTF("xfer=%p\n", xfer);
1693 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1695 /* transfer is transferred */
1696 xhci_device_done(xfer, USB_ERR_TIMEOUT);
1700 xhci_do_poll(struct usb_bus *bus)
1702 struct xhci_softc *sc = XHCI_BUS2SC(bus);
1704 USB_BUS_LOCK(&sc->sc_bus);
1705 xhci_interrupt_poll(sc);
1706 USB_BUS_UNLOCK(&sc->sc_bus);
1710 xhci_setup_generic_chain_sub(struct xhci_std_temp *temp)
1712 struct usb_page_search buf_res;
1714 struct xhci_td *td_next;
1715 struct xhci_td *td_alt_next;
1716 struct xhci_td *td_first;
1717 uint32_t buf_offset;
1722 uint8_t shortpkt_old;
1728 shortpkt_old = temp->shortpkt;
1729 len_old = temp->len;
1736 td_next = td_first = temp->td_next;
1740 if (temp->len == 0) {
1745 /* send a Zero Length Packet, ZLP, last */
1752 average = temp->average;
1754 if (temp->len < average) {
1755 if (temp->len % temp->max_packet_size) {
1758 average = temp->len;
1762 if (td_next == NULL)
1763 panic("%s: out of XHCI transfer descriptors!", __FUNCTION__);
1768 td_next = td->obj_next;
1770 /* check if we are pre-computing */
1774 /* update remaining length */
1776 temp->len -= average;
1780 /* fill out current TD */
1786 /* update remaining length */
1788 temp->len -= average;
1790 /* reset TRB index */
1794 if (temp->trb_type == XHCI_TRB_TYPE_SETUP_STAGE) {
1795 /* immediate data */
1800 td->td_trb[0].qwTrb0 = 0;
1802 usbd_copy_out(temp->pc, temp->offset + buf_offset,
1803 (uint8_t *)(uintptr_t)&td->td_trb[0].qwTrb0,
1806 dword = XHCI_TRB_2_BYTES_SET(8) |
1807 XHCI_TRB_2_TDSZ_SET(0) |
1808 XHCI_TRB_2_IRQ_SET(0);
1810 td->td_trb[0].dwTrb2 = htole32(dword);
1812 dword = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SETUP_STAGE) |
1813 XHCI_TRB_3_IDT_BIT | XHCI_TRB_3_CYCLE_BIT;
1816 if (td->td_trb[0].qwTrb0 &
1817 htole64(XHCI_TRB_0_WLENGTH_MASK)) {
1818 if (td->td_trb[0].qwTrb0 &
1819 htole64(XHCI_TRB_0_DIR_IN_MASK))
1820 dword |= XHCI_TRB_3_TRT_IN;
1822 dword |= XHCI_TRB_3_TRT_OUT;
1825 td->td_trb[0].dwTrb3 = htole32(dword);
1827 xhci_dump_trb(&td->td_trb[x]);
1835 /* fill out buffer pointers */
1838 memset(&buf_res, 0, sizeof(buf_res));
1840 usbd_get_page(temp->pc, temp->offset +
1841 buf_offset, &buf_res);
1843 /* get length to end of page */
1844 if (buf_res.length > average)
1845 buf_res.length = average;
1847 /* check for maximum length */
1848 if (buf_res.length > XHCI_TD_PAGE_SIZE)
1849 buf_res.length = XHCI_TD_PAGE_SIZE;
1851 npkt_off += buf_res.length;
1855 npkt = (len_old - npkt_off + temp->max_packet_size - 1) /
1856 temp->max_packet_size;
1863 /* fill out TRB's */
1864 td->td_trb[x].qwTrb0 =
1865 htole64((uint64_t)buf_res.physaddr);
1868 XHCI_TRB_2_BYTES_SET(buf_res.length) |
1869 XHCI_TRB_2_TDSZ_SET(npkt) |
1870 XHCI_TRB_2_IRQ_SET(0);
1872 td->td_trb[x].dwTrb2 = htole32(dword);
1874 switch (temp->trb_type) {
1875 case XHCI_TRB_TYPE_ISOCH:
1876 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1877 XHCI_TRB_3_TBC_SET(temp->tbc) |
1878 XHCI_TRB_3_TLBPC_SET(temp->tlbpc);
1879 if (td != td_first) {
1880 dword |= XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL);
1881 } else if (temp->do_isoc_sync != 0) {
1882 temp->do_isoc_sync = 0;
1883 /* wait until "isoc_frame" */
1884 dword |= XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ISOCH) |
1885 XHCI_TRB_3_FRID_SET(temp->isoc_frame / 8);
1887 /* start data transfer at next interval */
1888 dword |= XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ISOCH) |
1889 XHCI_TRB_3_ISO_SIA_BIT;
1891 if (temp->direction == UE_DIR_IN)
1892 dword |= XHCI_TRB_3_ISP_BIT;
1894 case XHCI_TRB_TYPE_DATA_STAGE:
1895 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1896 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DATA_STAGE);
1897 if (temp->direction == UE_DIR_IN)
1898 dword |= XHCI_TRB_3_DIR_IN | XHCI_TRB_3_ISP_BIT;
1900 * Section 3.2.9 in the XHCI
1901 * specification about control
1902 * transfers says that we should use a
1903 * normal-TRB if there are more TRBs
1904 * extending the data-stage
1905 * TRB. Update the "trb_type".
1907 temp->trb_type = XHCI_TRB_TYPE_NORMAL;
1909 case XHCI_TRB_TYPE_STATUS_STAGE:
1910 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1911 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STATUS_STAGE);
1912 if (temp->direction == UE_DIR_IN)
1913 dword |= XHCI_TRB_3_DIR_IN;
1915 default: /* XHCI_TRB_TYPE_NORMAL */
1916 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1917 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL);
1918 if (temp->direction == UE_DIR_IN)
1919 dword |= XHCI_TRB_3_ISP_BIT;
1922 td->td_trb[x].dwTrb3 = htole32(dword);
1924 average -= buf_res.length;
1925 buf_offset += buf_res.length;
1927 xhci_dump_trb(&td->td_trb[x]);
1931 } while (average != 0);
1933 td->td_trb[x-1].dwTrb3 |= htole32(XHCI_TRB_3_IOC_BIT);
1935 /* store number of data TRB's */
1939 DPRINTF("NTRB=%u\n", x);
1941 /* fill out link TRB */
1943 if (td_next != NULL) {
1944 /* link the current TD with the next one */
1945 td->td_trb[x].qwTrb0 = htole64((uint64_t)td_next->td_self);
1946 DPRINTF("LINK=0x%08llx\n", (long long)td_next->td_self);
1948 /* this field will get updated later */
1949 DPRINTF("NOLINK\n");
1952 dword = XHCI_TRB_2_IRQ_SET(0);
1954 td->td_trb[x].dwTrb2 = htole32(dword);
1956 dword = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) |
1957 XHCI_TRB_3_CYCLE_BIT | XHCI_TRB_3_IOC_BIT |
1959 * CHAIN-BIT: Ensure that a multi-TRB IN-endpoint
1960 * frame only receives a single short packet event
1961 * by setting the CHAIN bit in the LINK field. In
1962 * addition some XHCI controllers have problems
1963 * sending a ZLP unless the CHAIN-BIT is set in
1966 XHCI_TRB_3_CHAIN_BIT;
1968 td->td_trb[x].dwTrb3 = htole32(dword);
1970 td->alt_next = td_alt_next;
1972 xhci_dump_trb(&td->td_trb[x]);
1974 usb_pc_cpu_flush(td->page_cache);
1980 /* set up alt next pointer, if any */
1981 if (temp->last_frame) {
1984 /* we use this field internally */
1985 td_alt_next = td_next;
1989 temp->shortpkt = shortpkt_old;
1990 temp->len = len_old;
1995 * Remove cycle bit from the first TRB if we are
1998 if (temp->step_td != 0) {
1999 td_first->td_trb[0].dwTrb3 &= ~htole32(XHCI_TRB_3_CYCLE_BIT);
2000 usb_pc_cpu_flush(td_first->page_cache);
2003 /* clear TD SIZE to zero, hence this is the last TRB */
2004 /* remove chain bit because this is the last data TRB in the chain */
2005 td->td_trb[td->ntrb - 1].dwTrb2 &= ~htole32(XHCI_TRB_2_TDSZ_SET(15));
2006 td->td_trb[td->ntrb - 1].dwTrb3 &= ~htole32(XHCI_TRB_3_CHAIN_BIT);
2007 /* remove CHAIN-BIT from last LINK TRB */
2008 td->td_trb[td->ntrb].dwTrb3 &= ~htole32(XHCI_TRB_3_CHAIN_BIT);
2010 usb_pc_cpu_flush(td->page_cache);
2013 temp->td_next = td_next;
2017 xhci_setup_generic_chain(struct usb_xfer *xfer)
2019 struct xhci_std_temp temp;
2025 temp.do_isoc_sync = 0;
2029 temp.average = xfer->max_hc_frame_size;
2030 temp.max_packet_size = xfer->max_packet_size;
2031 temp.sc = XHCI_BUS2SC(xfer->xroot->bus);
2033 temp.last_frame = 0;
2035 temp.multishort = xfer->flags_int.isochronous_xfr ||
2036 xfer->flags_int.control_xfr ||
2037 xfer->flags_int.short_frames_ok;
2039 /* toggle the DMA set we are using */
2040 xfer->flags_int.curr_dma_set ^= 1;
2042 /* get next DMA set */
2043 td = xfer->td_start[xfer->flags_int.curr_dma_set];
2048 xfer->td_transfer_first = td;
2049 xfer->td_transfer_cache = td;
2051 if (xfer->flags_int.isochronous_xfr) {
2054 /* compute multiplier for ISOCHRONOUS transfers */
2055 mult = xfer->endpoint->ecomp ?
2056 UE_GET_SS_ISO_MULT(xfer->endpoint->ecomp->bmAttributes)
2058 /* check for USB 2.0 multiplier */
2060 mult = (xfer->endpoint->edesc->
2061 wMaxPacketSize[1] >> 3) & 3;
2069 x = XREAD4(temp.sc, runt, XHCI_MFINDEX);
2071 DPRINTF("MFINDEX=0x%08x\n", x);
2073 switch (usbd_get_speed(xfer->xroot->udev)) {
2074 case USB_SPEED_FULL:
2076 temp.isoc_delta = 8; /* 1ms */
2077 x += temp.isoc_delta - 1;
2078 x &= ~(temp.isoc_delta - 1);
2081 shift = usbd_xfer_get_fps_shift(xfer);
2082 temp.isoc_delta = 1U << shift;
2083 x += temp.isoc_delta - 1;
2084 x &= ~(temp.isoc_delta - 1);
2085 /* simple frame load balancing */
2086 x += xfer->endpoint->usb_uframe;
2090 y = XHCI_MFINDEX_GET(x - xfer->endpoint->isoc_next);
2092 if ((xfer->endpoint->is_synced == 0) ||
2093 (y < (xfer->nframes << shift)) ||
2094 (XHCI_MFINDEX_GET(-y) >= (128 * 8))) {
2096 * If there is data underflow or the pipe
2097 * queue is empty we schedule the transfer a
2098 * few frames ahead of the current frame
2099 * position. Else two isochronous transfers
2102 xfer->endpoint->isoc_next = XHCI_MFINDEX_GET(x + (3 * 8));
2103 xfer->endpoint->is_synced = 1;
2104 temp.do_isoc_sync = 1;
2106 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
2109 /* compute isochronous completion time */
2111 y = XHCI_MFINDEX_GET(xfer->endpoint->isoc_next - (x & ~7));
2113 xfer->isoc_time_complete =
2114 usb_isoc_time_expand(&temp.sc->sc_bus, x / 8) +
2115 (y / 8) + (((xfer->nframes << shift) + 7) / 8);
2118 temp.isoc_frame = xfer->endpoint->isoc_next;
2119 temp.trb_type = XHCI_TRB_TYPE_ISOCH;
2121 xfer->endpoint->isoc_next += xfer->nframes << shift;
2123 } else if (xfer->flags_int.control_xfr) {
2125 /* check if we should prepend a setup message */
2127 if (xfer->flags_int.control_hdr) {
2129 temp.len = xfer->frlengths[0];
2130 temp.pc = xfer->frbuffers + 0;
2131 temp.shortpkt = temp.len ? 1 : 0;
2132 temp.trb_type = XHCI_TRB_TYPE_SETUP_STAGE;
2135 /* check for last frame */
2136 if (xfer->nframes == 1) {
2137 /* no STATUS stage yet, SETUP is last */
2138 if (xfer->flags_int.control_act)
2139 temp.last_frame = 1;
2142 xhci_setup_generic_chain_sub(&temp);
2146 temp.isoc_delta = 0;
2147 temp.isoc_frame = 0;
2148 temp.trb_type = xfer->flags_int.control_did_data ?
2149 XHCI_TRB_TYPE_NORMAL : XHCI_TRB_TYPE_DATA_STAGE;
2153 temp.isoc_delta = 0;
2154 temp.isoc_frame = 0;
2155 temp.trb_type = XHCI_TRB_TYPE_NORMAL;
2158 if (x != xfer->nframes) {
2159 /* set up page_cache pointer */
2160 temp.pc = xfer->frbuffers + x;
2161 /* set endpoint direction */
2162 temp.direction = UE_GET_DIR(xfer->endpointno);
2165 while (x != xfer->nframes) {
2167 /* DATA0 / DATA1 message */
2169 temp.len = xfer->frlengths[x];
2170 temp.step_td = ((xfer->endpointno & UE_DIR_IN) &&
2171 x != 0 && temp.multishort == 0);
2175 if (x == xfer->nframes) {
2176 if (xfer->flags_int.control_xfr) {
2177 /* no STATUS stage yet, DATA is last */
2178 if (xfer->flags_int.control_act)
2179 temp.last_frame = 1;
2181 temp.last_frame = 1;
2184 if (temp.len == 0) {
2186 /* make sure that we send an USB packet */
2191 temp.tlbpc = mult - 1;
2193 } else if (xfer->flags_int.isochronous_xfr) {
2198 * Isochronous transfers don't have short
2199 * packet termination:
2204 /* isochronous transfers have a transfer limit */
2206 if (temp.len > xfer->max_frame_size)
2207 temp.len = xfer->max_frame_size;
2209 /* compute TD packet count */
2210 tdpc = (temp.len + xfer->max_packet_size - 1) /
2211 xfer->max_packet_size;
2213 temp.tbc = ((tdpc + mult - 1) / mult) - 1;
2214 temp.tlbpc = (tdpc % mult);
2216 if (temp.tlbpc == 0)
2217 temp.tlbpc = mult - 1;
2222 /* regular data transfer */
2224 temp.shortpkt = xfer->flags.force_short_xfer ? 0 : 1;
2227 xhci_setup_generic_chain_sub(&temp);
2229 if (xfer->flags_int.isochronous_xfr) {
2230 temp.offset += xfer->frlengths[x - 1];
2231 temp.isoc_frame += temp.isoc_delta;
2233 /* get next Page Cache pointer */
2234 temp.pc = xfer->frbuffers + x;
2238 /* check if we should append a status stage */
2240 if (xfer->flags_int.control_xfr &&
2241 !xfer->flags_int.control_act) {
2244 * Send a DATA1 message and invert the current
2245 * endpoint direction.
2247 #ifdef XHCI_STEP_STATUS_STAGE
2248 temp.step_td = (xfer->nframes != 0);
2252 temp.direction = UE_GET_DIR(xfer->endpointno) ^ UE_DIR_IN;
2256 temp.last_frame = 1;
2257 temp.trb_type = XHCI_TRB_TYPE_STATUS_STAGE;
2259 xhci_setup_generic_chain_sub(&temp);
2264 /* must have at least one frame! */
2266 xfer->td_transfer_last = td;
2268 DPRINTF("first=%p last=%p\n", xfer->td_transfer_first, td);
2272 xhci_set_slot_pointer(struct xhci_softc *sc, uint8_t index, uint64_t dev_addr)
2274 struct usb_page_search buf_res;
2275 struct xhci_dev_ctx_addr *pdctxa;
2277 usbd_get_page(&sc->sc_hw.ctx_pc, 0, &buf_res);
2279 pdctxa = buf_res.buffer;
2281 DPRINTF("addr[%u]=0x%016llx\n", index, (long long)dev_addr);
2283 pdctxa->qwBaaDevCtxAddr[index] = htole64(dev_addr);
2285 usb_pc_cpu_flush(&sc->sc_hw.ctx_pc);
2289 xhci_configure_mask(struct usb_device *udev, uint32_t mask, uint8_t drop)
2291 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2292 struct usb_page_search buf_inp;
2293 struct xhci_input_dev_ctx *pinp;
2298 index = udev->controller_slot_id;
2300 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
2302 pinp = buf_inp.buffer;
2305 mask &= XHCI_INCTX_NON_CTRL_MASK;
2306 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx0, mask);
2307 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx1, 0);
2310 * Some hardware requires that we drop the endpoint
2311 * context before adding it again:
2313 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx0,
2314 mask & XHCI_INCTX_NON_CTRL_MASK);
2316 /* Add new endpoint context */
2317 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx1, mask);
2319 /* find most significant set bit */
2320 for (x = 31; x != 1; x--) {
2321 if (mask & (1 << x))
2328 /* figure out the maximum number of contexts */
2329 if (x > sc->sc_hw.devs[index].context_num)
2330 sc->sc_hw.devs[index].context_num = x;
2332 x = sc->sc_hw.devs[index].context_num;
2334 /* update number of contexts */
2335 temp = xhci_ctx_get_le32(sc, &pinp->ctx_slot.dwSctx0);
2336 temp &= ~XHCI_SCTX_0_CTX_NUM_SET(31);
2337 temp |= XHCI_SCTX_0_CTX_NUM_SET(x + 1);
2338 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx0, temp);
2340 usb_pc_cpu_flush(&sc->sc_hw.devs[index].input_pc);
2345 xhci_configure_endpoint(struct usb_device *udev,
2346 struct usb_endpoint_descriptor *edesc, struct xhci_endpoint_ext *pepext,
2347 uint16_t interval, uint8_t max_packet_count,
2348 uint8_t mult, uint8_t fps_shift, uint16_t max_packet_size,
2349 uint16_t max_frame_size, uint8_t ep_mode)
2351 struct usb_page_search buf_inp;
2352 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2353 struct xhci_input_dev_ctx *pinp;
2354 uint64_t ring_addr = pepext->physaddr;
2360 index = udev->controller_slot_id;
2362 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
2364 pinp = buf_inp.buffer;
2366 epno = edesc->bEndpointAddress;
2367 type = edesc->bmAttributes & UE_XFERTYPE;
2369 if (type == UE_CONTROL)
2372 epno = XHCI_EPNO2EPID(epno);
2375 return (USB_ERR_NO_PIPE); /* invalid */
2377 if (max_packet_count == 0)
2378 return (USB_ERR_BAD_BUFSIZE);
2383 return (USB_ERR_BAD_BUFSIZE);
2385 /* store endpoint mode */
2386 pepext->trb_ep_mode = ep_mode;
2387 /* store bMaxPacketSize for control endpoints */
2388 pepext->trb_ep_maxp = edesc->wMaxPacketSize[0];
2389 usb_pc_cpu_flush(pepext->page_cache);
2391 if (ep_mode == USB_EP_MODE_STREAMS) {
2392 temp = XHCI_EPCTX_0_EPSTATE_SET(0) |
2393 XHCI_EPCTX_0_MAXP_STREAMS_SET(XHCI_MAX_STREAMS_LOG - 1) |
2394 XHCI_EPCTX_0_LSA_SET(1);
2396 ring_addr += sizeof(struct xhci_trb) *
2397 XHCI_MAX_TRANSFERS * XHCI_MAX_STREAMS;
2399 temp = XHCI_EPCTX_0_EPSTATE_SET(0) |
2400 XHCI_EPCTX_0_MAXP_STREAMS_SET(0) |
2401 XHCI_EPCTX_0_LSA_SET(0);
2403 ring_addr |= XHCI_EPCTX_2_DCS_SET(1);
2406 switch (udev->speed) {
2407 case USB_SPEED_FULL:
2420 temp |= XHCI_EPCTX_0_IVAL_SET(fps_shift);
2422 case UE_ISOCHRONOUS:
2423 temp |= XHCI_EPCTX_0_IVAL_SET(fps_shift);
2425 switch (udev->speed) {
2426 case USB_SPEED_SUPER:
2429 temp |= XHCI_EPCTX_0_MULT_SET(mult - 1);
2430 max_packet_count /= mult;
2440 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx0, temp);
2443 XHCI_EPCTX_1_HID_SET(0) |
2444 XHCI_EPCTX_1_MAXB_SET(max_packet_count) |
2445 XHCI_EPCTX_1_MAXP_SIZE_SET(max_packet_size);
2448 * Always enable the "three strikes and you are gone" feature
2449 * except for ISOCHRONOUS endpoints. This is suggested by
2450 * section 4.3.3 in the XHCI specification about device slot
2453 if (type != UE_ISOCHRONOUS)
2454 temp |= XHCI_EPCTX_1_CERR_SET(3);
2458 temp |= XHCI_EPCTX_1_EPTYPE_SET(4);
2460 case UE_ISOCHRONOUS:
2461 temp |= XHCI_EPCTX_1_EPTYPE_SET(1);
2464 temp |= XHCI_EPCTX_1_EPTYPE_SET(2);
2467 temp |= XHCI_EPCTX_1_EPTYPE_SET(3);
2471 /* check for IN direction */
2473 temp |= XHCI_EPCTX_1_EPTYPE_SET(4);
2475 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx1, temp);
2476 xhci_ctx_set_le64(sc, &pinp->ctx_ep[epno - 1].qwEpCtx2, ring_addr);
2478 switch (edesc->bmAttributes & UE_XFERTYPE) {
2480 case UE_ISOCHRONOUS:
2481 temp = XHCI_EPCTX_4_MAX_ESIT_PAYLOAD_SET(max_frame_size) |
2482 XHCI_EPCTX_4_AVG_TRB_LEN_SET(MIN(XHCI_PAGE_SIZE,
2486 temp = XHCI_EPCTX_4_AVG_TRB_LEN_SET(8);
2489 temp = XHCI_EPCTX_4_AVG_TRB_LEN_SET(XHCI_PAGE_SIZE);
2493 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx4, temp);
2496 xhci_dump_endpoint(sc, &pinp->ctx_ep[epno - 1]);
2498 usb_pc_cpu_flush(&sc->sc_hw.devs[index].input_pc);
2500 return (0); /* success */
2504 xhci_configure_endpoint_by_xfer(struct usb_xfer *xfer)
2506 struct xhci_endpoint_ext *pepext;
2507 struct usb_endpoint_ss_comp_descriptor *ecomp;
2510 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2511 xfer->endpoint->edesc);
2513 ecomp = xfer->endpoint->ecomp;
2515 for (x = 0; x != XHCI_MAX_STREAMS; x++) {
2518 /* halt any transfers */
2519 pepext->trb[x * XHCI_MAX_TRANSFERS].dwTrb3 = 0;
2521 /* compute start of TRB ring for stream "x" */
2522 temp = pepext->physaddr +
2523 (x * XHCI_MAX_TRANSFERS * sizeof(struct xhci_trb)) +
2524 XHCI_SCTX_0_SCT_SEC_TR_RING;
2526 /* make tree structure */
2527 pepext->trb[(XHCI_MAX_TRANSFERS *
2528 XHCI_MAX_STREAMS) + x].qwTrb0 = htole64(temp);
2530 /* reserved fields */
2531 pepext->trb[(XHCI_MAX_TRANSFERS *
2532 XHCI_MAX_STREAMS) + x].dwTrb2 = 0;
2533 pepext->trb[(XHCI_MAX_TRANSFERS *
2534 XHCI_MAX_STREAMS) + x].dwTrb3 = 0;
2536 usb_pc_cpu_flush(pepext->page_cache);
2538 return (xhci_configure_endpoint(xfer->xroot->udev,
2539 xfer->endpoint->edesc, pepext,
2540 xfer->interval, xfer->max_packet_count,
2541 (ecomp != NULL) ? UE_GET_SS_ISO_MULT(ecomp->bmAttributes) + 1 : 1,
2542 usbd_xfer_get_fps_shift(xfer), xfer->max_packet_size,
2543 xfer->max_frame_size, xfer->endpoint->ep_mode));
2547 xhci_configure_device(struct usb_device *udev)
2549 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2550 struct usb_page_search buf_inp;
2551 struct usb_page_cache *pcinp;
2552 struct xhci_input_dev_ctx *pinp;
2553 struct usb_device *hubdev;
2561 index = udev->controller_slot_id;
2563 DPRINTF("index=%u\n", index);
2565 pcinp = &sc->sc_hw.devs[index].input_pc;
2567 usbd_get_page(pcinp, 0, &buf_inp);
2569 pinp = buf_inp.buffer;
2574 /* figure out route string and root HUB port number */
2576 for (hubdev = udev; hubdev != NULL; hubdev = hubdev->parent_hub) {
2578 if (hubdev->parent_hub == NULL)
2581 depth = hubdev->parent_hub->depth;
2584 * NOTE: HS/FS/LS devices and the SS root HUB can have
2585 * more than 15 ports
2588 rh_port = hubdev->port_no;
2597 route |= rh_port << (4 * (depth - 1));
2600 DPRINTF("Route=0x%08x\n", route);
2602 temp = XHCI_SCTX_0_ROUTE_SET(route) |
2603 XHCI_SCTX_0_CTX_NUM_SET(
2604 sc->sc_hw.devs[index].context_num + 1);
2606 switch (udev->speed) {
2608 temp |= XHCI_SCTX_0_SPEED_SET(2);
2609 if (udev->parent_hs_hub != NULL &&
2610 udev->parent_hs_hub->ddesc.bDeviceProtocol ==
2612 DPRINTF("Device inherits MTT\n");
2613 temp |= XHCI_SCTX_0_MTT_SET(1);
2616 case USB_SPEED_HIGH:
2617 temp |= XHCI_SCTX_0_SPEED_SET(3);
2618 if (sc->sc_hw.devs[index].nports != 0 &&
2619 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) {
2620 DPRINTF("HUB supports MTT\n");
2621 temp |= XHCI_SCTX_0_MTT_SET(1);
2624 case USB_SPEED_FULL:
2625 temp |= XHCI_SCTX_0_SPEED_SET(1);
2626 if (udev->parent_hs_hub != NULL &&
2627 udev->parent_hs_hub->ddesc.bDeviceProtocol ==
2629 DPRINTF("Device inherits MTT\n");
2630 temp |= XHCI_SCTX_0_MTT_SET(1);
2634 temp |= XHCI_SCTX_0_SPEED_SET(4);
2638 is_hub = sc->sc_hw.devs[index].nports != 0 &&
2639 (udev->speed == USB_SPEED_SUPER ||
2640 udev->speed == USB_SPEED_HIGH);
2643 temp |= XHCI_SCTX_0_HUB_SET(1);
2645 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx0, temp);
2647 temp = XHCI_SCTX_1_RH_PORT_SET(rh_port);
2650 temp |= XHCI_SCTX_1_NUM_PORTS_SET(
2651 sc->sc_hw.devs[index].nports);
2654 switch (udev->speed) {
2655 case USB_SPEED_SUPER:
2656 switch (sc->sc_hw.devs[index].state) {
2657 case XHCI_ST_ADDRESSED:
2658 case XHCI_ST_CONFIGURED:
2659 /* enable power save */
2660 temp |= XHCI_SCTX_1_MAX_EL_SET(sc->sc_exit_lat_max);
2663 /* disable power save */
2671 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx1, temp);
2673 temp = XHCI_SCTX_2_IRQ_TARGET_SET(0);
2676 temp |= XHCI_SCTX_2_TT_THINK_TIME_SET(
2677 sc->sc_hw.devs[index].tt);
2680 hubdev = udev->parent_hs_hub;
2682 /* check if we should activate the transaction translator */
2683 switch (udev->speed) {
2684 case USB_SPEED_FULL:
2686 if (hubdev != NULL) {
2687 temp |= XHCI_SCTX_2_TT_HUB_SID_SET(
2688 hubdev->controller_slot_id);
2689 temp |= XHCI_SCTX_2_TT_PORT_NUM_SET(
2697 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx2, temp);
2700 * These fields should be initialized to zero, according to
2701 * XHCI section 6.2.2 - slot context:
2703 temp = XHCI_SCTX_3_DEV_ADDR_SET(0) |
2704 XHCI_SCTX_3_SLOT_STATE_SET(0);
2706 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx3, temp);
2709 xhci_dump_device(sc, &pinp->ctx_slot);
2711 usb_pc_cpu_flush(pcinp);
2713 return (0); /* success */
2717 xhci_alloc_device_ext(struct usb_device *udev)
2719 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2720 struct usb_page_search buf_dev;
2721 struct usb_page_search buf_ep;
2722 struct xhci_trb *trb;
2723 struct usb_page_cache *pc;
2724 struct usb_page *pg;
2729 index = udev->controller_slot_id;
2731 pc = &sc->sc_hw.devs[index].device_pc;
2732 pg = &sc->sc_hw.devs[index].device_pg;
2734 /* need to initialize the page cache */
2735 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2737 if (usb_pc_alloc_mem(pc, pg, sc->sc_ctx_is_64_byte ?
2738 (2 * sizeof(struct xhci_dev_ctx)) :
2739 sizeof(struct xhci_dev_ctx), XHCI_PAGE_SIZE))
2742 usbd_get_page(pc, 0, &buf_dev);
2744 pc = &sc->sc_hw.devs[index].input_pc;
2745 pg = &sc->sc_hw.devs[index].input_pg;
2747 /* need to initialize the page cache */
2748 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2750 if (usb_pc_alloc_mem(pc, pg, sc->sc_ctx_is_64_byte ?
2751 (2 * sizeof(struct xhci_input_dev_ctx)) :
2752 sizeof(struct xhci_input_dev_ctx), XHCI_PAGE_SIZE)) {
2756 /* initialize all endpoint LINK TRBs */
2758 for (i = 0; i != XHCI_MAX_ENDPOINTS; i++) {
2760 pc = &sc->sc_hw.devs[index].endpoint_pc[i];
2761 pg = &sc->sc_hw.devs[index].endpoint_pg[i];
2763 /* need to initialize the page cache */
2764 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2766 if (usb_pc_alloc_mem(pc, pg,
2767 sizeof(struct xhci_dev_endpoint_trbs), XHCI_TRB_ALIGN)) {
2771 /* lookup endpoint TRB ring */
2772 usbd_get_page(pc, 0, &buf_ep);
2774 /* get TRB pointer */
2775 trb = buf_ep.buffer;
2776 trb += XHCI_MAX_TRANSFERS - 1;
2778 /* get TRB start address */
2779 addr = buf_ep.physaddr;
2781 /* create LINK TRB */
2782 trb->qwTrb0 = htole64(addr);
2783 trb->dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
2784 trb->dwTrb3 = htole32(XHCI_TRB_3_CYCLE_BIT |
2785 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
2787 usb_pc_cpu_flush(pc);
2790 xhci_set_slot_pointer(sc, index, buf_dev.physaddr);
2795 xhci_free_device_ext(udev);
2797 return (USB_ERR_NOMEM);
2801 xhci_free_device_ext(struct usb_device *udev)
2803 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2807 index = udev->controller_slot_id;
2808 xhci_set_slot_pointer(sc, index, 0);
2810 usb_pc_free_mem(&sc->sc_hw.devs[index].device_pc);
2811 usb_pc_free_mem(&sc->sc_hw.devs[index].input_pc);
2812 for (i = 0; i != XHCI_MAX_ENDPOINTS; i++)
2813 usb_pc_free_mem(&sc->sc_hw.devs[index].endpoint_pc[i]);
2816 static struct xhci_endpoint_ext *
2817 xhci_get_endpoint_ext(struct usb_device *udev, struct usb_endpoint_descriptor *edesc)
2819 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2820 struct xhci_endpoint_ext *pepext;
2821 struct usb_page_cache *pc;
2822 struct usb_page_search buf_ep;
2826 epno = edesc->bEndpointAddress;
2827 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL)
2830 epno = XHCI_EPNO2EPID(epno);
2832 index = udev->controller_slot_id;
2834 pc = &sc->sc_hw.devs[index].endpoint_pc[epno];
2836 usbd_get_page(pc, 0, &buf_ep);
2838 pepext = &sc->sc_hw.devs[index].endp[epno];
2839 pepext->page_cache = pc;
2840 pepext->trb = buf_ep.buffer;
2841 pepext->physaddr = buf_ep.physaddr;
2847 xhci_endpoint_doorbell(struct usb_xfer *xfer)
2849 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
2853 epno = xfer->endpointno;
2854 if (xfer->flags_int.control_xfr)
2857 epno = XHCI_EPNO2EPID(epno);
2858 index = xfer->xroot->udev->controller_slot_id;
2860 if (xfer->xroot->udev->flags.self_suspended == 0) {
2861 XWRITE4(sc, door, XHCI_DOORBELL(index),
2862 epno | XHCI_DB_SID_SET(xfer->stream_id));
2867 xhci_transfer_remove(struct usb_xfer *xfer, usb_error_t error)
2869 struct xhci_endpoint_ext *pepext;
2871 if (xfer->flags_int.bandwidth_reclaimed) {
2872 xfer->flags_int.bandwidth_reclaimed = 0;
2874 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2875 xfer->endpoint->edesc);
2877 pepext->trb_used[xfer->stream_id]--;
2879 pepext->xfer[xfer->qh_pos] = NULL;
2881 if (error && pepext->trb_running != 0) {
2882 pepext->trb_halted = 1;
2883 pepext->trb_running = 0;
2889 xhci_transfer_insert(struct usb_xfer *xfer)
2891 struct xhci_td *td_first;
2892 struct xhci_td *td_last;
2893 struct xhci_trb *trb_link;
2894 struct xhci_endpoint_ext *pepext;
2903 id = xfer->stream_id;
2905 /* check if already inserted */
2906 if (xfer->flags_int.bandwidth_reclaimed) {
2907 DPRINTFN(8, "Already in schedule\n");
2911 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2912 xfer->endpoint->edesc);
2914 td_first = xfer->td_transfer_first;
2915 td_last = xfer->td_transfer_last;
2916 addr = pepext->physaddr;
2918 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
2921 /* single buffered */
2925 /* multi buffered */
2926 trb_limit = (XHCI_MAX_TRANSFERS - 2);
2930 if (pepext->trb_used[id] >= trb_limit) {
2931 DPRINTFN(8, "Too many TDs queued.\n");
2932 return (USB_ERR_NOMEM);
2935 /* check if bMaxPacketSize changed */
2936 if (xfer->flags_int.control_xfr != 0 &&
2937 pepext->trb_ep_maxp != xfer->endpoint->edesc->wMaxPacketSize[0]) {
2939 DPRINTFN(8, "Reconfigure control endpoint\n");
2941 /* force driver to reconfigure endpoint */
2942 pepext->trb_halted = 1;
2943 pepext->trb_running = 0;
2946 /* check for stopped condition, after putting transfer on interrupt queue */
2947 if (pepext->trb_running == 0) {
2948 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
2950 DPRINTFN(8, "Not running\n");
2952 /* start configuration */
2953 (void)usb_proc_msignal(USB_BUS_CONTROL_XFER_PROC(&sc->sc_bus),
2954 &sc->sc_config_msg[0], &sc->sc_config_msg[1]);
2958 pepext->trb_used[id]++;
2960 /* get current TRB index */
2961 i = pepext->trb_index[id];
2963 /* get next TRB index */
2966 /* the last entry of the ring is a hardcoded link TRB */
2967 if (inext >= (XHCI_MAX_TRANSFERS - 1))
2970 /* store next TRB index, before stream ID offset is added */
2971 pepext->trb_index[id] = inext;
2973 /* offset for stream */
2974 i += id * XHCI_MAX_TRANSFERS;
2975 inext += id * XHCI_MAX_TRANSFERS;
2977 /* compute terminating return address */
2978 addr += (inext * sizeof(struct xhci_trb));
2980 /* compute link TRB pointer */
2981 trb_link = td_last->td_trb + td_last->ntrb;
2983 /* update next pointer of last link TRB */
2984 trb_link->qwTrb0 = htole64(addr);
2985 trb_link->dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
2986 trb_link->dwTrb3 = htole32(XHCI_TRB_3_IOC_BIT |
2987 XHCI_TRB_3_CYCLE_BIT |
2988 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
2991 xhci_dump_trb(&td_last->td_trb[td_last->ntrb]);
2993 usb_pc_cpu_flush(td_last->page_cache);
2995 /* write ahead chain end marker */
2997 pepext->trb[inext].qwTrb0 = 0;
2998 pepext->trb[inext].dwTrb2 = 0;
2999 pepext->trb[inext].dwTrb3 = 0;
3001 /* update next pointer of link TRB */
3003 pepext->trb[i].qwTrb0 = htole64((uint64_t)td_first->td_self);
3004 pepext->trb[i].dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
3007 xhci_dump_trb(&pepext->trb[i]);
3009 usb_pc_cpu_flush(pepext->page_cache);
3011 /* toggle cycle bit which activates the transfer chain */
3013 pepext->trb[i].dwTrb3 = htole32(XHCI_TRB_3_CYCLE_BIT |
3014 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
3016 usb_pc_cpu_flush(pepext->page_cache);
3018 DPRINTF("qh_pos = %u\n", i);
3020 pepext->xfer[i] = xfer;
3024 xfer->flags_int.bandwidth_reclaimed = 1;
3026 xhci_endpoint_doorbell(xfer);
3032 xhci_root_intr(struct xhci_softc *sc)
3036 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
3038 /* clear any old interrupt data */
3039 memset(sc->sc_hub_idata, 0, sizeof(sc->sc_hub_idata));
3041 for (i = 1; i <= sc->sc_noport; i++) {
3042 /* pick out CHANGE bits from the status register */
3043 if (XREAD4(sc, oper, XHCI_PORTSC(i)) & (
3044 XHCI_PS_CSC | XHCI_PS_PEC |
3045 XHCI_PS_OCC | XHCI_PS_WRC |
3046 XHCI_PS_PRC | XHCI_PS_PLC |
3048 sc->sc_hub_idata[i / 8] |= 1 << (i % 8);
3049 DPRINTF("port %d changed\n", i);
3052 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
3053 sizeof(sc->sc_hub_idata));
3056 /*------------------------------------------------------------------------*
3057 * xhci_device_done - XHCI done handler
3059 * NOTE: This function can be called two times in a row on
3060 * the same USB transfer. From close and from interrupt.
3061 *------------------------------------------------------------------------*/
3063 xhci_device_done(struct usb_xfer *xfer, usb_error_t error)
3065 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
3066 xfer, xfer->endpoint, error);
3068 /* remove transfer from HW queue */
3069 xhci_transfer_remove(xfer, error);
3071 /* dequeue transfer and start next transfer */
3072 usbd_transfer_done(xfer, error);
3075 /*------------------------------------------------------------------------*
3076 * XHCI data transfer support (generic type)
3077 *------------------------------------------------------------------------*/
3079 xhci_device_generic_open(struct usb_xfer *xfer)
3081 if (xfer->flags_int.isochronous_xfr) {
3082 switch (xfer->xroot->udev->speed) {
3083 case USB_SPEED_FULL:
3086 usb_hs_bandwidth_alloc(xfer);
3093 xhci_device_generic_close(struct usb_xfer *xfer)
3097 xhci_device_done(xfer, USB_ERR_CANCELLED);
3099 if (xfer->flags_int.isochronous_xfr) {
3100 switch (xfer->xroot->udev->speed) {
3101 case USB_SPEED_FULL:
3104 usb_hs_bandwidth_free(xfer);
3111 xhci_device_generic_multi_enter(struct usb_endpoint *ep,
3112 usb_stream_t stream_id, struct usb_xfer *enter_xfer)
3114 struct usb_xfer *xfer;
3116 /* check if there is a current transfer */
3117 xfer = ep->endpoint_q[stream_id].curr;
3122 * Check if the current transfer is started and then pickup
3123 * the next one, if any. Else wait for next start event due to
3124 * block on failure feature.
3126 if (!xfer->flags_int.bandwidth_reclaimed)
3129 xfer = TAILQ_FIRST(&ep->endpoint_q[stream_id].head);
3132 * In case of enter we have to consider that the
3133 * transfer is queued by the USB core after the enter
3142 /* try to multi buffer */
3143 xhci_transfer_insert(xfer);
3147 xhci_device_generic_enter(struct usb_xfer *xfer)
3151 /* set up TD's and QH */
3152 xhci_setup_generic_chain(xfer);
3154 xhci_device_generic_multi_enter(xfer->endpoint,
3155 xfer->stream_id, xfer);
3159 xhci_device_generic_start(struct usb_xfer *xfer)
3163 /* try to insert xfer on HW queue */
3164 xhci_transfer_insert(xfer);
3166 /* try to multi buffer */
3167 xhci_device_generic_multi_enter(xfer->endpoint,
3168 xfer->stream_id, NULL);
3170 /* add transfer last on interrupt queue */
3171 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
3173 /* start timeout, if any */
3174 if (xfer->timeout != 0)
3175 usbd_transfer_timeout_ms(xfer, &xhci_timeout, xfer->timeout);
3178 struct usb_pipe_methods xhci_device_generic_methods =
3180 .open = xhci_device_generic_open,
3181 .close = xhci_device_generic_close,
3182 .enter = xhci_device_generic_enter,
3183 .start = xhci_device_generic_start,
3186 /*------------------------------------------------------------------------*
3187 * xhci root HUB support
3188 *------------------------------------------------------------------------*
3189 * Simulate a hardware HUB by handling all the necessary requests.
3190 *------------------------------------------------------------------------*/
3192 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
3195 struct usb_device_descriptor xhci_devd =
3197 .bLength = sizeof(xhci_devd),
3198 .bDescriptorType = UDESC_DEVICE, /* type */
3199 HSETW(.bcdUSB, 0x0300), /* USB version */
3200 .bDeviceClass = UDCLASS_HUB, /* class */
3201 .bDeviceSubClass = UDSUBCLASS_HUB, /* subclass */
3202 .bDeviceProtocol = UDPROTO_SSHUB, /* protocol */
3203 .bMaxPacketSize = 9, /* max packet size */
3204 HSETW(.idVendor, 0x0000), /* vendor */
3205 HSETW(.idProduct, 0x0000), /* product */
3206 HSETW(.bcdDevice, 0x0100), /* device version */
3210 .bNumConfigurations = 1, /* # of configurations */
3214 struct xhci_bos_desc xhci_bosd = {
3216 .bLength = sizeof(xhci_bosd.bosd),
3217 .bDescriptorType = UDESC_BOS,
3218 HSETW(.wTotalLength, sizeof(xhci_bosd)),
3219 .bNumDeviceCaps = 3,
3222 .bLength = sizeof(xhci_bosd.usb2extd),
3223 .bDescriptorType = 1,
3224 .bDevCapabilityType = 2,
3225 .bmAttributes[0] = 2,
3228 .bLength = sizeof(xhci_bosd.usbdcd),
3229 .bDescriptorType = UDESC_DEVICE_CAPABILITY,
3230 .bDevCapabilityType = 3,
3231 .bmAttributes = 0, /* XXX */
3232 HSETW(.wSpeedsSupported, 0x000C),
3233 .bFunctionalitySupport = 8,
3234 .bU1DevExitLat = 255, /* dummy - not used */
3235 .wU2DevExitLat = { 0x00, 0x08 },
3238 .bLength = sizeof(xhci_bosd.cidd),
3239 .bDescriptorType = 1,
3240 .bDevCapabilityType = 4,
3242 .bContainerID = 0, /* XXX */
3247 struct xhci_config_desc xhci_confd = {
3249 .bLength = sizeof(xhci_confd.confd),
3250 .bDescriptorType = UDESC_CONFIG,
3251 .wTotalLength[0] = sizeof(xhci_confd),
3253 .bConfigurationValue = 1,
3254 .iConfiguration = 0,
3255 .bmAttributes = UC_SELF_POWERED,
3256 .bMaxPower = 0 /* max power */
3259 .bLength = sizeof(xhci_confd.ifcd),
3260 .bDescriptorType = UDESC_INTERFACE,
3262 .bInterfaceClass = UICLASS_HUB,
3263 .bInterfaceSubClass = UISUBCLASS_HUB,
3264 .bInterfaceProtocol = 0,
3267 .bLength = sizeof(xhci_confd.endpd),
3268 .bDescriptorType = UDESC_ENDPOINT,
3269 .bEndpointAddress = UE_DIR_IN | XHCI_INTR_ENDPT,
3270 .bmAttributes = UE_INTERRUPT,
3271 .wMaxPacketSize[0] = 2, /* max 15 ports */
3275 .bLength = sizeof(xhci_confd.endpcd),
3276 .bDescriptorType = UDESC_ENDPOINT_SS_COMP,
3283 struct usb_hub_ss_descriptor xhci_hubd = {
3284 .bLength = sizeof(xhci_hubd),
3285 .bDescriptorType = UDESC_SS_HUB,
3289 xhci_roothub_exec(struct usb_device *udev,
3290 struct usb_device_request *req, const void **pptr, uint16_t *plength)
3292 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3293 const char *str_ptr;
3304 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
3307 ptr = (const void *)&sc->sc_hub_desc;
3311 value = UGETW(req->wValue);
3312 index = UGETW(req->wIndex);
3314 DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
3315 "wValue=0x%04x wIndex=0x%04x\n",
3316 req->bmRequestType, req->bRequest,
3317 UGETW(req->wLength), value, index);
3319 #define C(x,y) ((x) | ((y) << 8))
3320 switch (C(req->bRequest, req->bmRequestType)) {
3321 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
3322 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
3323 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
3325 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
3326 * for the integrated root hub.
3329 case C(UR_GET_CONFIG, UT_READ_DEVICE):
3331 sc->sc_hub_desc.temp[0] = sc->sc_conf;
3333 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
3334 switch (value >> 8) {
3336 if ((value & 0xff) != 0) {
3337 err = USB_ERR_IOERROR;
3340 len = sizeof(xhci_devd);
3341 ptr = (const void *)&xhci_devd;
3345 if ((value & 0xff) != 0) {
3346 err = USB_ERR_IOERROR;
3349 len = sizeof(xhci_bosd);
3350 ptr = (const void *)&xhci_bosd;
3354 if ((value & 0xff) != 0) {
3355 err = USB_ERR_IOERROR;
3358 len = sizeof(xhci_confd);
3359 ptr = (const void *)&xhci_confd;
3363 switch (value & 0xff) {
3364 case 0: /* Language table */
3368 case 1: /* Vendor */
3369 str_ptr = sc->sc_vendor;
3372 case 2: /* Product */
3373 str_ptr = "XHCI root HUB";
3381 len = usb_make_str_desc(
3382 sc->sc_hub_desc.temp,
3383 sizeof(sc->sc_hub_desc.temp),
3388 err = USB_ERR_IOERROR;
3392 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3394 sc->sc_hub_desc.temp[0] = 0;
3396 case C(UR_GET_STATUS, UT_READ_DEVICE):
3398 USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
3400 case C(UR_GET_STATUS, UT_READ_INTERFACE):
3401 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3403 USETW(sc->sc_hub_desc.stat.wStatus, 0);
3405 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3406 if (value >= XHCI_MAX_DEVICES) {
3407 err = USB_ERR_IOERROR;
3411 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
3412 if (value != 0 && value != 1) {
3413 err = USB_ERR_IOERROR;
3416 sc->sc_conf = value;
3418 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
3420 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
3421 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
3422 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3423 err = USB_ERR_IOERROR;
3425 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
3427 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
3430 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3432 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3433 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE\n");
3436 (index > sc->sc_noport)) {
3437 err = USB_ERR_IOERROR;
3440 port = XHCI_PORTSC(index);
3442 v = XREAD4(sc, oper, port);
3443 i = XHCI_PS_PLS_GET(v);
3444 v &= ~XHCI_PS_CLEAR;
3447 case UHF_C_BH_PORT_RESET:
3448 XWRITE4(sc, oper, port, v | XHCI_PS_WRC);
3450 case UHF_C_PORT_CONFIG_ERROR:
3451 XWRITE4(sc, oper, port, v | XHCI_PS_CEC);
3453 case UHF_C_PORT_SUSPEND:
3454 case UHF_C_PORT_LINK_STATE:
3455 XWRITE4(sc, oper, port, v | XHCI_PS_PLC);
3457 case UHF_C_PORT_CONNECTION:
3458 XWRITE4(sc, oper, port, v | XHCI_PS_CSC);
3460 case UHF_C_PORT_ENABLE:
3461 XWRITE4(sc, oper, port, v | XHCI_PS_PEC);
3463 case UHF_C_PORT_OVER_CURRENT:
3464 XWRITE4(sc, oper, port, v | XHCI_PS_OCC);
3466 case UHF_C_PORT_RESET:
3467 XWRITE4(sc, oper, port, v | XHCI_PS_PRC);
3469 case UHF_PORT_ENABLE:
3470 XWRITE4(sc, oper, port, v | XHCI_PS_PED);
3472 case UHF_PORT_POWER:
3473 XWRITE4(sc, oper, port, v & ~XHCI_PS_PP);
3475 case UHF_PORT_INDICATOR:
3476 XWRITE4(sc, oper, port, v & ~XHCI_PS_PIC_SET(3));
3478 case UHF_PORT_SUSPEND:
3482 XWRITE4(sc, oper, port, v |
3483 XHCI_PS_PLS_SET(0xF) | XHCI_PS_LWS);
3486 /* wait 20ms for resume sequence to complete */
3487 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
3490 XWRITE4(sc, oper, port, v |
3491 XHCI_PS_PLS_SET(0) | XHCI_PS_LWS);
3494 err = USB_ERR_IOERROR;
3499 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3500 if ((value & 0xff) != 0) {
3501 err = USB_ERR_IOERROR;
3505 v = XREAD4(sc, capa, XHCI_HCSPARAMS0);
3507 sc->sc_hub_desc.hubd = xhci_hubd;
3509 sc->sc_hub_desc.hubd.bNbrPorts = sc->sc_noport;
3511 if (XHCI_HCS0_PPC(v))
3512 i = UHD_PWR_INDIVIDUAL;
3516 if (XHCI_HCS0_PIND(v))
3519 i |= UHD_OC_INDIVIDUAL;
3521 USETW(sc->sc_hub_desc.hubd.wHubCharacteristics, i);
3523 /* see XHCI section 5.4.9: */
3524 sc->sc_hub_desc.hubd.bPwrOn2PwrGood = 10;
3526 for (j = 1; j <= sc->sc_noport; j++) {
3528 v = XREAD4(sc, oper, XHCI_PORTSC(j));
3529 if (v & XHCI_PS_DR) {
3530 sc->sc_hub_desc.hubd.
3531 DeviceRemovable[j / 8] |= 1U << (j % 8);
3534 len = sc->sc_hub_desc.hubd.bLength;
3537 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3539 memset(sc->sc_hub_desc.temp, 0, 16);
3542 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3543 DPRINTFN(9, "UR_GET_STATUS i=%d\n", index);
3546 (index > sc->sc_noport)) {
3547 err = USB_ERR_IOERROR;
3551 v = XREAD4(sc, oper, XHCI_PORTSC(index));
3553 DPRINTFN(9, "port status=0x%08x\n", v);
3555 i = UPS_PORT_LINK_STATE_SET(XHCI_PS_PLS_GET(v));
3557 switch (XHCI_PS_SPEED_GET(v)) {
3559 i |= UPS_HIGH_SPEED;
3568 i |= UPS_OTHER_SPEED;
3572 if (v & XHCI_PS_CCS)
3573 i |= UPS_CURRENT_CONNECT_STATUS;
3574 if (v & XHCI_PS_PED)
3575 i |= UPS_PORT_ENABLED;
3576 if (v & XHCI_PS_OCA)
3577 i |= UPS_OVERCURRENT_INDICATOR;
3580 if (v & XHCI_PS_PP) {
3582 * The USB 3.0 RH is using the
3583 * USB 2.0's power bit
3585 i |= UPS_PORT_POWER;
3587 USETW(sc->sc_hub_desc.ps.wPortStatus, i);
3590 if (v & XHCI_PS_CSC)
3591 i |= UPS_C_CONNECT_STATUS;
3592 if (v & XHCI_PS_PEC)
3593 i |= UPS_C_PORT_ENABLED;
3594 if (v & XHCI_PS_OCC)
3595 i |= UPS_C_OVERCURRENT_INDICATOR;
3596 if (v & XHCI_PS_WRC)
3597 i |= UPS_C_BH_PORT_RESET;
3598 if (v & XHCI_PS_PRC)
3599 i |= UPS_C_PORT_RESET;
3600 if (v & XHCI_PS_PLC)
3601 i |= UPS_C_PORT_LINK_STATE;
3602 if (v & XHCI_PS_CEC)
3603 i |= UPS_C_PORT_CONFIG_ERROR;
3605 USETW(sc->sc_hub_desc.ps.wPortChange, i);
3606 len = sizeof(sc->sc_hub_desc.ps);
3609 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3610 err = USB_ERR_IOERROR;
3613 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3616 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3622 (index > sc->sc_noport)) {
3623 err = USB_ERR_IOERROR;
3627 port = XHCI_PORTSC(index);
3628 v = XREAD4(sc, oper, port) & ~XHCI_PS_CLEAR;
3631 case UHF_PORT_U1_TIMEOUT:
3632 if (XHCI_PS_SPEED_GET(v) != 4) {
3633 err = USB_ERR_IOERROR;
3636 port = XHCI_PORTPMSC(index);
3637 v = XREAD4(sc, oper, port);
3638 v &= ~XHCI_PM3_U1TO_SET(0xFF);
3639 v |= XHCI_PM3_U1TO_SET(i);
3640 XWRITE4(sc, oper, port, v);
3642 case UHF_PORT_U2_TIMEOUT:
3643 if (XHCI_PS_SPEED_GET(v) != 4) {
3644 err = USB_ERR_IOERROR;
3647 port = XHCI_PORTPMSC(index);
3648 v = XREAD4(sc, oper, port);
3649 v &= ~XHCI_PM3_U2TO_SET(0xFF);
3650 v |= XHCI_PM3_U2TO_SET(i);
3651 XWRITE4(sc, oper, port, v);
3653 case UHF_BH_PORT_RESET:
3654 XWRITE4(sc, oper, port, v | XHCI_PS_WPR);
3656 case UHF_PORT_LINK_STATE:
3657 XWRITE4(sc, oper, port, v |
3658 XHCI_PS_PLS_SET(i) | XHCI_PS_LWS);
3659 /* 4ms settle time */
3660 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
3662 case UHF_PORT_ENABLE:
3663 DPRINTFN(3, "set port enable %d\n", index);
3665 case UHF_PORT_SUSPEND:
3666 DPRINTFN(6, "suspend port %u (LPM=%u)\n", index, i);
3667 j = XHCI_PS_SPEED_GET(v);
3668 if ((j < 1) || (j > 3)) {
3669 /* non-supported speed */
3670 err = USB_ERR_IOERROR;
3673 XWRITE4(sc, oper, port, v |
3674 XHCI_PS_PLS_SET(i ? 2 /* LPM */ : 3) | XHCI_PS_LWS);
3676 case UHF_PORT_RESET:
3677 DPRINTFN(6, "reset port %d\n", index);
3678 XWRITE4(sc, oper, port, v | XHCI_PS_PR);
3680 case UHF_PORT_POWER:
3681 DPRINTFN(3, "set port power %d\n", index);
3682 XWRITE4(sc, oper, port, v | XHCI_PS_PP);
3685 DPRINTFN(3, "set port test %d\n", index);
3687 case UHF_PORT_INDICATOR:
3688 DPRINTFN(3, "set port indicator %d\n", index);
3690 v &= ~XHCI_PS_PIC_SET(3);
3691 v |= XHCI_PS_PIC_SET(1);
3693 XWRITE4(sc, oper, port, v);
3696 err = USB_ERR_IOERROR;
3701 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
3702 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
3703 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
3704 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
3707 err = USB_ERR_IOERROR;
3717 xhci_xfer_setup(struct usb_setup_params *parm)
3719 struct usb_page_search page_info;
3720 struct usb_page_cache *pc;
3721 struct xhci_softc *sc;
3722 struct usb_xfer *xfer;
3727 sc = XHCI_BUS2SC(parm->udev->bus);
3728 xfer = parm->curr_xfer;
3731 * The proof for the "ntd" formula is illustrated like this:
3733 * +------------------------------------+
3737 * | | xxx | x | frm 0 |
3739 * | | xxx | xx | frm 1 |
3742 * +------------------------------------+
3744 * "xxx" means a completely full USB transfer descriptor
3746 * "x" and "xx" means a short USB packet
3748 * For the remainder of an USB transfer modulo
3749 * "max_data_length" we need two USB transfer descriptors.
3750 * One to transfer the remaining data and one to finalise with
3751 * a zero length packet in case the "force_short_xfer" flag is
3752 * set. We only need two USB transfer descriptors in the case
3753 * where the transfer length of the first one is a factor of
3754 * "max_frame_size". The rest of the needed USB transfer
3755 * descriptors is given by the buffer size divided by the
3756 * maximum data payload.
3758 parm->hc_max_packet_size = 0x400;
3759 parm->hc_max_packet_count = 16 * 3;
3760 parm->hc_max_frame_size = XHCI_TD_PAYLOAD_MAX;
3762 xfer->flags_int.bdma_enable = 1;
3764 usbd_transfer_setup_sub(parm);
3766 if (xfer->flags_int.isochronous_xfr) {
3767 ntd = ((1 * xfer->nframes)
3768 + (xfer->max_data_length / xfer->max_hc_frame_size));
3769 } else if (xfer->flags_int.control_xfr) {
3770 ntd = ((2 * xfer->nframes) + 1 /* STATUS */
3771 + (xfer->max_data_length / xfer->max_hc_frame_size));
3773 ntd = ((2 * xfer->nframes)
3774 + (xfer->max_data_length / xfer->max_hc_frame_size));
3783 * Allocate queue heads and transfer descriptors
3787 if (usbd_transfer_setup_sub_malloc(
3788 parm, &pc, sizeof(struct xhci_td),
3789 XHCI_TD_ALIGN, ntd)) {
3790 parm->err = USB_ERR_NOMEM;
3794 for (n = 0; n != ntd; n++) {
3797 usbd_get_page(pc + n, 0, &page_info);
3799 td = page_info.buffer;
3802 td->td_self = page_info.physaddr;
3803 td->obj_next = last_obj;
3804 td->page_cache = pc + n;
3808 usb_pc_cpu_flush(pc + n);
3811 xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
3813 if (!xfer->flags_int.curr_dma_set) {
3814 xfer->flags_int.curr_dma_set = 1;
3820 xhci_configure_reset_endpoint(struct usb_xfer *xfer)
3822 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
3823 struct usb_page_search buf_inp;
3824 struct usb_device *udev;
3825 struct xhci_endpoint_ext *pepext;
3826 struct usb_endpoint_descriptor *edesc;
3827 struct usb_page_cache *pcinp;
3829 usb_stream_t stream_id;
3833 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
3834 xfer->endpoint->edesc);
3836 udev = xfer->xroot->udev;
3837 index = udev->controller_slot_id;
3839 pcinp = &sc->sc_hw.devs[index].input_pc;
3841 usbd_get_page(pcinp, 0, &buf_inp);
3843 edesc = xfer->endpoint->edesc;
3845 epno = edesc->bEndpointAddress;
3846 stream_id = xfer->stream_id;
3848 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL)
3851 epno = XHCI_EPNO2EPID(epno);
3854 return (USB_ERR_NO_PIPE); /* invalid */
3858 /* configure endpoint */
3860 err = xhci_configure_endpoint_by_xfer(xfer);
3863 XHCI_CMD_UNLOCK(sc);
3868 * Get the endpoint into the stopped state according to the
3869 * endpoint context state diagram in the XHCI specification:
3872 err = xhci_cmd_stop_ep(sc, 0, epno, index);
3875 DPRINTF("Could not stop endpoint %u\n", epno);
3877 err = xhci_cmd_reset_ep(sc, 0, epno, index);
3880 DPRINTF("Could not reset endpoint %u\n", epno);
3882 err = xhci_cmd_set_tr_dequeue_ptr(sc,
3883 (pepext->physaddr + (stream_id * sizeof(struct xhci_trb) *
3884 XHCI_MAX_TRANSFERS)) | XHCI_EPCTX_2_DCS_SET(1),
3885 stream_id, epno, index);
3888 DPRINTF("Could not set dequeue ptr for endpoint %u\n", epno);
3891 * Get the endpoint into the running state according to the
3892 * endpoint context state diagram in the XHCI specification:
3895 xhci_configure_mask(udev, (1U << epno) | 1U, 0);
3898 err = xhci_cmd_configure_ep(sc, buf_inp.physaddr, 0, index);
3900 err = xhci_cmd_evaluate_ctx(sc, buf_inp.physaddr, index);
3903 DPRINTF("Could not configure endpoint %u\n", epno);
3905 XHCI_CMD_UNLOCK(sc);
3911 xhci_xfer_unsetup(struct usb_xfer *xfer)
3917 xhci_start_dma_delay(struct usb_xfer *xfer)
3919 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
3921 /* put transfer on interrupt queue (again) */
3922 usbd_transfer_enqueue(&sc->sc_bus.intr_q, xfer);
3924 (void)usb_proc_msignal(USB_BUS_CONTROL_XFER_PROC(&sc->sc_bus),
3925 &sc->sc_config_msg[0], &sc->sc_config_msg[1]);
3929 xhci_configure_msg(struct usb_proc_msg *pm)
3931 struct xhci_softc *sc;
3932 struct xhci_endpoint_ext *pepext;
3933 struct usb_xfer *xfer;
3935 sc = XHCI_BUS2SC(((struct usb_bus_msg *)pm)->bus);
3938 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3940 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
3941 xfer->endpoint->edesc);
3943 if ((pepext->trb_halted != 0) ||
3944 (pepext->trb_running == 0)) {
3948 /* clear halted and running */
3949 pepext->trb_halted = 0;
3950 pepext->trb_running = 0;
3952 /* nuke remaining buffered transfers */
3954 for (i = 0; i != (XHCI_MAX_TRANSFERS *
3955 XHCI_MAX_STREAMS); i++) {
3957 * NOTE: We need to use the timeout
3958 * error code here else existing
3959 * isochronous clients can get
3962 if (pepext->xfer[i] != NULL) {
3963 xhci_device_done(pepext->xfer[i],
3969 * NOTE: The USB transfer cannot vanish in
3973 USB_BUS_UNLOCK(&sc->sc_bus);
3975 xhci_configure_reset_endpoint(xfer);
3977 USB_BUS_LOCK(&sc->sc_bus);
3979 /* check if halted is still cleared */
3980 if (pepext->trb_halted == 0) {
3981 pepext->trb_running = 1;
3982 memset(pepext->trb_index, 0,
3983 sizeof(pepext->trb_index));
3988 if (xfer->flags_int.did_dma_delay) {
3990 /* remove transfer from interrupt queue (again) */
3991 usbd_transfer_dequeue(xfer);
3993 /* we are finally done */
3994 usb_dma_delay_done_cb(xfer);
3996 /* queue changed - restart */
4001 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
4003 /* try to insert xfer on HW queue */
4004 xhci_transfer_insert(xfer);
4006 /* try to multi buffer */
4007 xhci_device_generic_multi_enter(xfer->endpoint,
4008 xfer->stream_id, NULL);
4013 xhci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
4014 struct usb_endpoint *ep)
4016 struct xhci_endpoint_ext *pepext;
4018 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d\n",
4019 ep, udev->address, edesc->bEndpointAddress, udev->flags.usb_mode);
4021 if (udev->parent_hub == NULL) {
4022 /* root HUB has special endpoint handling */
4026 ep->methods = &xhci_device_generic_methods;
4028 pepext = xhci_get_endpoint_ext(udev, edesc);
4030 USB_BUS_LOCK(udev->bus);
4031 pepext->trb_halted = 1;
4032 pepext->trb_running = 0;
4033 USB_BUS_UNLOCK(udev->bus);
4037 xhci_ep_uninit(struct usb_device *udev, struct usb_endpoint *ep)
4043 xhci_ep_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
4045 struct xhci_endpoint_ext *pepext;
4049 if (udev->flags.usb_mode != USB_MODE_HOST) {
4053 if (udev->parent_hub == NULL) {
4054 /* root HUB has special endpoint handling */
4058 pepext = xhci_get_endpoint_ext(udev, ep->edesc);
4060 USB_BUS_LOCK(udev->bus);
4061 pepext->trb_halted = 1;
4062 pepext->trb_running = 0;
4063 USB_BUS_UNLOCK(udev->bus);
4067 xhci_device_init(struct usb_device *udev)
4069 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
4073 /* no init for root HUB */
4074 if (udev->parent_hub == NULL)
4079 /* set invalid default */
4081 udev->controller_slot_id = sc->sc_noslot + 1;
4083 /* try to get a new slot ID from the XHCI */
4085 err = xhci_cmd_enable_slot(sc, &temp);
4088 XHCI_CMD_UNLOCK(sc);
4092 if (temp > sc->sc_noslot) {
4093 XHCI_CMD_UNLOCK(sc);
4094 return (USB_ERR_BAD_ADDRESS);
4097 if (sc->sc_hw.devs[temp].state != XHCI_ST_DISABLED) {
4098 DPRINTF("slot %u already allocated.\n", temp);
4099 XHCI_CMD_UNLOCK(sc);
4100 return (USB_ERR_BAD_ADDRESS);
4103 /* store slot ID for later reference */
4105 udev->controller_slot_id = temp;
4107 /* reset data structure */
4109 memset(&sc->sc_hw.devs[temp], 0, sizeof(sc->sc_hw.devs[0]));
4111 /* set mark slot allocated */
4113 sc->sc_hw.devs[temp].state = XHCI_ST_ENABLED;
4115 err = xhci_alloc_device_ext(udev);
4117 XHCI_CMD_UNLOCK(sc);
4119 /* get device into default state */
4122 err = xhci_set_address(udev, NULL, 0);
4128 xhci_device_uninit(struct usb_device *udev)
4130 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
4133 /* no init for root HUB */
4134 if (udev->parent_hub == NULL)
4139 index = udev->controller_slot_id;
4141 if (index <= sc->sc_noslot) {
4142 xhci_cmd_disable_slot(sc, index);
4143 sc->sc_hw.devs[index].state = XHCI_ST_DISABLED;
4145 /* free device extension */
4146 xhci_free_device_ext(udev);
4149 XHCI_CMD_UNLOCK(sc);
4153 xhci_get_dma_delay(struct usb_device *udev, uint32_t *pus)
4156 * Wait until the hardware has finished any possible use of
4157 * the transfer descriptor(s)
4159 *pus = 2048; /* microseconds */
4163 xhci_device_resume(struct usb_device *udev)
4165 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
4172 /* check for root HUB */
4173 if (udev->parent_hub == NULL)
4176 index = udev->controller_slot_id;
4180 /* blindly resume all endpoints */
4182 USB_BUS_LOCK(udev->bus);
4184 for (n = 1; n != XHCI_MAX_ENDPOINTS; n++) {
4185 for (p = 0; p != XHCI_MAX_STREAMS; p++) {
4186 XWRITE4(sc, door, XHCI_DOORBELL(index),
4187 n | XHCI_DB_SID_SET(p));
4191 USB_BUS_UNLOCK(udev->bus);
4193 XHCI_CMD_UNLOCK(sc);
4197 xhci_device_suspend(struct usb_device *udev)
4199 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
4206 /* check for root HUB */
4207 if (udev->parent_hub == NULL)
4210 index = udev->controller_slot_id;
4214 /* blindly suspend all endpoints */
4216 for (n = 1; n != XHCI_MAX_ENDPOINTS; n++) {
4217 err = xhci_cmd_stop_ep(sc, 1, n, index);
4219 DPRINTF("Failed to suspend endpoint "
4220 "%u on slot %u (ignored).\n", n, index);
4224 XHCI_CMD_UNLOCK(sc);
4228 xhci_set_hw_power(struct usb_bus *bus)
4234 xhci_device_state_change(struct usb_device *udev)
4236 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
4237 struct usb_page_search buf_inp;
4241 /* check for root HUB */
4242 if (udev->parent_hub == NULL)
4245 index = udev->controller_slot_id;
4249 if (usb_get_device_state(udev) == USB_STATE_CONFIGURED) {
4250 err = uhub_query_info(udev, &sc->sc_hw.devs[index].nports,
4251 &sc->sc_hw.devs[index].tt);
4253 sc->sc_hw.devs[index].nports = 0;
4258 switch (usb_get_device_state(udev)) {
4259 case USB_STATE_POWERED:
4260 if (sc->sc_hw.devs[index].state == XHCI_ST_DEFAULT)
4263 /* set default state */
4264 sc->sc_hw.devs[index].state = XHCI_ST_DEFAULT;
4266 /* reset number of contexts */
4267 sc->sc_hw.devs[index].context_num = 0;
4269 err = xhci_cmd_reset_dev(sc, index);
4272 DPRINTF("Device reset failed "
4273 "for slot %u.\n", index);
4277 case USB_STATE_ADDRESSED:
4278 if (sc->sc_hw.devs[index].state == XHCI_ST_ADDRESSED)
4281 sc->sc_hw.devs[index].state = XHCI_ST_ADDRESSED;
4283 /* set configure mask to slot only */
4284 xhci_configure_mask(udev, 1, 0);
4286 /* deconfigure all endpoints, except EP0 */
4287 err = xhci_cmd_configure_ep(sc, 0, 1, index);
4290 DPRINTF("Failed to deconfigure "
4291 "slot %u.\n", index);
4295 case USB_STATE_CONFIGURED:
4296 if (sc->sc_hw.devs[index].state == XHCI_ST_CONFIGURED)
4299 /* set configured state */
4300 sc->sc_hw.devs[index].state = XHCI_ST_CONFIGURED;
4302 /* reset number of contexts */
4303 sc->sc_hw.devs[index].context_num = 0;
4305 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
4307 xhci_configure_mask(udev, 3, 0);
4309 err = xhci_configure_device(udev);
4311 DPRINTF("Could not configure device "
4312 "at slot %u.\n", index);
4315 err = xhci_cmd_evaluate_ctx(sc, buf_inp.physaddr, index);
4317 DPRINTF("Could not evaluate device "
4318 "context at slot %u.\n", index);
4325 XHCI_CMD_UNLOCK(sc);
4329 xhci_set_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep,
4333 case USB_EP_MODE_DEFAULT:
4335 case USB_EP_MODE_STREAMS:
4336 if (xhcistreams == 0 ||
4337 (ep->edesc->bmAttributes & UE_XFERTYPE) != UE_BULK ||
4338 udev->speed != USB_SPEED_SUPER)
4339 return (USB_ERR_INVAL);
4342 return (USB_ERR_INVAL);
4346 struct usb_bus_methods xhci_bus_methods = {
4347 .endpoint_init = xhci_ep_init,
4348 .endpoint_uninit = xhci_ep_uninit,
4349 .xfer_setup = xhci_xfer_setup,
4350 .xfer_unsetup = xhci_xfer_unsetup,
4351 .get_dma_delay = xhci_get_dma_delay,
4352 .device_init = xhci_device_init,
4353 .device_uninit = xhci_device_uninit,
4354 .device_resume = xhci_device_resume,
4355 .device_suspend = xhci_device_suspend,
4356 .set_hw_power = xhci_set_hw_power,
4357 .roothub_exec = xhci_roothub_exec,
4358 .xfer_poll = xhci_do_poll,
4359 .start_dma_delay = xhci_start_dma_delay,
4360 .set_address = xhci_set_address,
4361 .clear_stall = xhci_ep_clear_stall,
4362 .device_state_change = xhci_device_state_change,
4363 .set_hw_power_sleep = xhci_set_hw_power_sleep,
4364 .set_endpoint_mode = xhci_set_endpoint_mode,