GNU Linux-libre 5.10.153-gnu1
[releases.git] / drivers / crypto / hisilicon / sec2 / sec.h
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         struct sec_sqe sec_sqe;
42         struct sec_ctx *ctx;
43         struct sec_qp_ctx *qp_ctx;
44
45         struct sec_cipher_req c_req;
46         struct sec_aead_req aead_req;
47         struct list_head backlog_head;
48
49         int err_type;
50         int req_id;
51         u32 flag;
52
53         /* Status of the SEC request */
54         bool fake_busy;
55         bool use_pbuf;
56 };
57
58 /**
59  * struct sec_req_op - Operations for SEC request
60  * @buf_map: DMA map the SGL buffers of the request
61  * @buf_unmap: DMA unmap the SGL buffers of the request
62  * @bd_fill: Fill the SEC queue BD
63  * @bd_send: Send the SEC BD into the hardware queue
64  * @callback: Call back for the request
65  * @process: Main processing logic of Skcipher
66  */
67 struct sec_req_op {
68         int (*buf_map)(struct sec_ctx *ctx, struct sec_req *req);
69         void (*buf_unmap)(struct sec_ctx *ctx, struct sec_req *req);
70         void (*do_transfer)(struct sec_ctx *ctx, struct sec_req *req);
71         int (*bd_fill)(struct sec_ctx *ctx, struct sec_req *req);
72         int (*bd_send)(struct sec_ctx *ctx, struct sec_req *req);
73         void (*callback)(struct sec_ctx *ctx, struct sec_req *req, int err);
74         int (*process)(struct sec_ctx *ctx, struct sec_req *req);
75 };
76
77 /* SEC auth context */
78 struct sec_auth_ctx {
79         dma_addr_t a_key_dma;
80         u8 *a_key;
81         u8 a_key_len;
82         u8 mac_len;
83         u8 a_alg;
84         struct crypto_shash *hash_tfm;
85 };
86
87 /* SEC cipher context which cipher's relatives */
88 struct sec_cipher_ctx {
89         u8 *c_key;
90         dma_addr_t c_key_dma;
91         sector_t iv_offset;
92         u32 c_gran_size;
93         u32 ivsize;
94         u8 c_mode;
95         u8 c_alg;
96         u8 c_key_len;
97 };
98
99 /* SEC queue context which defines queue's relatives */
100 struct sec_qp_ctx {
101         struct hisi_qp *qp;
102         struct sec_req *req_list[QM_Q_DEPTH];
103         struct idr req_idr;
104         struct sec_alg_res res[QM_Q_DEPTH];
105         struct sec_ctx *ctx;
106         spinlock_t req_lock;
107         struct list_head backlog;
108         struct hisi_acc_sgl_pool *c_in_pool;
109         struct hisi_acc_sgl_pool *c_out_pool;
110         atomic_t pending_reqs;
111 };
112
113 enum sec_alg_type {
114         SEC_SKCIPHER,
115         SEC_AEAD
116 };
117
118 /* SEC Crypto TFM context which defines queue and cipher .etc relatives */
119 struct sec_ctx {
120         struct sec_qp_ctx *qp_ctx;
121         struct sec_dev *sec;
122         const struct sec_req_op *req_op;
123         struct hisi_qp **qps;
124
125         /* Half queues for encipher, and half for decipher */
126         u32 hlf_q_num;
127
128         /* Threshold for fake busy, trigger to return -EBUSY to user */
129         u32 fake_req_limit;
130
131         /* Currrent cyclic index to select a queue for encipher */
132         atomic_t enc_qcyclic;
133
134          /* Currrent cyclic index to select a queue for decipher */
135         atomic_t dec_qcyclic;
136
137         enum sec_alg_type alg_type;
138         bool pbuf_supported;
139         struct sec_cipher_ctx c_ctx;
140         struct sec_auth_ctx a_ctx;
141         struct device *dev;
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         unsigned long status;
183 };
184
185 void sec_destroy_qps(struct hisi_qp **qps, int qp_num);
186 struct hisi_qp **sec_create_qps(void);
187 int sec_register_to_crypto(void);
188 void sec_unregister_from_crypto(void);
189 #endif