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