From patchwork Fri Sep 18 08:31:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ikjoon Jang X-Patchwork-Id: 11784333 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 04A5B59D for ; Fri, 18 Sep 2020 08:32:49 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id BD4DB2084C for ; Fri, 18 Sep 2020 08:32:48 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="V31/FgsF"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="gIWNzXXI" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org BD4DB2084C Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; 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=RPibM+taRsZ9v1XqsAZKBatZW33MBorxCS+y+F4yLls=; b=V31/FgsFTbwO6aaXlhEf29Ffd tYkgYp7ydbELlStdY9rY1sbCd66DwwdGzM33CyFe9s3l9O3LGsIiJLrjU7Fsq2nCYUqTl9Of5yw4m inR3tJRWnj4utWxG40lWu5G++N/+tznG33frzw9pzysa1k2dyz0IAXVo0dQEcDqRiH+dygbteKx6s IHNERXysUyinryuZUpzMwbCmsXcjwc46aykCj6Z43UV2isOYyjRjbFP4FnilYpxpZlN4kL/kzj7LM 2A5CzcyZSI4edaiVL4ZWZRv5KG771zhRPZrmX6XVXf7TTEwVMr9z9diQIcuhkLpd6dg34vZJZQX1P 7jzQPvsXg==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kJBok-0008V6-Ja; Fri, 18 Sep 2020 08:32:30 +0000 Received: from mail-pg1-x541.google.com ([2607:f8b0:4864:20::541]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kJBnu-0008DM-Eu for linux-arm-kernel@lists.infradead.org; Fri, 18 Sep 2020 08:31:41 +0000 Received: by mail-pg1-x541.google.com with SMTP id 67so3032786pgd.12 for ; Fri, 18 Sep 2020 01:31:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=PtgT8oGdi7tJWYrZylWP5X+Qu5mtWrTjssOMjWBLBio=; b=gIWNzXXI/65W9lRuW4rM4ZuoKImgBcadG9k1ZTZdlozcVR0w6y7VHSw5ovnfxV+y4s me4YUC5KA0N/CovI2zxVtNEXpQ8aXA1C+c8tsuTUo/mPKvhbcVunwTsO8sq35KVqV2wP tRlaD2cHR/DshX6cxwuMkOhzPkx4cdjcbJxpY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=PtgT8oGdi7tJWYrZylWP5X+Qu5mtWrTjssOMjWBLBio=; b=UjMORSFROzoaS+LWfo2TrNJH+wKd0IclgszGpwv777mkxJLQsFD9gxoLTOk4du2pSv X6jM3uL2u9YPdpsjj/ksXDtMVbj+Kp2n6sc9ugOf2I+VS+7s9cLRDjAYAqBAodvX6Sqi V85NdwJxLIcwy45sVpnoG3rwk+dSKXYSXRZrBD7rgPaOyRskdpI5KDaZuSCAK4fA8A4z LtedbPy7nf5MSqWgufwRa1OMmAAsCsdpOACzt9HH6J7WaXGfCUKC8Sgg6TROFqUDRfgZ GKmWo2UZQ1dS1oEPDu3kMkXu+e3KzKnP0Fyxeuu908MzjHpJWyyYCVbSVWU8DdV4D1ID tkpQ== X-Gm-Message-State: AOAM532xPymQwNjjspYE5mfP+i7G6FulENXyV1Sc0x8q2PS6pyRvQaUo 1wvHYoSNnrb4baHMCuJGpmQk3A== X-Google-Smtp-Source: ABdhPJylLeVr7aTAeXl/TquJqTgllezEHHoyI6zJ5/+F2fB1EMgD/15lCBC8Kt+Lo/SC0WxayecFLQ== X-Received: by 2002:aa7:934e:0:b029:13f:d056:593 with SMTP id 14-20020aa7934e0000b029013fd0560593mr25187131pfn.15.1600417896081; Fri, 18 Sep 2020 01:31:36 -0700 (PDT) Received: from ikjn-p920.tpe.corp.google.com ([2401:fa00:1:10:f693:9fff:fef4:a8fc]) by smtp.gmail.com with ESMTPSA id g206sm2193172pfb.178.2020.09.18.01.31.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Sep 2020 01:31:35 -0700 (PDT) From: Ikjoon Jang To: Rob Herring , Mark Brown , devicetree@vger.kernel.org, linux-spi@vger.kernel.org, linux-mtd@lists.infradead.org Subject: [PATCH v2 2/5] spi: spi-mtk-nor: fix mishandled logics in checking SPI memory operation Date: Fri, 18 Sep 2020 16:31:20 +0800 Message-Id: <20200918162834.v2.2.I3de2918f09b817cc2ae6d324f1ece62779ecc7cf@changeid> X-Mailer: git-send-email 2.28.0.681.g6f77f65b4e-goog In-Reply-To: <20200918083124.3921207-1-ikjn@chromium.org> References: <20200918083124.3921207-1-ikjn@chromium.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200918_043138_564949_92678AFA X-CRM114-Status: GOOD ( 17.81 ) X-Spam-Score: -3.2 (---) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (-3.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:541 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -3.0 DKIMWL_WL_HIGH DKIMwl.org - Whitelisted High sender X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Matthias Brugger , linux-mediatek@lists.infradead.org, Ikjoon Jang , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org Fix a simple bug which can limits its transfer size, and add a simple helper function for code cleanups. Fixes: a59b2c7c56bf ("spi: spi-mtk-nor: support standard spi properties") Signed-off-by: Ikjoon Jang --- (no changes since v1) drivers/spi/spi-mtk-nor.c | 62 ++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/drivers/spi/spi-mtk-nor.c b/drivers/spi/spi-mtk-nor.c index 6e6ca2b8e6c8..54b2c0fde95b 100644 --- a/drivers/spi/spi-mtk-nor.c +++ b/drivers/spi/spi-mtk-nor.c @@ -167,52 +167,63 @@ static bool mtk_nor_match_read(const struct spi_mem_op *op) return false; } -static int mtk_nor_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op) +static bool need_bounce(void *cpu_addr, unsigned long len) { - size_t len; + return !!(((uintptr_t)cpu_addr) & MTK_NOR_DMA_ALIGN_MASK); +} +static int mtk_nor_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op) +{ if (!op->data.nbytes) return 0; if ((op->addr.nbytes == 3) || (op->addr.nbytes == 4)) { - if ((op->data.dir == SPI_MEM_DATA_IN) && - mtk_nor_match_read(op)) { + switch (op->data.dir) { + case SPI_MEM_DATA_IN: + if (!mtk_nor_match_read(op)) + return -EINVAL; + /* check if it's DMAable */ if ((op->addr.val & MTK_NOR_DMA_ALIGN_MASK) || - (op->data.nbytes < MTK_NOR_DMA_ALIGN)) + (op->data.nbytes < MTK_NOR_DMA_ALIGN)) { op->data.nbytes = 1; - else if (!((ulong)(op->data.buf.in) & - MTK_NOR_DMA_ALIGN_MASK)) + } else { + if (need_bounce(op->data.buf.in, op->data.nbytes) && + (op->data.nbytes > MTK_NOR_BOUNCE_BUF_SIZE)) + op->data.nbytes = MTK_NOR_BOUNCE_BUF_SIZE; op->data.nbytes &= ~MTK_NOR_DMA_ALIGN_MASK; - else if (op->data.nbytes > MTK_NOR_BOUNCE_BUF_SIZE) - op->data.nbytes = MTK_NOR_BOUNCE_BUF_SIZE; - return 0; - } else if (op->data.dir == SPI_MEM_DATA_OUT) { + } + break; + case SPI_MEM_DATA_OUT: if (op->data.nbytes >= MTK_NOR_PP_SIZE) op->data.nbytes = MTK_NOR_PP_SIZE; else op->data.nbytes = 1; - return 0; + break; + default: + break; } + } else { + u8 len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes; + + if (len > MTK_NOR_PRG_MAX_SIZE) + return -EINVAL; + if (op->data.nbytes && !(MTK_NOR_PRG_MAX_SIZE - len)) + return -EINVAL; + if (op->data.nbytes > (MTK_NOR_PRG_MAX_SIZE - len)) + op->data.nbytes = MTK_NOR_PRG_MAX_SIZE - len; } - len = MTK_NOR_PRG_MAX_SIZE - op->cmd.nbytes - op->addr.nbytes - - op->dummy.nbytes; - if (op->data.nbytes > len) - op->data.nbytes = len; - return 0; } static bool mtk_nor_supports_op(struct spi_mem *mem, const struct spi_mem_op *op) { - size_t len; - if (op->cmd.buswidth != 1) return false; if ((op->addr.nbytes == 3) || (op->addr.nbytes == 4)) { - switch(op->data.dir) { + switch (op->data.dir) { case SPI_MEM_DATA_IN: if (!mtk_nor_match_read(op)) return false; @@ -226,11 +237,14 @@ static bool mtk_nor_supports_op(struct spi_mem *mem, default: break; } + } else { + u8 len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes; + + if (len > MTK_NOR_PRG_MAX_SIZE) + return false; + if (op->data.nbytes && !(MTK_NOR_PRG_MAX_SIZE - len)) + return false; } - len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes; - if ((len > MTK_NOR_PRG_MAX_SIZE) || - ((op->data.nbytes) && (len == MTK_NOR_PRG_MAX_SIZE))) - return false; return spi_mem_default_supports_op(mem, op); }