Skip to contentSkip to conversation

cxl: Media Operations

RFC PATCH In review Davidlohr Bueso 4 participants 12 messages Mar 23 – Apr 21 CXL

lore
Share & tools
lore

Davidlohr Bueso

author maintainer
#0 lore raw

cover cxl: Media Operations

Hello,

This implements support for Media Operation cmd. It is marked RFC because of these noteworthy points:

(i) Sysfs interfaces. These changes implement 'security/zero' and multiplex 'security/sanitize' to allow dpa, len pairs for the range (no impact for current usage as whole-device). This breaks the 1 value per file "rule", and would be a first for drivers/cxl/*, so am I open to suggestions.

(ii) Background cmd handling semantics. Media operations are synchronous,
which for raged zeroing operations makes sense and for range sanitize differs from the current async handling for whole-device, so user response differs significantly, something I am not crazy about. Another downside of this is that it exposes lock hold times to be user-controllable, but is no different than what we do for fw. Fundamentally, users have the responsibility to break up the work into sane ranges. Further, this is without considering abortable bg cmds[0], which, per-spec, are unsupported today for media operations.

Tested with qemu + fixlets (https://lore.kernel.org/all/[email protected]/)

Applies against latest 'next' from cxl.git.

[0] https://lore.kernel.org/all/[email protected]/

Thanks!

Davidlohr Bueso (1):
cxl/mbox: Support Media Operation

 Documentation/ABI/testing/sysfs-bus-cxl |  31 +++-
 drivers/cxl/core/mbox.c                 | 235 ++++++++++++++++++++++++
 drivers/cxl/core/memdev.c               |  45 ++++-
 drivers/cxl/cxlmem.h                    |  59 ++++++
 drivers/cxl/pci.c                       |   4 +
 5 files changed, 369 insertions(+), 5 deletions(-)

--
2.39.5

author maintainer
#1 lore raw

PATCH 1/1 cxl/mbox: Support Media Operation

Add support for the Media Operation command (opcode 4402h) which enables targeted sanitize and zero operations on specific DPA ranges, as defined in CXL 4.0 Section 8.2.10.9.5.3.

Run Discovery during probe to learn the device's DPA range granularity and which operations are supported, and expose to sysfs accordingly:

o 'security/sanitize' is multiplexed to allow single ranged input.
o 'security/zero' is added, also with single ranged input.

Operations are only allowed on DPA ranges that do not overlap any hardware-committed endpoint decoder to ensure not corrupting active mappings. Background operations are handled synchronously.

---
 Documentation/ABI/testing/sysfs-bus-cxl |  31 +++-
 drivers/cxl/core/mbox.c                 | 235 ++++++++++++++++++++++++
 drivers/cxl/core/memdev.c               |  45 ++++-
 drivers/cxl/cxlmem.h                    |  59 ++++++
 drivers/cxl/pci.c                       |   4 +
 5 files changed, 369 insertions(+), 5 deletions(-)
Documentation/ABI/testing/sysfs-bus-cxl +27 -4
index c80a1b5a03db..da8f8dc92a5a 100644
@@ -117,10 +117,12 @@ Contact:	[email protected]
 Description:
 		(RO) Reading this file will display the CXL security state for
 		that device. Such states can be: 'disabled', 'sanitize', when
-		a sanitization is currently underway; or those available only
-		for persistent memory: 'locked', 'unlocked' or 'frozen'. This
-		sysfs entry is select/poll capable from userspace to notify
-		upon completion of a sanitize operation.
+		a whole-device sanitization is currently underway; or those
+		available only for persistent memory: 'locked', 'unlocked' or
+		'frozen'. This sysfs entry is select/poll capable from
+		userspace to notify upon completion of a sanitize operation.
+		Targeted range sanitize via Media Operation does not affect
+		this state as it completes synchronously.
 
 
 What:           /sys/bus/cxl/devices/memX/security/sanitize
@@ -141,6 +143,13 @@ Description:
 		states. If this file is not present, then there is no hardware
 		support for the operation.
 
+		If the device supports the Media Operation command with the
+		sanitize operation, a DPA range may be written as 'start
+		length' in bytes to sanitize a targeted region of media.
+		The start address and length must be aligned to the device's
+		media operation granularity. The sanitize method applied to
+		the range is the same as for whole-device sanitize.
+
 
 What            /sys/bus/cxl/devices/memX/security/erase
 Date:           June, 2023
@@ -158,6 +167,20 @@ Description:
 		support for the operation.
 
 
+What:		/sys/bus/cxl/devices/memX/security/zero
+Date:		March, 2026
+KernelVersion:	v7.1
+Contact:	[email protected]
+Description:
+		(WO) Write a DPA range as 'start length' in bytes to this
+		attribute to zero a specific region of device media via the
+		Media Operation command. After completion, reads from the
+		specified range will return zero. The start address and
+		length must be aligned to the device's media operation
+		granularity. If this file is not present, then the device
+		does not support the Media Operation zero command.
+
+
 What:		/sys/bus/cxl/devices/memX/firmware/
 Date:		April, 2023
 KernelVersion:	v6.5
drivers/cxl/core/mbox.c +235 -0

253 lines not on this page · raw message

drivers/cxl/core/memdev.c +44 -1

78 lines not on this page · raw message

drivers/cxl/cxlmem.h +59 -0

89 lines not on this page · raw message

drivers/cxl/pci.c +4 -0
index bace662dc988..95bf773aab14 100644
@@ -878,6 +878,10 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	if (rc)
 		dev_dbg(&pdev->dev, "No CXL Features discovered\n");
 
+	rc = cxl_media_op_discover(mds);
+	if (rc)
+		dev_dbg(&pdev->dev, "No Media Operation discovered\n");
+
 	cxlmd = devm_cxl_add_memdev(cxlds, NULL);
 	if (IS_ERR(cxlmd))
 		return PTR_ERR(cxlmd);
Signature · 1 line
2.39.5
#2 lore raw

Re: [PATCH 1/1] cxl/mbox: Support Media Operation

Quoted 7 lines · On Mon, 23 Mar 2026 13:40:13 -0700 Davidlohr Bueso <[email protected]> wrote:

Add support for the Media Operation command (opcode 4402h) which enables targeted sanitize and zero operations on specific DPA ranges, as defined in CXL 4.0 Section 8.2.10.9.5.3.

Run Discovery during probe to learn the device's DPA range granularity and which operations are supported, and expose to sysfs accordingly:

Quoted 8 lines

o 'security/sanitize' is multiplexed to allow single ranged input.
o 'security/zero' is added, also with single ranged input.

Operations are only allowed on DPA ranges that do not overlap any hardware-committed endpoint decoder to ensure not corrupting active mappings. Background operations are handled synchronously.

Hi Davidlohr,

Various comments inline,

Thanks,

Jonathan

Quoted 46 lines
---
 Documentation/ABI/testing/sysfs-bus-cxl |  31 +++-
 drivers/cxl/core/mbox.c                 | 235 ++++++++++++++++++++++++
 drivers/cxl/core/memdev.c               |  45 ++++-
 drivers/cxl/cxlmem.h                    |  59 ++++++
 drivers/cxl/pci.c                       |   4 +
 5 files changed, 369 insertions(+), 5 deletions(-)
Documentation/ABI/testing/sysfs-bus-cxl +13 -4
index c80a1b5a03db..da8f8dc92a5a 100644
@@ -117,10 +117,12 @@ Contact:	[email protected]
 Description:
 		(RO) Reading this file will display the CXL security state for
 		that device. Such states can be: 'disabled', 'sanitize', when
-		a sanitization is currently underway; or those available only
-		for persistent memory: 'locked', 'unlocked' or 'frozen'. This
-		sysfs entry is select/poll capable from userspace to notify
-		upon completion of a sanitize operation.
+		a whole-device sanitization is currently underway; or those
+		available only for persistent memory: 'locked', 'unlocked' or
+		'frozen'. This sysfs entry is select/poll capable from
+		userspace to notify upon completion of a sanitize operation.
+		Targeted range sanitize via Media Operation does not affect
+		this state as it completes synchronously.
 
 
 What:           /sys/bus/cxl/devices/memX/security/sanitize
@@ -141,6 +143,13 @@ Description:
 		states. If this file is not present, then there is no hardware
 		support for the operation.
 
+		If the device supports the Media Operation command with the
+		sanitize operation, a DPA range may be written as 'start
+		length' in bytes to sanitize a targeted region of media.
+		The start address and length must be aligned to the device's
+		media operation granularity. The sanitize method applied to
+		the range is the same as for whole-device sanitize.
+
 
 What            /sys/bus/cxl/devices/memX/security/erase
 Date:           June, 2023
@@ -158,6 +167,20 @@ Description:
 		support for the operation.
 

Documentation/ABI/testing/sysfs-bus-cxl Description: · line 178

Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-cxl +9 -0
 
+What:		/sys/bus/cxl/devices/memX/security/zero
+Date:		March, 2026
+KernelVersion:	v7.1
+Contact:	[email protected]
+Description:
+		(WO) Write a DPA range as 'start length' in bytes to this
+		attribute to zero a specific region of device media via the
+		Media Operation command. After completion, reads from the
+		specified range will return zero.

This is only true if we've flushed any caches on the CPU Side of the link. Will happen anyway I think as part of making the memory available. Perhaps not worth mentioning that complexity here.

Quoted 24 lines

The start address and

Documentation/ABI/testing/sysfs-bus-cxl +5 -0
+		length must be aligned to the device's media operation
+		granularity. If this file is not present, then the device
+		does not support the Media Operation zero command.
+
+
 What:		/sys/bus/cxl/devices/memX/firmware/
 Date:		April, 2023
 KernelVersion:	v6.5
drivers/cxl/core/mbox.c +9 -0
index aaa5c6277ebf..b7047cb9abed 100644
 
 
+/**
+ * cxl_media_op_discover() - Discover supported media operation
+ * @mds: The device data for the operation
+ *
+ * Discover any available Media Operations.
+ *
+ * Return: 0 on success or if Media Operation is not supported,
+ * negative error code on failure.
+ */

drivers/cxl/core/mbox.c int cxl_enumerate_cmds(struct cxl_memdev_state *mds) · line 918

Quoted 10 lines
drivers/cxl/core/mbox.c +10 -0
+#define CXL_MEDIA_OP_MAX_OPS 2
+
+int cxl_media_op_discover(struct cxl_memdev_state *mds)
+{
+	size_t out_size, in_size;
+	void *payload_in;
+	struct cxl_mbox_cmd mbox_cmd;
+	struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox;
+	struct cxl_mbox_media_op_input *payload;
+	struct cxl_mbox_media_op_discovery_args *args;

Ah. I'd forgotten this spec oddity of two different types of trailing member, or technically a flex array in the middle but one that is defined to be zero length if the structure
after it is present. Maybe cleanest option is
a separate cxl_mbox_media_op_discover_input structure? Probably with a comment to say there is a zero length array in the middle.

drivers/cxl/core/mbox.c int cxl_enumerate_cmds(struct cxl_memdev_state *mds) · line 928

Quoted 10 lines
drivers/cxl/core/mbox.c +10 -0
+	struct cxl_mbox_media_op_discovery_out *disc_out;
+	int rc, i;
+	u16 num_returned;
+
+	if (!test_bit(CXL_SEC_ENABLED_MEDIA_OPERATIONS,
+		      mds->security.enabled_cmds))
+		return 0;
+
+	in_size = sizeof(*payload) + sizeof(*args);
+	payload_in = kzalloc(in_size, GFP_KERNEL);

With a discovery specific input structure can use kzalloc_obj() and __free.

Quoted 4 lines
drivers/cxl/core/mbox.c +4 -0
+	if (!payload_in)
+		return -ENOMEM;
+
+	payload = payload_in;

drivers/cxl/core/mbox.c int cxl_enumerate_cmds(struct cxl_memdev_state *mds) · line 942

Quoted 10 lines
drivers/cxl/core/mbox.c +10 -0
+	payload->class = CXL_MEDIA_OP_CLASS_GENERAL;
+	payload->subclass = CXL_MEDIA_OP_SUBCLASS_DISCOVERY;
+	payload->dpa_range_count = 0;
+
+	args = payload_in + sizeof(*payload);
+	args->start_index = 0;
+	args->num_ops = cpu_to_le16(CXL_MEDIA_OP_MAX_OPS);
+
+	out_size = sizeof(*disc_out) +
+		   CXL_MEDIA_OP_MAX_OPS * sizeof(disc_out->ops[0]);

kzalloc_flex() and __free() again.

drivers/cxl/core/mbox.c int cxl_enumerate_cmds(struct cxl_memdev_state *mds) · line 945

Quoted 3 lines
drivers/cxl/core/mbox.c +3 -0
+	disc_out = kzalloc(out_size, GFP_KERNEL);
+	if (!disc_out) {
+		kfree(payload_in);

If you don't switch to __free(), use a second label in error path to just free this.

Quoted 14 lines
drivers/cxl/core/mbox.c +14 -0
+		return -ENOMEM;
+	}
+
+	mbox_cmd = (struct cxl_mbox_cmd) {
+		.opcode = CXL_MBOX_OP_MEDIA_OPERATION,
+		.payload_in = payload_in,
+		.size_in = in_size,
+		.payload_out = disc_out,
+		.size_out = out_size,
+		.min_out = sizeof(*disc_out),
+		.poll_count = 1,
+		.poll_interval_ms = 1000,
+	};
+

drivers/cxl/core/mbox.c int cxl_enumerate_cmds(struct cxl_memdev_state *mds) · line 969

Quoted 10 lines
drivers/cxl/core/mbox.c +10 -0
+	rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd);
+	if (rc < 0) {
+		dev_dbg(cxl_mbox->host,
+			"Media Operation Discovery failed: %d\n", rc);
+		goto out;
+	}
+
+	mds->media_op.granularity = le64_to_cpu(disc_out->granularity);
+	num_returned = min_t(u16, le16_to_cpu(disc_out->num_returned),
+			     CXL_MEDIA_OP_MAX_OPS);

If it claims to have returned more than we asked for, why not error out? I might be missing something.

Quoted 46 lines
drivers/cxl/core/mbox.c +39 -0
+
+	for (i = 0; i < num_returned; i++) {
+		u8 cls = disc_out->ops[i].class;
+		u8 sub = disc_out->ops[i].subclass;
+
+		if (cls == CXL_MEDIA_OP_CLASS_SANITIZE &&
+		    sub == CXL_MEDIA_OP_SUBCLASS_SANITIZE)
+			mds->media_op.sanitize_supported = true;
+		if (cls == CXL_MEDIA_OP_CLASS_SANITIZE &&
+		    sub == CXL_MEDIA_OP_SUBCLASS_ZERO)
+			mds->media_op.zero_supported = true;
+	}
+
+	dev_dbg(cxl_mbox->host,
+		"Media Operation: granularity=%llu sanitize=%d zero=%d\n",
+		mds->media_op.granularity,
+		mds->media_op.sanitize_supported,
+		mds->media_op.zero_supported);
+
+out:
+	kfree(disc_out);
+	kfree(payload_in);
+	return rc;
+}
+EXPORT_SYMBOL_NS_GPL(cxl_media_op_discover, "CXL");
+
 void cxl_event_trace_record(struct cxl_memdev *cxlmd,
 			    enum cxl_event_log_type type,
 			    enum cxl_event_type event_type,
@@ -1308,6 +1408,141 @@ int cxl_mem_sanitize(struct cxl_memdev *cxlmd, u16 cmd)
 	return -EBUSY;
 }
 
+static int __cxl_mem_media_op(struct cxl_memdev_state *mds, u8 class,
+			      u8 subclass, u64 dpa_start, u64 dpa_length)
+{
+	struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox;
+	struct cxl_mbox_media_op_input *payload;
+	struct cxl_mbox_cmd mbox_cmd;
+	u64 granularity;
+	size_t size;
+	int rc;
+
+	if (!test_bit(CXL_SEC_ENABLED_MEDIA_OPERATIONS,
+		      mds->security.enabled_cmds))
+		return -EOPNOTSUPP;

drivers/cxl/core/mbox.c int cxl_mem_sanitize(struct cxl_memdev *cxlmd, u16 cmd) · line 1433

Quoted 10 lines
drivers/cxl/core/mbox.c +10 -0
+
+	/* ensure the specific operation was reported by Discovery */
+	if (class == CXL_MEDIA_OP_CLASS_SANITIZE &&
+	    subclass == CXL_MEDIA_OP_SUBCLASS_SANITIZE &&
+	    !mds->media_op.sanitize_supported)
+		return -EOPNOTSUPP;
+	if (class == CXL_MEDIA_OP_CLASS_SANITIZE &&
+	    subclass == CXL_MEDIA_OP_SUBCLASS_ZERO &&
+	    !mds->media_op.zero_supported)
+		return -EOPNOTSUPP;

Maybe nest multiple ifs.

	if (class == CXL_MEDIA_OP_CLASS_SANITIZE) {
		if (subclass == CXL_MEDIA_OP_SUBCLASS_SANITIZE &&
		    !mds->media_op.sanitize_supported)
`			return -EOPNOTSUPP;
		if (subclass = CXL_MEDIA_OP_SUBCLASS_ZERO &&
		    !mds->media_op.zero_supported)
			return -EOPNOTSUPP;
	}

Doesn't matter much whilst we have only two, but if we get lots it may make them easier to find in the code.

...

drivers/cxl/core/mbox.c int cxl_mem_sanitize(struct cxl_memdev *cxlmd, u16 cmd) · line 1449

Quoted 3 lines
drivers/cxl/core/mbox.c +3 -0
+
+	size = sizeof(*payload) + sizeof(payload->dpa_range_list[0]);
+	payload = kzalloc(size, GFP_KERNEL);
	payload = kzalloc_flex(*payload, dpa_range_list, 1);

I think... (not gotten used to these yet so I'm not 100% sure on syntax).

Quoted 9 lines
drivers/cxl/core/mbox.c +9 -0
+	if (!payload)
+		return -ENOMEM;
+
+	payload->class = class;
+	payload->subclass = subclass;
+	payload->dpa_range_count = cpu_to_le32(1);
+	payload->dpa_range_list[0].starting_dpa = cpu_to_le64(dpa_start);
+	payload->dpa_range_list[0].length = cpu_to_le64(dpa_length);
+

drivers/cxl/core/mbox.c int cxl_mem_sanitize(struct cxl_memdev *cxlmd, u16 cmd) · line 1468

Quoted 10 lines
drivers/cxl/core/mbox.c +10 -0
+	mbox_cmd = (struct cxl_mbox_cmd) {
+		.opcode = CXL_MBOX_OP_MEDIA_OPERATION,
+		.payload_in = payload,
+		.size_in = size,
+		.poll_count = 60,
+		.poll_interval_ms = 1000,
+	};
+
+	rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd);
+	kfree(payload);

Apply some __free() magic.

Quoted 17 lines
drivers/cxl/core/mbox.c +10 -0
+
+	if (rc < 0) {
+		dev_dbg(cxl_mbox->host,
+			"Media Operation (class=%u sub=%u) failed: %d\n",
+			class, subclass, rc);
+		return rc;
+	}
+
+	return 0;
+}
 
drivers/cxl/core/memdev.c +0 -0
index 99e422594885..a78a0469fb41 100644

drivers/cxl/core/memdev.c static umode_t cxl_memdev_security_visible(struct kobject *kobj, · line 577

Quoted 7 lines
drivers/cxl/core/memdev.c +3 -1
@@ -538,13 +572,22 @@ static umode_t cxl_memdev_security_visible(struct kobject *kobj,
 	struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlmd->cxlds);
 
 	if (a == &dev_attr_security_sanitize.attr &&
-	    !test_bit(CXL_SEC_ENABLED_SANITIZE, mds->security.enabled_cmds))
+	    !test_bit(CXL_SEC_ENABLED_SANITIZE, mds->security.enabled_cmds) &&
+	    !(test_bit(CXL_SEC_ENABLED_MEDIA_OPERATIONS,
+		       mds->security.enabled_cmds) &&

I'd put that on one slightly long line for better readability.

Maybe add a comment on the dual use of the sanitize attribute. That had me confused for a bit.

drivers/cxl/core/memdev.c static umode_t cxl_memdev_security_visible(struct kobject *kobj, · line 587

Quoted 10 lines
drivers/cxl/core/memdev.c +4 -0
+	      mds->media_op.sanitize_supported))
 		return 0;
 
 	if (a == &dev_attr_security_erase.attr &&
 	    !test_bit(CXL_SEC_ENABLED_SECURE_ERASE, mds->security.enabled_cmds))
 		return 0;
 
+	if (a == &dev_attr_security_zero.attr &&
+	    (!test_bit(CXL_SEC_ENABLED_MEDIA_OPERATIONS,
+		       mds->security.enabled_cmds) ||

I'd put the two lines above on line line. Bit log but more redable.

Quoted 10 lines
drivers/cxl/core/memdev.c +3 -0
+	     !mds->media_op.zero_supported))
+		return 0;
+
 	return a->mode;
 }
 
drivers/cxl/cxlmem.h +0 -0
index 02054f233fc5..cde613b25581 100644

drivers/cxl/cxlmem.h struct cxl_fw_state { · line 360

Quoted 6 lines
drivers/cxl/cxlmem.h +5 -0
 
+/* Media Operation classes and subclasses (CXL 4.0 Table 8-331) */
+#define CXL_MEDIA_OP_CLASS_GENERAL	0x00
+#define CXL_MEDIA_OP_CLASS_SANITIZE	0x01
+
+#define CXL_MEDIA_OP_SUBCLASS_DISCOVERY	0x00

Maybe worth associating these more explicitly with which class they are in? Bit tricky to do without long names, but perhaps worth doing even so.

Quoted 6 lines
drivers/cxl/cxlmem.h +6 -0
+#define CXL_MEDIA_OP_SUBCLASS_SANITIZE	0x00
+#define CXL_MEDIA_OP_SUBCLASS_ZERO	0x01
+
+/* Media Operation DPA Range (CXL 4.0 Table 8-330) */
+struct cxl_media_op_dpa_range {
+	__le64 starting_dpa;

drivers/cxl/cxlmem.h struct cxl_fw_state { · line 376

Quoted 10 lines
drivers/cxl/cxlmem.h +10 -0
+	__le64 length;
+} __packed;
+
+/* Media Operation Input Payload (CXL 4.0 Table 8-329) */
+struct cxl_mbox_media_op_input {
+	u8 class;
+	u8 subclass;
+	u8 rsvd[2];
+	__le32 dpa_range_count;
+	struct cxl_media_op_dpa_range dpa_range_list[];

__counted_by_le(dpa_range_count)

Quoted 7 lines
drivers/cxl/cxlmem.h +7 -0
+} __packed;
+
+/* Discovery operation-specific arguments (CXL 4.0 Table 8-332) */
+struct cxl_mbox_media_op_discovery_args {
+	__le16 start_index;
+	__le16 num_ops;
+} __packed;

drivers/cxl/cxlmem.h struct cxl_fw_state { · line 393

Quoted 10 lines
drivers/cxl/cxlmem.h +10 -0
+
+/* Discovery output payload (CXL 4.0 Table 8-333) */
+struct cxl_mbox_media_op_discovery_out {
+	__le64 granularity;
+	__le16 total_supported;
+	__le16 num_returned;
+	struct {
+		u8 class;
+		u8 subclass;
+	} __packed ops[];  

__counted_by_le(num_returned) ?

Quoted 1 line

+} __packed;

author maintainer
#3 lore raw

Re: [PATCH 1/1] cxl/mbox: Support Media Operation

Quoted 14 lines · Jonathan Cameron

Documentation/ABI/testing/sysfs-bus-cxl Description:

Quoted 9 lines · On Mon, 23 Mar 2026 13:40:13 -0700 Davidlohr Bueso <[email protected]> wrote:
Documentation/ABI/testing/sysfs-bus-cxl +9 -0
+What:		/sys/bus/cxl/devices/memX/security/zero
+Date:		March, 2026
+KernelVersion:	v7.1
+Contact:	[email protected]
+Description:
+		(WO) Write a DPA range as 'start length' in bytes to this
+		attribute to zero a specific region of device media via the
+		Media Operation command. After completion, reads from the
+		specified range will return zero.

This is only true if we've flushed any caches on the CPU Side of the link. Will happen anyway I think as part of making the memory available.

Correct, same as for current sanitize/secure erase, described in the sysfs doc.

Quoted 1 line

Perhaps not worth mentioning that complexity here.

Still important, I will mention this in the changelog.

Quoted 34 lines
Quoted 34 lines

The start address and

Documentation/ABI/testing/sysfs-bus-cxl +5 -0
+		length must be aligned to the device's media operation
+		granularity. If this file is not present, then the device
+		does not support the Media Operation zero command.
+
+
 What:		/sys/bus/cxl/devices/memX/firmware/
 Date:		April, 2023
 KernelVersion:	v6.5
drivers/cxl/core/mbox.c +18 -0
index aaa5c6277ebf..b7047cb9abed 100644
 
 
+/**
+ * cxl_media_op_discover() - Discover supported media operation
+ * @mds: The device data for the operation
+ *
+ * Discover any available Media Operations.
+ *
+ * Return: 0 on success or if Media Operation is not supported,
+ * negative error code on failure.
+ */
+#define CXL_MEDIA_OP_MAX_OPS 2
+
+int cxl_media_op_discover(struct cxl_memdev_state *mds)
+{
+	size_t out_size, in_size;
+	void *payload_in;
+	struct cxl_mbox_cmd mbox_cmd;
+	struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox;
+	struct cxl_mbox_media_op_input *payload;
Quoted 8 lines

drivers/cxl/core/mbox.c int cxl_enumerate_cmds(struct cxl_memdev_state *mds) · line 918

Quoted 1 line
drivers/cxl/core/mbox.c +1 -0
+	struct cxl_mbox_media_op_discovery_args *args;

Ah. I'd forgotten this spec oddity of two different types of trailing member, or technically a flex array in the middle but one that is defined to be zero length if the structure
after it is present. Maybe cleanest option is
a separate cxl_mbox_media_op_discover_input structure? Probably with a comment to say there is a zero length array in the middle.

Yeah this is a pita.

...

Quoted 16 lines
Quoted 16 lines
drivers/cxl/core/mbox.c +16 -0
+	mbox_cmd = (struct cxl_mbox_cmd) {
+		.opcode = CXL_MBOX_OP_MEDIA_OPERATION,
+		.payload_in = payload_in,
+		.size_in = in_size,
+		.payload_out = disc_out,
+		.size_out = out_size,
+		.min_out = sizeof(*disc_out),
+		.poll_count = 1,
+		.poll_interval_ms = 1000,
+	};
+
+	rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd);
+	if (rc < 0) {
+		dev_dbg(cxl_mbox->host,
+			"Media Operation Discovery failed: %d\n", rc);
+		goto out;
Quoted 8 lines

drivers/cxl/core/mbox.c int cxl_enumerate_cmds(struct cxl_memdev_state *mds) · line 969

Quoted 5 lines
drivers/cxl/core/mbox.c +5 -0
+	}
+
+	mds->media_op.granularity = le64_to_cpu(disc_out->granularity);
+	num_returned = min_t(u16, le16_to_cpu(disc_out->num_returned),
+			     CXL_MEDIA_OP_MAX_OPS);

If it claims to have returned more than we asked for, why not error out? I might be missing something.

Made sense at the time, reconsidering I tend to agree.

...

Quoted 33 lines
Quoted 13 lines
drivers/cxl/core/mbox.c +13 -0
+static int __cxl_mem_media_op(struct cxl_memdev_state *mds, u8 class,
+			      u8 subclass, u64 dpa_start, u64 dpa_length)
+{
+	struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox;
+	struct cxl_mbox_media_op_input *payload;
+	struct cxl_mbox_cmd mbox_cmd;
+	u64 granularity;
+	size_t size;
+	int rc;
+
+	if (!test_bit(CXL_SEC_ENABLED_MEDIA_OPERATIONS,
+		      mds->security.enabled_cmds))
+		return -EOPNOTSUPP;

drivers/cxl/core/mbox.c int cxl_mem_sanitize(struct cxl_memdev *cxlmd, u16 cmd) · line 1433

Quoted 10 lines
drivers/cxl/core/mbox.c +10 -0
+
+	/* ensure the specific operation was reported by Discovery */
+	if (class == CXL_MEDIA_OP_CLASS_SANITIZE &&
+	    subclass == CXL_MEDIA_OP_SUBCLASS_SANITIZE &&
+	    !mds->media_op.sanitize_supported)
+		return -EOPNOTSUPP;
+	if (class == CXL_MEDIA_OP_CLASS_SANITIZE &&
+	    subclass == CXL_MEDIA_OP_SUBCLASS_ZERO &&
+	    !mds->media_op.zero_supported)
+		return -EOPNOTSUPP;

Maybe nest multiple ifs.

	if (class == CXL_MEDIA_OP_CLASS_SANITIZE) {
		if (subclass == CXL_MEDIA_OP_SUBCLASS_SANITIZE &&
		    !mds->media_op.sanitize_supported)
`			return -EOPNOTSUPP;
		if (subclass = CXL_MEDIA_OP_SUBCLASS_ZERO &&
		    !mds->media_op.zero_supported)
			return -EOPNOTSUPP;
	}
Quoted 2 lines

Doesn't matter much whilst we have only two, but if we get lots it may make them easier to find in the code.

I will use a switch.

... and ack to the rest of the feedback.

Thanks,
Davidlohr

#4 lore raw

Re: [PATCH 1/1] cxl/mbox: Support Media Operation

In reply to Davidlohr Bueso
kernel test robot noticed the following build warnings:

Hi Davidlohr,

kernel test robot noticed the following build warnings:

[auto build test WARNING on cxl/next]
[also build test WARNING on next-20260324]
[cannot apply to linus/master cxl/pending v7.0-rc5]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Davidlohr-Bueso/cxl-mbox-Support-Media-Operation/20260325-172343
base:   https://git.kernel.org/pub/scm/linux/kernel/git/cxl/cxl.git next
patch link:    https://lore.kernel.org/r/20260323204013.4010634-2-dave%40stgolabs.net
patch subject: [PATCH 1/1] cxl/mbox: Support Media Operation
config: x86_64-rhel-9.4-ltp (https://download.01.org/0day-ci/archive/20260325/[email protected]/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260325/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> Warning: drivers/cxl/core/mbox.c:908 expecting prototype for cxl_media_op_discover(). Prototype was for CXL_MEDIA_OP_MAX_OPS() instead
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#5 lore raw

Re: [PATCH 1/1] cxl/mbox: Support Media Operation

In reply to Davidlohr Bueso
kernel test robot noticed the following build warnings:

Hi Davidlohr,

kernel test robot noticed the following build warnings:

[auto build test WARNING on cxl/next]
[also build test WARNING on next-20260324]
[cannot apply to linus/master cxl/pending v7.0-rc5]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Davidlohr-Bueso/cxl-mbox-Support-Media-Operation/20260325-172343
base:   https://git.kernel.org/pub/scm/linux/kernel/git/cxl/cxl.git next
patch link:    https://lore.kernel.org/r/20260323204013.4010634-2-dave%40stgolabs.net
patch subject: [PATCH 1/1] cxl/mbox: Support Media Operation
config: openrisc-randconfig-r071-20260325 (https://download.01.org/0day-ci/archive/20260325/[email protected]/config)
compiler: or1k-linux-gcc (GCC) 14.3.0
smatch: v0.5.0-9004-gb810ac53
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260325/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> Warning: drivers/cxl/core/mbox.c:908 expecting prototype for cxl_media_op_discover(). Prototype was for CXL_MEDIA_OP_MAX_OPS() instead
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#6 lore raw
In reply to Davidlohr Bueso
Quoted 9 lines · On Mon, 23 Mar 2026 13:40:12 -0700 Davidlohr Bueso <[email protected]> wrote:

Hello,

This implements support for Media Operation cmd. It is marked RFC because of these noteworthy points:

(i) Sysfs interfaces. These changes implement 'security/zero' and multiplex 'security/sanitize' to allow dpa, len pairs for the range (no impact for current usage as whole-device). This breaks the 1 value per file "rule", and would be a first for drivers/cxl/*, so am I open to suggestions.

Value is a vague sort of term. It's one thing (an range) so seems fine to me.

Quoted 3 lines

(ii) Background cmd handling semantics. Media operations are synchronous,
which for raged zeroing operations makes sense and for range sanitize

ranged

Quoted 5 lines

differs from the current async handling for whole-device, so user response differs significantly, something I am not crazy about. Another downside of this is that it exposes lock hold times to be user-controllable, but is no different than what we do for fw. Fundamentally, users have the responsibility to break up the work into sane ranges.

We could apply a limit on this and make that discoverable via another sysfs attribute. So let userspace request in say 1Gig chunks. Would have to check that the device supports that granularity though.
I don't think there is anything in the spec today to let us query how long something takes (which would be a better way to bound this).

Quoted 5 lines

Further, this is
without considering abortable bg cmds[0], which, per-spec, are unsupported today for media operations.

Tested with qemu + fixlets (https://lore.kernel.org/all/[email protected]/)

I think those all landed upstream today and are getting pulled into qemu stable as well.

Quoted 21 lines

Applies against latest 'next' from cxl.git.

[0] https://lore.kernel.org/all/[email protected]/

Thanks!

Davidlohr Bueso (1):
cxl/mbox: Support Media Operation

 Documentation/ABI/testing/sysfs-bus-cxl |  31 +++-
 drivers/cxl/core/mbox.c                 | 235 ++++++++++++++++++++++++
 drivers/cxl/core/memdev.c               |  45 ++++-
 drivers/cxl/cxlmem.h                    |  59 ++++++
 drivers/cxl/pci.c                       |   4 +
 5 files changed, 369 insertions(+), 5 deletions(-)

--
2.39.5

author maintainer
#7 lore raw
Quoted 8 lines · Jonathan Cameron
Quoted 5 lines · On Mon, 23 Mar 2026 13:40:12 -0700 Davidlohr Bueso <[email protected]> wrote:

Hello,

This implements support for Media Operation cmd. It is marked RFC because of these noteworthy points:

Quoted 6 lines
Quoted 4 lines

(i) Sysfs interfaces. These changes implement 'security/zero' and multiplex 'security/sanitize' to allow dpa, len pairs for the range (no impact for current usage as whole-device). This breaks the 1 value per file "rule", and would be a first for drivers/cxl/*, so am I open to suggestions.

Value is a vague sort of term. It's one thing (an range) so seems fine to me.

I agree.

Quoted 13 lines
Quoted 4 lines

(ii) Background cmd handling semantics. Media operations are synchronous,
which for raged zeroing operations makes sense and for range sanitize

ranged

Quoted 5 lines

differs from the current async handling for whole-device, so user response differs significantly, something I am not crazy about. Another downside of this is that it exposes lock hold times to be user-controllable, but is no different than what we do for fw. Fundamentally, users have the responsibility to break up the work into sane ranges.

Quoted 3 lines

We could apply a limit on this and make that discoverable via another sysfs attribute. So let userspace request in say 1Gig chunks. Would have to check that the device supports that granularity though.

If we agree that this will be performed synchronously, then yes, adding a size cap would be necessary. I don't love having it in ie: security/range_limit(?), but that could be created at discovery time when we know the granularity. Another way would be to avoid the extra file and just print the limit in the error message - but I guess dev_dbg() would be needed to not spam the dmesg. In any case with a cap, while no guarantees, this also reduces the chances of the user triggering driver timeouts.

Quoted 2 lines

I don't think there is anything in the spec today to let us query how long something takes (which would be a better way to bound this).

Correct, that is just something Scan Media allows for - and yes, that's exactly the sane way to do the work breakup.

Thanks,
Davidlohr

#8 lore raw
Quoted 43 lines · On Tue, 24 Mar 2026 11:07:54 -0700 Davidlohr Bueso <[email protected]> wrote:
Quoted 8 lines · Jonathan Cameron
Quoted 5 lines · On Mon, 23 Mar 2026 13:40:12 -0700 Davidlohr Bueso <[email protected]> wrote:

Hello,

This implements support for Media Operation cmd. It is marked RFC because of these noteworthy points:

Quoted 6 lines
Quoted 4 lines

(i) Sysfs interfaces. These changes implement 'security/zero' and multiplex 'security/sanitize' to allow dpa, len pairs for the range (no impact for current usage as whole-device). This breaks the 1 value per file "rule", and would be a first for drivers/cxl/*, so am I open to suggestions.

Value is a vague sort of term. It's one thing (an range) so seems fine to me.

I agree.

Quoted 13 lines
Quoted 4 lines

(ii) Background cmd handling semantics. Media operations are synchronous,
which for raged zeroing operations makes sense and for range sanitize

ranged

Quoted 5 lines

differs from the current async handling for whole-device, so user response differs significantly, something I am not crazy about. Another downside of this is that it exposes lock hold times to be user-controllable, but is no different than what we do for fw. Fundamentally, users have the responsibility to break up the work into sane ranges.

Quoted 3 lines

We could apply a limit on this and make that discoverable via another sysfs attribute. So let userspace request in say 1Gig chunks. Would have to check that the device supports that granularity though.

If we agree that this will be performed synchronously, then yes, adding a size cap would be necessary. I don't love having it in ie: security/range_limit(?), but that could be created at discovery time when we know the granularity. Another way would be to avoid the extra file and just print the limit in the error message - but I guess dev_dbg() would be needed to not spam the dmesg. In any case with a cap, while no guarantees, this also reduces the chances of the user triggering driver timeouts.

Quoted 6 lines
Quoted 2 lines

I don't think there is anything in the spec today to let us query how long something takes (which would be a better way to bound this).

Correct, that is just something Scan Media allows for - and yes, that's exactly the sane way to do the work breakup.

Yo John,

I'm sure you are keeping a list of potential improvements for the appropriate group that I believe you have some leadership role in ;)

I'd like devices to provide info to allow us to establish an upper bound on how long a zero / sanitize media operation would take. This one doesn't need to be code first so we can consider this a potential feature request.

Jonathan

Quoted 2 lines

Thanks,
Davidlohr

#9 lore raw
Quoted 54 lines · Jonathan Cameron
Quoted 43 lines · On Tue, 24 Mar 2026 11:07:54 -0700 Davidlohr Bueso <[email protected]> wrote:
Quoted 8 lines · Jonathan Cameron
Quoted 5 lines · On Mon, 23 Mar 2026 13:40:12 -0700 Davidlohr Bueso <[email protected]> wrote:

Hello,

This implements support for Media Operation cmd. It is marked RFC because of these noteworthy points:

Quoted 6 lines
Quoted 4 lines

(i) Sysfs interfaces. These changes implement 'security/zero' and multiplex 'security/sanitize' to allow dpa, len pairs for the range (no impact for current usage as whole-device). This breaks the 1 value per file "rule", and would be a first for drivers/cxl/*, so am I open to suggestions.

Value is a vague sort of term. It's one thing (an range) so seems fine to me.

I agree.

Quoted 13 lines
Quoted 4 lines

(ii) Background cmd handling semantics. Media operations are synchronous,
which for raged zeroing operations makes sense and for range sanitize

ranged

Quoted 5 lines

differs from the current async handling for whole-device, so user response differs significantly, something I am not crazy about. Another downside of this is that it exposes lock hold times to be user-controllable, but is no different than what we do for fw. Fundamentally, users have the responsibility to break up the work into sane ranges.

Quoted 3 lines

We could apply a limit on this and make that discoverable via another sysfs attribute. So let userspace request in say 1Gig chunks. Would have to check that the device supports that granularity though.

If we agree that this will be performed synchronously, then yes, adding a size cap would be necessary. I don't love having it in ie: security/range_limit(?), but that could be created at discovery time when we know the granularity. Another way would be to avoid the extra file and just print the limit in the error message - but I guess dev_dbg() would be needed to not spam the dmesg. In any case with a cap, while no guarantees, this also reduces the chances of the user triggering driver timeouts.

Quoted 6 lines
Quoted 2 lines

I don't think there is anything in the spec today to let us query how long something takes (which would be a better way to bound this).

Correct, that is just something Scan Media allows for - and yes, that's exactly the sane way to do the work breakup.

Yo John,

Quoted 12 lines

I'm sure you are keeping a list of potential improvements for the appropriate group that I believe you have some leadership role in ;)

I'd like devices to provide info to allow us to establish an upper bound on how long a zero / sanitize media operation would take. This one doesn't need to be code first so we can consider this a potential feature request.

Jonathan

Quoted 2 lines

Thanks,
Davidlohr

Acknowledged. Are you thinking of a one-off for sanitize, or is there a class of commands that might ought to provide such an upper-bound?

I'd be happy to try to push something through, but the solution ought to come from somebody who experiences the inconvenience directtly :D

Regards,
John

#10 lore raw
Quoted 62 lines · On Fri, 27 Mar 2026 10:17:01 -0500 John Groves <[email protected]> wrote:
Quoted 61 lines · Jonathan Cameron
Quoted 43 lines · On Tue, 24 Mar 2026 11:07:54 -0700 Davidlohr Bueso <[email protected]> wrote:
Quoted 8 lines · Jonathan Cameron
Quoted 5 lines · On Mon, 23 Mar 2026 13:40:12 -0700 Davidlohr Bueso <[email protected]> wrote:

Hello,

This implements support for Media Operation cmd. It is marked RFC because of these noteworthy points:

Quoted 6 lines
Quoted 4 lines

(i) Sysfs interfaces. These changes implement 'security/zero' and multiplex 'security/sanitize' to allow dpa, len pairs for the range (no impact for current usage as whole-device). This breaks the 1 value per file "rule", and would be a first for drivers/cxl/*, so am I open to suggestions.

Value is a vague sort of term. It's one thing (an range) so seems fine to me.

I agree.

Quoted 13 lines
Quoted 4 lines

(ii) Background cmd handling semantics. Media operations are synchronous,
which for raged zeroing operations makes sense and for range sanitize

ranged

Quoted 5 lines

differs from the current async handling for whole-device, so user response differs significantly, something I am not crazy about. Another downside of this is that it exposes lock hold times to be user-controllable, but is no different than what we do for fw. Fundamentally, users have the responsibility to break up the work into sane ranges.

Quoted 3 lines

We could apply a limit on this and make that discoverable via another sysfs attribute. So let userspace request in say 1Gig chunks. Would have to check that the device supports that granularity though.

If we agree that this will be performed synchronously, then yes, adding a size cap would be necessary. I don't love having it in ie: security/range_limit(?), but that could be created at discovery time when we know the granularity. Another way would be to avoid the extra file and just print the limit in the error message - but I guess dev_dbg() would be needed to not spam the dmesg. In any case with a cap, while no guarantees, this also reduces the chances of the user triggering driver timeouts.

Quoted 6 lines
Quoted 2 lines

I don't think there is anything in the spec today to let us query how long something takes (which would be a better way to bound this).

Correct, that is just something Scan Media allows for - and yes, that's exactly the sane way to do the work breakup.

Yo John,

I'm sure you are keeping a list of potential improvements for the appropriate group that I believe you have some leadership role in ;)

I'd like devices to provide info to allow us to establish an upper bound on how long a zero / sanitize media operation would take. This one doesn't need to be code first so we can consider this a potential feature request.

Quoted 8 lines
Quoted 5 lines

Jonathan

Quoted 2 lines

Thanks,
Davidlohr

Acknowledged. Are you thinking of a one-off for sanitize, or is there a class of commands that might ought to provide such an upper-bound?

Not sure - probably one to through at the spec group and see if anyone can think of more usecases beyond the various media operations.

Quoted 3 lines

I'd be happy to try to push something through, but the solution ought to come from somebody who experiences the inconvenience directtly :D

Over to you Davidlohr ;)

Quoted 6 lines

Regards,
John

author maintainer
#11 lore raw
Quoted 19 lines · Jonathan Cameron
Quoted 16 lines · On Fri, 27 Mar 2026 10:17:01 -0500 John Groves <[email protected]> wrote:
Quoted 14 lines · Jonathan Cameron

Yo John,

I'm sure you are keeping a list of potential improvements for the appropriate group that I believe you have some leadership role in ;)

I'd like devices to provide info to allow us to establish an upper bound on how long a zero / sanitize media operation would take. This one doesn't need to be code first so we can consider this a potential feature request.

Jonathan

Quoted 2 lines

Thanks,
Davidlohr

Quoted 5 lines
Quoted 2 lines

Acknowledged. Are you thinking of a one-off for sanitize, or is there a class of commands that might ought to provide such an upper-bound?

Not sure - probably one to through at the spec group and see if anyone can think of more usecases beyond the various media operations.

Right, so sanitize has always been the painfully slow one.

Quoted 3 lines
Quoted 3 lines

I'd be happy to try to push something through, but the solution ought to come from somebody who experiences the inconvenience directtly :D

I'm really doing this for better end-to-end BI testing, so not really running into any issues (much less on real hw); mostly just forward thinking for/if whenever there is a device with this.

Thanks,
Davidlohr