2022-04-22 00:20:16

by Adam Wujek

[permalink] [raw]
Subject: [PATCH] hwmod: (pmbus) disable PEC if not enabled

Explicitly disable PEC when the client does not support it.
The problematic scenario is the following. A device with enabled PEC
support is up and running, a kernel driver 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 a new code, at the second load of the driver
(or bind) the "flags" variable is not updated to avoid PEC usage. As a
consequence the further communication with the device is done with
the PEC enabled, which is wrong.

Signed-off-by: Adam Wujek <[email protected]>
---
drivers/hwmon/pmbus/pmbus_core.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/hwmon/pmbus/pmbus_core.c b/drivers/hwmon/pmbus/pmbus_core.c
index b2618b1d529e..0af7a3d74f47 100644
--- a/drivers/hwmon/pmbus/pmbus_core.c
+++ b/drivers/hwmon/pmbus/pmbus_core.c
@@ -2334,7 +2334,8 @@ static int pmbus_init_common(struct i2c_client *client, struct pmbus_data *data,
client->flags |= I2C_CLIENT_PEC;
}
}
- }
+ } else
+ client->flags &= ~I2C_CLIENT_PEC;

/*
* Check if the chip is write protected. If it is, we can not clear
--
2.25.1



2022-04-22 19:57:28

by Guenter Roeck

[permalink] [raw]
Subject: Re: [PATCH] hwmod: (pmbus) disable PEC if not enabled

On 4/20/22 05:54, Adam Wujek wrote:
> Explicitly disable PEC when the client does not support it.
> The problematic scenario is the following. A device with enabled PEC
> support is up and running, a kernel driver 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 a new code, at the second load of the driver
> (or bind) the "flags" variable is not updated to avoid PEC usage. As a
> consequence the further communication with the device is done with
> the PEC enabled, which is wrong.
>
> Signed-off-by: Adam Wujek <[email protected]>

Subject should start with hwmon:. Please version your patches,
and provide change logs.

> ---
> drivers/hwmon/pmbus/pmbus_core.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/hwmon/pmbus/pmbus_core.c b/drivers/hwmon/pmbus/pmbus_core.c
> index b2618b1d529e..0af7a3d74f47 100644
> --- a/drivers/hwmon/pmbus/pmbus_core.c
> +++ b/drivers/hwmon/pmbus/pmbus_core.c
> @@ -2334,7 +2334,8 @@ static int pmbus_init_common(struct i2c_client *client, struct pmbus_data *data,
> client->flags |= I2C_CLIENT_PEC;
> }
> }
> - }
> + } else
> + client->flags &= ~I2C_CLIENT_PEC;

Since if() is in {}, else should be in {} as well.

Guenter

>
> /*
> * Check if the chip is write protected. If it is, we can not clear
> --
> 2.25.1
>
>

2022-04-22 20:08:43

by Adam Wujek

[permalink] [raw]
Subject: [v3 PATCH] hwmon: (pmbus) disable PEC if not enabled

Explicitly disable PEC when the client does not support it.
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 a new code, at the second load of the driver
(or bind) the "flags" variable is not updated to avoid PEC usage. As a
consequence the further communication with the device is done with
the PEC enabled, which is wrong and may fail.

The implementation first disable the I2C_CLIENT_PEC flag, then the old
code enable it if needed.

Signed-off-by: Adam Wujek <[email protected]>
---
Notes:
Changes in v2:
- Rebase to the latest kernel
- Update commit message

Changes in v3:
- Rework the patch, disable the flag first, then enable if needed.
Adding three else statements to disable the flag will make the code
less readable.
- Update commit message

drivers/hwmon/pmbus/pmbus_core.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/drivers/hwmon/pmbus/pmbus_core.c b/drivers/hwmon/pmbus/pmbus_core.c
index b2618b1d529e..d93574d6a1fb 100644
--- a/drivers/hwmon/pmbus/pmbus_core.c
+++ b/drivers/hwmon/pmbus/pmbus_core.c
@@ -2326,6 +2326,9 @@ 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);
--
2.25.1