[PATCH 23/40] nitro_enclaves: Add the device add command

From: Alexander Graf

Date: Wed Nov 11 2026 - 18:07:18 EST


In preparation to support Nitro Sandboxes, which allow the parent to
back arbitrary virtio devices, add the command definition for adding
devices to a slot.

The device add command specifies the virtio device shape (type, nr VQs,
features, config space), but also allows to specify backing ranges for
DMB or SHM regions.

The full response with the device information is part of the info page
at the returned offset.

Signed-off-by: Alexander Graf <graf@xxxxxxxxxx>
---
drivers/virt/nitro_enclaves/ne_pci_dev.h | 88 ++++++++++++++++++++++++
1 file changed, 88 insertions(+)

diff --git a/drivers/virt/nitro_enclaves/ne_pci_dev.h b/drivers/virt/nitro_enclaves/ne_pci_dev.h
index f5cb24312d442..3445318b84672 100644
--- a/drivers/virt/nitro_enclaves/ne_pci_dev.h
+++ b/drivers/virt/nitro_enclaves/ne_pci_dev.h
@@ -161,6 +161,7 @@
* @SLOT_ADD_BULK_VCPUS: Add a number of vCPUs, not providing CPU ids.
* @SLOT_ADD_INFO_PAGE: Give a slot the info page, a memory region the
* device writes.
+ * @SLOT_ADD_DEVICE: Give a slot a device the enclave will drive.
* @SLOT_POOL_DONATE: Donate one page of parent memory to the slot
* pool.
* @SLOT_POOL_SHUTDOWN: Give the whole slot pool back.
@@ -186,6 +187,7 @@ enum ne_pci_dev_cmd_type {
SLOT_ADD_BULK_VCPUS = 11,
/* 12 is a command this driver does not implement. */
SLOT_ADD_INFO_PAGE = 13,
+ SLOT_ADD_DEVICE = 14,
SLOT_POOL_DONATE = 16,
SLOT_POOL_SHUTDOWN = 17,
SLOT_POOL_BASE_DONATE = 25,
@@ -333,6 +335,73 @@ struct slot_add_info_page_req {
u64 info_size;
};

+/**
+ * NE_MAX_VQS_PER_DEVICE - Highest number of virtqueues one device may have.
+ */
+#define NE_MAX_VQS_PER_DEVICE (64)
+
+/**
+ * NE_MAX_BACKING_RANGES - Highest number of memory ranges one device may be
+ * backed by.
+ */
+#define NE_MAX_BACKING_RANGES (4)
+
+/**
+ * struct ne_backing_range - One run of enclave memory a device works out of.
+ * @phys_addr: Physical address of the run, in the parent's address space.
+ * @size: Size, in bytes, of the run.
+ */
+struct ne_backing_range {
+ u64 phys_addr;
+ u64 size;
+};
+
+/**
+ * struct ne_vq_wiring - How the enclave reaches one virtqueue of a device.
+ * @msix_vector: Interrupt vector the device raises for the queue.
+ * @notify_index: Index the enclave writes to notify the device.
+ * @msix_entry: Entry in the enclave's interrupt table holding
+ * @msix_vector.
+ * @doorbell_offset: Offset of the queue's doorbell in the device's
+ * doorbell window.
+ */
+struct ne_vq_wiring {
+ u16 msix_vector;
+ u16 notify_index;
+ u16 msix_entry;
+ u16 doorbell_offset;
+};
+
+static_assert(sizeof(struct ne_vq_wiring) == 8);
+
+/**
+ * struct slot_add_device_req - SLOT_ADD_DEVICE request.
+ * @slot_uid: Slot unique id mapped to the slot to give the
+ * device to.
+ * @device_type: Type of device to give the slot.
+ * @num_vqs: Number of virtqueues the device has, at most
+ * NE_MAX_VQS_PER_DEVICE.
+ * @device_features: Feature bits the parent offers the enclave for
+ * the device.
+ * @config_size: Number of bytes of @device_config the device
+ * reads.
+ * @num_backing_ranges: Number of entries of @backing_ranges the
+ * device reads, at most NE_MAX_BACKING_RANGES.
+ * @device_config: Type-specific configuration of the device.
+ * @backing_ranges: Runs of enclave memory the device works out of.
+ */
+struct slot_add_device_req {
+ u64 slot_uid;
+ u32 device_type;
+ u32 num_vqs;
+ u8 device_features[64];
+ u32 config_size;
+ u32 num_backing_ranges;
+ u8 device_config[64];
+ struct ne_backing_range backing_ranges[NE_MAX_BACKING_RANGES];
+} __packed;
+static_assert(sizeof(struct slot_add_device_req) <= NE_SEND_DATA_SIZE);
+
/**
* struct slot_pool_donate_req - SLOT_POOL_DONATE and SLOT_POOL_BASE_DONATE
* request.
@@ -408,6 +477,15 @@ struct slot_pool_mem_donate_req {
* slot out of.
* @limits.reserved14: SLOT_POOL_GET_LIMITS. Bytes the device writes as
* zero.
+ * @add_device: Fields SLOT_ADD_DEVICE fills.
+ * @add_device.device_uid: SLOT_ADD_DEVICE. Unique id of the new device.
+ * @add_device.num_vqs: SLOT_ADD_DEVICE. Number of entries of wiring
+ * the device wrote to the info page.
+ * @add_device.reserved12: SLOT_ADD_DEVICE. Bytes owned by other
+ * commands.
+ * @add_device.vq_wiring_offset: SLOT_ADD_DEVICE. Offset into the info
+ * page at which @num_vqs entries of struct
+ * ne_vq_wiring start.
*/
struct ne_pci_dev_cmd_reply {
s32 rc;
@@ -443,6 +521,12 @@ struct ne_pci_dev_cmd_reply {
u16 slots_per_donation;
u16 reserved14;
} limits;
+ struct {
+ u64 device_uid;
+ u32 num_vqs;
+ u32 reserved12;
+ u64 vq_wiring_offset;
+ } add_device;
};
};

@@ -452,6 +536,10 @@ static_assert(offsetof(struct ne_pci_dev_cmd_reply, start.enclave_cid) == 16);
static_assert(offsetof(struct ne_pci_dev_cmd_reply, alloc.slot_uid) == 8);
static_assert(offsetof(struct ne_pci_dev_cmd_reply, alloc.mem_regions) == 32);
static_assert(offsetof(struct ne_pci_dev_cmd_reply, info.slot_uid) == 8);
+static_assert(offsetof(struct ne_pci_dev_cmd_reply, add_device.device_uid) == 8);
+static_assert(offsetof(struct ne_pci_dev_cmd_reply, add_device.num_vqs) == 16);
+static_assert(offsetof(struct ne_pci_dev_cmd_reply,
+ add_device.vq_wiring_offset) == 24);
static_assert(offsetof(struct ne_pci_dev_cmd_reply, info.enclave_cid) == 16);
static_assert(offsetof(struct ne_pci_dev_cmd_reply, info.mem_regions) == 32);
static_assert(offsetof(struct ne_pci_dev_cmd_reply, info.mem_size) == 40);
--
2.47.1