From: Niklas Neronin <niklas.neronin@linux.intel.com>
To: mathias.nyman@linux.intel.com
Cc: linux-usb@vger.kernel.org,
Niklas Neronin <niklas.neronin@linux.intel.com>
Subject: [PATCH 4/7] usb: xhci: split Configure Endpoint and Evaluate Context handling
Date: Tue, 1 Dec 2026 20:41:31 +0100 [thread overview]
Message-ID: <20261201194134.265480-5-niklas.neronin@linux.intel.com> (raw)
In-Reply-To: <20261201194134.265480-1-niklas.neronin@linux.intel.com>
xhci_configure_endpoint() handles both Configure Endpoint and Evaluate
Context commands, selected through the 'ctx_change' parameter.
While the two commands share some infrastructure, they have different
requirements, queue different command TRBs, and handle different
completion codes. As a result, xhci_configure_endpoint() contains
command-specific branching throughout its setup and completion paths.
The completion status handling is also split into separate helper
functions.
Separate the Evaluate Context path into a dedicated xhci_evaluate_context()
helper and fold the command completion handling into the respective command
submission functions. This removes the 'ctx_change' parameter, reduces
command specific branching while making the Configure Endpoint and
Evaluate Context flows easier to follow.
Evaluate Context no longer goes through the XHCI_EP_LIMIT_QUIRK.
Evaluate Context only modifies the Slot and EP0 contexts, which
xhci_count_num_new_endpoints() and xhci_count_num_dropped_endpoints()
ignore, so this accounting was always a no-op for it.
Software bandwidth checking was already skipped for Evaluate Context.
Add separate xhci_evaluate_context and xhci_evaluate_context_ctrl_ctx
trace events, so that Evaluate Context commands are no longer traced as
Configure Endpoint.
The resulting code is smaller and more closely reflects the distinct
semantics of the two xHCI commands.
Signed-off-by: Niklas Neronin <niklas.neronin@linux.intel.com>
---
drivers/usb/host/xhci-trace.h | 10 ++
drivers/usb/host/xhci.c | 262 ++++++++++++++++------------------
2 files changed, 132 insertions(+), 140 deletions(-)
diff --git a/drivers/usb/host/xhci-trace.h b/drivers/usb/host/xhci-trace.h
index 724cba2dbb78..9b3f45743aa9 100644
--- a/drivers/usb/host/xhci-trace.h
+++ b/drivers/usb/host/xhci-trace.h
@@ -446,6 +446,11 @@ DEFINE_EVENT(xhci_log_slot_ctx, xhci_configure_endpoint,
TP_ARGS(ctx)
);
+DEFINE_EVENT(xhci_log_slot_ctx, xhci_evaluate_context,
+ TP_PROTO(struct xhci_slot_ctx *ctx),
+ TP_ARGS(ctx)
+);
+
DECLARE_EVENT_CLASS(xhci_log_ctrl_ctx,
TP_PROTO(struct xhci_input_control_ctx *ctrl_ctx),
TP_ARGS(ctrl_ctx),
@@ -471,6 +476,11 @@ DEFINE_EVENT(xhci_log_ctrl_ctx, xhci_configure_endpoint_ctrl_ctx,
TP_ARGS(ctrl_ctx)
);
+DEFINE_EVENT(xhci_log_ctrl_ctx, xhci_evaluate_context_ctrl_ctx,
+ TP_PROTO(struct xhci_input_control_ctx *ctrl_ctx),
+ TP_ARGS(ctrl_ctx)
+);
+
DECLARE_EVENT_CLASS(xhci_log_ring,
TP_PROTO(struct xhci_ring *ring),
TP_ARGS(ring),
diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c
index 9688c5986a40..d2ca031cff70 100644
--- a/drivers/usb/host/xhci.c
+++ b/drivers/usb/host/xhci.c
@@ -1535,9 +1535,8 @@ static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
return 1;
}
-static int xhci_configure_endpoint(struct xhci_hcd *xhci,
- struct usb_device *udev, struct xhci_command *command,
- bool ctx_change, bool must_succeed);
+static int xhci_evaluate_context(struct xhci_hcd *xhci, struct usb_device *udev,
+ struct xhci_command *command, bool must_succeed);
/*
* Full speed devices may have a max packet size greater than 8 bytes, but the
@@ -1592,8 +1591,7 @@ static int xhci_check_ep0_maxpacket(struct xhci_hcd *xhci, struct xhci_virt_devi
ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
ctrl_ctx->drop_flags = 0;
- ret = xhci_configure_endpoint(xhci, vdev->udev, command,
- true, false);
+ ret = xhci_evaluate_context(xhci, vdev->udev, command, false);
/* Clean up the input context for later use by bandwidth functions */
ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
break;
@@ -2096,106 +2094,6 @@ static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *vir
}
}
-static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
- struct usb_device *udev, u32 *cmd_status)
-{
- int ret;
-
- switch (*cmd_status) {
- case COMP_COMMAND_ABORTED:
- case COMP_COMMAND_RING_STOPPED:
- xhci_warn(xhci, "Timeout while waiting for configure endpoint command\n");
- ret = -ETIME;
- break;
- case COMP_RESOURCE_ERROR:
- dev_warn(&udev->dev,
- "Not enough host controller resources for new device state.\n");
- ret = -ENOMEM;
- /* FIXME: can we allocate more resources for the HC? */
- break;
- case COMP_BANDWIDTH_ERROR:
- case COMP_SECONDARY_BANDWIDTH_ERROR:
- dev_warn(&udev->dev,
- "Not enough bandwidth for new device state.\n");
- ret = -ENOSPC;
- /* FIXME: can we go back to the old state? */
- break;
- case COMP_TRB_ERROR:
- /* the HCD set up something wrong */
- dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
- "add flag = 1, "
- "and endpoint is not disabled.\n");
- ret = -EINVAL;
- break;
- case COMP_INCOMPATIBLE_DEVICE_ERROR:
- dev_warn(&udev->dev,
- "ERROR: Incompatible device for endpoint configure command.\n");
- ret = -ENODEV;
- break;
- case COMP_SUCCESS:
- xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
- "Successful Endpoint Configure command");
- ret = 0;
- break;
- default:
- xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n",
- *cmd_status);
- ret = -EINVAL;
- break;
- }
- return ret;
-}
-
-static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
- struct usb_device *udev, u32 *cmd_status)
-{
- int ret;
-
- switch (*cmd_status) {
- case COMP_COMMAND_ABORTED:
- case COMP_COMMAND_RING_STOPPED:
- xhci_warn(xhci, "Timeout while waiting for evaluate context command\n");
- ret = -ETIME;
- break;
- case COMP_PARAMETER_ERROR:
- dev_warn(&udev->dev,
- "WARN: xHCI driver setup invalid evaluate context command.\n");
- ret = -EINVAL;
- break;
- case COMP_SLOT_NOT_ENABLED_ERROR:
- dev_warn(&udev->dev,
- "WARN: slot not enabled for evaluate context command.\n");
- ret = -EINVAL;
- break;
- case COMP_CONTEXT_STATE_ERROR:
- dev_warn(&udev->dev,
- "WARN: invalid context state for evaluate context command.\n");
- ret = -EINVAL;
- break;
- case COMP_INCOMPATIBLE_DEVICE_ERROR:
- dev_warn(&udev->dev,
- "ERROR: Incompatible device for evaluate context command.\n");
- ret = -ENODEV;
- break;
- case COMP_MAX_EXIT_LATENCY_TOO_LARGE_ERROR:
- /* Max Exit Latency too large error */
- dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n");
- ret = -EINVAL;
- break;
- case COMP_SUCCESS:
- xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
- "Successful evaluate context command");
- ret = 0;
- break;
- default:
- xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n",
- *cmd_status);
- ret = -EINVAL;
- break;
- }
- return ret;
-}
-
static u32 xhci_count_num_new_endpoints(struct xhci_hcd *xhci,
struct xhci_input_control_ctx *ctrl_ctx)
{
@@ -2928,13 +2826,8 @@ int xhci_usb_endpoint_maxp(struct usb_device *udev,
return usb_endpoint_maxp(&host_ep->desc);
}
-/* Issue a configure endpoint command or evaluate context command
- * and wait for it to finish.
- */
-static int xhci_configure_endpoint(struct xhci_hcd *xhci,
- struct usb_device *udev,
- struct xhci_command *command,
- bool ctx_change, bool must_succeed)
+static int xhci_configure_endpoint(struct xhci_hcd *xhci, struct usb_device *udev,
+ struct xhci_command *command, bool must_succeed)
{
int ret;
unsigned long flags;
@@ -2964,7 +2857,8 @@ static int xhci_configure_endpoint(struct xhci_hcd *xhci,
xhci->num_active_eps);
return -ENOMEM;
}
- if ((xhci->quirks & XHCI_SW_BW_CHECKING) && !ctx_change &&
+
+ if ((xhci->quirks & XHCI_SW_BW_CHECKING) &&
xhci_reserve_bandwidth(xhci, virt_dev, command->in_ctx)) {
if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
xhci_free_host_resources(xhci, ctrl_ctx);
@@ -2978,14 +2872,8 @@ static int xhci_configure_endpoint(struct xhci_hcd *xhci,
trace_xhci_configure_endpoint_ctrl_ctx(ctrl_ctx);
trace_xhci_configure_endpoint(slot_ctx);
- if (!ctx_change)
- ret = xhci_queue_configure_endpoint(xhci, command,
- command->in_ctx->dma,
- udev->slot_id, must_succeed);
- else
- ret = xhci_queue_evaluate_context(xhci, command,
- command->in_ctx->dma,
- udev->slot_id, must_succeed);
+ ret = xhci_queue_configure_endpoint(xhci, command, command->in_ctx->dma,
+ udev->slot_id, must_succeed);
if (ret < 0) {
if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
xhci_free_host_resources(xhci, ctrl_ctx);
@@ -2994,18 +2882,47 @@ static int xhci_configure_endpoint(struct xhci_hcd *xhci,
"FIXME allocate a new ring segment");
return -ENOMEM;
}
+
xhci_ring_cmd_db(xhci);
spin_unlock_irqrestore(&xhci->lock, flags);
/* Wait for the configure endpoint command to complete */
wait_for_completion(command->completion);
- if (!ctx_change)
- ret = xhci_configure_endpoint_result(xhci, udev,
- &command->status);
- else
- ret = xhci_evaluate_context_result(xhci, udev,
- &command->status);
+ switch (command->status) {
+ case COMP_COMMAND_ABORTED:
+ case COMP_COMMAND_RING_STOPPED:
+ xhci_warn(xhci, "Timeout while waiting for configure endpoint command\n");
+ ret = -ETIME;
+ break;
+ case COMP_RESOURCE_ERROR:
+ dev_warn(&udev->dev, "Not enough host controller resources for new device state.\n");
+ ret = -ENOMEM;
+ break;
+ case COMP_BANDWIDTH_ERROR:
+ case COMP_SECONDARY_BANDWIDTH_ERROR:
+ dev_warn(&udev->dev, "Not enough bandwidth for new device state.\n");
+ ret = -ENOSPC;
+ /* FIXME: can we go back to the old state? */
+ break;
+ case COMP_TRB_ERROR:
+ dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, add flag = 1, and endpoint is not disabled.\n");
+ ret = -EINVAL;
+ break;
+ case COMP_INCOMPATIBLE_DEVICE_ERROR:
+ dev_warn(&udev->dev, "ERROR: Incompatible device for endpoint configure command.\n");
+ ret = -ENODEV;
+ break;
+ case COMP_SUCCESS:
+ xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
+ "Successful Endpoint Configure command");
+ break;
+ default:
+ xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n",
+ command->status);
+ ret = -EINVAL;
+ break;
+ }
if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
spin_lock_irqsave(&xhci->lock, flags);
@@ -3021,6 +2938,78 @@ static int xhci_configure_endpoint(struct xhci_hcd *xhci,
return ret;
}
+static int xhci_evaluate_context(struct xhci_hcd *xhci, struct usb_device *udev,
+ struct xhci_command *command, bool must_succeed)
+{
+ int ret;
+ unsigned long flags;
+
+ if (!command)
+ return -EINVAL;
+
+ spin_lock_irqsave(&xhci->lock, flags);
+
+ if (xhci->xhc_state & XHCI_STATE_DYING) {
+ spin_unlock_irqrestore(&xhci->lock, flags);
+ return -ESHUTDOWN;
+ }
+
+ trace_xhci_evaluate_context_ctrl_ctx(xhci_get_input_control_ctx(command->in_ctx));
+ trace_xhci_evaluate_context(xhci_get_slot_ctx(xhci, command->in_ctx));
+
+ ret = xhci_queue_evaluate_context(xhci, command, command->in_ctx->dma, udev->slot_id,
+ must_succeed);
+ if (ret < 0) {
+ spin_unlock_irqrestore(&xhci->lock, flags);
+ return -ENOMEM;
+ }
+
+ xhci_ring_cmd_db(xhci);
+ spin_unlock_irqrestore(&xhci->lock, flags);
+
+ wait_for_completion(command->completion);
+
+ switch (command->status) {
+ case COMP_COMMAND_ABORTED:
+ case COMP_COMMAND_RING_STOPPED:
+ xhci_warn(xhci, "Timeout while waiting for evaluate context command\n");
+ ret = -ETIME;
+ break;
+ case COMP_PARAMETER_ERROR:
+ dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate context command.\n");
+ ret = -EINVAL;
+ break;
+ case COMP_SLOT_NOT_ENABLED_ERROR:
+ dev_warn(&udev->dev, "WARN: slot not enabled for evaluate context command.\n");
+ ret = -EINVAL;
+ break;
+ case COMP_CONTEXT_STATE_ERROR:
+ dev_warn(&udev->dev, "WARN: invalid context state for evaluate context command.\n");
+ ret = -EINVAL;
+ break;
+ case COMP_INCOMPATIBLE_DEVICE_ERROR:
+ dev_warn(&udev->dev, "ERROR: Incompatible device for evaluate context command.\n");
+ ret = -ENODEV;
+ break;
+ case COMP_MAX_EXIT_LATENCY_TOO_LARGE_ERROR:
+ /* Max Exit Latency too large error */
+ dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n");
+ ret = -EINVAL;
+ break;
+ case COMP_SUCCESS:
+ xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
+ "Successful evaluate context command");
+ break;
+ default:
+ xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n",
+ command->status);
+ ret = -EINVAL;
+ break;
+ }
+
+ return ret;
+}
+
static void xhci_check_bw_drop_ep_streams(struct xhci_hcd *xhci,
struct xhci_virt_device *vdev, int i)
{
@@ -3097,8 +3086,7 @@ int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
}
}
- ret = xhci_configure_endpoint(xhci, udev, command,
- false, false);
+ ret = xhci_configure_endpoint(xhci, udev, command, false);
if (ret)
/* Callee should call reset_bandwidth() */
goto command_cleanup;
@@ -3677,8 +3665,7 @@ static int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
changed_ep_bitmask, changed_ep_bitmask);
/* Issue and wait for the configure endpoint command */
- ret = xhci_configure_endpoint(xhci, udev, config_cmd,
- false, false);
+ ret = xhci_configure_endpoint(xhci, udev, config_cmd, false);
/* xHC rejected the configure endpoint command for some reason, so we
* leave the old ring intact and free our internal streams data
@@ -3783,8 +3770,7 @@ static int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
/* Issue and wait for the configure endpoint command,
* which must succeed.
*/
- ret = xhci_configure_endpoint(xhci, udev, command,
- false, true);
+ ret = xhci_configure_endpoint(xhci, udev, command, true);
/* xHC rejected the configure endpoint command for some reason, so we
* leave the streams rings intact.
@@ -4522,9 +4508,7 @@ static int __maybe_unused xhci_change_max_exit_latency(struct xhci_hcd *xhci,
"Set up evaluate context for LPM MEL change.");
/* Issue and wait for the evaluate context command. */
- ret = xhci_configure_endpoint(xhci, udev, command,
- true, true);
-
+ ret = xhci_evaluate_context(xhci, udev, command, true);
if (!ret) {
spin_lock_irqsave(&xhci->lock, flags);
virt_dev->current_mel = max_exit_latency;
@@ -5291,11 +5275,9 @@ int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
* evaluate context command.
*/
if (xhci->hci_version > 0x95)
- ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
- false, false);
+ ret = xhci_configure_endpoint(xhci, hdev, config_cmd, false);
else
- ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
- true, false);
+ ret = xhci_evaluate_context(xhci, hdev, config_cmd, false);
xhci_free_command(xhci, config_cmd);
return ret;
--
2.50.1
next prev parent reply other threads:[~2026-12-01 19:42 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 19:41 [PATCH 0/7] usb: xhci: command queueing cleanups Niklas Neronin
2026-12-01 19:41 ` [PATCH 1/7] usb: xhci: simplify xhci_get_input_control_ctx() Niklas Neronin
2026-12-01 19:47 ` sashiko-bot
2026-12-01 19:41 ` [PATCH 2/7] usb: xhci: add xhci_queue_set_deq() helper Niklas Neronin
2026-12-01 19:41 ` [PATCH 3/7] usb: xhci: simplify prepare_ring() Niklas Neronin
2026-12-01 19:41 ` Niklas Neronin [this message]
2026-12-01 19:41 ` [PATCH 5/7] usb: xhci: clear EP_GETTING_NO_STREAMS when freeing streams fails Niklas Neronin
2026-12-01 19:52 ` sashiko-bot
2026-12-01 19:41 ` [PATCH 6/7] usb: xhci: remove pre-allocated free streams command Niklas Neronin
2026-12-01 19:41 ` [PATCH 7/7] usb: xhci: remove command ring TRB reservation Niklas Neronin
2026-12-01 19:52 ` [PATCH 0/7] usb: xhci: command queueing cleanups sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261201194134.265480-5-niklas.neronin@linux.intel.com \
--to=niklas.neronin@linux.intel.com \
--cc=linux-usb@vger.kernel.org \
--cc=mathias.nyman@linux.intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox