CXL: Standalone switch CCI driver
cover CXL: Standalone switch CCI driver
Note that the PCI SIG has recently release the MMPT specification which is similar to the CXL mailboxes. I would suggest that we don't get too distracted by that, but keep in mind that we may end up factoring some aspects of this code out into the PCI core and just use them here (at some future date!)
https://members.pcisig.com/wg/PCI-SIG/document/20109?uploaded=1
Based on v6.6-rc6
Chances since rfc4.
- Mostly a rebase because of fixes around sanitize handling.
- Dropped new command support. For, now RAW all the way.
- Aim is to support getting the QEMU emulation upstream.
I'll be posting user space code to test this against the QEMU emulation shortly. I just need to finish writing the documentation as getting it up and running is less trivial than I'd like (lots of moving parts).
(I need this series on the list so I can point to it ;)
Introduction:
CXL r3.0 introduced the option for a PCI function, intended to sit on an upstream port of a CXL switch. This function provides a mailbox interface similar to that seen on CXL type 3 devices. However, the command set is mostly different and intended for Fabric management. Note however that as we add support for multi headed devices (MHDs) a subset of commands will be available on selected MHD type 3 mailboxes. (tunnelling for DCD commands for example)
See: CXL rev 3.0
7.2.9 Switch Mailbox CCI
8.1.13 Switch Mailbox CCI Configuration Space Layout 8.2.8.6 Switch Mailbox CCI capability
It is probably relatively unusual that a typical host of CXL devices will have access to the one of these devices, in many cases they will be on a port connected to a BMC or similar. There are a few use cases where the host might be in charge of the configuration.
These are very convenient for testing in conjunction with the QEMU emulation. CXL switch and type 3 emulation is in QEMU is not complex enough to make these particular interesting but that should change soon.
For now don't provide any additional commands over those defined for the main CXL mailbox and in practice that means the RAW command path under CONFIG_CXL_MEM_RAW_COMMANDS is used with the IOCTL to interact with this device and the fabric beyond it.
The foot guns are behind the same "don't enable this unless you know what you are doing" barrier as they are for type 3 devices. Note that the blast radius is far worse than for a CXL type 3 mailbox. This device can be used to rip memory out from under _other live hosts_.
Jonathan Cameron (4):
cxl: mbox: Preparatory move of functions to core/mbox.c and cxlmbox.h
cxl: mbox: Factor out the mbox specific data for reuse in switch cci
PCI: Add PCI_CLASS_SERIAL_CXL_SWITCH_CCI class ID to pci_ids.h cxl/pci: Add support for stand alone CXL Switch mailbox CCI
drivers/cxl/Kconfig | 14 ++ drivers/cxl/Makefile | 2 + drivers/cxl/core/Makefile | 1 + drivers/cxl/core/core.h | 12 +- drivers/cxl/core/mbox.c | 453 +++++++++++++++++++++++++++-------- drivers/cxl/core/memdev.c | 44 ++-- drivers/cxl/core/regs.c | 35 ++- drivers/cxl/core/switchdev.c | 129 ++++++++++ drivers/cxl/cxl.h | 4 +- drivers/cxl/cxlmbox.h | 201 ++++++++++++++++ drivers/cxl/cxlmem.h | 176 ++------------ drivers/cxl/pci.c | 442 ++++++++++------------------------ drivers/cxl/pmem.c | 6 +- drivers/cxl/security.c | 13 +- drivers/cxl/switch.h | 19 ++ drivers/cxl/switchdev.c | 169 +++++++++++++ include/linux/pci_ids.h | 1 + 17 files changed, 1123 insertions(+), 598 deletions(-) create mode 100644 drivers/cxl/core/switchdev.c create mode 100644 drivers/cxl/cxlmbox.h create mode 100644 drivers/cxl/switch.h create mode 100644 drivers/cxl/switchdev.c
Signature · 1 line
2.39.2
PATCH 1/4 cxl: mbox: Preparatory move of functions to core/mbox.c and cxlmbox.h
A later patch will modify this code to separate out the mbox functionality from the memdev. This patch is intended to make that a little more readable. Related move of cxl_err(), cxl_cmd_err() to make them accessible from other parts of the cxl core code.
--- drivers/cxl/core/mbox.c | 261 ++++++++++++++++++++++++++++++++++++++- drivers/cxl/cxlmbox.h | 146 ++++++++++++++++++++++ drivers/cxl/cxlmem.h | 148 ++-------------------- drivers/cxl/pci.c | 265 +--------------------------------------- 4 files changed, 418 insertions(+), 402 deletions(-)
drivers/cxl/core/mbox.c +259 -2
@@ -1,10 +1,12 @@// SPDX-License-Identifier: GPL-2.0-only /* Copyright(c) 2020 Intel Corporation. All rights reserved. */ +#include <linux/io-64-nonatomic-lo-hi.h> #include <linux/security.h> #include <linux/debugfs.h> #include <linux/ktime.h> #include <linux/mutex.h> #include <asm/unaligned.h> +#include <cxlmbox.h> #include <cxlpci.h> #include <cxlmem.h> #include <cxl.h>
@@ -12,6 +14,14 @@#include "core.h" #include "trace.h" + +/* CXL 2.0 - 8.2.8.4 */ +#define CXL_MAILBOX_TIMEOUT_MS (2 * HZ) + +#define cxl_doorbell_busy (cxlds) \ + (readl((cxlds)->regs.mbox + CXLDEV_MBOX_CTRL_OFFSET) & \ + CXLDEV_MBOX_CTRL_DOORBELL) +static bool cxl_raw_allow_all;/**
@@ -219,6 +229,253 @@ static const char *cxl_mem_opcode_to_name(u16 opcode)return cxl_command_names[c->info.id].name; } +int cxl_pci_mbox_wait_for_doorbell (struct cxl_dev_state *cxlds) +{ +const unsigned long start = jiffies; +unsigned long end = start; + +while (cxl_doorbell_busy (cxlds)) { + end = jiffies; + +if (time_after (end, start +CXL_MAILBOX_TIMEOUT_MS )) { +/* Check again in case preempted before timeout test */ +if (!cxl_doorbell_busy (cxlds)) +break ; +return -ETIMEDOUT ; + } +cpu_relax (); + } + +dev_dbg (cxlds->dev,"Doorbell wait took %dms" , +jiffies_to_msecs (end) -jiffies_to_msecs (start)); +return 0 ; +} +EXPORT_SYMBOL_NS_GPL (cxl_pci_mbox_wait_for_doorbell,CXL ); + +bool cxl_mbox_background_complete (struct cxl_dev_state *cxlds) +{ +u64 reg; + + reg =readq (cxlds->regs.mbox +CXLDEV_MBOX_BG_CMD_STATUS_OFFSET ); +return FIELD_GET (CXLDEV_MBOX_BG_CMD_COMMAND_PCT_MASK , reg) ==100 ; +} +EXPORT_SYMBOL_NS_GPL (cxl_mbox_background_complete,CXL ); + +/** +* __cxl_pci_mbox_send_cmd() - Execute a mailbox command +* @mds: The memory device driver data +* @mbox_cmd: Command to send to the memory device. +* +* Context: Any context. Expects mbox_mutex to be held. +* Return: -ETIMEDOUT if timeout occurred waiting for completion. 0 on success. +* Caller should check the return code in @mbox_cmd to make sure it +* succeeded. +* +* This is a generic form of the CXL mailbox send command thus only using the +* registers defined by the mailbox capability ID - CXL 2.0 8.2.8.4. Memory +* devices, and perhaps other types of CXL devices may have further information +* available upon error conditions. Driver facilities wishing to send mailbox +* commands should use the wrapper command. +* +* The CXL spec allows for up to two mailboxes. The intention is for the primary +* mailbox to be OS controlled and the secondary mailbox to be used by system +* firmware. This allows the OS and firmware to communicate with the device and +* not need to coordinate with each other. The driver only uses the primary +* mailbox. +*/ +static int __cxl_pci_mbox_send_cmd (struct cxl_memdev_state *mds, +struct cxl_mbox_cmd *mbox_cmd) +{ +struct cxl_dev_state *cxlds = &mds->cxlds; +void __iomem *payload = cxlds->regs.mbox +CXLDEV_MBOX_PAYLOAD_OFFSET ; +struct device *dev = cxlds->dev; +u64 cmd_reg, status_reg; +size_t out_len; +int rc; + +lockdep_assert_held (&mds->mbox_mutex); + +/* +* Here are the steps from 8.2.8.4 of the CXL 2.0 spec. +* 1. Caller reads MB Control Register to verify doorbell is clear +* 2. Caller writes Command Register +* 3. Caller writes Command Payload Registers if input payload is non-empty +* 4. Caller writes MB Control Register to set doorbell +* 5. Caller either polls for doorbell to be clear or waits for interrupt if configured +* 6. Caller reads MB Status Register to fetch Return code +* 7. If command successful, Caller reads Command Register to get Payload Length +* 8. If output payload is non-empty, host reads Command Payload Registers +* +* Hardware is free to do whatever it wants before the doorbell is rung, +* and isn't allowed to change anything after it clears the doorbell. As +* such, steps 2 and 3 can happen in any order, and steps 6, 7, 8 can +* also happen in any order (though some orders might not make sense). +*/ + +/* #1 */ +if (cxl_doorbell_busy (cxlds)) { +u64 md_status = +readq (cxlds->regs.memdev +CXLMDEV_STATUS_OFFSET ); + +cxl_cmd_err (cxlds->dev, mbox_cmd, md_status, +"mailbox queue busy" ); +return -EBUSY ; + } + +/* +* With sanitize polling, hardware might be done and the poller still +* not be in sync. Ensure no new command comes in until so. Keep the +* hardware semantics and only allow device health status. +*/ +if (mds->security.poll_tmo_secs >0 ) { +if (mbox_cmd->opcode !=CXL_MBOX_OP_GET_HEALTH_INFO ) +return -EBUSY ; + } + + cmd_reg =FIELD_PREP (CXLDEV_MBOX_CMD_COMMAND_OPCODE_MASK , + mbox_cmd->opcode); +if (mbox_cmd->size_in) { +if (WARN_ON (!mbox_cmd->payload_in)) +return -EINVAL ; + + cmd_reg |=FIELD_PREP (CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK , + mbox_cmd->size_in); +memcpy_toio (payload, mbox_cmd->payload_in, mbox_cmd->size_in); + } + +/* #2, #3 */ +writeq (cmd_reg, cxlds->regs.mbox +CXLDEV_MBOX_CMD_OFFSET ); + +/* #4 */ +dev_dbg (dev,"Sending command: 0x%04x\n" , mbox_cmd->opcode); +writel (CXLDEV_MBOX_CTRL_DOORBELL , + cxlds->regs.mbox +CXLDEV_MBOX_CTRL_OFFSET ); + +/* #5 */ + rc =cxl_pci_mbox_wait_for_doorbell (cxlds); +if (rc == -ETIMEDOUT ) { +u64 md_status =readq (cxlds->regs.memdev +CXLMDEV_STATUS_OFFSET ); + +cxl_cmd_err (cxlds->dev, mbox_cmd, md_status,"mailbox timeout" ); +return rc; + } + +/* #6 */ + status_reg =readq (cxlds->regs.mbox +CXLDEV_MBOX_STATUS_OFFSET ); + mbox_cmd->return_code = +FIELD_GET (CXLDEV_MBOX_STATUS_RET_CODE_MASK , status_reg); + +/* +* Handle the background command in a synchronous manner. +* +* All other mailbox commands will serialize/queue on the mbox_mutex, +* which we currently hold. Furthermore this also guarantees that +* cxl_mbox_background_complete() checks are safe amongst each other, +* in that no new bg operation can occur in between. +* +* Background operations are timesliced in accordance with the nature +* of the command. In the event of timeout, the mailbox state is +* indeterminate until the next successful command submission and the +* driver can get back in sync with the hardware state. +*/ +if (mbox_cmd->return_code ==CXL_MBOX_CMD_RC_BACKGROUND ) { +u64 bg_status_reg; +int i, timeout; + +/* +* Sanitization is a special case which monopolizes the device +* and cannot be timesliced. Handle asynchronously instead, +* and allow userspace to poll(2) for completion. +*/ +if (mbox_cmd->opcode ==CXL_MBOX_OP_SANITIZE ) { +if (mds->security.poll) { +/* hold the device throughout */ +get_device (cxlds->dev); + +/* give first timeout a second */ + timeout =1 ; + mds->security.poll_tmo_secs = timeout; +queue_delayed_work (system_wq, + &mds->security.poll_dwork, + timeout *HZ ); + } + +dev_dbg (dev,"Sanitization operation started\n" ); + goto success; + } + +dev_dbg (dev,"Mailbox background operation (0x%04x) started\n" , + mbox_cmd->opcode); + + timeout = mbox_cmd->poll_interval_ms; +for (i =0 ; i < mbox_cmd->poll_count; i++) { +if (rcuwait_wait_event_timeout (&mds->mbox_wait, +cxl_mbox_background_complete (cxlds), +TASK_UNINTERRUPTIBLE , +msecs_to_jiffies (timeout)) >0 ) +break ; + } + +if (!cxl_mbox_background_complete (cxlds)) { +dev_err (dev,"timeout waiting for background (%d ms)\n" , + timeout * mbox_cmd->poll_count); +return -ETIMEDOUT ; + } + + bg_status_reg =readq (cxlds->regs.mbox + +CXLDEV_MBOX_BG_CMD_STATUS_OFFSET ); + mbox_cmd->return_code = +FIELD_GET (CXLDEV_MBOX_BG_CMD_COMMAND_RC_MASK , + bg_status_reg); +dev_dbg (dev, +"Mailbox background operation (0x%04x) completed\n" , + mbox_cmd->opcode); + } + +if (mbox_cmd->return_code !=CXL_MBOX_CMD_RC_SUCCESS ) { +dev_dbg (dev,"Mailbox operation had an error: %s\n" , +cxl_mbox_cmd_rc2str (mbox_cmd)); +return 0 ;/* completed but caller must check return_code */ + } + +success: +/* #7 */ + cmd_reg =readq (cxlds->regs.mbox +CXLDEV_MBOX_CMD_OFFSET ); + out_len =FIELD_GET (CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK , cmd_reg); + +/* #8 */ +if (out_len && mbox_cmd->payload_out) { +/* +* Sanitize the copy. If hardware misbehaves, out_len per the +* spec can actually be greater than the max allowed size (21 +* bits available but spec defined 1M max). The caller also may +* have requested less data than the hardware supplied even +* within spec. +*/ +size_t n; + + n =min3 (mbox_cmd->size_out, mds->payload_size, out_len); +memcpy_fromio (mbox_cmd->payload_out, payload, n); + mbox_cmd->size_out = n; + }else { + mbox_cmd->size_out =0 ; + } + +return 0 ; +} + +static int cxl_pci_mbox_send (struct cxl_memdev_state *mds, +struct cxl_mbox_cmd *cmd) +{ +int rc; + +mutex_lock_io (&mds->mbox_mutex); + rc =__cxl_pci_mbox_send_cmd (mds, cmd); +mutex_unlock (&mds->mbox_mutex); + +return rc; +} +/** * cxl_internal_send_cmd() - Kernel internal interface to send a mailbox command * @mds: The driver data for the operation
@@ -249,7 +506,7 @@ int cxl_internal_send_cmd(struct cxl_memdev_state *mds, out_size = mbox_cmd->size_out; min_out = mbox_cmd->min_out; - rc = mds->mbox_send (mds, mbox_cmd); + rc =cxl_pci_mbox_send (mds, mbox_cmd);/* * EIO is reserved for a payload size mismatch and mbox_send() * may not return this error.
@@ -588,7 +845,7 @@ static int handle_mailbox_cmd_from_user(struct cxl_memdev_state *mds,cxl_mem_opcode_to_name (mbox_cmd->opcode), mbox_cmd->opcode, mbox_cmd->size_in); - rc = mds->mbox_send (mds, mbox_cmd); + rc =cxl_pci_mbox_send (mds, mbox_cmd);if (rc) goto out;
drivers/cxl/cxlmbox.h new file +146 -0
146 lines not on this page · raw message
drivers/cxl/cxlmem.h +12 -136
172 lines not on this page · raw message
drivers/cxl/pci.c +1 -264
301 lines not on this page · raw message
Signature · 1 line
2.39.2
Re: [PATCH 1/4] cxl: mbox: Preparatory move of functions to core/mbox.c and cxlmbox.h
Quoted 5 lines · Jonathan Cameron
A later patch will modify this code to separate out the mbox functionality from the memdev. This patch is intended to make that a little more readable. Related move of
cxl_err(),cxl_cmd_err()to make them accessible from other parts of the cxl core code.
Quoted 8 lines
Reviewed-by Davidlohr Bueso <[email protected]>Signed-off-by Jonathan Cameron <[email protected]>--- drivers/cxl/core/mbox.c | 261 ++++++++++++++++++++++++++++++++++++++- drivers/cxl/cxlmbox.h | 146 ++++++++++++++++++++++ drivers/cxl/cxlmem.h | 148 ++-------------------- drivers/cxl/pci.c | 265 +--------------------------------------- 4 files changed, 418 insertions(+), 402 deletions(-)
Given that the CXL mailbox format is being adopted by other standards efforts in PCIe and OCP I would expect that this functionality is better served moving out of cxl_core.ko into its own compilation unit.
Something like drivers/cxl/cci/, that CXL can select. The s/mbox/cci/ rename is a proposal to both drop a letter out of the acronym and perhaps make the code a bit more discoverable / palatable for folks coming from those non-CXL specs to find the Linux code. However, if folks think that's too much thrash I am ok with drivers/cxl/mbox/.
As for the policy for raw commands. I would still like for there to be some discipline of registratants to this facility to classify production vs debug commands to give Linux distributors a single place to set policy. I.e. keep up the need for consumers of this to define "Linux" commands to avoid the "raw" warning.
That said, how much of what switch CCI wants to do should be an ioctl() ABI vs sysfs / configfs? The raw commands are really only there for prototyping until production flows are established.
Re: [PATCH 1/4] cxl: mbox: Preparatory move of functions to core/mbox.c and cxlmbox.h
Quoted 3 lines
Quoted 3 lines
Given that the CXL mailbox format is being adopted by other standards efforts in PCIe and OCP I would expect that this functionality is better served moving out of cxl_core.ko into its own compilation unit.
Hi Dan, Jonathan,
Agree with the statement above, and in fact, I think that there should be a way applications can send CCI commands even from hosts without CXL root port.
Consider a CXL switch that is connected to two hosts, one with CXL root port, and other having a pure PCIe root port.
Ideally applications on either host should be able to communicate with/configure the switch in the same way from both the hosts (CXL capable or not).
If the CCI mailbox driver is completely dependent on the CXL root port, then it will not even load on a PCIe root port, even if we implement the 0x0c0b00 class code in a CXL switch.
In this respect, a CXL switch can implement the PCIe MMPT interface too, so that PCIe based drivers can access that interface to send mailbox commands.
The idea is, even if the CCI mailbox driver does not load in a non-CXL root port, an application can use the MMPT interface to manage the driver.
Please let me know if you think that will work?
One potential problem I see is that in CXL root port, both CCI and MMPT mailbox will be available, which might lead to conflict if two applications use the two interfaces at the same time. Ultimately I think having a root port independent mailbox driver (that works in both CXL and PCIe root port) would be helpful for switches that can connect to both CXL and PCIe root ports.
Would appreciate any insight on this.
sincerely,
Sumanesh
Quoted 36 lines · Dan Williams
Quoted 5 lines · Jonathan Cameron
A later patch will modify this code to separate out the mbox functionality from the memdev. This patch is intended to make that a little more readable. Related move of
cxl_err(),cxl_cmd_err()to make them accessible from other parts of the cxl core code.Quoted 8 lines
Reviewed-by Davidlohr Bueso <[email protected]>Signed-off-by Jonathan Cameron <[email protected]>--- drivers/cxl/core/mbox.c | 261 ++++++++++++++++++++++++++++++++++++++- drivers/cxl/cxlmbox.h | 146 ++++++++++++++++++++++ drivers/cxl/cxlmem.h | 148 ++-------------------- drivers/cxl/pci.c | 265 +--------------------------------------- 4 files changed, 418 insertions(+), 402 deletions(-)Given that the CXL mailbox format is being adopted by other standards efforts in PCIe and OCP I would expect that this functionality is better served moving out of cxl_core.ko into its own compilation unit.
Something like
drivers/cxl/cci/, that CXL can select. The s/mbox/cci/ rename is a proposal to both drop a letter out of the acronym and perhaps make the code a bit more discoverable / palatable for folks coming from those non-CXL specs to find the Linux code. However, if folks think that's too much thrash I am ok withdrivers/cxl/mbox/.As for the policy for raw commands. I would still like for there to be some discipline of registratants to this facility to classify production vs debug commands to give Linux distributors a single place to set policy. I.e. keep up the need for consumers of this to define "Linux" commands to avoid the "raw" warning.
That said, how much of what switch CCI wants to do should be an
ioctl()ABI vs sysfs / configfs? The raw commands are really only there for prototyping until production flows are established.
Signature · 11 lines
This electronic communication and the information and any files transmitted with it, or attached to it, are confidential and are intended solely for the use of the individual or entity to whom it is addressed and may contain information that is confidential, legally privileged, protected by privacy laws, or otherwise restricted from disclosure to anyone else. If you are not the intended recipient or the person responsible for delivering the e-mail to the intended recipient, you are hereby notified that any use, copying, distributing, dissemination, forwarding, printing, or copying of this e-mail is strictly prohibited. If you received this e-mail in error, please return the e-mail to the sender, delete it from your computer, and destroy any printed copy of it.
Re: [PATCH 1/4] cxl: mbox: Preparatory move of functions to core/mbox.c and cxlmbox.h
Quoted 3 lines · On Mon, 13 Nov 2023 12:39:15 -0700 Sumanesh Samanta…
Quoted 3 lines
Quoted 3 lines
Given that the CXL mailbox format is being adopted by other standards efforts in PCIe and OCP I would expect that this functionality is better served moving out of cxl_core.ko into its own compilation unit.
Makes sense to break it out to a separate module. I was kind of assuming that would happen later and the fact it isn't is more about history of this patch set than anything else (predates MMPT surfacing)
Quoted 11 lines
Hi Dan, Jonathan,
Agree with the statement above, and in fact, I think that there should be a way applications can send CCI commands even from hosts without CXL root port.
Consider a CXL switch that is connected to two hosts, one with CXL root port, and other having a pure PCIe root port.
Ideally applications on either host should be able to communicate with/configure the switch in the same way from both the hosts (CXL capable or not).
Absolutely agree, but I think this should just work with the current code. It's using stuff from the library module, not stuff that is loaded just when the ACPI tables say it's a CXL system. We can reduce what is pulled in by doing what Dan suggests but that's a software modularity question rather than anything about the hardware supported.
Quoted 12 lines
If the CCI mailbox driver is completely dependent on the CXL root port, then it will not even load on a PCIe root port, even if we implement the
0x0c0b00class code in a CXL switch.
In this respect, a CXL switch can implement the PCIe MMPT interface too, so that PCIe based drivers can access that interface to send mailbox commands.
The idea is, even if the CCI mailbox driver does not load in a non-CXL root port, an application can use the MMPT interface to manage the driver.
Please let me know if you think that will work?
Quoted 6 lines
One potential problem I see is that in CXL root port, both CCI and MMPT mailbox will be available, which might lead to conflict if two applications use the two interfaces at the same time. Ultimately I think having a root port independent mailbox driver (that works in both CXL and PCIe root port) would be helpful for switches that can connect to both CXL and PCIe root ports.
'Watch this space' as interaction of MMPT and CXL is not something the CXL spec speaks about and there are some corners that need to be resolved. Take questions about this to appropriate standards orgs as we can't resolve this here.
However, the switch CCI is not associated with the root port it's associated with a PCIe function that can be on a PCIe bus. Whether we end up with PCIe software support MMPT without needing to be part of a driver binding to the class code (which inherently here means it understands the CXL mailbox) is an interesting question. I find it unlikely that Linux will support such 'bare' MMPT instances but maybe...
Jonathan
Quoted 47 lines
Would appreciate any insight on this.
sincerely,
SumaneshQuoted 36 lines · Dan Williams
Quoted 5 lines · Jonathan Cameron
A later patch will modify this code to separate out the mbox functionality from the memdev. This patch is intended to make that a little more readable. Related move of
cxl_err(),cxl_cmd_err()to make them accessible from other parts of the cxl core code.Quoted 8 lines
Reviewed-by Davidlohr Bueso <[email protected]>Signed-off-by Jonathan Cameron <[email protected]>--- drivers/cxl/core/mbox.c | 261 ++++++++++++++++++++++++++++++++++++++- drivers/cxl/cxlmbox.h | 146 ++++++++++++++++++++++ drivers/cxl/cxlmem.h | 148 ++-------------------- drivers/cxl/pci.c | 265 +--------------------------------------- 4 files changed, 418 insertions(+), 402 deletions(-)Given that the CXL mailbox format is being adopted by other standards efforts in PCIe and OCP I would expect that this functionality is better served moving out of cxl_core.ko into its own compilation unit.
Something like
drivers/cxl/cci/, that CXL can select. The s/mbox/cci/ rename is a proposal to both drop a letter out of the acronym and perhaps make the code a bit more discoverable / palatable for folks coming from those non-CXL specs to find the Linux code. However, if folks think that's too much thrash I am ok withdrivers/cxl/mbox/.As for the policy for raw commands. I would still like for there to be some discipline of registratants to this facility to classify production vs debug commands to give Linux distributors a single place to set policy. I.e. keep up the need for consumers of this to define "Linux" commands to avoid the "raw" warning.
That said, how much of what switch CCI wants to do should be an
ioctl()ABI vs sysfs / configfs? The raw commands are really only there for prototyping until production flows are established.
Re: [PATCH 1/4] cxl: mbox: Preparatory move of functions to core/mbox.c and cxlmbox.h
In reply to Dan WilliamsQuoted 15 lines · On Sun, 12 Nov 2023 16:09:35 -0800 Dan Williams <[email protected]>…
Quoted 5 lines · Jonathan Cameron
A later patch will modify this code to separate out the mbox functionality from the memdev. This patch is intended to make that a little more readable. Related move of
cxl_err(),cxl_cmd_err()to make them accessible from other parts of the cxl core code.Quoted 8 lines
Reviewed-by Davidlohr Bueso <[email protected]>Signed-off-by Jonathan Cameron <[email protected]>--- drivers/cxl/core/mbox.c | 261 ++++++++++++++++++++++++++++++++++++++- drivers/cxl/cxlmbox.h | 146 ++++++++++++++++++++++ drivers/cxl/cxlmem.h | 148 ++-------------------- drivers/cxl/pci.c | 265 +--------------------------------------- 4 files changed, 418 insertions(+), 402 deletions(-)
Quoted 3 lines
Given that the CXL mailbox format is being adopted by other standards efforts in PCIe and OCP I would expect that this functionality is better served moving out of cxl_core.ko into its own compilation unit.
Makes sense.
Quoted 6 lines
Something like
drivers/cxl/cci/, that CXL can select. The s/mbox/cci/ rename is a proposal to both drop a letter out of the acronym and perhaps make the code a bit more discoverable / palatable for folks coming from those non-CXL specs to find the Linux code. However, if folks think that's too much thrash I am ok withdrivers/cxl/mbox/.
Medium term I might see how bad it is to just rip it out of CXL entirely and push it to drivers/pci/mmpt.c (with a few hooks to deal with CXL parts). However, right now I think that's a step 2 (as MMPT whilst published isn't widely implemented yet)
Quoted 10 lines
As for the policy for raw commands. I would still like for there to be some discipline of registratants to this facility to classify production vs debug commands to give Linux distributors a single place to set policy. I.e. keep up the need for consumers of this to define "Linux" commands to avoid the "raw" warning.
That said, how much of what switch CCI wants to do should be an
ioctl()ABI vs sysfs / configfs? The raw commands are really only there for prototyping until production flows are established.
I'd love us to be in the world where the kernel modeled the topology being controlled nicely even though it would get complex. There is also a clear argument that this complexity isn't of interest to the kernel so it doesn't really belong there anyway.
The main issue here is that the 'other' interface I'd expect a userspace tool to be poking is MCTP. There the software model is a network port. So all the smarts pretty much have to be in userspace.
If the assumption is that it's all in userspace, the kernel won't have enough visibility to do high quality sanity checking + pretty much everything you set with the FMAPI is destructive, most likely to 'other hosts'.
We could maybe add commands (so no taint) for the non destructive stuff like querying topology, and maybe even DCD add / remove (not forced) with the tunneling wrap up pushed down into the kernel. I'll admit I'm doubtful and think it likely any software stack is going to use raw anyway because they have it all implemented for the MCTP path.
I expect we'll get requests to drop the taint on the Switch Mailbox CCI raw command and it's much harder to argue against that it is for type 3 devices.
Jonathan
PATCH 2/4 cxl: mbox: Factor out the mbox specific data for reuse in switch cci
In reply to Jonathan CameronThe mbox implementation should be reusuable on devices that are not CXL type 3 memory devices. The implementation has a number of direct calls that assume it is such a device. Move the data to a separate structure under struct cxl_memdev_state and add callbacks to deal with the non generic corners.
--- drivers/cxl/core/core.h | 3 +- drivers/cxl/core/mbox.c | 315 +++++++++++++++++++------------------- drivers/cxl/core/memdev.c | 32 ++-- drivers/cxl/core/regs.c | 33 +++- drivers/cxl/cxl.h | 4 +- drivers/cxl/cxlmbox.h | 60 +++++++- drivers/cxl/cxlmem.h | 28 ++-- drivers/cxl/pci.c | 206 +++++++++++++++++-------- drivers/cxl/pmem.c | 6 +- drivers/cxl/security.c | 13 +- 10 files changed, 431 insertions(+), 269 deletions(-)
drivers/cxl/core/core.h +2 -1
@@ -51,9 +51,10 @@ static inline void cxl_region_exit(void)struct cxl_send_command ;struct cxl_mem_query_commands ; +struct cxl_mbox ;int cxl_query_cmd (struct cxl_memdev *cxlmd,struct cxl_mem_query_commands __user *q); -int cxl_send_cmd (struct cxl_memdev *cxlmd,struct cxl_send_command __user *s); +int cxl_send_cmd (struct cxl_mbox *mbox,struct cxl_send_command __user *s);void __iomem *devm_cxl_iomap_block (struct device *dev,resource_size_t addr,resource_size_t length);
drivers/cxl/core/mbox.c +157 -158
833 lines not on this page · raw message
drivers/cxl/core/memdev.c +18 -14
@@ -58,7 +58,7 @@ static ssize_t payload_max_show(struct device *dev,if (!mds)return sysfs_emit (buf,"\n" ); -return sysfs_emit (buf,"%zu\n" , mds->payload_size); +return sysfs_emit (buf,"%zu\n" , mds->mbox.payload_size); }static DEVICE_ATTR_RO (payload_max);
@@ -125,7 +125,8 @@ static ssize_t security_state_show(struct device *dev,struct cxl_memdev *cxlmd =to_cxl_memdev (dev);struct cxl_dev_state *cxlds = cxlmd->cxlds;struct cxl_memdev_state *mds =to_cxl_memdev_state (cxlds); -u64 reg =readq (cxlds->regs.mbox +CXLDEV_MBOX_BG_CMD_STATUS_OFFSET ); +//accessor? +u64 reg =readq (mds->mbox.mbox +CXLDEV_MBOX_BG_CMD_STATUS_OFFSET );u32 pct =FIELD_GET (CXLDEV_MBOX_BG_CMD_COMMAND_PCT_MASK , reg);u16 cmd =FIELD_GET (CXLDEV_MBOX_BG_CMD_COMMAND_OPCODE_MASK , reg);unsigned long state = mds->security.state;
@@ -349,7 +350,7 @@ int cxl_inject_poison(struct cxl_memdev *cxlmd, u64 dpa) .size_in = sizeof(inject), .payload_in = &inject, }; - rc =cxl_internal_send_cmd (mds, &mbox_cmd); + rc =cxl_internal_send_cmd (&mds->mbox, &mbox_cmd);if (rc) goto out;
@@ -406,7 +407,7 @@ int cxl_clear_poison(struct cxl_memdev *cxlmd, u64 dpa) .payload_in = &clear, }; - rc =cxl_internal_send_cmd (mds, &mbox_cmd); + rc =cxl_internal_send_cmd (&mds->mbox, &mbox_cmd);if (rc) goto out;
@@ -535,7 +536,7 @@ void set_exclusive_cxl_commands(struct cxl_memdev_state *mds, unsigned long *cmds) {down_write (&cxl_memdev_rwsem); -bitmap_or (mds->exclusive_cmds, mds->exclusive_cmds, cmds, +bitmap_or (mds->mbox.exclusive_cmds, mds->mbox.exclusive_cmds, cmds,CXL_MEM_COMMAND_ID_MAX );up_write (&cxl_memdev_rwsem); }
@@ -550,7 +551,7 @@ void clear_exclusive_cxl_commands(struct cxl_memdev_state *mds, unsigned long *cmds) {down_write (&cxl_memdev_rwsem); -bitmap_andnot (mds->exclusive_cmds, mds->exclusive_cmds, cmds, +bitmap_andnot (mds->mbox.exclusive_cmds, mds->mbox.exclusive_cmds, cmds,CXL_MEM_COMMAND_ID_MAX );up_write (&cxl_memdev_rwsem); }
@@ -636,11 +637,14 @@ static struct cxl_memdev *cxl_memdev_alloc(struct cxl_dev_state *cxlds,static long __cxl_memdev_ioctl (struct cxl_memdev *cxlmd,unsigned int cmd,unsigned long arg) { +struct cxl_dev_state *cxlds = cxlmd->cxlds; + struct cxl_memdev_state *mds = container_of(cxlds, struct cxl_memdev_state, cxlds); +switch (cmd) {case CXL_MEM_QUERY_COMMANDS :return cxl_query_cmd (cxlmd, (void __user *)arg);case CXL_MEM_SEND_COMMAND : -return cxl_send_cmd (cxlmd, (void __user *)arg); +return cxl_send_cmd (&mds->mbox, (void __user *)arg);default :return -ENOTTY ; }
@@ -705,7 +709,7 @@ static int cxl_mem_get_fw_info(struct cxl_memdev_state *mds) .payload_out = &info, }; - rc =cxl_internal_send_cmd (mds, &mbox_cmd); + rc =cxl_internal_send_cmd (&mds->mbox, &mbox_cmd);if (rc <0 )return rc;
@@ -745,7 +749,7 @@ static int cxl_mem_activate_fw(struct cxl_memdev_state *mds, int slot) activate.action =CXL_FW_ACTIVATE_OFFLINE ; activate.slot = slot; -return cxl_internal_send_cmd (mds, &mbox_cmd); +return cxl_internal_send_cmd (&mds->mbox, &mbox_cmd); }/**
@@ -779,7 +783,7 @@ static int cxl_mem_abort_fw_xfer(struct cxl_memdev_state *mds) transfer->action =CXL_FW_TRANSFER_ACTION_ABORT ; - rc =cxl_internal_send_cmd (mds, &mbox_cmd); + rc =cxl_internal_send_cmd (&mds->mbox, &mbox_cmd);kfree (transfer);return rc; }
@@ -815,7 +819,7 @@ static enum fw_upload_err cxl_fw_prepare(struct fw_upload *fwl, const u8 *data,return FW_UPLOAD_ERR_INVALID_SIZE ; mds->fw.oneshot =struct_size (transfer, data, size) < - mds->payload_size; + mds->mbox.payload_size;if (cxl_mem_get_fw_info (mds))return FW_UPLOAD_ERR_HW_ERROR ;
@@ -858,7 +862,7 @@ static enum fw_upload_err cxl_fw_write(struct fw_upload *fwl, const u8 *data,* sizeof(*transfer) is 128. These constraints imply that @cur_size * will always be 128b aligned. */ - cur_size =min_t (size_t, size, mds->payload_size -sizeof (*transfer)); + cur_size =min_t (size_t, size, mds->mbox.payload_size -sizeof (*transfer)); remaining = size - cur_size; size_in =struct_size (transfer, data, cur_size);
@@ -902,7 +906,7 @@ static enum fw_upload_err cxl_fw_write(struct fw_upload *fwl, const u8 *data, .poll_count = 30, }; - rc =cxl_internal_send_cmd (mds, &mbox_cmd); + rc =cxl_internal_send_cmd (&mds->mbox, &mbox_cmd);if (rc <0 ) { rc =FW_UPLOAD_ERR_RW_ERROR ; goto out_free;
@@ -973,7 +977,7 @@ int cxl_memdev_setup_fw_upload(struct cxl_memdev_state *mds)struct fw_upload *fwl;int rc; -if (!test_bit (CXL_MEM_COMMAND_ID_GET_FW_INFO , mds->enabled_cmds)) +if (!test_bit (CXL_MEM_COMMAND_ID_GET_FW_INFO , mds->mbox.enabled_cmds))return 0 ; fwl = firmware_upload_register(THIS_MODULE, dev, dev_name(dev),
drivers/cxl/core/regs.c +32 -1
@@ -244,7 +244,6 @@ int cxl_map_device_regs(const struct cxl_register_map *map,void __iomem **addr; } mapinfo[] = { { &map->device_map.status, ®s->status, }, - { &map->device_map.mbox, ®s->mbox, }, { &map->device_map.memdev, ®s->memdev, }, }; int i;
@@ -268,6 +267,38 @@ int cxl_map_device_regs(const struct cxl_register_map *map, }EXPORT_SYMBOL_NS_GPL (cxl_map_device_regs,CXL ); +int cxl_map_mbox_regs (const struct cxl_register_map *map, +void __iomem **mbox_regs) +{ +struct device *dev = map->dev; +resource_size_t phys_addr = map->resource; +struct mapinfo { +const struct cxl_reg_map *rmap; +void __iomem **addr; + } mapinfo[] = { + { &map->device_map.mbox, mbox_regs, }, + }; +int i; + +for (i =0 ; i <ARRAY_SIZE (mapinfo); i++) { +struct mapinfo *mi = &mapinfo[i]; +resource_size_t length; +resource_size_t addr; + +if (!mi->rmap || !mi->rmap->valid) +continue ; + + addr = phys_addr + mi->rmap->offset; + length = mi->rmap->size; + *(mi->addr) =devm_cxl_iomap_block (dev, addr, length); +if (!*(mi->addr)) +return -ENOMEM ; + } + +return 0 ; +} +EXPORT_SYMBOL_NS_GPL (cxl_map_mbox_regs,CXL ); +static bool cxl_decode_regblock (struct pci_dev *pdev,u32 reg_lo,u32 reg_hi,struct cxl_register_map *map) {
drivers/cxl/cxl.h +3 -1
@@ -215,7 +215,7 @@ struct cxl_regs {* @memdev: CXL 2.0 8.2.8.5 Memory Device Registers */ struct_group_tagged (cxl_device_regs, device_regs, - void __iomem *status, *mbox, *memdev; + void __iomem *status, *memdev; ); struct_group_tagged(cxl_pmu_regs, pmu_regs,
@@ -278,6 +278,8 @@ int cxl_map_component_regs(const struct cxl_register_map *map, unsigned long map_mask);int cxl_map_device_regs (const struct cxl_register_map *map,struct cxl_device_regs *regs); +int cxl_map_mbox_regs (const struct cxl_register_map *map, +void __iomem **mbox_reg);int cxl_map_pmu_regs (struct pci_dev *pdev,struct cxl_pmu_regs *regs,struct cxl_register_map *map);
drivers/cxl/cxlmbox.h +57 -3
70 lines not on this page · raw message
drivers/cxl/cxlmem.h +11 -17
@@ -347,14 +347,10 @@ struct cxl_dev_state {* the functionality related to that like Identify Memory Device and Get * Partition Info * @cxlds: Core driver state common across Type-2 and Type-3 devices -* @payload_size: Size of space for payload -* (CXL 2.0 8.2.8.4.3 Mailbox Capabilities Register) +* @mbox: Mailbox instance. * @lsa_size: Size of Label Storage Area * (CXL 2.0 8.2.9.5.1.1 Identify Memory Device) -* @mbox_mutex: Mutex to synchronize mailbox access. * @firmware_version: Firmware version for the memory device. -* @enabled_cmds: Hardware commands found enabled in CEL. -* @exclusive_cmds: Commands that are kernel-internal only * @total_bytes: sum of all possible capacities * @volatile_only_bytes: hard volatile capacity * @persistent_only_bytes: hard persistent capacity
@@ -367,19 +363,16 @@ struct cxl_dev_state {* @poison: poison driver state info * @security: security driver state info * @fw: firmware upload / activation state -* @mbox_send: @dev specific transport for transmitting mailbox commands * * See CXL 3.0 8.2.9.8.2 Capacity Configuration and Label Storage for * details on capacity parameters. */ struct cxl_memdev_state {struct cxl_dev_state cxlds; -size_t payload_size; +struct cxl_mbox mbox; +size_t lsa_size; -struct mutex mbox_mutex;/* Protects device mailbox and firmware */ char firmware_version[0x10 ]; - DECLARE_BITMAP(enabled_cmds, CXL_MEM_COMMAND_ID_MAX); - DECLARE_BITMAP(exclusive_cmds, CXL_MEM_COMMAND_ID_MAX);u64 total_bytes;u64 volatile_only_bytes;u64 persistent_only_bytes;
@@ -392,10 +385,6 @@ struct cxl_memdev_state {struct cxl_poison_state poison;struct cxl_security_state security;struct cxl_fw_state fw; - -struct rcuwait mbox_wait; -int (*mbox_send)(struct cxl_memdev_state *mds, -struct cxl_mbox_cmd *cmd); }; static inline struct cxl_memdev_state *
@@ -727,11 +716,16 @@ enum { CXL_PMEM_SEC_PASS_USER, }; -int cxl_internal_send_cmd (struct cxl_memdev_state *mds, -struct cxl_mbox_cmd *cmd); +bool cxl_is_poison_command (u16 opcode); +void cxl_set_poison_cmd_enabled (struct cxl_poison_state *poison, +u16 opcode); + +bool cxl_is_security_command (u16 opcode); +void cxl_set_security_cmd_enabled (struct cxl_security_state *security, +u16 opcode);int cxl_dev_state_identify (struct cxl_memdev_state *mds);int cxl_await_media_ready (struct cxl_dev_state *cxlds); -int cxl_enumerate_cmds (struct cxl_memdev_state *mds); +int cxl_mem_create_range_info (struct cxl_memdev_state *mds);struct cxl_memdev_state *cxl_memdev_state_create (struct device *dev); void set_exclusive_cxl_commands(struct cxl_memdev_state *mds,
drivers/cxl/pci.c +141 -65
384 lines not on this page · raw message
drivers/cxl/pmem.c +3 -3
@@ -110,7 +110,7 @@ static int cxl_pmem_get_config_size(struct cxl_memdev_state *mds, *cmd = (struct nd_cmd_get_config_size ){ .config_size = mds->lsa_size, .max_xfer = - mds->payload_size -sizeof (struct cxl_mbox_set_lsa ), + mds->mbox.payload_size -sizeof (struct cxl_mbox_set_lsa ), };return 0 ;
@@ -141,7 +141,7 @@ static int cxl_pmem_get_config_data(struct cxl_memdev_state *mds, .payload_out = cmd->out_buf, }; - rc =cxl_internal_send_cmd (mds, &mbox_cmd); + rc =cxl_internal_send_cmd (&mds->mbox, &mbox_cmd); cmd->status =0 ;return rc;
@@ -177,7 +177,7 @@ static int cxl_pmem_set_config_data(struct cxl_memdev_state *mds, .size_in = struct_size(set_lsa, data, cmd->in_length), }; - rc =cxl_internal_send_cmd (mds, &mbox_cmd); + rc =cxl_internal_send_cmd (&mds->mbox, &mbox_cmd);/* * Set "firmware" status (4-packed bytes at the end of the input
drivers/cxl/security.c +7 -6
@@ -6,6 +6,7 @@#include <linux/async.h> #include <linux/slab.h> #include <linux/memregion.h> +#include "cxlmbox.h" #include "cxlmem.h" #include "cxl.h"
@@ -29,7 +30,7 @@ static unsigned long cxl_pmem_get_security_flags(struct nvdimm *nvdimm, .payload_out = &out, }; - rc =cxl_internal_send_cmd (mds, &mbox_cmd); + rc =cxl_internal_send_cmd (&mds->mbox, &mbox_cmd);if (rc <0 )return 0 ;
@@ -87,7 +88,7 @@ static int cxl_pmem_security_change_key(struct nvdimm *nvdimm, .payload_in = &set_pass, }; -return cxl_internal_send_cmd (mds, &mbox_cmd); +return cxl_internal_send_cmd (&mds->mbox, &mbox_cmd); } static int __cxl_pmem_security_disable(struct nvdimm *nvdimm,
@@ -112,7 +113,7 @@ static int __cxl_pmem_security_disable(struct nvdimm *nvdimm, .payload_in = &dis_pass, }; -return cxl_internal_send_cmd (mds, &mbox_cmd); +return cxl_internal_send_cmd (&mds->mbox, &mbox_cmd); } static int cxl_pmem_security_disable(struct nvdimm *nvdimm,
@@ -136,7 +137,7 @@ static int cxl_pmem_security_freeze(struct nvdimm *nvdimm) .opcode = CXL_MBOX_OP_FREEZE_SECURITY, }; -return cxl_internal_send_cmd (mds, &mbox_cmd); +return cxl_internal_send_cmd (&mds->mbox, &mbox_cmd); } static int cxl_pmem_security_unlock(struct nvdimm *nvdimm,
@@ -156,7 +157,7 @@ static int cxl_pmem_security_unlock(struct nvdimm *nvdimm, .payload_in = pass, }; - rc =cxl_internal_send_cmd (mds, &mbox_cmd); + rc =cxl_internal_send_cmd (&mds->mbox, &mbox_cmd);if (rc <0 )return rc;
@@ -185,7 +186,7 @@ static int cxl_pmem_security_passphrase_erase(struct nvdimm *nvdimm, .payload_in = &erase, }; - rc =cxl_internal_send_cmd (mds, &mbox_cmd); + rc =cxl_internal_send_cmd (&mds->mbox, &mbox_cmd);if (rc <0 )return rc;
Signature · 1 line
2.39.2
PATCH 3/4 PCI: Add PCI_CLASS_SERIAL_CXL_SWITCH_CCI class ID to pci_ids.h
In reply to Jonathan CameronThis ID is used for CXL Switch Mailbox CCIs. Incorporate the Programming Interface in the definition in common with similar entries.
See CXL rev3.0 81.1.3.1 PCI Header Class Code Register
--- include/linux/pci_ids.h | 1 + 1 file changed, 1 insertion(+)
include/linux/pci_ids.h +1 -0
@@ -128,6 +128,7 @@#define PCI_CLASS_SERIAL_IPMI_SMIC 0x0c0700#define PCI_CLASS_SERIAL_IPMI_KCS 0x0c0701#define PCI_CLASS_SERIAL_IPMI_BT 0x0c0702 +#define PCI_CLASS_SERIAL_CXL_SWITCH_CCI 0x0c0b00#define PCI_BASE_CLASS_WIRELESS 0x0d#define PCI_CLASS_WIRELESS_RF_CONTROLLER 0x0d10
Signature · 1 line
2.39.2
Re: [PATCH 3/4] PCI: Add PCI_CLASS_SERIAL_CXL_SWITCH_CCI class ID to pci_ids.h
Quoted 5 lines · Jonathan Cameron
This ID is used for CXL Switch Mailbox CCIs. Incorporate the Programming Interface in the definition in common with similar entries.
See CXL rev3.0 81.1.3.1 PCI Header Class Code Register
Also PCI Code and ID Assignment r1.15, sec 1.13, which I assume is definitive since this is a piece of the PCI namespace.
Quoted 1 line
Signed-off-by Jonathan Cameron <[email protected]>
Quoted 19 lines
--- include/linux/pci_ids.h | 1 + 1 file changed, 1 insertion(+)include/linux/pci_ids.h +1
-0@@ -128,6 +128,7 @@#define PCI_CLASS_SERIAL_IPMI_SMIC 0x0c0700#define PCI_CLASS_SERIAL_IPMI_KCS 0x0c0701#define PCI_CLASS_SERIAL_IPMI_BT 0x0c0702 +#define PCI_CLASS_SERIAL_CXL_SWITCH_CCI 0x0c0b00#define PCI_BASE_CLASS_WIRELESS 0x0d#define PCI_CLASS_WIRELESS_RF_CONTROLLER 0x0d10Signature · 1 line
2.39.2
PATCH 4/4 cxl/pci: Add support for stand alone CXL Switch mailbox CCI
In reply to Jonathan CameronCXL 3.0 defines a mailbox PCI function independent of any other CXL components. The intent is that instances of this mailbox will be found as additional PCI functions of upstream CXL switch ports.
---
Non RFC:
- Drop adding new commands. For now kick the security model questions into the long grass but using RAW comamnds only.
--- drivers/cxl/Kconfig | 14 +++ drivers/cxl/Makefile | 2 + drivers/cxl/core/Makefile | 1 + drivers/cxl/core/core.h | 9 ++ drivers/cxl/core/mbox.c | 1 + drivers/cxl/core/memdev.c | 12 +-- drivers/cxl/core/regs.c | 6 +- drivers/cxl/core/switchdev.c | 129 ++++++++++++++++++++++++++ drivers/cxl/cxl.h | 2 +- drivers/cxl/cxlmbox.h | 1 + drivers/cxl/pci.c | 7 +- drivers/cxl/switch.h | 19 ++++ drivers/cxl/switchdev.c | 169 +++++++++++++++++++++++++++++++++++ 13 files changed, 359 insertions(+), 13 deletions(-)
drivers/cxl/Kconfig +14 -0
@@ -153,5 +153,19 @@ config CXL_PMU Say 'y/m' to enable a driver that will attach to performance monitoring units and provide standard perf based interfaces. + If unsure say 'm'. + +config CXL_SWITCH + tristate "CXL switch mailbox access" + help + The CXL r3.0 specification defines a "CXL switch CCI" sub-class in the + PCI "Serial" base class of devices. Device's identified by + this class code provide a mailbox interface to allow control of CXL + switch configuration over inband PCI. + + Say 'y/m' to enable a driver that will attach to CXL Switch CCI + devices enumerated by the CXL switch CCI class code for configuration + and management primarily via the mailbox interface. + If unsure say 'm'. endif
drivers/cxl/Makefile +2 -0
@@ -5,9 +5,11 @@ obj-$(CONFIG_CXL_MEM) += cxl_mem.o obj-$(CONFIG_CXL_ACPI) += cxl_acpi.o obj-$(CONFIG_CXL_PMEM) += cxl_pmem.o obj-$(CONFIG_CXL_PORT) += cxl_port.o +obj-$(CONFIG_CXL_SWITCH) += cxl_switchdev.o cxl_mem-y := mem.o cxl_pci-y := pci.o cxl_acpi-y := acpi.o cxl_pmem-y := pmem.o security.o cxl_port-y := port.o +cxl_switchdev-y := switchdev.o
drivers/cxl/core/Makefile +1 -0
@@ -13,5 +13,6 @@ cxl_core-y += mbox.o cxl_core-y += pci.o cxl_core-y += hdm.o cxl_core-y += pmu.o +cxl_core-y += switchdev.o cxl_core-$(CONFIG_TRACING) += trace.o cxl_core-$(CONFIG_CXL_REGION) += region.o
drivers/cxl/core/core.h +9 -0
@@ -87,4 +87,13 @@ enum cxl_poison_trace_type { CXL_POISON_TRACE_CLEAR, }; +/* +* An entire PCI topology full of devices should be enough for any +* config +*/ +#define CXL_MEM_MAX_DEVS 65536 + +extern int cxl_mem_major; +extern struct ida cxl_memdev_ida; +extern struct rw_semaphore cxl_memdev_rwsem;#endif /* __CXL_CORE_H__ */
drivers/cxl/core/mbox.c +1 -0
@@ -9,6 +9,7 @@#include <cxlmbox.h> #include <cxlpci.h> #include <cxlmem.h> +#include <cxlpci.h> #include <cxl.h> #include "core.h"
drivers/cxl/core/memdev.c +3 -9
@@ -11,16 +11,10 @@#include "trace.h" #include "core.h" -static DECLARE_RWSEM (cxl_memdev_rwsem); +DECLARE_RWSEM (cxl_memdev_rwsem); -/* -* An entire PCI topology full of devices should be enough for any -* config -*/ -#define CXL_MEM_MAX_DEVS 65536 - -static int cxl_mem_major; -static DEFINE_IDA (cxl_memdev_ida); +int cxl_mem_major; +DEFINE_IDA (cxl_memdev_ida);static void cxl_memdev_release (struct device *dev) {
drivers/cxl/core/regs.c +4 -2
@@ -268,7 +268,7 @@ int cxl_map_device_regs(const struct cxl_register_map *map,EXPORT_SYMBOL_NS_GPL (cxl_map_device_regs,CXL );int cxl_map_mbox_regs (const struct cxl_register_map *map, -void __iomem **mbox_regs) +void __iomem **mbox_regs,void __iomem **status_regs) {struct device *dev = map->dev;resource_size_t phys_addr = map->resource;
@@ -277,10 +277,12 @@ int cxl_map_mbox_regs(const struct cxl_register_map *map,void __iomem **addr; } mapinfo[] = { { &map->device_map.mbox, mbox_regs, }, + { &map->device_map.status, status_regs, }, }; + int limit = status_regs ? ARRAY_SIZE(mapinfo) : 1; int i; - for (i = 0; i < ARRAY_SIZE(mapinfo); i++) { + for (i = 0; i < limit; i++) { struct mapinfo *mi = &mapinfo[i]; resource_size_t length; resource_size_t addr;
drivers/cxl/core/switchdev.c new file +129 -0
129 lines not on this page · raw message
drivers/cxl/cxl.h +1 -1
@@ -279,7 +279,7 @@ int cxl_map_component_regs(const struct cxl_register_map *map,int cxl_map_device_regs (const struct cxl_register_map *map,struct cxl_device_regs *regs);int cxl_map_mbox_regs (const struct cxl_register_map *map, -void __iomem **mbox_reg); +void __iomem **mbox_reg,void __iomem **status_reg);int cxl_map_pmu_regs (struct pci_dev *pdev,struct cxl_pmu_regs *regs,struct cxl_register_map *map);
drivers/cxl/cxlmbox.h +1 -0
@@ -155,6 +155,7 @@ static inline int cxl_mbox_cmd_rc2errno(struct cxl_mbox_cmd *mbox_cmd)enum cxl_opcode {CXL_MBOX_OP_INVALID =0x0000 , +CXL_MBOX_OP_GET_BG_CMD_STATUS =0x0002 ,CXL_MBOX_OP_RAW =CXL_MBOX_OP_INVALID ,CXL_MBOX_OP_GET_EVENT_RECORD =0x0100 ,CXL_MBOX_OP_CLEAR_EVENT_RECORD =0x0101 ,
drivers/cxl/pci.c +6 -1
@@ -632,9 +632,14 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)if (rc)return rc; - rc =cxl_map_mbox_regs (&map, &mds->mbox.mbox); +/* Status register already mapped */ + rc =cxl_map_mbox_regs (&map, &mds->mbox.mbox,NULL );if (rc)return rc; + +/* Connect up the status register access for the mbox */ + mds->mbox.status = cxlds->regs.status; +/* * If the component registers can't be found, the cxl_pci driver may * still be useful for management functions so don't return an error.
drivers/cxl/switch.h new file +19 -0
@@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __CXL_SWITCH_H__ +#define __CXL_SWITCH_H__ + +#include <linux/device.h> +#include <linux/cdev.h> +#include "cxlmbox.h" + +struct cxl_swdev { +struct device dev; +struct cdev cdev; +struct cxl_mbox mbox; +int id; +bool dying; +}; + +struct cxl_swdev *cxl_swdev_alloc (struct device *parent); +void cxl_swdev_shutdown (struct cxl_swdev *cxlswd); +#endif /* __CXL_SWITCH_H__ */
drivers/cxl/switchdev.c new file +169 -0
169 lines not on this page · raw message
Signature · 1 line
2.39.2
Re: [PATCH 4/4] cxl/pci: Add support for stand alone CXL Switch mailbox CCI
Quoted 38 lines · Jonathan Cameron
CXL 3.0 defines a mailbox PCI function independent of any other CXL components. The intent is that instances of this mailbox will be found as additional PCI functions of upstream CXL switch ports.
Signed-off-by Jonathan Cameron <[email protected]>---
Non RFC:
- Drop adding new commands. For now kick the security model questions into the long grass but using RAW comamnds only.--- drivers/cxl/Kconfig | 14 +++ drivers/cxl/Makefile | 2 + drivers/cxl/core/Makefile | 1 + drivers/cxl/core/core.h | 9 ++ drivers/cxl/core/mbox.c | 1 + drivers/cxl/core/memdev.c | 12 +-- drivers/cxl/core/regs.c | 6 +- drivers/cxl/core/switchdev.c | 129 ++++++++++++++++++++++++++ drivers/cxl/cxl.h | 2 +- drivers/cxl/cxlmbox.h | 1 + drivers/cxl/pci.c | 7 +- drivers/cxl/switch.h | 19 ++++ drivers/cxl/switchdev.c | 169 +++++++++++++++++++++++++++++++++++ 13 files changed, 359 insertions(+), 13 deletions(-)drivers/cxl/Kconfig +4
-0@@ -153,5 +153,19 @@ config CXL_PMU Say 'y/m' to enable a driver that will attach to performance monitoring units and provide standard perf based interfaces. + If unsure say 'm'. + +config CXL_SWITCH + tristate "CXL switch mailbox access"
drivers/cxl/Kconfig config CXL_PMU · line 169
Quoted 10 lines
drivers/cxl/Kconfig +10
-0+ help + The CXL r3.0 specification defines a "CXL switch CCI" sub-class in the + PCI "Serial" base class of devices. Device's identified by + this class code provide a mailbox interface to allow control of CXL + switch configuration over inband PCI. + + Say 'y/m' to enable a driver that will attach to CXL Switch CCI + devices enumerated by the CXL switch CCI class code for configuration + and management primarily via the mailbox interface. +
Is there anything in cxl_core.ko that the switch driver needs?
drivers/cxl/core/switchdev.c
Quoted 115 lines
drivers/cxl/Kconfig +0
-0If unsure say 'm'. endifdrivers/cxl/Makefile +2
-0@@ -5,9 +5,11 @@ obj-$(CONFIG_CXL_MEM) += cxl_mem.o obj-$(CONFIG_CXL_ACPI) += cxl_acpi.o obj-$(CONFIG_CXL_PMEM) += cxl_pmem.o obj-$(CONFIG_CXL_PORT) += cxl_port.o +obj-$(CONFIG_CXL_SWITCH) += cxl_switchdev.o cxl_mem-y := mem.o cxl_pci-y := pci.o cxl_acpi-y := acpi.o cxl_pmem-y := pmem.o security.o cxl_port-y := port.o +cxl_switchdev-y := switchdev.odrivers/cxl/core/Makefile +1
-0@@ -13,5 +13,6 @@ cxl_core-y += mbox.o cxl_core-y += pci.o cxl_core-y += hdm.o cxl_core-y += pmu.o +cxl_core-y += switchdev.o cxl_core-$(CONFIG_TRACING) += trace.o cxl_core-$(CONFIG_CXL_REGION) += region.odrivers/cxl/core/core.h +9
-0@@ -87,4 +87,13 @@ enum cxl_poison_trace_type { CXL_POISON_TRACE_CLEAR, }; +/* +* An entire PCI topology full of devices should be enough for any +* config +*/ +#define CXL_MEM_MAX_DEVS 65536 + +extern int cxl_mem_major; +extern struct ida cxl_memdev_ida; +extern struct rw_semaphore cxl_memdev_rwsem;#endif /* __CXL_CORE_H__ */ drivers/cxl/core/mbox.c +1
-0@@ -9,6 +9,7 @@#include <cxlmbox.h> #include <cxlpci.h> #include <cxlmem.h> +#include <cxlpci.h> #include <cxl.h> #include "core.h" drivers/cxl/core/memdev.c +3
-9@@ -11,16 +11,10 @@#include "trace.h" #include "core.h" -static DECLARE_RWSEM (cxl_memdev_rwsem); +DECLARE_RWSEM (cxl_memdev_rwsem); -/* -* An entire PCI topology full of devices should be enough for any -* config -*/ -#define CXL_MEM_MAX_DEVS 65536 - -static int cxl_mem_major; -static DEFINE_IDA (cxl_memdev_ida); +int cxl_mem_major; +DEFINE_IDA (cxl_memdev_ida);static void cxl_memdev_release (struct device *dev) {drivers/cxl/core/regs.c +4
-2@@ -268,7 +268,7 @@ int cxl_map_device_regs(const struct cxl_register_map *map,EXPORT_SYMBOL_NS_GPL (cxl_map_device_regs,CXL );int cxl_map_mbox_regs (const struct cxl_register_map *map, -void __iomem **mbox_regs) +void __iomem **mbox_regs,void __iomem **status_regs) {struct device *dev = map->dev;resource_size_t phys_addr = map->resource;@@ -277,10 +277,12 @@ int cxl_map_mbox_regs(const struct cxl_register_map *map,void __iomem **addr; } mapinfo[] = { { &map->device_map.mbox, mbox_regs, }, + { &map->device_map.status, status_regs, }, }; + int limit = status_regs ? ARRAY_SIZE(mapinfo) : 1; int i; - for (i = 0; i < ARRAY_SIZE(mapinfo); i++) { + for (i = 0; i < limit; i++) { struct mapinfo *mi = &mapinfo[i]; resource_size_t length; resource_size_t addr;drivers/cxl/core/switchdev.c new file +0
-0
All of this core code mostly looks like something that could be replaced by a miscdev. The medev ended up with not miscdev due to trying to combine the lifetime of the memdev in sysfs with the chardev, but if this is a chardev only interface then maybe it does not need to duplicate the cxl_memdev template?
Alternatively if the operations that this is going to perform have a formal sysfs ABI then maybe this does not need to bother with an chardev at all.
I guess I am missing why Linux upstream wants this. It enables wider testing, but to what end? What does the non-debug test bench version of this driver look like?
[..]
Quoted 92 lines
drivers/cxl/cxl.h +1
-1@@ -279,7 +279,7 @@ int cxl_map_component_regs(const struct cxl_register_map *map,int cxl_map_device_regs (const struct cxl_register_map *map,struct cxl_device_regs *regs);int cxl_map_mbox_regs (const struct cxl_register_map *map, -void __iomem **mbox_reg); +void __iomem **mbox_reg,void __iomem **status_reg);int cxl_map_pmu_regs (struct pci_dev *pdev,struct cxl_pmu_regs *regs,struct cxl_register_map *map);drivers/cxl/cxlmbox.h +1
-0@@ -155,6 +155,7 @@ static inline int cxl_mbox_cmd_rc2errno(struct cxl_mbox_cmd *mbox_cmd)enum cxl_opcode {CXL_MBOX_OP_INVALID =0x0000 , +CXL_MBOX_OP_GET_BG_CMD_STATUS =0x0002 ,CXL_MBOX_OP_RAW =CXL_MBOX_OP_INVALID ,CXL_MBOX_OP_GET_EVENT_RECORD =0x0100 ,CXL_MBOX_OP_CLEAR_EVENT_RECORD =0x0101 ,drivers/cxl/pci.c +6
-1@@ -632,9 +632,14 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)if (rc)return rc; - rc =cxl_map_mbox_regs (&map, &mds->mbox.mbox); +/* Status register already mapped */ + rc =cxl_map_mbox_regs (&map, &mds->mbox.mbox,NULL );if (rc)return rc; + +/* Connect up the status register access for the mbox */ + mds->mbox.status = cxlds->regs.status; +/* * If the component registers can't be found, the cxl_pci driver may * still be useful for management functions so don't return an error. drivers/cxl/switch.h new file +19
-0@@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __CXL_SWITCH_H__ +#define __CXL_SWITCH_H__ + +#include <linux/device.h> +#include <linux/cdev.h> +#include "cxlmbox.h" + +struct cxl_swdev { +struct device dev; +struct cdev cdev; +struct cxl_mbox mbox; +int id; +bool dying; +}; + +struct cxl_swdev *cxl_swdev_alloc (struct device *parent); +void cxl_swdev_shutdown (struct cxl_swdev *cxlswd); +#endif /* __CXL_SWITCH_H__ */ drivers/cxl/switchdev.c new file +16
-0@@ -0,0 +1,169 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright(c) Huawei Technologies +* Based on cxl/pci.c Copyright(c) 2020 Intel Corporation. All rights reserved. +*/ + +#include <linux/io-64-nonatomic-lo-hi.h> +#include <linux/moduleparam.h> +#include <linux/module.h> +#include <linux/delay.h> +#include <linux/sizes.h> +#include <linux/mutex.h> +#include <linux/list.h> +#include <linux/pci.h> +#include <linux/io.h> +#include "cxlpci.h"
drivers/cxl/switchdev.c line 26
Quoted 10 lines
drivers/cxl/switchdev.c new file +10
-0+#include "switch.h" +#include "cxl.h" + +static irqreturn_t cxl_swmb_mbox_irq (int irq,void *d) +{ +return cxl_mbox_irq (irq, d); +} + +static int cxl_swmb_setup_mailbox (struct cxl_mbox *mbox) +{
It feels like most of this function could be shared common setup code in the library.
[..]
Quoted 19 lines
drivers/cxl/switchdev.c new file +19
-0+} + + +static int cxl_swmb_probe (struct pci_dev *pdev,const struct pci_device_id *id) +{ +struct cxl_register_map map; +struct cxl_swdev *cxlswd; +int rc; + + rc =pcim_enable_device (pdev); +if (rc) +return rc; + + cxlswd =cxl_swdev_alloc (&pdev->dev); +if (IS_ERR (cxlswd)) +return PTR_ERR (cxlswd); + +mutex_init (&cxlswd->mbox.mbox_mutex); + rc =cxl_find_regblock (pdev,CXL_REGLOC_RBI_MEMDEV , &map);
drivers/cxl/switchdev.c line 118
Quoted 10 lines
drivers/cxl/switchdev.c new file +10
-0+if (rc) +return rc; + rc =cxl_setup_regs (&map); +if (rc) +return rc; + + rc =cxl_map_mbox_regs (&map, &cxlswd->mbox.mbox, &cxlswd->mbox.status); +if (rc) +return rc; +
I guess this points to the need to move CXL-style register enumeration out of cxl_core.ko as well because users of the CCI-library functionality care only about register enumeration and mailbox operation. Then cxl_core.ko is trimmed to just medev, port, and region concerns.
drivers/cxl/switchdev.c line 129
Quoted 11 lines
drivers/cxl/switchdev.c new file +11
-0+ cxlswd->mbox.dev = &pdev->dev; + + rc =cxl_swmb_setup_mailbox (&cxlswd->mbox); +if (rc) +return rc; + +pci_set_drvdata (pdev, cxlswd); + + rc =cxl_enumerate_cmds (&cxlswd->mbox); +if (rc) + goto error_put_device;
This circles back to the concern about CCI command enumeration might expand beyond the "CXL" usecase, so I think cci_enumerate_cmds() starts to feel more natrual as part of this refactor.
On Mon, 16 Oct 2023 13:53:19 +0100
Jonathan Cameron <[email protected]> wrote:
FYI: Userspace tooling and something resembling documentation / test instructions at:
https://gitlab.com/jic23/cxl-fmapi-test
The tool covers the switch CCI and MCTP used to access individual components, plus tunneling through them.
The code (in particularly the 'elegantly formatted output' and 'intuitive user interface' clearly indicates why I should never be let near any userspace tooling ;)
J
Quoted 83 lines
Note that the PCI SIG has recently release the MMPT specification which is similar to the CXL mailboxes. I would suggest that we don't get too distracted by that, but keep in mind that we may end up factoring some aspects of this code out into the PCI core and just use them here (at some future date!)
https://members.pcisig.com/wg/PCI-SIG/document/20109?uploaded=1
Based on v6.6-rc6
Chances since rfc4.
- Mostly a rebase because of fixes around sanitize handling.
- Dropped new command support. For, now RAW all the way.
- Aim is to support getting the QEMU emulation upstream.I'll be posting user space code to test this against the QEMU emulation shortly. I just need to finish writing the documentation as getting it up and running is less trivial than I'd like (lots of moving parts).
(I need this series on the list so I can point to it ;)Introduction:
CXL r3.0 introduced the option for a PCI function, intended to sit on an upstream port of a CXL switch. This function provides a mailbox interface similar to that seen on CXL type 3 devices. However, the command set is mostly different and intended for Fabric management. Note however that as we add support for multi headed devices (MHDs) a subset of commands will be available on selected MHD type 3 mailboxes. (tunnelling for DCD commands for example)
See: CXL rev 3.0
7.2.9 Switch Mailbox CCI
8.1.13 Switch Mailbox CCI Configuration Space Layout 8.2.8.6 Switch Mailbox CCI capabilityIt is probably relatively unusual that a typical host of CXL devices will have access to the one of these devices, in many cases they will be on a port connected to a BMC or similar. There are a few use cases where the host might be in charge of the configuration.
These are very convenient for testing in conjunction with the QEMU emulation. CXL switch and type 3 emulation is in QEMU is not complex enough to make these particular interesting but that should change soon.
For now don't provide any additional commands over those defined for the main CXL mailbox and in practice that means the RAW command path under
CONFIG_CXL_MEM_RAW_COMMANDSis used with the IOCTL to interact with this device and the fabric beyond it.The foot guns are behind the same "don't enable this unless you know what you are doing" barrier as they are for type 3 devices. Note that the blast radius is far worse than for a CXL type 3 mailbox. This device can be used to rip memory out from under
_otherlivehosts_.Jonathan Cameron (4):
cxl: mbox: Preparatory move of functions tocore/mbox.candcxlmbox.h
cxl: mbox: Factor out the mbox specific data for reuse in switch cci
PCI: AddPCI_CLASS_SERIAL_CXL_SWITCH_CCIclass ID topci_ids.hcxl/pci: Add support for stand alone CXL Switch mailbox CCIdrivers/cxl/Kconfig | 14 ++ drivers/cxl/Makefile | 2 + drivers/cxl/core/Makefile | 1 + drivers/cxl/core/core.h | 12 +- drivers/cxl/core/mbox.c | 453 +++++++++++++++++++++++++++-------- drivers/cxl/core/memdev.c | 44 ++-- drivers/cxl/core/regs.c | 35 ++- drivers/cxl/core/switchdev.c | 129 ++++++++++ drivers/cxl/cxl.h | 4 +- drivers/cxl/cxlmbox.h | 201 ++++++++++++++++ drivers/cxl/cxlmem.h | 176 ++------------ drivers/cxl/pci.c | 442 ++++++++++------------------------ drivers/cxl/pmem.c | 6 +- drivers/cxl/security.c | 13 +- drivers/cxl/switch.h | 19 ++ drivers/cxl/switchdev.c | 169 +++++++++++++ include/linux/pci_ids.h | 1 + 17 files changed, 1123 insertions(+), 598 deletions(-) create mode 100644 drivers/cxl/core/switchdev.c create mode 100644 drivers/cxl/cxlmbox.h create mode 100644 drivers/cxl/switch.h create mode 100644 drivers/cxl/switchdev.c