0001
0002
0003
0004 #ifndef _SEC_DRV_H_
0005 #define _SEC_DRV_H_
0006
0007 #include <crypto/algapi.h>
0008 #include <linux/kfifo.h>
0009
0010 #define SEC_MAX_SGE_NUM 64
0011 #define SEC_HW_RING_NUM 3
0012
0013 #define SEC_CMD_RING 0
0014 #define SEC_OUTORDER_RING 1
0015 #define SEC_DBG_RING 2
0016
0017
0018 #define SEC_QUEUE_LEN 512
0019
0020 #define SEC_MAX_SGE_NUM 64
0021
0022 struct sec_bd_info {
0023 #define SEC_BD_W0_T_LEN_M GENMASK(4, 0)
0024 #define SEC_BD_W0_T_LEN_S 0
0025
0026 #define SEC_BD_W0_C_WIDTH_M GENMASK(6, 5)
0027 #define SEC_BD_W0_C_WIDTH_S 5
0028 #define SEC_C_WIDTH_AES_128BIT 0
0029 #define SEC_C_WIDTH_AES_8BIT 1
0030 #define SEC_C_WIDTH_AES_1BIT 2
0031 #define SEC_C_WIDTH_DES_64BIT 0
0032 #define SEC_C_WIDTH_DES_8BIT 1
0033 #define SEC_C_WIDTH_DES_1BIT 2
0034
0035 #define SEC_BD_W0_C_MODE_M GENMASK(9, 7)
0036 #define SEC_BD_W0_C_MODE_S 7
0037 #define SEC_C_MODE_ECB 0
0038 #define SEC_C_MODE_CBC 1
0039 #define SEC_C_MODE_CTR 4
0040 #define SEC_C_MODE_CCM 5
0041 #define SEC_C_MODE_GCM 6
0042 #define SEC_C_MODE_XTS 7
0043
0044 #define SEC_BD_W0_SEQ BIT(10)
0045 #define SEC_BD_W0_DE BIT(11)
0046 #define SEC_BD_W0_DAT_SKIP_M GENMASK(13, 12)
0047 #define SEC_BD_W0_DAT_SKIP_S 12
0048 #define SEC_BD_W0_C_GRAN_SIZE_19_16_M GENMASK(17, 14)
0049 #define SEC_BD_W0_C_GRAN_SIZE_19_16_S 14
0050
0051 #define SEC_BD_W0_CIPHER_M GENMASK(19, 18)
0052 #define SEC_BD_W0_CIPHER_S 18
0053 #define SEC_CIPHER_NULL 0
0054 #define SEC_CIPHER_ENCRYPT 1
0055 #define SEC_CIPHER_DECRYPT 2
0056
0057 #define SEC_BD_W0_AUTH_M GENMASK(21, 20)
0058 #define SEC_BD_W0_AUTH_S 20
0059 #define SEC_AUTH_NULL 0
0060 #define SEC_AUTH_MAC 1
0061 #define SEC_AUTH_VERIF 2
0062
0063 #define SEC_BD_W0_AI_GEN BIT(22)
0064 #define SEC_BD_W0_CI_GEN BIT(23)
0065 #define SEC_BD_W0_NO_HPAD BIT(24)
0066 #define SEC_BD_W0_HM_M GENMASK(26, 25)
0067 #define SEC_BD_W0_HM_S 25
0068 #define SEC_BD_W0_ICV_OR_SKEY_EN_M GENMASK(28, 27)
0069 #define SEC_BD_W0_ICV_OR_SKEY_EN_S 27
0070
0071
0072 #define SEC_BD_W0_FLAG_M GENMASK(30, 29)
0073 #define SEC_BD_W0_C_GRAN_SIZE_21_20_M GENMASK(30, 29)
0074 #define SEC_BD_W0_FLAG_S 29
0075 #define SEC_BD_W0_C_GRAN_SIZE_21_20_S 29
0076
0077 #define SEC_BD_W0_DONE BIT(31)
0078 u32 w0;
0079
0080 #define SEC_BD_W1_AUTH_GRAN_SIZE_M GENMASK(21, 0)
0081 #define SEC_BD_W1_AUTH_GRAN_SIZE_S 0
0082 #define SEC_BD_W1_M_KEY_EN BIT(22)
0083 #define SEC_BD_W1_BD_INVALID BIT(23)
0084 #define SEC_BD_W1_ADDR_TYPE BIT(24)
0085
0086 #define SEC_BD_W1_A_ALG_M GENMASK(28, 25)
0087 #define SEC_BD_W1_A_ALG_S 25
0088 #define SEC_A_ALG_SHA1 0
0089 #define SEC_A_ALG_SHA256 1
0090 #define SEC_A_ALG_MD5 2
0091 #define SEC_A_ALG_SHA224 3
0092 #define SEC_A_ALG_HMAC_SHA1 8
0093 #define SEC_A_ALG_HMAC_SHA224 10
0094 #define SEC_A_ALG_HMAC_SHA256 11
0095 #define SEC_A_ALG_HMAC_MD5 12
0096 #define SEC_A_ALG_AES_XCBC 13
0097 #define SEC_A_ALG_AES_CMAC 14
0098
0099 #define SEC_BD_W1_C_ALG_M GENMASK(31, 29)
0100 #define SEC_BD_W1_C_ALG_S 29
0101 #define SEC_C_ALG_DES 0
0102 #define SEC_C_ALG_3DES 1
0103 #define SEC_C_ALG_AES 2
0104
0105 u32 w1;
0106
0107 #define SEC_BD_W2_C_GRAN_SIZE_15_0_M GENMASK(15, 0)
0108 #define SEC_BD_W2_C_GRAN_SIZE_15_0_S 0
0109 #define SEC_BD_W2_GRAN_NUM_M GENMASK(31, 16)
0110 #define SEC_BD_W2_GRAN_NUM_S 16
0111 u32 w2;
0112
0113 #define SEC_BD_W3_AUTH_LEN_OFFSET_M GENMASK(9, 0)
0114 #define SEC_BD_W3_AUTH_LEN_OFFSET_S 0
0115 #define SEC_BD_W3_CIPHER_LEN_OFFSET_M GENMASK(19, 10)
0116 #define SEC_BD_W3_CIPHER_LEN_OFFSET_S 10
0117 #define SEC_BD_W3_MAC_LEN_M GENMASK(24, 20)
0118 #define SEC_BD_W3_MAC_LEN_S 20
0119 #define SEC_BD_W3_A_KEY_LEN_M GENMASK(29, 25)
0120 #define SEC_BD_W3_A_KEY_LEN_S 25
0121 #define SEC_BD_W3_C_KEY_LEN_M GENMASK(31, 30)
0122 #define SEC_BD_W3_C_KEY_LEN_S 30
0123 #define SEC_KEY_LEN_AES_128 0
0124 #define SEC_KEY_LEN_AES_192 1
0125 #define SEC_KEY_LEN_AES_256 2
0126 #define SEC_KEY_LEN_DES 1
0127 #define SEC_KEY_LEN_3DES_3_KEY 1
0128 #define SEC_KEY_LEN_3DES_2_KEY 3
0129 u32 w3;
0130
0131
0132 union {
0133 u32 authkey_addr_lo;
0134 u32 authiv_addr_lo;
0135 };
0136 union {
0137 u32 authkey_addr_hi;
0138 u32 authiv_addr_hi;
0139 };
0140
0141
0142 u32 cipher_key_addr_lo;
0143 u32 cipher_key_addr_hi;
0144
0145
0146 u32 cipher_iv_addr_lo;
0147 u32 cipher_iv_addr_hi;
0148
0149
0150 u32 data_addr_lo;
0151 u32 data_addr_hi;
0152
0153
0154 u32 mac_addr_lo;
0155 u32 mac_addr_hi;
0156
0157
0158 u32 cipher_destin_addr_lo;
0159 u32 cipher_destin_addr_hi;
0160 };
0161
0162 enum sec_mem_region {
0163 SEC_COMMON = 0,
0164 SEC_SAA,
0165 SEC_NUM_ADDR_REGIONS
0166 };
0167
0168 #define SEC_NAME_SIZE 64
0169 #define SEC_Q_NUM 16
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180 struct sec_queue_ring_cmd {
0181 atomic_t used;
0182 struct mutex lock;
0183 struct sec_bd_info *vaddr;
0184 dma_addr_t paddr;
0185 void (*callback)(struct sec_bd_info *resp, void *ctx);
0186 };
0187
0188 struct sec_debug_bd_info;
0189 struct sec_queue_ring_db {
0190 struct sec_debug_bd_info *vaddr;
0191 dma_addr_t paddr;
0192 };
0193
0194 struct sec_out_bd_info;
0195 struct sec_queue_ring_cq {
0196 struct sec_out_bd_info *vaddr;
0197 dma_addr_t paddr;
0198 };
0199
0200 struct sec_dev_info;
0201
0202 enum sec_cipher_alg {
0203 SEC_C_DES_ECB_64,
0204 SEC_C_DES_CBC_64,
0205
0206 SEC_C_3DES_ECB_192_3KEY,
0207 SEC_C_3DES_ECB_192_2KEY,
0208
0209 SEC_C_3DES_CBC_192_3KEY,
0210 SEC_C_3DES_CBC_192_2KEY,
0211
0212 SEC_C_AES_ECB_128,
0213 SEC_C_AES_ECB_192,
0214 SEC_C_AES_ECB_256,
0215
0216 SEC_C_AES_CBC_128,
0217 SEC_C_AES_CBC_192,
0218 SEC_C_AES_CBC_256,
0219
0220 SEC_C_AES_CTR_128,
0221 SEC_C_AES_CTR_192,
0222 SEC_C_AES_CTR_256,
0223
0224 SEC_C_AES_XTS_128,
0225 SEC_C_AES_XTS_256,
0226
0227 SEC_C_NULL,
0228 };
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242 struct sec_alg_tfm_ctx {
0243 enum sec_cipher_alg cipher_alg;
0244 u8 *key;
0245 dma_addr_t pkey;
0246 struct sec_bd_info req_template;
0247 struct sec_queue *queue;
0248 struct mutex lock;
0249 u8 *auth_buf;
0250 struct list_head backlog;
0251 };
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274 struct sec_request {
0275 struct list_head elements;
0276 int num_elements;
0277 struct mutex lock;
0278 struct sec_alg_tfm_ctx *tfm_ctx;
0279 int len_in;
0280 int len_out;
0281 dma_addr_t dma_iv;
0282 int err;
0283 struct crypto_async_request *req_base;
0284 void (*cb)(struct sec_bd_info *resp, struct crypto_async_request *req);
0285 struct list_head backlog_head;
0286 };
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302 struct sec_request_el {
0303 struct list_head head;
0304 struct sec_bd_info req;
0305 struct sec_hw_sgl *in;
0306 dma_addr_t dma_in;
0307 struct scatterlist *sgl_in;
0308 struct sec_hw_sgl *out;
0309 dma_addr_t dma_out;
0310 struct scatterlist *sgl_out;
0311 struct sec_request *sec_req;
0312 size_t el_length;
0313 };
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335 struct sec_queue {
0336 struct sec_dev_info *dev_info;
0337 int task_irq;
0338 char name[SEC_NAME_SIZE];
0339 struct sec_queue_ring_cmd ring_cmd;
0340 struct sec_queue_ring_cq ring_cq;
0341 struct sec_queue_ring_db ring_db;
0342 void __iomem *regs;
0343 u32 queue_id;
0344 bool in_use;
0345 int expected;
0346
0347 DECLARE_BITMAP(unprocessed, SEC_QUEUE_LEN);
0348 DECLARE_KFIFO_PTR(softqueue, typeof(struct sec_request_el *));
0349 bool havesoftqueue;
0350 spinlock_t queuelock;
0351 void *shadow[SEC_QUEUE_LEN];
0352 };
0353
0354
0355
0356
0357
0358
0359
0360 struct sec_hw_sge {
0361 dma_addr_t buf;
0362 unsigned int len;
0363 unsigned int pad;
0364 };
0365
0366
0367
0368
0369
0370
0371
0372
0373
0374
0375
0376
0377
0378
0379
0380 struct sec_hw_sgl {
0381 dma_addr_t next_sgl;
0382 u16 entry_sum_in_chain;
0383 u16 entry_sum_in_sgl;
0384 u32 flag;
0385 u64 serial_num;
0386 u32 cpuid;
0387 u32 data_bytes_in_sgl;
0388 struct sec_hw_sgl *next;
0389 u64 reserved;
0390 struct sec_hw_sge sge_entries[SEC_MAX_SGE_NUM];
0391 u8 node[16];
0392 };
0393
0394 struct dma_pool;
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406 struct sec_dev_info {
0407 int sec_id;
0408 int num_saas;
0409 void __iomem *regs[SEC_NUM_ADDR_REGIONS];
0410 struct mutex dev_lock;
0411 int queues_in_use;
0412 struct sec_queue queues[SEC_Q_NUM];
0413 struct device *dev;
0414 struct dma_pool *hw_sgl_pool;
0415 };
0416
0417 int sec_queue_send(struct sec_queue *queue, struct sec_bd_info *msg, void *ctx);
0418 bool sec_queue_can_enqueue(struct sec_queue *queue, int num);
0419 int sec_queue_stop_release(struct sec_queue *queue);
0420 struct sec_queue *sec_queue_alloc_start_safe(void);
0421 bool sec_queue_empty(struct sec_queue *queue);
0422
0423
0424 void sec_alg_callback(struct sec_bd_info *resp, void *ctx);
0425 int sec_algs_register(void);
0426 void sec_algs_unregister(void);
0427
0428 #endif