hwmon: (spd5118) Select page 0 unconditionally during probe
PATCH v3 1/1 hwmon: (spd5118) Select page 0 unconditionally during probe
Some Intel i2c controllers can be configured by the BIOS to reject writes to the SPD device. This often causes problems when the register page needs to be changed, usually during resume.
Avoid probing on affected devices by unconditionally selecting page 0 by writing the SPD5118_REG_I2C_LEGACY_MODE register during probe. This will fail on affected controllers and thus prevent the driver from probing.
---
Changes since v2:
- restore original register content if i2c init fails
Changes since v1:
- Avoid zeroing reserved bits inside SPD5118_REG_I2C_LEGACY_MODE
--- drivers/hwmon/spd5118.c | 55 ++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 34 deletions(-)
drivers/hwmon/spd5118.c +21 -34
@@ -637,44 +637,31 @@ static int spd5118_i2c_init(struct i2c_client *client) I2C_FUNC_SMBUS_WORD_DATA))return -ENODEV ; - regval =i2c_smbus_read_word_swapped (client,SPD5118_REG_TYPE ); -if (regval <0 || (regval && regval !=0x5118 )) -return -ENODEV ; -/* -* If the device type registers return 0, it is possible that the chip -* has a non-zero page selected and takes the specification literally, +* We must first select page 0 to ensure that we can reliably read +* the volatile registers on chips that take the specification literally, * i.e. disables access to volatile registers besides the page register * if the page is not 0. The Renesas/ITD SPD5118 Hub Controller is known -* to show this behavior. Try to identify such chips. +* to show this behavior. +* +* We must also perform an unconditional register write to detect if +* the i2c controller blocks write accesses to the SPD device. Some Intel +* controllers might be configured by the BIOS to do this. */ -if (!regval) { -/* Vendor ID registers must also be 0 */ - regval =i2c_smbus_read_word_data (client,SPD5118_REG_VENDOR ); -if (regval) -return -ENODEV ; - -/* The selected page in MR11 must not be 0 */ - mode =i2c_smbus_read_byte_data (client,SPD5118_REG_I2C_LEGACY_MODE ); -if (mode <0 || (mode & ~SPD5118_LEGACY_MODE_MASK ) || - !(mode &SPD5118_LEGACY_PAGE_MASK )) -return -ENODEV ; - - err =i2c_smbus_write_byte_data (client,SPD5118_REG_I2C_LEGACY_MODE , - mode &SPD5118_LEGACY_MODE_ADDR ); -if (err) -return -ENODEV ; - -/* -* If the device type registers are still bad after selecting -* page 0, this is not a SPD5118 device. Restore original -* legacy mode register value and abort. -*/ - regval =i2c_smbus_read_word_swapped (client,SPD5118_REG_TYPE ); -if (regval !=0x5118 ) { -i2c_smbus_write_byte_data (client,SPD5118_REG_I2C_LEGACY_MODE , mode); -return -ENODEV ; - } + mode =i2c_smbus_read_byte_data (client,SPD5118_REG_I2C_LEGACY_MODE ); +if (mode <0 ) +return mode; + + err =i2c_smbus_write_byte_data (client,SPD5118_REG_I2C_LEGACY_MODE , + mode & ~SPD5118_LEGACY_PAGE_MASK ); +if (err <0 ) +return err; + + regval =i2c_smbus_read_word_swapped (client,SPD5118_REG_TYPE ); +if (regval !=0x5118 ) { +/* Restore original register content */ +i2c_smbus_write_byte_data (client,SPD5118_REG_I2C_LEGACY_MODE , mode); +return -ENODEV ; }/* We are reasonably sure that this is really a SPD5118 hub controller */
Signature · 1 line
2.39.5
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] Unconditional write during I2C probe permanently corrupts standard EEPROMs sharing the SPD…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] Unconditional write during I2C probe permanently corrupts standard EEPROMs sharing the SPD address space. --
commit e2971449a7fa80893d9c9314c59edd4a92223f9c
Author: Armin Wolf <[email protected]>
hwmon: (spd5118) Select page 0 unconditionally during probe
Some Intel i2c controllers can be configured by the BIOS to reject writes to the SPD device. This commit avoids probing on affected devices by unconditionally selecting page 0 by writing the SPD5118_REG_I2C_LEGACY_MODE register during probe.
Quoted 5 lines
drivers/hwmon/spd5118.c +0
-0@@ -637,44 +637,31 @@ static int spd5118_i2c_init(struct i2c_client *client)
[ ... ]
drivers/hwmon/spd5118.c static int spd5118_i2c_init(struct i2c_client *client) · line 656
Quoted 6 lines
drivers/hwmon/spd5118.c +6
-0+ mode =i2c_smbus_read_byte_data (client,SPD5118_REG_I2C_LEGACY_MODE ); +if (mode <0 ) +return mode; + + err =i2c_smbus_write_byte_data (client,SPD5118_REG_I2C_LEGACY_MODE , + mode & ~SPD5118_LEGACY_PAGE_MASK );
[Severity: Critical]
Does this unconditional write risk corrupting standard EEPROMs that share the SPD address space (0x50-0x57)?
Writing to offset 0x0B of a standard EEPROM (such as a DDR4 SPD) before the device identity is verified modifies its contents, potentially rendering the memory module unrecognizable to the system BIOS.
This also initiates a self-timed write cycle (typically 5ms) which puts the device into a busy state where it NAKs all subsequent I2C transactions.
drivers/hwmon/spd5118.c static int spd5118_i2c_init(struct i2c_client *client) · line 663
Quoted 7 lines
drivers/hwmon/spd5118.c +7
-0+if (err <0 ) +return err; + + regval =i2c_smbus_read_word_swapped (client,SPD5118_REG_TYPE ); +if (regval !=0x5118 ) { +/* Restore original register content */ +i2c_smbus_write_byte_data (client,SPD5118_REG_I2C_LEGACY_MODE , mode);
[Severity: Critical]
Does this restore attempt silently fail on standard EEPROMs?
Since the initial write puts a standard EEPROM into a busy write cycle where it NAKs transactions, the subsequent read of SPD5118_REG_TYPE will fail.
The code then reaches this point to restore the original value, but because the EEPROM is still busy, this write is also NAKed and silently fails. The probe then exits with -ENODEV, leaving the EEPROM permanently corrupted.
Quoted 2 lines
drivers/hwmon/spd5118.c +1
-0+return -ENODEV ; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Guenter Roeck
maintainerQuoted 25 lines · Armin Wolf
Some Intel i2c controllers can be configured by the BIOS to reject writes to the SPD device. This often causes problems when the register page needs to be changed, usually during resume.
Avoid probing on affected devices by unconditionally selecting page 0 by writing the
SPD5118_REG_I2C_LEGACY_MODEregister during probe. This will fail on affected controllers and thus prevent the driver from probing.Signed-off-by Armin Wolf <[email protected]>---
Changes since v2:
- restore original register content if i2c init failsChanges since v1:
- Avoid zeroing reserved bits insideSPD5118_REG_I2C_LEGACY_MODE--- drivers/hwmon/spd5118.c | 55 ++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 34 deletions(-)drivers/hwmon/spd5118.c +0
-0
drivers/hwmon/spd5118.c static int spd5118_i2c_init(struct i2c_client *client) · line 643
Quoted 7 lines
drivers/hwmon/spd5118.c +0
-4@@ -637,44 +637,31 @@ static int spd5118_i2c_init(struct i2c_client *client) I2C_FUNC_SMBUS_WORD_DATA))return -ENODEV ; - regval =i2c_smbus_read_word_swapped (client,SPD5118_REG_TYPE ); -if (regval <0 || (regval && regval !=0x5118 )) -return -ENODEV ; -
Thinking about it, Sashiko has a point here. I think we should keep the above code to have an early exit point.
Quoted 35 lines
drivers/hwmon/spd5118.c +7
-24/* -* If the device type registers return 0, it is possible that the chip -* has a non-zero page selected and takes the specification literally, +* We must first select page 0 to ensure that we can reliably read +* the volatile registers on chips that take the specification literally, * i.e. disables access to volatile registers besides the page register * if the page is not 0. The Renesas/ITD SPD5118 Hub Controller is known -* to show this behavior. Try to identify such chips. +* to show this behavior. +* +* We must also perform an unconditional register write to detect if +* the i2c controller blocks write accesses to the SPD device. Some Intel +* controllers might be configured by the BIOS to do this. */ -if (!regval) { -/* Vendor ID registers must also be 0 */ - regval =i2c_smbus_read_word_data (client,SPD5118_REG_VENDOR ); -if (regval) -return -ENODEV ; - -/* The selected page in MR11 must not be 0 */ - mode =i2c_smbus_read_byte_data (client,SPD5118_REG_I2C_LEGACY_MODE ); -if (mode <0 || (mode & ~SPD5118_LEGACY_MODE_MASK ) || - !(mode &SPD5118_LEGACY_PAGE_MASK )) -return -ENODEV ; - - err =i2c_smbus_write_byte_data (client,SPD5118_REG_I2C_LEGACY_MODE , - mode &SPD5118_LEGACY_MODE_ADDR ); -if (err) -return -ENODEV ; - -/* -* If the device type registers are still bad after selecting -* page 0, this is not a SPD5118 device. Restore original -* legacy mode register value and abort.
drivers/hwmon/spd5118.c static int spd5118_i2c_init(struct i2c_client *client) · line 654
Quoted 10 lines
drivers/hwmon/spd5118.c +4
-6-*/ - regval =i2c_smbus_read_word_swapped (client,SPD5118_REG_TYPE ); -if (regval !=0x5118 ) { -i2c_smbus_write_byte_data (client,SPD5118_REG_I2C_LEGACY_MODE , mode); -return -ENODEV ; - } + mode =i2c_smbus_read_byte_data (client,SPD5118_REG_I2C_LEGACY_MODE ); +if (mode <0 ) +return mode; +
I also wonder if we should strengthen this and bail out if any of bit 3..7 is set. 2-byte addressing is known not to work in the first place, and bit 4..7 are reserved in the supported version of the standard and should always be 0.
Also, if we keep above check, we should only only get here if the page is != 0. That means the register value has to be 0x01..0x07. So we could bail out with
if (!mode || (mode & ~SPD5118_LEGACY_PAGE_MASK)) return -ENODEV; followed by err = i2c_smbus_write_byte_data(client, SPD5118_REG_I2C_LEGACY_MODE, 0);
Thoughts ?
Thanks,
Guenter
Quoted 13 lines
drivers/hwmon/spd5118.c +10
-0+ err =i2c_smbus_write_byte_data (client,SPD5118_REG_I2C_LEGACY_MODE , + mode & ~SPD5118_LEGACY_PAGE_MASK ); +if (err <0 ) +return err; + + regval =i2c_smbus_read_word_swapped (client,SPD5118_REG_TYPE ); +if (regval !=0x5118 ) { +/* Restore original register content */ +i2c_smbus_write_byte_data (client,SPD5118_REG_I2C_LEGACY_MODE , mode); +return -ENODEV ; }/* We are reasonably sure that this is really a SPD5118 hub controller */
Am 01.09.26 um 00:22 schrieb Guenter Roeck:
Quoted 86 lines · Armin Wolf
Quoted 25 lines
Some Intel i2c controllers can be configured by the BIOS to reject writes to the SPD device. This often causes problems when the register page needs to be changed, usually during resume.
Avoid probing on affected devices by unconditionally selecting page 0 by writing the
SPD5118_REG_I2C_LEGACY_MODEregister during probe. This will fail on affected controllers and thus prevent the driver from probing.Signed-off-by Armin Wolf <[email protected]>---
Changes since v2:
- restore original register content if i2c init failsChanges since v1:
- Avoid zeroing reserved bits insideSPD5118_REG_I2C_LEGACY_MODE
---
drivers/hwmon/spd5118.c| 55 ++++++++++++++++-------------------------1 file changed, 21 insertions(+), 34 deletions(-)drivers/hwmon/spd5118.c +0
-0drivers/hwmon/spd5118.c static int spd5118_i2c_init(struct i2c_client · line 643
Quoted 7 lines
drivers/hwmon/spd5118.c +0
-3@@ -637,44 +637,31 @@ static int spd5118_i2c_init(struct i2c_client *client) I2C_FUNC_SMBUS_WORD_DATA)) return -ENODEV; - regval = i2c_smbus_read_word_swapped(client, SPD5118_REG_TYPE); - if (regval < 0 || (regval && regval != 0x5118)) - return -ENODEV; -Thinking about it, Sashiko has a point here. I think we should keep the above code to have an early exit point.
Quoted 50 lines
drivers/hwmon/spd5118.c +0
-1/* -* If the device type registers return 0, it is possible that the chip
- * has a non-zero page selected and takes the specification literally,
+ * We must first select page 0 to ensure that we can reliably read + * the volatile registers on chips that take the specification literally,
* i.e. disables access to volatile registers besides the page register
* if the page is not 0. The Renesas/ITD SPD5118 Hub Controller is known - * to show this behavior. Try to identify such chips.
+ * to show this behavior.
+ *
+ * We must also perform an unconditional register write to detect if
+ * the i2c controller blocks write accesses to the SPD device. Some Intel
+ * controllers might be configured by the BIOS to do this. */
- if (!regval) {
- /* Vendor ID registers must also be 0 */
- regval =i2c_smbus_read_word_data(client, SPD5118_REG_VENDOR); - if (regval)
- return-ENODEV;
-
- /* The selected page in MR11 must not be 0 */
- mode =i2c_smbus_read_byte_data(client, SPD5118_REG_I2C_LEGACY_MODE);
- if (mode < 0 || (mode & ~SPD5118_LEGACY_MODE_MASK) ||
- !(mode &SPD5118_LEGACY_PAGE_MASK))
- return-ENODEV;
-
- err = i2c_smbus_write_byte_data(client,SPD5118_REG_I2C_LEGACY_MODE,
- mode &SPD5118_LEGACY_MODE_ADDR);
- if (err)
- return-ENODEV;
-
- /*
- * If the device type registers are still bad after selecting - * page 0, this is not a SPD5118 device. Restore original - * legacy mode register value and abort.
- */
- regval =i2c_smbus_read_word_swapped(client, SPD5118_REG_TYPE); - if (regval !=0x5118) {
-i2c_smbus_write_byte_data(client, SPD5118_REG_I2C_LEGACY_MODE, mode);
- return-ENODEV;
- }
+ mode = i2c_smbus_read_byte_data(client,
Quoted 9 lines
Quoted 4 lines
SPD5118_REG_I2C_LEGACY_MODE);
+ if (mode < 0)
+ return mode;
+I also wonder if we should strengthen this and bail out if any of bit 3..7 is set. 2-byte addressing is known not to work in the first place, and bit 4..7 are reserved in the supported version of the standard and should always be 0.
This would prevent the driver from being forward-compatible to newer standard revisions.
Quoted 9 lines
Also, if we keep above check, we should only only get here if the page is != 0. That means the register value has to be 0x01..0x07. So we could bail out with
if (!mode || (mode & ~SPD5118_LEGACY_PAGE_MASK))
return-ENODEV;
followed by
err =i2c_smbus_write_byte_data(client, SPD5118_REG_I2C_LEGACY_MODE, 0);Thoughts ?
Honestly i think that users should rely on the detection feature if they are not sure if a given device is really a spd5118 device. Manually instantiating a spd5118 device on a given bus address tells the kernel that there _is_ a valid spd5118 device at this address.
Otherwise we would basically duplicate parts of the detect callback inside spd5118_i2c_init().
Thanks,
Armin Wolf
Quoted 19 lines
Thanks,
GuenterQuoted 14 lines
+ err = i2c_smbus_write_byte_data(client,
SPD5118_REG_I2C_LEGACY_MODE,
+ mode & ~SPD5118_LEGACY_PAGE_MASK);
+ if (err < 0)
+ return err;
+
+ regval =i2c_smbus_read_word_swapped(client, SPD5118_REG_TYPE); + if (regval !=0x5118) {
+ /* Restore original register content */
+i2c_smbus_write_byte_data(client, SPD5118_REG_I2C_LEGACY_MODE, mode);
+ return-ENODEV;
}
/* We are reasonably sure that this is really a SPD5118 hub controller */
Guenter Roeck
maintainerQuoted 87 lines · Armin Wolf
Am 01.09.26 um 00:22 schrieb Guenter Roeck:
Quoted 76 lines · Armin Wolf
Quoted 25 lines
Some Intel i2c controllers can be configured by the BIOS to reject writes to the SPD device. This often causes problems when the register page needs to be changed, usually during resume.
Avoid probing on affected devices by unconditionally selecting page 0 by writing the
SPD5118_REG_I2C_LEGACY_MODEregister during probe. This will fail on affected controllers and thus prevent the driver from probing.Signed-off-by Armin Wolf <[email protected]>---
Changes since v2:
- restore original register content if i2c init failsChanges since v1:
- Avoid zeroing reserved bits insideSPD5118_REG_I2C_LEGACY_MODE
---
drivers/hwmon/spd5118.c| 55 ++++++++++++++++-------------------------1 file changed, 21 insertions(+), 34 deletions(-)drivers/hwmon/spd5118.c +0
-0drivers/hwmon/spd5118.c static int spd5118_i2c_init(struct i2c_client *client) · line 642
Quoted 6 lines
drivers/hwmon/spd5118.c +0
-3@@ -637,44 +637,31 @@ static int spd5118_i2c_init(struct i2c_client *client) I2C_FUNC_SMBUS_WORD_DATA)) return -ENODEV; - regval = i2c_smbus_read_word_swapped(client, SPD5118_REG_TYPE); - if (regval < 0 || (regval && regval != 0x5118)) - return -ENODEV; -Thinking about it, Sashiko has a point here. I think we should keep the above code to have an early exit point.
Quoted 41 lines
drivers/hwmon/spd5118.c +7
-30/* - * If the device type registers return 0, it is possible that the chip - * has a non-zero page selected and takes the specification literally, +* We must first select page 0 to ensure that we can reliably read +* the volatile registers on chips that take the specification literally, * i.e. disables access to volatile registers besides the page register * if the page is not 0. The Renesas/ITD SPD5118 Hub Controller is known - * to show this behavior. Try to identify such chips. +* to show this behavior. +* +* We must also perform an unconditional register write to detect if +* the i2c controller blocks write accesses to the SPD device. Some Intel +* controllers might be configured by the BIOS to do this. */ - if (!regval) { - /* Vendor ID registers must also be 0 */ - regval = i2c_smbus_read_word_data(client, SPD5118_REG_VENDOR); - if (regval) - return -ENODEV; - - /* The selected page in MR11 must not be 0 */ - mode = i2c_smbus_read_byte_data(client, SPD5118_REG_I2C_LEGACY_MODE); - if (mode < 0 || (mode & ~SPD5118_LEGACY_MODE_MASK) || - !(mode & SPD5118_LEGACY_PAGE_MASK)) - return -ENODEV; - - err = i2c_smbus_write_byte_data(client, SPD5118_REG_I2C_LEGACY_MODE, - mode & SPD5118_LEGACY_MODE_ADDR); - if (err) - return -ENODEV; - - /* - * If the device type registers are still bad after selecting - * page 0, this is not a SPD5118 device. Restore original - * legacy mode register value and abort. - */ - regval = i2c_smbus_read_word_swapped(client, SPD5118_REG_TYPE); - if (regval != 0x5118) { - i2c_smbus_write_byte_data(client, SPD5118_REG_I2C_LEGACY_MODE, mode); - return -ENODEV; - }Quoted 9 lines
drivers/hwmon/spd5118.c static int spd5118_i2c_init(struct i2c_client *client) · line 654
Quoted 4 lines
drivers/hwmon/spd5118.c +4
-0+ mode = i2c_smbus_read_byte_data(client, SPD5118_REG_I2C_LEGACY_MODE); + if (mode < 0) + return mode; +I also wonder if we should strengthen this and bail out if any of bit 3..7 is set. 2-byte addressing is known not to work in the first place, and bit 4..7 are reserved in the supported version of the standard and should always be 0.
Quoted 2 lines
This would prevent the driver from being forward-compatible to newer standard revisions.
Good point.
Quoted 8 lines
Quoted 8 lines
Also, if we keep above check, we should only only get here if the page is != 0. That means the register value has to be 0x01..0x07. So we could bail out with
if (!mode || (mode & ~SPD5118_LEGACY_PAGE_MASK))
return-ENODEV;
followed by
err =i2c_smbus_write_byte_data(client, SPD5118_REG_I2C_LEGACY_MODE, 0);
Quoted 9 lines
Quoted 1 line
Thoughts ?
Honestly i think that users should rely on the detection feature if they are not sure if a given device is really a spd5118 device. Manually instantiating a spd5118 device on a given bus address tells the kernel that there _is_ a valid spd5118 device at this address.
Otherwise we would basically duplicate parts of the detect callback inside
spd5118_i2c_init().
This _is_ the detect callback inside spd5118_i2c_init(). Problem is that the device currently instantiates if bit 3 of MR11 is set, even though it is known not to work. Prior to commit 7bf5a11dde2c ("hwmon: spd5118: Remove 16-bit addressing") it would at least bail out if the controller does not support I2C_FUNC_I2C. That means we'll at least need to check for bit 3 (SPD5118_LEGACY_MODE_ADDR) and bail out if the bit is set.
Thanks,
Guenter
Am 01.09.26 um 18:35 schrieb Guenter Roeck:
Quoted 131 lines · Armin Wolf
Quoted 101 lines
Am 01.09.26 um 00:22 schrieb Guenter Roeck:
Quoted 90 lines · Armin Wolf
Quoted 26 lines
Some Intel i2c controllers can be configured by the BIOS to reject writes to the SPD device. This often causes problems when the register page needs to be changed, usually during resume.
Avoid probing on affected devices by unconditionally selecting page 0 by writing the
SPD5118_REG_I2C_LEGACY_MODEregister during probe. This will fail on affected controllers and thus prevent the driver from probing.Signed-off-by Armin Wolf <[email protected]>---
Changes since v2:
- restore original register content if i2c init failsChanges since v1:
- Avoid zeroing reserved bits insideSPD5118_REG_I2C_LEGACY_MODE
---
drivers/hwmon/spd5118.c| 55
++++++++++++++++-------------------------1 file changed, 21 insertions(+), 34 deletions(-)drivers/hwmon/spd5118.c +0
-0drivers/hwmon/spd5118.c static int spd5118_i2c_init(struct i2c_client · line 643
Quoted 7 lines
drivers/hwmon/spd5118.c +0
-3@@ -637,44 +637,31 @@ static int spd5118_i2c_init(struct i2c_client *client) I2C_FUNC_SMBUS_WORD_DATA)) return -ENODEV; - regval = i2c_smbus_read_word_swapped(client, SPD5118_REG_TYPE); - if (regval < 0 || (regval && regval != 0x5118)) - return -ENODEV; -Thinking about it, Sashiko has a point here. I think we should keep the above code to have an early exit point.
Quoted 53 lines
drivers/hwmon/spd5118.c +0
-1/* -* If the device type registers return 0, it is possible that the chip
- * has a non-zero page selected and takes the specification literally,
+ * We must first select page 0 to ensure that we can reliably read
+ * the volatile registers on chips that take the specification literally,
* i.e. disables access to volatile registers besides the page register
* if the page is not 0. The Renesas/ITD SPD5118 Hub Controller is known - * to show this behavior. Try to identify such chips.
+ * to show this behavior.
+ *
+ * We must also perform an unconditional register write to detect if
+ * the i2c controller blocks write accesses to the SPD device. Some Intel
+ * controllers might be configured by the BIOS to do this. */
- if (!regval) {
- /* Vendor ID registers must also be 0 */
- regval =i2c_smbus_read_word_data(client, SPD5118_REG_VENDOR);
- if (regval)
- return-ENODEV;
-
- /* The selected page in MR11 must not be 0 */
- mode =i2c_smbus_read_byte_data(client, SPD5118_REG_I2C_LEGACY_MODE);
- if (mode < 0 || (mode & ~SPD5118_LEGACY_MODE_MASK) ||
- !(mode &SPD5118_LEGACY_PAGE_MASK))
- return-ENODEV;
-
- err = i2c_smbus_write_byte_data(client,SPD5118_REG_I2C_LEGACY_MODE,
- mode &SPD5118_LEGACY_MODE_ADDR);
- if (err)
- return-ENODEV;
-
- /*
- * If the device type registers are still bad after selecting - * page 0, this is not a SPD5118 device. Restore original - * legacy mode register value and abort.
- */
- regval =i2c_smbus_read_word_swapped(client, SPD5118_REG_TYPE);
- if (regval !=0x5118) {
-i2c_smbus_write_byte_data(client, SPD5118_REG_I2C_LEGACY_MODE, mode);
- return-ENODEV;
- }
+ mode = i2c_smbus_read_byte_data(client,Quoted 9 lines
Quoted 4 lines
SPD5118_REG_I2C_LEGACY_MODE);
+ if (mode < 0)
+ return mode;
+I also wonder if we should strengthen this and bail out if any of bit 3..7 is set. 2-byte addressing is known not to work in the first place, and bit 4..7 are reserved in the supported version of the standard and should always be 0.
Quoted 2 lines
This would prevent the driver from being forward-compatible to newer standard revisions.
Good point.
Quoted 8 lines
Quoted 8 lines
Also, if we keep above check, we should only only get here if the page is != 0. That means the register value has to be 0x01..0x07. So we could bail out with
if (!mode || (mode & ~SPD5118_LEGACY_PAGE_MASK))
return-ENODEV;
followed by
err =i2c_smbus_write_byte_data(client, SPD5118_REG_I2C_LEGACY_MODE, 0);Quoted 9 lines
Quoted 1 line
Thoughts ?
Honestly i think that users should rely on the detection feature if they are not sure if a given device is really a spd5118 device. Manually instantiating a spd5118 device on a given bus address tells the kernel that there _is_ a valid spd5118 device at this address.
Otherwise we would basically duplicate parts of the detect callback inside
spd5118_i2c_init().This _is_ the detect callback inside
spd5118_i2c_init(). Problem is that the device currently instantiates if bit 3 of MR11 is set, even though it is known not to work. Prior to commit7bf5a11dde2c("hwmon: spd5118: Remove 16-bit addressing") it would at least bail out if the controller does not supportI2C_FUNC_I2C. That means we'll at least need to check for bit 3 (SPD5118_LEGACY_MODE_ADDR) and bail out if the bit is set.
Quoted 2 lines
Thanks,
Guenter
Good point, we can indeed check for that. But the early exit point suggested by Sashiko is a bit overblown IMHO.
Thanks,
Armin Wolf
Guenter Roeck
maintainerQuoted 119 lines · Armin Wolf
Am 01.09.26 um 18:35 schrieb Guenter Roeck:
Quoted 117 lines · Armin Wolf
Quoted 87 lines
Am 01.09.26 um 00:22 schrieb Guenter Roeck:
Quoted 76 lines · Armin Wolf
Quoted 25 lines
Some Intel i2c controllers can be configured by the BIOS to reject writes to the SPD device. This often causes problems when the register page needs to be changed, usually during resume.
Avoid probing on affected devices by unconditionally selecting page 0 by writing the
SPD5118_REG_I2C_LEGACY_MODEregister during probe. This will fail on affected controllers and thus prevent the driver from probing.Signed-off-by Armin Wolf <[email protected]>---
Changes since v2:
- restore original register content if i2c init failsChanges since v1:
- Avoid zeroing reserved bits insideSPD5118_REG_I2C_LEGACY_MODE
---
drivers/hwmon/spd5118.c| 55 ++++++++++++++++-------------------------1 file changed, 21 insertions(+), 34 deletions(-)drivers/hwmon/spd5118.c +0
-0drivers/hwmon/spd5118.c static int spd5118_i2c_init(struct i2c_client *client) · line 642
Quoted 6 lines
drivers/hwmon/spd5118.c +0
-3@@ -637,44 +637,31 @@ static int spd5118_i2c_init(struct i2c_client *client) I2C_FUNC_SMBUS_WORD_DATA)) return -ENODEV; - regval = i2c_smbus_read_word_swapped(client, SPD5118_REG_TYPE); - if (regval < 0 || (regval && regval != 0x5118)) - return -ENODEV; -Thinking about it, Sashiko has a point here. I think we should keep the above code to have an early exit point.
Quoted 41 lines
drivers/hwmon/spd5118.c +7
-30/* - * If the device type registers return 0, it is possible that the chip - * has a non-zero page selected and takes the specification literally, +* We must first select page 0 to ensure that we can reliably read +* the volatile registers on chips that take the specification literally, * i.e. disables access to volatile registers besides the page register * if the page is not 0. The Renesas/ITD SPD5118 Hub Controller is known - * to show this behavior. Try to identify such chips. +* to show this behavior. +* +* We must also perform an unconditional register write to detect if +* the i2c controller blocks write accesses to the SPD device. Some Intel +* controllers might be configured by the BIOS to do this. */ - if (!regval) { - /* Vendor ID registers must also be 0 */ - regval = i2c_smbus_read_word_data(client, SPD5118_REG_VENDOR); - if (regval) - return -ENODEV; - - /* The selected page in MR11 must not be 0 */ - mode = i2c_smbus_read_byte_data(client, SPD5118_REG_I2C_LEGACY_MODE); - if (mode < 0 || (mode & ~SPD5118_LEGACY_MODE_MASK) || - !(mode & SPD5118_LEGACY_PAGE_MASK)) - return -ENODEV; - - err = i2c_smbus_write_byte_data(client, SPD5118_REG_I2C_LEGACY_MODE, - mode & SPD5118_LEGACY_MODE_ADDR); - if (err) - return -ENODEV; - - /* - * If the device type registers are still bad after selecting - * page 0, this is not a SPD5118 device. Restore original - * legacy mode register value and abort. - */ - regval = i2c_smbus_read_word_swapped(client, SPD5118_REG_TYPE); - if (regval != 0x5118) { - i2c_smbus_write_byte_data(client, SPD5118_REG_I2C_LEGACY_MODE, mode); - return -ENODEV; - }Quoted 9 lines
drivers/hwmon/spd5118.c static int spd5118_i2c_init(struct i2c_client *client) · line 654
Quoted 4 lines
drivers/hwmon/spd5118.c +4
-0+ mode = i2c_smbus_read_byte_data(client, SPD5118_REG_I2C_LEGACY_MODE); + if (mode < 0) + return mode; +I also wonder if we should strengthen this and bail out if any of bit 3..7 is set. 2-byte addressing is known not to work in the first place, and bit 4..7 are reserved in the supported version of the standard and should always be 0.
Quoted 2 lines
This would prevent the driver from being forward-compatible to newer standard revisions.
Good point.
Quoted 8 lines
Quoted 8 lines
Also, if we keep above check, we should only only get here if the page is != 0. That means the register value has to be 0x01..0x07. So we could bail out with
if (!mode || (mode & ~SPD5118_LEGACY_PAGE_MASK))
return-ENODEV;
followed by
err =i2c_smbus_write_byte_data(client, SPD5118_REG_I2C_LEGACY_MODE, 0);Quoted 9 lines
Quoted 1 line
Thoughts ?
Honestly i think that users should rely on the detection feature if they are not sure if a given device is really a spd5118 device. Manually instantiating a spd5118 device on a given bus address tells the kernel that there _is_ a valid spd5118 device at this address.
Otherwise we would basically duplicate parts of the detect callback inside
spd5118_i2c_init().This _is_ the detect callback inside
spd5118_i2c_init(). Problem is that the device currently instantiates if bit 3 of MR11 is set, even though it is known not to work. Prior to commit7bf5a11dde2c("hwmon: spd5118: Remove 16-bit addressing") it would at least bail out if the controller does not supportI2C_FUNC_I2C. That means we'll at least need to check for bit 3 (SPD5118_LEGACY_MODE_ADDR) and bail out if the bit is set.
Quoted 6 lines
Quoted 2 lines
Thanks,
GuenterGood point, we can indeed check for that. But the early exit point suggested by Sashiko is a bit overblown IMHO.
I disagree. This is a critical component, and overwriting SPD data would be fatal. The additional read may be a nuisance, but does not hurt. One thing we could do though is to only read SPD5118_REG_TYPE again if the first read failed.
Thanks,
Guenter
Quoted 2 lines
Thanks,
Armin Wolf