public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v5 0/3] PCI: Refactor PCIe speed validation and conversion functions
@ 2026-04-06 10:47 Hans Zhang
  2026-04-06 10:47 ` [PATCH v5 1/3] PCI: Add public pcie_valid_speed() for shared validation Hans Zhang
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Hans Zhang @ 2026-04-06 10:47 UTC (permalink / raw)
  To: bhelgaas, lpieralisi, kw, kwilczynski, mani, ilpo.jarvinen,
	jingoohan1
  Cc: robh, linux-pci, linux-kernel, Hans Zhang

This series refactors PCIe speed validation and conversion logic to
shared functions in the public header, eliminating code duplication
and ensuring consistency across drivers.

---
Changes for v5:
- Rebase to v7.0-rc1. (pci/next tree)

Changes for v4:
https://patchwork.kernel.org/project/linux-pci/patch/20251102143206.111347-1-18255117159@163.com/

- Maintain O(1) array-based lookup for speed conversion (addressing
  performance concerns from v3 feedback)
- Move pcie_valid_speed() and pci_bus_speed2lnkctl2() to pci.h
- Update dwc driver to use the shared functions
- Rebase to v6.18-rc3.

This addresses the feedback from Lukas Wunner and Manivannan Sadhasivam
on the v3 submission, ensuring no runtime performance regression while
achieving code reuse.

Changes for v3:
https://patchwork.kernel.org/project/linux-pci/patch/20250816154633.338653-1-18255117159@163.com/

- Rebase to v6.17-rc1.
- Gentle ping.

Changes for v2:
- s/PCIE_SPEED2LNKCTL2_TLS_ENC/PCIE_SPEED2LNKCTL2_TLS
- The patch commit message were modified.
---

Hans Zhang (3):
  PCI: Add public pcie_valid_speed() for shared validation
  PCI: Move pci_bus_speed2lnkctl2() to public header
  PCI: dwc: Use common speed conversion function

 drivers/pci/controller/dwc/pcie-designware.c | 17 ++-------------
 drivers/pci/pci.h                            | 22 ++++++++++++++++++++
 drivers/pci/pcie/bwctrl.c                    | 22 --------------------
 3 files changed, 24 insertions(+), 37 deletions(-)


base-commit: 525e91d84dc085492b36d4b87abb7c1cc93fcb44
-- 
2.34.1


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v5 1/3] PCI: Add public pcie_valid_speed() for shared validation
  2026-04-06 10:47 [PATCH v5 0/3] PCI: Refactor PCIe speed validation and conversion functions Hans Zhang
@ 2026-04-06 10:47 ` Hans Zhang
  2026-04-06 10:47 ` [PATCH v5 2/3] PCI: Move pci_bus_speed2lnkctl2() to public header Hans Zhang
  2026-04-06 10:47 ` [PATCH v5 3/3] PCI: dwc: Use common speed conversion function Hans Zhang
  2 siblings, 0 replies; 12+ messages in thread
From: Hans Zhang @ 2026-04-06 10:47 UTC (permalink / raw)
  To: bhelgaas, lpieralisi, kw, kwilczynski, mani, ilpo.jarvinen,
	jingoohan1
  Cc: robh, linux-pci, linux-kernel, Hans Zhang

Extract the PCIe speed validation logic from bwctrl.c's static
pcie_valid_speed() into a public static inline function in pci.h.

This allows consistent speed range checks (2.5GT/s to 64.0GT/s) across
multiple drivers and functions, avoiding duplicate code and ensuring
validation consistency as per PCIe specifications.

Signed-off-by: Hans Zhang <18255117159@163.com>
---
 drivers/pci/pci.h         | 5 +++++
 drivers/pci/pcie/bwctrl.c | 5 -----
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index 4a14f88e543a..f0a082bfd6f1 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -606,6 +606,11 @@ void pci_bus_put(struct pci_bus *bus);
 	 (speed) == PCIE_SPEED_2_5GT  ?  2500*8/10 : \
 	 0)
 
+static inline bool pcie_valid_speed(enum pci_bus_speed speed)
+{
+	return (speed >= PCIE_SPEED_2_5GT) && (speed <= PCIE_SPEED_64_0GT);
+}
+
 static inline int pcie_dev_speed_mbps(enum pci_bus_speed speed)
 {
 	switch (speed) {
diff --git a/drivers/pci/pcie/bwctrl.c b/drivers/pci/pcie/bwctrl.c
index c4c8d260bf96..ea82e326f164 100644
--- a/drivers/pci/pcie/bwctrl.c
+++ b/drivers/pci/pcie/bwctrl.c
@@ -48,11 +48,6 @@ struct pcie_bwctrl_data {
 /* Prevent port removal during Link Speed changes. */
 static DECLARE_RWSEM(pcie_bwctrl_setspeed_rwsem);
 
-static bool pcie_valid_speed(enum pci_bus_speed speed)
-{
-	return (speed >= PCIE_SPEED_2_5GT) && (speed <= PCIE_SPEED_64_0GT);
-}
-
 static u16 pci_bus_speed2lnkctl2(enum pci_bus_speed speed)
 {
 	static const u8 speed_conv[] = {
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v5 2/3] PCI: Move pci_bus_speed2lnkctl2() to public header
  2026-04-06 10:47 [PATCH v5 0/3] PCI: Refactor PCIe speed validation and conversion functions Hans Zhang
  2026-04-06 10:47 ` [PATCH v5 1/3] PCI: Add public pcie_valid_speed() for shared validation Hans Zhang
@ 2026-04-06 10:47 ` Hans Zhang
  2026-04-07  8:11   ` Ilpo Järvinen
  2026-04-06 10:47 ` [PATCH v5 3/3] PCI: dwc: Use common speed conversion function Hans Zhang
  2 siblings, 1 reply; 12+ messages in thread
From: Hans Zhang @ 2026-04-06 10:47 UTC (permalink / raw)
  To: bhelgaas, lpieralisi, kw, kwilczynski, mani, ilpo.jarvinen,
	jingoohan1
  Cc: robh, linux-pci, linux-kernel, Hans Zhang

Move the static array-based pci_bus_speed2lnkctl2() function from
bwctrl.c to pci.h as a public inline function.

This provides efficient O(1) speed-to-LNKCTL2 value conversion using
static array lookup, maintaining optimal performance while enabling
code reuse by other PCIe drivers.

Signed-off-by: Hans Zhang <18255117159@163.com>
---
 drivers/pci/pci.h         | 17 +++++++++++++++++
 drivers/pci/pcie/bwctrl.c | 17 -----------------
 2 files changed, 17 insertions(+), 17 deletions(-)

diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index f0a082bfd6f1..db91878a86ac 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -611,6 +611,23 @@ static inline bool pcie_valid_speed(enum pci_bus_speed speed)
 	return (speed >= PCIE_SPEED_2_5GT) && (speed <= PCIE_SPEED_64_0GT);
 }
 
+static inline u16 pci_bus_speed2lnkctl2(enum pci_bus_speed speed)
+{
+	static const u8 speed_conv[] = {
+		[PCIE_SPEED_2_5GT] = PCI_EXP_LNKCTL2_TLS_2_5GT,
+		[PCIE_SPEED_5_0GT] = PCI_EXP_LNKCTL2_TLS_5_0GT,
+		[PCIE_SPEED_8_0GT] = PCI_EXP_LNKCTL2_TLS_8_0GT,
+		[PCIE_SPEED_16_0GT] = PCI_EXP_LNKCTL2_TLS_16_0GT,
+		[PCIE_SPEED_32_0GT] = PCI_EXP_LNKCTL2_TLS_32_0GT,
+		[PCIE_SPEED_64_0GT] = PCI_EXP_LNKCTL2_TLS_64_0GT,
+	};
+
+	if (WARN_ON_ONCE(!pcie_valid_speed(speed)))
+		return 0;
+
+	return speed_conv[speed];
+}
+
 static inline int pcie_dev_speed_mbps(enum pci_bus_speed speed)
 {
 	switch (speed) {
diff --git a/drivers/pci/pcie/bwctrl.c b/drivers/pci/pcie/bwctrl.c
index ea82e326f164..d48021bfd844 100644
--- a/drivers/pci/pcie/bwctrl.c
+++ b/drivers/pci/pcie/bwctrl.c
@@ -48,23 +48,6 @@ struct pcie_bwctrl_data {
 /* Prevent port removal during Link Speed changes. */
 static DECLARE_RWSEM(pcie_bwctrl_setspeed_rwsem);
 
-static u16 pci_bus_speed2lnkctl2(enum pci_bus_speed speed)
-{
-	static const u8 speed_conv[] = {
-		[PCIE_SPEED_2_5GT] = PCI_EXP_LNKCTL2_TLS_2_5GT,
-		[PCIE_SPEED_5_0GT] = PCI_EXP_LNKCTL2_TLS_5_0GT,
-		[PCIE_SPEED_8_0GT] = PCI_EXP_LNKCTL2_TLS_8_0GT,
-		[PCIE_SPEED_16_0GT] = PCI_EXP_LNKCTL2_TLS_16_0GT,
-		[PCIE_SPEED_32_0GT] = PCI_EXP_LNKCTL2_TLS_32_0GT,
-		[PCIE_SPEED_64_0GT] = PCI_EXP_LNKCTL2_TLS_64_0GT,
-	};
-
-	if (WARN_ON_ONCE(!pcie_valid_speed(speed)))
-		return 0;
-
-	return speed_conv[speed];
-}
-
 static inline u16 pcie_supported_speeds2target_speed(u8 supported_speeds)
 {
 	return __fls(supported_speeds);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v5 3/3] PCI: dwc: Use common speed conversion function
  2026-04-06 10:47 [PATCH v5 0/3] PCI: Refactor PCIe speed validation and conversion functions Hans Zhang
  2026-04-06 10:47 ` [PATCH v5 1/3] PCI: Add public pcie_valid_speed() for shared validation Hans Zhang
  2026-04-06 10:47 ` [PATCH v5 2/3] PCI: Move pci_bus_speed2lnkctl2() to public header Hans Zhang
@ 2026-04-06 10:47 ` Hans Zhang
  2026-04-06 10:54   ` Hans Zhang
  2 siblings, 1 reply; 12+ messages in thread
From: Hans Zhang @ 2026-04-06 10:47 UTC (permalink / raw)
  To: bhelgaas, lpieralisi, kw, kwilczynski, mani, ilpo.jarvinen,
	jingoohan1
  Cc: robh, linux-pci, linux-kernel, Hans Zhang

Replace the private switch-based speed conversion in
dw_pcie_link_set_max_speed() with the public pci_bus_speed2lnkctl2()
function.

This eliminates duplicate conversion logic and ensures consistency with
other PCIe drivers, while handling invalid speeds by falling back to
hardware capabilities.

Signed-off-by: Hans Zhang <18255117159@163.com>
Acked-by: Manivannan Sadhasivam <mani@kernel.org>
---
 drivers/pci/controller/dwc/pcie-designware.c | 17 ++---------------
 1 file changed, 2 insertions(+), 15 deletions(-)

diff --git a/drivers/pci/controller/dwc/pcie-designware.c b/drivers/pci/controller/dwc/pcie-designware.c
index 06792ba92aa7..f2dd5da69c76 100644
--- a/drivers/pci/controller/dwc/pcie-designware.c
+++ b/drivers/pci/controller/dwc/pcie-designware.c
@@ -861,24 +861,11 @@ static void dw_pcie_link_set_max_speed(struct dw_pcie *pci)
 	ctrl2 = dw_pcie_readl_dbi(pci, offset + PCI_EXP_LNKCTL2);
 	ctrl2 &= ~PCI_EXP_LNKCTL2_TLS;
 
-	switch (pcie_get_link_speed(pci->max_link_speed)) {
-	case PCIE_SPEED_2_5GT:
-		link_speed = PCI_EXP_LNKCTL2_TLS_2_5GT;
-		break;
-	case PCIE_SPEED_5_0GT:
-		link_speed = PCI_EXP_LNKCTL2_TLS_5_0GT;
-		break;
-	case PCIE_SPEED_8_0GT:
-		link_speed = PCI_EXP_LNKCTL2_TLS_8_0GT;
-		break;
-	case PCIE_SPEED_16_0GT:
-		link_speed = PCI_EXP_LNKCTL2_TLS_16_0GT;
-		break;
-	default:
+	link_speed = pci_bus_speed2lnkctl2(link_speed);
+	if (link_speed == 0) {
 		/* Use hardware capability */
 		link_speed = FIELD_GET(PCI_EXP_LNKCAP_SLS, cap);
 		ctrl2 &= ~PCI_EXP_LNKCTL2_HASD;
-		break;
 	}
 
 	dw_pcie_writel_dbi(pci, offset + PCI_EXP_LNKCTL2, ctrl2 | link_speed);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH v5 3/3] PCI: dwc: Use common speed conversion function
  2026-04-06 10:47 ` [PATCH v5 3/3] PCI: dwc: Use common speed conversion function Hans Zhang
@ 2026-04-06 10:54   ` Hans Zhang
  0 siblings, 0 replies; 12+ messages in thread
From: Hans Zhang @ 2026-04-06 10:54 UTC (permalink / raw)
  To: bhelgaas, lpieralisi, kw, kwilczynski, mani, ilpo.jarvinen,
	jingoohan1
  Cc: robh, linux-pci, linux-kernel

Hi Mani,

This patch is missing one line of code:
link_speed = pcie_get_link_speed(pci->max_link_speed);


I'll send out v6. Sorry for the noise.

Best regards,
Hans

On 4/6/26 18:47, Hans Zhang wrote:
> Replace the private switch-based speed conversion in
> dw_pcie_link_set_max_speed() with the public pci_bus_speed2lnkctl2()
> function.
> 
> This eliminates duplicate conversion logic and ensures consistency with
> other PCIe drivers, while handling invalid speeds by falling back to
> hardware capabilities.
> 
> Signed-off-by: Hans Zhang <18255117159@163.com>
> Acked-by: Manivannan Sadhasivam <mani@kernel.org>
> ---
>   drivers/pci/controller/dwc/pcie-designware.c | 17 ++---------------
>   1 file changed, 2 insertions(+), 15 deletions(-)
> 
> diff --git a/drivers/pci/controller/dwc/pcie-designware.c b/drivers/pci/controller/dwc/pcie-designware.c
> index 06792ba92aa7..f2dd5da69c76 100644
> --- a/drivers/pci/controller/dwc/pcie-designware.c
> +++ b/drivers/pci/controller/dwc/pcie-designware.c
> @@ -861,24 +861,11 @@ static void dw_pcie_link_set_max_speed(struct dw_pcie *pci)
>   	ctrl2 = dw_pcie_readl_dbi(pci, offset + PCI_EXP_LNKCTL2);
>   	ctrl2 &= ~PCI_EXP_LNKCTL2_TLS;
>   
> -	switch (pcie_get_link_speed(pci->max_link_speed)) {
> -	case PCIE_SPEED_2_5GT:
> -		link_speed = PCI_EXP_LNKCTL2_TLS_2_5GT;
> -		break;
> -	case PCIE_SPEED_5_0GT:
> -		link_speed = PCI_EXP_LNKCTL2_TLS_5_0GT;
> -		break;
> -	case PCIE_SPEED_8_0GT:
> -		link_speed = PCI_EXP_LNKCTL2_TLS_8_0GT;
> -		break;
> -	case PCIE_SPEED_16_0GT:
> -		link_speed = PCI_EXP_LNKCTL2_TLS_16_0GT;
> -		break;
> -	default:
> +	link_speed = pci_bus_speed2lnkctl2(link_speed);
> +	if (link_speed == 0) {
>   		/* Use hardware capability */
>   		link_speed = FIELD_GET(PCI_EXP_LNKCAP_SLS, cap);
>   		ctrl2 &= ~PCI_EXP_LNKCTL2_HASD;
> -		break;
>   	}
>   
>   	dw_pcie_writel_dbi(pci, offset + PCI_EXP_LNKCTL2, ctrl2 | link_speed);


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v5 2/3] PCI: Move pci_bus_speed2lnkctl2() to public header
  2026-04-06 10:47 ` [PATCH v5 2/3] PCI: Move pci_bus_speed2lnkctl2() to public header Hans Zhang
@ 2026-04-07  8:11   ` Ilpo Järvinen
  2026-04-07 12:09     ` Hans Zhang
  0 siblings, 1 reply; 12+ messages in thread
From: Ilpo Järvinen @ 2026-04-07  8:11 UTC (permalink / raw)
  To: Hans Zhang
  Cc: bhelgaas, lpieralisi, kw, kwilczynski, mani, jingoohan1, robh,
	linux-pci, LKML

On Mon, 6 Apr 2026, Hans Zhang wrote:

> Move the static array-based pci_bus_speed2lnkctl2() function from
> bwctrl.c to pci.h as a public inline function.
> 
> This provides efficient O(1) speed-to-LNKCTL2 value conversion using
> static array lookup, maintaining optimal performance while enabling
> code reuse by other PCIe drivers.
> 
> Signed-off-by: Hans Zhang <18255117159@163.com>
> ---
>  drivers/pci/pci.h         | 17 +++++++++++++++++
>  drivers/pci/pcie/bwctrl.c | 17 -----------------
>  2 files changed, 17 insertions(+), 17 deletions(-)
> 
> diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
> index f0a082bfd6f1..db91878a86ac 100644
> --- a/drivers/pci/pci.h
> +++ b/drivers/pci/pci.h
> @@ -611,6 +611,23 @@ static inline bool pcie_valid_speed(enum pci_bus_speed speed)
>  	return (speed >= PCIE_SPEED_2_5GT) && (speed <= PCIE_SPEED_64_0GT);
>  }
>  
> +static inline u16 pci_bus_speed2lnkctl2(enum pci_bus_speed speed)
> +{
> +	static const u8 speed_conv[] = {
> +		[PCIE_SPEED_2_5GT] = PCI_EXP_LNKCTL2_TLS_2_5GT,
> +		[PCIE_SPEED_5_0GT] = PCI_EXP_LNKCTL2_TLS_5_0GT,
> +		[PCIE_SPEED_8_0GT] = PCI_EXP_LNKCTL2_TLS_8_0GT,
> +		[PCIE_SPEED_16_0GT] = PCI_EXP_LNKCTL2_TLS_16_0GT,
> +		[PCIE_SPEED_32_0GT] = PCI_EXP_LNKCTL2_TLS_32_0GT,
> +		[PCIE_SPEED_64_0GT] = PCI_EXP_LNKCTL2_TLS_64_0GT,
> +	};
> +
> +	if (WARN_ON_ONCE(!pcie_valid_speed(speed)))

drivers/pci/pci.h doesn't seem to have include for WARN_ON_ONCE() so you 
should add it.

-- 
 i.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v5 2/3] PCI: Move pci_bus_speed2lnkctl2() to public header
  2026-04-07  8:11   ` Ilpo Järvinen
@ 2026-04-07 12:09     ` Hans Zhang
  2026-04-07 12:26       ` Ilpo Järvinen
  0 siblings, 1 reply; 12+ messages in thread
From: Hans Zhang @ 2026-04-07 12:09 UTC (permalink / raw)
  To: Ilpo Järvinen
  Cc: bhelgaas, lpieralisi, kw, kwilczynski, mani, jingoohan1, robh,
	linux-pci, LKML



On 4/7/26 16:11, Ilpo Järvinen wrote:
> On Mon, 6 Apr 2026, Hans Zhang wrote:
> 
>> Move the static array-based pci_bus_speed2lnkctl2() function from
>> bwctrl.c to pci.h as a public inline function.
>>
>> This provides efficient O(1) speed-to-LNKCTL2 value conversion using
>> static array lookup, maintaining optimal performance while enabling
>> code reuse by other PCIe drivers.
>>
>> Signed-off-by: Hans Zhang <18255117159@163.com>
>> ---
>>   drivers/pci/pci.h         | 17 +++++++++++++++++
>>   drivers/pci/pcie/bwctrl.c | 17 -----------------
>>   2 files changed, 17 insertions(+), 17 deletions(-)
>>
>> diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
>> index f0a082bfd6f1..db91878a86ac 100644
>> --- a/drivers/pci/pci.h
>> +++ b/drivers/pci/pci.h
>> @@ -611,6 +611,23 @@ static inline bool pcie_valid_speed(enum pci_bus_speed speed)
>>   	return (speed >= PCIE_SPEED_2_5GT) && (speed <= PCIE_SPEED_64_0GT);
>>   }
>>   
>> +static inline u16 pci_bus_speed2lnkctl2(enum pci_bus_speed speed)
>> +{
>> +	static const u8 speed_conv[] = {
>> +		[PCIE_SPEED_2_5GT] = PCI_EXP_LNKCTL2_TLS_2_5GT,
>> +		[PCIE_SPEED_5_0GT] = PCI_EXP_LNKCTL2_TLS_5_0GT,
>> +		[PCIE_SPEED_8_0GT] = PCI_EXP_LNKCTL2_TLS_8_0GT,
>> +		[PCIE_SPEED_16_0GT] = PCI_EXP_LNKCTL2_TLS_16_0GT,
>> +		[PCIE_SPEED_32_0GT] = PCI_EXP_LNKCTL2_TLS_32_0GT,
>> +		[PCIE_SPEED_64_0GT] = PCI_EXP_LNKCTL2_TLS_64_0GT,
>> +	};
>> +
>> +	if (WARN_ON_ONCE(!pcie_valid_speed(speed)))
> 
> drivers/pci/pci.h doesn't seem to have include for WARN_ON_ONCE() so you
> should add it.

Hi Ilpo,

In the file "drivers/pci/pcie/bwctrl.c", there is no reference to the 
header file of WARN_ON_ONCE(). It seems that no error was reported. I 
think it might be that some other header files have indirectly 
referenced the header file of WARN_ON_ONCE(). However, when I compiled 
it locally, no errors were reported.

I think if we want to add a header file, it should be #include 
<asm-generic/bug.h> ?

Best regards,
Hans

> 


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v5 2/3] PCI: Move pci_bus_speed2lnkctl2() to public header
  2026-04-07 12:09     ` Hans Zhang
@ 2026-04-07 12:26       ` Ilpo Järvinen
  2026-04-07 12:29         ` Hans Zhang
  0 siblings, 1 reply; 12+ messages in thread
From: Ilpo Järvinen @ 2026-04-07 12:26 UTC (permalink / raw)
  To: Hans Zhang
  Cc: bhelgaas, lpieralisi, kw, kwilczynski, mani, jingoohan1, robh,
	linux-pci, LKML

[-- Attachment #1: Type: text/plain, Size: 2542 bytes --]

On Tue, 7 Apr 2026, Hans Zhang wrote:

> 
> 
> On 4/7/26 16:11, Ilpo Järvinen wrote:
> > On Mon, 6 Apr 2026, Hans Zhang wrote:
> > 
> > > Move the static array-based pci_bus_speed2lnkctl2() function from
> > > bwctrl.c to pci.h as a public inline function.
> > > 
> > > This provides efficient O(1) speed-to-LNKCTL2 value conversion using
> > > static array lookup, maintaining optimal performance while enabling
> > > code reuse by other PCIe drivers.
> > > 
> > > Signed-off-by: Hans Zhang <18255117159@163.com>
> > > ---
> > >   drivers/pci/pci.h         | 17 +++++++++++++++++
> > >   drivers/pci/pcie/bwctrl.c | 17 -----------------
> > >   2 files changed, 17 insertions(+), 17 deletions(-)
> > > 
> > > diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
> > > index f0a082bfd6f1..db91878a86ac 100644
> > > --- a/drivers/pci/pci.h
> > > +++ b/drivers/pci/pci.h
> > > @@ -611,6 +611,23 @@ static inline bool pcie_valid_speed(enum
> > > pci_bus_speed speed)
> > >   	return (speed >= PCIE_SPEED_2_5GT) && (speed <= PCIE_SPEED_64_0GT);
> > >   }
> > >   +static inline u16 pci_bus_speed2lnkctl2(enum pci_bus_speed speed)
> > > +{
> > > +	static const u8 speed_conv[] = {
> > > +		[PCIE_SPEED_2_5GT] = PCI_EXP_LNKCTL2_TLS_2_5GT,
> > > +		[PCIE_SPEED_5_0GT] = PCI_EXP_LNKCTL2_TLS_5_0GT,
> > > +		[PCIE_SPEED_8_0GT] = PCI_EXP_LNKCTL2_TLS_8_0GT,
> > > +		[PCIE_SPEED_16_0GT] = PCI_EXP_LNKCTL2_TLS_16_0GT,
> > > +		[PCIE_SPEED_32_0GT] = PCI_EXP_LNKCTL2_TLS_32_0GT,
> > > +		[PCIE_SPEED_64_0GT] = PCI_EXP_LNKCTL2_TLS_64_0GT,
> > > +	};
> > > +
> > > +	if (WARN_ON_ONCE(!pcie_valid_speed(speed)))
> > 
> > drivers/pci/pci.h doesn't seem to have include for WARN_ON_ONCE() so you
> > should add it.
> 
> Hi Ilpo,
> 
> In the file "drivers/pci/pcie/bwctrl.c", there is no reference to the header
> file of WARN_ON_ONCE(). It seems that no error was reported. I think it might
> be that some other header files have indirectly referenced the header file of
> WARN_ON_ONCE(). However, when I compiled it locally, no errors were reported.

Hi,

Apparently it was missing from there as well.

It might build now (I don't actually even doubt that), but that depends 
on the includes in every file including drivers/pci/pci.h. We should not 
depend on that as it is fragile but ensure a header is self-sufficient 
when it comes to includes necessary for it.

> I think if we want to add a header file, it should be #include
> <asm-generic/bug.h> ?

Yes

#include <asm/bug.h>

-- 
 i.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v5 2/3] PCI: Move pci_bus_speed2lnkctl2() to public header
  2026-04-07 12:26       ` Ilpo Järvinen
@ 2026-04-07 12:29         ` Hans Zhang
  2026-04-07 12:48           ` Hans Zhang
  0 siblings, 1 reply; 12+ messages in thread
From: Hans Zhang @ 2026-04-07 12:29 UTC (permalink / raw)
  To: Ilpo Järvinen
  Cc: bhelgaas, lpieralisi, kw, kwilczynski, mani, jingoohan1, robh,
	linux-pci, LKML



On 4/7/26 20:26, Ilpo Järvinen wrote:
> On Tue, 7 Apr 2026, Hans Zhang wrote:
> 
>>
>>
>> On 4/7/26 16:11, Ilpo Järvinen wrote:
>>> On Mon, 6 Apr 2026, Hans Zhang wrote:
>>>
>>>> Move the static array-based pci_bus_speed2lnkctl2() function from
>>>> bwctrl.c to pci.h as a public inline function.
>>>>
>>>> This provides efficient O(1) speed-to-LNKCTL2 value conversion using
>>>> static array lookup, maintaining optimal performance while enabling
>>>> code reuse by other PCIe drivers.
>>>>
>>>> Signed-off-by: Hans Zhang <18255117159@163.com>
>>>> ---
>>>>    drivers/pci/pci.h         | 17 +++++++++++++++++
>>>>    drivers/pci/pcie/bwctrl.c | 17 -----------------
>>>>    2 files changed, 17 insertions(+), 17 deletions(-)
>>>>
>>>> diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
>>>> index f0a082bfd6f1..db91878a86ac 100644
>>>> --- a/drivers/pci/pci.h
>>>> +++ b/drivers/pci/pci.h
>>>> @@ -611,6 +611,23 @@ static inline bool pcie_valid_speed(enum
>>>> pci_bus_speed speed)
>>>>    	return (speed >= PCIE_SPEED_2_5GT) && (speed <= PCIE_SPEED_64_0GT);
>>>>    }
>>>>    +static inline u16 pci_bus_speed2lnkctl2(enum pci_bus_speed speed)
>>>> +{
>>>> +	static const u8 speed_conv[] = {
>>>> +		[PCIE_SPEED_2_5GT] = PCI_EXP_LNKCTL2_TLS_2_5GT,
>>>> +		[PCIE_SPEED_5_0GT] = PCI_EXP_LNKCTL2_TLS_5_0GT,
>>>> +		[PCIE_SPEED_8_0GT] = PCI_EXP_LNKCTL2_TLS_8_0GT,
>>>> +		[PCIE_SPEED_16_0GT] = PCI_EXP_LNKCTL2_TLS_16_0GT,
>>>> +		[PCIE_SPEED_32_0GT] = PCI_EXP_LNKCTL2_TLS_32_0GT,
>>>> +		[PCIE_SPEED_64_0GT] = PCI_EXP_LNKCTL2_TLS_64_0GT,
>>>> +	};
>>>> +
>>>> +	if (WARN_ON_ONCE(!pcie_valid_speed(speed)))
>>>
>>> drivers/pci/pci.h doesn't seem to have include for WARN_ON_ONCE() so you
>>> should add it.
>>
>> Hi Ilpo,
>>
>> In the file "drivers/pci/pcie/bwctrl.c", there is no reference to the header
>> file of WARN_ON_ONCE(). It seems that no error was reported. I think it might
>> be that some other header files have indirectly referenced the header file of
>> WARN_ON_ONCE(). However, when I compiled it locally, no errors were reported.
> 
> Hi,
> 
> Apparently it was missing from there as well.
> 
> It might build now (I don't actually even doubt that), but that depends
> on the includes in every file including drivers/pci/pci.h. We should not
> depend on that as it is fragile but ensure a header is self-sufficient
> when it comes to includes necessary for it.

Hi Ilpo,

OK, I see.


> 
>> I think if we want to add a header file, it should be #include
>> <asm-generic/bug.h> ?
> 
> Yes
> 
> #include <asm/bug.h>

Thank you. Will add.

Best regards,
Hans
> 


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v5 2/3] PCI: Move pci_bus_speed2lnkctl2() to public header
  2026-04-07 12:29         ` Hans Zhang
@ 2026-04-07 12:48           ` Hans Zhang
  2026-04-07 12:53             ` Ilpo Järvinen
  0 siblings, 1 reply; 12+ messages in thread
From: Hans Zhang @ 2026-04-07 12:48 UTC (permalink / raw)
  To: Ilpo Järvinen
  Cc: bhelgaas, lpieralisi, kw, kwilczynski, mani, jingoohan1, robh,
	linux-pci, LKML



On 4/7/26 20:29, Hans Zhang wrote:
> 
> 
> On 4/7/26 20:26, Ilpo Järvinen wrote:
>> On Tue, 7 Apr 2026, Hans Zhang wrote:
>>
>>>
>>>
>>> On 4/7/26 16:11, Ilpo Järvinen wrote:
>>>> On Mon, 6 Apr 2026, Hans Zhang wrote:
>>>>
>>>>> Move the static array-based pci_bus_speed2lnkctl2() function from
>>>>> bwctrl.c to pci.h as a public inline function.
>>>>>
>>>>> This provides efficient O(1) speed-to-LNKCTL2 value conversion using
>>>>> static array lookup, maintaining optimal performance while enabling
>>>>> code reuse by other PCIe drivers.
>>>>>
>>>>> Signed-off-by: Hans Zhang <18255117159@163.com>
>>>>> ---
>>>>>    drivers/pci/pci.h         | 17 +++++++++++++++++
>>>>>    drivers/pci/pcie/bwctrl.c | 17 -----------------
>>>>>    2 files changed, 17 insertions(+), 17 deletions(-)
>>>>>
>>>>> diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
>>>>> index f0a082bfd6f1..db91878a86ac 100644
>>>>> --- a/drivers/pci/pci.h
>>>>> +++ b/drivers/pci/pci.h
>>>>> @@ -611,6 +611,23 @@ static inline bool pcie_valid_speed(enum
>>>>> pci_bus_speed speed)
>>>>>        return (speed >= PCIE_SPEED_2_5GT) && (speed <= 
>>>>> PCIE_SPEED_64_0GT);
>>>>>    }
>>>>>    +static inline u16 pci_bus_speed2lnkctl2(enum pci_bus_speed speed)
>>>>> +{
>>>>> +    static const u8 speed_conv[] = {
>>>>> +        [PCIE_SPEED_2_5GT] = PCI_EXP_LNKCTL2_TLS_2_5GT,
>>>>> +        [PCIE_SPEED_5_0GT] = PCI_EXP_LNKCTL2_TLS_5_0GT,
>>>>> +        [PCIE_SPEED_8_0GT] = PCI_EXP_LNKCTL2_TLS_8_0GT,
>>>>> +        [PCIE_SPEED_16_0GT] = PCI_EXP_LNKCTL2_TLS_16_0GT,
>>>>> +        [PCIE_SPEED_32_0GT] = PCI_EXP_LNKCTL2_TLS_32_0GT,
>>>>> +        [PCIE_SPEED_64_0GT] = PCI_EXP_LNKCTL2_TLS_64_0GT,
>>>>> +    };
>>>>> +
>>>>> +    if (WARN_ON_ONCE(!pcie_valid_speed(speed)))
>>>>
>>>> drivers/pci/pci.h doesn't seem to have include for WARN_ON_ONCE() so 
>>>> you
>>>> should add it.
>>>
>>> Hi Ilpo,
>>>
>>> In the file "drivers/pci/pcie/bwctrl.c", there is no reference to the 
>>> header
>>> file of WARN_ON_ONCE(). It seems that no error was reported. I think 
>>> it might
>>> be that some other header files have indirectly referenced the header 
>>> file of
>>> WARN_ON_ONCE(). However, when I compiled it locally, no errors were 
>>> reported.
>>
>> Hi,
>>
>> Apparently it was missing from there as well.
>>
>> It might build now (I don't actually even doubt that), but that depends
>> on the includes in every file including drivers/pci/pci.h. We should not
>> depend on that as it is fragile but ensure a header is self-sufficient
>> when it comes to includes necessary for it.
> 
> Hi Ilpo,
> 
> OK, I see.
> 
> 
>>
>>> I think if we want to add a header file, it should be #include
>>> <asm-generic/bug.h> ?
>>
>> Yes
>>
>> #include <asm/bug.h>

Hi Ilpo,

Executing the following check script will trigger a warning. However, 
using #include <asm-generic/bug.h> will not cause any issues. So, should 
we use #include <asm-generic/bug.h> ?

./scripts/checkpatch.pl ...../*patch

0002-PCI-Move-pci_bus_speed2lnkctl2-to-public-header.patch
---------------------------------------------------------------------
WARNING: Use #include <linux/bug.h> instead of <asm/bug.h>
#27: FILE: drivers/pci/pci.h:5:
+#include <asm/bug.h>

total: 0 errors, 1 warnings, 53 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
       mechanically convert to the typical style using --fix or 
--fix-inplace.

Best regards,
Hans



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v5 2/3] PCI: Move pci_bus_speed2lnkctl2() to public header
  2026-04-07 12:48           ` Hans Zhang
@ 2026-04-07 12:53             ` Ilpo Järvinen
  2026-04-07 13:01               ` Hans Zhang
  0 siblings, 1 reply; 12+ messages in thread
From: Ilpo Järvinen @ 2026-04-07 12:53 UTC (permalink / raw)
  To: Hans Zhang
  Cc: bhelgaas, lpieralisi, kw, kwilczynski, mani, jingoohan1, robh,
	linux-pci, LKML

[-- Attachment #1: Type: text/plain, Size: 4136 bytes --]

On Tue, 7 Apr 2026, Hans Zhang wrote:
> On 4/7/26 20:29, Hans Zhang wrote:
> > On 4/7/26 20:26, Ilpo Järvinen wrote:
> > > On Tue, 7 Apr 2026, Hans Zhang wrote:
> > > > On 4/7/26 16:11, Ilpo Järvinen wrote:
> > > > > On Mon, 6 Apr 2026, Hans Zhang wrote:
> > > > > 
> > > > > > Move the static array-based pci_bus_speed2lnkctl2() function from
> > > > > > bwctrl.c to pci.h as a public inline function.
> > > > > > 
> > > > > > This provides efficient O(1) speed-to-LNKCTL2 value conversion using
> > > > > > static array lookup, maintaining optimal performance while enabling
> > > > > > code reuse by other PCIe drivers.
> > > > > > 
> > > > > > Signed-off-by: Hans Zhang <18255117159@163.com>
> > > > > > ---
> > > > > >    drivers/pci/pci.h         | 17 +++++++++++++++++
> > > > > >    drivers/pci/pcie/bwctrl.c | 17 -----------------
> > > > > >    2 files changed, 17 insertions(+), 17 deletions(-)
> > > > > > 
> > > > > > diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
> > > > > > index f0a082bfd6f1..db91878a86ac 100644
> > > > > > --- a/drivers/pci/pci.h
> > > > > > +++ b/drivers/pci/pci.h
> > > > > > @@ -611,6 +611,23 @@ static inline bool pcie_valid_speed(enum
> > > > > > pci_bus_speed speed)
> > > > > >        return (speed >= PCIE_SPEED_2_5GT) && (speed <=
> > > > > > PCIE_SPEED_64_0GT);
> > > > > >    }
> > > > > >    +static inline u16 pci_bus_speed2lnkctl2(enum pci_bus_speed
> > > > > > speed)
> > > > > > +{
> > > > > > +    static const u8 speed_conv[] = {
> > > > > > +        [PCIE_SPEED_2_5GT] = PCI_EXP_LNKCTL2_TLS_2_5GT,
> > > > > > +        [PCIE_SPEED_5_0GT] = PCI_EXP_LNKCTL2_TLS_5_0GT,
> > > > > > +        [PCIE_SPEED_8_0GT] = PCI_EXP_LNKCTL2_TLS_8_0GT,
> > > > > > +        [PCIE_SPEED_16_0GT] = PCI_EXP_LNKCTL2_TLS_16_0GT,
> > > > > > +        [PCIE_SPEED_32_0GT] = PCI_EXP_LNKCTL2_TLS_32_0GT,
> > > > > > +        [PCIE_SPEED_64_0GT] = PCI_EXP_LNKCTL2_TLS_64_0GT,
> > > > > > +    };
> > > > > > +
> > > > > > +    if (WARN_ON_ONCE(!pcie_valid_speed(speed)))
> > > > > 
> > > > > drivers/pci/pci.h doesn't seem to have include for WARN_ON_ONCE() so
> > > > > you
> > > > > should add it.
> > > > 
> > > > Hi Ilpo,
> > > > 
> > > > In the file "drivers/pci/pcie/bwctrl.c", there is no reference to the
> > > > header
> > > > file of WARN_ON_ONCE(). It seems that no error was reported. I think it
> > > > might
> > > > be that some other header files have indirectly referenced the header
> > > > file of
> > > > WARN_ON_ONCE(). However, when I compiled it locally, no errors were
> > > > reported.
> > > 
> > > Hi,
> > > 
> > > Apparently it was missing from there as well.
> > > 
> > > It might build now (I don't actually even doubt that), but that depends
> > > on the includes in every file including drivers/pci/pci.h. We should not
> > > depend on that as it is fragile but ensure a header is self-sufficient
> > > when it comes to includes necessary for it.
> > 
> > Hi Ilpo,
> > 
> > OK, I see.
> > 
> > 
> > > 
> > > > I think if we want to add a header file, it should be #include
> > > > <asm-generic/bug.h> ?
> > > 
> > > Yes
> > > 
> > > #include <asm/bug.h>
> 
> Hi Ilpo,
> 
> Executing the following check script will trigger a warning. However, using
> #include <asm-generic/bug.h> will not cause any issues. So, should we use
> #include <asm-generic/bug.h> ?
> 
> ./scripts/checkpatch.pl ...../*patch
> 
> 0002-PCI-Move-pci_bus_speed2lnkctl2-to-public-header.patch
> ---------------------------------------------------------------------
> WARNING: Use #include <linux/bug.h> instead of <asm/bug.h>
> #27: FILE: drivers/pci/pci.h:5:
> +#include <asm/bug.h>
> 
> total: 0 errors, 1 warnings, 53 lines checked
> 
> NOTE: For some of the reported defects, checkpatch may be able to
>       mechanically convert to the typical style using --fix or --fix-inplace.

This is a header, so using <asm/bug.h> minimizes what it pulls in so it 
is justified to use asm/ over linux/ in this case.


-- 
 i.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v5 2/3] PCI: Move pci_bus_speed2lnkctl2() to public header
  2026-04-07 12:53             ` Ilpo Järvinen
@ 2026-04-07 13:01               ` Hans Zhang
  0 siblings, 0 replies; 12+ messages in thread
From: Hans Zhang @ 2026-04-07 13:01 UTC (permalink / raw)
  To: Ilpo Järvinen
  Cc: bhelgaas, lpieralisi, kw, kwilczynski, mani, jingoohan1, robh,
	linux-pci, LKML



On 4/7/26 20:53, Ilpo Järvinen wrote:
> On Tue, 7 Apr 2026, Hans Zhang wrote:
>> On 4/7/26 20:29, Hans Zhang wrote:
>>> On 4/7/26 20:26, Ilpo Järvinen wrote:
>>>> On Tue, 7 Apr 2026, Hans Zhang wrote:
>>>>> On 4/7/26 16:11, Ilpo Järvinen wrote:
>>>>>> On Mon, 6 Apr 2026, Hans Zhang wrote:
>>>>>>
>>>>>>> Move the static array-based pci_bus_speed2lnkctl2() function from
>>>>>>> bwctrl.c to pci.h as a public inline function.
>>>>>>>
>>>>>>> This provides efficient O(1) speed-to-LNKCTL2 value conversion using
>>>>>>> static array lookup, maintaining optimal performance while enabling
>>>>>>> code reuse by other PCIe drivers.
>>>>>>>
>>>>>>> Signed-off-by: Hans Zhang <18255117159@163.com>
>>>>>>> ---
>>>>>>>     drivers/pci/pci.h         | 17 +++++++++++++++++
>>>>>>>     drivers/pci/pcie/bwctrl.c | 17 -----------------
>>>>>>>     2 files changed, 17 insertions(+), 17 deletions(-)
>>>>>>>
>>>>>>> diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
>>>>>>> index f0a082bfd6f1..db91878a86ac 100644
>>>>>>> --- a/drivers/pci/pci.h
>>>>>>> +++ b/drivers/pci/pci.h
>>>>>>> @@ -611,6 +611,23 @@ static inline bool pcie_valid_speed(enum
>>>>>>> pci_bus_speed speed)
>>>>>>>         return (speed >= PCIE_SPEED_2_5GT) && (speed <=
>>>>>>> PCIE_SPEED_64_0GT);
>>>>>>>     }
>>>>>>>     +static inline u16 pci_bus_speed2lnkctl2(enum pci_bus_speed
>>>>>>> speed)
>>>>>>> +{
>>>>>>> +    static const u8 speed_conv[] = {
>>>>>>> +        [PCIE_SPEED_2_5GT] = PCI_EXP_LNKCTL2_TLS_2_5GT,
>>>>>>> +        [PCIE_SPEED_5_0GT] = PCI_EXP_LNKCTL2_TLS_5_0GT,
>>>>>>> +        [PCIE_SPEED_8_0GT] = PCI_EXP_LNKCTL2_TLS_8_0GT,
>>>>>>> +        [PCIE_SPEED_16_0GT] = PCI_EXP_LNKCTL2_TLS_16_0GT,
>>>>>>> +        [PCIE_SPEED_32_0GT] = PCI_EXP_LNKCTL2_TLS_32_0GT,
>>>>>>> +        [PCIE_SPEED_64_0GT] = PCI_EXP_LNKCTL2_TLS_64_0GT,
>>>>>>> +    };
>>>>>>> +
>>>>>>> +    if (WARN_ON_ONCE(!pcie_valid_speed(speed)))
>>>>>>
>>>>>> drivers/pci/pci.h doesn't seem to have include for WARN_ON_ONCE() so
>>>>>> you
>>>>>> should add it.
>>>>>
>>>>> Hi Ilpo,
>>>>>
>>>>> In the file "drivers/pci/pcie/bwctrl.c", there is no reference to the
>>>>> header
>>>>> file of WARN_ON_ONCE(). It seems that no error was reported. I think it
>>>>> might
>>>>> be that some other header files have indirectly referenced the header
>>>>> file of
>>>>> WARN_ON_ONCE(). However, when I compiled it locally, no errors were
>>>>> reported.
>>>>
>>>> Hi,
>>>>
>>>> Apparently it was missing from there as well.
>>>>
>>>> It might build now (I don't actually even doubt that), but that depends
>>>> on the includes in every file including drivers/pci/pci.h. We should not
>>>> depend on that as it is fragile but ensure a header is self-sufficient
>>>> when it comes to includes necessary for it.
>>>
>>> Hi Ilpo,
>>>
>>> OK, I see.
>>>
>>>
>>>>
>>>>> I think if we want to add a header file, it should be #include
>>>>> <asm-generic/bug.h> ?
>>>>
>>>> Yes
>>>>
>>>> #include <asm/bug.h>
>>
>> Hi Ilpo,
>>
>> Executing the following check script will trigger a warning. However, using
>> #include <asm-generic/bug.h> will not cause any issues. So, should we use
>> #include <asm-generic/bug.h> ?
>>
>> ./scripts/checkpatch.pl ...../*patch
>>
>> 0002-PCI-Move-pci_bus_speed2lnkctl2-to-public-header.patch
>> ---------------------------------------------------------------------
>> WARNING: Use #include <linux/bug.h> instead of <asm/bug.h>
>> #27: FILE: drivers/pci/pci.h:5:
>> +#include <asm/bug.h>
>>
>> total: 0 errors, 1 warnings, 53 lines checked
>>
>> NOTE: For some of the reported defects, checkpatch may be able to
>>        mechanically convert to the typical style using --fix or --fix-inplace.
> 
> This is a header, so using <asm/bug.h> minimizes what it pulls in so it
> is justified to use asm/ over linux/ in this case.
> 

Hi Ilpo,

Thank you. OK.

Best regards,
Hans

> 


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2026-04-07 13:02 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-06 10:47 [PATCH v5 0/3] PCI: Refactor PCIe speed validation and conversion functions Hans Zhang
2026-04-06 10:47 ` [PATCH v5 1/3] PCI: Add public pcie_valid_speed() for shared validation Hans Zhang
2026-04-06 10:47 ` [PATCH v5 2/3] PCI: Move pci_bus_speed2lnkctl2() to public header Hans Zhang
2026-04-07  8:11   ` Ilpo Järvinen
2026-04-07 12:09     ` Hans Zhang
2026-04-07 12:26       ` Ilpo Järvinen
2026-04-07 12:29         ` Hans Zhang
2026-04-07 12:48           ` Hans Zhang
2026-04-07 12:53             ` Ilpo Järvinen
2026-04-07 13:01               ` Hans Zhang
2026-04-06 10:47 ` [PATCH v5 3/3] PCI: dwc: Use common speed conversion function Hans Zhang
2026-04-06 10:54   ` Hans Zhang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox