PCI: Add DOE support for endpoint
Subsystems (3)
v5 cover PCI: Add DOE support for endpoint
This patch series introduces the framework for supporting the Data Object Exchange (DOE) feature for PCIe endpoint devices. Please refer to the documentation added in patch 4 for details on the feature and implementation architecture.
The implementation provides a common framework for all PCIe endpoint controllers, not specific to any particular SoC vendor.
The changes since v1 are documented in the respective patch descriptions.
v4: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1 (RFC): https://lore.kernel.org/all/[email protected]/
Below is a code demonstration showing the integration of DOE-EP APIs with EPC drivers.
Note: The provided code is just to show how an EPC driver is expected to
utilize the pci_ep_doe_process_request() and pci_ep_doe_abort() APIs, and might not cover all the corner cases. The below implementation also expects the EPC hardware to have some memory buffer to store the data from(for) write_mailbox(read_mailbox) DOE capability registers.
============================================================================
/* ========== DOE Completion Callback (invoked by DOE-EP core) ========== */
static void doe_completion_cb(struct pci_epc *epc, u8 func_no, u16 cap_offset,
int status, u16 vendor, u8 type, void *response_pl, size_t response_pl_sz)
{
struct epc_driver *drv = epc_get_drvdata(epc);
u32 *response = (u32 *)response_pl;
u32 header1, header2;
int payload_dw, i;
if (readl(drv->base + PF_DOE_CTRL_REG(func_no, cap_offset)) & DOE_CTRL_ABORT) {
/* Aborted: do not send response */
goto free;
}
if (status < 0) {
/* Error: set ERROR bit in DOE Status register */
writel(1 << DOE_STATUS_ERROR,
drv->base + PF_DOE_STATUS_REG(func_no, cap_offset));
goto free;
}
/* Success: write DOE headers first, then response to the read memory */
/* Header 1: Vendor ID (bits 15:0) | Type (bits 23:16) */
header1 = (type << 16) | vendor;
writel(header1, drv->base + PF_DOE_RD_MEMORY_WR_REG(func_no, cap_offset));
/* Header 2: Length in DW (including 2 DW of headers + payload) */
payload_dw = DIV_ROUND_UP(response_pl_sz, sizeof(u32));
header2 = 2 + payload_dw; /* 2 header DWs + payload */
writel(header2, drv->base + PF_DOE_RD_MEMORY_WR_REG(func_no, cap_offset));
/* Set READY bit to signal response ready */
writel(1 << DOE_STATUS_READY,
drv->base + PF_DOE_STATUS_REG(func_no, cap_offset));
/* Write response payload DWORDs to Read memory */
for (i = 0; i < payload_dw; i++)
writel(response[i],
drv->base + PF_DOE_RD_MEMORY_WR_REG(func_no, cap_offset));
/* Wait for the memory to empty before clearing the READY bit */
while (!RD_MEMORY_EMPTY()) {/* wait */}
writel(0 << DOE_STATUS_READY,
drv->base + PF_DOE_STATUS_REG(func_no, cap_offset));free:
/* unset BUSY bit */ writel(0 << DOE_STATUS_BUSY, drv->base + PF_DOE_STATUS_REG(func_no, cap_offset)); kfree(response_pl); }
/* ========== DOE Interrupt Handler (triggered on GO bit from root complex) ========== */
static irqreturn_t doe_interrupt_handler(int irq, void *priv)
{
struct epc_driver *drv = priv;
u16 cap_offset = extract_cap_offset_from_irq(irq);
u8 func_no = extract_func_from_irq(irq);
u32 header1, header2, length_dw, *request;
u16 vendor;
u8 type;
int i, ret;
/* Read first header DWORD: Vendor ID (bits 15:0) | Type (bits 23:16) */
header1 = readl(drv->base + PF_DOE_WR_MEMORY_RD_REG(func_no, cap_offset));
vendor = header1 & 0xFFFF;
type = (header1 >> 16) & 0xFF;
/* Read second header DWORD: Length in DW (includes 2 DW of headers) */
header2 = readl(drv->base + PF_DOE_WR_MEMORY_RD_REG(func_no, cap_offset));
length_dw = header2 & 0x3FFFF; /* Bits 17:0 */
if (!length_dw)
length_dw = PCI_DOE_MAX_LENGTH;
length_dw -= 2; /* Subtract 2 DW of headers to get payload length */
/* Allocate buffer for complete request (headers + payload) */
request = kzalloc(length_dw * sizeof(u32), GFP_ATOMIC);
if (!request) {
writel(1 << DOE_STATUS_ERROR,
drv->base + PF_DOE_STATUS_REG(func_no, cap_offset));
return IRQ_HANDLED;
}
/* Read remaining payload DWORDs from Write memory */
for (i = 0; i < length_dw; i++) {
while (WR_MEMORY_EMPTY()) { /* wait */ }
request[i] = readl(drv->base + PF_DOE_WR_MEMORY_RD_REG(func_no, cap_offset));
}
mutex_lock(&lock);
/* Check the ABORT bit, if set then return */
if (readl(drv->base + PF_DOE_CTRL_REG(func_no, cap_offset)) & DOE_CTRL_ABORT) {
kfree(request);
mutex_unlock(&lock);
return IRQ_HANDLED;
}
/* Set BUSY bit */
writel(1 << DOE_STATUS_BUSY,
drv->base + PF_DOE_STATUS_REG(func_no, cap_offset));
mutex_unlock(&lock);
/* Hand off to DOE-EP core for asynchronous processing */
ret = pci_ep_doe_process_request(drv->epc, func_no, cap_offset,
vendor, type, (void *)request,
length_dw * sizeof(u32),
doe_completion_cb);
if (ret) {
writel(1 << DOE_STATUS_ERROR,
drv->base + PF_DOE_STATUS_REG(func_no, cap_offset));
kfree(request);
}
return IRQ_HANDLED;
}/* ========== Abort Handler (triggered on ABORT bit from root complex) ========== */
static irqreturn_t doe_abort_handler(int irq, void *priv)
{
struct epc_driver *drv = priv;
u16 cap_offset = extract_cap_offset_from_irq(irq);
u8 func_no = extract_func_from_irq(irq);
mutex_lock(&lock);
/* call abort API only if BUSY bit set (pci_ep_doe_process_request() called) */
if (readl(drv->base + PF_DOE_STATUS_REG(func_no, cap_offset)) & DOE_STATUS_BUSY)
pci_ep_doe_abort(drv->epc, func_no, cap_offset);
mutex_unlock(&lock);
/* Discard Write memory contents */
writel(DOE_WR_MEMORY_CTRL_DISCARD,
drv->base + PF_DOE_WR_MEMORY_CTRL_REG(func_no, cap_offset));
/* Clear status bits */
writel((0 << DOE_STATUS_ERROR) | (0 << DOE_STATUS_READY),
drv->base + PF_DOE_STATUS_REG(func_no, cap_offset));
return IRQ_HANDLED;
}====================================================================================
Aksh Garg (4):
PCI/DOE: Move common definitions to the header file
PCI: endpoint: Add DOE mailbox support for endpoint functions
PCI: endpoint: Add support for DOE initialization and setup in EPC
core
Documentation: PCI: Add documentation for DOE endpoint support
Documentation/PCI/endpoint/index.rst | 1 + .../PCI/endpoint/pci-endpoint-doe.rst | 333 ++++++++++ drivers/pci/doe.c | 11 - drivers/pci/endpoint/Kconfig | 14 + drivers/pci/endpoint/Makefile | 1 + drivers/pci/endpoint/pci-ep-doe.c | 594 ++++++++++++++++++ drivers/pci/endpoint/pci-epc-core.c | 104 +++ drivers/pci/pci.h | 48 ++ include/linux/pci-doe.h | 8 + include/linux/pci-epc.h | 9 + 10 files changed, 1112 insertions(+), 11 deletions(-) create mode 100644 Documentation/PCI/endpoint/pci-endpoint-doe.rst create mode 100644 drivers/pci/endpoint/pci-ep-doe.c
Signature · 1 line
2.34.1
PATCH v5 1/4 PCI/DOE: Move common definitions to the header file
Move common macros and structures from drivers/pci/doe.c to drivers/pci/pci.h to allow reuse across root complex and endpoint DOE implementations.
PCI_DOE_MAX_LENGTH macro can be used outside the PCI core as well, hence move the macro to include/linux/pci-doe.h.
These changes prepare the groundwork for the DOE endpoint implementation that will reuse these common definitions.
---
Changes from v4 to v5:
- None.
Changes from v3 to v4:
- None.
Changes from v2 to v3:
- Rebased on 7.1-rc1.
Changes since v1:
- Moved the common macros that need not be visible outside the PCI core to drivers/pci/pci.h instead to include/linux/pci-doe.h as suggested by Lukas Wunner
- Removed the redundant empty inlines guarded with CONFIG_PCI_DOE in include/linux/pci-doe.h.
v4: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
drivers/pci/doe.c | 11 ----------- drivers/pci/pci.h | 9 +++++++++ include/linux/pci-doe.h | 3 +++ 3 files changed, 12 insertions(+), 11 deletions(-)
drivers/pci/doe.c +0 -11
@@ -28,12 +28,6 @@#define PCI_DOE_TIMEOUT HZ#define PCI_DOE_POLL_INTERVAL (PCI_DOE_TIMEOUT / 128) -#define PCI_DOE_FLAG_CANCEL 0 -#define PCI_DOE_FLAG_DEAD 1 - -/* Max data object length is 2^18 dwords */ -#define PCI_DOE_MAX_LENGTH (1 << 18) -/** * struct pci_doe_mb - State for a single DOE mailbox *
@@ -63,11 +57,6 @@ struct pci_doe_mb {#endif }; -struct pci_doe_feature { -u16 vid; -u8 type; -}; -/** * struct pci_doe_task - represents a single query/response *
drivers/pci/pci.h +9 -0
@@ -683,6 +683,15 @@ struct pci_sriov {bool drivers_autoprobe;/* Auto probing of VFs by driver */ }; +/* DOE Mailbox state flags */ +#define PCI_DOE_FLAG_CANCEL 0 +#define PCI_DOE_FLAG_DEAD 1 + +struct pci_doe_feature { +u16 vid; +u8 type; +}; +#ifdef CONFIG_PCI_DOE void pci_doe_init (struct pci_dev *pdev);void pci_doe_destroy (struct pci_dev *pdev);
include/linux/pci-doe.h +3 -0
@@ -19,6 +19,9 @@ struct pci_doe_mb;#define PCI_DOE_FEATURE_CMA 1#define PCI_DOE_FEATURE_SSESSION 2 +/* Max data object length is 2^18 dwords */ +#define PCI_DOE_MAX_LENGTH (1 << 18) +struct pci_doe_mb *pci_find_doe_mailbox (struct pci_dev *pdev,u16 vendor,u8 type);
Signature · 1 line
2.34.1
Re: [PATCH v5 1/4] PCI/DOE: Move common definitions to the header file
Quoted 7 lines · Aksh Garg
Move common macros and structures from
drivers/pci/doe.ctodrivers/pci/pci.hto allow reuse across root complex and endpoint DOE implementations.
PCI_DOE_MAX_LENGTHmacro can be used outside the PCI core as well, hence move the macro toinclude/linux/pci-doe.h.
Quoted 7 lines
These changes prepare the groundwork for the DOE endpoint implementation that will reuse these common definitions.
Co-developed-by Siddharth Vadapalli <[email protected]>Signed-off-by Siddharth Vadapalli <[email protected]>Signed-off-by Aksh Garg <[email protected]>---
Quoted 93 lines
Changes from v4 to v5:
- None.Changes from v3 to v4:
- None.Changes from v2 to v3:
- Rebased on 7.1-rc1.Changes since v1:
- Moved the common macros that need not be visible outside the PCI core todrivers/pci/pci.hinstead toinclude/linux/pci-doe.has suggested by Lukas Wunner
- Removed the redundant empty inlines guarded withCONFIG_PCI_DOEininclude/linux/pci-doe.h.v4: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/drivers/pci/doe.c | 11 ----------- drivers/pci/pci.h | 9 +++++++++ include/linux/pci-doe.h | 3 +++ 3 files changed, 12 insertions(+), 11 deletions(-)drivers/pci/doe.c +0
-11@@ -28,12 +28,6 @@#define PCI_DOE_TIMEOUT HZ#define PCI_DOE_POLL_INTERVAL (PCI_DOE_TIMEOUT / 128) -#define PCI_DOE_FLAG_CANCEL 0 -#define PCI_DOE_FLAG_DEAD 1 - -/* Max data object length is 2^18 dwords */ -#define PCI_DOE_MAX_LENGTH (1 << 18) -/** * struct pci_doe_mb - State for a single DOE mailbox * @@ -63,11 +57,6 @@ struct pci_doe_mb {#endif }; -struct pci_doe_feature { -u16 vid; -u8 type; -}; -/** * struct pci_doe_task - represents a single query/response * drivers/pci/pci.h +9
-0@@ -683,6 +683,15 @@ struct pci_sriov {bool drivers_autoprobe;/* Auto probing of VFs by driver */ }; +/* DOE Mailbox state flags */ +#define PCI_DOE_FLAG_CANCEL 0 +#define PCI_DOE_FLAG_DEAD 1 + +struct pci_doe_feature { +u16 vid; +u8 type; +}; +#ifdef CONFIG_PCI_DOE void pci_doe_init (struct pci_dev *pdev);void pci_doe_destroy (struct pci_dev *pdev);include/linux/pci-doe.h +3
-1@@ -19,6 +19,9 @@ struct pci_doe_mb;#define PCI_DOE_FEATURE_CMA 1#define PCI_DOE_FEATURE_SSESSION 2 +/* Max data object length is 2^18 dwords */ +#define PCI_DOE_MAX_LENGTH (1 << 18) +struct pci_doe_mb *pci_find_doe_mailbox (struct pci_dev *pdev,u16 vendor,u8 type); --2.34.1
PATCH v5 2/4 PCI: endpoint: Add DOE mailbox support for endpoint functions
In reply to Aksh GargDOE (Data Object Exchange) is a standard PCIe extended capability feature introduced in the Data Object Exchange (DOE) ECN for PCIe r5.0. It provides a communication mechanism primarily used for implementing PCIe security features such as device authentication, and secure link establishment. Think of DOE as a sophisticated mailbox system built into PCIe. The root complex can send structured requests to the endpoint device through DOE mailboxes, and the endpoint device responds with appropriate data.
Add the DOE support for PCIe endpoint devices, enabling endpoint functions to process the DOE requests from the host. The implementation provides framework APIs for EPC core driver and controller drivers to register mailboxes, and request processing with workqueues ensuring sequential handling per mailbox, and parallel handling across mailboxes. The Discovery protocol is handled internally by the DOE core.
This implementation complements the existing DOE implementation for root complex in drivers/pci/doe.c.
---
Changes from v4 to v5:
- Addressed the review comments by Sashiko
- Added refcount per DOE Mailbox to fix Use-After-Free bug
- Change in the Abort Sequence:
* Instead of waiting on flush_workqueue() to clear the CANCEL flag, return immediately after setting the CANCEL flag. The CANCEL flag gets cleared in signal_task_complete(), allowing the mailbox to accept new requests
* Abort sequence handling in various scenarios is updated and explained in the documentation at PATCH 4/4
Changes from v3 to v4:
- Used 'Returns' instead of 'RETURNS' in the function docstrings to comply with kernel-doc format, as suggested by Manivannan Sadhasivam.
- In pci_ep_doe_process_request(), changed the type of request buffer from "const void *" to "void *", as the ownership is transferred to DOE-EP framework, which is responsible to free the buffer.
- Added "struct pci_epc *epc" to typedef "pci_ep_doe_complete_t", to be used by the EPC driver.
Changes from v2 to v3:
- Rebased on 7.1-rc1.
Changes since v1:
- Moved the DOE-EP core file to drivers/pci/endpoint/pci-ep-doe.c, and corresponding Kconfig and Makefile to match the existing naming scheme, as suggested by Niklas Cassel.
- Renamed the config from PCI_DOE_EP to PCI_ENDPOINT_DOE
- Moved the function declarations that need not be visible outside the PCI core to drivers/pci/pci.h instead to include/linux/pci-doe.h as suggested by Lukas Wunner
- Converted from synchronous to asynchronous request processing:
* Removed wait_for_completion() from pci_ep_doe_process_request()
* Function returns immediately after queuing to workqueue, hence removed private data for completion in the task structure
* Added completion callback as an additional argument to pci_ep_doe_process_request(), which takes the response and status parameters as arguments (along with other required arguments), hence removed task_status in the task structure
* Created a typedef pci_ep_doe_complete_t for completion callback
* Removed the pci_ep_doe_task_complete() function, as it would not be required anymore with these changes
* Moved from INIT_WORK_ONSTACK() to INIT_WORK(), to initialize the work on heap instead of stack
* signal_task_complete() now invokes the completion callback, once the protocol handler completes its task
- Changed from dynamic xarray-based protocol registration to static array:
* Removed the register/unregister protocol APIs
* Replaced the dynamic xarray with static array of struct pci_doe_protocol
* Added discovery protocol to static array, instead of treating it specially, hence removed the special handling for Discovery protocol in doe_ep_task_work()
* Updated pci_ep_doe_handle_discovery() and pci_ep_doe_find_protocol() accordingly.
- Memory Management:
* DOE core frees request buffer in signal_task_complete()
or during error handling
* pci_ep_doe_process_request() defines response_pl and response_pl_sz as NULL and 0 respectively, whose pointer is passed to the protocol handler, hence removed the arguments void **response, size_t *response_sz to this function.
- Task structure refactoring:
* Response buffer: void **response_pl to void *response_pl
* Response size: size_t *response_pl_sz to size_t response_pl_sz
* Changed the completion callback to type pci_ep_doe_complete_t
* Removed void *private and int task_status
- Updated documentation comments of the functions according to the changes
v4: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
drivers/pci/endpoint/Kconfig | 14 + drivers/pci/endpoint/Makefile | 1 + drivers/pci/endpoint/pci-ep-doe.c | 594 ++++++++++++++++++++++++++++++ drivers/pci/pci.h | 39 ++ include/linux/pci-doe.h | 5 + include/linux/pci-epc.h | 3 + 6 files changed, 656 insertions(+) create mode 100644 drivers/pci/endpoint/pci-ep-doe.c
drivers/pci/endpoint/Kconfig +14 -0
@@ -36,6 +36,20 @@ config PCI_ENDPOINT_MSI_DOORBELL doorbell. The RC can trigger doorbell in EP by writing data to a dedicated BAR, which the EP maps to the controller's message address. +config PCI_ENDPOINT_DOE + bool "PCI Endpoint Data Object Exchange (DOE) support" + depends on PCI_ENDPOINT + help + This enables support for Data Object Exchange (DOE) protocol + on PCI Endpoint controllers. It provides a communication + mechanism through mailboxes, primarily used for PCIe security + features. + + Say Y here if you want be able to communicate using PCIe DOE + mailboxes. + + If unsure, say N. + source "drivers/pci/endpoint/functions/Kconfig" endmenu
drivers/pci/endpoint/Makefile +1 -0
@@ -7,3 +7,4 @@ obj-$(CONFIG_PCI_ENDPOINT_CONFIGFS) += pci-ep-cfs.o obj-$(CONFIG_PCI_ENDPOINT) += pci-epc-core.o pci-epf-core.o\ pci-epc-mem.o functions/ obj-$(CONFIG_PCI_ENDPOINT_MSI_DOORBELL) += pci-ep-msi.o +obj-$(CONFIG_PCI_ENDPOINT_DOE) += pci-ep-doe.o
drivers/pci/endpoint/pci-ep-doe.c new file +594 -0
594 lines not on this page · raw message
drivers/pci/pci.h +39 -0
@@ -692,6 +692,13 @@ struct pci_doe_feature {u8 type; }; +struct pci_epc ; + +typedef void (*pci_ep_doe_complete_t )(struct pci_epc *epc,u8 func_no, +u16 cap_offset,int status, +u16 vendor,u8 type, +void *response_pl,size_t response_pl_sz); +#ifdef CONFIG_PCI_DOE void pci_doe_init (struct pci_dev *pdev);void pci_doe_destroy (struct pci_dev *pdev);
@@ -702,6 +709,38 @@ static inline void pci_doe_destroy(struct pci_dev *pdev) { }static inline void pci_doe_disconnected (struct pci_dev *pdev) { }#endif +#ifdef CONFIG_PCI_ENDPOINT_DOE +int pci_ep_doe_init (struct pci_epc *epc); +int pci_ep_doe_add_mailbox (struct pci_epc *epc,u8 func_no,u16 cap_offset); +int pci_ep_doe_process_request (struct pci_epc *epc,u8 func_no,u16 cap_offset, +u16 vendor,u8 type,void *request, +size_t request_sz,pci_ep_doe_complete_t complete); +int pci_ep_doe_abort (struct pci_epc *epc,u8 func_no,u16 cap_offset); +void pci_ep_doe_destroy (struct pci_epc *epc); +#else +static inline int pci_ep_doe_init (struct pci_epc *epc) {return -EOPNOTSUPP ; } +static inline int pci_ep_doe_add_mailbox (struct pci_epc *epc,u8 func_no, +u16 cap_offset) +{ +return -EOPNOTSUPP ; +} + +static inline int pci_ep_doe_process_request (struct pci_epc *epc,u8 func_no, +u16 cap_offset,u16 vendor,u8 type, +void *request,size_t request_sz, +pci_ep_doe_complete_t complete) +{ +return -EOPNOTSUPP ; +} + +static inline int pci_ep_doe_abort (struct pci_epc *epc,u8 func_no,u16 cap_offset) +{ +return -EOPNOTSUPP ; +} + +static inline void pci_ep_doe_destroy (struct pci_epc *epc) { } +#endif +#ifdef CONFIG_PCI_NPEM void pci_npem_create (struct pci_dev *dev);void pci_npem_remove (struct pci_dev *dev);
include/linux/pci-doe.h +5 -0
@@ -22,6 +22,11 @@ struct pci_doe_mb;/* Max data object length is 2^18 dwords */ #define PCI_DOE_MAX_LENGTH (1 << 18) +typedef int (*pci_doe_protocol_handler_t )(const void *request, +size_t request_sz, +void **response, +size_t *response_sz); +struct pci_doe_mb *pci_find_doe_mailbox (struct pci_dev *pdev,u16 vendor,u8 type);
include/linux/pci-epc.h +3 -0
@@ -182,6 +182,9 @@ struct pci_epc {unsigned long function_num_map;int domain_nr;bool init_complete; +#ifdef CONFIG_PCI_ENDPOINT_DOE +struct xarray doe_mbs; +#endif };/**
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 2/4] PCI: endpoint: Add DOE mailbox support for endpoint functions
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] A sticky `PCI_DOE_FLAG_CANCEL` flag causes spurious aborts of valid requests following an idle…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] A sticky PCI_DOE_FLAG_CANCEL flag causes spurious aborts of valid requests following an idle mailbox abort.
- [High] Potential hard deadlock due to acquiring non-IRQ-safe xarray spinlocks in hardirq context.
- [High] A TOCTOU race between pci_ep_doe_submit_task() and pci_ep_doe_destroy_mb() causes queue_work() to be called on a destroyed workqueue.
- [High] Clearing the PCI_DOE_FLAG_CANCEL flag unconditionally can silently erase a concurrent DOE Abort signal from the host. --
commit 0a3d7b35451c74090ea52cb3095ab14d0e4a6a56
Author: Aksh Garg <[email protected]>
PCI: endpoint: Add DOE mailbox support for endpoint functions
This commit implements Data Object Exchange (DOE) for PCIe endpoint devices. It provides framework APIs to register mailboxes and handle DOE requests using workqueues. Discovery protocol support is built-in.
Quoted 4 lines
drivers/pci/endpoint/pci-ep-doe.c new file +0
-0
[ ... ]
drivers/pci/endpoint/pci-ep-doe.c line 236
Quoted 12 lines
drivers/pci/endpoint/pci-ep-doe.c new file +12
-0+static struct pci_ep_doe_mb *pci_ep_doe_get_mailbox (struct pci_epc *epc, +u8 func_no,u16 cap_offset) +{ +struct pci_ep_doe_mb *doe_mb; +unsigned long key; + +if (!epc) +return NULL ; + + key =PCI_DOE_MB_KEY (func_no, cap_offset); + +xa_lock (&epc->doe_mbs);
[Severity: High]
Does acquiring the plain xa_lock spinlock here pose a deadlock risk?
Both pci_ep_doe_process_request() and pci_ep_doe_abort() call this function, and these are intended to be executed from a controller's hardware interrupt handler. Because xa_lock() does not disable interrupts, if a thread is accessing the xarray in process context (like in pci_ep_doe_add_mailbox() or pci_ep_doe_destroy()) and is preempted by a DOE interrupt on the same CPU, attempting to acquire the lock in the interrupt handler will deadlock.
Could this be resolved by using xa_lock_irqsave() or initializing the xarray with XA_FLAGS_LOCK_IRQ?
Quoted 9 lines
drivers/pci/endpoint/pci-ep-doe.c new file +9
-0+ + doe_mb =xa_load (&epc->doe_mbs, key); +if (doe_mb && !refcount_inc_not_zero (&doe_mb->refs)) + doe_mb =NULL ; + +xa_unlock (&epc->doe_mbs); + +return doe_mb; +}
[ ... ]
drivers/pci/endpoint/pci-ep-doe.c line 359
Quoted 10 lines
drivers/pci/endpoint/pci-ep-doe.c new file +10
-0+static void signal_task_complete (struct pci_ep_doe_task *task,int status) +{ +struct pci_ep_doe_mb *doe_mb = task->doe_mb; + + task->complete (doe_mb->epc, doe_mb->func_no, doe_mb->cap_offset, + status, task->feat.vid, task->feat.type, + task->response_pl, task->response_pl_sz); + +/* Clear the CANCEL flag for next DOE request */ +clear_bit (PCI_DOE_FLAG_CANCEL , &doe_mb->flags);
[Severity: High]
Can this unconditional clear_bit() unintentionally erase a concurrent DOE Abort signal?
If the host issues a DOE Abort interrupt while the task->complete() callback is still running, pci_ep_doe_abort() will set the PCI_DOE_FLAG_CANCEL bit. However, once the completion callback returns, clear_bit() is unconditionally called here, erasing the concurrent abort signal. As a result, subsequent queued requests might be processed instead of being aborted.
Quoted 19 lines
drivers/pci/endpoint/pci-ep-doe.c new file +19
-0+ +kfree (task->request_pl); +kfree (task); + +/* Release the mailbox reference acquired during process_request */ +pci_ep_doe_put_mailbox (doe_mb); +} + +/** +* doe_ep_task_work() - Work function for processing DOE EP tasks +* @work: Work structure +* +* Process a DOE request by calling the appropriate protocol handler. +*/ +static void doe_ep_task_work (struct work_struct *work) +{ + struct pci_ep_doe_task *task = container_of(work, struct pci_ep_doe_task, + work); +struct pci_ep_doe_mb *doe_mb = task->doe_mb;
drivers/pci/endpoint/pci-ep-doe.c line 388
Quoted 10 lines
drivers/pci/endpoint/pci-ep-doe.c new file +10
-0+pci_doe_protocol_handler_t handler; +int rc; + +if (test_bit (PCI_DOE_FLAG_DEAD , &doe_mb->flags)) { +signal_task_complete (task, -EIO ); +return ; + } + +/* Check if request was aborted */ +if (test_bit (PCI_DOE_FLAG_CANCEL , &doe_mb->flags)) {
[Severity: High]
Is it possible for a newly submitted request to be erroneously aborted here due to a stale CANCEL flag?
Because pci_ep_doe_abort() simply sets the PCI_DOE_FLAG_CANCEL flag and returns, the flag is only cleared by signal_task_complete(). If an abort occurs when the mailbox is entirely idle, no worker will execute to clear the flag. When the host eventually sends a valid request, this worker will see the old CANCEL flag, drop the new request with -ECANCELED, and only then clear it, breaking the expected request sequence.
Quoted 3 lines
drivers/pci/endpoint/pci-ep-doe.c new file +3
-0+signal_task_complete (task, -ECANCELED ); +return ; + }
[ ... ]
drivers/pci/endpoint/pci-ep-doe.c line 428
Quoted 9 lines
drivers/pci/endpoint/pci-ep-doe.c new file +9
-0+static int pci_ep_doe_submit_task (struct pci_ep_doe_mb *doe_mb, +struct pci_ep_doe_task *task) +{ +if (test_bit (PCI_DOE_FLAG_DEAD , &doe_mb->flags)) +return -EIO ; + + task->doe_mb = doe_mb; +INIT_WORK (&task->work, doe_ep_task_work); +queue_work (doe_mb->work_queue, &task->work);
[Severity: High]
Could a Time-Of-Check to Time-Of-Use race here cause queue_work() to be called on a destroyed workqueue?
If the executing thread is preempted immediately after the lockless test_bit(PCI_DOE_FLAG_DEAD, ...) check evaluates to false, another thread running pci_ep_doe_destroy() could mark the mailbox DEAD and call destroy_workqueue(). The mailbox structure itself is kept alive by its reference count, but the workqueue pointer would point to freed memory.
When the preempted thread resumes, it would blindly call queue_work() on the freed workqueue, resulting in a use-after-free panic.
Quoted 2 lines
drivers/pci/endpoint/pci-ep-doe.c new file +2
-0+return 0 ; +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v5 2/4] PCI: endpoint: Add DOE mailbox support for endpoint functions
Quoted 29 lines · On 10/06/26 15:47, [email protected] wrote:
Quoted 5 lines
drivers/pci/endpoint/pci-ep-doe.c new file +0
-0[ ... ]
drivers/pci/endpoint/pci-ep-doe.c line 236
Quoted 12 lines
drivers/pci/endpoint/pci-ep-doe.c new file +12
-0+static struct pci_ep_doe_mb *pci_ep_doe_get_mailbox (struct pci_epc *epc, +u8 func_no,u16 cap_offset) +{ +struct pci_ep_doe_mb *doe_mb; +unsigned long key; + +if (!epc) +return NULL ; + + key =PCI_DOE_MB_KEY (func_no, cap_offset); + +xa_lock (&epc->doe_mbs);[Severity: High]
Does acquiring the plainxa_lockspinlock here pose a deadlock risk?Both
pci_ep_doe_process_request()andpci_ep_doe_abort()call this function, and these are intended to be executed from a controller's hardware interrupt handler. Becausexa_lock()does not disable interrupts, if a thread is accessing the xarray in process context (like inpci_ep_doe_add_mailbox()orpci_ep_doe_destroy()) and is preempted by a DOE interrupt on the same CPU, attempting to acquire the lock in the interrupt handler will deadlock.
Quoted 2 lines
Could this be resolved by using
xa_lock_irqsave()or initializing the xarray withXA_FLAGS_LOCK_IRQ?
will use xa_lock_irqsave()
Quoted 41 lines
drivers/pci/endpoint/pci-ep-doe.c line 359
Quoted 10 lines
drivers/pci/endpoint/pci-ep-doe.c new file +10
-0+static void signal_task_complete (struct pci_ep_doe_task *task,int status) +{ +struct pci_ep_doe_mb *doe_mb = task->doe_mb; + + task->complete (doe_mb->epc, doe_mb->func_no, doe_mb->cap_offset, + status, task->feat.vid, task->feat.type, + task->response_pl, task->response_pl_sz); + +/* Clear the CANCEL flag for next DOE request */ +clear_bit (PCI_DOE_FLAG_CANCEL , &doe_mb->flags);[Severity: High]
Can this unconditionalclear_bit()unintentionally erase a concurrent DOE Abort signal?If the host issues a DOE Abort interrupt while the
task->complete()callback is still running,pci_ep_doe_abort()will set thePCI_DOE_FLAG_CANCELbit. However, once the completion callback returns,clear_bit()is unconditionally called here, erasing the concurrent abort signal. As a result, subsequent queued requests might be processed instead of being aborted.Quoted 5 lines
drivers/pci/endpoint/pci-ep-doe.c new file +5
-0+static void doe_ep_task_work (struct work_struct *work) +{ + struct pci_ep_doe_task *task = container_of(work, struct pci_ep_doe_task, + work); +struct pci_ep_doe_mb *doe_mb = task->doe_mb;drivers/pci/endpoint/pci-ep-doe.c line 388
Quoted 10 lines
drivers/pci/endpoint/pci-ep-doe.c new file +10
-0+pci_doe_protocol_handler_t handler; +int rc; + +if (test_bit (PCI_DOE_FLAG_DEAD , &doe_mb->flags)) { +signal_task_complete (task, -EIO ); +return ; + } + +/* Check if request was aborted */ +if (test_bit (PCI_DOE_FLAG_CANCEL , &doe_mb->flags)) {[Severity: High]
Is it possible for a newly submitted request to be erroneously aborted here due to a stale CANCEL flag?
Quoted 6 lines
Because
pci_ep_doe_abort()simply sets thePCI_DOE_FLAG_CANCELflag and returns, the flag is only cleared bysignal_task_complete(). If an abort occurs when the mailbox is entirely idle, no worker will execute to clear the flag. When the host eventually sends a valid request, this worker will see the old CANCEL flag, drop the new request with-ECANCELED, and only then clear it, breaking the expected request sequence.
The cover letter of this patch shows the code snippet on how an EPC driver is expected to use these APIs. This and above comment has been addressed there.
Quoted 19 lines
Quoted 4 lines
drivers/pci/endpoint/pci-ep-doe.c new file +3
-0+signal_task_complete (task, -ECANCELED ); +return ; + }[ ... ]
drivers/pci/endpoint/pci-ep-doe.c line 428
Quoted 9 lines
drivers/pci/endpoint/pci-ep-doe.c new file +9
-0+static int pci_ep_doe_submit_task (struct pci_ep_doe_mb *doe_mb, +struct pci_ep_doe_task *task) +{ +if (test_bit (PCI_DOE_FLAG_DEAD , &doe_mb->flags)) +return -EIO ; + + task->doe_mb = doe_mb; +INIT_WORK (&task->work, doe_ep_task_work); +queue_work (doe_mb->work_queue, &task->work);[Severity: High]
Could a Time-Of-Check to Time-Of-Use race here causequeue_work()to be called on a destroyed workqueue?
Quoted 9 lines
If the executing thread is preempted immediately after the lockless
test_bit(PCI_DOE_FLAG_DEAD, ...)check evaluates to false, another thread runningpci_ep_doe_destroy()could mark the mailbox DEAD and calldestroy_workqueue(). The mailbox structure itself is kept alive by its reference count, but the workqueue pointer would point to freed memory.When the preempted thread resumes, it would blindly call
queue_work()on the freed workqueue, resulting in a use-after-free panic.
Will add rcu_read_lock()/unlock() in pci_ep_doe_submit_task(), and synchronize_rcu() in pci_ep_doe_destroy() before destroy_workqueue().
Before posting a v6 for these changes, I would wait for the reviewers and maintainers to provide their feedback on this series.
Regards,
Aksh Garg
Quoted 3 lines
Quoted 2 lines
drivers/pci/endpoint/pci-ep-doe.c new file +2
-0+return 0 ; +}
Re: [PATCH v5 2/4] PCI: endpoint: Add DOE mailbox support for endpoint functions
In reply to Aksh GargQuoted 8 lines · Aksh Garg
DOE (Data Object Exchange) is a standard PCIe extended capability feature introduced in the Data Object Exchange (DOE) ECN for PCIe r5.0. It provides a communication mechanism primarily used for implementing PCIe security features such as device authentication, and secure link establishment. Think of DOE as a sophisticated mailbox system built into PCIe. The root complex can send structured requests to the endpoint device through DOE mailboxes, and the endpoint device responds with appropriate data.
Please cite a spec revision and section instead of the ECN because it's easier to find the spec than the ECN. E.g., "PCIe r7.0, sec 6.30" or similar.
Quoted 229 lines
Add the DOE support for PCIe endpoint devices, enabling endpoint functions to process the DOE requests from the host. The implementation provides framework APIs for EPC core driver and controller drivers to register mailboxes, and request processing with workqueues ensuring sequential handling per mailbox, and parallel handling across mailboxes. The Discovery protocol is handled internally by the DOE core.
This implementation complements the existing DOE implementation for root complex in
drivers/pci/doe.c.Co-developed-by Siddharth Vadapalli <[email protected]>Signed-off-by Siddharth Vadapalli <[email protected]>Signed-off-by Aksh Garg <[email protected]>---
Changes from v4 to v5:
- Addressed the review comments by Sashiko
- Added refcount per DOE Mailbox to fix Use-After-Free bug
- Change in the Abort Sequence:
* Instead of waiting onflush_workqueue()to clear the CANCEL flag, return immediately after setting the CANCEL flag. The CANCEL flag gets cleared insignal_task_complete(), allowing the mailbox to accept new requests
* Abort sequence handling in various scenarios is updated and explained in the documentation at PATCH 4/4Changes from v3 to v4:
- Used 'Returns' instead of 'RETURNS' in the function docstrings to comply with kernel-doc format, as suggested by Manivannan Sadhasivam.
- Inpci_ep_doe_process_request(), changed the type of request buffer from "const void *" to "void *", as the ownership is transferred to DOE-EP framework, which is responsible to free the buffer.
- Added "structpci_epc*epc" to typedef "pci_ep_doe_complete_t", to be used by the EPC driver.Changes from v2 to v3:
- Rebased on 7.1-rc1.Changes since v1:
- Moved the DOE-EP core file todrivers/pci/endpoint/pci-ep-doe.c, and correspondingKconfigandMakefileto match the existing naming scheme, as suggested by Niklas Cassel.
- Renamed the config fromPCI_DOE_EPtoPCI_ENDPOINT_DOE
- Moved the function declarations that need not be visible outside the PCI core todrivers/pci/pci.hinstead toinclude/linux/pci-doe.has suggested by Lukas Wunner
- Converted from synchronous to asynchronous request processing:
* Removedwait_for_completion()frompci_ep_doe_process_request()
* Function returns immediately after queuing to workqueue, hence removed private data for completion in the task structure
* Added completion callback as an additional argument topci_ep_doe_process_request(), which takes the response and status parameters as arguments (along with other required arguments), hence removedtask_statusin the task structure
* Created a typedefpci_ep_doe_complete_tfor completion callback
* Removed thepci_ep_doe_task_complete()function, as it would not be required anymore with these changes
* Moved fromINIT_WORK_ONSTACK()toINIT_WORK(), to initialize the work on heap instead of stack
*signal_task_complete()now invokes the completion callback, once the protocol handler completes its task
- Changed from dynamic xarray-based protocol registration to static array:
* Removed the register/unregister protocol APIs
* Replaced the dynamic xarray with static array of structpci_doe_protocol
* Added discovery protocol to static array, instead of treating it specially, hence removed the special handling for Discovery protocol indoe_ep_task_work()
* Updatedpci_ep_doe_handle_discovery()andpci_ep_doe_find_protocol()accordingly.
- Memory Management:
* DOE core frees request buffer insignal_task_complete()
or during error handling
*pci_ep_doe_process_request()definesresponse_plandresponse_pl_szas NULL and 0 respectively, whose pointer is passed to the protocol handler, hence removed the arguments void **response,size_t*response_szto this function.
- Task structure refactoring:
* Response buffer: void **response_pl to void*response_pl
* Response size:size_t*response_pl_sztosize_tresponse_pl_sz
* Changed the completion callback to typepci_ep_doe_complete_t
* Removed void *private and inttask_status
- Updated documentation comments of the functions according to the changesv4: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/drivers/pci/endpoint/Kconfig | 14 + drivers/pci/endpoint/Makefile | 1 + drivers/pci/endpoint/pci-ep-doe.c | 594 ++++++++++++++++++++++++++++++ drivers/pci/pci.h | 39 ++ include/linux/pci-doe.h | 5 + include/linux/pci-epc.h | 3 + 6 files changed, 656 insertions(+) create mode 100644 drivers/pci/endpoint/pci-ep-doe.cdrivers/pci/endpoint/Kconfig +14
-0@@ -36,6 +36,20 @@ config PCI_ENDPOINT_MSI_DOORBELL doorbell. The RC can trigger doorbell in EP by writing data to a dedicated BAR, which the EP maps to the controller's message address. +config PCI_ENDPOINT_DOE + bool "PCI Endpoint Data Object Exchange (DOE) support" + depends on PCI_ENDPOINT + help + This enables support for Data Object Exchange (DOE) protocol + on PCI Endpoint controllers. It provides a communication + mechanism through mailboxes, primarily used for PCIe security + features. + + Say Y here if you want be able to communicate using PCIe DOE + mailboxes. + + If unsure, say N. + source "drivers/pci/endpoint/functions/Kconfig" endmenudrivers/pci/endpoint/Makefile +1
-0@@ -7,3 +7,4 @@ obj-$(CONFIG_PCI_ENDPOINT_CONFIGFS) += pci-ep-cfs.o obj-$(CONFIG_PCI_ENDPOINT) += pci-epc-core.o pci-epf-core.o\ pci-epc-mem.o functions/ obj-$(CONFIG_PCI_ENDPOINT_MSI_DOORBELL) += pci-ep-msi.o +obj-$(CONFIG_PCI_ENDPOINT_DOE) += pci-ep-doe.odrivers/pci/endpoint/pci-ep-doe.c new file +92
-0@@ -0,0 +1,594 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* +* Data Object Exchange for PCIe Endpoint +* PCIe r7.0, sec 6.30 DOE +* +* Copyright (C) 2026 Texas Instruments Incorporated - https://www.ti.com +* Aksh Garg <[email protected]> +* Siddharth Vadapalli <[email protected]> +*/ + +#define dev_fmt (fmt) "DOE EP: " fmt + +#include <linux/bitfield.h> +#include <linux/device.h> +#include <linux/pci.h> +#include <linux/pci-epc.h> +#include <linux/pci-doe.h> +#include <linux/refcount.h> +#include <linux/slab.h> +#include <linux/workqueue.h> +#include <linux/xarray.h> + +#include "../pci.h" + +/* Forward declaration of discovery protocol handler */ +static int pci_ep_doe_handle_discovery (const void *request,size_t request_sz, +void **response,size_t *response_sz); + +/** +* struct pci_doe_protocol - DOE protocol handler entry +* @vid: Vendor ID +* @type: Protocol type +* @handler: Handler function pointer +*/ +struct pci_doe_protocol { +u16 vid; +u8 type; +pci_doe_protocol_handler_t handler; +}; + +/** +* struct pci_ep_doe_mb - State for a single DOE mailbox on EP +* +* This state is used to manage a single DOE mailbox capability on the +* endpoint side. +* +* @epc: PCI endpoint controller this mailbox belongs to +* @func_no: Physical function number of the function this mailbox belongs to +* @cap_offset: Capability offset +* @work_queue: Queue of work items +* @flags: Bit array of PCI_DOE_FLAG_* flags +* @refs: Refcount to manage mailbox lifetime and ensure safe cleanup +*/ +struct pci_ep_doe_mb { +struct pci_epc *epc; +u8 func_no; +u16 cap_offset; +struct workqueue_struct *work_queue; +unsigned long flags; +refcount_t refs; +}; + +/** +* struct pci_ep_doe_task - Represents a single DOE request/response task +* +* @feat: DOE feature (vendor ID and type) +* @request_pl: Request payload +* @request_pl_sz: Size of request payload in bytes +* @response_pl: Response buffer +* @response_pl_sz: Size of response buffer in bytes +* @complete: Completion callback +* @work: Work structure for workqueue +* @doe_mb: DOE mailbox handling this task +*/ +struct pci_ep_doe_task { +struct pci_doe_feature feat; +const void *request_pl; +size_t request_pl_sz; +void *response_pl; +size_t response_pl_sz; +pci_ep_doe_complete_t complete; + +/* Initialized by pci_ep_doe_submit_task() */ +struct work_struct work; +struct pci_ep_doe_mb *doe_mb; +}; + +/* +* Global registry of protocol handlers. +* When a new DOE protocol, library is added, add an entry to this array. +*/ +static const struct pci_doe_protocol pci_doe_protocols[] = {
drivers/pci/endpoint/pci-ep-doe.c line 102
Quoted 10 lines
drivers/pci/endpoint/pci-ep-doe.c new file +10
-0+ { + .vid = PCI_VENDOR_ID_PCI_SIG, + .type = PCI_DOE_FEATURE_DISCOVERY, + .handler = pci_ep_doe_handle_discovery, + }, +}; + +/* +* Combines function number and capability offset into a unique lookup key +* for storing/retrieving DOE mailboxes in an xarray.
s/Combines/Combine/
drivers/pci/endpoint/pci-ep-doe.c line 115
Quoted 13 lines
drivers/pci/endpoint/pci-ep-doe.c new file +13
-0+*/ +#define PCI_DOE_MB_KEY (func, offset) \ + (((unsigned long)(func) << 16) | (offset)) +#define PCI_DOE_PROTOCOL_COUNT ARRAY_SIZE(pci_doe_protocols) + +/** +* pci_ep_doe_init() - Initialize the DOE framework for a controller in EP mode +* @epc: PCI endpoint controller +* +* Initialize the DOE framework data structures. This only initializes +* the xarray that will hold the mailboxes. +* +* Returns: 0 on success, -errno on failure
s/Returns:/Return:/ (throughout)
Mani suggested "Returns" (from v3 to v4 above), so that's OK too.
https://origin.kernel.org/doc/html/latest/doc-guide/kernel-doc.html
includes four "Return:" examples and one "Returns:" example, and "Return" fits better in the preferred imperative mood, so I have a slight preference for that.
drivers/pci/endpoint/pci-ep-doe.c line 120
Quoted 5 lines
drivers/pci/endpoint/pci-ep-doe.c new file +5
-0+*/ +int pci_ep_doe_init (struct pci_epc *epc) +{ +if (!epc) +return -EINVAL ;
I doubt this is useful. Obviously a bug in the caller and I'd rather take the NULL pointer dereference, which will definitely be noticed, than assume the buggy caller checks for failure. The function might as well be void then, same as pci_doe_init().
Quoted 17 lines
drivers/pci/endpoint/pci-ep-doe.c new file +17
-0+xa_init (&epc->doe_mbs); +return 0 ; +} +EXPORT_SYMBOL_GPL (pci_ep_doe_init); + +/** +* pci_ep_doe_add_mailbox() - Add a DOE mailbox for a physical function +* @epc: PCI endpoint controller +* @func_no: Physical function number +* @cap_offset: Offset of the DOE capability +* +* Create and register a DOE mailbox for the specified physical function +* and capability offset. +* +* EPC core driver calls this for each DOE capability discovered in the config +* space of each endpoint function if DOE support is available for the EPC. +*
drivers/pci/endpoint/pci-ep-doe.c line 148
Quoted 10 lines
drivers/pci/endpoint/pci-ep-doe.c new file +10
-0+* Returns: 0 on success, -errno on failure +*/ +int pci_ep_doe_add_mailbox (struct pci_epc *epc,u8 func_no,u16 cap_offset) +{ +struct pci_ep_doe_mb *doe_mb; +unsigned long key; +int ret; + +if (!epc) +return -EINVAL ;
Also doubtful about this.
Quoted 46 lines
drivers/pci/endpoint/pci-ep-doe.c new file +46
-0+ doe_mb =kzalloc_obj (*doe_mb,GFP_KERNEL ); +if (!doe_mb) +return -ENOMEM ; + + doe_mb->epc = epc; + doe_mb->func_no = func_no; + doe_mb->cap_offset = cap_offset; + + doe_mb->work_queue =alloc_ordered_workqueue ("pci_ep_doe[%s:pf%d:offset%x]" ,0 , +dev_name (&epc->dev), + func_no, cap_offset); +if (!doe_mb->work_queue) { +dev_err (epc->dev.parent, +"[pf%d:offset%x] failed to allocate work queue\n" , + func_no, cap_offset); + ret = -ENOMEM ; + goto err_free; + } + +/* Add to xarray with composite key */ + key =PCI_DOE_MB_KEY (func_no, cap_offset); + ret =xa_insert (&epc->doe_mbs, key, doe_mb,GFP_KERNEL ); +if (ret) { +dev_err (epc->dev.parent, +"[pf%d:offset%x] failed to insert mailbox: %d\n" , + func_no, cap_offset, ret); + goto err_destroy; + } + +refcount_set (&doe_mb->refs,1 ); + +dev_dbg (epc->dev.parent, +"DOE mailbox added: pf%d offset 0x%x\n" , + func_no, cap_offset); + +return 0 ; + +err_destroy: +destroy_workqueue (doe_mb->work_queue); +err_free: +kfree (doe_mb); +return ret; +} +EXPORT_SYMBOL_GPL (pci_ep_doe_add_mailbox); + +/**
drivers/pci/endpoint/pci-ep-doe.c line 205
Quoted 10 lines
drivers/pci/endpoint/pci-ep-doe.c new file +10
-0+* pci_ep_doe_cancel_tasks() - Cancel all pending tasks +* @doe_mb: DOE mailbox +* +* Cancel all pending tasks in the mailbox. Mark the mailbox as dead +* so no new tasks can be submitted. +*/ +static void pci_ep_doe_cancel_tasks (struct pci_ep_doe_mb *doe_mb) +{ +if (!doe_mb) +return ;
Seems like this silently hides caller bugs without even the possibility of checking for failure?
Quoted 16 lines
drivers/pci/endpoint/pci-ep-doe.c new file +16
-0+/* Mark the mailbox as dead */ +set_bit (PCI_DOE_FLAG_DEAD , &doe_mb->flags); + +/* Stop all pending work items from starting */ +set_bit (PCI_DOE_FLAG_CANCEL , &doe_mb->flags); +} + +/** +* pci_ep_doe_get_mailbox() - Get DOE mailbox by function and offset +* @epc: PCI endpoint controller +* @func_no: Physical function number +* @cap_offset: Offset of the DOE capability +* +* Internal helper to look up a DOE mailbox by its function number and +* capability offset. +*
drivers/pci/endpoint/pci-ep-doe.c line 232
Quoted 10 lines
drivers/pci/endpoint/pci-ep-doe.c new file +10
-0+* Returns: Pointer to the mailbox or NULL if not found +*/ +static struct pci_ep_doe_mb *pci_ep_doe_get_mailbox (struct pci_epc *epc, +u8 func_no,u16 cap_offset) +{ +struct pci_ep_doe_mb *doe_mb; +unsigned long key; + +if (!epc) +return NULL ;
Same?
Quoted 9 lines
drivers/pci/endpoint/pci-ep-doe.c new file +9
-0+ key =PCI_DOE_MB_KEY (func_no, cap_offset); + +xa_lock (&epc->doe_mbs); + + doe_mb =xa_load (&epc->doe_mbs, key); +if (doe_mb && !refcount_inc_not_zero (&doe_mb->refs)) + doe_mb =NULL ; + +xa_unlock (&epc->doe_mbs);
drivers/pci/endpoint/pci-ep-doe.c line 252
Quoted 10 lines
drivers/pci/endpoint/pci-ep-doe.c new file +10
-0+ +return doe_mb; +} + +/** +* pci_ep_doe_put_mailbox() - Release a reference to a DOE mailbox +* @doe_mb: The mailbox structure to release +* +* Drops the reference count. If this was the last active reference, +* the memory allocated for the mailbox structure is freed.
s/Drops/Drop/
s/If ... is freed/Free the ... if this was last active .../
drivers/pci/endpoint/pci-ep-doe.c line 257
Quoted 5 lines
drivers/pci/endpoint/pci-ep-doe.c new file +5
-0+*/ +static void pci_ep_doe_put_mailbox (struct pci_ep_doe_mb *doe_mb) +{ +if (!doe_mb) +return ;
Omit unless there's a reason for this.
Quoted 125 lines
drivers/pci/endpoint/pci-ep-doe.c new file +125
-0+if (refcount_dec_and_test (&doe_mb->refs)) +kfree (doe_mb); +} + +/** +* pci_ep_doe_find_protocol() - Find protocol handler in static array +* @vendor: Vendor ID +* @type: Protocol type +* +* Look up a protocol handler in the static protocol array by matching vendor ID +* and protocol type. +* +* Returns: Handler function pointer or NULL if not found +*/ +static pci_doe_protocol_handler_t pci_ep_doe_find_protocol (u16 vendor,u8 type) +{ +int i; + +/* Search static protocol array */ +for (i =0 ; i <PCI_DOE_PROTOCOL_COUNT ; i++) { +if (pci_doe_protocols[i].vid == vendor && + pci_doe_protocols[i].type == type) +return pci_doe_protocols[i].handler; + } + +return NULL ; +} + +/** +* pci_ep_doe_handle_discovery() - Handle Discovery protocol request +* @request: Request payload +* @request_sz: Request size +* @response: Output pointer for response buffer +* @response_sz: Output pointer for response size +* +* Handle the DOE Discovery protocol. The request contains an index specifying +* which protocol to query. This function creates a response containing the +* vendor ID and protocol type for the requested index, along with the next +* index value for further discovery: +* +* - next_index = 0: Signals this is the last protocol supported +* - next_index = n (non-zero): Signals more protocols available, +* query index n next +* +* Returns: 0 on success, -errno on failure +*/ +static int pci_ep_doe_handle_discovery (const void *request,size_t request_sz, +void **response,size_t *response_sz) +{ +struct pci_doe_protocol protocol; +u8 requested_index, next_index; +u32 *response_pl; +u32 request_pl; +u16 vendor; +u8 type; + +if (request_sz !=sizeof (u32)) +return -EINVAL ; + + request_pl = *(u32 *)request; + requested_index =FIELD_GET (PCI_DOE_DATA_OBJECT_DISC_REQ_3_INDEX , request_pl); + +if (requested_index >=PCI_DOE_PROTOCOL_COUNT ) { +/* No more protocols to report */ + vendor =0 ; + type =0 ; + }else { +/* Get protocol from array at requested_index */ + protocol = pci_doe_protocols[requested_index]; + vendor = protocol.vid; + type = protocol.type; + } + +/* Calculate next index */ + next_index = (requested_index +1 <PCI_DOE_PROTOCOL_COUNT ) ? requested_index +1 :0 ; + + response_pl =kzalloc_obj (*response_pl,GFP_KERNEL ); +if (!response_pl) +return -ENOMEM ; + +/* Build response */ + *response_pl =FIELD_PREP (PCI_DOE_DATA_OBJECT_DISC_RSP_3_VID , vendor) | +FIELD_PREP (PCI_DOE_DATA_OBJECT_DISC_RSP_3_TYPE , type) | +FIELD_PREP (PCI_DOE_DATA_OBJECT_DISC_RSP_3_NEXT_INDEX , next_index); + + *response = response_pl; + *response_sz =sizeof (*response_pl); + +return 0 ; +} + +static void signal_task_complete (struct pci_ep_doe_task *task,int status) +{ +struct pci_ep_doe_mb *doe_mb = task->doe_mb; + + task->complete (doe_mb->epc, doe_mb->func_no, doe_mb->cap_offset, + status, task->feat.vid, task->feat.type, + task->response_pl, task->response_pl_sz); + +/* Clear the CANCEL flag for next DOE request */ +clear_bit (PCI_DOE_FLAG_CANCEL , &doe_mb->flags); + +kfree (task->request_pl); +kfree (task); + +/* Release the mailbox reference acquired during process_request */ +pci_ep_doe_put_mailbox (doe_mb); +} + +/** +* doe_ep_task_work() - Work function for processing DOE EP tasks +* @work: Work structure +* +* Process a DOE request by calling the appropriate protocol handler. +*/ +static void doe_ep_task_work (struct work_struct *work) +{ + struct pci_ep_doe_task *task = container_of(work, struct pci_ep_doe_task, + work); +struct pci_ep_doe_mb *doe_mb = task->doe_mb; +pci_doe_protocol_handler_t handler; +int rc; + +if (test_bit (PCI_DOE_FLAG_DEAD , &doe_mb->flags)) { +signal_task_complete (task, -EIO );
drivers/pci/endpoint/pci-ep-doe.c line 393
Quoted 10 lines
drivers/pci/endpoint/pci-ep-doe.c new file +10
-0+return ; + } + +/* Check if request was aborted */ +if (test_bit (PCI_DOE_FLAG_CANCEL , &doe_mb->flags)) { +signal_task_complete (task, -ECANCELED ); +return ; + } + +/* Find protocol handler in the array */
Comment seems superfluous, given the function name.
drivers/pci/endpoint/pci-ep-doe.c line 404
Quoted 11 lines
drivers/pci/endpoint/pci-ep-doe.c new file +11
-0+ handler =pci_ep_doe_find_protocol (task->feat.vid, task->feat.type); +if (!handler) { +dev_warn_ratelimited (doe_mb->epc->dev.parent, +"[%d:%x] Unsupported protocol VID=%04x TYPE=%02x\n" , + doe_mb->func_no, doe_mb->cap_offset, + task->feat.vid, task->feat.type); +signal_task_complete (task, -EOPNOTSUPP ); +return ; + } + +/* Call protocol handler */
Ditto.
Quoted 34 lines
drivers/pci/endpoint/pci-ep-doe.c new file +34
-0+ rc =handler (task->request_pl, task->request_pl_sz, + &task->response_pl, &task->response_pl_sz); + +signal_task_complete (task, rc); +} + +/** +* pci_ep_doe_submit_task() - Submit a task to be processed +* @doe_mb: DOE mailbox +* @task: Task to submit +* +* Submit a DOE task to the workqueue for asynchronous processing. +* +* Returns: 0 on success, -errno on failure +*/ +static int pci_ep_doe_submit_task (struct pci_ep_doe_mb *doe_mb, +struct pci_ep_doe_task *task) +{ +if (test_bit (PCI_DOE_FLAG_DEAD , &doe_mb->flags)) +return -EIO ; + + task->doe_mb = doe_mb; +INIT_WORK (&task->work, doe_ep_task_work); +queue_work (doe_mb->work_queue, &task->work); +return 0 ; +} + +/** +* pci_ep_doe_process_request() - Process DOE request on endpoint +* @epc: PCI endpoint controller +* @func_no: Physical function number +* @cap_offset: DOE capability offset +* @vendor: Vendor ID from request header +* @type: Protocol type from request header
drivers/pci/endpoint/pci-ep-doe.c line 448
Quoted 10 lines
drivers/pci/endpoint/pci-ep-doe.c new file +10
-0+* @request: Request payload in CPU-native format +* @request_sz: Size of request payload (bytes) +* @complete: Callback to invoke upon completion +* +* Asynchronously process a DOE request received on the endpoint. The request +* payload should not include the DOE header (vendor/type/length). Ownership +* of the request buffer is transferred to DOE EP core, which frees the buffer +* either on error or after the completion callback fires. The protocol handler +* will allocate the response buffer, which the caller (controller driver) must +* free after use.
I guess signal_task_complete() is where the request buffer is freed after completion? Maybe mention the function name directly instead of just "completion callback", e.g., "by signal_task_complete(), the completion callback"?
drivers/pci/endpoint/pci-ep-doe.c line 454
Quoted 5 lines
drivers/pci/endpoint/pci-ep-doe.c new file +5
-0+* This function returns immediately after queuing the request. The completion +* callback will be invoked asynchronously from workqueue context once the +* request is processed. The callback receives the function number and capability +* offset to identify the mailbox, along with a status code (0 on success, -errno +* on failure), and other required arguments.
Wrap to fit in 80 columns.
drivers/pci/endpoint/pci-ep-doe.c line 463
Quoted 8 lines
drivers/pci/endpoint/pci-ep-doe.c new file +8
-0+* As per DOE specification, a mailbox processes one request at a time. +* Therefore, this function will never be called concurrently for the same +* mailbox by different callers. +* +* The caller is responsible for the conversion of the received DOE request +* with le32_to_cpu() before calling this function. +* Similarly, it is responsible for converting the response payload with +* cpu_to_le32() before sending it back over the DOE mailbox.
Wrap to fill 78-80 columns (or add blank line if you want a new paragraph, but this looks like all one paragraph).
drivers/pci/endpoint/pci-ep-doe.c line 472
Quoted 8 lines
drivers/pci/endpoint/pci-ep-doe.c new file +8
-0+* The caller is also responsible for ensuring that the request size +* is within the limits defined by PCI_DOE_MAX_LENGTH. +* +* Returns: 0 if the request was successfully queued, -errno on failure +*/ +int pci_ep_doe_process_request (struct pci_epc *epc,u8 func_no,u16 cap_offset, +u16 vendor,u8 type,void *request,size_t request_sz, +pci_ep_doe_complete_t complete)
Wrap to fit in 80 columns.
Quoted 22 lines
drivers/pci/endpoint/pci-ep-doe.c new file +22
-0+{ +struct pci_ep_doe_mb *doe_mb; +struct pci_ep_doe_task *task; +int rc; + + doe_mb =pci_ep_doe_get_mailbox (epc, func_no, cap_offset); +if (!doe_mb) { +kfree (request); +return -ENODEV ; + } + + task =kzalloc_obj (*task,GFP_ATOMIC ); +if (!task) { +kfree (request); +pci_ep_doe_put_mailbox (doe_mb); +return -ENOMEM ; + } + + task->feat.vid = vendor; + task->feat.type = type; + task->request_pl = request; + task->request_pl_sz = request_sz;
drivers/pci/endpoint/pci-ep-doe.c line 504
Quoted 10 lines
drivers/pci/endpoint/pci-ep-doe.c new file +10
-0+ task->response_pl =NULL ; + task->response_pl_sz =0 ; + task->complete = complete; + + rc =pci_ep_doe_submit_task (doe_mb, task); +if (rc) { +kfree (request); +kfree (task); +pci_ep_doe_put_mailbox (doe_mb); +return rc;
Good candidate for error path ladder, as you did in pci_ep_doe_add_mailbox().
Quoted 10 lines
drivers/pci/endpoint/pci-ep-doe.c new file +10
-0+ } + +return 0 ; +} +EXPORT_SYMBOL_GPL (pci_ep_doe_process_request); + +/** +* pci_ep_doe_abort() - Abort DOE operations on a mailbox +* @epc: PCI endpoint controller +* @func_no: Physical function number
drivers/pci/endpoint/pci-ep-doe.c line 524
Quoted 10 lines
drivers/pci/endpoint/pci-ep-doe.c new file +10
-0+* @cap_offset: DOE capability offset +* +* Abort the queued or in-flight DOE operation for the specified mailbox. +* This function is called by the EP controller driver when the RC sets the +* ABORT bit in the DOE Control register, and the BUSY bit is set in the +* DOE Status Register. +* +* The function sets the CANCEL flag on the mailbox to prevent queued requests +* from starting, and returns immediately. The CANCEL flag gets cleared in +* signal_task_complete(), allowing the mailbox to accept new requests.
s/The function sets .../Set .../
s/and returns/and return/
drivers/pci/endpoint/pci-ep-doe.c line 533
Quoted 9 lines
drivers/pci/endpoint/pci-ep-doe.c new file +9
-0+* +* Returns: 0 on success, -errno on failure +*/ +int pci_ep_doe_abort (struct pci_epc *epc,u8 func_no,u16 cap_offset) +{ +struct pci_ep_doe_mb *doe_mb; + +if (!epc) +return -EINVAL ;
?
Quoted 16 lines
drivers/pci/endpoint/pci-ep-doe.c new file +16
-0+ doe_mb =pci_ep_doe_get_mailbox (epc, func_no, cap_offset); +if (!doe_mb) +return -ENODEV ; + +/* Set CANCEL flag - worker will abort queued requests */ +set_bit (PCI_DOE_FLAG_CANCEL , &doe_mb->flags); + +dev_dbg_ratelimited (epc->dev.parent, +"DOE mailbox abort initialized: PF%d offset 0x%x\n" , + func_no, cap_offset); + +pci_ep_doe_put_mailbox (doe_mb); +return 0 ; +} +EXPORT_SYMBOL_GPL (pci_ep_doe_abort); +
drivers/pci/endpoint/pci-ep-doe.c line 560
Quoted 10 lines
drivers/pci/endpoint/pci-ep-doe.c new file +10
-0+/** +* pci_ep_doe_destroy_mb() - Destroy a single DOE mailbox +* @doe_mb: DOE mailbox to destroy +* +* Internal function to destroy a mailbox and free its resources. +*/ +static void pci_ep_doe_destroy_mb (struct pci_ep_doe_mb *doe_mb) +{ +if (!doe_mb) +return ;
?
Quoted 14 lines
drivers/pci/endpoint/pci-ep-doe.c new file +14
-0+pci_ep_doe_cancel_tasks (doe_mb); + +if (doe_mb->work_queue) +destroy_workqueue (doe_mb->work_queue); + +pci_ep_doe_put_mailbox (doe_mb); +} + +/** +* pci_ep_doe_destroy() - Destroy all DOE mailboxes +* @epc: PCI endpoint controller +* +* Destroy all DOE mailboxes and free associated resources. +*
drivers/pci/endpoint/pci-ep-doe.c line 585
Quoted 10 lines
drivers/pci/endpoint/pci-ep-doe.c new file +10
-0+* The EPC core driver calls this to free all DOE resources, +* if DOE support is available for the EPC. +*/ +void pci_ep_doe_destroy (struct pci_epc *epc) +{ +struct pci_ep_doe_mb *doe_mb; +unsigned long index; + +if (!epc) +return ;
?
Quoted 45 lines
drivers/pci/endpoint/pci-ep-doe.c new file +8
-0+xa_for_each (&epc->doe_mbs, index, doe_mb) { +xa_erase (&epc->doe_mbs, index); +pci_ep_doe_destroy_mb (doe_mb); + } + +xa_destroy (&epc->doe_mbs); +} +EXPORT_SYMBOL_GPL (pci_ep_doe_destroy);drivers/pci/pci.h +22
-0@@ -692,6 +692,13 @@ struct pci_doe_feature {u8 type; }; +struct pci_epc ; + +typedef void (*pci_ep_doe_complete_t )(struct pci_epc *epc,u8 func_no, +u16 cap_offset,int status, +u16 vendor,u8 type, +void *response_pl,size_t response_pl_sz); +#ifdef CONFIG_PCI_DOE void pci_doe_init (struct pci_dev *pdev);void pci_doe_destroy (struct pci_dev *pdev);@@ -702,6 +709,38 @@ static inline void pci_doe_destroy(struct pci_dev *pdev) { }static inline void pci_doe_disconnected (struct pci_dev *pdev) { }#endif +#ifdef CONFIG_PCI_ENDPOINT_DOE +int pci_ep_doe_init (struct pci_epc *epc); +int pci_ep_doe_add_mailbox (struct pci_epc *epc,u8 func_no,u16 cap_offset); +int pci_ep_doe_process_request (struct pci_epc *epc,u8 func_no,u16 cap_offset, +u16 vendor,u8 type,void *request, +size_t request_sz,pci_ep_doe_complete_t complete); +int pci_ep_doe_abort (struct pci_epc *epc,u8 func_no,u16 cap_offset); +void pci_ep_doe_destroy (struct pci_epc *epc); +#else +static inline int pci_ep_doe_init (struct pci_epc *epc) {return -EOPNOTSUPP ; } +static inline int pci_ep_doe_add_mailbox (struct pci_epc *epc,u8 func_no, +u16 cap_offset) +{ +return -EOPNOTSUPP ; +}
drivers/pci/pci.h static inline void pci_doe_destroy(struct pci_dev *pdev) { } · line 736
Quoted 10 lines
drivers/pci/pci.h +10
-0+ +static inline int pci_ep_doe_process_request (struct pci_epc *epc,u8 func_no, +u16 cap_offset,u16 vendor,u8 type, +void *request,size_t request_sz, +pci_ep_doe_complete_t complete) +{ +return -EOPNOTSUPP ; +} + +static inline int pci_ep_doe_abort (struct pci_epc *epc,u8 func_no,u16 cap_offset)
Wrap to fit in 80 columns.
Quoted 43 lines
drivers/pci/pci.h +7
-0+{ +return -EOPNOTSUPP ; +} + +static inline void pci_ep_doe_destroy (struct pci_epc *epc) { } +#endif +#ifdef CONFIG_PCI_NPEM void pci_npem_create (struct pci_dev *dev);void pci_npem_remove (struct pci_dev *dev);include/linux/pci-doe.h +5
-0@@ -22,6 +22,11 @@ struct pci_doe_mb;/* Max data object length is 2^18 dwords */ #define PCI_DOE_MAX_LENGTH (1 << 18) +typedef int (*pci_doe_protocol_handler_t )(const void *request, +size_t request_sz, +void **response, +size_t *response_sz); +struct pci_doe_mb *pci_find_doe_mailbox (struct pci_dev *pdev,u16 vendor,u8 type);include/linux/pci-epc.h +3
-0@@ -182,6 +182,9 @@ struct pci_epc {unsigned long function_num_map;int domain_nr;bool init_complete; +#ifdef CONFIG_PCI_ENDPOINT_DOE +struct xarray doe_mbs; +#endif };/** Signature · 1 line
2.34.1
PATCH v5 3/4 PCI: endpoint: Add support for DOE initialization and setup in EPC core
In reply to Aksh GargAdd pci_epc_init_capabilities() in EPC core driver to initialize and setup the capabilities supported by the EPC driver. This calls pci_epc_doe_setup() to setup the DOE framework for an endpoint controller, which discovers the DOE capabilities (extended capability ID 0x2E), and registers each discovered DOE mailbox for all the functions in the endpoint controller.
Add pci_epc_deinit_capabilities() in EPC core driver for cleanup of the resources used by the capabilities of the EPC driver. This calls pci_ep_doe_destroy() to destroy all DOE mailboxes and free associated resources.
---
Changes from v4 to v5:
- Addressed the review comments by Sashiko
Changes from v3 to v4:
- Call DOE setup and destroy APIs directly within the EPC core, instead of relying on the EPC drivers to call them individually. EPC drivers do not need to explicitly handle DOE setup, rather the EPC core manages this transparently. (Suggested by Manivannan Sadhasivam).
- Removed pci_epc_doe_destroy() API, which was just calling pci_ep_doe_destroy(). Instead, called pci_ep_doe_destroy() directly during cleanup.
- Called pci_ep_doe_init() before the "!epc->ops->find_ext_capability" check, because if doe-capable=1 and find_ext_capability() op is undefined, this would not initialize the epc->doe_mbs xarray. However during cleanup, the check "!epc->ops->find_ext_capability" would be unnecessary, and it will try to destroy the epc->doe_mbs xarray even when it was not initialized.
Changes from v2 to v3:
- Rebased on 7.1-rc1.
Changes since v1:
- New patch added to v2 (not present in v1)
v4: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
This patch is introduced based on the feedback provided by Manivannan Sadhasivam at [1].
[1]: https://lore.kernel.org/all/p57x6jleaim5w7t2k3v7tioujnaxuovfpj5euop5ogefvw23se@y5fw3che5p5d/
drivers/pci/endpoint/pci-epc-core.c | 104 ++++++++++++++++++++++++++++ include/linux/pci-epc.h | 6 ++ 2 files changed, 110 insertions(+)
drivers/pci/endpoint/pci-epc-core.c +104 -0
@@ -14,6 +14,8 @@#include <linux/pci-epf.h> #include <linux/pci-ep-cfs.h> +#include "../pci.h" +static const struct class pci_epc_class = { .name ="pci_epc" , };
@@ -842,6 +844,81 @@ void pci_epc_linkdown(struct pci_epc *epc) }EXPORT_SYMBOL_GPL (pci_epc_linkdown); +/** +* pci_epc_doe_setup() - Discover and setup DOE mailboxes for all functions +* @epc: the EPC device on which DOE mailboxes has to be setup +* +* Discover DOE (Data Object Exchange) capabilities for all physical functions +* in the endpoint controller and register DOE mailboxes. +* +* Returns: 0 on success, -errno on failure +*/ +static int pci_epc_doe_setup (struct pci_epc *epc) +{ +u8 func_no, vfunc_no =0 ; +u16 cap_offset; +int ret; + +if (!epc->ops || !epc->ops->find_ext_capability) +return -EINVAL ; + +/* Discover DOE capabilities for all functions */ +for (func_no =0 ; func_no < epc->max_functions; func_no++) { +mutex_lock (&epc->lock); + cap_offset = epc->ops->find_ext_capability (epc, func_no, + vfunc_no,0 , +PCI_EXT_CAP_ID_DOE ); +mutex_unlock (&epc->lock); + +while (cap_offset) { +/* Register this DOE mailbox */ + ret =pci_ep_doe_add_mailbox (epc, func_no, cap_offset); +if (ret) { +dev_warn (&epc->dev, +"[pf%d:offset %x] failed to add DOE mailbox\n" , + func_no, cap_offset); + } + +mutex_lock (&epc->lock); + cap_offset = epc->ops->find_ext_capability (epc, func_no, + vfunc_no, cap_offset, +PCI_EXT_CAP_ID_DOE ); +mutex_unlock (&epc->lock); + } + } + +dev_dbg (&epc->dev,"DOE mailboxes setup complete\n" ); +return 0 ; +} + +/** +* pci_epc_init_capabilities() - Initialize EPC capabilities +* @epc: the EPC device whose capabilities need to be initialized +* +* Invoke to initialize capabilities supported by the EPC device. +*/ +static void pci_epc_init_capabilities (struct pci_epc *epc) +{ +const struct pci_epc_features *epc_features; +int ret; + + epc_features =pci_epc_get_features (epc,0 ,0 ); +if (!epc_features) +return ; + +if (IS_ENABLED (CONFIG_PCI_ENDPOINT_DOE ) && epc_features->doe_capable) { + ret =pci_ep_doe_init (epc); +if (ret) { +dev_warn (&epc->dev,"DOE initialization failed: %d\n" , ret); +return ; + } + + ret =pci_epc_doe_setup (epc); +if (ret) +dev_warn (&epc->dev,"DOE setup failed: %d\n" , ret); + } +} +/** * pci_epc_init_notify() - Notify the EPF device that EPC device initialization * is completed.
@@ -857,6 +934,9 @@ void pci_epc_init_notify(struct pci_epc *epc)if (IS_ERR_OR_NULL (epc))return ; +if (!epc->init_complete) +pci_epc_init_capabilities (epc); +mutex_lock (&epc->list_lock);list_for_each_entry (epf, &epc->pci_epf, list) {mutex_lock (&epf->lock);
@@ -890,6 +970,27 @@ void pci_epc_notify_pending_init(struct pci_epc *epc, struct pci_epf *epf) }EXPORT_SYMBOL_GPL (pci_epc_notify_pending_init); +/** +* pci_epc_deinit_capabilities() - Cleanup EPC capabilities +* @epc: the EPC device whose capabilities need to be cleaned up +* +* Invoke to cleanup capabilities supported by the EPC device, +* and free the associated resources. +*/ +static void pci_epc_deinit_capabilities (struct pci_epc *epc) +{ +const struct pci_epc_features *epc_features; + + epc_features =pci_epc_get_features (epc,0 ,0 ); +if (!epc_features) +return ; + +if (IS_ENABLED (CONFIG_PCI_ENDPOINT_DOE ) && epc_features->doe_capable) { +pci_ep_doe_destroy (epc); +dev_dbg (&epc->dev,"DOE mailboxes destroyed\n" ); + } +} +/** * pci_epc_deinit_notify() - Notify the EPF device about EPC deinitialization * @epc: the EPC device whose deinitialization is completed
@@ -903,6 +1004,9 @@ void pci_epc_deinit_notify(struct pci_epc *epc)if (IS_ERR_OR_NULL (epc))return ; +if (epc->init_complete) +pci_epc_deinit_capabilities (epc); +mutex_lock (&epc->list_lock);list_for_each_entry (epf, &epc->pci_epf, list) {mutex_lock (&epf->lock);
include/linux/pci-epc.h +6 -0
@@ -84,6 +84,8 @@ struct pci_epc_map {* @start: ops to start the PCI link * @stop: ops to stop the PCI link * @get_features: ops to get the features supported by the EPC +* @find_ext_capability: ops to find extended capability offset for a function +* in endpoint controller * @owner: the module owner containing the ops */ struct pci_epc_ops {
@@ -115,6 +117,8 @@ struct pci_epc_ops {void (*stop)(struct pci_epc *epc);const struct pci_epc_features * (*get_features)(struct pci_epc *epc,u8 func_no,u8 vfunc_no); +u16 (*find_ext_capability)(struct pci_epc *epc,u8 func_no, +u8 vfunc_no,u16 start,u8 cap);struct module *owner; };
@@ -270,6 +274,7 @@ struct pci_epc_bar_desc {* @msi_capable: indicate if the endpoint function has MSI capability * @msix_capable: indicate if the endpoint function has MSI-X capability * @intx_capable: indicate if the endpoint can raise INTx interrupts +* @doe_capable: indicate if the endpoint function has DOE capability * @bar: array specifying the hardware description for each BAR * @align: alignment size required for BAR buffer allocation */
@@ -280,6 +285,7 @@ struct pci_epc_features { unsigned int msi_capable : 1; unsigned int msix_capable : 1; unsigned int intx_capable : 1; + unsigned int doe_capable : 1;struct pci_epc_bar_desc bar[PCI_STD_NUM_BARS ];size_t align; };
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 3/4] PCI: endpoint: Add support for DOE initialization and setup in EPC core
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [High] Lockless check of `epc->init_complete` in `pci_epc_init_notify()` and `pci_epc_deinit_notify()`…
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] Lockless check of epc->init_complete in pci_epc_init_notify() and pci_epc_deinit_notify() creates a race condition leading to double initialization or double destruction of DOE mailboxes.
- [High] Queueing a task on a mailbox's workqueue races with workqueue destruction.
- [Medium] DOE capability discovery completely skips Virtual Functions (VFs).
- [Medium] Inconsistent capability check assumes Physical Function 0 (PF0) dictates controller-wide DOE features.
- [Medium] DOE mailboxes and workqueues are leaked if the endpoint controller is destroyed without a prior pci_epc_deinit_notify() call.
--
commit f40c46636c83315eee171f04597bcd6e1bbbedab
Author: Aksh Garg <[email protected]>
PCI: endpoint: Add support for DOE initialization and setup in EPC core
This commit adds pci_epc_init_capabilities() and pci_epc_deinit_capabilities() to the EPC core driver to initialize and tear down DOE capabilities. It discovers and registers DOE mailboxes for the functions in the endpoint controller.
Quoted 4 lines
drivers/pci/endpoint/pci-epc-core.c +0
-0
[ ... ]
Quoted 6 lines
drivers/pci/endpoint/pci-epc-core.c +2
-0@@ -842,6 +844,81 @@ void pci_epc_linkdown(struct pci_epc *epc) }EXPORT_SYMBOL_GPL (pci_epc_linkdown); +/** +* pci_epc_doe_setup() - Discover and setup DOE mailboxes for all functions
[ ... ]
Quoted 5 lines
drivers/pci/endpoint/pci-epc-core.c +5
-0+static int pci_epc_doe_setup (struct pci_epc *epc) +{ +u8 func_no, vfunc_no =0 ; +u16 cap_offset; +int ret;
drivers/pci/endpoint/pci-epc-core.c void pci_epc_linkdown(struct pci_epc *epc) · line 870
Quoted 10 lines
drivers/pci/endpoint/pci-epc-core.c +10
-0+ +if (!epc->ops || !epc->ops->find_ext_capability) +return -EINVAL ; + +/* Discover DOE capabilities for all functions */ +for (func_no =0 ; func_no < epc->max_functions; func_no++) { +mutex_lock (&epc->lock); + cap_offset = epc->ops->find_ext_capability (epc, func_no, + vfunc_no,0 , +PCI_EXT_CAP_ID_DOE );
[Severity: Medium]
Will this skip Virtual Functions during DOE capability discovery?
The loop iterates over physical functions, but vfunc_no is permanently hardcoded to 0 here. VFs that advertise DOE capabilities won't be processed.
Quoted 1 line
drivers/pci/endpoint/pci-epc-core.c +1
-0+mutex_unlock (&epc->lock);
[ ... ]
drivers/pci/endpoint/pci-epc-core.c void pci_epc_linkdown(struct pci_epc *epc) · line 905
Quoted 6 lines
drivers/pci/endpoint/pci-epc-core.c +6
-0+static void pci_epc_init_capabilities (struct pci_epc *epc) +{ +const struct pci_epc_features *epc_features; +int ret; + + epc_features =pci_epc_get_features (epc,0 ,0 );
[Severity: Medium]
Is it intentional to check capability features exclusively against Physical Function 0 (PF0)?
If the controller implements DOE on a non-zero function but not PF0, it seems initialization and cleanup will be skipped entirely. Capability checks might need to be dynamically evaluated per-function.
Quoted 2 lines
drivers/pci/endpoint/pci-epc-core.c +2
-0+if (!epc_features) +return ;
[ ... ]
drivers/pci/endpoint/pci-epc-core.c void pci_epc_init_notify(struct pci_epc *epc) · line 939
Quoted 7 lines
drivers/pci/endpoint/pci-epc-core.c +3
-0@@ -857,6 +934,9 @@ void pci_epc_init_notify(struct pci_epc *epc)if (IS_ERR_OR_NULL (epc))return ; +if (!epc->init_complete) +pci_epc_init_capabilities (epc); +
[Severity: High]
Could checking epc->init_complete without acquiring epc->list_lock lead to a data race?
If pci_epc_init_notify() is invoked concurrently by different threads, both might see !epc->init_complete and execute pci_epc_init_capabilities(), which calls pci_ep_doe_init() twice. This overwrites the xarray internal lock and leaks previously registered mailboxes.
Quoted 2 lines
drivers/pci/endpoint/pci-epc-core.c +0
-0mutex_lock (&epc->list_lock);list_for_each_entry (epf, &epc->pci_epf, list) {
[ ... ]
Quoted 6 lines
drivers/pci/endpoint/pci-epc-core.c +2
-0@@ -890,6 +970,27 @@ void pci_epc_notify_pending_init(struct pci_epc *epc, struct pci_epf *epf) }EXPORT_SYMBOL_GPL (pci_epc_notify_pending_init); +/** +* pci_epc_deinit_capabilities() - Cleanup EPC capabilities
[ ... ]
drivers/pci/endpoint/pci-epc-core.c void pci_epc_notify_pending_init(struct pci_epc *epc, struct pci_epf *epf) · line 989
Quoted 10 lines
drivers/pci/endpoint/pci-epc-core.c +10
-0+static void pci_epc_deinit_capabilities (struct pci_epc *epc) +{ +const struct pci_epc_features *epc_features; + + epc_features =pci_epc_get_features (epc,0 ,0 ); +if (!epc_features) +return ; + +if (IS_ENABLED (CONFIG_PCI_ENDPOINT_DOE ) && epc_features->doe_capable) { +pci_ep_doe_destroy (epc);
[Severity: High]
Does destroying the workqueue here race with task queueing?
When pci_ep_doe_destroy() is called, it eventually destroys the workqueue. However, in pci_ep_doe_submit_task(), there's a lockless test_bit() check:
if (test_bit(PCI_DOE_FLAG_DEAD, &doe_mb->flags))
return -EIO;
... queue_work(doe_mb->work_queue, &task->work);If the workqueue is destroyed right after test_bit() passes, the submission thread might attempt to queue work on the freed workqueue, leading to a use-after-free.
[Severity: Medium]
What happens to the DOE mailboxes and workqueues if the endpoint controller is destroyed without a prior pci_epc_deinit_notify() call?
If the controller driver unbinds and calls pci_epc_destroy() directly, these resources might be leaked since pci_epc_destroy() does not have a fallback to clean up dynamically allocated DOE capabilities.
Quoted 3 lines
drivers/pci/endpoint/pci-epc-core.c +3
-0+dev_dbg (&epc->dev,"DOE mailboxes destroyed\n" ); + } +}
[ ... ]
drivers/pci/endpoint/pci-epc-core.c void pci_epc_deinit_notify(struct pci_epc *epc) · line 1009
Quoted 7 lines
drivers/pci/endpoint/pci-epc-core.c +3
-0@@ -903,6 +1004,9 @@ void pci_epc_deinit_notify(struct pci_epc *epc)if (IS_ERR_OR_NULL (epc))return ; +if (epc->init_complete) +pci_epc_deinit_capabilities (epc); +
[Severity: High]
Can this lockless check of epc->init_complete race similarly to the one in pci_epc_init_notify()?
A concurrent call could cause double destruction of DOE mailboxes and corrupt the xarray.
Quoted 2 lines
drivers/pci/endpoint/pci-epc-core.c +0
-0mutex_lock (&epc->list_lock);list_for_each_entry (epf, &epc->pci_epf, list) {
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH v5 3/4] PCI: endpoint: Add support for DOE initialization and setup in EPC core
In reply to Aksh GargQuoted 77 lines · Aksh Garg
Add
pci_epc_init_capabilities()in EPC core driver to initialize and setup the capabilities supported by the EPC driver. This callspci_epc_doe_setup()to setup the DOE framework for an endpoint controller, which discovers the DOE capabilities (extended capability ID0x2E), and registers each discovered DOE mailbox for all the functions in the endpoint controller.Add
pci_epc_deinit_capabilities()in EPC core driver for cleanup of the resources used by the capabilities of the EPC driver. This callspci_ep_doe_destroy()to destroy all DOE mailboxes and free associated resources.Co-developed-by Siddharth Vadapalli <[email protected]>Signed-off-by Siddharth Vadapalli <[email protected]>Signed-off-by Aksh Garg <[email protected]>---
Changes from v4 to v5:
- Addressed the review comments by SashikoChanges from v3 to v4:
- Call DOE setup and destroy APIs directly within the EPC core, instead of relying on the EPC drivers to call them individually. EPC drivers do not need to explicitly handle DOE setup, rather the EPC core manages this transparently. (Suggested by Manivannan Sadhasivam).
- Removedpci_epc_doe_destroy()API, which was just callingpci_ep_doe_destroy(). Instead, calledpci_ep_doe_destroy()directly during cleanup.
- Calledpci_ep_doe_init()before the "!epc->ops->find_ext_capability" check, because if doe-capable=1 andfind_ext_capability()op is undefined, this would not initialize theepc->doe_mbsxarray. However during cleanup, the check "!epc->ops->find_ext_capability" would be unnecessary, and it will try to destroy theepc->doe_mbsxarray even when it was not initialized.Changes from v2 to v3:
- Rebased on 7.1-rc1.Changes since v1:
- New patch added to v2 (not present in v1)v4: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/This patch is introduced based on the feedback provided by Manivannan Sadhasivam at [1].
[1]: https://lore.kernel.org/all/p57x6jleaim5w7t2k3v7tioujnaxuovfpj5euop5ogefvw23se@y5fw3che5p5d/
drivers/pci/endpoint/pci-epc-core.c | 104 ++++++++++++++++++++++++++++ include/linux/pci-epc.h | 6 ++ 2 files changed, 110 insertions(+)drivers/pci/endpoint/pci-epc-core.c +9
-0@@ -14,6 +14,8 @@#include <linux/pci-epf.h> #include <linux/pci-ep-cfs.h> +#include "../pci.h" +static const struct class pci_epc_class = { .name ="pci_epc" , };@@ -842,6 +844,81 @@ void pci_epc_linkdown(struct pci_epc *epc) }EXPORT_SYMBOL_GPL (pci_epc_linkdown); +/** +* pci_epc_doe_setup() - Discover and setup DOE mailboxes for all functions +* @epc: the EPC device on which DOE mailboxes has to be setup +* +* Discover DOE (Data Object Exchange) capabilities for all physical functions +* in the endpoint controller and register DOE mailboxes. +*
drivers/pci/endpoint/pci-epc-core.c void pci_epc_linkdown(struct pci_epc *epc) · line 863
Quoted 10 lines
drivers/pci/endpoint/pci-epc-core.c +10
-0+* Returns: 0 on success, -errno on failure +*/ +static int pci_epc_doe_setup (struct pci_epc *epc) +{ +u8 func_no, vfunc_no =0 ; +u16 cap_offset; +int ret; + +if (!epc->ops || !epc->ops->find_ext_capability) +return -EINVAL ;
I don't see anything that sets pci_epc_ops.find_ext_capability in this series, so this looks currently unused and untestable, so likely not mergeable as-is. What's the plan for users of this?
Quoted 24 lines
drivers/pci/endpoint/pci-epc-core.c +24
-0+/* Discover DOE capabilities for all functions */ +for (func_no =0 ; func_no < epc->max_functions; func_no++) { +mutex_lock (&epc->lock); + cap_offset = epc->ops->find_ext_capability (epc, func_no, + vfunc_no,0 , +PCI_EXT_CAP_ID_DOE ); +mutex_unlock (&epc->lock); + +while (cap_offset) { +/* Register this DOE mailbox */ + ret =pci_ep_doe_add_mailbox (epc, func_no, cap_offset); +if (ret) { +dev_warn (&epc->dev, +"[pf%d:offset %x] failed to add DOE mailbox\n" , + func_no, cap_offset); + } + +mutex_lock (&epc->lock); + cap_offset = epc->ops->find_ext_capability (epc, func_no, + vfunc_no, cap_offset, +PCI_EXT_CAP_ID_DOE ); +mutex_unlock (&epc->lock); + } + }
drivers/pci/endpoint/pci-epc-core.c void pci_epc_linkdown(struct pci_epc *epc) · line 898
Quoted 10 lines
drivers/pci/endpoint/pci-epc-core.c +10
-0+ +dev_dbg (&epc->dev,"DOE mailboxes setup complete\n" ); +return 0 ; +} + +/** +* pci_epc_init_capabilities() - Initialize EPC capabilities +* @epc: the EPC device whose capabilities need to be initialized +* +* Invoke to initialize capabilities supported by the EPC device.
s/Invoke to initialize/Initialize/
Quoted 37 lines
drivers/pci/endpoint/pci-epc-core.c +26
-0+*/ +static void pci_epc_init_capabilities (struct pci_epc *epc) +{ +const struct pci_epc_features *epc_features; +int ret; + + epc_features =pci_epc_get_features (epc,0 ,0 ); +if (!epc_features) +return ; + +if (IS_ENABLED (CONFIG_PCI_ENDPOINT_DOE ) && epc_features->doe_capable) { + ret =pci_ep_doe_init (epc); +if (ret) { +dev_warn (&epc->dev,"DOE initialization failed: %d\n" , ret); +return ; + } + + ret =pci_epc_doe_setup (epc); +if (ret) +dev_warn (&epc->dev,"DOE setup failed: %d\n" , ret); + } +} +/** * pci_epc_init_notify() - Notify the EPF device that EPC device initialization * is completed. @@ -857,6 +934,9 @@ void pci_epc_init_notify(struct pci_epc *epc)if (IS_ERR_OR_NULL (epc))return ; +if (!epc->init_complete) +pci_epc_init_capabilities (epc); +mutex_lock (&epc->list_lock);list_for_each_entry (epf, &epc->pci_epf, list) {mutex_lock (&epf->lock);
drivers/pci/endpoint/pci-epc-core.c void pci_epc_notify_pending_init(struct pci_epc *epc, struct pci_epf *epf) · line 978
Quoted 9 lines
drivers/pci/endpoint/pci-epc-core.c +6
-0@@ -890,6 +970,27 @@ void pci_epc_notify_pending_init(struct pci_epc *epc, struct pci_epf *epf) }EXPORT_SYMBOL_GPL (pci_epc_notify_pending_init); +/** +* pci_epc_deinit_capabilities() - Cleanup EPC capabilities +* @epc: the EPC device whose capabilities need to be cleaned up +* +* Invoke to cleanup capabilities supported by the EPC device, +* and free the associated resources.
s/Invoke to cleanup/Clean up/
Quoted 69 lines
drivers/pci/endpoint/pci-epc-core.c +18
-0+*/ +static void pci_epc_deinit_capabilities (struct pci_epc *epc) +{ +const struct pci_epc_features *epc_features; + + epc_features =pci_epc_get_features (epc,0 ,0 ); +if (!epc_features) +return ; + +if (IS_ENABLED (CONFIG_PCI_ENDPOINT_DOE ) && epc_features->doe_capable) { +pci_ep_doe_destroy (epc); +dev_dbg (&epc->dev,"DOE mailboxes destroyed\n" ); + } +} +/** * pci_epc_deinit_notify() - Notify the EPF device about EPC deinitialization * @epc: the EPC device whose deinitialization is completed @@ -903,6 +1004,9 @@ void pci_epc_deinit_notify(struct pci_epc *epc)if (IS_ERR_OR_NULL (epc))return ; +if (epc->init_complete) +pci_epc_deinit_capabilities (epc); +mutex_lock (&epc->list_lock);list_for_each_entry (epf, &epc->pci_epf, list) {mutex_lock (&epf->lock);include/linux/pci-epc.h +6
-0@@ -84,6 +84,8 @@ struct pci_epc_map {* @start: ops to start the PCI link * @stop: ops to stop the PCI link * @get_features: ops to get the features supported by the EPC +* @find_ext_capability: ops to find extended capability offset for a function +* in endpoint controller * @owner: the module owner containing the ops */ struct pci_epc_ops {@@ -115,6 +117,8 @@ struct pci_epc_ops {void (*stop)(struct pci_epc *epc);const struct pci_epc_features * (*get_features)(struct pci_epc *epc,u8 func_no,u8 vfunc_no); +u16 (*find_ext_capability)(struct pci_epc *epc,u8 func_no, +u8 vfunc_no,u16 start,u8 cap);struct module *owner; };@@ -270,6 +274,7 @@ struct pci_epc_bar_desc {* @msi_capable: indicate if the endpoint function has MSI capability * @msix_capable: indicate if the endpoint function has MSI-X capability * @intx_capable: indicate if the endpoint can raise INTx interrupts +* @doe_capable: indicate if the endpoint function has DOE capability * @bar: array specifying the hardware description for each BAR * @align: alignment size required for BAR buffer allocation */ @@ -280,6 +285,7 @@ struct pci_epc_features { unsigned int msi_capable : 1; unsigned int msix_capable : 1; unsigned int intx_capable : 1; + unsigned int doe_capable : 1;struct pci_epc_bar_desc bar[PCI_STD_NUM_BARS ];size_t align; };Signature · 1 line
2.34.1
Re: [PATCH v5 3/4] PCI: endpoint: Add support for DOE initialization and setup in EPC core
Quoted 26 lines · Bjorn Helgaas
Quoted 25 lines · Aksh Garg
Add
pci_epc_init_capabilities()in EPC core driver to initialize and setup the capabilities supported by the EPC driver. This callspci_epc_doe_setup()to setup the DOE framework for an endpoint controller, which discovers the DOE capabilities (extended capability ID0x2E), and registers each discovered DOE mailbox for all the functions in the endpoint controller.Add
pci_epc_deinit_capabilities()in EPC core driver for cleanup of the resources used by the capabilities of the EPC driver. This callspci_ep_doe_destroy()to destroy all DOE mailboxes and free associated resources.Co-developed-by Siddharth Vadapalli <[email protected]>Signed-off-by Siddharth Vadapalli <[email protected]>Signed-off-by Aksh Garg <[email protected]>---
+/**
+ *pci_epc_doe_setup()- Discover and setup DOE mailboxes for all functions
+ * @epc: the EPC device on which DOE mailboxes has to be setup
+ *
+ * Discover DOE (Data Object Exchange) capabilities for all physical functions
+ * in the endpoint controller and register DOE mailboxes.
+ *
+ * Returns: 0 on success, -errno on failure
+ */
Quoted 9 lines
drivers/pci/endpoint/pci-epc-core.c void pci_epc_linkdown(struct pci_epc *epc) · line 863
Quoted 8 lines
drivers/pci/endpoint/pci-epc-core.c +8
-0+static int pci_epc_doe_setup (struct pci_epc *epc) +{ +u8 func_no, vfunc_no =0 ; +u16 cap_offset; +int ret; + +if (!epc->ops || !epc->ops->find_ext_capability) +return -EINVAL ;
Hi Bjorn,
Thank you for your feedback comments. I will work on them and post v6 series incorporating the changes.
Quoted 4 lines
I don't see anything that sets pci_epc_ops.find_ext_capability in this series, so this looks currently unused and untestable, so likely not mergeable as-is. What's the plan for users of this?
Currently there is no EPC driver upstream which supports DOE yet. However, I am working on a platform which supports DOE (support for which would be added soon). Mani pointed out that if EPC driver support
for the same is guaranteed to be added soon, the APIs can be merged
first.
For the demonstration purpose, he asked to show how an EPC driver is expected to use the API as a snippet in the cover letter itself.
I will add a code snippet in the cover letter, which sets pci_epc_ops.find_ext_capability as well, if that is acceptable.
Regards,
Aksh Garg
Quoted 29 lines
Quoted 29 lines
drivers/pci/endpoint/pci-epc-core.c +29
-0+/* Discover DOE capabilities for all functions */ +for (func_no =0 ; func_no < epc->max_functions; func_no++) { +mutex_lock (&epc->lock); + cap_offset = epc->ops->find_ext_capability (epc, func_no, + vfunc_no,0 , +PCI_EXT_CAP_ID_DOE ); +mutex_unlock (&epc->lock); + +while (cap_offset) { +/* Register this DOE mailbox */ + ret =pci_ep_doe_add_mailbox (epc, func_no, cap_offset); +if (ret) { +dev_warn (&epc->dev, +"[pf%d:offset %x] failed to add DOE mailbox\n" , + func_no, cap_offset); + } + +mutex_lock (&epc->lock); + cap_offset = epc->ops->find_ext_capability (epc, func_no, + vfunc_no, cap_offset, +PCI_EXT_CAP_ID_DOE ); +mutex_unlock (&epc->lock); + } + } + +dev_dbg (&epc->dev,"DOE mailboxes setup complete\n" ); +return 0 ; +} +
Manivannan Sadhasivam
maintainerRe: [PATCH v5 3/4] PCI: endpoint: Add support for DOE initialization and setup in EPC core
Quoted 49 lines · Aksh Garg
Quoted 26 lines · Bjorn Helgaas
Quoted 25 lines · Aksh Garg
Add
pci_epc_init_capabilities()in EPC core driver to initialize and setup the capabilities supported by the EPC driver. This callspci_epc_doe_setup()to setup the DOE framework for an endpoint controller, which discovers the DOE capabilities (extended capability ID0x2E), and registers each discovered DOE mailbox for all the functions in the endpoint controller.Add
pci_epc_deinit_capabilities()in EPC core driver for cleanup of the resources used by the capabilities of the EPC driver. This callspci_ep_doe_destroy()to destroy all DOE mailboxes and free associated resources.Co-developed-by Siddharth Vadapalli <[email protected]>Signed-off-by Siddharth Vadapalli <[email protected]>Signed-off-by Aksh Garg <[email protected]>---
+/**
+ *pci_epc_doe_setup()- Discover and setup DOE mailboxes for all functions
+ * @epc: the EPC device on which DOE mailboxes has to be setup
+ *
+ * Discover DOE (Data Object Exchange) capabilities for all physical functions
+ * in the endpoint controller and register DOE mailboxes.
+ *
+ * Returns: 0 on success, -errno on failure
+ */Quoted 9 lines
drivers/pci/endpoint/pci-epc-core.c void pci_epc_linkdown(struct pci_epc *epc) · line 863
Quoted 8 lines
drivers/pci/endpoint/pci-epc-core.c +8
-0+static int pci_epc_doe_setup (struct pci_epc *epc) +{ +u8 func_no, vfunc_no =0 ; +u16 cap_offset; +int ret; + +if (!epc->ops || !epc->ops->find_ext_capability) +return -EINVAL ;Hi Bjorn,
Thank you for your feedback comments. I will work on them and post v6 series incorporating the changes.
Quoted 4 lines
I don't see anything that sets pci_epc_ops.find_ext_capability in this series, so this looks currently unused and untestable, so likely not mergeable as-is. What's the plan for users of this?
Quoted 9 lines
Currently there is no EPC driver upstream which supports DOE yet. However, I am working on a platform which supports DOE (support for
which would be added soon). Mani pointed out that if EPC driver support for the same is guaranteed to be added soon, the APIs can be merged first.For the demonstration purpose, he asked to show how an EPC driver is expected to use the API as a snippet in the cover letter itself.
I retract my previous comment here. Let's not introduce dead code in the kernel. We can review the series now, but cannot merge it until the EPC driver gets submitted.
- Mani
Signature · 1 line
மணிவண்ணன் சதாசிவம்
Re: [PATCH v5 3/4] PCI: endpoint: Add support for DOE initialization and setup in EPC core
Quoted 56 lines · Manivannan Sadhasivam
Quoted 55 lines · Aksh Garg
Quoted 26 lines · Bjorn Helgaas
Quoted 25 lines · Aksh Garg
Add
pci_epc_init_capabilities()in EPC core driver to initialize and setup the capabilities supported by the EPC driver. This callspci_epc_doe_setup()to setup the DOE framework for an endpoint controller, which discovers the DOE capabilities (extended capability ID0x2E), and registers each discovered DOE mailbox for all the functions in the endpoint controller.Add
pci_epc_deinit_capabilities()in EPC core driver for cleanup of the resources used by the capabilities of the EPC driver. This callspci_ep_doe_destroy()to destroy all DOE mailboxes and free associated resources.Co-developed-by Siddharth Vadapalli <[email protected]>Signed-off-by Siddharth Vadapalli <[email protected]>Signed-off-by Aksh Garg <[email protected]>---
+/**
+ *pci_epc_doe_setup()- Discover and setup DOE mailboxes for all functions
+ * @epc: the EPC device on which DOE mailboxes has to be setup
+ *
+ * Discover DOE (Data Object Exchange) capabilities for all physical functions
+ * in the endpoint controller and register DOE mailboxes.
+ *
+ * Returns: 0 on success, -errno on failure
+ */Quoted 9 lines
drivers/pci/endpoint/pci-epc-core.c void pci_epc_linkdown(struct pci_epc *epc) · line 863
Quoted 8 lines
drivers/pci/endpoint/pci-epc-core.c +8
-0+static int pci_epc_doe_setup (struct pci_epc *epc) +{ +u8 func_no, vfunc_no =0 ; +u16 cap_offset; +int ret; + +if (!epc->ops || !epc->ops->find_ext_capability) +return -EINVAL ;Hi Bjorn,
Thank you for your feedback comments. I will work on them and post v6 series incorporating the changes.
Quoted 4 lines
I don't see anything that sets pci_epc_ops.find_ext_capability in this series, so this looks currently unused and untestable, so likely not mergeable as-is. What's the plan for users of this?
Currently there is no EPC driver upstream which supports DOE yet. However, I am working on a platform which supports DOE (support for
which would be added soon). Mani pointed out that if EPC driver support for the same is guaranteed to be added soon, the APIs can be merged first.
Quoted 7 lines
Quoted 3 lines
For the demonstration purpose, he asked to show how an EPC driver is expected to use the API as a snippet in the cover letter itself.
I retract my previous comment here. Let's not introduce dead code in the kernel. We can review the series now, but cannot merge it until the EPC driver gets submitted.
Hi Mani,
Sure, I would prefer the series to be reviewed and gather Reviewed-by tags by the time the EPC driver gets submitted, which would help expedite the merge of this series later.
Quoted 3 lines
- Mani
PATCH v5 4/4 Documentation: PCI: Add documentation for DOE endpoint support
In reply to Aksh GargDocument the architecture and implementation details for the Data Object Exchange (DOE) framework for PCIe Endpoint devices.
---
Changes from v4 to v5:
- Updated the DOE Abort handling setion.
Changes from v3 to v4:
- Updated the maximum size of the DOE object from 256KB to 1MB, as per PCIe spec.
- Updated the DOE setup and cleanup sections.
Changes from v2 to v3:
- Rebased on 7.1-rc1.
Changes since v1:
- Squashed the patches [1] and [2], and moved the documentation file to Documentation/PCI/endpoint/pci-endpoint-doe.rst to match the existing naming scheme, as suggested by Niklas Cassel
- Updated the documentation as per the design and implementaion changes made to previous patches in this series:
* Updated for static protocol array instead of dynamic registration
* Documented asynchronous callback model
* Updated request/response flow with new callback signature
* Updated memory ownership: DOE core frees request, driver frees response
* Updated initialization and cleanup sections for new APIs
v4: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: [1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
Documentation/PCI/endpoint/index.rst | 1 + .../PCI/endpoint/pci-endpoint-doe.rst | 333 ++++++++++++++++++ 2 files changed, 334 insertions(+) create mode 100644 Documentation/PCI/endpoint/pci-endpoint-doe.rst
Documentation/PCI/endpoint/index.rst +1 -0
@@ -9,6 +9,7 @@ PCI Endpoint Framework pci-endpoint pci-endpoint-cfs + pci-endpoint-doe pci-test-function pci-test-howto pci-ntb-function
Documentation/PCI/endpoint/pci-endpoint-doe.rst new file +333 -0
333 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v5 4/4] Documentation: PCI: Add documentation for DOE endpoint support
Quoted 6 lines · Aksh Garg
Document the architecture and implementation details for the Data Object Exchange (DOE) framework for PCIe Endpoint devices.
Co-developed-by Siddharth Vadapalli <[email protected]>Signed-off-by Siddharth Vadapalli <[email protected]>Signed-off-by Aksh Garg <[email protected]>
Thanks.
Quoted 35 lines
---
Changes from v4 to v5:
- Updated the DOE Abort handling setion.Changes from v3 to v4:
- Updated the maximum size of the DOE object from 256KB to 1MB, as per PCIe spec.
- Updated the DOE setup and cleanup sections.Changes from v2 to v3:
- Rebased on 7.1-rc1.Changes since v1:
- Squashed the patches [1] and [2], and moved the documentation file toDocumentation/PCI/endpoint/pci-endpoint-doe.rstto match the existing naming scheme, as suggested by Niklas Cassel
- Updated the documentation as per the design and implementaion changes made to previous patches in this series:
* Updated for static protocol array instead of dynamic registration
* Documented asynchronous callback model
* Updated request/response flow with new callback signature
* Updated memory ownership: DOE core frees request, driver frees response
* Updated initialization and cleanup sections for new APIsv4: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: [1] https://lore.kernel.org/all/[email protected]/[2] https://lore.kernel.org/all/[email protected]/Documentation/PCI/endpoint/index.rst | 1 + .../PCI/endpoint/pci-endpoint-doe.rst | 333 ++++++++++++++++++ 2 files changed, 334 insertions(+) create mode 100644 Documentation/PCI/endpoint/pci-endpoint-doe.rst
Signature · 1 line
~Randy
Re: [PATCH v5 4/4] Documentation: PCI: Add documentation for DOE endpoint support
In reply to Aksh GargQuoted 67 lines · Aksh Garg
Document the architecture and implementation details for the Data Object Exchange (DOE) framework for PCIe Endpoint devices.
Co-developed-by Siddharth Vadapalli <[email protected]>Signed-off-by Siddharth Vadapalli <[email protected]>Signed-off-by Aksh Garg <[email protected]>---
Changes from v4 to v5:
- Updated the DOE Abort handling setion.Changes from v3 to v4:
- Updated the maximum size of the DOE object from 256KB to 1MB, as per PCIe spec.
- Updated the DOE setup and cleanup sections.Changes from v2 to v3:
- Rebased on 7.1-rc1.Changes since v1:
- Squashed the patches [1] and [2], and moved the documentation file toDocumentation/PCI/endpoint/pci-endpoint-doe.rstto match the existing naming scheme, as suggested by Niklas Cassel
- Updated the documentation as per the design and implementaion changes made to previous patches in this series:
* Updated for static protocol array instead of dynamic registration
* Documented asynchronous callback model
* Updated request/response flow with new callback signature
* Updated memory ownership: DOE core frees request, driver frees response
* Updated initialization and cleanup sections for new APIsv4: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: [1] https://lore.kernel.org/all/[email protected]/[2] https://lore.kernel.org/all/[email protected]/Documentation/PCI/endpoint/index.rst | 1 + .../PCI/endpoint/pci-endpoint-doe.rst | 333 ++++++++++++++++++ 2 files changed, 334 insertions(+) create mode 100644 Documentation/PCI/endpoint/pci-endpoint-doe.rstDocumentation/PCI/endpoint/index.rst +1
-0@@ -9,6 +9,7 @@ PCI Endpoint Framework pci-endpoint pci-endpoint-cfs + pci-endpoint-doe pci-test-function pci-test-howto pci-ntb-functionDocumentation/PCI/endpoint/pci-endpoint-doe.rst new file +7
-0@@ -0,0 +1,333 @@ +.. SPDX-License-Identifier: GPL-2.0-only OR MIT + +.. include:: <isonum.txt> + +============================================= +Data Object Exchange (DOE) for PCIe Endpoint +=============================================
Documentation/PCI/endpoint/pci-endpoint-doe.rst line 17
Quoted 10 lines
Documentation/PCI/endpoint/pci-endpoint-doe.rst new file +10
-0+ +:Copyright: |copy| 2026 Texas Instruments Incorporated +:Author: Aksh Garg <[email protected]> +:Co-Author: Siddharth Vadapalli <[email protected]> + +Overview +======== + +DOE (Data Object Exchange) is a standard PCIe extended capability feature +introduced in the Data Object Exchange (DOE) ECN for PCIe r5.0. It is an optional
Cite spec r7.0 as you do below, not ECN.
Wrap all to fit in 75-78 columns.
Documentation/PCI/endpoint/pci-endpoint-doe.rst line 20
Quoted 3 lines
Documentation/PCI/endpoint/pci-endpoint-doe.rst new file +3
-0+mechanism for system firmware/software running on root complex (host) to perform +:ref:`data object <data-object-term>` exchanges with an endpoint function. Each +data object is uniquely identified by the Vendor ID of the vendor publishing the
s/vendor ID/Vendor ID/ throughout, for consistency here and to match spec.
Quoted 8 lines
Documentation/PCI/endpoint/pci-endpoint-doe.rst new file +8
-0+data object definition and a Data Object Type value assigned by that vendor. + +Think of DOE as a sophisticated mailbox system built into PCIe. The root complex +can send structured requests to the endpoint device through DOE mailboxes, and +the endpoint device responds with appropriate data. DOE mailboxes are implemented +as PCIe Extended Capabilities in endpoint devices, allowing multiple mailboxes +per function, each potentially supporting different data object protocols. +
Documentation/PCI/endpoint/pci-endpoint-doe.rst line 38
Quoted 10 lines
Documentation/PCI/endpoint/pci-endpoint-doe.rst new file +10
-0+The DOE support for root complex devices has already been implemented in +``drivers/pci/doe.c``. + +How DOE Works +============= + +The DOE mailbox operates through a simple request-response model: + +1. **Host sends request**: The root complex writes a data object (vendor ID, type, + and payload) to the DOE write mailbox register (one DWORD at a time) of the
s/type/Type/
s/payload/Payload/ to match usage below and to indicate that these are termed defined by the PCIe spec.
Documentation/PCI/endpoint/pci-endpoint-doe.rst line 39
Quoted 1 line
Documentation/PCI/endpoint/pci-endpoint-doe.rst new file +1
-0+ endpoint function's config space and sets the GO bit in the DOE Control register
s/GO/DOE Go/ throughout to match spec.
Quoted 150 lines
Documentation/PCI/endpoint/pci-endpoint-doe.rst new file +150
-0+ to indicate that a request is ready for processing. +2. **Endpoint processes**: The endpoint function reads the request from DOE write + mailbox register, sets the BUSY bit in the DOE Status register, identifies the + protocol of the data object, and executes the appropriate handler. +3. **Endpoint responds**: The endpoint function writes the response data object to the + DOE read mailbox register (one DWORD at a time), and sets the READY bit in the DOE + Status register to indicate that the response is ready. If an error occurs during + request processing (such as unsupported protocol or handler failure), the endpoint + sets the ERROR bit in the DOE Status register instead of the READY bit. +4. **Host reads response**: The root complex retrieves the response data from the DOE read + mailbox register once the READY bit is set in the DOE Status register, and then writes + any value to this register to indicate a successful read. If the ERROR bit was set, + the root complex discards the response and performs error handling as needed. + +Each mailbox operates independently and can handle one transaction at a time. The +DOE specification supports data objects of size up to 1MB (2\ :sup:`18` dwords). + +For complete DOE capability details, refer to `PCI Express Base Specification Revision 7.0, +Section 6.30 - Data Object Exchange (DOE)`. + +Key Terminologies +================= + +.. _data-object-term: + +**Data Object** + A structured, vendor-defined, or standard-defined message exchanged between + root complex and endpoint function via DOE capability registers in configuration + space of the function. + +**Mailbox** + A DOE capability on the endpoint device, where each physical function can have + multiple mailboxes. + +**Protocol** + A specific type of DOE communication data object identified by a Vendor ID and Type. + +**Handler** + A function that processes DOE requests of a specific protocol and generates responses. + +Architecture of DOE Implementation for Endpoint +=============================================== + +.. code-block:: text + + +------------------+ + | | + | Root Complex | + | | + +--------^---------+ + | + | Config space access + | over PCIe link + | + +----------v-----------+ + | | + | PCIe Controller | + | as Endpoint | + | | + | +-----------------+ | + | | DOE Mailbox | | + | +-------^---------+ | + +----------|-----------+ + +-----------|---------------------------------------------------------------+ + | | +--------------------+ | + | +---------v--------+ Allocate | +--------------+ | | + | | |-------------------------------->| Request | | | + | | EP Controller | +--->| Buffer | | | + | | Driver | Free | | +--------------+ | | + | | |--------------------------+ | | | | + | +--------^---------+ | | | | | + | | | | | | | + | | | | | | | + | | pci_ep_doe_process_request() | | | | | + | | | | | | | + | +--------v---------+ Free | | | | | + | | |----------------------------+ | DDR | | + | | DOE EP Core |<----+ | | | | + | | (pci-ep-doe.c) | | Discovery | | | | + | | |-----+ Protocol Handler | | | | + | +--------^---------+ | | | | + | | | | | | + | | protocol_handler() | | | | + | | | | | | + | +--------v---------+ | | | | + | | | | | +--------------+ | | + | | Protocol Handler | +----->| Response | | | + | | Module |-------------------------------->| Buffer | | | + | | (CMA/SPDM/Other) | Allocate | +--------------+ | | + | | | | | | + | +------------------+ | | | + | +--------------------+ | + +---------------------------------------------------------------------------+ + +Initialization and Cleanup +-------------------------- + +**Framework Initialization and DOE Setup** + +The EPC core automatically initializes and sets up DOE mailboxes through the +``pci_epc_init_capabilities()`` internal function, which is invoked during +``pci_epc_init_notify()`` when the controller driver calls this API. +Controller drivers do not need to explicitly handle DOE initialization, +rather the EPC core manages this transparently. + +DOE initialization only occurs when the EPC driver reports DOE capability +through the ``doe_capable`` flag in its ``pci_epc_features``. + +This internal function performs the following steps: + +1. Calls ``pci_ep_doe_init(epc)`` to initialize the xarray data structure + (a resizable array data structure defined in linux) named ``doe_mbs`` that + stores metadata of DOE mailboxes for the controller in ``struct pci_epc``. +2. Calls ``pci_epc_doe_setup(epc)`` to discover all DOE capabilities in the + endpoint function's configuration space for each function. For each + discovered DOE capability, calls ``pci_ep_doe_add_mailbox(epc, func_no, + cap_offset)`` to register the mailbox. + +Each DOE mailbox structure created by ``pci_ep_doe_add_mailbox()`` gets an +ordered workqueue allocated for processing DOE requests sequentially for that +mailbox, enabling concurrent request handling across different mailboxes. Each +mailbox is uniquely identified by the combination of physical function number +and capability offset for that controller. + +**Cleanup** + +The EPC core automatically cleans up DOE mailboxes through the +``pci_epc_deinit_capabilities()`` internal function, which is invoked during +``pci_epc_deinit_notify()`` when the controller driver calls this API. +Controller drivers do not need to explicitly handle DOE cleanup, rather +the EPC core manages this transparently. + +DOE cleanup only occurs when the EPC device reported DOE capability +through the ``doe_capable`` flag in its ``pci_epc_features``. + +This internal function calls ``pci_ep_doe_destroy(epc)``, which destroys all +registered mailboxes, cancels any pending tasks, flushes and destroys the +workqueues, and frees all memory allocated to the mailboxes. + +Protocol Handler Support +------------------------ + +Protocol implementations (such as CMA, SPDM, or vendor-specific protocols) are +supported through a static array of protocol handlers. + +When a new DOE protocol library is introduced, its handler function is added to +the static ``pci_doe_protocols`` array in ``drivers/pci/endpoint/pci-ep-doe.c``. +The discovery protocol (VID = 0x0001 (PCI-SIG vendor ID), Type = 0x00 (discovery +protocol)) is included in this static array and handled internally by the +DOE EP core.
Documentation/PCI/endpoint/pci-endpoint-doe.rst line 199
Quoted 10 lines
Documentation/PCI/endpoint/pci-endpoint-doe.rst new file +10
-0+ +Request Handling +---------------- + +The complete flow of a DOE request from the root complex to the response: + +**Step 1: Root Complex → EP Controller Driver** + +The root complex writes a DOE request (Vendor ID, Type, and Payload) to the +DOE write mailbox register in the endpoint function's configuration space and sets
s/write mailbox register/Write Data Mailbox Register/ to match spec.
Specifically, "in the endpoint function's DOE Capability".
Quoted 40 lines
Documentation/PCI/endpoint/pci-endpoint-doe.rst new file +40
-0+the GO bit in the DOE Control register, indicating that the request is ready for +processing. + +**Step 2: EP Controller Driver → DOE EP Core** + +The controller driver reads the request header to determine the data object +length. Based on this length field, it allocates a request buffer in memory +(DDR) of the appropriate size. The driver then reads the complete request +payload from the DOE write mailbox register and converts the data from +little-endian format (the format followed in the PCIe transactions over the +link) to CPU-native format using ``le32_to_cpu()``. The driver defines a +completion callback function with signature ``void (*complete)(struct pci_epc *epc, +u8 func_no, u16 cap_offset, int status, u16 vendor, u8 type, void *response_pl, +size_t response_pl_sz)`` to be invoked when the request processing completes. +The driver then calls ``pci_ep_doe_process_request(epc, func_no, cap_offset, +vendor, type, request, request_sz, complete)`` to hand off the request to the +DOE EP core. This function returns immediately after queuing the work +(without blocking), and the driver sets the BUSY bit in the DOE Status register. + +**Step 3: DOE EP Core Processing** + +The DOE EP core creates a task structure and submits it to the mailbox's ordered +workqueue. This ensures that requests for each mailbox are processed +sequentially, one at a time, as required by the DOE specification. It looks up +the protocol handler based on the Vendor ID and Type from the request header, +and executes the handler function. + +**Step 4: Protocol Handler Execution** + +The workqueue executes the task by calling the registered protocol handler: +``handler(request, request_sz, &response, &response_sz)``. The handler processes +the request, allocates a response buffer in memory (DDR), builds the response +data, and returns the response pointer and size. For the discovery protocol, +the DOE EP core handles this directly without invoking an external handler. + +**Step 5: DOE EP Core → EP Controller Driver** + +After the protocol handler completes, the DOE EP core frees the request buffer, +and invokes the completion callback provided by the controller driver asynchronously. +The callback receives the struct pci_epc, function number, capability offset (to
Documentation/PCI/endpoint/pci-endpoint-doe.rst line 249
Quoted 10 lines
Documentation/PCI/endpoint/pci-endpoint-doe.rst new file +10
-0+identify the mailbox), status code indicating the result of request processing, +vendor ID and type of the data object, the response buffer, and its size. + +**Step 6: EP Controller Driver → Root Complex** + +The controller driver converts the response from CPU-native format to +little-endian format using ``cpu_to_le32()``, writes the response to DOE read +mailbox register, and sets the READY bit in the DOE Status register. The root +complex then reads the response from the read mailbox register. Finally, the controller +driver frees the response buffer (which the handler allocated).
s/READY/Data Object Read/ to match spec
s/read mailbox/DOE Read Data Mailbox/ throughout to match spec
Quoted 12 lines
Documentation/PCI/endpoint/pci-endpoint-doe.rst new file +12
-0+Asynchronous Request Processing +------------------------------- + +The DOE-EP framework implements asynchronous request processing because an +endpoint function can have multiple instances of DOE mailboxes, and requests may +be interleaved across these mailboxes. Request processing of one mailbox should +not result in blocking request processing of other mailboxes. Hence, requests +on each mailbox need to be handled in parallel for optimization. + +For the EP controller driver to handle requests on multiple mailboxes in +parallel, ``pci_ep_doe_process_request()`` must be asynchronous. The function +returns immediately after submitting the request to the mailbox's workqueue,
Documentation/PCI/endpoint/pci-endpoint-doe.rst line 272
Quoted 10 lines
Documentation/PCI/endpoint/pci-endpoint-doe.rst new file +10
-0+without waiting for the request to complete. A completion callback provided by +the controller driver is invoked asynchronously when request processing +finishes. This asynchronous design enables concurrent processing of requests +across different mailboxes. + +Abort Handling +-------------- + +The DOE specification allows the root complex to abort ongoing DOE operations +by setting the ABORT bit in the DOE Control register.
Use spec name for ABORT
Quoted 18 lines
Documentation/PCI/endpoint/pci-endpoint-doe.rst new file +18
-0+**Trigger** + +When the root complex sets the ABORT bit, the EP controller driver detects this +condition (typically in an interrupt handler or register polling routine). The +action taken depends on the timing of the abort: + +- **ABORT before request transfer**: If the ABORT bit is set before the root complex + transfers the request to the mailbox registers, the controller driver should not + call ``pci_ep_doe_abort()`` API. + +- **ABORT during request transfer**: If the ABORT bit is set while the root complex + is still transferring the request to the mailbox registers, the controller driver + should discard the request, and should not call ``pci_ep_doe_abort()`` and + ``pci_ep_doe_process_request()`` APIs in the respective IRQ handlers. + +- **ABORT after request submission**: If the ABORT bit is set after the request + has been fully received and submitted to the DOE EP core via + ``pci_ep_doe_process_request()``, the controller driver must call
Documentation/PCI/endpoint/pci-endpoint-doe.rst line 301
Quoted 10 lines
Documentation/PCI/endpoint/pci-endpoint-doe.rst new file +10
-0+ ``pci_ep_doe_abort(epc, func_no, cap_offset)`` for the affected mailbox to + perform abort sequence in the DOE EP core. + +**Abort Sequence** + +The abort function sets the CANCEL flag on the mailbox to prevent queued requests +from starting. Instead of waiting for the workqueue to flush, it returns immediately. + +The CANCEL flag gets cleared after invoking the completion callback, allowing the +mailbox to accept new requests.
I guess "CANCEL" is a software thing. Using the spec names for the hardware bits will help distinguish CANCEL, ABORT, ERROR, BUSY, etc.
Quoted 34 lines
Documentation/PCI/endpoint/pci-endpoint-doe.rst new file +31
-0+Queued requests that have not started execution will be aborted with an error +status. The currently executing request will complete normally, and the controller +will reject the response if it arrives after the abort sequence has been triggered. + +.. note:: + Independent of when the ABORT bit is triggered, the controller driver must + clear the ERROR, BUSY, and READY bits in the DOE Status register after + completing the abort operation to reset the mailbox to an idle state. + +Error Handling +-------------- + +Errors can occur during DOE request processing for various reasons, such as +unsupported protocols, handler failures, or memory allocation failures. + +**Error Detection** + +When an error occurs during DOE request processing, the DOE EP core propagates this error +back to the controller driver either through the ``pci_ep_doe_process_request()`` return value, +or the status code passed to the completion callback. + +**Error Response** + +When the controller driver receives an error code, it sets the ERROR bit in the DOE Status +register instead of writing a response to the read mailbox register, and frees the buffers. + +API Reference +============= + +.. kernel-doc:: drivers/pci/endpoint/pci-ep-doe.c + :export:Signature · 1 line
2.34.1
Quoted 8 lines · Aksh Garg
This patch series introduces the framework for supporting the Data Object Exchange (DOE) feature for PCIe endpoint devices. Please refer to the documentation added in patch 4 for details on the feature and implementation architecture.
The implementation provides a common framework for all PCIe endpoint controllers, not specific to any particular SoC vendor.
General question, does DOE generate irq when received msg for HOST? I have not related irq handle code.
Any program to test it? such as pci_endpoint_test, need at least one real user to use it.
Frank
Quoted 194 lines
The changes since v1 are documented in the respective patch descriptions.
v4: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1 (RFC): https://lore.kernel.org/all/[email protected]/Below is a code demonstration showing the integration of DOE-EP APIs with EPC drivers.
Note: The provided code is just to show how an EPC driver is expected to
utilize thepci_ep_doe_process_request()andpci_ep_doe_abort()APIs, and might not cover all the corner cases. The below implementation also expects the EPC hardware to have some memory buffer to store the data from(for)write_mailbox(read_mailbox)DOE capability registers.============================================================================
/* ========== DOE Completion Callback (invoked by DOE-EP core) ========== */
static void doe_completion_cb(struct
pci_epc*epc, u8func_no, u16cap_offset,
int status, u16 vendor, u8 type, void*response_pl,size_tresponse_pl_sz){ struct epc_driver *drv = epc_get_drvdata(epc); u32 *response = (u32 *)response_pl; u32 header1, header2; int payload_dw, i; if (readl(drv->base + PF_DOE_CTRL_REG(func_no, cap_offset)) & DOE_CTRL_ABORT) { /* Aborted: do not send response */ goto free; } if (status < 0) { /* Error: set ERROR bit in DOE Status register */ writel(1 << DOE_STATUS_ERROR, drv->base + PF_DOE_STATUS_REG(func_no, cap_offset)); goto free; } /* Success: write DOE headers first, then response to the read memory */ /* Header 1: Vendor ID (bits 15:0) | Type (bits 23:16) */ header1 = (type << 16) | vendor; writel(header1, drv->base + PF_DOE_RD_MEMORY_WR_REG(func_no, cap_offset)); /* Header 2: Length in DW (including 2 DW of headers + payload) */ payload_dw = DIV_ROUND_UP(response_pl_sz, sizeof(u32)); header2 = 2 + payload_dw; /* 2 header DWs + payload */ writel(header2, drv->base + PF_DOE_RD_MEMORY_WR_REG(func_no, cap_offset)); /* Set READY bit to signal response ready */ writel(1 << DOE_STATUS_READY, drv->base + PF_DOE_STATUS_REG(func_no, cap_offset)); /* Write response payload DWORDs to Read memory */ for (i = 0; i < payload_dw; i++) writel(response[i], drv->base + PF_DOE_RD_MEMORY_WR_REG(func_no, cap_offset)); /* Wait for the memory to empty before clearing the READY bit */ while (!RD_MEMORY_EMPTY()) {/* wait */} writel(0 << DOE_STATUS_READY, drv->base + PF_DOE_STATUS_REG(func_no, cap_offset));free:
/* unset BUSY bit */ writel(0 << DOE_STATUS_BUSY, drv->base + PF_DOE_STATUS_REG(func_no, cap_offset)); kfree(response_pl); }/* ========== DOE Interrupt Handler (triggered on GO bit from root complex) ========== */
static
irqreturn_tdoe_interrupt_handler(int irq, void *priv){ struct epc_driver *drv = priv; u16 cap_offset = extract_cap_offset_from_irq(irq); u8 func_no = extract_func_from_irq(irq); u32 header1, header2, length_dw, *request; u16 vendor; u8 type; int i, ret; /* Read first header DWORD: Vendor ID (bits 15:0) | Type (bits 23:16) */ header1 = readl(drv->base + PF_DOE_WR_MEMORY_RD_REG(func_no, cap_offset)); vendor = header1 & 0xFFFF; type = (header1 >> 16) & 0xFF; /* Read second header DWORD: Length in DW (includes 2 DW of headers) */ header2 = readl(drv->base + PF_DOE_WR_MEMORY_RD_REG(func_no, cap_offset)); length_dw = header2 & 0x3FFFF; /* Bits 17:0 */ if (!length_dw) length_dw = PCI_DOE_MAX_LENGTH; length_dw -= 2; /* Subtract 2 DW of headers to get payload length */ /* Allocate buffer for complete request (headers + payload) */ request = kzalloc(length_dw * sizeof(u32), GFP_ATOMIC); if (!request) { writel(1 << DOE_STATUS_ERROR, drv->base + PF_DOE_STATUS_REG(func_no, cap_offset)); return IRQ_HANDLED; } /* Read remaining payload DWORDs from Write memory */ for (i = 0; i < length_dw; i++) { while (WR_MEMORY_EMPTY()) { /* wait */ } request[i] = readl(drv->base + PF_DOE_WR_MEMORY_RD_REG(func_no, cap_offset)); } mutex_lock(&lock); /* Check the ABORT bit, if set then return */ if (readl(drv->base + PF_DOE_CTRL_REG(func_no, cap_offset)) & DOE_CTRL_ABORT) { kfree(request); mutex_unlock(&lock); return IRQ_HANDLED; } /* Set BUSY bit */ writel(1 << DOE_STATUS_BUSY, drv->base + PF_DOE_STATUS_REG(func_no, cap_offset)); mutex_unlock(&lock); /* Hand off to DOE-EP core for asynchronous processing */ ret = pci_ep_doe_process_request(drv->epc, func_no, cap_offset, vendor, type, (void *)request, length_dw * sizeof(u32), doe_completion_cb); if (ret) { writel(1 << DOE_STATUS_ERROR, drv->base + PF_DOE_STATUS_REG(func_no, cap_offset)); kfree(request); } return IRQ_HANDLED; }/* ========== Abort Handler (triggered on ABORT bit from root complex) ========== */
static
irqreturn_tdoe_abort_handler(int irq, void *priv){ struct epc_driver *drv = priv; u16 cap_offset = extract_cap_offset_from_irq(irq); u8 func_no = extract_func_from_irq(irq); mutex_lock(&lock); /* call abort API only if BUSY bit set (pci_ep_doe_process_request() called) */ if (readl(drv->base + PF_DOE_STATUS_REG(func_no, cap_offset)) & DOE_STATUS_BUSY) pci_ep_doe_abort(drv->epc, func_no, cap_offset); mutex_unlock(&lock); /* Discard Write memory contents */ writel(DOE_WR_MEMORY_CTRL_DISCARD, drv->base + PF_DOE_WR_MEMORY_CTRL_REG(func_no, cap_offset)); /* Clear status bits */ writel((0 << DOE_STATUS_ERROR) | (0 << DOE_STATUS_READY), drv->base + PF_DOE_STATUS_REG(func_no, cap_offset)); return IRQ_HANDLED; }====================================================================================
Aksh Garg (4):
PCI/DOE: Move common definitions to the header file
PCI: endpoint: Add DOE mailbox support for endpoint functions
PCI: endpoint: Add support for DOE initialization and setup in EPC
core
Documentation: PCI: Add documentation for DOE endpoint supportDocumentation/PCI/endpoint/index.rst | 1 + .../PCI/endpoint/pci-endpoint-doe.rst | 333 ++++++++++ drivers/pci/doe.c | 11 - drivers/pci/endpoint/Kconfig | 14 + drivers/pci/endpoint/Makefile | 1 + drivers/pci/endpoint/pci-ep-doe.c | 594 ++++++++++++++++++ drivers/pci/endpoint/pci-epc-core.c | 104 +++ drivers/pci/pci.h | 48 ++ include/linux/pci-doe.h | 8 + include/linux/pci-epc.h | 9 + 10 files changed, 1112 insertions(+), 11 deletions(-) create mode 100644 Documentation/PCI/endpoint/pci-endpoint-doe.rst create mode 100644 drivers/pci/endpoint/pci-ep-doe.c--
2.34.1
Quoted 9 lines · Frank Li
Quoted 8 lines · Aksh Garg
This patch series introduces the framework for supporting the Data Object Exchange (DOE) feature for PCIe endpoint devices. Please refer to the documentation added in patch 4 for details on the feature and implementation architecture.
The implementation provides a common framework for all PCIe endpoint controllers, not specific to any particular SoC vendor.
Hi Frank,
Quoted 4 lines
General question, does DOE generate irq when received msg for HOST? I have not related irq handle code.
The EPC hardware is expected to raise IRQ when it receives DOE signals from the host. The example IRQ code handler have been provided below.
When the response DOE is ready for the host, the signal_task_complete() in pci-ep-doe.c invokes completion callback function, through which the EPC driver handles to send the response back to the host using the DOE mailbox.
Quoted 3 lines
Any program to test it? such as
pci_endpoint_test, need at least one real user to use it.
Currently there is no EPC driver upstream which support DOE yet. However, you can refer to the conversation at [1] where the plan to add user for this framework has been discussed.
Regards,
Aksh Garg
Quoted 196 lines
Frank
Quoted 194 lines
The changes since v1 are documented in the respective patch descriptions.
v4: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1 (RFC): https://lore.kernel.org/all/[email protected]/Below is a code demonstration showing the integration of DOE-EP APIs with EPC drivers.
Note: The provided code is just to show how an EPC driver is expected to
utilize thepci_ep_doe_process_request()andpci_ep_doe_abort()APIs, and might not cover all the corner cases. The below implementation also expects the EPC hardware to have some memory buffer to store the data from(for)write_mailbox(read_mailbox)DOE capability registers.============================================================================
/* ========== DOE Completion Callback (invoked by DOE-EP core) ========== */
static void doe_completion_cb(struct
pci_epc*epc, u8func_no, u16cap_offset,
int status, u16 vendor, u8 type, void*response_pl,size_tresponse_pl_sz){ struct epc_driver *drv = epc_get_drvdata(epc); u32 *response = (u32 *)response_pl; u32 header1, header2; int payload_dw, i; if (readl(drv->base + PF_DOE_CTRL_REG(func_no, cap_offset)) & DOE_CTRL_ABORT) { /* Aborted: do not send response */ goto free; } if (status < 0) { /* Error: set ERROR bit in DOE Status register */ writel(1 << DOE_STATUS_ERROR, drv->base + PF_DOE_STATUS_REG(func_no, cap_offset)); goto free; } /* Success: write DOE headers first, then response to the read memory */ /* Header 1: Vendor ID (bits 15:0) | Type (bits 23:16) */ header1 = (type << 16) | vendor; writel(header1, drv->base + PF_DOE_RD_MEMORY_WR_REG(func_no, cap_offset)); /* Header 2: Length in DW (including 2 DW of headers + payload) */ payload_dw = DIV_ROUND_UP(response_pl_sz, sizeof(u32)); header2 = 2 + payload_dw; /* 2 header DWs + payload */ writel(header2, drv->base + PF_DOE_RD_MEMORY_WR_REG(func_no, cap_offset)); /* Set READY bit to signal response ready */ writel(1 << DOE_STATUS_READY, drv->base + PF_DOE_STATUS_REG(func_no, cap_offset)); /* Write response payload DWORDs to Read memory */ for (i = 0; i < payload_dw; i++) writel(response[i], drv->base + PF_DOE_RD_MEMORY_WR_REG(func_no, cap_offset)); /* Wait for the memory to empty before clearing the READY bit */ while (!RD_MEMORY_EMPTY()) {/* wait */} writel(0 << DOE_STATUS_READY, drv->base + PF_DOE_STATUS_REG(func_no, cap_offset));free:
/* unset BUSY bit */ writel(0 << DOE_STATUS_BUSY, drv->base + PF_DOE_STATUS_REG(func_no, cap_offset)); kfree(response_pl); }/* ========== DOE Interrupt Handler (triggered on GO bit from root complex) ========== */
static
irqreturn_tdoe_interrupt_handler(int irq, void *priv){ struct epc_driver *drv = priv; u16 cap_offset = extract_cap_offset_from_irq(irq); u8 func_no = extract_func_from_irq(irq); u32 header1, header2, length_dw, *request; u16 vendor; u8 type; int i, ret; /* Read first header DWORD: Vendor ID (bits 15:0) | Type (bits 23:16) */ header1 = readl(drv->base + PF_DOE_WR_MEMORY_RD_REG(func_no, cap_offset)); vendor = header1 & 0xFFFF; type = (header1 >> 16) & 0xFF; /* Read second header DWORD: Length in DW (includes 2 DW of headers) */ header2 = readl(drv->base + PF_DOE_WR_MEMORY_RD_REG(func_no, cap_offset)); length_dw = header2 & 0x3FFFF; /* Bits 17:0 */ if (!length_dw) length_dw = PCI_DOE_MAX_LENGTH; length_dw -= 2; /* Subtract 2 DW of headers to get payload length */ /* Allocate buffer for complete request (headers + payload) */ request = kzalloc(length_dw * sizeof(u32), GFP_ATOMIC); if (!request) { writel(1 << DOE_STATUS_ERROR, drv->base + PF_DOE_STATUS_REG(func_no, cap_offset)); return IRQ_HANDLED; } /* Read remaining payload DWORDs from Write memory */ for (i = 0; i < length_dw; i++) { while (WR_MEMORY_EMPTY()) { /* wait */ } request[i] = readl(drv->base + PF_DOE_WR_MEMORY_RD_REG(func_no, cap_offset)); } mutex_lock(&lock); /* Check the ABORT bit, if set then return */ if (readl(drv->base + PF_DOE_CTRL_REG(func_no, cap_offset)) & DOE_CTRL_ABORT) { kfree(request); mutex_unlock(&lock); return IRQ_HANDLED; } /* Set BUSY bit */ writel(1 << DOE_STATUS_BUSY, drv->base + PF_DOE_STATUS_REG(func_no, cap_offset)); mutex_unlock(&lock); /* Hand off to DOE-EP core for asynchronous processing */ ret = pci_ep_doe_process_request(drv->epc, func_no, cap_offset, vendor, type, (void *)request, length_dw * sizeof(u32), doe_completion_cb); if (ret) { writel(1 << DOE_STATUS_ERROR, drv->base + PF_DOE_STATUS_REG(func_no, cap_offset)); kfree(request); } return IRQ_HANDLED; }/* ========== Abort Handler (triggered on ABORT bit from root complex) ========== */
static
irqreturn_tdoe_abort_handler(int irq, void *priv){ struct epc_driver *drv = priv; u16 cap_offset = extract_cap_offset_from_irq(irq); u8 func_no = extract_func_from_irq(irq); mutex_lock(&lock); /* call abort API only if BUSY bit set (pci_ep_doe_process_request() called) */ if (readl(drv->base + PF_DOE_STATUS_REG(func_no, cap_offset)) & DOE_STATUS_BUSY) pci_ep_doe_abort(drv->epc, func_no, cap_offset); mutex_unlock(&lock); /* Discard Write memory contents */ writel(DOE_WR_MEMORY_CTRL_DISCARD, drv->base + PF_DOE_WR_MEMORY_CTRL_REG(func_no, cap_offset)); /* Clear status bits */ writel((0 << DOE_STATUS_ERROR) | (0 << DOE_STATUS_READY), drv->base + PF_DOE_STATUS_REG(func_no, cap_offset)); return IRQ_HANDLED; }====================================================================================
Aksh Garg (4):
PCI/DOE: Move common definitions to the header file
PCI: endpoint: Add DOE mailbox support for endpoint functions
PCI: endpoint: Add support for DOE initialization and setup in EPC
core
Documentation: PCI: Add documentation for DOE endpoint supportDocumentation/PCI/endpoint/index.rst | 1 + .../PCI/endpoint/pci-endpoint-doe.rst | 333 ++++++++++ drivers/pci/doe.c | 11 - drivers/pci/endpoint/Kconfig | 14 + drivers/pci/endpoint/Makefile | 1 + drivers/pci/endpoint/pci-ep-doe.c | 594 ++++++++++++++++++ drivers/pci/endpoint/pci-epc-core.c | 104 +++ drivers/pci/pci.h | 48 ++ include/linux/pci-doe.h | 8 + include/linux/pci-epc.h | 9 + 10 files changed, 1112 insertions(+), 11 deletions(-)create mode 100644
Documentation/PCI/endpoint/pci-endpoint-doe.rstcreate mode 100644drivers/pci/endpoint/pci-ep-doe.c--
2.34.1