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