FAILED: patch "[PATCH] usb: xhci: bail out of setup if the controller is" failed to apply to 6.6-stable tree
Subsystems (3)
[email protected]
maintainerThe patch below does not apply to the 6.6-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.6.y
git checkout FETCH_HEAD
git cherry-pick -x 78203d5b54a40f0e36196ebf31c9c7a380fc8811
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026083121-usher-contest-f49c@gregkh' --subject-prefix 'PATCH 6.6.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 78203d5b54a40f0e36196ebf31c9c7a380fc8811 Mon Sep 17 00:00:00 2001
From: Breno Leitao <[email protected]>
Date: Thu, 6 Aug 2026 17:21:06 +0300
Subject: [PATCH] usb: xhci: bail out of setup if the controller is
inaccessible
xhci_gen_setup() locates the operational registers using the capability length read from the very first register:
xhci->op_regs = hcd->regs + HC_LENGTH(readl(&xhci->cap_regs->hc_capbase));
If the controller is dead or has dropped off the bus, that read returns ~0, HC_LENGTH() truncates it to 0xff, and op_regs ends up 0xff bytes past the page-aligned MMIO base, i.e. unaligned. The first access through it, xhci_halt() -> xhci_handshake() reading op_regs->status, is then an unaligned readl() on device memory. arm64 faults on unaligned device accesses, so instead of xhci_handshake() catching the all-ones value and returning -ENODEV, setup oopses:
xhci-pci-renesas 0005:08:00.0: Unable to change power state from D3cold to D0, device inaccessible xhci-pci-renesas 0005:08:00.0: xHCI Host Controller
xhci-pci-renesas 0005:08:00.0: new USB bus registered, assigned bus number 1
Unable to handle kernel paging request at virtual address ffff80030a770103
ESR = 0x0000000096000021
FSC = 0x21: alignment fault
Internal error: Oops: 0000000096000021 [#1] SMP
pc : xhci_halt [xhci_hcd]
Call trace:
xhci_halt
xhci_gen_setup
xhci_pci_setup
usb_add_hcd
usb_hcd_pci_probe
xhci_pci_common_probe
xhci_pci_renesas_probeThis was hit with a Renesas uPD720201 that failed to power up ("Unable to change power state from D3cold to D0, device inaccessible") yet still reached the HCD probe path.
Read the capability register once, and if it reads back the all-ones value (as xhci_handshake() and xhci_reset() already test for), abort setup with -ENODEV before op_regs is derived from it. Reading it once also avoids re-reading a register that may change under a concurrent hot-removal.
drivers/usb/host/xhci.c +8 -3
@@ -5434,6 +5434,7 @@ int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks)struct device *dev = hcd->self.sysdev;int retval;u32 hcs_params1; +u32 hc_capbase;/* Accept arbitrarily long scatter-gather lists */ hcd->self.sg_tablesize = ~0 ;
@@ -5454,15 +5455,19 @@ int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks)mutex_init (&xhci->mutex); xhci->main_hcd = hcd; xhci->cap_regs = hcd->regs; - xhci->op_regs = hcd->regs + -HC_LENGTH (readl (&xhci->cap_regs->hc_capbase)); + hc_capbase =readl (&xhci->cap_regs->hc_capbase); +if (hc_capbase ==U32_MAX ) { +xhci_warn (xhci,"Host controller not accessible, removed?\n" ); +return -ENODEV ; + } + xhci->op_regs = hcd->regs +HC_LENGTH (hc_capbase); xhci->run_regs = hcd->regs + (readl (&xhci->cap_regs->run_regs_off) &RTSOFF_MASK );/* Cache read-only capability registers */ hcs_params1 =readl (&xhci->cap_regs->hcs_params1); xhci->hcs_params2 =readl (&xhci->cap_regs->hcs_params2); xhci->hcs_params3 =readl (&xhci->cap_regs->hcs_params3); - xhci->hci_version =HC_VERSION (readl (&xhci->cap_regs->hc_capbase)); + xhci->hci_version =HC_VERSION (hc_capbase); xhci->hcc_params =readl (&xhci->cap_regs->hcc_params);if (xhci->hci_version >0x100 ) xhci->hcc_params2 =readl (&xhci->cap_regs->hcc_params2);
Sasha Levin
maintainerPATCH 1/7 usb: xhci: add tracing for PORTSC register writes
From: Niklas Neronin <[email protected]>
[ Upstream commit 829738e59f1fad90ef7b63d6a1a4de9d5d22544a ]
Introduce a dedicated write function for the USB Port Register Set (PORTSC) that includes tracing capabilities for values written to the PORTSC register. This enhancement minimizes code duplication and improves debugging.
The PORTSC register is part of the Host Controller USB Port Register Set, comprising 4 x 32-bit registers. As the first register, PORTSC is accessed directly via 'port->addr'. Future commits will introduce a dedicated Port register struct to further streamline access.
By adding the xhci_portsc_writel() function prior to these changes, we significantly reduce the number of same line modifications required.
[ dep note: dropped the context-only xhci.h hunk pulling in
xhci_stop_endpoint_sync()/xhci_update_erst_dequeue()/xhci_add_interrupter()/ xhci_usb_endpoint_maxp() declarations from upstream's parent commit - those functions do not exist in this tree yet and are unrelated to this change; only the new xhci_portsc_writel() declaration is kept. Also, this tree's xhci_log_portsc trace event class still uses the older TP_PROTO(u32 portnum, u32 portsc) signature (the later conversion to struct xhci_port * is not present here), so the new xhci_portsc_writel tracepoint and its call site were adapted to use port->hcd_portnum instead of struct xhci_port * to match. ]
Stable-dep-of: 78203d5b54a4 ("usb: xhci: bail out of setup if the controller is inaccessible")
--- drivers/usb/host/xhci-debugfs.c | 2 +- drivers/usb/host/xhci-hub.c | 33 ++++++++++++++++----------------- drivers/usb/host/xhci-pci.c | 2 +- drivers/usb/host/xhci-trace.h | 5 +++++ drivers/usb/host/xhci.c | 9 ++++++++- drivers/usb/host/xhci.h | 1 + 6 files changed, 32 insertions(+), 20 deletions(-)
drivers/usb/host/xhci-debugfs.c +1 -1
@@ -387,7 +387,7 @@ static ssize_t xhci_port_write(struct file *file, const char __user *ubuf, portsc =xhci_port_state_to_neutral (portsc); portsc &= ~PORT_PLS_MASK ; portsc |=PORT_LINK_STROBE |XDEV_COMP_MODE ; -writel (portsc, port->addr); +xhci_portsc_writel (port, portsc);spin_unlock_irqrestore (&xhci->lock, flags); }else {return -EINVAL ;
drivers/usb/host/xhci-hub.c +16 -17
@@ -601,7 +601,7 @@ static void xhci_disable_port(struct xhci_hcd *xhci, struct xhci_port *port) portsc =xhci_port_state_to_neutral (portsc);/* Write 1 to disable the port */ -writel (portsc |PORT_PE , port->addr); +xhci_portsc_writel (port, portsc |PORT_PE ); portsc =readl (port->addr); xhci_dbg(xhci, "disable port %d-%d, portsc: 0x%x\n",
@@ -609,7 +609,7 @@ static void xhci_disable_port(struct xhci_hcd *xhci, struct xhci_port *port) }static void xhci_clear_port_change_bit (struct xhci_hcd *xhci,u16 wValue, -u16 wIndex,__le32 __iomem *addr,u32 port_status) +u16 wIndex,struct xhci_port *port,u32 port_status) {char *port_change_bit;u32 status;
@@ -652,8 +652,8 @@ static void xhci_clear_port_change_bit(struct xhci_hcd *xhci, u16 wValue,return ; }/* Change bits are all write 1 to clear */ -writel (port_status | status, addr); - port_status =readl (addr); +xhci_portsc_writel (port, port_status | status); + port_status =readl (port->addr);xhci_dbg (xhci,"clear port%d %s change, portsc: 0x%x\n" , wIndex +1 , port_change_bit, port_status);
@@ -690,11 +690,11 @@ static void xhci_set_port_power(struct xhci_hcd *xhci, struct xhci_port *port,if (on) {/* Power on */ -writel (temp |PORT_POWER , port->addr); +xhci_portsc_writel (port, temp |PORT_POWER );readl (port->addr); }else {/* Power off */ -writel (temp & ~PORT_POWER , port->addr); +xhci_portsc_writel (port, temp & ~PORT_POWER ); }spin_unlock_irqrestore (&xhci->lock, *flags);
@@ -793,7 +793,7 @@ void xhci_set_link_state(struct xhci_hcd *xhci, struct xhci_port *port, temp =xhci_port_state_to_neutral (portsc); temp &= ~PORT_PLS_MASK ; temp |=PORT_LINK_STROBE | link_state; -writel (temp, port->addr); +xhci_portsc_writel (port, temp); xhci_dbg(xhci, "Set port %d-%d link state, portsc: 0x%x, write 0x%x", port->rhub->hcd->self.busnum, port->hcd_portnum + 1,
@@ -823,7 +823,7 @@ static void xhci_set_remote_wake_mask(struct xhci_hcd *xhci,else temp &= ~PORT_WKOC_E ; -writel (temp, port->addr); +xhci_portsc_writel (port, temp); }/* Test and clear port RWC bit */
@@ -836,7 +836,7 @@ void xhci_test_and_clear_bit(struct xhci_hcd *xhci, struct xhci_port *port,if (temp & port_bit) { temp =xhci_port_state_to_neutral (temp); temp |= port_bit; -writel (temp, port->addr); +xhci_portsc_writel (port, temp); } }
@@ -1365,7 +1365,7 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, temp |=PORT_CSC |PORT_PEC |PORT_WRC |PORT_OCC |PORT_RC |PORT_PLC |PORT_CEC ; -writel (temp |PORT_PE , port->addr); +xhci_portsc_writel (port, temp |PORT_PE ); temp =readl (port->addr);break ; }
@@ -1496,7 +1496,7 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,break ;case USB_PORT_FEAT_RESET : temp = (temp |PORT_RESET ); -writel (temp, port->addr); +xhci_portsc_writel (port, temp); temp =readl (port->addr); xhci_dbg(xhci, "set port reset, actual port %d-%d status = 0x%x\n",
@@ -1510,7 +1510,7 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,break ;case USB_PORT_FEAT_BH_PORT_RESET : temp |=PORT_WR ; -writel (temp, port->addr); +xhci_portsc_writel (port, temp); temp =readl (port->addr);break ;case USB_PORT_FEAT_U1_TIMEOUT :
@@ -1601,8 +1601,7 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,case USB_PORT_FEAT_C_ENABLE :case USB_PORT_FEAT_C_PORT_LINK_STATE :case USB_PORT_FEAT_C_PORT_CONFIG_ERROR : -xhci_clear_port_change_bit (xhci, wValue, wIndex, - port->addr, temp); +xhci_clear_port_change_bit (xhci, wValue, wIndex, port, temp);break ;case USB_PORT_FEAT_ENABLE :xhci_disable_port (xhci, port);
@@ -1830,7 +1829,7 @@ int xhci_bus_suspend(struct usb_hcd *hcd)spin_lock_irqsave (&xhci->lock, flags); } } -writel (portsc_buf[port_index], ports[port_index]->addr); +xhci_portsc_writel (ports[port_index], portsc_buf[port_index]); } hcd->state =HC_STATE_SUSPENDED ; bus_state->next_statechange = jiffies +msecs_to_jiffies (10 );
@@ -1864,7 +1863,7 @@ static bool xhci_port_missing_cas_quirk(struct xhci_port *port)/* clear wakeup/change bits, and do a warm port reset */ portsc &= ~(PORT_RWC_BITS |PORT_CEC |PORT_WAKE_BITS ); portsc |=PORT_WR ; -writel (portsc, port->addr); +xhci_portsc_writel (port, portsc);/* flush write */ readl (port->addr);return true;
@@ -1944,7 +1943,7 @@ int xhci_bus_resume(struct usb_hcd *hcd) }/* disable wake for all ports, write new link state if needed */ portsc &= ~(PORT_RWC_BITS |PORT_CEC |PORT_WAKE_BITS ); -writel (portsc, ports[port_index]->addr); +xhci_portsc_writel (ports[port_index], portsc); }/* USB2 specific resume signaling delay and U0 link state transition */
drivers/usb/host/xhci-pci.c +1 -1
@@ -964,7 +964,7 @@ static int xhci_pci_poweroff_late(struct usb_hcd *hcd, bool do_wakeup)xhci_dbg (xhci,"port %d-%d in U3 without wakeup, disable it\n" , port->rhub->hcd->self.busnum, port->hcd_portnum +1 ); portsc =xhci_port_state_to_neutral (portsc); -writel (portsc |PORT_PE , port->addr); +xhci_portsc_writel (port, portsc |PORT_PE ); }return 0 ;
drivers/usb/host/xhci-trace.h +5 -0
@@ -540,6 +540,11 @@ DEFINE_EVENT(xhci_log_portsc, xhci_hub_status_data,TP_ARGS (portnum, portsc) ); +DEFINE_EVENT (xhci_log_portsc, xhci_portsc_writel, + TP_PROTO(u32 portnum, u32 portsc), +TP_ARGS (portnum, portsc) +); + DECLARE_EVENT_CLASS(xhci_log_doorbell, TP_PROTO(u32 slot, u32 doorbell), TP_ARGS(slot, doorbell),
drivers/usb/host/xhci.c +8 -1
@@ -39,6 +39,13 @@ static unsigned long long quirks;module_param (quirks, ullong,S_IRUGO );MODULE_PARM_DESC (quirks,"Bit flags for quirks to be enabled as default" ); +void xhci_portsc_writel (struct xhci_port *port,u32 val) +{ +trace_xhci_portsc_writel (port->hcd_portnum, val); +writel (val, port->addr); +} +EXPORT_SYMBOL_GPL (xhci_portsc_writel); +static bool td_on_ring (struct xhci_td *td,struct xhci_ring *ring) {struct xhci_segment *seg = ring->first_seg;
@@ -826,7 +833,7 @@ static void xhci_disable_hub_port_wake(struct xhci_hcd *xhci, t2 |=PORT_CSC ;if (t1 != t2) { -writel (t2, rhub->ports[i]->addr); +xhci_portsc_writel (rhub->ports[i], t2);xhci_dbg (xhci,"config port %d-%d wake bits, portsc: 0x%x, write: 0x%x\n" , rhub->hcd->self.busnum, i +1 , portsc, t2); }
drivers/usb/host/xhci.h +1 -0
@@ -1974,6 +1974,7 @@ void xhci_cleanup_command_queue(struct xhci_hcd *xhci);void inc_deq (struct xhci_hcd *xhci,struct xhci_ring *ring);unsigned int count_trbs (u64 addr,u64 len);void xhci_process_cancelled_tds (struct xhci_virt_ep *ep); +void xhci_portsc_writel (struct xhci_port *port,u32 val);/* xHCI roothub code */ void xhci_set_link_state(struct xhci_hcd *xhci, struct xhci_port *port,
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/7 usb: xhci: add helper to read PORTSC register
From: Niklas Neronin <[email protected]>
[ Upstream commit 511afe80b82d2b21086e459906e6c97b4eaeed20 ]
Add a dedicated helper function to read the USB Port Status and Control (PORTSC) register. This complements xhci_portsc_writel() and improves code clarity by providing a clear counterpart for reading the register.
Stable-dep-of: 78203d5b54a4 ("usb: xhci: bail out of setup if the controller is inaccessible")
--- drivers/usb/host/xhci-debugfs.c | 4 +- drivers/usb/host/xhci-hub.c | 68 ++++++++++++++++----------------- drivers/usb/host/xhci-pci.c | 2 +- drivers/usb/host/xhci-ring.c | 2 +- drivers/usb/host/xhci-tegra.c | 12 +++--- drivers/usb/host/xhci.c | 14 +++++-- drivers/usb/host/xhci.h | 1 + 7 files changed, 55 insertions(+), 48 deletions(-)
drivers/usb/host/xhci-debugfs.c +2 -2
@@ -349,7 +349,7 @@ static int xhci_portsc_show(struct seq_file *s, void *unused)u32 portsc;char str[XHCI_MSG_MAX ]; - portsc =readl (port->addr); + portsc =xhci_portsc_readl (port);seq_printf (s,"%s\n" ,xhci_decode_portsc (str, portsc));return 0 ;
@@ -379,7 +379,7 @@ static ssize_t xhci_port_write(struct file *file, const char __user *ubuf,return count;spin_lock_irqsave (&xhci->lock, flags);/* compliance mode can only be enabled on ports in RxDetect */ - portsc =readl (port->addr); + portsc =xhci_portsc_readl (port);if ((portsc &PORT_PLS_MASK ) !=XDEV_RXDETECT ) {spin_unlock_irqrestore (&xhci->lock, flags);return -EPERM ;
drivers/usb/host/xhci-hub.c +34 -34
265 lines not on this page · raw message
drivers/usb/host/xhci-pci.c +1 -1
@@ -942,7 +942,7 @@ static int xhci_pci_poweroff_late(struct usb_hcd *hcd, bool do_wakeup)for (i =0 ; i <HCS_MAX_PORTS (xhci->hcs_params1); i++) { port = &xhci->hw_ports[i]; - portsc =readl (port->addr); + portsc =xhci_portsc_readl (port);if ((portsc &PORT_PLS_MASK ) !=XDEV_U3 )continue ;
drivers/usb/host/xhci-ring.c +1 -1
@@ -2019,7 +2019,7 @@ static void handle_port_status(struct xhci_hcd *xhci, hcd = port->rhub->hcd; bus_state = &port->rhub->bus_state; hcd_portnum = port->hcd_portnum; - portsc =readl (port->addr); + portsc =xhci_portsc_readl (port);xhci_dbg (xhci,"Port change event, %d-%d, id %d, portsc: 0x%x\n" , hcd->self.busnum, hcd_portnum +1 , port_id, portsc);
drivers/usb/host/xhci-tegra.c +6 -6
@@ -1974,7 +1974,7 @@ static bool xhci_hub_ports_suspended(struct xhci_hub *hub)u32 value;for (i =0 ; i < hub->num_ports; i++) { - value =readl (hub->ports[i]->addr); + value =xhci_portsc_readl (hub->ports[i]);if ((value &PORT_PE ) ==0 )continue ;
@@ -2100,7 +2100,7 @@ static void tegra_xhci_enable_phy_sleepwalk_wake(struct tegra_xusb *tegra)if (!is_host_mode_phy (tegra, i, j))continue ; - portsc =readl (rhub->ports[index]->addr); + portsc =xhci_portsc_readl (rhub->ports[index]); speed =tegra_xhci_portsc_to_speed (tegra, portsc);tegra_xusb_padctl_enable_phy_sleepwalk (padctl, phy, speed);tegra_xusb_padctl_enable_phy_wake (padctl, phy);
@@ -2195,7 +2195,7 @@ static int tegra_xusb_enter_elpg(struct tegra_xusb *tegra, bool runtime)for (i =0 ; i < xhci->usb2_rhub.num_ports; i++) {if (!xhci->usb2_rhub.ports[i])continue ; - portsc =readl (xhci->usb2_rhub.ports[i]->addr); + portsc =xhci_portsc_readl (xhci->usb2_rhub.ports[i]); tegra->lp0_utmi_pad_mask &= ~BIT (i);if (((portsc &PORT_PLS_MASK ) ==XDEV_U3 ) || ((portsc &DEV_SPEED_MASK ) ==XDEV_FS )) tegra->lp0_utmi_pad_mask |=BIT (i);
@@ -2722,7 +2722,7 @@ static int tegra_xhci_hub_control(struct usb_hcd *hcd, u16 type_req, u16 value,while (i--) {if (!test_bit (i, &bus_state->resuming_ports))continue ; - portsc =readl (ports[i]->addr); + portsc =xhci_portsc_readl (ports[i]);if ((portsc &PORT_PLS_MASK ) ==XDEV_RESUME )tegra_phy_xusb_utmi_pad_power_on (tegra_xusb_get_phy (tegra,"usb2" , (int ) i));
@@ -2740,7 +2740,7 @@ static int tegra_xhci_hub_control(struct usb_hcd *hcd, u16 type_req, u16 value,if (!index || index > rhub->num_ports)return -EPIPE ; ports = rhub->ports; - portsc =readl (ports[port]->addr); + portsc =xhci_portsc_readl (ports[port]);if (portsc &PORT_CONNECT )tegra_phy_xusb_utmi_pad_power_on (phy); }
@@ -2759,7 +2759,7 @@ static int tegra_xhci_hub_control(struct usb_hcd *hcd, u16 type_req, u16 value,if ((type_req == ClearPortFeature) && (value ==USB_PORT_FEAT_C_CONNECTION )) { ports = rhub->ports; - portsc =readl (ports[port]->addr); + portsc =xhci_portsc_readl (ports[port]);if (!(portsc &PORT_CONNECT )) {/* We don't suspend the PAD while HNP role swap happens on the OTG * port
drivers/usb/host/xhci.c +10 -4
@@ -46,6 +46,12 @@ void xhci_portsc_writel(struct xhci_port *port, u32 val) }EXPORT_SYMBOL_GPL (xhci_portsc_writel); +u32 xhci_portsc_readl (struct xhci_port *port) +{ +return readl (port->addr); +} +EXPORT_SYMBOL_GPL (xhci_portsc_readl); +static bool td_on_ring (struct xhci_td *td,struct xhci_ring *ring) {struct xhci_segment *seg = ring->first_seg;
@@ -365,7 +371,7 @@ static void compliance_mode_recovery(struct timer_list *t)return ;for (i =0 ; i < rhub->num_ports; i++) { - temp =readl (rhub->ports[i]->addr); + temp =xhci_portsc_readl (rhub->ports[i]);if ((temp &PORT_PLS_MASK ) ==USB_SS_PORT_LS_COMP_MOD ) {/* * Compliance Mode Detected. Letting USB Core
@@ -820,7 +826,7 @@ static void xhci_disable_hub_port_wake(struct xhci_hcd *xhci,spin_lock_irqsave (&xhci->lock, flags);for (i =0 ; i < rhub->num_ports; i++) { - portsc =readl (rhub->ports[i]->addr); + portsc =xhci_portsc_readl (rhub->ports[i]); t1 =xhci_port_state_to_neutral (portsc); t2 = t1;
@@ -860,7 +866,7 @@ static bool xhci_pending_portevent(struct xhci_hcd *xhci) port_index = xhci->usb2_rhub.num_ports; ports = xhci->usb2_rhub.ports;while (port_index--) { - portsc =readl (ports[port_index]->addr); + portsc =xhci_portsc_readl (ports[port_index]);if (portsc &PORT_CHANGE_MASK || (portsc &PORT_PLS_MASK ) ==XDEV_RESUME )return true;
@@ -868,7 +874,7 @@ static bool xhci_pending_portevent(struct xhci_hcd *xhci) port_index = xhci->usb3_rhub.num_ports; ports = xhci->usb3_rhub.ports;while (port_index--) { - portsc =readl (ports[port_index]->addr); + portsc =xhci_portsc_readl (ports[port_index]);if (portsc & (PORT_CHANGE_MASK |PORT_CAS ) || (portsc &PORT_PLS_MASK ) ==XDEV_RESUME )return true;
drivers/usb/host/xhci.h +1 -0
@@ -1975,6 +1975,7 @@ void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring);unsigned int count_trbs (u64 addr,u64 len);void xhci_process_cancelled_tds (struct xhci_virt_ep *ep);void xhci_portsc_writel (struct xhci_port *port,u32 val); +u32 xhci_portsc_readl (struct xhci_port *port);/* xHCI roothub code */ void xhci_set_link_state(struct xhci_hcd *xhci, struct xhci_port *port,
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/7 usb: xhci: add USB Port Register Set struct
In reply to Sasha LevinFrom: Niklas Neronin <[email protected]>
[ Upstream commit 377a91594e008848363641d07f51d2e48f4bdde5 ]
Introduce a new struct for the Host Controller USB Port Register Set to enhance readability and maintainability.
The Host Controller Operational Registers (struct 'xhci_op_regs') span from offset 0x0 to 0x3FF and consist of fixed fields. Following these fixed fields are the Host Controller USB Port Register Sets, which are dynamic and repeat from 1 to MaxPorts, as defined by HCSPARAMS1.
Currently, the struct 'xhci_op_regs' includes:
__le32 port_status_base; The first PORTSC
__le32 port_power_base; The first PORTPMSC
__le32 port_link_base; The first PORTLI
__le32 reserved5; The first PORTHLPMC, not reserved
__le32 reserved6[NUM_PORT_REGS*254]; Port registers 2 to MaxPorts
Replace this with the simpler:
struct xhci_port_regs port_regs[]; Port registers 1 to MaxPorts
Host Controller USB Port Register Set:
| Offset | Mnemonic | Register Name
--------------------------------------------------------------------------
| 0x0 | PORTSC | Port Status and Control | 0x4 | PORTPMSC | Port Power Management Status and Control | 0x8 | PORTLI | Port Link Info | 0xC | PORTHLPMC | Port Hardware LPM Control
Stable-dep-of: 78203d5b54a4 ("usb: xhci: bail out of setup if the controller is inaccessible")
--- drivers/usb/host/xhci-mem.c | 3 +-- drivers/usb/host/xhci.h | 36 ++++++++++++++++-------------------- 2 files changed, 17 insertions(+), 22 deletions(-)
drivers/usb/host/xhci-mem.c +1 -2
@@ -2182,8 +2182,7 @@ static int xhci_setup_port_arrays(struct xhci_hcd *xhci, gfp_t flags)return -ENOMEM ;for (i =0 ; i < num_ports; i++) { - xhci->hw_ports[i].addr = &xhci->op_regs->port_status_base + -NUM_PORT_REGS * i; + xhci->hw_ports[i].addr = &xhci->op_regs->port_regs[i].portsc; xhci->hw_ports[i].hw_portnum = i;init_completion (&xhci->hw_ports[i].rexit_done);
drivers/usb/host/xhci.h +16 -20
@@ -66,14 +66,25 @@ struct xhci_cap_regs {/* Reserved up to (CAPLENGTH - 0x1C) */ }; -/* Number of registers per port */ -#define NUM_PORT_REGS 4 -#define PORTSC 0#define PORTPMSC 1#define PORTLI 2#define PORTHLPMC 3 +/* +* struct xhci_port_regs - Host Controller USB Port Register Set. xHCI spec 5.4.8 +* @portsc: Port Status and Control +* @portpmsc: Port Power Management Status and Control +* @portli: Port Link Info +* @porthlmpc: Port Hardware LPM Control +*/ +struct xhci_port_regs { +__le32 portsc; +__le32 portpmsc; +__le32 portli; +__le32 porthlmpc; +}; +/** * struct xhci_op_regs - xHCI Host Controller Operational Registers. * @command: USBCMD - xHC command register
@@ -85,16 +96,7 @@ struct xhci_cap_regs {* @cmd_ring: CRP - 64-bit Command Ring Pointer * @dcbaa_ptr: DCBAAP - 64-bit Device Context Base Address Array Pointer * @config_reg: CONFIG - Configure Register -* @port_status_base: PORTSCn - base address for Port Status and Control -* Each port has a Port Status and Control register, -* followed by a Port Power Management Status and Control -* register, a Port Link Info register, and a reserved -* register. -* @port_power_base: PORTPMSCn - base address for -* Port Power Management Status and Control -* @port_link_base: PORTLIn - base address for Port Link Info (current -* Link PM state and control) for USB 2.1 and USB 3.0 -* devices. +* @port_regs: Port Register Sets, from 1 to MaxPorts (defined by HCSPARAMS1). */ struct xhci_op_regs {__le32 command;
@@ -110,13 +112,7 @@ struct xhci_op_regs {__le32 config_reg;/* rsvd: offset 0x3C-3FF */ __le32 reserved4[241 ]; -/* port 1 registers, which serve as a base address for other ports */ -__le32 port_status_base; -__le32 port_power_base; -__le32 port_link_base; -__le32 reserved5; -/* registers for ports 2-255 */ -__le32 reserved6[NUM_PORT_REGS *254 ]; +struct xhci_port_regs port_regs[]; };/* USBCMD - USB command - command bitmasks */
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 4/7 usb: xhci: implement USB Port Register Set struct
In reply to Sasha LevinFrom: Niklas Neronin <[email protected]>
[ Upstream commit f2469d89a70cc6eb3d8141770995a3b251afa6ff ]
Previously, each port's 'addr' field pointed to the base of the Host Controller USB Port Register Set, and specific registers were accessed using macros such as (port->addr + PORTPMSC).
This patch replaces the raw '__le32 __iomem *addr' pointer with a typed 'struct xhci_port_regs __iomem *port_reg' pointer. With this change, individual registers can be accessed directly through the structure fields:
Before:
port->addr
port->addr + PORTPMSC
port->addr + PORTLI
port->addr + PORTHLPMC
After:
port->port_reg->portsc
port->port_reg->portpmsc
port->port_reg->portli
port->port_reg->porthlpmc
This improves code readability and makes register access more intuitive by using named struct members instead of pointer arithmetic and macros.
[ This stable tree's xhci_portsc_writel() takes an already-different
trace_xhci_portsc_writel() signature (u32 portnum, u32 portsc) rather than upstream's (struct xhci_port *port, u32 portsc), stemming from local commit 3db853f6be156 ("usb: xhci: add tracing for PORTSC register writes"). Kept the existing trace_xhci_portsc_writel(port->hcd_portnum, val) call as-is and only applied the port_reg->portsc register-access conversion from this commit. ]
Stable-dep-of: 78203d5b54a4 ("usb: xhci: bail out of setup if the controller is inaccessible")
--- drivers/usb/host/xhci-hub.c | 16 ++++++++-------- drivers/usb/host/xhci-mem.c | 2 +- drivers/usb/host/xhci.c | 29 ++++++++++++++--------------- drivers/usb/host/xhci.h | 7 +------ 4 files changed, 24 insertions(+), 30 deletions(-)
drivers/usb/host/xhci-hub.c +8 -8
@@ -714,9 +714,9 @@ static void xhci_port_set_test_mode(struct xhci_hcd *xhci,/* xhci only supports test mode for usb2 ports */ port = xhci->usb2_rhub.ports[wIndex]; - temp =readl (port->addr +PORTPMSC ); + temp =readl (&port->port_reg->portpmsc); temp |= test_mode <<PORT_TEST_MODE_SHIFT ; -writel (temp, port->addr +PORTPMSC ); +writel (temp, &port->port_reg->portpmsc); xhci->test_mode = test_mode;if (test_mode ==USB_TEST_FORCE_ENABLE )xhci_start (xhci);
@@ -1280,7 +1280,7 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, retval = -EINVAL ;break ; } - port_li =readl (port->addr +PORTLI ); + port_li =readl (&port->port_reg->portli); status =xhci_get_ext_port_status (temp, port_li);put_unaligned_le32 (status, &buf[4 ]); }
@@ -1516,18 +1516,18 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,case USB_PORT_FEAT_U1_TIMEOUT :if (hcd->speed <HCD_USB3 ) goto error; - temp =readl (port->addr +PORTPMSC ); + temp =readl (&port->port_reg->portpmsc); temp &= ~PORT_U1_TIMEOUT_MASK ; temp |=PORT_U1_TIMEOUT (timeout); -writel (temp, port->addr +PORTPMSC ); +writel (temp, &port->port_reg->portpmsc);break ;case USB_PORT_FEAT_U2_TIMEOUT :if (hcd->speed <HCD_USB3 ) goto error; - temp =readl (port->addr +PORTPMSC ); + temp =readl (&port->port_reg->portpmsc); temp &= ~PORT_U2_TIMEOUT_MASK ; temp |=PORT_U2_TIMEOUT (timeout); -writel (temp, port->addr +PORTPMSC ); +writel (temp, &port->port_reg->portpmsc);break ;case USB_PORT_FEAT_TEST :/* 4.19.6 Port Test Modes (USB2 Test Mode) */
@@ -1964,7 +1964,7 @@ int xhci_bus_resume(struct usb_hcd *hcd)/* poll for U0 link state complete, both USB2 and USB3 */ for_each_set_bit (port_index, &bus_state->bus_suspended,BITS_PER_LONG ) { - sret =xhci_handshake (ports[port_index]->addr,PORT_PLC , + sret =xhci_handshake (&ports[port_index]->port_reg->portsc,PORT_PLC ,PORT_PLC ,10 *1000 );if (sret) { xhci_warn(xhci, "port %d-%d resume PLC timeout\n",
drivers/usb/host/xhci-mem.c +1 -1
@@ -2182,7 +2182,7 @@ static int xhci_setup_port_arrays(struct xhci_hcd *xhci, gfp_t flags)return -ENOMEM ;for (i =0 ; i < num_ports; i++) { - xhci->hw_ports[i].addr = &xhci->op_regs->port_regs[i].portsc; + xhci->hw_ports[i].port_reg = &xhci->op_regs->port_regs[i]; xhci->hw_ports[i].hw_portnum = i;init_completion (&xhci->hw_ports[i].rexit_done);
drivers/usb/host/xhci.c +14 -15
@@ -42,13 +42,13 @@ MODULE_PARM_DESC(quirks, "Bit flags for quirks to be enabled as default");void xhci_portsc_writel (struct xhci_port *port,u32 val) {trace_xhci_portsc_writel (port->hcd_portnum, val); -writel (val, port->addr); +writel (val, &port->port_reg->portsc); }EXPORT_SYMBOL_GPL (xhci_portsc_writel);u32 xhci_portsc_readl (struct xhci_port *port) { -return readl (port->addr); +return readl (&port->port_reg->portsc); }EXPORT_SYMBOL_GPL (xhci_portsc_readl);
@@ -4467,7 +4467,7 @@ static int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd, {struct xhci_hcd *xhci =hcd_to_xhci (hcd);struct xhci_port **ports; -__le32 __iomem *pm_addr, *hlpm_addr; +struct xhci_port_regs __iomem *port_reg;u32 pm_val, hlpm_val, field;unsigned int port_num;unsigned long flags;
@@ -4492,9 +4492,8 @@ static int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd, ports = xhci->usb2_rhub.ports; port_num = udev->portnum -1 ; - pm_addr = ports[port_num]->addr +PORTPMSC ; - pm_val =readl (pm_addr); - hlpm_addr = ports[port_num]->addr +PORTHLPMC ; + port_reg = ports[port_num]->port_reg; + pm_val =readl (&port_reg->portpmsc);xhci_dbg (xhci,"%s port %d USB2 hardware LPM\n" , enable ?"enable" :"disable" , port_num +1 );
@@ -4523,30 +4522,30 @@ static int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,spin_lock_irqsave (&xhci->lock, flags); hlpm_val =xhci_calculate_usb2_hw_lpm_params (udev); -writel (hlpm_val, hlpm_addr); +writel (hlpm_val, &port_reg->porthlmpc);/* flush write */ -readl (hlpm_addr); +readl (&port_reg->porthlmpc); }else { hird =xhci_calculate_hird_besl (xhci, udev); } pm_val &= ~PORT_HIRD_MASK ; pm_val |=PORT_HIRD (hird) |PORT_RWE |PORT_L1DS (udev->slot_id); -writel (pm_val, pm_addr); - pm_val =readl (pm_addr); +writel (pm_val, &port_reg->portpmsc); + pm_val =readl (&port_reg->portpmsc); pm_val |=PORT_HLE ; -writel (pm_val, pm_addr); +writel (pm_val, &port_reg->portpmsc);/* flush write */ -readl (pm_addr); +readl (&port_reg->portpmsc); }else { pm_val &= ~(PORT_HLE |PORT_RWE |PORT_HIRD_MASK |PORT_L1DS_MASK ); -writel (pm_val, pm_addr); +writel (pm_val, &port_reg->portpmsc);/* flush write */ -readl (pm_addr); +readl (&port_reg->portpmsc);if (udev->usb2_hw_lpm_besl_capable) {spin_unlock_irqrestore (&xhci->lock, flags);xhci_change_max_exit_latency (xhci, udev,0 ); -readl_poll_timeout (ports[port_num]->addr, pm_val, +readl_poll_timeout (&ports[port_num]->port_reg->portsc, pm_val, (pm_val &PORT_PLS_MASK ) ==XDEV_U0 ,100 ,10000 );return 0 ;
drivers/usb/host/xhci.h +1 -6
@@ -66,11 +66,6 @@ struct xhci_cap_regs {/* Reserved up to (CAPLENGTH - 0x1C) */ }; -#define PORTSC 0 -#define PORTPMSC 1 -#define PORTLI 2 -#define PORTHLPMC 3 -/* * struct xhci_port_regs - Host Controller USB Port Register Set. xHCI spec 5.4.8 * @portsc: Port Status and Control
@@ -1479,7 +1474,7 @@ struct xhci_port_cap { };struct xhci_port { -__le32 __iomem *addr; +struct xhci_port_regs __iomem *port_reg;int hw_portnum;int hcd_portnum;struct xhci_hub *rhub;
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 5/7 usb: xhci: use cached HCSPARAMS1 value
In reply to Sasha LevinFrom: Niklas Neronin <[email protected]>
[ Upstream commit 70651cc3f5a4c7cec529f121e36ea3b45ea84778 ]
The Structural Parameters 1 (HCSPARAMS1) register is read and cached in 'xhci->hcs_params1' during host controller initialization. Since this register is read-only and its value remains constant for the lifetime of the controller, re-reading it later is unnecessary.
Replace subsequent register reads with the cached 'xhci->hcs_params1' value to avoid redundant MMIO access.
Stable-dep-of: 78203d5b54a4 ("usb: xhci: bail out of setup if the controller is inaccessible")
--- drivers/usb/host/xhci.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/usb/host/xhci.c +1 -2
@@ -4053,8 +4053,7 @@ int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)xhci_err (xhci,"Error while assigning device slot ID: %s\n" ,xhci_trb_comp_code_string (command->status));xhci_err (xhci,"Max number of devices this xHCI host supports is %u.\n" , -HCS_MAX_SLOTS ( -readl (&xhci->cap_regs->hcs_params1))); +HCS_MAX_SLOTS (xhci->hcs_params1));xhci_free_command (xhci, command);return 0 ; }
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 6/7 usb: xhci: simplify handling of Structural Parameters 1 values
In reply to Sasha LevinFrom: Niklas Neronin <[email protected]>
[ Upstream commit df08973556851b29bd78e79db696d992ed1b43f0 ]
The 32-bit read-only HCSPARAMS1 register contains the following fields:
Bits 7:0 - Number of Device Slots (MaxSlots) Bits 18:8 - Number of Interrupters (MaxIntrs) Bits 23:19 - Reserved Bits 31:24 - Number of Ports (MaxPorts)
Since the register value is constant for the lifetime of the controller, it is cached in 'xhci->hcs_params1'. However, platform drivers may override the number of interrupters through a separate variable, 'xhci->max_interrupters', leaving only the maximum slots and ports values still derived from the cached register.
To simplify the code and improve readability, replace 'xhci->hcs_params1' with two dedicated 'u8' fields: 'xhci->max_slots' and 'xhci->max_ports'. These values are initialized once and used directly instead of calling 'HCS_MAX_SLOTS()' and 'HCS_MAX_PORTS()' macros.
This change reduces code clutter without increasing memory usage.
[ Adjusted for this stable tree, which lacks several upstream prerequisites:
- drivers/usb/host/xhci-debugfs.c: dropped the hunk touching xhci_port_bw_show() and the surrounding SS/HS/FS bandwidth debugfs files, since this tree does not yet have the "port bandwidth" debugfs feature (xhci_get_port_bandwidth() et al.) those hunks apply against.
- drivers/usb/host/xhci.c: dropped the hunk adding xhci_hcd_page_size(), xhci_enable_max_dev_slots(), xhci_set_cmd_ring_deq(), xhci_set_doorbell_ptr() and xhci_set_dev_notifications() helpers, since this tree does not have the xhci_init()/xhci_gen_setup() refactor that split those out of xhci_mem_init()/xhci_run() - xhci_set_cmd_ring_deq() already exists here in its pre-refactor form, so adding the duplicate would fail to build. The max-slot programming that xhci_enable_max_dev_slots() would have simplified still exists here in its pre-refactor, inlined form in xhci_mem_init() (drivers/usb/host/xhci-mem.c); that site was updated the same way upstream updates the helper: it now uses the cached 'xhci->max_slots' instead of re-deriving it via HCS_MAX_SLOTS(readl(&xhci->cap_regs->hcs_params1)).
- drivers/usb/host/xhci.h: this tree already independently carries 'u8 max_slots' and 'u8 max_ports' fields (used only by other, already-applied backports) plus 'u8 isoc_threshold', so the field additions from this commit are not needed; kept this tree's existing field list unchanged.
- Removing 'xhci->hcs_params1' also required converting two more leftover open-coded users this backport's original hunks don't
cover: xhci-mem.c's real_port bounds checks in the TT-info cleanup paths, and xhci-pci.c's xhci_setup_msix(), which computed the MSI-X vector count from HCS_MAX_INTRS(xhci->hcs_params1) directly. The former now use xhci->max_ports; the latter now uses xhci->max_interrupters, which xhci_gen_setup() has already initialized to HCS_MAX_INTRS(hcs_params1) (or a driver override) by the time xhci_setup_msix() runs. ]
Stable-dep-of: 78203d5b54a4 ("usb: xhci: bail out of setup if the controller is inaccessible")
--- drivers/usb/host/xhci-debugfs.c | 10 +++------ drivers/usb/host/xhci-hub.c | 2 +- drivers/usb/host/xhci-mem.c | 37 ++++++++++++++------------------- drivers/usb/host/xhci-pci.c | 4 ++-- drivers/usb/host/xhci-ring.c | 6 ++---- drivers/usb/host/xhci.c | 15 ++++++------- drivers/usb/host/xhci.h | 1 - 7 files changed, 32 insertions(+), 43 deletions(-)
drivers/usb/host/xhci-debugfs.c +3 -7
@@ -632,20 +632,16 @@ void xhci_debugfs_remove_slot(struct xhci_hcd *xhci, int slot_id)static void xhci_debugfs_create_ports (struct xhci_hcd *xhci,struct dentry *parent) { -unsigned int num_ports;char port_name[8 ];struct xhci_port *port;struct dentry *dir; - num_ports =HCS_MAX_PORTS (xhci->hcs_params1); - parent =debugfs_create_dir ("ports" , parent); -while (num_ports--) { -scnprintf (port_name,sizeof (port_name),"port%02d" , - num_ports +1 ); +for (int i =0 ; i < xhci->max_ports; i++) { +scnprintf (port_name,sizeof (port_name),"port%02d" , i +1 ); dir =debugfs_create_dir (port_name, parent); - port = &xhci->hw_ports[num_ports]; + port = &xhci->hw_ports[i];debugfs_create_file ("portsc" ,0644 , dir, port, &port_fops); } }
drivers/usb/host/xhci-hub.c +1 -1
@@ -731,7 +731,7 @@ static int xhci_enter_test_mode(struct xhci_hcd *xhci,/* Disable all Device Slots */ xhci_dbg (xhci,"Disable all slots\n" );spin_unlock_irqrestore (&xhci->lock, *flags); -for (i =1 ; i <=HCS_MAX_SLOTS (xhci->hcs_params1); i++) { +for (i =1 ; i <= xhci->max_slots; i++) {if (!xhci->devs[i])continue ;
drivers/usb/host/xhci-mem.c +16 -21
@@ -780,7 +780,7 @@ static void xhci_free_tt_info(struct xhci_hcd *xhci,* it may not have the real_port set correctly. */ if (virt_dev->real_port ==0 || - virt_dev->real_port >HCS_MAX_PORTS (xhci->hcs_params1)) { + virt_dev->real_port > xhci->max_ports) {xhci_dbg (xhci,"Bad real port.\n" );return ; }
@@ -918,7 +918,7 @@ static void xhci_free_virt_devices_depth_first(struct xhci_hcd *xhci, int slot_ireturn ;if (vdev->real_port ==0 || - vdev->real_port >HCS_MAX_PORTS (xhci->hcs_params1)) { + vdev->real_port > xhci->max_ports) {xhci_dbg (xhci,"Bad vdev->real_port.\n" ); goto out; }
@@ -928,7 +928,7 @@ static void xhci_free_virt_devices_depth_first(struct xhci_hcd *xhci, int slot_i/* is this a hub device that added a tt_info to the tts list */ if (tt_info->slot_id == slot_id) {/* are any devices using this tt_info? */ -for (i =1 ; i <HCS_MAX_SLOTS (xhci->hcs_params1); i++) { +for (i =1 ; i < xhci->max_slots; i++) { vdev = xhci->devs[i];if (vdev && (vdev->tt_info == tt_info)) xhci_free_virt_devices_depth_first(
@@ -1878,7 +1878,7 @@ EXPORT_SYMBOL_GPL(xhci_remove_secondary_interrupter);void xhci_mem_cleanup (struct xhci_hcd *xhci) {struct device *dev =xhci_to_hcd (xhci)->self.sysdev; -int i, j, num_ports; +int i, j;cancel_delayed_work_sync (&xhci->cmd_timer);
@@ -1897,8 +1897,7 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci)xhci_dbg_trace (xhci, trace_xhci_dbg_init,"Freed command ring" );xhci_cleanup_command_queue (xhci); - num_ports =HCS_MAX_PORTS (xhci->hcs_params1); -for (i =0 ; i < num_ports && xhci->rh_bw; i++) { +for (i =0 ; i < xhci->max_ports && xhci->rh_bw; i++) {struct xhci_interval_bw_table *bwt = &xhci->rh_bw[i].bw_table;for (j =0 ; j <XHCI_MAX_INTERVAL ; j++) {struct list_head *ep = &bwt->interval_bw[j].endpoints;
@@ -1907,7 +1906,7 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci) } } -for (i =HCS_MAX_SLOTS (xhci->hcs_params1); i >0 ; i--) +for (i = xhci->max_slots; i >0 ; i--)xhci_free_virt_devices_depth_first (xhci, i);dma_pool_destroy (xhci->segment_pool);
@@ -1938,7 +1937,7 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci)if (!xhci->rh_bw) goto no_bw; -for (i =0 ; i < num_ports; i++) { +for (i =0 ; i < xhci->max_ports; i++) {struct xhci_tt_bw_info *tt, *n;list_for_each_entry_safe (tt, n, &xhci->rh_bw[i].tts, tt_list) {list_del (&tt->tt_list);
@@ -2146,7 +2145,7 @@ static void xhci_create_rhub_port_array(struct xhci_hcd *xhci,if (!rhub->ports)return ; -for (i =0 ; i <HCS_MAX_PORTS (xhci->hcs_params1); i++) { +for (i =0 ; i < xhci->max_ports; i++) {if (xhci->hw_ports[i].rhub != rhub || xhci->hw_ports[i].hcd_portnum ==DUPLICATE_ENTRY )continue ;
@@ -2169,19 +2168,17 @@ static int xhci_setup_port_arrays(struct xhci_hcd *xhci, gfp_t flags) {void __iomem *base;u32 offset; -unsigned int num_ports;int i, j;int cap_count =0 ;u32 cap_start;struct device *dev =xhci_to_hcd (xhci)->self.sysdev; - num_ports =HCS_MAX_PORTS (xhci->hcs_params1); - xhci->hw_ports =kcalloc_node (num_ports,sizeof (*xhci->hw_ports), - flags,dev_to_node (dev)); + xhci->hw_ports =kcalloc_node (xhci->max_ports,sizeof (*xhci->hw_ports), + flags,dev_to_node (dev));if (!xhci->hw_ports)return -ENOMEM ; -for (i =0 ; i < num_ports; i++) { +for (i =0 ; i < xhci->max_ports; i++) { xhci->hw_ports[i].port_reg = &xhci->op_regs->port_regs[i]; xhci->hw_ports[i].hw_portnum = i;
@@ -2189,11 +2186,10 @@ static int xhci_setup_port_arrays(struct xhci_hcd *xhci, gfp_t flags)init_completion (&xhci->hw_ports[i].u3exit_done); } - xhci->rh_bw =kcalloc_node (num_ports,sizeof (*xhci->rh_bw), flags, -dev_to_node (dev)); + xhci->rh_bw =kcalloc_node (xhci->max_ports,sizeof (*xhci->rh_bw), flags,dev_to_node (dev));if (!xhci->rh_bw)return -ENOMEM ; -for (i =0 ; i < num_ports; i++) { +for (i =0 ; i < xhci->max_ports; i++) {struct xhci_interval_bw_table *bw_table;INIT_LIST_HEAD (&xhci->rh_bw[i].tts);
@@ -2230,9 +2226,8 @@ static int xhci_setup_port_arrays(struct xhci_hcd *xhci, gfp_t flags) offset = cap_start;while (offset) { -xhci_add_in_port (xhci, num_ports, base + offset, cap_count); -if (xhci->usb2_rhub.num_ports + xhci->usb3_rhub.num_ports == - num_ports) +xhci_add_in_port (xhci, xhci->max_ports, base + offset, cap_count); +if (xhci->usb2_rhub.num_ports + xhci->usb3_rhub.num_ports == xhci->max_ports)break ; offset =xhci_find_next_ext_cap (base, offset,XHCI_EXT_CAPS_PROTOCOL );
@@ -2433,7 +2428,7 @@ int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)* Program the Number of Device Slots Enabled field in the CONFIG * register with the max value of slots the HC can handle. */ - val =HCS_MAX_SLOTS (readl (&xhci->cap_regs->hcs_params1)); + val = xhci->max_slots;xhci_dbg_trace (xhci, trace_xhci_dbg_init,"// xHC can handle at most %d device slots." , val); val2 =readl (&xhci->op_regs->config_reg);
drivers/usb/host/xhci-pci.c +2 -2
@@ -188,7 +188,7 @@ static int xhci_setup_msix(struct xhci_hcd *xhci)* Add additional 1 vector to ensure always available interrupt. */ xhci->msix_count =min (num_online_cpus () +1 , -HCS_MAX_INTRS (xhci->hcs_params1)); + xhci->max_interrupters); ret =pci_alloc_irq_vectors (pdev, xhci->msix_count, xhci->msix_count,PCI_IRQ_MSIX );
@@ -940,7 +940,7 @@ static int xhci_pci_poweroff_late(struct usb_hcd *hcd, bool do_wakeup)if (!(xhci->quirks &XHCI_RESET_TO_DEFAULT ))return 0 ; -for (i =0 ; i <HCS_MAX_PORTS (xhci->hcs_params1); i++) { +for (i =0 ; i < xhci->max_ports; i++) { port = &xhci->hw_ports[i]; portsc =xhci_portsc_readl (port);
drivers/usb/host/xhci-ring.c +2 -4
@@ -1347,7 +1347,7 @@ void xhci_hc_died(struct xhci_hcd *xhci)xhci_cleanup_command_queue (xhci);/* return any pending urbs, remove may be waiting for them */ -for (i =0 ; i <=HCS_MAX_SLOTS (xhci->hcs_params1); i++) { +for (i =0 ; i <= xhci->max_slots; i++) {if (!xhci->devs[i])continue ;for (j =0 ; j <31 ; j++)
@@ -1979,7 +1979,6 @@ static void handle_port_status(struct xhci_hcd *xhci,struct usb_hcd *hcd;u32 port_id;u32 portsc, cmd_reg; -int max_ports;int slot_id;unsigned int hcd_portnum;struct xhci_bus_state *bus_state;
@@ -1992,9 +1991,8 @@ static void handle_port_status(struct xhci_hcd *xhci, "WARN: xHC returned failed port status event\n"); port_id =GET_PORT_ID (le32_to_cpu (event->generic.field[0 ])); - max_ports =HCS_MAX_PORTS (xhci->hcs_params1); -if ((port_id <=0 ) || (port_id > max_ports)) { +if ((port_id <=0 ) || (port_id > xhci->max_ports)) {xhci_warn (xhci,"Port change event with invalid port ID %d\n" , port_id);inc_deq (xhci, ir->event_ring);
drivers/usb/host/xhci.c +8 -7
@@ -289,8 +289,7 @@ static void xhci_zero_64b_regs(struct xhci_hcd *xhci)if (upper_32_bits (val))xhci_write_64 (xhci,0 , &xhci->op_regs->cmd_ring); - intrs =min_t (u32,HCS_MAX_INTRS (xhci->hcs_params1), -ARRAY_SIZE (xhci->run_regs->ir_set)); + intrs =min_t (u32, xhci->max_interrupters,ARRAY_SIZE (xhci->run_regs->ir_set));for (i =0 ; i < intrs; i++) {struct xhci_intr_reg __iomem *ir;
@@ -4053,7 +4052,7 @@ int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)xhci_err (xhci,"Error while assigning device slot ID: %s\n" ,xhci_trb_comp_code_string (command->status));xhci_err (xhci,"Max number of devices this xHCI host supports is %u.\n" , -HCS_MAX_SLOTS (xhci->hcs_params1)); + xhci->max_slots);xhci_free_command (xhci, command);return 0 ; }
@@ -5245,6 +5244,7 @@ int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks)*/ struct device *dev = hcd->self.sysdev;int retval; +u32 hcs_params1;/* Accept arbitrarily long scatter-gather lists */ hcd->self.sg_tablesize = ~0 ;
@@ -5270,7 +5270,7 @@ int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks) xhci->run_regs = hcd->regs + (readl (&xhci->cap_regs->run_regs_off) &RTSOFF_MASK );/* Cache read-only capability registers */ - xhci->hcs_params1 =readl (&xhci->cap_regs->hcs_params1); + hcs_params1 =readl (&xhci->cap_regs->hcs_params1); xhci->hcs_params2 =readl (&xhci->cap_regs->hcs_params2); xhci->hcs_params3 =readl (&xhci->cap_regs->hcs_params3); xhci->hci_version =HC_VERSION (readl (&xhci->cap_regs->hc_capbase));
@@ -5279,10 +5279,11 @@ int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks) xhci->hcc_params2 =readl (&xhci->cap_regs->hcc_params2); xhci->dma_mask_bits =64 ; + xhci->max_slots =HCS_MAX_SLOTS (hcs_params1); + xhci->max_ports =HCS_MAX_PORTS (hcs_params1);/* xhci-plat or xhci-pci might have set max_interrupters already */ -if ((!xhci->max_interrupters) || - xhci->max_interrupters >HCS_MAX_INTRS (xhci->hcs_params1)) - xhci->max_interrupters =HCS_MAX_INTRS (xhci->hcs_params1); +if ((!xhci->max_interrupters) || xhci->max_interrupters >HCS_MAX_INTRS (hcs_params1)) + xhci->max_interrupters =HCS_MAX_INTRS (hcs_params1); xhci->quirks |= quirks;
drivers/usb/host/xhci.h +0 -1
@@ -1508,7 +1508,6 @@ struct xhci_hcd {struct xhci_doorbell_array __iomem *dba;/* Cached register copies of read-only HC data */ -__u32 hcs_params1;__u32 hcs_params2;__u32 hcs_params3;__u32 hcc_params;
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 7/7 usb: xhci: bail out of setup if the controller is inaccessible
In reply to Sasha LevinFrom: Breno Leitao <[email protected]>
[ Upstream commit 78203d5b54a40f0e36196ebf31c9c7a380fc8811 ]
xhci_gen_setup() locates the operational registers using the capability length read from the very first register:
xhci->op_regs = hcd->regs + HC_LENGTH(readl(&xhci->cap_regs->hc_capbase));
If the controller is dead or has dropped off the bus, that read returns ~0, HC_LENGTH() truncates it to 0xff, and op_regs ends up 0xff bytes past the page-aligned MMIO base, i.e. unaligned. The first access through it, xhci_halt() -> xhci_handshake() reading op_regs->status, is then an unaligned readl() on device memory. arm64 faults on unaligned device accesses, so instead of xhci_handshake() catching the all-ones value and returning -ENODEV, setup oopses:
xhci-pci-renesas 0005:08:00.0: Unable to change power state from D3cold to D0, device inaccessible xhci-pci-renesas 0005:08:00.0: xHCI Host Controller
xhci-pci-renesas 0005:08:00.0: new USB bus registered, assigned bus number 1
Unable to handle kernel paging request at virtual address ffff80030a770103
ESR = 0x0000000096000021
FSC = 0x21: alignment fault
Internal error: Oops: 0000000096000021 [#1] SMP
pc : xhci_halt [xhci_hcd]
Call trace:
xhci_halt
xhci_gen_setup
xhci_pci_setup
usb_add_hcd
usb_hcd_pci_probe
xhci_pci_common_probe
xhci_pci_renesas_probeThis was hit with a Renesas uPD720201 that failed to power up ("Unable to change power state from D3cold to D0, device inaccessible") yet still reached the HCD probe path.
Read the capability register once, and if it reads back the all-ones value (as xhci_handshake() and xhci_reset() already test for), abort setup with -ENODEV before op_regs is derived from it. Reading it once also avoids re-reading a register that may change under a concurrent hot-removal.
--- drivers/usb/host/xhci.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/usb/host/xhci.c +8 -3
@@ -5245,6 +5245,7 @@ int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks)struct device *dev = hcd->self.sysdev;int retval;u32 hcs_params1; +u32 hc_capbase;/* Accept arbitrarily long scatter-gather lists */ hcd->self.sg_tablesize = ~0 ;
@@ -5265,15 +5266,19 @@ int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks)mutex_init (&xhci->mutex); xhci->main_hcd = hcd; xhci->cap_regs = hcd->regs; - xhci->op_regs = hcd->regs + -HC_LENGTH (readl (&xhci->cap_regs->hc_capbase)); + hc_capbase =readl (&xhci->cap_regs->hc_capbase); +if (hc_capbase ==U32_MAX ) { +xhci_warn (xhci,"Host controller not accessible, removed?\n" ); +return -ENODEV ; + } + xhci->op_regs = hcd->regs +HC_LENGTH (hc_capbase); xhci->run_regs = hcd->regs + (readl (&xhci->cap_regs->run_regs_off) &RTSOFF_MASK );/* Cache read-only capability registers */ hcs_params1 =readl (&xhci->cap_regs->hcs_params1); xhci->hcs_params2 =readl (&xhci->cap_regs->hcs_params2); xhci->hcs_params3 =readl (&xhci->cap_regs->hcs_params3); - xhci->hci_version =HC_VERSION (readl (&xhci->cap_regs->hc_capbase)); + xhci->hci_version =HC_VERSION (hc_capbase); xhci->hcc_params =readl (&xhci->cap_regs->hcc_params);if (xhci->hci_version >0x100 ) xhci->hcc_params2 =readl (&xhci->cap_regs->hcc_params2);
Signature · 1 line
2.53.0