Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /* Copyright (c) 2019 HiSilicon Limited. */
0003 
0004 #ifndef __HISI_SEC_V2_H
0005 #define __HISI_SEC_V2_H
0006 
0007 #include <linux/hisi_acc_qm.h>
0008 #include "sec_crypto.h"
0009 
0010 /* Algorithm resource per hardware SEC queue */
0011 struct sec_alg_res {
0012     u8 *pbuf;
0013     dma_addr_t pbuf_dma;
0014     u8 *c_ivin;
0015     dma_addr_t c_ivin_dma;
0016     u8 *a_ivin;
0017     dma_addr_t a_ivin_dma;
0018     u8 *out_mac;
0019     dma_addr_t out_mac_dma;
0020 };
0021 
0022 /* Cipher request of SEC private */
0023 struct sec_cipher_req {
0024     struct hisi_acc_hw_sgl *c_out;
0025     dma_addr_t c_out_dma;
0026     u8 *c_ivin;
0027     dma_addr_t c_ivin_dma;
0028     struct skcipher_request *sk_req;
0029     u32 c_len;
0030     bool encrypt;
0031 };
0032 
0033 struct sec_aead_req {
0034     u8 *out_mac;
0035     dma_addr_t out_mac_dma;
0036     u8 *a_ivin;
0037     dma_addr_t a_ivin_dma;
0038     struct aead_request *aead_req;
0039 };
0040 
0041 /* SEC request of Crypto */
0042 struct sec_req {
0043     union {
0044         struct sec_sqe sec_sqe;
0045         struct sec_sqe3 sec_sqe3;
0046     };
0047     struct sec_ctx *ctx;
0048     struct sec_qp_ctx *qp_ctx;
0049 
0050     /**
0051      * Common parameter of the SEC request.
0052      */
0053     struct hisi_acc_hw_sgl *in;
0054     dma_addr_t in_dma;
0055     struct sec_cipher_req c_req;
0056     struct sec_aead_req aead_req;
0057     struct list_head backlog_head;
0058 
0059     int err_type;
0060     int req_id;
0061     u32 flag;
0062 
0063     /* Status of the SEC request */
0064     bool fake_busy;
0065     bool use_pbuf;
0066 };
0067 
0068 /**
0069  * struct sec_req_op - Operations for SEC request
0070  * @buf_map: DMA map the SGL buffers of the request
0071  * @buf_unmap: DMA unmap the SGL buffers of the request
0072  * @bd_fill: Fill the SEC queue BD
0073  * @bd_send: Send the SEC BD into the hardware queue
0074  * @callback: Call back for the request
0075  * @process: Main processing logic of Skcipher
0076  */
0077 struct sec_req_op {
0078     int (*buf_map)(struct sec_ctx *ctx, struct sec_req *req);
0079     void (*buf_unmap)(struct sec_ctx *ctx, struct sec_req *req);
0080     void (*do_transfer)(struct sec_ctx *ctx, struct sec_req *req);
0081     int (*bd_fill)(struct sec_ctx *ctx, struct sec_req *req);
0082     int (*bd_send)(struct sec_ctx *ctx, struct sec_req *req);
0083     void (*callback)(struct sec_ctx *ctx, struct sec_req *req, int err);
0084     int (*process)(struct sec_ctx *ctx, struct sec_req *req);
0085 };
0086 
0087 /* SEC auth context */
0088 struct sec_auth_ctx {
0089     dma_addr_t a_key_dma;
0090     u8 *a_key;
0091     u8 a_key_len;
0092     u8 mac_len;
0093     u8 a_alg;
0094     bool fallback;
0095     struct crypto_shash *hash_tfm;
0096     struct crypto_aead *fallback_aead_tfm;
0097 };
0098 
0099 /* SEC cipher context which cipher's relatives */
0100 struct sec_cipher_ctx {
0101     u8 *c_key;
0102     dma_addr_t c_key_dma;
0103     sector_t iv_offset;
0104     u32 c_gran_size;
0105     u32 ivsize;
0106     u8 c_mode;
0107     u8 c_alg;
0108     u8 c_key_len;
0109 
0110     /* add software support */
0111     bool fallback;
0112     struct crypto_sync_skcipher *fbtfm;
0113 };
0114 
0115 /* SEC queue context which defines queue's relatives */
0116 struct sec_qp_ctx {
0117     struct hisi_qp *qp;
0118     struct sec_req *req_list[QM_Q_DEPTH];
0119     struct idr req_idr;
0120     struct sec_alg_res res[QM_Q_DEPTH];
0121     struct sec_ctx *ctx;
0122     spinlock_t req_lock;
0123     struct list_head backlog;
0124     struct hisi_acc_sgl_pool *c_in_pool;
0125     struct hisi_acc_sgl_pool *c_out_pool;
0126 };
0127 
0128 enum sec_alg_type {
0129     SEC_SKCIPHER,
0130     SEC_AEAD
0131 };
0132 
0133 /* SEC Crypto TFM context which defines queue and cipher .etc relatives */
0134 struct sec_ctx {
0135     struct sec_qp_ctx *qp_ctx;
0136     struct sec_dev *sec;
0137     const struct sec_req_op *req_op;
0138     struct hisi_qp **qps;
0139 
0140     /* Half queues for encipher, and half for decipher */
0141     u32 hlf_q_num;
0142 
0143     /* Threshold for fake busy, trigger to return -EBUSY to user */
0144     u32 fake_req_limit;
0145 
0146     /* Current cyclic index to select a queue for encipher */
0147     atomic_t enc_qcyclic;
0148 
0149      /* Current cyclic index to select a queue for decipher */
0150     atomic_t dec_qcyclic;
0151 
0152     enum sec_alg_type alg_type;
0153     bool pbuf_supported;
0154     struct sec_cipher_ctx c_ctx;
0155     struct sec_auth_ctx a_ctx;
0156     u8 type_supported;
0157     struct device *dev;
0158 };
0159 
0160 
0161 enum sec_debug_file_index {
0162     SEC_CLEAR_ENABLE,
0163     SEC_DEBUG_FILE_NUM,
0164 };
0165 
0166 struct sec_debug_file {
0167     enum sec_debug_file_index index;
0168     spinlock_t lock;
0169     struct hisi_qm *qm;
0170 };
0171 
0172 struct sec_dfx {
0173     atomic64_t send_cnt;
0174     atomic64_t recv_cnt;
0175     atomic64_t send_busy_cnt;
0176     atomic64_t recv_busy_cnt;
0177     atomic64_t err_bd_cnt;
0178     atomic64_t invalid_req_cnt;
0179     atomic64_t done_flag_cnt;
0180 };
0181 
0182 struct sec_debug {
0183     struct sec_dfx dfx;
0184     struct sec_debug_file files[SEC_DEBUG_FILE_NUM];
0185 };
0186 
0187 struct sec_dev {
0188     struct hisi_qm qm;
0189     struct sec_debug debug;
0190     u32 ctx_q_num;
0191     bool iommu_used;
0192 };
0193 
0194 void sec_destroy_qps(struct hisi_qp **qps, int qp_num);
0195 struct hisi_qp **sec_create_qps(void);
0196 int sec_register_to_crypto(struct hisi_qm *qm);
0197 void sec_unregister_from_crypto(struct hisi_qm *qm);
0198 #endif