virtio_crypto_core.c 12 KB

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