qi.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Public definitions for the CAAM/QI (Queue Interface) backend.
  4. *
  5. * Copyright 2013-2016 Freescale Semiconductor, Inc.
  6. * Copyright 2016-2017 NXP
  7. */
  8. #ifndef __QI_H__
  9. #define __QI_H__
  10. #include <soc/fsl/qman.h>
  11. #include "compat.h"
  12. #include "desc.h"
  13. #include "desc_constr.h"
  14. /*
  15. * CAAM hardware constructs a job descriptor which points to a shared descriptor
  16. * (as pointed by context_a of to-CAAM FQ).
  17. * When the job descriptor is executed by DECO, the whole job descriptor
  18. * together with shared descriptor gets loaded in DECO buffer, which is
  19. * 64 words (each 32-bit) long.
  20. *
  21. * The job descriptor constructed by CAAM hardware has the following layout:
  22. *
  23. * HEADER (1 word)
  24. * Shdesc ptr (1 or 2 words)
  25. * SEQ_OUT_PTR (1 word)
  26. * Out ptr (1 or 2 words)
  27. * Out length (1 word)
  28. * SEQ_IN_PTR (1 word)
  29. * In ptr (1 or 2 words)
  30. * In length (1 word)
  31. *
  32. * The shdesc ptr is used to fetch shared descriptor contents into DECO buffer.
  33. *
  34. * Apart from shdesc contents, the total number of words that get loaded in DECO
  35. * buffer are '8' or '11'. The remaining words in DECO buffer can be used for
  36. * storing shared descriptor.
  37. */
  38. #define MAX_SDLEN ((CAAM_DESC_BYTES_MAX - DESC_JOB_IO_LEN) / CAAM_CMD_SZ)
  39. /* Length of a single buffer in the QI driver memory cache */
  40. #define CAAM_QI_MEMCACHE_SIZE 768
  41. extern bool caam_congested __read_mostly;
  42. /*
  43. * This is the request structure the driver application should fill while
  44. * submitting a job to driver.
  45. */
  46. struct caam_drv_req;
  47. /*
  48. * caam_qi_cbk - application's callback function invoked by the driver when the
  49. * request has been successfully processed.
  50. * @drv_req: original request that was submitted
  51. * @status: completion status of request (0 - success, non-zero - error code)
  52. */
  53. typedef void (*caam_qi_cbk)(struct caam_drv_req *drv_req, u32 status);
  54. enum optype {
  55. ENCRYPT,
  56. DECRYPT,
  57. GIVENCRYPT,
  58. NUM_OP
  59. };
  60. /**
  61. * caam_drv_ctx - CAAM/QI backend driver context
  62. *
  63. * The jobs are processed by the driver against a driver context.
  64. * With every cryptographic context, a driver context is attached.
  65. * The driver context contains data for private use by driver.
  66. * For the applications, this is an opaque structure.
  67. *
  68. * @prehdr: preheader placed before shrd desc
  69. * @sh_desc: shared descriptor
  70. * @context_a: shared descriptor dma address
  71. * @req_fq: to-CAAM request frame queue
  72. * @rsp_fq: from-CAAM response frame queue
  73. * @cpu: cpu on which to receive CAAM response
  74. * @op_type: operation type
  75. * @qidev: device pointer for CAAM/QI backend
  76. */
  77. struct caam_drv_ctx {
  78. u32 prehdr[2];
  79. u32 sh_desc[MAX_SDLEN];
  80. dma_addr_t context_a;
  81. struct qman_fq *req_fq;
  82. struct qman_fq *rsp_fq;
  83. int cpu;
  84. enum optype op_type;
  85. struct device *qidev;
  86. } ____cacheline_aligned;
  87. /**
  88. * caam_drv_req - The request structure the driver application should fill while
  89. * submitting a job to driver.
  90. * @fd_sgt: QMan S/G pointing to output (fd_sgt[0]) and input (fd_sgt[1])
  91. * buffers.
  92. * @cbk: callback function to invoke when job is completed
  93. * @app_ctx: arbitrary context attached with request by the application
  94. *
  95. * The fields mentioned below should not be used by application.
  96. * These are for private use by driver.
  97. *
  98. * @hdr__: linked list header to maintain list of outstanding requests to CAAM
  99. * @hwaddr: DMA address for the S/G table.
  100. */
  101. struct caam_drv_req {
  102. struct qm_sg_entry fd_sgt[2];
  103. struct caam_drv_ctx *drv_ctx;
  104. caam_qi_cbk cbk;
  105. void *app_ctx;
  106. } ____cacheline_aligned;
  107. /**
  108. * caam_drv_ctx_init - Initialise a CAAM/QI driver context
  109. *
  110. * A CAAM/QI driver context must be attached with each cryptographic context.
  111. * This function allocates memory for CAAM/QI context and returns a handle to
  112. * the application. This handle must be submitted along with each enqueue
  113. * request to the driver by the application.
  114. *
  115. * @cpu: CPU where the application prefers to the driver to receive CAAM
  116. * responses. The request completion callback would be issued from this
  117. * CPU.
  118. * @sh_desc: shared descriptor pointer to be attached with CAAM/QI driver
  119. * context.
  120. *
  121. * Returns a driver context on success or negative error code on failure.
  122. */
  123. struct caam_drv_ctx *caam_drv_ctx_init(struct device *qidev, int *cpu,
  124. u32 *sh_desc);
  125. /**
  126. * caam_qi_enqueue - Submit a request to QI backend driver.
  127. *
  128. * The request structure must be properly filled as described above.
  129. *
  130. * @qidev: device pointer for QI backend
  131. * @req: CAAM QI request structure
  132. *
  133. * Returns 0 on success or negative error code on failure.
  134. */
  135. int caam_qi_enqueue(struct device *qidev, struct caam_drv_req *req);
  136. /**
  137. * caam_drv_ctx_busy - Check if there are too many jobs pending with CAAM
  138. * or too many CAAM responses are pending to be processed.
  139. * @drv_ctx: driver context for which job is to be submitted
  140. *
  141. * Returns caam congestion status 'true/false'
  142. */
  143. bool caam_drv_ctx_busy(struct caam_drv_ctx *drv_ctx);
  144. /**
  145. * caam_drv_ctx_update - Update QI driver context
  146. *
  147. * Invoked when shared descriptor is required to be change in driver context.
  148. *
  149. * @drv_ctx: driver context to be updated
  150. * @sh_desc: new shared descriptor pointer to be updated in QI driver context
  151. *
  152. * Returns 0 on success or negative error code on failure.
  153. */
  154. int caam_drv_ctx_update(struct caam_drv_ctx *drv_ctx, u32 *sh_desc);
  155. /**
  156. * caam_drv_ctx_rel - Release a QI driver context
  157. * @drv_ctx: context to be released
  158. */
  159. void caam_drv_ctx_rel(struct caam_drv_ctx *drv_ctx);
  160. int caam_qi_init(struct platform_device *pdev);
  161. int caam_qi_shutdown(struct device *dev);
  162. /**
  163. * qi_cache_alloc - Allocate buffers from CAAM-QI cache
  164. *
  165. * Invoked when a user of the CAAM-QI (i.e. caamalg-qi) needs data which has
  166. * to be allocated on the hotpath. Instead of using malloc, one can use the
  167. * services of the CAAM QI memory cache (backed by kmem_cache). The buffers
  168. * will have a size of 256B, which is sufficient for hosting 16 SG entries.
  169. *
  170. * @flags: flags that would be used for the equivalent malloc(..) call
  171. *
  172. * Returns a pointer to a retrieved buffer on success or NULL on failure.
  173. */
  174. void *qi_cache_alloc(gfp_t flags);
  175. /**
  176. * qi_cache_free - Frees buffers allocated from CAAM-QI cache
  177. *
  178. * Invoked when a user of the CAAM-QI (i.e. caamalg-qi) no longer needs
  179. * the buffer previously allocated by a qi_cache_alloc call.
  180. * No checking is being done, the call is a passthrough call to
  181. * kmem_cache_free(...)
  182. *
  183. * @obj: object previously allocated using qi_cache_alloc()
  184. */
  185. void qi_cache_free(void *obj);
  186. #endif /* __QI_H__ */