xref: /linux/drivers/dma/idxd/sysfs.c (revision af1f459233d4edeef634f559539e7f4b64cb1d25)
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/device.h>
8 #include <linux/io-64-nonatomic-lo-hi.h>
9 #include <uapi/linux/idxd.h>
10 #include "registers.h"
11 #include "idxd.h"
12 
13 static char *idxd_wq_type_names[] = {
14 	[IDXD_WQT_NONE]		= "none",
15 	[IDXD_WQT_KERNEL]	= "kernel",
16 	[IDXD_WQT_USER]		= "user",
17 };
18 
19 /* IDXD engine attributes */
20 static ssize_t engine_group_id_show(struct device *dev,
21 				    struct device_attribute *attr, char *buf)
22 {
23 	struct idxd_engine *engine = confdev_to_engine(dev);
24 
25 	if (engine->group)
26 		return sysfs_emit(buf, "%d\n", engine->group->id);
27 	else
28 		return sysfs_emit(buf, "%d\n", -1);
29 }
30 
31 static ssize_t engine_group_id_store(struct device *dev,
32 				     struct device_attribute *attr,
33 				     const char *buf, size_t count)
34 {
35 	struct idxd_engine *engine = confdev_to_engine(dev);
36 	struct idxd_device *idxd = engine->idxd;
37 	long id;
38 	int rc;
39 	struct idxd_group *prevg;
40 
41 	rc = kstrtol(buf, 10, &id);
42 	if (rc < 0)
43 		return -EINVAL;
44 
45 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
46 		return -EPERM;
47 
48 	if (id > idxd->max_groups - 1 || id < -1)
49 		return -EINVAL;
50 
51 	if (id == -1) {
52 		if (engine->group) {
53 			engine->group->num_engines--;
54 			engine->group = NULL;
55 		}
56 		return count;
57 	}
58 
59 	prevg = engine->group;
60 
61 	if (prevg)
62 		prevg->num_engines--;
63 	engine->group = idxd->groups[id];
64 	engine->group->num_engines++;
65 
66 	return count;
67 }
68 
69 static struct device_attribute dev_attr_engine_group =
70 		__ATTR(group_id, 0644, engine_group_id_show,
71 		       engine_group_id_store);
72 
73 static struct attribute *idxd_engine_attributes[] = {
74 	&dev_attr_engine_group.attr,
75 	NULL,
76 };
77 
78 static const struct attribute_group idxd_engine_attribute_group = {
79 	.attrs = idxd_engine_attributes,
80 };
81 
82 static const struct attribute_group *idxd_engine_attribute_groups[] = {
83 	&idxd_engine_attribute_group,
84 	NULL,
85 };
86 
87 static void idxd_conf_engine_release(struct device *dev)
88 {
89 	struct idxd_engine *engine = confdev_to_engine(dev);
90 
91 	kfree(engine);
92 }
93 
94 struct device_type idxd_engine_device_type = {
95 	.name = "engine",
96 	.release = idxd_conf_engine_release,
97 	.groups = idxd_engine_attribute_groups,
98 };
99 
100 /* Group attributes */
101 
102 static void idxd_set_free_rdbufs(struct idxd_device *idxd)
103 {
104 	int i, rdbufs;
105 
106 	for (i = 0, rdbufs = 0; i < idxd->max_groups; i++) {
107 		struct idxd_group *g = idxd->groups[i];
108 
109 		rdbufs += g->rdbufs_reserved;
110 	}
111 
112 	idxd->nr_rdbufs = idxd->max_rdbufs - rdbufs;
113 }
114 
115 static ssize_t group_read_buffers_reserved_show(struct device *dev,
116 						struct device_attribute *attr,
117 						char *buf)
118 {
119 	struct idxd_group *group = confdev_to_group(dev);
120 
121 	return sysfs_emit(buf, "%u\n", group->rdbufs_reserved);
122 }
123 
124 static ssize_t group_tokens_reserved_show(struct device *dev,
125 					  struct device_attribute *attr,
126 					  char *buf)
127 {
128 	dev_warn_once(dev, "attribute deprecated, see read_buffers_reserved.\n");
129 	return group_read_buffers_reserved_show(dev, attr, buf);
130 }
131 
132 static ssize_t group_read_buffers_reserved_store(struct device *dev,
133 						 struct device_attribute *attr,
134 						 const char *buf, size_t count)
135 {
136 	struct idxd_group *group = confdev_to_group(dev);
137 	struct idxd_device *idxd = group->idxd;
138 	unsigned long val;
139 	int rc;
140 
141 	rc = kstrtoul(buf, 10, &val);
142 	if (rc < 0)
143 		return -EINVAL;
144 
145 	if (idxd->data->type == IDXD_TYPE_IAX)
146 		return -EOPNOTSUPP;
147 
148 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
149 		return -EPERM;
150 
151 	if (idxd->state == IDXD_DEV_ENABLED)
152 		return -EPERM;
153 
154 	if (val > idxd->max_rdbufs)
155 		return -EINVAL;
156 
157 	if (val > idxd->nr_rdbufs + group->rdbufs_reserved)
158 		return -EINVAL;
159 
160 	group->rdbufs_reserved = val;
161 	idxd_set_free_rdbufs(idxd);
162 	return count;
163 }
164 
165 static ssize_t group_tokens_reserved_store(struct device *dev,
166 					   struct device_attribute *attr,
167 					   const char *buf, size_t count)
168 {
169 	dev_warn_once(dev, "attribute deprecated, see read_buffers_reserved.\n");
170 	return group_read_buffers_reserved_store(dev, attr, buf, count);
171 }
172 
173 static struct device_attribute dev_attr_group_tokens_reserved =
174 		__ATTR(tokens_reserved, 0644, group_tokens_reserved_show,
175 		       group_tokens_reserved_store);
176 
177 static struct device_attribute dev_attr_group_read_buffers_reserved =
178 		__ATTR(read_buffers_reserved, 0644, group_read_buffers_reserved_show,
179 		       group_read_buffers_reserved_store);
180 
181 static ssize_t group_read_buffers_allowed_show(struct device *dev,
182 					       struct device_attribute *attr,
183 					       char *buf)
184 {
185 	struct idxd_group *group = confdev_to_group(dev);
186 
187 	return sysfs_emit(buf, "%u\n", group->rdbufs_allowed);
188 }
189 
190 static ssize_t group_tokens_allowed_show(struct device *dev,
191 					 struct device_attribute *attr,
192 					 char *buf)
193 {
194 	dev_warn_once(dev, "attribute deprecated, see read_buffers_allowed.\n");
195 	return group_read_buffers_allowed_show(dev, attr, buf);
196 }
197 
198 static ssize_t group_read_buffers_allowed_store(struct device *dev,
199 						struct device_attribute *attr,
200 						const char *buf, size_t count)
201 {
202 	struct idxd_group *group = confdev_to_group(dev);
203 	struct idxd_device *idxd = group->idxd;
204 	unsigned long val;
205 	int rc;
206 
207 	rc = kstrtoul(buf, 10, &val);
208 	if (rc < 0)
209 		return -EINVAL;
210 
211 	if (idxd->data->type == IDXD_TYPE_IAX)
212 		return -EOPNOTSUPP;
213 
214 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
215 		return -EPERM;
216 
217 	if (idxd->state == IDXD_DEV_ENABLED)
218 		return -EPERM;
219 
220 	if (val < 4 * group->num_engines ||
221 	    val > group->rdbufs_reserved + idxd->nr_rdbufs)
222 		return -EINVAL;
223 
224 	group->rdbufs_allowed = val;
225 	return count;
226 }
227 
228 static ssize_t group_tokens_allowed_store(struct device *dev,
229 					  struct device_attribute *attr,
230 					  const char *buf, size_t count)
231 {
232 	dev_warn_once(dev, "attribute deprecated, see read_buffers_allowed.\n");
233 	return group_read_buffers_allowed_store(dev, attr, buf, count);
234 }
235 
236 static struct device_attribute dev_attr_group_tokens_allowed =
237 		__ATTR(tokens_allowed, 0644, group_tokens_allowed_show,
238 		       group_tokens_allowed_store);
239 
240 static struct device_attribute dev_attr_group_read_buffers_allowed =
241 		__ATTR(read_buffers_allowed, 0644, group_read_buffers_allowed_show,
242 		       group_read_buffers_allowed_store);
243 
244 static ssize_t group_use_read_buffer_limit_show(struct device *dev,
245 						struct device_attribute *attr,
246 						char *buf)
247 {
248 	struct idxd_group *group = confdev_to_group(dev);
249 
250 	return sysfs_emit(buf, "%u\n", group->use_rdbuf_limit);
251 }
252 
253 static ssize_t group_use_token_limit_show(struct device *dev,
254 					  struct device_attribute *attr,
255 					  char *buf)
256 {
257 	dev_warn_once(dev, "attribute deprecated, see use_read_buffer_limit.\n");
258 	return group_use_read_buffer_limit_show(dev, attr, buf);
259 }
260 
261 static ssize_t group_use_read_buffer_limit_store(struct device *dev,
262 						 struct device_attribute *attr,
263 						 const char *buf, size_t count)
264 {
265 	struct idxd_group *group = confdev_to_group(dev);
266 	struct idxd_device *idxd = group->idxd;
267 	unsigned long val;
268 	int rc;
269 
270 	rc = kstrtoul(buf, 10, &val);
271 	if (rc < 0)
272 		return -EINVAL;
273 
274 	if (idxd->data->type == IDXD_TYPE_IAX)
275 		return -EOPNOTSUPP;
276 
277 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
278 		return -EPERM;
279 
280 	if (idxd->state == IDXD_DEV_ENABLED)
281 		return -EPERM;
282 
283 	if (idxd->rdbuf_limit == 0)
284 		return -EPERM;
285 
286 	group->use_rdbuf_limit = !!val;
287 	return count;
288 }
289 
290 static ssize_t group_use_token_limit_store(struct device *dev,
291 					   struct device_attribute *attr,
292 					   const char *buf, size_t count)
293 {
294 	dev_warn_once(dev, "attribute deprecated, see use_read_buffer_limit.\n");
295 	return group_use_read_buffer_limit_store(dev, attr, buf, count);
296 }
297 
298 static struct device_attribute dev_attr_group_use_token_limit =
299 		__ATTR(use_token_limit, 0644, group_use_token_limit_show,
300 		       group_use_token_limit_store);
301 
302 static struct device_attribute dev_attr_group_use_read_buffer_limit =
303 		__ATTR(use_read_buffer_limit, 0644, group_use_read_buffer_limit_show,
304 		       group_use_read_buffer_limit_store);
305 
306 static ssize_t group_engines_show(struct device *dev,
307 				  struct device_attribute *attr, char *buf)
308 {
309 	struct idxd_group *group = confdev_to_group(dev);
310 	int i, rc = 0;
311 	struct idxd_device *idxd = group->idxd;
312 
313 	for (i = 0; i < idxd->max_engines; i++) {
314 		struct idxd_engine *engine = idxd->engines[i];
315 
316 		if (!engine->group)
317 			continue;
318 
319 		if (engine->group->id == group->id)
320 			rc += sysfs_emit_at(buf, rc, "engine%d.%d ", idxd->id, engine->id);
321 	}
322 
323 	if (!rc)
324 		return 0;
325 	rc--;
326 	rc += sysfs_emit_at(buf, rc, "\n");
327 
328 	return rc;
329 }
330 
331 static struct device_attribute dev_attr_group_engines =
332 		__ATTR(engines, 0444, group_engines_show, NULL);
333 
334 static ssize_t group_work_queues_show(struct device *dev,
335 				      struct device_attribute *attr, char *buf)
336 {
337 	struct idxd_group *group = confdev_to_group(dev);
338 	int i, rc = 0;
339 	struct idxd_device *idxd = group->idxd;
340 
341 	for (i = 0; i < idxd->max_wqs; i++) {
342 		struct idxd_wq *wq = idxd->wqs[i];
343 
344 		if (!wq->group)
345 			continue;
346 
347 		if (wq->group->id == group->id)
348 			rc += sysfs_emit_at(buf, rc, "wq%d.%d ", idxd->id, wq->id);
349 	}
350 
351 	if (!rc)
352 		return 0;
353 	rc--;
354 	rc += sysfs_emit_at(buf, rc, "\n");
355 
356 	return rc;
357 }
358 
359 static struct device_attribute dev_attr_group_work_queues =
360 		__ATTR(work_queues, 0444, group_work_queues_show, NULL);
361 
362 static ssize_t group_traffic_class_a_show(struct device *dev,
363 					  struct device_attribute *attr,
364 					  char *buf)
365 {
366 	struct idxd_group *group = confdev_to_group(dev);
367 
368 	return sysfs_emit(buf, "%d\n", group->tc_a);
369 }
370 
371 static ssize_t group_traffic_class_a_store(struct device *dev,
372 					   struct device_attribute *attr,
373 					   const char *buf, size_t count)
374 {
375 	struct idxd_group *group = confdev_to_group(dev);
376 	struct idxd_device *idxd = group->idxd;
377 	long val;
378 	int rc;
379 
380 	rc = kstrtol(buf, 10, &val);
381 	if (rc < 0)
382 		return -EINVAL;
383 
384 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
385 		return -EPERM;
386 
387 	if (idxd->state == IDXD_DEV_ENABLED)
388 		return -EPERM;
389 
390 	if (idxd->hw.version <= DEVICE_VERSION_2 && !tc_override)
391 		return -EPERM;
392 
393 	if (val < 0 || val > 7)
394 		return -EINVAL;
395 
396 	group->tc_a = val;
397 	return count;
398 }
399 
400 static struct device_attribute dev_attr_group_traffic_class_a =
401 		__ATTR(traffic_class_a, 0644, group_traffic_class_a_show,
402 		       group_traffic_class_a_store);
403 
404 static ssize_t group_traffic_class_b_show(struct device *dev,
405 					  struct device_attribute *attr,
406 					  char *buf)
407 {
408 	struct idxd_group *group = confdev_to_group(dev);
409 
410 	return sysfs_emit(buf, "%d\n", group->tc_b);
411 }
412 
413 static ssize_t group_traffic_class_b_store(struct device *dev,
414 					   struct device_attribute *attr,
415 					   const char *buf, size_t count)
416 {
417 	struct idxd_group *group = confdev_to_group(dev);
418 	struct idxd_device *idxd = group->idxd;
419 	long val;
420 	int rc;
421 
422 	rc = kstrtol(buf, 10, &val);
423 	if (rc < 0)
424 		return -EINVAL;
425 
426 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
427 		return -EPERM;
428 
429 	if (idxd->state == IDXD_DEV_ENABLED)
430 		return -EPERM;
431 
432 	if (idxd->hw.version <= DEVICE_VERSION_2 && !tc_override)
433 		return -EPERM;
434 
435 	if (val < 0 || val > 7)
436 		return -EINVAL;
437 
438 	group->tc_b = val;
439 	return count;
440 }
441 
442 static struct device_attribute dev_attr_group_traffic_class_b =
443 		__ATTR(traffic_class_b, 0644, group_traffic_class_b_show,
444 		       group_traffic_class_b_store);
445 
446 static ssize_t group_desc_progress_limit_show(struct device *dev,
447 					      struct device_attribute *attr,
448 					      char *buf)
449 {
450 	struct idxd_group *group = confdev_to_group(dev);
451 
452 	return sysfs_emit(buf, "%d\n", group->desc_progress_limit);
453 }
454 
455 static ssize_t group_desc_progress_limit_store(struct device *dev,
456 					       struct device_attribute *attr,
457 					       const char *buf, size_t count)
458 {
459 	struct idxd_group *group = confdev_to_group(dev);
460 	int val, rc;
461 
462 	rc = kstrtoint(buf, 10, &val);
463 	if (rc < 0)
464 		return -EINVAL;
465 
466 	if (val & ~GENMASK(1, 0))
467 		return -EINVAL;
468 
469 	group->desc_progress_limit = val;
470 	return count;
471 }
472 
473 static struct device_attribute dev_attr_group_desc_progress_limit =
474 		__ATTR(desc_progress_limit, 0644, group_desc_progress_limit_show,
475 		       group_desc_progress_limit_store);
476 
477 static ssize_t group_batch_progress_limit_show(struct device *dev,
478 					       struct device_attribute *attr,
479 					       char *buf)
480 {
481 	struct idxd_group *group = confdev_to_group(dev);
482 
483 	return sysfs_emit(buf, "%d\n", group->batch_progress_limit);
484 }
485 
486 static ssize_t group_batch_progress_limit_store(struct device *dev,
487 						struct device_attribute *attr,
488 						const char *buf, size_t count)
489 {
490 	struct idxd_group *group = confdev_to_group(dev);
491 	int val, rc;
492 
493 	rc = kstrtoint(buf, 10, &val);
494 	if (rc < 0)
495 		return -EINVAL;
496 
497 	if (val & ~GENMASK(1, 0))
498 		return -EINVAL;
499 
500 	group->batch_progress_limit = val;
501 	return count;
502 }
503 
504 static struct device_attribute dev_attr_group_batch_progress_limit =
505 		__ATTR(batch_progress_limit, 0644, group_batch_progress_limit_show,
506 		       group_batch_progress_limit_store);
507 static struct attribute *idxd_group_attributes[] = {
508 	&dev_attr_group_work_queues.attr,
509 	&dev_attr_group_engines.attr,
510 	&dev_attr_group_use_token_limit.attr,
511 	&dev_attr_group_use_read_buffer_limit.attr,
512 	&dev_attr_group_tokens_allowed.attr,
513 	&dev_attr_group_read_buffers_allowed.attr,
514 	&dev_attr_group_tokens_reserved.attr,
515 	&dev_attr_group_read_buffers_reserved.attr,
516 	&dev_attr_group_traffic_class_a.attr,
517 	&dev_attr_group_traffic_class_b.attr,
518 	&dev_attr_group_desc_progress_limit.attr,
519 	&dev_attr_group_batch_progress_limit.attr,
520 	NULL,
521 };
522 
523 static bool idxd_group_attr_progress_limit_invisible(struct attribute *attr,
524 						     struct idxd_device *idxd)
525 {
526 	return (attr == &dev_attr_group_desc_progress_limit.attr ||
527 		attr == &dev_attr_group_batch_progress_limit.attr) &&
528 		!idxd->hw.group_cap.progress_limit;
529 }
530 
531 static bool idxd_group_attr_read_buffers_invisible(struct attribute *attr,
532 						   struct idxd_device *idxd)
533 {
534 	/*
535 	 * Intel IAA does not support Read Buffer allocation control,
536 	 * make these attributes invisible.
537 	 */
538 	return (attr == &dev_attr_group_use_token_limit.attr ||
539 		attr == &dev_attr_group_use_read_buffer_limit.attr ||
540 		attr == &dev_attr_group_tokens_allowed.attr ||
541 		attr == &dev_attr_group_read_buffers_allowed.attr ||
542 		attr == &dev_attr_group_tokens_reserved.attr ||
543 		attr == &dev_attr_group_read_buffers_reserved.attr) &&
544 		idxd->data->type == IDXD_TYPE_IAX;
545 }
546 
547 static umode_t idxd_group_attr_visible(struct kobject *kobj,
548 				       struct attribute *attr, int n)
549 {
550 	struct device *dev = container_of(kobj, struct device, kobj);
551 	struct idxd_group *group = confdev_to_group(dev);
552 	struct idxd_device *idxd = group->idxd;
553 
554 	if (idxd_group_attr_progress_limit_invisible(attr, idxd))
555 		return 0;
556 
557 	if (idxd_group_attr_read_buffers_invisible(attr, idxd))
558 		return 0;
559 
560 	return attr->mode;
561 }
562 
563 static const struct attribute_group idxd_group_attribute_group = {
564 	.attrs = idxd_group_attributes,
565 	.is_visible = idxd_group_attr_visible,
566 };
567 
568 static const struct attribute_group *idxd_group_attribute_groups[] = {
569 	&idxd_group_attribute_group,
570 	NULL,
571 };
572 
573 static void idxd_conf_group_release(struct device *dev)
574 {
575 	struct idxd_group *group = confdev_to_group(dev);
576 
577 	kfree(group);
578 }
579 
580 struct device_type idxd_group_device_type = {
581 	.name = "group",
582 	.release = idxd_conf_group_release,
583 	.groups = idxd_group_attribute_groups,
584 };
585 
586 /* IDXD work queue attribs */
587 static ssize_t wq_clients_show(struct device *dev,
588 			       struct device_attribute *attr, char *buf)
589 {
590 	struct idxd_wq *wq = confdev_to_wq(dev);
591 
592 	return sysfs_emit(buf, "%d\n", wq->client_count);
593 }
594 
595 static struct device_attribute dev_attr_wq_clients =
596 		__ATTR(clients, 0444, wq_clients_show, NULL);
597 
598 static ssize_t wq_state_show(struct device *dev,
599 			     struct device_attribute *attr, char *buf)
600 {
601 	struct idxd_wq *wq = confdev_to_wq(dev);
602 
603 	switch (wq->state) {
604 	case IDXD_WQ_DISABLED:
605 		return sysfs_emit(buf, "disabled\n");
606 	case IDXD_WQ_ENABLED:
607 		return sysfs_emit(buf, "enabled\n");
608 	}
609 
610 	return sysfs_emit(buf, "unknown\n");
611 }
612 
613 static struct device_attribute dev_attr_wq_state =
614 		__ATTR(state, 0444, wq_state_show, NULL);
615 
616 static ssize_t wq_group_id_show(struct device *dev,
617 				struct device_attribute *attr, char *buf)
618 {
619 	struct idxd_wq *wq = confdev_to_wq(dev);
620 
621 	if (wq->group)
622 		return sysfs_emit(buf, "%u\n", wq->group->id);
623 	else
624 		return sysfs_emit(buf, "-1\n");
625 }
626 
627 static ssize_t wq_group_id_store(struct device *dev,
628 				 struct device_attribute *attr,
629 				 const char *buf, size_t count)
630 {
631 	struct idxd_wq *wq = confdev_to_wq(dev);
632 	struct idxd_device *idxd = wq->idxd;
633 	long id;
634 	int rc;
635 	struct idxd_group *prevg, *group;
636 
637 	rc = kstrtol(buf, 10, &id);
638 	if (rc < 0)
639 		return -EINVAL;
640 
641 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
642 		return -EPERM;
643 
644 	if (wq->state != IDXD_WQ_DISABLED)
645 		return -EPERM;
646 
647 	if (id > idxd->max_groups - 1 || id < -1)
648 		return -EINVAL;
649 
650 	if (id == -1) {
651 		if (wq->group) {
652 			wq->group->num_wqs--;
653 			wq->group = NULL;
654 		}
655 		return count;
656 	}
657 
658 	group = idxd->groups[id];
659 	prevg = wq->group;
660 
661 	if (prevg)
662 		prevg->num_wqs--;
663 	wq->group = group;
664 	group->num_wqs++;
665 	return count;
666 }
667 
668 static struct device_attribute dev_attr_wq_group_id =
669 		__ATTR(group_id, 0644, wq_group_id_show, wq_group_id_store);
670 
671 static ssize_t wq_mode_show(struct device *dev, struct device_attribute *attr,
672 			    char *buf)
673 {
674 	struct idxd_wq *wq = confdev_to_wq(dev);
675 
676 	return sysfs_emit(buf, "%s\n", wq_dedicated(wq) ? "dedicated" : "shared");
677 }
678 
679 static ssize_t wq_mode_store(struct device *dev,
680 			     struct device_attribute *attr, const char *buf,
681 			     size_t count)
682 {
683 	struct idxd_wq *wq = confdev_to_wq(dev);
684 	struct idxd_device *idxd = wq->idxd;
685 
686 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
687 		return -EPERM;
688 
689 	if (wq->state != IDXD_WQ_DISABLED)
690 		return -EPERM;
691 
692 	if (sysfs_streq(buf, "dedicated")) {
693 		set_bit(WQ_FLAG_DEDICATED, &wq->flags);
694 		wq->threshold = 0;
695 	} else if (sysfs_streq(buf, "shared")) {
696 		clear_bit(WQ_FLAG_DEDICATED, &wq->flags);
697 	} else {
698 		return -EINVAL;
699 	}
700 
701 	return count;
702 }
703 
704 static struct device_attribute dev_attr_wq_mode =
705 		__ATTR(mode, 0644, wq_mode_show, wq_mode_store);
706 
707 static ssize_t wq_size_show(struct device *dev, struct device_attribute *attr,
708 			    char *buf)
709 {
710 	struct idxd_wq *wq = confdev_to_wq(dev);
711 
712 	return sysfs_emit(buf, "%u\n", wq->size);
713 }
714 
715 static int total_claimed_wq_size(struct idxd_device *idxd)
716 {
717 	int i;
718 	int wq_size = 0;
719 
720 	for (i = 0; i < idxd->max_wqs; i++) {
721 		struct idxd_wq *wq = idxd->wqs[i];
722 
723 		wq_size += wq->size;
724 	}
725 
726 	return wq_size;
727 }
728 
729 static ssize_t wq_size_store(struct device *dev,
730 			     struct device_attribute *attr, const char *buf,
731 			     size_t count)
732 {
733 	struct idxd_wq *wq = confdev_to_wq(dev);
734 	unsigned long size;
735 	struct idxd_device *idxd = wq->idxd;
736 	int rc;
737 
738 	rc = kstrtoul(buf, 10, &size);
739 	if (rc < 0)
740 		return -EINVAL;
741 
742 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
743 		return -EPERM;
744 
745 	if (idxd->state == IDXD_DEV_ENABLED)
746 		return -EPERM;
747 
748 	if (size + total_claimed_wq_size(idxd) - wq->size > idxd->max_wq_size)
749 		return -EINVAL;
750 
751 	wq->size = size;
752 	return count;
753 }
754 
755 static struct device_attribute dev_attr_wq_size =
756 		__ATTR(size, 0644, wq_size_show, wq_size_store);
757 
758 static ssize_t wq_priority_show(struct device *dev,
759 				struct device_attribute *attr, char *buf)
760 {
761 	struct idxd_wq *wq = confdev_to_wq(dev);
762 
763 	return sysfs_emit(buf, "%u\n", wq->priority);
764 }
765 
766 static ssize_t wq_priority_store(struct device *dev,
767 				 struct device_attribute *attr,
768 				 const char *buf, size_t count)
769 {
770 	struct idxd_wq *wq = confdev_to_wq(dev);
771 	unsigned long prio;
772 	struct idxd_device *idxd = wq->idxd;
773 	int rc;
774 
775 	rc = kstrtoul(buf, 10, &prio);
776 	if (rc < 0)
777 		return -EINVAL;
778 
779 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
780 		return -EPERM;
781 
782 	if (wq->state != IDXD_WQ_DISABLED)
783 		return -EPERM;
784 
785 	if (prio > IDXD_MAX_PRIORITY)
786 		return -EINVAL;
787 
788 	wq->priority = prio;
789 	return count;
790 }
791 
792 static struct device_attribute dev_attr_wq_priority =
793 		__ATTR(priority, 0644, wq_priority_show, wq_priority_store);
794 
795 static ssize_t wq_block_on_fault_show(struct device *dev,
796 				      struct device_attribute *attr, char *buf)
797 {
798 	struct idxd_wq *wq = confdev_to_wq(dev);
799 
800 	return sysfs_emit(buf, "%u\n", test_bit(WQ_FLAG_BLOCK_ON_FAULT, &wq->flags));
801 }
802 
803 static ssize_t wq_block_on_fault_store(struct device *dev,
804 				       struct device_attribute *attr,
805 				       const char *buf, size_t count)
806 {
807 	struct idxd_wq *wq = confdev_to_wq(dev);
808 	struct idxd_device *idxd = wq->idxd;
809 	bool bof;
810 	int rc;
811 
812 	if (!idxd->hw.gen_cap.block_on_fault)
813 		return -EOPNOTSUPP;
814 
815 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
816 		return -EPERM;
817 
818 	if (wq->state != IDXD_WQ_DISABLED)
819 		return -ENXIO;
820 
821 	rc = kstrtobool(buf, &bof);
822 	if (rc < 0)
823 		return rc;
824 
825 	if (bof) {
826 		if (test_bit(WQ_FLAG_PRS_DISABLE, &wq->flags))
827 			return -EOPNOTSUPP;
828 
829 		set_bit(WQ_FLAG_BLOCK_ON_FAULT, &wq->flags);
830 	} else {
831 		clear_bit(WQ_FLAG_BLOCK_ON_FAULT, &wq->flags);
832 	}
833 
834 	return count;
835 }
836 
837 static struct device_attribute dev_attr_wq_block_on_fault =
838 		__ATTR(block_on_fault, 0644, wq_block_on_fault_show,
839 		       wq_block_on_fault_store);
840 
841 static ssize_t wq_threshold_show(struct device *dev,
842 				 struct device_attribute *attr, char *buf)
843 {
844 	struct idxd_wq *wq = confdev_to_wq(dev);
845 
846 	return sysfs_emit(buf, "%u\n", wq->threshold);
847 }
848 
849 static ssize_t wq_threshold_store(struct device *dev,
850 				  struct device_attribute *attr,
851 				  const char *buf, size_t count)
852 {
853 	struct idxd_wq *wq = confdev_to_wq(dev);
854 	struct idxd_device *idxd = wq->idxd;
855 	unsigned int val;
856 	int rc;
857 
858 	rc = kstrtouint(buf, 0, &val);
859 	if (rc < 0)
860 		return -EINVAL;
861 
862 	if (val > wq->size || val <= 0)
863 		return -EINVAL;
864 
865 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
866 		return -EPERM;
867 
868 	if (wq->state != IDXD_WQ_DISABLED)
869 		return -ENXIO;
870 
871 	if (test_bit(WQ_FLAG_DEDICATED, &wq->flags))
872 		return -EINVAL;
873 
874 	wq->threshold = val;
875 
876 	return count;
877 }
878 
879 static struct device_attribute dev_attr_wq_threshold =
880 		__ATTR(threshold, 0644, wq_threshold_show, wq_threshold_store);
881 
882 static ssize_t wq_type_show(struct device *dev,
883 			    struct device_attribute *attr, char *buf)
884 {
885 	struct idxd_wq *wq = confdev_to_wq(dev);
886 
887 	switch (wq->type) {
888 	case IDXD_WQT_KERNEL:
889 		return sysfs_emit(buf, "%s\n", idxd_wq_type_names[IDXD_WQT_KERNEL]);
890 	case IDXD_WQT_USER:
891 		return sysfs_emit(buf, "%s\n", idxd_wq_type_names[IDXD_WQT_USER]);
892 	case IDXD_WQT_NONE:
893 	default:
894 		return sysfs_emit(buf, "%s\n", idxd_wq_type_names[IDXD_WQT_NONE]);
895 	}
896 
897 	return -EINVAL;
898 }
899 
900 static ssize_t wq_type_store(struct device *dev,
901 			     struct device_attribute *attr, const char *buf,
902 			     size_t count)
903 {
904 	struct idxd_wq *wq = confdev_to_wq(dev);
905 	enum idxd_wq_type old_type;
906 
907 	if (wq->state != IDXD_WQ_DISABLED)
908 		return -EPERM;
909 
910 	old_type = wq->type;
911 	if (sysfs_streq(buf, idxd_wq_type_names[IDXD_WQT_NONE]))
912 		wq->type = IDXD_WQT_NONE;
913 	else if (sysfs_streq(buf, idxd_wq_type_names[IDXD_WQT_KERNEL]))
914 		wq->type = IDXD_WQT_KERNEL;
915 	else if (sysfs_streq(buf, idxd_wq_type_names[IDXD_WQT_USER]))
916 		wq->type = IDXD_WQT_USER;
917 	else
918 		return -EINVAL;
919 
920 	/* If we are changing queue type, clear the name */
921 	if (wq->type != old_type)
922 		memset(wq->name, 0, WQ_NAME_SIZE + 1);
923 
924 	return count;
925 }
926 
927 static struct device_attribute dev_attr_wq_type =
928 		__ATTR(type, 0644, wq_type_show, wq_type_store);
929 
930 static ssize_t wq_name_show(struct device *dev,
931 			    struct device_attribute *attr, char *buf)
932 {
933 	struct idxd_wq *wq = confdev_to_wq(dev);
934 
935 	return sysfs_emit(buf, "%s\n", wq->name);
936 }
937 
938 static ssize_t wq_name_store(struct device *dev,
939 			     struct device_attribute *attr, const char *buf,
940 			     size_t count)
941 {
942 	struct idxd_wq *wq = confdev_to_wq(dev);
943 	char *input, *pos;
944 
945 	if (wq->state != IDXD_WQ_DISABLED)
946 		return -EPERM;
947 
948 	if (strlen(buf) > WQ_NAME_SIZE || strlen(buf) == 0)
949 		return -EINVAL;
950 
951 	input = kstrndup(buf, count, GFP_KERNEL);
952 	if (!input)
953 		return -ENOMEM;
954 
955 	pos = strim(input);
956 	memset(wq->name, 0, WQ_NAME_SIZE + 1);
957 	sprintf(wq->name, "%s", pos);
958 	kfree(input);
959 	return count;
960 }
961 
962 static struct device_attribute dev_attr_wq_name =
963 		__ATTR(name, 0644, wq_name_show, wq_name_store);
964 
965 static ssize_t wq_cdev_minor_show(struct device *dev,
966 				  struct device_attribute *attr, char *buf)
967 {
968 	struct idxd_wq *wq = confdev_to_wq(dev);
969 	int minor = -1;
970 
971 	mutex_lock(&wq->wq_lock);
972 	if (wq->idxd_cdev)
973 		minor = wq->idxd_cdev->minor;
974 	mutex_unlock(&wq->wq_lock);
975 
976 	if (minor == -1)
977 		return -ENXIO;
978 	return sysfs_emit(buf, "%d\n", minor);
979 }
980 
981 static struct device_attribute dev_attr_wq_cdev_minor =
982 		__ATTR(cdev_minor, 0444, wq_cdev_minor_show, NULL);
983 
984 static int __get_sysfs_u64(const char *buf, u64 *val)
985 {
986 	int rc;
987 
988 	rc = kstrtou64(buf, 0, val);
989 	if (rc < 0)
990 		return -EINVAL;
991 
992 	if (*val == 0)
993 		return -EINVAL;
994 
995 	*val = roundup_pow_of_two(*val);
996 	return 0;
997 }
998 
999 static ssize_t wq_max_transfer_size_show(struct device *dev, struct device_attribute *attr,
1000 					 char *buf)
1001 {
1002 	struct idxd_wq *wq = confdev_to_wq(dev);
1003 
1004 	return sysfs_emit(buf, "%llu\n", wq->max_xfer_bytes);
1005 }
1006 
1007 static ssize_t wq_max_transfer_size_store(struct device *dev, struct device_attribute *attr,
1008 					  const char *buf, size_t count)
1009 {
1010 	struct idxd_wq *wq = confdev_to_wq(dev);
1011 	struct idxd_device *idxd = wq->idxd;
1012 	u64 xfer_size;
1013 	int rc;
1014 
1015 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
1016 		return -EPERM;
1017 
1018 	if (wq->state != IDXD_WQ_DISABLED)
1019 		return -EPERM;
1020 
1021 	rc = __get_sysfs_u64(buf, &xfer_size);
1022 	if (rc < 0)
1023 		return rc;
1024 
1025 	if (xfer_size > idxd->max_xfer_bytes)
1026 		return -EINVAL;
1027 
1028 	wq->max_xfer_bytes = xfer_size;
1029 
1030 	return count;
1031 }
1032 
1033 static struct device_attribute dev_attr_wq_max_transfer_size =
1034 		__ATTR(max_transfer_size, 0644,
1035 		       wq_max_transfer_size_show, wq_max_transfer_size_store);
1036 
1037 static ssize_t wq_max_batch_size_show(struct device *dev, struct device_attribute *attr, char *buf)
1038 {
1039 	struct idxd_wq *wq = confdev_to_wq(dev);
1040 
1041 	return sysfs_emit(buf, "%u\n", wq->max_batch_size);
1042 }
1043 
1044 static ssize_t wq_max_batch_size_store(struct device *dev, struct device_attribute *attr,
1045 				       const char *buf, size_t count)
1046 {
1047 	struct idxd_wq *wq = confdev_to_wq(dev);
1048 	struct idxd_device *idxd = wq->idxd;
1049 	u64 batch_size;
1050 	int rc;
1051 
1052 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
1053 		return -EPERM;
1054 
1055 	if (wq->state != IDXD_WQ_DISABLED)
1056 		return -EPERM;
1057 
1058 	rc = __get_sysfs_u64(buf, &batch_size);
1059 	if (rc < 0)
1060 		return rc;
1061 
1062 	if (batch_size > idxd->max_batch_size)
1063 		return -EINVAL;
1064 
1065 	idxd_wq_set_max_batch_size(idxd->data->type, wq, (u32)batch_size);
1066 
1067 	return count;
1068 }
1069 
1070 static struct device_attribute dev_attr_wq_max_batch_size =
1071 		__ATTR(max_batch_size, 0644, wq_max_batch_size_show, wq_max_batch_size_store);
1072 
1073 static ssize_t wq_ats_disable_show(struct device *dev, struct device_attribute *attr, char *buf)
1074 {
1075 	struct idxd_wq *wq = confdev_to_wq(dev);
1076 
1077 	return sysfs_emit(buf, "%u\n", test_bit(WQ_FLAG_ATS_DISABLE, &wq->flags));
1078 }
1079 
1080 static ssize_t wq_ats_disable_store(struct device *dev, struct device_attribute *attr,
1081 				    const char *buf, size_t count)
1082 {
1083 	struct idxd_wq *wq = confdev_to_wq(dev);
1084 	struct idxd_device *idxd = wq->idxd;
1085 	bool ats_dis;
1086 	int rc;
1087 
1088 	if (wq->state != IDXD_WQ_DISABLED)
1089 		return -EPERM;
1090 
1091 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
1092 		return -EPERM;
1093 
1094 	rc = kstrtobool(buf, &ats_dis);
1095 	if (rc < 0)
1096 		return rc;
1097 
1098 	if (ats_dis)
1099 		set_bit(WQ_FLAG_ATS_DISABLE, &wq->flags);
1100 	else
1101 		clear_bit(WQ_FLAG_ATS_DISABLE, &wq->flags);
1102 
1103 	return count;
1104 }
1105 
1106 static struct device_attribute dev_attr_wq_ats_disable =
1107 		__ATTR(ats_disable, 0644, wq_ats_disable_show, wq_ats_disable_store);
1108 
1109 static ssize_t wq_prs_disable_show(struct device *dev, struct device_attribute *attr, char *buf)
1110 {
1111 	struct idxd_wq *wq = confdev_to_wq(dev);
1112 
1113 	return sysfs_emit(buf, "%u\n", test_bit(WQ_FLAG_PRS_DISABLE, &wq->flags));
1114 }
1115 
1116 static ssize_t wq_prs_disable_store(struct device *dev, struct device_attribute *attr,
1117 				    const char *buf, size_t count)
1118 {
1119 	struct idxd_wq *wq = confdev_to_wq(dev);
1120 	struct idxd_device *idxd = wq->idxd;
1121 	bool prs_dis;
1122 	int rc;
1123 
1124 	if (wq->state != IDXD_WQ_DISABLED)
1125 		return -EPERM;
1126 
1127 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
1128 		return -EPERM;
1129 
1130 	rc = kstrtobool(buf, &prs_dis);
1131 	if (rc < 0)
1132 		return rc;
1133 
1134 	if (prs_dis) {
1135 		set_bit(WQ_FLAG_PRS_DISABLE, &wq->flags);
1136 		/* when PRS is disabled, BOF needs to be off as well */
1137 		clear_bit(WQ_FLAG_BLOCK_ON_FAULT, &wq->flags);
1138 	} else {
1139 		clear_bit(WQ_FLAG_PRS_DISABLE, &wq->flags);
1140 	}
1141 	return count;
1142 }
1143 
1144 static struct device_attribute dev_attr_wq_prs_disable =
1145 		__ATTR(prs_disable, 0644, wq_prs_disable_show, wq_prs_disable_store);
1146 
1147 static ssize_t wq_occupancy_show(struct device *dev, struct device_attribute *attr, char *buf)
1148 {
1149 	struct idxd_wq *wq = confdev_to_wq(dev);
1150 	struct idxd_device *idxd = wq->idxd;
1151 	u32 occup, offset;
1152 
1153 	if (!idxd->hw.wq_cap.occupancy)
1154 		return -EOPNOTSUPP;
1155 
1156 	offset = WQCFG_OFFSET(idxd, wq->id, WQCFG_OCCUP_IDX);
1157 	occup = ioread32(idxd->reg_base + offset) & WQCFG_OCCUP_MASK;
1158 
1159 	return sysfs_emit(buf, "%u\n", occup);
1160 }
1161 
1162 static struct device_attribute dev_attr_wq_occupancy =
1163 		__ATTR(occupancy, 0444, wq_occupancy_show, NULL);
1164 
1165 static ssize_t wq_enqcmds_retries_show(struct device *dev,
1166 				       struct device_attribute *attr, char *buf)
1167 {
1168 	struct idxd_wq *wq = confdev_to_wq(dev);
1169 
1170 	if (wq_dedicated(wq))
1171 		return -EOPNOTSUPP;
1172 
1173 	return sysfs_emit(buf, "%u\n", wq->enqcmds_retries);
1174 }
1175 
1176 static ssize_t wq_enqcmds_retries_store(struct device *dev, struct device_attribute *attr,
1177 					const char *buf, size_t count)
1178 {
1179 	struct idxd_wq *wq = confdev_to_wq(dev);
1180 	int rc;
1181 	unsigned int retries;
1182 
1183 	if (wq_dedicated(wq))
1184 		return -EOPNOTSUPP;
1185 
1186 	rc = kstrtouint(buf, 10, &retries);
1187 	if (rc < 0)
1188 		return rc;
1189 
1190 	if (retries > IDXD_ENQCMDS_MAX_RETRIES)
1191 		retries = IDXD_ENQCMDS_MAX_RETRIES;
1192 
1193 	wq->enqcmds_retries = retries;
1194 	return count;
1195 }
1196 
1197 static struct device_attribute dev_attr_wq_enqcmds_retries =
1198 		__ATTR(enqcmds_retries, 0644, wq_enqcmds_retries_show, wq_enqcmds_retries_store);
1199 
1200 static ssize_t wq_op_config_show(struct device *dev,
1201 				 struct device_attribute *attr, char *buf)
1202 {
1203 	struct idxd_wq *wq = confdev_to_wq(dev);
1204 
1205 	return sysfs_emit(buf, "%*pb\n", IDXD_MAX_OPCAP_BITS, wq->opcap_bmap);
1206 }
1207 
1208 static int idxd_verify_supported_opcap(struct idxd_device *idxd, unsigned long *opmask)
1209 {
1210 	int bit;
1211 
1212 	/*
1213 	 * The OPCAP is defined as 256 bits that represents each operation the device
1214 	 * supports per bit. Iterate through all the bits and check if the input mask
1215 	 * is set for bits that are not set in the OPCAP for the device. If no OPCAP
1216 	 * bit is set and input mask has the bit set, then return error.
1217 	 */
1218 	for_each_set_bit(bit, opmask, IDXD_MAX_OPCAP_BITS) {
1219 		if (!test_bit(bit, idxd->opcap_bmap))
1220 			return -EINVAL;
1221 	}
1222 
1223 	return 0;
1224 }
1225 
1226 static ssize_t wq_op_config_store(struct device *dev, struct device_attribute *attr,
1227 				  const char *buf, size_t count)
1228 {
1229 	struct idxd_wq *wq = confdev_to_wq(dev);
1230 	struct idxd_device *idxd = wq->idxd;
1231 	unsigned long *opmask;
1232 	int rc;
1233 
1234 	if (wq->state != IDXD_WQ_DISABLED)
1235 		return -EPERM;
1236 
1237 	opmask = bitmap_zalloc(IDXD_MAX_OPCAP_BITS, GFP_KERNEL);
1238 	if (!opmask)
1239 		return -ENOMEM;
1240 
1241 	rc = bitmap_parse(buf, count, opmask, IDXD_MAX_OPCAP_BITS);
1242 	if (rc < 0)
1243 		goto err;
1244 
1245 	rc = idxd_verify_supported_opcap(idxd, opmask);
1246 	if (rc < 0)
1247 		goto err;
1248 
1249 	bitmap_copy(wq->opcap_bmap, opmask, IDXD_MAX_OPCAP_BITS);
1250 
1251 	bitmap_free(opmask);
1252 	return count;
1253 
1254 err:
1255 	bitmap_free(opmask);
1256 	return rc;
1257 }
1258 
1259 static struct device_attribute dev_attr_wq_op_config =
1260 		__ATTR(op_config, 0644, wq_op_config_show, wq_op_config_store);
1261 
1262 static struct attribute *idxd_wq_attributes[] = {
1263 	&dev_attr_wq_clients.attr,
1264 	&dev_attr_wq_state.attr,
1265 	&dev_attr_wq_group_id.attr,
1266 	&dev_attr_wq_mode.attr,
1267 	&dev_attr_wq_size.attr,
1268 	&dev_attr_wq_priority.attr,
1269 	&dev_attr_wq_block_on_fault.attr,
1270 	&dev_attr_wq_threshold.attr,
1271 	&dev_attr_wq_type.attr,
1272 	&dev_attr_wq_name.attr,
1273 	&dev_attr_wq_cdev_minor.attr,
1274 	&dev_attr_wq_max_transfer_size.attr,
1275 	&dev_attr_wq_max_batch_size.attr,
1276 	&dev_attr_wq_ats_disable.attr,
1277 	&dev_attr_wq_prs_disable.attr,
1278 	&dev_attr_wq_occupancy.attr,
1279 	&dev_attr_wq_enqcmds_retries.attr,
1280 	&dev_attr_wq_op_config.attr,
1281 	NULL,
1282 };
1283 
1284 /*  A WQ attr is invisible if the feature is not supported in WQCAP. */
1285 #define idxd_wq_attr_invisible(name, cap_field, a, idxd)		\
1286 	((a) == &dev_attr_wq_##name.attr && !(idxd)->hw.wq_cap.cap_field)
1287 
1288 static bool idxd_wq_attr_max_batch_size_invisible(struct attribute *attr,
1289 						  struct idxd_device *idxd)
1290 {
1291 	/* Intel IAA does not support batch processing, make it invisible */
1292 	return attr == &dev_attr_wq_max_batch_size.attr &&
1293 	       idxd->data->type == IDXD_TYPE_IAX;
1294 }
1295 
1296 static umode_t idxd_wq_attr_visible(struct kobject *kobj,
1297 				    struct attribute *attr, int n)
1298 {
1299 	struct device *dev = container_of(kobj, struct device, kobj);
1300 	struct idxd_wq *wq = confdev_to_wq(dev);
1301 	struct idxd_device *idxd = wq->idxd;
1302 
1303 	if (idxd_wq_attr_invisible(op_config, op_config, attr, idxd))
1304 		return 0;
1305 
1306 	if (idxd_wq_attr_max_batch_size_invisible(attr, idxd))
1307 		return 0;
1308 
1309 	if (idxd_wq_attr_invisible(prs_disable, wq_prs_support, attr, idxd))
1310 		return 0;
1311 
1312 	if (idxd_wq_attr_invisible(ats_disable, wq_ats_support, attr, idxd))
1313 		return 0;
1314 
1315 	return attr->mode;
1316 }
1317 
1318 static const struct attribute_group idxd_wq_attribute_group = {
1319 	.attrs = idxd_wq_attributes,
1320 	.is_visible = idxd_wq_attr_visible,
1321 };
1322 
1323 static const struct attribute_group *idxd_wq_attribute_groups[] = {
1324 	&idxd_wq_attribute_group,
1325 	NULL,
1326 };
1327 
1328 static void idxd_conf_wq_release(struct device *dev)
1329 {
1330 	struct idxd_wq *wq = confdev_to_wq(dev);
1331 
1332 	bitmap_free(wq->opcap_bmap);
1333 	kfree(wq->wqcfg);
1334 	xa_destroy(&wq->upasid_xa);
1335 	kfree(wq);
1336 }
1337 
1338 struct device_type idxd_wq_device_type = {
1339 	.name = "wq",
1340 	.release = idxd_conf_wq_release,
1341 	.groups = idxd_wq_attribute_groups,
1342 };
1343 
1344 /* IDXD device attribs */
1345 static ssize_t version_show(struct device *dev, struct device_attribute *attr,
1346 			    char *buf)
1347 {
1348 	struct idxd_device *idxd = confdev_to_idxd(dev);
1349 
1350 	return sysfs_emit(buf, "%#x\n", idxd->hw.version);
1351 }
1352 static DEVICE_ATTR_RO(version);
1353 
1354 static ssize_t max_work_queues_size_show(struct device *dev,
1355 					 struct device_attribute *attr,
1356 					 char *buf)
1357 {
1358 	struct idxd_device *idxd = confdev_to_idxd(dev);
1359 
1360 	return sysfs_emit(buf, "%u\n", idxd->max_wq_size);
1361 }
1362 static DEVICE_ATTR_RO(max_work_queues_size);
1363 
1364 static ssize_t max_groups_show(struct device *dev,
1365 			       struct device_attribute *attr, char *buf)
1366 {
1367 	struct idxd_device *idxd = confdev_to_idxd(dev);
1368 
1369 	return sysfs_emit(buf, "%u\n", idxd->max_groups);
1370 }
1371 static DEVICE_ATTR_RO(max_groups);
1372 
1373 static ssize_t max_work_queues_show(struct device *dev,
1374 				    struct device_attribute *attr, char *buf)
1375 {
1376 	struct idxd_device *idxd = confdev_to_idxd(dev);
1377 
1378 	return sysfs_emit(buf, "%u\n", idxd->max_wqs);
1379 }
1380 static DEVICE_ATTR_RO(max_work_queues);
1381 
1382 static ssize_t max_engines_show(struct device *dev,
1383 				struct device_attribute *attr, char *buf)
1384 {
1385 	struct idxd_device *idxd = confdev_to_idxd(dev);
1386 
1387 	return sysfs_emit(buf, "%u\n", idxd->max_engines);
1388 }
1389 static DEVICE_ATTR_RO(max_engines);
1390 
1391 static ssize_t numa_node_show(struct device *dev,
1392 			      struct device_attribute *attr, char *buf)
1393 {
1394 	struct idxd_device *idxd = confdev_to_idxd(dev);
1395 
1396 	return sysfs_emit(buf, "%d\n", dev_to_node(&idxd->pdev->dev));
1397 }
1398 static DEVICE_ATTR_RO(numa_node);
1399 
1400 static ssize_t max_batch_size_show(struct device *dev,
1401 				   struct device_attribute *attr, char *buf)
1402 {
1403 	struct idxd_device *idxd = confdev_to_idxd(dev);
1404 
1405 	return sysfs_emit(buf, "%u\n", idxd->max_batch_size);
1406 }
1407 static DEVICE_ATTR_RO(max_batch_size);
1408 
1409 static ssize_t max_transfer_size_show(struct device *dev,
1410 				      struct device_attribute *attr,
1411 				      char *buf)
1412 {
1413 	struct idxd_device *idxd = confdev_to_idxd(dev);
1414 
1415 	return sysfs_emit(buf, "%llu\n", idxd->max_xfer_bytes);
1416 }
1417 static DEVICE_ATTR_RO(max_transfer_size);
1418 
1419 static ssize_t op_cap_show(struct device *dev,
1420 			   struct device_attribute *attr, char *buf)
1421 {
1422 	struct idxd_device *idxd = confdev_to_idxd(dev);
1423 
1424 	return sysfs_emit(buf, "%*pb\n", IDXD_MAX_OPCAP_BITS, idxd->opcap_bmap);
1425 }
1426 static DEVICE_ATTR_RO(op_cap);
1427 
1428 static ssize_t gen_cap_show(struct device *dev,
1429 			    struct device_attribute *attr, char *buf)
1430 {
1431 	struct idxd_device *idxd = confdev_to_idxd(dev);
1432 
1433 	return sysfs_emit(buf, "%#llx\n", idxd->hw.gen_cap.bits);
1434 }
1435 static DEVICE_ATTR_RO(gen_cap);
1436 
1437 static ssize_t configurable_show(struct device *dev,
1438 				 struct device_attribute *attr, char *buf)
1439 {
1440 	struct idxd_device *idxd = confdev_to_idxd(dev);
1441 
1442 	return sysfs_emit(buf, "%u\n", test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags));
1443 }
1444 static DEVICE_ATTR_RO(configurable);
1445 
1446 static ssize_t clients_show(struct device *dev,
1447 			    struct device_attribute *attr, char *buf)
1448 {
1449 	struct idxd_device *idxd = confdev_to_idxd(dev);
1450 	int count = 0, i;
1451 
1452 	spin_lock(&idxd->dev_lock);
1453 	for (i = 0; i < idxd->max_wqs; i++) {
1454 		struct idxd_wq *wq = idxd->wqs[i];
1455 
1456 		count += wq->client_count;
1457 	}
1458 	spin_unlock(&idxd->dev_lock);
1459 
1460 	return sysfs_emit(buf, "%d\n", count);
1461 }
1462 static DEVICE_ATTR_RO(clients);
1463 
1464 static ssize_t pasid_enabled_show(struct device *dev,
1465 				  struct device_attribute *attr, char *buf)
1466 {
1467 	struct idxd_device *idxd = confdev_to_idxd(dev);
1468 
1469 	return sysfs_emit(buf, "%u\n", device_user_pasid_enabled(idxd));
1470 }
1471 static DEVICE_ATTR_RO(pasid_enabled);
1472 
1473 static ssize_t state_show(struct device *dev,
1474 			  struct device_attribute *attr, char *buf)
1475 {
1476 	struct idxd_device *idxd = confdev_to_idxd(dev);
1477 
1478 	switch (idxd->state) {
1479 	case IDXD_DEV_DISABLED:
1480 		return sysfs_emit(buf, "disabled\n");
1481 	case IDXD_DEV_ENABLED:
1482 		return sysfs_emit(buf, "enabled\n");
1483 	case IDXD_DEV_HALTED:
1484 		return sysfs_emit(buf, "halted\n");
1485 	}
1486 
1487 	return sysfs_emit(buf, "unknown\n");
1488 }
1489 static DEVICE_ATTR_RO(state);
1490 
1491 static ssize_t errors_show(struct device *dev,
1492 			   struct device_attribute *attr, char *buf)
1493 {
1494 	struct idxd_device *idxd = confdev_to_idxd(dev);
1495 	DECLARE_BITMAP(swerr_bmap, 256);
1496 
1497 	bitmap_zero(swerr_bmap, 256);
1498 	spin_lock(&idxd->dev_lock);
1499 	multi_u64_to_bmap(swerr_bmap, &idxd->sw_err.bits[0], 4);
1500 	spin_unlock(&idxd->dev_lock);
1501 	return sysfs_emit(buf, "%*pb\n", 256, swerr_bmap);
1502 }
1503 static DEVICE_ATTR_RO(errors);
1504 
1505 static ssize_t max_read_buffers_show(struct device *dev,
1506 				     struct device_attribute *attr, char *buf)
1507 {
1508 	struct idxd_device *idxd = confdev_to_idxd(dev);
1509 
1510 	return sysfs_emit(buf, "%u\n", idxd->max_rdbufs);
1511 }
1512 
1513 static ssize_t max_tokens_show(struct device *dev,
1514 			       struct device_attribute *attr, char *buf)
1515 {
1516 	dev_warn_once(dev, "attribute deprecated, see max_read_buffers.\n");
1517 	return max_read_buffers_show(dev, attr, buf);
1518 }
1519 
1520 static DEVICE_ATTR_RO(max_tokens);	/* deprecated */
1521 static DEVICE_ATTR_RO(max_read_buffers);
1522 
1523 static ssize_t read_buffer_limit_show(struct device *dev,
1524 				      struct device_attribute *attr, char *buf)
1525 {
1526 	struct idxd_device *idxd = confdev_to_idxd(dev);
1527 
1528 	return sysfs_emit(buf, "%u\n", idxd->rdbuf_limit);
1529 }
1530 
1531 static ssize_t token_limit_show(struct device *dev,
1532 				struct device_attribute *attr, char *buf)
1533 {
1534 	dev_warn_once(dev, "attribute deprecated, see read_buffer_limit.\n");
1535 	return read_buffer_limit_show(dev, attr, buf);
1536 }
1537 
1538 static ssize_t read_buffer_limit_store(struct device *dev,
1539 				       struct device_attribute *attr,
1540 				       const char *buf, size_t count)
1541 {
1542 	struct idxd_device *idxd = confdev_to_idxd(dev);
1543 	unsigned long val;
1544 	int rc;
1545 
1546 	rc = kstrtoul(buf, 10, &val);
1547 	if (rc < 0)
1548 		return -EINVAL;
1549 
1550 	if (idxd->state == IDXD_DEV_ENABLED)
1551 		return -EPERM;
1552 
1553 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
1554 		return -EPERM;
1555 
1556 	if (!idxd->hw.group_cap.rdbuf_limit)
1557 		return -EPERM;
1558 
1559 	if (val > idxd->hw.group_cap.total_rdbufs)
1560 		return -EINVAL;
1561 
1562 	idxd->rdbuf_limit = val;
1563 	return count;
1564 }
1565 
1566 static ssize_t token_limit_store(struct device *dev,
1567 				 struct device_attribute *attr,
1568 				 const char *buf, size_t count)
1569 {
1570 	dev_warn_once(dev, "attribute deprecated, see read_buffer_limit\n");
1571 	return read_buffer_limit_store(dev, attr, buf, count);
1572 }
1573 
1574 static DEVICE_ATTR_RW(token_limit);	/* deprecated */
1575 static DEVICE_ATTR_RW(read_buffer_limit);
1576 
1577 static ssize_t cdev_major_show(struct device *dev,
1578 			       struct device_attribute *attr, char *buf)
1579 {
1580 	struct idxd_device *idxd = confdev_to_idxd(dev);
1581 
1582 	return sysfs_emit(buf, "%u\n", idxd->major);
1583 }
1584 static DEVICE_ATTR_RO(cdev_major);
1585 
1586 static ssize_t cmd_status_show(struct device *dev,
1587 			       struct device_attribute *attr, char *buf)
1588 {
1589 	struct idxd_device *idxd = confdev_to_idxd(dev);
1590 
1591 	return sysfs_emit(buf, "%#x\n", idxd->cmd_status);
1592 }
1593 
1594 static ssize_t cmd_status_store(struct device *dev, struct device_attribute *attr,
1595 				const char *buf, size_t count)
1596 {
1597 	struct idxd_device *idxd = confdev_to_idxd(dev);
1598 
1599 	idxd->cmd_status = 0;
1600 	return count;
1601 }
1602 static DEVICE_ATTR_RW(cmd_status);
1603 
1604 static ssize_t iaa_cap_show(struct device *dev,
1605 			    struct device_attribute *attr, char *buf)
1606 {
1607 	struct idxd_device *idxd = confdev_to_idxd(dev);
1608 
1609 	if (idxd->hw.version < DEVICE_VERSION_2)
1610 		return -EOPNOTSUPP;
1611 
1612 	return sysfs_emit(buf, "%#llx\n", idxd->hw.iaa_cap.bits);
1613 }
1614 static DEVICE_ATTR_RO(iaa_cap);
1615 
1616 static ssize_t event_log_size_show(struct device *dev,
1617 				   struct device_attribute *attr, char *buf)
1618 {
1619 	struct idxd_device *idxd = confdev_to_idxd(dev);
1620 
1621 	if (!idxd->evl)
1622 		return -EOPNOTSUPP;
1623 
1624 	return sysfs_emit(buf, "%u\n", idxd->evl->size);
1625 }
1626 
1627 static ssize_t event_log_size_store(struct device *dev,
1628 				    struct device_attribute *attr,
1629 				    const char *buf, size_t count)
1630 {
1631 	struct idxd_device *idxd = confdev_to_idxd(dev);
1632 	unsigned long val;
1633 	int rc;
1634 
1635 	if (!idxd->evl)
1636 		return -EOPNOTSUPP;
1637 
1638 	rc = kstrtoul(buf, 10, &val);
1639 	if (rc < 0)
1640 		return -EINVAL;
1641 
1642 	if (idxd->state == IDXD_DEV_ENABLED)
1643 		return -EPERM;
1644 
1645 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
1646 		return -EPERM;
1647 
1648 	if (val < IDXD_EVL_SIZE_MIN || val > IDXD_EVL_SIZE_MAX ||
1649 	    (val * evl_ent_size(idxd) > ULONG_MAX - idxd->evl->dma))
1650 		return -EINVAL;
1651 
1652 	idxd->evl->size = val;
1653 	return count;
1654 }
1655 static DEVICE_ATTR_RW(event_log_size);
1656 
1657 static bool idxd_device_attr_max_batch_size_invisible(struct attribute *attr,
1658 						      struct idxd_device *idxd)
1659 {
1660 	/* Intel IAA does not support batch processing, make it invisible */
1661 	return attr == &dev_attr_max_batch_size.attr &&
1662 	       idxd->data->type == IDXD_TYPE_IAX;
1663 }
1664 
1665 static bool idxd_device_attr_read_buffers_invisible(struct attribute *attr,
1666 						    struct idxd_device *idxd)
1667 {
1668 	/*
1669 	 * Intel IAA does not support Read Buffer allocation control,
1670 	 * make these attributes invisible.
1671 	 */
1672 	return (attr == &dev_attr_max_tokens.attr ||
1673 		attr == &dev_attr_max_read_buffers.attr ||
1674 		attr == &dev_attr_token_limit.attr ||
1675 		attr == &dev_attr_read_buffer_limit.attr) &&
1676 		idxd->data->type == IDXD_TYPE_IAX;
1677 }
1678 
1679 static bool idxd_device_attr_iaa_cap_invisible(struct attribute *attr,
1680 					       struct idxd_device *idxd)
1681 {
1682 	return attr == &dev_attr_iaa_cap.attr &&
1683 	       (idxd->data->type != IDXD_TYPE_IAX ||
1684 	       idxd->hw.version < DEVICE_VERSION_2);
1685 }
1686 
1687 static bool idxd_device_attr_event_log_size_invisible(struct attribute *attr,
1688 						      struct idxd_device *idxd)
1689 {
1690 	return (attr == &dev_attr_event_log_size.attr &&
1691 		!idxd->hw.gen_cap.evl_support);
1692 }
1693 
1694 static umode_t idxd_device_attr_visible(struct kobject *kobj,
1695 					struct attribute *attr, int n)
1696 {
1697 	struct device *dev = container_of(kobj, struct device, kobj);
1698 	struct idxd_device *idxd = confdev_to_idxd(dev);
1699 
1700 	if (idxd_device_attr_max_batch_size_invisible(attr, idxd))
1701 		return 0;
1702 
1703 	if (idxd_device_attr_read_buffers_invisible(attr, idxd))
1704 		return 0;
1705 
1706 	if (idxd_device_attr_iaa_cap_invisible(attr, idxd))
1707 		return 0;
1708 
1709 	if (idxd_device_attr_event_log_size_invisible(attr, idxd))
1710 		return 0;
1711 
1712 	return attr->mode;
1713 }
1714 
1715 static struct attribute *idxd_device_attributes[] = {
1716 	&dev_attr_version.attr,
1717 	&dev_attr_max_groups.attr,
1718 	&dev_attr_max_work_queues.attr,
1719 	&dev_attr_max_work_queues_size.attr,
1720 	&dev_attr_max_engines.attr,
1721 	&dev_attr_numa_node.attr,
1722 	&dev_attr_max_batch_size.attr,
1723 	&dev_attr_max_transfer_size.attr,
1724 	&dev_attr_op_cap.attr,
1725 	&dev_attr_gen_cap.attr,
1726 	&dev_attr_configurable.attr,
1727 	&dev_attr_clients.attr,
1728 	&dev_attr_pasid_enabled.attr,
1729 	&dev_attr_state.attr,
1730 	&dev_attr_errors.attr,
1731 	&dev_attr_max_tokens.attr,
1732 	&dev_attr_max_read_buffers.attr,
1733 	&dev_attr_token_limit.attr,
1734 	&dev_attr_read_buffer_limit.attr,
1735 	&dev_attr_cdev_major.attr,
1736 	&dev_attr_cmd_status.attr,
1737 	&dev_attr_iaa_cap.attr,
1738 	&dev_attr_event_log_size.attr,
1739 	NULL,
1740 };
1741 
1742 static const struct attribute_group idxd_device_attribute_group = {
1743 	.attrs = idxd_device_attributes,
1744 	.is_visible = idxd_device_attr_visible,
1745 };
1746 
1747 static const struct attribute_group *idxd_attribute_groups[] = {
1748 	&idxd_device_attribute_group,
1749 	NULL,
1750 };
1751 
1752 static void idxd_conf_device_release(struct device *dev)
1753 {
1754 	struct idxd_device *idxd = confdev_to_idxd(dev);
1755 
1756 	kfree(idxd->groups);
1757 	bitmap_free(idxd->wq_enable_map);
1758 	kfree(idxd->wqs);
1759 	kfree(idxd->engines);
1760 	kfree(idxd->evl);
1761 	kmem_cache_destroy(idxd->evl_cache);
1762 	ida_free(&idxd_ida, idxd->id);
1763 	bitmap_free(idxd->opcap_bmap);
1764 	kfree(idxd);
1765 }
1766 
1767 struct device_type dsa_device_type = {
1768 	.name = "dsa",
1769 	.release = idxd_conf_device_release,
1770 	.groups = idxd_attribute_groups,
1771 };
1772 
1773 struct device_type iax_device_type = {
1774 	.name = "iax",
1775 	.release = idxd_conf_device_release,
1776 	.groups = idxd_attribute_groups,
1777 };
1778 
1779 static int idxd_register_engine_devices(struct idxd_device *idxd)
1780 {
1781 	struct idxd_engine *engine;
1782 	int i, j, rc;
1783 
1784 	for (i = 0; i < idxd->max_engines; i++) {
1785 		engine = idxd->engines[i];
1786 		rc = device_add(engine_confdev(engine));
1787 		if (rc < 0)
1788 			goto cleanup;
1789 	}
1790 
1791 	return 0;
1792 
1793 cleanup:
1794 	j = i - 1;
1795 	for (; i < idxd->max_engines; i++) {
1796 		engine = idxd->engines[i];
1797 		put_device(engine_confdev(engine));
1798 	}
1799 
1800 	while (j--) {
1801 		engine = idxd->engines[j];
1802 		device_unregister(engine_confdev(engine));
1803 	}
1804 	return rc;
1805 }
1806 
1807 static int idxd_register_group_devices(struct idxd_device *idxd)
1808 {
1809 	struct idxd_group *group;
1810 	int i, j, rc;
1811 
1812 	for (i = 0; i < idxd->max_groups; i++) {
1813 		group = idxd->groups[i];
1814 		rc = device_add(group_confdev(group));
1815 		if (rc < 0)
1816 			goto cleanup;
1817 	}
1818 
1819 	return 0;
1820 
1821 cleanup:
1822 	j = i - 1;
1823 	for (; i < idxd->max_groups; i++) {
1824 		group = idxd->groups[i];
1825 		put_device(group_confdev(group));
1826 	}
1827 
1828 	while (j--) {
1829 		group = idxd->groups[j];
1830 		device_unregister(group_confdev(group));
1831 	}
1832 	return rc;
1833 }
1834 
1835 static int idxd_register_wq_devices(struct idxd_device *idxd)
1836 {
1837 	struct idxd_wq *wq;
1838 	int i, rc, j;
1839 
1840 	for (i = 0; i < idxd->max_wqs; i++) {
1841 		wq = idxd->wqs[i];
1842 		rc = device_add(wq_confdev(wq));
1843 		if (rc < 0)
1844 			goto cleanup;
1845 	}
1846 
1847 	return 0;
1848 
1849 cleanup:
1850 	j = i - 1;
1851 	for (; i < idxd->max_wqs; i++) {
1852 		wq = idxd->wqs[i];
1853 		put_device(wq_confdev(wq));
1854 	}
1855 
1856 	while (j--) {
1857 		wq = idxd->wqs[j];
1858 		device_unregister(wq_confdev(wq));
1859 	}
1860 	return rc;
1861 }
1862 
1863 int idxd_register_devices(struct idxd_device *idxd)
1864 {
1865 	struct device *dev = &idxd->pdev->dev;
1866 	int rc, i;
1867 
1868 	rc = device_add(idxd_confdev(idxd));
1869 	if (rc < 0)
1870 		return rc;
1871 
1872 	rc = idxd_register_wq_devices(idxd);
1873 	if (rc < 0) {
1874 		dev_dbg(dev, "WQ devices registering failed: %d\n", rc);
1875 		goto err_wq;
1876 	}
1877 
1878 	rc = idxd_register_engine_devices(idxd);
1879 	if (rc < 0) {
1880 		dev_dbg(dev, "Engine devices registering failed: %d\n", rc);
1881 		goto err_engine;
1882 	}
1883 
1884 	rc = idxd_register_group_devices(idxd);
1885 	if (rc < 0) {
1886 		dev_dbg(dev, "Group device registering failed: %d\n", rc);
1887 		goto err_group;
1888 	}
1889 
1890 	return 0;
1891 
1892  err_group:
1893 	for (i = 0; i < idxd->max_engines; i++)
1894 		device_unregister(engine_confdev(idxd->engines[i]));
1895  err_engine:
1896 	for (i = 0; i < idxd->max_wqs; i++)
1897 		device_unregister(wq_confdev(idxd->wqs[i]));
1898  err_wq:
1899 	device_del(idxd_confdev(idxd));
1900 	return rc;
1901 }
1902 
1903 void idxd_unregister_devices(struct idxd_device *idxd)
1904 {
1905 	int i;
1906 
1907 	for (i = 0; i < idxd->max_wqs; i++) {
1908 		struct idxd_wq *wq = idxd->wqs[i];
1909 
1910 		device_unregister(wq_confdev(wq));
1911 	}
1912 
1913 	for (i = 0; i < idxd->max_engines; i++) {
1914 		struct idxd_engine *engine = idxd->engines[i];
1915 
1916 		device_unregister(engine_confdev(engine));
1917 	}
1918 
1919 	for (i = 0; i < idxd->max_groups; i++) {
1920 		struct idxd_group *group = idxd->groups[i];
1921 
1922 		device_unregister(group_confdev(group));
1923 	}
1924 }
1925 
1926 int idxd_register_bus_type(void)
1927 {
1928 	return bus_register(&dsa_bus_type);
1929 }
1930 
1931 void idxd_unregister_bus_type(void)
1932 {
1933 	bus_unregister(&dsa_bus_type);
1934 }
1935