virtio_crypto_core.c 12 KB

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