Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /* Common header for Virtio crypto device.
0003  *
0004  * Copyright 2016 HUAWEI TECHNOLOGIES CO., LTD.
0005  */
0006 
0007 #ifndef _VIRTIO_CRYPTO_COMMON_H
0008 #define _VIRTIO_CRYPTO_COMMON_H
0009 
0010 #include <linux/virtio.h>
0011 #include <linux/crypto.h>
0012 #include <linux/spinlock.h>
0013 #include <crypto/aead.h>
0014 #include <crypto/aes.h>
0015 #include <crypto/engine.h>
0016 #include <uapi/linux/virtio_crypto.h>
0017 
0018 
0019 /* Internal representation of a data virtqueue */
0020 struct data_queue {
0021     /* Virtqueue associated with this send _queue */
0022     struct virtqueue *vq;
0023 
0024     /* To protect the vq operations for the dataq */
0025     spinlock_t lock;
0026 
0027     /* Name of the tx queue: dataq.$index */
0028     char name[32];
0029 
0030     struct crypto_engine *engine;
0031 };
0032 
0033 struct virtio_crypto {
0034     struct virtio_device *vdev;
0035     struct virtqueue *ctrl_vq;
0036     struct data_queue *data_vq;
0037 
0038     /* To protect the vq operations for the controlq */
0039     spinlock_t ctrl_lock;
0040 
0041     /* Maximum of data queues supported by the device */
0042     u32 max_data_queues;
0043 
0044     /* Number of queue currently used by the driver */
0045     u32 curr_queue;
0046 
0047     /*
0048      * Specifies the services mask which the device support,
0049      * see VIRTIO_CRYPTO_SERVICE_*
0050      */
0051     u32 crypto_services;
0052 
0053     /* Detailed algorithms mask */
0054     u32 cipher_algo_l;
0055     u32 cipher_algo_h;
0056     u32 hash_algo;
0057     u32 mac_algo_l;
0058     u32 mac_algo_h;
0059     u32 aead_algo;
0060     u32 akcipher_algo;
0061 
0062     /* Maximum length of cipher key */
0063     u32 max_cipher_key_len;
0064     /* Maximum length of authenticated key */
0065     u32 max_auth_key_len;
0066     /* Maximum size of per request */
0067     u64 max_size;
0068 
0069     unsigned long status;
0070     atomic_t ref_count;
0071     struct list_head list;
0072     struct module *owner;
0073     uint8_t dev_id;
0074 
0075     /* Does the affinity hint is set for virtqueues? */
0076     bool affinity_hint_set;
0077 };
0078 
0079 struct virtio_crypto_sym_session_info {
0080     /* Backend session id, which come from the host side */
0081     __u64 session_id;
0082 };
0083 
0084 /*
0085  * Note: there are padding fields in request, clear them to zero before
0086  *       sending to host to avoid to divulge any information.
0087  * Ex, virtio_crypto_ctrl_request::ctrl::u::destroy_session::padding[48]
0088  */
0089 struct virtio_crypto_ctrl_request {
0090     struct virtio_crypto_op_ctrl_req ctrl;
0091     struct virtio_crypto_session_input input;
0092     struct virtio_crypto_inhdr ctrl_status;
0093     struct completion compl;
0094 };
0095 
0096 struct virtio_crypto_request;
0097 typedef void (*virtio_crypto_data_callback)
0098         (struct virtio_crypto_request *vc_req, int len);
0099 
0100 struct virtio_crypto_request {
0101     uint8_t status;
0102     struct virtio_crypto_op_data_req *req_data;
0103     struct scatterlist **sgs;
0104     struct data_queue *dataq;
0105     virtio_crypto_data_callback alg_cb;
0106 };
0107 
0108 int virtcrypto_devmgr_add_dev(struct virtio_crypto *vcrypto_dev);
0109 struct list_head *virtcrypto_devmgr_get_head(void);
0110 void virtcrypto_devmgr_rm_dev(struct virtio_crypto *vcrypto_dev);
0111 struct virtio_crypto *virtcrypto_devmgr_get_first(void);
0112 int virtcrypto_dev_in_use(struct virtio_crypto *vcrypto_dev);
0113 int virtcrypto_dev_get(struct virtio_crypto *vcrypto_dev);
0114 void virtcrypto_dev_put(struct virtio_crypto *vcrypto_dev);
0115 int virtcrypto_dev_started(struct virtio_crypto *vcrypto_dev);
0116 bool virtcrypto_algo_is_supported(struct virtio_crypto *vcrypto_dev,
0117                   uint32_t service,
0118                   uint32_t algo);
0119 struct virtio_crypto *virtcrypto_get_dev_node(int node,
0120                           uint32_t service,
0121                           uint32_t algo);
0122 int virtcrypto_dev_start(struct virtio_crypto *vcrypto);
0123 void virtcrypto_dev_stop(struct virtio_crypto *vcrypto);
0124 int virtio_crypto_skcipher_crypt_req(
0125     struct crypto_engine *engine, void *vreq);
0126 
0127 void
0128 virtcrypto_clear_request(struct virtio_crypto_request *vc_req);
0129 
0130 static inline int virtio_crypto_get_current_node(void)
0131 {
0132     int cpu, node;
0133 
0134     cpu = get_cpu();
0135     node = topology_physical_package_id(cpu);
0136     put_cpu();
0137 
0138     return node;
0139 }
0140 
0141 int virtio_crypto_skcipher_algs_register(struct virtio_crypto *vcrypto);
0142 void virtio_crypto_skcipher_algs_unregister(struct virtio_crypto *vcrypto);
0143 int virtio_crypto_akcipher_algs_register(struct virtio_crypto *vcrypto);
0144 void virtio_crypto_akcipher_algs_unregister(struct virtio_crypto *vcrypto);
0145 int virtio_crypto_ctrl_vq_request(struct virtio_crypto *vcrypto, struct scatterlist *sgs[],
0146                   unsigned int out_sgs, unsigned int in_sgs,
0147                   struct virtio_crypto_ctrl_request *vc_ctrl_req);
0148 
0149 #endif /* _VIRTIO_CRYPTO_COMMON_H */