[v1,01/10] scatterlist: introduce sg_nents_for_dma() helper
diff mbox

Message ID 20161021173535.100245-1-andriy.shevchenko@linux.intel.com
State Changes Requested
Headers show

Commit Message

Andy Shevchenko Oct. 21, 2016, 5:35 p.m. UTC
From: Andy Shevchenko <andy.shevchenko@gmail.com>

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.

Signed-off-by: Andy Shevchenko <andy.shevchenko@gmail.com>
---
 include/linux/scatterlist.h |  1 +
 lib/scatterlist.c           | 25 +++++++++++++++++++++++++
 2 files changed, 26 insertions(+)

Comments

Vinod Koul Nov. 14, 2016, 3:13 a.m. UTC | #1
On Fri, Oct 21, 2016 at 08:35:26PM +0300, Andy Shevchenko wrote:

> +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;
> +}

Should we have this in a global one as it is DMAengine specfic stuff..?
Andy Shevchenko Nov. 14, 2016, 7:06 a.m. UTC | #2
On Mon, Nov 14, 2016 at 5:13 AM, Vinod Koul <vinod.koul@intel.com> wrote:
> On Fri, Oct 21, 2016 at 08:35:26PM +0300, Andy Shevchenko wrote:
>
>> +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;
>> +}
>
> Should we have this in a global one as it is DMAengine specfic stuff..?

I think we may start as global for DMA engine. Anyway, I have to fix
the kbuild bot
warnings, meanwhile I move the stuff to local dmaengine.h.

Patch
diff mbox

diff --git a/include/linux/scatterlist.h b/include/linux/scatterlist.h
index cb3c8fe..d199667 100644
--- a/include/linux/scatterlist.h
+++ b/include/linux/scatterlist.h
@@ -243,6 +243,7 @@  static inline void *sg_virt(struct scatterlist *sg)
 
 int sg_nents(struct scatterlist *sg);
 int sg_nents_for_len(struct scatterlist *sg, u64 len);
+int sg_nents_for_dma(struct scatterlist *sgl, unsigned int sglen, size_t len);
 struct scatterlist *sg_next(struct scatterlist *);
 struct scatterlist *sg_last(struct scatterlist *s, unsigned int);
 void sg_init_table(struct scatterlist *, unsigned int);
diff --git a/lib/scatterlist.c b/lib/scatterlist.c
index 004fc70..5b75a21 100644
--- a/lib/scatterlist.c
+++ b/lib/scatterlist.c
@@ -90,6 +90,31 @@  int sg_nents_for_len(struct scatterlist *sg, u64 len)
 EXPORT_SYMBOL(sg_nents_for_len);
 
 /**
+ * sg_nents_for_dma - return 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;
+}
+
+/**
  * sg_last - return the last scatterlist entry in a list
  * @sgl:	First entry in the scatterlist
  * @nents:	Number of entries in the scatterlist