virtio_crypto_common.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Common header for Virtio crypto device.
  2. *
  3. * Copyright 2016 HUAWEI TECHNOLOGIES CO., LTD.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef _VIRTIO_CRYPTO_COMMON_H
  19. #define _VIRTIO_CRYPTO_COMMON_H
  20. #include <linux/virtio.h>
  21. #include <linux/crypto.h>
  22. #include <linux/spinlock.h>
  23. #include <crypto/aead.h>
  24. #include <crypto/aes.h>
  25. #include <crypto/authenc.h>
  26. #include <crypto/engine.h>
  27. /* Internal representation of a data virtqueue */
  28. struct data_queue {
  29. /* Virtqueue associated with this send _queue */
  30. struct virtqueue *vq;
  31. /* To protect the vq operations for the dataq */
  32. spinlock_t lock;
  33. /* Name of the tx queue: dataq.$index */
  34. char name[32];
  35. struct crypto_engine *engine;
  36. };
  37. struct virtio_crypto {
  38. struct virtio_device *vdev;
  39. struct virtqueue *ctrl_vq;
  40. struct data_queue *data_vq;
  41. /* To protect the vq operations for the controlq */
  42. spinlock_t ctrl_lock;
  43. /* Maximum of data queues supported by the device */
  44. u32 max_data_queues;
  45. /* Number of queue currently used by the driver */
  46. u32 curr_queue;
  47. /* Maximum length of cipher key */
  48. u32 max_cipher_key_len;
  49. /* Maximum length of authenticated key */
  50. u32 max_auth_key_len;
  51. /* Maximum size of per request */
  52. u64 max_size;
  53. /* Control VQ buffers: protected by the ctrl_lock */
  54. struct virtio_crypto_op_ctrl_req ctrl;
  55. struct virtio_crypto_session_input input;
  56. struct virtio_crypto_inhdr ctrl_status;
  57. unsigned long status;
  58. atomic_t ref_count;
  59. struct list_head list;
  60. struct module *owner;
  61. uint8_t dev_id;
  62. /* Does the affinity hint is set for virtqueues? */
  63. bool affinity_hint_set;
  64. };
  65. struct virtio_crypto_sym_session_info {
  66. /* Backend session id, which come from the host side */
  67. __u64 session_id;
  68. };
  69. struct virtio_crypto_ablkcipher_ctx {
  70. struct virtio_crypto *vcrypto;
  71. struct crypto_tfm *tfm;
  72. struct virtio_crypto_sym_session_info enc_sess_info;
  73. struct virtio_crypto_sym_session_info dec_sess_info;
  74. };
  75. struct virtio_crypto_request {
  76. /* Cipher or aead */
  77. uint32_t type;
  78. uint8_t status;
  79. struct virtio_crypto_ablkcipher_ctx *ablkcipher_ctx;
  80. struct ablkcipher_request *ablkcipher_req;
  81. struct virtio_crypto_op_data_req *req_data;
  82. struct scatterlist **sgs;
  83. uint8_t *iv;
  84. /* Encryption? */
  85. bool encrypt;
  86. struct data_queue *dataq;
  87. };
  88. int virtcrypto_devmgr_add_dev(struct virtio_crypto *vcrypto_dev);
  89. struct list_head *virtcrypto_devmgr_get_head(void);
  90. void virtcrypto_devmgr_rm_dev(struct virtio_crypto *vcrypto_dev);
  91. struct virtio_crypto *virtcrypto_devmgr_get_first(void);
  92. int virtcrypto_dev_in_use(struct virtio_crypto *vcrypto_dev);
  93. int virtcrypto_dev_get(struct virtio_crypto *vcrypto_dev);
  94. void virtcrypto_dev_put(struct virtio_crypto *vcrypto_dev);
  95. int virtcrypto_dev_started(struct virtio_crypto *vcrypto_dev);
  96. struct virtio_crypto *virtcrypto_get_dev_node(int node);
  97. int virtcrypto_dev_start(struct virtio_crypto *vcrypto);
  98. void virtcrypto_dev_stop(struct virtio_crypto *vcrypto);
  99. int virtio_crypto_ablkcipher_crypt_req(
  100. struct crypto_engine *engine,
  101. struct ablkcipher_request *req);
  102. void virtio_crypto_ablkcipher_finalize_req(
  103. struct virtio_crypto_request *vc_req,
  104. struct ablkcipher_request *req,
  105. int err);
  106. void
  107. virtcrypto_clear_request(struct virtio_crypto_request *vc_req);
  108. static inline int virtio_crypto_get_current_node(void)
  109. {
  110. int cpu, node;
  111. cpu = get_cpu();
  112. node = topology_physical_package_id(cpu);
  113. put_cpu();
  114. return node;
  115. }
  116. int virtio_crypto_algs_register(void);
  117. void virtio_crypto_algs_unregister(void);
  118. #endif /* _VIRTIO_CRYPTO_COMMON_H */