Skip to contentSkip to conversation

i2c: stub: Add support for SMBus block commands

PATCH Superseded Guenter Roeck 4 participants 15 messages Jul 7, 2014 – Jul 17, 2014

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 i2c: stub: Add support for SMBus block commands

SMBus block commands are different to I2C block commands since the returned data is not normally accessible with byte or word commands on other command offsets. Add linked list of 'block' commands to support those commands.

Access mechanism is quite simple: Block commands must be written before they can be read. The first write selects the block length. Subsequent writes can be partial. Block read commands always return the number of bytes selected with the first write.

---
v2: Make new functionality only available on request via functionality
module parameter
Add more details about SMBus block mode support to documentation Use correct sizeof() variable in devm_kzalloc
Use stub_find_block() only in SMBus block command itself. Store first word of block data in chip->words[].
When writing block data and the written data is longer than the first write, bail out with debug message indicating the reason for the error.

 Documentation/i2c/i2c-stub |  9 ++++-
 drivers/i2c/i2c-stub.c     | 99 +++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 101 insertions(+), 7 deletions(-)
Documentation/i2c/i2c-stub +7 -2
index fa4b669..531510b 100644
@@ -2,9 +2,9 @@ MODULE: i2c-stub
 
 DESCRIPTION:
 
-This module is a very simple fake I2C/SMBus driver.  It implements five
+This module is a very simple fake I2C/SMBus driver.  It implements six
 types of SMBus commands: write quick, (r/w) byte, (r/w) byte data, (r/w)
-word data, and (r/w) I2C block data.
+word data, (r/w) I2C block data, and (r/w) SMBus block data.
 
 You need to provide chip addresses as a module parameter when loading this
 driver, which will then only react to SMBus commands to these addresses.
@@ -19,6 +19,11 @@ A pointer register with auto-increment is implemented for all byte
 operations.  This allows for continuous byte reads like those supported by
 EEPROMs, among others.
 
+SMBus block commands must be written to configure an SMBus command for
+SMBus block operations. The first SMBus block write selects the block length.
+Subsequent writes can be partial. Block read commands always return
+the number of bytes selected with the first write.
+
 The typical use-case is like this:
 	1. load this module
 	2. use i2cset (from the i2c-tools project) to pre-load some data
drivers/i2c/i2c-stub.c +94 -5

148 lines not on this page · raw message

Signature · 1 line
1.9.1
Reviewed-by on v2 1/1
#1 lore raw

Hi Guenter,

Quoted 11 lines · Guenter Roeck

SMBus block commands are different to I2C block commands since the returned data is not normally accessible with byte or word commands on other command offsets. Add linked list of 'block' commands to support those commands.

Access mechanism is quite simple: Block commands must be written before they can be read. The first write selects the block length. Subsequent writes can be partial. Block read commands always return the number of bytes selected with the first write.

Quoted 10 lines

---
v2: Make new functionality only available on request via functionality
module parameter
Add more details about SMBus block mode support to documentation Use correct sizeof() variable in devm_kzalloc
Use stub_find_block() only in SMBus block command itself. Store first word of block data in chip->words[].
When writing block data and the written data is longer than the first write, bail out with debug message indicating the reason for the error.

Looks good, thanks for the quick update.

Just one thing I have been thinking about while reviewing the updated code... You decided to make the first SMBus block write select the maximum block length, and you always use that for SMBus block reads. However you accept partial writes. The fact that the order in which writes are performed has an effect on which writes are accepted is somewhat unexpected.

Wouldn't it make more sense to accept all SMBus block writes, regardless of the size (as long as it is within the limits of the SMBus standard, of course)? Then the only thing left to decide is whether SMBus block reads use the maximum size or the size of the most recent SMBus block write.

I suspect this would mimic the behavior of real chips better. What do you think?

Signature · 2 lines
Jean Delvare
SUSE L3 Support
#2 lore raw

Hi Jean,

Quoted 36 lines · Jean Delvare

Hi Guenter,

Quoted 11 lines · Guenter Roeck

SMBus block commands are different to I2C block commands since the returned data is not normally accessible with byte or word commands on other command offsets. Add linked list of 'block' commands to support those commands.

Access mechanism is quite simple: Block commands must be written before they can be read. The first write selects the block length. Subsequent writes can be partial. Block read commands always return the number of bytes selected with the first write.

Quoted 10 lines

---
v2: Make new functionality only available on request via functionality
module parameter
Add more details about SMBus block mode support to documentation Use correct sizeof() variable in devm_kzalloc
Use stub_find_block() only in SMBus block command itself. Store first word of block data in chip->words[].
When writing block data and the written data is longer than the first write, bail out with debug message indicating the reason for the error.

Looks good, thanks for the quick update.

Just one thing I have been thinking about while reviewing the updated code... You decided to make the first SMBus block write select the maximum block length, and you always use that for SMBus block reads. However you accept partial writes. The fact that the order in which writes are performed has an effect on which writes are accepted is somewhat unexpected.

Quoted 9 lines

Wouldn't it make more sense to accept all SMBus block writes, regardless of the size (as long as it is within the limits of the SMBus standard, of course)? Then the only thing left to decide is whether SMBus block reads use the maximum size or the size of the most recent SMBus block write.

I suspect this would mimic the behavior of real chips better. What do you think?

Not really sure what the expected behavior is. My original code accepted all writes and returned the most recent write, including the most recent write length. I thought this was untypical, and that it would be more typical for the chip to return a fixed length. But ultimately I don't really know, and I am fine either way.

Guenter

#3 lore raw

Hi Guenter,

Quoted 14 lines · Guenter Roeck
Quoted 13 lines · Jean Delvare

Just one thing I have been thinking about while reviewing the updated code... You decided to make the first SMBus block write select the maximum block length, and you always use that for SMBus block reads. However you accept partial writes. The fact that the order in which writes are performed has an effect on which writes are accepted is somewhat unexpected.

Wouldn't it make more sense to accept all SMBus block writes, regardless of the size (as long as it is within the limits of the SMBus standard, of course)? Then the only thing left to decide is whether SMBus block reads use the maximum size or the size of the most recent SMBus block write.

Quoted 8 lines
Quoted 2 lines

I suspect this would mimic the behavior of real chips better. What do you think?

Not really sure what the expected behavior is. My original code accepted all writes and returned the most recent write, including the most recent write length. I thought this was untypical, and that it would be more typical for the chip to return a fixed length. But ultimately I don't really know, and I am fine either way.

I agree that different chips may behave differently and it is not possible for i2c-stub to please everyone. However I do not think that the current implementation mimics any actual chip behavior. So we might as well switch to something more simple and more likely to please at least one device driver:

From: Jean Delvare <[email protected]>
Subject: i2c-stub: Allow the increasing SMBus block write length

This is no good reason to not allow SMBus block writes longer than the first one was. Lift this limitation, this makes the code more simple.

---
 Documentation/i2c/i2c-stub |    5 ++---
 drivers/i2c/i2c-stub.c     |   12 +++---------
 2 files changed, 5 insertions(+), 12 deletions(-)
Documentation/i2c/i2c-stub +2 -3
@@ -20,9 +20,8 @@ operations.  This allows for continuous
 EEPROMs, among others.
 
 SMBus block commands must be written to configure an SMBus command for
-SMBus block operations. The first SMBus block write selects the block length.
-Subsequent writes can be partial. Block read commands always return
-the number of bytes selected with the first write.
+SMBus block operations. Writes can be partial. Block read commands always
+return the number of bytes selected with the largest write so far.
 
 The typical use-case is like this:
 	1. load this module
drivers/i2c/i2c-stub.c +3 -9
@@ -254,13 +254,6 @@ static s32 stub_xfer(struct i2c_adapter
 				ret = -EINVAL;
 				break;
 			}
-			if (b && len > b->len) {
-				dev_dbg(&adap->dev,
-					"Attempt to write more data (%d) than with initial SMBus block write (%d)\n",
-					len, b->len);
-				ret = -EINVAL;
-				break;
-			}
 			if (b == NULL) {
 				b = stub_find_block(&adap->dev, chip, command,
 						    true);
@@ -268,9 +261,10 @@ static s32 stub_xfer(struct i2c_adapter
 					ret = -ENOMEM;
 					break;
 				}
-				/* First write sets block length */
-				b->len = len;
 			}
+			/* Largest write sets read block length */
+			if (len > b->len)
+				b->len = len;
 			for (i = 0; i < len; i++)
 				b->block[i] = data->block[i + 1];
 			/* update for byte and word commands */

Would that work for you?

Signature · 2 lines
Jean Delvare
SUSE L3 Support
author Reviewed-by
#4 lore raw
Quoted 89 lines · Jean Delvare

Hi Guenter,

Quoted 14 lines · Guenter Roeck
Quoted 13 lines · Jean Delvare

Just one thing I have been thinking about while reviewing the updated code... You decided to make the first SMBus block write select the maximum block length, and you always use that for SMBus block reads. However you accept partial writes. The fact that the order in which writes are performed has an effect on which writes are accepted is somewhat unexpected.

Wouldn't it make more sense to accept all SMBus block writes, regardless of the size (as long as it is within the limits of the SMBus standard, of course)? Then the only thing left to decide is whether SMBus block reads use the maximum size or the size of the most recent SMBus block write.

Quoted 8 lines
Quoted 2 lines

I suspect this would mimic the behavior of real chips better. What do you think?

Not really sure what the expected behavior is. My original code accepted all writes and returned the most recent write, including the most recent write length. I thought this was untypical, and that it would be more typical for the chip to return a fixed length. But ultimately I don't really know, and I am fine either way.

I agree that different chips may behave differently and it is not possible for i2c-stub to please everyone. However I do not think that the current implementation mimics any actual chip behavior. So we might as well switch to something more simple and more likely to please at least one device driver:

From: Jean Delvare <[email protected]>
Subject: i2c-stub: Allow the increasing SMBus block write length

This is no good reason to not allow SMBus block writes longer than the first one was. Lift this limitation, this makes the code more simple.

---
  Documentation/i2c/i2c-stub |    5 ++---
  drivers/i2c/i2c-stub.c     |   12 +++---------
  2 files changed, 5 insertions(+), 12 deletions(-)
Documentation/i2c/i2c-stub +2 -3
@@ -20,9 +20,8 @@ operations.  This allows for continuous
  EEPROMs, among others.
 
  SMBus block commands must be written to configure an SMBus command for
-SMBus block operations. The first SMBus block write selects the block length.
-Subsequent writes can be partial. Block read commands always return
-the number of bytes selected with the first write.
+SMBus block operations. Writes can be partial. Block read commands always
+return the number of bytes selected with the largest write so far.
 
  The typical use-case is like this:
  	1. load this module
drivers/i2c/i2c-stub.c +3 -9
@@ -254,13 +254,6 @@ static s32 stub_xfer(struct i2c_adapter
  				ret = -EINVAL;
  				break;
  			}
-			if (b && len > b->len) {
-				dev_dbg(&adap->dev,
-					"Attempt to write more data (%d) than with initial SMBus block write (%d)\n",
-					len, b->len);
-				ret = -EINVAL;
-				break;
-			}
  			if (b == NULL) {
  				b = stub_find_block(&adap->dev, chip, command,
  						    true);
@@ -268,9 +261,10 @@ static s32 stub_xfer(struct i2c_adapter
  					ret = -ENOMEM;
  					break;
  				}
-				/* First write sets block length */
-				b->len = len;
  			}
+			/* Largest write sets read block length */
+			if (len > b->len)
+				b->len = len;
  			for (i = 0; i < len; i++)
  				b->block[i] = data->block[i + 1];
  			/* update for byte and word commands */
Quoted 2 lines

Would that work for you?

Yes, sure, that works fine.

Thanks,
Guenter

#5 lore raw
In reply to Jean Delvare

Hi Jean,

Quoted 2 lines · Jean Delvare

Hi Guenter,

Something else:

Any idea how we could inject errors ? Error path testing would be quite useful.

Thanks,
Guenter

#6 lore raw

Hi Guenter,

Quoted 1 line · Guenter Roeck

Any idea how we could inject errors ? Error path testing would be quite useful.

Good idea. This should probably be done with a sysfs attribute so that it can be turned on and off as desired. Off by default, of course. Some other subsystems already support error injection, you could check how they are doing it, do that we do not diverge needlessly.

Do you think there is any value in failing with different error codes, or just -EIO is enough?

Do you think it should fail all the time when error injection is enabled, or is there a value in having only a certain % of commands fail?

Signature · 2 lines
Jean Delvare
SUSE L3 Support
#7 lore raw
Quoted 5 lines · Jean Delvare

Hi Guenter,

Quoted 1 line · Guenter Roeck

Any idea how we could inject errors ? Error path testing would be quite useful.

Quoted 8 lines

Good idea. This should probably be done with a sysfs attribute so that it can be turned on and off as desired. Off by default, of course. Some other subsystems already support error injection, you could check how they are doing it, do that we do not diverge needlessly.

Do you think there is any value in failing with different error codes, or just -EIO is enough?

How about writing the error code to return into the attribute ? Write anything negative, and it is returned as error. Write 0, and the driver works as normal.

Quoted 4 lines

Do you think it should fail all the time when error injection is enabled, or is there a value in having only a certain % of commands fail?

For my purposes I would want it to fail reliably. We could add some fanciness,
though: Provide a second attribute which specifies how many operations should pass before the first failure.

Thanks,
Guenter

#8 lore raw
Quoted 11 lines · Guenter Roeck
Quoted 10 lines · Jean Delvare

Hi Guenter,

Quoted 1 line · Guenter Roeck

Any idea how we could inject errors ? Error path testing would be quite useful.

Good idea. This should probably be done with a sysfs attribute so that it can be turned on and off as desired. Off by default, of course. Some other subsystems already support error injection, you could check how they are doing it, do that we do not diverge needlessly.

Quoted 6 lines
Quoted 2 lines

Do you think there is any value in failing with different error codes, or just -EIO is enough?

How about writing the error code to return into the attribute ? Write anything negative, and it is returned as error. Write 0, and the driver works as normal.

This is smart, I like it :)

Quoted 7 lines
Quoted 3 lines

Do you think it should fail all the time when error injection is enabled, or is there a value in having only a certain % of commands fail?

For my purposes I would want it to fail reliably. We could add some fanciness,
though: Provide a second attribute which specifies how many operations should pass before the first failure.

Let's start simple and just implement what you need.

Signature · 2 lines
Jean Delvare
SUSE L3 Support
#9 lore raw
Quoted 25 lines · Jean Delvare
Quoted 11 lines · Guenter Roeck
Quoted 10 lines · Jean Delvare

Hi Guenter,

Quoted 1 line · Guenter Roeck

Any idea how we could inject errors ? Error path testing would be quite useful.

Good idea. This should probably be done with a sysfs attribute so that it can be turned on and off as desired. Off by default, of course. Some other subsystems already support error injection, you could check how they are doing it, do that we do not diverge needlessly.

Quoted 6 lines
Quoted 2 lines

Do you think there is any value in failing with different error codes, or just -EIO is enough?

How about writing the error code to return into the attribute ? Write anything negative, and it is returned as error. Write 0, and the driver works as normal.

This is smart, I like it :)

Quoted 4 lines
Quoted 3 lines

Do you think it should fail all the time when error injection is enabled, or is there a value in having only a certain % of commands fail?

Quoted 6 lines
Quoted 3 lines

For my purposes I would want it to fail reliably. We could add some fanciness,
though: Provide a second attribute which specifies how many operations should pass before the first failure.

Let's start simple and just implement what you need.

I would actually benefit from both. The ability to return an error unconditionally lets me test the first error path. The ability to return an error starting with the n-th transfer lets me test the n-th error path.

Guenter

#10 lore raw

Forgive me for jumping in. I'm a noob at I2C. But I have built a couple substantial error injection frameworks over the years, one for a mainframe DBMS and one for Java's checked exceptions. So while I have nothing useful to say about how to inject exceptions here, I have thought a lot about use cases.

Failing all the time is the necessary first step. It allows for testing error paths without manually inserting a failure and recompiling, and makes it possible to build unit tests.

Failing randomly (or pseudo-randomly) is important for testing the overall recovery mechanism, particularly where you have an inherently unreliable subsystem like networks or I2C. By changing the failure rate you can explore, for example, at what point the failure rate of the lower level system becomes so great that it makes the upper level system unreliable.

The one use case I would add, and it may be outside the scope here, is data errors. I've been using the DDC protocol over I2C to communicate with monitors. The DDC Get Capabilities request entails multiple write/read exchanges, with responses of up to 37 bytes each. Most of the time this works ok, but I have one monitor that produces a high volume of data errors (double bytes or missing bytes). This is only detected by examining the data itself (fixed fields and checksum).

Sanford

Quoted 44 lines · Guenter Roeck
Quoted 25 lines · Jean Delvare
Quoted 11 lines · Guenter Roeck
Quoted 10 lines · Jean Delvare

Hi Guenter,

Quoted 1 line · Guenter Roeck

Any idea how we could inject errors ? Error path testing would be quite useful.

Good idea. This should probably be done with a sysfs attribute so that it can be turned on and off as desired. Off by default, of course. Some other subsystems already support error injection, you could check how they are doing it, do that we do not diverge needlessly.

Quoted 6 lines
Quoted 2 lines

Do you think there is any value in failing with different error codes, or just -EIO is enough?

How about writing the error code to return into the attribute ? Write anything negative, and it is returned as error. Write 0, and the driver works as normal.

This is smart, I like it :)

Quoted 4 lines
Quoted 3 lines

Do you think it should fail all the time when error injection is enabled, or is there a value in having only a certain % of commands fail?

Quoted 6 lines
Quoted 3 lines

For my purposes I would want it to fail reliably. We could add some fanciness,
though: Provide a second attribute which specifies how many operations should pass before the first failure.

Let's start simple and just implement what you need.

I would actually benefit from both. The ability to return an error unconditionally lets me test the first error path. The ability to return an error starting with the n-th transfer lets me test the n-th error path.

Guenter

Signature · 3 lines
To unsubscribe from this list: send the line "unsubscribe linux-i2c" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#11 lore raw
In reply to Guenter Roeck
Quoted 11 lines · Guenter Roeck

SMBus block commands are different to I2C block commands since the returned data is not normally accessible with byte or word commands on other command offsets. Add linked list of 'block' commands to support those commands.

Access mechanism is quite simple: Block commands must be written before they can be read. The first write selects the block length. Subsequent writes can be partial. Block read commands always return the number of bytes selected with the first write.

Quoted 10 lines

---
v2: Make new functionality only available on request via functionality
module parameter
Add more details about SMBus block mode support to documentation Use correct sizeof() variable in devm_kzalloc
Use stub_find_block() only in SMBus block command itself. Store first word of block data in chip->words[].
When writing block data and the written data is longer than the first write, bail out with debug message indicating the reason for the error.

Thanks for doing this and thanks to Jean for the thorough review.

The thing I miss is the documentation that SMBUS_BLOCK must explicitly be activated and the description how to do it. A comment in the driver, above STUB_FUNC_* defines might not hurt as well.

Also, I'd prefer to squash Jean's "increasing block length" patch into this one if both of you are fine with that?

Regards,

Wolfram

#12 lore raw
Quoted 23 lines · Wolfram Sang
Quoted 11 lines · Guenter Roeck

SMBus block commands are different to I2C block commands since the returned data is not normally accessible with byte or word commands on other command offsets. Add linked list of 'block' commands to support those commands.

Access mechanism is quite simple: Block commands must be written before they can be read. The first write selects the block length. Subsequent writes can be partial. Block read commands always return the number of bytes selected with the first write.

Quoted 10 lines

---
v2: Make new functionality only available on request via functionality
module parameter
Add more details about SMBus block mode support to documentation Use correct sizeof() variable in devm_kzalloc
Use stub_find_block() only in SMBus block command itself. Store first word of block data in chip->words[].
When writing block data and the written data is longer than the first write, bail out with debug message indicating the reason for the error.

Quoted 1 line

Thanks for doing this and thanks to Jean for the thorough review.

You're welcome.

Quoted 3 lines

The thing I miss is the documentation that SMBUS_BLOCK must explicitly be activated and the description how to do it. A comment in the driver, above STUB_FUNC_* defines might not hurt as well.

Good point.

Quoted 2 lines

Also, I'd prefer to squash Jean's "increasing block length" patch into this one if both of you are fine with that?

Yes, I would be perfectly fine with that.

Guenter, can you please send an updated patch?

Signature · 2 lines
Jean Delvare
SUSE L3 Support
#13 lore raw

PATCH v3 1/1 i2c: stub: Add support for SMBus block commands

SMBus block commands are different to I2C block commands since the returned data is not normally accessible with byte or word commands on other command offsets. Add linked list of 'block' commands to support those commands.

Access mechanism is quite simple: Block commands must be written before they can be read. Subsequent writes can be partial. Block read commands always return the number of bytes associated with the longest previous write.

---
v3: Document that SMBus block commands are by default disabled and how
to enable them.
Merge Jean's "increasing block length" patch into this one.
v2: Make new functionality only available on request via functionality
module parameter.
Add more details about SMBus block mode support to documentation. Use correct sizeof() variable in devm_kzalloc.
Use stub_find_block() only in SMBus block command itself. Store first word of block data in chip->words[].
When writing block data and the written data is longer than the first write, bail out with debug message indicating the reason for the error.

 Documentation/i2c/i2c-stub | 12 +++++-
 drivers/i2c/i2c-stub.c     | 99 +++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 104 insertions(+), 7 deletions(-)
Documentation/i2c/i2c-stub +10 -2
index fa4b669..a0fe7a0 100644
@@ -2,9 +2,9 @@ MODULE: i2c-stub
 
 DESCRIPTION:
 
-This module is a very simple fake I2C/SMBus driver.  It implements five
+This module is a very simple fake I2C/SMBus driver.  It implements six
 types of SMBus commands: write quick, (r/w) byte, (r/w) byte data, (r/w)
-word data, and (r/w) I2C block data.
+word data, (r/w) I2C block data, and (r/w) SMBus block data.
 
 You need to provide chip addresses as a module parameter when loading this
 driver, which will then only react to SMBus commands to these addresses.
@@ -19,6 +19,14 @@ A pointer register with auto-increment is implemented for all byte
 operations.  This allows for continuous byte reads like those supported by
 EEPROMs, among others.
 
+SMBus block command support is disabled by default, and must be enabled
+explicitly by setting the respective bits (0x03000000) in the functionality
+module parameter.
+
+SMBus block commands must be written to configure an SMBus command for
+SMBus block operations. Writes can be partial. Block read commands always
+return the number of bytes selected with the largest write so far.
+
 The typical use-case is like this:
 	1. load this module
 	2. use i2cset (from the i2c-tools project) to pre-load some data
drivers/i2c/i2c-stub.c +94 -5

148 lines not on this page · raw message

Signature · 1 line
1.9.1
Applied
#14 lore raw
Quoted 11 lines · Guenter Roeck

SMBus block commands are different to I2C block commands since the returned data is not normally accessible with byte or word commands on other command offsets. Add linked list of 'block' commands to support those commands.

Access mechanism is quite simple: Block commands must be written before they can be read. Subsequent writes can be partial. Block read commands always return the number of bytes associated with the longest previous write.

Applied to for-next, thanks! I took the liberty to add Jean's Rev-by tag from V2.