#include <sys/usb/hcd/xhci/xhci.h>
#include <sys/sdt.h>
boolean_t
xhci_endpoint_is_periodic_in(xhci_endpoint_t *xep)
{
usba_pipe_handle_data_t *ph;
ASSERT(xep != NULL);
ph = xep->xep_pipe;
ASSERT(ph != NULL);
return ((xep->xep_type == USB_EP_ATTR_INTR ||
xep->xep_type == USB_EP_ATTR_ISOCH) &&
(ph->p_ep.bEndpointAddress & USB_EP_DIR_MASK) == USB_EP_DIR_IN);
}
static int
xhci_input_context_sync(xhci_t *xhcip, xhci_device_t *xd, xhci_endpoint_t *xep)
{
XHCI_DMA_SYNC(xd->xd_ictx, DDI_DMA_SYNC_FORDEV);
if (xhci_check_dma_handle(xhcip, &xd->xd_ictx) != DDI_FM_OK) {
xhci_error(xhcip, "failed to initialize device input "
"context on slot %d and port %d for endpoint %u: "
"encountered fatal FM error synchronizing input context "
"DMA memory", xd->xd_slot, xd->xd_port, xep->xep_num);
xhci_fm_runtime_reset(xhcip);
return (EIO);
}
return (0);
}
uint_t
xhci_endpoint_pipe_to_epid(usba_pipe_handle_data_t *ph)
{
int ep;
ep = ph->p_ep.bEndpointAddress & USB_EP_NUM_MASK;
if (ep == 0)
return (ep);
ep = ep * 2 - 1;
if ((ph->p_ep.bEndpointAddress & USB_EP_DIR_MASK) == USB_EP_DIR_IN)
ep++;
VERIFY(ep < XHCI_NUM_ENDPOINTS);
return (ep);
}
void
xhci_endpoint_timeout_cancel(xhci_t *xhcip, xhci_endpoint_t *xep)
{
xep->xep_state |= XHCI_ENDPOINT_TEARDOWN;
if (xep->xep_timeout != 0) {
mutex_exit(&xhcip->xhci_lock);
(void) untimeout(xep->xep_timeout);
mutex_enter(&xhcip->xhci_lock);
xep->xep_timeout = 0;
}
}
void
xhci_endpoint_close(xhci_t *xhcip, xhci_endpoint_t *xep)
{
VERIFY(MUTEX_HELD(&xhcip->xhci_lock));
VERIFY3U(xep->xep_num, !=, XHCI_DEFAULT_ENDPOINT);
VERIFY(list_is_empty(&xep->xep_transfers));
VERIFY(xep->xep_pipe != NULL);
xep->xep_pipe = NULL;
VERIFY(xep->xep_state & XHCI_ENDPOINT_OPEN);
xep->xep_state &= ~XHCI_ENDPOINT_OPEN;
xhci_endpoint_timeout_cancel(xhcip, xep);
}
int
xhci_endpoint_unconfigure(xhci_t *xhcip, xhci_device_t *xd,
xhci_endpoint_t *xep)
{
int ret;
VERIFY(MUTEX_HELD(&xhcip->xhci_lock));
VERIFY3U(xep->xep_num, !=, XHCI_DEFAULT_ENDPOINT);
VERIFY(!(xep->xep_state & XHCI_ENDPOINT_OPEN));
VERIFY(xep->xep_state & XHCI_ENDPOINT_TEARDOWN);
VERIFY(xep->xep_type == USB_EP_ATTR_INTR ||
xep->xep_type == USB_EP_ATTR_ISOCH);
mutex_enter(&xd->xd_imtx);
xd->xd_input->xic_drop_flags =
LE_32(XHCI_INCTX_MASK_DCI(xep->xep_num + 1));
xd->xd_input->xic_add_flags = LE_32(XHCI_INCTX_MASK_DCI(0));
ret = xhci_input_context_sync(xhcip, xd, xep);
mutex_exit(&xhcip->xhci_lock);
if (ret != 0) {
ret = USB_HC_HARDWARE_ERROR;
goto done;
}
ret = xhci_command_configure_endpoint(xhcip, xd);
done:
mutex_exit(&xd->xd_imtx);
mutex_enter(&xhcip->xhci_lock);
return (ret);
}
void
xhci_endpoint_fini(xhci_device_t *xd, int endpoint)
{
xhci_endpoint_t *xep = xd->xd_endpoints[endpoint];
VERIFY(xep != NULL);
VERIFY3P(xep->xep_pipe, ==, NULL);
xd->xd_endpoints[endpoint] = NULL;
if (endpoint != XHCI_DEFAULT_ENDPOINT) {
VERIFY(!(xep->xep_state & XHCI_ENDPOINT_OPEN));
}
xhci_ring_free(&xep->xep_ring);
cv_destroy(&xep->xep_state_cv);
list_destroy(&xep->xep_transfers);
kmem_free(xep, sizeof (xhci_endpoint_t));
}
int
xhci_endpoint_setup_default_context(xhci_t *xhcip, xhci_device_t *xd,
xhci_endpoint_t *xep)
{
uint_t mps;
xhci_endpoint_context_t *ectx;
uint64_t deq;
ectx = xd->xd_endin[xep->xep_num];
VERIFY(ectx != NULL);
switch (xd->xd_usbdev->usb_port_status) {
case USBA_LOW_SPEED_DEV:
if (xd->xd_usbdev->usb_dev_descr != NULL) {
mps = xd->xd_usbdev->usb_dev_descr->bMaxPacketSize0;
} else {
mps = 8;
}
break;
case USBA_FULL_SPEED_DEV:
case USBA_HIGH_SPEED_DEV:
if (xd->xd_usbdev->usb_dev_descr != NULL) {
mps = xd->xd_usbdev->usb_dev_descr->bMaxPacketSize0;
} else {
mps = 64;
}
break;
case USBA_SUPER_SPEED_DEV:
default:
if (xd->xd_usbdev->usb_dev_descr != NULL) {
mps = xd->xd_usbdev->usb_dev_descr->bMaxPacketSize0;
mps = 1 << mps;
} else {
mps = 512;
}
break;
}
bzero(ectx, sizeof (xhci_endpoint_context_t));
ectx->xec_info = LE_32(0);
ectx->xec_info2 = LE_32(XHCI_EPCTX_SET_CERR(3) |
XHCI_EPCTX_SET_EPTYPE(XHCI_EPCTX_TYPE_CTRL) |
XHCI_EPCTX_SET_MAXB(0) | XHCI_EPCTX_SET_MPS(mps));
deq = xhci_dma_pa(&xep->xep_ring.xr_dma) + sizeof (xhci_trb_t) *
xep->xep_ring.xr_tail;
ectx->xec_dequeue = LE_64(deq | xep->xep_ring.xr_cycle);
ectx->xec_txinfo = LE_32(XHCI_EPCTX_MAX_ESIT_PAYLOAD(0) |
XHCI_EPCTX_AVG_TRB_LEN(XHCI_CONTEXT_DEF_CTRL_ATL));
return (xhci_input_context_sync(xhcip, xd, xep));
}
int
xhci_endpoint_update_default(xhci_t *xhcip, xhci_device_t *xd,
xhci_endpoint_t *xep)
{
int mps, desc, info, ret;
ASSERT(xd->xd_usbdev != NULL);
mps = XHCI_EPCTX_GET_MPS(xd->xd_endout[xep->xep_num]->xec_info2);
desc = xd->xd_usbdev->usb_dev_descr->bMaxPacketSize0;
if (xd->xd_usbdev->usb_port_status >= USBA_SUPER_SPEED_DEV) {
desc = 1 << desc;
}
if (mps == desc)
return (USB_SUCCESS);
mutex_enter(&xd->xd_imtx);
info = LE_32(xd->xd_endout[xep->xep_num]->xec_info2);
info &= ~XHCI_EPCTX_SET_MPS(mps);
info |= XHCI_EPCTX_SET_MPS(desc);
xd->xd_endin[xep->xep_num]->xec_info2 = LE_32(info);
xd->xd_input->xic_drop_flags = LE_32(0);
xd->xd_input->xic_add_flags = LE_32(XHCI_INCTX_MASK_DCI(1));
if (xhci_input_context_sync(xhcip, xd, xep) != 0) {
ret = USB_HC_HARDWARE_ERROR;
goto done;
}
ret = xhci_command_evaluate_context(xhcip, xd);
done:
mutex_exit(&xd->xd_imtx);
return (ret);
}
static uint_t
xhci_endpoint_epdesc_to_type(usb_ep_descr_t *ep)
{
int type = ep->bmAttributes & USB_EP_ATTR_MASK;
boolean_t in = (ep->bEndpointAddress & USB_EP_DIR_MASK) ==
USB_EP_DIR_IN;
switch (type) {
case USB_EP_ATTR_CONTROL:
return (XHCI_EPCTX_TYPE_CTRL);
case USB_EP_ATTR_ISOCH:
if (in == B_TRUE)
return (XHCI_EPCTX_TYPE_ISOCH_IN);
return (XHCI_EPCTX_TYPE_ISOCH_OUT);
case USB_EP_ATTR_BULK:
if (in == B_TRUE)
return (XHCI_EPCTX_TYPE_BULK_IN);
return (XHCI_EPCTX_TYPE_BULK_OUT);
case USB_EP_ATTR_INTR:
if (in == B_TRUE)
return (XHCI_EPCTX_TYPE_INTR_IN);
return (XHCI_EPCTX_TYPE_INTR_OUT);
default:
panic("bad USB attribute type: %d", type);
}
}
static uint_t
xhci_endpoint_determine_burst(xhci_device_t *xd, xhci_endpoint_t *xep)
{
switch (xd->xd_usbdev->usb_port_status) {
case USBA_LOW_SPEED_DEV:
case USBA_FULL_SPEED_DEV:
return (0);
case USBA_HIGH_SPEED_DEV:
if (xep->xep_type == USB_EP_ATTR_CONTROL ||
xep->xep_type == USB_EP_ATTR_BULK)
return (0);
return ((xep->xep_pipe->p_xep.uex_ep.wMaxPacketSize &
XHCI_CONTEXT_BURST_MASK) >> XHCI_CONTEXT_BURST_SHIFT);
default:
ASSERT(xep->xep_pipe->p_xep.uex_flags & USB_EP_XFLAGS_SS_COMP);
return (xep->xep_pipe->p_xep.uex_ep_ss.bMaxBurst);
}
}
static uint_t
xhci_endpoint_linear_interval(usb_ep_descr_t *ep)
{
int exp;
int ival = ep->bInterval;
if (ival < 1)
ival = 1;
if (ival > 255)
ival = 255;
exp = ddi_fls(ival) - 1;
ASSERT(exp >= 0 && exp <= 7);
return (exp);
}
static uint_t
xhci_endpoint_exponential_interval(usb_ep_descr_t *ep)
{
int ival;
ival = ep->bInterval;
if (ival < 1)
ival = 1;
if (ival > 16)
ival = 16;
ival--;
ASSERT(ival >= 0 && ival <= 15);
return (ival);
}
static uint_t
xhci_endpoint_interval(xhci_device_t *xd, usb_ep_descr_t *ep)
{
int type = ep->bmAttributes & USB_EP_ATTR_MASK;
int speed = xd->xd_usbdev->usb_port_status;
if (type == USB_EP_ATTR_CONTROL || type == USB_EP_ATTR_BULK) {
if (speed != USBA_HIGH_SPEED_DEV)
return (0);
return (xhci_endpoint_linear_interval(ep));
}
switch (speed) {
case USBA_LOW_SPEED_DEV:
if (type == USB_EP_ATTR_ISOCH) {
return (xhci_endpoint_exponential_interval(ep) + 3);
}
case USBA_FULL_SPEED_DEV:
return (xhci_endpoint_linear_interval(ep) + 3);
case USBA_HIGH_SPEED_DEV:
case USBA_SUPER_SPEED_DEV:
default:
return (xhci_endpoint_exponential_interval(ep));
}
}
static uint_t
xhci_endpoint_max_esit(xhci_device_t *xd, xhci_endpoint_t *xep, uint_t mps,
uint_t burst)
{
if (xep->xep_type == USB_EP_ATTR_CONTROL ||
xep->xep_type == USB_EP_ATTR_BULK) {
return (0);
}
if (xd->xd_usbdev->usb_port_status >= USBA_SUPER_SPEED_DEV) {
usb_ep_xdescr_t *ep_xdesc = &xep->xep_pipe->p_xep;
ASSERT(xep->xep_pipe->p_xep.uex_flags & USB_EP_XFLAGS_SS_COMP);
return (ep_xdesc->uex_ep_ss.wBytesPerInterval);
}
return (mps * (burst + 1));
}
static uint_t
xhci_endpoint_avg_trb(xhci_t *xhcip, usb_ep_descr_t *ep, int mps)
{
int type = ep->bmAttributes & USB_EP_ATTR_MASK;
switch (type) {
case USB_EP_ATTR_ISOCH:
case USB_EP_ATTR_INTR:
return (MIN(xhcip->xhci_caps.xcap_pagesize, mps));
case USB_EP_ATTR_CONTROL:
return (XHCI_CONTEXT_DEF_CTRL_ATL);
case USB_EP_ATTR_BULK:
return (xhcip->xhci_caps.xcap_pagesize);
default:
panic("bad USB endpoint type: %d", type);
}
}
int
xhci_endpoint_setup_context(xhci_t *xhcip, xhci_device_t *xd,
xhci_endpoint_t *xep)
{
xhci_endpoint_params_t new_xepp;
xhci_endpoint_context_t *ectx;
uint64_t deq;
int ret;
bzero(&new_xepp, sizeof (new_xepp));
new_xepp.xepp_configured = B_TRUE;
if (xd->xd_usbdev->usb_port_status >= USBA_SUPER_SPEED_DEV &&
(xep->xep_pipe->p_xep.uex_flags & USB_EP_XFLAGS_SS_COMP) == 0) {
const char *prod, *mfg;
prod = xd->xd_usbdev->usb_product_str;
if (prod == NULL)
prod = "Unknown Device";
mfg = xd->xd_usbdev->usb_mfg_str;
if (mfg == NULL)
mfg = "Unknown Manufacturer";
xhci_log(xhcip, "Encountered USB >=3.0 device without endpoint "
"companion descriptor. Ensure driver %s is properly using "
"usb_pipe_xopen() for device %s %s",
ddi_driver_name(xd->xd_usbdev->usb_dip), prod, mfg);
return (EINVAL);
}
ectx = xd->xd_endin[xep->xep_num];
VERIFY(ectx != NULL);
VERIFY(xd->xd_usbdev->usb_dev_descr != NULL);
VERIFY(xep->xep_pipe != NULL);
new_xepp.xepp_mps =
xep->xep_pipe->p_ep.wMaxPacketSize & XHCI_CONTEXT_MPS_MASK;
new_xepp.xepp_mult = XHCI_CONTEXT_DEF_MULT;
new_xepp.xepp_cerr = XHCI_CONTEXT_DEF_CERR;
switch (xep->xep_type) {
case USB_EP_ATTR_ISOCH:
if (xd->xd_usbdev->usb_port_status >= USBA_SUPER_SPEED_DEV) {
ASSERT(xep->xep_pipe->p_xep.uex_flags &
USB_EP_XFLAGS_SS_COMP);
new_xepp.xepp_mult =
xep->xep_pipe->p_xep.uex_ep_ss.bmAttributes &
USB_EP_SS_COMP_ISOC_MULT_MASK;
}
new_xepp.xepp_mps &= XHCI_CONTEXT_MPS_MASK;
new_xepp.xepp_cerr = XHCI_CONTEXT_ISOCH_CERR;
break;
default:
break;
}
new_xepp.xepp_eptype = xhci_endpoint_epdesc_to_type(
&xep->xep_pipe->p_xep.uex_ep);
new_xepp.xepp_burst = xhci_endpoint_determine_burst(xd, xep);
new_xepp.xepp_ival = xhci_endpoint_interval(xd,
&xep->xep_pipe->p_xep.uex_ep);
new_xepp.xepp_max_esit = xhci_endpoint_max_esit(xd, xep,
new_xepp.xepp_mps, new_xepp.xepp_burst);
new_xepp.xepp_avgtrb = xhci_endpoint_avg_trb(xhcip,
&xep->xep_pipe->p_xep.uex_ep, new_xepp.xepp_mps);
if (xhcip->xhci_caps.xcap_flags2 & XCAP2_LEC)
new_xepp.xepp_mult = 0;
if (xep->xep_params.xepp_configured) {
if (bcmp(&xep->xep_params, &new_xepp, sizeof (new_xepp)) == 0) {
return (0);
}
DTRACE_PROBE3(xhci__context__mismatch,
xhci_t *, xhcip,
xhci_endpoint_t *, xep,
xhci_endpoint_params_t *, &new_xepp);
xhci_error(xhcip, "device input context on slot %d and "
"port %d for endpoint %u was already initialized but "
"with incompatible parameters",
xd->xd_slot, xd->xd_port, xep->xep_num);
return (EINVAL);
}
bzero(ectx, sizeof (xhci_endpoint_context_t));
ectx->xec_info = LE_32(XHCI_EPCTX_SET_MULT(new_xepp.xepp_mult) |
XHCI_EPCTX_SET_IVAL(new_xepp.xepp_ival));
if (xhcip->xhci_caps.xcap_flags2 & XCAP2_LEC) {
ectx->xec_info |=
LE_32(XHCI_EPCTX_SET_MAX_ESIT_HI(new_xepp.xepp_max_esit));
}
ectx->xec_info2 = LE_32(XHCI_EPCTX_SET_CERR(new_xepp.xepp_cerr) |
XHCI_EPCTX_SET_EPTYPE(new_xepp.xepp_eptype) |
XHCI_EPCTX_SET_MAXB(new_xepp.xepp_burst) |
XHCI_EPCTX_SET_MPS(new_xepp.xepp_mps));
deq = xhci_dma_pa(&xep->xep_ring.xr_dma) + sizeof (xhci_trb_t) *
xep->xep_ring.xr_tail;
ectx->xec_dequeue = LE_64(deq | xep->xep_ring.xr_cycle);
ectx->xec_txinfo = LE_32(
XHCI_EPCTX_MAX_ESIT_PAYLOAD(new_xepp.xepp_max_esit) |
XHCI_EPCTX_AVG_TRB_LEN(new_xepp.xepp_avgtrb));
if ((ret = xhci_input_context_sync(xhcip, xd, xep)) != 0) {
return (ret);
}
bcopy(&new_xepp, &xep->xep_params, sizeof (new_xepp));
VERIFY(xep->xep_params.xepp_configured);
return (0);
}
int
xhci_endpoint_init(xhci_t *xhcip, xhci_device_t *xd,
usba_pipe_handle_data_t *ph)
{
int ret;
uint_t epid;
xhci_endpoint_t *xep;
if (ph == NULL) {
epid = XHCI_DEFAULT_ENDPOINT;
} else {
ASSERT(MUTEX_HELD(&xhcip->xhci_lock));
epid = xhci_endpoint_pipe_to_epid(ph);
}
VERIFY(xd->xd_endpoints[epid] == NULL);
xep = kmem_zalloc(sizeof (xhci_endpoint_t), KM_SLEEP);
list_create(&xep->xep_transfers, sizeof (xhci_transfer_t),
offsetof(xhci_transfer_t, xt_link));
cv_init(&xep->xep_state_cv, NULL, CV_DRIVER, NULL);
xep->xep_xd = xd;
xep->xep_xhci = xhcip;
xep->xep_num = epid;
if (ph == NULL) {
xep->xep_pipe = NULL;
xep->xep_type = USB_EP_ATTR_CONTROL;
} else {
xep->xep_pipe = ph;
xep->xep_type = ph->p_ep.bmAttributes & USB_EP_ATTR_MASK;
}
if ((ret = xhci_ring_alloc(xhcip, &xep->xep_ring)) != 0) {
cv_destroy(&xep->xep_state_cv);
list_destroy(&xep->xep_transfers);
kmem_free(xep, sizeof (xhci_endpoint_t));
return (ret);
}
if ((ret = xhci_ring_reset(xhcip, &xep->xep_ring)) != 0) {
xhci_ring_free(&xep->xep_ring);
cv_destroy(&xep->xep_state_cv);
list_destroy(&xep->xep_transfers);
kmem_free(xep, sizeof (xhci_endpoint_t));
return (ret);
}
xd->xd_endpoints[epid] = xep;
if (ph == NULL) {
ret = xhci_endpoint_setup_default_context(xhcip, xd, xep);
} else {
ret = xhci_endpoint_setup_context(xhcip, xd, xep);
}
if (ret != 0) {
xhci_endpoint_fini(xd, xep->xep_num);
return (ret);
}
xep->xep_state |= XHCI_ENDPOINT_OPEN;
return (0);
}
int
xhci_endpoint_reopen(xhci_t *xhcip, xhci_device_t *xd, xhci_endpoint_t *xep,
usba_pipe_handle_data_t *ph)
{
VERIFY(MUTEX_HELD(&xhcip->xhci_lock));
VERIFY(ph != NULL);
VERIFY3U(xhci_endpoint_pipe_to_epid(ph), ==, xep->xep_num);
VERIFY3U(xep->xep_num, !=, XHCI_DEFAULT_ENDPOINT);
if (xep->xep_type != (ph->p_ep.bmAttributes & USB_EP_ATTR_MASK)) {
return (EINVAL);
}
if (xep->xep_state & XHCI_ENDPOINT_OPEN) {
return (EBUSY);
}
VERIFY(xep->xep_state & XHCI_ENDPOINT_TEARDOWN);
xep->xep_state &= ~XHCI_ENDPOINT_TEARDOWN;
VERIFY3U(xep->xep_timeout, ==, 0);
VERIFY(list_is_empty(&xep->xep_transfers));
VERIFY3P(xep->xep_pipe, ==, NULL);
xep->xep_pipe = ph;
int ret;
if ((ret = xhci_endpoint_setup_context(xhcip, xd, xep)) != 0) {
xep->xep_pipe = NULL;
xhci_endpoint_timeout_cancel(xhcip, xep);
return (ret);
}
xep->xep_state |= XHCI_ENDPOINT_OPEN;
return (0);
}
void
xhci_endpoint_serialize(xhci_t *xhcip, xhci_endpoint_t *xep)
{
VERIFY(MUTEX_HELD(&xhcip->xhci_lock));
while ((xep->xep_state & XHCI_ENDPOINT_SERIALIZE) != 0) {
cv_wait(&xep->xep_state_cv, &xhcip->xhci_lock);
}
}
int
xhci_endpoint_quiesce(xhci_t *xhcip, xhci_device_t *xd, xhci_endpoint_t *xep)
{
int ret = USB_SUCCESS;
xhci_endpoint_context_t *epctx = xd->xd_endout[xep->xep_num];
ASSERT(MUTEX_HELD(&xhcip->xhci_lock));
ASSERT(xep->xep_state & XHCI_ENDPOINT_QUIESCE);
if (!(xep->xep_state & XHCI_ENDPOINT_HALTED)) {
mutex_exit(&xhcip->xhci_lock);
ret = xhci_command_stop_endpoint(xhcip, xd, xep);
mutex_enter(&xhcip->xhci_lock);
if (ret == USB_INVALID_CONTEXT) {
XHCI_DMA_SYNC(xd->xd_octx, DDI_DMA_SYNC_FORKERNEL);
}
}
if ((xep->xep_state & XHCI_ENDPOINT_HALTED) ||
(ret == USB_INVALID_CONTEXT &&
XHCI_EPCTX_STATE(LE_32(epctx->xec_info)) == XHCI_EP_HALTED)) {
mutex_exit(&xhcip->xhci_lock);
ret = xhci_command_reset_endpoint(xhcip, xd, xep);
mutex_enter(&xhcip->xhci_lock);
}
if (ret != USB_SUCCESS && ret != USB_INVALID_CONTEXT) {
return (ret);
}
if (ret == USB_INVALID_CONTEXT) {
XHCI_DMA_SYNC(xd->xd_octx, DDI_DMA_SYNC_FORKERNEL);
switch (XHCI_EPCTX_STATE(LE_32(epctx->xec_info))) {
case XHCI_EP_STOPPED:
case XHCI_EP_ERROR:
ret = USB_SUCCESS;
break;
case XHCI_EP_DISABLED:
case XHCI_EP_RUNNING:
case XHCI_EP_HALTED:
default:
xhci_error(xhcip, "!asked to stop endpoint %u on slot "
"%d and port %d: ended up in unexpected state %d",
xep->xep_num, xd->xd_slot, xd->xd_port,
XHCI_EPCTX_STATE(LE_32(epctx->xec_info)));
return (ret);
}
}
if (ret == USB_SUCCESS) {
xep->xep_state &= ~XHCI_ENDPOINT_HALTED;
}
return (ret);
}
int
xhci_endpoint_ring(xhci_t *xhcip, xhci_device_t *xd, xhci_endpoint_t *xep)
{
xhci_put32(xhcip, XHCI_R_DOOR, XHCI_DOORBELL(xd->xd_slot),
xep->xep_num + 1);
if (xhci_check_regs_acc(xhcip) != DDI_FM_OK) {
xhci_error(xhcip, "failed to ring doorbell for slot %d and "
"endpoint %u: encountered fatal FM register access error",
xd->xd_slot, xep->xep_num);
xhci_fm_runtime_reset(xhcip);
return (USB_HC_HARDWARE_ERROR);
}
DTRACE_PROBE3(xhci__doorbell__ring, xhci_t *, xhcip, uint32_t,
XHCI_DOORBELL(xd->xd_slot), uint32_t, xep->xep_num + 1);
return (USB_SUCCESS);
}
static void
xhci_endpoint_tick(void *arg)
{
int ret;
xhci_transfer_t *xt;
xhci_endpoint_t *xep = arg;
xhci_device_t *xd = xep->xep_xd;
xhci_t *xhcip = xep->xep_xhci;
mutex_enter(&xhcip->xhci_lock);
if (xep->xep_state & (XHCI_ENDPOINT_TEARDOWN |
XHCI_ENDPOINT_PERIODIC)) {
xep->xep_timeout = 0;
mutex_exit(&xhcip->xhci_lock);
return;
}
if (xep->xep_state & XHCI_ENDPOINT_DONT_SCHEDULE) {
xep->xep_timeout = timeout(xhci_endpoint_tick, xep,
drv_usectohz(XHCI_TICK_TIMEOUT_US));
mutex_exit(&xhcip->xhci_lock);
return;
}
xt = list_head(&xep->xep_transfers);
if (xt == NULL) {
xep->xep_timeout = 0;
mutex_exit(&xhcip->xhci_lock);
return;
}
if (xt->xt_timeout > 0) {
xt->xt_timeout--;
xep->xep_timeout = timeout(xhci_endpoint_tick, xep,
drv_usectohz(XHCI_TICK_TIMEOUT_US));
mutex_exit(&xhcip->xhci_lock);
return;
}
xep->xep_state |= XHCI_ENDPOINT_TIMED_OUT | XHCI_ENDPOINT_QUIESCE;
ret = xhci_endpoint_quiesce(xhcip, xd, xep);
if (ret != USB_SUCCESS) {
xep->xep_state &= ~(XHCI_ENDPOINT_QUIESCE |
XHCI_ENDPOINT_TIMED_OUT);
xep->xep_timeout = timeout(xhci_endpoint_tick, xep,
drv_usectohz(XHCI_TICK_TIMEOUT_US));
mutex_exit(&xhcip->xhci_lock);
cv_broadcast(&xep->xep_state_cv);
xhci_error(xhcip, "failed to successfully quiesce timed out "
"endpoint %u of device on slot %d and port %d: device "
"remains timed out", xep->xep_num, xd->xd_slot,
xd->xd_port);
return;
}
xhci_ring_skip_transfer(&xep->xep_ring, xt);
(void) list_remove_head(&xep->xep_transfers);
mutex_exit(&xhcip->xhci_lock);
ret = xhci_command_set_tr_dequeue(xhcip, xd, xep);
mutex_enter(&xhcip->xhci_lock);
xep->xep_state &= ~XHCI_ENDPOINT_QUIESCE;
if (ret == USB_SUCCESS) {
xep->xep_state &= ~XHCI_ENDPOINT_TIMED_OUT;
} else {
xhci_error(xhcip, "failed to successfully set transfer ring "
"dequeue pointer of timed out endpoint %u of "
"device on slot %d and port %d: device remains timed out, "
"please use cfgadm to recover", xep->xep_num, xd->xd_slot,
xd->xd_port);
}
xep->xep_timeout = timeout(xhci_endpoint_tick, xep,
drv_usectohz(XHCI_TICK_TIMEOUT_US));
mutex_exit(&xhcip->xhci_lock);
cv_broadcast(&xep->xep_state_cv);
ASSERT(xt->xt_usba_req != NULL);
usba_hcdi_cb(xep->xep_pipe, xt->xt_usba_req, USB_CR_TIMEOUT);
xhci_transfer_free(xhcip, xt);
}
int
xhci_endpoint_schedule(xhci_t *xhcip, xhci_device_t *xd, xhci_endpoint_t *xep,
xhci_transfer_t *xt, boolean_t ring)
{
int i;
xhci_ring_t *rp = &xep->xep_ring;
ASSERT(MUTEX_HELD(&xhcip->xhci_lock));
ASSERT(xt->xt_ntrbs > 0);
ASSERT(xt->xt_trbs != NULL);
if ((xep->xep_state & XHCI_ENDPOINT_DONT_SCHEDULE) != 0)
return (USB_FAILURE);
if (xhci_ring_trb_space(rp, xt->xt_ntrbs) == B_FALSE)
return (USB_NO_RESOURCES);
for (i = xt->xt_ntrbs - 1; i > 0; i--) {
xhci_ring_trb_fill(rp, i, &xt->xt_trbs[i], &xt->xt_trbs_pa[i],
B_TRUE);
}
xhci_ring_trb_fill(rp, 0U, &xt->xt_trbs[0], &xt->xt_trbs_pa[0],
B_FALSE);
XHCI_DMA_SYNC(rp->xr_dma, DDI_DMA_SYNC_FORDEV);
xhci_ring_trb_produce(rp, xt->xt_ntrbs);
list_insert_tail(&xep->xep_transfers, xt);
XHCI_DMA_SYNC(rp->xr_dma, DDI_DMA_SYNC_FORDEV);
if (xhci_check_dma_handle(xhcip, &rp->xr_dma) != DDI_FM_OK) {
xhci_error(xhcip, "failed to write out TRB for device on slot "
"%d, port %d, and endpoint %u: encountered fatal FM error "
"synchronizing ring DMA memory", xd->xd_slot, xd->xd_port,
xep->xep_num);
xhci_fm_runtime_reset(xhcip);
return (USB_HC_HARDWARE_ERROR);
}
if (xep->xep_timeout == 0 &&
!(xep->xep_state & XHCI_ENDPOINT_PERIODIC)) {
xep->xep_timeout = timeout(xhci_endpoint_tick, xep,
drv_usectohz(XHCI_TICK_TIMEOUT_US));
}
xt->xt_sched_time = gethrtime();
if (ring == B_FALSE)
return (USB_SUCCESS);
return (xhci_endpoint_ring(xhcip, xd, xep));
}
xhci_transfer_t *
xhci_endpoint_determine_transfer(xhci_t *xhcip, xhci_endpoint_t *xep,
xhci_trb_t *trb, uint_t *offp)
{
uint_t i;
uint64_t addr;
xhci_transfer_t *xt;
ASSERT(xhcip != NULL);
ASSERT(offp != NULL);
ASSERT(xep != NULL);
ASSERT(trb != NULL);
ASSERT(MUTEX_HELD(&xhcip->xhci_lock));
if ((xt = list_head(&xep->xep_transfers)) == NULL)
return (NULL);
addr = LE_64(trb->trb_addr);
if (XHCI_TRB_GET_ED(LE_32(trb->trb_flags)) != 0) {
if (LE_64(trb->trb_addr) != (uintptr_t)xt)
return (NULL);
*offp = xt->xt_ntrbs - 1;
return (xt);
}
for (i = 0; i < xt->xt_ntrbs; i++) {
if (xt->xt_trbs_pa[i] == addr)
break;
}
if (i == xt->xt_ntrbs)
return (NULL);
if (xhci_ring_trb_valid_range(&xep->xep_ring, LE_64(trb->trb_addr),
xt->xt_ntrbs) == -1)
return (NULL);
*offp = i;
return (xt);
}
static void
xhci_endpoint_reschedule_periodic(xhci_t *xhcip, xhci_device_t *xd,
xhci_endpoint_t *xep, xhci_transfer_t *xt)
{
int ret;
xhci_pipe_t *xp = (xhci_pipe_t *)xep->xep_pipe->p_hcd_private;
xhci_periodic_pipe_t *xpp = &xp->xp_periodic;
ASSERT3U(xpp->xpp_tsize, >, 0);
xt->xt_short = 0;
xt->xt_cr = USB_CR_OK;
mutex_enter(&xhcip->xhci_lock);
if (xpp->xpp_poll_state != XHCI_PERIODIC_POLL_ACTIVE) {
mutex_exit(&xhcip->xhci_lock);
return;
}
if (xep->xep_type == USB_EP_ATTR_ISOCH) {
int i;
for (i = 0; i < xt->xt_ntrbs; i++) {
xt->xt_isoc[i].isoc_pkt_actual_length =
xt->xt_isoc[i].isoc_pkt_length;
xt->xt_isoc[i].isoc_pkt_status = USB_CR_OK;
}
}
ret = xhci_endpoint_schedule(xhcip, xd, xep, xt, B_TRUE);
if (ret != 0) {
xhci_log(xhcip, "!failed to reschedule periodic endpoint %u "
"(type %u) on slot %d: %d", xep->xep_num, xep->xep_type,
xd->xd_slot, ret);
}
mutex_exit(&xhcip->xhci_lock);
}
static boolean_t
xhci_endpoint_control_callback(xhci_t *xhcip, xhci_device_t *xd,
xhci_endpoint_t *xep, xhci_transfer_t *xt, uint_t off, xhci_trb_t *trb)
{
int code;
usb_ctrl_req_t *ucrp;
xhci_transfer_t *rem;
ASSERT(MUTEX_HELD(&xhcip->xhci_lock));
code = XHCI_TRB_GET_CODE(LE_32(trb->trb_status));
ucrp = (usb_ctrl_req_t *)xt->xt_usba_req;
if (off != xt->xt_ntrbs - 1) {
uint_t remain;
usb_ctrl_req_t *ucrp = (usb_ctrl_req_t *)xt->xt_usba_req;
VERIFY3S(code, ==, XHCI_CODE_SHORT_XFER);
if (!(ucrp->ctrl_attributes & USB_ATTRS_SHORT_XFER_OK)) {
xt->xt_cr = USB_CR_DATA_UNDERRUN;
mutex_exit(&xhcip->xhci_lock);
return (B_TRUE);
}
remain = XHCI_TRB_REMAIN(LE_32(trb->trb_status));
xt->xt_short = xt->xt_buffer.xdb_len - remain;
mutex_exit(&xhcip->xhci_lock);
return (B_TRUE);
}
if (xt->xt_data_tohost == B_TRUE) {
size_t len;
if (xt->xt_short != 0) {
len = xt->xt_short;
} else {
len = xt->xt_buffer.xdb_len;
}
if (xhci_transfer_sync(xhcip, xt, DDI_DMA_SYNC_FORCPU) !=
DDI_FM_OK) {
xhci_error(xhcip, "failed to process control transfer "
"callback for endpoint %u of device on slot %d and "
"port %d: encountered fatal FM error synchronizing "
"DMA memory, resetting device", xep->xep_num,
xd->xd_slot, xd->xd_port);
xhci_fm_runtime_reset(xhcip);
mutex_exit(&xhcip->xhci_lock);
return (B_FALSE);
}
xhci_transfer_copy(xt, ucrp->ctrl_data->b_rptr, len, B_TRUE);
ucrp->ctrl_data->b_wptr += len;
}
VERIFY(xhci_ring_trb_consumed(&xep->xep_ring, LE_64(trb->trb_addr)));
rem = list_remove_head(&xep->xep_transfers);
VERIFY3P(rem, ==, xt);
mutex_exit(&xhcip->xhci_lock);
usba_hcdi_cb(xep->xep_pipe, (usb_opaque_t)ucrp, xt->xt_cr);
xhci_transfer_free(xhcip, xt);
return (B_TRUE);
}
static usb_opaque_t
xhci_endpoint_dup_periodic(xhci_endpoint_t *xep, xhci_transfer_t *xt,
usb_cr_t *cr)
{
usb_opaque_t urp;
xhci_pipe_t *xp = (xhci_pipe_t *)xep->xep_pipe->p_hcd_private;
xhci_periodic_pipe_t *xpp = &xp->xp_periodic;
if (XHCI_IS_ONESHOT_XFER(xt)) {
return (xt->xt_usba_req);
}
if (xep->xep_type == USB_EP_ATTR_INTR) {
urp = (usb_opaque_t)usba_hcdi_dup_intr_req(xep->xep_pipe->p_dip,
(usb_intr_req_t *)xpp->xpp_usb_req, xpp->xpp_tsize, 0);
} else {
urp = (usb_opaque_t)usba_hcdi_dup_isoc_req(xep->xep_pipe->p_dip,
(usb_isoc_req_t *)xpp->xpp_usb_req, 0);
}
if (urp == NULL) {
xpp->xpp_poll_state = XHCI_PERIODIC_POLL_NOMEM;
urp = xpp->xpp_usb_req;
xpp->xpp_usb_req = NULL;
*cr = USB_CR_NO_RESOURCES;
} else {
mutex_enter(&xep->xep_pipe->p_mutex);
xep->xep_pipe->p_req_count++;
mutex_exit(&xep->xep_pipe->p_mutex);
}
return (urp);
}
xhci_device_t *
xhci_device_lookup_by_slot(xhci_t *xhcip, int slot)
{
xhci_device_t *xd;
ASSERT(MUTEX_HELD(&xhcip->xhci_lock));
for (xd = list_head(&xhcip->xhci_usba.xa_devices); xd != NULL;
xd = list_next(&xhcip->xhci_usba.xa_devices, xd)) {
if (xd->xd_slot == slot)
return (xd);
}
return (NULL);
}
static boolean_t
xhci_endpoint_norm_callback(xhci_t *xhcip, xhci_device_t *xd,
xhci_endpoint_t *xep, xhci_transfer_t *xt, uint_t off, xhci_trb_t *trb)
{
int code;
usb_cr_t cr;
xhci_transfer_t *rem;
int attrs;
mblk_t *mp;
boolean_t periodic = B_FALSE;
usb_opaque_t urp;
ASSERT(MUTEX_HELD(&xhcip->xhci_lock));
ASSERT(xep->xep_type == USB_EP_ATTR_BULK ||
xep->xep_type == USB_EP_ATTR_INTR);
code = XHCI_TRB_GET_CODE(LE_32(trb->trb_status));
if (code == XHCI_CODE_SHORT_XFER) {
uint_t residue;
residue = XHCI_TRB_REMAIN(LE_32(trb->trb_status));
if (xep->xep_type == USB_EP_ATTR_BULK) {
VERIFY3U(XHCI_TRB_GET_ED(LE_32(trb->trb_flags)), !=, 0);
xt->xt_short = residue;
} else {
xt->xt_short = xt->xt_buffer.xdb_len - residue;
}
}
if (off < xt->xt_ntrbs - 1) {
mutex_exit(&xhcip->xhci_lock);
return (B_TRUE);
}
urp = xt->xt_usba_req;
if (xep->xep_type == USB_EP_ATTR_BULK) {
usb_bulk_req_t *ubrp = (usb_bulk_req_t *)xt->xt_usba_req;
attrs = ubrp->bulk_attributes;
mp = ubrp->bulk_data;
} else {
usb_intr_req_t *uirp = (usb_intr_req_t *)xt->xt_usba_req;
if (uirp == NULL) {
periodic = B_TRUE;
urp = xhci_endpoint_dup_periodic(xep, xt, &cr);
uirp = (usb_intr_req_t *)urp;
if (cr == USB_CR_NO_RESOURCES)
goto out;
}
attrs = uirp->intr_attributes;
mp = uirp->intr_data;
}
if (xt->xt_data_tohost == B_TRUE) {
size_t len;
if (xt->xt_short != 0) {
if (!(attrs & USB_ATTRS_SHORT_XFER_OK)) {
cr = USB_CR_DATA_UNDERRUN;
goto out;
}
len = xt->xt_short;
} else {
len = xt->xt_buffer.xdb_len;
}
if (xhci_transfer_sync(xhcip, xt, DDI_DMA_SYNC_FORCPU) !=
DDI_FM_OK) {
xhci_error(xhcip, "failed to process normal transfer "
"callback for endpoint %u of device on slot %d and "
"port %d: encountered fatal FM error synchronizing "
"DMA memory, resetting device", xep->xep_num,
xd->xd_slot, xd->xd_port);
xhci_fm_runtime_reset(xhcip);
mutex_exit(&xhcip->xhci_lock);
return (B_FALSE);
}
xhci_transfer_copy(xt, mp->b_rptr, len, B_TRUE);
mp->b_wptr += len;
}
cr = USB_CR_OK;
out:
VERIFY(xhci_ring_trb_consumed(&xep->xep_ring, xt->xt_trbs_pa[off]));
rem = list_remove_head(&xep->xep_transfers);
VERIFY3P(rem, ==, xt);
mutex_exit(&xhcip->xhci_lock);
usba_hcdi_cb(xep->xep_pipe, urp, cr);
if (periodic == B_TRUE) {
xhci_endpoint_reschedule_periodic(xhcip, xd, xep, xt);
} else {
xhci_transfer_free(xhcip, xt);
}
return (B_TRUE);
}
static boolean_t
xhci_endpoint_isoch_callback(xhci_t *xhcip, xhci_device_t *xd,
xhci_endpoint_t *xep, xhci_transfer_t *xt, uint_t off, xhci_trb_t *trb)
{
int code;
usb_cr_t cr;
xhci_transfer_t *rem;
usb_isoc_pkt_descr_t *desc;
usb_isoc_req_t *usrp;
ASSERT(MUTEX_HELD(&xhcip->xhci_lock));
ASSERT3S(xep->xep_type, ==, USB_EP_ATTR_ISOCH);
code = XHCI_TRB_GET_CODE(LE_32(trb->trb_status));
desc = &xt->xt_isoc[off];
if (code == XHCI_CODE_SHORT_XFER) {
int residue = XHCI_TRB_REMAIN(LE_32(trb->trb_status));
desc->isoc_pkt_actual_length -= residue;
}
if (off < xt->xt_ntrbs - 1) {
mutex_exit(&xhcip->xhci_lock);
return (B_TRUE);
}
VERIFY(xhci_ring_trb_consumed(&xep->xep_ring, LE_64(trb->trb_addr)));
rem = list_remove_head(&xep->xep_transfers);
VERIFY3P(rem, ==, xt);
mutex_exit(&xhcip->xhci_lock);
cr = USB_CR_OK;
if (xt->xt_data_tohost == B_TRUE) {
usb_opaque_t urp;
urp = xhci_endpoint_dup_periodic(xep, xt, &cr);
usrp = (usb_isoc_req_t *)urp;
if (cr == USB_CR_OK) {
mblk_t *mp;
size_t len;
if (xhci_transfer_sync(xhcip, xt,
DDI_DMA_SYNC_FORCPU) != DDI_FM_OK) {
xhci_error(xhcip, "failed to process "
"isochronous transfer callback for "
"endpoint %u of device on slot %d and port "
"%d: encountered fatal FM error "
"synchronizing DMA memory, resetting "
"device",
xep->xep_num, xd->xd_slot, xd->xd_port);
xhci_fm_runtime_reset(xhcip);
mutex_exit(&xhcip->xhci_lock);
return (B_FALSE);
}
mp = usrp->isoc_data;
len = xt->xt_buffer.xdb_len;
xhci_transfer_copy(xt, mp->b_rptr, len, B_TRUE);
mp->b_wptr += len;
}
} else {
usrp = (usb_isoc_req_t *)xt->xt_usba_req;
}
if (cr == USB_CR_OK) {
bcopy(xt->xt_isoc, usrp->isoc_pkt_descr,
sizeof (usb_isoc_pkt_descr_t) * usrp->isoc_pkts_count);
}
usba_hcdi_cb(xep->xep_pipe, (usb_opaque_t)usrp, cr);
if (xt->xt_data_tohost == B_TRUE) {
xhci_endpoint_reschedule_periodic(xhcip, xd, xep, xt);
} else {
xhci_transfer_free(xhcip, xt);
}
return (B_TRUE);
}
boolean_t
xhci_endpoint_transfer_callback(xhci_t *xhcip, xhci_trb_t *trb)
{
boolean_t ret;
int slot, endpoint, code;
uint_t off;
xhci_device_t *xd;
xhci_endpoint_t *xep;
xhci_transfer_t *xt;
boolean_t transfer_done;
endpoint = XHCI_TRB_GET_EP(LE_32(trb->trb_flags));
slot = XHCI_TRB_GET_SLOT(LE_32(trb->trb_flags));
code = XHCI_TRB_GET_CODE(LE_32(trb->trb_status));
switch (code) {
case XHCI_CODE_RING_UNDERRUN:
case XHCI_CODE_RING_OVERRUN:
return (B_TRUE);
case XHCI_CODE_UNDEFINED:
xhci_error(xhcip, "received transfer trb with undefined fatal "
"error: resetting device");
xhci_fm_runtime_reset(xhcip);
return (B_FALSE);
case XHCI_CODE_XFER_STOPPED:
case XHCI_CODE_XFER_STOPINV:
case XHCI_CODE_XFER_STOPSHORT:
return (B_TRUE);
default:
break;
}
mutex_enter(&xhcip->xhci_lock);
xd = xhci_device_lookup_by_slot(xhcip, slot);
if (xd == NULL) {
xhci_error(xhcip, "received transfer trb with code %d for "
"unknown slot %d and endpoint %d: resetting device", code,
slot, endpoint);
mutex_exit(&xhcip->xhci_lock);
xhci_fm_runtime_reset(xhcip);
return (B_FALSE);
}
xep = xd->xd_endpoints[endpoint - 1];
if (xep == NULL) {
xhci_error(xhcip, "received transfer trb with code %d, slot "
"%d, and unknown endpoint %d: resetting device", code,
slot, endpoint);
mutex_exit(&xhcip->xhci_lock);
xhci_fm_runtime_reset(xhcip);
return (B_FALSE);
}
if ((xt = xhci_endpoint_determine_transfer(xhcip, xep, trb, &off)) ==
NULL) {
xhci_error(xhcip, "received transfer trb with code %d, slot "
"%d, and endpoint %d, but does not match current transfer "
"for endpoint: resetting device", code, slot, endpoint);
mutex_exit(&xhcip->xhci_lock);
xhci_fm_runtime_reset(xhcip);
return (B_FALSE);
}
transfer_done = B_FALSE;
switch (code) {
case XHCI_CODE_SUCCESS:
case XHCI_CODE_SHORT_XFER:
break;
case XHCI_CODE_STALL:
xep->xep_state |= XHCI_ENDPOINT_HALTED;
xt->xt_cr = USB_CR_STALL;
transfer_done = B_TRUE;
break;
case XHCI_CODE_BABBLE:
transfer_done = B_TRUE;
xt->xt_cr = USB_CR_DATA_OVERRUN;
xep->xep_state |= XHCI_ENDPOINT_HALTED;
break;
case XHCI_CODE_TXERR:
case XHCI_CODE_SPLITERR:
transfer_done = B_TRUE;
xt->xt_cr = USB_CR_DEV_NOT_RESP;
xep->xep_state |= XHCI_ENDPOINT_HALTED;
break;
case XHCI_CODE_BW_OVERRUN:
transfer_done = B_TRUE;
xt->xt_cr = USB_CR_DATA_OVERRUN;
break;
case XHCI_CODE_DATA_BUF:
transfer_done = B_TRUE;
if (xt->xt_data_tohost)
xt->xt_cr = USB_CR_DATA_OVERRUN;
else
xt->xt_cr = USB_CR_DATA_UNDERRUN;
break;
default:
transfer_done = B_TRUE;
xt->xt_cr = USB_CR_HC_HARDWARE_ERR;
break;
}
if (transfer_done == B_TRUE) {
xhci_transfer_t *alt;
alt = list_remove_head(&xep->xep_transfers);
VERIFY3P(alt, ==, xt);
mutex_exit(&xhcip->xhci_lock);
if (xt->xt_usba_req == NULL) {
usb_opaque_t urp;
urp = xhci_endpoint_dup_periodic(xep, xt, &xt->xt_cr);
usba_hcdi_cb(xep->xep_pipe, urp, xt->xt_cr);
} else {
usba_hcdi_cb(xep->xep_pipe,
(usb_opaque_t)xt->xt_usba_req, xt->xt_cr);
xhci_transfer_free(xhcip, xt);
}
return (B_TRUE);
}
switch (xep->xep_type) {
case USB_EP_ATTR_CONTROL:
ret = xhci_endpoint_control_callback(xhcip, xd, xep, xt, off,
trb);
break;
case USB_EP_ATTR_BULK:
ret = xhci_endpoint_norm_callback(xhcip, xd, xep, xt, off, trb);
break;
case USB_EP_ATTR_INTR:
ret = xhci_endpoint_norm_callback(xhcip, xd, xep, xt, off,
trb);
break;
case USB_EP_ATTR_ISOCH:
ret = xhci_endpoint_isoch_callback(xhcip, xd, xep, xt, off,
trb);
break;
default:
panic("bad endpoint type: %u", xep->xep_type);
}
return (ret);
}