From patchwork Tue Apr 20 16:44:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Shevchenko X-Patchwork-Id: 424975 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B1B5AC433ED for ; Tue, 20 Apr 2021 16:44:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 79C5B613D4 for ; Tue, 20 Apr 2021 16:44:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232940AbhDTQop (ORCPT ); Tue, 20 Apr 2021 12:44:45 -0400 Received: from mga18.intel.com ([134.134.136.126]:43728 "EHLO mga18.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232504AbhDTQoo (ORCPT ); Tue, 20 Apr 2021 12:44:44 -0400 IronPort-SDR: +uMUa0AbBr1Y2cR1OOgMa0eb/mk966i1j3Yg3Xi4LSDCqbqO0RYcVKEpvIN60TkEaENgoDKaII y2P2mdW2rWKw== X-IronPort-AV: E=McAfee;i="6200,9189,9960"; a="183034461" X-IronPort-AV: E=Sophos;i="5.82,237,1613462400"; d="scan'208";a="183034461" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Apr 2021 09:44:12 -0700 IronPort-SDR: 4FEJkK2qsvZ7J1racoP3GUaZUgoB/YJlN4bSwseSYhTP5f379QjJrMiMxrRnGh6/OGOFB8cNBz kSCOi0RFwFTg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.82,237,1613462400"; d="scan'208";a="454900279" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga002.fm.intel.com with ESMTP; 20 Apr 2021 09:44:10 -0700 Received: by black.fi.intel.com (Postfix, from userid 1003) id E0792BB; Tue, 20 Apr 2021 19:44:27 +0300 (EEST) From: Andy Shevchenko To: Mark Brown , linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Andy Shevchenko Subject: [PATCH v2 1/2] spi: Allow to have all native CSs in use along with GPIOs Date: Tue, 20 Apr 2021 19:44:24 +0300 Message-Id: <20210420164425.40287-1-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org The commit 7d93aecdb58d ("spi: Add generic support for unused native cs with cs-gpios") excludes the valid case for the controllers that doesn't need to switch native CS in order to perform the transfer, i.e. when 0 native ... ... - 1 native GPIO + 1 GPIO ... ... where defines maximum of native CSs supported by the controller. To allow this, bail out from spi_get_gpio_descs() conditionally for the controllers which explicitly marked with SPI_MASTER_GPIO_SS. Fixes: 7d93aecdb58d ("spi: Add generic support for unused native cs with cs-gpios") Signed-off-by: Andy Shevchenko --- v2: no changes drivers/spi/spi.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index 36c46feab6d4..9c3730a9f7d5 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -2610,8 +2610,9 @@ static int spi_get_gpio_descs(struct spi_controller *ctlr) } ctlr->unused_native_cs = ffz(native_cs_mask); - if (num_cs_gpios && ctlr->max_native_cs && - ctlr->unused_native_cs >= ctlr->max_native_cs) { + + if ((ctlr->flags & SPI_MASTER_GPIO_SS) && num_cs_gpios && + ctlr->max_native_cs && ctlr->unused_native_cs >= ctlr->max_native_cs) { dev_err(dev, "No unused native chip select available\n"); return -EINVAL; } From patchwork Tue Apr 20 16:44:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Shevchenko X-Patchwork-Id: 425693 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 298B8C433B4 for ; Tue, 20 Apr 2021 16:44:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B0D6C613DC for ; Tue, 20 Apr 2021 16:44:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233194AbhDTQop (ORCPT ); Tue, 20 Apr 2021 12:44:45 -0400 Received: from mga14.intel.com ([192.55.52.115]:3030 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232916AbhDTQoo (ORCPT ); Tue, 20 Apr 2021 12:44:44 -0400 IronPort-SDR: LkK24RjWW/QGpmSByTyb66ASZuDCqrhh92aV16xH9hQKy7Fy6CNMKML/ygnLNB1mtVO690J0er L0KUEplSYGoA== X-IronPort-AV: E=McAfee;i="6200,9189,9960"; a="195098657" X-IronPort-AV: E=Sophos;i="5.82,237,1613462400"; d="scan'208";a="195098657" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Apr 2021 09:44:12 -0700 IronPort-SDR: HcaxeBXaOCRlZFWSlqoRGIc79xOi2yvB/OxQoB+GpZUB16je5aAAJ/a238bGskr4zNq0cYrISu NC+0XunhHXxQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.82,237,1613462400"; d="scan'208";a="427063562" Received: from black.fi.intel.com ([10.237.72.28]) by orsmga008.jf.intel.com with ESMTP; 20 Apr 2021 09:44:10 -0700 Received: by black.fi.intel.com (Postfix, from userid 1003) id 7FBAD103; Tue, 20 Apr 2021 19:44:28 +0300 (EEST) From: Andy Shevchenko To: Mark Brown , linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Andy Shevchenko Subject: [PATCH v2 2/2] spi: Avoid undefined behaviour when counting unused native CSs Date: Tue, 20 Apr 2021 19:44:25 +0300 Message-Id: <20210420164425.40287-2-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210420164425.40287-1-andriy.shevchenko@linux.intel.com> References: <20210420164425.40287-1-andriy.shevchenko@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org ffz(), that has been used to count unused native CSs, might cause undefined behaviour when called against ~0U. To fix that, open code it with ffs(~value) - 1. Fixes: 7d93aecdb58d ("spi: Add generic support for unused native cs with cs-gpios") Signed-off-by: Andy Shevchenko --- v2: decoded UB abbreviation (Mark) drivers/spi/spi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index 9c3730a9f7d5..01f95bee2ac8 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -2609,7 +2609,7 @@ static int spi_get_gpio_descs(struct spi_controller *ctlr) native_cs_mask |= BIT(i); } - ctlr->unused_native_cs = ffz(native_cs_mask); + ctlr->unused_native_cs = ffs(~native_cs_mask) - 1; if ((ctlr->flags & SPI_MASTER_GPIO_SS) && num_cs_gpios && ctlr->max_native_cs && ctlr->unused_native_cs >= ctlr->max_native_cs) {