ff1e8581 avg Nov. 26, 2021, 2:20 p.m.
Most prominently, add support for a transfer where a write with no-stop
flag is followed by a write with no-start flag.  Logically, it's a
single larger write, but consumers may want to split it like that
because one part can be a register ID and the other part can be data to
be written to (or starting at) that register.

Such a transfer can be created by i2c tool and iic(4) driver, e.g., for
an EEPROM write at specific offset:
    i2c -m tr -a 0x50 -d w -w 16 -o 0 -c 8 -v < /dev/random

This should be fixed by new code that handles the end of data transfer
for both reads and writes.  It handles two existing conditions and one
new.  Namely:
- the last message has been completed -- end of transfer;
- a message has been completed and the next one requires the start
  condition;
- a message has been completed and the next one should be sent without
  the start condition.

In the last case we simply switch to the next message and start sending
its data.  Reads without the start condition are not supported yet,
though.  That's because we NACK the last byte of the previous message,
so the device stops sending data.  To fix this we will need to add a
look-ahead at the next message when handling the penultimate byte of the
current one.

This change also fixed a bug where msg_idx was not incremented after a
read message.  Apparently, typically a read message is a last message in
a transfer, so the bug did not cause much trouble.

PR:		258994
MFC after:	3 weeks
cgit
00c07d95 avg Nov. 26, 2021, 2:18 p.m.
Assert that we are not receiving data beyond the requested length.
Assert that we have not NACK-ed incoming data prematurely.
Abort the current transfer if the incoming data is NACK-ed or not
NACK-ed unexpectedly.

Add debug logging of received data to complement logging of sent data.

MFC after:	3 weeks
cgit
aeacf172 avg Nov. 26, 2021, 2:18 p.m.
The write at the end of twsi_intr() already handles all cases, no need
to have another write for TWSI_STATUS_START / TWSI_STATUS_RPTD_START.

MFC after:	3 weeks
cgit
04622a7f avg Nov. 26, 2021, 2:17 p.m.
Previously the code set TWSI_CONTROL_ACK in twsi_transfer() based on
whether the first message had a length of one.  That was done regardless
of whether the message was a read or write and what kind of messages
followed it.
Now the bit is set or cleared while handling TWSI_STATUS_ADDR_R_ACK
state transition based on the current (read) message.

The old code did not correctly work in a scenario where a single byte
was read from an EEPROM device with two byte addressing.
For example:
    i2c -m tr -a 0x50 -d r -w 16 -o 0 -c 1 -v
The reason is that the first message (a write) has two bytes, so
TWSI_CONTROL_ACK was set and never cleared.
Since the controller did not send NACK the EEPROM sent more data resulting
in a buffer overrun.

While working on TWSI_STATUS_ADDR_R_ACK I also added support for
the zero-length read access and then I did the same for zero-length write
access.
While rare, those types of I2C transactions are completely valid and are
used by some devices.

PR:		258994
MFC after:	3 weeks
cgit
a4fe8922 avg Nov. 26, 2021, 2:16 p.m.
twsi_error() is a new function that stops the current transfer and sets
up softc when an error condition is detected.
TWSI_STATUS_DATA_WR_NACK, TWSI_STATUS_BUS_ERROR and
TWSI_STATUS_ARBITRATION_LOST are now handled explicitly rather than
via the catch-all unknown status.

Also, twsi_intr() now calls wakeup() in a single place when the
transfer is finished.

MFC after:	2 weeks
cgit
578707ed avg Nov. 26, 2021, 2:16 p.m.
- use 0x prefix for hex values
- print indexes and counts as decimals
- break too long lines

MFC after:	2 weeks
cgit
cb167e1a avg Nov. 26, 2021, 2:15 p.m.
MFC after:	2 weeks
cgit
bc02583c avg Nov. 26, 2021, 2:15 p.m.
MFC after:	2 weeks
cgit
69cfa60d avg Nov. 26, 2021, 2:14 p.m.
All accesses to softc are now done under a mutex to prevent data races
between the open context and the interrupt handler.
Additionally, the wait time in twsi_transfer is bounded now.
Previously we could get stuck there forever if an interrupt got lost.

MFC after:	2 weeks
cgit
a2793d61 avg Nov. 26, 2021, 2:04 p.m.
Debug messages can now be enabled per driver instance via a new sysctl.
Also, debug messages in TWSI_READ and TWSI_WRITE require debug level
greater than 1 as they are mostly redundant because callers of those
functions already log most interesting results.

NB: the twsi drivers call their device iichb, so the new sysctl will
appear under dev.iichb.N.

MFC after:	1 week
cgit
f00bc54f avg Nov. 26, 2021, 2:04 p.m.
MFC after:	1 week
cgit
de86f339 avg Nov. 26, 2021, 2:03 p.m.
For completeness and for future use.

MFC after:	1 week
cgit
26559dd1 avg Nov. 26, 2021, 2:03 p.m.
MFC after:	1 week
cgit
3c1ba6f3 tuexen Nov. 26, 2021, 11:53 a.m.
054f5815 mjg Nov. 26, 2021, 12:22 p.m.
Sponsored by:	Rubicon Communications, LLC ("Netgate")
cgit