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