virtio_crypto_mgr.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Management for virtio crypto devices (refer to adf_dev_mgr.c)
  3. *
  4. * Copyright 2016 HUAWEI TECHNOLOGIES CO., LTD.
  5. */
  6. #include <linux/mutex.h>
  7. #include <linux/list.h>
  8. #include <linux/module.h>
  9. #include <uapi/linux/virtio_crypto.h>
  10. #include "virtio_crypto_common.h"
  11. static LIST_HEAD(virtio_crypto_table);
  12. static uint32_t num_devices;
  13. /* The table_lock protects the above global list and num_devices */
  14. static DEFINE_MUTEX(table_lock);
  15. #define VIRTIO_CRYPTO_MAX_DEVICES 32
  16. /*
  17. * virtcrypto_devmgr_add_dev() - Add vcrypto_dev to the acceleration
  18. * framework.
  19. * @vcrypto_dev: Pointer to virtio crypto device.
  20. *
  21. * Function adds virtio crypto device to the global list.
  22. * To be used by virtio crypto device specific drivers.
  23. *
  24. * Return: 0 on success, error code othewise.
  25. */
  26. int virtcrypto_devmgr_add_dev(struct virtio_crypto *vcrypto_dev)
  27. {
  28. struct list_head *itr;
  29. mutex_lock(&table_lock);
  30. if (num_devices == VIRTIO_CRYPTO_MAX_DEVICES) {
  31. pr_info("virtio_crypto: only support up to %d devices\n",
  32. VIRTIO_CRYPTO_MAX_DEVICES);
  33. mutex_unlock(&table_lock);
  34. return -EFAULT;
  35. }
  36. list_for_each(itr, &virtio_crypto_table) {
  37. struct virtio_crypto *ptr =
  38. list_entry(itr, struct virtio_crypto, list);
  39. if (ptr == vcrypto_dev) {
  40. mutex_unlock(&table_lock);
  41. return -EEXIST;
  42. }
  43. }
  44. atomic_set(&vcrypto_dev->ref_count, 0);
  45. list_add_tail(&vcrypto_dev->list, &virtio_crypto_table);
  46. vcrypto_dev->dev_id = num_devices++;
  47. mutex_unlock(&table_lock);
  48. return 0;
  49. }
  50. struct list_head *virtcrypto_devmgr_get_head(void)
  51. {
  52. return &virtio_crypto_table;
  53. }
  54. /*
  55. * virtcrypto_devmgr_rm_dev() - Remove vcrypto_dev from the acceleration
  56. * framework.
  57. * @vcrypto_dev: Pointer to virtio crypto device.
  58. *
  59. * Function removes virtio crypto device from the acceleration framework.
  60. * To be used by virtio crypto device specific drivers.
  61. *
  62. * Return: void
  63. */
  64. void virtcrypto_devmgr_rm_dev(struct virtio_crypto *vcrypto_dev)
  65. {
  66. mutex_lock(&table_lock);
  67. list_del(&vcrypto_dev->list);
  68. num_devices--;
  69. mutex_unlock(&table_lock);
  70. }
  71. /*
  72. * virtcrypto_dev_get() - Increment vcrypto_dev reference count
  73. * @vcrypto_dev: Pointer to virtio crypto device.
  74. *
  75. * Increment the vcrypto_dev refcount and if this is the first time
  76. * incrementing it during this period the vcrypto_dev is in use,
  77. * increment the module refcount too.
  78. * To be used by virtio crypto device specific drivers.
  79. *
  80. * Return: 0 when successful, EFAULT when fail to bump module refcount
  81. */
  82. int virtcrypto_dev_get(struct virtio_crypto *vcrypto_dev)
  83. {
  84. if (atomic_add_return(1, &vcrypto_dev->ref_count) == 1)
  85. if (!try_module_get(vcrypto_dev->owner))
  86. return -EFAULT;
  87. return 0;
  88. }
  89. /*
  90. * virtcrypto_dev_put() - Decrement vcrypto_dev reference count
  91. * @vcrypto_dev: Pointer to virtio crypto device.
  92. *
  93. * Decrement the vcrypto_dev refcount and if this is the last time
  94. * decrementing it during this period the vcrypto_dev is in use,
  95. * decrement the module refcount too.
  96. * To be used by virtio crypto device specific drivers.
  97. *
  98. * Return: void
  99. */
  100. void virtcrypto_dev_put(struct virtio_crypto *vcrypto_dev)
  101. {
  102. if (atomic_sub_return(1, &vcrypto_dev->ref_count) == 0)
  103. module_put(vcrypto_dev->owner);
  104. }
  105. /*
  106. * virtcrypto_dev_started() - Check whether device has started
  107. * @vcrypto_dev: Pointer to virtio crypto device.
  108. *
  109. * To be used by virtio crypto device specific drivers.
  110. *
  111. * Return: 1 when the device has started, 0 otherwise
  112. */
  113. int virtcrypto_dev_started(struct virtio_crypto *vcrypto_dev)
  114. {
  115. return (vcrypto_dev->status & VIRTIO_CRYPTO_S_HW_READY);
  116. }
  117. /*
  118. * virtcrypto_get_dev_node() - Get vcrypto_dev on the node.
  119. * @node: Node id the driver works.
  120. * @service: Crypto service that needs to be supported by the
  121. * dev
  122. * @algo: The algorithm number that needs to be supported by the
  123. * dev
  124. *
  125. * Function returns the virtio crypto device used fewest on the node,
  126. * and supports the given crypto service and algorithm.
  127. *
  128. * To be used by virtio crypto device specific drivers.
  129. *
  130. * Return: pointer to vcrypto_dev or NULL if not found.
  131. */
  132. struct virtio_crypto *virtcrypto_get_dev_node(int node, uint32_t service,
  133. uint32_t algo)
  134. {
  135. struct virtio_crypto *vcrypto_dev = NULL, *tmp_dev;
  136. unsigned long best = ~0;
  137. unsigned long ctr;
  138. mutex_lock(&table_lock);
  139. list_for_each_entry(tmp_dev, virtcrypto_devmgr_get_head(), list) {
  140. if ((node == dev_to_node(&tmp_dev->vdev->dev) ||
  141. dev_to_node(&tmp_dev->vdev->dev) < 0) &&
  142. virtcrypto_dev_started(tmp_dev) &&
  143. virtcrypto_algo_is_supported(tmp_dev, service, algo)) {
  144. ctr = atomic_read(&tmp_dev->ref_count);
  145. if (best > ctr) {
  146. vcrypto_dev = tmp_dev;
  147. best = ctr;
  148. }
  149. }
  150. }
  151. if (!vcrypto_dev) {
  152. pr_info("virtio_crypto: Could not find a device on node %d\n",
  153. node);
  154. /* Get any started device */
  155. list_for_each_entry(tmp_dev,
  156. virtcrypto_devmgr_get_head(), list) {
  157. if (virtcrypto_dev_started(tmp_dev) &&
  158. virtcrypto_algo_is_supported(tmp_dev,
  159. service, algo)) {
  160. vcrypto_dev = tmp_dev;
  161. break;
  162. }
  163. }
  164. }
  165. mutex_unlock(&table_lock);
  166. if (!vcrypto_dev)
  167. return NULL;
  168. virtcrypto_dev_get(vcrypto_dev);
  169. return vcrypto_dev;
  170. }
  171. /*
  172. * virtcrypto_dev_start() - Start virtio crypto device
  173. * @vcrypto: Pointer to virtio crypto device.
  174. *
  175. * Function notifies all the registered services that the virtio crypto device
  176. * is ready to be used.
  177. * To be used by virtio crypto device specific drivers.
  178. *
  179. * Return: 0 on success, EFAULT when fail to register algorithms
  180. */
  181. int virtcrypto_dev_start(struct virtio_crypto *vcrypto)
  182. {
  183. if (virtio_crypto_skcipher_algs_register(vcrypto)) {
  184. pr_err("virtio_crypto: Failed to register crypto skcipher algs\n");
  185. return -EFAULT;
  186. }
  187. if (virtio_crypto_akcipher_algs_register(vcrypto)) {
  188. pr_err("virtio_crypto: Failed to register crypto akcipher algs\n");
  189. virtio_crypto_skcipher_algs_unregister(vcrypto);
  190. return -EFAULT;
  191. }
  192. return 0;
  193. }
  194. /*
  195. * virtcrypto_dev_stop() - Stop virtio crypto device
  196. * @vcrypto: Pointer to virtio crypto device.
  197. *
  198. * Function notifies all the registered services that the virtio crypto device
  199. * shall no longer be used.
  200. * To be used by virtio crypto device specific drivers.
  201. *
  202. * Return: void
  203. */
  204. void virtcrypto_dev_stop(struct virtio_crypto *vcrypto)
  205. {
  206. virtio_crypto_skcipher_algs_unregister(vcrypto);
  207. virtio_crypto_akcipher_algs_unregister(vcrypto);
  208. }
  209. /*
  210. * vcrypto_algo_is_supported()
  211. * @vcrypto: Pointer to virtio crypto device.
  212. * @service: The bit number for service validate.
  213. * See VIRTIO_CRYPTO_SERVICE_*
  214. * @algo : The bit number for the algorithm to validate.
  215. *
  216. *
  217. * Validate if the virtio crypto device supports a service and
  218. * algo.
  219. *
  220. * Return true if device supports a service and algo.
  221. */
  222. bool virtcrypto_algo_is_supported(struct virtio_crypto *vcrypto,
  223. uint32_t service,
  224. uint32_t algo)
  225. {
  226. uint32_t service_mask = 1u << service;
  227. uint32_t algo_mask = 0;
  228. bool low = true;
  229. if (algo > 31) {
  230. algo -= 32;
  231. low = false;
  232. }
  233. if (!(vcrypto->crypto_services & service_mask))
  234. return false;
  235. switch (service) {
  236. case VIRTIO_CRYPTO_SERVICE_CIPHER:
  237. if (low)
  238. algo_mask = vcrypto->cipher_algo_l;
  239. else
  240. algo_mask = vcrypto->cipher_algo_h;
  241. break;
  242. case VIRTIO_CRYPTO_SERVICE_HASH:
  243. algo_mask = vcrypto->hash_algo;
  244. break;
  245. case VIRTIO_CRYPTO_SERVICE_MAC:
  246. if (low)
  247. algo_mask = vcrypto->mac_algo_l;
  248. else
  249. algo_mask = vcrypto->mac_algo_h;
  250. break;
  251. case VIRTIO_CRYPTO_SERVICE_AEAD:
  252. algo_mask = vcrypto->aead_algo;
  253. break;
  254. case VIRTIO_CRYPTO_SERVICE_AKCIPHER:
  255. algo_mask = vcrypto->akcipher_algo;
  256. break;
  257. }
  258. if (!(algo_mask & (1u << algo)))
  259. return false;
  260. return true;
  261. }