Re: [PATCH v2] dmaengine: qcom: bam_dma: Report the residue of packet

From: Frank Li

Date: Thu Dec 03 2026 - 15:49:32 EST


On Mon, Nov 16, 2026 at 04:49:17PM +0300, Nickolay Goppen wrote:
> The BAM writes back the number of bytes actually received into the
> descriptor that ended an RX transfer. Report this through the standard
> dmaengine_result (residue = length - transferred) so clients get the
> packet length from callback_result().
>
> Assisted-by: LLM
> Signed-off-by: Nickolay Goppen <setotau@xxxxxxxxxxxxxx>
> ---
> IPA v2.x uses BAM to send and receive IP packets, to and from the AP.
> However, unlike its predecessor BAM-DMUX, it doesn't send information
> about the packet length, but ipa2 driver will need that. To find the
> length of the packet, one must instead read the difference between
> packet length and residue.
> ---
> Changes in v2:
> - Fixed residues of old patch (sashiko)
> - changed readl_relaxed to readl for offset reading in proccess_channel_irqs (sashiko)
> - Link to v1: https://patch.msgid.link/20261115-qcom-bam-dma-metadata-v1-1-52e4de54ffd3@xxxxxxxxxxxxxx
>
> To: Vinod Koul <vkoul@xxxxxxxxxx>
> To: Frank Li <Frank.Li@xxxxxxxxxx>
> Cc: linux-arm-msm@xxxxxxxxxxxxxxx
> Cc: dmaengine@xxxxxxxxxxxxxxx
> Cc: linux-kernel@xxxxxxxxxxxxxxx
> ---
> drivers/dma/qcom/bam_dma.c | 33 ++++++++++++++++++++++++++++++++-
> 1 file changed, 32 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/dma/qcom/bam_dma.c b/drivers/dma/qcom/bam_dma.c
> index e62e411dc8f8..bf0c25d7c111 100644
> --- a/drivers/dma/qcom/bam_dma.c
> +++ b/drivers/dma/qcom/bam_dma.c
> @@ -65,6 +65,7 @@ struct bam_async_desc {
>
> u32 num_desc;
> u32 xfer_len;
> + size_t transferred;
>
> /* transaction flags, EOT|EOB|NWD */
> u16 flags;
> @@ -447,6 +448,31 @@ static inline void __iomem *bam_addr(struct bam_device *bdev, u32 pipe,
> r.ee_mult * bdev->ee;
> }
>
> +/**
> + * bam_fifo_xfer_len - bytes transferred for a completed descriptor chunk
> + * @bchan: BAM channel the chunk was queued on
> + * @async_desc: BAM async descriptor owning the chunk
> + *
> + * For RX pipes the BAM overwrites the size field of the descriptor that
> + * ended the transfer with the number of bytes actually written to memory,
> + * which can be less than the buffer size (e.g. an IPA packet). Those
> + * values live in the FIFO copy, not in async_desc->desc[].
> + */
> +
> +static u32 bam_fifo_xfer_len(struct bam_chan *bchan,
> + const struct bam_async_desc *async_desc)
> +{
> + struct bam_desc_hw *fifo = PTR_ALIGN(bchan->fifo_virt,
> + sizeof(struct bam_desc_hw));
> + u32 len = 0;
> + unsigned int i;
> +
> + for (i = bchan->head; i < bchan->head + async_desc->xfer_len; i++)
> + len += le16_to_cpu(fifo[i % MAX_DESCRIPTORS].size);
> +
> + return len;
> +}
> +
> /**
> * bam_reset() - reset and initialize BAM registers
> * @bdev: bam device
> @@ -865,7 +891,7 @@ static u32 process_channel_irqs(struct bam_device *bdev)
>
> guard(spinlock_irqsave)(&bchan->vc.lock);
>
> - offset = readl_relaxed(bam_addr(bdev, i, BAM_P_SW_OFSTS)) &
> + offset = readl(bam_addr(bdev, i, BAM_P_SW_OFSTS)) &
> P_SW_OFSTS_MASK;

Use differece patch fix this problem.

> offset /= sizeof(struct bam_desc_hw);
>
> @@ -881,6 +907,11 @@ static u32 process_channel_irqs(struct bam_device *bdev)
> if (avail < async_desc->xfer_len)
> break;
>
> + async_desc->transferred +=
> + bam_fifo_xfer_len(bchan, async_desc);

why += here

suppose below logic should work
async_desc->vd.tx_result.residue = async_desc->length - bam_fifo_xfer_len(bchan, async_desc);

Frank

> + async_desc->vd.tx_result.residue =
> + async_desc->length - async_desc->transferred;
> +
> /* manage FIFO */
> bchan->head += async_desc->xfer_len;
> bchan->head %= MAX_DESCRIPTORS;
>
> ---
> base-commit: 2cc223d6ffadd48e25eff0c8a03108a5d7a61c41
> change-id: 20261115-qcom-bam-dma-metadata-f34e592d400c
>
> Best regards,
> --
A
> Nickolay Goppen <setotau@xxxxxxxxxxxxxx>