net: mctp: usb: Improve IN endpoint status handling

Currently, we give-up on all non-zero status values on our IN/rx urb,
and do not re-queue the urb. This will stall the driver, and prevent
any further receive.

Instead, attempt a re-queue on transient errors, with a max of ten
successive failures. Handle EPIPE specially, by scheduling a
usb_clear_halt() in non-atomic context.

Signed-off-by: Jeremy Kerr <jk@codeconstruct.com.au>
Link: https://patch.msgid.link/20260724-dev-mctp-usb-1-1-v5-4-e66bbba0dbdc@codeconstruct.com.au
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
Jeremy Kerr 2026-07-24 13:15:25 +08:00 committed by Jakub Kicinski
parent b8564b19c0
commit 9f66be0140

View File

@ -32,6 +32,9 @@ struct mctp_usb {
struct urb *tx_urb;
struct urb *rx_urb;
int in_err_count;
int in_err_orig;
bool clear_halt;
/* enforces atomic access to rx_stopped and requeuing the retry work */
spinlock_t rx_lock;
@ -158,27 +161,75 @@ static int mctp_usb_rx_queue(struct mctp_usb *mctp_usb, gfp_t gfp)
return 0;
}
static const unsigned int rx_err_max = 10;
/* Returns -1 if we have hit excessive errors, zero otherwise. */
static int mctp_usb_in_urb_err(struct mctp_usb *mctp_usb, int status,
bool stalled)
{
mctp_usblib_rx_cancel(&mctp_usb->rx);
if (!mctp_usb->in_err_count++)
mctp_usb->in_err_orig = status;
if (mctp_usb->in_err_count >= rx_err_max) {
netdev_err(mctp_usb->netdev,
"excessive errors from%s IN EP, first: %d\n",
stalled ? " (stalled)" : "",
mctp_usb->in_err_orig);
return -1;
}
return 0;
}
static void mctp_usb_in_complete(struct urb *urb)
{
struct mctp_usb *mctp_usb = urb->context;
struct net_device *netdev = mctp_usb->netdev;
int status;
unsigned long flags;
int rc, status;
status = urb->status;
switch (status) {
default:
netdev_dbg(netdev, "unexpected rx urb status: %d\n", status);
fallthrough;
case -ENOENT:
case -ECONNRESET:
case -ESHUTDOWN:
case -EPROTO:
/* device shutdown, don't resubmit */
mctp_usblib_rx_cancel(&mctp_usb->rx);
return;
case -EPIPE:
/* endpoint stall: clear halt, which will cause a resubmit */
rc = mctp_usb_in_urb_err(mctp_usb, status, true);
if (rc)
return;
mctp_usb->clear_halt = true;
spin_lock_irqsave(&mctp_usb->rx_lock, flags);
if (!mctp_usb->rx_stopped)
schedule_delayed_work(&mctp_usb->rx_retry_work,
RX_RETRY_DELAY);
spin_unlock_irqrestore(&mctp_usb->rx_lock, flags);
return;
default:
netdev_dbg(netdev, "unexpected rx urb status: %d\n", status);
fallthrough;
case -ETIME:
case -EPROTO:
case -EILSEQ:
case -EOVERFLOW:
/* possibly transient; record first failure, resubmit */
rc = mctp_usb_in_urb_err(mctp_usb, status, false);
if (rc)
return;
break;
case 0:
mctp_usblib_rx_complete(netdev, &mctp_usb->rx,
urb->actual_length);
mctp_usblib_rx_complete(netdev, &mctp_usb->rx, urb->actual_length);
mctp_usb->in_err_count = 0;
break;
}
@ -189,6 +240,30 @@ static void mctp_usb_rx_retry_work(struct work_struct *work)
{
struct mctp_usb *mctp_usb = container_of(work, struct mctp_usb,
rx_retry_work.work);
unsigned long flags;
int rc;
/* We are only called when rx completions are suspended */
if (mctp_usb->clear_halt) {
int pipe = usb_rcvbulkpipe(mctp_usb->usbdev, mctp_usb->ep_in);
rc = usb_clear_halt(mctp_usb->usbdev, pipe);
if (rc) {
netdev_err(mctp_usb->netdev,
"can't clear IN EP halt: %d\n", rc);
if (++mctp_usb->in_err_count >= rx_err_max)
return;
spin_lock_irqsave(&mctp_usb->rx_lock, flags);
if (!mctp_usb->rx_stopped)
schedule_delayed_work(&mctp_usb->rx_retry_work,
RX_RETRY_DELAY);
spin_unlock_irqrestore(&mctp_usb->rx_lock, flags);
return;
}
mctp_usb->clear_halt = false;
}
mctp_usb_rx_queue(mctp_usb, GFP_KERNEL);
}
@ -198,6 +273,8 @@ static int mctp_usb_open(struct net_device *dev)
struct mctp_usb *mctp_usb = netdev_priv(dev);
WRITE_ONCE(mctp_usb->rx_stopped, false);
mctp_usb->clear_halt = false;
mctp_usb->in_err_count = 0;
netif_start_queue(dev);