diff options
Diffstat (limited to 'drivers/pci/iov.c')
-rw-r--r-- | drivers/pci/iov.c | 155 |
1 files changed, 114 insertions, 41 deletions
diff --git a/drivers/pci/iov.c b/drivers/pci/iov.c index 4b3a4eaad996..ee0ebff103a4 100644 --- a/drivers/pci/iov.c +++ b/drivers/pci/iov.c | |||
@@ -19,16 +19,59 @@ | |||
19 | 19 | ||
20 | #define VIRTFN_ID_LEN 16 | 20 | #define VIRTFN_ID_LEN 16 |
21 | 21 | ||
22 | static inline u8 virtfn_bus(struct pci_dev *dev, int id) | 22 | int pci_iov_virtfn_bus(struct pci_dev *dev, int vf_id) |
23 | { | 23 | { |
24 | if (!dev->is_physfn) | ||
25 | return -EINVAL; | ||
24 | return dev->bus->number + ((dev->devfn + dev->sriov->offset + | 26 | return dev->bus->number + ((dev->devfn + dev->sriov->offset + |
25 | dev->sriov->stride * id) >> 8); | 27 | dev->sriov->stride * vf_id) >> 8); |
26 | } | 28 | } |
27 | 29 | ||
28 | static inline u8 virtfn_devfn(struct pci_dev *dev, int id) | 30 | int pci_iov_virtfn_devfn(struct pci_dev *dev, int vf_id) |
29 | { | 31 | { |
32 | if (!dev->is_physfn) | ||
33 | return -EINVAL; | ||
30 | return (dev->devfn + dev->sriov->offset + | 34 | return (dev->devfn + dev->sriov->offset + |
31 | dev->sriov->stride * id) & 0xff; | 35 | dev->sriov->stride * vf_id) & 0xff; |
36 | } | ||
37 | |||
38 | /* | ||
39 | * Per SR-IOV spec sec 3.3.10 and 3.3.11, First VF Offset and VF Stride may | ||
40 | * change when NumVFs changes. | ||
41 | * | ||
42 | * Update iov->offset and iov->stride when NumVFs is written. | ||
43 | */ | ||
44 | static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn) | ||
45 | { | ||
46 | struct pci_sriov *iov = dev->sriov; | ||
47 | |||
48 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn); | ||
49 | pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &iov->offset); | ||
50 | pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &iov->stride); | ||
51 | } | ||
52 | |||
53 | /* | ||
54 | * The PF consumes one bus number. NumVFs, First VF Offset, and VF Stride | ||
55 | * determine how many additional bus numbers will be consumed by VFs. | ||
56 | * | ||
57 | * Iterate over all valid NumVFs and calculate the maximum number of bus | ||
58 | * numbers that could ever be required. | ||
59 | */ | ||
60 | static inline u8 virtfn_max_buses(struct pci_dev *dev) | ||
61 | { | ||
62 | struct pci_sriov *iov = dev->sriov; | ||
63 | int nr_virtfn; | ||
64 | u8 max = 0; | ||
65 | int busnr; | ||
66 | |||
67 | for (nr_virtfn = 1; nr_virtfn <= iov->total_VFs; nr_virtfn++) { | ||
68 | pci_iov_set_numvfs(dev, nr_virtfn); | ||
69 | busnr = pci_iov_virtfn_bus(dev, nr_virtfn - 1); | ||
70 | if (busnr > max) | ||
71 | max = busnr; | ||
72 | } | ||
73 | |||
74 | return max; | ||
32 | } | 75 | } |
33 | 76 | ||
34 | static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr) | 77 | static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr) |
@@ -57,6 +100,14 @@ static void virtfn_remove_bus(struct pci_bus *physbus, struct pci_bus *virtbus) | |||
57 | pci_remove_bus(virtbus); | 100 | pci_remove_bus(virtbus); |
58 | } | 101 | } |
59 | 102 | ||
103 | resource_size_t pci_iov_resource_size(struct pci_dev *dev, int resno) | ||
104 | { | ||
105 | if (!dev->is_physfn) | ||
106 | return 0; | ||
107 | |||
108 | return dev->sriov->barsz[resno - PCI_IOV_RESOURCES]; | ||
109 | } | ||
110 | |||
60 | static int virtfn_add(struct pci_dev *dev, int id, int reset) | 111 | static int virtfn_add(struct pci_dev *dev, int id, int reset) |
61 | { | 112 | { |
62 | int i; | 113 | int i; |
@@ -69,7 +120,7 @@ static int virtfn_add(struct pci_dev *dev, int id, int reset) | |||
69 | struct pci_bus *bus; | 120 | struct pci_bus *bus; |
70 | 121 | ||
71 | mutex_lock(&iov->dev->sriov->lock); | 122 | mutex_lock(&iov->dev->sriov->lock); |
72 | bus = virtfn_add_bus(dev->bus, virtfn_bus(dev, id)); | 123 | bus = virtfn_add_bus(dev->bus, pci_iov_virtfn_bus(dev, id)); |
73 | if (!bus) | 124 | if (!bus) |
74 | goto failed; | 125 | goto failed; |
75 | 126 | ||
@@ -77,7 +128,7 @@ static int virtfn_add(struct pci_dev *dev, int id, int reset) | |||
77 | if (!virtfn) | 128 | if (!virtfn) |
78 | goto failed0; | 129 | goto failed0; |
79 | 130 | ||
80 | virtfn->devfn = virtfn_devfn(dev, id); | 131 | virtfn->devfn = pci_iov_virtfn_devfn(dev, id); |
81 | virtfn->vendor = dev->vendor; | 132 | virtfn->vendor = dev->vendor; |
82 | pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_DID, &virtfn->device); | 133 | pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_DID, &virtfn->device); |
83 | pci_setup_device(virtfn); | 134 | pci_setup_device(virtfn); |
@@ -87,13 +138,12 @@ static int virtfn_add(struct pci_dev *dev, int id, int reset) | |||
87 | virtfn->multifunction = 0; | 138 | virtfn->multifunction = 0; |
88 | 139 | ||
89 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { | 140 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { |
90 | res = dev->resource + PCI_IOV_RESOURCES + i; | 141 | res = &dev->resource[i + PCI_IOV_RESOURCES]; |
91 | if (!res->parent) | 142 | if (!res->parent) |
92 | continue; | 143 | continue; |
93 | virtfn->resource[i].name = pci_name(virtfn); | 144 | virtfn->resource[i].name = pci_name(virtfn); |
94 | virtfn->resource[i].flags = res->flags; | 145 | virtfn->resource[i].flags = res->flags; |
95 | size = resource_size(res); | 146 | size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES); |
96 | do_div(size, iov->total_VFs); | ||
97 | virtfn->resource[i].start = res->start + size * id; | 147 | virtfn->resource[i].start = res->start + size * id; |
98 | virtfn->resource[i].end = virtfn->resource[i].start + size - 1; | 148 | virtfn->resource[i].end = virtfn->resource[i].start + size - 1; |
99 | rc = request_resource(res, &virtfn->resource[i]); | 149 | rc = request_resource(res, &virtfn->resource[i]); |
@@ -140,8 +190,8 @@ static void virtfn_remove(struct pci_dev *dev, int id, int reset) | |||
140 | struct pci_sriov *iov = dev->sriov; | 190 | struct pci_sriov *iov = dev->sriov; |
141 | 191 | ||
142 | virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus), | 192 | virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus), |
143 | virtfn_bus(dev, id), | 193 | pci_iov_virtfn_bus(dev, id), |
144 | virtfn_devfn(dev, id)); | 194 | pci_iov_virtfn_devfn(dev, id)); |
145 | if (!virtfn) | 195 | if (!virtfn) |
146 | return; | 196 | return; |
147 | 197 | ||
@@ -170,6 +220,11 @@ static void virtfn_remove(struct pci_dev *dev, int id, int reset) | |||
170 | pci_dev_put(dev); | 220 | pci_dev_put(dev); |
171 | } | 221 | } |
172 | 222 | ||
223 | int __weak pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs) | ||
224 | { | ||
225 | return 0; | ||
226 | } | ||
227 | |||
173 | static int sriov_enable(struct pci_dev *dev, int nr_virtfn) | 228 | static int sriov_enable(struct pci_dev *dev, int nr_virtfn) |
174 | { | 229 | { |
175 | int rc; | 230 | int rc; |
@@ -180,6 +235,8 @@ static int sriov_enable(struct pci_dev *dev, int nr_virtfn) | |||
180 | struct pci_dev *pdev; | 235 | struct pci_dev *pdev; |
181 | struct pci_sriov *iov = dev->sriov; | 236 | struct pci_sriov *iov = dev->sriov; |
182 | int bars = 0; | 237 | int bars = 0; |
238 | int bus; | ||
239 | int retval; | ||
183 | 240 | ||
184 | if (!nr_virtfn) | 241 | if (!nr_virtfn) |
185 | return 0; | 242 | return 0; |
@@ -204,7 +261,7 @@ static int sriov_enable(struct pci_dev *dev, int nr_virtfn) | |||
204 | nres = 0; | 261 | nres = 0; |
205 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { | 262 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { |
206 | bars |= (1 << (i + PCI_IOV_RESOURCES)); | 263 | bars |= (1 << (i + PCI_IOV_RESOURCES)); |
207 | res = dev->resource + PCI_IOV_RESOURCES + i; | 264 | res = &dev->resource[i + PCI_IOV_RESOURCES]; |
208 | if (res->parent) | 265 | if (res->parent) |
209 | nres++; | 266 | nres++; |
210 | } | 267 | } |
@@ -216,8 +273,10 @@ static int sriov_enable(struct pci_dev *dev, int nr_virtfn) | |||
216 | iov->offset = offset; | 273 | iov->offset = offset; |
217 | iov->stride = stride; | 274 | iov->stride = stride; |
218 | 275 | ||
219 | if (virtfn_bus(dev, nr_virtfn - 1) > dev->bus->busn_res.end) { | 276 | bus = pci_iov_virtfn_bus(dev, nr_virtfn - 1); |
220 | dev_err(&dev->dev, "SR-IOV: bus number out of range\n"); | 277 | if (bus > dev->bus->busn_res.end) { |
278 | dev_err(&dev->dev, "can't enable %d VFs (bus %02x out of range of %pR)\n", | ||
279 | nr_virtfn, bus, &dev->bus->busn_res); | ||
221 | return -ENOMEM; | 280 | return -ENOMEM; |
222 | } | 281 | } |
223 | 282 | ||
@@ -243,7 +302,7 @@ static int sriov_enable(struct pci_dev *dev, int nr_virtfn) | |||
243 | return rc; | 302 | return rc; |
244 | } | 303 | } |
245 | 304 | ||
246 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn); | 305 | pci_iov_set_numvfs(dev, nr_virtfn); |
247 | iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE; | 306 | iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE; |
248 | pci_cfg_access_lock(dev); | 307 | pci_cfg_access_lock(dev); |
249 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); | 308 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); |
@@ -254,6 +313,12 @@ static int sriov_enable(struct pci_dev *dev, int nr_virtfn) | |||
254 | if (nr_virtfn < initial) | 313 | if (nr_virtfn < initial) |
255 | initial = nr_virtfn; | 314 | initial = nr_virtfn; |
256 | 315 | ||
316 | if ((retval = pcibios_sriov_enable(dev, initial))) { | ||
317 | dev_err(&dev->dev, "failure %d from pcibios_sriov_enable()\n", | ||
318 | retval); | ||
319 | return retval; | ||
320 | } | ||
321 | |||
257 | for (i = 0; i < initial; i++) { | 322 | for (i = 0; i < initial; i++) { |
258 | rc = virtfn_add(dev, i, 0); | 323 | rc = virtfn_add(dev, i, 0); |
259 | if (rc) | 324 | if (rc) |
@@ -272,7 +337,7 @@ failed: | |||
272 | iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE); | 337 | iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE); |
273 | pci_cfg_access_lock(dev); | 338 | pci_cfg_access_lock(dev); |
274 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); | 339 | 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); | 340 | pci_iov_set_numvfs(dev, 0); |
276 | ssleep(1); | 341 | ssleep(1); |
277 | pci_cfg_access_unlock(dev); | 342 | pci_cfg_access_unlock(dev); |
278 | 343 | ||
@@ -282,6 +347,11 @@ failed: | |||
282 | return rc; | 347 | return rc; |
283 | } | 348 | } |
284 | 349 | ||
350 | int __weak pcibios_sriov_disable(struct pci_dev *pdev) | ||
351 | { | ||
352 | return 0; | ||
353 | } | ||
354 | |||
285 | static void sriov_disable(struct pci_dev *dev) | 355 | static void sriov_disable(struct pci_dev *dev) |
286 | { | 356 | { |
287 | int i; | 357 | int i; |
@@ -293,6 +363,8 @@ static void sriov_disable(struct pci_dev *dev) | |||
293 | for (i = 0; i < iov->num_VFs; i++) | 363 | for (i = 0; i < iov->num_VFs; i++) |
294 | virtfn_remove(dev, i, 0); | 364 | virtfn_remove(dev, i, 0); |
295 | 365 | ||
366 | pcibios_sriov_disable(dev); | ||
367 | |||
296 | iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE); | 368 | iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE); |
297 | pci_cfg_access_lock(dev); | 369 | pci_cfg_access_lock(dev); |
298 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); | 370 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); |
@@ -303,12 +375,12 @@ static void sriov_disable(struct pci_dev *dev) | |||
303 | sysfs_remove_link(&dev->dev.kobj, "dep_link"); | 375 | sysfs_remove_link(&dev->dev.kobj, "dep_link"); |
304 | 376 | ||
305 | iov->num_VFs = 0; | 377 | iov->num_VFs = 0; |
306 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, 0); | 378 | pci_iov_set_numvfs(dev, 0); |
307 | } | 379 | } |
308 | 380 | ||
309 | static int sriov_init(struct pci_dev *dev, int pos) | 381 | static int sriov_init(struct pci_dev *dev, int pos) |
310 | { | 382 | { |
311 | int i; | 383 | int i, bar64; |
312 | int rc; | 384 | int rc; |
313 | int nres; | 385 | int nres; |
314 | u32 pgsz; | 386 | u32 pgsz; |
@@ -357,27 +429,29 @@ found: | |||
357 | pgsz &= ~(pgsz - 1); | 429 | pgsz &= ~(pgsz - 1); |
358 | pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz); | 430 | pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz); |
359 | 431 | ||
432 | iov = kzalloc(sizeof(*iov), GFP_KERNEL); | ||
433 | if (!iov) | ||
434 | return -ENOMEM; | ||
435 | |||
360 | nres = 0; | 436 | nres = 0; |
361 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { | 437 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { |
362 | res = dev->resource + PCI_IOV_RESOURCES + i; | 438 | res = &dev->resource[i + PCI_IOV_RESOURCES]; |
363 | i += __pci_read_base(dev, pci_bar_unknown, res, | 439 | bar64 = __pci_read_base(dev, pci_bar_unknown, res, |
364 | pos + PCI_SRIOV_BAR + i * 4); | 440 | pos + PCI_SRIOV_BAR + i * 4); |
365 | if (!res->flags) | 441 | if (!res->flags) |
366 | continue; | 442 | continue; |
367 | if (resource_size(res) & (PAGE_SIZE - 1)) { | 443 | if (resource_size(res) & (PAGE_SIZE - 1)) { |
368 | rc = -EIO; | 444 | rc = -EIO; |
369 | goto failed; | 445 | goto failed; |
370 | } | 446 | } |
447 | iov->barsz[i] = resource_size(res); | ||
371 | res->end = res->start + resource_size(res) * total - 1; | 448 | res->end = res->start + resource_size(res) * total - 1; |
449 | dev_info(&dev->dev, "VF(n) BAR%d space: %pR (contains BAR%d for %d VFs)\n", | ||
450 | i, res, i, total); | ||
451 | i += bar64; | ||
372 | nres++; | 452 | nres++; |
373 | } | 453 | } |
374 | 454 | ||
375 | iov = kzalloc(sizeof(*iov), GFP_KERNEL); | ||
376 | if (!iov) { | ||
377 | rc = -ENOMEM; | ||
378 | goto failed; | ||
379 | } | ||
380 | |||
381 | iov->pos = pos; | 455 | iov->pos = pos; |
382 | iov->nres = nres; | 456 | iov->nres = nres; |
383 | iov->ctrl = ctrl; | 457 | iov->ctrl = ctrl; |
@@ -400,15 +474,17 @@ found: | |||
400 | 474 | ||
401 | dev->sriov = iov; | 475 | dev->sriov = iov; |
402 | dev->is_physfn = 1; | 476 | dev->is_physfn = 1; |
477 | iov->max_VF_buses = virtfn_max_buses(dev); | ||
403 | 478 | ||
404 | return 0; | 479 | return 0; |
405 | 480 | ||
406 | failed: | 481 | failed: |
407 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { | 482 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { |
408 | res = dev->resource + PCI_IOV_RESOURCES + i; | 483 | res = &dev->resource[i + PCI_IOV_RESOURCES]; |
409 | res->flags = 0; | 484 | res->flags = 0; |
410 | } | 485 | } |
411 | 486 | ||
487 | kfree(iov); | ||
412 | return rc; | 488 | return rc; |
413 | } | 489 | } |
414 | 490 | ||
@@ -439,7 +515,7 @@ static void sriov_restore_state(struct pci_dev *dev) | |||
439 | pci_update_resource(dev, i); | 515 | pci_update_resource(dev, i); |
440 | 516 | ||
441 | pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz); | 517 | 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); | 518 | pci_iov_set_numvfs(dev, iov->num_VFs); |
443 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); | 519 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); |
444 | if (iov->ctrl & PCI_SRIOV_CTRL_VFE) | 520 | if (iov->ctrl & PCI_SRIOV_CTRL_VFE) |
445 | msleep(100); | 521 | msleep(100); |
@@ -493,6 +569,12 @@ int pci_iov_resource_bar(struct pci_dev *dev, int resno) | |||
493 | 4 * (resno - PCI_IOV_RESOURCES); | 569 | 4 * (resno - PCI_IOV_RESOURCES); |
494 | } | 570 | } |
495 | 571 | ||
572 | resource_size_t __weak pcibios_iov_resource_alignment(struct pci_dev *dev, | ||
573 | int resno) | ||
574 | { | ||
575 | return pci_iov_resource_size(dev, resno); | ||
576 | } | ||
577 | |||
496 | /** | 578 | /** |
497 | * pci_sriov_resource_alignment - get resource alignment for VF BAR | 579 | * pci_sriov_resource_alignment - get resource alignment for VF BAR |
498 | * @dev: the PCI device | 580 | * @dev: the PCI device |
@@ -505,14 +587,7 @@ int pci_iov_resource_bar(struct pci_dev *dev, int resno) | |||
505 | */ | 587 | */ |
506 | resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno) | 588 | resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno) |
507 | { | 589 | { |
508 | struct resource tmp; | 590 | return pcibios_iov_resource_alignment(dev, resno); |
509 | int reg = pci_iov_resource_bar(dev, resno); | ||
510 | |||
511 | if (!reg) | ||
512 | return 0; | ||
513 | |||
514 | __pci_read_base(dev, pci_bar_unknown, &tmp, reg); | ||
515 | return resource_alignment(&tmp); | ||
516 | } | 591 | } |
517 | 592 | ||
518 | /** | 593 | /** |
@@ -535,15 +610,13 @@ void pci_restore_iov_state(struct pci_dev *dev) | |||
535 | int pci_iov_bus_range(struct pci_bus *bus) | 610 | int pci_iov_bus_range(struct pci_bus *bus) |
536 | { | 611 | { |
537 | int max = 0; | 612 | int max = 0; |
538 | u8 busnr; | ||
539 | struct pci_dev *dev; | 613 | struct pci_dev *dev; |
540 | 614 | ||
541 | list_for_each_entry(dev, &bus->devices, bus_list) { | 615 | list_for_each_entry(dev, &bus->devices, bus_list) { |
542 | if (!dev->is_physfn) | 616 | if (!dev->is_physfn) |
543 | continue; | 617 | continue; |
544 | busnr = virtfn_bus(dev, dev->sriov->total_VFs - 1); | 618 | if (dev->sriov->max_VF_buses > max) |
545 | if (busnr > max) | 619 | max = dev->sriov->max_VF_buses; |
546 | max = busnr; | ||
547 | } | 620 | } |
548 | 621 | ||
549 | return max ? max - bus->number : 0; | 622 | return max ? max - bus->number : 0; |