Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp180487rwr; Wed, 19 Apr 2023 20:13:54 -0700 (PDT) X-Google-Smtp-Source: AKy350bf/CLkJSY20Cmuayez2m2busNOcnuiYdBBjexLt5IVo/rWyI64FNvty3PpUdvI4CfPLdDS X-Received: by 2002:a05:6808:1288:b0:387:2e2e:7b2 with SMTP id a8-20020a056808128800b003872e2e07b2mr131195oiw.26.1681960434617; Wed, 19 Apr 2023 20:13:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681960434; cv=none; d=google.com; s=arc-20160816; b=V2HhKHVwV8VCFwNBLYyfVE3ODajhDa/BCTZABgH/OPhjzp5Nvq8unXso5Ag+oZ9GcC Dc5rbEyjPYqm8xhehk7qwIGmnMtLQD/X5XtHe/J0fw4KmteEN5ngSuSrytau/3DW/GKP GIfIt/qbiTA937FyhGprVGwF60PeZmymZ70HOUtg4+3thKosgD64PrgodOjq9JV55cAA 8dmoRpEXtAQBFHRkJAg49P3USAd9iFgUUM6Y3bRhv0ePUiuHSs1kAkiTMfTKRyN5slv6 QITA2iYHsV9+jSFDE7Fc1dbZ6XsN1T656Hft+duIq0HwpQeDk0AS6+Eo5yqPTlbozxGT Bnag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=WJkzeJvwCETUjWcwNy+63mU9+yCXou1XIKFoGxVIfQ0=; b=GfqpYr1lqUSepvs2WSw/buKJ9oCXA6ypdnB5+h9EpA0GD61XZTJJ0LdZpygahNJjx0 Z1cn4o5vtj+W8NUqqE7UV8E5FQ3BZY/bW0MBWK61v6+U0YEVR8KA6Dka5haSMC47rPbo oyBPZnr75FkmXt3T1WaUYL1EYD3XXlr8gJp4rwoTNv2R3+8KYXg5M3D1pIgXCPpF4eGm DMSKaaZJVA4qSPZ3u45m/myvRvYItFURGy1P5OKzp9B1NI43fH717X0B/FlyixAozEIa pItOToqao2hN+gC8L0WrCF7XxTXVMtwO2UbLP49c/xX5v2fdzCecY/aHSq7g8b4Jjlun 6SMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=UzlpK56K; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bk15-20020a0568081a0f00b0038e6b344b03si471685oib.193.2023.04.19.20.13.40; Wed, 19 Apr 2023 20:13:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=UzlpK56K; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233609AbjDTDMo (ORCPT + 99 others); Wed, 19 Apr 2023 23:12:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233174AbjDTDMk (ORCPT ); Wed, 19 Apr 2023 23:12:40 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BDF382103 for ; Wed, 19 Apr 2023 20:11:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681960318; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=WJkzeJvwCETUjWcwNy+63mU9+yCXou1XIKFoGxVIfQ0=; b=UzlpK56KozcxWUUR2VWbvemwuxkAcB4A5hqCA2PpI1Bsfv4Mjg344YOqjN4stmrqvKoVsq PHlVHUgOkiZdJXdF8d+wbQeep+78LVaIo8p2LLxqiIkRhAP/GNXtYVLLa/irVKOKBjWCaU dbnFddmUOBSsbR0q6C5EwEDsz/DeIa8= Received: from mail-pl1-f199.google.com (mail-pl1-f199.google.com [209.85.214.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-204-AH6bzaKFM2eK5ZznJ8_Mwg-1; Wed, 19 Apr 2023 23:11:57 -0400 X-MC-Unique: AH6bzaKFM2eK5ZznJ8_Mwg-1 Received: by mail-pl1-f199.google.com with SMTP id d9443c01a7336-1a6820f90c6so3849435ad.0 for ; Wed, 19 Apr 2023 20:11:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681960316; x=1684552316; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=WJkzeJvwCETUjWcwNy+63mU9+yCXou1XIKFoGxVIfQ0=; b=UsuLTPsjD29IGTmG/0T8lQ6Ykhv5FyI4ZYwPKx85xDY/WbfriC44yO/DZqQEttBkHO HDi2MiY0lKOr15MzSlQHmKTImm/7/KqlpaiKI4dYP7kWfbaWRbvrCU5GiYIPIwXXrNlY FZE4Nh3IYEAUYP+ZjeBE8RX3skNXROJP7pwbnh2gktLuEZzoE4+Ca8tqT3j1Slj+12mO snmcDZuxCjg8xf1SCO/qghws650U6ZSIz0HWRs70/ZhoLH8wVlLNvy+j7vC49AOPqwld UoHrGEfkFcxxnOxfVw1MzSbpFdc7TGTJzLzXjbCtv8KPV5GyhTJK8mgMA3g4entZKw6F FmiA== X-Gm-Message-State: AAQBX9fzqHo3yweCo1/1sGxusDC9sxA1lzXqhpB4BUh9+lJF/lDLRQK6 tO0IH1pa2WpCyukRMytsTtDp1m11CbD0qsIsPxusQ/UUhA04ezKH7yqO9cyPLRTZoY8h6dde0ms hGulBaAvrVwoakbM56inhaBKR X-Received: by 2002:a17:902:e0cc:b0:19e:dc0e:1269 with SMTP id e12-20020a170902e0cc00b0019edc0e1269mr76460pla.7.1681960316375; Wed, 19 Apr 2023 20:11:56 -0700 (PDT) X-Received: by 2002:a17:902:e0cc:b0:19e:dc0e:1269 with SMTP id e12-20020a170902e0cc00b0019edc0e1269mr76444pla.7.1681960316010; Wed, 19 Apr 2023 20:11:56 -0700 (PDT) Received: from localhost (ip98-179-76-75.ph.ph.cox.net. [98.179.76.75]) by smtp.gmail.com with ESMTPSA id a13-20020a1709027d8d00b001a647709860sm153930plm.157.2023.04.19.20.11.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 20:11:55 -0700 (PDT) Date: Wed, 19 Apr 2023 20:11:54 -0700 From: Jerry Snitselaar To: Krishna Yarlagadda Cc: robh+dt@kernel.org, broonie@kernel.org, peterhuewe@gmx.de, jgg@ziepe.ca, jarkko@kernel.org, krzysztof.kozlowski+dt@linaro.org, linux-spi@vger.kernel.org, linux-tegra@vger.kernel.org, linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, thierry.reding@gmail.com, jonathanh@nvidia.com, skomatineni@nvidia.com, ldewangan@nvidia.com Subject: Re: [Patch V9 2/3] tpm_tis-spi: Add hardware wait polling Message-ID: References: <20230325183409.7695-1-kyarlagadda@nvidia.com> <20230325183409.7695-3-kyarlagadda@nvidia.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Apr 19, 2023 at 07:32:40PM -0700, Jerry Snitselaar wrote: > On Sun, Mar 26, 2023 at 12:04:08AM +0530, Krishna Yarlagadda wrote: > > TPM devices may insert wait state on last clock cycle of ADDR phase. > > For SPI controllers that support full-duplex transfers, this can be > > detected using software by reading the MISO line. For SPI controllers > > that only support half-duplex transfers, such as the Tegra QSPI, it is > > not possible to detect the wait signal from software. The QSPI > > controller in Tegra234 and Tegra241 implement hardware detection of the > > wait signal which can be enabled in the controller for TPM devices. > > > > The current TPM TIS driver only supports software detection of the wait > > signal. To support SPI controllers that use hardware to detect the wait > > signal, add the function tpm_tis_spi_hw_flow_transfer() and move the > > existing code for software based detection into a function called > > tpm_tis_spi_sw_flow_transfer(). SPI controllers that only support > > half-duplex transfers will always call tpm_tis_spi_hw_flow_transfer() > > because they cannot support software based detection. The bit > > SPI_TPM_HW_FLOW is set to indicate to the SPI controller that hardware > > detection is required and it is the responsibility of the SPI controller > > driver to determine if this is supported or not. > > > > For hardware flow control, CMD-ADDR-DATA messages are combined into a > > single message where as for software flow control exiting method of > > CMD-ADDR in a message and DATA in another is followed. > > > > Signed-off-by: Krishna Yarlagadda > > --- > > drivers/char/tpm/tpm_tis_spi_main.c | 91 ++++++++++++++++++++++++++++- > > 1 file changed, 89 insertions(+), 2 deletions(-) > > > > diff --git a/drivers/char/tpm/tpm_tis_spi_main.c b/drivers/char/tpm/tpm_tis_spi_main.c > > index a0963a3e92bd..db9afd0b83da 100644 > > --- a/drivers/char/tpm/tpm_tis_spi_main.c > > +++ b/drivers/char/tpm/tpm_tis_spi_main.c > > @@ -71,8 +71,74 @@ static int tpm_tis_spi_flow_control(struct tpm_tis_spi_phy *phy, > > return 0; > > } > > > > -int tpm_tis_spi_transfer(struct tpm_tis_data *data, u32 addr, u16 len, > > - u8 *in, const u8 *out) > > +/* > > + * Half duplex controller with support for TPM wait state detection like > > + * Tegra QSPI need CMD, ADDR & DATA sent in single message to manage HW flow > > + * control. Each phase sent in different transfer for controller to idenity > > + * phase. > > + */ > > +static int tpm_tis_spi_transfer_half(struct tpm_tis_data *data, u32 addr, > > + u16 len, u8 *in, const u8 *out) > > +{ > > + struct tpm_tis_spi_phy *phy = to_tpm_tis_spi_phy(data); > > + struct spi_transfer spi_xfer[3]; > > + struct spi_message m; > > + u8 transfer_len; > > + int ret; > > + > > + while (len) { > > + transfer_len = min_t(u16, len, MAX_SPI_FRAMESIZE); > > + > > + spi_message_init(&m); > > + phy->iobuf[0] = (in ? 0x80 : 0) | (transfer_len - 1); > > + phy->iobuf[1] = 0xd4; > > + phy->iobuf[2] = addr >> 8; > > + phy->iobuf[3] = addr; > > I haven't looked at much TPM code in the past couple of years, but > perhaps some defines instead of magic numbers here? 0x80 is the rw bit, > and 0xd4 the transaction offset? > > > + > > + memset(&spi_xfer, 0, sizeof(spi_xfer)); > > + > > + spi_xfer[0].tx_buf = phy->iobuf; > > + spi_xfer[0].len = 1; > > + spi_message_add_tail(&spi_xfer[0], &m); > > + > > + spi_xfer[1].tx_buf = phy->iobuf + 1; > > + spi_xfer[1].len = 3; > > + spi_message_add_tail(&spi_xfer[1], &m); > > + > > + if (out) { > > + spi_xfer[2].tx_buf = &phy->iobuf[4]; > > + spi_xfer[2].rx_buf = NULL; > > + memcpy(&phy->iobuf[4], out, transfer_len); > > + out += transfer_len; > > + } > > + > > + if (in) { > > + spi_xfer[2].tx_buf = NULL; > > + spi_xfer[2].rx_buf = &phy->iobuf[4]; > > + } > > + > > + spi_xfer[2].len = transfer_len; > > + spi_message_add_tail(&spi_xfer[2], &m); > > + > > + reinit_completion(&phy->ready); > > + > > + ret = spi_sync_locked(phy->spi_device, &m); > > + if (ret < 0) > > + return ret; > > + > > + if (in) { > > + memcpy(in, &phy->iobuf[4], transfer_len); > > + in += transfer_len; > > + } > > + > > + len -= transfer_len; > > + } > > + > > + return ret; > > +} > > Does tpm_tis_spi_transfer_half not need to lock the bus? The doc comments for spi_sync_locked > state: > > This call should be used by drivers that require exclusive access to the > SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must > be released by a spi_bus_unlock call when the exclusive access is over. > > If that isn't the case should it be using spi_sync instead of spi_sync_locked? > > Regards, > Jerry b4 mbox -c to the rescue. I found the earlier discussion with Mark about the lock, so I guess the question is just should this call spi_sync instead of spi_sync_locked then? The magic numbers is a minor nit, and can probably be cleaned up separately since the full duplex code was already doing the same thing. The only other nit is just the older tcg spec being referenced in patch 1. Regards, Jerry > > > + > > +static int tpm_tis_spi_transfer_full(struct tpm_tis_data *data, u32 addr, > > + u16 len, u8 *in, const u8 *out) > > { > > struct tpm_tis_spi_phy *phy = to_tpm_tis_spi_phy(data); > > int ret = 0; > > @@ -140,6 +206,24 @@ int tpm_tis_spi_transfer(struct tpm_tis_data *data, u32 addr, u16 len, > > return ret; > > } > > > > +int tpm_tis_spi_transfer(struct tpm_tis_data *data, u32 addr, u16 len, > > + u8 *in, const u8 *out) > > +{ > > + struct tpm_tis_spi_phy *phy = to_tpm_tis_spi_phy(data); > > + struct spi_controller *ctlr = phy->spi_device->controller; > > + > > + /* > > + * TPM flow control over SPI requires full duplex support. > > + * Send entire message to a half duplex controller to handle > > + * wait polling in controller. > > + * Set TPM HW flow control flag.. > > + */ > > + if (ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) > > + return tpm_tis_spi_transfer_half(data, addr, len, in, out); > > + else > > + return tpm_tis_spi_transfer_full(data, addr, len, in, out); > > +} > > + > > static int tpm_tis_spi_read_bytes(struct tpm_tis_data *data, u32 addr, > > u16 len, u8 *result, enum tpm_tis_io_mode io_mode) > > { > > @@ -181,6 +265,9 @@ static int tpm_tis_spi_probe(struct spi_device *dev) > > > > phy->flow_control = tpm_tis_spi_flow_control; > > > > + if (dev->controller->flags & SPI_CONTROLLER_HALF_DUPLEX) > > + dev->mode |= SPI_TPM_HW_FLOW; > > + > > /* If the SPI device has an IRQ then use that */ > > if (dev->irq > 0) > > irq = dev->irq; > > -- > > 2.17.1 > > >