Re: [RFC PATCH 2/7] coco/tdx-host: Establish an SPDM session with the device

From: Edgecombe, Rick P

Date: Mon Nov 23 2026 - 18:23:22 EST


On Mon, 2026-11-16 at 03:10 -0800, Chao Gao wrote:
> Setting up PCIe link encryption requires a secure channel to the device
> to exchange keys. This channel is an SPDM session over the device's PCI
> DOE mailbox.

Probably SPDM and DOE mailbox need some 10,000ft summary.

>
> The TDX module cannot access the DOE mailbox but needs to exchange SPDM
> messages with the device, so the host relays SPDM messages between the
> two.
>
> TDH.SPDM.CONNECT establishes the session and TDH.SPDM.DISCONNECT tears
> it down. Both may need several message exchanges with the device.
> Whenever the TDX module has a message for the device, the SEAMCALL
> returns with a dedicated status code. The host sends the message to the
> device and calls the SEAMCALL again with the device's response, until
> the SEAMCALL completes.

SEAMCALL completes? Could mean the full extension operation or a single seamcall
execution.

>
> Call both SEAMCALLs in such a loop. The DOE transfer itself is a stub
> for now.

I'd put something higher up explaining this is laying the ground work, rather
than announcing the gaps at the end.

>
> Pass the messages through two pages per device, one for each direction.
> One pair is enough because PCI/TSM serializes operations on a device.

This probably needs more explanation.

>
> The TDX module limits how many TDH.SPDM.CONNECT and TDH.SPDM.DISCONNECT
> calls can run at the same time. The limit is implementation-specific,
> and a call over the limit fails with TDX_OPERAND_BUSY. For simplicity,
> serialize them across all devices with a mutex.
>
> Based on work by Yilun.
>
> Signed-off-by: Chao Gao <chao.gao@xxxxxxxxx>
> ---
> arch/x86/include/asm/shared/tdx_errno.h | 1 +
> drivers/virt/coco/tdx-host/main.c | 131 +++++++++++++++++++++++-
> 2 files changed, 130 insertions(+), 2 deletions(-)
>
> diff --git a/arch/x86/include/asm/shared/tdx_errno.h b/arch/x86/include/asm/shared/tdx_errno.h
> index ee411b360e20..6581cec7e097 100644
> --- a/arch/x86/include/asm/shared/tdx_errno.h
> +++ b/arch/x86/include/asm/shared/tdx_errno.h
> @@ -27,6 +27,7 @@
> #define TDX_EPT_WALK_FAILED 0xC0000B0000000000ULL
> #define TDX_EPT_ENTRY_STATE_INCORRECT 0xC0000B0D00000000ULL
> #define TDX_METADATA_FIELD_NOT_READABLE 0xC0000C0200000000ULL
> +#define TDX_SPDM_REQUEST 0x00000F5700000000ULL
>
> /*
> * TDX module operand ID, appears in 31:0 part of error code as
> diff --git a/drivers/virt/coco/tdx-host/main.c b/drivers/virt/coco/tdx-host/main.c
> index 919e77ae2326..32755d70dac5 100644
> --- a/drivers/virt/coco/tdx-host/main.c
> +++ b/drivers/virt/coco/tdx-host/main.c
> @@ -44,11 +44,26 @@ static inline u32 tdisp_func_id(struct pci_dev *pdev)
> return func_id;
> }
>
> +#define SPDM_CAP_HBEAT BIT(13)
> +#define SPDM_CAP_KEY_UPD BIT(14)
> +
> +struct spdm_config_info_t {
> + u32 vmm_spdm_cap;
> + u8 spdm_session_policy;
> + u8 certificate_slot_mask;
> + u8 raw_bitstream_requested;
> + u8 reserved[4089];
> +} __packed;

This looks like more SPDM specific stuff in TDX. Do really know other vendors
need it? We should at least group it in a header such that it is clear which
things are SPDM spec and which are TDX.

> +
> +static_assert(sizeof(struct spdm_config_info_t) == 4096);
> +
> struct tdx_tsm_link {
> struct pci_tsm_pf0 pci;
> u32 func_id;
>
> u64 spdm_id;
> + struct page *in_msg;
> + struct page *out_msg;

SNP and TDX both have the DOE mailbox shuffling done by the host, right? So why
doesn't TSM allocate and provide these buffers?

Oh looking at the code I'm now very confused. The request for SNP is in private
memory? The SNP commit has very little info to explain what is going on there.

> unsigned int spdm_mt_nr_pages;
> void *spdm_mt;
> };
> @@ -58,9 +73,89 @@ static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm)
> return container_of(tsm, struct tdx_tsm_link, pci.base_tsm);
> }
>
> +static int tdx_spdm_msg_exchange(struct tdx_tsm_link *tlink)
> +{
> + /* TODO: implement the PCI DOE transfer */
> + return 0;
> +}
> +
> +/*
> + * When serving SEAMCALLs, the TDX module extensions work like service thread
> + * pools. The number of service threads is limited per service type (or
> + * "per extension" in terms of TDX). When the host issues a request via
> + * SEAMCALL but the TDX module can't find an available thread, it returns
> + * TDX_OPERAND_BUSY. Currently the thread number limit for the TDISP extension
> + * is 1. So a mutex could work to prevent SEAMCALL busy returns.
> + */
> +static DEFINE_MUTEX(tdx_spdm_lock);

Yea I think this is a good solution to start. We can support enumerating
multiple sessions in follow on work.

> +
> +static int tdx_spdm_xfer_handler(struct tdx_tsm_link *tlink, u64 sret,
> + u64 out_msg_sz)
> +{
> + int ret;
> +
> + if (sret == TDX_SPDM_REQUEST) {
> + ret = tdx_spdm_msg_exchange(tlink);
> + if (ret < 0)
> + return ret;
> +
> + return -EAGAIN;
> + }
> +
> + if (sret == TDX_SUCCESS)
> + return 0;
> +
> + return -EIO;
> +}
> +
> +static int tdx_spdm_session_connect(struct tdx_tsm_link *tlink)
> +{
> + struct spdm_config_info_t *spdm_conf;
> + u64 sret, out_msg_sz;
> + int ret;
> +
> + /* create & fill SPDM configuration buffer - a single page */
> + spdm_conf = kzalloc_obj(*spdm_conf);
> + if (!spdm_conf)
> + return -ENOMEM;
> +
> + spdm_conf->certificate_slot_mask = 0xff;

Can we not have the magic number? I guess it means any?

> +
> + do {
> + mutex_lock(&tdx_spdm_lock);
> + sret = tdh_spdm_connect(tlink->spdm_id,
> + virt_to_page(spdm_conf),
> + tlink->in_msg, tlink->out_msg,
> + NULL, &out_msg_sz);
> + mutex_unlock(&tdx_spdm_lock);
> + ret = tdx_spdm_xfer_handler(tlink, sret, out_msg_sz);
> + } while (ret == -EAGAIN);
> +
> + kfree(spdm_conf);
> +
> + return ret;
> +}
> +
> +static void tdx_spdm_session_disconnect(struct tdx_tsm_link *tlink)
> +{
> + u64 sret, out_msg_sz;
> + int ret;
> +
> + do {
> + mutex_lock(&tdx_spdm_lock);

The fact that TSM protects us from the buffers getting clobbered by another
request, but we still need the mutex only around the call is a bit hidden. I'd
think it at least needs a comment. But since the tdx_spdm_lock is really a TDX
module specific limitation, why not put it inside the SEAMCALL wrappers? It
seems more related to core TDX module setup type things.

> + sret = tdh_spdm_disconnect(tlink->spdm_id, tlink->in_msg,
> + tlink->out_msg, &out_msg_sz);
> + mutex_unlock(&tdx_spdm_lock);
> + ret = tdx_spdm_xfer_handler(tlink, sret, out_msg_sz);
> + } while (ret == -EAGAIN);
> +
> + WARN_ON(ret);
> +}
> +
> static int tdx_spdm_create(struct tdx_tsm_link *tlink)
> {
> const struct tdx_sys_info *tdx_sysinfo = tdx_get_sysinfo();
> + struct page *in_msg_page, *out_msg_page;
> struct tdx_hpa_list_info info;
> unsigned int nr_pages;
> u64 spdm_id, sret;
> @@ -70,12 +165,21 @@ static int tdx_spdm_create(struct tdx_tsm_link *tlink)
> if (!tdx_sysinfo)
> return -ENXIO;
>
> + ret = -ENOMEM;
> + in_msg_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
> + if (!in_msg_page)
> + return ret;
> +
> + out_msg_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
> + if (!out_msg_page)
> + goto out_free_in_msg;
> +

Maybe dumb question, but why do we need two buffers for this? After we post the
request can't we just read it back the response to the same buffer? It doesn't
seem DOE code is setup for that though.

> nr_pages = tdx_sysinfo->tdx_connect.spdm_mt_page_count;
>
> spdm_mt = alloc_pages_exact(nr_pages * PAGE_SIZE,
> GFP_KERNEL | __GFP_ZERO);
> if (!spdm_mt)
> - return -ENOMEM;
> + goto out_free_out_msg;
>
> ret = tdx_hpa_list_info_setup(&info, spdm_mt, nr_pages);
> if (ret)
> @@ -92,11 +196,17 @@ static int tdx_spdm_create(struct tdx_tsm_link *tlink)
>
> tlink->spdm_id = spdm_id;
> tlink->spdm_mt = spdm_mt;
> + tlink->in_msg = in_msg_page;
> + tlink->out_msg = out_msg_page;
> tlink->spdm_mt_nr_pages = nr_pages;
> return 0;
>
> out_free_spdm_mt:
> free_pages_exact(spdm_mt, nr_pages * PAGE_SIZE);
> +out_free_out_msg:
> + __free_page(out_msg_page);
> +out_free_in_msg:
> + __free_page(in_msg_page);
> return ret;
> }
>
> @@ -112,16 +222,33 @@ static void tdx_spdm_delete(struct tdx_tsm_link *tlink)
> return;
> }
>
> + __free_page(tlink->in_msg);
> + __free_page(tlink->out_msg);
> free_pages_exact(tlink->spdm_mt, tlink->spdm_mt_nr_pages * PAGE_SIZE);
> }
>
> static int tdx_spdm_session_setup(struct tdx_tsm_link *tlink)
> {
> - return tdx_spdm_create(tlink);
> + int ret;
> +
> + ret = tdx_spdm_create(tlink);
> + if (ret)
> + return ret;
> +
> + ret = tdx_spdm_session_connect(tlink);
> + if (ret)
> + goto out_spdm_delete;
> +
> + return 0;
> +
> +out_spdm_delete:
> + tdx_spdm_delete(tlink);

We might some basics explanation of the SPDM operations are. create, disconnect,
delete, etc. What about adding some comments across the patches that make it
clear when and where each needs to be called?

> + return ret;
> }
>
> static void tdx_spdm_session_teardown(struct tdx_tsm_link *tlink)
> {
> + tdx_spdm_session_disconnect(tlink);
> tdx_spdm_delete(tlink);
> }
>