Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp1024122pxk; Fri, 18 Sep 2020 01:33:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw1kxYTcIiWg9bPYLaV/Hs9+/zgck5QKwah/fBew0wcZJ6hSTWgvcrkU6vhJxN+P31Ms54o X-Received: by 2002:a17:906:4a44:: with SMTP id a4mr34609652ejv.492.1600418022338; Fri, 18 Sep 2020 01:33:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600418022; cv=none; d=google.com; s=arc-20160816; b=RKJrUq0+z08H9xsTEPZAXOHGEAa3qAW1OWzXQQKU1T3WDd3cpG1+L0P3TfR5hsajXn sr8GP51fxuFHVStPhDnl8APuVHEKwr0gmBK4tV9/hNHcn2j1cZRkuZLappkDCKQyC55R fHmhNDqxQRtgr+tPHaZyD9haAT/Dg0k/nxghCLIPnG18wWybOYOwcyRhsuXoXvQtgRzi AzwlHt0EBhaiAMswNX8R6snI8biektmkmhTPDtpqRoCCm7A7SNZ4dQqN6Pe6BMbVJdhZ vfzhoP26nny3ppsCAbko01nhR6dq305kXRICs+Q4bnxIWzqz5m0IVAWrYVlxSW3TbQEW Wsvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=PtgT8oGdi7tJWYrZylWP5X+Qu5mtWrTjssOMjWBLBio=; b=YsvvqH7b8gjTe7hW2lysirjXfJ5UFic4usdDewzPT0hvcWfxXnXL9K13LaK2YhBvAS EOfAKq53DvUgGjXMT1fJp/bx2beTC7+ksoYDXpe/z8uo+W2C6BteNLVfVIvX/kofVnl5 AyMjKkJaZ5O2dZk3MYrC9/8grF1K4pBwpDGimKpJa8mUv2YZIsPpwz6dSU8jKKUTpGBN CrciTvOQHanPqOt3EfoxA93UXMEciFq4BQzA5FvMRD1JI9WxXr3s72zmHc8CPAB9hf33 2TUcZnXwJ855CsPYOexDlrOmuwosVH3j0SSYWjNwncR4aCcq26I2WYi/pCK/qYoaL0pq ZgZg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=gIWNzXXI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bl18si1791031ejb.171.2020.09.18.01.33.18; Fri, 18 Sep 2020 01:33:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=gIWNzXXI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726688AbgIRIbh (ORCPT + 99 others); Fri, 18 Sep 2020 04:31:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57006 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726718AbgIRIbg (ORCPT ); Fri, 18 Sep 2020 04:31:36 -0400 Received: from mail-pg1-x544.google.com (mail-pg1-x544.google.com [IPv6:2607:f8b0:4864:20::544]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C6EAC061756 for ; Fri, 18 Sep 2020 01:31:36 -0700 (PDT) Received: by mail-pg1-x544.google.com with SMTP id l71so3059523pge.4 for ; Fri, 18 Sep 2020 01:31:36 -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=KEc8Z0C4O67I6HU4P/dJyGdN00jFMUefKFkNCvwqxQA2X6ns9j2SRrnqHItyiwdSlS IAac8SLsaEF996PDTniqJh6394h+fDY7uPEEuEuwyXseEn8fUNKDkXDwGVkjJ9jqjFHb SBBiWlkKUpZ+sYN0jdpaXgTe3ecnVJbnPysxC/0yXOnBszmqOAdSR42pmk4Cp4OJ2b4m 5nfr9ZKlTW+HUQ1Te/f8PmLsiauLpsXgkOlLdvUKBA7ZxN9eLZx/jLC7eN0yQ2zbifht n+MJFUuukjBVZaWO9hxOaroJFRoDaODfJayYdjOtn4BeGvLGpYCE0PPABEc4yjGxNNb/ iFYA== X-Gm-Message-State: AOAM530bEePk1AolsjXOAVSIPtD4Om6Z1F8FenyOYgQDw12DUTzLONQm lmwZXqBuCATaR8/7BIucn1NmHA== 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 Cc: Ikjoon Jang , Matthias Brugger , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mediatek@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 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.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); } -- 2.28.0.681.g6f77f65b4e-goog