crypto: scatterwalk - Add support for calculating number of SG elements
authorJoel Fernandes <joelf@ti.com>
Sun, 18 Aug 2013 02:42:22 +0000 (21:42 -0500)
committerHerbert Xu <herbert@gondor.apana.org.au>
Wed, 21 Aug 2013 11:27:58 +0000 (21:27 +1000)
Crypto layer only passes nbytes to encrypt but in omap-aes driver we need to
know number of SG elements to pass to dmaengine slave API.  We add function for
the same to scatterwalk library.

Signed-off-by: Joel Fernandes <joelf@ti.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
crypto/scatterwalk.c
include/crypto/scatterwalk.h

index 7281b8a93ad3a4c486f3ca18721e73f79094659e..79ca2278c2a38a6c022d7d3ccbae30981434e238 100644 (file)
@@ -124,3 +124,25 @@ void scatterwalk_map_and_copy(void *buf, struct scatterlist *sg,
        scatterwalk_done(&walk, out, 0);
 }
 EXPORT_SYMBOL_GPL(scatterwalk_map_and_copy);
+
+int scatterwalk_bytes_sglen(struct scatterlist *sg, int num_bytes)
+{
+       int offset = 0, n = 0;
+
+       /* num_bytes is too small */
+       if (num_bytes < sg->length)
+               return -1;
+
+       do {
+               offset += sg->length;
+               n++;
+               sg = scatterwalk_sg_next(sg);
+
+               /* num_bytes is too large */
+               if (unlikely(!sg && (num_bytes < offset)))
+                       return -1;
+       } while (sg && (num_bytes > offset));
+
+       return n;
+}
+EXPORT_SYMBOL_GPL(scatterwalk_bytes_sglen);
index 3744d2a642dfbdf8a13141f0e2491b372a1d7a55..13621cc8cf4c454f546a6fbb33fb5e85e8b1b1aa 100644 (file)
@@ -113,4 +113,6 @@ void scatterwalk_done(struct scatter_walk *walk, int out, int more);
 void scatterwalk_map_and_copy(void *buf, struct scatterlist *sg,
                              unsigned int start, unsigned int nbytes, int out);
 
+int scatterwalk_bytes_sglen(struct scatterlist *sg, int num_bytes);
+
 #endif  /* _CRYPTO_SCATTERWALK_H */