iov.c 18.7 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
int pci_iov_virtfn_bus(struct pci_dev *dev, int vf_id)
23
{
24 25
	if (!dev->is_physfn)
		return -EINVAL;
26
	return dev->bus->number + ((dev->devfn + dev->sriov->offset +
27
				    dev->sriov->stride * vf_id) >> 8);
28 29
}

30
int pci_iov_virtfn_devfn(struct pci_dev *dev, int vf_id)
31
{
32 33
	if (!dev->is_physfn)
		return -EINVAL;
34
	return (dev->devfn + dev->sriov->offset +
35
		dev->sriov->stride * vf_id) & 0xff;
36 37
}

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/*
 * Per SR-IOV spec sec 3.3.10 and 3.3.11, First VF Offset and VF Stride may
 * change when NumVFs changes.
 *
 * Update iov->offset and iov->stride when NumVFs is written.
 */
static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn)
{
	struct pci_sriov *iov = dev->sriov;

	pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
	pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &iov->offset);
	pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &iov->stride);
}

53 54 55 56
/*
 * The PF consumes one bus number.  NumVFs, First VF Offset, and VF Stride
 * determine how many additional bus numbers will be consumed by VFs.
 *
57 58
 * Iterate over all valid NumVFs, validate offset and stride, and calculate
 * the maximum number of bus numbers that could ever be required.
59
 */
60
static int compute_max_vf_buses(struct pci_dev *dev)
61 62
{
	struct pci_sriov *iov = dev->sriov;
63
	int nr_virtfn, busnr, rc = 0;
64

65
	for (nr_virtfn = iov->total_VFs; nr_virtfn; nr_virtfn--) {
66
		pci_iov_set_numvfs(dev, nr_virtfn);
67 68 69 70 71
		if (!iov->offset || (nr_virtfn > 1 && !iov->stride)) {
			rc = -EIO;
			goto out;
		}

72
		busnr = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
73 74
		if (busnr > iov->max_VF_buses)
			iov->max_VF_buses = busnr;
75 76
	}

77 78 79
out:
	pci_iov_set_numvfs(dev, 0);
	return rc;
80 81
}

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
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 已提交
97
	pci_bus_insert_busn_res(child, busnr, busnr);
98 99 100 101

	return child;
}

102
static void virtfn_remove_bus(struct pci_bus *physbus, struct pci_bus *virtbus)
103
{
104 105
	if (physbus != virtbus && list_empty(&virtbus->devices))
		pci_remove_bus(virtbus);
106 107
}

108 109 110 111 112 113 114 115
resource_size_t pci_iov_resource_size(struct pci_dev *dev, int resno)
{
	if (!dev->is_physfn)
		return 0;

	return dev->sriov->barsz[resno - PCI_IOV_RESOURCES];
}

116
int pci_iov_add_virtfn(struct pci_dev *dev, int id)
117 118
{
	int i;
119
	int rc = -ENOMEM;
120 121 122 123 124
	u64 size;
	char buf[VIRTFN_ID_LEN];
	struct pci_dev *virtfn;
	struct resource *res;
	struct pci_sriov *iov = dev->sriov;
125
	struct pci_bus *bus;
126

127
	bus = virtfn_add_bus(dev->bus, pci_iov_virtfn_bus(dev, id));
128 129 130 131
	if (!bus)
		goto failed;

	virtfn = pci_alloc_dev(bus);
132
	if (!virtfn)
133
		goto failed0;
134

135
	virtfn->devfn = pci_iov_virtfn_devfn(dev, id);
136
	virtfn->vendor = dev->vendor;
137
	virtfn->device = iov->vf_device;
138 139 140 141
	rc = pci_setup_device(virtfn);
	if (rc)
		goto failed0;

142
	virtfn->dev.parent = dev->dev.parent;
143 144
	virtfn->physfn = pci_dev_get(dev);
	virtfn->is_virtfn = 1;
145
	virtfn->multifunction = 0;
146 147

	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
148
		res = &dev->resource[i + PCI_IOV_RESOURCES];
149 150 151 152
		if (!res->parent)
			continue;
		virtfn->resource[i].name = pci_name(virtfn);
		virtfn->resource[i].flags = res->flags;
153
		size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
		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);
	}

	pci_device_add(virtfn, virtfn->bus);

	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);

172 173
	pci_bus_add_device(virtfn);

174 175 176 177 178 179
	return 0;

failed2:
	sysfs_remove_link(&dev->dev.kobj, buf);
failed1:
	pci_dev_put(dev);
180
	pci_stop_and_remove_bus_device(virtfn);
181 182 183
failed0:
	virtfn_remove_bus(dev->bus, bus);
failed:
184 185 186 187

	return rc;
}

188
void pci_iov_remove_virtfn(struct pci_dev *dev, int id)
189 190 191 192
{
	char buf[VIRTFN_ID_LEN];
	struct pci_dev *virtfn;

193
	virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus),
194 195
					     pci_iov_virtfn_bus(dev, id),
					     pci_iov_virtfn_devfn(dev, id));
196 197 198 199 200
	if (!virtfn)
		return;

	sprintf(buf, "virtfn%u", id);
	sysfs_remove_link(&dev->dev.kobj, buf);
201 202 203 204 205 206 207
	/*
	 * 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");
208

209
	pci_stop_and_remove_bus_device(virtfn);
210
	virtfn_remove_bus(dev->bus, virtfn->bus);
211

212 213
	/* balance pci_get_domain_bus_and_slot() */
	pci_dev_put(virtfn);
214 215 216
	pci_dev_put(dev);
}

217 218
int __weak pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
{
219 220 221 222 223 224
	return 0;
}

int __weak pcibios_sriov_disable(struct pci_dev *pdev)
{
	return 0;
225 226
}

227 228 229
static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
{
	int rc;
230
	int i;
231
	int nres;
232
	u16 initial;
233 234 235
	struct resource *res;
	struct pci_dev *pdev;
	struct pci_sriov *iov = dev->sriov;
R
Ram Pai 已提交
236
	int bars = 0;
237
	int bus;
238 239 240 241

	if (!nr_virtfn)
		return 0;

242
	if (iov->num_VFs)
243 244 245
		return -EINVAL;

	pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
246 247
	if (initial > iov->total_VFs ||
	    (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
248 249
		return -EIO;

250
	if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
251 252 253 254 255
	    (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
		return -EINVAL;

	nres = 0;
	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
R
Ram Pai 已提交
256
		bars |= (1 << (i + PCI_IOV_RESOURCES));
257
		res = &dev->resource[i + PCI_IOV_RESOURCES];
258 259 260 261 262 263 264 265
		if (res->parent)
			nres++;
	}
	if (nres != iov->nres) {
		dev_err(&dev->dev, "not enough MMIO resources for SR-IOV\n");
		return -ENOMEM;
	}

266
	bus = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
267 268 269
	if (bus > dev->bus->busn_res.end) {
		dev_err(&dev->dev, "can't enable %d VFs (bus %02x out of range of %pR)\n",
			nr_virtfn, bus, &dev->bus->busn_res);
270 271 272
		return -ENOMEM;
	}

R
Ram Pai 已提交
273 274 275 276 277
	if (pci_enable_resources(dev, bars)) {
		dev_err(&dev->dev, "SR-IOV: IOV BARS not allocated\n");
		return -ENOMEM;
	}

278 279 280 281 282
	if (iov->link != dev->devfn) {
		pdev = pci_get_slot(dev->bus, iov->link);
		if (!pdev)
			return -ENODEV;

283 284
		if (!pdev->is_physfn) {
			pci_dev_put(pdev);
285
			return -ENOSYS;
286
		}
287 288 289

		rc = sysfs_create_link(&dev->dev.kobj,
					&pdev->dev.kobj, "dep_link");
290
		pci_dev_put(pdev);
291 292 293 294
		if (rc)
			return rc;
	}

295
	iov->initial_VFs = initial;
296 297 298
	if (nr_virtfn < initial)
		initial = nr_virtfn;

299 300 301 302
	rc = pcibios_sriov_enable(dev, initial);
	if (rc) {
		dev_err(&dev->dev, "failure %d from pcibios_sriov_enable()\n", rc);
		goto err_pcibios;
303 304
	}

305 306 307 308 309 310 311
	pci_iov_set_numvfs(dev, nr_virtfn);
	iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
	pci_cfg_access_lock(dev);
	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
	msleep(100);
	pci_cfg_access_unlock(dev);

312
	for (i = 0; i < initial; i++) {
313
		rc = pci_iov_add_virtfn(dev, i);
314 315 316 317 318
		if (rc)
			goto failed;
	}

	kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
319
	iov->num_VFs = nr_virtfn;
320 321 322 323

	return 0;

failed:
324
	while (i--)
325
		pci_iov_remove_virtfn(dev, i);
326

327
err_pcibios:
328
	iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
329
	pci_cfg_access_lock(dev);
330 331
	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
	ssleep(1);
332
	pci_cfg_access_unlock(dev);
333

334 335
	pcibios_sriov_disable(dev);

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

339
	pci_iov_set_numvfs(dev, 0);
340 341 342 343 344 345 346 347
	return rc;
}

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

348
	if (!iov->num_VFs)
349 350
		return;

351
	for (i = 0; i < iov->num_VFs; i++)
352
		pci_iov_remove_virtfn(dev, i);
353 354

	iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
355
	pci_cfg_access_lock(dev);
356 357
	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
	ssleep(1);
358
	pci_cfg_access_unlock(dev);
359

360 361
	pcibios_sriov_disable(dev);

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

365
	iov->num_VFs = 0;
366
	pci_iov_set_numvfs(dev, 0);
367 368
}

369 370
static int sriov_init(struct pci_dev *dev, int pos)
{
371
	int i, bar64;
372 373 374
	int rc;
	int nres;
	u32 pgsz;
375
	u16 ctrl, total;
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
	struct pci_sriov *iov;
	struct resource *res;
	struct pci_dev *pdev;

	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);
	}

	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);

398 399 400
	pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
	if (!total)
		return 0;
401 402 403 404 405 406 407 408

	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);
409
	pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
410

411 412 413 414
	iov = kzalloc(sizeof(*iov), GFP_KERNEL);
	if (!iov)
		return -ENOMEM;

415 416
	nres = 0;
	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
417
		res = &dev->resource[i + PCI_IOV_RESOURCES];
418 419 420 421 422 423 424 425 426
		/*
		 * If it is already FIXED, don't change it, something
		 * (perhaps EA or header fixups) wants it this way.
		 */
		if (res->flags & IORESOURCE_PCI_FIXED)
			bar64 = (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
		else
			bar64 = __pci_read_base(dev, pci_bar_unknown, res,
						pos + PCI_SRIOV_BAR + i * 4);
427 428 429 430 431 432
		if (!res->flags)
			continue;
		if (resource_size(res) & (PAGE_SIZE - 1)) {
			rc = -EIO;
			goto failed;
		}
433
		iov->barsz[i] = resource_size(res);
434
		res->end = res->start + resource_size(res) * total - 1;
435 436
		dev_info(&dev->dev, "VF(n) BAR%d space: %pR (contains BAR%d for %d VFs)\n",
			 i, res, i, total);
437
		i += bar64;
438 439 440 441 442 443
		nres++;
	}

	iov->pos = pos;
	iov->nres = nres;
	iov->ctrl = ctrl;
444
	iov->total_VFs = total;
445
	pci_read_config_word(dev, pos + PCI_SRIOV_VF_DID, &iov->vf_device);
446 447
	iov->pgsz = pgsz;
	iov->self = dev;
448
	iov->drivers_autoprobe = true;
449 450
	pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
	pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
451
	if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
452
		iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
453 454 455

	if (pdev)
		iov->dev = pci_dev_get(pdev);
456
	else
457
		iov->dev = dev;
458

459 460
	dev->sriov = iov;
	dev->is_physfn = 1;
461 462 463
	rc = compute_max_vf_buses(dev);
	if (rc)
		goto fail_max_buses;
464 465 466

	return 0;

467 468 469
fail_max_buses:
	dev->sriov = NULL;
	dev->is_physfn = 0;
470 471
failed:
	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
472
		res = &dev->resource[i + PCI_IOV_RESOURCES];
473 474 475
		res->flags = 0;
	}

476
	kfree(iov);
477 478 479 480 481
	return rc;
}

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

484
	if (dev != dev->sriov->dev)
485 486 487 488 489 490
		pci_dev_put(dev->sriov->dev);

	kfree(dev->sriov);
	dev->sriov = NULL;
}

Y
Yu Zhao 已提交
491 492 493 494 495 496 497 498 499 500
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;

501 502 503 504 505 506 507 508
	/*
	 * Restore PCI_SRIOV_CTRL_ARI before pci_iov_set_numvfs() because
	 * it reads offset & stride, which depend on PCI_SRIOV_CTRL_ARI.
	 */
	ctrl &= ~PCI_SRIOV_CTRL_ARI;
	ctrl |= iov->ctrl & PCI_SRIOV_CTRL_ARI;
	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, ctrl);

Y
Yu Zhao 已提交
509 510 511 512
	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);
513
	pci_iov_set_numvfs(dev, iov->num_VFs);
Y
Yu Zhao 已提交
514 515 516 517 518
	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
	if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
		msleep(100);
}

519 520 521 522 523 524 525 526 527 528
/**
 * 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;

529
	if (!pci_is_pcie(dev))
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
		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);
}

549 550 551 552 553 554 555 556 557 558 559 560 561
/**
 * pci_iov_update_resource - update a VF BAR
 * @dev: the PCI device
 * @resno: the resource number
 *
 * Update a VF BAR in the SR-IOV capability of a PF.
 */
void pci_iov_update_resource(struct pci_dev *dev, int resno)
{
	struct pci_sriov *iov = dev->is_physfn ? dev->sriov : NULL;
	struct resource *res = dev->resource + resno;
	int vf_bar = resno - PCI_IOV_RESOURCES;
	struct pci_bus_region region;
562
	u16 cmd;
563 564 565 566 567 568 569 570 571 572 573
	u32 new;
	int reg;

	/*
	 * The generic pci_restore_bars() path calls this for all devices,
	 * including VFs and non-SR-IOV devices.  If this is not a PF, we
	 * have nothing to do.
	 */
	if (!iov)
		return;

574 575 576 577 578 579 580
	pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &cmd);
	if ((cmd & PCI_SRIOV_CTRL_VFE) && (cmd & PCI_SRIOV_CTRL_MSE)) {
		dev_WARN(&dev->dev, "can't update enabled VF BAR%d %pR\n",
			 vf_bar, res);
		return;
	}

581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
	/*
	 * Ignore unimplemented BARs, unused resource slots for 64-bit
	 * BARs, and non-movable resources, e.g., those described via
	 * Enhanced Allocation.
	 */
	if (!res->flags)
		return;

	if (res->flags & IORESOURCE_UNSET)
		return;

	if (res->flags & IORESOURCE_PCI_FIXED)
		return;

	pcibios_resource_to_bus(dev->bus, &region, res);
	new = region.start;
	new |= res->flags & ~PCI_BASE_ADDRESS_MEM_MASK;

	reg = iov->pos + PCI_SRIOV_BAR + 4 * vf_bar;
	pci_write_config_dword(dev, reg, new);
	if (res->flags & IORESOURCE_MEM_64) {
		new = region.start >> 16 >> 16;
		pci_write_config_dword(dev, reg + 4, new);
	}
}

607 608 609 610 611 612
resource_size_t __weak pcibios_iov_resource_alignment(struct pci_dev *dev,
						      int resno)
{
	return pci_iov_resource_size(dev, resno);
}

613 614 615 616 617 618 619 620 621 622
/**
 * 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.
 */
623
resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
624
{
625
	return pcibios_iov_resource_alignment(dev, resno);
626 627
}

Y
Yu Zhao 已提交
628 629 630 631 632 633 634 635 636
/**
 * 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);
}
637

638 639 640 641 642 643 644 645 646 647 648
/**
 * pci_vf_drivers_autoprobe - set PF property drivers_autoprobe for VFs
 * @dev: the PCI device
 * @auto_probe: set VF drivers auto probe flag
 */
void pci_vf_drivers_autoprobe(struct pci_dev *dev, bool auto_probe)
{
	if (dev->is_physfn)
		dev->sriov->drivers_autoprobe = auto_probe;
}

649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
/**
 * 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;
	struct pci_dev *dev;

	list_for_each_entry(dev, &bus->devices, bus_list) {
		if (!dev->is_physfn)
			continue;
664 665
		if (dev->sriov->max_VF_buses > max)
			max = dev->sriov->max_VF_buses;
666 667 668 669
	}

	return max ? max - bus->number : 0;
}
670 671 672 673

/**
 * pci_enable_sriov - enable the SR-IOV capability
 * @dev: the PCI device
R
Randy Dunlap 已提交
674
 * @nr_virtfn: number of virtual functions to enable
675 676 677 678 679 680 681 682
 *
 * 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)
683
		return -ENOSYS;
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702

	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);
703

704 705 706 707 708 709 710 711
/**
 * 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 已提交
712
	if (!dev->is_physfn)
713
		return 0;
B
Bjorn Helgaas 已提交
714 715

	return dev->sriov->num_VFs;
716 717
}
EXPORT_SYMBOL_GPL(pci_num_vf);
718

719 720 721 722 723
/**
 * 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.
724
 * If device is not a physical function returns 0.
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
 */
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
	 */
740
	dev_id = dev->sriov->vf_device;
741 742 743 744 745 746 747 748 749

	/* 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) &&
750
			pci_is_dev_assigned(vfdev))
751 752 753 754 755 756 757 758 759
			vfs_assigned++;

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

	return vfs_assigned;
}
EXPORT_SYMBOL_GPL(pci_vfs_assigned);

760 761 762
/**
 * pci_sriov_set_totalvfs -- reduce the TotalVFs available
 * @dev: the PCI PF device
763
 * @numvfs: number that should be used for TotalVFs supported
764 765 766 767 768
 *
 * Should be called from PF driver's probe routine with
 * device's mutex held.
 *
 * Returns 0 if PF is an SRIOV-capable device and
769 770
 * value of numvfs valid. If not a PF return -ENOSYS;
 * if numvfs is invalid return -EINVAL;
771 772 773 774
 * if VFs already enabled, return -EBUSY.
 */
int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
{
775 776 777
	if (!dev->is_physfn)
		return -ENOSYS;
	if (numvfs > dev->sriov->total_VFs)
778 779 780 781 782 783
		return -EINVAL;

	/* Shouldn't change if VFs already enabled */
	if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
		return -EBUSY;
	else
784
		dev->sriov->driver_max_VFs = numvfs;
785 786 787 788 789 790

	return 0;
}
EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);

/**
J
Jonghwan Choi 已提交
791
 * pci_sriov_get_totalvfs -- get total VFs supported on this device
792 793 794
 * @dev: the PCI PF device
 *
 * For a PCIe device with SRIOV support, return the PCIe
795
 * SRIOV capability value of TotalVFs or the value of driver_max_VFs
796
 * if the driver reduced it.  Otherwise 0.
797 798 799
 */
int pci_sriov_get_totalvfs(struct pci_dev *dev)
{
B
Bjorn Helgaas 已提交
800
	if (!dev->is_physfn)
801
		return 0;
802

803 804
	if (dev->sriov->driver_max_VFs)
		return dev->sriov->driver_max_VFs;
B
Bjorn Helgaas 已提交
805 806

	return dev->sriov->total_VFs;
807 808
}
EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);