162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Public definitions for the CAAM/QI (Queue Interface) backend.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright 2013-2016 Freescale Semiconductor, Inc.
662306a36Sopenharmony_ci * Copyright 2016-2017, 2020 NXP
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#ifndef __QI_H__
1062306a36Sopenharmony_ci#define __QI_H__
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#include <crypto/algapi.h>
1362306a36Sopenharmony_ci#include <linux/compiler_attributes.h>
1462306a36Sopenharmony_ci#include <soc/fsl/qman.h>
1562306a36Sopenharmony_ci#include "compat.h"
1662306a36Sopenharmony_ci#include "desc.h"
1762306a36Sopenharmony_ci#include "desc_constr.h"
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci/* Length of a single buffer in the QI driver memory cache */
2062306a36Sopenharmony_ci#define CAAM_QI_MEMCACHE_SIZE	768
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ciextern bool caam_congested __read_mostly;
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci/*
2562306a36Sopenharmony_ci * This is the request structure the driver application should fill while
2662306a36Sopenharmony_ci * submitting a job to driver.
2762306a36Sopenharmony_ci */
2862306a36Sopenharmony_cistruct caam_drv_req;
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci/*
3162306a36Sopenharmony_ci * caam_qi_cbk - application's callback function invoked by the driver when the
3262306a36Sopenharmony_ci *               request has been successfully processed.
3362306a36Sopenharmony_ci * @drv_req: original request that was submitted
3462306a36Sopenharmony_ci * @status: completion status of request (0 - success, non-zero - error code)
3562306a36Sopenharmony_ci */
3662306a36Sopenharmony_citypedef void (*caam_qi_cbk)(struct caam_drv_req *drv_req, u32 status);
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_cienum optype {
3962306a36Sopenharmony_ci	ENCRYPT,
4062306a36Sopenharmony_ci	DECRYPT,
4162306a36Sopenharmony_ci	NUM_OP
4262306a36Sopenharmony_ci};
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci/**
4562306a36Sopenharmony_ci * caam_drv_ctx - CAAM/QI backend driver context
4662306a36Sopenharmony_ci *
4762306a36Sopenharmony_ci * The jobs are processed by the driver against a driver context.
4862306a36Sopenharmony_ci * With every cryptographic context, a driver context is attached.
4962306a36Sopenharmony_ci * The driver context contains data for private use by driver.
5062306a36Sopenharmony_ci * For the applications, this is an opaque structure.
5162306a36Sopenharmony_ci *
5262306a36Sopenharmony_ci * @prehdr: preheader placed before shrd desc
5362306a36Sopenharmony_ci * @sh_desc: shared descriptor
5462306a36Sopenharmony_ci * @context_a: shared descriptor dma address
5562306a36Sopenharmony_ci * @req_fq: to-CAAM request frame queue
5662306a36Sopenharmony_ci * @rsp_fq: from-CAAM response frame queue
5762306a36Sopenharmony_ci * @refcnt: reference counter incremented for each frame enqueued in to-CAAM FQ
5862306a36Sopenharmony_ci * @cpu: cpu on which to receive CAAM response
5962306a36Sopenharmony_ci * @op_type: operation type
6062306a36Sopenharmony_ci * @qidev: device pointer for CAAM/QI backend
6162306a36Sopenharmony_ci */
6262306a36Sopenharmony_cistruct caam_drv_ctx {
6362306a36Sopenharmony_ci	struct {
6462306a36Sopenharmony_ci		u32 prehdr[2];
6562306a36Sopenharmony_ci		u32 sh_desc[MAX_SDLEN];
6662306a36Sopenharmony_ci	} __aligned(CRYPTO_DMA_ALIGN);
6762306a36Sopenharmony_ci	dma_addr_t context_a;
6862306a36Sopenharmony_ci	struct qman_fq *req_fq;
6962306a36Sopenharmony_ci	struct qman_fq *rsp_fq;
7062306a36Sopenharmony_ci	refcount_t refcnt;
7162306a36Sopenharmony_ci	int cpu;
7262306a36Sopenharmony_ci	enum optype op_type;
7362306a36Sopenharmony_ci	struct device *qidev;
7462306a36Sopenharmony_ci};
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci/**
7762306a36Sopenharmony_ci * caam_drv_req - The request structure the driver application should fill while
7862306a36Sopenharmony_ci *                submitting a job to driver.
7962306a36Sopenharmony_ci * @fd_sgt: QMan S/G pointing to output (fd_sgt[0]) and input (fd_sgt[1])
8062306a36Sopenharmony_ci *          buffers.
8162306a36Sopenharmony_ci * @cbk: callback function to invoke when job is completed
8262306a36Sopenharmony_ci * @app_ctx: arbitrary context attached with request by the application
8362306a36Sopenharmony_ci *
8462306a36Sopenharmony_ci * The fields mentioned below should not be used by application.
8562306a36Sopenharmony_ci * These are for private use by driver.
8662306a36Sopenharmony_ci *
8762306a36Sopenharmony_ci * @hdr__: linked list header to maintain list of outstanding requests to CAAM
8862306a36Sopenharmony_ci * @hwaddr: DMA address for the S/G table.
8962306a36Sopenharmony_ci */
9062306a36Sopenharmony_cistruct caam_drv_req {
9162306a36Sopenharmony_ci	struct qm_sg_entry fd_sgt[2];
9262306a36Sopenharmony_ci	struct caam_drv_ctx *drv_ctx;
9362306a36Sopenharmony_ci	caam_qi_cbk cbk;
9462306a36Sopenharmony_ci	void *app_ctx;
9562306a36Sopenharmony_ci} __aligned(CRYPTO_DMA_ALIGN);
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci/**
9862306a36Sopenharmony_ci * caam_drv_ctx_init - Initialise a CAAM/QI driver context
9962306a36Sopenharmony_ci *
10062306a36Sopenharmony_ci * A CAAM/QI driver context must be attached with each cryptographic context.
10162306a36Sopenharmony_ci * This function allocates memory for CAAM/QI context and returns a handle to
10262306a36Sopenharmony_ci * the application. This handle must be submitted along with each enqueue
10362306a36Sopenharmony_ci * request to the driver by the application.
10462306a36Sopenharmony_ci *
10562306a36Sopenharmony_ci * @cpu: CPU where the application prefers to the driver to receive CAAM
10662306a36Sopenharmony_ci *       responses. The request completion callback would be issued from this
10762306a36Sopenharmony_ci *       CPU.
10862306a36Sopenharmony_ci * @sh_desc: shared descriptor pointer to be attached with CAAM/QI driver
10962306a36Sopenharmony_ci *           context.
11062306a36Sopenharmony_ci *
11162306a36Sopenharmony_ci * Returns a driver context on success or negative error code on failure.
11262306a36Sopenharmony_ci */
11362306a36Sopenharmony_cistruct caam_drv_ctx *caam_drv_ctx_init(struct device *qidev, int *cpu,
11462306a36Sopenharmony_ci				       u32 *sh_desc);
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci/**
11762306a36Sopenharmony_ci * caam_qi_enqueue - Submit a request to QI backend driver.
11862306a36Sopenharmony_ci *
11962306a36Sopenharmony_ci * The request structure must be properly filled as described above.
12062306a36Sopenharmony_ci *
12162306a36Sopenharmony_ci * @qidev: device pointer for QI backend
12262306a36Sopenharmony_ci * @req: CAAM QI request structure
12362306a36Sopenharmony_ci *
12462306a36Sopenharmony_ci * Returns 0 on success or negative error code on failure.
12562306a36Sopenharmony_ci */
12662306a36Sopenharmony_ciint caam_qi_enqueue(struct device *qidev, struct caam_drv_req *req);
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci/**
12962306a36Sopenharmony_ci * caam_drv_ctx_busy - Check if there are too many jobs pending with CAAM
13062306a36Sopenharmony_ci *		       or too many CAAM responses are pending to be processed.
13162306a36Sopenharmony_ci * @drv_ctx: driver context for which job is to be submitted
13262306a36Sopenharmony_ci *
13362306a36Sopenharmony_ci * Returns caam congestion status 'true/false'
13462306a36Sopenharmony_ci */
13562306a36Sopenharmony_cibool caam_drv_ctx_busy(struct caam_drv_ctx *drv_ctx);
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci/**
13862306a36Sopenharmony_ci * caam_drv_ctx_update - Update QI driver context
13962306a36Sopenharmony_ci *
14062306a36Sopenharmony_ci * Invoked when shared descriptor is required to be change in driver context.
14162306a36Sopenharmony_ci *
14262306a36Sopenharmony_ci * @drv_ctx: driver context to be updated
14362306a36Sopenharmony_ci * @sh_desc: new shared descriptor pointer to be updated in QI driver context
14462306a36Sopenharmony_ci *
14562306a36Sopenharmony_ci * Returns 0 on success or negative error code on failure.
14662306a36Sopenharmony_ci */
14762306a36Sopenharmony_ciint caam_drv_ctx_update(struct caam_drv_ctx *drv_ctx, u32 *sh_desc);
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci/**
15062306a36Sopenharmony_ci * caam_drv_ctx_rel - Release a QI driver context
15162306a36Sopenharmony_ci * @drv_ctx: context to be released
15262306a36Sopenharmony_ci */
15362306a36Sopenharmony_civoid caam_drv_ctx_rel(struct caam_drv_ctx *drv_ctx);
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ciint caam_qi_init(struct platform_device *pdev);
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci/**
15862306a36Sopenharmony_ci * qi_cache_alloc - Allocate buffers from CAAM-QI cache
15962306a36Sopenharmony_ci *
16062306a36Sopenharmony_ci * Invoked when a user of the CAAM-QI (i.e. caamalg-qi) needs data which has
16162306a36Sopenharmony_ci * to be allocated on the hotpath. Instead of using malloc, one can use the
16262306a36Sopenharmony_ci * services of the CAAM QI memory cache (backed by kmem_cache). The buffers
16362306a36Sopenharmony_ci * will have a size of 256B, which is sufficient for hosting 16 SG entries.
16462306a36Sopenharmony_ci *
16562306a36Sopenharmony_ci * @flags: flags that would be used for the equivalent malloc(..) call
16662306a36Sopenharmony_ci *
16762306a36Sopenharmony_ci * Returns a pointer to a retrieved buffer on success or NULL on failure.
16862306a36Sopenharmony_ci */
16962306a36Sopenharmony_civoid *qi_cache_alloc(gfp_t flags);
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci/**
17262306a36Sopenharmony_ci * qi_cache_free - Frees buffers allocated from CAAM-QI cache
17362306a36Sopenharmony_ci *
17462306a36Sopenharmony_ci * Invoked when a user of the CAAM-QI (i.e. caamalg-qi) no longer needs
17562306a36Sopenharmony_ci * the buffer previously allocated by a qi_cache_alloc call.
17662306a36Sopenharmony_ci * No checking is being done, the call is a passthrough call to
17762306a36Sopenharmony_ci * kmem_cache_free(...)
17862306a36Sopenharmony_ci *
17962306a36Sopenharmony_ci * @obj: object previously allocated using qi_cache_alloc()
18062306a36Sopenharmony_ci */
18162306a36Sopenharmony_civoid qi_cache_free(void *obj);
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci#endif /* __QI_H__ */
184