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