xhci: cleanup xhci_hub_control port references

[ Upstream commit faaae0190dcd1e230616c85bbc3b339f27ba5b81 ]

Both port number and port structure of a port are referred to several
times when handing hub requests in xhci.

Use more suitable data types and readable names for these.
Cleanup only, no functional changes

Signed-off-by: Mathias Nyman <mathias.nyman@linux.intel.com>
Link: https://lore.kernel.org/r/20230202150505.618915-6-mathias.nyman@linux.intel.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Stable-dep-of: d7cdfc319b2b ("xhci: track port suspend state correctly in unsuccessful resume cases")
Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
Mathias Nyman
2023-02-02 17:04:59 +02:00
committed by Greg Kroah-Hartman
parent 95b9f1e392
commit e2b4de13e5

View File

@@ -1108,11 +1108,14 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
u16 test_mode = 0; u16 test_mode = 0;
struct xhci_hub *rhub; struct xhci_hub *rhub;
struct xhci_port **ports; struct xhci_port **ports;
struct xhci_port *port;
int portnum1;
rhub = xhci_get_rhub(hcd); rhub = xhci_get_rhub(hcd);
ports = rhub->ports; ports = rhub->ports;
max_ports = rhub->num_ports; max_ports = rhub->num_ports;
bus_state = &rhub->bus_state; bus_state = &rhub->bus_state;
portnum1 = wIndex & 0xff;
spin_lock_irqsave(&xhci->lock, flags); spin_lock_irqsave(&xhci->lock, flags);
switch (typeReq) { switch (typeReq) {
@@ -1146,10 +1149,12 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
spin_unlock_irqrestore(&xhci->lock, flags); spin_unlock_irqrestore(&xhci->lock, flags);
return retval; return retval;
case GetPortStatus: case GetPortStatus:
if (!wIndex || wIndex > max_ports) if (!portnum1 || portnum1 > max_ports)
goto error; goto error;
wIndex--; wIndex--;
temp = readl(ports[wIndex]->addr); port = ports[portnum1 - 1];
temp = readl(port->addr);
if (temp == ~(u32)0) { if (temp == ~(u32)0) {
xhci_hc_died(xhci); xhci_hc_died(xhci);
retval = -ENODEV; retval = -ENODEV;
@@ -1162,7 +1167,7 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
goto error; goto error;
xhci_dbg(xhci, "Get port status %d-%d read: 0x%x, return 0x%x", xhci_dbg(xhci, "Get port status %d-%d read: 0x%x, return 0x%x",
hcd->self.busnum, wIndex + 1, temp, status); hcd->self.busnum, portnum1, temp, status);
put_unaligned(cpu_to_le32(status), (__le32 *) buf); put_unaligned(cpu_to_le32(status), (__le32 *) buf);
/* if USB 3.1 extended port status return additional 4 bytes */ /* if USB 3.1 extended port status return additional 4 bytes */
@@ -1174,7 +1179,7 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
retval = -EINVAL; retval = -EINVAL;
break; break;
} }
port_li = readl(ports[wIndex]->addr + PORTLI); port_li = readl(port->addr + PORTLI);
status = xhci_get_ext_port_status(temp, port_li); status = xhci_get_ext_port_status(temp, port_li);
put_unaligned_le32(status, &buf[4]); put_unaligned_le32(status, &buf[4]);
} }
@@ -1188,11 +1193,14 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
test_mode = (wIndex & 0xff00) >> 8; test_mode = (wIndex & 0xff00) >> 8;
/* The MSB of wIndex is the U1/U2 timeout */ /* The MSB of wIndex is the U1/U2 timeout */
timeout = (wIndex & 0xff00) >> 8; timeout = (wIndex & 0xff00) >> 8;
wIndex &= 0xff; wIndex &= 0xff;
if (!wIndex || wIndex > max_ports) if (!portnum1 || portnum1 > max_ports)
goto error; goto error;
port = ports[portnum1 - 1];
wIndex--; wIndex--;
temp = readl(ports[wIndex]->addr); temp = readl(port->addr);
if (temp == ~(u32)0) { if (temp == ~(u32)0) {
xhci_hc_died(xhci); xhci_hc_died(xhci);
retval = -ENODEV; retval = -ENODEV;
@@ -1202,11 +1210,10 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
/* FIXME: What new port features do we need to support? */ /* FIXME: What new port features do we need to support? */
switch (wValue) { switch (wValue) {
case USB_PORT_FEAT_SUSPEND: case USB_PORT_FEAT_SUSPEND:
temp = readl(ports[wIndex]->addr); temp = readl(port->addr);
if ((temp & PORT_PLS_MASK) != XDEV_U0) { if ((temp & PORT_PLS_MASK) != XDEV_U0) {
/* Resume the port to U0 first */ /* Resume the port to U0 first */
xhci_set_link_state(xhci, ports[wIndex], xhci_set_link_state(xhci, port, XDEV_U0);
XDEV_U0);
spin_unlock_irqrestore(&xhci->lock, flags); spin_unlock_irqrestore(&xhci->lock, flags);
msleep(10); msleep(10);
spin_lock_irqsave(&xhci->lock, flags); spin_lock_irqsave(&xhci->lock, flags);
@@ -1215,16 +1222,16 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
* a port unless the port reports that it is in the * a port unless the port reports that it is in the
* enabled (PED = 1,PLS < 3) state. * enabled (PED = 1,PLS < 3) state.
*/ */
temp = readl(ports[wIndex]->addr); temp = readl(port->addr);
if ((temp & PORT_PE) == 0 || (temp & PORT_RESET) if ((temp & PORT_PE) == 0 || (temp & PORT_RESET)
|| (temp & PORT_PLS_MASK) >= XDEV_U3) { || (temp & PORT_PLS_MASK) >= XDEV_U3) {
xhci_warn(xhci, "USB core suspending port %d-%d not in U0/U1/U2\n", xhci_warn(xhci, "USB core suspending port %d-%d not in U0/U1/U2\n",
hcd->self.busnum, wIndex + 1); hcd->self.busnum, portnum1);
goto error; goto error;
} }
slot_id = xhci_find_slot_id_by_port(hcd, xhci, slot_id = xhci_find_slot_id_by_port(hcd, xhci,
wIndex + 1); portnum1);
if (!slot_id) { if (!slot_id) {
xhci_warn(xhci, "slot_id is zero\n"); xhci_warn(xhci, "slot_id is zero\n");
goto error; goto error;
@@ -1234,21 +1241,21 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
xhci_stop_device(xhci, slot_id, 1); xhci_stop_device(xhci, slot_id, 1);
spin_lock_irqsave(&xhci->lock, flags); spin_lock_irqsave(&xhci->lock, flags);
xhci_set_link_state(xhci, ports[wIndex], XDEV_U3); xhci_set_link_state(xhci, port, XDEV_U3);
spin_unlock_irqrestore(&xhci->lock, flags); spin_unlock_irqrestore(&xhci->lock, flags);
msleep(10); /* wait device to enter */ msleep(10); /* wait device to enter */
spin_lock_irqsave(&xhci->lock, flags); spin_lock_irqsave(&xhci->lock, flags);
temp = readl(ports[wIndex]->addr); temp = readl(port->addr);
bus_state->suspended_ports |= 1 << wIndex; bus_state->suspended_ports |= 1 << wIndex;
break; break;
case USB_PORT_FEAT_LINK_STATE: case USB_PORT_FEAT_LINK_STATE:
temp = readl(ports[wIndex]->addr); temp = readl(port->addr);
/* Disable port */ /* Disable port */
if (link_state == USB_SS_PORT_LS_SS_DISABLED) { if (link_state == USB_SS_PORT_LS_SS_DISABLED) {
xhci_dbg(xhci, "Disable port %d-%d\n", xhci_dbg(xhci, "Disable port %d-%d\n",
hcd->self.busnum, wIndex + 1); hcd->self.busnum, portnum1);
temp = xhci_port_state_to_neutral(temp); temp = xhci_port_state_to_neutral(temp);
/* /*
* Clear all change bits, so that we get a new * Clear all change bits, so that we get a new
@@ -1257,18 +1264,17 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
temp |= PORT_CSC | PORT_PEC | PORT_WRC | temp |= PORT_CSC | PORT_PEC | PORT_WRC |
PORT_OCC | PORT_RC | PORT_PLC | PORT_OCC | PORT_RC | PORT_PLC |
PORT_CEC; PORT_CEC;
writel(temp | PORT_PE, ports[wIndex]->addr); writel(temp | PORT_PE, port->addr);
temp = readl(ports[wIndex]->addr); temp = readl(port->addr);
break; break;
} }
/* Put link in RxDetect (enable port) */ /* Put link in RxDetect (enable port) */
if (link_state == USB_SS_PORT_LS_RX_DETECT) { if (link_state == USB_SS_PORT_LS_RX_DETECT) {
xhci_dbg(xhci, "Enable port %d-%d\n", xhci_dbg(xhci, "Enable port %d-%d\n",
hcd->self.busnum, wIndex + 1); hcd->self.busnum, portnum1);
xhci_set_link_state(xhci, ports[wIndex], xhci_set_link_state(xhci, port, link_state);
link_state); temp = readl(port->addr);
temp = readl(ports[wIndex]->addr);
break; break;
} }
@@ -1298,11 +1304,10 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
} }
xhci_dbg(xhci, "Enable compliance mode transition for port %d-%d\n", xhci_dbg(xhci, "Enable compliance mode transition for port %d-%d\n",
hcd->self.busnum, wIndex + 1); hcd->self.busnum, portnum1);
xhci_set_link_state(xhci, ports[wIndex], xhci_set_link_state(xhci, port, link_state);
link_state);
temp = readl(ports[wIndex]->addr); temp = readl(port->addr);
break; break;
} }
/* Port must be enabled */ /* Port must be enabled */
@@ -1313,8 +1318,7 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
/* Can't set port link state above '3' (U3) */ /* Can't set port link state above '3' (U3) */
if (link_state > USB_SS_PORT_LS_U3) { if (link_state > USB_SS_PORT_LS_U3) {
xhci_warn(xhci, "Cannot set port %d-%d link state %d\n", xhci_warn(xhci, "Cannot set port %d-%d link state %d\n",
hcd->self.busnum, wIndex + 1, hcd->self.busnum, portnum1, link_state);
link_state);
goto error; goto error;
} }
@@ -1339,8 +1343,7 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
reinit_completion(&bus_state->u3exit_done[wIndex]); reinit_completion(&bus_state->u3exit_done[wIndex]);
} }
if (pls <= XDEV_U3) /* U1, U2, U3 */ if (pls <= XDEV_U3) /* U1, U2, U3 */
xhci_set_link_state(xhci, ports[wIndex], xhci_set_link_state(xhci, port, USB_SS_PORT_LS_U0);
USB_SS_PORT_LS_U0);
if (!wait_u0) { if (!wait_u0) {
if (pls > XDEV_U3) if (pls > XDEV_U3)
goto error; goto error;
@@ -1350,16 +1353,16 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
if (!wait_for_completion_timeout(&bus_state->u3exit_done[wIndex], if (!wait_for_completion_timeout(&bus_state->u3exit_done[wIndex],
msecs_to_jiffies(500))) msecs_to_jiffies(500)))
xhci_dbg(xhci, "missing U0 port change event for port %d-%d\n", xhci_dbg(xhci, "missing U0 port change event for port %d-%d\n",
hcd->self.busnum, wIndex + 1); hcd->self.busnum, portnum1);
spin_lock_irqsave(&xhci->lock, flags); spin_lock_irqsave(&xhci->lock, flags);
temp = readl(ports[wIndex]->addr); temp = readl(port->addr);
break; break;
} }
if (link_state == USB_SS_PORT_LS_U3) { if (link_state == USB_SS_PORT_LS_U3) {
int retries = 16; int retries = 16;
slot_id = xhci_find_slot_id_by_port(hcd, xhci, slot_id = xhci_find_slot_id_by_port(hcd, xhci,
wIndex + 1); portnum1);
if (slot_id) { if (slot_id) {
/* unlock to execute stop endpoint /* unlock to execute stop endpoint
* commands */ * commands */
@@ -1368,16 +1371,16 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
xhci_stop_device(xhci, slot_id, 1); xhci_stop_device(xhci, slot_id, 1);
spin_lock_irqsave(&xhci->lock, flags); spin_lock_irqsave(&xhci->lock, flags);
} }
xhci_set_link_state(xhci, ports[wIndex], USB_SS_PORT_LS_U3); xhci_set_link_state(xhci, port, USB_SS_PORT_LS_U3);
spin_unlock_irqrestore(&xhci->lock, flags); spin_unlock_irqrestore(&xhci->lock, flags);
while (retries--) { while (retries--) {
usleep_range(4000, 8000); usleep_range(4000, 8000);
temp = readl(ports[wIndex]->addr); temp = readl(port->addr);
if ((temp & PORT_PLS_MASK) == XDEV_U3) if ((temp & PORT_PLS_MASK) == XDEV_U3)
break; break;
} }
spin_lock_irqsave(&xhci->lock, flags); spin_lock_irqsave(&xhci->lock, flags);
temp = readl(ports[wIndex]->addr); temp = readl(port->addr);
bus_state->suspended_ports |= 1 << wIndex; bus_state->suspended_ports |= 1 << wIndex;
} }
break; break;
@@ -1392,39 +1395,38 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
break; break;
case USB_PORT_FEAT_RESET: case USB_PORT_FEAT_RESET:
temp = (temp | PORT_RESET); temp = (temp | PORT_RESET);
writel(temp, ports[wIndex]->addr); writel(temp, port->addr);
temp = readl(ports[wIndex]->addr); temp = readl(port->addr);
xhci_dbg(xhci, "set port reset, actual port %d-%d status = 0x%x\n", xhci_dbg(xhci, "set port reset, actual port %d-%d status = 0x%x\n",
hcd->self.busnum, wIndex + 1, temp); hcd->self.busnum, portnum1, temp);
break; break;
case USB_PORT_FEAT_REMOTE_WAKE_MASK: case USB_PORT_FEAT_REMOTE_WAKE_MASK:
xhci_set_remote_wake_mask(xhci, ports[wIndex], xhci_set_remote_wake_mask(xhci, port, wake_mask);
wake_mask); temp = readl(port->addr);
temp = readl(ports[wIndex]->addr);
xhci_dbg(xhci, "set port remote wake mask, actual port %d-%d status = 0x%x\n", xhci_dbg(xhci, "set port remote wake mask, actual port %d-%d status = 0x%x\n",
hcd->self.busnum, wIndex + 1, temp); hcd->self.busnum, portnum1, temp);
break; break;
case USB_PORT_FEAT_BH_PORT_RESET: case USB_PORT_FEAT_BH_PORT_RESET:
temp |= PORT_WR; temp |= PORT_WR;
writel(temp, ports[wIndex]->addr); writel(temp, port->addr);
temp = readl(ports[wIndex]->addr); temp = readl(port->addr);
break; break;
case USB_PORT_FEAT_U1_TIMEOUT: case USB_PORT_FEAT_U1_TIMEOUT:
if (hcd->speed < HCD_USB3) if (hcd->speed < HCD_USB3)
goto error; goto error;
temp = readl(ports[wIndex]->addr + PORTPMSC); temp = readl(port->addr + PORTPMSC);
temp &= ~PORT_U1_TIMEOUT_MASK; temp &= ~PORT_U1_TIMEOUT_MASK;
temp |= PORT_U1_TIMEOUT(timeout); temp |= PORT_U1_TIMEOUT(timeout);
writel(temp, ports[wIndex]->addr + PORTPMSC); writel(temp, port->addr + PORTPMSC);
break; break;
case USB_PORT_FEAT_U2_TIMEOUT: case USB_PORT_FEAT_U2_TIMEOUT:
if (hcd->speed < HCD_USB3) if (hcd->speed < HCD_USB3)
goto error; goto error;
temp = readl(ports[wIndex]->addr + PORTPMSC); temp = readl(port->addr + PORTPMSC);
temp &= ~PORT_U2_TIMEOUT_MASK; temp &= ~PORT_U2_TIMEOUT_MASK;
temp |= PORT_U2_TIMEOUT(timeout); temp |= PORT_U2_TIMEOUT(timeout);
writel(temp, ports[wIndex]->addr + PORTPMSC); writel(temp, port->addr + PORTPMSC);
break; break;
case USB_PORT_FEAT_TEST: case USB_PORT_FEAT_TEST:
/* 4.19.6 Port Test Modes (USB2 Test Mode) */ /* 4.19.6 Port Test Modes (USB2 Test Mode) */
@@ -1440,13 +1442,16 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
goto error; goto error;
} }
/* unblock any posted writes */ /* unblock any posted writes */
temp = readl(ports[wIndex]->addr); temp = readl(port->addr);
break; break;
case ClearPortFeature: case ClearPortFeature:
if (!wIndex || wIndex > max_ports) if (!portnum1 || portnum1 > max_ports)
goto error; goto error;
port = ports[portnum1 - 1];
wIndex--; wIndex--;
temp = readl(ports[wIndex]->addr); temp = readl(port->addr);
if (temp == ~(u32)0) { if (temp == ~(u32)0) {
xhci_hc_died(xhci); xhci_hc_died(xhci);
retval = -ENODEV; retval = -ENODEV;
@@ -1456,7 +1461,7 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
temp = xhci_port_state_to_neutral(temp); temp = xhci_port_state_to_neutral(temp);
switch (wValue) { switch (wValue) {
case USB_PORT_FEAT_SUSPEND: case USB_PORT_FEAT_SUSPEND:
temp = readl(ports[wIndex]->addr); temp = readl(port->addr);
xhci_dbg(xhci, "clear USB_PORT_FEAT_SUSPEND\n"); xhci_dbg(xhci, "clear USB_PORT_FEAT_SUSPEND\n");
xhci_dbg(xhci, "PORTSC %04x\n", temp); xhci_dbg(xhci, "PORTSC %04x\n", temp);
if (temp & PORT_RESET) if (temp & PORT_RESET)
@@ -1467,20 +1472,18 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
set_bit(wIndex, &bus_state->resuming_ports); set_bit(wIndex, &bus_state->resuming_ports);
usb_hcd_start_port_resume(&hcd->self, wIndex); usb_hcd_start_port_resume(&hcd->self, wIndex);
xhci_set_link_state(xhci, ports[wIndex], xhci_set_link_state(xhci, port, XDEV_RESUME);
XDEV_RESUME);
spin_unlock_irqrestore(&xhci->lock, flags); spin_unlock_irqrestore(&xhci->lock, flags);
msleep(USB_RESUME_TIMEOUT); msleep(USB_RESUME_TIMEOUT);
spin_lock_irqsave(&xhci->lock, flags); spin_lock_irqsave(&xhci->lock, flags);
xhci_set_link_state(xhci, ports[wIndex], xhci_set_link_state(xhci, port, XDEV_U0);
XDEV_U0);
clear_bit(wIndex, &bus_state->resuming_ports); clear_bit(wIndex, &bus_state->resuming_ports);
usb_hcd_end_port_resume(&hcd->self, wIndex); usb_hcd_end_port_resume(&hcd->self, wIndex);
} }
bus_state->port_c_suspend |= 1 << wIndex; bus_state->port_c_suspend |= 1 << wIndex;
slot_id = xhci_find_slot_id_by_port(hcd, xhci, slot_id = xhci_find_slot_id_by_port(hcd, xhci,
wIndex + 1); portnum1);
if (!slot_id) { if (!slot_id) {
xhci_dbg(xhci, "slot_id is zero\n"); xhci_dbg(xhci, "slot_id is zero\n");
goto error; goto error;
@@ -1498,11 +1501,11 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
case USB_PORT_FEAT_C_PORT_LINK_STATE: case USB_PORT_FEAT_C_PORT_LINK_STATE:
case USB_PORT_FEAT_C_PORT_CONFIG_ERROR: case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
xhci_clear_port_change_bit(xhci, wValue, wIndex, xhci_clear_port_change_bit(xhci, wValue, wIndex,
ports[wIndex]->addr, temp); port->addr, temp);
break; break;
case USB_PORT_FEAT_ENABLE: case USB_PORT_FEAT_ENABLE:
xhci_disable_port(hcd, xhci, wIndex, xhci_disable_port(hcd, xhci, wIndex,
ports[wIndex]->addr, temp); port->addr, temp);
break; break;
case USB_PORT_FEAT_POWER: case USB_PORT_FEAT_POWER:
xhci_set_port_power(xhci, hcd, wIndex, false, &flags); xhci_set_port_power(xhci, hcd, wIndex, false, &flags);