From patchwork Fri Jul 15 01:39:04 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Pronin X-Patchwork-Id: 9231021 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id DEAB060868 for ; Fri, 15 Jul 2016 02:01:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C055D2830A for ; Fri, 15 Jul 2016 02:01:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B104928326; Fri, 15 Jul 2016 02:01:27 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from lists.sourceforge.net (lists.sourceforge.net [216.34.181.88]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 5B3702830A for ; Fri, 15 Jul 2016 02:01:26 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=sfs-ml-4.v29.ch3.sourceforge.com) by sfs-ml-4.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1bNsRP-0006OD-Pg; Fri, 15 Jul 2016 02:01:23 +0000 Received: from sog-mx-1.v43.ch3.sourceforge.com ([172.29.43.191] helo=mx.sourceforge.net) by sfs-ml-4.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1bNsRO-0006O5-Ek for tpmdd-devel@lists.sourceforge.net; Fri, 15 Jul 2016 02:01:22 +0000 Received-SPF: pass (sog-mx-1.v43.ch3.sourceforge.com: domain of chromium.org designates 209.85.192.174 as permitted sender) client-ip=209.85.192.174; envelope-from=apronin@chromium.org; helo=mail-pf0-f174.google.com; Received: from mail-pf0-f174.google.com ([209.85.192.174]) by sog-mx-1.v43.ch3.sourceforge.com with esmtps (TLSv1:AES128-SHA:128) (Exim 4.76) id 1bNsRN-0007xV-Gn for tpmdd-devel@lists.sourceforge.net; Fri, 15 Jul 2016 02:01:22 +0000 Received: by mail-pf0-f174.google.com with SMTP id y134so9749781pfg.0 for ; Thu, 14 Jul 2016 19:01:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=flZEiGO8ItcuX75TyyDgImZ/2gtKL27AXVxQFVcHIq0=; b=MD0yhKFkxBj+rGrCc4vUY+7ZStg7hld8w2h8aKtXfERNMUyvn/fPO5zuyjUA57W1i8 wDEuSeHj30PcvHhksk7aiIkxjomz9bHbLzN5ggCPY/k+/mnRA31uqRS96/fgHCOplGt2 dUZl8NAkc+kCJld8etAbRgXU0Mf2DXkVMHUhHApDAIqPsxA58fn833tLVLvI5xsfC2R/ XXLuDB4zn1dcP7+c2Mm1/GJOs3h9ILSZXS0B8ZNpCY2hJluk9rBxDdogaVFg3f4lyn5H yr26gch0GhqLWrrdsMiRTNcY9qdk2Oe3uGE6Qqx/mEf+5L9bo0aq57vJL9nj2xGivS9Q 3zPA== X-Gm-Message-State: ALyK8tKYtoLKWHFpuEGBmENXx5dDnUiJaQY9/oxB+nQAQJEXqN6ou+CEiGmMqWt6d6JszXSa X-Received: by 10.98.200.142 with SMTP id i14mr17387448pfk.136.1468546748531; Thu, 14 Jul 2016 18:39:08 -0700 (PDT) Received: from apronin0.mtv.corp.google.com ([172.22.64.136]) by smtp.gmail.com with ESMTPSA id g26sm6490883pfj.82.2016.07.14.18.39.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 14 Jul 2016 18:39:08 -0700 (PDT) From: Andrey Pronin To: Jarkko Sakkinen Date: Thu, 14 Jul 2016 18:39:04 -0700 Message-Id: <1468546745-14646-2-git-send-email-apronin@chromium.org> X-Mailer: git-send-email 2.8.0.rc3.226.g39d4020 In-Reply-To: <1468546745-14646-1-git-send-email-apronin@chromium.org> References: <1468546745-14646-1-git-send-email-apronin@chromium.org> X-Headers-End: 1bNsRN-0007xV-Gn Cc: dianders@chromium.org, linux-kernel@vger.kernel.org, smbarber@chromium.org, tpmdd-devel@lists.sourceforge.net, groeck@chromium.org Subject: [tpmdd-devel] [PATCH 1/2] tpm_tis_core: add optional max xfer size check X-BeenThere: tpmdd-devel@lists.sourceforge.net X-Mailman-Version: 2.1.9 Precedence: list List-Id: Tpm Device Driver maintainance List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: tpmdd-devel-bounces@lists.sourceforge.net X-Virus-Scanned: ClamAV using ClamSMTP If tpm reports a bigger burstcnt than allowed by the physical protocol, re-query the burstcnt and correct, if needed, if still too large. In practice, seen in case of xfer issues (e.g. in spi interface case, lost header causing flow control issues and wrong values returned on read from TPM_STS). Without catching, causes the physical layer to reject xfer, while is easily preventable by re-querying TPM_STS. Signed-off-by: Andrey Pronin --- drivers/char/tpm/tpm_tis_core.c | 17 +++++++++++++++-- drivers/char/tpm/tpm_tis_core.h | 13 +++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/drivers/char/tpm/tpm_tis_core.c b/drivers/char/tpm/tpm_tis_core.c index 8110b52..f5d456c 100644 --- a/drivers/char/tpm/tpm_tis_core.c +++ b/drivers/char/tpm/tpm_tis_core.c @@ -158,6 +158,7 @@ static int get_burstcount(struct tpm_chip *chip) unsigned long stop; int burstcnt, rc; u32 value; + bool retry_burstcnt = false; /* wait for burstcount */ /* which timeout value, spec has 2 answers (c & d) */ @@ -168,8 +169,20 @@ static int get_burstcount(struct tpm_chip *chip) return rc; burstcnt = (value >> 8) & 0xFFFF; - if (burstcnt) - return burstcnt; + if (burstcnt) { + /* If burstcnt is larger than max allowed xfer + * size, retry once - may be a glitch. Return + * max_xfer_size on the 2nd try to avoid being + * stuck forever. + */ + if (tpm_tis_burstcnt_is_valid(priv, burstcnt)) + return burstcnt; + if (retry_burstcnt) + return tpm_tis_max_xfer_size(priv); + dev_warn(&chip->dev, "Bad burstcnt read: %d\n", + burstcnt); + retry_burstcnt = true; + } msleep(TPM_TIMEOUT); } while (time_before(jiffies, stop)); return -EBUSY; diff --git a/drivers/char/tpm/tpm_tis_core.h b/drivers/char/tpm/tpm_tis_core.h index 9191aab..713aa5a 100644 --- a/drivers/char/tpm/tpm_tis_core.h +++ b/drivers/char/tpm/tpm_tis_core.h @@ -102,6 +102,7 @@ struct tpm_tis_phy_ops { int (*read16)(struct tpm_tis_data *data, u32 addr, u16 *result); int (*read32)(struct tpm_tis_data *data, u32 addr, u32 *result); int (*write32)(struct tpm_tis_data *data, u32 addr, u32 src); + u16 max_xfer_size; }; static inline int tpm_tis_read_bytes(struct tpm_tis_data *data, u32 addr, @@ -144,6 +145,18 @@ static inline int tpm_tis_write32(struct tpm_tis_data *data, u32 addr, return data->phy_ops->write32(data, addr, value); } +static inline u16 tpm_tis_max_xfer_size(struct tpm_tis_data *data) +{ + return data->phy_ops->max_xfer_size; +} + +static inline bool tpm_tis_burstcnt_is_valid(struct tpm_tis_data *data, + u16 burstcnt) +{ + return (tpm_tis_max_xfer_size(data) == 0) + || (burstcnt <= tpm_tis_max_xfer_size(data)); +} + void tpm_tis_remove(struct tpm_chip *chip); int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq, const struct tpm_tis_phy_ops *phy_ops,