1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2021 Intel Corporation. All rights rsvd. */
3
4 #include <linux/init.h>
5 #include <linux/kernel.h>
6 #include <linux/module.h>
7 #include <linux/pci.h>
8 #include <linux/device.h>
9 #include <linux/iommu.h>
10 #include <uapi/linux/idxd.h>
11 #include <linux/highmem.h>
12 #include <linux/sched/smt.h>
13 #include <crypto/internal/acompress.h>
14
15 #include "idxd.h"
16 #include "iaa_crypto.h"
17 #include "iaa_crypto_stats.h"
18
19 #ifdef pr_fmt
20 #undef pr_fmt
21 #endif
22
23 #define pr_fmt(fmt) "idxd: " IDXD_SUBDRIVER_NAME ": " fmt
24
25 #define IAA_ALG_PRIORITY 300
26
27 /* number of iaa instances probed */
28 static unsigned int nr_iaa;
29 static unsigned int nr_cpus;
30 static unsigned int nr_nodes;
31 static unsigned int nr_cpus_per_node;
32
33 /* Number of physical cpus sharing each iaa instance */
34 static unsigned int cpus_per_iaa;
35
36 static struct crypto_comp *deflate_generic_tfm;
37
38 /* Per-cpu lookup table for balanced wqs */
39 static struct wq_table_entry __percpu *wq_table;
40
wq_table_next_wq(int cpu)41 static struct idxd_wq *wq_table_next_wq(int cpu)
42 {
43 struct wq_table_entry *entry = per_cpu_ptr(wq_table, cpu);
44
45 if (++entry->cur_wq >= entry->n_wqs)
46 entry->cur_wq = 0;
47
48 if (!entry->wqs[entry->cur_wq])
49 return NULL;
50
51 pr_debug("%s: returning wq at idx %d (iaa wq %d.%d) from cpu %d\n", __func__,
52 entry->cur_wq, entry->wqs[entry->cur_wq]->idxd->id,
53 entry->wqs[entry->cur_wq]->id, cpu);
54
55 return entry->wqs[entry->cur_wq];
56 }
57
wq_table_add(int cpu,struct idxd_wq * wq)58 static void wq_table_add(int cpu, struct idxd_wq *wq)
59 {
60 struct wq_table_entry *entry = per_cpu_ptr(wq_table, cpu);
61
62 if (WARN_ON(entry->n_wqs == entry->max_wqs))
63 return;
64
65 entry->wqs[entry->n_wqs++] = wq;
66
67 pr_debug("%s: added iaa wq %d.%d to idx %d of cpu %d\n", __func__,
68 entry->wqs[entry->n_wqs - 1]->idxd->id,
69 entry->wqs[entry->n_wqs - 1]->id, entry->n_wqs - 1, cpu);
70 }
71
wq_table_free_entry(int cpu)72 static void wq_table_free_entry(int cpu)
73 {
74 struct wq_table_entry *entry = per_cpu_ptr(wq_table, cpu);
75
76 kfree(entry->wqs);
77 memset(entry, 0, sizeof(*entry));
78 }
79
wq_table_clear_entry(int cpu)80 static void wq_table_clear_entry(int cpu)
81 {
82 struct wq_table_entry *entry = per_cpu_ptr(wq_table, cpu);
83
84 entry->n_wqs = 0;
85 entry->cur_wq = 0;
86 memset(entry->wqs, 0, entry->max_wqs * sizeof(struct idxd_wq *));
87 }
88
89 LIST_HEAD(iaa_devices);
90 DEFINE_MUTEX(iaa_devices_lock);
91
92 /* If enabled, IAA hw crypto algos are registered, unavailable otherwise */
93 static bool iaa_crypto_enabled;
94 static bool iaa_crypto_registered;
95
96 /* Verify results of IAA compress or not */
97 static bool iaa_verify_compress = true;
98
verify_compress_show(struct device_driver * driver,char * buf)99 static ssize_t verify_compress_show(struct device_driver *driver, char *buf)
100 {
101 return sprintf(buf, "%d\n", iaa_verify_compress);
102 }
103
verify_compress_store(struct device_driver * driver,const char * buf,size_t count)104 static ssize_t verify_compress_store(struct device_driver *driver,
105 const char *buf, size_t count)
106 {
107 int ret = -EBUSY;
108
109 mutex_lock(&iaa_devices_lock);
110
111 if (iaa_crypto_enabled)
112 goto out;
113
114 ret = kstrtobool(buf, &iaa_verify_compress);
115 if (ret)
116 goto out;
117
118 ret = count;
119 out:
120 mutex_unlock(&iaa_devices_lock);
121
122 return ret;
123 }
124 static DRIVER_ATTR_RW(verify_compress);
125
126 /*
127 * The iaa crypto driver supports three 'sync' methods determining how
128 * compressions and decompressions are performed:
129 *
130 * - sync: the compression or decompression completes before
131 * returning. This is the mode used by the async crypto
132 * interface when the sync mode is set to 'sync' and by
133 * the sync crypto interface regardless of setting.
134 *
135 * - async: the compression or decompression is submitted and returns
136 * immediately. Completion interrupts are not used so
137 * the caller is responsible for polling the descriptor
138 * for completion. This mode is applicable to only the
139 * async crypto interface and is ignored for anything
140 * else.
141 *
142 * - async_irq: the compression or decompression is submitted and
143 * returns immediately. Completion interrupts are
144 * enabled so the caller can wait for the completion and
145 * yield to other threads. When the compression or
146 * decompression completes, the completion is signaled
147 * and the caller awakened. This mode is applicable to
148 * only the async crypto interface and is ignored for
149 * anything else.
150 *
151 * These modes can be set using the iaa_crypto sync_mode driver
152 * attribute.
153 */
154
155 /* Use async mode */
156 static bool async_mode;
157 /* Use interrupts */
158 static bool use_irq;
159
160 /**
161 * set_iaa_sync_mode - Set IAA sync mode
162 * @name: The name of the sync mode
163 *
164 * Make the IAA sync mode named @name the current sync mode used by
165 * compression/decompression.
166 */
167
set_iaa_sync_mode(const char * name)168 static int set_iaa_sync_mode(const char *name)
169 {
170 int ret = 0;
171
172 if (sysfs_streq(name, "sync")) {
173 async_mode = false;
174 use_irq = false;
175 } else if (sysfs_streq(name, "async")) {
176 async_mode = false;
177 use_irq = false;
178 } else if (sysfs_streq(name, "async_irq")) {
179 async_mode = true;
180 use_irq = true;
181 } else {
182 ret = -EINVAL;
183 }
184
185 return ret;
186 }
187
sync_mode_show(struct device_driver * driver,char * buf)188 static ssize_t sync_mode_show(struct device_driver *driver, char *buf)
189 {
190 int ret = 0;
191
192 if (!async_mode && !use_irq)
193 ret = sprintf(buf, "%s\n", "sync");
194 else if (async_mode && !use_irq)
195 ret = sprintf(buf, "%s\n", "async");
196 else if (async_mode && use_irq)
197 ret = sprintf(buf, "%s\n", "async_irq");
198
199 return ret;
200 }
201
sync_mode_store(struct device_driver * driver,const char * buf,size_t count)202 static ssize_t sync_mode_store(struct device_driver *driver,
203 const char *buf, size_t count)
204 {
205 int ret = -EBUSY;
206
207 mutex_lock(&iaa_devices_lock);
208
209 if (iaa_crypto_enabled)
210 goto out;
211
212 ret = set_iaa_sync_mode(buf);
213 if (ret == 0)
214 ret = count;
215 out:
216 mutex_unlock(&iaa_devices_lock);
217
218 return ret;
219 }
220 static DRIVER_ATTR_RW(sync_mode);
221
222 static struct iaa_compression_mode *iaa_compression_modes[IAA_COMP_MODES_MAX];
223
find_empty_iaa_compression_mode(void)224 static int find_empty_iaa_compression_mode(void)
225 {
226 int i = -EINVAL;
227
228 for (i = 0; i < IAA_COMP_MODES_MAX; i++) {
229 if (iaa_compression_modes[i])
230 continue;
231 break;
232 }
233
234 return i;
235 }
236
find_iaa_compression_mode(const char * name,int * idx)237 static struct iaa_compression_mode *find_iaa_compression_mode(const char *name, int *idx)
238 {
239 struct iaa_compression_mode *mode;
240 int i;
241
242 for (i = 0; i < IAA_COMP_MODES_MAX; i++) {
243 mode = iaa_compression_modes[i];
244 if (!mode)
245 continue;
246
247 if (!strcmp(mode->name, name)) {
248 *idx = i;
249 return iaa_compression_modes[i];
250 }
251 }
252
253 return NULL;
254 }
255
free_iaa_compression_mode(struct iaa_compression_mode * mode)256 static void free_iaa_compression_mode(struct iaa_compression_mode *mode)
257 {
258 kfree(mode->name);
259 kfree(mode->ll_table);
260 kfree(mode->d_table);
261
262 kfree(mode);
263 }
264
265 /*
266 * IAA Compression modes are defined by an ll_table and a d_table.
267 * These tables are typically generated and captured using statistics
268 * collected from running actual compress/decompress workloads.
269 *
270 * A module or other kernel code can add and remove compression modes
271 * with a given name using the exported @add_iaa_compression_mode()
272 * and @remove_iaa_compression_mode functions.
273 *
274 * When a new compression mode is added, the tables are saved in a
275 * global compression mode list. When IAA devices are added, a
276 * per-IAA device dma mapping is created for each IAA device, for each
277 * compression mode. These are the tables used to do the actual
278 * compression/deccompression and are unmapped if/when the devices are
279 * removed. Currently, compression modes must be added before any
280 * device is added, and removed after all devices have been removed.
281 */
282
283 /**
284 * remove_iaa_compression_mode - Remove an IAA compression mode
285 * @name: The name the compression mode will be known as
286 *
287 * Remove the IAA compression mode named @name.
288 */
remove_iaa_compression_mode(const char * name)289 void remove_iaa_compression_mode(const char *name)
290 {
291 struct iaa_compression_mode *mode;
292 int idx;
293
294 mutex_lock(&iaa_devices_lock);
295
296 if (!list_empty(&iaa_devices))
297 goto out;
298
299 mode = find_iaa_compression_mode(name, &idx);
300 if (mode) {
301 free_iaa_compression_mode(mode);
302 iaa_compression_modes[idx] = NULL;
303 }
304 out:
305 mutex_unlock(&iaa_devices_lock);
306 }
307 EXPORT_SYMBOL_GPL(remove_iaa_compression_mode);
308
309 /**
310 * add_iaa_compression_mode - Add an IAA compression mode
311 * @name: The name the compression mode will be known as
312 * @ll_table: The ll table
313 * @ll_table_size: The ll table size in bytes
314 * @d_table: The d table
315 * @d_table_size: The d table size in bytes
316 * @init: Optional callback function to init the compression mode data
317 * @free: Optional callback function to free the compression mode data
318 *
319 * Add a new IAA compression mode named @name.
320 *
321 * Returns 0 if successful, errcode otherwise.
322 */
add_iaa_compression_mode(const char * name,const u32 * ll_table,int ll_table_size,const u32 * d_table,int d_table_size,iaa_dev_comp_init_fn_t init,iaa_dev_comp_free_fn_t free)323 int add_iaa_compression_mode(const char *name,
324 const u32 *ll_table,
325 int ll_table_size,
326 const u32 *d_table,
327 int d_table_size,
328 iaa_dev_comp_init_fn_t init,
329 iaa_dev_comp_free_fn_t free)
330 {
331 struct iaa_compression_mode *mode;
332 int idx, ret = -ENOMEM;
333
334 mutex_lock(&iaa_devices_lock);
335
336 if (!list_empty(&iaa_devices)) {
337 ret = -EBUSY;
338 goto out;
339 }
340
341 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
342 if (!mode)
343 goto out;
344
345 mode->name = kstrdup(name, GFP_KERNEL);
346 if (!mode->name)
347 goto free;
348
349 if (ll_table) {
350 mode->ll_table = kmemdup(ll_table, ll_table_size, GFP_KERNEL);
351 if (!mode->ll_table)
352 goto free;
353 mode->ll_table_size = ll_table_size;
354 }
355
356 if (d_table) {
357 mode->d_table = kmemdup(d_table, d_table_size, GFP_KERNEL);
358 if (!mode->d_table)
359 goto free;
360 mode->d_table_size = d_table_size;
361 }
362
363 mode->init = init;
364 mode->free = free;
365
366 idx = find_empty_iaa_compression_mode();
367 if (idx < 0)
368 goto free;
369
370 pr_debug("IAA compression mode %s added at idx %d\n",
371 mode->name, idx);
372
373 iaa_compression_modes[idx] = mode;
374
375 ret = 0;
376 out:
377 mutex_unlock(&iaa_devices_lock);
378
379 return ret;
380 free:
381 free_iaa_compression_mode(mode);
382 goto out;
383 }
384 EXPORT_SYMBOL_GPL(add_iaa_compression_mode);
385
386 static struct iaa_device_compression_mode *
get_iaa_device_compression_mode(struct iaa_device * iaa_device,int idx)387 get_iaa_device_compression_mode(struct iaa_device *iaa_device, int idx)
388 {
389 return iaa_device->compression_modes[idx];
390 }
391
free_device_compression_mode(struct iaa_device * iaa_device,struct iaa_device_compression_mode * device_mode)392 static void free_device_compression_mode(struct iaa_device *iaa_device,
393 struct iaa_device_compression_mode *device_mode)
394 {
395 size_t size = sizeof(struct aecs_comp_table_record) + IAA_AECS_ALIGN;
396 struct device *dev = &iaa_device->idxd->pdev->dev;
397
398 kfree(device_mode->name);
399
400 if (device_mode->aecs_comp_table)
401 dma_free_coherent(dev, size, device_mode->aecs_comp_table,
402 device_mode->aecs_comp_table_dma_addr);
403 kfree(device_mode);
404 }
405
406 #define IDXD_OP_FLAG_AECS_RW_TGLS 0x400000
407 #define IAX_AECS_DEFAULT_FLAG (IDXD_OP_FLAG_CRAV | IDXD_OP_FLAG_RCR | IDXD_OP_FLAG_CC)
408 #define IAX_AECS_COMPRESS_FLAG (IAX_AECS_DEFAULT_FLAG | IDXD_OP_FLAG_RD_SRC2_AECS)
409 #define IAX_AECS_DECOMPRESS_FLAG (IAX_AECS_DEFAULT_FLAG | IDXD_OP_FLAG_RD_SRC2_AECS)
410 #define IAX_AECS_GEN_FLAG (IAX_AECS_DEFAULT_FLAG | \
411 IDXD_OP_FLAG_WR_SRC2_AECS_COMP | \
412 IDXD_OP_FLAG_AECS_RW_TGLS)
413
414 static int check_completion(struct device *dev,
415 struct iax_completion_record *comp,
416 bool compress,
417 bool only_once);
418
init_device_compression_mode(struct iaa_device * iaa_device,struct iaa_compression_mode * mode,int idx,struct idxd_wq * wq)419 static int init_device_compression_mode(struct iaa_device *iaa_device,
420 struct iaa_compression_mode *mode,
421 int idx, struct idxd_wq *wq)
422 {
423 size_t size = sizeof(struct aecs_comp_table_record) + IAA_AECS_ALIGN;
424 struct device *dev = &iaa_device->idxd->pdev->dev;
425 struct iaa_device_compression_mode *device_mode;
426 int ret = -ENOMEM;
427
428 device_mode = kzalloc(sizeof(*device_mode), GFP_KERNEL);
429 if (!device_mode)
430 return -ENOMEM;
431
432 device_mode->name = kstrdup(mode->name, GFP_KERNEL);
433 if (!device_mode->name)
434 goto free;
435
436 device_mode->aecs_comp_table = dma_alloc_coherent(dev, size,
437 &device_mode->aecs_comp_table_dma_addr, GFP_KERNEL);
438 if (!device_mode->aecs_comp_table)
439 goto free;
440
441 /* Add Huffman table to aecs */
442 memset(device_mode->aecs_comp_table, 0, sizeof(*device_mode->aecs_comp_table));
443 memcpy(device_mode->aecs_comp_table->ll_sym, mode->ll_table, mode->ll_table_size);
444 memcpy(device_mode->aecs_comp_table->d_sym, mode->d_table, mode->d_table_size);
445
446 if (mode->init) {
447 ret = mode->init(device_mode);
448 if (ret)
449 goto free;
450 }
451
452 /* mode index should match iaa_compression_modes idx */
453 iaa_device->compression_modes[idx] = device_mode;
454
455 pr_debug("IAA %s compression mode initialized for iaa device %d\n",
456 mode->name, iaa_device->idxd->id);
457
458 ret = 0;
459 out:
460 return ret;
461 free:
462 pr_debug("IAA %s compression mode initialization failed for iaa device %d\n",
463 mode->name, iaa_device->idxd->id);
464
465 free_device_compression_mode(iaa_device, device_mode);
466 goto out;
467 }
468
init_device_compression_modes(struct iaa_device * iaa_device,struct idxd_wq * wq)469 static int init_device_compression_modes(struct iaa_device *iaa_device,
470 struct idxd_wq *wq)
471 {
472 struct iaa_compression_mode *mode;
473 int i, ret = 0;
474
475 for (i = 0; i < IAA_COMP_MODES_MAX; i++) {
476 mode = iaa_compression_modes[i];
477 if (!mode)
478 continue;
479
480 ret = init_device_compression_mode(iaa_device, mode, i, wq);
481 if (ret)
482 break;
483 }
484
485 return ret;
486 }
487
remove_device_compression_modes(struct iaa_device * iaa_device)488 static void remove_device_compression_modes(struct iaa_device *iaa_device)
489 {
490 struct iaa_device_compression_mode *device_mode;
491 int i;
492
493 for (i = 0; i < IAA_COMP_MODES_MAX; i++) {
494 device_mode = iaa_device->compression_modes[i];
495 if (!device_mode)
496 continue;
497
498 if (iaa_compression_modes[i]->free)
499 iaa_compression_modes[i]->free(device_mode);
500 free_device_compression_mode(iaa_device, device_mode);
501 iaa_device->compression_modes[i] = NULL;
502 }
503 }
504
iaa_device_alloc(void)505 static struct iaa_device *iaa_device_alloc(void)
506 {
507 struct iaa_device *iaa_device;
508
509 iaa_device = kzalloc(sizeof(*iaa_device), GFP_KERNEL);
510 if (!iaa_device)
511 return NULL;
512
513 INIT_LIST_HEAD(&iaa_device->wqs);
514
515 return iaa_device;
516 }
517
iaa_has_wq(struct iaa_device * iaa_device,struct idxd_wq * wq)518 static bool iaa_has_wq(struct iaa_device *iaa_device, struct idxd_wq *wq)
519 {
520 struct iaa_wq *iaa_wq;
521
522 list_for_each_entry(iaa_wq, &iaa_device->wqs, list) {
523 if (iaa_wq->wq == wq)
524 return true;
525 }
526
527 return false;
528 }
529
add_iaa_device(struct idxd_device * idxd)530 static struct iaa_device *add_iaa_device(struct idxd_device *idxd)
531 {
532 struct iaa_device *iaa_device;
533
534 iaa_device = iaa_device_alloc();
535 if (!iaa_device)
536 return NULL;
537
538 iaa_device->idxd = idxd;
539
540 list_add_tail(&iaa_device->list, &iaa_devices);
541
542 nr_iaa++;
543
544 return iaa_device;
545 }
546
init_iaa_device(struct iaa_device * iaa_device,struct iaa_wq * iaa_wq)547 static int init_iaa_device(struct iaa_device *iaa_device, struct iaa_wq *iaa_wq)
548 {
549 int ret = 0;
550
551 ret = init_device_compression_modes(iaa_device, iaa_wq->wq);
552 if (ret)
553 return ret;
554
555 return ret;
556 }
557
del_iaa_device(struct iaa_device * iaa_device)558 static void del_iaa_device(struct iaa_device *iaa_device)
559 {
560 list_del(&iaa_device->list);
561
562 nr_iaa--;
563 }
564
add_iaa_wq(struct iaa_device * iaa_device,struct idxd_wq * wq,struct iaa_wq ** new_wq)565 static int add_iaa_wq(struct iaa_device *iaa_device, struct idxd_wq *wq,
566 struct iaa_wq **new_wq)
567 {
568 struct idxd_device *idxd = iaa_device->idxd;
569 struct pci_dev *pdev = idxd->pdev;
570 struct device *dev = &pdev->dev;
571 struct iaa_wq *iaa_wq;
572
573 iaa_wq = kzalloc(sizeof(*iaa_wq), GFP_KERNEL);
574 if (!iaa_wq)
575 return -ENOMEM;
576
577 iaa_wq->wq = wq;
578 iaa_wq->iaa_device = iaa_device;
579 idxd_wq_set_private(wq, iaa_wq);
580
581 list_add_tail(&iaa_wq->list, &iaa_device->wqs);
582
583 iaa_device->n_wq++;
584
585 if (new_wq)
586 *new_wq = iaa_wq;
587
588 dev_dbg(dev, "added wq %d to iaa device %d, n_wq %d\n",
589 wq->id, iaa_device->idxd->id, iaa_device->n_wq);
590
591 return 0;
592 }
593
del_iaa_wq(struct iaa_device * iaa_device,struct idxd_wq * wq)594 static void del_iaa_wq(struct iaa_device *iaa_device, struct idxd_wq *wq)
595 {
596 struct idxd_device *idxd = iaa_device->idxd;
597 struct pci_dev *pdev = idxd->pdev;
598 struct device *dev = &pdev->dev;
599 struct iaa_wq *iaa_wq;
600
601 list_for_each_entry(iaa_wq, &iaa_device->wqs, list) {
602 if (iaa_wq->wq == wq) {
603 list_del(&iaa_wq->list);
604 iaa_device->n_wq--;
605
606 dev_dbg(dev, "removed wq %d from iaa_device %d, n_wq %d, nr_iaa %d\n",
607 wq->id, iaa_device->idxd->id,
608 iaa_device->n_wq, nr_iaa);
609
610 if (iaa_device->n_wq == 0)
611 del_iaa_device(iaa_device);
612 break;
613 }
614 }
615 }
616
clear_wq_table(void)617 static void clear_wq_table(void)
618 {
619 int cpu;
620
621 for (cpu = 0; cpu < nr_cpus; cpu++)
622 wq_table_clear_entry(cpu);
623
624 pr_debug("cleared wq table\n");
625 }
626
free_iaa_device(struct iaa_device * iaa_device)627 static void free_iaa_device(struct iaa_device *iaa_device)
628 {
629 if (!iaa_device)
630 return;
631
632 remove_device_compression_modes(iaa_device);
633 kfree(iaa_device);
634 }
635
__free_iaa_wq(struct iaa_wq * iaa_wq)636 static void __free_iaa_wq(struct iaa_wq *iaa_wq)
637 {
638 struct iaa_device *iaa_device;
639
640 if (!iaa_wq)
641 return;
642
643 iaa_device = iaa_wq->iaa_device;
644 if (iaa_device->n_wq == 0)
645 free_iaa_device(iaa_wq->iaa_device);
646 }
647
free_iaa_wq(struct iaa_wq * iaa_wq)648 static void free_iaa_wq(struct iaa_wq *iaa_wq)
649 {
650 struct idxd_wq *wq;
651
652 __free_iaa_wq(iaa_wq);
653
654 wq = iaa_wq->wq;
655
656 kfree(iaa_wq);
657 idxd_wq_set_private(wq, NULL);
658 }
659
iaa_wq_get(struct idxd_wq * wq)660 static int iaa_wq_get(struct idxd_wq *wq)
661 {
662 struct idxd_device *idxd = wq->idxd;
663 struct iaa_wq *iaa_wq;
664 int ret = 0;
665
666 spin_lock(&idxd->dev_lock);
667 iaa_wq = idxd_wq_get_private(wq);
668 if (iaa_wq && !iaa_wq->remove) {
669 iaa_wq->ref++;
670 idxd_wq_get(wq);
671 } else {
672 ret = -ENODEV;
673 }
674 spin_unlock(&idxd->dev_lock);
675
676 return ret;
677 }
678
iaa_wq_put(struct idxd_wq * wq)679 static int iaa_wq_put(struct idxd_wq *wq)
680 {
681 struct idxd_device *idxd = wq->idxd;
682 struct iaa_wq *iaa_wq;
683 bool free = false;
684 int ret = 0;
685
686 spin_lock(&idxd->dev_lock);
687 iaa_wq = idxd_wq_get_private(wq);
688 if (iaa_wq) {
689 iaa_wq->ref--;
690 if (iaa_wq->ref == 0 && iaa_wq->remove) {
691 idxd_wq_set_private(wq, NULL);
692 free = true;
693 }
694 idxd_wq_put(wq);
695 } else {
696 ret = -ENODEV;
697 }
698 spin_unlock(&idxd->dev_lock);
699 if (free) {
700 __free_iaa_wq(iaa_wq);
701 kfree(iaa_wq);
702 }
703
704 return ret;
705 }
706
free_wq_table(void)707 static void free_wq_table(void)
708 {
709 int cpu;
710
711 for (cpu = 0; cpu < nr_cpus; cpu++)
712 wq_table_free_entry(cpu);
713
714 free_percpu(wq_table);
715
716 pr_debug("freed wq table\n");
717 }
718
alloc_wq_table(int max_wqs)719 static int alloc_wq_table(int max_wqs)
720 {
721 struct wq_table_entry *entry;
722 int cpu;
723
724 wq_table = alloc_percpu(struct wq_table_entry);
725 if (!wq_table)
726 return -ENOMEM;
727
728 for (cpu = 0; cpu < nr_cpus; cpu++) {
729 entry = per_cpu_ptr(wq_table, cpu);
730 entry->wqs = kcalloc(max_wqs, sizeof(struct wq *), GFP_KERNEL);
731 if (!entry->wqs) {
732 free_wq_table();
733 return -ENOMEM;
734 }
735
736 entry->max_wqs = max_wqs;
737 }
738
739 pr_debug("initialized wq table\n");
740
741 return 0;
742 }
743
save_iaa_wq(struct idxd_wq * wq)744 static int save_iaa_wq(struct idxd_wq *wq)
745 {
746 struct iaa_device *iaa_device, *found = NULL;
747 struct idxd_device *idxd;
748 struct pci_dev *pdev;
749 struct device *dev;
750 int ret = 0;
751
752 list_for_each_entry(iaa_device, &iaa_devices, list) {
753 if (iaa_device->idxd == wq->idxd) {
754 idxd = iaa_device->idxd;
755 pdev = idxd->pdev;
756 dev = &pdev->dev;
757 /*
758 * Check to see that we don't already have this wq.
759 * Shouldn't happen but we don't control probing.
760 */
761 if (iaa_has_wq(iaa_device, wq)) {
762 dev_dbg(dev, "same wq probed multiple times for iaa_device %p\n",
763 iaa_device);
764 goto out;
765 }
766
767 found = iaa_device;
768
769 ret = add_iaa_wq(iaa_device, wq, NULL);
770 if (ret)
771 goto out;
772
773 break;
774 }
775 }
776
777 if (!found) {
778 struct iaa_device *new_device;
779 struct iaa_wq *new_wq;
780
781 new_device = add_iaa_device(wq->idxd);
782 if (!new_device) {
783 ret = -ENOMEM;
784 goto out;
785 }
786
787 ret = add_iaa_wq(new_device, wq, &new_wq);
788 if (ret) {
789 del_iaa_device(new_device);
790 free_iaa_device(new_device);
791 goto out;
792 }
793
794 ret = init_iaa_device(new_device, new_wq);
795 if (ret) {
796 del_iaa_wq(new_device, new_wq->wq);
797 del_iaa_device(new_device);
798 free_iaa_wq(new_wq);
799 goto out;
800 }
801 }
802
803 if (WARN_ON(nr_iaa == 0))
804 return -EINVAL;
805
806 cpus_per_iaa = (nr_nodes * nr_cpus_per_node) / nr_iaa;
807 if (!cpus_per_iaa)
808 cpus_per_iaa = 1;
809 out:
810 return 0;
811 }
812
remove_iaa_wq(struct idxd_wq * wq)813 static void remove_iaa_wq(struct idxd_wq *wq)
814 {
815 struct iaa_device *iaa_device;
816
817 list_for_each_entry(iaa_device, &iaa_devices, list) {
818 if (iaa_has_wq(iaa_device, wq)) {
819 del_iaa_wq(iaa_device, wq);
820 break;
821 }
822 }
823
824 if (nr_iaa) {
825 cpus_per_iaa = (nr_nodes * nr_cpus_per_node) / nr_iaa;
826 if (!cpus_per_iaa)
827 cpus_per_iaa = 1;
828 } else
829 cpus_per_iaa = 1;
830 }
831
wq_table_add_wqs(int iaa,int cpu)832 static int wq_table_add_wqs(int iaa, int cpu)
833 {
834 struct iaa_device *iaa_device, *found_device = NULL;
835 int ret = 0, cur_iaa = 0, n_wqs_added = 0;
836 struct idxd_device *idxd;
837 struct iaa_wq *iaa_wq;
838 struct pci_dev *pdev;
839 struct device *dev;
840
841 list_for_each_entry(iaa_device, &iaa_devices, list) {
842 idxd = iaa_device->idxd;
843 pdev = idxd->pdev;
844 dev = &pdev->dev;
845
846 if (cur_iaa != iaa) {
847 cur_iaa++;
848 continue;
849 }
850
851 found_device = iaa_device;
852 dev_dbg(dev, "getting wq from iaa_device %d, cur_iaa %d\n",
853 found_device->idxd->id, cur_iaa);
854 break;
855 }
856
857 if (!found_device) {
858 found_device = list_first_entry_or_null(&iaa_devices,
859 struct iaa_device, list);
860 if (!found_device) {
861 pr_debug("couldn't find any iaa devices with wqs!\n");
862 ret = -EINVAL;
863 goto out;
864 }
865 cur_iaa = 0;
866
867 idxd = found_device->idxd;
868 pdev = idxd->pdev;
869 dev = &pdev->dev;
870 dev_dbg(dev, "getting wq from only iaa_device %d, cur_iaa %d\n",
871 found_device->idxd->id, cur_iaa);
872 }
873
874 list_for_each_entry(iaa_wq, &found_device->wqs, list) {
875 wq_table_add(cpu, iaa_wq->wq);
876 pr_debug("rebalance: added wq for cpu=%d: iaa wq %d.%d\n",
877 cpu, iaa_wq->wq->idxd->id, iaa_wq->wq->id);
878 n_wqs_added++;
879 }
880
881 if (!n_wqs_added) {
882 pr_debug("couldn't find any iaa wqs!\n");
883 ret = -EINVAL;
884 goto out;
885 }
886 out:
887 return ret;
888 }
889
890 /*
891 * Rebalance the wq table so that given a cpu, it's easy to find the
892 * closest IAA instance. The idea is to try to choose the most
893 * appropriate IAA instance for a caller and spread available
894 * workqueues around to clients.
895 */
rebalance_wq_table(void)896 static void rebalance_wq_table(void)
897 {
898 const struct cpumask *node_cpus;
899 int node, cpu, iaa = -1;
900
901 if (nr_iaa == 0)
902 return;
903
904 pr_debug("rebalance: nr_nodes=%d, nr_cpus %d, nr_iaa %d, cpus_per_iaa %d\n",
905 nr_nodes, nr_cpus, nr_iaa, cpus_per_iaa);
906
907 clear_wq_table();
908
909 if (nr_iaa == 1) {
910 for (cpu = 0; cpu < nr_cpus; cpu++) {
911 if (WARN_ON(wq_table_add_wqs(0, cpu))) {
912 pr_debug("could not add any wqs for iaa 0 to cpu %d!\n", cpu);
913 return;
914 }
915 }
916
917 return;
918 }
919
920 for_each_node_with_cpus(node) {
921 node_cpus = cpumask_of_node(node);
922
923 for (cpu = 0; cpu < cpumask_weight(node_cpus); cpu++) {
924 int node_cpu = cpumask_nth(cpu, node_cpus);
925
926 if (WARN_ON(node_cpu >= nr_cpu_ids)) {
927 pr_debug("node_cpu %d doesn't exist!\n", node_cpu);
928 return;
929 }
930
931 if ((cpu % cpus_per_iaa) == 0)
932 iaa++;
933
934 if (WARN_ON(wq_table_add_wqs(iaa, node_cpu))) {
935 pr_debug("could not add any wqs for iaa %d to cpu %d!\n", iaa, cpu);
936 return;
937 }
938 }
939 }
940 }
941
check_completion(struct device * dev,struct iax_completion_record * comp,bool compress,bool only_once)942 static inline int check_completion(struct device *dev,
943 struct iax_completion_record *comp,
944 bool compress,
945 bool only_once)
946 {
947 char *op_str = compress ? "compress" : "decompress";
948 int ret = 0;
949
950 while (!comp->status) {
951 if (only_once)
952 return -EAGAIN;
953 cpu_relax();
954 }
955
956 if (comp->status != IAX_COMP_SUCCESS) {
957 if (comp->status == IAA_ERROR_WATCHDOG_EXPIRED) {
958 ret = -ETIMEDOUT;
959 dev_dbg(dev, "%s timed out, size=0x%x\n",
960 op_str, comp->output_size);
961 update_completion_timeout_errs();
962 goto out;
963 }
964
965 if (comp->status == IAA_ANALYTICS_ERROR &&
966 comp->error_code == IAA_ERROR_COMP_BUF_OVERFLOW && compress) {
967 ret = -E2BIG;
968 dev_dbg(dev, "compressed > uncompressed size,"
969 " not compressing, size=0x%x\n",
970 comp->output_size);
971 update_completion_comp_buf_overflow_errs();
972 goto out;
973 }
974
975 if (comp->status == IAA_ERROR_DECOMP_BUF_OVERFLOW) {
976 ret = -EOVERFLOW;
977 goto out;
978 }
979
980 ret = -EINVAL;
981 dev_dbg(dev, "iaa %s status=0x%x, error=0x%x, size=0x%x\n",
982 op_str, comp->status, comp->error_code, comp->output_size);
983 print_hex_dump(KERN_INFO, "cmp-rec: ", DUMP_PREFIX_OFFSET, 8, 1, comp, 64, 0);
984 update_completion_einval_errs();
985
986 goto out;
987 }
988 out:
989 return ret;
990 }
991
deflate_generic_decompress(struct acomp_req * req)992 static int deflate_generic_decompress(struct acomp_req *req)
993 {
994 void *src, *dst;
995 int ret;
996
997 src = kmap_local_page(sg_page(req->src)) + req->src->offset;
998 dst = kmap_local_page(sg_page(req->dst)) + req->dst->offset;
999
1000 ret = crypto_comp_decompress(deflate_generic_tfm,
1001 src, req->slen, dst, &req->dlen);
1002
1003 kunmap_local(src);
1004 kunmap_local(dst);
1005
1006 update_total_sw_decomp_calls();
1007
1008 return ret;
1009 }
1010
1011 static int iaa_remap_for_verify(struct device *dev, struct iaa_wq *iaa_wq,
1012 struct acomp_req *req,
1013 dma_addr_t *src_addr, dma_addr_t *dst_addr);
1014
1015 static int iaa_compress_verify(struct crypto_tfm *tfm, struct acomp_req *req,
1016 struct idxd_wq *wq,
1017 dma_addr_t src_addr, unsigned int slen,
1018 dma_addr_t dst_addr, unsigned int *dlen,
1019 u32 compression_crc);
1020
iaa_desc_complete(struct idxd_desc * idxd_desc,enum idxd_complete_type comp_type,bool free_desc,void * __ctx,u32 * status)1021 static void iaa_desc_complete(struct idxd_desc *idxd_desc,
1022 enum idxd_complete_type comp_type,
1023 bool free_desc, void *__ctx,
1024 u32 *status)
1025 {
1026 struct iaa_device_compression_mode *active_compression_mode;
1027 struct iaa_compression_ctx *compression_ctx;
1028 struct crypto_ctx *ctx = __ctx;
1029 struct iaa_device *iaa_device;
1030 struct idxd_device *idxd;
1031 struct iaa_wq *iaa_wq;
1032 struct pci_dev *pdev;
1033 struct device *dev;
1034 int ret, err = 0;
1035
1036 compression_ctx = crypto_tfm_ctx(ctx->tfm);
1037
1038 iaa_wq = idxd_wq_get_private(idxd_desc->wq);
1039 iaa_device = iaa_wq->iaa_device;
1040 idxd = iaa_device->idxd;
1041 pdev = idxd->pdev;
1042 dev = &pdev->dev;
1043
1044 active_compression_mode = get_iaa_device_compression_mode(iaa_device,
1045 compression_ctx->mode);
1046 dev_dbg(dev, "%s: compression mode %s,"
1047 " ctx->src_addr %llx, ctx->dst_addr %llx\n", __func__,
1048 active_compression_mode->name,
1049 ctx->src_addr, ctx->dst_addr);
1050
1051 ret = check_completion(dev, idxd_desc->iax_completion,
1052 ctx->compress, false);
1053 if (ret) {
1054 dev_dbg(dev, "%s: check_completion failed ret=%d\n", __func__, ret);
1055 if (!ctx->compress &&
1056 idxd_desc->iax_completion->status == IAA_ANALYTICS_ERROR) {
1057 pr_warn("%s: falling back to deflate-generic decompress, "
1058 "analytics error code %x\n", __func__,
1059 idxd_desc->iax_completion->error_code);
1060 ret = deflate_generic_decompress(ctx->req);
1061 if (ret) {
1062 dev_dbg(dev, "%s: deflate-generic failed ret=%d\n",
1063 __func__, ret);
1064 err = -EIO;
1065 goto err;
1066 }
1067 } else {
1068 err = -EIO;
1069 goto err;
1070 }
1071 } else {
1072 ctx->req->dlen = idxd_desc->iax_completion->output_size;
1073 }
1074
1075 /* Update stats */
1076 if (ctx->compress) {
1077 update_total_comp_bytes_out(ctx->req->dlen);
1078 update_wq_comp_bytes(iaa_wq->wq, ctx->req->dlen);
1079 } else {
1080 update_total_decomp_bytes_in(ctx->req->slen);
1081 update_wq_decomp_bytes(iaa_wq->wq, ctx->req->slen);
1082 }
1083
1084 if (ctx->compress && compression_ctx->verify_compress) {
1085 dma_addr_t src_addr, dst_addr;
1086 u32 compression_crc;
1087
1088 compression_crc = idxd_desc->iax_completion->crc;
1089
1090 ret = iaa_remap_for_verify(dev, iaa_wq, ctx->req, &src_addr, &dst_addr);
1091 if (ret) {
1092 dev_dbg(dev, "%s: compress verify remap failed ret=%d\n", __func__, ret);
1093 err = -EIO;
1094 goto out;
1095 }
1096
1097 ret = iaa_compress_verify(ctx->tfm, ctx->req, iaa_wq->wq, src_addr,
1098 ctx->req->slen, dst_addr, &ctx->req->dlen,
1099 compression_crc);
1100 if (ret) {
1101 dev_dbg(dev, "%s: compress verify failed ret=%d\n", __func__, ret);
1102 err = -EIO;
1103 }
1104
1105 dma_unmap_sg(dev, ctx->req->dst, sg_nents(ctx->req->dst), DMA_TO_DEVICE);
1106 dma_unmap_sg(dev, ctx->req->src, sg_nents(ctx->req->src), DMA_FROM_DEVICE);
1107
1108 goto out;
1109 }
1110 err:
1111 dma_unmap_sg(dev, ctx->req->dst, sg_nents(ctx->req->dst), DMA_FROM_DEVICE);
1112 dma_unmap_sg(dev, ctx->req->src, sg_nents(ctx->req->src), DMA_TO_DEVICE);
1113 out:
1114 if (ret != 0)
1115 dev_dbg(dev, "asynchronous compress failed ret=%d\n", ret);
1116
1117 if (ctx->req->base.complete)
1118 acomp_request_complete(ctx->req, err);
1119
1120 if (free_desc)
1121 idxd_free_desc(idxd_desc->wq, idxd_desc);
1122 iaa_wq_put(idxd_desc->wq);
1123 }
1124
iaa_compress(struct crypto_tfm * tfm,struct acomp_req * req,struct idxd_wq * wq,dma_addr_t src_addr,unsigned int slen,dma_addr_t dst_addr,unsigned int * dlen,u32 * compression_crc)1125 static int iaa_compress(struct crypto_tfm *tfm, struct acomp_req *req,
1126 struct idxd_wq *wq,
1127 dma_addr_t src_addr, unsigned int slen,
1128 dma_addr_t dst_addr, unsigned int *dlen,
1129 u32 *compression_crc)
1130 {
1131 struct iaa_device_compression_mode *active_compression_mode;
1132 struct iaa_compression_ctx *ctx = crypto_tfm_ctx(tfm);
1133 struct iaa_device *iaa_device;
1134 struct idxd_desc *idxd_desc;
1135 struct iax_hw_desc *desc;
1136 struct idxd_device *idxd;
1137 struct iaa_wq *iaa_wq;
1138 struct pci_dev *pdev;
1139 struct device *dev;
1140 int ret = 0;
1141
1142 iaa_wq = idxd_wq_get_private(wq);
1143 iaa_device = iaa_wq->iaa_device;
1144 idxd = iaa_device->idxd;
1145 pdev = idxd->pdev;
1146 dev = &pdev->dev;
1147
1148 active_compression_mode = get_iaa_device_compression_mode(iaa_device, ctx->mode);
1149
1150 idxd_desc = idxd_alloc_desc(wq, IDXD_OP_BLOCK);
1151 if (IS_ERR(idxd_desc)) {
1152 dev_dbg(dev, "idxd descriptor allocation failed\n");
1153 dev_dbg(dev, "iaa compress failed: ret=%ld\n", PTR_ERR(idxd_desc));
1154 return PTR_ERR(idxd_desc);
1155 }
1156 desc = idxd_desc->iax_hw;
1157
1158 desc->flags = IDXD_OP_FLAG_CRAV | IDXD_OP_FLAG_RCR |
1159 IDXD_OP_FLAG_RD_SRC2_AECS | IDXD_OP_FLAG_CC;
1160 desc->opcode = IAX_OPCODE_COMPRESS;
1161 desc->compr_flags = IAA_COMP_FLAGS;
1162 desc->priv = 0;
1163
1164 desc->src1_addr = (u64)src_addr;
1165 desc->src1_size = slen;
1166 desc->dst_addr = (u64)dst_addr;
1167 desc->max_dst_size = *dlen;
1168 desc->src2_addr = active_compression_mode->aecs_comp_table_dma_addr;
1169 desc->src2_size = sizeof(struct aecs_comp_table_record);
1170 desc->completion_addr = idxd_desc->compl_dma;
1171
1172 if (ctx->use_irq) {
1173 desc->flags |= IDXD_OP_FLAG_RCI;
1174
1175 idxd_desc->crypto.req = req;
1176 idxd_desc->crypto.tfm = tfm;
1177 idxd_desc->crypto.src_addr = src_addr;
1178 idxd_desc->crypto.dst_addr = dst_addr;
1179 idxd_desc->crypto.compress = true;
1180
1181 dev_dbg(dev, "%s use_async_irq: compression mode %s,"
1182 " src_addr %llx, dst_addr %llx\n", __func__,
1183 active_compression_mode->name,
1184 src_addr, dst_addr);
1185 }
1186
1187 dev_dbg(dev, "%s: compression mode %s,"
1188 " desc->src1_addr %llx, desc->src1_size %d,"
1189 " desc->dst_addr %llx, desc->max_dst_size %d,"
1190 " desc->src2_addr %llx, desc->src2_size %d\n", __func__,
1191 active_compression_mode->name,
1192 desc->src1_addr, desc->src1_size, desc->dst_addr,
1193 desc->max_dst_size, desc->src2_addr, desc->src2_size);
1194
1195 ret = idxd_submit_desc(wq, idxd_desc);
1196 if (ret) {
1197 dev_dbg(dev, "submit_desc failed ret=%d\n", ret);
1198 goto err;
1199 }
1200
1201 /* Update stats */
1202 update_total_comp_calls();
1203 update_wq_comp_calls(wq);
1204
1205 if (ctx->async_mode) {
1206 ret = -EINPROGRESS;
1207 dev_dbg(dev, "%s: returning -EINPROGRESS\n", __func__);
1208 goto out;
1209 }
1210
1211 ret = check_completion(dev, idxd_desc->iax_completion, true, false);
1212 if (ret) {
1213 dev_dbg(dev, "check_completion failed ret=%d\n", ret);
1214 goto err;
1215 }
1216
1217 *dlen = idxd_desc->iax_completion->output_size;
1218
1219 /* Update stats */
1220 update_total_comp_bytes_out(*dlen);
1221 update_wq_comp_bytes(wq, *dlen);
1222
1223 *compression_crc = idxd_desc->iax_completion->crc;
1224
1225 if (!ctx->async_mode)
1226 idxd_free_desc(wq, idxd_desc);
1227 out:
1228 return ret;
1229 err:
1230 idxd_free_desc(wq, idxd_desc);
1231 dev_dbg(dev, "iaa compress failed: ret=%d\n", ret);
1232
1233 goto out;
1234 }
1235
iaa_remap_for_verify(struct device * dev,struct iaa_wq * iaa_wq,struct acomp_req * req,dma_addr_t * src_addr,dma_addr_t * dst_addr)1236 static int iaa_remap_for_verify(struct device *dev, struct iaa_wq *iaa_wq,
1237 struct acomp_req *req,
1238 dma_addr_t *src_addr, dma_addr_t *dst_addr)
1239 {
1240 int ret = 0;
1241 int nr_sgs;
1242
1243 dma_unmap_sg(dev, req->dst, sg_nents(req->dst), DMA_FROM_DEVICE);
1244 dma_unmap_sg(dev, req->src, sg_nents(req->src), DMA_TO_DEVICE);
1245
1246 nr_sgs = dma_map_sg(dev, req->src, sg_nents(req->src), DMA_FROM_DEVICE);
1247 if (nr_sgs <= 0 || nr_sgs > 1) {
1248 dev_dbg(dev, "verify: couldn't map src sg for iaa device %d,"
1249 " wq %d: ret=%d\n", iaa_wq->iaa_device->idxd->id,
1250 iaa_wq->wq->id, ret);
1251 ret = -EIO;
1252 goto out;
1253 }
1254 *src_addr = sg_dma_address(req->src);
1255 dev_dbg(dev, "verify: dma_map_sg, src_addr %llx, nr_sgs %d, req->src %p,"
1256 " req->slen %d, sg_dma_len(sg) %d\n", *src_addr, nr_sgs,
1257 req->src, req->slen, sg_dma_len(req->src));
1258
1259 nr_sgs = dma_map_sg(dev, req->dst, sg_nents(req->dst), DMA_TO_DEVICE);
1260 if (nr_sgs <= 0 || nr_sgs > 1) {
1261 dev_dbg(dev, "verify: couldn't map dst sg for iaa device %d,"
1262 " wq %d: ret=%d\n", iaa_wq->iaa_device->idxd->id,
1263 iaa_wq->wq->id, ret);
1264 ret = -EIO;
1265 dma_unmap_sg(dev, req->src, sg_nents(req->src), DMA_FROM_DEVICE);
1266 goto out;
1267 }
1268 *dst_addr = sg_dma_address(req->dst);
1269 dev_dbg(dev, "verify: dma_map_sg, dst_addr %llx, nr_sgs %d, req->dst %p,"
1270 " req->dlen %d, sg_dma_len(sg) %d\n", *dst_addr, nr_sgs,
1271 req->dst, req->dlen, sg_dma_len(req->dst));
1272 out:
1273 return ret;
1274 }
1275
iaa_compress_verify(struct crypto_tfm * tfm,struct acomp_req * req,struct idxd_wq * wq,dma_addr_t src_addr,unsigned int slen,dma_addr_t dst_addr,unsigned int * dlen,u32 compression_crc)1276 static int iaa_compress_verify(struct crypto_tfm *tfm, struct acomp_req *req,
1277 struct idxd_wq *wq,
1278 dma_addr_t src_addr, unsigned int slen,
1279 dma_addr_t dst_addr, unsigned int *dlen,
1280 u32 compression_crc)
1281 {
1282 struct iaa_device_compression_mode *active_compression_mode;
1283 struct iaa_compression_ctx *ctx = crypto_tfm_ctx(tfm);
1284 struct iaa_device *iaa_device;
1285 struct idxd_desc *idxd_desc;
1286 struct iax_hw_desc *desc;
1287 struct idxd_device *idxd;
1288 struct iaa_wq *iaa_wq;
1289 struct pci_dev *pdev;
1290 struct device *dev;
1291 int ret = 0;
1292
1293 iaa_wq = idxd_wq_get_private(wq);
1294 iaa_device = iaa_wq->iaa_device;
1295 idxd = iaa_device->idxd;
1296 pdev = idxd->pdev;
1297 dev = &pdev->dev;
1298
1299 active_compression_mode = get_iaa_device_compression_mode(iaa_device, ctx->mode);
1300
1301 idxd_desc = idxd_alloc_desc(wq, IDXD_OP_BLOCK);
1302 if (IS_ERR(idxd_desc)) {
1303 dev_dbg(dev, "idxd descriptor allocation failed\n");
1304 dev_dbg(dev, "iaa compress failed: ret=%ld\n",
1305 PTR_ERR(idxd_desc));
1306 return PTR_ERR(idxd_desc);
1307 }
1308 desc = idxd_desc->iax_hw;
1309
1310 /* Verify (optional) - decompress and check crc, suppress dest write */
1311
1312 desc->flags = IDXD_OP_FLAG_CRAV | IDXD_OP_FLAG_RCR | IDXD_OP_FLAG_CC;
1313 desc->opcode = IAX_OPCODE_DECOMPRESS;
1314 desc->decompr_flags = IAA_DECOMP_FLAGS | IAA_DECOMP_SUPPRESS_OUTPUT;
1315 desc->priv = 0;
1316
1317 desc->src1_addr = (u64)dst_addr;
1318 desc->src1_size = *dlen;
1319 desc->dst_addr = (u64)src_addr;
1320 desc->max_dst_size = slen;
1321 desc->completion_addr = idxd_desc->compl_dma;
1322
1323 dev_dbg(dev, "(verify) compression mode %s,"
1324 " desc->src1_addr %llx, desc->src1_size %d,"
1325 " desc->dst_addr %llx, desc->max_dst_size %d,"
1326 " desc->src2_addr %llx, desc->src2_size %d\n",
1327 active_compression_mode->name,
1328 desc->src1_addr, desc->src1_size, desc->dst_addr,
1329 desc->max_dst_size, desc->src2_addr, desc->src2_size);
1330
1331 ret = idxd_submit_desc(wq, idxd_desc);
1332 if (ret) {
1333 dev_dbg(dev, "submit_desc (verify) failed ret=%d\n", ret);
1334 goto err;
1335 }
1336
1337 ret = check_completion(dev, idxd_desc->iax_completion, false, false);
1338 if (ret) {
1339 dev_dbg(dev, "(verify) check_completion failed ret=%d\n", ret);
1340 goto err;
1341 }
1342
1343 if (compression_crc != idxd_desc->iax_completion->crc) {
1344 ret = -EINVAL;
1345 dev_dbg(dev, "(verify) iaa comp/decomp crc mismatch:"
1346 " comp=0x%x, decomp=0x%x\n", compression_crc,
1347 idxd_desc->iax_completion->crc);
1348 print_hex_dump(KERN_INFO, "cmp-rec: ", DUMP_PREFIX_OFFSET,
1349 8, 1, idxd_desc->iax_completion, 64, 0);
1350 goto err;
1351 }
1352
1353 idxd_free_desc(wq, idxd_desc);
1354 out:
1355 return ret;
1356 err:
1357 idxd_free_desc(wq, idxd_desc);
1358 dev_dbg(dev, "iaa compress failed: ret=%d\n", ret);
1359
1360 goto out;
1361 }
1362
iaa_decompress(struct crypto_tfm * tfm,struct acomp_req * req,struct idxd_wq * wq,dma_addr_t src_addr,unsigned int slen,dma_addr_t dst_addr,unsigned int * dlen,bool disable_async)1363 static int iaa_decompress(struct crypto_tfm *tfm, struct acomp_req *req,
1364 struct idxd_wq *wq,
1365 dma_addr_t src_addr, unsigned int slen,
1366 dma_addr_t dst_addr, unsigned int *dlen,
1367 bool disable_async)
1368 {
1369 struct iaa_device_compression_mode *active_compression_mode;
1370 struct iaa_compression_ctx *ctx = crypto_tfm_ctx(tfm);
1371 struct iaa_device *iaa_device;
1372 struct idxd_desc *idxd_desc;
1373 struct iax_hw_desc *desc;
1374 struct idxd_device *idxd;
1375 struct iaa_wq *iaa_wq;
1376 struct pci_dev *pdev;
1377 struct device *dev;
1378 int ret = 0;
1379
1380 iaa_wq = idxd_wq_get_private(wq);
1381 iaa_device = iaa_wq->iaa_device;
1382 idxd = iaa_device->idxd;
1383 pdev = idxd->pdev;
1384 dev = &pdev->dev;
1385
1386 active_compression_mode = get_iaa_device_compression_mode(iaa_device, ctx->mode);
1387
1388 idxd_desc = idxd_alloc_desc(wq, IDXD_OP_BLOCK);
1389 if (IS_ERR(idxd_desc)) {
1390 dev_dbg(dev, "idxd descriptor allocation failed\n");
1391 dev_dbg(dev, "iaa decompress failed: ret=%ld\n",
1392 PTR_ERR(idxd_desc));
1393 return PTR_ERR(idxd_desc);
1394 }
1395 desc = idxd_desc->iax_hw;
1396
1397 desc->flags = IDXD_OP_FLAG_CRAV | IDXD_OP_FLAG_RCR | IDXD_OP_FLAG_CC;
1398 desc->opcode = IAX_OPCODE_DECOMPRESS;
1399 desc->max_dst_size = PAGE_SIZE;
1400 desc->decompr_flags = IAA_DECOMP_FLAGS;
1401 desc->priv = 0;
1402
1403 desc->src1_addr = (u64)src_addr;
1404 desc->dst_addr = (u64)dst_addr;
1405 desc->max_dst_size = *dlen;
1406 desc->src1_size = slen;
1407 desc->completion_addr = idxd_desc->compl_dma;
1408
1409 if (ctx->use_irq && !disable_async) {
1410 desc->flags |= IDXD_OP_FLAG_RCI;
1411
1412 idxd_desc->crypto.req = req;
1413 idxd_desc->crypto.tfm = tfm;
1414 idxd_desc->crypto.src_addr = src_addr;
1415 idxd_desc->crypto.dst_addr = dst_addr;
1416 idxd_desc->crypto.compress = false;
1417
1418 dev_dbg(dev, "%s: use_async_irq compression mode %s,"
1419 " src_addr %llx, dst_addr %llx\n", __func__,
1420 active_compression_mode->name,
1421 src_addr, dst_addr);
1422 }
1423
1424 dev_dbg(dev, "%s: decompression mode %s,"
1425 " desc->src1_addr %llx, desc->src1_size %d,"
1426 " desc->dst_addr %llx, desc->max_dst_size %d,"
1427 " desc->src2_addr %llx, desc->src2_size %d\n", __func__,
1428 active_compression_mode->name,
1429 desc->src1_addr, desc->src1_size, desc->dst_addr,
1430 desc->max_dst_size, desc->src2_addr, desc->src2_size);
1431
1432 ret = idxd_submit_desc(wq, idxd_desc);
1433 if (ret) {
1434 dev_dbg(dev, "submit_desc failed ret=%d\n", ret);
1435 goto err;
1436 }
1437
1438 /* Update stats */
1439 update_total_decomp_calls();
1440 update_wq_decomp_calls(wq);
1441
1442 if (ctx->async_mode && !disable_async) {
1443 ret = -EINPROGRESS;
1444 dev_dbg(dev, "%s: returning -EINPROGRESS\n", __func__);
1445 goto out;
1446 }
1447
1448 ret = check_completion(dev, idxd_desc->iax_completion, false, false);
1449 if (ret) {
1450 dev_dbg(dev, "%s: check_completion failed ret=%d\n", __func__, ret);
1451 if (idxd_desc->iax_completion->status == IAA_ANALYTICS_ERROR) {
1452 pr_warn("%s: falling back to deflate-generic decompress, "
1453 "analytics error code %x\n", __func__,
1454 idxd_desc->iax_completion->error_code);
1455 ret = deflate_generic_decompress(req);
1456 if (ret) {
1457 dev_dbg(dev, "%s: deflate-generic failed ret=%d\n",
1458 __func__, ret);
1459 goto err;
1460 }
1461 } else {
1462 goto err;
1463 }
1464 } else {
1465 req->dlen = idxd_desc->iax_completion->output_size;
1466 }
1467
1468 *dlen = req->dlen;
1469
1470 if (!ctx->async_mode || disable_async)
1471 idxd_free_desc(wq, idxd_desc);
1472
1473 /* Update stats */
1474 update_total_decomp_bytes_in(slen);
1475 update_wq_decomp_bytes(wq, slen);
1476 out:
1477 return ret;
1478 err:
1479 idxd_free_desc(wq, idxd_desc);
1480 dev_dbg(dev, "iaa decompress failed: ret=%d\n", ret);
1481
1482 goto out;
1483 }
1484
iaa_comp_acompress(struct acomp_req * req)1485 static int iaa_comp_acompress(struct acomp_req *req)
1486 {
1487 struct iaa_compression_ctx *compression_ctx;
1488 struct crypto_tfm *tfm = req->base.tfm;
1489 dma_addr_t src_addr, dst_addr;
1490 int nr_sgs, cpu, ret = 0;
1491 struct iaa_wq *iaa_wq;
1492 u32 compression_crc;
1493 struct idxd_wq *wq;
1494 struct device *dev;
1495
1496 compression_ctx = crypto_tfm_ctx(tfm);
1497
1498 if (!iaa_crypto_enabled) {
1499 pr_debug("iaa_crypto disabled, not compressing\n");
1500 return -ENODEV;
1501 }
1502
1503 if (!req->src || !req->slen) {
1504 pr_debug("invalid src, not compressing\n");
1505 return -EINVAL;
1506 }
1507
1508 cpu = get_cpu();
1509 wq = wq_table_next_wq(cpu);
1510 put_cpu();
1511 if (!wq) {
1512 pr_debug("no wq configured for cpu=%d\n", cpu);
1513 return -ENODEV;
1514 }
1515
1516 ret = iaa_wq_get(wq);
1517 if (ret) {
1518 pr_debug("no wq available for cpu=%d\n", cpu);
1519 return -ENODEV;
1520 }
1521
1522 iaa_wq = idxd_wq_get_private(wq);
1523
1524 dev = &wq->idxd->pdev->dev;
1525
1526 nr_sgs = dma_map_sg(dev, req->src, sg_nents(req->src), DMA_TO_DEVICE);
1527 if (nr_sgs <= 0 || nr_sgs > 1) {
1528 dev_dbg(dev, "couldn't map src sg for iaa device %d,"
1529 " wq %d: ret=%d\n", iaa_wq->iaa_device->idxd->id,
1530 iaa_wq->wq->id, ret);
1531 ret = -EIO;
1532 goto out;
1533 }
1534 src_addr = sg_dma_address(req->src);
1535 dev_dbg(dev, "dma_map_sg, src_addr %llx, nr_sgs %d, req->src %p,"
1536 " req->slen %d, sg_dma_len(sg) %d\n", src_addr, nr_sgs,
1537 req->src, req->slen, sg_dma_len(req->src));
1538
1539 nr_sgs = dma_map_sg(dev, req->dst, sg_nents(req->dst), DMA_FROM_DEVICE);
1540 if (nr_sgs <= 0 || nr_sgs > 1) {
1541 dev_dbg(dev, "couldn't map dst sg for iaa device %d,"
1542 " wq %d: ret=%d\n", iaa_wq->iaa_device->idxd->id,
1543 iaa_wq->wq->id, ret);
1544 ret = -EIO;
1545 goto err_map_dst;
1546 }
1547 dst_addr = sg_dma_address(req->dst);
1548 dev_dbg(dev, "dma_map_sg, dst_addr %llx, nr_sgs %d, req->dst %p,"
1549 " req->dlen %d, sg_dma_len(sg) %d\n", dst_addr, nr_sgs,
1550 req->dst, req->dlen, sg_dma_len(req->dst));
1551
1552 ret = iaa_compress(tfm, req, wq, src_addr, req->slen, dst_addr,
1553 &req->dlen, &compression_crc);
1554 if (ret == -EINPROGRESS)
1555 return ret;
1556
1557 if (!ret && compression_ctx->verify_compress) {
1558 ret = iaa_remap_for_verify(dev, iaa_wq, req, &src_addr, &dst_addr);
1559 if (ret) {
1560 dev_dbg(dev, "%s: compress verify remap failed ret=%d\n", __func__, ret);
1561 goto out;
1562 }
1563
1564 ret = iaa_compress_verify(tfm, req, wq, src_addr, req->slen,
1565 dst_addr, &req->dlen, compression_crc);
1566 if (ret)
1567 dev_dbg(dev, "asynchronous compress verification failed ret=%d\n", ret);
1568
1569 dma_unmap_sg(dev, req->dst, sg_nents(req->dst), DMA_TO_DEVICE);
1570 dma_unmap_sg(dev, req->src, sg_nents(req->src), DMA_FROM_DEVICE);
1571
1572 goto out;
1573 }
1574
1575 if (ret)
1576 dev_dbg(dev, "asynchronous compress failed ret=%d\n", ret);
1577
1578 dma_unmap_sg(dev, req->dst, sg_nents(req->dst), DMA_FROM_DEVICE);
1579 err_map_dst:
1580 dma_unmap_sg(dev, req->src, sg_nents(req->src), DMA_TO_DEVICE);
1581 out:
1582 iaa_wq_put(wq);
1583
1584 return ret;
1585 }
1586
iaa_comp_adecompress(struct acomp_req * req)1587 static int iaa_comp_adecompress(struct acomp_req *req)
1588 {
1589 struct crypto_tfm *tfm = req->base.tfm;
1590 dma_addr_t src_addr, dst_addr;
1591 int nr_sgs, cpu, ret = 0;
1592 struct iaa_wq *iaa_wq;
1593 struct device *dev;
1594 struct idxd_wq *wq;
1595
1596 if (!iaa_crypto_enabled) {
1597 pr_debug("iaa_crypto disabled, not decompressing\n");
1598 return -ENODEV;
1599 }
1600
1601 if (!req->src || !req->slen) {
1602 pr_debug("invalid src, not decompressing\n");
1603 return -EINVAL;
1604 }
1605
1606 cpu = get_cpu();
1607 wq = wq_table_next_wq(cpu);
1608 put_cpu();
1609 if (!wq) {
1610 pr_debug("no wq configured for cpu=%d\n", cpu);
1611 return -ENODEV;
1612 }
1613
1614 ret = iaa_wq_get(wq);
1615 if (ret) {
1616 pr_debug("no wq available for cpu=%d\n", cpu);
1617 return -ENODEV;
1618 }
1619
1620 iaa_wq = idxd_wq_get_private(wq);
1621
1622 dev = &wq->idxd->pdev->dev;
1623
1624 nr_sgs = dma_map_sg(dev, req->src, sg_nents(req->src), DMA_TO_DEVICE);
1625 if (nr_sgs <= 0 || nr_sgs > 1) {
1626 dev_dbg(dev, "couldn't map src sg for iaa device %d,"
1627 " wq %d: ret=%d\n", iaa_wq->iaa_device->idxd->id,
1628 iaa_wq->wq->id, ret);
1629 ret = -EIO;
1630 goto out;
1631 }
1632 src_addr = sg_dma_address(req->src);
1633 dev_dbg(dev, "dma_map_sg, src_addr %llx, nr_sgs %d, req->src %p,"
1634 " req->slen %d, sg_dma_len(sg) %d\n", src_addr, nr_sgs,
1635 req->src, req->slen, sg_dma_len(req->src));
1636
1637 nr_sgs = dma_map_sg(dev, req->dst, sg_nents(req->dst), DMA_FROM_DEVICE);
1638 if (nr_sgs <= 0 || nr_sgs > 1) {
1639 dev_dbg(dev, "couldn't map dst sg for iaa device %d,"
1640 " wq %d: ret=%d\n", iaa_wq->iaa_device->idxd->id,
1641 iaa_wq->wq->id, ret);
1642 ret = -EIO;
1643 goto err_map_dst;
1644 }
1645 dst_addr = sg_dma_address(req->dst);
1646 dev_dbg(dev, "dma_map_sg, dst_addr %llx, nr_sgs %d, req->dst %p,"
1647 " req->dlen %d, sg_dma_len(sg) %d\n", dst_addr, nr_sgs,
1648 req->dst, req->dlen, sg_dma_len(req->dst));
1649
1650 ret = iaa_decompress(tfm, req, wq, src_addr, req->slen,
1651 dst_addr, &req->dlen, false);
1652 if (ret == -EINPROGRESS)
1653 return ret;
1654
1655 if (ret != 0)
1656 dev_dbg(dev, "asynchronous decompress failed ret=%d\n", ret);
1657
1658 dma_unmap_sg(dev, req->dst, sg_nents(req->dst), DMA_FROM_DEVICE);
1659 err_map_dst:
1660 dma_unmap_sg(dev, req->src, sg_nents(req->src), DMA_TO_DEVICE);
1661 out:
1662 iaa_wq_put(wq);
1663
1664 return ret;
1665 }
1666
compression_ctx_init(struct iaa_compression_ctx * ctx)1667 static void compression_ctx_init(struct iaa_compression_ctx *ctx)
1668 {
1669 ctx->verify_compress = iaa_verify_compress;
1670 ctx->async_mode = async_mode;
1671 ctx->use_irq = use_irq;
1672 }
1673
iaa_comp_init_fixed(struct crypto_acomp * acomp_tfm)1674 static int iaa_comp_init_fixed(struct crypto_acomp *acomp_tfm)
1675 {
1676 struct crypto_tfm *tfm = crypto_acomp_tfm(acomp_tfm);
1677 struct iaa_compression_ctx *ctx = crypto_tfm_ctx(tfm);
1678
1679 compression_ctx_init(ctx);
1680
1681 ctx->mode = IAA_MODE_FIXED;
1682
1683 return 0;
1684 }
1685
1686 static struct acomp_alg iaa_acomp_fixed_deflate = {
1687 .init = iaa_comp_init_fixed,
1688 .compress = iaa_comp_acompress,
1689 .decompress = iaa_comp_adecompress,
1690 .base = {
1691 .cra_name = "deflate",
1692 .cra_driver_name = "deflate-iaa",
1693 .cra_flags = CRYPTO_ALG_ASYNC,
1694 .cra_ctxsize = sizeof(struct iaa_compression_ctx),
1695 .cra_module = THIS_MODULE,
1696 .cra_priority = IAA_ALG_PRIORITY,
1697 }
1698 };
1699
iaa_register_compression_device(void)1700 static int iaa_register_compression_device(void)
1701 {
1702 int ret;
1703
1704 ret = crypto_register_acomp(&iaa_acomp_fixed_deflate);
1705 if (ret) {
1706 pr_err("deflate algorithm acomp fixed registration failed (%d)\n", ret);
1707 goto out;
1708 }
1709
1710 iaa_crypto_registered = true;
1711 out:
1712 return ret;
1713 }
1714
iaa_unregister_compression_device(void)1715 static int iaa_unregister_compression_device(void)
1716 {
1717 if (iaa_crypto_registered)
1718 crypto_unregister_acomp(&iaa_acomp_fixed_deflate);
1719
1720 return 0;
1721 }
1722
iaa_crypto_probe(struct idxd_dev * idxd_dev)1723 static int iaa_crypto_probe(struct idxd_dev *idxd_dev)
1724 {
1725 struct idxd_wq *wq = idxd_dev_to_wq(idxd_dev);
1726 struct idxd_device *idxd = wq->idxd;
1727 struct idxd_driver_data *data = idxd->data;
1728 struct device *dev = &idxd_dev->conf_dev;
1729 bool first_wq = false;
1730 int ret = 0;
1731
1732 if (idxd->state != IDXD_DEV_ENABLED)
1733 return -ENXIO;
1734
1735 if (data->type != IDXD_TYPE_IAX)
1736 return -ENODEV;
1737
1738 mutex_lock(&wq->wq_lock);
1739
1740 if (idxd_wq_get_private(wq)) {
1741 mutex_unlock(&wq->wq_lock);
1742 return -EBUSY;
1743 }
1744
1745 if (!idxd_wq_driver_name_match(wq, dev)) {
1746 dev_dbg(dev, "wq %d.%d driver_name match failed: wq driver_name %s, dev driver name %s\n",
1747 idxd->id, wq->id, wq->driver_name, dev->driver->name);
1748 idxd->cmd_status = IDXD_SCMD_WQ_NO_DRV_NAME;
1749 ret = -ENODEV;
1750 goto err;
1751 }
1752
1753 wq->type = IDXD_WQT_KERNEL;
1754
1755 ret = idxd_drv_enable_wq(wq);
1756 if (ret < 0) {
1757 dev_dbg(dev, "enable wq %d.%d failed: %d\n",
1758 idxd->id, wq->id, ret);
1759 ret = -ENXIO;
1760 goto err;
1761 }
1762
1763 mutex_lock(&iaa_devices_lock);
1764
1765 if (list_empty(&iaa_devices)) {
1766 ret = alloc_wq_table(wq->idxd->max_wqs);
1767 if (ret)
1768 goto err_alloc;
1769 first_wq = true;
1770 }
1771
1772 ret = save_iaa_wq(wq);
1773 if (ret)
1774 goto err_save;
1775
1776 rebalance_wq_table();
1777
1778 if (first_wq) {
1779 iaa_crypto_enabled = true;
1780 ret = iaa_register_compression_device();
1781 if (ret != 0) {
1782 iaa_crypto_enabled = false;
1783 dev_dbg(dev, "IAA compression device registration failed\n");
1784 goto err_register;
1785 }
1786 try_module_get(THIS_MODULE);
1787
1788 pr_info("iaa_crypto now ENABLED\n");
1789 }
1790
1791 mutex_unlock(&iaa_devices_lock);
1792 out:
1793 mutex_unlock(&wq->wq_lock);
1794
1795 return ret;
1796
1797 err_register:
1798 remove_iaa_wq(wq);
1799 free_iaa_wq(idxd_wq_get_private(wq));
1800 err_save:
1801 if (first_wq)
1802 free_wq_table();
1803 err_alloc:
1804 mutex_unlock(&iaa_devices_lock);
1805 idxd_drv_disable_wq(wq);
1806 err:
1807 wq->type = IDXD_WQT_NONE;
1808
1809 goto out;
1810 }
1811
iaa_crypto_remove(struct idxd_dev * idxd_dev)1812 static void iaa_crypto_remove(struct idxd_dev *idxd_dev)
1813 {
1814 struct idxd_wq *wq = idxd_dev_to_wq(idxd_dev);
1815 struct idxd_device *idxd = wq->idxd;
1816 struct iaa_wq *iaa_wq;
1817 bool free = false;
1818
1819 idxd_wq_quiesce(wq);
1820
1821 mutex_lock(&wq->wq_lock);
1822 mutex_lock(&iaa_devices_lock);
1823
1824 remove_iaa_wq(wq);
1825
1826 spin_lock(&idxd->dev_lock);
1827 iaa_wq = idxd_wq_get_private(wq);
1828 if (!iaa_wq) {
1829 spin_unlock(&idxd->dev_lock);
1830 pr_err("%s: no iaa_wq available to remove\n", __func__);
1831 goto out;
1832 }
1833
1834 if (iaa_wq->ref) {
1835 iaa_wq->remove = true;
1836 } else {
1837 wq = iaa_wq->wq;
1838 idxd_wq_set_private(wq, NULL);
1839 free = true;
1840 }
1841 spin_unlock(&idxd->dev_lock);
1842 if (free) {
1843 __free_iaa_wq(iaa_wq);
1844 kfree(iaa_wq);
1845 }
1846
1847 idxd_drv_disable_wq(wq);
1848 rebalance_wq_table();
1849
1850 if (nr_iaa == 0) {
1851 iaa_crypto_enabled = false;
1852 free_wq_table();
1853 module_put(THIS_MODULE);
1854
1855 pr_info("iaa_crypto now DISABLED\n");
1856 }
1857 out:
1858 mutex_unlock(&iaa_devices_lock);
1859 mutex_unlock(&wq->wq_lock);
1860 }
1861
1862 static enum idxd_dev_type dev_types[] = {
1863 IDXD_DEV_WQ,
1864 IDXD_DEV_NONE,
1865 };
1866
1867 static struct idxd_device_driver iaa_crypto_driver = {
1868 .probe = iaa_crypto_probe,
1869 .remove = iaa_crypto_remove,
1870 .name = IDXD_SUBDRIVER_NAME,
1871 .type = dev_types,
1872 .desc_complete = iaa_desc_complete,
1873 };
1874
iaa_crypto_init_module(void)1875 static int __init iaa_crypto_init_module(void)
1876 {
1877 int ret = 0;
1878 int node;
1879
1880 nr_cpus = num_possible_cpus();
1881 for_each_node_with_cpus(node)
1882 nr_nodes++;
1883 if (!nr_nodes) {
1884 pr_err("IAA couldn't find any nodes with cpus\n");
1885 return -ENODEV;
1886 }
1887 nr_cpus_per_node = nr_cpus / nr_nodes;
1888
1889 if (crypto_has_comp("deflate-generic", 0, 0))
1890 deflate_generic_tfm = crypto_alloc_comp("deflate-generic", 0, 0);
1891
1892 if (IS_ERR_OR_NULL(deflate_generic_tfm)) {
1893 pr_err("IAA could not alloc %s tfm: errcode = %ld\n",
1894 "deflate-generic", PTR_ERR(deflate_generic_tfm));
1895 return -ENOMEM;
1896 }
1897
1898 ret = iaa_aecs_init_fixed();
1899 if (ret < 0) {
1900 pr_debug("IAA fixed compression mode init failed\n");
1901 goto err_aecs_init;
1902 }
1903
1904 ret = idxd_driver_register(&iaa_crypto_driver);
1905 if (ret) {
1906 pr_debug("IAA wq sub-driver registration failed\n");
1907 goto err_driver_reg;
1908 }
1909
1910 ret = driver_create_file(&iaa_crypto_driver.drv,
1911 &driver_attr_verify_compress);
1912 if (ret) {
1913 pr_debug("IAA verify_compress attr creation failed\n");
1914 goto err_verify_attr_create;
1915 }
1916
1917 ret = driver_create_file(&iaa_crypto_driver.drv,
1918 &driver_attr_sync_mode);
1919 if (ret) {
1920 pr_debug("IAA sync mode attr creation failed\n");
1921 goto err_sync_attr_create;
1922 }
1923
1924 if (iaa_crypto_debugfs_init())
1925 pr_warn("debugfs init failed, stats not available\n");
1926
1927 pr_debug("initialized\n");
1928 out:
1929 return ret;
1930
1931 err_sync_attr_create:
1932 driver_remove_file(&iaa_crypto_driver.drv,
1933 &driver_attr_verify_compress);
1934 err_verify_attr_create:
1935 idxd_driver_unregister(&iaa_crypto_driver);
1936 err_driver_reg:
1937 iaa_aecs_cleanup_fixed();
1938 err_aecs_init:
1939 crypto_free_comp(deflate_generic_tfm);
1940
1941 goto out;
1942 }
1943
iaa_crypto_cleanup_module(void)1944 static void __exit iaa_crypto_cleanup_module(void)
1945 {
1946 if (iaa_unregister_compression_device())
1947 pr_debug("IAA compression device unregister failed\n");
1948
1949 iaa_crypto_debugfs_cleanup();
1950 driver_remove_file(&iaa_crypto_driver.drv,
1951 &driver_attr_sync_mode);
1952 driver_remove_file(&iaa_crypto_driver.drv,
1953 &driver_attr_verify_compress);
1954 idxd_driver_unregister(&iaa_crypto_driver);
1955 iaa_aecs_cleanup_fixed();
1956 crypto_free_comp(deflate_generic_tfm);
1957
1958 pr_debug("cleaned up\n");
1959 }
1960
1961 MODULE_IMPORT_NS(IDXD);
1962 MODULE_LICENSE("GPL");
1963 MODULE_ALIAS_IDXD_DEVICE(0);
1964 MODULE_AUTHOR("Intel Corporation");
1965 MODULE_DESCRIPTION("IAA Compression Accelerator Crypto Driver");
1966
1967 module_init(iaa_crypto_init_module);
1968 module_exit(iaa_crypto_cleanup_module);
1969