Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /* Copyright (c) 2019 HiSilicon Limited. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #ifndef __HISI_SEC_V2_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #define __HISI_SEC_V2_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include "../qm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include "sec_crypto.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) /* Algorithm resource per hardware SEC queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) struct sec_alg_res {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 	u8 *pbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 	dma_addr_t pbuf_dma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	u8 *c_ivin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	dma_addr_t c_ivin_dma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	u8 *out_mac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	dma_addr_t out_mac_dma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) /* Cipher request of SEC private */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) struct sec_cipher_req {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	struct hisi_acc_hw_sgl *c_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	dma_addr_t c_in_dma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct hisi_acc_hw_sgl *c_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	dma_addr_t c_out_dma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	u8 *c_ivin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	dma_addr_t c_ivin_dma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	struct skcipher_request *sk_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	u32 c_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	bool encrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) struct sec_aead_req {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	u8 *out_mac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	dma_addr_t out_mac_dma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	struct aead_request *aead_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) /* SEC request of Crypto */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) struct sec_req {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	struct sec_sqe sec_sqe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	struct sec_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	struct sec_qp_ctx *qp_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct sec_cipher_req c_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	struct sec_aead_req aead_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	struct list_head backlog_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	int err_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	int req_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	int flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	/* Status of the SEC request */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	bool fake_busy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	bool use_pbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  * struct sec_req_op - Operations for SEC request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)  * @buf_map: DMA map the SGL buffers of the request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  * @buf_unmap: DMA unmap the SGL buffers of the request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)  * @bd_fill: Fill the SEC queue BD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65)  * @bd_send: Send the SEC BD into the hardware queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66)  * @callback: Call back for the request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)  * @process: Main processing logic of Skcipher
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) struct sec_req_op {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	int (*buf_map)(struct sec_ctx *ctx, struct sec_req *req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	void (*buf_unmap)(struct sec_ctx *ctx, struct sec_req *req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	void (*do_transfer)(struct sec_ctx *ctx, struct sec_req *req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	int (*bd_fill)(struct sec_ctx *ctx, struct sec_req *req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	int (*bd_send)(struct sec_ctx *ctx, struct sec_req *req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	void (*callback)(struct sec_ctx *ctx, struct sec_req *req, int err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	int (*process)(struct sec_ctx *ctx, struct sec_req *req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) /* SEC auth context */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) struct sec_auth_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	dma_addr_t a_key_dma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	u8 *a_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	u8 a_key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	u8 mac_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	u8 a_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	struct crypto_shash *hash_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) /* SEC cipher context which cipher's relatives */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) struct sec_cipher_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	u8 *c_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	dma_addr_t c_key_dma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	sector_t iv_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	u32 c_gran_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	u32 ivsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	u8 c_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	u8 c_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	u8 c_key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) /* SEC queue context which defines queue's relatives */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct sec_qp_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	struct hisi_qp *qp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	struct sec_req *req_list[QM_Q_DEPTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	struct idr req_idr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	struct sec_alg_res res[QM_Q_DEPTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	struct sec_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	struct mutex req_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	struct list_head backlog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	struct hisi_acc_sgl_pool *c_in_pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	struct hisi_acc_sgl_pool *c_out_pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	atomic_t pending_reqs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) enum sec_alg_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	SEC_SKCIPHER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	SEC_AEAD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) /* SEC Crypto TFM context which defines queue and cipher .etc relatives */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct sec_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	struct sec_qp_ctx *qp_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	struct sec_dev *sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	const struct sec_req_op *req_op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	struct hisi_qp **qps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	/* Half queues for encipher, and half for decipher */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	u32 hlf_q_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	/* Threshold for fake busy, trigger to return -EBUSY to user */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	u32 fake_req_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	/* Currrent cyclic index to select a queue for encipher */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	atomic_t enc_qcyclic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	 /* Currrent cyclic index to select a queue for decipher */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	atomic_t dec_qcyclic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	enum sec_alg_type alg_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	bool pbuf_supported;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	struct sec_cipher_ctx c_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	struct sec_auth_ctx a_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) enum sec_endian {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	SEC_LE = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	SEC_32BE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	SEC_64BE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) enum sec_debug_file_index {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	SEC_CURRENT_QM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	SEC_CLEAR_ENABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	SEC_DEBUG_FILE_NUM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct sec_debug_file {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	enum sec_debug_file_index index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	struct hisi_qm *qm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) struct sec_dfx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	atomic64_t send_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	atomic64_t recv_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	atomic64_t send_busy_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	atomic64_t recv_busy_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	atomic64_t err_bd_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	atomic64_t invalid_req_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	atomic64_t done_flag_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) struct sec_debug {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	struct sec_dfx dfx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	struct sec_debug_file files[SEC_DEBUG_FILE_NUM];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) struct sec_dev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	struct hisi_qm qm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	struct sec_debug debug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	u32 ctx_q_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	bool iommu_used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	unsigned long status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) void sec_destroy_qps(struct hisi_qp **qps, int qp_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) struct hisi_qp **sec_create_qps(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) int sec_register_to_crypto(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) void sec_unregister_from_crypto(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) #endif