Skip to contentSkip to conversation

CXL: Read CDAT

PATCH Superseded [email protected] 3 participants 23 messages Jul 15, 2022 – Jul 20, 2022

Subsystems (4)

v14 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

v14 cover CXL: Read CDAT

From: Ira Weiny <[email protected]>

Details of changes are in the individual patches.

Major changes from V13:[10]
Dan minor updates
Willy's suggestion of documentation is good but I'm deferring it until we get the location of the PCI mailboxes settled.
Drop retry CDAT patch
Drop DSMAS patch
Rebased on latest cxl-pending

CXL drivers need various data which are provided through generic DOE mailboxes as defined in the PCIe 6.0 spec.[1]

One such data is the Coherent Device Attribute Table (CDAT). CDAT data provides coherent information about the various devices in the system. It was developed because systems no longer have a priori knowledge of all coherent devices within a system. CDAT describes the coherent characteristics of the components on the CXL bus separate from system configurations. The OS can then, for example, use this information to form correct interleave sets.

To begin reading the CDAT the OS must have support to access the DOE mailboxes provided by the CXL devices.

Because DOE is not specific to DOE but is provided within the PCI spec, the series adds PCI DOE capability library functions. These functions allow for the iteration of the DOE capabilities on a device as well as creating pci_doe_mb structures which can control the operation of the DOE state machine.

For now the iteration of and storage of the DOE mailboxes is done on memdev objects within the CXL stack. When this is needed in more generic code this can be lifted later.

This work was tested using qemu.

[0] https://lore.kernel.org/linux-cxl/[email protected]/
[1] https://pcisig.com/specifications
[2] https://lore.kernel.org/qemu-devel/[email protected]/
[3] https://lore.kernel.org/linux-cxl/[email protected]/
[4] https://lore.kernel.org/linux-cxl/[email protected]/
[5] https://lore.kernel.org/linux-cxl/[email protected]/
[6] https://lore.kernel.org/linux-cxl/[email protected]/
[7] https://lore.kernel.org/linux-cxl/[email protected]/
[8] https://lore.kernel.org/linux-cxl/[email protected]/
[9] https://lore.kernel.org/linux-cxl/[email protected]/
[10] https://lore.kernel.org/linux-cxl/[email protected]/

Previous changes
================

Changes from V12:[9]
A couple of bug fixes in the new XArray stuff
Remove the IRQ support because I did not realize how that worked and it was complicating things.
Remove busy retries and replace with an error as there is no good way to ensure it will work.
Other code clean ups mentioned in the individual patches.

Changes from V11:[8]
The major change in this version is to remove the workqueue from the internal implementation of the state machine. A single ordered workqueue within each mailbox processes tasks submitted. This workqueue takes care of all locking and guarantees that tasks are completed in the order submitted. Any synchronization which is required between tasks will need to be handled by the user of the mailbox. However, the user can depend on work items being completed in the order they are submitted. So a single thread submitter is guaranteed to get all work items completed in order. This also aids in the support of a single mailbox supporting multiple protocols. Each protocol could have a separate thread submitting tasks for that protocol. The mailbox object will ensure that each protocol task is complete before another task starts. But multiple user threads can be submitting tasks for different protocols all at the same time without regard to other protocols being used.

XArrays are used throughout the series.

Other minor changes are noted in the individual patches.

Changes from V10:[7]
Address Ben Widawsky's comments
Protect against potentially malicious devices. Fix ownership issue of cdat_mb

Changes from V9:[6]
Address feedback from
Lukas Wunner, Davidlohr Bueso, Jonathan Cameron, Alison Schofield, and Ben Widawsky Details in each individual patch.

Changes from V8:[5]
For this version I've punted a bit to get it out and drop the auxiliary bus functionality. I like where Jonathan is going with the port driver idea. I think eventually the irq/mailbox creation will need to be more generic in a PCI port driver. I've modeled this version on such an architecture but used the CXL port for the time being.

From Dan
Drop the auxiliary bus/device
From Jonathan
Cleanups
From Bjorn
Clean up commit messages
move pci-doe.c to doe.c
Clean up PCI spec references
Ensure all messages use pci_*()
Add offset to error messages to distinguish mailboxes
use hex for DOE offsets
Print 4 nibbles for Vendor ID and 2 for type. s/irq/IRQ in comments
Fix long lines
Fix typos

Changes from V7:[4]
Avoid code bloat by making pci-doe.c conditional on CONFIG_PCI_DOE
which is auto selected by the CXL_PCI config option.
Minor code clean ups
Fix bug in pci_doe_supports_prot()
Rebase to cxl-pending

Changes from V6:[3]
The big change is the removal of the auxiliary bus code from the PCI layer. The auxiliary bus usage is now in the CXL layer. The PCI layer provides helpers for subsystems to utilize DOE mailboxes by creating a pci_doe_mb object which controls a state machine for that mailbox capability. The CXL layer wraps this object in an auxiliary device and driver which can then be used to determine if the kernel is controlling the capability or it is available to be used by user space. Reads from user space via lspci are allowed. Writes are allowed but flagged via a tainting the kernel.

Feedback from Bjorn, Jonathan, and Dan
Details in each patch

Changes from V5:[0]

Rework the patch set to split PCI vs CXL changes
Also make each change a bit more stand alone for easier review
Add cxl_cdat structure
Put CDAT related data structures in cdat.h
Clarify some device lifetimes with comments
Incorporate feedback from Jonathan, Bjorn and Dan
The bigest change is placing the DOE scanning code into the
pci_doe driver (part of the PCI codre).
Validate the CDAT when it is read rather than before DSMAS
parsing
Do not report DSMAS failure as an error, report a warning and
keep going.
Retry reading the table 1 time.
Update commit messages and this cover letter

Ira Weiny (5):
PCI: Replace magic constant for PCI Sig Vendor ID cxl/pci: Create PCI DOE mailbox's for memory devices
driver-core: Introduce BIN_ATTR_ADMIN_{RO,RW} cxl/port: Read CDAT table
cxl/port: Introduce cxl_cdat_valid()

Jonathan Cameron (2):
PCI: Add vendor ID for the PCI SIG
PCI/DOE: Add DOE mailbox support functions

 .clang-format                           |   1 +
 Documentation/ABI/testing/sysfs-bus-cxl |  10 +
 drivers/cxl/Kconfig                     |   1 +
 drivers/cxl/cdat.h                      |  63 +++
 drivers/cxl/core/pci.c                  | 206 +++++++++
 drivers/cxl/cxl.h                       |   5 +
 drivers/cxl/cxlmem.h                    |   3 +
 drivers/cxl/cxlpci.h                    |   1 +
 drivers/cxl/pci.c                       |  44 ++
 drivers/cxl/port.c                      |  54 +++
 drivers/pci/Kconfig                     |   3 +
 drivers/pci/Makefile                    |   1 +
 drivers/pci/doe.c                       | 546 ++++++++++++++++++++++++
 drivers/pci/probe.c                     |   2 +-
 include/linux/pci-doe.h                 |  79 ++++
 include/linux/pci_ids.h                 |   1 +
 include/linux/sysfs.h                   |  16 +
 include/uapi/linux/pci_regs.h           |  29 +-
 18 files changed, 1063 insertions(+), 2 deletions(-)
 create mode 100644 drivers/cxl/cdat.h
 create mode 100644 drivers/pci/doe.c
 create mode 100644 include/linux/pci-doe.h

base-commit: b060edfd8cdd52bc8648392500bf152a8dd6d4c5

Signature · 1 line
2.35.3
#1 lore raw

PATCH v14 1/7 PCI: Add vendor ID for the PCI SIG

From: Jonathan Cameron <[email protected]>

This ID is used in DOE headers to identify protocols that are defined within the PCI Express Base Specification, PCIe r6.0, sec 6.30.1.1 table 6-32.

---
 include/linux/pci_ids.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/pci_ids.h +1 -0
index 0178823ce8c2..8af3b86206b1 100644
@@ -151,6 +151,7 @@
 #define PCI_CLASS_OTHERS		0xff
 
 /* Vendors and devices.  Sort key: vendor first, device next. */
+#define PCI_VENDOR_ID_PCI_SIG		0x0001
 
 #define PCI_VENDOR_ID_LOONGSON		0x0014
 
Signature · 1 line
2.35.3
#2 lore raw

PATCH v14 2/7 PCI: Replace magic constant for PCI Sig Vendor ID

In reply to [email protected]

From: Ira Weiny <[email protected]>

Replace the magic value in pci_bus_crs_vendor_id() with PCI_VENDOR_ID_PCI_SIG.

---
Changes from V6
Simplify commit message

---
 drivers/pci/probe.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/probe.c +1 -1
index 17a969942d37..6280e780a48c 100644
@@ -2312,7 +2312,7 @@ EXPORT_SYMBOL(pci_alloc_dev);
 
 static bool pci_bus_crs_vendor_id(u32 l)
 {
-	return (l & 0xffff) == 0x0001;
+	return (l & 0xffff) == PCI_VENDOR_ID_PCI_SIG;
 }
 
 static bool pci_bus_wait_crs(struct pci_bus *bus, int devfn, u32 *l,
Signature · 1 line
2.35.3
#3 lore raw

PATCH v14 3/7 PCI/DOE: Add DOE mailbox support functions

In reply to [email protected]

From: Jonathan Cameron <[email protected]>

Introduced in a PCIe r6.0, sec 6.30, DOE provides a config space based mailbox with standard protocol discovery. Each mailbox is accessed through a DOE Extended Capability.

Each DOE mailbox must support the DOE discovery protocol in addition to any number of additional protocols.

Define core PCIe functionality to manage a single PCIe DOE mailbox at a defined config space offset. Functionality includes iterating, creating, query of supported protocol, and task submission. Destruction of the mailboxes is device managed.

---
Changes from V13.1
Clean up docs
Dan:
pass up pci_doe_wait() error
use dev_err_ratelimited
Remove extra '{'
pass up xa_insert() return code
Use pci_name() and dev_driver_string() to name WQ

Changes from V13
Bjorn:
Update commit message
Put fmt strings on same line as pci_err s/successful/successfully Clean up line wrap on comments
Matthew:
Fix up shifting in xa_mk_value()

Changes from V12
Remove busy retries
s/pci_doe_xa_entry/pci_doe_xa_prot_entry
Open code pci_doe_abort call
s/ABORT/CANCEL
Clarify canceling a task by using a cancel flag instead
of 'abort' flag
Clean up comments
Jonathan:
use xa_mk_value() for XArray value
spaces around '-' operator
s/possitive/positive
s/pci_doe_issue_abort/pci_doe_abort
Remove pci_doe_free_mb() and use devm_* exclusively in pci_doe_create_mb() s/ret/rc to be consistent
Remove irq support.
Clean up timeout values and ensure waits values are correct Remove unnecessary arm_wait function

Changes from V11
Bjorn: s/PCI/PCIe
use dev_fmt
move cap_offset in struct pci_doe_mb
use break and return from a central place fix interrupt prints s/PCI_DOE_CAP_IRQ/PCI_DOE_CAP_INT_MSG_NUM s/irq_msg_num/int_msg_num
when the value is not an irq but rather the interrupt message number for the DOE
s/irq/IRQ in comments
Clarify request and response payload size units
In addition clarify the rv units Check for invalid response payload size (must be at least 1 DW)
Dan: s/EOPNOTSUPP/ENXIO/
Add pci_doe_for_each_off to .clang-format use xarray for supported protocols s/pci_doe_create_mb/pcim_doe_create_mb/
Remove pci_doe_destroy_mb

Dan: Convert the statemachine to process tasks as work items
Define pci_doe_write_ctrl()
Introduce pci_doe_irq_enabled()
issue a stand alone abort
Don't go through the state machine for the abort. Just poll/irq until the response comes back.
Remove Wait Abort state
A wait abort can just be triggered from outside and stop the state machine from whatever loop it may be in. Let the state machine issue the abort itself and wait for it to return or not.
Remove Wait abort on error
Issue the abort directly before returning. Abort failure will flag the MB dead.
Remove workqueue processing from state machine clean up function locations in the file
Move abort flag/document it
React to an abort while aborting and bail. This will mark the mailbox dead.
Convert task to a work item
Create a workqueue in the mailbox. Remove cur_task and locking. Set DEAD when taking mailbox down.
print error on marking mailbox dead
Introduce signal_task_abort
flatten out the state machine

Changes from V9
Lukas Wunner
Update comments
Move private doe structures and defines from pci-doe.h to doe.c check Data Obj Ready prior to last ack
Davidlohr
make task_lock a spinlock
Lukas/Jonathan
Remove special case of error in irq handler
Fix potential race with the scheduling of a task when one is ending.
The current task can't be retired until the state machine is idle. Otherwise a new task work item may run and the state machine would be out of sync.

Changes from V8
Remove Bjorn's ack
Expose a function to find the irq number for a mailbox based on offset. This is the code Jonathan proposed for finding the irq number here:
https://lore.kernel.org/linux-cxl/[email protected]/
This removes funky bool parameter to create.
Move pci_set_master() within the pci_doe_enable_irq() Per Bjorn
Clean up commit messages
move pci-doe.c to doe.c
Clean up PCI spec references
Ensure all messages use pci_*()
Add offset to error messages to distinguish mailboxes
use hex for DOE offsets
Print 4 nibbles for Vendor ID and 2 for type. s/irq/IRQ in comments
Fix long lines
Fix typos

Changes from V7
Add a Kconfig for this functionality
Fix bug in pci_doe_supports_prot()
Rebased on cxl-pending

Changes from V6
Clean up signed off by lines
Make this functionality all PCI library functions
Clean up header files
s/pci_doe_irq/pci_doe_irq_handler
Use pci_{request,free}_irq
Remove irq_name (maintained by pci_request_irq)
Fix checks to use an irq
Consistently use u16 for cap_offset
Cleanup kdocs and comments
Create a helper retire_cur_task() to handle locking of the
current task pointer.
Remove devm_ calls from PCI layer.
The devm_ calls do not allow for the pci_doe_mb objects to be tied to an auxiliary device. Leave it to the caller to use devm_ if desired.
From Dan Williams
s/cb/end_task/; Pass pci_doe_task to end_task Clarify exchange/task/request/response.
Merge pci_doe_task and pci_doe_exchange into pci_doe_task which represents a single request/response task for the state machine to process.
Simplify submitting work to the mailbox
Replace pci_doe_exchange_sync() with pci_doe_submit_task() Consumers of the mailbox are now responsible for setting up callbacks within a task object and submitting them to the mailbox to be processed.
Remove WARN_ON when task != NULL and be sure to abort that task. Convert abort/dead to atomic flags s/state_lock/task_lock to better define what the lock is
protecting
Remove all the auxiliary bus code from the PCI layer
The PCI layer provides helpers to use the DOE Mailboxes. Each subsystem can then use the helpers as they see fit. The CXL layer in this series uses aux devices to manage the new pci_doe_mb objects.

From Bjorn
Clarify the fact that DOE mailboxes are capabilities of
the device.
Code clean ups
Cleanup Makefile
Update references to PCI SIG spec v6.0
Move this attribution here:
This code is based on Jonathan's V4 series here:
https://lore.kernel.org/linux-cxl/[email protected]/

Changes from V5
From Bjorn
s/pci_WARN/pci_warn
Add timeout period to print
Trim to 80 chars
Use Tabs for DOE define spacing
Use %#x for clarity
From Jonathan
Addresses concerns about the order of unwinding stuff s/doe/doe_dev in pci_doe_exhcnage_sync
Correct kernel Doc comment
Move pci_doe_task_complete() down in the file. Rework pci_doe_irq()
process STATUS_ERROR first
Return IRQ_NONE if the irq is not processed Use PCI_DOE_STATUS_INT_STATUS explicitly to
clear the irq
Clean up goto label s/err_free_irqs/err_free_irq
use devm_kzalloc for doe struct
clean up error paths in pci_doe_probe s/pci_doe_drv/pci_doe
remove include mutex.h
remove device name and define, move it in the next patch which uses it use devm_kasprintf() for irq_name
use devm_request_irq()
remove pci_doe_unregister()
[get/put]_device() were unneeded and with the use of devm_* this function can be removed completely.
refactor pci_doe_register and s/pci_doe_register/pci_doe_reg_irq
make this function just a registration of the irq and move pci_doe_abort() into pci_doe_probe()
use devm_* to allocate the protocol array

Changes from Jonathan's V4
Move the DOE MB code into the DOE auxiliary driver Remove Task List in favor of a wait queue

Changes from Ben
remove CXL references
propagate rc from pci functions on error

---
 .clang-format                 |   1 +
 drivers/pci/Kconfig           |   3 +
 drivers/pci/Makefile          |   1 +
 drivers/pci/doe.c             | 546 ++++++++++++++++++++++++++++++++++
 include/linux/pci-doe.h       |  79 +++++
 include/uapi/linux/pci_regs.h |  29 +-
 6 files changed, 658 insertions(+), 1 deletion(-)
 create mode 100644 drivers/pci/doe.c
 create mode 100644 include/linux/pci-doe.h
.clang-format +1 -0
index 9b87ea1fc16e..1247d54f9e49 100644
@@ -516,6 +516,7 @@ ForEachMacros:
   - 'of_property_for_each_string'
   - 'of_property_for_each_u32'
   - 'pci_bus_for_each_resource'
+  - 'pci_doe_for_each_off'
   - 'pcl_for_each_chunk'
   - 'pcl_for_each_segment'
   - 'pcm_for_each_format'
drivers/pci/Kconfig +3 -0
index 133c73207782..b2f2e588a817 100644
@@ -121,6 +121,9 @@ config XEN_PCIDEV_FRONTEND
 config PCI_ATS
 	bool
 
+config PCI_DOE
+	bool
+
 config PCI_ECAM
 	bool
 
drivers/pci/Makefile +1 -0
index 0da6b1ebc694..2680e4c92f0a 100644
@@ -31,6 +31,7 @@ obj-$(CONFIG_PCI_ECAM)		+= ecam.o
 obj-$(CONFIG_PCI_P2PDMA)	+= p2pdma.o
 obj-$(CONFIG_XEN_PCIDEV_FRONTEND) += xen-pcifront.o
 obj-$(CONFIG_VGA_ARB)		+= vgaarb.o
+obj-$(CONFIG_PCI_DOE)		+= doe.o
 
 # Endpoint library must be initialized before its users
 obj-$(CONFIG_PCI_ENDPOINT)	+= endpoint/
drivers/pci/doe.c new file +546 -0

546 lines not on this page · raw message

include/linux/pci-doe.h new file +79 -0

79 lines not on this page · raw message

include/uapi/linux/pci_regs.h +28 -1
index 108f8523fa04..57b8e2ffb1dd 100644
@@ -737,7 +737,8 @@
 #define PCI_EXT_CAP_ID_DVSEC	0x23	/* Designated Vendor-Specific */
 #define PCI_EXT_CAP_ID_DLF	0x25	/* Data Link Feature */
 #define PCI_EXT_CAP_ID_PL_16GT	0x26	/* Physical Layer 16.0 GT/s */
-#define PCI_EXT_CAP_ID_MAX	PCI_EXT_CAP_ID_PL_16GT
+#define PCI_EXT_CAP_ID_DOE	0x2E	/* Data Object Exchange */
+#define PCI_EXT_CAP_ID_MAX	PCI_EXT_CAP_ID_DOE
 
 #define PCI_EXT_CAP_DSN_SIZEOF	12
 #define PCI_EXT_CAP_MCAST_ENDPOINT_SIZEOF 40
@@ -1103,4 +1104,30 @@
 #define  PCI_PL_16GT_LE_CTRL_USP_TX_PRESET_MASK		0x000000F0
 #define  PCI_PL_16GT_LE_CTRL_USP_TX_PRESET_SHIFT	4
 
+/* Data Object Exchange */
+#define PCI_DOE_CAP		0x04    /* DOE Capabilities Register */
+#define  PCI_DOE_CAP_INT_SUP			0x00000001  /* Interrupt Support */
+#define  PCI_DOE_CAP_INT_MSG_NUM		0x00000ffe  /* Interrupt Message Number */
+#define PCI_DOE_CTRL		0x08    /* DOE Control Register */
+#define  PCI_DOE_CTRL_ABORT			0x00000001  /* DOE Abort */
+#define  PCI_DOE_CTRL_INT_EN			0x00000002  /* DOE Interrupt Enable */
+#define  PCI_DOE_CTRL_GO			0x80000000  /* DOE Go */
+#define PCI_DOE_STATUS		0x0c    /* DOE Status Register */
+#define  PCI_DOE_STATUS_BUSY			0x00000001  /* DOE Busy */
+#define  PCI_DOE_STATUS_INT_STATUS		0x00000002  /* DOE Interrupt Status */
+#define  PCI_DOE_STATUS_ERROR			0x00000004  /* DOE Error */
+#define  PCI_DOE_STATUS_DATA_OBJECT_READY	0x80000000  /* Data Object Ready */
+#define PCI_DOE_WRITE		0x10    /* DOE Write Data Mailbox Register */
+#define PCI_DOE_READ		0x14    /* DOE Read Data Mailbox Register */
+
+/* DOE Data Object - note not actually registers */
+#define PCI_DOE_DATA_OBJECT_HEADER_1_VID		0x0000ffff
+#define PCI_DOE_DATA_OBJECT_HEADER_1_TYPE		0x00ff0000
+#define PCI_DOE_DATA_OBJECT_HEADER_2_LENGTH		0x0003ffff
+
+#define PCI_DOE_DATA_OBJECT_DISC_REQ_3_INDEX		0x000000ff
+#define PCI_DOE_DATA_OBJECT_DISC_RSP_3_VID		0x0000ffff
+#define PCI_DOE_DATA_OBJECT_DISC_RSP_3_PROTOCOL		0x00ff0000
+#define PCI_DOE_DATA_OBJECT_DISC_RSP_3_NEXT_INDEX	0xff000000
+
 #endif /* LINUX_PCI_REGS_H */
Signature · 1 line
2.35.3
#4 lore raw

Re: [PATCH V14 3/7] PCI/DOE: Add DOE mailbox support functions

Quoted 14 lines · On Thu, 14 Jul 2022 20:04:20 -0700 [email protected] wrote:

From: Jonathan Cameron <[email protected]>

Introduced in a PCIe r6.0, sec 6.30, DOE provides a config space based mailbox with standard protocol discovery. Each mailbox is accessed through a DOE Extended Capability.

Each DOE mailbox must support the DOE discovery protocol in addition to any number of additional protocols.

Define core PCIe functionality to manage a single PCIe DOE mailbox at a defined config space offset. Functionality includes iterating, creating, query of supported protocol, and task submission. Destruction of the mailboxes is device managed.

Quoted 7 lines

Hi Ira,

Thanks for persisting with this!

So, I think this works, but there is at least one 'sleep' I can't see a purpose for. I think it's just a left over from refactoring.

A few other more trivial things inline.

Thanks,

Jonathan

Quoted 24 lines
Quoted 2 lines

# Endpoint library must be initialized before its users

obj-$(CONFIG_PCI_ENDPOINT) += endpoint/

drivers/pci/doe.c new file +15 -0
new file mode 100644
index 000000000000..12c3762be22f
@@ -0,0 +1,546 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Data Object Exchange
+ *	PCIe r6.0, sec 6.30 DOE
+ *
+ * Copyright (C) 2021 Huawei
+ *	Jonathan Cameron <[email protected]>
+ *
+ * Copyright (C) 2022 Intel Corporation
+ *	Ira Weiny <[email protected]>
+ */
+
+#define dev_fmt(fmt) "DOE: " fmt
+
+#include <linux/bitfield.h>

drivers/pci/doe.c line 25

Quoted 10 lines
drivers/pci/doe.c new file +10 -0
+#include <linux/delay.h>
+#include <linux/jiffies.h>
+#include <linux/mutex.h>
+#include <linux/pci.h>
+#include <linux/pci-doe.h>
+#include <linux/workqueue.h>
+
+#define PCI_DOE_PROTOCOL_DISCOVERY 0
+
+#define PCI_DOE_BUSY_MAX_RETRIES 16

Left over from removed code.

drivers/pci/doe.c line 58

Quoted 1 line
drivers/pci/doe.c new file +1 -0
+static int pci_doe_wait(struct pci_doe_mb *doe_mb, unsigned long timeout)

This feels misnamed to me now. It's not waiting for the DOE, it's just sleeping unless we cancel. The actual poll / wait is handled outside this. pci_doe_sleep_unless_cancel() maybe?

Quoted 11 lines
drivers/pci/doe.c new file +10 -0
+{
+	if (wait_event_timeout(doe_mb->wq,
+			       test_bit(PCI_DOE_FLAG_CANCEL, &doe_mb->flags),
+			       timeout))
+		return -EIO;
+	return 0;
+}
 
+static int pci_doe_send_req(struct pci_doe_mb *doe_mb,
+			    struct pci_doe_task *task)
+{

drivers/pci/doe.c line 119

Quoted 10 lines
drivers/pci/doe.c new file +10 -0
+	struct pci_dev *pdev = doe_mb->pdev;
+	int offset = doe_mb->cap_offset;
+	u32 val;
+	int i;
+
+	/*
+	 * Check the DOE busy bit is not set. If it is set, this could indicate
+	 * someone other than Linux (e.g. firmware) is using the mailbox. Note
+	 * it is expected that firmware and OS will negotiate access rights via
+	 * an, as yet to be defined method.

trivial: an, as yet to be defined, method.

Quoted 14 lines
drivers/pci/doe.c new file +14 -0
+	 */
+	pci_read_config_dword(pdev, offset + PCI_DOE_STATUS, &val);
+	if (FIELD_GET(PCI_DOE_STATUS_BUSY, val))
+		return -EBUSY;
+
+	if (FIELD_GET(PCI_DOE_STATUS_ERROR, val))
+		return -EIO;
+
+	/* Write DOE Header */
+	val = FIELD_PREP(PCI_DOE_DATA_OBJECT_HEADER_1_VID, task->prot.vid) |
+		FIELD_PREP(PCI_DOE_DATA_OBJECT_HEADER_1_TYPE, task->prot.type);
+	pci_write_config_dword(pdev, offset + PCI_DOE_WRITE, val);
+	/* Length is 2 DW of header + length of payload in DW */
+	pci_write_config_dword(pdev, offset + PCI_DOE_WRITE,

drivers/pci/doe.c line 143

Quoted 10 lines
drivers/pci/doe.c new file +10 -0
+			       FIELD_PREP(PCI_DOE_DATA_OBJECT_HEADER_2_LENGTH,
+					  2 + task->request_pl_sz /
+						sizeof(u32)));
+	for (i = 0; i < task->request_pl_sz / sizeof(u32); i++)
+		pci_write_config_dword(pdev, offset + PCI_DOE_WRITE,
+				       task->request_pl[i]);
+
+	pci_doe_write_ctrl(doe_mb, PCI_DOE_CTRL_GO);
+
+	/* Request is sent - now wait for poll or IRQ */

Could drop the IRQ given not currently handling. Though I suppose it's correct documentation for this function, so can leave it if preferred. More than possible we'll get a follow up patch dropping it though from someone doing cleanup.

Quoted 3 lines
drivers/pci/doe.c new file +3 -0
+	return 0;
+}
+

...

Quoted 10 lines
drivers/pci/doe.c new file +10 -0
+
+static void doe_statemachine_work(struct work_struct *work)
+{
+	struct pci_doe_task *task = container_of(work, struct pci_doe_task,
+						 work);
+	struct pci_doe_mb *doe_mb = task->doe_mb;
+	struct pci_dev *pdev = doe_mb->pdev;
+	int offset = doe_mb->cap_offset;
+	unsigned long timeout_jiffies;
+	u32 val;

drivers/pci/doe.c line 255

Quoted 10 lines
drivers/pci/doe.c new file +10 -0
+	int rc;
+
+	if (test_bit(PCI_DOE_FLAG_DEAD, &doe_mb->flags)) {
+		signal_task_complete(task, -EIO);
+		return;
+	}
+
+	/* Send request */
+	rc = pci_doe_send_req(doe_mb, task);
+

nitpick, but blank line separating call from error handling reduces readability in my opinion.

Quoted 7 lines
drivers/pci/doe.c new file +7 -0
+	if (rc) {
+		/*
+		 * The specification does not provide any guidance on how to
+		 * resolve conflicting requests from other entities.
+		 * Furthermore, it is likely that busy will not be detected
+		 * most of the time.  Flag any detection of status busy with an
+		 * error.

drivers/pci/doe.c line 272

Quoted 10 lines
drivers/pci/doe.c new file +10 -0
+		 */
+		if (rc == -EBUSY)
+			dev_err_ratelimited(&pdev->dev, "[%x] busy detected; another entity is sending conflicting requests\n",
+					    offset);
+		signal_task_abort(task, rc);
+		return;
+	}
+
+	timeout_jiffies = jiffies + PCI_DOE_TIMEOUT;
+	rc = pci_doe_wait(doe_mb, PCI_DOE_POLL_INTERVAL);

What's this particular wait for? I think you can just move directly to checking if the response is ready.

Quoted 43 lines
drivers/pci/doe.c new file +42 -0
+	if (rc) {
+		signal_task_abort(task, rc);
+		return;
+	}
+
+	/* Poll for response */
+retry_resp:
+	pci_read_config_dword(pdev, offset + PCI_DOE_STATUS, &val);
+	if (FIELD_GET(PCI_DOE_STATUS_ERROR, val)) {
+		signal_task_abort(task, -EIO);
+		return;
+	}
+
+	if (!FIELD_GET(PCI_DOE_STATUS_DATA_OBJECT_READY, val)) {
+		if (time_after(jiffies, timeout_jiffies)) {
+			signal_task_abort(task, -EIO);
+			return;
+		}
+		rc = pci_doe_wait(doe_mb, PCI_DOE_POLL_INTERVAL);
+		if (rc) {
+			signal_task_abort(task, rc);
+			return;
+		}
+		goto retry_resp;
+	}
+
+	rc  = pci_doe_recv_resp(doe_mb, task);
+	if (rc < 0) {
+		signal_task_abort(task, rc);
+		return;
+	}
+
+	signal_task_complete(task, rc);
+}
+
 
+/**
+ * pci_doe_supports_prot() - Return if the DOE instance supports the given
+ *			     protocol
+ * @doe_mb: DOE mailbox capability to query
+ * @vid: Protocol Vendor ID
+ * @type: Protocol type
+ *

drivers/pci/doe.c

Quoted 10 lines
drivers/pci/doe.c new file +10 -0
+ * RETURNS: True if the DOE mailbox supports the protocol specified
+ */
+bool pci_doe_supports_prot(struct pci_doe_mb *doe_mb, u16 vid, u8 type)
+{
+	unsigned long index;
+	void *entry;
+
+	/* The discovery protocol must always be supported */
+	if (vid == PCI_VENDOR_ID_PCI_SIG && type == PCI_DOE_PROTOCOL_DISCOVERY)
+		return true;

Given how cheap this look up is now it's all in xarray, we could drop this 'optimization'. I'm fairly sure the discovery protocol will always be discovered (spec says it must be returned when calling itself as the fist protocol).

Quoted 23 lines
drivers/pci/doe.c new file +9 -0
+
+	xa_for_each(&doe_mb->prots, index, entry)
+		if (entry == pci_doe_xa_prot_entry(vid, type))
+			return true;
+
+	return false;
+}
+EXPORT_SYMBOL_GPL(pci_doe_supports_prot);
 
 
+EXPORT_SYMBOL_GPL(pci_doe_submit_task);
include/linux/pci-doe.h new file +6 -0
new file mode 100644
index 000000000000..c77f6258c996
@@ -0,0 +1,79 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Data Object Exchange
+ *	PCIe r6.0, sec 6.30 DOE
+ *
+ * Copyright (C) 2021 Huawei

include/linux/pci-doe.h line 16

Quoted 10 lines
include/linux/pci-doe.h new file +10 -0
+ *     Jonathan Cameron <[email protected]>
+ *
+ * Copyright (C) 2022 Intel Corporation
+ *	Ira Weiny <[email protected]>
+ */
+
+#ifndef LINUX_PCI_DOE_H
+#define LINUX_PCI_DOE_H
+
+#include <linux/completion.h>

include not needed any more.

Thanks,

Jonathan

Quoted 63 lines
include/linux/pci-doe.h new file +63 -0
+
+struct pci_doe_protocol {
+	u16 vid;
+	u8 type;
+};
+
+struct pci_doe_mb;
+
+/**
+ * struct pci_doe_task - represents a single query/response
+ *
+ * @prot: DOE Protocol
+ * @request_pl: The request payload
+ * @request_pl_sz: Size of the request payload (bytes)
+ * @response_pl: The response payload
+ * @response_pl_sz: Size of the response payload (bytes)
+ * @rv: Return value.  Length of received response or error (bytes)
+ * @complete: Called when task is complete
+ * @private: Private data for the consumer
+ * @work: Used internally by the mailbox
+ * @doe_mb: Used internally by the mailbox
+ *
+ * The payload sizes and rv are specified in bytes with the following
+ * restrictions concerning the protocol.
+ *
+ *	1) The request_pl_sz must be a multiple of double words (4 bytes)
+ *	2) The response_pl_sz must be >= a single double word (4 bytes)
+ *	3) rv is returned as bytes but it will be a multiple of double words
+ *
+ * NOTE there is no need for the caller to initialize work or doe_mb.
+ */
+struct pci_doe_task {
+	struct pci_doe_protocol prot;
+	u32 *request_pl;
+	size_t request_pl_sz;
+	u32 *response_pl;
+	size_t response_pl_sz;
+	int rv;
+	void (*complete)(struct pci_doe_task *task);
+	void *private;
+
+	/* No need for the user to initialize these fields */
+	struct work_struct work;
+	struct pci_doe_mb *doe_mb;
+};
+
+/**
+ * pci_doe_for_each_off - Iterate each DOE capability
+ * @pdev: struct pci_dev to iterate
+ * @off: u16 of config space offset of each mailbox capability found
+ */
+#define pci_doe_for_each_off(pdev, off) \
+	for (off = pci_find_next_ext_capability(pdev, off, \
+					PCI_EXT_CAP_ID_DOE); \
+		off > 0; \
+		off = pci_find_next_ext_capability(pdev, off, \
+					PCI_EXT_CAP_ID_DOE))
+
+struct pci_doe_mb *pcim_doe_create_mb(struct pci_dev *pdev, u16 cap_offset);
+bool pci_doe_supports_prot(struct pci_doe_mb *doe_mb, u16 vid, u8 type);
+int pci_doe_submit_task(struct pci_doe_mb *doe_mb, struct pci_doe_task *task);
+
+#endif
author
#5 lore raw

Re: [PATCH V14 3/7] PCI/DOE: Add DOE mailbox support functions

Quoted 65 lines · Jonathan Cameron
Quoted 14 lines · On Thu, 14 Jul 2022 20:04:20 -0700 [email protected] wrote:

From: Jonathan Cameron <[email protected]>

Introduced in a PCIe r6.0, sec 6.30, DOE provides a config space based mailbox with standard protocol discovery. Each mailbox is accessed through a DOE Extended Capability.

Each DOE mailbox must support the DOE discovery protocol in addition to any number of additional protocols.

Define core PCIe functionality to manage a single PCIe DOE mailbox at a defined config space offset. Functionality includes iterating, creating, query of supported protocol, and task submission. Destruction of the mailboxes is device managed.

Quoted 7 lines

Hi Ira,

Thanks for persisting with this!

So, I think this works, but there is at least one 'sleep' I can't see a purpose for. I think it's just a left over from refactoring.

A few other more trivial things inline.

Thanks,

Jonathan

Quoted 27 lines
Quoted 2 lines

# Endpoint library must be initialized before its users

obj-$(CONFIG_PCI_ENDPOINT) += endpoint/

drivers/pci/doe.c new file +18 -0
new file mode 100644
index 000000000000..12c3762be22f
@@ -0,0 +1,546 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Data Object Exchange
+ *	PCIe r6.0, sec 6.30 DOE
+ *
+ * Copyright (C) 2021 Huawei
+ *	Jonathan Cameron <[email protected]>
+ *
+ * Copyright (C) 2022 Intel Corporation
+ *	Ira Weiny <[email protected]>
+ */
+
+#define dev_fmt(fmt) "DOE: " fmt
+
+#include <linux/bitfield.h>
+#include <linux/delay.h>
+#include <linux/jiffies.h>
+#include <linux/mutex.h>
Quoted 8 lines

drivers/pci/doe.c line 25

Quoted 7 lines
drivers/pci/doe.c new file +7 -0
+#include <linux/pci.h>
+#include <linux/pci-doe.h>
+#include <linux/workqueue.h>
+
+#define PCI_DOE_PROTOCOL_DISCOVERY 0
+
+#define PCI_DOE_BUSY_MAX_RETRIES 16

Left over from removed code.

I think Dan may have taken these. If so I'll send a clean up. If not I can spin. Let me check.

Quoted 6 lines

drivers/pci/doe.c line 58

Quoted 2 lines
drivers/pci/doe.c new file +1 -0
+static int pci_doe_wait(struct pci_doe_mb *doe_mb, unsigned long timeout)

This feels misnamed to me now. It's not waiting for the DOE, it's just sleeping unless we cancel. The actual poll / wait is handled outside this. pci_doe_sleep_unless_cancel() maybe?

It is waiting a timeout period _or_ checking if we are canceled.

So I think it is correct for the common case.

Quoted 9 lines
Quoted 8 lines
drivers/pci/doe.c new file +7 -0
+{
+	if (wait_event_timeout(doe_mb->wq,
+			       test_bit(PCI_DOE_FLAG_CANCEL, &doe_mb->flags),
+			       timeout))
+		return -EIO;
+	return 0;
+}
Quoted 15 lines

drivers/pci/doe.c line 119

Quoted 13 lines
drivers/pci/doe.c new file +13 -0
+static int pci_doe_send_req(struct pci_doe_mb *doe_mb,
+			    struct pci_doe_task *task)
+{
+	struct pci_dev *pdev = doe_mb->pdev;
+	int offset = doe_mb->cap_offset;
+	u32 val;
+	int i;
+
+	/*
+	 * Check the DOE busy bit is not set. If it is set, this could indicate
+	 * someone other than Linux (e.g. firmware) is using the mailbox. Note
+	 * it is expected that firmware and OS will negotiate access rights via
+	 * an, as yet to be defined method.

trivial: an, as yet to be defined, method.

I'll send a follow on patch for this.

Quoted 22 lines
Quoted 22 lines
drivers/pci/doe.c new file +21 -0
+	 */
+	pci_read_config_dword(pdev, offset + PCI_DOE_STATUS, &val);
+	if (FIELD_GET(PCI_DOE_STATUS_BUSY, val))
+		return -EBUSY;
+
+	if (FIELD_GET(PCI_DOE_STATUS_ERROR, val))
+		return -EIO;
+
+	/* Write DOE Header */
+	val = FIELD_PREP(PCI_DOE_DATA_OBJECT_HEADER_1_VID, task->prot.vid) |
+		FIELD_PREP(PCI_DOE_DATA_OBJECT_HEADER_1_TYPE, task->prot.type);
+	pci_write_config_dword(pdev, offset + PCI_DOE_WRITE, val);
+	/* Length is 2 DW of header + length of payload in DW */
+	pci_write_config_dword(pdev, offset + PCI_DOE_WRITE,
+			       FIELD_PREP(PCI_DOE_DATA_OBJECT_HEADER_2_LENGTH,
+					  2 + task->request_pl_sz /
+						sizeof(u32)));
+	for (i = 0; i < task->request_pl_sz / sizeof(u32); i++)
+		pci_write_config_dword(pdev, offset + PCI_DOE_WRITE,
+				       task->request_pl[i]);
+
Quoted 8 lines

drivers/pci/doe.c line 143

Quoted 3 lines
drivers/pci/doe.c new file +3 -0
+	pci_doe_write_ctrl(doe_mb, PCI_DOE_CTRL_GO);
+
+	/* Request is sent - now wait for poll or IRQ */

Could drop the IRQ given not currently handling. Though I suppose it's correct documentation for this function, so can leave it if preferred. More than possible we'll get a follow up patch dropping it though from someone doing cleanup.

I send a follow on clean patch for this as well. I suspect IRQ may never make it in. But for sure it will not for this cycle. Best to have the comment correct for the final 5.20.

Quoted 21 lines
Quoted 4 lines
drivers/pci/doe.c new file +3 -0
+	return 0;
+}
+

...

Quoted 14 lines
drivers/pci/doe.c new file +14 -0
+
+static void doe_statemachine_work(struct work_struct *work)
+{
+	struct pci_doe_task *task = container_of(work, struct pci_doe_task,
+						 work);
+	struct pci_doe_mb *doe_mb = task->doe_mb;
+	struct pci_dev *pdev = doe_mb->pdev;
+	int offset = doe_mb->cap_offset;
+	unsigned long timeout_jiffies;
+	u32 val;
+	int rc;
+
+	if (test_bit(PCI_DOE_FLAG_DEAD, &doe_mb->flags)) {
+		signal_task_complete(task, -EIO);
Quoted 8 lines

drivers/pci/doe.c line 255

Quoted 6 lines
drivers/pci/doe.c new file +6 -0
+		return;
+	}
+
+	/* Send request */
+	rc = pci_doe_send_req(doe_mb, task);
+

nitpick, but blank line separating call from error handling reduces readability in my opinion.

:-/

I'm not sure why I did this. More clean ups.

Quoted 13 lines
Quoted 13 lines
drivers/pci/doe.c new file +12 -0
+	if (rc) {
+		/*
+		 * The specification does not provide any guidance on how to
+		 * resolve conflicting requests from other entities.
+		 * Furthermore, it is likely that busy will not be detected
+		 * most of the time.  Flag any detection of status busy with an
+		 * error.
+		 */
+		if (rc == -EBUSY)
+			dev_err_ratelimited(&pdev->dev, "[%x] busy detected; another entity is sending conflicting requests\n",
+					    offset);
+		signal_task_abort(task, rc);
Quoted 8 lines

drivers/pci/doe.c line 272

Quoted 5 lines
drivers/pci/doe.c new file +5 -0
+		return;
+	}
+
+	timeout_jiffies = jiffies + PCI_DOE_TIMEOUT;
+	rc = pci_doe_wait(doe_mb, PCI_DOE_POLL_INTERVAL);

What's this particular wait for? I think you can just move directly to checking if the response is ready.

We could but I assume it will take at least some time to process the request. So it seemed best to wait and then check.

But of course we all know that also used to wait for an IRQ as an option. :-/

I'm really on the fence here because I don't think it really matters. We are sleeping so it does not really affect the system much and this is not a performance path. If we were spinning I would agree with you.

Quoted 51 lines
Quoted 51 lines
drivers/pci/doe.c new file +49 -0
+	if (rc) {
+		signal_task_abort(task, rc);
+		return;
+	}
+
+	/* Poll for response */
+retry_resp:
+	pci_read_config_dword(pdev, offset + PCI_DOE_STATUS, &val);
+	if (FIELD_GET(PCI_DOE_STATUS_ERROR, val)) {
+		signal_task_abort(task, -EIO);
+		return;
+	}
+
+	if (!FIELD_GET(PCI_DOE_STATUS_DATA_OBJECT_READY, val)) {
+		if (time_after(jiffies, timeout_jiffies)) {
+			signal_task_abort(task, -EIO);
+			return;
+		}
+		rc = pci_doe_wait(doe_mb, PCI_DOE_POLL_INTERVAL);
+		if (rc) {
+			signal_task_abort(task, rc);
+			return;
+		}
+		goto retry_resp;
+	}
+
+	rc  = pci_doe_recv_resp(doe_mb, task);
+	if (rc < 0) {
+		signal_task_abort(task, rc);
+		return;
+	}
+
+	signal_task_complete(task, rc);
+}
+
 
+/**
+ * pci_doe_supports_prot() - Return if the DOE instance supports the given
+ *			     protocol
+ * @doe_mb: DOE mailbox capability to query
+ * @vid: Protocol Vendor ID
+ * @type: Protocol type
+ *
+ * RETURNS: True if the DOE mailbox supports the protocol specified
+ */
+bool pci_doe_supports_prot(struct pci_doe_mb *doe_mb, u16 vid, u8 type)
+{
+	unsigned long index;
+	void *entry;
+
Quoted 8 lines

drivers/pci/doe.c line 498

Quoted 3 lines
drivers/pci/doe.c new file +3 -0
+	/* The discovery protocol must always be supported */
+	if (vid == PCI_VENDOR_ID_PCI_SIG && type == PCI_DOE_PROTOCOL_DISCOVERY)
+		return true;

Given how cheap this look up is now it's all in xarray, we could drop this 'optimization'. I'm fairly sure the discovery protocol will always be discovered (spec says it must be returned when calling itself as the fist protocol).

No we can't because this is called before the xarray is populated with the discovery protocol. This was actually added not as an optimization but to allow the discovery protocol to run through the common query path.

Quoted 28 lines
Quoted 28 lines
drivers/pci/doe.c new file +9 -0
+
+	xa_for_each(&doe_mb->prots, index, entry)
+		if (entry == pci_doe_xa_prot_entry(vid, type))
+			return true;
+
+	return false;
+}
+EXPORT_SYMBOL_GPL(pci_doe_supports_prot);
 
 
+EXPORT_SYMBOL_GPL(pci_doe_submit_task);
include/linux/pci-doe.h new file +10 -0
new file mode 100644
index 000000000000..c77f6258c996
@@ -0,0 +1,79 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Data Object Exchange
+ *	PCIe r6.0, sec 6.30 DOE
+ *
+ * Copyright (C) 2021 Huawei
+ *     Jonathan Cameron <[email protected]>
+ *
+ * Copyright (C) 2022 Intel Corporation
+ *	Ira Weiny <[email protected]>
Quoted 8 lines

include/linux/pci-doe.h line 16

Quoted 6 lines
include/linux/pci-doe.h new file +6 -0
+ */
+
+#ifndef LINUX_PCI_DOE_H
+#define LINUX_PCI_DOE_H
+
+#include <linux/completion.h>

include not needed any more.

Will fix,
Ira

Quoted 69 lines

Thanks,

Jonathan

Quoted 63 lines
include/linux/pci-doe.h new file +63 -0
+
+struct pci_doe_protocol {
+	u16 vid;
+	u8 type;
+};
+
+struct pci_doe_mb;
+
+/**
+ * struct pci_doe_task - represents a single query/response
+ *
+ * @prot: DOE Protocol
+ * @request_pl: The request payload
+ * @request_pl_sz: Size of the request payload (bytes)
+ * @response_pl: The response payload
+ * @response_pl_sz: Size of the response payload (bytes)
+ * @rv: Return value.  Length of received response or error (bytes)
+ * @complete: Called when task is complete
+ * @private: Private data for the consumer
+ * @work: Used internally by the mailbox
+ * @doe_mb: Used internally by the mailbox
+ *
+ * The payload sizes and rv are specified in bytes with the following
+ * restrictions concerning the protocol.
+ *
+ *	1) The request_pl_sz must be a multiple of double words (4 bytes)
+ *	2) The response_pl_sz must be >= a single double word (4 bytes)
+ *	3) rv is returned as bytes but it will be a multiple of double words
+ *
+ * NOTE there is no need for the caller to initialize work or doe_mb.
+ */
+struct pci_doe_task {
+	struct pci_doe_protocol prot;
+	u32 *request_pl;
+	size_t request_pl_sz;
+	u32 *response_pl;
+	size_t response_pl_sz;
+	int rv;
+	void (*complete)(struct pci_doe_task *task);
+	void *private;
+
+	/* No need for the user to initialize these fields */
+	struct work_struct work;
+	struct pci_doe_mb *doe_mb;
+};
+
+/**
+ * pci_doe_for_each_off - Iterate each DOE capability
+ * @pdev: struct pci_dev to iterate
+ * @off: u16 of config space offset of each mailbox capability found
+ */
+#define pci_doe_for_each_off(pdev, off) \
+	for (off = pci_find_next_ext_capability(pdev, off, \
+					PCI_EXT_CAP_ID_DOE); \
+		off > 0; \
+		off = pci_find_next_ext_capability(pdev, off, \
+					PCI_EXT_CAP_ID_DOE))
+
+struct pci_doe_mb *pcim_doe_create_mb(struct pci_dev *pdev, u16 cap_offset);
+bool pci_doe_supports_prot(struct pci_doe_mb *doe_mb, u16 vid, u8 type);
+int pci_doe_submit_task(struct pci_doe_mb *doe_mb, struct pci_doe_task *task);
+
+#endif
author
#6 lore raw

Re: [PATCH V14 3/7] PCI/DOE: Add DOE mailbox support functions

Quoted 1 line · Ira

On Tue, Jul 19, 2022 at 05:35:53PM +0100, Jonathan Cameron wrote:

[snip]

Quoted 8 lines
Quoted 8 lines

Hi Ira,

Thanks for persisting with this!

So, I think this works, but there is at least one 'sleep' I can't see a purpose for. I think it's just a left over from refactoring.

A few other more trivial things inline.

[snip]

Quoted 6 lines
Quoted 3 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+#define PCI_DOE_BUSY_MAX_RETRIES 16

Left over from removed code.

I think Dan may have taken these. If so I'll send a clean up. If not I can spin. Let me check.

I'm spinning a v15 of this patch.

[snip]

Quoted 23 lines
Quoted 14 lines
Quoted 14 lines
drivers/pci/doe.c new file +12 -0
+	if (rc) {
+		/*
+		 * The specification does not provide any guidance on how to
+		 * resolve conflicting requests from other entities.
+		 * Furthermore, it is likely that busy will not be detected
+		 * most of the time.  Flag any detection of status busy with an
+		 * error.
+		 */
+		if (rc == -EBUSY)
+			dev_err_ratelimited(&pdev->dev, "[%x] busy detected; another entity is sending conflicting requests\n",
+					    offset);
+		signal_task_abort(task, rc);
Quoted 8 lines

drivers/pci/doe.c line 272

Quoted 5 lines
drivers/pci/doe.c new file +5 -0
+		return;
+	}
+
+	timeout_jiffies = jiffies + PCI_DOE_TIMEOUT;
+	rc = pci_doe_wait(doe_mb, PCI_DOE_POLL_INTERVAL);

What's this particular wait for? I think you can just move directly to checking if the response is ready.

Quoted 8 lines

We could but I assume it will take at least some time to process the request. So it seemed best to wait and then check.

But of course we all know that also used to wait for an IRQ as an option. :-/

I'm really on the fence here because I don't think it really matters. We are sleeping so it does not really affect the system much and this is not a performance path. If we were spinning I would agree with you.

I've deferred to your expertise here and removed the extra wait.

Ira

#7 lore raw

Re: [PATCH V14 3/7] PCI/DOE: Add DOE mailbox support functions

In reply to Ira Weiny
Quoted 75 lines · On Tue, 19 Jul 2022 12:16:06 -0700 Ira Weiny <[email protected]> wrote:
Quoted 65 lines · Jonathan Cameron
Quoted 14 lines · On Thu, 14 Jul 2022 20:04:20 -0700 [email protected] wrote:

From: Jonathan Cameron <[email protected]>

Introduced in a PCIe r6.0, sec 6.30, DOE provides a config space based mailbox with standard protocol discovery. Each mailbox is accessed through a DOE Extended Capability.

Each DOE mailbox must support the DOE discovery protocol in addition to any number of additional protocols.

Define core PCIe functionality to manage a single PCIe DOE mailbox at a defined config space offset. Functionality includes iterating, creating, query of supported protocol, and task submission. Destruction of the mailboxes is device managed.

Quoted 7 lines

Hi Ira,

Thanks for persisting with this!

So, I think this works, but there is at least one 'sleep' I can't see a purpose for. I think it's just a left over from refactoring.

A few other more trivial things inline.

Thanks,

Jonathan

Quoted 27 lines
Quoted 2 lines

# Endpoint library must be initialized before its users

obj-$(CONFIG_PCI_ENDPOINT) += endpoint/

drivers/pci/doe.c new file +18 -0
new file mode 100644
index 000000000000..12c3762be22f
@@ -0,0 +1,546 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Data Object Exchange
+ *	PCIe r6.0, sec 6.30 DOE
+ *
+ * Copyright (C) 2021 Huawei
+ *	Jonathan Cameron <[email protected]>
+ *
+ * Copyright (C) 2022 Intel Corporation
+ *	Ira Weiny <[email protected]>
+ */
+
+#define dev_fmt(fmt) "DOE: " fmt
+
+#include <linux/bitfield.h>
+#include <linux/delay.h>
+#include <linux/jiffies.h>
+#include <linux/mutex.h>
Quoted 8 lines

drivers/pci/doe.c line 25

Quoted 7 lines
drivers/pci/doe.c new file +7 -0
+#include <linux/pci.h>
+#include <linux/pci-doe.h>
+#include <linux/workqueue.h>
+
+#define PCI_DOE_PROTOCOL_DISCOVERY 0
+
+#define PCI_DOE_BUSY_MAX_RETRIES 16  

Left over from removed code.

Quoted 3 lines

I think Dan may have taken these. If so I'll send a clean up. If not I can spin. Let me check.

Absolutely. All tiny improvements, so fine to go in next cycle given late timing.

Quoted 23 lines
Quoted 14 lines
Quoted 14 lines
drivers/pci/doe.c new file +14 -0
+/**
+ * pci_doe_supports_prot() - Return if the DOE instance supports the given
+ *			     protocol
+ * @doe_mb: DOE mailbox capability to query
+ * @vid: Protocol Vendor ID
+ * @type: Protocol type
+ *
+ * RETURNS: True if the DOE mailbox supports the protocol specified
+ */
+bool pci_doe_supports_prot(struct pci_doe_mb *doe_mb, u16 vid, u8 type)
+{
+	unsigned long index;
+	void *entry;
+
Quoted 8 lines

drivers/pci/doe.c line 498

Quoted 3 lines
drivers/pci/doe.c new file +3 -0
+	/* The discovery protocol must always be supported */
+	if (vid == PCI_VENDOR_ID_PCI_SIG && type == PCI_DOE_PROTOCOL_DISCOVERY)
+		return true;  

Given how cheap this look up is now it's all in xarray, we could drop this 'optimization'. I'm fairly sure the discovery protocol will always be discovered (spec says it must be returned when calling itself as the fist protocol).

Quoted 4 lines

No we can't because this is called before the xarray is populated with the discovery protocol. This was actually added not as an optimization but to allow the discovery protocol to run through the common query path.

Ah. I was too lazy to check if that was the case :)

Jonathan

#8 lore raw

PATCH v14 4/7 cxl/pci: Create PCI DOE mailbox's for memory devices

In reply to [email protected]

From: Ira Weiny <[email protected]>

DOE mailbox objects will be needed for various mailbox communications with each memory device.

Iterate each DOE mailbox capability and create PCI DOE mailbox objects as found.

It is not anticipated that this is the final resting place for the iteration of the DOE devices. The support of switch ports will drive this code into the PCIe side. In this imagined architecture the CXL port driver would then query into the PCI device for the DOE mailbox array.

For now creating the mailboxes in the CXL port is good enough for the endpoints. Later PCIe ports will need to support this to support switch ports more generically.

Cc Dan Williams <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Lukas Wunner <[email protected]>

---
Changes from V13:
Dan:
s/pci_err/dev_err
s/pci_dbg/dev_dbg
Move xa_init() into devm_cxl_pci_create_doe()

Changes from V12:
remove irq param from CXL
Jonathan:
remove xa local variable
clarify MB creation as best effort
But ensure pci_err() if they fail Check devm_add_action() return for failure
Davidlohr and Jonathan:
Return error ...

Changes from V11:
Drop review from: Ben Widawsky <[email protected]> Remove irq code for now
Adjust for pci_doe_get_int_msg_num() Adjust for pcim_doe_create_mb()
(No longer need to handle the destroy.)
Use xarray for DOE mailbox array

Changes from V9:
Bug fix: ensure DOE mailboxes are iterated before memdev add Ben Widawsky
Set use_irq to false and just return on error. Don't return a value from devm_cxl_pci_create_doe() Skip allocating doe_mb array if there are no mailboxes Skip requesting irqs if none found.
Ben/Jonathan Cameron
s/num_irqs/max_irqs

Changes from V8:
Move PCI_DOE selection to CXL_BUS to support future patches which move queries into the port code.
Remove Auxiliary device arch
Squash the functionality of the auxiliary driver into this patch.
Split out the irq handling a bit.

Changes from V7:
Minor code clean ups
Rebased on cxl-pending

Changes from V6:
Move all the auxiliary device stuff to the CXL layer

Changes from V5:
Split the CXL specific stuff off from the PCI DOE create auxiliary device code.

---
 drivers/cxl/Kconfig  |  1 +
 drivers/cxl/cxlmem.h |  3 +++
 drivers/cxl/pci.c    | 44 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 48 insertions(+)
drivers/cxl/Kconfig +1 -0
index f64e3984689f..7adaaf80b302 100644
@@ -2,6 +2,7 @@
 menuconfig CXL_BUS
 	tristate "CXL (Compute Express Link) Devices Support"
 	depends on PCI
+	select PCI_DOE
 	help
 	  CXL is a bus that is electrically compatible with PCI Express, but
 	  layers three protocols on that signalling (CXL.io, CXL.cache, and
drivers/cxl/cxlmem.h +3 -0
index c6d6f57856cc..bfa2eaf649a9 100644
@@ -192,6 +192,7 @@ struct cxl_endpoint_dvsec_info {
  * @component_reg_phys: register base of component registers
  * @info: Cached DVSEC information about the device.
  * @serial: PCIe Device Serial Number
+ * @doe_mbs: PCI DOE mailbox array
  * @mbox_send: @dev specific transport for transmitting mailbox commands
  *
  * See section 8.2.9.5.2 Capacity Configuration and Label Storage for
@@ -226,6 +227,8 @@ struct cxl_dev_state {
 	resource_size_t component_reg_phys;
 	u64 serial;
 
+	struct xarray doe_mbs;
+
 	int (*mbox_send)(struct cxl_dev_state *cxlds, struct cxl_mbox_cmd *cmd);
 };
 
drivers/cxl/pci.c +44 -0

62 lines not on this page · raw message

Signature · 1 line
2.35.3
Reviewed-by on v14 4/7
#9 lore raw

Re: [PATCH V14 4/7] cxl/pci: Create PCI DOE mailbox's for memory devices

Quoted 14 lines · On Thu, 14 Jul 2022 20:04:21 -0700 [email protected] wrote:

From: Ira Weiny <[email protected]>

DOE mailbox objects will be needed for various mailbox communications with each memory device.

Iterate each DOE mailbox capability and create PCI DOE mailbox objects as found.

It is not anticipated that this is the final resting place for the iteration of the DOE devices. The support of switch ports will drive this code into the PCIe side. In this imagined architecture the CXL port driver would then query into the PCI device for the DOE mailbox array.

Quoted 8 lines

For now creating the mailboxes in the CXL port is good enough for the endpoints. Later PCIe ports will need to support this to support switch ports more generically.

Cc Dan Williams <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Lukas Wunner <[email protected]>

LGTM

#10 lore raw

PATCH v14 5/7 driver-core: Introduce BIN_ATTR_ADMIN_{RO,RW}

In reply to [email protected]

From: Ira Weiny <[email protected]>

Many binary attributes need to limit access to CAP_SYS_ADMIN only; ie many binary attributes specify is_visible with 0400 or 0600.

Make setting the permissions of such attributes more explicit by defining BIN_ATTR_ADMIN_{RO,RW}.

---
Changes from V12:
Pick up review tag
Bjorn:
NOTE: this has a lot of similarities to
https://lore.kernel.org/all/[email protected]/
I'm not sure why that patch was not picked up. But I've added Krzysztof as a suggested by if that is ok?

Changes from V11:
New Patch

---
 include/linux/sysfs.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
include/linux/sysfs.h +16 -0
index e3f1e8ac1f85..fd3fe5c8c17f 100644
@@ -235,6 +235,22 @@ struct bin_attribute bin_attr_##_name = __BIN_ATTR_WO(_name, _size)
 #define BIN_ATTR_RW(_name, _size)					\
 struct bin_attribute bin_attr_##_name = __BIN_ATTR_RW(_name, _size)
 
+
+#define __BIN_ATTR_ADMIN_RO(_name, _size) {					\
+	.attr	= { .name = __stringify(_name), .mode = 0400 },		\
+	.read	= _name##_read,						\
+	.size	= _size,						\
+}
+
+#define __BIN_ATTR_ADMIN_RW(_name, _size)					\
+	__BIN_ATTR(_name, 0600, _name##_read, _name##_write, _size)
+
+#define BIN_ATTR_ADMIN_RO(_name, _size)					\
+struct bin_attribute bin_attr_##_name = __BIN_ATTR_ADMIN_RO(_name, _size)
+
+#define BIN_ATTR_ADMIN_RW(_name, _size)					\
+struct bin_attribute bin_attr_##_name = __BIN_ATTR_ADMIN_RW(_name, _size)
+
 struct sysfs_ops {
 	ssize_t	(*show)(struct kobject *, struct attribute *, char *);
 	ssize_t	(*store)(struct kobject *, struct attribute *, const char *, size_t);
Signature · 1 line
2.35.3
Reviewed-by on v14 5/7
#11 lore raw

Re: [PATCH V14 5/7] driver-core: Introduce BIN_ATTR_ADMIN_{RO,RW}

Quoted 5 lines · On Thu, 14 Jul 2022 20:04:22 -0700 [email protected] wrote:

From: Ira Weiny <[email protected]>

Many binary attributes need to limit access to CAP_SYS_ADMIN only; ie many binary attributes specify is_visible with 0400 or 0600.

Quoted 8 lines

Make setting the permissions of such attributes more explicit by defining BIN_ATTR_ADMIN_{RO,RW}.

Seems sensible.

Quoted 43 lines

---
Changes from V12:
Pick up review tag
Bjorn:
NOTE: this has a lot of similarities to
https://lore.kernel.org/all/[email protected]/
I'm not sure why that patch was not picked up. But I've added Krzysztof as a suggested by if that is ok?

Changes from V11:
New Patch

---
 include/linux/sysfs.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
include/linux/sysfs.h +16 -0
index e3f1e8ac1f85..fd3fe5c8c17f 100644
@@ -235,6 +235,22 @@ struct bin_attribute bin_attr_##_name = __BIN_ATTR_WO(_name, _size)
 #define BIN_ATTR_RW(_name, _size)					\
 struct bin_attribute bin_attr_##_name = __BIN_ATTR_RW(_name, _size)
 
+
+#define __BIN_ATTR_ADMIN_RO(_name, _size) {					\
+	.attr	= { .name = __stringify(_name), .mode = 0400 },		\
+	.read	= _name##_read,						\
+	.size	= _size,						\
+}
+
+#define __BIN_ATTR_ADMIN_RW(_name, _size)					\
+	__BIN_ATTR(_name, 0600, _name##_read, _name##_write, _size)
+
+#define BIN_ATTR_ADMIN_RO(_name, _size)					\
+struct bin_attribute bin_attr_##_name = __BIN_ATTR_ADMIN_RO(_name, _size)
+
+#define BIN_ATTR_ADMIN_RW(_name, _size)					\
+struct bin_attribute bin_attr_##_name = __BIN_ATTR_ADMIN_RW(_name, _size)
+
 struct sysfs_ops {
 	ssize_t	(*show)(struct kobject *, struct attribute *, char *);
 	ssize_t	(*store)(struct kobject *, struct attribute *, const char *, size_t);
#12 lore raw

PATCH v14 6/7 cxl/port: Read CDAT table

In reply to [email protected]

From: Ira Weiny <[email protected]>

The OS will need CDAT data from CXL devices to properly set up interleave sets. Currently this is supported through a DOE mailbox which supports CDAT.

Search the DOE mailboxes available, query CDAT data, and cache the data for later parsing.

Provide a sysfs binary attribute to allow dumping of the CDAT.

Binary dumping is modeled on /sys/firmware/ACPI/tables/

The ability to dump this table will be very useful for emulation of real devices once they become available as QEMU CXL type 3 device emulation will be able to load this file in.

This does not support table updates at runtime. It will always provide whatever was there when first cached. Handling of table updates can be implemented later.

Finally create a complete list of CDAT defines within cdat.h for code wishing to decode the CDAT table.

---
Changes from V13:
Dan:
Add entry in Documentation/ABI/testing/sysfs-bus-cxl Remove table parsing defines. s/cdat_sup/cdat_available s/cdat_mb/cdat_doe/
Don't check endpoint in find_cdat_doe() Create CDAT_DOE_TASK macro

Changes from V12:
Fix checking for task.rv for errors
Ensure no over run of non-DW aligned buffer length's

Changes from V11:
Adjust for the use of DOE mailbox xarray
Dan Williams:
Remove unnecessary get/put device
Use new BIN_ATTR_ADMIN_RO macro
Flag that CDAT was supported
If there is a read error then the CDAT sysfs will return a 0 length entry

Changes from V10:
Ben Widawsky
Failure to find CDAT should be a debug message not error Remove reference to cdat_mb from the port object
Dropped [PATCH V10 5/9] cxl/port: Find a DOE mailbox which supports
CDAT
Iterate the mailboxes for the CDAT one each time.
Define CXL_DOE_TABLE_ACCESS_LAST_ENTRY and add comment about
it's use.

Changes from V9:
Add debugging output
Jonathan Cameron
Move read_cdat to port probe by using dev_groups for the sysfs attributes. This avoids issues with using devm before the driver is loaded while making sure the CDAT binary is available.

Changes from V8:
Fix length print format
Incorporate feedback from Jonathan
Move all this to cxl_port which can help support switches when the time comes.

Changes from V6:
Fix issue with devm use
Move cached cdat data to cxl_dev_state
Use new pci_doe_submit_task()
Ensure the aux driver is locked while processing tasks Rebased on cxl-pending

Changes from V5:
Add proper guards around cdat.h
Split out finding the CDAT DOE mailbox
Use cxl_cdat to group CDAT data together
Adjust to use auxiliary_find_device() to find the DOE device
which supplies the CDAT protocol.
Rebased to latest
Remove dev_dbg(length)
Remove unneeded DOE Table access defines
Move CXL_DOE_PROTOCOL_TABLE_ACCESS define into this patch where
it is used

Changes from V4:
Split this into it's own patch
Rearchitect this such that the memdev driver calls into the DOE driver via the cxl_mem state object. This allows CDAT data to come from any type of cxl_mem object not just PCI DOE. Rebase on new struct cxl_dev_state

---
 Documentation/ABI/testing/sysfs-bus-cxl |  10 ++
 drivers/cxl/cdat.h                      |  61 +++++++++
 drivers/cxl/core/pci.c                  | 169 ++++++++++++++++++++++++
 drivers/cxl/cxl.h                       |   5 +
 drivers/cxl/cxlpci.h                    |   1 +
 drivers/cxl/port.c                      |  54 ++++++++
 6 files changed, 300 insertions(+)
 create mode 100644 drivers/cxl/cdat.h
Documentation/ABI/testing/sysfs-bus-cxl +10 -0
index 1fd5984b6158..6fb6459466f8 100644
@@ -164,3 +164,13 @@ Description:
 		expander memory (type-3). The 'target_type' attribute indicates
 		the current setting which may dynamically change based on what
 		memory regions are activated in this decode hierarchy.
+
+What:		/sys/bus/cxl/devices/endpointX/CDAT/cdat
+Date:		July, 2022
+KernelVersion:	v5.19
+Contact:	[email protected]
+Description:
+		(RO) If this sysfs entry is not present no DOE mailbox was
+		found to support CDAT data.  If it is present and the length of
+		the data is 0 reading the CDAT data failed.  Otherwise the CDAT
+		data is reported.
drivers/cxl/cdat.h new file +61 -0

61 lines not on this page · raw message

drivers/cxl/core/pci.c +169 -0

182 lines not on this page · raw message

drivers/cxl/cxl.h +5 -0
index 570bd9f8141b..64ea8c80b917 100644
@@ -8,6 +8,7 @@
 #include <linux/bitfield.h>
 #include <linux/bitops.h>
 #include <linux/io.h>
+#include "cdat.h"
 
 /**
  * DOC: cxl objects
@@ -289,6 +290,8 @@ struct cxl_nvdimm {
  * @component_reg_phys: component register capability base address (optional)
  * @dead: last ep has been removed, force port re-creation
  * @depth: How deep this port is relative to the root. depth 0 is the root.
+ * @cdat: Cached CDAT data
+ * @cdat_available: Should a CDAT attribute be available in sysfs
  */
 struct cxl_port {
 	struct device dev;
@@ -301,6 +304,8 @@ struct cxl_port {
 	resource_size_t component_reg_phys;
 	bool dead;
 	unsigned int depth;
+	struct cxl_cdat cdat;
+	bool cdat_available;
 };
 
 /**
drivers/cxl/cxlpci.h +1 -0
index fce1c11729c2..eec597dbe763 100644
@@ -74,4 +74,5 @@ static inline resource_size_t cxl_regmap_to_base(struct pci_dev *pdev,
 int devm_cxl_port_enumerate_dports(struct cxl_port *port);
 struct cxl_dev_state;
 int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm);
+void read_cdat_data(struct cxl_port *port);
 #endif /* __CXL_PCI_H__ */
drivers/cxl/port.c +54 -0

70 lines not on this page · raw message

Signature · 1 line
2.35.3
#13 lore raw

RE: [PATCH V14 6/7] cxl/port: Read CDAT table

Quoted 241 lines · ira.weiny@ wrote:

From: Ira Weiny <[email protected]>

The OS will need CDAT data from CXL devices to properly set up interleave sets. Currently this is supported through a DOE mailbox which supports CDAT.

Search the DOE mailboxes available, query CDAT data, and cache the data for later parsing.

Provide a sysfs binary attribute to allow dumping of the CDAT.

Binary dumping is modeled on /sys/firmware/ACPI/tables/

The ability to dump this table will be very useful for emulation of real devices once they become available as QEMU CXL type 3 device emulation will be able to load this file in.

This does not support table updates at runtime. It will always provide whatever was there when first cached. Handling of table updates can be implemented later.

Finally create a complete list of CDAT defines within cdat.h for code wishing to decode the CDAT table.

---
Changes from V13:
Dan:
Add entry in Documentation/ABI/testing/sysfs-bus-cxl Remove table parsing defines. s/cdat_sup/cdat_available s/cdat_mb/cdat_doe/
Don't check endpoint in find_cdat_doe() Create CDAT_DOE_TASK macro

Changes from V12:
Fix checking for task.rv for errors
Ensure no over run of non-DW aligned buffer length's

Changes from V11:
Adjust for the use of DOE mailbox xarray
Dan Williams:
Remove unnecessary get/put device
Use new BIN_ATTR_ADMIN_RO macro
Flag that CDAT was supported
If there is a read error then the CDAT sysfs will return a 0 length entry

Changes from V10:
Ben Widawsky
Failure to find CDAT should be a debug message not error Remove reference to cdat_mb from the port object
Dropped [PATCH V10 5/9] cxl/port: Find a DOE mailbox which supports
CDAT
Iterate the mailboxes for the CDAT one each time.
Define CXL_DOE_TABLE_ACCESS_LAST_ENTRY and add comment about
it's use.

Changes from V9:
Add debugging output
Jonathan Cameron
Move read_cdat to port probe by using dev_groups for the sysfs attributes. This avoids issues with using devm before the driver is loaded while making sure the CDAT binary is available.

Changes from V8:
Fix length print format
Incorporate feedback from Jonathan
Move all this to cxl_port which can help support switches when the time comes.

Changes from V6:
Fix issue with devm use
Move cached cdat data to cxl_dev_state
Use new pci_doe_submit_task()
Ensure the aux driver is locked while processing tasks Rebased on cxl-pending

Changes from V5:
Add proper guards around cdat.h
Split out finding the CDAT DOE mailbox
Use cxl_cdat to group CDAT data together
Adjust to use auxiliary_find_device() to find the DOE device
which supplies the CDAT protocol.
Rebased to latest
Remove dev_dbg(length)
Remove unneeded DOE Table access defines
Move CXL_DOE_PROTOCOL_TABLE_ACCESS define into this patch where
it is used

Changes from V4:
Split this into it's own patch
Rearchitect this such that the memdev driver calls into the DOE driver via the cxl_mem state object. This allows CDAT data to come from any type of cxl_mem object not just PCI DOE. Rebase on new struct cxl_dev_state

---
 Documentation/ABI/testing/sysfs-bus-cxl |  10 ++
 drivers/cxl/cdat.h                      |  61 +++++++++
 drivers/cxl/core/pci.c                  | 169 ++++++++++++++++++++++++
 drivers/cxl/cxl.h                       |   5 +
 drivers/cxl/cxlpci.h                    |   1 +
 drivers/cxl/port.c                      |  54 ++++++++
 6 files changed, 300 insertions(+)
 create mode 100644 drivers/cxl/cdat.h
Documentation/ABI/testing/sysfs-bus-cxl +10 -0
index 1fd5984b6158..6fb6459466f8 100644
@@ -164,3 +164,13 @@ Description:
 		expander memory (type-3). The 'target_type' attribute indicates
 		the current setting which may dynamically change based on what
 		memory regions are activated in this decode hierarchy.
+
+What:		/sys/bus/cxl/devices/endpointX/CDAT/cdat
+Date:		July, 2022
+KernelVersion:	v5.19
+Contact:	[email protected]
+Description:
+		(RO) If this sysfs entry is not present no DOE mailbox was
+		found to support CDAT data.  If it is present and the length of
+		the data is 0 reading the CDAT data failed.  Otherwise the CDAT
+		data is reported.
drivers/cxl/cdat.h new file +61 -0
new file mode 100644
index 000000000000..67010717ffca
@@ -0,0 +1,61 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __CXL_CDAT_H__
+#define __CXL_CDAT_H__
+
+/*
+ * Coherent Device Attribute table (CDAT)
+ *
+ * Specification available from UEFI.org
+ *
+ * Whilst CDAT is defined as a single table, the access via DOE maiboxes is
+ * done one entry at a time, where the first entry is the header.
+ */
+
+#define CXL_DOE_TABLE_ACCESS_REQ_CODE		0x000000ff
+#define   CXL_DOE_TABLE_ACCESS_REQ_CODE_READ	0
+#define CXL_DOE_TABLE_ACCESS_TABLE_TYPE		0x0000ff00
+#define   CXL_DOE_TABLE_ACCESS_TABLE_TYPE_CDATA	0
+#define CXL_DOE_TABLE_ACCESS_ENTRY_HANDLE	0xffff0000
+#define CXL_DOE_TABLE_ACCESS_LAST_ENTRY		0xffff
+
+/*
+ * CDAT entries are little endian and are read from PCI config space which
+ * is also little endian.
+ * As such, on a big endian system these will have been reversed.
+ * This prevents us from making easy use of packed structures.
+ * Style form pci_regs.h
+ */
+
+#define CDAT_HEADER_LENGTH_DW 4
+#define CDAT_HEADER_LENGTH_BYTES (CDAT_HEADER_LENGTH_DW * sizeof(u32))
+#define CDAT_HEADER_DW0_LENGTH		0xffffffff
+#define CDAT_HEADER_DW1_REVISION	0x000000ff
+#define CDAT_HEADER_DW1_CHECKSUM	0x0000ff00
+/* CDAT_HEADER_DW2_RESERVED	*/
+#define CDAT_HEADER_DW3_SEQUENCE	0xffffffff
+
+/* All structures have a common first DW */
+#define CDAT_STRUCTURE_DW0_TYPE		0x000000ff
+#define   CDAT_STRUCTURE_DW0_TYPE_DSMAS 0
+#define   CDAT_STRUCTURE_DW0_TYPE_DSLBIS 1
+#define   CDAT_STRUCTURE_DW0_TYPE_DSMSCIS 2
+#define   CDAT_STRUCTURE_DW0_TYPE_DSIS 3
+#define   CDAT_STRUCTURE_DW0_TYPE_DSEMTS 4
+#define   CDAT_STRUCTURE_DW0_TYPE_SSLBIS 5
+
+#define CDAT_STRUCTURE_DW0_LENGTH	0xffff0000
+
+#define CXL_DOE_PROTOCOL_TABLE_ACCESS 2
+
+/**
+ * struct cxl_cdat - CXL CDAT data
+ *
+ * @table: cache of CDAT table
+ * @length: length of cached CDAT table
+ */
+struct cxl_cdat {
+	void *table;
+	size_t length;
+};
+
+#endif /* !__CXL_CDAT_H__ */
drivers/cxl/core/pci.c +27 -0
index 7672789c3225..dd5d1da412ca 100644
@@ -4,10 +4,12 @@
 #include <linux/device.h>
 #include <linux/delay.h>
 #include <linux/pci.h>
+#include <linux/pci-doe.h>
 #include <cxlpci.h>
 #include <cxlmem.h>
 #include <cxl.h>
 #include "core.h"
+#include "cdat.h"
 
 /**
  * DOC: cxl core pci
@@ -452,3 +454,170 @@ int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm)
 	return 0;
 }
 EXPORT_SYMBOL_NS_GPL(cxl_hdm_decode_init, CXL);
+
+static struct pci_doe_mb *find_cdat_doe(struct device *uport)
+{
+	struct cxl_memdev *cxlmd;
+	struct cxl_dev_state *cxlds;
+	unsigned long index;
+	void *entry;
+
+	cxlmd = to_cxl_memdev(uport);
+	cxlds = cxlmd->cxlds;
+
+	xa_for_each(&cxlds->doe_mbs, index, entry) {
+		struct pci_doe_mb *cur = entry;
+
+		if (pci_doe_supports_prot(cur, PCI_DVSEC_VENDOR_ID_CXL,
+					  CXL_DOE_PROTOCOL_TABLE_ACCESS))
+			return cur;
+	}
+
+	return NULL;
+}
+
+#define CDAT_DOE_REQ(entry_handle)					\
+	(FIELD_PREP(CXL_DOE_TABLE_ACCESS_REQ_CODE,			\
+		    CXL_DOE_TABLE_ACCESS_REQ_CODE_READ) |		\

drivers/cxl/core/pci.c int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm) · line 491

Quoted 10 lines
drivers/cxl/core/pci.c +10 -0
+	 FIELD_PREP(CXL_DOE_TABLE_ACCESS_TABLE_TYPE,			\
+		    CXL_DOE_TABLE_ACCESS_TABLE_TYPE_CDATA) |		\
+	 FIELD_PREP(CXL_DOE_TABLE_ACCESS_ENTRY_HANDLE, (entry_handle)))
+
+static void cxl_doe_task_complete(struct pci_doe_task *task)
+{
+	complete(task->private);
+}
+
+#define CDAT_DOE_TASK(req, req_sz, rsp, rsp_sz, comp)	\

I was hoping this could save some more typing by calculating sizes of the buffers internally. I'll take a shot at that as I apply it.

drivers/cxl/core/pci.c int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm) · line 500

Quoted 9 lines
drivers/cxl/core/pci.c +9 -0
+{							\
+	.prot.vid = PCI_DVSEC_VENDOR_ID_CXL,		\
+	.prot.type = CXL_DOE_PROTOCOL_TABLE_ACCESS,	\
+	.request_pl = req,				\
+	.request_pl_sz = req_sz,			\
+	.response_pl = rsp,				\
+	.response_pl_sz = rsp_sz,			\
+	.complete = cxl_doe_task_complete,		\
+	.private = comp,			\

...and since this one is DOE specific the completion can be declared internally to the macro as well. Acutally, the request and response payloads are identical in all usages so this can make the call sites even smaller.

Quoted 80 lines
drivers/cxl/core/pci.c +80 -0
+}
+
+static int cxl_cdat_get_length(struct device *dev,
+			       struct pci_doe_mb *cdat_doe,
+			       size_t *length)
+{
+	u32 cdat_request_pl = CDAT_DOE_REQ(0);
+	u32 cdat_response_pl[32];
+	DECLARE_COMPLETION_ONSTACK(c);
+	struct pci_doe_task task = CDAT_DOE_TASK(&cdat_request_pl,
+						 sizeof(cdat_request_pl),
+						 cdat_response_pl,
+						 sizeof(cdat_response_pl),
+						 &c);
+	int rc;
+
+	rc = pci_doe_submit_task(cdat_doe, &task);
+	if (rc < 0) {
+		dev_err(dev, "DOE submit failed: %d", rc);
+		return rc;
+	}
+	wait_for_completion(&c);
+	if (task.rv < sizeof(u32))
+		return -EIO;
+
+	*length = cdat_response_pl[1];
+	dev_dbg(dev, "CDAT length %zu\n", *length);
+
+	return 0;
+}
+
+static int cxl_cdat_read_table(struct device *dev,
+			       struct pci_doe_mb *cdat_doe,
+			       struct cxl_cdat *cdat)
+{
+	size_t length = cdat->length;
+	u32 *data = cdat->table;
+	int entry_handle = 0;
+
+	do {
+		u32 cdat_request_pl = CDAT_DOE_REQ(entry_handle);
+		u32 cdat_response_pl[32];
+		DECLARE_COMPLETION_ONSTACK(c);
+		struct pci_doe_task task = CDAT_DOE_TASK(&cdat_request_pl,
+							 sizeof(cdat_request_pl),
+							 cdat_response_pl,
+							 sizeof(cdat_response_pl),
+							 &c);
+		size_t entry_dw;
+		u32 *entry;
+		int rc;
+
+		rc = pci_doe_submit_task(cdat_doe, &task);
+		if (rc < 0) {
+			dev_err(dev, "DOE submit failed: %d", rc);
+			return rc;
+		}
+		wait_for_completion(&c);
+		/* 1 DW header + 1 DW data min */
+		if (task.rv < (2 * sizeof(u32)))
+			return -EIO;
+
+		/* Get the CXL table access header entry handle */
+		entry_handle = FIELD_GET(CXL_DOE_TABLE_ACCESS_ENTRY_HANDLE,
+					 cdat_response_pl[0]);
+		entry = cdat_response_pl + 1;
+		entry_dw = task.rv / sizeof(u32);
+		/* Skip Header */
+		entry_dw -= 1;
+		entry_dw = min(length / sizeof(u32), entry_dw);
+		/* Prevent length < 1 DW from causing a buffer overflow */
+		if (entry_dw) {
+			memcpy(data, entry, entry_dw * sizeof(u32));
+			length -= entry_dw * sizeof(u32);
+			data += entry_dw;
+		}
+	} while (entry_handle != CXL_DOE_TABLE_ACCESS_LAST_ENTRY);
+
+	return 0;
+}

drivers/cxl/core/pci.c int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm) · line 590

Quoted 10 lines
drivers/cxl/core/pci.c +10 -0
+
+/**
+ * read_cdat_data - Read the CDAT data on this port
+ * @port: Port to read data from
+ *
+ * This call will sleep waiting for responses from the DOE mailbox.
+ */
+void read_cdat_data(struct cxl_port *port)
+{
+	static struct pci_doe_mb *cdat_doe;

Why is this variable static?

Quoted 120 lines
drivers/cxl/core/pci.c +33 -0
+	struct device *dev = &port->dev;
+	struct device *uport = port->uport;
+	size_t cdat_length;
+	int rc;
+
+	cdat_doe = find_cdat_doe(uport);
+	if (!cdat_doe) {
+		dev_dbg(dev, "No CDAT mailbox\n");
+		return;
+	}
+
+	port->cdat_available = true;
+
+	if (cxl_cdat_get_length(dev, cdat_doe, &cdat_length)) {
+		dev_dbg(dev, "No CDAT length\n");
+		return;
+	}
+
+	port->cdat.table = devm_kzalloc(dev, cdat_length, GFP_KERNEL);
+	if (!port->cdat.table)
+		return;
+
+	port->cdat.length = cdat_length;
+	rc = cxl_cdat_read_table(dev, cdat_doe, &port->cdat);
+	if (rc) {
+		/* Don't leave table data allocated on error */
+		devm_kfree(dev, port->cdat.table);
+		port->cdat.table = NULL;
+		port->cdat.length = 0;
+		dev_err(dev, "CDAT data read error\n");
+	}
+}
+EXPORT_SYMBOL_NS_GPL(read_cdat_data, CXL);
drivers/cxl/cxl.h +5 -0
index 570bd9f8141b..64ea8c80b917 100644
@@ -8,6 +8,7 @@
 #include <linux/bitfield.h>
 #include <linux/bitops.h>
 #include <linux/io.h>
+#include "cdat.h"
 
 /**
  * DOC: cxl objects
@@ -289,6 +290,8 @@ struct cxl_nvdimm {
  * @component_reg_phys: component register capability base address (optional)
  * @dead: last ep has been removed, force port re-creation
  * @depth: How deep this port is relative to the root. depth 0 is the root.
+ * @cdat: Cached CDAT data
+ * @cdat_available: Should a CDAT attribute be available in sysfs
  */
 struct cxl_port {
 	struct device dev;
@@ -301,6 +304,8 @@ struct cxl_port {
 	resource_size_t component_reg_phys;
 	bool dead;
 	unsigned int depth;
+	struct cxl_cdat cdat;
+	bool cdat_available;
 };
 
 /**
drivers/cxl/cxlpci.h +1 -0
index fce1c11729c2..eec597dbe763 100644
@@ -74,4 +74,5 @@ static inline resource_size_t cxl_regmap_to_base(struct pci_dev *pdev,
 int devm_cxl_port_enumerate_dports(struct cxl_port *port);
 struct cxl_dev_state;
 int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm);
+void read_cdat_data(struct cxl_port *port);
 #endif /* __CXL_PCI_H__ */
drivers/cxl/port.c +32 -0
index 3cf308f114c4..08b8ac76f87b 100644
@@ -53,6 +53,9 @@ static int cxl_port_probe(struct device *dev)
 		struct cxl_memdev *cxlmd = to_cxl_memdev(port->uport);
 		struct cxl_dev_state *cxlds = cxlmd->cxlds;
 
+		/* Cache the data early to ensure is_visible() works */
+		read_cdat_data(port);
+
 		get_device(&cxlmd->dev);
 		rc = devm_add_action_or_reset(dev, schedule_detach, cxlmd);
 		if (rc)
@@ -78,10 +81,61 @@ static int cxl_port_probe(struct device *dev)
 	return 0;
 }
 
+static ssize_t cdat_read(struct file *filp, struct kobject *kobj,
+			 struct bin_attribute *bin_attr, char *buf,
+			 loff_t offset, size_t count)
+{
+	struct device *dev = kobj_to_dev(kobj);
+	struct cxl_port *port = to_cxl_port(dev);
+
+	if (!port->cdat_available)
+		return -ENXIO;
+
+	if (!port->cdat.table)
+		return 0;
+
+	return memory_read_from_buffer(buf, count, &offset,
+				       port->cdat.table,
+				       port->cdat.length);
+}
+
+static BIN_ATTR_ADMIN_RO(cdat, 0);
+
+static umode_t cxl_port_bin_attr_is_visible(struct kobject *kobj,
+					      struct bin_attribute *attr, int i)
+{
+	struct device *dev = kobj_to_dev(kobj);
+	struct cxl_port *port = to_cxl_port(dev);
+
+	if ((attr == &bin_attr_cdat) && port->cdat_available)
+		return attr->attr.mode;
+

drivers/cxl/port.c static int cxl_port_probe(struct device *dev) · line 122

Quoted 10 lines
drivers/cxl/port.c +10 -0
+	return 0;
+}
+
+static struct bin_attribute *cxl_cdat_bin_attributes[] = {
+	&bin_attr_cdat,
+	NULL,
+};
+
+static struct attribute_group cxl_cdat_attribute_group = {
+	.name = "CDAT",

Why does it need its own directory? I'll just fix this up to drop the extra CDAT directory, and rename the attribute file to CDAT directly.

Quoted 22 lines
drivers/cxl/port.c +12 -0
+	.bin_attrs = cxl_cdat_bin_attributes,
+	.is_bin_visible = cxl_port_bin_attr_is_visible,
+};
+
+static const struct attribute_group *cxl_port_attribute_groups[] = {
+	&cxl_cdat_attribute_group,
+	NULL,
+};
+
 static struct cxl_driver cxl_port_driver = {
 	.name = "cxl_port",
 	.probe = cxl_port_probe,
 	.id = CXL_DEVICE_PORT,
+	.drv = {
+		.dev_groups = cxl_port_attribute_groups,
+	},
 };
 
 module_cxl_driver(cxl_port_driver);
Signature · 1 line
2.35.3
#14 lore raw

RE: [PATCH V14 6/7] cxl/port: Read CDAT table

In reply to [email protected]
Quoted 151 lines · ira.weiny@ wrote:

From: Ira Weiny <[email protected]>

The OS will need CDAT data from CXL devices to properly set up interleave sets. Currently this is supported through a DOE mailbox which supports CDAT.

Search the DOE mailboxes available, query CDAT data, and cache the data for later parsing.

Provide a sysfs binary attribute to allow dumping of the CDAT.

Binary dumping is modeled on /sys/firmware/ACPI/tables/

The ability to dump this table will be very useful for emulation of real devices once they become available as QEMU CXL type 3 device emulation will be able to load this file in.

This does not support table updates at runtime. It will always provide whatever was there when first cached. Handling of table updates can be implemented later.

Finally create a complete list of CDAT defines within cdat.h for code wishing to decode the CDAT table.

---
Changes from V13:
Dan:
Add entry in Documentation/ABI/testing/sysfs-bus-cxl Remove table parsing defines. s/cdat_sup/cdat_available s/cdat_mb/cdat_doe/
Don't check endpoint in find_cdat_doe() Create CDAT_DOE_TASK macro

Changes from V12:
Fix checking for task.rv for errors
Ensure no over run of non-DW aligned buffer length's

Changes from V11:
Adjust for the use of DOE mailbox xarray
Dan Williams:
Remove unnecessary get/put device
Use new BIN_ATTR_ADMIN_RO macro
Flag that CDAT was supported
If there is a read error then the CDAT sysfs will return a 0 length entry

Changes from V10:
Ben Widawsky
Failure to find CDAT should be a debug message not error Remove reference to cdat_mb from the port object
Dropped [PATCH V10 5/9] cxl/port: Find a DOE mailbox which supports
CDAT
Iterate the mailboxes for the CDAT one each time.
Define CXL_DOE_TABLE_ACCESS_LAST_ENTRY and add comment about
it's use.

Changes from V9:
Add debugging output
Jonathan Cameron
Move read_cdat to port probe by using dev_groups for the sysfs attributes. This avoids issues with using devm before the driver is loaded while making sure the CDAT binary is available.

Changes from V8:
Fix length print format
Incorporate feedback from Jonathan
Move all this to cxl_port which can help support switches when the time comes.

Changes from V6:
Fix issue with devm use
Move cached cdat data to cxl_dev_state
Use new pci_doe_submit_task()
Ensure the aux driver is locked while processing tasks Rebased on cxl-pending

Changes from V5:
Add proper guards around cdat.h
Split out finding the CDAT DOE mailbox
Use cxl_cdat to group CDAT data together
Adjust to use auxiliary_find_device() to find the DOE device
which supplies the CDAT protocol.
Rebased to latest
Remove dev_dbg(length)
Remove unneeded DOE Table access defines
Move CXL_DOE_PROTOCOL_TABLE_ACCESS define into this patch where
it is used

Changes from V4:
Split this into it's own patch
Rearchitect this such that the memdev driver calls into the DOE driver via the cxl_mem state object. This allows CDAT data to come from any type of cxl_mem object not just PCI DOE. Rebase on new struct cxl_dev_state

---
 Documentation/ABI/testing/sysfs-bus-cxl |  10 ++
 drivers/cxl/cdat.h                      |  61 +++++++++
 drivers/cxl/core/pci.c                  | 169 ++++++++++++++++++++++++
 drivers/cxl/cxl.h                       |   5 +
 drivers/cxl/cxlpci.h                    |   1 +
 drivers/cxl/port.c                      |  54 ++++++++
 6 files changed, 300 insertions(+)
 create mode 100644 drivers/cxl/cdat.h
Documentation/ABI/testing/sysfs-bus-cxl +10 -0
index 1fd5984b6158..6fb6459466f8 100644
@@ -164,3 +164,13 @@ Description:
 		expander memory (type-3). The 'target_type' attribute indicates
 		the current setting which may dynamically change based on what
 		memory regions are activated in this decode hierarchy.
+
+What:		/sys/bus/cxl/devices/endpointX/CDAT/cdat
+Date:		July, 2022
+KernelVersion:	v5.19
+Contact:	[email protected]
+Description:
+		(RO) If this sysfs entry is not present no DOE mailbox was
+		found to support CDAT data.  If it is present and the length of
+		the data is 0 reading the CDAT data failed.  Otherwise the CDAT
+		data is reported.
drivers/cxl/cdat.h new file +17 -0
new file mode 100644
index 000000000000..67010717ffca
@@ -0,0 +1,61 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __CXL_CDAT_H__
+#define __CXL_CDAT_H__
+
+/*
+ * Coherent Device Attribute table (CDAT)
+ *
+ * Specification available from UEFI.org
+ *
+ * Whilst CDAT is defined as a single table, the access via DOE maiboxes is
+ * done one entry at a time, where the first entry is the header.
+ */
+
+#define CXL_DOE_TABLE_ACCESS_REQ_CODE		0x000000ff
+#define   CXL_DOE_TABLE_ACCESS_REQ_CODE_READ	0
+#define CXL_DOE_TABLE_ACCESS_TABLE_TYPE		0x0000ff00
+#define   CXL_DOE_TABLE_ACCESS_TABLE_TYPE_CDATA	0

drivers/cxl/cdat.h line 27

Quoted 10 lines
drivers/cxl/cdat.h new file +10 -0
+#define CXL_DOE_TABLE_ACCESS_ENTRY_HANDLE	0xffff0000
+#define CXL_DOE_TABLE_ACCESS_LAST_ENTRY		0xffff
+
+/*
+ * CDAT entries are little endian and are read from PCI config space which
+ * is also little endian.
+ * As such, on a big endian system these will have been reversed.
+ * This prevents us from making easy use of packed structures.
+ * Style form pci_regs.h
+ */

I do not get this... the PCI ops on big endian machines are already handling the fact that PCI config space is in le-order. So you should be able to use data structure definitions directly just like any other PCI config data payload.

I dropped this along with the other revisions I spotted for this patch.

Quoted 9 lines
drivers/cxl/cdat.h new file +9 -0
+
+#define CDAT_HEADER_LENGTH_DW 4
+#define CDAT_HEADER_LENGTH_BYTES (CDAT_HEADER_LENGTH_DW * sizeof(u32))
+#define CDAT_HEADER_DW0_LENGTH		0xffffffff
+#define CDAT_HEADER_DW1_REVISION	0x000000ff
+#define CDAT_HEADER_DW1_CHECKSUM	0x0000ff00
+/* CDAT_HEADER_DW2_RESERVED	*/
+#define CDAT_HEADER_DW3_SEQUENCE	0xffffffff
+

drivers/cxl/cdat.h line 46

Quoted 10 lines
drivers/cxl/cdat.h new file +10 -0
+/* All structures have a common first DW */
+#define CDAT_STRUCTURE_DW0_TYPE		0x000000ff
+#define   CDAT_STRUCTURE_DW0_TYPE_DSMAS 0
+#define   CDAT_STRUCTURE_DW0_TYPE_DSLBIS 1
+#define   CDAT_STRUCTURE_DW0_TYPE_DSMSCIS 2
+#define   CDAT_STRUCTURE_DW0_TYPE_DSIS 3
+#define   CDAT_STRUCTURE_DW0_TYPE_DSEMTS 4
+#define   CDAT_STRUCTURE_DW0_TYPE_SSLBIS 5
+
+#define CDAT_STRUCTURE_DW0_LENGTH	0xffff0000

Dropped these too since they are unused.

#15 lore raw

PATCH v15 6/7 cxl/port: Read CDAT table

In reply to [email protected]

From: Ira Weiny <[email protected]>

The per-device CDAT data provides performance data that is relevant for mapping which CXL devices can participate in which CXL ranges by QTG (QoS Throttling Group) (per ECN: CXL 2.0 CEDT CFMWS & QTG_DSM) [1]. The QTG association specified in the ECN is advisory. Until the cxl_acpi driver grows support for invoking the QTG _DSM method the CDAT data is only of interest to userspace that may need it for debug purposes.

Search the DOE mailboxes available, query CDAT data, cache the data and make it available via a sysfs binary attribute per endpoint at:

/sys/bus/cxl/devices/endpointX/CDAT

...similar to other ACPI-structured table data in /sys/firmware/ACPI/tables. The CDAT is relative to 'struct cxl_port' objects since switches in addition to endpoints can host a CDAT instance. Switch CDAT support is not implemented.

This does not support table updates at runtime. It will always provide whatever was there when first cached. It is also the case that table updates are not expected outside of explicit DPA address map affecting commands like Set Partition with the immediate flag set. Given that the driver does not support Set Partition with the immediate flag set there is no current need for update support.

[djbw: drop in-kernel parsing infra for now, and other minor fixups]

---
Changes since v14:
- Add a DECLARE_CDAT_DOE_TASK() to reduce boilerplate in the functions
- Delete cdat.h since the parsing code is deferred until later
- Add some more details to the changelog about the expectations for this implementation

 Documentation/ABI/testing/sysfs-bus-cxl |   10 ++
 drivers/cxl/core/pci.c                  |  173 +++++++++++++++++++++++++++++++
 drivers/cxl/cxl.h                       |    7 +
 drivers/cxl/cxlpci.h                    |    1 
 drivers/cxl/port.c                      |   53 +++++++++
 5 files changed, 244 insertions(+)
Documentation/ABI/testing/sysfs-bus-cxl +10 -0
index 1fd5984b6158..e94c5aebc368 100644
@@ -164,3 +164,13 @@ Description:
 		expander memory (type-3). The 'target_type' attribute indicates
 		the current setting which may dynamically change based on what
 		memory regions are activated in this decode hierarchy.
+
+What:		/sys/bus/cxl/devices/endpointX/CDAT
+Date:		July, 2022
+KernelVersion:	v5.20
+Contact:	[email protected]
+Description:
+		(RO) If this sysfs entry is not present no DOE mailbox was
+		found to support CDAT data.  If it is present and the length of
+		the data is 0 reading the CDAT data failed.  Otherwise the CDAT
+		data is reported.
drivers/cxl/core/pci.c +173 -0

182 lines not on this page · raw message

drivers/cxl/cxl.h +7 -0
index 570bd9f8141b..21a9d6fcc61e 100644
@@ -289,6 +289,8 @@ struct cxl_nvdimm {
  * @component_reg_phys: component register capability base address (optional)
  * @dead: last ep has been removed, force port re-creation
  * @depth: How deep this port is relative to the root. depth 0 is the root.
+ * @cdat: Cached CDAT data
+ * @cdat_available: Should a CDAT attribute be available in sysfs
  */
 struct cxl_port {
 	struct device dev;
@@ -301,6 +303,11 @@ struct cxl_port {
 	resource_size_t component_reg_phys;
 	bool dead;
 	unsigned int depth;
+	struct cxl_cdat {
+		void *table;
+		size_t length;
+	} cdat;
+	bool cdat_available;
 };
 
 /**
drivers/cxl/cxlpci.h +1 -0
index fce1c11729c2..eec597dbe763 100644
@@ -74,4 +74,5 @@ static inline resource_size_t cxl_regmap_to_base(struct pci_dev *pdev,
 int devm_cxl_port_enumerate_dports(struct cxl_port *port);
 struct cxl_dev_state;
 int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm);
+void read_cdat_data(struct cxl_port *port);
 #endif /* __CXL_PCI_H__ */
drivers/cxl/port.c +53 -0

69 lines not on this page · raw message

Reviewed-by on v15 6/7
#16 lore raw

Re: [PATCH v15 6/7] cxl/port: Read CDAT table

Quoted 27 lines · On Mon, 18 Jul 2022 18:55:01 -0700 Dan Williams <[email protected]>…

From: Ira Weiny <[email protected]>

The per-device CDAT data provides performance data that is relevant for mapping which CXL devices can participate in which CXL ranges by QTG (QoS Throttling Group) (per ECN: CXL 2.0 CEDT CFMWS & QTG_DSM) [1]. The QTG association specified in the ECN is advisory. Until the cxl_acpi driver grows support for invoking the QTG _DSM method the CDAT data is only of interest to userspace that may need it for debug purposes.

Search the DOE mailboxes available, query CDAT data, cache the data and make it available via a sysfs binary attribute per endpoint at:

/sys/bus/cxl/devices/endpointX/CDAT

...similar to other ACPI-structured table data in /sys/firmware/ACPI/tables. The CDAT is relative to 'struct cxl_port' objects since switches in addition to endpoints can host a CDAT instance. Switch CDAT support is not implemented.

This does not support table updates at runtime. It will always provide whatever was there when first cached. It is also the case that table updates are not expected outside of explicit DPA address map affecting commands like Set Partition with the immediate flag set. Given that the driver does not support Set Partition with the immediate flag set there is no current need for update support.

Quoted 6 lines
[djbw: drop in-kernel parsing infra for now, and other minor fixups]

One comment inline that basically says the error message will do for now if we race with an autonomous update of CDAT by the device. (see response to earlier version on why that can happen).

Quoted 36 lines

---

drivers/cxl/core/pci.c +29 -0
index 7672789c3225..9240df53ed87 100644
 
 
+/**
+ * read_cdat_data - Read the CDAT data on this port
+ * @port: Port to read data from
+ *
+ * This call will sleep waiting for responses from the DOE mailbox.
+ */
+void read_cdat_data(struct cxl_port *port)
+{
+	struct pci_doe_mb *cdat_doe;
+	struct device *dev = &port->dev;
+	struct device *uport = port->uport;
+	size_t cdat_length;
+	int rc;
+
+	cdat_doe = find_cdat_doe(uport);
+	if (!cdat_doe) {
+		dev_dbg(dev, "No CDAT mailbox\n");
+		return;
+	}
+
+	port->cdat_available = true;
+
+	if (cxl_cdat_get_length(dev, cdat_doe, &cdat_length)) {
+		dev_dbg(dev, "No CDAT length\n");
+		return;
+	}
+
+	port->cdat.table = devm_kzalloc(dev, cdat_length, GFP_KERNEL);
+	if (!port->cdat.table)

drivers/cxl/core/pci.c int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm) · line 624

Quoted 10 lines
drivers/cxl/core/pci.c +10 -0
+		return;
+
+	port->cdat.length = cdat_length;
+	rc = cxl_cdat_read_table(dev, cdat_doe, &port->cdat);
+	if (rc) {
+		/* Don't leave table data allocated on error */
+		devm_kfree(dev, port->cdat.table);
+		port->cdat.table = NULL;
+		port->cdat.length = 0;
+		dev_err(dev, "CDAT data read error\n");

This will be sufficient for now for the race against autonomous CDAT update potential issue mentioned in earlier review. If it happens and someone really cares they can unbind and rebind the device.

Quoted 6 lines
drivers/cxl/core/pci.c +3 -0
+	}
+}
+EXPORT_SYMBOL_NS_GPL(read_cdat_data, CXL);
#17 lore raw

Re: [PATCH v15 6/7] cxl/port: Read CDAT table

Quoted 83 lines · Jonathan Cameron
Quoted 27 lines · On Mon, 18 Jul 2022 18:55:01 -0700 Dan Williams <[email protected]>…

From: Ira Weiny <[email protected]>

The per-device CDAT data provides performance data that is relevant for mapping which CXL devices can participate in which CXL ranges by QTG (QoS Throttling Group) (per ECN: CXL 2.0 CEDT CFMWS & QTG_DSM) [1]. The QTG association specified in the ECN is advisory. Until the cxl_acpi driver grows support for invoking the QTG _DSM method the CDAT data is only of interest to userspace that may need it for debug purposes.

Search the DOE mailboxes available, query CDAT data, cache the data and make it available via a sysfs binary attribute per endpoint at:

/sys/bus/cxl/devices/endpointX/CDAT

...similar to other ACPI-structured table data in /sys/firmware/ACPI/tables. The CDAT is relative to 'struct cxl_port' objects since switches in addition to endpoints can host a CDAT instance. Switch CDAT support is not implemented.

This does not support table updates at runtime. It will always provide whatever was there when first cached. It is also the case that table updates are not expected outside of explicit DPA address map affecting commands like Set Partition with the immediate flag set. Given that the driver does not support Set Partition with the immediate flag set there is no current need for update support.

Quoted 6 lines
[djbw: drop in-kernel parsing infra for now, and other minor fixups]

One comment inline that basically says the error message will do for now if we race with an autonomous update of CDAT by the device. (see response to earlier version on why that can happen).

Quoted 42 lines

---

drivers/cxl/core/pci.c +35 -0
index 7672789c3225..9240df53ed87 100644
 
 
+/**
+ * read_cdat_data - Read the CDAT data on this port
+ * @port: Port to read data from
+ *
+ * This call will sleep waiting for responses from the DOE mailbox.
+ */
+void read_cdat_data(struct cxl_port *port)
+{
+	struct pci_doe_mb *cdat_doe;
+	struct device *dev = &port->dev;
+	struct device *uport = port->uport;
+	size_t cdat_length;
+	int rc;
+
+	cdat_doe = find_cdat_doe(uport);
+	if (!cdat_doe) {
+		dev_dbg(dev, "No CDAT mailbox\n");
+		return;
+	}
+
+	port->cdat_available = true;
+
+	if (cxl_cdat_get_length(dev, cdat_doe, &cdat_length)) {
+		dev_dbg(dev, "No CDAT length\n");
+		return;
+	}
+
+	port->cdat.table = devm_kzalloc(dev, cdat_length, GFP_KERNEL);
+	if (!port->cdat.table)
+		return;
+
+	port->cdat.length = cdat_length;
+	rc = cxl_cdat_read_table(dev, cdat_doe, &port->cdat);
+	if (rc) {
+		/* Don't leave table data allocated on error */
Quoted 8 lines

drivers/cxl/core/pci.c int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm) · line 624

Quoted 4 lines
drivers/cxl/core/pci.c +4 -0
+		devm_kfree(dev, port->cdat.table);
+		port->cdat.table = NULL;
+		port->cdat.length = 0;
+		dev_err(dev, "CDAT data read error\n");

This will be sufficient for now for the race against autonomous CDAT update potential issue mentioned in earlier review. If it happens and someone really cares they can unbind and rebind the device.

Even it is too late to change the spec, which I do not think it is, it is not too late to make a public statement that Linux has no interest in supporting such shenanigans outside of a documented set of explicit commands that have a CDAT-update side effect.

#18 lore raw

PATCH v14 7/7 cxl/port: Introduce cxl_cdat_valid()

In reply to [email protected]

From: Ira Weiny <[email protected]>

The CDAT data is protected by a checksum and should be the proper length.

Introduce cxl_cdat_valid() to validate the data. While at it check and store the sequence number.

---
Changes from V12
Jonathan:
Remove unneeded rc check.

Changes from V8
Move code to cxl/core/pci.c

Changes from V6
Change name to cxl_cdat_valid() as this validates all the CDAT
data not just the header
Add error and debug prints

Changes from V5
New patch, split out
Update cdat_hdr_valid()
Remove revision and cs field parsing
There is no point in these
Add seq check and debug print.

---
 drivers/cxl/cdat.h     |  2 ++
 drivers/cxl/core/pci.c | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+)
drivers/cxl/cdat.h +2 -0
index 67010717ffca..162ef474ee5a 100644
@@ -52,10 +52,12 @@
  *
  * @table: cache of CDAT table
  * @length: length of cached CDAT table
+ * @seq: Last read Sequence number of the CDAT table
  */
 struct cxl_cdat {
 	void *table;
 	size_t length;
+	u32 seq;
 };
 
 #endif /* !__CXL_CDAT_H__ */
drivers/cxl/core/pci.c +37 -0
index dd5d1da412ca..36e14549997d 100644
@@ -529,6 +529,40 @@ static int cxl_cdat_get_length(struct device *dev,
 	return 0;
 }
 
+static bool cxl_cdat_valid(struct device *dev, struct cxl_cdat *cdat)
+{
+	u32 *table = cdat->table;
+	u8 *data8 = cdat->table;
+	u32 length, seq;
+	u8 check;
+	int i;
+
+	length = FIELD_GET(CDAT_HEADER_DW0_LENGTH, table[0]);
+	if ((length < CDAT_HEADER_LENGTH_BYTES) || (length > cdat->length)) {
+		dev_err(dev, "CDAT Invalid length %u (%zu-%zu)\n", length,
+			CDAT_HEADER_LENGTH_BYTES, cdat->length);
+		return false;
+	}
+
+	for (check = 0, i = 0; i < length; i++)
+		check += data8[i];
+
+	dev_dbg(dev, "CDAT length %u CS %u\n", length, check);
+	if (check != 0) {
+		dev_err(dev, "CDAT Invalid checksum %u\n", check);
+		return false;
+	}
+
+	seq = FIELD_GET(CDAT_HEADER_DW3_SEQUENCE, table[3]);
+	/* Store the sequence for now. */
+	if (cdat->seq != seq) {
+		dev_info(dev, "CDAT seq change %x -> %x\n", cdat->seq, seq);
+		cdat->seq = seq;
+	}
+
+	return true;
+}
+
 static int cxl_cdat_read_table(struct device *dev,
 			       struct pci_doe_mb *cdat_doe,
 			       struct cxl_cdat *cdat)
@@ -576,6 +610,9 @@ static int cxl_cdat_read_table(struct device *dev,
 		}
 	} while (entry_handle != CXL_DOE_TABLE_ACCESS_LAST_ENTRY);
 
+	if (!cxl_cdat_valid(dev, cdat))
+		return -EIO;
+
 	return 0;
 }
 
Signature · 1 line
2.35.3
#19 lore raw

RE: [PATCH V14 7/7] cxl/port: Introduce cxl_cdat_valid()

Quoted 7 lines · ira.weiny@ wrote:

From: Ira Weiny <[email protected]>

The CDAT data is protected by a checksum and should be the proper length.

Introduce cxl_cdat_valid() to validate the data. While at it check and store the sequence number.

I am going to drop this one. Userspace can determine validity when it parses it. When the kernel grows a CDAT parser it will rely on the standard validation of ACPI-table-like structures from a future __acpi_table_parse_entries() derivative for this purpose.

#20 lore raw

Re: [PATCH V14 7/7] cxl/port: Introduce cxl_cdat_valid()

Quoted 6 lines · On Fri, 15 Jul 2022 19:26:01 -0700 Dan Williams <[email protected]>…
Quoted 5 lines · ira.weiny@ wrote:

From: Ira Weiny <[email protected]>

The CDAT data is protected by a checksum and should be the proper length.

Quoted 7 lines
Quoted 2 lines

Introduce cxl_cdat_valid() to validate the data. While at it check and store the sequence number.

I am going to drop this one. Userspace can determine validity when it parses it. When the kernel grows a CDAT parser it will rely on the standard validation of ACPI-table-like structures from a future __acpi_table_parse_entries() derivative for this purpose.

OK, for now I guess.

#21 lore raw
In reply to [email protected]
Quoted 52 lines · On Thu, 14 Jul 2022 20:04:17 -0700 [email protected] wrote:

From: Ira Weiny <[email protected]>

Details of changes are in the individual patches.

Major changes from V13:[10]
Dan minor updates
Willy's suggestion of documentation is good but I'm deferring it until we get the location of the PCI mailboxes settled.
Drop retry CDAT patch
Drop DSMAS patch
Rebased on latest cxl-pending

CXL drivers need various data which are provided through generic DOE mailboxes as defined in the PCIe 6.0 spec.[1]

One such data is the Coherent Device Attribute Table (CDAT). CDAT data provides coherent information about the various devices in the system. It was developed because systems no longer have a priori knowledge of all coherent devices within a system. CDAT describes the coherent characteristics of the components on the CXL bus separate from system configurations. The OS can then, for example, use this information to form correct interleave sets.

To begin reading the CDAT the OS must have support to access the DOE mailboxes provided by the CXL devices.

Because DOE is not specific to DOE but is provided within the PCI spec, the series adds PCI DOE capability library functions. These functions allow for the iteration of the DOE capabilities on a device as well as creating pci_doe_mb structures which can control the operation of the DOE state machine.

For now the iteration of and storage of the DOE mailboxes is done on memdev objects within the CXL stack. When this is needed in more generic code this can be lifted later.

This work was tested using qemu.

[0] https://lore.kernel.org/linux-cxl/[email protected]/
[1] https://pcisig.com/specifications
[2] https://lore.kernel.org/qemu-devel/[email protected]/
[3] https://lore.kernel.org/linux-cxl/[email protected]/
[4] https://lore.kernel.org/linux-cxl/[email protected]/
[5] https://lore.kernel.org/linux-cxl/[email protected]/
[6] https://lore.kernel.org/linux-cxl/[email protected]/
[7] https://lore.kernel.org/linux-cxl/[email protected]/
[8] https://lore.kernel.org/linux-cxl/[email protected]/
[9] https://lore.kernel.org/linux-cxl/[email protected]/
[10] https://lore.kernel.org/linux-cxl/[email protected]/

Previous changes
================

Quoted 6 lines

Changes from V12:[9]
A couple of bug fixes in the new XArray stuff
Remove the IRQ support because I did not realize how that worked and it was complicating things.
Remove busy retries and replace with an error as there is no good way to ensure it will work.

This is fine for userspace access, but I think we probably will want retries once we are using it in kernel. Whilst we'd not expect it to be common as per (very late) reply I sent to v13 discussion, the CDAT table can change all on it's own (as far as software can see). I'd expect it to be a once in a blue moon thing though.

Quoted 134 lines

Other code clean ups mentioned in the individual patches.

Changes from V11:[8]
The major change in this version is to remove the workqueue from the internal implementation of the state machine. A single ordered workqueue within each mailbox processes tasks submitted. This workqueue takes care of all locking and guarantees that tasks are completed in the order submitted. Any synchronization which is required between tasks will need to be handled by the user of the mailbox. However, the user can depend on work items being completed in the order they are submitted. So a single thread submitter is guaranteed to get all work items completed in order. This also aids in the support of a single mailbox supporting multiple protocols. Each protocol could have a separate thread submitting tasks for that protocol. The mailbox object will ensure that each protocol task is complete before another task starts. But multiple user threads can be submitting tasks for different protocols all at the same time without regard to other protocols being used.

XArrays are used throughout the series.

Other minor changes are noted in the individual patches.

Changes from V10:[7]
Address Ben Widawsky's comments
Protect against potentially malicious devices. Fix ownership issue of cdat_mb

Changes from V9:[6]
Address feedback from
Lukas Wunner, Davidlohr Bueso, Jonathan Cameron, Alison Schofield, and Ben Widawsky Details in each individual patch.

Changes from V8:[5]
For this version I've punted a bit to get it out and drop the auxiliary bus functionality. I like where Jonathan is going with the port driver idea. I think eventually the irq/mailbox creation will need to be more generic in a PCI port driver. I've modeled this version on such an architecture but used the CXL port for the time being.

From Dan
Drop the auxiliary bus/device
From Jonathan
Cleanups
From Bjorn
Clean up commit messages
move pci-doe.c to doe.c
Clean up PCI spec references
Ensure all messages use pci_*()
Add offset to error messages to distinguish mailboxes
use hex for DOE offsets
Print 4 nibbles for Vendor ID and 2 for type. s/irq/IRQ in comments
Fix long lines
Fix typos

Changes from V7:[4]
Avoid code bloat by making pci-doe.c conditional on CONFIG_PCI_DOE
which is auto selected by the CXL_PCI config option.
Minor code clean ups
Fix bug in pci_doe_supports_prot()
Rebase to cxl-pending

Changes from V6:[3]
The big change is the removal of the auxiliary bus code from the PCI layer. The auxiliary bus usage is now in the CXL layer. The PCI layer provides helpers for subsystems to utilize DOE mailboxes by creating a pci_doe_mb object which controls a state machine for that mailbox capability. The CXL layer wraps this object in an auxiliary device and driver which can then be used to determine if the kernel is controlling the capability or it is available to be used by user space. Reads from user space via lspci are allowed. Writes are allowed but flagged via a tainting the kernel.

Feedback from Bjorn, Jonathan, and Dan
Details in each patch

Changes from V5:[0]

Rework the patch set to split PCI vs CXL changes
Also make each change a bit more stand alone for easier review
Add cxl_cdat structure
Put CDAT related data structures in cdat.h
Clarify some device lifetimes with comments
Incorporate feedback from Jonathan, Bjorn and Dan
The bigest change is placing the DOE scanning code into the
pci_doe driver (part of the PCI codre).
Validate the CDAT when it is read rather than before DSMAS
parsing
Do not report DSMAS failure as an error, report a warning and
keep going.
Retry reading the table 1 time.
Update commit messages and this cover letter

Ira Weiny (5):
PCI: Replace magic constant for PCI Sig Vendor ID cxl/pci: Create PCI DOE mailbox's for memory devices
driver-core: Introduce BIN_ATTR_ADMIN_{RO,RW} cxl/port: Read CDAT table
cxl/port: Introduce cxl_cdat_valid()

Jonathan Cameron (2):
PCI: Add vendor ID for the PCI SIG
PCI/DOE: Add DOE mailbox support functions

 .clang-format                           |   1 +
 Documentation/ABI/testing/sysfs-bus-cxl |  10 +
 drivers/cxl/Kconfig                     |   1 +
 drivers/cxl/cdat.h                      |  63 +++
 drivers/cxl/core/pci.c                  | 206 +++++++++
 drivers/cxl/cxl.h                       |   5 +
 drivers/cxl/cxlmem.h                    |   3 +
 drivers/cxl/cxlpci.h                    |   1 +
 drivers/cxl/pci.c                       |  44 ++
 drivers/cxl/port.c                      |  54 +++
 drivers/pci/Kconfig                     |   3 +
 drivers/pci/Makefile                    |   1 +
 drivers/pci/doe.c                       | 546 ++++++++++++++++++++++++
 drivers/pci/probe.c                     |   2 +-
 include/linux/pci-doe.h                 |  79 ++++
 include/linux/pci_ids.h                 |   1 +
 include/linux/sysfs.h                   |  16 +
 include/uapi/linux/pci_regs.h           |  29 +-
 18 files changed, 1063 insertions(+), 2 deletions(-)
 create mode 100644 drivers/cxl/cdat.h
 create mode 100644 drivers/pci/doe.c
 create mode 100644 include/linux/pci-doe.h

base-commit: b060edfd8cdd52bc8648392500bf152a8dd6d4c5

#22 lore raw
Quoted 62 lines · Jonathan Cameron
Quoted 52 lines · On Thu, 14 Jul 2022 20:04:17 -0700 [email protected] wrote:

From: Ira Weiny <[email protected]>

Details of changes are in the individual patches.

Major changes from V13:[10]
Dan minor updates
Willy's suggestion of documentation is good but I'm deferring it until we get the location of the PCI mailboxes settled.
Drop retry CDAT patch
Drop DSMAS patch
Rebased on latest cxl-pending

CXL drivers need various data which are provided through generic DOE mailboxes as defined in the PCIe 6.0 spec.[1]

One such data is the Coherent Device Attribute Table (CDAT). CDAT data provides coherent information about the various devices in the system. It was developed because systems no longer have a priori knowledge of all coherent devices within a system. CDAT describes the coherent characteristics of the components on the CXL bus separate from system configurations. The OS can then, for example, use this information to form correct interleave sets.

To begin reading the CDAT the OS must have support to access the DOE mailboxes provided by the CXL devices.

Because DOE is not specific to DOE but is provided within the PCI spec, the series adds PCI DOE capability library functions. These functions allow for the iteration of the DOE capabilities on a device as well as creating pci_doe_mb structures which can control the operation of the DOE state machine.

For now the iteration of and storage of the DOE mailboxes is done on memdev objects within the CXL stack. When this is needed in more generic code this can be lifted later.

This work was tested using qemu.

[0] https://lore.kernel.org/linux-cxl/[email protected]/
[1] https://pcisig.com/specifications
[2] https://lore.kernel.org/qemu-devel/[email protected]/
[3] https://lore.kernel.org/linux-cxl/[email protected]/
[4] https://lore.kernel.org/linux-cxl/[email protected]/
[5] https://lore.kernel.org/linux-cxl/[email protected]/
[6] https://lore.kernel.org/linux-cxl/[email protected]/
[7] https://lore.kernel.org/linux-cxl/[email protected]/
[8] https://lore.kernel.org/linux-cxl/[email protected]/
[9] https://lore.kernel.org/linux-cxl/[email protected]/
[10] https://lore.kernel.org/linux-cxl/[email protected]/

Previous changes
================

Quoted 6 lines

Changes from V12:[9]
A couple of bug fixes in the new XArray stuff
Remove the IRQ support because I did not realize how that worked and it was complicating things.
Remove busy retries and replace with an error as there is no good way to ensure it will work.

Quoted 5 lines

This is fine for userspace access, but I think we probably will want retries once we are using it in kernel. Whilst we'd not expect it to be common as per (very late) reply I sent to v13 discussion, the CDAT table can change all on it's own (as far as software can see). I'd expect it to be a once in a blue moon thing though.

It had better not change outside an explicit remap of the DPA space via a command like set-partition with the immediate flag set... or maybe after a firmware update. Anything is just unsupportable and broken and the vendor of a device that changes the CDAT without the OS asking for the change gets to keep the pieces.