Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2199063iof; Tue, 7 Jun 2022 22:59:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzhrBcVWqkF4NZ27R8JFQRN+KtV+cTuBBAAWEcErrWLdPhsN/hiEc3OpdoYltbdjRNjABip X-Received: by 2002:a17:902:9049:b0:167:78c0:e065 with SMTP id w9-20020a170902904900b0016778c0e065mr15031706plz.46.1654667995774; Tue, 07 Jun 2022 22:59:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654667995; cv=none; d=google.com; s=arc-20160816; b=rHV0YviTRtATqF2gasDWeecdiArHbq/X3Ekbg1Pgb39WeoSwZycU073hYUdgtrX6iF r8Zv/2WU5qPfSd7QGLxf7pGDBL6s/DyyulYnHdV0QDxjnfQQa/K6+F20IqMmPbbIMK3d LiFYfdx2CkfA9s8pDLCkoEtbcT+oq/sByFz+YZxfkGmbbwN8VzsSaW9elO4RN/XIhVQl eEd+w1/t3nexdm99uhuzq4ilI0e2lhtqrF2RtYwUxrfcX2HyBxIkCXZlbi5mXhuN+O2W 5GesX5Iq/p4CGB5J6c7xrr305hcLGr+tbmsCQ5+cALiVlph8Loy5ZzQ0Q5A3JFRmIWY0 K6PQ== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=UDUyc9KiXiU5NmqDI4iwuIJnzJcgf6Xsbc95/1Nevyo=; b=stn4FYilMVEN12KXPtsc2yEjX84ixvJDOtwXeCBuR/3LNFvHZA0XOxcLTo5N///k7E 3BVM0pHQayOawSfYA1bc9hYvkKdZG8E+LAL08kIfAsQIyEkSDT0kVMpWzwW6Eqd1//3z xTBrU7wBI8EhiX6pbN0gWjhcWE4cVeQffJeDXV+sFL/xFU336hdEZNDnFM8x6SN4KhJ0 e5Xy/71rIw6cg1eGsPmuey7up2Xc4YTW9tIocOIpkEE2jJVzifDiGxnWXnYgPg4HP4MC PFu7TfW1i8brT3DhMqs28iRTriMAQDC/njduc4+NS9sCi+EdxPNAEcjqw4WzWARGvTe4 6FtA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=cK24aMgq; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id lr1-20020a17090b4b8100b001e896f91ba0si9386258pjb.64.2022.06.07.22.59.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 22:59:55 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=cK24aMgq; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 997CC27B4B3; Tue, 7 Jun 2022 22:26:33 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382935AbiFHBPN (ORCPT + 99 others); Tue, 7 Jun 2022 21:15:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38594 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1382649AbiFGVvm (ORCPT ); Tue, 7 Jun 2022 17:51:42 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A39F923D5D4; Tue, 7 Jun 2022 12:09:09 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id B6C906188D; Tue, 7 Jun 2022 19:09:08 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C5AEDC385A2; Tue, 7 Jun 2022 19:09:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654628948; bh=cNOrBeipYs4+o24KDc75FBnvoDRrZfjh1pCMj8Jy9TM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cK24aMgqOPccQErEIgfbvid26hd1GbU1RciCC6o4YSYJhAXs9Me95Gg/jlSOGYw+z XuEnx4lmklZojWT/pn1Iy0zHMqr93AfkFqw6eA/ONFQIgDJqZvcR8Ob5DMlGNEb1Su kusQU8TSnR5MlgpfMjBAPPkLzNEyl6Zv7l3f5CB0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Adam Wujek , Guenter Roeck , Sasha Levin Subject: [PATCH 5.18 515/879] hwmon: (pmbus) Check PEC support before reading other registers Date: Tue, 7 Jun 2022 19:00:33 +0200 Message-Id: <20220607165017.825232303@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220607165002.659942637@linuxfoundation.org> References: <20220607165002.659942637@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Adam Wujek [ Upstream commit d1baf7a3a3177d46a7149858beddb88a9eca7a54 ] Make sure that the support of PEC is determined before the read of other registers. Otherwise the validation of PEC can trigger an error on the read of STATUS_BYTE or STATUS_WORD registers. The problematic scenario is the following. A device with enabled PEC support is up and running and a kernel driver is loaded. Then the driver is unloaded (or device unbound), the HW device is reconfigured externally (e.g. by i2cset) to advertise itself as not supporting PEC. Without the move of the code, at the second load of the driver (or bind) the STATUS_BYTE or STATUS_WORD register is always read with PEC enabled, which is likely to cause a read error resulting with fail of a driver load (or bind). Signed-off-by: Adam Wujek Link: https://lore.kernel.org/r/20220519233334.438621-1-dev_public@wujek.eu Fixes: 75d2b2b06bd84 ("hwmon: (pmbus) disable PEC if not enabled") Fixes: 4e5418f787ec5 ("hwmon: (pmbus_core) Check adapter PEC support") [groeck: Added Fixes: tags, dropped continuation line] Signed-off-by: Guenter Roeck Signed-off-by: Sasha Levin --- drivers/hwmon/pmbus/pmbus_core.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/drivers/hwmon/pmbus/pmbus_core.c b/drivers/hwmon/pmbus/pmbus_core.c index 5a1796650f5b..86429bfa4847 100644 --- a/drivers/hwmon/pmbus/pmbus_core.c +++ b/drivers/hwmon/pmbus/pmbus_core.c @@ -2308,6 +2308,21 @@ static int pmbus_init_common(struct i2c_client *client, struct pmbus_data *data, struct device *dev = &client->dev; int page, ret; + /* + * Figure out if PEC is enabled before accessing any other register. + * Make sure PEC is disabled, will be enabled later if needed. + */ + client->flags &= ~I2C_CLIENT_PEC; + + /* Enable PEC if the controller and bus supports it */ + if (!(data->flags & PMBUS_NO_CAPABILITY)) { + ret = i2c_smbus_read_byte_data(client, PMBUS_CAPABILITY); + if (ret >= 0 && (ret & PB_CAPABILITY_ERROR_CHECK)) { + if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_PEC)) + client->flags |= I2C_CLIENT_PEC; + } + } + /* * Some PMBus chips don't support PMBUS_STATUS_WORD, so try * to use PMBUS_STATUS_BYTE instead if that is the case. @@ -2326,19 +2341,6 @@ static int pmbus_init_common(struct i2c_client *client, struct pmbus_data *data, data->has_status_word = true; } - /* Make sure PEC is disabled, will be enabled later if needed */ - client->flags &= ~I2C_CLIENT_PEC; - - /* Enable PEC if the controller and bus supports it */ - if (!(data->flags & PMBUS_NO_CAPABILITY)) { - ret = i2c_smbus_read_byte_data(client, PMBUS_CAPABILITY); - if (ret >= 0 && (ret & PB_CAPABILITY_ERROR_CHECK)) { - if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_PEC)) { - client->flags |= I2C_CLIENT_PEC; - } - } - } - /* * Check if the chip is write protected. If it is, we can not clear * faults, and we should not try it. Also, in that case, writes into -- 2.35.1