Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] i2c: omap: u8 truncation in omap_i2c_resize_fifo() breaks DMA
  threshold
- [Medium] i2c: omap: DMA request disabled after buffer unmap in
  omap_i2c_xfer_msg()
- [Medium] i2c: omap: ERR_PTR assigned to omap->dma after IRQ init in
  omap_i2c_probe()
- [Medium] i2c: omap: missing PIO fallback on DMA failure in
  omap_i2c_xfer_msg()
- [Low] i2c: omap: invalid kernel-doc comment prefix in
  omap_i2c_xfer_msg()
--

commit 7c0a49ff7dadae86047390fd06ce1307e0609c76
Author: Antoni Pokusinski <[email protected]>

i2c: omap: add DMA support

This commit adds TX and RX DMA support to the OMAP I2C controller for
messages whose length is at least 8 bytes and a multiple of the FIFO
threshold.

> diff --git a/drivers/i2c/busses/i2c-omap.c b/drivers/i2c/busses/i2c-omap.c
> index f02d294db42a6..650d1cce05c75 100644
> --- a/drivers/i2c/busses/i2c-omap.c
> +++ b/drivers/i2c/busses/i2c-omap.c

[ ... ]

> @@ -663,8 +861,8 @@ static int omap_i2c_xfer_msg(struct i2c_adapter *adap,
>  {
>       struct omap_i2c_dev *omap = i2c_get_adapdata(adap);
>       unsigned long time_left;
> -     u16 w;
> -     int ret;
> +     u16 w, pio_irq_mask;
> +     int ret, use_dma;
>  
>       dev_dbg(omap->dev, "addr: 0x%04x, len: %d, flags: 0x%x, stop: %d\n",
>               msg->addr, msg->len, msg->flags, stop);
>  
>       omap->receiver = !!(msg->flags & I2C_M_RD);
>       omap_i2c_resize_fifo(omap, msg->len, omap->receiver);
>  
> +     /**
> +      * REVISIT: If the message size is not a multiple of the threshold,
> +      * the draining feature must be used.
> +      */

[Severity: Low]
This internal comment uses the "/**" kernel-doc prefix, which causes a
W=1 kernel-doc build warning.

> +     use_dma = !polling && omap->dma && msg->len >= OMAP_I2C_DMA_THRESHOLD &&
> +               !(omap->flags & OMAP_I2C_FLAG_NO_FIFO) &&
> +               msg->len % omap->threshold == 0;

[Severity: Medium]
Does passing the 16-bit msg->len into omap_i2c_resize_fifo() truncate
lengths >= 256 bytes and corrupt omap->threshold for DMA?

drivers/i2c/busses/i2c-omap.c:omap_i2c_resize_fifo() {
        ...
static void omap_i2c_resize_fifo(struct omap_i2c_dev *omap, u8 size, bool is_rx)
{
        ...
        omap->threshold = clamp(size, (u8) 1, omap->fifo_size);
}

For a 256-byte transfer (such as an EEPROM page read or write), size
wraps to 0 and omap->threshold is clamped to 1 instead of
omap->fifo_size.  Because 256 % 1 == 0, use_dma is true and
omap_i2c_dma_xfer() programs src_maxburst or dst_maxburst to 1 byte for
the entire transfer.  Changing size in omap_i2c_resize_fifo() to u16 (and
widening the clamp types) would avoid the truncation.

>  
>       omap_i2c_write_reg(omap, OMAP_I2C_SA_REG, msg->addr);
>  
>       /* REVISIT: Could the STB bit of I2C_CON be used with probing? */
> @@ -692,6 +898,23 @@ static int omap_i2c_xfer_msg(struct i2c_adapter *adap,
>               reinit_completion(&omap->cmd_complete);
>       omap->cmd_err = 0;
>  
> +     if (use_dma) {
> +             ret = omap_i2c_dma_xfer(omap, msg);
> +             if (ret) {
> +                     omap_i2c_reset(omap);
> +                     __omap_i2c_init(omap);
> +                     return ret;
> +             }
> +
> +             omap_i2c_dma_enable_req(omap);
> +     } else {
> +             pio_irq_mask = OMAP_I2C_IE_XRDY | OMAP_I2C_IE_RRDY |
> +                            ((omap->fifo_size) ? (OMAP_I2C_IE_RDR |
> +                                                 OMAP_I2C_IE_XDR) : 0);
> +
> +             omap_i2c_enable_pio_irqs(omap, pio_irq_mask);
> +     }

[Severity: Medium]
Should omap_i2c_xfer_msg() fall back to PIO when omap_i2c_dma_xfer()
fails instead of resetting the controller and aborting the transfer?

Nothing has been started on the bus for this message yet, and in a
multi-message transfer where an earlier message completed with stop == 0
(repeated start), calling omap_i2c_reset() here resets the controller
mid-transaction while the bus is still held active without sending a STOP
condition.

[ ... ]

> @@ -753,7 +994,18 @@ static int omap_i2c_xfer_msg(struct i2c_adapter *adap,
>               time_left = !ret;
>       }
>  
> -     if (time_left == 0) {
> +     if (use_dma) {
> +             if (!time_left || omap->cmd_err)
> +                     dmaengine_terminate_sync(omap->dma->chan_using);
> +
> +             omap_i2c_dma_cleanup(omap, time_left && !omap->cmd_err);
> +
> +             omap_i2c_dma_disable_req(omap);
> +     } else {
> +             omap_i2c_disable_pio_irqs(omap, pio_irq_mask);
> +     }

[Severity: Medium]
Should omap_i2c_dma_disable_req() be called before
dmaengine_terminate_sync() and omap_i2c_dma_cleanup(), matching the
start-condition timeout path earlier in omap_i2c_xfer_msg()?  On a
timeout or bus error, the I2C controller can keep asserting DMA requests
while the channel is being terminated and the buffer is unmapped.

[ ... ]

> @@ -1478,6 +1787,22 @@ omap_i2c_probe(struct platform_device *pdev)
>               goto err_unuse_clocks;
>       }
>  
> +     if (omap->scheme != OMAP_I2C_SCHEME_0) {
> +             omap->dma = omap_i2c_dma_init(omap, (dma_addr_t)res->start);
> +             if (PTR_ERR(omap->dma) == -EPROBE_DEFER) {
> +                     r = -EPROBE_DEFER;
> +                     dev_err_probe(&pdev->dev, r,
> +                                   "can't get DMA channels\n");
> +                     goto err_unuse_clocks;
> +             } else if (PTR_ERR(omap->dma) == -ENODEV) {
> +                     dev_dbg(omap->dev, "DMA setup failed\n");
> +                     omap->dma = NULL;
> +             } else if (IS_ERR(omap->dma)) {
> +                     r = PTR_ERR(omap->dma);
> +                     goto err_unuse_clocks;
> +             }
> +     }

[Severity: Medium]
Can assigning the return value of omap_i2c_dma_init() directly to
omap->dma cause the IRQ handler to dereference an ERR_PTR?

At this point in omap_i2c_probe(), omap_i2c_init() has enabled the
controller interrupts and devm_request_threaded_irq() has registered
omap_i2c_isr_thread().  If omap_i2c_dma_init() returns an ERR_PTR,
omap->dma is non-NULL (and remains an ERR_PTR on the err_unuse_clocks
unwind path), while omap_i2c_complete_cmd() and omap_i2c_xfer_data()
only check if (omap->dma) before calling complete(&omap->dma->complete).
In addition, omap_i2c_dma_init() already calls dev_err_probe() on
-EPROBE_DEFER, so calling dev_err_probe() again here overwrites the
channel-specific deferred probe reason.  Also, since DMA is optional,
should errors other than -EPROBE_DEFER fall back to PIO with
omap->dma = NULL instead of failing the adapter probe?

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=3

Reply via email to