Skip to content

Conversation

@samitsingh7
Copy link

@samitsingh7 samitsingh7 commented Jul 17, 2023

  • Updated driver to reset the i2c node if its corresponding dtsi instance has a resets property.
  • Handling invalid 0 length i2c transfer and logging error if i2c transfer is initiated with length 0.
  • Modified i2c shell to support i2c scan command only if I2C_ZERO_LEN_XFER_SUPPORTED Kconfig flag is enabled.
  • Updated i2c shell for checking i2c-speed parameter in i2c speed shell command.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @samitsingh7, and thank you very much for your first pull request to the Zephyr project!

A project maintainer just triggered our CI pipeline to run it against your PR and ensure it's compliant and doesn't cause any issues. You might want to take this opportunity to review the project's Contributor Expectations and make any updates to your pull request if necessary. 😊

@samitsingh7 samitsingh7 changed the title I2c dw driver drivers: i2c_dw: add support for agilex platform and reset Jul 17, 2023
@samitsingh7 samitsingh7 marked this pull request as ready for review July 17, 2023 11:38
@zephyrbot zephyrbot added area: Devicetree Binding PR modifies or adds a Device Tree binding area: ARM64 ARM (64-bit) Architecture area: I2C labels Jul 17, 2023
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a strange ifdef being added here, can you explain?

Copy link
Author

@samitsingh7 samitsingh7 Jul 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i2c scan shell command initiates i2c transfer without any data which is not a standard in i2c protocol but SMbus supports such transfers.
Also, the current i2c_dw driver is not handling 0 length i2c transfer. Thus, issuing this shell command could hang the system(which is happening in case of i2c_dw driver).

So by introducing the kconfig macro: CONFIG_I2C_SMBUS_SUPPORTED,
By default, this macro is enabled and the i2c scan shell command can be issued but if we disable this macro explicitly then the i2c scan shell command will not be available and thus preventing the program from getting hung if such i2c transfers without data are not handled in the driver.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if I'm understanding this correctly, the issue is on this device the zero sized transfer used by i2c scan here https://github.com/zephyrproject-rtos/zephyr/blob/main/drivers/i2c/i2c_shell.c#L79 is unsupported by this particular hardware or driver.

There's nothing in the i2c specification regarding sending an address only being invalid that I can find. Do you have a source for this being invalid?

The Kconfig here makes sense in terms of this particular hardware and driver combo possibly not supporting i2c scan. But to have a Kconfig like this is a bit strange to me. Instead the driver should return an error code on such transfers and this Kconfig is no longer needed I'd think.

If it's something the driver can possibly do on some instances of the designware controller then it should be a Kconfig along the lines of I2C_DW_HAS_ZERO_LEN_XFER or so and worked with in that way.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's nothing in the i2c specification regarding sending an address only being invalid that I can find. Do you have a source for this being invalid?

I agree that sending an address only in i2c transaction (Quick Command) is valid.

As per the comment of the i2c-scan function:-

WARNING: As there is no standard I2C detection command, this code
 uses arbitrary SMBus commands (namely SMBus quick write and SMBus
  receive byte) to probe for devices.  This operation can confuse
 your I2C bus, cause data loss, and is known to corrupt the Atmel
  AT24RF08 EEPROM found on many IBM Thinkpad laptops. 
  https://manpages.debian.org/buster/i2c-tools/i2cdetect.8.en.html

We are issuing SMbus Quick command for i2c-scan functionality. Also, it is well documented that this operation could confuse i2c bus and cause data loss. or corrupt the target device. The same is documented here: https://manpages.debian.org/buster/i2c-tools/i2cdetect.8.en.html#WARNING.

Another such combination where this was not working: #54694.

Also, few of the I2C IPs support Quick Commands or 0 length i2c command based on the hardware configuration.
Thus, making use of this Kconfig macro we can avoid such conditions from various i2c controller and target device combination on various platform by disabling i2c-scan functionality for such applications where 0 length command is not supported.

The Kconfig here makes sense in terms of this particular hardware and driver combo possibly not supporting i2c scan.

Exactly as mentioned above and observed from various OS and hardware combination that i2c scan command could cause an issue. Thus introducing Kconfig macro which could help in enabling and disabling for the combination user have.

Instead the driver should return an error code on such transfers and this Kconfig is no longer needed I'd think.

Totally agreed and doing the same here, we are doing 0 length check and returning an error code - https://github.com/samitsingh7/zephyr_main/blob/i2c_dw_driver/drivers/i2c/i2c_dw.c#L478
The introduction of this Kconfig is rather for facilitating user to disable the i2c scan functionality from shell and safeguard from vulnerabilities from data corruption or i2c bus hang.

There could be different platforms where the i2c controller is not configured to support i2c quick commands.
Then all such platforms can make use of this macro to enable or disable this shell command.
We are still open to suggestions on alternative names for this macro: I2C_ZERO_LEN_XFER_SUPPORTED or I2C_QUICK_CMD_EN ?

Thanks for reviewing the changes!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets rename it I2C_ZERO_LEN_XFER_SUPPORTED as I think thats closer to the feature

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't make much sense to me, why is this needed

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i2c shell command: i2c speed <device> <input>
will result in setting up the same speed either if we pass input as (1 or 9), (2 or 10), (3 or 11)..
Because in i2c speed shell function, I2C_SPEED_SET(speed) macro masks the speed parameter with 0x7.

Thus, introduced the check for the input parameter as passing the value more than 0x7 is not possible.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't make much sense, this is i2c not smbus (which has a separate API)

Copy link
Author

@samitsingh7 samitsingh7 Jul 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name of the macro: CONFIG_I2C_SMBUS_SUPPORTED, is chosen like this because i2c transfer without any data is supported by SMbus.

By default, this macro is enabled and the i2c scan shell command will be available for an execution but if we disable this macro explicitly then the i2c scan shell command will not be available and thus preventing the program from getting hung if i2c transfers without data are not handled in the driver (ex: i2c_dw driver).

@teburd
Copy link
Contributor

teburd commented Aug 1, 2023

Renaming of the Kconfig option is a +1 from me

@github-actions
Copy link

github-actions bot commented Oct 1, 2023

This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time.

@github-actions github-actions bot added the Stale label Oct 1, 2023
@samitsingh7
Copy link
Author

I would like to have the stale label removed

@teburd teburd removed the Stale label Oct 1, 2023
@zephyrbot zephyrbot added the platform: Intel SoC FPGA Agilex Intel Corporation, SoC FPGA Agilex label Nov 6, 2023
@samitsingh7
Copy link
Author

Renaming of the Kconfig option is a +1 from me

Done

teburd
teburd previously approved these changes Nov 6, 2023
@github-actions
Copy link

github-actions bot commented Jan 6, 2024

This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time.

@samitsingh7 samitsingh7 force-pushed the i2c_dw_driver branch 2 times, most recently from bb63b97 to d5a4a49 Compare November 18, 2024 19:07
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing back to ret will mostly solve the CI problem

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

teburd
teburd previously approved these changes Nov 19, 2024
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be disabled by default and selected by drivers supporting zero-length transfers?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some specific hardware configuration does not support zero-length transfers. In such cases, explicitly disable it. However, the default behavior should remain enabled to avoid impacting other configurations unnecessarily, given the current default is set to 'enabled.'"

Comment on lines 741 to 758
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually do not indent variable names like this in Zephyr. Please leave this change out of this PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please leave the empty line before the return statement intact.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines 1211 to 1248
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid mixing reformatting with functional changes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, reverted reformatting changes

Comment on lines -842 to +849
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change looks unrelated to the actual, functional change. Please leave this kind of changes out as it makes reading the diff much harder.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the change is unrelated to functionality.
The variable name 'rc' was not very intuitive, so I updated it to 'ret' for clarity.
In the future, I’ll avoid making such changes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this rename into a separate commit.

Added device tree for I2C

Signed-off-by: samit singh sikarwar <[email protected]>
Handling invalid 0 length i2c transfer and logging error
if i2c transfer is initiated with length 0.
Modified i2c shell to support i2c scan command only if
I2C_ZERO_LEN_XFER_SUPPORTED Kconfig flag is enabled.
Updated i2c shell for checking i2c-speed parameter in
i2c speed shell command.

Signed-off-by: samit singh sikarwar <[email protected]>
@samitsingh7
Copy link
Author

Hi @henrikbrixandersen-

Addressed all the comments.

Seeking your review.

@samitsingh7
Copy link
Author

Hi @teburd :
Seeking your review, addressed all the comments.

@henrikbrixandersen
Copy link
Member

Hi @henrikbrixandersen-

Addressed all the comments.

Seeking your review.

No, you did not address all of my comments. You are still adding support for Quick Commands, which is an SMBus feature, to the I2C API, which I commented on earlier, while we have a dedicated SMBus API.

@samitsingh7
Copy link
Author

samitsingh7 commented Jan 29, 2025

You are still adding support for Quick Commands, which is an SMBus feature, to the I2C API, which I commented on earlier, while we have a dedicated SMBus API.

Hi @henrikbrixandersen -
I understand your concern regarding SMBus Quick Commands, but I am not adding support for SMBus features to the I2C API. The purpose of the I2C_ZERO_LEN_XFER_SUPPORTED macro is solely to provide a way to prevent system hangs in drivers that do not handle zero-length I2C transfers (such as i2c_dw).

The existing i2c scan shell command initiates a transfer with zero-length data, which is not standard in I2C but is supported in SMBus. Since the i2c_dw driver does not handle this case properly, issuing the command can lead to a system hang. The macro allows users to disable the command explicitly, avoiding this issue without modifying I2C behavior.

This change does not introduce SMBus support into the I2C API but rather provides a safeguard against unintended hangs in non-SMBus-capable I2C drivers. Let me know if further clarification is needed.

return MIN(MAX_BYTES_FOR_REGISTER_INDEX, length);
}

#if CONFIG_I2C_ZERO_LEN_XFER_SUPPORTED
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still do not understand why the Kconfig is needed. The driver returns an error on unsupported msg lengths. Why isn't this sufficient?

Copy link
Author

@samitsingh7 samitsingh7 Feb 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @henrikbrixandersen ,

I have explained the reasoning behind it here: #60427 (comment)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @henrikbrixandersen, Please let me know if anything else is blocking this PR.

@teburd teburd requested review from keith-zephyr and soburi and removed request for malto101 March 19, 2025 18:58
ret = i2c_dw_set_master_mode(dev);

return ret;
return (int)i2c_dw_set_master_mode(dev);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cast doesn't seem necessary.

static int i2c_dw_set_master_mode(const struct device *dev)


#ifdef CONFIG_I2C_TARGET
static inline uint8_t i2c_dw_read_byte_non_blocking(const struct device *dev);
static inline int i2c_dw_read_byte_non_blocking(const struct device *dev, uint8_t *data);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 461 is still eave as old signature. Please run compilation.

				data = i2c_dw_read_byte_non_blocking(port);

The I2C shell supports scanning, bus recovery, I2C read and write
operations.

config I2C_ZERO_LEN_XFER_SUPPORTED
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this config use only in i2c_sell.c, the name should be better start with I2C_SHELL_.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @henrikbrixandersen - this Kconfig doesn't seem necessary if the I2C controllers that cannot support zero length transfers are required to return an error instead.

If you're going to keep the Kconfig, I suggest flipping the logic so that the Kconfig turns off zero length transfers. Something like CONFIG_I2C_DISABLE_ZERO_LENGTH_TRANSFER.

Also, could the zero length check get moved directly into the common i2c_transfer() syscall instead.

@github-actions
Copy link

This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time.

@github-actions github-actions bot added the Stale label May 24, 2025
@github-actions github-actions bot closed this Jun 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: ARM64 ARM (64-bit) Architecture area: Devicetree Binding PR modifies or adds a Device Tree binding area: I2C platform: Intel SoC FPGA Agilex Intel Corporation, SoC FPGA Agilex Stale

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants