Skip to content

Commit 80c70bf

Browse files
andy-shevvinodkoul
authored andcommitted
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>
1 parent fe7b87d commit 80c70bf

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

include/linux/scatterlist.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,8 @@ static inline void sg_init_marker(struct scatterlist *sgl,
441441

442442
int sg_nents(struct scatterlist *sg);
443443
int sg_nents_for_len(struct scatterlist *sg, u64 len);
444+
int sg_nents_for_dma(struct scatterlist *sgl, unsigned int sglen, size_t len);
445+
444446
struct scatterlist *sg_last(struct scatterlist *s, unsigned int);
445447
void sg_init_table(struct scatterlist *, unsigned int);
446448
void sg_init_one(struct scatterlist *, const void *, unsigned int);

lib/scatterlist.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,32 @@ int sg_nents_for_len(struct scatterlist *sg, u64 len)
6464
}
6565
EXPORT_SYMBOL(sg_nents_for_len);
6666

67+
/**
68+
* sg_nents_for_dma - return the count of DMA-capable entries in scatterlist
69+
* @sgl: The scatterlist
70+
* @sglen: The current number of entries
71+
* @len: The maximum length of DMA-capable block
72+
*
73+
* Description:
74+
* Determines the number of entries in @sgl which would be permitted in
75+
* DMA-capable transfer if list had been split accordingly, taking into
76+
* account chaining as well.
77+
*
78+
* Returns:
79+
* the number of sgl entries needed
80+
*
81+
**/
82+
int sg_nents_for_dma(struct scatterlist *sgl, unsigned int sglen, size_t len)
83+
{
84+
struct scatterlist *sg;
85+
int i, nents = 0;
86+
87+
for_each_sg(sgl, sg, sglen, i)
88+
nents += DIV_ROUND_UP(sg_dma_len(sg), len);
89+
return nents;
90+
}
91+
EXPORT_SYMBOL(sg_nents_for_dma);
92+
6793
/**
6894
* sg_last - return the last scatterlist entry in a list
6995
* @sgl: First entry in the scatterlist

0 commit comments

Comments
 (0)