Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2 /* Driver for Virtio crypto device.
  3  *
  4  * Copyright 2016 HUAWEI TECHNOLOGIES CO., LTD.
  5  */
  6
  7#include <linux/err.h>
  8#include <linux/module.h>
  9#include <linux/virtio_config.h>
 10#include <linux/cpu.h>
 11
 12#include <uapi/linux/virtio_crypto.h>
 13#include "virtio_crypto_common.h"
 14
 15
 16void
 17virtcrypto_clear_request(struct virtio_crypto_request *vc_req)
 18{
 19	if (vc_req) {
 20		kfree_sensitive(vc_req->req_data);
 21		kfree(vc_req->sgs);
 22	}
 23}
 24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 25static void virtcrypto_dataq_callback(struct virtqueue *vq)
 26{
 27	struct virtio_crypto *vcrypto = vq->vdev->priv;
 28	struct virtio_crypto_request *vc_req;
 29	unsigned long flags;
 30	unsigned int len;
 31	unsigned int qid = vq->index;
 32
 33	spin_lock_irqsave(&vcrypto->data_vq[qid].lock, flags);
 34	do {
 35		virtqueue_disable_cb(vq);
 36		while ((vc_req = virtqueue_get_buf(vq, &len)) != NULL) {
 37			spin_unlock_irqrestore(
 38				&vcrypto->data_vq[qid].lock, flags);
 39			if (vc_req->alg_cb)
 40				vc_req->alg_cb(vc_req, len);
 41			spin_lock_irqsave(
 42				&vcrypto->data_vq[qid].lock, flags);
 43		}
 44	} while (!virtqueue_enable_cb(vq));
 45	spin_unlock_irqrestore(&vcrypto->data_vq[qid].lock, flags);
 46}
 47
 48static int virtcrypto_find_vqs(struct virtio_crypto *vi)
 49{
 50	vq_callback_t **callbacks;
 51	struct virtqueue **vqs;
 52	int ret = -ENOMEM;
 53	int i, total_vqs;
 54	const char **names;
 55	struct device *dev = &vi->vdev->dev;
 56
 57	/*
 58	 * We expect 1 data virtqueue, followed by
 59	 * possible N-1 data queues used in multiqueue mode,
 60	 * followed by control vq.
 61	 */
 62	total_vqs = vi->max_data_queues + 1;
 63
 64	/* Allocate space for find_vqs parameters */
 65	vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
 66	if (!vqs)
 67		goto err_vq;
 68	callbacks = kcalloc(total_vqs, sizeof(*callbacks), GFP_KERNEL);
 69	if (!callbacks)
 70		goto err_callback;
 71	names = kcalloc(total_vqs, sizeof(*names), GFP_KERNEL);
 72	if (!names)
 73		goto err_names;
 74
 75	/* Parameters for control virtqueue */
 76	callbacks[total_vqs - 1] = NULL;
 77	names[total_vqs - 1] = "controlq";
 78
 79	/* Allocate/initialize parameters for data virtqueues */
 80	for (i = 0; i < vi->max_data_queues; i++) {
 81		callbacks[i] = virtcrypto_dataq_callback;
 82		snprintf(vi->data_vq[i].name, sizeof(vi->data_vq[i].name),
 83				"dataq.%d", i);
 84		names[i] = vi->data_vq[i].name;
 85	}
 86
 87	ret = virtio_find_vqs(vi->vdev, total_vqs, vqs, callbacks, names, NULL);
 88	if (ret)
 89		goto err_find;
 90
 91	vi->ctrl_vq = vqs[total_vqs - 1];
 92
 93	for (i = 0; i < vi->max_data_queues; i++) {
 94		spin_lock_init(&vi->data_vq[i].lock);
 95		vi->data_vq[i].vq = vqs[i];
 96		/* Initialize crypto engine */
 97		vi->data_vq[i].engine = crypto_engine_alloc_init(dev, 1);
 
 98		if (!vi->data_vq[i].engine) {
 99			ret = -ENOMEM;
100			goto err_engine;
101		}
102	}
103
104	kfree(names);
105	kfree(callbacks);
106	kfree(vqs);
107
108	return 0;
109
110err_engine:
111err_find:
112	kfree(names);
113err_names:
114	kfree(callbacks);
115err_callback:
116	kfree(vqs);
117err_vq:
118	return ret;
119}
120
121static int virtcrypto_alloc_queues(struct virtio_crypto *vi)
122{
123	vi->data_vq = kcalloc(vi->max_data_queues, sizeof(*vi->data_vq),
124				GFP_KERNEL);
125	if (!vi->data_vq)
126		return -ENOMEM;
127
128	return 0;
129}
130
131static void virtcrypto_clean_affinity(struct virtio_crypto *vi, long hcpu)
132{
133	int i;
134
135	if (vi->affinity_hint_set) {
136		for (i = 0; i < vi->max_data_queues; i++)
137			virtqueue_set_affinity(vi->data_vq[i].vq, NULL);
138
139		vi->affinity_hint_set = false;
140	}
141}
142
143static void virtcrypto_set_affinity(struct virtio_crypto *vcrypto)
144{
145	int i = 0;
146	int cpu;
147
148	/*
149	 * In single queue mode, we don't set the cpu affinity.
150	 */
151	if (vcrypto->curr_queue == 1 || vcrypto->max_data_queues == 1) {
152		virtcrypto_clean_affinity(vcrypto, -1);
153		return;
154	}
155
156	/*
157	 * In multiqueue mode, we let the queue to be private to one cpu
158	 * by setting the affinity hint to eliminate the contention.
159	 *
160	 * TODO: adds cpu hotplug support by register cpu notifier.
161	 *
162	 */
163	for_each_online_cpu(cpu) {
164		virtqueue_set_affinity(vcrypto->data_vq[i].vq, cpumask_of(cpu));
165		if (++i >= vcrypto->max_data_queues)
166			break;
167	}
168
169	vcrypto->affinity_hint_set = true;
170}
171
172static void virtcrypto_free_queues(struct virtio_crypto *vi)
173{
174	kfree(vi->data_vq);
175}
176
177static int virtcrypto_init_vqs(struct virtio_crypto *vi)
178{
179	int ret;
180
181	/* Allocate send & receive queues */
182	ret = virtcrypto_alloc_queues(vi);
183	if (ret)
184		goto err;
185
186	ret = virtcrypto_find_vqs(vi);
187	if (ret)
188		goto err_free;
189
190	get_online_cpus();
191	virtcrypto_set_affinity(vi);
192	put_online_cpus();
193
194	return 0;
195
196err_free:
197	virtcrypto_free_queues(vi);
198err:
199	return ret;
200}
201
202static int virtcrypto_update_status(struct virtio_crypto *vcrypto)
203{
204	u32 status;
205	int err;
206
207	virtio_cread_le(vcrypto->vdev,
208			struct virtio_crypto_config, status, &status);
209
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
218		virtio_break_device(vcrypto->vdev);
219		return -EPERM;
220	}
221
222	if (vcrypto->status == status)
223		return 0;
224
225	vcrypto->status = status;
226
227	if (vcrypto->status & VIRTIO_CRYPTO_S_HW_READY) {
228		err = virtcrypto_dev_start(vcrypto);
229		if (err) {
230			dev_err(&vcrypto->vdev->dev,
231				"Failed to start virtio crypto device.\n");
232
233			return -EPERM;
234		}
235		dev_info(&vcrypto->vdev->dev, "Accelerator device is ready\n");
236	} else {
237		virtcrypto_dev_stop(vcrypto);
238		dev_info(&vcrypto->vdev->dev, "Accelerator is not ready\n");
239	}
240
241	return 0;
242}
243
244static int virtcrypto_start_crypto_engines(struct virtio_crypto *vcrypto)
245{
246	int32_t i;
247	int ret;
248
249	for (i = 0; i < vcrypto->max_data_queues; i++) {
250		if (vcrypto->data_vq[i].engine) {
251			ret = crypto_engine_start(vcrypto->data_vq[i].engine);
252			if (ret)
253				goto err;
254		}
255	}
256
257	return 0;
258
259err:
260	while (--i >= 0)
261		if (vcrypto->data_vq[i].engine)
262			crypto_engine_exit(vcrypto->data_vq[i].engine);
263
264	return ret;
265}
266
267static void virtcrypto_clear_crypto_engines(struct virtio_crypto *vcrypto)
268{
269	u32 i;
270
271	for (i = 0; i < vcrypto->max_data_queues; i++)
272		if (vcrypto->data_vq[i].engine)
273			crypto_engine_exit(vcrypto->data_vq[i].engine);
274}
275
276static void virtcrypto_del_vqs(struct virtio_crypto *vcrypto)
277{
278	struct virtio_device *vdev = vcrypto->vdev;
279
280	virtcrypto_clean_affinity(vcrypto, -1);
281
282	vdev->config->del_vqs(vdev);
283
284	virtcrypto_free_queues(vcrypto);
285}
286
287static int virtcrypto_probe(struct virtio_device *vdev)
288{
289	int err = -EFAULT;
290	struct virtio_crypto *vcrypto;
291	u32 max_data_queues = 0, max_cipher_key_len = 0;
292	u32 max_auth_key_len = 0;
293	u64 max_size = 0;
294	u32 cipher_algo_l = 0;
295	u32 cipher_algo_h = 0;
296	u32 hash_algo = 0;
297	u32 mac_algo_l = 0;
298	u32 mac_algo_h = 0;
299	u32 aead_algo = 0;
 
300	u32 crypto_services = 0;
301
302	if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
303		return -ENODEV;
304
305	if (!vdev->config->get) {
306		dev_err(&vdev->dev, "%s failure: config access disabled\n",
307			__func__);
308		return -EINVAL;
309	}
310
311	if (num_possible_nodes() > 1 && dev_to_node(&vdev->dev) < 0) {
312		/*
313		 * If the accelerator is connected to a node with no memory
314		 * there is no point in using the accelerator since the remote
315		 * memory transaction will be very slow.
316		 */
317		dev_err(&vdev->dev, "Invalid NUMA configuration.\n");
318		return -EINVAL;
319	}
320
321	vcrypto = kzalloc_node(sizeof(*vcrypto), GFP_KERNEL,
322					dev_to_node(&vdev->dev));
323	if (!vcrypto)
324		return -ENOMEM;
325
326	virtio_cread_le(vdev, struct virtio_crypto_config,
327			max_dataqueues, &max_data_queues);
328	if (max_data_queues < 1)
329		max_data_queues = 1;
330
331	virtio_cread_le(vdev, struct virtio_crypto_config,
332			max_cipher_key_len, &max_cipher_key_len);
333	virtio_cread_le(vdev, struct virtio_crypto_config,
334			max_auth_key_len, &max_auth_key_len);
335	virtio_cread_le(vdev, struct virtio_crypto_config,
336			max_size, &max_size);
337	virtio_cread_le(vdev, struct virtio_crypto_config,
338			crypto_services, &crypto_services);
339	virtio_cread_le(vdev, struct virtio_crypto_config,
340			cipher_algo_l, &cipher_algo_l);
341	virtio_cread_le(vdev, struct virtio_crypto_config,
342			cipher_algo_h, &cipher_algo_h);
343	virtio_cread_le(vdev, struct virtio_crypto_config,
344			hash_algo, &hash_algo);
345	virtio_cread_le(vdev, struct virtio_crypto_config,
346			mac_algo_l, &mac_algo_l);
347	virtio_cread_le(vdev, struct virtio_crypto_config,
348			mac_algo_h, &mac_algo_h);
349	virtio_cread_le(vdev, struct virtio_crypto_config,
350			aead_algo, &aead_algo);
 
 
 
351
352	/* Add virtio crypto device to global table */
353	err = virtcrypto_devmgr_add_dev(vcrypto);
354	if (err) {
355		dev_err(&vdev->dev, "Failed to add new virtio crypto device.\n");
356		goto free;
357	}
358	vcrypto->owner = THIS_MODULE;
359	vcrypto = vdev->priv = vcrypto;
360	vcrypto->vdev = vdev;
361
362	spin_lock_init(&vcrypto->ctrl_lock);
363
364	/* Use single data queue as default */
365	vcrypto->curr_queue = 1;
366	vcrypto->max_data_queues = max_data_queues;
367	vcrypto->max_cipher_key_len = max_cipher_key_len;
368	vcrypto->max_auth_key_len = max_auth_key_len;
369	vcrypto->max_size = max_size;
370	vcrypto->crypto_services = crypto_services;
371	vcrypto->cipher_algo_l = cipher_algo_l;
372	vcrypto->cipher_algo_h = cipher_algo_h;
373	vcrypto->mac_algo_l = mac_algo_l;
374	vcrypto->mac_algo_h = mac_algo_h;
375	vcrypto->hash_algo = hash_algo;
376	vcrypto->aead_algo = aead_algo;
377
378
379	dev_info(&vdev->dev,
380		"max_queues: %u, max_cipher_key_len: %u, max_auth_key_len: %u, max_size 0x%llx\n",
381		vcrypto->max_data_queues,
382		vcrypto->max_cipher_key_len,
383		vcrypto->max_auth_key_len,
384		vcrypto->max_size);
385
386	err = virtcrypto_init_vqs(vcrypto);
387	if (err) {
388		dev_err(&vdev->dev, "Failed to initialize vqs.\n");
389		goto free_dev;
390	}
391
392	err = virtcrypto_start_crypto_engines(vcrypto);
393	if (err)
394		goto free_vqs;
395
396	virtio_device_ready(vdev);
397
398	err = virtcrypto_update_status(vcrypto);
399	if (err)
400		goto free_engines;
401
402	return 0;
403
404free_engines:
405	virtcrypto_clear_crypto_engines(vcrypto);
406free_vqs:
407	vcrypto->vdev->config->reset(vdev);
408	virtcrypto_del_vqs(vcrypto);
409free_dev:
410	virtcrypto_devmgr_rm_dev(vcrypto);
411free:
412	kfree(vcrypto);
413	return err;
414}
415
416static void virtcrypto_free_unused_reqs(struct virtio_crypto *vcrypto)
417{
418	struct virtio_crypto_request *vc_req;
419	int i;
420	struct virtqueue *vq;
421
422	for (i = 0; i < vcrypto->max_data_queues; i++) {
423		vq = vcrypto->data_vq[i].vq;
424		while ((vc_req = virtqueue_detach_unused_buf(vq)) != NULL) {
425			kfree(vc_req->req_data);
426			kfree(vc_req->sgs);
427		}
428	}
429}
430
431static void virtcrypto_remove(struct virtio_device *vdev)
432{
433	struct virtio_crypto *vcrypto = vdev->priv;
434
435	dev_info(&vdev->dev, "Start virtcrypto_remove.\n");
436
437	if (virtcrypto_dev_started(vcrypto))
438		virtcrypto_dev_stop(vcrypto);
439	vdev->config->reset(vdev);
440	virtcrypto_free_unused_reqs(vcrypto);
441	virtcrypto_clear_crypto_engines(vcrypto);
442	virtcrypto_del_vqs(vcrypto);
443	virtcrypto_devmgr_rm_dev(vcrypto);
444	kfree(vcrypto);
445}
446
447static void virtcrypto_config_changed(struct virtio_device *vdev)
448{
449	struct virtio_crypto *vcrypto = vdev->priv;
450
451	virtcrypto_update_status(vcrypto);
452}
453
454#ifdef CONFIG_PM_SLEEP
455static int virtcrypto_freeze(struct virtio_device *vdev)
456{
457	struct virtio_crypto *vcrypto = vdev->priv;
458
459	vdev->config->reset(vdev);
460	virtcrypto_free_unused_reqs(vcrypto);
461	if (virtcrypto_dev_started(vcrypto))
462		virtcrypto_dev_stop(vcrypto);
463
464	virtcrypto_clear_crypto_engines(vcrypto);
465	virtcrypto_del_vqs(vcrypto);
466	return 0;
467}
468
469static int virtcrypto_restore(struct virtio_device *vdev)
470{
471	struct virtio_crypto *vcrypto = vdev->priv;
472	int err;
473
474	err = virtcrypto_init_vqs(vcrypto);
475	if (err)
476		return err;
477
478	err = virtcrypto_start_crypto_engines(vcrypto);
479	if (err)
480		goto free_vqs;
481
482	virtio_device_ready(vdev);
483
484	err = virtcrypto_dev_start(vcrypto);
485	if (err) {
486		dev_err(&vdev->dev, "Failed to start virtio crypto device.\n");
487		goto free_engines;
488	}
489
490	return 0;
491
492free_engines:
493	virtcrypto_clear_crypto_engines(vcrypto);
494free_vqs:
495	vcrypto->vdev->config->reset(vdev);
496	virtcrypto_del_vqs(vcrypto);
497	return err;
498}
499#endif
500
501static const unsigned int features[] = {
502	/* none */
503};
504
505static const struct virtio_device_id id_table[] = {
506	{ VIRTIO_ID_CRYPTO, VIRTIO_DEV_ANY_ID },
507	{ 0 },
508};
509
510static struct virtio_driver virtio_crypto_driver = {
511	.driver.name         = KBUILD_MODNAME,
512	.driver.owner        = THIS_MODULE,
513	.feature_table       = features,
514	.feature_table_size  = ARRAY_SIZE(features),
515	.id_table            = id_table,
516	.probe               = virtcrypto_probe,
517	.remove              = virtcrypto_remove,
518	.config_changed = virtcrypto_config_changed,
519#ifdef CONFIG_PM_SLEEP
520	.freeze = virtcrypto_freeze,
521	.restore = virtcrypto_restore,
522#endif
523};
524
525module_virtio_driver(virtio_crypto_driver);
526
527MODULE_DEVICE_TABLE(virtio, id_table);
528MODULE_DESCRIPTION("virtio crypto device driver");
529MODULE_LICENSE("GPL");
530MODULE_AUTHOR("Gonglei <arei.gonglei@huawei.com>");
v6.2
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2 /* Driver for Virtio crypto device.
  3  *
  4  * Copyright 2016 HUAWEI TECHNOLOGIES CO., LTD.
  5  */
  6
  7#include <linux/err.h>
  8#include <linux/module.h>
  9#include <linux/virtio_config.h>
 10#include <linux/cpu.h>
 11
 12#include <uapi/linux/virtio_crypto.h>
 13#include "virtio_crypto_common.h"
 14
 15
 16void
 17virtcrypto_clear_request(struct virtio_crypto_request *vc_req)
 18{
 19	if (vc_req) {
 20		kfree_sensitive(vc_req->req_data);
 21		kfree(vc_req->sgs);
 22	}
 23}
 24
 25static void virtio_crypto_ctrlq_callback(struct virtio_crypto_ctrl_request *vc_ctrl_req)
 26{
 27	complete(&vc_ctrl_req->compl);
 28}
 29
 30static void virtcrypto_ctrlq_callback(struct virtqueue *vq)
 31{
 32	struct virtio_crypto *vcrypto = vq->vdev->priv;
 33	struct virtio_crypto_ctrl_request *vc_ctrl_req;
 34	unsigned long flags;
 35	unsigned int len;
 36
 37	spin_lock_irqsave(&vcrypto->ctrl_lock, flags);
 38	do {
 39		virtqueue_disable_cb(vq);
 40		while ((vc_ctrl_req = virtqueue_get_buf(vq, &len)) != NULL) {
 41			spin_unlock_irqrestore(&vcrypto->ctrl_lock, flags);
 42			virtio_crypto_ctrlq_callback(vc_ctrl_req);
 43			spin_lock_irqsave(&vcrypto->ctrl_lock, flags);
 44		}
 45		if (unlikely(virtqueue_is_broken(vq)))
 46			break;
 47	} while (!virtqueue_enable_cb(vq));
 48	spin_unlock_irqrestore(&vcrypto->ctrl_lock, flags);
 49}
 50
 51int virtio_crypto_ctrl_vq_request(struct virtio_crypto *vcrypto, struct scatterlist *sgs[],
 52		unsigned int out_sgs, unsigned int in_sgs,
 53		struct virtio_crypto_ctrl_request *vc_ctrl_req)
 54{
 55	int err;
 56	unsigned long flags;
 57
 58	init_completion(&vc_ctrl_req->compl);
 59
 60	spin_lock_irqsave(&vcrypto->ctrl_lock, flags);
 61	err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, out_sgs, in_sgs, vc_ctrl_req, GFP_ATOMIC);
 62	if (err < 0) {
 63		spin_unlock_irqrestore(&vcrypto->ctrl_lock, flags);
 64		return err;
 65	}
 66
 67	virtqueue_kick(vcrypto->ctrl_vq);
 68	spin_unlock_irqrestore(&vcrypto->ctrl_lock, flags);
 69
 70	wait_for_completion(&vc_ctrl_req->compl);
 71
 72	return 0;
 73}
 74
 75static void virtcrypto_dataq_callback(struct virtqueue *vq)
 76{
 77	struct virtio_crypto *vcrypto = vq->vdev->priv;
 78	struct virtio_crypto_request *vc_req;
 79	unsigned long flags;
 80	unsigned int len;
 81	unsigned int qid = vq->index;
 82
 83	spin_lock_irqsave(&vcrypto->data_vq[qid].lock, flags);
 84	do {
 85		virtqueue_disable_cb(vq);
 86		while ((vc_req = virtqueue_get_buf(vq, &len)) != NULL) {
 87			spin_unlock_irqrestore(
 88				&vcrypto->data_vq[qid].lock, flags);
 89			if (vc_req->alg_cb)
 90				vc_req->alg_cb(vc_req, len);
 91			spin_lock_irqsave(
 92				&vcrypto->data_vq[qid].lock, flags);
 93		}
 94	} while (!virtqueue_enable_cb(vq));
 95	spin_unlock_irqrestore(&vcrypto->data_vq[qid].lock, flags);
 96}
 97
 98static int virtcrypto_find_vqs(struct virtio_crypto *vi)
 99{
100	vq_callback_t **callbacks;
101	struct virtqueue **vqs;
102	int ret = -ENOMEM;
103	int i, total_vqs;
104	const char **names;
105	struct device *dev = &vi->vdev->dev;
106
107	/*
108	 * We expect 1 data virtqueue, followed by
109	 * possible N-1 data queues used in multiqueue mode,
110	 * followed by control vq.
111	 */
112	total_vqs = vi->max_data_queues + 1;
113
114	/* Allocate space for find_vqs parameters */
115	vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
116	if (!vqs)
117		goto err_vq;
118	callbacks = kcalloc(total_vqs, sizeof(*callbacks), GFP_KERNEL);
119	if (!callbacks)
120		goto err_callback;
121	names = kcalloc(total_vqs, sizeof(*names), GFP_KERNEL);
122	if (!names)
123		goto err_names;
124
125	/* Parameters for control virtqueue */
126	callbacks[total_vqs - 1] = virtcrypto_ctrlq_callback;
127	names[total_vqs - 1] = "controlq";
128
129	/* Allocate/initialize parameters for data virtqueues */
130	for (i = 0; i < vi->max_data_queues; i++) {
131		callbacks[i] = virtcrypto_dataq_callback;
132		snprintf(vi->data_vq[i].name, sizeof(vi->data_vq[i].name),
133				"dataq.%d", i);
134		names[i] = vi->data_vq[i].name;
135	}
136
137	ret = virtio_find_vqs(vi->vdev, total_vqs, vqs, callbacks, names, NULL);
138	if (ret)
139		goto err_find;
140
141	vi->ctrl_vq = vqs[total_vqs - 1];
142
143	for (i = 0; i < vi->max_data_queues; i++) {
144		spin_lock_init(&vi->data_vq[i].lock);
145		vi->data_vq[i].vq = vqs[i];
146		/* Initialize crypto engine */
147		vi->data_vq[i].engine = crypto_engine_alloc_init_and_set(dev, true, NULL, true,
148						virtqueue_get_vring_size(vqs[i]));
149		if (!vi->data_vq[i].engine) {
150			ret = -ENOMEM;
151			goto err_engine;
152		}
153	}
154
155	kfree(names);
156	kfree(callbacks);
157	kfree(vqs);
158
159	return 0;
160
161err_engine:
162err_find:
163	kfree(names);
164err_names:
165	kfree(callbacks);
166err_callback:
167	kfree(vqs);
168err_vq:
169	return ret;
170}
171
172static int virtcrypto_alloc_queues(struct virtio_crypto *vi)
173{
174	vi->data_vq = kcalloc(vi->max_data_queues, sizeof(*vi->data_vq),
175				GFP_KERNEL);
176	if (!vi->data_vq)
177		return -ENOMEM;
178
179	return 0;
180}
181
182static void virtcrypto_clean_affinity(struct virtio_crypto *vi, long hcpu)
183{
184	int i;
185
186	if (vi->affinity_hint_set) {
187		for (i = 0; i < vi->max_data_queues; i++)
188			virtqueue_set_affinity(vi->data_vq[i].vq, NULL);
189
190		vi->affinity_hint_set = false;
191	}
192}
193
194static void virtcrypto_set_affinity(struct virtio_crypto *vcrypto)
195{
196	int i = 0;
197	int cpu;
198
199	/*
200	 * In single queue mode, we don't set the cpu affinity.
201	 */
202	if (vcrypto->curr_queue == 1 || vcrypto->max_data_queues == 1) {
203		virtcrypto_clean_affinity(vcrypto, -1);
204		return;
205	}
206
207	/*
208	 * In multiqueue mode, we let the queue to be private to one cpu
209	 * by setting the affinity hint to eliminate the contention.
210	 *
211	 * TODO: adds cpu hotplug support by register cpu notifier.
212	 *
213	 */
214	for_each_online_cpu(cpu) {
215		virtqueue_set_affinity(vcrypto->data_vq[i].vq, cpumask_of(cpu));
216		if (++i >= vcrypto->max_data_queues)
217			break;
218	}
219
220	vcrypto->affinity_hint_set = true;
221}
222
223static void virtcrypto_free_queues(struct virtio_crypto *vi)
224{
225	kfree(vi->data_vq);
226}
227
228static int virtcrypto_init_vqs(struct virtio_crypto *vi)
229{
230	int ret;
231
232	/* Allocate send & receive queues */
233	ret = virtcrypto_alloc_queues(vi);
234	if (ret)
235		goto err;
236
237	ret = virtcrypto_find_vqs(vi);
238	if (ret)
239		goto err_free;
240
241	cpus_read_lock();
242	virtcrypto_set_affinity(vi);
243	cpus_read_unlock();
244
245	return 0;
246
247err_free:
248	virtcrypto_free_queues(vi);
249err:
250	return ret;
251}
252
253static int virtcrypto_update_status(struct virtio_crypto *vcrypto)
254{
255	u32 status;
256	int err;
257
258	virtio_cread_le(vcrypto->vdev,
259			struct virtio_crypto_config, status, &status);
260
261	/*
262	 * Unknown status bits would be a host error and the driver
263	 * should consider the device to be broken.
264	 */
265	if (status & (~VIRTIO_CRYPTO_S_HW_READY)) {
266		dev_warn(&vcrypto->vdev->dev,
267				"Unknown status bits: 0x%x\n", status);
268
269		virtio_break_device(vcrypto->vdev);
270		return -EPERM;
271	}
272
273	if (vcrypto->status == status)
274		return 0;
275
276	vcrypto->status = status;
277
278	if (vcrypto->status & VIRTIO_CRYPTO_S_HW_READY) {
279		err = virtcrypto_dev_start(vcrypto);
280		if (err) {
281			dev_err(&vcrypto->vdev->dev,
282				"Failed to start virtio crypto device.\n");
283
284			return -EPERM;
285		}
286		dev_info(&vcrypto->vdev->dev, "Accelerator device is ready\n");
287	} else {
288		virtcrypto_dev_stop(vcrypto);
289		dev_info(&vcrypto->vdev->dev, "Accelerator is not ready\n");
290	}
291
292	return 0;
293}
294
295static int virtcrypto_start_crypto_engines(struct virtio_crypto *vcrypto)
296{
297	int32_t i;
298	int ret;
299
300	for (i = 0; i < vcrypto->max_data_queues; i++) {
301		if (vcrypto->data_vq[i].engine) {
302			ret = crypto_engine_start(vcrypto->data_vq[i].engine);
303			if (ret)
304				goto err;
305		}
306	}
307
308	return 0;
309
310err:
311	while (--i >= 0)
312		if (vcrypto->data_vq[i].engine)
313			crypto_engine_exit(vcrypto->data_vq[i].engine);
314
315	return ret;
316}
317
318static void virtcrypto_clear_crypto_engines(struct virtio_crypto *vcrypto)
319{
320	u32 i;
321
322	for (i = 0; i < vcrypto->max_data_queues; i++)
323		if (vcrypto->data_vq[i].engine)
324			crypto_engine_exit(vcrypto->data_vq[i].engine);
325}
326
327static void virtcrypto_del_vqs(struct virtio_crypto *vcrypto)
328{
329	struct virtio_device *vdev = vcrypto->vdev;
330
331	virtcrypto_clean_affinity(vcrypto, -1);
332
333	vdev->config->del_vqs(vdev);
334
335	virtcrypto_free_queues(vcrypto);
336}
337
338static int virtcrypto_probe(struct virtio_device *vdev)
339{
340	int err = -EFAULT;
341	struct virtio_crypto *vcrypto;
342	u32 max_data_queues = 0, max_cipher_key_len = 0;
343	u32 max_auth_key_len = 0;
344	u64 max_size = 0;
345	u32 cipher_algo_l = 0;
346	u32 cipher_algo_h = 0;
347	u32 hash_algo = 0;
348	u32 mac_algo_l = 0;
349	u32 mac_algo_h = 0;
350	u32 aead_algo = 0;
351	u32 akcipher_algo = 0;
352	u32 crypto_services = 0;
353
354	if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
355		return -ENODEV;
356
357	if (!vdev->config->get) {
358		dev_err(&vdev->dev, "%s failure: config access disabled\n",
359			__func__);
360		return -EINVAL;
361	}
362
363	if (num_possible_nodes() > 1 && dev_to_node(&vdev->dev) < 0) {
364		/*
365		 * If the accelerator is connected to a node with no memory
366		 * there is no point in using the accelerator since the remote
367		 * memory transaction will be very slow.
368		 */
369		dev_err(&vdev->dev, "Invalid NUMA configuration.\n");
370		return -EINVAL;
371	}
372
373	vcrypto = kzalloc_node(sizeof(*vcrypto), GFP_KERNEL,
374					dev_to_node(&vdev->dev));
375	if (!vcrypto)
376		return -ENOMEM;
377
378	virtio_cread_le(vdev, struct virtio_crypto_config,
379			max_dataqueues, &max_data_queues);
380	if (max_data_queues < 1)
381		max_data_queues = 1;
382
383	virtio_cread_le(vdev, struct virtio_crypto_config,
384			max_cipher_key_len, &max_cipher_key_len);
385	virtio_cread_le(vdev, struct virtio_crypto_config,
386			max_auth_key_len, &max_auth_key_len);
387	virtio_cread_le(vdev, struct virtio_crypto_config,
388			max_size, &max_size);
389	virtio_cread_le(vdev, struct virtio_crypto_config,
390			crypto_services, &crypto_services);
391	virtio_cread_le(vdev, struct virtio_crypto_config,
392			cipher_algo_l, &cipher_algo_l);
393	virtio_cread_le(vdev, struct virtio_crypto_config,
394			cipher_algo_h, &cipher_algo_h);
395	virtio_cread_le(vdev, struct virtio_crypto_config,
396			hash_algo, &hash_algo);
397	virtio_cread_le(vdev, struct virtio_crypto_config,
398			mac_algo_l, &mac_algo_l);
399	virtio_cread_le(vdev, struct virtio_crypto_config,
400			mac_algo_h, &mac_algo_h);
401	virtio_cread_le(vdev, struct virtio_crypto_config,
402			aead_algo, &aead_algo);
403	if (crypto_services & (1 << VIRTIO_CRYPTO_SERVICE_AKCIPHER))
404		virtio_cread_le(vdev, struct virtio_crypto_config,
405				akcipher_algo, &akcipher_algo);
406
407	/* Add virtio crypto device to global table */
408	err = virtcrypto_devmgr_add_dev(vcrypto);
409	if (err) {
410		dev_err(&vdev->dev, "Failed to add new virtio crypto device.\n");
411		goto free;
412	}
413	vcrypto->owner = THIS_MODULE;
414	vcrypto = vdev->priv = vcrypto;
415	vcrypto->vdev = vdev;
416
417	spin_lock_init(&vcrypto->ctrl_lock);
418
419	/* Use single data queue as default */
420	vcrypto->curr_queue = 1;
421	vcrypto->max_data_queues = max_data_queues;
422	vcrypto->max_cipher_key_len = max_cipher_key_len;
423	vcrypto->max_auth_key_len = max_auth_key_len;
424	vcrypto->max_size = max_size;
425	vcrypto->crypto_services = crypto_services;
426	vcrypto->cipher_algo_l = cipher_algo_l;
427	vcrypto->cipher_algo_h = cipher_algo_h;
428	vcrypto->mac_algo_l = mac_algo_l;
429	vcrypto->mac_algo_h = mac_algo_h;
430	vcrypto->hash_algo = hash_algo;
431	vcrypto->aead_algo = aead_algo;
432	vcrypto->akcipher_algo = akcipher_algo;
433
434	dev_info(&vdev->dev,
435		"max_queues: %u, max_cipher_key_len: %u, max_auth_key_len: %u, max_size 0x%llx\n",
436		vcrypto->max_data_queues,
437		vcrypto->max_cipher_key_len,
438		vcrypto->max_auth_key_len,
439		vcrypto->max_size);
440
441	err = virtcrypto_init_vqs(vcrypto);
442	if (err) {
443		dev_err(&vdev->dev, "Failed to initialize vqs.\n");
444		goto free_dev;
445	}
446
447	err = virtcrypto_start_crypto_engines(vcrypto);
448	if (err)
449		goto free_vqs;
450
451	virtio_device_ready(vdev);
452
453	err = virtcrypto_update_status(vcrypto);
454	if (err)
455		goto free_engines;
456
457	return 0;
458
459free_engines:
460	virtcrypto_clear_crypto_engines(vcrypto);
461free_vqs:
462	virtio_reset_device(vdev);
463	virtcrypto_del_vqs(vcrypto);
464free_dev:
465	virtcrypto_devmgr_rm_dev(vcrypto);
466free:
467	kfree(vcrypto);
468	return err;
469}
470
471static void virtcrypto_free_unused_reqs(struct virtio_crypto *vcrypto)
472{
473	struct virtio_crypto_request *vc_req;
474	int i;
475	struct virtqueue *vq;
476
477	for (i = 0; i < vcrypto->max_data_queues; i++) {
478		vq = vcrypto->data_vq[i].vq;
479		while ((vc_req = virtqueue_detach_unused_buf(vq)) != NULL) {
480			kfree(vc_req->req_data);
481			kfree(vc_req->sgs);
482		}
483	}
484}
485
486static void virtcrypto_remove(struct virtio_device *vdev)
487{
488	struct virtio_crypto *vcrypto = vdev->priv;
489
490	dev_info(&vdev->dev, "Start virtcrypto_remove.\n");
491
492	if (virtcrypto_dev_started(vcrypto))
493		virtcrypto_dev_stop(vcrypto);
494	virtio_reset_device(vdev);
495	virtcrypto_free_unused_reqs(vcrypto);
496	virtcrypto_clear_crypto_engines(vcrypto);
497	virtcrypto_del_vqs(vcrypto);
498	virtcrypto_devmgr_rm_dev(vcrypto);
499	kfree(vcrypto);
500}
501
502static void virtcrypto_config_changed(struct virtio_device *vdev)
503{
504	struct virtio_crypto *vcrypto = vdev->priv;
505
506	virtcrypto_update_status(vcrypto);
507}
508
509#ifdef CONFIG_PM_SLEEP
510static int virtcrypto_freeze(struct virtio_device *vdev)
511{
512	struct virtio_crypto *vcrypto = vdev->priv;
513
514	virtio_reset_device(vdev);
515	virtcrypto_free_unused_reqs(vcrypto);
516	if (virtcrypto_dev_started(vcrypto))
517		virtcrypto_dev_stop(vcrypto);
518
519	virtcrypto_clear_crypto_engines(vcrypto);
520	virtcrypto_del_vqs(vcrypto);
521	return 0;
522}
523
524static int virtcrypto_restore(struct virtio_device *vdev)
525{
526	struct virtio_crypto *vcrypto = vdev->priv;
527	int err;
528
529	err = virtcrypto_init_vqs(vcrypto);
530	if (err)
531		return err;
532
533	err = virtcrypto_start_crypto_engines(vcrypto);
534	if (err)
535		goto free_vqs;
536
537	virtio_device_ready(vdev);
538
539	err = virtcrypto_dev_start(vcrypto);
540	if (err) {
541		dev_err(&vdev->dev, "Failed to start virtio crypto device.\n");
542		goto free_engines;
543	}
544
545	return 0;
546
547free_engines:
548	virtcrypto_clear_crypto_engines(vcrypto);
549free_vqs:
550	virtio_reset_device(vdev);
551	virtcrypto_del_vqs(vcrypto);
552	return err;
553}
554#endif
555
556static const unsigned int features[] = {
557	/* none */
558};
559
560static const struct virtio_device_id id_table[] = {
561	{ VIRTIO_ID_CRYPTO, VIRTIO_DEV_ANY_ID },
562	{ 0 },
563};
564
565static struct virtio_driver virtio_crypto_driver = {
566	.driver.name         = KBUILD_MODNAME,
567	.driver.owner        = THIS_MODULE,
568	.feature_table       = features,
569	.feature_table_size  = ARRAY_SIZE(features),
570	.id_table            = id_table,
571	.probe               = virtcrypto_probe,
572	.remove              = virtcrypto_remove,
573	.config_changed = virtcrypto_config_changed,
574#ifdef CONFIG_PM_SLEEP
575	.freeze = virtcrypto_freeze,
576	.restore = virtcrypto_restore,
577#endif
578};
579
580module_virtio_driver(virtio_crypto_driver);
581
582MODULE_DEVICE_TABLE(virtio, id_table);
583MODULE_DESCRIPTION("virtio crypto device driver");
584MODULE_LICENSE("GPL");
585MODULE_AUTHOR("Gonglei <arei.gonglei@huawei.com>");