xref: /linux/drivers/dma/idxd/device.c (revision de5819b994893197c71c86d21af10f85f50d6499)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2019 Intel Corporation. All rights rsvd. */
3 #include <linux/init.h>
4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <linux/pci.h>
7 #include <linux/io-64-nonatomic-lo-hi.h>
8 #include <linux/dmaengine.h>
9 #include <linux/irq.h>
10 #include <linux/msi.h>
11 #include <uapi/linux/idxd.h>
12 #include "../dmaengine.h"
13 #include "idxd.h"
14 #include "registers.h"
15 
16 static void idxd_cmd_exec(struct idxd_device *idxd, int cmd_code, u32 operand,
17 			  u32 *status);
18 static void idxd_device_wqs_clear_state(struct idxd_device *idxd);
19 static void idxd_wq_disable_cleanup(struct idxd_wq *wq);
20 
21 /* Interrupt control bits */
22 void idxd_unmask_error_interrupts(struct idxd_device *idxd)
23 {
24 	union genctrl_reg genctrl;
25 
26 	genctrl.bits = ioread32(idxd->reg_base + IDXD_GENCTRL_OFFSET);
27 	genctrl.softerr_int_en = 1;
28 	genctrl.halt_int_en = 1;
29 	iowrite32(genctrl.bits, idxd->reg_base + IDXD_GENCTRL_OFFSET);
30 }
31 
32 void idxd_mask_error_interrupts(struct idxd_device *idxd)
33 {
34 	union genctrl_reg genctrl;
35 
36 	genctrl.bits = ioread32(idxd->reg_base + IDXD_GENCTRL_OFFSET);
37 	genctrl.softerr_int_en = 0;
38 	genctrl.halt_int_en = 0;
39 	iowrite32(genctrl.bits, idxd->reg_base + IDXD_GENCTRL_OFFSET);
40 }
41 
42 static void free_hw_descs(struct idxd_wq *wq)
43 {
44 	int i;
45 
46 	for (i = 0; i < wq->num_descs; i++)
47 		kfree(wq->hw_descs[i]);
48 
49 	kfree(wq->hw_descs);
50 }
51 
52 static int alloc_hw_descs(struct idxd_wq *wq, int num)
53 {
54 	struct device *dev = &wq->idxd->pdev->dev;
55 	int i;
56 	int node = dev_to_node(dev);
57 
58 	wq->hw_descs = kcalloc_node(num, sizeof(struct dsa_hw_desc *),
59 				    GFP_KERNEL, node);
60 	if (!wq->hw_descs)
61 		return -ENOMEM;
62 
63 	for (i = 0; i < num; i++) {
64 		wq->hw_descs[i] = kzalloc_node(sizeof(*wq->hw_descs[i]),
65 					       GFP_KERNEL, node);
66 		if (!wq->hw_descs[i]) {
67 			free_hw_descs(wq);
68 			return -ENOMEM;
69 		}
70 	}
71 
72 	return 0;
73 }
74 
75 static void free_descs(struct idxd_wq *wq)
76 {
77 	int i;
78 
79 	for (i = 0; i < wq->num_descs; i++)
80 		kfree(wq->descs[i]);
81 
82 	kfree(wq->descs);
83 }
84 
85 static int alloc_descs(struct idxd_wq *wq, int num)
86 {
87 	struct device *dev = &wq->idxd->pdev->dev;
88 	int i;
89 	int node = dev_to_node(dev);
90 
91 	wq->descs = kcalloc_node(num, sizeof(struct idxd_desc *),
92 				 GFP_KERNEL, node);
93 	if (!wq->descs)
94 		return -ENOMEM;
95 
96 	for (i = 0; i < num; i++) {
97 		wq->descs[i] = kzalloc_node(sizeof(*wq->descs[i]),
98 					    GFP_KERNEL, node);
99 		if (!wq->descs[i]) {
100 			free_descs(wq);
101 			return -ENOMEM;
102 		}
103 	}
104 
105 	return 0;
106 }
107 
108 /* WQ control bits */
109 int idxd_wq_alloc_resources(struct idxd_wq *wq)
110 {
111 	struct idxd_device *idxd = wq->idxd;
112 	struct device *dev = &idxd->pdev->dev;
113 	int rc, num_descs, i;
114 
115 	if (wq->type != IDXD_WQT_KERNEL)
116 		return 0;
117 
118 	num_descs = wq_dedicated(wq) ? wq->size : wq->threshold;
119 	wq->num_descs = num_descs;
120 
121 	rc = alloc_hw_descs(wq, num_descs);
122 	if (rc < 0)
123 		return rc;
124 
125 	wq->compls_size = num_descs * idxd->data->compl_size;
126 	wq->compls = dma_alloc_coherent(dev, wq->compls_size, &wq->compls_addr, GFP_KERNEL);
127 	if (!wq->compls) {
128 		rc = -ENOMEM;
129 		goto fail_alloc_compls;
130 	}
131 
132 	rc = alloc_descs(wq, num_descs);
133 	if (rc < 0)
134 		goto fail_alloc_descs;
135 
136 	rc = sbitmap_queue_init_node(&wq->sbq, num_descs, -1, false, GFP_KERNEL,
137 				     dev_to_node(dev));
138 	if (rc < 0)
139 		goto fail_sbitmap_init;
140 
141 	for (i = 0; i < num_descs; i++) {
142 		struct idxd_desc *desc = wq->descs[i];
143 
144 		desc->hw = wq->hw_descs[i];
145 		if (idxd->data->type == IDXD_TYPE_DSA)
146 			desc->completion = &wq->compls[i];
147 		else if (idxd->data->type == IDXD_TYPE_IAX)
148 			desc->iax_completion = &wq->iax_compls[i];
149 		desc->compl_dma = wq->compls_addr + idxd->data->compl_size * i;
150 		desc->id = i;
151 		desc->wq = wq;
152 		desc->cpu = -1;
153 	}
154 
155 	return 0;
156 
157  fail_sbitmap_init:
158 	free_descs(wq);
159  fail_alloc_descs:
160 	dma_free_coherent(dev, wq->compls_size, wq->compls, wq->compls_addr);
161  fail_alloc_compls:
162 	free_hw_descs(wq);
163 	return rc;
164 }
165 
166 void idxd_wq_free_resources(struct idxd_wq *wq)
167 {
168 	struct device *dev = &wq->idxd->pdev->dev;
169 
170 	if (wq->type != IDXD_WQT_KERNEL)
171 		return;
172 
173 	free_hw_descs(wq);
174 	free_descs(wq);
175 	dma_free_coherent(dev, wq->compls_size, wq->compls, wq->compls_addr);
176 	sbitmap_queue_free(&wq->sbq);
177 }
178 
179 int idxd_wq_enable(struct idxd_wq *wq)
180 {
181 	struct idxd_device *idxd = wq->idxd;
182 	struct device *dev = &idxd->pdev->dev;
183 	u32 status;
184 
185 	if (wq->state == IDXD_WQ_ENABLED) {
186 		dev_dbg(dev, "WQ %d already enabled\n", wq->id);
187 		return 0;
188 	}
189 
190 	idxd_cmd_exec(idxd, IDXD_CMD_ENABLE_WQ, wq->id, &status);
191 
192 	if (status != IDXD_CMDSTS_SUCCESS &&
193 	    status != IDXD_CMDSTS_ERR_WQ_ENABLED) {
194 		dev_dbg(dev, "WQ enable failed: %#x\n", status);
195 		return -ENXIO;
196 	}
197 
198 	wq->state = IDXD_WQ_ENABLED;
199 	set_bit(wq->id, idxd->wq_enable_map);
200 	dev_dbg(dev, "WQ %d enabled\n", wq->id);
201 	return 0;
202 }
203 
204 int idxd_wq_disable(struct idxd_wq *wq, bool reset_config)
205 {
206 	struct idxd_device *idxd = wq->idxd;
207 	struct device *dev = &idxd->pdev->dev;
208 	u32 status, operand;
209 
210 	dev_dbg(dev, "Disabling WQ %d\n", wq->id);
211 
212 	if (wq->state != IDXD_WQ_ENABLED) {
213 		dev_dbg(dev, "WQ %d in wrong state: %d\n", wq->id, wq->state);
214 		return 0;
215 	}
216 
217 	operand = BIT(wq->id % 16) | ((wq->id / 16) << 16);
218 	idxd_cmd_exec(idxd, IDXD_CMD_DISABLE_WQ, operand, &status);
219 
220 	if (status != IDXD_CMDSTS_SUCCESS) {
221 		dev_dbg(dev, "WQ disable failed: %#x\n", status);
222 		return -ENXIO;
223 	}
224 
225 	if (reset_config)
226 		idxd_wq_disable_cleanup(wq);
227 	clear_bit(wq->id, idxd->wq_enable_map);
228 	wq->state = IDXD_WQ_DISABLED;
229 	dev_dbg(dev, "WQ %d disabled\n", wq->id);
230 	return 0;
231 }
232 
233 void idxd_wq_drain(struct idxd_wq *wq)
234 {
235 	struct idxd_device *idxd = wq->idxd;
236 	struct device *dev = &idxd->pdev->dev;
237 	u32 operand;
238 
239 	if (wq->state != IDXD_WQ_ENABLED) {
240 		dev_dbg(dev, "WQ %d in wrong state: %d\n", wq->id, wq->state);
241 		return;
242 	}
243 
244 	dev_dbg(dev, "Draining WQ %d\n", wq->id);
245 	operand = BIT(wq->id % 16) | ((wq->id / 16) << 16);
246 	idxd_cmd_exec(idxd, IDXD_CMD_DRAIN_WQ, operand, NULL);
247 }
248 
249 void idxd_wq_reset(struct idxd_wq *wq)
250 {
251 	struct idxd_device *idxd = wq->idxd;
252 	struct device *dev = &idxd->pdev->dev;
253 	u32 operand;
254 
255 	if (wq->state != IDXD_WQ_ENABLED) {
256 		dev_dbg(dev, "WQ %d in wrong state: %d\n", wq->id, wq->state);
257 		return;
258 	}
259 
260 	operand = BIT(wq->id % 16) | ((wq->id / 16) << 16);
261 	idxd_cmd_exec(idxd, IDXD_CMD_RESET_WQ, operand, NULL);
262 	idxd_wq_disable_cleanup(wq);
263 }
264 
265 int idxd_wq_map_portal(struct idxd_wq *wq)
266 {
267 	struct idxd_device *idxd = wq->idxd;
268 	struct pci_dev *pdev = idxd->pdev;
269 	struct device *dev = &pdev->dev;
270 	resource_size_t start;
271 
272 	start = pci_resource_start(pdev, IDXD_WQ_BAR);
273 	start += idxd_get_wq_portal_full_offset(wq->id, IDXD_PORTAL_LIMITED);
274 
275 	wq->portal = devm_ioremap(dev, start, IDXD_PORTAL_SIZE);
276 	if (!wq->portal)
277 		return -ENOMEM;
278 
279 	return 0;
280 }
281 
282 void idxd_wq_unmap_portal(struct idxd_wq *wq)
283 {
284 	struct device *dev = &wq->idxd->pdev->dev;
285 
286 	devm_iounmap(dev, wq->portal);
287 	wq->portal = NULL;
288 	wq->portal_offset = 0;
289 }
290 
291 void idxd_wqs_unmap_portal(struct idxd_device *idxd)
292 {
293 	int i;
294 
295 	for (i = 0; i < idxd->max_wqs; i++) {
296 		struct idxd_wq *wq = idxd->wqs[i];
297 
298 		if (wq->portal)
299 			idxd_wq_unmap_portal(wq);
300 	}
301 }
302 
303 static void __idxd_wq_set_priv_locked(struct idxd_wq *wq, int priv)
304 {
305 	struct idxd_device *idxd = wq->idxd;
306 	union wqcfg wqcfg;
307 	unsigned int offset;
308 
309 	offset = WQCFG_OFFSET(idxd, wq->id, WQCFG_PRIVL_IDX);
310 	spin_lock(&idxd->dev_lock);
311 	wqcfg.bits[WQCFG_PRIVL_IDX] = ioread32(idxd->reg_base + offset);
312 	wqcfg.priv = priv;
313 	wq->wqcfg->bits[WQCFG_PRIVL_IDX] = wqcfg.bits[WQCFG_PRIVL_IDX];
314 	iowrite32(wqcfg.bits[WQCFG_PRIVL_IDX], idxd->reg_base + offset);
315 	spin_unlock(&idxd->dev_lock);
316 }
317 
318 static void __idxd_wq_set_pasid_locked(struct idxd_wq *wq, int pasid)
319 {
320 	struct idxd_device *idxd = wq->idxd;
321 	union wqcfg wqcfg;
322 	unsigned int offset;
323 
324 	offset = WQCFG_OFFSET(idxd, wq->id, WQCFG_PASID_IDX);
325 	spin_lock(&idxd->dev_lock);
326 	wqcfg.bits[WQCFG_PASID_IDX] = ioread32(idxd->reg_base + offset);
327 	wqcfg.pasid_en = 1;
328 	wqcfg.pasid = pasid;
329 	wq->wqcfg->bits[WQCFG_PASID_IDX] = wqcfg.bits[WQCFG_PASID_IDX];
330 	iowrite32(wqcfg.bits[WQCFG_PASID_IDX], idxd->reg_base + offset);
331 	spin_unlock(&idxd->dev_lock);
332 }
333 
334 int idxd_wq_set_pasid(struct idxd_wq *wq, int pasid)
335 {
336 	int rc;
337 
338 	rc = idxd_wq_disable(wq, false);
339 	if (rc < 0)
340 		return rc;
341 
342 	__idxd_wq_set_pasid_locked(wq, pasid);
343 
344 	rc = idxd_wq_enable(wq);
345 	if (rc < 0)
346 		return rc;
347 
348 	return 0;
349 }
350 
351 int idxd_wq_disable_pasid(struct idxd_wq *wq)
352 {
353 	struct idxd_device *idxd = wq->idxd;
354 	int rc;
355 	union wqcfg wqcfg;
356 	unsigned int offset;
357 
358 	rc = idxd_wq_disable(wq, false);
359 	if (rc < 0)
360 		return rc;
361 
362 	offset = WQCFG_OFFSET(idxd, wq->id, WQCFG_PASID_IDX);
363 	spin_lock(&idxd->dev_lock);
364 	wqcfg.bits[WQCFG_PASID_IDX] = ioread32(idxd->reg_base + offset);
365 	wqcfg.pasid_en = 0;
366 	wqcfg.pasid = 0;
367 	iowrite32(wqcfg.bits[WQCFG_PASID_IDX], idxd->reg_base + offset);
368 	spin_unlock(&idxd->dev_lock);
369 
370 	rc = idxd_wq_enable(wq);
371 	if (rc < 0)
372 		return rc;
373 
374 	return 0;
375 }
376 
377 static void idxd_wq_disable_cleanup(struct idxd_wq *wq)
378 {
379 	struct idxd_device *idxd = wq->idxd;
380 
381 	lockdep_assert_held(&wq->wq_lock);
382 	wq->state = IDXD_WQ_DISABLED;
383 	memset(wq->wqcfg, 0, idxd->wqcfg_size);
384 	wq->type = IDXD_WQT_NONE;
385 	wq->threshold = 0;
386 	wq->priority = 0;
387 	wq->ats_dis = 0;
388 	wq->enqcmds_retries = IDXD_ENQCMDS_RETRIES;
389 	clear_bit(WQ_FLAG_DEDICATED, &wq->flags);
390 	clear_bit(WQ_FLAG_BLOCK_ON_FAULT, &wq->flags);
391 	memset(wq->name, 0, WQ_NAME_SIZE);
392 	wq->max_xfer_bytes = WQ_DEFAULT_MAX_XFER;
393 	wq->max_batch_size = WQ_DEFAULT_MAX_BATCH;
394 }
395 
396 static void idxd_wq_device_reset_cleanup(struct idxd_wq *wq)
397 {
398 	lockdep_assert_held(&wq->wq_lock);
399 
400 	wq->size = 0;
401 	wq->group = NULL;
402 }
403 
404 static void idxd_wq_ref_release(struct percpu_ref *ref)
405 {
406 	struct idxd_wq *wq = container_of(ref, struct idxd_wq, wq_active);
407 
408 	complete(&wq->wq_dead);
409 }
410 
411 int idxd_wq_init_percpu_ref(struct idxd_wq *wq)
412 {
413 	int rc;
414 
415 	memset(&wq->wq_active, 0, sizeof(wq->wq_active));
416 	rc = percpu_ref_init(&wq->wq_active, idxd_wq_ref_release,
417 			     PERCPU_REF_ALLOW_REINIT, GFP_KERNEL);
418 	if (rc < 0)
419 		return rc;
420 	reinit_completion(&wq->wq_dead);
421 	reinit_completion(&wq->wq_resurrect);
422 	return 0;
423 }
424 
425 void __idxd_wq_quiesce(struct idxd_wq *wq)
426 {
427 	lockdep_assert_held(&wq->wq_lock);
428 	reinit_completion(&wq->wq_resurrect);
429 	percpu_ref_kill(&wq->wq_active);
430 	complete_all(&wq->wq_resurrect);
431 	wait_for_completion(&wq->wq_dead);
432 }
433 
434 void idxd_wq_quiesce(struct idxd_wq *wq)
435 {
436 	mutex_lock(&wq->wq_lock);
437 	__idxd_wq_quiesce(wq);
438 	mutex_unlock(&wq->wq_lock);
439 }
440 
441 /* Device control bits */
442 static inline bool idxd_is_enabled(struct idxd_device *idxd)
443 {
444 	union gensts_reg gensts;
445 
446 	gensts.bits = ioread32(idxd->reg_base + IDXD_GENSTATS_OFFSET);
447 
448 	if (gensts.state == IDXD_DEVICE_STATE_ENABLED)
449 		return true;
450 	return false;
451 }
452 
453 static inline bool idxd_device_is_halted(struct idxd_device *idxd)
454 {
455 	union gensts_reg gensts;
456 
457 	gensts.bits = ioread32(idxd->reg_base + IDXD_GENSTATS_OFFSET);
458 
459 	return (gensts.state == IDXD_DEVICE_STATE_HALT);
460 }
461 
462 /*
463  * This is function is only used for reset during probe and will
464  * poll for completion. Once the device is setup with interrupts,
465  * all commands will be done via interrupt completion.
466  */
467 int idxd_device_init_reset(struct idxd_device *idxd)
468 {
469 	struct device *dev = &idxd->pdev->dev;
470 	union idxd_command_reg cmd;
471 
472 	if (idxd_device_is_halted(idxd)) {
473 		dev_warn(&idxd->pdev->dev, "Device is HALTED!\n");
474 		return -ENXIO;
475 	}
476 
477 	memset(&cmd, 0, sizeof(cmd));
478 	cmd.cmd = IDXD_CMD_RESET_DEVICE;
479 	dev_dbg(dev, "%s: sending reset for init.\n", __func__);
480 	spin_lock(&idxd->cmd_lock);
481 	iowrite32(cmd.bits, idxd->reg_base + IDXD_CMD_OFFSET);
482 
483 	while (ioread32(idxd->reg_base + IDXD_CMDSTS_OFFSET) &
484 	       IDXD_CMDSTS_ACTIVE)
485 		cpu_relax();
486 	spin_unlock(&idxd->cmd_lock);
487 	return 0;
488 }
489 
490 static void idxd_cmd_exec(struct idxd_device *idxd, int cmd_code, u32 operand,
491 			  u32 *status)
492 {
493 	union idxd_command_reg cmd;
494 	DECLARE_COMPLETION_ONSTACK(done);
495 	u32 stat;
496 
497 	if (idxd_device_is_halted(idxd)) {
498 		dev_warn(&idxd->pdev->dev, "Device is HALTED!\n");
499 		if (status)
500 			*status = IDXD_CMDSTS_HW_ERR;
501 		return;
502 	}
503 
504 	memset(&cmd, 0, sizeof(cmd));
505 	cmd.cmd = cmd_code;
506 	cmd.operand = operand;
507 	cmd.int_req = 1;
508 
509 	spin_lock(&idxd->cmd_lock);
510 	wait_event_lock_irq(idxd->cmd_waitq,
511 			    !test_bit(IDXD_FLAG_CMD_RUNNING, &idxd->flags),
512 			    idxd->cmd_lock);
513 
514 	dev_dbg(&idxd->pdev->dev, "%s: sending cmd: %#x op: %#x\n",
515 		__func__, cmd_code, operand);
516 
517 	idxd->cmd_status = 0;
518 	__set_bit(IDXD_FLAG_CMD_RUNNING, &idxd->flags);
519 	idxd->cmd_done = &done;
520 	iowrite32(cmd.bits, idxd->reg_base + IDXD_CMD_OFFSET);
521 
522 	/*
523 	 * After command submitted, release lock and go to sleep until
524 	 * the command completes via interrupt.
525 	 */
526 	spin_unlock(&idxd->cmd_lock);
527 	wait_for_completion(&done);
528 	stat = ioread32(idxd->reg_base + IDXD_CMDSTS_OFFSET);
529 	spin_lock(&idxd->cmd_lock);
530 	if (status)
531 		*status = stat;
532 	idxd->cmd_status = stat & GENMASK(7, 0);
533 
534 	__clear_bit(IDXD_FLAG_CMD_RUNNING, &idxd->flags);
535 	/* Wake up other pending commands */
536 	wake_up(&idxd->cmd_waitq);
537 	spin_unlock(&idxd->cmd_lock);
538 }
539 
540 int idxd_device_enable(struct idxd_device *idxd)
541 {
542 	struct device *dev = &idxd->pdev->dev;
543 	u32 status;
544 
545 	if (idxd_is_enabled(idxd)) {
546 		dev_dbg(dev, "Device already enabled\n");
547 		return -ENXIO;
548 	}
549 
550 	idxd_cmd_exec(idxd, IDXD_CMD_ENABLE_DEVICE, 0, &status);
551 
552 	/* If the command is successful or if the device was enabled */
553 	if (status != IDXD_CMDSTS_SUCCESS &&
554 	    status != IDXD_CMDSTS_ERR_DEV_ENABLED) {
555 		dev_dbg(dev, "%s: err_code: %#x\n", __func__, status);
556 		return -ENXIO;
557 	}
558 
559 	idxd->state = IDXD_DEV_ENABLED;
560 	return 0;
561 }
562 
563 int idxd_device_disable(struct idxd_device *idxd)
564 {
565 	struct device *dev = &idxd->pdev->dev;
566 	u32 status;
567 
568 	if (!idxd_is_enabled(idxd)) {
569 		dev_dbg(dev, "Device is not enabled\n");
570 		return 0;
571 	}
572 
573 	idxd_cmd_exec(idxd, IDXD_CMD_DISABLE_DEVICE, 0, &status);
574 
575 	/* If the command is successful or if the device was disabled */
576 	if (status != IDXD_CMDSTS_SUCCESS &&
577 	    !(status & IDXD_CMDSTS_ERR_DIS_DEV_EN)) {
578 		dev_dbg(dev, "%s: err_code: %#x\n", __func__, status);
579 		return -ENXIO;
580 	}
581 
582 	idxd_device_clear_state(idxd);
583 	return 0;
584 }
585 
586 void idxd_device_reset(struct idxd_device *idxd)
587 {
588 	idxd_cmd_exec(idxd, IDXD_CMD_RESET_DEVICE, 0, NULL);
589 	idxd_device_clear_state(idxd);
590 	spin_lock(&idxd->dev_lock);
591 	idxd_unmask_error_interrupts(idxd);
592 	spin_unlock(&idxd->dev_lock);
593 }
594 
595 void idxd_device_drain_pasid(struct idxd_device *idxd, int pasid)
596 {
597 	struct device *dev = &idxd->pdev->dev;
598 	u32 operand;
599 
600 	operand = pasid;
601 	dev_dbg(dev, "cmd: %u operand: %#x\n", IDXD_CMD_DRAIN_PASID, operand);
602 	idxd_cmd_exec(idxd, IDXD_CMD_DRAIN_PASID, operand, NULL);
603 	dev_dbg(dev, "pasid %d drained\n", pasid);
604 }
605 
606 int idxd_device_request_int_handle(struct idxd_device *idxd, int idx, int *handle,
607 				   enum idxd_interrupt_type irq_type)
608 {
609 	struct device *dev = &idxd->pdev->dev;
610 	u32 operand, status;
611 
612 	if (!(idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)))
613 		return -EOPNOTSUPP;
614 
615 	dev_dbg(dev, "get int handle, idx %d\n", idx);
616 
617 	operand = idx & GENMASK(15, 0);
618 	if (irq_type == IDXD_IRQ_IMS)
619 		operand |= CMD_INT_HANDLE_IMS;
620 
621 	dev_dbg(dev, "cmd: %u operand: %#x\n", IDXD_CMD_REQUEST_INT_HANDLE, operand);
622 
623 	idxd_cmd_exec(idxd, IDXD_CMD_REQUEST_INT_HANDLE, operand, &status);
624 
625 	if ((status & IDXD_CMDSTS_ERR_MASK) != IDXD_CMDSTS_SUCCESS) {
626 		dev_dbg(dev, "request int handle failed: %#x\n", status);
627 		return -ENXIO;
628 	}
629 
630 	*handle = (status >> IDXD_CMDSTS_RES_SHIFT) & GENMASK(15, 0);
631 
632 	dev_dbg(dev, "int handle acquired: %u\n", *handle);
633 	return 0;
634 }
635 
636 int idxd_device_release_int_handle(struct idxd_device *idxd, int handle,
637 				   enum idxd_interrupt_type irq_type)
638 {
639 	struct device *dev = &idxd->pdev->dev;
640 	u32 operand, status;
641 	union idxd_command_reg cmd;
642 
643 	if (!(idxd->hw.cmd_cap & BIT(IDXD_CMD_RELEASE_INT_HANDLE)))
644 		return -EOPNOTSUPP;
645 
646 	dev_dbg(dev, "release int handle, handle %d\n", handle);
647 
648 	memset(&cmd, 0, sizeof(cmd));
649 	operand = handle & GENMASK(15, 0);
650 
651 	if (irq_type == IDXD_IRQ_IMS)
652 		operand |= CMD_INT_HANDLE_IMS;
653 
654 	cmd.cmd = IDXD_CMD_RELEASE_INT_HANDLE;
655 	cmd.operand = operand;
656 
657 	dev_dbg(dev, "cmd: %u operand: %#x\n", IDXD_CMD_RELEASE_INT_HANDLE, operand);
658 
659 	spin_lock(&idxd->cmd_lock);
660 	iowrite32(cmd.bits, idxd->reg_base + IDXD_CMD_OFFSET);
661 
662 	while (ioread32(idxd->reg_base + IDXD_CMDSTS_OFFSET) & IDXD_CMDSTS_ACTIVE)
663 		cpu_relax();
664 	status = ioread32(idxd->reg_base + IDXD_CMDSTS_OFFSET);
665 	spin_unlock(&idxd->cmd_lock);
666 
667 	if ((status & IDXD_CMDSTS_ERR_MASK) != IDXD_CMDSTS_SUCCESS) {
668 		dev_dbg(dev, "release int handle failed: %#x\n", status);
669 		return -ENXIO;
670 	}
671 
672 	dev_dbg(dev, "int handle released.\n");
673 	return 0;
674 }
675 
676 /* Device configuration bits */
677 static void idxd_engines_clear_state(struct idxd_device *idxd)
678 {
679 	struct idxd_engine *engine;
680 	int i;
681 
682 	lockdep_assert_held(&idxd->dev_lock);
683 	for (i = 0; i < idxd->max_engines; i++) {
684 		engine = idxd->engines[i];
685 		engine->group = NULL;
686 	}
687 }
688 
689 static void idxd_groups_clear_state(struct idxd_device *idxd)
690 {
691 	struct idxd_group *group;
692 	int i;
693 
694 	lockdep_assert_held(&idxd->dev_lock);
695 	for (i = 0; i < idxd->max_groups; i++) {
696 		group = idxd->groups[i];
697 		memset(&group->grpcfg, 0, sizeof(group->grpcfg));
698 		group->num_engines = 0;
699 		group->num_wqs = 0;
700 		group->use_rdbuf_limit = false;
701 		group->rdbufs_allowed = 0;
702 		group->rdbufs_reserved = 0;
703 		if (idxd->hw.version < DEVICE_VERSION_2 && !tc_override) {
704 			group->tc_a = 1;
705 			group->tc_b = 1;
706 		} else {
707 			group->tc_a = -1;
708 			group->tc_b = -1;
709 		}
710 	}
711 }
712 
713 static void idxd_device_wqs_clear_state(struct idxd_device *idxd)
714 {
715 	int i;
716 
717 	for (i = 0; i < idxd->max_wqs; i++) {
718 		struct idxd_wq *wq = idxd->wqs[i];
719 
720 		mutex_lock(&wq->wq_lock);
721 		idxd_wq_disable_cleanup(wq);
722 		idxd_wq_device_reset_cleanup(wq);
723 		mutex_unlock(&wq->wq_lock);
724 	}
725 }
726 
727 void idxd_device_clear_state(struct idxd_device *idxd)
728 {
729 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
730 		return;
731 
732 	idxd_device_wqs_clear_state(idxd);
733 	spin_lock(&idxd->dev_lock);
734 	idxd_groups_clear_state(idxd);
735 	idxd_engines_clear_state(idxd);
736 	idxd->state = IDXD_DEV_DISABLED;
737 	spin_unlock(&idxd->dev_lock);
738 }
739 
740 static void idxd_group_config_write(struct idxd_group *group)
741 {
742 	struct idxd_device *idxd = group->idxd;
743 	struct device *dev = &idxd->pdev->dev;
744 	int i;
745 	u32 grpcfg_offset;
746 
747 	dev_dbg(dev, "Writing group %d cfg registers\n", group->id);
748 
749 	/* setup GRPWQCFG */
750 	for (i = 0; i < GRPWQCFG_STRIDES; i++) {
751 		grpcfg_offset = GRPWQCFG_OFFSET(idxd, group->id, i);
752 		iowrite64(group->grpcfg.wqs[i], idxd->reg_base + grpcfg_offset);
753 		dev_dbg(dev, "GRPCFG wq[%d:%d: %#x]: %#llx\n",
754 			group->id, i, grpcfg_offset,
755 			ioread64(idxd->reg_base + grpcfg_offset));
756 	}
757 
758 	/* setup GRPENGCFG */
759 	grpcfg_offset = GRPENGCFG_OFFSET(idxd, group->id);
760 	iowrite64(group->grpcfg.engines, idxd->reg_base + grpcfg_offset);
761 	dev_dbg(dev, "GRPCFG engs[%d: %#x]: %#llx\n", group->id,
762 		grpcfg_offset, ioread64(idxd->reg_base + grpcfg_offset));
763 
764 	/* setup GRPFLAGS */
765 	grpcfg_offset = GRPFLGCFG_OFFSET(idxd, group->id);
766 	iowrite32(group->grpcfg.flags.bits, idxd->reg_base + grpcfg_offset);
767 	dev_dbg(dev, "GRPFLAGS flags[%d: %#x]: %#x\n",
768 		group->id, grpcfg_offset,
769 		ioread32(idxd->reg_base + grpcfg_offset));
770 }
771 
772 static int idxd_groups_config_write(struct idxd_device *idxd)
773 
774 {
775 	union gencfg_reg reg;
776 	int i;
777 	struct device *dev = &idxd->pdev->dev;
778 
779 	/* Setup bandwidth rdbuf limit */
780 	if (idxd->hw.gen_cap.config_en && idxd->rdbuf_limit) {
781 		reg.bits = ioread32(idxd->reg_base + IDXD_GENCFG_OFFSET);
782 		reg.rdbuf_limit = idxd->rdbuf_limit;
783 		iowrite32(reg.bits, idxd->reg_base + IDXD_GENCFG_OFFSET);
784 	}
785 
786 	dev_dbg(dev, "GENCFG(%#x): %#x\n", IDXD_GENCFG_OFFSET,
787 		ioread32(idxd->reg_base + IDXD_GENCFG_OFFSET));
788 
789 	for (i = 0; i < idxd->max_groups; i++) {
790 		struct idxd_group *group = idxd->groups[i];
791 
792 		idxd_group_config_write(group);
793 	}
794 
795 	return 0;
796 }
797 
798 static bool idxd_device_pasid_priv_enabled(struct idxd_device *idxd)
799 {
800 	struct pci_dev *pdev = idxd->pdev;
801 
802 	if (pdev->pasid_enabled && (pdev->pasid_features & PCI_PASID_CAP_PRIV))
803 		return true;
804 	return false;
805 }
806 
807 static int idxd_wq_config_write(struct idxd_wq *wq)
808 {
809 	struct idxd_device *idxd = wq->idxd;
810 	struct device *dev = &idxd->pdev->dev;
811 	u32 wq_offset;
812 	int i;
813 
814 	if (!wq->group)
815 		return 0;
816 
817 	/*
818 	 * Instead of memset the entire shadow copy of WQCFG, copy from the hardware after
819 	 * wq reset. This will copy back the sticky values that are present on some devices.
820 	 */
821 	for (i = 0; i < WQCFG_STRIDES(idxd); i++) {
822 		wq_offset = WQCFG_OFFSET(idxd, wq->id, i);
823 		wq->wqcfg->bits[i] |= ioread32(idxd->reg_base + wq_offset);
824 	}
825 
826 	if (wq->size == 0 && wq->type != IDXD_WQT_NONE)
827 		wq->size = WQ_DEFAULT_QUEUE_DEPTH;
828 
829 	/* byte 0-3 */
830 	wq->wqcfg->wq_size = wq->size;
831 
832 	/* bytes 4-7 */
833 	wq->wqcfg->wq_thresh = wq->threshold;
834 
835 	/* byte 8-11 */
836 	if (wq_dedicated(wq))
837 		wq->wqcfg->mode = 1;
838 
839 	/*
840 	 * The WQ priv bit is set depending on the WQ type. priv = 1 if the
841 	 * WQ type is kernel to indicate privileged access. This setting only
842 	 * matters for dedicated WQ. According to the DSA spec:
843 	 * If the WQ is in dedicated mode, WQ PASID Enable is 1, and the
844 	 * Privileged Mode Enable field of the PCI Express PASID capability
845 	 * is 0, this field must be 0.
846 	 *
847 	 * In the case of a dedicated kernel WQ that is not able to support
848 	 * the PASID cap, then the configuration will be rejected.
849 	 */
850 	if (wq_dedicated(wq) && wq->wqcfg->pasid_en &&
851 	    !idxd_device_pasid_priv_enabled(idxd) &&
852 	    wq->type == IDXD_WQT_KERNEL) {
853 		idxd->cmd_status = IDXD_SCMD_WQ_NO_PRIV;
854 		return -EOPNOTSUPP;
855 	}
856 
857 	wq->wqcfg->priority = wq->priority;
858 
859 	if (idxd->hw.gen_cap.block_on_fault &&
860 	    test_bit(WQ_FLAG_BLOCK_ON_FAULT, &wq->flags))
861 		wq->wqcfg->bof = 1;
862 
863 	if (idxd->hw.wq_cap.wq_ats_support)
864 		wq->wqcfg->wq_ats_disable = wq->ats_dis;
865 
866 	/* bytes 12-15 */
867 	wq->wqcfg->max_xfer_shift = ilog2(wq->max_xfer_bytes);
868 	wq->wqcfg->max_batch_shift = ilog2(wq->max_batch_size);
869 
870 	dev_dbg(dev, "WQ %d CFGs\n", wq->id);
871 	for (i = 0; i < WQCFG_STRIDES(idxd); i++) {
872 		wq_offset = WQCFG_OFFSET(idxd, wq->id, i);
873 		iowrite32(wq->wqcfg->bits[i], idxd->reg_base + wq_offset);
874 		dev_dbg(dev, "WQ[%d][%d][%#x]: %#x\n",
875 			wq->id, i, wq_offset,
876 			ioread32(idxd->reg_base + wq_offset));
877 	}
878 
879 	return 0;
880 }
881 
882 static int idxd_wqs_config_write(struct idxd_device *idxd)
883 {
884 	int i, rc;
885 
886 	for (i = 0; i < idxd->max_wqs; i++) {
887 		struct idxd_wq *wq = idxd->wqs[i];
888 
889 		rc = idxd_wq_config_write(wq);
890 		if (rc < 0)
891 			return rc;
892 	}
893 
894 	return 0;
895 }
896 
897 static void idxd_group_flags_setup(struct idxd_device *idxd)
898 {
899 	int i;
900 
901 	/* TC-A 0 and TC-B 1 should be defaults */
902 	for (i = 0; i < idxd->max_groups; i++) {
903 		struct idxd_group *group = idxd->groups[i];
904 
905 		if (group->tc_a == -1)
906 			group->tc_a = group->grpcfg.flags.tc_a = 0;
907 		else
908 			group->grpcfg.flags.tc_a = group->tc_a;
909 		if (group->tc_b == -1)
910 			group->tc_b = group->grpcfg.flags.tc_b = 1;
911 		else
912 			group->grpcfg.flags.tc_b = group->tc_b;
913 		group->grpcfg.flags.use_rdbuf_limit = group->use_rdbuf_limit;
914 		group->grpcfg.flags.rdbufs_reserved = group->rdbufs_reserved;
915 		if (group->rdbufs_allowed)
916 			group->grpcfg.flags.rdbufs_allowed = group->rdbufs_allowed;
917 		else
918 			group->grpcfg.flags.rdbufs_allowed = idxd->max_rdbufs;
919 	}
920 }
921 
922 static int idxd_engines_setup(struct idxd_device *idxd)
923 {
924 	int i, engines = 0;
925 	struct idxd_engine *eng;
926 	struct idxd_group *group;
927 
928 	for (i = 0; i < idxd->max_groups; i++) {
929 		group = idxd->groups[i];
930 		group->grpcfg.engines = 0;
931 	}
932 
933 	for (i = 0; i < idxd->max_engines; i++) {
934 		eng = idxd->engines[i];
935 		group = eng->group;
936 
937 		if (!group)
938 			continue;
939 
940 		group->grpcfg.engines |= BIT(eng->id);
941 		engines++;
942 	}
943 
944 	if (!engines)
945 		return -EINVAL;
946 
947 	return 0;
948 }
949 
950 static int idxd_wqs_setup(struct idxd_device *idxd)
951 {
952 	struct idxd_wq *wq;
953 	struct idxd_group *group;
954 	int i, j, configured = 0;
955 	struct device *dev = &idxd->pdev->dev;
956 
957 	for (i = 0; i < idxd->max_groups; i++) {
958 		group = idxd->groups[i];
959 		for (j = 0; j < 4; j++)
960 			group->grpcfg.wqs[j] = 0;
961 	}
962 
963 	for (i = 0; i < idxd->max_wqs; i++) {
964 		wq = idxd->wqs[i];
965 		group = wq->group;
966 
967 		if (!wq->group)
968 			continue;
969 
970 		if (wq_shared(wq) && !wq_shared_supported(wq)) {
971 			idxd->cmd_status = IDXD_SCMD_WQ_NO_SWQ_SUPPORT;
972 			dev_warn(dev, "No shared wq support but configured.\n");
973 			return -EINVAL;
974 		}
975 
976 		group->grpcfg.wqs[wq->id / 64] |= BIT(wq->id % 64);
977 		configured++;
978 	}
979 
980 	if (configured == 0) {
981 		idxd->cmd_status = IDXD_SCMD_WQ_NONE_CONFIGURED;
982 		return -EINVAL;
983 	}
984 
985 	return 0;
986 }
987 
988 int idxd_device_config(struct idxd_device *idxd)
989 {
990 	int rc;
991 
992 	lockdep_assert_held(&idxd->dev_lock);
993 	rc = idxd_wqs_setup(idxd);
994 	if (rc < 0)
995 		return rc;
996 
997 	rc = idxd_engines_setup(idxd);
998 	if (rc < 0)
999 		return rc;
1000 
1001 	idxd_group_flags_setup(idxd);
1002 
1003 	rc = idxd_wqs_config_write(idxd);
1004 	if (rc < 0)
1005 		return rc;
1006 
1007 	rc = idxd_groups_config_write(idxd);
1008 	if (rc < 0)
1009 		return rc;
1010 
1011 	return 0;
1012 }
1013 
1014 static int idxd_wq_load_config(struct idxd_wq *wq)
1015 {
1016 	struct idxd_device *idxd = wq->idxd;
1017 	struct device *dev = &idxd->pdev->dev;
1018 	int wqcfg_offset;
1019 	int i;
1020 
1021 	wqcfg_offset = WQCFG_OFFSET(idxd, wq->id, 0);
1022 	memcpy_fromio(wq->wqcfg, idxd->reg_base + wqcfg_offset, idxd->wqcfg_size);
1023 
1024 	wq->size = wq->wqcfg->wq_size;
1025 	wq->threshold = wq->wqcfg->wq_thresh;
1026 
1027 	/* The driver does not support shared WQ mode in read-only config yet */
1028 	if (wq->wqcfg->mode == 0 || wq->wqcfg->pasid_en)
1029 		return -EOPNOTSUPP;
1030 
1031 	set_bit(WQ_FLAG_DEDICATED, &wq->flags);
1032 
1033 	wq->priority = wq->wqcfg->priority;
1034 
1035 	wq->max_xfer_bytes = 1ULL << wq->wqcfg->max_xfer_shift;
1036 	wq->max_batch_size = 1ULL << wq->wqcfg->max_batch_shift;
1037 
1038 	for (i = 0; i < WQCFG_STRIDES(idxd); i++) {
1039 		wqcfg_offset = WQCFG_OFFSET(idxd, wq->id, i);
1040 		dev_dbg(dev, "WQ[%d][%d][%#x]: %#x\n", wq->id, i, wqcfg_offset, wq->wqcfg->bits[i]);
1041 	}
1042 
1043 	return 0;
1044 }
1045 
1046 static void idxd_group_load_config(struct idxd_group *group)
1047 {
1048 	struct idxd_device *idxd = group->idxd;
1049 	struct device *dev = &idxd->pdev->dev;
1050 	int i, j, grpcfg_offset;
1051 
1052 	/*
1053 	 * Load WQS bit fields
1054 	 * Iterate through all 256 bits 64 bits at a time
1055 	 */
1056 	for (i = 0; i < GRPWQCFG_STRIDES; i++) {
1057 		struct idxd_wq *wq;
1058 
1059 		grpcfg_offset = GRPWQCFG_OFFSET(idxd, group->id, i);
1060 		group->grpcfg.wqs[i] = ioread64(idxd->reg_base + grpcfg_offset);
1061 		dev_dbg(dev, "GRPCFG wq[%d:%d: %#x]: %#llx\n",
1062 			group->id, i, grpcfg_offset, group->grpcfg.wqs[i]);
1063 
1064 		if (i * 64 >= idxd->max_wqs)
1065 			break;
1066 
1067 		/* Iterate through all 64 bits and check for wq set */
1068 		for (j = 0; j < 64; j++) {
1069 			int id = i * 64 + j;
1070 
1071 			/* No need to check beyond max wqs */
1072 			if (id >= idxd->max_wqs)
1073 				break;
1074 
1075 			/* Set group assignment for wq if wq bit is set */
1076 			if (group->grpcfg.wqs[i] & BIT(j)) {
1077 				wq = idxd->wqs[id];
1078 				wq->group = group;
1079 			}
1080 		}
1081 	}
1082 
1083 	grpcfg_offset = GRPENGCFG_OFFSET(idxd, group->id);
1084 	group->grpcfg.engines = ioread64(idxd->reg_base + grpcfg_offset);
1085 	dev_dbg(dev, "GRPCFG engs[%d: %#x]: %#llx\n", group->id,
1086 		grpcfg_offset, group->grpcfg.engines);
1087 
1088 	/* Iterate through all 64 bits to check engines set */
1089 	for (i = 0; i < 64; i++) {
1090 		if (i >= idxd->max_engines)
1091 			break;
1092 
1093 		if (group->grpcfg.engines & BIT(i)) {
1094 			struct idxd_engine *engine = idxd->engines[i];
1095 
1096 			engine->group = group;
1097 		}
1098 	}
1099 
1100 	grpcfg_offset = GRPFLGCFG_OFFSET(idxd, group->id);
1101 	group->grpcfg.flags.bits = ioread32(idxd->reg_base + grpcfg_offset);
1102 	dev_dbg(dev, "GRPFLAGS flags[%d: %#x]: %#x\n",
1103 		group->id, grpcfg_offset, group->grpcfg.flags.bits);
1104 }
1105 
1106 int idxd_device_load_config(struct idxd_device *idxd)
1107 {
1108 	union gencfg_reg reg;
1109 	int i, rc;
1110 
1111 	reg.bits = ioread32(idxd->reg_base + IDXD_GENCFG_OFFSET);
1112 	idxd->rdbuf_limit = reg.rdbuf_limit;
1113 
1114 	for (i = 0; i < idxd->max_groups; i++) {
1115 		struct idxd_group *group = idxd->groups[i];
1116 
1117 		idxd_group_load_config(group);
1118 	}
1119 
1120 	for (i = 0; i < idxd->max_wqs; i++) {
1121 		struct idxd_wq *wq = idxd->wqs[i];
1122 
1123 		rc = idxd_wq_load_config(wq);
1124 		if (rc < 0)
1125 			return rc;
1126 	}
1127 
1128 	return 0;
1129 }
1130 
1131 static void idxd_flush_pending_descs(struct idxd_irq_entry *ie)
1132 {
1133 	struct idxd_desc *desc, *itr;
1134 	struct llist_node *head;
1135 	LIST_HEAD(flist);
1136 	enum idxd_complete_type ctype;
1137 
1138 	spin_lock(&ie->list_lock);
1139 	head = llist_del_all(&ie->pending_llist);
1140 	if (head) {
1141 		llist_for_each_entry_safe(desc, itr, head, llnode)
1142 			list_add_tail(&desc->list, &ie->work_list);
1143 	}
1144 
1145 	list_for_each_entry_safe(desc, itr, &ie->work_list, list)
1146 		list_move_tail(&desc->list, &flist);
1147 	spin_unlock(&ie->list_lock);
1148 
1149 	list_for_each_entry_safe(desc, itr, &flist, list) {
1150 		list_del(&desc->list);
1151 		ctype = desc->completion->status ? IDXD_COMPLETE_NORMAL : IDXD_COMPLETE_ABORT;
1152 		idxd_dma_complete_txd(desc, ctype, true);
1153 	}
1154 }
1155 
1156 static void idxd_device_set_perm_entry(struct idxd_device *idxd,
1157 				       struct idxd_irq_entry *ie)
1158 {
1159 	union msix_perm mperm;
1160 
1161 	if (ie->pasid == INVALID_IOASID)
1162 		return;
1163 
1164 	mperm.bits = 0;
1165 	mperm.pasid = ie->pasid;
1166 	mperm.pasid_en = 1;
1167 	iowrite32(mperm.bits, idxd->reg_base + idxd->msix_perm_offset + ie->id * 8);
1168 }
1169 
1170 static void idxd_device_clear_perm_entry(struct idxd_device *idxd,
1171 					 struct idxd_irq_entry *ie)
1172 {
1173 	iowrite32(0, idxd->reg_base + idxd->msix_perm_offset + ie->id * 8);
1174 }
1175 
1176 void idxd_wq_free_irq(struct idxd_wq *wq)
1177 {
1178 	struct idxd_device *idxd = wq->idxd;
1179 	struct idxd_irq_entry *ie = &wq->ie;
1180 
1181 	if (wq->type != IDXD_WQT_KERNEL)
1182 		return;
1183 
1184 	free_irq(ie->vector, ie);
1185 	idxd_flush_pending_descs(ie);
1186 	if (idxd->request_int_handles)
1187 		idxd_device_release_int_handle(idxd, ie->int_handle, IDXD_IRQ_MSIX);
1188 	idxd_device_clear_perm_entry(idxd, ie);
1189 	ie->vector = -1;
1190 	ie->int_handle = INVALID_INT_HANDLE;
1191 	ie->pasid = INVALID_IOASID;
1192 }
1193 
1194 int idxd_wq_request_irq(struct idxd_wq *wq)
1195 {
1196 	struct idxd_device *idxd = wq->idxd;
1197 	struct pci_dev *pdev = idxd->pdev;
1198 	struct device *dev = &pdev->dev;
1199 	struct idxd_irq_entry *ie;
1200 	int rc;
1201 
1202 	if (wq->type != IDXD_WQT_KERNEL)
1203 		return 0;
1204 
1205 	ie = &wq->ie;
1206 	ie->vector = pci_irq_vector(pdev, ie->id);
1207 	ie->pasid = device_pasid_enabled(idxd) ? idxd->pasid : INVALID_IOASID;
1208 	idxd_device_set_perm_entry(idxd, ie);
1209 
1210 	rc = request_threaded_irq(ie->vector, NULL, idxd_wq_thread, 0, "idxd-portal", ie);
1211 	if (rc < 0) {
1212 		dev_err(dev, "Failed to request irq %d.\n", ie->vector);
1213 		goto err_irq;
1214 	}
1215 
1216 	if (idxd->request_int_handles) {
1217 		rc = idxd_device_request_int_handle(idxd, ie->id, &ie->int_handle,
1218 						    IDXD_IRQ_MSIX);
1219 		if (rc < 0)
1220 			goto err_int_handle;
1221 	} else {
1222 		ie->int_handle = ie->id;
1223 	}
1224 
1225 	return 0;
1226 
1227 err_int_handle:
1228 	ie->int_handle = INVALID_INT_HANDLE;
1229 	free_irq(ie->vector, ie);
1230 err_irq:
1231 	idxd_device_clear_perm_entry(idxd, ie);
1232 	ie->pasid = INVALID_IOASID;
1233 	return rc;
1234 }
1235 
1236 int drv_enable_wq(struct idxd_wq *wq)
1237 {
1238 	struct idxd_device *idxd = wq->idxd;
1239 	struct device *dev = &idxd->pdev->dev;
1240 	int rc = -ENXIO;
1241 
1242 	lockdep_assert_held(&wq->wq_lock);
1243 
1244 	if (idxd->state != IDXD_DEV_ENABLED) {
1245 		idxd->cmd_status = IDXD_SCMD_DEV_NOT_ENABLED;
1246 		goto err;
1247 	}
1248 
1249 	if (wq->state != IDXD_WQ_DISABLED) {
1250 		dev_dbg(dev, "wq %d already enabled.\n", wq->id);
1251 		idxd->cmd_status = IDXD_SCMD_WQ_ENABLED;
1252 		rc = -EBUSY;
1253 		goto err;
1254 	}
1255 
1256 	if (!wq->group) {
1257 		dev_dbg(dev, "wq %d not attached to group.\n", wq->id);
1258 		idxd->cmd_status = IDXD_SCMD_WQ_NO_GRP;
1259 		goto err;
1260 	}
1261 
1262 	if (strlen(wq->name) == 0) {
1263 		idxd->cmd_status = IDXD_SCMD_WQ_NO_NAME;
1264 		dev_dbg(dev, "wq %d name not set.\n", wq->id);
1265 		goto err;
1266 	}
1267 
1268 	/* Shared WQ checks */
1269 	if (wq_shared(wq)) {
1270 		if (!wq_shared_supported(wq)) {
1271 			idxd->cmd_status = IDXD_SCMD_WQ_NO_SVM;
1272 			dev_dbg(dev, "PASID not enabled and shared wq.\n");
1273 			goto err;
1274 		}
1275 		/*
1276 		 * Shared wq with the threshold set to 0 means the user
1277 		 * did not set the threshold or transitioned from a
1278 		 * dedicated wq but did not set threshold. A value
1279 		 * of 0 would effectively disable the shared wq. The
1280 		 * driver does not allow a value of 0 to be set for
1281 		 * threshold via sysfs.
1282 		 */
1283 		if (wq->threshold == 0) {
1284 			idxd->cmd_status = IDXD_SCMD_WQ_NO_THRESH;
1285 			dev_dbg(dev, "Shared wq and threshold 0.\n");
1286 			goto err;
1287 		}
1288 	}
1289 
1290 	/*
1291 	 * In the event that the WQ is configurable for pasid and priv bits.
1292 	 * For kernel wq, the driver should setup the pasid, pasid_en, and priv bit.
1293 	 * However, for non-kernel wq, the driver should only set the pasid_en bit for
1294 	 * shared wq. A dedicated wq that is not 'kernel' type will configure pasid and
1295 	 * pasid_en later on so there is no need to setup.
1296 	 */
1297 	if (test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) {
1298 		int priv = 0;
1299 
1300 		if (wq_pasid_enabled(wq)) {
1301 			if (is_idxd_wq_kernel(wq) || wq_shared(wq)) {
1302 				u32 pasid = wq_dedicated(wq) ? idxd->pasid : 0;
1303 
1304 				__idxd_wq_set_pasid_locked(wq, pasid);
1305 			}
1306 		}
1307 
1308 		if (is_idxd_wq_kernel(wq))
1309 			priv = 1;
1310 		__idxd_wq_set_priv_locked(wq, priv);
1311 	}
1312 
1313 	rc = 0;
1314 	spin_lock(&idxd->dev_lock);
1315 	if (test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
1316 		rc = idxd_device_config(idxd);
1317 	spin_unlock(&idxd->dev_lock);
1318 	if (rc < 0) {
1319 		dev_dbg(dev, "Writing wq %d config failed: %d\n", wq->id, rc);
1320 		goto err;
1321 	}
1322 
1323 	rc = idxd_wq_enable(wq);
1324 	if (rc < 0) {
1325 		dev_dbg(dev, "wq %d enabling failed: %d\n", wq->id, rc);
1326 		goto err;
1327 	}
1328 
1329 	rc = idxd_wq_map_portal(wq);
1330 	if (rc < 0) {
1331 		idxd->cmd_status = IDXD_SCMD_WQ_PORTAL_ERR;
1332 		dev_dbg(dev, "wq %d portal mapping failed: %d\n", wq->id, rc);
1333 		goto err_map_portal;
1334 	}
1335 
1336 	wq->client_count = 0;
1337 
1338 	rc = idxd_wq_request_irq(wq);
1339 	if (rc < 0) {
1340 		idxd->cmd_status = IDXD_SCMD_WQ_IRQ_ERR;
1341 		dev_dbg(dev, "WQ %d irq setup failed: %d\n", wq->id, rc);
1342 		goto err_irq;
1343 	}
1344 
1345 	rc = idxd_wq_alloc_resources(wq);
1346 	if (rc < 0) {
1347 		idxd->cmd_status = IDXD_SCMD_WQ_RES_ALLOC_ERR;
1348 		dev_dbg(dev, "WQ resource alloc failed\n");
1349 		goto err_res_alloc;
1350 	}
1351 
1352 	rc = idxd_wq_init_percpu_ref(wq);
1353 	if (rc < 0) {
1354 		idxd->cmd_status = IDXD_SCMD_PERCPU_ERR;
1355 		dev_dbg(dev, "percpu_ref setup failed\n");
1356 		goto err_ref;
1357 	}
1358 
1359 	return 0;
1360 
1361 err_ref:
1362 	idxd_wq_free_resources(wq);
1363 err_res_alloc:
1364 	idxd_wq_free_irq(wq);
1365 err_irq:
1366 	idxd_wq_unmap_portal(wq);
1367 err_map_portal:
1368 	rc = idxd_wq_disable(wq, false);
1369 	if (rc < 0)
1370 		dev_dbg(dev, "wq %s disable failed\n", dev_name(wq_confdev(wq)));
1371 err:
1372 	return rc;
1373 }
1374 
1375 void drv_disable_wq(struct idxd_wq *wq)
1376 {
1377 	struct idxd_device *idxd = wq->idxd;
1378 	struct device *dev = &idxd->pdev->dev;
1379 
1380 	lockdep_assert_held(&wq->wq_lock);
1381 
1382 	if (idxd_wq_refcount(wq))
1383 		dev_warn(dev, "Clients has claim on wq %d: %d\n",
1384 			 wq->id, idxd_wq_refcount(wq));
1385 
1386 	idxd_wq_free_resources(wq);
1387 	idxd_wq_unmap_portal(wq);
1388 	idxd_wq_drain(wq);
1389 	idxd_wq_free_irq(wq);
1390 	idxd_wq_reset(wq);
1391 	percpu_ref_exit(&wq->wq_active);
1392 	wq->type = IDXD_WQT_NONE;
1393 	wq->client_count = 0;
1394 }
1395 
1396 int idxd_device_drv_probe(struct idxd_dev *idxd_dev)
1397 {
1398 	struct idxd_device *idxd = idxd_dev_to_idxd(idxd_dev);
1399 	int rc = 0;
1400 
1401 	/*
1402 	 * Device should be in disabled state for the idxd_drv to load. If it's in
1403 	 * enabled state, then the device was altered outside of driver's control.
1404 	 * If the state is in halted state, then we don't want to proceed.
1405 	 */
1406 	if (idxd->state != IDXD_DEV_DISABLED) {
1407 		idxd->cmd_status = IDXD_SCMD_DEV_ENABLED;
1408 		return -ENXIO;
1409 	}
1410 
1411 	/* Device configuration */
1412 	spin_lock(&idxd->dev_lock);
1413 	if (test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
1414 		rc = idxd_device_config(idxd);
1415 	spin_unlock(&idxd->dev_lock);
1416 	if (rc < 0)
1417 		return -ENXIO;
1418 
1419 	/* Start device */
1420 	rc = idxd_device_enable(idxd);
1421 	if (rc < 0)
1422 		return rc;
1423 
1424 	/* Setup DMA device without channels */
1425 	rc = idxd_register_dma_device(idxd);
1426 	if (rc < 0) {
1427 		idxd_device_disable(idxd);
1428 		idxd->cmd_status = IDXD_SCMD_DEV_DMA_ERR;
1429 		return rc;
1430 	}
1431 
1432 	idxd->cmd_status = 0;
1433 	return 0;
1434 }
1435 
1436 void idxd_device_drv_remove(struct idxd_dev *idxd_dev)
1437 {
1438 	struct device *dev = &idxd_dev->conf_dev;
1439 	struct idxd_device *idxd = idxd_dev_to_idxd(idxd_dev);
1440 	int i;
1441 
1442 	for (i = 0; i < idxd->max_wqs; i++) {
1443 		struct idxd_wq *wq = idxd->wqs[i];
1444 		struct device *wq_dev = wq_confdev(wq);
1445 
1446 		if (wq->state == IDXD_WQ_DISABLED)
1447 			continue;
1448 		dev_warn(dev, "Active wq %d on disable %s.\n", i, dev_name(wq_dev));
1449 		device_release_driver(wq_dev);
1450 	}
1451 
1452 	idxd_unregister_dma_device(idxd);
1453 	idxd_device_disable(idxd);
1454 	if (test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
1455 		idxd_device_reset(idxd);
1456 }
1457 
1458 static enum idxd_dev_type dev_types[] = {
1459 	IDXD_DEV_DSA,
1460 	IDXD_DEV_IAX,
1461 	IDXD_DEV_NONE,
1462 };
1463 
1464 struct idxd_device_driver idxd_drv = {
1465 	.type = dev_types,
1466 	.probe = idxd_device_drv_probe,
1467 	.remove = idxd_device_drv_remove,
1468 	.name = "idxd",
1469 };
1470 EXPORT_SYMBOL_GPL(idxd_drv);
1471