Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1227845

[PATCH v2 5/8] lib: introduce sg_nents_len_chained

From LABBE Corentin <clabbe.montjoie@gmail.com>
Newsgroups linux.kernel
Subject [PATCH v2 5/8] lib: introduce sg_nents_len_chained
Date 2015-09-18 15:00 +0200
Message-ID <qa3qH-33F-29@gated-at.bofh.it> (permalink)
References <qa3qG-33F-7@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


Some driver use a modified version of sg_nents_for_len with an
additional parameter bool *chained for knowing if the scatterlist is
chained or not.

So, for removing duplicate code, add sg_nents_len_chained in
lib/scatterlist.c

Signed-off-by: LABBE Corentin <clabbe.montjoie@gmail.com>
---
 include/linux/scatterlist.h |  1 +
 lib/scatterlist.c           | 40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)

diff --git a/include/linux/scatterlist.h b/include/linux/scatterlist.h
index 556ec1e..594cdb0 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_len_chained(struct scatterlist *sg, u64 len, bool *chained);
 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 bafa993..070e396 100644
--- a/lib/scatterlist.c
+++ b/lib/scatterlist.c
@@ -90,6 +90,46 @@ int sg_nents_for_len(struct scatterlist *sg, u64 len)
 EXPORT_SYMBOL(sg_nents_for_len);
 
 /**
+ * sg_nents_len_chained - return total count of entries in scatterlist
+ *                        needed to satisfy the supplied length
+ * @sg:		The scatterlist
+ * @len:	The total required length
+ * @chained	A pointer where to store if SG is chained or not
+ *
+ * Description:
+ * Determines the number of entries in sg that are required to meet
+ * the supplied length, taking into account chaining as well
+ * If the scatterlist is chained, set *chained to true.
+ *
+ * Returns:
+ *   the number of sg entries needed, negative error on failure
+ *
+ **/
+int sg_nents_len_chained(struct scatterlist *sg, u64 len, bool *chained)
+{
+	int nents;
+	u64 total;
+
+	if (chained)
+		*chained = false;
+
+	if (!len)
+		return 0;
+
+	for (nents = 0, total = 0; sg; sg = sg_next(sg)) {
+		nents++;
+		total += sg->length;
+		if (!sg_is_last(sg) && (sg + 1)->length == 0 && chained)
+			*chained = true;
+		if (total >= len)
+			return nents;
+	}
+
+	return -EINVAL;
+}
+EXPORT_SYMBOL(sg_nents_len_chained);
+
+/**
  * sg_last - return the last scatterlist entry in a list
  * @sgl:	First entry in the scatterlist
  * @nents:	Number of entries in the scatterlist
-- 
2.4.6

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH v2] crypto: Remove duplicate code of SG helpers functions LABBE Corentin <clabbe.montjoie@gmail.com> - 2015-09-18 15:00 +0200
  [PATCH v2 4/8] s390: replace zfcp_qdio_sbale_count by sg_nents LABBE Corentin <clabbe.montjoie@gmail.com> - 2015-09-18 15:00 +0200
  [PATCH v2 5/8] lib: introduce sg_nents_len_chained LABBE Corentin <clabbe.montjoie@gmail.com> - 2015-09-18 15:00 +0200
    Re: [PATCH v2 5/8] lib: introduce sg_nents_len_chained Tony Battersby <tonyb@cybernetics.com> - 2015-09-18 18:20 +0200
      Re: [PATCH v2 5/8] lib: introduce sg_nents_len_chained Tony Battersby <tonyb@cybernetics.com> - 2015-09-18 23:30 +0200
        Re: [PATCH v2 5/8] lib: introduce sg_nents_len_chained Herbert Xu <herbert@gondor.apana.org.au> - 2015-09-21 16:30 +0200
          Re: [PATCH v2 5/8] lib: introduce sg_nents_len_chained LABBE Corentin <clabbe.montjoie@gmail.com> - 2015-09-21 17:10 +0200
  [PATCH v2 3/8] crypto: sahara: replace sahara_sg_length with sg_nents_for_len LABBE Corentin <clabbe.montjoie@gmail.com> - 2015-09-18 15:00 +0200
  Re: [PATCH v2] crypto: Remove duplicate code of SG helpers functions Herbert Xu <herbert@gondor.apana.org.au> - 2015-09-21 17:10 +0200

csiph-web