Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i2c: mcux driver does not prevent simultaneous transactions #29239

Closed
drandreas opened this issue Oct 15, 2020 · 4 comments · Fixed by #29269
Closed

i2c: mcux driver does not prevent simultaneous transactions #29239

drandreas opened this issue Oct 15, 2020 · 4 comments · Fixed by #29269
Labels
area: I2C bug The issue is a bug, or the PR is fixing a bug platform: NXP NXP priority: medium Medium impact/importance bug

Comments

@drandreas
Copy link
Contributor

drandreas commented Oct 15, 2020

Introduction

Several peripherals might be attached to one i2c bus. Some of those peripherals might therefore, access the same i2c bus simultaneously. I am expecting the i2c bus to have locking mechanism in place to prevent race conditions if an i2c transfer consists of multiple i2c_msg.

Problem description

Neither i2c_transfer in i2c.h nor i2c_mcux_transfer in i2c_mcux.c have any sort of locking. If e.g. an i2c eprom is updated using a shell and simultaneously the system work queue accesses an i2c-port-expander then one of the two transfers will fail or produce a random result. I suspect several of the i2c drivers are affected but haven't checked them all.

Proposed change

Add a semaphore to i2c_transfer in i2c.h or every i2c bus driver.

Alternatives

  • Global I2C Lock
  • Queue i2c_msgs
@drandreas drandreas added the RFC Request For Comments: want input from the community label Oct 15, 2020
@drandreas drandreas changed the title Locking of I2C bus [RFC] Locking of I2C bus Oct 15, 2020
@pabigot
Copy link
Collaborator

pabigot commented Oct 16, 2020

There is currently no global solution for exclusive access to devices, though there is a proposal in #24511 (which needs to be discussed since some drivers shouldn't use blocking locks).

Many (most?) drivers take a semaphore on entry to i2c_transfer() and hold it until done. The MCUX I2C implementation needs to do this.

@drandreas Would you mind if I converted this from an RFC to a bug against MCUX and any other I2C drivers that seem to lack a mutex?

@tbursztyka
Copy link
Collaborator

For the time being - until we agree on a generic solutions - I'd go for the same solution as in SPI: see drivers/spi/spi_context.h
It generalizes such lock and sync to SPI, so each and every driver uses such structure. You could get the same for i²C. That, at least would not need any changes to any public API.

@drandreas
Copy link
Contributor Author

Sure, feel free to convert this ticket

For MCUX I2C I've already created a patch:
drandreas@0174989
I'm happy to create a PR.

@pabigot pabigot added bug The issue is a bug, or the PR is fixing a bug and removed RFC Request For Comments: want input from the community labels Oct 16, 2020
@pabigot pabigot changed the title [RFC] Locking of I2C bus i2c: mcux driver does not prevent simultaneous transactions Oct 16, 2020
@pabigot pabigot removed their assignment Oct 16, 2020
@pabigot
Copy link
Collaborator

pabigot commented Oct 16, 2020

@drandreas Done, please submit your PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: I2C bug The issue is a bug, or the PR is fixing a bug platform: NXP NXP priority: medium Medium impact/importance bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants