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