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 /* Cipher resource per hardware SEC queue */
13 struct sec_cipher_res {
14 	u8 *c_ivin;
15 	dma_addr_t c_ivin_dma;
16 };
17 
18 /* Cipher request of SEC private */
19 struct sec_cipher_req {
20 	struct hisi_acc_hw_sgl *c_in;
21 	dma_addr_t c_in_dma;
22 	struct hisi_acc_hw_sgl *c_out;
23 	dma_addr_t c_out_dma;
24 	u8 *c_ivin;
25 	dma_addr_t c_ivin_dma;
26 	struct skcipher_request *sk_req;
27 	u32 c_len;
28 	bool encrypt;
29 };
30 
31 /* SEC request of Crypto */
32 struct sec_req {
33 	struct sec_sqe sec_sqe;
34 	struct sec_ctx *ctx;
35 	struct sec_qp_ctx *qp_ctx;
36 
37 	/* Cipher supported only at present */
38 	struct sec_cipher_req c_req;
39 	int err_type;
40 	int req_id;
41 
42 	/* Status of the SEC request */
43 	int fake_busy;
44 };
45 
46 /**
47  * struct sec_req_op - Operations for SEC request
48  * @get_res: Get resources for TFM on the SEC device
49  * @resource_alloc: Allocate resources for queue context on the SEC device
50  * @resource_free: Free resources for queue context on the SEC device
51  * @buf_map: DMA map the SGL buffers of the request
52  * @buf_unmap: DMA unmap the SGL buffers of the request
53  * @bd_fill: Fill the SEC queue BD
54  * @bd_send: Send the SEC BD into the hardware queue
55  * @callback: Call back for the request
56  * @process: Main processing logic of Skcipher
57  */
58 struct sec_req_op {
59 	int (*get_res)(struct sec_ctx *ctx, struct sec_req *req);
60 	int (*resource_alloc)(struct sec_ctx *ctx, struct sec_qp_ctx *qp_ctx);
61 	void (*resource_free)(struct sec_ctx *ctx, struct sec_qp_ctx *qp_ctx);
62 	int (*buf_map)(struct sec_ctx *ctx, struct sec_req *req);
63 	void (*buf_unmap)(struct sec_ctx *ctx, struct sec_req *req);
64 	void (*do_transfer)(struct sec_ctx *ctx, struct sec_req *req);
65 	int (*bd_fill)(struct sec_ctx *ctx, struct sec_req *req);
66 	int (*bd_send)(struct sec_ctx *ctx, struct sec_req *req);
67 	void (*callback)(struct sec_ctx *ctx, struct sec_req *req);
68 	int (*process)(struct sec_ctx *ctx, struct sec_req *req);
69 };
70 
71 /* SEC cipher context which cipher's relatives */
72 struct sec_cipher_ctx {
73 	u8 *c_key;
74 	dma_addr_t c_key_dma;
75 	sector_t iv_offset;
76 	u32 c_gran_size;
77 	u32 ivsize;
78 	u8 c_mode;
79 	u8 c_alg;
80 	u8 c_key_len;
81 };
82 
83 /* SEC queue context which defines queue's relatives */
84 struct sec_qp_ctx {
85 	struct hisi_qp *qp;
86 	struct sec_req **req_list;
87 	struct idr req_idr;
88 	void *alg_meta_data;
89 	struct sec_ctx *ctx;
90 	struct mutex req_lock;
91 	struct hisi_acc_sgl_pool *c_in_pool;
92 	struct hisi_acc_sgl_pool *c_out_pool;
93 	atomic_t pending_reqs;
94 };
95 
96 /* SEC Crypto TFM context which defines queue and cipher .etc relatives */
97 struct sec_ctx {
98 	struct sec_qp_ctx *qp_ctx;
99 	struct sec_dev *sec;
100 	const struct sec_req_op *req_op;
101 
102 	/* Half queues for encipher, and half for decipher */
103 	u32 hlf_q_num;
104 
105 	/* Threshold for fake busy, trigger to return -EBUSY to user */
106 	u32 fake_req_limit;
107 
108 	/* Currrent cyclic index to select a queue for encipher */
109 	atomic_t enc_qcyclic;
110 
111 	 /* Currrent cyclic index to select a queue for decipher */
112 	atomic_t dec_qcyclic;
113 	struct sec_cipher_ctx c_ctx;
114 };
115 
116 enum sec_endian {
117 	SEC_LE = 0,
118 	SEC_32BE,
119 	SEC_64BE
120 };
121 
122 enum sec_debug_file_index {
123 	SEC_CURRENT_QM,
124 	SEC_CLEAR_ENABLE,
125 	SEC_DEBUG_FILE_NUM,
126 };
127 
128 struct sec_debug_file {
129 	enum sec_debug_file_index index;
130 	spinlock_t lock;
131 	struct hisi_qm *qm;
132 };
133 
134 struct sec_dfx {
135 	u64 send_cnt;
136 	u64 recv_cnt;
137 };
138 
139 struct sec_debug {
140 	struct sec_dfx dfx;
141 	struct sec_debug_file files[SEC_DEBUG_FILE_NUM];
142 };
143 
144 struct sec_dev {
145 	struct hisi_qm qm;
146 	struct list_head list;
147 	struct sec_debug debug;
148 	u32 ctx_q_num;
149 	u32 num_vfs;
150 	unsigned long status;
151 };
152 
153 struct sec_dev *sec_find_device(int node);
154 int sec_register_to_crypto(void);
155 void sec_unregister_from_crypto(void);
156 #endif
157