qi.h 5.7 KB

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