All of lore.kernel.org
 help / color / mirror / Atom feed
From: Niklas Neronin <niklas.neronin@linux.intel.com>
To: mathias.nyman@linux.intel.com
Cc: linux-usb@vger.kernel.org, michal.pecio@gmail.com,
	Niklas Neronin <niklas.neronin@linux.intel.com>
Subject: [PATCH v2 05/13] usb: xhci: limit number of ports to 127
Date: Thu, 13 Nov 2025 13:56:32 +0100	[thread overview]
Message-ID: <20251113125640.2875608-6-niklas.neronin@linux.intel.com> (raw)
In-Reply-To: <20251113125640.2875608-1-niklas.neronin@linux.intel.com>

The xHCI driver allocates various port-related structures based on the
maximum number of ports reported by the controller. The Number of Ports
(MaxPorts) field occupies bits 31:24 of the HCSPARAMS1 register and can
represent values up to 255. However, the 'HCS_MAX_PORTS()' macro currently
reads bits 30:24, effectively limiting the maximum to 127.

Fixing the macro increases the reported port limit to 255, which in turn
increases memory usage regardless of how many ports are actually used.

To maintain compatibility and control memory consumption, set
'xhci->max_ports' to the minimum of the value read from 'HCS_MAX_PORTS()'
and 127 (MAX_HC_PORTS). This preserves the existing limit while making
the restriction explicit and easier to adjust in the future.

Summary:
 * Port allocations are now limited to 127.
 * HC max ports macro now correctly reads the MaxPorts value.
 * Macro 'MAX_HC_PORTS' can be modified to set the port limit.

Signed-off-by: Niklas Neronin <niklas.neronin@linux.intel.com>
---
 drivers/usb/host/xhci-caps.h | 4 ++--
 drivers/usb/host/xhci.c      | 2 +-
 drivers/usb/host/xhci.h      | 5 ++++-
 3 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/drivers/usb/host/xhci-caps.h b/drivers/usb/host/xhci-caps.h
index 89bc83e4f1eb..8390c969389e 100644
--- a/drivers/usb/host/xhci-caps.h
+++ b/drivers/usb/host/xhci-caps.h
@@ -12,8 +12,8 @@
 #define HCS_SLOTS_MASK		0xff
 /* bits 8:18, Max Interrupters */
 #define HCS_MAX_INTRS(p)	(((p) >> 8) & 0x7ff)
-/* bits 24:31, Max Ports - max value is 0x7F = 127 ports */
-#define HCS_MAX_PORTS(p)	(((p) >> 24) & 0x7f)
+/* bits 31:24, Max Ports - max value is 255 */
+#define HCS_MAX_PORTS(p)	(((p) >> 24) & 0xff)
 
 /* HCSPARAMS2 - hcs_params2 - bitmasks */
 /* bits 0:3, frames or uframes that SW needs to queue transactions
diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c
index d74f47a75c68..5eb3ea0d9e15 100644
--- a/drivers/usb/host/xhci.c
+++ b/drivers/usb/host/xhci.c
@@ -5436,7 +5436,7 @@ int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks)
 		xhci->hcc_params2 = readl(&xhci->cap_regs->hcc_params2);
 
 	xhci->max_slots = HCS_MAX_SLOTS(hcs_params1);
-	xhci->max_ports = HCS_MAX_PORTS(hcs_params1);
+	xhci->max_ports = min(HCS_MAX_PORTS(hcs_params1), MAX_HC_PORTS);
 	/* xhci-plat or xhci-pci might have set max_interrupters already */
 	if ((!xhci->max_interrupters) || xhci->max_interrupters > HCS_MAX_INTRS(hcs_params1))
 		xhci->max_interrupters = HCS_MAX_INTRS(hcs_params1);
diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h
index acf52112ab4c..e68bf547f90b 100644
--- a/drivers/usb/host/xhci.h
+++ b/drivers/usb/host/xhci.h
@@ -34,7 +34,10 @@
 
 /* Max number of USB devices for any host controller - limit in section 6.1 */
 #define MAX_HC_SLOTS		256
-/* Section 5.3.3 - MaxPorts */
+/*
+ * Max Number of Ports. xHCI specification section 5.3.3
+ * Valid values are in the range of 1 to 255.
+ */
 #define MAX_HC_PORTS		127
 
 /*
-- 
2.50.1


  parent reply	other threads:[~2025-11-13 12:59 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-13 12:56 [PATCH v2 00/13] usb: xhci: Host Controller Capability Registers rework Niklas Neronin
2025-11-13 12:56 ` [PATCH v2 01/13] usb: xhci: remove deprecated TODO comment Niklas Neronin
2025-11-13 12:56 ` [PATCH v2 02/13] usb: xhci: remove unused trace operation and argument Niklas Neronin
2025-11-13 12:56 ` [PATCH v2 03/13] usb: xhci: use cached HCSPARAMS1 value Niklas Neronin
2025-11-13 12:56 ` [PATCH v2 04/13] usb: xhci: simplify handling of Structural Parameters 1 values Niklas Neronin
2025-11-13 12:56 ` Niklas Neronin [this message]
2025-11-13 12:56 ` [PATCH v2 06/13] usb: xhci: limit number of interrupts to 128 Niklas Neronin
2025-11-13 12:56 ` [PATCH v2 07/13] usb: xhci: improve xhci-caps.h comments Niklas Neronin
2025-11-13 12:56 ` [PATCH v2 08/13] usb: xhci: simplify Isochronous Scheduling Threshold handling Niklas Neronin
2025-11-13 12:56 ` [PATCH v2 09/13] usb: xhci: simplify Max Scratchpad buffer macros Niklas Neronin
2025-11-13 12:56 ` [PATCH v2 10/13] usb: xhci: drop xhci-caps.h dependence on xhci-ext-caps.h Niklas Neronin
2025-11-13 12:56 ` [PATCH v2 11/13] usb: xhci: standardize single bit-field macros Niklas Neronin
2025-11-13 12:56 ` [PATCH v2 12/13] usb: xhci: standardize multi " Niklas Neronin
2025-11-13 12:56 ` [PATCH v2 13/13] usb: xhci: use 64-bit Addressing Capability macro Niklas Neronin
2025-11-19 14:00 ` [PATCH v2 00/13] usb: xhci: Host Controller Capability Registers rework Mathias Nyman

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=20251113125640.2875608-6-niklas.neronin@linux.intel.com \
    --to=niklas.neronin@linux.intel.com \
    --cc=linux-usb@vger.kernel.org \
    --cc=mathias.nyman@linux.intel.com \
    --cc=michal.pecio@gmail.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.