sysfs.c 42.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// SPDX-License-Identifier: GPL-2.0
/* Copyright(c) 2019 Intel Corporation. All rights rsvd. */
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/device.h>
#include <linux/io-64-nonatomic-lo-hi.h>
#include <uapi/linux/idxd.h>
#include "registers.h"
#include "idxd.h"

static char *idxd_wq_type_names[] = {
	[IDXD_WQT_NONE]		= "none",
	[IDXD_WQT_KERNEL]	= "kernel",
16
	[IDXD_WQT_USER]		= "user",
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
};

static void idxd_conf_device_release(struct device *dev)
{
	dev_dbg(dev, "%s for %s\n", __func__, dev_name(dev));
}

static struct device_type idxd_group_device_type = {
	.name = "group",
	.release = idxd_conf_device_release,
};

static struct device_type idxd_wq_device_type = {
	.name = "wq",
	.release = idxd_conf_device_release,
};

static struct device_type idxd_engine_device_type = {
	.name = "engine",
	.release = idxd_conf_device_release,
};

static struct device_type dsa_device_type = {
	.name = "dsa",
	.release = idxd_conf_device_release,
};

44 45 46 47 48
static struct device_type iax_device_type = {
	.name = "iax",
	.release = idxd_conf_device_release,
};

49 50 51 52 53
static inline bool is_dsa_dev(struct device *dev)
{
	return dev ? dev->type == &dsa_device_type : false;
}

54 55 56 57 58
static inline bool is_iax_dev(struct device *dev)
{
	return dev ? dev->type == &iax_device_type : false;
}

59 60
static inline bool is_idxd_dev(struct device *dev)
{
61
	return is_dsa_dev(dev) || is_iax_dev(dev);
62 63 64 65 66 67 68
}

static inline bool is_idxd_wq_dev(struct device *dev)
{
	return dev ? dev->type == &idxd_wq_device_type : false;
}

69 70 71 72 73 74 75 76
static inline bool is_idxd_wq_dmaengine(struct idxd_wq *wq)
{
	if (wq->type == IDXD_WQT_KERNEL &&
	    strcmp(wq->name, "dmaengine") == 0)
		return true;
	return false;
}

77 78
static inline bool is_idxd_wq_cdev(struct idxd_wq *wq)
{
79
	return wq->type == IDXD_WQT_USER;
80 81
}

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
static int idxd_config_bus_match(struct device *dev,
				 struct device_driver *drv)
{
	int matched = 0;

	if (is_idxd_dev(dev)) {
		struct idxd_device *idxd = confdev_to_idxd(dev);

		if (idxd->state != IDXD_DEV_CONF_READY)
			return 0;
		matched = 1;
	} else if (is_idxd_wq_dev(dev)) {
		struct idxd_wq *wq = confdev_to_wq(dev);
		struct idxd_device *idxd = wq->idxd;

		if (idxd->state < IDXD_DEV_CONF_READY)
			return 0;

		if (wq->state != IDXD_WQ_DISABLED) {
			dev_dbg(dev, "%s not disabled\n", dev_name(dev));
			return 0;
		}
		matched = 1;
	}

	if (matched)
		dev_dbg(dev, "%s matched\n", dev_name(dev));

	return matched;
}

static int idxd_config_bus_probe(struct device *dev)
{
	int rc;
	unsigned long flags;

	dev_dbg(dev, "%s called\n", __func__);

	if (is_idxd_dev(dev)) {
		struct idxd_device *idxd = confdev_to_idxd(dev);

		if (idxd->state != IDXD_DEV_CONF_READY) {
			dev_warn(dev, "Device not ready for config\n");
			return -EBUSY;
		}

128 129 130
		if (!try_module_get(THIS_MODULE))
			return -ENXIO;

131
		/* Perform IDXD configuration and enabling */
132
		spin_lock_irqsave(&idxd->dev_lock, flags);
133
		rc = idxd_device_config(idxd);
134
		spin_unlock_irqrestore(&idxd->dev_lock, flags);
135
		if (rc < 0) {
136
			module_put(THIS_MODULE);
137 138 139 140 141 142 143
			dev_warn(dev, "Device config failed: %d\n", rc);
			return rc;
		}

		/* start device */
		rc = idxd_device_enable(idxd);
		if (rc < 0) {
144
			module_put(THIS_MODULE);
145 146 147 148 149 150
			dev_warn(dev, "Device enable failed: %d\n", rc);
			return rc;
		}

		dev_info(dev, "Device %s enabled\n", dev_name(dev));

151 152
		rc = idxd_register_dma_device(idxd);
		if (rc < 0) {
153
			module_put(THIS_MODULE);
154 155 156
			dev_dbg(dev, "Failed to register dmaengine device\n");
			return rc;
		}
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
		return 0;
	} else if (is_idxd_wq_dev(dev)) {
		struct idxd_wq *wq = confdev_to_wq(dev);
		struct idxd_device *idxd = wq->idxd;

		mutex_lock(&wq->wq_lock);

		if (idxd->state != IDXD_DEV_ENABLED) {
			mutex_unlock(&wq->wq_lock);
			dev_warn(dev, "Enabling while device not enabled.\n");
			return -EPERM;
		}

		if (wq->state != IDXD_WQ_DISABLED) {
			mutex_unlock(&wq->wq_lock);
			dev_warn(dev, "WQ %d already enabled.\n", wq->id);
			return -EBUSY;
		}

		if (!wq->group) {
			mutex_unlock(&wq->wq_lock);
			dev_warn(dev, "WQ not attached to group.\n");
			return -EINVAL;
		}

		if (strlen(wq->name) == 0) {
			mutex_unlock(&wq->wq_lock);
			dev_warn(dev, "WQ name not set.\n");
			return -EINVAL;
		}

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
		/* Shared WQ checks */
		if (wq_shared(wq)) {
			if (!device_swq_supported(idxd)) {
				dev_warn(dev,
					 "PASID not enabled and shared WQ.\n");
				mutex_unlock(&wq->wq_lock);
				return -ENXIO;
			}
			/*
			 * Shared wq with the threshold set to 0 means the user
			 * did not set the threshold or transitioned from a
			 * dedicated wq but did not set threshold. A value
			 * of 0 would effectively disable the shared wq. The
			 * driver does not allow a value of 0 to be set for
			 * threshold via sysfs.
			 */
			if (wq->threshold == 0) {
				dev_warn(dev,
					 "Shared WQ and threshold 0.\n");
				mutex_unlock(&wq->wq_lock);
				return -EINVAL;
			}
		}

212 213 214 215 216 217 218 219 220
		rc = idxd_wq_alloc_resources(wq);
		if (rc < 0) {
			mutex_unlock(&wq->wq_lock);
			dev_warn(dev, "WQ resource alloc failed\n");
			return rc;
		}

		spin_lock_irqsave(&idxd->dev_lock, flags);
		rc = idxd_device_config(idxd);
221
		spin_unlock_irqrestore(&idxd->dev_lock, flags);
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
		if (rc < 0) {
			mutex_unlock(&wq->wq_lock);
			dev_warn(dev, "Writing WQ %d config failed: %d\n",
				 wq->id, rc);
			return rc;
		}

		rc = idxd_wq_enable(wq);
		if (rc < 0) {
			mutex_unlock(&wq->wq_lock);
			dev_warn(dev, "WQ %d enabling failed: %d\n",
				 wq->id, rc);
			return rc;
		}

		rc = idxd_wq_map_portal(wq);
		if (rc < 0) {
			dev_warn(dev, "wq portal mapping failed: %d\n", rc);
			rc = idxd_wq_disable(wq);
			if (rc < 0)
				dev_warn(dev, "IDXD wq disable failed\n");
			mutex_unlock(&wq->wq_lock);
			return rc;
		}

		wq->client_count = 0;

		dev_info(dev, "wq %s enabled\n", dev_name(&wq->conf_dev));
250 251 252 253 254 255 256 257

		if (is_idxd_wq_dmaengine(wq)) {
			rc = idxd_register_dma_channel(wq);
			if (rc < 0) {
				dev_dbg(dev, "DMA channel register failed\n");
				mutex_unlock(&wq->wq_lock);
				return rc;
			}
258 259 260 261 262 263 264
		} else if (is_idxd_wq_cdev(wq)) {
			rc = idxd_wq_add_cdev(wq);
			if (rc < 0) {
				dev_dbg(dev, "Cdev creation failed\n");
				mutex_unlock(&wq->wq_lock);
				return rc;
			}
265 266
		}

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
		mutex_unlock(&wq->wq_lock);
		return 0;
	}

	return -ENODEV;
}

static void disable_wq(struct idxd_wq *wq)
{
	struct idxd_device *idxd = wq->idxd;
	struct device *dev = &idxd->pdev->dev;

	mutex_lock(&wq->wq_lock);
	dev_dbg(dev, "%s removing WQ %s\n", __func__, dev_name(&wq->conf_dev));
	if (wq->state == IDXD_WQ_DISABLED) {
		mutex_unlock(&wq->wq_lock);
		return;
	}

286 287
	if (is_idxd_wq_dmaengine(wq))
		idxd_unregister_dma_channel(wq);
288 289
	else if (is_idxd_wq_cdev(wq))
		idxd_wq_del_cdev(wq);
290

291 292 293 294 295 296
	if (idxd_wq_refcount(wq))
		dev_warn(dev, "Clients has claim on wq %d: %d\n",
			 wq->id, idxd_wq_refcount(wq));

	idxd_wq_unmap_portal(wq);

297
	idxd_wq_drain(wq);
298
	idxd_wq_reset(wq);
299 300 301 302 303

	idxd_wq_free_resources(wq);
	wq->client_count = 0;
	mutex_unlock(&wq->wq_lock);

304
	dev_info(dev, "wq %s disabled\n", dev_name(&wq->conf_dev));
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
}

static int idxd_config_bus_remove(struct device *dev)
{
	int rc;

	dev_dbg(dev, "%s called for %s\n", __func__, dev_name(dev));

	/* disable workqueue here */
	if (is_idxd_wq_dev(dev)) {
		struct idxd_wq *wq = confdev_to_wq(dev);

		disable_wq(wq);
	} else if (is_idxd_dev(dev)) {
		struct idxd_device *idxd = confdev_to_idxd(dev);
		int i;

		dev_dbg(dev, "%s removing dev %s\n", __func__,
			dev_name(&idxd->conf_dev));
		for (i = 0; i < idxd->max_wqs; i++) {
			struct idxd_wq *wq = &idxd->wqs[i];

			if (wq->state == IDXD_WQ_DISABLED)
				continue;
			dev_warn(dev, "Active wq %d on disable %s.\n", i,
				 dev_name(&idxd->conf_dev));
			device_release_driver(&wq->conf_dev);
		}

334
		idxd_unregister_dma_device(idxd);
335
		rc = idxd_device_disable(idxd);
336 337 338
		for (i = 0; i < idxd->max_wqs; i++) {
			struct idxd_wq *wq = &idxd->wqs[i];

V
Vinod Koul 已提交
339
			mutex_lock(&wq->wq_lock);
340
			idxd_wq_disable_cleanup(wq);
V
Vinod Koul 已提交
341
			mutex_unlock(&wq->wq_lock);
342
		}
343
		module_put(THIS_MODULE);
344 345 346 347
		if (rc < 0)
			dev_warn(dev, "Device disable failed\n");
		else
			dev_info(dev, "Device %s disabled\n", dev_name(dev));
348

349 350 351 352 353 354 355 356 357 358
	}

	return 0;
}

static void idxd_config_bus_shutdown(struct device *dev)
{
	dev_dbg(dev, "%s called\n", __func__);
}

359
struct bus_type dsa_bus_type = {
360 361 362 363 364 365 366
	.name = "dsa",
	.match = idxd_config_bus_match,
	.probe = idxd_config_bus_probe,
	.remove = idxd_config_bus_remove,
	.shutdown = idxd_config_bus_shutdown,
};

367 368 369 370 371 372 373 374
struct bus_type iax_bus_type = {
	.name = "iax",
	.match = idxd_config_bus_match,
	.probe = idxd_config_bus_probe,
	.remove = idxd_config_bus_remove,
	.shutdown = idxd_config_bus_shutdown,
};

375
static struct bus_type *idxd_bus_types[] = {
376 377
	&dsa_bus_type,
	&iax_bus_type
378 379 380 381 382 383 384 385 386 387 388
};

static struct idxd_device_driver dsa_drv = {
	.drv = {
		.name = "dsa",
		.bus = &dsa_bus_type,
		.owner = THIS_MODULE,
		.mod_name = KBUILD_MODNAME,
	},
};

389 390 391 392 393 394 395 396 397
static struct idxd_device_driver iax_drv = {
	.drv = {
		.name = "iax",
		.bus = &iax_bus_type,
		.owner = THIS_MODULE,
		.mod_name = KBUILD_MODNAME,
	},
};

398
static struct idxd_device_driver *idxd_drvs[] = {
399 400
	&dsa_drv,
	&iax_drv
401 402
};

403
struct bus_type *idxd_get_bus_type(struct idxd_device *idxd)
404 405 406 407 408 409 410 411
{
	return idxd_bus_types[idxd->type];
}

static struct device_type *idxd_get_device_type(struct idxd_device *idxd)
{
	if (idxd->type == IDXD_TYPE_DSA)
		return &dsa_device_type;
412 413
	else if (idxd->type == IDXD_TYPE_IAX)
		return &iax_device_type;
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
	else
		return NULL;
}

/* IDXD generic driver setup */
int idxd_register_driver(void)
{
	int i, rc;

	for (i = 0; i < IDXD_TYPE_MAX; i++) {
		rc = driver_register(&idxd_drvs[i]->drv);
		if (rc < 0)
			goto drv_fail;
	}

	return 0;

drv_fail:
432
	while (--i >= 0)
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
		driver_unregister(&idxd_drvs[i]->drv);
	return rc;
}

void idxd_unregister_driver(void)
{
	int i;

	for (i = 0; i < IDXD_TYPE_MAX; i++)
		driver_unregister(&idxd_drvs[i]->drv);
}

/* IDXD engine attributes */
static ssize_t engine_group_id_show(struct device *dev,
				    struct device_attribute *attr, char *buf)
{
	struct idxd_engine *engine =
		container_of(dev, struct idxd_engine, conf_dev);

	if (engine->group)
		return sprintf(buf, "%d\n", engine->group->id);
	else
		return sprintf(buf, "%d\n", -1);
}

static ssize_t engine_group_id_store(struct device *dev,
				     struct device_attribute *attr,
				     const char *buf, size_t count)
{
	struct idxd_engine *engine =
		container_of(dev, struct idxd_engine, conf_dev);
	struct idxd_device *idxd = engine->idxd;
	long id;
	int rc;
467
	struct idxd_group *prevg;
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553

	rc = kstrtol(buf, 10, &id);
	if (rc < 0)
		return -EINVAL;

	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
		return -EPERM;

	if (id > idxd->max_groups - 1 || id < -1)
		return -EINVAL;

	if (id == -1) {
		if (engine->group) {
			engine->group->num_engines--;
			engine->group = NULL;
		}
		return count;
	}

	prevg = engine->group;

	if (prevg)
		prevg->num_engines--;
	engine->group = &idxd->groups[id];
	engine->group->num_engines++;

	return count;
}

static struct device_attribute dev_attr_engine_group =
		__ATTR(group_id, 0644, engine_group_id_show,
		       engine_group_id_store);

static struct attribute *idxd_engine_attributes[] = {
	&dev_attr_engine_group.attr,
	NULL,
};

static const struct attribute_group idxd_engine_attribute_group = {
	.attrs = idxd_engine_attributes,
};

static const struct attribute_group *idxd_engine_attribute_groups[] = {
	&idxd_engine_attribute_group,
	NULL,
};

/* Group attributes */

static void idxd_set_free_tokens(struct idxd_device *idxd)
{
	int i, tokens;

	for (i = 0, tokens = 0; i < idxd->max_groups; i++) {
		struct idxd_group *g = &idxd->groups[i];

		tokens += g->tokens_reserved;
	}

	idxd->nr_tokens = idxd->max_tokens - tokens;
}

static ssize_t group_tokens_reserved_show(struct device *dev,
					  struct device_attribute *attr,
					  char *buf)
{
	struct idxd_group *group =
		container_of(dev, struct idxd_group, conf_dev);

	return sprintf(buf, "%u\n", group->tokens_reserved);
}

static ssize_t group_tokens_reserved_store(struct device *dev,
					   struct device_attribute *attr,
					   const char *buf, size_t count)
{
	struct idxd_group *group =
		container_of(dev, struct idxd_group, conf_dev);
	struct idxd_device *idxd = group->idxd;
	unsigned long val;
	int rc;

	rc = kstrtoul(buf, 10, &val);
	if (rc < 0)
		return -EINVAL;

554 555 556
	if (idxd->type == IDXD_TYPE_IAX)
		return -EOPNOTSUPP;

557 558 559 560 561 562 563 564 565
	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
		return -EPERM;

	if (idxd->state == IDXD_DEV_ENABLED)
		return -EPERM;

	if (val > idxd->max_tokens)
		return -EINVAL;

566
	if (val > idxd->nr_tokens + group->tokens_reserved)
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
		return -EINVAL;

	group->tokens_reserved = val;
	idxd_set_free_tokens(idxd);
	return count;
}

static struct device_attribute dev_attr_group_tokens_reserved =
		__ATTR(tokens_reserved, 0644, group_tokens_reserved_show,
		       group_tokens_reserved_store);

static ssize_t group_tokens_allowed_show(struct device *dev,
					 struct device_attribute *attr,
					 char *buf)
{
	struct idxd_group *group =
		container_of(dev, struct idxd_group, conf_dev);

	return sprintf(buf, "%u\n", group->tokens_allowed);
}

static ssize_t group_tokens_allowed_store(struct device *dev,
					  struct device_attribute *attr,
					  const char *buf, size_t count)
{
	struct idxd_group *group =
		container_of(dev, struct idxd_group, conf_dev);
	struct idxd_device *idxd = group->idxd;
	unsigned long val;
	int rc;

	rc = kstrtoul(buf, 10, &val);
	if (rc < 0)
		return -EINVAL;

602 603 604
	if (idxd->type == IDXD_TYPE_IAX)
		return -EOPNOTSUPP;

605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
		return -EPERM;

	if (idxd->state == IDXD_DEV_ENABLED)
		return -EPERM;

	if (val < 4 * group->num_engines ||
	    val > group->tokens_reserved + idxd->nr_tokens)
		return -EINVAL;

	group->tokens_allowed = val;
	return count;
}

static struct device_attribute dev_attr_group_tokens_allowed =
		__ATTR(tokens_allowed, 0644, group_tokens_allowed_show,
		       group_tokens_allowed_store);

static ssize_t group_use_token_limit_show(struct device *dev,
					  struct device_attribute *attr,
					  char *buf)
{
	struct idxd_group *group =
		container_of(dev, struct idxd_group, conf_dev);

	return sprintf(buf, "%u\n", group->use_token_limit);
}

static ssize_t group_use_token_limit_store(struct device *dev,
					   struct device_attribute *attr,
					   const char *buf, size_t count)
{
	struct idxd_group *group =
		container_of(dev, struct idxd_group, conf_dev);
	struct idxd_device *idxd = group->idxd;
	unsigned long val;
	int rc;

	rc = kstrtoul(buf, 10, &val);
	if (rc < 0)
		return -EINVAL;

647 648 649
	if (idxd->type == IDXD_TYPE_IAX)
		return -EOPNOTSUPP;

650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
		return -EPERM;

	if (idxd->state == IDXD_DEV_ENABLED)
		return -EPERM;

	if (idxd->token_limit == 0)
		return -EPERM;

	group->use_token_limit = !!val;
	return count;
}

static struct device_attribute dev_attr_group_use_token_limit =
		__ATTR(use_token_limit, 0644, group_use_token_limit_show,
		       group_use_token_limit_store);

static ssize_t group_engines_show(struct device *dev,
				  struct device_attribute *attr, char *buf)
{
	struct idxd_group *group =
		container_of(dev, struct idxd_group, conf_dev);
	int i, rc = 0;
	char *tmp = buf;
	struct idxd_device *idxd = group->idxd;

	for (i = 0; i < idxd->max_engines; i++) {
		struct idxd_engine *engine = &idxd->engines[i];

		if (!engine->group)
			continue;

		if (engine->group->id == group->id)
			rc += sprintf(tmp + rc, "engine%d.%d ",
					idxd->id, engine->id);
	}

	rc--;
	rc += sprintf(tmp + rc, "\n");

	return rc;
}

static struct device_attribute dev_attr_group_engines =
		__ATTR(engines, 0444, group_engines_show, NULL);

static ssize_t group_work_queues_show(struct device *dev,
				      struct device_attribute *attr, char *buf)
{
	struct idxd_group *group =
		container_of(dev, struct idxd_group, conf_dev);
	int i, rc = 0;
	char *tmp = buf;
	struct idxd_device *idxd = group->idxd;

	for (i = 0; i < idxd->max_wqs; i++) {
		struct idxd_wq *wq = &idxd->wqs[i];

		if (!wq->group)
			continue;

		if (wq->group->id == group->id)
			rc += sprintf(tmp + rc, "wq%d.%d ",
					idxd->id, wq->id);
	}

	rc--;
	rc += sprintf(tmp + rc, "\n");

	return rc;
}

static struct device_attribute dev_attr_group_work_queues =
		__ATTR(work_queues, 0444, group_work_queues_show, NULL);

static ssize_t group_traffic_class_a_show(struct device *dev,
					  struct device_attribute *attr,
					  char *buf)
{
	struct idxd_group *group =
		container_of(dev, struct idxd_group, conf_dev);

	return sprintf(buf, "%d\n", group->tc_a);
}

static ssize_t group_traffic_class_a_store(struct device *dev,
					   struct device_attribute *attr,
					   const char *buf, size_t count)
{
	struct idxd_group *group =
		container_of(dev, struct idxd_group, conf_dev);
	struct idxd_device *idxd = group->idxd;
	long val;
	int rc;

	rc = kstrtol(buf, 10, &val);
	if (rc < 0)
		return -EINVAL;

	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
		return -EPERM;

	if (idxd->state == IDXD_DEV_ENABLED)
		return -EPERM;

	if (val < 0 || val > 7)
		return -EINVAL;

	group->tc_a = val;
	return count;
}

static struct device_attribute dev_attr_group_traffic_class_a =
		__ATTR(traffic_class_a, 0644, group_traffic_class_a_show,
		       group_traffic_class_a_store);

static ssize_t group_traffic_class_b_show(struct device *dev,
					  struct device_attribute *attr,
					  char *buf)
{
	struct idxd_group *group =
		container_of(dev, struct idxd_group, conf_dev);

	return sprintf(buf, "%d\n", group->tc_b);
}

static ssize_t group_traffic_class_b_store(struct device *dev,
					   struct device_attribute *attr,
					   const char *buf, size_t count)
{
	struct idxd_group *group =
		container_of(dev, struct idxd_group, conf_dev);
	struct idxd_device *idxd = group->idxd;
	long val;
	int rc;

	rc = kstrtol(buf, 10, &val);
	if (rc < 0)
		return -EINVAL;

	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
		return -EPERM;

	if (idxd->state == IDXD_DEV_ENABLED)
		return -EPERM;

	if (val < 0 || val > 7)
		return -EINVAL;

	group->tc_b = val;
	return count;
}

static struct device_attribute dev_attr_group_traffic_class_b =
		__ATTR(traffic_class_b, 0644, group_traffic_class_b_show,
		       group_traffic_class_b_store);

static struct attribute *idxd_group_attributes[] = {
	&dev_attr_group_work_queues.attr,
	&dev_attr_group_engines.attr,
	&dev_attr_group_use_token_limit.attr,
	&dev_attr_group_tokens_allowed.attr,
	&dev_attr_group_tokens_reserved.attr,
	&dev_attr_group_traffic_class_a.attr,
	&dev_attr_group_traffic_class_b.attr,
	NULL,
};

static const struct attribute_group idxd_group_attribute_group = {
	.attrs = idxd_group_attributes,
};

static const struct attribute_group *idxd_group_attribute_groups[] = {
	&idxd_group_attribute_group,
	NULL,
};

/* IDXD work queue attribs */
static ssize_t wq_clients_show(struct device *dev,
			       struct device_attribute *attr, char *buf)
{
	struct idxd_wq *wq = container_of(dev, struct idxd_wq, conf_dev);

	return sprintf(buf, "%d\n", wq->client_count);
}

static struct device_attribute dev_attr_wq_clients =
		__ATTR(clients, 0444, wq_clients_show, NULL);

static ssize_t wq_state_show(struct device *dev,
			     struct device_attribute *attr, char *buf)
{
	struct idxd_wq *wq = container_of(dev, struct idxd_wq, conf_dev);

	switch (wq->state) {
	case IDXD_WQ_DISABLED:
		return sprintf(buf, "disabled\n");
	case IDXD_WQ_ENABLED:
		return sprintf(buf, "enabled\n");
	}

	return sprintf(buf, "unknown\n");
}

static struct device_attribute dev_attr_wq_state =
		__ATTR(state, 0444, wq_state_show, NULL);

static ssize_t wq_group_id_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct idxd_wq *wq = container_of(dev, struct idxd_wq, conf_dev);

	if (wq->group)
		return sprintf(buf, "%u\n", wq->group->id);
	else
		return sprintf(buf, "-1\n");
}

static ssize_t wq_group_id_store(struct device *dev,
				 struct device_attribute *attr,
				 const char *buf, size_t count)
{
	struct idxd_wq *wq = container_of(dev, struct idxd_wq, conf_dev);
	struct idxd_device *idxd = wq->idxd;
	long id;
	int rc;
	struct idxd_group *prevg, *group;

	rc = kstrtol(buf, 10, &id);
	if (rc < 0)
		return -EINVAL;

	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
		return -EPERM;

	if (wq->state != IDXD_WQ_DISABLED)
		return -EPERM;

	if (id > idxd->max_groups - 1 || id < -1)
		return -EINVAL;

	if (id == -1) {
		if (wq->group) {
			wq->group->num_wqs--;
			wq->group = NULL;
		}
		return count;
	}

	group = &idxd->groups[id];
	prevg = wq->group;

	if (prevg)
		prevg->num_wqs--;
	wq->group = group;
	group->num_wqs++;
	return count;
}

static struct device_attribute dev_attr_wq_group_id =
		__ATTR(group_id, 0644, wq_group_id_show, wq_group_id_store);

static ssize_t wq_mode_show(struct device *dev, struct device_attribute *attr,
			    char *buf)
{
	struct idxd_wq *wq = container_of(dev, struct idxd_wq, conf_dev);

	return sprintf(buf, "%s\n",
			wq_dedicated(wq) ? "dedicated" : "shared");
}

static ssize_t wq_mode_store(struct device *dev,
			     struct device_attribute *attr, const char *buf,
			     size_t count)
{
	struct idxd_wq *wq = container_of(dev, struct idxd_wq, conf_dev);
	struct idxd_device *idxd = wq->idxd;

	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
		return -EPERM;

	if (wq->state != IDXD_WQ_DISABLED)
		return -EPERM;

	if (sysfs_streq(buf, "dedicated")) {
		set_bit(WQ_FLAG_DEDICATED, &wq->flags);
		wq->threshold = 0;
937 938
	} else if (sysfs_streq(buf, "shared") && device_swq_supported(idxd)) {
		clear_bit(WQ_FLAG_DEDICATED, &wq->flags);
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
	} else {
		return -EINVAL;
	}

	return count;
}

static struct device_attribute dev_attr_wq_mode =
		__ATTR(mode, 0644, wq_mode_show, wq_mode_store);

static ssize_t wq_size_show(struct device *dev, struct device_attribute *attr,
			    char *buf)
{
	struct idxd_wq *wq = container_of(dev, struct idxd_wq, conf_dev);

	return sprintf(buf, "%u\n", wq->size);
}

957 958 959 960 961 962 963 964 965 966 967 968 969 970
static int total_claimed_wq_size(struct idxd_device *idxd)
{
	int i;
	int wq_size = 0;

	for (i = 0; i < idxd->max_wqs; i++) {
		struct idxd_wq *wq = &idxd->wqs[i];

		wq_size += wq->size;
	}

	return wq_size;
}

971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
static ssize_t wq_size_store(struct device *dev,
			     struct device_attribute *attr, const char *buf,
			     size_t count)
{
	struct idxd_wq *wq = container_of(dev, struct idxd_wq, conf_dev);
	unsigned long size;
	struct idxd_device *idxd = wq->idxd;
	int rc;

	rc = kstrtoul(buf, 10, &size);
	if (rc < 0)
		return -EINVAL;

	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
		return -EPERM;

987
	if (idxd->state == IDXD_DEV_ENABLED)
988 989
		return -EPERM;

990
	if (size + total_claimed_wq_size(idxd) - wq->size > idxd->max_wq_size)
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
		return -EINVAL;

	wq->size = size;
	return count;
}

static struct device_attribute dev_attr_wq_size =
		__ATTR(size, 0644, wq_size_show, wq_size_store);

static ssize_t wq_priority_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct idxd_wq *wq = container_of(dev, struct idxd_wq, conf_dev);

	return sprintf(buf, "%u\n", wq->priority);
}

static ssize_t wq_priority_store(struct device *dev,
				 struct device_attribute *attr,
				 const char *buf, size_t count)
{
	struct idxd_wq *wq = container_of(dev, struct idxd_wq, conf_dev);
	unsigned long prio;
	struct idxd_device *idxd = wq->idxd;
	int rc;

	rc = kstrtoul(buf, 10, &prio);
	if (rc < 0)
		return -EINVAL;

	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
		return -EPERM;

	if (wq->state != IDXD_WQ_DISABLED)
		return -EPERM;

	if (prio > IDXD_MAX_PRIORITY)
		return -EINVAL;

	wq->priority = prio;
	return count;
}

static struct device_attribute dev_attr_wq_priority =
		__ATTR(priority, 0644, wq_priority_show, wq_priority_store);

1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
static ssize_t wq_block_on_fault_show(struct device *dev,
				      struct device_attribute *attr, char *buf)
{
	struct idxd_wq *wq = container_of(dev, struct idxd_wq, conf_dev);

	return sprintf(buf, "%u\n",
		       test_bit(WQ_FLAG_BLOCK_ON_FAULT, &wq->flags));
}

static ssize_t wq_block_on_fault_store(struct device *dev,
				       struct device_attribute *attr,
				       const char *buf, size_t count)
{
	struct idxd_wq *wq = container_of(dev, struct idxd_wq, conf_dev);
	struct idxd_device *idxd = wq->idxd;
	bool bof;
	int rc;

	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
		return -EPERM;

	if (wq->state != IDXD_WQ_DISABLED)
		return -ENXIO;

	rc = kstrtobool(buf, &bof);
	if (rc < 0)
		return rc;

	if (bof)
		set_bit(WQ_FLAG_BLOCK_ON_FAULT, &wq->flags);
	else
		clear_bit(WQ_FLAG_BLOCK_ON_FAULT, &wq->flags);

	return count;
}

static struct device_attribute dev_attr_wq_block_on_fault =
		__ATTR(block_on_fault, 0644, wq_block_on_fault_show,
		       wq_block_on_fault_store);

static ssize_t wq_threshold_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
{
	struct idxd_wq *wq = container_of(dev, struct idxd_wq, conf_dev);

	return sprintf(buf, "%u\n", wq->threshold);
}

static ssize_t wq_threshold_store(struct device *dev,
				  struct device_attribute *attr,
				  const char *buf, size_t count)
{
	struct idxd_wq *wq = container_of(dev, struct idxd_wq, conf_dev);
	struct idxd_device *idxd = wq->idxd;
	unsigned int val;
	int rc;

	rc = kstrtouint(buf, 0, &val);
	if (rc < 0)
		return -EINVAL;

	if (val > wq->size || val <= 0)
		return -EINVAL;

	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
		return -EPERM;

	if (wq->state != IDXD_WQ_DISABLED)
		return -ENXIO;

	if (test_bit(WQ_FLAG_DEDICATED, &wq->flags))
		return -EINVAL;

	wq->threshold = val;

	return count;
}

static struct device_attribute dev_attr_wq_threshold =
		__ATTR(threshold, 0644, wq_threshold_show, wq_threshold_store);

1118 1119 1120 1121 1122 1123 1124 1125 1126
static ssize_t wq_type_show(struct device *dev,
			    struct device_attribute *attr, char *buf)
{
	struct idxd_wq *wq = container_of(dev, struct idxd_wq, conf_dev);

	switch (wq->type) {
	case IDXD_WQT_KERNEL:
		return sprintf(buf, "%s\n",
			       idxd_wq_type_names[IDXD_WQT_KERNEL]);
1127 1128 1129
	case IDXD_WQT_USER:
		return sprintf(buf, "%s\n",
			       idxd_wq_type_names[IDXD_WQT_USER]);
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
	case IDXD_WQT_NONE:
	default:
		return sprintf(buf, "%s\n",
			       idxd_wq_type_names[IDXD_WQT_NONE]);
	}

	return -EINVAL;
}

static ssize_t wq_type_store(struct device *dev,
			     struct device_attribute *attr, const char *buf,
			     size_t count)
{
	struct idxd_wq *wq = container_of(dev, struct idxd_wq, conf_dev);
	enum idxd_wq_type old_type;

	if (wq->state != IDXD_WQ_DISABLED)
		return -EPERM;

	old_type = wq->type;
1150 1151 1152
	if (sysfs_streq(buf, idxd_wq_type_names[IDXD_WQT_NONE]))
		wq->type = IDXD_WQT_NONE;
	else if (sysfs_streq(buf, idxd_wq_type_names[IDXD_WQT_KERNEL]))
1153
		wq->type = IDXD_WQT_KERNEL;
1154 1155
	else if (sysfs_streq(buf, idxd_wq_type_names[IDXD_WQT_USER]))
		wq->type = IDXD_WQT_USER;
1156
	else
1157
		return -EINVAL;
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188

	/* If we are changing queue type, clear the name */
	if (wq->type != old_type)
		memset(wq->name, 0, WQ_NAME_SIZE + 1);

	return count;
}

static struct device_attribute dev_attr_wq_type =
		__ATTR(type, 0644, wq_type_show, wq_type_store);

static ssize_t wq_name_show(struct device *dev,
			    struct device_attribute *attr, char *buf)
{
	struct idxd_wq *wq = container_of(dev, struct idxd_wq, conf_dev);

	return sprintf(buf, "%s\n", wq->name);
}

static ssize_t wq_name_store(struct device *dev,
			     struct device_attribute *attr, const char *buf,
			     size_t count)
{
	struct idxd_wq *wq = container_of(dev, struct idxd_wq, conf_dev);

	if (wq->state != IDXD_WQ_DISABLED)
		return -EPERM;

	if (strlen(buf) > WQ_NAME_SIZE || strlen(buf) == 0)
		return -EINVAL;

1189 1190 1191 1192 1193 1194 1195
	/*
	 * This is temporarily placed here until we have SVM support for
	 * dmaengine.
	 */
	if (wq->type == IDXD_WQT_KERNEL && device_pasid_enabled(wq->idxd))
		return -EOPNOTSUPP;

1196 1197 1198 1199 1200 1201 1202 1203 1204
	memset(wq->name, 0, WQ_NAME_SIZE + 1);
	strncpy(wq->name, buf, WQ_NAME_SIZE);
	strreplace(wq->name, '\n', '\0');
	return count;
}

static struct device_attribute dev_attr_wq_name =
		__ATTR(name, 0644, wq_name_show, wq_name_store);

1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
static ssize_t wq_cdev_minor_show(struct device *dev,
				  struct device_attribute *attr, char *buf)
{
	struct idxd_wq *wq = container_of(dev, struct idxd_wq, conf_dev);

	return sprintf(buf, "%d\n", wq->idxd_cdev.minor);
}

static struct device_attribute dev_attr_wq_cdev_minor =
		__ATTR(cdev_minor, 0444, wq_cdev_minor_show, NULL);

1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
static int __get_sysfs_u64(const char *buf, u64 *val)
{
	int rc;

	rc = kstrtou64(buf, 0, val);
	if (rc < 0)
		return -EINVAL;

	if (*val == 0)
		return -EINVAL;

	*val = roundup_pow_of_two(*val);
	return 0;
}

1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
static ssize_t wq_max_transfer_size_show(struct device *dev, struct device_attribute *attr,
					 char *buf)
{
	struct idxd_wq *wq = container_of(dev, struct idxd_wq, conf_dev);

	return sprintf(buf, "%llu\n", wq->max_xfer_bytes);
}

static ssize_t wq_max_transfer_size_store(struct device *dev, struct device_attribute *attr,
					  const char *buf, size_t count)
{
	struct idxd_wq *wq = container_of(dev, struct idxd_wq, conf_dev);
	struct idxd_device *idxd = wq->idxd;
	u64 xfer_size;
	int rc;

	if (wq->state != IDXD_WQ_DISABLED)
		return -EPERM;

1250
	rc = __get_sysfs_u64(buf, &xfer_size);
1251
	if (rc < 0)
1252
		return rc;
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265

	if (xfer_size > idxd->max_xfer_bytes)
		return -EINVAL;

	wq->max_xfer_bytes = xfer_size;

	return count;
}

static struct device_attribute dev_attr_wq_max_transfer_size =
		__ATTR(max_transfer_size, 0644,
		       wq_max_transfer_size_show, wq_max_transfer_size_store);

1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
static ssize_t wq_max_batch_size_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct idxd_wq *wq = container_of(dev, struct idxd_wq, conf_dev);

	return sprintf(buf, "%u\n", wq->max_batch_size);
}

static ssize_t wq_max_batch_size_store(struct device *dev, struct device_attribute *attr,
				       const char *buf, size_t count)
{
	struct idxd_wq *wq = container_of(dev, struct idxd_wq, conf_dev);
	struct idxd_device *idxd = wq->idxd;
	u64 batch_size;
	int rc;

	if (wq->state != IDXD_WQ_DISABLED)
		return -EPERM;

	rc = __get_sysfs_u64(buf, &batch_size);
	if (rc < 0)
		return rc;

	if (batch_size > idxd->max_batch_size)
		return -EINVAL;

	wq->max_batch_size = (u32)batch_size;

	return count;
}

static struct device_attribute dev_attr_wq_max_batch_size =
		__ATTR(max_batch_size, 0644, wq_max_batch_size_show, wq_max_batch_size_store);

1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
static ssize_t wq_ats_disable_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct idxd_wq *wq = container_of(dev, struct idxd_wq, conf_dev);

	return sprintf(buf, "%u\n", wq->ats_dis);
}

static ssize_t wq_ats_disable_store(struct device *dev, struct device_attribute *attr,
				    const char *buf, size_t count)
{
	struct idxd_wq *wq = container_of(dev, struct idxd_wq, conf_dev);
	struct idxd_device *idxd = wq->idxd;
	bool ats_dis;
	int rc;

	if (wq->state != IDXD_WQ_DISABLED)
		return -EPERM;

	if (!idxd->hw.wq_cap.wq_ats_support)
		return -EOPNOTSUPP;

	rc = kstrtobool(buf, &ats_dis);
	if (rc < 0)
		return rc;

	wq->ats_dis = ats_dis;

	return count;
}

static struct device_attribute dev_attr_wq_ats_disable =
		__ATTR(ats_disable, 0644, wq_ats_disable_show, wq_ats_disable_store);

1332 1333 1334 1335 1336 1337 1338
static struct attribute *idxd_wq_attributes[] = {
	&dev_attr_wq_clients.attr,
	&dev_attr_wq_state.attr,
	&dev_attr_wq_group_id.attr,
	&dev_attr_wq_mode.attr,
	&dev_attr_wq_size.attr,
	&dev_attr_wq_priority.attr,
1339 1340
	&dev_attr_wq_block_on_fault.attr,
	&dev_attr_wq_threshold.attr,
1341 1342
	&dev_attr_wq_type.attr,
	&dev_attr_wq_name.attr,
1343
	&dev_attr_wq_cdev_minor.attr,
1344
	&dev_attr_wq_max_transfer_size.attr,
1345
	&dev_attr_wq_max_batch_size.attr,
1346
	&dev_attr_wq_ats_disable.attr,
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
	NULL,
};

static const struct attribute_group idxd_wq_attribute_group = {
	.attrs = idxd_wq_attributes,
};

static const struct attribute_group *idxd_wq_attribute_groups[] = {
	&idxd_wq_attribute_group,
	NULL,
};

/* IDXD device attribs */
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
static ssize_t version_show(struct device *dev, struct device_attribute *attr,
			    char *buf)
{
	struct idxd_device *idxd =
		container_of(dev, struct idxd_device, conf_dev);

	return sprintf(buf, "%#x\n", idxd->hw.version);
}
static DEVICE_ATTR_RO(version);

1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
static ssize_t max_work_queues_size_show(struct device *dev,
					 struct device_attribute *attr,
					 char *buf)
{
	struct idxd_device *idxd =
		container_of(dev, struct idxd_device, conf_dev);

	return sprintf(buf, "%u\n", idxd->max_wq_size);
}
static DEVICE_ATTR_RO(max_work_queues_size);

static ssize_t max_groups_show(struct device *dev,
			       struct device_attribute *attr, char *buf)
{
	struct idxd_device *idxd =
		container_of(dev, struct idxd_device, conf_dev);

	return sprintf(buf, "%u\n", idxd->max_groups);
}
static DEVICE_ATTR_RO(max_groups);

static ssize_t max_work_queues_show(struct device *dev,
				    struct device_attribute *attr, char *buf)
{
	struct idxd_device *idxd =
		container_of(dev, struct idxd_device, conf_dev);

	return sprintf(buf, "%u\n", idxd->max_wqs);
}
static DEVICE_ATTR_RO(max_work_queues);

static ssize_t max_engines_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct idxd_device *idxd =
		container_of(dev, struct idxd_device, conf_dev);

	return sprintf(buf, "%u\n", idxd->max_engines);
}
static DEVICE_ATTR_RO(max_engines);

static ssize_t numa_node_show(struct device *dev,
			      struct device_attribute *attr, char *buf)
{
	struct idxd_device *idxd =
		container_of(dev, struct idxd_device, conf_dev);

	return sprintf(buf, "%d\n", dev_to_node(&idxd->pdev->dev));
}
static DEVICE_ATTR_RO(numa_node);

static ssize_t max_batch_size_show(struct device *dev,
				   struct device_attribute *attr, char *buf)
{
	struct idxd_device *idxd =
		container_of(dev, struct idxd_device, conf_dev);

	return sprintf(buf, "%u\n", idxd->max_batch_size);
}
static DEVICE_ATTR_RO(max_batch_size);

static ssize_t max_transfer_size_show(struct device *dev,
				      struct device_attribute *attr,
				      char *buf)
{
	struct idxd_device *idxd =
		container_of(dev, struct idxd_device, conf_dev);

	return sprintf(buf, "%llu\n", idxd->max_xfer_bytes);
}
static DEVICE_ATTR_RO(max_transfer_size);

static ssize_t op_cap_show(struct device *dev,
			   struct device_attribute *attr, char *buf)
{
	struct idxd_device *idxd =
		container_of(dev, struct idxd_device, conf_dev);
1447 1448 1449 1450
	int i, rc = 0;

	for (i = 0; i < 4; i++)
		rc += sysfs_emit_at(buf, rc, "%#llx ", idxd->hw.opcap.bits[i]);
1451

1452 1453 1454
	rc--;
	rc += sysfs_emit_at(buf, rc, "\n");
	return rc;
1455 1456 1457
}
static DEVICE_ATTR_RO(op_cap);

1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
static ssize_t gen_cap_show(struct device *dev,
			    struct device_attribute *attr, char *buf)
{
	struct idxd_device *idxd =
		container_of(dev, struct idxd_device, conf_dev);

	return sprintf(buf, "%#llx\n", idxd->hw.gen_cap.bits);
}
static DEVICE_ATTR_RO(gen_cap);

1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
static ssize_t configurable_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
{
	struct idxd_device *idxd =
		container_of(dev, struct idxd_device, conf_dev);

	return sprintf(buf, "%u\n",
			test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags));
}
static DEVICE_ATTR_RO(configurable);

static ssize_t clients_show(struct device *dev,
			    struct device_attribute *attr, char *buf)
{
	struct idxd_device *idxd =
		container_of(dev, struct idxd_device, conf_dev);
	unsigned long flags;
	int count = 0, i;

	spin_lock_irqsave(&idxd->dev_lock, flags);
	for (i = 0; i < idxd->max_wqs; i++) {
		struct idxd_wq *wq = &idxd->wqs[i];

		count += wq->client_count;
	}
	spin_unlock_irqrestore(&idxd->dev_lock, flags);

	return sprintf(buf, "%d\n", count);
}
static DEVICE_ATTR_RO(clients);

1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
static ssize_t pasid_enabled_show(struct device *dev,
				  struct device_attribute *attr, char *buf)
{
	struct idxd_device *idxd =
		container_of(dev, struct idxd_device, conf_dev);

	return sprintf(buf, "%u\n", device_pasid_enabled(idxd));
}
static DEVICE_ATTR_RO(pasid_enabled);

1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
static ssize_t state_show(struct device *dev,
			  struct device_attribute *attr, char *buf)
{
	struct idxd_device *idxd =
		container_of(dev, struct idxd_device, conf_dev);

	switch (idxd->state) {
	case IDXD_DEV_DISABLED:
	case IDXD_DEV_CONF_READY:
		return sprintf(buf, "disabled\n");
	case IDXD_DEV_ENABLED:
		return sprintf(buf, "enabled\n");
	case IDXD_DEV_HALTED:
		return sprintf(buf, "halted\n");
	}

	return sprintf(buf, "unknown\n");
}
static DEVICE_ATTR_RO(state);

static ssize_t errors_show(struct device *dev,
			   struct device_attribute *attr, char *buf)
{
	struct idxd_device *idxd =
		container_of(dev, struct idxd_device, conf_dev);
	int i, out = 0;
	unsigned long flags;

	spin_lock_irqsave(&idxd->dev_lock, flags);
	for (i = 0; i < 4; i++)
		out += sprintf(buf + out, "%#018llx ", idxd->sw_err.bits[i]);
	spin_unlock_irqrestore(&idxd->dev_lock, flags);
	out--;
	out += sprintf(buf + out, "\n");
	return out;
}
static DEVICE_ATTR_RO(errors);

static ssize_t max_tokens_show(struct device *dev,
			       struct device_attribute *attr, char *buf)
{
	struct idxd_device *idxd =
		container_of(dev, struct idxd_device, conf_dev);

	return sprintf(buf, "%u\n", idxd->max_tokens);
}
static DEVICE_ATTR_RO(max_tokens);

static ssize_t token_limit_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct idxd_device *idxd =
		container_of(dev, struct idxd_device, conf_dev);

	return sprintf(buf, "%u\n", idxd->token_limit);
}

static ssize_t token_limit_store(struct device *dev,
				 struct device_attribute *attr,
				 const char *buf, size_t count)
{
	struct idxd_device *idxd =
		container_of(dev, struct idxd_device, conf_dev);
	unsigned long val;
	int rc;

	rc = kstrtoul(buf, 10, &val);
	if (rc < 0)
		return -EINVAL;

	if (idxd->state == IDXD_DEV_ENABLED)
		return -EPERM;

	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
		return -EPERM;

	if (!idxd->hw.group_cap.token_limit)
		return -EPERM;

	if (val > idxd->hw.group_cap.total_tokens)
		return -EINVAL;

	idxd->token_limit = val;
	return count;
}
static DEVICE_ATTR_RW(token_limit);

1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
static ssize_t cdev_major_show(struct device *dev,
			       struct device_attribute *attr, char *buf)
{
	struct idxd_device *idxd =
		container_of(dev, struct idxd_device, conf_dev);

	return sprintf(buf, "%u\n", idxd->major);
}
static DEVICE_ATTR_RO(cdev_major);

1606 1607 1608 1609 1610 1611 1612 1613 1614
static ssize_t cmd_status_show(struct device *dev,
			       struct device_attribute *attr, char *buf)
{
	struct idxd_device *idxd = container_of(dev, struct idxd_device, conf_dev);

	return sprintf(buf, "%#x\n", idxd->cmd_status);
}
static DEVICE_ATTR_RO(cmd_status);

1615
static struct attribute *idxd_device_attributes[] = {
1616
	&dev_attr_version.attr,
1617 1618 1619 1620 1621 1622 1623 1624
	&dev_attr_max_groups.attr,
	&dev_attr_max_work_queues.attr,
	&dev_attr_max_work_queues_size.attr,
	&dev_attr_max_engines.attr,
	&dev_attr_numa_node.attr,
	&dev_attr_max_batch_size.attr,
	&dev_attr_max_transfer_size.attr,
	&dev_attr_op_cap.attr,
1625
	&dev_attr_gen_cap.attr,
1626 1627
	&dev_attr_configurable.attr,
	&dev_attr_clients.attr,
1628
	&dev_attr_pasid_enabled.attr,
1629 1630 1631 1632
	&dev_attr_state.attr,
	&dev_attr_errors.attr,
	&dev_attr_max_tokens.attr,
	&dev_attr_token_limit.attr,
1633
	&dev_attr_cdev_major.attr,
1634
	&dev_attr_cmd_status.attr,
1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843
	NULL,
};

static const struct attribute_group idxd_device_attribute_group = {
	.attrs = idxd_device_attributes,
};

static const struct attribute_group *idxd_attribute_groups[] = {
	&idxd_device_attribute_group,
	NULL,
};

static int idxd_setup_engine_sysfs(struct idxd_device *idxd)
{
	struct device *dev = &idxd->pdev->dev;
	int i, rc;

	for (i = 0; i < idxd->max_engines; i++) {
		struct idxd_engine *engine = &idxd->engines[i];

		engine->conf_dev.parent = &idxd->conf_dev;
		dev_set_name(&engine->conf_dev, "engine%d.%d",
			     idxd->id, engine->id);
		engine->conf_dev.bus = idxd_get_bus_type(idxd);
		engine->conf_dev.groups = idxd_engine_attribute_groups;
		engine->conf_dev.type = &idxd_engine_device_type;
		dev_dbg(dev, "Engine device register: %s\n",
			dev_name(&engine->conf_dev));
		rc = device_register(&engine->conf_dev);
		if (rc < 0) {
			put_device(&engine->conf_dev);
			goto cleanup;
		}
	}

	return 0;

cleanup:
	while (i--) {
		struct idxd_engine *engine = &idxd->engines[i];

		device_unregister(&engine->conf_dev);
	}
	return rc;
}

static int idxd_setup_group_sysfs(struct idxd_device *idxd)
{
	struct device *dev = &idxd->pdev->dev;
	int i, rc;

	for (i = 0; i < idxd->max_groups; i++) {
		struct idxd_group *group = &idxd->groups[i];

		group->conf_dev.parent = &idxd->conf_dev;
		dev_set_name(&group->conf_dev, "group%d.%d",
			     idxd->id, group->id);
		group->conf_dev.bus = idxd_get_bus_type(idxd);
		group->conf_dev.groups = idxd_group_attribute_groups;
		group->conf_dev.type = &idxd_group_device_type;
		dev_dbg(dev, "Group device register: %s\n",
			dev_name(&group->conf_dev));
		rc = device_register(&group->conf_dev);
		if (rc < 0) {
			put_device(&group->conf_dev);
			goto cleanup;
		}
	}

	return 0;

cleanup:
	while (i--) {
		struct idxd_group *group = &idxd->groups[i];

		device_unregister(&group->conf_dev);
	}
	return rc;
}

static int idxd_setup_wq_sysfs(struct idxd_device *idxd)
{
	struct device *dev = &idxd->pdev->dev;
	int i, rc;

	for (i = 0; i < idxd->max_wqs; i++) {
		struct idxd_wq *wq = &idxd->wqs[i];

		wq->conf_dev.parent = &idxd->conf_dev;
		dev_set_name(&wq->conf_dev, "wq%d.%d", idxd->id, wq->id);
		wq->conf_dev.bus = idxd_get_bus_type(idxd);
		wq->conf_dev.groups = idxd_wq_attribute_groups;
		wq->conf_dev.type = &idxd_wq_device_type;
		dev_dbg(dev, "WQ device register: %s\n",
			dev_name(&wq->conf_dev));
		rc = device_register(&wq->conf_dev);
		if (rc < 0) {
			put_device(&wq->conf_dev);
			goto cleanup;
		}
	}

	return 0;

cleanup:
	while (i--) {
		struct idxd_wq *wq = &idxd->wqs[i];

		device_unregister(&wq->conf_dev);
	}
	return rc;
}

static int idxd_setup_device_sysfs(struct idxd_device *idxd)
{
	struct device *dev = &idxd->pdev->dev;
	int rc;
	char devname[IDXD_NAME_SIZE];

	sprintf(devname, "%s%d", idxd_get_dev_name(idxd), idxd->id);
	idxd->conf_dev.parent = dev;
	dev_set_name(&idxd->conf_dev, "%s", devname);
	idxd->conf_dev.bus = idxd_get_bus_type(idxd);
	idxd->conf_dev.groups = idxd_attribute_groups;
	idxd->conf_dev.type = idxd_get_device_type(idxd);

	dev_dbg(dev, "IDXD device register: %s\n", dev_name(&idxd->conf_dev));
	rc = device_register(&idxd->conf_dev);
	if (rc < 0) {
		put_device(&idxd->conf_dev);
		return rc;
	}

	return 0;
}

int idxd_setup_sysfs(struct idxd_device *idxd)
{
	struct device *dev = &idxd->pdev->dev;
	int rc;

	rc = idxd_setup_device_sysfs(idxd);
	if (rc < 0) {
		dev_dbg(dev, "Device sysfs registering failed: %d\n", rc);
		return rc;
	}

	rc = idxd_setup_wq_sysfs(idxd);
	if (rc < 0) {
		/* unregister conf dev */
		dev_dbg(dev, "Work Queue sysfs registering failed: %d\n", rc);
		return rc;
	}

	rc = idxd_setup_group_sysfs(idxd);
	if (rc < 0) {
		/* unregister conf dev */
		dev_dbg(dev, "Group sysfs registering failed: %d\n", rc);
		return rc;
	}

	rc = idxd_setup_engine_sysfs(idxd);
	if (rc < 0) {
		/* unregister conf dev */
		dev_dbg(dev, "Engine sysfs registering failed: %d\n", rc);
		return rc;
	}

	return 0;
}

void idxd_cleanup_sysfs(struct idxd_device *idxd)
{
	int i;

	for (i = 0; i < idxd->max_wqs; i++) {
		struct idxd_wq *wq = &idxd->wqs[i];

		device_unregister(&wq->conf_dev);
	}

	for (i = 0; i < idxd->max_engines; i++) {
		struct idxd_engine *engine = &idxd->engines[i];

		device_unregister(&engine->conf_dev);
	}

	for (i = 0; i < idxd->max_groups; i++) {
		struct idxd_group *group = &idxd->groups[i];

		device_unregister(&group->conf_dev);
	}

	device_unregister(&idxd->conf_dev);
}

int idxd_register_bus_type(void)
{
	int i, rc;

	for (i = 0; i < IDXD_TYPE_MAX; i++) {
		rc = bus_register(idxd_bus_types[i]);
		if (rc < 0)
			goto bus_err;
	}

	return 0;

bus_err:
1844
	while (--i >= 0)
1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
		bus_unregister(idxd_bus_types[i]);
	return rc;
}

void idxd_unregister_bus_type(void)
{
	int i;

	for (i = 0; i < IDXD_TYPE_MAX; i++)
		bus_unregister(idxd_bus_types[i]);
}