18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/* Common header for Virtio crypto device.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright 2016 HUAWEI TECHNOLOGIES CO., LTD.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#ifndef _VIRTIO_CRYPTO_COMMON_H
88c2ecf20Sopenharmony_ci#define _VIRTIO_CRYPTO_COMMON_H
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/virtio.h>
118c2ecf20Sopenharmony_ci#include <linux/crypto.h>
128c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
138c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
148c2ecf20Sopenharmony_ci#include <crypto/aead.h>
158c2ecf20Sopenharmony_ci#include <crypto/aes.h>
168c2ecf20Sopenharmony_ci#include <crypto/engine.h>
178c2ecf20Sopenharmony_ci#include <uapi/linux/virtio_crypto.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/* Internal representation of a data virtqueue */
218c2ecf20Sopenharmony_cistruct data_queue {
228c2ecf20Sopenharmony_ci	/* Virtqueue associated with this send _queue */
238c2ecf20Sopenharmony_ci	struct virtqueue *vq;
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci	/* To protect the vq operations for the dataq */
268c2ecf20Sopenharmony_ci	spinlock_t lock;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	/* Name of the tx queue: dataq.$index */
298c2ecf20Sopenharmony_ci	char name[32];
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	struct crypto_engine *engine;
328c2ecf20Sopenharmony_ci	struct tasklet_struct done_task;
338c2ecf20Sopenharmony_ci};
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistruct virtio_crypto {
368c2ecf20Sopenharmony_ci	struct virtio_device *vdev;
378c2ecf20Sopenharmony_ci	struct virtqueue *ctrl_vq;
388c2ecf20Sopenharmony_ci	struct data_queue *data_vq;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	/* To protect the vq operations for the controlq */
418c2ecf20Sopenharmony_ci	spinlock_t ctrl_lock;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	/* Maximum of data queues supported by the device */
448c2ecf20Sopenharmony_ci	u32 max_data_queues;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	/* Number of queue currently used by the driver */
478c2ecf20Sopenharmony_ci	u32 curr_queue;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	/*
508c2ecf20Sopenharmony_ci	 * Specifies the services mask which the device support,
518c2ecf20Sopenharmony_ci	 * see VIRTIO_CRYPTO_SERVICE_*
528c2ecf20Sopenharmony_ci	 */
538c2ecf20Sopenharmony_ci	u32 crypto_services;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	/* Detailed algorithms mask */
568c2ecf20Sopenharmony_ci	u32 cipher_algo_l;
578c2ecf20Sopenharmony_ci	u32 cipher_algo_h;
588c2ecf20Sopenharmony_ci	u32 hash_algo;
598c2ecf20Sopenharmony_ci	u32 mac_algo_l;
608c2ecf20Sopenharmony_ci	u32 mac_algo_h;
618c2ecf20Sopenharmony_ci	u32 aead_algo;
628c2ecf20Sopenharmony_ci	u32 akcipher_algo;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	/* Maximum length of cipher key */
658c2ecf20Sopenharmony_ci	u32 max_cipher_key_len;
668c2ecf20Sopenharmony_ci	/* Maximum length of authenticated key */
678c2ecf20Sopenharmony_ci	u32 max_auth_key_len;
688c2ecf20Sopenharmony_ci	/* Maximum size of per request */
698c2ecf20Sopenharmony_ci	u64 max_size;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	unsigned long status;
728c2ecf20Sopenharmony_ci	atomic_t ref_count;
738c2ecf20Sopenharmony_ci	struct list_head list;
748c2ecf20Sopenharmony_ci	struct module *owner;
758c2ecf20Sopenharmony_ci	uint8_t dev_id;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	/* Does the affinity hint is set for virtqueues? */
788c2ecf20Sopenharmony_ci	bool affinity_hint_set;
798c2ecf20Sopenharmony_ci};
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistruct virtio_crypto_sym_session_info {
828c2ecf20Sopenharmony_ci	/* Backend session id, which come from the host side */
838c2ecf20Sopenharmony_ci	__u64 session_id;
848c2ecf20Sopenharmony_ci};
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci/*
878c2ecf20Sopenharmony_ci * Note: there are padding fields in request, clear them to zero before
888c2ecf20Sopenharmony_ci *       sending to host to avoid to divulge any information.
898c2ecf20Sopenharmony_ci * Ex, virtio_crypto_ctrl_request::ctrl::u::destroy_session::padding[48]
908c2ecf20Sopenharmony_ci */
918c2ecf20Sopenharmony_cistruct virtio_crypto_ctrl_request {
928c2ecf20Sopenharmony_ci	struct virtio_crypto_op_ctrl_req ctrl;
938c2ecf20Sopenharmony_ci	struct virtio_crypto_session_input input;
948c2ecf20Sopenharmony_ci	struct virtio_crypto_inhdr ctrl_status;
958c2ecf20Sopenharmony_ci	struct completion compl;
968c2ecf20Sopenharmony_ci};
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistruct virtio_crypto_request;
998c2ecf20Sopenharmony_citypedef void (*virtio_crypto_data_callback)
1008c2ecf20Sopenharmony_ci		(struct virtio_crypto_request *vc_req, int len);
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistruct virtio_crypto_request {
1038c2ecf20Sopenharmony_ci	uint8_t status;
1048c2ecf20Sopenharmony_ci	struct virtio_crypto_op_data_req *req_data;
1058c2ecf20Sopenharmony_ci	struct scatterlist **sgs;
1068c2ecf20Sopenharmony_ci	struct data_queue *dataq;
1078c2ecf20Sopenharmony_ci	virtio_crypto_data_callback alg_cb;
1088c2ecf20Sopenharmony_ci};
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ciint virtcrypto_devmgr_add_dev(struct virtio_crypto *vcrypto_dev);
1118c2ecf20Sopenharmony_cistruct list_head *virtcrypto_devmgr_get_head(void);
1128c2ecf20Sopenharmony_civoid virtcrypto_devmgr_rm_dev(struct virtio_crypto *vcrypto_dev);
1138c2ecf20Sopenharmony_cistruct virtio_crypto *virtcrypto_devmgr_get_first(void);
1148c2ecf20Sopenharmony_ciint virtcrypto_dev_in_use(struct virtio_crypto *vcrypto_dev);
1158c2ecf20Sopenharmony_ciint virtcrypto_dev_get(struct virtio_crypto *vcrypto_dev);
1168c2ecf20Sopenharmony_civoid virtcrypto_dev_put(struct virtio_crypto *vcrypto_dev);
1178c2ecf20Sopenharmony_ciint virtcrypto_dev_started(struct virtio_crypto *vcrypto_dev);
1188c2ecf20Sopenharmony_cibool virtcrypto_algo_is_supported(struct virtio_crypto *vcrypto_dev,
1198c2ecf20Sopenharmony_ci				  uint32_t service,
1208c2ecf20Sopenharmony_ci				  uint32_t algo);
1218c2ecf20Sopenharmony_cistruct virtio_crypto *virtcrypto_get_dev_node(int node,
1228c2ecf20Sopenharmony_ci					      uint32_t service,
1238c2ecf20Sopenharmony_ci					      uint32_t algo);
1248c2ecf20Sopenharmony_ciint virtcrypto_dev_start(struct virtio_crypto *vcrypto);
1258c2ecf20Sopenharmony_civoid virtcrypto_dev_stop(struct virtio_crypto *vcrypto);
1268c2ecf20Sopenharmony_ciint virtio_crypto_skcipher_crypt_req(
1278c2ecf20Sopenharmony_ci	struct crypto_engine *engine, void *vreq);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_civoid
1308c2ecf20Sopenharmony_civirtcrypto_clear_request(struct virtio_crypto_request *vc_req);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cistatic inline int virtio_crypto_get_current_node(void)
1338c2ecf20Sopenharmony_ci{
1348c2ecf20Sopenharmony_ci	int cpu, node;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	cpu = get_cpu();
1378c2ecf20Sopenharmony_ci	node = topology_physical_package_id(cpu);
1388c2ecf20Sopenharmony_ci	put_cpu();
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	return node;
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ciint virtio_crypto_algs_register(struct virtio_crypto *vcrypto);
1448c2ecf20Sopenharmony_civoid virtio_crypto_algs_unregister(struct virtio_crypto *vcrypto);
1458c2ecf20Sopenharmony_ciint virtio_crypto_akcipher_algs_register(struct virtio_crypto *vcrypto);
1468c2ecf20Sopenharmony_civoid virtio_crypto_akcipher_algs_unregister(struct virtio_crypto *vcrypto);
1478c2ecf20Sopenharmony_ciint virtio_crypto_ctrl_vq_request(struct virtio_crypto *vcrypto, struct scatterlist *sgs[],
1488c2ecf20Sopenharmony_ci				  unsigned int out_sgs, unsigned int in_sgs,
1498c2ecf20Sopenharmony_ci				  struct virtio_crypto_ctrl_request *vc_ctrl_req);
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci#endif /* _VIRTIO_CRYPTO_COMMON_H */
152