[PATCH v12 10/25] CXL/AER: Update PCI class code check to use FIELD_GET()

Terry Bowman posted 25 patches 4 months, 2 weeks ago
There is a newer version of this series
[PATCH v12 10/25] CXL/AER: Update PCI class code check to use FIELD_GET()
Posted by Terry Bowman 4 months, 2 weeks ago
Update the AER driver's is_cxl_mem_dev() to use FIELD_GET() while checking
for a CXL Endpoint class code.

Introduce a genmask bitmask for checking PCI class codes and locate in
include/uapi/linux/pci_regs.h.

Update the function documentation to reference the latest CXL
specification.

Signed-off-by: Terry Bowman <terry.bowman@amd.com>

---

Changes in v11->v12:

Changes in v10->v11:
- Add #include <linux/bitfield.h> to cxl_ras.c
- Removed line wrapping at "(CXL 3.2, 8.1.12.1)".
---
 drivers/pci/pcie/aer.c         | 1 +
 drivers/pci/pcie/aer_cxl_rch.c | 6 +++---
 include/uapi/linux/pci_regs.h  | 2 ++
 3 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/drivers/pci/pcie/aer.c b/drivers/pci/pcie/aer.c
index befa73ace9bb..6ba8f84add70 100644
--- a/drivers/pci/pcie/aer.c
+++ b/drivers/pci/pcie/aer.c
@@ -30,6 +30,7 @@
 #include <linux/kfifo.h>
 #include <linux/ratelimit.h>
 #include <linux/slab.h>
+#include <linux/bitfield.h>
 #include <acpi/apei.h>
 #include <acpi/ghes.h>
 #include <ras/ras_event.h>
diff --git a/drivers/pci/pcie/aer_cxl_rch.c b/drivers/pci/pcie/aer_cxl_rch.c
index bfe071eebf67..c3e2d4cbe8cc 100644
--- a/drivers/pci/pcie/aer_cxl_rch.c
+++ b/drivers/pci/pcie/aer_cxl_rch.c
@@ -17,10 +17,10 @@ static bool is_cxl_mem_dev(struct pci_dev *dev)
 		return false;
 
 	/*
-	 * CXL Memory Devices must have the 502h class code set (CXL
-	 * 3.0, 8.1.12.1).
+	 * CXL Memory Devices must have the 502h class code set
+	 * (CXL 3.2, 8.1.12.1).
 	 */
-	if ((dev->class >> 8) != PCI_CLASS_MEMORY_CXL)
+	if (FIELD_GET(PCI_CLASS_CODE_MASK, dev->class) != PCI_CLASS_MEMORY_CXL)
 		return false;
 
 	return true;
diff --git a/include/uapi/linux/pci_regs.h b/include/uapi/linux/pci_regs.h
index bd03799612d3..802a7384f99a 100644
--- a/include/uapi/linux/pci_regs.h
+++ b/include/uapi/linux/pci_regs.h
@@ -73,6 +73,8 @@
 #define PCI_CLASS_PROG		0x09	/* Reg. Level Programming Interface */
 #define PCI_CLASS_DEVICE	0x0a	/* Device class */
 
+#define PCI_CLASS_CODE_MASK     __GENMASK(23, 8)
+
 #define PCI_CACHE_LINE_SIZE	0x0c	/* 8 bits */
 #define PCI_LATENCY_TIMER	0x0d	/* 8 bits */
 #define PCI_HEADER_TYPE		0x0e	/* 8 bits */
-- 
2.34.1
Re: [PATCH v12 10/25] CXL/AER: Update PCI class code check to use FIELD_GET()
Posted by Jonathan Cameron 4 months, 1 week ago
On Thu, 25 Sep 2025 17:34:25 -0500
Terry Bowman <terry.bowman@amd.com> wrote:

> Update the AER driver's is_cxl_mem_dev() to use FIELD_GET() while checking
> for a CXL Endpoint class code.
> 
> Introduce a genmask bitmask for checking PCI class codes and locate in
> include/uapi/linux/pci_regs.h.
> 
> Update the function documentation to reference the latest CXL
> specification.
> 
> Signed-off-by: Terry Bowman <terry.bowman@amd.com>
> 

The way that class code definitions work in pci_ids.h is somewhat odd
in my opinion, so I'd like input from Bjorn, Lukas etc on whether a
generic mask definition is a good idea or more likely to cause problems.

See for example. 
#define PCI_BASE_CLASS_STORAGE		0x01
...

#define PCI_CLASS_STORAGE_SATA		0x0106
#define PCI_CLASS_STORAGE_SATA_AHCI	0x010601


This variability in what is called CLASS_* leads to fun
situations like in drivers/ata/ahci.c where we have some
PCI_CLASS_* shifted and some not...

> ---
> 
> Changes in v11->v12:
> 
> Changes in v10->v11:
> - Add #include <linux/bitfield.h> to cxl_ras.c
> - Removed line wrapping at "(CXL 3.2, 8.1.12.1)".
> ---
>  drivers/pci/pcie/aer.c         | 1 +
>  drivers/pci/pcie/aer_cxl_rch.c | 6 +++---
>  include/uapi/linux/pci_regs.h  | 2 ++
>  3 files changed, 6 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/pci/pcie/aer.c b/drivers/pci/pcie/aer.c
> index befa73ace9bb..6ba8f84add70 100644
> --- a/drivers/pci/pcie/aer.c
> +++ b/drivers/pci/pcie/aer.c
> @@ -30,6 +30,7 @@
>  #include <linux/kfifo.h>
>  #include <linux/ratelimit.h>
>  #include <linux/slab.h>
> +#include <linux/bitfield.h>
>  #include <acpi/apei.h>
>  #include <acpi/ghes.h>
>  #include <ras/ras_event.h>
> diff --git a/drivers/pci/pcie/aer_cxl_rch.c b/drivers/pci/pcie/aer_cxl_rch.c
> index bfe071eebf67..c3e2d4cbe8cc 100644
> --- a/drivers/pci/pcie/aer_cxl_rch.c
> +++ b/drivers/pci/pcie/aer_cxl_rch.c
> @@ -17,10 +17,10 @@ static bool is_cxl_mem_dev(struct pci_dev *dev)
>  		return false;
>  
>  	/*
> -	 * CXL Memory Devices must have the 502h class code set (CXL
> -	 * 3.0, 8.1.12.1).
> +	 * CXL Memory Devices must have the 502h class code set
> +	 * (CXL 3.2, 8.1.12.1).
>  	 */
> -	if ((dev->class >> 8) != PCI_CLASS_MEMORY_CXL)
> +	if (FIELD_GET(PCI_CLASS_CODE_MASK, dev->class) != PCI_CLASS_MEMORY_CXL)
>  		return false;
>  
>  	return true;
> diff --git a/include/uapi/linux/pci_regs.h b/include/uapi/linux/pci_regs.h
> index bd03799612d3..802a7384f99a 100644
> --- a/include/uapi/linux/pci_regs.h
> +++ b/include/uapi/linux/pci_regs.h
> @@ -73,6 +73,8 @@
>  #define PCI_CLASS_PROG		0x09	/* Reg. Level Programming Interface */
>  #define PCI_CLASS_DEVICE	0x0a	/* Device class */
>  
> +#define PCI_CLASS_CODE_MASK     __GENMASK(23, 8)
> +
>  #define PCI_CACHE_LINE_SIZE	0x0c	/* 8 bits */
>  #define PCI_LATENCY_TIMER	0x0d	/* 8 bits */
>  #define PCI_HEADER_TYPE		0x0e	/* 8 bits */
Re: [PATCH v12 10/25] CXL/AER: Update PCI class code check to use FIELD_GET()
Posted by Lukas Wunner 4 months, 1 week ago
On Wed, Oct 01, 2025 at 05:12:16PM +0100, Jonathan Cameron wrote:
> On Thu, 25 Sep 2025 17:34:25 -0500 Terry Bowman <terry.bowman@amd.com> wrote:
> The way that class code definitions work in pci_ids.h is somewhat odd
> in my opinion, so I'd like input from Bjorn, Lukas etc on whether a
> generic mask definition is a good idea or more likely to cause problems.
> 
> See for example. 
> #define PCI_BASE_CLASS_STORAGE		0x01
> ...
> 
> #define PCI_CLASS_STORAGE_SATA		0x0106
> #define PCI_CLASS_STORAGE_SATA_AHCI	0x010601
> 
> This variability in what is called CLASS_* leads to fun
> situations like in drivers/ata/ahci.c where we have some
> PCI_CLASS_* shifted and some not...

Macros working with PCI class codes generally accept a "shift" parameter
to know by how many bits the class code needs to be shifted, see e.g.
DECLARE_PCI_FIXUP_CLASS_EARLY() and friends.

A macro which shifts exactly by 8 is hence only of limited use.


> > +++ b/drivers/pci/pcie/aer_cxl_rch.c
> > @@ -17,10 +17,10 @@ static bool is_cxl_mem_dev(struct pci_dev *dev)
> >  		return false;
> >  
> >  	/*
> > -	 * CXL Memory Devices must have the 502h class code set (CXL
> > -	 * 3.0, 8.1.12.1).
> > +	 * CXL Memory Devices must have the 502h class code set
> > +	 * (CXL 3.2, 8.1.12.1).
> >  	 */
> > -	if ((dev->class >> 8) != PCI_CLASS_MEMORY_CXL)
> > +	if (FIELD_GET(PCI_CLASS_CODE_MASK, dev->class) != PCI_CLASS_MEMORY_CXL)
> >  		return false;

Hm, this doesn't look more readable TBH.

Refactoring changes like this one should be submitted separately from
this patch set.  If any of them are controversial, they delay upstreaming
of the actual change, i.e. the CXL AER plumbing.  They also increase the
size of the patch set, making it more difficult to review.


> > +++ b/include/uapi/linux/pci_regs.h
> > @@ -73,6 +73,8 @@
> >  #define PCI_CLASS_PROG		0x09	/* Reg. Level Programming Interface */
> >  #define PCI_CLASS_DEVICE	0x0a	/* Device class */
> >  
> > +#define PCI_CLASS_CODE_MASK     __GENMASK(23, 8)
> > +
> >  #define PCI_CACHE_LINE_SIZE	0x0c	/* 8 bits */
> >  #define PCI_LATENCY_TIMER	0x0d	/* 8 bits */
> >  #define PCI_HEADER_TYPE		0x0e	/* 8 bits */

Putting this in a uapi header means we'll have to support it
indefinitely.  Usually such macros are added to kernel-internal
headers first and moved to uapi headers if/when the need arises.

Thanks,

Lukas
Re: [PATCH v12 10/25] CXL/AER: Update PCI class code check to use FIELD_GET()
Posted by Bowman, Terry 3 months, 1 week ago

On 10/2/2025 2:40 AM, Lukas Wunner wrote:
> On Wed, Oct 01, 2025 at 05:12:16PM +0100, Jonathan Cameron wrote:
>> On Thu, 25 Sep 2025 17:34:25 -0500 Terry Bowman <terry.bowman@amd.com> wrote:
>> The way that class code definitions work in pci_ids.h is somewhat odd
>> in my opinion, so I'd like input from Bjorn, Lukas etc on whether a
>> generic mask definition is a good idea or more likely to cause problems.
>>
>> See for example. 
>> #define PCI_BASE_CLASS_STORAGE		0x01
>> ...
>>
>> #define PCI_CLASS_STORAGE_SATA		0x0106
>> #define PCI_CLASS_STORAGE_SATA_AHCI	0x010601
>>
>> This variability in what is called CLASS_* leads to fun
>> situations like in drivers/ata/ahci.c where we have some
>> PCI_CLASS_* shifted and some not...
> Macros working with PCI class codes generally accept a "shift" parameter
> to know by how many bits the class code needs to be shifted, see e.g.
> DECLARE_PCI_FIXUP_CLASS_EARLY() and friends.
>
> A macro which shifts exactly by 8 is hence only of limited use.
>
>
>>> +++ b/drivers/pci/pcie/aer_cxl_rch.c
>>> @@ -17,10 +17,10 @@ static bool is_cxl_mem_dev(struct pci_dev *dev)
>>>  		return false;
>>>  
>>>  	/*
>>> -	 * CXL Memory Devices must have the 502h class code set (CXL
>>> -	 * 3.0, 8.1.12.1).
>>> +	 * CXL Memory Devices must have the 502h class code set
>>> +	 * (CXL 3.2, 8.1.12.1).
>>>  	 */
>>> -	if ((dev->class >> 8) != PCI_CLASS_MEMORY_CXL)
>>> +	if (FIELD_GET(PCI_CLASS_CODE_MASK, dev->class) != PCI_CLASS_MEMORY_CXL)
>>>  		return false;
> Hm, this doesn't look more readable TBH.
>
> Refactoring changes like this one should be submitted separately from
> this patch set.  If any of them are controversial, they delay upstreaming
> of the actual change, i.e. the CXL AER plumbing.  They also increase the
> size of the patch set, making it more difficult to review.
>
>
>>> +++ b/include/uapi/linux/pci_regs.h
>>> @@ -73,6 +73,8 @@
>>>  #define PCI_CLASS_PROG		0x09	/* Reg. Level Programming Interface */
>>>  #define PCI_CLASS_DEVICE	0x0a	/* Device class */
>>>  
>>> +#define PCI_CLASS_CODE_MASK     __GENMASK(23, 8)
>>> +
>>>  #define PCI_CACHE_LINE_SIZE	0x0c	/* 8 bits */
>>>  #define PCI_LATENCY_TIMER	0x0d	/* 8 bits */
>>>  #define PCI_HEADER_TYPE		0x0e	/* 8 bits */
> Putting this in a uapi header means we'll have to support it
> indefinitely.  Usually such macros are added to kernel-internal
> headers first and moved to uapi headers if/when the need arises.
>
> Thanks,
>
> Lukas
Apologies for the late response.

The PCI_CLASS_CODE_MASK definition move to include/uapi/linux/pci_regs.h was requested here:
https://lore.kernel.org/linux-cxl/6881626a784f_134cc7100b4@dwillia2-xfh.jf.intel.com.notmuch/

Would you like this patch removed altogether? 

Terry


Re: [PATCH v12 10/25] CXL/AER: Update PCI class code check to use FIELD_GET()
Posted by Lukas Wunner 3 months, 1 week ago
On Thu, Oct 30, 2025 at 12:16:06PM -0500, Bowman, Terry wrote:
> On 10/2/2025 2:40 AM, Lukas Wunner wrote:
> > On Wed, Oct 01, 2025 at 05:12:16PM +0100, Jonathan Cameron wrote:
> >> On Thu, 25 Sep 2025 17:34:25 -0500 Terry Bowman <terry.bowman@amd.com> wrote:
> >> The way that class code definitions work in pci_ids.h is somewhat odd
> >> in my opinion, so I'd like input from Bjorn, Lukas etc on whether a
> >> generic mask definition is a good idea or more likely to cause problems.
> >>
> >> See for example. 
> >> #define PCI_BASE_CLASS_STORAGE		0x01
> >> ...
> >>
> >> #define PCI_CLASS_STORAGE_SATA		0x0106
> >> #define PCI_CLASS_STORAGE_SATA_AHCI	0x010601
> >>
> >> This variability in what is called CLASS_* leads to fun
> >> situations like in drivers/ata/ahci.c where we have some
> >> PCI_CLASS_* shifted and some not...
> > Macros working with PCI class codes generally accept a "shift" parameter
> > to know by how many bits the class code needs to be shifted, see e.g.
> > DECLARE_PCI_FIXUP_CLASS_EARLY() and friends.
> >
> > A macro which shifts exactly by 8 is hence only of limited use.
> >
> >>> +++ b/drivers/pci/pcie/aer_cxl_rch.c
> >>> @@ -17,10 +17,10 @@ static bool is_cxl_mem_dev(struct pci_dev *dev)
> >>>  		return false;
> >>>  
> >>>  	/*
> >>> -	 * CXL Memory Devices must have the 502h class code set (CXL
> >>> -	 * 3.0, 8.1.12.1).
> >>> +	 * CXL Memory Devices must have the 502h class code set
> >>> +	 * (CXL 3.2, 8.1.12.1).
> >>>  	 */
> >>> -	if ((dev->class >> 8) != PCI_CLASS_MEMORY_CXL)
> >>> +	if (FIELD_GET(PCI_CLASS_CODE_MASK, dev->class) != PCI_CLASS_MEMORY_CXL)
> >>>  		return false;
> > Hm, this doesn't look more readable TBH.
> >
> > Refactoring changes like this one should be submitted separately from
> > this patch set.  If any of them are controversial, they delay upstreaming
> > of the actual change, i.e. the CXL AER plumbing.  They also increase the
> > size of the patch set, making it more difficult to review.
> >
> >>> +++ b/include/uapi/linux/pci_regs.h
> >>> @@ -73,6 +73,8 @@
> >>>  #define PCI_CLASS_PROG		0x09	/* Reg. Level Programming Interface */
> >>>  #define PCI_CLASS_DEVICE	0x0a	/* Device class */
> >>>  
> >>> +#define PCI_CLASS_CODE_MASK     __GENMASK(23, 8)
> >>> +
> >>>  #define PCI_CACHE_LINE_SIZE	0x0c	/* 8 bits */
> >>>  #define PCI_LATENCY_TIMER	0x0d	/* 8 bits */
> >>>  #define PCI_HEADER_TYPE		0x0e	/* 8 bits */
> > Putting this in a uapi header means we'll have to support it
> > indefinitely.  Usually such macros are added to kernel-internal
> > headers first and moved to uapi headers if/when the need arises.
> 
> The PCI_CLASS_CODE_MASK definition move to include/uapi/linux/pci_regs.h
> was requested here:
> https://lore.kernel.org/linux-cxl/6881626a784f_134cc7100b4@dwillia2-xfh.jf.intel.com.notmuch/

Hm, I'm not seeing such a request in the linked message.

> Would you like this patch removed altogether?

"I wouldn't object to it being removed" would be a more pleasant way
of phrasing it. ;)

Thanks,

Lukas
Re: [PATCH v12 10/25] CXL/AER: Update PCI class code check to use FIELD_GET()
Posted by Dave Jiang 4 months, 2 weeks ago

On 9/25/25 3:34 PM, Terry Bowman wrote:
> Update the AER driver's is_cxl_mem_dev() to use FIELD_GET() while checking
> for a CXL Endpoint class code.
> 
> Introduce a genmask bitmask for checking PCI class codes and locate in
> include/uapi/linux/pci_regs.h.
> 
> Update the function documentation to reference the latest CXL
> specification.
> 
> Signed-off-by: Terry Bowman <terry.bowman@amd.com>

Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> 
> ---
> 
> Changes in v11->v12:
> 
> Changes in v10->v11:
> - Add #include <linux/bitfield.h> to cxl_ras.c
> - Removed line wrapping at "(CXL 3.2, 8.1.12.1)".
> ---
>  drivers/pci/pcie/aer.c         | 1 +
>  drivers/pci/pcie/aer_cxl_rch.c | 6 +++---
>  include/uapi/linux/pci_regs.h  | 2 ++
>  3 files changed, 6 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/pci/pcie/aer.c b/drivers/pci/pcie/aer.c
> index befa73ace9bb..6ba8f84add70 100644
> --- a/drivers/pci/pcie/aer.c
> +++ b/drivers/pci/pcie/aer.c
> @@ -30,6 +30,7 @@
>  #include <linux/kfifo.h>
>  #include <linux/ratelimit.h>
>  #include <linux/slab.h>
> +#include <linux/bitfield.h>
>  #include <acpi/apei.h>
>  #include <acpi/ghes.h>
>  #include <ras/ras_event.h>
> diff --git a/drivers/pci/pcie/aer_cxl_rch.c b/drivers/pci/pcie/aer_cxl_rch.c
> index bfe071eebf67..c3e2d4cbe8cc 100644
> --- a/drivers/pci/pcie/aer_cxl_rch.c
> +++ b/drivers/pci/pcie/aer_cxl_rch.c
> @@ -17,10 +17,10 @@ static bool is_cxl_mem_dev(struct pci_dev *dev)
>  		return false;
>  
>  	/*
> -	 * CXL Memory Devices must have the 502h class code set (CXL
> -	 * 3.0, 8.1.12.1).
> +	 * CXL Memory Devices must have the 502h class code set
> +	 * (CXL 3.2, 8.1.12.1).
>  	 */
> -	if ((dev->class >> 8) != PCI_CLASS_MEMORY_CXL)
> +	if (FIELD_GET(PCI_CLASS_CODE_MASK, dev->class) != PCI_CLASS_MEMORY_CXL)
>  		return false;
>  
>  	return true;
> diff --git a/include/uapi/linux/pci_regs.h b/include/uapi/linux/pci_regs.h
> index bd03799612d3..802a7384f99a 100644
> --- a/include/uapi/linux/pci_regs.h
> +++ b/include/uapi/linux/pci_regs.h
> @@ -73,6 +73,8 @@
>  #define PCI_CLASS_PROG		0x09	/* Reg. Level Programming Interface */
>  #define PCI_CLASS_DEVICE	0x0a	/* Device class */
>  
> +#define PCI_CLASS_CODE_MASK     __GENMASK(23, 8)
> +
>  #define PCI_CACHE_LINE_SIZE	0x0c	/* 8 bits */
>  #define PCI_LATENCY_TIMER	0x0d	/* 8 bits */
>  #define PCI_HEADER_TYPE		0x0e	/* 8 bits */