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