acompress.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Asynchronous Compression operations
  4. *
  5. * Copyright (c) 2016, Intel Corporation
  6. * Authors: Weigang Li <[email protected]>
  7. * Giovanni Cabiddu <[email protected]>
  8. */
  9. #ifndef _CRYPTO_ACOMP_H
  10. #define _CRYPTO_ACOMP_H
  11. #include <linux/crypto.h>
  12. #define CRYPTO_ACOMP_ALLOC_OUTPUT 0x00000001
  13. /**
  14. * struct acomp_req - asynchronous (de)compression request
  15. *
  16. * @base: Common attributes for asynchronous crypto requests
  17. * @src: Source Data
  18. * @dst: Destination data
  19. * @slen: Size of the input buffer
  20. * @dlen: Size of the output buffer and number of bytes produced
  21. * @flags: Internal flags
  22. * @__ctx: Start of private context data
  23. */
  24. struct acomp_req {
  25. struct crypto_async_request base;
  26. struct scatterlist *src;
  27. struct scatterlist *dst;
  28. unsigned int slen;
  29. unsigned int dlen;
  30. u32 flags;
  31. void *__ctx[] CRYPTO_MINALIGN_ATTR;
  32. };
  33. /**
  34. * struct crypto_acomp - user-instantiated objects which encapsulate
  35. * algorithms and core processing logic
  36. *
  37. * @compress: Function performs a compress operation
  38. * @decompress: Function performs a de-compress operation
  39. * @dst_free: Frees destination buffer if allocated inside the
  40. * algorithm
  41. * @reqsize: Context size for (de)compression requests
  42. * @base: Common crypto API algorithm data structure
  43. */
  44. struct crypto_acomp {
  45. int (*compress)(struct acomp_req *req);
  46. int (*decompress)(struct acomp_req *req);
  47. void (*dst_free)(struct scatterlist *dst);
  48. unsigned int reqsize;
  49. struct crypto_tfm base;
  50. };
  51. /**
  52. * struct acomp_alg - asynchronous compression algorithm
  53. *
  54. * @compress: Function performs a compress operation
  55. * @decompress: Function performs a de-compress operation
  56. * @dst_free: Frees destination buffer if allocated inside the algorithm
  57. * @init: Initialize the cryptographic transformation object.
  58. * This function is used to initialize the cryptographic
  59. * transformation object. This function is called only once at
  60. * the instantiation time, right after the transformation context
  61. * was allocated. In case the cryptographic hardware has some
  62. * special requirements which need to be handled by software, this
  63. * function shall check for the precise requirement of the
  64. * transformation and put any software fallbacks in place.
  65. * @exit: Deinitialize the cryptographic transformation object. This is a
  66. * counterpart to @init, used to remove various changes set in
  67. * @init.
  68. *
  69. * @reqsize: Context size for (de)compression requests
  70. * @base: Common crypto API algorithm data structure
  71. */
  72. struct acomp_alg {
  73. int (*compress)(struct acomp_req *req);
  74. int (*decompress)(struct acomp_req *req);
  75. void (*dst_free)(struct scatterlist *dst);
  76. int (*init)(struct crypto_acomp *tfm);
  77. void (*exit)(struct crypto_acomp *tfm);
  78. unsigned int reqsize;
  79. struct crypto_alg base;
  80. };
  81. /**
  82. * DOC: Asynchronous Compression API
  83. *
  84. * The Asynchronous Compression API is used with the algorithms of type
  85. * CRYPTO_ALG_TYPE_ACOMPRESS (listed as type "acomp" in /proc/crypto)
  86. */
  87. /**
  88. * crypto_alloc_acomp() -- allocate ACOMPRESS tfm handle
  89. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  90. * compression algorithm e.g. "deflate"
  91. * @type: specifies the type of the algorithm
  92. * @mask: specifies the mask for the algorithm
  93. *
  94. * Allocate a handle for a compression algorithm. The returned struct
  95. * crypto_acomp is the handle that is required for any subsequent
  96. * API invocation for the compression operations.
  97. *
  98. * Return: allocated handle in case of success; IS_ERR() is true in case
  99. * of an error, PTR_ERR() returns the error code.
  100. */
  101. struct crypto_acomp *crypto_alloc_acomp(const char *alg_name, u32 type,
  102. u32 mask);
  103. /**
  104. * crypto_alloc_acomp_node() -- allocate ACOMPRESS tfm handle with desired NUMA node
  105. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  106. * compression algorithm e.g. "deflate"
  107. * @type: specifies the type of the algorithm
  108. * @mask: specifies the mask for the algorithm
  109. * @node: specifies the NUMA node the ZIP hardware belongs to
  110. *
  111. * Allocate a handle for a compression algorithm. Drivers should try to use
  112. * (de)compressors on the specified NUMA node.
  113. * The returned struct crypto_acomp is the handle that is required for any
  114. * subsequent API invocation for the compression operations.
  115. *
  116. * Return: allocated handle in case of success; IS_ERR() is true in case
  117. * of an error, PTR_ERR() returns the error code.
  118. */
  119. struct crypto_acomp *crypto_alloc_acomp_node(const char *alg_name, u32 type,
  120. u32 mask, int node);
  121. static inline struct crypto_tfm *crypto_acomp_tfm(struct crypto_acomp *tfm)
  122. {
  123. return &tfm->base;
  124. }
  125. static inline struct acomp_alg *__crypto_acomp_alg(struct crypto_alg *alg)
  126. {
  127. return container_of(alg, struct acomp_alg, base);
  128. }
  129. static inline struct crypto_acomp *__crypto_acomp_tfm(struct crypto_tfm *tfm)
  130. {
  131. return container_of(tfm, struct crypto_acomp, base);
  132. }
  133. static inline struct acomp_alg *crypto_acomp_alg(struct crypto_acomp *tfm)
  134. {
  135. return __crypto_acomp_alg(crypto_acomp_tfm(tfm)->__crt_alg);
  136. }
  137. static inline unsigned int crypto_acomp_reqsize(struct crypto_acomp *tfm)
  138. {
  139. return tfm->reqsize;
  140. }
  141. static inline void acomp_request_set_tfm(struct acomp_req *req,
  142. struct crypto_acomp *tfm)
  143. {
  144. req->base.tfm = crypto_acomp_tfm(tfm);
  145. }
  146. static inline struct crypto_acomp *crypto_acomp_reqtfm(struct acomp_req *req)
  147. {
  148. return __crypto_acomp_tfm(req->base.tfm);
  149. }
  150. /**
  151. * crypto_free_acomp() -- free ACOMPRESS tfm handle
  152. *
  153. * @tfm: ACOMPRESS tfm handle allocated with crypto_alloc_acomp()
  154. *
  155. * If @tfm is a NULL or error pointer, this function does nothing.
  156. */
  157. static inline void crypto_free_acomp(struct crypto_acomp *tfm)
  158. {
  159. crypto_destroy_tfm(tfm, crypto_acomp_tfm(tfm));
  160. }
  161. static inline int crypto_has_acomp(const char *alg_name, u32 type, u32 mask)
  162. {
  163. type &= ~CRYPTO_ALG_TYPE_MASK;
  164. type |= CRYPTO_ALG_TYPE_ACOMPRESS;
  165. mask |= CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
  166. return crypto_has_alg(alg_name, type, mask);
  167. }
  168. /**
  169. * acomp_request_alloc() -- allocates asynchronous (de)compression request
  170. *
  171. * @tfm: ACOMPRESS tfm handle allocated with crypto_alloc_acomp()
  172. *
  173. * Return: allocated handle in case of success or NULL in case of an error
  174. */
  175. struct acomp_req *acomp_request_alloc(struct crypto_acomp *tfm);
  176. /**
  177. * acomp_request_free() -- zeroize and free asynchronous (de)compression
  178. * request as well as the output buffer if allocated
  179. * inside the algorithm
  180. *
  181. * @req: request to free
  182. */
  183. void acomp_request_free(struct acomp_req *req);
  184. /**
  185. * acomp_request_set_callback() -- Sets an asynchronous callback
  186. *
  187. * Callback will be called when an asynchronous operation on a given
  188. * request is finished.
  189. *
  190. * @req: request that the callback will be set for
  191. * @flgs: specify for instance if the operation may backlog
  192. * @cmlp: callback which will be called
  193. * @data: private data used by the caller
  194. */
  195. static inline void acomp_request_set_callback(struct acomp_req *req,
  196. u32 flgs,
  197. crypto_completion_t cmpl,
  198. void *data)
  199. {
  200. req->base.complete = cmpl;
  201. req->base.data = data;
  202. req->base.flags = flgs;
  203. }
  204. /**
  205. * acomp_request_set_params() -- Sets request parameters
  206. *
  207. * Sets parameters required by an acomp operation
  208. *
  209. * @req: asynchronous compress request
  210. * @src: pointer to input buffer scatterlist
  211. * @dst: pointer to output buffer scatterlist. If this is NULL, the
  212. * acomp layer will allocate the output memory
  213. * @slen: size of the input buffer
  214. * @dlen: size of the output buffer. If dst is NULL, this can be used by
  215. * the user to specify the maximum amount of memory to allocate
  216. */
  217. static inline void acomp_request_set_params(struct acomp_req *req,
  218. struct scatterlist *src,
  219. struct scatterlist *dst,
  220. unsigned int slen,
  221. unsigned int dlen)
  222. {
  223. req->src = src;
  224. req->dst = dst;
  225. req->slen = slen;
  226. req->dlen = dlen;
  227. if (!req->dst)
  228. req->flags |= CRYPTO_ACOMP_ALLOC_OUTPUT;
  229. }
  230. /**
  231. * crypto_acomp_compress() -- Invoke asynchronous compress operation
  232. *
  233. * Function invokes the asynchronous compress operation
  234. *
  235. * @req: asynchronous compress request
  236. *
  237. * Return: zero on success; error code in case of error
  238. */
  239. static inline int crypto_acomp_compress(struct acomp_req *req)
  240. {
  241. struct crypto_acomp *tfm = crypto_acomp_reqtfm(req);
  242. struct crypto_alg *alg = tfm->base.__crt_alg;
  243. unsigned int slen = req->slen;
  244. int ret;
  245. crypto_stats_get(alg);
  246. ret = tfm->compress(req);
  247. crypto_stats_compress(slen, ret, alg);
  248. return ret;
  249. }
  250. /**
  251. * crypto_acomp_decompress() -- Invoke asynchronous decompress operation
  252. *
  253. * Function invokes the asynchronous decompress operation
  254. *
  255. * @req: asynchronous compress request
  256. *
  257. * Return: zero on success; error code in case of error
  258. */
  259. static inline int crypto_acomp_decompress(struct acomp_req *req)
  260. {
  261. struct crypto_acomp *tfm = crypto_acomp_reqtfm(req);
  262. struct crypto_alg *alg = tfm->base.__crt_alg;
  263. unsigned int slen = req->slen;
  264. int ret;
  265. crypto_stats_get(alg);
  266. ret = tfm->decompress(req);
  267. crypto_stats_decompress(slen, ret, alg);
  268. return ret;
  269. }
  270. #endif