Re: [PATCH v14] i2c: virtio: add a virtio i2c frontend driver
From: Viresh Kumar
Date: Thu Jul 08 2021 - 23:45:35 EST
On 09-07-21, 10:25, Jie Deng wrote:
> Add an I2C bus driver for virtio para-virtualization.
>
> The controller can be emulated by the backend driver in
> any device model software by following the virtio protocol.
>
> The device specification can be found on
> https://lists.oasis-open.org/archives/virtio-comment/202101/msg00008.html.
>
> By following the specification, people may implement different
> backend drivers to emulate different controllers according to
> their needs.
>
> Co-developed-by: Conghui Chen <conghui.chen@xxxxxxxxx>
> Signed-off-by: Conghui Chen <conghui.chen@xxxxxxxxx>
> Signed-off-by: Jie Deng <jie.deng@xxxxxxxxx>
> ---
> Changes v13 -> v14
> - Put the headers in virtio_i2c.h in alphabetical order.
> - Dropped I2C_FUNC_SMBUS_QUICK support.
> - Dropped few unnecessary variables and checks.
> - Use "num" everywhere instead of num or nr, to be consistent.
> - Added few comments which make the design more clear.
Thanks a lot for following this up so far :)
> +static int virtio_i2c_prepare_reqs(struct virtqueue *vq,
> + struct virtio_i2c_req *reqs,
> + struct i2c_msg *msgs, int num)
> +{
> + struct scatterlist *sgs[3], out_hdr, msg_buf, in_hdr;
> + int i;
> +
> + for (i = 0; i < num; i++) {
> + int outcnt = 0, incnt = 0;
> +
> + /*
> + * We don't support 0 length messages and so masked out
> + * I2C_FUNC_SMBUS_QUICK in virtio_i2c_func().
> + */
> + if (!msgs[i].len)
> + break;
> +
> + /*
> + * Only 7-bit mode supported for this moment. For the address
> + * format, Please check the Virtio I2C Specification.
> + */
> + reqs[i].out_hdr.addr = cpu_to_le16(msgs[i].addr << 1);
> +
> + if (i != num - 1)
> + reqs[i].out_hdr.flags = cpu_to_le32(VIRTIO_I2C_FLAGS_FAIL_NEXT);
> +
> + sg_init_one(&out_hdr, &reqs[i].out_hdr, sizeof(reqs[i].out_hdr));
> + sgs[outcnt++] = &out_hdr;
> +
> + reqs[i].buf = i2c_get_dma_safe_msg_buf(&msgs[i], 1);
> + if (!reqs[i].buf)
> + break;
> +
> + sg_init_one(&msg_buf, reqs[i].buf, msgs[i].len);
> +
> + if (msgs[i].flags & I2C_M_RD)
> + sgs[outcnt + incnt++] = &msg_buf;
> + else
> + sgs[outcnt++] = &msg_buf;
> +
> + sg_init_one(&in_hdr, &reqs[i].in_hdr, sizeof(reqs[i].in_hdr));
> + sgs[outcnt + incnt++] = &in_hdr;
> +
> + if (virtqueue_add_sgs(vq, sgs, outcnt, incnt, &reqs[i], GFP_KERNEL)) {
> + i2c_put_dma_safe_msg_buf(reqs[i].buf, &msgs[i], false);
> + break;
> + }
> + }
> +
> + return i;
> +}
Wolfram, in case you wonder why we don't error out early as discussed earlier,
then ...
> +static int virtio_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
> + int num)
> +{
...
> + /*
> + * For the case where count < num, i.e. we weren't able to queue all the
> + * msgs, ideally we should abort right away and return early, but some
> + * of the messages are already sent to the remote I2C controller and the
> + * virtqueue will be left in undefined state in that case. We kick the
> + * remote here to clear the virtqueue, so we can try another set of
> + * messages later on.
> + */
... here is the reasoning for that.
Please see if you can still get it merged into 5.14-rc1/2. Thanks.
Reviewed-by: Viresh Kumar <viresh.kumar@xxxxxxxxxx>
Tested-by: Viresh Kumar <viresh.kumar@xxxxxxxxxx>
--
viresh