1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* Common header for Virtio crypto device. 3 * 4 * Copyright 2016 HUAWEI TECHNOLOGIES CO., LTD. 5 */ 6 7 #ifndef _VIRTIO_CRYPTO_COMMON_H 8 #define _VIRTIO_CRYPTO_COMMON_H 9 10 #include <linux/virtio.h> 11 #include <linux/crypto.h> 12 #include <linux/spinlock.h> 13 #include <linux/interrupt.h> 14 #include <linux/workqueue.h> 15 #include <crypto/aead.h> 16 #include <crypto/aes.h> 17 #include <crypto/engine.h> 18 #include <uapi/linux/virtio_crypto.h> 19 20 21 /* Internal representation of a data virtqueue */ 22 struct data_queue { 23 /* Virtqueue associated with this send _queue */ 24 struct virtqueue *vq; 25 26 /* To protect the vq operations for the dataq */ 27 spinlock_t lock; 28 29 /* Name of the tx queue: dataq.$index */ 30 char name[32]; 31 32 struct crypto_engine *engine; 33 struct work_struct done_work; 34 }; 35 36 struct virtio_crypto { 37 struct virtio_device *vdev; 38 struct virtqueue *ctrl_vq; 39 struct data_queue *data_vq; 40 41 /* Work struct for config space updates */ 42 struct work_struct config_work; 43 44 /* To protect the vq operations for the controlq */ 45 spinlock_t ctrl_lock; 46 47 /* Maximum of data queues supported by the device */ 48 u32 max_data_queues; 49 50 /* Number of queue currently used by the driver */ 51 u32 curr_queue; 52 53 /* 54 * Specifies the services mask which the device support, 55 * see VIRTIO_CRYPTO_SERVICE_* 56 */ 57 u32 crypto_services; 58 59 /* Detailed algorithms mask */ 60 u32 cipher_algo_l; 61 u32 cipher_algo_h; 62 u32 hash_algo; 63 u32 mac_algo_l; 64 u32 mac_algo_h; 65 u32 aead_algo; 66 u32 akcipher_algo; 67 68 /* Maximum length of cipher key */ 69 u32 max_cipher_key_len; 70 /* Maximum length of authenticated key */ 71 u32 max_auth_key_len; 72 /* Maximum size of per request */ 73 u64 max_size; 74 75 unsigned long status; 76 atomic_t ref_count; 77 struct list_head list; 78 struct module *owner; 79 uint8_t dev_id; 80 81 /* Does the affinity hint is set for virtqueues? */ 82 bool affinity_hint_set; 83 }; 84 85 struct virtio_crypto_sym_session_info { 86 /* Backend session id, which come from the host side */ 87 __u64 session_id; 88 }; 89 90 /* 91 * Note: there are padding fields in request, clear them to zero before 92 * sending to host to avoid to divulge any information. 93 * Ex, virtio_crypto_ctrl_request::ctrl::u::destroy_session::padding[48] 94 */ 95 struct virtio_crypto_ctrl_request { 96 struct virtio_crypto_op_ctrl_req ctrl; 97 struct virtio_crypto_session_input input; 98 struct virtio_crypto_inhdr ctrl_status; 99 struct completion compl; 100 }; 101 102 struct virtio_crypto_request; 103 typedef void (*virtio_crypto_data_callback) 104 (struct virtio_crypto_request *vc_req, int len); 105 106 struct virtio_crypto_request { 107 uint8_t status; 108 struct virtio_crypto_op_data_req *req_data; 109 struct scatterlist **sgs; 110 struct data_queue *dataq; 111 virtio_crypto_data_callback alg_cb; 112 }; 113 114 int virtcrypto_devmgr_add_dev(struct virtio_crypto *vcrypto_dev); 115 struct list_head *virtcrypto_devmgr_get_head(void); 116 void virtcrypto_devmgr_rm_dev(struct virtio_crypto *vcrypto_dev); 117 int virtcrypto_dev_get(struct virtio_crypto *vcrypto_dev); 118 void virtcrypto_dev_put(struct virtio_crypto *vcrypto_dev); 119 int virtcrypto_dev_started(struct virtio_crypto *vcrypto_dev); 120 bool virtcrypto_algo_is_supported(struct virtio_crypto *vcrypto_dev, 121 uint32_t service, 122 uint32_t algo); 123 struct virtio_crypto *virtcrypto_get_dev_node(int node, 124 uint32_t service, 125 uint32_t algo); 126 int virtcrypto_dev_start(struct virtio_crypto *vcrypto); 127 void virtcrypto_dev_stop(struct virtio_crypto *vcrypto); 128 int virtio_crypto_skcipher_crypt_req( 129 struct crypto_engine *engine, void *vreq); 130 131 void 132 virtcrypto_clear_request(struct virtio_crypto_request *vc_req); 133 134 static inline int virtio_crypto_get_current_node(void) 135 { 136 int cpu, node; 137 138 cpu = get_cpu(); 139 node = cpu_to_node(cpu); 140 put_cpu(); 141 142 return node; 143 } 144 145 int virtio_crypto_skcipher_algs_register(struct virtio_crypto *vcrypto); 146 void virtio_crypto_skcipher_algs_unregister(struct virtio_crypto *vcrypto); 147 int virtio_crypto_akcipher_algs_register(struct virtio_crypto *vcrypto); 148 void virtio_crypto_akcipher_algs_unregister(struct virtio_crypto *vcrypto); 149 int virtio_crypto_ctrl_vq_request(struct virtio_crypto *vcrypto, struct scatterlist *sgs[], 150 unsigned int out_sgs, unsigned int in_sgs, 151 struct virtio_crypto_ctrl_request *vc_ctrl_req); 152 153 #endif /* _VIRTIO_CRYPTO_COMMON_H */ 154