xref: /linux/Documentation/i2c/gpio-fault-injection.rst (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1ccf988b6SMauro Carvalho Chehab=========================
2ccf988b6SMauro Carvalho ChehabLinux I2C fault injection
3ccf988b6SMauro Carvalho Chehab=========================
4ccf988b6SMauro Carvalho Chehab
5ccf988b6SMauro Carvalho ChehabThe GPIO based I2C bus master driver can be configured to provide fault
6ccf988b6SMauro Carvalho Chehabinjection capabilities. It is then meant to be connected to another I2C bus
7ccf988b6SMauro Carvalho Chehabwhich is driven by the I2C bus master driver under test. The GPIO fault
8ccf988b6SMauro Carvalho Chehabinjection driver can create special states on the bus which the other I2C bus
9ccf988b6SMauro Carvalho Chehabmaster driver should handle gracefully.
10ccf988b6SMauro Carvalho Chehab
11ccf988b6SMauro Carvalho ChehabOnce the Kconfig option I2C_GPIO_FAULT_INJECTOR is enabled, there will be an
12ccf988b6SMauro Carvalho Chehab'i2c-fault-injector' subdirectory in the Kernel debugfs filesystem, usually
13ccf988b6SMauro Carvalho Chehabmounted at /sys/kernel/debug. There will be a separate subdirectory per GPIO
14ccf988b6SMauro Carvalho Chehabdriven I2C bus. Each subdirectory will contain files to trigger the fault
15ccf988b6SMauro Carvalho Chehabinjection. They will be described now along with their intended use-cases.
16ccf988b6SMauro Carvalho Chehab
17ccf988b6SMauro Carvalho ChehabWire states
18ccf988b6SMauro Carvalho Chehab===========
19ccf988b6SMauro Carvalho Chehab
20ccf988b6SMauro Carvalho Chehab"scl"
21ccf988b6SMauro Carvalho Chehab-----
22ccf988b6SMauro Carvalho Chehab
23ccf988b6SMauro Carvalho ChehabBy reading this file, you get the current state of SCL. By writing, you can
24ccf988b6SMauro Carvalho Chehabchange its state to either force it low or to release it again. So, by using
25ccf988b6SMauro Carvalho Chehab"echo 0 > scl" you force SCL low and thus, no communication will be possible
26ccf988b6SMauro Carvalho Chehabbecause the bus master under test will not be able to clock. It should detect
27ccf988b6SMauro Carvalho Chehabthe condition of SCL being unresponsive and report an error to the upper
28ccf988b6SMauro Carvalho Chehablayers.
29ccf988b6SMauro Carvalho Chehab
30ccf988b6SMauro Carvalho Chehab"sda"
31ccf988b6SMauro Carvalho Chehab-----
32ccf988b6SMauro Carvalho Chehab
33ccf988b6SMauro Carvalho ChehabBy reading this file, you get the current state of SDA. By writing, you can
34ccf988b6SMauro Carvalho Chehabchange its state to either force it low or to release it again. So, by using
35ccf988b6SMauro Carvalho Chehab"echo 0 > sda" you force SDA low and thus, data cannot be transmitted. The bus
36ccf988b6SMauro Carvalho Chehabmaster under test should detect this condition and trigger a bus recovery (see
37ccf988b6SMauro Carvalho ChehabI2C specification version 4, section 3.1.16) using the helpers of the Linux I2C
38ccf988b6SMauro Carvalho Chehabcore (see 'struct bus_recovery_info'). However, the bus recovery will not
39ccf988b6SMauro Carvalho Chehabsucceed because SDA is still pinned low until you manually release it again
40ccf988b6SMauro Carvalho Chehabwith "echo 1 > sda". A test with an automatic release can be done with the
41ccf988b6SMauro Carvalho Chehab"incomplete transfers" class of fault injectors.
42ccf988b6SMauro Carvalho Chehab
43ccf988b6SMauro Carvalho ChehabIncomplete transfers
44ccf988b6SMauro Carvalho Chehab====================
45ccf988b6SMauro Carvalho Chehab
46ccf988b6SMauro Carvalho ChehabThe following fault injectors create situations where SDA will be held low by a
47ccf988b6SMauro Carvalho Chehabdevice. Bus recovery should be able to fix these situations. But please note:
48ccf988b6SMauro Carvalho Chehabthere are I2C client devices which detect a stuck SDA on their side and release
49ccf988b6SMauro Carvalho Chehabit on their own after a few milliseconds. Also, there might be an external
50ccf988b6SMauro Carvalho Chehabdevice deglitching and monitoring the I2C bus. It could also detect a stuck SDA
51ccf988b6SMauro Carvalho Chehaband will init a bus recovery on its own. If you want to implement bus recovery
52ccf988b6SMauro Carvalho Chehabin a bus master driver, make sure you checked your hardware setup for such
53ccf988b6SMauro Carvalho Chehabdevices before. And always verify with a scope or logic analyzer!
54ccf988b6SMauro Carvalho Chehab
55ccf988b6SMauro Carvalho Chehab"incomplete_address_phase"
56ccf988b6SMauro Carvalho Chehab--------------------------
57ccf988b6SMauro Carvalho Chehab
58ccf988b6SMauro Carvalho ChehabThis file is write only and you need to write the address of an existing I2C
59ccf988b6SMauro Carvalho Chehabclient device to it. Then, a read transfer to this device will be started, but
60ccf988b6SMauro Carvalho Chehabit will stop at the ACK phase after the address of the client has been
61ccf988b6SMauro Carvalho Chehabtransmitted. Because the device will ACK its presence, this results in SDA
62ccf988b6SMauro Carvalho Chehabbeing pulled low by the device while SCL is high. So, similar to the "sda" file
63ccf988b6SMauro Carvalho Chehababove, the bus master under test should detect this condition and try a bus
64ccf988b6SMauro Carvalho Chehabrecovery. This time, however, it should succeed and the device should release
65ccf988b6SMauro Carvalho ChehabSDA after toggling SCL.
66ccf988b6SMauro Carvalho Chehab
67ccf988b6SMauro Carvalho Chehab"incomplete_write_byte"
68ccf988b6SMauro Carvalho Chehab-----------------------
69ccf988b6SMauro Carvalho Chehab
70ccf988b6SMauro Carvalho ChehabSimilar to above, this file is write only and you need to write the address of
71ccf988b6SMauro Carvalho Chehaban existing I2C client device to it.
72ccf988b6SMauro Carvalho Chehab
73ccf988b6SMauro Carvalho ChehabThe injector will again stop at one ACK phase, so the device will keep SDA low
74ccf988b6SMauro Carvalho Chehabbecause it acknowledges data. However, there are two differences compared to
75ccf988b6SMauro Carvalho Chehab'incomplete_address_phase':
76ccf988b6SMauro Carvalho Chehab
77ccf988b6SMauro Carvalho Chehaba) the message sent out will be a write message
78ccf988b6SMauro Carvalho Chehabb) after the address byte, a 0x00 byte will be transferred. Then, stop at ACK.
79ccf988b6SMauro Carvalho Chehab
80ccf988b6SMauro Carvalho ChehabThis is a highly delicate state, the device is set up to write any data to
81ccf988b6SMauro Carvalho Chehabregister 0x00 (if it has registers) when further clock pulses happen on SCL.
82ccf988b6SMauro Carvalho ChehabThis is why bus recovery (up to 9 clock pulses) must either check SDA or send
83ccf988b6SMauro Carvalho Chehabadditional STOP conditions to ensure the bus has been released. Otherwise
84ccf988b6SMauro Carvalho Chehabrandom data will be written to a device!
85ccf988b6SMauro Carvalho Chehab
86ccf988b6SMauro Carvalho ChehabLost arbitration
87ccf988b6SMauro Carvalho Chehab================
88ccf988b6SMauro Carvalho Chehab
89ccf988b6SMauro Carvalho ChehabHere, we want to simulate the condition where the master under test loses the
90ccf988b6SMauro Carvalho Chehabbus arbitration against another master in a multi-master setup.
91ccf988b6SMauro Carvalho Chehab
92ccf988b6SMauro Carvalho Chehab"lose_arbitration"
93ccf988b6SMauro Carvalho Chehab------------------
94ccf988b6SMauro Carvalho Chehab
95ccf988b6SMauro Carvalho ChehabThis file is write only and you need to write the duration of the arbitration
96*38bd4136SRandy Dunlapinterference (in µs, maximum is 100ms). The calling process will then sleep
97ccf988b6SMauro Carvalho Chehaband wait for the next bus clock. The process is interruptible, though.
98ccf988b6SMauro Carvalho Chehab
99ccf988b6SMauro Carvalho ChehabArbitration lost is achieved by waiting for SCL going down by the master under
100ccf988b6SMauro Carvalho Chehabtest and then pulling SDA low for some time. So, the I2C address sent out
101ccf988b6SMauro Carvalho Chehabshould be corrupted and that should be detected properly. That means that the
102ccf988b6SMauro Carvalho Chehabaddress sent out should have a lot of '1' bits to be able to detect corruption.
103ccf988b6SMauro Carvalho ChehabThere doesn't need to be a device at this address because arbitration lost
104ccf988b6SMauro Carvalho Chehabshould be detected beforehand. Also note, that SCL going down is monitored
105ccf988b6SMauro Carvalho Chehabusing interrupts, so the interrupt latency might cause the first bits to be not
106ccf988b6SMauro Carvalho Chehabcorrupted. A good starting point for using this fault injector on an otherwise
107ccf988b6SMauro Carvalho Chehabidle bus is::
108ccf988b6SMauro Carvalho Chehab
109ccf988b6SMauro Carvalho Chehab  # echo 200 > lose_arbitration &
110ccf988b6SMauro Carvalho Chehab  # i2cget -y <bus_to_test> 0x3f
111ccf988b6SMauro Carvalho Chehab
112ccf988b6SMauro Carvalho ChehabPanic during transfer
113ccf988b6SMauro Carvalho Chehab=====================
114ccf988b6SMauro Carvalho Chehab
115ccf988b6SMauro Carvalho ChehabThis fault injector will create a Kernel panic once the master under test
116ccf988b6SMauro Carvalho Chehabstarted a transfer. This usually means that the state machine of the bus master
117ccf988b6SMauro Carvalho Chehabdriver will be ungracefully interrupted and the bus may end up in an unusual
118ccf988b6SMauro Carvalho Chehabstate. Use this to check if your shutdown/reboot/boot code can handle this
119ccf988b6SMauro Carvalho Chehabscenario.
120ccf988b6SMauro Carvalho Chehab
121ccf988b6SMauro Carvalho Chehab"inject_panic"
122ccf988b6SMauro Carvalho Chehab--------------
123ccf988b6SMauro Carvalho Chehab
124ccf988b6SMauro Carvalho ChehabThis file is write only and you need to write the delay between the detected
125ccf988b6SMauro Carvalho Chehabstart of a transmission and the induced Kernel panic (in µs, maximum is 100ms).
126ccf988b6SMauro Carvalho ChehabThe calling process will then sleep and wait for the next bus clock. The
127ccf988b6SMauro Carvalho Chehabprocess is interruptible, though.
128ccf988b6SMauro Carvalho Chehab
129ccf988b6SMauro Carvalho ChehabStart of a transfer is detected by waiting for SCL going down by the master
130ccf988b6SMauro Carvalho Chehabunder test.  A good starting point for using this fault injector is::
131ccf988b6SMauro Carvalho Chehab
132ccf988b6SMauro Carvalho Chehab  # echo 0 > inject_panic &
133ccf988b6SMauro Carvalho Chehab  # i2cget -y <bus_to_test> <some_address>
134ccf988b6SMauro Carvalho Chehab
135ccf988b6SMauro Carvalho ChehabNote that there doesn't need to be a device listening to the address you are
136ccf988b6SMauro Carvalho Chehabusing. Results may vary depending on that, though.
137