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