summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAndy Shevchenko <andriy.shevchenko@linux.intel.com>2026-01-08 11:50:12 +0100
committerVinod Koul <vkoul@kernel.org>2026-01-09 08:36:00 +0530
commit80c70bfb95cdbe0c644070f4ca4754a60f0a4830 (patch)
tree79398b6fe665c89718f850cbfecef15e12bb7083 /lib
parentfe7b87d908da33326fbf6fe2b3830426432ec66c (diff)
scatterlist: introduce sg_nents_for_dma() helper
Sometimes the user needs to split each entry on the mapped scatter list due to DMA length constrains. This helper returns a number of entities assuming that each of them is not bigger than supplied maximum length. Reviewed-by: Bjorn Andersson <andersson@kernel.org> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Link: https://patch.msgid.link/20260108105619.3513561-2-andriy.shevchenko@linux.intel.com Signed-off-by: Vinod Koul <vkoul@kernel.org>
Diffstat (limited to 'lib')
-rw-r--r--lib/scatterlist.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/scatterlist.c b/lib/scatterlist.c
index 4af1c8b0775a..21bc9c1f7c06 100644
--- a/lib/scatterlist.c
+++ b/lib/scatterlist.c
@@ -65,6 +65,32 @@ int sg_nents_for_len(struct scatterlist *sg, u64 len)
EXPORT_SYMBOL(sg_nents_for_len);
/**
+ * sg_nents_for_dma - return the count of DMA-capable entries in scatterlist
+ * @sgl: The scatterlist
+ * @sglen: The current number of entries
+ * @len: The maximum length of DMA-capable block
+ *
+ * Description:
+ * Determines the number of entries in @sgl which would be permitted in
+ * DMA-capable transfer if list had been split accordingly, taking into
+ * account chaining as well.
+ *
+ * Returns:
+ * the number of sgl entries needed
+ *
+ **/
+int sg_nents_for_dma(struct scatterlist *sgl, unsigned int sglen, size_t len)
+{
+ struct scatterlist *sg;
+ int i, nents = 0;
+
+ for_each_sg(sgl, sg, sglen, i)
+ nents += DIV_ROUND_UP(sg_dma_len(sg), len);
+ return nents;
+}
+EXPORT_SYMBOL(sg_nents_for_dma);
+
+/**
* sg_last - return the last scatterlist entry in a list
* @sgl: First entry in the scatterlist
* @nents: Number of entries in the scatterlist