From nobody Fri Apr 3 17:56:52 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 827C03CD8C3 for ; Tue, 24 Mar 2026 07:18:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774336709; cv=none; b=Fe9FRvdwz+WJe6+ycuVAUcQFC3rhQvZ6UxjxMYCE+M1K6zeJhVd4bi/UTzon+BCnhDC3Upy84m0Il3QThBtd+8rPi0gXbrv2GsjXbKd36IKkTgcZfMQj8TmAFx+iPul3QQ9D0TP8s6b2Ici1w4GsNNCmGB0wPljw2r8bVWRUSy4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774336709; c=relaxed/simple; bh=+sKk+LR9zR9ZKqE6bhd3PLSWqidIdSpmgScoe+3cKZg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tdM+2mR2+p2EdqUv+8jrQhNCmwUPFrk3/7fTmXIVhjArOev4R4gew1aSmp3mQhOFArPdcB+NPBfnWQgKFL2jRW2g6QUa96NRj2jWdd0zWo2SC2WJ9SGNFH/8c7h73yE84tatU8zOqO7CIw4PUl+joB1+edw0qQKo5IpyUni82xE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=QgIxm0j+; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=KVPivqhP; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="QgIxm0j+"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="KVPivqhP" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1774336699; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=lNnPw5foALWPQjwieo1e/lEK9v8tBtgy/PYi8XddQNs=; b=QgIxm0j+2hhtRwZO5MbkqiW9ASk+yQ1oxNRRuCOwjbIkfxh5JAtFQ9brGJsHnBjJujZi9n wUUkgUMoXAsdMSW6/qJUkZX5KygxVo5SpDCkPqzisgpOPua65NAYrW+wGbZ9tTnU+exOiF FETlKAOq95JeqwXt5j/Q5HqX5cKxYrA= Received: from mail-pg1-f199.google.com (mail-pg1-f199.google.com [209.85.215.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-101-ednZSkYRPC2-3_M66_s5YA-1; Tue, 24 Mar 2026 03:18:18 -0400 X-MC-Unique: ednZSkYRPC2-3_M66_s5YA-1 X-Mimecast-MFC-AGG-ID: ednZSkYRPC2-3_M66_s5YA_1774336697 Received: by mail-pg1-f199.google.com with SMTP id 41be03b00d2f7-c741b950511so2264082a12.0 for ; Tue, 24 Mar 2026 00:18:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1774336697; x=1774941497; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=lNnPw5foALWPQjwieo1e/lEK9v8tBtgy/PYi8XddQNs=; b=KVPivqhP2VstkUeZ41TZ0odHldRhv/mbRqjFbqC0MfjO7f5PKWKz6YHuZ3J79uPymB rJQht4FXa+rmDU9evu1TM1JQT7CQ6uG5+cwA3jIeDlh9yiXuXw+AeFUYEgcUWsvjzTYd dpTv/Cj93QIGjhqSadSvIgBxUHGu33xXr6YfcEYYv0SdxT6c8BQ/xsHCQxkWP/Pmt1c0 K2dmZYZoz/zub5PaetGtLAXCa+MdiodTbUy704aTNFVcRXNJFm594qBLsApRoT3h515J bwSz7QrMxw8rGUV6NyavkEKdLFa3a1DcUZzs9FMF54hMQjVjrdgoiGdbpIHZsHREBCmk jS+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774336697; x=1774941497; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=lNnPw5foALWPQjwieo1e/lEK9v8tBtgy/PYi8XddQNs=; b=KL2QDDVsWXT0DQHWsZgHmafVVjoWLmuHhNrFSBOdUvDkJcehOUHoFWK4PhUSOttU+0 iIz2mmGSKCbODJv6AxQgQz6yD098sLOULDYdUlni7EpiWi6uABnK7ULk2Zz3SRCp8fuO 5aY1ujGTISyL4K+YNr84R9ZY7KfHjC4QoLwivw2rd4wiFPloe14gWm5s5EtOLKi+XIac INl0GppAGNXy5gJt21pDIUHJKuiH2upK0geLMplvIBkCFLE/EhdbAmblDkXDDgbJgxTa DPr3rLnZDxl7EIrOHiXOTeOjjRN548PLYaiXEEAVc/F5DGuCERgVF17l82YMHt6LpS2w sApA== X-Gm-Message-State: AOJu0Yx3okRtlA4oSjaTN5InWkQGzgst3pK1lyR3UN0AJq0vHf7selDt rCOWsGFbPX4qWAb1/yYqPQknDp6EX/0QWmSs2oQMl0zlqAgTaouRvZYBqtkn/ejDQpQ3IlWWlEQ f//isQZE5jSufiFHk1C6i+EYNMJF0hqe1PsIBqAS7ahdNXUxBb6DQDTU1xJYygZg58B34sCvzEL Yeo5Lzz/hZXkLdmwmHeAl3SBfu7R31326NPYPumqOpELPh7b1f X-Gm-Gg: ATEYQzy/Kn0vOqyRZ26fq3fFJfwQvpy+XSnurbNAlDA0ShFO6oehN1kpHOWZZU8IIA/ SWmWQqUEFowlHcR2cL3FLlwpGX3eyDHztiG7L9QZeP3XrcYWh/oDfmuGm7Wf7s2FLIWiYKBysy7 Ecw2/WA5lQdIESs3zPoh+ljpWlJmgb1a51xmr4Z2ioy/RPQYfPujtlP21zoIufbgcUPggni+cSq gSxqbOZYSwiSrOEUBoPyz2duusiiF2ti0Abt63chIbh9KLqbziKDS0N58TZrfMIp3eXqk4QpbaT Q0ZHr1tO0YX6vFazpMhTthWFY+86Nd6UwSDdl+pBPXvnoof9VOJHtjt2xb+lR3XHSyGiwENzRyA aF7/kPw3I0bkHxcrnQGCKtn+FzjxL3xgtU+m594m/cHAaCchodsMn46PeuodAKA== X-Received: by 2002:a05:6a00:13a1:b0:82a:6852:559e with SMTP id d2e1a72fcca58-82a8c21270amr12493389b3a.12.1774336696737; Tue, 24 Mar 2026 00:18:16 -0700 (PDT) X-Received: by 2002:a05:6a00:13a1:b0:82a:6852:559e with SMTP id d2e1a72fcca58-82a8c21270amr12493361b3a.12.1774336696100; Tue, 24 Mar 2026 00:18:16 -0700 (PDT) Received: from fedora.armenon-thinkpadp16vgen1.bengluru.csb ([49.36.106.26]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-82b0409c681sm13597766b3a.37.2026.03.24.00.18.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Mar 2026 00:18:15 -0700 (PDT) From: Arun Menon To: linux-kernel@vger.kernel.org Cc: linux-integrity@vger.kernel.org, Jason Gunthorpe , Jarkko Sakkinen , Peter Huewe , Arun Menon Subject: [RFC 3/4] tpm_crb: Implement command and response chunking logic Date: Tue, 24 Mar 2026 12:48:02 +0530 Message-ID: <20260324071803.324774-4-armenon@redhat.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260324071803.324774-1-armenon@redhat.com> References: <20260324071803.324774-1-armenon@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add support for sending and receiving TPM command data in chunks when the payload exceeds the physical size of the hardware MMIO window. This introduces the following changes: - crb_map_io(): Checks the device interface capability to determine if chunking is supported, setting the chunking_supported flag. It also stores the hardware's maximum response buffer size in priv->rsp_size. - crb_send(): Iteratively writes command chunks to the fixed priv->cmd MMIO window. It signals the TPM backend to process intermediate chunks using CRB_START_NEXT_CHUNK, and signals the final chunk to begin execution using CRB_START_INVOKE. - crb_recv(): Parses the expected response size from the initial TPM header. It then iteratively reads chunks from the fixed priv->rsp MMIO window into the destination buffer, advancing the buffer offset until the complete response is retrieved. Signed-off-by: Arun Menon --- drivers/char/tpm/tpm_crb.c | 150 +++++++++++++++++++++++++++---------- 1 file changed, 109 insertions(+), 41 deletions(-) diff --git a/drivers/char/tpm/tpm_crb.c b/drivers/char/tpm/tpm_crb.c index 10128d078245c..fb63cc3737253 100644 --- a/drivers/char/tpm/tpm_crb.c +++ b/drivers/char/tpm/tpm_crb.c @@ -104,11 +104,13 @@ struct crb_priv { u8 __iomem *cmd; u8 __iomem *rsp; u32 cmd_size; + u32 rsp_size; u32 smc_func_id; u32 __iomem *pluton_start_addr; u32 __iomem *pluton_reply_addr; u8 ffa_flags; u8 ffa_attributes; + bool chunking_supported; }; =20 struct tpm2_crb_smc { @@ -368,38 +370,6 @@ static u8 crb_status(struct tpm_chip *chip) return sts; } =20 -static int crb_recv(struct tpm_chip *chip, u8 *buf, size_t count) -{ - struct crb_priv *priv =3D dev_get_drvdata(&chip->dev); - unsigned int expected; - - /* A sanity check that the upper layer wants to get at least the header - * as that is the minimum size for any TPM response. - */ - if (count < TPM_HEADER_SIZE) - return -EIO; - - /* If this bit is set, according to the spec, the TPM is in - * unrecoverable condition. - */ - if (ioread32(&priv->regs_t->ctrl_sts) & CRB_CTRL_STS_ERROR) - return -EIO; - - /* Read the first 8 bytes in order to get the length of the response. - * We read exactly a quad word in order to make sure that the remaining - * reads will be aligned. - */ - memcpy_fromio(buf, priv->rsp, 8); - - expected =3D be32_to_cpup((__be32 *)&buf[2]); - if (expected > count || expected < TPM_HEADER_SIZE) - return -EIO; - - memcpy_fromio(&buf[8], &priv->rsp[8], expected - 8); - - return expected; -} - static int crb_do_acpi_start(struct tpm_chip *chip) { union acpi_object *obj; @@ -474,6 +444,8 @@ static int crb_trigger_tpm(struct tpm_chip *chip, u32 s= tart_cmd) static int crb_send(struct tpm_chip *chip, u8 *buf, size_t bufsiz, size_t = len) { struct crb_priv *priv =3D dev_get_drvdata(&chip->dev); + size_t offset =3D 0; + size_t chunk_size; int rc =3D 0; =20 /* Zero the cancel register so that the next command will not get @@ -481,7 +453,7 @@ static int crb_send(struct tpm_chip *chip, u8 *buf, siz= e_t bufsiz, size_t len) */ iowrite32(0, &priv->regs_t->ctrl_cancel); =20 - if (len > priv->cmd_size) { + if (len > priv->cmd_size && !priv->chunking_supported) { dev_err(&chip->dev, "invalid command count value %zd %d\n", len, priv->cmd_size); return -E2BIG; @@ -491,16 +463,101 @@ static int crb_send(struct tpm_chip *chip, u8 *buf, = size_t bufsiz, size_t len) if (priv->sm =3D=3D ACPI_TPM2_COMMAND_BUFFER_WITH_PLUTON) __crb_cmd_ready(&chip->dev, priv, chip->locality); =20 - memcpy_toio(priv->cmd, buf, len); + while (offset < len) { + chunk_size =3D min_t(size_t, len - offset, priv->cmd_size); + + memcpy_toio(priv->cmd, buf + offset, chunk_size); + offset +=3D chunk_size; + + /* Make sure that cmd is populated before issuing start. */ + wmb(); + if (offset < len) { + rc =3D crb_trigger_tpm(chip, CRB_START_NEXT_CHUNK); + if (rc) + return rc; + if (!crb_wait_for_reg_32(&priv->regs_t->ctrl_start, + CRB_START_NEXT_CHUNK, 0, TPM2_TIMEOUT_C)) { + dev_err(&chip->dev, + "Timeout waiting for backend to consume chunk\n"); + return -ETIME; + } + } else { + rc =3D crb_trigger_tpm(chip, CRB_START_INVOKE); + if (rc) + return rc; + } + } + return crb_try_pluton_doorbell(priv, false); +} =20 - /* Make sure that cmd is populated before issuing start. */ - wmb(); +static int crb_recv(struct tpm_chip *chip, u8 *buf, size_t count) +{ + struct crb_priv *priv =3D dev_get_drvdata(&chip->dev); + unsigned int expected; + size_t offset =3D 0; + size_t chunk_size; + size_t first_read; + int rc; =20 - rc =3D crb_trigger_tpm(chip, CRB_START_INVOKE); - if (rc) - return rc; + /* A sanity check that the upper layer wants to get at least the header + * as that is the minimum size for any TPM response. + */ + if (count < TPM_HEADER_SIZE) + return -EIO; =20 - return crb_try_pluton_doorbell(priv, false); + /* If this bit is set, according to the spec, the TPM is in + * unrecoverable condition. + */ + if (ioread32(&priv->regs_t->ctrl_sts) & CRB_CTRL_STS_ERROR) + return -EIO; + + /* Read the first 8 bytes in order to get the length of the response. + * We read exactly a quad word in order to make sure that the remaining + * reads will be aligned. + */ + memcpy_fromio(buf, priv->rsp, 8); + + expected =3D be32_to_cpup((__be32 *)&buf[2]); + if (expected > count || expected < TPM_HEADER_SIZE) + return -EIO; + + /* + * Set chunk_size by comparing the size of the buffer that the upper laye= r has + * allocated (count) to the hardware tpm limit (priv->rsp_size). + * This is to prevent buffer overflow while writing to buf. + */ + chunk_size =3D min_t(size_t, count, priv->rsp_size); + + /* + * Compare the actual size of the response we found in the header to the = chunk_size. + */ + first_read =3D min_t(size_t, expected, chunk_size); + + memcpy_fromio(&buf[8], &priv->rsp[8], first_read - 8); + offset =3D first_read; + + while (offset < expected) { + if (!priv->chunking_supported) { + dev_err(&chip->dev, "Response larger than MMIO and chunking not support= ed\n"); + return -EIO; + } + + rc =3D crb_trigger_tpm(chip, CRB_START_NEXT_CHUNK); + if (rc) + return rc; + + if (!crb_wait_for_reg_32(&priv->regs_t->ctrl_start, + CRB_START_NEXT_CHUNK, 0, TPM2_TIMEOUT_C)) { + dev_err(&chip->dev, "Timeout waiting for backend response\n"); + return -ETIME; + } + + chunk_size =3D min_t(size_t, expected - offset, priv->rsp_size); + memcpy_fromio(buf + offset, priv->rsp, chunk_size); + offset +=3D chunk_size; + } + + return expected; } =20 static void crb_cancel(struct tpm_chip *chip) @@ -727,6 +784,15 @@ static int crb_map_io(struct acpi_device *device, stru= ct crb_priv *priv, goto out; } =20 + if (priv->regs_h) { + u32 intf_id =3D ioread32((u32 __iomem *)&priv->regs_h->intf_id); + + if (intf_id & CRB_INTF_CAP_CRB_CHUNK) { + priv->chunking_supported =3D true; + dev_info(dev, "CRB Chunking is supported by backend\n"); + } + } + memcpy_fromio(&__rsp_pa, &priv->regs_t->ctrl_rsp_pa, 8); rsp_pa =3D le64_to_cpu(__rsp_pa); rsp_size =3D ioread32(&priv->regs_t->ctrl_rsp_size); @@ -764,8 +830,10 @@ static int crb_map_io(struct acpi_device *device, stru= ct crb_priv *priv, priv->rsp =3D priv->cmd; =20 out: - if (!ret) + if (!ret) { priv->cmd_size =3D cmd_size; + priv->rsp_size =3D rsp_size; + } =20 __crb_go_idle(dev, priv, 0); =20 --=20 2.53.0