Skip to contentSkip to conversation

CXL: Read CDAT and DSMAS data

PATCH Superseded [email protected] 4 participants 24 messages Jun 5, 2022 – Jun 20, 2022

Subsystems (2)

v10 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

v10 cover CXL: Read CDAT and DSMAS data

From: Ira Weiny <[email protected]>

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

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 Atribute 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.

CXL iterates the DOE capabilities creates auxiliary bus devices. These devices are driven by a CXL DOE auxiliary driver which calls into the PCI DOE library functions as appropriate.

The auxiliary bus architecture allows for root users to control which DOE mailboxes are controlled by the kernel and which should be allowed for unrestricted access by user space. One such use case is to allow for CXL Compliance Testing (CXL 2.0 14.16.4 Compliance Mode DOE). By default the kernel controls all mailboxes found.

After the devices are created and the driver attaches, CDAT data is read from the device and DSMAS information parsed from that CDAT blob for use later.

This work was tested using qemu with additional patches.

[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]/

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

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 (6):
PCI: Replace magic constant for PCI Sig Vendor ID cxl/pci: Create PCI DOE mailbox's for memory devices cxl/port: Find a DOE mailbox which supports CDAT cxl/port: Introduce cxl_cdat_valid() cxl/port: Retry reading CDAT on failure cxl/port: Parse out DSMAS data from CDAT table

Jonathan Cameron (3):
PCI: Add vendor ID for the PCI SIG
PCI: Create PCI library functions in support of DOE mailboxes. cxl/port: Read CDAT table

 drivers/cxl/Kconfig           |   1 +
 drivers/cxl/cdat.h            | 118 ++++++
 drivers/cxl/core/pci.c        | 280 ++++++++++++++
 drivers/cxl/cxl.h             |   7 +
 drivers/cxl/cxlmem.h          |  10 +
 drivers/cxl/cxlpci.h          |   3 +
 drivers/cxl/mem.c             |   1 +
 drivers/cxl/pci.c             | 114 ++++++
 drivers/cxl/port.c            |  52 +++
 drivers/pci/Kconfig           |   3 +
 drivers/pci/Makefile          |   1 +
 drivers/pci/doe.c             | 693 ++++++++++++++++++++++++++++++++++
 drivers/pci/probe.c           |   2 +-
 include/linux/pci-doe.h       |  65 ++++
 include/linux/pci_ids.h       |   1 +
 include/uapi/linux/pci_regs.h |  29 +-
 16 files changed, 1378 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
Signature · 1 line
2.35.1
#1 lore raw

PATCH v10 1/9 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.1
#2 lore raw

PATCH v10 2/9 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.1
#3 lore raw

PATCH v10 3/9 PCI: Create PCI library functions in support of DOE mailboxes.

In reply to [email protected]

From: Jonathan Cameron <[email protected]>

Introduced in a PCI 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 PCI functionality to manage a single PCI DOE mailbox at a defined config space offset. Functionality includes iterating, creating, query of supported protocol, task submission, and destruction of the mailboxes.

If interrupts are desired, the interrupt number can be queried and passed to the create function. Passing a negative value disables interrupts for that mailbox. It is the callers responsibility to ensure enough interrupt vectors are allocated.

---
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

---
 drivers/pci/Kconfig           |   3 +
 drivers/pci/Makefile          |   1 +
 drivers/pci/doe.c             | 693 ++++++++++++++++++++++++++++++++++
 include/linux/pci-doe.h       |  65 ++++
 include/uapi/linux/pci_regs.h |  29 +-
 5 files changed, 790 insertions(+), 1 deletion(-)
 create mode 100644 drivers/pci/doe.c
 create mode 100644 include/linux/pci-doe.h
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 +693 -0

693 lines not on this page · raw message

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

65 lines not on this page · raw message

include/uapi/linux/pci_regs.h +28 -1
index bee1a9ed6e66..4e96b45ee36d 100644
@@ -736,7 +736,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
@@ -1102,4 +1103,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			0x00000001  /* Interrupt Support */
+#define  PCI_DOE_CAP_IRQ			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.1
#4 lore raw

RE: [PATCH V10 3/9] PCI: Create PCI library functions in support of DOE mailboxes.

Quoted 14 lines

From: [email protected] <[email protected]>
Sent: Sunday, June 5, 2022 8:51 AM
...

drivers/pci/doe.c new file +11 -0
+static void retire_cur_task(struct pci_doe_mb *doe_mb) {
+	spin_lock(&doe_mb->task_lock);
+	doe_mb->cur_task = NULL;
+	spin_unlock(&doe_mb->task_lock);
+	wake_up_interruptible(&doe_mb->wq);
+}
+
+static void doe_statemachine_work(struct work_struct *work) {
+	struct delayed_work *w = to_delayed_work(work);
+	struct pci_doe_mb *doe_mb = container_of(w, struct pci_doe_mb,
+						 statemachine);

drivers/pci/doe.c line 262

Quoted 10 lines
drivers/pci/doe.c new file +10 -0
+	struct pci_dev *pdev = doe_mb->pdev;
+	int offset = doe_mb->cap_offset;
+	enum pci_doe_state prev_state;
+	struct pci_doe_task *task;
+	u32 val;
+	int rc;
+
+	spin_lock(&doe_mb->task_lock);
+	task = doe_mb->cur_task;
+	spin_unlock(&doe_mb->task_lock);

I don't think it needs the lock protection here.
No matter "task" is !NULL or NULL, it is checked before it's used within this function.

Quoted 133 lines
drivers/pci/doe.c new file +24 -0
+
+	if (test_and_clear_bit(PCI_DOE_FLAG_ABORT, &doe_mb->flags)) {
+		/*
+		 * Currently only used during init - care needed if
+		 * pci_doe_abort() is generally exposed as it would impact
+		 * queries in flight.
+		 */
+		if (task)
+			pci_err(pdev, "DOE [%x] Aborting with active task!\n",
+				doe_mb->cap_offset);
+		doe_mb->state = DOE_WAIT_ABORT;
+		pci_doe_abort_start(doe_mb);
+		return;
+	}
+
+	switch (doe_mb->state) {
+	case DOE_IDLE:
+		if (task == NULL)
+			return;
+
+		rc = pci_doe_send_req(doe_mb, task);
+
+		/*
+		 * The specification does not provide any guidance on how

long

drivers/pci/doe.c new file +8 -0
+		 * some other entity could keep the DOE busy, so try for 1
+		 * second then fail. Busy handling is best effort only, because
+		 * there is no way of avoiding racing against another user of
+		 * the DOE.
+		 */
+		if (rc == -EBUSY) {
+			doe_mb->busy_retries++;
+			if (doe_mb->busy_retries ==

PCI_DOE_BUSY_MAX_RETRIES) {

drivers/pci/doe.c new file +3 -0
+				/* Long enough, fail this request */
+				pci_warn(pdev,
+					"DOE [%x] busy for too long (> 1

sec)\n",

drivers/pci/doe.c new file +5 -0
+					doe_mb->cap_offset);
+				doe_mb->busy_retries = 0;
+				goto err_busy;
+			}
+			schedule_delayed_work(w, HZ /

PCI_DOE_BUSY_MAX_RETRIES);

drivers/pci/doe.c new file +18 -0
+			return;
+		}
+		if (rc)
+			goto err_abort;
+		doe_mb->busy_retries = 0;
+
+		doe_mb->state = DOE_WAIT_RESP;
+		doe_mb->timeout_jiffies = jiffies + HZ;
+		/* Now poll or wait for IRQ with timeout */
+		if (doe_mb->irq >= 0)
+			schedule_delayed_work(w, PCI_DOE_TIMEOUT);
+		else
+			schedule_delayed_work(w, PCI_DOE_POLL_INTERVAL);
+		return;
+
+	case DOE_WAIT_RESP:
+		/* Not possible to get here with NULL task */
+		pci_read_config_dword(pdev, offset + PCI_DOE_STATUS,

&val);

drivers/pci/doe.c new file +6 -0
+		if (FIELD_GET(PCI_DOE_STATUS_ERROR, val)) {
+			rc = -EIO;
+			goto err_abort;
+		}
+
+		if (!FIELD_GET(PCI_DOE_STATUS_DATA_OBJECT_READY, val))

{

(quoted diff) +1 -0
+			/* If not yet at timeout reschedule otherwise abort

*/

drivers/pci/doe.c new file +25 -0
+			if (time_after(jiffies, doe_mb->timeout_jiffies)) {
+				rc = -ETIMEDOUT;
+				goto err_abort;
+			}
+			schedule_delayed_work(w, PCI_DOE_POLL_INTERVAL);
+			return;
+		}
+
+		rc  = pci_doe_recv_resp(doe_mb, task);
+		if (rc < 0)
+			goto err_abort;
+
+		doe_mb->state = DOE_IDLE;
+
+		retire_cur_task(doe_mb);
+		/* Set the return value to the length of received payload */
+		signal_task_complete(task, rc);
+
+		return;
+
+	case DOE_WAIT_ABORT:
+	case DOE_WAIT_ABORT_ON_ERR:
+		prev_state = doe_mb->state;
+
+		pci_read_config_dword(pdev, offset + PCI_DOE_STATUS,

&val);

drivers/pci/doe.c new file +35 -0
+
+		if (!FIELD_GET(PCI_DOE_STATUS_ERROR, val) &&
+		    !FIELD_GET(PCI_DOE_STATUS_BUSY, val)) {
+			doe_mb->state = DOE_IDLE;
+			/* Back to normal state - carry on */
+			retire_cur_task(doe_mb);
+		} else if (time_after(jiffies, doe_mb->timeout_jiffies)) {
+			/* Task has timed out and is dead - abort */
+			pci_err(pdev, "DOE [%x] ABORT timed out\n",
+				doe_mb->cap_offset);
+			set_bit(PCI_DOE_FLAG_DEAD, &doe_mb->flags);
+			retire_cur_task(doe_mb);
+		}
+
+		/*
+		 * For deliberately triggered abort, someone is
+		 * waiting.
+		 */
+		if (prev_state == DOE_WAIT_ABORT) {
+			if (task)
+				signal_task_complete(task, -EFAULT);
+			complete(&doe_mb->abort_c);
+		}
+
+		return;
+	}
+
+err_abort:
+	doe_mb->state = DOE_WAIT_ABORT_ON_ERR;
+	pci_doe_abort_start(doe_mb);
+err_busy:
+	signal_task_complete(task, rc);
+	if (doe_mb->state == DOE_IDLE)
+		retire_cur_task(doe_mb);
+}
author
#5 lore raw

Re: [PATCH V10 3/9] PCI: Create PCI library functions in support of DOE mailboxes.

Quoted 19 lines · Qiuxu
Quoted 19 lines

From: [email protected] <[email protected]>
Sent: Sunday, June 5, 2022 8:51 AM
...

drivers/pci/doe.c new file +16 -0
+static void retire_cur_task(struct pci_doe_mb *doe_mb) {
+	spin_lock(&doe_mb->task_lock);
+	doe_mb->cur_task = NULL;
+	spin_unlock(&doe_mb->task_lock);
+	wake_up_interruptible(&doe_mb->wq);
+}
+
+static void doe_statemachine_work(struct work_struct *work) {
+	struct delayed_work *w = to_delayed_work(work);
+	struct pci_doe_mb *doe_mb = container_of(w, struct pci_doe_mb,
+						 statemachine);
+	struct pci_dev *pdev = doe_mb->pdev;
+	int offset = doe_mb->cap_offset;
+	enum pci_doe_state prev_state;
+	struct pci_doe_task *task;
+	u32 val;
Quoted 8 lines

drivers/pci/doe.c line 262

Quoted 5 lines
drivers/pci/doe.c new file +5 -0
+	int rc;
+
+	spin_lock(&doe_mb->task_lock);
+	task = doe_mb->cur_task;
+	spin_unlock(&doe_mb->task_lock);

I don't think it needs the lock protection here.
No matter "task" is !NULL or NULL, it is checked before it's used within this function.

No it does not.

However, Dan has suggested reworking the workqueue and I think it will eliminate this. I kept the lock more as a marker of where cur_task was being used even though it was not required. The fact that the rest of the function goes on to use a local alias was suspicious but was covered by the workqueue operation. I tried to explain that in the commit message but reworking as Dan has suggested is better overall.

Thanks for the review!

Ira

#6 lore raw

PATCH v10 4/9 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 ports may 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 this is good enough for the endpoints and the split is similar to the envisioned architecture where getting the mailbox array is separated from the various protocol needs. For example, it is not anticipated that the CDAT code will need to move because it is only needed by the cxl_ports.

Likewise irq's are separated out in a similar design pattern to the PCIe port driver. But a much simpler irq enabling flag is used and only DOE interrupts are supported.

Cc Ben Widawsky <[email protected]>

---
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 |   6 +++
 drivers/cxl/pci.c    | 114 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 121 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 +6 -0
index 60d10ee1e7fc..4d2764b865ab 100644
@@ -191,6 +191,8 @@ 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
+ * @num_mbs: Number of DOE mailboxes
  * @mbox_send: @dev specific transport for transmitting mailbox commands
  *
  * See section 8.2.9.5.2 Capacity Configuration and Label Storage for
@@ -224,6 +226,10 @@ struct cxl_dev_state {
 	resource_size_t component_reg_phys;
 	u64 serial;
 
+	bool doe_use_irq;
+	struct pci_doe_mb **doe_mbs;
+	int num_mbs;
+
 	int (*mbox_send)(struct cxl_dev_state *cxlds, struct cxl_mbox_cmd *cmd);
 };
 
drivers/cxl/pci.c +114 -0

132 lines not on this page · raw message

Signature · 1 line
2.35.1
Reviewed-by on v10 4/9
#7 lore raw

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

Quoted 19 lines · On 22-06-04 17:50:44, [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 ports may 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 this is good enough for the endpoints and the split is similar to the envisioned architecture where getting the mailbox array is separated from the various protocol needs. For example, it is not anticipated that the CDAT code will need to move because it is only needed by the cxl_ports.

Quoted 5 lines

Likewise irq's are separated out in a similar design pattern to the PCIe port driver. But a much simpler irq enabling flag is used and only DOE interrupts are supported.

Cc Ben Widawsky <[email protected]>
Quoted 215 lines

---
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 |   6 +++
 drivers/cxl/pci.c    | 114 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 121 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 +6 -0
index 60d10ee1e7fc..4d2764b865ab 100644
@@ -191,6 +191,8 @@ 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
+ * @num_mbs: Number of DOE mailboxes
  * @mbox_send: @dev specific transport for transmitting mailbox commands
  *
  * See section 8.2.9.5.2 Capacity Configuration and Label Storage for
@@ -224,6 +226,10 @@ struct cxl_dev_state {
 	resource_size_t component_reg_phys;
 	u64 serial;
 
+	bool doe_use_irq;
+	struct pci_doe_mb **doe_mbs;
+	int num_mbs;
+
 	int (*mbox_send)(struct cxl_dev_state *cxlds, struct cxl_mbox_cmd *cmd);
 };
 
drivers/cxl/pci.c +114 -0
index 5a0ae46d4989..72c7b535f5df 100644
@@ -8,6 +8,7 @@
 #include <linux/mutex.h>
 #include <linux/list.h>
 #include <linux/pci.h>
+#include <linux/pci-doe.h>
 #include <linux/io.h>
 #include "cxlmem.h"
 #include "cxlpci.h"
@@ -386,6 +387,116 @@ static int cxl_setup_regs(struct pci_dev *pdev, enum cxl_regloc_type type,
 	return rc;
 }
 
+static void cxl_pci_free_irq_vectors(void *data)
+{
+	pci_free_irq_vectors(data);
+}
+
+static void cxl_doe_destroy_mb(void *ds)
+{
+	struct cxl_dev_state *cxlds = ds;
+	int i;
+
+	for (i = 0; i < cxlds->num_mbs; i++) {
+		if (cxlds->doe_mbs[i])
+			pci_doe_destroy_mb(cxlds->doe_mbs[i]);
+	}
+}
+
+static void cxl_alloc_irq_vectors(struct cxl_dev_state *cxlds)
+{
+	struct device *dev = cxlds->dev;
+	struct pci_dev *pdev = to_pci_dev(dev);
+	int max_irqs = 0;
+	int off = 0;
+	int rc;
+
+	/* Account for all the DOE vectors needed */
+	pci_doe_for_each_off(pdev, off) {
+		int irq = pci_doe_get_irq_num(pdev, off);
+
+		if (irq < 0)
+			continue;
+		max_irqs = max(max_irqs, irq + 1);
+	}
+
+	if (!max_irqs)
+		return;
+
+	cxlds->doe_use_irq = false;
+
+	/*
+	 * Allocate enough vectors for the DOE's
+	 */
+	rc = pci_alloc_irq_vectors(pdev, max_irqs, max_irqs, PCI_IRQ_MSI |
+							     PCI_IRQ_MSIX);
+	if (rc != max_irqs) {
+		pci_err(pdev, "Not enough interrupts; use polling\n");
+		/* Some got allocated; clean them up */
+		if (rc > 0)
+			cxl_pci_free_irq_vectors(pdev);
+		return;
+	}
+
+	rc = devm_add_action_or_reset(dev, cxl_pci_free_irq_vectors, pdev);
+	if (rc)
+		return;
+
+	cxlds->doe_use_irq = true;
+}
+
+/**
+ * devm_cxl_pci_create_doe - Scan and set up DOE mailboxes
+ *
+ * @cxlds: The CXL device state
+ */
+static void devm_cxl_pci_create_doe(struct cxl_dev_state *cxlds)
+{
+	struct device *dev = cxlds->dev;
+	struct pci_dev *pdev = to_pci_dev(dev);
+	u16 off = 0;
+	int num_mbs = 0;
+	int rc;
+
+	pci_doe_for_each_off(pdev, off)
+		num_mbs++;
+
+	if (!num_mbs) {
+		pci_dbg(pdev, "0 DOE mailbox's found\n");
+		return;
+	}
+
+	cxlds->doe_mbs = devm_kcalloc(dev, num_mbs, sizeof(*cxlds->doe_mbs),
+				      GFP_KERNEL);
+	if (!cxlds->doe_mbs)
+		return;
+
+	pci_doe_for_each_off(pdev, off) {
+		struct pci_doe_mb *doe_mb;
+		int irq = -1;
+
+		if (cxlds->doe_use_irq)
+			irq = pci_doe_get_irq_num(pdev, off);
+
+		doe_mb = pci_doe_create_mb(pdev, off, irq);
+		if (IS_ERR(doe_mb)) {
+			pci_err(pdev,
+				"Failed to create MB object for MB @ %x\n",
+				off);
+			doe_mb = NULL;
+		}
+
+		cxlds->doe_mbs[cxlds->num_mbs] = doe_mb;
+		cxlds->num_mbs++;
+	}
+
+	rc = devm_add_action_or_reset(dev, cxl_doe_destroy_mb, cxlds);
+	if (rc)
+		return;
+
+	pci_info(pdev, "Configured %d DOE mailbox's\n", cxlds->num_mbs);
+}
+
 static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 {
 	struct cxl_register_map map;
@@ -434,6 +545,9 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 
 	cxlds->component_reg_phys = cxl_regmap_to_base(pdev, &map);
 
+	cxl_alloc_irq_vectors(cxlds);
+	devm_cxl_pci_create_doe(cxlds);
+
 	rc = cxl_pci_setup_mailbox(cxlds);
 	if (rc)
 		return rc;
Signature · 1 line
2.35.1
#8 lore raw

PATCH v10 5/9 cxl/port: Find a DOE mailbox which supports CDAT

In reply to [email protected]

From: Ira Weiny <[email protected]>

Each CXL device may have multiple DOE mailbox capabilities and each mailbox may support multiple protocols. CXL port devices need to query the CDAT information specifically.

Search the DOE mailboxes for one which supports the CDAT protocol. Cache that mailbox to be used for future queries.

Only support memory devices at this time.

Cc Ben Widawsky <[email protected]>
Cc Jonathan Cameron <[email protected]>

---
Changes from V9
Ben Widawsky
s/cxl_find_cdat_mb/cxl_cache_cdat_mb/; add kdoc
Jonathan Cameron
Move cache_cdat to port probe [Not 100% necessary but it goes along with reading the cdat data.]

Changes from V8
Incorporate feedback from Jonathan
Move all this to the cxl_port object

Changes from V7
Minor code clean ups

Changes from V6
Adjust for aux devices being a CXL only concept Update commit msg.
Ensure devices iterated by auxiliary_find_device() are checked
to be DOE devices prior to checking for the CDAT protocol
From Ben
Ensure reference from auxiliary_find_device() is dropped

---
 drivers/cxl/core/pci.c | 35 +++++++++++++++++++++++++++++++++++
 drivers/cxl/cxl.h      |  2 ++
 drivers/cxl/cxlpci.h   |  1 +
 drivers/cxl/port.c     |  2 ++
 4 files changed, 40 insertions(+)
drivers/cxl/core/pci.c +35 -0
index c4c99ff7b55e..d814d8317975 100644
@@ -4,11 +4,14 @@
 #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"
 
+#define CXL_DOE_PROTOCOL_TABLE_ACCESS 2
+
 /**
  * DOC: cxl core pci
  *
@@ -458,3 +461,35 @@ 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);
+
+/**
+ * cxl_cache_cdat_mb() -- cache the DOE mailbox which suports the CDAT protocol
+ *
+ * @port: Port to containing DOE Mailboxes
+ *
+ * Cache a pointer to the doe mailbox which supports CDAT.
+ */
+void cxl_cache_cdat_mb(struct cxl_port *port)
+{
+	struct device *dev = port->uport;
+	struct cxl_memdev *cxlmd;
+	struct cxl_dev_state *cxlds;
+	int i;
+
+	if (!is_cxl_memdev(dev))
+		return;
+
+	cxlmd = to_cxl_memdev(dev);
+	cxlds = cxlmd->cxlds;
+
+	for (i = 0; i < cxlds->num_mbs; i++) {
+		struct pci_doe_mb *cur = cxlds->doe_mbs[i];
+
+		if (pci_doe_supports_prot(cur, PCI_DVSEC_VENDOR_ID_CXL,
+					  CXL_DOE_PROTOCOL_TABLE_ACCESS)) {
+			port->cdat_mb = cur;
+			return;
+		}
+	}
+}
+EXPORT_SYMBOL_NS_GPL(cxl_cache_cdat_mb, CXL);
drivers/cxl/cxl.h +2 -0
index 140dc3278cde..0a86be589ffc 100644
@@ -267,6 +267,7 @@ 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_mb: Mailbox which supports the CDAT protocol
  */
 struct cxl_port {
 	struct device dev;
@@ -278,6 +279,7 @@ struct cxl_port {
 	resource_size_t component_reg_phys;
 	bool dead;
 	unsigned int depth;
+	struct pci_doe_mb *cdat_mb;
 };
 
 /**
drivers/cxl/cxlpci.h +1 -0
index fce1c11729c2..ddbb8b77752e 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 cxl_cache_cdat_mb(struct cxl_port *port);
 #endif /* __CXL_PCI_H__ */
drivers/cxl/port.c +2 -0
index 3cf308f114c4..04f3d1fc6e07 100644
@@ -49,6 +49,8 @@ static int cxl_port_probe(struct device *dev)
 	if (IS_ERR(cxlhdm))
 		return PTR_ERR(cxlhdm);
 
+	cxl_cache_cdat_mb(port);
+
 	if (is_cxl_endpoint(port)) {
 		struct cxl_memdev *cxlmd = to_cxl_memdev(port->uport);
 		struct cxl_dev_state *cxlds = cxlmd->cxlds;
Signature · 1 line
2.35.1
#9 lore raw

Re: [PATCH V10 5/9] cxl/port: Find a DOE mailbox which supports CDAT

Quoted 85 lines · On 22-06-04 17:50:45, [email protected] wrote:

From: Ira Weiny <[email protected]>

Each CXL device may have multiple DOE mailbox capabilities and each mailbox may support multiple protocols. CXL port devices need to query the CDAT information specifically.

Search the DOE mailboxes for one which supports the CDAT protocol. Cache that mailbox to be used for future queries.

Only support memory devices at this time.

Cc Ben Widawsky <[email protected]>
Cc Jonathan Cameron <[email protected]>

---
Changes from V9
Ben Widawsky
s/cxl_find_cdat_mb/cxl_cache_cdat_mb/; add kdoc
Jonathan Cameron
Move cache_cdat to port probe [Not 100% necessary but it goes along with reading the cdat data.]

Changes from V8
Incorporate feedback from Jonathan
Move all this to the cxl_port object

Changes from V7
Minor code clean ups

Changes from V6
Adjust for aux devices being a CXL only concept Update commit msg.
Ensure devices iterated by auxiliary_find_device() are checked
to be DOE devices prior to checking for the CDAT protocol
From Ben
Ensure reference from auxiliary_find_device() is dropped

---
 drivers/cxl/core/pci.c | 35 +++++++++++++++++++++++++++++++++++
 drivers/cxl/cxl.h      |  2 ++
 drivers/cxl/cxlpci.h   |  1 +
 drivers/cxl/port.c     |  2 ++
 4 files changed, 40 insertions(+)
drivers/cxl/core/pci.c +20 -0
index c4c99ff7b55e..d814d8317975 100644
@@ -4,11 +4,14 @@
 #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"
 
+#define CXL_DOE_PROTOCOL_TABLE_ACCESS 2
+
 /**
  * DOC: cxl core pci
  *
@@ -458,3 +461,35 @@ 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);
+
+/**
+ * cxl_cache_cdat_mb() -- cache the DOE mailbox which suports the CDAT protocol
+ *
+ * @port: Port to containing DOE Mailboxes
+ *
+ * Cache a pointer to the doe mailbox which supports CDAT.
+ */
+void cxl_cache_cdat_mb(struct cxl_port *port)
+{
+	struct device *dev = port->uport;
+	struct cxl_memdev *cxlmd;
+	struct cxl_dev_state *cxlds;
+	int i;
+
+	if (!is_cxl_memdev(dev))
+		return;

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

Quoted 10 lines
drivers/cxl/core/pci.c +10 -0
+
+	cxlmd = to_cxl_memdev(dev);
+	cxlds = cxlmd->cxlds;
+
+	for (i = 0; i < cxlds->num_mbs; i++) {
+		struct pci_doe_mb *cur = cxlds->doe_mbs[i];
+
+		if (pci_doe_supports_prot(cur, PCI_DVSEC_VENDOR_ID_CXL,
+					  CXL_DOE_PROTOCOL_TABLE_ACCESS)) {
+			port->cdat_mb = cur;

What happens if cxl_pci is unloaded after this? Would it be better to copy out the CDAT info? Otherwise, I think you need to hold a ref on the PCI device (though I only took a quick look).

Quoted 51 lines
drivers/cxl/core/pci.c +5 -0
+			return;
+		}
+	}
+}
+EXPORT_SYMBOL_NS_GPL(cxl_cache_cdat_mb, CXL);
drivers/cxl/cxl.h +2 -0
index 140dc3278cde..0a86be589ffc 100644
@@ -267,6 +267,7 @@ 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_mb: Mailbox which supports the CDAT protocol
  */
 struct cxl_port {
 	struct device dev;
@@ -278,6 +279,7 @@ struct cxl_port {
 	resource_size_t component_reg_phys;
 	bool dead;
 	unsigned int depth;
+	struct pci_doe_mb *cdat_mb;
 };
 
 /**
drivers/cxl/cxlpci.h +1 -0
index fce1c11729c2..ddbb8b77752e 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 cxl_cache_cdat_mb(struct cxl_port *port);
 #endif /* __CXL_PCI_H__ */
drivers/cxl/port.c +2 -0
index 3cf308f114c4..04f3d1fc6e07 100644
@@ -49,6 +49,8 @@ static int cxl_port_probe(struct device *dev)
 	if (IS_ERR(cxlhdm))
 		return PTR_ERR(cxlhdm);
 
+	cxl_cache_cdat_mb(port);
+
 	if (is_cxl_endpoint(port)) {
 		struct cxl_memdev *cxlmd = to_cxl_memdev(port->uport);
 		struct cxl_dev_state *cxlds = cxlmd->cxlds;
Signature · 1 line
2.35.1
author
#10 lore raw

Re: [PATCH V10 5/9] cxl/port: Find a DOE mailbox which supports CDAT

Quoted 92 lines · Ben Widawsky
Quoted 91 lines · On 22-06-04 17:50:45, [email protected] wrote:

From: Ira Weiny <[email protected]>

Each CXL device may have multiple DOE mailbox capabilities and each mailbox may support multiple protocols. CXL port devices need to query the CDAT information specifically.

Search the DOE mailboxes for one which supports the CDAT protocol. Cache that mailbox to be used for future queries.

Only support memory devices at this time.

Cc Ben Widawsky <[email protected]>
Cc Jonathan Cameron <[email protected]>

---
Changes from V9
Ben Widawsky
s/cxl_find_cdat_mb/cxl_cache_cdat_mb/; add kdoc
Jonathan Cameron
Move cache_cdat to port probe [Not 100% necessary but it goes along with reading the cdat data.]

Changes from V8
Incorporate feedback from Jonathan
Move all this to the cxl_port object

Changes from V7
Minor code clean ups

Changes from V6
Adjust for aux devices being a CXL only concept Update commit msg.
Ensure devices iterated by auxiliary_find_device() are checked
to be DOE devices prior to checking for the CDAT protocol
From Ben
Ensure reference from auxiliary_find_device() is dropped

---
 drivers/cxl/core/pci.c | 35 +++++++++++++++++++++++++++++++++++
 drivers/cxl/cxl.h      |  2 ++
 drivers/cxl/cxlpci.h   |  1 +
 drivers/cxl/port.c     |  2 ++
 4 files changed, 40 insertions(+)
drivers/cxl/core/pci.c +26 -0
index c4c99ff7b55e..d814d8317975 100644
@@ -4,11 +4,14 @@
 #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"
 
+#define CXL_DOE_PROTOCOL_TABLE_ACCESS 2
+
 /**
  * DOC: cxl core pci
  *
@@ -458,3 +461,35 @@ 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);
+
+/**
+ * cxl_cache_cdat_mb() -- cache the DOE mailbox which suports the CDAT protocol
+ *
+ * @port: Port to containing DOE Mailboxes
+ *
+ * Cache a pointer to the doe mailbox which supports CDAT.
+ */
+void cxl_cache_cdat_mb(struct cxl_port *port)
+{
+	struct device *dev = port->uport;
+	struct cxl_memdev *cxlmd;
+	struct cxl_dev_state *cxlds;
+	int i;
+
+	if (!is_cxl_memdev(dev))
+		return;
+
+	cxlmd = to_cxl_memdev(dev);
+	cxlds = cxlmd->cxlds;
+
+	for (i = 0; i < cxlds->num_mbs; i++) {
+		struct pci_doe_mb *cur = cxlds->doe_mbs[i];
Quoted 8 lines

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

Quoted 4 lines
drivers/cxl/core/pci.c +4 -0
+
+		if (pci_doe_supports_prot(cur, PCI_DVSEC_VENDOR_ID_CXL,
+					  CXL_DOE_PROTOCOL_TABLE_ACCESS)) {
+			port->cdat_mb = cur;

What happens if cxl_pci is unloaded after this? Would it be better to copy out the CDAT info? Otherwise, I think you need to hold a ref on the PCI device (though I only took a quick look).

<sigh> I thought that could not happen but I see I was wrong.

A reference will need to be taken for at least the duration of the query. Originally I had this set up to try and make it easier to query later. But I think that is a waste ATM.

I'm going to rework this ownership.

Thanks for the review,
Ira

Quoted 52 lines
Quoted 52 lines
drivers/cxl/core/pci.c +5 -0
+			return;
+		}
+	}
+}
+EXPORT_SYMBOL_NS_GPL(cxl_cache_cdat_mb, CXL);
drivers/cxl/cxl.h +2 -0
index 140dc3278cde..0a86be589ffc 100644
@@ -267,6 +267,7 @@ 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_mb: Mailbox which supports the CDAT protocol
  */
 struct cxl_port {
 	struct device dev;
@@ -278,6 +279,7 @@ struct cxl_port {
 	resource_size_t component_reg_phys;
 	bool dead;
 	unsigned int depth;
+	struct pci_doe_mb *cdat_mb;
 };
 
 /**
drivers/cxl/cxlpci.h +1 -0
index fce1c11729c2..ddbb8b77752e 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 cxl_cache_cdat_mb(struct cxl_port *port);
 #endif /* __CXL_PCI_H__ */
drivers/cxl/port.c +2 -0
index 3cf308f114c4..04f3d1fc6e07 100644
@@ -49,6 +49,8 @@ static int cxl_port_probe(struct device *dev)
 	if (IS_ERR(cxlhdm))
 		return PTR_ERR(cxlhdm);
 
+	cxl_cache_cdat_mb(port);
+
 	if (is_cxl_endpoint(port)) {
 		struct cxl_memdev *cxlmd = to_cxl_memdev(port->uport);
 		struct cxl_dev_state *cxlds = cxlmd->cxlds;
Signature · 1 line
2.35.1
author
#11 lore raw

Re: [PATCH V10 5/9] cxl/port: Find a DOE mailbox which supports CDAT

Quoted 4 lines · Ira
Quoted 3 lines · Ben Widawsky
Quoted 2 lines · On 22-06-04 17:50:45, [email protected] wrote:

From: Ira Weiny <[email protected]>

[snip]

Quoted 32 lines
Quoted 23 lines
Quoted 23 lines
drivers/cxl/core/pci.c +23 -0
+
+/**
+ * cxl_cache_cdat_mb() -- cache the DOE mailbox which suports the CDAT protocol
+ *
+ * @port: Port to containing DOE Mailboxes
+ *
+ * Cache a pointer to the doe mailbox which supports CDAT.
+ */
+void cxl_cache_cdat_mb(struct cxl_port *port)
+{
+	struct device *dev = port->uport;
+	struct cxl_memdev *cxlmd;
+	struct cxl_dev_state *cxlds;
+	int i;
+
+	if (!is_cxl_memdev(dev))
+		return;
+
+	cxlmd = to_cxl_memdev(dev);
+	cxlds = cxlmd->cxlds;
+
+	for (i = 0; i < cxlds->num_mbs; i++) {
+		struct pci_doe_mb *cur = cxlds->doe_mbs[i];
Quoted 8 lines

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

Quoted 4 lines
drivers/cxl/core/pci.c +4 -0
+
+		if (pci_doe_supports_prot(cur, PCI_DVSEC_VENDOR_ID_CXL,
+					  CXL_DOE_PROTOCOL_TABLE_ACCESS)) {
+			port->cdat_mb = cur;

What happens if cxl_pci is unloaded after this? Would it be better to copy out the CDAT info? Otherwise, I think you need to hold a ref on the PCI device (though I only took a quick look).

Quoted 7 lines

<sigh> I thought that could not happen but I see I was wrong.

A reference will need to be taken for at least the duration of the query. Originally I had this set up to try and make it easier to query later. But I think that is a waste ATM.

I'm going to rework this ownership.

BTW this means this patch is going away. I think just searching for the mailbox with CDAT support on each query is the way to go at this time.

Ira

Quoted 4 lines

Thanks for the review,
Ira

#12 lore raw

PATCH v10 6/9 cxl/port: Read CDAT table

In reply to [email protected]

From: Jonathan Cameron <[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.

Cache the CDAT 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 DOE defines within cdat.h for code wishing to decode the CDAT table.

---
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

---
 drivers/cxl/cdat.h     |  99 ++++++++++++++++++++++++++++++
 drivers/cxl/core/pci.c | 136 ++++++++++++++++++++++++++++++++++++++++-
 drivers/cxl/cxl.h      |   3 +
 drivers/cxl/cxlpci.h   |   1 +
 drivers/cxl/port.c     |  50 +++++++++++++++
 5 files changed, 287 insertions(+), 2 deletions(-)
 create mode 100644 drivers/cxl/cdat.h
drivers/cxl/cdat.h new file +99 -0
new file mode 100644
index 000000000000..f5193a6a51fe
@@ -0,0 +1,99 @@
+/* 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
+
+/*
+ * 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
+
+/* Device Scoped Memory Affinity Structure */
+#define CDAT_DSMAS_DW1_DSMAD_HANDLE	0x000000ff
+#define CDAT_DSMAS_DW1_FLAGS		0x0000ff00
+#define CDAT_DSMAS_DPA_OFFSET(entry) ((u64)((entry)[3]) << 32 | (entry)[2])
+#define CDAT_DSMAS_DPA_LEN(entry) ((u64)((entry)[5]) << 32 | (entry)[4])
+#define CDAT_DSMAS_NON_VOLATILE(flags)  ((flags & 0x04) >> 2)
+
+/* Device Scoped Latency and Bandwidth Information Structure */
+#define CDAT_DSLBIS_DW1_HANDLE		0x000000ff
+#define CDAT_DSLBIS_DW1_FLAGS		0x0000ff00
+#define CDAT_DSLBIS_DW1_DATA_TYPE	0x00ff0000
+#define CDAT_DSLBIS_BASE_UNIT(entry) ((u64)((entry)[3]) << 32 | (entry)[2])
+#define CDAT_DSLBIS_DW4_ENTRY_0		0x0000ffff
+#define CDAT_DSLBIS_DW4_ENTRY_1		0xffff0000
+#define CDAT_DSLBIS_DW5_ENTRY_2		0x0000ffff
+
+/* Device Scoped Memory Side Cache Information Structure */
+#define CDAT_DSMSCIS_DW1_HANDLE		0x000000ff
+#define CDAT_DSMSCIS_MEMORY_SIDE_CACHE_SIZE(entry) \
+	((u64)((entry)[3]) << 32 | (entry)[2])
+#define CDAT_DSMSCIS_DW4_MEMORY_SIDE_CACHE_ATTRS 0xffffffff
+
+/* Device Scoped Initiator Structure */
+#define CDAT_DSIS_DW1_FLAGS		0x000000ff
+#define CDAT_DSIS_DW1_HANDLE		0x0000ff00
+
+/* Device Scoped EFI Memory Type Structure */
+#define CDAT_DSEMTS_DW1_HANDLE		0x000000ff
+#define CDAT_DSEMTS_DW1_EFI_MEMORY_TYPE_ATTR	0x0000ff00
+#define CDAT_DSEMTS_DPA_OFFSET(entry)	((u64)((entry)[3]) << 32 | (entry)[2])
+#define CDAT_DSEMTS_DPA_LENGTH(entry)	((u64)((entry)[5]) << 32 | (entry)[4])
+
+/* Switch Scoped Latency and Bandwidth Information Structure */
+#define CDAT_SSLBIS_DW1_DATA_TYPE	0x000000ff
+#define CDAT_SSLBIS_BASE_UNIT(entry)	((u64)((entry)[3]) << 32 | (entry)[2])
+#define CDAT_SSLBIS_ENTRY_PORT_X(entry, i) ((entry)[4 + (i) * 2] & 0x0000ffff)
+#define CDAT_SSLBIS_ENTRY_PORT_Y(entry, i) (((entry)[4 + (i) * 2] & 0xffff0000) >> 16)
+#define CDAT_SSLBIS_ENTRY_LAT_OR_BW(entry, i) ((entry)[4 + (i) * 2 + 1] & 0x0000ffff)
+
+#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 +134 -2

145 lines not on this page · raw message

drivers/cxl/cxl.h +3 -0
index 0a86be589ffc..531b77d296c7 100644
@@ -8,6 +8,7 @@
 #include <linux/bitfield.h>
 #include <linux/bitops.h>
 #include <linux/io.h>
+#include "cdat.h"
 
 /**
  * DOC: cxl objects
@@ -268,6 +269,7 @@ struct cxl_nvdimm {
  * @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_mb: Mailbox which supports the CDAT protocol
+ * @cdat: Cached CDAT data
  */
 struct cxl_port {
 	struct device dev;
@@ -280,6 +282,7 @@ struct cxl_port {
 	bool dead;
 	unsigned int depth;
 	struct pci_doe_mb *cdat_mb;
+	struct cxl_cdat cdat;
 };
 
 /**
drivers/cxl/cxlpci.h +1 -0
index ddbb8b77752e..71009a167a92 100644
@@ -75,4 +75,5 @@ 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 cxl_cache_cdat_mb(struct cxl_port *port);
+void read_cdat_data(struct cxl_port *port);
 #endif /* __CXL_PCI_H__ */
drivers/cxl/port.c +50 -0

66 lines not on this page · raw message

Signature · 1 line
2.35.1
#13 lore raw

Re: [PATCH V10 6/9] cxl/port: Read CDAT table

Quoted 216 lines · On 22-06-04 17:50:46, [email protected] wrote:

From: Jonathan Cameron <[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.

Cache the CDAT 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 DOE defines within cdat.h for code wishing to decode the CDAT table.

---
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

---
 drivers/cxl/cdat.h     |  99 ++++++++++++++++++++++++++++++
 drivers/cxl/core/pci.c | 136 ++++++++++++++++++++++++++++++++++++++++-
 drivers/cxl/cxl.h      |   3 +
 drivers/cxl/cxlpci.h   |   1 +
 drivers/cxl/port.c     |  50 +++++++++++++++
 5 files changed, 287 insertions(+), 2 deletions(-)
 create mode 100644 drivers/cxl/cdat.h
drivers/cxl/cdat.h new file +99 -0
new file mode 100644
index 000000000000..f5193a6a51fe
@@ -0,0 +1,99 @@
+/* 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
+
+/*
+ * 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
+
+/* Device Scoped Memory Affinity Structure */
+#define CDAT_DSMAS_DW1_DSMAD_HANDLE	0x000000ff
+#define CDAT_DSMAS_DW1_FLAGS		0x0000ff00
+#define CDAT_DSMAS_DPA_OFFSET(entry) ((u64)((entry)[3]) << 32 | (entry)[2])
+#define CDAT_DSMAS_DPA_LEN(entry) ((u64)((entry)[5]) << 32 | (entry)[4])
+#define CDAT_DSMAS_NON_VOLATILE(flags)  ((flags & 0x04) >> 2)
+
+/* Device Scoped Latency and Bandwidth Information Structure */
+#define CDAT_DSLBIS_DW1_HANDLE		0x000000ff
+#define CDAT_DSLBIS_DW1_FLAGS		0x0000ff00
+#define CDAT_DSLBIS_DW1_DATA_TYPE	0x00ff0000
+#define CDAT_DSLBIS_BASE_UNIT(entry) ((u64)((entry)[3]) << 32 | (entry)[2])
+#define CDAT_DSLBIS_DW4_ENTRY_0		0x0000ffff
+#define CDAT_DSLBIS_DW4_ENTRY_1		0xffff0000
+#define CDAT_DSLBIS_DW5_ENTRY_2		0x0000ffff
+
+/* Device Scoped Memory Side Cache Information Structure */
+#define CDAT_DSMSCIS_DW1_HANDLE		0x000000ff
+#define CDAT_DSMSCIS_MEMORY_SIDE_CACHE_SIZE(entry) \
+	((u64)((entry)[3]) << 32 | (entry)[2])
+#define CDAT_DSMSCIS_DW4_MEMORY_SIDE_CACHE_ATTRS 0xffffffff
+
+/* Device Scoped Initiator Structure */
+#define CDAT_DSIS_DW1_FLAGS		0x000000ff
+#define CDAT_DSIS_DW1_HANDLE		0x0000ff00
+
+/* Device Scoped EFI Memory Type Structure */
+#define CDAT_DSEMTS_DW1_HANDLE		0x000000ff
+#define CDAT_DSEMTS_DW1_EFI_MEMORY_TYPE_ATTR	0x0000ff00
+#define CDAT_DSEMTS_DPA_OFFSET(entry)	((u64)((entry)[3]) << 32 | (entry)[2])
+#define CDAT_DSEMTS_DPA_LENGTH(entry)	((u64)((entry)[5]) << 32 | (entry)[4])
+
+/* Switch Scoped Latency and Bandwidth Information Structure */
+#define CDAT_SSLBIS_DW1_DATA_TYPE	0x000000ff
+#define CDAT_SSLBIS_BASE_UNIT(entry)	((u64)((entry)[3]) << 32 | (entry)[2])
+#define CDAT_SSLBIS_ENTRY_PORT_X(entry, i) ((entry)[4 + (i) * 2] & 0x0000ffff)
+#define CDAT_SSLBIS_ENTRY_PORT_Y(entry, i) (((entry)[4 + (i) * 2] & 0xffff0000) >> 16)
+#define CDAT_SSLBIS_ENTRY_LAT_OR_BW(entry, i) ((entry)[4 + (i) * 2 + 1] & 0x0000ffff)
+
+#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 +19 -2
index d814d8317975..76fa8382b3c7 100644
@@ -9,8 +9,7 @@
 #include <cxlmem.h>
 #include <cxl.h>
 #include "core.h"
-
-#define CXL_DOE_PROTOCOL_TABLE_ACCESS 2
+#include "cdat.h"
 
 /**
  * DOC: cxl core pci
@@ -493,3 +492,136 @@ void cxl_cache_cdat_mb(struct cxl_port *port)
 	}
 }
 EXPORT_SYMBOL_NS_GPL(cxl_cache_cdat_mb, CXL);
+
+#define CDAT_DOE_REQ(entry_handle)					\
+	(FIELD_PREP(CXL_DOE_TABLE_ACCESS_REQ_CODE,			\
+		    CXL_DOE_TABLE_ACCESS_REQ_CODE_READ) |		\
+	 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);
+}
+
+static int cxl_cdat_get_length(struct cxl_port *port, size_t *length)
+{
+	u32 cdat_request_pl = CDAT_DOE_REQ(0);
+	u32 cdat_response_pl[32];
+	DECLARE_COMPLETION_ONSTACK(c);

drivers/cxl/core/pci.c void cxl_cache_cdat_mb(struct cxl_port *port) · line 522

Quoted 10 lines
drivers/cxl/core/pci.c +10 -0
+	struct pci_doe_task task = {
+		.prot.vid = PCI_DVSEC_VENDOR_ID_CXL,
+		.prot.type = CXL_DOE_PROTOCOL_TABLE_ACCESS,
+		.request_pl = &cdat_request_pl,
+		.request_pl_sz = sizeof(cdat_request_pl),
+		.response_pl = cdat_response_pl,
+		.response_pl_sz = sizeof(cdat_response_pl),
+		.complete = cxl_doe_task_complete,
+		.private = &c,
+	};

This is looking like something that could be nicely populated with a macro.

drivers/cxl/core/pci.c void cxl_cache_cdat_mb(struct cxl_port *port) · line 528

Quoted 6 lines
drivers/cxl/core/pci.c +6 -0
+	int rc = 0;
+
+	if (!port->cdat_mb) {
+		dev_err(&port->dev, "No CDAT mailbox\n");
+		return -EIO;
+	}

AIUI, !port->cdat_mb isn't actually an error. Does it make sense to simply return 0 here?

Quoted 19 lines
drivers/cxl/core/pci.c +19 -0
+
+	rc = pci_doe_submit_task(port->cdat_mb, &task);
+	if (rc < 0) {
+		dev_err(&port->dev, "DOE submit failed: %d", rc);
+		return rc;
+	}
+	wait_for_completion(&c);
+
+	if (task.rv < 1)
+		return -EIO;
+
+	*length = cdat_response_pl[1];
+	dev_dbg(&port->dev, "CDAT length %zu\n", *length);
+
+	return rc;
+}
+
+static int cxl_cdat_read_table(struct cxl_port *port,
+			       struct cxl_cdat *cdat)

drivers/cxl/core/pci.c void cxl_cache_cdat_mb(struct cxl_port *port) · line 557

Quoted 10 lines
drivers/cxl/core/pci.c +10 -0
+{
+	size_t length = cdat->length;
+	u32 *data = cdat->table;
+	int entry_handle = 0;
+	int rc = 0;
+
+	if (!port->cdat_mb) {
+		dev_err(&port->dev, "No CDAT mailbox\n");
+		return -EIO;
+	}

Similar to above, maybe just return 0?

Quoted 14 lines
drivers/cxl/core/pci.c +14 -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 = {
+			.prot.vid = PCI_DVSEC_VENDOR_ID_CXL,
+			.prot.type = CXL_DOE_PROTOCOL_TABLE_ACCESS,
+			.request_pl = &cdat_request_pl,
+			.request_pl_sz = sizeof(cdat_request_pl),
+			.response_pl = cdat_response_pl,
+			.response_pl_sz = sizeof(cdat_response_pl),
+			.complete = cxl_doe_task_complete,
+			.private = &c,

drivers/cxl/core/pci.c void cxl_cache_cdat_mb(struct cxl_port *port) · line 581

Quoted 10 lines
drivers/cxl/core/pci.c +10 -0
+		};
+		size_t entry_dw;
+		u32 *entry;
+
+		rc = pci_doe_submit_task(port->cdat_mb, &task);
+		if (rc < 0) {
+			dev_err(&port->dev, "DOE submit failed: %d", rc);
+			return rc;
+		}
+		wait_for_completion(&c);

I'd use the timeout variant, but if you don't want to, see below. I can't quite tell if pci_doe_submit_task() is guaranteed to end with FLAG_DEAD at some point...

drivers/cxl/core/pci.c void cxl_cache_cdat_mb(struct cxl_port *port) · line 591

Quoted 10 lines
drivers/cxl/core/pci.c +10 -0
+
+		entry = cdat_response_pl + 1;
+		entry_dw = task.rv / sizeof(u32);
+		/* Skip Header */
+		entry_dw -= 1;
+		entry_dw = min(length / 4, entry_dw);
+		memcpy(data, entry, entry_dw * sizeof(u32));
+		length -= entry_dw * sizeof(u32);
+		data += entry_dw;
+		entry_handle = FIELD_GET(CXL_DOE_TABLE_ACCESS_ENTRY_HANDLE, cdat_response_pl[0]);

[0] looks suspicious...

drivers/cxl/core/pci.c void cxl_cache_cdat_mb(struct cxl_port *port) · line 598

Quoted 7 lines
drivers/cxl/core/pci.c +7 -0
+
+	} while (entry_handle != 0xFFFF);
+
+	return rc;
+}
+
+void read_cdat_data(struct cxl_port *port)

I think you need kdoc here, specifically because you've opted not to do a timed wait, which means its possible to wait forever.

Quoted 8 lines
drivers/cxl/core/pci.c +8 -0
+{
+	struct device *dev = &port->dev;
+	size_t cdat_length;
+	int ret;
+
+	if (cxl_cdat_get_length(port, &cdat_length))
+		return;
+

drivers/cxl/core/pci.c void cxl_cache_cdat_mb(struct cxl_port *port) · line 616

Quoted 10 lines
drivers/cxl/core/pci.c +10 -0
+	port->cdat.table = devm_kzalloc(dev, cdat_length, GFP_KERNEL);
+	if (!port->cdat.table) {
+		ret = -ENOMEM;
+		goto error;
+	}
+
+	port->cdat.length = cdat_length;
+	ret = cxl_cdat_read_table(port, &port->cdat);
+	if (ret) {
+		devm_kfree(dev, port->cdat.table);

Usually, when I see devm_kfree, it's a sign that it might not be a good candidate for devm. You could consider plain kzalloc, and then putting the kfree in the port destructor. I don't see anything incorrect though, so it's up to you.

Quoted 65 lines
drivers/cxl/core/pci.c +11 -0
+		port->cdat.table = NULL;
+		port->cdat.length = 0;
+		ret = -EIO;
+		goto error;
+	}
+
+	return;
+error:
+	dev_err(dev, "CDAT data read error (%d)\n", ret);
+}
+EXPORT_SYMBOL_NS_GPL(read_cdat_data, CXL);
drivers/cxl/cxl.h +3 -0
index 0a86be589ffc..531b77d296c7 100644
@@ -8,6 +8,7 @@
 #include <linux/bitfield.h>
 #include <linux/bitops.h>
 #include <linux/io.h>
+#include "cdat.h"
 
 /**
  * DOC: cxl objects
@@ -268,6 +269,7 @@ struct cxl_nvdimm {
  * @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_mb: Mailbox which supports the CDAT protocol
+ * @cdat: Cached CDAT data
  */
 struct cxl_port {
 	struct device dev;
@@ -280,6 +282,7 @@ struct cxl_port {
 	bool dead;
 	unsigned int depth;
 	struct pci_doe_mb *cdat_mb;
+	struct cxl_cdat cdat;
 };
 
 /**
drivers/cxl/cxlpci.h +1 -0
index ddbb8b77752e..71009a167a92 100644
@@ -75,4 +75,5 @@ 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 cxl_cache_cdat_mb(struct cxl_port *port);
+void read_cdat_data(struct cxl_port *port);
 #endif /* __CXL_PCI_H__ */
drivers/cxl/port.c +2 -0
index 04f3d1fc6e07..fdff20cf79e6 100644
@@ -50,6 +50,8 @@ static int cxl_port_probe(struct device *dev)
 		return PTR_ERR(cxlhdm);
 
 	cxl_cache_cdat_mb(port);
+	/* Cache the data early to ensure is_visible() works */
+	read_cdat_data(port);
 
 	if (is_cxl_endpoint(port)) {
 		struct cxl_memdev *cxlmd = to_cxl_memdev(port->uport);
@@ -80,10 +82,58 @@ static int cxl_port_probe(struct device *dev)
 	return 0;
 }

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

Quoted 10 lines
drivers/cxl/port.c +9 -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.table)
+		return 0;

With visibility setup below, do you need this?

Quoted 49 lines
drivers/cxl/port.c +39 -0
+
+	return memory_read_from_buffer(buf, count, &offset,
+				       port->cdat.table,
+				       port->cdat.length);
+}
+
+static BIN_ATTR_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.table)
+		return 0400;
+
+	return 0;
+}
+
+static struct bin_attribute *cxl_cdat_bin_attributes[] = {
+	&bin_attr_cdat,
+	NULL,
+};
+
+static struct attribute_group cxl_cdat_attribute_group = {
+	.name = "CDAT",
+	.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.1
author
#14 lore raw

Re: [PATCH V10 6/9] cxl/port: Read CDAT table

Quoted 3 lines · Ben Widawsky
Quoted 2 lines · On 22-06-04 17:50:46, [email protected] wrote:

From: Jonathan Cameron <[email protected]>

[snip]

Quoted 10 lines
Quoted 10 lines
drivers/cxl/core/pci.c +10 -0
+
+static int cxl_cdat_get_length(struct cxl_port *port, 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 = {
+		.prot.vid = PCI_DVSEC_VENDOR_ID_CXL,
+		.prot.type = CXL_DOE_PROTOCOL_TABLE_ACCESS,
+		.request_pl = &cdat_request_pl,
Quoted 8 lines

drivers/cxl/core/pci.c void cxl_cache_cdat_mb(struct cxl_port *port) · line 522

Quoted 6 lines
drivers/cxl/core/pci.c +6 -0
+		.request_pl_sz = sizeof(cdat_request_pl),
+		.response_pl = cdat_response_pl,
+		.response_pl_sz = sizeof(cdat_response_pl),
+		.complete = cxl_doe_task_complete,
+		.private = &c,
+	};

This is looking like something that could be nicely populated with a macro.

Probably. But I'll leave that for another day.

Quoted 9 lines

drivers/cxl/core/pci.c void cxl_cache_cdat_mb(struct cxl_port *port) · line 528

Quoted 7 lines
drivers/cxl/core/pci.c +6 -0
+	int rc = 0;
+
+	if (!port->cdat_mb) {
+		dev_err(&port->dev, "No CDAT mailbox\n");
+		return -EIO;
+	}

AIUI, !port->cdat_mb isn't actually an error.

It was when I was trying to get this to work... ;-) I change to dev_dbg().

Quoted 2 lines

Does it make sense to simply
return 0 here?

No because this is just a helper to the read_cdat below. 0 could be used to indicate 'no data' but easier to return an obvious error.

Quoted 24 lines
Quoted 24 lines
drivers/cxl/core/pci.c +23 -0
+
+	rc = pci_doe_submit_task(port->cdat_mb, &task);
+	if (rc < 0) {
+		dev_err(&port->dev, "DOE submit failed: %d", rc);
+		return rc;
+	}
+	wait_for_completion(&c);
+
+	if (task.rv < 1)
+		return -EIO;
+
+	*length = cdat_response_pl[1];
+	dev_dbg(&port->dev, "CDAT length %zu\n", *length);
+
+	return rc;
+}
+
+static int cxl_cdat_read_table(struct cxl_port *port,
+			       struct cxl_cdat *cdat)
+{
+	size_t length = cdat->length;
+	u32 *data = cdat->table;
+	int entry_handle = 0;
Quoted 8 lines

drivers/cxl/core/pci.c void cxl_cache_cdat_mb(struct cxl_port *port) · line 528

Quoted 6 lines
drivers/cxl/core/pci.c +6 -0
+	int rc = 0;
+
+	if (!port->cdat_mb) {
+		dev_err(&port->dev, "No CDAT mailbox\n");
+		return -EIO;
+	}

Similar to above, maybe just return 0?

Same response. But I'll change the messages to dev_dbg().

Quoted 21 lines
Quoted 21 lines
drivers/cxl/core/pci.c +20 -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 = {
+			.prot.vid = PCI_DVSEC_VENDOR_ID_CXL,
+			.prot.type = CXL_DOE_PROTOCOL_TABLE_ACCESS,
+			.request_pl = &cdat_request_pl,
+			.request_pl_sz = sizeof(cdat_request_pl),
+			.response_pl = cdat_response_pl,
+			.response_pl_sz = sizeof(cdat_response_pl),
+			.complete = cxl_doe_task_complete,
+			.private = &c,
+		};
+		size_t entry_dw;
+		u32 *entry;
+
+		rc = pci_doe_submit_task(port->cdat_mb, &task);
+		if (rc < 0) {
Quoted 8 lines

drivers/cxl/core/pci.c void cxl_cache_cdat_mb(struct cxl_port *port) · line 535

Quoted 4 lines
drivers/cxl/core/pci.c +4 -0
+			dev_err(&port->dev, "DOE submit failed: %d", rc);
+			return rc;
+		}
+		wait_for_completion(&c);

I'd use the timeout variant, but if you don't want to, see below. I can't quite tell if pci_doe_submit_task() is guaranteed to end with FLAG_DEAD at some point...

Yes it will if it goes south. The issue with a timeout here is what should this layer expect for that time?

Quoted 13 lines

drivers/cxl/core/pci.c void cxl_cache_cdat_mb(struct cxl_port *port) · line 591

Quoted 11 lines
drivers/cxl/core/pci.c +10 -0
+
+		entry = cdat_response_pl + 1;
+		entry_dw = task.rv / sizeof(u32);
+		/* Skip Header */
+		entry_dw -= 1;
+		entry_dw = min(length / 4, entry_dw);
+		memcpy(data, entry, entry_dw * sizeof(u32));
+		length -= entry_dw * sizeof(u32);
+		data += entry_dw;
+		entry_handle = FIELD_GET(CXL_DOE_TABLE_ACCESS_ENTRY_HANDLE, cdat_response_pl[0]);

[0] looks suspicious...

Actually I have to claim ignorance on this one. I've carried this from Jonathan's original patches. I'm not as worried about the [0] as that is just the first dword. But I'm confused as to this entry handle now.

Jonathan? Help?

Quoted 11 lines

drivers/cxl/core/pci.c void cxl_cache_cdat_mb(struct cxl_port *port) · line 598

Quoted 8 lines
drivers/cxl/core/pci.c +7 -0
+
+	} while (entry_handle != 0xFFFF);
+
+	return rc;
+}
+
+void read_cdat_data(struct cxl_port *port)

I think you need kdoc here, specifically because you've opted not to do a timed wait, which means its possible to wait forever.

Sure but we are not going to wait forever due to the DOE spec. But I'll document that, sure.

Quoted 16 lines
Quoted 16 lines
drivers/cxl/core/pci.c +15 -0
+{
+	struct device *dev = &port->dev;
+	size_t cdat_length;
+	int ret;
+
+	if (cxl_cdat_get_length(port, &cdat_length))
+		return;
+
+	port->cdat.table = devm_kzalloc(dev, cdat_length, GFP_KERNEL);
+	if (!port->cdat.table) {
+		ret = -ENOMEM;
+		goto error;
+	}
+
+	port->cdat.length = cdat_length;
Quoted 8 lines

drivers/cxl/core/pci.c void cxl_cache_cdat_mb(struct cxl_port *port) · line 616

Quoted 3 lines
drivers/cxl/core/pci.c +3 -0
+	ret = cxl_cdat_read_table(port, &port->cdat);
+	if (ret) {
+		devm_kfree(dev, port->cdat.table);

Usually, when I see devm_kfree, it's a sign that it might not be a good candidate for devm. You could consider plain kzalloc, and then putting the kfree in the port destructor. I don't see anything incorrect though, so it's up to you.

I like it this way because we are really only doing this as an error condition. And it is less error prone to use devm. Technically devm_kfree() does not even need to be here except that then we could potentially have a lot of cdat tables floating around until the port goes away.

I can put in a comment to indicate why this was an anti-pattern.

[snip]

Quoted 27 lines
Quoted 27 lines
drivers/cxl/cxlpci.h +1 -0
index ddbb8b77752e..71009a167a92 100644
@@ -75,4 +75,5 @@ 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 cxl_cache_cdat_mb(struct cxl_port *port);
+void read_cdat_data(struct cxl_port *port);
 #endif /* __CXL_PCI_H__ */
drivers/cxl/port.c +2 -0
index 04f3d1fc6e07..fdff20cf79e6 100644
@@ -50,6 +50,8 @@ static int cxl_port_probe(struct device *dev)
 		return PTR_ERR(cxlhdm);
 
 	cxl_cache_cdat_mb(port);
+	/* Cache the data early to ensure is_visible() works */
+	read_cdat_data(port);
 
 	if (is_cxl_endpoint(port)) {
 		struct cxl_memdev *cxlmd = to_cxl_memdev(port->uport);
@@ -80,10 +82,58 @@ static int cxl_port_probe(struct device *dev)
 	return 0;
 }
 
Quoted 11 lines

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

Quoted 9 lines
drivers/cxl/port.c +9 -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.table)
+		return 0;

With visibility setup below, do you need this?

Not currently. I was envisioning a later dynamic state for cdat.table where on error this could have been set to NULL.

Ira

[snip]

#15 lore raw

Re: [PATCH V10 6/9] cxl/port: Read CDAT table

Quoted 5 lines · On Wed, 8 Jun 2022 14:27:14 -0700 Ira Weiny <[email protected]> wrote:
Quoted 3 lines · Ben Widawsky
Quoted 2 lines · On 22-06-04 17:50:46, [email protected] wrote:

From: Jonathan Cameron <[email protected]>

[snip]

Quoted 14 lines
Quoted 13 lines

drivers/cxl/core/pci.c void cxl_cache_cdat_mb(struct cxl_port *port) · line 591

Quoted 11 lines
drivers/cxl/core/pci.c +10 -0
+
+		entry = cdat_response_pl + 1;
+		entry_dw = task.rv / sizeof(u32);
+		/* Skip Header */
+		entry_dw -= 1;
+		entry_dw = min(length / 4, entry_dw);
+		memcpy(data, entry, entry_dw * sizeof(u32));
+		length -= entry_dw * sizeof(u32);
+		data += entry_dw;
+		entry_handle = FIELD_GET(CXL_DOE_TABLE_ACCESS_ENTRY_HANDLE, cdat_response_pl[0]);  

[0] looks suspicious...

Quoted 5 lines

Actually I have to claim ignorance on this one. I've carried this from Jonathan's original patches. I'm not as worried about the [0] as that is just the first dword. But I'm confused as to this entry handle now.

Jonathan? Help?

Looks right to me. The entryhandle is a field in the upper 16 bits of the first dword defined in Read Entry Response table in the CXL spec and also used in the request of the next entry (which is more or less a CDAT structure) Two magic values.
0 - CDAT header (request only - can't be returned)
0xFFFF - No more entries.

As we are reading the whole table, we write 0 to first request and from there on use the value returned in the response for the next request until we see 0xFFFF and stop.

Note IIRC the meaning of entry handle was clarified in a CXL 2.0 errata as was a bit ambiguous in the original spec (we had two QEMU implementations briefly and they did different things :)

Jonathan

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/cxl/core/pci.c +6 -0
+
+	} while (entry_handle != 0xFFFF);
+
+	return rc;
+}
+
author
#16 lore raw

Re: [PATCH V10 6/9] cxl/port: Read CDAT table

Quoted 29 lines · Jonathan Cameron
Quoted 5 lines · On Wed, 8 Jun 2022 14:27:14 -0700 Ira Weiny <[email protected]> wrote:
Quoted 3 lines · Ben Widawsky
Quoted 2 lines · On 22-06-04 17:50:46, [email protected] wrote:

From: Jonathan Cameron <[email protected]>

[snip]

Quoted 18 lines
Quoted 13 lines

drivers/cxl/core/pci.c void cxl_cache_cdat_mb(struct cxl_port *port) · line 591

Quoted 11 lines
drivers/cxl/core/pci.c +10 -0
+
+		entry = cdat_response_pl + 1;
+		entry_dw = task.rv / sizeof(u32);
+		/* Skip Header */
+		entry_dw -= 1;
+		entry_dw = min(length / 4, entry_dw);
+		memcpy(data, entry, entry_dw * sizeof(u32));
+		length -= entry_dw * sizeof(u32);
+		data += entry_dw;
+		entry_handle = FIELD_GET(CXL_DOE_TABLE_ACCESS_ENTRY_HANDLE, cdat_response_pl[0]);  

[0] looks suspicious...

Actually I have to claim ignorance on this one. I've carried this from Jonathan's original patches. I'm not as worried about the [0] as that is just the first dword. But I'm confused as to this entry handle now.

Quoted 8 lines
Quoted 1 line

Jonathan? Help?

Looks right to me. The entryhandle is a field in the upper 16 bits of the first dword defined in Read Entry Response table in the CXL spec and also used in the request of the next entry (which is more or less a CDAT structure) Two magic values.
0 - CDAT header (request only - can't be returned)
0xFFFF - No more entries.

Thanks!

Quoted 8 lines

As we are reading the whole table, we write 0 to first request and from there on use the value returned in the response for the next request until we see 0xFFFF and stop.

Note IIRC the meaning of entry handle was clarified in a CXL 2.0 errata as was a bit ambiguous in the original spec (we had two QEMU implementations briefly and they did different things :)

I see it now! I'm going to add a define for 0xFFFF as well.

Ira

Quoted 14 lines

Jonathan

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/cxl/core/pci.c +6 -0
+
+	} while (entry_handle != 0xFFFF);
+
+	return rc;
+}
+
#17 lore raw

PATCH v10 7/9 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 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 | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+)
drivers/cxl/cdat.h +2 -0
index f5193a6a51fe..3d8945612511 100644
@@ -90,10 +90,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 +36 -0
index 76fa8382b3c7..73e28b82ffcf 100644
@@ -543,6 +543,40 @@ static int cxl_cdat_get_length(struct cxl_port *port, size_t *length)
 	return rc;
 }
 
+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, "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, "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 cxl_port *port,
 			       struct cxl_cdat *cdat)
 {
@@ -592,6 +626,8 @@ static int cxl_cdat_read_table(struct cxl_port *port,
 
 	} while (entry_handle != 0xFFFF);
 
+	if (!rc && !cxl_cdat_valid(&port->dev, cdat))
+		return -EIO;
 	return rc;
 }
 
Signature · 1 line
2.35.1
#18 lore raw

PATCH v10 8/9 cxl/port: Retry reading CDAT on failure

In reply to [email protected]

From: Ira Weiny <[email protected]>

The CDAT read may fail for a number of reasons but mainly it is possible to get different parts of a valid state. The checksum in the CDAT table protects against this.

Now that the cdat data is validated issue a retries if the CDAT read fails. For now 5 retries are implemented.

Cc Alison Schofield <[email protected]>
Cc Davidlohr Bueso <[email protected]>

---
Changes from V9
Alison Schofield/Davidlohr Bueso
Print debug on each iteration and error only after failure

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

Changes from V6
Move to pci.c
Fix retries count
Change to 5 retries

Changes from V5:
New patch -- easy to push off or drop.

---
 drivers/cxl/core/pci.c | 34 +++++++++++++++++++++++-----------
 1 file changed, 23 insertions(+), 11 deletions(-)
drivers/cxl/core/pci.c +23 -11
index 73e28b82ffcf..e68f13e66fcf 100644
@@ -631,20 +631,18 @@ static int cxl_cdat_read_table(struct cxl_port *port,
 	return rc;
 }
 
-void read_cdat_data(struct cxl_port *port)
+static int __read_cdat_data(struct cxl_port *port)
 {
 	struct device *dev = &port->dev;
 	size_t cdat_length;
 	int ret;
 
 	if (cxl_cdat_get_length(port, &cdat_length))
-		return;
+		return 0;
 
 	port->cdat.table = devm_kzalloc(dev, cdat_length, GFP_KERNEL);
-	if (!port->cdat.table) {
-		ret = -ENOMEM;
-		goto error;
-	}
+	if (!port->cdat.table)
+		return -ENOMEM;
 
 	port->cdat.length = cdat_length;
 	ret = cxl_cdat_read_table(port, &port->cdat);
@@ -652,12 +650,26 @@ void read_cdat_data(struct cxl_port *port)
 		devm_kfree(dev, port->cdat.table);
 		port->cdat.table = NULL;
 		port->cdat.length = 0;
-		ret = -EIO;
-		goto error;
+		return -EIO;
 	}
 
-	return;
-error:
-	dev_err(dev, "CDAT data read error (%d)\n", ret);
+	return 0;
+}
+
+void read_cdat_data(struct cxl_port *port)
+{
+	int retries = 5;
+	int rc;
+
+	while (retries--) {
+		rc = __read_cdat_data(port);
+		if (!rc)
+			return;
+		dev_dbg(&port->dev,
+			"CDAT data read error rc=%d (retries %d)\n",
+			rc, retries);
+	}
+	dev_err(&port->dev, "CDAT data read failed after %d retries\n",
+		retries);
 }
 EXPORT_SYMBOL_NS_GPL(read_cdat_data, CXL);
Signature · 1 line
2.35.1
Reviewed-by on v10 8/9
#19 lore raw

Re: [PATCH V10 8/9] cxl/port: Retry reading CDAT on failure

Quoted 7 lines · On 22-06-04 17:50:48, [email protected] wrote:

From: Ira Weiny <[email protected]>

The CDAT read may fail for a number of reasons but mainly it is possible to get different parts of a valid state. The checksum in the CDAT table protects against this.

Now that the cdat data is validated issue a retries if the CDAT read

s/validated issue a retries/validated, issue a retry/

Quoted 71 lines

fails. For now 5 retries are implemented.

Cc Alison Schofield <[email protected]>
Cc Davidlohr Bueso <[email protected]>

---
Changes from V9
Alison Schofield/Davidlohr Bueso
Print debug on each iteration and error only after failure

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

Changes from V6
Move to pci.c
Fix retries count
Change to 5 retries

Changes from V5:
New patch -- easy to push off or drop.

---
 drivers/cxl/core/pci.c | 34 +++++++++++++++++++++++-----------
 1 file changed, 23 insertions(+), 11 deletions(-)
drivers/cxl/core/pci.c +10 -11
index 73e28b82ffcf..e68f13e66fcf 100644
@@ -631,20 +631,18 @@ static int cxl_cdat_read_table(struct cxl_port *port,
 	return rc;
 }
 
-void read_cdat_data(struct cxl_port *port)
+static int __read_cdat_data(struct cxl_port *port)
 {
 	struct device *dev = &port->dev;
 	size_t cdat_length;
 	int ret;
 
 	if (cxl_cdat_get_length(port, &cdat_length))
-		return;
+		return 0;
 
 	port->cdat.table = devm_kzalloc(dev, cdat_length, GFP_KERNEL);
-	if (!port->cdat.table) {
-		ret = -ENOMEM;
-		goto error;
-	}
+	if (!port->cdat.table)
+		return -ENOMEM;
 
 	port->cdat.length = cdat_length;
 	ret = cxl_cdat_read_table(port, &port->cdat);
@@ -652,12 +650,26 @@ void read_cdat_data(struct cxl_port *port)
 		devm_kfree(dev, port->cdat.table);
 		port->cdat.table = NULL;
 		port->cdat.length = 0;
-		ret = -EIO;
-		goto error;
+		return -EIO;
 	}
 
-	return;
-error:
-	dev_err(dev, "CDAT data read error (%d)\n", ret);
+	return 0;
+}
+
+void read_cdat_data(struct cxl_port *port)
+{

drivers/cxl/core/pci.c void read_cdat_data(struct cxl_port *port) · line 670

Quoted 10 lines
drivers/cxl/core/pci.c +10 -0
+	int retries = 5;
+	int rc;
+
+	while (retries--) {
+		rc = __read_cdat_data(port);
+		if (!rc)
+			return;
+		dev_dbg(&port->dev,
+			"CDAT data read error rc=%d (retries %d)\n",
+			rc, retries);

Out of curiousity, what is the purpose of the dev_dbg? To diagnose delays or something?

drivers/cxl/core/pci.c void read_cdat_data(struct cxl_port *port) · line 673

Quoted 3 lines
drivers/cxl/core/pci.c +3 -0
+	}
+	dev_err(&port->dev, "CDAT data read failed after %d retries\n",
+		retries);
Quoted 5 lines
drivers/cxl/core/pci.c +0 -0
 }
 EXPORT_SYMBOL_NS_GPL(read_cdat_data, CXL);
Signature · 1 line
2.35.1
author
#20 lore raw

Re: [PATCH V10 8/9] cxl/port: Retry reading CDAT on failure

Quoted 9 lines · Ben Widawsky
Quoted 7 lines · On 22-06-04 17:50:48, [email protected] wrote:

From: Ira Weiny <[email protected]>

The CDAT read may fail for a number of reasons but mainly it is possible to get different parts of a valid state. The checksum in the CDAT table protects against this.

Now that the cdat data is validated issue a retries if the CDAT read

s/validated issue a retries/validated, issue a retry/

Thanks!

Quoted 76 lines
Quoted 76 lines

fails. For now 5 retries are implemented.

Cc Alison Schofield <[email protected]>
Cc Davidlohr Bueso <[email protected]>

---
Changes from V9
Alison Schofield/Davidlohr Bueso
Print debug on each iteration and error only after failure

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

Changes from V6
Move to pci.c
Fix retries count
Change to 5 retries

Changes from V5:
New patch -- easy to push off or drop.

---
 drivers/cxl/core/pci.c | 34 +++++++++++++++++++++++-----------
 1 file changed, 23 insertions(+), 11 deletions(-)
drivers/cxl/core/pci.c +15 -11
index 73e28b82ffcf..e68f13e66fcf 100644
@@ -631,20 +631,18 @@ static int cxl_cdat_read_table(struct cxl_port *port,
 	return rc;
 }
 
-void read_cdat_data(struct cxl_port *port)
+static int __read_cdat_data(struct cxl_port *port)
 {
 	struct device *dev = &port->dev;
 	size_t cdat_length;
 	int ret;
 
 	if (cxl_cdat_get_length(port, &cdat_length))
-		return;
+		return 0;
 
 	port->cdat.table = devm_kzalloc(dev, cdat_length, GFP_KERNEL);
-	if (!port->cdat.table) {
-		ret = -ENOMEM;
-		goto error;
-	}
+	if (!port->cdat.table)
+		return -ENOMEM;
 
 	port->cdat.length = cdat_length;
 	ret = cxl_cdat_read_table(port, &port->cdat);
@@ -652,12 +650,26 @@ void read_cdat_data(struct cxl_port *port)
 		devm_kfree(dev, port->cdat.table);
 		port->cdat.table = NULL;
 		port->cdat.length = 0;
-		ret = -EIO;
-		goto error;
+		return -EIO;
 	}
 
-	return;
-error:
-	dev_err(dev, "CDAT data read error (%d)\n", ret);
+	return 0;
+}
+
+void read_cdat_data(struct cxl_port *port)
+{
+	int retries = 5;
+	int rc;
+
+	while (retries--) {
+		rc = __read_cdat_data(port);
Quoted 8 lines

drivers/cxl/core/pci.c void read_cdat_data(struct cxl_port *port) · line 670

Quoted 5 lines
drivers/cxl/core/pci.c +5 -0
+		if (!rc)
+			return;
+		dev_dbg(&port->dev,
+			"CDAT data read error rc=%d (retries %d)\n",
+			rc, retries);

Out of curiousity, what is the purpose of the dev_dbg? To diagnose delays or something?

Yes

Quoted 7 lines

drivers/cxl/core/pci.c void read_cdat_data(struct cxl_port *port) · line 673

Quoted 4 lines
drivers/cxl/core/pci.c +3 -0
+	}
+	dev_err(&port->dev, "CDAT data read failed after %d retries\n",
+		retries);

Thanks!
Ira

Quoted 6 lines
Quoted 6 lines
drivers/cxl/core/pci.c +0 -0
 }
 EXPORT_SYMBOL_NS_GPL(read_cdat_data, CXL);
Signature · 1 line
2.35.1
#21 lore raw

PATCH v10 9/9 cxl/port: Parse out DSMAS data from CDAT table

In reply to [email protected]

From: Ira Weiny <[email protected]>

CXL Ports with memory devices attached need the information from the Device Scoped Memory Affinity Structure (DSMAS). This information is contained within the CDAT table buffer which is previously read and cached in the port device.

If CDAT data is available, parse and cache DSMAS data from the table. Store this data in unmarshaled struct dsmas data structures for ease of use later.

---
Changes from V8
Adjust to the cdat data being in cxl_port

Changes from V7
Rebased on cxl-pending

Changes from V6
Move to port.c
It is not an error if no DSMAS data is found

Changes from V5
Fix up sparse warnings
Split out cdat_hdr_valid()
Update cdat_hdr_valid()
Remove revision and cs field parsing
There is no point in these
Add seq check and debug print.
From Jonathan
Add spaces around '+' and '/' use devm_krealloc() for dmas_ary

---
 drivers/cxl/cdat.h     | 17 +++++++++++
 drivers/cxl/core/pci.c | 65 ++++++++++++++++++++++++++++++++++++++++++
 drivers/cxl/cxl.h      |  2 ++
 drivers/cxl/cxlmem.h   |  4 +++
 drivers/cxl/cxlpci.h   |  1 +
 drivers/cxl/mem.c      |  1 +
 6 files changed, 90 insertions(+)
drivers/cxl/cdat.h +17 -0
index 3d8945612511..0a510f73fe6d 100644
@@ -85,6 +85,23 @@
 
 #define CXL_DOE_PROTOCOL_TABLE_ACCESS 2
 
+/**
+ * struct cxl_dsmas - host unmarshaled version of DSMAS data
+ *
+ * As defined in the Coherent Device Attribute Table (CDAT) specification this
+ * represents a single DSMAS entry in that table.
+ *
+ * @dpa_base: The lowest Device Physical Address associated with this DSMAD
+ * @length: Length in bytes of this DSMAD
+ * @non_volatile: If set, the memory region represents Non-Volatile memory
+ */
+struct cxl_dsmas {
+	u64 dpa_base;
+	u64 length;
+	/* Flags */
+	u8 non_volatile:1;
+};
+
 /**
  * struct cxl_cdat - CXL CDAT data
  *
drivers/cxl/core/pci.c +65 -0

68 lines not on this page · raw message

drivers/cxl/cxl.h +2 -0
index 531b77d296c7..4d779a8fb807 100644
@@ -10,6 +10,8 @@
 #include <linux/io.h>
 #include "cdat.h"
 
+#include "cdat.h"
+
 /**
  * DOC: cxl objects
  *
drivers/cxl/cxlmem.h +4 -0
index 4d2764b865ab..ce5b00f3ebcb 100644
@@ -36,6 +36,8 @@
  * @cxlds: The device state backing this device
  * @detach_work: active memdev lost a port in its ancestry
  * @id: id number of this memdev instance.
+ * @dsmas_ary: Array of DSMAS entries as parsed from the CDAT table
+ * @nr_dsmas: Number of entries in dsmas_ary
  */
 struct cxl_memdev {
 	struct device dev;
@@ -43,6 +45,8 @@ struct cxl_memdev {
 	struct cxl_dev_state *cxlds;
 	struct work_struct detach_work;
 	int id;
+	struct cxl_dsmas *dsmas_ary;
+	int nr_dsmas;
 };
 
 static inline struct cxl_memdev *to_cxl_memdev(struct device *dev)
drivers/cxl/cxlpci.h +1 -0
index 71009a167a92..44d1c2b83aab 100644
@@ -76,4 +76,5 @@ struct cxl_dev_state;
 int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm);
 void cxl_cache_cdat_mb(struct cxl_port *port);
 void read_cdat_data(struct cxl_port *port);
+void parse_dsmas(struct cxl_memdev *cxlmd, struct cxl_port *port);
 #endif /* __CXL_PCI_H__ */
drivers/cxl/mem.c +1 -0
index c310f1fd3db0..a8768df4ae38 100644
@@ -35,6 +35,7 @@ static int create_endpoint(struct cxl_memdev *cxlmd,
 	if (IS_ERR(endpoint))
 		return PTR_ERR(endpoint);
 
+	parse_dsmas(cxlmd, endpoint);
 	dev_dbg(&cxlmd->dev, "add: %s\n", dev_name(&endpoint->dev));
 
 	if (!endpoint->dev.driver) {
Signature · 1 line
2.35.1
#22 lore raw

Re: [PATCH V10 9/9] cxl/port: Parse out DSMAS data from CDAT table

Quoted 106 lines · On 22-06-04 17:50:49, [email protected] wrote:

From: Ira Weiny <[email protected]>

CXL Ports with memory devices attached need the information from the Device Scoped Memory Affinity Structure (DSMAS). This information is contained within the CDAT table buffer which is previously read and cached in the port device.

If CDAT data is available, parse and cache DSMAS data from the table. Store this data in unmarshaled struct dsmas data structures for ease of use later.

---
Changes from V8
Adjust to the cdat data being in cxl_port

Changes from V7
Rebased on cxl-pending

Changes from V6
Move to port.c
It is not an error if no DSMAS data is found

Changes from V5
Fix up sparse warnings
Split out cdat_hdr_valid()
Update cdat_hdr_valid()
Remove revision and cs field parsing
There is no point in these
Add seq check and debug print.
From Jonathan
Add spaces around '+' and '/' use devm_krealloc() for dmas_ary

---
 drivers/cxl/cdat.h     | 17 +++++++++++
 drivers/cxl/core/pci.c | 65 ++++++++++++++++++++++++++++++++++++++++++
 drivers/cxl/cxl.h      |  2 ++
 drivers/cxl/cxlmem.h   |  4 +++
 drivers/cxl/cxlpci.h   |  1 +
 drivers/cxl/mem.c      |  1 +
 6 files changed, 90 insertions(+)
drivers/cxl/cdat.h +17 -0
index 3d8945612511..0a510f73fe6d 100644
@@ -85,6 +85,23 @@
 
 #define CXL_DOE_PROTOCOL_TABLE_ACCESS 2
 
+/**
+ * struct cxl_dsmas - host unmarshaled version of DSMAS data
+ *
+ * As defined in the Coherent Device Attribute Table (CDAT) specification this
+ * represents a single DSMAS entry in that table.
+ *
+ * @dpa_base: The lowest Device Physical Address associated with this DSMAD
+ * @length: Length in bytes of this DSMAD
+ * @non_volatile: If set, the memory region represents Non-Volatile memory
+ */
+struct cxl_dsmas {
+	u64 dpa_base;
+	u64 length;
+	/* Flags */
+	u8 non_volatile:1;
+};
+
 /**
  * struct cxl_cdat - CXL CDAT data
  *
drivers/cxl/core/pci.c +27 -0
index e68f13e66fcf..9666cc4a67aa 100644
@@ -673,3 +673,68 @@ void read_cdat_data(struct cxl_port *port)
 		retries);
 }
 EXPORT_SYMBOL_NS_GPL(read_cdat_data, CXL);
+
+void parse_dsmas(struct cxl_memdev *cxlmd, struct cxl_port *port)
+{
+	struct device *dev = &port->dev;
+	struct cxl_dsmas *dsmas_ary = NULL;
+	u32 *data = port->cdat.table;
+	int bytes_left = port->cdat.length;
+	int nr_dsmas = 0;
+
+	if (!data) {
+		dev_info(dev, "No CDAT data available for DSMAS\n");
+		return;
+	}
+
+	/* Skip header */
+	data += CDAT_HEADER_LENGTH_DW;
+	bytes_left -= CDAT_HEADER_LENGTH_BYTES;
+
+	while (bytes_left > 0) {
+		u32 *cur_rec = data;
+		u8 type = FIELD_GET(CDAT_STRUCTURE_DW0_TYPE, cur_rec[0]);
+		u16 length = FIELD_GET(CDAT_STRUCTURE_DW0_LENGTH, cur_rec[0]);
+
+		if (type == CDAT_STRUCTURE_DW0_TYPE_DSMAS) {
+			struct cxl_dsmas *new_ary;
+			u8 flags;
+

drivers/cxl/core/pci.c void read_cdat_data(struct cxl_port *port) · line 712

Quoted 10 lines
drivers/cxl/core/pci.c +10 -0
+			new_ary = devm_krealloc(dev, dsmas_ary,
+					   sizeof(*dsmas_ary) * (nr_dsmas + 1),
+					   GFP_KERNEL);
+			if (!new_ary) {
+				dev_err(dev,
+					"Failed to allocate memory for DSMAS data (nr_dsmas %d)\n",
+					nr_dsmas);
+				return;
+			}
+			dsmas_ary = new_ary;

I don't love the realloc on every loop (though I've done something similar before). What about allocating enough entries port->cdat.length, and then a single krealloc to shorten the array upon success? I think krealloc pretty much always succeeds when you shrink FWIW.

drivers/cxl/core/pci.c void read_cdat_data(struct cxl_port *port) · line 714

Quoted 2 lines
drivers/cxl/core/pci.c +2 -0
+
+			flags = FIELD_GET(CDAT_DSMAS_DW1_FLAGS, cur_rec[1]);

Does it make sense to test length/bytes_left before reading cur_rec[1]?

Quoted 44 lines
drivers/cxl/core/pci.c +26 -0
+
+			dsmas_ary[nr_dsmas].dpa_base = CDAT_DSMAS_DPA_OFFSET(cur_rec);
+			dsmas_ary[nr_dsmas].length = CDAT_DSMAS_DPA_LEN(cur_rec);
+			dsmas_ary[nr_dsmas].non_volatile = CDAT_DSMAS_NON_VOLATILE(flags);
+
+			dev_dbg(dev, "DSMAS %d: %llx:%llx %s\n",
+				nr_dsmas,
+				dsmas_ary[nr_dsmas].dpa_base,
+				dsmas_ary[nr_dsmas].dpa_base +
+					dsmas_ary[nr_dsmas].length,
+				(dsmas_ary[nr_dsmas].non_volatile ?
+					"Persistent" : "Volatile")
+				);
+
+			nr_dsmas++;
+		}
+
+		data += (length / sizeof(u32));
+		bytes_left -= length;
+	}
+
+	dev_dbg(dev, "Found %d DSMAS entries\n", nr_dsmas);
+	cxlmd->dsmas_ary = dsmas_ary;
+	cxlmd->nr_dsmas = nr_dsmas;
+}
+EXPORT_SYMBOL_NS_GPL(parse_dsmas, CXL);
drivers/cxl/cxl.h +2 -0
index 531b77d296c7..4d779a8fb807 100644
@@ -10,6 +10,8 @@
 #include <linux/io.h>
 #include "cdat.h"
 
+#include "cdat.h"
+
 /**
  * DOC: cxl objects
  *
drivers/cxl/cxlmem.h +0 -0
index 4d2764b865ab..ce5b00f3ebcb 100644

drivers/cxl/cxlmem.h line 40

Quoted 5 lines
drivers/cxl/cxlmem.h +2 -0
@@ -36,6 +36,8 @@
  * @cxlds: The device state backing this device
  * @detach_work: active memdev lost a port in its ancestry
  * @id: id number of this memdev instance.
+ * @dsmas_ary: Array of DSMAS entries as parsed from the CDAT table
+ * @nr_dsmas: Number of entries in dsmas_ary

Personally don't think it's necessary to append "_ary". Up to you.

Quoted 37 lines
drivers/cxl/cxlmem.h +2 -0
  */
 struct cxl_memdev {
 	struct device dev;
@@ -43,6 +45,8 @@ struct cxl_memdev {
 	struct cxl_dev_state *cxlds;
 	struct work_struct detach_work;
 	int id;
+	struct cxl_dsmas *dsmas_ary;
+	int nr_dsmas;
 };
 
 static inline struct cxl_memdev *to_cxl_memdev(struct device *dev)
drivers/cxl/cxlpci.h +1 -0
index 71009a167a92..44d1c2b83aab 100644
@@ -76,4 +76,5 @@ struct cxl_dev_state;
 int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm);
 void cxl_cache_cdat_mb(struct cxl_port *port);
 void read_cdat_data(struct cxl_port *port);
+void parse_dsmas(struct cxl_memdev *cxlmd, struct cxl_port *port);
 #endif /* __CXL_PCI_H__ */
drivers/cxl/mem.c +1 -0
index c310f1fd3db0..a8768df4ae38 100644
@@ -35,6 +35,7 @@ static int create_endpoint(struct cxl_memdev *cxlmd,
 	if (IS_ERR(endpoint))
 		return PTR_ERR(endpoint);
 
+	parse_dsmas(cxlmd, endpoint);
 	dev_dbg(&cxlmd->dev, "add: %s\n", dev_name(&endpoint->dev));
 
 	if (!endpoint->dev.driver) {
Signature · 1 line
2.35.1
author
#23 lore raw

Re: [PATCH V10 9/9] cxl/port: Parse out DSMAS data from CDAT table

Quoted 114 lines · Ben Widawsky
Quoted 113 lines · On 22-06-04 17:50:49, [email protected] wrote:

From: Ira Weiny <[email protected]>

CXL Ports with memory devices attached need the information from the Device Scoped Memory Affinity Structure (DSMAS). This information is contained within the CDAT table buffer which is previously read and cached in the port device.

If CDAT data is available, parse and cache DSMAS data from the table. Store this data in unmarshaled struct dsmas data structures for ease of use later.

---
Changes from V8
Adjust to the cdat data being in cxl_port

Changes from V7
Rebased on cxl-pending

Changes from V6
Move to port.c
It is not an error if no DSMAS data is found

Changes from V5
Fix up sparse warnings
Split out cdat_hdr_valid()
Update cdat_hdr_valid()
Remove revision and cs field parsing
There is no point in these
Add seq check and debug print.
From Jonathan
Add spaces around '+' and '/' use devm_krealloc() for dmas_ary

---
 drivers/cxl/cdat.h     | 17 +++++++++++
 drivers/cxl/core/pci.c | 65 ++++++++++++++++++++++++++++++++++++++++++
 drivers/cxl/cxl.h      |  2 ++
 drivers/cxl/cxlmem.h   |  4 +++
 drivers/cxl/cxlpci.h   |  1 +
 drivers/cxl/mem.c      |  1 +
 6 files changed, 90 insertions(+)
drivers/cxl/cdat.h +17 -0
index 3d8945612511..0a510f73fe6d 100644
@@ -85,6 +85,23 @@
 
 #define CXL_DOE_PROTOCOL_TABLE_ACCESS 2
 
+/**
+ * struct cxl_dsmas - host unmarshaled version of DSMAS data
+ *
+ * As defined in the Coherent Device Attribute Table (CDAT) specification this
+ * represents a single DSMAS entry in that table.
+ *
+ * @dpa_base: The lowest Device Physical Address associated with this DSMAD
+ * @length: Length in bytes of this DSMAD
+ * @non_volatile: If set, the memory region represents Non-Volatile memory
+ */
+struct cxl_dsmas {
+	u64 dpa_base;
+	u64 length;
+	/* Flags */
+	u8 non_volatile:1;
+};
+
 /**
  * struct cxl_cdat - CXL CDAT data
  *
drivers/cxl/core/pci.c +34 -0
index e68f13e66fcf..9666cc4a67aa 100644
@@ -673,3 +673,68 @@ void read_cdat_data(struct cxl_port *port)
 		retries);
 }
 EXPORT_SYMBOL_NS_GPL(read_cdat_data, CXL);
+
+void parse_dsmas(struct cxl_memdev *cxlmd, struct cxl_port *port)
+{
+	struct device *dev = &port->dev;
+	struct cxl_dsmas *dsmas_ary = NULL;
+	u32 *data = port->cdat.table;
+	int bytes_left = port->cdat.length;
+	int nr_dsmas = 0;
+
+	if (!data) {
+		dev_info(dev, "No CDAT data available for DSMAS\n");
+		return;
+	}
+
+	/* Skip header */
+	data += CDAT_HEADER_LENGTH_DW;
+	bytes_left -= CDAT_HEADER_LENGTH_BYTES;
+
+	while (bytes_left > 0) {
+		u32 *cur_rec = data;
+		u8 type = FIELD_GET(CDAT_STRUCTURE_DW0_TYPE, cur_rec[0]);
+		u16 length = FIELD_GET(CDAT_STRUCTURE_DW0_LENGTH, cur_rec[0]);
+
+		if (type == CDAT_STRUCTURE_DW0_TYPE_DSMAS) {
+			struct cxl_dsmas *new_ary;
+			u8 flags;
+
+			new_ary = devm_krealloc(dev, dsmas_ary,
+					   sizeof(*dsmas_ary) * (nr_dsmas + 1),
+					   GFP_KERNEL);
+			if (!new_ary) {
+				dev_err(dev,
+					"Failed to allocate memory for DSMAS data (nr_dsmas %d)\n",
+					nr_dsmas);
Quoted 8 lines

drivers/cxl/core/pci.c void read_cdat_data(struct cxl_port *port) · line 712

Quoted 3 lines
drivers/cxl/core/pci.c +3 -0
+				return;
+			}
+			dsmas_ary = new_ary;

I don't love the realloc on every loop (though I've done something similar before). What about allocating enough entries port->cdat.length, and then a single krealloc to shorten the array upon success? I think krealloc pretty much always succeeds when you shrink FWIW.

I'm not sure that it would be better in any significant way honestly. The chances of krealloc failing is small.

Quoted 5 lines

drivers/cxl/core/pci.c void read_cdat_data(struct cxl_port *port) · line 714

Quoted 3 lines
drivers/cxl/core/pci.c +2 -0
+
+			flags = FIELD_GET(CDAT_DSMAS_DW1_FLAGS, cur_rec[1]);

Does it make sense to test length/bytes_left before reading cur_rec[1]?

Interesting idea. On the one hand the CDAT was already validated with the checksum. But the checksum could be valid even if the dsmas data was corrupted. Especially by a malicious device. Also, I'm not sure but I think SPDM would protect us from malicious devices once implemented.

Overall, it seems like checking bytes_left is a good check to ensure we don't run off the end of our buffer even if it is just some fluke of data reads.

As to length I'm not seeing a danger but it might be nice to know that something in the dsmas header was wrong. I'll think on this more.

Quoted 44 lines
Quoted 44 lines
drivers/cxl/core/pci.c +26 -0
+
+			dsmas_ary[nr_dsmas].dpa_base = CDAT_DSMAS_DPA_OFFSET(cur_rec);
+			dsmas_ary[nr_dsmas].length = CDAT_DSMAS_DPA_LEN(cur_rec);
+			dsmas_ary[nr_dsmas].non_volatile = CDAT_DSMAS_NON_VOLATILE(flags);
+
+			dev_dbg(dev, "DSMAS %d: %llx:%llx %s\n",
+				nr_dsmas,
+				dsmas_ary[nr_dsmas].dpa_base,
+				dsmas_ary[nr_dsmas].dpa_base +
+					dsmas_ary[nr_dsmas].length,
+				(dsmas_ary[nr_dsmas].non_volatile ?
+					"Persistent" : "Volatile")
+				);
+
+			nr_dsmas++;
+		}
+
+		data += (length / sizeof(u32));
+		bytes_left -= length;
+	}
+
+	dev_dbg(dev, "Found %d DSMAS entries\n", nr_dsmas);
+	cxlmd->dsmas_ary = dsmas_ary;
+	cxlmd->nr_dsmas = nr_dsmas;
+}
+EXPORT_SYMBOL_NS_GPL(parse_dsmas, CXL);
drivers/cxl/cxl.h +2 -0
index 531b77d296c7..4d779a8fb807 100644
@@ -10,6 +10,8 @@
 #include <linux/io.h>
 #include "cdat.h"
 
+#include "cdat.h"
+
 /**
  * DOC: cxl objects
  *
drivers/cxl/cxlmem.h +0 -0
index 4d2764b865ab..ce5b00f3ebcb 100644
Quoted 8 lines

drivers/cxl/cxlmem.h line 40

Quoted 6 lines
drivers/cxl/cxlmem.h +2 -0
@@ -36,6 +36,8 @@
  * @cxlds: The device state backing this device
  * @detach_work: active memdev lost a port in its ancestry
  * @id: id number of this memdev instance.
+ * @dsmas_ary: Array of DSMAS entries as parsed from the CDAT table
+ * @nr_dsmas: Number of entries in dsmas_ary

Personally don't think it's necessary to append "_ary". Up to you.

I think it's fine.

Thanks for the review,
Ira