From patchwork Mon Jun 19 15:53:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 13284753 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B5992EB64D9 for ; Mon, 19 Jun 2023 15:54:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=nn7jzvjZXgSNHMP1VOAw1kMWhqQCYnx5uSa/pv5CNg0=; b=VESxsQqpKiW/Xm o6ah4nqIXi2CqoQXmmGmTReoAgDTdUEKKgZAMGab94oKCzaz2IBtdfAAbQhdZu12sv2MZIvjl/xZn EKeAktDWNIpQcraTmWy/CMFC8xc7VC2X+XPGvCqkGu00AgjjJD/R98aPDiR8m0RQSKJz6WU4Poc0y ASkUkwowHHeb1f7ehAJTD5MaLoQY95a1CIGn5Jfs+NzXxicwxeHzI06Qer0Usbitl4rtBa/TDnySI lFsJQMFjsSMsU6C2jHEFicSAqvBooJ0+4yd1DOLyfjuNohiE90IlA0BmC9uhCf/hAuuxoSMCQApcD M2kIfp4Hmx2sTVgjQ9jg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qBHCn-008qoK-24; Mon, 19 Jun 2023 15:54:13 +0000 Received: from relay8-d.mail.gandi.net ([217.70.183.201]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qBHCZ-008qk5-0B for linux-arm-kernel@lists.infradead.org; Mon, 19 Jun 2023 15:54:01 +0000 X-GND-Sasl: miquel.raynal@bootlin.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1687190035; 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=PhIQmvwCqc8f2ldfvdDwrf5zFPY90uFTbI6lO/wBafo=; b=ZXzrLBsgbSEa2r0CaAMT7Tkop+dVa6ENEoT112qvP15DZzXWqfx08nu9gIxTrjKVJcGlQy Xn/ATd8crYS0Jm2GobR+8EFj2W03ObKiLCcbI4Q92hNGINiNSeKXZmJ4UMj9Lv0JbpHPFm 02q9DHk86SKMg+0SYQ4Q1rw1O2nj1FQklaTWkoYdth+mqgZK/e7O3yPU9hakNGHmFS/AwF vRWe6BkgqqR0m03DwqRaD+h5iFF3rbu6n9VnWVMbA2KgzNs/jlB4mrfH1YTvZhaoVlz7YX treaYYh//jQ2MfZjHMPXqRUadUz5ITezyZZ/OnCjswhJAgTmyQUrXLR9Cc+g7g== X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com Received: by mail.gandi.net (Postfix) with ESMTPSA id 1ED2F1BF206; Mon, 19 Jun 2023 15:53:54 +0000 (UTC) From: Miquel Raynal To: Mark Brown , Subject: [PATCH v2 1/3] spi: Create a helper to derive adaptive timeouts Date: Mon, 19 Jun 2023 17:53:47 +0200 Message-Id: <20230619155349.3118420-2-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230619155349.3118420-1-miquel.raynal@bootlin.com> References: <20230619155349.3118420-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230619_085359_351619_CA29CC17 X-CRM114-Status: GOOD ( 12.83 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Alexandre Belloni , Samuel Holland , Chen-Yu Tsai , Jernej Skrabec , Tudor Ambarus , Thomas Petazzoni , Miquel Raynal , linux-sunxi@lists.linux.dev, Claudiu Beznea , linux-arm-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Big transfers might take a bit of time, too constraining timeouts might lead to false positives. In order to simplify the drivers work and with the goal of factorizing code in mind, let's add a helper that can be used by any spi controller driver to derive a relevant per-transfer timeout value. The logic is simple: we know how much time it would take to transfer a byte, we can easily derive the total theoretical amount of time involved for each transfer. We multiply it by two to have a bit of margin and enforce a minimum of 500ms. Suggested-by: Mark Brown Signed-off-by: Miquel Raynal --- include/linux/spi/spi.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h index fbf8c0d95968..4d6636c50465 100644 --- a/include/linux/spi/spi.h +++ b/include/linux/spi/spi.h @@ -1186,6 +1186,23 @@ static inline bool spi_is_bpw_supported(struct spi_device *spi, u32 bpw) return false; } +/** + * spi_controller_xfer_timeout - Compute a suitable timeout value + * @ctlr: SPI device + * @xfer: Transfer descriptor + * + * Compute a relevant timeout value for the given transfer. We derive the time + * that it would take on a single data line and take twice this amount of time + * with a minimum of 500ms to avoid false positives on loaded systems. + * + * Returns: Transfer timeout value in milliseconds. + */ +static inline unsigned int spi_controller_xfer_timeout(struct spi_controller *ctlr, + struct spi_transfer *xfer) +{ + return max(xfer->len * 8 * 2 / (xfer->speed_hz / 1000), 500U); +} + /*---------------------------------------------------------------------------*/ /* SPI transfer replacement methods which make use of spi_res */