xref: /kernel/linux/linux-5.10/drivers/crypto/caam/qi.h (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Public definitions for the CAAM/QI (Queue Interface) backend.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright 2013-2016 Freescale Semiconductor, Inc.
68c2ecf20Sopenharmony_ci * Copyright 2016-2017, 2020 NXP
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#ifndef __QI_H__
108c2ecf20Sopenharmony_ci#define __QI_H__
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <soc/fsl/qman.h>
138c2ecf20Sopenharmony_ci#include "compat.h"
148c2ecf20Sopenharmony_ci#include "desc.h"
158c2ecf20Sopenharmony_ci#include "desc_constr.h"
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci/* Length of a single buffer in the QI driver memory cache */
188c2ecf20Sopenharmony_ci#define CAAM_QI_MEMCACHE_SIZE	768
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ciextern bool caam_congested __read_mostly;
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/*
238c2ecf20Sopenharmony_ci * This is the request structure the driver application should fill while
248c2ecf20Sopenharmony_ci * submitting a job to driver.
258c2ecf20Sopenharmony_ci */
268c2ecf20Sopenharmony_cistruct caam_drv_req;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/*
298c2ecf20Sopenharmony_ci * caam_qi_cbk - application's callback function invoked by the driver when the
308c2ecf20Sopenharmony_ci *               request has been successfully processed.
318c2ecf20Sopenharmony_ci * @drv_req: original request that was submitted
328c2ecf20Sopenharmony_ci * @status: completion status of request (0 - success, non-zero - error code)
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_citypedef void (*caam_qi_cbk)(struct caam_drv_req *drv_req, u32 status);
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cienum optype {
378c2ecf20Sopenharmony_ci	ENCRYPT,
388c2ecf20Sopenharmony_ci	DECRYPT,
398c2ecf20Sopenharmony_ci	NUM_OP
408c2ecf20Sopenharmony_ci};
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci/**
438c2ecf20Sopenharmony_ci * caam_drv_ctx - CAAM/QI backend driver context
448c2ecf20Sopenharmony_ci *
458c2ecf20Sopenharmony_ci * The jobs are processed by the driver against a driver context.
468c2ecf20Sopenharmony_ci * With every cryptographic context, a driver context is attached.
478c2ecf20Sopenharmony_ci * The driver context contains data for private use by driver.
488c2ecf20Sopenharmony_ci * For the applications, this is an opaque structure.
498c2ecf20Sopenharmony_ci *
508c2ecf20Sopenharmony_ci * @prehdr: preheader placed before shrd desc
518c2ecf20Sopenharmony_ci * @sh_desc: shared descriptor
528c2ecf20Sopenharmony_ci * @context_a: shared descriptor dma address
538c2ecf20Sopenharmony_ci * @req_fq: to-CAAM request frame queue
548c2ecf20Sopenharmony_ci * @rsp_fq: from-CAAM response frame queue
558c2ecf20Sopenharmony_ci * @refcnt: reference counter incremented for each frame enqueued in to-CAAM FQ
568c2ecf20Sopenharmony_ci * @cpu: cpu on which to receive CAAM response
578c2ecf20Sopenharmony_ci * @op_type: operation type
588c2ecf20Sopenharmony_ci * @qidev: device pointer for CAAM/QI backend
598c2ecf20Sopenharmony_ci */
608c2ecf20Sopenharmony_cistruct caam_drv_ctx {
618c2ecf20Sopenharmony_ci	u32 prehdr[2];
628c2ecf20Sopenharmony_ci	u32 sh_desc[MAX_SDLEN];
638c2ecf20Sopenharmony_ci	dma_addr_t context_a;
648c2ecf20Sopenharmony_ci	struct qman_fq *req_fq;
658c2ecf20Sopenharmony_ci	struct qman_fq *rsp_fq;
668c2ecf20Sopenharmony_ci	refcount_t refcnt;
678c2ecf20Sopenharmony_ci	int cpu;
688c2ecf20Sopenharmony_ci	enum optype op_type;
698c2ecf20Sopenharmony_ci	struct device *qidev;
708c2ecf20Sopenharmony_ci} ____cacheline_aligned;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci/**
738c2ecf20Sopenharmony_ci * caam_drv_req - The request structure the driver application should fill while
748c2ecf20Sopenharmony_ci *                submitting a job to driver.
758c2ecf20Sopenharmony_ci * @fd_sgt: QMan S/G pointing to output (fd_sgt[0]) and input (fd_sgt[1])
768c2ecf20Sopenharmony_ci *          buffers.
778c2ecf20Sopenharmony_ci * @cbk: callback function to invoke when job is completed
788c2ecf20Sopenharmony_ci * @app_ctx: arbitrary context attached with request by the application
798c2ecf20Sopenharmony_ci *
808c2ecf20Sopenharmony_ci * The fields mentioned below should not be used by application.
818c2ecf20Sopenharmony_ci * These are for private use by driver.
828c2ecf20Sopenharmony_ci *
838c2ecf20Sopenharmony_ci * @hdr__: linked list header to maintain list of outstanding requests to CAAM
848c2ecf20Sopenharmony_ci * @hwaddr: DMA address for the S/G table.
858c2ecf20Sopenharmony_ci */
868c2ecf20Sopenharmony_cistruct caam_drv_req {
878c2ecf20Sopenharmony_ci	struct qm_sg_entry fd_sgt[2];
888c2ecf20Sopenharmony_ci	struct caam_drv_ctx *drv_ctx;
898c2ecf20Sopenharmony_ci	caam_qi_cbk cbk;
908c2ecf20Sopenharmony_ci	void *app_ctx;
918c2ecf20Sopenharmony_ci} ____cacheline_aligned;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci/**
948c2ecf20Sopenharmony_ci * caam_drv_ctx_init - Initialise a CAAM/QI driver context
958c2ecf20Sopenharmony_ci *
968c2ecf20Sopenharmony_ci * A CAAM/QI driver context must be attached with each cryptographic context.
978c2ecf20Sopenharmony_ci * This function allocates memory for CAAM/QI context and returns a handle to
988c2ecf20Sopenharmony_ci * the application. This handle must be submitted along with each enqueue
998c2ecf20Sopenharmony_ci * request to the driver by the application.
1008c2ecf20Sopenharmony_ci *
1018c2ecf20Sopenharmony_ci * @cpu: CPU where the application prefers to the driver to receive CAAM
1028c2ecf20Sopenharmony_ci *       responses. The request completion callback would be issued from this
1038c2ecf20Sopenharmony_ci *       CPU.
1048c2ecf20Sopenharmony_ci * @sh_desc: shared descriptor pointer to be attached with CAAM/QI driver
1058c2ecf20Sopenharmony_ci *           context.
1068c2ecf20Sopenharmony_ci *
1078c2ecf20Sopenharmony_ci * Returns a driver context on success or negative error code on failure.
1088c2ecf20Sopenharmony_ci */
1098c2ecf20Sopenharmony_cistruct caam_drv_ctx *caam_drv_ctx_init(struct device *qidev, int *cpu,
1108c2ecf20Sopenharmony_ci				       u32 *sh_desc);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci/**
1138c2ecf20Sopenharmony_ci * caam_qi_enqueue - Submit a request to QI backend driver.
1148c2ecf20Sopenharmony_ci *
1158c2ecf20Sopenharmony_ci * The request structure must be properly filled as described above.
1168c2ecf20Sopenharmony_ci *
1178c2ecf20Sopenharmony_ci * @qidev: device pointer for QI backend
1188c2ecf20Sopenharmony_ci * @req: CAAM QI request structure
1198c2ecf20Sopenharmony_ci *
1208c2ecf20Sopenharmony_ci * Returns 0 on success or negative error code on failure.
1218c2ecf20Sopenharmony_ci */
1228c2ecf20Sopenharmony_ciint caam_qi_enqueue(struct device *qidev, struct caam_drv_req *req);
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci/**
1258c2ecf20Sopenharmony_ci * caam_drv_ctx_busy - Check if there are too many jobs pending with CAAM
1268c2ecf20Sopenharmony_ci *		       or too many CAAM responses are pending to be processed.
1278c2ecf20Sopenharmony_ci * @drv_ctx: driver context for which job is to be submitted
1288c2ecf20Sopenharmony_ci *
1298c2ecf20Sopenharmony_ci * Returns caam congestion status 'true/false'
1308c2ecf20Sopenharmony_ci */
1318c2ecf20Sopenharmony_cibool caam_drv_ctx_busy(struct caam_drv_ctx *drv_ctx);
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci/**
1348c2ecf20Sopenharmony_ci * caam_drv_ctx_update - Update QI driver context
1358c2ecf20Sopenharmony_ci *
1368c2ecf20Sopenharmony_ci * Invoked when shared descriptor is required to be change in driver context.
1378c2ecf20Sopenharmony_ci *
1388c2ecf20Sopenharmony_ci * @drv_ctx: driver context to be updated
1398c2ecf20Sopenharmony_ci * @sh_desc: new shared descriptor pointer to be updated in QI driver context
1408c2ecf20Sopenharmony_ci *
1418c2ecf20Sopenharmony_ci * Returns 0 on success or negative error code on failure.
1428c2ecf20Sopenharmony_ci */
1438c2ecf20Sopenharmony_ciint caam_drv_ctx_update(struct caam_drv_ctx *drv_ctx, u32 *sh_desc);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci/**
1468c2ecf20Sopenharmony_ci * caam_drv_ctx_rel - Release a QI driver context
1478c2ecf20Sopenharmony_ci * @drv_ctx: context to be released
1488c2ecf20Sopenharmony_ci */
1498c2ecf20Sopenharmony_civoid caam_drv_ctx_rel(struct caam_drv_ctx *drv_ctx);
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ciint caam_qi_init(struct platform_device *pdev);
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci/**
1548c2ecf20Sopenharmony_ci * qi_cache_alloc - Allocate buffers from CAAM-QI cache
1558c2ecf20Sopenharmony_ci *
1568c2ecf20Sopenharmony_ci * Invoked when a user of the CAAM-QI (i.e. caamalg-qi) needs data which has
1578c2ecf20Sopenharmony_ci * to be allocated on the hotpath. Instead of using malloc, one can use the
1588c2ecf20Sopenharmony_ci * services of the CAAM QI memory cache (backed by kmem_cache). The buffers
1598c2ecf20Sopenharmony_ci * will have a size of 256B, which is sufficient for hosting 16 SG entries.
1608c2ecf20Sopenharmony_ci *
1618c2ecf20Sopenharmony_ci * @flags: flags that would be used for the equivalent malloc(..) call
1628c2ecf20Sopenharmony_ci *
1638c2ecf20Sopenharmony_ci * Returns a pointer to a retrieved buffer on success or NULL on failure.
1648c2ecf20Sopenharmony_ci */
1658c2ecf20Sopenharmony_civoid *qi_cache_alloc(gfp_t flags);
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci/**
1688c2ecf20Sopenharmony_ci * qi_cache_free - Frees buffers allocated from CAAM-QI cache
1698c2ecf20Sopenharmony_ci *
1708c2ecf20Sopenharmony_ci * Invoked when a user of the CAAM-QI (i.e. caamalg-qi) no longer needs
1718c2ecf20Sopenharmony_ci * the buffer previously allocated by a qi_cache_alloc call.
1728c2ecf20Sopenharmony_ci * No checking is being done, the call is a passthrough call to
1738c2ecf20Sopenharmony_ci * kmem_cache_free(...)
1748c2ecf20Sopenharmony_ci *
1758c2ecf20Sopenharmony_ci * @obj: object previously allocated using qi_cache_alloc()
1768c2ecf20Sopenharmony_ci */
1778c2ecf20Sopenharmony_civoid qi_cache_free(void *obj);
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci#endif /* __QI_H__ */
180