iov.c 16.0 KB
Newer Older
1 2 3 4 5 6 7
/*
 * drivers/pci/iov.c
 *
 * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com>
 *
 * PCI Express I/O Virtualization (IOV) support.
 *   Single Root IOV 1.0
Y
Yu Zhao 已提交
8
 *   Address Translation Service 1.0
9 10 11
 */

#include <linux/pci.h>
12
#include <linux/slab.h>
13
#include <linux/mutex.h>
14
#include <linux/export.h>
15 16
#include <linux/string.h>
#include <linux/delay.h>
17
#include <linux/pci-ats.h>
18 19
#include "pci.h"

20
#define VIRTFN_ID_LEN	16
21

22 23 24 25 26 27 28 29 30 31 32 33
static inline u8 virtfn_bus(struct pci_dev *dev, int id)
{
	return dev->bus->number + ((dev->devfn + dev->sriov->offset +
				    dev->sriov->stride * id) >> 8);
}

static inline u8 virtfn_devfn(struct pci_dev *dev, int id)
{
	return (dev->devfn + dev->sriov->offset +
		dev->sriov->stride * id) & 0xff;
}

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
{
	struct pci_bus *child;

	if (bus->number == busnr)
		return bus;

	child = pci_find_bus(pci_domain_nr(bus), busnr);
	if (child)
		return child;

	child = pci_add_new_bus(bus, NULL, busnr);
	if (!child)
		return NULL;

Y
Yinghai Lu 已提交
49
	pci_bus_insert_busn_res(child, busnr, busnr);
50 51 52 53

	return child;
}

54
static void virtfn_remove_bus(struct pci_bus *physbus, struct pci_bus *virtbus)
55
{
56 57
	if (physbus != virtbus && list_empty(&virtbus->devices))
		pci_remove_bus(virtbus);
58 59 60 61 62
}

static int virtfn_add(struct pci_dev *dev, int id, int reset)
{
	int i;
63
	int rc = -ENOMEM;
64 65 66 67 68
	u64 size;
	char buf[VIRTFN_ID_LEN];
	struct pci_dev *virtfn;
	struct resource *res;
	struct pci_sriov *iov = dev->sriov;
69
	struct pci_bus *bus;
70 71

	mutex_lock(&iov->dev->sriov->lock);
72
	bus = virtfn_add_bus(dev->bus, virtfn_bus(dev, id));
73 74 75 76
	if (!bus)
		goto failed;

	virtfn = pci_alloc_dev(bus);
77
	if (!virtfn)
78
		goto failed0;
79 80 81 82 83 84

	virtfn->devfn = virtfn_devfn(dev, id);
	virtfn->vendor = dev->vendor;
	pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_DID, &virtfn->device);
	pci_setup_device(virtfn);
	virtfn->dev.parent = dev->dev.parent;
85 86
	virtfn->physfn = pci_dev_get(dev);
	virtfn->is_virtfn = 1;
87
	virtfn->multifunction = 0;
88 89 90 91 92 93 94 95

	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
		res = dev->resource + PCI_IOV_RESOURCES + i;
		if (!res->parent)
			continue;
		virtfn->resource[i].name = pci_name(virtfn);
		virtfn->resource[i].flags = res->flags;
		size = resource_size(res);
96
		do_div(size, iov->total_VFs);
97 98 99 100 101 102 103
		virtfn->resource[i].start = res->start + size * id;
		virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
		rc = request_resource(res, &virtfn->resource[i]);
		BUG_ON(rc);
	}

	if (reset)
Y
Yu Zhao 已提交
104
		__pci_reset_function(virtfn);
105 106 107 108

	pci_device_add(virtfn, virtfn->bus);
	mutex_unlock(&iov->dev->sriov->lock);

109
	pci_bus_add_device(virtfn);
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
	sprintf(buf, "virtfn%u", id);
	rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
	if (rc)
		goto failed1;
	rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
	if (rc)
		goto failed2;

	kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);

	return 0;

failed2:
	sysfs_remove_link(&dev->dev.kobj, buf);
failed1:
	pci_dev_put(dev);
	mutex_lock(&iov->dev->sriov->lock);
127
	pci_stop_and_remove_bus_device(virtfn);
128 129 130
failed0:
	virtfn_remove_bus(dev->bus, bus);
failed:
131 132 133 134 135 136 137 138 139 140 141
	mutex_unlock(&iov->dev->sriov->lock);

	return rc;
}

static void virtfn_remove(struct pci_dev *dev, int id, int reset)
{
	char buf[VIRTFN_ID_LEN];
	struct pci_dev *virtfn;
	struct pci_sriov *iov = dev->sriov;

142 143 144
	virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus),
					     virtfn_bus(dev, id),
					     virtfn_devfn(dev, id));
145 146 147 148 149
	if (!virtfn)
		return;

	if (reset) {
		device_release_driver(&virtfn->dev);
Y
Yu Zhao 已提交
150
		__pci_reset_function(virtfn);
151 152 153 154
	}

	sprintf(buf, "virtfn%u", id);
	sysfs_remove_link(&dev->dev.kobj, buf);
155 156 157 158 159 160 161
	/*
	 * pci_stop_dev() could have been called for this virtfn already,
	 * so the directory for the virtfn may have been removed before.
	 * Double check to avoid spurious sysfs warnings.
	 */
	if (virtfn->dev.kobj.sd)
		sysfs_remove_link(&virtfn->dev.kobj, "physfn");
162 163

	mutex_lock(&iov->dev->sriov->lock);
164
	pci_stop_and_remove_bus_device(virtfn);
165
	virtfn_remove_bus(dev->bus, virtfn->bus);
166 167
	mutex_unlock(&iov->dev->sriov->lock);

168 169
	/* balance pci_get_domain_bus_and_slot() */
	pci_dev_put(virtfn);
170 171 172 173 174 175 176 177 178 179 180 181
	pci_dev_put(dev);
}

static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
{
	int rc;
	int i, j;
	int nres;
	u16 offset, stride, initial;
	struct resource *res;
	struct pci_dev *pdev;
	struct pci_sriov *iov = dev->sriov;
R
Ram Pai 已提交
182
	int bars = 0;
183 184 185 186

	if (!nr_virtfn)
		return 0;

187
	if (iov->num_VFs)
188 189 190
		return -EINVAL;

	pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
191 192
	if (initial > iov->total_VFs ||
	    (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
193 194
		return -EIO;

195
	if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
196 197 198 199 200 201 202 203 204 205
	    (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
		return -EINVAL;

	pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &offset);
	pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &stride);
	if (!offset || (nr_virtfn > 1 && !stride))
		return -EIO;

	nres = 0;
	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
R
Ram Pai 已提交
206
		bars |= (1 << (i + PCI_IOV_RESOURCES));
207 208 209 210 211 212 213 214 215 216 217 218
		res = dev->resource + PCI_IOV_RESOURCES + i;
		if (res->parent)
			nres++;
	}
	if (nres != iov->nres) {
		dev_err(&dev->dev, "not enough MMIO resources for SR-IOV\n");
		return -ENOMEM;
	}

	iov->offset = offset;
	iov->stride = stride;

219
	if (virtfn_bus(dev, nr_virtfn - 1) > dev->bus->busn_res.end) {
220 221 222 223
		dev_err(&dev->dev, "SR-IOV: bus number out of range\n");
		return -ENOMEM;
	}

R
Ram Pai 已提交
224 225 226 227 228
	if (pci_enable_resources(dev, bars)) {
		dev_err(&dev->dev, "SR-IOV: IOV BARS not allocated\n");
		return -ENOMEM;
	}

229 230 231 232 233
	if (iov->link != dev->devfn) {
		pdev = pci_get_slot(dev->bus, iov->link);
		if (!pdev)
			return -ENODEV;

234 235
		if (!pdev->is_physfn) {
			pci_dev_put(pdev);
236
			return -ENOSYS;
237
		}
238 239 240

		rc = sysfs_create_link(&dev->dev.kobj,
					&pdev->dev.kobj, "dep_link");
241
		pci_dev_put(pdev);
242 243 244 245
		if (rc)
			return rc;
	}

246
	pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
247
	iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
248
	pci_cfg_access_lock(dev);
249 250
	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
	msleep(100);
251
	pci_cfg_access_unlock(dev);
252

253
	iov->initial_VFs = initial;
254 255 256 257 258 259 260 261 262 263
	if (nr_virtfn < initial)
		initial = nr_virtfn;

	for (i = 0; i < initial; i++) {
		rc = virtfn_add(dev, i, 0);
		if (rc)
			goto failed;
	}

	kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
264
	iov->num_VFs = nr_virtfn;
265 266 267 268 269 270 271 272

	return 0;

failed:
	for (j = 0; j < i; j++)
		virtfn_remove(dev, j, 0);

	iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
273
	pci_cfg_access_lock(dev);
274
	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
275
	pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, 0);
276
	ssleep(1);
277
	pci_cfg_access_unlock(dev);
278 279 280 281 282 283 284 285 286 287 288 289

	if (iov->link != dev->devfn)
		sysfs_remove_link(&dev->dev.kobj, "dep_link");

	return rc;
}

static void sriov_disable(struct pci_dev *dev)
{
	int i;
	struct pci_sriov *iov = dev->sriov;

290
	if (!iov->num_VFs)
291 292
		return;

293
	for (i = 0; i < iov->num_VFs; i++)
294 295 296
		virtfn_remove(dev, i, 0);

	iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
297
	pci_cfg_access_lock(dev);
298 299
	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
	ssleep(1);
300
	pci_cfg_access_unlock(dev);
301 302 303 304

	if (iov->link != dev->devfn)
		sysfs_remove_link(&dev->dev.kobj, "dep_link");

305
	iov->num_VFs = 0;
306
	pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, 0);
307 308
}

309 310 311 312 313 314 315 316 317 318 319
static int sriov_init(struct pci_dev *dev, int pos)
{
	int i;
	int rc;
	int nres;
	u32 pgsz;
	u16 ctrl, total, offset, stride;
	struct pci_sriov *iov;
	struct resource *res;
	struct pci_dev *pdev;

320 321
	if (pci_pcie_type(dev) != PCI_EXP_TYPE_RC_END &&
	    pci_pcie_type(dev) != PCI_EXP_TYPE_ENDPOINT)
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
		return -ENODEV;

	pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
	if (ctrl & PCI_SRIOV_CTRL_VFE) {
		pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
		ssleep(1);
	}

	pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
	if (!total)
		return 0;

	ctrl = 0;
	list_for_each_entry(pdev, &dev->bus->devices, bus_list)
		if (pdev->is_physfn)
			goto found;

	pdev = NULL;
	if (pci_ari_enabled(dev->bus))
		ctrl |= PCI_SRIOV_CTRL_ARI;

found:
	pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
345
	pci_write_config_word(dev, pos + PCI_SRIOV_NUM_VF, 0);
346 347 348 349 350 351 352 353 354 355 356 357
	pci_read_config_word(dev, pos + PCI_SRIOV_VF_OFFSET, &offset);
	pci_read_config_word(dev, pos + PCI_SRIOV_VF_STRIDE, &stride);
	if (!offset || (total > 1 && !stride))
		return -EIO;

	pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
	i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
	pgsz &= ~((1 << i) - 1);
	if (!pgsz)
		return -EIO;

	pgsz &= ~(pgsz - 1);
358
	pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383

	nres = 0;
	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
		res = dev->resource + PCI_IOV_RESOURCES + i;
		i += __pci_read_base(dev, pci_bar_unknown, res,
				     pos + PCI_SRIOV_BAR + i * 4);
		if (!res->flags)
			continue;
		if (resource_size(res) & (PAGE_SIZE - 1)) {
			rc = -EIO;
			goto failed;
		}
		res->end = res->start + resource_size(res) * total - 1;
		nres++;
	}

	iov = kzalloc(sizeof(*iov), GFP_KERNEL);
	if (!iov) {
		rc = -ENOMEM;
		goto failed;
	}

	iov->pos = pos;
	iov->nres = nres;
	iov->ctrl = ctrl;
384
	iov->total_VFs = total;
385 386 387 388 389 390
	iov->offset = offset;
	iov->stride = stride;
	iov->pgsz = pgsz;
	iov->self = dev;
	pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
	pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
391
	if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
392
		iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
393 394 395

	if (pdev)
		iov->dev = pci_dev_get(pdev);
396
	else
397
		iov->dev = dev;
398 399

	mutex_init(&iov->lock);
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416

	dev->sriov = iov;
	dev->is_physfn = 1;

	return 0;

failed:
	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
		res = dev->resource + PCI_IOV_RESOURCES + i;
		res->flags = 0;
	}

	return rc;
}

static void sriov_release(struct pci_dev *dev)
{
417
	BUG_ON(dev->sriov->num_VFs);
418

419
	if (dev != dev->sriov->dev)
420 421
		pci_dev_put(dev->sriov->dev);

422 423
	mutex_destroy(&dev->sriov->lock);

424 425 426 427
	kfree(dev->sriov);
	dev->sriov = NULL;
}

Y
Yu Zhao 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440 441
static void sriov_restore_state(struct pci_dev *dev)
{
	int i;
	u16 ctrl;
	struct pci_sriov *iov = dev->sriov;

	pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
	if (ctrl & PCI_SRIOV_CTRL_VFE)
		return;

	for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++)
		pci_update_resource(dev, i);

	pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
442
	pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, iov->num_VFs);
Y
Yu Zhao 已提交
443 444 445 446 447
	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
	if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
		msleep(100);
}

448 449 450 451 452 453 454 455 456 457
/**
 * pci_iov_init - initialize the IOV capability
 * @dev: the PCI device
 *
 * Returns 0 on success, or negative on failure.
 */
int pci_iov_init(struct pci_dev *dev)
{
	int pos;

458
	if (!pci_is_pcie(dev))
459 460 461 462 463 464 465 466 467 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
		return -ENODEV;

	pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
	if (pos)
		return sriov_init(dev, pos);

	return -ENODEV;
}

/**
 * pci_iov_release - release resources used by the IOV capability
 * @dev: the PCI device
 */
void pci_iov_release(struct pci_dev *dev)
{
	if (dev->is_physfn)
		sriov_release(dev);
}

/**
 * pci_iov_resource_bar - get position of the SR-IOV BAR
 * @dev: the PCI device
 * @resno: the resource number
 * @type: the BAR type to be filled in
 *
 * Returns position of the BAR encapsulated in the SR-IOV capability.
 */
int pci_iov_resource_bar(struct pci_dev *dev, int resno,
			 enum pci_bar_type *type)
{
	if (resno < PCI_IOV_RESOURCES || resno > PCI_IOV_RESOURCE_END)
		return 0;

	BUG_ON(!dev->is_physfn);

	*type = pci_bar_unknown;

	return dev->sriov->pos + PCI_SRIOV_BAR +
		4 * (resno - PCI_IOV_RESOURCES);
}
Y
Yu Zhao 已提交
499

500 501 502 503 504 505 506 507 508 509
/**
 * pci_sriov_resource_alignment - get resource alignment for VF BAR
 * @dev: the PCI device
 * @resno: the resource number
 *
 * Returns the alignment of the VF BAR found in the SR-IOV capability.
 * This is not the same as the resource size which is defined as
 * the VF BAR size multiplied by the number of VFs.  The alignment
 * is just the VF BAR size.
 */
510
resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
511 512 513 514
{
	struct resource tmp;
	enum pci_bar_type type;
	int reg = pci_iov_resource_bar(dev, resno, &type);
515

516 517 518 519 520 521 522
	if (!reg)
		return 0;

	 __pci_read_base(dev, type, &tmp, reg);
	return resource_alignment(&tmp);
}

Y
Yu Zhao 已提交
523 524 525 526 527 528 529 530 531
/**
 * pci_restore_iov_state - restore the state of the IOV capability
 * @dev: the PCI device
 */
void pci_restore_iov_state(struct pci_dev *dev)
{
	if (dev->is_physfn)
		sriov_restore_state(dev);
}
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548

/**
 * pci_iov_bus_range - find bus range used by Virtual Function
 * @bus: the PCI bus
 *
 * Returns max number of buses (exclude current one) used by Virtual
 * Functions.
 */
int pci_iov_bus_range(struct pci_bus *bus)
{
	int max = 0;
	u8 busnr;
	struct pci_dev *dev;

	list_for_each_entry(dev, &bus->devices, bus_list) {
		if (!dev->is_physfn)
			continue;
549
		busnr = virtfn_bus(dev, dev->sriov->total_VFs - 1);
550 551 552 553 554 555
		if (busnr > max)
			max = busnr;
	}

	return max ? max - bus->number : 0;
}
556 557 558 559

/**
 * pci_enable_sriov - enable the SR-IOV capability
 * @dev: the PCI device
R
Randy Dunlap 已提交
560
 * @nr_virtfn: number of virtual functions to enable
561 562 563 564 565 566 567 568
 *
 * Returns 0 on success, or negative on failure.
 */
int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
{
	might_sleep();

	if (!dev->is_physfn)
569
		return -ENOSYS;
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588

	return sriov_enable(dev, nr_virtfn);
}
EXPORT_SYMBOL_GPL(pci_enable_sriov);

/**
 * pci_disable_sriov - disable the SR-IOV capability
 * @dev: the PCI device
 */
void pci_disable_sriov(struct pci_dev *dev)
{
	might_sleep();

	if (!dev->is_physfn)
		return;

	sriov_disable(dev);
}
EXPORT_SYMBOL_GPL(pci_disable_sriov);
589

590 591 592 593 594 595 596 597
/**
 * pci_num_vf - return number of VFs associated with a PF device_release_driver
 * @dev: the PCI device
 *
 * Returns number of VFs, or 0 if SR-IOV is not enabled.
 */
int pci_num_vf(struct pci_dev *dev)
{
B
Bjorn Helgaas 已提交
598
	if (!dev->is_physfn)
599
		return 0;
B
Bjorn Helgaas 已提交
600 601

	return dev->sriov->num_VFs;
602 603
}
EXPORT_SYMBOL_GPL(pci_num_vf);
604

605 606 607 608 609
/**
 * pci_vfs_assigned - returns number of VFs are assigned to a guest
 * @dev: the PCI device
 *
 * Returns number of VFs belonging to this device that are assigned to a guest.
610
 * If device is not a physical function returns 0.
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
 */
int pci_vfs_assigned(struct pci_dev *dev)
{
	struct pci_dev *vfdev;
	unsigned int vfs_assigned = 0;
	unsigned short dev_id;

	/* only search if we are a PF */
	if (!dev->is_physfn)
		return 0;

	/*
	 * determine the device ID for the VFs, the vendor ID will be the
	 * same as the PF so there is no need to check for that one
	 */
	pci_read_config_word(dev, dev->sriov->pos + PCI_SRIOV_VF_DID, &dev_id);

	/* loop through all the VFs to see if we own any that are assigned */
	vfdev = pci_get_device(dev->vendor, dev_id, NULL);
	while (vfdev) {
		/*
		 * It is considered assigned if it is a virtual function with
		 * our dev as the physical function and the assigned bit is set
		 */
		if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
		    (vfdev->dev_flags & PCI_DEV_FLAGS_ASSIGNED))
			vfs_assigned++;

		vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
	}

	return vfs_assigned;
}
EXPORT_SYMBOL_GPL(pci_vfs_assigned);

646 647 648
/**
 * pci_sriov_set_totalvfs -- reduce the TotalVFs available
 * @dev: the PCI PF device
649
 * @numvfs: number that should be used for TotalVFs supported
650 651 652 653 654
 *
 * Should be called from PF driver's probe routine with
 * device's mutex held.
 *
 * Returns 0 if PF is an SRIOV-capable device and
655 656
 * value of numvfs valid. If not a PF return -ENOSYS;
 * if numvfs is invalid return -EINVAL;
657 658 659 660
 * if VFs already enabled, return -EBUSY.
 */
int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
{
661 662 663
	if (!dev->is_physfn)
		return -ENOSYS;
	if (numvfs > dev->sriov->total_VFs)
664 665 666 667 668 669
		return -EINVAL;

	/* Shouldn't change if VFs already enabled */
	if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
		return -EBUSY;
	else
670
		dev->sriov->driver_max_VFs = numvfs;
671 672 673 674 675 676

	return 0;
}
EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);

/**
J
Jonghwan Choi 已提交
677
 * pci_sriov_get_totalvfs -- get total VFs supported on this device
678 679 680
 * @dev: the PCI PF device
 *
 * For a PCIe device with SRIOV support, return the PCIe
681
 * SRIOV capability value of TotalVFs or the value of driver_max_VFs
682
 * if the driver reduced it.  Otherwise 0.
683 684 685
 */
int pci_sriov_get_totalvfs(struct pci_dev *dev)
{
B
Bjorn Helgaas 已提交
686
	if (!dev->is_physfn)
687
		return 0;
688

689 690
	if (dev->sriov->driver_max_VFs)
		return dev->sriov->driver_max_VFs;
B
Bjorn Helgaas 已提交
691 692

	return dev->sriov->total_VFs;
693 694
}
EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);