sec.h 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/* SPDX-License-Identifier: GPL-2.0 */
/* Copyright (c) 2019 HiSilicon Limited. */

#ifndef __HISI_SEC_V2_H
#define __HISI_SEC_V2_H

#include <linux/list.h>

#include "../qm.h"
#include "sec_crypto.h"

12 13
/* Algorithm resource per hardware SEC queue */
struct sec_alg_res {
14 15
	u8 *pbuf;
	dma_addr_t pbuf_dma;
16 17
	u8 *c_ivin;
	dma_addr_t c_ivin_dma;
18 19
	u8 *out_mac;
	dma_addr_t out_mac_dma;
20 21 22 23 24 25 26 27
};

/* Cipher request of SEC private */
struct sec_cipher_req {
	struct hisi_acc_hw_sgl *c_in;
	dma_addr_t c_in_dma;
	struct hisi_acc_hw_sgl *c_out;
	dma_addr_t c_out_dma;
28 29
	u8 *c_ivin;
	dma_addr_t c_ivin_dma;
30 31 32 33 34
	struct skcipher_request *sk_req;
	u32 c_len;
	bool encrypt;
};

35 36 37 38 39 40
struct sec_aead_req {
	u8 *out_mac;
	dma_addr_t out_mac_dma;
	struct aead_request *aead_req;
};

41 42 43 44 45 46 47
/* SEC request of Crypto */
struct sec_req {
	struct sec_sqe sec_sqe;
	struct sec_ctx *ctx;
	struct sec_qp_ctx *qp_ctx;

	struct sec_cipher_req c_req;
48
	struct sec_aead_req aead_req;
49
	struct list_head backlog_head;
50

51 52
	int err_type;
	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 62 63 64 65 66 67 68 69 70 71 72 73 74
};

/**
 * struct sec_req_op - Operations for SEC request
 * @buf_map: DMA map the SGL buffers of the request
 * @buf_unmap: DMA unmap the SGL buffers of the request
 * @bd_fill: Fill the SEC queue BD
 * @bd_send: Send the SEC BD into the hardware queue
 * @callback: Call back for the request
 * @process: Main processing logic of Skcipher
 */
struct sec_req_op {
	int (*buf_map)(struct sec_ctx *ctx, struct sec_req *req);
	void (*buf_unmap)(struct sec_ctx *ctx, struct sec_req *req);
	void (*do_transfer)(struct sec_ctx *ctx, struct sec_req *req);
	int (*bd_fill)(struct sec_ctx *ctx, struct sec_req *req);
	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 77 78
	int (*process)(struct sec_ctx *ctx, struct sec_req *req);
};

79 80 81 82 83 84 85 86 87 88
/* SEC auth context */
struct sec_auth_ctx {
	dma_addr_t a_key_dma;
	u8 *a_key;
	u8 a_key_len;
	u8 mac_len;
	u8 a_alg;
	struct crypto_shash *hash_tfm;
};

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
/* SEC cipher context which cipher's relatives */
struct sec_cipher_ctx {
	u8 *c_key;
	dma_addr_t c_key_dma;
	sector_t iv_offset;
	u32 c_gran_size;
	u32 ivsize;
	u8 c_mode;
	u8 c_alg;
	u8 c_key_len;
};

/* SEC queue context which defines queue's relatives */
struct sec_qp_ctx {
	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 108
	struct sec_ctx *ctx;
	struct mutex req_lock;
109
	struct list_head backlog;
110 111 112 113
	struct hisi_acc_sgl_pool *c_in_pool;
	struct hisi_acc_sgl_pool *c_out_pool;
};

114 115 116 117 118
enum sec_alg_type {
	SEC_SKCIPHER,
	SEC_AEAD
};

119 120 121 122 123
/* SEC Crypto TFM context which defines queue and cipher .etc relatives */
struct sec_ctx {
	struct sec_qp_ctx *qp_ctx;
	struct sec_dev *sec;
	const struct sec_req_op *req_op;
124
	struct hisi_qp **qps;
125 126 127 128 129 130 131 132 133 134 135 136

	/* Half queues for encipher, and half for decipher */
	u32 hlf_q_num;

	/* Threshold for fake busy, trigger to return -EBUSY to user */
	u32 fake_req_limit;

	/* Currrent cyclic index to select a queue for encipher */
	atomic_t enc_qcyclic;

	 /* Currrent cyclic index to select a queue for decipher */
	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 145 146 147 148 149
};

enum sec_endian {
	SEC_LE = 0,
	SEC_32BE,
	SEC_64BE
};

150 151 152 153 154 155 156 157 158 159 160 161 162
enum sec_debug_file_index {
	SEC_CURRENT_QM,
	SEC_CLEAR_ENABLE,
	SEC_DEBUG_FILE_NUM,
};

struct sec_debug_file {
	enum sec_debug_file_index index;
	spinlock_t lock;
	struct hisi_qm *qm;
};

struct sec_dfx {
163 164
	atomic64_t send_cnt;
	atomic64_t recv_cnt;
165
	atomic64_t send_busy_cnt;
166
	atomic64_t recv_busy_cnt;
167 168 169
	atomic64_t err_bd_cnt;
	atomic64_t invalid_req_cnt;
	atomic64_t done_flag_cnt;
170 171 172 173 174 175 176
};

struct sec_debug {
	struct sec_dfx dfx;
	struct sec_debug_file files[SEC_DEBUG_FILE_NUM];
};

177 178
struct sec_dev {
	struct hisi_qm qm;
179
	struct sec_debug debug;
180
	u32 ctx_q_num;
181
	bool iommu_used;
182 183
};

184 185
void sec_destroy_qps(struct hisi_qp **qps, int qp_num);
struct hisi_qp **sec_create_qps(void);
186 187 188
int sec_register_to_crypto(void);
void sec_unregister_from_crypto(void);
#endif