virtio_crypto_core.c 12 KB

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