blob: 5bca0e1a279945307f6b484257eefd044e810152 [file] [log] [blame]
Yu Zhaod1b054d2009-03-20 11:25:11 +08001/*
2 * drivers/pci/iov.c
3 *
4 * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com>
5 *
6 * PCI Express I/O Virtualization (IOV) support.
7 * Single Root IOV 1.0
Yu Zhao302b4212009-05-18 13:51:32 +08008 * Address Translation Service 1.0
Yu Zhaod1b054d2009-03-20 11:25:11 +08009 */
10
11#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Yu Zhaod1b054d2009-03-20 11:25:11 +080013#include <linux/mutex.h>
Paul Gortmaker363c75d2011-05-27 09:37:25 -040014#include <linux/export.h>
Yu Zhaod1b054d2009-03-20 11:25:11 +080015#include <linux/string.h>
16#include <linux/delay.h>
Joerg Roedel5cdede22011-04-04 15:55:18 +020017#include <linux/pci-ats.h>
Yu Zhaod1b054d2009-03-20 11:25:11 +080018#include "pci.h"
19
Yu Zhaodd7cc442009-03-20 11:25:15 +080020#define VIRTFN_ID_LEN 16
Yu Zhaod1b054d2009-03-20 11:25:11 +080021
Yu Zhaoa28724b2009-03-20 11:25:13 +080022static inline u8 virtfn_bus(struct pci_dev *dev, int id)
23{
24 return dev->bus->number + ((dev->devfn + dev->sriov->offset +
25 dev->sriov->stride * id) >> 8);
26}
27
28static inline u8 virtfn_devfn(struct pci_dev *dev, int id)
29{
30 return (dev->devfn + dev->sriov->offset +
31 dev->sriov->stride * id) & 0xff;
32}
33
Yu Zhaodd7cc442009-03-20 11:25:15 +080034static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
35{
Yu Zhaodd7cc442009-03-20 11:25:15 +080036 struct pci_bus *child;
37
38 if (bus->number == busnr)
39 return bus;
40
41 child = pci_find_bus(pci_domain_nr(bus), busnr);
42 if (child)
43 return child;
44
45 child = pci_add_new_bus(bus, NULL, busnr);
46 if (!child)
47 return NULL;
48
Yinghai Lub7eac052012-05-17 18:51:13 -070049 pci_bus_insert_busn_res(child, busnr, busnr);
Yu Zhaodd7cc442009-03-20 11:25:15 +080050
51 return child;
52}
53
Jiang Liudc087f22013-05-25 21:48:37 +080054static void virtfn_remove_bus(struct pci_bus *physbus, struct pci_bus *virtbus)
Yu Zhaodd7cc442009-03-20 11:25:15 +080055{
Jiang Liudc087f22013-05-25 21:48:37 +080056 if (physbus != virtbus && list_empty(&virtbus->devices))
57 pci_remove_bus(virtbus);
Yu Zhaodd7cc442009-03-20 11:25:15 +080058}
59
Wei Yang0e6c9122015-03-25 16:23:44 +080060resource_size_t pci_iov_resource_size(struct pci_dev *dev, int resno)
61{
62 if (!dev->is_physfn)
63 return 0;
64
65 return dev->sriov->barsz[resno - PCI_IOV_RESOURCES];
66}
67
Yu Zhaodd7cc442009-03-20 11:25:15 +080068static int virtfn_add(struct pci_dev *dev, int id, int reset)
69{
70 int i;
Jiang Liudc087f22013-05-25 21:48:37 +080071 int rc = -ENOMEM;
Yu Zhaodd7cc442009-03-20 11:25:15 +080072 u64 size;
73 char buf[VIRTFN_ID_LEN];
74 struct pci_dev *virtfn;
75 struct resource *res;
76 struct pci_sriov *iov = dev->sriov;
Gu Zheng8b1fce02013-05-25 21:48:31 +080077 struct pci_bus *bus;
Yu Zhaodd7cc442009-03-20 11:25:15 +080078
79 mutex_lock(&iov->dev->sriov->lock);
Gu Zheng8b1fce02013-05-25 21:48:31 +080080 bus = virtfn_add_bus(dev->bus, virtfn_bus(dev, id));
Jiang Liudc087f22013-05-25 21:48:37 +080081 if (!bus)
82 goto failed;
83
84 virtfn = pci_alloc_dev(bus);
85 if (!virtfn)
86 goto failed0;
87
Yu Zhaodd7cc442009-03-20 11:25:15 +080088 virtfn->devfn = virtfn_devfn(dev, id);
89 virtfn->vendor = dev->vendor;
90 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_DID, &virtfn->device);
91 pci_setup_device(virtfn);
92 virtfn->dev.parent = dev->dev.parent;
Xudong Haofbf33f52013-05-31 12:21:29 +080093 virtfn->physfn = pci_dev_get(dev);
94 virtfn->is_virtfn = 1;
Alex Williamsonaa9319772014-01-09 08:36:08 -070095 virtfn->multifunction = 0;
Yu Zhaodd7cc442009-03-20 11:25:15 +080096
97 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
98 res = dev->resource + PCI_IOV_RESOURCES + i;
99 if (!res->parent)
100 continue;
101 virtfn->resource[i].name = pci_name(virtfn);
102 virtfn->resource[i].flags = res->flags;
Wei Yang0e6c9122015-03-25 16:23:44 +0800103 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800104 virtfn->resource[i].start = res->start + size * id;
105 virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
106 rc = request_resource(res, &virtfn->resource[i]);
107 BUG_ON(rc);
108 }
109
110 if (reset)
Yu Zhao8c1c6992009-06-13 15:52:13 +0800111 __pci_reset_function(virtfn);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800112
113 pci_device_add(virtfn, virtfn->bus);
114 mutex_unlock(&iov->dev->sriov->lock);
115
Yijing Wangc893d132014-05-30 11:01:03 +0800116 pci_bus_add_device(virtfn);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800117 sprintf(buf, "virtfn%u", id);
118 rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
119 if (rc)
120 goto failed1;
121 rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
122 if (rc)
123 goto failed2;
124
125 kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
126
127 return 0;
128
129failed2:
130 sysfs_remove_link(&dev->dev.kobj, buf);
131failed1:
132 pci_dev_put(dev);
133 mutex_lock(&iov->dev->sriov->lock);
Yinghai Lu210647a2012-02-25 13:54:20 -0800134 pci_stop_and_remove_bus_device(virtfn);
Jiang Liudc087f22013-05-25 21:48:37 +0800135failed0:
136 virtfn_remove_bus(dev->bus, bus);
137failed:
Yu Zhaodd7cc442009-03-20 11:25:15 +0800138 mutex_unlock(&iov->dev->sriov->lock);
139
140 return rc;
141}
142
143static void virtfn_remove(struct pci_dev *dev, int id, int reset)
144{
145 char buf[VIRTFN_ID_LEN];
Yu Zhaodd7cc442009-03-20 11:25:15 +0800146 struct pci_dev *virtfn;
147 struct pci_sriov *iov = dev->sriov;
148
Jiang Liudc087f22013-05-25 21:48:37 +0800149 virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus),
150 virtfn_bus(dev, id),
151 virtfn_devfn(dev, id));
Yu Zhaodd7cc442009-03-20 11:25:15 +0800152 if (!virtfn)
153 return;
154
Yu Zhaodd7cc442009-03-20 11:25:15 +0800155 if (reset) {
156 device_release_driver(&virtfn->dev);
Yu Zhao8c1c6992009-06-13 15:52:13 +0800157 __pci_reset_function(virtfn);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800158 }
159
160 sprintf(buf, "virtfn%u", id);
161 sysfs_remove_link(&dev->dev.kobj, buf);
Yinghai Lu09cedbe2012-02-04 22:55:01 -0800162 /*
163 * pci_stop_dev() could have been called for this virtfn already,
164 * so the directory for the virtfn may have been removed before.
165 * Double check to avoid spurious sysfs warnings.
166 */
167 if (virtfn->dev.kobj.sd)
168 sysfs_remove_link(&virtfn->dev.kobj, "physfn");
Yu Zhaodd7cc442009-03-20 11:25:15 +0800169
170 mutex_lock(&iov->dev->sriov->lock);
Yinghai Lu210647a2012-02-25 13:54:20 -0800171 pci_stop_and_remove_bus_device(virtfn);
Jiang Liudc087f22013-05-25 21:48:37 +0800172 virtfn_remove_bus(dev->bus, virtfn->bus);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800173 mutex_unlock(&iov->dev->sriov->lock);
174
Jiang Liudc087f22013-05-25 21:48:37 +0800175 /* balance pci_get_domain_bus_and_slot() */
176 pci_dev_put(virtfn);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800177 pci_dev_put(dev);
178}
179
180static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
181{
182 int rc;
183 int i, j;
184 int nres;
185 u16 offset, stride, initial;
186 struct resource *res;
187 struct pci_dev *pdev;
188 struct pci_sriov *iov = dev->sriov;
Ram Paibbef98a2011-11-06 10:33:10 +0800189 int bars = 0;
Bjorn Helgaas68f8e9f2015-03-25 16:23:42 +0800190 u8 bus;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800191
192 if (!nr_virtfn)
193 return 0;
194
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700195 if (iov->num_VFs)
Yu Zhaodd7cc442009-03-20 11:25:15 +0800196 return -EINVAL;
197
198 pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700199 if (initial > iov->total_VFs ||
200 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
Yu Zhaodd7cc442009-03-20 11:25:15 +0800201 return -EIO;
202
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700203 if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
Yu Zhaodd7cc442009-03-20 11:25:15 +0800204 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
205 return -EINVAL;
206
Yu Zhaodd7cc442009-03-20 11:25:15 +0800207 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &offset);
208 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &stride);
209 if (!offset || (nr_virtfn > 1 && !stride))
210 return -EIO;
211
212 nres = 0;
213 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
Ram Paibbef98a2011-11-06 10:33:10 +0800214 bars |= (1 << (i + PCI_IOV_RESOURCES));
Yu Zhaodd7cc442009-03-20 11:25:15 +0800215 res = dev->resource + PCI_IOV_RESOURCES + i;
216 if (res->parent)
217 nres++;
218 }
219 if (nres != iov->nres) {
220 dev_err(&dev->dev, "not enough MMIO resources for SR-IOV\n");
221 return -ENOMEM;
222 }
223
224 iov->offset = offset;
225 iov->stride = stride;
226
Bjorn Helgaas68f8e9f2015-03-25 16:23:42 +0800227 bus = virtfn_bus(dev, nr_virtfn - 1);
228 if (bus > dev->bus->busn_res.end) {
229 dev_err(&dev->dev, "can't enable %d VFs (bus %02x out of range of %pR)\n",
230 nr_virtfn, bus, &dev->bus->busn_res);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800231 return -ENOMEM;
232 }
233
Ram Paibbef98a2011-11-06 10:33:10 +0800234 if (pci_enable_resources(dev, bars)) {
235 dev_err(&dev->dev, "SR-IOV: IOV BARS not allocated\n");
236 return -ENOMEM;
237 }
238
Yu Zhaodd7cc442009-03-20 11:25:15 +0800239 if (iov->link != dev->devfn) {
240 pdev = pci_get_slot(dev->bus, iov->link);
241 if (!pdev)
242 return -ENODEV;
243
Jiang Liudc087f22013-05-25 21:48:37 +0800244 if (!pdev->is_physfn) {
245 pci_dev_put(pdev);
Stefan Assmann652d1102013-07-31 16:47:56 -0600246 return -ENOSYS;
Jiang Liudc087f22013-05-25 21:48:37 +0800247 }
Yu Zhaodd7cc442009-03-20 11:25:15 +0800248
249 rc = sysfs_create_link(&dev->dev.kobj,
250 &pdev->dev.kobj, "dep_link");
Jiang Liudc087f22013-05-25 21:48:37 +0800251 pci_dev_put(pdev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800252 if (rc)
253 return rc;
254 }
255
Yijing Wang19b69842013-07-24 17:26:12 +0800256 pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800257 iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100258 pci_cfg_access_lock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800259 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
260 msleep(100);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100261 pci_cfg_access_unlock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800262
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700263 iov->initial_VFs = initial;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800264 if (nr_virtfn < initial)
265 initial = nr_virtfn;
266
267 for (i = 0; i < initial; i++) {
268 rc = virtfn_add(dev, i, 0);
269 if (rc)
270 goto failed;
271 }
272
273 kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700274 iov->num_VFs = nr_virtfn;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800275
276 return 0;
277
278failed:
279 for (j = 0; j < i; j++)
280 virtfn_remove(dev, j, 0);
281
282 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100283 pci_cfg_access_lock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800284 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
Yijing Wang19b69842013-07-24 17:26:12 +0800285 pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, 0);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800286 ssleep(1);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100287 pci_cfg_access_unlock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800288
289 if (iov->link != dev->devfn)
290 sysfs_remove_link(&dev->dev.kobj, "dep_link");
291
292 return rc;
293}
294
295static void sriov_disable(struct pci_dev *dev)
296{
297 int i;
298 struct pci_sriov *iov = dev->sriov;
299
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700300 if (!iov->num_VFs)
Yu Zhaodd7cc442009-03-20 11:25:15 +0800301 return;
302
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700303 for (i = 0; i < iov->num_VFs; i++)
Yu Zhaodd7cc442009-03-20 11:25:15 +0800304 virtfn_remove(dev, i, 0);
305
306 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100307 pci_cfg_access_lock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800308 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
309 ssleep(1);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100310 pci_cfg_access_unlock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800311
312 if (iov->link != dev->devfn)
313 sysfs_remove_link(&dev->dev.kobj, "dep_link");
314
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700315 iov->num_VFs = 0;
Yijing Wang19b69842013-07-24 17:26:12 +0800316 pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, 0);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800317}
318
Yu Zhaod1b054d2009-03-20 11:25:11 +0800319static int sriov_init(struct pci_dev *dev, int pos)
320{
Wei Yang0e6c9122015-03-25 16:23:44 +0800321 int i, bar64;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800322 int rc;
323 int nres;
324 u32 pgsz;
325 u16 ctrl, total, offset, stride;
326 struct pci_sriov *iov;
327 struct resource *res;
328 struct pci_dev *pdev;
329
Yijing Wang62f87c02012-07-24 17:20:03 +0800330 if (pci_pcie_type(dev) != PCI_EXP_TYPE_RC_END &&
331 pci_pcie_type(dev) != PCI_EXP_TYPE_ENDPOINT)
Yu Zhaod1b054d2009-03-20 11:25:11 +0800332 return -ENODEV;
333
334 pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
335 if (ctrl & PCI_SRIOV_CTRL_VFE) {
336 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
337 ssleep(1);
338 }
339
340 pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
341 if (!total)
342 return 0;
343
344 ctrl = 0;
345 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
346 if (pdev->is_physfn)
347 goto found;
348
349 pdev = NULL;
350 if (pci_ari_enabled(dev->bus))
351 ctrl |= PCI_SRIOV_CTRL_ARI;
352
353found:
354 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
ethan.zhao045cc222013-11-06 22:49:13 +0800355 pci_write_config_word(dev, pos + PCI_SRIOV_NUM_VF, 0);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800356 pci_read_config_word(dev, pos + PCI_SRIOV_VF_OFFSET, &offset);
357 pci_read_config_word(dev, pos + PCI_SRIOV_VF_STRIDE, &stride);
358 if (!offset || (total > 1 && !stride))
359 return -EIO;
360
361 pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
362 i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
363 pgsz &= ~((1 << i) - 1);
364 if (!pgsz)
365 return -EIO;
366
367 pgsz &= ~(pgsz - 1);
Vaidyanathan Srinivasan8161fe92012-02-02 23:11:20 +0530368 pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800369
Wei Yang0e6c9122015-03-25 16:23:44 +0800370 iov = kzalloc(sizeof(*iov), GFP_KERNEL);
371 if (!iov)
372 return -ENOMEM;
373
Yu Zhaod1b054d2009-03-20 11:25:11 +0800374 nres = 0;
375 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
376 res = dev->resource + PCI_IOV_RESOURCES + i;
Wei Yang0e6c9122015-03-25 16:23:44 +0800377 bar64 = __pci_read_base(dev, pci_bar_unknown, res,
378 pos + PCI_SRIOV_BAR + i * 4);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800379 if (!res->flags)
380 continue;
381 if (resource_size(res) & (PAGE_SIZE - 1)) {
382 rc = -EIO;
383 goto failed;
384 }
Wei Yang0e6c9122015-03-25 16:23:44 +0800385 iov->barsz[i] = resource_size(res);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800386 res->end = res->start + resource_size(res) * total - 1;
Wei Yange88ae012015-03-25 16:23:43 +0800387 dev_info(&dev->dev, "VF(n) BAR%d space: %pR (contains BAR%d for %d VFs)\n",
388 i, res, i, total);
Wei Yang0e6c9122015-03-25 16:23:44 +0800389 i += bar64;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800390 nres++;
391 }
392
Yu Zhaod1b054d2009-03-20 11:25:11 +0800393 iov->pos = pos;
394 iov->nres = nres;
395 iov->ctrl = ctrl;
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700396 iov->total_VFs = total;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800397 iov->offset = offset;
398 iov->stride = stride;
399 iov->pgsz = pgsz;
400 iov->self = dev;
401 pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
402 pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
Yijing Wang62f87c02012-07-24 17:20:03 +0800403 if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
Yu Zhao4d135db2009-05-20 17:11:57 +0800404 iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800405
406 if (pdev)
407 iov->dev = pci_dev_get(pdev);
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800408 else
Yu Zhaod1b054d2009-03-20 11:25:11 +0800409 iov->dev = dev;
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800410
411 mutex_init(&iov->lock);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800412
413 dev->sriov = iov;
414 dev->is_physfn = 1;
415
416 return 0;
417
418failed:
419 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
420 res = dev->resource + PCI_IOV_RESOURCES + i;
421 res->flags = 0;
422 }
423
Wei Yang0e6c9122015-03-25 16:23:44 +0800424 kfree(iov);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800425 return rc;
426}
427
428static void sriov_release(struct pci_dev *dev)
429{
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700430 BUG_ON(dev->sriov->num_VFs);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800431
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800432 if (dev != dev->sriov->dev)
Yu Zhaod1b054d2009-03-20 11:25:11 +0800433 pci_dev_put(dev->sriov->dev);
434
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800435 mutex_destroy(&dev->sriov->lock);
436
Yu Zhaod1b054d2009-03-20 11:25:11 +0800437 kfree(dev->sriov);
438 dev->sriov = NULL;
439}
440
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800441static void sriov_restore_state(struct pci_dev *dev)
442{
443 int i;
444 u16 ctrl;
445 struct pci_sriov *iov = dev->sriov;
446
447 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
448 if (ctrl & PCI_SRIOV_CTRL_VFE)
449 return;
450
451 for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++)
452 pci_update_resource(dev, i);
453
454 pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700455 pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, iov->num_VFs);
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800456 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
457 if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
458 msleep(100);
459}
460
Yu Zhaod1b054d2009-03-20 11:25:11 +0800461/**
462 * pci_iov_init - initialize the IOV capability
463 * @dev: the PCI device
464 *
465 * Returns 0 on success, or negative on failure.
466 */
467int pci_iov_init(struct pci_dev *dev)
468{
469 int pos;
470
Kenji Kaneshige5f4d91a2009-11-11 14:36:17 +0900471 if (!pci_is_pcie(dev))
Yu Zhaod1b054d2009-03-20 11:25:11 +0800472 return -ENODEV;
473
474 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
475 if (pos)
476 return sriov_init(dev, pos);
477
478 return -ENODEV;
479}
480
481/**
482 * pci_iov_release - release resources used by the IOV capability
483 * @dev: the PCI device
484 */
485void pci_iov_release(struct pci_dev *dev)
486{
487 if (dev->is_physfn)
488 sriov_release(dev);
489}
490
491/**
492 * pci_iov_resource_bar - get position of the SR-IOV BAR
493 * @dev: the PCI device
494 * @resno: the resource number
Yu Zhaod1b054d2009-03-20 11:25:11 +0800495 *
496 * Returns position of the BAR encapsulated in the SR-IOV capability.
497 */
Myron Stowe26ff46c2014-11-11 08:04:50 -0700498int pci_iov_resource_bar(struct pci_dev *dev, int resno)
Yu Zhaod1b054d2009-03-20 11:25:11 +0800499{
500 if (resno < PCI_IOV_RESOURCES || resno > PCI_IOV_RESOURCE_END)
501 return 0;
502
503 BUG_ON(!dev->is_physfn);
504
Yu Zhaod1b054d2009-03-20 11:25:11 +0800505 return dev->sriov->pos + PCI_SRIOV_BAR +
506 4 * (resno - PCI_IOV_RESOURCES);
507}
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800508
509/**
Chris Wright6faf17f2009-08-28 13:00:06 -0700510 * pci_sriov_resource_alignment - get resource alignment for VF BAR
511 * @dev: the PCI device
512 * @resno: the resource number
513 *
514 * Returns the alignment of the VF BAR found in the SR-IOV capability.
515 * This is not the same as the resource size which is defined as
516 * the VF BAR size multiplied by the number of VFs. The alignment
517 * is just the VF BAR size.
518 */
Cam Macdonell0e522472010-09-07 17:25:20 -0700519resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
Chris Wright6faf17f2009-08-28 13:00:06 -0700520{
Wei Yang0e6c9122015-03-25 16:23:44 +0800521 return pci_iov_resource_size(dev, resno);
Chris Wright6faf17f2009-08-28 13:00:06 -0700522}
523
524/**
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800525 * pci_restore_iov_state - restore the state of the IOV capability
526 * @dev: the PCI device
527 */
528void pci_restore_iov_state(struct pci_dev *dev)
529{
530 if (dev->is_physfn)
531 sriov_restore_state(dev);
532}
Yu Zhaoa28724b2009-03-20 11:25:13 +0800533
534/**
535 * pci_iov_bus_range - find bus range used by Virtual Function
536 * @bus: the PCI bus
537 *
538 * Returns max number of buses (exclude current one) used by Virtual
539 * Functions.
540 */
541int pci_iov_bus_range(struct pci_bus *bus)
542{
543 int max = 0;
544 u8 busnr;
545 struct pci_dev *dev;
546
547 list_for_each_entry(dev, &bus->devices, bus_list) {
548 if (!dev->is_physfn)
549 continue;
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700550 busnr = virtfn_bus(dev, dev->sriov->total_VFs - 1);
Yu Zhaoa28724b2009-03-20 11:25:13 +0800551 if (busnr > max)
552 max = busnr;
553 }
554
555 return max ? max - bus->number : 0;
556}
Yu Zhaodd7cc442009-03-20 11:25:15 +0800557
558/**
559 * pci_enable_sriov - enable the SR-IOV capability
560 * @dev: the PCI device
Randy Dunlap52a88732009-04-01 17:45:30 -0700561 * @nr_virtfn: number of virtual functions to enable
Yu Zhaodd7cc442009-03-20 11:25:15 +0800562 *
563 * Returns 0 on success, or negative on failure.
564 */
565int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
566{
567 might_sleep();
568
569 if (!dev->is_physfn)
Stefan Assmann652d1102013-07-31 16:47:56 -0600570 return -ENOSYS;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800571
572 return sriov_enable(dev, nr_virtfn);
573}
574EXPORT_SYMBOL_GPL(pci_enable_sriov);
575
576/**
577 * pci_disable_sriov - disable the SR-IOV capability
578 * @dev: the PCI device
579 */
580void pci_disable_sriov(struct pci_dev *dev)
581{
582 might_sleep();
583
584 if (!dev->is_physfn)
585 return;
586
587 sriov_disable(dev);
588}
589EXPORT_SYMBOL_GPL(pci_disable_sriov);
Yu Zhao74bb1bc2009-03-20 11:25:16 +0800590
591/**
Williams, Mitch Afb8a0d92010-02-10 01:43:04 +0000592 * pci_num_vf - return number of VFs associated with a PF device_release_driver
593 * @dev: the PCI device
594 *
595 * Returns number of VFs, or 0 if SR-IOV is not enabled.
596 */
597int pci_num_vf(struct pci_dev *dev)
598{
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700599 if (!dev->is_physfn)
Williams, Mitch Afb8a0d92010-02-10 01:43:04 +0000600 return 0;
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700601
602 return dev->sriov->num_VFs;
Williams, Mitch Afb8a0d92010-02-10 01:43:04 +0000603}
604EXPORT_SYMBOL_GPL(pci_num_vf);
Donald Dutilebff73152012-11-05 15:20:37 -0500605
606/**
Alexander Duyck5a8eb242013-04-25 04:42:29 +0000607 * pci_vfs_assigned - returns number of VFs are assigned to a guest
608 * @dev: the PCI device
609 *
610 * Returns number of VFs belonging to this device that are assigned to a guest.
Stefan Assmann652d1102013-07-31 16:47:56 -0600611 * If device is not a physical function returns 0.
Alexander Duyck5a8eb242013-04-25 04:42:29 +0000612 */
613int pci_vfs_assigned(struct pci_dev *dev)
614{
615 struct pci_dev *vfdev;
616 unsigned int vfs_assigned = 0;
617 unsigned short dev_id;
618
619 /* only search if we are a PF */
620 if (!dev->is_physfn)
621 return 0;
622
623 /*
624 * determine the device ID for the VFs, the vendor ID will be the
625 * same as the PF so there is no need to check for that one
626 */
627 pci_read_config_word(dev, dev->sriov->pos + PCI_SRIOV_VF_DID, &dev_id);
628
629 /* loop through all the VFs to see if we own any that are assigned */
630 vfdev = pci_get_device(dev->vendor, dev_id, NULL);
631 while (vfdev) {
632 /*
633 * It is considered assigned if it is a virtual function with
634 * our dev as the physical function and the assigned bit is set
635 */
636 if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
Ethan Zhaobe634972014-09-09 10:21:28 +0800637 pci_is_dev_assigned(vfdev))
Alexander Duyck5a8eb242013-04-25 04:42:29 +0000638 vfs_assigned++;
639
640 vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
641 }
642
643 return vfs_assigned;
644}
645EXPORT_SYMBOL_GPL(pci_vfs_assigned);
646
647/**
Donald Dutilebff73152012-11-05 15:20:37 -0500648 * pci_sriov_set_totalvfs -- reduce the TotalVFs available
649 * @dev: the PCI PF device
Randy Dunlap2094f162013-01-09 17:12:52 -0800650 * @numvfs: number that should be used for TotalVFs supported
Donald Dutilebff73152012-11-05 15:20:37 -0500651 *
652 * Should be called from PF driver's probe routine with
653 * device's mutex held.
654 *
655 * Returns 0 if PF is an SRIOV-capable device and
Stefan Assmann652d1102013-07-31 16:47:56 -0600656 * value of numvfs valid. If not a PF return -ENOSYS;
657 * if numvfs is invalid return -EINVAL;
Donald Dutilebff73152012-11-05 15:20:37 -0500658 * if VFs already enabled, return -EBUSY.
659 */
660int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
661{
Stefan Assmann652d1102013-07-31 16:47:56 -0600662 if (!dev->is_physfn)
663 return -ENOSYS;
664 if (numvfs > dev->sriov->total_VFs)
Donald Dutilebff73152012-11-05 15:20:37 -0500665 return -EINVAL;
666
667 /* Shouldn't change if VFs already enabled */
668 if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
669 return -EBUSY;
670 else
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700671 dev->sriov->driver_max_VFs = numvfs;
Donald Dutilebff73152012-11-05 15:20:37 -0500672
673 return 0;
674}
675EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
676
677/**
Jonghwan Choiddc191f2013-07-08 14:02:43 -0600678 * pci_sriov_get_totalvfs -- get total VFs supported on this device
Donald Dutilebff73152012-11-05 15:20:37 -0500679 * @dev: the PCI PF device
680 *
681 * For a PCIe device with SRIOV support, return the PCIe
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700682 * SRIOV capability value of TotalVFs or the value of driver_max_VFs
Stefan Assmann652d1102013-07-31 16:47:56 -0600683 * if the driver reduced it. Otherwise 0.
Donald Dutilebff73152012-11-05 15:20:37 -0500684 */
685int pci_sriov_get_totalvfs(struct pci_dev *dev)
686{
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700687 if (!dev->is_physfn)
Stefan Assmann652d1102013-07-31 16:47:56 -0600688 return 0;
Donald Dutilebff73152012-11-05 15:20:37 -0500689
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700690 if (dev->sriov->driver_max_VFs)
691 return dev->sriov->driver_max_VFs;
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700692
693 return dev->sriov->total_VFs;
Donald Dutilebff73152012-11-05 15:20:37 -0500694}
695EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);