2 * Copyright (c) 2010 Hans Petter Selasky. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * USB eXtensible Host Controller Interface, a.k.a. USB 3.0 controller.
29 * The XHCI 1.0 spec can be found at
30 * http://www.intel.com/technology/usb/download/xHCI_Specification_for_USB.pdf
31 * and the USB 3.0 spec at
32 * http://www.usb.org/developers/docs/usb_30_spec_060910.zip
36 * A few words about the design implementation: This driver emulates
37 * the concept about TDs which is found in EHCI specification. This
38 * way we avoid too much diveration among USB drivers.
41 #include <sys/cdefs.h>
43 #include <sys/stdint.h>
44 #include <sys/param.h>
45 #include <sys/queue.h>
46 #include <sys/types.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
50 #include <sys/module.h>
52 #include <sys/condvar.h>
53 #include <sys/sysctl.h>
54 #include <sys/unistd.h>
55 #include <sys/callout.h>
56 #include <sys/malloc.h>
59 #include <bus/u4b/usb.h>
60 #include <bus/u4b/usbdi.h>
62 #define USB_DEBUG_VAR xhcidebug
64 #include <bus/u4b/usb_core.h>
65 #include <bus/u4b/usb_debug.h>
66 #include <bus/u4b/usb_busdma.h>
67 #include <bus/u4b/usb_process.h>
68 #include <bus/u4b/usb_transfer.h>
69 #include <bus/u4b/usb_device.h>
70 #include <bus/u4b/usb_hub.h>
71 #include <bus/u4b/usb_util.h>
73 #include <bus/u4b/usb_controller.h>
74 #include <bus/u4b/usb_bus.h>
75 #include <bus/u4b/controller/xhci.h>
76 #include <bus/u4b/controller/xhcireg.h>
78 #define XHCI_BUS2SC(bus) \
79 ((struct xhci_softc *)(((uint8_t *)(bus)) - \
80 ((uint8_t *)&(((struct xhci_softc *)0)->sc_bus))))
83 static int xhcidebug = 0;
85 static SYSCTL_NODE(_hw_usb, OID_AUTO, xhci, CTLFLAG_RW, 0, "USB XHCI");
86 SYSCTL_INT(_hw_usb_xhci, OID_AUTO, debug, CTLFLAG_RW,
87 &xhcidebug, 0, "Debug level");
89 TUNABLE_INT("hw.usb.xhci.debug", &xhcidebug);
93 #define XHCI_INTR_ENDPT 1
95 struct xhci_std_temp {
96 struct xhci_softc *sc;
97 struct usb_page_cache *pc;
99 struct xhci_td *td_next;
102 uint32_t max_packet_size;
116 static void xhci_do_poll(struct usb_bus *);
117 static void xhci_device_done(struct usb_xfer *, usb_error_t);
118 static void xhci_root_intr(struct xhci_softc *);
119 static void xhci_free_device_ext(struct usb_device *);
120 static struct xhci_endpoint_ext *xhci_get_endpoint_ext(struct usb_device *,
121 struct usb_endpoint_descriptor *);
122 static usb_proc_callback_t xhci_configure_msg;
123 static usb_error_t xhci_configure_device(struct usb_device *);
124 static usb_error_t xhci_configure_endpoint(struct usb_device *,
125 struct usb_endpoint_descriptor *, uint64_t, uint16_t,
126 uint8_t, uint8_t, uint8_t, uint16_t, uint16_t);
127 static usb_error_t xhci_configure_mask(struct usb_device *,
129 static usb_error_t xhci_cmd_evaluate_ctx(struct xhci_softc *,
131 static void xhci_endpoint_doorbell(struct usb_xfer *);
132 static void xhci_ctx_set_le32(struct xhci_softc *sc, volatile uint32_t *ptr, uint32_t val);
133 static uint32_t xhci_ctx_get_le32(struct xhci_softc *sc, volatile uint32_t *ptr);
134 static void xhci_ctx_set_le64(struct xhci_softc *sc, volatile uint64_t *ptr, uint64_t val);
136 static uint64_t xhci_ctx_get_le64(struct xhci_softc *sc, volatile uint64_t *ptr);
139 extern struct usb_bus_methods xhci_bus_methods;
143 xhci_dump_trb(struct xhci_trb *trb)
145 DPRINTFN(5, "trb = %p\n", trb);
146 DPRINTFN(5, "qwTrb0 = 0x%016llx\n", (long long)le64toh(trb->qwTrb0));
147 DPRINTFN(5, "dwTrb2 = 0x%08x\n", le32toh(trb->dwTrb2));
148 DPRINTFN(5, "dwTrb3 = 0x%08x\n", le32toh(trb->dwTrb3));
152 xhci_dump_endpoint(struct xhci_softc *sc, struct xhci_endp_ctx *pep)
154 DPRINTFN(5, "pep = %p\n", pep);
155 DPRINTFN(5, "dwEpCtx0=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx0));
156 DPRINTFN(5, "dwEpCtx1=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx1));
157 DPRINTFN(5, "qwEpCtx2=0x%016llx\n", (long long)xhci_ctx_get_le64(sc, &pep->qwEpCtx2));
158 DPRINTFN(5, "dwEpCtx4=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx4));
159 DPRINTFN(5, "dwEpCtx5=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx5));
160 DPRINTFN(5, "dwEpCtx6=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx6));
161 DPRINTFN(5, "dwEpCtx7=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx7));
165 xhci_dump_device(struct xhci_softc *sc, struct xhci_slot_ctx *psl)
167 DPRINTFN(5, "psl = %p\n", psl);
168 DPRINTFN(5, "dwSctx0=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx0));
169 DPRINTFN(5, "dwSctx1=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx1));
170 DPRINTFN(5, "dwSctx2=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx2));
171 DPRINTFN(5, "dwSctx3=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx3));
176 xhci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
178 struct xhci_softc *sc = XHCI_BUS2SC(bus);
181 cb(bus, &sc->sc_hw.root_pc, &sc->sc_hw.root_pg,
182 sizeof(struct xhci_hw_root), XHCI_PAGE_SIZE);
184 cb(bus, &sc->sc_hw.ctx_pc, &sc->sc_hw.ctx_pg,
185 sizeof(struct xhci_dev_ctx_addr), XHCI_PAGE_SIZE);
187 for (i = 0; i != XHCI_MAX_SCRATCHPADS; i++) {
188 cb(bus, &sc->sc_hw.scratch_pc[i], &sc->sc_hw.scratch_pg[i],
189 XHCI_PAGE_SIZE, XHCI_PAGE_SIZE);
194 xhci_ctx_set_le32(struct xhci_softc *sc, volatile uint32_t *ptr, uint32_t val)
196 if (sc->sc_ctx_is_64_byte) {
198 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
199 /* all contexts are initially 32-bytes */
200 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
201 ptr = (volatile uint32_t *)(((volatile uint8_t *)ptr) + offset);
207 xhci_ctx_get_le32(struct xhci_softc *sc, volatile uint32_t *ptr)
209 if (sc->sc_ctx_is_64_byte) {
211 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
212 /* all contexts are initially 32-bytes */
213 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
214 ptr = (volatile uint32_t *)(((volatile uint8_t *)ptr) + offset);
216 return (le32toh(*ptr));
220 xhci_ctx_set_le64(struct xhci_softc *sc, volatile uint64_t *ptr, uint64_t val)
222 if (sc->sc_ctx_is_64_byte) {
224 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
225 /* all contexts are initially 32-bytes */
226 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
227 ptr = (volatile uint64_t *)(((volatile uint8_t *)ptr) + offset);
234 xhci_ctx_get_le64(struct xhci_softc *sc, volatile uint64_t *ptr)
236 if (sc->sc_ctx_is_64_byte) {
238 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
239 /* all contexts are initially 32-bytes */
240 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
241 ptr = (volatile uint64_t *)(((volatile uint8_t *)ptr) + offset);
243 return (le64toh(*ptr));
248 xhci_start_controller(struct xhci_softc *sc)
250 struct usb_page_search buf_res;
251 struct xhci_hw_root *phwr;
252 struct xhci_dev_ctx_addr *pdctxa;
260 sc->sc_oper_off = XREAD1(sc, capa, XHCI_CAPLENGTH);
261 sc->sc_runt_off = XREAD4(sc, capa, XHCI_RTSOFF) & ~0x1F;
262 sc->sc_door_off = XREAD4(sc, capa, XHCI_DBOFF) & ~0x3;
264 DPRINTF("CAPLENGTH=0x%x\n", sc->sc_oper_off);
265 DPRINTF("RUNTIMEOFFSET=0x%x\n", sc->sc_runt_off);
266 DPRINTF("DOOROFFSET=0x%x\n", sc->sc_door_off);
268 sc->sc_event_ccs = 1;
269 sc->sc_event_idx = 0;
270 sc->sc_command_ccs = 1;
271 sc->sc_command_idx = 0;
273 DPRINTF("xHCI version = 0x%04x\n", XREAD2(sc, capa, XHCI_HCIVERSION));
275 temp = XREAD4(sc, capa, XHCI_HCSPARAMS0);
277 DPRINTF("HCS0 = 0x%08x\n", temp);
279 if (XHCI_HCS0_CSZ(temp)) {
280 sc->sc_ctx_is_64_byte = 1;
281 device_printf(sc->sc_bus.parent, "64 byte context size.\n");
283 sc->sc_ctx_is_64_byte = 0;
284 device_printf(sc->sc_bus.parent, "32 byte context size.\n");
287 /* Reset controller */
288 XWRITE4(sc, oper, XHCI_USBCMD, XHCI_CMD_HCRST);
290 for (i = 0; i != 100; i++) {
291 usb_pause_mtx(NULL, hz / 100);
292 temp = XREAD4(sc, oper, XHCI_USBCMD) &
293 (XHCI_CMD_HCRST | XHCI_STS_CNR);
299 device_printf(sc->sc_bus.parent, "Controller "
301 return (USB_ERR_IOERROR);
304 if (!(XREAD4(sc, oper, XHCI_PAGESIZE) & XHCI_PAGESIZE_4K)) {
305 device_printf(sc->sc_bus.parent, "Controller does "
306 "not support 4K page size.\n");
307 return (USB_ERR_IOERROR);
310 temp = XREAD4(sc, capa, XHCI_HCSPARAMS1);
312 i = XHCI_HCS1_N_PORTS(temp);
315 device_printf(sc->sc_bus.parent, "Invalid number "
316 "of ports: %u\n", i);
317 return (USB_ERR_IOERROR);
321 sc->sc_noslot = XHCI_HCS1_DEVSLOT_MAX(temp);
323 if (sc->sc_noslot > XHCI_MAX_DEVICES)
324 sc->sc_noslot = XHCI_MAX_DEVICES;
326 /* setup number of device slots */
328 DPRINTF("CONFIG=0x%08x -> 0x%08x\n",
329 XREAD4(sc, oper, XHCI_CONFIG), sc->sc_noslot);
331 XWRITE4(sc, oper, XHCI_CONFIG, sc->sc_noslot);
333 DPRINTF("Max slots: %u\n", sc->sc_noslot);
335 temp = XREAD4(sc, capa, XHCI_HCSPARAMS2);
337 sc->sc_noscratch = XHCI_HCS2_SPB_MAX(temp);
339 if (sc->sc_noscratch > XHCI_MAX_SCRATCHPADS) {
340 device_printf(sc->sc_bus.parent, "XHCI request "
341 "too many scratchpads\n");
342 return (USB_ERR_NOMEM);
345 DPRINTF("Max scratch: %u\n", sc->sc_noscratch);
347 temp = XREAD4(sc, capa, XHCI_HCSPARAMS3);
349 sc->sc_exit_lat_max = XHCI_HCS3_U1_DEL(temp) +
350 XHCI_HCS3_U2_DEL(temp) + 250 /* us */;
352 temp = XREAD4(sc, oper, XHCI_USBSTS);
354 /* clear interrupts */
355 XWRITE4(sc, oper, XHCI_USBSTS, temp);
356 /* disable all device notifications */
357 XWRITE4(sc, oper, XHCI_DNCTRL, 0);
359 /* setup device context base address */
360 usbd_get_page(&sc->sc_hw.ctx_pc, 0, &buf_res);
361 pdctxa = buf_res.buffer;
362 memset(pdctxa, 0, sizeof(*pdctxa));
364 addr = buf_res.physaddr;
365 addr += (uintptr_t)&((struct xhci_dev_ctx_addr *)0)->qwSpBufPtr[0];
367 /* slot 0 points to the table of scratchpad pointers */
368 pdctxa->qwBaaDevCtxAddr[0] = htole64(addr);
370 for (i = 0; i != sc->sc_noscratch; i++) {
371 struct usb_page_search buf_scp;
372 usbd_get_page(&sc->sc_hw.scratch_pc[i], 0, &buf_scp);
373 pdctxa->qwSpBufPtr[i] = htole64((uint64_t)buf_scp.physaddr);
376 addr = buf_res.physaddr;
378 XWRITE4(sc, oper, XHCI_DCBAAP_LO, (uint32_t)addr);
379 XWRITE4(sc, oper, XHCI_DCBAAP_HI, (uint32_t)(addr >> 32));
380 XWRITE4(sc, oper, XHCI_DCBAAP_LO, (uint32_t)addr);
381 XWRITE4(sc, oper, XHCI_DCBAAP_HI, (uint32_t)(addr >> 32));
383 /* Setup event table size */
385 temp = XREAD4(sc, capa, XHCI_HCSPARAMS2);
387 DPRINTF("HCS2=0x%08x\n", temp);
389 temp = XHCI_HCS2_ERST_MAX(temp);
391 if (temp > XHCI_MAX_RSEG)
392 temp = XHCI_MAX_RSEG;
394 sc->sc_erst_max = temp;
396 DPRINTF("ERSTSZ=0x%08x -> 0x%08x\n",
397 XREAD4(sc, runt, XHCI_ERSTSZ(0)), temp);
399 XWRITE4(sc, runt, XHCI_ERSTSZ(0), XHCI_ERSTS_SET(temp));
401 /* Setup interrupt rate */
402 XWRITE4(sc, runt, XHCI_IMOD(0), XHCI_IMOD_DEFAULT);
404 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
406 phwr = buf_res.buffer;
407 addr = buf_res.physaddr;
408 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_events[0];
410 /* reset hardware root structure */
411 memset(phwr, 0, sizeof(*phwr));
413 phwr->hwr_ring_seg[0].qwEvrsTablePtr = htole64(addr);
414 phwr->hwr_ring_seg[0].dwEvrsTableSize = htole32(XHCI_MAX_EVENTS);
416 DPRINTF("ERDP(0)=0x%016llx\n", (unsigned long long)addr);
418 XWRITE4(sc, runt, XHCI_ERDP_LO(0), (uint32_t)addr);
419 XWRITE4(sc, runt, XHCI_ERDP_HI(0), (uint32_t)(addr >> 32));
421 addr = (uint64_t)buf_res.physaddr;
423 DPRINTF("ERSTBA(0)=0x%016llx\n", (unsigned long long)addr);
425 XWRITE4(sc, runt, XHCI_ERSTBA_LO(0), (uint32_t)addr);
426 XWRITE4(sc, runt, XHCI_ERSTBA_HI(0), (uint32_t)(addr >> 32));
428 /* Setup interrupter registers */
430 temp = XREAD4(sc, runt, XHCI_IMAN(0));
431 temp |= XHCI_IMAN_INTR_ENA;
432 XWRITE4(sc, runt, XHCI_IMAN(0), temp);
434 /* setup command ring control base address */
435 addr = buf_res.physaddr;
436 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_commands[0];
438 DPRINTF("CRCR=0x%016llx\n", (unsigned long long)addr);
440 XWRITE4(sc, oper, XHCI_CRCR_LO, ((uint32_t)addr) | XHCI_CRCR_LO_RCS);
441 XWRITE4(sc, oper, XHCI_CRCR_HI, (uint32_t)(addr >> 32));
443 phwr->hwr_commands[XHCI_MAX_COMMANDS - 1].qwTrb0 = htole64(addr);
445 usb_bus_mem_flush_all(&sc->sc_bus, &xhci_iterate_hw_softc);
448 XWRITE4(sc, oper, XHCI_USBCMD, XHCI_CMD_RS |
449 XHCI_CMD_INTE | XHCI_CMD_HSEE);
451 for (i = 0; i != 100; i++) {
452 usb_pause_mtx(NULL, hz / 100);
453 temp = XREAD4(sc, oper, XHCI_USBSTS) & XHCI_STS_HCH;
458 XWRITE4(sc, oper, XHCI_USBCMD, 0);
459 device_printf(sc->sc_bus.parent, "Run timeout.\n");
460 return (USB_ERR_IOERROR);
463 /* catch any lost interrupts */
464 xhci_do_poll(&sc->sc_bus);
470 xhci_halt_controller(struct xhci_softc *sc)
478 sc->sc_oper_off = XREAD1(sc, capa, XHCI_CAPLENGTH);
479 sc->sc_runt_off = XREAD4(sc, capa, XHCI_RTSOFF) & ~0xF;
480 sc->sc_door_off = XREAD4(sc, capa, XHCI_DBOFF) & ~0x3;
482 /* Halt controller */
483 XWRITE4(sc, oper, XHCI_USBCMD, 0);
485 for (i = 0; i != 100; i++) {
486 usb_pause_mtx(NULL, hz / 100);
487 temp = XREAD4(sc, oper, XHCI_USBSTS) & XHCI_STS_HCH;
493 device_printf(sc->sc_bus.parent, "Controller halt timeout.\n");
494 return (USB_ERR_IOERROR);
500 xhci_init(struct xhci_softc *sc, device_t self)
502 /* initialise some bus fields */
503 sc->sc_bus.parent = self;
505 /* set the bus revision */
506 sc->sc_bus.usbrev = USB_REV_3_0;
508 /* set up the bus struct */
509 sc->sc_bus.methods = &xhci_bus_methods;
511 /* setup devices array */
512 sc->sc_bus.devices = sc->sc_devices;
513 sc->sc_bus.devices_max = XHCI_MAX_DEVICES;
515 /* setup command queue mutex and condition varible */
516 cv_init(&sc->sc_cmd_cv, "CMDQ");
517 lockinit(&sc->sc_cmd_lock, "CMDQ lock", 0, 0);
519 /* get all DMA memory */
520 if (usb_bus_mem_alloc_all(&sc->sc_bus,
521 USB_GET_DMA_TAG(self), &xhci_iterate_hw_softc)) {
525 sc->sc_config_msg[0].hdr.pm_callback = &xhci_configure_msg;
526 sc->sc_config_msg[0].bus = &sc->sc_bus;
527 sc->sc_config_msg[1].hdr.pm_callback = &xhci_configure_msg;
528 sc->sc_config_msg[1].bus = &sc->sc_bus;
530 if (usb_proc_create(&sc->sc_config_proc,
531 &sc->sc_bus.bus_lock, device_get_nameunit(self), USB_PRI_MED)) {
532 kprintf("WARNING: Creation of XHCI configure "
533 "callback process failed.\n");
539 xhci_uninit(struct xhci_softc *sc)
541 usb_proc_free(&sc->sc_config_proc);
543 usb_bus_mem_free_all(&sc->sc_bus, &xhci_iterate_hw_softc);
545 cv_destroy(&sc->sc_cmd_cv);
546 lockuninit(&sc->sc_cmd_lock);
550 xhci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
552 struct xhci_softc *sc = XHCI_BUS2SC(bus);
555 case USB_HW_POWER_SUSPEND:
556 DPRINTF("Stopping the XHCI\n");
557 xhci_halt_controller(sc);
559 case USB_HW_POWER_SHUTDOWN:
560 DPRINTF("Stopping the XHCI\n");
561 xhci_halt_controller(sc);
563 case USB_HW_POWER_RESUME:
564 DPRINTF("Starting the XHCI\n");
565 xhci_start_controller(sc);
573 xhci_generic_done_sub(struct usb_xfer *xfer)
576 struct xhci_td *td_alt_next;
580 td = xfer->td_transfer_cache;
581 td_alt_next = td->alt_next;
583 if (xfer->aframes != xfer->nframes)
584 usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
588 usb_pc_cpu_invalidate(td->page_cache);
593 DPRINTFN(4, "xfer=%p[%u/%u] rem=%u/%u status=%u\n",
594 xfer, (unsigned int)xfer->aframes,
595 (unsigned int)xfer->nframes,
596 (unsigned int)len, (unsigned int)td->len,
597 (unsigned int)status);
600 * Verify the status length and
601 * add the length to "frlengths[]":
604 /* should not happen */
605 DPRINTF("Invalid status length, "
606 "0x%04x/0x%04x bytes\n", len, td->len);
607 status = XHCI_TRB_ERROR_LENGTH;
608 } else if (xfer->aframes != xfer->nframes) {
609 xfer->frlengths[xfer->aframes] += td->len - len;
611 /* Check for last transfer */
612 if (((void *)td) == xfer->td_transfer_last) {
616 /* Check for transfer error */
617 if (status != XHCI_TRB_ERROR_SHORT_PKT &&
618 status != XHCI_TRB_ERROR_SUCCESS) {
619 /* the transfer is finished */
623 /* Check for short transfer */
625 if (xfer->flags_int.short_frames_ok ||
626 xfer->flags_int.isochronous_xfr ||
627 xfer->flags_int.control_xfr) {
628 /* follow alt next */
631 /* the transfer is finished */
638 if (td->alt_next != td_alt_next) {
639 /* this USB frame is complete */
644 /* update transfer cache */
646 xfer->td_transfer_cache = td;
648 return ((status == XHCI_TRB_ERROR_STALL) ? USB_ERR_STALLED :
649 (status != XHCI_TRB_ERROR_SHORT_PKT &&
650 status != XHCI_TRB_ERROR_SUCCESS) ? USB_ERR_IOERROR :
651 USB_ERR_NORMAL_COMPLETION);
655 xhci_generic_done(struct usb_xfer *xfer)
659 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
660 xfer, xfer->endpoint);
664 xfer->td_transfer_cache = xfer->td_transfer_first;
666 if (xfer->flags_int.control_xfr) {
668 if (xfer->flags_int.control_hdr)
669 err = xhci_generic_done_sub(xfer);
673 if (xfer->td_transfer_cache == NULL)
677 while (xfer->aframes != xfer->nframes) {
679 err = xhci_generic_done_sub(xfer);
682 if (xfer->td_transfer_cache == NULL)
686 if (xfer->flags_int.control_xfr &&
687 !xfer->flags_int.control_act)
688 err = xhci_generic_done_sub(xfer);
690 /* transfer is complete */
691 xhci_device_done(xfer, err);
695 xhci_activate_transfer(struct usb_xfer *xfer)
699 td = xfer->td_transfer_cache;
701 usb_pc_cpu_invalidate(td->page_cache);
703 if (!(td->td_trb[0].dwTrb3 & htole32(XHCI_TRB_3_CYCLE_BIT))) {
705 /* activate the transfer */
707 td->td_trb[0].dwTrb3 |= htole32(XHCI_TRB_3_CYCLE_BIT);
708 usb_pc_cpu_flush(td->page_cache);
710 xhci_endpoint_doorbell(xfer);
715 xhci_skip_transfer(struct usb_xfer *xfer)
718 struct xhci_td *td_last;
720 td = xfer->td_transfer_cache;
721 td_last = xfer->td_transfer_last;
725 usb_pc_cpu_invalidate(td->page_cache);
727 if (!(td->td_trb[0].dwTrb3 & htole32(XHCI_TRB_3_CYCLE_BIT))) {
729 usb_pc_cpu_invalidate(td_last->page_cache);
731 /* copy LINK TRB to current waiting location */
733 td->td_trb[0].qwTrb0 = td_last->td_trb[td_last->ntrb].qwTrb0;
734 td->td_trb[0].dwTrb2 = td_last->td_trb[td_last->ntrb].dwTrb2;
735 usb_pc_cpu_flush(td->page_cache);
737 td->td_trb[0].dwTrb3 = td_last->td_trb[td_last->ntrb].dwTrb3;
738 usb_pc_cpu_flush(td->page_cache);
740 xhci_endpoint_doorbell(xfer);
744 /*------------------------------------------------------------------------*
745 * xhci_check_transfer
746 *------------------------------------------------------------------------*/
748 xhci_check_transfer(struct xhci_softc *sc, struct xhci_trb *trb)
761 td_event = le64toh(trb->qwTrb0);
762 temp = le32toh(trb->dwTrb2);
764 remainder = XHCI_TRB_2_REM_GET(temp);
765 status = XHCI_TRB_2_ERROR_GET(temp);
767 temp = le32toh(trb->dwTrb3);
768 epno = XHCI_TRB_3_EP_GET(temp);
769 index = XHCI_TRB_3_SLOT_GET(temp);
771 /* check if error means halted */
772 halted = (status != XHCI_TRB_ERROR_SHORT_PKT &&
773 status != XHCI_TRB_ERROR_SUCCESS);
775 DPRINTF("slot=%u epno=%u remainder=%u status=%u\n",
776 index, epno, remainder, status);
778 if (index > sc->sc_noslot) {
779 DPRINTF("Invalid slot.\n");
783 if ((epno == 0) || (epno >= XHCI_MAX_ENDPOINTS)) {
784 DPRINTF("Invalid endpoint.\n");
788 /* try to find the USB transfer that generated the event */
789 for (i = 0; i != (XHCI_MAX_TRANSFERS - 1); i++) {
790 struct usb_xfer *xfer;
792 struct xhci_endpoint_ext *pepext;
794 pepext = &sc->sc_hw.devs[index].endp[epno];
796 xfer = pepext->xfer[i];
800 td = xfer->td_transfer_cache;
802 DPRINTFN(5, "Checking if 0x%016llx == (0x%016llx .. 0x%016llx)\n",
804 (long long)td->td_self,
805 (long long)td->td_self + sizeof(td->td_trb));
808 * NOTE: Some XHCI implementations might not trigger
809 * an event on the last LINK TRB so we need to
810 * consider both the last and second last event
811 * address as conditions for a successful transfer.
813 * NOTE: We assume that the XHCI will only trigger one
814 * event per chain of TRBs.
817 offset = td_event - td->td_self;
820 offset < sizeof(td->td_trb)) {
822 usb_pc_cpu_invalidate(td->page_cache);
824 /* compute rest of remainder, if any */
825 for (i = (offset / 16) + 1; i < td->ntrb; i++) {
826 temp = le32toh(td->td_trb[i].dwTrb2);
827 remainder += XHCI_TRB_2_BYTES_GET(temp);
830 DPRINTFN(5, "New remainder: %u\n", remainder);
832 /* clear isochronous transfer errors */
833 if (xfer->flags_int.isochronous_xfr) {
836 status = XHCI_TRB_ERROR_SUCCESS;
841 /* "td->remainder" is verified later */
842 td->remainder = remainder;
845 usb_pc_cpu_flush(td->page_cache);
848 * 1) Last transfer descriptor makes the
851 if (((void *)td) == xfer->td_transfer_last) {
852 DPRINTF("TD is last\n");
853 xhci_generic_done(xfer);
858 * 2) Any kind of error makes the transfer
862 DPRINTF("TD has I/O error\n");
863 xhci_generic_done(xfer);
868 * 3) If there is no alternate next transfer,
869 * a short packet also makes the transfer done
871 if (td->remainder > 0) {
872 DPRINTF("TD has short pkt\n");
873 if (xfer->flags_int.short_frames_ok ||
874 xfer->flags_int.isochronous_xfr ||
875 xfer->flags_int.control_xfr) {
876 /* follow the alt next */
877 xfer->td_transfer_cache = td->alt_next;
878 xhci_activate_transfer(xfer);
881 xhci_skip_transfer(xfer);
882 xhci_generic_done(xfer);
887 * 4) Transfer complete - go to next TD
889 DPRINTF("Following next TD\n");
890 xfer->td_transfer_cache = td->obj_next;
891 xhci_activate_transfer(xfer);
892 break; /* there should only be one match */
898 xhci_check_command(struct xhci_softc *sc, struct xhci_trb *trb)
900 if (sc->sc_cmd_addr == trb->qwTrb0) {
901 DPRINTF("Received command event\n");
902 sc->sc_cmd_result[0] = trb->dwTrb2;
903 sc->sc_cmd_result[1] = trb->dwTrb3;
904 cv_signal(&sc->sc_cmd_cv);
909 xhci_interrupt_poll(struct xhci_softc *sc)
911 struct usb_page_search buf_res;
912 struct xhci_hw_root *phwr;
921 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
923 phwr = buf_res.buffer;
925 /* Receive any events */
927 usb_pc_cpu_invalidate(&sc->sc_hw.root_pc);
929 i = sc->sc_event_idx;
930 j = sc->sc_event_ccs;
935 temp = le32toh(phwr->hwr_events[i].dwTrb3);
937 k = (temp & XHCI_TRB_3_CYCLE_BIT) ? 1 : 0;
942 event = XHCI_TRB_3_TYPE_GET(temp);
944 DPRINTFN(10, "event[%u] = %u (0x%016llx 0x%08lx 0x%08lx)\n",
945 i, event, (long long)le64toh(phwr->hwr_events[i].qwTrb0),
946 (long)le32toh(phwr->hwr_events[i].dwTrb2),
947 (long)le32toh(phwr->hwr_events[i].dwTrb3));
950 case XHCI_TRB_EVENT_TRANSFER:
951 xhci_check_transfer(sc, &phwr->hwr_events[i]);
953 case XHCI_TRB_EVENT_CMD_COMPLETE:
954 xhci_check_command(sc, &phwr->hwr_events[i]);
957 DPRINTF("Unhandled event = %u\n", event);
963 if (i == XHCI_MAX_EVENTS) {
967 /* check for timeout */
973 sc->sc_event_idx = i;
974 sc->sc_event_ccs = j;
977 * NOTE: The Event Ring Dequeue Pointer Register is 64-bit
978 * latched. That means to activate the register we need to
979 * write both the low and high double word of the 64-bit
983 addr = (uint32_t)buf_res.physaddr;
984 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_events[i];
986 /* try to clear busy bit */
987 addr |= XHCI_ERDP_LO_BUSY;
989 XWRITE4(sc, runt, XHCI_ERDP_LO(0), (uint32_t)addr);
990 XWRITE4(sc, runt, XHCI_ERDP_HI(0), (uint32_t)(addr >> 32));
994 xhci_do_command(struct xhci_softc *sc, struct xhci_trb *trb,
997 struct usb_page_search buf_res;
998 struct xhci_hw_root *phwr;
1005 XHCI_CMD_ASSERT_LOCKED(sc);
1007 /* get hardware root structure */
1009 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
1011 phwr = buf_res.buffer;
1015 USB_BUS_LOCK(&sc->sc_bus);
1017 i = sc->sc_command_idx;
1018 j = sc->sc_command_ccs;
1020 DPRINTFN(10, "command[%u] = %u (0x%016llx, 0x%08lx, 0x%08lx)\n",
1021 i, XHCI_TRB_3_TYPE_GET(le32toh(trb->dwTrb3)),
1022 (long long)le64toh(trb->qwTrb0),
1023 (long)le32toh(trb->dwTrb2),
1024 (long)le32toh(trb->dwTrb3));
1026 phwr->hwr_commands[i].qwTrb0 = trb->qwTrb0;
1027 phwr->hwr_commands[i].dwTrb2 = trb->dwTrb2;
1029 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1034 temp |= htole32(XHCI_TRB_3_CYCLE_BIT);
1036 temp &= ~htole32(XHCI_TRB_3_CYCLE_BIT);
1038 temp &= ~htole32(XHCI_TRB_3_TC_BIT);
1040 phwr->hwr_commands[i].dwTrb3 = temp;
1042 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1044 addr = buf_res.physaddr;
1045 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_commands[i];
1047 sc->sc_cmd_addr = htole64(addr);
1051 if (i == (XHCI_MAX_COMMANDS - 1)) {
1054 temp = htole32(XHCI_TRB_3_TC_BIT |
1055 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) |
1056 XHCI_TRB_3_CYCLE_BIT);
1058 temp = htole32(XHCI_TRB_3_TC_BIT |
1059 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
1062 phwr->hwr_commands[i].dwTrb3 = temp;
1064 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1070 sc->sc_command_idx = i;
1071 sc->sc_command_ccs = j;
1073 XWRITE4(sc, door, XHCI_DOORBELL(0), 0);
1075 err = cv_timedwait(&sc->sc_cmd_cv, &sc->sc_bus.bus_lock,
1076 USB_MS_TO_TICKS(timeout_ms));
1079 DPRINTFN(0, "Command timeout!\n");
1080 err = USB_ERR_TIMEOUT;
1084 temp = le32toh(sc->sc_cmd_result[0]);
1085 if (XHCI_TRB_2_ERROR_GET(temp) != XHCI_TRB_ERROR_SUCCESS)
1086 err = USB_ERR_IOERROR;
1088 trb->dwTrb2 = sc->sc_cmd_result[0];
1089 trb->dwTrb3 = sc->sc_cmd_result[1];
1092 USB_BUS_UNLOCK(&sc->sc_bus);
1099 xhci_cmd_nop(struct xhci_softc *sc)
1101 struct xhci_trb trb;
1108 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NOOP);
1110 trb.dwTrb3 = htole32(temp);
1112 return (xhci_do_command(sc, &trb, 100 /* ms */));
1117 xhci_cmd_enable_slot(struct xhci_softc *sc, uint8_t *pslot)
1119 struct xhci_trb trb;
1127 trb.dwTrb3 = htole32(XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ENABLE_SLOT));
1129 err = xhci_do_command(sc, &trb, 100 /* ms */);
1133 temp = le32toh(trb.dwTrb3);
1135 *pslot = XHCI_TRB_3_SLOT_GET(temp);
1142 xhci_cmd_disable_slot(struct xhci_softc *sc, uint8_t slot_id)
1144 struct xhci_trb trb;
1151 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DISABLE_SLOT) |
1152 XHCI_TRB_3_SLOT_SET(slot_id);
1154 trb.dwTrb3 = htole32(temp);
1156 return (xhci_do_command(sc, &trb, 100 /* ms */));
1160 xhci_cmd_set_address(struct xhci_softc *sc, uint64_t input_ctx,
1161 uint8_t bsr, uint8_t slot_id)
1163 struct xhci_trb trb;
1168 trb.qwTrb0 = htole64(input_ctx);
1170 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ADDRESS_DEVICE) |
1171 XHCI_TRB_3_SLOT_SET(slot_id);
1174 temp |= XHCI_TRB_3_BSR_BIT;
1176 trb.dwTrb3 = htole32(temp);
1178 return (xhci_do_command(sc, &trb, 500 /* ms */));
1182 xhci_set_address(struct usb_device *udev, struct lock *lock, uint16_t address)
1184 struct usb_page_search buf_inp;
1185 struct usb_page_search buf_dev;
1186 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
1187 struct xhci_hw_dev *hdev;
1188 struct xhci_dev_ctx *pdev;
1189 struct xhci_endpoint_ext *pepext;
1195 /* the root HUB case is not handled here */
1196 if (udev->parent_hub == NULL)
1197 return (USB_ERR_INVAL);
1199 index = udev->controller_slot_id;
1201 hdev = &sc->sc_hw.devs[index];
1204 lockmgr(lock, LK_RELEASE);
1208 switch (hdev->state) {
1209 case XHCI_ST_DEFAULT:
1210 case XHCI_ST_ENABLED:
1212 hdev->state = XHCI_ST_ENABLED;
1214 /* set configure mask to slot and EP0 */
1215 xhci_configure_mask(udev, 3, 0);
1217 /* configure input slot context structure */
1218 err = xhci_configure_device(udev);
1221 DPRINTF("Could not configure device\n");
1225 /* configure input endpoint context structure */
1226 switch (udev->speed) {
1228 case USB_SPEED_FULL:
1231 case USB_SPEED_HIGH:
1239 pepext = xhci_get_endpoint_ext(udev,
1240 &udev->ctrl_ep_desc);
1241 err = xhci_configure_endpoint(udev,
1242 &udev->ctrl_ep_desc, pepext->physaddr,
1243 0, 1, 1, 0, mps, mps);
1246 DPRINTF("Could not configure default endpoint\n");
1250 /* execute set address command */
1251 usbd_get_page(&hdev->input_pc, 0, &buf_inp);
1253 err = xhci_cmd_set_address(sc, buf_inp.physaddr,
1254 (address == 0), index);
1257 DPRINTF("Could not set address "
1258 "for slot %u.\n", index);
1263 /* update device address to new value */
1265 usbd_get_page(&hdev->device_pc, 0, &buf_dev);
1266 pdev = buf_dev.buffer;
1267 usb_pc_cpu_invalidate(&hdev->device_pc);
1269 temp = xhci_ctx_get_le32(sc, &pdev->ctx_slot.dwSctx3);
1270 udev->address = XHCI_SCTX_3_DEV_ADDR_GET(temp);
1272 /* update device state to new value */
1275 hdev->state = XHCI_ST_ADDRESSED;
1277 hdev->state = XHCI_ST_DEFAULT;
1281 DPRINTF("Wrong state for set address.\n");
1282 err = USB_ERR_IOERROR;
1285 XHCI_CMD_UNLOCK(sc);
1288 lockmgr(lock, LK_EXCLUSIVE);
1294 xhci_cmd_configure_ep(struct xhci_softc *sc, uint64_t input_ctx,
1295 uint8_t deconfigure, uint8_t slot_id)
1297 struct xhci_trb trb;
1302 trb.qwTrb0 = htole64(input_ctx);
1304 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_CONFIGURE_EP) |
1305 XHCI_TRB_3_SLOT_SET(slot_id);
1308 temp |= XHCI_TRB_3_DCEP_BIT;
1310 trb.dwTrb3 = htole32(temp);
1312 return (xhci_do_command(sc, &trb, 100 /* ms */));
1316 xhci_cmd_evaluate_ctx(struct xhci_softc *sc, uint64_t input_ctx,
1319 struct xhci_trb trb;
1324 trb.qwTrb0 = htole64(input_ctx);
1326 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVALUATE_CTX) |
1327 XHCI_TRB_3_SLOT_SET(slot_id);
1328 trb.dwTrb3 = htole32(temp);
1330 return (xhci_do_command(sc, &trb, 100 /* ms */));
1334 xhci_cmd_reset_ep(struct xhci_softc *sc, uint8_t preserve,
1335 uint8_t ep_id, uint8_t slot_id)
1337 struct xhci_trb trb;
1344 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_EP) |
1345 XHCI_TRB_3_SLOT_SET(slot_id) |
1346 XHCI_TRB_3_EP_SET(ep_id);
1349 temp |= XHCI_TRB_3_PRSV_BIT;
1351 trb.dwTrb3 = htole32(temp);
1353 return (xhci_do_command(sc, &trb, 100 /* ms */));
1357 xhci_cmd_set_tr_dequeue_ptr(struct xhci_softc *sc, uint64_t dequeue_ptr,
1358 uint16_t stream_id, uint8_t ep_id, uint8_t slot_id)
1360 struct xhci_trb trb;
1365 trb.qwTrb0 = htole64(dequeue_ptr);
1367 temp = XHCI_TRB_2_STREAM_SET(stream_id);
1368 trb.dwTrb2 = htole32(temp);
1370 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SET_TR_DEQUEUE) |
1371 XHCI_TRB_3_SLOT_SET(slot_id) |
1372 XHCI_TRB_3_EP_SET(ep_id);
1373 trb.dwTrb3 = htole32(temp);
1375 return (xhci_do_command(sc, &trb, 100 /* ms */));
1379 xhci_cmd_stop_ep(struct xhci_softc *sc, uint8_t suspend,
1380 uint8_t ep_id, uint8_t slot_id)
1382 struct xhci_trb trb;
1389 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STOP_EP) |
1390 XHCI_TRB_3_SLOT_SET(slot_id) |
1391 XHCI_TRB_3_EP_SET(ep_id);
1394 temp |= XHCI_TRB_3_SUSP_EP_BIT;
1396 trb.dwTrb3 = htole32(temp);
1398 return (xhci_do_command(sc, &trb, 100 /* ms */));
1402 xhci_cmd_reset_dev(struct xhci_softc *sc, uint8_t slot_id)
1404 struct xhci_trb trb;
1411 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_DEVICE) |
1412 XHCI_TRB_3_SLOT_SET(slot_id);
1414 trb.dwTrb3 = htole32(temp);
1416 return (xhci_do_command(sc, &trb, 100 /* ms */));
1419 /*------------------------------------------------------------------------*
1420 * xhci_interrupt - XHCI interrupt handler
1421 *------------------------------------------------------------------------*/
1423 xhci_interrupt(struct xhci_softc *sc)
1428 USB_BUS_LOCK(&sc->sc_bus);
1430 status = XREAD4(sc, oper, XHCI_USBSTS);
1432 /* acknowledge interrupts */
1434 XWRITE4(sc, oper, XHCI_USBSTS, status);
1436 temp = XREAD4(sc, runt, XHCI_IMAN(0));
1438 /* acknowledge pending event */
1440 XWRITE4(sc, runt, XHCI_IMAN(0), temp);
1442 DPRINTFN(16, "real interrupt (sts=0x%08x, "
1443 "iman=0x%08x)\n", status, temp);
1446 if (status & XHCI_STS_PCD) {
1450 if (status & XHCI_STS_HCH) {
1451 kprintf("%s: host controller halted\n",
1455 if (status & XHCI_STS_HSE) {
1456 kprintf("%s: host system error\n",
1460 if (status & XHCI_STS_HCE) {
1461 kprintf("%s: host controller error\n",
1466 xhci_interrupt_poll(sc);
1468 USB_BUS_UNLOCK(&sc->sc_bus);
1471 /*------------------------------------------------------------------------*
1472 * xhci_timeout - XHCI timeout handler
1473 *------------------------------------------------------------------------*/
1475 xhci_timeout(void *arg)
1477 struct usb_xfer *xfer = arg;
1479 DPRINTF("xfer=%p\n", xfer);
1481 USB_BUS_LOCK_ASSERT(xfer->xroot->bus);
1483 /* transfer is transferred */
1484 xhci_device_done(xfer, USB_ERR_TIMEOUT);
1488 xhci_do_poll(struct usb_bus *bus)
1490 struct xhci_softc *sc = XHCI_BUS2SC(bus);
1492 USB_BUS_LOCK(&sc->sc_bus);
1493 xhci_interrupt_poll(sc);
1494 USB_BUS_UNLOCK(&sc->sc_bus);
1498 xhci_setup_generic_chain_sub(struct xhci_std_temp *temp)
1500 struct usb_page_search buf_res;
1502 struct xhci_td *td_next;
1503 struct xhci_td *td_alt_next;
1504 uint32_t buf_offset;
1508 uint8_t shortpkt_old;
1514 shortpkt_old = temp->shortpkt;
1515 len_old = temp->len;
1521 td_next = temp->td_next;
1525 if (temp->len == 0) {
1530 /* send a Zero Length Packet, ZLP, last */
1537 average = temp->average;
1539 if (temp->len < average) {
1540 if (temp->len % temp->max_packet_size) {
1543 average = temp->len;
1547 if (td_next == NULL)
1548 panic("%s: out of XHCI transfer descriptors!", __FUNCTION__);
1553 td_next = td->obj_next;
1555 /* check if we are pre-computing */
1559 /* update remaining length */
1561 temp->len -= average;
1565 /* fill out current TD */
1571 /* update remaining length */
1573 temp->len -= average;
1575 /* reset TRB index */
1579 if (temp->trb_type == XHCI_TRB_TYPE_SETUP_STAGE) {
1580 /* immediate data */
1585 td->td_trb[0].qwTrb0 = 0;
1587 usbd_copy_out(temp->pc, temp->offset + buf_offset,
1588 (uint8_t *)(uintptr_t)&td->td_trb[0].qwTrb0,
1591 dword = XHCI_TRB_2_BYTES_SET(8) |
1592 XHCI_TRB_2_TDSZ_SET(0) |
1593 XHCI_TRB_2_IRQ_SET(0);
1595 td->td_trb[0].dwTrb2 = htole32(dword);
1597 dword = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SETUP_STAGE) |
1598 XHCI_TRB_3_IDT_BIT | XHCI_TRB_3_CYCLE_BIT;
1601 if (td->td_trb[0].qwTrb0 &
1602 htole64(XHCI_TRB_0_WLENGTH_MASK)) {
1603 if (td->td_trb[0].qwTrb0 & htole64(1))
1604 dword |= XHCI_TRB_3_TRT_IN;
1606 dword |= XHCI_TRB_3_TRT_OUT;
1609 td->td_trb[0].dwTrb3 = htole32(dword);
1611 xhci_dump_trb(&td->td_trb[x]);
1619 /* fill out buffer pointers */
1623 memset(&buf_res, 0, sizeof(buf_res));
1625 usbd_get_page(temp->pc, temp->offset +
1626 buf_offset, &buf_res);
1628 /* get length to end of page */
1629 if (buf_res.length > average)
1630 buf_res.length = average;
1632 /* check for maximum length */
1633 if (buf_res.length > XHCI_TD_PAGE_SIZE)
1634 buf_res.length = XHCI_TD_PAGE_SIZE;
1637 npkt = (average + temp->max_packet_size - 1) /
1638 temp->max_packet_size;
1644 /* fill out TRB's */
1645 td->td_trb[x].qwTrb0 =
1646 htole64((uint64_t)buf_res.physaddr);
1649 XHCI_TRB_2_BYTES_SET(buf_res.length) |
1650 XHCI_TRB_2_TDSZ_SET(npkt) |
1651 XHCI_TRB_2_IRQ_SET(0);
1653 td->td_trb[x].dwTrb2 = htole32(dword);
1655 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1656 XHCI_TRB_3_TYPE_SET(temp->trb_type) |
1657 XHCI_TRB_3_FRID_SET(temp->isoc_frame / 8) |
1658 XHCI_TRB_3_TBC_SET(temp->tbc) |
1659 XHCI_TRB_3_TLBPC_SET(temp->tlbpc);
1661 if (temp->direction == UE_DIR_IN) {
1662 dword |= XHCI_TRB_3_DIR_IN;
1665 * NOTE: Only the SETUP stage should
1666 * use the IDT bit. Else transactions
1667 * can be sent using the wrong data
1670 if (temp->trb_type !=
1671 XHCI_TRB_TYPE_SETUP_STAGE &&
1673 XHCI_TRB_TYPE_STATUS_STAGE)
1674 dword |= XHCI_TRB_3_ISP_BIT;
1677 td->td_trb[x].dwTrb3 = htole32(dword);
1679 average -= buf_res.length;
1680 buf_offset += buf_res.length;
1682 xhci_dump_trb(&td->td_trb[x]);
1686 } while (average != 0);
1688 td->td_trb[x-1].dwTrb3 |= htole32(XHCI_TRB_3_IOC_BIT);
1690 /* store number of data TRB's */
1694 DPRINTF("NTRB=%u\n", x);
1696 /* fill out link TRB */
1698 if (td_next != NULL) {
1699 /* link the current TD with the next one */
1700 td->td_trb[x].qwTrb0 = htole64((uint64_t)td_next->td_self);
1701 DPRINTF("LINK=0x%08llx\n", (long long)td_next->td_self);
1703 /* this field will get updated later */
1704 DPRINTF("NOLINK\n");
1707 dword = XHCI_TRB_2_IRQ_SET(0);
1709 td->td_trb[x].dwTrb2 = htole32(dword);
1711 dword = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) |
1712 XHCI_TRB_3_CYCLE_BIT | XHCI_TRB_3_IOC_BIT;
1714 td->td_trb[x].dwTrb3 = htole32(dword);
1716 td->alt_next = td_alt_next;
1718 xhci_dump_trb(&td->td_trb[x]);
1720 usb_pc_cpu_flush(td->page_cache);
1726 /* setup alt next pointer, if any */
1727 if (temp->last_frame) {
1730 /* we use this field internally */
1731 td_alt_next = td_next;
1735 temp->shortpkt = shortpkt_old;
1736 temp->len = len_old;
1740 /* remove cycle bit from first if we are stepping the TRBs */
1742 td->td_trb[0].dwTrb3 &= ~htole32(XHCI_TRB_3_CYCLE_BIT);
1744 /* remove chain bit because this is the last TRB in the chain */
1745 td->td_trb[td->ntrb - 1].dwTrb2 &= ~htole32(XHCI_TRB_2_TDSZ_SET(15));
1746 td->td_trb[td->ntrb - 1].dwTrb3 &= ~htole32(XHCI_TRB_3_CHAIN_BIT);
1748 usb_pc_cpu_flush(td->page_cache);
1751 temp->td_next = td_next;
1755 xhci_setup_generic_chain(struct usb_xfer *xfer)
1757 struct xhci_std_temp temp;
1766 temp.average = xfer->max_hc_frame_size;
1767 temp.max_packet_size = xfer->max_packet_size;
1768 temp.sc = XHCI_BUS2SC(xfer->xroot->bus);
1770 temp.last_frame = 0;
1772 temp.multishort = xfer->flags_int.isochronous_xfr ||
1773 xfer->flags_int.control_xfr ||
1774 xfer->flags_int.short_frames_ok;
1776 /* toggle the DMA set we are using */
1777 xfer->flags_int.curr_dma_set ^= 1;
1779 /* get next DMA set */
1780 td = xfer->td_start[xfer->flags_int.curr_dma_set];
1785 xfer->td_transfer_first = td;
1786 xfer->td_transfer_cache = td;
1788 if (xfer->flags_int.isochronous_xfr) {
1791 /* compute multiplier for ISOCHRONOUS transfers */
1792 mult = xfer->endpoint->ecomp ?
1793 (xfer->endpoint->ecomp->bmAttributes & 3) : 0;
1794 /* check for USB 2.0 multiplier */
1796 mult = (xfer->endpoint->edesc->
1797 wMaxPacketSize[1] >> 3) & 3;
1805 x = XREAD4(temp.sc, runt, XHCI_MFINDEX);
1807 DPRINTF("MFINDEX=0x%08x\n", x);
1809 switch (usbd_get_speed(xfer->xroot->udev)) {
1810 case USB_SPEED_FULL:
1812 temp.isoc_delta = 8; /* 1ms */
1813 x += temp.isoc_delta - 1;
1814 x &= ~(temp.isoc_delta - 1);
1817 shift = usbd_xfer_get_fps_shift(xfer);
1818 temp.isoc_delta = 1U << shift;
1819 x += temp.isoc_delta - 1;
1820 x &= ~(temp.isoc_delta - 1);
1821 /* simple frame load balancing */
1822 x += xfer->endpoint->usb_uframe;
1826 y = XHCI_MFINDEX_GET(x - xfer->endpoint->isoc_next);
1828 if ((xfer->endpoint->is_synced == 0) ||
1829 (y < (xfer->nframes << shift)) ||
1830 (XHCI_MFINDEX_GET(-y) >= (128 * 8))) {
1832 * If there is data underflow or the pipe
1833 * queue is empty we schedule the transfer a
1834 * few frames ahead of the current frame
1835 * position. Else two isochronous transfers
1838 xfer->endpoint->isoc_next = XHCI_MFINDEX_GET(x + (3 * 8));
1839 xfer->endpoint->is_synced = 1;
1840 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
1843 /* compute isochronous completion time */
1845 y = XHCI_MFINDEX_GET(xfer->endpoint->isoc_next - (x & ~7));
1847 xfer->isoc_time_complete =
1848 usb_isoc_time_expand(&temp.sc->sc_bus, x / 8) +
1849 (y / 8) + (((xfer->nframes << shift) + 7) / 8);
1852 temp.isoc_frame = xfer->endpoint->isoc_next;
1853 temp.trb_type = XHCI_TRB_TYPE_ISOCH;
1855 xfer->endpoint->isoc_next += xfer->nframes << shift;
1857 } else if (xfer->flags_int.control_xfr) {
1859 /* check if we should prepend a setup message */
1861 if (xfer->flags_int.control_hdr) {
1863 temp.len = xfer->frlengths[0];
1864 temp.pc = xfer->frbuffers + 0;
1865 temp.shortpkt = temp.len ? 1 : 0;
1866 temp.trb_type = XHCI_TRB_TYPE_SETUP_STAGE;
1869 /* check for last frame */
1870 if (xfer->nframes == 1) {
1871 /* no STATUS stage yet, SETUP is last */
1872 if (xfer->flags_int.control_act)
1873 temp.last_frame = 1;
1876 xhci_setup_generic_chain_sub(&temp);
1880 temp.isoc_delta = 0;
1881 temp.isoc_frame = 0;
1882 temp.trb_type = XHCI_TRB_TYPE_DATA_STAGE;
1886 temp.isoc_delta = 0;
1887 temp.isoc_frame = 0;
1888 temp.trb_type = XHCI_TRB_TYPE_NORMAL;
1891 if (x != xfer->nframes) {
1892 /* setup page_cache pointer */
1893 temp.pc = xfer->frbuffers + x;
1894 /* set endpoint direction */
1895 temp.direction = UE_GET_DIR(xfer->endpointno);
1898 while (x != xfer->nframes) {
1900 /* DATA0 / DATA1 message */
1902 temp.len = xfer->frlengths[x];
1903 temp.step_td = ((xfer->endpointno & UE_DIR_IN) &&
1904 x != 0 && temp.multishort == 0);
1908 if (x == xfer->nframes) {
1909 if (xfer->flags_int.control_xfr) {
1910 /* no STATUS stage yet, DATA is last */
1911 if (xfer->flags_int.control_act)
1912 temp.last_frame = 1;
1914 temp.last_frame = 1;
1917 if (temp.len == 0) {
1919 /* make sure that we send an USB packet */
1924 temp.tlbpc = mult - 1;
1926 } else if (xfer->flags_int.isochronous_xfr) {
1930 /* isochronous transfers don't have short packet termination */
1934 /* isochronous transfers have a transfer limit */
1936 if (temp.len > xfer->max_frame_size)
1937 temp.len = xfer->max_frame_size;
1939 /* compute TD packet count */
1940 tdpc = (temp.len + xfer->max_packet_size - 1) /
1941 xfer->max_packet_size;
1943 temp.tbc = ((tdpc + mult - 1) / mult) - 1;
1944 temp.tlbpc = (tdpc % mult);
1946 if (temp.tlbpc == 0)
1947 temp.tlbpc = mult - 1;
1952 /* regular data transfer */
1954 temp.shortpkt = xfer->flags.force_short_xfer ? 0 : 1;
1957 xhci_setup_generic_chain_sub(&temp);
1959 if (xfer->flags_int.isochronous_xfr) {
1960 temp.offset += xfer->frlengths[x - 1];
1961 temp.isoc_frame += temp.isoc_delta;
1963 /* get next Page Cache pointer */
1964 temp.pc = xfer->frbuffers + x;
1968 /* check if we should append a status stage */
1970 if (xfer->flags_int.control_xfr &&
1971 !xfer->flags_int.control_act) {
1974 * Send a DATA1 message and invert the current
1975 * endpoint direction.
1977 temp.step_td = (xfer->nframes != 0);
1978 temp.direction = UE_GET_DIR(xfer->endpointno) ^ UE_DIR_IN;
1982 temp.last_frame = 1;
1983 temp.trb_type = XHCI_TRB_TYPE_STATUS_STAGE;
1985 xhci_setup_generic_chain_sub(&temp);
1990 /* must have at least one frame! */
1992 xfer->td_transfer_last = td;
1994 DPRINTF("first=%p last=%p\n", xfer->td_transfer_first, td);
1998 xhci_set_slot_pointer(struct xhci_softc *sc, uint8_t index, uint64_t dev_addr)
2000 struct usb_page_search buf_res;
2001 struct xhci_dev_ctx_addr *pdctxa;
2003 usbd_get_page(&sc->sc_hw.ctx_pc, 0, &buf_res);
2005 pdctxa = buf_res.buffer;
2007 DPRINTF("addr[%u]=0x%016llx\n", index, (long long)dev_addr);
2009 pdctxa->qwBaaDevCtxAddr[index] = htole64(dev_addr);
2011 usb_pc_cpu_flush(&sc->sc_hw.ctx_pc);
2015 xhci_configure_mask(struct usb_device *udev, uint32_t mask, uint8_t drop)
2017 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2018 struct usb_page_search buf_inp;
2019 struct xhci_input_dev_ctx *pinp;
2022 index = udev->controller_slot_id;
2024 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
2026 pinp = buf_inp.buffer;
2029 mask &= XHCI_INCTX_NON_CTRL_MASK;
2030 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx0, mask);
2031 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx1, 0);
2033 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx0, 0);
2034 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx1, mask);
2040 xhci_configure_endpoint(struct usb_device *udev,
2041 struct usb_endpoint_descriptor *edesc, uint64_t ring_addr,
2042 uint16_t interval, uint8_t max_packet_count, uint8_t mult,
2043 uint8_t fps_shift, uint16_t max_packet_size, uint16_t max_frame_size)
2045 struct usb_page_search buf_inp;
2046 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2047 struct xhci_input_dev_ctx *pinp;
2053 index = udev->controller_slot_id;
2055 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
2057 pinp = buf_inp.buffer;
2059 epno = edesc->bEndpointAddress;
2060 type = edesc->bmAttributes & UE_XFERTYPE;
2062 if (type == UE_CONTROL)
2065 epno = XHCI_EPNO2EPID(epno);
2068 return (USB_ERR_NO_PIPE); /* invalid */
2070 if (max_packet_count == 0)
2071 return (USB_ERR_BAD_BUFSIZE);
2076 return (USB_ERR_BAD_BUFSIZE);
2078 temp = XHCI_EPCTX_0_EPSTATE_SET(0) |
2079 XHCI_EPCTX_0_MAXP_STREAMS_SET(0) |
2080 XHCI_EPCTX_0_LSA_SET(0);
2082 switch (udev->speed) {
2083 case USB_SPEED_FULL:
2096 temp |= XHCI_EPCTX_0_IVAL_SET(fps_shift);
2098 case UE_ISOCHRONOUS:
2099 temp |= XHCI_EPCTX_0_IVAL_SET(fps_shift);
2101 switch (udev->speed) {
2102 case USB_SPEED_SUPER:
2105 temp |= XHCI_EPCTX_0_MULT_SET(mult - 1);
2106 max_packet_count /= mult;
2116 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx0, temp);
2119 XHCI_EPCTX_1_HID_SET(0) |
2120 XHCI_EPCTX_1_MAXB_SET(max_packet_count) |
2121 XHCI_EPCTX_1_MAXP_SIZE_SET(max_packet_size);
2123 if ((udev->parent_hs_hub != NULL) || (udev->address != 0)) {
2124 if (type != UE_ISOCHRONOUS)
2125 temp |= XHCI_EPCTX_1_CERR_SET(3);
2130 temp |= XHCI_EPCTX_1_EPTYPE_SET(4);
2132 case UE_ISOCHRONOUS:
2133 temp |= XHCI_EPCTX_1_EPTYPE_SET(1);
2136 temp |= XHCI_EPCTX_1_EPTYPE_SET(2);
2139 temp |= XHCI_EPCTX_1_EPTYPE_SET(3);
2143 /* check for IN direction */
2145 temp |= XHCI_EPCTX_1_EPTYPE_SET(4);
2147 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx1, temp);
2149 ring_addr |= XHCI_EPCTX_2_DCS_SET(1);
2151 xhci_ctx_set_le64(sc, &pinp->ctx_ep[epno - 1].qwEpCtx2, ring_addr);
2153 switch (edesc->bmAttributes & UE_XFERTYPE) {
2155 case UE_ISOCHRONOUS:
2156 temp = XHCI_EPCTX_4_MAX_ESIT_PAYLOAD_SET(max_frame_size) |
2157 XHCI_EPCTX_4_AVG_TRB_LEN_SET(MIN(XHCI_PAGE_SIZE,
2161 temp = XHCI_EPCTX_4_AVG_TRB_LEN_SET(8);
2164 temp = XHCI_EPCTX_4_AVG_TRB_LEN_SET(XHCI_PAGE_SIZE);
2168 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx4, temp);
2171 xhci_dump_endpoint(sc, &pinp->ctx_ep[epno - 1]);
2173 usb_pc_cpu_flush(&sc->sc_hw.devs[index].input_pc);
2175 return (0); /* success */
2179 xhci_configure_endpoint_by_xfer(struct usb_xfer *xfer)
2181 struct xhci_endpoint_ext *pepext;
2182 struct usb_endpoint_ss_comp_descriptor *ecomp;
2184 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2185 xfer->endpoint->edesc);
2187 ecomp = xfer->endpoint->ecomp;
2189 pepext->trb[0].dwTrb3 = 0; /* halt any transfers */
2190 usb_pc_cpu_flush(pepext->page_cache);
2192 return (xhci_configure_endpoint(xfer->xroot->udev,
2193 xfer->endpoint->edesc, pepext->physaddr,
2194 xfer->interval, xfer->max_packet_count,
2195 (ecomp != NULL) ? (ecomp->bmAttributes & 3) + 1 : 1,
2196 usbd_xfer_get_fps_shift(xfer), xfer->max_packet_size,
2197 xfer->max_frame_size));
2201 xhci_configure_device(struct usb_device *udev)
2203 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2204 struct usb_page_search buf_inp;
2205 struct usb_page_cache *pcinp;
2206 struct xhci_input_dev_ctx *pinp;
2207 struct usb_device *hubdev;
2215 index = udev->controller_slot_id;
2217 DPRINTF("index=%u\n", index);
2219 pcinp = &sc->sc_hw.devs[index].input_pc;
2221 usbd_get_page(pcinp, 0, &buf_inp);
2223 pinp = buf_inp.buffer;
2228 /* figure out route string and root HUB port number */
2230 for (hubdev = udev; hubdev != NULL; hubdev = hubdev->parent_hub) {
2232 if (hubdev->parent_hub == NULL)
2235 depth = hubdev->parent_hub->depth;
2238 * NOTE: HS/FS/LS devices and the SS root HUB can have
2239 * more than 15 ports
2242 rh_port = hubdev->port_no;
2251 route |= rh_port << (4 * (depth - 1));
2254 DPRINTF("Route=0x%08x\n", route);
2256 temp = XHCI_SCTX_0_ROUTE_SET(route);
2258 switch (sc->sc_hw.devs[index].state) {
2259 case XHCI_ST_CONFIGURED:
2260 temp |= XHCI_SCTX_0_CTX_NUM_SET(XHCI_MAX_ENDPOINTS - 1);
2263 temp |= XHCI_SCTX_0_CTX_NUM_SET(1);
2267 switch (udev->speed) {
2269 temp |= XHCI_SCTX_0_SPEED_SET(2);
2271 case USB_SPEED_HIGH:
2272 temp |= XHCI_SCTX_0_SPEED_SET(3);
2274 case USB_SPEED_FULL:
2275 temp |= XHCI_SCTX_0_SPEED_SET(1);
2278 temp |= XHCI_SCTX_0_SPEED_SET(4);
2282 is_hub = sc->sc_hw.devs[index].nports != 0 &&
2283 (udev->speed == USB_SPEED_SUPER ||
2284 udev->speed == USB_SPEED_HIGH);
2287 temp |= XHCI_SCTX_0_HUB_SET(1);
2289 if (udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) {
2290 DPRINTF("HUB supports MTT\n");
2291 temp |= XHCI_SCTX_0_MTT_SET(1);
2296 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx0, temp);
2298 temp = XHCI_SCTX_1_RH_PORT_SET(rh_port);
2301 temp |= XHCI_SCTX_1_NUM_PORTS_SET(
2302 sc->sc_hw.devs[index].nports);
2305 switch (udev->speed) {
2306 case USB_SPEED_SUPER:
2307 switch (sc->sc_hw.devs[index].state) {
2308 case XHCI_ST_ADDRESSED:
2309 case XHCI_ST_CONFIGURED:
2310 /* enable power save */
2311 temp |= XHCI_SCTX_1_MAX_EL_SET(sc->sc_exit_lat_max);
2314 /* disable power save */
2322 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx1, temp);
2324 temp = XHCI_SCTX_2_IRQ_TARGET_SET(0);
2327 temp |= XHCI_SCTX_2_TT_THINK_TIME_SET(sc->sc_hw.devs[index].tt);
2329 hubdev = udev->parent_hs_hub;
2331 /* check if we should activate the transaction translator */
2332 switch (udev->speed) {
2333 case USB_SPEED_FULL:
2335 if (hubdev != NULL) {
2336 temp |= XHCI_SCTX_2_TT_HUB_SID_SET(
2337 hubdev->controller_slot_id);
2338 temp |= XHCI_SCTX_2_TT_PORT_NUM_SET(
2346 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx2, temp);
2348 temp = XHCI_SCTX_3_DEV_ADDR_SET(udev->address) |
2349 XHCI_SCTX_3_SLOT_STATE_SET(0);
2351 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx3, temp);
2354 xhci_dump_device(sc, &pinp->ctx_slot);
2356 usb_pc_cpu_flush(pcinp);
2358 return (0); /* success */
2362 xhci_alloc_device_ext(struct usb_device *udev)
2364 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2365 struct usb_page_search buf_dev;
2366 struct usb_page_search buf_ep;
2367 struct xhci_trb *trb;
2368 struct usb_page_cache *pc;
2369 struct usb_page *pg;
2374 index = udev->controller_slot_id;
2376 pc = &sc->sc_hw.devs[index].device_pc;
2377 pg = &sc->sc_hw.devs[index].device_pg;
2379 /* need to initialize the page cache */
2380 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2382 if (usb_pc_alloc_mem(pc, pg, sc->sc_ctx_is_64_byte ?
2383 (2 * sizeof(struct xhci_dev_ctx)) :
2384 sizeof(struct xhci_dev_ctx), XHCI_PAGE_SIZE))
2387 usbd_get_page(pc, 0, &buf_dev);
2389 pc = &sc->sc_hw.devs[index].input_pc;
2390 pg = &sc->sc_hw.devs[index].input_pg;
2392 /* need to initialize the page cache */
2393 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2395 if (usb_pc_alloc_mem(pc, pg, sc->sc_ctx_is_64_byte ?
2396 (2 * sizeof(struct xhci_input_dev_ctx)) :
2397 sizeof(struct xhci_input_dev_ctx), XHCI_PAGE_SIZE))
2400 pc = &sc->sc_hw.devs[index].endpoint_pc;
2401 pg = &sc->sc_hw.devs[index].endpoint_pg;
2403 /* need to initialize the page cache */
2404 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2406 if (usb_pc_alloc_mem(pc, pg, sizeof(struct xhci_dev_endpoint_trbs), XHCI_PAGE_SIZE))
2409 /* initialise all endpoint LINK TRBs */
2411 for (i = 0; i != XHCI_MAX_ENDPOINTS; i++) {
2413 /* lookup endpoint TRB ring */
2414 usbd_get_page(pc, (uintptr_t)&((struct xhci_dev_endpoint_trbs *)0)->trb[i][0], &buf_ep);
2416 /* get TRB pointer */
2417 trb = buf_ep.buffer;
2418 trb += XHCI_MAX_TRANSFERS - 1;
2420 /* get TRB start address */
2421 addr = buf_ep.physaddr;
2423 /* create LINK TRB */
2424 trb->qwTrb0 = htole64(addr);
2425 trb->dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
2426 trb->dwTrb3 = htole32(XHCI_TRB_3_CYCLE_BIT |
2427 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
2430 usb_pc_cpu_flush(pc);
2432 xhci_set_slot_pointer(sc, index, buf_dev.physaddr);
2437 xhci_free_device_ext(udev);
2439 return (USB_ERR_NOMEM);
2443 xhci_free_device_ext(struct usb_device *udev)
2445 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2448 index = udev->controller_slot_id;
2449 xhci_set_slot_pointer(sc, index, 0);
2451 usb_pc_free_mem(&sc->sc_hw.devs[index].device_pc);
2452 usb_pc_free_mem(&sc->sc_hw.devs[index].input_pc);
2453 usb_pc_free_mem(&sc->sc_hw.devs[index].endpoint_pc);
2456 static struct xhci_endpoint_ext *
2457 xhci_get_endpoint_ext(struct usb_device *udev, struct usb_endpoint_descriptor *edesc)
2459 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2460 struct xhci_endpoint_ext *pepext;
2461 struct usb_page_cache *pc;
2462 struct usb_page_search buf_ep;
2466 epno = edesc->bEndpointAddress;
2467 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL)
2470 epno = XHCI_EPNO2EPID(epno);
2472 index = udev->controller_slot_id;
2474 pc = &sc->sc_hw.devs[index].endpoint_pc;
2476 usbd_get_page(pc, (uintptr_t)&((struct xhci_dev_endpoint_trbs *)0)->trb[epno][0], &buf_ep);
2478 pepext = &sc->sc_hw.devs[index].endp[epno];
2479 pepext->page_cache = pc;
2480 pepext->trb = buf_ep.buffer;
2481 pepext->physaddr = buf_ep.physaddr;
2487 xhci_endpoint_doorbell(struct usb_xfer *xfer)
2489 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
2493 epno = xfer->endpointno;
2494 if (xfer->flags_int.control_xfr)
2497 epno = XHCI_EPNO2EPID(epno);
2498 index = xfer->xroot->udev->controller_slot_id;
2500 if (xfer->xroot->udev->flags.self_suspended == 0)
2501 XWRITE4(sc, door, XHCI_DOORBELL(index), epno | XHCI_DB_SID_SET(0));
2505 xhci_transfer_remove(struct usb_xfer *xfer, usb_error_t error)
2507 struct xhci_endpoint_ext *pepext;
2509 if (xfer->flags_int.bandwidth_reclaimed) {
2510 xfer->flags_int.bandwidth_reclaimed = 0;
2512 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2513 xfer->endpoint->edesc);
2517 pepext->xfer[xfer->qh_pos] = NULL;
2519 if (error && pepext->trb_running != 0) {
2520 pepext->trb_halted = 1;
2521 pepext->trb_running = 0;
2527 xhci_transfer_insert(struct usb_xfer *xfer)
2529 struct xhci_td *td_first;
2530 struct xhci_td *td_last;
2531 struct xhci_endpoint_ext *pepext;
2539 /* check if already inserted */
2540 if (xfer->flags_int.bandwidth_reclaimed) {
2541 DPRINTFN(8, "Already in schedule\n");
2545 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2546 xfer->endpoint->edesc);
2548 td_first = xfer->td_transfer_first;
2549 td_last = xfer->td_transfer_last;
2550 addr = pepext->physaddr;
2552 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
2555 /* single buffered */
2559 /* multi buffered */
2560 trb_limit = (XHCI_MAX_TRANSFERS - 2);
2564 if (pepext->trb_used >= trb_limit) {
2565 DPRINTFN(8, "Too many TDs queued.\n");
2566 return (USB_ERR_NOMEM);
2569 /* check for stopped condition, after putting transfer on interrupt queue */
2570 if (pepext->trb_running == 0) {
2571 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
2573 DPRINTFN(8, "Not running\n");
2575 /* start configuration */
2576 (void)usb_proc_msignal(&sc->sc_config_proc,
2577 &sc->sc_config_msg[0], &sc->sc_config_msg[1]);
2583 /* get current TRB index */
2584 i = pepext->trb_index;
2586 /* get next TRB index */
2589 /* the last entry of the ring is a hardcoded link TRB */
2590 if (inext >= (XHCI_MAX_TRANSFERS - 1))
2593 /* compute terminating return address */
2594 addr += inext * sizeof(struct xhci_trb);
2596 /* update next pointer of last link TRB */
2597 td_last->td_trb[td_last->ntrb].qwTrb0 = htole64(addr);
2598 td_last->td_trb[td_last->ntrb].dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
2599 td_last->td_trb[td_last->ntrb].dwTrb3 = htole32(XHCI_TRB_3_IOC_BIT |
2600 XHCI_TRB_3_CYCLE_BIT | XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
2603 xhci_dump_trb(&td_last->td_trb[td_last->ntrb]);
2605 usb_pc_cpu_flush(td_last->page_cache);
2607 /* write ahead chain end marker */
2609 pepext->trb[inext].qwTrb0 = 0;
2610 pepext->trb[inext].dwTrb2 = 0;
2611 pepext->trb[inext].dwTrb3 = 0;
2613 /* update next pointer of link TRB */
2615 pepext->trb[i].qwTrb0 = htole64((uint64_t)td_first->td_self);
2616 pepext->trb[i].dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
2619 xhci_dump_trb(&pepext->trb[i]);
2621 usb_pc_cpu_flush(pepext->page_cache);
2623 /* toggle cycle bit which activates the transfer chain */
2625 pepext->trb[i].dwTrb3 = htole32(XHCI_TRB_3_CYCLE_BIT |
2626 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
2628 usb_pc_cpu_flush(pepext->page_cache);
2630 DPRINTF("qh_pos = %u\n", i);
2632 pepext->xfer[i] = xfer;
2636 xfer->flags_int.bandwidth_reclaimed = 1;
2638 pepext->trb_index = inext;
2640 xhci_endpoint_doorbell(xfer);
2646 xhci_root_intr(struct xhci_softc *sc)
2650 USB_BUS_LOCK_ASSERT(&sc->sc_bus);
2652 /* clear any old interrupt data */
2653 memset(sc->sc_hub_idata, 0, sizeof(sc->sc_hub_idata));
2655 for (i = 1; i <= sc->sc_noport; i++) {
2656 /* pick out CHANGE bits from the status register */
2657 if (XREAD4(sc, oper, XHCI_PORTSC(i)) & (
2658 XHCI_PS_CSC | XHCI_PS_PEC |
2659 XHCI_PS_OCC | XHCI_PS_WRC |
2660 XHCI_PS_PRC | XHCI_PS_PLC |
2662 sc->sc_hub_idata[i / 8] |= 1 << (i % 8);
2663 DPRINTF("port %d changed\n", i);
2666 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
2667 sizeof(sc->sc_hub_idata));
2670 /*------------------------------------------------------------------------*
2671 * xhci_device_done - XHCI done handler
2673 * NOTE: This function can be called two times in a row on
2674 * the same USB transfer. From close and from interrupt.
2675 *------------------------------------------------------------------------*/
2677 xhci_device_done(struct usb_xfer *xfer, usb_error_t error)
2679 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
2680 xfer, xfer->endpoint, error);
2682 /* remove transfer from HW queue */
2683 xhci_transfer_remove(xfer, error);
2685 /* dequeue transfer and start next transfer */
2686 usbd_transfer_done(xfer, error);
2689 /*------------------------------------------------------------------------*
2690 * XHCI data transfer support (generic type)
2691 *------------------------------------------------------------------------*/
2693 xhci_device_generic_open(struct usb_xfer *xfer)
2695 if (xfer->flags_int.isochronous_xfr) {
2696 switch (xfer->xroot->udev->speed) {
2697 case USB_SPEED_FULL:
2700 usb_hs_bandwidth_alloc(xfer);
2707 xhci_device_generic_close(struct usb_xfer *xfer)
2711 xhci_device_done(xfer, USB_ERR_CANCELLED);
2713 if (xfer->flags_int.isochronous_xfr) {
2714 switch (xfer->xroot->udev->speed) {
2715 case USB_SPEED_FULL:
2718 usb_hs_bandwidth_free(xfer);
2725 xhci_device_generic_multi_enter(struct usb_endpoint *ep,
2726 struct usb_xfer *enter_xfer)
2728 struct usb_xfer *xfer;
2730 /* check if there is a current transfer */
2731 xfer = ep->endpoint_q.curr;
2736 * Check if the current transfer is started and then pickup
2737 * the next one, if any. Else wait for next start event due to
2738 * block on failure feature.
2740 if (!xfer->flags_int.bandwidth_reclaimed)
2743 xfer = TAILQ_FIRST(&ep->endpoint_q.head);
2746 * In case of enter we have to consider that the
2747 * transfer is queued by the USB core after the enter
2756 /* try to multi buffer */
2757 xhci_transfer_insert(xfer);
2761 xhci_device_generic_enter(struct usb_xfer *xfer)
2765 /* setup TD's and QH */
2766 xhci_setup_generic_chain(xfer);
2768 xhci_device_generic_multi_enter(xfer->endpoint, xfer);
2772 xhci_device_generic_start(struct usb_xfer *xfer)
2776 /* try to insert xfer on HW queue */
2777 xhci_transfer_insert(xfer);
2779 /* try to multi buffer */
2780 xhci_device_generic_multi_enter(xfer->endpoint, NULL);
2782 /* add transfer last on interrupt queue */
2783 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
2785 /* start timeout, if any */
2786 if (xfer->timeout != 0)
2787 usbd_transfer_timeout_ms(xfer, &xhci_timeout, xfer->timeout);
2790 struct usb_pipe_methods xhci_device_generic_methods =
2792 .open = xhci_device_generic_open,
2793 .close = xhci_device_generic_close,
2794 .enter = xhci_device_generic_enter,
2795 .start = xhci_device_generic_start,
2798 /*------------------------------------------------------------------------*
2799 * xhci root HUB support
2800 *------------------------------------------------------------------------*
2801 * Simulate a hardware HUB by handling all the necessary requests.
2802 *------------------------------------------------------------------------*/
2804 #define HSETW(ptr, val) ptr[0] = (uint8_t)(val), ptr[1] = (uint8_t)((val) >> 8)
2807 struct usb_device_descriptor xhci_devd =
2809 .bLength = sizeof(xhci_devd),
2810 .bDescriptorType = UDESC_DEVICE, /* type */
2811 HSETW(.bcdUSB, 0x0300), /* USB version */
2812 .bDeviceClass = UDCLASS_HUB, /* class */
2813 .bDeviceSubClass = UDSUBCLASS_HUB, /* subclass */
2814 .bDeviceProtocol = UDPROTO_SSHUB, /* protocol */
2815 .bMaxPacketSize = 9, /* max packet size */
2816 HSETW(.idVendor, 0x0000), /* vendor */
2817 HSETW(.idProduct, 0x0000), /* product */
2818 HSETW(.bcdDevice, 0x0100), /* device version */
2822 .bNumConfigurations = 1, /* # of configurations */
2826 struct xhci_bos_desc xhci_bosd = {
2828 .bLength = sizeof(xhci_bosd.bosd),
2829 .bDescriptorType = UDESC_BOS,
2830 HSETW(.wTotalLength, sizeof(xhci_bosd)),
2831 .bNumDeviceCaps = 3,
2834 .bLength = sizeof(xhci_bosd.usb2extd),
2835 .bDescriptorType = 1,
2836 .bDevCapabilityType = 2,
2837 .bmAttributes[0] = 2,
2840 .bLength = sizeof(xhci_bosd.usbdcd),
2841 .bDescriptorType = UDESC_DEVICE_CAPABILITY,
2842 .bDevCapabilityType = 3,
2843 .bmAttributes = 0, /* XXX */
2844 HSETW(.wSpeedsSupported, 0x000C),
2845 .bFunctionalitySupport = 8,
2846 .bU1DevExitLat = 255, /* dummy - not used */
2847 .wU2DevExitLat[0] = 0x00,
2848 .wU2DevExitLat[1] = 0x08,
2851 .bLength = sizeof(xhci_bosd.cidd),
2852 .bDescriptorType = 1,
2853 .bDevCapabilityType = 4,
2855 .bContainerID = 0, /* XXX */
2860 struct xhci_config_desc xhci_confd = {
2862 .bLength = sizeof(xhci_confd.confd),
2863 .bDescriptorType = UDESC_CONFIG,
2864 .wTotalLength[0] = sizeof(xhci_confd),
2866 .bConfigurationValue = 1,
2867 .iConfiguration = 0,
2868 .bmAttributes = UC_SELF_POWERED,
2869 .bMaxPower = 0 /* max power */
2872 .bLength = sizeof(xhci_confd.ifcd),
2873 .bDescriptorType = UDESC_INTERFACE,
2875 .bInterfaceClass = UICLASS_HUB,
2876 .bInterfaceSubClass = UISUBCLASS_HUB,
2877 .bInterfaceProtocol = 0,
2880 .bLength = sizeof(xhci_confd.endpd),
2881 .bDescriptorType = UDESC_ENDPOINT,
2882 .bEndpointAddress = UE_DIR_IN | XHCI_INTR_ENDPT,
2883 .bmAttributes = UE_INTERRUPT,
2884 .wMaxPacketSize[0] = 2, /* max 15 ports */
2888 .bLength = sizeof(xhci_confd.endpcd),
2889 .bDescriptorType = UDESC_ENDPOINT_SS_COMP,
2896 struct usb_hub_ss_descriptor xhci_hubd = {
2897 .bLength = sizeof(xhci_hubd),
2898 .bDescriptorType = UDESC_SS_HUB,
2902 xhci_roothub_exec(struct usb_device *udev,
2903 struct usb_device_request *req, const void **pptr, uint16_t *plength)
2905 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2906 const char *str_ptr;
2917 USB_BUS_LOCK_ASSERT(&sc->sc_bus);
2920 ptr = (const void *)&sc->sc_hub_desc;
2924 value = UGETW(req->wValue);
2925 index = UGETW(req->wIndex);
2927 DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
2928 "wValue=0x%04x wIndex=0x%04x\n",
2929 req->bmRequestType, req->bRequest,
2930 UGETW(req->wLength), value, index);
2932 #define C(x,y) ((x) | ((y) << 8))
2933 switch (C(req->bRequest, req->bmRequestType)) {
2934 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2935 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2936 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2938 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2939 * for the integrated root hub.
2942 case C(UR_GET_CONFIG, UT_READ_DEVICE):
2944 sc->sc_hub_desc.temp[0] = sc->sc_conf;
2946 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2947 switch (value >> 8) {
2949 if ((value & 0xff) != 0) {
2950 err = USB_ERR_IOERROR;
2953 len = sizeof(xhci_devd);
2954 ptr = (const void *)&xhci_devd;
2958 if ((value & 0xff) != 0) {
2959 err = USB_ERR_IOERROR;
2962 len = sizeof(xhci_bosd);
2963 ptr = (const void *)&xhci_bosd;
2967 if ((value & 0xff) != 0) {
2968 err = USB_ERR_IOERROR;
2971 len = sizeof(xhci_confd);
2972 ptr = (const void *)&xhci_confd;
2976 switch (value & 0xff) {
2977 case 0: /* Language table */
2981 case 1: /* Vendor */
2982 str_ptr = sc->sc_vendor;
2985 case 2: /* Product */
2986 str_ptr = "XHCI root HUB";
2994 len = usb_make_str_desc(
2995 sc->sc_hub_desc.temp,
2996 sizeof(sc->sc_hub_desc.temp),
3001 err = USB_ERR_IOERROR;
3005 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3007 sc->sc_hub_desc.temp[0] = 0;
3009 case C(UR_GET_STATUS, UT_READ_DEVICE):
3011 USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
3013 case C(UR_GET_STATUS, UT_READ_INTERFACE):
3014 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3016 USETW(sc->sc_hub_desc.stat.wStatus, 0);
3018 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3019 if (value >= XHCI_MAX_DEVICES) {
3020 err = USB_ERR_IOERROR;
3024 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
3025 if (value != 0 && value != 1) {
3026 err = USB_ERR_IOERROR;
3029 sc->sc_conf = value;
3031 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
3033 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
3034 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
3035 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3036 err = USB_ERR_IOERROR;
3038 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
3040 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
3043 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3045 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3046 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE\n");
3049 (index > sc->sc_noport)) {
3050 err = USB_ERR_IOERROR;
3053 port = XHCI_PORTSC(index);
3055 v = XREAD4(sc, oper, port);
3056 i = XHCI_PS_PLS_GET(v);
3057 v &= ~XHCI_PS_CLEAR;
3060 case UHF_C_BH_PORT_RESET:
3061 XWRITE4(sc, oper, port, v | XHCI_PS_WRC);
3063 case UHF_C_PORT_CONFIG_ERROR:
3064 XWRITE4(sc, oper, port, v | XHCI_PS_CEC);
3066 case UHF_C_PORT_SUSPEND:
3067 case UHF_C_PORT_LINK_STATE:
3068 XWRITE4(sc, oper, port, v | XHCI_PS_PLC);
3070 case UHF_C_PORT_CONNECTION:
3071 XWRITE4(sc, oper, port, v | XHCI_PS_CSC);
3073 case UHF_C_PORT_ENABLE:
3074 XWRITE4(sc, oper, port, v | XHCI_PS_PEC);
3076 case UHF_C_PORT_OVER_CURRENT:
3077 XWRITE4(sc, oper, port, v | XHCI_PS_OCC);
3079 case UHF_C_PORT_RESET:
3080 XWRITE4(sc, oper, port, v | XHCI_PS_PRC);
3082 case UHF_PORT_ENABLE:
3083 XWRITE4(sc, oper, port, v | XHCI_PS_PED);
3085 case UHF_PORT_POWER:
3086 XWRITE4(sc, oper, port, v & ~XHCI_PS_PP);
3088 case UHF_PORT_INDICATOR:
3089 XWRITE4(sc, oper, port, v & ~XHCI_PS_PIC_SET(3));
3091 case UHF_PORT_SUSPEND:
3095 XWRITE4(sc, oper, port, v |
3096 XHCI_PS_PLS_SET(0xF) | XHCI_PS_LWS);
3099 /* wait 20ms for resume sequence to complete */
3100 usb_pause_mtx(&sc->sc_bus.bus_lock, hz / 50);
3103 XWRITE4(sc, oper, port, v |
3104 XHCI_PS_PLS_SET(0) | XHCI_PS_LWS);
3107 err = USB_ERR_IOERROR;
3112 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3113 if ((value & 0xff) != 0) {
3114 err = USB_ERR_IOERROR;
3118 v = XREAD4(sc, capa, XHCI_HCSPARAMS0);
3120 sc->sc_hub_desc.hubd = xhci_hubd;
3122 sc->sc_hub_desc.hubd.bNbrPorts = sc->sc_noport;
3124 if (XHCI_HCS0_PPC(v))
3125 i = UHD_PWR_INDIVIDUAL;
3129 if (XHCI_HCS0_PIND(v))
3132 i |= UHD_OC_INDIVIDUAL;
3134 USETW(sc->sc_hub_desc.hubd.wHubCharacteristics, i);
3136 /* see XHCI section 5.4.9: */
3137 sc->sc_hub_desc.hubd.bPwrOn2PwrGood = 10;
3139 for (j = 1; j <= sc->sc_noport; j++) {
3141 v = XREAD4(sc, oper, XHCI_PORTSC(j));
3142 if (v & XHCI_PS_DR) {
3143 sc->sc_hub_desc.hubd.
3144 DeviceRemovable[j / 8] |= 1U << (j % 8);
3147 len = sc->sc_hub_desc.hubd.bLength;
3150 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3152 memset(sc->sc_hub_desc.temp, 0, 16);
3155 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3156 DPRINTFN(9, "UR_GET_STATUS i=%d\n", index);
3159 (index > sc->sc_noport)) {
3160 err = USB_ERR_IOERROR;
3164 v = XREAD4(sc, oper, XHCI_PORTSC(index));
3166 DPRINTFN(9, "port status=0x%08x\n", v);
3168 i = UPS_PORT_LINK_STATE_SET(XHCI_PS_PLS_GET(v));
3170 switch (XHCI_PS_SPEED_GET(v)) {
3172 i |= UPS_HIGH_SPEED;
3181 i |= UPS_OTHER_SPEED;
3185 if (v & XHCI_PS_CCS)
3186 i |= UPS_CURRENT_CONNECT_STATUS;
3187 if (v & XHCI_PS_PED)
3188 i |= UPS_PORT_ENABLED;
3189 if (v & XHCI_PS_OCA)
3190 i |= UPS_OVERCURRENT_INDICATOR;
3193 if (v & XHCI_PS_PP) {
3195 * The USB 3.0 RH is using the
3196 * USB 2.0's power bit
3198 i |= UPS_PORT_POWER;
3200 USETW(sc->sc_hub_desc.ps.wPortStatus, i);
3203 if (v & XHCI_PS_CSC)
3204 i |= UPS_C_CONNECT_STATUS;
3205 if (v & XHCI_PS_PEC)
3206 i |= UPS_C_PORT_ENABLED;
3207 if (v & XHCI_PS_OCC)
3208 i |= UPS_C_OVERCURRENT_INDICATOR;
3209 if (v & XHCI_PS_WRC)
3210 i |= UPS_C_BH_PORT_RESET;
3211 if (v & XHCI_PS_PRC)
3212 i |= UPS_C_PORT_RESET;
3213 if (v & XHCI_PS_PLC)
3214 i |= UPS_C_PORT_LINK_STATE;
3215 if (v & XHCI_PS_CEC)
3216 i |= UPS_C_PORT_CONFIG_ERROR;
3218 USETW(sc->sc_hub_desc.ps.wPortChange, i);
3219 len = sizeof(sc->sc_hub_desc.ps);
3222 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3223 err = USB_ERR_IOERROR;
3226 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3229 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3235 (index > sc->sc_noport)) {
3236 err = USB_ERR_IOERROR;
3240 port = XHCI_PORTSC(index);
3241 v = XREAD4(sc, oper, port) & ~XHCI_PS_CLEAR;
3244 case UHF_PORT_U1_TIMEOUT:
3245 if (XHCI_PS_SPEED_GET(v) != 4) {
3246 err = USB_ERR_IOERROR;
3249 port = XHCI_PORTPMSC(index);
3250 v = XREAD4(sc, oper, port);
3251 v &= ~XHCI_PM3_U1TO_SET(0xFF);
3252 v |= XHCI_PM3_U1TO_SET(i);
3253 XWRITE4(sc, oper, port, v);
3255 case UHF_PORT_U2_TIMEOUT:
3256 if (XHCI_PS_SPEED_GET(v) != 4) {
3257 err = USB_ERR_IOERROR;
3260 port = XHCI_PORTPMSC(index);
3261 v = XREAD4(sc, oper, port);
3262 v &= ~XHCI_PM3_U2TO_SET(0xFF);
3263 v |= XHCI_PM3_U2TO_SET(i);
3264 XWRITE4(sc, oper, port, v);
3266 case UHF_BH_PORT_RESET:
3267 XWRITE4(sc, oper, port, v | XHCI_PS_WPR);
3269 case UHF_PORT_LINK_STATE:
3270 XWRITE4(sc, oper, port, v |
3271 XHCI_PS_PLS_SET(i) | XHCI_PS_LWS);
3272 /* 4ms settle time */
3273 usb_pause_mtx(&sc->sc_bus.bus_lock, hz / 250);
3275 case UHF_PORT_ENABLE:
3276 DPRINTFN(3, "set port enable %d\n", index);
3278 case UHF_PORT_SUSPEND:
3279 DPRINTFN(6, "suspend port %u (LPM=%u)\n", index, i);
3280 j = XHCI_PS_SPEED_GET(v);
3281 if ((j < 1) || (j > 3)) {
3282 /* non-supported speed */
3283 err = USB_ERR_IOERROR;
3286 XWRITE4(sc, oper, port, v |
3287 XHCI_PS_PLS_SET(i ? 2 /* LPM */ : 3) | XHCI_PS_LWS);
3289 case UHF_PORT_RESET:
3290 DPRINTFN(6, "reset port %d\n", index);
3291 XWRITE4(sc, oper, port, v | XHCI_PS_PR);
3293 case UHF_PORT_POWER:
3294 DPRINTFN(3, "set port power %d\n", index);
3295 XWRITE4(sc, oper, port, v | XHCI_PS_PP);
3298 DPRINTFN(3, "set port test %d\n", index);
3300 case UHF_PORT_INDICATOR:
3301 DPRINTFN(3, "set port indicator %d\n", index);
3303 v &= ~XHCI_PS_PIC_SET(3);
3304 v |= XHCI_PS_PIC_SET(1);
3306 XWRITE4(sc, oper, port, v);
3309 err = USB_ERR_IOERROR;
3314 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
3315 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
3316 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
3317 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
3320 err = USB_ERR_IOERROR;
3330 xhci_xfer_setup(struct usb_setup_params *parm)
3332 struct usb_page_search page_info;
3333 struct usb_page_cache *pc;
3334 struct xhci_softc *sc;
3335 struct usb_xfer *xfer;
3340 sc = XHCI_BUS2SC(parm->udev->bus);
3341 xfer = parm->curr_xfer;
3344 * The proof for the "ntd" formula is illustrated like this:
3346 * +------------------------------------+
3350 * | | xxx | x | frm 0 |
3352 * | | xxx | xx | frm 1 |
3355 * +------------------------------------+
3357 * "xxx" means a completely full USB transfer descriptor
3359 * "x" and "xx" means a short USB packet
3361 * For the remainder of an USB transfer modulo
3362 * "max_data_length" we need two USB transfer descriptors.
3363 * One to transfer the remaining data and one to finalise with
3364 * a zero length packet in case the "force_short_xfer" flag is
3365 * set. We only need two USB transfer descriptors in the case
3366 * where the transfer length of the first one is a factor of
3367 * "max_frame_size". The rest of the needed USB transfer
3368 * descriptors is given by the buffer size divided by the
3369 * maximum data payload.
3371 parm->hc_max_packet_size = 0x400;
3372 parm->hc_max_packet_count = 16 * 3;
3373 parm->hc_max_frame_size = XHCI_TD_PAYLOAD_MAX;
3375 xfer->flags_int.bdma_enable = 1;
3377 usbd_transfer_setup_sub(parm);
3379 if (xfer->flags_int.isochronous_xfr) {
3380 ntd = ((1 * xfer->nframes)
3381 + (xfer->max_data_length / xfer->max_hc_frame_size));
3382 } else if (xfer->flags_int.control_xfr) {
3383 ntd = ((2 * xfer->nframes) + 1 /* STATUS */
3384 + (xfer->max_data_length / xfer->max_hc_frame_size));
3386 ntd = ((2 * xfer->nframes)
3387 + (xfer->max_data_length / xfer->max_hc_frame_size));
3396 * Allocate queue heads and transfer descriptors
3400 if (usbd_transfer_setup_sub_malloc(
3401 parm, &pc, sizeof(struct xhci_td),
3402 XHCI_TD_ALIGN, ntd)) {
3403 parm->err = USB_ERR_NOMEM;
3407 for (n = 0; n != ntd; n++) {
3410 usbd_get_page(pc + n, 0, &page_info);
3412 td = page_info.buffer;
3415 td->td_self = page_info.physaddr;
3416 td->obj_next = last_obj;
3417 td->page_cache = pc + n;
3421 usb_pc_cpu_flush(pc + n);
3424 xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
3426 if (!xfer->flags_int.curr_dma_set) {
3427 xfer->flags_int.curr_dma_set = 1;
3433 xhci_configure_reset_endpoint(struct usb_xfer *xfer)
3435 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
3436 struct usb_page_search buf_inp;
3437 struct usb_device *udev;
3438 struct xhci_endpoint_ext *pepext;
3439 struct usb_endpoint_descriptor *edesc;
3440 struct usb_page_cache *pcinp;
3445 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
3446 xfer->endpoint->edesc);
3448 udev = xfer->xroot->udev;
3449 index = udev->controller_slot_id;
3451 pcinp = &sc->sc_hw.devs[index].input_pc;
3453 usbd_get_page(pcinp, 0, &buf_inp);
3455 edesc = xfer->endpoint->edesc;
3457 epno = edesc->bEndpointAddress;
3459 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL)
3462 epno = XHCI_EPNO2EPID(epno);
3465 return (USB_ERR_NO_PIPE); /* invalid */
3469 /* configure endpoint */
3471 err = xhci_configure_endpoint_by_xfer(xfer);
3474 XHCI_CMD_UNLOCK(sc);
3479 * Get the endpoint into the stopped state according to the
3480 * endpoint context state diagram in the XHCI specification:
3483 err = xhci_cmd_stop_ep(sc, 0, epno, index);
3486 DPRINTF("Could not stop endpoint %u\n", epno);
3488 err = xhci_cmd_reset_ep(sc, 0, epno, index);
3491 DPRINTF("Could not reset endpoint %u\n", epno);
3493 err = xhci_cmd_set_tr_dequeue_ptr(sc, pepext->physaddr |
3494 XHCI_EPCTX_2_DCS_SET(1), 0, epno, index);
3497 DPRINTF("Could not set dequeue ptr for endpoint %u\n", epno);
3500 * Get the endpoint into the running state according to the
3501 * endpoint context state diagram in the XHCI specification:
3504 xhci_configure_mask(udev, 1U << epno, 0);
3506 err = xhci_cmd_evaluate_ctx(sc, buf_inp.physaddr, index);
3509 DPRINTF("Could not configure endpoint %u\n", epno);
3511 err = xhci_cmd_configure_ep(sc, buf_inp.physaddr, 0, index);
3514 DPRINTF("Could not configure endpoint %u\n", epno);
3516 XHCI_CMD_UNLOCK(sc);
3522 xhci_xfer_unsetup(struct usb_xfer *xfer)
3528 xhci_start_dma_delay(struct usb_xfer *xfer)
3530 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
3532 /* put transfer on interrupt queue (again) */
3533 usbd_transfer_enqueue(&sc->sc_bus.intr_q, xfer);
3535 (void)usb_proc_msignal(&sc->sc_config_proc,
3536 &sc->sc_config_msg[0], &sc->sc_config_msg[1]);
3540 xhci_configure_msg(struct usb_proc_msg *pm)
3542 struct xhci_softc *sc;
3543 struct xhci_endpoint_ext *pepext;
3544 struct usb_xfer *xfer;
3546 sc = XHCI_BUS2SC(((struct usb_bus_msg *)pm)->bus);
3549 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3551 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
3552 xfer->endpoint->edesc);
3554 if ((pepext->trb_halted != 0) ||
3555 (pepext->trb_running == 0)) {
3559 /* clear halted and running */
3560 pepext->trb_halted = 0;
3561 pepext->trb_running = 0;
3563 /* nuke remaining buffered transfers */
3565 for (i = 0; i != (XHCI_MAX_TRANSFERS - 1); i++) {
3567 * NOTE: We need to use the timeout
3568 * error code here else existing
3569 * isochronous clients can get
3572 if (pepext->xfer[i] != NULL) {
3573 xhci_device_done(pepext->xfer[i],
3579 * NOTE: The USB transfer cannot vanish in
3583 USB_BUS_UNLOCK(&sc->sc_bus);
3585 xhci_configure_reset_endpoint(xfer);
3587 USB_BUS_LOCK(&sc->sc_bus);
3589 /* check if halted is still cleared */
3590 if (pepext->trb_halted == 0) {
3591 pepext->trb_running = 1;
3592 pepext->trb_index = 0;
3597 if (xfer->flags_int.did_dma_delay) {
3599 /* remove transfer from interrupt queue (again) */
3600 usbd_transfer_dequeue(xfer);
3602 /* we are finally done */
3603 usb_dma_delay_done_cb(xfer);
3605 /* queue changed - restart */
3610 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3612 /* try to insert xfer on HW queue */
3613 xhci_transfer_insert(xfer);
3615 /* try to multi buffer */
3616 xhci_device_generic_multi_enter(xfer->endpoint, NULL);
3621 xhci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
3622 struct usb_endpoint *ep)
3624 struct xhci_endpoint_ext *pepext;
3626 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d\n",
3627 ep, udev->address, edesc->bEndpointAddress, udev->flags.usb_mode);
3629 if (udev->flags.usb_mode != USB_MODE_HOST) {
3633 if (udev->parent_hub == NULL) {
3634 /* root HUB has special endpoint handling */
3638 ep->methods = &xhci_device_generic_methods;
3640 pepext = xhci_get_endpoint_ext(udev, edesc);
3642 USB_BUS_LOCK(udev->bus);
3643 pepext->trb_halted = 1;
3644 pepext->trb_running = 0;
3645 USB_BUS_UNLOCK(udev->bus);
3649 xhci_ep_uninit(struct usb_device *udev, struct usb_endpoint *ep)
3655 xhci_ep_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
3657 struct xhci_endpoint_ext *pepext;
3661 if (udev->flags.usb_mode != USB_MODE_HOST) {
3665 if (udev->parent_hub == NULL) {
3666 /* root HUB has special endpoint handling */
3670 pepext = xhci_get_endpoint_ext(udev, ep->edesc);
3672 USB_BUS_LOCK(udev->bus);
3673 pepext->trb_halted = 1;
3674 pepext->trb_running = 0;
3675 USB_BUS_UNLOCK(udev->bus);
3679 xhci_device_init(struct usb_device *udev)
3681 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3685 /* no init for root HUB */
3686 if (udev->parent_hub == NULL)
3691 /* set invalid default */
3693 udev->controller_slot_id = sc->sc_noslot + 1;
3695 /* try to get a new slot ID from the XHCI */
3697 err = xhci_cmd_enable_slot(sc, &temp);
3700 XHCI_CMD_UNLOCK(sc);
3704 if (temp > sc->sc_noslot) {
3705 XHCI_CMD_UNLOCK(sc);
3706 return (USB_ERR_BAD_ADDRESS);
3709 if (sc->sc_hw.devs[temp].state != XHCI_ST_DISABLED) {
3710 DPRINTF("slot %u already allocated.\n", temp);
3711 XHCI_CMD_UNLOCK(sc);
3712 return (USB_ERR_BAD_ADDRESS);
3715 /* store slot ID for later reference */
3717 udev->controller_slot_id = temp;
3719 /* reset data structure */
3721 memset(&sc->sc_hw.devs[temp], 0, sizeof(sc->sc_hw.devs[0]));
3723 /* set mark slot allocated */
3725 sc->sc_hw.devs[temp].state = XHCI_ST_ENABLED;
3727 err = xhci_alloc_device_ext(udev);
3729 XHCI_CMD_UNLOCK(sc);
3731 /* get device into default state */
3734 err = xhci_set_address(udev, NULL, 0);
3740 xhci_device_uninit(struct usb_device *udev)
3742 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3745 /* no init for root HUB */
3746 if (udev->parent_hub == NULL)
3751 index = udev->controller_slot_id;
3753 if (index <= sc->sc_noslot) {
3754 xhci_cmd_disable_slot(sc, index);
3755 sc->sc_hw.devs[index].state = XHCI_ST_DISABLED;
3757 /* free device extension */
3758 xhci_free_device_ext(udev);
3761 XHCI_CMD_UNLOCK(sc);
3765 xhci_get_dma_delay(struct usb_device *udev, uint32_t *pus)
3768 * Wait until the hardware has finished any possible use of
3769 * the transfer descriptor(s)
3771 *pus = 2048; /* microseconds */
3775 xhci_device_resume(struct usb_device *udev)
3777 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3783 /* check for root HUB */
3784 if (udev->parent_hub == NULL)
3787 index = udev->controller_slot_id;
3791 /* blindly resume all endpoints */
3793 USB_BUS_LOCK(udev->bus);
3795 for (n = 1; n != XHCI_MAX_ENDPOINTS; n++)
3796 XWRITE4(sc, door, XHCI_DOORBELL(index), n | XHCI_DB_SID_SET(0));
3798 USB_BUS_UNLOCK(udev->bus);
3800 XHCI_CMD_UNLOCK(sc);
3804 xhci_device_suspend(struct usb_device *udev)
3806 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3813 /* check for root HUB */
3814 if (udev->parent_hub == NULL)
3817 index = udev->controller_slot_id;
3821 /* blindly suspend all endpoints */
3823 for (n = 1; n != XHCI_MAX_ENDPOINTS; n++) {
3824 err = xhci_cmd_stop_ep(sc, 1, n, index);
3826 DPRINTF("Failed to suspend endpoint "
3827 "%u on slot %u (ignored).\n", n, index);
3831 XHCI_CMD_UNLOCK(sc);
3835 xhci_set_hw_power(struct usb_bus *bus)
3841 xhci_device_state_change(struct usb_device *udev)
3843 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3844 struct usb_page_search buf_inp;
3848 /* check for root HUB */
3849 if (udev->parent_hub == NULL)
3852 index = udev->controller_slot_id;
3856 if (usb_get_device_state(udev) == USB_STATE_CONFIGURED) {
3857 err = uhub_query_info(udev, &sc->sc_hw.devs[index].nports,
3858 &sc->sc_hw.devs[index].tt);
3860 sc->sc_hw.devs[index].nports = 0;
3865 switch (usb_get_device_state(udev)) {
3866 case USB_STATE_POWERED:
3867 if (sc->sc_hw.devs[index].state == XHCI_ST_DEFAULT)
3870 sc->sc_hw.devs[index].state = XHCI_ST_DEFAULT;
3872 err = xhci_cmd_reset_dev(sc, index);
3875 DPRINTF("Device reset failed "
3876 "for slot %u.\n", index);
3880 case USB_STATE_ADDRESSED:
3881 if (sc->sc_hw.devs[index].state == XHCI_ST_ADDRESSED)
3884 sc->sc_hw.devs[index].state = XHCI_ST_ADDRESSED;
3886 err = xhci_cmd_configure_ep(sc, 0, 1, index);
3889 DPRINTF("Failed to deconfigure "
3890 "slot %u.\n", index);
3894 case USB_STATE_CONFIGURED:
3895 if (sc->sc_hw.devs[index].state == XHCI_ST_CONFIGURED)
3898 sc->sc_hw.devs[index].state = XHCI_ST_CONFIGURED;
3900 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
3902 xhci_configure_mask(udev, 1, 0);
3904 err = xhci_configure_device(udev);
3906 DPRINTF("Could not configure device "
3907 "at slot %u.\n", index);
3910 err = xhci_cmd_evaluate_ctx(sc, buf_inp.physaddr, index);
3912 DPRINTF("Could not evaluate device "
3913 "context at slot %u.\n", index);
3920 XHCI_CMD_UNLOCK(sc);
3923 struct usb_bus_methods xhci_bus_methods = {
3924 .endpoint_init = xhci_ep_init,
3925 .endpoint_uninit = xhci_ep_uninit,
3926 .xfer_setup = xhci_xfer_setup,
3927 .xfer_unsetup = xhci_xfer_unsetup,
3928 .get_dma_delay = xhci_get_dma_delay,
3929 .device_init = xhci_device_init,
3930 .device_uninit = xhci_device_uninit,
3931 .device_resume = xhci_device_resume,
3932 .device_suspend = xhci_device_suspend,
3933 .set_hw_power = xhci_set_hw_power,
3934 .roothub_exec = xhci_roothub_exec,
3935 .xfer_poll = xhci_do_poll,
3936 .start_dma_delay = xhci_start_dma_delay,
3937 .set_address = xhci_set_address,
3938 .clear_stall = xhci_ep_clear_stall,
3939 .device_state_change = xhci_device_state_change,
3940 .set_hw_power_sleep = xhci_set_hw_power_sleep,