1 /* Driver for Virtio crypto device.
3 * Copyright 2016 HUAWEI TECHNOLOGIES CO., LTD.
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.
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.
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/>.
19 #include <linux/err.h>
20 #include <linux/module.h>
21 #include <linux/virtio_config.h>
22 #include <linux/cpu.h>
24 #include <uapi/linux/virtio_crypto.h>
25 #include "virtio_crypto_common.h"
29 virtcrypto_clear_request(struct virtio_crypto_request
*vc_req
)
32 kzfree(vc_req
->req_data
);
37 static void virtcrypto_dataq_callback(struct virtqueue
*vq
)
39 struct virtio_crypto
*vcrypto
= vq
->vdev
->priv
;
40 struct virtio_crypto_request
*vc_req
;
43 unsigned int qid
= vq
->index
;
45 spin_lock_irqsave(&vcrypto
->data_vq
[qid
].lock
, flags
);
47 virtqueue_disable_cb(vq
);
48 while ((vc_req
= virtqueue_get_buf(vq
, &len
)) != NULL
) {
49 spin_unlock_irqrestore(
50 &vcrypto
->data_vq
[qid
].lock
, flags
);
52 vc_req
->alg_cb(vc_req
, len
);
54 &vcrypto
->data_vq
[qid
].lock
, flags
);
56 } while (!virtqueue_enable_cb(vq
));
57 spin_unlock_irqrestore(&vcrypto
->data_vq
[qid
].lock
, flags
);
60 static int virtcrypto_find_vqs(struct virtio_crypto
*vi
)
62 vq_callback_t
**callbacks
;
63 struct virtqueue
**vqs
;
67 struct device
*dev
= &vi
->vdev
->dev
;
70 * We expect 1 data virtqueue, followed by
71 * possible N-1 data queues used in multiqueue mode,
72 * followed by control vq.
74 total_vqs
= vi
->max_data_queues
+ 1;
76 /* Allocate space for find_vqs parameters */
77 vqs
= kcalloc(total_vqs
, sizeof(*vqs
), GFP_KERNEL
);
80 callbacks
= kcalloc(total_vqs
, sizeof(*callbacks
), GFP_KERNEL
);
83 names
= kcalloc(total_vqs
, sizeof(*names
), GFP_KERNEL
);
87 /* Parameters for control virtqueue */
88 callbacks
[total_vqs
- 1] = NULL
;
89 names
[total_vqs
- 1] = "controlq";
91 /* Allocate/initialize parameters for data virtqueues */
92 for (i
= 0; i
< vi
->max_data_queues
; i
++) {
93 callbacks
[i
] = virtcrypto_dataq_callback
;
94 snprintf(vi
->data_vq
[i
].name
, sizeof(vi
->data_vq
[i
].name
),
96 names
[i
] = vi
->data_vq
[i
].name
;
99 ret
= virtio_find_vqs(vi
->vdev
, total_vqs
, vqs
, callbacks
, names
, NULL
);
103 vi
->ctrl_vq
= vqs
[total_vqs
- 1];
105 for (i
= 0; i
< vi
->max_data_queues
; i
++) {
106 spin_lock_init(&vi
->data_vq
[i
].lock
);
107 vi
->data_vq
[i
].vq
= vqs
[i
];
108 /* Initialize crypto engine */
109 vi
->data_vq
[i
].engine
= crypto_engine_alloc_init(dev
, 1);
110 if (!vi
->data_vq
[i
].engine
) {
133 static int virtcrypto_alloc_queues(struct virtio_crypto
*vi
)
135 vi
->data_vq
= kcalloc(vi
->max_data_queues
, sizeof(*vi
->data_vq
),
143 static void virtcrypto_clean_affinity(struct virtio_crypto
*vi
, long hcpu
)
147 if (vi
->affinity_hint_set
) {
148 for (i
= 0; i
< vi
->max_data_queues
; i
++)
149 virtqueue_set_affinity(vi
->data_vq
[i
].vq
, -1);
151 vi
->affinity_hint_set
= false;
155 static void virtcrypto_set_affinity(struct virtio_crypto
*vcrypto
)
161 * In single queue mode, we don't set the cpu affinity.
163 if (vcrypto
->curr_queue
== 1 || vcrypto
->max_data_queues
== 1) {
164 virtcrypto_clean_affinity(vcrypto
, -1);
169 * In multiqueue mode, we let the queue to be private to one cpu
170 * by setting the affinity hint to eliminate the contention.
172 * TODO: adds cpu hotplug support by register cpu notifier.
175 for_each_online_cpu(cpu
) {
176 virtqueue_set_affinity(vcrypto
->data_vq
[i
].vq
, cpu
);
177 if (++i
>= vcrypto
->max_data_queues
)
181 vcrypto
->affinity_hint_set
= true;
184 static void virtcrypto_free_queues(struct virtio_crypto
*vi
)
189 static int virtcrypto_init_vqs(struct virtio_crypto
*vi
)
193 /* Allocate send & receive queues */
194 ret
= virtcrypto_alloc_queues(vi
);
198 ret
= virtcrypto_find_vqs(vi
);
203 virtcrypto_set_affinity(vi
);
209 virtcrypto_free_queues(vi
);
214 static int virtcrypto_update_status(struct virtio_crypto
*vcrypto
)
219 virtio_cread(vcrypto
->vdev
,
220 struct virtio_crypto_config
, status
, &status
);
223 * Unknown status bits would be a host error and the driver
224 * should consider the device to be broken.
226 if (status
& (~VIRTIO_CRYPTO_S_HW_READY
)) {
227 dev_warn(&vcrypto
->vdev
->dev
,
228 "Unknown status bits: 0x%x\n", status
);
230 virtio_break_device(vcrypto
->vdev
);
234 if (vcrypto
->status
== status
)
237 vcrypto
->status
= status
;
239 if (vcrypto
->status
& VIRTIO_CRYPTO_S_HW_READY
) {
240 err
= virtcrypto_dev_start(vcrypto
);
242 dev_err(&vcrypto
->vdev
->dev
,
243 "Failed to start virtio crypto device.\n");
247 dev_info(&vcrypto
->vdev
->dev
, "Accelerator device is ready\n");
249 virtcrypto_dev_stop(vcrypto
);
250 dev_info(&vcrypto
->vdev
->dev
, "Accelerator is not ready\n");
256 static int virtcrypto_start_crypto_engines(struct virtio_crypto
*vcrypto
)
261 for (i
= 0; i
< vcrypto
->max_data_queues
; i
++) {
262 if (vcrypto
->data_vq
[i
].engine
) {
263 ret
= crypto_engine_start(vcrypto
->data_vq
[i
].engine
);
273 if (vcrypto
->data_vq
[i
].engine
)
274 crypto_engine_exit(vcrypto
->data_vq
[i
].engine
);
279 static void virtcrypto_clear_crypto_engines(struct virtio_crypto
*vcrypto
)
283 for (i
= 0; i
< vcrypto
->max_data_queues
; i
++)
284 if (vcrypto
->data_vq
[i
].engine
)
285 crypto_engine_exit(vcrypto
->data_vq
[i
].engine
);
288 static void virtcrypto_del_vqs(struct virtio_crypto
*vcrypto
)
290 struct virtio_device
*vdev
= vcrypto
->vdev
;
292 virtcrypto_clean_affinity(vcrypto
, -1);
294 vdev
->config
->del_vqs(vdev
);
296 virtcrypto_free_queues(vcrypto
);
299 static int virtcrypto_probe(struct virtio_device
*vdev
)
302 struct virtio_crypto
*vcrypto
;
303 u32 max_data_queues
= 0, max_cipher_key_len
= 0;
304 u32 max_auth_key_len
= 0;
307 if (!virtio_has_feature(vdev
, VIRTIO_F_VERSION_1
))
310 if (!vdev
->config
->get
) {
311 dev_err(&vdev
->dev
, "%s failure: config access disabled\n",
316 if (num_possible_nodes() > 1 && dev_to_node(&vdev
->dev
) < 0) {
318 * If the accelerator is connected to a node with no memory
319 * there is no point in using the accelerator since the remote
320 * memory transaction will be very slow.
322 dev_err(&vdev
->dev
, "Invalid NUMA configuration.\n");
326 vcrypto
= kzalloc_node(sizeof(*vcrypto
), GFP_KERNEL
,
327 dev_to_node(&vdev
->dev
));
331 virtio_cread(vdev
, struct virtio_crypto_config
,
332 max_dataqueues
, &max_data_queues
);
333 if (max_data_queues
< 1)
336 virtio_cread(vdev
, struct virtio_crypto_config
,
337 max_cipher_key_len
, &max_cipher_key_len
);
338 virtio_cread(vdev
, struct virtio_crypto_config
,
339 max_auth_key_len
, &max_auth_key_len
);
340 virtio_cread(vdev
, struct virtio_crypto_config
,
341 max_size
, &max_size
);
343 /* Add virtio crypto device to global table */
344 err
= virtcrypto_devmgr_add_dev(vcrypto
);
346 dev_err(&vdev
->dev
, "Failed to add new virtio crypto device.\n");
349 vcrypto
->owner
= THIS_MODULE
;
350 vcrypto
= vdev
->priv
= vcrypto
;
351 vcrypto
->vdev
= vdev
;
353 spin_lock_init(&vcrypto
->ctrl_lock
);
355 /* Use single data queue as default */
356 vcrypto
->curr_queue
= 1;
357 vcrypto
->max_data_queues
= max_data_queues
;
358 vcrypto
->max_cipher_key_len
= max_cipher_key_len
;
359 vcrypto
->max_auth_key_len
= max_auth_key_len
;
360 vcrypto
->max_size
= max_size
;
363 "max_queues: %u, max_cipher_key_len: %u, max_auth_key_len: %u, max_size 0x%llx\n",
364 vcrypto
->max_data_queues
,
365 vcrypto
->max_cipher_key_len
,
366 vcrypto
->max_auth_key_len
,
369 err
= virtcrypto_init_vqs(vcrypto
);
371 dev_err(&vdev
->dev
, "Failed to initialize vqs.\n");
375 err
= virtcrypto_start_crypto_engines(vcrypto
);
379 virtio_device_ready(vdev
);
381 err
= virtcrypto_update_status(vcrypto
);
388 virtcrypto_clear_crypto_engines(vcrypto
);
390 vcrypto
->vdev
->config
->reset(vdev
);
391 virtcrypto_del_vqs(vcrypto
);
393 virtcrypto_devmgr_rm_dev(vcrypto
);
399 static void virtcrypto_free_unused_reqs(struct virtio_crypto
*vcrypto
)
401 struct virtio_crypto_request
*vc_req
;
403 struct virtqueue
*vq
;
405 for (i
= 0; i
< vcrypto
->max_data_queues
; i
++) {
406 vq
= vcrypto
->data_vq
[i
].vq
;
407 while ((vc_req
= virtqueue_detach_unused_buf(vq
)) != NULL
) {
408 kfree(vc_req
->req_data
);
414 static void virtcrypto_remove(struct virtio_device
*vdev
)
416 struct virtio_crypto
*vcrypto
= vdev
->priv
;
418 dev_info(&vdev
->dev
, "Start virtcrypto_remove.\n");
420 if (virtcrypto_dev_started(vcrypto
))
421 virtcrypto_dev_stop(vcrypto
);
422 vdev
->config
->reset(vdev
);
423 virtcrypto_free_unused_reqs(vcrypto
);
424 virtcrypto_clear_crypto_engines(vcrypto
);
425 virtcrypto_del_vqs(vcrypto
);
426 virtcrypto_devmgr_rm_dev(vcrypto
);
430 static void virtcrypto_config_changed(struct virtio_device
*vdev
)
432 struct virtio_crypto
*vcrypto
= vdev
->priv
;
434 virtcrypto_update_status(vcrypto
);
437 #ifdef CONFIG_PM_SLEEP
438 static int virtcrypto_freeze(struct virtio_device
*vdev
)
440 struct virtio_crypto
*vcrypto
= vdev
->priv
;
442 vdev
->config
->reset(vdev
);
443 virtcrypto_free_unused_reqs(vcrypto
);
444 if (virtcrypto_dev_started(vcrypto
))
445 virtcrypto_dev_stop(vcrypto
);
447 virtcrypto_clear_crypto_engines(vcrypto
);
448 virtcrypto_del_vqs(vcrypto
);
452 static int virtcrypto_restore(struct virtio_device
*vdev
)
454 struct virtio_crypto
*vcrypto
= vdev
->priv
;
457 err
= virtcrypto_init_vqs(vcrypto
);
461 err
= virtcrypto_start_crypto_engines(vcrypto
);
465 virtio_device_ready(vdev
);
467 err
= virtcrypto_dev_start(vcrypto
);
469 dev_err(&vdev
->dev
, "Failed to start virtio crypto device.\n");
476 virtcrypto_clear_crypto_engines(vcrypto
);
478 vcrypto
->vdev
->config
->reset(vdev
);
479 virtcrypto_del_vqs(vcrypto
);
484 static unsigned int features
[] = {
488 static struct virtio_device_id id_table
[] = {
489 { VIRTIO_ID_CRYPTO
, VIRTIO_DEV_ANY_ID
},
493 static struct virtio_driver virtio_crypto_driver
= {
494 .driver
.name
= KBUILD_MODNAME
,
495 .driver
.owner
= THIS_MODULE
,
496 .feature_table
= features
,
497 .feature_table_size
= ARRAY_SIZE(features
),
498 .id_table
= id_table
,
499 .probe
= virtcrypto_probe
,
500 .remove
= virtcrypto_remove
,
501 .config_changed
= virtcrypto_config_changed
,
502 #ifdef CONFIG_PM_SLEEP
503 .freeze
= virtcrypto_freeze
,
504 .restore
= virtcrypto_restore
,
508 module_virtio_driver(virtio_crypto_driver
);
510 MODULE_DEVICE_TABLE(virtio
, id_table
);
511 MODULE_DESCRIPTION("virtio crypto device driver");
512 MODULE_LICENSE("GPL");
513 MODULE_AUTHOR("Gonglei <arei.gonglei@huawei.com>");