Skip to contentSkip to conversation

hwmod: (pmbus) disable PEC if not enabled

PATCH In review Adam Wujek 2 participants 8 messages Apr 19, 2022 – Apr 20, 2022 Hardware monitoring

lore
Share & tools
lore

Adam Wujek

author
#0 lore raw

PATCH 1/1 hwmod: (pmbus) disable PEC if not enabled

Explicitly disable PEC when the client does not support it.
Without the explicit disable, when the device with the PEC support is removed later when a device without PEC support is inserted into the same address, the driver uses the old value of client->flags which contains the I2C_CLIENT_PEC flag. As a consequence the PEC is used when it should not.

---
 drivers/hwmon/pmbus/pmbus_core.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/hwmon/pmbus/pmbus_core.c +2 -0
index 82c3754e21e3..f8ca36759b0a 100644
@@ -2014,6 +2014,8 @@ static int pmbus_init_common(struct i2c_client *client, struct pmbus_data *data,
 	ret = i2c_smbus_read_byte_data(client, PMBUS_CAPABILITY);
 	if (ret >= 0 && (ret & PB_CAPABILITY_ERROR_CHECK))
 		client->flags |= I2C_CLIENT_PEC;
+	else
+		client->flags &= ~I2C_CLIENT_PEC;
 
 	pmbus_clear_faults(client);
 

--
2.17.1

maintainer
#1 lore raw
Quoted 6 lines · Adam Wujek

Explicitly disable PEC when the client does not support it.
Without the explicit disable, when the device with the PEC support is removed later when a device without PEC support is inserted into the same address, the driver uses the old value of client->flags which contains the I2C_CLIENT_PEC flag. As a consequence the PEC is used when it should not.

How can that happen ? I would assume the I2C device gets deleted and re-created in that case, which should clear the PEC flag.

Guenter

Quoted 22 lines
---
  drivers/hwmon/pmbus/pmbus_core.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/hwmon/pmbus/pmbus_core.c +2 -1
index 82c3754e21e3..f8ca36759b0a 100644
@@ -2014,6 +2014,8 @@ static int pmbus_init_common(struct i2c_client *client, struct pmbus_data *data,
  	ret = i2c_smbus_read_byte_data(client, PMBUS_CAPABILITY);
  	if (ret >= 0 && (ret & PB_CAPABILITY_ERROR_CHECK))
  		client->flags |= I2C_CLIENT_PEC;
+	else
+		client->flags &= ~I2C_CLIENT_PEC;
 
  	pmbus_clear_faults(client);
 
--

2.17.1

author
#2 lore raw

------- Original Message -------

Quoted 11 lines · Guenter Roeck
Quoted 5 lines · Adam Wujek

Explicitly disable PEC when the client does not support it.
Without the explicit disable, when the device with the PEC support is removed later when a device without PEC support is inserted into the same address, the driver uses the old value of client->flags which contains the I2C_CLIENT_PEC flag. As a consequence the PEC is used when it should not.

Quoted 4 lines

How can that happen ? I would assume the I2C device gets deleted and re-created in that case, which should clear the PEC flag.

Guenter

In my case it was when I unloaded the driver for the I2C slave, changed the advertised PEC value in PMBUS_CAPABILITY register on slave. Then loaded the driver. When the switch was from disable->enable it worked as expected (this case was already covered), but when the PEC was set in the slave from enabled->disabled it was still using PEC to communicate.

Adam

Quoted 21 lines
Quoted 21 lines

---
drivers/hwmon/pmbus/pmbus_core.c | 2 ++

1 file changed, 2 insertions(+)
drivers/hwmon/pmbus/pmbus_core.c +2 -1
index 82c3754e21e3..f8ca36759b0a 100644
@@ -2014,6 +2014,8 @@ static int pmbus_init_common(struct i2c_client *client, struct pmbus_data *data,
 ret = i2c_smbus_read_byte_data(client, PMBUS_CAPABILITY);
 if (ret >= 0 && (ret & PB_CAPABILITY_ERROR_CHECK))
 client->flags |= I2C_CLIENT_PEC;
+ else
+ client->flags &= ~I2C_CLIENT_PEC;
 
 pmbus_clear_faults(client);
 
--

2.17.1

maintainer
#3 lore raw
Quoted 13 lines · wujek dev

------- Original Message -------

Quoted 11 lines · Guenter Roeck
Quoted 5 lines · Adam Wujek

Explicitly disable PEC when the client does not support it.
Without the explicit disable, when the device with the PEC support is removed later when a device without PEC support is inserted into the same address, the driver uses the old value of client->flags which contains the I2C_CLIENT_PEC flag. As a consequence the PEC is used when it should not.

Quoted 5 lines
Quoted 4 lines

How can that happen ? I would assume the I2C device gets deleted and re-created in that case, which should clear the PEC flag.

Guenter

In my case it was when I unloaded the driver for the I2C slave, changed the advertised PEC value in PMBUS_CAPABILITY register on slave. Then loaded the driver. When the switch was from disable->enable it worked as expected (this case was already covered), but when the PEC was set in the slave from enabled->disabled it was still using PEC to communicate.

So it is really the same device, only you unload the driver, change the device configuration (presumably with i2cset commands), and load it again. Please explain that in more detail in the commit description.

Thanks,
Guenter

maintainer
#4 lore raw
In reply to Adam Wujek
Quoted 16 lines · Adam Wujek

Explicitly disable PEC when the client does not support it.
Without the explicit disable, when the device with the PEC support is removed later when a device without PEC support is inserted into the same address, the driver uses the old value of client->flags which contains the I2C_CLIENT_PEC flag. As a consequence the PEC is used when it should not.

---
  drivers/hwmon/pmbus/pmbus_core.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/hwmon/pmbus/pmbus_core.c +0 -0
index 82c3754e21e3..f8ca36759b0a 100644

drivers/hwmon/pmbus/pmbus_core.c static int pmbus_init_common(struct i2c_client *client, struct pmbus_data *data, · line 2019

Quoted 6 lines
drivers/hwmon/pmbus/pmbus_core.c +2 -0
@@ -2014,6 +2014,8 @@ static int pmbus_init_common(struct i2c_client *client, struct pmbus_data *data,
  	ret = i2c_smbus_read_byte_data(client, PMBUS_CAPABILITY);
  	if (ret >= 0 && (ret & PB_CAPABILITY_ERROR_CHECK))
  		client->flags |= I2C_CLIENT_PEC;
+	else
+		client->flags &= ~I2C_CLIENT_PEC;
 

I just realized that this patch is not based on the latest kernel version. Please rebase.

Guenter

author
#5 lore raw

PATCH 1/1 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.

---
 drivers/hwmon/pmbus/pmbus_core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/hwmon/pmbus/pmbus_core.c +2 -1
index b2618b1d529e..0af7a3d74f47 100644
@@ -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

maintainer
#6 lore raw
Quoted 11 lines · Adam Wujek

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.

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

Quoted 9 lines
---
  drivers/hwmon/pmbus/pmbus_core.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/hwmon/pmbus/pmbus_core.c +0 -0
index b2618b1d529e..0af7a3d74f47 100644

drivers/hwmon/pmbus/pmbus_core.c static int pmbus_init_common(struct i2c_client *client, struct pmbus_data *data, · line 2338

Quoted 6 lines
drivers/hwmon/pmbus/pmbus_core.c +2 -1
@@ -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

Quoted 7 lines
drivers/hwmon/pmbus/pmbus_core.c +0 -1
  	/*
  	 * Check if the chip is write protected. If it is, we can not clear
--

2.25.1

author
#7 lore raw

PATCH v3 1/1 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.

---
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(+)
drivers/hwmon/pmbus/pmbus_core.c +3 -0
index b2618b1d529e..d93574d6a1fb 100644
@@ -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