Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

xhci: use generic command timer for stop endpoint commands.

The 'stop endpoint' command timer was started when a 'stop endpoint'
command was added to the command queue.
This can trigger unwanted timeouts if there are several pending commands
in the queue that xHC needs to handle first.

The generic command timer, which was added later than the 'stop endpoint'
timeout timer, times each command currently being handled by xHC hardware.

A timed out stop endpoint command was treated as a more severe issue than
other failed commands, so the separate stop endpoint timer was left
unchanged.

Use the generic command timer for stop endpoint commands. Identify if
the timed out command was a stop endpoint command in the generic handler,
and treat it with the same severity as earlier.

Signed-off-by: Mathias Nyman <mathias.nyman@linux.intel.com>
Link: https://lore.kernel.org/r/20220511220450.85367-7-mathias.nyman@linux.intel.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Mathias Nyman and committed by
Greg Kroah-Hartman
25355e04 4736ebd7

+37 -91
+2 -10
drivers/usb/host/xhci-mem.c
··· 782 782 783 783 /***************** Device context manipulation *************************/ 784 784 785 - static void xhci_init_endpoint_timer(struct xhci_hcd *xhci, 786 - struct xhci_virt_ep *ep) 787 - { 788 - timer_setup(&ep->stop_cmd_timer, xhci_stop_endpoint_command_watchdog, 789 - 0); 790 - ep->xhci = xhci; 791 - } 792 - 793 785 static void xhci_free_tt_info(struct xhci_hcd *xhci, 794 786 struct xhci_virt_device *virt_dev, 795 787 int slot_id) ··· 986 994 xhci_dbg(xhci, "Slot %d input ctx = 0x%llx (dma)\n", slot_id, 987 995 (unsigned long long)dev->in_ctx->dma); 988 996 989 - /* Initialize the cancellation list and watchdog timers for each ep */ 997 + /* Initialize the cancellation and bandwidth list for each ep */ 990 998 for (i = 0; i < 31; i++) { 991 999 dev->eps[i].ep_index = i; 992 1000 dev->eps[i].vdev = dev; 993 - xhci_init_endpoint_timer(xhci, &dev->eps[i]); 1001 + dev->eps[i].xhci = xhci; 994 1002 INIT_LIST_HEAD(&dev->eps[i].cancelled_td_list); 995 1003 INIT_LIST_HEAD(&dev->eps[i].bw_endpoint_list); 996 1004 }
+34 -73
drivers/usb/host/xhci-ring.c
··· 740 740 } 741 741 } 742 742 743 - static void xhci_stop_watchdog_timer_in_irq(struct xhci_hcd *xhci, 744 - struct xhci_virt_ep *ep) 745 - { 746 - ep->ep_state &= ~EP_STOP_CMD_PENDING; 747 - /* Can't del_timer_sync in interrupt */ 748 - del_timer(&ep->stop_cmd_timer); 749 - } 750 - 751 743 /* 752 744 * Must be called with xhci->lock held in interrupt context, 753 745 * releases and re-acquires xhci->lock ··· 1114 1122 reset_type); 1115 1123 if (err) 1116 1124 break; 1117 - xhci_stop_watchdog_timer_in_irq(xhci, ep); 1125 + ep->ep_state &= ~EP_STOP_CMD_PENDING; 1118 1126 return; 1119 1127 case EP_STATE_RUNNING: 1120 1128 /* Race, HW handled stop ep cmd before ep was running */ 1121 1129 xhci_dbg(xhci, "Stop ep completion ctx error, ep is running\n"); 1122 1130 1123 1131 command = xhci_alloc_command(xhci, false, GFP_ATOMIC); 1124 - if (!command) 1125 - xhci_stop_watchdog_timer_in_irq(xhci, ep); 1126 - 1127 - mod_timer(&ep->stop_cmd_timer, 1128 - jiffies + XHCI_STOP_EP_CMD_TIMEOUT * HZ); 1132 + if (!command) { 1133 + ep->ep_state &= ~EP_STOP_CMD_PENDING; 1134 + return; 1135 + } 1129 1136 xhci_queue_stop_endpoint(xhci, command, slot_id, ep_index, 0); 1130 1137 xhci_ring_cmd_db(xhci); 1131 1138 ··· 1133 1142 break; 1134 1143 } 1135 1144 } 1145 + 1136 1146 /* will queue a set TR deq if stopped on a cancelled, uncleared TD */ 1137 1147 xhci_invalidate_cancelled_tds(ep); 1138 - xhci_stop_watchdog_timer_in_irq(xhci, ep); 1148 + ep->ep_state &= ~EP_STOP_CMD_PENDING; 1139 1149 1140 1150 /* Otherwise ring the doorbell(s) to restart queued transfers */ 1141 1151 xhci_giveback_invalidated_tds(ep); ··· 1238 1246 /* inform usb core hc died if PCI remove isn't already handling it */ 1239 1247 if (!(xhci->xhc_state & XHCI_STATE_REMOVING)) 1240 1248 usb_hc_died(xhci_to_hcd(xhci)); 1241 - } 1242 - 1243 - /* Watchdog timer function for when a stop endpoint command fails to complete. 1244 - * In this case, we assume the host controller is broken or dying or dead. The 1245 - * host may still be completing some other events, so we have to be careful to 1246 - * let the event ring handler and the URB dequeueing/enqueueing functions know 1247 - * through xhci->state. 1248 - * 1249 - * The timer may also fire if the host takes a very long time to respond to the 1250 - * command, and the stop endpoint command completion handler cannot delete the 1251 - * timer before the timer function is called. Another endpoint cancellation may 1252 - * sneak in before the timer function can grab the lock, and that may queue 1253 - * another stop endpoint command and add the timer back. So we cannot use a 1254 - * simple flag to say whether there is a pending stop endpoint command for a 1255 - * particular endpoint. 1256 - * 1257 - * Instead we use a combination of that flag and checking if a new timer is 1258 - * pending. 1259 - */ 1260 - void xhci_stop_endpoint_command_watchdog(struct timer_list *t) 1261 - { 1262 - struct xhci_virt_ep *ep = from_timer(ep, t, stop_cmd_timer); 1263 - struct xhci_hcd *xhci = ep->xhci; 1264 - unsigned long flags; 1265 - u32 usbsts; 1266 - char str[XHCI_MSG_MAX]; 1267 - 1268 - spin_lock_irqsave(&xhci->lock, flags); 1269 - 1270 - /* bail out if cmd completed but raced with stop ep watchdog timer.*/ 1271 - if (!(ep->ep_state & EP_STOP_CMD_PENDING) || 1272 - timer_pending(&ep->stop_cmd_timer)) { 1273 - spin_unlock_irqrestore(&xhci->lock, flags); 1274 - xhci_dbg(xhci, "Stop EP timer raced with cmd completion, exit"); 1275 - return; 1276 - } 1277 - usbsts = readl(&xhci->op_regs->status); 1278 - 1279 - xhci_warn(xhci, "xHCI host not responding to stop endpoint command.\n"); 1280 - xhci_warn(xhci, "USBSTS:%s\n", xhci_decode_usbsts(str, usbsts)); 1281 - 1282 - ep->ep_state &= ~EP_STOP_CMD_PENDING; 1283 - 1284 - xhci_halt(xhci); 1285 - 1286 - /* 1287 - * handle a stop endpoint cmd timeout as if host died (-ENODEV). 1288 - * In the future we could distinguish between -ENODEV and -ETIMEDOUT 1289 - * and try to recover a -ETIMEDOUT with a host controller reset 1290 - */ 1291 - xhci_hc_died(xhci); 1292 - 1293 - spin_unlock_irqrestore(&xhci->lock, flags); 1294 - xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, 1295 - "xHCI host controller is dead."); 1296 1249 } 1297 1250 1298 1251 static void update_ring_for_set_deq_completion(struct xhci_hcd *xhci, ··· 1587 1650 1588 1651 void xhci_handle_command_timeout(struct work_struct *work) 1589 1652 { 1590 - struct xhci_hcd *xhci; 1591 - unsigned long flags; 1592 - u64 hw_ring_state; 1653 + struct xhci_hcd *xhci; 1654 + unsigned long flags; 1655 + char str[XHCI_MSG_MAX]; 1656 + u64 hw_ring_state; 1657 + u32 cmd_field3; 1658 + u32 usbsts; 1593 1659 1594 1660 xhci = container_of(to_delayed_work(work), struct xhci_hcd, cmd_timer); 1595 1661 ··· 1606 1666 spin_unlock_irqrestore(&xhci->lock, flags); 1607 1667 return; 1608 1668 } 1669 + 1670 + cmd_field3 = le32_to_cpu(xhci->current_cmd->command_trb->generic.field[3]); 1671 + usbsts = readl(&xhci->op_regs->status); 1672 + xhci_dbg(xhci, "Command timeout, USBSTS:%s\n", xhci_decode_usbsts(str, usbsts)); 1673 + 1674 + /* Bail out and tear down xhci if a stop endpoint command failed */ 1675 + if (TRB_FIELD_TO_TYPE(cmd_field3) == TRB_STOP_RING) { 1676 + struct xhci_virt_ep *ep; 1677 + 1678 + xhci_warn(xhci, "xHCI host not responding to stop endpoint command\n"); 1679 + 1680 + ep = xhci_get_virt_ep(xhci, TRB_TO_SLOT_ID(cmd_field3), 1681 + TRB_TO_EP_INDEX(cmd_field3)); 1682 + if (ep) 1683 + ep->ep_state &= ~EP_STOP_CMD_PENDING; 1684 + 1685 + xhci_halt(xhci); 1686 + xhci_hc_died(xhci); 1687 + goto time_out_completed; 1688 + } 1689 + 1609 1690 /* mark this command to be cancelled */ 1610 1691 xhci->current_cmd->status = COMP_COMMAND_ABORTED; 1611 1692
+1 -6
drivers/usb/host/xhci.c
··· 1874 1874 goto done; 1875 1875 } 1876 1876 ep->ep_state |= EP_STOP_CMD_PENDING; 1877 - ep->stop_cmd_timer.expires = jiffies + 1878 - XHCI_STOP_EP_CMD_TIMEOUT * HZ; 1879 - add_timer(&ep->stop_cmd_timer); 1880 1877 xhci_queue_stop_endpoint(xhci, command, urb->dev->slot_id, 1881 1878 ep_index, 0); 1882 1879 xhci_ring_cmd_db(xhci); ··· 3983 3986 trace_xhci_free_dev(slot_ctx); 3984 3987 3985 3988 /* Stop any wayward timer functions (which may grab the lock) */ 3986 - for (i = 0; i < 31; i++) { 3989 + for (i = 0; i < 31; i++) 3987 3990 virt_dev->eps[i].ep_state &= ~EP_STOP_CMD_PENDING; 3988 - del_timer_sync(&virt_dev->eps[i].stop_cmd_timer); 3989 - } 3990 3991 virt_dev->udev = NULL; 3991 3992 xhci_disable_slot(xhci, udev->slot_id); 3992 3993 xhci_free_virt_device(xhci, udev->slot_id);
-2
drivers/usb/host/xhci.h
··· 948 948 #define EP_CLEARING_TT (1 << 8) 949 949 /* ---- Related to URB cancellation ---- */ 950 950 struct list_head cancelled_td_list; 951 - /* Watchdog timer for stop endpoint command to cancel URBs */ 952 - struct timer_list stop_cmd_timer; 953 951 struct xhci_hcd *xhci; 954 952 /* Dequeue pointer and dequeue segment for a submitted Set TR Dequeue 955 953 * command. We'll need to update the ring's dequeue segment and dequeue