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