acompress.h 7.7 KB

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