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