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 v3 02/12] usb: xhci: rework Port Speed macros
Date: Thu, 3 Dec 2026 15:42:34 +0100 [thread overview]
Message-ID: <20261203144244.1680587-3-niklas.neronin@linux.intel.com> (raw)
In-Reply-To: <20261203144244.1680587-1-niklas.neronin@linux.intel.com>
The PORTSC Port Speed macros are defined as values pre-shifted into
bits 13:10, along with a set of DEV_*SPEED() helpers that mask and
compare them.
Replace DEV_SPEED_MASK with PORT_SPEED_MASK, defined using GENMASK(),
and replace the pre-shifted XDEV_* speed IDs with plain PORT_SPEED_*
field values. Callers now use FIELD_GET() to extract the field and
compare it directly. Remove the DEV_*SPEED() and DEV_PORT_SPEED()
helpers.
DEV_UNDEFSPEED() and DEV_SUPERSPEEDPLUS() have no users and are dropped.
The SLOT_SPEED_* macros describe Slot Context bits 23:20 and were
built by shifting the pre-shifted PORTSC values again. Move them next
to the other Slot Context 'dev_info' definitions in xhci.h and shift
the speed ID by 20 directly.
Update the Port Speed comment to list all speed IDs defined in xHCI
specification section 7.2.2.1.1.
No functional change.
Signed-off-by: Niklas Neronin <niklas.neronin@linux.intel.com>
---
drivers/usb/host/xhci-debugfs.c | 6 ++---
drivers/usb/host/xhci-hub.c | 8 +++---
drivers/usb/host/xhci-port.h | 46 ++++++++++++++-------------------
drivers/usb/host/xhci-ring.c | 4 +--
drivers/usb/host/xhci-tegra.c | 13 ++++++----
drivers/usb/host/xhci.h | 13 ++++++++--
6 files changed, 48 insertions(+), 42 deletions(-)
diff --git a/drivers/usb/host/xhci-debugfs.c b/drivers/usb/host/xhci-debugfs.c
index 2aa01d99f23d..79bc1f7d745a 100644
--- a/drivers/usb/host/xhci-debugfs.c
+++ b/drivers/usb/host/xhci-debugfs.c
@@ -715,7 +715,7 @@ static int xhci_ss_bw_show(struct seq_file *s, void *unused)
int ret;
struct xhci_hcd *xhci = (struct xhci_hcd *)s->private;
- ret = xhci_port_bw_show(xhci, DEV_PORT_SPEED(XDEV_SS), s);
+ ret = xhci_port_bw_show(xhci, PORT_SPEED_SS, s);
return ret;
}
@@ -724,7 +724,7 @@ static int xhci_hs_bw_show(struct seq_file *s, void *unused)
int ret;
struct xhci_hcd *xhci = (struct xhci_hcd *)s->private;
- ret = xhci_port_bw_show(xhci, DEV_PORT_SPEED(XDEV_HS), s);
+ ret = xhci_port_bw_show(xhci, PORT_SPEED_HS, s);
return ret;
}
@@ -733,7 +733,7 @@ static int xhci_fs_bw_show(struct seq_file *s, void *unused)
int ret;
struct xhci_hcd *xhci = (struct xhci_hcd *)s->private;
- ret = xhci_port_bw_show(xhci, DEV_PORT_SPEED(XDEV_FS), s);
+ ret = xhci_port_bw_show(xhci, PORT_SPEED_FS, s);
return ret;
}
diff --git a/drivers/usb/host/xhci-hub.c b/drivers/usb/host/xhci-hub.c
index 55430b673f1e..ff9d9b26f810 100644
--- a/drivers/usb/host/xhci-hub.c
+++ b/drivers/usb/host/xhci-hub.c
@@ -376,9 +376,11 @@ static void xhci_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
static unsigned int xhci_port_speed(int portsc)
{
- if (DEV_LOWSPEED(portsc))
+ u32 port_speed = FIELD_GET(PORT_SPEED_MASK, portsc);
+
+ if (port_speed == PORT_SPEED_LS)
return USB_PORT_STAT_LOW_SPEED;
- if (DEV_HIGHSPEED(portsc))
+ if (port_speed == PORT_SPEED_HS)
return USB_PORT_STAT_HIGH_SPEED;
/*
* FIXME: Yes, we should check for full speed, but the core uses that as
@@ -1007,7 +1009,7 @@ static u32 xhci_get_ext_port_status(u32 portsc, u32 port_li)
int speed_id;
/* only support rx and tx lane counts of 1 in usb3.1 spec */
- speed_id = DEV_PORT_SPEED(portsc);
+ speed_id = FIELD_GET(PORT_SPEED_MASK, portsc);
ext_stat |= speed_id; /* bits 3:0, RX speed id */
ext_stat |= speed_id << 4; /* bits 7:4, TX speed id */
diff --git a/drivers/usb/host/xhci-port.h b/drivers/usb/host/xhci-port.h
index 45e081a9c510..ec822f2f92d0 100644
--- a/drivers/usb/host/xhci-port.h
+++ b/drivers/usb/host/xhci-port.h
@@ -1,5 +1,6 @@
/* SPDX-License-Identifier: GPL-2.0 */
+#include <linux/bitfield.h>
#include <linux/bits.h>
/* PORTSC - Port Status and Control Register - port_status_base bitmasks */
@@ -33,35 +34,26 @@
/* true: port has power (see HCC_PPC) */
#define PORT_POWER BIT(9)
-/* bits 10:13 indicate device speed:
- * 0 - undefined speed - port hasn't be initialized by a reset yet
- * 1 - full speed
- * 2 - low speed
- * 3 - high speed
- * 4 - super speed
- * 5-15 reserved
+/*
+ * bits 13:10 - Port Speed
+ * Values defined in xHCI specification 7.2.2.1.1:
+ * 0 - undefined speed - port hasn't been initialized by a reset yet
+ * 1 - Full-speed
+ * 2 - Low-speed
+ * 3 - High-speed
+ * 4 - SuperSpeed Gen1 x1
+ * 5 - SuperSpeed Gen2 x1
+ * 6 - SuperSpeed Gen1 x2
+ * 7 - SuperSpeed Gen2 x2
+ * 8-15 Reserved
*/
-#define DEV_SPEED_MASK (0xf << 10)
-#define XDEV_FS (0x1 << 10)
-#define XDEV_LS (0x2 << 10)
-#define XDEV_HS (0x3 << 10)
-#define XDEV_SS (0x4 << 10)
-#define XDEV_SSP (0x5 << 10)
-#define DEV_UNDEFSPEED(p) (((p) & DEV_SPEED_MASK) == (0x0<<10))
-#define DEV_FULLSPEED(p) (((p) & DEV_SPEED_MASK) == XDEV_FS)
-#define DEV_LOWSPEED(p) (((p) & DEV_SPEED_MASK) == XDEV_LS)
-#define DEV_HIGHSPEED(p) (((p) & DEV_SPEED_MASK) == XDEV_HS)
-#define DEV_SUPERSPEED(p) (((p) & DEV_SPEED_MASK) == XDEV_SS)
-#define DEV_SUPERSPEEDPLUS(p) (((p) & DEV_SPEED_MASK) == XDEV_SSP)
-#define DEV_SUPERSPEED_ANY(p) (((p) & DEV_SPEED_MASK) >= XDEV_SS)
-#define DEV_PORT_SPEED(p) (((p) >> 10) & 0x0f)
+#define PORT_SPEED_MASK GENMASK(13, 10)
+#define PORT_SPEED_FS 1
+#define PORT_SPEED_LS 2
+#define PORT_SPEED_HS 3
+#define PORT_SPEED_SS 4
+#define PORT_SPEED_SSP 5
-/* Bits 20:23 in the Slot Context are the speed for the device */
-#define SLOT_SPEED_FS (XDEV_FS << 10)
-#define SLOT_SPEED_LS (XDEV_LS << 10)
-#define SLOT_SPEED_HS (XDEV_HS << 10)
-#define SLOT_SPEED_SS (XDEV_SS << 10)
-#define SLOT_SPEED_SSP (XDEV_SSP << 10)
/* Port Indicator Control */
#define PORT_LED_OFF (0 << 14)
#define PORT_LED_AMBER (1 << 14)
diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index a1fa188680f5..676f71d8305b 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -2142,7 +2142,7 @@ static void handle_port_status(struct xhci_hcd *xhci, union xhci_trb *event)
goto cleanup;
}
- if (DEV_SUPERSPEED_ANY(portsc)) {
+ if (FIELD_GET(PORT_SPEED_MASK, portsc) >= PORT_SPEED_SS) {
xhci_dbg(xhci, "remote wake SS port %d\n", port_id);
/* Set a flag to say the port signaled remote wakeup,
* so we can tell the difference between the end of
@@ -2175,7 +2175,7 @@ static void handle_port_status(struct xhci_hcd *xhci, union xhci_trb *event)
}
if ((portsc & PORT_PLC) &&
- DEV_SUPERSPEED_ANY(portsc) &&
+ FIELD_GET(PORT_SPEED_MASK, portsc) >= PORT_SPEED_SS &&
((portsc & PORT_PLS_MASK) == XDEV_U0 ||
(portsc & PORT_PLS_MASK) == XDEV_U1 ||
(portsc & PORT_PLS_MASK) == XDEV_U2)) {
diff --git a/drivers/usb/host/xhci-tegra.c b/drivers/usb/host/xhci-tegra.c
index 1a14aea58b0b..b31c7b087fc7 100644
--- a/drivers/usb/host/xhci-tegra.c
+++ b/drivers/usb/host/xhci-tegra.c
@@ -2107,16 +2107,18 @@ static void tegra_xusb_restore_context(struct tegra_xusb *tegra)
static enum usb_device_speed tegra_xhci_portsc_to_speed(struct tegra_xusb *tegra, u32 portsc)
{
- if (DEV_LOWSPEED(portsc))
+ u32 port_speed = FIELD_GET(PORT_SPEED_MASK, portsc);
+
+ if (port_speed == PORT_SPEED_LS)
return USB_SPEED_LOW;
- if (DEV_HIGHSPEED(portsc))
+ if (port_speed == PORT_SPEED_HS)
return USB_SPEED_HIGH;
- if (DEV_FULLSPEED(portsc))
+ if (port_speed == PORT_SPEED_FS)
return USB_SPEED_FULL;
- if (DEV_SUPERSPEED_ANY(portsc))
+ if (port_speed >= PORT_SPEED_SS)
return USB_SPEED_SUPER;
return USB_SPEED_UNKNOWN;
@@ -2255,7 +2257,8 @@ static int tegra_xusb_enter_elpg(struct tegra_xusb *tegra, bool is_auto_resume)
continue;
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))
+ if (((portsc & PORT_PLS_MASK) == XDEV_U3) ||
+ (FIELD_GET(PORT_SPEED_MASK, portsc) == PORT_SPEED_FS))
tegra->lp0_utmi_pad_mask |= BIT(i);
}
diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h
index ec4bfeb4887c..61b132027963 100644
--- a/drivers/usb/host/xhci.h
+++ b/drivers/usb/host/xhci.h
@@ -12,6 +12,7 @@
#ifndef __LINUX_XHCI_HCD_H
#define __LINUX_XHCI_HCD_H
+#include <linux/bitfield.h>
#include <linux/bits.h>
#include <linux/usb.h>
#include <linux/timer.h>
@@ -362,9 +363,17 @@ struct xhci_slot_ctx {
/* dev_info bitmasks */
/* Route String - 0:19 */
#define ROUTE_STRING_MASK (0xfffff)
-/* Device speed - values defined by PORTSC Device Speed field - 20:23 */
+/*
+ * bits 23:20 - Device speed, values defined by PORTSC Speed field
+ * xHCI version 1.2 onwards these bits are Reserved
+ */
#define DEV_SPEED (0xf << 20)
#define GET_DEV_SPEED(n) (((n) & DEV_SPEED) >> 20)
+#define SLOT_SPEED_FS (PORT_SPEED_FS << 20)
+#define SLOT_SPEED_LS (PORT_SPEED_LS << 20)
+#define SLOT_SPEED_HS (PORT_SPEED_HS << 20)
+#define SLOT_SPEED_SS (PORT_SPEED_SS << 20)
+#define SLOT_SPEED_SSP (PORT_SPEED_SSP << 20)
/* bit 24 reserved */
/* Is this LS/FS device connected through a HS hub? - bit 25 */
#define DEV_MTT BIT(25)
@@ -2412,7 +2421,7 @@ static inline const char *xhci_decode_portsc(char *str, u32 portsc)
if (portsc == ~(u32)0)
return str;
- ret += sprintf(str + ret, "Speed=%d ", DEV_PORT_SPEED(portsc));
+ ret += sprintf(str + ret, "Speed=%lu ", FIELD_GET(PORT_SPEED_MASK, portsc));
ret += sprintf(str + ret, "Link=%s ", xhci_portsc_link_state_string(portsc));
/* RO/ROS: Read-only */
--
2.50.1
next prev parent reply other threads:[~2026-12-03 14:43 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 14:42 [PATCH v3 00/12] usb: xhci: rework xHCI Port macros Niklas Neronin
2026-12-03 14:42 ` [PATCH v3 01/12] usb: xhci: use BIT() for single bit port register flags Niklas Neronin
2026-12-03 14:42 ` Niklas Neronin [this message]
2026-12-03 14:42 ` [PATCH v3 03/12] usb: xhci: rework Port Link State macros Niklas Neronin
2026-12-03 14:59 ` sashiko-bot
2026-12-03 14:42 ` [PATCH v3 04/12] usb: xhci: rework Port Indicator Control macro Niklas Neronin
2026-12-03 14:42 ` [PATCH v3 05/12] usb: xhci: rework USB3 PORTPMSC macros Niklas Neronin
2026-12-03 14:50 ` sashiko-bot
2026-12-03 14:42 ` [PATCH v3 06/12] usb: xhci: rework USB2 " Niklas Neronin
2026-12-03 14:42 ` [PATCH v3 07/12] usb: xhci: rework USB3 PORTLI macros Niklas Neronin
2026-12-03 14:42 ` [PATCH v3 08/12] usb: xhci: rework USB2 " Niklas Neronin
2026-12-03 14:42 ` [PATCH v3 09/12] usb: xhci: document USB3 PORTHLPMC register layout Niklas Neronin
2026-12-03 14:42 ` [PATCH v3 10/12] usb: xhci: rework USB2 PORTHLPMC macros Niklas Neronin
2026-12-03 14:42 ` [PATCH v3 11/12] usb: xhci: update port register bitfield comments for consistency Niklas Neronin
2026-12-03 14:42 ` [PATCH v3 12/12] usb: xhci: rename PORTSC field macros to xHCI spec abbreviations Niklas Neronin
2026-12-03 14:54 ` sashiko-bot
2026-12-03 14:59 ` [PATCH v3 00/12] usb: xhci: rework xHCI Port macros 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=20261203144244.1680587-3-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