virtio_crypto_core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /* Driver 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. #include <linux/err.h>
  19. #include <linux/module.h>
  20. #include <linux/virtio_config.h>
  21. #include <linux/cpu.h>
  22. #include <uapi/linux/virtio_crypto.h>
  23. #include "virtio_crypto_common.h"
  24. static void
  25. virtcrypto_clear_request(struct virtio_crypto_request *vc_req)
  26. {
  27. if (vc_req) {
  28. kzfree(vc_req->iv);
  29. kzfree(vc_req->req_data);
  30. kfree(vc_req->sgs);
  31. }
  32. }
  33. static void virtcrypto_dataq_callback(struct virtqueue *vq)
  34. {
  35. struct virtio_crypto *vcrypto = vq->vdev->priv;
  36. struct virtio_crypto_request *vc_req;
  37. unsigned long flags;
  38. unsigned int len;
  39. struct ablkcipher_request *ablk_req;
  40. int error;
  41. unsigned int qid = vq->index;
  42. spin_lock_irqsave(&vcrypto->data_vq[qid].lock, flags);
  43. do {
  44. virtqueue_disable_cb(vq);
  45. while ((vc_req = virtqueue_get_buf(vq, &len)) != NULL) {
  46. if (vc_req->type == VIRTIO_CRYPTO_SYM_OP_CIPHER) {
  47. switch (vc_req->status) {
  48. case VIRTIO_CRYPTO_OK:
  49. error = 0;
  50. break;
  51. case VIRTIO_CRYPTO_INVSESS:
  52. case VIRTIO_CRYPTO_ERR:
  53. error = -EINVAL;
  54. break;
  55. case VIRTIO_CRYPTO_BADMSG:
  56. error = -EBADMSG;
  57. break;
  58. default:
  59. error = -EIO;
  60. break;
  61. }
  62. ablk_req = vc_req->ablkcipher_req;
  63. virtcrypto_clear_request(vc_req);
  64. spin_unlock_irqrestore(
  65. &vcrypto->data_vq[qid].lock, flags);
  66. /* Finish the encrypt or decrypt process */
  67. ablk_req->base.complete(&ablk_req->base, error);
  68. spin_lock_irqsave(
  69. &vcrypto->data_vq[qid].lock, flags);
  70. }
  71. }
  72. } while (!virtqueue_enable_cb(vq));
  73. spin_unlock_irqrestore(&vcrypto->data_vq[qid].lock, flags);
  74. }
  75. static int virtcrypto_find_vqs(struct virtio_crypto *vi)
  76. {
  77. vq_callback_t **callbacks;
  78. struct virtqueue **vqs;
  79. int ret = -ENOMEM;
  80. int i, total_vqs;
  81. const char **names;
  82. /*
  83. * We expect 1 data virtqueue, followed by
  84. * possible N-1 data queues used in multiqueue mode,
  85. * followed by control vq.
  86. */
  87. total_vqs = vi->max_data_queues + 1;
  88. /* Allocate space for find_vqs parameters */
  89. vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
  90. if (!vqs)
  91. goto err_vq;
  92. callbacks = kcalloc(total_vqs, sizeof(*callbacks), GFP_KERNEL);
  93. if (!callbacks)
  94. goto err_callback;
  95. names = kcalloc(total_vqs, sizeof(*names), GFP_KERNEL);
  96. if (!names)
  97. goto err_names;
  98. /* Parameters for control virtqueue */
  99. callbacks[total_vqs - 1] = NULL;
  100. names[total_vqs - 1] = "controlq";
  101. /* Allocate/initialize parameters for data virtqueues */
  102. for (i = 0; i < vi->max_data_queues; i++) {
  103. callbacks[i] = virtcrypto_dataq_callback;
  104. snprintf(vi->data_vq[i].name, sizeof(vi->data_vq[i].name),
  105. "dataq.%d", i);
  106. names[i] = vi->data_vq[i].name;
  107. }
  108. ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
  109. names, NULL);
  110. if (ret)
  111. goto err_find;
  112. vi->ctrl_vq = vqs[total_vqs - 1];
  113. for (i = 0; i < vi->max_data_queues; i++) {
  114. spin_lock_init(&vi->data_vq[i].lock);
  115. vi->data_vq[i].vq = vqs[i];
  116. }
  117. kfree(names);
  118. kfree(callbacks);
  119. kfree(vqs);
  120. return 0;
  121. err_find:
  122. kfree(names);
  123. err_names:
  124. kfree(callbacks);
  125. err_callback:
  126. kfree(vqs);
  127. err_vq:
  128. return ret;
  129. }
  130. static int virtcrypto_alloc_queues(struct virtio_crypto *vi)
  131. {
  132. vi->data_vq = kcalloc(vi->max_data_queues, sizeof(*vi->data_vq),
  133. GFP_KERNEL);
  134. if (!vi->data_vq)
  135. return -ENOMEM;
  136. return 0;
  137. }
  138. static void virtcrypto_clean_affinity(struct virtio_crypto *vi, long hcpu)
  139. {
  140. int i;
  141. if (vi->affinity_hint_set) {
  142. for (i = 0; i < vi->max_data_queues; i++)
  143. virtqueue_set_affinity(vi->data_vq[i].vq, -1);
  144. vi->affinity_hint_set = false;
  145. }
  146. }
  147. static void virtcrypto_set_affinity(struct virtio_crypto *vcrypto)
  148. {
  149. int i = 0;
  150. int cpu;
  151. /*
  152. * In single queue mode, we don't set the cpu affinity.
  153. */
  154. if (vcrypto->curr_queue == 1 || vcrypto->max_data_queues == 1) {
  155. virtcrypto_clean_affinity(vcrypto, -1);
  156. return;
  157. }
  158. /*
  159. * In multiqueue mode, we let the queue to be private to one cpu
  160. * by setting the affinity hint to eliminate the contention.
  161. *
  162. * TODO: adds cpu hotplug support by register cpu notifier.
  163. *
  164. */
  165. for_each_online_cpu(cpu) {
  166. virtqueue_set_affinity(vcrypto->data_vq[i].vq, cpu);
  167. if (++i >= vcrypto->max_data_queues)
  168. break;
  169. }
  170. vcrypto->affinity_hint_set = true;
  171. }
  172. static void virtcrypto_free_queues(struct virtio_crypto *vi)
  173. {
  174. kfree(vi->data_vq);
  175. }
  176. static int virtcrypto_init_vqs(struct virtio_crypto *vi)
  177. {
  178. int ret;
  179. /* Allocate send & receive queues */
  180. ret = virtcrypto_alloc_queues(vi);
  181. if (ret)
  182. goto err;
  183. ret = virtcrypto_find_vqs(vi);
  184. if (ret)
  185. goto err_free;
  186. get_online_cpus();
  187. virtcrypto_set_affinity(vi);
  188. put_online_cpus();
  189. return 0;
  190. err_free:
  191. virtcrypto_free_queues(vi);
  192. err:
  193. return ret;
  194. }
  195. static int virtcrypto_update_status(struct virtio_crypto *vcrypto)
  196. {
  197. u32 status;
  198. int err;
  199. virtio_cread(vcrypto->vdev,
  200. struct virtio_crypto_config, status, &status);
  201. /*
  202. * Unknown status bits would be a host error and the driver
  203. * should consider the device to be broken.
  204. */
  205. if (status & (~VIRTIO_CRYPTO_S_HW_READY)) {
  206. dev_warn(&vcrypto->vdev->dev,
  207. "Unknown status bits: 0x%x\n", status);
  208. virtio_break_device(vcrypto->vdev);
  209. return -EPERM;
  210. }
  211. if (vcrypto->status == status)
  212. return 0;
  213. vcrypto->status = status;
  214. if (vcrypto->status & VIRTIO_CRYPTO_S_HW_READY) {
  215. err = virtcrypto_dev_start(vcrypto);
  216. if (err) {
  217. dev_err(&vcrypto->vdev->dev,
  218. "Failed to start virtio crypto device.\n");
  219. return -EPERM;
  220. }
  221. dev_info(&vcrypto->vdev->dev, "Accelerator is ready\n");
  222. } else {
  223. virtcrypto_dev_stop(vcrypto);
  224. dev_info(&vcrypto->vdev->dev, "Accelerator is not ready\n");
  225. }
  226. return 0;
  227. }
  228. static void virtcrypto_del_vqs(struct virtio_crypto *vcrypto)
  229. {
  230. struct virtio_device *vdev = vcrypto->vdev;
  231. virtcrypto_clean_affinity(vcrypto, -1);
  232. vdev->config->del_vqs(vdev);
  233. virtcrypto_free_queues(vcrypto);
  234. }
  235. static int virtcrypto_probe(struct virtio_device *vdev)
  236. {
  237. int err = -EFAULT;
  238. struct virtio_crypto *vcrypto;
  239. u32 max_data_queues = 0, max_cipher_key_len = 0;
  240. u32 max_auth_key_len = 0;
  241. u64 max_size = 0;
  242. if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
  243. return -ENODEV;
  244. if (!vdev->config->get) {
  245. dev_err(&vdev->dev, "%s failure: config access disabled\n",
  246. __func__);
  247. return -EINVAL;
  248. }
  249. if (num_possible_nodes() > 1 && dev_to_node(&vdev->dev) < 0) {
  250. /*
  251. * If the accelerator is connected to a node with no memory
  252. * there is no point in using the accelerator since the remote
  253. * memory transaction will be very slow.
  254. */
  255. dev_err(&vdev->dev, "Invalid NUMA configuration.\n");
  256. return -EINVAL;
  257. }
  258. vcrypto = kzalloc_node(sizeof(*vcrypto), GFP_KERNEL,
  259. dev_to_node(&vdev->dev));
  260. if (!vcrypto)
  261. return -ENOMEM;
  262. virtio_cread(vdev, struct virtio_crypto_config,
  263. max_dataqueues, &max_data_queues);
  264. if (max_data_queues < 1)
  265. max_data_queues = 1;
  266. virtio_cread(vdev, struct virtio_crypto_config,
  267. max_cipher_key_len, &max_cipher_key_len);
  268. virtio_cread(vdev, struct virtio_crypto_config,
  269. max_auth_key_len, &max_auth_key_len);
  270. virtio_cread(vdev, struct virtio_crypto_config,
  271. max_size, &max_size);
  272. /* Add virtio crypto device to global table */
  273. err = virtcrypto_devmgr_add_dev(vcrypto);
  274. if (err) {
  275. dev_err(&vdev->dev, "Failed to add new virtio crypto device.\n");
  276. goto free;
  277. }
  278. vcrypto->owner = THIS_MODULE;
  279. vcrypto = vdev->priv = vcrypto;
  280. vcrypto->vdev = vdev;
  281. spin_lock_init(&vcrypto->ctrl_lock);
  282. /* Use single data queue as default */
  283. vcrypto->curr_queue = 1;
  284. vcrypto->max_data_queues = max_data_queues;
  285. vcrypto->max_cipher_key_len = max_cipher_key_len;
  286. vcrypto->max_auth_key_len = max_auth_key_len;
  287. vcrypto->max_size = max_size;
  288. dev_info(&vdev->dev,
  289. "max_queues: %u, max_cipher_key_len: %u, max_auth_key_len: %u, max_size 0x%llx\n",
  290. vcrypto->max_data_queues,
  291. vcrypto->max_cipher_key_len,
  292. vcrypto->max_auth_key_len,
  293. vcrypto->max_size);
  294. err = virtcrypto_init_vqs(vcrypto);
  295. if (err) {
  296. dev_err(&vdev->dev, "Failed to initialize vqs.\n");
  297. goto free_dev;
  298. }
  299. virtio_device_ready(vdev);
  300. err = virtcrypto_update_status(vcrypto);
  301. if (err)
  302. goto free_vqs;
  303. return 0;
  304. free_vqs:
  305. vcrypto->vdev->config->reset(vdev);
  306. virtcrypto_del_vqs(vcrypto);
  307. free_dev:
  308. virtcrypto_devmgr_rm_dev(vcrypto);
  309. free:
  310. kfree(vcrypto);
  311. return err;
  312. }
  313. static void virtcrypto_free_unused_reqs(struct virtio_crypto *vcrypto)
  314. {
  315. struct virtio_crypto_request *vc_req;
  316. int i;
  317. struct virtqueue *vq;
  318. for (i = 0; i < vcrypto->max_data_queues; i++) {
  319. vq = vcrypto->data_vq[i].vq;
  320. while ((vc_req = virtqueue_detach_unused_buf(vq)) != NULL) {
  321. kfree(vc_req->req_data);
  322. kfree(vc_req->sgs);
  323. }
  324. }
  325. }
  326. static void virtcrypto_remove(struct virtio_device *vdev)
  327. {
  328. struct virtio_crypto *vcrypto = vdev->priv;
  329. dev_info(&vdev->dev, "Start virtcrypto_remove.\n");
  330. if (virtcrypto_dev_started(vcrypto))
  331. virtcrypto_dev_stop(vcrypto);
  332. vdev->config->reset(vdev);
  333. virtcrypto_free_unused_reqs(vcrypto);
  334. virtcrypto_del_vqs(vcrypto);
  335. virtcrypto_devmgr_rm_dev(vcrypto);
  336. kfree(vcrypto);
  337. }
  338. static void virtcrypto_config_changed(struct virtio_device *vdev)
  339. {
  340. struct virtio_crypto *vcrypto = vdev->priv;
  341. virtcrypto_update_status(vcrypto);
  342. }
  343. #ifdef CONFIG_PM_SLEEP
  344. static int virtcrypto_freeze(struct virtio_device *vdev)
  345. {
  346. struct virtio_crypto *vcrypto = vdev->priv;
  347. vdev->config->reset(vdev);
  348. virtcrypto_free_unused_reqs(vcrypto);
  349. if (virtcrypto_dev_started(vcrypto))
  350. virtcrypto_dev_stop(vcrypto);
  351. virtcrypto_del_vqs(vcrypto);
  352. return 0;
  353. }
  354. static int virtcrypto_restore(struct virtio_device *vdev)
  355. {
  356. struct virtio_crypto *vcrypto = vdev->priv;
  357. int err;
  358. err = virtcrypto_init_vqs(vcrypto);
  359. if (err)
  360. return err;
  361. virtio_device_ready(vdev);
  362. err = virtcrypto_dev_start(vcrypto);
  363. if (err) {
  364. dev_err(&vdev->dev, "Failed to start virtio crypto device.\n");
  365. return -EFAULT;
  366. }
  367. return 0;
  368. }
  369. #endif
  370. static unsigned int features[] = {
  371. /* none */
  372. };
  373. static struct virtio_device_id id_table[] = {
  374. { VIRTIO_ID_CRYPTO, VIRTIO_DEV_ANY_ID },
  375. { 0 },
  376. };
  377. static struct virtio_driver virtio_crypto_driver = {
  378. .driver.name = KBUILD_MODNAME,
  379. .driver.owner = THIS_MODULE,
  380. .feature_table = features,
  381. .feature_table_size = ARRAY_SIZE(features),
  382. .id_table = id_table,
  383. .probe = virtcrypto_probe,
  384. .remove = virtcrypto_remove,
  385. .config_changed = virtcrypto_config_changed,
  386. #ifdef CONFIG_PM_SLEEP
  387. .freeze = virtcrypto_freeze,
  388. .restore = virtcrypto_restore,
  389. #endif
  390. };
  391. module_virtio_driver(virtio_crypto_driver);
  392. MODULE_DEVICE_TABLE(virtio, id_table);
  393. MODULE_DESCRIPTION("virtio crypto device driver");
  394. MODULE_LICENSE("GPL");
  395. MODULE_AUTHOR("Gonglei <arei.gonglei@huawei.com>");