virtio_crypto_mgr.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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_devmgr_get_first()
  73. *
  74. * Function returns the first virtio crypto device from the acceleration
  75. * framework.
  76. *
  77. * To be used by virtio crypto device specific drivers.
  78. *
  79. * Return: pointer to vcrypto_dev or NULL if not found.
  80. */
  81. struct virtio_crypto *virtcrypto_devmgr_get_first(void)
  82. {
  83. struct virtio_crypto *dev = NULL;
  84. mutex_lock(&table_lock);
  85. if (!list_empty(&virtio_crypto_table))
  86. dev = list_first_entry(&virtio_crypto_table,
  87. struct virtio_crypto,
  88. list);
  89. mutex_unlock(&table_lock);
  90. return dev;
  91. }
  92. /*
  93. * virtcrypto_dev_in_use() - Check whether vcrypto_dev is currently in use
  94. * @vcrypto_dev: Pointer to virtio crypto device.
  95. *
  96. * To be used by virtio crypto device specific drivers.
  97. *
  98. * Return: 1 when device is in use, 0 otherwise.
  99. */
  100. int virtcrypto_dev_in_use(struct virtio_crypto *vcrypto_dev)
  101. {
  102. return atomic_read(&vcrypto_dev->ref_count) != 0;
  103. }
  104. /*
  105. * virtcrypto_dev_get() - Increment vcrypto_dev reference count
  106. * @vcrypto_dev: Pointer to virtio crypto device.
  107. *
  108. * Increment the vcrypto_dev refcount and if this is the first time
  109. * incrementing it during this period the vcrypto_dev is in use,
  110. * increment the module refcount too.
  111. * To be used by virtio crypto device specific drivers.
  112. *
  113. * Return: 0 when successful, EFAULT when fail to bump module refcount
  114. */
  115. int virtcrypto_dev_get(struct virtio_crypto *vcrypto_dev)
  116. {
  117. if (atomic_add_return(1, &vcrypto_dev->ref_count) == 1)
  118. if (!try_module_get(vcrypto_dev->owner))
  119. return -EFAULT;
  120. return 0;
  121. }
  122. /*
  123. * virtcrypto_dev_put() - Decrement vcrypto_dev reference count
  124. * @vcrypto_dev: Pointer to virtio crypto device.
  125. *
  126. * Decrement the vcrypto_dev refcount and if this is the last time
  127. * decrementing it during this period the vcrypto_dev is in use,
  128. * decrement the module refcount too.
  129. * To be used by virtio crypto device specific drivers.
  130. *
  131. * Return: void
  132. */
  133. void virtcrypto_dev_put(struct virtio_crypto *vcrypto_dev)
  134. {
  135. if (atomic_sub_return(1, &vcrypto_dev->ref_count) == 0)
  136. module_put(vcrypto_dev->owner);
  137. }
  138. /*
  139. * virtcrypto_dev_started() - Check whether device has started
  140. * @vcrypto_dev: Pointer to virtio crypto device.
  141. *
  142. * To be used by virtio crypto device specific drivers.
  143. *
  144. * Return: 1 when the device has started, 0 otherwise
  145. */
  146. int virtcrypto_dev_started(struct virtio_crypto *vcrypto_dev)
  147. {
  148. return (vcrypto_dev->status & VIRTIO_CRYPTO_S_HW_READY);
  149. }
  150. /*
  151. * virtcrypto_get_dev_node() - Get vcrypto_dev on the node.
  152. * @node: Node id the driver works.
  153. * @service: Crypto service that needs to be supported by the
  154. * dev
  155. * @algo: The algorithm number that needs to be supported by the
  156. * dev
  157. *
  158. * Function returns the virtio crypto device used fewest on the node,
  159. * and supports the given crypto service and algorithm.
  160. *
  161. * To be used by virtio crypto device specific drivers.
  162. *
  163. * Return: pointer to vcrypto_dev or NULL if not found.
  164. */
  165. struct virtio_crypto *virtcrypto_get_dev_node(int node, uint32_t service,
  166. uint32_t algo)
  167. {
  168. struct virtio_crypto *vcrypto_dev = NULL, *tmp_dev;
  169. unsigned long best = ~0;
  170. unsigned long ctr;
  171. mutex_lock(&table_lock);
  172. list_for_each_entry(tmp_dev, virtcrypto_devmgr_get_head(), list) {
  173. if ((node == dev_to_node(&tmp_dev->vdev->dev) ||
  174. dev_to_node(&tmp_dev->vdev->dev) < 0) &&
  175. virtcrypto_dev_started(tmp_dev) &&
  176. virtcrypto_algo_is_supported(tmp_dev, service, algo)) {
  177. ctr = atomic_read(&tmp_dev->ref_count);
  178. if (best > ctr) {
  179. vcrypto_dev = tmp_dev;
  180. best = ctr;
  181. }
  182. }
  183. }
  184. if (!vcrypto_dev) {
  185. pr_info("virtio_crypto: Could not find a device on node %d\n",
  186. node);
  187. /* Get any started device */
  188. list_for_each_entry(tmp_dev,
  189. virtcrypto_devmgr_get_head(), list) {
  190. if (virtcrypto_dev_started(tmp_dev) &&
  191. virtcrypto_algo_is_supported(tmp_dev,
  192. service, algo)) {
  193. vcrypto_dev = tmp_dev;
  194. break;
  195. }
  196. }
  197. }
  198. mutex_unlock(&table_lock);
  199. if (!vcrypto_dev)
  200. return NULL;
  201. virtcrypto_dev_get(vcrypto_dev);
  202. return vcrypto_dev;
  203. }
  204. /*
  205. * virtcrypto_dev_start() - Start virtio crypto device
  206. * @vcrypto: Pointer to virtio crypto device.
  207. *
  208. * Function notifies all the registered services that the virtio crypto device
  209. * is ready to be used.
  210. * To be used by virtio crypto device specific drivers.
  211. *
  212. * Return: 0 on success, EFAULT when fail to register algorithms
  213. */
  214. int virtcrypto_dev_start(struct virtio_crypto *vcrypto)
  215. {
  216. if (virtio_crypto_algs_register(vcrypto)) {
  217. pr_err("virtio_crypto: Failed to register crypto algs\n");
  218. return -EFAULT;
  219. }
  220. return 0;
  221. }
  222. /*
  223. * virtcrypto_dev_stop() - Stop virtio crypto device
  224. * @vcrypto: Pointer to virtio crypto device.
  225. *
  226. * Function notifies all the registered services that the virtio crypto device
  227. * is ready to be used.
  228. * To be used by virtio crypto device specific drivers.
  229. *
  230. * Return: void
  231. */
  232. void virtcrypto_dev_stop(struct virtio_crypto *vcrypto)
  233. {
  234. virtio_crypto_algs_unregister(vcrypto);
  235. }
  236. /*
  237. * vcrypto_algo_is_supported()
  238. * @vcrypto: Pointer to virtio crypto device.
  239. * @service: The bit number for service validate.
  240. * See VIRTIO_CRYPTO_SERVICE_*
  241. * @algo : The bit number for the algorithm to validate.
  242. *
  243. *
  244. * Validate if the virtio crypto device supports a service and
  245. * algo.
  246. *
  247. * Return true if device supports a service and algo.
  248. */
  249. bool virtcrypto_algo_is_supported(struct virtio_crypto *vcrypto,
  250. uint32_t service,
  251. uint32_t algo)
  252. {
  253. uint32_t service_mask = 1u << service;
  254. uint32_t algo_mask = 0;
  255. bool low = true;
  256. if (algo > 31) {
  257. algo -= 32;
  258. low = false;
  259. }
  260. if (!(vcrypto->crypto_services & service_mask))
  261. return false;
  262. switch (service) {
  263. case VIRTIO_CRYPTO_SERVICE_CIPHER:
  264. if (low)
  265. algo_mask = vcrypto->cipher_algo_l;
  266. else
  267. algo_mask = vcrypto->cipher_algo_h;
  268. break;
  269. case VIRTIO_CRYPTO_SERVICE_HASH:
  270. algo_mask = vcrypto->hash_algo;
  271. break;
  272. case VIRTIO_CRYPTO_SERVICE_MAC:
  273. if (low)
  274. algo_mask = vcrypto->mac_algo_l;
  275. else
  276. algo_mask = vcrypto->mac_algo_h;
  277. break;
  278. case VIRTIO_CRYPTO_SERVICE_AEAD:
  279. algo_mask = vcrypto->aead_algo;
  280. break;
  281. }
  282. if (!(algo_mask & (1u << algo)))
  283. return false;
  284. return true;
  285. }