2 * Copyright (c) 2010 Hans Petter Selasky. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * USB eXtensible Host Controller Interface, a.k.a. USB 3.0 controller.
29 * The XHCI 1.0 spec can be found at
30 * http://www.intel.com/technology/usb/download/xHCI_Specification_for_USB.pdf
31 * and the USB 3.0 spec at
32 * http://www.usb.org/developers/docs/usb_30_spec_060910.zip
36 * A few words about the design implementation: This driver emulates
37 * the concept about TDs which is found in EHCI specification. This
38 * way we avoid too much diveration among USB drivers.
41 #include <sys/stdint.h>
42 #include <sys/param.h>
43 #include <sys/queue.h>
44 #include <sys/types.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
48 #include <sys/module.h>
50 #include <sys/condvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/unistd.h>
53 #include <sys/callout.h>
54 #include <sys/malloc.h>
57 #include <bus/u4b/usb.h>
58 #include <bus/u4b/usbdi.h>
60 #define USB_DEBUG_VAR xhcidebug
62 #include <bus/u4b/usb_core.h>
63 #include <bus/u4b/usb_debug.h>
64 #include <bus/u4b/usb_busdma.h>
65 #include <bus/u4b/usb_process.h>
66 #include <bus/u4b/usb_transfer.h>
67 #include <bus/u4b/usb_device.h>
68 #include <bus/u4b/usb_hub.h>
69 #include <bus/u4b/usb_util.h>
71 #include <bus/u4b/usb_controller.h>
72 #include <bus/u4b/usb_bus.h>
73 #include <bus/u4b/controller/xhci.h>
74 #include <bus/u4b/controller/xhcireg.h>
76 #define XHCI_BUS2SC(bus) \
77 ((struct xhci_softc *)(((uint8_t *)(bus)) - \
78 ((uint8_t *)&(((struct xhci_softc *)0)->sc_bus))))
81 static int xhcidebug = 0;
83 static SYSCTL_NODE(_hw_usb, OID_AUTO, xhci, CTLFLAG_RW, 0, "USB XHCI");
84 SYSCTL_INT(_hw_usb_xhci, OID_AUTO, debug, CTLFLAG_RW,
85 &xhcidebug, 0, "Debug level");
87 TUNABLE_INT("hw.usb.xhci.debug", &xhcidebug);
91 #define XHCI_INTR_ENDPT 1
93 struct xhci_std_temp {
94 struct xhci_softc *sc;
95 struct usb_page_cache *pc;
97 struct xhci_td *td_next;
100 uint32_t max_packet_size;
114 static void xhci_do_poll(struct usb_bus *);
115 static void xhci_device_done(struct usb_xfer *, usb_error_t);
116 static void xhci_root_intr(struct xhci_softc *);
117 static void xhci_free_device_ext(struct usb_device *);
118 static struct xhci_endpoint_ext *xhci_get_endpoint_ext(struct usb_device *,
119 struct usb_endpoint_descriptor *);
120 static usb_proc_callback_t xhci_configure_msg;
121 static usb_error_t xhci_configure_device(struct usb_device *);
122 static usb_error_t xhci_configure_endpoint(struct usb_device *,
123 struct usb_endpoint_descriptor *, uint64_t, uint16_t,
124 uint8_t, uint8_t, uint8_t, uint16_t, uint16_t);
125 static usb_error_t xhci_configure_mask(struct usb_device *,
127 static usb_error_t xhci_cmd_evaluate_ctx(struct xhci_softc *,
129 static void xhci_endpoint_doorbell(struct usb_xfer *);
130 static void xhci_ctx_set_le32(struct xhci_softc *sc, volatile uint32_t *ptr, uint32_t val);
131 static uint32_t xhci_ctx_get_le32(struct xhci_softc *sc, volatile uint32_t *ptr);
132 static void xhci_ctx_set_le64(struct xhci_softc *sc, volatile uint64_t *ptr, uint64_t val);
134 static uint64_t xhci_ctx_get_le64(struct xhci_softc *sc, volatile uint64_t *ptr);
137 extern struct usb_bus_methods xhci_bus_methods;
141 xhci_dump_trb(struct xhci_trb *trb)
143 DPRINTFN(5, "trb = %p\n", trb);
144 DPRINTFN(5, "qwTrb0 = 0x%016llx\n", (long long)le64toh(trb->qwTrb0));
145 DPRINTFN(5, "dwTrb2 = 0x%08x\n", le32toh(trb->dwTrb2));
146 DPRINTFN(5, "dwTrb3 = 0x%08x\n", le32toh(trb->dwTrb3));
150 xhci_dump_endpoint(struct xhci_softc *sc, struct xhci_endp_ctx *pep)
152 DPRINTFN(5, "pep = %p\n", pep);
153 DPRINTFN(5, "dwEpCtx0=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx0));
154 DPRINTFN(5, "dwEpCtx1=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx1));
155 DPRINTFN(5, "qwEpCtx2=0x%016llx\n", (long long)xhci_ctx_get_le64(sc, &pep->qwEpCtx2));
156 DPRINTFN(5, "dwEpCtx4=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx4));
157 DPRINTFN(5, "dwEpCtx5=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx5));
158 DPRINTFN(5, "dwEpCtx6=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx6));
159 DPRINTFN(5, "dwEpCtx7=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx7));
163 xhci_dump_device(struct xhci_softc *sc, struct xhci_slot_ctx *psl)
165 DPRINTFN(5, "psl = %p\n", psl);
166 DPRINTFN(5, "dwSctx0=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx0));
167 DPRINTFN(5, "dwSctx1=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx1));
168 DPRINTFN(5, "dwSctx2=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx2));
169 DPRINTFN(5, "dwSctx3=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx3));
174 xhci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
176 struct xhci_softc *sc = XHCI_BUS2SC(bus);
179 cb(bus, &sc->sc_hw.root_pc, &sc->sc_hw.root_pg,
180 sizeof(struct xhci_hw_root), XHCI_PAGE_SIZE);
182 cb(bus, &sc->sc_hw.ctx_pc, &sc->sc_hw.ctx_pg,
183 sizeof(struct xhci_dev_ctx_addr), XHCI_PAGE_SIZE);
185 for (i = 0; i != XHCI_MAX_SCRATCHPADS; i++) {
186 cb(bus, &sc->sc_hw.scratch_pc[i], &sc->sc_hw.scratch_pg[i],
187 XHCI_PAGE_SIZE, XHCI_PAGE_SIZE);
192 xhci_ctx_set_le32(struct xhci_softc *sc, volatile uint32_t *ptr, uint32_t val)
194 if (sc->sc_ctx_is_64_byte) {
196 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
197 /* all contexts are initially 32-bytes */
198 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
199 ptr = (volatile uint32_t *)(((volatile uint8_t *)ptr) + offset);
205 xhci_ctx_get_le32(struct xhci_softc *sc, volatile uint32_t *ptr)
207 if (sc->sc_ctx_is_64_byte) {
209 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
210 /* all contexts are initially 32-bytes */
211 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
212 ptr = (volatile uint32_t *)(((volatile uint8_t *)ptr) + offset);
214 return (le32toh(*ptr));
218 xhci_ctx_set_le64(struct xhci_softc *sc, volatile uint64_t *ptr, uint64_t val)
220 if (sc->sc_ctx_is_64_byte) {
222 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
223 /* all contexts are initially 32-bytes */
224 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
225 ptr = (volatile uint64_t *)(((volatile uint8_t *)ptr) + offset);
232 xhci_ctx_get_le64(struct xhci_softc *sc, volatile uint64_t *ptr)
234 if (sc->sc_ctx_is_64_byte) {
236 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
237 /* all contexts are initially 32-bytes */
238 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
239 ptr = (volatile uint64_t *)(((volatile uint8_t *)ptr) + offset);
241 return (le64toh(*ptr));
246 xhci_start_controller(struct xhci_softc *sc)
248 struct usb_page_search buf_res;
249 struct xhci_hw_root *phwr;
250 struct xhci_dev_ctx_addr *pdctxa;
258 sc->sc_oper_off = XREAD1(sc, capa, XHCI_CAPLENGTH);
259 sc->sc_runt_off = XREAD4(sc, capa, XHCI_RTSOFF) & ~0x1F;
260 sc->sc_door_off = XREAD4(sc, capa, XHCI_DBOFF) & ~0x3;
262 DPRINTF("CAPLENGTH=0x%x\n", sc->sc_oper_off);
263 DPRINTF("RUNTIMEOFFSET=0x%x\n", sc->sc_runt_off);
264 DPRINTF("DOOROFFSET=0x%x\n", sc->sc_door_off);
266 sc->sc_event_ccs = 1;
267 sc->sc_event_idx = 0;
268 sc->sc_command_ccs = 1;
269 sc->sc_command_idx = 0;
271 DPRINTF("xHCI version = 0x%04x\n", XREAD2(sc, capa, XHCI_HCIVERSION));
273 temp = XREAD4(sc, capa, XHCI_HCSPARAMS0);
275 DPRINTF("HCS0 = 0x%08x\n", temp);
277 if (XHCI_HCS0_CSZ(temp)) {
278 sc->sc_ctx_is_64_byte = 1;
279 device_printf(sc->sc_bus.parent, "64 byte context size.\n");
281 sc->sc_ctx_is_64_byte = 0;
282 device_printf(sc->sc_bus.parent, "32 byte context size.\n");
285 /* Reset controller */
286 XWRITE4(sc, oper, XHCI_USBCMD, XHCI_CMD_HCRST);
288 for (i = 0; i != 100; i++) {
289 usb_pause_mtx(NULL, hz / 100);
290 temp = XREAD4(sc, oper, XHCI_USBCMD) &
291 (XHCI_CMD_HCRST | XHCI_STS_CNR);
297 device_printf(sc->sc_bus.parent, "Controller "
299 return (USB_ERR_IOERROR);
302 if (!(XREAD4(sc, oper, XHCI_PAGESIZE) & XHCI_PAGESIZE_4K)) {
303 device_printf(sc->sc_bus.parent, "Controller does "
304 "not support 4K page size.\n");
305 return (USB_ERR_IOERROR);
308 temp = XREAD4(sc, capa, XHCI_HCSPARAMS1);
310 i = XHCI_HCS1_N_PORTS(temp);
313 device_printf(sc->sc_bus.parent, "Invalid number "
314 "of ports: %u\n", i);
315 return (USB_ERR_IOERROR);
319 sc->sc_noslot = XHCI_HCS1_DEVSLOT_MAX(temp);
321 if (sc->sc_noslot > XHCI_MAX_DEVICES)
322 sc->sc_noslot = XHCI_MAX_DEVICES;
324 /* setup number of device slots */
326 DPRINTF("CONFIG=0x%08x -> 0x%08x\n",
327 XREAD4(sc, oper, XHCI_CONFIG), sc->sc_noslot);
329 XWRITE4(sc, oper, XHCI_CONFIG, sc->sc_noslot);
331 DPRINTF("Max slots: %u\n", sc->sc_noslot);
333 temp = XREAD4(sc, capa, XHCI_HCSPARAMS2);
335 sc->sc_noscratch = XHCI_HCS2_SPB_MAX(temp);
337 if (sc->sc_noscratch > XHCI_MAX_SCRATCHPADS) {
338 device_printf(sc->sc_bus.parent, "XHCI request "
339 "too many scratchpads\n");
340 return (USB_ERR_NOMEM);
343 DPRINTF("Max scratch: %u\n", sc->sc_noscratch);
345 temp = XREAD4(sc, capa, XHCI_HCSPARAMS3);
347 sc->sc_exit_lat_max = XHCI_HCS3_U1_DEL(temp) +
348 XHCI_HCS3_U2_DEL(temp) + 250 /* us */;
350 temp = XREAD4(sc, oper, XHCI_USBSTS);
352 /* clear interrupts */
353 XWRITE4(sc, oper, XHCI_USBSTS, temp);
354 /* disable all device notifications */
355 XWRITE4(sc, oper, XHCI_DNCTRL, 0);
357 /* setup device context base address */
358 usbd_get_page(&sc->sc_hw.ctx_pc, 0, &buf_res);
359 pdctxa = buf_res.buffer;
360 memset(pdctxa, 0, sizeof(*pdctxa));
362 addr = buf_res.physaddr;
363 addr += (uintptr_t)&((struct xhci_dev_ctx_addr *)0)->qwSpBufPtr[0];
365 /* slot 0 points to the table of scratchpad pointers */
366 pdctxa->qwBaaDevCtxAddr[0] = htole64(addr);
368 for (i = 0; i != sc->sc_noscratch; i++) {
369 struct usb_page_search buf_scp;
370 usbd_get_page(&sc->sc_hw.scratch_pc[i], 0, &buf_scp);
371 pdctxa->qwSpBufPtr[i] = htole64((uint64_t)buf_scp.physaddr);
374 addr = buf_res.physaddr;
376 XWRITE4(sc, oper, XHCI_DCBAAP_LO, (uint32_t)addr);
377 XWRITE4(sc, oper, XHCI_DCBAAP_HI, (uint32_t)(addr >> 32));
378 XWRITE4(sc, oper, XHCI_DCBAAP_LO, (uint32_t)addr);
379 XWRITE4(sc, oper, XHCI_DCBAAP_HI, (uint32_t)(addr >> 32));
381 /* Setup event table size */
383 temp = XREAD4(sc, capa, XHCI_HCSPARAMS2);
385 DPRINTF("HCS2=0x%08x\n", temp);
387 temp = XHCI_HCS2_ERST_MAX(temp);
389 if (temp > XHCI_MAX_RSEG)
390 temp = XHCI_MAX_RSEG;
392 sc->sc_erst_max = temp;
394 DPRINTF("ERSTSZ=0x%08x -> 0x%08x\n",
395 XREAD4(sc, runt, XHCI_ERSTSZ(0)), temp);
397 XWRITE4(sc, runt, XHCI_ERSTSZ(0), XHCI_ERSTS_SET(temp));
399 /* Setup interrupt rate */
400 XWRITE4(sc, runt, XHCI_IMOD(0), XHCI_IMOD_DEFAULT);
402 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
404 phwr = buf_res.buffer;
405 addr = buf_res.physaddr;
406 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_events[0];
408 /* reset hardware root structure */
409 memset(phwr, 0, sizeof(*phwr));
411 phwr->hwr_ring_seg[0].qwEvrsTablePtr = htole64(addr);
412 phwr->hwr_ring_seg[0].dwEvrsTableSize = htole32(XHCI_MAX_EVENTS);
414 DPRINTF("ERDP(0)=0x%016llx\n", (unsigned long long)addr);
416 XWRITE4(sc, runt, XHCI_ERDP_LO(0), (uint32_t)addr);
417 XWRITE4(sc, runt, XHCI_ERDP_HI(0), (uint32_t)(addr >> 32));
419 addr = (uint64_t)buf_res.physaddr;
421 DPRINTF("ERSTBA(0)=0x%016llx\n", (unsigned long long)addr);
423 XWRITE4(sc, runt, XHCI_ERSTBA_LO(0), (uint32_t)addr);
424 XWRITE4(sc, runt, XHCI_ERSTBA_HI(0), (uint32_t)(addr >> 32));
426 /* Setup interrupter registers */
428 temp = XREAD4(sc, runt, XHCI_IMAN(0));
429 temp |= XHCI_IMAN_INTR_ENA;
430 XWRITE4(sc, runt, XHCI_IMAN(0), temp);
432 /* setup command ring control base address */
433 addr = buf_res.physaddr;
434 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_commands[0];
436 DPRINTF("CRCR=0x%016llx\n", (unsigned long long)addr);
438 XWRITE4(sc, oper, XHCI_CRCR_LO, ((uint32_t)addr) | XHCI_CRCR_LO_RCS);
439 XWRITE4(sc, oper, XHCI_CRCR_HI, (uint32_t)(addr >> 32));
441 phwr->hwr_commands[XHCI_MAX_COMMANDS - 1].qwTrb0 = htole64(addr);
443 usb_bus_mem_flush_all(&sc->sc_bus, &xhci_iterate_hw_softc);
446 XWRITE4(sc, oper, XHCI_USBCMD, XHCI_CMD_RS |
447 XHCI_CMD_INTE | XHCI_CMD_HSEE);
449 for (i = 0; i != 100; i++) {
450 usb_pause_mtx(NULL, hz / 100);
451 temp = XREAD4(sc, oper, XHCI_USBSTS) & XHCI_STS_HCH;
456 XWRITE4(sc, oper, XHCI_USBCMD, 0);
457 device_printf(sc->sc_bus.parent, "Run timeout.\n");
458 return (USB_ERR_IOERROR);
461 /* catch any lost interrupts */
462 xhci_do_poll(&sc->sc_bus);
468 xhci_halt_controller(struct xhci_softc *sc)
476 sc->sc_oper_off = XREAD1(sc, capa, XHCI_CAPLENGTH);
477 sc->sc_runt_off = XREAD4(sc, capa, XHCI_RTSOFF) & ~0xF;
478 sc->sc_door_off = XREAD4(sc, capa, XHCI_DBOFF) & ~0x3;
480 /* Halt controller */
481 XWRITE4(sc, oper, XHCI_USBCMD, 0);
483 for (i = 0; i != 100; i++) {
484 usb_pause_mtx(NULL, hz / 100);
485 temp = XREAD4(sc, oper, XHCI_USBSTS) & XHCI_STS_HCH;
491 device_printf(sc->sc_bus.parent, "Controller halt timeout.\n");
492 return (USB_ERR_IOERROR);
498 xhci_init(struct xhci_softc *sc, device_t self)
500 /* initialise some bus fields */
501 sc->sc_bus.parent = self;
503 /* set the bus revision */
504 sc->sc_bus.usbrev = USB_REV_3_0;
506 /* set up the bus struct */
507 sc->sc_bus.methods = &xhci_bus_methods;
509 /* setup devices array */
510 sc->sc_bus.devices = sc->sc_devices;
511 sc->sc_bus.devices_max = XHCI_MAX_DEVICES;
513 /* setup command queue mutex and condition varible */
514 cv_init(&sc->sc_cmd_cv, "CMDQ");
515 lockinit(&sc->sc_cmd_lock, "CMDQ lock", 0, 0);
517 /* get all DMA memory */
518 if (usb_bus_mem_alloc_all(&sc->sc_bus,
519 USB_GET_DMA_TAG(self), &xhci_iterate_hw_softc)) {
523 sc->sc_config_msg[0].hdr.pm_callback = &xhci_configure_msg;
524 sc->sc_config_msg[0].bus = &sc->sc_bus;
525 sc->sc_config_msg[1].hdr.pm_callback = &xhci_configure_msg;
526 sc->sc_config_msg[1].bus = &sc->sc_bus;
528 if (usb_proc_create(&sc->sc_config_proc,
529 &sc->sc_bus.bus_lock, device_get_nameunit(self), USB_PRI_MED)) {
530 kprintf("WARNING: Creation of XHCI configure "
531 "callback process failed.\n");
537 xhci_uninit(struct xhci_softc *sc)
539 usb_proc_free(&sc->sc_config_proc);
541 usb_bus_mem_free_all(&sc->sc_bus, &xhci_iterate_hw_softc);
543 cv_destroy(&sc->sc_cmd_cv);
544 lockuninit(&sc->sc_cmd_lock);
548 xhci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
550 struct xhci_softc *sc = XHCI_BUS2SC(bus);
553 case USB_HW_POWER_SUSPEND:
554 DPRINTF("Stopping the XHCI\n");
555 xhci_halt_controller(sc);
557 case USB_HW_POWER_SHUTDOWN:
558 DPRINTF("Stopping the XHCI\n");
559 xhci_halt_controller(sc);
561 case USB_HW_POWER_RESUME:
562 DPRINTF("Starting the XHCI\n");
563 xhci_start_controller(sc);
571 xhci_generic_done_sub(struct usb_xfer *xfer)
574 struct xhci_td *td_alt_next;
578 td = xfer->td_transfer_cache;
579 td_alt_next = td->alt_next;
581 if (xfer->aframes != xfer->nframes)
582 usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
586 usb_pc_cpu_invalidate(td->page_cache);
591 DPRINTFN(4, "xfer=%p[%u/%u] rem=%u/%u status=%u\n",
592 xfer, (unsigned int)xfer->aframes,
593 (unsigned int)xfer->nframes,
594 (unsigned int)len, (unsigned int)td->len,
595 (unsigned int)status);
598 * Verify the status length and
599 * add the length to "frlengths[]":
602 /* should not happen */
603 DPRINTF("Invalid status length, "
604 "0x%04x/0x%04x bytes\n", len, td->len);
605 status = XHCI_TRB_ERROR_LENGTH;
606 } else if (xfer->aframes != xfer->nframes) {
607 xfer->frlengths[xfer->aframes] += td->len - len;
609 /* Check for last transfer */
610 if (((void *)td) == xfer->td_transfer_last) {
614 /* Check for transfer error */
615 if (status != XHCI_TRB_ERROR_SHORT_PKT &&
616 status != XHCI_TRB_ERROR_SUCCESS) {
617 /* the transfer is finished */
621 /* Check for short transfer */
623 if (xfer->flags_int.short_frames_ok ||
624 xfer->flags_int.isochronous_xfr ||
625 xfer->flags_int.control_xfr) {
626 /* follow alt next */
629 /* the transfer is finished */
636 if (td->alt_next != td_alt_next) {
637 /* this USB frame is complete */
642 /* update transfer cache */
644 xfer->td_transfer_cache = td;
646 return ((status == XHCI_TRB_ERROR_STALL) ? USB_ERR_STALLED :
647 (status != XHCI_TRB_ERROR_SHORT_PKT &&
648 status != XHCI_TRB_ERROR_SUCCESS) ? USB_ERR_IOERROR :
649 USB_ERR_NORMAL_COMPLETION);
653 xhci_generic_done(struct usb_xfer *xfer)
657 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
658 xfer, xfer->endpoint);
662 xfer->td_transfer_cache = xfer->td_transfer_first;
664 if (xfer->flags_int.control_xfr) {
666 if (xfer->flags_int.control_hdr)
667 err = xhci_generic_done_sub(xfer);
671 if (xfer->td_transfer_cache == NULL)
675 while (xfer->aframes != xfer->nframes) {
677 err = xhci_generic_done_sub(xfer);
680 if (xfer->td_transfer_cache == NULL)
684 if (xfer->flags_int.control_xfr &&
685 !xfer->flags_int.control_act)
686 err = xhci_generic_done_sub(xfer);
688 /* transfer is complete */
689 xhci_device_done(xfer, err);
693 xhci_activate_transfer(struct usb_xfer *xfer)
697 td = xfer->td_transfer_cache;
699 usb_pc_cpu_invalidate(td->page_cache);
701 if (!(td->td_trb[0].dwTrb3 & htole32(XHCI_TRB_3_CYCLE_BIT))) {
703 /* activate the transfer */
705 td->td_trb[0].dwTrb3 |= htole32(XHCI_TRB_3_CYCLE_BIT);
706 usb_pc_cpu_flush(td->page_cache);
708 xhci_endpoint_doorbell(xfer);
713 xhci_skip_transfer(struct usb_xfer *xfer)
716 struct xhci_td *td_last;
718 td = xfer->td_transfer_cache;
719 td_last = xfer->td_transfer_last;
723 usb_pc_cpu_invalidate(td->page_cache);
725 if (!(td->td_trb[0].dwTrb3 & htole32(XHCI_TRB_3_CYCLE_BIT))) {
727 usb_pc_cpu_invalidate(td_last->page_cache);
729 /* copy LINK TRB to current waiting location */
731 td->td_trb[0].qwTrb0 = td_last->td_trb[td_last->ntrb].qwTrb0;
732 td->td_trb[0].dwTrb2 = td_last->td_trb[td_last->ntrb].dwTrb2;
733 usb_pc_cpu_flush(td->page_cache);
735 td->td_trb[0].dwTrb3 = td_last->td_trb[td_last->ntrb].dwTrb3;
736 usb_pc_cpu_flush(td->page_cache);
738 xhci_endpoint_doorbell(xfer);
742 /*------------------------------------------------------------------------*
743 * xhci_check_transfer
744 *------------------------------------------------------------------------*/
746 xhci_check_transfer(struct xhci_softc *sc, struct xhci_trb *trb)
759 td_event = le64toh(trb->qwTrb0);
760 temp = le32toh(trb->dwTrb2);
762 remainder = XHCI_TRB_2_REM_GET(temp);
763 status = XHCI_TRB_2_ERROR_GET(temp);
765 temp = le32toh(trb->dwTrb3);
766 epno = XHCI_TRB_3_EP_GET(temp);
767 index = XHCI_TRB_3_SLOT_GET(temp);
769 /* check if error means halted */
770 halted = (status != XHCI_TRB_ERROR_SHORT_PKT &&
771 status != XHCI_TRB_ERROR_SUCCESS);
773 DPRINTF("slot=%u epno=%u remainder=%u status=%u\n",
774 index, epno, remainder, status);
776 if (index > sc->sc_noslot) {
777 DPRINTF("Invalid slot.\n");
781 if ((epno == 0) || (epno >= XHCI_MAX_ENDPOINTS)) {
782 DPRINTF("Invalid endpoint.\n");
786 /* try to find the USB transfer that generated the event */
787 for (i = 0; i != (XHCI_MAX_TRANSFERS - 1); i++) {
788 struct usb_xfer *xfer;
790 struct xhci_endpoint_ext *pepext;
792 pepext = &sc->sc_hw.devs[index].endp[epno];
794 xfer = pepext->xfer[i];
798 td = xfer->td_transfer_cache;
800 DPRINTFN(5, "Checking if 0x%016llx == (0x%016llx .. 0x%016llx)\n",
802 (long long)td->td_self,
803 (long long)td->td_self + sizeof(td->td_trb));
806 * NOTE: Some XHCI implementations might not trigger
807 * an event on the last LINK TRB so we need to
808 * consider both the last and second last event
809 * address as conditions for a successful transfer.
811 * NOTE: We assume that the XHCI will only trigger one
812 * event per chain of TRBs.
815 offset = td_event - td->td_self;
818 offset < sizeof(td->td_trb)) {
820 usb_pc_cpu_invalidate(td->page_cache);
822 /* compute rest of remainder, if any */
823 for (i = (offset / 16) + 1; i < td->ntrb; i++) {
824 temp = le32toh(td->td_trb[i].dwTrb2);
825 remainder += XHCI_TRB_2_BYTES_GET(temp);
828 DPRINTFN(5, "New remainder: %u\n", remainder);
830 /* clear isochronous transfer errors */
831 if (xfer->flags_int.isochronous_xfr) {
834 status = XHCI_TRB_ERROR_SUCCESS;
839 /* "td->remainder" is verified later */
840 td->remainder = remainder;
843 usb_pc_cpu_flush(td->page_cache);
846 * 1) Last transfer descriptor makes the
849 if (((void *)td) == xfer->td_transfer_last) {
850 DPRINTF("TD is last\n");
851 xhci_generic_done(xfer);
856 * 2) Any kind of error makes the transfer
860 DPRINTF("TD has I/O error\n");
861 xhci_generic_done(xfer);
866 * 3) If there is no alternate next transfer,
867 * a short packet also makes the transfer done
869 if (td->remainder > 0) {
870 DPRINTF("TD has short pkt\n");
871 if (xfer->flags_int.short_frames_ok ||
872 xfer->flags_int.isochronous_xfr ||
873 xfer->flags_int.control_xfr) {
874 /* follow the alt next */
875 xfer->td_transfer_cache = td->alt_next;
876 xhci_activate_transfer(xfer);
879 xhci_skip_transfer(xfer);
880 xhci_generic_done(xfer);
885 * 4) Transfer complete - go to next TD
887 DPRINTF("Following next TD\n");
888 xfer->td_transfer_cache = td->obj_next;
889 xhci_activate_transfer(xfer);
890 break; /* there should only be one match */
896 xhci_check_command(struct xhci_softc *sc, struct xhci_trb *trb)
898 if (sc->sc_cmd_addr == trb->qwTrb0) {
899 DPRINTF("Received command event\n");
900 sc->sc_cmd_result[0] = trb->dwTrb2;
901 sc->sc_cmd_result[1] = trb->dwTrb3;
902 cv_signal(&sc->sc_cmd_cv);
907 xhci_interrupt_poll(struct xhci_softc *sc)
909 struct usb_page_search buf_res;
910 struct xhci_hw_root *phwr;
919 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
921 phwr = buf_res.buffer;
923 /* Receive any events */
925 usb_pc_cpu_invalidate(&sc->sc_hw.root_pc);
927 i = sc->sc_event_idx;
928 j = sc->sc_event_ccs;
933 temp = le32toh(phwr->hwr_events[i].dwTrb3);
935 k = (temp & XHCI_TRB_3_CYCLE_BIT) ? 1 : 0;
940 event = XHCI_TRB_3_TYPE_GET(temp);
942 DPRINTFN(10, "event[%u] = %u (0x%016llx 0x%08lx 0x%08lx)\n",
943 i, event, (long long)le64toh(phwr->hwr_events[i].qwTrb0),
944 (long)le32toh(phwr->hwr_events[i].dwTrb2),
945 (long)le32toh(phwr->hwr_events[i].dwTrb3));
948 case XHCI_TRB_EVENT_TRANSFER:
949 xhci_check_transfer(sc, &phwr->hwr_events[i]);
951 case XHCI_TRB_EVENT_CMD_COMPLETE:
952 xhci_check_command(sc, &phwr->hwr_events[i]);
955 DPRINTF("Unhandled event = %u\n", event);
961 if (i == XHCI_MAX_EVENTS) {
965 /* check for timeout */
971 sc->sc_event_idx = i;
972 sc->sc_event_ccs = j;
975 * NOTE: The Event Ring Dequeue Pointer Register is 64-bit
976 * latched. That means to activate the register we need to
977 * write both the low and high double word of the 64-bit
981 addr = (uint32_t)buf_res.physaddr;
982 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_events[i];
984 /* try to clear busy bit */
985 addr |= XHCI_ERDP_LO_BUSY;
987 XWRITE4(sc, runt, XHCI_ERDP_LO(0), (uint32_t)addr);
988 XWRITE4(sc, runt, XHCI_ERDP_HI(0), (uint32_t)(addr >> 32));
992 xhci_do_command(struct xhci_softc *sc, struct xhci_trb *trb,
995 struct usb_page_search buf_res;
996 struct xhci_hw_root *phwr;
1003 XHCI_CMD_ASSERT_LOCKED(sc);
1005 /* get hardware root structure */
1007 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
1009 phwr = buf_res.buffer;
1013 USB_BUS_LOCK(&sc->sc_bus);
1015 i = sc->sc_command_idx;
1016 j = sc->sc_command_ccs;
1018 DPRINTFN(10, "command[%u] = %u (0x%016llx, 0x%08lx, 0x%08lx)\n",
1019 i, XHCI_TRB_3_TYPE_GET(le32toh(trb->dwTrb3)),
1020 (long long)le64toh(trb->qwTrb0),
1021 (long)le32toh(trb->dwTrb2),
1022 (long)le32toh(trb->dwTrb3));
1024 phwr->hwr_commands[i].qwTrb0 = trb->qwTrb0;
1025 phwr->hwr_commands[i].dwTrb2 = trb->dwTrb2;
1027 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1032 temp |= htole32(XHCI_TRB_3_CYCLE_BIT);
1034 temp &= ~htole32(XHCI_TRB_3_CYCLE_BIT);
1036 temp &= ~htole32(XHCI_TRB_3_TC_BIT);
1038 phwr->hwr_commands[i].dwTrb3 = temp;
1040 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1042 addr = buf_res.physaddr;
1043 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_commands[i];
1045 sc->sc_cmd_addr = htole64(addr);
1049 if (i == (XHCI_MAX_COMMANDS - 1)) {
1052 temp = htole32(XHCI_TRB_3_TC_BIT |
1053 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) |
1054 XHCI_TRB_3_CYCLE_BIT);
1056 temp = htole32(XHCI_TRB_3_TC_BIT |
1057 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
1060 phwr->hwr_commands[i].dwTrb3 = temp;
1062 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1068 sc->sc_command_idx = i;
1069 sc->sc_command_ccs = j;
1071 XWRITE4(sc, door, XHCI_DOORBELL(0), 0);
1073 err = cv_timedwait(&sc->sc_cmd_cv, &sc->sc_bus.bus_lock,
1074 USB_MS_TO_TICKS(timeout_ms));
1077 DPRINTFN(0, "Command timeout!\n");
1078 err = USB_ERR_TIMEOUT;
1082 temp = le32toh(sc->sc_cmd_result[0]);
1083 if (XHCI_TRB_2_ERROR_GET(temp) != XHCI_TRB_ERROR_SUCCESS)
1084 err = USB_ERR_IOERROR;
1086 trb->dwTrb2 = sc->sc_cmd_result[0];
1087 trb->dwTrb3 = sc->sc_cmd_result[1];
1090 USB_BUS_UNLOCK(&sc->sc_bus);
1097 xhci_cmd_nop(struct xhci_softc *sc)
1099 struct xhci_trb trb;
1106 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NOOP);
1108 trb.dwTrb3 = htole32(temp);
1110 return (xhci_do_command(sc, &trb, 100 /* ms */));
1115 xhci_cmd_enable_slot(struct xhci_softc *sc, uint8_t *pslot)
1117 struct xhci_trb trb;
1125 trb.dwTrb3 = htole32(XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ENABLE_SLOT));
1127 err = xhci_do_command(sc, &trb, 100 /* ms */);
1131 temp = le32toh(trb.dwTrb3);
1133 *pslot = XHCI_TRB_3_SLOT_GET(temp);
1140 xhci_cmd_disable_slot(struct xhci_softc *sc, uint8_t slot_id)
1142 struct xhci_trb trb;
1149 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DISABLE_SLOT) |
1150 XHCI_TRB_3_SLOT_SET(slot_id);
1152 trb.dwTrb3 = htole32(temp);
1154 return (xhci_do_command(sc, &trb, 100 /* ms */));
1158 xhci_cmd_set_address(struct xhci_softc *sc, uint64_t input_ctx,
1159 uint8_t bsr, uint8_t slot_id)
1161 struct xhci_trb trb;
1166 trb.qwTrb0 = htole64(input_ctx);
1168 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ADDRESS_DEVICE) |
1169 XHCI_TRB_3_SLOT_SET(slot_id);
1172 temp |= XHCI_TRB_3_BSR_BIT;
1174 trb.dwTrb3 = htole32(temp);
1176 return (xhci_do_command(sc, &trb, 500 /* ms */));
1180 xhci_set_address(struct usb_device *udev, struct lock *lock, uint16_t address)
1182 struct usb_page_search buf_inp;
1183 struct usb_page_search buf_dev;
1184 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
1185 struct xhci_hw_dev *hdev;
1186 struct xhci_dev_ctx *pdev;
1187 struct xhci_endpoint_ext *pepext;
1193 /* the root HUB case is not handled here */
1194 if (udev->parent_hub == NULL)
1195 return (USB_ERR_INVAL);
1197 index = udev->controller_slot_id;
1199 hdev = &sc->sc_hw.devs[index];
1202 lockmgr(lock, LK_RELEASE);
1206 switch (hdev->state) {
1207 case XHCI_ST_DEFAULT:
1208 case XHCI_ST_ENABLED:
1210 hdev->state = XHCI_ST_ENABLED;
1212 /* set configure mask to slot and EP0 */
1213 xhci_configure_mask(udev, 3, 0);
1215 /* configure input slot context structure */
1216 err = xhci_configure_device(udev);
1219 DPRINTF("Could not configure device\n");
1223 /* configure input endpoint context structure */
1224 switch (udev->speed) {
1226 case USB_SPEED_FULL:
1229 case USB_SPEED_HIGH:
1237 pepext = xhci_get_endpoint_ext(udev,
1238 &udev->ctrl_ep_desc);
1239 err = xhci_configure_endpoint(udev,
1240 &udev->ctrl_ep_desc, pepext->physaddr,
1241 0, 1, 1, 0, mps, mps);
1244 DPRINTF("Could not configure default endpoint\n");
1248 /* execute set address command */
1249 usbd_get_page(&hdev->input_pc, 0, &buf_inp);
1251 err = xhci_cmd_set_address(sc, buf_inp.physaddr,
1252 (address == 0), index);
1255 DPRINTF("Could not set address "
1256 "for slot %u.\n", index);
1261 /* update device address to new value */
1263 usbd_get_page(&hdev->device_pc, 0, &buf_dev);
1264 pdev = buf_dev.buffer;
1265 usb_pc_cpu_invalidate(&hdev->device_pc);
1267 temp = xhci_ctx_get_le32(sc, &pdev->ctx_slot.dwSctx3);
1268 udev->address = XHCI_SCTX_3_DEV_ADDR_GET(temp);
1270 /* update device state to new value */
1273 hdev->state = XHCI_ST_ADDRESSED;
1275 hdev->state = XHCI_ST_DEFAULT;
1279 DPRINTF("Wrong state for set address.\n");
1280 err = USB_ERR_IOERROR;
1283 XHCI_CMD_UNLOCK(sc);
1286 lockmgr(lock, LK_EXCLUSIVE);
1292 xhci_cmd_configure_ep(struct xhci_softc *sc, uint64_t input_ctx,
1293 uint8_t deconfigure, uint8_t slot_id)
1295 struct xhci_trb trb;
1300 trb.qwTrb0 = htole64(input_ctx);
1302 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_CONFIGURE_EP) |
1303 XHCI_TRB_3_SLOT_SET(slot_id);
1306 temp |= XHCI_TRB_3_DCEP_BIT;
1308 trb.dwTrb3 = htole32(temp);
1310 return (xhci_do_command(sc, &trb, 100 /* ms */));
1314 xhci_cmd_evaluate_ctx(struct xhci_softc *sc, uint64_t input_ctx,
1317 struct xhci_trb trb;
1322 trb.qwTrb0 = htole64(input_ctx);
1324 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVALUATE_CTX) |
1325 XHCI_TRB_3_SLOT_SET(slot_id);
1326 trb.dwTrb3 = htole32(temp);
1328 return (xhci_do_command(sc, &trb, 100 /* ms */));
1332 xhci_cmd_reset_ep(struct xhci_softc *sc, uint8_t preserve,
1333 uint8_t ep_id, uint8_t slot_id)
1335 struct xhci_trb trb;
1342 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_EP) |
1343 XHCI_TRB_3_SLOT_SET(slot_id) |
1344 XHCI_TRB_3_EP_SET(ep_id);
1347 temp |= XHCI_TRB_3_PRSV_BIT;
1349 trb.dwTrb3 = htole32(temp);
1351 return (xhci_do_command(sc, &trb, 100 /* ms */));
1355 xhci_cmd_set_tr_dequeue_ptr(struct xhci_softc *sc, uint64_t dequeue_ptr,
1356 uint16_t stream_id, uint8_t ep_id, uint8_t slot_id)
1358 struct xhci_trb trb;
1363 trb.qwTrb0 = htole64(dequeue_ptr);
1365 temp = XHCI_TRB_2_STREAM_SET(stream_id);
1366 trb.dwTrb2 = htole32(temp);
1368 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SET_TR_DEQUEUE) |
1369 XHCI_TRB_3_SLOT_SET(slot_id) |
1370 XHCI_TRB_3_EP_SET(ep_id);
1371 trb.dwTrb3 = htole32(temp);
1373 return (xhci_do_command(sc, &trb, 100 /* ms */));
1377 xhci_cmd_stop_ep(struct xhci_softc *sc, uint8_t suspend,
1378 uint8_t ep_id, uint8_t slot_id)
1380 struct xhci_trb trb;
1387 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STOP_EP) |
1388 XHCI_TRB_3_SLOT_SET(slot_id) |
1389 XHCI_TRB_3_EP_SET(ep_id);
1392 temp |= XHCI_TRB_3_SUSP_EP_BIT;
1394 trb.dwTrb3 = htole32(temp);
1396 return (xhci_do_command(sc, &trb, 100 /* ms */));
1400 xhci_cmd_reset_dev(struct xhci_softc *sc, uint8_t slot_id)
1402 struct xhci_trb trb;
1409 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_DEVICE) |
1410 XHCI_TRB_3_SLOT_SET(slot_id);
1412 trb.dwTrb3 = htole32(temp);
1414 return (xhci_do_command(sc, &trb, 100 /* ms */));
1417 /*------------------------------------------------------------------------*
1418 * xhci_interrupt - XHCI interrupt handler
1419 *------------------------------------------------------------------------*/
1421 xhci_interrupt(struct xhci_softc *sc)
1426 USB_BUS_LOCK(&sc->sc_bus);
1428 status = XREAD4(sc, oper, XHCI_USBSTS);
1430 /* acknowledge interrupts */
1432 XWRITE4(sc, oper, XHCI_USBSTS, status);
1434 temp = XREAD4(sc, runt, XHCI_IMAN(0));
1436 /* acknowledge pending event */
1438 XWRITE4(sc, runt, XHCI_IMAN(0), temp);
1440 DPRINTFN(16, "real interrupt (sts=0x%08x, "
1441 "iman=0x%08x)\n", status, temp);
1444 if (status & XHCI_STS_PCD) {
1448 if (status & XHCI_STS_HCH) {
1449 kprintf("%s: host controller halted\n",
1453 if (status & XHCI_STS_HSE) {
1454 kprintf("%s: host system error\n",
1458 if (status & XHCI_STS_HCE) {
1459 kprintf("%s: host controller error\n",
1464 xhci_interrupt_poll(sc);
1466 USB_BUS_UNLOCK(&sc->sc_bus);
1469 /*------------------------------------------------------------------------*
1470 * xhci_timeout - XHCI timeout handler
1471 *------------------------------------------------------------------------*/
1473 xhci_timeout(void *arg)
1475 struct usb_xfer *xfer = arg;
1477 DPRINTF("xfer=%p\n", xfer);
1479 USB_BUS_LOCK_ASSERT(xfer->xroot->bus);
1481 /* transfer is transferred */
1482 xhci_device_done(xfer, USB_ERR_TIMEOUT);
1486 xhci_do_poll(struct usb_bus *bus)
1488 struct xhci_softc *sc = XHCI_BUS2SC(bus);
1490 USB_BUS_LOCK(&sc->sc_bus);
1491 xhci_interrupt_poll(sc);
1492 USB_BUS_UNLOCK(&sc->sc_bus);
1496 xhci_setup_generic_chain_sub(struct xhci_std_temp *temp)
1498 struct usb_page_search buf_res;
1500 struct xhci_td *td_next;
1501 struct xhci_td *td_alt_next;
1502 uint32_t buf_offset;
1506 uint8_t shortpkt_old;
1512 shortpkt_old = temp->shortpkt;
1513 len_old = temp->len;
1519 td_next = temp->td_next;
1523 if (temp->len == 0) {
1528 /* send a Zero Length Packet, ZLP, last */
1535 average = temp->average;
1537 if (temp->len < average) {
1538 if (temp->len % temp->max_packet_size) {
1541 average = temp->len;
1545 if (td_next == NULL)
1546 panic("%s: out of XHCI transfer descriptors!", __FUNCTION__);
1551 td_next = td->obj_next;
1553 /* check if we are pre-computing */
1557 /* update remaining length */
1559 temp->len -= average;
1563 /* fill out current TD */
1569 /* update remaining length */
1571 temp->len -= average;
1573 /* reset TRB index */
1577 if (temp->trb_type == XHCI_TRB_TYPE_SETUP_STAGE) {
1578 /* immediate data */
1583 td->td_trb[0].qwTrb0 = 0;
1585 usbd_copy_out(temp->pc, temp->offset + buf_offset,
1586 (uint8_t *)(uintptr_t)&td->td_trb[0].qwTrb0,
1589 dword = XHCI_TRB_2_BYTES_SET(8) |
1590 XHCI_TRB_2_TDSZ_SET(0) |
1591 XHCI_TRB_2_IRQ_SET(0);
1593 td->td_trb[0].dwTrb2 = htole32(dword);
1595 dword = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SETUP_STAGE) |
1596 XHCI_TRB_3_IDT_BIT | XHCI_TRB_3_CYCLE_BIT;
1599 if (td->td_trb[0].qwTrb0 &
1600 htole64(XHCI_TRB_0_WLENGTH_MASK)) {
1601 if (td->td_trb[0].qwTrb0 & htole64(1))
1602 dword |= XHCI_TRB_3_TRT_IN;
1604 dword |= XHCI_TRB_3_TRT_OUT;
1607 td->td_trb[0].dwTrb3 = htole32(dword);
1609 xhci_dump_trb(&td->td_trb[x]);
1617 /* fill out buffer pointers */
1621 memset(&buf_res, 0, sizeof(buf_res));
1623 usbd_get_page(temp->pc, temp->offset +
1624 buf_offset, &buf_res);
1626 /* get length to end of page */
1627 if (buf_res.length > average)
1628 buf_res.length = average;
1630 /* check for maximum length */
1631 if (buf_res.length > XHCI_TD_PAGE_SIZE)
1632 buf_res.length = XHCI_TD_PAGE_SIZE;
1635 npkt = (average + temp->max_packet_size - 1) /
1636 temp->max_packet_size;
1642 /* fill out TRB's */
1643 td->td_trb[x].qwTrb0 =
1644 htole64((uint64_t)buf_res.physaddr);
1647 XHCI_TRB_2_BYTES_SET(buf_res.length) |
1648 XHCI_TRB_2_TDSZ_SET(npkt) |
1649 XHCI_TRB_2_IRQ_SET(0);
1651 td->td_trb[x].dwTrb2 = htole32(dword);
1653 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1654 XHCI_TRB_3_TYPE_SET(temp->trb_type) |
1655 XHCI_TRB_3_FRID_SET(temp->isoc_frame / 8) |
1656 XHCI_TRB_3_TBC_SET(temp->tbc) |
1657 XHCI_TRB_3_TLBPC_SET(temp->tlbpc);
1659 if (temp->direction == UE_DIR_IN) {
1660 dword |= XHCI_TRB_3_DIR_IN;
1663 * NOTE: Only the SETUP stage should
1664 * use the IDT bit. Else transactions
1665 * can be sent using the wrong data
1668 if (temp->trb_type !=
1669 XHCI_TRB_TYPE_SETUP_STAGE &&
1671 XHCI_TRB_TYPE_STATUS_STAGE)
1672 dword |= XHCI_TRB_3_ISP_BIT;
1675 td->td_trb[x].dwTrb3 = htole32(dword);
1677 average -= buf_res.length;
1678 buf_offset += buf_res.length;
1680 xhci_dump_trb(&td->td_trb[x]);
1684 } while (average != 0);
1686 td->td_trb[x-1].dwTrb3 |= htole32(XHCI_TRB_3_IOC_BIT);
1688 /* store number of data TRB's */
1692 DPRINTF("NTRB=%u\n", x);
1694 /* fill out link TRB */
1696 if (td_next != NULL) {
1697 /* link the current TD with the next one */
1698 td->td_trb[x].qwTrb0 = htole64((uint64_t)td_next->td_self);
1699 DPRINTF("LINK=0x%08llx\n", (long long)td_next->td_self);
1701 /* this field will get updated later */
1702 DPRINTF("NOLINK\n");
1705 dword = XHCI_TRB_2_IRQ_SET(0);
1707 td->td_trb[x].dwTrb2 = htole32(dword);
1709 dword = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) |
1710 XHCI_TRB_3_CYCLE_BIT | XHCI_TRB_3_IOC_BIT;
1712 td->td_trb[x].dwTrb3 = htole32(dword);
1714 td->alt_next = td_alt_next;
1716 xhci_dump_trb(&td->td_trb[x]);
1718 usb_pc_cpu_flush(td->page_cache);
1724 /* setup alt next pointer, if any */
1725 if (temp->last_frame) {
1728 /* we use this field internally */
1729 td_alt_next = td_next;
1733 temp->shortpkt = shortpkt_old;
1734 temp->len = len_old;
1738 /* remove cycle bit from first if we are stepping the TRBs */
1740 td->td_trb[0].dwTrb3 &= ~htole32(XHCI_TRB_3_CYCLE_BIT);
1742 /* remove chain bit because this is the last TRB in the chain */
1743 td->td_trb[td->ntrb - 1].dwTrb2 &= ~htole32(XHCI_TRB_2_TDSZ_SET(15));
1744 td->td_trb[td->ntrb - 1].dwTrb3 &= ~htole32(XHCI_TRB_3_CHAIN_BIT);
1746 usb_pc_cpu_flush(td->page_cache);
1749 temp->td_next = td_next;
1753 xhci_setup_generic_chain(struct usb_xfer *xfer)
1755 struct xhci_std_temp temp;
1764 temp.average = xfer->max_hc_frame_size;
1765 temp.max_packet_size = xfer->max_packet_size;
1766 temp.sc = XHCI_BUS2SC(xfer->xroot->bus);
1768 temp.last_frame = 0;
1770 temp.multishort = xfer->flags_int.isochronous_xfr ||
1771 xfer->flags_int.control_xfr ||
1772 xfer->flags_int.short_frames_ok;
1774 /* toggle the DMA set we are using */
1775 xfer->flags_int.curr_dma_set ^= 1;
1777 /* get next DMA set */
1778 td = xfer->td_start[xfer->flags_int.curr_dma_set];
1783 xfer->td_transfer_first = td;
1784 xfer->td_transfer_cache = td;
1786 if (xfer->flags_int.isochronous_xfr) {
1789 /* compute multiplier for ISOCHRONOUS transfers */
1790 mult = xfer->endpoint->ecomp ?
1791 (xfer->endpoint->ecomp->bmAttributes & 3) : 0;
1792 /* check for USB 2.0 multiplier */
1794 mult = (xfer->endpoint->edesc->
1795 wMaxPacketSize[1] >> 3) & 3;
1803 x = XREAD4(temp.sc, runt, XHCI_MFINDEX);
1805 DPRINTF("MFINDEX=0x%08x\n", x);
1807 switch (usbd_get_speed(xfer->xroot->udev)) {
1808 case USB_SPEED_FULL:
1810 temp.isoc_delta = 8; /* 1ms */
1811 x += temp.isoc_delta - 1;
1812 x &= ~(temp.isoc_delta - 1);
1815 shift = usbd_xfer_get_fps_shift(xfer);
1816 temp.isoc_delta = 1U << shift;
1817 x += temp.isoc_delta - 1;
1818 x &= ~(temp.isoc_delta - 1);
1819 /* simple frame load balancing */
1820 x += xfer->endpoint->usb_uframe;
1824 y = XHCI_MFINDEX_GET(x - xfer->endpoint->isoc_next);
1826 if ((xfer->endpoint->is_synced == 0) ||
1827 (y < (xfer->nframes << shift)) ||
1828 (XHCI_MFINDEX_GET(-y) >= (128 * 8))) {
1830 * If there is data underflow or the pipe
1831 * queue is empty we schedule the transfer a
1832 * few frames ahead of the current frame
1833 * position. Else two isochronous transfers
1836 xfer->endpoint->isoc_next = XHCI_MFINDEX_GET(x + (3 * 8));
1837 xfer->endpoint->is_synced = 1;
1838 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
1841 /* compute isochronous completion time */
1843 y = XHCI_MFINDEX_GET(xfer->endpoint->isoc_next - (x & ~7));
1845 xfer->isoc_time_complete =
1846 usb_isoc_time_expand(&temp.sc->sc_bus, x / 8) +
1847 (y / 8) + (((xfer->nframes << shift) + 7) / 8);
1850 temp.isoc_frame = xfer->endpoint->isoc_next;
1851 temp.trb_type = XHCI_TRB_TYPE_ISOCH;
1853 xfer->endpoint->isoc_next += xfer->nframes << shift;
1855 } else if (xfer->flags_int.control_xfr) {
1857 /* check if we should prepend a setup message */
1859 if (xfer->flags_int.control_hdr) {
1861 temp.len = xfer->frlengths[0];
1862 temp.pc = xfer->frbuffers + 0;
1863 temp.shortpkt = temp.len ? 1 : 0;
1864 temp.trb_type = XHCI_TRB_TYPE_SETUP_STAGE;
1867 /* check for last frame */
1868 if (xfer->nframes == 1) {
1869 /* no STATUS stage yet, SETUP is last */
1870 if (xfer->flags_int.control_act)
1871 temp.last_frame = 1;
1874 xhci_setup_generic_chain_sub(&temp);
1878 temp.isoc_delta = 0;
1879 temp.isoc_frame = 0;
1880 temp.trb_type = XHCI_TRB_TYPE_DATA_STAGE;
1884 temp.isoc_delta = 0;
1885 temp.isoc_frame = 0;
1886 temp.trb_type = XHCI_TRB_TYPE_NORMAL;
1889 if (x != xfer->nframes) {
1890 /* setup page_cache pointer */
1891 temp.pc = xfer->frbuffers + x;
1892 /* set endpoint direction */
1893 temp.direction = UE_GET_DIR(xfer->endpointno);
1896 while (x != xfer->nframes) {
1898 /* DATA0 / DATA1 message */
1900 temp.len = xfer->frlengths[x];
1901 temp.step_td = ((xfer->endpointno & UE_DIR_IN) &&
1902 x != 0 && temp.multishort == 0);
1906 if (x == xfer->nframes) {
1907 if (xfer->flags_int.control_xfr) {
1908 /* no STATUS stage yet, DATA is last */
1909 if (xfer->flags_int.control_act)
1910 temp.last_frame = 1;
1912 temp.last_frame = 1;
1915 if (temp.len == 0) {
1917 /* make sure that we send an USB packet */
1922 temp.tlbpc = mult - 1;
1924 } else if (xfer->flags_int.isochronous_xfr) {
1928 /* isochronous transfers don't have short packet termination */
1932 /* isochronous transfers have a transfer limit */
1934 if (temp.len > xfer->max_frame_size)
1935 temp.len = xfer->max_frame_size;
1937 /* compute TD packet count */
1938 tdpc = (temp.len + xfer->max_packet_size - 1) /
1939 xfer->max_packet_size;
1941 temp.tbc = ((tdpc + mult - 1) / mult) - 1;
1942 temp.tlbpc = (tdpc % mult);
1944 if (temp.tlbpc == 0)
1945 temp.tlbpc = mult - 1;
1950 /* regular data transfer */
1952 temp.shortpkt = xfer->flags.force_short_xfer ? 0 : 1;
1955 xhci_setup_generic_chain_sub(&temp);
1957 if (xfer->flags_int.isochronous_xfr) {
1958 temp.offset += xfer->frlengths[x - 1];
1959 temp.isoc_frame += temp.isoc_delta;
1961 /* get next Page Cache pointer */
1962 temp.pc = xfer->frbuffers + x;
1966 /* check if we should append a status stage */
1968 if (xfer->flags_int.control_xfr &&
1969 !xfer->flags_int.control_act) {
1972 * Send a DATA1 message and invert the current
1973 * endpoint direction.
1975 temp.step_td = (xfer->nframes != 0);
1976 temp.direction = UE_GET_DIR(xfer->endpointno) ^ UE_DIR_IN;
1980 temp.last_frame = 1;
1981 temp.trb_type = XHCI_TRB_TYPE_STATUS_STAGE;
1983 xhci_setup_generic_chain_sub(&temp);
1988 /* must have at least one frame! */
1990 xfer->td_transfer_last = td;
1992 DPRINTF("first=%p last=%p\n", xfer->td_transfer_first, td);
1996 xhci_set_slot_pointer(struct xhci_softc *sc, uint8_t index, uint64_t dev_addr)
1998 struct usb_page_search buf_res;
1999 struct xhci_dev_ctx_addr *pdctxa;
2001 usbd_get_page(&sc->sc_hw.ctx_pc, 0, &buf_res);
2003 pdctxa = buf_res.buffer;
2005 DPRINTF("addr[%u]=0x%016llx\n", index, (long long)dev_addr);
2007 pdctxa->qwBaaDevCtxAddr[index] = htole64(dev_addr);
2009 usb_pc_cpu_flush(&sc->sc_hw.ctx_pc);
2013 xhci_configure_mask(struct usb_device *udev, uint32_t mask, uint8_t drop)
2015 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2016 struct usb_page_search buf_inp;
2017 struct xhci_input_dev_ctx *pinp;
2020 index = udev->controller_slot_id;
2022 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
2024 pinp = buf_inp.buffer;
2027 mask &= XHCI_INCTX_NON_CTRL_MASK;
2028 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx0, mask);
2029 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx1, 0);
2031 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx0, 0);
2032 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx1, mask);
2038 xhci_configure_endpoint(struct usb_device *udev,
2039 struct usb_endpoint_descriptor *edesc, uint64_t ring_addr,
2040 uint16_t interval, uint8_t max_packet_count, uint8_t mult,
2041 uint8_t fps_shift, uint16_t max_packet_size, uint16_t max_frame_size)
2043 struct usb_page_search buf_inp;
2044 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2045 struct xhci_input_dev_ctx *pinp;
2051 index = udev->controller_slot_id;
2053 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
2055 pinp = buf_inp.buffer;
2057 epno = edesc->bEndpointAddress;
2058 type = edesc->bmAttributes & UE_XFERTYPE;
2060 if (type == UE_CONTROL)
2063 epno = XHCI_EPNO2EPID(epno);
2066 return (USB_ERR_NO_PIPE); /* invalid */
2068 if (max_packet_count == 0)
2069 return (USB_ERR_BAD_BUFSIZE);
2074 return (USB_ERR_BAD_BUFSIZE);
2076 temp = XHCI_EPCTX_0_EPSTATE_SET(0) |
2077 XHCI_EPCTX_0_MAXP_STREAMS_SET(0) |
2078 XHCI_EPCTX_0_LSA_SET(0);
2080 switch (udev->speed) {
2081 case USB_SPEED_FULL:
2094 temp |= XHCI_EPCTX_0_IVAL_SET(fps_shift);
2096 case UE_ISOCHRONOUS:
2097 temp |= XHCI_EPCTX_0_IVAL_SET(fps_shift);
2099 switch (udev->speed) {
2100 case USB_SPEED_SUPER:
2103 temp |= XHCI_EPCTX_0_MULT_SET(mult - 1);
2104 max_packet_count /= mult;
2114 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx0, temp);
2117 XHCI_EPCTX_1_HID_SET(0) |
2118 XHCI_EPCTX_1_MAXB_SET(max_packet_count) |
2119 XHCI_EPCTX_1_MAXP_SIZE_SET(max_packet_size);
2121 if ((udev->parent_hs_hub != NULL) || (udev->address != 0)) {
2122 if (type != UE_ISOCHRONOUS)
2123 temp |= XHCI_EPCTX_1_CERR_SET(3);
2128 temp |= XHCI_EPCTX_1_EPTYPE_SET(4);
2130 case UE_ISOCHRONOUS:
2131 temp |= XHCI_EPCTX_1_EPTYPE_SET(1);
2134 temp |= XHCI_EPCTX_1_EPTYPE_SET(2);
2137 temp |= XHCI_EPCTX_1_EPTYPE_SET(3);
2141 /* check for IN direction */
2143 temp |= XHCI_EPCTX_1_EPTYPE_SET(4);
2145 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx1, temp);
2147 ring_addr |= XHCI_EPCTX_2_DCS_SET(1);
2149 xhci_ctx_set_le64(sc, &pinp->ctx_ep[epno - 1].qwEpCtx2, ring_addr);
2151 switch (edesc->bmAttributes & UE_XFERTYPE) {
2153 case UE_ISOCHRONOUS:
2154 temp = XHCI_EPCTX_4_MAX_ESIT_PAYLOAD_SET(max_frame_size) |
2155 XHCI_EPCTX_4_AVG_TRB_LEN_SET(MIN(XHCI_PAGE_SIZE,
2159 temp = XHCI_EPCTX_4_AVG_TRB_LEN_SET(8);
2162 temp = XHCI_EPCTX_4_AVG_TRB_LEN_SET(XHCI_PAGE_SIZE);
2166 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx4, temp);
2169 xhci_dump_endpoint(sc, &pinp->ctx_ep[epno - 1]);
2171 usb_pc_cpu_flush(&sc->sc_hw.devs[index].input_pc);
2173 return (0); /* success */
2177 xhci_configure_endpoint_by_xfer(struct usb_xfer *xfer)
2179 struct xhci_endpoint_ext *pepext;
2180 struct usb_endpoint_ss_comp_descriptor *ecomp;
2182 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2183 xfer->endpoint->edesc);
2185 ecomp = xfer->endpoint->ecomp;
2187 pepext->trb[0].dwTrb3 = 0; /* halt any transfers */
2188 usb_pc_cpu_flush(pepext->page_cache);
2190 return (xhci_configure_endpoint(xfer->xroot->udev,
2191 xfer->endpoint->edesc, pepext->physaddr,
2192 xfer->interval, xfer->max_packet_count,
2193 (ecomp != NULL) ? (ecomp->bmAttributes & 3) + 1 : 1,
2194 usbd_xfer_get_fps_shift(xfer), xfer->max_packet_size,
2195 xfer->max_frame_size));
2199 xhci_configure_device(struct usb_device *udev)
2201 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2202 struct usb_page_search buf_inp;
2203 struct usb_page_cache *pcinp;
2204 struct xhci_input_dev_ctx *pinp;
2205 struct usb_device *hubdev;
2213 index = udev->controller_slot_id;
2215 DPRINTF("index=%u\n", index);
2217 pcinp = &sc->sc_hw.devs[index].input_pc;
2219 usbd_get_page(pcinp, 0, &buf_inp);
2221 pinp = buf_inp.buffer;
2226 /* figure out route string and root HUB port number */
2228 for (hubdev = udev; hubdev != NULL; hubdev = hubdev->parent_hub) {
2230 if (hubdev->parent_hub == NULL)
2233 depth = hubdev->parent_hub->depth;
2236 * NOTE: HS/FS/LS devices and the SS root HUB can have
2237 * more than 15 ports
2240 rh_port = hubdev->port_no;
2249 route |= rh_port << (4 * (depth - 1));
2252 DPRINTF("Route=0x%08x\n", route);
2254 temp = XHCI_SCTX_0_ROUTE_SET(route);
2256 switch (sc->sc_hw.devs[index].state) {
2257 case XHCI_ST_CONFIGURED:
2258 temp |= XHCI_SCTX_0_CTX_NUM_SET(XHCI_MAX_ENDPOINTS - 1);
2261 temp |= XHCI_SCTX_0_CTX_NUM_SET(1);
2265 switch (udev->speed) {
2267 temp |= XHCI_SCTX_0_SPEED_SET(2);
2269 case USB_SPEED_HIGH:
2270 temp |= XHCI_SCTX_0_SPEED_SET(3);
2272 case USB_SPEED_FULL:
2273 temp |= XHCI_SCTX_0_SPEED_SET(1);
2276 temp |= XHCI_SCTX_0_SPEED_SET(4);
2280 is_hub = sc->sc_hw.devs[index].nports != 0 &&
2281 (udev->speed == USB_SPEED_SUPER ||
2282 udev->speed == USB_SPEED_HIGH);
2285 temp |= XHCI_SCTX_0_HUB_SET(1);
2287 if (udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) {
2288 DPRINTF("HUB supports MTT\n");
2289 temp |= XHCI_SCTX_0_MTT_SET(1);
2294 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx0, temp);
2296 temp = XHCI_SCTX_1_RH_PORT_SET(rh_port);
2299 temp |= XHCI_SCTX_1_NUM_PORTS_SET(
2300 sc->sc_hw.devs[index].nports);
2303 switch (udev->speed) {
2304 case USB_SPEED_SUPER:
2305 switch (sc->sc_hw.devs[index].state) {
2306 case XHCI_ST_ADDRESSED:
2307 case XHCI_ST_CONFIGURED:
2308 /* enable power save */
2309 temp |= XHCI_SCTX_1_MAX_EL_SET(sc->sc_exit_lat_max);
2312 /* disable power save */
2320 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx1, temp);
2322 temp = XHCI_SCTX_2_IRQ_TARGET_SET(0);
2325 temp |= XHCI_SCTX_2_TT_THINK_TIME_SET(sc->sc_hw.devs[index].tt);
2327 hubdev = udev->parent_hs_hub;
2329 /* check if we should activate the transaction translator */
2330 switch (udev->speed) {
2331 case USB_SPEED_FULL:
2333 if (hubdev != NULL) {
2334 temp |= XHCI_SCTX_2_TT_HUB_SID_SET(
2335 hubdev->controller_slot_id);
2336 temp |= XHCI_SCTX_2_TT_PORT_NUM_SET(
2344 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx2, temp);
2346 temp = XHCI_SCTX_3_DEV_ADDR_SET(udev->address) |
2347 XHCI_SCTX_3_SLOT_STATE_SET(0);
2349 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx3, temp);
2352 xhci_dump_device(sc, &pinp->ctx_slot);
2354 usb_pc_cpu_flush(pcinp);
2356 return (0); /* success */
2360 xhci_alloc_device_ext(struct usb_device *udev)
2362 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2363 struct usb_page_search buf_dev;
2364 struct usb_page_search buf_ep;
2365 struct xhci_trb *trb;
2366 struct usb_page_cache *pc;
2367 struct usb_page *pg;
2372 index = udev->controller_slot_id;
2374 pc = &sc->sc_hw.devs[index].device_pc;
2375 pg = &sc->sc_hw.devs[index].device_pg;
2377 /* need to initialize the page cache */
2378 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2380 if (usb_pc_alloc_mem(pc, pg, sc->sc_ctx_is_64_byte ?
2381 (2 * sizeof(struct xhci_dev_ctx)) :
2382 sizeof(struct xhci_dev_ctx), XHCI_PAGE_SIZE))
2385 usbd_get_page(pc, 0, &buf_dev);
2387 pc = &sc->sc_hw.devs[index].input_pc;
2388 pg = &sc->sc_hw.devs[index].input_pg;
2390 /* need to initialize the page cache */
2391 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2393 if (usb_pc_alloc_mem(pc, pg, sc->sc_ctx_is_64_byte ?
2394 (2 * sizeof(struct xhci_input_dev_ctx)) :
2395 sizeof(struct xhci_input_dev_ctx), XHCI_PAGE_SIZE))
2398 pc = &sc->sc_hw.devs[index].endpoint_pc;
2399 pg = &sc->sc_hw.devs[index].endpoint_pg;
2401 /* need to initialize the page cache */
2402 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2404 if (usb_pc_alloc_mem(pc, pg, sizeof(struct xhci_dev_endpoint_trbs), XHCI_PAGE_SIZE))
2407 /* initialise all endpoint LINK TRBs */
2409 for (i = 0; i != XHCI_MAX_ENDPOINTS; i++) {
2411 /* lookup endpoint TRB ring */
2412 usbd_get_page(pc, (uintptr_t)&((struct xhci_dev_endpoint_trbs *)0)->trb[i][0], &buf_ep);
2414 /* get TRB pointer */
2415 trb = buf_ep.buffer;
2416 trb += XHCI_MAX_TRANSFERS - 1;
2418 /* get TRB start address */
2419 addr = buf_ep.physaddr;
2421 /* create LINK TRB */
2422 trb->qwTrb0 = htole64(addr);
2423 trb->dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
2424 trb->dwTrb3 = htole32(XHCI_TRB_3_CYCLE_BIT |
2425 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
2428 usb_pc_cpu_flush(pc);
2430 xhci_set_slot_pointer(sc, index, buf_dev.physaddr);
2435 xhci_free_device_ext(udev);
2437 return (USB_ERR_NOMEM);
2441 xhci_free_device_ext(struct usb_device *udev)
2443 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2446 index = udev->controller_slot_id;
2447 xhci_set_slot_pointer(sc, index, 0);
2449 usb_pc_free_mem(&sc->sc_hw.devs[index].device_pc);
2450 usb_pc_free_mem(&sc->sc_hw.devs[index].input_pc);
2451 usb_pc_free_mem(&sc->sc_hw.devs[index].endpoint_pc);
2454 static struct xhci_endpoint_ext *
2455 xhci_get_endpoint_ext(struct usb_device *udev, struct usb_endpoint_descriptor *edesc)
2457 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2458 struct xhci_endpoint_ext *pepext;
2459 struct usb_page_cache *pc;
2460 struct usb_page_search buf_ep;
2464 epno = edesc->bEndpointAddress;
2465 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL)
2468 epno = XHCI_EPNO2EPID(epno);
2470 index = udev->controller_slot_id;
2472 pc = &sc->sc_hw.devs[index].endpoint_pc;
2474 usbd_get_page(pc, (uintptr_t)&((struct xhci_dev_endpoint_trbs *)0)->trb[epno][0], &buf_ep);
2476 pepext = &sc->sc_hw.devs[index].endp[epno];
2477 pepext->page_cache = pc;
2478 pepext->trb = buf_ep.buffer;
2479 pepext->physaddr = buf_ep.physaddr;
2485 xhci_endpoint_doorbell(struct usb_xfer *xfer)
2487 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
2491 epno = xfer->endpointno;
2492 if (xfer->flags_int.control_xfr)
2495 epno = XHCI_EPNO2EPID(epno);
2496 index = xfer->xroot->udev->controller_slot_id;
2498 if (xfer->xroot->udev->flags.self_suspended == 0)
2499 XWRITE4(sc, door, XHCI_DOORBELL(index), epno | XHCI_DB_SID_SET(0));
2503 xhci_transfer_remove(struct usb_xfer *xfer, usb_error_t error)
2505 struct xhci_endpoint_ext *pepext;
2507 if (xfer->flags_int.bandwidth_reclaimed) {
2508 xfer->flags_int.bandwidth_reclaimed = 0;
2510 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2511 xfer->endpoint->edesc);
2515 pepext->xfer[xfer->qh_pos] = NULL;
2517 if (error && pepext->trb_running != 0) {
2518 pepext->trb_halted = 1;
2519 pepext->trb_running = 0;
2525 xhci_transfer_insert(struct usb_xfer *xfer)
2527 struct xhci_td *td_first;
2528 struct xhci_td *td_last;
2529 struct xhci_endpoint_ext *pepext;
2537 /* check if already inserted */
2538 if (xfer->flags_int.bandwidth_reclaimed) {
2539 DPRINTFN(8, "Already in schedule\n");
2543 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2544 xfer->endpoint->edesc);
2546 td_first = xfer->td_transfer_first;
2547 td_last = xfer->td_transfer_last;
2548 addr = pepext->physaddr;
2550 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
2553 /* single buffered */
2557 /* multi buffered */
2558 trb_limit = (XHCI_MAX_TRANSFERS - 2);
2562 if (pepext->trb_used >= trb_limit) {
2563 DPRINTFN(8, "Too many TDs queued.\n");
2564 return (USB_ERR_NOMEM);
2567 /* check for stopped condition, after putting transfer on interrupt queue */
2568 if (pepext->trb_running == 0) {
2569 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
2571 DPRINTFN(8, "Not running\n");
2573 /* start configuration */
2574 (void)usb_proc_msignal(&sc->sc_config_proc,
2575 &sc->sc_config_msg[0], &sc->sc_config_msg[1]);
2581 /* get current TRB index */
2582 i = pepext->trb_index;
2584 /* get next TRB index */
2587 /* the last entry of the ring is a hardcoded link TRB */
2588 if (inext >= (XHCI_MAX_TRANSFERS - 1))
2591 /* compute terminating return address */
2592 addr += inext * sizeof(struct xhci_trb);
2594 /* update next pointer of last link TRB */
2595 td_last->td_trb[td_last->ntrb].qwTrb0 = htole64(addr);
2596 td_last->td_trb[td_last->ntrb].dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
2597 td_last->td_trb[td_last->ntrb].dwTrb3 = htole32(XHCI_TRB_3_IOC_BIT |
2598 XHCI_TRB_3_CYCLE_BIT | XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
2601 xhci_dump_trb(&td_last->td_trb[td_last->ntrb]);
2603 usb_pc_cpu_flush(td_last->page_cache);
2605 /* write ahead chain end marker */
2607 pepext->trb[inext].qwTrb0 = 0;
2608 pepext->trb[inext].dwTrb2 = 0;
2609 pepext->trb[inext].dwTrb3 = 0;
2611 /* update next pointer of link TRB */
2613 pepext->trb[i].qwTrb0 = htole64((uint64_t)td_first->td_self);
2614 pepext->trb[i].dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
2617 xhci_dump_trb(&pepext->trb[i]);
2619 usb_pc_cpu_flush(pepext->page_cache);
2621 /* toggle cycle bit which activates the transfer chain */
2623 pepext->trb[i].dwTrb3 = htole32(XHCI_TRB_3_CYCLE_BIT |
2624 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
2626 usb_pc_cpu_flush(pepext->page_cache);
2628 DPRINTF("qh_pos = %u\n", i);
2630 pepext->xfer[i] = xfer;
2634 xfer->flags_int.bandwidth_reclaimed = 1;
2636 pepext->trb_index = inext;
2638 xhci_endpoint_doorbell(xfer);
2644 xhci_root_intr(struct xhci_softc *sc)
2648 USB_BUS_LOCK_ASSERT(&sc->sc_bus);
2650 /* clear any old interrupt data */
2651 memset(sc->sc_hub_idata, 0, sizeof(sc->sc_hub_idata));
2653 for (i = 1; i <= sc->sc_noport; i++) {
2654 /* pick out CHANGE bits from the status register */
2655 if (XREAD4(sc, oper, XHCI_PORTSC(i)) & (
2656 XHCI_PS_CSC | XHCI_PS_PEC |
2657 XHCI_PS_OCC | XHCI_PS_WRC |
2658 XHCI_PS_PRC | XHCI_PS_PLC |
2660 sc->sc_hub_idata[i / 8] |= 1 << (i % 8);
2661 DPRINTF("port %d changed\n", i);
2664 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
2665 sizeof(sc->sc_hub_idata));
2668 /*------------------------------------------------------------------------*
2669 * xhci_device_done - XHCI done handler
2671 * NOTE: This function can be called two times in a row on
2672 * the same USB transfer. From close and from interrupt.
2673 *------------------------------------------------------------------------*/
2675 xhci_device_done(struct usb_xfer *xfer, usb_error_t error)
2677 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
2678 xfer, xfer->endpoint, error);
2680 /* remove transfer from HW queue */
2681 xhci_transfer_remove(xfer, error);
2683 /* dequeue transfer and start next transfer */
2684 usbd_transfer_done(xfer, error);
2687 /*------------------------------------------------------------------------*
2688 * XHCI data transfer support (generic type)
2689 *------------------------------------------------------------------------*/
2691 xhci_device_generic_open(struct usb_xfer *xfer)
2693 if (xfer->flags_int.isochronous_xfr) {
2694 switch (xfer->xroot->udev->speed) {
2695 case USB_SPEED_FULL:
2698 usb_hs_bandwidth_alloc(xfer);
2705 xhci_device_generic_close(struct usb_xfer *xfer)
2709 xhci_device_done(xfer, USB_ERR_CANCELLED);
2711 if (xfer->flags_int.isochronous_xfr) {
2712 switch (xfer->xroot->udev->speed) {
2713 case USB_SPEED_FULL:
2716 usb_hs_bandwidth_free(xfer);
2723 xhci_device_generic_multi_enter(struct usb_endpoint *ep,
2724 struct usb_xfer *enter_xfer)
2726 struct usb_xfer *xfer;
2728 /* check if there is a current transfer */
2729 xfer = ep->endpoint_q.curr;
2734 * Check if the current transfer is started and then pickup
2735 * the next one, if any. Else wait for next start event due to
2736 * block on failure feature.
2738 if (!xfer->flags_int.bandwidth_reclaimed)
2741 xfer = TAILQ_FIRST(&ep->endpoint_q.head);
2744 * In case of enter we have to consider that the
2745 * transfer is queued by the USB core after the enter
2754 /* try to multi buffer */
2755 xhci_transfer_insert(xfer);
2759 xhci_device_generic_enter(struct usb_xfer *xfer)
2763 /* setup TD's and QH */
2764 xhci_setup_generic_chain(xfer);
2766 xhci_device_generic_multi_enter(xfer->endpoint, xfer);
2770 xhci_device_generic_start(struct usb_xfer *xfer)
2774 /* try to insert xfer on HW queue */
2775 xhci_transfer_insert(xfer);
2777 /* try to multi buffer */
2778 xhci_device_generic_multi_enter(xfer->endpoint, NULL);
2780 /* add transfer last on interrupt queue */
2781 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
2783 /* start timeout, if any */
2784 if (xfer->timeout != 0)
2785 usbd_transfer_timeout_ms(xfer, &xhci_timeout, xfer->timeout);
2788 struct usb_pipe_methods xhci_device_generic_methods =
2790 .open = xhci_device_generic_open,
2791 .close = xhci_device_generic_close,
2792 .enter = xhci_device_generic_enter,
2793 .start = xhci_device_generic_start,
2796 /*------------------------------------------------------------------------*
2797 * xhci root HUB support
2798 *------------------------------------------------------------------------*
2799 * Simulate a hardware HUB by handling all the necessary requests.
2800 *------------------------------------------------------------------------*/
2802 #define HSETW(ptr, val) ptr[0] = (uint8_t)(val), ptr[1] = (uint8_t)((val) >> 8)
2805 struct usb_device_descriptor xhci_devd =
2807 .bLength = sizeof(xhci_devd),
2808 .bDescriptorType = UDESC_DEVICE, /* type */
2809 HSETW(.bcdUSB, 0x0300), /* USB version */
2810 .bDeviceClass = UDCLASS_HUB, /* class */
2811 .bDeviceSubClass = UDSUBCLASS_HUB, /* subclass */
2812 .bDeviceProtocol = UDPROTO_SSHUB, /* protocol */
2813 .bMaxPacketSize = 9, /* max packet size */
2814 HSETW(.idVendor, 0x0000), /* vendor */
2815 HSETW(.idProduct, 0x0000), /* product */
2816 HSETW(.bcdDevice, 0x0100), /* device version */
2820 .bNumConfigurations = 1, /* # of configurations */
2824 struct xhci_bos_desc xhci_bosd = {
2826 .bLength = sizeof(xhci_bosd.bosd),
2827 .bDescriptorType = UDESC_BOS,
2828 HSETW(.wTotalLength, sizeof(xhci_bosd)),
2829 .bNumDeviceCaps = 3,
2832 .bLength = sizeof(xhci_bosd.usb2extd),
2833 .bDescriptorType = 1,
2834 .bDevCapabilityType = 2,
2835 .bmAttributes[0] = 2,
2838 .bLength = sizeof(xhci_bosd.usbdcd),
2839 .bDescriptorType = UDESC_DEVICE_CAPABILITY,
2840 .bDevCapabilityType = 3,
2841 .bmAttributes = 0, /* XXX */
2842 HSETW(.wSpeedsSupported, 0x000C),
2843 .bFunctionalitySupport = 8,
2844 .bU1DevExitLat = 255, /* dummy - not used */
2845 .wU2DevExitLat[0] = 0x00,
2846 .wU2DevExitLat[1] = 0x08,
2849 .bLength = sizeof(xhci_bosd.cidd),
2850 .bDescriptorType = 1,
2851 .bDevCapabilityType = 4,
2853 .bContainerID = 0, /* XXX */
2858 struct xhci_config_desc xhci_confd = {
2860 .bLength = sizeof(xhci_confd.confd),
2861 .bDescriptorType = UDESC_CONFIG,
2862 .wTotalLength[0] = sizeof(xhci_confd),
2864 .bConfigurationValue = 1,
2865 .iConfiguration = 0,
2866 .bmAttributes = UC_SELF_POWERED,
2867 .bMaxPower = 0 /* max power */
2870 .bLength = sizeof(xhci_confd.ifcd),
2871 .bDescriptorType = UDESC_INTERFACE,
2873 .bInterfaceClass = UICLASS_HUB,
2874 .bInterfaceSubClass = UISUBCLASS_HUB,
2875 .bInterfaceProtocol = 0,
2878 .bLength = sizeof(xhci_confd.endpd),
2879 .bDescriptorType = UDESC_ENDPOINT,
2880 .bEndpointAddress = UE_DIR_IN | XHCI_INTR_ENDPT,
2881 .bmAttributes = UE_INTERRUPT,
2882 .wMaxPacketSize[0] = 2, /* max 15 ports */
2886 .bLength = sizeof(xhci_confd.endpcd),
2887 .bDescriptorType = UDESC_ENDPOINT_SS_COMP,
2894 struct usb_hub_ss_descriptor xhci_hubd = {
2895 .bLength = sizeof(xhci_hubd),
2896 .bDescriptorType = UDESC_SS_HUB,
2900 xhci_roothub_exec(struct usb_device *udev,
2901 struct usb_device_request *req, const void **pptr, uint16_t *plength)
2903 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2904 const char *str_ptr;
2915 USB_BUS_LOCK_ASSERT(&sc->sc_bus);
2918 ptr = (const void *)&sc->sc_hub_desc;
2922 value = UGETW(req->wValue);
2923 index = UGETW(req->wIndex);
2925 DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
2926 "wValue=0x%04x wIndex=0x%04x\n",
2927 req->bmRequestType, req->bRequest,
2928 UGETW(req->wLength), value, index);
2930 #define C(x,y) ((x) | ((y) << 8))
2931 switch (C(req->bRequest, req->bmRequestType)) {
2932 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2933 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2934 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2936 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2937 * for the integrated root hub.
2940 case C(UR_GET_CONFIG, UT_READ_DEVICE):
2942 sc->sc_hub_desc.temp[0] = sc->sc_conf;
2944 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2945 switch (value >> 8) {
2947 if ((value & 0xff) != 0) {
2948 err = USB_ERR_IOERROR;
2951 len = sizeof(xhci_devd);
2952 ptr = (const void *)&xhci_devd;
2956 if ((value & 0xff) != 0) {
2957 err = USB_ERR_IOERROR;
2960 len = sizeof(xhci_bosd);
2961 ptr = (const void *)&xhci_bosd;
2965 if ((value & 0xff) != 0) {
2966 err = USB_ERR_IOERROR;
2969 len = sizeof(xhci_confd);
2970 ptr = (const void *)&xhci_confd;
2974 switch (value & 0xff) {
2975 case 0: /* Language table */
2979 case 1: /* Vendor */
2980 str_ptr = sc->sc_vendor;
2983 case 2: /* Product */
2984 str_ptr = "XHCI root HUB";
2992 len = usb_make_str_desc(
2993 sc->sc_hub_desc.temp,
2994 sizeof(sc->sc_hub_desc.temp),
2999 err = USB_ERR_IOERROR;
3003 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3005 sc->sc_hub_desc.temp[0] = 0;
3007 case C(UR_GET_STATUS, UT_READ_DEVICE):
3009 USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
3011 case C(UR_GET_STATUS, UT_READ_INTERFACE):
3012 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3014 USETW(sc->sc_hub_desc.stat.wStatus, 0);
3016 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3017 if (value >= XHCI_MAX_DEVICES) {
3018 err = USB_ERR_IOERROR;
3022 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
3023 if (value != 0 && value != 1) {
3024 err = USB_ERR_IOERROR;
3027 sc->sc_conf = value;
3029 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
3031 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
3032 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
3033 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3034 err = USB_ERR_IOERROR;
3036 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
3038 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
3041 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3043 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3044 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE\n");
3047 (index > sc->sc_noport)) {
3048 err = USB_ERR_IOERROR;
3051 port = XHCI_PORTSC(index);
3053 v = XREAD4(sc, oper, port);
3054 i = XHCI_PS_PLS_GET(v);
3055 v &= ~XHCI_PS_CLEAR;
3058 case UHF_C_BH_PORT_RESET:
3059 XWRITE4(sc, oper, port, v | XHCI_PS_WRC);
3061 case UHF_C_PORT_CONFIG_ERROR:
3062 XWRITE4(sc, oper, port, v | XHCI_PS_CEC);
3064 case UHF_C_PORT_SUSPEND:
3065 case UHF_C_PORT_LINK_STATE:
3066 XWRITE4(sc, oper, port, v | XHCI_PS_PLC);
3068 case UHF_C_PORT_CONNECTION:
3069 XWRITE4(sc, oper, port, v | XHCI_PS_CSC);
3071 case UHF_C_PORT_ENABLE:
3072 XWRITE4(sc, oper, port, v | XHCI_PS_PEC);
3074 case UHF_C_PORT_OVER_CURRENT:
3075 XWRITE4(sc, oper, port, v | XHCI_PS_OCC);
3077 case UHF_C_PORT_RESET:
3078 XWRITE4(sc, oper, port, v | XHCI_PS_PRC);
3080 case UHF_PORT_ENABLE:
3081 XWRITE4(sc, oper, port, v | XHCI_PS_PED);
3083 case UHF_PORT_POWER:
3084 XWRITE4(sc, oper, port, v & ~XHCI_PS_PP);
3086 case UHF_PORT_INDICATOR:
3087 XWRITE4(sc, oper, port, v & ~XHCI_PS_PIC_SET(3));
3089 case UHF_PORT_SUSPEND:
3093 XWRITE4(sc, oper, port, v |
3094 XHCI_PS_PLS_SET(0xF) | XHCI_PS_LWS);
3097 /* wait 20ms for resume sequence to complete */
3098 usb_pause_mtx(&sc->sc_bus.bus_lock, hz / 50);
3101 XWRITE4(sc, oper, port, v |
3102 XHCI_PS_PLS_SET(0) | XHCI_PS_LWS);
3105 err = USB_ERR_IOERROR;
3110 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3111 if ((value & 0xff) != 0) {
3112 err = USB_ERR_IOERROR;
3116 v = XREAD4(sc, capa, XHCI_HCSPARAMS0);
3118 sc->sc_hub_desc.hubd = xhci_hubd;
3120 sc->sc_hub_desc.hubd.bNbrPorts = sc->sc_noport;
3122 if (XHCI_HCS0_PPC(v))
3123 i = UHD_PWR_INDIVIDUAL;
3127 if (XHCI_HCS0_PIND(v))
3130 i |= UHD_OC_INDIVIDUAL;
3132 USETW(sc->sc_hub_desc.hubd.wHubCharacteristics, i);
3134 /* see XHCI section 5.4.9: */
3135 sc->sc_hub_desc.hubd.bPwrOn2PwrGood = 10;
3137 for (j = 1; j <= sc->sc_noport; j++) {
3139 v = XREAD4(sc, oper, XHCI_PORTSC(j));
3140 if (v & XHCI_PS_DR) {
3141 sc->sc_hub_desc.hubd.
3142 DeviceRemovable[j / 8] |= 1U << (j % 8);
3145 len = sc->sc_hub_desc.hubd.bLength;
3148 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3150 memset(sc->sc_hub_desc.temp, 0, 16);
3153 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3154 DPRINTFN(9, "UR_GET_STATUS i=%d\n", index);
3157 (index > sc->sc_noport)) {
3158 err = USB_ERR_IOERROR;
3162 v = XREAD4(sc, oper, XHCI_PORTSC(index));
3164 DPRINTFN(9, "port status=0x%08x\n", v);
3166 i = UPS_PORT_LINK_STATE_SET(XHCI_PS_PLS_GET(v));
3168 switch (XHCI_PS_SPEED_GET(v)) {
3170 i |= UPS_HIGH_SPEED;
3179 i |= UPS_OTHER_SPEED;
3183 if (v & XHCI_PS_CCS)
3184 i |= UPS_CURRENT_CONNECT_STATUS;
3185 if (v & XHCI_PS_PED)
3186 i |= UPS_PORT_ENABLED;
3187 if (v & XHCI_PS_OCA)
3188 i |= UPS_OVERCURRENT_INDICATOR;
3191 if (v & XHCI_PS_PP) {
3193 * The USB 3.0 RH is using the
3194 * USB 2.0's power bit
3196 i |= UPS_PORT_POWER;
3198 USETW(sc->sc_hub_desc.ps.wPortStatus, i);
3201 if (v & XHCI_PS_CSC)
3202 i |= UPS_C_CONNECT_STATUS;
3203 if (v & XHCI_PS_PEC)
3204 i |= UPS_C_PORT_ENABLED;
3205 if (v & XHCI_PS_OCC)
3206 i |= UPS_C_OVERCURRENT_INDICATOR;
3207 if (v & XHCI_PS_WRC)
3208 i |= UPS_C_BH_PORT_RESET;
3209 if (v & XHCI_PS_PRC)
3210 i |= UPS_C_PORT_RESET;
3211 if (v & XHCI_PS_PLC)
3212 i |= UPS_C_PORT_LINK_STATE;
3213 if (v & XHCI_PS_CEC)
3214 i |= UPS_C_PORT_CONFIG_ERROR;
3216 USETW(sc->sc_hub_desc.ps.wPortChange, i);
3217 len = sizeof(sc->sc_hub_desc.ps);
3220 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3221 err = USB_ERR_IOERROR;
3224 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3227 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3233 (index > sc->sc_noport)) {
3234 err = USB_ERR_IOERROR;
3238 port = XHCI_PORTSC(index);
3239 v = XREAD4(sc, oper, port) & ~XHCI_PS_CLEAR;
3242 case UHF_PORT_U1_TIMEOUT:
3243 if (XHCI_PS_SPEED_GET(v) != 4) {
3244 err = USB_ERR_IOERROR;
3247 port = XHCI_PORTPMSC(index);
3248 v = XREAD4(sc, oper, port);
3249 v &= ~XHCI_PM3_U1TO_SET(0xFF);
3250 v |= XHCI_PM3_U1TO_SET(i);
3251 XWRITE4(sc, oper, port, v);
3253 case UHF_PORT_U2_TIMEOUT:
3254 if (XHCI_PS_SPEED_GET(v) != 4) {
3255 err = USB_ERR_IOERROR;
3258 port = XHCI_PORTPMSC(index);
3259 v = XREAD4(sc, oper, port);
3260 v &= ~XHCI_PM3_U2TO_SET(0xFF);
3261 v |= XHCI_PM3_U2TO_SET(i);
3262 XWRITE4(sc, oper, port, v);
3264 case UHF_BH_PORT_RESET:
3265 XWRITE4(sc, oper, port, v | XHCI_PS_WPR);
3267 case UHF_PORT_LINK_STATE:
3268 XWRITE4(sc, oper, port, v |
3269 XHCI_PS_PLS_SET(i) | XHCI_PS_LWS);
3270 /* 4ms settle time */
3271 usb_pause_mtx(&sc->sc_bus.bus_lock, hz / 250);
3273 case UHF_PORT_ENABLE:
3274 DPRINTFN(3, "set port enable %d\n", index);
3276 case UHF_PORT_SUSPEND:
3277 DPRINTFN(6, "suspend port %u (LPM=%u)\n", index, i);
3278 j = XHCI_PS_SPEED_GET(v);
3279 if ((j < 1) || (j > 3)) {
3280 /* non-supported speed */
3281 err = USB_ERR_IOERROR;
3284 XWRITE4(sc, oper, port, v |
3285 XHCI_PS_PLS_SET(i ? 2 /* LPM */ : 3) | XHCI_PS_LWS);
3287 case UHF_PORT_RESET:
3288 DPRINTFN(6, "reset port %d\n", index);
3289 XWRITE4(sc, oper, port, v | XHCI_PS_PR);
3291 case UHF_PORT_POWER:
3292 DPRINTFN(3, "set port power %d\n", index);
3293 XWRITE4(sc, oper, port, v | XHCI_PS_PP);
3296 DPRINTFN(3, "set port test %d\n", index);
3298 case UHF_PORT_INDICATOR:
3299 DPRINTFN(3, "set port indicator %d\n", index);
3301 v &= ~XHCI_PS_PIC_SET(3);
3302 v |= XHCI_PS_PIC_SET(1);
3304 XWRITE4(sc, oper, port, v);
3307 err = USB_ERR_IOERROR;
3312 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
3313 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
3314 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
3315 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
3318 err = USB_ERR_IOERROR;
3328 xhci_xfer_setup(struct usb_setup_params *parm)
3330 struct usb_page_search page_info;
3331 struct usb_page_cache *pc;
3332 struct xhci_softc *sc;
3333 struct usb_xfer *xfer;
3338 sc = XHCI_BUS2SC(parm->udev->bus);
3339 xfer = parm->curr_xfer;
3342 * The proof for the "ntd" formula is illustrated like this:
3344 * +------------------------------------+
3348 * | | xxx | x | frm 0 |
3350 * | | xxx | xx | frm 1 |
3353 * +------------------------------------+
3355 * "xxx" means a completely full USB transfer descriptor
3357 * "x" and "xx" means a short USB packet
3359 * For the remainder of an USB transfer modulo
3360 * "max_data_length" we need two USB transfer descriptors.
3361 * One to transfer the remaining data and one to finalise with
3362 * a zero length packet in case the "force_short_xfer" flag is
3363 * set. We only need two USB transfer descriptors in the case
3364 * where the transfer length of the first one is a factor of
3365 * "max_frame_size". The rest of the needed USB transfer
3366 * descriptors is given by the buffer size divided by the
3367 * maximum data payload.
3369 parm->hc_max_packet_size = 0x400;
3370 parm->hc_max_packet_count = 16 * 3;
3371 parm->hc_max_frame_size = XHCI_TD_PAYLOAD_MAX;
3373 xfer->flags_int.bdma_enable = 1;
3375 usbd_transfer_setup_sub(parm);
3377 if (xfer->flags_int.isochronous_xfr) {
3378 ntd = ((1 * xfer->nframes)
3379 + (xfer->max_data_length / xfer->max_hc_frame_size));
3380 } else if (xfer->flags_int.control_xfr) {
3381 ntd = ((2 * xfer->nframes) + 1 /* STATUS */
3382 + (xfer->max_data_length / xfer->max_hc_frame_size));
3384 ntd = ((2 * xfer->nframes)
3385 + (xfer->max_data_length / xfer->max_hc_frame_size));
3394 * Allocate queue heads and transfer descriptors
3398 if (usbd_transfer_setup_sub_malloc(
3399 parm, &pc, sizeof(struct xhci_td),
3400 XHCI_TD_ALIGN, ntd)) {
3401 parm->err = USB_ERR_NOMEM;
3405 for (n = 0; n != ntd; n++) {
3408 usbd_get_page(pc + n, 0, &page_info);
3410 td = page_info.buffer;
3413 td->td_self = page_info.physaddr;
3414 td->obj_next = last_obj;
3415 td->page_cache = pc + n;
3419 usb_pc_cpu_flush(pc + n);
3422 xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
3424 if (!xfer->flags_int.curr_dma_set) {
3425 xfer->flags_int.curr_dma_set = 1;
3431 xhci_configure_reset_endpoint(struct usb_xfer *xfer)
3433 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
3434 struct usb_page_search buf_inp;
3435 struct usb_device *udev;
3436 struct xhci_endpoint_ext *pepext;
3437 struct usb_endpoint_descriptor *edesc;
3438 struct usb_page_cache *pcinp;
3443 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
3444 xfer->endpoint->edesc);
3446 udev = xfer->xroot->udev;
3447 index = udev->controller_slot_id;
3449 pcinp = &sc->sc_hw.devs[index].input_pc;
3451 usbd_get_page(pcinp, 0, &buf_inp);
3453 edesc = xfer->endpoint->edesc;
3455 epno = edesc->bEndpointAddress;
3457 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL)
3460 epno = XHCI_EPNO2EPID(epno);
3463 return (USB_ERR_NO_PIPE); /* invalid */
3467 /* configure endpoint */
3469 err = xhci_configure_endpoint_by_xfer(xfer);
3472 XHCI_CMD_UNLOCK(sc);
3477 * Get the endpoint into the stopped state according to the
3478 * endpoint context state diagram in the XHCI specification:
3481 err = xhci_cmd_stop_ep(sc, 0, epno, index);
3484 DPRINTF("Could not stop endpoint %u\n", epno);
3486 err = xhci_cmd_reset_ep(sc, 0, epno, index);
3489 DPRINTF("Could not reset endpoint %u\n", epno);
3491 err = xhci_cmd_set_tr_dequeue_ptr(sc, pepext->physaddr |
3492 XHCI_EPCTX_2_DCS_SET(1), 0, epno, index);
3495 DPRINTF("Could not set dequeue ptr for endpoint %u\n", epno);
3498 * Get the endpoint into the running state according to the
3499 * endpoint context state diagram in the XHCI specification:
3502 xhci_configure_mask(udev, 1U << epno, 0);
3504 err = xhci_cmd_evaluate_ctx(sc, buf_inp.physaddr, index);
3507 DPRINTF("Could not configure endpoint %u\n", epno);
3509 err = xhci_cmd_configure_ep(sc, buf_inp.physaddr, 0, index);
3512 DPRINTF("Could not configure endpoint %u\n", epno);
3514 XHCI_CMD_UNLOCK(sc);
3520 xhci_xfer_unsetup(struct usb_xfer *xfer)
3526 xhci_start_dma_delay(struct usb_xfer *xfer)
3528 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
3530 /* put transfer on interrupt queue (again) */
3531 usbd_transfer_enqueue(&sc->sc_bus.intr_q, xfer);
3533 (void)usb_proc_msignal(&sc->sc_config_proc,
3534 &sc->sc_config_msg[0], &sc->sc_config_msg[1]);
3538 xhci_configure_msg(struct usb_proc_msg *pm)
3540 struct xhci_softc *sc;
3541 struct xhci_endpoint_ext *pepext;
3542 struct usb_xfer *xfer;
3544 sc = XHCI_BUS2SC(((struct usb_bus_msg *)pm)->bus);
3547 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3549 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
3550 xfer->endpoint->edesc);
3552 if ((pepext->trb_halted != 0) ||
3553 (pepext->trb_running == 0)) {
3557 /* clear halted and running */
3558 pepext->trb_halted = 0;
3559 pepext->trb_running = 0;
3561 /* nuke remaining buffered transfers */
3563 for (i = 0; i != (XHCI_MAX_TRANSFERS - 1); i++) {
3565 * NOTE: We need to use the timeout
3566 * error code here else existing
3567 * isochronous clients can get
3570 if (pepext->xfer[i] != NULL) {
3571 xhci_device_done(pepext->xfer[i],
3577 * NOTE: The USB transfer cannot vanish in
3581 USB_BUS_UNLOCK(&sc->sc_bus);
3583 xhci_configure_reset_endpoint(xfer);
3585 USB_BUS_LOCK(&sc->sc_bus);
3587 /* check if halted is still cleared */
3588 if (pepext->trb_halted == 0) {
3589 pepext->trb_running = 1;
3590 pepext->trb_index = 0;
3595 if (xfer->flags_int.did_dma_delay) {
3597 /* remove transfer from interrupt queue (again) */
3598 usbd_transfer_dequeue(xfer);
3600 /* we are finally done */
3601 usb_dma_delay_done_cb(xfer);
3603 /* queue changed - restart */
3608 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3610 /* try to insert xfer on HW queue */
3611 xhci_transfer_insert(xfer);
3613 /* try to multi buffer */
3614 xhci_device_generic_multi_enter(xfer->endpoint, NULL);
3619 xhci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
3620 struct usb_endpoint *ep)
3622 struct xhci_endpoint_ext *pepext;
3624 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d\n",
3625 ep, udev->address, edesc->bEndpointAddress, udev->flags.usb_mode);
3627 if (udev->flags.usb_mode != USB_MODE_HOST) {
3631 if (udev->parent_hub == NULL) {
3632 /* root HUB has special endpoint handling */
3636 ep->methods = &xhci_device_generic_methods;
3638 pepext = xhci_get_endpoint_ext(udev, edesc);
3640 USB_BUS_LOCK(udev->bus);
3641 pepext->trb_halted = 1;
3642 pepext->trb_running = 0;
3643 USB_BUS_UNLOCK(udev->bus);
3647 xhci_ep_uninit(struct usb_device *udev, struct usb_endpoint *ep)
3653 xhci_ep_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
3655 struct xhci_endpoint_ext *pepext;
3659 if (udev->flags.usb_mode != USB_MODE_HOST) {
3663 if (udev->parent_hub == NULL) {
3664 /* root HUB has special endpoint handling */
3668 pepext = xhci_get_endpoint_ext(udev, ep->edesc);
3670 USB_BUS_LOCK(udev->bus);
3671 pepext->trb_halted = 1;
3672 pepext->trb_running = 0;
3673 USB_BUS_UNLOCK(udev->bus);
3677 xhci_device_init(struct usb_device *udev)
3679 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3683 /* no init for root HUB */
3684 if (udev->parent_hub == NULL)
3689 /* set invalid default */
3691 udev->controller_slot_id = sc->sc_noslot + 1;
3693 /* try to get a new slot ID from the XHCI */
3695 err = xhci_cmd_enable_slot(sc, &temp);
3698 XHCI_CMD_UNLOCK(sc);
3702 if (temp > sc->sc_noslot) {
3703 XHCI_CMD_UNLOCK(sc);
3704 return (USB_ERR_BAD_ADDRESS);
3707 if (sc->sc_hw.devs[temp].state != XHCI_ST_DISABLED) {
3708 DPRINTF("slot %u already allocated.\n", temp);
3709 XHCI_CMD_UNLOCK(sc);
3710 return (USB_ERR_BAD_ADDRESS);
3713 /* store slot ID for later reference */
3715 udev->controller_slot_id = temp;
3717 /* reset data structure */
3719 memset(&sc->sc_hw.devs[temp], 0, sizeof(sc->sc_hw.devs[0]));
3721 /* set mark slot allocated */
3723 sc->sc_hw.devs[temp].state = XHCI_ST_ENABLED;
3725 err = xhci_alloc_device_ext(udev);
3727 XHCI_CMD_UNLOCK(sc);
3729 /* get device into default state */
3732 err = xhci_set_address(udev, NULL, 0);
3738 xhci_device_uninit(struct usb_device *udev)
3740 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3743 /* no init for root HUB */
3744 if (udev->parent_hub == NULL)
3749 index = udev->controller_slot_id;
3751 if (index <= sc->sc_noslot) {
3752 xhci_cmd_disable_slot(sc, index);
3753 sc->sc_hw.devs[index].state = XHCI_ST_DISABLED;
3755 /* free device extension */
3756 xhci_free_device_ext(udev);
3759 XHCI_CMD_UNLOCK(sc);
3763 xhci_get_dma_delay(struct usb_device *udev, uint32_t *pus)
3766 * Wait until the hardware has finished any possible use of
3767 * the transfer descriptor(s)
3769 *pus = 2048; /* microseconds */
3773 xhci_device_resume(struct usb_device *udev)
3775 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3781 /* check for root HUB */
3782 if (udev->parent_hub == NULL)
3785 index = udev->controller_slot_id;
3789 /* blindly resume all endpoints */
3791 USB_BUS_LOCK(udev->bus);
3793 for (n = 1; n != XHCI_MAX_ENDPOINTS; n++)
3794 XWRITE4(sc, door, XHCI_DOORBELL(index), n | XHCI_DB_SID_SET(0));
3796 USB_BUS_UNLOCK(udev->bus);
3798 XHCI_CMD_UNLOCK(sc);
3802 xhci_device_suspend(struct usb_device *udev)
3804 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3811 /* check for root HUB */
3812 if (udev->parent_hub == NULL)
3815 index = udev->controller_slot_id;
3819 /* blindly suspend all endpoints */
3821 for (n = 1; n != XHCI_MAX_ENDPOINTS; n++) {
3822 err = xhci_cmd_stop_ep(sc, 1, n, index);
3824 DPRINTF("Failed to suspend endpoint "
3825 "%u on slot %u (ignored).\n", n, index);
3829 XHCI_CMD_UNLOCK(sc);
3833 xhci_set_hw_power(struct usb_bus *bus)
3839 xhci_device_state_change(struct usb_device *udev)
3841 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3842 struct usb_page_search buf_inp;
3846 /* check for root HUB */
3847 if (udev->parent_hub == NULL)
3850 index = udev->controller_slot_id;
3854 if (usb_get_device_state(udev) == USB_STATE_CONFIGURED) {
3855 err = uhub_query_info(udev, &sc->sc_hw.devs[index].nports,
3856 &sc->sc_hw.devs[index].tt);
3858 sc->sc_hw.devs[index].nports = 0;
3863 switch (usb_get_device_state(udev)) {
3864 case USB_STATE_POWERED:
3865 if (sc->sc_hw.devs[index].state == XHCI_ST_DEFAULT)
3868 sc->sc_hw.devs[index].state = XHCI_ST_DEFAULT;
3870 err = xhci_cmd_reset_dev(sc, index);
3873 DPRINTF("Device reset failed "
3874 "for slot %u.\n", index);
3878 case USB_STATE_ADDRESSED:
3879 if (sc->sc_hw.devs[index].state == XHCI_ST_ADDRESSED)
3882 sc->sc_hw.devs[index].state = XHCI_ST_ADDRESSED;
3884 err = xhci_cmd_configure_ep(sc, 0, 1, index);
3887 DPRINTF("Failed to deconfigure "
3888 "slot %u.\n", index);
3892 case USB_STATE_CONFIGURED:
3893 if (sc->sc_hw.devs[index].state == XHCI_ST_CONFIGURED)
3896 sc->sc_hw.devs[index].state = XHCI_ST_CONFIGURED;
3898 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
3900 xhci_configure_mask(udev, 1, 0);
3902 err = xhci_configure_device(udev);
3904 DPRINTF("Could not configure device "
3905 "at slot %u.\n", index);
3908 err = xhci_cmd_evaluate_ctx(sc, buf_inp.physaddr, index);
3910 DPRINTF("Could not evaluate device "
3911 "context at slot %u.\n", index);
3918 XHCI_CMD_UNLOCK(sc);
3921 struct usb_bus_methods xhci_bus_methods = {
3922 .endpoint_init = xhci_ep_init,
3923 .endpoint_uninit = xhci_ep_uninit,
3924 .xfer_setup = xhci_xfer_setup,
3925 .xfer_unsetup = xhci_xfer_unsetup,
3926 .get_dma_delay = xhci_get_dma_delay,
3927 .device_init = xhci_device_init,
3928 .device_uninit = xhci_device_uninit,
3929 .device_resume = xhci_device_resume,
3930 .device_suspend = xhci_device_suspend,
3931 .set_hw_power = xhci_set_hw_power,
3932 .roothub_exec = xhci_roothub_exec,
3933 .xfer_poll = xhci_do_poll,
3934 .start_dma_delay = xhci_start_dma_delay,
3935 .set_address = xhci_set_address,
3936 .clear_stall = xhci_ep_clear_stall,
3937 .device_state_change = xhci_device_state_change,
3938 .set_hw_power_sleep = xhci_set_hw_power_sleep,